| 1 | /* Copyright (C) 2020-2021 Free Software Foundation, Inc. |
| 2 | |
| 3 | This file is part of GDB. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/ptrace.h> |
| 20 | #include <limits.h> |
| 21 | |
| 22 | #include "server.h" |
| 23 | #include "netbsd-low.h" |
| 24 | #include "gdbsupport/x86-xstate.h" |
| 25 | #include "arch/amd64.h" |
| 26 | #include "x86-tdesc.h" |
| 27 | #include "tdesc.h" |
| 28 | |
| 29 | /* The index of various registers inside the regcache. */ |
| 30 | |
| 31 | enum netbsd_x86_64_gdb_regnum |
| 32 | { |
| 33 | AMD64_RAX_REGNUM, /* %rax */ |
| 34 | AMD64_RBX_REGNUM, /* %rbx */ |
| 35 | AMD64_RCX_REGNUM, /* %rcx */ |
| 36 | AMD64_RDX_REGNUM, /* %rdx */ |
| 37 | AMD64_RSI_REGNUM, /* %rsi */ |
| 38 | AMD64_RDI_REGNUM, /* %rdi */ |
| 39 | AMD64_RBP_REGNUM, /* %rbp */ |
| 40 | AMD64_RSP_REGNUM, /* %rsp */ |
| 41 | AMD64_R8_REGNUM, /* %r8 */ |
| 42 | AMD64_R9_REGNUM, /* %r9 */ |
| 43 | AMD64_R10_REGNUM, /* %r10 */ |
| 44 | AMD64_R11_REGNUM, /* %r11 */ |
| 45 | AMD64_R12_REGNUM, /* %r12 */ |
| 46 | AMD64_R13_REGNUM, /* %r13 */ |
| 47 | AMD64_R14_REGNUM, /* %r14 */ |
| 48 | AMD64_R15_REGNUM, /* %r15 */ |
| 49 | AMD64_RIP_REGNUM, /* %rip */ |
| 50 | AMD64_EFLAGS_REGNUM, /* %eflags */ |
| 51 | AMD64_CS_REGNUM, /* %cs */ |
| 52 | AMD64_SS_REGNUM, /* %ss */ |
| 53 | AMD64_DS_REGNUM, /* %ds */ |
| 54 | AMD64_ES_REGNUM, /* %es */ |
| 55 | AMD64_FS_REGNUM, /* %fs */ |
| 56 | AMD64_GS_REGNUM, /* %gs */ |
| 57 | AMD64_ST0_REGNUM = 24, /* %st0 */ |
| 58 | AMD64_ST1_REGNUM, /* %st1 */ |
| 59 | AMD64_FCTRL_REGNUM = AMD64_ST0_REGNUM + 8, |
| 60 | AMD64_FSTAT_REGNUM = AMD64_ST0_REGNUM + 9, |
| 61 | AMD64_FTAG_REGNUM = AMD64_ST0_REGNUM + 10, |
| 62 | AMD64_XMM0_REGNUM = 40, /* %xmm0 */ |
| 63 | AMD64_XMM1_REGNUM, /* %xmm1 */ |
| 64 | AMD64_MXCSR_REGNUM = AMD64_XMM0_REGNUM + 16, |
| 65 | AMD64_YMM0H_REGNUM, /* %ymm0h */ |
| 66 | AMD64_YMM15H_REGNUM = AMD64_YMM0H_REGNUM + 15, |
| 67 | AMD64_BND0R_REGNUM = AMD64_YMM15H_REGNUM + 1, |
| 68 | AMD64_BND3R_REGNUM = AMD64_BND0R_REGNUM + 3, |
| 69 | AMD64_BNDCFGU_REGNUM, |
| 70 | AMD64_BNDSTATUS_REGNUM, |
| 71 | AMD64_XMM16_REGNUM, |
| 72 | AMD64_XMM31_REGNUM = AMD64_XMM16_REGNUM + 15, |
| 73 | AMD64_YMM16H_REGNUM, |
| 74 | AMD64_YMM31H_REGNUM = AMD64_YMM16H_REGNUM + 15, |
| 75 | AMD64_K0_REGNUM, |
| 76 | AMD64_K7_REGNUM = AMD64_K0_REGNUM + 7, |
| 77 | AMD64_ZMM0H_REGNUM, |
| 78 | AMD64_ZMM31H_REGNUM = AMD64_ZMM0H_REGNUM + 31, |
| 79 | AMD64_PKRU_REGNUM, |
| 80 | AMD64_FSBASE_REGNUM, |
| 81 | AMD64_GSBASE_REGNUM |
| 82 | }; |
| 83 | |
| 84 | /* The fill_function for the general-purpose register set. */ |
| 85 | |
| 86 | static void |
| 87 | netbsd_x86_64_fill_gregset (struct regcache *regcache, char *buf) |
| 88 | { |
| 89 | struct reg *r = (struct reg *) buf; |
| 90 | |
| 91 | #define netbsd_x86_64_collect_gp(regnum, fld) do { \ |
| 92 | collect_register (regcache, regnum, &r->regs[_REG_##fld]); \ |
| 93 | } while (0) |
| 94 | |
| 95 | netbsd_x86_64_collect_gp (AMD64_RAX_REGNUM, RAX); |
| 96 | netbsd_x86_64_collect_gp (AMD64_RBX_REGNUM, RBX); |
| 97 | netbsd_x86_64_collect_gp (AMD64_RCX_REGNUM, RCX); |
| 98 | netbsd_x86_64_collect_gp (AMD64_RDX_REGNUM, RDX); |
| 99 | netbsd_x86_64_collect_gp (AMD64_RSI_REGNUM, RSI); |
| 100 | netbsd_x86_64_collect_gp (AMD64_RDI_REGNUM, RDI); |
| 101 | netbsd_x86_64_collect_gp (AMD64_RBP_REGNUM, RBP); |
| 102 | netbsd_x86_64_collect_gp (AMD64_RSP_REGNUM, RSP); |
| 103 | netbsd_x86_64_collect_gp (AMD64_R8_REGNUM, R8); |
| 104 | netbsd_x86_64_collect_gp (AMD64_R9_REGNUM, R9); |
| 105 | netbsd_x86_64_collect_gp (AMD64_R10_REGNUM, R10); |
| 106 | netbsd_x86_64_collect_gp (AMD64_R11_REGNUM, R11); |
| 107 | netbsd_x86_64_collect_gp (AMD64_R12_REGNUM, R12); |
| 108 | netbsd_x86_64_collect_gp (AMD64_R13_REGNUM, R13); |
| 109 | netbsd_x86_64_collect_gp (AMD64_R14_REGNUM, R14); |
| 110 | netbsd_x86_64_collect_gp (AMD64_R15_REGNUM, R15); |
| 111 | netbsd_x86_64_collect_gp (AMD64_RIP_REGNUM, RIP); |
| 112 | netbsd_x86_64_collect_gp (AMD64_EFLAGS_REGNUM, RFLAGS); |
| 113 | netbsd_x86_64_collect_gp (AMD64_CS_REGNUM, CS); |
| 114 | netbsd_x86_64_collect_gp (AMD64_SS_REGNUM, SS); |
| 115 | netbsd_x86_64_collect_gp (AMD64_DS_REGNUM, DS); |
| 116 | netbsd_x86_64_collect_gp (AMD64_ES_REGNUM, ES); |
| 117 | netbsd_x86_64_collect_gp (AMD64_FS_REGNUM, FS); |
| 118 | netbsd_x86_64_collect_gp (AMD64_GS_REGNUM, GS); |
| 119 | } |
| 120 | |
| 121 | /* The store_function for the general-purpose register set. */ |
| 122 | |
| 123 | static void |
| 124 | netbsd_x86_64_store_gregset (struct regcache *regcache, const char *buf) |
| 125 | { |
| 126 | struct reg *r = (struct reg *) buf; |
| 127 | |
| 128 | #define netbsd_x86_64_supply_gp(regnum, fld) do { \ |
| 129 | supply_register (regcache, regnum, &r->regs[_REG_##fld]); \ |
| 130 | } while(0) |
| 131 | |
| 132 | netbsd_x86_64_supply_gp (AMD64_RAX_REGNUM, RAX); |
| 133 | netbsd_x86_64_supply_gp (AMD64_RBX_REGNUM, RBX); |
| 134 | netbsd_x86_64_supply_gp (AMD64_RCX_REGNUM, RCX); |
| 135 | netbsd_x86_64_supply_gp (AMD64_RDX_REGNUM, RDX); |
| 136 | netbsd_x86_64_supply_gp (AMD64_RSI_REGNUM, RSI); |
| 137 | netbsd_x86_64_supply_gp (AMD64_RDI_REGNUM, RDI); |
| 138 | netbsd_x86_64_supply_gp (AMD64_RBP_REGNUM, RBP); |
| 139 | netbsd_x86_64_supply_gp (AMD64_RSP_REGNUM, RSP); |
| 140 | netbsd_x86_64_supply_gp (AMD64_R8_REGNUM, R8); |
| 141 | netbsd_x86_64_supply_gp (AMD64_R9_REGNUM, R9); |
| 142 | netbsd_x86_64_supply_gp (AMD64_R10_REGNUM, R10); |
| 143 | netbsd_x86_64_supply_gp (AMD64_R11_REGNUM, R11); |
| 144 | netbsd_x86_64_supply_gp (AMD64_R12_REGNUM, R12); |
| 145 | netbsd_x86_64_supply_gp (AMD64_R13_REGNUM, R13); |
| 146 | netbsd_x86_64_supply_gp (AMD64_R14_REGNUM, R14); |
| 147 | netbsd_x86_64_supply_gp (AMD64_R15_REGNUM, R15); |
| 148 | netbsd_x86_64_supply_gp (AMD64_RIP_REGNUM, RIP); |
| 149 | netbsd_x86_64_supply_gp (AMD64_EFLAGS_REGNUM, RFLAGS); |
| 150 | netbsd_x86_64_supply_gp (AMD64_CS_REGNUM, CS); |
| 151 | netbsd_x86_64_supply_gp (AMD64_SS_REGNUM, SS); |
| 152 | netbsd_x86_64_supply_gp (AMD64_DS_REGNUM, DS); |
| 153 | netbsd_x86_64_supply_gp (AMD64_ES_REGNUM, ES); |
| 154 | netbsd_x86_64_supply_gp (AMD64_FS_REGNUM, FS); |
| 155 | netbsd_x86_64_supply_gp (AMD64_GS_REGNUM, GS); |
| 156 | } |
| 157 | |
| 158 | /* Description of all the x86-netbsd register sets. */ |
| 159 | |
| 160 | static const struct netbsd_regset_info netbsd_target_regsets[] = |
| 161 | { |
| 162 | /* General Purpose Registers. */ |
| 163 | {PT_GETREGS, PT_SETREGS, sizeof (struct reg), |
| 164 | netbsd_x86_64_fill_gregset, netbsd_x86_64_store_gregset}, |
| 165 | /* End of list marker. */ |
| 166 | {0, 0, -1, NULL, NULL } |
| 167 | }; |
| 168 | |
| 169 | /* NetBSD target op definitions for the amd64 architecture. */ |
| 170 | |
| 171 | class netbsd_amd64_target : public netbsd_process_target |
| 172 | { |
| 173 | protected: |
| 174 | const netbsd_regset_info *get_regs_info () override; |
| 175 | |
| 176 | void low_arch_setup () override; |
| 177 | }; |
| 178 | |
| 179 | /* Return the information to access registers. */ |
| 180 | |
| 181 | const netbsd_regset_info * |
| 182 | netbsd_amd64_target::get_regs_info () |
| 183 | { |
| 184 | return netbsd_target_regsets; |
| 185 | } |
| 186 | |
| 187 | /* Architecture-specific setup for the current process. */ |
| 188 | |
| 189 | void |
| 190 | netbsd_amd64_target::low_arch_setup () |
| 191 | { |
| 192 | target_desc *tdesc |
| 193 | = amd64_create_target_description (X86_XSTATE_SSE_MASK, false, false, false); |
| 194 | |
| 195 | init_target_desc (tdesc, amd64_expedite_regs); |
| 196 | |
| 197 | current_process ()->tdesc = tdesc; |
| 198 | } |
| 199 | |
| 200 | /* The singleton target ops object. */ |
| 201 | |
| 202 | static netbsd_amd64_target the_netbsd_amd64_target; |
| 203 | |
| 204 | /* The NetBSD target ops object. */ |
| 205 | |
| 206 | netbsd_process_target *the_netbsd_target = &the_netbsd_amd64_target; |