From 0989f22417b90b30d90fa99529093803534ee637 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:27:24 +0800 Subject: [PATCH 001/107] make kernel aware of the IniS type this should now introduce a crash in the kernel when an IniS process is in the image set; previously we didn't try to schedule and run them. --- kernel/src/services.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 547ef0608..d5e0eaa1e 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -352,7 +352,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; } } @@ -365,7 +368,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 { @@ -398,7 +404,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; From a6696aaf776c7ec58fce103798302dbfc49829cf Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:28:04 +0800 Subject: [PATCH 002/107] add structure to differentiate image type we now have three image types: inie, inif, inis; migrate away from a simple "xip" flag to something that can differentiate between all three options. --- loader/src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 71f71035b..d0c60aa3f 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -59,6 +59,16 @@ pub struct PageTable { entries: [usize; PAGE_SIZE / WORD_SIZE], } +#[derive(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. @@ -344,7 +354,10 @@ pub fn read_initial_config(cfg: &mut BootConfig) { assert!(!kernel_seen, "kernel appears twice"); assert!(tag.size as usize == mem::size_of::(), "invalid XKrn size"); kernel_seen = true; - } else if tag.name == u32::from_le_bytes(*b"IniE") || tag.name == u32::from_le_bytes(*b"IniF") { + } else if tag.name == u32::from_le_bytes(*b"IniE") + || tag.name == u32::from_le_bytes(*b"IniF") + || tag.name == u32::from_le_bytes(*b"IniS") + { assert!(tag.size >= 4, "invalid Init size"); init_seen = true; cfg.init_process_count += 1; @@ -467,6 +480,11 @@ fn check_load(cfg: &mut BootConfig) { println!("\n\nChecking IniF region"); inif.check(cfg, inif.load_offset as usize, pid, true); 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, true); + pid += 1; } } } From cb7cbd935738d66e0b662495cb2015d18d0c5df9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:28:53 +0800 Subject: [PATCH 003/107] put todo! placeholders for minielf handling of inis --- loader/src/minielf.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 4defe972f..50f8847aa 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -93,7 +93,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; @@ -104,16 +104,20 @@ impl MiniElf { 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 +235,7 @@ impl MiniElf { panic!("init section addresses are not strictly increasing"); } - if copy_to_ram || !xip { + if copy_to_ram || ini_type == IniType::IniE { let mut this_page = section.virt as usize & !(PAGE_SIZE - 1); let mut bytes_to_copy = section.len(); @@ -350,6 +354,8 @@ impl MiniElf { } // --- map FLASH pages to virtual memory --- + // TODO: handle mapping of swap-located pages to virtual memory + todo!("Handle swap-located pages mapping into 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); From e5ecdc88f5fecd90bc9e0d939934c3214e529ea6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:29:23 +0800 Subject: [PATCH 004/107] put placeholders to handle inis in phase1 --- loader/src/phase1.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 00b428af3..d19a60a27 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -147,6 +147,7 @@ pub fn copy_args(cfg: &mut BootConfig) { enum TagType { IniE, IniF, + IniS, XKrn, Other, } @@ -156,6 +157,8 @@ impl From 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 +172,7 @@ impl TagType { match self { TagType::IniE => "IniE", TagType::IniF => "IniF", + TagType::IniS => "IniS", TagType::XKrn => "XKrn", TagType::Other => "Other", } @@ -314,6 +318,17 @@ fn copy_processes(cfg: &mut BootConfig) { } println!("Done with sections"); } + TagType::IniS => { + // IniS does not necessarily exist in linear memory space. + + // TODO: access IniS image and figure out what regions are "write" and allocate/copy + // those into RAM. We do this because there isn't a 1:1 mapping of these to the + // images on disk which creates a problem for the swapper. + + // Then, for regions that *are* aligned, allocate them into the swapper's + // memory space, and copy the data into swap. + todo!("implement inis handling"); + } TagType::XKrn => { let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; From 5a3a8584ffc07eff38857a9cf7864d04855545b8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:29:38 +0800 Subject: [PATCH 005/107] put placeholders for inis handling, phase 2 --- loader/src/phase2.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 83f7808bd..f5f655768 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -80,17 +80,24 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { 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") { + 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) }; From 9afc7cd60574c398f88052091415a3349ae8847d Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:27:24 +0800 Subject: [PATCH 006/107] make kernel aware of the IniS type this should now introduce a crash in the kernel when an IniS process is in the image set; previously we didn't try to schedule and run them. --- kernel/src/services.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 09f25d3fe..6d1e96ce0 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -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; From bae3102e876dbd9fb7b56262b5c9f4ea2245f862 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:28:04 +0800 Subject: [PATCH 007/107] add structure to differentiate image type we now have three image types: inie, inif, inis; migrate away from a simple "xip" flag to something that can differentiate between all three options. --- loader/src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 71f71035b..d0c60aa3f 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -59,6 +59,16 @@ pub struct PageTable { entries: [usize; PAGE_SIZE / WORD_SIZE], } +#[derive(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. @@ -344,7 +354,10 @@ pub fn read_initial_config(cfg: &mut BootConfig) { assert!(!kernel_seen, "kernel appears twice"); assert!(tag.size as usize == mem::size_of::(), "invalid XKrn size"); kernel_seen = true; - } else if tag.name == u32::from_le_bytes(*b"IniE") || tag.name == u32::from_le_bytes(*b"IniF") { + } else if tag.name == u32::from_le_bytes(*b"IniE") + || tag.name == u32::from_le_bytes(*b"IniF") + || tag.name == u32::from_le_bytes(*b"IniS") + { assert!(tag.size >= 4, "invalid Init size"); init_seen = true; cfg.init_process_count += 1; @@ -467,6 +480,11 @@ fn check_load(cfg: &mut BootConfig) { println!("\n\nChecking IniF region"); inif.check(cfg, inif.load_offset as usize, pid, true); 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, true); + pid += 1; } } } From 184ab780d9f609b21d9407b9c3ceca8d63dc32af Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:28:53 +0800 Subject: [PATCH 008/107] put todo! placeholders for minielf handling of inis --- loader/src/minielf.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 4defe972f..50f8847aa 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -93,7 +93,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; @@ -104,16 +104,20 @@ impl MiniElf { 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 +235,7 @@ impl MiniElf { panic!("init section addresses are not strictly increasing"); } - if copy_to_ram || !xip { + if copy_to_ram || ini_type == IniType::IniE { let mut this_page = section.virt as usize & !(PAGE_SIZE - 1); let mut bytes_to_copy = section.len(); @@ -350,6 +354,8 @@ impl MiniElf { } // --- map FLASH pages to virtual memory --- + // TODO: handle mapping of swap-located pages to virtual memory + todo!("Handle swap-located pages mapping into 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); From baf61a4efa9afde6ad77ba50649323b42663526c Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:29:23 +0800 Subject: [PATCH 009/107] put placeholders to handle inis in phase1 --- loader/src/phase1.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 00b428af3..d19a60a27 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -147,6 +147,7 @@ pub fn copy_args(cfg: &mut BootConfig) { enum TagType { IniE, IniF, + IniS, XKrn, Other, } @@ -156,6 +157,8 @@ impl From 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 +172,7 @@ impl TagType { match self { TagType::IniE => "IniE", TagType::IniF => "IniF", + TagType::IniS => "IniS", TagType::XKrn => "XKrn", TagType::Other => "Other", } @@ -314,6 +318,17 @@ fn copy_processes(cfg: &mut BootConfig) { } println!("Done with sections"); } + TagType::IniS => { + // IniS does not necessarily exist in linear memory space. + + // TODO: access IniS image and figure out what regions are "write" and allocate/copy + // those into RAM. We do this because there isn't a 1:1 mapping of these to the + // images on disk which creates a problem for the swapper. + + // Then, for regions that *are* aligned, allocate them into the swapper's + // memory space, and copy the data into swap. + todo!("implement inis handling"); + } TagType::XKrn => { let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; From 4e843fbc4207c6445a3335fb5e95d3fe30bd64a9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:29:38 +0800 Subject: [PATCH 010/107] put placeholders for inis handling, phase 2 --- loader/src/phase2.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 83f7808bd..f5f655768 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -80,17 +80,24 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { 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") { + 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) }; From dd0b012706d1d04eff158856dedbe028a1b4896d Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:32:19 +0800 Subject: [PATCH 011/107] specify swap file name --- xtask/src/builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 98ddfd5a9..52db53892 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -971,9 +971,16 @@ impl Builder { } else { String::new() }; + let mut swap_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()) } if memory_spec.len() == 1 { From 9eebf5dd05a4411dc98e6bf624f5edcd2ba7a3b6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:32:45 +0800 Subject: [PATCH 012/107] add the swap writer This will take the buffer that is generated from a xous argument and encrypts it to the prescribed "detached swap" format. --- tools/src/swap_writer.rs | 103 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tools/src/swap_writer.rs diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs new file mode 100644 index 000000000..91b448a6d --- /dev/null +++ b/tools/src/swap_writer.rs @@ -0,0 +1,103 @@ +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>, +} + +struct SwapHeader { + aad: Vec, + 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::::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 AAD + 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 + + // serialize the MAC data offset + data.write(&(self.mac_offset as u32).to_le_bytes())?; // LE because this is a size field + + 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()) } } + + pub fn encrypt_to(&mut self, mut f: T) -> Result + where + T: Write + Seek, + { + let header = SwapHeader::new(self.buffer.get_ref().len()); + let mut macs = Vec::::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).to_be_bytes()); + nonce_vec.extend_from_slice(&header.partial_nonce.to_be_bytes()); + let nonce = Nonce::from_slice(&nonce_vec); + let enc = cipher + .encrypt(nonce, Payload { aad: &header.aad, msg: &padded_block }) + .expect("couldn't encrypt block"); + assert!(enc.len() == 0x1010); + 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 { 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 { self.buffer.seek(pos) } +} From e1dbd6726b6d9b346fa86b009af37ebb8e08d41a Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:33:29 +0800 Subject: [PATCH 013/107] add the swap_writer module to the library --- tools/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/src/lib.rs b/tools/src/lib.rs index 87f18cc2b..6a2efd11f 100644 --- a/tools/src/lib.rs +++ b/tools/src/lib.rs @@ -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; From 932f13b5320765eeae8956cbd0da467a59ae74c8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:34:00 +0800 Subject: [PATCH 014/107] rework create-image to put inis objects into the detached swap region --- tools/src/bin/create-image.rs | 52 ++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index 631942e4b..52f5a3cf8 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -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,13 @@ 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("debug") .short("d") @@ -342,6 +350,7 @@ fn main() { } let mut args = XousArguments::new(ram_config.offset, ram_config.size, ram_config.name); + let mut swap_args: Option = None; if !ram_config.regions.is_empty() { if let Some(s) = swap { @@ -360,6 +369,7 @@ fn main() { } // 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 +422,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(ref mut sargs) = 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 +466,18 @@ 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); + } 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); From 7facdf5e7d168546bcfbaeede385e285584dc225 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:34:23 +0800 Subject: [PATCH 015/107] add cryptographic libraries to the image creator manifest --- tools/Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 8600cf2b3..3c293dc22 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -24,6 +24,9 @@ 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" +aead = { version = "0.5.2", features = ["alloc"] } +aes-gcm-siv = "0.11.1" [[bin]] name = "copy-object" From 01ef436bd0e6af5b7fb4954e9927fc9b1ece288c Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:34:45 +0800 Subject: [PATCH 016/107] absorb new dependencies --- Cargo.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ff5a885e6..bd18acde1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] @@ -5132,6 +5133,8 @@ dependencies = [ name = "tools" version = "0.1.0" dependencies = [ + "aead", + "aes-gcm-siv", "base64 0.20.0", "bitflags 1.3.2", "clap 2.34.0", @@ -5142,6 +5145,7 @@ dependencies = [ "log", "pem", "pkcs8 0.8.0", + "rand 0.8.5", "ring", "sha2 0.10.8", "svd2utra 0.1.22", From 39e8184b5d31a29f108ae7ecafafb4b948324471 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:45:02 +0800 Subject: [PATCH 017/107] chore: add a comment --- tools/src/swap_writer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs index 91b448a6d..caa4cf45a 100644 --- a/tools/src/swap_writer.rs +++ b/tools/src/swap_writer.rs @@ -54,6 +54,8 @@ impl SwapHeader { 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(&mut self, mut f: T) -> Result where T: Write + Seek, From 9c0ac72aae879b4c6ceb8ac943aa968504870f51 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:45:16 +0800 Subject: [PATCH 018/107] add storage for the swap encryption key This is set to 0 in the image creator, but the keying ceremony should set this to something more secure after the device has been initialized (and likewise, re-encrypt the swap region to match the new, secure key). --- tools/src/tags/swap.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/src/tags/swap.rs b/tools/src/tags/swap.rs index e734858e0..83a6b101b 100644 --- a/tools/src/tags/swap.rs +++ b/tools/src/tags/swap.rs @@ -14,6 +14,10 @@ pub struct Swap { /// Region name (as a type) pub name: XousArgumentCode, + /// 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], + /// Unused _padding: u32, } @@ -21,28 +25,36 @@ pub struct Swap { 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], + _padding: 0, + } } } 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 { 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(&self.key)?; output.write_all(&0u32.to_le_bytes())?; - written += 4 * 4; + written += 4 * 4 + 32; Ok(written) } } From d4eac7325f2d242871cddcd29bab592dc3338e0a Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:23:07 +0800 Subject: [PATCH 019/107] fix ram offsets for precursor swap config --- xtask/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c1d93ce3c..d37a37fbd 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -244,7 +244,7 @@ fn main() -> Result<(), Box> { } 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(); // It is important that this is the first service added, because the swapper *must* be in PID 2 From 89daf48c398afed7966c0f058246d5bbc549e758 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:23:26 +0800 Subject: [PATCH 020/107] the image creator is sensitive to the target for swap configs --- xtask/src/builder.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 52db53892..848bcb541 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -933,7 +933,18 @@ impl Builder { memory_spec: Vec, ) -> Result { 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"); From aff3620bdf206d51b643f5a597ebff7aca8a7c22 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:23:49 +0800 Subject: [PATCH 021/107] make argument start/offset visible so we can patch it up for the swap testing on precursor --- tools/src/xous_arguments.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/src/xous_arguments.rs b/tools/src/xous_arguments.rs index b2cac387c..a2ef8145c 100644 --- a/tools/src/xous_arguments.rs +++ b/tools/src/xous_arguments.rs @@ -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>, } From cf32b4edb578cdae1994a3874d80b539a197af3a Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:24:17 +0800 Subject: [PATCH 022/107] patching routine for memory regions. ended up not being used because main memory is a special case :P --- tools/src/tags/memory.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/src/tags/memory.rs b/tools/src/tags/memory.rs index eedebc059..61754fd75 100644 --- a/tools/src/tags/memory.rs +++ b/tools/src/tags/memory.rs @@ -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 { From 2a68ee098f18c7b520718a10b93f3cb0d13716e8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:24:38 +0800 Subject: [PATCH 023/107] fix main memory patching for swap config on precursor --- tools/src/bin/create-image.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index 52f5a3cf8..5cb254611 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -366,6 +366,8 @@ 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. From 6639e1f2e10fcaadd5e9aa79283cd99f3395d26f Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 22:16:23 +0800 Subject: [PATCH 024/107] add renode configs for swap --- emulation/betrusted-swap.resc | 149 ++++++++++++++++++++++++++++++++++ emulation/xous-swap.resc | 10 +++ 2 files changed, 159 insertions(+) create mode 100644 emulation/betrusted-swap.resc create mode 100644 emulation/xous-swap.resc diff --git a/emulation/betrusted-swap.resc b/emulation/betrusted-swap.resc new file mode 100644 index 000000000..455fd8e26 --- /dev/null +++ b/emulation/betrusted-swap.resc @@ -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 diff --git a/emulation/xous-swap.resc b/emulation/xous-swap.resc new file mode 100644 index 000000000..56a8bd7d2 --- /dev/null +++ b/emulation/xous-swap.resc @@ -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 \ No newline at end of file From eeeb9281c0e0bce1407934f90eca6402585c90e1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 5 Apr 2024 16:44:55 +0800 Subject: [PATCH 025/107] enable debugging in loader --- xtask/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d37a37fbd..5ac92d283 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -247,6 +247,8 @@ fn main() -> Result<(), Box> { builder.set_swap(0x4080_0000, 8 * 1024 * 1024); } builder.target_renode(); + builder.add_loader_feature("debug-print"); + // 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"); From 28d07da7f1b9741e5c34a6b742861e0ec7e2e6be Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 5 Apr 2024 16:45:24 +0800 Subject: [PATCH 026/107] add the flash offset of the swap region in swap args Each platform interprets this differently. On Precursor, it's the offset in FLASH of the swap region. Probably, flags should be added to configure this somehow for each platform, but still figuring a lot of things out. The plan is on Cramium for the swap data to always be at 0 in the external SPI, so, we don't store an offset (field is ignored). --- tools/src/tags/swap.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/src/tags/swap.rs b/tools/src/tags/swap.rs index 83a6b101b..ff313697b 100644 --- a/tools/src/tags/swap.rs +++ b/tools/src/tags/swap.rs @@ -5,10 +5,10 @@ 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) @@ -18,8 +18,8 @@ pub struct Swap { /// by the device upon completion of the keying ceremony. pub key: [u8; 32], - /// Unused - _padding: u32, + /// Starting offset of swap in FLASH + pub offset_flash: u32, } impl fmt::Display for Swap { @@ -37,7 +37,8 @@ impl Swap { size, name: u32::from_le_bytes(*b"Swap") as XousArgumentCode, key: [0u8; 32], - _padding: 0, + // location for swap offset in FLASH (for precursor) + offset_flash: 0x21200000, } } } @@ -53,7 +54,7 @@ impl XousArgument for Swap { output.write_all(&self.size.to_le_bytes())?; output.write_all(&self.name.to_le_bytes())?; output.write_all(&self.key)?; - output.write_all(&0u32.to_le_bytes())?; + output.write_all(&self.offset_flash.to_le_bytes())?; written += 4 * 4 + 32; Ok(written) } From a732eed679b1a19eadc5b85f79fe12b3e8d54f23 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:27:24 +0800 Subject: [PATCH 027/107] make kernel aware of the IniS type this should now introduce a crash in the kernel when an IniS process is in the image set; previously we didn't try to schedule and run them. --- kernel/src/services.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 09f25d3fe..6d1e96ce0 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -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; From cdbd5dc0f41b2ad054870cbe6c5d4c5d43dcf90f Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:28:04 +0800 Subject: [PATCH 028/107] add structure to differentiate image type we now have three image types: inie, inif, inis; migrate away from a simple "xip" flag to something that can differentiate between all three options. --- loader/src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 71f71035b..d0c60aa3f 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -59,6 +59,16 @@ pub struct PageTable { entries: [usize; PAGE_SIZE / WORD_SIZE], } +#[derive(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. @@ -344,7 +354,10 @@ pub fn read_initial_config(cfg: &mut BootConfig) { assert!(!kernel_seen, "kernel appears twice"); assert!(tag.size as usize == mem::size_of::(), "invalid XKrn size"); kernel_seen = true; - } else if tag.name == u32::from_le_bytes(*b"IniE") || tag.name == u32::from_le_bytes(*b"IniF") { + } else if tag.name == u32::from_le_bytes(*b"IniE") + || tag.name == u32::from_le_bytes(*b"IniF") + || tag.name == u32::from_le_bytes(*b"IniS") + { assert!(tag.size >= 4, "invalid Init size"); init_seen = true; cfg.init_process_count += 1; @@ -467,6 +480,11 @@ fn check_load(cfg: &mut BootConfig) { println!("\n\nChecking IniF region"); inif.check(cfg, inif.load_offset as usize, pid, true); 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, true); + pid += 1; } } } From c37349778ed24d9d88b5aa2e823ab3813fcfb26d Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:28:53 +0800 Subject: [PATCH 029/107] put todo! placeholders for minielf handling of inis --- loader/src/minielf.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 4defe972f..50f8847aa 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -93,7 +93,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; @@ -104,16 +104,20 @@ impl MiniElf { 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 +235,7 @@ impl MiniElf { panic!("init section addresses are not strictly increasing"); } - if copy_to_ram || !xip { + if copy_to_ram || ini_type == IniType::IniE { let mut this_page = section.virt as usize & !(PAGE_SIZE - 1); let mut bytes_to_copy = section.len(); @@ -350,6 +354,8 @@ impl MiniElf { } // --- map FLASH pages to virtual memory --- + // TODO: handle mapping of swap-located pages to virtual memory + todo!("Handle swap-located pages mapping into 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); From c354b75d97b37a74e5dc90f6ee8bdfc4a42ab76e Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:29:23 +0800 Subject: [PATCH 030/107] put placeholders to handle inis in phase1 --- loader/src/phase1.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 00b428af3..d19a60a27 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -147,6 +147,7 @@ pub fn copy_args(cfg: &mut BootConfig) { enum TagType { IniE, IniF, + IniS, XKrn, Other, } @@ -156,6 +157,8 @@ impl From 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 +172,7 @@ impl TagType { match self { TagType::IniE => "IniE", TagType::IniF => "IniF", + TagType::IniS => "IniS", TagType::XKrn => "XKrn", TagType::Other => "Other", } @@ -314,6 +318,17 @@ fn copy_processes(cfg: &mut BootConfig) { } println!("Done with sections"); } + TagType::IniS => { + // IniS does not necessarily exist in linear memory space. + + // TODO: access IniS image and figure out what regions are "write" and allocate/copy + // those into RAM. We do this because there isn't a 1:1 mapping of these to the + // images on disk which creates a problem for the swapper. + + // Then, for regions that *are* aligned, allocate them into the swapper's + // memory space, and copy the data into swap. + todo!("implement inis handling"); + } TagType::XKrn => { let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; From 8bae95ef727072890ee39a8139cdc8371c07a40e Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Apr 2024 15:29:38 +0800 Subject: [PATCH 031/107] put placeholders for inis handling, phase 2 --- loader/src/phase2.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 83f7808bd..f5f655768 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -80,17 +80,24 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { 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") { + 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) }; From d8f3fc96d6e1070c328b5c65f45bd7cf05ed5c04 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:32:19 +0800 Subject: [PATCH 032/107] specify swap file name --- xtask/src/builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 98ddfd5a9..52db53892 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -971,9 +971,16 @@ impl Builder { } else { String::new() }; + let mut swap_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()) } if memory_spec.len() == 1 { From 0aecb4c1d3a49a75a2f50aeb2215e41a4f092890 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:32:45 +0800 Subject: [PATCH 033/107] add the swap writer This will take the buffer that is generated from a xous argument and encrypts it to the prescribed "detached swap" format. --- tools/src/swap_writer.rs | 103 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 tools/src/swap_writer.rs diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs new file mode 100644 index 000000000..91b448a6d --- /dev/null +++ b/tools/src/swap_writer.rs @@ -0,0 +1,103 @@ +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>, +} + +struct SwapHeader { + aad: Vec, + 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::::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 AAD + 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 + + // serialize the MAC data offset + data.write(&(self.mac_offset as u32).to_le_bytes())?; // LE because this is a size field + + 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()) } } + + pub fn encrypt_to(&mut self, mut f: T) -> Result + where + T: Write + Seek, + { + let header = SwapHeader::new(self.buffer.get_ref().len()); + let mut macs = Vec::::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).to_be_bytes()); + nonce_vec.extend_from_slice(&header.partial_nonce.to_be_bytes()); + let nonce = Nonce::from_slice(&nonce_vec); + let enc = cipher + .encrypt(nonce, Payload { aad: &header.aad, msg: &padded_block }) + .expect("couldn't encrypt block"); + assert!(enc.len() == 0x1010); + 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 { 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 { self.buffer.seek(pos) } +} From eb1cb4a2530c26d2185ce5e0493687f22eea87fb Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:33:29 +0800 Subject: [PATCH 034/107] add the swap_writer module to the library --- tools/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/src/lib.rs b/tools/src/lib.rs index 87f18cc2b..6a2efd11f 100644 --- a/tools/src/lib.rs +++ b/tools/src/lib.rs @@ -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; From a662f5edcb5b5b58d65f5b33a5054af7459a3873 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:34:00 +0800 Subject: [PATCH 035/107] rework create-image to put inis objects into the detached swap region --- tools/src/bin/create-image.rs | 52 ++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index 631942e4b..52f5a3cf8 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -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,13 @@ 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("debug") .short("d") @@ -342,6 +350,7 @@ fn main() { } let mut args = XousArguments::new(ram_config.offset, ram_config.size, ram_config.name); + let mut swap_args: Option = None; if !ram_config.regions.is_empty() { if let Some(s) = swap { @@ -360,6 +369,7 @@ fn main() { } // 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 +422,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(ref mut sargs) = 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 +466,18 @@ 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); + } 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); From 6dbea466fbc0f2982bc2aaa48400ab43d4fff1de Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:34:23 +0800 Subject: [PATCH 036/107] add cryptographic libraries to the image creator manifest --- tools/Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 8600cf2b3..3c293dc22 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -24,6 +24,9 @@ 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" +aead = { version = "0.5.2", features = ["alloc"] } +aes-gcm-siv = "0.11.1" [[bin]] name = "copy-object" From b72a949930460053e70e41821e0be3c61ac014df Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:34:45 +0800 Subject: [PATCH 037/107] absorb new dependencies --- Cargo.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ff5a885e6..bd18acde1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] @@ -5132,6 +5133,8 @@ dependencies = [ name = "tools" version = "0.1.0" dependencies = [ + "aead", + "aes-gcm-siv", "base64 0.20.0", "bitflags 1.3.2", "clap 2.34.0", @@ -5142,6 +5145,7 @@ dependencies = [ "log", "pem", "pkcs8 0.8.0", + "rand 0.8.5", "ring", "sha2 0.10.8", "svd2utra 0.1.22", From bac8e4ff4d7cf67d603e76a3a2400e05558f57de Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:45:02 +0800 Subject: [PATCH 038/107] chore: add a comment --- tools/src/swap_writer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs index 91b448a6d..caa4cf45a 100644 --- a/tools/src/swap_writer.rs +++ b/tools/src/swap_writer.rs @@ -54,6 +54,8 @@ impl SwapHeader { 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(&mut self, mut f: T) -> Result where T: Write + Seek, From 798566e6d287f6b752b9de599d47ba1cdbd7b3a6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 18:45:16 +0800 Subject: [PATCH 039/107] add storage for the swap encryption key This is set to 0 in the image creator, but the keying ceremony should set this to something more secure after the device has been initialized (and likewise, re-encrypt the swap region to match the new, secure key). --- tools/src/tags/swap.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tools/src/tags/swap.rs b/tools/src/tags/swap.rs index e734858e0..83a6b101b 100644 --- a/tools/src/tags/swap.rs +++ b/tools/src/tags/swap.rs @@ -14,6 +14,10 @@ pub struct Swap { /// Region name (as a type) pub name: XousArgumentCode, + /// 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], + /// Unused _padding: u32, } @@ -21,28 +25,36 @@ pub struct Swap { 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], + _padding: 0, + } } } 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 { 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(&self.key)?; output.write_all(&0u32.to_le_bytes())?; - written += 4 * 4; + written += 4 * 4 + 32; Ok(written) } } From 98d31c52740cba4cec7b17db20e01bcbf195a225 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:23:07 +0800 Subject: [PATCH 040/107] fix ram offsets for precursor swap config --- xtask/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c1d93ce3c..d37a37fbd 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -244,7 +244,7 @@ fn main() -> Result<(), Box> { } 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(); // It is important that this is the first service added, because the swapper *must* be in PID 2 From 78cd15e590fbdbc79d4a660a21459f19090f83eb Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:23:26 +0800 Subject: [PATCH 041/107] the image creator is sensitive to the target for swap configs --- xtask/src/builder.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 52db53892..848bcb541 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -933,7 +933,18 @@ impl Builder { memory_spec: Vec, ) -> Result { 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"); From b06dd72e7d0789d6b94170f8cfaaebbd2d3e6706 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:23:49 +0800 Subject: [PATCH 042/107] make argument start/offset visible so we can patch it up for the swap testing on precursor --- tools/src/xous_arguments.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/src/xous_arguments.rs b/tools/src/xous_arguments.rs index b2cac387c..a2ef8145c 100644 --- a/tools/src/xous_arguments.rs +++ b/tools/src/xous_arguments.rs @@ -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>, } From 8c219f8fb1c369d6d92fdb62c44b1ea0c2207793 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:24:17 +0800 Subject: [PATCH 043/107] patching routine for memory regions. ended up not being used because main memory is a special case :P --- tools/src/tags/memory.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/src/tags/memory.rs b/tools/src/tags/memory.rs index eedebc059..61754fd75 100644 --- a/tools/src/tags/memory.rs +++ b/tools/src/tags/memory.rs @@ -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 { From 50fa69a0621be55867305a8ac46421a892d4ac5c Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 19:24:38 +0800 Subject: [PATCH 044/107] fix main memory patching for swap config on precursor --- tools/src/bin/create-image.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index 52f5a3cf8..5cb254611 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -366,6 +366,8 @@ 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. From 8a7010a6c5f155af7f25eca82e4fe7a6861cbc43 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 4 Apr 2024 22:16:23 +0800 Subject: [PATCH 045/107] add renode configs for swap --- emulation/betrusted-swap.resc | 149 ++++++++++++++++++++++++++++++++++ emulation/xous-swap.resc | 10 +++ 2 files changed, 159 insertions(+) create mode 100644 emulation/betrusted-swap.resc create mode 100644 emulation/xous-swap.resc diff --git a/emulation/betrusted-swap.resc b/emulation/betrusted-swap.resc new file mode 100644 index 000000000..455fd8e26 --- /dev/null +++ b/emulation/betrusted-swap.resc @@ -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 diff --git a/emulation/xous-swap.resc b/emulation/xous-swap.resc new file mode 100644 index 000000000..56a8bd7d2 --- /dev/null +++ b/emulation/xous-swap.resc @@ -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 \ No newline at end of file From 588e7aaf0a5ce3cf17d954358b6e4e12f545ec2f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:54:34 +0800 Subject: [PATCH 046/107] add spi memory drivers to udma library totally untested, but, at least structurally present so we have a chance that our memory structures and APIs can work. --- libs/cramium-hal/src/udma.rs | 294 ++++++++++++++++++++++++++++++++++- 1 file changed, 286 insertions(+), 8 deletions(-) diff --git a/libs/cramium-hal/src/udma.rs b/libs/cramium-hal/src/udma.rs index 2aa3369e2..5c7a31602 100644 --- a/libs/cramium-hal/src/udma.rs +++ b/libs/cramium-hal/src/udma.rs @@ -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 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, max_tx_len_bytes: usize, max_rx_len_bytes: usize, + dummy_cycles: Option, ) -> Option { + // 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, + max_tx_len_bytes: usize, + max_rx_len_bytes: usize, + dummy_cycles: Option, + 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::() @@ -950,9 +1034,9 @@ 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 + // determine the valid length of data we could send + let tx_len = if (total_words - words_sent) >= self.tx_buf_len_bytes as usize { + self.tx_buf_len_bytes } else { total_words - words_sent }; @@ -962,7 +1046,7 @@ impl Spim { 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::() { @@ -1018,4 +1102,198 @@ impl Spim { pub fn rx_data(&mut self, _rx_data: &mut [T], _cs: Option) { 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::()[..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::()[..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::()[..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::()[..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::()[..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); + } + } } From d9b29c2d06529f92af1160fbd947f1b1bc0e7037 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:55:39 +0800 Subject: [PATCH 047/107] move the MAC (i.e. tag) above the variable length AAD field. --- tools/src/swap_writer.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs index caa4cf45a..86af52418 100644 --- a/tools/src/swap_writer.rs +++ b/tools/src/swap_writer.rs @@ -38,13 +38,13 @@ impl SwapHeader { // 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 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 - // serialize the MAC data offset - data.write(&(self.mac_offset as u32).to_le_bytes())?; // LE because this is a size field - output[..data.position() as usize].copy_from_slice(&data.into_inner()); Ok(output) From 7547d7c952b10c87851ef509fc9dd4a19dc89be7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:56:03 +0800 Subject: [PATCH 048/107] remove aead from dependencies (already part of aes-gcm-siv) --- tools/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 3c293dc22..26171a578 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -25,7 +25,6 @@ sha2 = { version = "0.10.8" } pkcs8 = { version = "0.8.0", features = ["pem"] } base64 = "0.20.0" rand = "0.8.5" -aead = { version = "0.5.2", features = ["alloc"] } aes-gcm-siv = "0.11.1" [[bin]] From 42063c222e7c79003f5bec879245bd8fa3689a81 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:56:25 +0800 Subject: [PATCH 049/107] add explicit build flags to swap target --- xtask/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 5ac92d283..bc6d70f7b 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -247,7 +247,11 @@ fn main() -> Result<(), Box> { 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); From fbce3f774c3b2c9214f2af905f2a2d01315ccb10 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:57:38 +0800 Subject: [PATCH 050/107] update cramium soc target to latest svd spec --- utralib/src/generated/cramium_soc.rs | 202 +++++++++++++++------------ 1 file changed, 110 insertions(+), 92 deletions(-) diff --git a/utralib/src/generated/cramium_soc.rs b/utralib/src/generated/cramium_soc.rs index 103ca6fcb..6cc895ffa 100644 --- a/utralib/src/generated/cramium_soc.rs +++ b/utralib/src/generated/cramium_soc.rs @@ -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); From e84fd34c9adddbb8df73a07adef1495d43b8ad56 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:57:58 +0800 Subject: [PATCH 051/107] aead dependency changes in cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index bd18acde1..b9a0b7346 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2789,6 +2789,7 @@ 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)", @@ -5133,7 +5134,6 @@ dependencies = [ name = "tools" version = "0.1.0" dependencies = [ - "aead", "aes-gcm-siv", "base64 0.20.0", "bitflags 1.3.2", From dddbc3c16376eca7f8911d6a55a188107aa91389 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:58:20 +0800 Subject: [PATCH 052/107] add aes-gcm-siv dependency to loader --- loader/Cargo.toml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 6067bd218..fe5ab35cd 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -29,6 +29,9 @@ 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 } [dependencies.com_rs] git = "https://github.com/betrusted-io/com_rs" @@ -77,5 +80,6 @@ secboot = [] simulation-only = [] board-bringup = [] #default = ["debug-print"] -resume = [] # suspend/resume pathway code +swap = ["aes-gcm-siv"] +resume = [] # suspend/resume pathway code default = [] From 32857a70b04ae209cdffa3f52ab0c2cb90f9cccc Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 22:59:43 +0800 Subject: [PATCH 053/107] add platform-dependent drivers for swap precursor does direct memory mapping cramium uses a qspi interface --- loader/src/platform/cramium.rs | 4 + loader/src/platform/cramium/cramium.rs | 11 +- loader/src/platform/cramium/swap.rs | 272 +++++++++++++++++++++++++ loader/src/platform/precursor.rs | 5 + loader/src/platform/precursor/swap.rs | 68 +++++++ 5 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 loader/src/platform/cramium/swap.rs create mode 100644 loader/src/platform/precursor/swap.rs diff --git a/loader/src/platform/cramium.rs b/loader/src/platform/cramium.rs index a900031bb..99a38c6b5 100644 --- a/loader/src/platform/cramium.rs +++ b/loader/src/platform/cramium.rs @@ -1,2 +1,6 @@ pub mod cramium; pub use cramium::*; +#[cfg(feature = "swap")] +pub mod swap; +#[cfg(feature = "swap")] +pub use swap::*; diff --git a/loader/src/platform/cramium/cramium.rs b/loader/src/platform/cramium/cramium.rs index d5fe007fb..f10390d96 100644 --- a/loader/src/platform/cramium/cramium.rs +++ b/loader/src/platform/cramium/cramium.rs @@ -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) diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs new file mode 100644 index 000000000..8f72bc01b --- /dev/null +++ b/loader/src/platform/cramium/swap.rs @@ -0,0 +1,272 @@ +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::udma::*; + +use crate::bootconfig::BootConfig; +use crate::platform::{SPIM_FLASH_IFRAM_ADDR, SPIM_RAM_IFRAM_ADDR}; +use crate::println; +use crate::swap::*; + +/// 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, + cipher: Aes256GcmSiv, + flash_spim: Spim, + ram_spim: Spim, + iox: Iox, + udma_global: GlobalConfig, + buf: RawPage, +} + +fn setup_port( + iox: &mut Iox, + port: IoxPort, + pin: u8, + function: Option, + direction: Option, + drive: Option, + slow_slew: Option, + schmitt: Option, + pullup: Option, +) { + 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 { + if let Some(swap) = cfg.swap { + // sanity check this structure + assert_eq!(core::mem::size_of::(), 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); + + // 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, + cipher: Aes256GcmSiv::new((&swap.key).into()), + flash_spim, + ram_spim, + iox, + udma_global, + 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_page_at(&mut self, offset: usize) -> &[u8] { + assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); + self.flash_spim.mem_read((self.image_start + offset) as u32, &mut self.buf.data); + let mut nonce = [0u8; size_of::()]; + 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::()]; + // 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::()) as u32, &mut tag); + match self.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 in swap: {:?}", e), + } + } +} diff --git a/loader/src/platform/precursor.rs b/loader/src/platform/precursor.rs index dd1c1980c..b4d9915d5 100644 --- a/loader/src/platform/precursor.rs +++ b/loader/src/platform/precursor.rs @@ -1,3 +1,8 @@ +#[cfg(feature = "swap")] +pub mod swap; +#[cfg(feature = "swap")] +pub use swap::*; + pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_EXT_MEM_LEN; pub const RAM_BASE: usize = utralib::generated::HW_SRAM_EXT_MEM; diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs new file mode 100644 index 000000000..81423e184 --- /dev/null +++ b/loader/src/platform/precursor/swap.rs @@ -0,0 +1,68 @@ +use core::mem::size_of; + +use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; + +use crate::bootconfig::BootConfig; +use crate::swap::*; + +pub struct SwapHal { + data_area: &'static [u8], + mac_area: &'static [u8], + partial_nonce: [u8; 8], + aad: &'static [u8], + cipher: Aes256GcmSiv, + buf: RawPage, +} + +impl SwapHal { + pub fn new(cfg: &BootConfig) -> Option { + if let Some(swap) = cfg.swap { + // sanity check this structure + assert_eq!(core::mem::size_of::(), 4096); + // 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) }; + let swap_len = ssh.mac_offset as usize - (swap.flash_offset as usize + 4096); + let mut hal = SwapHal { + // safety: the swap raw array is guaranteed to be correctly aligned by the image maker + 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 + mac_area: unsafe { + core::slice::from_raw_parts( + ssh.mac_offset as *const u8, + (swap_len / 4096) * size_of::(), + ) + }, + partial_nonce: [0u8; 8], + aad: &ssh.aad[..ssh.aad_len as usize], + cipher: Aes256GcmSiv::new((&swap.key).into()), + buf: RawPage { data: [0u8; 4096] }, + }; + hal.partial_nonce.copy_from_slice(&ssh.parital_nonce); + Some(hal) + } else { + None + } + } + + pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { + assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); + self.buf.data.copy_from_slice(&self.data_area[offset..offset + 4096]); + let mut nonce = [0u8; size_of::()]; + nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes()); + nonce[4..].copy_from_slice(&self.partial_nonce); + match self.cipher.decrypt_in_place_detached( + Nonce::from_slice(&nonce), + self.aad, + &mut self.buf.data, + (&self.mac_area + [(offset / 4096) * size_of::()..(offset / 4096) * size_of::() + size_of::()]) + .into(), + ) { + Ok(_) => &self.buf.data, + Err(e) => panic!("Decryption error in swap: {:?}", e), + } + } +} From 903b2219db8b3a5f3a4ba3d7cd18635321d49978 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 23:01:04 +0800 Subject: [PATCH 054/107] gate loader features behind flag --- loader/src/main.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index d0c60aa3f..7dcb99df4 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -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; @@ -122,6 +126,9 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - let mut cfg = BootConfig { base_addr: args.base as *const usize, args, ..Default::default() }; read_initial_config(&mut cfg); + #[cfg(feature = "swap")] + let mut swap = SwapHal::new(&cfg); + // 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); @@ -354,14 +361,16 @@ pub fn read_initial_config(cfg: &mut BootConfig) { assert!(!kernel_seen, "kernel appears twice"); assert!(tag.size as usize == mem::size_of::(), "invalid XKrn size"); kernel_seen = true; - } else if tag.name == u32::from_le_bytes(*b"IniE") - || tag.name == u32::from_le_bytes(*b"IniF") - || tag.name == u32::from_le_bytes(*b"IniS") - { + } else if tag.name == u32::from_le_bytes(*b"IniE") || tag.name == u32::from_le_bytes(*b"IniF") { assert!(tag.size >= 4, "invalid Init size"); 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"); From 4bb7c8764661866b0956d8e77984ae4714427232 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 23:01:33 +0800 Subject: [PATCH 055/107] gate loader todo behind feature flag --- loader/src/minielf.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 50f8847aa..0d61a502c 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -355,6 +355,7 @@ impl MiniElf { // --- map FLASH pages to virtual memory --- // TODO: handle mapping of swap-located pages to virtual memory + #[cfg(feature = "swap")] todo!("Handle swap-located pages mapping into virtual memory"); while pages_to_map > 0 { let map_phys_addr = (image_phys_base + section_map_phys_offset) & !(PAGE_SIZE - 1); From 71336ebf3ed3c9c5c563c53489aab3c2dc47f27d Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Apr 2024 23:04:03 +0800 Subject: [PATCH 056/107] start to sketch in bits and pieces of the swap HAL this is far from complete but needed to check in for the build check to pass --- loader/src/swap.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 loader/src/swap.rs diff --git a/loader/src/swap.rs b/loader/src/swap.rs new file mode 100644 index 000000000..33d6cb544 --- /dev/null +++ b/loader/src/swap.rs @@ -0,0 +1,70 @@ +use crate::{bootconfig::BootConfig, PageTable}; + +#[repr(C)] +pub struct SwapDescriptor { + pub ram_offset: u32, + pub ram_size: u32, + pub name: u32, + pub key: [u8; 32], + pub flash_offset: u32, +} + +#[repr(C)] +pub struct SwapSourceHeader { + pub version: u32, + pub parital_nonce: [u8; 8], + pub mac_offset: u32, + pub aad_len: u32, + // consumes up to the remainder of the page + pub aad: [u8; 4076], +} + +/// An aligned, raw-page structure +#[repr(C, align(4096))] +pub struct RawPage { + pub data: [u8; 4096], +} + +/// Mapping pages to swap. +/// +/// Given a virtual address and PID, map to a physical offset in swap. +/* +bitfield! { + #[derive(Copy, Clone, Eq)] + impl Debug; + pub u32, +} +pub struct SwapPte {} +*/ + +const FLG_VALID: usize = 0x1; + +/// 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 +pub fn set_l1_pte(from_va: usize, to_pa: usize, root_pt: &mut PageTable) { + let index = from_va >> 22; + root_pt.entries[index] = ((to_pa & 0xFFFF_FC00) >> 2) // top 2 bits of PA are not used, we don't do 34-bit PA featured by Sv32 + | FLG_VALID; +} + +/// Virtual address fields: +/// 31 22 21 12 11 0 +/// | L1 index | L2 index | LSB of addr | +/// +/// 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 +pub fn set_l2_pte(from_va: usize, to_pa: usize, l2_pt: &mut PageTable, flags: usize) { + let index = (from_va >> 12) & 0x3_FF; + l2_pt.entries[index] = ((to_pa & 0xFFFF_FC00) >> 2) // top 2 bits of PA are not used, we don't do 34-bit PA featured by Sv32 + | flags + | FLG_VALID; +} From 3079da3a577d408921a10f5d3c2d0ede7fd9a759 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 00:38:34 +0800 Subject: [PATCH 057/107] add debug provisions to encryption; limit AAD to 64 bytes --- tools/src/swap_writer.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs index 86af52418..8d6e236ac 100644 --- a/tools/src/swap_writer.rs +++ b/tools/src/swap_writer.rs @@ -42,6 +42,7 @@ impl SwapHeader { 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 @@ -82,10 +83,14 @@ impl SwapWriter { nonce_vec.extend_from_slice(&(offset as u32).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..]); } From 8f94ecaf6c4bb46f1b76a9b90824135f5ce0a9f5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 00:39:05 +0800 Subject: [PATCH 058/107] limit AAD to 64 bytes --- loader/src/swap.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/loader/src/swap.rs b/loader/src/swap.rs index 33d6cb544..a3cbf6103 100644 --- a/loader/src/swap.rs +++ b/loader/src/swap.rs @@ -9,14 +9,15 @@ pub struct SwapDescriptor { 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, - // consumes up to the remainder of the page - pub aad: [u8; 4076], + // aad is limited to 64 bytes! + pub aad: [u8; 64], } /// An aligned, raw-page structure From c7e1f22cfa4cc3fddbde30ae5e3829671d4740c4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 00:39:34 +0800 Subject: [PATCH 059/107] fix pointer issue in resolving swap config also a bunch of latent debug in case we need to muck around again --- loader/src/platform/precursor/swap.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index 81423e184..e5252fbc5 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -3,6 +3,7 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; use crate::bootconfig::BootConfig; +use crate::println; use crate::swap::*; pub struct SwapHal { @@ -17,12 +18,11 @@ pub struct SwapHal { impl SwapHal { pub fn new(cfg: &BootConfig) -> Option { if let Some(swap) = cfg.swap { - // sanity check this structure - assert_eq!(core::mem::size_of::(), 4096); // 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) }; - let swap_len = ssh.mac_offset as usize - (swap.flash_offset as usize + 4096); + let ssh: &SwapSourceHeader = + unsafe { (swap.flash_offset as *const SwapSourceHeader).as_ref().unwrap() }; + let swap_len = ssh.mac_offset as usize; let mut hal = SwapHal { // safety: the swap raw array is guaranteed to be correctly aligned by the image maker data_area: unsafe { @@ -31,7 +31,7 @@ impl SwapHal { // safety: the mac raw array is guaranteed to be correctly aligned by the image maker mac_area: unsafe { core::slice::from_raw_parts( - ssh.mac_offset as *const u8, + (ssh.mac_offset + swap.flash_offset + 4096) as *const u8, (swap_len / 4096) * size_of::(), ) }, @@ -49,17 +49,23 @@ impl SwapHal { pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); + // println!("data area: {:x?}", &self.data_area[..4]); + // println!("offset: {:x}", offset); self.buf.data.copy_from_slice(&self.data_area[offset..offset + 4096]); let mut nonce = [0u8; size_of::()]; nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes()); nonce[4..].copy_from_slice(&self.partial_nonce); + let tag = &self.mac_area + [(offset / 4096) * size_of::()..(offset / 4096) * size_of::() + size_of::()]; + // println!("nonce: {:x?}", nonce); + // println!("tag: {:x?}", tag); + // println!("aad: {:x?}", self.aad); + // println!("data: {:x?}", &self.buf.data[0..32]); match self.cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), self.aad, &mut self.buf.data, - (&self.mac_area - [(offset / 4096) * size_of::()..(offset / 4096) * size_of::() + size_of::()]) - .into(), + tag.into(), ) { Ok(_) => &self.buf.data, Err(e) => panic!("Decryption error in swap: {:?}", e), From f86d63aa41fec485dd562f2b562568136a74f69f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 00:40:33 +0800 Subject: [PATCH 060/107] add tracking for swap-specific data structures in bootconfig --- loader/src/bootconfig.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index 847372055..dd07a9474 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -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,19 @@ pub struct BootConfig { /// The number of 'Init' tags discovered pub init_process_count: usize, + + /// Swap HAL + #[cfg(feature = "swap")] + pub swap_hal: Option, + + /// Swap descriptor + #[cfg(feature = "swap")] + pub swap: Option<&'static SwapDescriptor>, + + /// Swap root page table. Array of root page tables, one per process. + /// Kernel does not get swap, so the first entry belongs to PID 2. + #[cfg(feature = "swap")] + pub swap_page_table: &'static mut [&'static mut PageTable], } impl Default for BootConfig { @@ -68,6 +83,12 @@ impl Default for BootConfig { runtime_page_tracker: Default::default(), init_process_count: 0, processes: Default::default(), + #[cfg(feature = "swap")] + swap_hal: None, + #[cfg(feature = "swap")] + swap: None, + #[cfg(feature = "swap")] + swap_page_table: Default::default(), } } } From c53515fc6364cf9dfe2d2b4a106376b3d5803635 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 00:41:18 +0800 Subject: [PATCH 061/107] coalesce swap hal into boot config object; read initial swap args this worked, so it validates the encryption/decryption of the swap at least! --- loader/src/main.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 7dcb99df4..7a803ee93 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -127,7 +127,10 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - read_initial_config(&mut cfg); #[cfg(feature = "swap")] - let mut swap = SwapHal::new(&cfg); + { + cfg.swap_hal = SwapHal::new(&cfg); + read_swap_config(&mut cfg); + } // check to see if we are recovering from a clean suspend or not #[cfg(feature = "resume")] @@ -377,6 +380,26 @@ pub fn read_initial_config(cfg: &mut BootConfig) { 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 the first page of swap. + let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_page_at(0); + 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 + 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. From 35082c468896d7ddab434f6000486386e7ec2b8e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 14:28:05 +0800 Subject: [PATCH 062/107] add crc handling to loader for patching the argument structure --- loader/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/loader/Cargo.toml b/loader/Cargo.toml index fe5ab35cd..4a9783a08 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -32,6 +32,7 @@ xous-pio = { path = "../libs/xous-pio", optional = true, features = [ aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ "aes", ], optional = true } +crc = { version = "1.8.1", default-features = false } [dependencies.com_rs] git = "https://github.com/betrusted-io/com_rs" From 999cb320ede55be906a5f5aaa22a47b43eafb945 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 14:28:31 +0800 Subject: [PATCH 063/107] add a function to nab the decrypt buffer HIGHLY unsafe, but saves us a decryption operation as we interleave between the various states of the loader. --- loader/src/platform/cramium/swap.rs | 5 +++++ loader/src/platform/precursor/swap.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index 8f72bc01b..bb0a79a70 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -269,4 +269,9 @@ impl SwapHal { Err(e) => panic!("Decryption error in swap: {:?}", 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 } } diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index e5252fbc5..82a5de6e9 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -71,4 +71,9 @@ impl SwapHal { Err(e) => panic!("Decryption error in swap: {:?}", 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 } } From 7602ec76709c5e41a3178e1f8c62069d9e40710f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 14:29:05 +0800 Subject: [PATCH 064/107] reflect crc crate --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index b9a0b7346..1d4083caf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2794,6 +2794,7 @@ dependencies = [ "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", From f2a8603e55290d36e1034079f2bc2ec381271611 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 14:29:25 +0800 Subject: [PATCH 065/107] merge encrypted swap argument structure into kernel arguments --- loader/src/phase1.rs | 112 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 8 deletions(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index d19a60a27..80b7922c4 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -102,7 +102,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::() - 1) & !(mem::size_of::() - 1); @@ -121,6 +121,7 @@ pub fn allocate_regions(cfg: &mut BootConfig) { 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::(); // Allocate the process table cfg.init_size += table_size; @@ -132,6 +133,97 @@ pub fn allocate_processes(cfg: &mut BootConfig) { unsafe { slice::from_raw_parts_mut(processes as *mut InitialProcess, process_count as usize) }; } +#[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. + + // 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) + println!("length of swap arg: {}", swap_xarg.data[0] as usize); + let final_len = cfg.args.size() + swap_xarg.data[0] as usize - 7; + cfg.init_size += final_len; + 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(); + println!("orig arg size: {}, new size: {}", cfg.args.size(), final_len); + + // 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 + let arg_slice = unsafe { + core::slice::from_raw_parts( + &a as *const KernelArgument as *const usize, + a.size as usize / core::mem::size_of::() + 2, + ) + }; + println!( + "copying arg: 0x{:x}, size: {}. index: {}, len: {}", + a.name, + a.size, + arg_index, + arg_slice.len() + ); + merged_arg_slice[arg_index..arg_index + arg_slice.len()].copy_from_slice(arg_slice); + arg_index += arg_slice.len(); + } + + // 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::(), + ) + }; + digest.write(&xarg_data); + // patch the CRC + let mut merged_arg_slice_u8 = unsafe { + core::slice::from_raw_parts_mut( + runtime_arg_buffer as *mut u8, + final_len * core::mem::size_of::(), + ) + }; + merged_arg_slice_u8[4..6].copy_from_slice(&digest.sum16().to_le_bytes()); +} + +#[cfg(not(feature = "swap"))] pub fn copy_args(cfg: &mut BootConfig) { // Copy the args list to target RAM cfg.init_size += cfg.args.size(); @@ -319,15 +411,19 @@ fn copy_processes(cfg: &mut BootConfig) { println!("Done with sections"); } TagType::IniS => { - // IniS does not necessarily exist in linear memory space. + // 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. - // TODO: access IniS image and figure out what regions are "write" and allocate/copy - // those into RAM. We do this because there isn't a 1:1 mapping of these to the - // images on disk which creates a problem for the swapper. + // TODO: access IniS image and figure out what regions are "write" and allocate/copy + // those into RAM. We do this because there isn't a 1:1 mapping of these to the + // images on disk which creates a problem for the swapper. - // Then, for regions that *are* aligned, allocate them into the swapper's - // memory space, and copy the data into swap. - todo!("implement inis handling"); + // Then, for regions that *are* aligned, allocate them into the swapper's + // memory space, and copy the data into swap. + todo!("implement inis handling"); + } } TagType::XKrn => { let prog = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; From 49f3570d231ee53341999bb286847f79315f9cf9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 18:56:40 +0800 Subject: [PATCH 066/107] make the test-swapper crate kick out NOCOPY data makes the test case more comprehensive --- services/test-swapper/src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/test-swapper/src/main.rs b/services/test-swapper/src/main.rs index e6c31a2fe..ed598ec0c 100644 --- a/services/test-swapper/src/main.rs +++ b/services/test-swapper/src/main.rs @@ -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)); } From 417fa16a22f3a8ea0650d5ba7d6b3b78f5ce752b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 18:57:23 +0800 Subject: [PATCH 067/107] add rand_chacha as a dependency for the loader this is needed in the cramium-soc case because the raw TRNG does not have a whitener, it should not be used directly to generate keys. --- loader/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 4a9783a08..11daf46a8 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -33,6 +33,7 @@ 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" @@ -65,6 +66,7 @@ cramium-soc = [ "cramium-hal", "xous-pio", "xous-pl230", + "rand_chacha", ] cramium-fpga = [ "utralib/cramium-fpga", From 884b4d372df03bf3b464453d78df423c36c5e0e4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 18:58:11 +0800 Subject: [PATCH 068/107] put rand_chacha into the lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 1d4083caf..e6d61fb00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2798,6 +2798,7 @@ dependencies = [ "curve25519-dalek-loader", "ed25519-dalek-loader", "lazy_static", + "rand_chacha 0.3.1", "sha2-loader", "utralib 0.1.24", "xous-pio", From c07f33a169c757da594aac0c136e4e38dac9d8c7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 18:58:31 +0800 Subject: [PATCH 069/107] track the current free page in swap ram --- loader/src/bootconfig.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index dd07a9474..3e0428125 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -66,6 +66,10 @@ pub struct BootConfig { /// Kernel does not get swap, so the first entry belongs to PID 2. #[cfg(feature = "swap")] pub swap_page_table: &'static mut [&'static mut PageTable], + + /// Offset of the current free page in swap + #[cfg(feature = "swap")] + pub swap_free_page: usize, } impl Default for BootConfig { @@ -89,6 +93,8 @@ impl Default for BootConfig { swap: None, #[cfg(feature = "swap")] swap_page_table: Default::default(), + #[cfg(feature = "swap")] + swap_free_page: 0, } } } From bd54417e44766db3c76395ab0757106e5a04e6f4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 18:58:54 +0800 Subject: [PATCH 070/107] add routines for shuttling encrypted data to/from swap RAM region RAM is assumed to be off-chip, and thus the data is encrypted in the region. We also encrypt it in the Precursor case, even though it's on-chip, because we want to capture this as a test case in a known working environment. Also incorporate some routines to generate the per-boot random key for swap encryption. --- loader/src/platform/cramium/swap.rs | 108 ++++++++++++++++++++++-- loader/src/platform/precursor/swap.rs | 116 +++++++++++++++++++++++--- 2 files changed, 209 insertions(+), 15 deletions(-) diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index bb0a79a70..b1389e0fa 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -3,12 +3,17 @@ 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; /// 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; @@ -20,11 +25,16 @@ pub struct SwapHal { // overflow AAD with panic if it's longer than this! aad_storage: [u8; 64], aad_len: usize, - cipher: Aes256GcmSiv, + 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: RawPage, } @@ -219,6 +229,33 @@ impl SwapHal { // 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::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + + // 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::()) { + 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 { @@ -227,11 +264,16 @@ impl SwapHal { partial_nonce: [0u8; 8], aad_storage: [0u8; 64], aad_len: 0, - cipher: Aes256GcmSiv::new((&swap.key).into()), + 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, }; hal.aad_storage[..ssh.aad_len as usize].copy_from_slice(&ssh.aad[..ssh.aad_len as usize]); @@ -245,7 +287,7 @@ impl SwapHal { fn aad(&self) -> &[u8] { &self.aad_storage[..self.aad_len] } - pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { + pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); self.flash_spim.mem_read((self.image_start + offset) as u32, &mut self.buf.data); let mut nonce = [0u8; size_of::()]; @@ -259,14 +301,70 @@ impl SwapHal { aad[..self.aad_len].copy_from_slice(self.aad()); self.flash_spim .mem_read((self.image_mac_start + (offset / 4096) * size_of::()) as u32, &mut tag); - match self.cipher.decrypt_in_place_detached( + 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 in swap: {:?}", e), + Err(e) => panic!("Decryption error from swap image: {:?}", e), + } + } + + 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[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.src_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::()) 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[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::()]; + self.ram_spim + .mem_read((self.swap_mac_start + (src_offset / PAGE_SIZE) * size_of::()) as u32, &mut tag); + self.ram_spim.mem_read(src_offset as u32, &mut self.buf.data); + match self.src_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), } } diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index 82a5de6e9..3cae5edfb 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -1,17 +1,23 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; +use utralib::generated::*; +use super::RAM_BASE; use crate::bootconfig::BootConfig; use crate::println; use crate::swap::*; +use crate::PAGE_SIZE; pub struct SwapHal { - data_area: &'static [u8], - mac_area: &'static [u8], + src_data_area: &'static [u8], + src_mac_area: &'static [u8], partial_nonce: [u8; 8], aad: &'static [u8], - cipher: Aes256GcmSiv, + src_cipher: Aes256GcmSiv, + dst_data_area: &'static mut [u8], + dst_mac_area: &'static mut [u8], + dst_cipher: Aes256GcmSiv, buf: RawPage, } @@ -23,13 +29,35 @@ impl SwapHal { 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::(); + let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + let ram_size_actual = (swap.ram_size as usize & !(PAGE_SIZE - 1)) - mac_size_to_page; + + // 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::()) { + 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 - data_area: unsafe { + 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 - mac_area: unsafe { + src_mac_area: unsafe { core::slice::from_raw_parts( (ssh.mac_offset + swap.flash_offset + 4096) as *const u8, (swap_len / 4096) * size_of::(), @@ -37,7 +65,21 @@ impl SwapHal { }, partial_nonce: [0u8; 8], aad: &ssh.aad[..ssh.aad_len as usize], - cipher: Aes256GcmSiv::new((&swap.key).into()), + 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: RawPage { data: [0u8; 4096] }, }; hal.partial_nonce.copy_from_slice(&ssh.parital_nonce); @@ -47,21 +89,21 @@ impl SwapHal { } } - pub fn decrypt_page_at(&mut self, offset: usize) -> &[u8] { + pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); // println!("data area: {:x?}", &self.data_area[..4]); // println!("offset: {:x}", offset); - self.buf.data.copy_from_slice(&self.data_area[offset..offset + 4096]); + self.buf.data.copy_from_slice(&self.src_data_area[offset..offset + 4096]); let mut nonce = [0u8; size_of::()]; nonce[..4].copy_from_slice(&(offset as u32).to_be_bytes()); nonce[4..].copy_from_slice(&self.partial_nonce); - let tag = &self.mac_area + let tag = &self.src_mac_area [(offset / 4096) * size_of::()..(offset / 4096) * size_of::() + size_of::()]; // println!("nonce: {:x?}", nonce); // println!("tag: {:x?}", tag); // println!("aad: {:x?}", self.aad); // println!("data: {:x?}", &self.buf.data[0..32]); - match self.cipher.decrypt_in_place_detached( + match self.src_cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), self.aad, &mut self.buf.data, @@ -72,6 +114,60 @@ impl SwapHal { } } + 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) { + assert!(buf.len() == PAGE_SIZE); + assert!(dest_offset & (PAGE_SIZE - 1) == 0); + let mut nonce = [0u8; size_of::()]; + nonce[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field + 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.src_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::(); + self.dst_mac_area[mac_offset..mac_offset + size_of::()].copy_from_slice(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[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::()]; + let mac_offset = (src_offset / PAGE_SIZE) * size_of::(); + tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::()]); + self.buf.data.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]); + match self.src_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. From 8aa20e65f681d5e91666229f2673caae2ac349bb Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 19:00:18 +0800 Subject: [PATCH 071/107] match new API for accessing swap --- loader/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 7a803ee93..e8bbdc183 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -383,7 +383,7 @@ pub fn read_initial_config(cfg: &mut BootConfig) { #[cfg(feature = "swap")] pub fn read_swap_config(cfg: &mut BootConfig) { // Read in the swap arguments: should be located at beginning of the first page of swap. - let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_page_at(0); + let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_src_page_at(0); 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") { From a478af445daa8dcb57d65fadd3d9d0e1ba594b11 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 19:01:09 +0800 Subject: [PATCH 072/107] fix IniS arg copying. lay down boilerplate for phase 1 copy the hard part - the core inner loop of the copy -- is still to be done. --- loader/src/phase1.rs | 127 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 104 insertions(+), 23 deletions(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 80b7922c4..5cb225494 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -174,21 +174,26 @@ pub fn copy_args(cfg: &mut BootConfig) { // - 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 - let arg_slice = unsafe { - core::slice::from_raw_parts( - &a as *const KernelArgument as *const usize, - a.size as usize / core::mem::size_of::() + 2, - ) - }; - println!( - "copying arg: 0x{:x}, size: {}. index: {}, len: {}", - a.name, - a.size, - arg_index, - arg_slice.len() - ); - merged_arg_slice[arg_index..arg_index + arg_slice.len()].copy_from_slice(arg_slice); - arg_index += arg_slice.len(); + 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::() + 2, + ) + }; + println!( + "copying arg: 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 @@ -413,16 +418,92 @@ fn copy_processes(cfg: &mut BootConfig) { 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. + if let Some(swap) = cfg.swap_hal.as_mut() { + // 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). - // TODO: access IniS image and figure out what regions are "write" and allocate/copy - // those into RAM. We do this because there isn't a 1:1 mapping of these to the - // images on disk which creates a problem for the swapper. + /* + 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 - // Then, for regions that *are* aligned, allocate them into the swapper's - // memory space, and copy the data into swap. - todo!("implement inis handling"); + 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 = None; + let mut working_buf = [0u8; 4096]; + + println!("tag size: {:x}", tag.size); + println!("tag data: {:x?}", &tag.data); + 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; + let mut last_section_perfect_fit = false; + + for section in inis.sections.iter() { + let flags = section.flags() as u8; + + 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)) + || last_section_perfect_fit + { + swap.encrypt_swap_to( + &mut working_buf, + swap_offset, + dst_page_vaddr & !(PAGE_SIZE - 1), + _pid, + ); + cfg.swap_free_page += 1; + working_buf.fill(0); + working_page_swap_offset = Some(cfg.swap_free_page); + } + } 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 { + todo!(); + } + // 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; + } + println!("Done with sections"); } } TagType::XKrn => { From 26efde0a7188eb69534d2a98610794280d180fb1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 22:36:54 +0800 Subject: [PATCH 073/107] track address of the decrypt buffer --- loader/src/platform/cramium/swap.rs | 5 +++++ loader/src/platform/precursor/swap.rs | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index b1389e0fa..fa7012b8d 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -35,6 +35,7 @@ pub struct SwapHal { swap_mac_start: usize, swap_mac_len: usize, dst_cipher: Aes256GcmSiv, + buf_addr: usize, buf: RawPage, } @@ -274,6 +275,7 @@ impl SwapHal { 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]); @@ -289,6 +291,7 @@ impl SwapHal { pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); + 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[..4].copy_from_slice(&(offset as u32).to_be_bytes()); @@ -312,6 +315,8 @@ impl SwapHal { } } + 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 } diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index 3cae5edfb..a53a2e53a 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -18,6 +18,7 @@ pub struct SwapHal { dst_data_area: &'static mut [u8], dst_mac_area: &'static mut [u8], dst_cipher: Aes256GcmSiv, + buf_addr: usize, buf: RawPage, } @@ -80,6 +81,7 @@ impl SwapHal { ) }, 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); @@ -91,7 +93,8 @@ impl SwapHal { pub fn decrypt_src_page_at(&mut self, offset: usize) -> &[u8] { assert!((offset & 0xFFF) == 0, "offset is not page-aligned"); - // println!("data area: {:x?}", &self.data_area[..4]); + 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::()]; @@ -114,6 +117,8 @@ impl SwapHal { } } + 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 } @@ -121,6 +126,7 @@ impl SwapHal { /// 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::()]; From d489e14f3aa3de4f3843c5a7bf3e57571fb0637b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 22:37:22 +0800 Subject: [PATCH 074/107] fix nonce generation (offset should be bytes, not pages) --- tools/src/swap_writer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/src/swap_writer.rs b/tools/src/swap_writer.rs index 8d6e236ac..ebddaf4f8 100644 --- a/tools/src/swap_writer.rs +++ b/tools/src/swap_writer.rs @@ -80,7 +80,7 @@ impl SwapWriter { }; let mut nonce_vec = Vec::new(); // use BE encoding because nonce is cryptographic matter - nonce_vec.extend_from_slice(&(offset as u32).to_be_bytes()); + 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); From 2ce579b2215bf58c2e6eaf493b445eeaf1be7591 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Apr 2024 22:37:40 +0800 Subject: [PATCH 075/107] phase 1 changes for swap --- loader/src/phase1.rs | 86 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 5cb225494..e043ba14b 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -228,6 +228,9 @@ pub fn copy_args(cfg: &mut BootConfig) { merged_arg_slice_u8[4..6].copy_from_slice(&digest.sum16().to_le_bytes()); } +#[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 @@ -449,6 +452,7 @@ fn copy_processes(cfg: &mut BootConfig) { _pid += 1; let mut working_page_swap_offset: Option = None; let mut working_buf = [0u8; 4096]; + let mut working_buf_dirty = false; println!("tag size: {:x}", tag.size); println!("tag data: {:x?}", &tag.data); @@ -459,7 +463,6 @@ fn copy_processes(cfg: &mut BootConfig) { println!("Swap free page at swap addr: {:x}", cfg.swap_free_page,); let mut last_copy_vaddr = 0; - let mut last_section_perfect_fit = false; for section in inis.sections.iter() { let flags = section.flags() as u8; @@ -468,18 +471,19 @@ fn copy_processes(cfg: &mut BootConfig) { 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)) - || last_section_perfect_fit - { + if (last_copy_vaddr & !(PAGE_SIZE - 1)) != (dst_page_vaddr & !(PAGE_SIZE - 1)) { + // handle case that the new section destination address is outside of the + // current page swap.encrypt_swap_to( &mut working_buf, - swap_offset, + swap_offset * 0x1000, dst_page_vaddr & !(PAGE_SIZE - 1), _pid, ); - cfg.swap_free_page += 1; working_buf.fill(0); working_page_swap_offset = Some(cfg.swap_free_page); + let mut working_buf_dirty = false; + cfg.swap_free_page += 1; } } else { // very first time through the loop. working_buf is guaranteed to be zero. @@ -496,12 +500,80 @@ fn copy_processes(cfg: &mut BootConfig) { // // while bytes_to_copy > 0 { - todo!(); + // 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 swap.decrypt_page_addr() != src_swap_img_page { + swap.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( + &swap.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 { + // 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 + swap.encrypt_swap_to( + &mut working_buf, + working_page_swap_offset.take().unwrap() * 0x1000, + dst_page_vaddr & !(PAGE_SIZE - 1), + _pid, + ); + working_buf.fill(0); + working_page_swap_offset = Some(cfg.swap_free_page); + let mut 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 true + /* VDBG */ + { + 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 { + swap.encrypt_swap_to( + &mut working_buf, + working_page_swap_offset.take().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"); } From 63a8b51fe9420ddfaf9315d9f48a11c4b11a851e Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:38:02 +0800 Subject: [PATCH 076/107] gate SDBG behind feature flag --- loader/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/loader/src/main.rs b/loader/src/main.rs index e8bbdc183..c8f0955f2 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -44,6 +44,8 @@ const STACK_PAGE_COUNT: usize = 8; const VDBG: bool = false; // verbose debug const VVDBG: bool = false; // very verbose debug +#[cfg(feature = "swap")] +const SDBG: bool = true; // swap debug #[cfg(test)] mod test; From 6ce157b20d1332fdfb4d192440a5e0d17ae328eb Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:38:42 +0800 Subject: [PATCH 077/107] add swap handling to minielf --- loader/src/minielf.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 0d61a502c..be4a68dc8 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -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)] @@ -163,6 +164,20 @@ impl MiniElf { (translation_table, tt_address) }; + /* + #[cfg(not(feature = "atsama5d27"))] + let (tt, _tt_address) = { + let tt_address = allocator.processes[pid as usize - 1].satp << 12; + let tt = unsafe { &mut *(tt_address as *mut PageTable) }; + + (tt, tt_address) + }; + #[cfg(feature = "atsama5d27")] + let (tt, _tt_address) = { + let tt_address = allocator.processes[pid as usize - 1].ttbr0; + let translation_table = tt_address as *mut TranslationTableMemory; + (translation_table, tt_address) + }; */ // Turn the satp address into a pointer println!(" Pagetable @ {:08x}", _tt_address); @@ -228,14 +243,15 @@ impl MiniElf { | FLG_X | FLG_VALID | if section.flags() & 1 == 1 { FLG_W } else { 0 } - | if section.flags() & 4 == 4 { FLG_X } else { 0 }; + | if section.flags() & 4 == 4 { FLG_X } else { 0 } + | if ini_type == IniType::IniS { FLG_P } else { 0 }; let copy_to_ram = ((section.flags() as u8) & MINIELF_FLG_W) != 0; if (section.virt as usize) < previous_addr { panic!("init section addresses are not strictly increasing"); } - if copy_to_ram || ini_type == IniType::IniE { + 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(); @@ -353,10 +369,7 @@ impl MiniElf { println!("section is 0x{:x} bytes long; mapping {} pages", section.len(), pages_to_map); } - // --- map FLASH pages to virtual memory --- - // TODO: handle mapping of swap-located pages to virtual memory - #[cfg(feature = "swap")] - todo!("Handle swap-located pages mapping into 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); From 30b5be7a790ac4f848f312fd2ff87f8feff9230b Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:39:07 +0800 Subject: [PATCH 078/107] remove superfluous import --- loader/src/platform/precursor/swap.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index a53a2e53a..d5f744f1b 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -3,7 +3,6 @@ use core::mem::size_of; use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit, Nonce, Tag}; use utralib::generated::*; -use super::RAM_BASE; use crate::bootconfig::BootConfig; use crate::println; use crate::swap::*; From 379d4e81c536e3036aa2a26ec2452f17118d6caf Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:39:29 +0800 Subject: [PATCH 079/107] cleanup swap top mod now that most of the implementation is done --- loader/src/swap.rs | 59 ++++++++++++---------------------------------- 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/loader/src/swap.rs b/loader/src/swap.rs index a3cbf6103..d8b42b670 100644 --- a/loader/src/swap.rs +++ b/loader/src/swap.rs @@ -1,4 +1,17 @@ -use crate::{bootconfig::BootConfig, PageTable}; +/// 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 { @@ -26,46 +39,4 @@ pub struct RawPage { pub data: [u8; 4096], } -/// Mapping pages to swap. -/// -/// Given a virtual address and PID, map to a physical offset in swap. -/* -bitfield! { - #[derive(Copy, Clone, Eq)] - impl Debug; - pub u32, -} -pub struct SwapPte {} -*/ - -const FLG_VALID: usize = 0x1; - -/// 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 -pub fn set_l1_pte(from_va: usize, to_pa: usize, root_pt: &mut PageTable) { - let index = from_va >> 22; - root_pt.entries[index] = ((to_pa & 0xFFFF_FC00) >> 2) // top 2 bits of PA are not used, we don't do 34-bit PA featured by Sv32 - | FLG_VALID; -} - -/// Virtual address fields: -/// 31 22 21 12 11 0 -/// | L1 index | L2 index | LSB of addr | -/// -/// 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 -pub fn set_l2_pte(from_va: usize, to_pa: usize, l2_pt: &mut PageTable, flags: usize) { - let index = (from_va >> 12) & 0x3_FF; - l2_pt.entries[index] = ((to_pa & 0xFFFF_FC00) >> 2) // top 2 bits of PA are not used, we don't do 34-bit PA featured by Sv32 - | flags - | FLG_VALID; -} +pub const SWAP_PT_VADDR: usize = 0xE000_0000; From 287ad58c8a2886b223757d6ae7f06197e6dca08e Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:39:52 +0800 Subject: [PATCH 080/107] add the swapper PID as a constant --- loader/src/consts.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/loader/src/consts.rs b/loader/src/consts.rs index 154e81a26..3c8e1cadb 100644 --- a/loader/src/consts.rs +++ b/loader/src/consts.rs @@ -15,3 +15,6 @@ pub const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; pub const KERNEL_STACK_PAGE_COUNT: usize = 1; pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; pub const GUARD_MEMORY_BYTES: usize = 3 * crate::PAGE_SIZE; + +#[cfg(feature = "swap")] +pub const SWAPPER_PID: u8 = 2; From 25cbf0dc6795d040f6a08e58e3fa925f137f5cb7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:40:23 +0800 Subject: [PATCH 081/107] add swap pt map handling --- loader/src/phase1.rs | 76 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index e043ba14b..e1c141280 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -19,6 +19,9 @@ pub struct InitialProcess { /// Address of the start of the env block pub env: usize, + + #[cfg(feature = "swap")] + pub swap_root: usize, } /// Phase 1: @@ -60,6 +63,9 @@ pub fn phase_1(cfg: &mut BootConfig) { // All further allocations must be page-aligned. cfg.init_size = (cfg.init_size + PAGE_SIZE - 1) & !(PAGE_SIZE - 1); + #[cfg(feature = "swap")] + allocate_swap(cfg); + // Additionally, from this point on all allocations come from // their respective processes rather than kernel memory. @@ -133,6 +139,53 @@ pub fn allocate_processes(cfg: &mut BootConfig) { unsafe { slice::from_raw_parts_mut(processes as *mut InitialProcess, process_count as 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::(); + cfg.init_size += 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::())) } + + for (index, proc) in cfg.processes.iter_mut().enumerate() { + proc.swap_root = swap_pt_base as usize + index * mem::size_of::(); + } + + // allocate a root PT entries - moved earlier in boot process because Swap needs it in Phase 1 + /* + for pid in 2..process_count + 1 { + #[cfg(not(feature = "atsama5d27"))] + { + let tt_address = cfg.alloc() as usize; + let tt = unsafe { &mut *(tt_address as *mut PageTable) }; + cfg.map_page(tt, tt_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W | FLG_VALID, pid as XousPid); + cfg.processes[pid - 1].satp = 0x8000_0000 | ((pid as usize) << 22) | (tt_address >> 12); + } + #[cfg(feature = "atsama5d27")] + { + let pid_idx = pid as usize - 1; + + // Allocate a page to handle the top-level memory translation + let tt_address = cfg.alloc_l1_page_table(pid) as usize; + cfg.processes[pid_idx].ttbr0 = tt_address; + + let translation_table = tt_address as *mut TranslationTableMemory; + // Map all four pages of the translation table to the process' virtual address space + for offset in 0..4 { + let offset = offset * PAGE_SIZE; + cfg.map_page( + translation_table, + tt_address + offset, + PAGE_TABLE_ROOT_OFFSET + offset, + FLG_R | FLG_W | FLG_VALID, + pid as XousPid, + ); + } + cfg.processes[pid - 1].asid = pid; + }; + }*/ +} + #[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 @@ -219,7 +272,7 @@ pub fn copy_args(cfg: &mut BootConfig) { }; digest.write(&xarg_data); // patch the CRC - let mut merged_arg_slice_u8 = unsafe { + let merged_arg_slice_u8 = unsafe { core::slice::from_raw_parts_mut( runtime_arg_buffer as *mut u8, final_len * core::mem::size_of::(), @@ -465,8 +518,6 @@ fn copy_processes(cfg: &mut BootConfig) { let mut last_copy_vaddr = 0; for section in inis.sections.iter() { - let flags = section.flags() as u8; - let mut dst_page_vaddr = section.virt as usize; let mut bytes_to_copy = section.len(); @@ -480,9 +531,10 @@ fn copy_processes(cfg: &mut BootConfig) { dst_page_vaddr & !(PAGE_SIZE - 1), _pid, ); + cfg.map_swap(swap_offset * 0x1000, dst_page_vaddr & !(PAGE_SIZE - 1), _pid); working_buf.fill(0); working_page_swap_offset = Some(cfg.swap_free_page); - let mut working_buf_dirty = false; + working_buf_dirty = false; cfg.swap_free_page += 1; } } else { @@ -537,13 +589,18 @@ fn copy_processes(cfg: &mut BootConfig) { // write the existing page to swap, and allocate a new swap page swap.encrypt_swap_to( &mut working_buf, + working_page_swap_offset.unwrap() * 0x1000, + dst_page_vaddr & !(PAGE_SIZE - 1), + _pid, + ); + cfg.map_swap( working_page_swap_offset.take().unwrap() * 0x1000, dst_page_vaddr & !(PAGE_SIZE - 1), _pid, ); working_buf.fill(0); working_page_swap_offset = Some(cfg.swap_free_page); - let mut working_buf_dirty = false; + working_buf_dirty = false; cfg.swap_free_page += 1; } } @@ -552,9 +609,7 @@ fn copy_processes(cfg: &mut BootConfig) { // the section load address. last_copy_vaddr = dst_page_vaddr; - if true - /* VDBG */ - { + if SDBG { println!("Looping to the next section (swap)"); println!( " swap_free_page: {:x}, dst_page_vaddr: {:x}, src_swap_img_addr: {:x}", @@ -567,6 +622,11 @@ fn copy_processes(cfg: &mut BootConfig) { if working_buf_dirty { swap.encrypt_swap_to( &mut working_buf, + working_page_swap_offset.unwrap() * 0x1000, + last_copy_vaddr & !(PAGE_SIZE - 1), + _pid, + ); + cfg.map_swap( working_page_swap_offset.take().unwrap() * 0x1000, last_copy_vaddr & !(PAGE_SIZE - 1), _pid, From 2b5fabb2fdcfc0458f741c1ee52b2d693b80aca4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:40:42 +0800 Subject: [PATCH 082/107] add swap pt routines --- loader/src/bootconfig.rs | 67 +++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index 3e0428125..7c6141ccc 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -62,11 +62,6 @@ pub struct BootConfig { #[cfg(feature = "swap")] pub swap: Option<&'static SwapDescriptor>, - /// Swap root page table. Array of root page tables, one per process. - /// Kernel does not get swap, so the first entry belongs to PID 2. - #[cfg(feature = "swap")] - pub swap_page_table: &'static mut [&'static mut PageTable], - /// Offset of the current free page in swap #[cfg(feature = "swap")] pub swap_free_page: usize, @@ -92,8 +87,6 @@ impl Default for BootConfig { #[cfg(feature = "swap")] swap: None, #[cfg(feature = "swap")] - swap_page_table: Default::default(), - #[cfg(feature = "swap")] swap_free_page: 0, } } @@ -182,6 +175,66 @@ impl BootConfig { } } + fn alloc_swap(&mut self) -> *mut usize { + self.init_size += PAGE_SIZE; + let pg = self.get_top(); + unsafe { + // Grab the page address and zero it out + bzero(pg as *mut usize, pg.add(PAGE_SIZE / mem::size_of::()) as *mut usize); + } + pg as *mut usize + } + + #[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.processes[owner as usize - 1].swap_root as *mut usize, + mem::size_of::() / mem::size_of::(), + ) + }; + + // Allocate a new level 1 pagetable entry if one doesn't exist. + if l1_pt[vpn1] & FLG_VALID == 0 { + let na = self.alloc_swap() 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, From cd140769ac87567ca63858d899ae524c858b9cb3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 12:41:08 +0800 Subject: [PATCH 083/107] add code to map swap page tables into swapper memory space --- loader/src/phase2.rs | 65 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index f5f655768..c7b664af0 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -1,4 +1,4 @@ -use crate::*; +use crate::{swap::SWAP_PT_VADDR, *}; /// Phase 2 bootloader /// @@ -92,12 +92,15 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { process_offset -= allocated; pid += 1; } else if tag.name == u32::from_le_bytes(*b"IniS") { - 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; + #[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) }; @@ -177,6 +180,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.processes[p].swap_root; + 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.processes[p].swap_root 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:"); From 73aeae480df8e5f4e209750b7c354fbb25cee9c1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:41:48 +0800 Subject: [PATCH 084/107] add plaintext debug output for swap image --- xtask/src/builder.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index 848bcb541..189a70054 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -983,6 +983,7 @@ impl Builder { 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); @@ -991,7 +992,13 @@ impl Builder { swap_file.push(stream); swap_file.push("swap.img"); args.push("--swap-name"); - args.push(swap_file.to_str().unwrap()) + 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 { From b802af6edca65793dfd847a7bd2d47c34a2c7f1e Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:43:05 +0800 Subject: [PATCH 085/107] add explicit resume support to renode image don't do resume on swap builds -- our stack is too deep to support the clean boot marker --- xtask/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index bc6d70f7b..dd3f347ad 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -210,10 +210,13 @@ fn main() -> Result<(), Box> { // ----- 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()); From 58ce8a2923b67396b70c7413db2d4d47c7833d12 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:43:45 +0800 Subject: [PATCH 086/107] make resume not a default feature in renode this is compensated for in xtask to put it back into your usual builds, but it allows us to not have it in the swap config. --- loader/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 11daf46a8..f986e53e5 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -55,7 +55,6 @@ renode = [ "utralib/renode", "ed25519-dalek-loader/renode", "curve25519-dalek-loader/renode", - "resume", "secboot", "sha2-loader", ] From 400088fb42f127f2aca56d1619b35fcd022110bc Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:47:54 +0800 Subject: [PATCH 087/107] add debug file generation for swap --- tools/src/bin/create-image.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index 5cb254611..b8797cb2d 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -174,6 +174,13 @@ fn main() { .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") @@ -479,6 +486,18 @@ fn main() { 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 { From 36173fde95d99585e0d60220503d6404ccfdbca6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:54:24 +0800 Subject: [PATCH 088/107] adjust ram available size when swap is enabled --- loader/src/platform/precursor.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/loader/src/platform/precursor.rs b/loader/src/platform/precursor.rs index b4d9915d5..e317c4a5a 100644 --- a/loader/src/platform/precursor.rs +++ b/loader/src/platform/precursor.rs @@ -3,7 +3,10 @@ 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 From 03c3f61ba33e009dd692ba9d934e7bcb5cb30756 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:54:36 +0800 Subject: [PATCH 089/107] fixes to encrypt/decrypt routines to adjust offsets etc. --- loader/src/platform/cramium/swap.rs | 19 +++++++++++++--- loader/src/platform/precursor/swap.rs | 31 +++++++++++++++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/loader/src/platform/cramium/swap.rs b/loader/src/platform/cramium/swap.rs index fa7012b8d..319756bd0 100644 --- a/loader/src/platform/cramium/swap.rs +++ b/loader/src/platform/cramium/swap.rs @@ -14,6 +14,7 @@ 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; @@ -232,8 +233,17 @@ impl SwapHal { // compute offsets for swap let mac_size = (swap.ram_size as usize / 4096) * size_of::(); - let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + 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); @@ -291,6 +301,9 @@ impl SwapHal { 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::()]; @@ -334,7 +347,7 @@ impl SwapHal { 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.src_cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) { + 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( @@ -362,7 +375,7 @@ impl SwapHal { self.ram_spim .mem_read((self.swap_mac_start + (src_offset / PAGE_SIZE) * size_of::()) as u32, &mut tag); self.ram_spim.mem_read(src_offset as u32, &mut self.buf.data); - match self.src_cipher.decrypt_in_place_detached( + match self.dst_cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), aad, &mut self.buf.data, diff --git a/loader/src/platform/precursor/swap.rs b/loader/src/platform/precursor/swap.rs index d5f744f1b..6c0279f53 100644 --- a/loader/src/platform/precursor/swap.rs +++ b/loader/src/platform/precursor/swap.rs @@ -7,6 +7,7 @@ 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], @@ -33,8 +34,17 @@ impl SwapHal { // 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::(); - let mac_size_to_page = (mac_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + 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); @@ -92,6 +102,9 @@ impl SwapHal { 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); @@ -125,7 +138,7 @@ impl SwapHal { /// 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); + // 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::()]; @@ -136,19 +149,23 @@ impl SwapHal { 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.src_cipher.encrypt_in_place_detached(Nonce::from_slice(&nonce), aad, buf) { + 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::(); self.dst_mac_area[mac_offset..mac_offset + size_of::()].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), } } - /// Swap count is fixed at 0 by this routine. The data to be encrypted is - /// assumed to already be in `self.buf` + /// 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[0..4].copy_from_slice(&[0u8; 4]); // this is the `swap_count` field @@ -161,8 +178,10 @@ impl SwapHal { let mut tag = [0u8; size_of::()]; let mac_offset = (src_offset / PAGE_SIZE) * size_of::(); tag.copy_from_slice(&self.dst_mac_area[mac_offset..mac_offset + size_of::()]); + // println!("dst_mac_area: {:x?}", &self.dst_mac_area[..32]); self.buf.data.copy_from_slice(&self.dst_data_area[src_offset..src_offset + PAGE_SIZE]); - match self.src_cipher.decrypt_in_place_detached( + // println!("Nonce: {:x?}, tag: {:x?}", &nonce, &tag); + match self.dst_cipher.decrypt_in_place_detached( Nonce::from_slice(&nonce), aad, &mut self.buf.data, From 1d0f34779a3746d8e26f25480097b2b3400c4fb9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:55:24 +0800 Subject: [PATCH 090/107] extend debug to print swap bits --- loader/src/debug.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/loader/src/debug.rs b/loader/src/debug.rs index 3b302e67f..e900728b6 100644 --- a/loader/src/debug.rs +++ b/loader/src/debug.rs @@ -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 ); } } From 8811114a9b8ded87db914111327ac281435efb5e Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:55:40 +0800 Subject: [PATCH 091/107] remove unused alignment feature we're not passing pages around like I thought I would. svae the stack space, so in the future if we do stuff with e.g. suspend/resume we don't have to reserve such a huge chunk of RAM for boot. --- loader/src/swap.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/loader/src/swap.rs b/loader/src/swap.rs index d8b42b670..d29a940c8 100644 --- a/loader/src/swap.rs +++ b/loader/src/swap.rs @@ -33,8 +33,7 @@ pub struct SwapSourceHeader { pub aad: [u8; 64], } -/// An aligned, raw-page structure -#[repr(C, align(4096))] +#[repr(C)] pub struct RawPage { pub data: [u8; 4096], } From bd58d9187ef9d71a280c0615d8e510165467202c Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:56:35 +0800 Subject: [PATCH 092/107] make secboot's stack test adjust to the platform-defined RAM available --- loader/src/secboot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/src/secboot.rs b/loader/src/secboot.rs index c19ba4306..913639ccc 100644 --- a/loader/src/secboot.rs +++ b/loader/src/secboot.rs @@ -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::*; From d6a9c85acb225db51c2a0bfc9f12ded35b9a5a62 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Apr 2024 23:58:19 +0800 Subject: [PATCH 093/107] extended debugging; make check_load aware of swap --- loader/src/main.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index c8f0955f2..fdf9542ac 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -44,7 +44,6 @@ const STACK_PAGE_COUNT: usize = 8; const VDBG: bool = false; // verbose debug const VVDBG: bool = false; // very verbose debug -#[cfg(feature = "swap")] const SDBG: bool = true; // swap debug #[cfg(test)] @@ -65,7 +64,7 @@ pub struct PageTable { entries: [usize; PAGE_SIZE / WORD_SIZE], } -#[derive(Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum IniType { // RAM IniE, @@ -126,12 +125,19 @@ 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::()); read_initial_config(&mut cfg); #[cfg(feature = "swap")] { cfg.swap_hal = SwapHal::new(&cfg); read_swap_config(&mut cfg); + #[cfg(feature = "resume")] + println!( + "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 @@ -147,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."); @@ -161,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."); @@ -384,8 +390,8 @@ pub fn read_initial_config(cfg: &mut BootConfig) { #[cfg(feature = "swap")] pub fn read_swap_config(cfg: &mut BootConfig) { - // Read in the swap arguments: should be located at beginning of the first page of swap. - let page0 = cfg.swap_hal.as_mut().unwrap().decrypt_src_page_at(0); + // 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") { @@ -468,8 +474,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::()) + 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); } @@ -493,7 +503,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; @@ -507,17 +517,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, true); + inis.check(cfg, inis.load_offset as usize, pid, IniType::IniS); pid += 1; } } From dbacbb609bdbd685300054010e366ad373a9e978 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 00:01:08 +0800 Subject: [PATCH 094/107] fix argument appending and mutability issues The argument appending code in copy_args() is truly abominable. Suspect more latent bugs in there. Definitely some deep technical debt in this routine, it's basically just a pile of unsafe pointers jammed together with no checks. This has to do in part with the underlying implementation of the argument iterators, which understandably try to do a lot with no allocations, but it's brittle and does not play nicely with the idea of extending the argument records. --- loader/src/phase1.rs | 71 +++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index e1c141280..8743b0f50 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -190,6 +190,14 @@ pub fn allocate_swap(cfg: &mut BootConfig) { 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 @@ -202,9 +210,8 @@ pub fn copy_args(cfg: &mut BootConfig) { // Merge the args list to target RAM // Reserve space for the primary arg list + swap args - swap's XArg structure (7 words long) - println!("length of swap arg: {}", swap_xarg.data[0] as usize); - let final_len = cfg.args.size() + swap_xarg.data[0] as usize - 7; - cfg.init_size += final_len; + let final_len = cfg.args.size() / core::mem::size_of::() + 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 { @@ -217,8 +224,7 @@ pub fn copy_args(cfg: &mut BootConfig) { // 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(); - println!("orig arg size: {}, new size: {}", cfg.args.size(), final_len); + let mut arg_index = cfg.args.size() / core::mem::size_of::(); // append the swap arguments, and patch the size field accordingly for a in j { @@ -234,14 +240,16 @@ pub fn copy_args(cfg: &mut BootConfig) { a.size as usize / core::mem::size_of::() + 2, ) }; - println!( - "copying arg: 0x{:x}, size: {}. index: {}, len: {}, data: {:x?}", - a.name, - a.size, - arg_index, - arg_slice.len(), - arg_slice - ); + 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 { @@ -474,7 +482,7 @@ fn copy_processes(cfg: &mut BootConfig) { TagType::IniS => { // if swap is not enabled, don't pull this code in, to keep the bootloader light-weight #[cfg(feature = "swap")] - if let Some(swap) = cfg.swap_hal.as_mut() { + { // 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 @@ -523,15 +531,21 @@ fn copy_processes(cfg: &mut BootConfig) { 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 - swap.encrypt_swap_to( + cfg.swap_hal.as_mut().expect("swap HAL uninit").encrypt_swap_to( &mut working_buf, swap_offset * 0x1000, - dst_page_vaddr & !(PAGE_SIZE - 1), + last_copy_vaddr & !(PAGE_SIZE - 1), _pid, ); - cfg.map_swap(swap_offset * 0x1000, dst_page_vaddr & !(PAGE_SIZE - 1), _pid); + cfg.map_swap(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; @@ -559,8 +573,13 @@ fn copy_processes(cfg: &mut BootConfig) { 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 swap.decrypt_page_addr() != src_swap_img_page { - swap.decrypt_src_page_at(src_swap_img_page); + 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); @@ -572,7 +591,8 @@ fn copy_processes(cfg: &mut BootConfig) { }; if !section.no_copy() { working_buf[dst_page_offset..dst_page_offset + copyable].copy_from_slice( - &swap.buf_as_ref()[src_swap_img_offset..src_swap_img_offset + copyable], + &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 { @@ -585,17 +605,20 @@ fn copy_processes(cfg: &mut BootConfig) { 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 - swap.encrypt_swap_to( + cfg.swap_hal.as_mut().expect("swap HAL uninit").encrypt_swap_to( &mut working_buf, working_page_swap_offset.unwrap() * 0x1000, - dst_page_vaddr & !(PAGE_SIZE - 1), + full_page_vaddr & !(PAGE_SIZE - 1), _pid, ); cfg.map_swap( working_page_swap_offset.take().unwrap() * 0x1000, - dst_page_vaddr & !(PAGE_SIZE - 1), + full_page_vaddr & !(PAGE_SIZE - 1), _pid, ); working_buf.fill(0); @@ -620,7 +643,7 @@ fn copy_processes(cfg: &mut BootConfig) { } // flush the encryption buffer if working_buf_dirty { - swap.encrypt_swap_to( + 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), From 62f1ad09f06bf3bbc97f206218ec401903d30fc4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 00:03:48 +0800 Subject: [PATCH 095/107] ensure that the kernel is loaded last, even after argument extension The kernel is supposed to be loaded last, but, when we append arguments that isn't true anymore. This fix makes sure the kernel is loaded last. --- loader/src/phase2.rs | 54 +++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index c7b664af0..db3dd2179 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -1,4 +1,6 @@ -use crate::{swap::SWAP_PT_VADDR, *}; +#[cfg(feature = "swap")] +use crate::swap::SWAP_PT_VADDR; +use crate::*; /// Phase 2 bootloader /// @@ -76,6 +78,7 @@ 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; + let mut xkrn: Option<&ProgramDescription> = None; for tag in args.iter() { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); @@ -102,28 +105,34 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { 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) }; - 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"))] - { - xkrn.load(cfg, process_offset - load_size_rounded, 1); - } - #[cfg(feature = "atsama5d27")] - { - (ktext_offset, kdata_offset, kernel_exception_sp, kernel_irq_sp) = - xkrn.load(cfg, process_offset - load_size_rounded, 1); - (ktext_size, kdata_size, ktext_virt_offset, kdata_virt_offset) = ( - (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)), - xkrn.text_offset as usize, - xkrn.data_offset as usize, - ); - } - process_offset -= load_size_rounded; + xkrn = Some(unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }); } } + // Ensure that the kernel is loaded last, even if the arguments are out of order. + // This is necessary due to the swap-append arguments. + if let Some(xkrn) = xkrn { + 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"))] + { + xkrn.load(cfg, process_offset - load_size_rounded, 1); + } + #[cfg(feature = "atsama5d27")] + { + (ktext_offset, kdata_offset, kernel_exception_sp, kernel_irq_sp) = + xkrn.load(cfg, process_offset - load_size_rounded, 1); + (ktext_size, kdata_size, ktext_virt_offset, kdata_virt_offset) = ( + (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)), + xkrn.text_offset as usize, + xkrn.data_offset as usize, + ); + } + // process_offset -= load_size_rounded; + } else { + panic!("No kernel in arguments, can't boot."); + } println!("Done loading."); @@ -322,6 +331,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, From d318134bdc27d089d48ec879d483aeb115f3aa41 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 00:04:47 +0800 Subject: [PATCH 096/107] fix default flags for swap; add load checking for swap load checking is aware of all three types of sections, and can natively call the encrypt/decrypt ops to ensure that the encrypted swap is set up correctly. --- loader/src/minielf.rs | 129 +++++++++++++++++++++++++++++++++--------- 1 file changed, 101 insertions(+), 28 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index be4a68dc8..afc678c14 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -243,8 +243,7 @@ impl MiniElf { | FLG_X | FLG_VALID | if section.flags() & 1 == 1 { FLG_W } else { 0 } - | if section.flags() & 4 == 4 { FLG_X } else { 0 } - | if ini_type == IniType::IniS { FLG_P } else { 0 }; + | if section.flags() & 4 == 4 { FLG_X } else { 0 }; let copy_to_ram = ((section.flags() as u8) & MINIELF_FLG_W) != 0; if (section.virt as usize) < previous_addr { @@ -371,8 +370,17 @@ impl MiniElf { // --- 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); last_mapped_xip = virt_page; section_map_phys_offset += PAGE_SIZE; @@ -404,18 +412,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.processes[pid as usize - 1].swap_root >> 12 + } + #[cfg(not(feature = "swap"))] + { + 0 + } + } + }; #[cfg(feature = "atsama5d27")] let tt = { allocator.processes[pid as usize - 1].ttbr0 }; @@ -435,18 +453,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!( @@ -461,7 +524,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) }; @@ -470,8 +533,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 { let l1_pt = unsafe { &mut (*((root << 12) as *mut PageTable)) }; let l1_entry = l1_pt.entries[(va & 0xFFC0_0000) >> 22]; From 9992d29b408e939a9739779d1dfdeb20e99beec0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 00:06:18 +0800 Subject: [PATCH 097/107] chore: cleanup warning in non-swap mode --- loader/src/bootconfig.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index 7c6141ccc..d9ad65371 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -175,6 +175,7 @@ impl BootConfig { } } + #[cfg(feature = "swap")] fn alloc_swap(&mut self) -> *mut usize { self.init_size += PAGE_SIZE; let pg = self.get_top(); From 30202d962c7ad4b16bb57c4ff6bdb166968d048e Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:16:26 +0800 Subject: [PATCH 098/107] don't build swap_root into the InitialProcess structure the kernel doesn't use it, so it just ends up being wasted space. --- loader/src/bootconfig.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index d9ad65371..98bfe99d0 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -54,6 +54,14 @@ 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, @@ -65,6 +73,10 @@ pub struct BootConfig { /// 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 { @@ -82,12 +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(), } } } @@ -175,17 +190,6 @@ impl BootConfig { } } - #[cfg(feature = "swap")] - fn alloc_swap(&mut self) -> *mut usize { - self.init_size += PAGE_SIZE; - let pg = self.get_top(); - unsafe { - // Grab the page address and zero it out - bzero(pg as *mut usize, pg.add(PAGE_SIZE / mem::size_of::()) as *mut usize); - } - pg as *mut usize - } - #[cfg(feature = "swap")] pub fn map_swap(&mut self, swap_phys: usize, virt: usize, owner: XousPid) { if SDBG { @@ -199,14 +203,14 @@ impl BootConfig { assert!(owner != 0); let l1_pt = unsafe { core::slice::from_raw_parts_mut( - self.processes[owner as usize - 1].swap_root as *mut usize, + self.swap_root[owner as usize - 1] as *mut usize, mem::size_of::() / mem::size_of::(), ) }; // Allocate a new level 1 pagetable entry if one doesn't exist. if l1_pt[vpn1] & FLG_VALID == 0 { - let na = self.alloc_swap() as usize; + let na = self.alloc() as usize; if SDBG { println!( "Swap Level 1 page table is invalid ({:08x}) @ {:08x} -- allocating a new one @ {:08x}", From 630a81b18559e0aea6644936b3eb693224937e2f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:17:21 +0800 Subject: [PATCH 099/107] we need more stack to decrypt memory in the loader. --- loader/src/consts.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/loader/src/consts.rs b/loader/src/consts.rs index 3c8e1cadb..a2c6849ef 100644 --- a/loader/src/consts.rs +++ b/loader/src/consts.rs @@ -14,7 +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; From d6e7f7ce16615b8dcfb83ff0f32323d4388457ac Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:17:49 +0800 Subject: [PATCH 100/107] add debugging and asserts to detect stack smash in loader In swap configuration, the loader does a lot more: it has to decrypt full pages of RAM and hold them temporarily; up to two pages at once, once for the decrypt of the source swap image, and one for the encrypt to swap memory. This causes a lot more stack to be used than the original loader. Add some debugging for this, and an assert based on a heuristic that the RPT (runtime page tracker) last few entries ought to be 0. This I think is probably true on most platforms. If this fails to be true, then, we can probably modify the assert to fail if any number in the RPT is greater than the number of processes: this should be true in *all* cases, but a stack smash is very likely to write something in there that's larger than the number of allowable processes in the system. --- loader/src/main.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index fdf9542ac..b526ba34d 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -216,7 +216,25 @@ 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 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 32 bytes of the RPT + // ought to be 0. This a valid assumption at least on Precursor, as it maps to some CSRs that should + // be unmapped at this point. TODO: check this assumption holds on other archs. + for &check in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 32..].iter() { + assert!( + check == 0x0, + "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 = @@ -345,6 +363,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; @@ -400,8 +422,10 @@ pub fn read_swap_config(cfg: &mut BootConfig) { } 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 - println!("swap start: {:x}", tag.data[2]); - println!("swap size: {:x}", tag.data[3]); + 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); } From a66e4f5901c640f8740ed94dbe77be5345f1cc25 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:24:22 +0800 Subject: [PATCH 101/107] modify stack smash check to detect if non-PID is written this is more robust, and should work across all architectures. Note that the reliability of the check is slightly impacted since we're now checking for a subset of valid numbers, so the number of bytes checked is doubled. --- loader/src/main.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index b526ba34d..06d9361e1 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -219,19 +219,20 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - 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 if stack did not overflow. + // 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 32 bytes of the RPT - // ought to be 0. This a valid assumption at least on Precursor, as it maps to some CSRs that should - // be unmapped at this point. TODO: check this assumption holds on other archs. - for &check in cfg.runtime_page_tracker[cfg.runtime_page_tracker.len() - 32..].iter() { + // 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 == 0x0, + check <= cfg.processes.len() as u8, "RPT looks corrupted, suspect stack overflow in loader. Increase GUARD_MEMORY_BYTES!" ); } From 8abb126577e1f1664883af2b4f2e9b967e46e39b Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:25:37 +0800 Subject: [PATCH 102/107] break out swap_root & improve debug --- loader/src/minielf.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index afc678c14..e2ed74dd6 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -102,6 +102,8 @@ 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. @@ -381,6 +383,11 @@ impl MiniElf { 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; @@ -426,7 +433,7 @@ impl MiniElf { IniType::IniS => { #[cfg(feature = "swap")] { - allocator.processes[pid as usize - 1].swap_root >> 12 + allocator.swap_root[pid as usize - 1] >> 12 } #[cfg(not(feature = "swap"))] { From 8c82d710bbf1ee2e5708db3fd6f64c002738f7f6 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:25:59 +0800 Subject: [PATCH 103/107] reflect swap offset patches in atamsam5d27 arch --- loader/src/platform/atsama5d27/boot.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/loader/src/platform/atsama5d27/boot.rs b/loader/src/platform/atsama5d27/boot.rs index afbee2624..c5c6bef28 100644 --- a/loader/src/platform/atsama5d27/boot.rs +++ b/loader/src/platform/atsama5d27/boot.rs @@ -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, From d367c99a1fde575aaf4f32d11807e560f1222d79 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:26:38 +0800 Subject: [PATCH 104/107] fix page mapping; allow kernel to load in arg order; swap_root breakout Page mapping just can't be done during the phase 1 loading, because we move the loading point by an unpredictable amount which messes up phase 2. I mean, we could try to predict the amount, but it's easier if we just stick with the current algorithm instead of trying to do both at once. Basically, phase 1 must be "no-alloc" entirely as it copies. Phase 2 can allocate page table entries, because all the data is copied to RAM and you know where that starts; and you can start to append data to the end of that without affecting the loading points of programs. Undo a previous patch to force kernel to load last. Turns out this is legal, it was only problematic because of other bugs. Also, reflect swap_root in the BootConfig structure and not in InitialProcess structure. + Various other debug fixes --- loader/src/phase1.rs | 123 +++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 52 deletions(-) diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 8743b0f50..36f34db41 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -19,9 +19,6 @@ pub struct InitialProcess { /// Address of the start of the env block pub env: usize, - - #[cfg(feature = "swap")] - pub swap_root: usize, } /// Phase 1: @@ -42,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. @@ -61,7 +59,15 @@ 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); @@ -74,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 @@ -123,6 +138,14 @@ 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::() + ); + } } pub fn allocate_processes(cfg: &mut BootConfig) { @@ -137,6 +160,31 @@ 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::() + ); + } + let swap_root_pt_size = process_count * mem::size_of::(); + 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::() + ); + } + } } #[cfg(feature = "swap")] @@ -144,46 +192,22 @@ pub fn allocate_swap(cfg: &mut BootConfig) { let process_count = cfg.init_process_count + 1; let swap_pt_size = process_count * mem::size_of::(); 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::())) } - - for (index, proc) in cfg.processes.iter_mut().enumerate() { - proc.swap_root = swap_pt_base as usize + index * mem::size_of::(); + // 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::(); } - // allocate a root PT entries - moved earlier in boot process because Swap needs it in Phase 1 - /* - for pid in 2..process_count + 1 { - #[cfg(not(feature = "atsama5d27"))] - { - let tt_address = cfg.alloc() as usize; - let tt = unsafe { &mut *(tt_address as *mut PageTable) }; - cfg.map_page(tt, tt_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W | FLG_VALID, pid as XousPid); - cfg.processes[pid - 1].satp = 0x8000_0000 | ((pid as usize) << 22) | (tt_address >> 12); - } - #[cfg(feature = "atsama5d27")] - { - let pid_idx = pid as usize - 1; - - // Allocate a page to handle the top-level memory translation - let tt_address = cfg.alloc_l1_page_table(pid) as usize; - cfg.processes[pid_idx].ttbr0 = tt_address; - - let translation_table = tt_address as *mut TranslationTableMemory; - // Map all four pages of the translation table to the process' virtual address space - for offset in 0..4 { - let offset = offset * PAGE_SIZE; - cfg.map_page( - translation_table, - tt_address + offset, - PAGE_TABLE_ROOT_OFFSET + offset, - FLG_R | FLG_W | FLG_VALID, - pid as XousPid, - ); - } - cfg.processes[pid - 1].asid = pid; - }; - }*/ + 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::() + ); + } } #[cfg(feature = "swap")] @@ -287,6 +311,14 @@ pub fn copy_args(cfg: &mut BootConfig) { ) }; 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::() + ); + } } #[cfg(feature = "swap")] @@ -515,8 +547,6 @@ fn copy_processes(cfg: &mut BootConfig) { let mut working_buf = [0u8; 4096]; let mut working_buf_dirty = false; - println!("tag size: {:x}", tag.size); - println!("tag data: {:x?}", &tag.data); let inis = MiniElf::new(&tag); let mut src_swap_img_addr = inis.load_offset as usize; @@ -545,7 +575,6 @@ fn copy_processes(cfg: &mut BootConfig) { last_copy_vaddr & !(PAGE_SIZE - 1), _pid, ); - cfg.map_swap(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; @@ -616,11 +645,6 @@ fn copy_processes(cfg: &mut BootConfig) { full_page_vaddr & !(PAGE_SIZE - 1), _pid, ); - cfg.map_swap( - working_page_swap_offset.take().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; @@ -649,11 +673,6 @@ fn copy_processes(cfg: &mut BootConfig) { last_copy_vaddr & !(PAGE_SIZE - 1), _pid, ); - cfg.map_swap( - working_page_swap_offset.take().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; From 673531244430ffb979f5ee558c9772945f4c791c Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:30:21 +0800 Subject: [PATCH 105/107] add some more asserts & checks; undo kernel load last patch kernel load last isn't necessary add some asserts to help sanity check things. In particular, the kernel mapped memory can't exceed 0xA000 in length, otherwise it bumps into hard-coded constants in the kernel for where hardware mapped pages should go (i.e., panic framebuffer, debug UART, etc.) --- loader/src/phase2.rs | 66 ++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index db3dd2179..31f2b1ec7 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -78,7 +78,7 @@ 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; - let mut xkrn: Option<&ProgramDescription> = None; + for tag in args.iter() { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); @@ -105,38 +105,33 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { pid += 1; } } else if tag.name == u32::from_le_bytes(*b"XKrn") { - xkrn = Some(unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }); - } - } - // Ensure that the kernel is loaded last, even if the arguments are out of order. - // This is necessary due to the swap-append arguments. - if let Some(xkrn) = xkrn { - 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"))] - { - xkrn.load(cfg, process_offset - load_size_rounded, 1); - } - #[cfg(feature = "atsama5d27")] - { - (ktext_offset, kdata_offset, kernel_exception_sp, kernel_irq_sp) = + 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"))] + { xkrn.load(cfg, process_offset - load_size_rounded, 1); - (ktext_size, kdata_size, ktext_virt_offset, kdata_virt_offset) = ( - (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)), - xkrn.text_offset as usize, - xkrn.data_offset as usize, - ); + } + #[cfg(feature = "atsama5d27")] + { + (ktext_offset, kdata_offset, kernel_exception_sp, kernel_irq_sp) = + xkrn.load(cfg, process_offset - load_size_rounded, 1); + (ktext_size, kdata_size, ktext_virt_offset, kdata_virt_offset) = ( + (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)), + xkrn.text_offset as usize, + xkrn.data_offset as usize, + ); + } + process_offset -= load_size_rounded; } - // process_offset -= load_size_rounded; - } else { - panic!("No kernel in arguments, can't boot."); } 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"))] @@ -151,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, @@ -160,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 @@ -198,7 +204,7 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { // 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.processes[p].swap_root; + let swap_root = cfg.swap_root[p]; println!( "Mapping root swap PT to PID 2 @paddr {:x} -> vaddr {:x}", swap_root, @@ -215,7 +221,7 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { } // now chase down any entries in the roots, and map valid pages for p in 0..cfg.processes.len() { - let root_pt = unsafe { &*(cfg.processes[p].swap_root as *const PageTable) }; + 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; From 336e47eabe44e6153bfd00a967c09b7ecb856f8c Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 15:46:48 +0800 Subject: [PATCH 106/107] remove comment --- loader/src/minielf.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index e2ed74dd6..f5b4f8fcc 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -166,20 +166,6 @@ impl MiniElf { (translation_table, tt_address) }; - /* - #[cfg(not(feature = "atsama5d27"))] - let (tt, _tt_address) = { - let tt_address = allocator.processes[pid as usize - 1].satp << 12; - let tt = unsafe { &mut *(tt_address as *mut PageTable) }; - - (tt, tt_address) - }; - #[cfg(feature = "atsama5d27")] - let (tt, _tt_address) = { - let tt_address = allocator.processes[pid as usize - 1].ttbr0; - let translation_table = tt_address as *mut TranslationTableMemory; - (translation_table, tt_address) - }; */ // Turn the satp address into a pointer println!(" Pagetable @ {:08x}", _tt_address); From 4ace363abcd6af29e49a8ef8d259a05a71aed018 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 9 Apr 2024 18:23:09 +0800 Subject: [PATCH 107/107] this resolves comments from @xobs' code review --- libs/cramium-hal/src/udma.rs | 6 +----- loader/src/main.rs | 2 +- tools/src/bin/create-image.rs | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/libs/cramium-hal/src/udma.rs b/libs/cramium-hal/src/udma.rs index 5c7a31602..c1dbdc2c5 100644 --- a/libs/cramium-hal/src/udma.rs +++ b/libs/cramium-hal/src/udma.rs @@ -1035,11 +1035,7 @@ impl Spim { } while words_sent < total_words { // determine the valid length of data we could send - let tx_len = if (total_words - words_sent) >= self.tx_buf_len_bytes as usize { - self.tx_buf_len_bytes - } else { - total_words - words_sent - }; + 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, diff --git a/loader/src/main.rs b/loader/src/main.rs index 06d9361e1..1938f49dc 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -134,7 +134,7 @@ fn boot_sequence(args: KernelArguments, _signature: u32, fs_prehash: [u8; 64]) - cfg.swap_hal = SwapHal::new(&cfg); read_swap_config(&mut cfg); #[cfg(feature = "resume")] - println!( + 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." ); diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index b8797cb2d..9cee9d0e3 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -431,7 +431,7 @@ fn main() { } if let Some(init_paths) = matches.values_of("inis") { - if let Some(ref mut sargs) = swap_args { + 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(