mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-10 05:37:06 +00:00
It compiles, but aborts immediately if you try to run it.
I didn't include ldt.c; I'm not sure how the LDT is used on AMD64. It can be
added later if necessary.
While doing this, did some 64-bit cleanness fixes:
- Added necessary intermediate casts to ULong to avoid warnings when converting
ThreadId to void* and vice versa, in vg_scheduler.c.
- Fixed VALGRIND_NON_SIMD_CALL[0123] to use 'long' as the return type.
- Fixed VALGRIND_PRINTF{,BACKTRACE} to use unsigned longs instead of unsigned
ints, as needed.
- Converted some offsets in vg_symtab2.h from "Int" to "OffT".
- Made strlen, strncat, etc, use SizeT instead of 'unsigned int' for the length
parameter.
- Couple of other minor things.
I had to insert some "#ifdef __amd64__" and "#ifndef __amd64__" guards in
places. In particular, in vg_mylibc.c, some of our syscall wrappers aren't
appropriate for AMD64 because the syscall numbering is a bit different in
places. This difference will have to be abstracted out somehow.
Also rewrote the sys_fcntl and sys_fcntl64 wrappers, as required for AMD64.
Also moved the ipc wrapper into x86, since it's not applicable for
AMD64. However, it is applicable (I think) for ARM, so it would be nice
to work out a way to share syscall wrappers between some, but not all,
archs. Hmm. Also now using the real IPC constants rather than magic
numbers in the wrapper.
Other non-AMD64-related fixes:
- ARM: fixed syscall table by accounting for the fact that syscall
numbers don't start at 0, but rather at 0x900000.
- Converted a few places to use ThreadId instead of 'int' or 'Int' for
thread IDs.
- Added both AMD64 and ARM (which I'd forgotten) entries to valgrind.spec.in.
- Tweaked comments in various places.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3136
294 lines
9.0 KiB
C
294 lines
9.0 KiB
C
|
|
/*--------------------------------------------------------------------*/
|
|
/*--- Arch-specific registers, etc. amd64/state.c ---*/
|
|
/*--------------------------------------------------------------------*/
|
|
|
|
/*
|
|
This file is part of Valgrind, an extensible x86 protected-mode
|
|
emulator for monitoring program execution on x86-Unixes.
|
|
|
|
Copyright (C) 2000-2004 Nicholas Nethercote
|
|
njn25@cam.ac.uk
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License as
|
|
published by the Free Software Foundation; either version 2 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program 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
|
|
General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
02111-1307, USA.
|
|
|
|
The GNU General Public License is contained in the file COPYING.
|
|
*/
|
|
|
|
#include "core.h"
|
|
#include "amd64_private.h"
|
|
#include <sys/ptrace.h>
|
|
|
|
#include "libvex_guest_amd64.h"
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Initialising the first thread ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
/* Given a pointer to the ThreadArchState for thread 1 (the root
|
|
thread), initialise the VEX guest state, and copy in essential
|
|
starting values.
|
|
*/
|
|
void VGA_(init_thread1state) ( Addr client_eip,
|
|
Addr esp_at_startup,
|
|
/*MOD*/ ThreadArchState* arch )
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
vg_assert(0 == sizeof(VexGuestX86State) % 8);
|
|
|
|
/* Zero out the initial state, and set up the simulated FPU in a
|
|
sane way. */
|
|
LibVEX_GuestX86_initialise(&arch->vex);
|
|
|
|
/* Zero out the shadow area. */
|
|
VG_(memset)(&arch->vex_shadow, 0, sizeof(VexGuestX86State));
|
|
|
|
/* Put essential stuff into the new state. */
|
|
/* initialise %cs, %ds and %ss to point at the operating systems
|
|
default code, data and stack segments */
|
|
arch->vex.guest_ESP = esp_at_startup;
|
|
arch->vex.guest_EIP = client_eip;
|
|
|
|
asm volatile("movw %%cs, %0"
|
|
:
|
|
: "m" (arch->vex.guest_CS));
|
|
asm volatile("movw %%ds, %0"
|
|
:
|
|
: "m" (arch->vex.guest_DS));
|
|
asm volatile("movw %%ss, %0"
|
|
:
|
|
: "m" (arch->vex.guest_SS));
|
|
|
|
VG_TRACK( post_reg_write, Vg_CoreStartup, /*tid*/1, /*offset*/0,
|
|
sizeof(VexGuestArchState));
|
|
|
|
/* I assume that if we have SSE2 we also have SSE */
|
|
VG_(have_ssestate) = False;
|
|
// VG_(cpu_has_feature)(VG_X86_FEAT_FXSR) &&
|
|
// VG_(cpu_has_feature)(VG_X86_FEAT_SSE);
|
|
|
|
if (0) {
|
|
if (VG_(have_ssestate))
|
|
VG_(printf)("Looks like a SSE-capable CPU\n");
|
|
else
|
|
VG_(printf)("Looks like a MMX-only CPU\n");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Thread stuff ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
void VGA_(clear_thread)( ThreadArchState *arch )
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
arch->ldt = NULL;
|
|
VG_(clear_TLS_for_thread)(arch->tls);
|
|
#endif
|
|
}
|
|
|
|
void VGA_(cleanup_thread) ( ThreadArchState *arch )
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
/* Deallocate its LDT, if it ever had one. */
|
|
VG_(deallocate_LDT_for_thread)( arch->ldt );
|
|
arch->ldt = NULL;
|
|
|
|
/* Clear its TLS array. */
|
|
VG_(clear_TLS_for_thread)( arch->tls );
|
|
#endif
|
|
}
|
|
|
|
void VGA_(setup_child) ( ThreadArchState *arch, ThreadArchState *parent_arch )
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
/* We inherit our parent's LDT. */
|
|
if (parent_arch->ldt == NULL) {
|
|
/* We hope this is the common case. */
|
|
arch->ldt = NULL;
|
|
} else {
|
|
/* No luck .. we have to take a copy of the parent's. */
|
|
arch->ldt = VG_(allocate_LDT_for_thread)( parent_arch->ldt );
|
|
}
|
|
|
|
/* Initialise the thread's TLS array */
|
|
VG_(clear_TLS_for_thread)( arch->tls );
|
|
#endif
|
|
}
|
|
|
|
void VGA_(set_arg_and_bogus_ret)( ThreadId tid, UWord arg, Addr ret )
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
/* Push the arg, and mark it as readable. */
|
|
SET_PTHREQ_ESP(tid, VG_(threads)[tid].arch.vex.guest_ESP - sizeof(UWord));
|
|
* (UInt*)(VG_(threads)[tid].arch.vex.guest_ESP) = arg;
|
|
VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
|
|
VG_(threads)[tid].arch.vex.guest_ESP, sizeof(void*) );
|
|
|
|
/* Don't mark the pushed return address as readable; any attempt to read
|
|
this is an internal valgrind bug since thread_exit_wrapper() should not
|
|
return. */
|
|
SET_PTHREQ_ESP(tid, VG_(threads)[tid].arch.vex.guest_ESP - sizeof(UWord));
|
|
* (UInt*)(VG_(threads)[tid].arch.vex.guest_ESP) = ret;
|
|
#endif
|
|
}
|
|
|
|
void VGA_(thread_initial_stack)(ThreadId tid, UWord arg, Addr ret)
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
Addr esp = (Addr)STACK_PTR(VG_(threads)[tid].arch);
|
|
|
|
/* push two args */
|
|
esp -= 2 * sizeof(UWord);
|
|
SET_PTHREQ_ESP(tid, esp);
|
|
|
|
VG_TRACK ( new_mem_stack, esp, 2 * sizeof(UWord) );
|
|
VG_TRACK ( pre_mem_write, Vg_CorePThread, tid, "new thread: stack",
|
|
esp, 2 * sizeof(UWord) );
|
|
|
|
/* push arg and (bogus) return address */
|
|
*(UWord*)(esp+sizeof(UWord)) = arg;
|
|
*(UWord*)(esp) = ret;
|
|
|
|
VG_TRACK ( post_mem_write, Vg_CoreSignal, tid, esp, 2 * sizeof(UWord) );
|
|
#endif
|
|
}
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Symtab stuff ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
#if 0
|
|
/* This is the Intel register encoding -- integer regs. */
|
|
#define R_EAX 0
|
|
#define R_ECX 1
|
|
#define R_EDX 2
|
|
#define R_EBX 3
|
|
#define R_ESP 4
|
|
#define R_EBP 5
|
|
#define R_ESI 6
|
|
#define R_EDI 7
|
|
#define R_E8 8
|
|
#define R_E9 9
|
|
#define R_E10 10
|
|
#define R_E11 11
|
|
#define R_E12 12
|
|
#define R_E13 13
|
|
#define R_E14 14
|
|
#define R_E15 15
|
|
#endif
|
|
|
|
UInt *VGA_(reg_addr_from_tst)(Int regno, ThreadArchState *arch)
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
switch (regno) {
|
|
case R_RAX: return &arch->vex.guest_RAX;
|
|
case R_RCX: return &arch->vex.guest_RCX;
|
|
case R_RDX: return &arch->vex.guest_RDX;
|
|
case R_RBX: return &arch->vex.guest_RBX;
|
|
case R_RSP: return &arch->vex.guest_RSP;
|
|
case R_RBP: return &arch->vex.guest_RBP;
|
|
case R_RSI: return &arch->vex.guest_RSI;
|
|
case R_RDI: return &arch->vex.guest_RDI;
|
|
case R_R8 : return &arch->vex.guest_R8 ;
|
|
case R_R9 : return &arch->vex.guest_R9 ;
|
|
case R_R10: return &arch->vex.guest_R10;
|
|
case R_R11: return &arch->vex.guest_R11;
|
|
case R_R12: return &arch->vex.guest_R12;
|
|
case R_R13: return &arch->vex.guest_R13;
|
|
case R_R14: return &arch->vex.guest_R14;
|
|
case R_R15: return &arch->vex.guest_R15;
|
|
default: return NULL;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- pointercheck ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
Bool VGA_(setup_pointercheck)(void)
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
vki_modify_ldt_t ldt = {
|
|
VG_POINTERCHECK_SEGIDX, // entry_number
|
|
VG_(client_base), // base_addr
|
|
(VG_(client_end)-VG_(client_base)) / VKI_PAGE_SIZE, // limit
|
|
1, // seg_32bit
|
|
0, // contents: data, RW, non-expanding
|
|
0, // ! read_exec_only
|
|
1, // limit_in_pages
|
|
0, // ! seg not present
|
|
1, // useable
|
|
};
|
|
int ret = VG_(do_syscall)(__NR_modify_ldt, 1, &ldt, sizeof(ldt));
|
|
if (ret < 0) {
|
|
VG_(message)(Vg_UserMsg,
|
|
"Warning: ignoring --pointercheck=yes, "
|
|
"because modify_ldt failed (errno=%d)", -ret);
|
|
return False;
|
|
} else {
|
|
return True;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/*------------------------------------------------------------*/
|
|
/*--- Debugger-related operations ---*/
|
|
/*------------------------------------------------------------*/
|
|
|
|
Int VGA_(ptrace_setregs_from_tst)(Int pid, ThreadArchState* arch)
|
|
{
|
|
I_die_here;
|
|
#if 0
|
|
struct vki_user_regs_struct regs;
|
|
|
|
regs.cs = arch->vex.guest_CS;
|
|
regs.ss = arch->vex.guest_SS;
|
|
regs.ds = arch->vex.guest_DS;
|
|
regs.es = arch->vex.guest_ES;
|
|
regs.fs = arch->vex.guest_FS;
|
|
regs.gs = arch->vex.guest_GS;
|
|
regs.eax = arch->vex.guest_EAX;
|
|
regs.ebx = arch->vex.guest_EBX;
|
|
regs.ecx = arch->vex.guest_ECX;
|
|
regs.edx = arch->vex.guest_EDX;
|
|
regs.esi = arch->vex.guest_ESI;
|
|
regs.edi = arch->vex.guest_EDI;
|
|
regs.ebp = arch->vex.guest_EBP;
|
|
regs.esp = arch->vex.guest_ESP;
|
|
regs.eflags = LibVEX_GuestX86_get_eflags(&arch->vex);
|
|
regs.eip = arch->vex.guest_EIP;
|
|
|
|
return ptrace(PTRACE_SETREGS, pid, NULL, ®s);
|
|
#endif
|
|
}
|
|
|
|
/*--------------------------------------------------------------------*/
|
|
/*--- end ---*/
|
|
/*--------------------------------------------------------------------*/
|
|
|