Skip to content

boot: Add freestanding version of raise_tpl #1276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2024
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
7 changes: 7 additions & 0 deletions uefi-test-runner/src/boot/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use uefi::{boot, guid, Event, Guid, Identify};

pub fn test(st: &SystemTable<Boot>) {
let bt = st.boot_services();
test_tpl();
info!("Testing timer...");
test_timer(bt);
info!("Testing events...");
Expand All @@ -28,6 +29,12 @@ pub fn test(st: &SystemTable<Boot>) {
test_install_configuration_table(st);
}

fn test_tpl() {
info!("Testing watchdog...");
// There's no way to query the TPL, so we can't assert that this does anything.
let _guard = unsafe { boot::raise_tpl(Tpl::NOTIFY) };
}

fn test_timer(bt: &BootServices) {
let timer_event = unsafe { bt.create_event(EventType::TIMER, Tpl::APPLICATION, None, None) }
.expect("Failed to create TIMER event");
Expand Down
45 changes: 44 additions & 1 deletion uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use {
pub use uefi::table::boot::{
AllocateType, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams, SearchType,
};
pub use uefi_raw::table::boot::MemoryType;
pub use uefi_raw::table::boot::{MemoryType, Tpl};

/// Global image handle. This is only set by [`set_image_handle`], and it is
/// only read by [`image_handle`].
Expand Down Expand Up @@ -58,6 +58,30 @@ fn boot_services_raw_panicking() -> NonNull<uefi_raw::table::boot::BootServices>
NonNull::new(st.boot_services).expect("boot services are not active")
}

/// Raises a task's priority level and returns a [`TplGuard`].
///
/// The effect of calling `raise_tpl` with a `Tpl` that is below the current
/// one (which, sadly, cannot be queried) is undefined by the UEFI spec,
/// which also warns against remaining at high `Tpl`s for a long time.
///
/// This function returns an RAII guard that will automatically restore the
/// original `Tpl` when dropped.
///
/// # Safety
///
/// Raising a task's priority level can affect other running tasks and
/// critical processes run by UEFI. The highest priority level is the
/// most dangerous, since it disables interrupts.
#[must_use]
pub unsafe fn raise_tpl(tpl: Tpl) -> TplGuard {
let bt = boot_services_raw_panicking();
let bt = unsafe { bt.as_ref() };

TplGuard {
old_tpl: (bt.raise_tpl)(tpl),
}
}

/// Allocates memory pages from the system.
///
/// UEFI OS loaders should allocate memory of the type `LoaderData`.
Expand Down Expand Up @@ -451,3 +475,22 @@ impl<P: Protocol + ?Sized> ScopedProtocol<P> {
self.interface.map(|mut p| unsafe { p.as_mut() })
}
}

/// RAII guard for task priority level changes.
///
/// Will automatically restore the former task priority level when dropped.
#[derive(Debug)]
pub struct TplGuard {
old_tpl: Tpl,
}

impl Drop for TplGuard {
fn drop(&mut self) {
let bt = boot_services_raw_panicking();
let bt = unsafe { bt.as_ref() };

unsafe {
(bt.restore_tpl)(self.old_tpl);
}
}
}