1 /* Native-dependent code for OpenBSD/amd64. 2 3 Copyright (C) 2003-2017 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 3 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, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "gdbcore.h" 22 #include "regcache.h" 23 #include "target.h" 24 25 #include "amd64-tdep.h" 26 #include "amd64-nat.h" 27 #include "obsd-nat.h" 28 29 /* Mapping between the general-purpose registers in OpenBSD/amd64 30 `struct reg' format and GDB's register cache layout for 31 OpenBSD/i386. 32 33 Note that most (if not all) OpenBSD/amd64 registers are 64-bit, 34 while the OpenBSD/i386 registers are all 32-bit, but since we're 35 little-endian we get away with that. */ 36 37 /* From <machine/reg.h>. */ 38 static int amd64obsd32_r_reg_offset[] = 39 { 40 14 * 8, /* %eax */ 41 3 * 8, /* %ecx */ 42 2 * 8, /* %edx */ 43 13 * 8, /* %ebx */ 44 15 * 8, /* %esp */ 45 12 * 8, /* %ebp */ 46 1 * 8, /* %esi */ 47 0 * 8, /* %edi */ 48 16 * 8, /* %eip */ 49 17 * 8, /* %eflags */ 50 18 * 8, /* %cs */ 51 19 * 8, /* %ss */ 52 20 * 8, /* %ds */ 53 21 * 8, /* %es */ 54 22 * 8, /* %fs */ 55 23 * 8 /* %gs */ 56 }; 57 58 59 /* Support for debugging kernel virtual memory images. */ 60 61 #include <sys/types.h> 62 #include <machine/frame.h> 63 #include <machine/pcb.h> 64 65 #include "bsd-kvm.h" 66 67 static int 68 amd64obsd_supply_pcb (struct regcache *regcache, struct pcb *pcb) 69 { 70 struct switchframe sf; 71 int regnum; 72 73 /* The following is true for OpenBSD 3.5: 74 75 The pcb contains the stack pointer at the point of the context 76 switch in cpu_switch(). At that point we have a stack frame as 77 described by `struct switchframe', which for OpenBSD 3.5 has the 78 following layout: 79 80 interrupt level 81 %r15 82 %r14 83 %r13 84 %r12 85 %rbp 86 %rbx 87 return address 88 89 Together with %rsp in the pcb, this accounts for all callee-saved 90 registers specified by the psABI. From this information we 91 reconstruct the register state as it would look when we just 92 returned from cpu_switch(). 93 94 For core dumps the pcb is saved by savectx(). In that case the 95 stack frame only contains the return address, and there is no way 96 to recover the other registers. */ 97 98 /* The stack pointer shouldn't be zero. */ 99 if (pcb->pcb_rsp == 0) 100 return 0; 101 102 /* Read the stack frame, and check its validity. */ 103 read_memory (pcb->pcb_rsp, (gdb_byte *) &sf, sizeof sf); 104 if (sf.sf_rbp == pcb->pcb_rbp) 105 { 106 /* Yes, we have a frame that matches cpu_switch(). */ 107 pcb->pcb_rsp += sizeof (struct switchframe); 108 regcache_raw_supply (regcache, 12, &sf.sf_r12); 109 regcache_raw_supply (regcache, 13, &sf.sf_r13); 110 regcache_raw_supply (regcache, 14, &sf.sf_r14); 111 regcache_raw_supply (regcache, 15, &sf.sf_r15); 112 regcache_raw_supply (regcache, AMD64_RBX_REGNUM, &sf.sf_rbx); 113 regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &sf.sf_rip); 114 } 115 else 116 { 117 /* No, the pcb must have been last updated by savectx(). */ 118 pcb->pcb_rsp += 8; 119 regcache_raw_supply (regcache, AMD64_RIP_REGNUM, &sf); 120 } 121 122 regcache_raw_supply (regcache, AMD64_RSP_REGNUM, &pcb->pcb_rsp); 123 regcache_raw_supply (regcache, AMD64_RBP_REGNUM, &pcb->pcb_rbp); 124 125 return 1; 126 } 127 128 129 /* Provide a prototype to silence -Wmissing-prototypes. */ 130 void _initialize_amd64obsd_nat (void); 131 132 void 133 _initialize_amd64obsd_nat (void) 134 { 135 amd64_native_gregset32_reg_offset = amd64obsd32_r_reg_offset; 136 amd64_native_gregset32_num_regs = ARRAY_SIZE (amd64obsd32_r_reg_offset); 137 amd64_native_gregset64_reg_offset = amd64obsd_r_reg_offset; 138 139 /* Add some extra features to the common *BSD/amd64 target. */ 140 obsd_add_target (amd64bsd_target ()); 141 142 /* Support debugging kernel virtual memory images. */ 143 bsd_kvm_add_target (amd64obsd_supply_pcb); 144 } 145