arceos_posix_api/imp/
sys.rs

1use core::ffi::{c_int, c_long};
2
3use crate::ctypes;
4
5const PAGE_SIZE_4K: usize = 4096;
6
7/// Return system configuration infomation
8///
9/// Notice: currently only support what unikraft covers
10pub fn sys_sysconf(name: c_int) -> c_long {
11    debug!("sys_sysconf <= {}", name);
12
13    syscall_body!(sys_sysconf, {
14        match name as u32 {
15            // Page size
16            ctypes::_SC_PAGE_SIZE => Ok(PAGE_SIZE_4K),
17            // Number of processors in use
18            ctypes::_SC_NPROCESSORS_ONLN => Ok(axconfig::plat::CPU_NUM),
19            // Total physical pages
20            ctypes::_SC_PHYS_PAGES => Ok(axhal::mem::total_ram_size() / PAGE_SIZE_4K),
21            // Avaliable physical pages
22            ctypes::_SC_AVPHYS_PAGES => {
23                #[cfg(feature = "alloc")]
24                {
25                    Ok(axalloc::global_allocator().available_pages())
26                }
27                #[cfg(not(feature = "alloc"))]
28                {
29                    let total_pages = axhal::mem::total_ram_size() / PAGE_SIZE_4K;
30                    let reserved_pages = axhal::mem::reserved_phys_ram_ranges()
31                        .iter()
32                        .map(|range| range.1 / PAGE_SIZE_4K)
33                        .sum::<usize>();
34                    Ok(total_pages - reserved_pages)
35                }
36            }
37            // Maximum number of files per process
38            #[cfg(feature = "fd")]
39            ctypes::_SC_OPEN_MAX => Ok(super::fd_ops::AX_FILE_LIMIT),
40            _ => Ok(0),
41        }
42    })
43}