Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * i386 register context support |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #ifdef __i386__ |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <errno.h> |
Ulrich Weigand | 8a1bdb3 | 2000-01-30 22:22:22 +0000 | [diff] [blame] | 27 | #ifdef HAVE_SYS_REG_H |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 28 | #include <sys/reg.h> |
Ulrich Weigand | 8a1bdb3 | 2000-01-30 22:22:22 +0000 | [diff] [blame] | 29 | #endif |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 30 | #include <unistd.h> |
Dimitrie O. Paun | 2af03e4 | 2000-11-29 20:04:09 +0000 | [diff] [blame] | 31 | #ifdef HAVE_SYS_PTRACE_H |
| 32 | # include <sys/ptrace.h> |
| 33 | #endif |
Patrik Stridvall | 49bcb93 | 2000-02-25 20:55:33 +0000 | [diff] [blame] | 34 | #ifdef HAVE_SYS_PARAM_H |
| 35 | # include <sys/param.h> |
| 36 | #endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 37 | |
| 38 | #include "winbase.h" |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 39 | #include "thread.h" |
| 40 | #include "request.h" |
| 41 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 42 | #ifndef PTRACE_PEEKUSER |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 43 | # ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */ |
| 44 | # define PTRACE_PEEKUSER PTRACE_PEEKUSR |
| 45 | # else |
| 46 | # define PTRACE_PEEKUSER PT_READ_U |
| 47 | # endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 48 | #endif |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 49 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 50 | #ifndef PTRACE_POKEUSER |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 51 | # ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */ |
| 52 | # define PTRACE_POKEUSER PTRACE_POKEUSR |
| 53 | # else |
| 54 | # define PTRACE_POKEUSER PT_WRITE_U |
| 55 | # endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 56 | #endif |
Peter Hunnisett | de1ff66 | 2000-03-24 19:45:11 +0000 | [diff] [blame] | 57 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 58 | #ifndef PTRACE_GETREGS |
| 59 | #define PTRACE_GETREGS PT_GETREGS |
| 60 | #endif |
| 61 | #ifndef PTRACE_GETFPREGS |
| 62 | #define PTRACE_GETFPREGS PT_GETFPREGS |
| 63 | #endif |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 64 | #ifndef PTRACE_SETREGS |
| 65 | #define PTRACE_SETREGS PT_SETREGS |
| 66 | #endif |
| 67 | #ifndef PTRACE_SETFPREGS |
| 68 | #define PTRACE_SETFPREGS PT_SETFPREGS |
| 69 | #endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 70 | |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 71 | #ifdef PT_GETDBREGS |
| 72 | #define PTRACE_GETDBREGS PT_GETDBREGS |
| 73 | #endif |
| 74 | |
| 75 | #ifdef PT_SETDBREGS |
| 76 | #define PTRACE_SETDBREGS PT_SETDBREGS |
| 77 | #endif |
| 78 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 79 | #ifdef linux |
Francois Gouget | 5c84fff | 2001-11-20 18:52:42 +0000 | [diff] [blame] | 80 | #ifdef HAVE_SYS_USER_H |
| 81 | # include <sys/user.h> |
| 82 | #endif |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 83 | |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 84 | /* user_regs definitions from asm/user.h */ |
| 85 | struct kernel_user_regs_struct |
| 86 | { |
| 87 | long ebx, ecx, edx, esi, edi, ebp, eax; |
| 88 | unsigned short ds, __ds, es, __es; |
| 89 | unsigned short fs, __fs, gs, __gs; |
| 90 | long orig_eax, eip; |
| 91 | unsigned short cs, __cs; |
| 92 | long eflags, esp; |
| 93 | unsigned short ss, __ss; |
| 94 | }; |
| 95 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 96 | /* debug register offset in struct user */ |
| 97 | #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr))) |
| 98 | |
| 99 | /* retrieve a debug register */ |
| 100 | static inline int get_debug_reg( int pid, int num, DWORD *data ) |
| 101 | { |
| 102 | int res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 ); |
| 103 | if ((res == -1) && errno) |
| 104 | { |
| 105 | file_set_error(); |
| 106 | return -1; |
| 107 | } |
| 108 | *data = res; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* retrieve a thread context */ |
| 113 | static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context ) |
| 114 | { |
| 115 | int pid = thread->unix_pid; |
| 116 | if (flags & CONTEXT_FULL) |
| 117 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 118 | struct kernel_user_regs_struct regs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 119 | if (ptrace( PTRACE_GETREGS, pid, 0, ®s ) == -1) goto error; |
| 120 | if (flags & CONTEXT_INTEGER) |
| 121 | { |
| 122 | context->Eax = regs.eax; |
| 123 | context->Ebx = regs.ebx; |
| 124 | context->Ecx = regs.ecx; |
| 125 | context->Edx = regs.edx; |
| 126 | context->Esi = regs.esi; |
| 127 | context->Edi = regs.edi; |
| 128 | } |
| 129 | if (flags & CONTEXT_CONTROL) |
| 130 | { |
| 131 | context->Ebp = regs.ebp; |
| 132 | context->Esp = regs.esp; |
| 133 | context->Eip = regs.eip; |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 134 | context->SegCs = regs.cs; |
| 135 | context->SegSs = regs.ss; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 136 | context->EFlags = regs.eflags; |
| 137 | } |
| 138 | if (flags & CONTEXT_SEGMENTS) |
| 139 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 140 | context->SegDs = regs.ds; |
| 141 | context->SegEs = regs.es; |
| 142 | context->SegFs = regs.fs; |
| 143 | context->SegGs = regs.gs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 147 | { |
| 148 | if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error; |
| 149 | if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error; |
| 150 | if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error; |
| 151 | if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error; |
| 152 | if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error; |
| 153 | if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error; |
| 154 | } |
| 155 | if (flags & CONTEXT_FLOATING_POINT) |
| 156 | { |
| 157 | /* we can use context->FloatSave directly as it is using the */ |
| 158 | /* correct structure (the same as fsave/frstor) */ |
| 159 | if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error; |
| 160 | context->FloatSave.Cr0NpxState = 0; /* FIXME */ |
| 161 | } |
| 162 | return; |
| 163 | error: |
| 164 | file_set_error(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /* set a thread context */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 169 | static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 170 | { |
| 171 | int pid = thread->unix_pid; |
| 172 | if (flags & CONTEXT_FULL) |
| 173 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 174 | struct kernel_user_regs_struct regs; |
Alexandre Julliard | 7614180 | 2001-07-11 17:30:59 +0000 | [diff] [blame] | 175 | |
| 176 | /* need to preserve some registers (at a minimum orig_eax must always be preserved) */ |
| 177 | if (ptrace( PTRACE_GETREGS, pid, 0, ®s ) == -1) goto error; |
| 178 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 179 | if (flags & CONTEXT_INTEGER) |
| 180 | { |
| 181 | regs.eax = context->Eax; |
| 182 | regs.ebx = context->Ebx; |
| 183 | regs.ecx = context->Ecx; |
| 184 | regs.edx = context->Edx; |
| 185 | regs.esi = context->Esi; |
| 186 | regs.edi = context->Edi; |
| 187 | } |
| 188 | if (flags & CONTEXT_CONTROL) |
| 189 | { |
| 190 | regs.ebp = context->Ebp; |
| 191 | regs.esp = context->Esp; |
| 192 | regs.eip = context->Eip; |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 193 | regs.cs = context->SegCs; |
| 194 | regs.ss = context->SegSs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 195 | regs.eflags = context->EFlags; |
| 196 | } |
| 197 | if (flags & CONTEXT_SEGMENTS) |
| 198 | { |
Alexandre Julliard | a661187 | 2000-01-29 23:35:42 +0000 | [diff] [blame] | 199 | regs.ds = context->SegDs; |
| 200 | regs.es = context->SegEs; |
| 201 | regs.fs = context->SegFs; |
| 202 | regs.gs = context->SegGs; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 203 | } |
| 204 | if (ptrace( PTRACE_SETREGS, pid, 0, ®s ) == -1) goto error; |
| 205 | } |
| 206 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 207 | { |
| 208 | if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error; |
| 209 | if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error; |
| 210 | if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error; |
| 211 | if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error; |
| 212 | if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error; |
| 213 | if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error; |
| 214 | } |
| 215 | if (flags & CONTEXT_FLOATING_POINT) |
| 216 | { |
| 217 | /* we can use context->FloatSave directly as it is using the */ |
| 218 | /* correct structure (the same as fsave/frstor) */ |
| 219 | if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 220 | } |
| 221 | return; |
| 222 | error: |
| 223 | file_set_error(); |
| 224 | } |
| 225 | |
Patrik Stridvall | 0e06d62 | 2000-02-26 13:16:13 +0000 | [diff] [blame] | 226 | #elif defined(__sun) || defined(__sun__) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 227 | |
| 228 | /* retrieve a thread context */ |
| 229 | static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context ) |
| 230 | { |
| 231 | int pid = thread->unix_pid; |
| 232 | if (flags & CONTEXT_FULL) |
| 233 | { |
| 234 | struct regs regs; |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 235 | if (ptrace( PTRACE_GETREGS, pid, (int) ®s, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 236 | if (flags & CONTEXT_INTEGER) |
| 237 | { |
| 238 | context->Eax = regs.r_eax; |
| 239 | context->Ebx = regs.r_ebx; |
| 240 | context->Ecx = regs.r_ecx; |
| 241 | context->Edx = regs.r_edx; |
| 242 | context->Esi = regs.r_esi; |
| 243 | context->Edi = regs.r_edi; |
| 244 | } |
| 245 | if (flags & CONTEXT_CONTROL) |
| 246 | { |
| 247 | context->Ebp = regs.r_ebp; |
| 248 | context->Esp = regs.r_esp; |
| 249 | context->Eip = regs.r_eip; |
| 250 | context->SegCs = regs.r_cs & 0xffff; |
| 251 | context->SegSs = regs.r_ss & 0xffff; |
| 252 | context->EFlags = regs.r_efl; |
| 253 | } |
| 254 | if (flags & CONTEXT_SEGMENTS) |
| 255 | { |
| 256 | context->SegDs = regs.r_ds & 0xffff; |
| 257 | context->SegEs = regs.r_es & 0xffff; |
| 258 | context->SegFs = regs.r_fs & 0xffff; |
| 259 | context->SegGs = regs.r_gs & 0xffff; |
| 260 | } |
| 261 | } |
| 262 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 263 | { |
| 264 | /* FIXME: How is this done on Solaris? */ |
| 265 | } |
| 266 | if (flags & CONTEXT_FLOATING_POINT) |
| 267 | { |
| 268 | /* we can use context->FloatSave directly as it is using the */ |
| 269 | /* correct structure (the same as fsave/frstor) */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 270 | if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 271 | context->FloatSave.Cr0NpxState = 0; /* FIXME */ |
| 272 | } |
| 273 | return; |
| 274 | error: |
| 275 | file_set_error(); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /* set a thread context */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 280 | static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context ) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 281 | { |
| 282 | int pid = thread->unix_pid; |
| 283 | if (flags & CONTEXT_FULL) |
| 284 | { |
| 285 | struct regs regs; |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 286 | if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL) |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 287 | { |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 288 | /* need to preserve some registers */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 289 | if (ptrace( PTRACE_GETREGS, pid, (int) ®s, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 290 | } |
| 291 | if (flags & CONTEXT_INTEGER) |
| 292 | { |
| 293 | regs.r_eax = context->Eax; |
| 294 | regs.r_ebx = context->Ebx; |
| 295 | regs.r_ecx = context->Ecx; |
| 296 | regs.r_edx = context->Edx; |
| 297 | regs.r_esi = context->Esi; |
| 298 | regs.r_edi = context->Edi; |
| 299 | } |
| 300 | if (flags & CONTEXT_CONTROL) |
| 301 | { |
| 302 | regs.r_ebp = context->Ebp; |
| 303 | regs.r_esp = context->Esp; |
| 304 | regs.r_eip = context->Eip; |
| 305 | regs.r_cs = context->SegCs; |
| 306 | regs.r_ss = context->SegSs; |
| 307 | regs.r_efl = context->EFlags; |
| 308 | } |
| 309 | if (flags & CONTEXT_SEGMENTS) |
| 310 | { |
| 311 | regs.r_ds = context->SegDs; |
| 312 | regs.r_es = context->SegEs; |
| 313 | regs.r_fs = context->SegFs; |
| 314 | regs.r_gs = context->SegGs; |
| 315 | } |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 316 | if (ptrace( PTRACE_SETREGS, pid, (int) ®s, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 317 | } |
| 318 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 319 | { |
| 320 | /* FIXME: How is this done on Solaris? */ |
| 321 | } |
| 322 | if (flags & CONTEXT_FLOATING_POINT) |
| 323 | { |
| 324 | /* we can use context->FloatSave directly as it is using the */ |
| 325 | /* correct structure (the same as fsave/frstor) */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 326 | if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error; |
Patrik Stridvall | de20c2a | 2000-01-30 00:23:12 +0000 | [diff] [blame] | 327 | } |
| 328 | return; |
| 329 | error: |
| 330 | file_set_error(); |
| 331 | } |
| 332 | |
Bang Jun-Young | 18bf392 | 2001-05-09 19:44:47 +0000 | [diff] [blame] | 333 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 334 | #include <machine/reg.h> |
| 335 | |
| 336 | /* retrieve a thread context */ |
| 337 | static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context ) |
| 338 | { |
| 339 | int pid = thread->unix_pid; |
| 340 | if (flags & CONTEXT_FULL) |
| 341 | { |
| 342 | struct reg regs; |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 343 | if (ptrace( PTRACE_GETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 344 | if (flags & CONTEXT_INTEGER) |
| 345 | { |
| 346 | context->Eax = regs.r_eax; |
| 347 | context->Ebx = regs.r_ebx; |
| 348 | context->Ecx = regs.r_ecx; |
| 349 | context->Edx = regs.r_edx; |
| 350 | context->Esi = regs.r_esi; |
| 351 | context->Edi = regs.r_edi; |
| 352 | } |
| 353 | if (flags & CONTEXT_CONTROL) |
| 354 | { |
| 355 | context->Ebp = regs.r_ebp; |
| 356 | context->Esp = regs.r_esp; |
| 357 | context->Eip = regs.r_eip; |
| 358 | context->SegCs = regs.r_cs & 0xffff; |
| 359 | context->SegSs = regs.r_ss & 0xffff; |
| 360 | context->EFlags = regs.r_eflags; |
| 361 | } |
| 362 | if (flags & CONTEXT_SEGMENTS) |
| 363 | { |
| 364 | context->SegDs = regs.r_ds & 0xffff; |
| 365 | context->SegEs = regs.r_es & 0xffff; |
| 366 | context->SegFs = regs.r_fs & 0xffff; |
| 367 | context->SegGs = regs.r_gs & 0xffff; |
| 368 | } |
| 369 | } |
| 370 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 371 | { |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 372 | #ifdef PTRACE_GETDBREGS |
| 373 | struct dbreg dbregs; |
| 374 | if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) |
| 375 | goto error; |
| 376 | context->Dr0 = dbregs.dr0; |
| 377 | context->Dr1 = dbregs.dr1; |
| 378 | context->Dr2 = dbregs.dr2; |
| 379 | context->Dr3 = dbregs.dr3; |
| 380 | context->Dr6 = dbregs.dr6; |
| 381 | context->Dr7 = dbregs.dr7; |
| 382 | #endif |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 383 | } |
| 384 | if (flags & CONTEXT_FLOATING_POINT) |
| 385 | { |
| 386 | /* we can use context->FloatSave directly as it is using the */ |
| 387 | /* correct structure (the same as fsave/frstor) */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 388 | if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 389 | context->FloatSave.Cr0NpxState = 0; /* FIXME */ |
| 390 | } |
| 391 | return; |
| 392 | error: |
| 393 | file_set_error(); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* set a thread context */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 398 | static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context ) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 399 | { |
| 400 | int pid = thread->unix_pid; |
| 401 | if (flags & CONTEXT_FULL) |
| 402 | { |
| 403 | struct reg regs; |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 404 | if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL) |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 405 | { |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 406 | /* need to preserve some registers */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 407 | if (ptrace( PTRACE_GETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 408 | } |
| 409 | if (flags & CONTEXT_INTEGER) |
| 410 | { |
| 411 | regs.r_eax = context->Eax; |
| 412 | regs.r_ebx = context->Ebx; |
| 413 | regs.r_ecx = context->Ecx; |
| 414 | regs.r_edx = context->Edx; |
| 415 | regs.r_esi = context->Esi; |
| 416 | regs.r_edi = context->Edi; |
| 417 | } |
| 418 | if (flags & CONTEXT_CONTROL) |
| 419 | { |
| 420 | regs.r_ebp = context->Ebp; |
| 421 | regs.r_esp = context->Esp; |
| 422 | regs.r_eip = context->Eip; |
| 423 | regs.r_cs = context->SegCs; |
| 424 | regs.r_ss = context->SegSs; |
| 425 | regs.r_eflags = context->EFlags; |
| 426 | } |
| 427 | if (flags & CONTEXT_SEGMENTS) |
| 428 | { |
| 429 | regs.r_ds = context->SegDs; |
| 430 | regs.r_es = context->SegEs; |
| 431 | regs.r_fs = context->SegFs; |
| 432 | regs.r_gs = context->SegGs; |
| 433 | } |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 434 | if (ptrace( PTRACE_SETREGS, pid, (caddr_t) ®s, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 435 | } |
| 436 | if (flags & CONTEXT_DEBUG_REGISTERS) |
| 437 | { |
Pierre Beyssac | 3528755 | 2002-08-14 20:59:03 +0000 | [diff] [blame] | 438 | #ifdef PTRACE_SETDBREGS |
| 439 | struct dbreg dbregs; |
| 440 | dbregs.dr0 = context->Dr0; |
| 441 | dbregs.dr1 = context->Dr1; |
| 442 | dbregs.dr2 = context->Dr2; |
| 443 | dbregs.dr3 = context->Dr3; |
| 444 | dbregs.dr4 = 0; |
| 445 | dbregs.dr5 = 0; |
| 446 | dbregs.dr6 = context->Dr6; |
| 447 | dbregs.dr7 = context->Dr7; |
| 448 | if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1) |
| 449 | goto error; |
| 450 | #endif |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 451 | } |
| 452 | if (flags & CONTEXT_FLOATING_POINT) |
| 453 | { |
| 454 | /* we can use context->FloatSave directly as it is using the */ |
| 455 | /* correct structure (the same as fsave/frstor) */ |
Pierre Beyssac | bd15bef | 2002-08-13 03:18:14 +0000 | [diff] [blame] | 456 | if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error; |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 457 | } |
| 458 | return; |
| 459 | error: |
| 460 | file_set_error(); |
| 461 | } |
| 462 | |
| 463 | #else /* linux || __sun__ || __FreeBSD__ */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 464 | #error You must implement get/set_thread_context for your platform |
Juergen Lock | 5782765 | 2000-02-07 16:03:08 +0000 | [diff] [blame] | 465 | #endif /* linux || __sun__ || __FreeBSD__ */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 466 | |
| 467 | |
| 468 | /* copy a context structure according to the flags */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 469 | static void copy_context( CONTEXT *to, const CONTEXT *from, int flags ) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 470 | { |
| 471 | if (flags & CONTEXT_CONTROL) |
| 472 | { |
| 473 | to->Ebp = from->Ebp; |
| 474 | to->Eip = from->Eip; |
| 475 | to->Esp = from->Esp; |
| 476 | to->SegCs = from->SegCs; |
| 477 | to->SegSs = from->SegSs; |
| 478 | to->EFlags = from->EFlags; |
| 479 | } |
| 480 | if (flags & CONTEXT_INTEGER) |
| 481 | { |
| 482 | to->Eax = from->Eax; |
| 483 | to->Ebx = from->Ebx; |
| 484 | to->Ecx = from->Ecx; |
| 485 | to->Edx = from->Edx; |
| 486 | to->Esi = from->Esi; |
| 487 | to->Edi = from->Edi; |
| 488 | } |
| 489 | if (flags & CONTEXT_SEGMENTS) |
| 490 | { |
| 491 | to->SegDs = from->SegDs; |
| 492 | to->SegEs = from->SegEs; |
| 493 | to->SegFs = from->SegFs; |
| 494 | to->SegGs = from->SegGs; |
| 495 | } |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 496 | if (flags & CONTEXT_FLOATING_POINT) |
| 497 | { |
| 498 | to->FloatSave = from->FloatSave; |
| 499 | } |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 500 | /* we don't bother copying the debug registers, since they */ |
| 501 | /* always need to be accessed by ptrace anyway */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Alexandre Julliard | b73421d | 2000-03-30 19:30:24 +0000 | [diff] [blame] | 504 | /* retrieve the current instruction pointer of a thread */ |
| 505 | void *get_thread_ip( struct thread *thread ) |
| 506 | { |
| 507 | CONTEXT context; |
| 508 | context.Eip = 0; |
| 509 | if (suspend_for_ptrace( thread )) |
| 510 | { |
| 511 | get_thread_context( thread, CONTEXT_CONTROL, &context ); |
| 512 | resume_thread( thread ); |
| 513 | } |
| 514 | return (void *)context.Eip; |
| 515 | } |
| 516 | |
Ove Kaaven | ada7383 | 2001-04-27 18:39:47 +0000 | [diff] [blame] | 517 | /* determine if we should continue the thread in single-step mode */ |
| 518 | int get_thread_single_step( struct thread *thread ) |
| 519 | { |
| 520 | CONTEXT context; |
| 521 | if (thread->context) return 0; /* don't single-step inside exception event */ |
| 522 | get_thread_context( thread, CONTEXT_CONTROL, &context ); |
| 523 | return (context.EFlags & 0x100) != 0; |
| 524 | } |
| 525 | |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 526 | /* retrieve the current context of a thread */ |
| 527 | DECL_HANDLER(get_thread_context) |
| 528 | { |
| 529 | struct thread *thread; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 530 | void *data; |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 531 | int flags = req->flags & ~CONTEXT_i386; /* get rid of CPU id */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 532 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 533 | if (get_reply_max_size() < sizeof(CONTEXT)) |
Alexandre Julliard | 9264300 | 2000-08-31 01:59:51 +0000 | [diff] [blame] | 534 | { |
| 535 | set_error( STATUS_INVALID_PARAMETER ); |
| 536 | return; |
| 537 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 538 | if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return; |
| 539 | |
| 540 | if ((data = set_reply_data_size( sizeof(CONTEXT) ))) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 541 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 542 | /* copy incoming context into reply */ |
| 543 | memset( data, 0, sizeof(CONTEXT) ); |
| 544 | memcpy( data, get_req_data(), min( get_req_data_size(), sizeof(CONTEXT) )); |
| 545 | |
Alexandre Julliard | ea0d028 | 2000-03-10 22:16:10 +0000 | [diff] [blame] | 546 | if (thread->context) /* thread is inside an exception event */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 547 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 548 | copy_context( data, thread->context, flags ); |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 549 | flags &= CONTEXT_DEBUG_REGISTERS; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 550 | } |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 551 | if (flags && suspend_for_ptrace( thread )) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 552 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 553 | get_thread_context( thread, flags, data ); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 554 | resume_thread( thread ); |
| 555 | } |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 556 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 557 | release_object( thread ); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | /* set the current context of a thread */ |
| 562 | DECL_HANDLER(set_thread_context) |
| 563 | { |
| 564 | struct thread *thread; |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 565 | int flags = req->flags & ~CONTEXT_i386; /* get rid of CPU id */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 566 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 567 | if (get_req_data_size() < sizeof(CONTEXT)) |
Alexandre Julliard | 9264300 | 2000-08-31 01:59:51 +0000 | [diff] [blame] | 568 | { |
| 569 | set_error( STATUS_INVALID_PARAMETER ); |
| 570 | return; |
| 571 | } |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 572 | if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) |
| 573 | { |
Alexandre Julliard | ea0d028 | 2000-03-10 22:16:10 +0000 | [diff] [blame] | 574 | if (thread->context) /* thread is inside an exception event */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 575 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 576 | copy_context( thread->context, get_req_data(), flags ); |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 577 | flags &= CONTEXT_DEBUG_REGISTERS; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 578 | } |
Alexandre Julliard | cd8cf5e | 2000-03-15 19:47:44 +0000 | [diff] [blame] | 579 | if (flags && suspend_for_ptrace( thread )) |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 580 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 581 | set_thread_context( thread, flags, get_req_data() ); |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 582 | resume_thread( thread ); |
| 583 | } |
| 584 | release_object( thread ); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | #endif /* __i386__ */ |