Skip to content

Commit

Permalink
fixup! mm: improve soundness of PageRef methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Freax13 committed Sep 12, 2024
1 parent 5591c56 commit 1cb9786
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
39 changes: 39 additions & 0 deletions kernel/src/cpu/mem.rs
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),
);
}
}
1 change: 1 addition & 0 deletions kernel/src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod features;
pub mod gdt;
pub mod idt;
pub mod irq_state;
pub mod mem;
pub mod msr;
pub mod percpu;
pub mod registers;
Expand Down
46 changes: 5 additions & 41 deletions kernel/src/mm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Author: Joerg Roedel <jroedel@suse.de>

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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit 1cb9786

Please sign in to comment.