blob: bc5c5c0d620c3bf72fbf90d15965424169b6f5ef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#![no_std]
#[macro_use]
pub mod util;
pub mod allocators;
pub mod collections;
pub mod console;
pub mod device_tree;
pub mod drivers;
pub mod interrupts;
pub mod prelude;
#[cfg(not(test))]
mod panic;
use crate::prelude::*;
/// The entrypoint to the kernel. This should be executed by hart0 alone. It performs some early
/// boot tasks, then wakes up any other harts.
///
/// # Safety
///
/// - The `device_tree` pointer must be a valid pointer into physical memory. See
/// `device_tree::FlattenedDeviceTree::from_ptr` for the precise requirements.
/// - This must be called in supervisor mode with paging and traps disabled, but with all traps
/// delegated to supervisor mode.
/// - Any other harts must not be running concurrently with us. TODO: Define their state.
#[no_mangle]
pub unsafe extern "C" fn hart0_boot(device_tree: *const u8) -> ! {
// Set up the logger.
//
// TODO: This should really be named something better than console.
console::init();
// Parse the DeviceTree.
let flattened_device_tree = unsafe { device_tree::FlattenedDeviceTree::from_ptr(device_tree) }
.expect("invalid DeviceTree");
// Find the available physical memory areas and initialize the physical memory
// free-list.
flattened_device_tree
.for_each_node(|node| {
if node.is_unit(&["", "cpus"]) {
// TODO: Do this later.
if let Some(timebase_frequency) = node.get_prop_u32("timebase-frequency") {
// SAFETY: Other harts are not concurrently running, so they can't be
// concurrently accessing or modifying this.
unsafe {
drivers::riscv_timer::TIMEBASE_FREQUENCY = timebase_frequency;
}
}
} else if node.is_unit(&["", "memory"]) {
// Get the memory ranges.
let Some(reg) = node.get_reg_usize() else {
warn!("{}reg was not valid", node.name());
return Ok(());
};
for (addr, size) in reg {
info!("found memory: addr = {addr:#x}, size = {size:#x}");
}
}
Ok(())
})
.unwrap_or_else(|err| void::unreachable(err));
// After this point, everything else is for debugging.
interrupts::example_timer();
info!("sleeping forever...");
loop {
unsafe { core::arch::asm!("wfi") }
}
}
|