mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
implement the writepage mechanism
1 - lock the CPU 2 - go into userspace to do the write 3 - on exit from userspace, mark the page as swapped in the original process so that if it tries to read the data it fetches a new clean copy 4 - free the page from the RPT so the data can be grabbed later 5 - unlock the CPU
This commit is contained in:
parent
24a70b0eb5
commit
1a34847b19
2 changed files with 40 additions and 106 deletions
|
|
@ -277,60 +277,6 @@ fn get_free_pages() -> usize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// De-allocate dirty SPI FLASH pages: write to SPI, then dispose of the page
|
|
||||||
fn dealloc_dirty(
|
|
||||||
ss: &mut SwapperSharedState,
|
|
||||||
candidate: SwapAlloc,
|
|
||||||
errs: &mut usize,
|
|
||||||
pages_to_free: &mut usize,
|
|
||||||
) -> core::result::Result<(), xous::Error> {
|
|
||||||
// step 1: steal the page from the other process. Its data gets mapped into the
|
|
||||||
// swapper as `local_ptr`. This will also unmap the page from memory.
|
|
||||||
let vaddr_in_swap = match xous::rsyscall(xous::SysCall::SwapOp(
|
|
||||||
SwapAbi::StealPage as usize,
|
|
||||||
candidate.raw_pid() as usize,
|
|
||||||
candidate.vaddr(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
)) {
|
|
||||||
Ok(Result::Scalar5(page_ptr, _, _, _, _)) => page_ptr,
|
|
||||||
Ok(_) => panic!("Malformed return value"),
|
|
||||||
Err(_e) => {
|
|
||||||
*errs += 1;
|
|
||||||
return Err(xous::Error::ShareViolation); // try another page
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// step 2: write the data to memory
|
|
||||||
let buf = unsafe { core::slice::from_raw_parts(vaddr_in_swap as *const u8, PAGE_SIZE) };
|
|
||||||
let offset = vaddr_in_swap & 0x0FFF_FFFF;
|
|
||||||
#[cfg(feature = "debug-print-swapper")]
|
|
||||||
writeln!(DebugUart {}, "DAD: PID{} VA {:x} buf {:x?}", candidate.raw_pid(), offset, &buf[..8]).ok();
|
|
||||||
|
|
||||||
if !RENODE_TESTING {
|
|
||||||
ss.hal.flash_write(buf, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
// step 3: release the page (currently mapped into the swapper's memory space). Need
|
|
||||||
// to demonstrate to the memory system that we know what we are
|
|
||||||
// doing by also presenting the original PID that owned the page.
|
|
||||||
xous::rsyscall(xous::SysCall::SwapOp(
|
|
||||||
SwapAbi::ReleaseMemory as usize,
|
|
||||||
vaddr_in_swap,
|
|
||||||
candidate.raw_pid() as usize,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
))
|
|
||||||
.expect("Unexpected error: couldn't release a page that was mapped into the swapper's space");
|
|
||||||
*pages_to_free -= 1;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Core of write_to_swap.
|
/// Core of write_to_swap.
|
||||||
fn write_to_swap_inner(
|
fn write_to_swap_inner(
|
||||||
ss: &mut SwapperSharedState,
|
ss: &mut SwapperSharedState,
|
||||||
|
|
@ -550,6 +496,8 @@ fn swap_handler(
|
||||||
let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) };
|
let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) };
|
||||||
let offset = vaddr_in_pid & 0x0FFF_FFFF; // mask out the top nibble to derive the offset in SPI flash
|
let offset = vaddr_in_pid & 0x0FFF_FFFF; // mask out the top nibble to derive the offset in SPI flash
|
||||||
ss.hal.flash_read(buf, offset);
|
ss.hal.flash_read(buf, offset);
|
||||||
|
#[cfg(feature = "debug-print-swapper")]
|
||||||
|
writeln!(DebugUart {}, "RF*F* PID{} VA {:x}, {:x?}", pid, vaddr_in_pid, &buf[..8]).ok();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// walk the PT to find the swap data, and remove it from the swap PT
|
// walk the PT to find the swap data, and remove it from the swap PT
|
||||||
|
|
@ -672,16 +620,8 @@ fn swap_handler(
|
||||||
{
|
{
|
||||||
wired += 1;
|
wired += 1;
|
||||||
} else {
|
} else {
|
||||||
if candidate.is_dirty() {
|
// errors are ignored because the correct behavior on error is to try another page
|
||||||
// handles the case of "dirty" pages waiting to be committed to SPI FLASH
|
write_to_swap_inner(ss, candidate, &mut errs, &mut pages_to_free).ok();
|
||||||
writeln!(DebugUart {}, "OOM found dirty FLASH @ {:x?}", candidate).ok();
|
|
||||||
dealloc_dirty(ss, candidate, &mut errs, &mut pages_to_free).ok();
|
|
||||||
} else {
|
|
||||||
// this is the typical case of pages of internal RAM destined for external RAM
|
|
||||||
|
|
||||||
// errors are ignored because the correct behavior on error is to try another page
|
|
||||||
write_to_swap_inner(ss, candidate, &mut errs, &mut pages_to_free).ok();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
writeln!(
|
writeln!(
|
||||||
|
|
@ -720,15 +660,14 @@ fn swap_handler(
|
||||||
}
|
}
|
||||||
// This just writes data to FLASH, but does not de-allocate the page
|
// This just writes data to FLASH, but does not de-allocate the page
|
||||||
Some(KernelOp::WriteToFlash) => {
|
Some(KernelOp::WriteToFlash) => {
|
||||||
let _pid = a2 as u8;
|
let vaddr_in_swap = a2;
|
||||||
let _vaddr_in_pid = a3;
|
let flash_offset = a3;
|
||||||
let vaddr_in_swap = a4;
|
|
||||||
|
|
||||||
let buf = unsafe { core::slice::from_raw_parts(vaddr_in_swap as *const u8, PAGE_SIZE) };
|
let buf = unsafe { core::slice::from_raw_parts(vaddr_in_swap as *const u8, PAGE_SIZE) };
|
||||||
let offset = vaddr_in_swap & 0x0FFF_FFFF;
|
let offset = flash_offset & 0x0FFF_FFFF;
|
||||||
if !RENODE_TESTING {
|
if !RENODE_TESTING {
|
||||||
#[cfg(feature = "debug-print-swapper")]
|
#[cfg(feature = "debug-print-swapper")]
|
||||||
writeln!(DebugUart {}, "WT*F*: PID{} VA {:x} buf {:x?}", _pid, offset, &buf[..8]).ok();
|
writeln!(DebugUart {}, "WT*F*: VA {:x} buf {:x?}", offset, &buf[..8]).ok();
|
||||||
ss.hal.flash_write(buf, offset);
|
ss.hal.flash_write(buf, offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -834,42 +773,25 @@ fn main() {
|
||||||
scalar.arg1 = free_pages;
|
scalar.arg1 = free_pages;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Opcode::FlashSync) => {
|
Some(Opcode::WritePage) => {
|
||||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
let mem_msg = msg.body.memory_message().unwrap();
|
||||||
let specified = scalar.arg1 != 0;
|
let offset = mem_msg.offset.expect("malformed WritePage").get();
|
||||||
let base = scalar.arg2;
|
log::info!(
|
||||||
let len_bytes = scalar.arg3;
|
"WritePage: PID{}, offset {:x}, vaddr_buf {:x}",
|
||||||
let sender_pid = msg.sender.pid().unwrap().get();
|
msg.sender.pid().unwrap().get() as usize,
|
||||||
if specified {
|
offset,
|
||||||
xous::rsyscall(xous::SysCall::SwapOp(
|
mem_msg.buf.as_ptr() as usize
|
||||||
SwapAbi::Sync as usize,
|
);
|
||||||
base,
|
xous::rsyscall(xous::SysCall::SwapOp(
|
||||||
len_bytes,
|
SwapAbi::WritePage as usize,
|
||||||
sender_pid as usize,
|
msg.sender.pid().unwrap().get() as usize,
|
||||||
0,
|
offset,
|
||||||
0,
|
mem_msg.buf.as_ptr() as usize,
|
||||||
0,
|
0,
|
||||||
))
|
0,
|
||||||
.expect("Couldn't mark region as dirty");
|
0,
|
||||||
} else {
|
))
|
||||||
#[cfg(any(feature = "precursor", feature = "renode"))]
|
.expect("couldn't WritePage");
|
||||||
let region_len = precursor_hal::board::PDDB_LEN;
|
|
||||||
#[cfg(feature = "cramium-soc")]
|
|
||||||
let region_len = cramium_hal::board::SPINOR_LEN;
|
|
||||||
|
|
||||||
xous::rsyscall(xous::SysCall::SwapOp(
|
|
||||||
SwapAbi::Sync as usize,
|
|
||||||
xous::arch::MMAP_VIRT_BASE,
|
|
||||||
region_len as usize,
|
|
||||||
sender_pid as usize,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
))
|
|
||||||
.expect("Couldn't mark region as dirty");
|
|
||||||
}
|
|
||||||
scalar.arg1 = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[cfg(feature = "swap-userspace-testing")]
|
#[cfg(feature = "swap-userspace-testing")]
|
||||||
Some(Opcode::Test0) => {
|
Some(Opcode::Test0) => {
|
||||||
|
|
|
||||||
|
|
@ -191,6 +191,8 @@ impl SwapHal {
|
||||||
/// This wrapper handles arbitrary alignments of `offset` and sizes of `buf`
|
/// This wrapper handles arbitrary alignments of `offset` and sizes of `buf`
|
||||||
/// `offset` is the offset from the start of FLASH in bytes.
|
/// `offset` is the offset from the start of FLASH in bytes.
|
||||||
pub fn flash_write(&mut self, buf: &[u8], offset: usize) {
|
pub fn flash_write(&mut self, buf: &[u8], offset: usize) {
|
||||||
|
#[cfg(feature = "debug-verbose")]
|
||||||
|
writeln!(DebugUart {}, "flash_write offset: {:x}", offset).ok();
|
||||||
let mut written = 0;
|
let mut written = 0;
|
||||||
|
|
||||||
// compute amount of data in the FLASH sector buffer to preserve: everything up to the offset
|
// compute amount of data in the FLASH sector buffer to preserve: everything up to the offset
|
||||||
|
|
@ -230,7 +232,15 @@ impl SwapHal {
|
||||||
// 2. write remaining pages
|
// 2. write remaining pages
|
||||||
let remaining_len = buf.len() - written;
|
let remaining_len = buf.len() - written;
|
||||||
// this is OK here because we took care of any misaligned start data in step 1.
|
// this is OK here because we took care of any misaligned start data in step 1.
|
||||||
let aligned_start = (offset + (FLASH_SECTOR_LEN - 1)) & !(FLASH_SECTOR_LEN);
|
let aligned_start = (offset + (FLASH_SECTOR_LEN - 1)) & !(FLASH_SECTOR_LEN - 1);
|
||||||
|
#[cfg(feature = "debug-verbose")]
|
||||||
|
writeln!(
|
||||||
|
DebugUart {},
|
||||||
|
"aligned_start {:x} end {:x}",
|
||||||
|
aligned_start,
|
||||||
|
(aligned_start + (remaining_len + (FLASH_SECTOR_LEN - 1)) & !(FLASH_SECTOR_LEN - 1))
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
for sector in (aligned_start
|
for sector in (aligned_start
|
||||||
..(aligned_start + (remaining_len + (FLASH_SECTOR_LEN - 1)) & !(FLASH_SECTOR_LEN - 1)))
|
..(aligned_start + (remaining_len + (FLASH_SECTOR_LEN - 1)) & !(FLASH_SECTOR_LEN - 1)))
|
||||||
.step_by(FLASH_SECTOR_LEN)
|
.step_by(FLASH_SECTOR_LEN)
|
||||||
|
|
@ -256,6 +266,8 @@ impl SwapHal {
|
||||||
.chunks_exact(FLASH_PAGE_LEN)
|
.chunks_exact(FLASH_PAGE_LEN)
|
||||||
.zip((sector..(sector + FLASH_SECTOR_LEN)).step_by(FLASH_PAGE_LEN))
|
.zip((sector..(sector + FLASH_SECTOR_LEN)).step_by(FLASH_PAGE_LEN))
|
||||||
{
|
{
|
||||||
|
#[cfg(feature = "debug-verbose")]
|
||||||
|
writeln!(DebugUart {}, "write_page {:x} {:x?}", addr, &page[..8]).ok();
|
||||||
self.flash_spim.borrow_mut().mem_flash_write_page(addr as u32, page.try_into().unwrap());
|
self.flash_spim.borrow_mut().mem_flash_write_page(addr as u32, page.try_into().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue