twizzler_abi/syscall/
info.rs

1use core::num::NonZeroUsize;
2
3use super::Syscall;
4use crate::arch::syscall::raw_syscall;
5#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq)]
6#[repr(C)]
7/// Information about the system.
8pub struct SysInfo {
9    /// The version of this data structure, to allow expansion.
10    pub version: u32,
11    /// Flags. Currently unused.
12    pub flags: u32,
13    /// The number of CPUs on this system. Hyperthreads are counted as individual CPUs.
14    pub cpu_count: usize,
15    /// The size of a virtual address page on this system.
16    pub page_size: usize,
17}
18
19impl SysInfo {
20    /// Get the number of CPUs on the system.
21    pub fn cpu_count(&self) -> NonZeroUsize {
22        NonZeroUsize::new(self.cpu_count).expect("CPU count from sysinfo should always be non-zero")
23    }
24
25    /// Get the page size of the system.
26    pub fn page_size(&self) -> usize {
27        self.page_size
28    }
29}
30
31/// Get a SysInfo struct from the kernel.
32pub fn sys_info() -> SysInfo {
33    let mut sysinfo = core::mem::MaybeUninit::<SysInfo>::zeroed();
34    unsafe {
35        raw_syscall(
36            Syscall::SysInfo,
37            &[&mut sysinfo as *mut core::mem::MaybeUninit<SysInfo> as u64],
38        );
39        sysinfo.assume_init()
40    }
41}