From 84e4c1c51c5f91301518d5ad07e66d7375108e2f Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 17 Feb 2023 15:18:12 +0100 Subject: [PATCH] feature (kernel): Implemented syscall architecture --- kernel/inc/platform/syscall.h | 36 +++++++++++++++++++++++ kernel/src/platform/interrupts.c | 7 ++++- kernel/src/platform/syscall.c | 45 +++++++++++++++++++++++++++++ kernel/src/platform/x86/syscall.asm | 5 ++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 kernel/inc/platform/syscall.h create mode 100644 kernel/src/platform/syscall.c create mode 100644 kernel/src/platform/x86/syscall.asm diff --git a/kernel/inc/platform/syscall.h b/kernel/inc/platform/syscall.h new file mode 100644 index 0000000..0b6f00a --- /dev/null +++ b/kernel/inc/platform/syscall.h @@ -0,0 +1,36 @@ +/* Copyright (C) Antifallobst + * + * NoxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#ifndef NOX_SYSCALL_H +#define NOX_SYSCALL_H + +#include "platform/cpu.h" + +typedef enum { + SYSCALLS_MISC = 0x00, + SYSCALLS_FILE = 0x01, + SYSCALLS_PROC = 0x02, + SYSCALLS_KERNEL = 0xFF, +}syscall_group_E; + +typedef enum { + SYSCALL_KERNEL_START_SCHEDULER = 0xFF00 +} syscall_E; + +extern void syscall_perform(syscall_E id); + +cpu_state_T* syscall_handle(cpu_state_T* state); + +#endif //NOX_SYSCALL_H diff --git a/kernel/src/platform/interrupts.c b/kernel/src/platform/interrupts.c index b811375..146e4f7 100644 --- a/kernel/src/platform/interrupts.c +++ b/kernel/src/platform/interrupts.c @@ -17,6 +17,7 @@ #include "platform/cpu.h" #include "platform/gdt.h" #include "platform/exceptions.h" +#include "platform/syscall.h" #include "mm/page_frame.h" #include "utils/logger.h" #include "utils/io.h" @@ -231,7 +232,7 @@ cpu_state_T* irq_handle(cpu_state_T* state, pic_irq_E irq) { switch (irq) { case IRQ_PIT: { - log(LOG_DEBUG, "PIT -> Tick"); +// log(LOG_DEBUG, "PIT -> Tick"); break; } default: { @@ -253,6 +254,10 @@ cpu_state_T* interrupts_handle(cpu_state_T* state) { return irq_handle(state, state->interrupt_id - IRQ_MASTER_OFFSET); } + if (state->interrupt_id == 0x80) { + return syscall_handle(state); + } + log(LOG_WARNING, "Unhandled interrupt: 0x%xb", state->interrupt_id); return state; diff --git a/kernel/src/platform/syscall.c b/kernel/src/platform/syscall.c new file mode 100644 index 0000000..fa8dc9b --- /dev/null +++ b/kernel/src/platform/syscall.c @@ -0,0 +1,45 @@ +/* Copyright (C) Antifallobst + * + * NoxOS is free software: + * you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. + * If not, see . + */ + +#include "platform/syscall.h" +#include "utils/logger.h" +#include "proc/scheduler.h" + +cpu_state_T* syscall_handle(cpu_state_T* state) { + cpu_state_T* return_state = state; + syscall_group_E group_id = (state->rax & 0xFF00) >> 8; + + if (group_id == SYSCALLS_KERNEL && + scheduler_is_initialized() && + scheduler_get_current_process() != PROC_KERNEL) + { + log(LOG_WARNING, "non kernel process[%d:%d] tried to perform a kernel syscall (permission denied)", scheduler_get_current_process(), scheduler_get_current_thread()); + return return_state; + } + + switch (state->rax) { + case SYSCALL_KERNEL_START_SCHEDULER: { + scheduler_start(state); + break; + } + + default: { + log(LOG_WARNING, "Unhandled syscall: 0x%xw RAX[0x%x]", state->rax, state->rax); + break; + } + } + + return return_state; +} \ No newline at end of file diff --git a/kernel/src/platform/x86/syscall.asm b/kernel/src/platform/x86/syscall.asm new file mode 100644 index 0000000..3f6016a --- /dev/null +++ b/kernel/src/platform/x86/syscall.asm @@ -0,0 +1,5 @@ +syscall_perform: + mov rax, rdi + int 0x80 + ret +GLOBAL syscall_perform \ No newline at end of file