All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] x86: replace `push` `pop` with callee-clobbered list
@ 2022-04-24  7:02 SU Hang
  2022-04-24  7:02 ` [PATCH 2/2] x86: replace `int 0x20` with `syscall` SU Hang
  0 siblings, 1 reply; 2+ messages in thread
From: SU Hang @ 2022-04-24  7:02 UTC (permalink / raw)
  To: kvm, pbonzini, thuth, drjones, SU Hang

Stopping mess up asm callee-clobbered list with `push` `pop`,
clean up code to make it more readable.

Signed-off-by: SU Hang <darcy.sh@antgroup.com>
---
 lib/x86/usermode.c | 22 +---------------------
 1 file changed, 1 insertion(+), 21 deletions(-)

diff --git a/lib/x86/usermode.c b/lib/x86/usermode.c
index e22fb8f..477cb9f 100644
--- a/lib/x86/usermode.c
+++ b/lib/x86/usermode.c
@@ -66,32 +66,12 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
 			"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 */
@@ -112,7 +92,7 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
 					sizeof(user_stack)),
 			[kernel_entry_vector]"i"(RET_TO_KERNEL_IRQ)
 			:
-			"rsi", "rdi", "rcx", "rdx");
+			"rsi", "rdi", "rbx", "rcx", "rdx", "r8", "r9", "r10", "r11");
 
 	return rax;
 }
-- 
2.32.0.3.g01195cf9f


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] x86: replace `int 0x20` with `syscall`
  2022-04-24  7:02 [PATCH 1/2] x86: replace `push` `pop` with callee-clobbered list SU Hang
@ 2022-04-24  7:02 ` SU Hang
  0 siblings, 0 replies; 2+ messages in thread
From: SU Hang @ 2022-04-24  7:02 UTC (permalink / raw)
  To: kvm, pbonzini, thuth, drjones, SU Hang

Signed-off-by: SU Hang <darcy.sh@antgroup.com>
---
 lib/x86/usermode.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/x86/usermode.c b/lib/x86/usermode.c
index 477cb9f..e4cb899 100644
--- a/lib/x86/usermode.c
+++ b/lib/x86/usermode.c
@@ -12,7 +12,6 @@
 #include <stdint.h>
 
 #define USERMODE_STACK_SIZE	0x2000
-#define RET_TO_KERNEL_IRQ	0x20
 
 static jmp_buf jmpbuf;
 
@@ -40,9 +39,11 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
 	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);
+	wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_SCE);
+	wrmsr(MSR_STAR, ((u64)(USER_CS32 << 16) | KERNEL_CS) << 32);
+	wrmsr(MSR_LSTAR, (u64)&ret_to_kernel);
 
 	if (setjmp(jmpbuf) != 0) {
 		*raised_vector = 1;
@@ -73,7 +74,7 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
 			"mov %[arg4], %%rcx\n\t"
 			"call *%[func]\n\t"
 			/* Return to kernel via system call */
-			"int %[kernel_entry_vector]\n\t"
+			"syscall\n\t"
 			/* Kernel Mode */
 			"ret_to_kernel:\n\t"
 			"mov %[rsp0], %%rsp\n\t"
@@ -89,8 +90,7 @@ uint64_t run_in_user(usermode_func func, unsigned int fault_vector,
 			[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)
+					sizeof(user_stack))
 			:
 			"rsi", "rdi", "rbx", "rcx", "rdx", "r8", "r9", "r10", "r11");
 
-- 
2.32.0.3.g01195cf9f


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-24  7:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-24  7:02 [PATCH 1/2] x86: replace `push` `pop` with callee-clobbered list SU Hang
2022-04-24  7:02 ` [PATCH 2/2] x86: replace `int 0x20` with `syscall` SU Hang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.