exit syscall

This commit is contained in:
2024-03-24 10:22:42 +01:00
parent 665de81587
commit aadc8148c1
12 changed files with 46 additions and 5 deletions

View File

@@ -35,7 +35,7 @@ real_start() {
// Set up the address space for the kernel and prepare other structures to work without the bootloader,
// then call real_start with this address space and the new stack.
extern "C" void _start(void) {
extern "C" __attribute__((unused)) void _start(void) {
_sse_setup();
barrier();
Arch::GDT::gdt_setup();

View File

@@ -16,6 +16,7 @@
#include "FDT.hpp"
#include "File.hpp"
#include "memman.hpp"
#include "task.hpp"
#include "timer.hpp"
// Don't forget the correct order
@@ -51,6 +52,10 @@ void setup_syscalls() {
wrmsr(0xC0000080, rdmsr(0xC0000080) | 0b1);
}
void syscall_exit() {
Scheduler::remove_self();
}
uint64_t syscall_putchar(char c) {
GlobalTtyManager.all_tty_putchar(c);
return 0;
@@ -132,6 +137,7 @@ uint64_t syscall_print_tasks() {
return 0;
}
uint64_t syscall_print_mem() {
String buf;
buf += "=====\n";
@@ -159,6 +165,9 @@ uint64_t syscall_print_mem() {
extern "C" uint64_t syscall_impl(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_rdx, uint64_t a3_rcx) {
assert2(are_interrupts_enabled(), "why wouldn't they be?");
switch (id_rdi) {
case SYSCALL_EXIT_ID:
syscall_exit();
return 0;
case SYSCALL_PUTCHAR_ID:
return syscall_putchar(a1_rsi);
case SYSCALL_SLEEP_ID:

View File

@@ -67,7 +67,7 @@ cgistd::priority_queue<List<Task *>::Node *, cgistd::vector<List<Task *>::Node *
static std::atomic<bool> initialized = false;
//
static void remove_self() {
void Scheduler::remove_self() {
assert(RunningTask != nullptr);
{
LockGuard l(TasksToFree_lock);
@@ -86,7 +86,7 @@ static void remove_self() {
static void trampoline(void *rdi, void (*rsi_entrypoint)()) {
rsi_entrypoint();
remove_self();
Scheduler::remove_self();
}
Task::Task(Task::TaskMode mode, void (*entrypoint)(), const char *name) {

View File

@@ -89,6 +89,8 @@ namespace Scheduler {
void sleep_self(uint64_t diff);
void remove_self();
void self_block();
void self_block(Spinlock &to_unlock);
void self_block(Mutex &to_unlock);

View File

@@ -5,11 +5,13 @@ add_custom_target(iso
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/limine.cfg ${CMAKE_CURRENT_BINARY_DIR}/isodir/
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:kernel.elf> ${CMAKE_CURRENT_BINARY_DIR}/isodir/os2.elf
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:hello> ${CMAKE_CURRENT_BINARY_DIR}/isodir/hello
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:hello2> ${CMAKE_CURRENT_BINARY_DIR}/isodir/hello2
COMMAND xorriso -as mkisofs -b limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table --efi-boot limine-uefi-cd.bin -efi-boot-part --efi-boot-image --protective-msdos-label ${CMAKE_CURRENT_BINARY_DIR}/isodir -o ${CMAKE_CURRENT_BINARY_DIR}/os2.iso
COMMAND ${tools}/limine/prefix/bin/limine bios-install ${CMAKE_CURRENT_BINARY_DIR}/os2.iso
VERBATIM
DEPENDS kernel.elf
DEPENDS hello
DEPENDS hello2
DEPENDS iso_limine
)

View File

@@ -10,7 +10,8 @@ TIMEOUT=1
KERNEL_PATH=boot:///os2.elf
MODULE_PATH=boot:///hello
MODULE_PATH=boot:///hello2
# Same thing, but without KASLR.
:os2 (KASLR off)
PROTOCOL=limine
@@ -20,3 +21,4 @@ TIMEOUT=1
KERNEL_PATH=boot:///os2.elf
MODULE_PATH=boot:///hello
MODULE_PATH=boot:///hello2

View File

@@ -7,3 +7,12 @@ target_include_directories(hello PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_options(hello PRIVATE "SHELL:-T${CMAKE_CURRENT_SOURCE_DIR}/linker.ld")
set_target_properties(hello PROPERTIES LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld")
add_executable(hello2 hello2.c)
target_link_libraries(hello2 syscalls_interface)
target_include_directories(hello2 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_options(hello2 PRIVATE "SHELL:-T${CMAKE_CURRENT_SOURCE_DIR}/linker.ld")
set_target_properties(hello2 PROPERTIES LINK_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld")

View File

@@ -1,7 +1,7 @@
#include "syscalls_interface.h"
void _start() {
__attribute__((unused)) void _start() {
// putchar('h');
// putchar('i');
// putchar('\n');

9
src/test/hello2.c Normal file
View File

@@ -0,0 +1,9 @@
#include "syscalls_interface.h"
__attribute__((unused)) void _start() {
putchar('h');
putchar('i');
putchar('\n');
exit();
}

View File

@@ -14,6 +14,8 @@ extern "C" {
#include "FileOpts.h"
#define SYSCALL_EXIT_ID 0
#define SYSCALL_PUTCHAR_ID 1
#define SYSCALL_SLEEP_ID 2
#define SYSCALL_READCHAR_ID 3

View File

@@ -16,6 +16,10 @@ uint64_t do_syscall(uint64_t id_rdi, uint64_t a1_rsi, uint64_t a2_rdx, uint64_t
return res;
}
void exit() {
do_syscall(SYSCALL_EXIT_ID, 0, 0, 0);
}
uint64_t readchar() {
return do_syscall(SYSCALL_READCHAR_ID, 0, 0, 0);
}

View File

@@ -16,6 +16,8 @@ extern "C" {
#include "FileOpts.h"
void exit();
uint64_t putchar(char c);
uint64_t readchar();
uint64_t sleep(uint64_t micros);