1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2021 Google LLC
4 * Author: Fuad Tabba <tabba@google.com>
5 */
6
7 #include <linux/irqchip/arm-gic-v3.h>
8
9 #include <asm/kvm_asm.h>
10 #include <asm/kvm_mmu.h>
11
12 #include <hyp/adjust_pc.h>
13
14 #include <nvhe/pkvm.h>
15
16 #include "../../sys_regs.h"
17
18 /*
19 * Copies of the host's CPU features registers holding sanitized values at hyp.
20 */
21 u64 id_aa64pfr0_el1_sys_val;
22 u64 id_aa64pfr1_el1_sys_val;
23 u64 id_aa64zfr0_el1_sys_val;
24 u64 id_aa64isar0_el1_sys_val;
25 u64 id_aa64isar1_el1_sys_val;
26 u64 id_aa64isar2_el1_sys_val;
27 u64 id_aa64mmfr0_el1_sys_val;
28 u64 id_aa64mmfr1_el1_sys_val;
29 u64 id_aa64mmfr2_el1_sys_val;
30 u64 id_aa64smfr0_el1_sys_val;
31
32 struct pvm_ftr_bits {
33 bool sign;
34 u8 shift;
35 u8 width;
36 u8 max_val;
37 bool (*vm_supported)(const struct kvm *kvm);
38 };
39
40 #define __MAX_FEAT_FUNC(id, fld, max, func, sgn) \
41 { \
42 .sign = sgn, \
43 .shift = id##_##fld##_SHIFT, \
44 .width = id##_##fld##_WIDTH, \
45 .max_val = id##_##fld##_##max, \
46 .vm_supported = func, \
47 }
48
49 #define MAX_FEAT_FUNC(id, fld, max, func) \
50 __MAX_FEAT_FUNC(id, fld, max, func, id##_##fld##_SIGNED)
51
52 #define MAX_FEAT(id, fld, max) \
53 MAX_FEAT_FUNC(id, fld, max, NULL)
54
55 #define MAX_FEAT_ENUM(id, fld, max) \
56 __MAX_FEAT_FUNC(id, fld, max, NULL, false)
57
58 #define FEAT_END { .width = 0, }
59
vm_has_ptrauth(const struct kvm * kvm)60 static bool vm_has_ptrauth(const struct kvm *kvm)
61 {
62 if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH))
63 return false;
64
65 return (cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH) ||
66 cpus_have_final_cap(ARM64_HAS_GENERIC_AUTH)) &&
67 kvm_vcpu_has_feature(kvm, KVM_ARM_VCPU_PTRAUTH_GENERIC);
68 }
69
vm_has_sve(const struct kvm * kvm)70 static bool vm_has_sve(const struct kvm *kvm)
71 {
72 return system_supports_sve() && kvm_vcpu_has_feature(kvm, KVM_ARM_VCPU_SVE);
73 }
74
75
76 /*
77 * Definitions for features to be allowed or restricted for protected guests.
78 *
79 * Each field in the masks represents the highest supported value for the
80 * feature. If a feature field is not present, it is not supported. Moreover,
81 * these are used to generate the guest's view of the feature registers.
82 *
83 * The approach for protected VMs is to at least support features that are:
84 * - Needed by common Linux distributions (e.g., floating point)
85 * - Trivial to support, e.g., supporting the feature does not introduce or
86 * require tracking of additional state in KVM
87 * - Cannot be trapped or prevent the guest from using anyway
88 *
89 * NOTE: Any field not explicitly defined will be restricted to 0. However,
90 * some of the features (mainly the signed ones) are restricted with a value
91 * other than 0. Therefore, it is important you define these if they are to be
92 * restricted.
93 */
94
95 static const struct pvm_ftr_bits pvmid_aa64pfr0[] = {
96 MAX_FEAT(ID_AA64PFR0_EL1, EL0, IMP),
97 MAX_FEAT(ID_AA64PFR0_EL1, EL1, IMP),
98 MAX_FEAT(ID_AA64PFR0_EL1, EL2, IMP),
99 MAX_FEAT(ID_AA64PFR0_EL1, EL3, IMP),
100 MAX_FEAT(ID_AA64PFR0_EL1, FP, FP16),
101 MAX_FEAT(ID_AA64PFR0_EL1, AdvSIMD, FP16),
102 MAX_FEAT(ID_AA64PFR0_EL1, GIC, IMP),
103 MAX_FEAT_FUNC(ID_AA64PFR0_EL1, SVE, IMP, vm_has_sve),
104 MAX_FEAT(ID_AA64PFR0_EL1, RAS, IMP),
105 MAX_FEAT(ID_AA64PFR0_EL1, DIT, IMP),
106 MAX_FEAT(ID_AA64PFR0_EL1, CSV2, IMP),
107 MAX_FEAT(ID_AA64PFR0_EL1, CSV3, IMP),
108 FEAT_END
109 };
110
111 static const struct pvm_ftr_bits pvmid_aa64pfr1[] = {
112 MAX_FEAT(ID_AA64PFR1_EL1, BT, IMP),
113 MAX_FEAT(ID_AA64PFR1_EL1, SSBS, SSBS2),
114 MAX_FEAT_ENUM(ID_AA64PFR1_EL1, MTE_frac, NI),
115 FEAT_END
116 };
117
118 /*
119 * Restrict to the following features for protected VMs:
120 * - 40-bit IPA
121 * - 16-bit ASID
122 */
123 // TODO: check the tgran
124 static const struct pvm_ftr_bits pvmid_aa64mmfr0[] = {
125 MAX_FEAT_ENUM(ID_AA64MMFR0_EL1, PARANGE, 40),
126 MAX_FEAT_ENUM(ID_AA64MMFR0_EL1, ASIDBITS, 16),
127 MAX_FEAT(ID_AA64MMFR0_EL1, BIGEND, IMP),
128 MAX_FEAT(ID_AA64MMFR0_EL1, SNSMEM, IMP),
129 MAX_FEAT(ID_AA64MMFR0_EL1, BIGENDEL0, IMP),
130 MAX_FEAT(ID_AA64MMFR0_EL1, EXS, IMP),
131 FEAT_END
132 };
133
134 static const struct pvm_ftr_bits pvmid_aa64mmfr1[] = {
135 MAX_FEAT(ID_AA64MMFR1_EL1, HAFDBS, DBM),
136 MAX_FEAT_ENUM(ID_AA64MMFR1_EL1, VMIDBits, 16),
137 MAX_FEAT(ID_AA64MMFR1_EL1, HPDS, HPDS2),
138 MAX_FEAT(ID_AA64MMFR1_EL1, PAN, PAN3),
139 MAX_FEAT(ID_AA64MMFR1_EL1, SpecSEI, IMP),
140 MAX_FEAT(ID_AA64MMFR1_EL1, ETS, IMP),
141 MAX_FEAT(ID_AA64MMFR1_EL1, CMOW, IMP),
142 FEAT_END
143 };
144
145 static const struct pvm_ftr_bits pvmid_aa64mmfr2[] = {
146 MAX_FEAT(ID_AA64MMFR2_EL1, CnP, IMP),
147 MAX_FEAT(ID_AA64MMFR2_EL1, UAO, IMP),
148 MAX_FEAT(ID_AA64MMFR2_EL1, IESB, IMP),
149 MAX_FEAT(ID_AA64MMFR2_EL1, AT, IMP),
150 MAX_FEAT_ENUM(ID_AA64MMFR2_EL1, IDS, 0x18),
151 MAX_FEAT(ID_AA64MMFR2_EL1, TTL, IMP),
152 MAX_FEAT(ID_AA64MMFR2_EL1, BBM, 2),
153 MAX_FEAT(ID_AA64MMFR2_EL1, E0PD, IMP),
154 FEAT_END
155 };
156
157 /* Restrict pointer authentication to the basic version. */
158 static const struct pvm_ftr_bits pvmid_aa64isar1[] = {
159 MAX_FEAT(ID_AA64ISAR1_EL1, DPB, DPB2),
160 MAX_FEAT_FUNC(ID_AA64ISAR1_EL1, APA, PAuth, vm_has_ptrauth),
161 MAX_FEAT_FUNC(ID_AA64ISAR1_EL1, API, PAuth, vm_has_ptrauth),
162 MAX_FEAT(ID_AA64ISAR1_EL1, JSCVT, IMP),
163 MAX_FEAT(ID_AA64ISAR1_EL1, FCMA, IMP),
164 MAX_FEAT(ID_AA64ISAR1_EL1, LRCPC, LRCPC3),
165 MAX_FEAT(ID_AA64ISAR1_EL1, GPA, IMP),
166 MAX_FEAT(ID_AA64ISAR1_EL1, GPI, IMP),
167 MAX_FEAT(ID_AA64ISAR1_EL1, FRINTTS, IMP),
168 MAX_FEAT(ID_AA64ISAR1_EL1, SB, IMP),
169 MAX_FEAT(ID_AA64ISAR1_EL1, SPECRES, COSP_RCTX),
170 MAX_FEAT(ID_AA64ISAR1_EL1, BF16, EBF16),
171 MAX_FEAT(ID_AA64ISAR1_EL1, DGH, IMP),
172 MAX_FEAT(ID_AA64ISAR1_EL1, I8MM, IMP),
173 FEAT_END
174 };
175
176 static const struct pvm_ftr_bits pvmid_aa64isar2[] = {
177 MAX_FEAT_FUNC(ID_AA64ISAR2_EL1, GPA3, IMP, vm_has_ptrauth),
178 MAX_FEAT_FUNC(ID_AA64ISAR2_EL1, APA3, PAuth, vm_has_ptrauth),
179 MAX_FEAT(ID_AA64ISAR2_EL1, MOPS, IMP),
180 MAX_FEAT(ID_AA64ISAR2_EL1, ATS1A, IMP),
181 FEAT_END
182 };
183
184 static const struct pvm_ftr_bits pvmid_aa64zfr0[] = {
185 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, SVEver, IMP, vm_has_sve),
186 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, AES, IMP, vm_has_sve),
187 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, BitPerm, IMP, vm_has_sve),
188 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, BF16, IMP, vm_has_sve),
189 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, SHA3, IMP, vm_has_sve),
190 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, SM4, IMP, vm_has_sve),
191 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, I8MM, IMP, vm_has_sve),
192 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, F32MM, IMP, vm_has_sve),
193 MAX_FEAT_FUNC(ID_AA64ZFR0_EL1, F64MM, IMP, vm_has_sve),
194 FEAT_END
195 };
196
197 /*
198 * None of the features in ID_AA64DFR0_EL1 nor ID_AA64MMFR4_EL1 are supported.
199 * However, both have Not-Implemented values that are non-zero. Define them
200 * so they can be used when getting the value of these registers.
201 */
202 #define ID_AA64DFR0_EL1_NONZERO_NI \
203 ( \
204 SYS_FIELD_PREP_ENUM(ID_AA64DFR0_EL1, DoubleLock, NI) | \
205 SYS_FIELD_PREP_ENUM(ID_AA64DFR0_EL1, MTPMU, NI) \
206 )
207
208 #define ID_AA64MMFR4_EL1_NONZERO_NI \
209 SYS_FIELD_PREP_ENUM(ID_AA64MMFR4_EL1, E2H0, NI)
210
211 /*
212 * Returns the value of the feature registers based on the system register
213 * value, the vcpu support for the revelant features, and the additional
214 * restrictions for protected VMs.
215 */
get_restricted_features(const struct kvm_vcpu * vcpu,u64 sys_reg_val,const struct pvm_ftr_bits restrictions[])216 static u64 get_restricted_features(const struct kvm_vcpu *vcpu,
217 u64 sys_reg_val,
218 const struct pvm_ftr_bits restrictions[])
219 {
220 u64 val = 0UL;
221 int i;
222
223 for (i = 0; restrictions[i].width != 0; i++) {
224 bool (*vm_supported)(const struct kvm *) = restrictions[i].vm_supported;
225 bool sign = restrictions[i].sign;
226 int shift = restrictions[i].shift;
227 int width = restrictions[i].width;
228 u64 min_signed = (1UL << width) - 1UL;
229 u64 sign_bit = 1UL << (width - 1);
230 u64 mask = GENMASK_ULL(width + shift - 1, shift);
231 u64 sys_val = (sys_reg_val & mask) >> shift;
232 u64 pvm_max = restrictions[i].max_val;
233
234 if (vm_supported && !vm_supported(vcpu->kvm))
235 val |= (sign ? min_signed : 0) << shift;
236 else if (sign && (sys_val >= sign_bit || pvm_max >= sign_bit))
237 val |= max(sys_val, pvm_max) << shift;
238 else
239 val |= min(sys_val, pvm_max) << shift;
240 }
241
242 return val;
243 }
244
245 /* Read a sanitized cpufeature ID register by its encoding */
pvm_calc_id_reg(const struct kvm_vcpu * vcpu,u32 id)246 static u64 pvm_calc_id_reg(const struct kvm_vcpu *vcpu, u32 id)
247 {
248 switch (id) {
249 case SYS_ID_AA64PFR0_EL1:
250 return get_restricted_features(vcpu, id_aa64pfr0_el1_sys_val, pvmid_aa64pfr0);
251 case SYS_ID_AA64PFR1_EL1:
252 return get_restricted_features(vcpu, id_aa64pfr1_el1_sys_val, pvmid_aa64pfr1);
253 case SYS_ID_AA64ISAR0_EL1:
254 return id_aa64isar0_el1_sys_val;
255 case SYS_ID_AA64ISAR1_EL1:
256 return get_restricted_features(vcpu, id_aa64isar1_el1_sys_val, pvmid_aa64isar1);
257 case SYS_ID_AA64ISAR2_EL1:
258 return get_restricted_features(vcpu, id_aa64isar2_el1_sys_val, pvmid_aa64isar2);
259 case SYS_ID_AA64MMFR0_EL1:
260 return get_restricted_features(vcpu, id_aa64mmfr0_el1_sys_val, pvmid_aa64mmfr0);
261 case SYS_ID_AA64MMFR1_EL1:
262 return get_restricted_features(vcpu, id_aa64mmfr1_el1_sys_val, pvmid_aa64mmfr1);
263 case SYS_ID_AA64MMFR2_EL1:
264 return get_restricted_features(vcpu, id_aa64mmfr2_el1_sys_val, pvmid_aa64mmfr2);
265 case SYS_ID_AA64ZFR0_EL1:
266 return get_restricted_features(vcpu, id_aa64zfr0_el1_sys_val, pvmid_aa64zfr0);
267 case SYS_ID_AA64DFR0_EL1:
268 return ID_AA64DFR0_EL1_NONZERO_NI;
269 case SYS_ID_AA64MMFR4_EL1:
270 return ID_AA64MMFR4_EL1_NONZERO_NI;
271 default:
272 /* Unhandled ID register, RAZ */
273 return 0;
274 }
275 }
276
277 /*
278 * Inject an unknown/undefined exception to an AArch64 guest while most of its
279 * sysregs are live.
280 */
inject_undef64(struct kvm_vcpu * vcpu)281 static void inject_undef64(struct kvm_vcpu *vcpu)
282 {
283 u64 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
284
285 *vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
286 *vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
287 __vcpu_sys_reg(vcpu, VBAR_EL1) = read_sysreg_el1(SYS_VBAR);
288
289 kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
290
291 __kvm_adjust_pc(vcpu);
292
293 write_sysreg_el1(esr, SYS_ESR);
294 write_sysreg_el1(read_sysreg_el2(SYS_ELR), SYS_ELR);
295 write_sysreg_el2(*vcpu_pc(vcpu), SYS_ELR);
296 write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
297 }
298
read_id_reg(const struct kvm_vcpu * vcpu,struct sys_reg_desc const * r)299 static u64 read_id_reg(const struct kvm_vcpu *vcpu,
300 struct sys_reg_desc const *r)
301 {
302 struct kvm *kvm = vcpu->kvm;
303 u32 reg = reg_to_encoding(r);
304
305 if (WARN_ON_ONCE(!test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags)))
306 return 0;
307
308 if (reg >= sys_reg(3, 0, 0, 1, 0) && reg <= sys_reg(3, 0, 0, 7, 7))
309 return kvm->arch.id_regs[IDREG_IDX(reg)];
310
311 return 0;
312 }
313
314 /* Handler to RAZ/WI sysregs */
pvm_access_raz_wi(struct kvm_vcpu * vcpu,struct sys_reg_params * p,const struct sys_reg_desc * r)315 static bool pvm_access_raz_wi(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
316 const struct sys_reg_desc *r)
317 {
318 if (!p->is_write)
319 p->regval = 0;
320
321 return true;
322 }
323
324 /*
325 * Accessor for AArch32 feature id registers.
326 *
327 * The value of these registers is "unknown" according to the spec if AArch32
328 * isn't supported.
329 */
pvm_access_id_aarch32(struct kvm_vcpu * vcpu,struct sys_reg_params * p,const struct sys_reg_desc * r)330 static bool pvm_access_id_aarch32(struct kvm_vcpu *vcpu,
331 struct sys_reg_params *p,
332 const struct sys_reg_desc *r)
333 {
334 if (p->is_write) {
335 inject_undef64(vcpu);
336 return false;
337 }
338
339 return pvm_access_raz_wi(vcpu, p, r);
340 }
341
342 /*
343 * Accessor for AArch64 feature id registers.
344 *
345 * If access is allowed, set the regval to the protected VM's view of the
346 * register and return true.
347 * Otherwise, inject an undefined exception and return false.
348 */
pvm_access_id_aarch64(struct kvm_vcpu * vcpu,struct sys_reg_params * p,const struct sys_reg_desc * r)349 static bool pvm_access_id_aarch64(struct kvm_vcpu *vcpu,
350 struct sys_reg_params *p,
351 const struct sys_reg_desc *r)
352 {
353 if (p->is_write) {
354 inject_undef64(vcpu);
355 return false;
356 }
357
358 p->regval = read_id_reg(vcpu, r);
359 return true;
360 }
361
pvm_access_unallocated(struct kvm_vcpu * vcpu,struct sys_reg_params * p,const struct sys_reg_desc * r)362 static bool pvm_access_unallocated(struct kvm_vcpu *vcpu,
363 struct sys_reg_params *p,
364 const struct sys_reg_desc *r)
365 {
366 if (p->is_write) {
367 inject_undef64(vcpu);
368 return false;
369 }
370
371 p->regval = 0;
372 return true;
373 }
374
pvm_gic_read_sre(struct kvm_vcpu * vcpu,struct sys_reg_params * p,const struct sys_reg_desc * r)375 static bool pvm_gic_read_sre(struct kvm_vcpu *vcpu,
376 struct sys_reg_params *p,
377 const struct sys_reg_desc *r)
378 {
379 /* pVMs only support GICv3. 'nuf said. */
380 if (!p->is_write)
381 p->regval = ICC_SRE_EL1_DIB | ICC_SRE_EL1_DFB | ICC_SRE_EL1_SRE;
382
383 return true;
384 }
385
386 /* Mark the specified system register as an AArch32 feature id register. */
387 #define AARCH32(REG) { SYS_DESC(REG), .access = pvm_access_id_aarch32 }
388
389 /* Mark the specified system register as an AArch64 feature id register. */
390 #define AARCH64(REG) { SYS_DESC(REG), .access = pvm_access_id_aarch64 }
391
392 /*
393 * sys_reg_desc initialiser for architecturally unallocated cpufeature ID
394 * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
395 * (1 <= crm < 8, 0 <= Op2 < 8).
396 */
397 #define ID_UNALLOCATED(crm, op2) { \
398 Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2), \
399 .access = pvm_access_unallocated, \
400 }
401
402 /*
403 * sys_reg_desc initialiser for known ID registers that we hide from guests.
404 * For now, these are handled just like unallocated ID regs.
405 */
406 #define ID_HIDDEN(REG) { \
407 SYS_DESC(REG), \
408 .access = pvm_access_unallocated, \
409 }
410
411 /* Mark the specified system register as Read-As-Zero/Write-Ignored */
412 #define RAZ_WI(REG) { SYS_DESC(REG), .access = pvm_access_raz_wi }
413
414 /* Mark the specified system register as not being handled in hyp. */
415 #define HOST_HANDLED(REG) { SYS_DESC(REG), .access = NULL }
416
417 /*
418 * Architected system registers.
419 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
420 *
421 * NOTE: Anything not explicitly listed here is *restricted by default*, i.e.,
422 * it will lead to injecting an exception into the guest.
423 */
424 static const struct sys_reg_desc pvm_sys_reg_descs[] = {
425 /* Cache maintenance by set/way operations are restricted. */
426
427 /* Debug and Trace Registers are restricted. */
428 RAZ_WI(SYS_DBGBVRn_EL1(0)),
429 RAZ_WI(SYS_DBGBCRn_EL1(0)),
430 RAZ_WI(SYS_DBGWVRn_EL1(0)),
431 RAZ_WI(SYS_DBGWCRn_EL1(0)),
432 RAZ_WI(SYS_MDSCR_EL1),
433 RAZ_WI(SYS_OSLAR_EL1),
434 RAZ_WI(SYS_OSLSR_EL1),
435 RAZ_WI(SYS_OSDLR_EL1),
436
437 /* Group 1 ID registers */
438 RAZ_WI(SYS_REVIDR_EL1),
439
440 /* AArch64 mappings of the AArch32 ID registers */
441 /* CRm=1 */
442 AARCH32(SYS_ID_PFR0_EL1),
443 AARCH32(SYS_ID_PFR1_EL1),
444 AARCH32(SYS_ID_DFR0_EL1),
445 AARCH32(SYS_ID_AFR0_EL1),
446 AARCH32(SYS_ID_MMFR0_EL1),
447 AARCH32(SYS_ID_MMFR1_EL1),
448 AARCH32(SYS_ID_MMFR2_EL1),
449 AARCH32(SYS_ID_MMFR3_EL1),
450
451 /* CRm=2 */
452 AARCH32(SYS_ID_ISAR0_EL1),
453 AARCH32(SYS_ID_ISAR1_EL1),
454 AARCH32(SYS_ID_ISAR2_EL1),
455 AARCH32(SYS_ID_ISAR3_EL1),
456 AARCH32(SYS_ID_ISAR4_EL1),
457 AARCH32(SYS_ID_ISAR5_EL1),
458 AARCH32(SYS_ID_MMFR4_EL1),
459 AARCH32(SYS_ID_ISAR6_EL1),
460
461 /* CRm=3 */
462 AARCH32(SYS_MVFR0_EL1),
463 AARCH32(SYS_MVFR1_EL1),
464 AARCH32(SYS_MVFR2_EL1),
465 ID_UNALLOCATED(3,3),
466 AARCH32(SYS_ID_PFR2_EL1),
467 AARCH32(SYS_ID_DFR1_EL1),
468 AARCH32(SYS_ID_MMFR5_EL1),
469 ID_UNALLOCATED(3,7),
470
471 /* AArch64 ID registers */
472 /* CRm=4 */
473 AARCH64(SYS_ID_AA64PFR0_EL1),
474 AARCH64(SYS_ID_AA64PFR1_EL1),
475 ID_UNALLOCATED(4,2),
476 ID_UNALLOCATED(4,3),
477 AARCH64(SYS_ID_AA64ZFR0_EL1),
478 ID_HIDDEN(SYS_ID_AA64SMFR0_EL1),
479 ID_UNALLOCATED(4,6),
480 ID_UNALLOCATED(4,7),
481 AARCH64(SYS_ID_AA64DFR0_EL1),
482 AARCH64(SYS_ID_AA64DFR1_EL1),
483 ID_UNALLOCATED(5,2),
484 ID_UNALLOCATED(5,3),
485 ID_HIDDEN(SYS_ID_AA64AFR0_EL1),
486 ID_HIDDEN(SYS_ID_AA64AFR1_EL1),
487 ID_UNALLOCATED(5,6),
488 ID_UNALLOCATED(5,7),
489 AARCH64(SYS_ID_AA64ISAR0_EL1),
490 AARCH64(SYS_ID_AA64ISAR1_EL1),
491 AARCH64(SYS_ID_AA64ISAR2_EL1),
492 ID_UNALLOCATED(6,3),
493 ID_UNALLOCATED(6,4),
494 ID_UNALLOCATED(6,5),
495 ID_UNALLOCATED(6,6),
496 ID_UNALLOCATED(6,7),
497 AARCH64(SYS_ID_AA64MMFR0_EL1),
498 AARCH64(SYS_ID_AA64MMFR1_EL1),
499 AARCH64(SYS_ID_AA64MMFR2_EL1),
500 ID_UNALLOCATED(7,3),
501 ID_UNALLOCATED(7,4),
502 ID_UNALLOCATED(7,5),
503 ID_UNALLOCATED(7,6),
504 ID_UNALLOCATED(7,7),
505
506 RAZ_WI(SYS_ERRIDR_EL1),
507 RAZ_WI(SYS_ERRSELR_EL1),
508 RAZ_WI(SYS_ERXFR_EL1),
509 RAZ_WI(SYS_ERXCTLR_EL1),
510 RAZ_WI(SYS_ERXSTATUS_EL1),
511 RAZ_WI(SYS_ERXADDR_EL1),
512 RAZ_WI(SYS_ERXMISC0_EL1),
513 RAZ_WI(SYS_ERXMISC1_EL1),
514
515 /* Performance Monitoring Registers are restricted. */
516
517 /* Limited Ordering Regions Registers are restricted. */
518
519 HOST_HANDLED(SYS_ICC_SGI1R_EL1),
520 HOST_HANDLED(SYS_ICC_ASGI1R_EL1),
521 HOST_HANDLED(SYS_ICC_SGI0R_EL1),
522 { SYS_DESC(SYS_ICC_SRE_EL1), .access = pvm_gic_read_sre, },
523
524 HOST_HANDLED(SYS_CCSIDR_EL1),
525 HOST_HANDLED(SYS_CLIDR_EL1),
526 RAZ_WI(SYS_AIDR_EL1),
527 HOST_HANDLED(SYS_CSSELR_EL1),
528 HOST_HANDLED(SYS_CTR_EL0),
529
530 /* Performance Monitoring Registers are restricted. */
531
532 /* Activity Monitoring Registers are restricted. */
533
534 HOST_HANDLED(SYS_CNTP_TVAL_EL0),
535 HOST_HANDLED(SYS_CNTP_CTL_EL0),
536 HOST_HANDLED(SYS_CNTP_CVAL_EL0),
537
538 /* Performance Monitoring Registers are restricted. */
539 };
540
541 /* A structure to track reset values for system registers in protected vcpus. */
542 struct sys_reg_desc_reset {
543 /* Index into sys_reg[]. */
544 int reg;
545
546 /* Reset function. */
547 void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc_reset *);
548
549 /* Reset value. */
550 u64 value;
551 };
552
reset_actlr(struct kvm_vcpu * vcpu,const struct sys_reg_desc_reset * r)553 static void reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc_reset *r)
554 {
555 __vcpu_sys_reg(vcpu, r->reg) = read_sysreg(actlr_el1);
556 }
557
reset_amair_el1(struct kvm_vcpu * vcpu,const struct sys_reg_desc_reset * r)558 static void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc_reset *r)
559 {
560 __vcpu_sys_reg(vcpu, r->reg) = read_sysreg(amair_el1);
561 }
562
reset_mpidr(struct kvm_vcpu * vcpu,const struct sys_reg_desc_reset * r)563 static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc_reset *r)
564 {
565 __vcpu_sys_reg(vcpu, r->reg) = calculate_mpidr(vcpu);
566 }
567
reset_value(struct kvm_vcpu * vcpu,const struct sys_reg_desc_reset * r)568 static void reset_value(struct kvm_vcpu *vcpu, const struct sys_reg_desc_reset *r)
569 {
570 __vcpu_sys_reg(vcpu, r->reg) = r->value;
571 }
572
573 /* Specify the register's reset value. */
574 #define RESET_VAL(REG, RESET_VAL) { REG, reset_value, RESET_VAL }
575
576 #define RESET_ZERO(REG) RESET_VAL(REG, 0)
577
578 #define RESET_UNKNOWN(REG) RESET_VAL(REG, 0x1de7ec7edbadc0deULL)
579
580 /* Specify a function that calculates the register's reset value. */
581 #define RESET_FUNC(REG, RESET_FUNC) { REG, RESET_FUNC, 0 }
582
583 /*
584 * Architected system registers reset values for Protected VMs.
585 * Important: Must be sorted ascending by REG (index into sys_reg[])
586 */
587 static const struct sys_reg_desc_reset pvm_sys_reg_reset_vals[] = {
588 RESET_FUNC(MPIDR_EL1, reset_mpidr),
589 RESET_UNKNOWN(TPIDR_EL0),
590 RESET_UNKNOWN(TPIDRRO_EL0),
591 RESET_UNKNOWN(TPIDR_EL1),
592 RESET_ZERO(CNTKCTL_EL1),
593 RESET_UNKNOWN(PAR_EL1),
594 RESET_ZERO(MDCCINT_EL1),
595 RESET_ZERO(DISR_EL1),
596 RESET_ZERO(PMCCFILTR_EL0),
597 RESET_ZERO(PMUSERENR_EL0),
598 RESET_ZERO(CPACR_EL1),
599 RESET_VAL(CONTEXTIDR_EL1, 0x00000000dbadc0deULL),
600 RESET_VAL(SCTLR_EL1, 0x00C50078ULL),
601 RESET_FUNC(ACTLR_EL1, reset_actlr),
602 RESET_ZERO(TCR_EL1),
603 RESET_UNKNOWN(AFSR0_EL1),
604 RESET_UNKNOWN(AFSR1_EL1),
605 RESET_UNKNOWN(ESR_EL1),
606 RESET_UNKNOWN(MAIR_EL1),
607 RESET_FUNC(AMAIR_EL1, reset_amair_el1),
608 RESET_ZERO(MDSCR_EL1),
609 RESET_ZERO(ZCR_EL1),
610 RESET_UNKNOWN(TTBR0_EL1),
611 RESET_UNKNOWN(TTBR1_EL1),
612 RESET_UNKNOWN(FAR_EL1),
613 RESET_VAL(VBAR_EL1, 0x1de7ec7edbadc000ULL),
614 RESET_UNKNOWN(PIRE0_EL1),
615 RESET_UNKNOWN(PIR_EL1),
616 };
617
618 /*
619 * Initializes feature registers for protected vms.
620 */
kvm_init_pvm_id_regs(struct kvm_vcpu * vcpu)621 void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu)
622 {
623 struct kvm *kvm = vcpu->kvm;
624 struct kvm_arch *ka = &kvm->arch;
625 u32 r;
626
627 if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
628 return;
629
630 /*
631 * Initialize only AArch64 id registers since AArch32 isn't supported
632 * for protected VMs.
633 */
634 for (r = sys_reg(3, 0, 0, 1, 0); r <= sys_reg(3, 0, 0, 7, 7); r += sys_reg(0, 0, 0, 0, 1))
635 ka->id_regs[IDREG_IDX(r)] = pvm_calc_id_reg(vcpu, r);
636
637 set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags);
638 }
639
640 /*
641 * Sets system registers to reset value
642 *
643 * This function finds the right entry and sets the registers on the protected
644 * vcpu to their architecturally defined reset values.
645 */
kvm_reset_pvm_sys_regs(struct kvm_vcpu * vcpu)646 void kvm_reset_pvm_sys_regs(struct kvm_vcpu *vcpu)
647 {
648 unsigned long i;
649
650 for (i = 0; i < ARRAY_SIZE(pvm_sys_reg_reset_vals); i++) {
651 const struct sys_reg_desc_reset *r = &pvm_sys_reg_reset_vals[i];
652
653 r->reset(vcpu, r);
654 }
655 }
656
657 /*
658 * Checks that the sysreg tables are unique and in-order.
659 *
660 * Returns 0 if the table is consistent, or 1 otherwise.
661 */
kvm_check_pvm_sysreg_table(void)662 int kvm_check_pvm_sysreg_table(void)
663 {
664 unsigned int i;
665
666 for (i = 1; i < ARRAY_SIZE(pvm_sys_reg_descs); i++) {
667 if (cmp_sys_reg(&pvm_sys_reg_descs[i-1], &pvm_sys_reg_descs[i]) >= 0)
668 return 1;
669 }
670
671 for (i = 1; i < ARRAY_SIZE(pvm_sys_reg_reset_vals); i++) {
672 if (pvm_sys_reg_reset_vals[i-1].reg >= pvm_sys_reg_reset_vals[i].reg)
673 return 1;
674 }
675
676 return 0;
677 }
678
679 /*
680 * Handler for protected VM MSR, MRS or System instruction execution.
681 *
682 * Returns true if the hypervisor has handled the exit, and control should go
683 * back to the guest, or false if it hasn't, to be handled by the host.
684 */
kvm_handle_pvm_sysreg(struct kvm_vcpu * vcpu,u64 * exit_code)685 bool kvm_handle_pvm_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
686 {
687 const struct sys_reg_desc *r;
688 struct sys_reg_params params;
689 unsigned long esr = kvm_vcpu_get_esr(vcpu);
690 int Rt = kvm_vcpu_sys_get_rt(vcpu);
691
692 params = esr_sys64_to_params(esr);
693 params.regval = vcpu_get_reg(vcpu, Rt);
694
695 r = find_reg(¶ms, pvm_sys_reg_descs, ARRAY_SIZE(pvm_sys_reg_descs));
696
697 /* Undefined (RESTRICTED). */
698 if (r == NULL) {
699 inject_undef64(vcpu);
700 return true;
701 }
702
703 /* Handled by the host (HOST_HANDLED) */
704 if (r->access == NULL)
705 return false;
706
707 /* Handled by hyp: skip instruction if instructed to do so. */
708 if (r->access(vcpu, ¶ms, r))
709 __kvm_skip_instr(vcpu);
710
711 if (!params.is_write)
712 vcpu_set_reg(vcpu, Rt, params.regval);
713
714 return true;
715 }
716
717 /*
718 * Handler for protected VM restricted exceptions.
719 *
720 * Inject an undefined exception into the guest and return true to indicate that
721 * the hypervisor has handled the exit, and control should go back to the guest.
722 */
kvm_handle_pvm_restricted(struct kvm_vcpu * vcpu,u64 * exit_code)723 bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code)
724 {
725 inject_undef64(vcpu);
726 return true;
727 }
728