Skip to content

Commit 306b3e1

Browse files
authored
Merge pull request #472 from rust-osdev/fix/pcid-no-flush
add write_pcid_no_flush
2 parents 115f0e4 + 472d863 commit 306b3e1

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/registers/control.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ mod x86_64 {
308308
#[inline]
309309
pub unsafe fn write(frame: PhysFrame, flags: Cr3Flags) {
310310
unsafe {
311-
Cr3::write_raw(frame, flags.bits() as u16);
311+
Cr3::write_raw_impl(false, frame, flags.bits() as u16);
312312
}
313313
}
314314

@@ -322,7 +322,22 @@ mod x86_64 {
322322
#[inline]
323323
pub unsafe fn write_pcid(frame: PhysFrame, pcid: Pcid) {
324324
unsafe {
325-
Cr3::write_raw(frame, pcid.value());
325+
Cr3::write_raw_impl(false, frame, pcid.value());
326+
}
327+
}
328+
329+
/// Write a new P4 table address into the CR3 register without flushing existing TLB entries for
330+
/// the PCID.
331+
///
332+
/// ## Safety
333+
///
334+
/// Changing the level 4 page table is unsafe, because it's possible to violate memory safety by
335+
/// changing the page mapping.
336+
/// [`Cr4Flags::PCID`] must be set before calling this method.
337+
#[inline]
338+
pub unsafe fn write_pcid_no_flush(frame: PhysFrame, pcid: Pcid) {
339+
unsafe {
340+
Cr3::write_raw_impl(true, frame, pcid.value());
326341
}
327342
}
328343

@@ -334,8 +349,13 @@ mod x86_64 {
334349
/// changing the page mapping.
335350
#[inline]
336351
pub unsafe fn write_raw(frame: PhysFrame, val: u16) {
352+
unsafe { Self::write_raw_impl(false, frame, val) }
353+
}
354+
355+
#[inline]
356+
unsafe fn write_raw_impl(top_bit: bool, frame: PhysFrame, val: u16) {
337357
let addr = frame.start_address();
338-
let value = addr.as_u64() | val as u64;
358+
let value = ((top_bit as u64) << 63) | addr.as_u64() | val as u64;
339359

340360
unsafe {
341361
asm!("mov cr3, {}", in(reg) value, options(nostack, preserves_flags));

0 commit comments

Comments
 (0)