Merge pull request #524 from betrusted-io/swap-image-loader

WIP: Swap image loader
This commit is contained in:
bunnie 2024-04-09 03:27:57 -07:00 committed by GitHub
commit fe0fe0edef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 2244 additions and 196 deletions

6
Cargo.lock generated
View file

@ -1063,6 +1063,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"rand_core 0.6.4",
"typenum",
]
@ -2788,13 +2789,16 @@ dependencies = [
name = "loader"
version = "0.1.0"
dependencies = [
"aes-gcm-siv",
"armv7",
"atsama5d27",
"com_rs 0.1.0 (git+https://github.com/betrusted-io/com_rs?branch=main)",
"cramium-hal",
"crc",
"curve25519-dalek-loader",
"ed25519-dalek-loader",
"lazy_static",
"rand_chacha 0.3.1",
"sha2-loader",
"utralib 0.1.24",
"xous-pio",
@ -5132,6 +5136,7 @@ dependencies = [
name = "tools"
version = "0.1.0"
dependencies = [
"aes-gcm-siv",
"base64 0.20.0",
"bitflags 1.3.2",
"clap 2.34.0",
@ -5142,6 +5147,7 @@ dependencies = [
"log",
"pem",
"pkcs8 0.8.0",
"rand 0.8.5",
"ring",
"sha2 0.10.8",
"svd2utra 0.1.22",

View file

@ -0,0 +1,149 @@
# Boot Renode Script (boot.resc)
# Add this script's path to the global path, so
# we can include files relative to ourselves.
path add $ORIGIN
using sysbus
# Add peripherals that are defined in C#. You must restart Renode
# if you modify these files.
i @peripherals/ABRTCMC.cs
i @peripherals/BetrustedEcI2C.cs
i @peripherals/BetrustedSocI2C.cs
i @peripherals/BetrustedSpinor.cs
i @peripherals/BetrustedWatchdog.cs
i @peripherals/BQ24157.cs
i @peripherals/BQ27421.cs
i @peripherals/BtEvents.cs
i @peripherals/ComConnector.cs
EnsureTypeIsLoaded "Antmicro.Renode.Peripherals.SPI.Betrusted.IComPeripheral"
EnsureTypeIsLoaded "Antmicro.Renode.Peripherals.SPI.Betrusted.IComController"
i @peripherals/ComEc.cs
i @peripherals/ComSoC.cs
i @peripherals/EcPower.cs
i @peripherals/EcWifi.cs
i @peripherals/engine.cs
i @peripherals/Jtag.cs
i @peripherals/keyboard.cs
i @peripherals/keyrom.cs
i @peripherals/LiteX_Timer_32.cs
i @peripherals/LM3509.cs
i @peripherals/LSM6DS3.cs
i @peripherals/memlcd.cs
i @peripherals/MXIC_MX66UM1G45G.cs
i @peripherals/sha512.cs
i @peripherals/spinor_soft_int.cs
i @peripherals/ticktimer.cs
i @peripherals/trng_kernel.cs
i @peripherals/trng_server.cs
i @peripherals/TLV320AIC3100.cs
i @peripherals/TUSB320LAI.cs
i @peripherals/WF200.cs
i @peripherals/wfi.cs
EnsureTypeIsLoaded "Antmicro.Renode.Peripherals.CPU.VexRiscv"
i @peripherals/vexriscv-aes.cs
EnsureTypeIsLoaded "Antmicro.Renode.Peripherals.Timers.Betrusted.TickTimer"
i @peripherals/susres.cs
# Create the COM SPI bus
emulation CreateComConnector "com"
############### Define the Betrusted SoC ###############
mach create "SoC"
machine LoadPlatformDescription @soc/betrusted-soc.repl
machine StartGdbServer 3333 true
showAnalyzer uart
showAnalyzer console
showAnalyzer app_uart
showAnalyzer memlcd
# sysbus LogPeripheralAccess engine true
# sysbus LogPeripheralAccess sha512 true
# sysbus LogPeripheralAccess i2c true
# sysbus LogPeripheralAccess trng_kernel true
sysbus Tag <0xB0000000, 0xB0006000> "Framebuffer"
# logLevel -1 keyboard
# logLevel -1 ticktimer
# logLevel -1 engine
# logLevel -1 i2c
# logLevel -1 i2c.abracom_rtc
# logLevel -1 sha512
# logLevel -1 sysbus.spinor.flash
# sysbus LogPeripheralAccess spinor true
# Silence GPIO
sysbus SilenceRange <0xf0003000, 0xF0003FFF>
# Silence POWER
sysbus SilenceRange <0xF0014000 100>
# Attach the flash backing to the filesystem
sysbus.spinor.flash BackingFile $ORIGIN/../tools/pddb-images/renode.bin
# Load the SPI flash into RAM
sysbus LoadBinary @../utralib/renode/soc_csr.bin 0x20000000
# Connect the SoC to the SPI bus
connector Connect sysbus.com com
# The macro `reset` gets called implicitly when running `machine Reset`
macro reset
"""
#sysbus LoadBinary @flash-formatted.bin 0x20000000
sysbus LoadBinary @../target/riscv32imac-unknown-xous-elf/release/loader.bin 0x20500000
sysbus LoadBinary @../target/riscv32imac-unknown-xous-elf/release/xous.img 0x20980000
sysbus LoadBinary @../target/riscv32imac-unknown-xous-elf/release/swap.img 0x21200000
# Set $a0 to point at the args binary
cpu SetRegisterUnsafe 10 0x20980000
cpu PC 0x20501000
"""
# Connect the serial port to an Ethernet port
emulation CreateServerSocketTerminal 8888 "kernel" False
connector Connect sysbus.uart kernel
# Connect the `app_uart` port -- which is the port on which
# the Xous gdbserver runs -- to TCP port 3456, to allow for
# application-aware debugging
emulation CreateServerSocketTerminal 3456 "gdb" False
connector Connect sysbus.app_uart gdb
runMacro $reset
mach clear
############### Define the Betrusted EC ###############
mach create "EC"
machine LoadPlatformDescription @ec/betrusted-ec.repl
# logLevel -1 i2c
# logLevel -1 i2c.bq27421
# logLevel -1 wifi
# logLevel 0 wifi.wf200
# sysbus LogPeripheralAccess wifi true
machine StartGdbServer 3334 true
showAnalyzer uart
# Connect the EC to the SPI bus
connector Connect sysbus.com com
macro reset
"""
sysbus LoadBinary @ec/bt-ec.bin 0x20000000
sysbus LoadBinary @ec/wf200_fw.bin 0x2009C000
sysbus LoadBinary @ec/loader.bin 0x00000000
cpu PC 0
cpu SP 0x1001FFFC
"""
runMacro $reset
mach clear
start

10
emulation/xous-swap.resc Normal file
View file

@ -0,0 +1,10 @@
i @emulation/betrusted-swap.resc
i @emulation/linux-server.resc
# Connect EC to the switch
mach set "EC"
connector Connect sysbus.wifi.wf200 switch
mach clear
start

View file

@ -354,7 +354,10 @@ impl SystemServices {
let init_offsets = {
let mut init_count = 1;
for arg in args.iter() {
if arg.name == u32::from_le_bytes(*b"IniE") || arg.name == u32::from_le_bytes(*b"IniF") {
if arg.name == u32::from_le_bytes(*b"IniE")
|| arg.name == u32::from_le_bytes(*b"IniF")
|| arg.name == u32::from_le_bytes(*b"IniS")
{
init_count += 1;
}
}
@ -367,7 +370,10 @@ impl SystemServices {
let mut arg_iter = args.iter().peekable();
loop {
if let Some(arg) = arg_iter.peek() {
if arg.name == u32::from_le_bytes(*b"IniE") || arg.name == u32::from_le_bytes(*b"IniF") {
if arg.name == u32::from_le_bytes(*b"IniE")
|| arg.name == u32::from_le_bytes(*b"IniF")
|| arg.name == u32::from_le_bytes(*b"IniS")
{
break;
}
} else {
@ -400,7 +406,7 @@ impl SystemServices {
} else {
// This code makes the following assumption:
// - Arguments are in the same order as processes
// - All processes take the form of IniE/IniF such that the flags are in the last word
// - All processes take the form of IniE/IniF/IniS such that the flags are in the last word
// - Process #1 is the kernel
// Any changes to this will break this code!
let mut eh_frame = 0;

View file

@ -613,7 +613,7 @@ pub enum SpimCs {
Cs3 = 3,
}
#[repr(u32)]
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum SpimMode {
Standard = 0,
Quad = 1,
@ -670,9 +670,9 @@ pub enum SpimCmd {
/// type of wait, channel, cycle count
Wait(SpimWaitType),
/// mode, words per xfer, bits per word, endianness, number of words to send
TxData(SpimMode, SpimWordsPerXfer, u8, SpimEndian, u16),
TxData(SpimMode, SpimWordsPerXfer, u8, SpimEndian, u32),
/// mode, words per xfer, bits per word, endianness, number of words to receive
RxData(SpimMode, SpimWordsPerXfer, u8, SpimEndian, u16),
RxData(SpimMode, SpimWordsPerXfer, u8, SpimEndian, u32),
/// repeat count
RepeatNextCmd(u16),
EndXfer(SpimEventGen),
@ -688,7 +688,7 @@ impl Into<u32> for SpimCmd {
SpimCmd::Config(pol, pha, div) => 0 << 28 | (pol as u32) << 9 | (pha as u32) << 8 | div as u32,
SpimCmd::StartXfer(cs) => 1 << 28 | cs as u32,
SpimCmd::SendCmd(mode, size, cmd) => {
2 << 28 | (mode as u32) << 27 | (size as u32 & 0x1F) << 16 | cmd as u32
2 << 28 | (mode as u32) << 27 | ((size - 1) as u32 & 0x1F) << 16 | cmd as u32
}
SpimCmd::SendAddr(mode, size) => 3 << 28 | (mode as u32) << 27 | (size as u32 & 0x1F) << 16,
SpimCmd::Dummy(cycles) => 4 << 28 | (cycles as u32 & 0x1F) << 16,
@ -752,6 +752,7 @@ pub struct Spim {
tx_buf_len_bytes: usize,
// immediately after the tx buf len
rx_buf_len_bytes: usize,
dummy_cycles: u8,
}
// length of the command buffer
@ -788,7 +789,11 @@ impl Spim {
event_channel: Option<EventChannel>,
max_tx_len_bytes: usize,
max_rx_len_bytes: usize,
dummy_cycles: Option<u8>,
) -> Option<Self> {
// this is a hardware limit - the DMA pointer is only this long!
assert!(max_tx_len_bytes < 65536);
assert!(max_rx_len_bytes < 65536);
// now setup the channel
let base_addr = match channel {
SpimChannel::Channel0 => utra::udma_spim_0::HW_UDMA_SPIM_0_BASE,
@ -827,6 +832,7 @@ impl Spim {
ifram,
tx_buf_len_bytes: max_tx_len_bytes,
rx_buf_len_bytes: max_rx_len_bytes,
dummy_cycles: dummy_cycles.unwrap_or(0),
};
// setup the interface using a UDMA command
spim.send_cmd_list(&[SpimCmd::Config(pol, pha, clk_div as u8)]);
@ -837,6 +843,84 @@ impl Spim {
}
}
/// This function is `unsafe` because it can only be called after the
/// global shared UDMA state has been set up to un-gate clocks and set up
/// events.
///
/// It is also `unsafe` on Drop because you have to remember to unmap
/// the clock manually as well once the object is dropped...
///
/// Return: the function can return None if it can't allocate enough memory
/// for the requested tx/rx length.
pub unsafe fn new_with_ifram(
channel: SpimChannel,
spi_clk_freq: u32,
sys_clk_freq: u32,
pol: SpimClkPol,
pha: SpimClkPha,
chip_select: SpimCs,
// cycles to wait between CS assert and data start
sot_wait: u8,
// cycles to wait after data stop and CS de-assert
eot_wait: u8,
event_channel: Option<EventChannel>,
max_tx_len_bytes: usize,
max_rx_len_bytes: usize,
dummy_cycles: Option<u8>,
ifram: IframRange,
) -> Self {
// this is a hardware limit - the DMA pointer is only this long!
assert!(max_tx_len_bytes < 65536);
assert!(max_rx_len_bytes < 65536);
// now setup the channel
let base_addr = match channel {
SpimChannel::Channel0 => utra::udma_spim_0::HW_UDMA_SPIM_0_BASE,
SpimChannel::Channel1 => utra::udma_spim_1::HW_UDMA_SPIM_1_BASE,
SpimChannel::Channel2 => utra::udma_spim_2::HW_UDMA_SPIM_2_BASE,
SpimChannel::Channel3 => utra::udma_spim_3::HW_UDMA_SPIM_3_BASE,
};
#[cfg(target_os = "xous")]
let csr_range = xous::syscall::map_memory(
xous::MemoryAddress::new(base_addr),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map serial port");
#[cfg(target_os = "xous")]
let csr = CSR::new(csr_range.as_mut_ptr() as *mut u32);
#[cfg(not(target_os = "xous"))]
let csr = CSR::new(base_addr as *mut u32);
let clk_div = sys_clk_freq / spi_clk_freq;
// make this a hard panic -- you'll find out at runtime that you f'd up
// but at least you find out.
assert!(clk_div < 256, "SPI clock divider is out of range");
let mut reqlen = max_tx_len_bytes + max_rx_len_bytes + SPIM_CMD_BUF_LEN_BYTES;
if reqlen % 4096 != 0 {
// round up to the nearest page size
reqlen = (reqlen + 4096) & !4095;
}
let mut spim = Spim {
csr,
cs: chip_select,
sot_wait,
eot_wait,
event_channel,
align: SpimByteAlign::Disable,
mode: SpimMode::Standard,
ifram,
tx_buf_len_bytes: max_tx_len_bytes,
rx_buf_len_bytes: max_rx_len_bytes,
dummy_cycles: dummy_cycles.unwrap_or(0),
};
// setup the interface using a UDMA command
spim.send_cmd_list(&[SpimCmd::Config(pol, pha, clk_div as u8)]);
spim
}
/// The command buf is *always* a `u32`; so tie the type down here.
fn cmd_buf_mut(&mut self) -> &mut [u32] {
&mut self.ifram.as_slice_mut()[(self.tx_buf_len_bytes + self.rx_buf_len_bytes) / size_of::<u32>()
@ -950,19 +1034,15 @@ impl Spim {
}
}
while words_sent < total_words {
// determine the valid length of data we could send -- has to fit into a u16::MAX
let tx_len = if (total_words - words_sent) >= u16::MAX as usize {
u16::MAX as usize
} else {
total_words - words_sent
};
// determine the valid length of data we could send
let tx_len = (total_words - words_sent).min(self.tx_buf_len_bytes);
// setup the command list for data to send
let cmd_list = [SpimCmd::TxData(
self.mode,
SpimWordsPerXfer::Words1,
bits_per_xfer as u8,
SpimEndian::LsbFirst,
tx_len as u16,
tx_len as u32,
)];
self.send_cmd_list(&cmd_list);
let cfg_size = match size_of::<T>() {
@ -1018,4 +1098,198 @@ impl Spim {
pub fn rx_data<T: UdmaWidths + Copy>(&mut self, _rx_data: &mut [T], _cs: Option<SpimCs>) {
todo!("Not yet done...let's see if tx_data works first before templating");
}
/// Activate is the logical sense, not the physical sense. To be clear: `true` causes CS to go low.
fn mem_cs(&mut self, activate: bool) {
if activate {
if self.sot_wait == 0 {
self.send_cmd_list(&[SpimCmd::StartXfer(self.cs)])
} else {
self.send_cmd_list(&[
SpimCmd::StartXfer(self.cs),
SpimCmd::Wait(SpimWaitType::Cycles(self.sot_wait)),
])
}
} else {
let evt =
if self.event_channel.is_some() { SpimEventGen::Enabled } else { SpimEventGen::Disabled };
if self.eot_wait == 0 {
self.send_cmd_list(&[SpimCmd::EndXfer(evt)])
} else {
self.send_cmd_list(&[
SpimCmd::Wait(SpimWaitType::Cycles(self.eot_wait)),
SpimCmd::EndXfer(evt),
])
}
}
}
fn mem_send_cmd(&mut self, cmd: u8) {
let cmd_list = [SpimCmd::SendCmd(self.mode, 8, (cmd as u16) << 8)];
self.send_cmd_list(&cmd_list);
while self.udma_busy(Bank::Tx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
}
/// Side-effects: unsets QPI mode if it was previously set
pub fn mem_read_id(&mut self) -> u32 {
if self.mode != SpimMode::Standard {
self.mem_qpi_mode(false);
}
self.mem_cs(true);
// send the RDID command
self.mem_send_cmd(0x9F);
// read back the ID result
let cmd_list =
[SpimCmd::RxData(SpimMode::Standard, SpimWordsPerXfer::Words1, 8, SpimEndian::MsbFirst, 3)];
while self.udma_busy(Bank::Tx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
let ret = u32::from_le_bytes([0x0, self.rx_buf()[0], self.rx_buf()[1], self.rx_buf()[2]]);
self.mem_cs(false);
ret
}
pub fn mem_qpi_mode(&mut self, activate: bool) {
self.mem_cs(true);
if activate {
self.mem_send_cmd(0x35);
self.mode = SpimMode::Quad;
} else {
self.mem_send_cmd(0xF5);
self.mode = SpimMode::Standard;
}
self.mem_cs(false);
}
/// Side-effects: unsets QPI mode if it was previously set
pub fn mem_write_status_register(&mut self, status: u8, config: u8) {
if self.mode != SpimMode::Standard {
self.mem_qpi_mode(false);
}
self.mem_cs(true);
self.mem_send_cmd(0x1);
// setup the command list for data to send
let cmd_list =
[SpimCmd::TxData(self.mode, SpimWordsPerXfer::Words1, 8 as u8, SpimEndian::MsbFirst, 2 as u32)];
self.send_cmd_list(&cmd_list);
self.tx_buf_mut()[..2].copy_from_slice(&[status, config]);
// safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference
unsafe { self.udma_enqueue(Bank::Tx, &self.tx_buf_phys::<u8>()[..2], CFG_EN | CFG_SIZE_8) }
while self.udma_busy(Bank::Tx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
self.mem_cs(false);
}
pub fn mem_read(&mut self, addr: u32, buf: &mut [u8]) {
// divide into buffer-sized chunks + repeat cycle on each buffer increment
// this is because the size of the buffer is meant to represent the limit of the
// target device's memory page (i.e., the point at which you'd wrap when reading)
let mut offset = 0;
for chunk in buf.chunks_mut(self.rx_buf_len_bytes) {
self.mem_cs(true);
let chunk_addr = addr as usize + offset;
let addr_plus_dummy = (24 / 8) + self.dummy_cycles / 2;
let cmd_list = [
SpimCmd::SendCmd(self.mode, 8, 0xEB << 8),
SpimCmd::TxData(
self.mode,
SpimWordsPerXfer::Words1,
8 as u8,
SpimEndian::MsbFirst,
addr_plus_dummy as u32,
),
];
self.send_cmd_list(&cmd_list);
let a = chunk_addr.to_be_bytes();
self.tx_buf_mut()[..3].copy_from_slice(&[a[2], a[1], a[0]]);
// the remaining bytes are just junk and ignored
// safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference
unsafe {
self.udma_enqueue(
Bank::Tx,
&self.tx_buf_phys::<u8>()[..addr_plus_dummy as usize],
CFG_EN | CFG_SIZE_8,
)
}
while self.udma_busy(Bank::Tx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
let rd_cmd = [SpimCmd::RxData(
self.mode,
SpimWordsPerXfer::Words1,
8,
SpimEndian::MsbFirst,
chunk.len() as u32,
)];
self.send_cmd_list(&cmd_list);
// safety: this is safe because rx_buf_phys() slice is only used as a base/bounds reference
unsafe {
self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::<u8>()[..chunk.len()], CFG_EN | CFG_SIZE_8)
};
while self.udma_busy(Bank::Rx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
chunk.copy_from_slice(&self.rx_buf()[..chunk.len()]);
offset += chunk.len();
self.mem_cs(false);
}
}
/// This should only be called on SPI RAM -- not valid for FLASH devices, they need a programming routine!
pub fn mem_ram_write(&mut self, addr: u32, buf: &[u8]) {
// divide into buffer-sized chunks + repeat cycle on each buffer increment
// this is because the size of the buffer is meant to represent the limit of the
// target device's memory page (i.e., the point at which you'd wrap when reading)
let mut offset = 0;
for chunk in buf.chunks(self.tx_buf_len_bytes) {
self.mem_cs(true);
let chunk_addr = addr as usize + offset;
let cmd_list = [
SpimCmd::SendCmd(self.mode, 8, 0x38 << 8),
SpimCmd::TxData(self.mode, SpimWordsPerXfer::Words1, 8 as u8, SpimEndian::MsbFirst, 3),
];
self.send_cmd_list(&cmd_list);
let a = chunk_addr.to_be_bytes();
self.tx_buf_mut()[..3].copy_from_slice(&[a[2], a[1], a[0]]);
// safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference
unsafe { self.udma_enqueue(Bank::Tx, &self.tx_buf_phys::<u8>()[..3], CFG_EN | CFG_SIZE_8) }
while self.udma_busy(Bank::Tx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
let wr_cmd = [SpimCmd::TxData(
self.mode,
SpimWordsPerXfer::Words1,
8,
SpimEndian::MsbFirst,
chunk.len() as u32,
)];
self.send_cmd_list(&cmd_list);
self.tx_buf_mut()[..chunk.len()].copy_from_slice(chunk);
// safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference
unsafe {
self.udma_enqueue(Bank::Tx, &self.tx_buf_phys::<u8>()[..chunk.len()], CFG_EN | CFG_SIZE_8)
};
while self.udma_busy(Bank::Tx) {
#[cfg(feature = "std")]
xous::yield_slice();
}
offset += chunk.len();
self.mem_cs(false);
}
}
}

View file

@ -29,6 +29,11 @@ xous-pio = { path = "../libs/xous-pio", optional = true, features = [
"cramium-soc",
"baremetal",
] }
aes-gcm-siv = { version = "0.11.1", default-features = false, features = [
"aes",
], optional = true }
crc = { version = "1.8.1", default-features = false }
rand_chacha = { version = "0.3.1", optional = true, default_features = false }
[dependencies.com_rs]
git = "https://github.com/betrusted-io/com_rs"
@ -50,7 +55,6 @@ renode = [
"utralib/renode",
"ed25519-dalek-loader/renode",
"curve25519-dalek-loader/renode",
"resume",
"secboot",
"sha2-loader",
]
@ -61,6 +65,7 @@ cramium-soc = [
"cramium-hal",
"xous-pio",
"xous-pl230",
"rand_chacha",
]
cramium-fpga = [
"utralib/cramium-fpga",
@ -77,5 +82,6 @@ secboot = []
simulation-only = []
board-bringup = []
#default = ["debug-print"]
resume = [] # suspend/resume pathway code
swap = ["aes-gcm-siv"]
resume = [] # suspend/resume pathway code
default = []

View file

@ -3,6 +3,8 @@ use core::mem;
#[cfg(not(feature = "atsama5d27"))]
use core::num::NonZeroUsize;
#[cfg(feature = "swap")]
use crate::swap::SwapDescriptor;
use crate::*;
/// In-memory copy of the configuration page. Stage 1 sets up the gross structure,
@ -51,6 +53,30 @@ pub struct BootConfig {
/// The number of 'Init' tags discovered
pub init_process_count: usize,
/// Amount that init_size is offset by swap. We have to track this
/// separately because init_size is used during allocations to track
/// cfg_top(), but then re-used during page mapping with the assumption
/// that it also points to exclusive kernel memory. swap_offset allows
/// us to subtract out the memory we allocated and gave to swap in that
/// phase of boot. When swap is not enabled, it is set to 0.
pub swap_offset: usize,
/// Swap HAL
#[cfg(feature = "swap")]
pub swap_hal: Option<SwapHal>,
/// Swap descriptor
#[cfg(feature = "swap")]
pub swap: Option<&'static SwapDescriptor>,
/// Offset of the current free page in swap
#[cfg(feature = "swap")]
pub swap_free_page: usize,
/// root swap page table of the process
#[cfg(feature = "swap")]
pub swap_root: &'static mut [usize],
}
impl Default for BootConfig {
@ -68,6 +94,15 @@ impl Default for BootConfig {
runtime_page_tracker: Default::default(),
init_process_count: 0,
processes: Default::default(),
swap_offset: 0,
#[cfg(feature = "swap")]
swap_hal: None,
#[cfg(feature = "swap")]
swap: None,
#[cfg(feature = "swap")]
swap_free_page: 0,
#[cfg(feature = "swap")]
swap_root: Default::default(),
}
}
}
@ -155,6 +190,56 @@ impl BootConfig {
}
}
#[cfg(feature = "swap")]
pub fn map_swap(&mut self, swap_phys: usize, virt: usize, owner: XousPid) {
if SDBG {
println!(" swap pa {:x} -> va {:x}", swap_phys, virt);
}
let ppn1 = (swap_phys >> 22) & ((1 << 12) - 1);
let ppn0 = (swap_phys >> 12) & ((1 << 10) - 1);
let vpn1 = (virt >> 22) & ((1 << 10) - 1);
let vpn0 = (virt >> 12) & ((1 << 10) - 1);
assert!(owner != 0);
let l1_pt = unsafe {
core::slice::from_raw_parts_mut(
self.swap_root[owner as usize - 1] as *mut usize,
mem::size_of::<PageTable>() / mem::size_of::<usize>(),
)
};
// Allocate a new level 1 pagetable entry if one doesn't exist.
if l1_pt[vpn1] & FLG_VALID == 0 {
let na = self.alloc() as usize;
if SDBG {
println!(
"Swap Level 1 page table is invalid ({:08x}) @ {:08x} -- allocating a new one @ {:08x}",
unsafe { l1_pt.as_ptr().add(vpn1) } as usize,
l1_pt[vpn1],
na
);
}
// Mark this entry as a leaf node (WRX as 0), and indicate
// it is a valid page by setting "V".
l1_pt[vpn1] = ((na >> 12) << 10) | FLG_VALID;
}
let l0_pt_idx = unsafe { &mut (*(((l1_pt[vpn1] << 2) & !((1 << 12) - 1)) as *mut PageTable)) };
let l0_pt = &mut l0_pt_idx.entries;
// Ensure the entry hasn't already been mapped to a different address.
if l0_pt[vpn0] & 1 != 0 && (l0_pt[vpn0] & 0xffff_fc00) != ((ppn1 << 20) | (ppn0 << 10)) {
panic!(
"Swap page {:08x} was already allocated to {:08x}, so cannot map to {:08x}!",
swap_phys,
(l0_pt[vpn0] >> 10) << 12,
virt
);
}
let previous_flags = l0_pt[vpn0] & 0xf;
l0_pt[vpn0] = (ppn1 << 20) | (ppn0 << 10) | previous_flags | FLG_VALID;
}
pub fn map_page_32(
&mut self,
root: &mut PageTable,

View file

@ -14,4 +14,10 @@ pub const KERNEL_STACK_TOP: usize = 0xfff8_0000;
pub const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000;
pub const KERNEL_STACK_PAGE_COUNT: usize = 1;
pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000;
#[cfg(not(feature = "swap"))]
pub const GUARD_MEMORY_BYTES: usize = 3 * crate::PAGE_SIZE;
#[cfg(feature = "swap")]
pub const GUARD_MEMORY_BYTES: usize = 4 * crate::PAGE_SIZE; // this is bare minimum. Increase more if we want to do resume!
#[cfg(feature = "swap")]
pub const SWAPPER_PID: u8 = 2;

View file

@ -91,11 +91,11 @@ pub fn print_pagetable(root: usize) {
}
let _superpage_addr = i as u32 * (1 << 22);
println!(
" {:4} Superpage for {:08x} @ {:08x} (flags: {:02x})",
" {:4} Superpage for {:08x} @ {:08x} (flags: {:03x})",
i,
_superpage_addr,
(*l1_entry >> 10) << 12,
l1_entry & 0xff
l1_entry & 0x3ff
);
// let l0_pt_addr = ((l1_entry >> 10) << 12) as *const u32;
let l0_pt = unsafe { &mut (*(((*l1_entry >> 10) << 12) as *mut PageTable)) };
@ -105,11 +105,11 @@ pub fn print_pagetable(root: usize) {
}
let _page_addr = j as u32 * (1 << 12);
println!(
" {:4} {:08x} -> {:08x} (flags: {:02x})",
" {:4} {:08x} -> {:08x} (flags: {:03x})",
j,
_superpage_addr + _page_addr,
(*l0_entry >> 10) << 12,
l0_entry & 0xff
l0_entry & 0x3ff
);
}
}

View file

@ -22,6 +22,8 @@ mod murmur3;
mod phase1;
mod phase2;
mod platform;
#[cfg(feature = "swap")]
mod swap;
use core::{mem, ptr, slice};
@ -31,6 +33,8 @@ use consts::*;
use minielf::*;
use phase1::{phase_1, InitialProcess};
use phase2::{phase_2, ProgramDescription};
#[cfg(feature = "swap")]
use platform::SwapHal;
pub type XousPid = u8;
pub const PAGE_SIZE: usize = 4096;
@ -40,6 +44,7 @@ const STACK_PAGE_COUNT: usize = 8;
const VDBG: bool = false; // verbose debug
const VVDBG: bool = false; // very verbose debug
const SDBG: bool = true; // swap debug
#[cfg(test)]
mod test;
@ -59,6 +64,16 @@ pub struct PageTable {
entries: [usize; PAGE_SIZE / WORD_SIZE],
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum IniType {
// RAM
IniE,
// XIP
IniF,
// Swap
IniS,
}
/// Entrypoint
/// This makes the program self-sufficient by setting up memory page assignment
/// and copying the arguments to RAM.
@ -110,8 +125,21 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
platform::platform_tests();
let mut cfg = BootConfig { base_addr: args.base as *const usize, args, ..Default::default() };
#[cfg(feature = "swap")]
println!("Size of BootConfig: {:x}", core::mem::size_of::<BootConfig>());
read_initial_config(&mut cfg);
#[cfg(feature = "swap")]
{
cfg.swap_hal = SwapHal::new(&cfg);
read_swap_config(&mut cfg);
#[cfg(feature = "resume")]
compile_error!(
"WARNING WARNING WARNING: swap and resume selected - this is not a valid configuration because\
the stack overlaps into the 'clean suspend' marker with swap. This needs to be fixed if resume is desired with swap."
);
}
// check to see if we are recovering from a clean suspend or not
#[cfg(feature = "resume")]
let (clean, was_forced_suspend, susres_pid) = check_resume(&mut cfg);
@ -125,8 +153,8 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
clear_ram(&mut cfg);
phase_1(&mut cfg);
phase_2(&mut cfg, &fs_prehash);
#[cfg(feature = "debug-print")]
if VDBG {
#[cfg(any(feature = "debug-print", feature = "swap"))]
if VDBG || SDBG {
check_load(&mut cfg);
}
println!("done initializing for cold boot.");
@ -139,8 +167,8 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
clear_ram(&mut cfg);
phase_1(&mut cfg);
phase_2(&mut cfg, &fs_prehash);
#[cfg(feature = "debug-print")]
if VDBG {
#[cfg(any(feature = "debug-print", feature = "swap"))]
if VDBG || SDBG {
check_load(&mut cfg);
}
println!("done initializing for cold boot.");
@ -188,7 +216,26 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) -
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 krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size + cfg.swap_offset;
#[cfg(feature = "swap")]
if SDBG && VDBG {
// activate to debug stack smashes. RPT should be 0's here (or at least valid PIDs) if stack did
// not overflow.
for (i, r) in
cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 1024..].chunks(32).enumerate()
{
println!(" rpt {:08x}: {:02x?}", cfg.runtime_page_tracker.len() - 1024 + i * 32, r);
}
}
// Add a static check for stack overflow, using a heuristic that the last 64 bytes of the RPT
// ought to be a valid PID. A stack smash is likely to write something that does not obey
// this heuristic within that range (any stack-stored pointer, for example, will break this).
for &check in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 64..].iter() {
assert!(
check <= cfg.processes.len() as u8,
"RPT looks corrupted, suspect stack overflow in loader. Increase GUARD_MEMORY_BYTES!"
);
}
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 =
@ -317,6 +364,10 @@ pub fn read_initial_config(cfg: &mut BootConfig) {
}
cfg.sram_start = xarg.data[2] as *mut usize;
cfg.sram_size = xarg.data[3] as usize;
#[cfg(feature = "swap")]
if SDBG {
println!("XAarg // sram_start: {:x}, sram_size: {:x}", cfg.sram_start as usize, cfg.sram_size);
}
let mut kernel_seen = false;
let mut init_seen = false;
@ -349,12 +400,39 @@ pub fn read_initial_config(cfg: &mut BootConfig) {
init_seen = true;
cfg.init_process_count += 1;
}
#[cfg(feature = "swap")]
if tag.name == u32::from_le_bytes(*b"Swap") {
// safety: the image creator guarantees this data is aligned and initialized properly
cfg.swap = Some(unsafe { &*(tag.data.as_ptr() as *const crate::swap::SwapDescriptor) });
}
}
assert!(kernel_seen, "no kernel definition");
assert!(init_seen, "no initial programs found");
}
#[cfg(feature = "swap")]
pub fn read_swap_config(cfg: &mut BootConfig) {
// Read in the swap arguments: should be located at beginning of image in swap.
let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_src_page_at(0x1000);
let swap_args = KernelArguments::new(page0.as_ptr() as *const usize);
for tag in swap_args.iter() {
if tag.name == u32::from_le_bytes(*b"IniS") {
assert!(tag.size >= 4, "invalid Init size");
cfg.init_process_count += 1;
} else if tag.name == u32::from_le_bytes(*b"XArg") {
// these are actually specified inside the `Swap` arg, this is just
// a mirror because this argument is added by default by the image creator
if SDBG {
println!("Swap start: {:x}", tag.data[2]);
println!("Swap size: {:x}", tag.data[3]);
}
} else {
println!("Unhandled argument in swap: {:x}", tag.name);
}
}
}
/// Checks a reserved area of RAM for a pattern with a pre-defined mathematical
/// relationship. The purpose is to detect if we have a "clean suspend", or if
/// we've rebooted from a corrupt/cold RAM state.
@ -421,8 +499,12 @@ fn clear_ram(cfg: &mut BootConfig) {
// stay there forever, if not explicitly cleared. This clear adds a couple seconds
// to a cold boot, but it's probably worth it. Note that it doesn't happen on a suspend/resume.
let ram: *mut u32 = cfg.sram_start as *mut u32;
let clear_limit = ((4096 + core::mem::size_of::<BootConfig>()) + 4095) & !4095;
if VDBG {
println!("Stack clearing limit: {:x}", clear_limit);
}
unsafe {
for addr in 0..(cfg.sram_size - 8192) / 4 {
for addr in 0..(cfg.sram_size - clear_limit) / 4 {
// 8k is reserved for our own stack
ram.add(addr).write_volatile(0);
}
@ -446,7 +528,7 @@ where
/// This function allows us to check the final loader results
/// It will print to the console the first 32 bytes of each loaded
/// region top/bottom, based upon extractions from the page table.
#[cfg(feature = "debug-print")]
#[cfg(any(feature = "debug-print", feature = "swap"))]
fn check_load(cfg: &mut BootConfig) {
let args = cfg.args;
@ -460,12 +542,17 @@ fn check_load(cfg: &mut BootConfig) {
if tag.name == u32::from_le_bytes(*b"IniE") {
let inie = MiniElf::new(&tag);
println!("\n\nChecking IniE region");
inie.check(cfg, inie.load_offset as usize, pid, false);
inie.check(cfg, inie.load_offset as usize, pid, IniType::IniE);
pid += 1;
} else if tag.name == u32::from_le_bytes(*b"IniF") {
let inif = MiniElf::new(&tag);
println!("\n\nChecking IniF region");
inif.check(cfg, inif.load_offset as usize, pid, true);
inif.check(cfg, inif.load_offset as usize, pid, IniType::IniF);
pid += 1;
} else if tag.name == u32::from_le_bytes(*b"IniS") {
let inis = MiniElf::new(&tag);
println!("\n\nChecking IniS region");
inis.check(cfg, inis.load_offset as usize, pid, IniType::IniS);
pid += 1;
}
}

View file

@ -22,6 +22,7 @@ pub const FLG_U: usize = 0x10;
pub const FLG_A: usize = 0x40;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_D: usize = 0x80;
pub const FLG_P: usize = 0x200;
pub const MINIELF_FLG_W: u8 = 1;
#[allow(dead_code)]
@ -93,7 +94,7 @@ impl MiniElf {
load_offset: usize,
pid: XousPid,
params: &[u8],
xip: bool,
ini_type: IniType,
) -> usize {
println!("Mapping PID {} starting at offset {:08x}", pid, load_offset);
let mut allocated_bytes = 0;
@ -101,19 +102,25 @@ impl MiniElf {
let mut current_page_addr: usize = 0;
let mut previous_addr: usize = 0;
let mut last_mapped_xip = 0;
#[cfg(feature = "swap")]
let mut last_swap_page = 0;
let image_phys_base = allocator.base_addr as usize + self.load_offset as usize;
// It is a requirement that the image generator lay out the artifacts on disk such that
// the page offsets line up for XIP sections. This assert confirms this necessary pre-condition.
if xip {
assert!(
(image_phys_base & (PAGE_SIZE - 1)) == self.sections[0].virt as usize & (PAGE_SIZE - 1),
"Image generator did not align load offsets to page offsets!"
);
match ini_type {
IniType::IniF => {
assert!(
(image_phys_base & (PAGE_SIZE - 1)) == self.sections[0].virt as usize & (PAGE_SIZE - 1),
"Image generator did not align load offsets to page offsets!"
);
}
_ => {
println!(
"flash_map_offset: {:x} / base_addr {:x} load_offset {:x}",
image_phys_base as usize, allocator.base_addr as usize, self.load_offset as usize
);
}
}
println!(
"flash_map_offset: {:x} / base_addr {:x} load_offset {:x}",
image_phys_base as usize, allocator.base_addr as usize, self.load_offset as usize
);
// The load offset is the end of this process. Shift it down by one page
// so we get the start of the first page.
@ -231,7 +238,7 @@ impl MiniElf {
panic!("init section addresses are not strictly increasing");
}
if copy_to_ram || !xip {
if (copy_to_ram || ini_type == IniType::IniE) && (ini_type != IniType::IniS) {
let mut this_page = section.virt as usize & !(PAGE_SIZE - 1);
let mut bytes_to_copy = section.len();
@ -349,10 +356,24 @@ impl MiniElf {
println!("section is 0x{:x} bytes long; mapping {} pages", section.len(), pages_to_map);
}
// --- map FLASH pages to virtual memory ---
// --- map FLASH or swap pages to virtual memory ---
while pages_to_map > 0 {
let map_phys_addr = (image_phys_base + section_map_phys_offset) & !(PAGE_SIZE - 1);
allocator.map_page(tt, map_phys_addr, virt_page, flag_defaults, pid as XousPid);
let map_phys_addr = if ini_type != IniType::IniS {
(image_phys_base + section_map_phys_offset) & !(PAGE_SIZE - 1)
} else {
0 // IniS physical addresses are non-existent; make that abundantly clear
};
let flags = if ini_type == IniType::IniS {
flag_defaults & !FLG_VALID | FLG_P
} else {
flag_defaults
};
allocator.map_page(tt, map_phys_addr, virt_page, flags, pid as XousPid);
#[cfg(feature = "swap")]
if ini_type == IniType::IniS {
allocator.map_swap(last_swap_page * 0x1000, virt_page, pid);
last_swap_page += 1;
}
last_mapped_xip = virt_page;
section_map_phys_offset += PAGE_SIZE;
@ -384,18 +405,28 @@ impl MiniElf {
}
/// Page through a processes allocated pages and check against the file spec.
#[cfg(feature = "debug-print")]
pub fn check(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid, xip: bool) {
println!(
"Checking {} PID {} starting at offset {:08x}",
if xip { "xip" } else { "ram" },
pid,
load_offset
);
let image_phys_base = allocator.base_addr as usize + self.load_offset as usize;
#[cfg(any(feature = "debug-print", feature = "swap"))]
pub fn check(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid, ini_type: IniType) {
println!("Checking {:?} PID {} starting at offset {:08x}", ini_type, pid, load_offset);
let image_phys_base = match ini_type {
IniType::IniE | IniType::IniF => allocator.base_addr as usize + self.load_offset as usize,
IniType::IniS => load_offset,
};
// the process offset is always 1 less than the PID, because that's how we built the table.
#[cfg(not(feature = "atsama5d27"))]
let tt = { allocator.processes[pid as usize - 1].satp };
let tt = match ini_type {
IniType::IniE | IniType::IniF => allocator.processes[pid as usize - 1].satp,
IniType::IniS => {
#[cfg(feature = "swap")]
{
allocator.swap_root[pid as usize - 1] >> 12
}
#[cfg(not(feature = "swap"))]
{
0
}
}
};
#[cfg(feature = "atsama5d27")]
let tt = { allocator.processes[pid as usize - 1].ttbr0 };
@ -415,18 +446,63 @@ impl MiniElf {
let dump_pa_src = section_offset + image_phys_base;
let dump_pa_dst = dest_offset;
let dump_pa_end_dst = pt_walk(tt, section.virt as usize + section.len() - 20);
dump_addr(dump_pa_src, " Src [:20] ");
dump_addr(dump_pa_dst, " Dst [:20] ");
dump_addr(dump_pa_src + section.len() - 20, " Src [-20:] ");
// recompute the end section mapping, because PA/VA mappings don't have to be linear (in fact
// they go in the opposite direction)
if let Some(pa_dst_end) = dump_pa_end_dst {
dump_addr(pa_dst_end, " Dst [-20:] ");
} else {
println!(
" End of destination VA 0x{:x}, ERR UNMAPPED!",
section.virt as usize + section.len() - 20
);
match ini_type {
IniType::IniE | IniType::IniF => {
dump_addr(dump_pa_src, " Src [:20] ");
dump_addr(dump_pa_dst, " Dst [:20] ");
dump_addr(dump_pa_src + section.len() - 20, " Src [-20:] ");
// recompute the end section mapping, because PA/VA mappings don't have to be linear
// (in fact they go in the opposite direction)
if let Some(pa_dst_end) = dump_pa_end_dst {
dump_addr(pa_dst_end, " Dst [-20:] ");
} else {
println!(
" End of destination VA 0x{:x}, ERR UNMAPPED!",
section.virt as usize + section.len() - 20
);
}
}
IniType::IniS => {
#[cfg(feature = "swap")]
if let Some(swap) = allocator.swap_hal.as_mut() {
let dump_disk = swap.decrypt_src_page_at(dump_pa_src & !(PAGE_SIZE - 1));
dump_slice(&dump_disk[dump_pa_src & (PAGE_SIZE - 1)..], " Src [:20] ");
let dump_swap = swap.decrypt_swap_from(
dest_offset & !(PAGE_SIZE - 1),
(section.virt as usize) & !(PAGE_SIZE - 1),
pid,
);
dump_slice(
&dump_swap[(section.virt as usize) & (PAGE_SIZE - 1)..],
" Dst [:20] ",
);
let dump_disk = swap
.decrypt_src_page_at((dump_pa_src + section.len() - 20) & !(PAGE_SIZE - 1));
dump_slice(
&dump_disk[(dump_pa_src + section.len() - 20) & (PAGE_SIZE - 1)..],
" Src [-20:] ",
);
// recompute the end section mapping, because PA/VA mappings don't have to be
// linear (in fact they go in the opposite direction)
if let Some(pa_dst_end) = dump_pa_end_dst {
let dump_swap = swap.decrypt_swap_from(
pa_dst_end & !(PAGE_SIZE - 1),
(section.virt as usize + section.len() - 20) & !(PAGE_SIZE - 1),
pid,
);
dump_slice(
&dump_swap
[(section.virt as usize + section.len() - 20) & (PAGE_SIZE - 1)..],
" Dst [-20:] ",
);
} else {
println!(
" End of destination VA 0x{:x}, ERR UNMAPPED!",
section.virt as usize + section.len() - 20
);
}
}
}
}
} else {
println!(
@ -441,7 +517,7 @@ impl MiniElf {
}
}
#[cfg(feature = "debug-print")]
#[cfg(any(feature = "debug-print", feature = "swap"))]
fn dump_addr(addr: usize, label: &str) {
print!("{}", label);
let slice = unsafe { core::slice::from_raw_parts(addr as *const u8, 20) };
@ -450,8 +526,18 @@ fn dump_addr(addr: usize, label: &str) {
}
print!("\n\r");
}
#[cfg(feature = "swap")]
fn dump_slice(slice: &[u8], label: &str) {
print!("{}", label);
// handle case that our decrypt region isn't 20 bytes long...
let len = slice.len().min(20);
for &b in &slice[..len] {
print!("{:02x}", b);
}
print!("\n\r");
}
#[cfg(all(feature = "debug-print", not(feature = "atsama5d27")))]
#[cfg(all(any(feature = "debug-print", feature = "swap"), not(feature = "atsama5d27")))]
pub fn pt_walk(root: usize, va: usize) -> Option<usize> {
let l1_pt = unsafe { &mut (*((root << 12) as *mut PageTable)) };
let l1_entry = l1_pt.entries[(va & 0xFFC0_0000) >> 22];

View file

@ -39,6 +39,7 @@ pub fn phase_1(cfg: &mut BootConfig) {
//
// As of Xous 0.8, the top page is bootloader stack, and the page below that is the 'clean suspend' page.
cfg.init_size += GUARD_MEMORY_BYTES;
println!("Loader runtime stack should not exceed: {:x}", cfg.get_top() as usize);
// The first region is defined as being "main RAM", which will be used
// to keep track of allocations.
@ -58,7 +59,18 @@ pub fn phase_1(cfg: &mut BootConfig) {
}
// All further allocations must be page-aligned.
#[cfg(feature = "swap")]
if SDBG {
println!(" -> cfg end pre-alignment: {:x}(-{:x})", cfg.get_top() as usize, cfg.init_size);
}
cfg.init_size = (cfg.init_size + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
#[cfg(feature = "swap")]
if SDBG {
println!(" -> cfg end post-alignment: {:x}(-{:x})", cfg.get_top() as usize, cfg.init_size);
}
#[cfg(feature = "swap")]
allocate_swap(cfg);
// Additionally, from this point on all allocations come from
// their respective processes rather than kernel memory.
@ -68,6 +80,15 @@ pub fn phase_1(cfg: &mut BootConfig) {
println!("Copying processes");
copy_processes(cfg);
}
// activate this to debug stack-smashing during copy_process(). The RPT is the first structure that gets
// smashed if the stack overflows! It should be all 0's if the stack did not overrun.
#[cfg(feature = "swap")]
if SDBG && VDBG {
for (i, r) in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 1024..].chunks(32).enumerate()
{
println!(" rpt {:08x}: {:02x?}", cfg.runtime_page_tracker.len() - 1024 + i * 32, r);
}
}
// Mark all pages as in-use by the kernel.
// NOTE: This causes the .text section to be owned by the kernel! This
@ -102,7 +123,7 @@ pub fn allocate_regions(cfg: &mut BootConfig) {
rpt_pages += region_length_rounded / PAGE_SIZE;
}
// Round the tracker to a multiple of the page size, so as to keep memory
// Round the tracker to a multiple of the pointer size, so as to keep memory
// operations fast.
rpt_pages = (rpt_pages + mem::size_of::<usize>() - 1) & !(mem::size_of::<usize>() - 1);
@ -117,10 +138,19 @@ pub fn allocate_regions(cfg: &mut BootConfig) {
cfg.runtime_page_tracker =
unsafe { slice::from_raw_parts_mut(runtime_page_tracker as *mut XousPid, rpt_pages) };
#[cfg(feature = "swap")]
if SDBG {
println!(
" -> RPT range: {:x} - {:x}",
runtime_page_tracker as usize,
runtime_page_tracker as usize + rpt_pages * core::mem::size_of::<XousPid>()
);
}
}
pub fn allocate_processes(cfg: &mut BootConfig) {
let process_count = cfg.init_process_count + 1;
println!("Allocating tables for {} processes", process_count);
let table_size = process_count * mem::size_of::<InitialProcess>();
// Allocate the process table
cfg.init_size += table_size;
@ -130,8 +160,171 @@ pub fn allocate_processes(cfg: &mut BootConfig) {
}
cfg.processes =
unsafe { slice::from_raw_parts_mut(processes as *mut InitialProcess, process_count as usize) };
#[cfg(feature = "swap")]
{
if SDBG {
println!(
" -> Processes range: {:x} - {:x}",
processes as usize,
processes as usize + cfg.processes.len() * mem::size_of::<InitialProcess>()
);
}
let swap_root_pt_size = process_count * mem::size_of::<usize>();
cfg.init_size += swap_root_pt_size;
let swap_root_pt = cfg.get_top();
unsafe {
bzero(swap_root_pt, swap_root_pt.add(process_count));
}
cfg.swap_root = unsafe { slice::from_raw_parts_mut(swap_root_pt, process_count as usize) };
if SDBG {
println!(
" -> Swap root pt range: {:x} - {:x}",
swap_root_pt as usize,
swap_root_pt as usize + cfg.swap_root.len() * mem::size_of::<usize>()
);
}
}
}
#[cfg(feature = "swap")]
pub fn allocate_swap(cfg: &mut BootConfig) {
let process_count = cfg.init_process_count + 1;
let swap_pt_size = process_count * mem::size_of::<PageTable>();
cfg.init_size += swap_pt_size;
cfg.swap_offset += swap_pt_size;
let swap_pt_base = cfg.get_top();
unsafe { bzero(swap_pt_base, swap_pt_base.add(swap_pt_size / mem::size_of::<usize>())) }
// The page table proper is "unbound": we don't put it in a slice, we simply put references
// to each page in cfg.swap_root[]. I wonder if this is UB?
for (index, root) in cfg.swap_root.iter_mut().enumerate() {
*root = swap_pt_base as usize + index * mem::size_of::<PageTable>();
}
if SDBG {
println!(
" -> Swap pt data range: {:x} - {:x}",
swap_pt_base as usize,
swap_pt_base as usize + cfg.swap_root.len() * mem::size_of::<PageTable>()
);
}
}
#[cfg(feature = "swap")]
pub fn copy_args(cfg: &mut BootConfig) {
// With swap enabled, copy_args also merges the IniS arguments from the swap region into the kernel
// arguments, and patches the length field accordingly.
//
// So much terrible, no-good awful code. This is basically not Rust code, because the whole thing
// is operating on pointers conjured out of thin air and confused senses of what is a byte-size
// versus a word-size. Deserves a re-write, but it would also require reworking a bunch of other
// bootloader code because the core issue is the way the kernel arguments are defined for parsing and
// iteration.
//
// Suspect many bugs in this code.
// Read in the swap arguments: should be located at beginning of the first page of swap.
// Safety: only safe because we know that the decrypt was setup by read_swap_config(), and no pages
// were decrypted between then and now!
let page0 = unsafe { cfg.swap_hal.as_mut().unwrap().get_decrypt() };
let swap_args = KernelArguments::new(page0.as_ptr() as *const usize);
let mut j = swap_args.iter();
// skip the first argument
let swap_xarg = j.next().expect("couldn't read initial swap tag");
// Merge the args list to target RAM
// Reserve space for the primary arg list + swap args - swap's XArg structure (7 words long)
let final_len = cfg.args.size() / core::mem::size_of::<u32>() + swap_xarg.data[0] as usize - 7;
cfg.init_size += final_len * 4;
let runtime_arg_buffer = cfg.get_top();
// places the boot image kernel arguments
unsafe {
#[allow(clippy::cast_ptr_alignment)]
memcpy(runtime_arg_buffer, cfg.args.base as *const usize, cfg.args.size() as usize)
};
// safety: this should be aligned, allowing this conversion. Note that we do violate a safety condition
// in that we haven't fully initialized the region (the swap arg extension area is uninit), but I think
// it's OK because we will write only to that region (but I suppose in practice, Rust could assume the
// untouched data is 0 or something and try an optimization based on that).
let merged_arg_slice =
unsafe { core::slice::from_raw_parts_mut(runtime_arg_buffer as *mut usize, final_len) };
let mut arg_index = cfg.args.size() / core::mem::size_of::<u32>();
// append the swap arguments, and patch the size field accordingly
for a in j {
// turn the argument into a raw slice
// this is safe because:
// - arguments are always guaranteed to be aligned to a word boundary by the image creator
// - arguments are fully initialized with no UB fields under this transformation
// +2 is for the tag field
if a.name == u32::from_le_bytes(*b"IniS") {
let arg_slice = unsafe {
core::slice::from_raw_parts(
a.data.as_ptr().sub(2) as *const usize, // backup by 2 to accommodate the tag field
a.size as usize / core::mem::size_of::<usize>() + 2,
)
};
if SDBG {
println!(
"Extending args with: 0x{:x}, size: {}. index: {}, len: {}, data: {:x?}",
a.name,
a.size,
arg_index,
arg_slice.len(),
arg_slice
);
}
merged_arg_slice[arg_index..arg_index + arg_slice.len()].copy_from_slice(arg_slice);
arg_index += arg_slice.len();
} else {
println!("Unhandled arg type: {:x}", a.name);
}
}
// redirect the arg buffer to point at the newly copied arguments
cfg.args = KernelArguments::new(runtime_arg_buffer);
// extract the new XArg field, pointing into RAM
let args = cfg.args;
let mut i = args.iter();
let xarg = i.next().expect("couldn't read initial tag");
// patch the total length of the arguments - just jam the value into the data field of the XArg by
// dead-reckoning to the offset
assert!(merged_arg_slice[2] == xarg.data[0] as usize); // sanity checks the dead-reckoning
merged_arg_slice[2] = arg_index;
use crc::{crc16, Hasher16};
// compute the new CRC
let mut digest = crc16::Digest::new(crc16::X25);
// safe because we know the entire region can map into a u8 slice with no UB
let xarg_data = unsafe {
core::slice::from_raw_parts(
xarg.data.as_ptr() as *const u8,
xarg.data.len() * core::mem::size_of::<u32>(),
)
};
digest.write(&xarg_data);
// patch the CRC
let merged_arg_slice_u8 = unsafe {
core::slice::from_raw_parts_mut(
runtime_arg_buffer as *mut u8,
final_len * core::mem::size_of::<u32>(),
)
};
merged_arg_slice_u8[4..6].copy_from_slice(&digest.sum16().to_le_bytes());
if SDBG {
println!(
" -> Patched kernel args range: {:x} - {:x}",
runtime_arg_buffer as usize,
runtime_arg_buffer as usize + final_len * mem::size_of::<usize>()
);
}
}
#[cfg(feature = "swap")]
fn remaining_in_page(addr: usize) -> usize { PAGE_SIZE - (addr & (PAGE_SIZE - 1)) }
#[cfg(not(feature = "swap"))]
pub fn copy_args(cfg: &mut BootConfig) {
// Copy the args list to target RAM
cfg.init_size += cfg.args.size();
@ -147,6 +340,7 @@ pub fn copy_args(cfg: &mut BootConfig) {
enum TagType {
IniE,
IniF,
IniS,
XKrn,
Other,
}
@ -156,6 +350,8 @@ impl From<u32> for TagType {
TagType::IniE
} else if code == u32::from_le_bytes(*b"IniF") {
TagType::IniF
} else if code == u32::from_le_bytes(*b"IniS") {
TagType::IniS
} else if code == u32::from_le_bytes(*b"XKrn") {
TagType::XKrn
} else {
@ -169,6 +365,7 @@ impl TagType {
match self {
TagType::IniE => "IniE",
TagType::IniF => "IniF",
TagType::IniS => "IniS",
TagType::XKrn => "XKrn",
TagType::Other => "Other",
}
@ -314,6 +511,175 @@ fn copy_processes(cfg: &mut BootConfig) {
}
println!("Done with sections");
}
TagType::IniS => {
// if swap is not enabled, don't pull this code in, to keep the bootloader light-weight
#[cfg(feature = "swap")]
{
// IniS does not necessarily exist in linear memory space, so it requires special
// handling. Instead of copying the IniS data into RAM, it's copied
// into encrypted swap (e.g. the RAM area (again, not necessarily in
// linear space) reserved for swap processes).
/*
Example of an IniS section:
1 IniS: entrypoint @ 00021e68, loaded from 00001114. Sections:
Physical offset in swap source image Destination range in virtual memory
src_swap_img_addr dst_page_vaddr
| |
v v
Loaded from 00001114 - Section .gcc_except_table 4056 bytes loading into 00010114..000110ec flags: NONE
Loaded from 000020ec - Section .rodata 19080 bytes loading into 000110f0..00015b78 flags: NONE
Loaded from 00006b74 - Section .eh_frame_hdr 2172 bytes loading into 00015b78..000163f4 flags: EH_HEADER
Loaded from 000073f0 - Section .eh_frame 7740 bytes loading into 000163f4..00018230 flags: EH_FRAME
Loaded from 0000922c - Section .text 67428 bytes loading into 00019230..00029994 flags: EXECUTE
Loaded from 00019990 - Section .data 4 bytes loading into 0002a994..0002a998 flags: WRITE
Loaded from 00019994 - Section .sdata 32 bytes loading into 0002a998..0002a9b8 flags: WRITE
Loaded from 000199b4 - Section .sbss 64 bytes loading into 0002a9b8..0002a9f8 flags: WRITE | NOCOPY
Loaded from 000199f4 - Section .bss 532 bytes loading into 0002a9f8..0002ac0c flags: WRITE | NOCOPY
Note that we have full control over what swap block we put things into, but the swap block's
address offsets should have a 1:1 correlation to the *virtual* destination addresess. We track
the current swap page with `working_page_swap_offset`.
*/
_pid += 1;
let mut working_page_swap_offset: Option<usize> = None;
let mut working_buf = [0u8; 4096];
let mut working_buf_dirty = false;
let inis = MiniElf::new(&tag);
let mut src_swap_img_addr = inis.load_offset as usize;
println!("\n\n{} {} has {} sections", tag_type.to_str(), _pid, inis.sections.len());
println!("Swap free page at swap addr: {:x}", cfg.swap_free_page,);
let mut last_copy_vaddr = 0;
for section in inis.sections.iter() {
let mut dst_page_vaddr = section.virt as usize;
let mut bytes_to_copy = section.len();
if let Some(swap_offset) = working_page_swap_offset {
if (last_copy_vaddr & !(PAGE_SIZE - 1)) != (dst_page_vaddr & !(PAGE_SIZE - 1)) {
if SDBG {
println!(
"New section not aligned: last_copy_vaddr {:x}, dst_page_vaddr {:x}",
last_copy_vaddr, dst_page_vaddr
);
}
// handle case that the new section destination address is outside of the
// current page
cfg.swap_hal.as_mut().expect("swap HAL uninit").encrypt_swap_to(
&mut working_buf,
swap_offset * 0x1000,
last_copy_vaddr & !(PAGE_SIZE - 1),
_pid,
);
working_buf.fill(0);
working_page_swap_offset = Some(cfg.swap_free_page);
working_buf_dirty = false;
cfg.swap_free_page += 1;
}
} else {
// very first time through the loop. working_buf is guaranteed to be zero.
working_page_swap_offset = Some(cfg.swap_free_page);
cfg.swap_free_page += 1;
}
// Decrypt the source image data and re-encrypt it to swap for the section at hand.
// - dst_page_vaddr is the virtual address of the section. We only care about this
// for tracking offsets in pages, at this stage.
// - working_page_swap_offset is the current destination swap RAM page
// - src_swap_img_addr is the offset of the section in source swap FLASH.
// - no_copy sections need to set the corresponding bytes in swap RAM to zero.
//
//
while bytes_to_copy > 0 {
// here are the cases we have to handle:
// - the available decrypted data is larger than the target region to encrypt
// - the available decrypted data is smaller than the target region to encrypt
// - the available decrypted data is equal to the target region to encrypt
let src_swap_img_page = src_swap_img_addr & !(PAGE_SIZE - 1);
let src_swap_img_offset = src_swap_img_addr & (PAGE_SIZE - 1);
// it's almost free to check, so we check at every loop start
if cfg.swap_hal.as_ref().expect("swap HAL uninit").decrypt_page_addr()
!= src_swap_img_page
{
cfg.swap_hal
.as_mut()
.expect("swap HAL uninit")
.decrypt_src_page_at(src_swap_img_page);
}
let decrypt_avail = remaining_in_page(src_swap_img_addr);
let dst_page_avail = remaining_in_page(dst_page_vaddr);
let dst_page_offset = dst_page_vaddr & (PAGE_SIZE - 1);
let copyable = if decrypt_avail >= dst_page_avail {
dst_page_avail.min(bytes_to_copy)
} else {
decrypt_avail.min(bytes_to_copy)
};
if !section.no_copy() {
working_buf[dst_page_offset..dst_page_offset + copyable].copy_from_slice(
&cfg.swap_hal.as_ref().expect("swap HAL uninit").buf_as_ref()
[src_swap_img_offset..src_swap_img_offset + copyable],
);
working_buf_dirty = true;
} else {
// do nothing, because working_buff is filled with 0 on alloc
// but, mark the buffer as dirty, because, it still needs to be committed
working_buf_dirty = true;
}
bytes_to_copy -= copyable;
dst_page_vaddr += copyable;
src_swap_img_addr += copyable;
// if we filled up the destination, grab another page.
if (dst_page_vaddr & (PAGE_SIZE - 1)) == 0 {
// the current vaddr is pointing to a new, empty vaddr; we want to write
// out the previous, full page. Compute that here.
let full_page_vaddr = dst_page_vaddr - PAGE_SIZE;
// we copied exactly dst_page_avail, causing us to wrap around to 0
// write the existing page to swap, and allocate a new swap page
cfg.swap_hal.as_mut().expect("swap HAL uninit").encrypt_swap_to(
&mut working_buf,
working_page_swap_offset.unwrap() * 0x1000,
full_page_vaddr & !(PAGE_SIZE - 1),
_pid,
);
working_buf.fill(0);
working_page_swap_offset = Some(cfg.swap_free_page);
working_buf_dirty = false;
cfg.swap_free_page += 1;
}
}
// set the vpage based on our current vpage. This allows us to allocate
// a new vpage on the next iteration in case there is surprise padding in
// the section load address.
last_copy_vaddr = dst_page_vaddr;
if SDBG {
println!("Looping to the next section (swap)");
println!(
" swap_free_page: {:x}, dst_page_vaddr: {:x}, src_swap_img_addr: {:x}",
cfg.swap_free_page, dst_page_vaddr, src_swap_img_addr,
);
println!(" last_copy_vaddr: {:x}", last_copy_vaddr);
}
}
// flush the encryption buffer
if working_buf_dirty {
cfg.swap_hal.as_mut().expect("swap HAL uninit").encrypt_swap_to(
&mut working_buf,
working_page_swap_offset.unwrap() * 0x1000,
last_copy_vaddr & !(PAGE_SIZE - 1),
_pid,
);
} else {
// we didn't use the current page, de-allocate it
cfg.swap_free_page -= 1;
}
println!("Done with sections");
}
}
TagType::XKrn => {
let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };

View file

@ -1,3 +1,5 @@
#[cfg(feature = "swap")]
use crate::swap::SWAP_PT_VADDR;
use crate::*;
/// Phase 2 bootloader
@ -76,24 +78,35 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
let mut kernel_exception_sp = 0;
#[cfg(feature = "atsama5d27")]
let mut kernel_irq_sp = 0;
for tag in args.iter() {
if tag.name == u32::from_le_bytes(*b"IniE") {
let inie = MiniElf::new(&tag);
println!("\n\nCopying IniE program into memory");
let allocated = inie.load(cfg, process_offset, pid, &env, false);
let allocated = inie.load(cfg, process_offset, pid, &env, IniType::IniE);
println!("IniE Allocated {:x}", allocated);
process_offset -= allocated;
pid += 1;
} else if tag.name == u32::from_le_bytes(*b"IniF") {
let inif = MiniElf::new(&tag);
println!("\n\nMapping IniF program into memory");
let allocated = inif.load(cfg, process_offset, pid, &env, true);
let allocated = inif.load(cfg, process_offset, pid, &env, IniType::IniF);
println!("IniF Allocated {:x}", allocated);
process_offset -= allocated;
pid += 1;
} else if tag.name == u32::from_le_bytes(*b"IniS") {
#[cfg(feature = "swap")]
{
let inis = MiniElf::new(&tag);
println!("\n\nMapping IniS program into memory");
let allocated = inis.load(cfg, process_offset, pid, &env, IniType::IniS);
println!("IniS Allocated {:x}", allocated);
process_offset -= allocated;
pid += 1;
}
} else if tag.name == u32::from_le_bytes(*b"XKrn") {
println!("\n\nCopying kernel into memory");
let xkrn = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
println!("\n\nCopying kernel into memory");
let load_size_rounded = ((xkrn.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1))
+ (((xkrn.data_size + xkrn.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1));
#[cfg(not(feature = "atsama5d27"))]
@ -117,7 +130,8 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
println!("Done loading.");
let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size;
assert!(cfg.init_size & 0xFFF == 0); // check that we didn't mess this assumption up somewhere in the process
let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size + cfg.swap_offset;
#[cfg(feature = "atsama5d27")]
let krn_l1_pt_addr = cfg.processes[0].ttbr0;
#[cfg(not(feature = "atsama5d27"))]
@ -132,7 +146,9 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
// Map boot-generated kernel structures into the kernel
let satp = unsafe { &mut *(krn_l1_pt_addr as *mut PageTable) };
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE as usize) {
let kernel_arg_extents = cfg.init_size - (GUARD_MEMORY_BYTES + cfg.swap_offset);
assert!(kernel_arg_extents <= 0xA000, "Kernel init structures exceeded allocated region");
for addr in (0..kernel_arg_extents).step_by(PAGE_SIZE as usize) {
cfg.map_page(
satp,
addr + krn_struct_start,
@ -141,6 +157,15 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
1 as XousPid,
);
}
#[cfg(feature = "swap")]
if SDBG && VDBG {
// dumps the page with kernel struct data, so we can correlate offsets to data.
for i in (0..4096).step_by(32) {
println!("{:08x}: {:02x?}", krn_struct_start + i, unsafe {
core::slice::from_raw_parts((krn_struct_start + i) as *const u8, 32)
});
}
}
// Copy the kernel's "MMU Page 1023" into every process.
// This ensures a context switch into the kernel can
@ -170,6 +195,54 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) {
krn_struct_start,
);
}
#[cfg(feature = "swap")]
{
// map the swap page table into PID space 2
let tt_address = cfg.processes[SWAPPER_PID as usize].satp << 12;
let root = unsafe { &mut *(tt_address as *mut PageTable) };
let mut swap_pt_vaddr_offset = 0;
// map page table roots
for p in 0..cfg.processes.len() {
// loop is "decomposed" because iterating over processes causes a borrow conflic
let swap_root = cfg.swap_root[p];
println!(
"Mapping root swap PT to PID 2 @paddr {:x} -> vaddr {:x}",
swap_root,
SWAP_PT_VADDR + swap_pt_vaddr_offset
);
cfg.map_page(
root,
swap_root,
SWAP_PT_VADDR + swap_pt_vaddr_offset,
FLG_R | FLG_W | FLG_VALID,
SWAPPER_PID,
);
swap_pt_vaddr_offset += PAGE_SIZE;
}
// now chase down any entries in the roots, and map valid pages
for p in 0..cfg.processes.len() {
let root_pt = unsafe { &*(cfg.swap_root[p] as *const PageTable) };
for &entry in root_pt.entries.iter() {
if entry & FLG_VALID != 0 {
let paddr = (entry & !0x3FF) << 2;
println!(
"Mapping L2 swap PT to PID 2 @paddr {:x} -> vaddr {:x}",
paddr,
SWAP_PT_VADDR + swap_pt_vaddr_offset
);
cfg.map_page(
root,
paddr,
SWAP_PT_VADDR + swap_pt_vaddr_offset,
FLG_R | FLG_W | FLG_VALID,
SWAPPER_PID,
);
swap_pt_vaddr_offset += PAGE_SIZE;
}
}
}
// TODO: patch kernel arguments with Swap values...?
}
if VVDBG {
println!("PID1 pagetables:");
@ -264,6 +337,9 @@ impl ProgramDescription {
// Turn the satp address into a pointer
let satp = unsafe { &mut *(satp_address as *mut PageTable) };
if SDBG {
println!("Kernel root PT address: {:x}", satp_address);
}
allocator.map_page(
satp,
satp_address,

View file

@ -326,7 +326,7 @@ pub fn map_structs_to_kernel(cfg: &mut BootConfig, table_addr: usize, krn_struct
dprintln!("Making the kernel arguments visible");
cfg.map_page(translation_table, 0x20100000, 0x20100000, FLG_R | FLG_X | FLG_VALID, 1 as XousPid);
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE) {
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES + cfg.swap_offset).step_by(PAGE_SIZE) {
cfg.map_page(
tt,
addr + krn_struct_start,
@ -400,7 +400,7 @@ pub fn map_kernel_to_processes(
}
println!("Mapping kernel structures to PID{}", process.asid);
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE) {
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES + cfg.swap_offset).step_by(PAGE_SIZE) {
cfg.map_page(
translation_table,
addr + krn_struct_start,

View file

@ -1,2 +1,6 @@
pub mod cramium;
pub use cramium::*;
#[cfg(feature = "swap")]
pub mod swap;
#[cfg(feature = "swap")]
pub use swap::*;

View file

@ -1,11 +1,18 @@
use cramium_hal::iox::{Iox, IoxDir, IoxEnable, IoxFunction, IoxPort};
use cramium_hal::sce;
use cramium_hal::udma;
use utralib::generated::*;
pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_MEM_LEN;
pub const RAM_BASE: usize = utralib::generated::HW_SRAM_MEM;
// Locate the hard-wired IFRAM allocations for UDMA
pub const UART_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4096;
// RAM needs two buffers of 1k + 16 bytes = 2048 + 16 = 2064 bytes; round up to one page
pub const SPIM_RAM_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 2 * 4096;
// Flash will be released after the loader is done: it's only accessed to copy the IniS sectors into swap,
// then abandoned. It needs 4096 bytes for Rx, and 0 bytes for Tx + 16 bytes for cmd.
pub const SPIM_FLASH_IFRAM_ADDR: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4 * 4096;
// location of kernel, as offset from the base of ReRAM. This needs to match up with what is in link.x.
pub const KERNEL_OFFSET: usize = 0x28000;
@ -121,7 +128,7 @@ pub fn early_init() {
// 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 = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4096;
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)

View file

@ -0,0 +1,393 @@
use core::mem::size_of;
use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag};
use cramium_hal::ifram::IframRange;
use cramium_hal::iox::*;
use cramium_hal::sce;
use cramium_hal::udma::*;
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;
/// 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;
pub struct SwapHal {
image_start: usize,
image_mac_start: usize,
partial_nonce: [u8; 8],
// overflow AAD with panic if it's longer than this!
aad_storage: [u8; 64],
aad_len: usize,
src_cipher: Aes256GcmSiv,
flash_spim: Spim,
ram_spim: Spim,
iox: Iox,
udma_global: GlobalConfig,
swap_start: usize,
swap_len: usize,
swap_mac_start: usize,
swap_mac_len: usize,
dst_cipher: Aes256GcmSiv,
buf_addr: usize,
buf: RawPage,
}
fn setup_port(
iox: &mut Iox,
port: IoxPort,
pin: u8,
function: Option<IoxFunction>,
direction: Option<IoxDir>,
drive: Option<IoxDriveStrength>,
slow_slew: Option<IoxEnable>,
schmitt: Option<IoxEnable>,
pullup: Option<IoxEnable>,
) {
if let Some(f) = function {
iox.set_alternate_function(port, pin, f);
}
if let Some(d) = direction {
iox.set_gpio_dir(port, pin, d);
}
if let Some(t) = schmitt {
iox.set_gpio_schmitt_trigger(port, pin, t);
}
if let Some(p) = pullup {
iox.set_gpio_pullup(port, pin, p);
}
if let Some(s) = slow_slew {
iox.set_slow_slew_rate(port, pin, s);
}
if let Some(s) = drive {
iox.set_drive_strength(port, pin, s);
}
}
impl SwapHal {
pub fn new(cfg: &BootConfig) -> Option<SwapHal> {
if let Some(swap) = cfg.swap {
// sanity check this structure
assert_eq!(core::mem::size_of::<SwapSourceHeader>(), 4096);
// setup the I/O pins
let mut iox = Iox::new(utralib::generated::HW_IOX_BASE as *mut u32);
// JQSPI1
// SPIM_CLK_A[0]
setup_port(
&mut iox,
IoxPort::PD,
4,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Disable),
None,
None,
);
// SPIM_SD[0-3]_A[0]
for i in 0..3 {
setup_port(
&mut iox,
IoxPort::PD,
i,
Some(IoxFunction::AF1),
None,
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
}
// SPIM_CSN0_A[0]
setup_port(
&mut iox,
IoxPort::PD,
5,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
// SPIM_CSN0_A[1]
setup_port(
&mut iox,
IoxPort::PD,
6,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
/* // JPC7_13
// SPIM_CLK_A[1]
setup_port(
&mut iox,
IoxPort::PC,
11,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Disable),
None,
None,
);
// SPIM_SD[0-3]_A[1]
for i in 0..3 {
setup_port(
&mut iox,
IoxPort::PC,
i + 7,
Some(IoxFunction::AF1),
None,
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
);
}
// SPIM_CSN0_A[0]
setup_port(
&mut iox,
IoxPort::PC,
12,
Some(IoxFunction::AF1),
Some(IoxDir::Output),
Some(IoxDriveStrength::Drive2mA),
Some(IoxEnable::Enable),
None,
None,
); */
let mut udma_global = GlobalConfig::new(utralib::generated::HW_UDMA_CTRL_BASE as *mut u32);
udma_global.clock_on(PeriphId::Spim0); // JQSPI1
// udma_global.clock_on(PeriphId::Spim1); // JPC7_13
// safety: this is safe because clocks have been set up
let mut flash_spim = unsafe {
Spim::new_with_ifram(
SpimChannel::Channel0,
100_000_000,
100_000_000,
SpimClkPol::LeadingEdgeRise,
SpimClkPha::CaptureOnLeading,
SpimCs::Cs0,
0,
0,
None,
0, // we will never write to flash
4096,
Some(8),
IframRange::from_raw_parts(SPIM_FLASH_IFRAM_ADDR, SPIM_FLASH_IFRAM_ADDR, 4096 * 2),
)
};
let mut ram_spim = unsafe {
Spim::new_with_ifram(
SpimChannel::Channel0,
100_000_000,
100_000_000,
SpimClkPol::LeadingEdgeRise,
SpimClkPha::CaptureOnLeading,
SpimCs::Cs1,
0,
0,
None,
1024, // this is limited by the page length
1024,
Some(6),
IframRange::from_raw_parts(SPIM_RAM_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR, 4096 * 2),
)
};
// sanity check: read ID
println!("flash ID: {:x}", flash_spim.mem_read_id());
println!("ram ID: {:x}", ram_spim.mem_read_id());
// setup FLASH
// - QE enable
// - dummy cycles = 8
flash_spim.mem_write_status_register(0b01_0000_00, 0b10_00_0_111);
// set SPI devices to QPI mode
// We expect a MX25L12833F (3.3V) on CS0
// We expect a ISS66WVS4M8BLL (3.3V) on CS1
// Both support QPI.
flash_spim.mem_qpi_mode(true);
ram_spim.mem_qpi_mode(true);
// allocate the buf
let mut buf = RawPage { data: [0u8; 4096] };
// fetch the header
flash_spim.mem_read(SWAP_IMG_START as u32, &mut buf.data);
// compute offsets for swap
let mac_size = (swap.ram_size as usize / 4096) * size_of::<Tag>();
let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1);
let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page;
if SDBG {
println!(
"mac area size: {:x}, ram_size_actual: {:x}, swap.ram_size: {:x}, mac offset: {:x}",
mac_size,
ram_size_actual,
swap.ram_size,
swap.ram_offset as usize + ram_size_actual
);
}
// generate a random key for swap
let mut trng = sce::trng::Trng::new(utralib::generated::HW_TRNG_BASE as usize);
trng.setup_raw_generation(256);
let mut seed = 0u64;
seed |= trng.get_u32().expect("TRNG error") as u64;
seed |= (trng.get_u32().expect("TRNG error") as u64) << 32;
let mut cstrng = ChaCha8Rng::seed_from_u64(seed);
// accumulate more TRNG data, because I don't trust it.
// 1. whiten the existing TRNG data with ChaCha8
// 2. XOR in another 32 bits of TRNG data
// 3. create a new ChaCha8 from the resulting data
for _ in 0..16 {
seed = cstrng.next_u64();
seed ^= trng.get_u32().expect("TRNG error") as u64;
cstrng = ChaCha8Rng::seed_from_u64(seed);
}
// now we might have a properly seeded cryptographically secure TRNG...
let mut dest_key = [0u8; 32];
for word in dest_key.chunks_mut(core::mem::size_of::<u32>()) {
word.copy_from_slice(&cstrng.next_u32().to_be_bytes());
}
// safety: buf.data is aligned to 4096-byte boundary and filled with initialized data
let ssh: &SwapSourceHeader = unsafe { &*(buf.data.as_ptr() as *const &SwapSourceHeader) };
let mut hal = SwapHal {
image_start: SWAP_IMG_START as usize + 4096,
image_mac_start: SWAP_IMG_START as usize + 4096 + ssh.mac_offset as usize,
partial_nonce: [0u8; 8],
aad_storage: [0u8; 64],
aad_len: 0,
src_cipher: Aes256GcmSiv::new((&swap.key).into()),
flash_spim,
ram_spim,
iox,
udma_global,
swap_start: 0,
swap_len: ram_size_actual,
swap_mac_start: ram_size_actual,
swap_mac_len: mac_size,
dst_cipher: Aes256GcmSiv::new((&dest_key).into()),
buf_addr: 0,
buf,
};
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;
hal.partial_nonce.copy_from_slice(&ssh.parital_nonce);
Some(hal)
} else {
None
}
}
fn aad(&self) -> &[u8] { &self.aad_storage[..self.aad_len] }
pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] {
assert!((offset & 0xFFF) == 0, "offset is not page-aligned");
assert!(offset >= 0x1000);
// compensate for the unencrypted header that is not included in the `src_data_area` slice
let offset = offset - 0x1000;
self.buf_addr = offset;
self.flash_spim.mem_read((self.image_start + offset) as u32, &mut self.buf.data);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes());
nonce[4..].copy_from_slice(&self.partial_nonce);
let mut tag = [0u8; size_of::<Tag>()];
// avoid mutable borrow problem by copying AAD to a dedicated location:
// we could do this with a Refcell but I suspect this is probably actually
// cheaper than the Refcell bookkeeping.
let mut aad = [0u8; 64];
aad[..self.aad_len].copy_from_slice(self.aad());
self.flash_spim
.mem_read((self.image_mac_start + (offset / 4096) * size_of::<Tag>()) as u32, &mut tag);
match self.src_cipher.decrypt_in_place_detached(
Nonce::from_slice(&nonce),
&aad[..self.aad_len],
&mut self.buf.data,
(&tag).into(),
) {
Ok(_) => &self.buf.data,
Err(e) => panic!("Decryption error from swap image: {:?}", e),
}
}
pub fn decrypt_page_addr(&self) -> usize { self.buf_addr }
pub fn buf_as_mut(&mut self) -> &mut [u8] { &mut self.buf.data }
pub fn buf_as_ref(&self) -> &[u8] { &self.buf.data }
/// Swap count is fixed at 0 by this routine. The data to be encrypted is
/// assumed to already be in `self.buf`
pub fn encrypt_swap_to(&mut self, buf: &mut [u8], dest_offset: usize, src_vaddr: usize, src_pid: u8) {
assert!(buf.len() == PAGE_SIZE);
assert!(dest_offset & (PAGE_SIZE - 1) == 0);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field
nonce[5] = src_pid;
let ppage_masked = dest_offset & !(PAGE_SIZE - 1);
nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]);
let vpage_masked = src_vaddr & !(PAGE_SIZE - 1);
nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]);
let aad: &[u8] = &[];
match self.dst_cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) {
Ok(tag) => {
self.ram_spim.mem_ram_write(dest_offset as u32, buf);
self.ram_spim.mem_ram_write(
(self.swap_mac_start + (dest_offset / PAGE_SIZE) * size_of::<Tag>()) as u32,
tag.as_slice(),
);
}
Err(e) => panic!("Encryption error to swap ram: {:?}", e),
}
}
/// Swap count is fixed at 0 by this routine. The data to be encrypted is
/// assumed to already be in `self.buf`
pub fn decrypt_swap_from(&mut self, src_offset: usize, dst_vaddr: usize, dst_pid: u8) -> &[u8] {
assert!(src_offset & (PAGE_SIZE - 1) == 0);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field
nonce[5] = dst_pid;
let ppage_masked = src_offset & !(PAGE_SIZE - 1);
nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]);
let vpage_masked = dst_vaddr & !(PAGE_SIZE - 1);
nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]);
let aad: &[u8] = &[];
let mut tag = [0u8; size_of::<Tag>()];
self.ram_spim
.mem_read((self.swap_mac_start + (src_offset / PAGE_SIZE) * size_of::<Tag>()) as u32, &mut tag);
self.ram_spim.mem_read(src_offset as u32, &mut self.buf.data);
match self.dst_cipher.decrypt_in_place_detached(
Nonce::from_slice(&nonce),
aad,
&mut self.buf.data,
(&tag).into(),
) {
Ok(_) => &self.buf.data,
Err(e) => panic!("Decryption error from swap ram: {:?}", e),
}
}
/// Grabs a slice of the internal buffer. Useful for re-using the decrypted page
/// between elements of the bootloader (saving us from redundant decrypt ops),
/// but extremely unsafe because we have to track the use of this buffer manually.
pub unsafe fn get_decrypt(&self) -> &[u8] { &self.buf.data }
}

View file

@ -1,4 +1,12 @@
#[cfg(feature = "swap")]
pub mod swap;
#[cfg(feature = "swap")]
pub use swap::*;
#[cfg(not(feature = "swap"))]
pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_EXT_MEM_LEN;
#[cfg(feature = "swap")]
pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_EXT_MEM_LEN / 2;
pub const RAM_BASE: usize = utralib::generated::HW_SRAM_EXT_MEM;
/// Note that this memory test is "destructive" -- supend/resume will fail if it is enabled

View file

@ -0,0 +1,199 @@
use core::mem::size_of;
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;
pub struct SwapHal {
src_data_area: &'static [u8],
src_mac_area: &'static [u8],
partial_nonce: [u8; 8],
aad: &'static [u8],
src_cipher: Aes256GcmSiv,
dst_data_area: &'static mut [u8],
dst_mac_area: &'static mut [u8],
dst_cipher: Aes256GcmSiv,
buf_addr: usize,
buf: RawPage,
}
impl SwapHal {
pub fn new(cfg: &BootConfig) -> Option<SwapHal> {
if let Some(swap) = cfg.swap {
// safety: the swap source header is guaranteed to be aligned and initialized in memory
// by the image creator.
let ssh: &SwapSourceHeader =
unsafe { (swap.flash_offset as *const SwapSourceHeader).as_ref().unwrap() };
let swap_len = ssh.mac_offset as usize;
// compute the MAC area needed for the total RAM size. This is a slight over-estimate
// because once we remove the MAC area, we need even less storage, but it's a small error.
let mac_size = (swap.ram_size as usize / 4096) * size_of::<Tag>();
let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) & !(PAGE_SIZE - 1);
let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page;
if SDBG {
println!(
"mac area size: {:x}, ram_size_actual: {:x}, swap.ram_size: {:x}, mac offset: {:x}",
mac_size,
ram_size_actual,
swap.ram_size,
swap.ram_offset as usize + ram_size_actual
);
}
// access the hardware TRNG manually to generate a per-boot random key for RAM swap
let trng = CSR::new(utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u32);
for _ in 0..4 {
// wait until the urandom port is initialized
while trng.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {}
// pull a dummy piece of data
trng.rf(utra::trng_kernel::URANDOM_URANDOM);
}
let mut ram_swap_key = [0u8; 32];
for word in ram_swap_key.chunks_exact_mut(core::mem::size_of::<u32>()) {
while trng.rf(utra::trng_kernel::URANDOM_VALID_URANDOM_VALID) == 0 {}
let r = trng.rf(utra::trng_kernel::URANDOM_URANDOM);
word.copy_from_slice(&r.to_be_bytes())
}
let mut hal = SwapHal {
// safety: the swap raw array is guaranteed to be correctly aligned by the image maker
src_data_area: unsafe {
core::slice::from_raw_parts((swap.flash_offset as usize + 4096) as *const u8, swap_len)
},
// safety: the mac raw array is guaranteed to be correctly aligned by the image maker
src_mac_area: unsafe {
core::slice::from_raw_parts(
(ssh.mac_offset + swap.flash_offset + 4096) as *const u8,
(swap_len / 4096) * size_of::<Tag>(),
)
},
partial_nonce: [0u8; 8],
aad: &ssh.aad[..ssh.aad_len as usize],
src_cipher: Aes256GcmSiv::new((&swap.key).into()),
// safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our
// calculations on lengths ensure area alignment
dst_data_area: unsafe {
core::slice::from_raw_parts_mut(swap.ram_offset as *mut u8, ram_size_actual)
},
// safety: the ram swap area is guaranteed aligned by the ram_offset specifier, and our
// calculations on lengths ensure area alignment
dst_mac_area: unsafe {
core::slice::from_raw_parts_mut(
(swap.ram_offset as usize + ram_size_actual) as *mut u8,
mac_size,
)
},
dst_cipher: Aes256GcmSiv::new(&ram_swap_key.into()),
buf_addr: 0,
buf: RawPage { data: [0u8; 4096] },
};
hal.partial_nonce.copy_from_slice(&ssh.parital_nonce);
Some(hal)
} else {
None
}
}
pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] {
assert!((offset & 0xFFF) == 0, "offset is not page-aligned");
assert!(offset >= 0x1000);
// compensate for the unencrypted header that is not included in the `src_data_area` slice
let offset = offset - 0x1000;
self.buf_addr = offset;
// println!("data area: {:x?}", &self.src_data_area[..4]);
// println!("offset: {:x}", offset);
self.buf.data.copy_from_slice(&self.src_data_area[offset..offset + 4096]);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes());
nonce[4..].copy_from_slice(&self.partial_nonce);
let tag = &self.src_mac_area
[(offset / 4096) * size_of::<Tag>()..(offset / 4096) * size_of::<Tag>() + size_of::<Tag>()];
// println!("nonce: {:x?}", nonce);
// println!("tag: {:x?}", tag);
// println!("aad: {:x?}", self.aad);
// println!("data: {:x?}", &self.buf.data[0..32]);
match self.src_cipher.decrypt_in_place_detached(
Nonce::from_slice(&nonce),
self.aad,
&mut self.buf.data,
tag.into(),
) {
Ok(_) => &self.buf.data,
Err(e) => panic!("Decryption error in swap: {:?}", e),
}
}
pub fn decrypt_page_addr(&self) -> usize { self.buf_addr }
pub fn buf_as_mut(&mut self) -> &mut [u8] { &mut self.buf.data }
pub fn buf_as_ref(&self) -> &[u8] { &self.buf.data }
/// Swap count is fixed at 0 by this routine. The data to be encrypted is provided in
/// `buf`, and is replaced with part of the encrypted data upon completion of the routine.
pub fn encrypt_swap_to(&mut self, buf: &mut [u8], dest_offset: usize, src_vaddr: usize, src_pid: u8) {
// println!("enc_to: pid: {}, src_vaddr: {:x} dest_offset: {:x}", src_pid, src_vaddr, dest_offset);
assert!(buf.len() == PAGE_SIZE);
assert!(dest_offset & (PAGE_SIZE - 1) == 0);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field
nonce[5] = src_pid;
let ppage_masked = dest_offset & !(PAGE_SIZE - 1);
nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]);
let vpage_masked = src_vaddr & !(PAGE_SIZE - 1);
nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]);
let aad: &[u8] = &[];
match self.dst_cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) {
Ok(tag) => {
self.dst_data_area[dest_offset..dest_offset + PAGE_SIZE].copy_from_slice(buf);
let mac_offset = (dest_offset / PAGE_SIZE) * size_of::<Tag>();
self.dst_mac_area[mac_offset..mac_offset + size_of::<Tag>()].copy_from_slice(tag.as_slice());
// println!("Nonce: {:x?}, tag: {:x?}", &nonce, tag.as_slice());
// println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]);
}
Err(e) => panic!("Encryption error to swap ram: {:?}", e),
}
}
/// 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);
assert!(src_offset & (PAGE_SIZE - 1) == 0);
let mut nonce = [0u8; size_of::<Nonce>()];
nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field
nonce[5] = dst_pid;
let ppage_masked = src_offset & !(PAGE_SIZE - 1);
nonce[6..9].copy_from_slice(&(ppage_masked as u32).to_be_bytes()[..3]);
let vpage_masked = dst_vaddr & !(PAGE_SIZE - 1);
nonce[9..12].copy_from_slice(&(vpage_masked as u32).to_be_bytes()[..3]);
let aad: &[u8] = &[];
let mut tag = [0u8; size_of::<Tag>()];
let mac_offset = (src_offset / PAGE_SIZE) * size_of::<Tag>();
tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::<Tag>()]);
// 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);
match self.dst_cipher.decrypt_in_place_detached(
Nonce::from_slice(&nonce),
aad,
&mut self.buf.data,
(&tag).into(),
) {
Ok(_) => &self.buf.data,
Err(e) => panic!("Decryption error from swap ram: {:?}", e),
}
}
/// Grabs a slice of the internal buffer. Useful for re-using the decrypted page
/// between elements of the bootloader (saving us from redundant decrypt ops),
/// but extremely unsafe because we have to track the use of this buffer manually.
pub unsafe fn get_decrypt(&self) -> &[u8] { &self.buf.data }
}

View file

@ -14,7 +14,7 @@ const VERSION_STR: &'static str = "Xous OS Loader v0.9.6\n\r";
// v0.9.6 -- convert signature check to pre-hash signature (see #472 https://github.com/betrusted-io/xous-core/issues/472)
pub const STACK_LEN: u32 = 8192 - (7 * 4); // 7 words for backup kernel args
pub const STACK_TOP: u32 = 0x4100_0000 - STACK_LEN;
pub const STACK_TOP: u32 = (crate::platform::RAM_BASE + crate::platform::RAM_SIZE) as u32 - STACK_LEN;
use utralib::generated::*;

41
loader/src/swap.rs Normal file
View file

@ -0,0 +1,41 @@
/// Virtual address fields:
/// 31 22 21 12 11 0
/// | L1 index | L2 index | LSB of addr |
///
/// L1 PTE thus consists of 1024 entries, each resolving to a 22-bit number.
/// - The bottom 10 bits are flags
/// - The top 2 bits are 0
/// - The middle 20 bits are the MSB of the address to the PA of the L2 PTE
///
/// L2 PTE thus consists of 1024 entries, each resolving to a 22-bit number. It is
/// indexed by the "L2 index" bits.
/// - The bottom 10 bits are flags
/// - The top 2 bits are 0
/// - The middle 20 bits are the MSB of the address to the PA of the target page
#[repr(C)]
pub struct SwapDescriptor {
pub ram_offset: u32,
pub ram_size: u32,
pub name: u32,
pub key: [u8; 32],
pub flash_offset: u32,
}
#[derive(Debug)]
#[repr(C)]
pub struct SwapSourceHeader {
pub version: u32,
pub parital_nonce: [u8; 8],
pub mac_offset: u32,
pub aad_len: u32,
// aad is limited to 64 bytes!
pub aad: [u8; 64],
}
#[repr(C)]
pub struct RawPage {
pub data: [u8; 4096],
}
pub const SWAP_PT_VADDR: usize = 0xE000_0000;

View file

@ -1,8 +1,12 @@
use std::sync::atomic::{AtomicU32, Ordering};
use std::thread::sleep;
use std::time::Duration;
use log::info;
// put here to force a .sbss/.bss section for loader testing
static mut LOOP_COUNT: AtomicU32 = AtomicU32::new(0);
fn main() -> ! {
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Trace);
@ -11,7 +15,8 @@ fn main() -> ! {
const DELAY_MS: u64 = 2000;
for i in 0.. {
info!("Loop #{}, waiting {} ms", i, DELAY_MS);
unsafe { LOOP_COUNT.store(i, Ordering::SeqCst) };
info!("Loop #{}, waiting {} ms", unsafe { LOOP_COUNT.load(Ordering::SeqCst) }, DELAY_MS);
sleep(Duration::from_millis(DELAY_MS));
}

View file

@ -24,6 +24,8 @@ ed25519-dalek = { version = "2.1.0", features = ["digest"] }
sha2 = { version = "0.10.8" }
pkcs8 = { version = "0.8.0", features = ["pem"] }
base64 = "0.20.0"
rand = "0.8.5"
aes-gcm-siv = "0.11.1"
[[bin]]
name = "copy-object"

View file

@ -8,6 +8,7 @@ use std::fs::File;
use clap::{App, Arg};
use tools::elf::{read_minielf, read_program};
use tools::swap_writer::SwapWriter;
use tools::tags::bflg::Bflg;
use tools::tags::inie::IniE;
use tools::tags::inif::IniF;
@ -166,6 +167,20 @@ fn main() {
.value_name("OFFSET:SIZE")
.help("Swap offset and size, in the form of [offset]:[size]; note: offset and size have platform-dependent interpretations")
)
.arg(
Arg::with_name("swap-name")
.long("swap-name")
.takes_value(true)
.value_name("OUTPUT")
.help("Output file to store swap image data")
)
.arg(
Arg::with_name("swap-debug-name")
.long("swap-debug-name")
.takes_value(true)
.value_name("OUTPUT")
.help("Output file to store swap debug data")
)
.arg(
Arg::with_name("debug")
.short("d")
@ -342,6 +357,7 @@ fn main() {
}
let mut args = XousArguments::new(ram_config.offset, ram_config.size, ram_config.name);
let mut swap_args: Option<XousArguments> = None;
if !ram_config.regions.is_empty() {
if let Some(s) = swap {
@ -357,9 +373,12 @@ fn main() {
if s.offset < ram_config.offset + ram_config.size {
ram_config.size = s.offset - ram_config.offset;
}
args.ram_start = ram_config.offset;
args.ram_length = ram_config.size;
}
// Note that other configurations don't split RAM, since the swap is provisioned directly
// in hardware, and thus, no post-processing is required.
swap_args = Some(XousArguments::new(s.offset, s.size, s.name));
args.add(s);
args.add(ram_config.regions);
@ -412,19 +431,23 @@ fn main() {
}
if let Some(init_paths) = matches.values_of("inis") {
for init_path in init_paths {
let program_name = std::path::Path::new(init_path);
process_names.set(
pid,
program_name
.file_stem()
.expect("program had no name")
.to_str()
.expect("program name is not valid utf-8"),
);
pid += 1;
let init = read_minielf(init_path).expect("couldn't parse init file");
args.add(IniS::new(init.entry_point, init.sections, init.program, init.alignment_offset));
if let Some(sargs) = &mut swap_args {
for init_path in init_paths {
let program_name = std::path::Path::new(init_path);
process_names.set(
pid,
program_name
.file_stem()
.expect("program had no name")
.to_str()
.expect("program name is not valid utf-8"),
);
pid += 1;
let init = read_minielf(init_path).expect("couldn't parse init file");
sargs.add(IniS::new(init.entry_point, init.sections, init.program, init.alignment_offset));
}
} else {
println!("Warning: inis regions specified, but no swap region specified. Ignoring inis regions!");
}
}
@ -452,6 +475,30 @@ fn main() {
println!("Arguments: {}", args);
if let Some(mut sargs) = swap_args {
let mut swap_buffer = SwapWriter::new();
sargs.write(&mut swap_buffer).expect("Couldn't write out swap args");
let swap_filename = matches.value_of("swap-name").expect("swap filename not present");
let sf = File::create(swap_filename)
.unwrap_or_else(|_| panic!("Couldn't create output file {}", swap_filename));
swap_buffer.encrypt_to(sf).expect("Couldn't flush swap buffer to disk");
println!("Swap arguments: {}", sargs);
println!("Swap data created in file {}", swap_filename);
match matches.value_of("swap-debug-name") {
Some(sf_dbg) => {
let sf =
File::create(sf_dbg).unwrap_or_else(|_| panic!("Couldn't create debug file {}", sf_dbg));
sargs.write(&sf).expect("Couldn't write debug swap image");
println!("Swap debug created in file {}", sf_dbg);
}
None => {
// Argument wasn't specified, do nothing.
}
}
}
println!("Runtime will require {} bytes to track memory allocations", ram_config.memory_required);
if let Some(s) = swap {
println!("Runtime will also require {} bytes to track swap", s.size / 4096);

View file

@ -4,5 +4,6 @@ extern crate log;
pub mod xous_arguments;
pub mod elf;
pub mod sign_image;
pub mod swap_writer;
pub mod tags;
pub mod utils;

110
tools/src/swap_writer.rs Normal file
View file

@ -0,0 +1,110 @@
use std::convert::TryInto;
use std::io::{Cursor, Result, Seek, SeekFrom, Write};
use aes_gcm_siv::{
aead::{Aead, KeyInit, Payload},
Aes256GcmSiv, Nonce,
};
use rand::Rng;
const SWAP_VERSION: u32 = 0x01_01_0000;
pub struct SwapWriter {
pub buffer: Cursor<Vec<u8>>,
}
struct SwapHeader {
aad: Vec<u8>,
partial_nonce: u64,
/// offset, starting from the top of payload data (so, +0x1000 to get absolute offset after the header)
mac_offset: usize,
}
impl SwapHeader {
pub fn new(swap_len: usize) -> Self {
let mut rng = rand::thread_rng();
SwapHeader {
aad: "swap".as_bytes().to_vec(),
partial_nonce: rng.gen(),
mac_offset: ((swap_len + 0xFFF) / 0x1000) * 0x1000,
}
}
/// Returns exactly a page of data with the header format serialized
pub fn serialize(&self) -> Result<[u8; 4096]> {
let mut data = Cursor::new(Vec::<u8>::new());
let mut output = [0u8; 4096];
data.write(&SWAP_VERSION.to_le_bytes())?;
// note that the nonce is in big-endian format, as is expected for cryptographic matter
data.write(&self.partial_nonce.to_be_bytes())?;
// serialize the MAC data offset
data.write(&(self.mac_offset as u32).to_le_bytes())?; // LE because this is a size field
// serialize the AAD
assert!(self.aad.len() < 64, "AAD is limited to 64 bytes");
data.write(&(self.aad.len() as u32).to_le_bytes())?; // LE because this is a size field
data.write(&self.aad)?; // BE because this is cryptographic matter
output[..data.position() as usize].copy_from_slice(&data.into_inner());
Ok(output)
}
}
impl SwapWriter {
pub fn new() -> Self { SwapWriter { buffer: Cursor::new(Vec::new()) } }
/// Take the swap file and wrap it data structures that facilitate per-device encryption
/// after deployment to a user device.
pub fn encrypt_to<T>(&mut self, mut f: T) -> Result<usize>
where
T: Write + Seek,
{
let header = SwapHeader::new(self.buffer.get_ref().len());
let mut macs = Vec::<u8>::new();
f.write(&header.serialize()?)?;
// encrypt using the "zero key" for the default distribution. The intention is not to
// provide security, but to lay out the data structure so that a future re-encryption to a
// secret key generated in the device can provide security.
let zero_key = [0u8; 32];
let cipher = Aes256GcmSiv::new(&zero_key.try_into().unwrap());
let buf = self.buffer.get_ref();
for (offset, block) in buf.chunks(0x1000).enumerate() {
let padded_block = if block.len() != 0x1000 {
[block, &vec![0u8; 0x1000 - block.len()]].concat()
} else {
block.to_owned()
};
let mut nonce_vec = Vec::new();
// use BE encoding because nonce is cryptographic matter
nonce_vec.extend_from_slice(&((offset as u32) * 0x1000).to_be_bytes());
nonce_vec.extend_from_slice(&header.partial_nonce.to_be_bytes());
let nonce = Nonce::from_slice(&nonce_vec);
// println!("nonce: {:x?}", nonce);
// println!("aad: {:x?}", header.aad);
let enc = cipher
.encrypt(nonce, Payload { aad: &header.aad, msg: &padded_block })
.expect("couldn't encrypt block");
assert!(enc.len() == 0x1010);
// println!("data: {:x?}", &enc[..32]);
// println!("tag: {:x?}", &enc[0x1000..]);
f.write(&enc[..0x1000])?;
macs.extend_from_slice(&enc[0x1000..]);
}
f.write(&macs)
}
}
impl Write for SwapWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { self.buffer.write(buf) }
fn flush(&mut self) -> std::io::Result<()> { self.buffer.flush() }
}
impl Seek for SwapWriter {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> { self.buffer.seek(pos) }
}

View file

@ -84,6 +84,15 @@ impl MemoryRegions {
pub fn len(&self) -> usize { self.regions.len() }
pub fn is_empty(&self) -> bool { self.regions.is_empty() }
#[allow(dead_code)]
pub fn patch(&mut self, region_name: &str, new_spec: MemoryRegion) {
if let Some(index) = self.regions.iter().position(|&x| x.name == MemoryRegion::make_name(region_name))
{
self.regions.remove(index);
self.regions.insert(index, new_spec);
}
}
}
impl XousArgument for MemoryRegions {

View file

@ -5,44 +5,57 @@ use crate::xous_arguments::{XousArgument, XousArgumentCode, XousSize};
#[derive(Debug, Default, Clone, Copy)]
pub struct Swap {
/// Starting offset (in bytes)
/// Starting offset (in bytes) of the swap RAM
pub offset: u32,
/// Length (in bytes)
/// Total swap length (in bytes) - not all is usable, as a portion is reserved for the MAC codes
pub size: u32,
/// Region name (as a type)
pub name: XousArgumentCode,
/// Unused
_padding: u32,
/// Encryption key. Set to 0 by the image creator, but set to something more interesting
/// by the device upon completion of the keying ceremony.
pub key: [u8; 32],
/// Starting offset of swap in FLASH
pub offset_flash: u32,
}
impl fmt::Display for Swap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, " Swap: {:08x} - {:08x}", self.offset, self.offset + self.size)?;
writeln!(f, " key: {:x?}", self.key)?;
Ok(())
}
}
impl Swap {
pub fn new(offset: XousSize, size: XousSize) -> Self {
Swap { offset, size, name: u32::from_le_bytes(*b"Swap") as XousArgumentCode, _padding: 0 }
Swap {
offset,
size,
name: u32::from_le_bytes(*b"Swap") as XousArgumentCode,
key: [0u8; 32],
// location for swap offset in FLASH (for precursor)
offset_flash: 0x21200000,
}
}
}
impl XousArgument for Swap {
fn code(&self) -> XousArgumentCode { self.name }
fn length(&self) -> XousSize { 16 as XousSize }
fn length(&self) -> XousSize { (16 + 32) as XousSize }
fn serialize(&self, output: &mut dyn io::Write) -> io::Result<usize> {
let mut written = 0;
output.write_all(&self.offset.to_le_bytes())?;
output.write_all(&self.size.to_le_bytes())?;
output.write_all(&self.name.to_le_bytes())?;
output.write_all(&0u32.to_le_bytes())?;
written += 4 * 4;
output.write_all(&self.key)?;
output.write_all(&self.offset_flash.to_le_bytes())?;
written += 4 * 4 + 32;
Ok(written)
}
}

View file

@ -40,8 +40,8 @@ pub trait XousArgument: fmt::Display {
}
pub struct XousArguments {
ram_start: XousSize,
ram_length: XousSize,
pub ram_start: XousSize,
pub ram_length: XousSize,
ram_name: u32,
pub arguments: Vec<Box<dyn XousArgument>>,
}

View file

@ -4678,9 +4678,11 @@ pub mod utra {
pub const SFR_CFGINFO_CONSTANT1: crate::Field = crate::Field::new(8, 8, SFR_CFGINFO);
pub const SFR_CFGINFO_CONSTANT2: crate::Field = crate::Field::new(16, 16, SFR_CFGINFO);
pub const SFR_CONFIG: crate::Register = crate::Register::new(2, 0x7);
pub const SFR_CONFIG_SNAP_TO_WHICH: crate::Field = crate::Field::new(2, 0, SFR_CONFIG);
pub const SFR_CONFIG_SNAP_TO_QUANTUM: crate::Field = crate::Field::new(1, 2, SFR_CONFIG);
pub const SFR_CONFIG: crate::Register = crate::Register::new(2, 0x3f);
pub const SFR_CONFIG_SNAP_OUTPUT_TO_WHICH: crate::Field = crate::Field::new(2, 0, SFR_CONFIG);
pub const SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM: crate::Field = crate::Field::new(1, 2, SFR_CONFIG);
pub const SFR_CONFIG_SNAP_INPUT_TO_WHICH: crate::Field = crate::Field::new(2, 3, SFR_CONFIG);
pub const SFR_CONFIG_SNAP_INPUT_TO_QUANTUM: crate::Field = crate::Field::new(1, 5, SFR_CONFIG);
pub const SFR_FLEVEL: crate::Register = crate::Register::new(3, 0xffff);
pub const SFR_FLEVEL_PCLK_REGFIFO_LEVEL0: crate::Field = crate::Field::new(4, 0, SFR_FLEVEL);
@ -4712,39 +4714,40 @@ pub mod utra {
pub const SFR_RXF3: crate::Register = crate::Register::new(11, 0xffffffff);
pub const SFR_RXF3_FDOUT: crate::Field = crate::Field::new(32, 0, SFR_RXF3);
pub const SFR_ELEVEL0: crate::Register = crate::Register::new(12, 0xffffffff);
pub const SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL0: crate::Field = crate::Field::new(8, 0, SFR_ELEVEL0);
pub const SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL1: crate::Field = crate::Field::new(8, 8, SFR_ELEVEL0);
pub const SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL2: crate::Field = crate::Field::new(8, 16, SFR_ELEVEL0);
pub const SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL3: crate::Field = crate::Field::new(8, 24, SFR_ELEVEL0);
pub const SFR_ELEVEL: crate::Register = crate::Register::new(12, 0xffffffff);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL0: crate::Field = crate::Field::new(4, 0, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL1: crate::Field = crate::Field::new(4, 4, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL2: crate::Field = crate::Field::new(4, 8, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL3: crate::Field = crate::Field::new(4, 12, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL4: crate::Field = crate::Field::new(4, 16, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL5: crate::Field = crate::Field::new(4, 20, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL6: crate::Field = crate::Field::new(4, 24, SFR_ELEVEL);
pub const SFR_ELEVEL_FIFO_EVENT_LEVEL7: crate::Field = crate::Field::new(4, 28, SFR_ELEVEL);
pub const SFR_ELEVEL1: crate::Register = crate::Register::new(13, 0xffffffff);
pub const SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL4: crate::Field = crate::Field::new(8, 0, SFR_ELEVEL1);
pub const SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL5: crate::Field = crate::Field::new(8, 8, SFR_ELEVEL1);
pub const SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL6: crate::Field = crate::Field::new(8, 16, SFR_ELEVEL1);
pub const SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL7: crate::Field = crate::Field::new(8, 24, SFR_ELEVEL1);
pub const SFR_ETYPE: crate::Register = crate::Register::new(13, 0xffffff);
pub const SFR_ETYPE_FIFO_EVENT_LT_MASK: crate::Field = crate::Field::new(8, 0, SFR_ETYPE);
pub const SFR_ETYPE_FIFO_EVENT_EQ_MASK: crate::Field = crate::Field::new(8, 8, SFR_ETYPE);
pub const SFR_ETYPE_FIFO_EVENT_GT_MASK: crate::Field = crate::Field::new(8, 16, SFR_ETYPE);
pub const SFR_ETYPE: crate::Register = crate::Register::new(14, 0xffffff);
pub const SFR_ETYPE_PCLK_FIFO_EVENT_LT_MASK: crate::Field = crate::Field::new(8, 0, SFR_ETYPE);
pub const SFR_ETYPE_PCLK_FIFO_EVENT_EQ_MASK: crate::Field = crate::Field::new(8, 8, SFR_ETYPE);
pub const SFR_ETYPE_PCLK_FIFO_EVENT_GT_MASK: crate::Field = crate::Field::new(8, 16, SFR_ETYPE);
pub const SFR_EVENT_SET: crate::Register = crate::Register::new(15, 0xffffff);
pub const SFR_EVENT_SET: crate::Register = crate::Register::new(14, 0xffffff);
pub const SFR_EVENT_SET_SFR_EVENT_SET: crate::Field = crate::Field::new(24, 0, SFR_EVENT_SET);
pub const SFR_EVENT_CLR: crate::Register = crate::Register::new(16, 0xffffff);
pub const SFR_EVENT_CLR: crate::Register = crate::Register::new(15, 0xffffff);
pub const SFR_EVENT_CLR_SFR_EVENT_CLR: crate::Field = crate::Field::new(24, 0, SFR_EVENT_CLR);
pub const SFR_EVENT_STATUS: crate::Register = crate::Register::new(17, 0xffffffff);
pub const SFR_EVENT_STATUS: crate::Register = crate::Register::new(16, 0xffffffff);
pub const SFR_EVENT_STATUS_SFR_EVENT_STATUS: crate::Field = crate::Field::new(32, 0, SFR_EVENT_STATUS);
pub const SFR_EXTCLOCK: crate::Register = crate::Register::new(18, 0xffffff);
pub const SFR_EXTCLOCK: crate::Register = crate::Register::new(17, 0xffffff);
pub const SFR_EXTCLOCK_USE_EXTCLK: crate::Field = crate::Field::new(4, 0, SFR_EXTCLOCK);
pub const SFR_EXTCLOCK_EXTCLK_GPIO_0: crate::Field = crate::Field::new(5, 4, SFR_EXTCLOCK);
pub const SFR_EXTCLOCK_EXTCLK_GPIO_1: crate::Field = crate::Field::new(5, 9, SFR_EXTCLOCK);
pub const SFR_EXTCLOCK_EXTCLK_GPIO_2: crate::Field = crate::Field::new(5, 14, SFR_EXTCLOCK);
pub const SFR_EXTCLOCK_EXTCLK_GPIO_3: crate::Field = crate::Field::new(5, 19, SFR_EXTCLOCK);
pub const SFR_FIFO_CLR: crate::Register = crate::Register::new(18, 0xf);
pub const SFR_FIFO_CLR_SFR_FIFO_CLR: crate::Field = crate::Field::new(4, 0, SFR_FIFO_CLR);
pub const SFR_QDIV0: crate::Register = crate::Register::new(20, 0xffffffff);
pub const SFR_QDIV0_UNUSED_DIV: crate::Field = crate::Field::new(8, 0, SFR_QDIV0);
pub const SFR_QDIV0_DIV_FRAC: crate::Field = crate::Field::new(8, 8, SFR_QDIV0);
@ -18949,16 +18952,26 @@ mod tests {
let foo = bio_csr.r(utra::bio::SFR_CONFIG);
bio_csr.wo(utra::bio::SFR_CONFIG, foo);
let bar = bio_csr.rf(utra::bio::SFR_CONFIG_SNAP_TO_WHICH);
bio_csr.rmwf(utra::bio::SFR_CONFIG_SNAP_TO_WHICH, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_CONFIG_SNAP_TO_WHICH, bar);
baz |= bio_csr.ms(utra::bio::SFR_CONFIG_SNAP_TO_WHICH, 1);
bio_csr.wfo(utra::bio::SFR_CONFIG_SNAP_TO_WHICH, baz);
let bar = bio_csr.rf(utra::bio::SFR_CONFIG_SNAP_TO_QUANTUM);
bio_csr.rmwf(utra::bio::SFR_CONFIG_SNAP_TO_QUANTUM, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_CONFIG_SNAP_TO_QUANTUM, bar);
baz |= bio_csr.ms(utra::bio::SFR_CONFIG_SNAP_TO_QUANTUM, 1);
bio_csr.wfo(utra::bio::SFR_CONFIG_SNAP_TO_QUANTUM, baz);
let bar = bio_csr.rf(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH);
bio_csr.rmwf(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, bar);
baz |= bio_csr.ms(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, 1);
bio_csr.wfo(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_WHICH, baz);
let bar = bio_csr.rf(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM);
bio_csr.rmwf(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, bar);
baz |= bio_csr.ms(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, 1);
bio_csr.wfo(utra::bio::SFR_CONFIG_SNAP_OUTPUT_TO_QUANTUM, baz);
let bar = bio_csr.rf(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_WHICH);
bio_csr.rmwf(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_WHICH, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_WHICH, bar);
baz |= bio_csr.ms(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_WHICH, 1);
bio_csr.wfo(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_WHICH, baz);
let bar = bio_csr.rf(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_QUANTUM);
bio_csr.rmwf(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_QUANTUM, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_QUANTUM, bar);
baz |= bio_csr.ms(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_QUANTUM, 1);
bio_csr.wfo(utra::bio::SFR_CONFIG_SNAP_INPUT_TO_QUANTUM, baz);
let foo = bio_csr.r(utra::bio::SFR_FLEVEL);
bio_csr.wo(utra::bio::SFR_FLEVEL, foo);
@ -19047,69 +19060,66 @@ mod tests {
baz |= bio_csr.ms(utra::bio::SFR_RXF3_FDOUT, 1);
bio_csr.wfo(utra::bio::SFR_RXF3_FDOUT, baz);
let foo = bio_csr.r(utra::bio::SFR_ELEVEL0);
bio_csr.wo(utra::bio::SFR_ELEVEL0, foo);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL0);
bio_csr.rmwf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL0, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL0, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL0, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL0, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL1);
bio_csr.rmwf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL1, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL1, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL1, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL1, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL2);
bio_csr.rmwf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL2, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL2, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL2, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL2, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL3);
bio_csr.rmwf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL3, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL3, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL3, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL0_PCLK_FIFO_EVENT_LEVEL3, baz);
let foo = bio_csr.r(utra::bio::SFR_ELEVEL1);
bio_csr.wo(utra::bio::SFR_ELEVEL1, foo);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL4);
bio_csr.rmwf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL4, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL4, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL4, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL4, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL5);
bio_csr.rmwf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL5, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL5, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL5, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL5, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL6);
bio_csr.rmwf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL6, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL6, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL6, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL6, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL7);
bio_csr.rmwf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL7, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL7, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL7, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL1_PCLK_FIFO_EVENT_LEVEL7, baz);
let foo = bio_csr.r(utra::bio::SFR_ELEVEL);
bio_csr.wo(utra::bio::SFR_ELEVEL, foo);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL0);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL0, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL0, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL0, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL0, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL1);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL1, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL1, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL1, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL1, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL2);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL2, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL2, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL2, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL2, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL3);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL3, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL3, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL3, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL3, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL4, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL5);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL5, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL5, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL5, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL5, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL6);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL6, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL6, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL6, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL6, baz);
let bar = bio_csr.rf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL7);
bio_csr.rmwf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL7, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL7, bar);
baz |= bio_csr.ms(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL7, 1);
bio_csr.wfo(utra::bio::SFR_ELEVEL_FIFO_EVENT_LEVEL7, baz);
let foo = bio_csr.r(utra::bio::SFR_ETYPE);
bio_csr.wo(utra::bio::SFR_ETYPE, foo);
let bar = bio_csr.rf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_LT_MASK);
bio_csr.rmwf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_LT_MASK, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_LT_MASK, bar);
baz |= bio_csr.ms(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_LT_MASK, 1);
bio_csr.wfo(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_LT_MASK, baz);
let bar = bio_csr.rf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_EQ_MASK);
bio_csr.rmwf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_EQ_MASK, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_EQ_MASK, bar);
baz |= bio_csr.ms(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_EQ_MASK, 1);
bio_csr.wfo(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_EQ_MASK, baz);
let bar = bio_csr.rf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_GT_MASK);
bio_csr.rmwf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_GT_MASK, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_GT_MASK, bar);
baz |= bio_csr.ms(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_GT_MASK, 1);
bio_csr.wfo(utra::bio::SFR_ETYPE_PCLK_FIFO_EVENT_GT_MASK, baz);
let bar = bio_csr.rf(utra::bio::SFR_ETYPE_FIFO_EVENT_LT_MASK);
bio_csr.rmwf(utra::bio::SFR_ETYPE_FIFO_EVENT_LT_MASK, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ETYPE_FIFO_EVENT_LT_MASK, bar);
baz |= bio_csr.ms(utra::bio::SFR_ETYPE_FIFO_EVENT_LT_MASK, 1);
bio_csr.wfo(utra::bio::SFR_ETYPE_FIFO_EVENT_LT_MASK, baz);
let bar = bio_csr.rf(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK);
bio_csr.rmwf(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK, bar);
baz |= bio_csr.ms(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK, 1);
bio_csr.wfo(utra::bio::SFR_ETYPE_FIFO_EVENT_EQ_MASK, baz);
let bar = bio_csr.rf(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK);
bio_csr.rmwf(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, bar);
baz |= bio_csr.ms(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, 1);
bio_csr.wfo(utra::bio::SFR_ETYPE_FIFO_EVENT_GT_MASK, baz);
let foo = bio_csr.r(utra::bio::SFR_EVENT_SET);
bio_csr.wo(utra::bio::SFR_EVENT_SET, foo);
@ -19163,6 +19173,14 @@ mod tests {
baz |= bio_csr.ms(utra::bio::SFR_EXTCLOCK_EXTCLK_GPIO_3, 1);
bio_csr.wfo(utra::bio::SFR_EXTCLOCK_EXTCLK_GPIO_3, baz);
let foo = bio_csr.r(utra::bio::SFR_FIFO_CLR);
bio_csr.wo(utra::bio::SFR_FIFO_CLR, foo);
let bar = bio_csr.rf(utra::bio::SFR_FIFO_CLR_SFR_FIFO_CLR);
bio_csr.rmwf(utra::bio::SFR_FIFO_CLR_SFR_FIFO_CLR, bar);
let mut baz = bio_csr.zf(utra::bio::SFR_FIFO_CLR_SFR_FIFO_CLR, bar);
baz |= bio_csr.ms(utra::bio::SFR_FIFO_CLR_SFR_FIFO_CLR, 1);
bio_csr.wfo(utra::bio::SFR_FIFO_CLR_SFR_FIFO_CLR, baz);
let foo = bio_csr.r(utra::bio::SFR_QDIV0);
bio_csr.wo(utra::bio::SFR_QDIV0, foo);
let bar = bio_csr.rf(utra::bio::SFR_QDIV0_UNUSED_DIV);

View file

@ -933,7 +933,18 @@ impl Builder {
memory_spec: Vec<String>,
) -> Result<PathBuf, DynError> {
let stream = self.stream.as_str();
let mut args = vec!["run", "--package", "tools", "--bin", "create-image", "--"];
let mut args = vec!["run", "--package", "tools", "--bin", "create-image"];
args.push("--features");
if self.utra_target.contains("renode") {
args.push("renode");
} else if self.utra_target.contains("precursor") {
args.push("precursor");
} else if self.utra_target.contains("atsama5d2") {
args.push("atsama5d2");
} else if self.utra_target.contains("cramium-soc") {
args.push("cramium-soc")
}
args.push("--");
let mut output_file = PathBuf::new();
output_file.push("target");
@ -971,9 +982,23 @@ impl Builder {
} else {
String::new()
};
let mut swap_file = PathBuf::new();
let mut swap_dbg_file = PathBuf::new();
if self.swap.is_some() {
args.push("--swap");
args.push(&swap_spec);
swap_file.push("target");
swap_file.push(self.target.as_ref().expect("target"));
swap_file.push(stream);
swap_file.push("swap.img");
args.push("--swap-name");
args.push(swap_file.to_str().unwrap());
swap_dbg_file.push("target");
swap_dbg_file.push(self.target.as_ref().expect("target"));
swap_dbg_file.push(stream);
swap_dbg_file.push("swap-debug.img");
args.push("--swap-debug-name");
args.push(swap_dbg_file.to_str().unwrap());
}
if memory_spec.len() == 1 {

View file

@ -210,10 +210,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// ----- renode configs --------
Some("renode-image") => {
builder.target_renode().add_services(&user_pkgs).add_apps(&get_cratespecs());
builder.add_loader_feature("resume");
// builder.add_loader_feature("debug-print");
}
Some("renode-image-debug") => {
builder
.target_renode()
.add_loader_feature("resume")
.add_services(&user_pkgs)
.stream(BuildStream::Debug)
.add_apps(&get_cratespecs());
@ -244,9 +247,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
Some("renode-swap") => {
if !builder.is_swap_set() {
builder.set_swap(0x4040_0000, 8 * 1024 * 1024);
builder.set_swap(0x4080_0000, 8 * 1024 * 1024);
}
builder.target_renode();
// builder.target_cramium_soc();
builder.add_loader_feature("debug-print");
builder.add_loader_feature("swap");
builder.add_kernel_feature("swap");
builder.add_kernel_feature("debug-swap");
// It is important that this is the first service added, because the swapper *must* be in PID 2
builder.add_service("xous-swapper", LoaderRegion::Ram);
builder.add_kernel_feature("swap");