Skip to content

Commit 66eee27

Browse files
committed
Init the ch4
1 parent bebe289 commit 66eee27

24 files changed

+1426
-189
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ easy-fs-fuse/target/*
1515
tools/
1616
pushall.sh
1717
*.bak
18+
1819
ci-user/
20+
os/vendor/
1921
user/
20-
os/vendor/

os/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ edition = "2021"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
bitflags = "1.2.1"
1011
buddy_system_allocator = "0.6"
1112
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
1213
log = "0.4"
1314
riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] }
15+
xmas-elf = "0.7.0"

os/build.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ fn main() {
99
insert_app_data().unwrap();
1010
}
1111

12-
static TARGET_PATH: &str = "../user/build/bin/";
12+
static TARGET_PATH: &str = "../user/build/elf/";
1313

1414
/// get app data and build linker
1515
fn insert_app_data() -> Result<()> {
1616
let mut f = File::create("src/link_app.S").unwrap();
17-
let mut apps: Vec<_> = read_dir("../user/build/bin/")
17+
let mut apps: Vec<_> = read_dir("../user/build/elf/")
1818
.unwrap()
1919
.into_iter()
2020
.map(|dir_entry| {
@@ -49,8 +49,9 @@ _num_app:
4949
.section .data
5050
.global app_{0}_start
5151
.global app_{0}_end
52+
.align 3
5253
app_{0}_start:
53-
.incbin "{2}{1}.bin"
54+
.incbin "{2}{1}.elf"
5455
app_{0}_end:"#,
5556
idx, app, TARGET_PATH
5657
)?;

os/src/config.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33
#[allow(unused)]
44

55
/// user app's stack size
6-
pub const USER_STACK_SIZE: usize = 4096;
6+
pub const USER_STACK_SIZE: usize = 4096 * 2;
77
/// kernel stack size
88
pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
99
/// kernel heap size
10-
pub const KERNEL_HEAP_SIZE: usize = 0x20000;
11-
/// the max number of apps
12-
pub const MAX_APP_NUM: usize = 16;
13-
/// base_addr(changed) of app
14-
pub const APP_BASE_ADDRESS: usize = 0x80400000;
15-
/// size limit of app
16-
pub const APP_SIZE_LIMIT: usize = 0x20000;
10+
pub const KERNEL_HEAP_SIZE: usize = 0x200_0000;
1711

12+
/// page size : 4KB
13+
pub const PAGE_SIZE: usize = 0x1000;
14+
/// page size bits: 12
15+
pub const PAGE_SIZE_BITS: usize = 0xc;
16+
/// the max number of syscall
17+
pub const MAX_SYSCALL_NUM: usize = 500;
18+
/// the virtual addr of trapoline
19+
pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1;
20+
/// the virtual addr of trap context
21+
pub const TRAP_CONTEXT_BASE: usize = TRAMPOLINE - PAGE_SIZE;
1822
/// clock frequency
1923
pub const CLOCK_FREQ: usize = 12500000;
2024
/// the physical memory end

os/src/linker.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ SECTIONS
1010
stext = .;
1111
.text : {
1212
*(.text.entry)
13+
. = ALIGN(4K);
14+
strampoline = .;
15+
*(.text.trampoline);
16+
. = ALIGN(4K);
1317
*(.text .text.*)
1418
}
1519

@@ -31,6 +35,7 @@ SECTIONS
3135

3236
. = ALIGN(4K);
3337
edata = .;
38+
sbss_with_stack = .;
3439
.bss : {
3540
*(.bss.stack)
3641
sbss = .;

os/src/loader.rs

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,4 @@
11
//! Loading user applications into memory
2-
//!
3-
//! For chapter 3, user applications are simply part of the data included in the
4-
//! kernel binary, so we only need to copy them to the space allocated for each
5-
//! app to load them. We also allocate fixed spaces for each task's
6-
//! [`KernelStack`] and [`UserStack`].
7-
8-
use crate::config::*;
9-
use crate::trap::TrapContext;
10-
use core::arch::asm;
11-
12-
#[repr(align(4096))]
13-
#[derive(Copy, Clone)]
14-
struct KernelStack {
15-
data: [u8; KERNEL_STACK_SIZE],
16-
}
17-
18-
#[repr(align(4096))]
19-
#[derive(Copy, Clone)]
20-
struct UserStack {
21-
data: [u8; USER_STACK_SIZE],
22-
}
23-
24-
static KERNEL_STACK: [KernelStack; MAX_APP_NUM] = [KernelStack {
25-
data: [0; KERNEL_STACK_SIZE],
26-
}; MAX_APP_NUM];
27-
28-
static USER_STACK: [UserStack; MAX_APP_NUM] = [UserStack {
29-
data: [0; USER_STACK_SIZE],
30-
}; MAX_APP_NUM];
31-
32-
impl KernelStack {
33-
fn get_sp(&self) -> usize {
34-
self.data.as_ptr() as usize + KERNEL_STACK_SIZE
35-
}
36-
pub fn push_context(&self, trap_cx: TrapContext) -> usize {
37-
let trap_cx_ptr = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext;
38-
unsafe {
39-
*trap_cx_ptr = trap_cx;
40-
}
41-
trap_cx_ptr as usize
42-
}
43-
}
44-
45-
impl UserStack {
46-
fn get_sp(&self) -> usize {
47-
self.data.as_ptr() as usize + USER_STACK_SIZE
48-
}
49-
}
50-
51-
/// Get base address of app i.
52-
fn get_base_i(app_id: usize) -> usize {
53-
APP_BASE_ADDRESS + app_id * APP_SIZE_LIMIT
54-
}
552
563
/// Get the total number of applications.
574
pub fn get_num_app() -> usize {
@@ -61,43 +8,19 @@ pub fn get_num_app() -> usize {
618
unsafe { (_num_app as usize as *const usize).read_volatile() }
629
}
6310

64-
/// Load nth user app at
65-
/// [APP_BASE_ADDRESS + n * APP_SIZE_LIMIT, APP_BASE_ADDRESS + (n+1) * APP_SIZE_LIMIT).
66-
pub fn load_apps() {
11+
/// get applications data
12+
pub fn get_app_data(app_id: usize) -> &'static [u8] {
6713
extern "C" {
6814
fn _num_app();
6915
}
7016
let num_app_ptr = _num_app as usize as *const usize;
7117
let num_app = get_num_app();
7218
let app_start = unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) };
73-
// load apps
74-
for i in 0..num_app {
75-
let base_i = get_base_i(i);
76-
// clear region
77-
(base_i..base_i + APP_SIZE_LIMIT)
78-
.for_each(|addr| unsafe { (addr as *mut u8).write_volatile(0) });
79-
// load app from data section to memory
80-
let src = unsafe {
81-
core::slice::from_raw_parts(app_start[i] as *const u8, app_start[i + 1] - app_start[i])
82-
};
83-
let dst = unsafe { core::slice::from_raw_parts_mut(base_i as *mut u8, src.len()) };
84-
dst.copy_from_slice(src);
85-
}
86-
// Memory fence about fetching the instruction memory
87-
// It is guaranteed that a subsequent instruction fetch must
88-
// observes all previous writes to the instruction memory.
89-
// Therefore, fence.i must be executed after we have loaded
90-
// the code of the next app into the instruction memory.
91-
// See also: riscv non-priv spec chapter 3, 'Zifencei' extension.
19+
assert!(app_id < num_app);
9220
unsafe {
93-
asm!("fence.i");
21+
core::slice::from_raw_parts(
22+
app_start[app_id] as *const u8,
23+
app_start[app_id + 1] - app_start[app_id],
24+
)
9425
}
9526
}
96-
97-
/// get app info with entry and sp and save `TrapContext` in kernel stack
98-
pub fn init_app_cx(app_id: usize) -> usize {
99-
KERNEL_STACK[app_id].push_context(TrapContext::app_init_context(
100-
get_base_i(app_id),
101-
USER_STACK[app_id].get_sp(),
102-
))
103-
}

os/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#![feature(panic_info_message)]
2323
#![feature(alloc_error_handler)]
2424

25+
#[macro_use]
26+
extern crate bitflags;
2527
#[macro_use]
2628
extern crate log;
2729

@@ -30,10 +32,10 @@ extern crate alloc;
3032
#[macro_use]
3133
mod console;
3234
pub mod config;
33-
mod heap_alloc;
3435
pub mod lang_items;
3536
mod loader;
3637
pub mod logging;
38+
pub mod mm;
3739
pub mod sbi;
3840
pub mod sync;
3941
pub mod syscall;
@@ -97,9 +99,10 @@ fn kernel_log_info() {
9799
pub fn rust_main() -> ! {
98100
clear_bss();
99101
kernel_log_info();
100-
heap_alloc::init_heap();
102+
mm::init();
103+
println!("[kernel] back to world!");
104+
mm::remap_test();
101105
trap::init();
102-
loader::load_apps();
103106
trap::enable_timer_interrupt();
104107
timer::set_next_trigger();
105108
task::run_first_task();

0 commit comments

Comments
 (0)