Skip to content

Commit

Permalink
improve elf loader
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6D70 committed Feb 22, 2024
1 parent f829996 commit e63feba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 8 additions & 0 deletions xernel/kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use alloc::string::ToString;
use alloc::sync::Arc;
use alloc::vec;
use alloc::vec::Vec;
use x86_64::VirtAddr;
use core::arch::asm;
use core::panic::PanicInfo;
use libxernel::sync::Spinlock;
Expand Down Expand Up @@ -154,6 +155,13 @@ extern "C" fn kernel_main() -> ! {
scheduler::init();

let process = Arc::new(Spinlock::new(Process::new(Some(KERNEL_PROCESS.clone()))));
dbg!("before pt load");
unsafe {
process.lock().page_table.as_ref().unwrap().load_pt();
dbg!("ffffffff80016140 => {:x}", KERNEL_PAGE_MAPPER.lock().translate(VirtAddr::new(0xffffffff80016140)).unwrap().as_u64());
dbg!("ffffffff80016140 => {:x}", process.lock().page_table.as_ref().unwrap().translate(VirtAddr::new(0xffffffff80016140)).unwrap().as_u64());
}
dbg!("after pt load");

let test_elf = include_bytes!("../test-elfloader");
let user_task = Thread::new_user_thread_from_elf(process.clone(), test_elf);
Expand Down
3 changes: 1 addition & 2 deletions xernel/kernel/src/mem/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ impl Pagemap {
self.flush(virt.start_address());
}
}
dbg!("end map");
}

pub fn flush(&self, addr: VirtAddr) {
Expand Down Expand Up @@ -296,7 +295,7 @@ impl Pagemap {
}

unsafe fn get_pt(pt: *mut PageTable, pt_index: PageTableIndex) -> *mut PageTable {
(*pt)[pt_index].addr().as_u64() as *mut PageTable
((*pt)[pt_index].addr().as_u64() + *HIGHER_HALF_OFFSET) as *mut PageTable
}

/// Only works with 4KiB pages
Expand Down
17 changes: 14 additions & 3 deletions xernel/kernel/src/sched/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use elf::abi::{ET_EXEC, PT_LOAD};
use elf::endian::LittleEndian;
use elf::ElfBytes;
use libxernel::sync::Spinlock;
use x86_64::registers::control::Cr3;
use x86_64::structures::paging::{Page, PageSize, PageTableFlags, PhysFrame, Size4KiB};

use crate::allocator::align_up;
Expand Down Expand Up @@ -202,7 +203,7 @@ impl Thread {

for segment in segments {
if segment.p_type == PT_LOAD {
dbg!("{:#x?}", segment);
dbg!("{:x?}", segment);
let file_size = segment.p_filesz as usize;
let mem_size = segment.p_memsz as usize;
let vaddr = segment.p_vaddr as usize;
Expand All @@ -220,7 +221,11 @@ impl Thread {
let process = self.get_process().unwrap();
let process = process.lock();

dbg!("before mapping");
// we need to use the page table of the process to copy the content of the segment into the memory
assert!(
process.get_page_table().unwrap().pml4().as_u64() == Cr3::read().0.start_address().as_u64()
);

process.get_page_table().unwrap().map(
frame,
page,
Expand All @@ -230,13 +235,19 @@ impl Thread {
| PageTableFlags::WRITABLE, // TODO: make this depend on the elf flags, currently we need it to write the content of the segment ==> make it read-only after writing
true,
);
dbg!("after mapping");

unsafe {
write_bytes(page.start_address().as_mut_ptr::<u8>(), 0, Size4KiB::SIZE as usize);
}
}

dbg!(
"{:x} - {:x}; executable: {}",
vaddr,
vaddr + file_size,
segment.p_flags & 1 == 1
);

unsafe {
core::ptr::copy_nonoverlapping(
elf_data.as_ptr().add(segment.p_offset as usize),
Expand Down

0 comments on commit e63feba

Please sign in to comment.