Skip to content

Commit a14677a

Browse files
authored
Unrolled build for rust-lang#138236
Rollup merge of rust-lang#138236 - Ayush1325:uefi-event, r=petrochenkov uefi: Add OwnedEvent abstraction - Events are going to become quite important for Networking, so needed owned abstractions. - Switch to OwnedEvent abstraction for Exit boot services event. cc ````@nicholasbishop````
2 parents aa8f0fd + aa2c24b commit a14677a

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

Diff for: library/std/src/sys/pal/uefi/helpers.rs

+53-33
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,6 @@ pub(crate) fn open_protocol<T>(
120120
}
121121
}
122122

123-
pub(crate) fn create_event(
124-
signal: u32,
125-
tpl: efi::Tpl,
126-
handler: Option<efi::EventNotify>,
127-
context: *mut crate::ffi::c_void,
128-
) -> io::Result<NonNull<crate::ffi::c_void>> {
129-
let boot_services: NonNull<efi::BootServices> =
130-
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
131-
let mut event: r_efi::efi::Event = crate::ptr::null_mut();
132-
let r = unsafe {
133-
let create_event = (*boot_services.as_ptr()).create_event;
134-
(create_event)(signal, tpl, handler, context, &mut event)
135-
};
136-
if r.is_error() {
137-
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
138-
} else {
139-
NonNull::new(event).ok_or(const_error!(io::ErrorKind::Other, "null protocol"))
140-
}
141-
}
142-
143-
/// # SAFETY
144-
/// - The supplied event must be valid
145-
pub(crate) unsafe fn close_event(evt: NonNull<crate::ffi::c_void>) -> io::Result<()> {
146-
let boot_services: NonNull<efi::BootServices> =
147-
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
148-
let r = unsafe {
149-
let close_event = (*boot_services.as_ptr()).close_event;
150-
(close_event)(evt.as_ptr())
151-
};
152-
153-
if r.is_error() { Err(crate::io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
154-
}
155-
156123
/// Gets the Protocol for current system handle.
157124
///
158125
/// Note: Some protocols need to be manually freed. It is the caller's responsibility to do so.
@@ -735,3 +702,56 @@ impl Drop for ServiceProtocol {
735702
}
736703
}
737704
}
705+
706+
#[repr(transparent)]
707+
pub(crate) struct OwnedEvent(NonNull<crate::ffi::c_void>);
708+
709+
impl OwnedEvent {
710+
pub(crate) fn new(
711+
signal: u32,
712+
tpl: efi::Tpl,
713+
handler: Option<efi::EventNotify>,
714+
context: Option<NonNull<crate::ffi::c_void>>,
715+
) -> io::Result<Self> {
716+
let boot_services: NonNull<efi::BootServices> =
717+
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
718+
let mut event: r_efi::efi::Event = crate::ptr::null_mut();
719+
let context = context.map(NonNull::as_ptr).unwrap_or(crate::ptr::null_mut());
720+
721+
let r = unsafe {
722+
let create_event = (*boot_services.as_ptr()).create_event;
723+
(create_event)(signal, tpl, handler, context, &mut event)
724+
};
725+
726+
if r.is_error() {
727+
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
728+
} else {
729+
NonNull::new(event)
730+
.ok_or(const_error!(io::ErrorKind::Other, "failed to create event"))
731+
.map(Self)
732+
}
733+
}
734+
735+
pub(crate) fn into_raw(self) -> *mut crate::ffi::c_void {
736+
let r = self.0.as_ptr();
737+
crate::mem::forget(self);
738+
r
739+
}
740+
741+
/// SAFETY: Assumes that ptr is a non-null valid UEFI event
742+
pub(crate) unsafe fn from_raw(ptr: *mut crate::ffi::c_void) -> Self {
743+
Self(unsafe { NonNull::new_unchecked(ptr) })
744+
}
745+
}
746+
747+
impl Drop for OwnedEvent {
748+
fn drop(&mut self) {
749+
if let Some(boot_services) = boot_services() {
750+
let bt: NonNull<r_efi::efi::BootServices> = boot_services.cast();
751+
unsafe {
752+
let close_event = (*bt.as_ptr()).close_event;
753+
(close_event)(self.0.as_ptr())
754+
};
755+
}
756+
}
757+
}

Diff for: library/std/src/sys/pal/uefi/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ pub(crate) unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
4646
unsafe { uefi::env::init_globals(image_handle, system_table) };
4747

4848
// Register exit boot services handler
49-
match helpers::create_event(
49+
match helpers::OwnedEvent::new(
5050
r_efi::efi::EVT_SIGNAL_EXIT_BOOT_SERVICES,
5151
r_efi::efi::TPL_NOTIFY,
5252
Some(exit_boot_service_handler),
53-
crate::ptr::null_mut(),
53+
None,
5454
) {
5555
Ok(x) => {
5656
if EXIT_BOOT_SERVICE_EVENT
5757
.compare_exchange(
5858
crate::ptr::null_mut(),
59-
x.as_ptr(),
59+
x.into_raw(),
6060
Ordering::Release,
6161
Ordering::Acquire,
6262
)
@@ -76,7 +76,7 @@ pub unsafe fn cleanup() {
7676
if let Some(exit_boot_service_event) =
7777
NonNull::new(EXIT_BOOT_SERVICE_EVENT.swap(crate::ptr::null_mut(), Ordering::Acquire))
7878
{
79-
let _ = unsafe { helpers::close_event(exit_boot_service_event) };
79+
let _ = unsafe { helpers::OwnedEvent::from_raw(exit_boot_service_event.as_ptr()) };
8080
}
8181
}
8282

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

148148
if let (Some(boot_services), Some(handle)) =

0 commit comments

Comments
 (0)