From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Sat, 17 Jun 2023 01:54:09 +0000 (+0200)
Subject: Removed dependency on correct user stack for syscall. Simplified code slightly as... 
X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=793fa8a7938fa17aebc69cf2eb0996ead0c37332;p=voyage-au-centre-des-fichiers.git

Removed dependency on correct user stack for syscall. Simplified code slightly as well
---

diff --git a/kernel/src/boot.S b/kernel/src/boot.S
index f595ff9..7bfad4e 100644
--- a/kernel/src/boot.S
+++ b/kernel/src/boot.S
@@ -128,6 +128,7 @@ phys_mem_map: .skip 0x1000 * 128 - 8
 .align 16
 stack_bottom:
 .skip 4096 * 4 # 16KiB
+.globl stack_top
 stack_top:
 interrupt_stack_bottom:
 .skip 4096 * 4 # 16KiB
diff --git a/kernel/src/ring3.S b/kernel/src/ring3.S
index a3a0e76..dd23354 100644
--- a/kernel/src/ring3.S
+++ b/kernel/src/ring3.S
@@ -37,30 +37,30 @@ load_tss:
 
 .globl syscall_64bit_handler
 syscall_64bit_handler:
-	push %rax
+	mov %rax, rax_save
 	movq current_pid, %rax
 	shl $17, %rax # * 0x1000 * 32 (i.e. the size of a process)
 	addq process_struct_table, %rax
 	# %rax now contains the address of the current process struct.
 	movq %rcx, 0x10(%rax) # %rip
 	movq %rbx, 0x18(%rax)
-	movq %rsp, 0x20(%rax) # original %rsp
-	addq $8, 0x20(%rax) # here as well
+	movq %rsp, 0x20(%rax)
 	movq %rbp, 0x28(%rax)
 	movq %r12, 0x30(%rax)
 	movq %r13, 0x38(%rax)
 	movq %r14, 0x40(%rax)
 	movq %r15, 0x48(%rax)
 	fstcw 0x58(%rax)
-	# Current process registers have now all been updated.
-	pop %rax
+	# Current process registers have now all been saved.
+	mov rax_save, %rax
 
 	mov %r10, %rcx
 	cmp $syscall_n, %rax
+	mov $stack_top, %rsp # Setup stack
 	jae incorrect_syscall
 	callq *syscalls_call_table(, %rax, 8)
 syscall_end:
-	movq %rax, %rbp
+	mov %rax, rax_save
 	movq current_pid, %rax
 	shl $17, %rax # * 0x1000 * 32 (i.e. the size of a process)
 	addq process_struct_table, %rax
@@ -68,15 +68,14 @@ syscall_end:
 	movq 0x10(%rax), %rcx # %rip
 	movq 0x18(%rax), %rbx
 	movq 0x20(%rax), %rsp
-	push %rbp # %rax
 	movq 0x28(%rax), %rbp
 	movq 0x30(%rax), %r12
 	movq 0x38(%rax), %r13
 	movq 0x40(%rax), %r14
 	movq 0x48(%rax), %r15
 	fldcw 0x58(%rax)
-	# Current process registers have now all been updated.
-	pop %rax
+	# Current process registers have now all been loaded.
+	mov rax_save, %rax
 	sysretq
 incorrect_syscall:
 	call syscall_rax_error_handler
@@ -89,3 +88,6 @@ syscalls_call_table:
 	.quad syscall_print_low_mmap
 .set syscall_n, 3
 process_struct_table: .quad 0xFFFFC00000000000
+
+.section .data
+rax_save: .quad 1