Skip to content

Commit a13aa87

Browse files
committed
ch5
1 parent 33c28aa commit a13aa87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1520
-655
lines changed

os/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ OBJCOPY := rust-objcopy --binary-architecture=riscv64
2626
# Disassembly
2727
DISASM ?= -x
2828

29+
# Run usertests or usershell
30+
TEST ?=
31+
2932
build: env $(KERNEL_BIN)
3033

3134
env:
@@ -38,6 +41,7 @@ $(KERNEL_BIN): kernel
3841
@$(OBJCOPY) $(KERNEL_ELF) $(OBJCOPY_ARG) -O binary $@
3942

4043
kernel:
44+
@cd ../user && make build TEST=$(TEST)
4145
@echo Platform: $(BOARD)
4246
@cp src/linker-$(BOARD).ld src/linker.ld
4347
@cargo build $(MODE_ARG)

os/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ _num_app:
4545
}
4646
writeln!(f, " .quad app_{}_end", apps.len() - 1)?;
4747

48+
writeln!(
49+
f,
50+
r#" .globl _app_names
51+
_app_names:"#,
52+
)?;
53+
for app in &apps {
54+
writeln!(f, r#" .string "{}""#, app)?;
55+
}
56+
4857
for (i, app) in apps.iter().enumerate() {
4958
println!("+app_{}: {}", i, app);
5059
writeln!(
@@ -53,6 +62,7 @@ _num_app:
5362
.section .data
5463
.globl app_{0}_start
5564
.globl app_{0}_end
65+
.align 3
5666
app_{0}_start:
5767
.incbin "{1}/{2}.elf"
5868
app_{0}_end:"#,

os/src/config.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,3 @@ pub const PAGE_SIZE: usize = 0x1000;
1212
pub const PAGE_SIZE_BITS: usize = 12;
1313
pub const TRAMPOLINE: usize = usize::MAX - PAGE_SIZE + 1;
1414
pub const TRAP_CONTEXT: usize = TRAMPOLINE - PAGE_SIZE;
15-
16-
/// Return (bottom, top) of a kernel stack in kernel space.
17-
pub fn kernel_stack_position(app_id: usize) -> (usize, usize) {
18-
// 每个app的kernel stack大小固定, 且从高到低依次排列
19-
// 每个app间使用一个guard page来分隔, 即这里为什么要+ PAGE_SIZE
20-
let top = TRAMPOLINE - app_id * (KERNEL_STACK_SIZE + PAGE_SIZE);
21-
let bottom = top - KERNEL_STACK_SIZE;
22-
(bottom, top)
23-
}

os/src/lang_item.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::panic::PanicInfo;
22

3-
use crate::{sbi::shutdown, stack_trace::print_stack_trace};
3+
use crate::sbi::shutdown;
44

55
#[panic_handler]
66
fn panic(info: &PanicInfo) -> ! {
@@ -14,10 +14,5 @@ fn panic(info: &PanicInfo) -> ! {
1414
} else {
1515
log::error!("Panicked: {}", info.message());
1616
}
17-
18-
unsafe {
19-
print_stack_trace();
20-
}
21-
2217
shutdown(true)
2318
}

os/src/loader.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1+
use alloc::vec::Vec;
2+
use lazy_static::lazy_static;
3+
14
extern "C" {
25
fn _num_app();
6+
fn _app_names();
7+
}
8+
9+
lazy_static! {
10+
static ref APP_NAMES: Vec<&'static str> = {
11+
let num_app = get_num_app();
12+
let mut start = _app_names as usize as *const u8;
13+
let mut vs = Vec::new();
14+
unsafe {
15+
for _ in 0..num_app {
16+
let mut end = start;
17+
while end.read_volatile() != '\0' as u8 {
18+
end = end.add(1);
19+
}
20+
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
21+
let str = core::str::from_utf8(slice).unwrap();
22+
vs.push(str);
23+
start = end.add(1);
24+
}
25+
}
26+
vs
27+
};
328
}
429

530
/// Get the total number of applications.
@@ -18,3 +43,20 @@ pub fn get_app_data(i: usize) -> &'static [u8] {
1843
core::slice::from_raw_parts(app_start[i] as *const u8, app_start[i + 1] - app_start[i])
1944
}
2045
}
46+
47+
/// Get app data(in elf) by name
48+
pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> {
49+
let num_app = get_num_app();
50+
(0..num_app)
51+
.find(|&i| APP_NAMES[i] == name)
52+
.map(get_app_data)
53+
}
54+
55+
/// List loaded apps
56+
pub fn list_apps() {
57+
println!("/**** APPS ****");
58+
for app in APP_NAMES.iter() {
59+
println!("{}", app);
60+
}
61+
println!("**************/");
62+
}

os/src/logging.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ impl Log for NaiveLogger {
1414
return;
1515
}
1616
print_color(
17-
format_args!("[{:>5}] {}\n", record.level(), record.args()),
17+
format_args!(
18+
"[{} {}:{}] {}\n",
19+
record.level(),
20+
record.file().unwrap_or("??.rs"),
21+
record.line().unwrap_or(0),
22+
record.args()
23+
),
1824
color_code_for_level(&record.level()),
1925
);
2026
}

os/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![no_std]
33
#![feature(array_windows)]
44
#![feature(alloc_error_handler)]
5+
#![feature(slice_split_once)]
56

67
extern crate alloc;
78
extern crate bitflags;
@@ -16,7 +17,6 @@ mod loader;
1617
mod logging;
1718
mod mm;
1819
mod sbi;
19-
mod stack_trace;
2020
mod sync;
2121
mod syscall;
2222
mod task;
@@ -89,16 +89,17 @@ pub fn rust_main() -> ! {
8989
config::MEMORY_END,
9090
);
9191

92-
log::info!("[kernel] hello!");
9392
mm::init();
94-
log::info!("[kernel] back to world!");
9593
mm::remap_test();
9694

95+
task::add_initproc();
96+
9797
trap::init();
9898
trap::enable_timer_interrupt();
9999
timer::set_next_trigger();
100100

101-
task::run_first_task();
101+
loader::list_apps();
102+
task::run_tasks();
102103
panic!("Unreachable in rust_main!");
103104
}
104105

os/src/mm/address.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl PhysAddr {
7878
pub fn ceil(&self) -> PhysPageNum {
7979
PhysPageNum((self.0 + PAGE_SIZE - 1) / PAGE_SIZE)
8080
}
81+
82+
pub fn get_mut<T>(&self) -> &'static mut T {
83+
unsafe { (self.0 as *mut T).as_mut().unwrap() }
84+
}
8185
}
8286

8387
// usize <-> PhysPageNum

os/src/mm/frame_allocator.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ pub struct StackFrameAllocator {
9898

9999
impl StackFrameAllocator {
100100
pub fn init(&mut self, l: PhysPageNum, r: PhysPageNum) {
101-
// log::debug!(
102-
// "[kernel] phys_frames: [{:?}, {:?}) (total = {})",
103-
// l,
104-
// r,
105-
// r.0 - l.0
106-
// );
107101
self.current = l.0;
108102
self.end = r.0;
109103
}

os/src/mm/heap_allocator.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
1515

1616
pub fn init_heap() {
1717
let start = unsafe { HEAP_SPACE.as_ptr() as usize };
18-
log::info!(
19-
"[kernel] {:<10} [{:#x}, {:#x})",
20-
"heap",
21-
start,
22-
start + KERNEL_HEAP_SIZE
23-
);
2418
unsafe {
2519
HEAP_ALLOCATOR.lock().init(start, KERNEL_HEAP_SIZE);
2620
}

0 commit comments

Comments
 (0)