able to detect the clean suspend, now figuring out how to resume

This commit is contained in:
bunnie 2021-04-30 05:00:03 +08:00
parent 0e2d57c9fb
commit 7053c2b7c6
2 changed files with 116 additions and 72 deletions

View file

@ -36,6 +36,8 @@ const FLG_A: usize = 0x40;
const FLG_D: usize = 0x80;
const STACK_PAGE_COUNT: usize = 5;
const VDBG: bool = false; // verbose debug
mod debug;
mod fonts;
@ -263,7 +265,7 @@ impl MiniElf {
// Example: Page starts at 0xf0c0 and is 128 bytes long
// 1. Copy 128 bytes to page 1
for section in self.sections {
println!(" Section @ {:08x}", section.virt as usize);
if VDBG {println!(" Section @ {:08x}", section.virt as usize);}
let flag_defaults = FLG_U
| FLG_R
| if section.flags() & 1 == 1 { FLG_W } else { 0 }
@ -279,7 +281,7 @@ impl MiniElf {
// 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 != current_page_addr {
println!("1 {:08x} -> {:08x}", top as usize, this_page);
if VDBG {println!("1 {:08x} -> {:08x}", top as usize, this_page);}
allocator.map_page(satp, top as usize, this_page, flag_defaults);
allocator.change_owner(pid as XousPid, top as usize);
allocated_bytes += PAGE_SIZE;
@ -293,31 +295,31 @@ impl MiniElf {
}
bytes_to_copy -= first_chunk_size;
} else {
println!(
if VDBG {println!(
"This page is {:08x}, and last page was {:08x}",
this_page, current_page_addr
);
);}
// This is a continuation of the previous section, and as a result
// the memory will have been copied already. Avoid copying this data
// to a new page.
let first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1));
println!("First chunk size: {}", first_chunk_size);
if VDBG {println!("First chunk size: {}", first_chunk_size);}
if bytes_to_copy < first_chunk_size {
bytes_to_copy = 0;
println!("Clamping to 0 bytes");
if VDBG {println!("Clamping to 0 bytes");}
} else {
bytes_to_copy -= first_chunk_size;
println!(
if VDBG {println!(
"Clamping to {} bytes by cutting off {} bytes",
bytes_to_copy, first_chunk_size
);
);}
}
this_page += PAGE_SIZE;
}
// Part 2: Copy any full pages.
while bytes_to_copy > PAGE_SIZE {
println!("2 {:08x} -> {:08x}", top as usize, this_page);
if VDBG {println!("2 {:08x} -> {:08x}", top as usize, this_page);}
allocator.map_page(satp, top as usize, this_page, flag_defaults);
allocator.change_owner(pid as XousPid, top as usize);
allocated_bytes += PAGE_SIZE;
@ -329,7 +331,7 @@ impl MiniElf {
// Part 3: Copy the final residual partial page
if bytes_to_copy > 0 {
let this_page = (section.virt as usize + section.len()) & !(PAGE_SIZE - 1);
println!("3 {:08x} -> {:08x}", top as usize, this_page);
if VDBG {println!("3 {:08x} -> {:08x}", top as usize, this_page);}
allocator.map_page(satp, top as usize, this_page, flag_defaults);
allocator.change_owner(pid as XousPid, top as usize);
allocated_bytes += PAGE_SIZE;
@ -393,6 +395,7 @@ extern "C" {
) -> !;
}
impl ProgramDescription {
/// Map this ProgramDescription into RAM.
/// The program may already have been relocated, and so may be
@ -481,11 +484,11 @@ impl ProgramDescription {
// let load_size_rounded = (self.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
for offset in (0..self.text_size as usize).step_by(PAGE_SIZE) {
println!(
if VDBG {println!(
" TEXT: Mapping {:08x} -> {:08x}",
load_offset + offset + rounded_data_bss,
self.text_offset as usize + offset
);
);}
allocator.map_page(
satp,
load_offset + offset + rounded_data_bss,
@ -498,11 +501,11 @@ impl ProgramDescription {
// Map the process data section into RAM.
for offset in (0..(self.data_size + self.bss_size) as usize).step_by(PAGE_SIZE as usize) {
// let page_addr = allocator.alloc();
println!(
if VDBG {println!(
" DATA: Mapping {:08x} -> {:08x}",
load_offset + offset,
self.data_offset as usize + offset
);
);}
allocator.map_page(
satp,
load_offset + offset,
@ -531,7 +534,7 @@ unsafe fn bzero<T>(mut sbss: *mut T, ebss: *mut T)
where
T: Copy,
{
println!("ZERO: {:08x} - {:08x}", sbss as usize, ebss as usize);
if VDBG {println!("ZERO: {:08x} - {:08x}", sbss as usize, ebss as usize);}
while sbss < ebss {
// NOTE(volatile) to prevent this from being transformed into `memclr`
ptr::write_volatile(sbss, mem::zeroed());
@ -544,14 +547,14 @@ unsafe fn memcpy<T>(dest: *mut T, src: *const T, count: usize)
where
T: Copy,
{
println!(
if VDBG {println!(
"COPY: {:08x} - {:08x} {} {:08x} - {:08x}",
src as usize,
src as usize + count,
count,
dest as usize,
dest as usize + count
);
);}
let mut offset = 0;
while offset < (count / mem::size_of::<T>()) {
dest.add(offset)
@ -647,19 +650,19 @@ fn copy_processes(cfg: &mut BootConfig) {
let this_page = section.virt as usize & !(PAGE_SIZE - 1);
let mut bytes_to_copy = section.len();
println!(
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 {
println!("New page @ {:08x}", this_page);
if VDBG {println!("New page @ {:08x}", this_page);}
if previous_addr != 0 {
println!(
if VDBG {println!(
"Zeroing-out remainder of previous page: {:08x} (mapped to physical address {:08x})",
previous_addr, top as usize,
);
);}
unsafe {
bzero(
top.add(previous_addr as usize & (PAGE_SIZE - 1)),
@ -675,7 +678,7 @@ fn copy_processes(cfg: &mut BootConfig) {
// Zero out the page, if necessary.
unsafe { bzero(top, top.add(section.virt as usize & (PAGE_SIZE - 1))) };
} else {
println!("Reusing existing page @ {:08x}", this_page);
if VDBG {println!("Reusing existing page @ {:08x}", this_page);}
}
// Part 1: Copy the first chunk over.
@ -684,7 +687,7 @@ fn copy_processes(cfg: &mut BootConfig) {
first_chunk_size = section.len();
}
let first_chunk_offset = section.virt as usize & (PAGE_SIZE - 1);
println!(
if VDBG {println!(
"First chunk is {} bytes, copying from {:08x}:{:08x} -> {:08x}:{:08x} (virt: {:08x})",
first_chunk_size,
src_addr as usize,
@ -693,7 +696,7 @@ fn copy_processes(cfg: &mut BootConfig) {
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 {
@ -731,11 +734,11 @@ fn copy_processes(cfg: &mut BootConfig) {
// Part 3: Copy the final residual partial page
if bytes_to_copy > 0 {
println!(
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() {
@ -750,7 +753,7 @@ fn copy_processes(cfg: &mut BootConfig) {
previous_addr = section.virt as usize + section.len();
page_addr = previous_addr & !(PAGE_SIZE - 1);
println!("Looping to the next section");
if VDBG {println!("Looping to the next section");}
}
println!("Done with sections, zeroing out remaining data");
@ -1110,35 +1113,70 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
println!(" Small @ {:08x}", fonts::small::DATA_SMALL.as_ptr() as u32);
println!(" Bold @ {:08x}", fonts::bold::DATA_BOLD.as_ptr() as u32);
// The MMU should be set up now, and memory pages assigned to their
// respective processes.
let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size;
let arg_offset = cfg.args.base as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
let ip_offset = cfg.processes.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
let rpt_offset =
cfg.runtime_page_tracker.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
println!(
"Jumping to kernel @ {:08x} with map @ {:08x} and stack @ {:08x} (kargs: {:08x}, ip: {:08x}, rpt: {:08x})",
cfg.processes[0].entrypoint, cfg.processes[0].satp, cfg.processes[0].sp,
arg_offset, ip_offset, rpt_offset,
);
unsafe {
start_kernel(
arg_offset,
ip_offset,
rpt_offset,
cfg.processes[0].satp,
cfg.processes[0].entrypoint,
cfg.processes[0].sp,
cfg.debug,
clean,
if !clean {
// The MMU should be set up now, and memory pages assigned to their
// respective processes.
let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size;
let arg_offset = cfg.args.base as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
let ip_offset = cfg.processes.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
let rpt_offset =
cfg.runtime_page_tracker.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
println!(
"Jumping to kernel @ {:08x} with map @ {:08x} and stack @ {:08x} (kargs: {:08x}, ip: {:08x}, rpt: {:08x})",
cfg.processes[0].entrypoint, cfg.processes[0].satp, cfg.processes[0].sp,
arg_offset, ip_offset, rpt_offset,
);
unsafe {
let backup_args: *mut [u32; 7] = 0x40FF_F000 as *mut[u32; 7];
(*backup_args)[0] = arg_offset as u32;
(*backup_args)[1] = ip_offset as u32;
(*backup_args)[2] = rpt_offset as u32;
(*backup_args)[3] = cfg.processes[0].satp as u32;
(*backup_args)[4] = cfg.processes[0].entrypoint as u32;
(*backup_args)[5] = cfg.processes[0].sp as u32;
(*backup_args)[6] = if cfg.debug {1} else {0};
println!("Backup kernel args:");
for i in 0..7 {
println!("0x{:08x}", (*backup_args)[i]);
}
}
unsafe {
start_kernel(
arg_offset,
ip_offset,
rpt_offset,
cfg.processes[0].satp,
cfg.processes[0].entrypoint,
cfg.processes[0].sp,
cfg.debug,
clean,
);
}
} else {
unsafe {
let backup_args: *mut [u32; 7] = 0x40FF_F000 as *mut[u32; 7];
println!("Using backed up kernel args:");
for i in 0..7 {
println!("0x{:08x}", (*backup_args)[i]);
}
start_kernel(
(*backup_args)[0] as usize,
(*backup_args)[1] as usize,
(*backup_args)[2] as usize,
(*backup_args)[3] as usize,
(*backup_args)[4] as usize,
(*backup_args)[5] as usize,
if (*backup_args)[6] == 0 {false} else {true},
clean,
);
}
}
}
fn check_resume(cfg: &mut BootConfig) -> (bool, bool) {
use utralib::generated::*;
const WORDS_PER_SECTOR: usize = 127;
const WORDS_PER_SECTOR: usize = 128;
const NUM_SECTORS: usize = 8;
const WORDS_PER_PAGE: usize = PAGE_SIZE / 4;
@ -1151,11 +1189,11 @@ fn check_resume(cfg: &mut BootConfig) -> (bool, bool) {
let was_forced_suspend: bool = if unsafe{(*marker)[0]} != 0 { true } else { false };
let mut clean = true;
let mut hashbuf: [u32; WORDS_PER_SECTOR] = [0; WORDS_PER_SECTOR];
let mut hashbuf: [u32; WORDS_PER_SECTOR - 1] = [0; WORDS_PER_SECTOR - 1];
let mut index: usize = 0;
for sector in 0..NUM_SECTORS {
for i in 0..hashbuf.len() {
hashbuf[i] = unsafe{(*marker)[index * (WORDS_PER_SECTOR + 1) + i]};
hashbuf[i] = unsafe{(*marker)[index * WORDS_PER_SECTOR + i]};
}
// sector 0 contains the boot seeds, which we replace with our own as read out from our FPGA before computing the hash
if sector == 0 {
@ -1163,8 +1201,11 @@ fn check_resume(cfg: &mut BootConfig) -> (bool, bool) {
hashbuf[2] = seed1;
}
let hash = crate::murmur3::murmur3_32(&hashbuf, 0);
if hash != unsafe{(*marker)[index * (WORDS_PER_SECTOR) + 1]} {
if hash != unsafe{(*marker)[(index+1) * WORDS_PER_SECTOR - 1]} {
println!("* computed 0x{:08x} - stored 0x{:08x}", hash, unsafe{(*marker)[(index+1) * (WORDS_PER_SECTOR) - 1]});
clean = false;
} else {
println!(" computed 0x{:08x} - match", hash);
}
index += 1;
}
@ -1286,21 +1327,23 @@ pub fn phase_2(cfg: &mut BootConfig) {
unsafe { (l1_pt_addr as *mut usize).add(1023).write(krn_pg1023_ptr) };
}
println!("PID1 pagetables:");
debug::print_pagetable(cfg.processes[0].satp);
println!();
println!();
for (_pid, process) in cfg.processes[1..].iter().enumerate() {
println!("PID{} pagetables:", _pid + 2);
debug::print_pagetable(process.satp);
if VDBG {
println!("PID1 pagetables:");
debug::print_pagetable(cfg.processes[0].satp);
println!();
println!();
for (_pid, process) in cfg.processes[1..].iter().enumerate() {
println!("PID{} pagetables:", _pid + 2);
debug::print_pagetable(process.satp);
println!();
println!();
}
}
println!(
"Runtime Page Tracker: {} bytes",
cfg.runtime_page_tracker.len()
);
// mark pages used by suspend/resume as unused for later allocation by the susres server
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 1] = 0; // loader stack
// mark pages used by suspend/resume according to their needs
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 1] = 1; // loader stack -- do not touch as it contains kerner args
cfg.runtime_page_tracker[cfg.sram_size / PAGE_SIZE - 2] = 0; // clean suspend page
}

View file

@ -93,7 +93,7 @@ mod implementation {
/// memory region for the "clean suspend" marker
marker: xous::MemoryRange,
/// loader stack region -- this data is dirtied on every resume; claim it in this process so no others accidentally use it
loader_stack: xous::MemoryRange,
//loader_stack: xous::MemoryRange,
/// backing store for the ticktimer value
stored_time: Option<u64>,
/// so we can access the build seed and detect if the FPGA image was changed on us
@ -132,12 +132,12 @@ mod implementation {
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't map clean suspend page");
let loader_stack = xous::syscall::map_memory(
/*let loader_stack = xous::syscall::map_memory(
xous::MemoryAddress::new(0x40FFF000), // this is a special, hard-coded location
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
).expect("couldn't map clean suspend page");
).expect("couldn't map clean suspend page");*/
let seed_csr = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::seed::HW_SEED_BASE),
None,
@ -158,7 +158,7 @@ mod implementation {
os_timer: CSR::new(ostimer_csr.as_mut_ptr() as *mut u32),
stored_time: None,
marker,
loader_stack,
//loader_stack,
seed_csr: CSR::new(seed_csr.as_mut_ptr() as *mut u32),
reboot_csr: CSR::new(reboot_csr.as_mut_ptr() as *mut u32),
};
@ -181,11 +181,12 @@ mod implementation {
// note to self: don't use log:: here because we're upstream of logging being initialized
assert!(unsafe{(*check_marker)[words]} == 0, "marker had non-zero entry!");
}
/*
// clear the loader stack, mostly to get rid of unused code warnings
let stack = loader_stack.as_ptr() as *mut [u32; 1024];
for words in 0..1024 {
unsafe{(*stack)[words] = 0;}
}
}*/
xous::claim_interrupt(
utra::susres::SUSRES_IRQ,
@ -314,10 +315,10 @@ mod implementation {
// clear the loader stack, for no particular reason other than to be vengeful.
let stack = self.loader_stack.as_ptr() as *mut [u32; 1024];
/*let stack = self.loader_stack.as_ptr() as *mut [u32; 1024];
for words in 0..1024 {
unsafe{(*stack)[words] = 0;}
}
}*/
println!("Triggering suspend interrupt");
// trigger an interrupt to process the final suspend bits
@ -368,10 +369,10 @@ mod implementation {
println!("Ticktimer and OS timer now running");
// clear the loader stack, for no other reason other than to be vengeful
let stack = self.loader_stack.as_ptr() as *mut [u32; 1024];
/*let stack = self.loader_stack.as_ptr() as *mut [u32; 1024];
for words in 0..1024 {
unsafe{(*stack)[words] = 0;}
}
}*/
} else {
panic!("Can't resume because the ticktimer value was not saved properly before suspend!")
};