: x86: Add tests for VMware backdoor
cover : x86: Add tests for VMware backdoor
Hi,
This series adds a set of unit tests to series:
"KVM: x86: Add support for VMware backdoor I/O ports & Pseduo-PMCs"
The series is split into the following patches:
1. Add an x86 utility to run a function in User Mode.
2. Add a test utility to run a function in User Mode / Kernel Mode, catch an exception, and run a callback to test function return value.
3. Add CR4 PCE bit definition.
4. Add x86_64 segment descriptor struct.
5. Add VMware backdoor tests:
Test VMware port and VMware RDPMC backdoors, both in user mode and kernel mode.
Thanks,
Arbel
PATCH 1/5 x86: Add utility to run function in User Mode
Add usermode util that enables running a function in user mode. In addition, it enables catching an exception which is raised from the user-mode function.
--- lib/x86/usermode.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/usermode.h | 30 ++++++++++++++ x86/Makefile.common | 1 + 3 files changed, 144 insertions(+) create mode 100644 lib/x86/usermode.c create mode 100644 lib/x86/usermode.h
lib/x86/usermode.c new file +113 -0
@@ -0,0 +1,113 @@ +#include "x86/msr.h" +#include "x86/processor.h" +#include "x86/apic-defs.h" +#include "x86/apic.h" +#include "x86/desc.h" +#include "x86/isr.h" +#include "alloc.h" +#include "setjmp.h" +#include "usermode.h" + +#include "libcflat.h" +#include <stdint.h> + +#define USERMODE_STACK_SIZE 0x2000 +#define RET_TO_KERNEL_IRQ 0x20 + +jmp_buf jmpbuf; + +static void restore_exec_to_jmpbuf (void ) +{ +longjmp (jmpbuf,1 ); +} + +static void restore_exec_to_jmpbuf_exception_handler (struct ex_regs *regs) +{ +/* longjmp must happen after iret, so do not do it now. */ + regs->rip = (unsigned long )&restore_exec_to_jmpbuf; + regs->cs =KERNEL_CS ; +} + +uint64_t run_in_user (usermode_func func,unsigned int fault_vector, +uint64_t arg1,uint64_t arg2,uint64_t arg3, +uint64_t arg4,bool *raised_vector) +{ +extern char ret_to_kernel; +uint64_t rax =0 ; +static unsigned char user_stack[USERMODE_STACK_SIZE ]; + + *raised_vector =0 ; +set_idt_entry (RET_TO_KERNEL_IRQ , &ret_to_kernel,3 ); +handle_exception (fault_vector, + restore_exec_to_jmpbuf_exception_handler); + +if (setjmp (jmpbuf) !=0 ) { + *raised_vector =1 ; +return 0 ; + } + + asmvolatile ( +/* Backing Up Stack in rdi */ +"mov %%rsp, %%rdi\n\t" +/* Load user_ds to DS and ES */ +"mov %[user_ds], %%ax\n\t" +"mov %%ax, %%ds\n\t" +"mov %%ax, %%es\n\t" +/* IRET into user mode */ +"pushq %[user_ds]\n\t" +"pushq %[user_stack_top]\n\t" +"pushfq\n\t" +"pushq %[user_cs]\n\t" +"pushq $user_mode\n\t" +"iretq\n" + +"user_mode:\n\t" +/* Back up registers before invoking func */ +"push %%rbx\n\t" +"push %%rcx\n\t" +"push %%rdx\n\t" +"push %%r8\n\t" +"push %%r9\n\t" +"push %%r10\n\t" +"push %%r11\n\t" +"push %%rdi\n\t" +"push %%rsi\n\t" +/* Call user mode function */ +"mov %[arg1], %%rdi\n\t" +"mov %[arg2], %%rsi\n\t" +"mov %[arg3], %%rdx\n\t" +"mov %[arg4], %%rcx\n\t" +"call %[func]\n\t" +/* Restore registers */ +"pop %%rsi\n\t" +"pop %%rdi\n\t" +"pop %%r11\n\t" +"pop %%r10\n\t" +"pop %%r9\n\t" +"pop %%r8\n\t" +"pop %%rdx\n\t" +"pop %%rcx\n\t" +"pop %%rbx\n\t" +/* Return to kernel via system call */ +"int %[kernel_entry_vector]\n\t" +/* Kernel Mode */ +"ret_to_kernel:\n\t" +"mov %%rdi, %%rsp\n\t" + : +"+a" (rax) + : + [arg1]"m" (arg1), + [arg2]"m" (arg2), + [arg3]"m" (arg3), + [arg4]"m" (arg4), + [func]"m" (func), + [user_ds]"i" (USER_DS ), + [user_cs]"i" (USER_CS ), + [user_stack_top]"r" (user_stack + +sizeof (user_stack)), + [kernel_entry_vector]"i" (RET_TO_KERNEL_IRQ ) + : +"rsi" ,"rdi" ,"rcx" ,"rdx" ); + +return rax; +}
lib/x86/usermode.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef _USERMODE_H_ +#define _USERMODE_H_ + +#include "x86/msr.h" +#include "x86/processor.h" +#include "x86/apic-defs.h" +#include "x86/apic.h" +#include "x86/desc.h" +#include "x86/isr.h" +#include "alloc.h" +#include "setjmp.h" + +#include "libcflat.h" +#include <stdint.h> + +typedef uint64_t (*usermode_func )(void ); + +/* +* Run function in user mode +* Supports running functions with up to 4 arguments. +* fault_vector: exception vector that might get thrown during the function. +* raised_vector: outputs true if exception occurred. +* +* returns: return value returned by function, or 0 if an exception occurred. +*/ +uint64_t run_in_user (usermode_func func,unsigned int fault_vector, +uint64_t arg1,uint64_t arg2,uint64_t arg3, +uint64_t arg4,bool *raised_vector); + +#endif
x86/Makefile.common +1 -0
@@ -19,6 +19,7 @@ cflatobjs += lib/x86/desc.o cflatobjs += lib/x86/isr.o cflatobjs += lib/x86/acpi.o cflatobjs += lib/x86/stack.o +cflatobjs += lib/x86/usermode.o OBJDIRS += lib/x86
Signature · 1 line
2.14.1
PATCH 2/5 x86: Add Test Utility to run in User Mode and catch exceptions
In reply to Arbel MosheAdd a test utility that enables:
1. Running test function in User Mode or Kernel Mode.
2. Catching an exception.
3. Running a callback function to test return val.
--- lib/x86/fault_test.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/x86/fault_test.h | 42 ++++++++++++++++++++++++++++++++++++++++++ x86/Makefile.common | 1 + 3 files changed, 95 insertions(+) create mode 100644 lib/x86/fault_test.c create mode 100644 lib/x86/fault_test.h
lib/x86/fault_test.c new file +52 -0
@@ -0,0 +1,52 @@ +#include "fault_test.h" + +jmp_buf jmpbuf; + +static void restore_exec_to_jmpbuf (void ) +{ +longjmp (jmpbuf,1 ); +} + +static void fault_test_fault (struct ex_regs *regs) +{ + regs->rip = (unsigned long )&restore_exec_to_jmpbuf; +} + +static bool fault_test (struct fault_test_arg *arg) +{ +uint64_t val; +bool raised_vector = false; +test_fault_func func = (test_fault_func ) arg->func; +/* Init as success in case there isn't callback */ +bool callback_success = true; + +if (arg->usermode) { + val =run_in_user ((usermode_func ) func, arg->fault_vector, + arg->arg[0 ], arg->arg[1 ], arg->arg[2 ], + arg->arg[3 ], &raised_vector); + }else { +handle_exception (arg->fault_vector, fault_test_fault); +if (setjmp (jmpbuf) ==0 ) + val =func (arg->arg[0 ], arg->arg[1 ], arg->arg[2 ], + arg->arg[3 ]); +else + raised_vector = true; + } + +if (!raised_vector) { + arg->retval = val; +if (arg->callback !=NULL ) + callback_success = arg->callback (arg); + } + +return arg->should_fault ? + raised_vector : (!raised_vector && callback_success); +} + +void test_run (struct fault_test *test) +{ +bool passed =fault_test (&(test->arg)); + +report ("%s" , passed, test->name); +} +
lib/x86/fault_test.h new file +42 -0
42 lines not on this page · raw message
x86/Makefile.common +1 -0
@@ -20,6 +20,7 @@ cflatobjs += lib/x86/isr.o cflatobjs += lib/x86/acpi.o cflatobjs += lib/x86/stack.o cflatobjs += lib/x86/usermode.o +cflatobjs += lib/x86/fault_test.o OBJDIRS += lib/x86
Signature · 1 line
2.14.1
PATCH 3/5 x86: Add Definition for PCE bit in CR4
In reply to Arbel Moshe--- lib/x86/processor.h | 1 + 1 file changed, 1 insertion(+)
lib/x86/processor.h +1 -0
@@ -27,6 +27,7 @@#define X86_CR4_PSE 0x00000010#define X86_CR4_PAE 0x00000020#define X86_CR4_MCE 0x00000040 +#define X86_CR4_PCE 0x00000100#define X86_CR4_VMXE 0x00002000#define X86_CR4_PCIDE 0x00020000#define X86_CR4_SMAP 0x00200000
Signature · 1 line
2.14.1
PATCH 4/5 x86: Add definition for 64 bit Segment descriptor
In reply to Arbel Moshe--- lib/x86/desc.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
lib/x86/desc.h +25 -0
@@ -167,6 +167,31 @@ typedef struct {u8 base_high; } gdt_entry_t; +struct segment_desc64 { +uint16_t limit1; +uint16_t base1; +uint8_t base2; +union { +uint16_t type_limit_flags;/* Type and limit flags */ +struct { +uint16_t type:4 ; +uint16_t s:1 ; +uint16_t dpl:2 ; +uint16_t p:1 ; +uint16_t limit:4 ; +uint16_t avl:1 ; +uint16_t l:1 ; +uint16_t db:1 ; +uint16_t g:1 ; + } __attribute__((__packed__ )); + } __attribute__((__packed__ )); +uint8_t base3; +uint32_t base4; +uint32_t zero; +} __attribute__((__packed__ )); + +#define DESC_BUSY ((uint64_t) 1 << 41) +extern idt_entry_t boot_idt[256 ];#ifndef __x86_64__
Signature · 1 line
2.14.1
PATCH 5/5 x86: Add VMware backdoor unit test
In reply to Arbel MosheAdd VMware backdoors unit test.
VMware backdoors, is a PV interface VMware ESX and Workstation expose to their running VMs, in order to better operate in a virtualized environment.
There are 2 main backdoor mechanisms: I/O ports & Pseduo-PMCs.
VMware backdoor I/O ports: VMware expose a special I/O port which sends/gives ParaVirtualize info. The I/O port is accessible from User Mode, even if the TSS I/O permission bitmap states otherwise.
VMware backdoor Pseduo-PMCs: VMware exposes 3 Pseduo-PMCs to its running VMS, that lets the VMs collect information regarding host time. For more info, see:
www.vmware.com/files/pdf/techpaper/Timekeeping-In-VirtualMachines.pdf
This unit test tests the functionality of these backdoors. They are tested both from User Mode and Kernel Mode, as they will be accessible running on ESX.
--- x86/Makefile.common | 1 + x86/unittests.cfg | 4 ++ x86/vmware_backdoors.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 x86/vmware_backdoors.c
x86/Makefile.common +1 -0
@@ -56,6 +56,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \ $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \ $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \ $(TEST_DIR)/hyperv_connections.flat \ + $(TEST_DIR)/vmware_backdoors.flat\ ifdef API tests-api = api/api-sample api/dirty-log api/dirty-log-perf
x86/unittests.cfg +4 -0
@@ -150,6 +150,10 @@ file = pmu.flat extra_params = -cpu host check = /proc/sys/kernel/nmi_watchdog=0 +[vmware_backdoors] +file = vmware_backdoors.flat +extra_params = -machine vmport=on + [port80] file = port80.flat
x86/vmware_backdoors.c new file +191 -0
191 lines not on this page · raw message
Signature · 1 line
2.14.1
Paolo Bonzini
maintainerQuoted 4 lines · Arbel Moshe
Hi,
This series adds a set of unit tests to series:
"KVM: x86: Add support for VMware backdoor I/O ports & Pseduo-PMCs"
Quoted 9 lines
The series is split into the following patches:
1. Add an x86 utility to run a function in User Mode.
2. Add a test utility to run a function in User Mode / Kernel Mode, catch an exception, and run a callback to test function return value.
3. Add CR4 PCE bit definition.
4. Addx86_64segment descriptor struct.
5. Add VMware backdoor tests:
Test VMware port and VMware RDPMC backdoors, both in user mode and kernel mode.
Looks good, thanks. I'll shortly review the kernel parts too.
Paolo