blob: e3134102dd68391aeb8fcecd2ff550f61b0c6bd7 [file] [log] [blame]
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00001/*
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00003 *
David Cunadodedfde52017-05-11 17:30:06 +01004 * SPDX-License-Identifier: BSD-3-Clause
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00005 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <stdbool.h>
8#include <string.h>
9
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000010#include <arch_helpers.h>
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000011#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <lib/el3_runtime/context_mgmt.h>
13#include <lib/psci/psci.h>
14#include <lib/utils.h>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000015#include <smccc_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016
17#include <arm_sip_svc.h>
18#include <plat_arm.h>
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000019
20/*
21 * Handle SMC from a lower exception level to switch its execution state
22 * (either from AArch64 to AArch32, or vice versa).
23 *
24 * smc_fid:
25 * SMC function ID - either ARM_SIP_SVC_STATE_SWITCH_64 or
26 * ARM_SIP_SVC_STATE_SWITCH_32.
27 * pc_hi, pc_lo:
28 * PC upon re-entry to the calling exception level; width dependent on the
29 * calling exception level.
30 * cookie_hi, cookie_lo:
31 * Opaque pointer pairs received from the caller to pass it back, upon
32 * re-entry.
33 * handle:
34 * Handle to saved context.
35 */
36int arm_execution_state_switch(unsigned int smc_fid,
37 uint32_t pc_hi,
38 uint32_t pc_lo,
39 uint32_t cookie_hi,
40 uint32_t cookie_lo,
41 void *handle)
42{
43 /* Execution state can be switched only if EL3 is AArch64 */
44#ifdef AARCH64
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +010045 bool caller_64, thumb = false, from_el2;
46 unsigned int el, endianness;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000047 u_register_t spsr, pc, scr, sctlr;
48 entry_point_info_t ep;
49 cpu_context_t *ctx = (cpu_context_t *) handle;
50 el3_state_t *el3_ctx = get_el3state_ctx(ctx);
51
52 /* That the SMC originated from NS is already validated by the caller */
53
54 /*
55 * Disallow state switch if any of the secondaries have been brought up.
56 */
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +010057 if (psci_secondaries_brought_up() != 0)
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000058 goto exec_denied;
59
60 spsr = read_ctx_reg(el3_ctx, CTX_SPSR_EL3);
61 caller_64 = (GET_RW(spsr) == MODE_RW_64);
62
63 if (caller_64) {
64 /*
65 * If the call originated from AArch64, expect 32-bit pointers when
66 * switching to AArch32.
67 */
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +010068 if ((pc_hi != 0U) || (cookie_hi != 0U))
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000069 goto invalid_param;
70
71 pc = pc_lo;
72
73 /* Instruction state when entering AArch32 */
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +010074 thumb = (pc & 1U) != 0U;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000075 } else {
76 /* Construct AArch64 PC */
77 pc = (((u_register_t) pc_hi) << 32) | pc_lo;
78 }
79
80 /* Make sure PC is 4-byte aligned, except for Thumb */
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +010081 if (((pc & 0x3U) != 0U) && !thumb)
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +000082 goto invalid_param;
83
84 /*
85 * EL3 controls register width of the immediate lower EL only. Expect
86 * this request from EL2/Hyp unless:
87 *
88 * - EL2 is not implemented;
89 * - EL2 is implemented, but was disabled. This can be inferred from
90 * SCR_EL3.HCE.
91 */
92 from_el2 = caller_64 ? (GET_EL(spsr) == MODE_EL2) :
93 (GET_M32(spsr) == MODE32_hyp);
94 scr = read_ctx_reg(el3_ctx, CTX_SCR_EL3);
95 if (!from_el2) {
96 /* The call is from NS privilege level other than HYP */
97
98 /*
99 * Disallow switching state if there's a Hypervisor in place;
100 * this request must be taken up with the Hypervisor instead.
101 */
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100102 if ((scr & SCR_HCE_BIT) != 0U)
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000103 goto exec_denied;
104 }
105
106 /*
107 * Return to the caller using the same endianness. Extract
108 * endianness bit from the respective system control register
109 * directly.
110 */
111 sctlr = from_el2 ? read_sctlr_el2() : read_sctlr_el1();
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100112 endianness = ((sctlr & SCTLR_EE_BIT) != 0U) ? 1U : 0U;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000113
114 /* Construct SPSR for the exception state we're about to switch to */
115 if (caller_64) {
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100116 unsigned long long impl;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000117
118 /*
119 * Switching from AArch64 to AArch32. Ensure this CPU implements
120 * the target EL in AArch32.
121 */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000122 impl = from_el2 ? el_implemented(2) : el_implemented(1);
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000123 if (impl != EL_IMPL_A64_A32)
124 goto exec_denied;
125
126 /* Return to the equivalent AArch32 privilege level */
127 el = from_el2 ? MODE32_hyp : MODE32_svc;
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100128 spsr = SPSR_MODE32((u_register_t) el,
129 thumb ? SPSR_T_THUMB : SPSR_T_ARM,
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000130 endianness, DISABLE_ALL_EXCEPTIONS);
131 } else {
132 /*
133 * Switching from AArch32 to AArch64. Since it's not possible to
134 * implement an EL as AArch32-only (from which this call was
135 * raised), it's safe to assume AArch64 is also implemented.
136 */
137 el = from_el2 ? MODE_EL2 : MODE_EL1;
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100138 spsr = SPSR_64((u_register_t) el, MODE_SP_ELX,
139 DISABLE_ALL_EXCEPTIONS);
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000140 }
141
142 /*
143 * Use the context management library to re-initialize the existing
144 * context with the execution state flipped. Since the library takes
145 * entry_point_info_t pointer as the argument, construct a dummy one
146 * with PC, state width, endianness, security etc. appropriately set.
147 * Other entries in the entry point structure are irrelevant for
148 * purpose.
149 */
150 zeromem(&ep, sizeof(ep));
151 ep.pc = pc;
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100152 ep.spsr = (uint32_t) spsr;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000153 SET_PARAM_HEAD(&ep, PARAM_EP, VERSION_1,
Jeenu Viswambharan210f0a82018-08-02 10:14:12 +0100154 ((unsigned int) ((endianness != 0U) ? EP_EE_BIG :
155 EP_EE_LITTLE)
156 | NON_SECURE | EP_ST_DISABLE));
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +0000157
158 /*
159 * Re-initialize the system register context, and exit EL3 as if for the
160 * first time. State switch is effectively a soft reset of the
161 * calling EL.
162 */
163 cm_init_my_context(&ep);
164 cm_prepare_el3_exit(NON_SECURE);
165
166 /*
167 * State switch success. The caller of SMC wouldn't see the SMC
168 * returning. Instead, execution starts at the supplied entry point,
169 * with context pointers populated in registers 0 and 1.
170 */
171 SMC_RET2(handle, cookie_hi, cookie_lo);
172
173invalid_param:
174 SMC_RET1(handle, STATE_SW_E_PARAM);
175
176exec_denied:
177#endif
178 /* State switch denied */
179 SMC_RET1(handle, STATE_SW_E_DENIED);
180}