diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index 54fe1d0ee..a8647e92f 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -5,16 +5,15 @@ use cramium_hal::ifram::IframRange; use cramium_hal::iox::*; use cramium_hal::sce; use cramium_hal::udma::*; +use loader::swap::SPIM_RAM_IFRAM_ADDR; use rand_chacha::rand_core::RngCore; use rand_chacha::rand_core::SeedableRng; use rand_chacha::ChaCha8Rng; use crate::bootconfig::BootConfig; use crate::platform::{SPIM_FLASH_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR}; -use crate::println; use crate::swap::*; -use crate::PAGE_SIZE; -use crate::SDBG; +use crate::*; /// hard coded at offset 0 of SPI FLASH for now, until we figure out if and how to move this around. const SWAP_IMG_START: usize = 0; @@ -38,6 +37,7 @@ pub struct SwapHal { dst_cipher: Aes256GcmSiv, buf_addr: usize, buf: RawPage, + ram_swap_key: [u8; 32], } fn setup_port( @@ -329,6 +329,7 @@ impl SwapHal { dst_cipher: Aes256GcmSiv::new((&dest_key).into()), buf_addr: 0, buf, + ram_swap_key: dest_key, }; hal.aad_storage[..ssh.aad_len as usize].copy_from_slice(&ssh.aad[..ssh.aad_len as usize]); hal.aad_len = ssh.aad_len as usize; @@ -339,6 +340,8 @@ impl SwapHal { } } + pub fn get_swap_key(&self) -> &[u8] { &self.ram_swap_key } + fn aad(&self) -> &[u8] { &self.aad_storage[..self.aad_len] } pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { @@ -435,3 +438,77 @@ impl SwapHal { /// but extremely unsafe because we have to track the use of this buffer manually. pub unsafe fn get_decrypt(&self) -> &[u8] { &self.buf.data } } + +/// Function for initializing any PTE mappings needed by the swapper to be functional +/// at boot -- the swapper userspace cannot itself invoke page maps to initialize itself +/// because this would cause a circular dependency. +pub fn userspace_maps(cfg: &mut BootConfig) { + let tt_address = cfg.processes[SWAPPER_PID as usize - 1].satp << 12; + let root = unsafe { &mut *(tt_address as *mut crate::PageTable) }; + + // map the IFRAM structure into userspace + // use map_page_32 because we don't track this in the RPT. + cfg.map_page_32( + root, + SPIM_RAM_IFRAM_ADDR, + SWAP_HAL_VADDR, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); + + let mut iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + iox.set_alternate_function(IoxPort::PD, 2, IoxFunction::AF2); + iox.set_alternate_function(IoxPort::PD, 3, IoxFunction::AF2); + // rx as input, with pull-up + iox.set_gpio_dir(IoxPort::PD, 2, IoxDir::Input); + iox.set_gpio_pullup(IoxPort::PD, 2, IoxEnable::Enable); + // tx as output + iox.set_gpio_dir(IoxPort::PD, 3, IoxDir::Output); + + // Set up the UDMA_UART block to the correct baud rate and enable status + let mut udma_global = udma::GlobalConfig::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE as *mut u32); + udma_global.clock_on(udma::PeriphId::Uart0); + udma_global.map_event( + udma::PeriphId::Uart0, + udma::PeriphEventType::Uart(udma::EventUartOffset::Rx), + udma::EventChannel::Channel2, + ); + udma_global.map_event( + udma::PeriphId::Uart0, + udma::PeriphEventType::Uart(udma::EventUartOffset::Tx), + udma::EventChannel::Channel3, + ); + + let baudrate: u32 = 115200; + let freq: u32 = perclk / 2; + + // the address of the UART buffer is "hard-allocated" at an offset one page from the top of + // IFRAM0. This is a convention that must be respected by the UDMA UART library implementation + // for things to work. + let uart_buf_addr = UART_IFRAM_ADDR; + let mut udma_uart = unsafe { + // safety: this is safe to call, because we set up clock and events prior to calling new. + udma::Uart::get_handle(utra::udma_uart_0::HW_UDMA_UART_0_BASE, uart_buf_addr, uart_buf_addr) + }; + crate::println!("Baud freq is {} Hz, baudrate is {}", freq, baudrate); + udma_uart.set_baud(baudrate, freq); + + // map the debug UART HW page + cfg.map_page_32( + root, + // TODO: use PD2/3 AF2 for this UART; set up the IOs for this + utra::udma_uart_0::HW_UDMA_UART_0_BASE, + SWAP_APP_UART_VADDR, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); + // map the debug UART IFRAM page + cfg.map_page_32( + root, + // TODO: use PD2/3 AF2 for this UART; set up the IOs for this + APP_UART_IFRAM_ADDR, + SWAP_APP_UART_IFRAM_VADDR, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); +} diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index b71d356bd..045ca4837 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -4,10 +4,8 @@ use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; use utralib::generated::*; use crate::bootconfig::BootConfig; -use crate::println; use crate::swap::*; -use crate::PAGE_SIZE; -use crate::SDBG; +use crate::*; pub struct SwapHal { src_data_area: &'static [u8], @@ -22,6 +20,7 @@ pub struct SwapHal { buf: RawPage, mac_offset: u32, mac_len: u32, + ram_swap_key: [u8; 32], } impl SwapHal { @@ -96,6 +95,7 @@ impl SwapHal { buf: RawPage { data: [0u8; 4096] }, mac_offset: ssh.mac_offset, mac_len: mac_size as u32, + ram_swap_key, }; hal.partial_nonce.copy_from_slice(&ssh.parital_nonce); Some(hal) @@ -104,6 +104,8 @@ impl SwapHal { } } + pub fn get_swap_key(&self) -> &[u8] { &self.ram_swap_key } + pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); assert!(offset >= 0x1000); @@ -170,8 +172,8 @@ impl SwapHal { /// Used to examine contents of swap RAM. Swap count is fixed at 0 by this routine. Decrypted data is /// returned as a slice. pub fn decrypt_swap_from(&mut self, src_offset: usize, dst_vaddr: usize, dst_pid: u8) -> &[u8] { - // println!("Decrypt swap:"); - // println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid); + println!("Decrypt swap:"); + println!(" offset: {:x}, vaddr: {:x}, pid: {}", src_offset, dst_vaddr, dst_pid); assert!(src_offset & (PAGE_SIZE - 1) == 0); let mut nonce = [0u8; size_of::()]; nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field @@ -184,9 +186,9 @@ impl SwapHal { let mut tag = [0u8; size_of::()]; let mac_offset = (src_offset / PAGE_SIZE) * size_of::(); tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::()]); - // println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]); + println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]); self.buf.data.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]); - // println!("Nonce: {:x?}, tag: {:x?}", &nonce, &tag); + println!("Nonce: {:x?}, tag: {:x?}", &nonce, &tag); match self.dst_cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), aad, @@ -203,3 +205,36 @@ impl SwapHal { /// but extremely unsafe because we have to track the use of this buffer manually. pub unsafe fn get_decrypt(&self) -> &[u8] { &self.buf.data } } + +/// Function for initializing any PTE mappings needed by the swapper to be functional +/// at boot -- the swapper userspace cannot itself invoke page maps to initialize itself +/// because this would cause a circular dependency. +pub fn userspace_maps(cfg: &mut BootConfig) { + let tt_address = cfg.processes[SWAPPER_PID as usize - 1].satp << 12; + let root = unsafe { &mut *(tt_address as *mut crate::PageTable) }; + + let swap = cfg.swap.unwrap(); + let base = swap.ram_offset as usize; + let bounds = swap.ram_size as usize; + + // map the entire swap RAM space into the swapper + // use map_page_32 because we don't track this region in the RPT + for (i, addr) in (base..base + bounds).step_by(PAGE_SIZE).enumerate() { + cfg.map_page_32( + root, + addr, + SWAP_HAL_VADDR + PAGE_SIZE * i, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ); + } + + // map the debug UART + cfg.map_page_32( + root, + utra::app_uart::HW_APP_UART_BASE, + SWAP_APP_UART_VADDR, + FLG_R | FLG_W | FLG_U | FLG_VALID, + SWAPPER_PID, + ) +}