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