diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fff5aea..6d24a66 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -42,6 +42,11 @@ jobs: # sparc64 uses experimental asm features - target: sparc64-unknown-linux-gnu toolchain: nightly + # mips64 is tier-3 and uses experimental asm features + - target: mips64-unknown-linux-gnuabi64 + toolchain: nightly + components: rust-src + build_std: true steps: - name: "Checkout" diff --git a/crates/asm/src/lib.rs b/crates/asm/src/lib.rs index c877c1a..26bae90 100644 --- a/crates/asm/src/lib.rs +++ b/crates/asm/src/lib.rs @@ -6,11 +6,16 @@ //! faster? //! //! Supports `x86_64`, `aarch64`, `riscv64`, `loongarch64`, `s390x`, -//! `powerpc64`, `arm` (armv7), `riscv32`, and `sparc64` architectures. +//! `powerpc64`, `arm` (armv7), `riscv32`, `sparc64`, and `mips64` +//! architectures. #![no_std] #![cfg_attr( - any(target_arch = "powerpc64", target_arch = "sparc64"), + any( + target_arch = "powerpc64", + target_arch = "sparc64", + target_arch = "mips64" + ), feature(asm_experimental_arch) )] @@ -24,11 +29,12 @@ target_arch = "powerpc64", target_arch = "arm", target_arch = "riscv32", - target_arch = "sparc64" + target_arch = "sparc64", + target_arch = "mips64" )))] compile_error!( "Unsupported architecture: only x86_64, aarch64, riscv64, loongarch64, \ - s390x, powerpc64, arm, riscv32, and sparc64 are supported" + s390x, powerpc64, arm, riscv32, sparc64, and mips64 are supported" ); // Per-arch syscall implementations live in their own module files. @@ -59,6 +65,9 @@ mod arch; #[cfg(target_arch = "sparc64")] #[path = "sparc64.rs"] mod arch; +#[cfg(target_arch = "mips64")] +#[path = "mips64.rs"] +mod arch; /// Copies `n` bytes from `src` to `dest`. /// @@ -295,7 +304,8 @@ pub unsafe fn sys_uname(buf: *mut UtsNameBuf) -> i32 { #[cfg(not(any( target_arch = "s390x", target_arch = "arm", - target_arch = "riscv32" + target_arch = "riscv32", + target_arch = "mips64" )))] pub struct StatfsBuf { pub f_type: i64, @@ -355,6 +365,27 @@ pub struct StatfsBuf { pub _pad: [u32; 4], } +/// mips reorders fields; see +/// https://github.com/torvalds/linux/blob/v6.19/arch/mips/include/uapi/asm/statfs.h +#[repr(C)] +#[cfg(target_arch = "mips64")] +pub struct StatfsBuf { + pub f_type: i64, + pub f_bsize: i64, + pub f_frsize: i64, + pub f_blocks: u64, + pub f_bfree: u64, + pub f_files: u64, + pub f_ffree: u64, + pub f_bavail: u64, + pub f_fsid: [i32; 2], + pub f_namelen: i64, + pub f_flags: i64, + + #[allow(clippy::pub_underscore_fields, reason = "This is not a public API")] + pub _pad: [i64; 5], +} + /// Direct `statfs(2)` syscall /// /// # Returns diff --git a/crates/asm/src/mips64.rs b/crates/asm/src/mips64.rs new file mode 100644 index 0000000..9127a1e --- /dev/null +++ b/crates/asm/src/mips64.rs @@ -0,0 +1,206 @@ +//! Syscall implementations for `mips64`. + +use super::{StatfsBuf, SysInfo, UtsNameBuf}; + +pub(super) unsafe fn sys_open(path: *const u8, flags: i32) -> i32 { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000i64 + 2 => ret, // SYS_open + in("$4") path, + in("$5") flags, + in("$6") 0i32, // mode + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + #[allow(clippy::cast_possible_truncation)] + { + ret as i32 + } + } +} + +pub(super) unsafe fn sys_read(fd: i32, buf: *mut u8, count: usize) -> isize { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000i64 + 0 => ret, // SYS_read + in("$4") fd, + in("$5") buf, + in("$6") count, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + ret as isize + } +} + +pub(super) unsafe fn sys_write(fd: i32, buf: *const u8, count: usize) -> isize { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000i64 + 1 => ret, // SYS_write + in("$4") fd, + in("$5") buf, + in("$6") count, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + ret as isize + } +} + +pub(super) unsafe fn sys_close(fd: i32) -> i32 { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000i64 + 3 => ret, // SYS_close + in("$4") fd, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + #[allow(clippy::cast_possible_truncation)] + { + ret as i32 + } + } +} + +pub(super) unsafe fn sys_uname(buf: *mut UtsNameBuf) -> i32 { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000i64 + 61 => ret, // SYS_newuname + in("$4") buf, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + #[allow(clippy::cast_possible_truncation)] + { + ret as i32 + } + } +} + +pub(super) unsafe fn sys_statfs(path: *const u8, buf: *mut StatfsBuf) -> i32 { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000i64 + 134 => ret, // SYS_statfs + in("$4") path, + in("$5") buf, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + #[allow(clippy::cast_possible_truncation)] + { + ret as i32 + } + } +} + +pub(super) unsafe fn sys_sysinfo(info: *mut SysInfo) -> i64 { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + "beqz $7, 1f", + "nop", + "dsubu $2, $0, $2", + "1:", + inlateout("$2") 5000_i64 + 97 => ret, // SYS_sysinfo + in("$4") info, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + ret + } +} + +pub(super) unsafe fn sys_sched_getaffinity( + pid: i32, + mask_size: usize, + mask: *mut u8, +) -> i32 { + unsafe { + let ret: i64; + core::arch::asm!( + "syscall", + inlateout("$2") 5000i64 + 196 => ret, // __NR_sched_getaffinity + in("$4") pid, + in("$5") mask_size, + in("$6") mask, + lateout("$7") _, + lateout("$8") _, lateout("$9") _, lateout("$10") _, lateout("$11") _, + lateout("$12") _, lateout("$13") _, lateout("$14") _, lateout("$15") _, + lateout("$24") _, lateout("$25") _, + options(nostack) + ); + #[allow(clippy::cast_possible_truncation)] + { + ret as i32 + } + } +} + +pub(super) unsafe fn sys_exit(code: i32) -> ! { + unsafe { + core::arch::asm!( + "syscall", + in("$2") 5000i64 + 58, // SYS_exit + in("$4") code, + options(noreturn, nostack) + ); + } +} diff --git a/crates/lib/src/cpu.rs b/crates/lib/src/cpu.rs index 3bb53a0..dca327b 100644 --- a/crates/lib/src/cpu.rs +++ b/crates/lib/src/cpu.rs @@ -190,6 +190,8 @@ fn get_cpu_freq_mhz() -> Option { b"cpu MHz static", b"CPU MHz", b"clock", + // BogoMIPS on MIPS is calibrated to the clock frequency (unlike x86). + b"BogoMIPS", ] { if let Some(val) = extract_field(data, key) { // Parse integer part of the MHz value (e.g. "5200.00" -> 5200) @@ -270,6 +272,7 @@ fn extract_name(data: &[u8]) -> Option { b"model name" as &[u8], b"Model Name", b"uarch", + b"cpu model", b"isa", b"cpu", b"machine", diff --git a/microfetch/src/main.rs b/microfetch/src/main.rs index 42bad34..4745d63 100644 --- a/microfetch/src/main.rs +++ b/microfetch/src/main.rs @@ -1,7 +1,11 @@ #![no_std] #![no_main] #![cfg_attr( - any(target_arch = "powerpc64", target_arch = "sparc64"), + any( + target_arch = "powerpc64", + target_arch = "sparc64", + target_arch = "mips64" + ), feature(asm_experimental_arch) )] @@ -193,6 +197,22 @@ unsafe extern "C" fn _start() { ); } +#[cfg(target_arch = "mips64")] +#[unsafe(no_mangle)] +#[unsafe(naked)] +unsafe extern "C" fn _start() { + naked_asm!( + "move $a0, $sp", // first arg = original sp (argc/argv) + "daddiu $sp, $sp, -16",// reserve + keep 16-byte alignment (N64 ABI) + "jal {entry_rust}", + "nop", // delay slot + "move $a0, $v0", // exit code = entry_rust return value + "li $v0, 5058", // SYS_exit (5000 + 58) + "syscall", + entry_rust = sym entry_rust, + ); +} + // Global allocator #[global_allocator] static ALLOCATOR: BumpAllocator = BumpAllocator::new();