blob: 3a63f1c69f3a4b0f8a981ed28fcdbf939ae86b88 [file] [log] [blame]
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01001/*
Ambroise Vincentb237bca2019-02-13 15:58:00 +00002 * Copyright (c) 2017-2019, 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>
Ambroise Vincentb237bca2019-02-13 15:58:00 +000023#include <services/spm_svc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <services/sprt_svc.h>
25#include <smccc_helpers.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010026
27#include "spm_private.h"
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010028
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010029/*******************************************************************************
30 * Secure Partition context information.
31 ******************************************************************************/
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +000032sp_context_t sp_ctx_array[PLAT_SPM_MAX_PARTITIONS];
33
34/* Last Secure Partition last used by the CPU */
35sp_context_t *cpu_sp_ctx[PLATFORM_CORE_COUNT];
36
37void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx)
38{
39 assert(linear_id < PLATFORM_CORE_COUNT);
40
41 cpu_sp_ctx[linear_id] = sp_ctx;
42}
43
44sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id)
45{
46 assert(linear_id < PLATFORM_CORE_COUNT);
47
48 return cpu_sp_ctx[linear_id];
49}
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010050
51/*******************************************************************************
Antonio Nino Diaz44ef4eb2018-07-03 19:54:59 +010052 * Functions to keep track of how many requests a Secure Partition has received
53 * and hasn't finished.
54 ******************************************************************************/
55void spm_sp_request_increase(sp_context_t *sp_ctx)
56{
57 spin_lock(&(sp_ctx->request_count_lock));
58 sp_ctx->request_count++;
59 spin_unlock(&(sp_ctx->request_count_lock));
60}
61
62void spm_sp_request_decrease(sp_context_t *sp_ctx)
63{
64 spin_lock(&(sp_ctx->request_count_lock));
65 sp_ctx->request_count--;
66 spin_unlock(&(sp_ctx->request_count_lock));
67}
68
69/* Returns 0 if it was originally 0, -1 otherwise. */
70int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx)
71{
72 int ret = -1;
73
74 spin_lock(&(sp_ctx->request_count_lock));
75 if (sp_ctx->request_count == 0U) {
76 sp_ctx->request_count++;
77 ret = 0U;
78 }
79 spin_unlock(&(sp_ctx->request_count_lock));
80
81 return ret;
82}
83
84/*******************************************************************************
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +000085 * This function returns a pointer to the context of the Secure Partition that
86 * handles the service specified by an UUID. It returns NULL if the UUID wasn't
87 * found.
88 ******************************************************************************/
89sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4])
90{
91 unsigned int i;
92
93 for (i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
94
95 sp_context_t *sp_ctx = &sp_ctx_array[i];
96
97 if (sp_ctx->is_present == 0) {
98 continue;
99 }
100
101 struct sp_rd_sect_service *rdsvc;
102
103 for (rdsvc = sp_ctx->rd.service; rdsvc != NULL;
104 rdsvc = rdsvc->next) {
105 uint32_t *rd_uuid = (uint32_t *)(rdsvc->uuid);
106
Paul Beesleyb0c9ed12019-04-08 15:24:43 +0100107 if (memcmp(rd_uuid, svc_uuid, sizeof(*svc_uuid)) == 0) {
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +0000108 return sp_ctx;
109 }
110 }
111 }
112
113 return NULL;
114}
115
116/*******************************************************************************
Antonio Nino Diazc4f27522018-05-23 09:09:41 +0100117 * Set state of a Secure Partition context.
118 ******************************************************************************/
119void sp_state_set(sp_context_t *sp_ptr, sp_state_t state)
120{
121 spin_lock(&(sp_ptr->state_lock));
122 sp_ptr->state = state;
123 spin_unlock(&(sp_ptr->state_lock));
124}
125
126/*******************************************************************************
127 * Wait until the state of a Secure Partition is the specified one and change it
128 * to the desired state.
129 ******************************************************************************/
130void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
131{
132 int success = 0;
133
134 while (success == 0) {
135 spin_lock(&(sp_ptr->state_lock));
136
137 if (sp_ptr->state == from) {
138 sp_ptr->state = to;
139
140 success = 1;
141 }
142
143 spin_unlock(&(sp_ptr->state_lock));
144 }
145}
146
147/*******************************************************************************
148 * Check if the state of a Secure Partition is the specified one and, if so,
149 * change it to the desired state. Returns 0 on success, -1 on error.
150 ******************************************************************************/
151int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
152{
153 int ret = -1;
154
155 spin_lock(&(sp_ptr->state_lock));
156
157 if (sp_ptr->state == from) {
158 sp_ptr->state = to;
159
160 ret = 0;
161 }
162
163 spin_unlock(&(sp_ptr->state_lock));
164
165 return ret;
166}
167
168/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100169 * This function takes an SP context pointer and performs a synchronous entry
170 * into it.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100171 ******************************************************************************/
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100172uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100173{
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100174 uint64_t rc;
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000175 unsigned int linear_id = plat_my_core_pos();
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100176
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100177 assert(sp_ctx != NULL);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100178
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100179 /* Assign the context of the SP to this CPU */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000180 spm_cpu_set_sp_ctx(linear_id, sp_ctx);
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100181 cm_set_context(&(sp_ctx->cpu_ctx), SECURE);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100182
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100183 /* Restore the context assigned above */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100184 cm_el1_sysregs_context_restore(SECURE);
185 cm_set_next_eret_context(SECURE);
186
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100187 /* Invalidate TLBs at EL1. */
188 tlbivmalle1();
189 dsbish();
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100190
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100191 if (can_preempt == 1) {
192 enable_intr_rm_local(INTR_TYPE_NS, SECURE);
193 } else {
194 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
195 }
196
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100197 /* Enter Secure Partition */
198 rc = spm_secure_partition_enter(&sp_ctx->c_rt_ctx);
199
200 /* Save secure state */
201 cm_el1_sysregs_context_save(SECURE);
202
203 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100204}
205
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100206/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100207 * This function returns to the place where spm_sp_synchronous_entry() was
208 * called originally.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100209 ******************************************************************************/
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000210__dead2 void spm_sp_synchronous_exit(uint64_t rc)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100211{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000212 /* Get context of the SP in use by this CPU. */
213 unsigned int linear_id = plat_my_core_pos();
214 sp_context_t *ctx = spm_cpu_get_sp_ctx(linear_id);
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100215
216 /*
217 * The SPM must have initiated the original request through a
218 * synchronous entry into the secure partition. Jump back to the
219 * original C runtime context with the value of rc in x0;
220 */
221 spm_secure_partition_exit(ctx->c_rt_ctx, rc);
222
223 panic();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100224}
225
226/*******************************************************************************
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100227 * This function is the handler registered for Non secure interrupts by the SPM.
228 * It validates the interrupt and upon success arranges entry into the normal
229 * world for handling the interrupt.
230 ******************************************************************************/
231static uint64_t spm_ns_interrupt_handler(uint32_t id, uint32_t flags,
232 void *handle, void *cookie)
233{
234 /* Check the security state when the exception was generated */
235 assert(get_interrupt_src_ss(flags) == SECURE);
236
237 spm_sp_synchronous_exit(SPM_SECURE_PARTITION_PREEMPTED);
238}
239
240/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100241 * Jump to each Secure Partition for the first time.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100242 ******************************************************************************/
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100243static int32_t spm_init(void)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100244{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000245 uint64_t rc = 0;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100246 sp_context_t *ctx;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100247
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000248 for (unsigned int i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100249
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000250 ctx = &sp_ctx_array[i];
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100251
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000252 if (ctx->is_present == 0) {
253 continue;
254 }
255
256 INFO("Secure Partition %u init...\n", i);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100257
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000258 ctx->state = SP_STATE_RESET;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100259
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100260 rc = spm_sp_synchronous_entry(ctx, 0);
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000261 if (rc != SPRT_YIELD_AARCH64) {
262 ERROR("Unexpected return value 0x%llx\n", rc);
263 panic();
264 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100265
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000266 ctx->state = SP_STATE_IDLE;
267
268 INFO("Secure Partition %u initialized.\n", i);
269 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100270
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100271 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100272}
273
274/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100275 * Initialize contexts of all Secure Partitions.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100276 ******************************************************************************/
277int32_t spm_setup(void)
278{
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000279 int rc;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100280 sp_context_t *ctx;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000281 void *sp_base, *rd_base;
282 size_t sp_size, rd_size;
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100283 uint64_t flags = 0U;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100284
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100285 /* Disable MMU at EL1 (initialized by BL2) */
286 disable_mmu_icache_el1();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100287
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100288 /*
289 * Non-blocking services can be interrupted by Non-secure interrupts.
290 * Register an interrupt handler for NS interrupts when generated while
291 * the CPU is in secure state. They are routed to EL3.
292 */
293 set_interrupt_rm_flag(flags, SECURE);
294
295 uint64_t rc_int = register_interrupt_type_handler(INTR_TYPE_NS,
296 spm_ns_interrupt_handler, flags);
297 if (rc_int) {
298 ERROR("SPM: Failed to register NS interrupt handler with rc = %llx\n",
299 rc_int);
300 panic();
301 }
302
Antonio Nino Diaz37f97a52019-03-27 11:10:31 +0000303 /* Setup shim layer */
304 spm_exceptions_xlat_init_context();
305
Antonio Nino Diazfb763962018-07-03 16:54:33 +0100306 /*
307 * Setup all Secure Partitions.
308 */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000309 unsigned int i = 0U;
310
311 while (1) {
312 rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
313 &rd_base, &rd_size);
314 if (rc < 0) {
315 /* Reached the end of the package. */
316 break;
317 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100318
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000319 if (i >= PLAT_SPM_MAX_PARTITIONS) {
320 ERROR("Too many partitions in the package.\n");
321 panic();
322 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100323
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000324 ctx = &sp_ctx_array[i];
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000325
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000326 assert(ctx->is_present == 0);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100327
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000328 /* Initialize context of the SP */
329 INFO("Secure Partition %u context setup start...\n", i);
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000330
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000331 /* Save location of the image in physical memory */
332 ctx->image_base = (uintptr_t)sp_base;
333 ctx->image_size = sp_size;
334
335 rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
336 if (rc < 0) {
337 ERROR("Error while loading RD blob.\n");
338 panic();
339 }
340
341 spm_sp_setup(ctx);
342
343 ctx->is_present = 1;
344
345 INFO("Secure Partition %u setup done.\n", i);
346
347 i++;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000348 }
349
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000350 if (i == 0U) {
351 ERROR("No present partitions in the package.\n");
352 panic();
353 }
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100354
355 /* Register init function for deferred init. */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100356 bl31_register_bl32_init(&spm_init);
357
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100358 return 0;
359}