]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ARMv8M/tz_demo/tz_demo.c
7d42da48a5ebb0340f61f051ccd57cf43f5c1c60
[freertos] / FreeRTOS / Demo / Common / ARMv8M / tz_demo / tz_demo.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* FreeRTOS includes. */\r
29 #include "FreeRTOS.h"\r
30 #include "task.h"\r
31 \r
32 /* Non-Secure callable functions. */\r
33 #include "nsc_functions.h"\r
34 \r
35 /**\r
36  * @brief Counter incremented in the callback which is called from the secure\r
37  * side.\r
38  *\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
44  */\r
45 static uint32_t ulNonSecureCounter[8] __attribute__( ( aligned( 32 ) ) ) = { 0 };\r
46 /*-----------------------------------------------------------*/\r
47 \r
48 /**\r
49  * @brief Creates all the tasks for TZ demo.\r
50  */\r
51 void vStartTZDemo( void );\r
52 \r
53 /**\r
54  * @brief Increments the ulNonSecureCounter.\r
55  *\r
56  * This function is called from the secure side.\r
57  */\r
58 static void prvCallback( void );\r
59 \r
60 /**\r
61  * @brief Implements the task which calls the functions exported from the secure\r
62  * side.\r
63  *\r
64  * @param pvParameters[in] Parameters as passed during task creation.\r
65  */\r
66 static void prvSecureCallingTask( void * pvParameters );\r
67 /*-----------------------------------------------------------*/\r
68 \r
69 void vStartTZDemo( void )\r
70 {\r
71 static StackType_t xSecureCallingTaskStack[ configMINIMAL_STACK_SIZE ] __attribute__( ( aligned( 32 ) ) );\r
72 TaskParameters_t xSecureCallingTaskParameters =\r
73 {\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
80         .xRegions               =       {\r
81                                                         { ulNonSecureCounter,   32,     tskMPU_REGION_READ_WRITE | tskMPU_REGION_EXECUTE_NEVER  },\r
82                                                         { 0,                                    0,      0                                                                                                               },\r
83                                                         { 0,                                    0,      0                                                                                                               },\r
84                                                 }\r
85 };\r
86 \r
87         /* Create an unprivileged task which calls secure functions. */\r
88         xTaskCreateRestricted( &( xSecureCallingTaskParameters ), NULL );\r
89 }\r
90 /*-----------------------------------------------------------*/\r
91 \r
92 static void prvCallback( void )\r
93 {\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
98 }\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 static void prvSecureCallingTask( void * pvParameters )\r
102 {\r
103 uint32_t ulLastSecureCounter = 0, ulLastNonSecureCounter = 0;\r
104 uint32_t ulCurrentSecureCounter = 0;\r
105 \r
106         /* This task calls secure side functions. So allocate a secure context for\r
107          * it. */\r
108         portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );\r
109 \r
110         for( ; ; )\r
111         {\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
118                  */\r
119                 ulCurrentSecureCounter = NSCFunction( prvCallback );\r
120 \r
121                 /* Make sure that both the counters are incremented. */\r
122                 configASSERT( ulCurrentSecureCounter == ulLastSecureCounter + 1 );\r
123                 configASSERT( ulNonSecureCounter[ 0 ] == ulLastNonSecureCounter + 1 );\r
124 \r
125                 /* Update the last values for both the counters. */\r
126                 ulLastSecureCounter = ulCurrentSecureCounter;\r
127                 ulLastNonSecureCounter = ulNonSecureCounter[ 0 ];\r
128 \r
129                 /* Wait for a second. */\r
130                 vTaskDelay( pdMS_TO_TICKS( 1000 ) );\r
131         }\r
132 }\r
133 /*-----------------------------------------------------------*/\r