Skip to content

Commit 8c7a953

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 8c7a953

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

td-payload/src/arch/x86_64/init.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ use crate::{
77
arch::{gdt, idt},
88
hob::{self, get_hob},
99
mm::{
10-
get_usable, heap::init_heap, init_ram, layout::RuntimeLayout,
11-
page_table::init_pt_frame_allocator, shared::init_shared_memory,
10+
get_usable,
11+
heap::init_heap,
12+
init_ram,
13+
layout::RuntimeLayout,
14+
page_table::init_pt_frame_allocator,
15+
shared::{init_shared_memory, init_shared_memory_with_shadow},
1216
},
1317
};
1418

@@ -22,7 +26,7 @@ use super::{
2226
idt::{PAGE_FAULT_EXCEPTION, PAGE_FAULT_IST},
2327
};
2428

25-
pub fn pre_init(hob: u64, layout: &RuntimeLayout) {
29+
pub fn pre_init(hob: u64, layout: &RuntimeLayout, use_shared_shadow: bool) {
2630
let hob = hob::init(hob).expect("Invalid payload HOB");
2731
let memory_map = init_ram(hob).expect("Failed to parse E820 table from payload HOB");
2832

@@ -35,7 +39,13 @@ pub fn pre_init(hob: u64, layout: &RuntimeLayout) {
3539
init_heap(heap, layout.heap_size);
3640

3741
let shared = get_usable(layout.shared_memory_size).expect("Failed to allocate shared memory");
38-
init_shared_memory(shared, layout.shared_memory_size);
42+
if use_shared_shadow {
43+
let shadow =
44+
get_usable(layout.shared_memory_size).expect("Failed to allocate shared shadow");
45+
init_shared_memory_with_shadow(shared, layout.shared_memory_size, shadow);
46+
} else {
47+
init_shared_memory(shared, layout.shared_memory_size);
48+
}
3949

4050
// Init Global Descriptor Table and Task State Segment
4151
gdt::init_gdt();

td-payload/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub extern "C" fn _start(hob: u64, _payload: u64) -> ! {
3333

3434
let layout = RuntimeLayout::default();
3535

36-
arch::init::pre_init(hob, &layout);
36+
arch::init::pre_init(hob, &layout, false);
3737
arch::init::init(&layout, main);
3838
}
3939

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+
SHARED_START.call_once(|| start as usize);
32+
SHADOW_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
}

tests/test-td-payload/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub extern "C" fn _start(hob: u64, _payload: u64) -> ! {
132132
shadow_stack_size: layout::DEFAULT_SHADOW_STACK_SIZE,
133133
};
134134

135-
arch::init::pre_init(hob, &layout);
135+
arch::init::pre_init(hob, &layout, false);
136136
arch::init::init(&layout, main);
137137
}
138138

0 commit comments

Comments
 (0)