@@ -122,11 +122,12 @@ impl MmapManager {
122
122
123
123
pub fn lookup ( & self , addr : usize ) -> Option < & MmapInfo > {
124
124
// Search for the range that contains the address
125
- self . mappings
126
- . range ( ..=RangeWrapper ( addr..( addr + 1 ) ) ) // Find all ranges up to the address
127
- . rev ( ) // Reverse the iterator to start from the largest range
128
- . find ( |( range, _) | range. 0 . contains ( & addr) ) // Check if the range contains the address
129
- . map ( |( _, info) | info) // Return the associated MmapInfo
125
+ for ( range, info) in self . mappings . range ( ..=RangeWrapper ( addr..usize:: max_value ( ) ) ) . rev ( ) {
126
+ if range. 0 . contains ( & addr) {
127
+ return Some ( info) ;
128
+ }
129
+ }
130
+ None
130
131
}
131
132
}
132
133
@@ -256,6 +257,7 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
256
257
// update trustlet's page table
257
258
let flags = ProcessPageFlags :: FLAG_REUSE ;
258
259
page_table_ref. map_4k_page ( dst, new_page, flags) ;
260
+ log:: info!( "Mapped new page for the trustlet at 0x{:x}" , trustlet. pf_target_vaddr) ;
259
261
}
260
262
}
261
263
@@ -738,6 +740,8 @@ impl ProcessRuntime for PALContext {
738
740
// example: [1..3] < [2..4] < [2..5] < [3..4]
739
741
// page fault hander uses mmap infomation whose range contains the faulting address
740
742
// and priority is given to the latter one in the order
743
+ // FIXME: as the ordering above does not consider the time of mapping,
744
+ // the mmap handler might not use the latest mapping that includes the faulting address
741
745
mmap_manger. add_mapping ( addr as usize , size as usize , fd as i32 , offset as usize ) ;
742
746
743
747
// Allocate virtul memory address
@@ -914,11 +918,12 @@ impl ProcessRuntime for PALContext {
914
918
const PF_RESERVED : u64 = 1 << 3 ;
915
919
const PF_INSTRUCTION : u64 = 1 << 4 ;
916
920
let mmap_manager = & self . process . mmap_manager ;
921
+ log:: info!( "[Trustlet] #PF: CR2=0x{:x}" , cr2) ;
917
922
if let Some ( mmap_info) = mmap_manager. lookup ( cr2 as usize ) {
918
- log:: info!( " [Trustlet] Found file mapping: mmap_info={:?}" , mmap_info) ;
923
+ log:: info!( "Found file mapping: mmap_info={:?}" , mmap_info) ;
919
924
if error_code & PF_PRESENT == 0 {
920
925
// non-presente page
921
- log:: debug!( " [Trustlet] Page fault: not present page" ) ;
926
+ log:: debug!( "[Trustlet] Page fault: not present page" ) ;
922
927
let target_page_addr = cr2 & !0xFFF ;
923
928
self . process . pf_target_vaddr = target_page_addr;
924
929
assert ! ( target_page_addr >= mmap_info. addr as u64 ) ;
@@ -945,19 +950,25 @@ impl ProcessRuntime for PALContext {
945
950
// make a guest request to load the page
946
951
self . return_value = TrustletReturnType :: MMAP as u64 ;
947
952
return false ;
948
- } else if error_code & PF_PRESENT != 0 && error_code & PF_WRITE != 0 {
949
- // CoW
950
- let mut page_table_ref = ProcessPageTableRef :: default ( ) ;
951
- page_table_ref. set_external_table ( self . vmsa . cr3 ) ;
952
- // Handle CoW
953
- log:: debug!( " [Trustlet] CoW: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
954
- let user_access = error_code & PF_USER != 0 ;
955
- let handled = page_table_ref. handle_cow ( VirtAddr :: from ( cr2) , user_access) ;
956
- if handled {
957
- log:: debug!( " [Trustlet] CoW: handled" ) ;
958
- return true ;
959
- }
953
+ } else {
954
+ log:: info!( "[Trustlet] #PF on present mmaped-page" ) ;
960
955
}
956
+ } else {
957
+ log:: info!( "[Trustlet] #PF: address is not mmaped-page" ) ;
958
+ }
959
+ if error_code & PF_PRESENT != 0 && error_code & PF_WRITE != 0 {
960
+ // CoW
961
+ let mut page_table_ref = ProcessPageTableRef :: default ( ) ;
962
+ page_table_ref. set_external_table ( self . vmsa . cr3 ) ;
963
+ // Handle CoW
964
+ log:: debug!( "[Trustlet] CoW: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
965
+ let user_access = error_code & PF_USER != 0 ;
966
+ let handled = page_table_ref. handle_cow ( VirtAddr :: from ( cr2) , user_access) ;
967
+ if handled {
968
+ log:: debug!( "[Trustlet] CoW: handled" ) ;
969
+ return true ;
970
+ }
971
+ log:: info!( "[Trustlet] [BUG] CoW: not handled" ) ;
961
972
}
962
973
963
974
// XXX: it should not come here
@@ -968,7 +979,7 @@ impl ProcessRuntime for PALContext {
968
979
let cr4 = self . vmsa . cr4 ;
969
980
let rsp = self . vmsa . rsp ;
970
981
let rflags = self . vmsa . rflags ;
971
- log:: info!( " [Trustlet] Page Fault!" ) ;
982
+ log:: info!( "[Trustlet] [BUG] Unhandled Page Fault!" ) ;
972
983
log:: info!( "vmsa EFER: {:?}" , efer) ;
973
984
log:: info!( "vmsa CR2: {:?}" , cr2) ;
974
985
log:: info!( "vmsa cr4: {:?}" , cr4) ;
@@ -985,7 +996,7 @@ impl ProcessRuntime for PALContext {
985
996
let offset = ( rsp & 0xFFF ) / 8 ;
986
997
let ( _mapping, stack_mapping) = map_paddr ! ( stack_base_paddr) ;
987
998
for i in 0 ..9 {
988
- log:: info!( " [Trustlet] Stack (rsp+{}): {:#x}" , i* 8 , unsafe { stack_mapping. as_ptr:: <u64 >( ) . offset( ( offset + i) . try_into( ) . unwrap( ) ) . read( ) } ) ;
999
+ log:: info!( "[Trustlet] Stack (rsp+{}): {:#x}" , i* 8 , unsafe { stack_mapping. as_ptr:: <u64 >( ) . offset( ( offset + i) . try_into( ) . unwrap( ) ) . read( ) } ) ;
989
1000
}
990
1001
991
1002
/*
@@ -996,22 +1007,21 @@ impl ProcessRuntime for PALContext {
996
1007
return true;
997
1008
*/
998
1009
999
- // #PF for other reasons
1000
- log:: info!( " [Trustlet] Unhandled #PF: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
1010
+ log:: info!( "[Trustlet] #PF: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
1001
1011
if error_code & PF_PRESENT == 0 {
1002
- log:: info!( " [Trustlet] Page fault: not present" ) ;
1012
+ log:: info!( "[Trustlet] Page fault: not present" ) ;
1003
1013
}
1004
1014
if error_code & PF_WRITE != 0 {
1005
- log:: info!( " [Trustlet] Page fault: write" ) ;
1015
+ log:: info!( "[Trustlet] Page fault: write" ) ;
1006
1016
}
1007
1017
if error_code & PF_USER != 0 {
1008
- log:: info!( " [Trustlet] Page fault: user" ) ;
1018
+ log:: info!( "[Trustlet] Page fault: user" ) ;
1009
1019
}
1010
1020
if error_code & PF_RESERVED != 0 {
1011
- log:: info!( " [Trustlet] Page fault: reserved" ) ;
1021
+ log:: info!( "[Trustlet] Page fault: reserved" ) ;
1012
1022
}
1013
1023
if error_code & PF_INSTRUCTION != 0 {
1014
- log:: info!( " [Trustlet] Page fault: instruction fetch" ) ;
1024
+ log:: info!( "[Trustlet] Page fault: instruction fetch" ) ;
1015
1025
}
1016
1026
false
1017
1027
}
0 commit comments