2 * i386 register context support
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifdef HAVE_SYS_PTRACE_H
32 # include <sys/ptrace.h>
34 #ifdef HAVE_SYS_PARAM_H
35 # include <sys/param.h>
44 #ifndef PTRACE_PEEKUSER
45 # ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
46 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
48 # define PTRACE_PEEKUSER PT_READ_U
52 #ifndef PTRACE_POKEUSER
53 # ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
54 # define PTRACE_POKEUSER PTRACE_POKEUSR
56 # define PTRACE_POKEUSER PT_WRITE_U
60 #ifndef PTRACE_GETREGS
61 #define PTRACE_GETREGS PT_GETREGS
63 #ifndef PTRACE_GETFPREGS
64 #define PTRACE_GETFPREGS PT_GETFPREGS
66 #ifndef PTRACE_SETREGS
67 #define PTRACE_SETREGS PT_SETREGS
69 #ifndef PTRACE_SETFPREGS
70 #define PTRACE_SETFPREGS PT_SETFPREGS
74 #define PTRACE_GETDBREGS PT_GETDBREGS
78 #define PTRACE_SETDBREGS PT_SETDBREGS
82 #ifdef HAVE_SYS_USER_H
83 # include <sys/user.h>
86 /* user_regs definitions from asm/user.h */
87 struct kernel_user_regs_struct
89 long ebx
, ecx
, edx
, esi
, edi
, ebp
, eax
;
90 unsigned short ds
, __ds
, es
, __es
;
91 unsigned short fs
, __fs
, gs
, __gs
;
93 unsigned short cs
, __cs
;
95 unsigned short ss
, __ss
;
98 /* debug register offset in struct user */
99 #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
101 /* retrieve a debug register */
102 static inline int get_debug_reg( int pid
, int num
, DWORD
*data
)
104 int res
= ptrace( PTRACE_PEEKUSER
, pid
, DR_OFFSET(num
), 0 );
105 if ((res
== -1) && errno
)
114 /* retrieve a thread context */
115 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
117 int pid
= get_ptrace_pid(thread
);
118 if (flags
& CONTEXT_FULL
)
120 struct kernel_user_regs_struct regs
;
121 if (ptrace( PTRACE_GETREGS
, pid
, 0, ®s
) == -1) goto error
;
122 if (flags
& CONTEXT_INTEGER
)
124 context
->Eax
= regs
.eax
;
125 context
->Ebx
= regs
.ebx
;
126 context
->Ecx
= regs
.ecx
;
127 context
->Edx
= regs
.edx
;
128 context
->Esi
= regs
.esi
;
129 context
->Edi
= regs
.edi
;
131 if (flags
& CONTEXT_CONTROL
)
133 context
->Ebp
= regs
.ebp
;
134 context
->Esp
= regs
.esp
;
135 context
->Eip
= regs
.eip
;
136 context
->SegCs
= regs
.cs
;
137 context
->SegSs
= regs
.ss
;
138 context
->EFlags
= regs
.eflags
;
140 if (flags
& CONTEXT_SEGMENTS
)
142 context
->SegDs
= regs
.ds
;
143 context
->SegEs
= regs
.es
;
144 context
->SegFs
= regs
.fs
;
145 context
->SegGs
= regs
.gs
;
148 if (flags
& CONTEXT_DEBUG_REGISTERS
)
150 if (get_debug_reg( pid
, 0, &context
->Dr0
) == -1) goto error
;
151 if (get_debug_reg( pid
, 1, &context
->Dr1
) == -1) goto error
;
152 if (get_debug_reg( pid
, 2, &context
->Dr2
) == -1) goto error
;
153 if (get_debug_reg( pid
, 3, &context
->Dr3
) == -1) goto error
;
154 if (get_debug_reg( pid
, 6, &context
->Dr6
) == -1) goto error
;
155 if (get_debug_reg( pid
, 7, &context
->Dr7
) == -1) goto error
;
157 if (flags
& CONTEXT_FLOATING_POINT
)
159 /* we can use context->FloatSave directly as it is using the */
160 /* correct structure (the same as fsave/frstor) */
161 if (ptrace( PTRACE_GETFPREGS
, pid
, 0, &context
->FloatSave
) == -1) goto error
;
162 context
->FloatSave
.Cr0NpxState
= 0; /* FIXME */
170 /* set a thread context */
171 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
173 int pid
= get_ptrace_pid(thread
);
174 if (flags
& CONTEXT_FULL
)
176 struct kernel_user_regs_struct regs
;
178 /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
179 if (ptrace( PTRACE_GETREGS
, pid
, 0, ®s
) == -1) goto error
;
181 if (flags
& CONTEXT_INTEGER
)
183 regs
.eax
= context
->Eax
;
184 regs
.ebx
= context
->Ebx
;
185 regs
.ecx
= context
->Ecx
;
186 regs
.edx
= context
->Edx
;
187 regs
.esi
= context
->Esi
;
188 regs
.edi
= context
->Edi
;
190 if (flags
& CONTEXT_CONTROL
)
192 regs
.ebp
= context
->Ebp
;
193 regs
.esp
= context
->Esp
;
194 regs
.eip
= context
->Eip
;
195 regs
.cs
= context
->SegCs
;
196 regs
.ss
= context
->SegSs
;
197 regs
.eflags
= context
->EFlags
;
199 if (flags
& CONTEXT_SEGMENTS
)
201 regs
.ds
= context
->SegDs
;
202 regs
.es
= context
->SegEs
;
203 regs
.fs
= context
->SegFs
;
204 regs
.gs
= context
->SegGs
;
206 if (ptrace( PTRACE_SETREGS
, pid
, 0, ®s
) == -1) goto error
;
208 if (flags
& CONTEXT_DEBUG_REGISTERS
)
210 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(0), context
->Dr0
) == -1) goto error
;
211 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(1), context
->Dr1
) == -1) goto error
;
212 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(2), context
->Dr2
) == -1) goto error
;
213 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(3), context
->Dr3
) == -1) goto error
;
214 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(6), context
->Dr6
) == -1) goto error
;
215 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(7), context
->Dr7
) == -1) goto error
;
217 if (flags
& CONTEXT_FLOATING_POINT
)
219 /* we can use context->FloatSave directly as it is using the */
220 /* correct structure (the same as fsave/frstor) */
221 if (ptrace( PTRACE_SETFPREGS
, pid
, 0, &context
->FloatSave
) == -1) goto error
;
228 #elif defined(__sun) || defined(__sun__)
230 /* retrieve a thread context */
231 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
233 int pid
= get_ptrace_pid(thread
);
234 if (flags
& CONTEXT_FULL
)
237 if (ptrace( PTRACE_GETREGS
, pid
, (int) ®s
, 0 ) == -1) goto error
;
238 if (flags
& CONTEXT_INTEGER
)
240 context
->Eax
= regs
.r_eax
;
241 context
->Ebx
= regs
.r_ebx
;
242 context
->Ecx
= regs
.r_ecx
;
243 context
->Edx
= regs
.r_edx
;
244 context
->Esi
= regs
.r_esi
;
245 context
->Edi
= regs
.r_edi
;
247 if (flags
& CONTEXT_CONTROL
)
249 context
->Ebp
= regs
.r_ebp
;
250 context
->Esp
= regs
.r_esp
;
251 context
->Eip
= regs
.r_eip
;
252 context
->SegCs
= regs
.r_cs
& 0xffff;
253 context
->SegSs
= regs
.r_ss
& 0xffff;
254 context
->EFlags
= regs
.r_efl
;
256 if (flags
& CONTEXT_SEGMENTS
)
258 context
->SegDs
= regs
.r_ds
& 0xffff;
259 context
->SegEs
= regs
.r_es
& 0xffff;
260 context
->SegFs
= regs
.r_fs
& 0xffff;
261 context
->SegGs
= regs
.r_gs
& 0xffff;
264 if (flags
& CONTEXT_DEBUG_REGISTERS
)
266 /* FIXME: How is this done on Solaris? */
268 if (flags
& CONTEXT_FLOATING_POINT
)
270 /* we can use context->FloatSave directly as it is using the */
271 /* correct structure (the same as fsave/frstor) */
272 if (ptrace( PTRACE_GETFPREGS
, pid
, (int) &context
->FloatSave
, 0 ) == -1) goto error
;
273 context
->FloatSave
.Cr0NpxState
= 0; /* FIXME */
281 /* set a thread context */
282 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
284 int pid
= get_ptrace_pid(thread
);
285 if (flags
& CONTEXT_FULL
)
288 if (((flags
| CONTEXT_i386
) & CONTEXT_FULL
) != CONTEXT_FULL
)
290 /* need to preserve some registers */
291 if (ptrace( PTRACE_GETREGS
, pid
, (int) ®s
, 0 ) == -1) goto error
;
293 if (flags
& CONTEXT_INTEGER
)
295 regs
.r_eax
= context
->Eax
;
296 regs
.r_ebx
= context
->Ebx
;
297 regs
.r_ecx
= context
->Ecx
;
298 regs
.r_edx
= context
->Edx
;
299 regs
.r_esi
= context
->Esi
;
300 regs
.r_edi
= context
->Edi
;
302 if (flags
& CONTEXT_CONTROL
)
304 regs
.r_ebp
= context
->Ebp
;
305 regs
.r_esp
= context
->Esp
;
306 regs
.r_eip
= context
->Eip
;
307 regs
.r_cs
= context
->SegCs
;
308 regs
.r_ss
= context
->SegSs
;
309 regs
.r_efl
= context
->EFlags
;
311 if (flags
& CONTEXT_SEGMENTS
)
313 regs
.r_ds
= context
->SegDs
;
314 regs
.r_es
= context
->SegEs
;
315 regs
.r_fs
= context
->SegFs
;
316 regs
.r_gs
= context
->SegGs
;
318 if (ptrace( PTRACE_SETREGS
, pid
, (int) ®s
, 0 ) == -1) goto error
;
320 if (flags
& CONTEXT_DEBUG_REGISTERS
)
322 /* FIXME: How is this done on Solaris? */
324 if (flags
& CONTEXT_FLOATING_POINT
)
326 /* we can use context->FloatSave directly as it is using the */
327 /* correct structure (the same as fsave/frstor) */
328 if (ptrace( PTRACE_SETFPREGS
, pid
, (int) &context
->FloatSave
, 0 ) == -1) goto error
;
335 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
336 #include <machine/reg.h>
338 /* retrieve a thread context */
339 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
341 int pid
= get_ptrace_pid(thread
);
342 if (flags
& CONTEXT_FULL
)
345 if (ptrace( PTRACE_GETREGS
, pid
, (caddr_t
) ®s
, 0 ) == -1) goto error
;
346 if (flags
& CONTEXT_INTEGER
)
348 context
->Eax
= regs
.r_eax
;
349 context
->Ebx
= regs
.r_ebx
;
350 context
->Ecx
= regs
.r_ecx
;
351 context
->Edx
= regs
.r_edx
;
352 context
->Esi
= regs
.r_esi
;
353 context
->Edi
= regs
.r_edi
;
355 if (flags
& CONTEXT_CONTROL
)
357 context
->Ebp
= regs
.r_ebp
;
358 context
->Esp
= regs
.r_esp
;
359 context
->Eip
= regs
.r_eip
;
360 context
->SegCs
= regs
.r_cs
& 0xffff;
361 context
->SegSs
= regs
.r_ss
& 0xffff;
362 context
->EFlags
= regs
.r_eflags
;
364 if (flags
& CONTEXT_SEGMENTS
)
366 context
->SegDs
= regs
.r_ds
& 0xffff;
367 context
->SegEs
= regs
.r_es
& 0xffff;
368 context
->SegFs
= regs
.r_fs
& 0xffff;
369 context
->SegGs
= regs
.r_gs
& 0xffff;
372 if (flags
& CONTEXT_DEBUG_REGISTERS
)
374 #ifdef PTRACE_GETDBREGS
376 if (ptrace( PTRACE_GETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1)
379 /* needed for FreeBSD, the structure fields have changed under 5.x */
380 context
->Dr0
= DBREG_DRX((&dbregs
), 0);
381 context
->Dr1
= DBREG_DRX((&dbregs
), 1);
382 context
->Dr2
= DBREG_DRX((&dbregs
), 2);
383 context
->Dr3
= DBREG_DRX((&dbregs
), 3);
384 context
->Dr6
= DBREG_DRX((&dbregs
), 6);
385 context
->Dr7
= DBREG_DRX((&dbregs
), 7);
387 context
->Dr0
= dbregs
.dr0
;
388 context
->Dr1
= dbregs
.dr1
;
389 context
->Dr2
= dbregs
.dr2
;
390 context
->Dr3
= dbregs
.dr3
;
391 context
->Dr6
= dbregs
.dr6
;
392 context
->Dr7
= dbregs
.dr7
;
397 if (flags
& CONTEXT_FLOATING_POINT
)
399 /* we can use context->FloatSave directly as it is using the */
400 /* correct structure (the same as fsave/frstor) */
401 if (ptrace( PTRACE_GETFPREGS
, pid
, (caddr_t
) &context
->FloatSave
, 0 ) == -1) goto error
;
402 context
->FloatSave
.Cr0NpxState
= 0; /* FIXME */
410 /* set a thread context */
411 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
413 int pid
= get_ptrace_pid(thread
);
414 if (flags
& CONTEXT_FULL
)
417 if (((flags
| CONTEXT_i386
) & CONTEXT_FULL
) != CONTEXT_FULL
)
419 /* need to preserve some registers */
420 if (ptrace( PTRACE_GETREGS
, pid
, (caddr_t
) ®s
, 0 ) == -1) goto error
;
422 if (flags
& CONTEXT_INTEGER
)
424 regs
.r_eax
= context
->Eax
;
425 regs
.r_ebx
= context
->Ebx
;
426 regs
.r_ecx
= context
->Ecx
;
427 regs
.r_edx
= context
->Edx
;
428 regs
.r_esi
= context
->Esi
;
429 regs
.r_edi
= context
->Edi
;
431 if (flags
& CONTEXT_CONTROL
)
433 regs
.r_ebp
= context
->Ebp
;
434 regs
.r_esp
= context
->Esp
;
435 regs
.r_eip
= context
->Eip
;
436 regs
.r_cs
= context
->SegCs
;
437 regs
.r_ss
= context
->SegSs
;
438 regs
.r_eflags
= context
->EFlags
;
440 if (flags
& CONTEXT_SEGMENTS
)
442 regs
.r_ds
= context
->SegDs
;
443 regs
.r_es
= context
->SegEs
;
444 regs
.r_fs
= context
->SegFs
;
445 regs
.r_gs
= context
->SegGs
;
447 if (ptrace( PTRACE_SETREGS
, pid
, (caddr_t
) ®s
, 0 ) == -1) goto error
;
449 if (flags
& CONTEXT_DEBUG_REGISTERS
)
451 #ifdef PTRACE_SETDBREGS
454 /* needed for FreeBSD, the structure fields have changed under 5.x */
455 DBREG_DRX((&dbregs
), 0) = context
->Dr0
;
456 DBREG_DRX((&dbregs
), 1) = context
->Dr1
;
457 DBREG_DRX((&dbregs
), 2) = context
->Dr2
;
458 DBREG_DRX((&dbregs
), 3) = context
->Dr3
;
459 DBREG_DRX((&dbregs
), 4) = 0;
460 DBREG_DRX((&dbregs
), 5) = 0;
461 DBREG_DRX((&dbregs
), 6) = context
->Dr6
;
462 DBREG_DRX((&dbregs
), 7) = context
->Dr7
;
464 dbregs
.dr0
= context
->Dr0
;
465 dbregs
.dr1
= context
->Dr1
;
466 dbregs
.dr2
= context
->Dr2
;
467 dbregs
.dr3
= context
->Dr3
;
470 dbregs
.dr6
= context
->Dr6
;
471 dbregs
.dr7
= context
->Dr7
;
473 if (ptrace( PTRACE_SETDBREGS
, pid
, (caddr_t
) &dbregs
, 0 ) == -1)
477 if (flags
& CONTEXT_FLOATING_POINT
)
479 /* we can use context->FloatSave directly as it is using the */
480 /* correct structure (the same as fsave/frstor) */
481 if (ptrace( PTRACE_SETFPREGS
, pid
, (caddr_t
) &context
->FloatSave
, 0 ) == -1) goto error
;
488 #else /* linux || __sun__ || __FreeBSD__ */
489 #error You must implement get/set_thread_context for your platform
490 #endif /* linux || __sun__ || __FreeBSD__ */
493 /* copy a context structure according to the flags */
494 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, int flags
)
496 if (flags
& CONTEXT_CONTROL
)
501 to
->SegCs
= from
->SegCs
;
502 to
->SegSs
= from
->SegSs
;
503 to
->EFlags
= from
->EFlags
;
505 if (flags
& CONTEXT_INTEGER
)
514 if (flags
& CONTEXT_SEGMENTS
)
516 to
->SegDs
= from
->SegDs
;
517 to
->SegEs
= from
->SegEs
;
518 to
->SegFs
= from
->SegFs
;
519 to
->SegGs
= from
->SegGs
;
521 if (flags
& CONTEXT_FLOATING_POINT
)
523 to
->FloatSave
= from
->FloatSave
;
525 /* we don't bother copying the debug registers, since they */
526 /* always need to be accessed by ptrace anyway */
529 /* retrieve the current instruction pointer of a thread */
530 void *get_thread_ip( struct thread
*thread
)
534 if (suspend_for_ptrace( thread
))
536 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
537 resume_after_ptrace( thread
);
539 return (void *)context
.Eip
;
542 /* determine if we should continue the thread in single-step mode */
543 int get_thread_single_step( struct thread
*thread
)
546 if (thread
->context
) return 0; /* don't single-step inside exception event */
547 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
548 return (context
.EFlags
& 0x100) != 0;
551 /* retrieve the current context of a thread */
552 DECL_HANDLER(get_thread_context
)
554 struct thread
*thread
;
556 int flags
= req
->flags
& ~CONTEXT_i386
; /* get rid of CPU id */
558 if (get_reply_max_size() < sizeof(CONTEXT
))
560 set_error( STATUS_INVALID_PARAMETER
);
563 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
565 if ((data
= set_reply_data_size( sizeof(CONTEXT
) )))
567 /* copy incoming context into reply */
568 memset( data
, 0, sizeof(CONTEXT
) );
569 memcpy( data
, get_req_data(), min( get_req_data_size(), sizeof(CONTEXT
) ));
571 if (thread
->context
) /* thread is inside an exception event */
573 copy_context( data
, thread
->context
, flags
);
574 flags
&= CONTEXT_DEBUG_REGISTERS
;
576 if (flags
&& suspend_for_ptrace( thread
))
578 get_thread_context( thread
, flags
, data
);
579 resume_after_ptrace( thread
);
582 release_object( thread
);
586 /* set the current context of a thread */
587 DECL_HANDLER(set_thread_context
)
589 struct thread
*thread
;
590 int flags
= req
->flags
& ~CONTEXT_i386
; /* get rid of CPU id */
592 if (get_req_data_size() < sizeof(CONTEXT
))
594 set_error( STATUS_INVALID_PARAMETER
);
597 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
599 if (thread
->context
) /* thread is inside an exception event */
601 copy_context( thread
->context
, get_req_data(), flags
);
602 flags
&= CONTEXT_DEBUG_REGISTERS
;
604 if (flags
&& suspend_for_ptrace( thread
))
606 set_thread_context( thread
, flags
, get_req_data() );
607 resume_after_ptrace( thread
);
609 release_object( thread
);
613 #endif /* __i386__ */