blob: 9c9de9102ff35aaf281a4ef76634d03d0e87bda3 [file] [log] [blame]
Varun Wadekarc1d2a282016-11-08 15:46:48 -08001/*
Varun Wadekarbd3c9532017-02-16 18:14:37 -08002 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekarc1d2a282016-11-08 15:46:48 -08003 *
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
Anthony Zhou700ebe52015-10-31 06:03:41 +080031#include <arch_helpers.h>
32#include <assert.h> /* for context_mgmt.h */
Varun Wadekarc1d2a282016-11-08 15:46:48 -080033#include <bl_common.h>
34#include <bl31.h>
35#include <context_mgmt.h>
36#include <debug.h>
37#include <interrupt_mgmt.h>
38#include <platform.h>
39#include <runtime_svc.h>
40#include <string.h>
41
42#include "smcall.h"
43#include "sm_err.h"
44
Anthony Zhou700ebe52015-10-31 06:03:41 +080045/* macro to check if Hypervisor is enabled in the HCR_EL2 register */
46#define HYP_ENABLE_FLAG 0x286001
47
Wayne Lincd712fd2016-05-24 15:28:42 -070048/* length of Trusty's input parameters (in bytes) */
49#define TRUSTY_PARAMS_LEN_BYTES (4096*2)
50
Varun Wadekarc1d2a282016-11-08 15:46:48 -080051struct trusty_stack {
52 uint8_t space[PLATFORM_STACK_SIZE] __aligned(16);
Varun Wadekarbd3c9532017-02-16 18:14:37 -080053 uint32_t end;
Varun Wadekarc1d2a282016-11-08 15:46:48 -080054};
55
56struct trusty_cpu_ctx {
57 cpu_context_t cpu_ctx;
58 void *saved_sp;
59 uint32_t saved_security_state;
60 int fiq_handler_active;
61 uint64_t fiq_handler_pc;
62 uint64_t fiq_handler_cpsr;
63 uint64_t fiq_handler_sp;
64 uint64_t fiq_pc;
65 uint64_t fiq_cpsr;
66 uint64_t fiq_sp_el1;
67 gp_regs_t fiq_gpregs;
68 struct trusty_stack secure_stack;
69};
70
71struct args {
72 uint64_t r0;
73 uint64_t r1;
74 uint64_t r2;
75 uint64_t r3;
Anthony Zhou700ebe52015-10-31 06:03:41 +080076 uint64_t r4;
77 uint64_t r5;
78 uint64_t r6;
79 uint64_t r7;
Varun Wadekarc1d2a282016-11-08 15:46:48 -080080};
81
82struct trusty_cpu_ctx trusty_cpu_ctx[PLATFORM_CORE_COUNT];
83
84struct args trusty_init_context_stack(void **sp, void *new_stack);
Anthony Zhou700ebe52015-10-31 06:03:41 +080085struct args trusty_context_switch_helper(void **sp, void *smc_params);
Varun Wadekarc1d2a282016-11-08 15:46:48 -080086
Anthony Zhou43384822016-04-20 10:16:48 +080087static uint32_t current_vmid;
88
Varun Wadekarc1d2a282016-11-08 15:46:48 -080089static struct trusty_cpu_ctx *get_trusty_ctx(void)
90{
91 return &trusty_cpu_ctx[plat_my_core_pos()];
92}
93
Anthony Zhou700ebe52015-10-31 06:03:41 +080094static uint32_t is_hypervisor_mode(void)
95{
96 uint64_t hcr = read_hcr();
97
98 return !!(hcr & HYP_ENABLE_FLAG);
99}
100
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800101static struct args trusty_context_switch(uint32_t security_state, uint64_t r0,
102 uint64_t r1, uint64_t r2, uint64_t r3)
103{
104 struct args ret;
105 struct trusty_cpu_ctx *ctx = get_trusty_ctx();
Anthony Zhou700ebe52015-10-31 06:03:41 +0800106 struct trusty_cpu_ctx *ctx_smc;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800107
108 assert(ctx->saved_security_state != security_state);
109
Anthony Zhou700ebe52015-10-31 06:03:41 +0800110 ret.r7 = 0;
111 if (is_hypervisor_mode()) {
112 /* According to the ARM DEN0028A spec, VMID is stored in x7 */
113 ctx_smc = cm_get_context(NON_SECURE);
114 assert(ctx_smc);
115 ret.r7 = SMC_GET_GP(ctx_smc, CTX_GPREG_X7);
116 }
117 /* r4, r5, r6 reserved for future use. */
118 ret.r6 = 0;
119 ret.r5 = 0;
120 ret.r4 = 0;
121 ret.r3 = r3;
122 ret.r2 = r2;
123 ret.r1 = r1;
124 ret.r0 = r0;
125
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800126 cm_el1_sysregs_context_save(security_state);
127
128 ctx->saved_security_state = security_state;
Anthony Zhou700ebe52015-10-31 06:03:41 +0800129 ret = trusty_context_switch_helper(&ctx->saved_sp, &ret);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800130
131 assert(ctx->saved_security_state == !security_state);
132
133 cm_el1_sysregs_context_restore(security_state);
134 cm_set_next_eret_context(security_state);
135
136 return ret;
137}
138
139static uint64_t trusty_fiq_handler(uint32_t id,
140 uint32_t flags,
141 void *handle,
142 void *cookie)
143{
144 struct args ret;
145 struct trusty_cpu_ctx *ctx = get_trusty_ctx();
146
147 assert(!is_caller_secure(flags));
148
149 ret = trusty_context_switch(NON_SECURE, SMC_FC_FIQ_ENTER, 0, 0, 0);
150 if (ret.r0) {
151 SMC_RET0(handle);
152 }
153
154 if (ctx->fiq_handler_active) {
155 INFO("%s: fiq handler already active\n", __func__);
156 SMC_RET0(handle);
157 }
158
159 ctx->fiq_handler_active = 1;
160 memcpy(&ctx->fiq_gpregs, get_gpregs_ctx(handle), sizeof(ctx->fiq_gpregs));
161 ctx->fiq_pc = SMC_GET_EL3(handle, CTX_ELR_EL3);
162 ctx->fiq_cpsr = SMC_GET_EL3(handle, CTX_SPSR_EL3);
163 ctx->fiq_sp_el1 = read_ctx_reg(get_sysregs_ctx(handle), CTX_SP_EL1);
164
165 write_ctx_reg(get_sysregs_ctx(handle), CTX_SP_EL1, ctx->fiq_handler_sp);
166 cm_set_elr_spsr_el3(NON_SECURE, ctx->fiq_handler_pc, ctx->fiq_handler_cpsr);
167
168 SMC_RET0(handle);
169}
170
171static uint64_t trusty_set_fiq_handler(void *handle, uint64_t cpu,
172 uint64_t handler, uint64_t stack)
173{
174 struct trusty_cpu_ctx *ctx;
175
176 if (cpu >= PLATFORM_CORE_COUNT) {
177 ERROR("%s: cpu %ld >= %d\n", __func__, cpu, PLATFORM_CORE_COUNT);
178 return SM_ERR_INVALID_PARAMETERS;
179 }
180
181 ctx = &trusty_cpu_ctx[cpu];
182 ctx->fiq_handler_pc = handler;
183 ctx->fiq_handler_cpsr = SMC_GET_EL3(handle, CTX_SPSR_EL3);
184 ctx->fiq_handler_sp = stack;
185
186 SMC_RET1(handle, 0);
187}
188
189static uint64_t trusty_get_fiq_regs(void *handle)
190{
191 struct trusty_cpu_ctx *ctx = get_trusty_ctx();
192 uint64_t sp_el0 = read_ctx_reg(&ctx->fiq_gpregs, CTX_GPREG_SP_EL0);
193
194 SMC_RET4(handle, ctx->fiq_pc, ctx->fiq_cpsr, sp_el0, ctx->fiq_sp_el1);
195}
196
197static uint64_t trusty_fiq_exit(void *handle, uint64_t x1, uint64_t x2, uint64_t x3)
198{
199 struct args ret;
200 struct trusty_cpu_ctx *ctx = get_trusty_ctx();
201
202 if (!ctx->fiq_handler_active) {
203 NOTICE("%s: fiq handler not active\n", __func__);
204 SMC_RET1(handle, SM_ERR_INVALID_PARAMETERS);
205 }
206
207 ret = trusty_context_switch(NON_SECURE, SMC_FC_FIQ_EXIT, 0, 0, 0);
208 if (ret.r0 != 1) {
209 INFO("%s(%p) SMC_FC_FIQ_EXIT returned unexpected value, %ld\n",
210 __func__, handle, ret.r0);
211 }
212
213 /*
214 * Restore register state to state recorded on fiq entry.
215 *
216 * x0, sp_el1, pc and cpsr need to be restored because el1 cannot
217 * restore them.
218 *
219 * x1-x4 and x8-x17 need to be restored here because smc_handler64
220 * corrupts them (el1 code also restored them).
221 */
222 memcpy(get_gpregs_ctx(handle), &ctx->fiq_gpregs, sizeof(ctx->fiq_gpregs));
223 ctx->fiq_handler_active = 0;
224 write_ctx_reg(get_sysregs_ctx(handle), CTX_SP_EL1, ctx->fiq_sp_el1);
225 cm_set_elr_spsr_el3(NON_SECURE, ctx->fiq_pc, ctx->fiq_cpsr);
226
227 SMC_RET0(handle);
228}
229
230static uint64_t trusty_smc_handler(uint32_t smc_fid,
231 uint64_t x1,
232 uint64_t x2,
233 uint64_t x3,
234 uint64_t x4,
235 void *cookie,
236 void *handle,
237 uint64_t flags)
238{
239 struct args ret;
Anthony Zhou43384822016-04-20 10:16:48 +0800240 uint32_t vmid = 0;
Varun Wadekar528a7922016-09-29 16:08:16 -0700241 entry_point_info_t *ep_info = bl31_plat_get_next_image_ep_info(SECURE);
242
243 /*
244 * Return success for SET_ROT_PARAMS if Trusty is not present, as
245 * Verified Boot is not even supported and returning success here
246 * would not compromise the boot process.
247 */
248 if (!ep_info && (smc_fid == SMC_SC_SET_ROT_PARAMS)) {
249 SMC_RET1(handle, 0);
250 } else if (!ep_info) {
251 SMC_RET1(handle, SMC_UNK);
252 }
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800253
254 if (is_caller_secure(flags)) {
255 if (smc_fid == SMC_SC_NS_RETURN) {
256 ret = trusty_context_switch(SECURE, x1, 0, 0, 0);
Anthony Zhou700ebe52015-10-31 06:03:41 +0800257 SMC_RET8(handle, ret.r0, ret.r1, ret.r2, ret.r3,
258 ret.r4, ret.r5, ret.r6, ret.r7);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800259 }
260 INFO("%s (0x%x, 0x%lx, 0x%lx, 0x%lx, 0x%lx, %p, %p, 0x%lx) \
261 cpu %d, unknown smc\n",
262 __func__, smc_fid, x1, x2, x3, x4, cookie, handle, flags,
263 plat_my_core_pos());
264 SMC_RET1(handle, SMC_UNK);
265 } else {
266 switch (smc_fid) {
267 case SMC_FC64_SET_FIQ_HANDLER:
268 return trusty_set_fiq_handler(handle, x1, x2, x3);
269 case SMC_FC64_GET_FIQ_REGS:
270 return trusty_get_fiq_regs(handle);
271 case SMC_FC_FIQ_EXIT:
272 return trusty_fiq_exit(handle, x1, x2, x3);
273 default:
Anthony Zhou43384822016-04-20 10:16:48 +0800274 if (is_hypervisor_mode())
275 vmid = SMC_GET_GP(handle, CTX_GPREG_X7);
276
277 if ((current_vmid != 0) && (current_vmid != vmid)) {
278 /* This message will cause SMC mechanism
279 * abnormal in multi-guest environment.
280 * Change it to WARN in case you need it.
281 */
282 VERBOSE("Previous SMC not finished.\n");
283 SMC_RET1(handle, SM_ERR_BUSY);
284 }
285 current_vmid = vmid;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800286 ret = trusty_context_switch(NON_SECURE, smc_fid, x1,
287 x2, x3);
Anthony Zhou43384822016-04-20 10:16:48 +0800288 current_vmid = 0;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800289 SMC_RET1(handle, ret.r0);
290 }
291 }
292}
293
294static int32_t trusty_init(void)
295{
Sandrine Bailleuxf148e6f2016-11-23 10:53:07 +0000296 void el3_exit(void);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800297 entry_point_info_t *ep_info;
Anthony Zhou700ebe52015-10-31 06:03:41 +0800298 struct args zero_args = {0};
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800299 struct trusty_cpu_ctx *ctx = get_trusty_ctx();
300 uint32_t cpu = plat_my_core_pos();
301 int reg_width = GET_RW(read_ctx_reg(get_el3state_ctx(&ctx->cpu_ctx),
302 CTX_SPSR_EL3));
303
Sandrine Bailleuxf8220902016-11-30 11:24:01 +0000304 /*
305 * Get information about the Trusty image. Its absence is a critical
306 * failure.
307 */
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800308 ep_info = bl31_plat_get_next_image_ep_info(SECURE);
Sandrine Bailleuxf8220902016-11-30 11:24:01 +0000309 assert(ep_info);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800310
311 cm_el1_sysregs_context_save(NON_SECURE);
312
313 cm_set_context(&ctx->cpu_ctx, SECURE);
314 cm_init_my_context(ep_info);
315
316 /*
317 * Adjust secondary cpu entry point for 32 bit images to the
318 * end of exeption vectors
319 */
320 if ((cpu != 0) && (reg_width == MODE_RW_32)) {
321 INFO("trusty: cpu %d, adjust entry point to 0x%lx\n",
322 cpu, ep_info->pc + (1U << 5));
323 cm_set_elr_el3(SECURE, ep_info->pc + (1U << 5));
324 }
325
326 cm_el1_sysregs_context_restore(SECURE);
327 cm_set_next_eret_context(SECURE);
328
329 ctx->saved_security_state = ~0; /* initial saved state is invalid */
Varun Wadekarbd3c9532017-02-16 18:14:37 -0800330 trusty_init_context_stack(&ctx->saved_sp, &ctx->secure_stack.end);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800331
Anthony Zhou700ebe52015-10-31 06:03:41 +0800332 trusty_context_switch_helper(&ctx->saved_sp, &zero_args);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800333
334 cm_el1_sysregs_context_restore(NON_SECURE);
335 cm_set_next_eret_context(NON_SECURE);
336
337 return 0;
338}
339
340static void trusty_cpu_suspend(void)
341{
342 struct args ret;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800343
344 ret = trusty_context_switch(NON_SECURE, SMC_FC_CPU_SUSPEND, 0, 0, 0);
345 if (ret.r0 != 0) {
346 INFO("%s: cpu %d, SMC_FC_CPU_SUSPEND returned unexpected value, %ld\n",
Sandrine Bailleux5f665c82016-11-23 09:50:53 +0000347 __func__, plat_my_core_pos(), ret.r0);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800348 }
349}
350
351static void trusty_cpu_resume(void)
352{
353 struct args ret;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800354
355 ret = trusty_context_switch(NON_SECURE, SMC_FC_CPU_RESUME, 0, 0, 0);
356 if (ret.r0 != 0) {
357 INFO("%s: cpu %d, SMC_FC_CPU_RESUME returned unexpected value, %ld\n",
Sandrine Bailleux5f665c82016-11-23 09:50:53 +0000358 __func__, plat_my_core_pos(), ret.r0);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800359 }
360}
361
362static int32_t trusty_cpu_off_handler(uint64_t unused)
363{
364 trusty_cpu_suspend();
365
366 return 0;
367}
368
369static void trusty_cpu_on_finish_handler(uint64_t unused)
370{
371 struct trusty_cpu_ctx *ctx = get_trusty_ctx();
372
373 if (!ctx->saved_sp) {
374 trusty_init();
375 } else {
376 trusty_cpu_resume();
377 }
378}
379
380static void trusty_cpu_suspend_handler(uint64_t unused)
381{
382 trusty_cpu_suspend();
383}
384
385static void trusty_cpu_suspend_finish_handler(uint64_t unused)
386{
387 trusty_cpu_resume();
388}
389
390static const spd_pm_ops_t trusty_pm = {
391 .svc_off = trusty_cpu_off_handler,
392 .svc_suspend = trusty_cpu_suspend_handler,
393 .svc_on_finish = trusty_cpu_on_finish_handler,
394 .svc_suspend_finish = trusty_cpu_suspend_finish_handler,
395};
396
397static int32_t trusty_setup(void)
398{
399 entry_point_info_t *ep_info;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800400 uint32_t flags;
401 int ret;
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800402
Varun Wadekarba33a282017-02-23 10:34:06 -0800403 /* Get trusty's entry point info */
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800404 ep_info = bl31_plat_get_next_image_ep_info(SECURE);
405 if (!ep_info) {
406 INFO("Trusty image missing.\n");
407 return -1;
408 }
409
Varun Wadekarba33a282017-02-23 10:34:06 -0800410 /* Trusty runs in AARCH64 mode */
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800411 SET_PARAM_HEAD(ep_info, PARAM_EP, VERSION_1, SECURE | EP_ST_ENABLE);
Varun Wadekarba33a282017-02-23 10:34:06 -0800412 ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800413
Wayne Lincd712fd2016-05-24 15:28:42 -0700414 /*
415 * arg0 = TZDRAM aperture available for BL32
416 * arg1 = BL32 boot params
417 * arg2 = BL32 boot params length
418 */
419 ep_info->args.arg1 = ep_info->args.arg2;
420 ep_info->args.arg2 = TRUSTY_PARAMS_LEN_BYTES;
421
Varun Wadekarba33a282017-02-23 10:34:06 -0800422 /* register init handler */
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800423 bl31_register_bl32_init(trusty_init);
424
Varun Wadekarba33a282017-02-23 10:34:06 -0800425 /* register power management hooks */
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800426 psci_register_spd_pm_hook(&trusty_pm);
427
Varun Wadekarba33a282017-02-23 10:34:06 -0800428 /* register interrupt handler */
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800429 flags = 0;
430 set_interrupt_rm_flag(flags, NON_SECURE);
431 ret = register_interrupt_type_handler(INTR_TYPE_S_EL1,
432 trusty_fiq_handler,
433 flags);
434 if (ret)
435 ERROR("trusty: failed to register fiq handler, ret = %d\n", ret);
436
437 return 0;
438}
439
440/* Define a SPD runtime service descriptor for fast SMC calls */
441DECLARE_RT_SVC(
442 trusty_fast,
443
444 OEN_TOS_START,
445 SMC_ENTITY_SECURE_MONITOR,
446 SMC_TYPE_FAST,
447 trusty_setup,
448 trusty_smc_handler
449);
450
451/* Define a SPD runtime service descriptor for standard SMC calls */
452DECLARE_RT_SVC(
453 trusty_std,
454
Amith43e89d32015-08-19 20:13:12 -0700455 OEN_TAP_START,
Varun Wadekarc1d2a282016-11-08 15:46:48 -0800456 SMC_ENTITY_SECURE_MONITOR,
457 SMC_TYPE_STD,
458 NULL,
459 trusty_smc_handler
460);