-
Notifications
You must be signed in to change notification settings - Fork 708
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 UEFI target support #1406
base: main
Are you sure you want to change the base?
Add UEFI target support #1406
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ include = [ | |
"crypto/fipsmodule/modes/asm/ghash-x86.pl", | ||
"crypto/fipsmodule/modes/asm/ghash-x86_64.pl", | ||
"crypto/fipsmodule/modes/asm/ghashv8-armx.pl", | ||
"crypto/fipsmodule/rand/asm/rdrand-x86_64.pl", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can go away since we'd use |
||
"crypto/fipsmodule/sha/asm/sha256-armv4.pl", | ||
"crypto/fipsmodule/sha/asm/sha512-armv4.pl", | ||
"crypto/fipsmodule/sha/asm/sha512-armv8.pl", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ const RING_SRCS: &[(&[&str], &str)] = &[ | |
(&[X86_64], "crypto/fipsmodule/ec/asm/p256-x86_64-asm.pl"), | ||
(&[X86_64], "crypto/fipsmodule/modes/asm/aesni-gcm-x86_64.pl"), | ||
(&[X86_64], "crypto/fipsmodule/modes/asm/ghash-x86_64.pl"), | ||
(&[X86_64], "crypto/fipsmodule/rand/asm/rdrand-x86_64.pl"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
(&[X86_64], "crypto/poly1305/poly1305_vec.c"), | ||
(&[X86_64], SHA512_X86_64), | ||
(&[X86_64], "crypto/cipher_extra/asm/chacha20_poly1305_x86_64.pl"), | ||
|
@@ -222,7 +223,7 @@ const ASM_TARGETS: &[AsmTarget] = &[ | |
preassemble: true, | ||
}, | ||
AsmTarget { | ||
oss: &[WINDOWS], | ||
oss: &[WINDOWS, UEFI], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This deserves a comment about why UEFI uses Windows ASM stuff unlike everything else. |
||
arch: "x86_64", | ||
perlasm_format: "nasm", | ||
asm_extension: "asm", | ||
|
@@ -279,6 +280,8 @@ const MACOS_ABI: &[&str] = &["ios", "macos"]; | |
|
||
const WINDOWS: &str = "windows"; | ||
|
||
const UEFI: &str = "uefi"; | ||
|
||
/// Read an environment variable and tell Cargo that we depend on it. | ||
/// | ||
/// This needs to be used for any environment variable that isn't a standard | ||
|
@@ -381,8 +384,9 @@ fn pregenerate_asm_main() { | |
perlasm(&perlasm_src_dsts, asm_target); | ||
|
||
if asm_target.preassemble { | ||
// Preassembly is currently only done for Windows targets. | ||
assert_eq!(&asm_target.oss, &[WINDOWS]); | ||
// Preassembly is currently done for Windows and UEFI targets. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's just remove the comment since there's now a |
||
assert!(asm_target.oss.contains(&WINDOWS) || asm_target.oss.contains(&UEFI)); | ||
|
||
let os = WINDOWS; | ||
|
||
let srcs = asm_srcs(perlasm_src_dsts); | ||
|
@@ -460,7 +464,7 @@ fn build_c_code( | |
|
||
// For Windows we also pregenerate the object files for non-Git builds so | ||
// the user doesn't need to install the assembler. | ||
if use_pregenerated && target.os == WINDOWS { | ||
if use_pregenerated && supports_preassembly(&target.arch, &target.os) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to look at the |
||
asm_srcs = asm_srcs | ||
.iter() | ||
.map(|src| obj_path(&pregenerated, src.as_path())) | ||
|
@@ -554,7 +558,7 @@ fn compile(p: &Path, target: &Target, include_dir: &Path, out_dir: &Path) -> Str | |
p.to_str().expect("Invalid path").into() | ||
} else { | ||
let out_file = obj_path(out_dir, p); | ||
let cmd = if target.os != WINDOWS || ext != "asm" { | ||
let cmd = if !supports_preassembly(&target.arch, &target.os) || ext != "asm" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
cc(p, ext, target, include_dir, &out_file) | ||
} else { | ||
nasm(p, &target.arch, include_dir, &out_file) | ||
|
@@ -602,6 +606,7 @@ fn cc(file: &Path, ext: &str, target: &Target, include_dir: &Path, out_file: &Pa | |
&& target.os != "redox" | ||
&& target.os != "windows" | ||
&& target.arch != "wasm32" | ||
&& target.os != UEFI | ||
{ | ||
let _ = c.flag("-fstack-protector"); | ||
} | ||
|
@@ -627,17 +632,25 @@ fn cc(file: &Path, ext: &str, target: &Target, include_dir: &Path, out_file: &Pa | |
} | ||
} | ||
|
||
// UEFI is a baremental freestanding environment without stdlib. | ||
let freestanding = target.os == UEFI; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still true? I heard there is a libstd now. |
||
|
||
// Allow cross-compiling without a target sysroot for these targets. | ||
// | ||
// poly1305_vec.c requires <emmintrin.h> which requires <stdlib.h>. | ||
if (target.arch == "wasm32" && target.os == "unknown") | ||
|| (target.os == "linux" && target.is_musl && target.arch != "x86_64") | ||
|| freestanding | ||
{ | ||
if let Ok(compiler) = c.try_get_compiler() { | ||
// TODO: Expand this to non-clang compilers in 0.17.0 if practical. | ||
if compiler.is_like_clang() { | ||
let _ = c.flag("-nostdlibinc"); | ||
let _ = c.define("RING_CORE_NOSTDLIBINC", "1"); | ||
|
||
if freestanding { | ||
let _ = c.flag("-ffreestanding"); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -680,7 +693,10 @@ fn nasm(file: &Path, arch: &str, include_dir: &Path, out_file: &Path) -> Command | |
std::path::MAIN_SEPARATOR, | ||
))); | ||
|
||
let mut c = Command::new("./target/tools/windows/nasm/nasm"); | ||
let mut c = Command::new(&get_command( | ||
"NASM_EXECUTABLE", | ||
"./target/tools/windows/nasm/nasm", | ||
)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move this to a separate PR. |
||
let _ = c | ||
.arg("-o") | ||
.arg(out_file.to_str().expect("Invalid path")) | ||
|
@@ -917,6 +933,8 @@ fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String { | |
"CRYPTO_poly1305_init_neon", | ||
"CRYPTO_poly1305_update", | ||
"CRYPTO_poly1305_update_neon", | ||
"CRYPTO_rdrand", | ||
"CRYPTO_rdrand_multiple8_buf", | ||
"ChaCha20_ctr32", | ||
"LIMBS_add_mod", | ||
"LIMBS_are_even", | ||
|
@@ -1027,3 +1045,9 @@ fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String { | |
|
||
out | ||
} | ||
|
||
fn supports_preassembly(arch: &str, os: &str) -> bool { | ||
ASM_TARGETS.iter().any(|asm_target| { | ||
asm_target.preassemble && asm_target.arch == arch && asm_target.oss.contains(&os) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/env perl | ||
|
||
# Copyright (c) 2015, Google Inc. | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any | ||
# purpose with or without fee is hereby granted, provided that the above | ||
# copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION | ||
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ | ||
|
||
use strict; | ||
|
||
my $flavour = shift; | ||
my $output = shift; | ||
if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } | ||
|
||
my $win64 = 0; | ||
$win64 = 1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); | ||
|
||
$0 =~ m/(.*[\/\\])[^\/\\]+$/; | ||
my $dir = $1; | ||
my $xlate; | ||
( $xlate="${dir}../../../perlasm/x86_64-xlate.pl" and -f $xlate) or | ||
die "can't locate x86_64-xlate.pl"; | ||
|
||
open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""; | ||
*STDOUT=*OUT; | ||
|
||
my ($out, $len, $tmp1, $tmp2) = $win64 ? ("%rcx", "%rdx", "%r8", "%r9") | ||
: ("%rdi", "%rsi", "%rdx", "%rcx"); | ||
|
||
print<<___; | ||
.text | ||
|
||
# CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to | ||
# |out|. It returns one on success or zero on hardware failure. | ||
# int CRYPTO_rdrand(uint8_t out[8]); | ||
.globl CRYPTO_rdrand | ||
.type CRYPTO_rdrand,\@abi-omnipotent | ||
.align 16 | ||
CRYPTO_rdrand: | ||
.cfi_startproc | ||
xorq %rax, %rax | ||
rdrand $tmp1 | ||
# An add-with-carry of zero effectively sets %rax to the carry flag. | ||
adcq %rax, %rax | ||
movq $tmp1, 0($out) | ||
retq | ||
.cfi_endproc | ||
.size CRYPTO_rdrand,.-CRYPTO_rdrand | ||
|
||
# CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from | ||
# the hardware RNG. The |len| argument must be a multiple of eight. It returns | ||
# one on success and zero on hardware failure. | ||
# int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); | ||
.globl CRYPTO_rdrand_multiple8_buf | ||
.type CRYPTO_rdrand_multiple8_buf,\@abi-omnipotent | ||
.align 16 | ||
CRYPTO_rdrand_multiple8_buf: | ||
.cfi_startproc | ||
test $len, $len | ||
jz .Lout | ||
movq \$8, $tmp1 | ||
.Lloop: | ||
rdrand $tmp2 | ||
jnc .Lerr | ||
movq $tmp2, 0($out) | ||
addq $tmp1, $out | ||
subq $tmp1, $len | ||
jnz .Lloop | ||
.Lout: | ||
movq \$1, %rax | ||
retq | ||
.Lerr: | ||
xorq %rax, %rax | ||
retq | ||
.cfi_endproc | ||
.size CRYPTO_rdrand_multiple8_buf,.-CRYPTO_rdrand_multiple8_buf | ||
___ | ||
|
||
close STDOUT or die "error closing STDOUT: $!"; # flush |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still the case? Or can we simplify all this now?