mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
compact phase 1 loader
merge inie/inif routines more intelligently
This commit is contained in:
parent
039de0bfe9
commit
b8cc156cfc
1 changed files with 256 additions and 360 deletions
|
|
@ -148,398 +148,294 @@ pub fn copy_args(cfg: &mut BootConfig) {
|
||||||
cfg.args = KernelArguments::new(runtime_arg_buffer);
|
cfg.args = KernelArguments::new(runtime_arg_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq)]
|
||||||
|
enum TagType {
|
||||||
|
IniE,
|
||||||
|
IniF,
|
||||||
|
XKrn,
|
||||||
|
Other,
|
||||||
|
}
|
||||||
|
impl From<u32> for TagType {
|
||||||
|
fn from(code: u32) -> Self {
|
||||||
|
if code == u32::from_le_bytes(*b"IniE") {
|
||||||
|
TagType::IniE
|
||||||
|
} else if code == u32::from_le_bytes(*b"IniF") {
|
||||||
|
TagType::IniF
|
||||||
|
} else if code == u32::from_le_bytes(*b"XKrn") {
|
||||||
|
TagType::XKrn
|
||||||
|
} else {
|
||||||
|
TagType::Other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl TagType {
|
||||||
|
#[cfg(feature="debug-print")]
|
||||||
|
pub fn to_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
TagType::IniE => "IniE",
|
||||||
|
TagType::IniF => "IniF",
|
||||||
|
TagType::XKrn => "XKrn",
|
||||||
|
TagType::Other => "Other",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Copy program data from the SPI flash into newly-allocated RAM
|
/// Copy program data from the SPI flash into newly-allocated RAM
|
||||||
/// located at the end of memory space.
|
/// located at the end of memory space.
|
||||||
fn copy_processes(cfg: &mut BootConfig) {
|
fn copy_processes(cfg: &mut BootConfig) {
|
||||||
let mut _pid = 1;
|
let mut _pid = 1;
|
||||||
for tag in cfg.args.iter() {
|
for tag in cfg.args.iter() {
|
||||||
if tag.name == u32::from_le_bytes(*b"IniE") {
|
let tag_type = TagType::from(tag.name);
|
||||||
_pid += 1;
|
match tag_type {
|
||||||
let mut page_addr: usize = 0;
|
TagType::IniF |
|
||||||
let mut previous_addr: usize = 0;
|
TagType::IniE => {
|
||||||
let mut top = core::ptr::null_mut::<u8>();
|
_pid += 1;
|
||||||
|
let mut page_addr: usize = 0;
|
||||||
|
let mut previous_addr: usize = 0;
|
||||||
|
let mut previous_ram_zero_addr: usize = 0;
|
||||||
|
let mut top = core::ptr::null_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;
|
} 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
|
||||||
// 2. Copy 4096 bytes to page 2
|
// 2. Copy 4096 bytes to page 2
|
||||||
// 3. Copy 192 bytes to page 3
|
// 3. Copy 192 bytes to page 3
|
||||||
//
|
//
|
||||||
// Example: Page starts at 0xf000 and is 4096 bytes long
|
// Example: Page starts at 0xf000 and is 4096 bytes long
|
||||||
// 1. Copy 4096 bytes to page 1
|
// 1. Copy 4096 bytes to page 1
|
||||||
//
|
//
|
||||||
// Example: Page starts at 0xf000 and is 128 bytes long
|
// Example: Page starts at 0xf000 and is 128 bytes long
|
||||||
// 1. Copy 128 bytes to page 1
|
// 1. Copy 128 bytes to page 1
|
||||||
//
|
//
|
||||||
// Example: Page starts at oxf0c0 and is 128 bytes long
|
// Example: Page starts at oxf0c0 and is 128 bytes long
|
||||||
// 1. Copy 128 bytes to page 1
|
// 1. Copy 128 bytes to page 1
|
||||||
println!("\n\nIniE {} has {} sections", _pid, inie.sections.len());
|
println!("\n\n{} {} has {} sections", tag_type.to_str(), _pid, inie.sections.len());
|
||||||
println!("Initial top: {:x}, extra_pages: {:x}, init_size: {:x}, base_addr: {:x}",
|
println!("Initial top: {:x}, extra_pages: {:x}, init_size: {:x}, base_addr: {:x}",
|
||||||
cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32);
|
cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32);
|
||||||
for section in inie.sections.iter() {
|
for section in inie.sections.iter() {
|
||||||
if (section.virt as usize) < previous_addr {
|
let flags = section.flags() as u8;
|
||||||
panic!("init section addresses are not strictly increasing (new virt: {:08x}, last virt: {:08x})", section.virt, previous_addr);
|
// any section that requires "write" must be copied to RAM
|
||||||
}
|
// note that ELF helpfully adds a 4096-byte gap between non-write pages and write-pages
|
||||||
|
// allowing us to just trundle through the pages and not have to deal with partially
|
||||||
|
// writeable pages.
|
||||||
|
// IniE is always copy_to_ram
|
||||||
|
let copy_to_ram = (flags & MINIELF_FLG_W != 0) || (tag_type == TagType::IniE);
|
||||||
|
|
||||||
let this_page = section.virt as usize & !(PAGE_SIZE - 1);
|
if (section.virt as usize) < previous_addr {
|
||||||
let mut bytes_to_copy = section.len();
|
panic!("init section addresses are not strictly increasing (new virt: {:08x}, last virt: {:08x})", section.virt, previous_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if copy_to_ram {
|
||||||
|
let this_page = section.virt as usize & !(PAGE_SIZE - 1);
|
||||||
|
let mut bytes_to_copy = section.len();
|
||||||
|
|
||||||
if VDBG {println!(
|
|
||||||
"Section is {} bytes long, loaded to {:08x}",
|
|
||||||
bytes_to_copy, section.virt
|
|
||||||
);}
|
|
||||||
// If this is not a new page, ensure the uninitialized values from between
|
|
||||||
// this section and the previous one are all zeroed out.
|
|
||||||
if this_page != page_addr || previous_addr == page_addr {
|
|
||||||
if VDBG {println!("New page @ {:08x}", this_page);}
|
|
||||||
if previous_addr != 0 && previous_addr != page_addr {
|
|
||||||
if VDBG {println!(
|
if VDBG {println!(
|
||||||
"Zeroing-out remainder of previous page: {:08x} (mapped to physical address {:08x})",
|
"Section is {} bytes long, loaded to {:08x}",
|
||||||
previous_addr, top as usize,
|
bytes_to_copy, section.virt
|
||||||
);}
|
);}
|
||||||
unsafe {
|
// If this is not a new page, ensure the uninitialized values from between
|
||||||
bzero(
|
// this section and the previous one are all zeroed out.
|
||||||
top.add(previous_addr as usize & (PAGE_SIZE - 1)),
|
if this_page != page_addr || previous_addr == page_addr {
|
||||||
top.add(PAGE_SIZE as usize),
|
if VDBG {println!("New page @ {:08x}", this_page);}
|
||||||
)
|
if previous_ram_zero_addr != 0 && previous_ram_zero_addr != page_addr && cfg.extra_pages > 0 {
|
||||||
};
|
if VDBG {println!(
|
||||||
}
|
"Zeroing-out remainder of previous page: {:08x} (mapped to physical address {:08x})",
|
||||||
|
previous_ram_zero_addr, top as usize,
|
||||||
|
);}
|
||||||
|
unsafe {
|
||||||
|
bzero(
|
||||||
|
top.add(previous_ram_zero_addr as usize & (PAGE_SIZE - 1)),
|
||||||
|
top.add(PAGE_SIZE as usize),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Allocate a new page.
|
// Allocate a new page.
|
||||||
cfg.extra_pages += 1;
|
cfg.extra_pages += 1;
|
||||||
top = cfg.get_top() as *mut u8;
|
top = cfg.get_top() as *mut u8;
|
||||||
|
|
||||||
// Zero out the page, if necessary.
|
// Zero out the page, if necessary.
|
||||||
unsafe { bzero(top, top.add(section.virt as usize & (PAGE_SIZE - 1))) };
|
unsafe { bzero(top, top.add(section.virt as usize & (PAGE_SIZE - 1))) };
|
||||||
} else {
|
} else {
|
||||||
if VDBG {println!("Reusing existing page @ {:08x}", this_page);}
|
if VDBG {println!("Reusing existing page @ {:08x}", this_page);}
|
||||||
}
|
|
||||||
|
|
||||||
// Part 1: Copy the first chunk over.
|
|
||||||
let mut first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1));
|
|
||||||
if first_chunk_size > section.len() {
|
|
||||||
first_chunk_size = section.len();
|
|
||||||
}
|
|
||||||
let first_chunk_offset = section.virt as usize & (PAGE_SIZE - 1);
|
|
||||||
if VDBG {println!(
|
|
||||||
"Section chunk is {} bytes, {} from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})",
|
|
||||||
first_chunk_size,
|
|
||||||
if section.no_copy() { "zeroing" } else { "copying" },
|
|
||||||
src_addr as usize,
|
|
||||||
unsafe { src_addr.add(first_chunk_size) as usize },
|
|
||||||
unsafe { top.add(first_chunk_offset) as usize },
|
|
||||||
unsafe { top.add(first_chunk_size + first_chunk_offset)
|
|
||||||
as usize },
|
|
||||||
this_page + first_chunk_offset,
|
|
||||||
);}
|
|
||||||
// Perform the copy, if NOCOPY is not set
|
|
||||||
if !section.no_copy() {
|
|
||||||
unsafe {
|
|
||||||
memcpy(top.add(first_chunk_offset), src_addr, first_chunk_size);
|
|
||||||
src_addr = src_addr.add(first_chunk_size);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unsafe {
|
|
||||||
bzero(
|
|
||||||
top.add(first_chunk_offset),
|
|
||||||
top.add(first_chunk_offset + first_chunk_size),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bytes_to_copy -= first_chunk_size;
|
|
||||||
|
|
||||||
// Part 2: Copy any full pages.
|
|
||||||
while bytes_to_copy >= PAGE_SIZE {
|
|
||||||
cfg.extra_pages += 1;
|
|
||||||
top = cfg.get_top() as *mut u8;
|
|
||||||
// println!(
|
|
||||||
// "Copying next page from {:08x} {:08x} ({} bytes to go...)",
|
|
||||||
// src_addr as usize, top as usize, bytes_to_copy
|
|
||||||
// );
|
|
||||||
if !section.no_copy() {
|
|
||||||
unsafe {
|
|
||||||
memcpy(top, src_addr, PAGE_SIZE);
|
|
||||||
src_addr = src_addr.add(PAGE_SIZE);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
unsafe { bzero(top, top.add(PAGE_SIZE)) };
|
|
||||||
}
|
|
||||||
bytes_to_copy -= PAGE_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Part 3: Copy the final residual partial page
|
// Part 1: Copy the first chunk over.
|
||||||
if bytes_to_copy > 0 {
|
let mut first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1));
|
||||||
if VDBG {println!(
|
if first_chunk_size > section.len() {
|
||||||
"Copying final section -- {} bytes @ {:08x}",
|
first_chunk_size = section.len();
|
||||||
bytes_to_copy,
|
|
||||||
section.virt + (section.len() as u32) - (bytes_to_copy as u32)
|
|
||||||
);}
|
|
||||||
cfg.extra_pages += 1;
|
|
||||||
top = cfg.get_top() as *mut u8;
|
|
||||||
if !section.no_copy() {
|
|
||||||
unsafe {
|
|
||||||
memcpy(top, src_addr, bytes_to_copy);
|
|
||||||
src_addr = src_addr.add(bytes_to_copy);
|
|
||||||
}
|
}
|
||||||
} else {
|
let first_chunk_offset = section.virt as usize & (PAGE_SIZE - 1);
|
||||||
unsafe { bzero(top, top.add(bytes_to_copy)) };
|
if VDBG {println!(
|
||||||
}
|
"Section chunk is {} bytes, {} from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})",
|
||||||
}
|
first_chunk_size,
|
||||||
|
if section.no_copy() { "zeroing" } else { "copying" },
|
||||||
previous_addr = section.virt as usize + section.len();
|
src_addr as usize,
|
||||||
page_addr = previous_addr & !(PAGE_SIZE - 1);
|
unsafe { src_addr.add(first_chunk_size) as usize },
|
||||||
if VDBG {
|
unsafe { top.add(first_chunk_offset) as usize },
|
||||||
println!("Looping to the next section");
|
unsafe { top.add(first_chunk_size + first_chunk_offset)
|
||||||
println!("top: {:x}, extra_pages: {:x}, init_size: {:x}, base_addr: {:x}", cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32);
|
as usize },
|
||||||
println!("previous_addr: {:x}, page_addr: {:x}", previous_addr, page_addr);
|
this_page + first_chunk_offset,
|
||||||
}
|
);}
|
||||||
}
|
// Perform the copy, if NOCOPY is not set
|
||||||
|
if !section.no_copy() {
|
||||||
println!("Done with sections");
|
unsafe {
|
||||||
if previous_addr as usize & (PAGE_SIZE - 1) != 0 {
|
memcpy(top.add(first_chunk_offset), src_addr, first_chunk_size);
|
||||||
println!("Zeroing out remaining data");
|
src_addr = src_addr.add(first_chunk_size);
|
||||||
// Zero-out the trailing bytes
|
}
|
||||||
unsafe {
|
} else {
|
||||||
bzero(
|
|
||||||
top.add(previous_addr as usize & (PAGE_SIZE - 1)),
|
|
||||||
top.add(PAGE_SIZE as usize),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
println!("Skipping zero step -- we ended on a page boundary");
|
|
||||||
}
|
|
||||||
} else if tag.name == u32::from_le_bytes(*b"XKrn") {
|
|
||||||
let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
|
|
||||||
|
|
||||||
// TEXT SECTION
|
|
||||||
// Round it off to a page boundary
|
|
||||||
let load_size_rounded = (prog.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
|
||||||
cfg.extra_pages += load_size_rounded / PAGE_SIZE;
|
|
||||||
let top = cfg.get_top();
|
|
||||||
println!("\n\nKernel top: {:x}, extra_pages: {:x}", top as u32, cfg.extra_pages);
|
|
||||||
unsafe {
|
|
||||||
// Copy the program to the target address, rounding it off to the load size.
|
|
||||||
let src_addr = cfg
|
|
||||||
.base_addr
|
|
||||||
.add(prog.load_offset as usize / mem::size_of::<usize>());
|
|
||||||
println!(
|
|
||||||
" Copying TEXT from {:08x}-{:08x} to {:08x}-{:08x} ({} bytes long)",
|
|
||||||
src_addr as usize,
|
|
||||||
src_addr as u32 + prog.text_size,
|
|
||||||
top as usize,
|
|
||||||
top as u32 + prog.text_size + 4,
|
|
||||||
prog.text_size + 4
|
|
||||||
);
|
|
||||||
println!(
|
|
||||||
" Zeroing out TEXT from {:08x}-{:08x}",
|
|
||||||
top.add(prog.text_size as usize / mem::size_of::<usize>()) as usize,
|
|
||||||
top.add(load_size_rounded as usize / mem::size_of::<usize>()) as usize,
|
|
||||||
);
|
|
||||||
|
|
||||||
memcpy(top, src_addr, prog.text_size as usize + 1);
|
|
||||||
|
|
||||||
// Zero out the remaining data.
|
|
||||||
bzero(
|
|
||||||
top.add(prog.text_size as usize / mem::size_of::<usize>()),
|
|
||||||
top.add(load_size_rounded as usize / mem::size_of::<usize>()),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// DATA SECTION
|
|
||||||
// Round it off to a page boundary
|
|
||||||
let load_size_rounded =
|
|
||||||
((prog.data_size + prog.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
|
||||||
cfg.extra_pages += load_size_rounded / PAGE_SIZE;
|
|
||||||
let top = cfg.get_top();
|
|
||||||
unsafe {
|
|
||||||
// Copy the program to the target address, rounding it off to the load size.
|
|
||||||
let src_addr = cfg.base_addr.add(
|
|
||||||
(prog.load_offset + prog.text_size + 4) as usize / mem::size_of::<usize>() - 1,
|
|
||||||
);
|
|
||||||
println!(
|
|
||||||
" Copying DATA from {:08x}-{:08x} to {:08x}-{:08x} ({} bytes long)",
|
|
||||||
src_addr as usize,
|
|
||||||
src_addr as u32 + prog.data_size,
|
|
||||||
top as usize,
|
|
||||||
top as u32 + prog.data_size,
|
|
||||||
prog.data_size
|
|
||||||
);
|
|
||||||
memcpy(top, src_addr, prog.data_size as usize + 1);
|
|
||||||
|
|
||||||
// Zero out the remaining data.
|
|
||||||
println!(
|
|
||||||
" Zeroing out DATA from {:08x} - {:08x}",
|
|
||||||
top.add(prog.data_size as usize / mem::size_of::<usize>()) as usize,
|
|
||||||
top.add(load_size_rounded as usize / mem::size_of::<usize>()) as usize
|
|
||||||
);
|
|
||||||
bzero(
|
|
||||||
top.add(prog.data_size as usize / mem::size_of::<usize>()),
|
|
||||||
top.add(load_size_rounded as usize / mem::size_of::<usize>()),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
} else if tag.name == u32::from_le_bytes(*b"IniF") {
|
|
||||||
_pid += 1;
|
|
||||||
let mut page_addr: usize = 0;
|
|
||||||
let mut previous_addr: usize = 0;
|
|
||||||
let mut previous_ram_zero_addr: usize = 0;
|
|
||||||
let mut top = core::ptr::null_mut::<u8>();
|
|
||||||
|
|
||||||
let inif = MiniElf::new(&tag);
|
|
||||||
let mut src_addr = unsafe {
|
|
||||||
cfg.base_addr
|
|
||||||
.add(inif.load_offset as usize / mem::size_of::<usize>())
|
|
||||||
} as *const u8;
|
|
||||||
// let mut accumulated_len = 0;
|
|
||||||
|
|
||||||
println!("\n\nIniF {} has {} sections", _pid, inif.sections.len());
|
|
||||||
println!("Initial top: {:x}, extra_pages: {:x}, init_size: {:x}, base_addr: {:x}", cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32);
|
|
||||||
for section in inif.sections.iter() {
|
|
||||||
let flags = section.flags() as u8;
|
|
||||||
// any section that requires "write" must be copied to RAM
|
|
||||||
// note that ELF helpfully adds a 4096-byte gap between non-write pages and write-pages
|
|
||||||
// allowing us to just trundle through the pages and not have to deal with partially
|
|
||||||
// writeable pages.
|
|
||||||
let copy_to_ram = flags & MINIELF_FLG_W != 0;
|
|
||||||
if (section.virt as usize) < previous_addr {
|
|
||||||
panic!("init section addresses are not strictly increasing (new virt: {:08x}, last virt: {:08x})", section.virt, previous_addr);
|
|
||||||
}
|
|
||||||
if copy_to_ram {
|
|
||||||
let this_page = section.virt as usize & !(PAGE_SIZE - 1);
|
|
||||||
let mut bytes_to_copy = section.len();
|
|
||||||
|
|
||||||
if VDBG {println!(
|
|
||||||
"Section is {} bytes long, loaded to {:08x}",
|
|
||||||
bytes_to_copy, section.virt
|
|
||||||
);}
|
|
||||||
// If this is not a new page, ensure the uninitialized values from between
|
|
||||||
// this section and the previous one are all zeroed out.
|
|
||||||
if this_page != page_addr || previous_addr == page_addr {
|
|
||||||
if VDBG {println!("New page @ {:08x}", this_page);}
|
|
||||||
if previous_ram_zero_addr != 0 && previous_ram_zero_addr != page_addr && cfg.extra_pages > 0 {
|
|
||||||
if VDBG {println!(
|
|
||||||
"Zeroing-out remainder of previous page: {:08x} (mapped to physical address {:08x})",
|
|
||||||
previous_ram_zero_addr, top as usize,
|
|
||||||
);}
|
|
||||||
unsafe {
|
unsafe {
|
||||||
bzero(
|
bzero(
|
||||||
top.add(previous_ram_zero_addr as usize & (PAGE_SIZE - 1)),
|
top.add(first_chunk_offset),
|
||||||
top.add(PAGE_SIZE as usize),
|
top.add(first_chunk_offset + first_chunk_size),
|
||||||
)
|
);
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allocate a new page.
|
|
||||||
cfg.extra_pages += 1;
|
|
||||||
top = cfg.get_top() as *mut u8;
|
|
||||||
|
|
||||||
// Zero out the page, if necessary.
|
|
||||||
unsafe { bzero(top, top.add(section.virt as usize & (PAGE_SIZE - 1))) };
|
|
||||||
} else {
|
|
||||||
if VDBG {println!("Reusing existing page @ {:08x}", this_page);}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Part 1: Copy the first chunk over.
|
|
||||||
let mut first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1));
|
|
||||||
if first_chunk_size > section.len() {
|
|
||||||
first_chunk_size = section.len();
|
|
||||||
}
|
|
||||||
let first_chunk_offset = section.virt as usize & (PAGE_SIZE - 1);
|
|
||||||
if VDBG {println!(
|
|
||||||
"Section chunk is {} bytes, {} from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})",
|
|
||||||
first_chunk_size,
|
|
||||||
if section.no_copy() { "zeroing" } else { "copying" },
|
|
||||||
src_addr as usize,
|
|
||||||
unsafe { src_addr.add(first_chunk_size) as usize },
|
|
||||||
unsafe { top.add(first_chunk_offset) as usize },
|
|
||||||
unsafe { top.add(first_chunk_size + first_chunk_offset)
|
|
||||||
as usize },
|
|
||||||
this_page + first_chunk_offset,
|
|
||||||
);}
|
|
||||||
// Perform the copy, if NOCOPY is not set
|
|
||||||
if !section.no_copy() {
|
|
||||||
unsafe {
|
|
||||||
memcpy(top.add(first_chunk_offset), src_addr, first_chunk_size);
|
|
||||||
src_addr = src_addr.add(first_chunk_size);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unsafe {
|
|
||||||
bzero(
|
|
||||||
top.add(first_chunk_offset),
|
|
||||||
top.add(first_chunk_offset + first_chunk_size),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bytes_to_copy -= first_chunk_size;
|
|
||||||
|
|
||||||
// Part 2: Copy any full pages.
|
|
||||||
while bytes_to_copy >= PAGE_SIZE {
|
|
||||||
cfg.extra_pages += 1;
|
|
||||||
top = cfg.get_top() as *mut u8;
|
|
||||||
// println!(
|
|
||||||
// "Copying next page from {:08x} {:08x} ({} bytes to go...)",
|
|
||||||
// src_addr as usize, top as usize, bytes_to_copy
|
|
||||||
// );
|
|
||||||
if !section.no_copy() {
|
|
||||||
unsafe {
|
|
||||||
memcpy(top, src_addr, PAGE_SIZE);
|
|
||||||
src_addr = src_addr.add(PAGE_SIZE);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
unsafe { bzero(top, top.add(PAGE_SIZE)) };
|
|
||||||
}
|
}
|
||||||
bytes_to_copy -= PAGE_SIZE;
|
bytes_to_copy -= first_chunk_size;
|
||||||
|
|
||||||
|
// Part 2: Copy any full pages.
|
||||||
|
while bytes_to_copy >= PAGE_SIZE {
|
||||||
|
cfg.extra_pages += 1;
|
||||||
|
top = cfg.get_top() as *mut u8;
|
||||||
|
// println!(
|
||||||
|
// "Copying next page from {:08x} {:08x} ({} bytes to go...)",
|
||||||
|
// src_addr as usize, top as usize, bytes_to_copy
|
||||||
|
// );
|
||||||
|
if !section.no_copy() {
|
||||||
|
unsafe {
|
||||||
|
memcpy(top, src_addr, PAGE_SIZE);
|
||||||
|
src_addr = src_addr.add(PAGE_SIZE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unsafe { bzero(top, top.add(PAGE_SIZE)) };
|
||||||
|
}
|
||||||
|
bytes_to_copy -= PAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part 3: Copy the final residual partial page
|
||||||
|
if bytes_to_copy > 0 {
|
||||||
|
if VDBG {println!(
|
||||||
|
"Copying final section -- {} bytes @ {:08x}",
|
||||||
|
bytes_to_copy,
|
||||||
|
section.virt + (section.len() as u32) - (bytes_to_copy as u32)
|
||||||
|
);}
|
||||||
|
cfg.extra_pages += 1;
|
||||||
|
top = cfg.get_top() as *mut u8;
|
||||||
|
if !section.no_copy() {
|
||||||
|
unsafe {
|
||||||
|
memcpy(top, src_addr, bytes_to_copy);
|
||||||
|
src_addr = src_addr.add(bytes_to_copy);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unsafe { bzero(top, top.add(bytes_to_copy)) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
previous_ram_zero_addr = section.virt as usize + section.len();
|
||||||
|
} else {
|
||||||
|
top = cfg.get_top() as *mut u8;
|
||||||
|
src_addr = unsafe {src_addr.add(section.len())};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Part 3: Copy the final residual partial page
|
previous_addr = section.virt as usize + section.len();
|
||||||
if bytes_to_copy > 0 {
|
page_addr = previous_addr & !(PAGE_SIZE - 1);
|
||||||
if VDBG {println!(
|
if VDBG {
|
||||||
"Copying final section -- {} bytes @ {:08x}",
|
println!("Looping to the next section");
|
||||||
bytes_to_copy,
|
println!("top: {:x}, extra_pages: {:x}, init_size: {:x}, base_addr: {:x}", cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32);
|
||||||
section.virt + (section.len() as u32) - (bytes_to_copy as u32)
|
println!("previous_addr: {:x}, page_addr: {:x}", previous_addr, page_addr);
|
||||||
);}
|
|
||||||
cfg.extra_pages += 1;
|
|
||||||
top = cfg.get_top() as *mut u8;
|
|
||||||
if !section.no_copy() {
|
|
||||||
unsafe {
|
|
||||||
memcpy(top, src_addr, bytes_to_copy);
|
|
||||||
src_addr = src_addr.add(bytes_to_copy);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unsafe { bzero(top, top.add(bytes_to_copy)) };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
previous_ram_zero_addr = section.virt as usize + section.len();
|
|
||||||
} else {
|
|
||||||
top = cfg.get_top() as *mut u8;
|
|
||||||
src_addr = unsafe {src_addr.add(section.len())};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
previous_addr = section.virt as usize + section.len();
|
println!("Done with sections");
|
||||||
page_addr = previous_addr & !(PAGE_SIZE - 1);
|
if previous_addr as usize & (PAGE_SIZE - 1) != 0 {
|
||||||
if VDBG {
|
println!("Zeroing out remaining data");
|
||||||
println!("Looping to the next section");
|
// Zero-out the trailing bytes
|
||||||
println!("top: {:x}, extra_pages: {:x}, init_size: {:x}, base_addr: {:x}", cfg.get_top() as *mut u8 as u32, cfg.extra_pages, cfg.init_size, cfg.base_addr as u32);
|
unsafe {
|
||||||
println!("previous_addr: {:x}, page_addr: {:x}", previous_addr, page_addr);
|
bzero(
|
||||||
|
top.add(previous_addr as usize & (PAGE_SIZE - 1)),
|
||||||
|
top.add(PAGE_SIZE as usize),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
println!("Skipping zero step -- we ended on a page boundary");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
TagType::XKrn => {
|
||||||
|
let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
|
||||||
|
|
||||||
println!("Done with sections");
|
// TEXT SECTION
|
||||||
if previous_addr as usize & (PAGE_SIZE - 1) != 0 {
|
// Round it off to a page boundary
|
||||||
println!("Zeroing out remaining data");
|
let load_size_rounded = (prog.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||||
// Zero-out the trailing bytes
|
cfg.extra_pages += load_size_rounded / PAGE_SIZE;
|
||||||
|
let top = cfg.get_top();
|
||||||
|
println!("\n\nKernel top: {:x}, extra_pages: {:x}", top as u32, cfg.extra_pages);
|
||||||
unsafe {
|
unsafe {
|
||||||
|
// Copy the program to the target address, rounding it off to the load size.
|
||||||
|
let src_addr = cfg
|
||||||
|
.base_addr
|
||||||
|
.add(prog.load_offset as usize / mem::size_of::<usize>());
|
||||||
|
println!(
|
||||||
|
" Copying TEXT from {:08x}-{:08x} to {:08x}-{:08x} ({} bytes long)",
|
||||||
|
src_addr as usize,
|
||||||
|
src_addr as u32 + prog.text_size,
|
||||||
|
top as usize,
|
||||||
|
top as u32 + prog.text_size + 4,
|
||||||
|
prog.text_size + 4
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
" Zeroing out TEXT from {:08x}-{:08x}",
|
||||||
|
top.add(prog.text_size as usize / mem::size_of::<usize>()) as usize,
|
||||||
|
top.add(load_size_rounded as usize / mem::size_of::<usize>()) as usize,
|
||||||
|
);
|
||||||
|
|
||||||
|
memcpy(top, src_addr, prog.text_size as usize + 1);
|
||||||
|
|
||||||
|
// Zero out the remaining data.
|
||||||
bzero(
|
bzero(
|
||||||
top.add(previous_addr as usize & (PAGE_SIZE - 1)),
|
top.add(prog.text_size as usize / mem::size_of::<usize>()),
|
||||||
top.add(PAGE_SIZE as usize),
|
top.add(load_size_rounded as usize / mem::size_of::<usize>()),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
println!("Skipping zero step -- we ended on a page boundary");
|
// DATA SECTION
|
||||||
|
// Round it off to a page boundary
|
||||||
|
let load_size_rounded =
|
||||||
|
((prog.data_size + prog.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||||
|
cfg.extra_pages += load_size_rounded / PAGE_SIZE;
|
||||||
|
let top = cfg.get_top();
|
||||||
|
unsafe {
|
||||||
|
// Copy the program to the target address, rounding it off to the load size.
|
||||||
|
let src_addr = cfg.base_addr.add(
|
||||||
|
(prog.load_offset + prog.text_size + 4) as usize / mem::size_of::<usize>() - 1,
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
" Copying DATA from {:08x}-{:08x} to {:08x}-{:08x} ({} bytes long)",
|
||||||
|
src_addr as usize,
|
||||||
|
src_addr as u32 + prog.data_size,
|
||||||
|
top as usize,
|
||||||
|
top as u32 + prog.data_size,
|
||||||
|
prog.data_size
|
||||||
|
);
|
||||||
|
memcpy(top, src_addr, prog.data_size as usize + 1);
|
||||||
|
|
||||||
|
// Zero out the remaining data.
|
||||||
|
println!(
|
||||||
|
" Zeroing out DATA from {:08x} - {:08x}",
|
||||||
|
top.add(prog.data_size as usize / mem::size_of::<usize>()) as usize,
|
||||||
|
top.add(load_size_rounded as usize / mem::size_of::<usize>()) as usize
|
||||||
|
);
|
||||||
|
bzero(
|
||||||
|
top.add(prog.data_size as usize / mem::size_of::<usize>()),
|
||||||
|
top.add(load_size_rounded as usize / mem::size_of::<usize>()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue