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 /* Offset to the thread_state_t location where REG is stored. */
38 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
40 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
41 the GDB register N is stored. */
42 static int reg_offset[] =
44 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
45 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
46 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
47 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
50 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
53 /* Get the whole floating-point state of THREAD and record the values
54 of the corresponding (pseudo) registers. */
57 fetch_fpregs (struct regcache *regcache, struct proc *thread)
59 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
60 struct i386_float_state state;
63 err = thread_get_state (thread->port, i386_FLOAT_STATE,
64 (thread_state_t) &state, &count);
67 warning (_("Couldn't fetch floating-point state from %s"),
68 proc_string (thread));
72 if (!state.initialized)
74 /* The floating-point state isn't initialized. */
75 i387_supply_fsave (regcache, -1, NULL);
79 /* Supply the floating-point registers. */
80 i387_supply_fsave (regcache, -1, state.hw_state);
84 /* Fetch register REGNO, or all regs if REGNO is -1. */
86 gnu_fetch_registers (struct target_ops *ops,
87 struct regcache *regcache, int regno)
91 /* Make sure we know about new threads. */
92 inf_update_procs (gnu_current_inf);
94 thread = inf_tid_to_thread (gnu_current_inf,
95 ptid_get_lwp (inferior_ptid));
97 error (_("Can't fetch registers from thread %s: No such thread"),
98 target_pid_to_str (inferior_ptid));
100 if (regno < I386_NUM_GREGS || regno == -1)
102 thread_state_t state;
104 /* This does the dirty work for us. */
105 state = proc_get_state (thread, 0);
108 warning (_("Couldn't fetch registers from %s"),
109 proc_string (thread));
117 proc_debug (thread, "fetching all register");
119 for (i = 0; i < I386_NUM_GREGS; i++)
120 regcache_raw_supply (regcache, i, REG_ADDR (state, i));
121 thread->fetched_regs = ~0;
125 proc_debug (thread, "fetching register %s",
126 gdbarch_register_name (get_regcache_arch (regcache),
129 regcache_raw_supply (regcache, regno,
130 REG_ADDR (state, regno));
131 thread->fetched_regs |= (1 << regno);
135 if (regno >= I386_NUM_GREGS || regno == -1)
137 proc_debug (thread, "fetching floating-point registers");
139 fetch_fpregs (regcache, thread);
144 /* Store the whole floating-point state into THREAD using information
145 from the corresponding (pseudo) registers. */
147 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
149 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
150 struct i386_float_state state;
153 err = thread_get_state (thread->port, i386_FLOAT_STATE,
154 (thread_state_t) &state, &count);
157 warning (_("Couldn't fetch floating-point state from %s"),
158 proc_string (thread));
162 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow
163 take into account DEPRECATED_REGISTER_VALID like the old code did? */
164 i387_collect_fsave (regcache, regno, state.hw_state);
166 err = thread_set_state (thread->port, i386_FLOAT_STATE,
167 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
170 warning (_("Couldn't store floating-point state into %s"),
171 proc_string (thread));
176 /* Store at least register REGNO, or all regs if REGNO == -1. */
178 gnu_store_registers (struct target_ops *ops,
179 struct regcache *regcache, int regno)
182 struct gdbarch *gdbarch = get_regcache_arch (regcache);
184 /* Make sure we know about new threads. */
185 inf_update_procs (gnu_current_inf);
187 thread = inf_tid_to_thread (gnu_current_inf,
188 ptid_get_lwp (inferior_ptid));
190 error (_("Couldn't store registers into thread %s: No such thread"),
191 target_pid_to_str (inferior_ptid));
193 if (regno < I386_NUM_GREGS || regno == -1)
195 thread_state_t state;
196 thread_state_data_t old_state;
197 int was_aborted = thread->aborted;
198 int was_valid = thread->state_valid;
201 if (!was_aborted && was_valid)
202 memcpy (&old_state, &thread->state, sizeof (old_state));
204 state = proc_get_state (thread, 1);
207 warning (_("Couldn't store registers into %s"),
208 proc_string (thread));
212 /* Save the T bit. We might try to restore the %eflags register
213 below, but changing the T bit would seriously confuse GDB. */
214 trace = ((struct i386_thread_state *)state)->efl & 0x100;
216 if (!was_aborted && was_valid)
217 /* See which registers have changed after aborting the thread. */
221 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
222 if ((thread->fetched_regs & (1 << check_regno))
223 && memcpy (REG_ADDR (&old_state, check_regno),
224 REG_ADDR (state, check_regno),
225 register_size (gdbarch, check_regno)))
226 /* Register CHECK_REGNO has changed! Ack! */
228 warning (_("Register %s changed after the thread was aborted"),
229 gdbarch_register_name (gdbarch, check_regno));
230 if (regno >= 0 && regno != check_regno)
231 /* Update GDB's copy of the register. */
232 regcache_raw_supply (regcache, check_regno,
233 REG_ADDR (state, check_regno));
235 warning (_("... also writing this register! "
244 proc_debug (thread, "storing all registers");
246 for (i = 0; i < I386_NUM_GREGS; i++)
247 if (REG_VALID == regcache_register_status (regcache, i))
248 regcache_raw_collect (regcache, i, REG_ADDR (state, i));
252 proc_debug (thread, "storing register %s",
253 gdbarch_register_name (gdbarch, regno));
255 gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
256 regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
259 /* Restore the T bit. */
260 ((struct i386_thread_state *)state)->efl &= ~0x100;
261 ((struct i386_thread_state *)state)->efl |= trace;
264 if (regno >= I386_NUM_GREGS || regno == -1)
266 proc_debug (thread, "storing floating-point registers");
268 store_fpregs (regcache, thread, regno);
273 /* Support for debug registers. */
275 #ifdef i386_DEBUG_STATE
276 /* Get debug registers for thread THREAD. */
279 i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread)
281 mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT;
284 err = thread_get_state (thread->port, i386_DEBUG_STATE,
285 (thread_state_t) regs, &count);
286 if (err != 0 || count != i386_DEBUG_STATE_COUNT)
287 warning (_("Couldn't fetch debug state from %s"),
288 proc_string (thread));
291 /* Set debug registers for thread THREAD. */
294 i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread)
298 err = thread_set_state (thread->port, i386_DEBUG_STATE,
299 (thread_state_t) regs, i386_DEBUG_STATE_COUNT);
301 warning (_("Couldn't store debug state into %s"),
302 proc_string (thread));
305 /* Set DR_CONTROL in THREAD. */
308 i386_gnu_dr_set_control_one (struct proc *thread, void *arg)
310 unsigned long *control = arg;
311 struct i386_debug_state regs;
313 i386_gnu_dr_get (®s, thread);
314 regs.dr[DR_CONTROL] = *control;
315 i386_gnu_dr_set (®s, thread);
318 /* Set DR_CONTROL to CONTROL in all threads. */
321 i386_gnu_dr_set_control (unsigned long control)
323 inf_update_procs (gnu_current_inf);
324 inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control);
327 /* Parameters to set a debugging address. */
331 int regnum; /* Register number (zero based). */
332 CORE_ADDR addr; /* Address. */
335 /* Set address REGNUM (zero based) to ADDR in THREAD. */
338 i386_gnu_dr_set_addr_one (struct proc *thread, void *arg)
340 struct reg_addr *reg_addr = arg;
341 struct i386_debug_state regs;
343 i386_gnu_dr_get (®s, thread);
344 regs.dr[reg_addr->regnum] = reg_addr->addr;
345 i386_gnu_dr_set (®s, thread);
348 /* Set address REGNUM (zero based) to ADDR in all threads. */
351 i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr)
353 struct reg_addr reg_addr;
355 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
357 reg_addr.regnum = regnum;
358 reg_addr.addr = addr;
360 inf_update_procs (gnu_current_inf);
361 inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, ®_addr);
364 /* Get debug register REGNUM value from only the one LWP of PTID. */
367 i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
369 struct i386_debug_state regs;
372 /* Make sure we know about new threads. */
373 inf_update_procs (gnu_current_inf);
375 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
376 i386_gnu_dr_get (®s, thread);
378 return regs.dr[regnum];
381 /* Return the inferior's debug register REGNUM. */
384 i386_gnu_dr_get_addr (int regnum)
386 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
388 return i386_gnu_dr_get_reg (inferior_ptid, regnum);
391 /* Get DR_STATUS from only the one thread of INFERIOR_PTID. */
394 i386_gnu_dr_get_status (void)
396 return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS);
399 /* Return the inferior's DR7 debug control register. */
402 i386_gnu_dr_get_control (void)
404 return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL);
406 #endif /* i386_DEBUG_STATE */
408 /* Provide a prototype to silence -Wmissing-prototypes. */
409 extern initialize_file_ftype _initialize_i386gnu_nat;
412 _initialize_i386gnu_nat (void)
414 struct target_ops *t;
416 /* Fill in the generic GNU/Hurd methods. */
419 #ifdef i386_DEBUG_STATE
420 x86_use_watchpoints (t);
422 x86_dr_low.set_control = i386_gnu_dr_set_control;
423 gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT);
424 x86_dr_low.set_addr = i386_gnu_dr_set_addr;
425 x86_dr_low.get_addr = i386_gnu_dr_get_addr;
426 x86_dr_low.get_status = i386_gnu_dr_get_status;
427 x86_dr_low.get_control = i386_gnu_dr_get_control;
428 x86_set_debug_register_length (4);
429 #endif /* i386_DEBUG_STATE */
431 t->to_fetch_registers = gnu_fetch_registers;
432 t->to_store_registers = gnu_store_registers;
434 /* Register the target. */