From: Nicholas Nethercote Date: Sat, 18 Jun 2005 15:54:25 +0000 (+0000) Subject: Moved call_on_new_stack_0_1 and jump_and_switch_stacks to better places. X-Git-Tag: svn/VALGRIND_3_0_0~380 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6404b199cb5abeb112619508a59a1abe398a5ea8;p=thirdparty%2Fvalgrind.git Moved call_on_new_stack_0_1 and jump_and_switch_stacks to better places. This enabled the removal of $ARCH/jmp_with_stack.c, hurrah! git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3938 --- diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am index 4106101116..be6ea6799f 100644 --- a/coregrind/Makefile.am +++ b/coregrind/Makefile.am @@ -87,8 +87,7 @@ valgrind_SOURCES = \ ume.c \ \ stage1.c \ - m_debuglog.c \ - ${VG_ARCH}/jmp_with_stack.c + m_debuglog.c valgrind_DEPENDENCIES = valgrind_LDFLAGS=-static -g valgrind_LDADD= diff --git a/coregrind/amd64/Makefile.am b/coregrind/amd64/Makefile.am index 5453d083c1..ff04952875 100644 --- a/coregrind/amd64/Makefile.am +++ b/coregrind/amd64/Makefile.am @@ -7,8 +7,7 @@ BUILT_SOURCES = stage2.lds CLEANFILES = stage2.lds libarch_a_SOURCES = \ - cpuid.S \ - jmp_with_stack.c + cpuid.S # Extract ld's default linker script and hack it to our needs stage2.lds: Makefile diff --git a/coregrind/amd64/jmp_with_stack.c b/coregrind/amd64/jmp_with_stack.c deleted file mode 100644 index 1b7033e5a8..0000000000 --- a/coregrind/amd64/jmp_with_stack.c +++ /dev/null @@ -1,103 +0,0 @@ - -/* - This file is part of Valgrind, a dynamic binary instrumentation - framework. - - Copyright (C) 2000-2005 Julian Seward - jseward@acm.org - - 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 "ume.h" - - -#define ZERO_ALL_INT_REGS \ - " movq $0, %rax\n" \ - " movq $0, %rbx\n" \ - " movq $0, %rcx\n" \ - " movq $0, %rdx\n" \ - " movq $0, %rsi\n" \ - " movq $0, %rdi\n" \ - " movq $0, %rbp\n" \ - " movq $0, %r8\n" \ - " movq $0, %r9\n" \ - " movq $0, %r10\n" \ - " movq $0, %r11\n" \ - " movq $0, %r12\n" \ - " movq $0, %r13\n" \ - " movq $0, %r14\n" \ - " movq $0, %r15\n" - - -/* Jump to 'dst', but first set the stack pointer to 'stack'. Also, - clear all the integer registers before entering 'dst'. It's - important that the stack pointer is set to exactly 'stack' and not - (eg) stack - apparently_harmless_looking_small_offset. Basically - because the code at 'dst' might be wanting to scan the area above - 'stack' (viz, the auxv array), and putting spurious words on the - stack confuses it. -*/ -/* -__attribute__((noreturn)) -void jump_and_switch_stacks ( Addr stack, Addr dst ); - - %rdi == stack - %rsi == dst -*/ -asm( -".global jump_and_switch_stacks\n" -"jump_and_switch_stacks:\n" -" movq %rdi, %rsp\n" /* set stack */ -" pushq %rsi\n" /* f to stack*/ - ZERO_ALL_INT_REGS -" ret\n" /* jump to f */ -" ud2\n" /* should never get here */ -); - - - -/* Call f(arg1), but first switch stacks, using 'stack' as the new - stack, and use 'retaddr' as f's return-to address. Also, clear all - the integer registers before entering f.*/ -/* -__attribute__((noreturn)) -void call_on_new_stack_0_1 ( Addr stack, - Addr retaddr, - void (*f)(Word), - Word arg1 ); - %rdi == stack - %rsi == retaddr - %rdx == f - %rcx == arg1 -*/ -asm( -".global call_on_new_stack_0_1\n" -"call_on_new_stack_0_1:\n" -" movq %rdi, %rsp\n" /* set stack */ -" pushq %rsi\n" /* retaddr to stack */ -" pushq %rdx\n" /* f to stack*/ -" pushq %rcx\n" /* arg1 to stack*/ - ZERO_ALL_INT_REGS -" popq %rdi\n" /* arg1 to correct arg reg */ -" ret\n" /* jump to f */ -" ud2\n" /* should never get here */ -); - - -#undef ZERO_ALL_INT_REGS diff --git a/coregrind/arm/Makefile.am b/coregrind/arm/Makefile.am index 1735781220..3b0144aafa 100644 --- a/coregrind/arm/Makefile.am +++ b/coregrind/arm/Makefile.am @@ -6,8 +6,7 @@ noinst_LIBRARIES = libarch.a BUILT_SOURCES = stage2.lds CLEANFILES = stage2.lds -libarch_a_SOURCES = \ - jmp_with_stack.c +libarch_a_SOURCES = # Extract ld's default linker script and hack it to our needs stage2.lds: Makefile diff --git a/coregrind/arm/jmp_with_stack.c b/coregrind/arm/jmp_with_stack.c deleted file mode 100644 index 63e2a22c18..0000000000 --- a/coregrind/arm/jmp_with_stack.c +++ /dev/null @@ -1,38 +0,0 @@ - -/* - This file is part of Valgrind, a dynamic binary instrumentation - framework. - - Copyright (C) 2000-2005 Julian Seward - jseward@acm.org - - 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 "ume.h" - -void jmp_with_stack(Addr eip, Addr esp) -{ - // XXX: temporary only -extern int printf (__const char *__restrict __format, ...); -extern void exit (int __status); - printf("jmp_with_stack: argh\n"); - exit(1); - - // XXX: see x86/jmp_with_stack.c for how to implement this -} diff --git a/coregrind/m_syswrap/syswrap-amd64-linux.c b/coregrind/m_syswrap/syswrap-amd64-linux.c index d59dcea210..31df7e2efa 100644 --- a/coregrind/m_syswrap/syswrap-amd64-linux.c +++ b/coregrind/m_syswrap/syswrap-amd64-linux.c @@ -29,7 +29,6 @@ */ #include "core.h" -#include "ume.h" /* for jmp_with_stack */ #include "pub_core_debuglog.h" #include "pub_core_aspacemgr.h" #include "pub_core_options.h" @@ -197,6 +196,44 @@ static void run_a_thread_NORETURN ( Word tidW ) } +/* Call f(arg1), but first switch stacks, using 'stack' as the new + stack, and use 'retaddr' as f's return-to address. Also, clear all + the integer registers before entering f. */ +__attribute__((noreturn)) +void call_on_new_stack_0_1 ( Addr stack, + Addr retaddr, + void (*f)(Word), + Word arg1 ); +// %rdi == stack +// %rsi == retaddr +// %rdx == f +// %rcx == arg1 +asm( +"call_on_new_stack_0_1:\n" +" movq %rdi, %rsp\n" // set stack +" pushq %rsi\n" // retaddr to stack +" pushq %rdx\n" // f to stack +" pushq %rcx\n" // arg1 to stack +" movq $0, %rax\n" // zero all GP regs +" movq $0, %rbx\n" +" movq $0, %rcx\n" +" movq $0, %rdx\n" +" movq $0, %rsi\n" +" movq $0, %rdi\n" +" movq $0, %rbp\n" +" movq $0, %r8\n" +" movq $0, %r9\n" +" movq $0, %r10\n" +" movq $0, %r11\n" +" movq $0, %r12\n" +" movq $0, %r13\n" +" movq $0, %r14\n" +" movq $0, %r15\n" +" popq %rdi\n" // arg1 to correct arg reg +" ret\n" // jump to f +" ud2\n" // should never get here +); + /* Allocate a stack for the main thread, and run it all the way to the end. diff --git a/coregrind/m_syswrap/syswrap-x86-linux.c b/coregrind/m_syswrap/syswrap-x86-linux.c index 759abe584f..e800393f71 100644 --- a/coregrind/m_syswrap/syswrap-x86-linux.c +++ b/coregrind/m_syswrap/syswrap-x86-linux.c @@ -34,7 +34,6 @@ */ #include "core.h" -#include "ume.h" /* for jmp_with_stack */ #include "pub_core_debuglog.h" #include "pub_core_aspacemgr.h" #include "pub_core_options.h" @@ -203,6 +202,37 @@ static void run_a_thread_NORETURN ( Word tidW ) } +/* Call f(arg1), but first switch stacks, using 'stack' as the new + stack, and use 'retaddr' as f's return-to address. Also, clear all + the integer registers before entering f.*/ +__attribute__((noreturn)) +void call_on_new_stack_0_1 ( Addr stack, + Addr retaddr, + void (*f)(Word), + Word arg1 ); +// 4(%esp) == stack +// 8(%esp) == retaddr +// 12(%esp) == f +// 16(%esp) == arg1 +asm( +"call_on_new_stack_0_1:\n" +" movl %esp, %esi\n" // remember old stack pointer +" movl 4(%esi), %esp\n" // set stack +" pushl 16(%esi)\n" // arg1 to stack +" pushl 8(%esi)\n" // retaddr to stack +" pushl 12(%esi)\n" // f to stack +" movl $0, %eax\n" // zero all GP regs +" movl $0, %ebx\n" +" movl $0, %ecx\n" +" movl $0, %edx\n" +" movl $0, %esi\n" +" movl $0, %edi\n" +" movl $0, %ebp\n" +" ret\n" // jump to f +" ud2\n" // should never get here +); + + /* Allocate a stack for the main thread, and run it all the way to the end. diff --git a/coregrind/ume.c b/coregrind/ume.c index e3b38a238d..983dfe8a1a 100644 --- a/coregrind/ume.c +++ b/coregrind/ume.c @@ -121,6 +121,61 @@ void foreach_map(int (*fn)(char *start, char *end, } } +/*------------------------------------------------------------*/ +/*--- Stack switching ---*/ +/*------------------------------------------------------------*/ + +// __attribute__((noreturn)) +// void jump_and_switch_stacks ( Addr stack, Addr dst ); +#if defined(VGA_x86) +// 4(%esp) == stack +// 8(%esp) == dst +asm( +".global jump_and_switch_stacks\n" +"jump_and_switch_stacks:\n" +" movl %esp, %esi\n" // remember old stack pointer +" movl 4(%esi), %esp\n" // set stack +" pushl 8(%esi)\n" // dst to stack +" movl $0, %eax\n" // zero all GP regs +" movl $0, %ebx\n" +" movl $0, %ecx\n" +" movl $0, %edx\n" +" movl $0, %esi\n" +" movl $0, %edi\n" +" movl $0, %ebp\n" +" ret\n" // jump to dst +" ud2\n" // should never get here +); +#elif defined(VGA_amd64) +// %rdi == stack +// %rsi == dst +asm( +".global jump_and_switch_stacks\n" +"jump_and_switch_stacks:\n" +" movq %rdi, %rsp\n" // set stack +" pushq %rsi\n" // dst to stack +" movq $0, %rax\n" // zero all GP regs +" movq $0, %rbx\n" +" movq $0, %rcx\n" +" movq $0, %rdx\n" +" movq $0, %rsi\n" +" movq $0, %rdi\n" +" movq $0, %rbp\n" +" movq $0, %r8\n"\ +" movq $0, %r9\n"\ +" movq $0, %r10\n" +" movq $0, %r11\n" +" movq $0, %r12\n" +" movq $0, %r13\n" +" movq $0, %r14\n" +" movq $0, %r15\n" +" ret\n" // jump to dst +" ud2\n" // should never get here +); +#else +# error Unknown architecture +#endif + /*------------------------------------------------------------*/ /*--- Finding auxv on the stack ---*/ /*------------------------------------------------------------*/ diff --git a/coregrind/ume.h b/coregrind/ume.h index 39818e204d..a2679c7b78 100644 --- a/coregrind/ume.h +++ b/coregrind/ume.h @@ -54,23 +54,14 @@ void foreach_map(int (*fn)(char *start, char *end, because the code at 'dst' might be wanting to scan the area above 'stack' (viz, the auxv array), and putting spurious words on the stack confuses it. + + This is only exported so that vgtest_ume.c can use it. */ extern __attribute__((noreturn)) void jump_and_switch_stacks ( Addr stack, Addr dst ); -/* Call f(arg1), but first switch stacks, using 'stack' as the new - stack, and use 'retaddr' as f's return-to address. Also, clear all - the integer registers before entering f.*/ -extern -__attribute__((noreturn)) -void call_on_new_stack_0_1 ( Addr stack, - Addr retaddr, - void (*f)(Word), - Word arg1 ); - - /*------------------------------------------------------------*/ /*--- Loading ELF files ---*/ /*------------------------------------------------------------*/ diff --git a/coregrind/x86/Makefile.am b/coregrind/x86/Makefile.am index 6ed6d13caa..72a381fb37 100644 --- a/coregrind/x86/Makefile.am +++ b/coregrind/x86/Makefile.am @@ -7,8 +7,7 @@ BUILT_SOURCES = stage2.lds CLEANFILES = stage2.lds libarch_a_SOURCES = \ - cpuid.S \ - jmp_with_stack.c + cpuid.S # Extract ld's default linker script and hack it to our needs stage2.lds: Makefile diff --git a/coregrind/x86/jmp_with_stack.c b/coregrind/x86/jmp_with_stack.c deleted file mode 100644 index 17ce451898..0000000000 --- a/coregrind/x86/jmp_with_stack.c +++ /dev/null @@ -1,96 +0,0 @@ - -/* - This file is part of Valgrind, a dynamic binary instrumentation - framework. - - Copyright (C) 2000-2005 Julian Seward - jseward@acm.org - - 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 "ume.h" - - -#define ZERO_ALL_INT_REGS \ - " movl $0, %eax\n" \ - " movl $0, %ebx\n" \ - " movl $0, %ecx\n" \ - " movl $0, %edx\n" \ - " movl $0, %esi\n" \ - " movl $0, %edi\n" \ - " movl $0, %ebp\n" - - -/* Jump to 'dst', but first set the stack pointer to 'stack'. Also, - clear all the integer registers before entering 'dst'. It's - important that the stack pointer is set to exactly 'stack' and not - (eg) stack - apparently_harmless_looking_small_offset. Basically - because the code at 'dst' might be wanting to scan the area above - 'stack' (viz, the auxv array), and putting spurious words on the - stack confuses it. -*/ -/* -__attribute__((noreturn)) -void jump_and_switch_stacks ( Addr stack, Addr dst ); - - 4(%esp) == stack - 8(%esp) == f -*/ -asm( -".global jump_and_switch_stacks\n" -"jump_and_switch_stacks:\n" -" movl %esp, %esi\n" /* remember old stack pointer */ -" movl 4(%esi), %esp\n" /* set stack */ -" pushl 8(%esi)\n" /* f to stack*/ - ZERO_ALL_INT_REGS -" ret\n" /* jump to f */ -" ud2\n" /* should never get here */ -); - - - -/* Call f(arg1), but first switch stacks, using 'stack' as the new - stack, and use 'retaddr' as f's return-to address. Also, clear all - the integer registers before entering f.*/ -/* -__attribute__((noreturn)) -void call_on_new_stack_0_1 ( Addr stack, - Addr retaddr, - void (*f)(Word), - Word arg1 ); - 4(%esp) == stack - 8(%esp) == retaddr - 12(%esp) == f - 16(%esp) == arg1 -*/ -asm( -".global call_on_new_stack_0_1\n" -"call_on_new_stack_0_1:\n" -" movl %esp, %esi\n" /* remember old stack pointer */ -" movl 4(%esi), %esp\n" /* set stack */ -" pushl 16(%esi)\n" /* arg1 to stack */ -" pushl 8(%esi)\n" /* retaddr to stack */ -" pushl 12(%esi)\n" /* f to stack */ - ZERO_ALL_INT_REGS -" ret\n" /* jump to f */ -" ud2\n" /* should never get here */ -); - - -#undef ZERO_ALL_INT_REGS