diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 0d32bc82df..0000000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,55 +0,0 @@ -name: Build - -on: - push: - branches: - - master - pull_request: - branches: - - master - schedule: - - cron: '0 0 * * 6' - -jobs: - build: - - runs-on: ${{ matrix.os }} - - strategy: - matrix: - os: [ubuntu-latest, windows-latest, macOS-latest] - rust: [nightly] - include: - - os: macOS-latest - rust: 'nightly' - components: 'rust-src' - targets: 'x86_64-apple-darwin' - - os: windows-latest - rust: 'nightly' - components: 'rust-src' - targets: 'x86_64-pc-windows-msvc' - - os: ubuntu-latest - rust: 'nightly' - components: 'rust-src' - targets: 'x86_64-unknown-linux-gnu' - - - steps: - - uses: hecrj/setup-rust-action@v1.3.1 - with: - rust-version: ${{ matrix.rust }} - components: ${{ matrix.components || '' }} - targets: ${{ matrix.targets || '' }} - - uses: actions/checkout@v1 - with: - submodules: true - - name: Check Cargo availability - run: cargo --version - - name: Building dev version - run: - cargo build -Z build-std=core,alloc --target x86_64-unknown-hermit-kernel - - name: Building release version - run: - cargo build -Z build-std=core,alloc --target x86_64-unknown-hermit-kernel --release - env: - RUSTFLAGS: -Clinker-plugin-lto diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5a79f94fb7..b46fc76546 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Test +name: Build and test on: push: @@ -55,8 +55,9 @@ jobs: - name: Check Cargo availability run: cargo --version - name: Cargo Test libhermit-rs (Unittests on Host) - run: cargo test --lib + run: cargo test --lib --target x86_64-unknown-linux-gnu working-directory: libhermit-rs + if: ${{ matrix.os == 'ubuntu-latest' }} - name: Install qemu/nasm (apt) run: sudo apt-get update --fix-missing && sudo apt-get install qemu-system-x86 nasm if: ${{ matrix.os == 'ubuntu-latest' }} diff --git a/src/arch/x86_64/kernel/processor.rs b/src/arch/x86_64/kernel/processor.rs index 68d424ff72..6492b463e9 100644 --- a/src/arch/x86_64/kernel/processor.rs +++ b/src/arch/x86_64/kernel/processor.rs @@ -877,15 +877,35 @@ fn print_cpu_information() { print_information(); }*/ -pub fn generate_random_number() -> Option { - if unsafe { SUPPORTS_RDRAND } { - let value: u32; - unsafe { - llvm_asm!("rdrand $0" : "=r"(value) ::: "volatile"); +pub fn generate_random_number32() -> Option { + unsafe { + if SUPPORTS_RDRAND { + let mut value: u32 = 0; + + while core::arch::x86_64::_rdrand32_step(&mut value) == 1 { + spin_loop_hint(); + } + + Some(value) + } else { + None + } + } +} + +pub fn generate_random_number64() -> Option { + unsafe { + if SUPPORTS_RDRAND { + let mut value: u64 = 0; + + while core::arch::x86_64::_rdrand64_step(&mut value) == 1 { + spin_loop_hint(); + } + + Some(value) + } else { + None } - Some(value) - } else { - None } } diff --git a/src/syscalls/random.rs b/src/syscalls/random.rs index 7f50438a46..ae55259e53 100644 --- a/src/syscalls/random.rs +++ b/src/syscalls/random.rs @@ -9,27 +9,65 @@ use crate::arch; use crate::synch::spinlock::Spinlock; static PARK_MILLER_LEHMER_SEED: Spinlock = Spinlock::new(0); +const RAND_MAX: u64 = 2_147_483_647; fn generate_park_miller_lehmer_random_number() -> u32 { let mut seed = PARK_MILLER_LEHMER_SEED.lock(); - let random = ((u64::from(*seed) * 48271) % 2_147_483_647) as u32; + let random = ((u64::from(*seed) * 48271) % RAND_MAX) as u32; *seed = random; random } +fn __sys_rand32() -> Option { + arch::processor::generate_random_number32() +} + +fn __sys_rand64() -> Option { + arch::processor::generate_random_number64() +} + fn __sys_rand() -> u32 { - if let Some(value) = arch::processor::generate_random_number() { - value - } else { - generate_park_miller_lehmer_random_number() - } + generate_park_miller_lehmer_random_number() +} + +/// Create a cryptographicly secure 32bit random number with the support of +/// the underlying hardware. If the required hardware isn't available, +/// the function returns `None`. +#[cfg(not(feature = "newlib"))] +#[no_mangle] +pub fn sys_secure_rand32() -> Option { + kernel_function!(__sys_rand32()) +} + +/// Create a cryptographicly secure 64bit random number with the support of +/// the underlying hardware. If the required hardware isn't available, +/// the function returns `None`. +#[cfg(not(feature = "newlib"))] +#[no_mangle] +pub fn sys_secure_rand64() -> Option { + kernel_function!(__sys_rand64()) } +/// The function computes a sequence of pseudo-random integers +/// in the range of 0 to RAND_MAX #[no_mangle] pub extern "C" fn sys_rand() -> u32 { kernel_function!(__sys_rand()) } +fn __sys_srand(seed: u32) { + *(PARK_MILLER_LEHMER_SEED.lock()) = seed; +} + +/// The function sets its argument as the seed for a new sequence +/// of pseudo-random numbers to be returned by rand() +#[no_mangle] +pub extern "C" fn sys_srand(seed: u32) { + kernel_function!(__sys_srand(seed)) +} + pub fn random_init() { - *PARK_MILLER_LEHMER_SEED.lock() = arch::processor::get_timestamp() as u32; + let seed: u32 = arch::processor::get_timestamp() as u32; + + *PARK_MILLER_LEHMER_SEED.lock() = seed; }