From 610a71e5df67e1480da7cabf1c5b31f5cd579f44 Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Tue, 18 Oct 2022 12:52:10 +0200 Subject: [PATCH 1/4] syscalls: initial --- src/arch/i686/interrupts/mod.rs | 5 +++ src/arch/i686/interrupts/software.rs | 51 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/arch/i686/interrupts/software.rs diff --git a/src/arch/i686/interrupts/mod.rs b/src/arch/i686/interrupts/mod.rs index 79a7570..439370f 100644 --- a/src/arch/i686/interrupts/mod.rs +++ b/src/arch/i686/interrupts/mod.rs @@ -4,6 +4,7 @@ mod exceptions; pub mod idt; mod irq; mod pic; +mod software; #[inline(always)] pub unsafe fn init() @@ -86,6 +87,10 @@ pub unsafe extern "C" fn interrupt_handler(state: &State) { irq::handler(state); } + 0x80 => + { + software::handler(state); + } _ => { crate::serial_println!("Got unhandled interrupt {:02x}", interrupt); diff --git a/src/arch/i686/interrupts/software.rs b/src/arch/i686/interrupts/software.rs new file mode 100644 index 0000000..72a58a5 --- /dev/null +++ b/src/arch/i686/interrupts/software.rs @@ -0,0 +1,51 @@ +use super::State; + +use core::slice; + +#[derive(Copy, Clone)] +pub struct Syscall +{ + name: &'static str, + pub handler: unsafe fn(u32, u32, u32) +} + +pub static SYSCALLS: [Syscall; 2] = +[ + Syscall {name: "read", handler: sys_dummy}, + Syscall {name: "write", handler: sys_write} +]; + + +pub unsafe fn handler(state: &State) +{ + syscall(state.eax, state.ebx, state.ecx, state.edx); +} + +unsafe fn syscall(syscall_number: u32, arg1: u32, arg2: u32, arg3: u32) +{ + if (syscall_number as usize) < SYSCALLS.len() + { + let syscall = SYSCALLS[syscall_number as usize]; + crate::serial_println!("Called syscall {}({}, {}, {})", syscall.name, arg1, arg2, arg3); + (syscall.handler)(arg1, arg2, arg3); + } + else + { + panic!("Invalid syscall {}({}, {}, {})", syscall_number, arg1, arg2, arg3); + } +} + +unsafe fn sys_dummy(_arg1: u32, _arg2: u32, _arg3: u32) +{ + +} + +unsafe fn sys_write(_file_descriptor: u32, buffer: u32, len: u32) +{ + let len = len as usize; + let buffer = slice::from_raw_parts(buffer as *const u8, len); + for i in 0..len + { + crate::log!("{}", buffer[i] as char); + } +} From 077c9c8526241b36cb392d071d5ccdeeb0a4770a Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Wed, 2 Nov 2022 02:39:53 +0100 Subject: [PATCH 2/4] syscalls: add 0x30 for kfs compliance --- src/arch/i686/interrupts/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arch/i686/interrupts/mod.rs b/src/arch/i686/interrupts/mod.rs index 439370f..8112032 100644 --- a/src/arch/i686/interrupts/mod.rs +++ b/src/arch/i686/interrupts/mod.rs @@ -87,7 +87,7 @@ pub unsafe extern "C" fn interrupt_handler(state: &State) { irq::handler(state); } - 0x80 => + 0x30 | 0x80 => { software::handler(state); } From d406aedea5f84dda90e0619f7e6db79102290314 Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Wed, 2 Nov 2022 02:38:01 +0100 Subject: [PATCH 3/4] syscalls: add syscall function to the ferramenta --- src/ferramenta.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ferramenta.rs b/src/ferramenta.rs index dd7efcf..d12b16b 100644 --- a/src/ferramenta.rs +++ b/src/ferramenta.rs @@ -1,6 +1,8 @@ -use core::slice; use crate::memory; +use core::slice; +use core::arch::asm; + pub fn shutdown_qemu() { crate::arch::port::outw(0x604, 0x2000); @@ -93,6 +95,18 @@ pub unsafe fn print_memory_bin(ptr: *const u8, n: usize) crate::logln!(); } +pub unsafe fn syscall(syscall_number: u32, arg1: u32, arg2: u32, arg3: u32) -> u32 +{ + let ret: u32; + asm!("int 0x80", + in("eax") syscall_number, + in("ebx") arg1, + in("ecx") arg2, + in("edx") arg3, + lateout("eax") ret); + ret +} + #[macro_export] macro_rules! get_reg { From 26394c33a673e85efba7b8ea9ce97d41f3e89ba2 Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Wed, 2 Nov 2022 02:38:31 +0100 Subject: [PATCH 4/4] syscalls: testing --- src/elsos.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/elsos.rs b/src/elsos.rs index 7868654..24378ac 100644 --- a/src/elsos.rs +++ b/src/elsos.rs @@ -55,13 +55,13 @@ pub extern "C" fn kernel_main(magic: u32, address: u32) logln!(" #+# #+# :` .'._.'. `; Willkumme uf elsOS {}.{}.{}{}", VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION); logln!(" ### ######### '-`'.___.'`-' Hello, kernel world !"); logln!(); - tests(); tty::prompt(); unsafe { arch::interrupts::init(); arch::interrupts::enable(); } + tests(); loop { arch::halt(); @@ -72,6 +72,12 @@ pub extern "C" fn kernel_main(magic: u32, address: u32) fn tests() { // put tests here + unsafe + { + let text = [b'H', b'e', b'l', b'l', b'o', b'\n']; + let len = 6; + ferramenta::syscall(1, 0, &text as *const _ as u32, len); + } } fn init_vga()