CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
futex.h
Go to the documentation of this file.
1// Copyright Microsoft and CHERIoT Contributors.
2// SPDX-License-Identifier: MIT
3
4#pragma once
5#include <cdefs.h>
6#include <stdint.h>
7#include <timeout.h>
8
9/**
10 * \file
11 *
12 * Futex (compare-and-wait) APIs.
13 */
14
15/**
16 * Flags for `futex_timed_wait`.
17 */
18enum [[clang::flag_enum]] FutexWaitFlags
19{
20 /// No flags
22 /**
23 * This futex uses priority inheritance. The low 16 bits of the futex word
24 * are assumed to hold the thread ID of the thread that currently holds the
25 * lock.
26 */
28};
29
30/**
31 * Compare the value at `address` to `expected` and, if they match, sleep the
32 * thread until a wake event is sent with `futex_wake` or until the thread has
33 * slept for `ticks` ticks.
34 *
35 * The value of `ticks` specifies the permitted timeout. See `timeout.h` for
36 * details.
37 *
38 * The `address` argument must permit loading four bytes of data after the
39 * address.
40 *
41 * The `flags` argument contains flags that may control the behaviour of the
42 * call. This is either `FutexNone` (zero) for the default behaviour or
43 * `FutexPriorityInheritance` if the low 16 bits should be treated as a thread
44 * ID for priority inheritance.
45 *
46 * This returns:
47 *
48 * - 0 on success: either `*address` and `expected` differ or a wake is
49 * received.
50 * - `-EINVAL` if the arguments are invalid.
51 * - `-ETIMEDOUT` if the timeout expires.
52 */
53[[cheriot::interrupt_state(disabled)]] int __cheri_compartment("scheduler")
54 futex_timed_wait(TimeoutArgument timeout,
55 const volatile uint32_t *address,
56 uint32_t expected,
57 uint32_t flags __if_cxx(= FutexNone));
58
59/**
60 * Compare the value at `address` to `expected` and, if they match, sleep the
61 * thread until a wake event is sent with `futex_wake`.
62 *
63 * The `address` argument must permit loading four bytes of data after the
64 * address.
65 *
66 * This returns 0 on success or `-EINVAL` if the arguments are invalid.
67 */
68__always_inline static int futex_wait(const volatile uint32_t *address,
69 uint32_t expected)
70{
71 return futex_timed_wait(TimeoutWaitForever, address, expected, FutexNone);
72}
73
74/**
75 * Wakes up to `count` threads that are sleeping with `futex_timed_wait` on
76 * `address`.
77 *
78 * The `address` argument must be a valid unsealed pointer with a length of at
79 * least four after the address but the scheduler does not require any explicit
80 * permissions. The scheduler never needs store access to the futex word.
81 * Removing store permission means that a compromised scheduler can cause
82 * spurious wakes but cannot tamper with the futex word. If, for example, the
83 * futex word is a lock then the scheduler can wake threads that are blocked on
84 * the lock but cannot release the lock and so cannot make two threads believe
85 * that they have simultaneously acquired the same lock.
86 *
87 * The return value for a successful call is the number of threads that were
88 * woken. `-EINVAL` is returned for invalid arguments.
89 */
90[[cheriot::interrupt_state(disabled)]] int __cheri_compartment("scheduler")
91 futex_wake(const volatile uint32_t *address, uint32_t count);
FutexWaitFlags
Flags for futex_timed_wait.
Definition futex.h:19
@ FutexPriorityInheritance
This futex uses priority inheritance.
Definition futex.h:27
@ FutexNone
No flags.
Definition futex.h:21
int futex_timed_wait(TimeoutArgument timeout, const volatile uint32_t *address, uint32_t expected, uint32_t flags)
Compare the value at address to expected and, if they match, sleep the thread until a wake event is s...
static int futex_wait(const volatile uint32_t *address, uint32_t expected)
Compare the value at address to expected and, if they match, sleep the thread until a wake event is s...
Definition futex.h:68
int futex_wake(const volatile uint32_t *address, uint32_t count)
Wakes up to count threads that are sleeping with futex_timed_wait on address.
This file contains the types used for timeouts across scheduler APIs.
static const AbsoluteMonotonicTimeout TimeoutWaitForever
Timeout value for unlimited blocking.
Definition timeout.h:31