Skip to content

Commit b67b30e

Browse files
committed
td-payload: add new shared memory init function with private shadow
To keep the original API behavior unchanged. The extended API init the shared memory allocator with a private shadow start address. If the private shadow is not available, the method `copy_to_private_shadow` will return None. As the `shadow_start` may be lower or higher than start of shared memory, the way of allocating private shadow is changed to use the offset of the allocated shared address to the start of shared allocator. Signed-off-by: Jiaqi Gao <[email protected]>
1 parent 5f46c59 commit b67b30e

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

td-payload/src/mm/shared.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,38 @@ use super::SIZE_4K;
1010
use crate::arch::shared::decrypt;
1111

1212
static SHARED_MEMORY_ALLOCATOR: LockedHeap = LockedHeap::empty();
13-
static SHADOW_OFFSET: Once<usize> = Once::new();
13+
static SHARED_START: Once<usize> = Once::new();
14+
static SHADOW_START: Once<usize> = Once::new();
1415

1516
pub fn init_shared_memory(start: u64, size: usize) {
1617
if size % SIZE_4K != 0 {
1718
panic!("Failed to initialize shared memory: size needs to be aligned with 0x1000");
1819
}
19-
let shared_size = size / 2;
2020

2121
// Set the shared memory region to be shared
22-
decrypt(start, shared_size);
22+
decrypt(start, size);
2323
// Initialize the shared memory allocator
2424
unsafe {
25-
SHARED_MEMORY_ALLOCATOR
26-
.lock()
27-
.init(start as *mut u8, shared_size);
25+
SHARED_MEMORY_ALLOCATOR.lock().init(start as *mut u8, size);
2826
}
29-
SHADOW_OFFSET.call_once(|| shared_size);
27+
}
28+
29+
pub fn init_shared_memory_with_shadow(start: u64, size: usize, shadow_start: u64) {
30+
init_shared_memory(start, size);
31+
SHADOW_START.call_once(|| start as usize);
32+
SHARED_START.call_once(|| shadow_start as usize);
3033
}
3134

3235
pub struct SharedMemory {
3336
addr: usize,
34-
shadow_addr: usize,
37+
shadow_addr: Option<usize>,
3538
size: usize,
3639
}
3740

3841
impl SharedMemory {
3942
pub fn new(num_page: usize) -> Option<Self> {
4043
let addr = unsafe { alloc_shared_pages(num_page)? };
41-
let shadow_addr = alloc_private_shadow_pages(addr)?;
44+
let shadow_addr = alloc_private_shadow_pages(addr);
4245

4346
Some(Self {
4447
addr,
@@ -47,12 +50,13 @@ impl SharedMemory {
4750
})
4851
}
4952

50-
pub fn copy_to_private_shadow(&mut self) -> &[u8] {
51-
let shadow =
52-
unsafe { core::slice::from_raw_parts_mut(self.shadow_addr as *mut u8, self.size) };
53-
shadow.copy_from_slice(self.as_bytes());
53+
pub fn copy_to_private_shadow(&mut self) -> Option<&[u8]> {
54+
self.shadow_addr.map(|addr| {
55+
let shadow = unsafe { core::slice::from_raw_parts_mut(addr as *mut u8, self.size) };
56+
shadow.copy_from_slice(self.as_bytes());
5457

55-
shadow
58+
&shadow[..]
59+
})
5660
}
5761

5862
pub fn as_bytes(&self) -> &[u8] {
@@ -110,5 +114,6 @@ pub unsafe fn free_shared_page(addr: usize) {
110114
}
111115

112116
fn alloc_private_shadow_pages(shared_addr: usize) -> Option<usize> {
113-
Some(shared_addr + SHADOW_OFFSET.get()?)
117+
let offset = shared_addr.checked_sub(*SHARED_START.get()?)?;
118+
Some(SHADOW_START.get()? + offset)
114119
}

0 commit comments

Comments
 (0)