tlkd_main.c 15 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3
 * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
4
 *
dp-arm's avatar
dp-arm committed
5
 * SPDX-License-Identifier: BSD-3-Clause
6
7
8
9
10
11
12
13
14
15
16
17
 */

/*******************************************************************************
 * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
 * plug-in component to the Secure Monitor, registered as a runtime service. The
 * SPD is expected to be a functional extension of the Secure Payload (SP) that
 * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
 * the Trusted OS/Applications range to the dispatcher. The SPD will either
 * handle the request locally or delegate it to the Secure Payload. It is also
 * responsible for initialising and maintaining communication with the SP.
 ******************************************************************************/
#include <assert.h>
18
#include <bl31/interrupt_mgmt.h>
19
20
#include <errno.h>
#include <stddef.h>
21

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
22
#include <arch_helpers.h>
23
#include <bl31/bl31.h>
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
24
#include <bl32/payloads/tlk.h>
25
26
27
28
29
30
31
#include <common/bl_common.h>
#include <common/debug.h>
#include <common/runtime_svc.h>
#include <lib/el3_runtime/context_mgmt.h>
#include <plat/common/platform.h>
#include <tools_share/uuid.h>

32
33
34
35
36
#include "tlkd_private.h"

extern const spd_pm_ops_t tlkd_pm_ops;

/*******************************************************************************
37
 * Per-cpu Secure Payload state
38
 ******************************************************************************/
39
tlk_context_t tlk_ctx;
40

41
42
43
/*******************************************************************************
 * CPU number on which TLK booted up
 ******************************************************************************/
44
static uint32_t boot_cpu;
45

46
/* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
47
48
49
DEFINE_SVC_UUID2(tlk_uuid,
	0xc9e911bd, 0xba2b, 0xee52, 0xb1, 0x72,
	0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
50

51
static int32_t tlkd_init(void);
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
84
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************************************************
 * Secure Payload Dispatcher's timer interrupt handler
 ******************************************************************************/
static uint64_t tlkd_interrupt_handler(uint32_t id,
					uint32_t flags,
					void *handle,
					void *cookie)
{
	cpu_context_t *s_cpu_context;
	int irq = plat_ic_get_pending_interrupt_id();

	/* acknowledge the interrupt and mark it complete */
	(void)plat_ic_acknowledge_interrupt();
	plat_ic_end_of_interrupt(irq);

	/*
	 * Disable the routing of NS interrupts from secure world to
	 * EL3 while interrupted on this core.
	 */
	disable_intr_rm_local(INTR_TYPE_S_EL1, SECURE);

	/* Check the security state when the exception was generated */
	assert(get_interrupt_src_ss(flags) == NON_SECURE);
	assert(handle == cm_get_context(NON_SECURE));

	/* Save non-secure state */
	cm_el1_sysregs_context_save(NON_SECURE);

	/* Get a reference to the secure context */
	s_cpu_context = cm_get_context(SECURE);
	assert(s_cpu_context);

	/*
	 * Restore non-secure state. There is no need to save the
	 * secure system register context since the SP was supposed
	 * to preserve it during S-EL1 interrupt handling.
	 */
	cm_el1_sysregs_context_restore(SECURE);
	cm_set_next_eret_context(SECURE);

	/* Provide the IRQ number to the SPD */
	SMC_RET4(s_cpu_context, (uint32_t)TLK_IRQ_FIRED, 0, (uint32_t)irq, 0);
}

97
98
99
100
101
/*******************************************************************************
 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
 * (aarch32/aarch64) if not already known and initialises the context for entry
 * into the SP for its initialisation.
 ******************************************************************************/
102
static int32_t tlkd_setup(void)
103
104
{
	entry_point_info_t *tlk_ep_info;
105
106
	uint32_t flags;
	int32_t ret;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

	/*
	 * Get information about the Secure Payload (BL32) image. Its
	 * absence is a critical failure.
	 */
	tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
	if (!tlk_ep_info) {
		WARN("No SP provided. Booting device without SP"
			" initialization. SMC`s destined for SP"
			" will return SMC_UNK\n");
		return 1;
	}

	/*
	 * If there's no valid entry point for SP, we return a non-zero value
	 * signalling failure initializing the service. We bail out without
	 * registering any handlers
	 */
	if (!tlk_ep_info->pc)
		return 1;

	/*
	 * Inspect the SP image's SPSR and determine it's execution state
	 * i.e whether AArch32 or AArch64.
	 */
	tlkd_init_tlk_ep_state(tlk_ep_info,
		(tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
		tlk_ep_info->pc,
		&tlk_ctx);

137
138
139
140
141
142
143
144
145
146
147
148
	/* get a list of all S-EL1 IRQs from the platform */

	/* register interrupt handler */
	flags = 0;
	set_interrupt_rm_flag(flags, NON_SECURE);
	ret = register_interrupt_type_handler(INTR_TYPE_S_EL1,
					      tlkd_interrupt_handler,
					      flags);
	if (ret != 0) {
		ERROR("failed to register tlkd interrupt handler (%d)\n", ret);
	}

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	/*
	 * All TLK SPD initialization done. Now register our init function
	 * with BL31 for deferred invocation
	 */
	bl31_register_bl32_init(&tlkd_init);

	return 0;
}

/*******************************************************************************
 * This function passes control to the Secure Payload image (BL32) for the first
 * time on the primary cpu after a cold boot. It assumes that a valid secure
 * context has already been created by tlkd_setup() which can be directly
 * used. This function performs a synchronous entry into the Secure payload.
 * The SP passes control back to this routine through a SMC.
 ******************************************************************************/
165
static int32_t tlkd_init(void)
166
167
168
169
170
171
172
173
174
175
{
	entry_point_info_t *tlk_entry_point;

	/*
	 * Get information about the Secure Payload (BL32) image. Its
	 * absence is a critical failure.
	 */
	tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
	assert(tlk_entry_point);

176
	cm_init_my_context(tlk_entry_point);
177

178
179
180
181
182
183
	/*
	 * TLK runs only on a single CPU. Store the value of the boot
	 * CPU for sanity checking later.
	 */
	boot_cpu = plat_my_core_pos();

184
185
186
187
188
189
190
191
192
193
194
195
196
197
	/*
	 * Arrange for an entry into the test secure payload.
	 */
	return tlkd_synchronous_sp_entry(&tlk_ctx);
}

/*******************************************************************************
 * This function is responsible for handling all SMCs in the Trusted OS/App
 * range from the non-secure state as defined in the SMC Calling Convention
 * Document. It is also responsible for communicating with the Secure payload
 * to delegate work and return results back to the non-secure state. Lastly it
 * will also return any information that the secure payload needs to do the
 * work assigned to it.
 ******************************************************************************/
198
199
200
201
202
static uintptr_t tlkd_smc_handler(uint32_t smc_fid,
			 u_register_t x1,
			 u_register_t x2,
			 u_register_t x3,
			 u_register_t x4,
203
204
			 void *cookie,
			 void *handle,
205
			 u_register_t flags)
206
{
207
	cpu_context_t *ns_cpu_context;
208
	gp_regs_t *gp_regs;
209
	uint32_t ns;
210
	uint64_t par;
211
212
213
214

	/* Passing a NULL context is a critical programming error */
	assert(handle);

215
216
	/* These SMCs are only supported by a single CPU */
	if (boot_cpu != plat_my_core_pos())
217
218
		SMC_RET1(handle, SMC_UNK);

219
220
221
222
223
	/* Determine which security state this SMC originated from */
	ns = is_caller_non_secure(flags);

	switch (smc_fid) {

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
	/*
	 * This function ID is used by SP to indicate that it was
	 * preempted by a non-secure world IRQ.
	 */
	case TLK_PREEMPTED:

		if (ns)
			SMC_RET1(handle, SMC_UNK);

		assert(handle == cm_get_context(SECURE));
		cm_el1_sysregs_context_save(SECURE);

		/* Get a reference to the non-secure context */
		ns_cpu_context = cm_get_context(NON_SECURE);
		assert(ns_cpu_context);

		/*
		 * Restore non-secure state. There is no need to save the
		 * secure system register context since the SP was supposed
		 * to preserve it during S-EL1 interrupt handling.
		 */
		cm_el1_sysregs_context_restore(NON_SECURE);
		cm_set_next_eret_context(NON_SECURE);

248
		SMC_RET1(ns_cpu_context, x1);
249

250
251
252
253
254
255
256
257
	/*
	 * This is a request from the non-secure context to:
	 *
	 * a. register shared memory with the SP for storing it's
	 *    activity logs.
	 * b. register shared memory with the SP for passing args
	 *    required for maintaining sessions with the Trusted
	 *    Applications.
Mihir Joshi's avatar
Mihir Joshi committed
258
259
260
261
262
263
	 * c. register shared persistent buffers for secure storage
	 * d. register NS DRAM ranges passed by Cboot
	 * e. register Root of Trust parameters from Cboot for Verified Boot
	 * f. open/close sessions
	 * g. issue commands to the Trusted Apps
	 * h. resume the preempted yielding SMC call.
264
265
266
	 */
	case TLK_REGISTER_LOGBUF:
	case TLK_REGISTER_REQBUF:
Mihir Joshi's avatar
Mihir Joshi committed
267
268
269
	case TLK_SS_REGISTER_HANDLER:
	case TLK_REGISTER_NS_DRAM_RANGES:
	case TLK_SET_ROOT_OF_TRUST:
270
271
272
273
	case TLK_OPEN_TA_SESSION:
	case TLK_CLOSE_TA_SESSION:
	case TLK_TA_LAUNCH_OP:
	case TLK_TA_SEND_EVENT:
274
	case TLK_RESUME_FID:
275

276
		if (!ns)
277
278
279
280
281
282
283
284
285
286
			SMC_RET1(handle, SMC_UNK);

		/*
		 * This is a fresh request from the non-secure client.
		 * The parameters are in x1 and x2. Figure out which
		 * registers need to be preserved, save the non-secure
		 * state and send the request to the secure payload.
		 */
		assert(handle == cm_get_context(NON_SECURE));

287
		/*
288
		 * Check if we are already processing a yielding SMC
289
290
291
292
		 * call. Of all the supported fids, only the "resume"
		 * fid expects the flag to be set.
		 */
		if (smc_fid == TLK_RESUME_FID) {
293
			if (!get_yield_smc_active_flag(tlk_ctx.state))
294
295
				SMC_RET1(handle, SMC_UNK);
		} else {
296
			if (get_yield_smc_active_flag(tlk_ctx.state))
297
298
				SMC_RET1(handle, SMC_UNK);
		}
299
300
301
302
303
304
305
306
307
308
309

		cm_el1_sysregs_context_save(NON_SECURE);

		/*
		 * Verify if there is a valid context to use.
		 */
		assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));

		/*
		 * Mark the SP state as active.
		 */
310
		set_yield_smc_active_flag(tlk_ctx.state);
311
312
313
314
315
316
317

		/*
		 * We are done stashing the non-secure context. Ask the
		 * secure payload to do the work now.
		 */
		cm_el1_sysregs_context_restore(SECURE);
		cm_set_next_eret_context(SECURE);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

		/*
		 * TLK is a 32-bit Trusted OS and so expects the SMC
		 * arguments via r0-r7. TLK expects the monitor frame
		 * registers to be 64-bits long. Hence, we pass x0 in
		 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
		 *
		 * As smc_fid is a uint32 value, r1 contains 0.
		 */
		gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
		write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
		write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
		write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
		write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
		SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
			(uint32_t)(x1 >> 32));
334

335
	/*
336
337
338
339
340
341
	 * Translate NS/EL1-S virtual addresses.
	 *
	 * x1 = virtual address
	 * x3 = type (NS/S)
	 *
	 * Returns PA:lo in r0, PA:hi in r1.
342
343
	 */
	case TLK_VA_TRANSLATE:
344
345
346

		/* Should be invoked only by secure world */
		if (ns)
347
348
			SMC_RET1(handle, SMC_UNK);

349
350
351
		/* NS virtual addresses are 64-bit long */
		if (x3 & TLK_TRANSLATE_NS_VADDR)
			x1 = (uint32_t)x1 | (x2 << 32);
352

353
354
355
356
357
358
359
		if (!x1)
			SMC_RET1(handle, SMC_UNK);

		/*
		 * TODO: Sanity check x1. This would require platform
		 * support.
		 */
360

361
362
		/* virtual address and type: ns/s */
		par = tlkd_va_translate(x1, x3);
363

364
365
		/* return physical address in r0-r1 */
		SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
366

367
368
	/*
	 * This is a request from the SP to mark completion of
369
	 * a yielding function ID.
370
371
	 */
	case TLK_REQUEST_DONE:
372
		if (ns)
373
374
375
376
377
			SMC_RET1(handle, SMC_UNK);

		/*
		 * Mark the SP state as inactive.
		 */
378
		clr_yield_smc_active_flag(tlk_ctx.state);
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

		/* Get a reference to the non-secure context */
		ns_cpu_context = cm_get_context(NON_SECURE);
		assert(ns_cpu_context);

		/*
		 * This is a request completion SMC and we must switch to
		 * the non-secure world to pass the result.
		 */
		cm_el1_sysregs_context_save(SECURE);

		/*
		 * We are done stashing the secure context. Switch to the
		 * non-secure context and return the result.
		 */
		cm_el1_sysregs_context_restore(NON_SECURE);
		cm_set_next_eret_context(NON_SECURE);
396
		SMC_RET1(ns_cpu_context, x1);
397

398
399
400
401
402
	/*
	 * This function ID is used only by the SP to indicate it has
	 * finished initialising itself after a cold boot
	 */
	case TLK_ENTRY_DONE:
403
		if (ns)
404
405
406
407
			SMC_RET1(handle, SMC_UNK);

		/*
		 * SP has been successfully initialized. Register power
Paul Beesley's avatar
Paul Beesley committed
408
		 * management hooks with PSCI
409
410
411
412
413
414
415
416
417
		 */
		psci_register_spd_pm_hook(&tlkd_pm_ops);

		/*
		 * TLK reports completion. The SPD must have initiated
		 * the original request through a synchronous entry
		 * into the SP. Jump back to the original C runtime
		 * context.
		 */
418
		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
419
		break;
420

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
	/*
	 * These function IDs are used only by TLK to indicate it has
	 * finished:
	 * 1. suspending itself after an earlier psci cpu_suspend
	 *    request.
	 * 2. resuming itself after an earlier psci cpu_suspend
	 *    request.
	 * 3. powering down after an earlier psci system_off/system_reset
	 *    request.
	 */
	case TLK_SUSPEND_DONE:
	case TLK_RESUME_DONE:

		if (ns)
			SMC_RET1(handle, SMC_UNK);

		/*
		 * TLK reports completion. TLKD must have initiated the
		 * original request through a synchronous entry into the SP.
		 * Jump back to the original C runtime context, and pass x1 as
		 * return value to the caller
		 */
		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
444
		break;
445

446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
	/*
	 * This function ID is used by SP to indicate that it has completed
	 * handling the secure interrupt.
	 */
	case TLK_IRQ_DONE:

		if (ns)
			SMC_RET1(handle, SMC_UNK);

		assert(handle == cm_get_context(SECURE));

		/* save secure world context */
		cm_el1_sysregs_context_save(SECURE);

		/* Get a reference to the non-secure context */
		ns_cpu_context = cm_get_context(NON_SECURE);
		assert(ns_cpu_context);

		/*
		 * Restore non-secure state. There is no need to save the
		 * secure system register context since the SP was supposed
		 * to preserve it during S-EL1 interrupt handling.
		 */
		cm_el1_sysregs_context_restore(NON_SECURE);
		cm_set_next_eret_context(NON_SECURE);

		SMC_RET0(ns_cpu_context);

474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
	/*
	 * Return the number of service function IDs implemented to
	 * provide service to non-secure
	 */
	case TOS_CALL_COUNT:
		SMC_RET1(handle, TLK_NUM_FID);

	/*
	 * Return TLK's UID to the caller
	 */
	case TOS_UID:
		SMC_UUID_RET(handle, tlk_uuid);

	/*
	 * Return the version of current implementation
	 */
	case TOS_CALL_VERSION:
		SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);

	default:
Mihir Joshi's avatar
Mihir Joshi committed
494
		WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid);
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
		break;
	}

	SMC_RET1(handle, SMC_UNK);
}

/* Define a SPD runtime service descriptor for fast SMC calls */
DECLARE_RT_SVC(
	tlkd_tos_fast,

	OEN_TOS_START,
	OEN_TOS_END,
	SMC_TYPE_FAST,
	tlkd_setup,
	tlkd_smc_handler
);

512
/* Define a SPD runtime service descriptor for yielding SMC calls */
513
514
515
516
517
DECLARE_RT_SVC(
	tlkd_tos_std,

	OEN_TOS_START,
	OEN_TOS_END,
518
	SMC_TYPE_YIELD,
519
520
521
	NULL,
	tlkd_smc_handler
);
522
523
524
525
526
527
528
529
530
531
532
533

/* Define a SPD runtime service descriptor for fast SMC calls */
DECLARE_RT_SVC(
	tlkd_tap_fast,

	OEN_TAP_START,
	OEN_TAP_END,
	SMC_TYPE_FAST,
	NULL,
	tlkd_smc_handler
);

534
/* Define a SPD runtime service descriptor for yielding SMC calls */
535
536
537
538
539
DECLARE_RT_SVC(
	tlkd_tap_std,

	OEN_TAP_START,
	OEN_TAP_END,
540
	SMC_TYPE_YIELD,
541
542
543
	NULL,
	tlkd_smc_handler
);