Skip to content
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

Add Rust std support for x86_64-unknown-uefi #100316

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f872477
Add UEFI module in std/sys
Ayush1325 Jun 15, 2022
2be029d
Improve Handling of Pointers
Ayush1325 Jul 2, 2022
ff55531
Add OsStringExt and OsStrExt traits
Ayush1325 Jul 3, 2022
b988418
Graceful abort
Ayush1325 Jul 4, 2022
2800931
Re-export r-efi in std::os::uefi::raw
Ayush1325 Jul 5, 2022
868e3e1
Implement args
Ayush1325 Jul 7, 2022
6769cb5
Implement SystemTime
Ayush1325 Jul 8, 2022
a14d62e
Implment fs
Ayush1325 Jul 10, 2022
289c445
Implement TCP
Ayush1325 Jul 16, 2022
645647d
Add UEFI support for running tests
Ayush1325 Jul 18, 2022
3e891b3
Implement Environment Variables
Ayush1325 Jul 20, 2022
5689d37
Implement append for files
Ayush1325 Jul 23, 2022
372f6e1
Allow Running remote-test-server for UEFI
Ayush1325 Jul 23, 2022
65849ae
Remove r_efi reexports from std
Ayush1325 Jul 25, 2022
ebc5ca2
Various fixes
Ayush1325 Jul 25, 2022
d5e78bf
Add vectored read/write for TcpStream
Ayush1325 Jul 26, 2022
368ab5a
Overhaul File I/O
Ayush1325 Jul 27, 2022
ce43ad6
Fix/Ignore tests
Ayush1325 Jul 27, 2022
72641b8
Implement more of fs
Ayush1325 Jul 29, 2022
95e8f68
Improve TCP
Ayush1325 Jul 30, 2022
aa980a6
Rebase to latest master
Ayush1325 Aug 2, 2022
3d9a83b
Add UEFI Std docs
Ayush1325 Aug 5, 2022
767650c
Refactor UCS-2 stuff
Ayush1325 Aug 6, 2022
4f32221
Improve process
Ayush1325 Aug 7, 2022
0bb8203
Add x86_64-uefi ci
Ayush1325 Aug 9, 2022
e85df7a
Add nothread and staticlink for remote-test-server
Ayush1325 Aug 9, 2022
1f52c23
Fix some tidy errors
Ayush1325 Aug 9, 2022
d84c8e3
Fixes from PR
Ayush1325 Aug 9, 2022
3bf5104
Update compiler_builtins
Ayush1325 Aug 10, 2022
176361a
Improve Envirnoment Variables
Ayush1325 Aug 11, 2022
88c615c
Implement hashmap_random_keys()
Ayush1325 Aug 11, 2022
8cd315c
Improve UEFI sys documentation
Ayush1325 Aug 13, 2022
4bc53e3
Fixes from PR
Ayush1325 Aug 13, 2022
c257c00
Initial implementation of Instant using TSC
Ayush1325 Aug 18, 2022
7262c22
Overhaul Dealing with UEFI ZSTs
Ayush1325 Aug 19, 2022
36c628f
Refactor `std::os::uefi`
Ayush1325 Aug 19, 2022
73852ec
Inline more functions
Ayush1325 Aug 19, 2022
d610cde
Implement `std::fs::File::set_times`
Ayush1325 Aug 20, 2022
3702334
Improve Environment variables
Ayush1325 Aug 22, 2022
c4320c1
Fix CI for x86_64-uefi
Ayush1325 Aug 23, 2022
944bf59
Update r-efi
Ayush1325 Aug 23, 2022
c83cbd2
Improve sys::process
Ayush1325 Aug 26, 2022
a95933b
Fix Env vars
Ayush1325 Aug 27, 2022
3df71e6
Apply fixes suggested in PR
Ayush1325 Aug 28, 2022
545899b
Improve Pipe Protocol
Ayush1325 Sep 13, 2022
945049c
Revert formatting changes to tests
Ayush1325 Sep 14, 2022
1baa38f
Rebase on master
Ayush1325 Sep 19, 2022
a9c1bb8
Implement io for UEFI
Ayush1325 Sep 19, 2022
939e11e
Improve std::uefi::env APIs
Ayush1325 Sep 19, 2022
67329ff
Fixes from PR
Ayush1325 Sep 20, 2022
2345867
Improve handling of Globals
Ayush1325 Oct 2, 2022
d343822
Improve UEFI fs
Ayush1325 Oct 2, 2022
dae6b19
Remove ignore-uefi from tests
Ayush1325 Oct 2, 2022
195633a
Improve TCP
Ayush1325 Oct 5, 2022
4f7ff72
Fixes from PR
Ayush1325 Oct 5, 2022
77283ae
Use compiler generated entry point for UEFI
Ayush1325 Oct 6, 2022
f89fceb
Fixes after rebase
Ayush1325 Oct 14, 2022
000228b
Implement UEFI argument parsing
Ayush1325 Dec 1, 2022
d48d07d
Use r-efi-alloc for allocator
Ayush1325 Dec 6, 2022
824f067
Fixes suggested in PR
Ayush1325 Dec 19, 2022
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
Prev Previous commit
Next Next commit
Use r-efi-alloc for allocator
Switch to using r-efi-alloc for allocation. This is useful since a lot
of UEFI projects will probably end up using this allocator anyway to
have custom allocation types or to simply not use std.

Signed-off-by: Ayush Singh <[email protected]>
  • Loading branch information
Ayush1325 committed Dec 6, 2022
commit d48d07d3e158113c97226974744bdd100e4bbf56
12 changes: 12 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,17 @@ dependencies = [
"rustc-std-workspace-core",
]

[[package]]
name = "r-efi-alloc"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31d6f09fe2b6ad044bc3d2c34ce4979796581afd2f1ebc185837e02421e02fd7"
dependencies = [
"compiler_builtins",
"r-efi",
"rustc-std-workspace-core",
]

[[package]]
name = "rand"
version = "0.7.3"
Expand Down Expand Up @@ -4909,6 +4920,7 @@ dependencies = [
"panic_unwind",
"profiler_builtins",
"r-efi",
"r-efi-alloc",
"rand 0.7.3",
"rustc-demangle",
"std_detect",
Expand Down
1 change: 1 addition & 0 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features =

[target.'cfg(target_os = "uefi")'.dependencies]
r-efi = { version = "4.1.0", features = ['rustc-dep-of-std', 'efiapi']}
r-efi-alloc = { version = "1.0.0", features = ['rustc-dep-of-std']}

[features]
backtrace = [
Expand Down
95 changes: 15 additions & 80 deletions library/std/src/sys/uefi/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Global Allocator for UEFI.
//! Uses `EFI_BOOT_SERVICES.AllocatePool()` and `EFI_BOOT_SERVICES.FreePool()`.
//! Takes a lot of inspiration from Windows allocator for Alignment > 8.
//! Uses [r-efi-alloc](https://crates.io/crates/r-efi-alloc)

use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;
use crate::sys::uefi::common;
use crate::alloc::{handle_alloc_error, GlobalAlloc, Layout, System};

pub(crate) const POOL_ALIGNMENT: usize = 8;

Expand All @@ -13,87 +10,25 @@ 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 {
let align = layout.align();
let size = layout.size();

// Return NULL pointer if boot_services pointer cannot be obtained. The only time this
// should happen is if SystemTable has not been initialized
let boot_services = match common::try_boot_services() {
Some(x) => x,
None => return ptr::null_mut(),
let system_table = match crate::os::uefi::env::try_system_table() {
None => return crate::ptr::null_mut(),
Some(x) => x.as_ptr() as *mut _,
};

let allocate_pool_ptr = unsafe { (*boot_services.as_ptr()).allocate_pool };

let mut ptr: *mut crate::ffi::c_void = ptr::null_mut();
let aligned_size = align_size(size, align);

let r = (allocate_pool_ptr)(MEMORY_TYPE, aligned_size, &mut ptr);

if r.is_error() || ptr.is_null() {
return ptr::null_mut();
if layout.size() > 0 {
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) }
} else {
layout.dangling().as_ptr()
}

unsafe { align_ptr(ptr.cast(), align) }
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let boot_services = match common::try_boot_services() {
Some(x) => x,
None => return,
let system_table = match crate::os::uefi::env::try_system_table() {
None => handle_alloc_error(layout),
Some(x) => x.as_ptr() as *mut _,
};

let free_pool_ptr = unsafe { (*boot_services.as_ptr()).free_pool };

let ptr = unsafe { unalign_ptr(ptr, layout.align()) };
let r = (free_pool_ptr)(ptr.cast());

assert!(!r.is_error());
}
}

#[inline]
fn align_size(size: usize, align: usize) -> usize {
if align > POOL_ALIGNMENT {
// Allocate extra padding in order to be able to satisfy the alignment.
size + align
} else {
size
}
}

#[repr(C)]
struct Header(*mut u8);

#[inline]
unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
if align > POOL_ALIGNMENT {
let offset = ptr.align_offset(align);

// SAFETY: `MIN_ALIGN` <= `offset` <= `layout.align()` and the size of the allocated
// block is `layout.align() + layout.size()`. `aligned` will thus be a correctly aligned
// pointer inside the allocated block with at least `layout.size()` bytes after it and at
// least `MIN_ALIGN` bytes of padding before it.
let aligned = unsafe { ptr.add(offset) };

// SAFETY: Because the size and alignment of a header is <= `MIN_ALIGN` and `aligned`
// is aligned to at least `MIN_ALIGN` and has at least `MIN_ALIGN` bytes of padding before
// it, it is safe to write a header directly before it.
unsafe { crate::ptr::write((aligned as *mut Header).offset(-1), Header(ptr)) };

aligned
} else {
ptr
}
}

#[inline]
unsafe fn unalign_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
if align > POOL_ALIGNMENT {
// SAFETY: Because of the contract of `System`, `ptr` is guaranteed to be non-null
// and have a header readable directly before it.
unsafe { crate::ptr::read((ptr as *mut Header).offset(-1)).0 }
} else {
ptr
if layout.size() > 0 {
unsafe { r_efi_alloc::raw::dealloc(system_table, ptr, layout) }
}
}
}
1 change: 1 addition & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"zerovec",
"zerovec-derive",
"r-efi",
"r-efi-alloc",
];

const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
Expand Down