Skip to content
This repository was archived by the owner on Aug 12, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions kernel/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,35 @@
//! `sys_` then the name of the syscall. You can find functions like this in
//! submodules, and you should also implement syscalls this way.

const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETPID: usize = 172;
const SYSCALL_FORK: usize = 220;
const SYSCALL_EXEC: usize = 221;
const SYSCALL_WAITPID: usize = 260;

mod fs;
mod process;

use fs::sys_write;
use process::{sys_exit, sys_get_time, sys_yield};

use crate::syscall::process::{sys_exec, sys_fork, sys_getpid, sys_read, sys_waitpid};

pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
match syscall_id {
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
SYSCALL_EXIT => sys_exit(args[0] as i32),
SYSCALL_YIELD => sys_yield(),
SYSCALL_GET_TIME => sys_get_time(),
SYSCALL_GETPID => sys_getpid(),
SYSCALL_FORK => sys_fork(),
SYSCALL_EXEC => sys_exec(args[0] as *const u8),
SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
_ => panic!("Unsupported syscall_id: {}", syscall_id),
}
}
20 changes: 20 additions & 0 deletions kernel/src/syscall/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@ pub fn sys_yield() -> isize {
pub fn sys_get_time() -> isize {
get_time_ms() as isize
}

pub fn sys_fork() -> isize {
unimplemented!()
}

pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
unimplemented!()
}

pub fn sys_exec(path: *const u8) -> isize {
unimplemented!()
}

pub fn sys_getpid() -> isize {
unimplemented!()
}

pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
unimplemented!()
}
Empty file added kernel/src/task/processor.rs
Empty file.
30 changes: 29 additions & 1 deletion user_lib/src/syscall.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use core::arch::asm;

const SYSCALL_READ: usize = 63;
const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETPID: usize = 172;
const SYSCALL_FORK: usize = 220;
const SYSCALL_EXEC: usize = 221;
const SYSCALL_WAITPID: usize = 260;

fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
Expand All @@ -19,6 +24,13 @@ fn syscall(id: usize, args: [usize; 3]) -> isize {
ret
}

pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
syscall(
SYSCALL_READ,
[fd, buffer.as_mut_ptr() as usize, buffer.len()],
)
}

pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
Expand All @@ -34,3 +46,19 @@ pub fn sys_get_time() -> isize {
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}

pub fn sys_getpid() -> isize {
syscall(SYSCALL_GETPID, [0, 0, 0])
}

pub fn sys_fork() -> isize {
syscall(SYSCALL_FORK, [0, 0, 0])
}

pub fn sys_exec(path: &str) -> isize {
syscall(SYSCALL_EXEC, [path.as_ptr() as usize, 0, 0])
}

pub fn sys_waitpid(pid: isize, exit_code: *mut i32) -> isize {
syscall(SYSCALL_WAITPID, [pid as usize, exit_code as usize, 0])
}
Loading