@@ -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,9 @@ impl SandboxMemoryLayout {
321312 stack_size : usize ,
322313 heap_size : usize ,
323314 ) -> Result < Self > {
324- let total_page_table_size =
325- Self :: get_total_page_table_size ( cfg, code_size, stack_size, heap_size) ;
326- let guest_code_offset = total_page_table_size ;
315+ let guest_code_offset = 0x0 ;
316+ let total_page_table_size = Self :: get_total_page_table_size ( cfg, code_size, stack_size, heap_size) ;
317+ let paging_sections_offset = guest_code_offset + round_up_to ( code_size , PAGE_SIZE_USIZE ) ;
327318 // The following offsets are to the fields of the PEB struct itself!
328319 let peb_offset = total_page_table_size + round_up_to ( code_size, PAGE_SIZE_USIZE ) ;
329320 let peb_security_cookie_seed_offset =
@@ -424,9 +415,34 @@ impl SandboxMemoryLayout {
424415 kernel_stack_guard_page_offset,
425416 kernel_stack_size_rounded,
426417 boot_stack_buffer_offset,
418+ paging_sections_offset,
427419 } )
428420 }
429421
422+ /// Gets the PML4 offset
423+ /// (i.e., the `paging_sections_offset` == aligned code size)
424+ pub fn get_pml4_offset ( & self ) -> usize {
425+ self . paging_sections_offset
426+ }
427+
428+ /// Gets the PDPT offset
429+ /// (i.e., the `paging_sections_offset` + 0x1000)
430+ pub fn get_pdpt_offset ( & self ) -> usize {
431+ self . paging_sections_offset + Self :: PDPT_OFFSET
432+ }
433+
434+ /// Gets the PD offset
435+ /// (i.e., the `paging_sections_offset` + 0x2000)
436+ pub fn get_pd_offset ( & self ) -> usize {
437+ self . paging_sections_offset + Self :: PD_OFFSET
438+ }
439+
440+ /// Gets the PT offset
441+ /// (i.e., the `paging_sections_offset` + 0x3000)
442+ pub fn get_pt_offset ( & self ) -> usize {
443+ self . paging_sections_offset + Self :: PT_OFFSET
444+ }
445+
430446 /// Gets the offset in guest memory to the RunMode field in the PEB struct.
431447 pub fn get_run_mode_offset ( & self ) -> usize {
432448 self . peb_runmode_offset
@@ -778,28 +794,22 @@ impl SandboxMemoryLayout {
778794 pub fn get_memory_regions ( & self , shared_mem : & GuestSharedMemory ) -> Result < Vec < MemoryRegion > > {
779795 let mut builder = MemoryRegionVecBuilder :: new ( Self :: BASE_ADDRESS , shared_mem. base_addr ( ) ) ;
780796
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- }
797+ assert_eq ! ( self . guest_code_offset, 0x0 ) ;
795798
796- // code
797- let peb_offset = builder. push_page_aligned (
799+ // Code
800+ builder. push_page_aligned (
798801 self . code_size ,
799802 MemoryRegionFlags :: READ | MemoryRegionFlags :: WRITE | MemoryRegionFlags :: EXECUTE ,
800803 Code ,
801804 ) ;
802805
806+ // PML4, PDPT, PD
807+ let peb_offset = builder. push_page_aligned (
808+ self . total_page_table_size ,
809+ MemoryRegionFlags :: READ | MemoryRegionFlags :: WRITE ,
810+ PageTables ,
811+ ) ;
812+
803813 let expected_peb_offset = TryInto :: < usize > :: try_into ( self . peb_offset ) ?;
804814
805815 if peb_offset != expected_peb_offset {
0 commit comments