Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
18d4a63
msg_proto
Dec 10, 2025
2c4d585
improve optee msg handlers
Dec 11, 2025
17da58f
improve abstraction
Dec 11, 2025
1941a9f
revison and add RemotePtr placeholders
Dec 11, 2025
6557c40
rename
Dec 11, 2025
91032b7
revision
Dec 12, 2025
e760550
revise remote pointers
Dec 12, 2025
731ca17
ratchet
Dec 12, 2025
0e08c57
handle ta request. wip
Dec 12, 2025
b7fc736
support tmem and some revision
Dec 12, 2025
8d87f2e
fix tmem and rmem handling
Dec 12, 2025
052c7ab
separate out handle_ta_request
Dec 12, 2025
e127b63
get os uuid
Dec 12, 2025
06ce355
comment
Dec 12, 2025
27b7367
replace Errno with OpteeSmcReturn
Dec 12, 2025
077f951
add comments
Dec 14, 2025
900ab8c
validate message
Dec 14, 2025
79eca70
clarification
Dec 15, 2025
57694d0
get rid of recursive handler invocation
Dec 16, 2025
6a9cf6e
some docs for physical pointer (wip)
Dec 19, 2025
e69d204
improve phys ptr abstraction (wip)
Dec 20, 2025
ff9fc36
checkpoint
Dec 22, 2025
a3c9a4a
separate const and mut ptrs
Dec 22, 2025
5fd251d
read/write slice
Dec 22, 2025
b764f0a
revised
Dec 22, 2025
e2a25b5
check page contiguity
Dec 23, 2025
75ee395
VmapProvider
Dec 23, 2025
52326a1
addressed comments
Dec 24, 2025
d06ca13
impl Drop for PhysPtrs
Dec 24, 2025
b418268
VmapProvider validate and protect
Dec 24, 2025
378097a
use existing NonZeroAddress
Dec 26, 2025
04fa48d
rename
Dec 29, 2025
c0a1030
use PhysPageAddr for ShmRefMap
Dec 29, 2025
2fbb19d
clarification
Jan 8, 2026
532ebe9
move VmapProvider to litebox_common_optee
Jan 21, 2026
45ba6db
vmap: replace todos with actual impl
Jan 22, 2026
f35be19
revise VmapProvider
Jan 22, 2026
00fa88c
revise comments
Jan 22, 2026
8bfbd9b
feedbacks
Jan 23, 2026
1ae12d9
ratchet
Jan 23, 2026
e46f5dc
clippy
Jan 23, 2026
25c0151
feature gate
Jan 23, 2026
ae7a2e1
move vmap to litebox_common_linux
Jan 23, 2026
ce31693
rename
Jan 23, 2026
4d0df5e
typo
Jan 24, 2026
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dev_tests/src/ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn ratchet_globals() -> Result<()> {
("litebox_runner_lvbs/", 3),
("litebox_runner_snp/", 1),
("litebox_shim_linux/", 1),
("litebox_shim_optee/", 1),
("litebox_shim_optee/", 2),
],
|file| {
Ok(file
Expand Down Expand Up @@ -69,6 +69,7 @@ fn ratchet_maybe_uninit() -> Result<()> {
("litebox_platform_linux_userland/", 3),
("litebox_platform_lvbs/", 5),
("litebox_shim_linux/", 5),
("litebox_shim_optee/", 1),
],
|file| {
Ok(file
Expand Down
1 change: 1 addition & 0 deletions litebox/src/mm/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ impl<const ALIGN: usize> core::ops::Add<usize> for NonZeroPageSize<ALIGN> {
}

/// A non-zero address that is `ALIGN`-aligned.
#[derive(Clone, Copy)]
pub struct NonZeroAddress<const ALIGN: usize>(usize);

impl<const ALIGN: usize> NonZeroAddress<ALIGN> {
Expand Down
1 change: 1 addition & 0 deletions litebox_common_linux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod errno;
pub mod loader;
pub mod mm;
pub mod signal;
pub mod vmap;

extern crate alloc;

Expand Down
175 changes: 175 additions & 0 deletions litebox_common_linux/src/vmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use litebox::platform::page_mgmt::MemoryRegionPermissions;
use thiserror::Error;

/// A provider to map and unmap physical pages with virtually contiguous addresses.
///
/// `ALIGN`: The page frame size.
///
/// This provider exists to service `litebox_shim_optee::ptr::PhysMutPtr` and
/// `litebox_shim_optee::ptr::PhysConstPtr`. It can benefit other modules which need
/// Linux kernel's `vmap()` and `vunmap()` functionalities (e.g., HVCI/HEKI, drivers).
pub trait VmapManager<const ALIGN: usize> {
/// Map the given `PhysPageAddrArray` into virtually contiguous addresses with the given
/// [`PhysPageMapPermissions`] while returning [`PhysPageMapInfo`].
///
/// This function is analogous to Linux kernel's `vmap()`.
///
/// # Safety
///
/// The caller should ensure that `pages` are not in active use by other entities
/// (especially, there should be no read/write or write/write conflicts).
/// Unfortunately, LiteBox itself cannot fully guarantee this and it needs some helps
/// from the caller, hypervisor, or hardware.
/// Multiple LiteBox threads might concurrently call this function with overlapping
/// physical pages, so the implementation should safely handle such cases.
unsafe fn vmap(
&self,
_pages: &PhysPageAddrArray<ALIGN>,
_perms: PhysPageMapPermissions,
) -> Result<PhysPageMapInfo<ALIGN>, PhysPointerError> {
Err(PhysPointerError::UnsupportedOperation)
}

/// Unmap the previously mapped virtually contiguous addresses ([`PhysPageMapInfo`]).
///
/// This function is analogous to Linux kernel's `vunmap()`.
///
/// # Safety
///
/// The caller should ensure that the virtual addresses in `vmap_info` are not in active
/// use by other entities.
unsafe fn vunmap(&self, _vmap_info: PhysPageMapInfo<ALIGN>) -> Result<(), PhysPointerError> {
Err(PhysPointerError::UnsupportedOperation)
}

/// Validate that the given physical pages are not owned by LiteBox.
///
/// Platform is expected to track which physical memory addresses are owned by LiteBox (e.g., VTL1 memory addresses).
///
/// Returns `Ok(())` if the physical pages are not owned by LiteBox. Otherwise, returns `Err(PhysPointerError)`.
fn validate_unowned(&self, _pages: &PhysPageAddrArray<ALIGN>) -> Result<(), PhysPointerError> {
Ok(())
}

/// Protect the given physical pages to ensure concurrent read or exclusive write access:
/// - Read protection: prevent others from writing to the pages.
/// - Read/write protection: prevent others from reading or writing to the pages.
/// - No protection: allow others to read and write the pages.
///
/// This function can be implemented using EPT/NPT, TZASC, PMP, or some other hardware mechanisms.
/// If the platform does not support such protection, this function returns `Ok(())` without any action.
///
/// Returns `Ok(())` if it successfully protects the pages. If it fails, returns
/// `Err(PhysPointerError)`.
///
/// # Safety
///
/// This function relies on hypercalls or other privileged hardware features and assumes those features
/// are safe to use.
/// The caller should unprotect the pages when they are no longer needed to access them.
unsafe fn protect(
&self,
_pages: &PhysPageAddrArray<ALIGN>,
_perms: PhysPageMapPermissions,
) -> Result<(), PhysPointerError> {
Ok(())
}
}

/// Data structure representing a physical address with page alignment.
///
/// Currently, this is an alias to `crate::mm::linux::NonZeroAddress`. This might change if
/// we selectively conduct sanity checks based on whether an address is virtual or physical
/// (e.g., whether a virtual address is canonical, whether a physical address is tagged with
/// a valid key ID, etc.).
pub type PhysPageAddr<const ALIGN: usize> = litebox::mm::linux::NonZeroAddress<ALIGN>;

/// Data structure for an array of physical page addresses which are virtually contiguous.
pub type PhysPageAddrArray<const ALIGN: usize> = [PhysPageAddr<ALIGN>];

/// Data structure to maintain the mapping information returned by `vmap()`.
#[derive(Clone)]
pub struct PhysPageMapInfo<const ALIGN: usize> {
/// Virtual address of the mapped region which is page aligned.
pub base: *mut u8,
/// The size of the mapped region in bytes.
pub size: usize,
}

bitflags::bitflags! {
/// Physical page map permissions which is a restricted version of
/// [`litebox::platform::page_mgmt::MemoryRegionPermissions`].
///
/// This module only supports READ and WRITE permissions. Both EXECUTE and SHARED
/// permissions are explicitly prohibited.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PhysPageMapPermissions: u8 {
/// Readable
const READ = 1 << 0;
/// Writable
const WRITE = 1 << 1;
}
}

impl From<MemoryRegionPermissions> for PhysPageMapPermissions {
fn from(perms: MemoryRegionPermissions) -> Self {
let mut phys_perms = PhysPageMapPermissions::empty();
if perms.contains(MemoryRegionPermissions::READ) {
phys_perms |= PhysPageMapPermissions::READ;
}
if perms.contains(MemoryRegionPermissions::WRITE) {
phys_perms |= PhysPageMapPermissions::WRITE;
}
phys_perms
}
}

impl From<PhysPageMapPermissions> for MemoryRegionPermissions {
fn from(perms: PhysPageMapPermissions) -> Self {
let mut mem_perms = MemoryRegionPermissions::empty();
if perms.contains(PhysPageMapPermissions::READ) {
mem_perms |= MemoryRegionPermissions::READ;
}
if perms.contains(PhysPageMapPermissions::WRITE) {
mem_perms |= MemoryRegionPermissions::WRITE;
}
mem_perms
}
}

/// Possible errors for physical pointer access with `VmapManager`
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum PhysPointerError {
#[error("Physical address {0:#x} is invalid to access")]
InvalidPhysicalAddress(usize),
#[error("Physical address {0:#x} is not aligned to {1} bytes")]
UnalignedPhysicalAddress(usize, usize),
#[error("Offset {0:#x} is not aligned to {1} bytes")]
UnalignedOffset(usize, usize),
#[error("Base offset {0:#x} is greater than or equal to alignment ({1} bytes)")]
InvalidBaseOffset(usize, usize),
#[error(
"The total size of the given pages ({0} bytes) is insufficient for the requested type ({1} bytes)"
)]
InsufficientPhysicalPages(usize, usize),
#[error("Index {0} is out of bounds (count: {1})")]
IndexOutOfBounds(usize, usize),
#[error("Physical address {0:#x} is already mapped")]
AlreadyMapped(usize),
#[error("Physical address {0:#x} is unmapped")]
Unmapped(usize),
#[error("No mapping information available")]
NoMappingInfo,
#[error("Overflow occurred during calculation")]
Overflow,
#[error("Non-contiguous physical pages in the array")]
NonContiguousPages,
#[error("The operation is unsupported on this platform")]
UnsupportedOperation,
#[error("Unsupported permissions: {0:#x}")]
UnsupportedPermissions(u8),
}
1 change: 1 addition & 0 deletions litebox_common_optee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ litebox = { path = "../litebox/", version = "0.1.0" }
litebox_common_linux = { path = "../litebox_common_linux/", version = "0.1.0" }
modular-bitfield = { version = "0.12.0", default-features = false }
num_enum = { version = "0.7.3", default-features = false }
thiserror = { version = "2.0.6", default-features = false }

[lints]
workspace = true
Loading