context_i386.c 18.2 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 <stdarg.h>
31
#include <unistd.h>
32 33 34
#ifdef HAVE_SYS_PTRACE_H
# include <sys/ptrace.h>
#endif
35 36 37
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
38

39
#include "windef.h"
40 41

#include "file.h"
42 43 44 45
#include "thread.h"
#include "request.h"

#ifndef PTRACE_PEEKUSER
46 47 48 49 50
#  ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
#    define PTRACE_PEEKUSER PTRACE_PEEKUSR
#  else
#    define PTRACE_PEEKUSER PT_READ_U
#  endif
51
#endif
52

53
#ifndef PTRACE_POKEUSER
54 55 56 57 58
#  ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
#    define PTRACE_POKEUSER PTRACE_POKEUSR
#  else
#    define PTRACE_POKEUSER PT_WRITE_U
#  endif
59
#endif
60

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

74 75 76 77 78 79 80 81
#ifdef PT_GETDBREGS
#define PTRACE_GETDBREGS PT_GETDBREGS
#endif

#ifdef PT_SETDBREGS
#define PTRACE_SETDBREGS PT_SETDBREGS
#endif

82
#ifdef linux
83 84 85
#ifdef HAVE_SYS_USER_H
# include <sys/user.h>
#endif
86

87 88 89 90 91 92 93 94 95 96 97 98
/* 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;
};

99 100 101 102 103 104
/* 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 )
{
105 106 107
    int res;
    errno = 0;
    res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
108 109 110 111 112 113 114 115 116 117
    if ((res == -1) && errno)
    {
        file_set_error();
        return -1;
    }
    *data = res;
    return 0;
}

/* retrieve a thread context */
118
static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
119
{
120
    int pid = get_ptrace_pid(thread);
121 122
    if (flags & CONTEXT_FULL)
    {
123
        struct kernel_user_regs_struct regs;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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;
139 140
            context->SegCs  = regs.cs;
            context->SegSs  = regs.ss;
141 142 143 144
            context->EFlags = regs.eflags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
145 146 147 148
            context->SegDs = regs.ds;
            context->SegEs = regs.es;
            context->SegFs = regs.fs;
            context->SegGs = regs.gs;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        }
    }
    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 */
174
static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
175
{
176
    int pid = get_ptrace_pid(thread);
177 178
    if (flags & CONTEXT_FULL)
    {
179
        struct kernel_user_regs_struct regs;
180 181 182 183

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

184 185 186 187 188 189 190 191 192 193 194 195 196 197
        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;
198 199
            regs.cs  = context->SegCs;
            regs.ss  = context->SegSs;
200 201 202 203
            regs.eflags = context->EFlags;
        }
        if (flags & CONTEXT_SEGMENTS)
        {
204 205 206 207
            regs.ds = context->SegDs;
            regs.es = context->SegEs;
            regs.fs = context->SegFs;
            regs.gs = context->SegGs;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        }
        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();
}

231
#elif defined(__sun) || defined(__sun__)
232 233

/* retrieve a thread context */
234
static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
235
{
236
    int pid = get_ptrace_pid(thread);
237 238 239
    if (flags & CONTEXT_FULL)
    {
        struct regs regs;
240
        if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
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 272 273 274
        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) */
275
        if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
276 277 278 279 280 281 282 283 284
        context->FloatSave.Cr0NpxState = 0;  /* FIXME */
    }
    return;
 error:
    file_set_error();
}


/* set a thread context */
285
static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
286
{
287
    int pid = get_ptrace_pid(thread);
288 289 290
    if (flags & CONTEXT_FULL)
    {
        struct regs regs;
291
        if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
292
        {
293
            /* need to preserve some registers */
294
            if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
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
        }
        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;
        }
321
        if (ptrace( PTRACE_SETREGS, pid, (int) &regs, 0 ) == -1) goto error;
322 323 324 325 326 327 328 329 330
    }
    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) */
331
        if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
332 333 334 335 336 337
    }
    return;
 error:
    file_set_error();
}

338
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
339 340 341
#include <machine/reg.h>

/* retrieve a thread context */
342
static void get_thread_context_ptrace( struct thread *thread, unsigned int flags, CONTEXT *context )
343
{
344
    int pid = get_ptrace_pid(thread);
345 346 347
    if (flags & CONTEXT_FULL)
    {
        struct reg regs;
348
        if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
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
        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)
    {
377 378 379 380
#ifdef PTRACE_GETDBREGS
	struct dbreg dbregs;
        if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
		goto error;
381 382 383 384 385 386 387 388 389
#ifdef DBREG_DRX
	/* needed for FreeBSD, the structure fields have changed under 5.x */
	context->Dr0 = DBREG_DRX((&dbregs), 0);
	context->Dr1 = DBREG_DRX((&dbregs), 1);
	context->Dr2 = DBREG_DRX((&dbregs), 2);
	context->Dr3 = DBREG_DRX((&dbregs), 3);
	context->Dr6 = DBREG_DRX((&dbregs), 6);
	context->Dr7 = DBREG_DRX((&dbregs), 7);
#else
390 391 392 393 394 395
	context->Dr0 = dbregs.dr0;
	context->Dr1 = dbregs.dr1;
	context->Dr2 = dbregs.dr2;
	context->Dr3 = dbregs.dr3;
	context->Dr6 = dbregs.dr6;
	context->Dr7 = dbregs.dr7;
396 397
#endif

398
#endif
399 400 401 402 403
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
404
        if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
405 406 407 408 409 410 411 412 413
        context->FloatSave.Cr0NpxState = 0;  /* FIXME */
    }
    return;
 error:
    file_set_error();
}


/* set a thread context */
414
static void set_thread_context_ptrace( struct thread *thread, unsigned int flags, const CONTEXT *context )
415
{
416
    int pid = get_ptrace_pid(thread);
417 418 419
    if (flags & CONTEXT_FULL)
    {
        struct reg regs;
420
        if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
421
        {
422
            /* need to preserve some registers */
423
            if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
        }
        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;
        }
450
        if (ptrace( PTRACE_SETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
451 452 453
    }
    if (flags & CONTEXT_DEBUG_REGISTERS)
    {
454 455
#ifdef PTRACE_SETDBREGS
	struct dbreg dbregs;
456 457 458 459 460 461 462 463 464 465 466
#ifdef DBREG_DRX
	/* needed for FreeBSD, the structure fields have changed under 5.x */
	DBREG_DRX((&dbregs), 0) = context->Dr0;
	DBREG_DRX((&dbregs), 1) = context->Dr1;
	DBREG_DRX((&dbregs), 2) = context->Dr2;
	DBREG_DRX((&dbregs), 3) = context->Dr3;
	DBREG_DRX((&dbregs), 4) = 0;
	DBREG_DRX((&dbregs), 5) = 0;
	DBREG_DRX((&dbregs), 6) = context->Dr6;
	DBREG_DRX((&dbregs), 7) = context->Dr7;
#else
467 468 469 470 471 472 473 474
	dbregs.dr0 = context->Dr0;
	dbregs.dr1 = context->Dr1;
	dbregs.dr2 = context->Dr2;
	dbregs.dr3 = context->Dr3;
	dbregs.dr4 = 0;
	dbregs.dr5 = 0;
	dbregs.dr6 = context->Dr6;
	dbregs.dr7 = context->Dr7;
475
#endif
476 477 478
        if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
		goto error;
#endif
479 480 481 482 483
    }
    if (flags & CONTEXT_FLOATING_POINT)
    {
        /* we can use context->FloatSave directly as it is using the */
        /* correct structure (the same as fsave/frstor) */
484
        if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
485 486 487 488 489 490 491
    }
    return;
 error:
    file_set_error();
}

#else  /* linux || __sun__ || __FreeBSD__ */
492
#error You must implement get/set_thread_context_ptrace for your platform
493
#endif  /* linux || __sun__ || __FreeBSD__ */
494 495 496


/* copy a context structure according to the flags */
497
static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
{
    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;
    }
528 529
    /* we don't bother copying the debug registers, since they */
    /* always need to be accessed by ptrace anyway */
530 531
}

532 533 534 535 536 537 538
/* 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 ))
    {
539
        get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
540
        resume_after_ptrace( thread );
541 542 543 544
    }
    return (void *)context.Eip;
}

545 546 547 548 549
/* 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 */
550
    get_thread_context_ptrace( thread, CONTEXT_CONTROL, &context );
551 552 553
    return (context.EFlags & 0x100) != 0;
}

554 555 556 557 558 559 560 561 562 563 564
/* send a signal to a specific thread */
int tkill( int pid, int sig )
{
#ifdef __linux__
    int ret;
    __asm__( "pushl %%ebx\n\t"
             "movl %2,%%ebx\n\t"
             "int $0x80\n\t"
             "popl %%ebx\n\t"
             : "=a" (ret)
             : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
565
    if (ret >= 0) return ret;
566 567 568 569 570 571 572 573
    errno = -ret;
    return -1;
#else
    errno = ENOSYS;
    return -1;
#endif
}

574 575
/* retrieve the thread context */
void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
576
{
577
    flags &= ~CONTEXT_i386;  /* get rid of CPU id */
578

579
    if (thread->context)  /* thread is inside an exception event or suspended */
580
    {
581 582
        copy_context( context, thread->context, flags );
        flags &= CONTEXT_DEBUG_REGISTERS;
583
    }
584

585
    if (flags && suspend_for_ptrace( thread ))
586
    {
587 588
        get_thread_context_ptrace( thread, flags, context );
        resume_after_ptrace( thread );
589 590 591
    }
}

592 593
/* set the thread context */
void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
594
{
595
    flags &= ~CONTEXT_i386;  /* get rid of CPU id */
596

597
    if (thread->context)  /* thread is inside an exception event or suspended */
598
    {
599 600
        copy_context( thread->context, context, flags );
        flags &= CONTEXT_DEBUG_REGISTERS;
601
    }
602 603

    if (flags && suspend_for_ptrace( thread ))
604
    {
605 606
        set_thread_context_ptrace( thread, flags, context );
        resume_after_ptrace( thread );
607 608 609 610
    }
}

#endif  /* __i386__ */