@@ -119,11 +119,12 @@ impl MmapManager {
119
119
120
120
pub fn lookup ( & self , addr : usize ) -> Option < & MmapInfo > {
121
121
// Search for the range that contains the address
122
- self . mappings
123
- . range ( ..=RangeWrapper ( addr..( addr + 1 ) ) ) // Find all ranges up to the address
124
- . rev ( ) // Reverse the iterator to start from the largest range
125
- . find ( |( range, _) | range. 0 . contains ( & addr) ) // Check if the range contains the address
126
- . map ( |( _, info) | info) // Return the associated MmapInfo
122
+ for ( range, info) in self . mappings . range ( ..=RangeWrapper ( addr..usize:: max_value ( ) ) ) . rev ( ) {
123
+ if range. 0 . contains ( & addr) {
124
+ return Some ( info) ;
125
+ }
126
+ }
127
+ None
127
128
}
128
129
}
129
130
@@ -252,6 +253,7 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
252
253
// update trustlet's page table
253
254
let flags = ProcessPageFlags :: FLAG_REUSE ;
254
255
page_table_ref. map_4k_page ( dst, new_page, flags) ;
256
+ log:: info!( "Mapped new page for the trustlet at 0x{:x}" , trustlet. pf_target_vaddr) ;
255
257
}
256
258
}
257
259
@@ -716,6 +718,8 @@ impl ProcessRuntime for PALContext {
716
718
// example: [1..3] < [2..4] < [2..5] < [3..4]
717
719
// page fault hander uses mmap infomation whose range contains the faulting address
718
720
// and priority is given to the latter one in the order
721
+ // FIXME: as the ordering above does not consider the time of mapping,
722
+ // the mmap handler might not use the latest mapping that includes the faulting address
719
723
mmap_manger. add_mapping ( addr as usize , size as usize , fd as i32 , offset as usize ) ;
720
724
721
725
// Allocate virtul memory address
@@ -892,11 +896,12 @@ impl ProcessRuntime for PALContext {
892
896
const PF_RESERVED : u64 = 1 << 3 ;
893
897
const PF_INSTRUCTION : u64 = 1 << 4 ;
894
898
let mmap_manager = & self . process . mmap_manager ;
899
+ log:: info!( "[Trustlet] #PF: CR2=0x{:x}" , cr2) ;
895
900
if let Some ( mmap_info) = mmap_manager. lookup ( cr2 as usize ) {
896
- log:: info!( " [Trustlet] Found file mapping: mmap_info={:?}" , mmap_info) ;
901
+ log:: info!( "Found file mapping: mmap_info={:?}" , mmap_info) ;
897
902
if error_code & PF_PRESENT == 0 {
898
903
// non-presente page
899
- log:: debug!( " [Trustlet] Page fault: not present page" ) ;
904
+ log:: debug!( "[Trustlet] Page fault: not present page" ) ;
900
905
let target_page_addr = cr2 & !0xFFF ;
901
906
self . process . pf_target_vaddr = target_page_addr;
902
907
assert ! ( target_page_addr >= mmap_info. addr as u64 ) ;
@@ -923,19 +928,25 @@ impl ProcessRuntime for PALContext {
923
928
// make a guest request to load the page
924
929
self . return_value = TrustletReturnType :: MMAP as u64 ;
925
930
return false ;
926
- } else if error_code & PF_PRESENT != 0 && error_code & PF_WRITE != 0 {
927
- // CoW
928
- let mut page_table_ref = ProcessPageTableRef :: default ( ) ;
929
- page_table_ref. set_external_table ( self . vmsa . cr3 ) ;
930
- // Handle CoW
931
- log:: debug!( " [Trustlet] CoW: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
932
- let user_access = error_code & PF_USER != 0 ;
933
- let handled = page_table_ref. handle_cow ( VirtAddr :: from ( cr2) , user_access) ;
934
- if handled {
935
- log:: debug!( " [Trustlet] CoW: handled" ) ;
936
- return true ;
937
- }
931
+ } else {
932
+ log:: info!( "[Trustlet] #PF on present mmaped-page" ) ;
938
933
}
934
+ } else {
935
+ log:: info!( "[Trustlet] #PF: address is not mmaped-page" ) ;
936
+ }
937
+ if error_code & PF_PRESENT != 0 && error_code & PF_WRITE != 0 {
938
+ // CoW
939
+ let mut page_table_ref = ProcessPageTableRef :: default ( ) ;
940
+ page_table_ref. set_external_table ( self . vmsa . cr3 ) ;
941
+ // Handle CoW
942
+ log:: debug!( "[Trustlet] CoW: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
943
+ let user_access = error_code & PF_USER != 0 ;
944
+ let handled = page_table_ref. handle_cow ( VirtAddr :: from ( cr2) , user_access) ;
945
+ if handled {
946
+ log:: debug!( "[Trustlet] CoW: handled" ) ;
947
+ return true ;
948
+ }
949
+ log:: info!( "[Trustlet] [BUG] CoW: not handled" ) ;
939
950
}
940
951
941
952
// XXX: it should not come here
@@ -946,7 +957,7 @@ impl ProcessRuntime for PALContext {
946
957
let cr4 = self . vmsa . cr4 ;
947
958
let rsp = self . vmsa . rsp ;
948
959
let rflags = self . vmsa . rflags ;
949
- log:: info!( " [Trustlet] Page Fault!" ) ;
960
+ log:: info!( "[Trustlet] [BUG] Unhandled Page Fault!" ) ;
950
961
log:: info!( "vmsa EFER: {:?}" , efer) ;
951
962
log:: info!( "vmsa CR2: {:?}" , cr2) ;
952
963
log:: info!( "vmsa cr4: {:?}" , cr4) ;
@@ -963,7 +974,7 @@ impl ProcessRuntime for PALContext {
963
974
let offset = ( rsp & 0xFFF ) / 8 ;
964
975
let ( _mapping, stack_mapping) = map_paddr ! ( stack_base_paddr) ;
965
976
for i in 0 ..9 {
966
- log:: info!( " [Trustlet] Stack (rsp+{}): {:#x}" , i* 8 , unsafe { stack_mapping. as_ptr:: <u64 >( ) . offset( ( offset + i) . try_into( ) . unwrap( ) ) . read( ) } ) ;
977
+ log:: info!( "[Trustlet] Stack (rsp+{}): {:#x}" , i* 8 , unsafe { stack_mapping. as_ptr:: <u64 >( ) . offset( ( offset + i) . try_into( ) . unwrap( ) ) . read( ) } ) ;
967
978
}
968
979
969
980
/*
@@ -974,22 +985,21 @@ impl ProcessRuntime for PALContext {
974
985
return true;
975
986
*/
976
987
977
- // #PF for other reasons
978
- log:: info!( " [Trustlet] Unhandled #PF: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
988
+ log:: info!( "[Trustlet] #PF: RIP={:#x}, CR2={:#x}, Error code={:?}" , rip, cr2, error_code) ;
979
989
if error_code & PF_PRESENT == 0 {
980
- log:: info!( " [Trustlet] Page fault: not present" ) ;
990
+ log:: info!( "[Trustlet] Page fault: not present" ) ;
981
991
}
982
992
if error_code & PF_WRITE != 0 {
983
- log:: info!( " [Trustlet] Page fault: write" ) ;
993
+ log:: info!( "[Trustlet] Page fault: write" ) ;
984
994
}
985
995
if error_code & PF_USER != 0 {
986
- log:: info!( " [Trustlet] Page fault: user" ) ;
996
+ log:: info!( "[Trustlet] Page fault: user" ) ;
987
997
}
988
998
if error_code & PF_RESERVED != 0 {
989
- log:: info!( " [Trustlet] Page fault: reserved" ) ;
999
+ log:: info!( "[Trustlet] Page fault: reserved" ) ;
990
1000
}
991
1001
if error_code & PF_INSTRUCTION != 0 {
992
- log:: info!( " [Trustlet] Page fault: instruction fetch" ) ;
1002
+ log:: info!( "[Trustlet] Page fault: instruction fetch" ) ;
993
1003
}
994
1004
false
995
1005
}
0 commit comments