2017-10-24 10:07:35 +01:00
|
|
|
/*
|
2019-03-27 11:10:31 +00:00
|
|
|
* Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
|
2017-10-24 10:07:35 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
2018-12-14 00:18:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <platform_def.h>
|
|
|
|
|
2017-10-24 10:07:35 +01:00
|
|
|
#include <arch.h>
|
|
|
|
#include <arch_helpers.h>
|
|
|
|
#include <context.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
#include <common/debug.h>
|
|
|
|
#include <lib/el3_runtime/context_mgmt.h>
|
|
|
|
#include <lib/xlat_tables/xlat_tables_v2.h>
|
|
|
|
#include <plat/common/common_def.h>
|
|
|
|
#include <plat/common/platform.h>
|
|
|
|
#include <services/sp_res_desc.h>
|
2018-11-08 14:21:19 +00:00
|
|
|
#include <sprt_host.h>
|
2017-10-24 10:07:35 +01:00
|
|
|
|
|
|
|
#include "spm_private.h"
|
|
|
|
#include "spm_shim_private.h"
|
|
|
|
|
|
|
|
/* Setup context of the Secure Partition */
|
2018-05-22 16:26:48 +01:00
|
|
|
void spm_sp_setup(sp_context_t *sp_ctx)
|
2017-10-24 10:07:35 +01:00
|
|
|
{
|
2018-05-23 11:40:46 +01:00
|
|
|
cpu_context_t *ctx = &(sp_ctx->cpu_ctx);
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2018-05-23 11:40:46 +01:00
|
|
|
/*
|
|
|
|
* Initialize CPU context
|
|
|
|
* ----------------------
|
|
|
|
*/
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2018-05-23 11:40:46 +01:00
|
|
|
entry_point_info_t ep_info = {0};
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2018-05-23 11:40:46 +01:00
|
|
|
SET_PARAM_HEAD(&ep_info, PARAM_EP, VERSION_1, SECURE | EP_ST_ENABLE);
|
2018-06-11 11:01:54 +01:00
|
|
|
|
|
|
|
/* Setup entrypoint and SPSR */
|
2018-06-26 10:34:25 +01:00
|
|
|
ep_info.pc = sp_ctx->rd.attribute.entrypoint;
|
2018-05-23 11:40:46 +01:00
|
|
|
ep_info.spsr = SPSR_64(MODE_EL0, MODE_SP_EL0, DISABLE_ALL_EXCEPTIONS);
|
2017-10-24 10:07:35 +01:00
|
|
|
|
|
|
|
/*
|
2018-10-30 11:54:20 +00:00
|
|
|
* X0: Unused (MBZ).
|
|
|
|
* X1: Unused (MBZ).
|
2017-10-24 10:07:35 +01:00
|
|
|
* X2: cookie value (Implementation Defined)
|
|
|
|
* X3: cookie value (Implementation Defined)
|
2018-06-11 11:01:54 +01:00
|
|
|
* X4 to X7 = 0
|
2017-10-24 10:07:35 +01:00
|
|
|
*/
|
2018-10-30 11:54:20 +00:00
|
|
|
ep_info.args.arg0 = 0;
|
|
|
|
ep_info.args.arg1 = 0;
|
2018-06-11 11:01:54 +01:00
|
|
|
ep_info.args.arg2 = PLAT_SPM_COOKIE_0;
|
|
|
|
ep_info.args.arg3 = PLAT_SPM_COOKIE_1;
|
|
|
|
|
|
|
|
cm_setup_context(ctx, &ep_info);
|
2017-10-24 10:07:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup translation tables
|
|
|
|
* ------------------------
|
|
|
|
*/
|
|
|
|
|
2019-03-27 13:04:46 +00:00
|
|
|
/* Assign translation tables context. */
|
|
|
|
spm_sp_xlat_context_alloc(sp_ctx);
|
|
|
|
|
2018-10-30 11:34:23 +00:00
|
|
|
sp_map_memory_regions(sp_ctx);
|
2017-10-24 10:07:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MMU-related registers
|
|
|
|
* ---------------------
|
|
|
|
*/
|
2018-07-11 13:07:06 +01:00
|
|
|
xlat_ctx_t *xlat_ctx = sp_ctx->xlat_ctx_handle;
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2018-07-11 13:07:06 +01:00
|
|
|
uint64_t mmu_cfg_params[MMU_CFG_PARAM_MAX];
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2018-07-11 13:07:06 +01:00
|
|
|
setup_mmu_cfg((uint64_t *)&mmu_cfg_params, 0, xlat_ctx->base_table,
|
|
|
|
xlat_ctx->pa_max_address, xlat_ctx->va_max_address,
|
|
|
|
EL1_EL0_REGIME);
|
|
|
|
|
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_MAIR_EL1,
|
|
|
|
mmu_cfg_params[MMU_CFG_MAIR]);
|
|
|
|
|
2019-03-27 11:10:31 +00:00
|
|
|
/* Enable translations using TTBR1_EL1 */
|
|
|
|
int t1sz = 64 - __builtin_ctzll(SPM_SHIM_XLAT_VIRT_ADDR_SPACE_SIZE);
|
|
|
|
mmu_cfg_params[MMU_CFG_TCR] &= ~TCR_EPD1_BIT;
|
|
|
|
mmu_cfg_params[MMU_CFG_TCR] |=
|
|
|
|
((uint64_t)t1sz << TCR_T1SZ_SHIFT) |
|
|
|
|
TCR_SH1_INNER_SHAREABLE |
|
|
|
|
TCR_RGN1_OUTER_WBA | TCR_RGN1_INNER_WBA |
|
|
|
|
TCR_TG1_4K;
|
|
|
|
|
2018-07-11 13:07:06 +01:00
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_TCR_EL1,
|
|
|
|
mmu_cfg_params[MMU_CFG_TCR]);
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2018-07-11 13:07:06 +01:00
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR0_EL1,
|
|
|
|
mmu_cfg_params[MMU_CFG_TTBR0]);
|
2017-10-24 10:07:35 +01:00
|
|
|
|
2019-03-27 11:10:31 +00:00
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR1_EL1,
|
|
|
|
(uint64_t)spm_exceptions_xlat_get_base_table());
|
|
|
|
|
2017-10-24 10:07:35 +01:00
|
|
|
/* Setup SCTLR_EL1 */
|
|
|
|
u_register_t sctlr_el1 = read_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1);
|
|
|
|
|
|
|
|
sctlr_el1 |=
|
|
|
|
/*SCTLR_EL1_RES1 |*/
|
|
|
|
/* Don't trap DC CVAU, DC CIVAC, DC CVAC, DC CVAP, or IC IVAU */
|
|
|
|
SCTLR_UCI_BIT |
|
|
|
|
/* RW regions at xlat regime EL1&0 are forced to be XN. */
|
|
|
|
SCTLR_WXN_BIT |
|
|
|
|
/* Don't trap to EL1 execution of WFI or WFE at EL0. */
|
|
|
|
SCTLR_NTWI_BIT | SCTLR_NTWE_BIT |
|
|
|
|
/* Don't trap to EL1 accesses to CTR_EL0 from EL0. */
|
|
|
|
SCTLR_UCT_BIT |
|
|
|
|
/* Don't trap to EL1 execution of DZ ZVA at EL0. */
|
|
|
|
SCTLR_DZE_BIT |
|
|
|
|
/* Enable SP Alignment check for EL0 */
|
|
|
|
SCTLR_SA0_BIT |
|
|
|
|
/* Allow cacheable data and instr. accesses to normal memory. */
|
|
|
|
SCTLR_C_BIT | SCTLR_I_BIT |
|
|
|
|
/* Alignment fault checking enabled when at EL1 and EL0. */
|
|
|
|
SCTLR_A_BIT |
|
|
|
|
/* Enable MMU. */
|
|
|
|
SCTLR_M_BIT
|
|
|
|
;
|
|
|
|
|
|
|
|
sctlr_el1 &= ~(
|
|
|
|
/* Explicit data accesses at EL0 are little-endian. */
|
|
|
|
SCTLR_E0E_BIT |
|
|
|
|
/* Accesses to DAIF from EL0 are trapped to EL1. */
|
|
|
|
SCTLR_UMA_BIT
|
|
|
|
);
|
|
|
|
|
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_el1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup other system registers
|
|
|
|
* ----------------------------
|
|
|
|
*/
|
|
|
|
|
2019-03-27 11:10:31 +00:00
|
|
|
/*
|
|
|
|
* Shim exception vector base address. It is mapped at the start of the
|
|
|
|
* address space accessed by TTBR1_EL1, which means that the base
|
|
|
|
* address of the exception vectors depends on the size of the address
|
|
|
|
* space specified in TCR_EL1.T1SZ.
|
|
|
|
*/
|
2017-10-24 10:07:35 +01:00
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_VBAR_EL1,
|
2019-03-27 11:10:31 +00:00
|
|
|
UINT64_MAX - (SPM_SHIM_XLAT_VIRT_ADDR_SPACE_SIZE - 1ULL));
|
2017-10-24 10:07:35 +01:00
|
|
|
|
|
|
|
/*
|
2018-05-09 13:45:34 +01:00
|
|
|
* FPEN: Allow the Secure Partition to access FP/SIMD registers.
|
|
|
|
* Note that SPM will not do any saving/restoring of these registers on
|
|
|
|
* behalf of the SP. This falls under the SP's responsibility.
|
2017-10-24 10:07:35 +01:00
|
|
|
* TTA: Enable access to trace registers.
|
|
|
|
* ZEN (v8.2): Trap SVE instructions and access to SVE registers.
|
|
|
|
*/
|
|
|
|
write_ctx_reg(get_sysregs_ctx(ctx), CTX_CPACR_EL1,
|
2018-05-09 13:45:34 +01:00
|
|
|
CPACR_EL1_FPEN(CPACR_EL1_FP_TRAP_NONE));
|
2018-11-08 14:21:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare shared buffers
|
|
|
|
* ----------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Initialize SPRT queues */
|
|
|
|
sprt_initialize_queues((void *)sp_ctx->spm_sp_buffer_base,
|
|
|
|
sp_ctx->spm_sp_buffer_size);
|
2017-10-24 10:07:35 +01:00
|
|
|
}
|