Implement getcpu syscall and add corresponding test application

This commit is contained in:
wheatfox
2025-02-25 17:24:28 +08:00
committed by Tate, Hongliang Tian
parent 9b8c6b5aa9
commit 6d42a07e95
8 changed files with 63 additions and 1 deletions

View File

@ -26,6 +26,7 @@ use crate::syscall::{
flock::sys_flock,
fsync::{sys_fdatasync, sys_fsync},
futex::sys_futex,
getcpu::sys_getcpu,
getcwd::sys_getcwd,
getdents64::sys_getdents64,
getegid::sys_getegid,
@ -225,6 +226,7 @@ impl_syscall_nums_and_dispatch_fn! {
SYS_GETRUSAGE = 165 => sys_getrusage(args[..2]);
SYS_UMASK = 166 => sys_umask(args[..1]);
SYS_PRCTL = 167 => sys_prctl(args[..5]);
SYS_GETCPU = 168 => sys_getcpu(args[..3]);
SYS_GETTIMEOFDAY = 169 => sys_gettimeofday(args[..1]);
SYS_GETPID = 172 => sys_getpid(args[..0]);
SYS_GETPPID = 173 => sys_getppid(args[..0]);

View File

@ -29,6 +29,7 @@ use crate::syscall::{
fork::sys_fork,
fsync::{sys_fdatasync, sys_fsync},
futex::sys_futex,
getcpu::sys_getcpu,
getcwd::sys_getcwd,
getdents64::{sys_getdents, sys_getdents64},
getegid::sys_getegid,
@ -314,6 +315,7 @@ impl_syscall_nums_and_dispatch_fn! {
SYS_PREADV = 295 => sys_preadv(args[..4]);
SYS_PWRITEV = 296 => sys_pwritev(args[..4]);
SYS_PRLIMIT64 = 302 => sys_prlimit64(args[..4]);
SYS_GETCPU = 309 => sys_getcpu(args[..3]);
SYS_GETRANDOM = 318 => sys_getrandom(args[..3]);
SYS_EXECVEAT = 322 => sys_execveat(args[..5], &mut user_ctx);
SYS_PREADV2 = 327 => sys_preadv2(args[..5]);

View File

@ -0,0 +1,27 @@
// SPDX-License-Identifier: MPL-2.0
use ostd::{cpu::PinCurrentCpu, task::disable_preempt};
use super::SyscallReturn;
use crate::prelude::*;
pub fn sys_getcpu(cpu: Vaddr, node: Vaddr, _tcache: Vaddr, ctx: &Context) -> Result<SyscallReturn> {
// The third argument tcache is unused after Linux 2.6.24 so we ignore it
let preempt_guard = disable_preempt();
let cpuid = preempt_guard.current_cpu();
drop(preempt_guard);
debug!(
"getcpu: cpuid = {}, total_cpus = {}",
cpuid.as_usize(),
ostd::cpu::num_cpus()
);
// Since cpu and node can be NULL, we need to check them before writing
if cpu != 0 {
ctx.user_space()
.write_val::<usize>(cpu, &cpuid.as_usize())?;
}
if node != 0 {
ctx.user_space().write_val::<usize>(node, &0)?; // TODO: NUMA is not supported
}
Ok(SyscallReturn::Return(0))
}

View File

@ -37,6 +37,7 @@ mod flock;
mod fork;
mod fsync;
mod futex;
mod getcpu;
mod getcwd;
mod getdents64;
mod getegid;