1 /* Native-dependent code for OpenBSD/amd64.
2 
3    Copyright 2003, 2004 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21 
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 
27 #include "gdb_assert.h"
28 
29 #include "obsd-nat.h"
30 #include "amd64-tdep.h"
31 #include "amd64-nat.h"
32 
33 /* Mapping between the general-purpose registers in OpenBSD/amd64
34    `struct reg' format and GDB's register cache layout for
35    OpenBSD/i386.
36 
37    Note that most (if not all) OpenBSD/amd64 registers are 64-bit,
38    while the OpenBSD/i386 registers are all 32-bit, but since we're
39    little-endian we get away with that.  */
40 
41 /* From <machine/reg.h>.  */
42 static int amd64obsd32_r_reg_offset[] =
43 {
44   14 * 8,			/* %eax */
45   3 * 8,			/* %ecx */
46   2 * 8,			/* %edx */
47   13 * 8,			/* %ebx */
48   15 * 8,			/* %esp */
49   12 * 8,			/* %ebp */
50   1 * 8,			/* %esi */
51   0 * 8,			/* %edi */
52   16 * 8,			/* %eip */
53   17 * 8,			/* %eflags */
54   18 * 8,			/* %cs */
55   19 * 8,			/* %ss */
56   20 * 8,			/* %ds */
57   21 * 8,			/* %es */
58   22 * 8,			/* %fs */
59   23 * 8			/* %gs */
60 };
61 
62 
63 /* Support for debugging kernel virtual memory images.  */
64 
65 #include <sys/types.h>
66 #include <machine/frame.h>
67 #include <machine/pcb.h>
68 
69 #include "bsd-kvm.h"
70 
71 static int
amd64obsd_supply_pcb(struct regcache * regcache,struct pcb * pcb)72 amd64obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
73 {
74   struct switchframe sf;
75   int regnum;
76 
77   /* The following is true for OpenBSD 3.5:
78 
79      The pcb contains the stack pointer at the point of the context
80      switch in cpu_switch().  At that point we have a stack frame as
81      described by `struct switchframe', which for OpenBSD 3.5 has the
82      following layout:
83 
84      interrupt level
85      %r15
86      %r14
87      %r13
88      %r12
89      %rbp
90      %rbx
91      return address
92 
93      Together with %rsp in the pcb, this accounts for all callee-saved
94      registers specified by the psABI.  From this information we
95      reconstruct the register state as it would look when we just
96      returned from cpu_switch().
97 
98      For core dumps the pcb is saved by savectx().  In that case the
99      stack frame only contains the return address, and there is no way
100      to recover the other registers.  */
101 
102   /* The stack pointer shouldn't be zero.  */
103   if (pcb->pcb_rsp == 0)
104     return 0;
105 
106   /* Read the stack frame, and check its validity.  */
107   read_memory (pcb->pcb_rsp, (char *) &sf, sizeof sf);
108   if (sf.sf_rbp == pcb->pcb_rbp)
109     {
110       /* Yes, we have a frame that matches cpu_switch().  */
111       pcb->pcb_rsp += sizeof (struct switchframe);
112       regcache_raw_supply (regcache, 12, &sf.sf_r12);
113       regcache_raw_supply (regcache, 13, &sf.sf_r13);
114       regcache_raw_supply (regcache, 14, &sf.sf_r14);
115       regcache_raw_supply (regcache, 15, &sf.sf_r15);
116       regcache_raw_supply (regcache, AMD64_RBX_REGNUM, &sf.sf_rbx);
117       regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &sf.sf_rip);
118     }
119   else
120     {
121       /* No, the pcb must have been last updated by savectx().  */
122       pcb->pcb_rsp += 8;
123       regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &sf);
124     }
125 
126   regcache_raw_supply (regcache, AMD64_RSP_REGNUM, &pcb->pcb_rsp);
127   regcache_raw_supply (regcache, AMD64_RBP_REGNUM, &pcb->pcb_rbp);
128 
129   return 1;
130 }
131 
132 
133 /* Provide a prototype to silence -Wmissing-prototypes.  */
134 void _initialize_amd64obsd_nat (void);
135 
136 void
_initialize_amd64obsd_nat(void)137 _initialize_amd64obsd_nat (void)
138 {
139   struct target_ops *t;
140 
141   amd64_native_gregset32_reg_offset = amd64obsd32_r_reg_offset;
142   amd64_native_gregset32_num_regs = ARRAY_SIZE (amd64obsd32_r_reg_offset);
143   amd64_native_gregset64_reg_offset = amd64obsd_r_reg_offset;
144 
145   /* Add some extra features to the common *BSD/amd64 target.  */
146   t = amd64bsd_target ();
147   t->to_pid_to_str = obsd_pid_to_str;
148   t->to_find_new_threads = obsd_find_new_threads;
149   t->to_wait = obsd_wait;
150   add_target (t);
151 
152   /* Support debugging kernel virtual memory images.  */
153   bsd_kvm_add_target (amd64obsd_supply_pcb);
154 }
155