1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28 
29 #ifndef MPU_WRAPPERS_H
30 #define MPU_WRAPPERS_H
31 
32 /* This file redefines API functions to be called through a wrapper macro, but
33  * only for ports that are using the MPU. */
34 #if ( portUSING_MPU_WRAPPERS == 1 )
35 
36 /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE will be defined when this file is
37  * included from queue.c or task.c to prevent it from having an effect within
38  * those files. */
39     #ifndef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
40 
41 /*
42  * Map standard (non MPU) API functions to equivalents that start
43  * "MPU_".  This will cause the application code to call the MPU_
44  * version, which wraps the non-MPU version with privilege promoting
45  * then demoting code, so the kernel code always runs will full
46  * privileges.
47  */
48 
49 /* Map standard task.h API functions to the MPU equivalents. */
50         #define vTaskDelay                            MPU_vTaskDelay
51         #define xTaskDelayUntil                       MPU_xTaskDelayUntil
52         #define xTaskAbortDelay                       MPU_xTaskAbortDelay
53         #define uxTaskPriorityGet                     MPU_uxTaskPriorityGet
54         #define eTaskGetState                         MPU_eTaskGetState
55         #define vTaskGetInfo                          MPU_vTaskGetInfo
56         #define vTaskSuspend                          MPU_vTaskSuspend
57         #define vTaskResume                           MPU_vTaskResume
58         #define xTaskGetTickCount                     MPU_xTaskGetTickCount
59         #define uxTaskGetNumberOfTasks                MPU_uxTaskGetNumberOfTasks
60         #define uxTaskGetStackHighWaterMark           MPU_uxTaskGetStackHighWaterMark
61         #define uxTaskGetStackHighWaterMark2          MPU_uxTaskGetStackHighWaterMark2
62         #define vTaskSetApplicationTaskTag            MPU_vTaskSetApplicationTaskTag
63         #define xTaskGetApplicationTaskTag            MPU_xTaskGetApplicationTaskTag
64         #define vTaskSetThreadLocalStoragePointer     MPU_vTaskSetThreadLocalStoragePointer
65         #define pvTaskGetThreadLocalStoragePointer    MPU_pvTaskGetThreadLocalStoragePointer
66         #define xTaskGetIdleTaskHandle                MPU_xTaskGetIdleTaskHandle
67         #define uxTaskGetSystemState                  MPU_uxTaskGetSystemState
68         #define ulTaskGetIdleRunTimeCounter           MPU_ulTaskGetIdleRunTimeCounter
69         #define ulTaskGetIdleRunTimePercent           MPU_ulTaskGetIdleRunTimePercent
70         #define xTaskGenericNotify                    MPU_xTaskGenericNotify
71         #define xTaskGenericNotifyWait                MPU_xTaskGenericNotifyWait
72         #define ulTaskGenericNotifyTake               MPU_ulTaskGenericNotifyTake
73         #define xTaskGenericNotifyStateClear          MPU_xTaskGenericNotifyStateClear
74         #define ulTaskGenericNotifyValueClear         MPU_ulTaskGenericNotifyValueClear
75         #define vTaskSetTimeOutState                  MPU_vTaskSetTimeOutState
76         #define xTaskCheckForTimeOut                  MPU_xTaskCheckForTimeOut
77         #define xTaskGetCurrentTaskHandle             MPU_xTaskGetCurrentTaskHandle
78         #define xTaskGetSchedulerState                MPU_xTaskGetSchedulerState
79 
80         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
81             #define ulTaskGetRunTimeCounter           MPU_ulTaskGetRunTimeCounter
82             #define ulTaskGetRunTimePercent           MPU_ulTaskGetRunTimePercent
83         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
84 
85 /* Privileged only wrappers for Task APIs. These are needed so that
86  * the application can use opaque handles maintained in mpu_wrappers.c
87  * with all the APIs. */
88         #if ( configUSE_MPU_WRAPPERS_V1 == 1 )
89 
90 /* These are not needed in v2 because they do not take a task
91  * handle and therefore, no lookup is needed. Needed in v1 because
92  * these are available as system calls in v1. */
93             #define vTaskGetRunTimeStatistics            MPU_vTaskGetRunTimeStatistics
94             #define vTaskListTasks                       MPU_vTaskListTasks
95             #define vTaskSuspendAll                      MPU_vTaskSuspendAll
96             #define xTaskCatchUpTicks                    MPU_xTaskCatchUpTicks
97             #define xTaskResumeAll                       MPU_xTaskResumeAll
98         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 1 ) */
99 
100         #define xTaskCreate                              MPU_xTaskCreate
101         #define xTaskCreateStatic                        MPU_xTaskCreateStatic
102         #define vTaskDelete                              MPU_vTaskDelete
103         #define vTaskPrioritySet                         MPU_vTaskPrioritySet
104         #define xTaskGetHandle                           MPU_xTaskGetHandle
105         #define xTaskCallApplicationTaskHook             MPU_xTaskCallApplicationTaskHook
106 
107         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
108             #define pcTaskGetName                        MPU_pcTaskGetName
109             #define xTaskCreateRestricted                MPU_xTaskCreateRestricted
110             #define xTaskCreateRestrictedStatic          MPU_xTaskCreateRestrictedStatic
111             #define vTaskAllocateMPURegions              MPU_vTaskAllocateMPURegions
112             #define xTaskGetStaticBuffers                MPU_xTaskGetStaticBuffers
113             #define uxTaskPriorityGetFromISR             MPU_uxTaskPriorityGetFromISR
114             #define uxTaskBasePriorityGet                MPU_uxTaskBasePriorityGet
115             #define uxTaskBasePriorityGetFromISR         MPU_uxTaskBasePriorityGetFromISR
116             #define xTaskResumeFromISR                   MPU_xTaskResumeFromISR
117             #define xTaskGetApplicationTaskTagFromISR    MPU_xTaskGetApplicationTaskTagFromISR
118             #define xTaskGenericNotifyFromISR            MPU_xTaskGenericNotifyFromISR
119             #define vTaskGenericNotifyGiveFromISR        MPU_vTaskGenericNotifyGiveFromISR
120         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
121 
122 /* Map standard queue.h API functions to the MPU equivalents. */
123         #define xQueueGenericSend            MPU_xQueueGenericSend
124         #define xQueueReceive                MPU_xQueueReceive
125         #define xQueuePeek                   MPU_xQueuePeek
126         #define xQueueSemaphoreTake          MPU_xQueueSemaphoreTake
127         #define uxQueueMessagesWaiting       MPU_uxQueueMessagesWaiting
128         #define uxQueueSpacesAvailable       MPU_uxQueueSpacesAvailable
129         #define xQueueGetMutexHolder         MPU_xQueueGetMutexHolder
130         #define xQueueTakeMutexRecursive     MPU_xQueueTakeMutexRecursive
131         #define xQueueGiveMutexRecursive     MPU_xQueueGiveMutexRecursive
132         #define xQueueAddToSet               MPU_xQueueAddToSet
133         #define xQueueSelectFromSet          MPU_xQueueSelectFromSet
134 
135         #if ( configQUEUE_REGISTRY_SIZE > 0 )
136             #define vQueueAddToRegistry      MPU_vQueueAddToRegistry
137             #define vQueueUnregisterQueue    MPU_vQueueUnregisterQueue
138             #define pcQueueGetName           MPU_pcQueueGetName
139         #endif /* #if ( configQUEUE_REGISTRY_SIZE > 0 ) */
140 
141 /* Privileged only wrappers for Queue APIs. These are needed so that
142  * the application can use opaque handles maintained in mpu_wrappers.c
143  * with all the APIs. */
144         #define vQueueDelete                           MPU_vQueueDelete
145         #define xQueueCreateMutex                      MPU_xQueueCreateMutex
146         #define xQueueCreateMutexStatic                MPU_xQueueCreateMutexStatic
147         #define xQueueCreateCountingSemaphore          MPU_xQueueCreateCountingSemaphore
148         #define xQueueCreateCountingSemaphoreStatic    MPU_xQueueCreateCountingSemaphoreStatic
149         #define xQueueGenericCreate                    MPU_xQueueGenericCreate
150         #define xQueueGenericCreateStatic              MPU_xQueueGenericCreateStatic
151         #define xQueueGenericReset                     MPU_xQueueGenericReset
152         #define xQueueCreateSet                        MPU_xQueueCreateSet
153         #define xQueueCreateSetStatic                  MPU_xQueueCreateSetStatic
154         #define xQueueRemoveFromSet                    MPU_xQueueRemoveFromSet
155 
156         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
157             #define xQueueGenericGetStaticBuffers      MPU_xQueueGenericGetStaticBuffers
158             #define xQueueGenericSendFromISR           MPU_xQueueGenericSendFromISR
159             #define xQueueGiveFromISR                  MPU_xQueueGiveFromISR
160             #define xQueuePeekFromISR                  MPU_xQueuePeekFromISR
161             #define xQueueReceiveFromISR               MPU_xQueueReceiveFromISR
162             #define xQueueIsQueueEmptyFromISR          MPU_xQueueIsQueueEmptyFromISR
163             #define xQueueIsQueueFullFromISR           MPU_xQueueIsQueueFullFromISR
164             #define uxQueueMessagesWaitingFromISR      MPU_uxQueueMessagesWaitingFromISR
165             #define xQueueGetMutexHolderFromISR        MPU_xQueueGetMutexHolderFromISR
166             #define xQueueSelectFromSetFromISR         MPU_xQueueSelectFromSetFromISR
167         #endif /* if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
168 
169 /* Map standard timer.h API functions to the MPU equivalents. */
170         #define pvTimerGetTimerID                 MPU_pvTimerGetTimerID
171         #define vTimerSetTimerID                  MPU_vTimerSetTimerID
172         #define xTimerIsTimerActive               MPU_xTimerIsTimerActive
173         #define xTimerGetTimerDaemonTaskHandle    MPU_xTimerGetTimerDaemonTaskHandle
174         #define xTimerGenericCommandFromTask      MPU_xTimerGenericCommandFromTask
175         #define pcTimerGetName                    MPU_pcTimerGetName
176         #define vTimerSetReloadMode               MPU_vTimerSetReloadMode
177         #define uxTimerGetReloadMode              MPU_uxTimerGetReloadMode
178         #define xTimerGetPeriod                   MPU_xTimerGetPeriod
179         #define xTimerGetExpiryTime               MPU_xTimerGetExpiryTime
180 
181         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
182             #define xTimerGetReloadMode           MPU_xTimerGetReloadMode
183         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
184 
185 /* Privileged only wrappers for Timer APIs. These are needed so that
186  * the application can use opaque handles maintained in mpu_wrappers.c
187  * with all the APIs. */
188         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
189             #define xTimerCreate                   MPU_xTimerCreate
190             #define xTimerCreateStatic             MPU_xTimerCreateStatic
191             #define xTimerGetStaticBuffer          MPU_xTimerGetStaticBuffer
192             #define xTimerGenericCommandFromISR    MPU_xTimerGenericCommandFromISR
193         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
194 
195 /* Map standard event_group.h API functions to the MPU equivalents. */
196         #define xEventGroupWaitBits          MPU_xEventGroupWaitBits
197         #define xEventGroupClearBits         MPU_xEventGroupClearBits
198         #define xEventGroupSetBits           MPU_xEventGroupSetBits
199         #define xEventGroupSync              MPU_xEventGroupSync
200 
201         #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
202             #define uxEventGroupGetNumber    MPU_uxEventGroupGetNumber
203             #define vEventGroupSetNumber     MPU_vEventGroupSetNumber
204         #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) */
205 
206 /* Privileged only wrappers for Event Group APIs. These are needed so that
207  * the application can use opaque handles maintained in mpu_wrappers.c
208  * with all the APIs. */
209         #define xEventGroupCreate                  MPU_xEventGroupCreate
210         #define xEventGroupCreateStatic            MPU_xEventGroupCreateStatic
211         #define vEventGroupDelete                  MPU_vEventGroupDelete
212 
213         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
214             #define xEventGroupGetStaticBuffer     MPU_xEventGroupGetStaticBuffer
215             #define xEventGroupClearBitsFromISR    MPU_xEventGroupClearBitsFromISR
216             #define xEventGroupSetBitsFromISR      MPU_xEventGroupSetBitsFromISR
217             #define xEventGroupGetBitsFromISR      MPU_xEventGroupGetBitsFromISR
218         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
219 
220 /* Map standard message/stream_buffer.h API functions to the MPU
221  * equivalents. */
222         #define xStreamBufferSend                      MPU_xStreamBufferSend
223         #define xStreamBufferReceive                   MPU_xStreamBufferReceive
224         #define xStreamBufferIsFull                    MPU_xStreamBufferIsFull
225         #define xStreamBufferIsEmpty                   MPU_xStreamBufferIsEmpty
226         #define xStreamBufferSpacesAvailable           MPU_xStreamBufferSpacesAvailable
227         #define xStreamBufferBytesAvailable            MPU_xStreamBufferBytesAvailable
228         #define xStreamBufferSetTriggerLevel           MPU_xStreamBufferSetTriggerLevel
229         #define xStreamBufferNextMessageLengthBytes    MPU_xStreamBufferNextMessageLengthBytes
230 
231 /* Privileged only wrappers for Stream Buffer APIs. These are needed so that
232  * the application can use opaque handles maintained in mpu_wrappers.c
233  * with all the APIs. */
234 
235         #define xStreamBufferGenericCreate                  MPU_xStreamBufferGenericCreate
236         #define xStreamBufferGenericCreateStatic            MPU_xStreamBufferGenericCreateStatic
237         #define vStreamBufferDelete                         MPU_vStreamBufferDelete
238         #define xStreamBufferReset                          MPU_xStreamBufferReset
239 
240         #if ( configUSE_MPU_WRAPPERS_V1 == 0 )
241             #define xStreamBufferGetStaticBuffers           MPU_xStreamBufferGetStaticBuffers
242             #define xStreamBufferSendFromISR                MPU_xStreamBufferSendFromISR
243             #define xStreamBufferReceiveFromISR             MPU_xStreamBufferReceiveFromISR
244             #define xStreamBufferSendCompletedFromISR       MPU_xStreamBufferSendCompletedFromISR
245             #define xStreamBufferReceiveCompletedFromISR    MPU_xStreamBufferReceiveCompletedFromISR
246             #define xStreamBufferResetFromISR               MPU_xStreamBufferResetFromISR
247         #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */
248 
249         #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) )
250 
251             #define vGrantAccessToTask( xTask, xTaskToGrantAccess )                        vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xTaskToGrantAccess ) )
252             #define vRevokeAccessToTask( xTask, xTaskToRevokeAccess )                      vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xTaskToRevokeAccess ) )
253 
254             #define vGrantAccessToSemaphore( xTask, xSemaphoreToGrantAccess )              vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xSemaphoreToGrantAccess ) )
255             #define vRevokeAccessToSemaphore( xTask, xSemaphoreToRevokeAccess )            vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xSemaphoreToRevokeAccess ) )
256 
257             #define vGrantAccessToQueue( xTask, xQueueToGrantAccess )                      vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xQueueToGrantAccess ) )
258             #define vRevokeAccessToQueue( xTask, xQueueToRevokeAccess )                    vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xQueueToRevokeAccess ) )
259 
260             #define vGrantAccessToQueueSet( xTask, xQueueSetToGrantAccess )                vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xQueueSetToGrantAccess ) )
261             #define vRevokeAccessToQueueSet( xTask, xQueueSetToRevokeAccess )              vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xQueueSetToRevokeAccess ) )
262 
263             #define vGrantAccessToEventGroup( xTask, xEventGroupToGrantAccess )            vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xEventGroupToGrantAccess ) )
264             #define vRevokeAccessToEventGroup( xTask, xEventGroupToRevokeAccess )          vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xEventGroupToRevokeAccess ) )
265 
266             #define vGrantAccessToStreamBuffer( xTask, xStreamBufferToGrantAccess )        vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xStreamBufferToGrantAccess ) )
267             #define vRevokeAccessToStreamBuffer( xTask, xStreamBufferToRevokeAccess )      vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xStreamBufferToRevokeAccess ) )
268 
269             #define vGrantAccessToMessageBuffer( xTask, xMessageBufferToGrantAccess )      vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xMessageBufferToGrantAccess ) )
270             #define vRevokeAccessToMessageBuffer( xTask, xMessageBufferToRevokeAccess )    vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xMessageBufferToRevokeAccess ) )
271 
272             #define vGrantAccessToTimer( xTask, xTimerToGrantAccess )                      vGrantAccessToKernelObject( ( xTask ), ( int32_t ) ( xTimerToGrantAccess ) )
273             #define vRevokeAccessToTimer( xTask, xTimerToRevokeAccess )                    vRevokeAccessToKernelObject( ( xTask ), ( int32_t ) ( xTimerToRevokeAccess ) )
274 
275         #endif /* #if ( ( configUSE_MPU_WRAPPERS_V1 == 0 ) && ( configENABLE_ACCESS_CONTROL_LIST == 1 ) ) */
276 
277     #endif /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE */
278 
279     #define PRIVILEGED_FUNCTION     __attribute__( ( section( "privileged_functions" ) ) )
280     #define PRIVILEGED_DATA         __attribute__( ( section( "privileged_data" ) ) )
281     #define FREERTOS_SYSTEM_CALL    __attribute__( ( section( "freertos_system_calls" ) ) )
282 
283 #else /* portUSING_MPU_WRAPPERS */
284 
285     #define PRIVILEGED_FUNCTION
286     #define PRIVILEGED_DATA
287     #define FREERTOS_SYSTEM_CALL
288 
289 #endif /* portUSING_MPU_WRAPPERS */
290 
291 
292 #endif /* MPU_WRAPPERS_H */
293