context_i386.c 16.9 KB
Newer Older
1 2 3 4
/*
 * i386 register context support
 *
 * Copyright (C) 1999 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20 21 22 23 24 25 26
 */

#include "config.h"

#ifdef __i386__

#include <assert.h>
#include <errno.h>
27
#ifdef HAVE_SYS_REG_H
28
#include <sys/reg.h>
29
#endif
30
#include <unistd.h>
31 32 33
#ifdef HAVE_SYS_PTRACE_H
# include <sys/ptrace.h>
#endif
34 35 36
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
37 38 39 40 41 42

#include "winbase.h"
#include "thread.h"
#include "request.h"

#ifndef PTRACE_PEEKUSER
43 44 45 46 47
#  ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
#    define PTRACE_PEEKUSER PTRACE_PEEKUSR
#  else
#    define PTRACE_PEEKUSER PT_READ_U
#  endif
48
#endif
49

50
#ifndef PTRACE_POKEUSER
51 52 53 54 55
#  ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
#    define PTRACE_POKEUSER PTRACE_POKEUSR
#  else
#    define PTRACE_POKEUSER PT_WRITE_U
#  endif
56
#endif
57

58 59 60 61 62 63
#ifndef PTRACE_GETREGS
#define PTRACE_GETREGS PT_GETREGS
#endif
#ifndef PTRACE_GETFPREGS
#define PTRACE_GETFPREGS PT_GETFPREGS
#endif
64 65 66 67 68 69
#ifndef PTRACE_SETREGS
#define PTRACE_SETREGS PT_SETREGS
#endif
#ifndef PTRACE_SETFPREGS
#define PTRACE_SETFPREGS PT_SETFPREGS
#endif
70 71

#ifdef linux
72 73 74
#ifdef HAVE_SYS_USER_H
# include <sys/user.h>
#endif
75

76 77 78 79 80 81 82 83 84 85 86 87
/* user_regs definitions from asm/user.h */
struct kernel_user_regs_struct
{
    long ebx, ecx, edx, esi, edi, ebp, eax;
    unsigned short ds, __ds, es, __es;
    unsigned short fs, __fs, gs, __gs;
    long orig_eax, eip;
    unsigned short cs, __cs;
    long eflags, esp;
    unsigned short ss, __ss;
};

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* debug register offset in struct user */
#define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))

/* retrieve a debug register */
static inline int get_debug_reg( int pid, int num, DWORD *data )
{
    int res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
    if ((res == -1) && errno)
    {
        file_set_error();
        return -1;
    }
    *data = res;
    return 0;
}

/* retrieve a thread context */
static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
{
    int pid = thread->unix_pid;
    if (flags & CONTEXT_FULL)
    {
110
        struct kernel_user_regs_struct regs;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
        if (flags & CONTEXT_INTEGER)
        {
            context->Eax = regs.eax;
            context->Ebx = regs.ebx;
            context->Ecx = regs.ecx;
            context->Edx = regs.edx;
            context->Esi = regs.esi;
            context->Edi = regs.edi;
        }
        if (flags & CONTEXT_CONTROL)
        {
            context->Ebp    = regs.ebp;
            context->Esp    = regs.esp;
            context->Eip    = regs.eip;
126 127
            context->SegCs  = regs.cs;
            context->SegSs  = regs.ss;
128 129 130 131
            context->EFlags = regs.eflags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
132 133 134 135
            context->SegDs = regs.ds;
            context->SegEs = regs.es;
            context->SegFs = regs.fs;
            context->SegGs = regs.gs;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
        if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
        if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
        if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
        if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
        if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
        if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
        if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
        context->FloatSave.Cr0NpxState = 0;  /* FIXME */
    }
    return;
 error:
    file_set_error();
}


/* set a thread context */
161
static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
162 163 164 165
{
    int pid = thread->unix_pid;
    if (flags & CONTEXT_FULL)
    {
166
        struct kernel_user_regs_struct regs;
167 168 169 170

        /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
        if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;

171 172 173 174 175 176 177 178 179 180 181 182 183 184
        if (flags & CONTEXT_INTEGER)
        {
            regs.eax = context->Eax;
            regs.ebx = context->Ebx;
            regs.ecx = context->Ecx;
            regs.edx = context->Edx;
            regs.esi = context->Esi;
            regs.edi = context->Edi;
        }
        if (flags & CONTEXT_CONTROL)
        {
            regs.ebp = context->Ebp;
            regs.esp = context->Esp;
            regs.eip = context->Eip;
185 186
            regs.cs  = context->SegCs;
            regs.ss  = context->SegSs;
187 188 189 190
            regs.eflags = context->EFlags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
191 192 193 194
            regs.ds = context->SegDs;
            regs.es = context->SegEs;
            regs.fs = context->SegFs;
            regs.gs = context->SegGs;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        }
        if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
        if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
        if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
        if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
        if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
        if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
        if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
        if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
    }
    return;
 error:
    file_set_error();
}

218
#elif defined(__sun) || defined(__sun__)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

/* retrieve a thread context */
static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
{
    int pid = thread->unix_pid;
    if (flags & CONTEXT_FULL)
    {
        struct regs regs;
        if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
        if (flags & CONTEXT_INTEGER)
        {
            context->Eax = regs.r_eax;
            context->Ebx = regs.r_ebx;
            context->Ecx = regs.r_ecx;
            context->Edx = regs.r_edx;
            context->Esi = regs.r_esi;
            context->Edi = regs.r_edi;
        }
        if (flags & CONTEXT_CONTROL)
        {
            context->Ebp    = regs.r_ebp;
            context->Esp    = regs.r_esp;
            context->Eip    = regs.r_eip;
            context->SegCs  = regs.r_cs & 0xffff;
            context->SegSs  = regs.r_ss & 0xffff;
            context->EFlags = regs.r_efl;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
            context->SegDs = regs.r_ds & 0xffff;
            context->SegEs = regs.r_es & 0xffff;
            context->SegFs = regs.r_fs & 0xffff;
            context->SegGs = regs.r_gs & 0xffff;
        }
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
        /* FIXME: How is this done on Solaris? */
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
        if (ptrace( PTRACE_GETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
        context->FloatSave.Cr0NpxState = 0;  /* FIXME */
    }
    return;
 error:
    file_set_error();
}


/* set a thread context */
272
static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
273 274 275 276 277
{
    int pid = thread->unix_pid;
    if (flags & CONTEXT_FULL)
    {
        struct regs regs;
278
        if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
279
        {
280
            /* need to preserve some registers */
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
        }
        if (flags & CONTEXT_INTEGER)
        {
            regs.r_eax = context->Eax;
            regs.r_ebx = context->Ebx;
            regs.r_ecx = context->Ecx;
            regs.r_edx = context->Edx;
            regs.r_esi = context->Esi;
            regs.r_edi = context->Edi;
        }
        if (flags & CONTEXT_CONTROL)
        {
            regs.r_ebp = context->Ebp;
            regs.r_esp = context->Esp;
            regs.r_eip = context->Eip;
            regs.r_cs = context->SegCs;
            regs.r_ss = context->SegSs;
            regs.r_efl = context->EFlags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
            regs.r_ds = context->SegDs;
            regs.r_es = context->SegEs;
            regs.r_fs = context->SegFs;
            regs.r_gs = context->SegGs;
        }
        if (ptrace( PTRACE_SETREGS, pid, 0, (int) &regs ) == -1) goto error;
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
        /* FIXME: How is this done on Solaris? */
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
        if (ptrace( PTRACE_SETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
    }
    return;
 error:
    file_set_error();
}

325
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
#include <machine/reg.h>

/* retrieve a thread context */
static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
{
    int pid = thread->unix_pid;
    if (flags & CONTEXT_FULL)
    {
        struct reg regs;
        if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
        if (flags & CONTEXT_INTEGER)
        {
            context->Eax = regs.r_eax;
            context->Ebx = regs.r_ebx;
            context->Ecx = regs.r_ecx;
            context->Edx = regs.r_edx;
            context->Esi = regs.r_esi;
            context->Edi = regs.r_edi;
        }
        if (flags & CONTEXT_CONTROL)
        {
            context->Ebp    = regs.r_ebp;
            context->Esp    = regs.r_esp;
            context->Eip    = regs.r_eip;
            context->SegCs  = regs.r_cs & 0xffff;
            context->SegSs  = regs.r_ss & 0xffff;
            context->EFlags = regs.r_eflags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
            context->SegDs = regs.r_ds & 0xffff;
            context->SegEs = regs.r_es & 0xffff;
            context->SegFs = regs.r_fs & 0xffff;
            context->SegGs = regs.r_gs & 0xffff;
        }
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
        /* FIXME: How is this done on FreeBSD? */
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
        if (ptrace( PTRACE_GETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
        context->FloatSave.Cr0NpxState = 0;  /* FIXME */
    }
    return;
 error:
    file_set_error();
}


/* set a thread context */
380
static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
381 382 383 384 385
{
    int pid = thread->unix_pid;
    if (flags & CONTEXT_FULL)
    {
        struct reg regs;
386
        if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
387
        {
388
            /* need to preserve some registers */
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
            if (ptrace( PTRACE_GETREGS, pid, 0, (int) &regs ) == -1) goto error;
        }
        if (flags & CONTEXT_INTEGER)
        {
            regs.r_eax = context->Eax;
            regs.r_ebx = context->Ebx;
            regs.r_ecx = context->Ecx;
            regs.r_edx = context->Edx;
            regs.r_esi = context->Esi;
            regs.r_edi = context->Edi;
        }
        if (flags & CONTEXT_CONTROL)
        {
            regs.r_ebp = context->Ebp;
            regs.r_esp = context->Esp;
            regs.r_eip = context->Eip;
            regs.r_cs = context->SegCs;
            regs.r_ss = context->SegSs;
            regs.r_eflags = context->EFlags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
            regs.r_ds = context->SegDs;
            regs.r_es = context->SegEs;
            regs.r_fs = context->SegFs;
            regs.r_gs = context->SegGs;
        }
        if (ptrace( PTRACE_SETREGS, pid, 0, (int) &regs ) == -1) goto error;
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
        /* FIXME: How is this done on FreeBSD? */
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
        if (ptrace( PTRACE_SETFPREGS, pid, 0, (int) &context->FloatSave ) == -1) goto error;
    }
    return;
 error:
    file_set_error();
}

#else  /* linux || __sun__ || __FreeBSD__ */
434
#error You must implement get/set_thread_context for your platform
435
#endif  /* linux || __sun__ || __FreeBSD__ */
436 437 438


/* copy a context structure according to the flags */
439
static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
{
    if (flags & CONTEXT_CONTROL)
    {
        to->Ebp    = from->Ebp;
        to->Eip    = from->Eip;
        to->Esp    = from->Esp;
        to->SegCs  = from->SegCs;
        to->SegSs  = from->SegSs;
        to->EFlags = from->EFlags;
    }
    if (flags & CONTEXT_INTEGER)
    {
        to->Eax = from->Eax;
        to->Ebx = from->Ebx;
        to->Ecx = from->Ecx;
        to->Edx = from->Edx;
        to->Esi = from->Esi;
        to->Edi = from->Edi;
    }
    if (flags & CONTEXT_SEGMENTS)
    {
        to->SegDs = from->SegDs;
        to->SegEs = from->SegEs;
        to->SegFs = from->SegFs;
        to->SegGs = from->SegGs;
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        to->FloatSave = from->FloatSave;
    }
470 471
    /* we don't bother copying the debug registers, since they */
    /* always need to be accessed by ptrace anyway */
472 473
}

474 475 476 477 478 479 480 481 482 483 484 485 486
/* retrieve the current instruction pointer of a thread */
void *get_thread_ip( struct thread *thread )
{
    CONTEXT context;
    context.Eip = 0;
    if (suspend_for_ptrace( thread ))
    {
        get_thread_context( thread, CONTEXT_CONTROL, &context );
        resume_thread( thread );
    }
    return (void *)context.Eip;
}

487 488 489 490 491 492 493 494 495
/* determine if we should continue the thread in single-step mode */
int get_thread_single_step( struct thread *thread )
{
    CONTEXT context;
    if (thread->context) return 0;  /* don't single-step inside exception event */
    get_thread_context( thread, CONTEXT_CONTROL, &context );
    return (context.EFlags & 0x100) != 0;
}

496 497 498 499
/* retrieve the current context of a thread */
DECL_HANDLER(get_thread_context)
{
    struct thread *thread;
500
    void *data;
501
    int flags = req->flags & ~CONTEXT_i386;  /* get rid of CPU id */
502

503
    if (get_reply_max_size() < sizeof(CONTEXT))
504 505 506 507
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
508 509 510
    if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;

    if ((data = set_reply_data_size( sizeof(CONTEXT) )))
511
    {
512 513 514 515
        /* copy incoming context into reply */
        memset( data, 0, sizeof(CONTEXT) );
        memcpy( data, get_req_data(), min( get_req_data_size(), sizeof(CONTEXT) ));

516
        if (thread->context)  /* thread is inside an exception event */
517
        {
518
            copy_context( data, thread->context, flags );
519
            flags &= CONTEXT_DEBUG_REGISTERS;
520
        }
521
        if (flags && suspend_for_ptrace( thread ))
522
        {
523
            get_thread_context( thread, flags, data );
524 525 526
            resume_thread( thread );
        }
    }
527
    release_object( thread );
528 529 530 531 532 533 534
}


/* set the current context of a thread */
DECL_HANDLER(set_thread_context)
{
    struct thread *thread;
535
    int flags = req->flags & ~CONTEXT_i386;  /* get rid of CPU id */
536

537
    if (get_req_data_size() < sizeof(CONTEXT))
538 539 540 541
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }
542 543
    if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
    {
544
        if (thread->context)  /* thread is inside an exception event */
545
        {
546
            copy_context( thread->context, get_req_data(), flags );
547
            flags &= CONTEXT_DEBUG_REGISTERS;
548
        }
549
        if (flags && suspend_for_ptrace( thread ))
550
        {
551
            set_thread_context( thread, flags, get_req_data() );
552 553 554 555 556 557 558
            resume_thread( thread );
        }
        release_object( thread );
    }
}

#endif  /* __i386__ */