Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdb / i386-gnu-nat.c
blob52d4a4b7cec2816610dd9cb8a945dd17c7f632ec
1 /* Low level interface to i386 running the GNU Hurd.
3 Copyright (C) 1992-2024 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 /* Include this first, to pick up the <mach.h> 'thread_info' diversion. */
21 #include "gnu-nat.h"
23 /* Mach/Hurd headers are not yet ready for C++ compilation. */
24 extern "C"
26 #include <mach.h>
27 #include <mach_error.h>
28 #include <mach/message.h>
29 #include <mach/exception.h>
32 #include "x86-nat.h"
33 #include "inferior.h"
34 #include "floatformat.h"
35 #include "regcache.h"
37 #include "i386-tdep.h"
39 #include "inf-child.h"
40 #include "i387-tdep.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 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
59 /* The i386 GNU Hurd target. */
61 #ifdef i386_DEBUG_STATE
62 using gnu_base_target = x86_nat_target<gnu_nat_target>;
63 #else
64 using gnu_base_target = gnu_nat_target;
65 #endif
67 struct i386_gnu_nat_target final : public gnu_base_target
69 void fetch_registers (struct regcache *, int) override;
70 void store_registers (struct regcache *, int) override;
73 static i386_gnu_nat_target the_i386_gnu_nat_target;
75 /* Get the whole floating-point state of THREAD and record the values
76 of the corresponding (pseudo) registers. */
78 static void
79 fetch_fpregs (struct regcache *regcache, struct proc *thread)
81 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
82 struct i386_float_state state;
83 kern_return_t err;
85 err = thread_get_state (thread->port, i386_FLOAT_STATE,
86 (thread_state_t) &state, &count);
87 if (err)
89 warning (_("Couldn't fetch floating-point state from %s"),
90 proc_string (thread));
91 return;
94 if (!state.initialized)
96 /* The floating-point state isn't initialized. */
97 i387_supply_fsave (regcache, -1, NULL);
99 else
101 /* Supply the floating-point registers. */
102 i387_supply_fsave (regcache, -1, state.hw_state);
106 /* Fetch register REGNO, or all regs if REGNO is -1. */
107 void
108 i386_gnu_nat_target::fetch_registers (struct regcache *regcache, int regno)
110 struct proc *thread;
111 ptid_t ptid = regcache->ptid ();
113 /* Make sure we know about new threads. */
114 inf_update_procs (gnu_current_inf);
116 thread = inf_tid_to_thread (gnu_current_inf, ptid.lwp ());
117 if (!thread)
118 error (_("Can't fetch registers from thread %s: No such thread"),
119 target_pid_to_str (ptid).c_str ());
121 if (regno < I386_NUM_GREGS || regno == -1)
123 thread_state_t state;
125 /* This does the dirty work for us. */
126 state = proc_get_state (thread, 0);
127 if (!state)
129 warning (_("Couldn't fetch registers from %s"),
130 proc_string (thread));
131 return;
134 if (regno == -1)
136 int i;
138 proc_debug (thread, "fetching all register");
140 for (i = 0; i < I386_NUM_GREGS; i++)
141 regcache->raw_supply (i, REG_ADDR (state, i));
142 thread->fetched_regs = ~0;
144 else
146 proc_debug (thread, "fetching register %s",
147 gdbarch_register_name (regcache->arch (),
148 regno));
150 regcache->raw_supply (regno, REG_ADDR (state, regno));
151 thread->fetched_regs |= (1 << regno);
155 if (regno >= I386_NUM_GREGS || regno == -1)
157 proc_debug (thread, "fetching floating-point registers");
159 fetch_fpregs (regcache, thread);
164 /* Store the whole floating-point state into THREAD using information
165 from the corresponding (pseudo) registers. */
166 static void
167 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
169 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
170 struct i386_float_state state;
171 kern_return_t err;
173 err = thread_get_state (thread->port, i386_FLOAT_STATE,
174 (thread_state_t) &state, &count);
175 if (err)
177 warning (_("Couldn't fetch floating-point state from %s"),
178 proc_string (thread));
179 return;
182 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow
183 take into account DEPRECATED_REGISTER_VALID like the old code did? */
184 i387_collect_fsave (regcache, regno, state.hw_state);
186 err = thread_set_state (thread->port, i386_FLOAT_STATE,
187 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
188 if (err)
190 warning (_("Couldn't store floating-point state into %s"),
191 proc_string (thread));
192 return;
196 /* Store at least register REGNO, or all regs if REGNO == -1. */
197 void
198 i386_gnu_nat_target::store_registers (struct regcache *regcache, int regno)
200 struct proc *thread;
201 struct gdbarch *gdbarch = regcache->arch ();
202 ptid_t ptid = regcache->ptid ();
204 /* Make sure we know about new threads. */
205 inf_update_procs (gnu_current_inf);
207 thread = inf_tid_to_thread (gnu_current_inf, ptid.lwp ());
208 if (!thread)
209 error (_("Couldn't store registers into thread %s: No such thread"),
210 target_pid_to_str (ptid).c_str ());
212 if (regno < I386_NUM_GREGS || regno == -1)
214 thread_state_t state;
215 thread_state_data_t old_state;
216 int was_aborted = thread->aborted;
217 int was_valid = thread->state_valid;
218 int trace;
220 if (!was_aborted && was_valid)
221 memcpy (&old_state, &thread->state, sizeof (old_state));
223 state = proc_get_state (thread, 1);
224 if (!state)
226 warning (_("Couldn't store registers into %s"),
227 proc_string (thread));
228 return;
231 /* Save the T bit. We might try to restore the %eflags register
232 below, but changing the T bit would seriously confuse GDB. */
233 trace = ((struct i386_thread_state *)state)->efl & 0x100;
235 if (!was_aborted && was_valid)
236 /* See which registers have changed after aborting the thread. */
238 int check_regno;
240 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
241 if ((thread->fetched_regs & (1 << check_regno))
242 && memcpy (REG_ADDR (&old_state, check_regno),
243 REG_ADDR (state, check_regno),
244 register_size (gdbarch, check_regno)))
245 /* Register CHECK_REGNO has changed! Ack! */
247 warning (_("Register %s changed after the thread was aborted"),
248 gdbarch_register_name (gdbarch, check_regno));
249 if (regno >= 0 && regno != check_regno)
250 /* Update GDB's copy of the register. */
251 regcache->raw_supply (check_regno,
252 REG_ADDR (state, check_regno));
253 else
254 warning (_("... also writing this register! "
255 "Suspicious..."));
259 if (regno == -1)
261 int i;
263 proc_debug (thread, "storing all registers");
265 for (i = 0; i < I386_NUM_GREGS; i++)
266 if (REG_VALID == regcache->get_register_status (i))
267 regcache->raw_collect (i, REG_ADDR (state, i));
269 else
271 proc_debug (thread, "storing register %s",
272 gdbarch_register_name (gdbarch, regno));
274 gdb_assert (REG_VALID == regcache->get_register_status (regno));
275 regcache->raw_collect (regno, REG_ADDR (state, regno));
278 /* Restore the T bit. */
279 ((struct i386_thread_state *)state)->efl &= ~0x100;
280 ((struct i386_thread_state *)state)->efl |= trace;
283 if (regno >= I386_NUM_GREGS || regno == -1)
285 proc_debug (thread, "storing floating-point registers");
287 store_fpregs (regcache, thread, regno);
292 /* Support for debug registers. */
294 #ifdef i386_DEBUG_STATE
295 /* Get debug registers for thread THREAD. */
297 static void
298 i386_gnu_dr_get (struct i386_debug_state *regs, struct proc *thread)
300 mach_msg_type_number_t count = i386_DEBUG_STATE_COUNT;
301 kern_return_t err;
303 err = thread_get_state (thread->port, i386_DEBUG_STATE,
304 (thread_state_t) regs, &count);
305 if (err != 0 || count != i386_DEBUG_STATE_COUNT)
306 warning (_("Couldn't fetch debug state from %s"),
307 proc_string (thread));
310 /* Set debug registers for thread THREAD. */
312 static void
313 i386_gnu_dr_set (const struct i386_debug_state *regs, struct proc *thread)
315 kern_return_t err;
317 err = thread_set_state (thread->port, i386_DEBUG_STATE,
318 (thread_state_t) regs, i386_DEBUG_STATE_COUNT);
319 if (err != 0)
320 warning (_("Couldn't store debug state into %s"),
321 proc_string (thread));
324 /* Set DR_CONTROL in THREAD. */
326 static void
327 i386_gnu_dr_set_control_one (struct proc *thread, void *arg)
329 unsigned long *control = (unsigned long *) arg;
330 struct i386_debug_state regs;
332 i386_gnu_dr_get (&regs, thread);
333 regs.dr[DR_CONTROL] = *control;
334 i386_gnu_dr_set (&regs, thread);
337 /* Set DR_CONTROL to CONTROL in all threads. */
339 static void
340 i386_gnu_dr_set_control (unsigned long control)
342 inf_update_procs (gnu_current_inf);
343 inf_threads (gnu_current_inf, i386_gnu_dr_set_control_one, &control);
346 /* Parameters to set a debugging address. */
348 struct reg_addr
350 int regnum; /* Register number (zero based). */
351 CORE_ADDR addr; /* Address. */
354 /* Set address REGNUM (zero based) to ADDR in THREAD. */
356 static void
357 i386_gnu_dr_set_addr_one (struct proc *thread, void *arg)
359 struct reg_addr *reg_addr = (struct reg_addr *) arg;
360 struct i386_debug_state regs;
362 i386_gnu_dr_get (&regs, thread);
363 regs.dr[reg_addr->regnum] = reg_addr->addr;
364 i386_gnu_dr_set (&regs, thread);
367 /* Set address REGNUM (zero based) to ADDR in all threads. */
369 static void
370 i386_gnu_dr_set_addr (int regnum, CORE_ADDR addr)
372 struct reg_addr reg_addr;
374 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
376 reg_addr.regnum = regnum;
377 reg_addr.addr = addr;
379 inf_update_procs (gnu_current_inf);
380 inf_threads (gnu_current_inf, i386_gnu_dr_set_addr_one, &reg_addr);
383 /* Get debug register REGNUM value from only the one LWP of PTID. */
385 static unsigned long
386 i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
388 struct i386_debug_state regs;
389 struct proc *thread;
391 /* Make sure we know about new threads. */
392 inf_update_procs (gnu_current_inf);
394 thread = inf_tid_to_thread (gnu_current_inf, ptid.lwp ());
395 i386_gnu_dr_get (&regs, thread);
397 return regs.dr[regnum];
400 /* Return the inferior's debug register REGNUM. */
402 static CORE_ADDR
403 i386_gnu_dr_get_addr (int regnum)
405 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
407 return i386_gnu_dr_get_reg (inferior_ptid, regnum);
410 /* Get DR_STATUS from only the one thread of INFERIOR_PTID. */
412 static unsigned long
413 i386_gnu_dr_get_status (void)
415 return i386_gnu_dr_get_reg (inferior_ptid, DR_STATUS);
418 /* Return the inferior's DR7 debug control register. */
420 static unsigned long
421 i386_gnu_dr_get_control (void)
423 return i386_gnu_dr_get_reg (inferior_ptid, DR_CONTROL);
425 #endif /* i386_DEBUG_STATE */
427 void _initialize_i386gnu_nat ();
428 void
429 _initialize_i386gnu_nat ()
431 #ifdef i386_DEBUG_STATE
432 x86_dr_low.set_control = i386_gnu_dr_set_control;
433 gdb_assert (DR_FIRSTADDR == 0 && DR_LASTADDR < i386_DEBUG_STATE_COUNT);
434 x86_dr_low.set_addr = i386_gnu_dr_set_addr;
435 x86_dr_low.get_addr = i386_gnu_dr_get_addr;
436 x86_dr_low.get_status = i386_gnu_dr_get_status;
437 x86_dr_low.get_control = i386_gnu_dr_get_control;
438 x86_set_debug_register_length (4);
439 #endif /* i386_DEBUG_STATE */
441 gnu_target = &the_i386_gnu_nat_target;
443 /* Register the target. */
444 add_inf_child_target (&the_i386_gnu_nat_target);