Add dummy syscall sched_getaffinity

This commit is contained in:
Weijie Liu
2024-05-26 04:42:16 +08:00
committed by Tate, Hongliang Tian
parent e1480f94ee
commit 2ecca7af6b
7 changed files with 114 additions and 1 deletions

View File

@ -72,6 +72,7 @@ use crate::syscall::{
rt_sigprocmask::sys_rt_sigprocmask,
rt_sigreturn::sys_rt_sigreturn,
rt_sigsuspend::sys_rt_sigsuspend,
sched_getaffinity::sys_sched_getaffinity,
sched_yield::sys_sched_yield,
select::sys_select,
sendfile::sys_sendfile,
@ -220,6 +221,7 @@ impl_syscall_nums_and_dispatch_fn! {
SYS_GETTID = 186 => sys_gettid(args[..0]);
SYS_TIME = 201 => sys_time(args[..1]);
SYS_FUTEX = 202 => sys_futex(args[..6]);
SYS_SCHED_GETAFFINITY = 204 => sys_sched_getaffinity(args[..3]);
SYS_EPOLL_CREATE = 213 => sys_epoll_create(args[..1]);
SYS_GETDENTS64 = 217 => sys_getdents64(args[..3]);
SYS_SET_TID_ADDRESS = 218 => sys_set_tid_address(args[..1]);

View File

@ -79,6 +79,7 @@ mod rt_sigaction;
mod rt_sigprocmask;
mod rt_sigreturn;
mod rt_sigsuspend;
mod sched_getaffinity;
mod sched_yield;
mod select;
mod sendfile;

View File

@ -0,0 +1,69 @@
// SPDX-License-Identifier: MPL-2.0
use core::{cmp, mem};
use super::SyscallReturn;
use crate::{
prelude::*,
process::{process_table, Pid},
util::write_val_to_user,
};
fn get_num_cpus() -> usize {
// TODO: Properly determine the number of available CPUs
// This could be through a system configuration query.
1
}
pub fn sys_sched_getaffinity(
pid: Pid,
cpuset_size: usize,
cpu_set_ptr: Vaddr,
) -> Result<SyscallReturn> {
let num_cpus = get_num_cpus();
if cpuset_size < core::mem::size_of::<cpu_set_t>() {
return Err(Error::with_message(Errno::EINVAL, "invalid cpuset size"));
}
match pid {
0 => {
// TODO: Get the current thread's CPU affinity
// Placeholder for future implementation.
}
_ => {
match process_table::get_process(&pid) {
Some(_process) => { /* Placeholder if process-specific logic needed */ }
None => return Err(Error::with_message(Errno::ESRCH, "process does not exist")),
}
}
}
let dummy_cpu_set = cpu_set_t::new(num_cpus);
write_val_to_user(cpu_set_ptr, &dummy_cpu_set)?;
Ok(SyscallReturn::Return(0))
}
const CPU_SETSIZE: usize = 1024; // Max number of CPU bits.
const __NCPUBITS: usize = 8 * mem::size_of::<usize>();
#[derive(Debug, Clone, Copy, Pod)]
#[repr(C, packed)]
struct cpu_set_t {
__bits: [usize; CPU_SETSIZE / __NCPUBITS],
}
impl cpu_set_t {
/// Creates a new cpu_set_t representing available CPUs.
fn new(num_cpus: usize) -> Self {
let mut bits = [0usize; CPU_SETSIZE / __NCPUBITS];
for cpu in 0..cmp::min(num_cpus, CPU_SETSIZE) {
bits[cpu / __NCPUBITS] |= 1 << (cpu % __NCPUBITS);
}
Self { __bits: bits }
}
}