Skip to content

uefi: Add OwnedEvent abstraction #138236

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 1 commit into from
Mar 24, 2025
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
86 changes: 53 additions & 33 deletions library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,39 +120,6 @@ pub(crate) fn open_protocol<T>(
}
}

pub(crate) fn create_event(
signal: u32,
tpl: efi::Tpl,
handler: Option<efi::EventNotify>,
context: *mut crate::ffi::c_void,
) -> io::Result<NonNull<crate::ffi::c_void>> {
let boot_services: NonNull<efi::BootServices> =
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
let mut event: r_efi::efi::Event = crate::ptr::null_mut();
let r = unsafe {
let create_event = (*boot_services.as_ptr()).create_event;
(create_event)(signal, tpl, handler, context, &mut event)
};
if r.is_error() {
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
} else {
NonNull::new(event).ok_or(const_error!(io::ErrorKind::Other, "null protocol"))
}
}

/// # SAFETY
/// - The supplied event must be valid
pub(crate) unsafe fn close_event(evt: NonNull<crate::ffi::c_void>) -> io::Result<()> {
let boot_services: NonNull<efi::BootServices> =
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
let r = unsafe {
let close_event = (*boot_services.as_ptr()).close_event;
(close_event)(evt.as_ptr())
};

if r.is_error() { Err(crate::io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
}

/// Gets the Protocol for current system handle.
///
/// Note: Some protocols need to be manually freed. It is the caller's responsibility to do so.
Expand Down Expand Up @@ -559,3 +526,56 @@ impl Drop for ServiceProtocol {
}
}
}

#[repr(transparent)]
pub(crate) struct OwnedEvent(NonNull<crate::ffi::c_void>);

impl OwnedEvent {
pub(crate) fn new(
signal: u32,
tpl: efi::Tpl,
handler: Option<efi::EventNotify>,
context: Option<NonNull<crate::ffi::c_void>>,
) -> io::Result<Self> {
let boot_services: NonNull<efi::BootServices> =
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
let mut event: r_efi::efi::Event = crate::ptr::null_mut();
let context = context.map(NonNull::as_ptr).unwrap_or(crate::ptr::null_mut());

let r = unsafe {
let create_event = (*boot_services.as_ptr()).create_event;
(create_event)(signal, tpl, handler, context, &mut event)
};

if r.is_error() {
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
} else {
NonNull::new(event)
.ok_or(const_error!(io::ErrorKind::Other, "failed to create event"))
.map(Self)
}
}

pub(crate) fn into_raw(self) -> *mut crate::ffi::c_void {
let r = self.0.as_ptr();
crate::mem::forget(self);
r
}

/// SAFETY: Assumes that ptr is a non-null valid UEFI event
pub(crate) unsafe fn from_raw(ptr: *mut crate::ffi::c_void) -> Self {
Self(unsafe { NonNull::new_unchecked(ptr) })
}
}

impl Drop for OwnedEvent {
fn drop(&mut self) {
if let Some(boot_services) = boot_services() {
let bt: NonNull<r_efi::efi::BootServices> = boot_services.cast();
unsafe {
let close_event = (*bt.as_ptr()).close_event;
(close_event)(self.0.as_ptr())
};
}
}
}
10 changes: 5 additions & 5 deletions library/std/src/sys/pal/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ pub(crate) unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
unsafe { uefi::env::init_globals(image_handle, system_table) };

// Register exit boot services handler
match helpers::create_event(
match helpers::OwnedEvent::new(
r_efi::efi::EVT_SIGNAL_EXIT_BOOT_SERVICES,
r_efi::efi::TPL_NOTIFY,
Some(exit_boot_service_handler),
crate::ptr::null_mut(),
None,
) {
Ok(x) => {
if EXIT_BOOT_SERVICE_EVENT
.compare_exchange(
crate::ptr::null_mut(),
x.as_ptr(),
x.into_raw(),
Ordering::Release,
Ordering::Acquire,
)
Expand All @@ -79,7 +79,7 @@ pub unsafe fn cleanup() {
if let Some(exit_boot_service_event) =
NonNull::new(EXIT_BOOT_SERVICE_EVENT.swap(crate::ptr::null_mut(), Ordering::Acquire))
{
let _ = unsafe { helpers::close_event(exit_boot_service_event) };
let _ = unsafe { helpers::OwnedEvent::from_raw(exit_boot_service_event.as_ptr()) };
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ pub fn abort_internal() -> ! {
if let Some(exit_boot_service_event) =
NonNull::new(EXIT_BOOT_SERVICE_EVENT.load(Ordering::Acquire))
{
let _ = unsafe { helpers::close_event(exit_boot_service_event) };
let _ = unsafe { helpers::OwnedEvent::from_raw(exit_boot_service_event.as_ptr()) };
}

if let (Some(boot_services), Some(handle)) =
Expand Down
Loading