Skip to content

Commit

Permalink
Merge changes from topic "rmm_sme" into integration
Browse files Browse the repository at this point in the history
* changes:
  fix(realm): initialize rec aux data during rec create
  fix(lib/arch): remove CPTR_EL2_RES1 and rename cptr_el2 fields
  • Loading branch information
soby-mathew authored and TrustedFirmware Code Review committed Sep 20, 2023
2 parents 808551f + 40b3bf0 commit c6213fa
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 172 deletions.
48 changes: 23 additions & 25 deletions lib/arch/include/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,31 +725,29 @@
#define SCTLR_EL2_RUNTIME (SCTLR_EL2_INIT | \
SCTLR_ELx_M_BIT /* MMU enabled */)

/* CPTR_EL2 definitions */
#define CPTR_EL2_RES1 ((UL(1) << 13) | (UL(1) << 12) | (UL(1) << 9) | 0xff)
#define CPTR_EL2_TTA (UL(1) << 28)
#define CPTR_EL2_TAM (UL(1) << 30)

#define CPTR_EL2_FPEN_SHIFT UL(20)
#define CPTR_EL2_FPEN_WIDTH UL(2)
#define CPTR_EL2_FPEN_TRAP_ALL_00 UL(0)
#define CPTR_EL2_FPEN_TRAP_TGE_01 UL(1)
#define CPTR_EL2_FPEN_TRAP_ALL_10 UL(2)
#define CPTR_EL2_FPEN_NO_TRAP_11 UL(3)

#define CPTR_EL2_ZEN_SHIFT UL(16)
#define CPTR_EL2_ZEN_WIDTH UL(2)
#define CPTR_EL2_ZEN_TRAP_ALL_00 UL(0)
#define CPTR_EL2_ZEN_NO_TRAP_11 UL(3)

/* Trap all FPU/SVE accesses */
#define CPTR_EL2_INIT ((CPTR_EL2_ZEN_TRAP_ALL_00 << \
CPTR_EL2_ZEN_SHIFT) | \
(CPTR_EL2_FPEN_TRAP_ALL_00 << \
CPTR_EL2_FPEN_SHIFT) | \
CPTR_EL2_TTA /* trap trace access */ | \
CPTR_EL2_TAM /* trap AMU access */ | \
CPTR_EL2_RES1)
/* RMM sets HCR_EL2.E2H to 1. CPTR_EL2 definitions when HCR_EL2.E2H == 1 */
#define CPTR_EL2_VHE_TTA (UL(1) << 28)
#define CPTR_EL2_VHE_TAM (UL(1) << 30)

#define CPTR_EL2_VHE_FPEN_SHIFT UL(20)
#define CPTR_EL2_VHE_FPEN_WIDTH UL(2)
#define CPTR_EL2_VHE_FPEN_TRAP_ALL_00 UL(0)
#define CPTR_EL2_VHE_FPEN_TRAP_TGE_01 UL(1)
#define CPTR_EL2_VHE_FPEN_TRAP_ALL_10 UL(2)
#define CPTR_EL2_VHE_FPEN_NO_TRAP_11 UL(3)

#define CPTR_EL2_VHE_ZEN_SHIFT UL(16)
#define CPTR_EL2_VHE_ZEN_WIDTH UL(2)
#define CPTR_EL2_VHE_ZEN_TRAP_ALL_00 UL(0x0)
#define CPTR_EL2_VHE_ZEN_NO_TRAP_11 UL(0x3)

/* Trap all AMU, trace, FPU, SVE accesses */
#define CPTR_EL2_VHE_INIT ((CPTR_EL2_VHE_ZEN_TRAP_ALL_00 << \
CPTR_EL2_VHE_ZEN_SHIFT) | \
(CPTR_EL2_VHE_FPEN_TRAP_ALL_00 << \
CPTR_EL2_VHE_FPEN_SHIFT) | \
CPTR_EL2_VHE_TTA | \
CPTR_EL2_VHE_TAM)

/* MDCR_EL2 definitions */
#define MDCR_EL2_HPMFZS (UL(1) << 36)
Expand Down
8 changes: 4 additions & 4 deletions lib/arch/include/arch_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ void inv_dcache_range(uintptr_t addr, size_t size);
/*******************************************************************************
* FPU management
******************************************************************************/
#define is_fpen_enabled() (EXTRACT(CPTR_EL2_FPEN, read_cptr_el2()) == \
CPTR_EL2_FPEN_NO_TRAP_11)
#define is_fpen_enabled() (EXTRACT(CPTR_EL2_VHE_FPEN, read_cptr_el2()) == \
CPTR_EL2_VHE_FPEN_NO_TRAP_11)

/*******************************************************************************
* SVE management
******************************************************************************/
#define is_zen_enabled() (EXTRACT(CPTR_EL2_ZEN, read_cptr_el2()) == \
CPTR_EL2_ZEN_NO_TRAP_11)
#define is_zen_enabled() (EXTRACT(CPTR_EL2_VHE_ZEN, read_cptr_el2()) == \
CPTR_EL2_VHE_ZEN_NO_TRAP_11)

/*******************************************************************************
* Misc. accessor prototypes
Expand Down
16 changes: 8 additions & 8 deletions lib/arch/include/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,18 @@ static inline void simd_enable(simd_t type)
unsigned long cptr;

cptr = read_cptr_el2();
cptr &= ~(MASK(CPTR_EL2_FPEN) | MASK(CPTR_EL2_ZEN));
cptr &= ~(MASK(CPTR_EL2_VHE_FPEN) | MASK(CPTR_EL2_VHE_ZEN));

switch (type) {
case SIMD_SVE:
assert(is_feat_sve_present());

cptr |= INPLACE(CPTR_EL2_ZEN, CPTR_EL2_ZEN_NO_TRAP_11);
cptr |= INPLACE(CPTR_EL2_FPEN, CPTR_EL2_FPEN_NO_TRAP_11);
cptr |= INPLACE(CPTR_EL2_VHE_ZEN, CPTR_EL2_VHE_ZEN_NO_TRAP_11);
cptr |= INPLACE(CPTR_EL2_VHE_FPEN, CPTR_EL2_VHE_FPEN_NO_TRAP_11);
break;
case SIMD_FPU:
cptr |= INPLACE(CPTR_EL2_ZEN, CPTR_EL2_ZEN_TRAP_ALL_00);
cptr |= INPLACE(CPTR_EL2_FPEN, CPTR_EL2_FPEN_NO_TRAP_11);
cptr |= INPLACE(CPTR_EL2_VHE_ZEN, CPTR_EL2_VHE_ZEN_TRAP_ALL_00);
cptr |= INPLACE(CPTR_EL2_VHE_FPEN, CPTR_EL2_VHE_FPEN_NO_TRAP_11);
break;
default:
assert(false);
Expand All @@ -185,10 +185,10 @@ static inline void simd_disable(void)
unsigned long cptr;

cptr = read_cptr_el2();
cptr &= ~(MASK(CPTR_EL2_FPEN) | MASK(CPTR_EL2_ZEN));
cptr &= ~(MASK(CPTR_EL2_VHE_FPEN) | MASK(CPTR_EL2_VHE_ZEN));

cptr |= INPLACE(CPTR_EL2_ZEN, CPTR_EL2_ZEN_TRAP_ALL_00);
cptr |= INPLACE(CPTR_EL2_FPEN, CPTR_EL2_FPEN_TRAP_ALL_00);
cptr |= INPLACE(CPTR_EL2_VHE_ZEN, CPTR_EL2_VHE_ZEN_TRAP_ALL_00);
cptr |= INPLACE(CPTR_EL2_VHE_FPEN, CPTR_EL2_VHE_FPEN_TRAP_ALL_00);

write_cptr_el2(cptr);
isb();
Expand Down
3 changes: 3 additions & 0 deletions lib/realm/include/granule.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ void granule_memzero(struct granule *g, enum buffer_slot slot);

void granule_memzero_mapped(void *buf);

void *aux_granules_map(struct granule *rec_aux_pages[], unsigned int num_aux);
void aux_granules_unmap(void *rec_aux, unsigned int num_aux);

/* Must be called with g->lock held */
static inline void __granule_get(struct granule *g)
{
Expand Down
6 changes: 1 addition & 5 deletions lib/realm/include/rec.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ struct common_sysreg_state {
struct rec_simd_state {
struct simd_state *simd; /* Pointer to SIMD context in AUX page */
bool simd_allowed; /* Set when REC is allowed to use SIMD */
bool init_done; /* flag used to check if SIMD state initialized */
};

/*
Expand All @@ -116,10 +115,7 @@ struct rec_attest_data {
struct token_sign_cntxt token_sign_ctx;

/* Buffer allocation info used for heap init and management */
struct {
struct buffer_alloc_ctx ctx;
bool ctx_initialised;
} alloc_info;
struct buffer_alloc_ctx alloc_ctx;
};
COMPILER_ASSERT(sizeof(struct rec_attest_data) <= GRANULE_SIZE);

Expand Down
37 changes: 37 additions & 0 deletions lib/realm/src/granule.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,40 @@ void granule_memzero_mapped(void *buf)
{
(void)memset(buf, 0, GRANULE_SIZE);
}

/*
* The parent REC granules lock is expected to be acquired before functions
* aux_granules_map() and aux_granules_unmap() are called.
*/
void *aux_granules_map(struct granule *rec_aux_pages[], unsigned int num_aux)
{
void *rec_aux = NULL;

assert(rec_aux_pages != NULL);
assert(num_aux <= MAX_REC_AUX_GRANULES);

for (unsigned int i = 0U; i < num_aux; i++) {
void *aux = granule_map(rec_aux_pages[i],
(enum buffer_slot)((unsigned int)
SLOT_REC_AUX0 + i));

assert(aux != NULL);

if (i == 0UL) {
rec_aux = aux;
}
}
return rec_aux;
}

void aux_granules_unmap(void *rec_aux, unsigned int num_aux)
{
unsigned char *rec_aux_vaddr = (unsigned char *)rec_aux;

assert(rec_aux != NULL);
assert(num_aux <= MAX_REC_AUX_GRANULES);

for (unsigned int i = 0U; i < num_aux; i++) {
buffer_unmap(rec_aux_vaddr + (i * GRANULE_SIZE));
}
}
2 changes: 1 addition & 1 deletion runtime/core/aarch64/head.S
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
mov_imm x2, HCR_EL2_INIT
msr hcr_el2, x2

mov_imm x3, CPTR_EL2_INIT
mov_imm x3, CPTR_EL2_VHE_INIT
msr cptr_el2, x3

mov_imm x4, ICC_SRE_EL2_INIT
Expand Down
113 changes: 6 additions & 107 deletions runtime/core/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <buffer.h>
#include <cpuid.h>
#include <exit.h>
#include <granule.h>
#include <pmu.h>
#include <rec.h>
#include <run.h>
Expand All @@ -20,68 +21,6 @@
static struct ns_state g_ns_data[MAX_CPUS];
static struct pmu_state g_pmu_data[MAX_CPUS];

/*
* Initialize pointers in @aux_data
*
* Call with parent REC granule's lock held.
*/
void init_rec_aux_data(struct rec_aux_data *aux_data, void *rec_aux,
unsigned long num_aux)
{
/*
* Ensure we have enough aux granules for use by REC:
* - REC_HEAP_PAGES for MbedTLS heap
* - REC_PMU_PAGES for PMU state
* - REC_SIMD_PAGES for SIMD state
* - REC_ATTEST_PAGES for 'rec_attest_data' structure
*/
assert(num_aux >= REC_NUM_PAGES);

aux_data->attest_heap_buf = (uint8_t *)rec_aux;
aux_data->pmu = (struct pmu_state *)((uint8_t *)rec_aux +
REC_HEAP_SIZE);
aux_data->rec_simd.simd = (struct simd_state *)((uint8_t *)rec_aux +
REC_HEAP_SIZE + REC_PMU_SIZE);
aux_data->attest_data = (struct rec_attest_data *)((uint8_t *)rec_aux +
REC_HEAP_SIZE + REC_PMU_SIZE + REC_SIMD_SIZE);
}

/*
* Map REC auxiliary Granules
*
* Call with parent REC granule's lock held.
*/
void *map_rec_aux(struct granule *rec_aux_pages[], unsigned long num_aux)
{
void *rec_aux = NULL;

for (unsigned long i = 0UL; i < num_aux; i++) {
void *aux = granule_map(rec_aux_pages[i],
(enum buffer_slot)((unsigned long)SLOT_REC_AUX0 + i));

assert(aux != NULL);

if (i == 0UL) {
rec_aux = aux;
}
}
return rec_aux;
}

/*
* Unmap REC auxiliary Granules
*
* Call with parent REC granule's lock held.
*/
void unmap_rec_aux(void *rec_aux, unsigned long num_aux)
{
unsigned char *rec_aux_vaddr = (unsigned char *)rec_aux;

for (unsigned long i = 0UL; i < num_aux; i++) {
buffer_unmap(rec_aux_vaddr + i * GRANULE_SIZE);
}
}

static void save_sysreg_state(struct sysreg_state *sysregs)
{
sysregs->sp_el0 = read_sp_el0();
Expand Down Expand Up @@ -290,31 +229,6 @@ void inject_serror(struct rec *rec, unsigned long vsesr)
rec->serror_info.inject = true;
}

/* Initialize REC simd state once on the first REC enter */
static void rec_simd_state_init(struct rec *rec)
{
struct rec_simd_state *rec_simd;
simd_t stype;

rec_simd = &rec->aux_data.rec_simd;
assert(rec_simd->simd != NULL);

if (rec_simd->init_done == true) {
return;
}

stype = rec_simd_type(rec);
/*
* As part of lazy save/restore, the first state will be restored from
* the REC's simd_state. So the initial state is considered saved, call
* simd_state_init() to set the simd type. sve_vq will be set if the REC
* 'stype' is SIMD_SVE.
*/
simd_state_init(stype, rec_simd->simd, rec->realm_info.sve_vq);
rec_simd->simd_allowed = false;
rec_simd->init_done = true;
}

/* Save the REC SIMD state to memory and disable simd access for the REC */
void rec_simd_save_disable(struct rec *rec)
{
Expand Down Expand Up @@ -362,7 +276,6 @@ void rec_run_loop(struct rec *rec, struct rmi_rec_exit *rec_exit)
int realm_exception_code;
void *rec_aux;
unsigned int cpuid = my_cpuid();
struct rec_attest_data *attest_data;
int ret __unused;

assert(cpuid < MAX_CPUS);
Expand All @@ -376,29 +289,15 @@ void rec_run_loop(struct rec *rec, struct rmi_rec_exit *rec_exit)
rec->ns = ns_state;

/* Map auxiliary granules */
rec_aux = map_rec_aux(rec->g_aux, rec->num_rec_aux);
rec_aux = aux_granules_map(rec->g_aux, rec->num_rec_aux);

/*
* The attset heap on the REC aux pages is mapped now. It is time to
* associate it with the current CPU.
* This heap will be used for attestation RSI calls when the
* REC is running.
* Associate the attest heap with the current CPU. This heap will be
* used for attestation RSI calls when the REC is running.
*/
attest_data = rec->aux_data.attest_data;
ret = attestation_heap_ctx_assign_pe(&attest_data->alloc_info.ctx);
ret = attestation_heap_ctx_assign_pe(&rec->aux_data.attest_data->alloc_ctx);
assert(ret == 0);

/*
* Initialise the heap for attestation if necessary.
*/
if (!attest_data->alloc_info.ctx_initialised) {
(void)attestation_heap_ctx_init(rec->aux_data.attest_heap_buf,
REC_HEAP_SIZE);
attest_data->alloc_info.ctx_initialised = true;
}

rec_simd_state_init(rec);

ns_state->pmu = &g_pmu_data[cpuid];

save_ns_state(rec);
Expand Down Expand Up @@ -469,5 +368,5 @@ void rec_run_loop(struct rec *rec, struct rmi_rec_exit *rec_exit)
assert(ret == 0);

/* Unmap auxiliary granules */
unmap_rec_aux(rec_aux, rec->num_rec_aux);
aux_granules_unmap(rec_aux, rec->num_rec_aux);
}
7 changes: 0 additions & 7 deletions runtime/include/run.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
#ifndef RUN_H
#define RUN_H

struct granule;
struct rec;
struct rec_aux_data;

void init_rec_aux_data(struct rec_aux_data *aux_data, void *rec_aux,
unsigned long num_aux);
void *map_rec_aux(struct granule *rec_aux_pages[], unsigned long num_aux);
void unmap_rec_aux(void *rec_aux, unsigned long num_aux);

/*
* Function to enter Realm with `regs` pointing to GP Regs to be
Expand Down
Loading

0 comments on commit c6213fa

Please sign in to comment.