Contiki-NG
Loading...
Searching...
No Matches
tz-api.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 * Authors: Niclas Finne <niclas.finne@ri.se>
36 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
37 */
38
39#include "contiki.h"
40#include "sys/autostart.h"
41#include "tz-api.h"
42
43#include <arm_cmse.h>
44#include <stdarg.h>
45#include <string.h>
46
47static struct tz_api tz_api;
48static bool initialized;
49static volatile unsigned poll_wait_counter;
50
51/*---------------------------------------------------------------------------*/
52#include "sys/log.h"
53#define LOG_MODULE "TZAPI"
54#define LOG_LEVEL LOG_LEVEL_INFO
55/*---------------------------------------------------------------------------*/
56process_event_t trustzone_init_event;
57/*---------------------------------------------------------------------------*/
58CC_TRUSTZONE_SECURE_CALL bool
59tz_api_init(struct tz_api *apip)
60{
61 if(initialized || apip == NULL) {
62 return false;
63 }
64
65 trustzone_init_event = process_alloc_event();
66
67 apip = cmse_check_address_range(apip, sizeof(*apip), CMSE_NONSECURE);
68 if(apip == NULL || apip->request_poll == NULL) {
69 return false;
70 }
71
72 if(cmse_check_address_range(apip->request_poll, sizeof(apip->request_poll),
73 CMSE_NONSECURE) == NULL) {
74 return false;
75 }
76
77 memcpy(&tz_api, apip, sizeof(tz_api));
78
79 initialized = true;
80
81 for(size_t i = 0; autostart_processes[i] != NULL; i++) {
82 process_post(autostart_processes[i], trustzone_init_event, NULL);
83 }
85
86 return true;
87}
88/*---------------------------------------------------------------------------*/
89CC_TRUSTZONE_SECURE_CALL bool
91{
92 static bool is_poll_running;
93
94 if(!initialized || is_poll_running) {
95 return false;
96 }
97 is_poll_running = true;
98
99 process_num_events_t event_count = process_nevents();
100
101 if(event_count > 0) {
102 LOG_DBG("Processing %u event%s at %lu\n", event_count,
103 event_count == 1 ? "" : "s", (unsigned long)clock_time());
104 }
105
106 while(event_count-- > 0) {
107 process_run();
109 }
110
111 is_poll_running = false;
112 poll_wait_counter = 0;
113
114 return process_nevents() > 0;
115}
116/*---------------------------------------------------------------------------*/
117CC_TRUSTZONE_SECURE_CALL void
118tz_api_println(const char *text)
119{
120 printf("n> %s\n", text);
121}
122/*---------------------------------------------------------------------------*/
123bool
125{
126 if(!initialized) {
127 return false;
128 }
129 if(poll_wait_counter > 0) {
130 poll_wait_counter--;
131 return false;
132 }
133 /* Wait some time for the poll before timeout and request again */
134 poll_wait_counter = 128;
135 return tz_api.request_poll();
136}
137/*---------------------------------------------------------------------------*/
Header file for module for automatically starting and exiting a list of processes.
clock_time_t clock_time(void)
Get the current clock time.
Definition clock.c:118
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition watchdog.c:85
process_num_events_t process_run(void)
Run the system once - call poll handlers and process one event.
Definition process.c:305
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition process.c:325
process_event_t process_alloc_event(void)
Allocate a global event number.
Definition process.c:111
process_num_events_t process_nevents(void)
Number of events waiting to be processed.
Definition process.c:319
CC_TRUSTZONE_SECURE_CALL void tz_api_println(const char *text)
Print the specified message via the secure world.
Definition tz-api.c:118
bool tz_api_request_poll_from_ns(void)
Request poll from normal world.
Definition tz-api.c:124
CC_TRUSTZONE_SECURE_CALL bool tz_api_init(struct tz_api *apip)
Initialize the TrustZone API.
Definition tz-api.c:59
CC_TRUSTZONE_SECURE_CALL bool tz_api_poll(void)
Poll the secure world and process all events in the queue.
Definition tz-api.c:90
Header file for the logging system.