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>
42 #ifndef PTRACE_PEEKUSER
43 # ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
44 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
46 # define PTRACE_PEEKUSER PT_READ_U
50 #ifndef PTRACE_POKEUSER
51 # ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
52 # define PTRACE_POKEUSER PTRACE_POKEUSR
54 # define PTRACE_POKEUSER PT_WRITE_U
58 #ifndef PTRACE_GETREGS
59 #define PTRACE_GETREGS PT_GETREGS
61 #ifndef PTRACE_GETFPREGS
62 #define PTRACE_GETFPREGS PT_GETFPREGS
64 #ifndef PTRACE_SETREGS
65 #define PTRACE_SETREGS PT_SETREGS
67 #ifndef PTRACE_SETFPREGS
68 #define PTRACE_SETFPREGS PT_SETFPREGS
72 #ifdef HAVE_SYS_USER_H
73 # include <sys/user.h>
76 /* user_regs definitions from asm/user.h */
77 struct kernel_user_regs_struct
79 long ebx
, ecx
, edx
, esi
, edi
, ebp
, eax
;
80 unsigned short ds
, __ds
, es
, __es
;
81 unsigned short fs
, __fs
, gs
, __gs
;
83 unsigned short cs
, __cs
;
85 unsigned short ss
, __ss
;
88 /* debug register offset in struct user */
89 #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
91 /* retrieve a debug register */
92 static inline int get_debug_reg( int pid
, int num
, DWORD
*data
)
94 int res
= ptrace( PTRACE_PEEKUSER
, pid
, DR_OFFSET(num
), 0 );
95 if ((res
== -1) && errno
)
104 /* retrieve a thread context */
105 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
107 int pid
= thread
->unix_pid
;
108 if (flags
& CONTEXT_FULL
)
110 struct kernel_user_regs_struct regs
;
111 if (ptrace( PTRACE_GETREGS
, pid
, 0, ®s
) == -1) goto error
;
112 if (flags
& CONTEXT_INTEGER
)
114 context
->Eax
= regs
.eax
;
115 context
->Ebx
= regs
.ebx
;
116 context
->Ecx
= regs
.ecx
;
117 context
->Edx
= regs
.edx
;
118 context
->Esi
= regs
.esi
;
119 context
->Edi
= regs
.edi
;
121 if (flags
& CONTEXT_CONTROL
)
123 context
->Ebp
= regs
.ebp
;
124 context
->Esp
= regs
.esp
;
125 context
->Eip
= regs
.eip
;
126 context
->SegCs
= regs
.cs
;
127 context
->SegSs
= regs
.ss
;
128 context
->EFlags
= regs
.eflags
;
130 if (flags
& CONTEXT_SEGMENTS
)
132 context
->SegDs
= regs
.ds
;
133 context
->SegEs
= regs
.es
;
134 context
->SegFs
= regs
.fs
;
135 context
->SegGs
= regs
.gs
;
138 if (flags
& CONTEXT_DEBUG_REGISTERS
)
140 if (get_debug_reg( pid
, 0, &context
->Dr0
) == -1) goto error
;
141 if (get_debug_reg( pid
, 1, &context
->Dr1
) == -1) goto error
;
142 if (get_debug_reg( pid
, 2, &context
->Dr2
) == -1) goto error
;
143 if (get_debug_reg( pid
, 3, &context
->Dr3
) == -1) goto error
;
144 if (get_debug_reg( pid
, 6, &context
->Dr6
) == -1) goto error
;
145 if (get_debug_reg( pid
, 7, &context
->Dr7
) == -1) goto error
;
147 if (flags
& CONTEXT_FLOATING_POINT
)
149 /* we can use context->FloatSave directly as it is using the */
150 /* correct structure (the same as fsave/frstor) */
151 if (ptrace( PTRACE_GETFPREGS
, pid
, 0, &context
->FloatSave
) == -1) goto error
;
152 context
->FloatSave
.Cr0NpxState
= 0; /* FIXME */
160 /* set a thread context */
161 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
163 int pid
= thread
->unix_pid
;
164 if (flags
& CONTEXT_FULL
)
166 struct kernel_user_regs_struct regs
;
168 /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
169 if (ptrace( PTRACE_GETREGS
, pid
, 0, ®s
) == -1) goto error
;
171 if (flags
& CONTEXT_INTEGER
)
173 regs
.eax
= context
->Eax
;
174 regs
.ebx
= context
->Ebx
;
175 regs
.ecx
= context
->Ecx
;
176 regs
.edx
= context
->Edx
;
177 regs
.esi
= context
->Esi
;
178 regs
.edi
= context
->Edi
;
180 if (flags
& CONTEXT_CONTROL
)
182 regs
.ebp
= context
->Ebp
;
183 regs
.esp
= context
->Esp
;
184 regs
.eip
= context
->Eip
;
185 regs
.cs
= context
->SegCs
;
186 regs
.ss
= context
->SegSs
;
187 regs
.eflags
= context
->EFlags
;
189 if (flags
& CONTEXT_SEGMENTS
)
191 regs
.ds
= context
->SegDs
;
192 regs
.es
= context
->SegEs
;
193 regs
.fs
= context
->SegFs
;
194 regs
.gs
= context
->SegGs
;
196 if (ptrace( PTRACE_SETREGS
, pid
, 0, ®s
) == -1) goto error
;
198 if (flags
& CONTEXT_DEBUG_REGISTERS
)
200 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(0), context
->Dr0
) == -1) goto error
;
201 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(1), context
->Dr1
) == -1) goto error
;
202 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(2), context
->Dr2
) == -1) goto error
;
203 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(3), context
->Dr3
) == -1) goto error
;
204 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(6), context
->Dr6
) == -1) goto error
;
205 if (ptrace( PTRACE_POKEUSER
, pid
, DR_OFFSET(7), context
->Dr7
) == -1) goto error
;
207 if (flags
& CONTEXT_FLOATING_POINT
)
209 /* we can use context->FloatSave directly as it is using the */
210 /* correct structure (the same as fsave/frstor) */
211 if (ptrace( PTRACE_SETFPREGS
, pid
, 0, &context
->FloatSave
) == -1) goto error
;
218 #elif defined(__sun) || defined(__sun__)
220 /* retrieve a thread context */
221 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
223 int pid
= thread
->unix_pid
;
224 if (flags
& CONTEXT_FULL
)
227 if (ptrace( PTRACE_GETREGS
, pid
, 0, (int) ®s
) == -1) goto error
;
228 if (flags
& CONTEXT_INTEGER
)
230 context
->Eax
= regs
.r_eax
;
231 context
->Ebx
= regs
.r_ebx
;
232 context
->Ecx
= regs
.r_ecx
;
233 context
->Edx
= regs
.r_edx
;
234 context
->Esi
= regs
.r_esi
;
235 context
->Edi
= regs
.r_edi
;
237 if (flags
& CONTEXT_CONTROL
)
239 context
->Ebp
= regs
.r_ebp
;
240 context
->Esp
= regs
.r_esp
;
241 context
->Eip
= regs
.r_eip
;
242 context
->SegCs
= regs
.r_cs
& 0xffff;
243 context
->SegSs
= regs
.r_ss
& 0xffff;
244 context
->EFlags
= regs
.r_efl
;
246 if (flags
& CONTEXT_SEGMENTS
)
248 context
->SegDs
= regs
.r_ds
& 0xffff;
249 context
->SegEs
= regs
.r_es
& 0xffff;
250 context
->SegFs
= regs
.r_fs
& 0xffff;
251 context
->SegGs
= regs
.r_gs
& 0xffff;
254 if (flags
& CONTEXT_DEBUG_REGISTERS
)
256 /* FIXME: How is this done on Solaris? */
258 if (flags
& CONTEXT_FLOATING_POINT
)
260 /* we can use context->FloatSave directly as it is using the */
261 /* correct structure (the same as fsave/frstor) */
262 if (ptrace( PTRACE_GETFPREGS
, pid
, 0, (int) &context
->FloatSave
) == -1) goto error
;
263 context
->FloatSave
.Cr0NpxState
= 0; /* FIXME */
271 /* set a thread context */
272 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
274 int pid
= thread
->unix_pid
;
275 if (flags
& CONTEXT_FULL
)
278 if (((flags
| CONTEXT_i386
) & CONTEXT_FULL
) != CONTEXT_FULL
)
280 /* need to preserve some registers */
281 if (ptrace( PTRACE_GETREGS
, pid
, 0, (int) ®s
) == -1) goto error
;
283 if (flags
& CONTEXT_INTEGER
)
285 regs
.r_eax
= context
->Eax
;
286 regs
.r_ebx
= context
->Ebx
;
287 regs
.r_ecx
= context
->Ecx
;
288 regs
.r_edx
= context
->Edx
;
289 regs
.r_esi
= context
->Esi
;
290 regs
.r_edi
= context
->Edi
;
292 if (flags
& CONTEXT_CONTROL
)
294 regs
.r_ebp
= context
->Ebp
;
295 regs
.r_esp
= context
->Esp
;
296 regs
.r_eip
= context
->Eip
;
297 regs
.r_cs
= context
->SegCs
;
298 regs
.r_ss
= context
->SegSs
;
299 regs
.r_efl
= context
->EFlags
;
301 if (flags
& CONTEXT_SEGMENTS
)
303 regs
.r_ds
= context
->SegDs
;
304 regs
.r_es
= context
->SegEs
;
305 regs
.r_fs
= context
->SegFs
;
306 regs
.r_gs
= context
->SegGs
;
308 if (ptrace( PTRACE_SETREGS
, pid
, 0, (int) ®s
) == -1) goto error
;
310 if (flags
& CONTEXT_DEBUG_REGISTERS
)
312 /* FIXME: How is this done on Solaris? */
314 if (flags
& CONTEXT_FLOATING_POINT
)
316 /* we can use context->FloatSave directly as it is using the */
317 /* correct structure (the same as fsave/frstor) */
318 if (ptrace( PTRACE_SETFPREGS
, pid
, 0, (int) &context
->FloatSave
) == -1) goto error
;
325 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
326 #include <machine/reg.h>
328 /* retrieve a thread context */
329 static void get_thread_context( struct thread
*thread
, unsigned int flags
, CONTEXT
*context
)
331 int pid
= thread
->unix_pid
;
332 if (flags
& CONTEXT_FULL
)
335 if (ptrace( PTRACE_GETREGS
, pid
, 0, (int) ®s
) == -1) goto error
;
336 if (flags
& CONTEXT_INTEGER
)
338 context
->Eax
= regs
.r_eax
;
339 context
->Ebx
= regs
.r_ebx
;
340 context
->Ecx
= regs
.r_ecx
;
341 context
->Edx
= regs
.r_edx
;
342 context
->Esi
= regs
.r_esi
;
343 context
->Edi
= regs
.r_edi
;
345 if (flags
& CONTEXT_CONTROL
)
347 context
->Ebp
= regs
.r_ebp
;
348 context
->Esp
= regs
.r_esp
;
349 context
->Eip
= regs
.r_eip
;
350 context
->SegCs
= regs
.r_cs
& 0xffff;
351 context
->SegSs
= regs
.r_ss
& 0xffff;
352 context
->EFlags
= regs
.r_eflags
;
354 if (flags
& CONTEXT_SEGMENTS
)
356 context
->SegDs
= regs
.r_ds
& 0xffff;
357 context
->SegEs
= regs
.r_es
& 0xffff;
358 context
->SegFs
= regs
.r_fs
& 0xffff;
359 context
->SegGs
= regs
.r_gs
& 0xffff;
362 if (flags
& CONTEXT_DEBUG_REGISTERS
)
364 /* FIXME: How is this done on FreeBSD? */
366 if (flags
& CONTEXT_FLOATING_POINT
)
368 /* we can use context->FloatSave directly as it is using the */
369 /* correct structure (the same as fsave/frstor) */
370 if (ptrace( PTRACE_GETFPREGS
, pid
, 0, (int) &context
->FloatSave
) == -1) goto error
;
371 context
->FloatSave
.Cr0NpxState
= 0; /* FIXME */
379 /* set a thread context */
380 static void set_thread_context( struct thread
*thread
, unsigned int flags
, const CONTEXT
*context
)
382 int pid
= thread
->unix_pid
;
383 if (flags
& CONTEXT_FULL
)
386 if (((flags
| CONTEXT_i386
) & CONTEXT_FULL
) != CONTEXT_FULL
)
388 /* need to preserve some registers */
389 if (ptrace( PTRACE_GETREGS
, pid
, 0, (int) ®s
) == -1) goto error
;
391 if (flags
& CONTEXT_INTEGER
)
393 regs
.r_eax
= context
->Eax
;
394 regs
.r_ebx
= context
->Ebx
;
395 regs
.r_ecx
= context
->Ecx
;
396 regs
.r_edx
= context
->Edx
;
397 regs
.r_esi
= context
->Esi
;
398 regs
.r_edi
= context
->Edi
;
400 if (flags
& CONTEXT_CONTROL
)
402 regs
.r_ebp
= context
->Ebp
;
403 regs
.r_esp
= context
->Esp
;
404 regs
.r_eip
= context
->Eip
;
405 regs
.r_cs
= context
->SegCs
;
406 regs
.r_ss
= context
->SegSs
;
407 regs
.r_eflags
= context
->EFlags
;
409 if (flags
& CONTEXT_SEGMENTS
)
411 regs
.r_ds
= context
->SegDs
;
412 regs
.r_es
= context
->SegEs
;
413 regs
.r_fs
= context
->SegFs
;
414 regs
.r_gs
= context
->SegGs
;
416 if (ptrace( PTRACE_SETREGS
, pid
, 0, (int) ®s
) == -1) goto error
;
418 if (flags
& CONTEXT_DEBUG_REGISTERS
)
420 /* FIXME: How is this done on FreeBSD? */
422 if (flags
& CONTEXT_FLOATING_POINT
)
424 /* we can use context->FloatSave directly as it is using the */
425 /* correct structure (the same as fsave/frstor) */
426 if (ptrace( PTRACE_SETFPREGS
, pid
, 0, (int) &context
->FloatSave
) == -1) goto error
;
433 #else /* linux || __sun__ || __FreeBSD__ */
434 #error You must implement get/set_thread_context for your platform
435 #endif /* linux || __sun__ || __FreeBSD__ */
438 /* copy a context structure according to the flags */
439 static void copy_context( CONTEXT
*to
, const CONTEXT
*from
, int flags
)
441 if (flags
& CONTEXT_CONTROL
)
446 to
->SegCs
= from
->SegCs
;
447 to
->SegSs
= from
->SegSs
;
448 to
->EFlags
= from
->EFlags
;
450 if (flags
& CONTEXT_INTEGER
)
459 if (flags
& CONTEXT_SEGMENTS
)
461 to
->SegDs
= from
->SegDs
;
462 to
->SegEs
= from
->SegEs
;
463 to
->SegFs
= from
->SegFs
;
464 to
->SegGs
= from
->SegGs
;
466 if (flags
& CONTEXT_FLOATING_POINT
)
468 to
->FloatSave
= from
->FloatSave
;
470 /* we don't bother copying the debug registers, since they */
471 /* always need to be accessed by ptrace anyway */
474 /* retrieve the current instruction pointer of a thread */
475 void *get_thread_ip( struct thread
*thread
)
479 if (suspend_for_ptrace( thread
))
481 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
482 resume_thread( thread
);
484 return (void *)context
.Eip
;
487 /* determine if we should continue the thread in single-step mode */
488 int get_thread_single_step( struct thread
*thread
)
491 if (thread
->context
) return 0; /* don't single-step inside exception event */
492 get_thread_context( thread
, CONTEXT_CONTROL
, &context
);
493 return (context
.EFlags
& 0x100) != 0;
496 /* retrieve the current context of a thread */
497 DECL_HANDLER(get_thread_context
)
499 struct thread
*thread
;
501 int flags
= req
->flags
& ~CONTEXT_i386
; /* get rid of CPU id */
503 if (get_reply_max_size() < sizeof(CONTEXT
))
505 set_error( STATUS_INVALID_PARAMETER
);
508 if (!(thread
= get_thread_from_handle( req
->handle
, THREAD_GET_CONTEXT
))) return;
510 if ((data
= set_reply_data_size( sizeof(CONTEXT
) )))
512 /* copy incoming context into reply */
513 memset( data
, 0, sizeof(CONTEXT
) );
514 memcpy( data
, get_req_data(), min( get_req_data_size(), sizeof(CONTEXT
) ));
516 if (thread
->context
) /* thread is inside an exception event */
518 copy_context( data
, thread
->context
, flags
);
519 flags
&= CONTEXT_DEBUG_REGISTERS
;
521 if (flags
&& suspend_for_ptrace( thread
))
523 get_thread_context( thread
, flags
, data
);
524 resume_thread( thread
);
527 release_object( thread
);
531 /* set the current context of a thread */
532 DECL_HANDLER(set_thread_context
)
534 struct thread
*thread
;
535 int flags
= req
->flags
& ~CONTEXT_i386
; /* get rid of CPU id */
537 if (get_req_data_size() < sizeof(CONTEXT
))
539 set_error( STATUS_INVALID_PARAMETER
);
542 if ((thread
= get_thread_from_handle( req
->handle
, THREAD_SET_CONTEXT
)))
544 if (thread
->context
) /* thread is inside an exception event */
546 copy_context( thread
->context
, get_req_data(), flags
);
547 flags
&= CONTEXT_DEBUG_REGISTERS
;
549 if (flags
&& suspend_for_ptrace( thread
))
551 set_thread_context( thread
, flags
, get_req_data() );
552 resume_thread( thread
);
554 release_object( thread
);
558 #endif /* __i386__ */