Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 13 | #include <gic.h> |
| 14 | #include <memory_alloc.h> |
Arvind Ram Prakash | bd36a1b | 2022-12-15 12:16:36 -0600 | [diff] [blame] | 15 | #include <pauth.h> |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 16 | #include <pmu.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 17 | #include <ripas.h> |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 18 | #include <simd.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 19 | #include <sizes.h> |
| 20 | #include <smc-rmi.h> |
| 21 | #include <utils_def.h> |
| 22 | |
| 23 | struct granule; |
| 24 | |
| 25 | /* |
| 26 | * System registers whose contents are specific to a REC. |
| 27 | */ |
| 28 | struct 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 34 | 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 */ |
Arunachalam Ganapathy | 4f601e7 | 2023-05-22 11:49:29 +0100 | [diff] [blame] | 78 | |
| 79 | unsigned long cptr_el2; /* restored only */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* |
| 83 | * System registers whose contents are |
| 84 | * common across all RECs in a Realm. |
| 85 | */ |
| 86 | struct common_sysreg_state { |
| 87 | unsigned long vttbr_el2; |
| 88 | unsigned long vtcr_el2; |
| 89 | unsigned long hcr_el2; |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 90 | unsigned long mdcr_el2; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /* |
| 94 | * This structure is aligned on cache line size to avoid cache line trashing |
| 95 | * when allocated as an array for N CPUs. |
| 96 | */ |
| 97 | struct ns_state { |
| 98 | struct sysreg_state sysregs; |
| 99 | unsigned long sp_el0; |
| 100 | unsigned long icc_sre_el2; |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 101 | struct pmu_state *pmu; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 102 | } __attribute__((aligned(CACHE_WRITEBACK_GRANULE))); |
| 103 | |
| 104 | /* |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 105 | * Data used when handling attestation requests |
| 106 | */ |
| 107 | struct rec_attest_data { |
| 108 | unsigned char rmm_realm_token_buf[SZ_1K]; |
| 109 | size_t rmm_realm_token_len; |
| 110 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 111 | struct token_sign_cntxt token_sign_ctx; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 112 | |
| 113 | /* Buffer allocation info used for heap init and management */ |
Arunachalam Ganapathy | 40b3bf0 | 2023-06-12 12:19:55 +0100 | [diff] [blame] | 114 | struct buffer_alloc_ctx alloc_ctx; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 115 | }; |
| 116 | COMPILER_ASSERT(sizeof(struct rec_attest_data) <= GRANULE_SIZE); |
| 117 | |
| 118 | /* |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 119 | * This structure contains pointers to data that are allocated |
| 120 | * in auxilary granules for a REC. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 121 | */ |
| 122 | struct rec_aux_data { |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 123 | /* Pointer to the heap buffer */ |
| 124 | uint8_t *attest_heap_buf; |
| 125 | |
| 126 | /* Pointer to PMU state */ |
| 127 | struct pmu_state *pmu; |
| 128 | |
| 129 | /* SIMD context region */ |
Arunachalam Ganapathy | 4f601e7 | 2023-05-22 11:49:29 +0100 | [diff] [blame] | 130 | struct simd_context *simd_ctx; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 131 | |
| 132 | /* Pointer to attestation-related data */ |
| 133 | struct rec_attest_data *attest_data; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | struct rec { |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 137 | struct granule *g_rec; /* the granule in which this REC lives */ |
| 138 | unsigned long rec_idx; /* which REC is this */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 139 | bool runnable; |
| 140 | |
| 141 | unsigned long regs[31]; |
Arvind Ram Prakash | bd36a1b | 2022-12-15 12:16:36 -0600 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * PAuth state of Realm. |
| 145 | * Note that we do not need to save NS state as EL3 will save this as part of world switch. |
| 146 | */ |
| 147 | struct pauth_state pauth; |
| 148 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 149 | unsigned long pc; |
| 150 | unsigned long pstate; |
| 151 | |
| 152 | struct sysreg_state sysregs; |
| 153 | struct common_sysreg_state common_sysregs; |
| 154 | |
AlexeiFedorov | 5cf35ba | 2023-04-25 10:02:20 +0100 | [diff] [blame] | 155 | /* Populated when the REC issues a RIPAS change request */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 156 | struct { |
AlexeiFedorov | 5cf35ba | 2023-04-25 10:02:20 +0100 | [diff] [blame] | 157 | unsigned long base; |
| 158 | unsigned long top; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 159 | unsigned long addr; |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 160 | enum ripas ripas_val; |
AlexeiFedorov | 63614ea | 2023-07-14 17:07:20 +0100 | [diff] [blame] | 161 | enum ripas_change_destroyed change_destroyed; |
AlexeiFedorov | 9b29c6b | 2023-09-12 17:09:50 +0100 | [diff] [blame] | 162 | enum ripas_response response; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 163 | } set_ripas; |
| 164 | |
| 165 | /* |
| 166 | * Common values across all RECs in a Realm. |
| 167 | */ |
| 168 | struct { |
| 169 | unsigned long ipa_bits; |
| 170 | int s2_starting_level; |
| 171 | struct granule *g_rtt; |
| 172 | struct granule *g_rd; |
AlexeiFedorov | eaec0c4 | 2023-02-01 18:13:32 +0000 | [diff] [blame] | 173 | bool pmu_enabled; |
AlexeiFedorov | 1800292 | 2023-04-06 10:19:51 +0100 | [diff] [blame] | 174 | unsigned int pmu_num_ctrs; |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 175 | enum hash_algo algorithm; |
Arunachalam Ganapathy | 4f601e7 | 2023-05-22 11:49:29 +0100 | [diff] [blame] | 176 | struct simd_config simd_cfg; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 177 | } realm_info; |
| 178 | |
| 179 | struct { |
| 180 | /* |
| 181 | * The contents of the *_EL2 system registers at the last time |
| 182 | * the REC exited to the host due to a synchronous exception. |
| 183 | * These are the unsanitized register values which may differ |
| 184 | * from the value returned to the host in rec_exit structure. |
| 185 | */ |
| 186 | unsigned long esr; |
| 187 | unsigned long hpfar; |
| 188 | unsigned long far; |
| 189 | } last_run_info; |
| 190 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 191 | /* Pointer to per-cpu non-secure state */ |
| 192 | struct ns_state *ns; |
| 193 | |
| 194 | struct { |
| 195 | /* |
| 196 | * Set to 'true' when there is a pending PSCI |
| 197 | * command that must be resolved by the host. |
| 198 | * The command is encoded in rec->regs[0]. |
| 199 | * |
| 200 | * A REC with pending PSCI is not schedulable. |
| 201 | */ |
| 202 | bool pending; |
| 203 | } psci_info; |
| 204 | |
| 205 | /* Number of auxiliary granules */ |
| 206 | unsigned int num_rec_aux; |
| 207 | |
| 208 | /* Addresses of auxiliary granules */ |
| 209 | struct granule *g_aux[MAX_REC_AUX_GRANULES]; |
| 210 | struct rec_aux_data aux_data; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 211 | struct { |
| 212 | unsigned long vsesr_el2; |
| 213 | bool inject; |
| 214 | } serror_info; |
| 215 | |
| 216 | /* True if host call is pending */ |
| 217 | bool host_call; |
Arunachalam Ganapathy | 4f601e7 | 2023-05-22 11:49:29 +0100 | [diff] [blame] | 218 | |
| 219 | /* The active SIMD context that is live in CPU registers */ |
| 220 | struct simd_context *active_simd_ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 221 | }; |
| 222 | COMPILER_ASSERT(sizeof(struct rec) <= GRANULE_SIZE); |
| 223 | |
| 224 | /* |
| 225 | * Check that mpidr has a valid value with all fields except |
| 226 | * Aff3[39:32]:Aff2[23:16]:Aff1[15:8]:Aff0[3:0] set to 0. |
| 227 | */ |
| 228 | static inline bool mpidr_is_valid(unsigned long mpidr) |
| 229 | { |
| 230 | return (mpidr & ~(MASK(MPIDR_EL2_AFF0) | |
| 231 | MASK(MPIDR_EL2_AFF1) | |
| 232 | MASK(MPIDR_EL2_AFF2) | |
| 233 | MASK(MPIDR_EL2_AFF3))) == 0ULL; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Calculate REC index from mpidr value. |
| 238 | * index = Aff3[39:32]:Aff2[23:16]:Aff1[15:8]:Aff0[3:0] |
| 239 | */ |
| 240 | static inline unsigned long mpidr_to_rec_idx(unsigned long mpidr) |
| 241 | { |
| 242 | return (MPIDR_EL2_AFF(0, mpidr) + |
| 243 | MPIDR_EL2_AFF(1, mpidr) + |
| 244 | MPIDR_EL2_AFF(2, mpidr) + |
| 245 | MPIDR_EL2_AFF(3, mpidr)); |
| 246 | } |
| 247 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 248 | void rec_run_loop(struct rec *rec, struct rmi_rec_exit *rec_exit); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 249 | void inject_serror(struct rec *rec, unsigned long vsesr); |
AlexeiFedorov | 950bd7b | 2023-08-24 15:25:22 +0100 | [diff] [blame] | 250 | void emulate_stage2_data_abort(struct rec *rec, struct rmi_rec_exit *rec_exit, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 251 | unsigned long rtt_level); |
| 252 | |
| 253 | #endif /* __ASSEMBLER__ */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 254 | #endif /* REC_H */ |