From f819a702c7d5ff9420fbd1039259d278cf7098da Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 13 Jul 2018 15:12:35 +0800 Subject: [PATCH] User programs Rust workspace. Compile OK for x86_64. --- user/Cargo.toml | 6 +++ user/Makefile | 2 + user/hello/Cargo.toml | 7 +++ user/hello/src/main.rs | 11 ++++ user/riscv32-blog_os.json | 30 +++++++++++ user/ucore-ulib/Cargo.toml | 6 +++ user/ucore-ulib/src/lang_items.rs | 40 +++++++++++++++ user/ucore-ulib/src/lib.rs | 10 ++++ user/ucore-ulib/src/syscall.rs | 85 +++++++++++++++++++++++++++++++ user/x86_64-blog_os.json | 14 +++++ 10 files changed, 211 insertions(+) create mode 100644 user/Cargo.toml create mode 100644 user/Makefile create mode 100644 user/hello/Cargo.toml create mode 100644 user/hello/src/main.rs create mode 100644 user/riscv32-blog_os.json create mode 100644 user/ucore-ulib/Cargo.toml create mode 100644 user/ucore-ulib/src/lang_items.rs create mode 100644 user/ucore-ulib/src/lib.rs create mode 100644 user/ucore-ulib/src/syscall.rs create mode 100644 user/x86_64-blog_os.json diff --git a/user/Cargo.toml b/user/Cargo.toml new file mode 100644 index 00000000..9044a4cd --- /dev/null +++ b/user/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] + +members = [ + "ucore-ulib", + "hello", +] \ No newline at end of file diff --git a/user/Makefile b/user/Makefile new file mode 100644 index 00000000..ec5f14e5 --- /dev/null +++ b/user/Makefile @@ -0,0 +1,2 @@ +all: + @RUST_TARGET_PATH=$(pwd) xargo build --target x86_64-blog_os \ No newline at end of file diff --git a/user/hello/Cargo.toml b/user/hello/Cargo.toml new file mode 100644 index 00000000..28a5ac69 --- /dev/null +++ b/user/hello/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hello" +version = "0.1.0" +authors = ["WangRunji "] + +[dependencies] +ucore-ulib = { path = "../ucore-ulib" } diff --git a/user/hello/src/main.rs b/user/hello/src/main.rs new file mode 100644 index 00000000..5a29680d --- /dev/null +++ b/user/hello/src/main.rs @@ -0,0 +1,11 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate ucore_ulib; + +// IMPORTANT: Must define main() like this +#[no_mangle] +pub fn main() { + println!("Hello uCore!"); +} diff --git a/user/riscv32-blog_os.json b/user/riscv32-blog_os.json new file mode 100644 index 00000000..ab76d65a --- /dev/null +++ b/user/riscv32-blog_os.json @@ -0,0 +1,30 @@ +{ + "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": "riscv", + "cpu": "generic-rv32", + "features": "+m", + "max-atomic-width": "32", + "linker": "ld.lld", + "linker-flavor": "ld", + "executables": true, + "panic-strategy": "abort", + "relocation-model": "static", + "abi-blacklist": [ + "cdecl", + "stdcall", + "fastcall", + "vectorcall", + "thiscall", + "aapcs", + "win64", + "sysv64", + "ptx-kernel", + "msp430-interrupt", + "x86-interrupt" + ] +} \ No newline at end of file diff --git a/user/ucore-ulib/Cargo.toml b/user/ucore-ulib/Cargo.toml new file mode 100644 index 00000000..186517ce --- /dev/null +++ b/user/ucore-ulib/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ucore-ulib" +version = "0.1.0" +authors = ["WangRunji "] + +[dependencies] diff --git a/user/ucore-ulib/src/lang_items.rs b/user/ucore-ulib/src/lang_items.rs new file mode 100644 index 00000000..ef8a1161 --- /dev/null +++ b/user/ucore-ulib/src/lang_items.rs @@ -0,0 +1,40 @@ +use syscall::sys_exit; + +#[linkage = "weak"] +#[no_mangle] +fn main() { + panic!("No main() linked"); +} + +#[no_mangle] +pub extern fn _start(_argc: isize, _argv: *const *const u8) -> ! +{ + main(); + sys_exit(0) +} + +#[lang = "eh_personality"] +fn eh_personality() {} + +#[cfg(target_arch = "x86_64")] +#[panic_implementation] +fn panic(info: &::core::panic::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) +} + +#[cfg(target_arch = "riscv")] +#[lang = "panic_fmt"] +#[no_mangle] +pub fn panic_fmt(fmt: ::core::fmt::Arguments, file: &'static str, line: u32, col: u32) -> ! { + println!("\n\nPANIC in {} at {}:{}\n {}", file, line, col, fmt); + sys_exit(1) +} + +#[cfg(target_arch = "x86_64")] +#[lang = "oom"] +fn oom() -> ! { + panic!("out of memory"); +} \ No newline at end of file diff --git a/user/ucore-ulib/src/lib.rs b/user/ucore-ulib/src/lib.rs new file mode 100644 index 00000000..a266b23f --- /dev/null +++ b/user/ucore-ulib/src/lib.rs @@ -0,0 +1,10 @@ +#![no_std] +#![feature(asm)] +#![feature(lang_items)] +#![feature(panic_implementation)] +#![feature(panic_info_message)] +#![feature(linkage)] + +#[macro_use] +pub mod syscall; +pub mod lang_items; \ No newline at end of file diff --git a/user/ucore-ulib/src/syscall.rs b/user/ucore-ulib/src/syscall.rs new file mode 100644 index 00000000..570e6c9b --- /dev/null +++ b/user/ucore-ulib/src/syscall.rs @@ -0,0 +1,85 @@ +use core::fmt::{self, Write}; + +#[macro_export] +macro_rules! print { + ($($arg:tt)*) => ({ + $crate::syscall::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(); +} + +struct StdOut; + +impl fmt::Write for StdOut { + fn write_str(&mut self, s: &str) -> fmt::Result { + match sys_write(0, s.as_ptr(), s.len()) { + 0 => Ok(()), + _ => Err(fmt::Error::default()), + } + } +} + +#[inline(always)] +fn sys_call(id: usize, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> isize { + let ret: isize; + unsafe { + #[cfg(target_arch = "riscv")] + asm!("ecall" + : "={x10}" (ret) + : "{x17}" (id), "{x10}" (arg0), "{x11}" (arg1), "{x12}" (arg2), "{x13}" (arg3), "{x14}" (arg4), "{x15}" (arg5) + : "memory" + : "volatile"); + #[cfg(target_arch = "x86_64")] + asm!("int 0x80" + : "={rax}" (ret) + : "{rax}" (id), "{rdi}" (arg0), "{rsi}" (arg1), "{rdx}" (arg2), "{rcx}" (arg3), "{r8}" (arg4), "{r9}" (arg5) + : "memory" + : "intel" "volatile"); + } + ret +} + +pub fn sys_exit(code: usize) -> ! { + sys_call(SYS_EXIT, code, 0, 0, 0, 0, 0); + unreachable!() +} + +pub fn sys_write(fd: usize, base: *const u8, len: usize) -> isize { + sys_call(SYS_WRITE, fd, base as usize, len, 0, 0, 0) +} + +const SYS_EXIT: usize = 1; +const SYS_FORK: usize = 2; +const SYS_WAIT: usize = 3; +const SYS_EXEC: usize = 4; +const SYS_CLONE: usize = 5; +const SYS_YIELD: usize = 10; +const SYS_SLEEP: usize = 11; +const SYS_KILL: usize = 12; +const SYS_GETTIME: usize = 17; +const SYS_GETPID: usize = 18; +const SYS_MMAP: usize = 20; +const SYS_MUNMAP: usize = 21; +const SYS_SHMEM: usize = 22; +const SYS_PUTC: usize = 30; +const SYS_PGDIR: usize = 31; +const SYS_OPEN: usize = 100; +const SYS_CLOSE: usize = 101; +const SYS_READ: usize = 102; +const SYS_WRITE: usize = 103; +const SYS_SEEK: usize = 104; +const SYS_FSTAT: usize = 110; +const SYS_FSYNC: usize = 111; +const SYS_GETCWD: usize = 121; +const SYS_GETDIRENTRY: usize = 128; +const SYS_DUP: usize = 130; +const SYS_LAB6_SET_PRIORITY: usize = 255; \ No newline at end of file diff --git a/user/x86_64-blog_os.json b/user/x86_64-blog_os.json new file mode 100644 index 00000000..c5871467 --- /dev/null +++ b/user/x86_64-blog_os.json @@ -0,0 +1,14 @@ +{ + "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-flavor": "ld.lld", + "panic-strategy": "abort", + "disable-redzone": true, + "features": "-mmx,-sse,+soft-float" +} \ No newline at end of file