Skip to content

Commit 1c57d75

Browse files
author
wayne ren
committed
example: reconstruct the kernel_secure example
1 parent d7da45b commit 1c57d75

File tree

7 files changed

+136
-59
lines changed

7 files changed

+136
-59
lines changed

example/freertos/kernel_secure/background_container.c

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@
4242
* ### Extra Required Peripherals
4343
*
4444
* ### Design Concept
45-
* This example is designed to test secureshield runtime in embARC.
45+
* This example is designed to show how FreeRTOS is integrated with SecureShield.
46+
* Here, trap_s is used to trigger a task context switch request. So application
47+
* should not use trap exception.
48+
*
49+
* As container switch is not with task switch. So during a container call, task scheduler
50+
* should be suspended to avoid a task context.
4651
*
4752
* ### Usage Manual
4853
* Here we take EMSK 2.3 EM7D for example, you can run the program using Metaware toolset.
@@ -67,19 +72,14 @@
6772
#include "embARC.h"
6873
#include "embARC_debug.h"
6974

75+
#include "background_container.h"
7076
#include "container1.h"
7177
#include "container2.h"
7278

7379
static void task1(void * par);
7480
static void task2(void * par);
75-
static void trap_exception(void *p_excinf);
76-
static void soft_interrupt(void *p_excinf);
77-
78-
#ifdef INTNO_I2C0
79-
#define INTNO_SWI INTNO_I2C0
80-
#else
81-
#define INTNO_SWI 20
82-
#endif
81+
static void interrupt_high_pri(void *p_excinf);
82+
static void interrupt_low_pri(void *p_excinf);
8383

8484
/**
8585
* \var task1_handle
@@ -97,9 +97,8 @@ static TaskHandle_t task2_handle = NULL;
9797
static volatile unsigned int start = 0;
9898
static unsigned int perf_id = 0xFF;
9999

100+
static unsigned int t_int_t2;
100101
static unsigned int t_t2_t1;
101-
static unsigned int t_int_t1;
102-
static unsigned int t_t1_t2;
103102
static unsigned int t_t1_int;
104103
static unsigned int t_int_nest;
105104
static unsigned int t_nest_int;
@@ -165,13 +164,16 @@ int main(void)
165164
}
166165

167166

168-
int_handler_install(INTNO_SWI, (EXC_HANDLER)soft_interrupt);
169-
int_pri_set(INTNO_SWI, INT_PRI_MIN);
170-
int_enable(INTNO_SWI);
167+
int_handler_install(INTNO_LOW_PRI, (INT_HANDLER)interrupt_low_pri);
168+
int_pri_set(INTNO_LOW_PRI, INT_PRI_MAX);
169+
int_enable(INTNO_LOW_PRI);
171170

172-
exc_handler_install(EXC_NO_TRAP, trap_exception); /*!< install the exception handler */
171+
int_handler_install(INTNO_HIGH_PRI, (INT_HANDLER)interrupt_high_pri);
172+
int_pri_set(INTNO_HIGH_PRI, INT_PRI_MIN);
173+
int_enable(INTNO_HIGH_PRI);
173174

174175

176+
vTaskSuspendAll();
175177
if (xTaskCreate(task1, "task1", 256, (void *)1, configMAX_PRIORITIES-2, &task1_handle)
176178
!= pdPASS) { /*!< FreeRTOS xTaskCreate() API function */
177179
EMBARC_PRINTF("create task1 error\r\n");
@@ -182,8 +184,7 @@ int main(void)
182184
EMBARC_PRINTF("create task2 error\r\n");
183185
return -1;
184186
}
185-
186-
vTaskSuspend(NULL);
187+
xTaskResumeAll();
187188

188189
return 0;
189190
}
@@ -196,26 +197,25 @@ int main(void)
196197
*/
197198
static void task1(void * par)
198199
{
199-
TickType_t xLastExecutionTime;
200-
xLastExecutionTime = xTaskGetTickCount(); /*!< initialize current tick */
201-
int i = 0;
202-
203200
while(1) {
204-
t_t2_t1 = perf_end();
205-
vTaskDelayUntil( &xLastExecutionTime, 500); /*!< This task should execute exactly every 1 second. */
206201
perf_start();
207-
_arc_aux_write(AUX_IRQ_HINT, INTNO_SWI); /*!< activate soft_interrupt */
208-
t_int_t1 = perf_end();
202+
_arc_aux_write(AUX_IRQ_HINT, INTNO_LOW_PRI); /*!< activate low priority interrupt */
203+
t_t2_t1 = perf_end();
204+
205+
EMBARC_PRINTF("The performance data is:\r\n");
206+
EMBARC_PRINTF("\ttask2->task1:%d cycles\r\n",t_t2_t1);
207+
EMBARC_PRINTF("\ttask1->int:%d cycles\r\n", t_t1_int);
208+
EMBARC_PRINTF("\tint->nest int:%d cycles\r\n", t_int_nest);
209+
EMBARC_PRINTF("\tnest int->int:%d cycles\r\n", t_nest_int);
210+
EMBARC_PRINTF("\tint->task2:%d cycles\r\n", t_int_t2);
211+
EMBARC_PRINTF("\r\n");
209212

210-
EMBARC_PRINTF("Rounds: %d\r\n", i++);
211213
EMBARC_PRINTF("Task1 is running and makes a container call...\r\n");
212214
vTaskSuspendAll();
213215
container_call(container2, trusted_ops);
214216
xTaskResumeAll();
215217

216-
perf_start();
217-
vTaskResume(task2_handle);
218-
218+
vTaskDelay(500);
219219
}
220220

221221
}
@@ -227,14 +227,16 @@ static void task1(void * par)
227227
*/
228228
static void task2(void * par)
229229
{
230+
int i = 0;
230231
uint32_t ret = 0;
231232

232233
perf_init(TIMER_1);
233234
while(1) {
234235
perf_start();
235236
vTaskSuspend(NULL); /*!< suspend task2 */
236-
t_t1_t2 = perf_end();
237+
t_int_t2 = perf_end();
237238

239+
EMBARC_PRINTF("\r\nRounds: %d\r\n", i++);
238240
EMBARC_PRINTF("\r\nTask2 is running and makes a container call...\r\n");
239241
vTaskSuspendAll();
240242
ret = container_call(container1, operate_secret, "embarc", GET_SECRET, public_data);
@@ -245,41 +247,35 @@ static void task2(void * par)
245247
EMBARC_PRINTF("the secret is:%s\r\n", public_data);
246248
memset(public_data, 0, SECRET_LEN);
247249
}
248-
249-
EMBARC_PRINTF("The performance data is:\r\n");
250-
EMBARC_PRINTF("\ttask2->task1:%d cycles\r\n",t_t2_t1);
251-
EMBARC_PRINTF("\ttask1->int:%d cycles\r\n", t_t1_int);
252-
EMBARC_PRINTF("\tint->nest int:%d cycles\r\n", t_int_nest);
253-
EMBARC_PRINTF("\tnest int->int:%d cycles\r\n", t_nest_int);
254-
EMBARC_PRINTF("\tint->task1:%d cycles\r\n", t_int_t1);
255-
EMBARC_PRINTF("\ttask1->task2:%d cycles\r\n",t_t1_t2);
256-
EMBARC_PRINTF("\r\n");
257250
}
258251
}
259252

260253
/**
261-
* \brief trap exception
254+
* \brief high priority interrupt
262255
* \details Call xTaskResumeFromISR() to resume task2 that can be called from within ISR.
263256
* If resuming the task2 should result in a context switch, call vPortYieldFromIsr() to generate task switch request.
264257
* \param[in] *p_excinf
265258
*/
266-
static void trap_exception(void *p_excinf)
259+
static void interrupt_high_pri(void *p_excinf)
267260
{
268261
// show exception frame
269262
t_int_nest = perf_end();
263+
if (xTaskResumeFromISR(task2_handle) == pdTRUE) {
264+
portYIELD_FROM_ISR(); /* need to make task switch */
265+
}
270266
perf_start();
271267
}
272268

273269
/**
274-
* \brief soft interrupt
275-
* \details Call trap_s instruction to raise the exception.
270+
* \brief low priority interrupt
271+
* \details write AUX_IRQ_HINT to raise higher priority interrtupt.
276272
* \param[in] *p_excinf
277273
*/
278-
static void soft_interrupt(void *p_exinf)
274+
static void interrupt_low_pri(void *p_exinf)
279275
{
280276
t_t1_int = perf_end();
281277
perf_start();
282-
Asm("trap_s 1");
278+
_arc_aux_write(AUX_IRQ_HINT, INTNO_HIGH_PRI); /*!< activate high priority interrupt */
283279
t_nest_int = perf_end();
284280
perf_start();
285281
}
Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
1+
/* ------------------------------------------
2+
* Copyright (c) 2017, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
* \version 2017.03
30+
* \date 2016-11-24
31+
* \author Wayne Ren([email protected])
32+
--------------------------------------------- */
33+
/**
34+
* \file
35+
* \ingroup EMBARC_APP_FREERTOS_KERNEL_SECURE
36+
* \brief background container head file
37+
*/
38+
139
#ifndef BACKGROUND_CONTAINER_H
240
#define BACKGROUND_CONTAINER_H
341

4-
5-
#ifdef INTNO_I2C0
6-
#define INTNO_SWI INTNO_I2C0
7-
#else
8-
#define INTNO_SWI 20
9-
#endif
42+
#define INTNO_LOW_PRI 18
43+
#define INTNO_HIGH_PRI 19
1044

1145
extern void default_interrupt_handler(void *p_exinf);
1246

13-
#endif
47+
#endif /* BACKGROUND_CONTAINER_H */

example/freertos/kernel_secure/container1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434
/**
3535
* \file
36-
* \ingroup EMBARC_APP_BAREMETAL_SECURESHIELD_SECRET2
37-
* \brief secureshield secret2 example container1 source file
36+
* \ingroup EMBARC_APP_FREERTOS_KERNEL_SECURE
37+
* \brief freertos kernel secure example container1 source file
3838
*/
3939

4040
#include <string.h>

example/freertos/kernel_secure/container1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
* \date 2016-11-21
3131
* \author Wayne Ren([email protected])
3232
--------------------------------------------- */
33+
/**
34+
* \file
35+
* \ingroup EMBARC_APP_FREERTOS_KERNEL_SECURE
36+
* \brief freertos kernel secure example container1 head file
37+
*/
3338
#ifndef CONTAINER1_H
3439
#define CONTAINER1_H
3540

example/freertos/kernel_secure/container2.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
* \date 2016-11-21
3131
* \author Wayne Ren([email protected])
3232
--------------------------------------------- */
33-
3433
/**
3534
* \file
36-
* \ingroup EMBARC_APP_BAREMETAL_SECURESHIELD_SECRET2
37-
* \brief secureshield secret2 example container2 source file
35+
* \ingroup EMBARC_APP_FREERTOS_KERNEL_SECURE
36+
* \brief freertos kernel secure example container2 source file
3837
*/
3938

40-
4139
#include "embARC.h"
4240
#include "embARC_debug.h"
4341
#include "embARC_assert.h"

example/freertos/kernel_secure/container2.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
* \date 2016-11-21
3131
* \author Wayne Ren([email protected])
3232
--------------------------------------------- */
33+
/**
34+
* \file
35+
* \ingroup EMBARC_APP_FREERTOS_KERNEL_SECURE
36+
* \brief freertos kernel secure example container2 head file
37+
*/
3338
#ifndef CONTAINER2_H
3439
#define CONTAINER2_H
3540

example/freertos/kernel_secure/container_cfg.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/*------------------------------------------
2+
* Copyright (c) 2017, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
* \version 2017.03
30+
* \date 2016-11-21
31+
* \author Wayne Ren([email protected])
32+
--------------------------------------------- */
33+
/**
34+
* \file
35+
* \ingroup EMBARC_APP_FREERTOS_KERNEL_SECURE
36+
* \brief freertos kernel secure example container configuration file
37+
*/
38+
139
#include "embARC.h"
240
#include "container1.h"
341
#include "container2.h"
@@ -33,7 +71,8 @@ static CONTAINER_AC_TABLE g_main_container_act[] = {
3371
{(void *)0x100, 0x6, SECURESHIELD_AC_AUX},
3472
{(void *)0x75, 0x1, SECURESHIELD_AC_AUX},
3573
#endif
36-
{default_interrupt_handler, INTNO_SWI, SECURESHIELD_AC_IRQ},
74+
{default_interrupt_handler, INTNO_LOW_PRI, SECURESHIELD_AC_IRQ},
75+
{default_interrupt_handler, INTNO_HIGH_PRI, SECURESHIELD_AC_IRQ},
3776
{default_interrupt_handler, INTNO_TIMER0, SECURESHIELD_AC_IRQ}
3877
};
3978

0 commit comments

Comments
 (0)