Release 20050930.
[wine] / server / context_x86_64.c
1 /*
2  * x86-64 register context support
3  *
4  * Copyright (C) 1999, 2005 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #ifdef __x86_64__
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <unistd.h>
29 #ifdef HAVE_SYS_PTRACE_H
30 # include <sys/ptrace.h>
31 #endif
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif
35
36 #define NONAMELESSUNION
37 #include "windef.h"
38 #include "winbase.h"
39
40 #include "file.h"
41 #include "thread.h"
42 #include "request.h"
43
44 #ifdef __linux__
45
46 #ifdef HAVE_SYS_USER_H
47 # include <sys/user.h>
48 #endif
49
50 /* debug register offset in struct user */
51 #define DR_OFFSET(dr) ((unsigned long)((((struct user *)0)->u_debugreg) + (dr)))
52
53 /* retrieve a debug register */
54 static inline int get_debug_reg( int pid, int num, DWORD64 *data )
55 {
56     int res;
57     errno = 0;
58     res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
59     if ((res == -1) && errno)
60     {
61         file_set_error();
62         return -1;
63     }
64     *data = res;
65     return 0;
66 }
67
68 /* retrieve a thread context */
69 static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
70 {
71     int pid = get_ptrace_pid(thread);
72     if (flags & CONTEXT_FULL)
73     {
74         struct user_regs_struct regs;
75         if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
76         if (flags & CONTEXT_INTEGER)
77         {
78             context->Rax = regs.rax;
79             context->Rbx = regs.rbx;
80             context->Rcx = regs.rcx;
81             context->Rdx = regs.rdx;
82             context->Rsi = regs.rsi;
83             context->Rdi = regs.rdi;
84             context->R8  = regs.r8;
85             context->R9  = regs.r9;
86             context->R10 = regs.r10;
87             context->R11 = regs.r11;
88             context->R12 = regs.r12;
89             context->R13 = regs.r13;
90             context->R14 = regs.r14;
91             context->R15 = regs.r15;
92         }
93         if (flags & CONTEXT_CONTROL)
94         {
95             context->Rbp    = regs.rbp;
96             context->Rsp    = regs.rsp;
97             context->Rip    = regs.rip;
98             context->SegCs  = regs.cs;
99             context->SegSs  = regs.ss;
100             context->EFlags = regs.eflags;
101         }
102         if (flags & CONTEXT_SEGMENTS)
103         {
104             context->SegDs = regs.ds;
105             context->SegEs = regs.es;
106             context->SegFs = regs.fs;
107             context->SegGs = regs.gs;
108         }
109     }
110     if (flags & CONTEXT_DEBUG_REGISTERS)
111     {
112         if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
113         if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
114         if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
115         if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
116         if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
117         if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
118     }
119     if (flags & CONTEXT_FLOATING_POINT)
120     {
121         /* we can use context->FloatSave directly as it is using the */
122         /* correct structure (the same as fsave/frstor) */
123         if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->u.FltSave ) == -1) goto error;
124     }
125     return;
126  error:
127     file_set_error();
128 }
129
130
131 /* set a thread context */
132 static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
133 {
134     int pid = get_ptrace_pid(thread);
135     if (flags & CONTEXT_FULL)
136     {
137         struct user_regs_struct regs;
138
139         /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
140         if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
141
142         if (flags & CONTEXT_INTEGER)
143         {
144             regs.rax = context->Rax;
145             regs.rbx = context->Rbx;
146             regs.rcx = context->Rcx;
147             regs.rdx = context->Rdx;
148             regs.rsi = context->Rsi;
149             regs.rdi = context->Rdi;
150             regs.r8  = context->R8;
151             regs.r9  = context->R9;
152             regs.r10 = context->R10;
153             regs.r11 = context->R11;
154             regs.r12 = context->R12;
155             regs.r13 = context->R13;
156             regs.r14 = context->R14;
157             regs.r15 = context->R15;
158         }
159         if (flags & CONTEXT_CONTROL)
160         {
161             regs.rbp = context->Rbp;
162             regs.rip = context->Rip;
163             regs.rsp = context->Rsp;
164             regs.cs  = context->SegCs;
165             regs.ss  = context->SegSs;
166             regs.eflags = context->EFlags;
167         }
168         if (flags & CONTEXT_SEGMENTS)
169         {
170             regs.ds = context->SegDs;
171             regs.es = context->SegEs;
172             regs.fs = context->SegFs;
173             regs.gs = context->SegGs;
174         }
175         if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
176     }
177     if (flags & CONTEXT_DEBUG_REGISTERS)
178     {
179         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
180         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
181         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
182         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
183         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
184         if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
185     }
186     if (flags & CONTEXT_FLOATING_POINT)
187     {
188         /* we can use context->FloatSave directly as it is using the */
189         /* correct structure (the same as fsave/frstor) */
190         if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->u.FltSave ) == -1) goto error;
191     }
192     return;
193  error:
194     file_set_error();
195 }
196
197 #else  /* linux */
198 #error You must implement get/set_thread_context for your platform
199 #endif  /* linux */
200
201
202 /* copy a context structure according to the flags */
203 static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
204 {
205     if (flags & CONTEXT_CONTROL)
206     {
207         to->Rbp    = from->Rbp;
208         to->Rip    = from->Rip;
209         to->Rsp    = from->Rsp;
210         to->SegCs  = from->SegCs;
211         to->SegSs  = from->SegSs;
212         to->EFlags = from->EFlags;
213         to->MxCsr  = from->MxCsr;
214     }
215     if (flags & CONTEXT_INTEGER)
216     {
217         to->Rax = from->Rax;
218         to->Rcx = from->Rcx;
219         to->Rdx = from->Rdx;
220         to->Rbx = from->Rbx;
221         to->Rsi = from->Rsi;
222         to->Rdi = from->Rdi;
223         to->R8  = from->R8;
224         to->R9  = from->R9;
225         to->R10 = from->R10;
226         to->R11 = from->R11;
227         to->R12 = from->R12;
228         to->R13 = from->R13;
229         to->R14 = from->R14;
230         to->R15 = from->R15;
231     }
232     if (flags & CONTEXT_SEGMENTS)
233     {
234         to->SegDs = from->SegDs;
235         to->SegEs = from->SegEs;
236         to->SegFs = from->SegFs;
237         to->SegGs = from->SegGs;
238     }
239     if (flags & CONTEXT_FLOATING_POINT)
240     {
241         to->u.FltSave = from->u.FltSave;
242     }
243     /* we don't bother copying the debug registers, since they */
244     /* always need to be accessed by ptrace anyway */
245 }
246
247 /* retrieve the current instruction pointer of a thread */
248 void *get_thread_ip( struct thread *thread )
249 {
250     CONTEXT context;
251     context.Rip = 0;
252     if (suspend_for_ptrace( thread ))
253     {
254         get_thread_context( thread, CONTEXT_CONTROL, &context );
255         resume_after_ptrace( thread );
256     }
257     return (void *)context.Rip;
258 }
259
260 /* determine if we should continue the thread in single-step mode */
261 int get_thread_single_step( struct thread *thread )
262 {
263     CONTEXT context;
264     if (thread->context) return 0;  /* don't single-step inside exception event */
265     get_thread_context( thread, CONTEXT_CONTROL, &context );
266     return (context.EFlags & 0x100) != 0;
267 }
268
269 /* send a signal to a specific thread */
270 int tkill( int pid, int sig )
271 {
272 #ifdef __linux__
273     int ret;
274     __asm__( "syscall" : "=a" (ret)
275              : "0" (200) /*SYS_tkill*/, "D" (pid), "S" (sig) );
276     if (ret >= 0) return ret;
277     errno = -ret;
278     return -1;
279 #else
280     errno = ENOSYS;
281     return -1;
282 #endif
283 }
284
285 /* retrieve the current context of a thread */
286 DECL_HANDLER(get_thread_context)
287 {
288     struct thread *thread;
289     void *data;
290     int flags = req->flags & ~CONTEXT_AMD64;  /* get rid of CPU id */
291
292     if (get_reply_max_size() < sizeof(CONTEXT))
293     {
294         set_error( STATUS_INVALID_PARAMETER );
295         return;
296     }
297     if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
298
299     if ((data = set_reply_data_size( sizeof(CONTEXT) )))
300     {
301         /* copy incoming context into reply */
302         memset( data, 0, sizeof(CONTEXT) );
303         memcpy( data, get_req_data(), min( get_req_data_size(), sizeof(CONTEXT) ));
304
305         if (thread->context)  /* thread is inside an exception event */
306         {
307             copy_context( data, thread->context, flags );
308             flags &= CONTEXT_DEBUG_REGISTERS;
309         }
310         if (flags && suspend_for_ptrace( thread ))
311         {
312             get_thread_context( thread, flags, data );
313             resume_after_ptrace( thread );
314         }
315     }
316     release_object( thread );
317 }
318
319
320 /* set the current context of a thread */
321 DECL_HANDLER(set_thread_context)
322 {
323     struct thread *thread;
324     int flags = req->flags & ~CONTEXT_AMD64;  /* get rid of CPU id */
325
326     if (get_req_data_size() < sizeof(CONTEXT))
327     {
328         set_error( STATUS_INVALID_PARAMETER );
329         return;
330     }
331     if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
332     {
333         if (thread->context)  /* thread is inside an exception event */
334         {
335             copy_context( thread->context, get_req_data(), flags );
336             flags &= CONTEXT_DEBUG_REGISTERS;
337         }
338         if (flags && suspend_for_ptrace( thread ))
339         {
340             set_thread_context( thread, flags, get_req_data() );
341             resume_after_ptrace( thread );
342         }
343         release_object( thread );
344     }
345 }
346
347 #endif  /* __x86_64__ */