blob: 60c349248c7b5c016c4090d4fafb75f5a43133e9 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
2 * Copyright (c) 2013-2014, 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#include <stdio.h>
32#include <errno.h>
33#include <string.h>
34#include <assert.h>
35#include <arch_helpers.h>
36#include <platform.h>
37#include <bl_common.h>
38#include <runtime_svc.h>
39#include <context_mgmt.h>
40
41/*******************************************************************************
42 * Data structure which holds the pointers to non-secure and secure security
43 * state contexts for each cpu. It is aligned to the cache line boundary to
44 * allow efficient concurrent manipulation of these pointers on different cpus
45 ******************************************************************************/
46typedef struct {
47 void *ptr[2];
48} __aligned (CACHE_WRITEBACK_GRANULE) context_info;
49
50static context_info cm_context_info[PLATFORM_CORE_COUNT];
51
52/*******************************************************************************
53 * Context management library initialisation routine. This library is used by
54 * runtime services to share pointers to 'cpu_context' structures for the secure
55 * and non-secure states. Management of the structures and their associated
56 * memory is not done by the context management library e.g. the PSCI service
57 * manages the cpu context used for entry from and exit to the non-secure state.
58 * The Secure payload dispatcher service manages the context(s) corresponding to
59 * the secure state. It also uses this library to get access to the non-secure
60 * state cpu context pointers.
61 * Lastly, this library provides the api to make SP_EL3 point to the cpu context
62 * which will used for programming an entry into a lower EL. The same context
63 * will used to save state upon exception entry from that EL.
64 ******************************************************************************/
65void cm_init()
66{
67 /*
68 * The context management library has only global data to intialize, but
69 * that will be done when the BSS is zeroed out
70 */
71}
72
73/*******************************************************************************
74 * This function returns a pointer to the most recent 'cpu_context' structure
75 * that was set as the context for the specified security state. NULL is
76 * returned if no such structure has been specified.
77 ******************************************************************************/
78void *cm_get_context(uint64_t mpidr, uint32_t security_state)
79{
80 uint32_t linear_id = platform_get_core_pos(mpidr);
81
82 assert(security_state <= NON_SECURE);
83
84 return cm_context_info[linear_id].ptr[security_state];
85}
86
87/*******************************************************************************
88 * This function sets the pointer to the current 'cpu_context' structure for the
89 * specified security state.
90 ******************************************************************************/
91void cm_set_context(uint64_t mpidr, void *context, uint32_t security_state)
92{
93 uint32_t linear_id = platform_get_core_pos(mpidr);
94
95 assert(security_state <= NON_SECURE);
96
97 cm_context_info[linear_id].ptr[security_state] = context;
98}
99
100/*******************************************************************************
101 * The next four functions are used by runtime services to save and restore EL3
102 * and EL1 contexts on the 'cpu_context' structure for the specified security
103 * state.
104 ******************************************************************************/
105void cm_el3_sysregs_context_save(uint32_t security_state)
106{
107 cpu_context *ctx;
108
109 ctx = cm_get_context(read_mpidr(), security_state);
110 assert(ctx);
111
112 el3_sysregs_context_save(get_el3state_ctx(ctx));
113}
114
115void cm_el3_sysregs_context_restore(uint32_t security_state)
116{
117 cpu_context *ctx;
118
119 ctx = cm_get_context(read_mpidr(), security_state);
120 assert(ctx);
121
122 el3_sysregs_context_restore(get_el3state_ctx(ctx));
123}
124
125void cm_el1_sysregs_context_save(uint32_t security_state)
126{
127 cpu_context *ctx;
128
129 ctx = cm_get_context(read_mpidr(), security_state);
130 assert(ctx);
131
132 el1_sysregs_context_save(get_sysregs_ctx(ctx));
133}
134
135void cm_el1_sysregs_context_restore(uint32_t security_state)
136{
137 cpu_context *ctx;
138
139 ctx = cm_get_context(read_mpidr(), security_state);
140 assert(ctx);
141
142 el1_sysregs_context_restore(get_sysregs_ctx(ctx));
143}
144
145/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000146 * This function function populates 'cpu_context' pertaining to the given
147 * security state with the entrypoint, SPSR and SCR values so that an ERET from
148 * this securit state correctly restores corresponding values to drop the CPU to
149 * the next exception level
150 ******************************************************************************/
151void cm_set_el3_eret_context(uint32_t security_state, uint64_t entrypoint,
152 uint32_t spsr, uint32_t scr)
153{
154 cpu_context *ctx;
155 el3_state *state;
156
157 ctx = cm_get_context(read_mpidr(), security_state);
158 assert(ctx);
159
160 /* Populate EL3 state so that we've the right context before doing ERET */
161 state = get_el3state_ctx(ctx);
162 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
163 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
164 write_ctx_reg(state, CTX_SCR_EL3, scr);
165}
166
167/*******************************************************************************
Achin Gupta607084e2014-02-09 18:24:19 +0000168 * This function function populates ELR_EL3 member of 'cpu_context' pertaining
169 * to the given security state with the given entrypoint
170 ******************************************************************************/
171void cm_set_el3_elr(uint32_t security_state, uint64_t entrypoint)
172{
173 cpu_context *ctx;
174 el3_state *state;
175
176 ctx = cm_get_context(read_mpidr(), security_state);
177 assert(ctx);
178
179 /* Populate EL3 state so that ERET jumps to the correct entry */
180 state = get_el3state_ctx(ctx);
181 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
182}
183
184/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000185 * This function is used to program the context that's used for exception
186 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
187 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +0000188 ******************************************************************************/
189void cm_set_next_eret_context(uint32_t security_state)
190{
191 cpu_context *ctx;
192#if DEBUG
193 uint64_t sp_mode;
194#endif
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000195
Achin Gupta7aea9082014-02-01 07:51:28 +0000196 ctx = cm_get_context(read_mpidr(), security_state);
197 assert(ctx);
198
199#if DEBUG
200 /*
201 * Check that this function is called with SP_EL0 as the stack
202 * pointer
203 */
204 __asm__ volatile("mrs %0, SPSel\n"
205 : "=r" (sp_mode));
206
207 assert(sp_mode == MODE_SP_EL0);
208#endif
209
210 __asm__ volatile("msr spsel, #1\n"
211 "mov sp, %0\n"
212 "msr spsel, #0\n"
213 : : "r" (ctx));
214}
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000215
216/*******************************************************************************
217 * This function is used to program exception stack in the 'cpu_context'
218 * structure. This is the initial stack used for taking and handling exceptions
219 * at EL3. This stack is expected to be initialized once by each security state
220 ******************************************************************************/
221void cm_init_exception_stack(uint64_t mpidr, uint32_t security_state)
222{
223 cpu_context *ctx;
224 el3_state *state;
225
226 ctx = cm_get_context(mpidr, security_state);
227 assert(ctx);
228
229 /* Set exception stack in the context */
230 state = get_el3state_ctx(ctx);
231
232 write_ctx_reg(state, CTX_EXCEPTION_SP, get_exception_stack(mpidr));
233}