mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-28 11:25:22 +00:00
arch: add armv7 support
This commit is contained in:
parent
03e6d1f4cf
commit
07239e2c0b
5 changed files with 318 additions and 32 deletions
140
crates/asm/src/arm.rs
Normal file
140
crates/asm/src/arm.rs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
//! Syscall implementations for `arm`.
|
||||
|
||||
use super::{StatfsBuf, SysInfo, UtsNameBuf};
|
||||
|
||||
pub(super) unsafe fn sys_open(path: *const u8, flags: i32) -> i32 {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 5i32, // SYS_open
|
||||
in("r0") path,
|
||||
in("r1") flags,
|
||||
in("r2") 0i32, // mode
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_read(fd: i32, buf: *mut u8, count: usize) -> isize {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 3i32, // SYS_read
|
||||
in("r0") fd,
|
||||
in("r1") buf,
|
||||
in("r2") count,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret as isize
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_write(fd: i32, buf: *const u8, count: usize) -> isize {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 4i32, // SYS_write
|
||||
in("r0") fd,
|
||||
in("r1") buf,
|
||||
in("r2") count,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret as isize
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_close(fd: i32) -> i32 {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 6i32, // SYS_close
|
||||
in("r0") fd,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_uname(buf: *mut UtsNameBuf) -> i32 {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 122i32, // SYS_newuname
|
||||
in("r0") buf,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_statfs(path: *const u8, buf: *mut StatfsBuf) -> i32 {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 266i32, // SYS_statfs64
|
||||
in("r0") path,
|
||||
in("r1") core::mem::size_of::<StatfsBuf>(),
|
||||
in("r2") buf,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_sysinfo(info: *mut SysInfo) -> i64 {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 116_i32, // __NR_sysinfo
|
||||
in("r0") info,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
i64::from(ret)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_sched_getaffinity(
|
||||
pid: i32,
|
||||
mask_size: usize,
|
||||
mask: *mut u8,
|
||||
) -> i32 {
|
||||
unsafe {
|
||||
let ret: i32;
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 242i32, // __NR_sched_getaffinity
|
||||
in("r0") pid,
|
||||
in("r1") mask_size,
|
||||
in("r2") mask,
|
||||
lateout("r0") ret,
|
||||
options(nostack)
|
||||
);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sys_exit(code: i32) -> ! {
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"svc #0",
|
||||
in("r7") 1i32, // SYS_exit
|
||||
in("r0") code,
|
||||
options(noreturn, nostack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,8 +5,8 @@
|
|||
//! What do you mean I wasted two whole hours to make the program only 100µs
|
||||
//! faster?
|
||||
//!
|
||||
//! Supports `x86_64`, `aarch64`, `riscv64`, `loongarch64`, `s390x`, and
|
||||
//! `powerpc64` architectures.
|
||||
//! Supports `x86_64`, `aarch64`, `riscv64`, `loongarch64`, `s390x`,
|
||||
//! `powerpc64`, and `arm` (armv7) architectures.
|
||||
|
||||
#![no_std]
|
||||
#![cfg_attr(target_arch = "powerpc64", feature(asm_experimental_arch))]
|
||||
|
|
@ -18,11 +18,12 @@
|
|||
target_arch = "riscv64",
|
||||
target_arch = "loongarch64",
|
||||
target_arch = "s390x",
|
||||
target_arch = "powerpc64"
|
||||
target_arch = "powerpc64",
|
||||
target_arch = "arm"
|
||||
)))]
|
||||
compile_error!(
|
||||
"Unsupported architecture: only x86_64, aarch64, riscv64, loongarch64, \
|
||||
s390x, and powerpc64 are supported"
|
||||
s390x, powerpc64, and arm are supported"
|
||||
);
|
||||
|
||||
// Per-arch syscall implementations live in their own module files.
|
||||
|
|
@ -44,6 +45,9 @@ mod arch;
|
|||
#[cfg(target_arch = "powerpc64")]
|
||||
#[path = "powerpc64.rs"]
|
||||
mod arch;
|
||||
#[cfg(target_arch = "arm")]
|
||||
#[path = "arm.rs"]
|
||||
mod arch;
|
||||
|
||||
/// Copies `n` bytes from `src` to `dest`.
|
||||
///
|
||||
|
|
@ -277,7 +281,7 @@ pub unsafe fn sys_uname(buf: *mut UtsNameBuf) -> i32 {
|
|||
/// offsets on both architectures. Only the fields needed for disk usage are
|
||||
/// declared; the remainder of the 120-byte struct is covered by `_pad`.
|
||||
#[repr(C)]
|
||||
#[cfg(not(target_arch = "s390x"))]
|
||||
#[cfg(not(any(target_arch = "s390x", target_arch = "arm")))]
|
||||
pub struct StatfsBuf {
|
||||
pub f_type: i64,
|
||||
pub f_bsize: i64,
|
||||
|
|
@ -315,6 +319,27 @@ pub struct StatfsBuf {
|
|||
pub _pad: [u32; 5],
|
||||
}
|
||||
|
||||
/// on armv7 `statfs64(2)` has 32-bit word fields; see
|
||||
/// https://github.com/torvalds/linux/blob/v6.19/include/uapi/asm-generic/statfs.h
|
||||
#[repr(C)]
|
||||
#[cfg(target_arch = "arm")]
|
||||
pub struct StatfsBuf {
|
||||
pub f_type: u32,
|
||||
pub f_bsize: u32,
|
||||
pub f_blocks: u64,
|
||||
pub f_bfree: u64,
|
||||
pub f_bavail: u64,
|
||||
pub f_files: u64,
|
||||
pub f_ffree: u64,
|
||||
pub f_fsid: [i32; 2],
|
||||
pub f_namelen: u32,
|
||||
pub f_frsize: u32,
|
||||
pub f_flags: u32,
|
||||
|
||||
#[allow(clippy::pub_underscore_fields, reason = "This is not a public API")]
|
||||
pub _pad: [u32; 4],
|
||||
}
|
||||
|
||||
/// Direct `statfs(2)` syscall
|
||||
///
|
||||
/// # Returns
|
||||
|
|
@ -384,6 +409,7 @@ pub fn read_file_fast(path: &str, buffer: &mut [u8]) -> Result<usize, i32> {
|
|||
/// The layout matches the kernel's `struct sysinfo` *exactly*:
|
||||
/// `mem_unit` ends at offset 108, then 4 bytes of implicit padding to 112.
|
||||
#[repr(C)]
|
||||
#[cfg(not(target_arch = "arm"))]
|
||||
pub struct SysInfo {
|
||||
pub uptime: i64,
|
||||
pub loads: [u64; 3],
|
||||
|
|
@ -404,6 +430,28 @@ pub struct SysInfo {
|
|||
// needed
|
||||
}
|
||||
|
||||
/// on armv7 `__kernel_long_t` is 4 bytes; see
|
||||
/// https://github.com/torvalds/linux/blob/v6.19/include/uapi/linux/sysinfo.h
|
||||
#[repr(C)]
|
||||
#[cfg(target_arch = "arm")]
|
||||
pub struct SysInfo {
|
||||
pub uptime: i32,
|
||||
pub loads: [u32; 3],
|
||||
pub totalram: u32,
|
||||
pub freeram: u32,
|
||||
pub sharedram: u32,
|
||||
pub bufferram: u32,
|
||||
pub totalswap: u32,
|
||||
pub freeswap: u32,
|
||||
pub procs: u16,
|
||||
_pad: u16,
|
||||
pub totalhigh: u32,
|
||||
pub freehigh: u32,
|
||||
pub mem_unit: u32,
|
||||
#[allow(clippy::pub_underscore_fields, reason = "This is not a public API")]
|
||||
pub _f: [u8; 8],
|
||||
}
|
||||
|
||||
/// Direct `sysinfo(2)` syscall
|
||||
///
|
||||
/// # Returns
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue