@@ -61,16 +61,16 @@ use crate::{log_then_return, new_error, Result};
61
61
// +-------------------------------------------+
62
62
// | PEB Struct (0x98) |
63
63
// +-------------------------------------------+
64
- // | Guest Code |
65
- // +-------------------------------------------+
66
64
// | PT |
67
- // +-------------------------------------------+ 0x3_000
65
+ // +-------------------------------------------+ guest_code_offset + 0x3_000
68
66
// | PD |
69
- // +-------------------------------------------+ 0x2_000
67
+ // +-------------------------------------------+ guest_code_offset + 0x2_000
70
68
// | PDPT |
71
- // +-------------------------------------------+ 0x1_000
69
+ // +-------------------------------------------+ guest_code_offset + 0x1_000
72
70
// | PML4 |
73
- // +-------------------------------------------+ 0x0_000
71
+ // +-------------------------------------------+ guest_code_offset
72
+ // | Guest Code |
73
+ // +-------------------------------------------+ 0x0
74
74
75
75
///
76
76
/// - `HostDefinitions` - the length of this is the `HostFunctionDefinitionSize`
@@ -160,6 +160,8 @@ pub(crate) struct SandboxMemoryLayout {
160
160
total_page_table_size : usize ,
161
161
// The offset in the sandbox memory where the code starts
162
162
guest_code_offset : usize ,
163
+ // The offset in the sandbox memory where the PML4 Table is located
164
+ paging_sections_offset : usize ,
163
165
}
164
166
165
167
impl Debug for SandboxMemoryLayout {
@@ -283,24 +285,13 @@ impl Debug for SandboxMemoryLayout {
283
285
}
284
286
285
287
impl 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 ;
292
291
/// 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 ;
294
293
/// 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 ;
304
295
/// The maximum amount of memory a single sandbox will be allowed.
305
296
/// The addressable virtual memory with current paging setup is virtual address 0x0 - 0x40000000,
306
297
/// excluding the memory up to BASE_ADDRESS (which is 0 by default).
@@ -321,9 +312,9 @@ impl SandboxMemoryLayout {
321
312
stack_size : usize ,
322
313
heap_size : usize ,
323
314
) -> 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 ) ;
327
318
// The following offsets are to the fields of the PEB struct itself!
328
319
let peb_offset = total_page_table_size + round_up_to ( code_size, PAGE_SIZE_USIZE ) ;
329
320
let peb_security_cookie_seed_offset =
@@ -424,9 +415,34 @@ impl SandboxMemoryLayout {
424
415
kernel_stack_guard_page_offset,
425
416
kernel_stack_size_rounded,
426
417
boot_stack_buffer_offset,
418
+ paging_sections_offset,
427
419
} )
428
420
}
429
421
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
+
430
446
/// Gets the offset in guest memory to the RunMode field in the PEB struct.
431
447
pub fn get_run_mode_offset ( & self ) -> usize {
432
448
self . peb_runmode_offset
@@ -778,28 +794,22 @@ impl SandboxMemoryLayout {
778
794
pub fn get_memory_regions ( & self , shared_mem : & GuestSharedMemory ) -> Result < Vec < MemoryRegion > > {
779
795
let mut builder = MemoryRegionVecBuilder :: new ( Self :: BASE_ADDRESS , shared_mem. base_addr ( ) ) ;
780
796
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 ) ;
795
798
796
- // code
797
- let peb_offset = builder. push_page_aligned (
799
+ // Code
800
+ builder. push_page_aligned (
798
801
self . code_size ,
799
802
MemoryRegionFlags :: READ | MemoryRegionFlags :: WRITE | MemoryRegionFlags :: EXECUTE ,
800
803
Code ,
801
804
) ;
802
805
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
+
803
813
let expected_peb_offset = TryInto :: < usize > :: try_into ( self . peb_offset ) ?;
804
814
805
815
if peb_offset != expected_peb_offset {
0 commit comments