Add a syscall infrastructure (our baby program can print to the console now!)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
caa3fe8c45
commit
fd8a0175d9
13
apps/app.asm
13
apps/app.asm
@ -1,4 +1,17 @@
|
||||
section .text
|
||||
global _start
|
||||
_start:
|
||||
mov rax, 1
|
||||
mov rdi, message
|
||||
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
|
||||
|
@ -15,6 +15,9 @@ set(SOURCES
|
||||
src/thread/Spinlock.cpp
|
||||
src/thread/Thread.cpp
|
||||
src/thread/Scheduler.cpp
|
||||
src/sys/Syscall.cpp
|
||||
src/sys/exit.cpp
|
||||
src/sys/console_print.cpp
|
||||
src/InitRD.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)
|
||||
|
||||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/moon" DESTINATION ${LUNA_ROOT}/initrd/boot)
|
||||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/moon" DESTINATION ${LUNA_ROOT}/initrd/boot)
|
||||
|
@ -188,4 +188,4 @@ ISR_ERROR 21 ; control-protection exception (#CP)
|
||||
; ISR 22-31 reserved
|
||||
IRQ 32, 0 ; timer interrupt
|
||||
IRQ 33, 0 ; keyboard interrupt
|
||||
ISR 66 ; user exit
|
||||
ISR 66 ; system call
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "arch/x86_64/CPU.h"
|
||||
#include "arch/x86_64/IO.h"
|
||||
#include "memory/MemoryManager.h"
|
||||
#include "sys/Syscall.h"
|
||||
#include "thread/Scheduler.h"
|
||||
#include <cpuid.h>
|
||||
#include <luna/CString.h>
|
||||
@ -107,11 +108,10 @@ extern "C" void arch_interrupt_entry(Registers* regs)
|
||||
scancode_queue.try_push(scancode);
|
||||
pic_eoi(regs);
|
||||
}
|
||||
else if (regs->isr == 66) // Exit!!
|
||||
else if (regs->isr == 66) // System call
|
||||
{
|
||||
kdbgln("exiting from user task!!");
|
||||
Scheduler::current()->state = ThreadState::Dying;
|
||||
kernel_yield();
|
||||
SyscallArgs args = { regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9 };
|
||||
regs->rax = (u64)invoke_syscall(regs, args, regs->rax);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
18
kernel/src/sys/Syscall.cpp
Normal file
18
kernel/src/sys/Syscall.cpp
Normal 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
16
kernel/src/sys/Syscall.h
Normal 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
|
12
kernel/src/sys/console_print.cpp
Normal file
12
kernel/src/sys/console_print.cpp
Normal 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
7
kernel/src/sys/exit.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "sys/Syscall.h"
|
||||
#include "thread/Scheduler.h"
|
||||
|
||||
Result<u64> sys_exit(Registers*, SyscallArgs)
|
||||
{
|
||||
kernel_exit();
|
||||
}
|
12
luna/include/luna/Syscall.h
Normal file
12
luna/include/luna/Syscall.h
Normal 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
|
||||
};
|
Loading…
Reference in New Issue
Block a user