| 1 | /* Copyright (C) 2007-2020 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 "server.h" |
| 19 | #include "win32-low.h" |
| 20 | #include "x86-low.h" |
| 21 | #include "gdbsupport/x86-xstate.h" |
| 22 | #ifdef __x86_64__ |
| 23 | #include "arch/amd64.h" |
| 24 | #endif |
| 25 | #include "arch/i386.h" |
| 26 | #include "tdesc.h" |
| 27 | #include "x86-tdesc.h" |
| 28 | |
| 29 | using namespace windows_nat; |
| 30 | |
| 31 | #ifndef CONTEXT_EXTENDED_REGISTERS |
| 32 | #define CONTEXT_EXTENDED_REGISTERS 0 |
| 33 | #endif |
| 34 | |
| 35 | #define FCS_REGNUM 27 |
| 36 | #define FOP_REGNUM 31 |
| 37 | |
| 38 | #define FLAG_TRACE_BIT 0x100 |
| 39 | |
| 40 | static struct x86_debug_reg_state debug_reg_state; |
| 41 | |
| 42 | static void |
| 43 | update_debug_registers (thread_info *thread) |
| 44 | { |
| 45 | windows_thread_info *th = (windows_thread_info *) thread_target_data (thread); |
| 46 | |
| 47 | /* The actual update is done later just before resuming the lwp, |
| 48 | we just mark that the registers need updating. */ |
| 49 | th->debug_registers_changed = true; |
| 50 | } |
| 51 | |
| 52 | /* Update the inferior's debug register REGNUM from STATE. */ |
| 53 | |
| 54 | static void |
| 55 | x86_dr_low_set_addr (int regnum, CORE_ADDR addr) |
| 56 | { |
| 57 | gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR); |
| 58 | |
| 59 | /* Only update the threads of this process. */ |
| 60 | for_each_thread (current_thread->id.pid (), update_debug_registers); |
| 61 | } |
| 62 | |
| 63 | /* Update the inferior's DR7 debug control register from STATE. */ |
| 64 | |
| 65 | static void |
| 66 | x86_dr_low_set_control (unsigned long control) |
| 67 | { |
| 68 | /* Only update the threads of this process. */ |
| 69 | for_each_thread (current_thread->id.pid (), update_debug_registers); |
| 70 | } |
| 71 | |
| 72 | /* Return the current value of a DR register of the current thread's |
| 73 | context. */ |
| 74 | |
| 75 | static DWORD64 |
| 76 | win32_get_current_dr (int dr) |
| 77 | { |
| 78 | windows_thread_info *th |
| 79 | = (windows_thread_info *) thread_target_data (current_thread); |
| 80 | |
| 81 | win32_require_context (th); |
| 82 | |
| 83 | #define RET_DR(DR) \ |
| 84 | case DR: \ |
| 85 | return th->context.Dr ## DR |
| 86 | |
| 87 | switch (dr) |
| 88 | { |
| 89 | RET_DR (0); |
| 90 | RET_DR (1); |
| 91 | RET_DR (2); |
| 92 | RET_DR (3); |
| 93 | RET_DR (6); |
| 94 | RET_DR (7); |
| 95 | } |
| 96 | |
| 97 | #undef RET_DR |
| 98 | |
| 99 | gdb_assert_not_reached ("unhandled dr"); |
| 100 | } |
| 101 | |
| 102 | static CORE_ADDR |
| 103 | x86_dr_low_get_addr (int regnum) |
| 104 | { |
| 105 | gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR); |
| 106 | |
| 107 | return win32_get_current_dr (regnum - DR_FIRSTADDR); |
| 108 | } |
| 109 | |
| 110 | static unsigned long |
| 111 | x86_dr_low_get_control (void) |
| 112 | { |
| 113 | return win32_get_current_dr (7); |
| 114 | } |
| 115 | |
| 116 | /* Get the value of the DR6 debug status register from the inferior |
| 117 | and record it in STATE. */ |
| 118 | |
| 119 | static unsigned long |
| 120 | x86_dr_low_get_status (void) |
| 121 | { |
| 122 | return win32_get_current_dr (6); |
| 123 | } |
| 124 | |
| 125 | /* Low-level function vector. */ |
| 126 | struct x86_dr_low_type x86_dr_low = |
| 127 | { |
| 128 | x86_dr_low_set_control, |
| 129 | x86_dr_low_set_addr, |
| 130 | x86_dr_low_get_addr, |
| 131 | x86_dr_low_get_status, |
| 132 | x86_dr_low_get_control, |
| 133 | sizeof (void *), |
| 134 | }; |
| 135 | |
| 136 | /* Breakpoint/watchpoint support. */ |
| 137 | |
| 138 | static int |
| 139 | i386_supports_z_point_type (char z_type) |
| 140 | { |
| 141 | switch (z_type) |
| 142 | { |
| 143 | case Z_PACKET_WRITE_WP: |
| 144 | case Z_PACKET_ACCESS_WP: |
| 145 | return 1; |
| 146 | default: |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | i386_insert_point (enum raw_bkpt_type type, CORE_ADDR addr, |
| 153 | int size, struct raw_breakpoint *bp) |
| 154 | { |
| 155 | switch (type) |
| 156 | { |
| 157 | case raw_bkpt_type_write_wp: |
| 158 | case raw_bkpt_type_access_wp: |
| 159 | { |
| 160 | enum target_hw_bp_type hw_type |
| 161 | = raw_bkpt_type_to_target_hw_bp_type (type); |
| 162 | |
| 163 | return x86_dr_insert_watchpoint (&debug_reg_state, |
| 164 | hw_type, addr, size); |
| 165 | } |
| 166 | default: |
| 167 | /* Unsupported. */ |
| 168 | return 1; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static int |
| 173 | i386_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, |
| 174 | int size, struct raw_breakpoint *bp) |
| 175 | { |
| 176 | switch (type) |
| 177 | { |
| 178 | case raw_bkpt_type_write_wp: |
| 179 | case raw_bkpt_type_access_wp: |
| 180 | { |
| 181 | enum target_hw_bp_type hw_type |
| 182 | = raw_bkpt_type_to_target_hw_bp_type (type); |
| 183 | |
| 184 | return x86_dr_remove_watchpoint (&debug_reg_state, |
| 185 | hw_type, addr, size); |
| 186 | } |
| 187 | default: |
| 188 | /* Unsupported. */ |
| 189 | return 1; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static int |
| 194 | x86_stopped_by_watchpoint (void) |
| 195 | { |
| 196 | return x86_dr_stopped_by_watchpoint (&debug_reg_state); |
| 197 | } |
| 198 | |
| 199 | static CORE_ADDR |
| 200 | x86_stopped_data_address (void) |
| 201 | { |
| 202 | CORE_ADDR addr; |
| 203 | if (x86_dr_stopped_data_address (&debug_reg_state, &addr)) |
| 204 | return addr; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | i386_initial_stuff (void) |
| 210 | { |
| 211 | x86_low_init_dregs (&debug_reg_state); |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | i386_get_thread_context (windows_thread_info *th) |
| 216 | { |
| 217 | /* Requesting the CONTEXT_EXTENDED_REGISTERS register set fails if |
| 218 | the system doesn't support extended registers. */ |
| 219 | static DWORD extended_registers = CONTEXT_EXTENDED_REGISTERS; |
| 220 | |
| 221 | again: |
| 222 | th->context.ContextFlags = (CONTEXT_FULL |
| 223 | | CONTEXT_FLOATING_POINT |
| 224 | | CONTEXT_DEBUG_REGISTERS |
| 225 | | extended_registers); |
| 226 | |
| 227 | if (!GetThreadContext (th->h, &th->context)) |
| 228 | { |
| 229 | DWORD e = GetLastError (); |
| 230 | |
| 231 | if (extended_registers && e == ERROR_INVALID_PARAMETER) |
| 232 | { |
| 233 | extended_registers = 0; |
| 234 | goto again; |
| 235 | } |
| 236 | |
| 237 | error ("GetThreadContext failure %ld\n", (long) e); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | i386_prepare_to_resume (windows_thread_info *th) |
| 243 | { |
| 244 | if (th->debug_registers_changed) |
| 245 | { |
| 246 | struct x86_debug_reg_state *dr = &debug_reg_state; |
| 247 | |
| 248 | win32_require_context (th); |
| 249 | |
| 250 | th->context.Dr0 = dr->dr_mirror[0]; |
| 251 | th->context.Dr1 = dr->dr_mirror[1]; |
| 252 | th->context.Dr2 = dr->dr_mirror[2]; |
| 253 | th->context.Dr3 = dr->dr_mirror[3]; |
| 254 | /* th->context.Dr6 = dr->dr_status_mirror; |
| 255 | FIXME: should we set dr6 also ?? */ |
| 256 | th->context.Dr7 = dr->dr_control_mirror; |
| 257 | |
| 258 | th->debug_registers_changed = false; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | i386_thread_added (windows_thread_info *th) |
| 264 | { |
| 265 | th->debug_registers_changed = true; |
| 266 | } |
| 267 | |
| 268 | static void |
| 269 | i386_single_step (windows_thread_info *th) |
| 270 | { |
| 271 | th->context.EFlags |= FLAG_TRACE_BIT; |
| 272 | } |
| 273 | |
| 274 | #ifndef __x86_64__ |
| 275 | |
| 276 | /* An array of offset mappings into a Win32 Context structure. |
| 277 | This is a one-to-one mapping which is indexed by gdb's register |
| 278 | numbers. It retrieves an offset into the context structure where |
| 279 | the 4 byte register is located. |
| 280 | An offset value of -1 indicates that Win32 does not provide this |
| 281 | register in it's CONTEXT structure. In this case regptr will return |
| 282 | a pointer into a dummy register. */ |
| 283 | #define context_offset(x) ((int)&(((CONTEXT *)NULL)->x)) |
| 284 | static const int mappings[] = { |
| 285 | context_offset (Eax), |
| 286 | context_offset (Ecx), |
| 287 | context_offset (Edx), |
| 288 | context_offset (Ebx), |
| 289 | context_offset (Esp), |
| 290 | context_offset (Ebp), |
| 291 | context_offset (Esi), |
| 292 | context_offset (Edi), |
| 293 | context_offset (Eip), |
| 294 | context_offset (EFlags), |
| 295 | context_offset (SegCs), |
| 296 | context_offset (SegSs), |
| 297 | context_offset (SegDs), |
| 298 | context_offset (SegEs), |
| 299 | context_offset (SegFs), |
| 300 | context_offset (SegGs), |
| 301 | context_offset (FloatSave.RegisterArea[0 * 10]), |
| 302 | context_offset (FloatSave.RegisterArea[1 * 10]), |
| 303 | context_offset (FloatSave.RegisterArea[2 * 10]), |
| 304 | context_offset (FloatSave.RegisterArea[3 * 10]), |
| 305 | context_offset (FloatSave.RegisterArea[4 * 10]), |
| 306 | context_offset (FloatSave.RegisterArea[5 * 10]), |
| 307 | context_offset (FloatSave.RegisterArea[6 * 10]), |
| 308 | context_offset (FloatSave.RegisterArea[7 * 10]), |
| 309 | context_offset (FloatSave.ControlWord), |
| 310 | context_offset (FloatSave.StatusWord), |
| 311 | context_offset (FloatSave.TagWord), |
| 312 | context_offset (FloatSave.ErrorSelector), |
| 313 | context_offset (FloatSave.ErrorOffset), |
| 314 | context_offset (FloatSave.DataSelector), |
| 315 | context_offset (FloatSave.DataOffset), |
| 316 | context_offset (FloatSave.ErrorSelector), |
| 317 | /* XMM0-7 */ |
| 318 | context_offset (ExtendedRegisters[10 * 16]), |
| 319 | context_offset (ExtendedRegisters[11 * 16]), |
| 320 | context_offset (ExtendedRegisters[12 * 16]), |
| 321 | context_offset (ExtendedRegisters[13 * 16]), |
| 322 | context_offset (ExtendedRegisters[14 * 16]), |
| 323 | context_offset (ExtendedRegisters[15 * 16]), |
| 324 | context_offset (ExtendedRegisters[16 * 16]), |
| 325 | context_offset (ExtendedRegisters[17 * 16]), |
| 326 | /* MXCSR */ |
| 327 | context_offset (ExtendedRegisters[24]) |
| 328 | }; |
| 329 | #undef context_offset |
| 330 | |
| 331 | #else /* __x86_64__ */ |
| 332 | |
| 333 | #define context_offset(x) (offsetof (CONTEXT, x)) |
| 334 | static const int mappings[] = |
| 335 | { |
| 336 | context_offset (Rax), |
| 337 | context_offset (Rbx), |
| 338 | context_offset (Rcx), |
| 339 | context_offset (Rdx), |
| 340 | context_offset (Rsi), |
| 341 | context_offset (Rdi), |
| 342 | context_offset (Rbp), |
| 343 | context_offset (Rsp), |
| 344 | context_offset (R8), |
| 345 | context_offset (R9), |
| 346 | context_offset (R10), |
| 347 | context_offset (R11), |
| 348 | context_offset (R12), |
| 349 | context_offset (R13), |
| 350 | context_offset (R14), |
| 351 | context_offset (R15), |
| 352 | context_offset (Rip), |
| 353 | context_offset (EFlags), |
| 354 | context_offset (SegCs), |
| 355 | context_offset (SegSs), |
| 356 | context_offset (SegDs), |
| 357 | context_offset (SegEs), |
| 358 | context_offset (SegFs), |
| 359 | context_offset (SegGs), |
| 360 | context_offset (FloatSave.FloatRegisters[0]), |
| 361 | context_offset (FloatSave.FloatRegisters[1]), |
| 362 | context_offset (FloatSave.FloatRegisters[2]), |
| 363 | context_offset (FloatSave.FloatRegisters[3]), |
| 364 | context_offset (FloatSave.FloatRegisters[4]), |
| 365 | context_offset (FloatSave.FloatRegisters[5]), |
| 366 | context_offset (FloatSave.FloatRegisters[6]), |
| 367 | context_offset (FloatSave.FloatRegisters[7]), |
| 368 | context_offset (FloatSave.ControlWord), |
| 369 | context_offset (FloatSave.StatusWord), |
| 370 | context_offset (FloatSave.TagWord), |
| 371 | context_offset (FloatSave.ErrorSelector), |
| 372 | context_offset (FloatSave.ErrorOffset), |
| 373 | context_offset (FloatSave.DataSelector), |
| 374 | context_offset (FloatSave.DataOffset), |
| 375 | context_offset (FloatSave.ErrorSelector) |
| 376 | /* XMM0-7 */ , |
| 377 | context_offset (Xmm0), |
| 378 | context_offset (Xmm1), |
| 379 | context_offset (Xmm2), |
| 380 | context_offset (Xmm3), |
| 381 | context_offset (Xmm4), |
| 382 | context_offset (Xmm5), |
| 383 | context_offset (Xmm6), |
| 384 | context_offset (Xmm7), |
| 385 | context_offset (Xmm8), |
| 386 | context_offset (Xmm9), |
| 387 | context_offset (Xmm10), |
| 388 | context_offset (Xmm11), |
| 389 | context_offset (Xmm12), |
| 390 | context_offset (Xmm13), |
| 391 | context_offset (Xmm14), |
| 392 | context_offset (Xmm15), |
| 393 | /* MXCSR */ |
| 394 | context_offset (FloatSave.MxCsr) |
| 395 | }; |
| 396 | #undef context_offset |
| 397 | |
| 398 | #endif /* __x86_64__ */ |
| 399 | |
| 400 | /* Fetch register from gdbserver regcache data. */ |
| 401 | static void |
| 402 | i386_fetch_inferior_register (struct regcache *regcache, |
| 403 | windows_thread_info *th, int r) |
| 404 | { |
| 405 | char *context_offset = (char *) &th->context + mappings[r]; |
| 406 | |
| 407 | long l; |
| 408 | if (r == FCS_REGNUM) |
| 409 | { |
| 410 | l = *((long *) context_offset) & 0xffff; |
| 411 | supply_register (regcache, r, (char *) &l); |
| 412 | } |
| 413 | else if (r == FOP_REGNUM) |
| 414 | { |
| 415 | l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1); |
| 416 | supply_register (regcache, r, (char *) &l); |
| 417 | } |
| 418 | else |
| 419 | supply_register (regcache, r, context_offset); |
| 420 | } |
| 421 | |
| 422 | /* Store a new register value into the thread context of TH. */ |
| 423 | static void |
| 424 | i386_store_inferior_register (struct regcache *regcache, |
| 425 | windows_thread_info *th, int r) |
| 426 | { |
| 427 | char *context_offset = (char *) &th->context + mappings[r]; |
| 428 | collect_register (regcache, r, context_offset); |
| 429 | } |
| 430 | |
| 431 | static const unsigned char i386_win32_breakpoint = 0xcc; |
| 432 | #define i386_win32_breakpoint_len 1 |
| 433 | |
| 434 | static void |
| 435 | i386_arch_setup (void) |
| 436 | { |
| 437 | struct target_desc *tdesc; |
| 438 | |
| 439 | #ifdef __x86_64__ |
| 440 | tdesc = amd64_create_target_description (X86_XSTATE_SSE_MASK, false, |
| 441 | false, false); |
| 442 | const char **expedite_regs = amd64_expedite_regs; |
| 443 | #else |
| 444 | tdesc = i386_create_target_description (X86_XSTATE_SSE_MASK, false, false); |
| 445 | const char **expedite_regs = i386_expedite_regs; |
| 446 | #endif |
| 447 | |
| 448 | init_target_desc (tdesc, expedite_regs); |
| 449 | |
| 450 | win32_tdesc = tdesc; |
| 451 | } |
| 452 | |
| 453 | struct win32_target_ops the_low_target = { |
| 454 | i386_arch_setup, |
| 455 | sizeof (mappings) / sizeof (mappings[0]), |
| 456 | i386_initial_stuff, |
| 457 | i386_get_thread_context, |
| 458 | i386_prepare_to_resume, |
| 459 | i386_thread_added, |
| 460 | i386_fetch_inferior_register, |
| 461 | i386_store_inferior_register, |
| 462 | i386_single_step, |
| 463 | &i386_win32_breakpoint, |
| 464 | i386_win32_breakpoint_len, |
| 465 | i386_supports_z_point_type, |
| 466 | i386_insert_point, |
| 467 | i386_remove_point, |
| 468 | x86_stopped_by_watchpoint, |
| 469 | x86_stopped_data_address |
| 470 | }; |