Fixed Solaris specific compiler issue.
[wine] / server / context_i386.c
1 /*
2  * i386 register context support
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #ifdef __i386__
10
11 #include <assert.h>
12 #include <errno.h>
13 #ifdef HAVE_SYS_REG_H
14 #include <sys/reg.h>
15 #endif
16 #include <unistd.h>
17 #include <sys/ptrace.h>
18 #ifdef HAVE_SYS_PARAM_H
19 # include <sys/param.h>
20 #endif
21 #include <sys/user.h>
22
23 #include "winbase.h"
24
25 #include "thread.h"
26 #include "request.h"
27
28
29 #ifndef PTRACE_PEEKUSER
30 #define PTRACE_PEEKUSER PT_READ_U
31 #endif
32 #ifndef PTRACE_POKEUSER
33 #define PTRACE_POKEUSER PT_WRITE_U
34 #endif
35 #ifndef PTRACE_GETREGS
36 #define PTRACE_GETREGS PT_GETREGS
37 #endif
38 #ifndef PTRACE_GETFPREGS
39 #define PTRACE_GETFPREGS PT_GETFPREGS
40 #endif
41 #ifndef PTRACE_SETREGS
42 #define PTRACE_SETREGS PT_SETREGS
43 #endif
44 #ifndef PTRACE_SETFPREGS
45 #define PTRACE_SETFPREGS PT_SETFPREGS
46 #endif
47
48 #ifdef linux
49
50 /* user_regs definitions from asm/user.h */
51 struct kernel_user_regs_struct
52 {
53     long ebx, ecx, edx, esi, edi, ebp, eax;
54     unsigned short ds, __ds, es, __es;
55     unsigned short fs, __fs, gs, __gs;
56     long orig_eax, eip;
57     unsigned short cs, __cs;
58     long eflags, esp;
59     unsigned short ss, __ss;
60 };
61
62 /* debug register offset in struct user */
63 #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
64
65 /* retrieve a debug register */
66 static inline int get_debug_reg( int pid, int num, DWORD *data )
67 {
68     int res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
69     if ((res == -1) && errno)
70     {
71         file_set_error();
72         return -1;
73     }
74     *data = res;
75     return 0;
76 }
77
78 /* retrieve a thread context */
79 static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
80 {
81     int pid = thread->unix_pid;
82     if (flags & CONTEXT_FULL)
83     {
84         struct kernel_user_regs_struct regs;
85         if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
86         if (flags & CONTEXT_INTEGER)
87         {
88             context->Eax = regs.eax;
89             context->Ebx = regs.ebx;
90             context->Ecx = regs.ecx;
91             context->Edx = regs.edx;
92             context->Esi = regs.esi;
93             context->Edi = regs.edi;
94         }
95         if (flags & CONTEXT_CONTROL)
96         {
97             context->Ebp    = regs.ebp;
98             context->Esp    = regs.esp;
99             context->Eip    = regs.eip;
100             context->SegCs  = regs.cs;
101             context->SegSs  = regs.ss;
102             context->EFlags = regs.eflags;
103         }
104         if (flags & CONTEXT_SEGMENTS)
105         {
106             context->SegDs = regs.ds;
107             context->SegEs = regs.es;
108             context->SegFs = regs.fs;
109             context->SegGs = regs.gs;
110         }
111     }
112     if (flags & CONTEXT_DEBUG_REGISTERS)
113     {
114         if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
115         if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
116         if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
117         if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
118         if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
119         if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
120     }
121     if (flags & CONTEXT_FLOATING_POINT)
122     {
123         /* we can use context->FloatSave directly as it is using the */
124         /* correct structure (the same as fsave/frstor) */
125         if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
126         context->FloatSave.Cr0NpxState = 0;  /* FIXME */
127     }
128     return;
129  error:
130     file_set_error();
131 }
132
133
134 /* set a thread context */
135 static void set_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
136 {
137     int pid = thread->unix_pid;
138     if (flags & CONTEXT_FULL)
139     {
140         struct kernel_user_regs_struct regs;
141         if ((flags & CONTEXT_FULL) != CONTEXT_FULL)  /* need to preserve some registers */
142         {
143             if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
144         }
145         if (flags & CONTEXT_INTEGER)
146         {
147             regs.eax = context->Eax;
148             regs.ebx = context->Ebx;
149             regs.ecx = context->Ecx;
150             regs.edx = context->Edx;
151             regs.esi = context->Esi;
152             regs.edi = context->Edi;
153         }
154         if (flags & CONTEXT_CONTROL)
155         {
156             regs.ebp = context->Ebp;
157             regs.esp = context->Esp;
158             regs.eip = context->Eip;
159             regs.cs  = context->SegCs;
160             regs.ss  = context->SegSs;
161             regs.eflags = context->EFlags;
162         }
163         if (flags & CONTEXT_SEGMENTS)
164         {
165             regs.ds = context->SegDs;
166             regs.es = context->SegEs;
167             regs.fs = context->SegFs;
168             regs.gs = context->SegGs;
169         }
170         if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
171     }
172     if (flags & CONTEXT_DEBUG_REGISTERS)
173     {
174         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
175         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
176         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
177         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
178         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
179         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
180     }
181     if (flags & CONTEXT_FLOATING_POINT)
182     {
183         /* we can use context->FloatSave directly as it is using the */
184         /* correct structure (the same as fsave/frstor) */
185         if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
186         context->FloatSave.Cr0NpxState = 0;  /* FIXME */
187     }
188     return;
189  error:
190     file_set_error();
191 }
192
193 #elif defined(__sun) || defined(__sun__)
194
195 /* retrieve a thread context */
196 static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
197 {
198     int pid = thread->unix_pid;
199     if (flags & CONTEXT_FULL)
200     {
201         struct regs regs;
202         if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
203         if (flags & CONTEXT_INTEGER)
204         {
205             context->Eax = regs.r_eax;
206             context->Ebx = regs.r_ebx;
207             context->Ecx = regs.r_ecx;
208             context->Edx = regs.r_edx;
209             context->Esi = regs.r_esi;
210             context->Edi = regs.r_edi;
211         }
212         if (flags & CONTEXT_CONTROL)
213         {
214             context->Ebp    = regs.r_ebp;
215             context->Esp    = regs.r_esp;
216             context->Eip    = regs.r_eip;
217             context->SegCs  = regs.r_cs & 0xffff;
218             context->SegSs  = regs.r_ss & 0xffff;
219             context->EFlags = regs.r_efl;
220         }
221         if (flags & CONTEXT_SEGMENTS)
222         {
223             context->SegDs = regs.r_ds & 0xffff;
224             context->SegEs = regs.r_es & 0xffff;
225             context->SegFs = regs.r_fs & 0xffff;
226             context->SegGs = regs.r_gs & 0xffff;
227         }
228     }
229     if (flags & CONTEXT_DEBUG_REGISTERS)
230     {
231         /* FIXME: How is this done on Solaris? */
232     }
233     if (flags & CONTEXT_FLOATING_POINT)
234     {
235         /* we can use context->FloatSave directly as it is using the */
236         /* correct structure (the same as fsave/frstor) */
237         if (ptrace( PTRACE_GETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
238         context->FloatSave.Cr0NpxState = 0;  /* FIXME */
239     }
240     return;
241  error:
242     file_set_error();
243 }
244
245
246 /* set a thread context */
247 static void set_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
248 {
249     int pid = thread->unix_pid;
250     if (flags & CONTEXT_FULL)
251     {
252         struct regs regs;
253         if ((flags & CONTEXT_FULL) != CONTEXT_FULL)  /* need to preserve some registers */
254         {
255             if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
256         }
257         if (flags & CONTEXT_INTEGER)
258         {
259             regs.r_eax = context->Eax;
260             regs.r_ebx = context->Ebx;
261             regs.r_ecx = context->Ecx;
262             regs.r_edx = context->Edx;
263             regs.r_esi = context->Esi;
264             regs.r_edi = context->Edi;
265         }
266         if (flags & CONTEXT_CONTROL)
267         {
268             regs.r_ebp = context->Ebp;
269             regs.r_esp = context->Esp;
270             regs.r_eip = context->Eip;
271             regs.r_cs = context->SegCs;
272             regs.r_ss = context->SegSs;
273             regs.r_efl = context->EFlags;
274         }
275         if (flags & CONTEXT_SEGMENTS)
276         {
277             regs.r_ds = context->SegDs;
278             regs.r_es = context->SegEs;
279             regs.r_fs = context->SegFs;
280             regs.r_gs = context->SegGs;
281         }
282         if (ptrace( PTRACE_SETREGS, pid, 0, (int) &regs ) == -1) goto error;
283     }
284     if (flags & CONTEXT_DEBUG_REGISTERS)
285     {
286         /* FIXME: How is this done on Solaris? */
287     }
288     if (flags & CONTEXT_FLOATING_POINT)
289     {
290         /* we can use context->FloatSave directly as it is using the */
291         /* correct structure (the same as fsave/frstor) */
292         if (ptrace( PTRACE_SETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
293         context->FloatSave.Cr0NpxState = 0;  /* FIXME */
294     }
295     return;
296  error:
297     file_set_error();
298 }
299
300 #elif defined(__FreeBSD__)
301 #include <machine/reg.h>
302
303 /* retrieve a thread context */
304 static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
305 {
306     int pid = thread->unix_pid;
307     if (flags & CONTEXT_FULL)
308     {
309         struct reg regs;
310         if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
311         if (flags & CONTEXT_INTEGER)
312         {
313             context->Eax = regs.r_eax;
314             context->Ebx = regs.r_ebx;
315             context->Ecx = regs.r_ecx;
316             context->Edx = regs.r_edx;
317             context->Esi = regs.r_esi;
318             context->Edi = regs.r_edi;
319         }
320         if (flags & CONTEXT_CONTROL)
321         {
322             context->Ebp    = regs.r_ebp;
323             context->Esp    = regs.r_esp;
324             context->Eip    = regs.r_eip;
325             context->SegCs  = regs.r_cs & 0xffff;
326             context->SegSs  = regs.r_ss & 0xffff;
327             context->EFlags = regs.r_eflags;
328         }
329         if (flags & CONTEXT_SEGMENTS)
330         {
331             context->SegDs = regs.r_ds & 0xffff;
332             context->SegEs = regs.r_es & 0xffff;
333             context->SegFs = regs.r_fs & 0xffff;
334             context->SegGs = regs.r_gs & 0xffff;
335         }
336     }
337     if (flags & CONTEXT_DEBUG_REGISTERS)
338     {
339         /* FIXME: How is this done on FreeBSD? */
340     }
341     if (flags & CONTEXT_FLOATING_POINT)
342     {
343         /* we can use context->FloatSave directly as it is using the */
344         /* correct structure (the same as fsave/frstor) */
345         if (ptrace( PTRACE_GETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
346         context->FloatSave.Cr0NpxState = 0;  /* FIXME */
347     }
348     return;
349  error:
350     file_set_error();
351 }
352
353
354 /* set a thread context */
355 static void set_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
356 {
357     int pid = thread->unix_pid;
358     if (flags & CONTEXT_FULL)
359     {
360         struct reg regs;
361         if ((flags & CONTEXT_FULL) != CONTEXT_FULL)  /* need to preserve some registers */
362         {
363             if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
364         }
365         if (flags & CONTEXT_INTEGER)
366         {
367             regs.r_eax = context->Eax;
368             regs.r_ebx = context->Ebx;
369             regs.r_ecx = context->Ecx;
370             regs.r_edx = context->Edx;
371             regs.r_esi = context->Esi;
372             regs.r_edi = context->Edi;
373         }
374         if (flags & CONTEXT_CONTROL)
375         {
376             regs.r_ebp = context->Ebp;
377             regs.r_esp = context->Esp;
378             regs.r_eip = context->Eip;
379             regs.r_cs = context->SegCs;
380             regs.r_ss = context->SegSs;
381             regs.r_eflags = context->EFlags;
382         }
383         if (flags & CONTEXT_SEGMENTS)
384         {
385             regs.r_ds = context->SegDs;
386             regs.r_es = context->SegEs;
387             regs.r_fs = context->SegFs;
388             regs.r_gs = context->SegGs;
389         }
390         if (ptrace( PTRACE_SETREGS, pid, 0, (int) &regs ) == -1) goto error;
391     }
392     if (flags & CONTEXT_DEBUG_REGISTERS)
393     {
394         /* FIXME: How is this done on FreeBSD? */
395     }
396     if (flags & CONTEXT_FLOATING_POINT)
397     {
398         /* we can use context->FloatSave directly as it is using the */
399         /* correct structure (the same as fsave/frstor) */
400         if (ptrace( PTRACE_SETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
401         context->FloatSave.Cr0NpxState = 0;  /* FIXME */
402     }
403     return;
404  error:
405     file_set_error();
406 }
407
408 #else  /* linux || __sun__ || __FreeBSD__ */
409 #error You must implement get/set_thread_context for your platform
410 #endif  /* linux || __sun__ || __FreeBSD__ */
411
412
413 /* copy a context structure according to the flags */
414 static void copy_context( CONTEXT *to, CONTEXT *from, int flags )
415 {
416     if (flags & CONTEXT_CONTROL)
417     {
418         to->Ebp    = from->Ebp;
419         to->Eip    = from->Eip;
420         to->Esp    = from->Esp;
421         to->SegCs  = from->SegCs;
422         to->SegSs  = from->SegSs;
423         to->EFlags = from->EFlags;
424     }
425     if (flags & CONTEXT_INTEGER)
426     {
427         to->Eax = from->Eax;
428         to->Ebx = from->Ebx;
429         to->Ecx = from->Ecx;
430         to->Edx = from->Edx;
431         to->Esi = from->Esi;
432         to->Edi = from->Edi;
433     }
434     if (flags & CONTEXT_SEGMENTS)
435     {
436         to->SegDs = from->SegDs;
437         to->SegEs = from->SegEs;
438         to->SegFs = from->SegFs;
439         to->SegGs = from->SegGs;
440     }
441     if (flags & CONTEXT_DEBUG_REGISTERS)
442     {
443         to->Dr0 = from->Dr0;
444         to->Dr1 = from->Dr1;
445         to->Dr2 = from->Dr2;
446         to->Dr3 = from->Dr3;
447         to->Dr6 = from->Dr6;
448         to->Dr7 = from->Dr7;
449     }
450     if (flags & CONTEXT_FLOATING_POINT)
451     {
452         to->FloatSave = from->FloatSave;
453     }
454 }
455
456 /* retrieve the current context of a thread */
457 DECL_HANDLER(get_thread_context)
458 {
459     struct thread *thread;
460     CONTEXT *context;
461
462     if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
463     {
464         if ((context = get_debug_context( thread )))  /* thread is inside an exception event */
465         {
466             copy_context( &req->context, context, req->flags );
467         }
468         else
469         {
470             suspend_thread( thread, 0 );
471             if (thread->attached) get_thread_context( thread, req->flags, &req->context );
472             else set_error( STATUS_ACCESS_DENIED );
473             resume_thread( thread );
474         }
475         release_object( thread );
476     }
477 }
478
479
480 /* set the current context of a thread */
481 DECL_HANDLER(set_thread_context)
482 {
483     struct thread *thread;
484     CONTEXT *context;
485
486     if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
487     {
488         if ((context = get_debug_context( thread )))  /* thread is inside an exception event */
489         {
490             copy_context( context, &req->context, req->flags );
491         }
492         else
493         {
494             suspend_thread( thread, 0 );
495             if (thread->attached) set_thread_context( thread, req->flags, &req->context );
496             else set_error( STATUS_ACCESS_DENIED );
497             resume_thread( thread );
498         }
499         release_object( thread );
500     }
501 }
502
503 #endif  /* __i386__ */