loader: implement byte-addressing for init elf files

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-03-12 15:29:10 +08:00
parent 8e515f2846
commit 1b83177b8a
3 changed files with 32 additions and 43 deletions

View file

@ -575,13 +575,13 @@ fn copy_processes(cfg: &mut BootConfig) {
if tag.name == make_type!("IniE") { if tag.name == make_type!("IniE") {
let mut page_addr: usize = 0; let mut page_addr: usize = 0;
let mut previous_addr: usize = 0; let mut previous_addr: usize = 0;
let mut top = 0 as *mut usize; let mut top = 0 as *mut u8;
let inie = MiniElf::new(&tag); let inie = MiniElf::new(&tag);
let mut src_addr = unsafe { let mut src_addr = unsafe {
cfg.base_addr cfg.base_addr
.add(inie.load_offset as usize / mem::size_of::<usize>()) .add(inie.load_offset as usize / mem::size_of::<usize>())
}; } as *const u8;
// Example: Page starts at 0xf0c0 and is 8192 bytes long. // Example: Page starts at 0xf0c0 and is 8192 bytes long.
// 1. Copy 3094 bytes to page 1 // 1. Copy 3094 bytes to page 1
@ -620,28 +620,18 @@ fn copy_processes(cfg: &mut BootConfig) {
); );
unsafe { unsafe {
bzero( bzero(
top.add( top.add(previous_addr as usize & (PAGE_SIZE - 1)),
(previous_addr as usize & (PAGE_SIZE - 1)) top.add(PAGE_SIZE as usize),
/ mem::size_of::<usize>(),
),
top.add(PAGE_SIZE as usize / mem::size_of::<usize>()),
) )
}; };
} }
// Allocate a new page. // Allocate a new page.
cfg.extra_pages += 1; cfg.extra_pages += 1;
top = cfg.get_top(); top = cfg.get_top() as *mut u8;
// Zero out the page, if necessary. // Zero out the page, if necessary.
unsafe { unsafe { bzero(top, top.add(section.virt as usize & (PAGE_SIZE - 1))) };
bzero(
top,
top.add(
(section.virt as usize & (PAGE_SIZE - 1)) / mem::size_of::<usize>(),
),
)
};
} }
// Part 1: Copy the first chunk over. // Part 1: Copy the first chunk over.
@ -654,29 +644,23 @@ fn copy_processes(cfg: &mut BootConfig) {
"First chunk is {} bytes, copying from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})", "First chunk is {} bytes, copying from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})",
first_chunk_size, first_chunk_size,
src_addr as usize, src_addr as usize,
unsafe { src_addr.add(first_chunk_size / 4) as usize }, unsafe { src_addr.add(first_chunk_size) as usize },
unsafe { top.add(first_chunk_offset / mem::size_of::<usize>()) as usize }, unsafe { top.add(first_chunk_offset) as usize },
unsafe { top.add((first_chunk_size + first_chunk_offset) / mem::size_of::<usize>()) unsafe { top.add(first_chunk_size + first_chunk_offset)
as usize }, as usize },
this_page + first_chunk_offset, this_page + first_chunk_offset,
); );
// Perform the copy, if NOCOPY is not set // Perform the copy, if NOCOPY is not set
if !section.no_copy() { if !section.no_copy() {
unsafe { unsafe {
memcpy( memcpy(top.add(first_chunk_offset), src_addr, first_chunk_size);
top.add(first_chunk_offset / mem::size_of::<usize>()), src_addr = src_addr.add(first_chunk_size);
src_addr,
first_chunk_size,
);
src_addr = src_addr.add(first_chunk_size / mem::size_of::<usize>());
} }
} else { } else {
unsafe { unsafe {
bzero( bzero(
top.add(first_chunk_offset / mem::size_of::<usize>()), top.add(first_chunk_offset),
top.add( top.add(first_chunk_offset + first_chunk_size),
(first_chunk_offset + first_chunk_size) / mem::size_of::<usize>(),
),
); );
} }
} }
@ -685,7 +669,7 @@ fn copy_processes(cfg: &mut BootConfig) {
// Part 2: Copy any full pages. // Part 2: Copy any full pages.
while bytes_to_copy > PAGE_SIZE { while bytes_to_copy > PAGE_SIZE {
cfg.extra_pages += 1; cfg.extra_pages += 1;
top = cfg.get_top(); top = cfg.get_top() as *mut u8;
// println!( // println!(
// "Copying next page from {:08x} {:08x}", // "Copying next page from {:08x} {:08x}",
// src_addr as usize, top as usize // src_addr as usize, top as usize
@ -693,10 +677,10 @@ fn copy_processes(cfg: &mut BootConfig) {
if !section.no_copy() { if !section.no_copy() {
unsafe { unsafe {
memcpy(top, src_addr, PAGE_SIZE); memcpy(top, src_addr, PAGE_SIZE);
src_addr = src_addr.add(PAGE_SIZE / mem::size_of::<usize>()); src_addr = src_addr.add(PAGE_SIZE);
} }
} else { } else {
unsafe { bzero(top, top.add(PAGE_SIZE / 4)) }; unsafe { bzero(top, top.add(PAGE_SIZE)) };
} }
bytes_to_copy -= PAGE_SIZE; bytes_to_copy -= PAGE_SIZE;
} }
@ -705,14 +689,14 @@ fn copy_processes(cfg: &mut BootConfig) {
if bytes_to_copy > 0 { if bytes_to_copy > 0 {
println!("Copying final section -- {} bytes", bytes_to_copy); println!("Copying final section -- {} bytes", bytes_to_copy);
cfg.extra_pages += 1; cfg.extra_pages += 1;
top = cfg.get_top(); top = cfg.get_top() as *mut u8;
if !section.no_copy() { if !section.no_copy() {
unsafe { unsafe {
memcpy(top, src_addr, bytes_to_copy); memcpy(top, src_addr, bytes_to_copy);
src_addr = src_addr.add(bytes_to_copy / mem::size_of::<usize>()); src_addr = src_addr.add(bytes_to_copy);
} }
} else { } else {
unsafe { bzero(top, top.add(bytes_to_copy / 4)) }; unsafe { bzero(top, top.add(bytes_to_copy)) };
} }
} }
@ -725,8 +709,8 @@ fn copy_processes(cfg: &mut BootConfig) {
// Zero-out the trailing bytes // Zero-out the trailing bytes
unsafe { unsafe {
bzero( bzero(
top.add((previous_addr as usize & (PAGE_SIZE - 1)) / mem::size_of::<usize>()), top.add(previous_addr as usize & (PAGE_SIZE - 1)),
top.add(PAGE_SIZE as usize / mem::size_of::<usize>()), top.add(PAGE_SIZE as usize),
) )
}; };
} else if tag.name == make_type!("XKrn") { } else if tag.name == make_type!("XKrn") {

View file

@ -191,6 +191,11 @@ fn read_word(satp: usize, virt: usize) -> Result<u32, &'static str> {
Ok(unsafe { (page_base as *mut u32).read() }) Ok(unsafe { (page_base as *mut u32).read() })
} }
fn read_byte(satp: usize, virt: usize) -> Result<u8, &'static str> {
let word = read_word(satp, virt & 0xfffffffc)?;
Ok(word.to_le_bytes()[virt & 3])
}
fn verify_kernel(cfg: &BootConfig, pid: usize, arg: &crate::args::KernelArgument) { fn verify_kernel(cfg: &BootConfig, pid: usize, arg: &crate::args::KernelArgument) {
let prog = unsafe { &*(arg.data.as_ptr() as *const crate::ProgramDescription) }; let prog = unsafe { &*(arg.data.as_ptr() as *const crate::ProgramDescription) };
let program_offset = prog.load_offset as usize; let program_offset = prog.load_offset as usize;
@ -273,21 +278,21 @@ fn verify_program(cfg: &BootConfig, pid: usize, arg: &crate::args::KernelArgumen
let mut program_offset = elf.load_offset as usize; let mut program_offset = elf.load_offset as usize;
for section in elf.sections.iter() { for section in elf.sections.iter() {
for addr in (section.virt..(section.virt + section.len() as u32)).step_by(4) { for addr in section.virt..(section.virt + section.len() as u32) {
let addr = addr as usize; let addr = addr as usize;
let word = read_word(cfg.processes[pid].satp as usize, addr).unwrap(); let word = read_byte(cfg.processes[pid].satp as usize, addr).unwrap();
if section.no_copy() { if section.no_copy() {
assert!(word == 0, "bss is {:08x}, not 0 @ {:08x}", word, addr); assert!(word == 0, "bss is {:08x}, not 0 @ {:08x}", word, addr);
} else { } else {
let check_word = unsafe { let check_word = unsafe {
(cfg.base_addr as *mut u32) (cfg.base_addr as *mut u8)
.add(program_offset / 4) .add(program_offset)
.read() .read()
}; };
program_offset += 4; program_offset += 1;
assert!( assert!(
word == check_word, word == check_word,
"program doesn't match @ {:08x} (expected: {:08x} found: {:08x})", "program doesn't match @ {:08x} (expected: {:02x} found: {:02x})",
addr, addr,
check_word, check_word,
word, word,

Binary file not shown.