Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "3"
members = ["components/*", "core/*", "sdk/*", "patina_dxe_core"]
members = ["components/*", "core/*", "sdk/*", "patina_dxe_core", "patina_mm_supervisor", "patina_mm_user_core"]

[workspace.package]
version = "24.4.1"
Expand Down Expand Up @@ -40,6 +40,9 @@ patina_internal_cpu = { version = "24.4.1", path = "core/patina_internal_cpu", d
lzma-rust2 = { version = "0.16", default-features = false }
patina_macro = { version = "24.4.1", path = "sdk/patina_macro" }
patina_mm = { version = "24.4.1", path = "components/patina_mm" }
patina_mm_cpu = { version = "24.4.1", path = "components/patina_mm_cpu" }
patina_mm_supervisor = { version = "24.4.1", path = "patina_mm_supervisor" }
patina_mm_user_core = { version = "24.4.1", path = "patina_mm_user_core" }
patina_mtrr = { version = "3" }
patina_paging = { version = "11.1.1" }
patina_performance = { version = "24.4.1", path = "components/patina_performance" }
Expand Down
23 changes: 23 additions & 0 deletions components/patina_mm_cpu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "patina_mm_cpu"
version.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
readme = "README.md"
description = "MM CPU component producing the EFI_MM_CPU_PROTOCOL (save-state access) in the MM User Core."
keywords.workspace = true
categories.workspace = true

[lints]
workspace = true

[dependencies]
log = { workspace = true }

patina = { workspace = true, features = ["alloc"] }

[features]
std = []
# All non-std features to test in CI
ci_features = []
10 changes: 10 additions & 0 deletions components/patina_mm_cpu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Patina MM CPU Component

The Patina MM CPU component produces the PI `EFI_MM_CPU_PROTOCOL` inside the MM User Core. The protocol allows MM
drivers to read architecture-standard registers from a CPU's MM save state, including information about the trapping
I/O instruction that generated a software MMI.

Because the MM save state resides in supervisor-only SMRAM, save-state reads are forwarded to the MM Supervisor. The
supervisor enforces the save-state security policy before returning the requested value.

Writing the save state is not supported: `WriteSaveState` always returns `EFI_UNSUPPORTED`.
153 changes: 153 additions & 0 deletions components/patina_mm_cpu/src/component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//! The MM CPU component.
//!
//! ## License
//!
//! Copyright (c) Microsoft Corporation.
//!
//! SPDX-License-Identifier: Apache-2.0
//!

use core::ffi::c_void;

use patina::{
component::component,
error::{EfiError, Result},
management_mode::mm_services::MmServiceProvider,
standard::efi,
};

use crate::protocol::{MM_CPU_PROTOCOL_GUID, MmCpuProtocol};

/// The static `EFI_MM_CPU_PROTOCOL` interface installed by [`MmCpuComponent`].
///
/// Function pointers are `Send`/`Sync`, so this is safe to place in a `static`
/// and hand to consumers as a stable interface pointer.
static MM_CPU_PROTOCOL: MmCpuProtocol =
MmCpuProtocol { read_save_state: mm_cpu_read_save_state, write_save_state: mm_cpu_write_save_state };

/// Component that produces the `EFI_MM_CPU_PROTOCOL` in the MM User Core.
///
/// Replaces the C `MmSupervisorPkg/Drivers/MmSupervisedCpu` driver. It installs
/// `gEfiMmCpuProtocolGuid`; its `ReadSaveState` forwards save-state reads to the
/// MM Supervisor, which owns SMRAM and enforces the save-state security policy.
#[derive(Default)]
pub struct MmCpuComponent;

impl MmCpuComponent {
/// Creates a new [`MmCpuComponent`].
pub fn new() -> Self {
Self
}
}

#[component]
impl MmCpuComponent {
#[cfg_attr(coverage, coverage(off))]
fn entry_point(self, mm: MmServiceProvider) -> Result<()> {
// SAFETY: `MM_CPU_PROTOCOL` is a valid, `'static` interface; a `None` handle
// requests a freshly allocated handle for the installed interface.
let result = unsafe {
mm.install_protocol_interface(
None,
&MM_CPU_PROTOCOL_GUID,
core::ptr::from_ref(&MM_CPU_PROTOCOL).cast_mut().cast::<c_void>(),
)
};

match result {
Ok(handle) => {
log::info!("Installed EFI_MM_CPU_PROTOCOL on handle {handle:p}");
Ok(())
}
Err(status) => {
log::error!("Failed to install EFI_MM_CPU_PROTOCOL: {status:?}");
EfiError::status_to_result(status)
}
}
}
}

/// `EFI_MM_CPU_PROTOCOL.ReadSaveState` implementation.
///
/// Every register is forwarded to the MM Supervisor, which reads SMRAM,
/// enforces the save-state policy, and assembles the composite
/// `EFI_MM_SAVE_STATE_IO_INFO` for the IO pseudo-register.
extern "efiapi" fn mm_cpu_read_save_state(
this: *const MmCpuProtocol,
width: usize,
register: u32,
cpu_index: usize,
buffer: *mut c_void,
) -> efi::Status {
if this.is_null() || buffer.is_null() {
return efi::Status::INVALID_PARAMETER;
}

// SAFETY: `buffer` is a caller-provided output buffer of at least `width`
// bytes (for IO, at least `size_of::<EFI_MM_SAVE_STATE_IO_INFO>()`). The
// supervisor validates that the buffer is user-owned before writing to it.
unsafe { crate::save_state::read_save_state_register(this.addr(), width, register, cpu_index, buffer.cast::<u8>()) }
}

/// `EFI_MM_CPU_PROTOCOL.WriteSaveState` implementation.
///
/// Writing the MM save state is not supported, mirroring the C `MmSupervisedCpu`
/// driver, which installs a NULL `WriteSaveState`.
extern "efiapi" fn mm_cpu_write_save_state(
_this: *const MmCpuProtocol,
_width: usize,
_register: u32,
_cpu_index: usize,
_buffer: *const c_void,
) -> efi::Status {
efi::Status::UNSUPPORTED
}

#[cfg(test)]
#[cfg_attr(coverage, coverage(off))]
mod tests {
use super::*;
use crate::protocol;

/// A non-null protocol pointer for tests. The read path never dereferences
/// `this`; it only forwards its address to the (stubbed) syscall.
fn dummy_this() -> *const MmCpuProtocol {
core::ptr::from_ref(&MM_CPU_PROTOCOL)
}

#[test]
fn test_mm_cpu_read_save_state_rejects_null_this() {
let mut buf = [0u8; 8];
let status = mm_cpu_read_save_state(core::ptr::null(), 8, protocol::REGISTER_RAX, 0, buf.as_mut_ptr().cast());
assert_eq!(status, efi::Status::INVALID_PARAMETER);
}

#[test]
fn test_mm_cpu_read_save_state_rejects_null_buffer() {
let status = mm_cpu_read_save_state(dummy_this(), 8, protocol::REGISTER_RAX, 0, core::ptr::null_mut());
assert_eq!(status, efi::Status::INVALID_PARAMETER);
}

#[test]
fn test_mm_cpu_read_save_state_forwards_every_register_to_syscall() {
let mut buf = [0u8; 8];
// Every register reaches the syscall wrapper, which on the host (non-UEFI)
// target is a stub reporting the operation as unsupported. Reaching
// UNSUPPORTED proves the forward path; the supervisor applies the policy.
// `EFI_MM_SAVE_STATE_REGISTER_RBX`. 999 is not a defined `EFI_MM_SAVE_STATE_REGISTER` value.
const REGISTER_RBX: u32 = 39;
for register in
[protocol::REGISTER_PROCESSOR_ID, protocol::REGISTER_RAX, protocol::REGISTER_IO, REGISTER_RBX, 999]
{
let status = mm_cpu_read_save_state(dummy_this(), 8, register, 0, buf.as_mut_ptr().cast());
assert_eq!(status, efi::Status::UNSUPPORTED, "register {register}");
}
}

#[test]
fn test_mm_cpu_write_save_state_is_unsupported() {
let buf = [0u8; 8];
let status = mm_cpu_write_save_state(dummy_this(), 8, protocol::REGISTER_RAX, 0, buf.as_ptr().cast());
assert_eq!(status, efi::Status::UNSUPPORTED);
}
}
43 changes: 43 additions & 0 deletions components/patina_mm_cpu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! MM CPU Component
//!
//! This crate provides the [`MmCpuComponent`](component::MmCpuComponent), which
//! produces the PI `EFI_MM_CPU_PROTOCOL` (`gEfiMmCpuProtocolGuid`) inside the MM
//! User Core. That protocol lets MM drivers read architecture-standard registers
//! from any CPU's MM save state (for example, the trapping I/O instruction that
//! generated a software MMI).
//!
//! The MM save state lives in supervisor-only SMRAM, so `ReadSaveState` forwards
//! each read to the MM Supervisor, which enforces the save-state security policy
//! before returning the value. This component is the Rust replacement for the C
//! `MmSupervisorPkg/Drivers/MmSupervisedCpu` driver.
//!
//! ## Usage
//!
//! Register the component with the MM User Core via the platform's
//! `MmComponentInfo` implementation:
//!
//! ```rust,ignore
//! use patina_mm_user_core::component_dispatcher::{Add, Component, MmComponentInfo};
//!
//! struct MyMmPlatform;
//! impl MmComponentInfo for MyMmPlatform {
//! fn components(mut add: Add<Component>) {
//! add.component(patina_mm_cpu::component::MmCpuComponent::new());
//! }
//! }
//! ```
//!
//! ## License
//!
//! Copyright (c) Microsoft Corporation.
//!
//! SPDX-License-Identifier: Apache-2.0
//!
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![cfg_attr(coverage, feature(coverage_attribute))]
#![deny(missing_docs)]

pub mod component;
pub mod protocol;

mod save_state;
58 changes: 58 additions & 0 deletions components/patina_mm_cpu/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! `EFI_MM_CPU_PROTOCOL` ABI definitions.
//!
//! Rust definitions of the PI 1.5 `EFI_MM_CPU_PROTOCOL` (`gEfiMmCpuProtocolGuid`)
//! and a few well-known `EFI_MM_SAVE_STATE_REGISTER` values. These mirror
//! `MdePkg/Include/Protocol/MmCpu.h`.
//!
//! ## License
//!
//! Copyright (c) Microsoft Corporation.
//!
//! SPDX-License-Identifier: Apache-2.0
//!

use core::ffi::c_void;

use patina::standard::efi;

/// The `gEfiMmCpuProtocolGuid` value, eb346b97-975f-4a9f-8b22-f8e92bb3d569.
pub const MM_CPU_PROTOCOL_GUID: efi::Guid =
efi::Guid::from_fields(0xeb34_6b97, 0x975f, 0x4a9f, 0x8b, 0x22, &[0xf8, 0xe9, 0x2b, 0xb3, 0xd5, 0x69]);

/// `EFI_MM_SAVE_STATE_REGISTER_RAX`, the general-purpose RAX register.
pub const REGISTER_RAX: u32 = 38;

/// `EFI_MM_SAVE_STATE_REGISTER_IO`, a pseudo-register describing the I/O
/// instruction that was in progress when the MMI was triggered. Reading it
/// returns an `EFI_MM_SAVE_STATE_IO_INFO`.
pub const REGISTER_IO: u32 = 512;

/// `EFI_MM_SAVE_STATE_REGISTER_PROCESSOR_ID`, a pseudo-register holding the CPU's ID.
pub const REGISTER_PROCESSOR_ID: u32 = 514;

/// `EFI_MM_READ_SAVE_STATE`, which reads a register from a CPU's MM save state.
pub type MmReadSaveState = extern "efiapi" fn(
this: *const MmCpuProtocol,
width: usize,
register: u32,
cpu_index: usize,
buffer: *mut c_void,
) -> efi::Status;

/// `EFI_MM_WRITE_SAVE_STATE`, which writes a register to a CPU's MM save state.
pub type MmWriteSaveState = extern "efiapi" fn(
this: *const MmCpuProtocol,
width: usize,
register: u32,
cpu_index: usize,
buffer: *const c_void,
) -> efi::Status;

/// `EFI_MM_CPU_PROTOCOL`, which gives access to CPU save-state registers while in MM.
#[repr(C)]
pub struct MmCpuProtocol {
/// Reads a register from the specified CPU's MM save state.
pub read_save_state: MmReadSaveState,
/// Writes a register to the specified CPU's MM save state.
pub write_save_state: MmWriteSaveState,
}
77 changes: 77 additions & 0 deletions components/patina_mm_cpu/src/save_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! MM save-state read syscalls from Ring 3 user MM to the MM Supervisor.
//!
//! The MM save state lives in supervisor-only SMRAM, so the CPU protocol's
//! `ReadSaveState` cannot read it directly. It issues these syscalls to have the
//! MM Supervisor perform the read (and its security-policy check) in Ring 0.
//!
//! ## ABI
//!
//! The read is a two-phase syscall, mirroring the historical `SysCallLib.h`
//! interface used by the C `MmSupervisedCpu` driver:
//!
//! 1. [`SyscallIndex::SaveStateRead`] takes the `this` token in arg1, the
//! register in arg2, and the CPU index in arg3. The supervisor validates the
//! request and records it.
//! 2. [`SyscallIndex::SaveStateRead2`] takes the `this` token in arg1, the width
//! in arg2, and the output buffer in arg3. The supervisor applies the MM
//! security policy, reads the register (building the composite
//! `EFI_MM_SAVE_STATE_IO_INFO` for the IO pseudo-register), and copies the
//! result into the caller's buffer.
//!
//! The supervisor returns the `EFI_STATUS` for each phase in RAX. The `this`
//! token is passed in both phases so the supervisor can check they match.
//!
//! ## License
//!
//! Copyright (c) Microsoft Corporation.
//!
//! SPDX-License-Identifier: Apache-2.0
//!

use patina::{
management_mode::supervisor::{SyscallIndex, raw_syscall},
standard::efi,
};

/// Reads `width` bytes of save-state `register` for CPU `cpu_index` into
/// `buffer` through the MM Supervisor.
///
/// `register` is a raw `EFI_MM_SAVE_STATE_REGISTER` value. `this` is an opaque
/// token passed in both syscall phases so the supervisor can check they match.
/// The CPU protocol producer passes its protocol interface pointer.
///
/// Returns `EFI_SUCCESS`, or the supervisor's error status such as
/// `EFI_NOT_FOUND`, `EFI_ACCESS_DENIED`, or `EFI_INVALID_PARAMETER`.
///
/// ## Safety
///
/// `buffer` must point to at least `width` bytes of user-owned, writable memory.
/// The supervisor validates that the buffer is user-owned before writing, but
/// the caller must still guarantee the pointer and length are valid.
pub(crate) unsafe fn read_save_state_register(
this: usize,
width: usize,
register: u32,
cpu_index: usize,
buffer: *mut u8,
) -> efi::Status {
// Phase 1: record the register and CPU index.
// SAFETY: `SaveStateRead` takes the `this` token, register, and CPU index as
// scalar arguments; no memory is dereferenced by the supervisor in this phase.
let phase1 = unsafe {
raw_syscall(SyscallIndex::SaveStateRead.as_u64(), this as u64, u64::from(register), cpu_index as u64)
};
let phase1 = efi::Status::from_usize(phase1 as usize);
if phase1 != efi::Status::SUCCESS {
return phase1;
}

// Phase 2: apply policy, read, and copy into the caller's buffer.
// SAFETY: `SaveStateRead2` writes up to `width` bytes into `buffer`, which the
// caller guarantees is valid for `width` bytes. The supervisor also checks user
// ownership before writing.
let phase2 = unsafe {
raw_syscall(SyscallIndex::SaveStateRead2.as_u64(), this as u64, width as u64, buffer.expose_provenance() as u64)
};
efi::Status::from_usize(phase2 as usize)
}
Loading
Loading