Skip to content

Commit cac0617

Browse files
committed
Try unmap/remap instead since prev didn't work
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 93ae2a1 commit cac0617

File tree

1 file changed

+9
-16
lines changed
  • src/hyperlight_host/src/hypervisor/virtual_machine

1 file changed

+9
-16
lines changed

src/hyperlight_host/src/hypervisor/virtual_machine/whp.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,10 @@ struct NptFlushPage {
112112
surrogate_addr: *mut c_void,
113113
/// Cached function pointer for `WHvMapGpaRange2`.
114114
map_gpa_range2: WHvMapGpaRange2Func,
115-
/// Toggled on each flush to alternate the GPA permission between
116-
/// READ and READ|WRITE.
117-
toggle: bool,
118115
}
119116

120117
impl NptFlushPage {
121118
const GPA_FLAGS_READ: WHV_MAP_GPA_RANGE_FLAGS = WHvMapGpaRangeFlagRead;
122-
const GPA_FLAGS_READWRITE: WHV_MAP_GPA_RANGE_FLAGS =
123-
WHV_MAP_GPA_RANGE_FLAGS(WHvMapGpaRangeFlagRead.0 | WHvMapGpaRangeFlagWrite.0);
124119

125120
/// Allocate a dummy page, map it into the surrogate process, and
126121
/// create the initial GPA mapping at [`NPT_FLUSH_GPA`].
@@ -173,32 +168,30 @@ impl NptFlushPage {
173168
handle,
174169
surrogate_addr,
175170
map_gpa_range2,
176-
toggle: false,
177171
})
178172
}
179173

180-
/// Toggle the dummy page's GPA permission to force an NPT TLB
181-
/// flush. VID skips the hypercall when permissions are unchanged,
182-
/// so we alternate between READ and READ|WRITE.
174+
/// Force an NPT TLB flush by unmapping and remapping the dummy
175+
/// page. An unmap+remap cycle forces VID through the full
176+
/// delete+create GPA range path (bypassing `TryUpdateInPlace`),
177+
/// which guarantees the hypervisor issues `ValFlushNestedTb`.
183178
fn flush(
184179
&mut self,
185180
partition: WHV_PARTITION_HANDLE,
186181
surrogate_process: &SurrogateProcess,
187182
) -> std::result::Result<(), RegisterError> {
188-
self.toggle = !self.toggle;
189-
let flags = if self.toggle {
190-
Self::GPA_FLAGS_READWRITE
191-
} else {
192-
Self::GPA_FLAGS_READ
193-
};
183+
unsafe {
184+
WHvUnmapGpaRange(partition, NPT_FLUSH_GPA, PAGE_SIZE_USIZE as u64)
185+
.map_err(|e| RegisterError::ResetPartition(e.into()))?;
186+
}
194187
let res = unsafe {
195188
(self.map_gpa_range2)(
196189
partition,
197190
surrogate_process.process_handle.into(),
198191
self.surrogate_addr,
199192
NPT_FLUSH_GPA,
200193
PAGE_SIZE_USIZE as u64,
201-
flags,
194+
Self::GPA_FLAGS_READ,
202195
)
203196
};
204197
if res.is_err() {

0 commit comments

Comments
 (0)