blob: 050c66cc1bd2326bedace54ff54413c1e3e877b6 [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>
14#include <platform.h>
15#include <runtime_svc.h>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000016#include <smccc.h>
17#include <smccc_helpers.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010018#include <spinlock.h>
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +000019#include <string.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010020#include <spm_svc.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 Diaz8c83ad82018-11-08 14:21:19 +0000170uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx)
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
189 /* Enter Secure Partition */
190 rc = spm_secure_partition_enter(&sp_ctx->c_rt_ctx);
191
192 /* Save secure state */
193 cm_el1_sysregs_context_save(SECURE);
194
195 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100196}
197
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100198/*******************************************************************************
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100199 * This function returns to the place where spm_sp_synchronous_entry() was
200 * called originally.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100201 ******************************************************************************/
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000202__dead2 void spm_sp_synchronous_exit(uint64_t rc)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100203{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000204 /* Get context of the SP in use by this CPU. */
205 unsigned int linear_id = plat_my_core_pos();
206 sp_context_t *ctx = spm_cpu_get_sp_ctx(linear_id);
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100207
208 /*
209 * The SPM must have initiated the original request through a
210 * synchronous entry into the secure partition. Jump back to the
211 * original C runtime context with the value of rc in x0;
212 */
213 spm_secure_partition_exit(ctx->c_rt_ctx, rc);
214
215 panic();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100216}
217
218/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100219 * Jump to each Secure Partition for the first time.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100220 ******************************************************************************/
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100221static int32_t spm_init(void)
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100222{
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000223 uint64_t rc = 0;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100224 sp_context_t *ctx;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100225
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000226 for (unsigned int i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100227
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000228 ctx = &sp_ctx_array[i];
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100229
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000230 if (ctx->is_present == 0) {
231 continue;
232 }
233
234 INFO("Secure Partition %u init...\n", i);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100235
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000236 ctx->state = SP_STATE_RESET;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100237
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000238 rc = spm_sp_synchronous_entry(ctx);
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +0000239 if (rc != SPRT_YIELD_AARCH64) {
240 ERROR("Unexpected return value 0x%llx\n", rc);
241 panic();
242 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100243
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000244 ctx->state = SP_STATE_IDLE;
245
246 INFO("Secure Partition %u initialized.\n", i);
247 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100248
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100249 return rc;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100250}
251
252/*******************************************************************************
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100253 * Initialize contexts of all Secure Partitions.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100254 ******************************************************************************/
255int32_t spm_setup(void)
256{
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000257 int rc;
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100258 sp_context_t *ctx;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000259 void *sp_base, *rd_base;
260 size_t sp_size, rd_size;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100261
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100262 /* Disable MMU at EL1 (initialized by BL2) */
263 disable_mmu_icache_el1();
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100264
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000265 unsigned int i = 0U;
266
267 while (1) {
268 rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
269 &rd_base, &rd_size);
270 if (rc < 0) {
271 /* Reached the end of the package. */
272 break;
273 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100274
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000275 if (i >= PLAT_SPM_MAX_PARTITIONS) {
276 ERROR("Too many partitions in the package.\n");
277 panic();
278 }
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100279
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000280 ctx = &sp_ctx_array[i];
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000281
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000282 assert(ctx->is_present == 0);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100283
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000284 /* Initialize context of the SP */
285 INFO("Secure Partition %u context setup start...\n", i);
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000286
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000287 /* Assign translation tables context. */
288 ctx->xlat_ctx_handle = spm_sp_xlat_context_alloc();
289
290 /* Save location of the image in physical memory */
291 ctx->image_base = (uintptr_t)sp_base;
292 ctx->image_size = sp_size;
293
294 rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
295 if (rc < 0) {
296 ERROR("Error while loading RD blob.\n");
297 panic();
298 }
299
300 spm_sp_setup(ctx);
301
302 ctx->is_present = 1;
303
304 INFO("Secure Partition %u setup done.\n", i);
305
306 i++;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +0000307 }
308
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000309 if (i == 0U) {
310 ERROR("No present partitions in the package.\n");
311 panic();
312 }
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100313
314 /* Register init function for deferred init. */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100315 bl31_register_bl32_init(&spm_init);
316
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100317 return 0;
318}
319
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100320/*******************************************************************************
321 * Secure Partition Manager SMC handler.
322 ******************************************************************************/
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100323uint64_t spm_smc_handler(uint32_t smc_fid,
324 uint64_t x1,
325 uint64_t x2,
326 uint64_t x3,
327 uint64_t x4,
328 void *cookie,
329 void *handle,
330 uint64_t flags)
331{
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100332 unsigned int ns;
333
334 /* Determine which security state this SMC originated from */
335 ns = is_caller_non_secure(flags);
336
337 if (ns == SMC_FROM_SECURE) {
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000338 unsigned int linear_id = plat_my_core_pos();
339 sp_context_t *sp_ctx = spm_cpu_get_sp_ctx(linear_id);
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100340
341 /* Handle SMCs from Secure world. */
342
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100343 assert(handle == cm_get_context(SECURE));
344
345 /* Make next ERET jump to S-EL0 instead of S-EL1. */
346 cm_set_elr_spsr_el3(SECURE, read_elr_el1(), read_spsr_el1());
347
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100348 switch (smc_fid) {
349
Sandrine Bailleux843d3f02017-12-07 09:48:56 +0000350 case SPM_VERSION_AARCH32:
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100351 SMC_RET1(handle, SPM_VERSION_COMPILED);
352
Antonio Nino Diaz837173f2017-12-01 14:12:43 +0000353 case SP_MEMORY_ATTRIBUTES_GET_AARCH64:
354 INFO("Received SP_MEMORY_ATTRIBUTES_GET_AARCH64 SMC\n");
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100355
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000356 if (sp_ctx->state != SP_STATE_RESET) {
Antonio Nino Diaz837173f2017-12-01 14:12:43 +0000357 WARN("SP_MEMORY_ATTRIBUTES_GET_AARCH64 is available at boot time only\n");
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100358 SMC_RET1(handle, SPM_NOT_SUPPORTED);
359 }
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100360 SMC_RET1(handle,
361 spm_memory_attributes_get_smc_handler(
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000362 sp_ctx, x1));
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100363
Antonio Nino Diaz837173f2017-12-01 14:12:43 +0000364 case SP_MEMORY_ATTRIBUTES_SET_AARCH64:
365 INFO("Received SP_MEMORY_ATTRIBUTES_SET_AARCH64 SMC\n");
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100366
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000367 if (sp_ctx->state != SP_STATE_RESET) {
Antonio Nino Diaz837173f2017-12-01 14:12:43 +0000368 WARN("SP_MEMORY_ATTRIBUTES_SET_AARCH64 is available at boot time only\n");
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100369 SMC_RET1(handle, SPM_NOT_SUPPORTED);
370 }
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +0100371 SMC_RET1(handle,
372 spm_memory_attributes_set_smc_handler(
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000373 sp_ctx, x1, x2, x3));
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100374 default:
375 break;
376 }
377 } else {
378
379 /* Handle SMCs from Non-secure world. */
380
Antonio Nino Diazda50cd02018-06-15 16:21:01 +0100381 assert(handle == cm_get_context(NON_SECURE));
382
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100383 switch (smc_fid) {
384
Antonio Nino Diaz837173f2017-12-01 14:12:43 +0000385 case SP_MEMORY_ATTRIBUTES_GET_AARCH64:
386 case SP_MEMORY_ATTRIBUTES_SET_AARCH64:
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100387 /* SMC interfaces reserved for secure callers. */
388 SMC_RET1(handle, SPM_NOT_SUPPORTED);
389
390 default:
391 break;
392 }
393 }
394
395 SMC_RET1(handle, SMC_UNK);
396}