From f8978d7511190f76a1c1ea09393ce8c7500afdb7 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 11 May 2025 12:27:41 +0800 Subject: [PATCH] move platform-specific locations into the xous-rs crates This enables a couple of things that have been long wanting, including: - the ability to know what PAGE_SIZE is from a single central define - the ability to determine where certain virtual memory ranges are located (useful for the userspace swapper). Previously these were "hidden" from crates because they were embedded directly in the kernel implementation module, which is not accessible as a dependency, thus causing us to e.g. create redundant copies of the PAGE_SIZE variable. This hopefully makes migrating to a larger page size in some future version of the OS a bit simpler. --- xous-rs/src/arch/arm/mod.rs | 2 ++ xous-rs/src/arch/hosted/mem.rs | 38 +++++++++++++++++++++++++++- xous-rs/src/arch/riscv/mem.rs | 38 ++++++++++++++++++++++++++++ xous-rs/src/definitions.rs | 45 ---------------------------------- 4 files changed, 77 insertions(+), 46 deletions(-) diff --git a/xous-rs/src/arch/arm/mod.rs b/xous-rs/src/arch/arm/mod.rs index a62ecf051..b931c6e71 100644 --- a/xous-rs/src/arch/arm/mod.rs +++ b/xous-rs/src/arch/arm/mod.rs @@ -10,6 +10,8 @@ pub use syscall::*; pub mod process; pub use process::*; +pub const PAGE_SIZE: usize = 4096; + pub struct ProcessHandle { pub pid: crate::PID, pub cid: crate::CID, diff --git a/xous-rs/src/arch/hosted/mem.rs b/xous-rs/src/arch/hosted/mem.rs index 79d5889ae..466768d85 100644 --- a/xous-rs/src/arch/hosted/mem.rs +++ b/xous-rs/src/arch/hosted/mem.rs @@ -1,5 +1,41 @@ use crate::{Error, MemoryAddress, MemoryFlags, MemoryRange}; -const PAGE_SIZE: usize = 4096; + +pub const PAGE_SIZE: usize = 4096; +pub const DEFAULT_HEAP_BASE: usize = 0x2000_0000; +pub const DEFAULT_MESSAGE_BASE: usize = 0x4000_0000; +pub const DEFAULT_BASE: usize = 0x6000_0000; + +// open a large aperture from A000-E000 for a potential RAM-mapped swap area: this gives us up to 1GiB swap +// space. Please don't actually use all of it: performance will be unimaginably bad. Note that the +// A000-E000 range is also shared with the MMAP virtual region. +pub const SWAP_HAL_VADDR: usize = 0xa000_0000; +pub const MMAP_VIRT_BASE: usize = 0xb000_0000; + +pub const USER_AREA_END: usize = 0xff00_0000; +pub const EXCEPTION_STACK_TOP: usize = 0xffff_0000; +pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000; +pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000; +pub const THREAD_CONTEXT_AREA: usize = 0xff80_1000; + +pub const FLG_VALID: usize = 0x1; +pub const FLG_R: usize = 0x2; +pub const FLG_W: usize = 0x4; +pub const FLG_X: usize = 0x8; +pub const FLG_U: usize = 0x10; // User +pub const FLG_A: usize = 0x40; +pub const FLG_D: usize = 0x80; // Dirty (explicitly managed, not automatic) +pub const FLG_S: usize = 0x100; // Shared +pub const FLG_P: usize = 0x200; // swaP + +/// swap-specific flags +pub const SWAP_FLG_WIRED: u32 = 0x1_00; +pub const SWAP_PT_VADDR: usize = 0xE000_0000; +// E000_0000 - E100_0000 => 16 MiB of vaddr space for page tables; should be more than enough +pub const SWAP_CFG_VADDR: usize = 0xE100_0000; +pub const SWAP_RPT_VADDR: usize = 0xE100_1000; +pub const SWAP_COUNT_VADDR: usize = 0xE110_0000; +pub const SWAP_APP_UART_VADDR: usize = 0xE180_0000; +pub const SWAP_APP_UART_IFRAM_VADDR: usize = 0xE180_1000; extern crate alloc; use alloc::alloc::{Layout, alloc, dealloc}; diff --git a/xous-rs/src/arch/riscv/mem.rs b/xous-rs/src/arch/riscv/mem.rs index 2dac71fcb..f7c330a87 100644 --- a/xous-rs/src/arch/riscv/mem.rs +++ b/xous-rs/src/arch/riscv/mem.rs @@ -1,5 +1,43 @@ use crate::{Error, MemoryAddress, MemoryFlags, MemoryRange}; +// pub const DEFAULT_STACK_TOP: usize = 0x8000_0000; +pub const DEFAULT_HEAP_BASE: usize = 0x2000_0000; +pub const DEFAULT_MESSAGE_BASE: usize = 0x4000_0000; +pub const DEFAULT_BASE: usize = 0x6000_0000; + +// open a large aperture from A000-E000 for a potential RAM-mapped swap area: this gives us up to 1GiB swap +// space. Please don't actually use all of it: performance will be unimaginably bad. Note that the +// A000-E000 range is also shared with the MMAP virtual region. +pub const SWAP_HAL_VADDR: usize = 0xa000_0000; +pub const MMAP_VIRT_BASE: usize = 0xb000_0000; + +pub const USER_AREA_END: usize = 0xff00_0000; +pub const EXCEPTION_STACK_TOP: usize = 0xffff_0000; +pub const PAGE_SIZE: usize = 4096; +pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000; +pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000; +pub const THREAD_CONTEXT_AREA: usize = 0xff80_1000; + +pub const FLG_VALID: usize = 0x1; +pub const FLG_R: usize = 0x2; +pub const FLG_W: usize = 0x4; +pub const FLG_X: usize = 0x8; +pub const FLG_U: usize = 0x10; // User +pub const FLG_A: usize = 0x40; +pub const FLG_D: usize = 0x80; // Dirty (explicitly managed, not automatic) +pub const FLG_S: usize = 0x100; // Shared +pub const FLG_P: usize = 0x200; // swaP + +/// swap-specific flags +pub const SWAP_FLG_WIRED: u32 = 0x1_00; +pub const SWAP_PT_VADDR: usize = 0xE000_0000; +// E000_0000 - E100_0000 => 16 MiB of vaddr space for page tables; should be more than enough +pub const SWAP_CFG_VADDR: usize = 0xE100_0000; +pub const SWAP_RPT_VADDR: usize = 0xE100_1000; +pub const SWAP_COUNT_VADDR: usize = 0xE110_0000; +pub const SWAP_APP_UART_VADDR: usize = 0xE180_0000; +pub const SWAP_APP_UART_IFRAM_VADDR: usize = 0xE180_1000; + pub fn map_memory_pre( _phys: &Option, _virt: &Option, diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 65c53af0f..849a2e503 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -10,51 +10,6 @@ pub type Connection = usize; pub const MAX_CID: usize = 34; -pub const FLASH_PHYS_BASE: u32 = 0x2000_0000; -pub const SOC_REGION_LOC: u32 = 0x0000_0000; -pub const SOC_REGION_LEN: u32 = 0x00D0_0000; // gw + staging + loader + kernel - -// note to self: if these locations change, be sure to update the "filters" addresses -// in the gateware, so that we are consistent on what parts of the SPINOR are allowed access via USB debug -pub const SOC_MAIN_GW_LOC: u32 = 0x0000_0000; // gateware - primary loading address -pub const SOC_MAIN_GW_LEN: u32 = 0x0028_0000; -pub const SOC_STAGING_GW_LOC: u32 = 0x0028_0000; // gateware - staging copy -pub const SOC_STAGING_GW_LEN: u32 = 0x0028_0000; - -pub const LOADER_LOC: u32 = 0x0050_0000; // loader - base -pub const LOADER_CODE_LEN: u32 = 0x0003_0000; // code region only -pub const LOADER_FONT_LOC: u32 = 0x0053_0000; // should be the same as graphics-server/src/fontmap.rs/FONT_BASE -pub const LOADER_FONT_LEN: u32 = 0x0044_0000; // length of font region only -pub const LOADER_TOTAL_LEN: u32 = LOADER_CODE_LEN + LOADER_FONT_LEN; // code + font - -pub const EARLY_SETTINGS: u32 = 0x0097_0000; - -pub const KERNEL_LOC: u32 = 0x0098_0000; // kernel start -pub const KERNEL_LEN: u32 = 0x0140_0000; // max kernel length = 0xA0_0000 * 2 => half the area for backup kernel & updates -pub const KERNEL_BACKUP_OFFSET: u32 = KERNEL_LEN - 0x1000; // last page of kernel is where the backup block gets located = 0x1D7_F000 - -pub const EC_REGION_LOC: u32 = 0x07F8_0000; // EC update staging area. Must be aligned to a 64k-address. -pub const EC_WF200_PKG_LOC: u32 = 0x07F8_0000; -pub const EC_WF200_PKG_LEN: u32 = 0x0004_E000; -pub const EC_FW_PKG_LOC: u32 = 0x07FC_E000; -pub const EC_FW_PKG_LEN: u32 = 0x0003_2000; -pub const EC_REGION_LEN: u32 = 0x0008_0000; - -pub const PDDB_LOC: u32 = 0x01D8_0000; // PDDB start -pub const PDDB_LEN: u32 = EC_REGION_LOC - PDDB_LOC; // must be 64k-aligned (bulk erase block size) for proper function. - -// quantum alloted to each process before a context switch is forced -pub const BASE_QUANTA_MS: u32 = 10; - -// sentinel used by test infrastructure to assist with parsing -// The format of any test infrastructure output to recover is as follows: -// _|TT|_,,_|TE|_ -// where _|TT|_ and _|TE|_ are bookends around the data to be reported -// is a single-word identifier that routes the data to a given parser -// is free-form data, which will be split at comma boundaries by the parser -pub const BOOKEND_START: &str = "_|TT|_"; -pub const BOOKEND_END: &str = "_|TE|_"; - /// Hard-wired PID of the swapper #[cfg(feature = "swap")] pub const SWAPPER_PID: u8 = 2;