| 1 |
//! Implementation for Linux / Android using `asm!`-based syscalls. |
| 2 |
use super::sanitizer; |
| 3 |
pub use crate::util::{inner_u32, inner_u64}; |
| 4 |
use crate::{Error, MaybeUninit}; |
| 5 |
|
| 6 |
#[cfg(not(any(target_os = "android", target_os = "linux")))] |
| 7 |
compile_error!("`linux_raw` backend can be enabled only for Linux/Android targets!"); |
| 8 |
|
| 9 |
#[allow(non_upper_case_globals)] |
| 10 |
unsafe fn getrandom_syscall(buf: *mut u8, buflen: usize, flags: u32) -> isize { |
| 11 |
let r0; |
| 12 |
|
| 13 |
// Based on `rustix` and `linux-raw-sys` code. |
| 14 |
cfg_if! { |
| 15 |
if #[cfg(target_arch = "arm")] { |
| 16 |
// TODO(MSRV-1.78): Also check `target_abi = "eabi"`. |
| 17 |
// In thumb-mode, r7 is the frame pointer and is not permitted to be used in |
| 18 |
// an inline asm operand, so we have to use a different register and copy it |
| 19 |
// into r7 inside the inline asm. |
| 20 |
// Theoretically, we could detect thumb mode in the build script, but several |
| 21 |
// register moves are cheap enough compared to the syscall cost, so we do not |
| 22 |
// bother with it. |
| 23 |
core::arch::asm!( |
| 24 |
"mov {tmp}, r7", |
| 25 |
// TODO(MSRV-1.82): replace with `nr = const __NR_getrandom,` |
| 26 |
"mov r7, #384", |
| 27 |
"svc 0", |
| 28 |
"mov r7, {tmp}", |
| 29 |
tmp = out(reg) _, |
| 30 |
inlateout("r0") buf => r0, |
| 31 |
in("r1") buflen, |
| 32 |
in("r2") flags, |
| 33 |
options(nostack, preserves_flags) |
| 34 |
); |
| 35 |
} else if #[cfg(target_arch = "aarch64")] { |
| 36 |
// TODO(MSRV-1.78): Also check `any(target_abi = "", target_abi = "ilp32")` above. |
| 37 |
// According to the ILP32 patch for the kernel that hasn't yet |
| 38 |
// been merged into the mainline, "AARCH64/ILP32 ABI uses standard |
| 39 |
// syscall table [...] with the exceptions listed below," where |
| 40 |
// getrandom is not mentioned as an exception. |
| 41 |
const __NR_getrandom: u32 = 278; |
| 42 |
core::arch::asm!( |
| 43 |
"svc 0", |
| 44 |
in("x8") __NR_getrandom, |
| 45 |
inlateout("x0") buf => r0, |
| 46 |
in("x1") buflen, |
| 47 |
in("x2") flags, |
| 48 |
options(nostack, preserves_flags) |
| 49 |
); |
| 50 |
} else if #[cfg(target_arch = "loongarch64")] { |
| 51 |
// TODO(MSRV-1.78): Also check `any(target_abi = "", target_abi = "ilp32")` above. |
| 52 |
const __NR_getrandom: u32 = 278; |
| 53 |
core::arch::asm!( |
| 54 |
"syscall 0", |
| 55 |
in("$a7") __NR_getrandom, |
| 56 |
inlateout("$a0") buf => r0, |
| 57 |
in("$a1") buflen, |
| 58 |
in("$a2") flags, |
| 59 |
options(nostack, preserves_flags) |
| 60 |
); |
| 61 |
} else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] { |
| 62 |
const __NR_getrandom: u32 = 278; |
| 63 |
core::arch::asm!( |
| 64 |
"ecall", |
| 65 |
in("a7") __NR_getrandom, |
| 66 |
inlateout("a0") buf => r0, |
| 67 |
in("a1") buflen, |
| 68 |
in("a2") flags, |
| 69 |
options(nostack, preserves_flags) |
| 70 |
); |
| 71 |
} else if #[cfg(target_arch = "s390x")] { |
| 72 |
const __NR_getrandom: u32 = 349; |
| 73 |
core::arch::asm!( |
| 74 |
"svc 0", |
| 75 |
in("r1") __NR_getrandom, |
| 76 |
inlateout("r2") buf => r0, |
| 77 |
in("r3") buflen, |
| 78 |
in("r4") flags, |
| 79 |
options(nostack, preserves_flags) |
| 80 |
); |
| 81 |
} else if #[cfg(target_arch = "x86")] { |
| 82 |
const __NR_getrandom: u32 = 355; |
| 83 |
// `int 0x80` is famously slow, but implementing vDSO is too complex |
| 84 |
// and `sysenter`/`syscall` have their own portability issues, |
| 85 |
// so we use the simple "legacy" way of doing syscalls. |
| 86 |
core::arch::asm!( |
| 87 |
"int $$0x80", |
| 88 |
in("eax") __NR_getrandom, |
| 89 |
in("ebx") buf, |
| 90 |
in("ecx") buflen, |
| 91 |
in("edx") flags, |
| 92 |
lateout("eax") r0, |
| 93 |
options(nostack, preserves_flags) |
| 94 |
); |
| 95 |
} else if #[cfg(target_arch = "x86_64")] { |
| 96 |
// TODO(MSRV-1.78): Add `any(target_abi = "", target_abi = "x32")` above. |
| 97 |
const __X32_SYSCALL_BIT: u32 = 0x40000000; |
| 98 |
const OFFSET: u32 = if cfg!(target_pointer_width = "32") { __X32_SYSCALL_BIT } else { 0 }; |
| 99 |
const __NR_getrandom: u32 = OFFSET + 318; |
| 100 |
|
| 101 |
core::arch::asm!( |
| 102 |
"syscall", |
| 103 |
in("rax") __NR_getrandom, |
| 104 |
in("rdi") buf, |
| 105 |
in("rsi") buflen, |
| 106 |
in("rdx") flags, |
| 107 |
lateout("rax") r0, |
| 108 |
lateout("rcx") _, |
| 109 |
lateout("r11") _, |
| 110 |
options(nostack, preserves_flags) |
| 111 |
); |
| 112 |
} else { |
| 113 |
compile_error!("`linux_raw` backend does not support this target arch"); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
r0 |
| 118 |
} |
| 119 |
|
| 120 |
#[inline] |
| 121 |
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 122 |
// Value of this error code is stable across all target arches. |
| 123 |
const EINTR: isize = -4; |
| 124 |
|
| 125 |
loop { |
| 126 |
let ret = unsafe { getrandom_syscall(dest.as_mut_ptr().cast(), dest.len(), 0) }; |
| 127 |
unsafe { sanitizer::unpoison_linux_getrandom_result(dest, ret) }; |
| 128 |
match usize::try_from(ret) { |
| 129 |
Ok(0) => return Err(Error::UNEXPECTED), |
| 130 |
Ok(len) => { |
| 131 |
dest = dest.get_mut(len..).ok_or(Error::UNEXPECTED)?; |
| 132 |
if dest.is_empty() { |
| 133 |
return Ok(()); |
| 134 |
} |
| 135 |
} |
| 136 |
Err(_) if ret == EINTR => continue, |
| 137 |
Err(_) => { |
| 138 |
let code = i32::try_from(ret).map_err(|_| Error::UNEXPECTED)?; |
| 139 |
return Err(Error::from_neg_error_code(code)); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |