Remove some redundant null pointer checks.
[wine/multimedia.git] / server / context_i386.c
blobc2a985bf2c06fcdd2f6968fdb9826ec03aba376b
1 /*
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
21 #include "config.h"
23 #ifdef __i386__
25 #include <assert.h>
26 #include <errno.h>
27 #ifdef HAVE_SYS_REG_H
28 #include <sys/reg.h>
29 #endif
30 #include <stdarg.h>
31 #include <unistd.h>
32 #ifdef HAVE_SYS_PTRACE_H
33 # include <sys/ptrace.h>
34 #endif
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
39 #include "windef.h"
41 #include "file.h"
42 #include "thread.h"
43 #include "request.h"
45 #ifndef PTRACE_PEEKUSER
46 # ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
47 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
48 # else
49 # define PTRACE_PEEKUSER PT_READ_U
50 # endif
51 #endif
53 #ifndef PTRACE_POKEUSER
54 # ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
55 # define PTRACE_POKEUSER PTRACE_POKEUSR
56 # else
57 # define PTRACE_POKEUSER PT_WRITE_U
58 # endif
59 #endif
61 #ifndef PTRACE_GETREGS
62 #define PTRACE_GETREGS PT_GETREGS
63 #endif
64 #ifndef PTRACE_GETFPREGS
65 #define PTRACE_GETFPREGS PT_GETFPREGS
66 #endif
67 #ifndef PTRACE_SETREGS
68 #define PTRACE_SETREGS PT_SETREGS
69 #endif
70 #ifndef PTRACE_SETFPREGS
71 #define PTRACE_SETFPREGS PT_SETFPREGS
72 #endif
74 #ifdef PT_GETDBREGS
75 #define PTRACE_GETDBREGS PT_GETDBREGS
76 #endif
78 #ifdef PT_SETDBREGS
79 #define PTRACE_SETDBREGS PT_SETDBREGS
80 #endif
82 #ifdef linux
83 #ifdef HAVE_SYS_USER_H
84 # include <sys/user.h>
85 #endif
87 /* user_regs definitions from asm/user.h */
88 struct kernel_user_regs_struct
90 long ebx, ecx, edx, esi, edi, ebp, eax;
91 unsigned short ds, __ds, es, __es;
92 unsigned short fs, __fs, gs, __gs;
93 long orig_eax, eip;
94 unsigned short cs, __cs;
95 long eflags, esp;
96 unsigned short ss, __ss;
99 /* debug register offset in struct user */
100 #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
102 /* retrieve a debug register */
103 static inline int get_debug_reg( int pid, int num, DWORD *data )
105 int res;
106 errno = 0;
107 res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
108 if ((res == -1) && errno)
110 file_set_error();
111 return -1;
113 *data = res;
114 return 0;
117 /* retrieve a thread context */
118 static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
120 int pid = get_ptrace_pid(thread);
121 if (flags & CONTEXT_FULL)
123 struct kernel_user_regs_struct regs;
124 if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
125 if (flags & CONTEXT_INTEGER)
127 context->Eax = regs.eax;
128 context->Ebx = regs.ebx;
129 context->Ecx = regs.ecx;
130 context->Edx = regs.edx;
131 context->Esi = regs.esi;
132 context->Edi = regs.edi;
134 if (flags & CONTEXT_CONTROL)
136 context->Ebp = regs.ebp;
137 context->Esp = regs.esp;
138 context->Eip = regs.eip;
139 context->SegCs = regs.cs;
140 context->SegSs = regs.ss;
141 context->EFlags = regs.eflags;
143 if (flags & CONTEXT_SEGMENTS)
145 context->SegDs = regs.ds;
146 context->SegEs = regs.es;
147 context->SegFs = regs.fs;
148 context->SegGs = regs.gs;
151 if (flags & CONTEXT_DEBUG_REGISTERS)
153 if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
154 if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
155 if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
156 if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
157 if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
158 if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
160 if (flags & CONTEXT_FLOATING_POINT)
162 /* we can use context->FloatSave directly as it is using the */
163 /* correct structure (the same as fsave/frstor) */
164 if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
165 context->FloatSave.Cr0NpxState = 0; /* FIXME */
167 return;
168 error:
169 file_set_error();
173 /* set a thread context */
174 static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
176 int pid = get_ptrace_pid(thread);
177 if (flags & CONTEXT_FULL)
179 struct kernel_user_regs_struct regs;
181 /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
182 if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
184 if (flags & CONTEXT_INTEGER)
186 regs.eax = context->Eax;
187 regs.ebx = context->Ebx;
188 regs.ecx = context->Ecx;
189 regs.edx = context->Edx;
190 regs.esi = context->Esi;
191 regs.edi = context->Edi;
193 if (flags & CONTEXT_CONTROL)
195 regs.ebp = context->Ebp;
196 regs.esp = context->Esp;
197 regs.eip = context->Eip;
198 regs.cs = context->SegCs;
199 regs.ss = context->SegSs;
200 regs.eflags = context->EFlags;
202 if (flags & CONTEXT_SEGMENTS)
204 regs.ds = context->SegDs;
205 regs.es = context->SegEs;
206 regs.fs = context->SegFs;
207 regs.gs = context->SegGs;
209 if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
211 if (flags & CONTEXT_DEBUG_REGISTERS)
213 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
214 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
215 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
216 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
217 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
218 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
220 if (flags & CONTEXT_FLOATING_POINT)
222 /* we can use context->FloatSave directly as it is using the */
223 /* correct structure (the same as fsave/frstor) */
224 if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
226 return;
227 error:
228 file_set_error();
231 #elif defined(__sun) || defined(__sun__)
233 /* retrieve a thread context */
234 static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
236 int pid = get_ptrace_pid(thread);
237 if (flags & CONTEXT_FULL)
239 struct regs regs;
240 if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
241 if (flags & CONTEXT_INTEGER)
243 context->Eax = regs.r_eax;
244 context->Ebx = regs.r_ebx;
245 context->Ecx = regs.r_ecx;
246 context->Edx = regs.r_edx;
247 context->Esi = regs.r_esi;
248 context->Edi = regs.r_edi;
250 if (flags & CONTEXT_CONTROL)
252 context->Ebp = regs.r_ebp;
253 context->Esp = regs.r_esp;
254 context->Eip = regs.r_eip;
255 context->SegCs = regs.r_cs & 0xffff;
256 context->SegSs = regs.r_ss & 0xffff;
257 context->EFlags = regs.r_efl;
259 if (flags & CONTEXT_SEGMENTS)
261 context->SegDs = regs.r_ds & 0xffff;
262 context->SegEs = regs.r_es & 0xffff;
263 context->SegFs = regs.r_fs & 0xffff;
264 context->SegGs = regs.r_gs & 0xffff;
267 if (flags & CONTEXT_DEBUG_REGISTERS)
269 /* FIXME: How is this done on Solaris? */
271 if (flags & CONTEXT_FLOATING_POINT)
273 /* we can use context->FloatSave directly as it is using the */
274 /* correct structure (the same as fsave/frstor) */
275 if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
276 context->FloatSave.Cr0NpxState = 0; /* FIXME */
278 return;
279 error:
280 file_set_error();
284 /* set a thread context */
285 static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
287 int pid = get_ptrace_pid(thread);
288 if (flags & CONTEXT_FULL)
290 struct regs regs;
291 if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
293 /* need to preserve some registers */
294 if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
296 if (flags & CONTEXT_INTEGER)
298 regs.r_eax = context->Eax;
299 regs.r_ebx = context->Ebx;
300 regs.r_ecx = context->Ecx;
301 regs.r_edx = context->Edx;
302 regs.r_esi = context->Esi;
303 regs.r_edi = context->Edi;
305 if (flags & CONTEXT_CONTROL)
307 regs.r_ebp = context->Ebp;
308 regs.r_esp = context->Esp;
309 regs.r_eip = context->Eip;
310 regs.r_cs = context->SegCs;
311 regs.r_ss = context->SegSs;
312 regs.r_efl = context->EFlags;
314 if (flags & CONTEXT_SEGMENTS)
316 regs.r_ds = context->SegDs;
317 regs.r_es = context->SegEs;
318 regs.r_fs = context->SegFs;
319 regs.r_gs = context->SegGs;
321 if (ptrace( PTRACE_SETREGS, pid, (int) &regs, 0 ) == -1) goto error;
323 if (flags & CONTEXT_DEBUG_REGISTERS)
325 /* FIXME: How is this done on Solaris? */
327 if (flags & CONTEXT_FLOATING_POINT)
329 /* we can use context->FloatSave directly as it is using the */
330 /* correct structure (the same as fsave/frstor) */
331 if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
333 return;
334 error:
335 file_set_error();
338 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
339 #include <machine/reg.h>
341 /* retrieve a thread context */
342 static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
344 int pid = get_ptrace_pid(thread);
345 if (flags & CONTEXT_FULL)
347 struct reg regs;
348 if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
349 if (flags & CONTEXT_INTEGER)
351 context->Eax = regs.r_eax;
352 context->Ebx = regs.r_ebx;
353 context->Ecx = regs.r_ecx;
354 context->Edx = regs.r_edx;
355 context->Esi = regs.r_esi;
356 context->Edi = regs.r_edi;
358 if (flags & CONTEXT_CONTROL)
360 context->Ebp = regs.r_ebp;
361 context->Esp = regs.r_esp;
362 context->Eip = regs.r_eip;
363 context->SegCs = regs.r_cs & 0xffff;
364 context->SegSs = regs.r_ss & 0xffff;
365 context->EFlags = regs.r_eflags;
367 if (flags & CONTEXT_SEGMENTS)
369 context->SegDs = regs.r_ds & 0xffff;
370 context->SegEs = regs.r_es & 0xffff;
371 context->SegFs = regs.r_fs & 0xffff;
372 context->SegGs = regs.r_gs & 0xffff;
375 if (flags & CONTEXT_DEBUG_REGISTERS)
377 #ifdef PTRACE_GETDBREGS
378 struct dbreg dbregs;
379 if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
380 goto error;
381 #ifdef DBREG_DRX
382 /* needed for FreeBSD, the structure fields have changed under 5.x */
383 context->Dr0 = DBREG_DRX((&dbregs), 0);
384 context->Dr1 = DBREG_DRX((&dbregs), 1);
385 context->Dr2 = DBREG_DRX((&dbregs), 2);
386 context->Dr3 = DBREG_DRX((&dbregs), 3);
387 context->Dr6 = DBREG_DRX((&dbregs), 6);
388 context->Dr7 = DBREG_DRX((&dbregs), 7);
389 #else
390 context->Dr0 = dbregs.dr0;
391 context->Dr1 = dbregs.dr1;
392 context->Dr2 = dbregs.dr2;
393 context->Dr3 = dbregs.dr3;
394 context->Dr6 = dbregs.dr6;
395 context->Dr7 = dbregs.dr7;
396 #endif
398 #endif
400 if (flags & CONTEXT_FLOATING_POINT)
402 /* we can use context->FloatSave directly as it is using the */
403 /* correct structure (the same as fsave/frstor) */
404 if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
405 context->FloatSave.Cr0NpxState = 0; /* FIXME */
407 return;
408 error:
409 file_set_error();
413 /* set a thread context */
414 static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
416 int pid = get_ptrace_pid(thread);
417 if (flags & CONTEXT_FULL)
419 struct reg regs;
420 if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
422 /* need to preserve some registers */
423 if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
425 if (flags & CONTEXT_INTEGER)
427 regs.r_eax = context->Eax;
428 regs.r_ebx = context->Ebx;
429 regs.r_ecx = context->Ecx;
430 regs.r_edx = context->Edx;
431 regs.r_esi = context->Esi;
432 regs.r_edi = context->Edi;
434 if (flags & CONTEXT_CONTROL)
436 regs.r_ebp = context->Ebp;
437 regs.r_esp = context->Esp;
438 regs.r_eip = context->Eip;
439 regs.r_cs = context->SegCs;
440 regs.r_ss = context->SegSs;
441 regs.r_eflags = context->EFlags;
443 if (flags & CONTEXT_SEGMENTS)
445 regs.r_ds = context->SegDs;
446 regs.r_es = context->SegEs;
447 regs.r_fs = context->SegFs;
448 regs.r_gs = context->SegGs;
450 if (ptrace( PTRACE_SETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
452 if (flags & CONTEXT_DEBUG_REGISTERS)
454 #ifdef PTRACE_SETDBREGS
455 struct dbreg dbregs;
456 #ifdef DBREG_DRX
457 /* needed for FreeBSD, the structure fields have changed under 5.x */
458 DBREG_DRX((&dbregs), 0) = context->Dr0;
459 DBREG_DRX((&dbregs), 1) = context->Dr1;
460 DBREG_DRX((&dbregs), 2) = context->Dr2;
461 DBREG_DRX((&dbregs), 3) = context->Dr3;
462 DBREG_DRX((&dbregs), 4) = 0;
463 DBREG_DRX((&dbregs), 5) = 0;
464 DBREG_DRX((&dbregs), 6) = context->Dr6;
465 DBREG_DRX((&dbregs), 7) = context->Dr7;
466 #else
467 dbregs.dr0 = context->Dr0;
468 dbregs.dr1 = context->Dr1;
469 dbregs.dr2 = context->Dr2;
470 dbregs.dr3 = context->Dr3;
471 dbregs.dr4 = 0;
472 dbregs.dr5 = 0;
473 dbregs.dr6 = context->Dr6;
474 dbregs.dr7 = context->Dr7;
475 #endif
476 if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
477 goto error;
478 #endif
480 if (flags & CONTEXT_FLOATING_POINT)
482 /* we can use context->FloatSave directly as it is using the */
483 /* correct structure (the same as fsave/frstor) */
484 if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
486 return;
487 error:
488 file_set_error();
491 #else /* linux || __sun__ || __FreeBSD__ */
492 #error You must implement get/set_thread_context_ptrace for your platform
493 #endif /* linux || __sun__ || __FreeBSD__ */
496 /* copy a context structure according to the flags */
497 static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
499 if (flags & CONTEXT_CONTROL)
501 to->Ebp = from->Ebp;
502 to->Eip = from->Eip;
503 to->Esp = from->Esp;
504 to->SegCs = from->SegCs;
505 to->SegSs = from->SegSs;
506 to->EFlags = from->EFlags;
508 if (flags & CONTEXT_INTEGER)
510 to->Eax = from->Eax;
511 to->Ebx = from->Ebx;
512 to->Ecx = from->Ecx;
513 to->Edx = from->Edx;
514 to->Esi = from->Esi;
515 to->Edi = from->Edi;
517 if (flags & CONTEXT_SEGMENTS)
519 to->SegDs = from->SegDs;
520 to->SegEs = from->SegEs;
521 to->SegFs = from->SegFs;
522 to->SegGs = from->SegGs;
524 if (flags & CONTEXT_FLOATING_POINT)
526 to->FloatSave = from->FloatSave;
528 /* we don't bother copying the debug registers, since they */
529 /* always need to be accessed by ptrace anyway */
532 /* retrieve the current instruction pointer of a thread */
533 void *get_thread_ip( struct thread *thread )
535 CONTEXT context;
536 context.Eip = 0;
537 if (suspend_for_ptrace( thread ))
539 get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
540 resume_after_ptrace( thread );
542 return (void *)context.Eip;
545 /* determine if we should continue the thread in single-step mode */
546 int get_thread_single_step( struct thread *thread )
548 CONTEXT context;
549 if (thread->context) return 0; /* don't single-step inside exception event */
550 get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
551 return (context.EFlags & 0x100) != 0;
554 /* send a signal to a specific thread */
555 int tkill( int pid, int sig )
557 #ifdef __linux__
558 int ret;
559 __asm__( "pushl %%ebx\n\t"
560 "movl %2,%%ebx\n\t"
561 "int $0x80\n\t"
562 "popl %%ebx\n\t"
563 : "=a" (ret)
564 : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
565 if (ret >= 0) return ret;
566 errno = -ret;
567 return -1;
568 #else
569 errno = ENOSYS;
570 return -1;
571 #endif
574 /* retrieve the thread context */
575 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
577 flags &= ~CONTEXT_i386; /* get rid of CPU id */
579 if (thread->context) /* thread is inside an exception event or suspended */
581 copy_context( context, thread->context, flags );
582 flags &= CONTEXT_DEBUG_REGISTERS;
585 if (flags && suspend_for_ptrace( thread ))
587 get_thread_context_ptrace( thread, flags, context );
588 resume_after_ptrace( thread );
592 /* set the thread context */
593 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
595 flags &= ~CONTEXT_i386; /* get rid of CPU id */
597 if (thread->context) /* thread is inside an exception event or suspended */
599 copy_context( thread->context, context, flags );
600 flags &= CONTEXT_DEBUG_REGISTERS;
603 if (flags && suspend_for_ptrace( thread ))
605 set_thread_context_ptrace( thread, flags, context );
606 resume_after_ptrace( thread );
610 #endif /* __i386__ */