spm_main.c 9.42 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
9
#include <string.h>
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <arch_helpers.h>
#include <bl31/bl31.h>
#include <bl31/ehf.h>
#include <bl31/interrupt_mgmt.h>
#include <common/debug.h>
#include <common/runtime_svc.h>
#include <lib/el3_runtime/context_mgmt.h>
#include <lib/smccc.h>
#include <lib/spinlock.h>
#include <lib/utils.h>
#include <lib/xlat_tables/xlat_tables_v2.h>
#include <plat/common/platform.h>
23
#include <services/spm_svc.h>
24
25
#include <services/sprt_svc.h>
#include <smccc_helpers.h>
26
27

#include "spm_private.h"
28

29
30
31
/*******************************************************************************
 * Secure Partition context information.
 ******************************************************************************/
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
sp_context_t sp_ctx_array[PLAT_SPM_MAX_PARTITIONS];

/* Last Secure Partition last used by the CPU */
sp_context_t *cpu_sp_ctx[PLATFORM_CORE_COUNT];

void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx)
{
	assert(linear_id < PLATFORM_CORE_COUNT);

	cpu_sp_ctx[linear_id] = sp_ctx;
}

sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id)
{
	assert(linear_id < PLATFORM_CORE_COUNT);

	return cpu_sp_ctx[linear_id];
}
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 * Functions to keep track of how many requests a Secure Partition has received
 * and hasn't finished.
 ******************************************************************************/
void spm_sp_request_increase(sp_context_t *sp_ctx)
{
	spin_lock(&(sp_ctx->request_count_lock));
	sp_ctx->request_count++;
	spin_unlock(&(sp_ctx->request_count_lock));
}

void spm_sp_request_decrease(sp_context_t *sp_ctx)
{
	spin_lock(&(sp_ctx->request_count_lock));
	sp_ctx->request_count--;
	spin_unlock(&(sp_ctx->request_count_lock));
}

/* Returns 0 if it was originally 0, -1 otherwise. */
int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx)
{
	int ret = -1;

	spin_lock(&(sp_ctx->request_count_lock));
	if (sp_ctx->request_count == 0U) {
		sp_ctx->request_count++;
		ret = 0U;
	}
	spin_unlock(&(sp_ctx->request_count_lock));

	return ret;
}

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 * This function returns a pointer to the context of the Secure Partition that
 * handles the service specified by an UUID. It returns NULL if the UUID wasn't
 * found.
 ******************************************************************************/
sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4])
{
	unsigned int i;

	for (i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {

		sp_context_t *sp_ctx = &sp_ctx_array[i];

		if (sp_ctx->is_present == 0) {
			continue;
		}

		struct sp_rd_sect_service *rdsvc;

		for (rdsvc = sp_ctx->rd.service; rdsvc != NULL;
		     rdsvc = rdsvc->next) {
			uint32_t *rd_uuid = (uint32_t *)(rdsvc->uuid);

			if (memcmp(rd_uuid, svc_uuid, sizeof(rd_uuid)) == 0) {
				return sp_ctx;
			}
		}
	}

	return NULL;
}

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
 * Set state of a Secure Partition context.
 ******************************************************************************/
void sp_state_set(sp_context_t *sp_ptr, sp_state_t state)
{
	spin_lock(&(sp_ptr->state_lock));
	sp_ptr->state = state;
	spin_unlock(&(sp_ptr->state_lock));
}

/*******************************************************************************
 * Wait until the state of a Secure Partition is the specified one and change it
 * to the desired state.
 ******************************************************************************/
void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
{
	int success = 0;

	while (success == 0) {
		spin_lock(&(sp_ptr->state_lock));

		if (sp_ptr->state == from) {
			sp_ptr->state = to;

			success = 1;
		}

		spin_unlock(&(sp_ptr->state_lock));
	}
}

/*******************************************************************************
 * Check if the state of a Secure Partition is the specified one and, if so,
 * change it to the desired state. Returns 0 on success, -1 on error.
 ******************************************************************************/
int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to)
{
	int ret = -1;

	spin_lock(&(sp_ptr->state_lock));

	if (sp_ptr->state == from) {
		sp_ptr->state = to;

		ret = 0;
	}

	spin_unlock(&(sp_ptr->state_lock));

	return ret;
}

168
/*******************************************************************************
169
170
 * This function takes an SP context pointer and performs a synchronous entry
 * into it.
171
 ******************************************************************************/
172
uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt)
173
{
174
	uint64_t rc;
175
	unsigned int linear_id = plat_my_core_pos();
176

177
	assert(sp_ctx != NULL);
178

179
	/* Assign the context of the SP to this CPU */
180
	spm_cpu_set_sp_ctx(linear_id, sp_ctx);
181
	cm_set_context(&(sp_ctx->cpu_ctx), SECURE);
182

183
	/* Restore the context assigned above */
184
185
186
	cm_el1_sysregs_context_restore(SECURE);
	cm_set_next_eret_context(SECURE);

187
188
189
	/* Invalidate TLBs at EL1. */
	tlbivmalle1();
	dsbish();
190

191
192
193
194
195
196
	if (can_preempt == 1) {
		enable_intr_rm_local(INTR_TYPE_NS, SECURE);
	} else {
		disable_intr_rm_local(INTR_TYPE_NS, SECURE);
	}

197
198
199
200
201
202
203
	/* Enter Secure Partition */
	rc = spm_secure_partition_enter(&sp_ctx->c_rt_ctx);

	/* Save secure state */
	cm_el1_sysregs_context_save(SECURE);

	return rc;
204
205
206
}

/*******************************************************************************
207
208
 * This function returns to the place where spm_sp_synchronous_entry() was
 * called originally.
209
 ******************************************************************************/
210
__dead2 void spm_sp_synchronous_exit(uint64_t rc)
211
{
212
213
214
	/* Get context of the SP in use by this CPU. */
	unsigned int linear_id = plat_my_core_pos();
	sp_context_t *ctx = spm_cpu_get_sp_ctx(linear_id);
215
216
217
218
219
220
221
222
223

	/*
	 * The SPM must have initiated the original request through a
	 * synchronous entry into the secure partition. Jump back to the
	 * original C runtime context with the value of rc in x0;
	 */
	spm_secure_partition_exit(ctx->c_rt_ctx, rc);

	panic();
224
225
}

226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*******************************************************************************
 * This function is the handler registered for Non secure interrupts by the SPM.
 * It validates the interrupt and upon success arranges entry into the normal
 * world for handling the interrupt.
 ******************************************************************************/
static uint64_t spm_ns_interrupt_handler(uint32_t id, uint32_t flags,
					  void *handle, void *cookie)
{
	/* Check the security state when the exception was generated */
	assert(get_interrupt_src_ss(flags) == SECURE);

	spm_sp_synchronous_exit(SPM_SECURE_PARTITION_PREEMPTED);
}

240
/*******************************************************************************
241
 * Jump to each Secure Partition for the first time.
242
 ******************************************************************************/
243
static int32_t spm_init(void)
244
{
245
	uint64_t rc = 0;
246
	sp_context_t *ctx;
247

248
249
250
251
252
253
254
	for (unsigned int i = 0U; i < PLAT_SPM_MAX_PARTITIONS; i++) {

		ctx = &sp_ctx_array[i];

		if (ctx->is_present == 0) {
			continue;
		}
255

256
		INFO("Secure Partition %u init...\n", i);
257

258
		ctx->state = SP_STATE_RESET;
259

260
		rc = spm_sp_synchronous_entry(ctx, 0);
261
262
263
264
		if (rc != SPRT_YIELD_AARCH64) {
			ERROR("Unexpected return value 0x%llx\n", rc);
			panic();
		}
265

266
		ctx->state = SP_STATE_IDLE;
267

268
269
		INFO("Secure Partition %u initialized.\n", i);
	}
270

271
	return rc;
272
273
274
}

/*******************************************************************************
275
 * Initialize contexts of all Secure Partitions.
276
277
278
 ******************************************************************************/
int32_t spm_setup(void)
{
279
	int rc;
280
	sp_context_t *ctx;
281
282
	void *sp_base, *rd_base;
	size_t sp_size, rd_size;
283
	uint64_t flags = 0U;
284

285
286
287
	/* Disable MMU at EL1 (initialized by BL2) */
	disable_mmu_icache_el1();

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
	/*
	 * Non-blocking services can be interrupted by Non-secure interrupts.
	 * Register an interrupt handler for NS interrupts when generated while
	 * the CPU is in secure state. They are routed to EL3.
	 */
	set_interrupt_rm_flag(flags, SECURE);

	uint64_t rc_int = register_interrupt_type_handler(INTR_TYPE_NS,
				spm_ns_interrupt_handler, flags);
	if (rc_int) {
		ERROR("SPM: Failed to register NS interrupt handler with rc = %llx\n",
		      rc_int);
		panic();
	}

303
304
305
	/* Setup shim layer */
	spm_exceptions_xlat_init_context();

306
307
308
	/*
	 * Setup all Secure Partitions.
	 */
309
	unsigned int i = 0U;
310

311
312
313
314
315
316
317
	while (1) {
		rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
						&rd_base, &rd_size);
		if (rc < 0) {
			/* Reached the end of the package. */
			break;
		}
318

319
320
321
322
		if (i >= PLAT_SPM_MAX_PARTITIONS) {
			ERROR("Too many partitions in the package.\n");
			panic();
		}
323

324
		ctx = &sp_ctx_array[i];
325

326
		assert(ctx->is_present == 0);
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
		/* Initialize context of the SP */
		INFO("Secure Partition %u context setup start...\n", i);

		/* Save location of the image in physical memory */
		ctx->image_base = (uintptr_t)sp_base;
		ctx->image_size = sp_size;

		rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
		if (rc < 0) {
			ERROR("Error while loading RD blob.\n");
			panic();
		}

		spm_sp_setup(ctx);

		ctx->is_present = 1;

		INFO("Secure Partition %u setup done.\n", i);

		i++;
348
349
	}

350
351
352
353
	if (i == 0U) {
		ERROR("No present partitions in the package.\n");
		panic();
	}
354
355

	/* Register init function for deferred init.  */
356
357
358
359
	bl31_register_bl32_init(&spm_init);

	return 0;
}