CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1#pragma once
2#include "FreeRTOS.h"
3#include "thread.h"
4#include <locks.h>
5#define INC_TASK_H
6
7/**
8 * \file
9 * \brief Minimal FreeRTOS task compatibility APIs.
10 *
11 * This file defines the compatibility wrappers for FreeRTOS's task APIs.
12 *
13 * FreeRTOS's task abstraction is similar to a UNIX process and does not map
14 * directly to a CHERIoT RTOS concept. CHERIoT RTOS also does not support
15 * dynamic thread creation and so the task-creation APIs are defined to give
16 * compile-time errors.
17 */
18
19/**
20 * Initialise a timeout with the current number of elapsed ticks. This is used
21 * in a blocking function in conjunction with `xTaskCheckForTimeOut()`. The
22 * value returned here should be passed as the first argument of
23 * `xTaskCheckForTimeOut()`.
24 */
25static inline void vTaskSetTimeOutState(TimeOut_t *timeout)
26{
27 SystickReturn ret = thread_systemtick_get();
28 *timeout = ((uint64_t)ret.hi << 32) | ret.lo;
29}
30
31/**
32 * Return whether a timeout has expired. The first argument should be a
33 * pointer to the value returned by `vTaskSetTimeOutState()` before any
34 * blocking operations. The second argument is the number of ticks to wait.
35 * This function returns `pdTRUE` if the timeout has expired, or `pdFALSE` if
36 * the timeout has not expired, and updates the arguments to reflect the
37 * remaining time to wait.
38 */
39static inline BaseType_t xTaskCheckForTimeOut(TimeOut_t *pxTimeOut,
40 TickType_t *pxTicksToWait)
41{
42 BaseType_t xReturn;
43 uint64_t timeBlock;
44 TickType_t xElapsedTime;
45
46 vTaskSetTimeOutState(&timeBlock);
47
48 xElapsedTime = timeBlock - *pxTimeOut;
49 if (xElapsedTime < *pxTicksToWait)
50 {
51 *pxTicksToWait -= xElapsedTime;
52 *pxTimeOut = timeBlock;
53 xReturn = pdFALSE;
54 }
55 else
56 {
57 *pxTicksToWait = (TickType_t)0;
58 xReturn = pdTRUE;
59 }
60
61 return xReturn;
62}
63
64/**
65 * Block the current thread for a given number of ticks.
66 */
67static inline void vTaskDelay(const TickType_t xTicksToDelay)
68{
69 struct Timeout timeout = {0, xTicksToDelay};
70 /*
71 * The FreeRTOS API does not have a way to signal failure of sleep, so we
72 * override the nodiscard annotation on thread_sleep.
73 */
74 (void)thread_sleep(&timeout, ThreadSleepNoEarlyWake);
75}
76
77/**
78 * Return the number of ticks elapsed since the system booted. This is
79 * truncated to a 32-bit value.
80 */
81static inline TickType_t xTaskGetTickCount(void)
82{
83 return thread_systemtick_get().lo;
84}
85
86/**
87 * Returns the current thread ID.
88 */
90{
91 return thread_id_get();
92}
93
94__BEGIN_DECLS
95
96/**
97 * Lock used to simulate disabling interrupts in `taskENTER_CRITICAL` and
98 * `taskEXIT_CRITICAL`. Code using these APIs must provide a definition to
99 * accompany this declaration.
100 */
102
103/**
104 * Lock used to simulate disabling interrupts in `vTaskSuspendAll` and
105 * `xTaskResumeAll`. Code using these APIs must provide a definition to
106 * accompany this declaration.
107 */
109
110__END_DECLS
111
112/**
113 * Critical section. This acquires a recursive mutex. In FreeRTOS, this
114 * disables interrupts. The CHERIoT RTOS security model does not permit
115 * interrupts to be arbitrarily disabled, only for designated scopes. Callers
116 * of this should be carefully audited to ensure that they are safe.
117 */
118static inline void taskENTER_CRITICAL(void)
119{
120 Timeout t = {0, UnlimitedTimeout};
122}
123
124/**
125 * Exit a critical section entered with `taskENTER_CRITICAL`.
126 */
127static inline void taskEXIT_CRITICAL(void)
128{
130}
131
132/**
133 * Critical section. This acquires a recursive mutex. In FreeRTOS, this
134 * disables interrupts. The CHERIoT RTOS security model does not permit
135 * interrupts to be arbitrarily disabled, only for designated scopes. Callers
136 * of this should be carefully audited to ensure that they are safe.
137 */
138static inline void vTaskSuspendAll(void)
139{
140 Timeout t = {0, UnlimitedTimeout};
142}
143
144/**
145 * Exit a critical section entered with `vTaskSuspendAll`.
146 */
147static inline BaseType_t xTaskResumeAll(void)
148{
150 return pdTRUE;
151}
152
153/**
154 * Exit a critical section entered with `vTaskSuspendAll`.
155 */
156static inline void vTaskResumeAll(void)
157{
159}
160
161/**
162 * Task creation API. CHERIoT RTOS does not permit dynamic thread creation and
163 * so this simply provides a warning so that ported code can be modified to
164 * avoid the API.
165 */
166#define xTaskCreate(...) \
167 _Pragma("GCC warning \"Dynamic thread creation is not supported\"")( \
168 TaskHandle_t) - \
169 1
170
171/**
172 * Task creation API. CHERIoT RTOS does not permit dynamic thread creation and
173 * so this simply provides a warning so that ported code can be modified to
174 * avoid the API.
175 */
176#define xTaskCreateStatic(...) \
177 _Pragma("GCC warning \"Dynamic thread creation is not supported\"")( \
178 TaskHandle_t) - \
179 1
FreeRTOS porting layer.
int32_t BaseType_t
FreeRTOS default signed integer type.
Definition FreeRTOS.h:179
Ticks TickType_t
FreeRTOS type for durations expressed in ticks.
Definition FreeRTOS.h:175
#define pdFALSE
FreeRTOS definition of false.
Definition FreeRTOS.h:48
uint16_t TaskHandle_t
FreeRTOS type representing a task handle, mapped to a thread ID in CHERIoT RTOS.
Definition FreeRTOS.h:188
uint64_t TimeOut_t
FreeRTOS type for a duration expressed in large numbers of ticks.
Definition FreeRTOS.h:192
#define pdTRUE
FreeRTOS definition of true.
Definition FreeRTOS.h:52
C compatible interface to synchronisation primitives.
int __cheri_libcall recursivemutex_unlock(struct RecursiveMutexState *mutex)
Unlock a recursive mutex.
int __cheri_libcall recursivemutex_trylock(TimeoutArgument timeout, struct RecursiveMutexState *lock)
Try to acquire a recursive mutex.
State for a recursive mutex.
Definition locks.h:48
the absolute system tick value since boot, 64-bit, assuming never overflows
Definition thread.h:21
uint32_t hi
hi 32 bits
Definition thread.h:25
uint32_t lo
low 32 bits of the system tick
Definition thread.h:23
Structure representing a timeout.
Definition timeout.h:47
static TaskHandle_t xTaskGetCurrentTaskHandle(void)
Returns the current thread ID.
Definition task.h:89
static BaseType_t xTaskCheckForTimeOut(TimeOut_t *pxTimeOut, TickType_t *pxTicksToWait)
Return whether a timeout has expired.
Definition task.h:39
static void taskENTER_CRITICAL(void)
Critical section.
Definition task.h:118
static void taskEXIT_CRITICAL(void)
Exit a critical section entered with taskENTER_CRITICAL.
Definition task.h:127
static void vTaskSetTimeOutState(TimeOut_t *timeout)
Initialise a timeout with the current number of elapsed ticks.
Definition task.h:25
static void vTaskDelay(const TickType_t xTicksToDelay)
Block the current thread for a given number of ticks.
Definition task.h:67
struct RecursiveMutexState __CriticalSectionFlagLock
Lock used to simulate disabling interrupts in taskENTER_CRITICAL and taskEXIT_CRITICAL.
static void vTaskResumeAll(void)
Exit a critical section entered with vTaskSuspendAll.
Definition task.h:156
struct RecursiveMutexState __SuspendFlagLock
Lock used to simulate disabling interrupts in vTaskSuspendAll and xTaskResumeAll.
static BaseType_t xTaskResumeAll(void)
Exit a critical section entered with vTaskSuspendAll.
Definition task.h:147
static void vTaskSuspendAll(void)
Critical section.
Definition task.h:138
static TickType_t xTaskGetTickCount(void)
Return the number of ticks elapsed since the system booted.
Definition task.h:81
Functions and types used for thread management.
int thread_sleep(TimeoutArgument timeout, uint32_t flags)
Sleep for at most the specified timeout (see timeout.h).
@ ThreadSleepNoEarlyWake
Sleep for up to the specified timeout, but wake early if there are no other runnable threads.
Definition thread.h:38
uint16_t __cheri_libcall thread_id_get(__if_c(void))
Return the thread ID of the current running thread.
static const Ticks UnlimitedTimeout
Value indicating an unbounded timeout for use with Timeout.
Definition timeout.h:22