diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 26e2e034e..61fa7320c 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -22,8 +22,8 @@ lazy_static = "1.4.0" [features] precursor = ["utralib/precursor", "ed25519-dalek-loader/precursor", "curve25519-dalek-loader/precursor", "resume", "secboot"] renode = ["utralib/renode", "ed25519-dalek-loader/renode", "curve25519-dalek-loader/renode", "resume", "secboot"] -cramium-soc = ["utralib/cramium-soc"] -cramium-fpga = ["utralib/cramium-fpga"] +cramium-soc = ["utralib/cramium-soc", "debug-print", "simulation-only"] +cramium-fpga = ["utralib/cramium-fpga", "debug-print", "simulation-only"] atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"] platform-tests = [] debug-print = [] diff --git a/loader/build.rs b/loader/build.rs index 0a1f6b876..6bbae1846 100644 --- a/loader/build.rs +++ b/loader/build.rs @@ -13,9 +13,11 @@ fn main() { #[cfg(feature="cramium-soc")] let p = PathBuf::from("src/platform/cramium/link-soc.x"); #[cfg(feature="cramium-fpga")] - let p = PathBuf::from("src/platform/cramium/link-fpga.x"); + let p = PathBuf::from("src/platform/cramium/link-soc.x"); // note deviation -- this is because we are still simulating the FPGA model and offsets are not yet settled for that + #[cfg(not(any(feature="cramium-soc", feature="cramium-fpga")))] + let p = PathBuf::from("src/platform/precursor/link.x"); - #[cfg(not(any(feature="cramium-fpga", feature="cramium-soc")))] + println!("cargo:rerun-if-changed={}", p.clone().into_os_string().into_string().unwrap()); println!("cargo:rustc-link-arg=-Tlink.x"); p @@ -37,8 +39,9 @@ fn main() { unreachable!("unsupported target"); }; + println!("{}", out_dir.join("link.x").display()); // Put the linker script somewhere the linker can find it - fs::File::create(out_dir.join("link.x")) + std::fs::OpenOptions::new().create(true).write(true).truncate(true).open(out_dir.join("link.x")) .unwrap() .write_all(fs::read_to_string(linker_file_path).expect("linker file read").as_bytes()) .unwrap(); diff --git a/loader/src/debug.rs b/loader/src/debug.rs index fc8b34f3e..0bdb4f08a 100644 --- a/loader/src/debug.rs +++ b/loader/src/debug.rs @@ -4,6 +4,7 @@ pub struct Uart { } impl Uart { + #[cfg(feature="precursor")] pub fn putc(&self, c: u8) { let base = utra::uart::HW_UART_BASE as *mut u32; let mut uart = CSR::new(base); @@ -11,6 +12,13 @@ impl Uart { while uart.r(utra::uart::TXFULL) != 0 {} uart.wo(utra::uart::RXTX, c as u32) } + #[cfg(any(feature="cramium-soc", feature="cramium-fpga"))] + pub fn putc(&self, c: u8) { + let base = utra::duart::HW_DUART_BASE as *mut u32; + let mut uart = CSR::new(base); + while uart.r(utra::duart::SFR_SR) != 0 {} + uart.wo(utra::duart::SFR_TXD, c as u32); + } } use core::fmt::{Error, Write}; diff --git a/loader/src/main.rs b/loader/src/main.rs index 77b0e02be..db23ae99b 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -69,6 +69,16 @@ pub struct PageTable { /// This function is safe to call exactly once. #[export_name = "rust_entry"] pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) -> ! { + // + // YOU WILL FORGET THIS -- but I haven't figured out a good way to describe this scenario + // when cross-building a SoC image (for s32 sim) to run on the FPGA variant, + // you need to comment this out. or else, the simulation will hang. But really, the SoC + // should be the SoC, the FPGA should be the FPGA...the cross-sim is not really guaranteed to work + // but it is just a handy tool to use when it *does* work. + // + #[cfg(feature="cramium-soc")] + crate::platform::early_init(); // sets up PLLs so we're not running at 16MHz... + // initially validate the whole image on disk (including kernel args) // kernel args must be validated because tampering with them can change critical assumptions about // how data is loaded into memory @@ -222,9 +232,12 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! { #[cfg(not(feature = "atsama5d27"))] { - use utralib::generated::*; - let mut gpio_csr = CSR::new(utra::gpio::HW_GPIO_BASE as *mut u32); - gpio_csr.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // patch us over to a different UART for debug (1=LOG 2=APP, 0=KERNEL(hw reset default)) + #[cfg(not(feature="cramium-soc"))] + { // uart mux only exists on the FPGA variant + use utralib::generated::*; + let mut gpio_csr = CSR::new(utra::gpio::HW_GPIO_BASE as *mut u32); + gpio_csr.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // patch us over to a different UART for debug (1=LOG 2=APP, 0=KERNEL(hw reset default)) + } start_kernel( arg_offset, diff --git a/loader/src/platform/cramium/cramium.rs b/loader/src/platform/cramium/cramium.rs index 16a6f822e..46f54c7d6 100644 --- a/loader/src/platform/cramium/cramium.rs +++ b/loader/src/platform/cramium/cramium.rs @@ -13,7 +13,7 @@ pub mod duart { pub const UART_BUSY: utralib::Register = utralib::Register::new(2, 1); pub const UART_BUSY_BUSY: utralib::Field = utralib::Field::new(1, 0, UART_BUSY); - pub const HW_DUART_BASE: usize = 0x4000_1000; + pub const HW_DUART_BASE: usize = 0x4004_2000; } #[cfg(feature="platform-tests")] struct Duart { @@ -58,4 +58,40 @@ fn test_duart() { #[cfg(feature="platform-tests")] pub fn platform_tests() { test_duart(); +} + +#[cfg(feature="cramium-soc")] +pub fn early_init() { + unsafe { + (0x400400a0 as *mut u32).write_volatile(0x1F598); // F + crate::println!("F: {:08x}", ((0x400400a0 as *const u32).read_volatile())); + let poke_array: [(u32, u32, bool); 12] = [ + (0x400400a4, 0x2812, false), // MN + (0x400400a8, 0x3301, false), // Q + (0x40040090, 0x0032, true), // setpll + (0x40040014, 0x7f7f, false), // fclk + (0x40040018, 0x7f7f, false), // aclk + (0x4004001c, 0x3f3f, false), // hclk + (0x40040020, 0x1f1f, false), // iclk + (0x40040024, 0x0f0f, false), // pclk + (0x40040010, 0x0001, false), // sel0 + (0x4004002c, 0x0032, true), // setcgu + (0x40040060, 0x0003, false), // aclk gates + (0x40040064, 0x0003, false), // hclk gates + ]; + for &(addr, dat, is_u32) in poke_array.iter() { + let rbk = if is_u32 { + (addr as *mut u32).write_volatile(dat); + (addr as *const u32).read_volatile() + } else { + (addr as *mut u16).write_volatile(dat as u16); + (addr as *const u16).read_volatile() as u32 + }; + if dat != rbk { + crate::println!("{:08x}(w) != {:08x}(r)", dat, rbk); + } else { + crate::println!("{:08x} ok", dat); + } + } + } } \ No newline at end of file diff --git a/loader/src/platform/precursor/link.x b/loader/src/platform/precursor/link.x new file mode 100644 index 000000000..39021f109 --- /dev/null +++ b/loader/src/platform/precursor/link.x @@ -0,0 +1,190 @@ +MEMORY +{ + FLASH : ORIGIN = 0x20501000, LENGTH = 188k + FONTS : ORIGIN = 0x20530000, LENGTH = 4352k + RAM : ORIGIN = 0x40000000, LENGTH = 16M + MEMLCD : ORIGIN = 0xB0000000, LENGTH = 32k +} +/* +Fonts go from 0x2053_0000 to 0x2097_0000 +*/ + +REGION_ALIAS("REGION_TEXT", FLASH); +REGION_ALIAS("REGION_RODATA", FLASH); +REGION_ALIAS("REGION_DATA", RAM); +REGION_ALIAS("REGION_BSS", RAM); +REGION_ALIAS("REGION_STACK", RAM); +REGION_ALIAS("REGION_HEAP", RAM); + +_lcdfb = ORIGIN(MEMLCD); +/* Size of the main kernel stack */ +_stack_size = 16K; +_eheap = ORIGIN(RAM) + LENGTH(RAM); + + +PROVIDE(_stext = ORIGIN(REGION_TEXT)); +PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); +PROVIDE(_max_hart_id = 0); +PROVIDE(_hart_stack_size = 2K); +PROVIDE(_heap_size = 0); + +PROVIDE(trap_handler = default_trap_handler); + +/* # Pre-initialization function */ +/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, + then the function this points to will be called before the RAM is initialized. */ +PROVIDE(__pre_init = default_pre_init); + +/* # Multi-processing hook function + fn _mp_hook() -> bool; + + This function is called from all the harts and must return true only for one hart, + which will perform memory initialization. For other harts it must return false + and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). +*/ +PROVIDE(_mp_hook = default_mp_hook); + +ENTRY(_start) + +SECTIONS +{ + .text.dummy (NOLOAD) : + { + /* This section is intended to make _stext address work */ + . = ABSOLUTE(_stext); + } > REGION_TEXT + + .text _stext : + { + /* Put reset handler first in .text section so it ends up as the entry */ + /* point of the program. */ + KEEP(*(.text.init)); + KEEP(*(.init)); + KEEP(*(.init.rust)); + . = ALIGN(4); + KEEP(*(.trap)); + KEEP(*(.trap.rust)); + + *(.text .text.*); + } > REGION_TEXT + + .rodata : ALIGN(4) + { + *(.rodata .rodata.*); + + /* 4-byte align the end (VMA) of this section. + This is required by LLD to ensure the LMA of the following .data + section will have the correct alignment. */ + . = ALIGN(4); + _etext = .; + } > REGION_RODATA + + .data : ALIGN(4) + { + _sidata = LOADADDR(.data); + _sdata = .; + /* Must be called __global_pointer$ for linker relaxations to work. */ + PROVIDE(__global_pointer$ = . + 0x800); + *(.sdata .sdata.* .sdata2 .sdata2.*); + *(.data .data.*); + . = ALIGN(4); + _edata = .; + } > REGION_DATA AT > REGION_RODATA + + .fonts : ALIGN(65536) + { + KEEP(*(.fontdata)); + } > FONTS + + .bss (NOLOAD) : + { + _sbss = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _ebss = .; + } > REGION_BSS + + /* fictitious region that represents the memory available for the stack */ + .stack (NOLOAD) : + { + _sstack = .; + . += _stack_size; + . = ALIGN(4096); + _estack = .; + } > REGION_STACK + + /* fictitious region that represents the memory available for the heap */ + .heap (NOLOAD) : + { + . = ALIGN(4); + _sheap = .; + /* _eheap is defined elsewhere and is the remainder of RAM */ + } > REGION_HEAP + + /* fake output .got section */ + /* Dynamic relocations are unsupported. This section is only used to detect + relocatable code in the input files and raise an error if relocatable code + is found */ + .got (INFO) : + { + KEEP(*(.got .got.*)); + } + + /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ + /DISCARD/ : + { + *(.eh_frame); + *(.eh_frame_hdr); + } +} + +/* Do not exceed this mark in the error messages above | */ +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); + +ASSERT(_stext % 4 == 0, " +ERROR(riscv-rt): `_stext` must be 4-byte aligned"); + +ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " +BUG(riscv-rt): .data is not 4-byte aligned"); + +ASSERT(_sidata % 4 == 0, " +BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); + +ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " +BUG(riscv-rt): .bss is not 4-byte aligned"); + +ASSERT(_sheap % 4 == 0, " +BUG(riscv-rt): start of .heap is not 4-byte aligned"); + +ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " +ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. +Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); + +ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " +ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. +Consider changing `_max_hart_id` or `_hart_stack_size`."); + +ASSERT(SIZEOF(.got) == 0, " +.got section detected in the input files. Dynamic relocations are not +supported. If you are linking to C code compiled using the `gcc` crate +then modify your build script to compile the C code _without_ the +-fPIC flag. See the documentation of the `gcc::Config.fpic` method for +details."); + +/* Do not exceed this mark in the error messages above | */