@@ -61,16 +61,16 @@ use crate::{log_then_return, new_error, Result};
6161// +-------------------------------------------+
6262// | PEB Struct (0x98) |
6363// +-------------------------------------------+
64- // | Guest Code |
65- // +-------------------------------------------+
6664// | PT |
67- // +-------------------------------------------+ 0x3_000
65+ // +-------------------------------------------+ guest_code_offset + 0x3_000
6866// | PD |
69- // +-------------------------------------------+ 0x2_000
67+ // +-------------------------------------------+ guest_code_offset + 0x2_000
7068// | PDPT |
71- // +-------------------------------------------+ 0x1_000
69+ // +-------------------------------------------+ guest_code_offset + 0x1_000
7270// | PML4 |
73- // +-------------------------------------------+ 0x0_000
71+ // +-------------------------------------------+ guest_code_offset
72+ // | Guest Code |
73+ // +-------------------------------------------+ 0x0
7474
7575///
7676/// - `HostDefinitions` - the length of this is the `HostFunctionDefinitionSize`
@@ -160,6 +160,8 @@ pub(crate) struct SandboxMemoryLayout {
160160 total_page_table_size : usize ,
161161 // The offset in the sandbox memory where the code starts
162162 guest_code_offset : usize ,
163+ // The offset in the sandbox memory where the PML4 Table is located
164+ paging_sections_offset : usize ,
163165}
164166
165167impl Debug for SandboxMemoryLayout {
@@ -283,24 +285,13 @@ impl Debug for SandboxMemoryLayout {
283285}
284286
285287impl SandboxMemoryLayout {
286- /// The offset into the sandbox's memory where the PML4 Table is located.
287- /// See https://www.pagetable.com/?p=14 for more information.
288- pub ( crate ) const PML4_OFFSET : usize = 0x0000 ;
289- /// The offset into the sandbox's memory where the Page Directory Pointer
290- /// Table starts.
291- pub ( super ) const PDPT_OFFSET : usize = 0x1000 ;
288+ /// The offset from the start of the paging section region into the sandbox's memory where the
289+ /// Page Directory Pointer Table starts.
290+ const PDPT_OFFSET : usize = 0x1000 ;
292291 /// The offset into the sandbox's memory where the Page Directory starts.
293- pub ( super ) const PD_OFFSET : usize = 0x2000 ;
292+ const PD_OFFSET : usize = 0x2000 ;
294293 /// The offset into the sandbox's memory where the Page Tables start.
295- pub ( super ) const PT_OFFSET : usize = 0x3000 ;
296- /// The address (not the offset) to the start of the page directory
297- pub ( super ) const PD_GUEST_ADDRESS : usize = Self :: BASE_ADDRESS + Self :: PD_OFFSET ;
298- /// The address (not the offset) into sandbox memory where the Page
299- /// Directory Pointer Table starts
300- pub ( super ) const PDPT_GUEST_ADDRESS : usize = Self :: BASE_ADDRESS + Self :: PDPT_OFFSET ;
301- /// The address (not the offset) into sandbox memory where the Page
302- /// Tables start
303- pub ( super ) const PT_GUEST_ADDRESS : usize = Self :: BASE_ADDRESS + Self :: PT_OFFSET ;
294+ const PT_OFFSET : usize = 0x3000 ;
304295 /// The maximum amount of memory a single sandbox will be allowed.
305296 /// The addressable virtual memory with current paging setup is virtual address 0x0 - 0x40000000,
306297 /// excluding the memory up to BASE_ADDRESS (which is 0 by default).
@@ -321,9 +312,10 @@ impl SandboxMemoryLayout {
321312 stack_size : usize ,
322313 heap_size : usize ,
323314 ) -> Result < Self > {
315+ let guest_code_offset = 0x0 ;
324316 let total_page_table_size =
325317 Self :: get_total_page_table_size ( cfg, code_size, stack_size, heap_size) ;
326- let guest_code_offset = total_page_table_size ;
318+ let paging_sections_offset = guest_code_offset + round_up_to ( code_size , PAGE_SIZE_USIZE ) ;
327319 // The following offsets are to the fields of the PEB struct itself!
328320 let peb_offset = total_page_table_size + round_up_to ( code_size, PAGE_SIZE_USIZE ) ;
329321 let peb_security_cookie_seed_offset =
@@ -424,9 +416,34 @@ impl SandboxMemoryLayout {
424416 kernel_stack_guard_page_offset,
425417 kernel_stack_size_rounded,
426418 boot_stack_buffer_offset,
419+ paging_sections_offset,
427420 } )
428421 }
429422
423+ /// Gets the PML4 offset
424+ /// (i.e., the `paging_sections_offset` == aligned code size)
425+ pub fn get_pml4_offset ( & self ) -> usize {
426+ self . paging_sections_offset
427+ }
428+
429+ /// Gets the PDPT offset
430+ /// (i.e., the `paging_sections_offset` + 0x1000)
431+ pub fn get_pdpt_offset ( & self ) -> usize {
432+ self . paging_sections_offset + Self :: PDPT_OFFSET
433+ }
434+
435+ /// Gets the PD offset
436+ /// (i.e., the `paging_sections_offset` + 0x2000)
437+ pub fn get_pd_offset ( & self ) -> usize {
438+ self . paging_sections_offset + Self :: PD_OFFSET
439+ }
440+
441+ /// Gets the PT offset
442+ /// (i.e., the `paging_sections_offset` + 0x3000)
443+ pub fn get_pt_offset ( & self ) -> usize {
444+ self . paging_sections_offset + Self :: PT_OFFSET
445+ }
446+
430447 /// Gets the offset in guest memory to the RunMode field in the PEB struct.
431448 pub fn get_run_mode_offset ( & self ) -> usize {
432449 self . peb_runmode_offset
@@ -778,28 +795,22 @@ impl SandboxMemoryLayout {
778795 pub fn get_memory_regions ( & self , shared_mem : & GuestSharedMemory ) -> Result < Vec < MemoryRegion > > {
779796 let mut builder = MemoryRegionVecBuilder :: new ( Self :: BASE_ADDRESS , shared_mem. base_addr ( ) ) ;
780797
781- // PML4, PDPT, PD
782- let code_offset = builder. push_page_aligned (
783- self . total_page_table_size ,
784- MemoryRegionFlags :: READ | MemoryRegionFlags :: WRITE ,
785- PageTables ,
786- ) ;
787-
788- if code_offset != self . guest_code_offset {
789- return Err ( new_error ! (
790- "Code offset does not match expected code offset expected: {}, actual: {}" ,
791- self . guest_code_offset,
792- code_offset
793- ) ) ;
794- }
798+ assert_eq ! ( self . guest_code_offset, 0x0 ) ;
795799
796- // code
797- let peb_offset = builder. push_page_aligned (
800+ // Code
801+ builder. push_page_aligned (
798802 self . code_size ,
799803 MemoryRegionFlags :: READ | MemoryRegionFlags :: WRITE | MemoryRegionFlags :: EXECUTE ,
800804 Code ,
801805 ) ;
802806
807+ // PML4, PDPT, PD
808+ let peb_offset = builder. push_page_aligned (
809+ self . total_page_table_size ,
810+ MemoryRegionFlags :: READ | MemoryRegionFlags :: WRITE ,
811+ PageTables ,
812+ ) ;
813+
803814 let expected_peb_offset = TryInto :: < usize > :: try_into ( self . peb_offset ) ?;
804815
805816 if peb_offset != expected_peb_offset {
0 commit comments