blob: 2f20b7cae4e0c541051899c78a231c6f61663c07 [file] [log] [blame]
Jens Wiklanderc2888862014-08-04 15:39:58 +02001/*
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 <arch_helpers.h>
32#include <assert.h>
33#include <bl_common.h>
34#include <context_mgmt.h>
35#include <string.h>
36#include "opteed_private.h"
37
38/*******************************************************************************
39 * Given a OPTEE entrypoint info pointer, entry point PC, register width,
40 * cpu id & pointer to a context data structure, this function will
41 * initialize OPTEE context and entry point info for OPTEE.
42 ******************************************************************************/
43void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
44 uint32_t rw, uint64_t pc,
45 optee_context_t *optee_ctx)
46{
47 uint32_t ep_attr;
48
49 /* Passing a NULL context is a critical programming error */
50 assert(optee_ctx);
51 assert(optee_entry_point);
52 assert(pc);
53
54 /* Associate this context with the cpu specified */
55 optee_ctx->mpidr = read_mpidr_el1();
56 optee_ctx->state = 0;
57 set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF);
58
59 cm_set_context(&optee_ctx->cpu_ctx, SECURE);
60
61 /* initialise an entrypoint to set up the CPU context */
62 ep_attr = SECURE | EP_ST_ENABLE;
63 if (read_sctlr_el3() & SCTLR_EE_BIT)
64 ep_attr |= EP_EE_BIG;
65 SET_PARAM_HEAD(optee_entry_point, PARAM_EP, VERSION_1, ep_attr);
66 optee_entry_point->pc = pc;
67 if (rw == OPTEE_AARCH64)
68 optee_entry_point->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
69 DISABLE_ALL_EXCEPTIONS);
70 else
71 optee_entry_point->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
72 SPSR_E_LITTLE,
73 DAIF_FIQ_BIT |
74 DAIF_IRQ_BIT |
75 DAIF_ABT_BIT);
76 memset(&optee_entry_point->args, 0, sizeof(optee_entry_point->args));
77}
78
79/*******************************************************************************
80 * This function takes an OPTEE context pointer and:
81 * 1. Applies the S-EL1 system register context from optee_ctx->cpu_ctx.
82 * 2. Saves the current C runtime state (callee saved registers) on the stack
83 * frame and saves a reference to this state.
84 * 3. Calls el3_exit() so that the EL3 system and general purpose registers
85 * from the optee_ctx->cpu_ctx are used to enter the OPTEE image.
86 ******************************************************************************/
87uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx)
88{
89 uint64_t rc;
90
91 assert(optee_ctx != NULL);
92 assert(optee_ctx->c_rt_ctx == 0);
93
94 /* Apply the Secure EL1 system register context and switch to it */
95 assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx);
96 cm_el1_sysregs_context_restore(SECURE);
97 cm_set_next_eret_context(SECURE);
98
99 rc = opteed_enter_sp(&optee_ctx->c_rt_ctx);
100#if DEBUG
101 optee_ctx->c_rt_ctx = 0;
102#endif
103
104 return rc;
105}
106
107
108/*******************************************************************************
109 * This function takes an OPTEE context pointer and:
110 * 1. Saves the S-EL1 system register context tp optee_ctx->cpu_ctx.
111 * 2. Restores the current C runtime state (callee saved registers) from the
112 * stack frame using the reference to this state saved in opteed_enter_sp().
113 * 3. It does not need to save any general purpose or EL3 system register state
114 * as the generic smc entry routine should have saved those.
115 ******************************************************************************/
116void opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret)
117{
118 assert(optee_ctx != NULL);
119 /* Save the Secure EL1 system register context */
120 assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx);
121 cm_el1_sysregs_context_save(SECURE);
122
123 assert(optee_ctx->c_rt_ctx != 0);
124 opteed_exit_sp(optee_ctx->c_rt_ctx, ret);
125
126 /* Should never reach here */
127 assert(0);
128}