diff --git a/user/Cargo.lock b/user/Cargo.lock index a0f951e..49754d9 100644 --- a/user/Cargo.lock +++ b/user/Cargo.lock @@ -1,6 +1,17 @@ +[[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-ulib" version = "0.1.0" +dependencies = [ + "linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "rcore-user-programs" @@ -9,3 +20,11 @@ dependencies = [ "rcore-ulib 0.1.0", ] +[[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/rcore-ulib/Cargo.toml b/user/rcore-ulib/Cargo.toml index ea67902..96590a8 100644 --- a/user/rcore-ulib/Cargo.toml +++ b/user/rcore-ulib/Cargo.toml @@ -5,3 +5,4 @@ authors = ["WangRunji "] edition = "2018" [dependencies] +linked_list_allocator = "0.6" diff --git a/user/rcore-ulib/src/lang_items.rs b/user/rcore-ulib/src/lang_items.rs index fbcb1b2..613db41 100644 --- a/user/rcore-ulib/src/lang_items.rs +++ b/user/rcore-ulib/src/lang_items.rs @@ -1,5 +1,7 @@ use crate::syscall::{sys_close, sys_dup, sys_exit, sys_open}; use crate::io::{O_RDONLY, O_WRONLY, STDIN, STDOUT}; +use crate::ALLOCATOR; + use core::alloc::Layout; use core::panic::PanicInfo; @@ -31,6 +33,12 @@ fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 { 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); @@ -42,6 +50,7 @@ pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! { panic!("open failed: {}.", fd); } + init_heap(); main(); sys_exit(0) } @@ -66,18 +75,3 @@ fn oom(_: Layout) -> ! { pub extern "C" fn abort() -> ! { sys_exit(2) } - -#[no_mangle] -pub extern "C" fn __mulsi3(mut a: u32, mut b: u32) -> u32 { - let mut r: u32 = 0; - - while a > 0 { - if a & 1 > 0 { - r += b; - } - a >>= 1; - b <<= 1; - } - - r -} diff --git a/user/rcore-ulib/src/lib.rs b/user/rcore-ulib/src/lib.rs index 3cd6042..18aa0cb 100644 --- a/user/rcore-ulib/src/lib.rs +++ b/user/rcore-ulib/src/lib.rs @@ -8,4 +8,9 @@ #[macro_use] pub mod io; pub mod syscall; -pub mod lang_items; \ No newline at end of file +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/targets/riscv32-rcore.json b/user/targets/riscv32-rcore.json index 354279e..d3456c7 100644 --- a/user/targets/riscv32-rcore.json +++ b/user/targets/riscv32-rcore.json @@ -7,7 +7,7 @@ "os": "none", "arch": "riscv32", "cpu": "generic-rv32", - "features": "", + "features": "+m,+a,+c", "max-atomic-width": "32", "linker": "rust-lld", "linker-flavor": "ld.lld",