Heap test passed on k210/qemu, heap size = 3M.

This commit is contained in:
Yifan Wu 2020-12-02 10:32:26 +08:00
parent 4b01ff7cc4
commit 2fd70b0ff4
7 changed files with 81 additions and 11 deletions

18
os/Cargo.lock generated
View File

@ -30,13 +30,22 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "buddy_system_allocator"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4e85e760e105b46ae0bd1236578793c6c147ae7463fe95c8350296b8bfcb830"
dependencies = [
"spin 0.7.0",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [ dependencies = [
"spin", "spin 0.5.2",
] ]
[[package]] [[package]]
@ -49,6 +58,7 @@ checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
name = "os" name = "os"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"buddy_system_allocator",
"lazy_static", "lazy_static",
"riscv", "riscv",
] ]
@ -122,6 +132,12 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "652ac3743312871a5fb703f0337e68ffa3cdc28c863efad0b8dc858fa10c991b"
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.0.1" version = "1.0.1"

View File

@ -9,6 +9,7 @@ edition = "2018"
[dependencies] [dependencies]
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
lazy_static = { version = "1.4.0", features = ["spin_no_std"] } lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
buddy_system_allocator = "0.6"
[features] [features]
board_qemu = [] board_qemu = []

View File

@ -6,7 +6,7 @@ fn main() {
insert_app_data().unwrap(); insert_app_data().unwrap();
} }
static TARGET_PATH: &str = "../user/target/riscv64gc-unknown-none-elf/debug/"; static TARGET_PATH: &str = "../user/target/riscv64gc-unknown-none-elf/release/";
fn insert_app_data() -> Result<()> { fn insert_app_data() -> Result<()> {
let mut f = File::create("src/link_app.S").unwrap(); let mut f = File::create("src/link_app.S").unwrap();
@ -26,17 +26,14 @@ fn insert_app_data() -> Result<()> {
.section .data .section .data
.global _num_app .global _num_app
_num_app: _num_app:
.quad {} .quad {}"#, apps.len())?;
"#, apps.len())?;
for i in 0..apps.len() { for i in 0..apps.len() {
writeln!(f, r#" writeln!(f, r#"
.quad app_{}_start .quad app_{}_start"#, i)?;
"#, i)?;
} }
writeln!(f, r#" writeln!(f, r#"
.quad app_{}_end .quad app_{}_end"#, apps.len() - 1)?;
"#, apps.len() - 1)?;
for (idx, app) in apps.iter().enumerate() { for (idx, app) in apps.iter().enumerate() {
println!("app_{}: {}", idx, app); println!("app_{}: {}", idx, app);
@ -45,9 +42,8 @@ _num_app:
.global app_{0}_start .global app_{0}_start
.global app_{0}_end .global app_{0}_end
app_{0}_start: app_{0}_start:
.incbin "{2}{1}.bin" .incbin "{2}{1}"
app_{0}_end: app_{0}_end:"#, idx, app, TARGET_PATH)?;
"#, idx, app, TARGET_PATH)?;
} }
Ok(()) Ok(())
} }

View File

@ -3,6 +3,7 @@ pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
pub const MAX_APP_NUM: usize = 4; pub const MAX_APP_NUM: usize = 4;
pub const APP_BASE_ADDRESS: usize = 0x80100000; pub const APP_BASE_ADDRESS: usize = 0x80100000;
pub const APP_SIZE_LIMIT: usize = 0x20000; pub const APP_SIZE_LIMIT: usize = 0x20000;
pub const KERNEL_HEAP_SIZE: usize = 0x30_0000;
#[cfg(feature = "board_k210")] #[cfg(feature = "board_k210")]
pub const CPU_FREQ: usize = 10000000; pub const CPU_FREQ: usize = 10000000;

View File

@ -4,6 +4,9 @@
#![feature(llvm_asm)] #![feature(llvm_asm)]
#![feature(panic_info_message)] #![feature(panic_info_message)]
#![feature(const_in_array_repeat_expressions)] #![feature(const_in_array_repeat_expressions)]
#![feature(alloc_error_handler)]
extern crate alloc;
#[macro_use] #[macro_use]
mod console; mod console;
@ -15,6 +18,7 @@ mod loader;
mod config; mod config;
mod task; mod task;
mod timer; mod timer;
mod mm;
global_asm!(include_str!("entry.asm")); global_asm!(include_str!("entry.asm"));
global_asm!(include_str!("link_app.S")); global_asm!(include_str!("link_app.S"));
@ -33,6 +37,9 @@ fn clear_bss() {
pub fn rust_main() -> ! { pub fn rust_main() -> ! {
clear_bss(); clear_bss();
println!("[kernel] Hello, world!"); println!("[kernel] Hello, world!");
mm::init_heap();
mm::heap_test();
loop {}
trap::init(); trap::init();
loader::load_apps(); loader::load_apps();
trap::enable_interrupt(); trap::enable_interrupt();

View File

@ -0,0 +1,45 @@
use buddy_system_allocator::LockedHeap;
use crate::config::KERNEL_HEAP_SIZE;
#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
panic!("Heap allocation error, layout = {:?}", layout);
}
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
pub fn init_heap() {
unsafe {
HEAP_ALLOCATOR
.lock()
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
}
}
#[allow(unused)]
pub fn heap_test() {
use alloc::boxed::Box;
use alloc::vec::Vec;
extern "C" {
fn sbss();
fn ebss();
}
let bss_range = sbss as usize..ebss as usize;
let a = Box::new(5);
assert_eq!(*a, 5);
assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
drop(a);
let mut v: Vec<usize> = Vec::new();
for i in 0..500 {
v.push(i);
}
for i in 0..500 {
assert_eq!(v[i], i);
}
assert!(bss_range.contains(&(v.as_ptr() as usize)));
drop(v);
println!("heap_test passed!");
}

4
os/src/mm/mod.rs Normal file
View File

@ -0,0 +1,4 @@
mod heap_allocator;
pub use heap_allocator::init_heap;
pub use heap_allocator::heap_test;