From e0c8cc1da4ead8c69586349930b2423f626251d7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 17 Jul 2023 18:23:17 +0800 Subject: [PATCH] add cramium to loader target --- loader/build.rs | 16 +- loader/src/platform/cramium.rs | 62 +------- loader/src/platform/cramium/cramium.rs | 61 ++++++++ loader/src/platform/cramium/link-fpga.x | 188 ++++++++++++++++++++++++ loader/src/platform/cramium/link-soc.x | 188 ++++++++++++++++++++++++ 5 files changed, 452 insertions(+), 63 deletions(-) create mode 100644 loader/src/platform/cramium/cramium.rs create mode 100644 loader/src/platform/cramium/link-fpga.x create mode 100644 loader/src/platform/cramium/link-soc.x diff --git a/loader/build.rs b/loader/build.rs index 27c93aefb..83b8c120b 100644 --- a/loader/build.rs +++ b/loader/build.rs @@ -10,9 +10,21 @@ fn main() { let linker_file_path = if target.starts_with("riscv") { println!("cargo:rustc-link-search={}", out_dir.display()); - println!("cargo:rustc-link-arg=-Tlink.x"); + #[cfg(feature="cramium-soc")] + println!("cargo:rustc-link-arg=-Tlink-soc.x"); + #[cfg(feature="cramium-soc")] + let p = PathBuf::from("src/platform/cramium/link-soc.x"); + #[cfg(feature="cramium-fpga")] + println!("cargo:rustc-link-arg=-Tlink-fpga.x"); + #[cfg(feature="cramium-fpga")] + let p = PathBuf::from("src/platform/cramium/link-fpga.x"); - PathBuf::from("link.x") + #[cfg(not(any(feature="cramium-fpga", feature="cramium-soc")))] + println!("cargo:rustc-link-arg=-Tlink.x"); + #[cfg(not(any(feature="cramium-fpga", feature="cramium-soc")))] + let p = PathBuf::from("link.x"); + + p } else if target.starts_with("armv7a") { let name = env::var("CARGO_PKG_NAME").unwrap(); diff --git a/loader/src/platform/cramium.rs b/loader/src/platform/cramium.rs index 16a6f822e..092a1e6d4 100644 --- a/loader/src/platform/cramium.rs +++ b/loader/src/platform/cramium.rs @@ -1,61 +1 @@ -pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_MEM_LEN; -pub const RAM_BASE: usize = utralib::generated::HW_SRAM_MEM; - -// 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 = 0x9000; - -#[cfg(feature="platform-tests")] -pub mod duart { - pub const UART_DOUT: utralib::Register = utralib::Register::new(0, 0xff); - pub const UART_DOUT_DOUT: utralib::Field = utralib::Field::new(8, 0, UART_DOUT); - pub const UART_CTL: utralib::Register = utralib::Register::new(1, 1); - pub const UART_CTL_EN: utralib::Field = utralib::Field::new(1, 0, UART_CTL); - 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; -} -#[cfg(feature="platform-tests")] -struct Duart { - csr: utralib::CSR::, -} -#[cfg(feature="platform-tests")] -impl Duart { - pub fn new() -> Self { - let mut duart_csr = utralib::CSR::new(duart::HW_DUART_BASE as *mut u32); - duart_csr.wfo(duart::UART_CTL_EN, 1); - Duart { - csr: duart_csr, - } - } - pub fn putc(&mut self, ch: char) { - while self.csr.rf(duart::UART_BUSY_BUSY) != 0 { - // spin wait - } - // the code here bypasses a lot of checks to simulate very fast write cycles so - // that the read waitback actually returns something other than not busy. - // unsafe {(duart::HW_DUART_BASE as *mut u32).write_volatile(ch as u32) }; // this line really ensures we have to readback something, but it causes double-printing - while unsafe{(duart::HW_DUART_BASE as *mut u32).add(2).read_volatile()} != 0 { - // wait - } - unsafe {(duart::HW_DUART_BASE as *mut u32).write_volatile(ch as u32) }; - } - pub fn puts(&mut self, s: &str) { - for c in s.as_bytes() { - self.putc(*c as char); - } - } -} -#[cfg(feature="platform-tests")] -fn test_duart() { - // println!("Duart test\n"); - let mut duart = Duart::new(); - loop { - duart.puts("hello world\n"); - } -} - -#[cfg(feature="platform-tests")] -pub fn platform_tests() { - test_duart(); -} \ No newline at end of file +pub mod cramium; diff --git a/loader/src/platform/cramium/cramium.rs b/loader/src/platform/cramium/cramium.rs new file mode 100644 index 000000000..16a6f822e --- /dev/null +++ b/loader/src/platform/cramium/cramium.rs @@ -0,0 +1,61 @@ +pub const RAM_SIZE: usize = utralib::generated::HW_SRAM_MEM_LEN; +pub const RAM_BASE: usize = utralib::generated::HW_SRAM_MEM; + +// 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 = 0x9000; + +#[cfg(feature="platform-tests")] +pub mod duart { + pub const UART_DOUT: utralib::Register = utralib::Register::new(0, 0xff); + pub const UART_DOUT_DOUT: utralib::Field = utralib::Field::new(8, 0, UART_DOUT); + pub const UART_CTL: utralib::Register = utralib::Register::new(1, 1); + pub const UART_CTL_EN: utralib::Field = utralib::Field::new(1, 0, UART_CTL); + 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; +} +#[cfg(feature="platform-tests")] +struct Duart { + csr: utralib::CSR::, +} +#[cfg(feature="platform-tests")] +impl Duart { + pub fn new() -> Self { + let mut duart_csr = utralib::CSR::new(duart::HW_DUART_BASE as *mut u32); + duart_csr.wfo(duart::UART_CTL_EN, 1); + Duart { + csr: duart_csr, + } + } + pub fn putc(&mut self, ch: char) { + while self.csr.rf(duart::UART_BUSY_BUSY) != 0 { + // spin wait + } + // the code here bypasses a lot of checks to simulate very fast write cycles so + // that the read waitback actually returns something other than not busy. + // unsafe {(duart::HW_DUART_BASE as *mut u32).write_volatile(ch as u32) }; // this line really ensures we have to readback something, but it causes double-printing + while unsafe{(duart::HW_DUART_BASE as *mut u32).add(2).read_volatile()} != 0 { + // wait + } + unsafe {(duart::HW_DUART_BASE as *mut u32).write_volatile(ch as u32) }; + } + pub fn puts(&mut self, s: &str) { + for c in s.as_bytes() { + self.putc(*c as char); + } + } +} +#[cfg(feature="platform-tests")] +fn test_duart() { + // println!("Duart test\n"); + let mut duart = Duart::new(); + loop { + duart.puts("hello world\n"); + } +} + +#[cfg(feature="platform-tests")] +pub fn platform_tests() { + test_duart(); +} \ No newline at end of file diff --git a/loader/src/platform/cramium/link-fpga.x b/loader/src/platform/cramium/link-fpga.x new file mode 100644 index 000000000..8b0e921ca --- /dev/null +++ b/loader/src/platform/cramium/link-fpga.x @@ -0,0 +1,188 @@ +MEMORY +{ + /* we are using an unsigned loader, so the offset is not 1k offset */ + FLASH : ORIGIN = 0x60500000, LENGTH = 32k + RAM : ORIGIN = 0x61000000, LENGTH = 16M +} +/* +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); + +/* 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 | */ diff --git a/loader/src/platform/cramium/link-soc.x b/loader/src/platform/cramium/link-soc.x new file mode 100644 index 000000000..bd9a776b8 --- /dev/null +++ b/loader/src/platform/cramium/link-soc.x @@ -0,0 +1,188 @@ +MEMORY +{ + /* we are using an unsigned loader, so the offset is not 1k offset */ + FLASH : ORIGIN = 0x60000000, LENGTH = 4M + RAM : ORIGIN = 0x61000000, LENGTH = 2M +} +/* +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); + +/* 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 | */