blob: 34cc31bcd541c03b6935c42242f54d27c436e618 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#ifndef REC_H
7#define REC_H
8
9#ifndef __ASSEMBLER__
10
11#include <arch.h>
12#include <attestation_token.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000013#include <gic.h>
14#include <memory_alloc.h>
Arvind Ram Prakashbd36a1b2022-12-15 12:16:36 -060015#include <pauth.h>
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000016#include <pmu.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000017#include <ripas.h>
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000018#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000019#include <sizes.h>
20#include <smc-rmi.h>
21#include <utils_def.h>
22
23struct granule;
24
25/*
26 * System registers whose contents are specific to a REC.
27 */
28struct sysreg_state {
29 unsigned long sp_el0;
30 unsigned long sp_el1;
31 unsigned long elr_el1;
32 unsigned long spsr_el1;
33 unsigned long pmcr_el0;
Soby Mathewb4c6df42022-11-09 11:13:29 +000034 unsigned long tpidrro_el0;
35 unsigned long tpidr_el0;
36 unsigned long csselr_el1;
37 unsigned long sctlr_el1;
38 unsigned long actlr_el1;
39 unsigned long cpacr_el1;
40 unsigned long zcr_el1;
41 unsigned long ttbr0_el1;
42 unsigned long ttbr1_el1;
43 unsigned long tcr_el1;
44 unsigned long esr_el1;
45 unsigned long afsr0_el1;
46 unsigned long afsr1_el1;
47 unsigned long far_el1;
48 unsigned long mair_el1;
49 unsigned long vbar_el1;
50 unsigned long contextidr_el1;
51 unsigned long tpidr_el1;
52 unsigned long amair_el1;
53 unsigned long cntkctl_el1;
54 unsigned long par_el1;
55 unsigned long mdscr_el1;
56 unsigned long mdccint_el1;
57 unsigned long disr_el1;
58 unsigned long mpam0_el1;
59
60 /* Timer Registers */
61 unsigned long cnthctl_el2;
62 unsigned long cntvoff_el2;
63 unsigned long cntpoff_el2;
64 unsigned long cntp_ctl_el0;
65 unsigned long cntp_cval_el0;
66 unsigned long cntv_ctl_el0;
67 unsigned long cntv_cval_el0;
68
69 /* GIC Registers */
70 struct gic_cpu_state gicstate;
71
72 /* TODO MPAM */
73 /* TODO Performance Monitor Registers */
74 /* TODO Pointer Authentication Registers */
75
76 unsigned long vmpidr_el2; /* restored only */
77 unsigned long hcr_el2; /* restored only */
78};
79
80/*
81 * System registers whose contents are
82 * common across all RECs in a Realm.
83 */
84struct common_sysreg_state {
85 unsigned long vttbr_el2;
86 unsigned long vtcr_el2;
87 unsigned long hcr_el2;
AlexeiFedoroveaec0c42023-02-01 18:13:32 +000088 unsigned long mdcr_el2;
Soby Mathewb4c6df42022-11-09 11:13:29 +000089};
90
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000091/* This structure is used for storing FPU or SVE context for realm. */
92struct rec_simd_state {
93 struct simd_state *simd; /* Pointer to SIMD context in AUX page */
94 bool simd_allowed; /* Set when REC is allowed to use SIMD */
95 bool init_done; /* flag used to check if SIMD state initialized */
96};
97
Soby Mathewb4c6df42022-11-09 11:13:29 +000098/*
99 * This structure is aligned on cache line size to avoid cache line trashing
100 * when allocated as an array for N CPUs.
101 */
102struct ns_state {
103 struct sysreg_state sysregs;
104 unsigned long sp_el0;
105 unsigned long icc_sre_el2;
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000106 struct pmu_state *pmu;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000107} __attribute__((aligned(CACHE_WRITEBACK_GRANULE)));
108
109/*
AlexeiFedorovec35c542023-04-27 17:52:02 +0100110 * Data used when handling attestation requests
111 */
112struct rec_attest_data {
113 unsigned char rmm_realm_token_buf[SZ_1K];
114 size_t rmm_realm_token_len;
115
116 struct token_sign_ctx token_sign_ctx;
117
118 /* Buffer allocation info used for heap init and management */
119 struct {
120 struct buffer_alloc_ctx ctx;
121 bool ctx_initialised;
122 } alloc_info;
123};
124COMPILER_ASSERT(sizeof(struct rec_attest_data) <= GRANULE_SIZE);
125
126/*
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000127 * This structure contains pointers to data that are allocated
128 * in auxilary granules for a REC.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000129 */
130struct rec_aux_data {
AlexeiFedorovec35c542023-04-27 17:52:02 +0100131 /* Pointer to the heap buffer */
132 uint8_t *attest_heap_buf;
133
134 /* Pointer to PMU state */
135 struct pmu_state *pmu;
136
137 /* SIMD context region */
138 struct rec_simd_state rec_simd;
139
140 /* Pointer to attestation-related data */
141 struct rec_attest_data *attest_data;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000142};
143
144struct rec {
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000145 struct granule *g_rec; /* the granule in which this REC lives */
146 unsigned long rec_idx; /* which REC is this */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000147 bool runnable;
148
149 unsigned long regs[31];
Arvind Ram Prakashbd36a1b2022-12-15 12:16:36 -0600150
151 /*
152 * PAuth state of Realm.
153 * Note that we do not need to save NS state as EL3 will save this as part of world switch.
154 */
155 struct pauth_state pauth;
156
Soby Mathewb4c6df42022-11-09 11:13:29 +0000157 unsigned long pc;
158 unsigned long pstate;
159
160 struct sysreg_state sysregs;
161 struct common_sysreg_state common_sysregs;
162
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +0100163 /* Populated when the REC issues a RIPAS change request */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000164 struct {
AlexeiFedorov5cf35ba2023-04-25 10:02:20 +0100165 unsigned long base;
166 unsigned long top;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000167 unsigned long addr;
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100168 enum ripas ripas_val;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000169 } set_ripas;
170
171 /*
172 * Common values across all RECs in a Realm.
173 */
174 struct {
175 unsigned long ipa_bits;
176 int s2_starting_level;
177 struct granule *g_rtt;
178 struct granule *g_rd;
AlexeiFedoroveaec0c42023-02-01 18:13:32 +0000179 bool pmu_enabled;
AlexeiFedorov18002922023-04-06 10:19:51 +0100180 unsigned int pmu_num_ctrs;
AlexeiFedorov531b5272023-06-13 15:36:32 +0100181 enum hash_algo hash_algo;
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000182 bool sve_enabled;
183 uint8_t sve_vq;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000184 } realm_info;
185
186 struct {
187 /*
188 * The contents of the *_EL2 system registers at the last time
189 * the REC exited to the host due to a synchronous exception.
190 * These are the unsanitized register values which may differ
191 * from the value returned to the host in rec_exit structure.
192 */
193 unsigned long esr;
194 unsigned long hpfar;
195 unsigned long far;
196 } last_run_info;
197
Soby Mathewb4c6df42022-11-09 11:13:29 +0000198 /* Pointer to per-cpu non-secure state */
199 struct ns_state *ns;
200
201 struct {
202 /*
203 * Set to 'true' when there is a pending PSCI
204 * command that must be resolved by the host.
205 * The command is encoded in rec->regs[0].
206 *
207 * A REC with pending PSCI is not schedulable.
208 */
209 bool pending;
210 } psci_info;
211
212 /* Number of auxiliary granules */
213 unsigned int num_rec_aux;
214
215 /* Addresses of auxiliary granules */
216 struct granule *g_aux[MAX_REC_AUX_GRANULES];
217 struct rec_aux_data aux_data;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000218 struct {
219 unsigned long vsesr_el2;
220 bool inject;
221 } serror_info;
222
223 /* True if host call is pending */
224 bool host_call;
225};
226COMPILER_ASSERT(sizeof(struct rec) <= GRANULE_SIZE);
227
228/*
229 * Check that mpidr has a valid value with all fields except
230 * Aff3[39:32]:Aff2[23:16]:Aff1[15:8]:Aff0[3:0] set to 0.
231 */
232static inline bool mpidr_is_valid(unsigned long mpidr)
233{
234 return (mpidr & ~(MASK(MPIDR_EL2_AFF0) |
235 MASK(MPIDR_EL2_AFF1) |
236 MASK(MPIDR_EL2_AFF2) |
237 MASK(MPIDR_EL2_AFF3))) == 0ULL;
238}
239
240/*
241 * Calculate REC index from mpidr value.
242 * index = Aff3[39:32]:Aff2[23:16]:Aff1[15:8]:Aff0[3:0]
243 */
244static inline unsigned long mpidr_to_rec_idx(unsigned long mpidr)
245{
246 return (MPIDR_EL2_AFF(0, mpidr) +
247 MPIDR_EL2_AFF(1, mpidr) +
248 MPIDR_EL2_AFF(2, mpidr) +
249 MPIDR_EL2_AFF(3, mpidr));
250}
251
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000252static inline simd_t rec_simd_type(struct rec *rec)
253{
254 if (rec->realm_info.sve_enabled) {
255 return SIMD_SVE;
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000256 }
AlexeiFedorov18002922023-04-06 10:19:51 +0100257 return SIMD_FPU;
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +0000258}
259
260static inline bool rec_is_simd_allowed(struct rec *rec)
261{
262 assert(rec != NULL);
263 return rec->aux_data.rec_simd.simd_allowed;
264}
265
Soby Mathewb4c6df42022-11-09 11:13:29 +0000266void rec_run_loop(struct rec *rec, struct rmi_rec_exit *rec_exit);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000267void inject_serror(struct rec *rec, unsigned long vsesr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000268void emulate_stage2_data_abort(struct rec *rec, struct rmi_rec_exit *exit,
269 unsigned long rtt_level);
270
271#endif /* __ASSEMBLER__ */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000272#endif /* REC_H */