1 /* Low level interface to i386 running the GNU Hurd.
3 Copyright (C) 1992-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "floatformat.h"
27 #include <mach_error.h>
28 #include <mach/message.h>
29 #include <mach/exception.h>
31 #include "i386-tdep.h"
34 #include "inf-child.h"
35 #include "i387-tdep.h"
37 #ifdef HAVE_SYS_PROCFS_H
38 # include <sys/procfs.h>
42 /* Offset to the thread_state_t location where REG is stored. */
43 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
45 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
46 the GDB register N is stored. */
47 static int reg_offset[] =
49 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
50 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
51 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
52 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
55 /* Offset to the greg_t location where REG is stored. */
56 #define CREG_OFFSET(reg) (REG_##reg * 4)
58 /* At CREG_OFFSET[N] is the offset to the greg_t location where
59 the GDB register N is stored. */
60 static int creg_offset[] =
62 CREG_OFFSET (EAX), CREG_OFFSET (ECX), CREG_OFFSET (EDX), CREG_OFFSET (EBX),
63 CREG_OFFSET (UESP), CREG_OFFSET (EBP), CREG_OFFSET (ESI), CREG_OFFSET (EDI),
64 CREG_OFFSET (EIP), CREG_OFFSET (EFL), CREG_OFFSET (CS), CREG_OFFSET (SS),
65 CREG_OFFSET (DS), CREG_OFFSET (ES), CREG_OFFSET (FS), CREG_OFFSET (GS)
68 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
69 #define CREG_ADDR(state, regnum) ((const char *)(state) + creg_offset[regnum])
72 /* Get the whole floating-point state of THREAD and record the values
73 of the corresponding (pseudo) registers. */
76 fetch_fpregs (struct regcache *regcache, struct proc *thread)
78 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
79 struct i386_float_state state;
82 err = thread_get_state (thread->port, i386_FLOAT_STATE,
83 (thread_state_t) &state, &count);
86 warning (_("Couldn't fetch floating-point state from %s"),
87 proc_string (thread));
91 if (!state.initialized)
93 /* The floating-point state isn't initialized. */
94 i387_supply_fsave (regcache, -1, NULL);
98 /* Supply the floating-point registers. */
99 i387_supply_fsave (regcache, -1, state.hw_state);
103 #ifdef HAVE_SYS_PROCFS_H
104 /* These two calls are used by the core-regset.c code for
105 reading ELF core files. */
107 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
110 for (i = 0; i < I386_NUM_GREGS; i++)
111 regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i));
115 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
117 i387_supply_fsave (regcache, -1, fpregs);
121 /* Fetch register REGNO, or all regs if REGNO is -1. */
123 gnu_fetch_registers (struct target_ops *ops,
124 struct regcache *regcache, int regno)
128 /* Make sure we know about new threads. */
129 inf_update_procs (gnu_current_inf);
131 thread = inf_tid_to_thread (gnu_current_inf,
132 ptid_get_lwp (inferior_ptid));
134 error (_("Can't fetch registers from thread %s: No such thread"),
135 target_pid_to_str (inferior_ptid));
137 if (regno < I386_NUM_GREGS || regno == -1)
139 thread_state_t state;
141 /* This does the dirty work for us. */
142 state = proc_get_state (thread, 0);
145 warning (_("Couldn't fetch registers from %s"),
146 proc_string (thread));
154 proc_debug (thread, "fetching all register");
156 for (i = 0; i < I386_NUM_GREGS; i++)
157 regcache_raw_supply (regcache, i, REG_ADDR (state, i));
158 thread->fetched_regs = ~0;
162 proc_debug (thread, "fetching register %s",
163 gdbarch_register_name (get_regcache_arch (regcache),
166 regcache_raw_supply (regcache, regno,
167 REG_ADDR (state, regno));
168 thread->fetched_regs |= (1 << regno);
172 if (regno >= I386_NUM_GREGS || regno == -1)
174 proc_debug (thread, "fetching floating-point registers");
176 fetch_fpregs (regcache, thread);
181 /* Store the whole floating-point state into THREAD using information
182 from the corresponding (pseudo) registers. */
184 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
186 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
187 struct i386_float_state state;
190 err = thread_get_state (thread->port, i386_FLOAT_STATE,
191 (thread_state_t) &state, &count);
194 warning (_("Couldn't fetch floating-point state from %s"),
195 proc_string (thread));
199 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow
200 take into account DEPRECATED_REGISTER_VALID like the old code did? */
201 i387_collect_fsave (regcache, regno, state.hw_state);
203 err = thread_set_state (thread->port, i386_FLOAT_STATE,
204 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
207 warning (_("Couldn't store floating-point state into %s"),
208 proc_string (thread));
213 /* Store at least register REGNO, or all regs if REGNO == -1. */
215 gnu_store_registers (struct target_ops *ops,
216 struct regcache *regcache, int regno)
219 struct gdbarch *gdbarch = get_regcache_arch (regcache);
221 /* Make sure we know about new threads. */
222 inf_update_procs (gnu_current_inf);
224 thread = inf_tid_to_thread (gnu_current_inf,
225 ptid_get_lwp (inferior_ptid));
227 error (_("Couldn't store registers into thread %s: No such thread"),
228 target_pid_to_str (inferior_ptid));
230 if (regno < I386_NUM_GREGS || regno == -1)
232 thread_state_t state;
233 thread_state_data_t old_state;
234 int was_aborted = thread->aborted;
235 int was_valid = thread->state_valid;
238 if (!was_aborted && was_valid)
239 memcpy (&old_state, &thread->state, sizeof (old_state));
241 state = proc_get_state (thread, 1);
244 warning (_("Couldn't store registers into %s"),
245 proc_string (thread));
249 /* Save the T bit. We might try to restore the %eflags register
250 below, but changing the T bit would seriously confuse GDB. */
251 trace = ((struct i386_thread_state *)state)->efl & 0x100;
253 if (!was_aborted && was_valid)
254 /* See which registers have changed after aborting the thread. */
258 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
259 if ((thread->fetched_regs & (1 << check_regno))
260 && memcpy (REG_ADDR (&old_state, check_regno),
261 REG_ADDR (state, check_regno),
262 register_size (gdbarch, check_regno)))
263 /* Register CHECK_REGNO has changed! Ack! */
265 warning (_("Register %s changed after the thread was aborted"),
266 gdbarch_register_name (gdbarch, check_regno));
267 if (regno >= 0 && regno != check_regno)
268 /* Update GDB's copy of the register. */
269 regcache_raw_supply (regcache, check_regno,
270 REG_ADDR (state, check_regno));
272 warning (_("... also writing this register! "
281 proc_debug (thread, "storing all registers");
283 for (i = 0; i < I386_NUM_GREGS; i++)
284 if (REG_VALID == regcache_register_status (regcache, i))
285 regcache_raw_collect (regcache, i, REG_ADDR (state, i));
289 proc_debug (thread, "storing register %s",
290 gdbarch_register_name (gdbarch, regno));
292 gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
293 regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
296 /* Restore the T bit. */
297 ((struct i386_thread_state *)state)->efl &= ~0x100;
298 ((struct i386_thread_state *)state)->efl |= trace;
301 if (regno >= I386_NUM_GREGS || regno == -1)
303 proc_debug (thread, "storing floating-point registers");
305 store_fpregs (regcache, thread, regno);
310 /* Support for debug registers. */
312 #ifdef i386_DEBUG_STATE
313 /* Get debug registers for thread THREAD. */
316 i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread)
318 mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT;
321 err = thread_get_state (thread->port, i386_DEBUG_STATE,
322 (thread_state_t) regs, &count);
323 if (err != 0 || count != i386_DEBUG_STATE_COUNT)
324 warning (_("Couldn't fetch debug state from %s"),
325 proc_string (thread));
328 /* Set debug registers for thread THREAD. */
331 i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread)
335 err = thread_set_state (thread->port, i386_DEBUG_STATE,
336 (thread_state_t) regs, i386_DEBUG_STATE_COUNT);
338 warning (_("Couldn't store debug state into %s"),
339 proc_string (thread));
342 /* Set DR_CONTROL in THREAD. */
345 i386_gnu_dr_set_control_one (struct proc *thread, void *arg)
347 unsigned long *control = arg;
348 struct i386_debug_state regs;
350 i386_gnu_dr_get (®s, thread);
351 regs.dr[DR_CONTROL] = *control;
352 i386_gnu_dr_set (®s, thread);
355 /* Set DR_CONTROL to CONTROL in all threads. */
358 i386_gnu_dr_set_control (unsigned long control)
360 inf_update_procs (gnu_current_inf);
361 inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control);
364 /* Parameters to set a debugging address. */
368 int regnum; /* Register number (zero based). */
369 CORE_ADDR addr; /* Address. */
372 /* Set address REGNUM (zero based) to ADDR in THREAD. */
375 i386_gnu_dr_set_addr_one (struct proc *thread, void *arg)
377 struct reg_addr *reg_addr = arg;
378 struct i386_debug_state regs;
380 i386_gnu_dr_get (®s, thread);
381 regs.dr[reg_addr->regnum] = reg_addr->addr;
382 i386_gnu_dr_set (®s, thread);
385 /* Set address REGNUM (zero based) to ADDR in all threads. */
388 i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr)
390 struct reg_addr reg_addr;
392 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
394 reg_addr.regnum = regnum;
395 reg_addr.addr = addr;
397 inf_update_procs (gnu_current_inf);
398 inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, ®_addr);
401 /* Get debug register REGNUM value from only the one LWP of PTID. */
404 i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
406 struct i386_debug_state regs;
409 /* Make sure we know about new threads. */
410 inf_update_procs (gnu_current_inf);
412 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
413 i386_gnu_dr_get (®s, thread);
415 return regs.dr[regnum];
418 /* Return the inferior's debug register REGNUM. */
421 i386_gnu_dr_get_addr (int regnum)
423 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
425 return i386_gnu_dr_get_reg (inferior_ptid, regnum);
428 /* Get DR_STATUS from only the one thread of INFERIOR_PTID. */
431 i386_gnu_dr_get_status (void)
433 return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS);
436 /* Return the inferior's DR7 debug control register. */
439 i386_gnu_dr_get_control (void)
441 return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL);
443 #endif /* i386_DEBUG_STATE */
445 /* Provide a prototype to silence -Wmissing-prototypes. */
446 extern initialize_file_ftype _initialize_i386gnu_nat;
449 _initialize_i386gnu_nat (void)
451 struct target_ops *t;
453 /* Fill in the generic GNU/Hurd methods. */
456 #ifdef i386_DEBUG_STATE
457 x86_use_watchpoints (t);
459 x86_dr_low.set_control = i386_gnu_dr_set_control;
460 gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT);
461 x86_dr_low.set_addr = i386_gnu_dr_set_addr;
462 x86_dr_low.get_addr = i386_gnu_dr_get_addr;
463 x86_dr_low.get_status = i386_gnu_dr_get_status;
464 x86_dr_low.get_control = i386_gnu_dr_get_control;
465 x86_set_debug_register_length (4);
466 #endif /* i386_DEBUG_STATE */
468 t->to_fetch_registers = gnu_fetch_registers;
469 t->to_store_registers = gnu_store_registers;
471 /* Register the target. */