forked from FreeRTOS/FreeRTOS-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpu_wrappers_v2.c
5267 lines (4144 loc) · 217 KB
/
mpu_wrappers_v2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeRTOS Kernel <DEVELOPMENT BRANCH>
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
/*
* Implementation of the wrapper functions used to raise the processor privilege
* before calling a standard FreeRTOS API function.
*/
/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
* all the API functions to use the MPU wrappers. That should only be done when
* task.h is included from an application file. */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#include "event_groups.h"
#include "stream_buffer.h"
#include "mpu_prototypes.h"
#include "mpu_syscall_numbers.h"
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
/*-----------------------------------------------------------*/
#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) )
#ifndef configPROTECTED_KERNEL_OBJECT_POOL_SIZE
#error configPROTECTED_KERNEL_OBJECT_POOL_SIZE must be defined to maximum number of kernel objects in the application.
#endif
/**
* @brief Offset added to the index before returning to the user.
*
* If the actual handle is stored at index i, ( i + INDEX_OFFSET )
* is returned to the user.
*/
#define INDEX_OFFSET 1
/**
* @brief Opaque type for a kernel object.
*/
struct OpaqueObject;
typedef struct OpaqueObject * OpaqueObjectHandle_t;
/**
* @brief Defines kernel object in the kernel object pool.
*/
typedef struct KernelObject
{
OpaqueObjectHandle_t xInternalObjectHandle;
uint32_t ulKernelObjectType;
void * pvKernelObjectData;
} KernelObject_t;
/**
* @brief Kernel object types.
*/
#define KERNEL_OBJECT_TYPE_INVALID ( 0UL )
#define KERNEL_OBJECT_TYPE_QUEUE ( 1UL )
#define KERNEL_OBJECT_TYPE_TASK ( 2UL )
#define KERNEL_OBJECT_TYPE_STREAM_BUFFER ( 3UL )
#define KERNEL_OBJECT_TYPE_EVENT_GROUP ( 4UL )
#define KERNEL_OBJECT_TYPE_TIMER ( 5UL )
/**
* @brief Checks whether an external index is valid or not.
*/
#define IS_EXTERNAL_INDEX_VALID( lIndex ) \
( ( ( ( lIndex ) >= INDEX_OFFSET ) && \
( ( lIndex ) < ( configPROTECTED_KERNEL_OBJECT_POOL_SIZE + INDEX_OFFSET ) ) ) ? pdTRUE : pdFALSE )
/**
* @brief Checks whether an internal index is valid or not.
*/
#define IS_INTERNAL_INDEX_VALID( lIndex ) \
( ( ( ( lIndex ) >= 0 ) && \
( ( lIndex ) < ( configPROTECTED_KERNEL_OBJECT_POOL_SIZE ) ) ) ? pdTRUE : pdFALSE )
/**
* @brief Converts an internal index into external.
*/
#define CONVERT_TO_EXTERNAL_INDEX( lIndex ) ( ( lIndex ) + INDEX_OFFSET )
/**
* @brief Converts an external index into internal.
*/
#define CONVERT_TO_INTERNAL_INDEX( lIndex ) ( ( lIndex ) - INDEX_OFFSET )
/**
* @brief Max value that fits in a uint32_t type.
*/
#define mpuUINT32_MAX ( ~( ( uint32_t ) 0 ) )
/**
* @brief Check if multiplying a and b will result in overflow.
*/
#define mpuMULTIPLY_UINT32_WILL_OVERFLOW( a, b ) ( ( ( a ) > 0 ) && ( ( b ) > ( mpuUINT32_MAX / ( a ) ) ) )
/**
* @brief Get the index of a free slot in the kernel object pool.
*
* If a free slot is found, this function marks the slot as
* "not free".
*
* @return Index of a free slot is returned, if a free slot is
* found. Otherwise -1 is returned.
*/
static int32_t MPU_GetFreeIndexInKernelObjectPool( void ) PRIVILEGED_FUNCTION;
/**
* @brief Set the given index as free in the kernel object pool.
*
* @param lIndex The index to set as free.
*/
static void MPU_SetIndexFreeInKernelObjectPool( int32_t lIndex ) PRIVILEGED_FUNCTION;
/**
* @brief Get the index at which a given kernel object is stored.
*
* @param xHandle The given kernel object handle.
* @param ulKernelObjectType The kernel object type.
*
* @return Index at which the kernel object is stored if it is a valid
* handle, -1 otherwise.
*/
static int32_t MPU_GetIndexForHandle( OpaqueObjectHandle_t xHandle,
uint32_t ulKernelObjectType ) PRIVILEGED_FUNCTION;
/**
* @brief Store the given kernel object handle at the given index in
* the kernel object pool.
*
* @param lIndex Index to store the given handle at.
* @param xHandle Kernel object handle to store.
* @param pvKernelObjectData The data associated with the kernel object.
* Currently, only used for timer objects to store timer callback.
* @param ulKernelObjectType The kernel object type.
*/
static void MPU_StoreHandleAndDataAtIndex( int32_t lIndex,
OpaqueObjectHandle_t xHandle,
void * pvKernelObjectData,
uint32_t ulKernelObjectType ) PRIVILEGED_FUNCTION;
/**
* @brief Get the kernel object handle at the given index from
* the kernel object pool.
*
* @param lIndex Index at which to get the kernel object handle.
* @param ulKernelObjectType The kernel object type.
*
* @return The kernel object handle at the index.
*/
static OpaqueObjectHandle_t MPU_GetHandleAtIndex( int32_t lIndex,
uint32_t ulKernelObjectType ) PRIVILEGED_FUNCTION;
#if ( configUSE_TIMERS == 1 )
/**
* @brief The function registered as callback for all the timers.
*
* We intercept all the timer callbacks so that we can call application
* callbacks with opaque handle.
*
* @param xInternalHandle The internal timer handle.
*/
static void MPU_TimerCallback( TimerHandle_t xInternalHandle ) PRIVILEGED_FUNCTION;
#endif /* #if ( configUSE_TIMERS == 1 ) */
/*
* Wrappers to keep all the casting in one place.
*/
#define MPU_StoreQueueHandleAtIndex( lIndex, xHandle ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle ), NULL, KERNEL_OBJECT_TYPE_QUEUE )
#define MPU_GetQueueHandleAtIndex( lIndex ) ( QueueHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_QUEUE )
#if ( configUSE_QUEUE_SETS == 1 )
#define MPU_StoreQueueSetHandleAtIndex( lIndex, xHandle ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle ), NULL, KERNEL_OBJECT_TYPE_QUEUE )
#define MPU_GetQueueSetHandleAtIndex( lIndex ) ( QueueSetHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_QUEUE )
#define MPU_StoreQueueSetMemberHandleAtIndex( lIndex, xHandle ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle ), NULL, KERNEL_OBJECT_TYPE_QUEUE )
#define MPU_GetQueueSetMemberHandleAtIndex( lIndex ) ( QueueSetMemberHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_QUEUE )
#define MPU_GetIndexForQueueSetMemberHandle( xHandle ) MPU_GetIndexForHandle( ( OpaqueObjectHandle_t ) ( xHandle ), KERNEL_OBJECT_TYPE_QUEUE )
#endif
/*
* Wrappers to keep all the casting in one place for Task APIs.
*/
#define MPU_StoreTaskHandleAtIndex( lIndex, xHandle ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle ), NULL, KERNEL_OBJECT_TYPE_TASK )
#define MPU_GetTaskHandleAtIndex( lIndex ) ( TaskHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_TASK )
#define MPU_GetIndexForTaskHandle( xHandle ) MPU_GetIndexForHandle( ( OpaqueObjectHandle_t ) ( xHandle ), KERNEL_OBJECT_TYPE_TASK )
#if ( configUSE_EVENT_GROUPS == 1 )
/*
* Wrappers to keep all the casting in one place for Event Group APIs.
*/
#define MPU_StoreEventGroupHandleAtIndex( lIndex, xHandle ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle ), NULL, KERNEL_OBJECT_TYPE_EVENT_GROUP )
#define MPU_GetEventGroupHandleAtIndex( lIndex ) ( EventGroupHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_EVENT_GROUP )
#define MPU_GetIndexForEventGroupHandle( xHandle ) MPU_GetIndexForHandle( ( OpaqueObjectHandle_t ) ( xHandle ), KERNEL_OBJECT_TYPE_EVENT_GROUP )
#endif /* #if ( configUSE_EVENT_GROUPS == 1 ) */
#if ( configUSE_STREAM_BUFFERS == 1 )
/*
* Wrappers to keep all the casting in one place for Stream Buffer APIs.
*/
#define MPU_StoreStreamBufferHandleAtIndex( lIndex, xHandle ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle), NULL, KERNEL_OBJECT_TYPE_STREAM_BUFFER )
#define MPU_GetStreamBufferHandleAtIndex( lIndex ) ( StreamBufferHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_STREAM_BUFFER )
#define MPU_GetIndexForStreamBufferHandle( xHandle ) MPU_GetIndexForHandle( ( OpaqueObjectHandle_t ) ( xHandle ), KERNEL_OBJECT_TYPE_STREAM_BUFFER )
#endif /* #if ( configUSE_STREAM_BUFFERS == 1 ) */
#if ( configUSE_TIMERS == 1 )
/*
* Wrappers to keep all the casting in one place for Timer APIs.
*/
#define MPU_StoreTimerHandleAtIndex( lIndex, xHandle, pxApplicationCallback ) MPU_StoreHandleAndDataAtIndex( ( lIndex ), ( OpaqueObjectHandle_t ) ( xHandle ), ( void * ) ( pxApplicationCallback ), KERNEL_OBJECT_TYPE_TIMER )
#define MPU_GetTimerHandleAtIndex( lIndex ) ( TimerHandle_t ) MPU_GetHandleAtIndex( ( lIndex ), KERNEL_OBJECT_TYPE_TIMER )
#define MPU_GetIndexForTimerHandle( xHandle ) MPU_GetIndexForHandle( ( OpaqueObjectHandle_t ) ( xHandle ), KERNEL_OBJECT_TYPE_TIMER )
#endif /* #if ( configUSE_TIMERS == 1 ) */
/*-----------------------------------------------------------*/
/**
* @brief Kernel object pool.
*/
PRIVILEGED_DATA static KernelObject_t xKernelObjectPool[ configPROTECTED_KERNEL_OBJECT_POOL_SIZE ] = { 0 };
/*-----------------------------------------------------------*/
static int32_t MPU_GetFreeIndexInKernelObjectPool( void ) /* PRIVILEGED_FUNCTION */
{
int32_t i, lFreeIndex = -1;
/* This function is called only from resource create APIs
* which are not supposed to be called from ISRs. Therefore,
* we only need to suspend the scheduler and do not require
* critical section. */
vTaskSuspendAll();
{
for( i = 0; i < configPROTECTED_KERNEL_OBJECT_POOL_SIZE; i++ )
{
if( xKernelObjectPool[ i ].xInternalObjectHandle == NULL )
{
/* Mark this index as not free. */
xKernelObjectPool[ i ].xInternalObjectHandle = ( OpaqueObjectHandle_t ) ( ~0U );
lFreeIndex = i;
break;
}
}
}
( void ) xTaskResumeAll();
return lFreeIndex;
}
/*-----------------------------------------------------------*/
static void MPU_SetIndexFreeInKernelObjectPool( int32_t lIndex ) /* PRIVILEGED_FUNCTION */
{
configASSERT( IS_INTERNAL_INDEX_VALID( lIndex ) != pdFALSE );
taskENTER_CRITICAL();
{
xKernelObjectPool[ lIndex ].xInternalObjectHandle = NULL;
xKernelObjectPool[ lIndex ].ulKernelObjectType = KERNEL_OBJECT_TYPE_INVALID;
xKernelObjectPool[ lIndex ].pvKernelObjectData = NULL;
}
taskEXIT_CRITICAL();
}
/*-----------------------------------------------------------*/
static int32_t MPU_GetIndexForHandle( OpaqueObjectHandle_t xHandle,
uint32_t ulKernelObjectType ) /* PRIVILEGED_FUNCTION */
{
int32_t i, lIndex = -1;
configASSERT( xHandle != NULL );
for( i = 0; i < configPROTECTED_KERNEL_OBJECT_POOL_SIZE; i++ )
{
if( ( xKernelObjectPool[ i ].xInternalObjectHandle == xHandle ) &&
( xKernelObjectPool[ i ].ulKernelObjectType == ulKernelObjectType ) )
{
lIndex = i;
break;
}
}
return lIndex;
}
/*-----------------------------------------------------------*/
static void MPU_StoreHandleAndDataAtIndex( int32_t lIndex,
OpaqueObjectHandle_t xHandle,
void * pvKernelObjectData,
uint32_t ulKernelObjectType ) /* PRIVILEGED_FUNCTION */
{
configASSERT( IS_INTERNAL_INDEX_VALID( lIndex ) != pdFALSE );
xKernelObjectPool[ lIndex ].xInternalObjectHandle = xHandle;
xKernelObjectPool[ lIndex ].ulKernelObjectType = ulKernelObjectType;
xKernelObjectPool[ lIndex ].pvKernelObjectData = pvKernelObjectData;
}
/*-----------------------------------------------------------*/
static OpaqueObjectHandle_t MPU_GetHandleAtIndex( int32_t lIndex,
uint32_t ulKernelObjectType ) /* PRIVILEGED_FUNCTION */
{
OpaqueObjectHandle_t xObjectHandle = NULL;
configASSERT( IS_INTERNAL_INDEX_VALID( lIndex ) != pdFALSE );
if( xKernelObjectPool[ lIndex ].ulKernelObjectType == ulKernelObjectType )
{
xObjectHandle = xKernelObjectPool[ lIndex ].xInternalObjectHandle;
}
return xObjectHandle;
}
/*-----------------------------------------------------------*/
#if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
void vGrantAccessToKernelObject( TaskHandle_t xExternalTaskHandle,
int32_t lExternalKernelObjectHandle ) /* PRIVILEGED_FUNCTION */
{
int32_t lExternalTaskIndex;
TaskHandle_t xInternalTaskHandle = NULL;
if( IS_EXTERNAL_INDEX_VALID( lExternalKernelObjectHandle ) != pdFALSE )
{
if( xExternalTaskHandle == NULL )
{
vPortGrantAccessToKernelObject( xExternalTaskHandle, CONVERT_TO_INTERNAL_INDEX( lExternalKernelObjectHandle ) );
}
else
{
lExternalTaskIndex = ( int32_t ) xExternalTaskHandle;
if( IS_EXTERNAL_INDEX_VALID( lExternalTaskIndex ) != pdFALSE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lExternalTaskIndex ) );
if( xInternalTaskHandle != NULL )
{
vPortGrantAccessToKernelObject( xInternalTaskHandle,
CONVERT_TO_INTERNAL_INDEX( lExternalKernelObjectHandle ) );
}
}
}
}
}
#endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
/*-----------------------------------------------------------*/
#if ( configENABLE_ACCESS_CONTROL_LIST == 1 )
void vRevokeAccessToKernelObject( TaskHandle_t xExternalTaskHandle,
int32_t lExternalKernelObjectHandle ) /* PRIVILEGED_FUNCTION */
{
int32_t lExternalTaskIndex;
TaskHandle_t xInternalTaskHandle = NULL;
if( IS_EXTERNAL_INDEX_VALID( lExternalKernelObjectHandle ) != pdFALSE )
{
if( xExternalTaskHandle == NULL )
{
vPortRevokeAccessToKernelObject( xExternalTaskHandle, CONVERT_TO_INTERNAL_INDEX( lExternalKernelObjectHandle ) );
}
else
{
lExternalTaskIndex = ( int32_t ) xExternalTaskHandle;
if( IS_EXTERNAL_INDEX_VALID( lExternalTaskIndex ) != pdFALSE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lExternalTaskIndex ) );
if( xInternalTaskHandle != NULL )
{
vPortRevokeAccessToKernelObject( xInternalTaskHandle,
CONVERT_TO_INTERNAL_INDEX( lExternalKernelObjectHandle ) );
}
}
}
}
}
#endif /* #if ( configENABLE_ACCESS_CONTROL_LIST == 1 ) */
/*-----------------------------------------------------------*/
#if ( configUSE_TIMERS == 1 )
static void MPU_TimerCallback( TimerHandle_t xInternalHandle ) /* PRIVILEGED_FUNCTION */
{
int32_t i, lIndex = -1;
TimerHandle_t xExternalHandle = NULL;
TimerCallbackFunction_t pxApplicationCallBack = NULL;
/* Coming from the timer task and therefore, should be valid. */
configASSERT( xInternalHandle != NULL );
for( i = 0; i < configPROTECTED_KERNEL_OBJECT_POOL_SIZE; i++ )
{
if( ( ( TimerHandle_t ) xKernelObjectPool[ i ].xInternalObjectHandle == xInternalHandle ) &&
( xKernelObjectPool[ i ].ulKernelObjectType == KERNEL_OBJECT_TYPE_TIMER ) )
{
lIndex = i;
break;
}
}
configASSERT( lIndex != -1 );
xExternalHandle = ( TimerHandle_t ) CONVERT_TO_EXTERNAL_INDEX( lIndex );
pxApplicationCallBack = ( TimerCallbackFunction_t ) xKernelObjectPool[ lIndex ].pvKernelObjectData;
pxApplicationCallBack( xExternalHandle );
}
#endif /* #if ( configUSE_TIMERS == 1 ) */
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
/* MPU wrappers for tasks APIs. */
/*-----------------------------------------------------------*/
#if ( INCLUDE_xTaskDelayUntil == 1 )
BaseType_t MPU_xTaskDelayUntilImpl( TickType_t * const pxPreviousWakeTime,
TickType_t xTimeIncrement ) PRIVILEGED_FUNCTION;
BaseType_t MPU_xTaskDelayUntilImpl( TickType_t * const pxPreviousWakeTime,
TickType_t xTimeIncrement ) /* PRIVILEGED_FUNCTION */
{
BaseType_t xReturn = pdFAIL;
BaseType_t xIsPreviousWakeTimeAccessible = pdFALSE;
if( ( pxPreviousWakeTime != NULL ) && ( xTimeIncrement > 0U ) )
{
xIsPreviousWakeTimeAccessible = xPortIsAuthorizedToAccessBuffer( pxPreviousWakeTime,
sizeof( TickType_t ),
( tskMPU_WRITE_PERMISSION | tskMPU_READ_PERMISSION ) );
if( xIsPreviousWakeTimeAccessible == pdTRUE )
{
xReturn = xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
}
}
return xReturn;
}
#endif /* if ( INCLUDE_xTaskDelayUntil == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_xTaskAbortDelay == 1 )
BaseType_t MPU_xTaskAbortDelayImpl( TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
BaseType_t MPU_xTaskAbortDelayImpl( TaskHandle_t xTask ) /* PRIVILEGED_FUNCTION */
{
BaseType_t xReturn = pdFAIL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
TaskHandle_t xInternalTaskHandle = NULL;
int32_t lIndex;
lIndex = ( int32_t ) xTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
xReturn = xTaskAbortDelay( xInternalTaskHandle );
}
}
}
return xReturn;
}
#endif /* if ( INCLUDE_xTaskAbortDelay == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskDelay == 1 )
void MPU_vTaskDelayImpl( TickType_t xTicksToDelay ) PRIVILEGED_FUNCTION;
void MPU_vTaskDelayImpl( TickType_t xTicksToDelay ) /* PRIVILEGED_FUNCTION */
{
vTaskDelay( xTicksToDelay );
}
#endif /* if ( INCLUDE_vTaskDelay == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_uxTaskPriorityGet == 1 )
UBaseType_t MPU_uxTaskPriorityGetImpl( const TaskHandle_t pxTask ) PRIVILEGED_FUNCTION;
UBaseType_t MPU_uxTaskPriorityGetImpl( const TaskHandle_t pxTask ) /* PRIVILEGED_FUNCTION */
{
UBaseType_t uxReturn = configMAX_PRIORITIES;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
if( pxTask == NULL )
{
uxReturn = uxTaskPriorityGet( pxTask );
}
else
{
lIndex = ( int32_t ) pxTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
uxReturn = uxTaskPriorityGet( xInternalTaskHandle );
}
}
}
}
return uxReturn;
}
#endif /* if ( INCLUDE_uxTaskPriorityGet == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_eTaskGetState == 1 )
eTaskState MPU_eTaskGetStateImpl( TaskHandle_t pxTask ) PRIVILEGED_FUNCTION;
eTaskState MPU_eTaskGetStateImpl( TaskHandle_t pxTask ) /* PRIVILEGED_FUNCTION */
{
eTaskState eReturn = eInvalid;
TaskHandle_t xInternalTaskHandle = NULL;
int32_t lIndex;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
lIndex = ( int32_t ) pxTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
eReturn = eTaskGetState( xInternalTaskHandle );
}
}
}
return eReturn;
}
#endif /* if ( INCLUDE_eTaskGetState == 1 ) */
/*-----------------------------------------------------------*/
#if ( configUSE_TRACE_FACILITY == 1 )
void MPU_vTaskGetInfoImpl( TaskHandle_t xTask,
TaskStatus_t * pxTaskStatus,
BaseType_t xGetFreeStackSpace,
eTaskState eState ) PRIVILEGED_FUNCTION;
void MPU_vTaskGetInfoImpl( TaskHandle_t xTask,
TaskStatus_t * pxTaskStatus,
BaseType_t xGetFreeStackSpace,
eTaskState eState ) /* PRIVILEGED_FUNCTION */
{
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xIsTaskStatusWriteable = pdFALSE;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
xIsTaskStatusWriteable = xPortIsAuthorizedToAccessBuffer( pxTaskStatus,
sizeof( TaskStatus_t ),
tskMPU_WRITE_PERMISSION );
if( xIsTaskStatusWriteable == pdTRUE )
{
if( xTask == NULL )
{
vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
}
else
{
lIndex = ( int32_t ) xTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
vTaskGetInfo( xInternalTaskHandle, pxTaskStatus, xGetFreeStackSpace, eState );
}
}
}
}
}
}
#endif /* if ( configUSE_TRACE_FACILITY == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
TaskHandle_t MPU_xTaskGetIdleTaskHandleImpl( void ) PRIVILEGED_FUNCTION;
TaskHandle_t MPU_xTaskGetIdleTaskHandleImpl( void ) /* PRIVILEGED_FUNCTION */
{
TaskHandle_t xIdleTaskHandle = NULL;
xIdleTaskHandle = xTaskGetIdleTaskHandle();
return xIdleTaskHandle;
}
#endif /* if ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskSuspend == 1 )
void MPU_vTaskSuspendImpl( TaskHandle_t pxTaskToSuspend ) PRIVILEGED_FUNCTION;
void MPU_vTaskSuspendImpl( TaskHandle_t pxTaskToSuspend ) /* PRIVILEGED_FUNCTION */
{
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
if( pxTaskToSuspend == NULL )
{
vTaskSuspend( pxTaskToSuspend );
}
else
{
/* After the scheduler starts, only privileged tasks are allowed
* to suspend other tasks. */
#if ( INCLUDE_xTaskGetSchedulerState == 1 )
if( ( xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED ) || ( portIS_TASK_PRIVILEGED() == pdTRUE ) )
#else
if( portIS_TASK_PRIVILEGED() == pdTRUE )
#endif
{
lIndex = ( int32_t ) pxTaskToSuspend;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
vTaskSuspend( xInternalTaskHandle );
}
}
}
}
}
}
#endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
/*-----------------------------------------------------------*/
#if ( INCLUDE_vTaskSuspend == 1 )
void MPU_vTaskResumeImpl( TaskHandle_t pxTaskToResume ) PRIVILEGED_FUNCTION;
void MPU_vTaskResumeImpl( TaskHandle_t pxTaskToResume ) /* PRIVILEGED_FUNCTION */
{
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
lIndex = ( int32_t ) pxTaskToResume;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
vTaskResume( xInternalTaskHandle );
}
}
}
}
#endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
/*-----------------------------------------------------------*/
TickType_t MPU_xTaskGetTickCountImpl( void ) PRIVILEGED_FUNCTION;
TickType_t MPU_xTaskGetTickCountImpl( void ) /* PRIVILEGED_FUNCTION */
{
TickType_t xReturn;
xReturn = xTaskGetTickCount();
return xReturn;
}
/*-----------------------------------------------------------*/
UBaseType_t MPU_uxTaskGetNumberOfTasksImpl( void ) PRIVILEGED_FUNCTION;
UBaseType_t MPU_uxTaskGetNumberOfTasksImpl( void ) /* PRIVILEGED_FUNCTION */
{
UBaseType_t uxReturn;
uxReturn = uxTaskGetNumberOfTasks();
return uxReturn;
}
/*-----------------------------------------------------------*/
#if ( configGENERATE_RUN_TIME_STATS == 1 )
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetRunTimeCounterImpl( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetRunTimeCounterImpl( const TaskHandle_t xTask ) /* PRIVILEGED_FUNCTION */
{
configRUN_TIME_COUNTER_TYPE xReturn = 0;
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
if( xTask == NULL )
{
xReturn = ulTaskGetRunTimeCounter( xTask );
}
else
{
lIndex = ( int32_t ) xTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
xReturn = ulTaskGetRunTimeCounter( xInternalTaskHandle );
}
}
}
}
return xReturn;
}
#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) */
/*-----------------------------------------------------------*/
#if ( configGENERATE_RUN_TIME_STATS == 1 )
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetRunTimePercentImpl( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetRunTimePercentImpl( const TaskHandle_t xTask ) /* PRIVILEGED_FUNCTION */
{
configRUN_TIME_COUNTER_TYPE xReturn = 0;
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
if( xTask == NULL )
{
xReturn = ulTaskGetRunTimePercent( xTask );
}
else
{
lIndex = ( int32_t ) xTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
xReturn = ulTaskGetRunTimePercent( xInternalTaskHandle );
}
}
}
}
return xReturn;
}
#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) */
/*-----------------------------------------------------------*/
#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetIdleRunTimePercentImpl( void ) PRIVILEGED_FUNCTION;
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetIdleRunTimePercentImpl( void ) /* PRIVILEGED_FUNCTION */
{
configRUN_TIME_COUNTER_TYPE xReturn;
xReturn = ulTaskGetIdleRunTimePercent();
return xReturn;
}
#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
/*-----------------------------------------------------------*/
#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetIdleRunTimeCounterImpl( void ) PRIVILEGED_FUNCTION;
configRUN_TIME_COUNTER_TYPE MPU_ulTaskGetIdleRunTimeCounterImpl( void ) /* PRIVILEGED_FUNCTION */
{
configRUN_TIME_COUNTER_TYPE xReturn;
xReturn = ulTaskGetIdleRunTimeCounter();
return xReturn;
}
#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
/*-----------------------------------------------------------*/
#if ( configUSE_APPLICATION_TASK_TAG == 1 )
void MPU_vTaskSetApplicationTaskTagImpl( TaskHandle_t xTask,
TaskHookFunction_t pxTagValue ) PRIVILEGED_FUNCTION;
void MPU_vTaskSetApplicationTaskTagImpl( TaskHandle_t xTask,
TaskHookFunction_t pxTagValue ) /* PRIVILEGED_FUNCTION */
{
TaskHandle_t xInternalTaskHandle = NULL;
int32_t lIndex;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
if( xTask == NULL )
{
vTaskSetApplicationTaskTag( xTask, pxTagValue );
}
else
{
lIndex = ( int32_t ) xTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
vTaskSetApplicationTaskTag( xInternalTaskHandle, pxTagValue );
}
}
}
}
}
#endif /* if ( configUSE_APPLICATION_TASK_TAG == 1 ) */
/*-----------------------------------------------------------*/
#if ( configUSE_APPLICATION_TASK_TAG == 1 )
TaskHookFunction_t MPU_xTaskGetApplicationTaskTagImpl( TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
TaskHookFunction_t MPU_xTaskGetApplicationTaskTagImpl( TaskHandle_t xTask ) /* PRIVILEGED_FUNCTION */
{
TaskHookFunction_t xReturn = NULL;
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
if( xTask == NULL )
{
xReturn = xTaskGetApplicationTaskTag( xTask );
}
else
{
lIndex = ( int32_t ) xTask;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
xReturn = xTaskGetApplicationTaskTag( xInternalTaskHandle );
}
}
}
}
return xReturn;
}
#endif /* if ( configUSE_APPLICATION_TASK_TAG == 1 ) */
/*-----------------------------------------------------------*/
#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
void MPU_vTaskSetThreadLocalStoragePointerImpl( TaskHandle_t xTaskToSet,
BaseType_t xIndex,
void * pvValue ) PRIVILEGED_FUNCTION;
void MPU_vTaskSetThreadLocalStoragePointerImpl( TaskHandle_t xTaskToSet,
BaseType_t xIndex,
void * pvValue ) /* PRIVILEGED_FUNCTION */
{
int32_t lIndex;
TaskHandle_t xInternalTaskHandle = NULL;
BaseType_t xCallingTaskIsAuthorizedToAccessTask = pdFALSE;
if( xTaskToSet == NULL )
{
vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
}
else
{
lIndex = ( int32_t ) xTaskToSet;
if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE )
{
xCallingTaskIsAuthorizedToAccessTask = xPortIsAuthorizedToAccessKernelObject( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xCallingTaskIsAuthorizedToAccessTask == pdTRUE )
{
xInternalTaskHandle = MPU_GetTaskHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) );
if( xInternalTaskHandle != NULL )
{
vTaskSetThreadLocalStoragePointer( xInternalTaskHandle, xIndex, pvValue );
}
}