arch: add i686 support

This commit is contained in:
Amaan Qureshi 2026-04-17 12:20:09 -04:00
commit a6fab02586
No known key found for this signature in database
4 changed files with 184 additions and 7 deletions

View file

@ -47,6 +47,8 @@ jobs:
toolchain: nightly toolchain: nightly
components: rust-src components: rust-src
build_std: true build_std: true
- target: i686-unknown-linux-gnu
toolchain: stable
steps: steps:
- name: "Checkout" - name: "Checkout"

View file

@ -6,8 +6,8 @@
//! faster? //! faster?
//! //!
//! Supports `x86_64`, `aarch64`, `riscv64`, `loongarch64`, `s390x`, //! Supports `x86_64`, `aarch64`, `riscv64`, `loongarch64`, `s390x`,
//! `powerpc64`, `arm` (armv7), `riscv32`, `sparc64`, and `mips64` //! `powerpc64`, `arm` (armv7), `riscv32`, `sparc64`, `mips64`, and `x86`
//! architectures. //! (i686) architectures.
#![no_std] #![no_std]
#![cfg_attr( #![cfg_attr(
@ -30,11 +30,12 @@
target_arch = "arm", target_arch = "arm",
target_arch = "riscv32", target_arch = "riscv32",
target_arch = "sparc64", target_arch = "sparc64",
target_arch = "mips64" target_arch = "mips64",
target_arch = "x86"
)))] )))]
compile_error!( compile_error!(
"Unsupported architecture: only x86_64, aarch64, riscv64, loongarch64, \ "Unsupported architecture: only x86_64, aarch64, riscv64, loongarch64, \
s390x, powerpc64, arm, riscv32, sparc64, and mips64 are supported" s390x, powerpc64, arm, riscv32, sparc64, mips64, and x86 are supported"
); );
// Per-arch syscall implementations live in their own module files. // Per-arch syscall implementations live in their own module files.
@ -68,6 +69,9 @@ mod arch;
#[cfg(target_arch = "mips64")] #[cfg(target_arch = "mips64")]
#[path = "mips64.rs"] #[path = "mips64.rs"]
mod arch; mod arch;
#[cfg(target_arch = "x86")]
#[path = "x86.rs"]
mod arch;
/// Copies `n` bytes from `src` to `dest`. /// Copies `n` bytes from `src` to `dest`.
/// ///
@ -305,6 +309,7 @@ pub unsafe fn sys_uname(buf: *mut UtsNameBuf) -> i32 {
target_arch = "s390x", target_arch = "s390x",
target_arch = "arm", target_arch = "arm",
target_arch = "riscv32", target_arch = "riscv32",
target_arch = "x86",
target_arch = "mips64" target_arch = "mips64"
)))] )))]
pub struct StatfsBuf { pub struct StatfsBuf {
@ -347,7 +352,7 @@ pub struct StatfsBuf {
/// on armv7 `statfs64(2)` has 32-bit word fields; see /// on armv7 `statfs64(2)` has 32-bit word fields; see
/// https://github.com/torvalds/linux/blob/v6.19/include/uapi/asm-generic/statfs.h /// https://github.com/torvalds/linux/blob/v6.19/include/uapi/asm-generic/statfs.h
#[repr(C)] #[repr(C)]
#[cfg(any(target_arch = "arm", target_arch = "riscv32"))] #[cfg(any(target_arch = "arm", target_arch = "riscv32", target_arch = "x86"))]
pub struct StatfsBuf { pub struct StatfsBuf {
pub f_type: u32, pub f_type: u32,
pub f_bsize: u32, pub f_bsize: u32,
@ -455,7 +460,11 @@ pub fn read_file_fast(path: &str, buffer: &mut [u8]) -> Result<usize, i32> {
/// The layout matches the kernel's `struct sysinfo` *exactly*: /// The layout matches the kernel's `struct sysinfo` *exactly*:
/// `mem_unit` ends at offset 108, then 4 bytes of implicit padding to 112. /// `mem_unit` ends at offset 108, then 4 bytes of implicit padding to 112.
#[repr(C)] #[repr(C)]
#[cfg(not(any(target_arch = "arm", target_arch = "riscv32")))] #[cfg(not(any(
target_arch = "arm",
target_arch = "riscv32",
target_arch = "x86"
)))]
pub struct SysInfo { pub struct SysInfo {
pub uptime: i64, pub uptime: i64,
pub loads: [u64; 3], pub loads: [u64; 3],
@ -479,7 +488,7 @@ pub struct SysInfo {
/// on armv7 `__kernel_long_t` is 4 bytes; see /// on armv7 `__kernel_long_t` is 4 bytes; see
/// https://github.com/torvalds/linux/blob/v6.19/include/uapi/linux/sysinfo.h /// https://github.com/torvalds/linux/blob/v6.19/include/uapi/linux/sysinfo.h
#[repr(C)] #[repr(C)]
#[cfg(any(target_arch = "arm", target_arch = "riscv32"))] #[cfg(any(target_arch = "arm", target_arch = "riscv32", target_arch = "x86"))]
pub struct SysInfo { pub struct SysInfo {
pub uptime: i32, pub uptime: i32,
pub loads: [u32; 3], pub loads: [u32; 3],

149
crates/asm/src/x86.rs Normal file
View file

@ -0,0 +1,149 @@
//! Syscall implementations for `x86`.
use super::{StatfsBuf, SysInfo, UtsNameBuf};
pub(super) unsafe fn sys_open(path: *const u8, flags: i32) -> i32 {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) path,
inlateout("eax") 5i32 => ret, // SYS_open
in("ecx") flags,
in("edx") 0i32, // mode
);
ret
}
}
pub(super) unsafe fn sys_read(fd: i32, buf: *mut u8, count: usize) -> isize {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) fd,
inlateout("eax") 3i32 => ret, // SYS_read
in("ecx") buf,
in("edx") count,
);
ret as isize
}
}
pub(super) unsafe fn sys_write(fd: i32, buf: *const u8, count: usize) -> isize {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) fd,
inlateout("eax") 4i32 => ret, // SYS_write
in("ecx") buf,
in("edx") count,
);
ret as isize
}
}
pub(super) unsafe fn sys_close(fd: i32) -> i32 {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) fd,
inlateout("eax") 6i32 => ret, // SYS_close
);
ret
}
}
pub(super) unsafe fn sys_uname(buf: *mut UtsNameBuf) -> i32 {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) buf,
inlateout("eax") 122i32 => ret, // SYS_newuname
);
ret
}
}
pub(super) unsafe fn sys_statfs(path: *const u8, buf: *mut StatfsBuf) -> i32 {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) path,
inlateout("eax") 268i32 => ret, // SYS_statfs64
in("ecx") core::mem::size_of::<StatfsBuf>(),
in("edx") buf,
);
ret
}
}
pub(super) unsafe fn sys_sysinfo(info: *mut SysInfo) -> i64 {
unsafe {
let ret: i32;
core::arch::asm!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) info,
inlateout("eax") 116_i32 => ret, // __NR_sysinfo
);
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!(
"push ebx",
"mov ebx, {arg1:e}",
"int 0x80",
"pop ebx",
arg1 = in(reg) pid,
inlateout("eax") 242i32 => ret, // __NR_sched_getaffinity
in("ecx") mask_size,
in("edx") mask,
);
ret
}
}
pub(super) unsafe fn sys_exit(code: i32) -> ! {
unsafe {
core::arch::asm!(
"mov ebx, {code:e}",
"int 0x80",
code = in(reg) code,
in("eax") 1i32, // SYS_exit
options(noreturn)
);
}
}

View file

@ -33,6 +33,23 @@ unsafe extern "C" fn _start() {
); );
} }
#[cfg(target_arch = "x86")]
#[unsafe(no_mangle)]
#[unsafe(naked)]
unsafe extern "C" fn _start() {
naked_asm!(
"mov eax, esp", // save original sp (argc/argv)
"and esp, -16", // align stack to 16 bytes
"sub esp, 12", // leave room so that the one-arg push keeps alignment
"push eax", // arg: initial stack pointer
"call {entry_rust}",
"mov ebx, eax", // exit code -> first syscall arg
"mov eax, 1", // SYS_exit
"int 0x80",
entry_rust = sym entry_rust,
);
}
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
#[unsafe(naked)] #[unsafe(naked)]