From 34eca151ccb5e3a6bf68708728d39b1424e78609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Wed, 4 Sep 2024 14:46:08 +0100 Subject: [PATCH 1/2] RISC-V: Reorganise module declarations and re-exports --- src/riscv/lib/src/state_backend.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/riscv/lib/src/state_backend.rs b/src/riscv/lib/src/state_backend.rs index b594906adf8e..dd99dce55e88 100644 --- a/src/riscv/lib/src/state_backend.rs +++ b/src/riscv/lib/src/state_backend.rs @@ -55,25 +55,20 @@ //! [Layouts]: Layout //! [Locations]: Location -pub mod memory_backend; - -mod layout; -pub use layout::*; - mod alloc; -pub use alloc::*; - -mod region; -pub use region::*; - -mod enums; -pub use enums::*; - +mod effects; mod elems; -pub use elems::*; +mod enums; +mod layout; +pub mod memory_backend; +mod region; -mod effects; +pub use alloc::*; pub use effects::*; +pub use elems::*; +pub use enums::*; +pub use layout::*; +pub use region::*; /// Manager of the state backend storage pub trait ManagerBase { -- GitLab From c5b1724f75bc3a3883af14f80d49f01277b2d947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Sat, 7 Sep 2024 00:06:34 +0100 Subject: [PATCH 2/2] RISC-V: Introduce owned regions --- src/riscv/lib/src/state_backend.rs | 1 + .../lib/src/state_backend/owned_backend.rs | 166 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 src/riscv/lib/src/state_backend/owned_backend.rs diff --git a/src/riscv/lib/src/state_backend.rs b/src/riscv/lib/src/state_backend.rs index dd99dce55e88..d7a7f5e8a93e 100644 --- a/src/riscv/lib/src/state_backend.rs +++ b/src/riscv/lib/src/state_backend.rs @@ -61,6 +61,7 @@ mod elems; mod enums; mod layout; pub mod memory_backend; +pub mod owned_backend; mod region; pub use alloc::*; diff --git a/src/riscv/lib/src/state_backend/owned_backend.rs b/src/riscv/lib/src/state_backend/owned_backend.rs new file mode 100644 index 000000000000..384f93aba8bf --- /dev/null +++ b/src/riscv/lib/src/state_backend/owned_backend.rs @@ -0,0 +1,166 @@ +// SPDX-FileCopyrightText: 2024 TriliTech +// +// SPDX-License-Identifier: MIT + +use super::{ + AllocatedOf, Elem, Layout, Location, ManagerAlloc, ManagerBase, ManagerRead, ManagerReadWrite, + ManagerWrite, +}; +use std::mem; + +/// Manager that allows state binders to own the state storage +pub struct Owned; + +impl Owned { + /// Allocate regions for the given layout. + pub fn allocate() -> AllocatedOf { + let places = L::placed(); + L::allocate(&mut Self, places.into_location()) + } +} + +impl ManagerBase for Owned { + type Region = [E; LEN]; + + type DynRegion = Box<[u8; LEN]>; +} + +impl ManagerAlloc for Owned { + fn allocate_region( + &mut self, + _loc: Location<[E; LEN]>, + ) -> Self::Region { + unsafe { std::mem::zeroed() } + } + + fn allocate_dyn_region( + &mut self, + _loc: Location<[u8; LEN]>, + ) -> Self::DynRegion { + unsafe { + let layout = std::alloc::Layout::new::<[u8; LEN]>(); + let alloc = std::alloc::alloc_zeroed(layout); + Box::from_raw(alloc.cast()) + } + } +} + +impl ManagerRead for Owned { + fn region_read(region: &Self::Region, index: usize) -> E { + region[index] + } + + fn region_read_all(region: &Self::Region) -> Vec { + region.to_vec() + } + + fn region_read_some( + region: &Self::Region, + offset: usize, + buffer: &mut [E], + ) { + let slice = ®ion[offset..][..buffer.len()]; + buffer.copy_from_slice(slice) + } + + fn dyn_region_read( + region: &Self::DynRegion, + address: usize, + ) -> E { + { + assert!(address + mem::size_of::() <= LEN); + + let mut result = unsafe { region.as_ptr().add(address).cast::().read_unaligned() }; + result.from_stored_in_place(); + + result + } + } + + fn dyn_region_read_all( + region: &Self::DynRegion, + address: usize, + values: &mut [E], + ) { + assert!(address + mem::size_of_val(values) <= LEN); + + unsafe { + region + .as_ptr() + .add(address) + .cast::() + .copy_to(values.as_mut_ptr(), values.len()); + } + + for elem in values.iter_mut() { + elem.from_stored_in_place(); + } + } +} + +impl ManagerWrite for Owned { + fn region_write( + region: &mut Self::Region, + index: usize, + value: E, + ) { + region[index] = value; + } + + fn region_write_all(region: &mut Self::Region, value: &[E]) { + region.copy_from_slice(value) + } + + fn region_write_some( + region: &mut Self::Region, + index: usize, + buffer: &[E], + ) { + region[index..][..buffer.len()].copy_from_slice(buffer) + } + + fn dyn_region_write( + region: &mut Self::DynRegion, + address: usize, + mut value: E, + ) { + assert!(address + mem::size_of_val(&value) <= LEN); + + value.to_stored_in_place(); + + unsafe { + region + .as_mut_ptr() + .add(address) + .cast::() + .write_unaligned(value); + } + } + + fn dyn_region_write_all( + region: &mut Self::DynRegion, + address: usize, + values: &[E], + ) { + assert!(address + mem::size_of_val(values) <= LEN); + + unsafe { + let ptr = region.as_mut_ptr().add(address).cast::(); + + for (i, mut value) in values.iter().copied().enumerate() { + value.to_stored_in_place(); + ptr.add(i).write_unaligned(value) + } + } + } +} + +impl ManagerReadWrite for Owned { + fn region_replace( + region: &mut Self::Region, + index: usize, + value: E, + ) -> E { + mem::replace(&mut region[index], value) + } +} -- GitLab