Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified initramfs/rprog.img
Binary file not shown.
Binary file added initramfs/vm.img
Binary file not shown.
5 changes: 3 additions & 2 deletions kernel/linker.ld
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
ENTRY(_start)

SECTIONS {
. = 0x80000;
. = 0xffff000000000000;
. += 0x80000;
.text : {
KEEP(*(.text._start_kernel))
KEEP(*(.text._start_section))
}
.rodata : {
*(.rodata*)
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/allocator/buddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl BuddyAllocator {
}
}
println!("Free list: {:?}", BUDDY_SYSTEM.free_list);
BUDDY_SYSTEM.initialized = true;
}

pub unsafe fn print_info(&self) {
Expand Down Expand Up @@ -115,7 +114,7 @@ impl BuddyAllocator {
if align < FRAME_SIZE {
layer = 0;
} else {
while (1 << layer) < align {
while (1 << layer) * FRAME_SIZE < align {
layer += 1;
}
}
Expand Down
10 changes: 6 additions & 4 deletions kernel/src/allocator/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ unsafe impl GlobalAlloc for DynamicAllocator {
return BumpAllocator.dealloc(ptr, layout);
}
if layout.size() > FRAME_SIZE {
debug!(
"Deallocating 0x{:x} with size > {}",
ptr as usize, FRAME_SIZE
);
if DYNAMIC_ALLOCATOR.verbose {
debug!(
"Deallocating 0x{:x} with size > {}",
ptr as usize, FRAME_SIZE
);
}
return BUDDY_SYSTEM.dealloc(ptr, layout);
}
if DYNAMIC_ALLOCATOR.verbose {
Expand Down
16 changes: 0 additions & 16 deletions kernel/src/commands/echo.rs

This file was deleted.

12 changes: 8 additions & 4 deletions kernel/src/commands/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ use alloc::vec::Vec;
use filesystem::cpio::CpioArchive;
use stdio::println;

pub fn exec(args: Vec<String>) {
pub fn exec(args: Vec<String>) -> ! {
println!("Executing exec command with args: {:?}", args);
let rootfs = CpioArchive::load(unsafe { INITRAMFS_ADDR } as *const u8);
for filename in args.iter().skip(1) {
let filename = filename.as_bytes();
if let Some(data) = rootfs.get_file(core::str::from_utf8(filename).unwrap()) {
let program = scheduler::alloc_prog(data);
scheduler::get().create_thread(program);
scheduler::get().create_thread(program.0, program.1);
} else {
println!(
"File not found: {}",
core::str::from_utf8(filename).unwrap()
);
}
}
assert!(!scheduler::get().ready_queue.is_empty());
scheduler::get().run_threads();

if scheduler::get().ready_queue.is_empty() {
panic!("No threads to run!");
} else {
scheduler::get().run_threads();
}
}
26 changes: 14 additions & 12 deletions kernel/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod buddy;
mod cat;
mod echo;
mod exec;
mod hello;
mod help;
Expand All @@ -14,30 +13,33 @@ use alloc::string::ToString;
use alloc::vec::Vec;

pub fn execute(command: &[u8]) {
let command = match command.iter().position(|&c| c == 0) {
Some(i) => &command[..i],
None => command,
};
let args: Vec<String> = core::str::from_utf8(command)
.unwrap()
.split_whitespace()
.map(|s| s.to_string())
.collect();
if command.starts_with(b"\x00") {
println!("Executing command: {:?}", args);
if args.is_empty() {
return;
} else if command.starts_with(b"hello") {
} else if args[0] == "hello" {
hello::exec();
} else if command.starts_with(b"help") {
} else if args[0] == "help" {
help::exec();
} else if command.starts_with(b"reboot") {
} else if args[0] == "reboot" {
reboot::exec();
} else if command.starts_with(b"ls") {
} else if args[0] == "ls" {
ls::exec();
} else if command.starts_with(b"cat") {
} else if args[0] == "cat" {
cat::exec(&command);
} else if command.starts_with(b"exec") {
} else if args[0] == "exec" {
exec::exec(args);
} else if command.starts_with(b"echo") {
echo::exec(&command);
} else if command.starts_with(b"setTimeOut") {
} else if args[0] == "setTimeOut" {
set_time_out::exec(&command);
} else if command.starts_with(b"buddy") {
} else if args[0] == "buddy" {
buddy::exec();
} else {
println!(
Expand Down
4 changes: 3 additions & 1 deletion kernel/src/dtb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod utils;
use alloc::vec::Vec;
use stdio::println;

const DTB_ADDRERSS: u64 = 0x6_f000;

pub fn load_dtb() -> dt::Dt {
let (dtb_addr, header) = get_dtb_addr();
let strings_addr = dtb_addr + header.off_dt_strings;
Expand All @@ -17,7 +19,7 @@ pub fn load_dtb() -> dt::Dt {
}

pub fn get_dtb_addr() -> (u32, fdt::FdtHeader) {
let dtb_addr = 0x6f000 as *const u8;
let dtb_addr = DTB_ADDRERSS as *const u8;
println!("DTB address: {:p}", dtb_addr);
let dtb_addr = unsafe { core::ptr::read_volatile(dtb_addr as *const u32) };
println!("DTB address: {:#x}", dtb_addr);
Expand Down
14 changes: 12 additions & 2 deletions kernel/src/exception/context_switch.S
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@
mrs x0, elr_el1
mrs x1, sp_el0
mrs x2, spsr_el1
mrs x3, ttbr0_el1
stp x0, x1, [sp, 16 * 16]
str x2, [sp, 16 * 17]
stp x2, x3, [sp, 16 * 17]
mov x0, 0x1000
msr ttbr0_el1, x0
tlbi vmalle1is
dsb ish
isb
.endm

// load general registers from stack
.macro load_all
ldp x0, x1, [sp, 16 * 16]
ldr x2, [sp, 16 * 17]
ldp x2, x3, [sp, 16 * 17]
msr elr_el1, x0
msr sp_el0, x1
msr spsr_el1, x2
msr ttbr0_el1, x3
tlbi vmalle1is
dsb ish
isb
ldp x0, x1, [sp ,16 * 0]
ldp x2, x3, [sp ,16 * 1]
ldp x4, x5, [sp ,16 * 2]
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/exception/exception_table.S
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exception_vector_table:
EXCEPTION_WITH_TYPE 3, unknown_exception_handler

.align 7
EXCEPTION_WITH_TYPE 4, svc_handler
EXCEPTION_WITH_TYPE 4, lower_exception_handler

.align 7
EXCEPTION_WITH_TYPE 5, irq_handler
Expand All @@ -39,7 +39,7 @@ exception_vector_table:
EXCEPTION_WITH_TYPE 7, unknown_exception_handler

.align 7
EXCEPTION_WITH_TYPE 8, svc_handler
EXCEPTION_WITH_TYPE 8, lower_exception_handler

.align 7
EXCEPTION_WITH_TYPE 9, irq_handler
Expand Down
1 change: 1 addition & 0 deletions kernel/src/exception/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod irq;
mod page;
mod svc;
28 changes: 28 additions & 0 deletions kernel/src/exception/handlers/page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::arch::asm;
use stdio::println;

pub unsafe fn page_fault() {
let esr_el1: u64;
asm!(
"mrs {0}, esr_el1",
out(reg) esr_el1,
);
let ec = esr_el1 >> 26;
let far_el1: u64;
asm!(
"mrs {0}, far_el1",
out(reg) far_el1,
);
let elr_el1: u64;
asm!(
"mrs {0}, elr_el1",
out(reg) elr_el1,
);

println!("Page fault");
println!("Exception Class: 0b{:06b}", ec);
println!("ESR_EL1: 0x{:x}", esr_el1);
println!("ELR_EL1: 0x{:x}", elr_el1);
println!("FAR_EL1: 0x{:x}", far_el1);
panic!("Page fault");
}
53 changes: 40 additions & 13 deletions kernel/src/exception/handlers/svc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::exception::trap_frame;
use core::arch::asm;
use super::page::page_fault;
use crate::{exception::trap_frame, mmu::vm::VirtualMemory};
use core::{arch::asm, fmt::Debug};
use stdio::{debug, println};

#[repr(C)]
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy)]
struct Syscall {
arg0: u64,
arg1: u64,
Expand Down Expand Up @@ -42,21 +43,42 @@ impl Syscall {
}
}

impl Debug for Syscall {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"Syscall {{ idx: 0x{:x}, arg0: 0x{:x}, arg1: 0x{:x}, arg2: 0x{:x}, arg3: 0x{:x}, arg4: 0x{:x}, arg5: 0x{:x}, arg6: 0x{:x}, arg7: 0x{:x} }}",
self.idx, self.arg0, self.arg1, self.arg2, self.arg3, self.arg4, self.arg5, self.arg6, self.arg7
)
}
}

#[no_mangle]
unsafe fn svc_handler(eidx: u64, sp: u64) {
trap_frame::TRAP_FRAME = Some(trap_frame::TrapFrame::new(sp));
// let syscall = Syscall::new(sp);
// println!("Exception {}", eidx);
// debug!("Syscall idx: {}", syscall.idx);
// debug!("Syscall {:?}", syscall);
match eidx {
4 => el1_interrupt(sp),
8 => syscall_handler(sp),
unsafe fn lower_exception_handler(eidx: u64, sp: u64) {
let esr_el1: u64;
asm!(
"mrs {0}, esr_el1",
out(reg) esr_el1,
);
let ec = esr_el1 >> 26;
match ec {
0b010101 => svc_handler(sp),
0b001110 => {
panic!("Illegal Execution state.");
}
0b100000 | 0b100001 | 0b100100 => page_fault(),
_ => {
println!("Exception {}", eidx);
println!("Unknown exception");
println!("ec: 0b{:06b}", ec);
el1_interrupt(sp);
}
}
}

unsafe fn svc_handler(sp: u64) {
trap_frame::TRAP_FRAME = Some(trap_frame::TrapFrame::new(sp));
syscall_handler(sp);
trap_frame::TRAP_FRAME.unwrap().restore();
trap_frame::TRAP_FRAME = None;
}
Expand Down Expand Up @@ -88,15 +110,17 @@ unsafe fn el1_interrupt(sp: u64) {
unsafe fn syscall_handler(sp: u64) {
let syscall = Syscall::new(sp);
assert!(trap_frame::TRAP_FRAME.is_some());
let vm = VirtualMemory::load(trap_frame::TRAP_FRAME.as_ref().unwrap().state.l0);
match syscall.idx {
0 => {
println!("Syscall get_pid");
// println!("Syscall get_pid");
let pid = crate::syscall::get_pid();
trap_frame::TRAP_FRAME.as_mut().unwrap().state.x[0] = pid;
}
1 => {
// println!("Syscall read");
let buf = syscall.arg0 as *mut u8;
let buf = vm.get_phys(buf as u64);
let size = syscall.arg1 as usize;
let read = crate::syscall::read(buf, size);
assert!(size == 1);
Expand All @@ -109,13 +133,15 @@ unsafe fn syscall_handler(sp: u64) {
2 => {
// println!("Syscall write");
let buf = syscall.arg0 as *const u8;
let buf = vm.get_phys(buf as u64);
let size = syscall.arg1 as usize;
let written = crate::syscall::write(buf, size);
trap_frame::TRAP_FRAME.as_mut().unwrap().state.x[0] = written as u64;
}
3 => {
// println!("Syscall exec");
let name = syscall.arg0 as *const u8;
let name = vm.get_phys(name as u64);
let ret = crate::syscall::exec(name);
trap_frame::TRAP_FRAME.as_mut().unwrap().state.x[0] = ret;
}
Expand All @@ -132,6 +158,7 @@ unsafe fn syscall_handler(sp: u64) {
// println!("Syscall mbox_call");
let channel = syscall.arg0 as u8;
let mbox = syscall.arg1 as *mut u32;
let mbox = vm.get_phys(mbox as u64) as *mut u32;
let ret = crate::syscall::mbox_call(channel, mbox);
trap_frame::TRAP_FRAME.as_mut().unwrap().state.x[0] = ret as u64;
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/exception/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ unsafe fn unknown_exception_handler(eidx: u64) {
);
debug!("ESR_EL1: 0x{:x}", esr_el1);
debug!("ELR_EL1: 0x{:x}", elr_el1);
panic!("Unknown exception {}", eidx);
panic!("Unknown exception handler {}", eidx);
}
Loading