Add a syscall infrastructure (our baby program can print to the console now!)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-05 22:39:56 +01:00
parent caa3fe8c45
commit fd8a0175d9
Signed by: apio
GPG Key ID: B8A7D06E42258954
9 changed files with 87 additions and 6 deletions

View File

@ -1,4 +1,17 @@
section .text section .text
global _start global _start
_start: _start:
mov rax, 1
mov rdi, message
int 42h int 42h
mov rax, 1
mov rdi, message2
int 42h
mov rax, 0
int 42h
section .rodata
message:
db "Hello!", 0xa, 0
message2:
db "I am an app", 0xa, 0

View File

@ -15,6 +15,9 @@ set(SOURCES
src/thread/Spinlock.cpp src/thread/Spinlock.cpp
src/thread/Thread.cpp src/thread/Thread.cpp
src/thread/Scheduler.cpp src/thread/Scheduler.cpp
src/sys/Syscall.cpp
src/sys/exit.cpp
src/sys/console_print.cpp
src/InitRD.cpp src/InitRD.cpp
src/ELF.cpp src/ELF.cpp
) )
@ -82,4 +85,4 @@ target_include_directories(moon PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/gen)
target_link_options(moon PRIVATE LINKER:-T ${CMAKE_CURRENT_LIST_DIR}/moon.ld -nostdlib -nodefaultlibs) target_link_options(moon PRIVATE LINKER:-T ${CMAKE_CURRENT_LIST_DIR}/moon.ld -nostdlib -nodefaultlibs)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/moon" DESTINATION ${LUNA_ROOT}/initrd/boot) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/moon" DESTINATION ${LUNA_ROOT}/initrd/boot)

View File

@ -188,4 +188,4 @@ ISR_ERROR 21 ; control-protection exception (#CP)
; ISR 22-31 reserved ; ISR 22-31 reserved
IRQ 32, 0 ; timer interrupt IRQ 32, 0 ; timer interrupt
IRQ 33, 0 ; keyboard interrupt IRQ 33, 0 ; keyboard interrupt
ISR 66 ; user exit ISR 66 ; system call

View File

@ -4,6 +4,7 @@
#include "arch/x86_64/CPU.h" #include "arch/x86_64/CPU.h"
#include "arch/x86_64/IO.h" #include "arch/x86_64/IO.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <cpuid.h> #include <cpuid.h>
#include <luna/CString.h> #include <luna/CString.h>
@ -107,11 +108,10 @@ extern "C" void arch_interrupt_entry(Registers* regs)
scancode_queue.try_push(scancode); scancode_queue.try_push(scancode);
pic_eoi(regs); pic_eoi(regs);
} }
else if (regs->isr == 66) // Exit!! else if (regs->isr == 66) // System call
{ {
kdbgln("exiting from user task!!"); SyscallArgs args = { regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9 };
Scheduler::current()->state = ThreadState::Dying; regs->rax = (u64)invoke_syscall(regs, args, regs->rax);
kernel_yield();
} }
else else
{ {

View File

@ -0,0 +1,18 @@
#include "sys/Syscall.h"
#include <luna/SystemError.h>
syscall_func_t syscalls[] = {
#undef __enumerate
#define __enumerate(name) sys_##name,
enumerate_syscalls(__enumerate)
#undef __enumerate
};
i64 invoke_syscall(Registers* regs, SyscallArgs args, u64 syscall)
{
if (syscall >= Syscalls::__count) { return -ENOSYS; }
auto rc = syscalls[syscall](regs, args);
if (rc.has_error()) return -rc.error();
return (i64)rc.value();
}

16
kernel/src/sys/Syscall.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include "arch/CPU.h"
#include <luna/Result.h>
#include <luna/Syscall.h>
typedef u64 SyscallArgs[6];
typedef Result<u64> (*syscall_func_t)(Registers*, SyscallArgs);
// Invoked by the architecture-dependent system call entry point.
i64 invoke_syscall(Registers*, SyscallArgs, u64 syscall);
#undef __enumerate
#define __enumerate(name) extern Result<u64> sys_##name(Registers*, SyscallArgs);
enumerate_syscalls(__enumerate)
#undef __enumerate

View File

@ -0,0 +1,12 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "video/TextConsole.h"
Result<u64> sys_console_print(Registers*, SyscallArgs args)
{
if (!MemoryManager::validate_userspace_string(args[0])) return err(EFAULT);
TextConsole::print((char*)args[0]);
return { 0 };
}

7
kernel/src/sys/exit.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
Result<u64> sys_exit(Registers*, SyscallArgs)
{
kernel_exit();
}

View File

@ -0,0 +1,12 @@
#pragma once
#define enumerate_syscalls(_e) _e(exit) _e(console_print)
enum Syscalls
{
#undef __enumerate
#define __enumerate(name) SYS_##name,
enumerate_syscalls(__enumerate)
#undef __enumerate
__count
};