blob: adfffd58c5561cdd0ddc9ee00bddaee875d39757 [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
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01007#include <assert.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01008#include <errno.h>
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +00009#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <arch_helpers.h>
12#include <bl31/bl31.h>
13#include <bl31/ehf.h>
14#include <bl31/interrupt_mgmt.h>
15#include <common/debug.h>
16#include <common/runtime_svc.h>
17#include <lib/el3_runtime/context_mgmt.h>
18#include <lib/smccc.h>
19#include <lib/spinlock.h>
20#include <lib/utils.h>
21#include <lib/xlat_tables/xlat_tables_v2.h>
22#include <plat/common/platform.h>
23#include <services/sprt_svc.h>
24#include <smccc_helpers.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010025
26#include "spm_private.h"
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010027
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010028/*******************************************************************************
29 * Secure Partition context information.
30 ******************************************************************************/
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +000031sp_context_t sp_ctx_array[PLAT_SPM_MAX_PARTITIONS];
32
33/* Last Secure Partition last used by the CPU */
34sp_context_t *cpu_sp_ctx[PLATFORM_CORE_COUNT];
35
36void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx)
37{
38 assert(linear_id < PLATFORM_CORE_COUNT);
39
40 cpu_sp_ctx[linear_id] = sp_ctx;
41}
42
43sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id)
44{
45 assert(linear_id < PLATFORM_CORE_COUNT);
46
47 return cpu_sp_ctx[linear_id];
48}
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010049
50/*******************************************************************************
Antonio Nino Diaz44ef4eb2018-07-03 19:54:59 +010051 * Functions to keep track of how many requests a Secure Partition has received
52 * and hasn't finished.
53 ******************************************************************************/
54void spm_sp_request_increase(sp_context_t *sp_ctx)
55{
56 spin_lock(&(sp_ctx->request_count_lock));
57 sp_ctx->request_count++;
58 spin_unlock(&(sp_ctx->request_count_lock));
59}
60
61void spm_sp_request_decrease(sp_context_t *sp_ctx)
62{
63 spin_lock(&(sp_ctx->request_count_lock));
64 sp_ctx->request_count--;
65 spin_unlock(&(sp_ctx->request_count_lock));
66}
67
68/* Returns 0 if it was originally 0, -1 otherwise. */
69int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx)
70{
71 int ret = -1;
72
73 spin_lock(&(sp_ctx->request_count_lock));
74 if (sp_ctx->request_count == 0U) {
75 sp_ctx->request_count++;
76 ret = 0U;
77 }
78 spin_unlock(&(sp_ctx->request_count_lock));
79
80 return ret;
81}
82
83/*******************************************************************************
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +000084 * This function returns a pointer to the context of the Secure Partition that
85 * handles the service specified by an UUID. It returns NULL if the UUID wasn't
86 * found.
87 ******************************************************************************/
88sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4])
89{
90 unsigned int i;
91
92 for (i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
93
94 sp_context_t *sp_ctx = &sp_ctx_array[i];
95
96 if (sp_ctx->is_present == 0) {
97 continue;
98 }
99
100 struct sp_rd_sect_service *rdsvc;
101
102 for (rdsvc = sp_ctx->rd.service; rdsvc != NULL;
103 rdsvc = rdsvc->next) {
104 uint32_t *rd_uuid = (uint32_t *)(rdsvc->uuid);
105
106 if (memcmp(rd_uuid, svc_uuid, sizeof(rd_uuid)) == 0) {
107 return sp_ctx;
108 }
109 }
110 }
111
112 return NULL;
113}
114
115/*******************************************************************************
Antonio Nino Diazc4f27522018-05-23 09:09:41 +0100116 * Set state of a Secure Partition context.
117 ******************************************************************************/
118void sp_state_set(sp_context_t *sp_ptr, sp_state_t state)
119{
120 spin_lock(&(sp_ptr->state_lock));
121 sp_ptr->state = state;
122 spin_unlock(&(sp_ptr->state_lock));
123}
124
125/*******************************************************************************
126 * Wait until the state of a Secure Partition is the specified one and change it
127 * to the desired state.
128 ******************************************************************************/
129void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
130{
131 int success = 0;
132
133 while (success == 0) {
134 spin_lock(&(sp_ptr->state_lock));
135
136 if (sp_ptr->state == from) {
137 sp_ptr->state = to;
138
139 success = 1;
140 }
141
142 spin_unlock(&(sp_ptr->state_lock));
143 }
144}
145
146/*******************************************************************************
147 * Check if the state of a Secure Partition is the specified one and, if so,
148 * change it to the desired state. Returns 0 on success, -1 on error.
149 ******************************************************************************/
150int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
151{
152 int ret = -1;
153
154 spin_lock(&(sp_ptr->state_lock));
155
156 if (sp_ptr->state == from) {
157 sp_ptr->state = to;
158
159 ret = 0;
160 }
161
162 spin_unlock(&(sp_ptr->state_lock));
163
164 return ret;
165}
166
167/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100168 * This function takes an SP context pointer and performs a synchronous entry
169 * into it.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100170 ******************************************************************************/
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100171uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100172{
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100173 uint64_t rc;
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000174 unsigned int linear_id = plat_my_core_pos();
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100175
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100176 assert(sp_ctx != NULL);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100177
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100178 /* Assign the context of the SP to this CPU */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000179 spm_cpu_set_sp_ctx(linear_id, sp_ctx);
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100180 cm_set_context(&(sp_ctx->cpu_ctx), SECURE);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100181
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100182 /* Restore the context assigned above */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100183 cm_el1_sysregs_context_restore(SECURE);
184 cm_set_next_eret_context(SECURE);
185
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100186 /* Invalidate TLBs at EL1. */
187 tlbivmalle1();
188 dsbish();
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100189
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100190 if (can_preempt == 1) {
191 enable_intr_rm_local(INTR_TYPE_NS, SECURE);
192 } else {
193 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
194 }
195
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100196 /* Enter Secure Partition */
197 rc = spm_secure_partition_enter(&sp_ctx->c_rt_ctx);
198
199 /* Save secure state */
200 cm_el1_sysregs_context_save(SECURE);
201
202 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100203}
204
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100205/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100206 * This function returns to the place where spm_sp_synchronous_entry() was
207 * called originally.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100208 ******************************************************************************/
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000209__dead2 void spm_sp_synchronous_exit(uint64_t rc)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100210{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000211 /* Get context of the SP in use by this CPU. */
212 unsigned int linear_id = plat_my_core_pos();
213 sp_context_t *ctx = spm_cpu_get_sp_ctx(linear_id);
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100214
215 /*
216 * The SPM must have initiated the original request through a
217 * synchronous entry into the secure partition. Jump back to the
218 * original C runtime context with the value of rc in x0;
219 */
220 spm_secure_partition_exit(ctx->c_rt_ctx, rc);
221
222 panic();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100223}
224
225/*******************************************************************************
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100226 * This function is the handler registered for Non secure interrupts by the SPM.
227 * It validates the interrupt and upon success arranges entry into the normal
228 * world for handling the interrupt.
229 ******************************************************************************/
230static uint64_t spm_ns_interrupt_handler(uint32_t id, uint32_t flags,
231 void *handle, void *cookie)
232{
233 /* Check the security state when the exception was generated */
234 assert(get_interrupt_src_ss(flags) == SECURE);
235
236 spm_sp_synchronous_exit(SPM_SECURE_PARTITION_PREEMPTED);
237}
238
239/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100240 * Jump to each Secure Partition for the first time.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100241 ******************************************************************************/
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100242static int32_t spm_init(void)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100243{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000244 uint64_t rc = 0;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100245 sp_context_t *ctx;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100246
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000247 for (unsigned int i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100248
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000249 ctx = &sp_ctx_array[i];
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100250
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000251 if (ctx->is_present == 0) {
252 continue;
253 }
254
255 INFO("Secure Partition %u init...\n", i);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100256
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000257 ctx->state = SP_STATE_RESET;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100258
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100259 rc = spm_sp_synchronous_entry(ctx, 0);
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000260 if (rc != SPRT_YIELD_AARCH64) {
261 ERROR("Unexpected return value 0x%llx\n", rc);
262 panic();
263 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100264
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000265 ctx->state = SP_STATE_IDLE;
266
267 INFO("Secure Partition %u initialized.\n", i);
268 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100269
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100270 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100271}
272
273/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100274 * Initialize contexts of all Secure Partitions.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100275 ******************************************************************************/
276int32_t spm_setup(void)
277{
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000278 int rc;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100279 sp_context_t *ctx;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000280 void *sp_base, *rd_base;
281 size_t sp_size, rd_size;
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100282 uint64_t flags = 0U;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100283
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100284 /* Disable MMU at EL1 (initialized by BL2) */
285 disable_mmu_icache_el1();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100286
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100287 /*
288 * Non-blocking services can be interrupted by Non-secure interrupts.
289 * Register an interrupt handler for NS interrupts when generated while
290 * the CPU is in secure state. They are routed to EL3.
291 */
292 set_interrupt_rm_flag(flags, SECURE);
293
294 uint64_t rc_int = register_interrupt_type_handler(INTR_TYPE_NS,
295 spm_ns_interrupt_handler, flags);
296 if (rc_int) {
297 ERROR("SPM: Failed to register NS interrupt handler with rc = %llx\n",
298 rc_int);
299 panic();
300 }
301
302 /*
303 * Setup all Secure Partitions.
304 */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000305 unsigned int i = 0U;
306
307 while (1) {
308 rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
309 &rd_base, &rd_size);
310 if (rc < 0) {
311 /* Reached the end of the package. */
312 break;
313 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100314
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000315 if (i >= PLAT_SPM_MAX_PARTITIONS) {
316 ERROR("Too many partitions in the package.\n");
317 panic();
318 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100319
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000320 ctx = &sp_ctx_array[i];
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000321
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000322 assert(ctx->is_present == 0);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100323
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000324 /* Initialize context of the SP */
325 INFO("Secure Partition %u context setup start...\n", i);
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000326
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000327 /* Assign translation tables context. */
328 ctx->xlat_ctx_handle = spm_sp_xlat_context_alloc();
329
330 /* Save location of the image in physical memory */
331 ctx->image_base = (uintptr_t)sp_base;
332 ctx->image_size = sp_size;
333
334 rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
335 if (rc < 0) {
336 ERROR("Error while loading RD blob.\n");
337 panic();
338 }
339
340 spm_sp_setup(ctx);
341
342 ctx->is_present = 1;
343
344 INFO("Secure Partition %u setup done.\n", i);
345
346 i++;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000347 }
348
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000349 if (i == 0U) {
350 ERROR("No present partitions in the package.\n");
351 panic();
352 }
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100353
354 /* Register init function for deferred init. */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100355 bl31_register_bl32_init(&spm_init);
356
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100357 return 0;
358}