From: Wei Liu Date: Fri, 9 Dec 2016 11:09:01 +0000 (+0000) Subject: x86emul/test: factor out emul_test_make_stack_executable X-Git-Tag: archive/raspbian/4.11.1-1+rpi1~1^2~66^2~3173 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=1105f057f5698aa25bb3e5f4dd2fd1f45c5fbfed;p=xen.git x86emul/test: factor out emul_test_make_stack_executable It will be used by emulator fuzzing target. Signed-off-by: Wei Liu Acked-by: Jan Beulich --- diff --git a/tools/tests/x86_emulator/test_x86_emulator.c b/tools/tests/x86_emulator/test_x86_emulator.c index eed8a0d179..0d80bff3ca 100644 --- a/tools/tests/x86_emulator/test_x86_emulator.c +++ b/tools/tests/x86_emulator/test_x86_emulator.c @@ -23,8 +23,6 @@ static const struct { #endif }; -#define MMAP_SZ 16384 - /* EFLAGS bit definitions. */ #define EFLG_OF (1<<11) #define EFLG_DF (1<<10) @@ -234,7 +232,6 @@ int main(int argc, char **argv) struct cpu_user_regs regs; char *instr; unsigned int *res, i, j; - unsigned long sp; bool stack_exec; int rc; #ifndef __x86_64__ @@ -258,13 +255,8 @@ int main(int argc, char **argv) } instr = (char *)res + 0x100; -#ifdef __x86_64__ - asm ("movq %%rsp, %0" : "=g" (sp)); -#else - asm ("movl %%esp, %0" : "=g" (sp)); -#endif - stack_exec = mprotect((void *)(sp & -0x1000L) - (MMAP_SZ - 0x1000), - MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EXEC) == 0; + stack_exec = emul_test_make_stack_executable(); + if ( !stack_exec ) printf("Warning: Stack could not be made executable (%d).\n", errno); diff --git a/tools/tests/x86_emulator/x86_emulate.c b/tools/tests/x86_emulator/x86_emulate.c index 66c2464bae..963dd71815 100644 --- a/tools/tests/x86_emulator/x86_emulate.c +++ b/tools/tests/x86_emulator/x86_emulate.c @@ -1,5 +1,7 @@ #include "x86_emulate.h" +#include + #define EFER_SCE (1 << 0) #define EFER_LMA (1 << 10) @@ -18,4 +20,22 @@ #define get_stub(stb) ((void *)((stb).addr = (uintptr_t)(stb).buf)) #define put_stub(stb) +bool emul_test_make_stack_executable(void) +{ + unsigned long sp; + + /* + * Mark the entire stack executable so that the stub executions + * don't fault + */ +#ifdef __x86_64__ + asm ("movq %%rsp, %0" : "=g" (sp)); +#else + asm ("movl %%esp, %0" : "=g" (sp)); +#endif + + return mprotect((void *)(sp & -0x1000L) - (MMAP_SZ - 0x1000), + MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EXEC) == 0; +} + #include "x86_emulate/x86_emulate.c" diff --git a/tools/tests/x86_emulator/x86_emulate.h b/tools/tests/x86_emulator/x86_emulate.h index 198132639f..a9b874cf54 100644 --- a/tools/tests/x86_emulator/x86_emulate.h +++ b/tools/tests/x86_emulator/x86_emulate.h @@ -33,4 +33,7 @@ #define is_canonical_address(x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63)) +#define MMAP_SZ 16384 +bool emul_test_make_stack_executable(void); + #include "x86_emulate/x86_emulate.h"