Skip to content

Commit 783f2b3

Browse files
committed
process_runtime: fix address range query
1 parent 207a63b commit 783f2b3

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

kernel/src/process_runtime/runtime.rs

+38-28
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ impl MmapManager {
119119

120120
pub fn lookup(&self, addr: usize) -> Option<&MmapInfo> {
121121
// 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
127128
}
128129
}
129130

@@ -252,6 +253,7 @@ pub fn invoke_trustlet(params: &mut RequestParams) -> Result<(), SvsmReqError> {
252253
// update trustlet's page table
253254
let flags = ProcessPageFlags::FLAG_REUSE;
254255
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);
255257
}
256258
}
257259

@@ -716,6 +718,8 @@ impl ProcessRuntime for PALContext {
716718
// example: [1..3] < [2..4] < [2..5] < [3..4]
717719
// page fault hander uses mmap infomation whose range contains the faulting address
718720
// 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
719723
mmap_manger.add_mapping(addr as usize, size as usize, fd as i32, offset as usize);
720724

721725
// Allocate virtul memory address
@@ -892,11 +896,12 @@ impl ProcessRuntime for PALContext {
892896
const PF_RESERVED: u64 = 1 << 3;
893897
const PF_INSTRUCTION: u64 = 1 << 4;
894898
let mmap_manager = &self.process.mmap_manager;
899+
log::info!("[Trustlet] #PF: CR2=0x{:x}", cr2);
895900
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);
897902
if error_code & PF_PRESENT == 0 {
898903
// non-presente page
899-
log::debug!(" [Trustlet] Page fault: not present page");
904+
log::debug!("[Trustlet] Page fault: not present page");
900905
let target_page_addr = cr2 & !0xFFF;
901906
self.process.pf_target_vaddr = target_page_addr;
902907
assert!(target_page_addr >= mmap_info.addr as u64);
@@ -923,19 +928,25 @@ impl ProcessRuntime for PALContext {
923928
// make a guest request to load the page
924929
self.return_value = TrustletReturnType::MMAP as u64;
925930
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");
938933
}
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");
939950
}
940951

941952
// XXX: it should not come here
@@ -946,7 +957,7 @@ impl ProcessRuntime for PALContext {
946957
let cr4 = self.vmsa.cr4;
947958
let rsp = self.vmsa.rsp;
948959
let rflags = self.vmsa.rflags;
949-
log::info!(" [Trustlet] Page Fault!");
960+
log::info!("[Trustlet] [BUG] Unhandled Page Fault!");
950961
log::info!("vmsa EFER: {:?}", efer);
951962
log::info!("vmsa CR2: {:?}", cr2);
952963
log::info!("vmsa cr4: {:?}", cr4);
@@ -963,7 +974,7 @@ impl ProcessRuntime for PALContext {
963974
let offset = (rsp & 0xFFF) / 8;
964975
let (_mapping, stack_mapping) = map_paddr!(stack_base_paddr);
965976
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()});
967978
}
968979

969980
/*
@@ -974,22 +985,21 @@ impl ProcessRuntime for PALContext {
974985
return true;
975986
*/
976987

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);
979989
if error_code & PF_PRESENT == 0 {
980-
log::info!(" [Trustlet] Page fault: not present");
990+
log::info!("[Trustlet] Page fault: not present");
981991
}
982992
if error_code & PF_WRITE != 0 {
983-
log::info!(" [Trustlet] Page fault: write");
993+
log::info!("[Trustlet] Page fault: write");
984994
}
985995
if error_code & PF_USER != 0 {
986-
log::info!(" [Trustlet] Page fault: user");
996+
log::info!("[Trustlet] Page fault: user");
987997
}
988998
if error_code & PF_RESERVED != 0 {
989-
log::info!(" [Trustlet] Page fault: reserved");
999+
log::info!("[Trustlet] Page fault: reserved");
9901000
}
9911001
if error_code & PF_INSTRUCTION != 0 {
992-
log::info!(" [Trustlet] Page fault: instruction fetch");
1002+
log::info!("[Trustlet] Page fault: instruction fetch");
9931003
}
9941004
false
9951005
}

0 commit comments

Comments
 (0)