1 /* Low level interface to i386 running the GNU Hurd. 2 3 Copyright (C) 1992-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 /* Mach/Hurd headers are not yet ready for C++ compilation. */ 21 extern "C" 22 { 23 #include <mach.h> 24 #include <mach_error.h> 25 #include <mach/message.h> 26 #include <mach/exception.h> 27 } 28 29 #include "defs.h" 30 #include "x86-nat.h" 31 #include "inferior.h" 32 #include "floatformat.h" 33 #include "regcache.h" 34 35 #include "i386-tdep.h" 36 37 #include "gnu-nat.h" 38 #include "inf-child.h" 39 #include "i387-tdep.h" 40 41 /* Offset to the thread_state_t location where REG is stored. */ 42 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg) 43 44 /* At REG_OFFSET[N] is the offset to the thread_state_t location where 45 the GDB register N is stored. */ 46 static int reg_offset[] = 47 { 48 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx), 49 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi), 50 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss), 51 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs) 52 }; 53 54 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum]) 55 56 57 /* Get the whole floating-point state of THREAD and record the values 58 of the corresponding (pseudo) registers. */ 59 60 static void 61 fetch_fpregs (struct regcache *regcache, struct proc *thread) 62 { 63 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT; 64 struct i386_float_state state; 65 kern_return_t err; 66 67 err = thread_get_state (thread->port, i386_FLOAT_STATE, 68 (thread_state_t) &state, &count); 69 if (err) 70 { 71 warning (_("Couldn't fetch floating-point state from %s"), 72 proc_string (thread)); 73 return; 74 } 75 76 if (!state.initialized) 77 { 78 /* The floating-point state isn't initialized. */ 79 i387_supply_fsave (regcache, -1, NULL); 80 } 81 else 82 { 83 /* Supply the floating-point registers. */ 84 i387_supply_fsave (regcache, -1, state.hw_state); 85 } 86 } 87 88 /* Fetch register REGNO, or all regs if REGNO is -1. */ 89 static void 90 gnu_fetch_registers (struct target_ops *ops, 91 struct regcache *regcache, int regno) 92 { 93 struct proc *thread; 94 ptid_t ptid = regcache_get_ptid (regcache); 95 96 /* Make sure we know about new threads. */ 97 inf_update_procs (gnu_current_inf); 98 99 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid)); 100 if (!thread) 101 error (_("Can't fetch registers from thread %s: No such thread"), 102 target_pid_to_str (ptid)); 103 104 if (regno < I386_NUM_GREGS || regno == -1) 105 { 106 thread_state_t state; 107 108 /* This does the dirty work for us. */ 109 state = proc_get_state (thread, 0); 110 if (!state) 111 { 112 warning (_("Couldn't fetch registers from %s"), 113 proc_string (thread)); 114 return; 115 } 116 117 if (regno == -1) 118 { 119 int i; 120 121 proc_debug (thread, "fetching all register"); 122 123 for (i = 0; i < I386_NUM_GREGS; i++) 124 regcache_raw_supply (regcache, i, REG_ADDR (state, i)); 125 thread->fetched_regs = ~0; 126 } 127 else 128 { 129 proc_debug (thread, "fetching register %s", 130 gdbarch_register_name (get_regcache_arch (regcache), 131 regno)); 132 133 regcache_raw_supply (regcache, regno, 134 REG_ADDR (state, regno)); 135 thread->fetched_regs |= (1 << regno); 136 } 137 } 138 139 if (regno >= I386_NUM_GREGS || regno == -1) 140 { 141 proc_debug (thread, "fetching floating-point registers"); 142 143 fetch_fpregs (regcache, thread); 144 } 145 } 146 147 148 /* Store the whole floating-point state into THREAD using information 149 from the corresponding (pseudo) registers. */ 150 static void 151 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno) 152 { 153 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT; 154 struct i386_float_state state; 155 kern_return_t err; 156 157 err = thread_get_state (thread->port, i386_FLOAT_STATE, 158 (thread_state_t) &state, &count); 159 if (err) 160 { 161 warning (_("Couldn't fetch floating-point state from %s"), 162 proc_string (thread)); 163 return; 164 } 165 166 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow 167 take into account DEPRECATED_REGISTER_VALID like the old code did? */ 168 i387_collect_fsave (regcache, regno, state.hw_state); 169 170 err = thread_set_state (thread->port, i386_FLOAT_STATE, 171 (thread_state_t) &state, i386_FLOAT_STATE_COUNT); 172 if (err) 173 { 174 warning (_("Couldn't store floating-point state into %s"), 175 proc_string (thread)); 176 return; 177 } 178 } 179 180 /* Store at least register REGNO, or all regs if REGNO == -1. */ 181 static void 182 gnu_store_registers (struct target_ops *ops, 183 struct regcache *regcache, int regno) 184 { 185 struct proc *thread; 186 struct gdbarch *gdbarch = get_regcache_arch (regcache); 187 ptid_t ptid = regcache_get_ptid (regcache); 188 189 /* Make sure we know about new threads. */ 190 inf_update_procs (gnu_current_inf); 191 192 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid)); 193 if (!thread) 194 error (_("Couldn't store registers into thread %s: No such thread"), 195 target_pid_to_str (ptid)); 196 197 if (regno < I386_NUM_GREGS || regno == -1) 198 { 199 thread_state_t state; 200 thread_state_data_t old_state; 201 int was_aborted = thread->aborted; 202 int was_valid = thread->state_valid; 203 int trace; 204 205 if (!was_aborted && was_valid) 206 memcpy (&old_state, &thread->state, sizeof (old_state)); 207 208 state = proc_get_state (thread, 1); 209 if (!state) 210 { 211 warning (_("Couldn't store registers into %s"), 212 proc_string (thread)); 213 return; 214 } 215 216 /* Save the T bit. We might try to restore the %eflags register 217 below, but changing the T bit would seriously confuse GDB. */ 218 trace = ((struct i386_thread_state *)state)->efl & 0x100; 219 220 if (!was_aborted && was_valid) 221 /* See which registers have changed after aborting the thread. */ 222 { 223 int check_regno; 224 225 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++) 226 if ((thread->fetched_regs & (1 << check_regno)) 227 && memcpy (REG_ADDR (&old_state, check_regno), 228 REG_ADDR (state, check_regno), 229 register_size (gdbarch, check_regno))) 230 /* Register CHECK_REGNO has changed! Ack! */ 231 { 232 warning (_("Register %s changed after the thread was aborted"), 233 gdbarch_register_name (gdbarch, check_regno)); 234 if (regno >= 0 && regno != check_regno) 235 /* Update GDB's copy of the register. */ 236 regcache_raw_supply (regcache, check_regno, 237 REG_ADDR (state, check_regno)); 238 else 239 warning (_("... also writing this register! " 240 "Suspicious...")); 241 } 242 } 243 244 if (regno == -1) 245 { 246 int i; 247 248 proc_debug (thread, "storing all registers"); 249 250 for (i = 0; i < I386_NUM_GREGS; i++) 251 if (REG_VALID == regcache_register_status (regcache, i)) 252 regcache_raw_collect (regcache, i, REG_ADDR (state, i)); 253 } 254 else 255 { 256 proc_debug (thread, "storing register %s", 257 gdbarch_register_name (gdbarch, regno)); 258 259 gdb_assert (REG_VALID == regcache_register_status (regcache, regno)); 260 regcache_raw_collect (regcache, regno, REG_ADDR (state, regno)); 261 } 262 263 /* Restore the T bit. */ 264 ((struct i386_thread_state *)state)->efl &= ~0x100; 265 ((struct i386_thread_state *)state)->efl |= trace; 266 } 267 268 if (regno >= I386_NUM_GREGS || regno == -1) 269 { 270 proc_debug (thread, "storing floating-point registers"); 271 272 store_fpregs (regcache, thread, regno); 273 } 274 } 275 276 277 /* Support for debug registers. */ 278 279 #ifdef i386_DEBUG_STATE 280 /* Get debug registers for thread THREAD. */ 281 282 static void 283 i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread) 284 { 285 mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT; 286 kern_return_t err; 287 288 err = thread_get_state (thread->port, i386_DEBUG_STATE, 289 (thread_state_t) regs, &count); 290 if (err != 0 || count != i386_DEBUG_STATE_COUNT) 291 warning (_("Couldn't fetch debug state from %s"), 292 proc_string (thread)); 293 } 294 295 /* Set debug registers for thread THREAD. */ 296 297 static void 298 i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread) 299 { 300 kern_return_t err; 301 302 err = thread_set_state (thread->port, i386_DEBUG_STATE, 303 (thread_state_t) regs, i386_DEBUG_STATE_COUNT); 304 if (err != 0) 305 warning (_("Couldn't store debug state into %s"), 306 proc_string (thread)); 307 } 308 309 /* Set DR_CONTROL in THREAD. */ 310 311 static void 312 i386_gnu_dr_set_control_one (struct proc *thread, void *arg) 313 { 314 unsigned long *control = (unsigned long *) arg; 315 struct i386_debug_state regs; 316 317 i386_gnu_dr_get (®s, thread); 318 regs.dr[DR_CONTROL] = *control; 319 i386_gnu_dr_set (®s, thread); 320 } 321 322 /* Set DR_CONTROL to CONTROL in all threads. */ 323 324 static void 325 i386_gnu_dr_set_control (unsigned long control) 326 { 327 inf_update_procs (gnu_current_inf); 328 inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control); 329 } 330 331 /* Parameters to set a debugging address. */ 332 333 struct reg_addr 334 { 335 int regnum; /* Register number (zero based). */ 336 CORE_ADDR addr; /* Address. */ 337 }; 338 339 /* Set address REGNUM (zero based) to ADDR in THREAD. */ 340 341 static void 342 i386_gnu_dr_set_addr_one (struct proc *thread, void *arg) 343 { 344 struct reg_addr *reg_addr = (struct reg_addr *) arg; 345 struct i386_debug_state regs; 346 347 i386_gnu_dr_get (®s, thread); 348 regs.dr[reg_addr->regnum] = reg_addr->addr; 349 i386_gnu_dr_set (®s, thread); 350 } 351 352 /* Set address REGNUM (zero based) to ADDR in all threads. */ 353 354 static void 355 i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr) 356 { 357 struct reg_addr reg_addr; 358 359 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR); 360 361 reg_addr.regnum = regnum; 362 reg_addr.addr = addr; 363 364 inf_update_procs (gnu_current_inf); 365 inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, ®_addr); 366 } 367 368 /* Get debug register REGNUM value from only the one LWP of PTID. */ 369 370 static unsigned long 371 i386_gnu_dr_get_reg (ptid_t ptid, int regnum) 372 { 373 struct i386_debug_state regs; 374 struct proc *thread; 375 376 /* Make sure we know about new threads. */ 377 inf_update_procs (gnu_current_inf); 378 379 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid)); 380 i386_gnu_dr_get (®s, thread); 381 382 return regs.dr[regnum]; 383 } 384 385 /* Return the inferior's debug register REGNUM. */ 386 387 static CORE_ADDR 388 i386_gnu_dr_get_addr (int regnum) 389 { 390 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR); 391 392 return i386_gnu_dr_get_reg (inferior_ptid, regnum); 393 } 394 395 /* Get DR_STATUS from only the one thread of INFERIOR_PTID. */ 396 397 static unsigned long 398 i386_gnu_dr_get_status (void) 399 { 400 return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS); 401 } 402 403 /* Return the inferior's DR7 debug control register. */ 404 405 static unsigned long 406 i386_gnu_dr_get_control (void) 407 { 408 return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL); 409 } 410 #endif /* i386_DEBUG_STATE */ 411 412 /* Provide a prototype to silence -Wmissing-prototypes. */ 413 extern initialize_file_ftype _initialize_i386gnu_nat; 414 415 void 416 _initialize_i386gnu_nat (void) 417 { 418 struct target_ops *t; 419 420 /* Fill in the generic GNU/Hurd methods. */ 421 t = gnu_target (); 422 423 #ifdef i386_DEBUG_STATE 424 x86_use_watchpoints (t); 425 426 x86_dr_low.set_control = i386_gnu_dr_set_control; 427 gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT); 428 x86_dr_low.set_addr = i386_gnu_dr_set_addr; 429 x86_dr_low.get_addr = i386_gnu_dr_get_addr; 430 x86_dr_low.get_status = i386_gnu_dr_get_status; 431 x86_dr_low.get_control = i386_gnu_dr_get_control; 432 x86_set_debug_register_length (4); 433 #endif /* i386_DEBUG_STATE */ 434 435 t->to_fetch_registers = gnu_fetch_registers; 436 t->to_store_registers = gnu_store_registers; 437 438 /* Register the target. */ 439 add_target (t); 440 } 441