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::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 STDOUT: usize = 1;
@ -24,10 +24,6 @@ pub fn print(args: fmt::Arguments) {
StdOut.write_fmt(args).unwrap();
}
pub fn print_putc(args: fmt::Arguments) {
SysPutc.write_fmt(args).unwrap();
}
pub fn getc() -> Option<u8> {
let mut c = 0u8;
let ret = sys_read(STDIN, &mut c, 1);
@ -68,13 +64,11 @@ pub fn get_line() -> String {
}
pub fn putc(c: u8) {
sys_putc(c);
sys_write(STDOUT, &c, 1);
}
struct StdOut;
struct SysPutc;
impl fmt::Write for StdOut {
fn write_str(&mut self, s: &str) -> fmt::Result {
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 */
// TODO: use bitflags
// 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::ALLOCATOR;
use core::alloc::Layout;
use core::panic::PanicInfo;
// used for panic
macro_rules! print {
($($arg:tt)*) => ({
$crate::io::print_putc(format_args!($($arg)*));
});
}
#[linkage = "weak"]
#[no_mangle]
fn main() {
@ -27,7 +20,7 @@ fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 {
let fd1 = fd1 as usize;
if fd1 != fd2 {
sys_close(fd2);
ret = sys_dup(fd1, fd2);
ret = sys_dup2(fd1, fd2);
sys_close(fd1);
}
return ret;

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

Loading…
Cancel
Save