debug the slot manager and rram APIs

This commit is contained in:
bunnie 2025-10-20 23:31:01 +08:00
parent 13348bf94a
commit 5c60de32ce
No known key found for this signature in database
2 changed files with 138 additions and 40 deletions

View file

@ -253,41 +253,55 @@ impl SlotManager {
// safety: the unsafes below remind us to check that all values are valid during // safety: the unsafes below remind us to check that all values are valid during
// the pointer type cast. In this case, there are no invalid values for a `u8`. // the pointer type cast. In this case, there are no invalid values for a `u8`.
Ok(match slot { Ok(match slot {
SlotIndex::Data(_, _) => unsafe { SlotIndex::Data(_, _, _) => unsafe {
let ptr: *const u8 = &self.data_range.as_slice::<u8>()[offset] as *const u8;
crate::println!("data loc: {:x}", ptr as usize);
&self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES] &self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES]
}, },
SlotIndex::Key(_, _) => unsafe { SlotIndex::Key(_, _, _) => unsafe {
let ptr: *const u8 = &self.data_range.as_slice::<u8>()[offset] as *const u8;
crate::println!("data loc: {:x}", ptr as usize);
&self.key_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES] &self.key_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES]
}, },
_ => return Err(AccessError::TypeError), SlotIndex::DataRange(range, _, _) => unsafe {
&self.data_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES * range.len()]
},
SlotIndex::KeyRange(range, _, _) => unsafe {
&self.key_range.as_slice()[offset..offset + SLOT_ELEMENT_LEN_BYTES * range.len()]
},
}) })
} else { } else {
Err(AccessError::AccessDenied) Err(AccessError::AccessDenied)
} }
} }
pub fn write( /// Safety: the caller must resolve the index into either the data or the key array correctly.
&self, /// There is access or type checking done. This is a "raw read" primitive mostly used for debugging.
writer: &mut Reram, pub unsafe fn read_data_slot(&self, absolute_offset: usize) -> &[u8] {
slot: &SlotIndex, let ptr: *const u8 = &self.data_range.as_slice::<u8>()[absolute_offset] as *const u8;
value: &[u8; SLOT_ELEMENT_LEN_BYTES], crate::println!("data loc: {:x}", ptr as usize);
) -> Result<(), AccessError> { &self.data_range.as_slice()[absolute_offset..absolute_offset + SLOT_ELEMENT_LEN_BYTES]
}
/// Safety: the caller must resolve the index into either the data or the key array correctly.
/// There is access or type checking done. This is a "raw read" primitive mostly used for debugging.
pub unsafe fn read_key_slot(&self, absolute_offset: usize) -> &[u8] {
let ptr: *const u8 = &self.key_range.as_slice::<u8>()[absolute_offset] as *const u8;
crate::println!("key loc: {:x}", ptr as usize);
&self.key_range.as_slice()[absolute_offset..absolute_offset + SLOT_ELEMENT_LEN_BYTES]
}
pub fn write(&self, writer: &mut Reram, slot: &SlotIndex, value: &[u8]) -> Result<(), AccessError> {
let acl = self.get_acl(slot)?; let acl = self.get_acl(slot)?;
if self.user_id.is_accessible(&acl, &AccessType::Write) { if self.user_id.is_accessible(&acl, &AccessType::Write) {
let offset = slot.try_into_data_offset()?; let offset = slot.try_into_data_offset()?;
let range = match slot { let range = match slot {
SlotIndex::Data(_, _) => &self.data_range, SlotIndex::Data(_, _, _) | SlotIndex::DataRange(_, _, _) => &self.data_range,
SlotIndex::Key(_, _) => &self.key_range, SlotIndex::Key(_, _, _) | SlotIndex::KeyRange(_, _, _) => &self.key_range,
_ => return Err(AccessError::TypeError),
}; };
if value.len() != slot.len() * SLOT_ELEMENT_LEN_BYTES {
return Err(AccessError::SizeError);
}
writer writer
.protected_write_slice(range.as_ptr() as usize + offset, value) .protected_write_slice(range.as_ptr() as usize + offset - utralib::HW_RERAM_MEM, value)
.map_err(|_| AccessError::WriteError)?; .map_err(|_| AccessError::WriteError)?;
crate::cache_flush(); crate::cache_flush();
} }
@ -302,49 +316,129 @@ impl SlotManager {
} }
pub fn get_acl(&self, slot: &SlotIndex) -> Result<AccessSettings, AccessError> { pub fn get_acl(&self, slot: &SlotIndex) -> Result<AccessSettings, AccessError> {
let offset = slot.try_into_acl_offset()?;
// safety: the unsafe blocks here are concerned that the data types of the resulting // safety: the unsafe blocks here are concerned that the data types of the resulting
// slice are all representable. In this case, all bits are valid in the final representation. // slice are all representable. In this case, all bits are valid in the final representation.
Ok(match slot { Ok(match slot {
SlotIndex::Data(_, _) => { SlotIndex::Data(_, _, _) => {
let offset = slot.try_into_acl_offset()?;
AccessSettings::Data(DataSlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe { AccessSettings::Data(DataSlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.data_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap() self.data_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap()
}))) })))
} }
SlotIndex::Key(_, _) => { SlotIndex::Key(_, _, _) => {
let offset = slot.try_into_acl_offset()?;
AccessSettings::Key(KeySlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe { AccessSettings::Key(KeySlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.key_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap() self.key_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap()
}))) })))
} }
_ => return Err(AccessError::TypeError), SlotIndex::DataRange(_, _, _) => {
let mut range = slot.try_into_acl_iter()?;
let offset = range.next().unwrap();
let prototype = DataSlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.data_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap()
}));
for offset in range {
let successor = DataSlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.data_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap()
}));
if prototype != successor {
return Err(AccessError::DataAclInconsistency(prototype));
}
}
AccessSettings::Data(prototype)
}
SlotIndex::KeyRange(_, _, _) => {
let mut range = slot.try_into_acl_iter()?;
let offset = range.next().unwrap();
let prototype = KeySlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.key_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap()
}));
for offset in range {
let successor = KeySlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.key_acl_range.as_slice()[offset..offset + size_of::<u32>()].try_into().unwrap()
}));
if prototype != successor {
return Err(AccessError::KeyAclInconsistency(prototype));
}
}
AccessSettings::Key(prototype)
}
}) })
} }
/// Safety: the caller must resolve the index into either the data or the key array correctly.
/// There is access or type checking done. This is a "raw read" primitive mostly used for debugging.
pub unsafe fn get_data_acl(&self, absolute_offset: usize) -> DataSlotAccess {
DataSlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.data_acl_range.as_slice()[absolute_offset..absolute_offset + size_of::<u32>()]
.try_into()
.unwrap()
}))
}
/// Safety: the caller must resolve the index into either the data or the key array correctly.
/// There is access or type checking done. This is a "raw read" primitive mostly used for debugging.
pub unsafe fn get_key_acl(&self, absolute_offset: usize) -> KeySlotAccess {
KeySlotAccess::new_with_raw_value(u32::from_le_bytes(unsafe {
self.key_acl_range.as_slice()[absolute_offset..absolute_offset + size_of::<u32>()]
.try_into()
.unwrap()
}))
}
pub fn set_acl( pub fn set_acl(
&self, &self,
writer: &mut Reram, writer: &mut Reram,
slot: &SlotIndex, slot: &SlotIndex,
setting: &AccessSettings, setting: &AccessSettings,
) -> Result<(), AccessError> { ) -> Result<(), AccessError> {
let offset = slot.try_into_acl_offset()?;
// safety: the unsafe blocks here are concerned that the data types of the resulting // safety: the unsafe blocks here are concerned that the data types of the resulting
// slice are all representable. In this case, all bits are valid in the final representation. // slice are all representable. In this case, all bits are valid in the final representation.
match slot { match slot {
SlotIndex::Data(_, _) => writer SlotIndex::Data(_, _, _) => {
.protected_write_slice( let offset = slot.try_into_acl_offset()?;
self.data_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset, writer
&setting.raw_u32().to_le_bytes(), .protected_write_slice(
) self.data_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset,
.map(|_| ()) &setting.raw_u32().to_le_bytes(),
.map_err(|_| AccessError::WriteError), )
SlotIndex::Key(_, _) => writer .map(|_| ())
.protected_write_slice( .map_err(|_| AccessError::WriteError)
self.key_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset, }
&setting.raw_u32().to_le_bytes(), SlotIndex::Key(_, _, _) => {
) let offset = slot.try_into_acl_offset()?;
.map(|_| ()) writer
.map_err(|_| AccessError::WriteError), .protected_write_slice(
_ => return Err(AccessError::TypeError), self.key_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset,
&setting.raw_u32().to_le_bytes(),
)
.map(|_| ())
.map_err(|_| AccessError::WriteError)
}
SlotIndex::DataRange(_, _, _) => {
for offset in slot.try_into_acl_iter()? {
writer
.protected_write_slice(
self.data_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset,
&setting.raw_u32().to_le_bytes(),
)
.map(|_| ())
.map_err(|_| AccessError::WriteError)?
}
Ok(())
}
SlotIndex::KeyRange(_, _, _) => {
for offset in slot.try_into_acl_iter()? {
writer
.protected_write_slice(
self.key_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset,
&setting.raw_u32().to_le_bytes(),
)
.map(|_| ())
.map_err(|_| AccessError::WriteError)?
}
Ok(())
}
} }
} }
} }

View file

@ -148,7 +148,7 @@ impl<'a> Reram {
let mut buffer = AlignedBuffer([0u8; ALIGNMENT]); let mut buffer = AlignedBuffer([0u8; ALIGNMENT]);
// ragged start // ragged start
let start_len = ALIGNMENT - (offset % ALIGNMENT); let start_len = (ALIGNMENT - (offset % ALIGNMENT)) % ALIGNMENT;
if start_len != 0 { if start_len != 0 {
let start_offset = offset & !(ALIGNMENT - 1); let start_offset = offset & !(ALIGNMENT - 1);
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
@ -172,7 +172,11 @@ impl<'a> Reram {
}; };
// populate from old data first // populate from old data first
buffer.0.copy_from_slice(&dest_slice); buffer.0.copy_from_slice(&dest_slice);
buffer.0[offset % ALIGNMENT..].copy_from_slice(&data[..start_len]); for (dst, &src) in
buffer.0[offset % ALIGNMENT..].iter_mut().zip(data[..start_len.min(data.len())].iter())
{
*dst = src;
}
// safe because alignment and buffer sizes are guaranteed // safe because alignment and buffer sizes are guaranteed
unsafe { unsafe {
self.write_u32_aligned(start_offset, buffer.as_slice_u32()); self.write_u32_aligned(start_offset, buffer.as_slice_u32());
@ -181,7 +185,7 @@ impl<'a> Reram {
// aligned middle & end // aligned middle & end
let mut cur_offset = offset + start_len; let mut cur_offset = offset + start_len;
if data.len() - start_len > 0 { if data.len().saturating_sub(start_len) > 0 {
for chunk in data[start_len..].chunks(buffer.0.len()) { for chunk in data[start_len..].chunks(buffer.0.len()) {
// full chunk // full chunk
if chunk.len() == buffer.0.len() { if chunk.len() == buffer.0.len() {