user: user rust programs are runnable

master
equation314 6 years ago
parent f882a085d6
commit e86229cb71

@ -33,7 +33,7 @@ fn main() {
} }
"aarch64" => { "aarch64" => {
if let Ok(file_path) = gen_sfsimg_asm() { if let Ok(file_path) = gen_sfsimg_asm() {
cc::Build::new().file(&file_path).compile("cobj"); cc::Build::new().file(&file_path).compile("sfsimg");
} }
} }
_ => panic!("Unknown arch {}", arch), _ => panic!("Unknown arch {}", arch),

@ -110,6 +110,10 @@ impl ContextImpl {
unsafe { unsafe {
memory_set.with(|| { memory_set.with(|| {
for ph in elf.program_iter() { for ph in elf.program_iter() {
if ph.get_type() != Ok(Type::Load) {
continue;
}
let virt_addr = ph.virtual_addr() as usize; let virt_addr = ph.virtual_addr() as usize;
let offset = ph.offset() as usize; let offset = ph.offset() as usize;
let file_size = ph.file_size() as usize; let file_size = ph.file_size() as usize;

@ -284,6 +284,7 @@ fn get_file(fd: usize) -> Result<&'static Arc<Mutex<File>>, SysError> {
pub type SysResult = Result<i32, SysError>; pub type SysResult = Result<i32, SysError>;
#[repr(i32)] #[repr(i32)]
#[derive(Debug)]
pub enum SysError { pub enum SysError {
VfsError, VfsError,
InvalidFile, InvalidFile,

@ -13,6 +13,11 @@
"linker": "rust-lld", "linker": "rust-lld",
"linker-flavor": "ld.lld", "linker-flavor": "ld.lld",
"linker-is-gnu": true, "linker-is-gnu": true,
"pre-link-args": {
"ld.lld": [
"-Tsrc/arch/aarch64/user.ld"
]
},
"llvm-target": "aarch64-unknown-none", "llvm-target": "aarch64-unknown-none",
"no-compiler-rt": true, "no-compiler-rt": true,
"features": "+a53,+strict-align,-neon", "features": "+a53,+strict-align,-neon",

@ -11,6 +11,11 @@
"max-atomic-width": "32", "max-atomic-width": "32",
"linker": "rust-lld", "linker": "rust-lld",
"linker-flavor": "ld.lld", "linker-flavor": "ld.lld",
"pre-link-args": {
"ld.lld": [
"-Tsrc/arch/riscv32/user.ld"
]
},
"executables": true, "executables": true,
"panic-strategy": "abort", "panic-strategy": "abort",
"relocation-model": "static", "relocation-model": "static",

@ -0,0 +1,46 @@
/* Simple linker script for ucore user-level programs.
See the GNU ld 'info' manual ("info ld") to learn the syntax. */
OUTPUT_ARCH(aarch64)
ENTRY(_start)
SECTIONS {
/* Load programs at this address: "." means the current address */
. = 0xffff000000000000;
.text : {
*(.text .stub .text.* .gnu.linkonce.t.*)
}
PROVIDE(etext = .); /* Define the 'etext' symbol to this value */
.rodata : {
*(.rodata .rodata.* .gnu.linkonce.r.*)
}
/* Adjust the address for the data segment to the next page */
. = ALIGN(0x1000);
/* The data segment */
.data : {
*(.data)
*(.data.*)
}
.sdata : {
*(.sdata)
*(.sdata.*)
}
PROVIDE(edata = .);
.bss : {
*(.bss)
}
PROVIDE(end = .);
/DISCARD/ : {
*(.eh_frame .note.GNU-stack .comment)
}
}

@ -0,0 +1,46 @@
/* Simple linker script for ucore user-level programs.
See the GNU ld 'info' manual ("info ld") to learn the syntax. */
OUTPUT_ARCH(riscv)
ENTRY(_start)
SECTIONS {
/* Load programs at this address: "." means the current address */
. = 0x800020;
.text : {
*(.text .stub .text.* .gnu.linkonce.t.*)
}
PROVIDE(etext = .); /* Define the 'etext' symbol to this value */
.rodata : {
*(.rodata .rodata.* .gnu.linkonce.r.*)
}
/* Adjust the address for the data segment to the next page */
. = ALIGN(0x1000);
/* The data segment */
.data : {
*(.data)
*(.data.*)
}
.sdata : {
*(.sdata)
*(.sdata.*)
}
PROVIDE(edata = .);
.bss : {
*(.bss)
}
PROVIDE(end = .);
/DISCARD/ : {
*(.eh_frame .note.GNU-stack .comment)
}
}

@ -0,0 +1,46 @@
/* Simple linker script for ucore user-level programs.
See the GNU ld 'info' manual ("info ld") to learn the syntax. */
OUTPUT_ARCH(x86_64)
ENTRY(_start)
SECTIONS {
/* Load programs at this address: "." means the current address */
. = 0x800020;
.text : {
*(.text .stub .text.* .gnu.linkonce.t.*)
}
PROVIDE(etext = .); /* Define the 'etext' symbol to this value */
.rodata : {
*(.rodata .rodata.* .gnu.linkonce.r.*)
}
/* Adjust the address for the data segment to the next page */
. = ALIGN(0x1000);
/* The data segment */
.data : {
*(.data)
*(.data.*)
}
.sdata : {
*(.sdata)
*(.sdata.*)
}
PROVIDE(edata = .);
.bss : {
*(.bss)
}
PROVIDE(end = .);
/DISCARD/ : {
*(.eh_frame .note.GNU-stack .comment)
}
}

@ -7,5 +7,7 @@ extern crate ucore_ulib;
// IMPORTANT: Must define main() like this // IMPORTANT: Must define main() like this
#[no_mangle] #[no_mangle]
pub fn main() { pub fn main() {
println!("Hello uCore!"); println!("Hello Rust uCore!");
println!("I am process {}.", ucore_ulib::syscall::sys_getpid());
println!("hello pass.");
} }

@ -1,15 +1,47 @@
use syscall::sys_exit; use syscall::{sys_close, sys_dup, sys_exit, sys_open};
use syscall::{O_RDONLY, O_WRONLY};
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::syscall::print_putc(format_args!($($arg)*));
});
}
#[linkage = "weak"] #[linkage = "weak"]
#[no_mangle] #[no_mangle]
fn main() { fn main() {
panic!("No main() linked"); 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_dup(fd1, fd2);
sys_close(fd1);
}
return ret;
}
#[no_mangle] #[no_mangle]
pub extern fn _start(_argc: isize, _argv: *const *const u8) -> ! { pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! {
let fd = initfd(0, "stdin:", O_RDONLY);
if fd < 0 {
panic!("open <stdin> failed: {}.", fd);
}
let fd = initfd(1, "stdout:", O_WRONLY);
if fd < 0 {
panic!("open <stdout> failed: {}.", fd);
}
main(); main();
sys_exit(0) sys_exit(0)
} }
@ -31,12 +63,12 @@ fn oom(_: Layout) -> ! {
} }
#[no_mangle] #[no_mangle]
pub extern fn abort() -> ! { pub extern "C" fn abort() -> ! {
sys_exit(2) sys_exit(2)
} }
#[no_mangle] #[no_mangle]
pub extern fn __mulsi3(mut a: u32, mut b: u32) -> u32 { pub extern "C" fn __mulsi3(mut a: u32, mut b: u32) -> u32 {
let mut r: u32 = 0; let mut r: u32 = 0;
while a > 0 { while a > 0 {

@ -17,14 +17,29 @@ 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();
}
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 {
match sys_write(0, s.as_ptr(), s.len()) { if sys_write(1, s.as_ptr(), s.len()) >= 0 {
0 => Ok(()), Ok(())
_ => Err(fmt::Error::default()), } else {
Err(fmt::Error::default())
}
}
}
impl fmt::Write for SysPutc {
fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.bytes() {
sys_putc(c as char);
} }
Ok(())
} }
} }
@ -77,6 +92,10 @@ pub fn sys_close(fd: usize) -> i32 {
sys_call(SYS_CLOSE, fd, 0, 0, 0, 0, 0) sys_call(SYS_CLOSE, fd, 0, 0, 0, 0, 0)
} }
pub fn sys_dup(fd1: usize, fd2: usize) -> i32 {
sys_call(SYS_DUP, fd1, fd2, 0, 0, 0, 0)
}
/// Fork the current process. Return the child's PID. /// Fork the current process. Return the child's PID.
pub fn sys_fork() -> i32 { pub fn sys_fork() -> i32 {
sys_call(SYS_FORK, 0, 0, 0, 0, 0, 0) sys_call(SYS_FORK, 0, 0, 0, 0, 0, 0)
@ -144,3 +163,15 @@ const SYS_GETCWD: usize = 121;
const SYS_GETDIRENTRY: usize = 128; const SYS_GETDIRENTRY: usize = 128;
const SYS_DUP: usize = 130; const SYS_DUP: usize = 130;
const SYS_LAB6_SET_PRIORITY: usize = 255; const SYS_LAB6_SET_PRIORITY: usize = 255;
/* 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

@ -9,6 +9,11 @@
"executables": true, "executables": true,
"linker": "rust-lld", "linker": "rust-lld",
"linker-flavor": "ld.lld", "linker-flavor": "ld.lld",
"pre-link-args": {
"ld.lld": [
"-Tsrc/arch/x86_64/user.ld"
]
},
"panic-strategy": "abort", "panic-strategy": "abort",
"disable-redzone": true, "disable-redzone": true,
"features": "-mmx,-sse,+soft-float" "features": "-mmx,-sse,+soft-float"

Loading…
Cancel
Save