blob: 6fec91bca75da409895d3c3306200662b7dae051 [file] [log] [blame]
Varun Wadekar3d4e6a52015-03-13 14:01:03 +05301/*
Daniel Boulby8942a1b2018-06-22 14:16:03 +01002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Varun Wadekar3d4e6a52015-03-13 14:01:03 +05303 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekar3d4e6a52015-03-13 14:01:03 +05305 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <bl_common.h>
10#include <context_mgmt.h>
11#include <string.h>
12#include "tlkd_private.h"
13
Varun Wadekar97625e32015-03-13 14:59:03 +053014#define AT_MASK 3
15
16/*******************************************************************************
17 * This function helps the SP to translate NS/S virtual addresses.
18 ******************************************************************************/
19uint64_t tlkd_va_translate(uintptr_t va, int type)
20{
21 uint64_t pa;
22
23 if (type & TLK_TRANSLATE_NS_VADDR) {
24
25 /* save secure context */
26 cm_el1_sysregs_context_save(SECURE);
27
28 /* restore non-secure context */
29 cm_el1_sysregs_context_restore(NON_SECURE);
30
31 /* switch NS bit to start using 64-bit, non-secure mappings */
32 write_scr(cm_get_scr_el3(NON_SECURE));
33 isb();
34 }
35
36 int at = type & AT_MASK;
37 switch (at) {
38 case 0:
39 ats12e1r(va);
40 break;
41 case 1:
42 ats12e1w(va);
43 break;
44 case 2:
45 ats12e0r(va);
46 break;
47 case 3:
48 ats12e0w(va);
49 break;
50 default:
Daniel Boulby8942a1b2018-06-22 14:16:03 +010051 assert(0); /* Unreachable */
Jonathan Wright75a5d8b2018-03-14 15:56:21 +000052 break;
Varun Wadekar97625e32015-03-13 14:59:03 +053053 }
54
55 /* get the (NS/S) physical address */
56 isb();
57 pa = read_par_el1();
58
59 /* Restore secure state */
60 if (type & TLK_TRANSLATE_NS_VADDR) {
61
62 /* restore secure context */
63 cm_el1_sysregs_context_restore(SECURE);
64
65 /* switch NS bit to start using 32-bit, secure mappings */
66 write_scr(cm_get_scr_el3(SECURE));
67 isb();
68 }
69
70 return pa;
71}
72
Varun Wadekar3d4e6a52015-03-13 14:01:03 +053073/*******************************************************************************
74 * Given a secure payload entrypoint, register width, cpu id & pointer to a
75 * context data structure, this function will create a secure context ready for
76 * programming an entry into the secure payload.
77 ******************************************************************************/
78void tlkd_init_tlk_ep_state(struct entry_point_info *tlk_entry_point,
79 uint32_t rw,
80 uint64_t pc,
81 tlk_context_t *tlk_ctx)
82{
83 uint32_t ep_attr, spsr;
84
85 /* Passing a NULL context is a critical programming error */
86 assert(tlk_ctx);
87 assert(tlk_entry_point);
88 assert(pc);
89
90 /* Associate this context with the cpu specified */
91 tlk_ctx->mpidr = read_mpidr_el1();
David Cunadoc8833ea2017-04-16 17:15:08 +010092 clr_yield_smc_active_flag(tlk_ctx->state);
Varun Wadekar3d4e6a52015-03-13 14:01:03 +053093 cm_set_context(&tlk_ctx->cpu_ctx, SECURE);
94
95 if (rw == SP_AARCH64)
96 spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
97 else
98 spsr = SPSR_MODE32(MODE32_svc,
99 SPSR_T_ARM,
100 read_sctlr_el3() & SCTLR_EE_BIT,
101 DISABLE_ALL_EXCEPTIONS);
102
103 /* initialise an entrypoint to set up the CPU context */
104 ep_attr = SECURE | EP_ST_ENABLE;
105 if (read_sctlr_el3() & SCTLR_EE_BIT)
106 ep_attr |= EP_EE_BIG;
107 SET_PARAM_HEAD(tlk_entry_point, PARAM_EP, VERSION_1, ep_attr);
108
109 tlk_entry_point->pc = pc;
110 tlk_entry_point->spsr = spsr;
111}
112
113/*******************************************************************************
114 * This function takes a TLK context pointer and:
115 * 1. Applies the S-EL1 system register context from tlk_ctx->cpu_ctx.
116 * 2. Saves the current C runtime state (callee saved registers) on the stack
117 * frame and saves a reference to this state.
118 * 3. Calls el3_exit() so that the EL3 system and general purpose registers
119 * from the tlk_ctx->cpu_ctx are used to enter the secure payload image.
120 ******************************************************************************/
121uint64_t tlkd_synchronous_sp_entry(tlk_context_t *tlk_ctx)
122{
123 uint64_t rc;
124
125 /* Passing a NULL context is a critical programming error */
126 assert(tlk_ctx);
127 assert(tlk_ctx->c_rt_ctx == 0);
128
129 /* Apply the Secure EL1 system register context and switch to it */
130 assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
131 cm_el1_sysregs_context_restore(SECURE);
132 cm_set_next_eret_context(SECURE);
133
134 rc = tlkd_enter_sp(&tlk_ctx->c_rt_ctx);
Antonio Nino Diaz0fbaa5c2017-10-19 16:55:48 +0100135#if ENABLE_ASSERTIONS
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530136 tlk_ctx->c_rt_ctx = 0;
137#endif
138
139 return rc;
140}
141
142/*******************************************************************************
143 * This function takes a TLK context pointer and:
144 * 1. Saves the S-EL1 system register context to tlk_ctx->cpu_ctx.
145 * 2. Restores the current C runtime state (callee saved registers) from the
146 * stack frame using reference to this state saved in tlkd_enter_sp().
147 * 3. It does not need to save any general purpose or EL3 system register state
148 * as the generic smc entry routine should have saved those.
149 ******************************************************************************/
150void tlkd_synchronous_sp_exit(tlk_context_t *tlk_ctx, uint64_t ret)
151{
152 /* Passing a NULL context is a critical programming error */
153 assert(tlk_ctx);
154
155 /* Save the Secure EL1 system register context */
156 assert(cm_get_context(SECURE) == &tlk_ctx->cpu_ctx);
157 cm_el1_sysregs_context_save(SECURE);
158
159 assert(tlk_ctx->c_rt_ctx != 0);
160 tlkd_exit_sp(tlk_ctx->c_rt_ctx, ret);
161
162 /* Should never reach here */
163 assert(0);
164}