From 7cdfb9e6e20552fac8fa54f2407ec9009b25103d Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sun, 13 May 2018 03:41:41 +0800 Subject: [PATCH] Basic syscall --- src/arch/x86_64/driver/apic/ioapic.rs | 2 +- src/arch/x86_64/driver/pic.rs | 2 +- src/arch/x86_64/driver/pit.rs | 2 +- src/arch/x86_64/driver/serial.rs | 2 +- src/arch/x86_64/interrupt/consts.rs | 3 +- src/arch/x86_64/interrupt/handler.rs | 4 ++- src/io/mod.rs | 8 +++++ src/lib.rs | 3 +- src/process/mod.rs | 4 +++ src/syscall.rs | 49 +++++++++++++++++++++++++++ 10 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/syscall.rs diff --git a/src/arch/x86_64/driver/apic/ioapic.rs b/src/arch/x86_64/driver/apic/ioapic.rs index 91f9797..1f67bcb 100644 --- a/src/arch/x86_64/driver/apic/ioapic.rs +++ b/src/arch/x86_64/driver/apic/ioapic.rs @@ -4,7 +4,7 @@ /// http://www.intel.com/design/chipsets/datashts/29056601.pdf /// See also picirq.c. -use syscall::io::{Io, Mmio}; +use redox_syscall::io::{Io, Mmio}; use bit_field::BitField; use arch::interrupt::consts::T_IRQ0; use spin::Mutex; diff --git a/src/arch/x86_64/driver/pic.rs b/src/arch/x86_64/driver/pic.rs index f597cdf..33a1187 100644 --- a/src/arch/x86_64/driver/pic.rs +++ b/src/arch/x86_64/driver/pic.rs @@ -1,6 +1,6 @@ // Copy from Redox -use syscall::io::*; +use redox_syscall::io::*; use spin::Mutex; static MASTER: Mutex = Mutex::new(Pic::new(0x20)); diff --git a/src/arch/x86_64/driver/pit.rs b/src/arch/x86_64/driver/pit.rs index c151095..87d1b7f 100644 --- a/src/arch/x86_64/driver/pit.rs +++ b/src/arch/x86_64/driver/pit.rs @@ -1,4 +1,4 @@ -use syscall::io::{Io, Pio}; +use redox_syscall::io::{Io, Pio}; static mut PIT: Pit = Pit::new(0x40); diff --git a/src/arch/x86_64/driver/serial.rs b/src/arch/x86_64/driver/serial.rs index 2e98289..66c6e22 100644 --- a/src/arch/x86_64/driver/serial.rs +++ b/src/arch/x86_64/driver/serial.rs @@ -2,7 +2,7 @@ use core::fmt::{self, Write}; use spin::Mutex; -use syscall::io::{Io, Pio, Mmio, ReadOnly}; +use redox_syscall::io::{Io, Pio, Mmio, ReadOnly}; pub static COM1: Mutex = Mutex::new(Serial::new(0x3F8)); pub static COM2: Mutex = Mutex::new(Serial::new(0x2F8)); diff --git a/src/arch/x86_64/interrupt/consts.rs b/src/arch/x86_64/interrupt/consts.rs index ba9cc0b..a43869f 100644 --- a/src/arch/x86_64/interrupt/consts.rs +++ b/src/arch/x86_64/interrupt/consts.rs @@ -26,6 +26,7 @@ pub const IRQ_COM1 : u8 = 4; pub const IRQ_IDE : u8 = 14; pub const IRQ_ERROR : u8 = 19; pub const IRQ_SPURIOUS : u8 = 31; -pub const T_SYSCALL : u8 = 0x80; // SYSCALL, ONLY FOR THIS PROJ +pub const T_SYSCALL: u8 = 0x40; +// SYSCALL, ONLY FOR THIS PROJ pub const T_SWITCH_TOU : u8 = 120; // user/kernel switch pub const T_SWITCH_TOK : u8 = 121; // user/kernel switch \ No newline at end of file diff --git a/src/arch/x86_64/interrupt/handler.rs b/src/arch/x86_64/interrupt/handler.rs index bbab556..8ce8e94 100644 --- a/src/arch/x86_64/interrupt/handler.rs +++ b/src/arch/x86_64/interrupt/handler.rs @@ -101,5 +101,7 @@ interrupt_stack_p!(to_kernel, stack, { }); interrupt_stack_p!(syscall, stack, { - println!("\nInterupt: Syscall"); + println!("\nInterupt: Syscall {}", stack.scratch.rax); + use syscall::syscall; + syscall(stack); }); \ No newline at end of file diff --git a/src/io/mod.rs b/src/io/mod.rs index 8e68d5c..0ce8438 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -47,4 +47,12 @@ pub fn print(args: fmt::Arguments) { pub fn debug(args: fmt::Arguments) { print_in_color(args, Color::LightRed); +} + +pub fn write(fd: usize, base: *const u8, len: usize) { + // debug!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len); + use core::slice; + use core::str; + let slice = unsafe { slice::from_raw_parts(base, len) }; + print!("{}", str::from_utf8(slice).unwrap()); } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a96ae77..85c60ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ extern crate linked_list_allocator; #[macro_use] extern crate lazy_static; extern crate bit_field; -extern crate syscall; +extern crate syscall as redox_syscall; extern crate xmas_elf; #[macro_use] // print! @@ -43,6 +43,7 @@ mod util; mod macros; mod consts; mod process; +mod syscall; #[allow(dead_code)] #[cfg(target_arch = "x86_64")] diff --git a/src/process/mod.rs b/src/process/mod.rs index 91c90c1..75e8867 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -68,4 +68,8 @@ extern fn idle_thread() { i += 1; } } +} + +pub fn fork(tf: &TrapFrame) { + unimplemented!() } \ No newline at end of file diff --git a/src/syscall.rs b/src/syscall.rs new file mode 100644 index 0000000..7571d76 --- /dev/null +++ b/src/syscall.rs @@ -0,0 +1,49 @@ +use super::*; +use process; +use arch::interrupt::TrapFrame; + +pub unsafe fn syscall(tf: &TrapFrame) -> i32 { + let id = tf.scratch.rax; + // For ucore: +// let args = [tf.scratch.rdx, tf.scratch.rcx, tf.preserved.rbx, tf.scratch.rdi, tf.scratch.rsi]; + // For xv6 x86_64 + let args = [tf.scratch.rdi, tf.scratch.rsi, tf.scratch.rdx, tf.scratch.rcx, tf.scratch.r8, tf.scratch.r9]; + + match id { + SYS_FORK => { + process::fork(tf); + 0 + } + SYS_WRITE => { + io::write(args[0], args[1] as *const u8, args[2]); + 0 + } + _ => { + debug!("unknown syscall {}", id); + -1 + } + } +} + +const SYS_FORK: usize = 1; +const SYS_EXIT: usize = 2; +const SYS_WAIT: usize = 3; +const SYS_PIPE: usize = 4; +const SYS_READ: usize = 5; +const SYS_KILL: usize = 6; +const SYS_EXEC: usize = 7; +const SYS_FSTAT: usize = 8; +const SYS_CHDIR: usize = 9; +const SYS_DUP: usize = 10; +const SYS_GETPID: usize = 11; +const SYS_SBRK: usize = 12; +const SYS_SLEEP: usize = 13; +const SYS_UPTIME: usize = 14; +const SYS_OPEN: usize = 15; +const SYS_WRITE: usize = 16; +const SYS_MKNOD: usize = 17; +const SYS_UNLINK: usize = 18; +const SYS_LINK: usize = 19; +const SYS_MKDIR: usize = 20; +const SYS_CLOSE: usize = 21; +const SYS_CHMOD: usize = 22;