1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <string.h>
9*54fd6939SJiyong Park
10*54fd6939SJiyong Park #include <arch_helpers.h>
11*54fd6939SJiyong Park #include <common/bl_common.h>
12*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
13*54fd6939SJiyong Park #include <lib/utils.h>
14*54fd6939SJiyong Park
15*54fd6939SJiyong Park #include "opteed_private.h"
16*54fd6939SJiyong Park
17*54fd6939SJiyong Park /*******************************************************************************
18*54fd6939SJiyong Park * Given a OPTEE entrypoint info pointer, entry point PC, register width,
19*54fd6939SJiyong Park * cpu id & pointer to a context data structure, this function will
20*54fd6939SJiyong Park * initialize OPTEE context and entry point info for OPTEE.
21*54fd6939SJiyong Park ******************************************************************************/
opteed_init_optee_ep_state(struct entry_point_info * optee_entry_point,uint32_t rw,uint64_t pc,uint64_t pageable_part,uint64_t mem_limit,uint64_t dt_addr,optee_context_t * optee_ctx)22*54fd6939SJiyong Park void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
23*54fd6939SJiyong Park uint32_t rw, uint64_t pc,
24*54fd6939SJiyong Park uint64_t pageable_part, uint64_t mem_limit,
25*54fd6939SJiyong Park uint64_t dt_addr, optee_context_t *optee_ctx)
26*54fd6939SJiyong Park {
27*54fd6939SJiyong Park uint32_t ep_attr;
28*54fd6939SJiyong Park
29*54fd6939SJiyong Park /* Passing a NULL context is a critical programming error */
30*54fd6939SJiyong Park assert(optee_ctx);
31*54fd6939SJiyong Park assert(optee_entry_point);
32*54fd6939SJiyong Park assert(pc);
33*54fd6939SJiyong Park
34*54fd6939SJiyong Park /* Associate this context with the cpu specified */
35*54fd6939SJiyong Park optee_ctx->mpidr = read_mpidr_el1();
36*54fd6939SJiyong Park optee_ctx->state = 0;
37*54fd6939SJiyong Park set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF);
38*54fd6939SJiyong Park
39*54fd6939SJiyong Park cm_set_context(&optee_ctx->cpu_ctx, SECURE);
40*54fd6939SJiyong Park
41*54fd6939SJiyong Park /* initialise an entrypoint to set up the CPU context */
42*54fd6939SJiyong Park ep_attr = SECURE | EP_ST_ENABLE;
43*54fd6939SJiyong Park if (read_sctlr_el3() & SCTLR_EE_BIT)
44*54fd6939SJiyong Park ep_attr |= EP_EE_BIG;
45*54fd6939SJiyong Park SET_PARAM_HEAD(optee_entry_point, PARAM_EP, VERSION_1, ep_attr);
46*54fd6939SJiyong Park optee_entry_point->pc = pc;
47*54fd6939SJiyong Park if (rw == OPTEE_AARCH64)
48*54fd6939SJiyong Park optee_entry_point->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
49*54fd6939SJiyong Park DISABLE_ALL_EXCEPTIONS);
50*54fd6939SJiyong Park else
51*54fd6939SJiyong Park optee_entry_point->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
52*54fd6939SJiyong Park SPSR_E_LITTLE,
53*54fd6939SJiyong Park DAIF_FIQ_BIT |
54*54fd6939SJiyong Park DAIF_IRQ_BIT |
55*54fd6939SJiyong Park DAIF_ABT_BIT);
56*54fd6939SJiyong Park zeromem(&optee_entry_point->args, sizeof(optee_entry_point->args));
57*54fd6939SJiyong Park optee_entry_point->args.arg0 = pageable_part;
58*54fd6939SJiyong Park optee_entry_point->args.arg1 = mem_limit;
59*54fd6939SJiyong Park optee_entry_point->args.arg2 = dt_addr;
60*54fd6939SJiyong Park }
61*54fd6939SJiyong Park
62*54fd6939SJiyong Park /*******************************************************************************
63*54fd6939SJiyong Park * This function takes an OPTEE context pointer and:
64*54fd6939SJiyong Park * 1. Applies the S-EL1 system register context from optee_ctx->cpu_ctx.
65*54fd6939SJiyong Park * 2. Saves the current C runtime state (callee saved registers) on the stack
66*54fd6939SJiyong Park * frame and saves a reference to this state.
67*54fd6939SJiyong Park * 3. Calls el3_exit() so that the EL3 system and general purpose registers
68*54fd6939SJiyong Park * from the optee_ctx->cpu_ctx are used to enter the OPTEE image.
69*54fd6939SJiyong Park ******************************************************************************/
opteed_synchronous_sp_entry(optee_context_t * optee_ctx)70*54fd6939SJiyong Park uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx)
71*54fd6939SJiyong Park {
72*54fd6939SJiyong Park uint64_t rc;
73*54fd6939SJiyong Park
74*54fd6939SJiyong Park assert(optee_ctx != NULL);
75*54fd6939SJiyong Park assert(optee_ctx->c_rt_ctx == 0);
76*54fd6939SJiyong Park
77*54fd6939SJiyong Park /* Apply the Secure EL1 system register context and switch to it */
78*54fd6939SJiyong Park assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx);
79*54fd6939SJiyong Park cm_el1_sysregs_context_restore(SECURE);
80*54fd6939SJiyong Park cm_set_next_eret_context(SECURE);
81*54fd6939SJiyong Park
82*54fd6939SJiyong Park rc = opteed_enter_sp(&optee_ctx->c_rt_ctx);
83*54fd6939SJiyong Park #if ENABLE_ASSERTIONS
84*54fd6939SJiyong Park optee_ctx->c_rt_ctx = 0;
85*54fd6939SJiyong Park #endif
86*54fd6939SJiyong Park
87*54fd6939SJiyong Park return rc;
88*54fd6939SJiyong Park }
89*54fd6939SJiyong Park
90*54fd6939SJiyong Park
91*54fd6939SJiyong Park /*******************************************************************************
92*54fd6939SJiyong Park * This function takes an OPTEE context pointer and:
93*54fd6939SJiyong Park * 1. Saves the S-EL1 system register context tp optee_ctx->cpu_ctx.
94*54fd6939SJiyong Park * 2. Restores the current C runtime state (callee saved registers) from the
95*54fd6939SJiyong Park * stack frame using the reference to this state saved in opteed_enter_sp().
96*54fd6939SJiyong Park * 3. It does not need to save any general purpose or EL3 system register state
97*54fd6939SJiyong Park * as the generic smc entry routine should have saved those.
98*54fd6939SJiyong Park ******************************************************************************/
opteed_synchronous_sp_exit(optee_context_t * optee_ctx,uint64_t ret)99*54fd6939SJiyong Park void opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret)
100*54fd6939SJiyong Park {
101*54fd6939SJiyong Park assert(optee_ctx != NULL);
102*54fd6939SJiyong Park /* Save the Secure EL1 system register context */
103*54fd6939SJiyong Park assert(cm_get_context(SECURE) == &optee_ctx->cpu_ctx);
104*54fd6939SJiyong Park cm_el1_sysregs_context_save(SECURE);
105*54fd6939SJiyong Park
106*54fd6939SJiyong Park assert(optee_ctx->c_rt_ctx != 0);
107*54fd6939SJiyong Park opteed_exit_sp(optee_ctx->c_rt_ctx, ret);
108*54fd6939SJiyong Park
109*54fd6939SJiyong Park /* Should never reach here */
110*54fd6939SJiyong Park assert(0);
111*54fd6939SJiyong Park }
112