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