From d02b3d62cb73006c922c82a7fbe464cc1815162a Mon Sep 17 00:00:00 2001 From: xamidev <121681048+xamidev@users.noreply.github.com> Date: Mon, 16 Sep 2024 16:43:59 +0200 Subject: [PATCH] err: invalid syscall no --- src/kernel/irq.c | 4 ++++ src/kernel/loader.s | 31 +++++++++++++++++++++++++++++++ src/kernel/syscall.c | 20 -------------------- src/kernel/syscall.h | 8 -------- src/kernel/syscalls.c | 33 +++++++++++++++++++++++++++++++++ src/kernel/system.h | 2 ++ src/programs/hello.c | 7 ++++--- 7 files changed, 74 insertions(+), 31 deletions(-) delete mode 100644 src/kernel/syscall.c delete mode 100644 src/kernel/syscall.h create mode 100644 src/kernel/syscalls.c diff --git a/src/kernel/irq.c b/src/kernel/irq.c index 05f6f6f..baa117a 100644 --- a/src/kernel/irq.c +++ b/src/kernel/irq.c @@ -25,6 +25,8 @@ extern void irq13(); extern void irq14(); extern void irq15(); +extern void syscall_common_stub(); + void *irq_routines[16] = { 0, 0, 0, 0, 0, 0, 0, 0, @@ -76,6 +78,8 @@ void irq_install() idt_set_gate(46, (unsigned)irq14, 0x08, 0x8E); idt_set_gate(47, (unsigned)irq15, 0x08, 0x8E); printf("[kernel] installed irq 0-15\n"); + + idt_set_gate(0x80, (unsigned long)syscall_common_stub, 0x08, 0x8E); } void irq_handler(struct regs *r) diff --git a/src/kernel/loader.s b/src/kernel/loader.s index 7ae9c0a..1011219 100644 --- a/src/kernel/loader.s +++ b/src/kernel/loader.s @@ -210,6 +210,37 @@ irq_common_stub: add esp, 8 iret +; we'll be placing the syscall_common_stub here. +; push everything, then call syscall_handler (be sure to define it extern) +; then pop back everything and iret +extern syscall_handler + +global syscall_common_stub +syscall_common_stub: + pusha + push ds + push es + push fs + push gs + + mov eax, ds + push eax ; save ds + mov ax, 0x10 ; kernel segment + mov ds, ax + mov es, ax + + call syscall_handler + + pop eax + mov ds, eax ; restore ds + + pop gs + pop fs + pop es + pop ds + popa + iret + section .bss align 4 diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c deleted file mode 100644 index a117fc7..0000000 --- a/src/kernel/syscall.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include "../libc/stdio.h" -#include "syscall.h" - -syscall_t syscalls[] = { - (syscall_t)printf, -}; - -int syscall_handler(int syscall_num, ...) -{ - if ((unsigned)syscall_num < sizeof(syscalls)/sizeof(syscall_t)) - { - va_list args; - va_start(args, syscall_num); - syscalls[syscall_num](va_arg(args, const char*)); - va_end(args); - return 0; - } - return -1; -} diff --git a/src/kernel/syscall.h b/src/kernel/syscall.h deleted file mode 100644 index 4367ba6..0000000 --- a/src/kernel/syscall.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SYSCALL_H -#define SYSCALL_H - -#define SYSCALL_PRINTF 0 - -typedef void (*syscall_t)(); - -#endif diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c new file mode 100644 index 0000000..a2e20dd --- /dev/null +++ b/src/kernel/syscalls.c @@ -0,0 +1,33 @@ +// System calls +// Author: xamidev +// Licensed under the Unlicense. See the repo below. +// https://github.com/xamidev/blankos + +#include "../libc/stdio.h" + +void handle_syscall(int syscall_number, void* arg) +{ + switch(syscall_number) + { + case 1: + puts("Here's the syscall 1\n"); + break; + default: + printf("[error] Invalid syscall number '%d'!\n", syscall_number); + break; + } +} + +void syscall_handler() +{ + int syscall_number; + void* arg; + // mov eax, syscall_number + // mov ebx, arg + asm volatile("mov %%eax, %0" : "=r"(syscall_number)); + asm volatile("mov %%ebx, %0" : "=r"(arg)); + + printf("[syscall] syscall_number=%d, arg=%p\n", syscall_number, arg); + + handle_syscall(syscall_number, arg); +} diff --git a/src/kernel/system.h b/src/kernel/system.h index 240f208..966d72b 100644 --- a/src/kernel/system.h +++ b/src/kernel/system.h @@ -39,5 +39,7 @@ extern volatile unsigned long global_ticks; extern unsigned int g_multiboot_info_address; +void syscall_handler(); + #endif diff --git a/src/programs/hello.c b/src/programs/hello.c index 09df5fa..af3a85f 100644 --- a/src/programs/hello.c +++ b/src/programs/hello.c @@ -1,9 +1,10 @@ -void user_printf(const char* format) { - asm volatile ("int $0x80" : : "a"(1), "b"(format)); +void user_syscall(int syscall_no) { + asm volatile ("mov %0, %%eax" : : "r"(syscall_no)); + asm volatile ("int $0x80"); } void main() { - user_printf("Hello, world, from a PROGRAM!\n"); + user_syscall(1); return; }