Contiki-NG
Loading...
Searching...
No Matches
tz-secure.c
1/*
2 * Copyright (c) 2022 RISE Research Institutes of Sweden
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Authors: John Kanwar <johnkanwar@hotmail.com>
34 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
35 * Niclas Finne <niclas.finne@ri.se>
36 */
37
38#include "contiki.h"
39#include "region_defs.h"
40#include "spu.h"
41#include "tz-api.h"
42#include "tz-fault.h"
43#include "tz-target-cfg.h"
44
45#include <arm_cmse.h>
46
47/*---------------------------------------------------------------------------*/
48#include "sys/log.h"
49#define LOG_MODULE "TZSecureWorld"
50#define LOG_LEVEL LOG_LEVEL_INFO
51/*---------------------------------------------------------------------------*/
52typedef void __attribute__((cmse_nonsecure_call)) (*tz_ns_func_ptr_t)(void);
53#define TZ_NONSECURE_FUNC_PTR_DECLARE(fptr) tz_ns_func_ptr_t fptr
54#define TZ_NONSECURE_FUNC_PTR_CREATE(fptr) \
55 ((tz_ns_func_ptr_t)(cmse_nsfptr_create(fptr)))
56/*---------------------------------------------------------------------------*/
57static uintptr_t *
58setup(void)
59{
60 LOG_INFO("Initializing TrustZone\n");
61
62 /* SPM example */
63 LOG_INFO("Enabling fault handlers: ");
64 if(enable_fault_handlers() == TFM_PLAT_ERR_SUCCESS) {
65 LOG_INFO_("success\n");
66 } else {
67 LOG_INFO_("failure\n");
68 return NULL;
69 }
70
71 /*
72 * Set flash and RAM secure.
73 * Set non-secure partition non-secure for both flash and RAM.
74 * Set all peripherals non-secure.
75 */
76 sau_and_idau_cfg();
77 non_secure_configuration();
78
79 /* Verify that the start of the vector table of the non-secure world
80 now has non-secure permissions. */
81 void *ptr = (void *)NS_CODE_START;
82 if(cmse_check_address_range(ptr, sizeof(ptr), CMSE_NONSECURE) == ptr) {
83 /* Check succeeded. */
84 LOG_INFO("Non-secure image has correct permissions\n");
85 } else {
86 LOG_ERR("Non-secure image has incorrect permissions\n");
87 return NULL;
88 }
89
90 enum tfm_plat_err_t tfm_err = nvic_interrupt_target_state_cfg();
91 if(tfm_err != TFM_PLAT_ERR_SUCCESS) {
92 LOG_DBG("Interrupt state: 0x%x\n", tfm_err);
93 }
94
95 uintptr_t *vtor_ns = (uintptr_t *)NS_CODE_START;
96 LOG_DBG("NS image at %p\n", vtor_ns);
97 LOG_DBG("NS main stack pointer at 0x%"PRIxPTR"\n", vtor_ns[0]);
98 LOG_DBG("NS reset vector at 0x%"PRIxPTR"\n", vtor_ns[1]);
99
100 /*
101 * Initialize the Non-Secure Callable (NSC) region in order to enable
102 * function calls from the non-secure world to the secure world.
103 */
104 LOG_DBG("Secure gateway region: %p - %p\n", &__sg_start, &__sg_end);
105 LOG_DBG("NSC size %p\n", &__nsc_size);
106 spu_regions_flash_config_non_secure_callable((uint32_t)&__sg_start,
107 (uint32_t)&__sg_end - 1);
108
109 LOG_DBG("__ARM_FEATURE_CMSE = %d\n", __ARM_FEATURE_CMSE);
110
111 /* Configure non-secure stack */
112 tz_nonsecure_setup_conf_t spm_ns_conf = {
113 .vtor_ns = (uintptr_t)vtor_ns,
114 .msp_ns = vtor_ns[0],
115 .psp_ns = vtor_ns[0], /* Was: 0 */
116 .control_ns.npriv = 0, /* Privileged mode */
117 .control_ns.spsel = 0, /* Use MSP in Thread mode */
118 };
119
120 tz_nonsecure_state_setup(&spm_ns_conf);
121
122 return vtor_ns;
123}
124/*---------------------------------------------------------------------------*/
125void
127{
128 tz_fault_init();
129
130 /* Process all events before switching to non-secure */
131 process_num_events_t r;
132 do {
133 r = process_run();
135 } while(r > 0);
136
137 uintptr_t *vtor_ns = setup();
138 if(vtor_ns == NULL) {
139 LOG_ERR("Not jumping to the normal world due to initialization error\n");
140 return;
141 }
142
143 TZ_NONSECURE_FUNC_PTR_DECLARE(reset_ns);
144 reset_ns = TZ_NONSECURE_FUNC_PTR_CREATE(vtor_ns[1]);
145 if(!cmse_is_nsfptr(reset_ns)) {
146 LOG_ERR("Invalid non-secure pointer type\n");
147 return;
148 }
149
150 LOG_INFO("Preparing to jump to the normal world\n");
151 spu_periph_config_uarte();
152
153 __DSB();
154 __ISB();
155 reset_ns();
156}
157/*---------------------------------------------------------------------------*/
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition watchdog.c:85
void platform_main_loop(void)
The platform's main loop, if provided.
Definition tz-secure.c:126
process_num_events_t process_run(void)
Run the system once - call poll handlers and process one event.
Definition process.c:305
Header file for the logging system.
A convenient struct to include all required Non-Secure state configuration.
nRF5340 target configuration header
tfm_plat_err_t
TFM error codes.