blob: 460d1fb31ca21e7795e61f61a2dfbd0f1b3cc32b [file] [log] [blame]
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01001/*
Antonio Nino Diaz35b37f02018-01-08 17:33:34 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <bl31.h>
10#include <context_mgmt.h>
11#include <debug.h>
Sughosh Ganue77f9e82018-11-14 11:06:24 +053012#include <ehf.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010013#include <errno.h>
Antonio Nino Diazfb763962018-07-03 16:54:33 +010014#include <interrupt_mgmt.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010015#include <platform.h>
16#include <runtime_svc.h>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000017#include <smccc.h>
18#include <smccc_helpers.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010019#include <spinlock.h>
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +000020#include <string.h>
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +000021#include <sprt_svc.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010022#include <utils.h>
23#include <xlat_tables_v2.h>
24
25#include "spm_private.h"
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010026
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010027/*******************************************************************************
28 * Secure Partition context information.
29 ******************************************************************************/
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +000030sp_context_t sp_ctx_array[PLAT_SPM_MAX_PARTITIONS];
31
32/* Last Secure Partition last used by the CPU */
33sp_context_t *cpu_sp_ctx[PLATFORM_CORE_COUNT];
34
35void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx)
36{
37 assert(linear_id < PLATFORM_CORE_COUNT);
38
39 cpu_sp_ctx[linear_id] = sp_ctx;
40}
41
42sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id)
43{
44 assert(linear_id < PLATFORM_CORE_COUNT);
45
46 return cpu_sp_ctx[linear_id];
47}
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010048
49/*******************************************************************************
Antonio Nino Diaz44ef4eb2018-07-03 19:54:59 +010050 * Functions to keep track of how many requests a Secure Partition has received
51 * and hasn't finished.
52 ******************************************************************************/
53void spm_sp_request_increase(sp_context_t *sp_ctx)
54{
55 spin_lock(&(sp_ctx->request_count_lock));
56 sp_ctx->request_count++;
57 spin_unlock(&(sp_ctx->request_count_lock));
58}
59
60void spm_sp_request_decrease(sp_context_t *sp_ctx)
61{
62 spin_lock(&(sp_ctx->request_count_lock));
63 sp_ctx->request_count--;
64 spin_unlock(&(sp_ctx->request_count_lock));
65}
66
67/* Returns 0 if it was originally 0, -1 otherwise. */
68int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx)
69{
70 int ret = -1;
71
72 spin_lock(&(sp_ctx->request_count_lock));
73 if (sp_ctx->request_count == 0U) {
74 sp_ctx->request_count++;
75 ret = 0U;
76 }
77 spin_unlock(&(sp_ctx->request_count_lock));
78
79 return ret;
80}
81
82/*******************************************************************************
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +000083 * This function returns a pointer to the context of the Secure Partition that
84 * handles the service specified by an UUID. It returns NULL if the UUID wasn't
85 * found.
86 ******************************************************************************/
87sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4])
88{
89 unsigned int i;
90
91 for (i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
92
93 sp_context_t *sp_ctx = &sp_ctx_array[i];
94
95 if (sp_ctx->is_present == 0) {
96 continue;
97 }
98
99 struct sp_rd_sect_service *rdsvc;
100
101 for (rdsvc = sp_ctx->rd.service; rdsvc != NULL;
102 rdsvc = rdsvc->next) {
103 uint32_t *rd_uuid = (uint32_t *)(rdsvc->uuid);
104
105 if (memcmp(rd_uuid, svc_uuid, sizeof(rd_uuid)) == 0) {
106 return sp_ctx;
107 }
108 }
109 }
110
111 return NULL;
112}
113
114/*******************************************************************************
Antonio Nino Diazc4f27522018-05-23 09:09:41 +0100115 * Set state of a Secure Partition context.
116 ******************************************************************************/
117void sp_state_set(sp_context_t *sp_ptr, sp_state_t state)
118{
119 spin_lock(&(sp_ptr->state_lock));
120 sp_ptr->state = state;
121 spin_unlock(&(sp_ptr->state_lock));
122}
123
124/*******************************************************************************
125 * Wait until the state of a Secure Partition is the specified one and change it
126 * to the desired state.
127 ******************************************************************************/
128void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
129{
130 int success = 0;
131
132 while (success == 0) {
133 spin_lock(&(sp_ptr->state_lock));
134
135 if (sp_ptr->state == from) {
136 sp_ptr->state = to;
137
138 success = 1;
139 }
140
141 spin_unlock(&(sp_ptr->state_lock));
142 }
143}
144
145/*******************************************************************************
146 * Check if the state of a Secure Partition is the specified one and, if so,
147 * change it to the desired state. Returns 0 on success, -1 on error.
148 ******************************************************************************/
149int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
150{
151 int ret = -1;
152
153 spin_lock(&(sp_ptr->state_lock));
154
155 if (sp_ptr->state == from) {
156 sp_ptr->state = to;
157
158 ret = 0;
159 }
160
161 spin_unlock(&(sp_ptr->state_lock));
162
163 return ret;
164}
165
166/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100167 * This function takes an SP context pointer and performs a synchronous entry
168 * into it.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100169 ******************************************************************************/
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100170uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100171{
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100172 uint64_t rc;
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000173 unsigned int linear_id = plat_my_core_pos();
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100174
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100175 assert(sp_ctx != NULL);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100176
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100177 /* Assign the context of the SP to this CPU */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000178 spm_cpu_set_sp_ctx(linear_id, sp_ctx);
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100179 cm_set_context(&(sp_ctx->cpu_ctx), SECURE);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100180
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100181 /* Restore the context assigned above */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100182 cm_el1_sysregs_context_restore(SECURE);
183 cm_set_next_eret_context(SECURE);
184
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100185 /* Invalidate TLBs at EL1. */
186 tlbivmalle1();
187 dsbish();
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100188
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100189 if (can_preempt == 1) {
190 enable_intr_rm_local(INTR_TYPE_NS, SECURE);
191 } else {
192 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
193 }
194
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100195 /* Enter Secure Partition */
196 rc = spm_secure_partition_enter(&sp_ctx->c_rt_ctx);
197
198 /* Save secure state */
199 cm_el1_sysregs_context_save(SECURE);
200
201 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100202}
203
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100204/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100205 * This function returns to the place where spm_sp_synchronous_entry() was
206 * called originally.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100207 ******************************************************************************/
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000208__dead2 void spm_sp_synchronous_exit(uint64_t rc)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100209{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000210 /* Get context of the SP in use by this CPU. */
211 unsigned int linear_id = plat_my_core_pos();
212 sp_context_t *ctx = spm_cpu_get_sp_ctx(linear_id);
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100213
214 /*
215 * The SPM must have initiated the original request through a
216 * synchronous entry into the secure partition. Jump back to the
217 * original C runtime context with the value of rc in x0;
218 */
219 spm_secure_partition_exit(ctx->c_rt_ctx, rc);
220
221 panic();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100222}
223
224/*******************************************************************************
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100225 * This function is the handler registered for Non secure interrupts by the SPM.
226 * It validates the interrupt and upon success arranges entry into the normal
227 * world for handling the interrupt.
228 ******************************************************************************/
229static uint64_t spm_ns_interrupt_handler(uint32_t id, uint32_t flags,
230 void *handle, void *cookie)
231{
232 /* Check the security state when the exception was generated */
233 assert(get_interrupt_src_ss(flags) == SECURE);
234
235 spm_sp_synchronous_exit(SPM_SECURE_PARTITION_PREEMPTED);
236}
237
238/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100239 * Jump to each Secure Partition for the first time.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100240 ******************************************************************************/
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100241static int32_t spm_init(void)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100242{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000243 uint64_t rc = 0;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100244 sp_context_t *ctx;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100245
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000246 for (unsigned int i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100247
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000248 ctx = &sp_ctx_array[i];
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100249
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000250 if (ctx->is_present == 0) {
251 continue;
252 }
253
254 INFO("Secure Partition %u init...\n", i);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100255
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000256 ctx->state = SP_STATE_RESET;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100257
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100258 rc = spm_sp_synchronous_entry(ctx, 0);
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000259 if (rc != SPRT_YIELD_AARCH64) {
260 ERROR("Unexpected return value 0x%llx\n", rc);
261 panic();
262 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100263
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000264 ctx->state = SP_STATE_IDLE;
265
266 INFO("Secure Partition %u initialized.\n", i);
267 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100268
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100269 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100270}
271
272/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100273 * Initialize contexts of all Secure Partitions.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100274 ******************************************************************************/
275int32_t spm_setup(void)
276{
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000277 int rc;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100278 sp_context_t *ctx;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000279 void *sp_base, *rd_base;
280 size_t sp_size, rd_size;
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100281 uint64_t flags = 0U;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100282
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100283 /* Disable MMU at EL1 (initialized by BL2) */
284 disable_mmu_icache_el1();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100285
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100286 /*
287 * Non-blocking services can be interrupted by Non-secure interrupts.
288 * Register an interrupt handler for NS interrupts when generated while
289 * the CPU is in secure state. They are routed to EL3.
290 */
291 set_interrupt_rm_flag(flags, SECURE);
292
293 uint64_t rc_int = register_interrupt_type_handler(INTR_TYPE_NS,
294 spm_ns_interrupt_handler, flags);
295 if (rc_int) {
296 ERROR("SPM: Failed to register NS interrupt handler with rc = %llx\n",
297 rc_int);
298 panic();
299 }
300
301 /*
302 * Setup all Secure Partitions.
303 */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000304 unsigned int i = 0U;
305
306 while (1) {
307 rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
308 &rd_base, &rd_size);
309 if (rc < 0) {
310 /* Reached the end of the package. */
311 break;
312 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100313
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000314 if (i >= PLAT_SPM_MAX_PARTITIONS) {
315 ERROR("Too many partitions in the package.\n");
316 panic();
317 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100318
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000319 ctx = &sp_ctx_array[i];
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000320
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000321 assert(ctx->is_present == 0);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100322
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000323 /* Initialize context of the SP */
324 INFO("Secure Partition %u context setup start...\n", i);
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000325
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000326 /* Assign translation tables context. */
327 ctx->xlat_ctx_handle = spm_sp_xlat_context_alloc();
328
329 /* Save location of the image in physical memory */
330 ctx->image_base = (uintptr_t)sp_base;
331 ctx->image_size = sp_size;
332
333 rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
334 if (rc < 0) {
335 ERROR("Error while loading RD blob.\n");
336 panic();
337 }
338
339 spm_sp_setup(ctx);
340
341 ctx->is_present = 1;
342
343 INFO("Secure Partition %u setup done.\n", i);
344
345 i++;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000346 }
347
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000348 if (i == 0U) {
349 ERROR("No present partitions in the package.\n");
350 panic();
351 }
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100352
353 /* Register init function for deferred init. */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100354 bl31_register_bl32_init(&spm_init);
355
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100356 return 0;
357}