| 1 |
//! linux_raw syscalls supporting `rustix::thread`. |
| 2 |
//! |
| 3 |
//! # Safety |
| 4 |
//! |
| 5 |
//! See the `rustix::backend` module documentation for details. |
| 6 |
#![allow(unsafe_code, clippy::undocumented_unsafe_blocks)] |
| 7 |
|
| 8 |
use super::types::RawCpuSet; |
| 9 |
use crate::backend::c; |
| 10 |
use crate::backend::conv::{ |
| 11 |
by_mut, by_ref, c_int, c_uint, opt_ref, ret, ret_c_int, ret_c_int_infallible, ret_c_uint, |
| 12 |
ret_usize, size_of, slice, slice_just_addr, slice_just_addr_mut, zero, |
| 13 |
}; |
| 14 |
use crate::fd::BorrowedFd; |
| 15 |
use crate::io; |
| 16 |
use crate::pid::Pid; |
| 17 |
use crate::thread::{ |
| 18 |
futex, ClockId, Cpuid, MembarrierCommand, MembarrierQuery, NanosleepRelativeResult, Timespec, |
| 19 |
}; |
| 20 |
use crate::utils::as_mut_ptr; |
| 21 |
use core::mem::MaybeUninit; |
| 22 |
use core::sync::atomic::AtomicU32; |
| 23 |
#[cfg(target_pointer_width = "32")] |
| 24 |
use linux_raw_sys::general::timespec as __kernel_old_timespec; |
| 25 |
use linux_raw_sys::general::{membarrier_cmd, membarrier_cmd_flag, TIMER_ABSTIME}; |
| 26 |
|
| 27 |
#[inline] |
| 28 |
pub(crate) fn clock_nanosleep_relative(id: ClockId, req: &Timespec) -> NanosleepRelativeResult { |
| 29 |
#[cfg(target_pointer_width = "32")] |
| 30 |
unsafe { |
| 31 |
let mut rem = MaybeUninit::<Timespec>::uninit(); |
| 32 |
match ret(syscall!( |
| 33 |
__NR_clock_nanosleep_time64, |
| 34 |
id, |
| 35 |
c_int(0), |
| 36 |
by_ref(req), |
| 37 |
&mut rem |
| 38 |
)) |
| 39 |
.or_else(|err| { |
| 40 |
// See the comments in `clock_gettime_via_syscall` about emulation. |
| 41 |
if err == io::Errno::NOSYS { |
| 42 |
clock_nanosleep_relative_old(id, req, &mut rem) |
| 43 |
} else { |
| 44 |
Err(err) |
| 45 |
} |
| 46 |
}) { |
| 47 |
Ok(()) => NanosleepRelativeResult::Ok, |
| 48 |
Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(rem.assume_init()), |
| 49 |
Err(err) => NanosleepRelativeResult::Err(err), |
| 50 |
} |
| 51 |
} |
| 52 |
#[cfg(target_pointer_width = "64")] |
| 53 |
unsafe { |
| 54 |
let mut rem = MaybeUninit::<Timespec>::uninit(); |
| 55 |
match ret(syscall!( |
| 56 |
__NR_clock_nanosleep, |
| 57 |
id, |
| 58 |
c_int(0), |
| 59 |
by_ref(req), |
| 60 |
&mut rem |
| 61 |
)) { |
| 62 |
Ok(()) => NanosleepRelativeResult::Ok, |
| 63 |
Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(rem.assume_init()), |
| 64 |
Err(err) => NanosleepRelativeResult::Err(err), |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[cfg(target_pointer_width = "32")] |
| 70 |
unsafe fn clock_nanosleep_relative_old( |
| 71 |
id: ClockId, |
| 72 |
req: &Timespec, |
| 73 |
rem: &mut MaybeUninit<Timespec>, |
| 74 |
) -> io::Result<()> { |
| 75 |
let old_req = __kernel_old_timespec { |
| 76 |
tv_sec: req.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?, |
| 77 |
tv_nsec: req.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?, |
| 78 |
}; |
| 79 |
let mut old_rem = MaybeUninit::<__kernel_old_timespec>::uninit(); |
| 80 |
ret(syscall!( |
| 81 |
__NR_clock_nanosleep, |
| 82 |
id, |
| 83 |
c_int(0), |
| 84 |
by_ref(&old_req), |
| 85 |
&mut old_rem |
| 86 |
))?; |
| 87 |
let old_rem = old_rem.assume_init(); |
| 88 |
rem.write(Timespec { |
| 89 |
tv_sec: old_rem.tv_sec.into(), |
| 90 |
tv_nsec: old_rem.tv_nsec.into(), |
| 91 |
}); |
| 92 |
Ok(()) |
| 93 |
} |
| 94 |
|
| 95 |
#[inline] |
| 96 |
pub(crate) fn clock_nanosleep_absolute(id: ClockId, req: &Timespec) -> io::Result<()> { |
| 97 |
#[cfg(target_pointer_width = "32")] |
| 98 |
unsafe { |
| 99 |
ret(syscall_readonly!( |
| 100 |
__NR_clock_nanosleep_time64, |
| 101 |
id, |
| 102 |
c_uint(TIMER_ABSTIME), |
| 103 |
by_ref(req), |
| 104 |
zero() |
| 105 |
)) |
| 106 |
.or_else(|err| { |
| 107 |
// See the comments in `clock_gettime_via_syscall` about emulation. |
| 108 |
if err == io::Errno::NOSYS { |
| 109 |
clock_nanosleep_absolute_old(id, req) |
| 110 |
} else { |
| 111 |
Err(err) |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
#[cfg(target_pointer_width = "64")] |
| 116 |
unsafe { |
| 117 |
ret(syscall_readonly!( |
| 118 |
__NR_clock_nanosleep, |
| 119 |
id, |
| 120 |
c_uint(TIMER_ABSTIME), |
| 121 |
by_ref(req), |
| 122 |
zero() |
| 123 |
)) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[cfg(target_pointer_width = "32")] |
| 128 |
unsafe fn clock_nanosleep_absolute_old(id: ClockId, req: &Timespec) -> io::Result<()> { |
| 129 |
let old_req = __kernel_old_timespec { |
| 130 |
tv_sec: req.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?, |
| 131 |
tv_nsec: req.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?, |
| 132 |
}; |
| 133 |
ret(syscall_readonly!( |
| 134 |
__NR_clock_nanosleep, |
| 135 |
id, |
| 136 |
c_int(0), |
| 137 |
by_ref(&old_req), |
| 138 |
zero() |
| 139 |
)) |
| 140 |
} |
| 141 |
|
| 142 |
#[inline] |
| 143 |
pub(crate) fn nanosleep(req: &Timespec) -> NanosleepRelativeResult { |
| 144 |
#[cfg(target_pointer_width = "32")] |
| 145 |
unsafe { |
| 146 |
let mut rem = MaybeUninit::<Timespec>::uninit(); |
| 147 |
match ret(syscall!( |
| 148 |
__NR_clock_nanosleep_time64, |
| 149 |
ClockId::Realtime, |
| 150 |
c_int(0), |
| 151 |
by_ref(req), |
| 152 |
&mut rem |
| 153 |
)) |
| 154 |
.or_else(|err| { |
| 155 |
// See the comments in `clock_gettime_via_syscall` about emulation. |
| 156 |
if err == io::Errno::NOSYS { |
| 157 |
nanosleep_old(req, &mut rem) |
| 158 |
} else { |
| 159 |
Err(err) |
| 160 |
} |
| 161 |
}) { |
| 162 |
Ok(()) => NanosleepRelativeResult::Ok, |
| 163 |
Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(rem.assume_init()), |
| 164 |
Err(err) => NanosleepRelativeResult::Err(err), |
| 165 |
} |
| 166 |
} |
| 167 |
#[cfg(target_pointer_width = "64")] |
| 168 |
unsafe { |
| 169 |
let mut rem = MaybeUninit::<Timespec>::uninit(); |
| 170 |
match ret(syscall!(__NR_nanosleep, by_ref(req), &mut rem)) { |
| 171 |
Ok(()) => NanosleepRelativeResult::Ok, |
| 172 |
Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(rem.assume_init()), |
| 173 |
Err(err) => NanosleepRelativeResult::Err(err), |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
#[cfg(target_pointer_width = "32")] |
| 179 |
unsafe fn nanosleep_old(req: &Timespec, rem: &mut MaybeUninit<Timespec>) -> io::Result<()> { |
| 180 |
let old_req = __kernel_old_timespec { |
| 181 |
tv_sec: req.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?, |
| 182 |
tv_nsec: req.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?, |
| 183 |
}; |
| 184 |
let mut old_rem = MaybeUninit::<__kernel_old_timespec>::uninit(); |
| 185 |
ret(syscall!(__NR_nanosleep, by_ref(&old_req), &mut old_rem))?; |
| 186 |
let old_rem = old_rem.assume_init(); |
| 187 |
rem.write(Timespec { |
| 188 |
tv_sec: old_rem.tv_sec.into(), |
| 189 |
tv_nsec: old_rem.tv_nsec.into(), |
| 190 |
}); |
| 191 |
Ok(()) |
| 192 |
} |
| 193 |
|
| 194 |
#[inline] |
| 195 |
#[must_use] |
| 196 |
pub(crate) fn gettid() -> Pid { |
| 197 |
unsafe { |
| 198 |
let tid = ret_c_int_infallible(syscall_readonly!(__NR_gettid)); |
| 199 |
Pid::from_raw_unchecked(tid) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/// # Safety |
| 204 |
/// |
| 205 |
/// The raw pointers must point to valid aligned memory. |
| 206 |
#[inline] |
| 207 |
pub(crate) unsafe fn futex_val2( |
| 208 |
uaddr: *const AtomicU32, |
| 209 |
op: super::futex::Operation, |
| 210 |
flags: futex::Flags, |
| 211 |
val: u32, |
| 212 |
val2: u32, |
| 213 |
uaddr2: *const AtomicU32, |
| 214 |
val3: u32, |
| 215 |
) -> io::Result<usize> { |
| 216 |
// Pass `val2` in the least-significant bytes of the `timeout` argument. |
| 217 |
// [“the kernel casts the timeout value first to unsigned long, then to |
| 218 |
// uint32_t”], so we perform that exact conversion in reverse to create |
| 219 |
// the pointer. |
| 220 |
// |
| 221 |
// [“the kernel casts the timeout value first to unsigned long, then to uint32_t”]: https://man7.org/linux/man-pages/man2/futex.2.html |
| 222 |
let timeout = val2 as usize as *const Timespec; |
| 223 |
|
| 224 |
#[cfg(target_pointer_width = "32")] |
| 225 |
{ |
| 226 |
// Linux 5.1 added `futex_time64`; if we have that, use it. We don't |
| 227 |
// need it here, because `timeout` is just passing `val2` and not a |
| 228 |
// real timeout, but it's nice to use `futex_time64` for consistency |
| 229 |
// with the other futex calls that do. |
| 230 |
#[cfg(feature = "linux_5_1")] |
| 231 |
{ |
| 232 |
ret_usize(syscall!( |
| 233 |
__NR_futex_time64, |
| 234 |
uaddr, |
| 235 |
(op, flags), |
| 236 |
c_uint(val), |
| 237 |
timeout, |
| 238 |
uaddr2, |
| 239 |
c_uint(val3) |
| 240 |
)) |
| 241 |
} |
| 242 |
|
| 243 |
// If we don't have Linux 5.1, use plain `futex`. |
| 244 |
#[cfg(not(feature = "linux_5_1"))] |
| 245 |
{ |
| 246 |
ret_usize(syscall!( |
| 247 |
__NR_futex, |
| 248 |
uaddr, |
| 249 |
(op, flags), |
| 250 |
c_uint(val), |
| 251 |
timeout, |
| 252 |
uaddr2, |
| 253 |
c_uint(val3) |
| 254 |
)) |
| 255 |
} |
| 256 |
} |
| 257 |
#[cfg(target_pointer_width = "64")] |
| 258 |
ret_usize(syscall!( |
| 259 |
__NR_futex, |
| 260 |
uaddr, |
| 261 |
(op, flags), |
| 262 |
c_uint(val), |
| 263 |
timeout, |
| 264 |
uaddr2, |
| 265 |
c_uint(val3) |
| 266 |
)) |
| 267 |
} |
| 268 |
|
| 269 |
/// # Safety |
| 270 |
/// |
| 271 |
/// The raw pointers must point to valid aligned memory. |
| 272 |
#[inline] |
| 273 |
pub(crate) unsafe fn futex_timeout( |
| 274 |
uaddr: *const AtomicU32, |
| 275 |
op: super::futex::Operation, |
| 276 |
flags: futex::Flags, |
| 277 |
val: u32, |
| 278 |
timeout: Option<&Timespec>, |
| 279 |
uaddr2: *const AtomicU32, |
| 280 |
val3: u32, |
| 281 |
) -> io::Result<usize> { |
| 282 |
#[cfg(target_pointer_width = "32")] |
| 283 |
{ |
| 284 |
// If we don't have Linux 5.1, and the timeout fits in a |
| 285 |
// `__kernel_old_timespec`, use plain `futex`. |
| 286 |
// |
| 287 |
// We do this unconditionally, rather than trying `futex_time64` and |
| 288 |
// falling back on `Errno::NOSYS`, because seccomp configurations will |
| 289 |
// sometimes abort the process on syscalls they don't recognize. |
| 290 |
#[cfg(not(feature = "linux_5_1"))] |
| 291 |
{ |
| 292 |
// If we don't have a timeout, or if we can convert the timeout to |
| 293 |
// a `__kernel_old_timespec`, the use `__NR_futex`. |
| 294 |
fn convert(timeout: &Timespec) -> Option<__kernel_old_timespec> { |
| 295 |
Some(__kernel_old_timespec { |
| 296 |
tv_sec: timeout.tv_sec.try_into().ok()?, |
| 297 |
tv_nsec: timeout.tv_nsec.try_into().ok()?, |
| 298 |
}) |
| 299 |
} |
| 300 |
let old_timeout = if let Some(timeout) = timeout { |
| 301 |
match convert(timeout) { |
| 302 |
// Could not convert timeout. |
| 303 |
None => None, |
| 304 |
// Could convert timeout. Ok! |
| 305 |
Some(old_timeout) => Some(Some(old_timeout)), |
| 306 |
} |
| 307 |
} else { |
| 308 |
// No timeout. Ok! |
| 309 |
Some(None) |
| 310 |
}; |
| 311 |
if let Some(old_timeout) = old_timeout { |
| 312 |
return ret_usize(syscall!( |
| 313 |
__NR_futex, |
| 314 |
uaddr, |
| 315 |
(op, flags), |
| 316 |
c_uint(val), |
| 317 |
opt_ref(old_timeout.as_ref()), |
| 318 |
uaddr2, |
| 319 |
c_uint(val3) |
| 320 |
)); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
// We either have Linux 5.1 or the timeout didn't fit in |
| 325 |
// `__kernel_old_timespec` so `__NR_futex_time64` will either succeed |
| 326 |
// or fail due to our having no other options. |
| 327 |
ret_usize(syscall!( |
| 328 |
__NR_futex_time64, |
| 329 |
uaddr, |
| 330 |
(op, flags), |
| 331 |
c_uint(val), |
| 332 |
opt_ref(timeout), |
| 333 |
uaddr2, |
| 334 |
c_uint(val3) |
| 335 |
)) |
| 336 |
} |
| 337 |
#[cfg(target_pointer_width = "64")] |
| 338 |
ret_usize(syscall!( |
| 339 |
__NR_futex, |
| 340 |
uaddr, |
| 341 |
(op, flags), |
| 342 |
c_uint(val), |
| 343 |
opt_ref(timeout), |
| 344 |
uaddr2, |
| 345 |
c_uint(val3) |
| 346 |
)) |
| 347 |
} |
| 348 |
|
| 349 |
#[inline] |
| 350 |
pub(crate) fn futex_waitv( |
| 351 |
waiters: &[futex::Wait], |
| 352 |
flags: futex::WaitvFlags, |
| 353 |
timeout: Option<&Timespec>, |
| 354 |
clockid: ClockId, |
| 355 |
) -> io::Result<usize> { |
| 356 |
let (waiters_addr, waiters_len) = slice(waiters); |
| 357 |
unsafe { |
| 358 |
ret_usize(syscall!( |
| 359 |
__NR_futex_waitv, |
| 360 |
waiters_addr, |
| 361 |
waiters_len, |
| 362 |
c_uint(flags.bits()), |
| 363 |
opt_ref(timeout), |
| 364 |
clockid |
| 365 |
)) |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
#[inline] |
| 370 |
pub(crate) fn setns(fd: BorrowedFd<'_>, nstype: c::c_int) -> io::Result<c::c_int> { |
| 371 |
unsafe { ret_c_int(syscall_readonly!(__NR_setns, fd, c_int(nstype))) } |
| 372 |
} |
| 373 |
|
| 374 |
#[inline] |
| 375 |
pub(crate) unsafe fn unshare(flags: crate::thread::UnshareFlags) -> io::Result<()> { |
| 376 |
ret(syscall_readonly!(__NR_unshare, flags)) |
| 377 |
} |
| 378 |
|
| 379 |
#[inline] |
| 380 |
pub(crate) fn capget( |
| 381 |
header: &mut linux_raw_sys::general::__user_cap_header_struct, |
| 382 |
data: &mut [MaybeUninit<linux_raw_sys::general::__user_cap_data_struct>], |
| 383 |
) -> io::Result<()> { |
| 384 |
unsafe { |
| 385 |
ret(syscall!( |
| 386 |
__NR_capget, |
| 387 |
by_mut(header), |
| 388 |
slice_just_addr_mut(data) |
| 389 |
)) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
#[inline] |
| 394 |
pub(crate) fn capset( |
| 395 |
header: &mut linux_raw_sys::general::__user_cap_header_struct, |
| 396 |
data: &[linux_raw_sys::general::__user_cap_data_struct], |
| 397 |
) -> io::Result<()> { |
| 398 |
unsafe { ret(syscall!(__NR_capset, by_mut(header), slice_just_addr(data))) } |
| 399 |
} |
| 400 |
|
| 401 |
#[inline] |
| 402 |
pub(crate) fn setuid_thread(uid: crate::ugid::Uid) -> io::Result<()> { |
| 403 |
unsafe { ret(syscall_readonly!(__NR_setuid, uid)) } |
| 404 |
} |
| 405 |
|
| 406 |
#[inline] |
| 407 |
pub(crate) fn setresuid_thread( |
| 408 |
ruid: Option<crate::ugid::Uid>, |
| 409 |
euid: Option<crate::ugid::Uid>, |
| 410 |
suid: Option<crate::ugid::Uid>, |
| 411 |
) -> io::Result<()> { |
| 412 |
#[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))] |
| 413 |
unsafe { |
| 414 |
ret(syscall_readonly!(__NR_setresuid32, ruid, euid, suid)) |
| 415 |
} |
| 416 |
#[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))] |
| 417 |
unsafe { |
| 418 |
ret(syscall_readonly!(__NR_setresuid, ruid, euid, suid)) |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
#[inline] |
| 423 |
pub(crate) fn setgid_thread(gid: crate::ugid::Gid) -> io::Result<()> { |
| 424 |
unsafe { ret(syscall_readonly!(__NR_setgid, gid)) } |
| 425 |
} |
| 426 |
|
| 427 |
#[inline] |
| 428 |
pub(crate) fn setresgid_thread( |
| 429 |
rgid: Option<crate::ugid::Gid>, |
| 430 |
egid: Option<crate::ugid::Gid>, |
| 431 |
sgid: Option<crate::ugid::Gid>, |
| 432 |
) -> io::Result<()> { |
| 433 |
#[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))] |
| 434 |
unsafe { |
| 435 |
ret(syscall_readonly!(__NR_setresgid32, rgid, egid, sgid)) |
| 436 |
} |
| 437 |
#[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))] |
| 438 |
unsafe { |
| 439 |
ret(syscall_readonly!(__NR_setresgid, rgid, egid, sgid)) |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
#[inline] |
| 444 |
pub(crate) fn setgroups_thread(gids: &[crate::ugid::Gid]) -> io::Result<()> { |
| 445 |
let (addr, len) = slice(gids); |
| 446 |
unsafe { ret(syscall_readonly!(__NR_setgroups, len, addr)) } |
| 447 |
} |
| 448 |
|
| 449 |
// `sched_getcpu` has special optimizations via the vDSO on some architectures. |
| 450 |
#[cfg(any( |
| 451 |
target_arch = "x86_64", |
| 452 |
target_arch = "x86", |
| 453 |
target_arch = "riscv64", |
| 454 |
target_arch = "powerpc", |
| 455 |
target_arch = "powerpc64", |
| 456 |
target_arch = "s390x" |
| 457 |
))] |
| 458 |
pub(crate) use crate::backend::vdso_wrappers::sched_getcpu; |
| 459 |
|
| 460 |
// `sched_getcpu` on platforms without a vDSO entry for it. |
| 461 |
#[cfg(not(any( |
| 462 |
target_arch = "x86_64", |
| 463 |
target_arch = "x86", |
| 464 |
target_arch = "riscv64", |
| 465 |
target_arch = "powerpc", |
| 466 |
target_arch = "powerpc64", |
| 467 |
target_arch = "s390x" |
| 468 |
)))] |
| 469 |
#[inline] |
| 470 |
pub(crate) fn sched_getcpu() -> usize { |
| 471 |
let mut cpu = MaybeUninit::<u32>::uninit(); |
| 472 |
unsafe { |
| 473 |
let r = ret(syscall!(__NR_getcpu, &mut cpu, zero(), zero())); |
| 474 |
debug_assert!(r.is_ok()); |
| 475 |
cpu.assume_init() as usize |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
#[inline] |
| 480 |
pub(crate) fn sched_getaffinity(pid: Option<Pid>, cpuset: &mut RawCpuSet) -> io::Result<()> { |
| 481 |
unsafe { |
| 482 |
// The raw Linux syscall returns the size (in bytes) of the `cpumask_t` |
| 483 |
// data type that is used internally by the kernel to represent the CPU |
| 484 |
// set bit mask. |
| 485 |
let size = ret_usize(syscall!( |
| 486 |
__NR_sched_getaffinity, |
| 487 |
c_int(Pid::as_raw(pid)), |
| 488 |
size_of::<RawCpuSet, _>(), |
| 489 |
by_mut(&mut cpuset.bits) |
| 490 |
))?; |
| 491 |
let bytes = as_mut_ptr(cpuset).cast::<u8>(); |
| 492 |
let rest = bytes.wrapping_add(size); |
| 493 |
// Zero every byte in the cpuset not set by the kernel. |
| 494 |
rest.write_bytes(0, core::mem::size_of::<RawCpuSet>() - size); |
| 495 |
Ok(()) |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
#[inline] |
| 500 |
pub(crate) fn sched_setaffinity(pid: Option<Pid>, cpuset: &RawCpuSet) -> io::Result<()> { |
| 501 |
unsafe { |
| 502 |
ret(syscall_readonly!( |
| 503 |
__NR_sched_setaffinity, |
| 504 |
c_int(Pid::as_raw(pid)), |
| 505 |
size_of::<RawCpuSet, _>(), |
| 506 |
slice_just_addr(&cpuset.bits) |
| 507 |
)) |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
#[inline] |
| 512 |
pub(crate) fn sched_yield() { |
| 513 |
unsafe { |
| 514 |
// See the documentation for [`crate::thread::sched_yield`] for why |
| 515 |
// errors are ignored. |
| 516 |
syscall_readonly!(__NR_sched_yield).decode_void(); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
#[inline] |
| 521 |
pub(crate) fn membarrier_query() -> MembarrierQuery { |
| 522 |
unsafe { |
| 523 |
match ret_c_uint(syscall!( |
| 524 |
__NR_membarrier, |
| 525 |
c_int(membarrier_cmd::MEMBARRIER_CMD_QUERY as _), |
| 526 |
c_uint(0) |
| 527 |
)) { |
| 528 |
Ok(query) => MembarrierQuery::from_bits_retain(query), |
| 529 |
Err(_) => MembarrierQuery::empty(), |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
#[inline] |
| 535 |
pub(crate) fn membarrier(cmd: MembarrierCommand) -> io::Result<()> { |
| 536 |
unsafe { ret(syscall!(__NR_membarrier, cmd, c_uint(0))) } |
| 537 |
} |
| 538 |
|
| 539 |
#[inline] |
| 540 |
pub(crate) fn membarrier_cpu(cmd: MembarrierCommand, cpu: Cpuid) -> io::Result<()> { |
| 541 |
unsafe { |
| 542 |
ret(syscall!( |
| 543 |
__NR_membarrier, |
| 544 |
cmd, |
| 545 |
c_uint(membarrier_cmd_flag::MEMBARRIER_CMD_FLAG_CPU as _), |
| 546 |
cpu |
| 547 |
)) |
| 548 |
} |
| 549 |
} |