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
Fix/Ignore tests
The following kinds of tests have been ignored:
1. need threads
2. need libc
3. are unix specific
4. need rust_test_helpers
5. need stack-unwinding
6. need backtrace-rs

The following kinds of tests have been fixed:
1. Use static linking for tests on UEFI
2. Implement thread::sleep using Stall
3. Implement Instant
4. Update compiler_builtins
5. Some conditional compilation tests

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
  • Loading branch information
Ayush1325 committed Dec 1, 2022
commit ce43ad679d2a1f1966e166a69368dc8005e351cc
4 changes: 4 additions & 0 deletions library/std/src/os/uefi/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl<T> VariableSizeType<T> {
pub(crate) fn as_ref(&self) -> &T {
unsafe { self.inner.as_ref() }
}

pub(crate) fn size(&self) -> usize {
self.layout.size()
}
}

impl<T> Drop for VariableSizeType<T> {
Expand Down
40 changes: 35 additions & 5 deletions library/std/src/sys/uefi/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::ffi::{OsStr, OsString};
use crate::fmt;
use crate::hash::Hash;
use crate::io::{self, IoSlice, IoSliceMut, ReadBuf, SeekFrom};
// use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
use crate::path::{Path, PathBuf};
use crate::sys::time::SystemTime;
use crate::sys::unsupported;
Expand Down Expand Up @@ -39,6 +38,7 @@ pub struct OpenOptions {
open_mode: u64,
attr: u64,
append: bool,
truncate: bool,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -172,7 +172,7 @@ impl DirEntry {
impl OpenOptions {
pub fn new() -> OpenOptions {
// These options open file in readonly mode
OpenOptions { open_mode: file::MODE_READ, attr: 0, append: false }
OpenOptions { open_mode: file::MODE_READ, attr: 0, append: false, truncate: false }
}

pub fn read(&mut self, read: bool) {
Expand All @@ -196,8 +196,9 @@ impl OpenOptions {
self.append = append;
}

// FIXME: Should be possible to implement
pub fn truncate(&mut self, _truncate: bool) {}
pub fn truncate(&mut self, truncate: bool) {
self.truncate = truncate;
}

pub fn create(&mut self, create: bool) {
if create {
Expand All @@ -214,6 +215,9 @@ impl OpenOptions {
impl File {
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
let file_opened = uefi_fs::FileProtocol::from_path(path, opts.open_mode, opts.attr)?;
if opts.truncate {
file_opened.set_file_info(0)?;
}
let file = File { ptr: file_opened };
if opts.append {
file.seek(SeekFrom::End(0))?;
Expand Down Expand Up @@ -376,7 +380,7 @@ pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
}

pub fn stat(p: &Path) -> io::Result<FileAttr> {
let opts = OpenOptions { open_mode: file::MODE_READ, attr: 0, append: false };
let opts = OpenOptions { open_mode: file::MODE_READ, attr: 0, append: false, truncate: false };
File::open(p, &opts)?.file_attr()
}

Expand Down Expand Up @@ -657,6 +661,22 @@ mod uefi_fs {
}
}

pub fn set_file_info(&self, file_size: u64) -> io::Result<()> {
let old_info = self.get_file_info()?;
// Update fields with new values
unsafe {
(*old_info.as_ptr()).file_size = file_size;
}
unsafe {
Self::set_info_raw(
self.inner.as_ptr(),
file::INFO_ID,
old_info.size(),
old_info.as_ptr().cast(),
)
}
}

pub fn delete(self) -> io::Result<()> {
let file = crate::mem::ManuallyDrop::new(self);
unsafe { Self::delete_raw(file.inner.as_ptr()) }
Expand Down Expand Up @@ -721,6 +741,16 @@ mod uefi_fs {
if r.is_error() { Err(status_to_io_error(r)) } else { Ok(()) }
}

unsafe fn set_info_raw(
protocol: *mut file::Protocol,
mut info_guid: r_efi::efi::Guid,
buf_size: usize,
buf: *mut crate::ffi::c_void,
) -> io::Result<()> {
let r = unsafe { ((*protocol).set_info)(protocol, &mut info_guid, buf_size, buf) };
if r.is_error() { Err(status_to_io_error(r)) } else { Ok(()) }
}

unsafe fn delete_raw(protocol: *mut file::Protocol) -> io::Result<()> {
let r = unsafe { ((*protocol).delete)(protocol) };
if r.is_error() { Err(status_to_io_error(r)) } else { Ok(()) }
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub mod path;
pub mod pipe;
pub mod process;
pub mod stdio;
#[path = "../unsupported/thread.rs"]
pub mod thread;
#[path = "../unsupported/thread_local_key.rs"]
pub mod thread_local_key;
Expand Down
52 changes: 52 additions & 0 deletions library/std/src/sys/uefi/thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::unsupported;
use crate::ffi::CStr;
use crate::io;
use crate::num::NonZeroUsize;
use crate::os::uefi;
use crate::time::Duration;

pub struct Thread(());

pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
unsupported()
}

pub fn yield_now() {
// do nothing
}

pub fn set_name(_name: &CStr) {
// nope
}

pub fn sleep(dur: Duration) {
if let Some(boot_services) = uefi::env::get_boot_services() {
let _ = unsafe { ((*boot_services.as_ptr()).stall)(dur.as_micros() as usize) };
} else {
panic!("Boot services needed for sleep")
}
}

pub fn join(self) {
self.0
}
}

pub fn available_parallelism() -> io::Result<NonZeroUsize> {
// UEFI is single threaded
Ok(NonZeroUsize::new(1).unwrap())
}

pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
None
}
pub unsafe fn init() -> Option<Guard> {
None
}
}
58 changes: 37 additions & 21 deletions library/std/src/sys/uefi/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ pub struct SystemTime(Duration);

pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::ZERO);

// FIXME: Implement using `EFI_TIMESTAMP_PROTOCOL.GetTimestamp()`
// FIXME: Maybe optionally use `EFI_TIMESTAMP_PROTOCOL.GetTimestamp()`?
impl Instant {
pub fn now() -> Instant {
panic!("time not implemented on this platform")
if let Some(runtime_services) = uefi::env::get_runtime_services() {
let mut t = r_efi::efi::Time::default();
let r =
unsafe { ((*runtime_services.as_ptr()).get_time)(&mut t, crate::ptr::null_mut()) };

if r.is_error() {
panic!("time not implemented on this platform")
} else {
Instant(uefi_time_to_duration(t))
}
} else {
panic!("Runtime Services are needed for Time to work")
}
}

pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
Expand Down Expand Up @@ -42,7 +54,7 @@ impl SystemTime {
SystemTime::from(t)
}
} else {
panic!("time not implemented on this platform")
panic!("Runtime Services are needed for Time to work")
}
}

Expand All @@ -60,24 +72,28 @@ impl SystemTime {
}

impl From<r_efi::system::Time> for SystemTime {
// FIXME: Don't know how to use Daylight Saving thing
fn from(t: r_efi::system::Time) -> Self {
const SEC_IN_MIN: u64 = 60;
const SEC_IN_HOUR: u64 = SEC_IN_MIN * 60;
const SEC_IN_DAY: u64 = SEC_IN_HOUR * 24;
const SEC_IN_YEAR: u64 = SEC_IN_DAY * 365;
const MONTH_DAYS: [u64; 12] = [0, 31, 59, 90, 120, 151, 181, 211, 242, 272, 303, 333];

let localtime_epoch: u64 = u64::from(t.year - 1970) * SEC_IN_YEAR
+ u64::from((t.year - 1968) / 4) * SEC_IN_DAY
+ MONTH_DAYS[usize::from(t.month - 1)] * SEC_IN_DAY
+ u64::from(t.day - 1) * SEC_IN_DAY
+ u64::from(t.hour) * SEC_IN_HOUR
+ u64::from(t.minute) * SEC_IN_MIN
+ u64::from(t.second);
let timezone_epoch: i64 = i64::from(t.timezone) * (SEC_IN_MIN as i64);
let utc_epoch: u64 = ((localtime_epoch as i64) + timezone_epoch) as u64;

SystemTime(Duration::new(utc_epoch, t.nanosecond))
SystemTime(uefi_time_to_duration(t))
}
}

// FIXME: Don't know how to use Daylight Saving thing
fn uefi_time_to_duration(t: r_efi::system::Time) -> Duration {
const SEC_IN_MIN: u64 = 60;
const SEC_IN_HOUR: u64 = SEC_IN_MIN * 60;
const SEC_IN_DAY: u64 = SEC_IN_HOUR * 24;
const SEC_IN_YEAR: u64 = SEC_IN_DAY * 365;
const MONTH_DAYS: [u64; 12] = [0, 31, 59, 90, 120, 151, 181, 211, 242, 272, 303, 333];

let localtime_epoch: u64 = u64::from(t.year - 1970) * SEC_IN_YEAR
+ u64::from((t.year - 1968) / 4) * SEC_IN_DAY
+ MONTH_DAYS[usize::from(t.month - 1)] * SEC_IN_DAY
+ u64::from(t.day - 1) * SEC_IN_DAY
+ u64::from(t.hour) * SEC_IN_HOUR
+ u64::from(t.minute) * SEC_IN_MIN
+ u64::from(t.second);
let timezone_epoch: i64 = i64::from(t.timezone) * (SEC_IN_MIN as i64);
let utc_epoch: u64 = ((localtime_epoch as i64) + timezone_epoch) as u64;

Duration::new(utc_epoch, t.nanosecond)
}
Loading