1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-26 09:53:28 +04:00

User programs Rust workspace. Compile OK for x86_64.

This commit is contained in:
WangRunji 2018-07-13 15:12:35 +08:00
parent 4faa8a65ae
commit f819a702c7
10 changed files with 211 additions and 0 deletions

6
user/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[workspace]
members = [
"ucore-ulib",
"hello",
]

2
user/Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
@RUST_TARGET_PATH=$(pwd) xargo build --target x86_64-blog_os

7
user/hello/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "hello"
version = "0.1.0"
authors = ["WangRunji <wangrunji0408@163.com>"]
[dependencies]
ucore-ulib = { path = "../ucore-ulib" }

11
user/hello/src/main.rs Normal file
View File

@ -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!");
}

30
user/riscv32-blog_os.json Normal file
View File

@ -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"
]
}

View File

@ -0,0 +1,6 @@
[package]
name = "ucore-ulib"
version = "0.1.0"
authors = ["WangRunji <wangrunji0408@163.com>"]
[dependencies]

View File

@ -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");
}

View File

@ -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;

View File

@ -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;

14
user/x86_64-blog_os.json Normal file
View File

@ -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"
}