xref: /openbmc/qemu/target/arm/cpregs.h (revision 93e0932b)
1 /*
2  * QEMU ARM CP Register access and descriptions
3  *
4  * Copyright (c) 2022 Linaro Ltd
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <http://www.gnu.org/licenses/gpl-2.0.html>
19  */
20 
21 #ifndef TARGET_ARM_CPREGS_H
22 #define TARGET_ARM_CPREGS_H
23 
24 /*
25  * ARMCPRegInfo type field bits:
26  */
27 enum {
28     /*
29      * Register must be handled specially during translation.
30      * The method is one of the values below:
31      */
32     ARM_CP_SPECIAL_MASK          = 0x000f,
33     /* Special: no change to PE state: writes ignored, reads ignored. */
34     ARM_CP_NOP                   = 0x0001,
35     /* Special: sysreg is WFI, for v5 and v6. */
36     ARM_CP_WFI                   = 0x0002,
37     /* Special: sysreg is NZCV. */
38     ARM_CP_NZCV                  = 0x0003,
39     /* Special: sysreg is CURRENTEL. */
40     ARM_CP_CURRENTEL             = 0x0004,
41     /* Special: sysreg is DC ZVA or similar. */
42     ARM_CP_DC_ZVA                = 0x0005,
43     ARM_CP_DC_GVA                = 0x0006,
44     ARM_CP_DC_GZVA               = 0x0007,
45 
46     /* Flag: reads produce resetvalue; writes ignored. */
47     ARM_CP_CONST                 = 1 << 4,
48     /* Flag: For ARM_CP_STATE_AA32, sysreg is 64-bit. */
49     ARM_CP_64BIT                 = 1 << 5,
50     /*
51      * Flag: TB should not be ended after a write to this register
52      * (the default is that the TB ends after cp writes).
53      */
54     ARM_CP_SUPPRESS_TB_END       = 1 << 6,
55     /*
56      * Flag: Permit a register definition to override a previous definition
57      * for the same (cp, is64, crn, crm, opc1, opc2) tuple: either the new
58      * or the old must have the ARM_CP_OVERRIDE bit set.
59      */
60     ARM_CP_OVERRIDE              = 1 << 7,
61     /*
62      * Flag: Register is an alias view of some underlying state which is also
63      * visible via another register, and that the other register is handling
64      * migration and reset; registers marked ARM_CP_ALIAS will not be migrated
65      * but may have their state set by syncing of register state from KVM.
66      */
67     ARM_CP_ALIAS                 = 1 << 8,
68     /*
69      * Flag: Register does I/O and therefore its accesses need to be marked
70      * with gen_io_start() and also end the TB. In particular, registers which
71      * implement clocks or timers require this.
72      */
73     ARM_CP_IO                    = 1 << 9,
74     /*
75      * Flag: Register has no underlying state and does not support raw access
76      * for state saving/loading; it will not be used for either migration or
77      * KVM state synchronization. Typically this is for "registers" which are
78      * actually used as instructions for cache maintenance and so on.
79      */
80     ARM_CP_NO_RAW                = 1 << 10,
81     /*
82      * Flag: The read or write hook might raise an exception; the generated
83      * code will synchronize the CPU state before calling the hook so that it
84      * is safe for the hook to call raise_exception().
85      */
86     ARM_CP_RAISES_EXC            = 1 << 11,
87     /*
88      * Flag: Writes to the sysreg might change the exception level - typically
89      * on older ARM chips. For those cases we need to re-read the new el when
90      * recomputing the translation flags.
91      */
92     ARM_CP_NEWEL                 = 1 << 12,
93     /*
94      * Flag: Access check for this sysreg is identical to accessing FPU state
95      * from an instruction: use translation fp_access_check().
96      */
97     ARM_CP_FPU                   = 1 << 13,
98     /*
99      * Flag: Access check for this sysreg is identical to accessing SVE state
100      * from an instruction: use translation sve_access_check().
101      */
102     ARM_CP_SVE                   = 1 << 14,
103     /* Flag: Do not expose in gdb sysreg xml. */
104     ARM_CP_NO_GDB                = 1 << 15,
105     /*
106      * Flags: If EL3 but not EL2...
107      *   - UNDEF: discard the cpreg,
108      *   -  KEEP: retain the cpreg as is,
109      *   -  C_NZ: set const on the cpreg, but retain resetvalue,
110      *   -  else: set const on the cpreg, zero resetvalue, aka RES0.
111      * See rule RJFFP in section D1.1.3 of DDI0487H.a.
112      */
113     ARM_CP_EL3_NO_EL2_UNDEF      = 1 << 16,
114     ARM_CP_EL3_NO_EL2_KEEP       = 1 << 17,
115     ARM_CP_EL3_NO_EL2_C_NZ       = 1 << 18,
116     /*
117      * Flag: Access check for this sysreg is constrained by the
118      * ARM pseudocode function CheckSMEAccess().
119      */
120     ARM_CP_SME                   = 1 << 19,
121 };
122 
123 /*
124  * Valid values for ARMCPRegInfo state field, indicating which of
125  * the AArch32 and AArch64 execution states this register is visible in.
126  * If the reginfo doesn't explicitly specify then it is AArch32 only.
127  * If the reginfo is declared to be visible in both states then a second
128  * reginfo is synthesised for the AArch32 view of the AArch64 register,
129  * such that the AArch32 view is the lower 32 bits of the AArch64 one.
130  * Note that we rely on the values of these enums as we iterate through
131  * the various states in some places.
132  */
133 typedef enum {
134     ARM_CP_STATE_AA32 = 0,
135     ARM_CP_STATE_AA64 = 1,
136     ARM_CP_STATE_BOTH = 2,
137 } CPState;
138 
139 /*
140  * ARM CP register secure state flags.  These flags identify security state
141  * attributes for a given CP register entry.
142  * The existence of both or neither secure and non-secure flags indicates that
143  * the register has both a secure and non-secure hash entry.  A single one of
144  * these flags causes the register to only be hashed for the specified
145  * security state.
146  * Although definitions may have any combination of the S/NS bits, each
147  * registered entry will only have one to identify whether the entry is secure
148  * or non-secure.
149  */
150 typedef enum {
151     ARM_CP_SECSTATE_BOTH = 0,       /* define one cpreg for each secstate */
152     ARM_CP_SECSTATE_S =   (1 << 0), /* bit[0]: Secure state register */
153     ARM_CP_SECSTATE_NS =  (1 << 1), /* bit[1]: Non-secure state register */
154 } CPSecureState;
155 
156 /*
157  * Access rights:
158  * We define bits for Read and Write access for what rev C of the v7-AR ARM ARM
159  * defines as PL0 (user), PL1 (fiq/irq/svc/abt/und/sys, ie privileged), and
160  * PL2 (hyp). The other level which has Read and Write bits is Secure PL1
161  * (ie any of the privileged modes in Secure state, or Monitor mode).
162  * If a register is accessible in one privilege level it's always accessible
163  * in higher privilege levels too. Since "Secure PL1" also follows this rule
164  * (ie anything visible in PL2 is visible in S-PL1, some things are only
165  * visible in S-PL1) but "Secure PL1" is a bit of a mouthful, we bend the
166  * terminology a little and call this PL3.
167  * In AArch64 things are somewhat simpler as the PLx bits line up exactly
168  * with the ELx exception levels.
169  *
170  * If access permissions for a register are more complex than can be
171  * described with these bits, then use a laxer set of restrictions, and
172  * do the more restrictive/complex check inside a helper function.
173  */
174 typedef enum {
175     PL3_R = 0x80,
176     PL3_W = 0x40,
177     PL2_R = 0x20 | PL3_R,
178     PL2_W = 0x10 | PL3_W,
179     PL1_R = 0x08 | PL2_R,
180     PL1_W = 0x04 | PL2_W,
181     PL0_R = 0x02 | PL1_R,
182     PL0_W = 0x01 | PL1_W,
183 
184     /*
185      * For user-mode some registers are accessible to EL0 via a kernel
186      * trap-and-emulate ABI. In this case we define the read permissions
187      * as actually being PL0_R. However some bits of any given register
188      * may still be masked.
189      */
190 #ifdef CONFIG_USER_ONLY
191     PL0U_R = PL0_R,
192 #else
193     PL0U_R = PL1_R,
194 #endif
195 
196     PL3_RW = PL3_R | PL3_W,
197     PL2_RW = PL2_R | PL2_W,
198     PL1_RW = PL1_R | PL1_W,
199     PL0_RW = PL0_R | PL0_W,
200 } CPAccessRights;
201 
202 typedef enum CPAccessResult {
203     /* Access is permitted */
204     CP_ACCESS_OK = 0,
205 
206     /*
207      * Combined with one of the following, the low 2 bits indicate the
208      * target exception level.  If 0, the exception is taken to the usual
209      * target EL (EL1 or PL1 if in EL0, otherwise to the current EL).
210      */
211     CP_ACCESS_EL_MASK = 3,
212 
213     /*
214      * Access fails due to a configurable trap or enable which would
215      * result in a categorized exception syndrome giving information about
216      * the failing instruction (ie syndrome category 0x3, 0x4, 0x5, 0x6,
217      * 0xc or 0x18).
218      */
219     CP_ACCESS_TRAP = (1 << 2),
220     CP_ACCESS_TRAP_EL2 = CP_ACCESS_TRAP | 2,
221     CP_ACCESS_TRAP_EL3 = CP_ACCESS_TRAP | 3,
222 
223     /*
224      * Access fails and results in an exception syndrome 0x0 ("uncategorized").
225      * Note that this is not a catch-all case -- the set of cases which may
226      * result in this failure is specifically defined by the architecture.
227      * This trap is always to the usual target EL, never directly to a
228      * specified target EL.
229      */
230     CP_ACCESS_TRAP_UNCATEGORIZED = (2 << 2),
231 } CPAccessResult;
232 
233 /* Indexes into fgt_read[] */
234 #define FGTREG_HFGRTR 0
235 #define FGTREG_HDFGRTR 1
236 /* Indexes into fgt_write[] */
237 #define FGTREG_HFGWTR 0
238 #define FGTREG_HDFGWTR 1
239 /* Indexes into fgt_exec[] */
240 #define FGTREG_HFGITR 0
241 
242 FIELD(HFGRTR_EL2, AFSR0_EL1, 0, 1)
243 FIELD(HFGRTR_EL2, AFSR1_EL1, 1, 1)
244 FIELD(HFGRTR_EL2, AIDR_EL1, 2, 1)
245 FIELD(HFGRTR_EL2, AMAIR_EL1, 3, 1)
246 FIELD(HFGRTR_EL2, APDAKEY, 4, 1)
247 FIELD(HFGRTR_EL2, APDBKEY, 5, 1)
248 FIELD(HFGRTR_EL2, APGAKEY, 6, 1)
249 FIELD(HFGRTR_EL2, APIAKEY, 7, 1)
250 FIELD(HFGRTR_EL2, APIBKEY, 8, 1)
251 FIELD(HFGRTR_EL2, CCSIDR_EL1, 9, 1)
252 FIELD(HFGRTR_EL2, CLIDR_EL1, 10, 1)
253 FIELD(HFGRTR_EL2, CONTEXTIDR_EL1, 11, 1)
254 FIELD(HFGRTR_EL2, CPACR_EL1, 12, 1)
255 FIELD(HFGRTR_EL2, CSSELR_EL1, 13, 1)
256 FIELD(HFGRTR_EL2, CTR_EL0, 14, 1)
257 FIELD(HFGRTR_EL2, DCZID_EL0, 15, 1)
258 FIELD(HFGRTR_EL2, ESR_EL1, 16, 1)
259 FIELD(HFGRTR_EL2, FAR_EL1, 17, 1)
260 FIELD(HFGRTR_EL2, ISR_EL1, 18, 1)
261 FIELD(HFGRTR_EL2, LORC_EL1, 19, 1)
262 FIELD(HFGRTR_EL2, LOREA_EL1, 20, 1)
263 FIELD(HFGRTR_EL2, LORID_EL1, 21, 1)
264 FIELD(HFGRTR_EL2, LORN_EL1, 22, 1)
265 FIELD(HFGRTR_EL2, LORSA_EL1, 23, 1)
266 FIELD(HFGRTR_EL2, MAIR_EL1, 24, 1)
267 FIELD(HFGRTR_EL2, MIDR_EL1, 25, 1)
268 FIELD(HFGRTR_EL2, MPIDR_EL1, 26, 1)
269 FIELD(HFGRTR_EL2, PAR_EL1, 27, 1)
270 FIELD(HFGRTR_EL2, REVIDR_EL1, 28, 1)
271 FIELD(HFGRTR_EL2, SCTLR_EL1, 29, 1)
272 FIELD(HFGRTR_EL2, SCXTNUM_EL1, 30, 1)
273 FIELD(HFGRTR_EL2, SCXTNUM_EL0, 31, 1)
274 FIELD(HFGRTR_EL2, TCR_EL1, 32, 1)
275 FIELD(HFGRTR_EL2, TPIDR_EL1, 33, 1)
276 FIELD(HFGRTR_EL2, TPIDRRO_EL0, 34, 1)
277 FIELD(HFGRTR_EL2, TPIDR_EL0, 35, 1)
278 FIELD(HFGRTR_EL2, TTBR0_EL1, 36, 1)
279 FIELD(HFGRTR_EL2, TTBR1_EL1, 37, 1)
280 FIELD(HFGRTR_EL2, VBAR_EL1, 38, 1)
281 FIELD(HFGRTR_EL2, ICC_IGRPENN_EL1, 39, 1)
282 FIELD(HFGRTR_EL2, ERRIDR_EL1, 40, 1)
283 FIELD(HFGRTR_EL2, ERRSELR_EL1, 41, 1)
284 FIELD(HFGRTR_EL2, ERXFR_EL1, 42, 1)
285 FIELD(HFGRTR_EL2, ERXCTLR_EL1, 43, 1)
286 FIELD(HFGRTR_EL2, ERXSTATUS_EL1, 44, 1)
287 FIELD(HFGRTR_EL2, ERXMISCN_EL1, 45, 1)
288 FIELD(HFGRTR_EL2, ERXPFGF_EL1, 46, 1)
289 FIELD(HFGRTR_EL2, ERXPFGCTL_EL1, 47, 1)
290 FIELD(HFGRTR_EL2, ERXPFGCDN_EL1, 48, 1)
291 FIELD(HFGRTR_EL2, ERXADDR_EL1, 49, 1)
292 FIELD(HFGRTR_EL2, NACCDATA_EL1, 50, 1)
293 /* 51-53: RES0 */
294 FIELD(HFGRTR_EL2, NSMPRI_EL1, 54, 1)
295 FIELD(HFGRTR_EL2, NTPIDR2_EL0, 55, 1)
296 /* 56-63: RES0 */
297 
298 /* These match HFGRTR but bits for RO registers are RES0 */
299 FIELD(HFGWTR_EL2, AFSR0_EL1, 0, 1)
300 FIELD(HFGWTR_EL2, AFSR1_EL1, 1, 1)
301 FIELD(HFGWTR_EL2, AMAIR_EL1, 3, 1)
302 FIELD(HFGWTR_EL2, APDAKEY, 4, 1)
303 FIELD(HFGWTR_EL2, APDBKEY, 5, 1)
304 FIELD(HFGWTR_EL2, APGAKEY, 6, 1)
305 FIELD(HFGWTR_EL2, APIAKEY, 7, 1)
306 FIELD(HFGWTR_EL2, APIBKEY, 8, 1)
307 FIELD(HFGWTR_EL2, CONTEXTIDR_EL1, 11, 1)
308 FIELD(HFGWTR_EL2, CPACR_EL1, 12, 1)
309 FIELD(HFGWTR_EL2, CSSELR_EL1, 13, 1)
310 FIELD(HFGWTR_EL2, ESR_EL1, 16, 1)
311 FIELD(HFGWTR_EL2, FAR_EL1, 17, 1)
312 FIELD(HFGWTR_EL2, LORC_EL1, 19, 1)
313 FIELD(HFGWTR_EL2, LOREA_EL1, 20, 1)
314 FIELD(HFGWTR_EL2, LORN_EL1, 22, 1)
315 FIELD(HFGWTR_EL2, LORSA_EL1, 23, 1)
316 FIELD(HFGWTR_EL2, MAIR_EL1, 24, 1)
317 FIELD(HFGWTR_EL2, PAR_EL1, 27, 1)
318 FIELD(HFGWTR_EL2, SCTLR_EL1, 29, 1)
319 FIELD(HFGWTR_EL2, SCXTNUM_EL1, 30, 1)
320 FIELD(HFGWTR_EL2, SCXTNUM_EL0, 31, 1)
321 FIELD(HFGWTR_EL2, TCR_EL1, 32, 1)
322 FIELD(HFGWTR_EL2, TPIDR_EL1, 33, 1)
323 FIELD(HFGWTR_EL2, TPIDRRO_EL0, 34, 1)
324 FIELD(HFGWTR_EL2, TPIDR_EL0, 35, 1)
325 FIELD(HFGWTR_EL2, TTBR0_EL1, 36, 1)
326 FIELD(HFGWTR_EL2, TTBR1_EL1, 37, 1)
327 FIELD(HFGWTR_EL2, VBAR_EL1, 38, 1)
328 FIELD(HFGWTR_EL2, ICC_IGRPENN_EL1, 39, 1)
329 FIELD(HFGWTR_EL2, ERRSELR_EL1, 41, 1)
330 FIELD(HFGWTR_EL2, ERXCTLR_EL1, 43, 1)
331 FIELD(HFGWTR_EL2, ERXSTATUS_EL1, 44, 1)
332 FIELD(HFGWTR_EL2, ERXMISCN_EL1, 45, 1)
333 FIELD(HFGWTR_EL2, ERXPFGCTL_EL1, 47, 1)
334 FIELD(HFGWTR_EL2, ERXPFGCDN_EL1, 48, 1)
335 FIELD(HFGWTR_EL2, ERXADDR_EL1, 49, 1)
336 FIELD(HFGWTR_EL2, NACCDATA_EL1, 50, 1)
337 FIELD(HFGWTR_EL2, NSMPRI_EL1, 54, 1)
338 FIELD(HFGWTR_EL2, NTPIDR2_EL0, 55, 1)
339 
340 FIELD(HFGITR_EL2, ICIALLUIS, 0, 1)
341 FIELD(HFGITR_EL2, ICIALLU, 1, 1)
342 FIELD(HFGITR_EL2, ICIVAU, 2, 1)
343 FIELD(HFGITR_EL2, DCIVAC, 3, 1)
344 FIELD(HFGITR_EL2, DCISW, 4, 1)
345 FIELD(HFGITR_EL2, DCCSW, 5, 1)
346 FIELD(HFGITR_EL2, DCCISW, 6, 1)
347 FIELD(HFGITR_EL2, DCCVAU, 7, 1)
348 FIELD(HFGITR_EL2, DCCVAP, 8, 1)
349 FIELD(HFGITR_EL2, DCCVADP, 9, 1)
350 FIELD(HFGITR_EL2, DCCIVAC, 10, 1)
351 FIELD(HFGITR_EL2, DCZVA, 11, 1)
352 FIELD(HFGITR_EL2, ATS1E1R, 12, 1)
353 FIELD(HFGITR_EL2, ATS1E1W, 13, 1)
354 FIELD(HFGITR_EL2, ATS1E0R, 14, 1)
355 FIELD(HFGITR_EL2, ATS1E0W, 15, 1)
356 FIELD(HFGITR_EL2, ATS1E1RP, 16, 1)
357 FIELD(HFGITR_EL2, ATS1E1WP, 17, 1)
358 FIELD(HFGITR_EL2, TLBIVMALLE1OS, 18, 1)
359 FIELD(HFGITR_EL2, TLBIVAE1OS, 19, 1)
360 FIELD(HFGITR_EL2, TLBIASIDE1OS, 20, 1)
361 FIELD(HFGITR_EL2, TLBIVAAE1OS, 21, 1)
362 FIELD(HFGITR_EL2, TLBIVALE1OS, 22, 1)
363 FIELD(HFGITR_EL2, TLBIVAALE1OS, 23, 1)
364 FIELD(HFGITR_EL2, TLBIRVAE1OS, 24, 1)
365 FIELD(HFGITR_EL2, TLBIRVAAE1OS, 25, 1)
366 FIELD(HFGITR_EL2, TLBIRVALE1OS, 26, 1)
367 FIELD(HFGITR_EL2, TLBIRVAALE1OS, 27, 1)
368 FIELD(HFGITR_EL2, TLBIVMALLE1IS, 28, 1)
369 FIELD(HFGITR_EL2, TLBIVAE1IS, 29, 1)
370 FIELD(HFGITR_EL2, TLBIASIDE1IS, 30, 1)
371 FIELD(HFGITR_EL2, TLBIVAAE1IS, 31, 1)
372 FIELD(HFGITR_EL2, TLBIVALE1IS, 32, 1)
373 FIELD(HFGITR_EL2, TLBIVAALE1IS, 33, 1)
374 FIELD(HFGITR_EL2, TLBIRVAE1IS, 34, 1)
375 FIELD(HFGITR_EL2, TLBIRVAAE1IS, 35, 1)
376 FIELD(HFGITR_EL2, TLBIRVALE1IS, 36, 1)
377 FIELD(HFGITR_EL2, TLBIRVAALE1IS, 37, 1)
378 FIELD(HFGITR_EL2, TLBIRVAE1, 38, 1)
379 FIELD(HFGITR_EL2, TLBIRVAAE1, 39, 1)
380 FIELD(HFGITR_EL2, TLBIRVALE1, 40, 1)
381 FIELD(HFGITR_EL2, TLBIRVAALE1, 41, 1)
382 FIELD(HFGITR_EL2, TLBIVMALLE1, 42, 1)
383 FIELD(HFGITR_EL2, TLBIVAE1, 43, 1)
384 FIELD(HFGITR_EL2, TLBIASIDE1, 44, 1)
385 FIELD(HFGITR_EL2, TLBIVAAE1, 45, 1)
386 FIELD(HFGITR_EL2, TLBIVALE1, 46, 1)
387 FIELD(HFGITR_EL2, TLBIVAALE1, 47, 1)
388 FIELD(HFGITR_EL2, CFPRCTX, 48, 1)
389 FIELD(HFGITR_EL2, DVPRCTX, 49, 1)
390 FIELD(HFGITR_EL2, CPPRCTX, 50, 1)
391 FIELD(HFGITR_EL2, ERET, 51, 1)
392 FIELD(HFGITR_EL2, SVC_EL0, 52, 1)
393 FIELD(HFGITR_EL2, SVC_EL1, 53, 1)
394 FIELD(HFGITR_EL2, DCCVAC, 54, 1)
395 FIELD(HFGITR_EL2, NBRBINJ, 55, 1)
396 FIELD(HFGITR_EL2, NBRBIALL, 56, 1)
397 
398 FIELD(HDFGRTR_EL2, DBGBCRN_EL1, 0, 1)
399 FIELD(HDFGRTR_EL2, DBGBVRN_EL1, 1, 1)
400 FIELD(HDFGRTR_EL2, DBGWCRN_EL1, 2, 1)
401 FIELD(HDFGRTR_EL2, DBGWVRN_EL1, 3, 1)
402 FIELD(HDFGRTR_EL2, MDSCR_EL1, 4, 1)
403 FIELD(HDFGRTR_EL2, DBGCLAIM, 5, 1)
404 FIELD(HDFGRTR_EL2, DBGAUTHSTATUS_EL1, 6, 1)
405 FIELD(HDFGRTR_EL2, DBGPRCR_EL1, 7, 1)
406 /* 8: RES0: OSLAR_EL1 is WO */
407 FIELD(HDFGRTR_EL2, OSLSR_EL1, 9, 1)
408 FIELD(HDFGRTR_EL2, OSECCR_EL1, 10, 1)
409 FIELD(HDFGRTR_EL2, OSDLR_EL1, 11, 1)
410 FIELD(HDFGRTR_EL2, PMEVCNTRN_EL0, 12, 1)
411 FIELD(HDFGRTR_EL2, PMEVTYPERN_EL0, 13, 1)
412 FIELD(HDFGRTR_EL2, PMCCFILTR_EL0, 14, 1)
413 FIELD(HDFGRTR_EL2, PMCCNTR_EL0, 15, 1)
414 FIELD(HDFGRTR_EL2, PMCNTEN, 16, 1)
415 FIELD(HDFGRTR_EL2, PMINTEN, 17, 1)
416 FIELD(HDFGRTR_EL2, PMOVS, 18, 1)
417 FIELD(HDFGRTR_EL2, PMSELR_EL0, 19, 1)
418 /* 20: RES0: PMSWINC_EL0 is WO */
419 /* 21: RES0: PMCR_EL0 is WO */
420 FIELD(HDFGRTR_EL2, PMMIR_EL1, 22, 1)
421 FIELD(HDFGRTR_EL2, PMBLIMITR_EL1, 23, 1)
422 FIELD(HDFGRTR_EL2, PMBPTR_EL1, 24, 1)
423 FIELD(HDFGRTR_EL2, PMBSR_EL1, 25, 1)
424 FIELD(HDFGRTR_EL2, PMSCR_EL1, 26, 1)
425 FIELD(HDFGRTR_EL2, PMSEVFR_EL1, 27, 1)
426 FIELD(HDFGRTR_EL2, PMSFCR_EL1, 28, 1)
427 FIELD(HDFGRTR_EL2, PMSICR_EL1, 29, 1)
428 FIELD(HDFGRTR_EL2, PMSIDR_EL1, 30, 1)
429 FIELD(HDFGRTR_EL2, PMSIRR_EL1, 31, 1)
430 FIELD(HDFGRTR_EL2, PMSLATFR_EL1, 32, 1)
431 FIELD(HDFGRTR_EL2, TRC, 33, 1)
432 FIELD(HDFGRTR_EL2, TRCAUTHSTATUS, 34, 1)
433 FIELD(HDFGRTR_EL2, TRCAUXCTLR, 35, 1)
434 FIELD(HDFGRTR_EL2, TRCCLAIM, 36, 1)
435 FIELD(HDFGRTR_EL2, TRCCNTVRn, 37, 1)
436 /* 38, 39: RES0 */
437 FIELD(HDFGRTR_EL2, TRCID, 40, 1)
438 FIELD(HDFGRTR_EL2, TRCIMSPECN, 41, 1)
439 /* 42: RES0: TRCOSLAR is WO */
440 FIELD(HDFGRTR_EL2, TRCOSLSR, 43, 1)
441 FIELD(HDFGRTR_EL2, TRCPRGCTLR, 44, 1)
442 FIELD(HDFGRTR_EL2, TRCSEQSTR, 45, 1)
443 FIELD(HDFGRTR_EL2, TRCSSCSRN, 46, 1)
444 FIELD(HDFGRTR_EL2, TRCSTATR, 47, 1)
445 FIELD(HDFGRTR_EL2, TRCVICTLR, 48, 1)
446 /* 49: RES0: TRFCR_EL1 is WO */
447 FIELD(HDFGRTR_EL2, TRBBASER_EL1, 50, 1)
448 FIELD(HDFGRTR_EL2, TRBIDR_EL1, 51, 1)
449 FIELD(HDFGRTR_EL2, TRBLIMITR_EL1, 52, 1)
450 FIELD(HDFGRTR_EL2, TRBMAR_EL1, 53, 1)
451 FIELD(HDFGRTR_EL2, TRBPTR_EL1, 54, 1)
452 FIELD(HDFGRTR_EL2, TRBSR_EL1, 55, 1)
453 FIELD(HDFGRTR_EL2, TRBTRG_EL1, 56, 1)
454 FIELD(HDFGRTR_EL2, PMUSERENR_EL0, 57, 1)
455 FIELD(HDFGRTR_EL2, PMCEIDN_EL0, 58, 1)
456 FIELD(HDFGRTR_EL2, NBRBIDR, 59, 1)
457 FIELD(HDFGRTR_EL2, NBRBCTL, 60, 1)
458 FIELD(HDFGRTR_EL2, NBRBDATA, 61, 1)
459 FIELD(HDFGRTR_EL2, NPMSNEVFR_EL1, 62, 1)
460 FIELD(HDFGRTR_EL2, PMBIDR_EL1, 63, 1)
461 
462 /*
463  * These match HDFGRTR_EL2, but bits for RO registers are RES0.
464  * A few bits are for WO registers, where the HDFGRTR_EL2 bit is RES0.
465  */
466 FIELD(HDFGWTR_EL2, DBGBCRN_EL1, 0, 1)
467 FIELD(HDFGWTR_EL2, DBGBVRN_EL1, 1, 1)
468 FIELD(HDFGWTR_EL2, DBGWCRN_EL1, 2, 1)
469 FIELD(HDFGWTR_EL2, DBGWVRN_EL1, 3, 1)
470 FIELD(HDFGWTR_EL2, MDSCR_EL1, 4, 1)
471 FIELD(HDFGWTR_EL2, DBGCLAIM, 5, 1)
472 FIELD(HDFGWTR_EL2, DBGPRCR_EL1, 7, 1)
473 FIELD(HDFGWTR_EL2, OSLAR_EL1, 8, 1)
474 FIELD(HDFGWTR_EL2, OSLSR_EL1, 9, 1)
475 FIELD(HDFGWTR_EL2, OSECCR_EL1, 10, 1)
476 FIELD(HDFGWTR_EL2, OSDLR_EL1, 11, 1)
477 FIELD(HDFGWTR_EL2, PMEVCNTRN_EL0, 12, 1)
478 FIELD(HDFGWTR_EL2, PMEVTYPERN_EL0, 13, 1)
479 FIELD(HDFGWTR_EL2, PMCCFILTR_EL0, 14, 1)
480 FIELD(HDFGWTR_EL2, PMCCNTR_EL0, 15, 1)
481 FIELD(HDFGWTR_EL2, PMCNTEN, 16, 1)
482 FIELD(HDFGWTR_EL2, PMINTEN, 17, 1)
483 FIELD(HDFGWTR_EL2, PMOVS, 18, 1)
484 FIELD(HDFGWTR_EL2, PMSELR_EL0, 19, 1)
485 FIELD(HDFGWTR_EL2, PMSWINC_EL0, 20, 1)
486 FIELD(HDFGWTR_EL2, PMCR_EL0, 21, 1)
487 FIELD(HDFGWTR_EL2, PMBLIMITR_EL1, 23, 1)
488 FIELD(HDFGWTR_EL2, PMBPTR_EL1, 24, 1)
489 FIELD(HDFGWTR_EL2, PMBSR_EL1, 25, 1)
490 FIELD(HDFGWTR_EL2, PMSCR_EL1, 26, 1)
491 FIELD(HDFGWTR_EL2, PMSEVFR_EL1, 27, 1)
492 FIELD(HDFGWTR_EL2, PMSFCR_EL1, 28, 1)
493 FIELD(HDFGWTR_EL2, PMSICR_EL1, 29, 1)
494 FIELD(HDFGWTR_EL2, PMSIRR_EL1, 31, 1)
495 FIELD(HDFGWTR_EL2, PMSLATFR_EL1, 32, 1)
496 FIELD(HDFGWTR_EL2, TRC, 33, 1)
497 FIELD(HDFGWTR_EL2, TRCAUXCTLR, 35, 1)
498 FIELD(HDFGWTR_EL2, TRCCLAIM, 36, 1)
499 FIELD(HDFGWTR_EL2, TRCCNTVRn, 37, 1)
500 FIELD(HDFGWTR_EL2, TRCIMSPECN, 41, 1)
501 FIELD(HDFGWTR_EL2, TRCOSLAR, 42, 1)
502 FIELD(HDFGWTR_EL2, TRCPRGCTLR, 44, 1)
503 FIELD(HDFGWTR_EL2, TRCSEQSTR, 45, 1)
504 FIELD(HDFGWTR_EL2, TRCSSCSRN, 46, 1)
505 FIELD(HDFGWTR_EL2, TRCVICTLR, 48, 1)
506 FIELD(HDFGWTR_EL2, TRFCR_EL1, 49, 1)
507 FIELD(HDFGWTR_EL2, TRBBASER_EL1, 50, 1)
508 FIELD(HDFGWTR_EL2, TRBLIMITR_EL1, 52, 1)
509 FIELD(HDFGWTR_EL2, TRBMAR_EL1, 53, 1)
510 FIELD(HDFGWTR_EL2, TRBPTR_EL1, 54, 1)
511 FIELD(HDFGWTR_EL2, TRBSR_EL1, 55, 1)
512 FIELD(HDFGWTR_EL2, TRBTRG_EL1, 56, 1)
513 FIELD(HDFGWTR_EL2, PMUSERENR_EL0, 57, 1)
514 FIELD(HDFGWTR_EL2, NBRBCTL, 60, 1)
515 FIELD(HDFGWTR_EL2, NBRBDATA, 61, 1)
516 FIELD(HDFGWTR_EL2, NPMSNEVFR_EL1, 62, 1)
517 
518 /* Which fine-grained trap bit register to check, if any */
519 FIELD(FGT, TYPE, 10, 3)
520 FIELD(FGT, REV, 9, 1) /* Is bit sense reversed? */
521 FIELD(FGT, IDX, 6, 3) /* Index within a uint64_t[] array */
522 FIELD(FGT, BITPOS, 0, 6) /* Bit position within the uint64_t */
523 
524 /*
525  * Macros to define FGT_##bitname enum constants to use in ARMCPRegInfo::fgt
526  * fields. We assume for brevity's sake that there are no duplicated
527  * bit names across the various FGT registers.
528  */
529 #define DO_BIT(REG, BITNAME)                                    \
530     FGT_##BITNAME = FGT_##REG | R_##REG##_EL2_##BITNAME##_SHIFT
531 
532 /* Some bits have reversed sense, so 0 means trap and 1 means not */
533 #define DO_REV_BIT(REG, BITNAME)                                        \
534     FGT_##BITNAME = FGT_##REG | FGT_REV | R_##REG##_EL2_##BITNAME##_SHIFT
535 
536 typedef enum FGTBit {
537     /*
538      * These bits tell us which register arrays to use:
539      * if FGT_R is set then reads are checked against fgt_read[];
540      * if FGT_W is set then writes are checked against fgt_write[];
541      * if FGT_EXEC is set then all accesses are checked against fgt_exec[].
542      *
543      * For almost all bits in the R/W register pairs, the bit exists in
544      * both registers for a RW register, in HFGRTR/HDFGRTR for a RO register
545      * with the corresponding HFGWTR/HDFGTWTR bit being RES0, and vice-versa
546      * for a WO register. There are unfortunately a couple of exceptions
547      * (PMCR_EL0, TRFCR_EL1) where the register being trapped is RW but
548      * the FGT system only allows trapping of writes, not reads.
549      *
550      * Note that we arrange these bits so that a 0 FGTBit means "no trap".
551      */
552     FGT_R = 1 << R_FGT_TYPE_SHIFT,
553     FGT_W = 2 << R_FGT_TYPE_SHIFT,
554     FGT_EXEC = 4 << R_FGT_TYPE_SHIFT,
555     FGT_RW = FGT_R | FGT_W,
556     /* Bit to identify whether trap bit is reversed sense */
557     FGT_REV = R_FGT_REV_MASK,
558 
559     /*
560      * If a bit exists in HFGRTR/HDFGRTR then either the register being
561      * trapped is RO or the bit also exists in HFGWTR/HDFGWTR, so we either
562      * want to trap for both reads and writes or else it's harmless to mark
563      * it as trap-on-writes.
564      * If a bit exists only in HFGWTR/HDFGWTR then either the register being
565      * trapped is WO, or else it is one of the two oddball special cases
566      * which are RW but have only a write trap. We mark these as only
567      * FGT_W so we get the right behaviour for those special cases.
568      * (If a bit was added in future that provided only a read trap for an
569      * RW register we'd need to do something special to get the FGT_R bit
570      * only. But this seems unlikely to happen.)
571      *
572      * So for the DO_BIT/DO_REV_BIT macros: use FGT_HFGRTR/FGT_HDFGRTR if
573      * the bit exists in that register. Otherwise use FGT_HFGWTR/FGT_HDFGWTR.
574      */
575     FGT_HFGRTR = FGT_RW | (FGTREG_HFGRTR << R_FGT_IDX_SHIFT),
576     FGT_HFGWTR = FGT_W | (FGTREG_HFGWTR << R_FGT_IDX_SHIFT),
577     FGT_HDFGRTR = FGT_RW | (FGTREG_HDFGRTR << R_FGT_IDX_SHIFT),
578     FGT_HDFGWTR = FGT_W | (FGTREG_HDFGWTR << R_FGT_IDX_SHIFT),
579     FGT_HFGITR = FGT_EXEC | (FGTREG_HFGITR << R_FGT_IDX_SHIFT),
580 
581     /* Trap bits in HFGRTR_EL2 / HFGWTR_EL2, starting from bit 0. */
582     DO_BIT(HFGRTR, AFSR0_EL1),
583     DO_BIT(HFGRTR, AFSR1_EL1),
584     DO_BIT(HFGRTR, AIDR_EL1),
585     DO_BIT(HFGRTR, AMAIR_EL1),
586     DO_BIT(HFGRTR, APDAKEY),
587     DO_BIT(HFGRTR, APDBKEY),
588     DO_BIT(HFGRTR, APGAKEY),
589     DO_BIT(HFGRTR, APIAKEY),
590     DO_BIT(HFGRTR, APIBKEY),
591     DO_BIT(HFGRTR, CCSIDR_EL1),
592     DO_BIT(HFGRTR, CLIDR_EL1),
593     DO_BIT(HFGRTR, CONTEXTIDR_EL1),
594     DO_BIT(HFGRTR, CPACR_EL1),
595     DO_BIT(HFGRTR, CSSELR_EL1),
596     DO_BIT(HFGRTR, CTR_EL0),
597     DO_BIT(HFGRTR, DCZID_EL0),
598     DO_BIT(HFGRTR, ESR_EL1),
599     DO_BIT(HFGRTR, FAR_EL1),
600     DO_BIT(HFGRTR, ISR_EL1),
601     DO_BIT(HFGRTR, LORC_EL1),
602     DO_BIT(HFGRTR, LOREA_EL1),
603     DO_BIT(HFGRTR, LORID_EL1),
604     DO_BIT(HFGRTR, LORN_EL1),
605     DO_BIT(HFGRTR, LORSA_EL1),
606     DO_BIT(HFGRTR, MAIR_EL1),
607     DO_BIT(HFGRTR, MIDR_EL1),
608     DO_BIT(HFGRTR, MPIDR_EL1),
609     DO_BIT(HFGRTR, PAR_EL1),
610     DO_BIT(HFGRTR, REVIDR_EL1),
611     DO_BIT(HFGRTR, SCTLR_EL1),
612     DO_BIT(HFGRTR, SCXTNUM_EL1),
613     DO_BIT(HFGRTR, SCXTNUM_EL0),
614     DO_BIT(HFGRTR, TCR_EL1),
615     DO_BIT(HFGRTR, TPIDR_EL1),
616     DO_BIT(HFGRTR, TPIDRRO_EL0),
617     DO_BIT(HFGRTR, TPIDR_EL0),
618     DO_BIT(HFGRTR, TTBR0_EL1),
619     DO_BIT(HFGRTR, TTBR1_EL1),
620     DO_BIT(HFGRTR, VBAR_EL1),
621     DO_BIT(HFGRTR, ICC_IGRPENN_EL1),
622     DO_BIT(HFGRTR, ERRIDR_EL1),
623     DO_REV_BIT(HFGRTR, NSMPRI_EL1),
624     DO_REV_BIT(HFGRTR, NTPIDR2_EL0),
625 
626     /* Trap bits in HDFGRTR_EL2 / HDFGWTR_EL2, starting from bit 0. */
627     DO_BIT(HDFGRTR, DBGBCRN_EL1),
628     DO_BIT(HDFGRTR, DBGBVRN_EL1),
629     DO_BIT(HDFGRTR, DBGWCRN_EL1),
630     DO_BIT(HDFGRTR, DBGWVRN_EL1),
631     DO_BIT(HDFGRTR, MDSCR_EL1),
632     DO_BIT(HDFGRTR, DBGCLAIM),
633     DO_BIT(HDFGWTR, OSLAR_EL1),
634     DO_BIT(HDFGRTR, OSLSR_EL1),
635     DO_BIT(HDFGRTR, OSECCR_EL1),
636     DO_BIT(HDFGRTR, OSDLR_EL1),
637     DO_BIT(HDFGRTR, PMEVCNTRN_EL0),
638     DO_BIT(HDFGRTR, PMEVTYPERN_EL0),
639     DO_BIT(HDFGRTR, PMCCFILTR_EL0),
640     DO_BIT(HDFGRTR, PMCCNTR_EL0),
641     DO_BIT(HDFGRTR, PMCNTEN),
642     DO_BIT(HDFGRTR, PMINTEN),
643     DO_BIT(HDFGRTR, PMOVS),
644     DO_BIT(HDFGRTR, PMSELR_EL0),
645     DO_BIT(HDFGWTR, PMSWINC_EL0),
646     DO_BIT(HDFGWTR, PMCR_EL0),
647     DO_BIT(HDFGRTR, PMMIR_EL1),
648     DO_BIT(HDFGRTR, PMCEIDN_EL0),
649 
650     /* Trap bits in HFGITR_EL2, starting from bit 0 */
651     DO_BIT(HFGITR, ICIALLUIS),
652     DO_BIT(HFGITR, ICIALLU),
653     DO_BIT(HFGITR, ICIVAU),
654     DO_BIT(HFGITR, DCIVAC),
655     DO_BIT(HFGITR, DCISW),
656     DO_BIT(HFGITR, DCCSW),
657     DO_BIT(HFGITR, DCCISW),
658     DO_BIT(HFGITR, DCCVAU),
659     DO_BIT(HFGITR, DCCVAP),
660     DO_BIT(HFGITR, DCCVADP),
661     DO_BIT(HFGITR, DCCIVAC),
662     DO_BIT(HFGITR, DCZVA),
663     DO_BIT(HFGITR, ATS1E1R),
664     DO_BIT(HFGITR, ATS1E1W),
665     DO_BIT(HFGITR, ATS1E0R),
666     DO_BIT(HFGITR, ATS1E0W),
667     DO_BIT(HFGITR, ATS1E1RP),
668     DO_BIT(HFGITR, ATS1E1WP),
669     DO_BIT(HFGITR, TLBIVMALLE1OS),
670     DO_BIT(HFGITR, TLBIVAE1OS),
671     DO_BIT(HFGITR, TLBIASIDE1OS),
672     DO_BIT(HFGITR, TLBIVAAE1OS),
673     DO_BIT(HFGITR, TLBIVALE1OS),
674     DO_BIT(HFGITR, TLBIVAALE1OS),
675     DO_BIT(HFGITR, TLBIRVAE1OS),
676     DO_BIT(HFGITR, TLBIRVAAE1OS),
677     DO_BIT(HFGITR, TLBIRVALE1OS),
678     DO_BIT(HFGITR, TLBIRVAALE1OS),
679     DO_BIT(HFGITR, TLBIVMALLE1IS),
680     DO_BIT(HFGITR, TLBIVAE1IS),
681     DO_BIT(HFGITR, TLBIASIDE1IS),
682     DO_BIT(HFGITR, TLBIVAAE1IS),
683     DO_BIT(HFGITR, TLBIVALE1IS),
684     DO_BIT(HFGITR, TLBIVAALE1IS),
685     DO_BIT(HFGITR, TLBIRVAE1IS),
686     DO_BIT(HFGITR, TLBIRVAAE1IS),
687     DO_BIT(HFGITR, TLBIRVALE1IS),
688     DO_BIT(HFGITR, TLBIRVAALE1IS),
689     DO_BIT(HFGITR, TLBIRVAE1),
690     DO_BIT(HFGITR, TLBIRVAAE1),
691     DO_BIT(HFGITR, TLBIRVALE1),
692     DO_BIT(HFGITR, TLBIRVAALE1),
693     DO_BIT(HFGITR, TLBIVMALLE1),
694     DO_BIT(HFGITR, TLBIVAE1),
695     DO_BIT(HFGITR, TLBIASIDE1),
696     DO_BIT(HFGITR, TLBIVAAE1),
697     DO_BIT(HFGITR, TLBIVALE1),
698     DO_BIT(HFGITR, TLBIVAALE1),
699     DO_BIT(HFGITR, CFPRCTX),
700     DO_BIT(HFGITR, DVPRCTX),
701     DO_BIT(HFGITR, CPPRCTX),
702     DO_BIT(HFGITR, DCCVAC),
703 } FGTBit;
704 
705 #undef DO_BIT
706 #undef DO_REV_BIT
707 
708 typedef struct ARMCPRegInfo ARMCPRegInfo;
709 
710 /*
711  * Access functions for coprocessor registers. These cannot fail and
712  * may not raise exceptions.
713  */
714 typedef uint64_t CPReadFn(CPUARMState *env, const ARMCPRegInfo *opaque);
715 typedef void CPWriteFn(CPUARMState *env, const ARMCPRegInfo *opaque,
716                        uint64_t value);
717 /* Access permission check functions for coprocessor registers. */
718 typedef CPAccessResult CPAccessFn(CPUARMState *env,
719                                   const ARMCPRegInfo *opaque,
720                                   bool isread);
721 /* Hook function for register reset */
722 typedef void CPResetFn(CPUARMState *env, const ARMCPRegInfo *opaque);
723 
724 #define CP_ANY 0xff
725 
726 /* Definition of an ARM coprocessor register */
727 struct ARMCPRegInfo {
728     /* Name of register (useful mainly for debugging, need not be unique) */
729     const char *name;
730     /*
731      * Location of register: coprocessor number and (crn,crm,opc1,opc2)
732      * tuple. Any of crm, opc1 and opc2 may be CP_ANY to indicate a
733      * 'wildcard' field -- any value of that field in the MRC/MCR insn
734      * will be decoded to this register. The register read and write
735      * callbacks will be passed an ARMCPRegInfo with the crn/crm/opc1/opc2
736      * used by the program, so it is possible to register a wildcard and
737      * then behave differently on read/write if necessary.
738      * For 64 bit registers, only crm and opc1 are relevant; crn and opc2
739      * must both be zero.
740      * For AArch64-visible registers, opc0 is also used.
741      * Since there are no "coprocessors" in AArch64, cp is purely used as a
742      * way to distinguish (for KVM's benefit) guest-visible system registers
743      * from demuxed ones provided to preserve the "no side effects on
744      * KVM register read/write from QEMU" semantics. cp==0x13 is guest
745      * visible (to match KVM's encoding); cp==0 will be converted to
746      * cp==0x13 when the ARMCPRegInfo is registered, for convenience.
747      */
748     uint8_t cp;
749     uint8_t crn;
750     uint8_t crm;
751     uint8_t opc0;
752     uint8_t opc1;
753     uint8_t opc2;
754     /* Execution state in which this register is visible: ARM_CP_STATE_* */
755     CPState state;
756     /* Register type: ARM_CP_* bits/values */
757     int type;
758     /* Access rights: PL*_[RW] */
759     CPAccessRights access;
760     /* Security state: ARM_CP_SECSTATE_* bits/values */
761     CPSecureState secure;
762     /*
763      * Which fine-grained trap register bit to check, if any. This
764      * value encodes both the trap register and bit within it.
765      */
766     FGTBit fgt;
767     /*
768      * The opaque pointer passed to define_arm_cp_regs_with_opaque() when
769      * this register was defined: can be used to hand data through to the
770      * register read/write functions, since they are passed the ARMCPRegInfo*.
771      */
772     void *opaque;
773     /*
774      * Value of this register, if it is ARM_CP_CONST. Otherwise, if
775      * fieldoffset is non-zero, the reset value of the register.
776      */
777     uint64_t resetvalue;
778     /*
779      * Offset of the field in CPUARMState for this register.
780      * This is not needed if either:
781      *  1. type is ARM_CP_CONST or one of the ARM_CP_SPECIALs
782      *  2. both readfn and writefn are specified
783      */
784     ptrdiff_t fieldoffset; /* offsetof(CPUARMState, field) */
785 
786     /*
787      * Offsets of the secure and non-secure fields in CPUARMState for the
788      * register if it is banked.  These fields are only used during the static
789      * registration of a register.  During hashing the bank associated
790      * with a given security state is copied to fieldoffset which is used from
791      * there on out.
792      *
793      * It is expected that register definitions use either fieldoffset or
794      * bank_fieldoffsets in the definition but not both.  It is also expected
795      * that both bank offsets are set when defining a banked register.  This
796      * use indicates that a register is banked.
797      */
798     ptrdiff_t bank_fieldoffsets[2];
799 
800     /*
801      * Function for making any access checks for this register in addition to
802      * those specified by the 'access' permissions bits. If NULL, no extra
803      * checks required. The access check is performed at runtime, not at
804      * translate time.
805      */
806     CPAccessFn *accessfn;
807     /*
808      * Function for handling reads of this register. If NULL, then reads
809      * will be done by loading from the offset into CPUARMState specified
810      * by fieldoffset.
811      */
812     CPReadFn *readfn;
813     /*
814      * Function for handling writes of this register. If NULL, then writes
815      * will be done by writing to the offset into CPUARMState specified
816      * by fieldoffset.
817      */
818     CPWriteFn *writefn;
819     /*
820      * Function for doing a "raw" read; used when we need to copy
821      * coprocessor state to the kernel for KVM or out for
822      * migration. This only needs to be provided if there is also a
823      * readfn and it has side effects (for instance clear-on-read bits).
824      */
825     CPReadFn *raw_readfn;
826     /*
827      * Function for doing a "raw" write; used when we need to copy KVM
828      * kernel coprocessor state into userspace, or for inbound
829      * migration. This only needs to be provided if there is also a
830      * writefn and it masks out "unwritable" bits or has write-one-to-clear
831      * or similar behaviour.
832      */
833     CPWriteFn *raw_writefn;
834     /*
835      * Function for resetting the register. If NULL, then reset will be done
836      * by writing resetvalue to the field specified in fieldoffset. If
837      * fieldoffset is 0 then no reset will be done.
838      */
839     CPResetFn *resetfn;
840 
841     /*
842      * "Original" writefn and readfn.
843      * For ARMv8.1-VHE register aliases, we overwrite the read/write
844      * accessor functions of various EL1/EL0 to perform the runtime
845      * check for which sysreg should actually be modified, and then
846      * forwards the operation.  Before overwriting the accessors,
847      * the original function is copied here, so that accesses that
848      * really do go to the EL1/EL0 version proceed normally.
849      * (The corresponding EL2 register is linked via opaque.)
850      */
851     CPReadFn *orig_readfn;
852     CPWriteFn *orig_writefn;
853 };
854 
855 /*
856  * Macros which are lvalues for the field in CPUARMState for the
857  * ARMCPRegInfo *ri.
858  */
859 #define CPREG_FIELD32(env, ri) \
860     (*(uint32_t *)((char *)(env) + (ri)->fieldoffset))
861 #define CPREG_FIELD64(env, ri) \
862     (*(uint64_t *)((char *)(env) + (ri)->fieldoffset))
863 
864 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, const ARMCPRegInfo *reg,
865                                        void *opaque);
866 
867 static inline void define_one_arm_cp_reg(ARMCPU *cpu, const ARMCPRegInfo *regs)
868 {
869     define_one_arm_cp_reg_with_opaque(cpu, regs, NULL);
870 }
871 
872 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
873                                         void *opaque, size_t len);
874 
875 #define define_arm_cp_regs_with_opaque(CPU, REGS, OPAQUE)               \
876     do {                                                                \
877         QEMU_BUILD_BUG_ON(ARRAY_SIZE(REGS) == 0);                       \
878         define_arm_cp_regs_with_opaque_len(CPU, REGS, OPAQUE,           \
879                                            ARRAY_SIZE(REGS));           \
880     } while (0)
881 
882 #define define_arm_cp_regs(CPU, REGS) \
883     define_arm_cp_regs_with_opaque(CPU, REGS, NULL)
884 
885 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp);
886 
887 /*
888  * Definition of an ARM co-processor register as viewed from
889  * userspace. This is used for presenting sanitised versions of
890  * registers to userspace when emulating the Linux AArch64 CPU
891  * ID/feature ABI (advertised as HWCAP_CPUID).
892  */
893 typedef struct ARMCPRegUserSpaceInfo {
894     /* Name of register */
895     const char *name;
896 
897     /* Is the name actually a glob pattern */
898     bool is_glob;
899 
900     /* Only some bits are exported to user space */
901     uint64_t exported_bits;
902 
903     /* Fixed bits are applied after the mask */
904     uint64_t fixed_bits;
905 } ARMCPRegUserSpaceInfo;
906 
907 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
908                                  const ARMCPRegUserSpaceInfo *mods,
909                                  size_t mods_len);
910 
911 #define modify_arm_cp_regs(REGS, MODS)                                  \
912     do {                                                                \
913         QEMU_BUILD_BUG_ON(ARRAY_SIZE(REGS) == 0);                       \
914         QEMU_BUILD_BUG_ON(ARRAY_SIZE(MODS) == 0);                       \
915         modify_arm_cp_regs_with_len(REGS, ARRAY_SIZE(REGS),             \
916                                     MODS, ARRAY_SIZE(MODS));            \
917     } while (0)
918 
919 /* CPWriteFn that can be used to implement writes-ignored behaviour */
920 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
921                          uint64_t value);
922 /* CPReadFn that can be used for read-as-zero behaviour */
923 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri);
924 
925 /* CPWriteFn that just writes the value to ri->fieldoffset */
926 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value);
927 
928 /*
929  * CPResetFn that does nothing, for use if no reset is required even
930  * if fieldoffset is non zero.
931  */
932 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque);
933 
934 /*
935  * Return true if this reginfo struct's field in the cpu state struct
936  * is 64 bits wide.
937  */
938 static inline bool cpreg_field_is_64bit(const ARMCPRegInfo *ri)
939 {
940     return (ri->state == ARM_CP_STATE_AA64) || (ri->type & ARM_CP_64BIT);
941 }
942 
943 static inline bool cp_access_ok(int current_el,
944                                 const ARMCPRegInfo *ri, int isread)
945 {
946     return (ri->access >> ((current_el * 2) + isread)) & 1;
947 }
948 
949 /* Raw read of a coprocessor register (as needed for migration, etc) */
950 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri);
951 
952 /*
953  * Return true if the cp register encoding is in the "feature ID space" as
954  * defined by FEAT_IDST (and thus should be reported with ER_ELx.EC
955  * as EC_SYSTEMREGISTERTRAP rather than EC_UNCATEGORIZED).
956  */
957 static inline bool arm_cpreg_encoding_in_idspace(uint8_t opc0, uint8_t opc1,
958                                                  uint8_t opc2,
959                                                  uint8_t crn, uint8_t crm)
960 {
961     return opc0 == 3 && (opc1 == 0 || opc1 == 1 || opc1 == 3) &&
962         crn == 0 && crm < 8;
963 }
964 
965 /*
966  * As arm_cpreg_encoding_in_idspace(), but take the encoding from an
967  * ARMCPRegInfo.
968  */
969 static inline bool arm_cpreg_in_idspace(const ARMCPRegInfo *ri)
970 {
971     return ri->state == ARM_CP_STATE_AA64 &&
972         arm_cpreg_encoding_in_idspace(ri->opc0, ri->opc1, ri->opc2,
973                                       ri->crn, ri->crm);
974 }
975 
976 #endif /* TARGET_ARM_CPREGS_H */
977