blob: bbdbad2ca8df2869b6e6e0c4e28626d78c7926af [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/*******************************************************************************
146 * This function is used to program SP_EL3 to point to the 'cpu_context'
147 * structure which will be used for programming the EL3 architectural state to
148 * enable an ERET into a lower EL e.g. general purpose registers and system
149 * registers like SCR_EL3, SPSR_EL3, SCR_EL3 etc. The same structure will be
150 * used to save the same registers after an exception entry from the lower EL.
151 ******************************************************************************/
152void cm_set_next_eret_context(uint32_t security_state)
153{
154 cpu_context *ctx;
155#if DEBUG
156 uint64_t sp_mode;
157#endif
158 ctx = cm_get_context(read_mpidr(), security_state);
159 assert(ctx);
160
161#if DEBUG
162 /*
163 * Check that this function is called with SP_EL0 as the stack
164 * pointer
165 */
166 __asm__ volatile("mrs %0, SPSel\n"
167 : "=r" (sp_mode));
168
169 assert(sp_mode == MODE_SP_EL0);
170#endif
171
172 __asm__ volatile("msr spsel, #1\n"
173 "mov sp, %0\n"
174 "msr spsel, #0\n"
175 : : "r" (ctx));
176}