blob: 6fdb6293f88f31005024ae7897a70f9d57af6c51 [file] [log] [blame]
Florian Lugou71d33be2021-09-08 12:42:54 +02001/*
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 ******************************************************************************/
24void 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 ******************************************************************************/
62uint64_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#if CTX_INCLUDE_FPREGS
71 fpregs_context_restore(get_fpregs_ctx(cm_get_context(SECURE)));
72#endif
73 cm_set_next_eret_context(SECURE);
74
75 return pncd_enter_sp(&pnc_ctx->c_rt_ctx);
76}
77
78
79/*******************************************************************************
80 * This function takes an SP context pointer and:
81 * 1. Saves the S-EL1 system register context tp pnc_ctx->cpu_ctx.
82 * 2. Restores the current C runtime state (callee saved registers) from the
83 * stack frame using the reference to this state saved in pncd_enter_sp().
84 * 3. It does not need to save any general purpose or EL3 system register state
85 * as the generic smc entry routine should have saved those.
86 ******************************************************************************/
87void pncd_synchronous_sp_exit(pnc_context_t *pnc_ctx, uint64_t ret)
88{
89 assert(pnc_ctx != NULL);
90 /* Save the Secure EL1 system register context */
91 assert(cm_get_context(SECURE) == &pnc_ctx->cpu_ctx);
92 cm_el1_sysregs_context_save(SECURE);
93#if CTX_INCLUDE_FPREGS
94 fpregs_context_save(get_fpregs_ctx(cm_get_context(SECURE)));
95#endif
96
97 assert(pnc_ctx->c_rt_ctx != 0);
98 pncd_exit_sp(pnc_ctx->c_rt_ctx, ret);
99
100 /* Should never reach here */
101 panic();
102}