xref: /kvm-unit-tests/lib/x86/usermode.c (revision 5b70cbdb7bc2ea65096b51565c75815cc95945b8)
1 #include "x86/msr.h"
2 #include "x86/processor.h"
3 #include "x86/apic-defs.h"
4 #include "x86/apic.h"
5 #include "x86/desc.h"
6 #include "x86/isr.h"
7 #include "alloc.h"
8 #include "setjmp.h"
9 #include "usermode.h"
10 
11 #include "libcflat.h"
12 #include <stdint.h>
13 
14 #define USERMODE_STACK_SIZE	0x2000
15 #define RET_TO_KERNEL_IRQ	0x20
16 
17 static jmp_buf jmpbuf;
18 
19 static void restore_exec_to_jmpbuf(void)
20 {
21 	longjmp(jmpbuf, 1);
22 }
23 
24 static void restore_exec_to_jmpbuf_exception_handler(struct ex_regs *regs)
25 {
26 	/* longjmp must happen after iret, so do not do it now.  */
27 	regs->rip = (unsigned long)&restore_exec_to_jmpbuf;
28 	regs->cs = KERNEL_CS;
29 }
30 
31 uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
32 		uint64_t arg1, uint64_t arg2, uint64_t arg3,
33 		uint64_t arg4, bool *raised_vector)
34 {
35 	extern char ret_to_kernel;
36 	uint64_t rax = 0;
37 	static unsigned char user_stack[USERMODE_STACK_SIZE];
38 
39 	*raised_vector = 0;
40 	set_idt_entry(RET_TO_KERNEL_IRQ, &ret_to_kernel, 3);
41 	handle_exception(fault_vector,
42 			restore_exec_to_jmpbuf_exception_handler);
43 
44 	if (setjmp(jmpbuf) != 0) {
45 		*raised_vector = 1;
46 		return 0;
47 	}
48 
49 	asm volatile (
50 			/* Backing Up Stack in rdi */
51 			"mov %%rsp, %%rdi\n\t"
52 			/* Load user_ds to DS and ES */
53 			"mov %[user_ds], %%ax\n\t"
54 			"mov %%ax, %%ds\n\t"
55 			"mov %%ax, %%es\n\t"
56 			/* IRET into user mode */
57 			"pushq %[user_ds]\n\t"
58 			"pushq %[user_stack_top]\n\t"
59 			"pushfq\n\t"
60 			"pushq %[user_cs]\n\t"
61 			"pushq $user_mode\n\t"
62 			"iretq\n"
63 
64 			"user_mode:\n\t"
65 			/* Back up registers before invoking func */
66 			"push %%rbx\n\t"
67 			"push %%rcx\n\t"
68 			"push %%rdx\n\t"
69 			"push %%r8\n\t"
70 			"push %%r9\n\t"
71 			"push %%r10\n\t"
72 			"push %%r11\n\t"
73 			"push %%rdi\n\t"
74 			"push %%rsi\n\t"
75 			/* Call user mode function */
76 			"mov %[arg1], %%rdi\n\t"
77 			"mov %[arg2], %%rsi\n\t"
78 			"mov %[arg3], %%rdx\n\t"
79 			"mov %[arg4], %%rcx\n\t"
80 			"call *%[func]\n\t"
81 			/* Restore registers */
82 			"pop %%rsi\n\t"
83 			"pop %%rdi\n\t"
84 			"pop %%r11\n\t"
85 			"pop %%r10\n\t"
86 			"pop %%r9\n\t"
87 			"pop %%r8\n\t"
88 			"pop %%rdx\n\t"
89 			"pop %%rcx\n\t"
90 			"pop %%rbx\n\t"
91 			/* Return to kernel via system call */
92 			"int %[kernel_entry_vector]\n\t"
93 			/* Kernel Mode */
94 			"ret_to_kernel:\n\t"
95 			"mov %%rdi, %%rsp\n\t"
96 			:
97 			"+a"(rax)
98 			:
99 			[arg1]"m"(arg1),
100 			[arg2]"m"(arg2),
101 			[arg3]"m"(arg3),
102 			[arg4]"m"(arg4),
103 			[func]"m"(func),
104 			[user_ds]"i"(USER_DS),
105 			[user_cs]"i"(USER_CS),
106 			[user_stack_top]"r"(user_stack +
107 					sizeof(user_stack)),
108 			[kernel_entry_vector]"i"(RET_TO_KERNEL_IRQ)
109 			:
110 			"rsi", "rdi", "rcx", "rdx");
111 
112 	return rax;
113 }
114