update Rust user programs

master
WangRunji 6 years ago
parent 67e8bef0f5
commit acf1c655d4

@ -2,7 +2,7 @@ use alloc::string::String;
use core::fmt::{self, Write}; use core::fmt::{self, Write};
use core::option::Option; use core::option::Option;
use crate::syscall::{sys_putc, sys_read, sys_write}; use crate::syscall::{sys_read, sys_write};
pub const STDIN: usize = 0; pub const STDIN: usize = 0;
pub const STDOUT: usize = 1; pub const STDOUT: usize = 1;
@ -24,10 +24,6 @@ pub fn print(args: fmt::Arguments) {
StdOut.write_fmt(args).unwrap(); StdOut.write_fmt(args).unwrap();
} }
pub fn print_putc(args: fmt::Arguments) {
SysPutc.write_fmt(args).unwrap();
}
pub fn getc() -> Option<u8> { pub fn getc() -> Option<u8> {
let mut c = 0u8; let mut c = 0u8;
let ret = sys_read(STDIN, &mut c, 1); let ret = sys_read(STDIN, &mut c, 1);
@ -68,13 +64,11 @@ pub fn get_line() -> String {
} }
pub fn putc(c: u8) { pub fn putc(c: u8) {
sys_putc(c); sys_write(STDOUT, &c, 1);
} }
struct StdOut; struct StdOut;
struct SysPutc;
impl fmt::Write for StdOut { impl fmt::Write for StdOut {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
if sys_write(STDOUT, s.as_ptr(), s.len()) >= 0 { if sys_write(STDOUT, s.as_ptr(), s.len()) >= 0 {
@ -85,15 +79,6 @@ impl fmt::Write for StdOut {
} }
} }
impl fmt::Write for SysPutc {
fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.bytes() {
sys_putc(c);
}
Ok(())
}
}
/* VFS flags */ /* VFS flags */
// TODO: use bitflags // TODO: use bitflags
// flags for open: choose one of these // flags for open: choose one of these

@ -1,17 +1,10 @@
use crate::syscall::{sys_close, sys_dup, sys_exit, sys_open}; use crate::syscall::{sys_close, sys_dup2, sys_exit, sys_open};
use crate::io::{O_RDONLY, O_WRONLY, STDIN, STDOUT}; use crate::io::{O_RDONLY, O_WRONLY, STDIN, STDOUT};
use crate::ALLOCATOR; use crate::ALLOCATOR;
use core::alloc::Layout; use core::alloc::Layout;
use core::panic::PanicInfo; use core::panic::PanicInfo;
// used for panic
macro_rules! print {
($($arg:tt)*) => ({
$crate::io::print_putc(format_args!($($arg)*));
});
}
#[linkage = "weak"] #[linkage = "weak"]
#[no_mangle] #[no_mangle]
fn main() { fn main() {
@ -27,7 +20,7 @@ fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 {
let fd1 = fd1 as usize; let fd1 = fd1 as usize;
if fd1 != fd2 { if fd1 != fd2 {
sys_close(fd2); sys_close(fd2);
ret = sys_dup(fd1, fd2); ret = sys_dup2(fd1, fd2);
sys_close(fd1); sys_close(fd1);
} }
return ret; return ret;

@ -17,7 +17,7 @@ fn sys_call(syscall_id: SyscallId, arg0: usize, arg1: usize, arg2: usize, arg3:
: "memory" : "memory"
: "intel" "volatile"); : "intel" "volatile");
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
asm!("int 0x40" asm!("syscall"
: "={rax}" (ret) : "={rax}" (ret)
: "{rax}" (id), "{rdi}" (arg0), "{rsi}" (arg1), "{rdx}" (arg2), "{rcx}" (arg3), "{r8}" (arg4), "{r9}" (arg5) : "{rax}" (id), "{rdi}" (arg0), "{rsi}" (arg1), "{rdx}" (arg2), "{rcx}" (arg3), "{r8}" (arg4), "{r9}" (arg5)
: "memory" : "memory"
@ -64,8 +64,8 @@ pub fn sys_close(fd: usize) -> i32 {
sys_call(SyscallId::Close, fd, 0, 0, 0, 0, 0) sys_call(SyscallId::Close, fd, 0, 0, 0, 0, 0)
} }
pub fn sys_dup(fd1: usize, fd2: usize) -> i32 { pub fn sys_dup2(fd1: usize, fd2: usize) -> i32 {
sys_call(SyscallId::Dup, fd1, fd2, 0, 0, 0, 0) sys_call(SyscallId::Dup2, fd1, fd2, 0, 0, 0, 0)
} }
/// Fork the current process. Return the child's PID. /// Fork the current process. Return the child's PID.
@ -101,40 +101,33 @@ pub fn sys_get_time() -> i32 {
sys_call(SyscallId::GetTime, 0, 0, 0, 0, 0, 0) sys_call(SyscallId::GetTime, 0, 0, 0, 0, 0, 0)
} }
pub fn sys_lab6_set_priority(priority: usize) -> i32 { pub fn sys_set_priority(priority: usize) -> i32 {
sys_call(SyscallId::Lab6SetPriority, priority, 0, 0, 0, 0, 0) sys_call(SyscallId::SetPriority, priority, 0, 0, 0, 0, 0)
}
pub fn sys_putc(c: u8) -> i32 {
sys_call(SyscallId::Putc, c as usize, 0, 0, 0, 0, 0)
} }
#[allow(dead_code)] #[allow(dead_code)]
enum SyscallId{ enum SyscallId {
Exit = 1, Exit = 60,
Fork = 2, Fork = 57,
Wait = 3, Wait = 61,
Exec = 4, Exec = 59,
Clone = 5, Clone = 56,
Yield = 10, Yield = 24,
Sleep = 11, Sleep = 35,
Kill = 12, Kill = 62,
GetTime = 17, GetTime = 96,
GetPid = 18, GetPid = 39,
Mmap = 20, Mmap = 9,
Munmap = 21, Munmap = 11,
Shmem = 22, Open = 2,
Putc = 30, Close = 3,
Pgdir = 31, Read = 0,
Open = 100, Write = 1,
Close = 101, Seek = 8,
Read = 102, Fstat = 4,
Write = 103, Fsync = 74,
Seek = 104, GetCwd = 79,
Fstat = 110, GetDirEntry = 78,
Fsync = 111, Dup2 = 33,
GetCwd = 121, SetPriority = 141,
GetDirEntry = 128,
Dup = 130,
Lab6SetPriority = 255,
} }

Loading…
Cancel
Save