2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* FreeRTOS includes. */
\r
29 #include "FreeRTOS.h"
\r
32 /* Non-Secure callable functions. */
\r
33 #include "nsc_functions.h"
\r
36 * @brief Counter incremented in the callback which is called from the secure
\r
39 * The size of an MPU region must be a multiple of 32 bytes. Therefore we need
\r
40 * to declare an array of size 8 to ensure that the total size is 32 bytes -
\r
41 * even though we only need 4 bytes. If we do not do that, anything placed after
\r
42 * 4 bytes and upto 32 bytes will also fall in the same MPU region and the task
\r
43 * having access to ulNonSecureCounter will also have access to all those items.
\r
45 static uint32_t ulNonSecureCounter[8] __attribute__( ( aligned( 32 ) ) ) = { 0 };
\r
46 /*-----------------------------------------------------------*/
\r
49 * @brief Creates all the tasks for TZ demo.
\r
51 void vStartTZDemo( void );
\r
54 * @brief Increments the ulNonSecureCounter.
\r
56 * This function is called from the secure side.
\r
58 static void prvCallback( void );
\r
61 * @brief Implements the task which calls the functions exported from the secure
\r
64 * @param pvParameters[in] Parameters as passed during task creation.
\r
66 static void prvSecureCallingTask( void * pvParameters );
\r
67 /*-----------------------------------------------------------*/
\r
69 void vStartTZDemo( void )
\r
71 static StackType_t xSecureCallingTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) );
\r
72 TaskParameters_t xSecureCallingTaskParameters =
\r
74 .pvTaskCode = prvSecureCallingTask,
\r
75 .pcName = "SecCalling",
\r
76 .usStackDepth = configMINIMAL_STACK_SIZE,
\r
77 .pvParameters = NULL,
\r
78 .uxPriority = tskIDLE_PRIORITY,
\r
79 .puxStackBuffer = xSecureCallingTaskStack,
\r
81 { ulNonSecureCounter, 32, tskMPU_REGION_READ_WRITE | tskMPU_REGION_EXECUTE_NEVER },
\r
87 /* Create an unprivileged task which calls secure functions. */
\r
88 xTaskCreateRestricted( &( xSecureCallingTaskParameters ), NULL );
\r
90 /*-----------------------------------------------------------*/
\r
92 static void prvCallback( void )
\r
94 /* This function is called from the secure side. Just increment the counter
\r
95 * here. The check that this counter keeps incrementing is performed in the
\r
96 * prvSecureCallingTask. */
\r
97 ulNonSecureCounter[ 0 ] += 1;
\r
99 /*-----------------------------------------------------------*/
\r
101 static void prvSecureCallingTask( void * pvParameters )
\r
103 uint32_t ulLastSecureCounter = 0, ulLastNonSecureCounter = 0;
\r
104 uint32_t ulCurrentSecureCounter = 0;
\r
106 /* This task calls secure side functions. So allocate a secure context for
\r
108 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
\r
112 /* Call the secure side function. It does two things:
\r
113 * - It calls the supplied function (prvCallback) which in turn
\r
114 * increments the non-secure counter.
\r
115 * - It increments the secure counter and returns the incremented value.
\r
116 * Therefore at the end of this function call both the secure and
\r
117 * non-secure counters must have been incremented.
\r
119 ulCurrentSecureCounter = NSCFunction( prvCallback );
\r
121 /* Make sure that both the counters are incremented. */
\r
122 configASSERT( ulCurrentSecureCounter == ulLastSecureCounter + 1 );
\r
123 configASSERT( ulNonSecureCounter[ 0 ] == ulLastNonSecureCounter + 1 );
\r
125 /* Update the last values for both the counters. */
\r
126 ulLastSecureCounter = ulCurrentSecureCounter;
\r
127 ulLastNonSecureCounter = ulNonSecureCounter[ 0 ];
\r
129 /* Wait for a second. */
\r
130 vTaskDelay( pdMS_TO_TICKS( 1000 ) );
\r
133 /*-----------------------------------------------------------*/
\r