mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 01:35:19 +08:00
Compare commits
9 Commits
93c4d3c5fb
...
41208c1e15
Author | SHA1 | Date | |
---|---|---|---|
41208c1e15 | |||
2ec3859da7 | |||
92801fccdc | |||
72a04f2905 | |||
d30f87e8e7 | |||
d8c1ba45ed | |||
78133158b6 | |||
6eb3a062c1 | |||
56fc041f9d |
3
Makefile
3
Makefile
@ -31,7 +31,8 @@ QEMU = qemu-system-$(ARCH)
|
||||
QEMU_ARGS = -nographic -serial mon:stdio -smp 1
|
||||
QEMU_ARGS += -machine virt # TODO: override by $(BOARD)
|
||||
QEMU_ARGS += -kernel target/$(BUILD_TARGET)/$(MODE)/kernel
|
||||
QEMU_ARGS += -initrd build/init.cpio
|
||||
QEMU_ARGS += -initrd root/init.cpio
|
||||
QEMU_ARGS += -append "loglevel=4"
|
||||
|
||||
kernel:
|
||||
env RUSTFLAGS="$(RUSTFLAGS)" cargo build --bin kernel $(CARGO_BUILD_ARGS)
|
||||
|
@ -121,6 +121,10 @@ impl TrapContextOps for TrapContext {
|
||||
assert!(index < 8, "TrapContext set_reg index out of range");
|
||||
self.gprs[index + 9] = value;
|
||||
}
|
||||
|
||||
fn set_pc(&mut self, value: usize) {
|
||||
self.sepc = value;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
|
@ -106,7 +106,7 @@ pub unsafe fn setup_kernel_paging() {
|
||||
info!("Setting up kernel paging");
|
||||
assert!(!is_kernel_pagetable_allocated(), "Kernel pagetable already allocated");
|
||||
|
||||
let root_pt = alloc_page();
|
||||
let root_pt = alloc_page(VirtAddr(0));
|
||||
let mut kernel_pt = Table::<Level0>::new(mmap_phys_to_virt(root_pt));
|
||||
|
||||
macro_rules! map_section {
|
||||
|
@ -16,7 +16,7 @@ fn alloc_callback() {
|
||||
}
|
||||
|
||||
#[cfg_attr(debug_assertions, tracer::trace_callback(log = true, callback = alloc_callback))]
|
||||
pub fn alloc_page() -> PhysAddr {
|
||||
pub fn alloc_page(_mapped_addr: VirtAddr) -> PhysAddr {
|
||||
let addr = RAM_ALLOCATOR.lock().alloc(PAGE_LAYOUT).expect("Failed to allocate page");
|
||||
|
||||
unsafe {
|
||||
@ -29,7 +29,7 @@ pub fn alloc_page() -> PhysAddr {
|
||||
}
|
||||
|
||||
pub fn map_range(
|
||||
mut alloc: impl FnMut() -> PhysAddr,
|
||||
mut alloc: impl FnMut(VirtAddr) -> PhysAddr,
|
||||
pt: &mut Table<Level0>,
|
||||
from: VirtAddr,
|
||||
to: PhysAddr,
|
||||
@ -63,8 +63,8 @@ pub fn map_range(
|
||||
unsafe {
|
||||
Table::<$level>::new(mmap_phys_to_virt(
|
||||
$pt.lookup_mut($addr).map(|e| e.paddr()).unwrap_or_else(|| {
|
||||
let page = alloc();
|
||||
let addr = $addr.align_down(<$level as TableLevel>::PREVIOUS::LEVEL_SIZE);
|
||||
let page = alloc(addr);
|
||||
debug!("Creating new pagetable:\t{:?} -> {:?}", addr, page);
|
||||
$pt.map(addr, page, MapAttr::PAGE_TABLE).unwrap();
|
||||
page
|
||||
|
@ -1,7 +1,9 @@
|
||||
use crate::logging::set_log_level;
|
||||
use crate::plat::console::{set_console, ConsoleDevice, ConsoleDriver, CONSOLE};
|
||||
use crate::plat::lowlevel::{Hardware, LowLevel};
|
||||
use crate::plat::timer::{Timer, TimerOps};
|
||||
use crate::plat::trap::{Trap, TrapOps};
|
||||
use crate::root::setup_root_server;
|
||||
use crate::scheduler::{IDLE_THREAD, SCHEDULER};
|
||||
use core::cell::Cell;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
@ -19,7 +21,9 @@ pub fn rust_main() -> ! {
|
||||
|
||||
N_CPUS.store(fdt.cpus().count(), Ordering::Release);
|
||||
|
||||
set_log_level(&fdt);
|
||||
setup_console(&fdt);
|
||||
|
||||
info!("Kernel Started");
|
||||
|
||||
#[cfg(test)]
|
||||
@ -28,33 +32,16 @@ pub fn rust_main() -> ! {
|
||||
crate::test_main();
|
||||
}
|
||||
|
||||
// TODO: setup and start scheduler
|
||||
|
||||
// --- some dummy tests ---
|
||||
|
||||
Trap::init();
|
||||
Timer::init();
|
||||
|
||||
SCHEDULER.init();
|
||||
SCHEDULER.add(&IDLE_THREAD);
|
||||
|
||||
setup_root_server(&fdt);
|
||||
|
||||
SCHEDULER.schedule();
|
||||
|
||||
loop {
|
||||
let data = CONSOLE.lock().try_read();
|
||||
match data {
|
||||
Some(ch) => {
|
||||
debug!("Key: {}", ch as char);
|
||||
if ch == b'q' {
|
||||
break;
|
||||
}
|
||||
},
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
|
||||
// --- end ---
|
||||
|
||||
error!("[rust_main] Should not reach here! Maybe scheduler is not working?");
|
||||
Hardware::shutdown(true);
|
||||
}
|
||||
|
@ -2,13 +2,14 @@ use crate::plat::console::CONSOLE;
|
||||
use crate::{entry::HART_ID, plat::timer::CURRENT_TICK};
|
||||
use core::fmt::Write;
|
||||
use core::sync::atomic::Ordering;
|
||||
use fdt::Fdt;
|
||||
use log::{LevelFilter, Log, Metadata, Record};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => {{
|
||||
use core::fmt::Write;
|
||||
use crate::plat::console::CONSOLE;
|
||||
use $crate::plat::console::CONSOLE;
|
||||
CONSOLE.lock().write_fmt(format_args!($($arg)*)).unwrap();
|
||||
}};
|
||||
}
|
||||
@ -59,6 +60,34 @@ impl Log for SimpleLogger {
|
||||
pub fn init() {
|
||||
log::set_logger(&SimpleLogger).unwrap();
|
||||
|
||||
// TODO: get log level from boot env
|
||||
// Always log everything before the FDT is parsed
|
||||
log::set_max_level(LevelFilter::Trace);
|
||||
}
|
||||
|
||||
pub fn set_log_level(fdt: &Fdt) {
|
||||
const UNKNOWN_LEVEL: usize = 5;
|
||||
|
||||
let log_level = fdt
|
||||
.chosen()
|
||||
.bootargs()
|
||||
.map(|args| args.split_ascii_whitespace())
|
||||
.map(|args| args.filter(|arg| arg.starts_with("loglevel=")))
|
||||
.map(|args| args.map(|arg| arg["loglevel=".len()..].parse::<usize>()))
|
||||
.and_then(|args| args.last())
|
||||
.map(|level| level.unwrap_or(UNKNOWN_LEVEL))
|
||||
.unwrap_or(UNKNOWN_LEVEL);
|
||||
|
||||
let level = match log_level {
|
||||
0 => LevelFilter::Error,
|
||||
1 => LevelFilter::Warn,
|
||||
2 => LevelFilter::Info,
|
||||
3 => LevelFilter::Debug,
|
||||
4 => LevelFilter::Trace,
|
||||
#[cfg(debug_assertions)]
|
||||
_ => LevelFilter::Trace,
|
||||
#[cfg(not(debug_assertions))]
|
||||
_ => LevelFilter::Error,
|
||||
};
|
||||
|
||||
log::set_max_level(level);
|
||||
}
|
||||
|
@ -2,8 +2,7 @@ use crate::arch::{layout::mmap_phys_to_virt, trap::TrapContext, vspace::*};
|
||||
use crate::{objects::*, plat::trap::TrapContextOps, vspace::*};
|
||||
use core::fmt::Debug;
|
||||
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||
use log::debug;
|
||||
use uapi::{cap::ObjectType, error::SysResult, fault::Fault, syscall::MessageInfo};
|
||||
use uapi::{cap::ObjectType, error::SysResult, fault::*, syscall::*};
|
||||
use utils::{
|
||||
addr::*,
|
||||
linked_list::{Link, LinkHelper},
|
||||
@ -60,6 +59,19 @@ impl TcbObject {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_cspace(&mut self, cspace: CapEntry) {
|
||||
CNodeCap::try_from(&cspace).expect("set_cspace: invalid cap");
|
||||
self.cspace = cspace;
|
||||
}
|
||||
|
||||
pub fn set_vspace(&mut self, vspace: CapEntry) {
|
||||
let cap = TableCap::try_from(&vspace).expect("set_vspace: invalid cap");
|
||||
cap.set_mapped(true);
|
||||
cap.set_mapped_asid(self.tid);
|
||||
cap.set_mapped_vaddr(VirtAddr(0));
|
||||
self.vspace = vspace;
|
||||
}
|
||||
|
||||
pub fn configure_idle_thread(&mut self) {
|
||||
self.trapframe.configure_idle_thread();
|
||||
self.vspace = CapEntry::new(TableCap::mint(unsafe { get_current_pagetable().paddr() }));
|
||||
|
@ -16,4 +16,5 @@ pub trait TrapContextOps {
|
||||
|
||||
fn get_reg(&self, index: usize) -> usize;
|
||||
fn set_reg(&mut self, index: usize, value: usize);
|
||||
fn set_pc(&mut self, value: usize);
|
||||
}
|
||||
|
@ -1,16 +1,21 @@
|
||||
use crate::arch::layout::{mmap_phys_to_virt, mmap_virt_to_phys, PAGE_BITS, PAGE_SIZE};
|
||||
use crate::arch::vspace::{copy_kernel_pagetable, map_range, RAM_ALLOCATOR};
|
||||
use crate::arch::FDT;
|
||||
use crate::objects::*;
|
||||
use crate::plat::trap::TrapContextOps;
|
||||
use crate::scheduler::SCHEDULER;
|
||||
use crate::vspace::*;
|
||||
use core::{cmp::min, ops::Range};
|
||||
use core::{cmp::min, ops::Range, sync::atomic::Ordering};
|
||||
use elf::{abi::*, endian::AnyEndian, ElfBytes};
|
||||
use fdt::Fdt;
|
||||
use log::{debug, trace};
|
||||
use spin::Lazy;
|
||||
use uapi::{cspace::CNodeSlot, error::SysError};
|
||||
use uapi::{consts::*, cspace::CNodeSlot, error::SysError};
|
||||
use utils::{addr::*, bin::prev_power_of_two};
|
||||
|
||||
const ROOT_ASID: usize = 1;
|
||||
const ROOT_CNODE_SIZE: usize = 128;
|
||||
|
||||
static mut ROOT_CNODE: Lazy<[CapEntry; ROOT_CNODE_SIZE]> =
|
||||
Lazy::new(|| core::array::from_fn(|_| CapEntry::new(NullCap::mint())));
|
||||
|
||||
@ -30,25 +35,32 @@ pub fn setup_root_server(fdt: &Fdt) {
|
||||
let cnode_obj = cnode.as_object_mut();
|
||||
cnode_obj[CNodeSlot::CNodeCap as usize] = cnode_entry.clone();
|
||||
|
||||
// 2. insert current free memory [CNodeSlot::UntypedCap, memory_idx)
|
||||
let memory_idx = create_untyped_memory(cnode_obj);
|
||||
let mut free_idx = CNodeSlot::UntypedCap as usize;
|
||||
|
||||
// 3. insert device memory [memory_idx, device_idx)
|
||||
let device_idx = create_untyped_device(cnode_obj, memory_idx, fdt);
|
||||
// 2. insert current free memory
|
||||
create_untyped_memory(cnode_obj, &mut free_idx);
|
||||
|
||||
// 3. insert device memory
|
||||
create_untyped_device(cnode_obj, &mut free_idx, fdt);
|
||||
|
||||
// 4. create objects
|
||||
create_objects(cnode_obj, device_idx);
|
||||
create_objects(cnode_obj, &mut free_idx, &cnode_entry);
|
||||
|
||||
// 5. load root server
|
||||
let root_server_idx = load_root_server(cnode_obj, device_idx, fdt);
|
||||
load_root_server(cnode_obj, &mut free_idx, fdt);
|
||||
|
||||
// 6. map other memory (fdt, initrd)
|
||||
map_misc(cnode_obj, &mut free_idx, fdt);
|
||||
|
||||
// 7. setup threads
|
||||
// 7. insert into scheduler
|
||||
SCHEDULER.add(
|
||||
TcbCap::try_from(&cnode_obj[CNodeSlot::TcbCap as usize])
|
||||
.unwrap()
|
||||
.as_object_mut(),
|
||||
);
|
||||
}
|
||||
|
||||
fn create_untyped_memory(cnode_obj: &mut CNodeObject) -> usize {
|
||||
let mut memory_idx = CNodeSlot::UntypedCap as usize;
|
||||
fn create_untyped_memory(cnode_obj: &mut CNodeObject, free_idx: &mut usize) {
|
||||
let mut ram = RAM_ALLOCATOR.lock();
|
||||
ram.blocks_mut()
|
||||
.iter_mut()
|
||||
@ -87,25 +99,22 @@ fn create_untyped_memory(cnode_obj: &mut CNodeObject) -> usize {
|
||||
|
||||
trace!(
|
||||
"[root] creating untyped memory: {}: {:?} -> {:?}, slot #{}",
|
||||
memory_idx,
|
||||
free_idx,
|
||||
start,
|
||||
start + (1 << bits),
|
||||
memory_idx
|
||||
free_idx
|
||||
);
|
||||
|
||||
let cap = UntypedCap::mint(0, bits, false, start);
|
||||
let cap_entry = CapEntry::new(cap);
|
||||
cnode_obj[memory_idx] = cap_entry;
|
||||
memory_idx += 1;
|
||||
cnode_obj[*free_idx] = cap_entry;
|
||||
*free_idx += 1;
|
||||
});
|
||||
|
||||
assert!(ram.blocks().iter().all(|b| b.is_none()));
|
||||
|
||||
memory_idx
|
||||
}
|
||||
|
||||
fn create_untyped_device(cnode_obj: &mut CNodeObject, memory_idx: usize, fdt: &Fdt) -> usize {
|
||||
let mut device_idx = memory_idx;
|
||||
fn create_untyped_device(cnode_obj: &mut CNodeObject, free_idx: &mut usize, fdt: &Fdt) {
|
||||
let memory = fdt.memory();
|
||||
|
||||
fdt.all_nodes().filter_map(|n| n.reg()).for_each(|r| {
|
||||
@ -134,25 +143,23 @@ fn create_untyped_device(cnode_obj: &mut CNodeObject, memory_idx: usize, fdt: &F
|
||||
|
||||
trace!(
|
||||
"[root] creating untyped device: {}: {:?} -> {:?}, slot #{}",
|
||||
device_idx,
|
||||
free_idx,
|
||||
start,
|
||||
start + (1 << bits),
|
||||
device_idx
|
||||
free_idx
|
||||
);
|
||||
|
||||
let cap = UntypedCap::mint(0, bits, true, start);
|
||||
let cap_entry = CapEntry::new(cap);
|
||||
cnode_obj[device_idx] = cap_entry;
|
||||
device_idx += 1;
|
||||
cnode_obj[*free_idx] = cap_entry;
|
||||
*free_idx += 1;
|
||||
})
|
||||
});
|
||||
|
||||
device_idx
|
||||
}
|
||||
|
||||
fn alloc_objects<T: KernelObject + ?Sized>(
|
||||
cnode_obj: &mut CNodeObject,
|
||||
end_idx: usize,
|
||||
free_idx: usize,
|
||||
user_obj_bits: usize,
|
||||
range: Range<usize>,
|
||||
) {
|
||||
@ -160,12 +167,12 @@ fn alloc_objects<T: KernelObject + ?Sized>(
|
||||
|
||||
let (memory, slots) = {
|
||||
let (prologue, remain) = cnode_obj.split_at_mut(untyped_start);
|
||||
let (memory, epilogue) = remain.split_at_mut(end_idx - untyped_start);
|
||||
let (memory, epilogue) = remain.split_at_mut(free_idx - untyped_start);
|
||||
|
||||
match range {
|
||||
r if r.end <= untyped_start => (memory, &mut prologue[r]),
|
||||
r if r.start >= end_idx => (memory, &mut epilogue[(r.start - end_idx)..(r.end - end_idx)]),
|
||||
_ => panic!("init: slot range overlaps with untyped memory"),
|
||||
r if r.start >= free_idx => (memory, &mut epilogue[(r.start - free_idx)..(r.end - free_idx)]),
|
||||
_ => panic!("init: slot range overlaps with allocated objects"),
|
||||
}
|
||||
};
|
||||
|
||||
@ -187,67 +194,101 @@ fn alloc_objects<T: KernelObject + ?Sized>(
|
||||
panic!("init: untyped memory is exhausted");
|
||||
}
|
||||
|
||||
fn create_objects(cnode_obj: &mut CNodeObject, end_idx: usize) {
|
||||
// tcb
|
||||
{
|
||||
let tcb_idx = CNodeSlot::TcbCap as usize;
|
||||
trace!("[root] allocating tcb object at slot #{}", tcb_idx);
|
||||
alloc_objects::<TcbObject>(cnode_obj, end_idx, 0, tcb_idx..tcb_idx + 1);
|
||||
}
|
||||
|
||||
fn create_objects(cnode_obj: &mut CNodeObject, free_idx: &mut usize, cnode_entry: &CapEntry) {
|
||||
// vspace
|
||||
{
|
||||
// root
|
||||
let vspace_idx = CNodeSlot::VSpaceCap as usize;
|
||||
trace!("[root] allocating vspace object at slot #{}", vspace_idx);
|
||||
alloc_objects::<TableObject>(cnode_obj, end_idx, PAGE_BITS, vspace_idx..vspace_idx + 1);
|
||||
alloc_objects::<TableObject>(cnode_obj, *free_idx, PAGE_BITS, vspace_idx..vspace_idx + 1);
|
||||
|
||||
// copy pagetable
|
||||
let root = &cnode_obj[vspace_idx];
|
||||
copy_kernel_pagetable(root);
|
||||
}
|
||||
|
||||
// bootinfo
|
||||
// tcb
|
||||
{
|
||||
let bootinfo_idx = CNodeSlot::BootInfoFrameCap as usize;
|
||||
trace!("[root] allocating bootinfo object at slot #{}", bootinfo_idx);
|
||||
alloc_objects::<FrameObject>(cnode_obj, end_idx, PAGE_BITS, bootinfo_idx..bootinfo_idx + 1);
|
||||
let tcb_idx = CNodeSlot::TcbCap as usize;
|
||||
trace!("[root] allocating tcb object at slot #{}", tcb_idx);
|
||||
alloc_objects::<TcbObject>(cnode_obj, *free_idx, 0, tcb_idx..tcb_idx + 1);
|
||||
|
||||
let tcb = &cnode_obj[CNodeSlot::TcbCap as usize];
|
||||
let vspace = &cnode_obj[CNodeSlot::VSpaceCap as usize];
|
||||
|
||||
let mut tcb_cap = TcbCap::try_from(tcb).unwrap();
|
||||
let tcb = tcb_cap.as_object_mut();
|
||||
|
||||
*tcb = TcbObject::new();
|
||||
assert!(tcb.tid() == ROOT_ASID, "init: root tcb tid is invalid");
|
||||
|
||||
tcb.set_cspace(cnode_entry.clone());
|
||||
tcb.set_vspace(vspace.clone());
|
||||
tcb.set_state(tcb::ThreadState::Idle);
|
||||
}
|
||||
}
|
||||
|
||||
fn load_root_server(cnode_obj: &mut CNodeObject, end_idx: usize, fdt: &Fdt) -> usize {
|
||||
let mut table = {
|
||||
let root = &cnode_obj[CNodeSlot::VSpaceCap as usize];
|
||||
macro_rules! get_root_table {
|
||||
($cnode_obj:expr) => {{
|
||||
let root = &$cnode_obj[CNodeSlot::VSpaceCap as usize];
|
||||
let root_vaddr = TableCap::try_from(root).unwrap().as_object::<Level0>().vaddr();
|
||||
|
||||
// use unsafe to workaround borrow checker
|
||||
unsafe { Table::new(root_vaddr) }
|
||||
}};
|
||||
}
|
||||
|
||||
// TODO: figure out a better way to create a `load_page` function for `map_range`
|
||||
macro_rules! create_load_page {
|
||||
($cnode_obj:expr, $free_idx:expr) => {
|
||||
|mapped_addr| {
|
||||
trace!("[root] allocating pagetable at slot #{}", $free_idx);
|
||||
|
||||
alloc_objects::<TableObject>($cnode_obj, *$free_idx, PAGE_BITS, *$free_idx..*$free_idx + 1);
|
||||
*$free_idx += 1;
|
||||
|
||||
let page = &$cnode_obj[*$free_idx - 1];
|
||||
let page = TableCap::try_from(page).unwrap();
|
||||
|
||||
page.set_mapped(true);
|
||||
page.set_mapped_asid(ROOT_ASID);
|
||||
page.set_mapped_vaddr(mapped_addr);
|
||||
|
||||
let table: Table<Level0> = page.as_object(); // Any level is fine
|
||||
|
||||
table.paddr()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let mut rootfs = unsafe {
|
||||
let chosen = fdt.find_node("/chosen").expect("/chosen is required");
|
||||
fn get_rootfs(fdt: &Fdt) -> cpio::Reader<'static> {
|
||||
let chosen = fdt.find_node("/chosen").expect("/chosen is required");
|
||||
|
||||
let initrd_start = chosen
|
||||
.properties()
|
||||
.find(|n| n.name == "linux,initrd-start")
|
||||
.expect("linux,initrd-start is required")
|
||||
.as_usize()
|
||||
.unwrap();
|
||||
let initrd_start = chosen
|
||||
.properties()
|
||||
.find(|n| n.name == "linux,initrd-start")
|
||||
.expect("linux,initrd-start is required")
|
||||
.as_usize()
|
||||
.unwrap();
|
||||
|
||||
let initrd_end = chosen
|
||||
.properties()
|
||||
.find(|n| n.name == "linux,initrd-end")
|
||||
.expect("linux,initrd-end is required")
|
||||
.as_usize()
|
||||
.unwrap();
|
||||
let initrd_end = chosen
|
||||
.properties()
|
||||
.find(|n| n.name == "linux,initrd-end")
|
||||
.expect("linux,initrd-end is required")
|
||||
.as_usize()
|
||||
.unwrap();
|
||||
|
||||
let vaddr = mmap_phys_to_virt(PhysAddr(initrd_start));
|
||||
let len = initrd_end - initrd_start;
|
||||
let vaddr = unsafe { mmap_phys_to_virt(PhysAddr(initrd_start)) };
|
||||
let len = initrd_end - initrd_start;
|
||||
|
||||
let data = core::slice::from_raw_parts(vaddr.as_const_ptr(), len);
|
||||
cpio::Reader::new(data)
|
||||
};
|
||||
let data = unsafe { core::slice::from_raw_parts(vaddr.as_const_ptr(), len) };
|
||||
cpio::Reader::new(data)
|
||||
}
|
||||
|
||||
fn load_root_server(cnode_obj: &mut CNodeObject, free_idx: &mut usize, fdt: &Fdt) {
|
||||
let mut table = get_root_table!(cnode_obj);
|
||||
|
||||
let mut rootfs = get_rootfs(fdt);
|
||||
let root_entry = rootfs
|
||||
.find_map(|entry| {
|
||||
if let Ok(entry) = entry {
|
||||
@ -261,7 +302,6 @@ fn load_root_server(cnode_obj: &mut CNodeObject, end_idx: usize, fdt: &Fdt) -> u
|
||||
|
||||
let root_elf = ElfBytes::<AnyEndian>::minimal_parse(root_entry.data()).expect("init: invalid root server");
|
||||
|
||||
let mut nxt_slot = end_idx;
|
||||
root_elf
|
||||
.segments()
|
||||
.expect("init: root server has no segments")
|
||||
@ -306,16 +346,16 @@ fn load_root_server(cnode_obj: &mut CNodeObject, end_idx: usize, fdt: &Fdt) -> u
|
||||
debug_assert!(block_size >= PAGE_SIZE);
|
||||
|
||||
// allocate frame
|
||||
trace!("[initrd] allocating frame at slot #{}", nxt_slot);
|
||||
alloc_objects::<FrameObject>(cnode_obj, end_idx, block_bits, nxt_slot..nxt_slot + 1);
|
||||
nxt_slot += 1;
|
||||
trace!("[initrd] allocating frame at slot #{}", free_idx);
|
||||
alloc_objects::<FrameObject>(cnode_obj, *free_idx, block_bits, *free_idx..*free_idx + 1);
|
||||
*free_idx += 1;
|
||||
|
||||
// copy data
|
||||
let mut frame = FrameCap::try_from(&cnode_obj[nxt_slot - 1]).unwrap();
|
||||
let mut frame = FrameCap::try_from(&cnode_obj[*free_idx - 1]).unwrap();
|
||||
let mem = frame.as_object_mut();
|
||||
copy(cur, mem);
|
||||
|
||||
// map frame to vspace
|
||||
// map attributes
|
||||
let vaddr = seg_addr + cur;
|
||||
let paddr = unsafe { mmap_virt_to_phys(VirtAddr::from(mem as *const _ as *const u8)) };
|
||||
let attr = {
|
||||
@ -334,19 +374,12 @@ fn load_root_server(cnode_obj: &mut CNodeObject, end_idx: usize, fdt: &Fdt) -> u
|
||||
attr
|
||||
};
|
||||
|
||||
let alloc_page = || {
|
||||
trace!("[initrd] allocating pagetable at slot #{}", nxt_slot);
|
||||
|
||||
alloc_objects::<TableObject>(cnode_obj, end_idx, PAGE_BITS, nxt_slot..nxt_slot + 1);
|
||||
nxt_slot += 1;
|
||||
|
||||
let page = &cnode_obj[nxt_slot - 1];
|
||||
let page = TableCap::try_from(page).unwrap();
|
||||
let table: Table<Level0> = page.as_object(); // Any level is fine
|
||||
|
||||
unsafe { mmap_virt_to_phys(table.vaddr()) }
|
||||
};
|
||||
// manually set frame object
|
||||
frame.set_mapped_asid(ROOT_ASID);
|
||||
frame.set_mapped_vaddr(vaddr);
|
||||
|
||||
// map to vspace
|
||||
let alloc_page = create_load_page!(cnode_obj, free_idx);
|
||||
map_range(alloc_page, &mut table, vaddr, paddr, block_size, attr);
|
||||
|
||||
// update pointer
|
||||
@ -354,5 +387,40 @@ fn load_root_server(cnode_obj: &mut CNodeObject, end_idx: usize, fdt: &Fdt) -> u
|
||||
}
|
||||
});
|
||||
|
||||
nxt_slot
|
||||
let mut tcb_cap = TcbCap::try_from(&cnode_obj[CNodeSlot::TcbCap as usize]).unwrap();
|
||||
let tcb = tcb_cap.as_object_mut();
|
||||
tcb.trapframe.set_pc(root_elf.ehdr.e_entry as usize);
|
||||
}
|
||||
|
||||
fn map_misc(cnode_obj: &mut CNodeObject, free_idx: &mut usize, fdt: &Fdt) {
|
||||
// NOTE: there are no FrameObjects referencing these memory regions
|
||||
|
||||
let mut table = get_root_table!(cnode_obj);
|
||||
let attr = MapAttr::USER_ACCESSIBLE | MapAttr::READABLE;
|
||||
|
||||
// map fdt
|
||||
{
|
||||
assert!(fdt.total_size() < ROOT_FDT_MAX_SIZE, "init: fdt is too large");
|
||||
|
||||
let fdt_vaddr = FDT.load(Ordering::Relaxed);
|
||||
let fdt_vaddr = VirtAddr::from(fdt_vaddr);
|
||||
let fdt_paddr = unsafe { mmap_virt_to_phys(fdt_vaddr) };
|
||||
let fdt_size = align_up(fdt.total_size(), PAGE_SIZE);
|
||||
|
||||
let alloc_page = create_load_page!(cnode_obj, free_idx);
|
||||
map_range(alloc_page, &mut table, ROOT_FDT_ADDR, fdt_paddr, fdt_size, attr);
|
||||
}
|
||||
|
||||
// map rootfs
|
||||
{
|
||||
let rootfs = get_rootfs(fdt);
|
||||
assert!(rootfs.data().len() < ROOT_INITRD_MAX_SIZE, "init: initrd is too large");
|
||||
|
||||
let initrd_vaddr = VirtAddr::from(rootfs.data().as_ptr());
|
||||
let initrd_paddr = unsafe { mmap_virt_to_phys(initrd_vaddr) };
|
||||
let initrd_size = align_up(rootfs.data().len(), PAGE_SIZE);
|
||||
|
||||
let alloc_page = create_load_page!(cnode_obj, free_idx);
|
||||
map_range(alloc_page, &mut table, ROOT_INITRD_ADDR, initrd_paddr, initrd_size, attr);
|
||||
}
|
||||
}
|
||||
|
@ -166,6 +166,10 @@ impl<'a> Reader<'a> {
|
||||
pub fn new(data: &'a [u8]) -> Self {
|
||||
Reader { data }
|
||||
}
|
||||
|
||||
pub fn data(&self) -> &'a [u8] {
|
||||
self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Reader<'a> {
|
||||
|
3
root/.gitignore
vendored
Normal file
3
root/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*.cpio
|
||||
*.o
|
||||
root
|
21
root/Makefile
Normal file
21
root/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
AS = riscv64-elf-as
|
||||
LD = riscv64-elf-ld
|
||||
|
||||
ASFLAGS += -march=rv64imac -mabi=lp64
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: init.cpio
|
||||
|
||||
%.o: %.S
|
||||
$(AS) $(ASFLAGS) -c $< -o $@
|
||||
|
||||
root: root.o
|
||||
$(LD) -Tlink.ld $^ -o $@
|
||||
|
||||
init.cpio: root
|
||||
find . -type f -not -name "*.cpio" | cpio -ov -H crc > init.cpio
|
||||
|
||||
clean:
|
||||
rm -f *.o root
|
||||
rm -f init.cpio
|
16
root/link.ld
Normal file
16
root/link.ld
Normal file
@ -0,0 +1,16 @@
|
||||
OUTPUT_ARCH(riscv)
|
||||
ENTRY(_start)
|
||||
|
||||
PHDRS {
|
||||
text PT_LOAD FLAGS (5);
|
||||
rodata PT_LOAD FLAGS (4);
|
||||
data PT_LOAD FLAGS (6);
|
||||
}
|
||||
|
||||
SECTIONS {
|
||||
. = 0x400000;
|
||||
.text : { *(.text .text.* ); . = ALIGN(4096); } : text
|
||||
.rodata : { *(.rodata .rodata.*); . = ALIGN(4096); } : rodata
|
||||
.data : { *(.data .data.* ); . = ALIGN(4096); } : data
|
||||
.bss : { *(.bss .bss.* ); . = ALIGN(4096); } : data
|
||||
}
|
19
root/root.S
Normal file
19
root/root.S
Normal file
@ -0,0 +1,19 @@
|
||||
.macro syscall msg ptr
|
||||
li a0, \msg
|
||||
li a1, \ptr
|
||||
ecall
|
||||
.endm
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
la t0, msg
|
||||
.loop:
|
||||
lb a2, 0(t0)
|
||||
beqz a2, _start # print forever
|
||||
syscall (1<<12 | 1), 1 # see uapi/syscall.rs
|
||||
addi t0, t0, 1
|
||||
j .loop
|
||||
|
||||
.data
|
||||
msg : .string "Hello, world!\n\0"
|
9
uapi/src/consts.rs
Normal file
9
uapi/src/consts.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use utils::{addr::VirtAddr, size::MIB};
|
||||
|
||||
pub const ROOT_FDT_ADDR: VirtAddr = VirtAddr(0xfbf0_0000);
|
||||
pub const ROOT_FDT_MAX_SIZE: usize = 1 * MIB;
|
||||
pub const ROOT_FDT_MAX_BITS: usize = ROOT_FDT_MAX_SIZE.ilog2() as usize;
|
||||
|
||||
pub const ROOT_INITRD_ADDR: VirtAddr = VirtAddr(0xfc00_0000);
|
||||
pub const ROOT_INITRD_MAX_SIZE: usize = 64 * MIB;
|
||||
pub const ROOT_INITRD_MAX_BITS: usize = ROOT_INITRD_MAX_SIZE.ilog2() as usize;
|
@ -4,7 +4,6 @@ pub enum CNodeSlot {
|
||||
TcbCap,
|
||||
CNodeCap,
|
||||
VSpaceCap,
|
||||
BootInfoFrameCap,
|
||||
UntypedCap,
|
||||
// IrqControlCap
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
extern crate num_derive;
|
||||
|
||||
pub mod cap;
|
||||
pub mod consts;
|
||||
pub mod cspace;
|
||||
pub mod error;
|
||||
pub mod fault;
|
||||
|
Loading…
Reference in New Issue
Block a user