From 0001b34978e15bb11365a9e9bd0364ad178046a3 Mon Sep 17 00:00:00 2001 From: Jiaqi Gao Date: Tue, 31 Oct 2023 04:29:30 -0400 Subject: [PATCH] td-payload: change `Dma` to `SharedMemory` To avoid misunderstanding. Signed-off-by: Jiaqi Gao --- td-payload/src/arch/x86_64/init.rs | 8 ++-- td-payload/src/arch/x86_64/mod.rs | 4 +- .../src/arch/x86_64/{dma.rs => shared.rs} | 0 td-payload/src/bin/example/main.rs | 6 +-- td-payload/src/mm/layout.rs | 6 +-- td-payload/src/mm/mod.rs | 4 +- td-payload/src/mm/{dma.rs => shared.rs} | 42 +++++++++---------- tests/test-td-payload/src/main.rs | 10 ++--- 8 files changed, 40 insertions(+), 40 deletions(-) rename td-payload/src/arch/x86_64/{dma.rs => shared.rs} (100%) rename td-payload/src/mm/{dma.rs => shared.rs} (56%) diff --git a/td-payload/src/arch/x86_64/init.rs b/td-payload/src/arch/x86_64/init.rs index 70d715f6..46d7f455 100644 --- a/td-payload/src/arch/x86_64/init.rs +++ b/td-payload/src/arch/x86_64/init.rs @@ -7,8 +7,8 @@ use crate::{ arch::{gdt, idt}, hob::{self, get_hob}, mm::{ - dma::init_dma, get_usable, heap::init_heap, init_ram, layout::RuntimeLayout, - page_table::init_pt_frame_allocator, + get_usable, heap::init_heap, init_ram, layout::RuntimeLayout, + page_table::init_pt_frame_allocator, shared::init_shared_memory, }, }; @@ -34,8 +34,8 @@ pub fn pre_init(hob: u64, layout: &RuntimeLayout) { let heap = get_usable(layout.heap_size).expect("Failed to allocate heap"); init_heap(heap, layout.heap_size); - let dma = get_usable(layout.dma_size).expect("Failed to allocate dma"); - init_dma(dma, layout.dma_size); + let shared = get_usable(layout.shared_memory_size).expect("Failed to allocate shared memory"); + init_shared_memory(shared, layout.shared_memory_size); // Init Global Descriptor Table and Task State Segment gdt::init_gdt(); diff --git a/td-payload/src/arch/x86_64/mod.rs b/td-payload/src/arch/x86_64/mod.rs index ad02138b..b2577c9e 100644 --- a/td-payload/src/arch/x86_64/mod.rs +++ b/td-payload/src/arch/x86_64/mod.rs @@ -4,11 +4,11 @@ pub mod apic; pub mod cet; -#[cfg(feature = "tdx")] -pub mod dma; pub mod gdt; pub mod guard_page; pub mod idt; pub mod init; pub mod paging; pub mod serial; +#[cfg(feature = "tdx")] +pub mod shared; diff --git a/td-payload/src/arch/x86_64/dma.rs b/td-payload/src/arch/x86_64/shared.rs similarity index 100% rename from td-payload/src/arch/x86_64/dma.rs rename to td-payload/src/arch/x86_64/shared.rs diff --git a/td-payload/src/bin/example/main.rs b/td-payload/src/bin/example/main.rs index 1aff1452..2962bbd9 100644 --- a/td-payload/src/bin/example/main.rs +++ b/td-payload/src/bin/example/main.rs @@ -64,9 +64,9 @@ pub extern "C" fn main() -> ! { #[cfg(all(feature = "coverage", feature = "tdx"))] { const MAX_COVERAGE_DATA_PAGE_COUNT: usize = 0x100; - let mut dma = td_payload::mm::dma::DmaMemory::new(MAX_COVERAGE_DATA_PAGE_COUNT) - .expect("New dma fail."); - let buffer = dma.as_mut_bytes(); + let mut shared = td_payload::mm::shared::SharedMemory::new(MAX_COVERAGE_DATA_PAGE_COUNT) + .expect("New shared memory fail."); + let buffer = shared.as_mut_bytes(); let coverage_len = minicov::get_coverage_data_size(); assert!(coverage_len < MAX_COVERAGE_DATA_PAGE_COUNT * td_paging::PAGE_SIZE); diff --git a/td-payload/src/mm/layout.rs b/td-payload/src/mm/layout.rs index 5525377b..fe4ad42d 100644 --- a/td-payload/src/mm/layout.rs +++ b/td-payload/src/mm/layout.rs @@ -5,7 +5,7 @@ pub const DEFAULT_HEAP_SIZE: usize = 0x1000000; pub const DEFAULT_STACK_SIZE: usize = 0x800000; pub const DEFAULT_PAGE_TABLE_SIZE: usize = 0x800000; -pub const DEFAULT_DMA_SIZE: usize = 0x100000; +pub const DEFAULT_SHARED_MEMORY_SIZE: usize = 0x100000; #[cfg(feature = "cet-shstk")] pub const DEFAULT_SHADOW_STACK_SIZE: usize = 0x10000; @@ -14,7 +14,7 @@ pub struct RuntimeLayout { pub heap_size: usize, pub stack_size: usize, pub page_table_size: usize, - pub dma_size: usize, + pub shared_memory_size: usize, #[cfg(feature = "cet-shstk")] pub shadow_stack_size: usize, } @@ -25,7 +25,7 @@ impl Default for RuntimeLayout { heap_size: DEFAULT_HEAP_SIZE, stack_size: DEFAULT_STACK_SIZE, page_table_size: DEFAULT_PAGE_TABLE_SIZE, - dma_size: DEFAULT_DMA_SIZE, + shared_memory_size: DEFAULT_SHARED_MEMORY_SIZE, #[cfg(feature = "cet-shstk")] shadow_stack_size: DEFAULT_SHADOW_STACK_SIZE, } diff --git a/td-payload/src/mm/mod.rs b/td-payload/src/mm/mod.rs index 34775f49..8ae84450 100644 --- a/td-payload/src/mm/mod.rs +++ b/td-payload/src/mm/mod.rs @@ -19,10 +19,10 @@ use zerocopy::FromBytes; use crate::Error; -#[cfg(feature = "tdx")] -pub mod dma; #[cfg(any(target_os = "none", target_os = "uefi"))] pub(crate) mod heap; +#[cfg(feature = "tdx")] +pub mod shared; #[cfg(not(any(target_os = "none", target_os = "uefi")))] pub(crate) mod heap { // A null implementation used by test diff --git a/td-payload/src/mm/dma.rs b/td-payload/src/mm/shared.rs similarity index 56% rename from td-payload/src/mm/dma.rs rename to td-payload/src/mm/shared.rs index 83f84422..ebec405f 100644 --- a/td-payload/src/mm/dma.rs +++ b/td-payload/src/mm/shared.rs @@ -6,27 +6,27 @@ use core::{alloc::Layout, ptr::NonNull}; use linked_list_allocator::LockedHeap; use super::SIZE_4K; -use crate::arch::dma::decrypt; +use crate::arch::shared::decrypt; -static DMA_ALLOCATOR: LockedHeap = LockedHeap::empty(); +static SHARED_MEMORY_ALLOCATOR: LockedHeap = LockedHeap::empty(); -pub fn init_dma(start: u64, size: usize) { - // Set the DMA memory region to be shared +pub fn init_shared_memory(start: u64, size: usize) { + // Set the shared memory region to be shared decrypt(start, size); - // Initialize the DMA allocator + // Initialize the shared memory allocator unsafe { - DMA_ALLOCATOR.lock().init(start as *mut u8, size); + SHARED_MEMORY_ALLOCATOR.lock().init(start as *mut u8, size); } } -pub struct DmaMemory { +pub struct SharedMemory { addr: usize, size: usize, } -impl DmaMemory { +impl SharedMemory { pub fn new(num_page: usize) -> Option { - let addr = unsafe { alloc_dma_pages(num_page)? }; + let addr = unsafe { alloc_shared_pages(num_page)? }; Some(Self { addr, @@ -43,18 +43,18 @@ impl DmaMemory { } } -impl Drop for DmaMemory { +impl Drop for SharedMemory { fn drop(&mut self) { - unsafe { free_dma_pages(self.addr, self.size / SIZE_4K) } + unsafe { free_shared_pages(self.addr, self.size / SIZE_4K) } } } /// # Safety -/// The caller needs to explicitly call the `free_dma_pages` function after use -pub unsafe fn alloc_dma_pages(num: usize) -> Option { +/// The caller needs to explicitly call the `free_shared_pages` function after use +pub unsafe fn alloc_shared_pages(num: usize) -> Option { let size = SIZE_4K.checked_mul(num)?; - let addr = DMA_ALLOCATOR + let addr = SHARED_MEMORY_ALLOCATOR .lock() .allocate_first_fit(Layout::from_size_align(size, SIZE_4K).ok()?) .map(|ptr| ptr.as_ptr() as usize) @@ -66,17 +66,17 @@ pub unsafe fn alloc_dma_pages(num: usize) -> Option { } /// # Safety -/// The caller needs to explicitly call the `free_dma_page` function after use -pub unsafe fn alloc_dma_page() -> Option { - alloc_dma_pages(1) +/// The caller needs to explicitly call the `free_shared_page` function after use +pub unsafe fn alloc_shared_page() -> Option { + alloc_shared_pages(1) } /// # Safety /// The caller needs to ensure the correctness of the addr and page num -pub unsafe fn free_dma_pages(addr: usize, num: usize) { +pub unsafe fn free_shared_pages(addr: usize, num: usize) { let size = SIZE_4K.checked_mul(num).expect("Invalid page num"); - DMA_ALLOCATOR.lock().deallocate( + SHARED_MEMORY_ALLOCATOR.lock().deallocate( NonNull::new(addr as *mut u8).unwrap(), Layout::from_size_align(size, SIZE_4K).unwrap(), ); @@ -84,6 +84,6 @@ pub unsafe fn free_dma_pages(addr: usize, num: usize) { /// # Safety /// The caller needs to ensure the correctness of the addr -pub unsafe fn free_dma_page(addr: usize) { - free_dma_pages(addr, 1) +pub unsafe fn free_shared_page(addr: usize) { + free_shared_pages(addr, 1) } diff --git a/tests/test-td-payload/src/main.rs b/tests/test-td-payload/src/main.rs index 3e1f7bc3..10cb1313 100644 --- a/tests/test-td-payload/src/main.rs +++ b/tests/test-td-payload/src/main.rs @@ -128,7 +128,7 @@ pub extern "C" fn _start(hob: u64, _payload: u64) -> ! { heap_size: layout::DEFAULT_HEAP_SIZE, stack_size: layout::DEFAULT_STACK_SIZE, page_table_size: PAGE_TABLE_SIZE, - dma_size: layout::DEFAULT_DMA_SIZE, + shared_memory_size: layout::DEFAULT_SHARED_MEMORY_SIZE, shadow_stack_size: layout::DEFAULT_SHADOW_STACK_SIZE, }; @@ -274,13 +274,13 @@ extern "C" fn main() -> ! { ts.failed_cases ); - // Need to set DEFAULT_DMA_SIZE to 0x200000 before build + // Need to set DEFAULT_SHARED_MEMORY_SIZE to 0x200000 before build #[cfg(all(feature = "coverage", feature = "tdx"))] { const MAX_COVERAGE_DATA_PAGE_COUNT: usize = 0x200; - let mut dma = td_payload::mm::dma::DmaMemory::new(MAX_COVERAGE_DATA_PAGE_COUNT) - .expect("New dma fail."); - let buffer = dma.as_mut_bytes(); + let mut shared = td_payload::mm::shared::SharedMemory::new(MAX_COVERAGE_DATA_PAGE_COUNT) + .expect("New shared memory fail."); + let buffer = shared.as_mut_bytes(); let coverage_len = minicov::get_coverage_data_size(); assert!(coverage_len < MAX_COVERAGE_DATA_PAGE_COUNT * td_paging::PAGE_SIZE); minicov::capture_coverage_to_buffer(&mut buffer[0..coverage_len]);