blob: 3c659e313add7cda35e61fc07e4571e6947219f6 [file] [log] [blame]
Igor Podgainõi4b672102024-09-23 13:06:15 +02001/*
2 * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef CONTEXT_EL2_H
8#define CONTEXT_EL2_H
9
10#include <stdint.h>
11
12#include <arch_features.h>
13#include <arch_helpers.h>
14#include <debug.h>
15
16/**
17 * Register mask for EL2 corruption test.
18 */
19#define REG_CORRUPTION_MASK ULL(0xffffffffffffffff)
20
21/**
22 * Bit masks for sensitive fields of various registers.
23 */
24#define SCTLR_EL2_EE ULL(0x2000000)
25#define TTBR1_EL2_ASID ULL(0xffff000000000000)
26#define TCR2_EL2_POE ULL(0x8)
27#define GCSCR_EL2_PCRSEL ULL(0x1)
28
29/**
30 * Enum of the supported operations for the el2_modify_registers API.
31 */
32typedef enum {
33 CORRUPT_REGS,
34 RESTORE_REGS
35} el2_modify_operation_t;
36
37/*******************************************************************************
38 * EL2 Registers:
39 * AArch64 EL2 system register context structure for preserving the
40 * architectural state during world switches.
41 ******************************************************************************/
42typedef struct el2_common_regs {
43 uint64_t actlr_el2;
44 uint64_t afsr0_el2;
45 uint64_t afsr1_el2;
46 uint64_t amair_el2;
47 uint64_t cnthctl_el2;
48 uint64_t cntvoff_el2;
49 uint64_t cptr_el2;
50 uint64_t dbgvcr32_el2;
51 uint64_t elr_el2;
52 uint64_t esr_el2;
53 uint64_t far_el2;
54 uint64_t hacr_el2;
55 uint64_t hcr_el2;
56 uint64_t hpfar_el2;
57 uint64_t hstr_el2;
58 uint64_t icc_sre_el2;
59 uint64_t ich_hcr_el2;
60 uint64_t ich_vmcr_el2;
61 uint64_t mair_el2;
62 uint64_t mdcr_el2;
63 uint64_t pmscr_el2;
64 uint64_t sctlr_el2;
65 uint64_t spsr_el2;
66 uint64_t sp_el2;
67 uint64_t tcr_el2;
68 uint64_t tpidr_el2;
69 uint64_t ttbr0_el2;
70 uint64_t vbar_el2;
71 uint64_t vmpidr_el2;
72 uint64_t vpidr_el2;
73 uint64_t vtcr_el2;
74 uint64_t vttbr_el2;
75} el2_common_regs_t;
76
77typedef struct el2_mte2_regs {
78 uint64_t tfsr_el2;
79} el2_mte2_regs_t;
80
81typedef struct el2_fgt_regs {
82 uint64_t hdfgrtr_el2;
83 uint64_t hafgrtr_el2;
84 uint64_t hdfgwtr_el2;
85 uint64_t hfgitr_el2;
86 uint64_t hfgrtr_el2;
87 uint64_t hfgwtr_el2;
88} el2_fgt_regs_t;
89
90typedef struct el2_fgt2_regs {
91 uint64_t hdfgrtr2_el2;
92 uint64_t hdfgwtr2_el2;
93 uint64_t hfgitr2_el2;
94 uint64_t hfgrtr2_el2;
95 uint64_t hfgwtr2_el2;
96} el2_fgt2_regs_t;
97
98typedef struct el2_ecv_regs {
99 uint64_t cntpoff_el2;
100} el2_ecv_regs_t;
101
102typedef struct el2_vhe_regs {
103 uint64_t contextidr_el2;
104 uint64_t ttbr1_el2;
105} el2_vhe_regs_t;
106
107typedef struct el2_ras_regs {
108 uint64_t vdisr_el2;
109 uint64_t vsesr_el2;
110} el2_ras_regs_t;
111
112typedef struct el2_neve_regs {
113 uint64_t vncr_el2;
114} el2_neve_regs_t;
115
116typedef struct el2_trf_regs {
117 uint64_t trfcr_el2;
118} el2_trf_regs_t;
119
120typedef struct el2_csv2_regs {
121 uint64_t scxtnum_el2;
122} el2_csv2_regs_t;
123
124typedef struct el2_hcx_regs {
125 uint64_t hcrx_el2;
126} el2_hcx_regs_t;
127
128typedef struct el2_tcr2_regs {
129 uint64_t tcr2_el2;
130} el2_tcr2_regs_t;
131
132typedef struct el2_sxpoe_regs {
133 uint64_t por_el2;
134} el2_sxpoe_regs_t;
135
136typedef struct el2_sxpie_regs {
137 uint64_t pire0_el2;
138 uint64_t pir_el2;
139} el2_sxpie_regs_t;
140
141typedef struct el2_s2pie_regs {
142 uint64_t s2pir_el2;
143} el2_s2pie_regs_t;
144
145typedef struct el2_gcs_regs {
146 uint64_t gcscr_el2;
147 uint64_t gcspr_el2;
148} el2_gcs_regs_t;
149
150typedef struct el2_mpam_regs {
151 uint64_t mpam2_el2;
152 uint64_t mpamhcr_el2;
153 uint64_t mpamvpm0_el2;
154 uint64_t mpamvpm1_el2;
155 uint64_t mpamvpm2_el2;
156 uint64_t mpamvpm3_el2;
157 uint64_t mpamvpm4_el2;
158 uint64_t mpamvpm5_el2;
159 uint64_t mpamvpm6_el2;
160 uint64_t mpamvpm7_el2;
161 uint64_t mpamvpmv_el2;
162} el2_mpam_regs_t;
163
164typedef struct el2_sysregs {
165 el2_common_regs_t common;
166
167 el2_mte2_regs_t mte2;
168 el2_fgt_regs_t fgt;
169 el2_fgt2_regs_t fgt2;
170 el2_ecv_regs_t ecv;
171 el2_vhe_regs_t vhe;
172 el2_ras_regs_t ras;
173 el2_neve_regs_t neve;
174 el2_trf_regs_t trf;
175 el2_csv2_regs_t csv2;
176 el2_hcx_regs_t hcx;
177 el2_tcr2_regs_t tcr2;
178 el2_sxpoe_regs_t sxpoe;
179 el2_sxpie_regs_t sxpie;
180 el2_s2pie_regs_t s2pie;
181 el2_gcs_regs_t gcs;
182 el2_mpam_regs_t mpam;
183} el2_sysregs_t;
184
185/******************************************************************************/
186/**
187 * --------------------------
188 * EL2 context helper macros.
189 * --------------------------
190 */
191#define EL2_SAVE_CTX_REG(ctx, reg_member) \
192 ctx->reg_member = read_##reg_member()
193
194#define EL2_SAVE_CTX_SP(ctx) \
195 do { \
196 if (read_spsel() == 1) \
197 ctx->sp_el2 = read_sp(); \
198 } while (0)
199
200#define EL2_WRITE_MASK_CTX_REG(ctx, reg_member, mask) \
201 write_##reg_member(ctx->reg_member | (mask))
202
203#define EL2_PRINT_CTX_HEADING(heading) \
204 INFO("\t%s:\n", heading)
205
206#define EL2_PRINT_CTX_MEMBER(ctx, reg_name, reg_member) \
207 INFO("\t-- %s: 0x%llx\n", reg_name, ctx->reg_member)
208
209/**
210 * --------------------------------------
211 * EL2 context accessor public functions.
212 * --------------------------------------
213 */
214void el2_save_registers(el2_sysregs_t *ctx);
215void el2_modify_registers(const el2_sysregs_t *ctx, const el2_modify_operation_t op);
216void el2_dump_register_context(const char *ctx_name, const el2_sysregs_t *ctx);
217
218#endif /* CONTEXT_EL2_H */