feat(kernel): implement ELF64 loader, user space paging, and Ring 3 jump via iretq
This commit is contained in:
+41
-21
@@ -7,6 +7,7 @@ pub mod arch;
|
||||
pub mod mm;
|
||||
pub mod ipc;
|
||||
pub mod sched;
|
||||
pub mod loader;
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use limine_requests::*;
|
||||
@@ -14,6 +15,16 @@ use limine_requests::*;
|
||||
// Include assembly trampolines directly into the binary
|
||||
core::arch::global_asm!(include_str!("../asm/context.S"));
|
||||
core::arch::global_asm!(include_str!("../asm/syscall_entry.S"));
|
||||
core::arch::global_asm!(include_str!("../asm/ring3_enter.S"));
|
||||
|
||||
extern "C" {
|
||||
fn enter_user_mode(
|
||||
entry_rip: u64,
|
||||
user_rsp: u64,
|
||||
pml4_paddr: u64,
|
||||
kernel_stack_top: u64,
|
||||
) -> !;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
@@ -64,26 +75,7 @@ pub extern "C" fn _start() -> ! {
|
||||
// 7. Initialize Scheduler
|
||||
sched::init();
|
||||
|
||||
// 8. Inspect Boot Modules (Initial Userspace Servers)
|
||||
unsafe {
|
||||
let mod_resp = MODULE_REQUEST.response;
|
||||
if !mod_resp.is_null() {
|
||||
let count = (*mod_resp).module_count;
|
||||
kprintln!("[BOOT] Found {} initial userspace module(s):", count);
|
||||
for i in 0..count {
|
||||
let mod_file = *(*mod_resp).modules.add(i as usize);
|
||||
let path_cstr = (*mod_file).path;
|
||||
let path_len = (0..256).find(|&j| *path_cstr.add(j) == 0).unwrap_or(0);
|
||||
let path = core::str::from_utf8(core::slice::from_raw_parts(path_cstr, path_len)).unwrap_or("unknown");
|
||||
kprintln!(" - Module [{}]: {} (size: {} bytes, addr: {:p})",
|
||||
i, path, (*mod_file).size, (*mod_file).address);
|
||||
}
|
||||
} else {
|
||||
kprintln!("[BOOT] No external modules passed by bootloader.");
|
||||
}
|
||||
}
|
||||
|
||||
// 9. Kernel Self-Test: Fast-Path IPC Ping
|
||||
// 8. Self-Test Fast-Path IPC Ping
|
||||
kprintln!("[TEST] Executing Kernel Fast-Path IPC Self-Test...");
|
||||
let (status, resp_op, val) = ipc::fastpath::handle_fastpath_call(
|
||||
ipc::fastpath::CAP_KERNEL_CONTROL,
|
||||
@@ -92,7 +84,35 @@ pub extern "C" fn _start() -> ! {
|
||||
);
|
||||
kprintln!("[TEST] IPC Fast-Path Ping Result: status={}, opcode={:#x}, val={}", status, resp_op, val);
|
||||
|
||||
kprintln!("[KERNEL] Initialization complete. Entering kernel idle loop.\n");
|
||||
// 9. Inspect and Load Initial Userspace Process (Ring 3 Transition)
|
||||
unsafe {
|
||||
let mod_resp = MODULE_REQUEST.response;
|
||||
if !mod_resp.is_null() && (*mod_resp).module_count > 0 {
|
||||
kprintln!("[BOOT] Loading root userspace module [0] into Ring 3...");
|
||||
let mod_file = *(*mod_resp).modules;
|
||||
let elf_slice = core::slice::from_raw_parts((*mod_file).address, (*mod_file).size as usize);
|
||||
|
||||
match loader::elf::load_elf(elf_slice, hhdm_offset) {
|
||||
Ok(proc) => {
|
||||
kprintln!("[BOOT] Transitioning CPU to Ring 3 (User Mode) via IRETQ...");
|
||||
kprintln!("-------------------------------------------------------");
|
||||
enter_user_mode(
|
||||
proc.entry_rip,
|
||||
proc.user_rsp,
|
||||
proc.pml4_paddr,
|
||||
proc.kernel_stack_top,
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
kprintln!("[ERROR] Failed to load ELF process: {}", err);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
kprintln!("[BOOT] No userspace modules detected. Staying in Ring 0 idle loop.");
|
||||
}
|
||||
}
|
||||
|
||||
kprintln!("[KERNEL] Entering idle loop.\n");
|
||||
|
||||
loop {
|
||||
unsafe {
|
||||
|
||||
Reference in New Issue
Block a user