kvm-unit-tests: Add a func to run instruction in emulator
PATCH 1/2 kvm-unit-tests: Add a func to run instruction in emulator
Add a function trap_emulator to run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec in alt_insn and call func with alt_insn_length. Get results in outregs.
--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)
x86/emulator.c +81 -0
@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;
@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" +"push %rdx\n\r" +"push %rsi\n\r" +"push %rdi\n\r" + ); +// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" +"pop %rsi\n\r" +"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;
Signature · 1 line
1.7.9.5
PATCH 2/2 kvm-unit-tests: Change two cases to use trap_emulator
Change two functions (test_mmx_movq_mf and test_movabs) using unified trap_emulator.
--- x86/emulator.c | 66 ++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 52 deletions(-)
x86/emulator.c +14 -52
@@ -776,72 +776,34 @@ static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page, uint8_t *alt_insn_page, void *insn_ram) { uint16_t fcw = 0; // all exceptions unmasked -ulong *cr3 = (ulong *)read_cr3 (); +uint8_t alt_insn[] = {0x0f ,0x7f ,0x00 };// movq %mm0, (%rax) write_cr0 (read_cr0 () & ~6 );// TS, EM -// Place a trapping instruction in the page to trigger a VMEXIT - insn_page[0 ] =0x89 ;// mov %eax, (%rax) - insn_page[1 ] =0x00 ; - insn_page[2 ] =0x90 ;// nop - insn_page[3 ] =0xc3 ;// ret -// Place the instruction we want the hypervisor to see in the alternate page - alt_insn_page[0 ] =0x0f ;// movq %mm0, (%rax) - alt_insn_page[1 ] =0x7f ; - alt_insn_page[2 ] =0x00 ; - alt_insn_page[3 ] =0xc3 ;// ret - exceptions =0 ;handle_exception (MF_VECTOR , advance_rip_by_3_and_note_exception); - -// Load the code TLB with insn_page, but point the page tables at -// alt_insn_page (and keep the data TLB clear, for AMD decode assist). -// This will make the CPU trap on the insn_page instruction but the -// hypervisor will see alt_insn_page. -install_page (cr3,virt_to_phys (insn_page), insn_ram); asmvolatile ("fninit; fldcw %0" : :"m" (fcw)); asmvolatile ("fldz; fldz; fdivp" );// generate exception -invlpg (insn_ram); -// Load code TLB - asmvolatile ("call *%0" : :"r" (insn_ram +3 )); -install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); -// Trap, let hypervisor emulate at alt_insn_page - asmvolatile ("call *%0" : :"r" (insn_ram),"a" (mem)); + + inregs = (struct regs ){0 }; +trap_emulator (mem, insn_page, alt_insn_page, insn_ram, + alt_insn,3 );// exit MMX mode asmvolatile ("fnclex; emms" ); -report ("movq mmx generates #MF" , exceptions ==1 ); +report ("movq mmx generates #MF2" , exceptions ==1 );handle_exception (MF_VECTOR ,0 ); }static void test_movabs (uint64_t *mem,uint8_t *insn_page,uint8_t *alt_insn_page,void *insn_ram) { -uint64_t val =0 ; -ulong *cr3 = (ulong *)read_cr3 (); - -// Pad with RET instructions -memset (insn_page,0xc3 ,4096 ); -memset (alt_insn_page,0xc3 ,4096 ); -// Place a trapping instruction in the page to trigger a VMEXIT - insn_page[0 ] =0x89 ;// mov %eax, (%rax) - insn_page[1 ] =0x00 ; -// Place the instruction we want the hypervisor to see in the alternate -// page. A buggy hypervisor will fetch a 32-bit immediate and return -// 0xffffffffc3c3c3c3. - alt_insn_page[0 ] =0x48 ;// mov $0xc3c3c3c3c3c3c3c3, %rcx - alt_insn_page[1 ] =0xb9 ; - -// Load the code TLB with insn_page, but point the page tables at -// alt_insn_page (and keep the data TLB clear, for AMD decode assist). -// This will make the CPU trap on the insn_page instruction but the -// hypervisor will see alt_insn_page. -install_page (cr3,virt_to_phys (insn_page), insn_ram); -// Load code TLB -invlpg (insn_ram); - asmvolatile ("call *%0" : :"r" (insn_ram +3 )); -// Trap, let hypervisor emulate at alt_insn_page -install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); - asmvolatile ("call *%1" :"=c" (val) :"r" (insn_ram),"a" (mem),"c" (0 )); -report ("64-bit mov imm" , val ==0xc3c3c3c3c3c3c3c3 ); +// mov $0xc3c3c3c3c3c3c3c3, %rcx +uint8_t alt_insn[] = {0x48 ,0xb9 ,0xc3 ,0xc3 ,0xc3 , +0xc3 ,0xc3 ,0xc3 ,0xc3 ,0xc3 }; + inregs = (struct regs ){ .rcx =0 }; + +trap_emulator (mem, insn_page, alt_insn_page, insn_ram, + alt_insn,10 ); +report ("64-bit mov imm2" , outregs.rcx ==0xc3c3c3c3c3c3c3c3 ); }static void test_crosspage_mmio (volatile uint8_t *mem)
Signature · 1 line
1.7.9.5
Paolo Bonzini
maintainerRe: [PATCH 2/2] kvm-unit-tests: Change two cases to use trap_emulator
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 45 lines
Change two functions (
test_mmx_movq_mfandtest_movabs) using unifiedtrap_emulator.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 66 ++++++++++++-------------------------------------------- 1 file changed, 14 insertions(+), 52 deletions(-)x86/emulator.c +1
-22@@ -776,72 +776,34 @@ static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page, uint8_t *alt_insn_page, void *insn_ram) { uint16_t fcw = 0; // all exceptions unmasked -ulong *cr3 = (ulong *)read_cr3 (); +uint8_t alt_insn[] = {0x0f ,0x7f ,0x00 };// movq %mm0, (%rax) write_cr0 (read_cr0 () & ~6 );// TS, EM -// Place a trapping instruction in the page to trigger a VMEXIT - insn_page[0 ] =0x89 ;// mov %eax, (%rax) - insn_page[1 ] =0x00 ; - insn_page[2 ] =0x90 ;// nop - insn_page[3 ] =0xc3 ;// ret -// Place the instruction we want the hypervisor to see in the alternate page - alt_insn_page[0 ] =0x0f ;// movq %mm0, (%rax) - alt_insn_page[1 ] =0x7f ; - alt_insn_page[2 ] =0x00 ; - alt_insn_page[3 ] =0xc3 ;// ret - exceptions =0 ;handle_exception (MF_VECTOR , advance_rip_by_3_and_note_exception); - -// Load the code TLB with insn_page, but point the page tables at -// alt_insn_page (and keep the data TLB clear, for AMD decode assist). -// This will make the CPU trap on the insn_page instruction but the -// hypervisor will see alt_insn_page. -install_page (cr3,virt_to_phys (insn_page), insn_ram); asmvolatile ("fninit; fldcw %0" : :"m" (fcw)); asmvolatile ("fldz; fldz; fdivp" );// generate exception -invlpg (insn_ram); -// Load code TLB - asmvolatile ("call *%0" : :"r" (insn_ram +3 )); -install_page (cr3,virt_to_phys (alt_insn_page), insn_ram);
x86/emulator.c static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page, · line 792
Quoted 10 lines
x86/emulator.c +5
-3-// Trap, let hypervisor emulate at alt_insn_page - asmvolatile ("call *%0" : :"r" (insn_ram),"a" (mem)); + + inregs = (struct regs ){0 }; +trap_emulator (mem, insn_page, alt_insn_page, insn_ram, + alt_insn,3 );// exit MMX mode asmvolatile ("fnclex; emms" ); -report ("movq mmx generates #MF" , exceptions ==1 ); +report ("movq mmx generates #MF2" , exceptions ==1 );
Extra hunk that is not needed? Otherwise it looks good.
Thanks,
Paolo
Quoted 45 lines
x86/emulator.c +8
-27handle_exception (MF_VECTOR ,0 ); }static void test_movabs (uint64_t *mem,uint8_t *insn_page,uint8_t *alt_insn_page,void *insn_ram) { -uint64_t val =0 ; -ulong *cr3 = (ulong *)read_cr3 (); - -// Pad with RET instructions -memset (insn_page,0xc3 ,4096 ); -memset (alt_insn_page,0xc3 ,4096 ); -// Place a trapping instruction in the page to trigger a VMEXIT - insn_page[0 ] =0x89 ;// mov %eax, (%rax) - insn_page[1 ] =0x00 ; -// Place the instruction we want the hypervisor to see in the alternate -// page. A buggy hypervisor will fetch a 32-bit immediate and return -// 0xffffffffc3c3c3c3. - alt_insn_page[0 ] =0x48 ;// mov $0xc3c3c3c3c3c3c3c3, %rcx - alt_insn_page[1 ] =0xb9 ; - -// Load the code TLB with insn_page, but point the page tables at -// alt_insn_page (and keep the data TLB clear, for AMD decode assist). -// This will make the CPU trap on the insn_page instruction but the -// hypervisor will see alt_insn_page. -install_page (cr3,virt_to_phys (insn_page), insn_ram); -// Load code TLB -invlpg (insn_ram); - asmvolatile ("call *%0" : :"r" (insn_ram +3 )); -// Trap, let hypervisor emulate at alt_insn_page -install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); - asmvolatile ("call *%1" :"=c" (val) :"r" (insn_ram),"a" (mem),"c" (0 )); -report ("64-bit mov imm" , val ==0xc3c3c3c3c3c3c3c3 ); +// mov $0xc3c3c3c3c3c3c3c3, %rcx +uint8_t alt_insn[] = {0x48 ,0xb9 ,0xc3 ,0xc3 ,0xc3 , +0xc3 ,0xc3 ,0xc3 ,0xc3 ,0xc3 }; + inregs = (struct regs ){ .rcx =0 }; + +trap_emulator (mem, insn_page, alt_insn_page, insn_ram, + alt_insn,10 ); +report ("64-bit mov imm2" , outregs.rcx ==0xc3c3c3c3c3c3c3c3 ); }static void test_crosspage_mmio (volatile uint8_t *mem)
This version of save/restore general register seems a bit too ugly, I will change it and commit another patch.
Some of the registers cannot be set as realmode.c do, for example %rax used to save return value, wrong %esp %ebp may cause crash, and I think changed %rflags may cause some unknown error. So these registers should not be set by caller.
Arthur
Quoted 112 lines · Arthur Chunqi Li
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +81
-1@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" +"push %rdx\n\r" +"push %rsi\n\r" +"push %rdi\n\r" + ); +// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" +"pop %rsi\n\r" +"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions; --1.7.9.5
Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
Paolo Bonzini
maintainerIl 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 53 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +28
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i];
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 10 lines
x86/emulator.c +10
-0+ +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" +"push %rdx\n\r" +"push %rsi\n\r" +"push %rdi\n\r" + );
This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Paolo
Quoted 47 lines
x86/emulator.c +43
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" +"pop %rsi\n\r" +"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"
Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"
Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in calling insn_page, so registers should be saved to (save) in insn_page. Because all the instructions should be generated outside and copy to insn_page, and the instructions generated outside is RIP-relative, so inside insn_page (save) will be wrong pointed with RIP-relative code.
I have tried to move (save) into insn_page. But when calling insn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.
Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
If you have any better solutions, please let me know.
Thanks,
Arthur
Quoted 135 lines · On Thu, Jun 13, 2013 at 12:50 PM, 李春奇 <Arthur Chunqi Li> <[email protected]>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
Paolo Bonzini
maintainerIl 13/06/2013 05:30, 李春奇 <Arthur Chunqi Li> ha scritto:
Quoted 13 lines
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.I have tried to move (save) into
insn_page. But when callinginsn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.
Quoted 8 lines
Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
%rbp should not be a problem, on the other hand it's okay not to include %rsp in the registers struct (and assume insn_page/alt_insn_page do not touch it). Interestingly, both VMX and SVM put the guest RSP in the VM control information so that the switch occurs atomically with the start of the guest.
Paolo
Quoted 145 lines
If you have any better solutions, please let me know.
Thanks,
ArthurQuoted 135 lines · On Thu, Jun 13, 2013 at 12:50 PM, 李春奇 <Arthur Chunqi Li> <[email protected]>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 8 lines · 李春奇
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.
They do not have to be generated outside. You can write code into insn_page directly. Something like this outside of any functions:
asm(".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:"
"mov %%rax, outregs \n\t"
...
"test_insn:\n\t"
"in (%ds), %al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t"
"mov outregs, %%rax\n\t"
...
"ret\n\t"
".align 4096\n\t"
"insn_page_end:\n\t");Now you copy that into alt_insn_page, put instruction you want to test into test_insn offset and remap alt_insn_page into "insn_page" virtual address.
Quoted 150 lines
I have tried to move (save) into
insn_page. But when callinginsn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
If you have any better solutions, please let me know.
Thanks,
ArthurQuoted 129 lines · On Thu, Jun 13, 2013 at 12:50 PM, 李春奇 <Arthur Chunqi Li> <[email protected]>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 51 lines
Paolo
Quoted 47 lines
x86/emulator.c +43
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" +"pop %rsi\n\r" +"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;
Quoted 15 lines
Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaSignature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
--
Gleb.
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · 李春奇
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.
Which function can be used to remap alt_insn_page into "insn_page" virtual address?
Arthur
Quoted 167 lines
Quoted 160 lines
I have tried to move (save) into
insn_page. But when callinginsn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
If you have any better solutions, please let me know.
Thanks,
ArthurQuoted 129 lines · On Thu, Jun 13, 2013 at 12:50 PM, 李春奇 <Arthur Chunqi Li> <[email protected]>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 2 lines
--
Gleb.
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · 李春奇
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.
I used such codes:
invlpg((void *)virt_to_phys(insn_page));
asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asm volatile("call *%0": : "r"(insn_page+1));
But it seems that alt_insn_page are not remapped to insn_page. Here insn_page and alt_insn_page are all declared statically with "asm(...)".
Arthur
Quoted 167 lines
Quoted 160 lines
I have tried to move (save) into
insn_page. But when callinginsn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
If you have any better solutions, please let me know.
Thanks,
ArthurQuoted 129 lines · On Thu, Jun 13, 2013 at 12:50 PM, 李春奇 <Arthur Chunqi Li> <[email protected]>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 2 lines
--
Gleb.
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 31 lines · �??�?��?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · 李春奇
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?
Quoted 3 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));
+1?
Quoted 178 lines
But it seems that
alt_insn_pageare not remapped toinsn_page. Hereinsn_pageandalt_insn_pageare all declared statically with "asm(...)".Arthur
Quoted 167 lines
Quoted 160 lines
I have tried to move (save) into
insn_page. But when callinginsn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
If you have any better solutions, please let me know.
Thanks,
ArthurQuoted 129 lines · On Thu, Jun 13, 2013 at 12:50 PM, 李春奇 <Arthur Chunqi Li> <[email protected]>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.
Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
--
Gleb.
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?
This is a mistake, I changed it to "invlpg(insn_page)" but the result is the same.
Quoted 5 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));+1?
Here I put "ret" on the first byte of insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content of insn_page.
Quoted 186 lines
Quoted 179 lines
But it seems that
alt_insn_pageare not remapped toinsn_page. Hereinsn_pageandalt_insn_pageare all declared statically with "asm(...)".Arthur
Quoted 167 lines
Quoted 160 lines
I have tried to move (save) into
insn_page. But when callinginsn_page, data in it can only be read and any instructions like "xchg %%rax, 0+%[save]" may cause error, because at this time read is from TLB but write will cause inconsistent.Another way is disabling RIP-relative code, but I failed when using "-mcmodel-large -fno-pic", the binary is also using RIP-relative mode. Is there any way to totally disable RIP-relative code? Besides, using this feature may specified to some newer C compiler. This may not be a good solution.
If we don't set %rsp and %rbp when executing emulator code, we can just use “push/pop" to save other general registers.
If you have any better solutions, please let me know.
Thanks,
ArthurQuoted 129 lines · On Thu, Jun 13, 2013 at 12:50 PM, ÊùéÊò••á <Arthur Chunqi Li>…
Quoted 61 lines · Paolo Bonzini
Il 06/06/2013 11:24, Arthur Chunqi Li ha scritto:
Quoted 60 lines
Add a function
trap_emulatorto run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec inalt_insnand call func withalt_insn_length. Get results in outregs.Signed-off-by Arthur Chunqi Li <[email protected]>--- x86/emulator.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+)x86/emulator.c +35
-0@@ -11,6 +11,14 @@ int fails, tests;static int exceptions; +struct regs { +u64 rax, rbx, rcx, rdx; +u64 rsi, rdi, rsp, rbp; +u64 rip, rflags; +}; + +static struct regs inregs, outregs; +void report (const char *name,int result) { ++tests;@@ -685,6 +693,79 @@ static void test_shld_shrd(u32 *mem)report ("shrd (cl)" , *mem == ((0x12345678 >>3 ) | (5u <<29 ))); } +static void trap_emulator (uint64_t *mem,uint8_t *insn_page, +uint8_t *alt_insn_page,void *insn_ram, +uint8_t *alt_insn,int alt_insn_length) +{ +ulong *cr3 = (ulong *)read_cr3 (); +int i; + +// Pad with RET instructions +memset (insn_page,0xc3 ,4096 ); +memset (alt_insn_page,0xc3 ,4096 ); + +// Place a trapping instruction in the page to trigger a VMEXIT + insn_page[0 ] =0x89 ;// mov %eax, (%rax) + insn_page[1 ] =0x00 ; + insn_page[2 ] =0x90 ;// nop + insn_page[3 ] =0xc3 ;// ret + +// Place the instruction we want the hypervisor to see in the alternate page +for (i=0 ; i<alt_insn_length; i++) + alt_insn_page[i] = alt_insn[i]; + +// Save general registers + asmvolatile ( +"push %rax\n\r" +"push %rbx\n\r" +"push %rcx\n\r" + "push %rdx\n\r"Quoted 8 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 725
Quoted 3 lines
x86/emulator.c +3
-0+"push %rsi\n\r" +"push %rdi\n\r" + );This will not work if GCC is using rsp-relative addresses to access local variables. You need to use mov instructions to load from inregs, and put the push/pop sequences inside the "main" asm that does the "call *%1".
Is there any way to let gcc use absolute address to access variables? I move variant "save" to the global and use "xchg %%rax, 0+%[save]" and it seems that addressing for "save" is wrong.
Arthur
Quoted 42 lines
Paolo
Quoted 39 lines
x86/emulator.c +39
-0+// Load the code TLB with insn_page, but point the page tables at +// alt_insn_page (and keep the data TLB clear, for AMD decode assist). +// This will make the CPU trap on the insn_page instruction but the +// hypervisor will see alt_insn_page. +install_page (cr3,virt_to_phys (insn_page), insn_ram); +invlpg (insn_ram); +// Load code TLB + asmvolatile ("call *%0" : :"r" (insn_ram +3 )); +install_page (cr3,virt_to_phys (alt_insn_page), insn_ram); +// Trap, let hypervisor emulate at alt_insn_page + asmvolatile ( +"call *%1\n\r" + +"mov %%rax, 0+%[outregs] \n\t" +"mov %%rbx, 8+%[outregs] \n\t" +"mov %%rcx, 16+%[outregs] \n\t" +"mov %%rdx, 24+%[outregs] \n\t" +"mov %%rsi, 32+%[outregs] \n\t" +"mov %%rdi, 40+%[outregs] \n\t" +"mov %%rsp,48+ %[outregs] \n\t" +"mov %%rbp, 56+%[outregs] \n\t" + +/* Save RFLAGS in outregs*/ +"pushf \n\t" +"popq 72+%[outregs] \n\t" + : [outregs]"+m" (outregs) + :"r" (insn_ram), +"a" (mem),"b" (inregs.rbx), +"c" (inregs.rcx),"d" (inregs.rdx), +"S" (inregs.rsi),"D" (inregs.rdi) + :"memory" ,"cc" + ); +// Restore general registers + asmvolatile ( +"pop %rax\n\r" +"pop %rbx\n\r" +"pop %rcx\n\r" +"pop %rdx\n\r" + "pop %rsi\n\r"Quoted 9 lines
x86/emulator.c static void test_shld_shrd(u32 *mem) · line 771
Quoted 8 lines
x86/emulator.c +4
-0+"pop %rdi\n\r" + ); +} +static void advance_rip_by_3_and_note_exception (struct ex_regs *regs) { ++exceptions;Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 2 lines
--
Gleb.
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 42 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.
Send the code.
--
Gleb.
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];
asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t"
"ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t"
"push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t"
"test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t"
"pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t"
"ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);
static void mk_insn_page(uint8_t *alt_insn_page,uint8_t *alt_insn, int alt_insn_length)
{
int i, emul_offset;
for (i=1; i<test_insn_end - test_insn; i++)
test_insn[i] = 0x90; // nop
emul_offset = test_insn - insn_page;
for (i=0; i<alt_insn_length; i++)
alt_insn_page[i+emul_offset] = alt_insn[i];
}static void trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length)
{
ulong *cr3 = (ulong *)read_cr3();
int save_offset = (u8 *)(&save) - insn_page;
memset(alt_insn_page, 0x90, 4096);
save = inregs;
mk_insn_page(alt_insn_page, alt_insn, alt_insn_length);
// Load the code TLB with insn_page, but point the page tables at
// alt_insn_page (and keep the data TLB clear, for AMD decode assist).
// This will make the CPU trap on the insn_page instruction but the
// hypervisor will see alt_insn_page.
//install_page(cr3, virt_to_phys(insn_page), insn_page);
invlpg(insn_page);
// Load code TLB
asm volatile("call *%0" : : "r"(insn_page));
install_page(cr3, virt_to_phys(alt_insn_page), insn_page);
// Trap, let hypervisor emulate at alt_insn_page
asm volatile("call *%0": : "r"(insn_page+1));
outregs = *((struct regs *)(&alt_insn_page[save_offset]));
}static void test_movabs(uint64_t *mem)
{
// mov $0xc3c3c3c3c3c3c3c3, %rcx
uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3,
0xc3, 0xc3, 0xc3, 0xc3, 0xc3};
inregs = (struct regs){ 0 };
trap_emulator(mem, alt_insn, 10);
report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3);
}Quoted 54 lines · Gleb Natapov
Quoted 42 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.
Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);
Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nop
Why? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);
alt_insn_page should contains the same instruction as insn_page except between test_insn and test_insn_end. I do not know how you expect it to work otherwise.
Quoted 74 lines
save = inregs; mk_insn_page(alt_insn_page, alt_insn, alt_insn_length); // Load the code TLB with insn_page, but point the page tables at // alt_insn_page (and keep the data TLB clear, for AMD decode assist). // This will make the CPU trap on the insn_page instruction but the // hypervisor will see alt_insn_page. //install_page(cr3, virt_to_phys(insn_page), insn_page); invlpg(insn_page); // Load code TLB asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); // Trap, let hypervisor emulate at alt_insn_page asm volatile("call *%0": : "r"(insn_page+1)); outregs = *((struct regs *)(&alt_insn_page[save_offset])); }static void
test_movabs(uint64_t *mem){ // mov $0xc3c3c3c3c3c3c3c3, %rcx uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}; inregs = (struct regs){ 0 }; trap_emulator(mem, alt_insn, 10); report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3); }Quoted 46 lines · Gleb Natapov
Quoted 45 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 4 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));
Quoted 17 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
--
Gleb.
Quoted 104 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 10 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page;
Quoted 4 lines
Quoted 1 line
memset(alt_insn_page, 0x90, 4096);
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.
In my oponion, only codes between test_insn and test_insn_end in alt_insn_page need to be set, insn_page will be executed in the guest, and when trapping into emulator OS will load alt_insn_page (because of invlpg(insn_page)), then return to guest with executing insn_page (from TLB). I don't know if this is right, but I use this trick in my previous patch and it runs well. I use "trace-cmd record -e kvm" to trace it and found instructions in alt_insn_page are not executed, so I suppose that alt_insn_page is not loaded to the right place.
Arthur
Quoted 93 lines
Quoted 86 lines
save = inregs; mk_insn_page(alt_insn_page, alt_insn, alt_insn_length); // Load the code TLB with insn_page, but point the page tables at // alt_insn_page (and keep the data TLB clear, for AMD decode assist). // This will make the CPU trap on the insn_page instruction but the // hypervisor will see alt_insn_page. //install_page(cr3, virt_to_phys(insn_page), insn_page); invlpg(insn_page); // Load code TLB asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); // Trap, let hypervisor emulate at alt_insn_page asm volatile("call *%0": : "r"(insn_page+1)); outregs = *((struct regs *)(&alt_insn_page[save_offset])); }static void
test_movabs(uint64_t *mem){ // mov $0xc3c3c3c3c3c3c3c3, %rcx uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}; inregs = (struct regs){ 0 }; trap_emulator(mem, alt_insn, 10); report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3); }Quoted 46 lines · Gleb Natapov
Quoted 45 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 4 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 2 lines
--
Gleb.
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 106 lines · 李春奇
Quoted 105 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);
Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).
While before trap the code will likely be executed from insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed from alt_insn_page if guest is scheduled out after invlpg() and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.
Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on alt_insn_page.
Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.
Do you see "in" instruction emulated? Anyway current code is incorrect since current install_page() implementation cannot handle large pages and the code is backed up by large pages. You can fix install_page() to check for that and break large page into small one before installing a page.
Quoted 100 lines
Arthur
Quoted 93 lines
Quoted 86 lines
save = inregs; mk_insn_page(alt_insn_page, alt_insn, alt_insn_length); // Load the code TLB with insn_page, but point the page tables at // alt_insn_page (and keep the data TLB clear, for AMD decode assist). // This will make the CPU trap on the insn_page instruction but the // hypervisor will see alt_insn_page. //install_page(cr3, virt_to_phys(insn_page), insn_page); invlpg(insn_page); // Load code TLB asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); // Trap, let hypervisor emulate at alt_insn_page asm volatile("call *%0": : "r"(insn_page+1)); outregs = *((struct regs *)(&alt_insn_page[save_offset])); }static void
test_movabs(uint64_t *mem){ // mov $0xc3c3c3c3c3c3c3c3, %rcx uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}; inregs = (struct regs){ 0 }; trap_emulator(mem, alt_insn, 10); report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3); }Quoted 46 lines · Gleb Natapov
Quoted 45 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 4 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.
Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
--
Gleb.
Quoted 129 lines · Gleb Natapov
Quoted 106 lines · 李春奇
Quoted 105 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.
Quoted 8 lines
Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.
Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
Arthur
Quoted 108 lines
Quoted 101 lines
Arthur
Quoted 93 lines
Quoted 86 lines
save = inregs; mk_insn_page(alt_insn_page, alt_insn, alt_insn_length); // Load the code TLB with insn_page, but point the page tables at // alt_insn_page (and keep the data TLB clear, for AMD decode assist). // This will make the CPU trap on the insn_page instruction but the // hypervisor will see alt_insn_page. //install_page(cr3, virt_to_phys(insn_page), insn_page); invlpg(insn_page); // Load code TLB asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); // Trap, let hypervisor emulate at alt_insn_page asm volatile("call *%0": : "r"(insn_page+1)); outregs = *((struct regs *)(&alt_insn_page[save_offset])); }static void
test_movabs(uint64_t *mem){ // mov $0xc3c3c3c3c3c3c3c3, %rcx uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}; inregs = (struct regs){ 0 }; trap_emulator(mem, alt_insn, 10); report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3); }Quoted 46 lines · Gleb Natapov
Quoted 45 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 4 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 2 lines
--
Gleb.
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 133 lines · 李春奇
Quoted 132 lines · Gleb Natapov
Quoted 106 lines · 李春奇
Quoted 105 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.
Quoted 8 lines
Quoted 5 lines
Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.
It is used when initial page tables are created.
See lib/x86/vm.c:setup_mmu_range()
Quoted 3 lines
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
No, they are 4K pages.
Quoted 114 lines
Arthur
Quoted 108 lines
Quoted 101 lines
Arthur
Quoted 93 lines
Quoted 86 lines
save = inregs; mk_insn_page(alt_insn_page, alt_insn, alt_insn_length); // Load the code TLB with insn_page, but point the page tables at // alt_insn_page (and keep the data TLB clear, for AMD decode assist). // This will make the CPU trap on the insn_page instruction but the // hypervisor will see alt_insn_page. //install_page(cr3, virt_to_phys(insn_page), insn_page); invlpg(insn_page); // Load code TLB asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); // Trap, let hypervisor emulate at alt_insn_page asm volatile("call *%0": : "r"(insn_page+1)); outregs = *((struct regs *)(&alt_insn_page[save_offset])); }static void
test_movabs(uint64_t *mem){ // mov $0xc3c3c3c3c3c3c3c3, %rcx uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}; inregs = (struct regs){ 0 }; trap_emulator(mem, alt_insn, 10); report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3); }Quoted 46 lines · Gleb Natapov
Quoted 45 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 4 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.
Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
--
Gleb.
Quoted 142 lines · Gleb Natapov
Quoted 133 lines · 李春奇
Quoted 132 lines · Gleb Natapov
Quoted 106 lines · 李春奇
Quoted 105 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.Quoted 8 lines
Quoted 5 lines
Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.
Quoted 7 lines
It is used when initial page tables are created.
See lib/x86/vm.c:setup_mmu_range()Quoted 3 lines
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
No, they are 4K pages.
Thus why dynamically creating insn_page and alt_insn_page with alloc_page() can get the right result?
Quoted 122 lines
Quoted 115 lines
Arthur
Quoted 108 lines
Quoted 101 lines
Arthur
Quoted 93 lines
Quoted 86 lines
save = inregs; mk_insn_page(alt_insn_page, alt_insn, alt_insn_length); // Load the code TLB with insn_page, but point the page tables at // alt_insn_page (and keep the data TLB clear, for AMD decode assist). // This will make the CPU trap on the insn_page instruction but the // hypervisor will see alt_insn_page. //install_page(cr3, virt_to_phys(insn_page), insn_page); invlpg(insn_page); // Load code TLB asm volatile("call *%0" : : "r"(insn_page)); install_page(cr3, virt_to_phys(alt_insn_page), insn_page); // Trap, let hypervisor emulate at alt_insn_page asm volatile("call *%0": : "r"(insn_page+1)); outregs = *((struct regs *)(&alt_insn_page[save_offset])); }static void
test_movabs(uint64_t *mem){ // mov $0xc3c3c3c3c3c3c3c3, %rcx uint8_t alt_insn[] = {0x48, 0xb9, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}; inregs = (struct regs){ 0 }; trap_emulator(mem, alt_insn, 10); report("64-bit mov imm2", outregs.rcx == 0xc3c3c3c3c3c3c3c3); }Quoted 46 lines · Gleb Natapov
Quoted 45 lines · 李春奇
Quoted 32 lines · Gleb Natapov
Quoted 31 lines · Ê??Ê?••?
Quoted 30 lines · Gleb Natapov
Quoted 8 lines · ÊùéÊò••á
Hi Gleb,
I'm trying to solve these problems in the past days and meet many difficulties. You want to save all the general registers in callinginsn_page, so registers should be saved to (save) ininsn_page. Because all the instructions should be generated outside and copy toinsn_page, and the instructions generated outside is RIP-relative, so insideinsn_page(save) will be wrong pointed with RIP-relative code.They do not have to be generated outside. You can write code into
insn_pagedirectly. Something like this outside of any functions:asm(".align 4096\n\t"
".global insn_page\n\t" ".global insn_page_end\n\t" ".global test_insn\n\t" ".global test_insn_end\n\t" "insn_page:" "mov %%rax, outregs \n\t" ... "test_insn:\n\t" "in (%ds), %al\n\t" ". = . + 31\n\t" "test_insn_end:\n\t" "mov outregs, %%rax\n\t" ... "ret\n\t" ".align 4096\n\t" "insn_page_end:\n\t");Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Now you copy that into
alt_insn_page, put instruction you want to test intotest_insnoffset and remapalt_insn_pageinto "insn_page" virtual address.I used such codes:
invlpg((void *)virt_to_phys(insn_page));
virt_to_phys?This is a mistake, I changed it to "
invlpg(insn_page)" but the result is the same.Quoted 4 lines
Quoted 4 lines
asm
volatile("call *%0" : : "r"(insn_page));install_page(cr3, virt_to_phys(alt_insn_page), insn_page); asmvolatile("call *%0": : "r"(insn_page+1));Quoted 8 lines
Quoted 4 lines
Quoted 1 line
+1?
Here I put "ret" on the first byte of
insn_page, so the first call of "insn_page" can just return, and the second call of "insn_page+1“ will directly call the second byte, which is the real content ofinsn_page.Send the code.
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, ChinaQuoted 2 lines
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 2 lines
--
Gleb.
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China
Quoted 146 lines · 李春奇
Quoted 145 lines · Gleb Natapov
Quoted 133 lines · 李春奇
Quoted 132 lines · Gleb Natapov
Quoted 106 lines · 李春奇
Quoted 105 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.Quoted 8 lines
Quoted 5 lines
Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.It is used when initial page tables are created.
See lib/x86/vm.c:setup_mmu_range()
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
No, they are 4K pages.
Thus why dynamically creating
insn_pageandalt_insn_pagewithalloc_page()can get the right result?
Probably.
--
Gleb.
I found the final reason! The initial use of init_ram is also used by test_rip_relative(), which will cause conflict. I changed it and everything runs well.
Quoted 157 lines · Gleb Natapov
Quoted 146 lines · 李春奇
Quoted 145 lines · Gleb Natapov
Quoted 133 lines · 李春奇
Quoted 132 lines · Gleb Natapov
Quoted 106 lines · 李春奇
Quoted 105 lines · Gleb Natapov
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.Quoted 8 lines
Quoted 5 lines
Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.It is used when initial page tables are created.
See lib/x86/vm.c:setup_mmu_range()Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
No, they are 4K pages.
Thus why dynamically creating
insn_pageandalt_insn_pagewithalloc_page()can get the right result?Probably.
--
Gleb.
Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
Quoted 4 lines · 李春奇
I found the final reason! The initial use of
init_ramis also used bytest_rip_relative(), which will cause conflict. I changed it and everything runs well.
Not sure what you mean. Your version of test_movabs does not use insn_ram.
Quoted 151 lines · Gleb Natapov
Quoted 150 lines · 李春奇
Quoted 149 lines · Gleb Natapov
Quoted 148 lines · 李春奇
Quoted 133 lines · Gleb Natapov
Quoted 132 lines · 李春奇
Quoted 106 lines · Gleb Natapov
Quoted 105 lines
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.Quoted 8 lines
Quoted 5 lines
Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.It is used when initial page tables are created.
See lib/x86/vm.c:setup_mmu_range()Quoted 3 lines
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
Quoted 16 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
No, they are 4K pages.
Thus why dynamically creating
insn_pageandalt_insn_pagewithalloc_page()can get the right result?Probably.
--
Gleb.Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China
--
Gleb.
I use insn_ram as what the origin/master done before. I don't know how to describe it clearly, I will commit a patch later and you can get to know from my codes.
Arthur
Quoted 177 lines · Gleb Natapov
Quoted 4 lines · 李春奇
I found the final reason! The initial use of
init_ramis also used bytest_rip_relative(), which will cause conflict. I changed it and everything runs well.Not sure what you mean. Your version of
test_movabsdoes not useinsn_ram.Quoted 161 lines · Gleb Natapov
Quoted 150 lines · 李春奇
Quoted 149 lines · Gleb Natapov
Quoted 148 lines · 李春奇
Quoted 133 lines · Gleb Natapov
Quoted 132 lines · 李春奇
Quoted 106 lines · Gleb Natapov
Quoted 105 lines
Send code in a form of a patch.
Quoted 83 lines · 李春奇
extern u8 insn_page[], insn_page_end[];
extern u8 test_insn[], test_insn_end[];
extern u8 alt_insn_page[];asm(
".align 4096\n\t"
".global insn_page\n\t"
".global insn_page_end\n\t"
".global test_insn\n\t"
".global test_insn_end\n\t"
"insn_page:\n\t""ret \n\t"
"push %rax; push %rbx\n\t"
"push %rcx; push %rdx\n\t"
"push %rsi; push %rdi\n\t"
"push %rbp\n\t"
"push %r8; push %r9\n\t"
"push %r10; push %r11\n\t"
"push %r12; push %r13\n\t"
"push %r14; push %r15\n\t"
"pushf\n\t""push 136+save \n\t"
"popf \n\t"
"mov 0+save, %rax \n\t"
"mov 8+save, %rbx \n\t"
"mov 16+save, %rcx \n\t"
"mov 24+save, %rdx \n\t"
"mov 32+save, %rsi \n\t"
"mov 40+save, %rdi \n\t"
"mov 56+save, %rbp \n\t"
"mov 64+save, %r8 \n\t"
"mov 72+save, %r9 \n\t"
"mov 80+save, %r10 \n\t"
"mov 88+save, %r11 \n\t"
"mov 96+save, %r12 \n\t"
"mov 104+save, %r13 \n\t"
"mov 112+save, %r14 \n\t"
"mov 120+save, %r15 \n\t""test_insn:\n\t"
"in (%dx),%al\n\t"
". = . + 31\n\t"
"test_insn_end:\n\t""pushf \n\t"
"pop 136+save \n\t"
"mov %rax, 0+save \n\t"
"mov %rbx, 8+save \n\t"
"mov %rcx, 16+save \n\t"
"mov %rdx, 24+save \n\t"
"mov %rsi, 32+save \n\t"
"mov %rdi, 40+save \n\t"
"mov %rbp, 56+save \n\t"
"mov %r8, 64+save \n\t"
"mov %r9, 72+save \n\t"
"mov %r10, 80+save \n\t"
"mov %r11, 88+save \n\t"
"mov %r12, 96+save \n\t"
"mov %r13, 104+save \n\t"
"mov %r14, 112+save \n\t"
"mov %r15, 120+save \n\t"
"popf \n\t"
"pop %r15; pop %r14 \n\t"
"pop %r13; pop %r12 \n\t"
"pop %r11; pop %r10 \n\t"
"pop %r9; pop %r8 \n\t"
"pop %rbp \n\t"
"pop %rdi; pop %rsi \n\t"
"pop %rdx; pop %rcx \n\t"
"pop %rbx; pop %rax \n\t""ret\n\t"
"save:\n\t"
". = . + 256\n\t"
".align 4096\n\t"
"alt_insn_page:\n\t"
". = . + 4096\n\t"
);Quoted 6 lines
static void mk_insn_page(uint8_t
*alt_insn_page,uint8_t*alt_insn, intalt_insn_length){ int i, emul_offset; for (i=1; i<test_insn_end - test_insn; i++) test_insn[i] = 0x90; // nopWhy? Gcc should pad it with nops.
Quoted 11 lines
emul_offset = test_insn - insn_page; for (i=0; i<alt_insn_length; i++) alt_insn_page[i+emul_offset] = alt_insn[i]; }static void
trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length){ ulong *cr3 = (ulong *)read_cr3(); int save_offset = (u8 *)(&save) - insn_page; memset(alt_insn_page, 0x90, 4096);Quoted 8 lines
Quoted 3 lines
alt_insn_pageshould contains the same instruction asinsn_pageexcept betweentest_insnandtest_insn_end. I do not know how you expect it to work otherwise.In my oponion, only codes between
test_insnandtest_insn_endinalt_insn_pageneed to be set,insn_pagewill be executed in the guest, and when trapping into emulator OS will loadalt_insn_page(because ofinvlpg(insn_page)), then return to guest with executinginsn_page(from TLB).While before trap the code will likely be executed from
insn_page, but after the trap it is very optimistic to assume that tlb cache will still contain this virtual address since host will execute quite a lot of code and can even schedule in the middle, so the TLB will not contain the address and your test will crash. Even the code before test instruction can be executed fromalt_insn_pageif guest is scheduled out afterinvlpg()and before it executes every instruction until trapping one. In your case the test will crash too instead of yielding false positive.Quoted 2 lines
I don't know if this is right, but I use this trick in my previous patch and it runs well.
Your previous patches always had c3 (ret) after tested instruction on
alt_insn_page.Quoted 3 lines
I use "trace-cmd record -e kvm" to
trace it and found instructions inalt_insn_pageare not executed, so I suppose thatalt_insn_pageis not loaded to the right place.Quoted 8 lines
Quoted 5 lines
Do you see "in" instruction emulated? Anyway current code is incorrect since current
install_page()implementation cannot handle large pages and the code is backed up by large pages. You can fixinstall_page()to check for that and break large page into small one before installing a page.Here I have two questions.
1. There's another function called "install_large_page", can it be used to our occasion? I found that this function is not used at all.It is used when initial page tables are created.
See lib/x86/vm.c:setup_mmu_range()Quoted 3 lines
2. Why will current version runs well? Do pages allocated dynamically are automatically aligned to 2MB (large page size)?
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
No, they are 4K pages.
Thus why dynamically creating
insn_pageandalt_insn_pagewithalloc_page()can get the right result?Probably.
--
Gleb.Quoted 6 lines
--
Arthur Chunqi Li
Department of Computer Science
School of EECS
Peking University
Beijing, China--
Gleb.
Signature · 5 lines
Arthur Chunqi Li Department of Computer Science School of EECS Peking University Beijing, China