From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Sun, 6 Aug 2023 00:35:21 +0000 (+0200)
Subject: Implemented yield syscall and added uses of it. Not tested well yet
X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=59f05a9470514abeb7b4f14c8ccee64be23a47e3;p=voyage-au-centre-des-fichiers.git

Implemented yield syscall and added uses of it. Not tested well yet
---

diff --git a/kernel/src/ring3.S b/kernel/src/ring3.S
index 3019544..43c8192 100644
--- a/kernel/src/ring3.S
+++ b/kernel/src/ring3.S
@@ -93,6 +93,7 @@ incorrect_syscall:
 syscalls_call_table:
 	.quad syscall_print
 	.quad syscall_print_low_mmap
+	.quad syscall_yield
 syscalls_call_table_end:
 .set syscall_n, (syscalls_call_table_end - syscalls_call_table) >> 3 # Because / 8 *doesn't work* !
 .globl syscall_n
diff --git a/kernel/src/ring3.cpp b/kernel/src/ring3.cpp
index 286a5bf..b173ac4 100644
--- a/kernel/src/ring3.cpp
+++ b/kernel/src/ring3.cpp
@@ -60,6 +60,14 @@ extern "C" void os::syscall_print_low_mmap() {
 	});
 }
 
+extern "C" void os::syscall_yield() {
+	if (os::processes.present(os::current_pid + 1)) {
+		os::current_pid += 1;
+	} else {
+		os::current_pid = 0;
+	}
+}
+
 extern "C" void os::syscall_rax_error_handler() {
 	os::assert(false, "Incorrect %rax for syscall.");
 }
diff --git a/kernel/src/ring3.hpp b/kernel/src/ring3.hpp
index 66fa287..a66f902 100644
--- a/kernel/src/ring3.hpp
+++ b/kernel/src/ring3.hpp
@@ -46,6 +46,7 @@ extern "C" void syscall_64bit_handler();
 
 extern "C" void syscall_print(char c);
 extern "C" void syscall_print_low_mmap();
+extern "C" void syscall_yield();
 extern "C" void syscall_rax_error_handler();
 
 void set_ring0_stack(tss& tss, std::uint64_t stack);
diff --git a/test_module/src/test.S b/test_module/src/test.S
index be3f7b9..348de63 100644
--- a/test_module/src/test.S
+++ b/test_module/src/test.S
@@ -24,3 +24,9 @@ check_mem:
 	mov $1, %rax
 	syscall
 	ret
+
+.globl yield
+yield:
+	mov $2, %rax
+	syscall
+	ret
diff --git a/test_module/src/test.cpp b/test_module/src/test.cpp
index 4f6294d..bf45ddf 100644
--- a/test_module/src/test.cpp
+++ b/test_module/src/test.cpp
@@ -15,6 +15,7 @@
 
 extern "C" void print(char c);
 extern "C" void check_mem();
+extern "C" void yield();
 
 void printstr(const char* str) {
 	for (size_t i = 0; str[i] != '\0'; i++) {
@@ -23,12 +24,15 @@ void printstr(const char* str) {
 }
 
 extern "C" void _start() {
-	const char* str = "ACAB cependant.\n";
-	for (size_t i = 0; i < 16; i++) {
-		printstr(str);
-	}
-	printstr("Mem:\n");
+	printstr("Mem prg 1:\n");
 	check_mem();
 	printstr("Done.\n");
-	while (true) {}
+	printstr("Prg 1.\n");
+	yield();
+	printstr("Prg 1 again.\n");
+
+	while (true) {
+		yield();
+		printstr("Prg 1 again.\n");
+	}
 }