xref: /rk3399_ARM-atf/services/spd/tlkd/tlkd_main.c (revision fd650ff61b80a2155002def233ffddb439e3c071)
1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*******************************************************************************
32  * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
33  * plug-in component to the Secure Monitor, registered as a runtime service. The
34  * SPD is expected to be a functional extension of the Secure Payload (SP) that
35  * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
36  * the Trusted OS/Applications range to the dispatcher. The SPD will either
37  * handle the request locally or delegate it to the Secure Payload. It is also
38  * responsible for initialising and maintaining communication with the SP.
39  ******************************************************************************/
40 #include <arch_helpers.h>
41 #include <assert.h>
42 #include <bl_common.h>
43 #include <bl31.h>
44 #include <context_mgmt.h>
45 #include <debug.h>
46 #include <errno.h>
47 #include <platform.h>
48 #include <runtime_svc.h>
49 #include <stddef.h>
50 #include <tlk.h>
51 #include <uuid.h>
52 #include "tlkd_private.h"
53 
54 extern const spd_pm_ops_t tlkd_pm_ops;
55 
56 /*******************************************************************************
57  * Array to keep track of per-cpu Secure Payload state
58  ******************************************************************************/
59 static tlk_context_t tlk_ctx;
60 
61 /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
62 DEFINE_SVC_UUID(tlk_uuid,
63 		0xbd11e9c9, 0x2bba, 0x52ee, 0xb1, 0x72,
64 		0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
65 
66 int32_t tlkd_init(void);
67 
68 /*******************************************************************************
69  * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
70  * (aarch32/aarch64) if not already known and initialises the context for entry
71  * into the SP for its initialisation.
72  ******************************************************************************/
73 int32_t tlkd_setup(void)
74 {
75 	entry_point_info_t *tlk_ep_info;
76 
77 	/*
78 	 * Get information about the Secure Payload (BL32) image. Its
79 	 * absence is a critical failure.
80 	 */
81 	tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
82 	if (!tlk_ep_info) {
83 		WARN("No SP provided. Booting device without SP"
84 			" initialization. SMC`s destined for SP"
85 			" will return SMC_UNK\n");
86 		return 1;
87 	}
88 
89 	/*
90 	 * If there's no valid entry point for SP, we return a non-zero value
91 	 * signalling failure initializing the service. We bail out without
92 	 * registering any handlers
93 	 */
94 	if (!tlk_ep_info->pc)
95 		return 1;
96 
97 	/*
98 	 * Inspect the SP image's SPSR and determine it's execution state
99 	 * i.e whether AArch32 or AArch64.
100 	 */
101 	tlkd_init_tlk_ep_state(tlk_ep_info,
102 		(tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
103 		tlk_ep_info->pc,
104 		&tlk_ctx);
105 
106 	/*
107 	 * All TLK SPD initialization done. Now register our init function
108 	 * with BL31 for deferred invocation
109 	 */
110 	bl31_register_bl32_init(&tlkd_init);
111 
112 	return 0;
113 }
114 
115 /*******************************************************************************
116  * This function passes control to the Secure Payload image (BL32) for the first
117  * time on the primary cpu after a cold boot. It assumes that a valid secure
118  * context has already been created by tlkd_setup() which can be directly
119  * used. This function performs a synchronous entry into the Secure payload.
120  * The SP passes control back to this routine through a SMC.
121  ******************************************************************************/
122 int32_t tlkd_init(void)
123 {
124 	entry_point_info_t *tlk_entry_point;
125 
126 	/*
127 	 * Get information about the Secure Payload (BL32) image. Its
128 	 * absence is a critical failure.
129 	 */
130 	tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
131 	assert(tlk_entry_point);
132 
133 	cm_init_my_context(tlk_entry_point);
134 
135 	/*
136 	 * Arrange for an entry into the test secure payload.
137 	 */
138 	return tlkd_synchronous_sp_entry(&tlk_ctx);
139 }
140 
141 /*******************************************************************************
142  * This function is responsible for handling all SMCs in the Trusted OS/App
143  * range from the non-secure state as defined in the SMC Calling Convention
144  * Document. It is also responsible for communicating with the Secure payload
145  * to delegate work and return results back to the non-secure state. Lastly it
146  * will also return any information that the secure payload needs to do the
147  * work assigned to it.
148  ******************************************************************************/
149 uint64_t tlkd_smc_handler(uint32_t smc_fid,
150 			 uint64_t x1,
151 			 uint64_t x2,
152 			 uint64_t x3,
153 			 uint64_t x4,
154 			 void *cookie,
155 			 void *handle,
156 			 uint64_t flags)
157 {
158 	cpu_context_t *ns_cpu_context;
159 	gp_regs_t *gp_regs;
160 	uint32_t ns;
161 	uint64_t par;
162 
163 	/* Passing a NULL context is a critical programming error */
164 	assert(handle);
165 
166 	/* These SMCs are only supported by CPU0 */
167 	if ((read_mpidr() & MPIDR_CPU_MASK) != 0)
168 		SMC_RET1(handle, SMC_UNK);
169 
170 	/* Determine which security state this SMC originated from */
171 	ns = is_caller_non_secure(flags);
172 
173 	switch (smc_fid) {
174 
175 	/*
176 	 * This function ID is used by SP to indicate that it was
177 	 * preempted by a non-secure world IRQ.
178 	 */
179 	case TLK_PREEMPTED:
180 
181 		if (ns)
182 			SMC_RET1(handle, SMC_UNK);
183 
184 		assert(handle == cm_get_context(SECURE));
185 		cm_el1_sysregs_context_save(SECURE);
186 
187 		/* Get a reference to the non-secure context */
188 		ns_cpu_context = cm_get_context(NON_SECURE);
189 		assert(ns_cpu_context);
190 
191 		/*
192 		 * Restore non-secure state. There is no need to save the
193 		 * secure system register context since the SP was supposed
194 		 * to preserve it during S-EL1 interrupt handling.
195 		 */
196 		cm_el1_sysregs_context_restore(NON_SECURE);
197 		cm_set_next_eret_context(NON_SECURE);
198 
199 		SMC_RET1(ns_cpu_context, x1);
200 
201 	/*
202 	 * Request from non secure world to resume the preempted
203 	 * Standard SMC call.
204 	 */
205 	case TLK_RESUME_FID:
206 
207 		/* RESUME should be invoked only by normal world */
208 		if (!ns)
209 			SMC_RET1(handle, SMC_UNK);
210 
211 		/*
212 		 * This is a resume request from the non-secure client.
213 		 * save the non-secure state and send the request to
214 		 * the secure payload.
215 		 */
216 		assert(handle == cm_get_context(NON_SECURE));
217 
218 		/* Check if we are already preempted before resume */
219 		if (!get_std_smc_active_flag(tlk_ctx.state))
220 			SMC_RET1(handle, SMC_UNK);
221 
222 		cm_el1_sysregs_context_save(NON_SECURE);
223 
224 		/*
225 		 * We are done stashing the non-secure context. Ask the
226 		 * secure payload to do the work now.
227 		 */
228 
229 		/* We just need to return to the preempted point in
230 		 * SP and the execution will resume as normal.
231 		 */
232 		cm_el1_sysregs_context_restore(SECURE);
233 		cm_set_next_eret_context(SECURE);
234 		SMC_RET0(handle);
235 
236 	/*
237 	 * This is a request from the non-secure context to:
238 	 *
239 	 * a. register shared memory with the SP for storing it's
240 	 *    activity logs.
241 	 * b. register shared memory with the SP for passing args
242 	 *    required for maintaining sessions with the Trusted
243 	 *    Applications.
244 	 * c. open/close sessions
245 	 * d. issue commands to the Trusted Apps
246 	 */
247 	case TLK_REGISTER_LOGBUF:
248 	case TLK_REGISTER_REQBUF:
249 	case TLK_OPEN_TA_SESSION:
250 	case TLK_CLOSE_TA_SESSION:
251 	case TLK_TA_LAUNCH_OP:
252 	case TLK_TA_SEND_EVENT:
253 
254 		if (!ns)
255 			SMC_RET1(handle, SMC_UNK);
256 
257 		/*
258 		 * This is a fresh request from the non-secure client.
259 		 * The parameters are in x1 and x2. Figure out which
260 		 * registers need to be preserved, save the non-secure
261 		 * state and send the request to the secure payload.
262 		 */
263 		assert(handle == cm_get_context(NON_SECURE));
264 
265 		/* Check if we are already preempted */
266 		if (get_std_smc_active_flag(tlk_ctx.state))
267 			SMC_RET1(handle, SMC_UNK);
268 
269 		cm_el1_sysregs_context_save(NON_SECURE);
270 
271 		/*
272 		 * Verify if there is a valid context to use.
273 		 */
274 		assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
275 
276 		/*
277 		 * Mark the SP state as active.
278 		 */
279 		set_std_smc_active_flag(tlk_ctx.state);
280 
281 		/*
282 		 * We are done stashing the non-secure context. Ask the
283 		 * secure payload to do the work now.
284 		 */
285 		cm_el1_sysregs_context_restore(SECURE);
286 		cm_set_next_eret_context(SECURE);
287 
288 		/*
289 		 * TLK is a 32-bit Trusted OS and so expects the SMC
290 		 * arguments via r0-r7. TLK expects the monitor frame
291 		 * registers to be 64-bits long. Hence, we pass x0 in
292 		 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
293 		 *
294 		 * As smc_fid is a uint32 value, r1 contains 0.
295 		 */
296 		gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
297 		write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
298 		write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
299 		write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
300 		write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
301 		SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
302 			(uint32_t)(x1 >> 32));
303 
304 	/*
305 	 * Translate NS/EL1-S virtual addresses.
306 	 *
307 	 * x1 = virtual address
308 	 * x3 = type (NS/S)
309 	 *
310 	 * Returns PA:lo in r0, PA:hi in r1.
311 	 */
312 	case TLK_VA_TRANSLATE:
313 
314 		/* Should be invoked only by secure world */
315 		if (ns)
316 			SMC_RET1(handle, SMC_UNK);
317 
318 		/* NS virtual addresses are 64-bit long */
319 		if (x3 & TLK_TRANSLATE_NS_VADDR)
320 			x1 = (uint32_t)x1 | (x2 << 32);
321 
322 		if (!x1)
323 			SMC_RET1(handle, SMC_UNK);
324 
325 		/*
326 		 * TODO: Sanity check x1. This would require platform
327 		 * support.
328 		 */
329 
330 		/* virtual address and type: ns/s */
331 		par = tlkd_va_translate(x1, x3);
332 
333 		/* return physical address in r0-r1 */
334 		SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
335 
336 	/*
337 	 * This is a request from the SP to mark completion of
338 	 * a standard function ID.
339 	 */
340 	case TLK_REQUEST_DONE:
341 		if (ns)
342 			SMC_RET1(handle, SMC_UNK);
343 
344 		/*
345 		 * Mark the SP state as inactive.
346 		 */
347 		clr_std_smc_active_flag(tlk_ctx.state);
348 
349 		/* Get a reference to the non-secure context */
350 		ns_cpu_context = cm_get_context(NON_SECURE);
351 		assert(ns_cpu_context);
352 
353 		/*
354 		 * This is a request completion SMC and we must switch to
355 		 * the non-secure world to pass the result.
356 		 */
357 		cm_el1_sysregs_context_save(SECURE);
358 
359 		/*
360 		 * We are done stashing the secure context. Switch to the
361 		 * non-secure context and return the result.
362 		 */
363 		cm_el1_sysregs_context_restore(NON_SECURE);
364 		cm_set_next_eret_context(NON_SECURE);
365 		SMC_RET1(ns_cpu_context, x1);
366 
367 	/*
368 	 * This function ID is used only by the SP to indicate it has
369 	 * finished initialising itself after a cold boot
370 	 */
371 	case TLK_ENTRY_DONE:
372 		if (ns)
373 			SMC_RET1(handle, SMC_UNK);
374 
375 		/*
376 		 * SP has been successfully initialized. Register power
377 		 * managemnt hooks with PSCI
378 		 */
379 		psci_register_spd_pm_hook(&tlkd_pm_ops);
380 
381 		/*
382 		 * TLK reports completion. The SPD must have initiated
383 		 * the original request through a synchronous entry
384 		 * into the SP. Jump back to the original C runtime
385 		 * context.
386 		 */
387 		tlkd_synchronous_sp_exit(&tlk_ctx, x1);
388 
389 	/*
390 	 * Return the number of service function IDs implemented to
391 	 * provide service to non-secure
392 	 */
393 	case TOS_CALL_COUNT:
394 		SMC_RET1(handle, TLK_NUM_FID);
395 
396 	/*
397 	 * Return TLK's UID to the caller
398 	 */
399 	case TOS_UID:
400 		SMC_UUID_RET(handle, tlk_uuid);
401 
402 	/*
403 	 * Return the version of current implementation
404 	 */
405 	case TOS_CALL_VERSION:
406 		SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
407 
408 	default:
409 		break;
410 	}
411 
412 	SMC_RET1(handle, SMC_UNK);
413 }
414 
415 /* Define a SPD runtime service descriptor for fast SMC calls */
416 DECLARE_RT_SVC(
417 	tlkd_tos_fast,
418 
419 	OEN_TOS_START,
420 	OEN_TOS_END,
421 	SMC_TYPE_FAST,
422 	tlkd_setup,
423 	tlkd_smc_handler
424 );
425 
426 /* Define a SPD runtime service descriptor for standard SMC calls */
427 DECLARE_RT_SVC(
428 	tlkd_tos_std,
429 
430 	OEN_TOS_START,
431 	OEN_TOS_END,
432 	SMC_TYPE_STD,
433 	NULL,
434 	tlkd_smc_handler
435 );
436 
437 /* Define a SPD runtime service descriptor for fast SMC calls */
438 DECLARE_RT_SVC(
439 	tlkd_tap_fast,
440 
441 	OEN_TAP_START,
442 	OEN_TAP_END,
443 	SMC_TYPE_FAST,
444 	NULL,
445 	tlkd_smc_handler
446 );
447 
448 /* Define a SPD runtime service descriptor for standard SMC calls */
449 DECLARE_RT_SVC(
450 	tlkd_tap_std,
451 
452 	OEN_TAP_START,
453 	OEN_TAP_END,
454 	SMC_TYPE_STD,
455 	NULL,
456 	tlkd_smc_handler
457 );
458