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