1 /* Low level interface to i386 running the GNU Hurd.
3 Copyright (C) 1992-2018 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/>. */
20 /* Mach/Hurd headers are not yet ready for C++ compilation. */
24 #include <mach_error.h>
25 #include <mach/message.h>
26 #include <mach/exception.h>
32 #include "floatformat.h"
35 #include "i386-tdep.h"
38 #include "inf-child.h"
39 #include "i387-tdep.h"
41 /* Offset to the thread_state_t location where REG is stored. */
42 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
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[] =
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)
54 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
57 /* Get the whole floating-point state of THREAD and record the values
58 of the corresponding (pseudo) registers. */
61 fetch_fpregs (struct regcache *regcache, struct proc *thread)
63 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
64 struct i386_float_state state;
67 err = thread_get_state (thread->port, i386_FLOAT_STATE,
68 (thread_state_t) &state, &count);
71 warning (_("Couldn't fetch floating-point state from %s"),
72 proc_string (thread));
76 if (!state.initialized)
78 /* The floating-point state isn't initialized. */
79 i387_supply_fsave (regcache, -1, NULL);
83 /* Supply the floating-point registers. */
84 i387_supply_fsave (regcache, -1, state.hw_state);
88 /* Fetch register REGNO, or all regs if REGNO is -1. */
90 gnu_fetch_registers (struct target_ops *ops,
91 struct regcache *regcache, int regno)
94 ptid_t ptid = regcache_get_ptid (regcache);
96 /* Make sure we know about new threads. */
97 inf_update_procs (gnu_current_inf);
99 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
101 error (_("Can't fetch registers from thread %s: No such thread"),
102 target_pid_to_str (ptid));
104 if (regno < I386_NUM_GREGS || regno == -1)
106 thread_state_t state;
108 /* This does the dirty work for us. */
109 state = proc_get_state (thread, 0);
112 warning (_("Couldn't fetch registers from %s"),
113 proc_string (thread));
121 proc_debug (thread, "fetching all register");
123 for (i = 0; i < I386_NUM_GREGS; i++)
124 regcache_raw_supply (regcache, i, REG_ADDR (state, i));
125 thread->fetched_regs = ~0;
129 proc_debug (thread, "fetching register %s",
130 gdbarch_register_name (regcache->arch (),
133 regcache_raw_supply (regcache, regno,
134 REG_ADDR (state, regno));
135 thread->fetched_regs |= (1 << regno);
139 if (regno >= I386_NUM_GREGS || regno == -1)
141 proc_debug (thread, "fetching floating-point registers");
143 fetch_fpregs (regcache, thread);
148 /* Store the whole floating-point state into THREAD using information
149 from the corresponding (pseudo) registers. */
151 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
153 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
154 struct i386_float_state state;
157 err = thread_get_state (thread->port, i386_FLOAT_STATE,
158 (thread_state_t) &state, &count);
161 warning (_("Couldn't fetch floating-point state from %s"),
162 proc_string (thread));
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);
170 err = thread_set_state (thread->port, i386_FLOAT_STATE,
171 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
174 warning (_("Couldn't store floating-point state into %s"),
175 proc_string (thread));
180 /* Store at least register REGNO, or all regs if REGNO == -1. */
182 gnu_store_registers (struct target_ops *ops,
183 struct regcache *regcache, int regno)
186 struct gdbarch *gdbarch = regcache->arch ();
187 ptid_t ptid = regcache_get_ptid (regcache);
189 /* Make sure we know about new threads. */
190 inf_update_procs (gnu_current_inf);
192 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
194 error (_("Couldn't store registers into thread %s: No such thread"),
195 target_pid_to_str (ptid));
197 if (regno < I386_NUM_GREGS || regno == -1)
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;
205 if (!was_aborted && was_valid)
206 memcpy (&old_state, &thread->state, sizeof (old_state));
208 state = proc_get_state (thread, 1);
211 warning (_("Couldn't store registers into %s"),
212 proc_string (thread));
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;
220 if (!was_aborted && was_valid)
221 /* See which registers have changed after aborting the thread. */
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! */
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));
239 warning (_("... also writing this register! "
248 proc_debug (thread, "storing all registers");
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));
256 proc_debug (thread, "storing register %s",
257 gdbarch_register_name (gdbarch, regno));
259 gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
260 regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
263 /* Restore the T bit. */
264 ((struct i386_thread_state *)state)->efl &= ~0x100;
265 ((struct i386_thread_state *)state)->efl |= trace;
268 if (regno >= I386_NUM_GREGS || regno == -1)
270 proc_debug (thread, "storing floating-point registers");
272 store_fpregs (regcache, thread, regno);
277 /* Support for debug registers. */
279 #ifdef i386_DEBUG_STATE
280 /* Get debug registers for thread THREAD. */
283 i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread)
285 mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT;
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));
295 /* Set debug registers for thread THREAD. */
298 i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread)
302 err = thread_set_state (thread->port, i386_DEBUG_STATE,
303 (thread_state_t) regs, i386_DEBUG_STATE_COUNT);
305 warning (_("Couldn't store debug state into %s"),
306 proc_string (thread));
309 /* Set DR_CONTROL in THREAD. */
312 i386_gnu_dr_set_control_one (struct proc *thread, void *arg)
314 unsigned long *control = (unsigned long *) arg;
315 struct i386_debug_state regs;
317 i386_gnu_dr_get (®s, thread);
318 regs.dr[DR_CONTROL] = *control;
319 i386_gnu_dr_set (®s, thread);
322 /* Set DR_CONTROL to CONTROL in all threads. */
325 i386_gnu_dr_set_control (unsigned long control)
327 inf_update_procs (gnu_current_inf);
328 inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control);
331 /* Parameters to set a debugging address. */
335 int regnum; /* Register number (zero based). */
336 CORE_ADDR addr; /* Address. */
339 /* Set address REGNUM (zero based) to ADDR in THREAD. */
342 i386_gnu_dr_set_addr_one (struct proc *thread, void *arg)
344 struct reg_addr *reg_addr = (struct reg_addr *) arg;
345 struct i386_debug_state regs;
347 i386_gnu_dr_get (®s, thread);
348 regs.dr[reg_addr->regnum] = reg_addr->addr;
349 i386_gnu_dr_set (®s, thread);
352 /* Set address REGNUM (zero based) to ADDR in all threads. */
355 i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr)
357 struct reg_addr reg_addr;
359 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
361 reg_addr.regnum = regnum;
362 reg_addr.addr = addr;
364 inf_update_procs (gnu_current_inf);
365 inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, ®_addr);
368 /* Get debug register REGNUM value from only the one LWP of PTID. */
371 i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
373 struct i386_debug_state regs;
376 /* Make sure we know about new threads. */
377 inf_update_procs (gnu_current_inf);
379 thread = inf_tid_to_thread (gnu_current_inf, ptid_get_lwp (ptid));
380 i386_gnu_dr_get (®s, thread);
382 return regs.dr[regnum];
385 /* Return the inferior's debug register REGNUM. */
388 i386_gnu_dr_get_addr (int regnum)
390 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
392 return i386_gnu_dr_get_reg (inferior_ptid, regnum);
395 /* Get DR_STATUS from only the one thread of INFERIOR_PTID. */
398 i386_gnu_dr_get_status (void)
400 return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS);
403 /* Return the inferior's DR7 debug control register. */
406 i386_gnu_dr_get_control (void)
408 return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL);
410 #endif /* i386_DEBUG_STATE */
413 _initialize_i386gnu_nat (void)
415 struct target_ops *t;
417 /* Fill in the generic GNU/Hurd methods. */
420 #ifdef i386_DEBUG_STATE
421 x86_use_watchpoints (t);
423 x86_dr_low.set_control = i386_gnu_dr_set_control;
424 gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT);
425 x86_dr_low.set_addr = i386_gnu_dr_set_addr;
426 x86_dr_low.get_addr = i386_gnu_dr_get_addr;
427 x86_dr_low.get_status = i386_gnu_dr_get_status;
428 x86_dr_low.get_control = i386_gnu_dr_get_control;
429 x86_set_debug_register_length (4);
430 #endif /* i386_DEBUG_STATE */
432 t->to_fetch_registers = gnu_fetch_registers;
433 t->to_store_registers = gnu_store_registers;
435 /* Register the target. */