-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! mm: improve soundness of PageRef methods
- Loading branch information
Showing
3 changed files
with
45 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use core::arch::asm; | ||
|
||
/// Copy `size` bytes from `src` to `dst`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function has all the safety requirements of `core::ptr::copy` except | ||
/// that data races (both on `src` and `dst`) are explicitly permitted. | ||
#[inline(always)] | ||
pub unsafe fn copy_bytes(src: usize, dst: usize, size: usize) { | ||
unsafe { | ||
asm!( | ||
"rep movsb", | ||
inout("rsi") src => _, | ||
inout("rdi") dst => _, | ||
inout("rcx") size => _, | ||
options(nostack), | ||
); | ||
} | ||
} | ||
|
||
/// Set `size` bytes at `dst` to `val`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function has all the safety requirements of `core::ptr::write_bytes` except | ||
/// that data races are explicitly permitted. | ||
#[inline(always)] | ||
pub unsafe fn write_bytes(dst: usize, size: usize, value: u8) { | ||
unsafe { | ||
asm!( | ||
"rep stosb", | ||
inout("rdi") dst => _, | ||
inout("rcx") size => _, | ||
in("al") value, | ||
options(nostack), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,13 @@ | |
// Author: Joerg Roedel <[email protected]> | ||
|
||
use crate::address::{Address, PhysAddr, VirtAddr}; | ||
use crate::cpu::mem::{copy_bytes, write_bytes}; | ||
use crate::error::SvsmError; | ||
use crate::locking::SpinLock; | ||
use crate::mm::virt_to_phys; | ||
use crate::types::{PAGE_SHIFT, PAGE_SIZE}; | ||
use crate::utils::{align_down, align_up, zero_mem_region}; | ||
use core::alloc::{GlobalAlloc, Layout}; | ||
use core::arch::asm; | ||
use core::mem::size_of; | ||
use core::ptr; | ||
|
||
|
@@ -939,7 +939,7 @@ impl PageRef { | |
let size = PAGE_SIZE; | ||
unsafe { | ||
// SAFETY: `src` and `dst` are both valid. | ||
rep_movs(src, dst, size); | ||
copy_bytes(src, dst, size); | ||
} | ||
|
||
Ok(PageRef { | ||
|
@@ -956,7 +956,7 @@ impl PageRef { | |
let size = buf.len(); | ||
unsafe { | ||
// SAFETY: `src` and `dst` are both valid. | ||
rep_movs(src, dst, size); | ||
copy_bytes(src, dst, size); | ||
} | ||
} | ||
|
||
|
@@ -968,7 +968,7 @@ impl PageRef { | |
let size = buf.len(); | ||
unsafe { | ||
// SAFETY: `src` and `dst` are both valid. | ||
rep_movs(src, dst, size); | ||
copy_bytes(src, dst, size); | ||
} | ||
} | ||
|
||
|
@@ -978,47 +978,11 @@ impl PageRef { | |
|
||
unsafe { | ||
// SAFETY: `dst` is valid. | ||
rep_stosb(dst, size, value); | ||
write_bytes(dst, size, value); | ||
} | ||
} | ||
} | ||
|
||
/// Copy `size` bytes from `src` to `dst`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function has all the safety requirements of `core::ptr::copy` except | ||
/// that data races (both on `src` and `dst`) are explicitly permitted. | ||
#[inline(always)] | ||
unsafe fn rep_movs(src: usize, dst: usize, size: usize) { | ||
unsafe { | ||
asm!("rep movsb", | ||
inout("rsi") src => _, | ||
inout("rdi") dst => _, | ||
inout("rcx") size => _, | ||
options(nostack), | ||
); | ||
} | ||
} | ||
|
||
/// Set `size` bytes at `dst` to `val`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This function has all the safety requirements of `core::ptr::write_bytes` except | ||
/// that data races are explicitly permitted. | ||
#[inline(always)] | ||
unsafe fn rep_stosb(dst: usize, size: usize, value: u8) { | ||
unsafe { | ||
asm!("rep stosb", | ||
inout("rdi") dst => _, | ||
inout("rcx") size => _, | ||
in("al") value, | ||
options(nostack), | ||
); | ||
} | ||
} | ||
|
||
impl Clone for PageRef { | ||
/// Clones the [`PageRef`] instance, obtaining a new reference to the same memory page. | ||
fn clone(&self) -> Self { | ||
|