fix one way counter offset calculations

This commit is contained in:
bunnie 2025-10-06 23:24:42 +08:00
parent dbcf346ffb
commit 7d6630b7ec

View file

@ -9,9 +9,11 @@ pub const ACRAM_DATASLOT_START: usize = 0x603D_C000;
pub const ACRAM_KEYSLOT_START: usize = 0x603D_E000; pub const ACRAM_KEYSLOT_START: usize = 0x603D_E000;
// pub const ACRAM_GKEYSLOT_START: usize = 0x603D_E400; // This is mentioned in the docs but I don't see it in // pub const ACRAM_GKEYSLOT_START: usize = 0x603D_E400; // This is mentioned in the docs but I don't see it in
// the code? // the code?
pub const ONEWAY_START: usize = 0x603D_A000; pub const ONEWAY_START: usize = 0x603D_A000; // page with 128 counters
pub const ONEWAY2_START: usize = 0x603D_B000; pub const ONEWAY2_START: usize = 0x603D_B000; // page with another 128 counters
pub const ONEWAY_LEN: usize = 2048; // in u32-sized elements const ONEWAY_LEN: usize = 256 / 8; // in bytes
const COUNTER_STRIDE_U32: usize = ONEWAY_LEN / size_of::<u32>();
pub const MAX_ONEWAY_COUNTERS: usize = 8192 / ONEWAY_LEN;
pub const CODESEL_END: usize = 0x603D_A000; pub const CODESEL_END: usize = 0x603D_A000;
#[bitfield(u32)] #[bitfield(u32)]
@ -94,7 +96,6 @@ pub struct OneWayCounter {
#[cfg(feature = "std")] #[cfg(feature = "std")]
mapping: MemoryRange, mapping: MemoryRange,
} }
const COUNTER_STRIDE_U32: usize = 8;
impl OneWayCounter { impl OneWayCounter {
pub fn new() -> Self { pub fn new() -> Self {
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
@ -103,9 +104,9 @@ impl OneWayCounter {
#[cfg(feature = "std")] #[cfg(feature = "std")]
let ret = OneWayCounter { let ret = OneWayCounter {
mapping: xous::syscall::map_memory( mapping: xous::syscall::map_memory(
xous::MemoryAddress::new(ONEWAY2_START), xous::MemoryAddress::new(ONEWAY_START),
None, None,
ONEWAY_LEN * size_of::<u32>(), ONEWAY_LEN * MAX_ONEWAY_COUNTERS,
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
) )
.expect("couldn't map oneway range"), .expect("couldn't map oneway range"),
@ -119,7 +120,7 @@ impl OneWayCounter {
let base = ONEWAY_START as *const u32; let base = ONEWAY_START as *const u32;
#[cfg(feature = "std")] #[cfg(feature = "std")]
let base = self.mapping.as_ptr() as *const u32; let base = self.mapping.as_ptr() as *const u32;
if offset < ONEWAY_LEN { if offset < MAX_ONEWAY_COUNTERS {
// safety: only safe because the pointer is length-checked // safety: only safe because the pointer is length-checked
// we use this form to access the array because we need to read_volatile() // we use this form to access the array because we need to read_volatile()
Ok(unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() }) Ok(unsafe { base.add(offset * COUNTER_STRIDE_U32).read_volatile() })
@ -139,7 +140,7 @@ impl OneWayCounter {
#[cfg(feature = "std")] #[cfg(feature = "std")]
let base = self.mapping.as_mut_ptr() as *mut u32; let base = self.mapping.as_mut_ptr() as *mut u32;
if offset < ONEWAY_LEN { if offset < MAX_ONEWAY_COUNTERS {
let starting_value = self.get(offset).unwrap(); // offset is already checked let starting_value = self.get(offset).unwrap(); // offset is already checked
// this will cause the increment in hardware // this will cause the increment in hardware
unsafe { base.add(offset * COUNTER_STRIDE_U32).write_volatile(0) } unsafe { base.add(offset * COUNTER_STRIDE_U32).write_volatile(0) }
@ -165,7 +166,7 @@ impl OneWayCounter {
let base = self.mapping.as_mut_ptr() as *mut u32; let base = self.mapping.as_mut_ptr() as *mut u32;
let offset = T::OFFSET; let offset = T::OFFSET;
if offset < ONEWAY_LEN { if offset < MAX_ONEWAY_COUNTERS {
let starting_value = self.get(offset).unwrap(); // offset is already checked let starting_value = self.get(offset).unwrap(); // offset is already checked
// this will cause the increment in hardware // this will cause the increment in hardware
unsafe { base.add(offset * COUNTER_STRIDE_U32).write_volatile(0) } unsafe { base.add(offset * COUNTER_STRIDE_U32).write_volatile(0) }