diff --git a/Cargo.lock b/Cargo.lock index 4a4cbba09..f63a6383f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -856,6 +856,7 @@ dependencies = [ "xous-ipc 0.9.62 (registry+https://github.com/rust-lang/crates.io-index)", "xous-pio", "xous-pl230", + "xous-swapper", ] [[package]] diff --git a/kernel/src/arch/riscv/mem.rs b/kernel/src/arch/riscv/mem.rs index 95d1b5d73..d85d38e57 100644 --- a/kernel/src/arch/riscv/mem.rs +++ b/kernel/src/arch/riscv/mem.rs @@ -922,7 +922,7 @@ pub fn ensure_page_exists_inner(address: usize) -> Result Result Result addr, + Err(e) => { + #[cfg(feature = "debug-swap")] + { + crate::arch::mem::MemoryMapping::current().print_map(); + let vpn1 = (vaddr >> 22) & ((1 << 10) - 1); + let l1_pt = unsafe { &(*(PAGE_TABLE_ROOT_OFFSET as *mut RootPageTable)) }; + let l1_pte = l1_pt.entries[vpn1]; + println!( + "evict_page_inner() PTE lookup error. vaddr in PID{}: {:x}, bad l1 pte: {:x}, err {:?}", + target_pid.get(), + vaddr, + l1_pte, + e + ); + } + let swapper_pid = PID::new(xous_kernel::SWAPPER_PID).unwrap(); + let swapper_map = system_services.get_process(swapper_pid).unwrap().mapping; + swapper_map.activate()?; + return Err(e); + } + }; let target_pte = unsafe { entry.read_volatile() }; let target_paddr = (target_pte >> 10) << 12; @@ -1147,6 +1176,17 @@ pub fn evict_page_inner(target_pid: PID, vaddr: usize) -> Result Result { - let phys = phys_ptr as usize; + let mut phys = phys_ptr as usize; let virt = self.find_virtual_address(virt_ptr, size, kind)?; + // Determine if a contiguous chunk of RAM needs to be allocated for a device + // This case happens when we don't specify a physical address, but also specify the DEV + // flag. + let device_ram = (flags & MemoryFlags::DEV == MemoryFlags::DEV) && (phys == 0); + // If no physical address is specified, give the user the next available pages - if phys == 0 { + if phys == 0 && !device_ram { return self.reserve_range(virt, size, flags); } + if device_ram { + // Device RAM allocation: search for contiguous block of physical RAM so we can share + // the pages with e.g. DMA or other hardware resources. + let pages_to_claim = size / PAGE_SIZE; // this is correct because only page-sized requests are allowed. + #[cfg(feature = "debug-swap")] + println!("Device RAM request. Searching for {} contiguous pages.", pages_to_claim); + + // Safety: is it safe to iterate through MEMORY_ALLOCATIONS like this? I'm not actually sure. + // I suppose we could end up in trouble if the kernel is interrupted and the allocation table + // changes, but we don't have a locking mechanism for this sort of thing (yet). Until we have + // a better way to do this, it will have to do! + let mut range_start: Option = None; + let mut current_run = 0; + unsafe { + for (index, entry) in MEMORY_ALLOCATIONS.iter().enumerate() { + if entry.is_none() { + if let Some(_start) = range_start { + current_run += 1; + } else { + range_start = Some(index); + current_run = 1; + } + } else { + range_start = None; + current_run = 0; + } + if current_run >= pages_to_claim { + break; + } + } + } + if let Some(start) = range_start { + if current_run >= pages_to_claim { + // success! + phys = start * PAGE_SIZE + self.ram_start; + #[cfg(feature = "debug-swap")] + println!("Block found starting at {:x}", phys); + } else { + return Err(xous_kernel::Error::OutOfMemory); + } + } else { + // couldn't find a contiguous location large enough; OOM for now. + // Punt to userland to clear up memory. + return Err(xous_kernel::Error::OutOfMemory); + } + } + // 1. Attempt to claim all physical pages in the range for claim_phys in (phys..(phys + size)).step_by(PAGE_SIZE) { if let Err(err) = self.claim_page(claim_phys as *mut usize, pid) { @@ -605,9 +657,21 @@ impl MemoryManager { self.release_page(rel_phys as *mut usize, pid).ok(); } return Err(err); + } else { + #[cfg(feature = "swap")] + if device_ram { + let offset = (claim_phys - self.ram_start) / PAGE_SIZE; + #[cfg(feature = "debug-swap")] + println!("Address {:x} marked as wired", claim_phys); + unsafe { + MEMORY_ALLOCATIONS[offset].set_wired(); + } + } } } + #[cfg(feature = "debug-swap")] + println!("Mapping pa {:x} | va {:x} region of {} bytes", phys, virt as usize, size); // Actually perform the map. At this stage, every physical page should be owned by us. for offset in (0..size).step_by(PAGE_SIZE) { if let Err(e) = crate::arch::mem::map_page_inner( @@ -622,10 +686,30 @@ impl MemoryManager { crate::arch::mem::unmap_page_inner(self, unmap_offset + virt as usize).ok(); self.release_page((unmap_offset + phys) as *mut usize, pid).ok(); } + #[cfg(feature = "debug-swap")] + println!("Error encountered in map_page_inner at last stage of mapping; releasing all pages"); return Err(e); } } + if device_ram { + // The assumption is that device_ram pages are only ever allocated by a userspace call. + // If the kernel uses this to allocate kernel structures, it will fail. + + // clear the memory first + unsafe { crate::mem::bzero(virt, virt.wrapping_add(size)) }; + + // now hand it to userspace + for offset in (0..size).step_by(PAGE_SIZE) { + crate::arch::mem::hand_page_to_user(virt.wrapping_add(offset)) + .expect("couldn't hand page to user"); + } + + // sanity check it + #[cfg(feature = "debug-swap-verbose")] + crate::arch::mem::MemoryMapping::current().print_map(); + } + unsafe { MemoryRange::new(virt as usize, size) } } diff --git a/kernel/src/swap.rs b/kernel/src/swap.rs index 781550735..7e7763566 100644 --- a/kernel/src/swap.rs +++ b/kernel/src/swap.rs @@ -6,7 +6,7 @@ use core::cmp::Ordering; use loader::swap::SWAP_FLG_WIRED; use loader::swap::SWAP_RPT_VADDR; use xous_kernel::SWAPPER_PID; -use xous_kernel::{SysCallResult, PID, SID, TID}; +use xous_kernel::{SysCallResult, PID, TID}; use crate::arch::current_pid; use crate::arch::mem::MMUFlags; @@ -142,6 +142,8 @@ impl SwapAlloc { }); } + pub fn set_wired(&mut self) { self.vpn |= SWAP_FLG_WIRED; } + #[cfg(feature = "debug-swap-verbose")] pub fn get_raw_vpn(&self) -> u32 { self.vpn } @@ -174,7 +176,6 @@ impl Ord for SwapAlloc { #[cfg(baremetal)] #[no_mangle] static mut SWAP: Swap = Swap { - sid: SID::from_u32(0, 0, 0, 0), pc: 0, prev_op: None, nested_op: None, @@ -194,8 +195,6 @@ static mut SWAP: Swap = Swap { }; pub struct Swap { - /// SID for the swapper - sid: SID, /// PC for blocking handler pc: usize, /// previous op @@ -298,10 +297,10 @@ impl Swap { pub fn register_handler( &mut self, - s0: u32, - s1: u32, - s2: u32, - s3: u32, + _s0: u32, + _s1: u32, + _s2: u32, + _s3: u32, handler: usize, state: usize, ) -> Result { @@ -320,15 +319,11 @@ impl Swap { self.used_pages = total_bytes / PAGE_SIZE; // now register the swapper - if self.sid == SID::from_u32(0, 0, 0, 0) { - self.sid = SID::from_u32(s0, s1, s2, s3); + if self.pc == 0 && self.swapper_state == 0 { self.pc = handler; self.swapper_state = state; #[cfg(feature = "debug-swap")] - println!( - "handler registered: sid {:?} pc {:?} state {:?}", - self.sid, self.pc, self.swapper_state - ); + println!("handler registered: pc {:?} state {:?}", self.pc, self.swapper_state); Ok(xous_kernel::Result::Scalar5(0, 0, 0, 0, 0)) } else { // someone is trying to steal the swapper's privileges! @@ -643,7 +638,7 @@ impl Swap { /// as well as the physical address of the page. /// /// Note that the originating caller to this will update the epoch of the memory - /// alloc counter, so, we don't have to update it explicitly her (as we do with + /// alloc counter, so, we don't have to update it explicitly here (as we do with /// evict_page) /// /// This call diverges into the userspace swapper. diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 9b1c24d7c..3d2320057 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -79,17 +79,21 @@ cramium-fpga = [ "cramium-hal", ] atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"] + +# precursor flags platform-tests = [] -debug-print = [] -earlyprintk = [] renode-bypass = [] secboot = [] -simulation-only = [] +#default = ["debug-print"] + +# swap flag +swap = ["aes-gcm-siv"] + +# cramium target flags board-bringup = [] sram-margin = [] boot-delay = [] -#default = ["debug-print"] -swap = ["aes-gcm-siv"] +simulation-only = [] quantum-timer-test = ["pio", "pio-proc"] spim-test = [] spi-alt-channel = [] @@ -97,5 +101,10 @@ irq-test = [] usb-test = [] trng-test = [] dump-trng = [] +fast-fclk = [] + +# general flags +debug-print = [] +earlyprintk = [] resume = [] # suspend/resume pathway code default = ["board-bringup", "dump-trng"] diff --git a/loader/src/platform/cramium/cramium.rs b/loader/src/platform/cramium/cramium.rs index c9fefc351..3d7f1481b 100644 --- a/loader/src/platform/cramium/cramium.rs +++ b/loader/src/platform/cramium/cramium.rs @@ -1009,7 +1009,13 @@ unsafe fn init_clock_asic(freq_hz: u32) -> u32 { // Hits a 16:8:4:2:1 ratio on fclk:aclk:hclk:iclk:pclk // Resulting in 800:400:200:100:50 MHz assuming 800MHz fclk + #[cfg(feature = "fast-fclk")] daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7fff); // fclk + + // Hits a 8:8:4:2:1 ratio on fclk:aclk:hclk:iclk:pclk + // Resulting in 400:400:200:100:50 MHz assuming 800MHz fclk + #[cfg(not(feature = "fast-fclk"))] + daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7f7f); // fclk daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x3f7f); // aclk daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2.offset()).write_volatile(0x1f3f); // hclk daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_3.offset()).write_volatile(0x0f1f); // iclk diff --git a/services/cram-console/Cargo.toml b/services/cram-console/Cargo.toml index fb3631b68..e34ae8982 100644 --- a/services/cram-console/Cargo.toml +++ b/services/cram-console/Cargo.toml @@ -27,6 +27,7 @@ usb-device-xous = { path = "../usb-device-xous", default-features = false } cramium-hal = { path = "../../libs/cramium-hal", features = ["std"] } cram-hal-service = { path = "../cram-hal-service" } ime-plugin-shell = { path = "../ime-plugin-shell" } +xous-swapper = { path = "../xous-swapper" } [features] "cramium-fpga" = ["utralib/cramium-fpga", "xous-pl230/cramium-fpga"] diff --git a/services/cram-console/src/cmds.rs b/services/cram-console/src/cmds.rs index 5ec223584..109ffa72b 100644 --- a/services/cram-console/src/cmds.rs +++ b/services/cram-console/src/cmds.rs @@ -92,6 +92,8 @@ mod pddb_cmd; use pddb_cmd::*; mod usb; use usb::*; +mod mbox; +use mbox::*; pub struct CmdEnv { common_env: CommonEnv, @@ -101,6 +103,7 @@ pub struct CmdEnv { #[cfg(feature = "pddb")] pddb_cmd: PddbCmd, usb_cmd: Usb, + mbox: Mbox, } impl CmdEnv { pub fn new(xns: &xous_names::XousNames) -> CmdEnv { @@ -130,6 +133,7 @@ impl CmdEnv { log::debug!("usb"); Usb::new() }, + mbox: Mbox::new(), } } @@ -152,6 +156,7 @@ impl CmdEnv { #[cfg(feature = "pddb")] &mut self.pddb_cmd, &mut self.usb_cmd, + &mut self.mbox, ]; if let Some(cmdline) = maybe_cmdline { diff --git a/services/cram-console/src/cmds/mbox.rs b/services/cram-console/src/cmds/mbox.rs new file mode 100644 index 000000000..4b7af46e5 --- /dev/null +++ b/services/cram-console/src/cmds/mbox.rs @@ -0,0 +1,414 @@ +use modals::Modals; +use utra::mailbox; +use utralib::generated::*; +use xous_ipc::String; + +use crate::{CommonEnv, ShellCmdApi}; + +/// This constraint is limited by the size of the memory on the CM7 side +const MAX_PKT_LEN: usize = 128; +const MBOX_PROTOCOL_REV: u32 = 0; +const TX_FIFO_DEPTH: u32 = 128; + +const CLIFFORD_SIZE: usize = 128; + +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub enum MboxError { + None, + NotReady, + TxOverflow, + TxUnderflow, + RxOverflow, + RxUnderflow, + InvalidOpcode, +} + +#[repr(u16)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub enum ToRvOp { + Invalid = 0, + + RetKnock = 128, + RetDct8x8 = 129, + RetClifford = 130, +} +impl TryFrom for ToRvOp { + type Error = MboxError; + + fn try_from(value: u16) -> Result { + match value { + 0 => Ok(ToRvOp::Invalid), + 128 => Ok(ToRvOp::RetKnock), + 129 => Ok(ToRvOp::RetDct8x8), + 130 => Ok(ToRvOp::RetClifford), + _ => Err(MboxError::InvalidOpcode), + } + } +} + +#[repr(u16)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +pub enum ToCm7Op { + Invalid = 0, + + Knock = 1, + Dct8x8 = 2, + Clifford = 3, +} + +pub struct MboxToCm7Pkt<'a> { + version: u32, + opcode: ToCm7Op, + data: &'a [u32], +} + +pub struct MboxToRvPkt { + version: u32, + opcode: ToRvOp, + data: Vec, +} + +pub struct Mbox { + csr: CSR, + phys_mem: xous::MemoryRange, + modals: Modals, +} +impl Mbox { + pub fn new() -> Mbox { + let swapper = xous_swapper::Swapper::new().expect("couldn't get handle to swapper"); + let phys_mem; + let pages_to_alloc = (CLIFFORD_SIZE * CLIFFORD_SIZE + (4096 - 1)) / 4096; + loop { + swapper.garbage_collect_pages(pages_to_alloc); + match xous::syscall::map_memory( + None, + None, + pages_to_alloc * 4096, + xous::MemoryFlags::R + | xous::MemoryFlags::W + | xous::MemoryFlags::RESERVE + | xous::MemoryFlags::DEV, + ) { + Ok(range) => { + phys_mem = Some(range); + break; + } + Err(e) => { + log::info!("Couldn't allocate contiguous memory, retrying: {:?}", e); + xous::yield_slice(); + } + } + } + log::info!("Device RAM allocated at vaddr {:x}", phys_mem.unwrap().as_ptr() as usize); + unsafe { + phys_mem.unwrap().as_slice_mut().fill(0); + } + log::info!("Range is zeroized"); + + let csr_mem = xous::syscall::map_memory( + xous::MemoryAddress::new(mailbox::HW_MAILBOX_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map mailbox CSR range"); + let mut csr = CSR::new(csr_mem.as_mut_ptr() as *mut u32); + csr.wfo(mailbox::LOOPBACK_LOOPBACK, 0); // ensure we're not in loopback mode + // generate available events - not hooked up to IRQ, but we'll poll for now + csr.wfo(mailbox::EV_ENABLE_AVAILABLE, 1); + + let xns = xous_names::XousNames::new().unwrap(); + + Self { csr, phys_mem: phys_mem.unwrap(), modals: Modals::new(&xns).unwrap() } + } + + fn expect_tx(&mut self, val: u32) -> Result<(), MboxError> { + if (TX_FIFO_DEPTH - self.csr.rf(mailbox::STATUS_TX_WORDS)) == 0 { + return Err(MboxError::TxOverflow); + } else { + self.csr.wo(mailbox::WDATA, val); + Ok(()) + } + } + + pub fn try_send(&mut self, to_cm7: MboxToCm7Pkt) -> Result<(), MboxError> { + if to_cm7.data.len() > MAX_PKT_LEN { + Err(MboxError::TxOverflow) + } else { + self.expect_tx(to_cm7.version)?; + self.expect_tx(to_cm7.opcode as u32 | (to_cm7.data.len() as u32) << 16)?; + for &d in to_cm7.data.iter() { + self.expect_tx(d)?; + } + // trigger the send + self.csr.wfo(mailbox::DONE_DONE, 1); + Ok(()) + } + } + + fn expect_rx(&mut self) -> Result { + if self.csr.rf(mailbox::STATUS_RX_WORDS) == 0 { + Err(MboxError::RxUnderflow) + } else { + Ok(self.csr.r(mailbox::RDATA)) + } + } + + pub fn try_rx(&mut self) -> Result { + let version = self.expect_rx()?; + let op_and_len = self.expect_rx()?; + let opcode = ToRvOp::try_from((op_and_len & 0xFFFF) as u16)?; + let len = (op_and_len >> 16) as usize; + let mut data = Vec::new(); + for _ in 0..len { + data.push(self.expect_rx()?); + } + Ok(MboxToRvPkt { version, opcode, data }) + } + + pub fn poll_not_ready(&self) -> bool { self.csr.rf(mailbox::EV_PENDING_AVAILABLE) == 0 } +} + +impl<'a> ShellCmdApi<'a> for Mbox { + cmd_api!(mbox); + + // inserts boilerplate for command API + + fn process( + &mut self, + args: String<1024>, + env: &mut CommonEnv, + ) -> Result>, xous::Error> { + use core::fmt::Write; + let mut ret = String::<1024>::new(); + let helpstring = "mbox [knock]"; + + let mut tokens = args.as_str().unwrap().split(' '); + + if let Some(sub_cmd) = tokens.next() { + match sub_cmd { + "knock" => { + let test_data = [0xC0DE_0000u32, 0x0000_600Du32]; + let mut expected_result = 0; + for &d in test_data.iter() { + expected_result ^= d; + } + let test_pkt = + MboxToCm7Pkt { version: MBOX_PROTOCOL_REV, opcode: ToCm7Op::Knock, data: &test_data }; + log::info!("sending knock..."); + match self.try_send(test_pkt) { + Ok(_) => { + write!(ret, "Packet send Ok\n").ok(); + let start = env.ticktimer.elapsed_ms(); + while self.poll_not_ready() { + xous::yield_slice(); + if env.ticktimer.elapsed_ms() - start > 1000 { + write!(ret, "Response timeout!\n").ok(); + return Ok(Some(ret)); + } + } + // now receive the packet + match self.try_rx() { + Ok(rx_pkt) => { + log::info!("Knock result: {:x}", rx_pkt.data[0]); + if rx_pkt.version != MBOX_PROTOCOL_REV { + write!( + ret, + "Version mismatch {} != {}", + rx_pkt.version, MBOX_PROTOCOL_REV + ) + .ok(); + } + if rx_pkt.opcode != ToRvOp::RetKnock { + write!( + ret, + "Opcode mismatch {} != {}", + rx_pkt.opcode as u16, + ToRvOp::RetKnock as u16 + ) + .ok(); + } + if rx_pkt.data.len() != 1 { + write!( + ret, + "Expected length mismatch {} != {}", + rx_pkt.data.len(), + 1 + ) + .ok(); + } else { + if rx_pkt.data[0] != expected_result { + write!( + ret, + "Expected data mismatch {:x} != {:x}", + rx_pkt.data[0], expected_result + ) + .ok(); + } else { + write!(ret, "Knock test PASS: {:x}", rx_pkt.data[0]).ok(); + } + } + } + Err(e) => { + write!(ret, "Error while deserializing: {:?}\n", e).ok(); + } + } + } + Err(e) => { + write!(ret, "Packet send error: {:?}\n", e).ok(); + } + }; + } + // Time trial results + // - on Precursor (100MHz): 27.60 seconds + // - RV32 Daric (400MHz): 10.11 seconds (2.72x over Precursor) + // - CM7 Daric (400MHz): 3.66 seconds (2.76x over RV32; 7.54x over Precursor) + "clifford" => { + log::info!("prefill"); + let prefill = unsafe { self.phys_mem.as_slice_mut() }; + log::info!("prefill: {:x}({})", prefill.as_ptr() as usize, prefill.len()); + prefill.fill(0); + log::info!("v2p"); + let virt_addr = self.phys_mem.as_ptr(); + let phys_addr = + xous::syscall::virt_to_phys(virt_addr as usize).expect("can't convert v2p"); + log::info!("phys addr: {:x}", phys_addr); + let test_data = [phys_addr as u32]; + let clifford_pkt = MboxToCm7Pkt { + version: MBOX_PROTOCOL_REV, + opcode: ToCm7Op::Clifford, + data: &test_data, + }; + log::info!("initiating attractor"); + let start_time = env.ticktimer.elapsed_ms(); + let mut end_time: Option = None; + match self.try_send(clifford_pkt) { + Ok(_) => { + write!(ret, "Packet send Ok\n").ok(); + #[cfg(feature = "cifford-poll")] + { + let start = env.ticktimer.elapsed_ms(); + while self.poll_not_ready() { + xous::yield_slice(); + if env.ticktimer.elapsed_ms() - start > 10_000 { + write!(ret, "Response timeout!\n").ok(); + return Ok(Some(ret)); + } + } + // now receive the packet + log::info!("computation done, rendering..."); + match self.try_rx() { + Ok(_) => { + // get the computed data and show it + // safety: safe because all u8 types are representable + let buf: &[u8] = &unsafe { self.phys_mem.as_slice() } + [..CLIFFORD_SIZE * CLIFFORD_SIZE]; + let img = gam::bitmap::Img::new( + buf.to_owned(), + CLIFFORD_SIZE, + gam::PixelType::U8, + ); + log::info!("showing image..."); + let modal_size = + gam::Point::new(CLIFFORD_SIZE as _, CLIFFORD_SIZE as _); + let bm = gam::Bitmap::from_img(&img, Some(modal_size)); + self.modals.show_image(bm).expect("couldn't render attractor"); + log::info!("done!"); + } + Err(e) => { + write!(ret, "Error while deserializing: {:?}\n", e).ok(); + } + } + } + #[cfg(not(feature = "clifford-poll"))] + { + const WIDTH: u32 = CLIFFORD_SIZE as _; + const HEIGHT: u32 = CLIFFORD_SIZE as _; + const X_CENTER: f32 = (WIDTH / 2) as f32; + const Y_CENTER: f32 = (HEIGHT / 2) as f32; + const SCALE: f32 = WIDTH as f32 / 5.1; + const STEP: u8 = 16; + const ITERATIONS: u32 = 200000; + let mut buf = vec![255u8; (WIDTH * HEIGHT).try_into().unwrap()]; + let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9); + let (mut x, mut y): (f32, f32) = (0.0, 0.0); + + log::info!("generating image"); + for _ in 0..=ITERATIONS { + if end_time.is_none() && !self.poll_not_ready() { + end_time = Some(env.ticktimer.elapsed_ms()); + } + // this takes a couple minutes to run + let x1 = f32::sin(a * y) + c * f32::cos(a * x); + let y1 = f32::sin(b * x) + d * f32::cos(b * y); + (x, y) = (x1, y1); + let (a, b): (u32, u32) = + ((x * SCALE + X_CENTER) as u32, (y * SCALE + Y_CENTER) as u32); + let i: usize = (a + WIDTH * b).try_into().unwrap(); + if buf[i] >= STEP { + buf[i] -= STEP; + } + } + log::info!( + "Local finished in {:.2} s", + (env.ticktimer.elapsed_ms() - start_time) as f32 / 1000.0 + ); + while end_time.is_none() { + if !self.poll_not_ready() { + end_time = Some(env.ticktimer.elapsed_ms()); + break; + } + } + log::info!( + "Remote finished in {:.2} s", + (end_time.unwrap_or(0) - start_time) as f32 / 1000.0 + ); + let img = gam::Img::new(buf, WIDTH.try_into().unwrap(), gam::PixelType::U8); + log::info!("showing local version"); + let modal_size = gam::Point::new(CLIFFORD_SIZE as _, CLIFFORD_SIZE as _); + let bm = gam::Bitmap::from_img(&img, Some(modal_size)); + self.modals.show_image(bm).expect("couldn't render attractor"); + + env.ticktimer.sleep_ms(1000).ok(); + log::info!("showing remote version"); + // now receive the packet + log::info!("computation done, rendering..."); + match self.try_rx() { + Ok(_) => { + // get the computed data and show it + // safety: safe because all u8 types are representable + let buf: &[u8] = &unsafe { self.phys_mem.as_slice() } + [..CLIFFORD_SIZE * CLIFFORD_SIZE]; + let img = gam::bitmap::Img::new( + buf.to_owned(), + CLIFFORD_SIZE, + gam::PixelType::U8, + ); + log::info!("showing image..."); + let modal_size = + gam::Point::new(CLIFFORD_SIZE as _, CLIFFORD_SIZE as _); + let bm = gam::Bitmap::from_img(&img, Some(modal_size)); + self.modals.show_image(bm).expect("couldn't render attractor"); + log::info!("done!"); + } + Err(e) => { + write!(ret, "Error while deserializing: {:?}\n", e).ok(); + } + } + } + } + Err(e) => { + write!(ret, "Packet send error: {:?}\n", e).ok(); + } + } + } + _ => { + write!(ret, "{}", helpstring).unwrap(); + } + } + } else { + write!(ret, "{}", helpstring).unwrap(); + } + Ok(Some(ret)) + } +} diff --git a/services/shellchat/src/cmds/test.rs b/services/shellchat/src/cmds/test.rs index f91e44033..b68f87cf2 100644 --- a/services/shellchat/src/cmds/test.rs +++ b/services/shellchat/src/cmds/test.rs @@ -997,6 +997,48 @@ impl<'a> ShellCmdApi<'a> for Test { xous::send_message(bench_new_cid, Message::new_blocking_scalar(1, 0, 0, 0, 0)).ok(); unsafe { xous::disconnect(bench_new_cid).ok() }; } + #[cfg(feature = "clifford-bench")] + // used to compare performance against Cramium target + "clifford" => { + use std::convert::TryInto; + const CLIFFORD_SIZE: usize = 128; + + const WIDTH: u32 = CLIFFORD_SIZE as _; + const HEIGHT: u32 = CLIFFORD_SIZE as _; + const X_CENTER: f32 = (WIDTH / 2) as f32; + const Y_CENTER: f32 = (HEIGHT / 2) as f32; + const SCALE: f32 = WIDTH as f32 / 5.1; + const STEP: u8 = 16; + const ITERATIONS: u32 = 200000; + let mut buf = vec![255u8; (WIDTH * HEIGHT).try_into().unwrap()]; + let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9); + let (mut x, mut y): (f32, f32) = (0.0, 0.0); + + log::info!("generating image"); + let start_time = env.ticktimer.elapsed_ms(); + for _ in 0..=ITERATIONS { + // this takes a couple minutes to run + let x1 = f32::sin(a * y) + c * f32::cos(a * x); + let y1 = f32::sin(b * x) + d * f32::cos(b * y); + (x, y) = (x1, y1); + let (a, b): (u32, u32) = + ((x * SCALE + X_CENTER) as u32, (y * SCALE + Y_CENTER) as u32); + let i: usize = (a + WIDTH * b).try_into().unwrap(); + if buf[i] >= STEP { + buf[i] -= STEP; + } + } + log::info!( + "Local finished in {:.2} s", + (env.ticktimer.elapsed_ms() - start_time) as f32 / 1000.0 + ); + let img = gam::Img::new(buf, WIDTH.try_into().unwrap(), gam::PixelType::U8); + log::info!("showing local version"); + let modal_size = gam::Point::new(CLIFFORD_SIZE as _, CLIFFORD_SIZE as _); + let bm = gam::Bitmap::from_img(&img, Some(modal_size)); + let modals = modals::Modals::new(&env.xns).unwrap(); + modals.show_image(bm).expect("couldn't render attractor"); + } _ => { () // do nothing } diff --git a/services/xous-log/src/platform/cramium/implementation.rs b/services/xous-log/src/platform/cramium/implementation.rs index 2704d8b79..0592be4c5 100644 --- a/services/xous-log/src/platform/cramium/implementation.rs +++ b/services/xous-log/src/platform/cramium/implementation.rs @@ -97,7 +97,12 @@ fn uart_handler(_irq_no: usize, _arg: *mut usize) { } } if unsafe { KBD_CONN != 0 } { - print!("{}", char::from_u32(c as u32).unwrap_or('.')); // local echo + let c = char::from_u32(c as u32).unwrap_or('.'); + print!("{}", c); // local echo + // naked carriage return + if c == '\r' { + println!(" "); // line feed with carriage return + } xous::try_send_message(unsafe { KBD_CONN }, xous::Message::new_scalar(0, c as usize, 0, 0, 0)) .ok(); } diff --git a/services/xous-swapper/src/lib.rs b/services/xous-swapper/src/lib.rs new file mode 100644 index 000000000..d51689141 --- /dev/null +++ b/services/xous-swapper/src/lib.rs @@ -0,0 +1,55 @@ +/// public userspace & swapper handler -> swapper userspace ABI +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +#[repr(usize)] +pub enum Opcode { + /// Userspace request to GC some physical pages + GarbageCollect, + /// Test messages + #[cfg(feature = "swap-userspace-testing")] + Test0, + None, +} + +pub const SWAPPER_PUBLIC_NAME: &'static str = "_swapper server_"; + +pub struct Swapper { + conn: xous::CID, +} +impl Swapper { + pub fn new() -> Result { + let xns = xous_api_names::XousNames::new().expect("couldn't connect to xous names"); + REFCOUNT.fetch_add(1, Ordering::Relaxed); + let conn = + xns.request_connection_blocking(SWAPPER_PUBLIC_NAME).expect("Can't connect to TRNG server"); + Ok(Swapper { conn }) + } + + /// Attempts to free `page_count` pages of RAM. + pub fn garbage_collect_pages(&self, page_count: usize) -> usize { + match xous::send_message( + self.conn, + xous::Message::new_blocking_scalar(Opcode::GarbageCollect as usize, page_count, 0, 0, 0), + ) { + Ok(xous::Result::Scalar5(free_pages, _, _, _, _)) => free_pages, + _e => { + log::warn!("Garbage collect call failed with internal error: {:?}", _e); + 0 + } + } + // no result is given, but the call blocks until the GC call has completed in the swapper. + } +} + +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); +impl Drop for Swapper { + fn drop(&mut self) { + // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the + // connection. + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { + unsafe { + xous::disconnect(self.conn).unwrap(); + } + } + } +} diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index dc6d9dd5e..0e6c92f08 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -58,7 +58,8 @@ use debug::*; use loader::swap::{SwapAlloc, SwapSpec, SWAP_CFG_VADDR, SWAP_COUNT_VADDR, SWAP_PT_VADDR, SWAP_RPT_VADDR}; use num_traits::*; use platform::{SwapHal, PAGE_SIZE}; -use xous::{MemoryFlags, MemoryRange, Result, CID, PID, SID}; +use xous::{MemoryFlags, MemoryRange, Result, PID}; +use xous_swapper::Opcode; /// Target of pages to free in case of a Hard OOM. Note that the PAGE_TARGET numbers /// are imprecise, in that there is a chance that one target is active during another @@ -118,16 +119,6 @@ pub enum KernelOp { HardOom = 3, } -/// public userspace & swapper handler -> swapper userspace ABI -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -#[repr(usize)] -pub enum Opcode { - /// Test messages - #[cfg(feature = "swap-userspace-testing")] - Test0, - None, -} - pub struct PtPage { pub entries: [u32; 1024], } @@ -217,20 +208,18 @@ impl SwapperSharedState { } struct SharedStateStorage { pub inner: Option, - pub _conn: CID, } impl SharedStateStorage { - pub fn init(&mut self, sid: SID) { + pub fn init(&mut self) { // Register the swapper with the kernel. Written as a raw syscall, since this is // the only instance of its use (no point in use-once code to wrap it). // This is an "early registration" which allows us to see debug output quickly, // even before we can constitute all of our shared state - let (s0, s1, s2, s3) = sid.to_u32(); xous::rsyscall(xous::SysCall::RegisterSwapper( - s0, - s1, - s2, - s3, + 0, + 0, + 0, + 0, swap_handler as *mut usize as usize, self as *mut SharedStateStorage as usize, )) @@ -653,10 +642,8 @@ fn swap_handler( } fn main() { - let sid = xous::create_server().unwrap(); - let conn = xous::connect(sid).unwrap(); - let mut sss = Box::new(SharedStateStorage { _conn: conn, inner: None }); - sss.init(sid); + let mut sss = Box::new(SharedStateStorage { inner: None }); + sss.init(); // init the log, but this is mostly unused. log_server::init_wait().unwrap(); @@ -705,6 +692,7 @@ fn main() { }); // This thread pings the free memory level and will try to clear memory to avoid OOM + // This claims sss, and is mutually exclusive with other options that claim sss #[cfg(feature = "oom-doom")] std::thread::spawn({ move || { @@ -720,6 +708,9 @@ fn main() { } }); + let xns = xous_api_names::XousNames::new().unwrap(); + let sid = xns.register_name(xous_swapper::SWAPPER_PUBLIC_NAME, None).unwrap(); + let mut msg_opt = None; loop { xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); @@ -727,6 +718,25 @@ fn main() { let op: Option = FromPrimitive::from_usize(msg.body.id()); log::debug!("Swapper got {:x?}", op); match op { + Some(Opcode::GarbageCollect) => { + if let Some(scalar) = msg.body.scalar_message_mut() { + let pages = scalar.arg1; + if pages > HARD_OOM_PAGE_TARGET * 2 { + log::warn!( + "Not honoring excessive GC request, reducing to {} pages", + HARD_OOM_PAGE_TARGET * 2 + ); + } + sss.inner.as_mut().unwrap().pages_to_free = pages.max(HARD_OOM_PAGE_TARGET * 2); + xous::rsyscall(xous::SysCall::SwapOp(SwapAbi::ClearMemoryNow as usize, 0, 0, 0, 0, 0, 0)) + .expect("ClearMemoryNow syscall failed"); + sss.inner.as_mut().unwrap().pages_to_free = HARD_OOM_PAGE_TARGET; + let free_pages = get_free_pages(); + log::info!("Free pages after GC: {}", free_pages); + // return the current free page count + scalar.arg1 = free_pages; + } + } #[cfg(feature = "swap-userspace-testing")] Some(Opcode::Test0) => { log::info!("Free mem: {}kiB", get_free_pages() * PAGE_SIZE / 1024); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index a39882379..1450601ad 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -520,14 +520,15 @@ fn main() -> Result<(), Box> { builder.add_feature("swap"); builder.add_loader_feature("debug-print"); - // builder.add_kernel_feature("debug-swap"); + builder.add_kernel_feature("debug-swap"); // builder.add_kernel_feature("debug-print"); // builder.add_kernel_feature("debug-swap-verbose"); builder.add_feature("quantum-timer"); // builder.add_feature("auto-trng"); // automatically initialize TRNG tester inside USB stack - // builder.add_kernel_feature("v2p"); + builder.add_kernel_feature("v2p"); builder.add_feature("mass-storage"); + builder.add_feature("ditherpunk"); match task.as_deref() { Some("cramium-fpga") => builder.target_cramium_fpga(), Some("cramium-soc") => builder.target_cramium_soc(),