-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #105861 - Ayush1325:uefi-std-minimial, r=workingjubilee
Add Minimal Std implementation for UEFI # Implemented modules: 1. alloc 2. os_str 3. env 4. math # Related Links Tracking Issue: #100499 API Change Proposal: rust-lang/libs-team#87 # Additional Information This was originally part of #100316. Since that PR was becoming too unwieldy and cluttered, and with suggestion from `@dvdhrm,` I have extracted a minimal std implementation to this PR. The example in `src/doc/rustc/src/platform-support/unknown-uefi.md` has been tested for `x86_64-unknown-uefi` and `i686-unknown-uefi` in OVMF. It would be great if someone more familiar with AARCH64 can help with testing for that target. Signed-off-by: Ayush Singh <[email protected]>
- Loading branch information
Showing
25 changed files
with
957 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//! UEFI-specific extensions to the primitives in `std::env` module | ||
#![unstable(feature = "uefi_std", issue = "100499")] | ||
|
||
use crate::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; | ||
use crate::{ffi::c_void, ptr::NonNull}; | ||
|
||
static SYSTEM_TABLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut()); | ||
static IMAGE_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut()); | ||
// Flag to check if BootServices are still valid. | ||
// Start with assuming that they are not available | ||
static BOOT_SERVICES_FLAG: AtomicBool = AtomicBool::new(false); | ||
|
||
/// Initializes the global System Table and Image Handle pointers. | ||
/// | ||
/// The standard library requires access to the UEFI System Table and the Application Image Handle | ||
/// to operate. Those are provided to UEFI Applications via their application entry point. By | ||
/// calling `init_globals()`, those pointers are retained by the standard library for future use. | ||
/// Thus this function must be called before any of the standard library services are used. | ||
/// | ||
/// The pointers are never exposed to any entity outside of this application and it is guaranteed | ||
/// that, once the application exited, these pointers are never dereferenced again. | ||
/// | ||
/// Callers are required to ensure the pointers are valid for the entire lifetime of this | ||
/// application. In particular, UEFI Boot Services must not be exited while an application with the | ||
/// standard library is loaded. | ||
/// | ||
/// # SAFETY | ||
/// Calling this function more than once will panic | ||
pub(crate) unsafe fn init_globals(handle: NonNull<c_void>, system_table: NonNull<c_void>) { | ||
IMAGE_HANDLE | ||
.compare_exchange( | ||
crate::ptr::null_mut(), | ||
handle.as_ptr(), | ||
Ordering::Release, | ||
Ordering::Acquire, | ||
) | ||
.unwrap(); | ||
SYSTEM_TABLE | ||
.compare_exchange( | ||
crate::ptr::null_mut(), | ||
system_table.as_ptr(), | ||
Ordering::Release, | ||
Ordering::Acquire, | ||
) | ||
.unwrap(); | ||
BOOT_SERVICES_FLAG.store(true, Ordering::Release) | ||
} | ||
|
||
/// Get the SystemTable Pointer. | ||
/// If you want to use `BootServices` then please use [`boot_services`] as it performs some | ||
/// additional checks. | ||
/// | ||
/// Note: This function panics if the System Table or Image Handle is not initialized | ||
pub fn system_table() -> NonNull<c_void> { | ||
try_system_table().unwrap() | ||
} | ||
|
||
/// Get the ImageHandle Pointer. | ||
/// | ||
/// Note: This function panics if the System Table or Image Handle is not initialized | ||
pub fn image_handle() -> NonNull<c_void> { | ||
try_image_handle().unwrap() | ||
} | ||
|
||
/// Get the BootServices Pointer. | ||
/// This function also checks if `ExitBootServices` has already been called. | ||
pub fn boot_services() -> Option<NonNull<c_void>> { | ||
if BOOT_SERVICES_FLAG.load(Ordering::Acquire) { | ||
let system_table: NonNull<r_efi::efi::SystemTable> = try_system_table()?.cast(); | ||
let boot_services = unsafe { (*system_table.as_ptr()).boot_services }; | ||
NonNull::new(boot_services).map(|x| x.cast()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Get the SystemTable Pointer. | ||
/// This function is mostly intended for places where panic is not an option | ||
pub(crate) fn try_system_table() -> Option<NonNull<c_void>> { | ||
NonNull::new(SYSTEM_TABLE.load(Ordering::Acquire)) | ||
} | ||
|
||
/// Get the SystemHandle Pointer. | ||
/// This function is mostly intended for places where panicking is not an option | ||
pub(crate) fn try_image_handle() -> Option<NonNull<c_void>> { | ||
NonNull::new(IMAGE_HANDLE.load(Ordering::Acquire)) | ||
} | ||
|
||
pub(crate) fn disable_boot_services() { | ||
BOOT_SERVICES_FLAG.store(false, Ordering::Release) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//! Platform-specific extensions to `std` for UEFI. | ||
#![unstable(feature = "uefi_std", issue = "100499")] | ||
#![doc(cfg(target_os = "uefi"))] | ||
|
||
pub mod env; | ||
#[path = "../windows/ffi.rs"] | ||
pub mod ffi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//! Global Allocator for UEFI. | ||
//! Uses [r-efi-alloc](https://crates.io/crates/r-efi-alloc) | ||
use crate::alloc::{GlobalAlloc, Layout, System}; | ||
|
||
const MEMORY_TYPE: u32 = r_efi::efi::LOADER_DATA; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
// Return null pointer if boot services are not available | ||
if crate::os::uefi::env::boot_services().is_none() { | ||
return crate::ptr::null_mut(); | ||
} | ||
|
||
// If boot services is valid then SystemTable is not null. | ||
let system_table = crate::os::uefi::env::system_table().as_ptr().cast(); | ||
// The caller must ensure non-0 layout | ||
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) } | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
// Do nothing if boot services are not available | ||
if crate::os::uefi::env::boot_services().is_none() { | ||
return; | ||
} | ||
|
||
// If boot services is valid then SystemTable is not null. | ||
let system_table = crate::os::uefi::env::system_table().as_ptr().cast(); | ||
// The caller must ensure non-0 layout | ||
unsafe { r_efi_alloc::raw::dealloc(system_table, ptr, layout) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pub mod os { | ||
pub const FAMILY: &str = ""; | ||
pub const OS: &str = "uefi"; | ||
pub const DLL_PREFIX: &str = ""; | ||
pub const DLL_SUFFIX: &str = ""; | ||
pub const DLL_EXTENSION: &str = ""; | ||
pub const EXE_SUFFIX: &str = ".efi"; | ||
pub const EXE_EXTENSION: &str = "efi"; | ||
} |
Oops, something went wrong.