Contiki-NG
Loading...
Searching...
No Matches
tz-normal.c
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 setup for normal world.
37 * \author
38 * Niclas Finne <niclas.finne@ri.se>
39 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
40 */
41
42#include "contiki.h"
43#include "sys/platform.h"
44#include "trustzone/tz-api.h"
46
47/*---------------------------------------------------------------------------*/
48#include "sys/log.h"
49#define LOG_MODULE "TZNormalWorld"
50#define LOG_LEVEL LOG_LEVEL_INFO
51/*---------------------------------------------------------------------------*/
52static volatile bool is_poll_requested;
53
54PROCESS(tz_normal_process, "TZ normal process");
55/*---------------------------------------------------------------------------*/
56bool
57tz_normal_request_poll(void)
58{
59 is_poll_requested = true;
60 process_poll(&tz_normal_process);
61 return true;
62}
63/*---------------------------------------------------------------------------*/
64__attribute__((weak)) void
65tz_arch_init_ns_signal(void)
66{
67}
68/*---------------------------------------------------------------------------*/
69static void
70init_tz_api(void)
71{
72 struct tz_api tz_api = {0};
73
74 tz_api.request_poll = tz_normal_request_poll;
75 bool result = tz_api_init(&tz_api);
76 LOG_INFO("Initialize TrustZone API: %s\n",
77 result ? "SUCCESS" : "FAILURE");
78
79 tz_arch_init_ns_signal();
80}
81/*---------------------------------------------------------------------------*/
82PROCESS_THREAD(tz_normal_process, ev, data)
83{
85
86 while(true) {
87 PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_POLL);
88 if(is_poll_requested) {
89 is_poll_requested = false;
90 LOG_DBG("> Poll secure world\n");
91 if(tz_api_poll()) {
92 tz_normal_request_poll();
93 }
94 LOG_DBG("< Poll secure world %s!\n",
95 is_poll_requested ? "waiting" : "done");
96 }
97 }
98
100}
101/*---------------------------------------------------------------------------*/
102void
104{
105 /*
106 * Start tz_normal_process before init_tz_api so the EGU doorbell
107 * armed by tz_arch_init_ns_signal() can validly poll a process in
108 * PROCESS_STATE_RUNNING if the IRQ fires immediately on enable.
109 * Then issue one explicit poll to drain trustzone_init_event and
110 * any other secure-side events queued during init -- process_post
111 * does not fire PROCESS_POLL_REQUESTED, so without this kick the
112 * init events would sit unread until something else woke NS.
113 */
114 process_start(&tz_normal_process, NULL);
115 init_tz_api();
116 tz_normal_request_poll();
117
118 while(1) {
119 process_num_events_t r;
120 do {
121 r = process_run();
123 } while(r > 0);
124
126 }
127}
128/*---------------------------------------------------------------------------*/
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition watchdog.c:85
void platform_idle()
The platform's idle/sleep function.
Definition platform.c:181
void platform_main_loop(void)
The platform's main loop, if provided.
Definition tz-normal.c:103
process_num_events_t process_run(void)
Run the system once - call poll handlers and process one event.
Definition process.c:305
#define PROCESS(name, strname)
Declare a process.
Definition process.h:309
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:122
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition process.h:159
#define PROCESS_END()
Define the end of a process.
Definition process.h:133
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:275
void process_poll(struct process *p)
Request a process to be polled.
Definition process.c:366
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
Header file for the logging system.
Header file for the Contiki-NG main routine.
Normal-world TrustZone API hooks.