| 1 |
/* |
| 2 |
* Copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <arch_helpers.h> |
| 11 |
#include <common/bl_common.h> |
| 12 |
#include <common/debug.h> |
| 13 |
#include <lib/el3_runtime/context_mgmt.h> |
| 14 |
#include <lib/utils.h> |
| 15 |
#include <plat/common/platform.h> |
| 16 |
|
| 17 |
#include "pncd_private.h" |
| 18 |
|
| 19 |
/******************************************************************************* |
| 20 |
* Given a secure payload entrypoint info pointer, entry point PC & pointer to a |
| 21 |
* context data structure, this function will initialize pnc context and entry |
| 22 |
* point info for the secure payload |
| 23 |
******************************************************************************/ |
| 24 |
void pncd_init_pnc_ep_state(struct entry_point_info *pnc_entry_point, |
| 25 |
uint64_t pc, |
| 26 |
pnc_context_t *pnc_ctx) |
| 27 |
{ |
| 28 |
uint32_t ep_attr; |
| 29 |
|
| 30 |
/* Passing a NULL context is a critical programming error */ |
| 31 |
assert(pnc_ctx); |
| 32 |
assert(pnc_entry_point); |
| 33 |
assert(pc); |
| 34 |
|
| 35 |
/* Associate this context with the current cpu */ |
| 36 |
pnc_ctx->mpidr = read_mpidr(); |
| 37 |
|
| 38 |
cm_set_context(&pnc_ctx->cpu_ctx, SECURE); |
| 39 |
|
| 40 |
/* initialise an entrypoint to set up the CPU context */ |
| 41 |
ep_attr = SECURE | EP_ST_ENABLE; |
| 42 |
if (read_sctlr_el3() & SCTLR_EE_BIT) { |
| 43 |
ep_attr |= EP_EE_BIG; |
| 44 |
} |
| 45 |
SET_PARAM_HEAD(pnc_entry_point, PARAM_EP, VERSION_1, ep_attr); |
| 46 |
|
| 47 |
pnc_entry_point->pc = pc; |
| 48 |
pnc_entry_point->spsr = SPSR_64(MODE_EL1, |
| 49 |
MODE_SP_ELX, |
| 50 |
DISABLE_ALL_EXCEPTIONS); |
| 51 |
memset(&pnc_entry_point->args, 0, sizeof(pnc_entry_point->args)); |
| 52 |
} |
| 53 |
|
| 54 |
/******************************************************************************* |
| 55 |
* This function takes an SP context pointer and: |
| 56 |
* 1. Applies the S-EL1 system register context from pnc_ctx->cpu_ctx. |
| 57 |
* 2. Saves the current C runtime state (callee saved registers) on the stack |
| 58 |
* frame and saves a reference to this state. |
| 59 |
* 3. Calls el3_exit() so that the EL3 system and general purpose registers |
| 60 |
* from the pnc_ctx->cpu_ctx are used to enter the secure payload image. |
| 61 |
******************************************************************************/ |
| 62 |
uint64_t pncd_synchronous_sp_entry(pnc_context_t *pnc_ctx) |
| 63 |
{ |
| 64 |
assert(pnc_ctx != NULL); |
| 65 |
assert(pnc_ctx->c_rt_ctx == 0U); |
| 66 |
|
| 67 |
/* Apply the Secure EL1 system register context and switch to it */ |
| 68 |
assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx); |
| 69 |
cm_el1_sysregs_context_restore(SECURE); |
| 70 |
|
| 71 |
#if CTX_INCLUDE_FPREGS |
| 72 |
simd_ctx_restore(SECURE); |
| 73 |
#endif |
| 74 |
cm_set_next_eret_context(SECURE); |
| 75 |
|
| 76 |
return pncd_enter_sp(&pnc_ctx->c_rt_ctx); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/******************************************************************************* |
| 81 |
* This function takes an SP context pointer and: |
| 82 |
* 1. Saves the S-EL1 system register context tp pnc_ctx->cpu_ctx. |
| 83 |
* 2. Restores the current C runtime state (callee saved registers) from the |
| 84 |
* stack frame using the reference to this state saved in pncd_enter_sp(). |
| 85 |
* 3. It does not need to save any general purpose or EL3 system register state |
| 86 |
* as the generic smc entry routine should have saved those. |
| 87 |
******************************************************************************/ |
| 88 |
void pncd_synchronous_sp_exit(pnc_context_t *pnc_ctx, uint64_t ret) |
| 89 |
{ |
| 90 |
assert(pnc_ctx != NULL); |
| 91 |
/* Save the Secure EL1 system register context */ |
| 92 |
assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx); |
| 93 |
cm_el1_sysregs_context_save(SECURE); |
| 94 |
|
| 95 |
#if CTX_INCLUDE_FPREGS |
| 96 |
simd_ctx_save(SECURE, false); |
| 97 |
#endif |
| 98 |
|
| 99 |
assert(pnc_ctx->c_rt_ctx != 0); |
| 100 |
pncd_exit_sp(pnc_ctx->c_rt_ctx, ret); |
| 101 |
|
| 102 |
/* Should never reach here */ |
| 103 |
panic(); |
| 104 |
} |