advpack: Remove some dead code. (Coverity)
[wine/multimedia.git] / server / context_x86_64.c
blobf2200ae1a80e8c069f878b702f8caaa557f45387
1 /*
2 * x86-64 register context support
4 * Copyright (C) 1999, 2005 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #ifdef __x86_64__
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
36 #define NONAMELESSUNION
37 #include "windef.h"
38 #include "winbase.h"
40 #include "file.h"
41 #include "thread.h"
42 #include "request.h"
44 #ifdef __linux__
46 #ifdef HAVE_SYS_USER_H
47 # include <sys/user.h>
48 #endif
50 /* debug register offset in struct user */
51 #define DR_OFFSET(dr) ((unsigned long)((((struct user *)0)->u_debugreg) + (dr)))
53 /* retrieve a debug register */
54 static inline int get_debug_reg( int pid, int num, DWORD64 *data )
56 int res;
57 errno = 0;
58 res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
59 if ((res == -1) && errno)
61 file_set_error();
62 return -1;
64 *data = res;
65 return 0;
68 /* retrieve a thread context */
69 static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
71 int pid = get_ptrace_pid(thread);
72 if (flags & CONTEXT_FULL)
74 struct user_regs_struct regs;
75 if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
76 if (flags & CONTEXT_INTEGER)
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;
93 if (flags & CONTEXT_CONTROL)
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;
102 if (flags & CONTEXT_SEGMENTS)
104 context->SegDs = regs.ds;
105 context->SegEs = regs.es;
106 context->SegFs = regs.fs;
107 context->SegGs = regs.gs;
109 context->ContextFlags |= flags & CONTEXT_FULL;
111 if (flags & CONTEXT_DEBUG_REGISTERS)
113 if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
114 if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
115 if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
116 if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
117 if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
118 if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
119 context->ContextFlags |= CONTEXT_DEBUG_REGISTERS;
121 if (flags & CONTEXT_FLOATING_POINT)
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->u.FltSave ) == -1) goto error;
126 context->ContextFlags |= CONTEXT_FLOATING_POINT;
128 return;
129 error:
130 file_set_error();
134 /* set a thread context */
135 static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
137 int pid = get_ptrace_pid(thread);
138 if (flags & CONTEXT_FULL)
140 struct user_regs_struct regs;
142 /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
143 if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
145 if (flags & CONTEXT_INTEGER)
147 regs.rax = context->Rax;
148 regs.rbx = context->Rbx;
149 regs.rcx = context->Rcx;
150 regs.rdx = context->Rdx;
151 regs.rsi = context->Rsi;
152 regs.rdi = context->Rdi;
153 regs.r8 = context->R8;
154 regs.r9 = context->R9;
155 regs.r10 = context->R10;
156 regs.r11 = context->R11;
157 regs.r12 = context->R12;
158 regs.r13 = context->R13;
159 regs.r14 = context->R14;
160 regs.r15 = context->R15;
162 if (flags & CONTEXT_CONTROL)
164 regs.rbp = context->Rbp;
165 regs.rip = context->Rip;
166 regs.rsp = context->Rsp;
167 regs.cs = context->SegCs;
168 regs.ss = context->SegSs;
169 regs.eflags = context->EFlags;
171 if (flags & CONTEXT_SEGMENTS)
173 regs.ds = context->SegDs;
174 regs.es = context->SegEs;
175 regs.fs = context->SegFs;
176 regs.gs = context->SegGs;
178 if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
180 if (flags & CONTEXT_DEBUG_REGISTERS)
182 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
183 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
184 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
185 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
186 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
187 if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
189 if (flags & CONTEXT_FLOATING_POINT)
191 /* we can use context->FloatSave directly as it is using the */
192 /* correct structure (the same as fsave/frstor) */
193 if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->u.FltSave ) == -1) goto error;
195 return;
196 error:
197 file_set_error();
200 #else /* linux */
201 #error You must implement get/set_thread_context_ptrace for your platform
202 #endif /* linux */
205 /* copy a context structure according to the flags */
206 static void copy_context( CONTEXT *to, const CONTEXT *from, unsigned int flags )
208 if (flags & CONTEXT_CONTROL)
210 to->Rbp = from->Rbp;
211 to->Rip = from->Rip;
212 to->Rsp = from->Rsp;
213 to->SegCs = from->SegCs;
214 to->SegSs = from->SegSs;
215 to->EFlags = from->EFlags;
216 to->MxCsr = from->MxCsr;
218 if (flags & CONTEXT_INTEGER)
220 to->Rax = from->Rax;
221 to->Rcx = from->Rcx;
222 to->Rdx = from->Rdx;
223 to->Rbx = from->Rbx;
224 to->Rsi = from->Rsi;
225 to->Rdi = from->Rdi;
226 to->R8 = from->R8;
227 to->R9 = from->R9;
228 to->R10 = from->R10;
229 to->R11 = from->R11;
230 to->R12 = from->R12;
231 to->R13 = from->R13;
232 to->R14 = from->R14;
233 to->R15 = from->R15;
235 if (flags & CONTEXT_SEGMENTS)
237 to->SegDs = from->SegDs;
238 to->SegEs = from->SegEs;
239 to->SegFs = from->SegFs;
240 to->SegGs = from->SegGs;
242 if (flags & CONTEXT_FLOATING_POINT)
244 to->u.FltSave = from->u.FltSave;
246 /* we don't bother copying the debug registers, since they */
247 /* always need to be accessed by ptrace anyway */
248 to->ContextFlags |= flags & ~CONTEXT_DEBUG_REGISTERS;
251 /* retrieve the current instruction pointer of a context */
252 void *get_context_ip( const CONTEXT *context )
254 return (void *)context->Rip;
257 /* retrieve the thread context */
258 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
260 context->ContextFlags |= CONTEXT_AMD64;
261 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
263 if (thread->context) /* thread is inside an exception event or suspended */
265 copy_context( context, thread->context, flags );
266 flags &= CONTEXT_DEBUG_REGISTERS;
269 if (flags && suspend_for_ptrace( thread ))
271 get_thread_context_ptrace( thread, flags, context );
272 resume_after_ptrace( thread );
276 /* set the thread context */
277 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
279 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
281 if (thread->context) /* thread is inside an exception event or suspended */
283 copy_context( thread->context, context, flags );
284 flags &= CONTEXT_DEBUG_REGISTERS;
287 if (flags && suspend_for_ptrace( thread ))
289 set_thread_context_ptrace( thread, flags, context );
290 resume_after_ptrace( thread );
294 #endif /* __x86_64__ */