diff --git a/user/Cargo.lock b/user/Cargo.lock deleted file mode 100644 index 7e2e08e..0000000 --- a/user/Cargo.lock +++ /dev/null @@ -1,25 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "linked_list_allocator" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rcore-user" -version = "0.1.0" -dependencies = [ - "linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "spin" -version = "0.4.10" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[metadata] -"checksum linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "655d57c71827fe0891ce72231b6aa5e14033dae3f604609e6a6f807267c1678d" -"checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" diff --git a/user/Cargo.toml b/user/Cargo.toml deleted file mode 100644 index 6122d8e..0000000 --- a/user/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "rcore-user" -version = "0.1.0" -authors = ["Runji Wang "] -edition = "2018" - -[dependencies] -linked_list_allocator = "0.6" \ No newline at end of file diff --git a/user/Makefile b/user/Makefile deleted file mode 100644 index 131199a..0000000 --- a/user/Makefile +++ /dev/null @@ -1,59 +0,0 @@ -# arch = {riscv32, riscv64, x86_64, aarch64} -# mode = {debug, release} -arch ?= riscv32 -mode ?= debug - -rust_src_dir := src/bin -rust_bin_path := target/$(arch)-rcore/$(mode) -user_rust_bins := $(patsubst $(rust_src_dir)/%.rs, $(rust_bin_path)/%, $(wildcard $(rust_src_dir)/*.rs)) -c_src_dir := -c_bin_path := -user_c_bins := -user_bins := $(user_rust_bins) $(user_c_bins) - -build_path := build -sfsroot := $(build_path)/sfsroot-$(arch) -sfsimg := $(build_path)/user-$(arch).img - -mksfs := mksfs - -build_args := --target targets/$(arch)-rcore.json -ifeq ($(mode), release) -build_args := $(build_args) --release -endif - -.PHONY: all clean build-rust build-c build mksfs sfsimg - -all: $(sfsimg) - -build-rust: - @echo Building user rust programs - @cargo xbuild $(build_args) - -build-c: - @echo Building user C programs - -build: build-rust build-c - -$(user_rust_bins): build-rust - -$(user_c_bins): build-c - -mksfs: -ifeq ($(shell which $(mksfs)),) - @cargo install --git https://github.com/wangrunji0408/SimpleFileSystem-Rust --features="std" -endif - -$(sfsroot): $(user_bins) - @mkdir -p $@ - @cp $^ $@/ - -$(sfsimg): $(user_bins) $(sfsroot) | mksfs - @echo Creating SFS image at $@ - @$(mksfs) zip $(sfsroot) $@ - -sfsimg: $(sfsimg) - -clean: - @cargo clean - @rm -rf $(build_path) diff --git a/user/img/ucore-aarch64.img b/user/img/ucore-aarch64.img deleted file mode 100644 index ba673f0..0000000 Binary files a/user/img/ucore-aarch64.img and /dev/null differ diff --git a/user/img/ucore-i386-pic.img b/user/img/ucore-i386-pic.img deleted file mode 100644 index a70e263..0000000 Binary files a/user/img/ucore-i386-pic.img and /dev/null differ diff --git a/user/img/ucore-i386.img b/user/img/ucore-i386.img deleted file mode 100644 index 88949ab..0000000 Binary files a/user/img/ucore-i386.img and /dev/null differ diff --git a/user/img/ucore-riscv32.img b/user/img/ucore-riscv32.img deleted file mode 100644 index 9c28ed5..0000000 Binary files a/user/img/ucore-riscv32.img and /dev/null differ diff --git a/user/img/ucore-riscv64.img b/user/img/ucore-riscv64.img deleted file mode 100644 index 4b2efb1..0000000 Binary files a/user/img/ucore-riscv64.img and /dev/null differ diff --git a/user/img/xv6-x86_64.img b/user/img/xv6-x86_64.img deleted file mode 100644 index 94090c8..0000000 Binary files a/user/img/xv6-x86_64.img and /dev/null differ diff --git a/user/src/bin/fantastic_text.rs b/user/src/bin/fantastic_text.rs deleted file mode 100644 index b1403ed..0000000 --- a/user/src/bin/fantastic_text.rs +++ /dev/null @@ -1,43 +0,0 @@ -#![no_std] -#![no_main] - -#[macro_use] -extern crate rcore_user; - -macro_rules! color_text { - ($text:expr, $color:expr) => {{ - format_args!("\x1b[{}m{}\x1b[0m", $color, $text) - }}; -} - -// IMPORTANT: Must define main() like this -#[no_mangle] -pub fn main() { - println!( - "{}{}{}{}{} {}{}{}{} {}{}{}{}{}{}", - color_text!("H", 31), - color_text!("e", 32), - color_text!("l", 33), - color_text!("l", 34), - color_text!("o", 35), - color_text!("R", 36), - color_text!("u", 37), - color_text!("s", 90), - color_text!("t", 91), - color_text!("u", 92), - color_text!("C", 93), - color_text!("o", 94), - color_text!("r", 95), - color_text!("e", 96), - color_text!("!", 97), - ); - - let text = "reguler \x1b[4munderline\x1b[24m \x1b[7mreverse\x1b[27m \x1b[9mstrikethrough\x1b[29m"; - println!("\x1b[47m{}\x1b[0m", color_text!(text, 30)); - for i in 31..38 { - println!("{}", color_text!(text, i)); - } - for i in 90..98 { - println!("{}", color_text!(text, i)); - } -} diff --git a/user/src/bin/hello_rust.rs b/user/src/bin/hello_rust.rs deleted file mode 100644 index 56f7d23..0000000 --- a/user/src/bin/hello_rust.rs +++ /dev/null @@ -1,13 +0,0 @@ -#![no_std] -#![no_main] - -#[macro_use] -extern crate rcore_user; - -// IMPORTANT: Must define main() like this -#[no_mangle] -pub fn main() { - println!("Hello Rust uCore!"); - println!("I am process {}.", rcore_user::syscall::sys_getpid()); - println!("hello pass."); -} diff --git a/user/src/bin/sh.rs b/user/src/bin/sh.rs deleted file mode 100644 index 43ba90e..0000000 --- a/user/src/bin/sh.rs +++ /dev/null @@ -1,39 +0,0 @@ -#![no_std] -#![no_main] -#![feature(alloc)] - -extern crate alloc; -#[macro_use] -extern crate rcore_user; - -use alloc::vec::Vec; - -use rcore_user::io::get_line; -use rcore_user::syscall::{sys_exec, sys_fork, sys_wait}; - -// IMPORTANT: Must define main() like this -#[no_mangle] -pub fn main() -> i32 { - println!("Rust user shell"); - loop { - print!(">> "); - let cmd = get_line(); - // split cmd, make argc & argv - let cmd = cmd.replace(' ', "\0") + "\0"; - let ptrs: Vec<*const u8> = cmd.split('\0') - .filter(|s| !s.is_empty()).map(|s| s.as_ptr()).collect(); - if ptrs.is_empty() { - continue; - } - - let pid = sys_fork(); - assert!(pid >= 0); - if pid == 0 { - return sys_exec(ptrs[0], ptrs.len(), ptrs.as_ptr()); - } else { - let mut code: i32 = 0; - sys_wait(pid as usize, &mut code); - println!("\n[Process exited with code {}]", code); - } - } -} diff --git a/user/src/io.rs b/user/src/io.rs deleted file mode 100644 index cf6c4a1..0000000 --- a/user/src/io.rs +++ /dev/null @@ -1,92 +0,0 @@ -use alloc::string::String; -use core::fmt::{self, Write}; -use core::option::Option; - -use crate::syscall::{sys_read, sys_write}; - -pub const STDIN: usize = 0; -pub const STDOUT: usize = 1; - -#[macro_export] -macro_rules! print { - ($($arg:tt)*) => ({ - $crate::io::print(format_args!($($arg)*)); - }); -} - -#[macro_export] -macro_rules! println { - ($fmt:expr) => (print!(concat!($fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); -} - -pub fn print(args: fmt::Arguments) { - StdOut.write_fmt(args).unwrap(); -} - -pub fn getc() -> Option { - let mut c = 0u8; - let ret = sys_read(STDIN, &mut c, 1); - match ret { - 1 => Some(c), - 0 => None, - _ => panic!(), - } -} - -pub fn get_line() -> String { - let mut s = String::new(); - loop { - let ret = getc(); - match ret { - None => return s, - Some(byte) => { - let c = byte as char; - match c { - '\x08' | '\x7f' /* '\b' */ => { - if s.pop().is_some() { - print!("\x08 \x08"); - } - } - ' '...'\x7e' => { - s.push(c); - print!("{}", c); - } - '\n' | '\r' => { - print!("\n"); - return s; - } - _ => {} - } - } - } - } -} - -pub fn putc(c: u8) { - sys_write(STDOUT, &c, 1); -} - -struct StdOut; - -impl fmt::Write for StdOut { - fn write_str(&mut self, s: &str) -> fmt::Result { - if sys_write(STDOUT, s.as_ptr(), s.len()) >= 0 { - Ok(()) - } else { - Err(fmt::Error::default()) - } - } -} - -/* VFS flags */ -// TODO: use bitflags -// flags for open: choose one of these -pub const O_RDONLY: usize = 0; // open for reading only -pub const O_WRONLY: usize = 1; // open for writing only -pub const O_RDWR: usize = 2; // open for reading and writing -// then or in any of these: -pub const O_CREAT: usize = 0x00000004; // create file if it does not exist -pub const O_EXCL: usize = 0x00000008; // error if O_CREAT and the file exists -pub const O_TRUNC: usize = 0x00000010; // truncate file upon open -pub const O_APPEND: usize = 0x00000020; // append on each write \ No newline at end of file diff --git a/user/src/lang_items.rs b/user/src/lang_items.rs deleted file mode 100644 index f125f8c..0000000 --- a/user/src/lang_items.rs +++ /dev/null @@ -1,70 +0,0 @@ -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; - -#[linkage = "weak"] -#[no_mangle] -fn main() { - panic!("No main() linked"); -} - -fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 { - let fd1 = sys_open(path, open_flags); - if fd1 < 0 { - return fd1; - } - let mut ret = fd1; - let fd1 = fd1 as usize; - if fd1 != fd2 { - sys_close(fd2); - ret = sys_dup2(fd1, fd2); - sys_close(fd1); - } - return ret; -} - -fn init_heap() { - const HEAP_SIZE: usize = 0x1000; - static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE]; - unsafe { ALLOCATOR.lock().init(HEAP.as_ptr() as usize, HEAP_SIZE); } -} - -#[no_mangle] -pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! { - let fd = initfd(STDIN, "stdin:", O_RDONLY); - if fd < 0 { - panic!("open failed: {}.", fd); - } - let fd = initfd(STDOUT, "stdout:", O_WRONLY); - if fd < 0 { - panic!("open failed: {}.", fd); - } - - init_heap(); - main(); - sys_exit(0) -} - -#[lang = "eh_personality"] -fn eh_personality() {} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - let location = info.location().unwrap(); - let message = info.message().unwrap(); - println!("\n\nPANIC in {} at line {}\n {}", location.file(), location.line(), message); - sys_exit(1) -} - -#[lang = "oom"] -fn oom(_: Layout) -> ! { - panic!("out of memory"); -} - -#[no_mangle] -pub extern "C" fn abort() -> ! { - sys_exit(2) -} diff --git a/user/src/lib.rs b/user/src/lib.rs deleted file mode 100644 index 3863e78..0000000 --- a/user/src/lib.rs +++ /dev/null @@ -1,19 +0,0 @@ -#![no_std] -#![feature(asm)] -#![feature(alloc)] -#![feature(lang_items)] -#![feature(panic_info_message)] -#![feature(linkage)] -#![feature(compiler_builtins_lib)] - -extern crate alloc; - -#[macro_use] -pub mod io; -pub mod syscall; -pub mod lang_items; - -use linked_list_allocator::LockedHeap; - -#[global_allocator] -static ALLOCATOR: LockedHeap = LockedHeap::empty(); \ No newline at end of file diff --git a/user/src/syscall.rs b/user/src/syscall.rs deleted file mode 100644 index b674201..0000000 --- a/user/src/syscall.rs +++ /dev/null @@ -1,133 +0,0 @@ -#[inline(always)] -fn sys_call(syscall_id: SyscallId, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> i32 { - let id = syscall_id as usize; - let ret: i32; - - unsafe { - #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] - asm!("ecall" - : "={x10}" (ret) - : "{x10}" (id), "{x11}" (arg0), "{x12}" (arg1), "{x13}" (arg2), "{x14}" (arg3), "{x15}" (arg4), "{x16}" (arg5) - : "memory" - : "volatile"); - #[cfg(target_arch = "x86")] - asm!("int 0x80" - : "={eax}" (ret) - : "{eax}" (id), "{edx}" (arg0), "{ecx}" (arg1), "{ebx}" (arg2), "{edi}" (arg3), "{esi}" (arg4) - : "memory" - : "intel" "volatile"); - #[cfg(target_arch = "x86_64")] - asm!("syscall" - : "={rax}" (ret) - : "{rax}" (id), "{rdi}" (arg0), "{rsi}" (arg1), "{rdx}" (arg2), "{rcx}" (arg3), "{r8}" (arg4), "{r9}" (arg5) - : "memory" - : "intel" "volatile"); - #[cfg(target_arch = "aarch64")] - asm!("svc 0" - : "={x0}" (ret) - : "{x8}" (id), "{x0}" (arg0), "{x1}" (arg1), "{x2}" (arg2), "{x3}" (arg3), "{x4}" (arg4), "{x5}" (arg5) - : "memory" - : "volatile"); - } - ret -} - -pub fn sys_exit(code: usize) -> ! { - sys_call(SyscallId::Exit, code, 0, 0, 0, 0, 0); - unreachable!() -} - - -pub fn sys_exec(name: *const u8, argc: usize, argv: *const *const u8) -> i32 { - sys_call(SyscallId::Exec, name as usize, argc, argv as usize, 0, 0, 0) -} - -pub fn sys_write(fd: usize, base: *const u8, len: usize) -> i32 { - sys_call(SyscallId::Write, fd, base as usize, len, 0, 0, 0) -} - -pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> i32 { - sys_call(SyscallId::Read, fd, base as usize, len, 0, 0, 0) -} - -pub fn sys_open(path: &str, flags: usize) -> i32 { - // UNSAFE: append '\0' to the string - use core::mem::replace; - let end = unsafe { &mut *(path.as_ptr().offset(path.len() as isize) as *mut u8) }; - let backup = replace(end, 0); - let ret = sys_call(SyscallId::Open, path.as_ptr() as usize, flags, 0, 0, 0, 0); - *end = backup; - ret -} - -pub fn sys_close(fd: usize) -> i32 { - sys_call(SyscallId::Close, fd, 0, 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. -pub fn sys_fork() -> i32 { - sys_call(SyscallId::Fork, 0, 0, 0, 0, 0, 0) -} - -/// Wait the process exit. -/// Return the PID. Store exit code to `code` if it's not null. -pub fn sys_wait(pid: usize, code: *mut i32) -> i32 { - sys_call(SyscallId::Wait, pid, code as usize, 0, 0, 0, 0) -} - -pub fn sys_yield() -> i32 { - sys_call(SyscallId::Yield, 0, 0, 0, 0, 0, 0) -} - -/// Kill the process -pub fn sys_kill(pid: usize) -> i32 { - sys_call(SyscallId::Kill, pid, 0, 0, 0, 0, 0) -} - -/// Get the current process id -pub fn sys_getpid() -> i32 { - sys_call(SyscallId::GetPid, 0, 0, 0, 0, 0, 0) -} - -pub fn sys_sleep(time: usize) -> i32 { - sys_call(SyscallId::Sleep, time, 0, 0, 0, 0, 0) -} - -pub fn sys_get_time() -> i32 { - sys_call(SyscallId::GetTime, 0, 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 = 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, -} diff --git a/user/targets/aarch64-rcore.json b/user/targets/aarch64-rcore.json deleted file mode 100644 index a4adac2..0000000 --- a/user/targets/aarch64-rcore.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "abi-blacklist": [ - "stdcall", - "fastcall", - "vectorcall", - "thiscall", - "win64", - "sysv64" - ], - "arch": "aarch64", - "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", - "executables": true, - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "linker-is-gnu": true, - "pre-link-args": { - "ld.lld": [ - "--image-base=0xffff000000000000" - ] - }, - "llvm-target": "aarch64-unknown-none", - "no-compiler-rt": true, - "features": "+a53,+strict-align,-neon", - "max-atomic-width": 128, - "os": "none", - "panic": "abort", - "panic-strategy": "abort", - "relocation-model": "static", - "position-independent-executables": true, - "target-c-int-width": "32", - "target-endian": "little", - "target-pointer-width": "64", - "target-family": "unix", - "disable-redzone": true -} diff --git a/user/targets/i686-rcore.json b/user/targets/i686-rcore.json deleted file mode 100644 index b5138ec..0000000 --- a/user/targets/i686-rcore.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "arch": "x86", - "cpu": "pentium4", - "data-layout": "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128", - "executables": true, - "linker-flavor": "ld.lld", - "linker": "rust-lld", - "llvm-target": "i686", - "max-atomic-width": 64, - "os": "none", - "target-c-int-width": "32", - "target-endian": "little", - "target-pointer-width": "32", - "features": "-mmx,-sse,+soft-float" -} \ No newline at end of file diff --git a/user/targets/riscv32-rcore.json b/user/targets/riscv32-rcore.json deleted file mode 100644 index aa03e0c..0000000 --- a/user/targets/riscv32-rcore.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "llvm-target": "riscv32", - "data-layout": "e-m:e-p:32:32-i64:64-n32-S128", - "target-endian": "little", - "target-pointer-width": "32", - "target-c-int-width": "32", - "os": "none", - "arch": "riscv32", - "cpu": "generic-rv32", - "features": "+m,+a,+c", - "max-atomic-width": "32", - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "executables": true, - "panic-strategy": "abort", - "relocation-model": "static", - "abi-blacklist": [ - "cdecl", - "stdcall", - "fastcall", - "vectorcall", - "thiscall", - "aapcs", - "win64", - "sysv64", - "ptx-kernel", - "msp430-interrupt", - "x86-interrupt" - ] -} diff --git a/user/targets/riscv64-rcore.json b/user/targets/riscv64-rcore.json deleted file mode 100644 index fe95ada..0000000 --- a/user/targets/riscv64-rcore.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "llvm-target": "riscv64", - "data-layout": "e-m:e-p:64:64-i64:64-n64-S128", - "target-endian": "little", - "target-pointer-width": "64", - "target-c-int-width": "32", - "os": "none", - "arch": "riscv64", - "cpu": "generic-rv64", - "features": "+m,+a,+c", - "max-atomic-width": "64", - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "executables": true, - "panic-strategy": "abort", - "relocation-model": "static", - "abi-blacklist": [ - "cdecl", - "stdcall", - "fastcall", - "vectorcall", - "thiscall", - "aapcs", - "win64", - "sysv64", - "ptx-kernel", - "msp430-interrupt", - "x86-interrupt" - ] -} diff --git a/user/targets/x86_64-rcore.json b/user/targets/x86_64-rcore.json deleted file mode 100644 index 6510efa..0000000 --- a/user/targets/x86_64-rcore.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "llvm-target": "x86_64-unknown-none", - "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", - "arch": "x86_64", - "target-endian": "little", - "target-pointer-width": "64", - "target-c-int-width": "32", - "os": "none", - "executables": true, - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "panic-strategy": "abort", - "disable-redzone": true, - "features": "-mmx,-sse,+soft-float" -}