Contiki-NG
Loading...
Searching...
No Matches
tz-api.h
1/*
2 * Copyright (c) 2023, 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 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * \file
36 * TrustZone API for communication between zones.
37 * \author
38 * Niclas Finne <niclas.finne@ri.se>
39 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
40 */
41
42/** \addtogroup arm
43 * @{
44 */
45
46/**
47 * \defgroup trustzone TrustZone for Arm Cortex-M
48 *
49 * This subsystem implements TrustZone support for Arm Cortex-M
50 * processors. The archtiecture is based on dual Contiki-NG firmwares:
51 * the secure world contains an instance of Contiki-NG with reduced
52 * functionality, and the normal world contains an instance with
53 * regular functionality. When programming an IoT device, the hex
54 * files with the two firmwares are merged into a single hex file,
55 * which is flashed to the device.
56 *
57 * Both worlds can access core system functionality such as processes,
58 * timers, and library functions. The normal world is expected to
59 * contain applications and networking functionality. By contrast, the
60 * secure world will contain secret information and functionality for
61 * monitoring the normal world. Hardware peripherals can be configured
62 * to be accessible in either of the worlds.
63 *
64 * Currently, the only supported Contiki-NG platform is the nRF5340
65 * development kit, which is equipped with two different Arm
66 * Cortex-M33 processors.
67 *
68 * @{
69 */
70
71#ifndef TZ_API_H
72#define TZ_API_H
73
74#include <stdbool.h>
75#include <stdlib.h>
76
77#ifdef TRUSTZONE_SECURE
78
79/**
80 * The CC_TRUSTZONE_SECURE_CALL marks a function in the secure world
81 * as being possible to call from the normal world. When executing
82 * such a function, the processor will be in secure state.
83 */
84#define CC_TRUSTZONE_SECURE_CALL __attribute__((cmse_nonsecure_entry))
85
86/**
87 * The CC_TRUSTZONE_NONSECURE_CALL marks a function in the normal
88 * world as being possible to call from the secure world. When
89 * executing such a function, the processor will be in non-secure
90 * state.
91 */
92#define CC_TRUSTZONE_NONSECURE_CALL __attribute__((cmse_nonsecure_call))
93
94/**
95 * The trustzone_init_event is posted to all automatically started
96 * processes when both the secure world and the normal world have
97 * finished their initialization.
98 *
99 * This allows user processes to wait until they can begin their
100 * execution of tasks that may depend on TrustZone-specific
101 * functionality.
102 */
103extern process_event_t trustzone_init_event;
104
105/**
106 * Linker symbols.
107 */
108
109/* End of the text region. */
110extern uint32_t __etext;
111
112/* Start of the Secure Gateway region. */
113extern uint32_t __sg_start;
114
115/* End of the Secure Gateway region. */
116extern uint32_t __sg_end;
117
118/* Secure Gateway region size, aligned to the next 32 byte boundary. */
119extern uint32_t __nsc_size;
120
121/**
122 * \brief Pend an NS-targeted IRQ to wake the normal world.
123 *
124 * Called from secure context (including secure ISRs) by
125 * tz_api_request_ns_poll after setting ns_poll_pending.
126 * Implemented by the platform; weakly defined as a no-op in
127 * tz-api.c so platforms without a wake mechanism still link.
128 */
129void tz_arch_signal_ns(void);
130/******************************************************************************/
131
132#else /* TRUSTZONE_SECURE */
133
134#define CC_TRUSTZONE_SECURE_CALL
135#define CC_TRUSTZONE_NONSECURE_CALL
136
137#endif /* TRUSTZONE_SECURE */
138
139typedef bool (*ns_poll_t)(void) CC_TRUSTZONE_NONSECURE_CALL;
140
141struct tz_api {
142 ns_poll_t request_poll;
143};
144
145/**
146 * \brief Initialize the TrustZone API.
147 * \param apip A pointer to a tz_api structure.
148 * \retval false Error (apip pointed to invalid memory,
149 * or the API has been initialized already.)
150 * \retval true Success.
151 *
152 * \note Must be called from the normal world before any
153 * normal-world scheduling begins, since the secure side
154 * posts trustzone_init_event to autostart processes
155 * from inside this call.
156 */
157bool tz_api_init(struct tz_api *apip);
158
159/**
160 * \brief Poll the secure world and process all events in the queue.
161 * \retval true If the secure world has more work to do — either residual
162 * events in the queue, or a deferred poll request raised by
163 * the secure side during the call. The NS caller should
164 * reschedule itself.
165 * \retval false If the secure world has nothing more to do, or the call
166 * was rejected (see note).
167 *
168 * \note Must be called only from NS thread mode. The function
169 * runs process_run() and is not reentrant; calls from a
170 * handler context (NS interrupt or, defensively, a
171 * secure ISR) are rejected and return false without
172 * running events.
173 */
174bool tz_api_poll(void);
175
176/**
177 * \brief Print the specified message via the secure world.
178 * \param text A pointer to the message text in non-secure memory.
179 * \param len The length of the message in bytes.
180 */
181void tz_api_println(const char *text, size_t len);
182
183/**
184 * \brief Mark the normal world as needing another poll cycle.
185 *
186 * Called from the secure world (e.g. via the Contiki-NG
187 * process module's PROCESS_CONF_POLL_REQUESTED hook)
188 * when secure-side state changes that the normal world
189 * needs to react to. The flag is observed by the next
190 * tz_api_poll(), which then returns true so the NS
191 * caller reschedules itself.
192 *
193 * This is a secure-internal helper, not a secure
194 * gateway entry, and must not be called from the
195 * normal world.
196 */
197bool tz_api_request_ns_poll(void);
198
199#endif /* !TZ_API_H */
200/** @} */
201/** @} */
void tz_api_println(const char *text, size_t len)
Print the specified message via the secure world.
Definition tz-api.c:136
bool tz_api_request_ns_poll(void)
Mark the normal world as needing another poll cycle.
Definition tz-api.c:155
bool tz_api_init(struct tz_api *apip)
Initialize the TrustZone API.
Definition tz-api.c:59
bool tz_api_poll(void)
Poll the secure world and process all events in the queue.
Definition tz-api.c:88