Skip to content

Commit cc5f84f

Browse files
Marc Zyngieroupton
authored andcommitted
KVM: arm64: Use the xarray as the primary sysreg/sysinsn walker
Since we always start sysreg/sysinsn handling by searching the xarray, use it as the source of the index in the correct sys_reg_desc array. This allows some cleanup, such as moving the handling of unknown sysregs in a single location. Reviewed-by: Joey Gouly <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Oliver Upton <[email protected]>
1 parent 19f3e7e commit cc5f84f

File tree

3 files changed

+50
-55
lines changed

3 files changed

+50
-55
lines changed

arch/arm64/include/asm/kvm_nested.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static inline u64 translate_ttbr0_el2_to_ttbr0_el1(u64 ttbr0)
6060
return ttbr0 & ~GENMASK_ULL(63, 48);
6161
}
6262

63-
extern bool __check_nv_sr_forward(struct kvm_vcpu *vcpu);
63+
extern bool __check_nv_sr_forward(struct kvm_vcpu *vcpu, int *sr_idx);
6464

6565
int kvm_init_nv_sysregs(struct kvm *kvm);
6666

arch/arm64/kvm/emulate-nested.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,17 +2007,14 @@ static bool check_fgt_bit(struct kvm *kvm, bool is_read,
20072007
return !(kvm_get_sysreg_res0(kvm, sr) & BIT(tc.bit));
20082008
}
20092009

2010-
bool __check_nv_sr_forward(struct kvm_vcpu *vcpu)
2010+
bool __check_nv_sr_forward(struct kvm_vcpu *vcpu, int *sr_index)
20112011
{
20122012
union trap_config tc;
20132013
enum trap_behaviour b;
20142014
bool is_read;
20152015
u32 sysreg;
20162016
u64 esr, val;
20172017

2018-
if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu))
2019-
return false;
2020-
20212018
esr = kvm_vcpu_get_esr(vcpu);
20222019
sysreg = esr_sys64_to_sysreg(esr);
20232020
is_read = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ;
@@ -2028,13 +2025,16 @@ bool __check_nv_sr_forward(struct kvm_vcpu *vcpu)
20282025
* A value of 0 for the whole entry means that we know nothing
20292026
* for this sysreg, and that it cannot be re-injected into the
20302027
* nested hypervisor. In this situation, let's cut it short.
2031-
*
2032-
* Note that ultimately, we could also make use of the xarray
2033-
* to store the index of the sysreg in the local descriptor
2034-
* array, avoiding another search... Hint, hint...
20352028
*/
20362029
if (!tc.val)
2037-
return false;
2030+
goto local;
2031+
2032+
/*
2033+
* If we're not nesting, immediately return to the caller, with the
2034+
* sysreg index, should we have it.
2035+
*/
2036+
if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu))
2037+
goto local;
20382038

20392039
switch ((enum fgt_group_id)tc.fgt) {
20402040
case __NO_FGT_GROUP__:
@@ -2076,7 +2076,7 @@ bool __check_nv_sr_forward(struct kvm_vcpu *vcpu)
20762076
case __NR_FGT_GROUP_IDS__:
20772077
/* Something is really wrong, bail out */
20782078
WARN_ONCE(1, "__NR_FGT_GROUP_IDS__");
2079-
return false;
2079+
goto local;
20802080
}
20812081

20822082
if (tc.fgt != __NO_FGT_GROUP__ && check_fgt_bit(vcpu->kvm, is_read,
@@ -2089,6 +2089,26 @@ bool __check_nv_sr_forward(struct kvm_vcpu *vcpu)
20892089
((b & BEHAVE_FORWARD_WRITE) && !is_read))
20902090
goto inject;
20912091

2092+
local:
2093+
if (!tc.sri) {
2094+
struct sys_reg_params params;
2095+
2096+
params = esr_sys64_to_params(esr);
2097+
2098+
/*
2099+
* Check for the IMPDEF range, as per DDI0487 J.a,
2100+
* D18.3.2 Reserved encodings for IMPLEMENTATION
2101+
* DEFINED registers.
2102+
*/
2103+
if (!(params.Op0 == 3 && (params.CRn & 0b1011) == 0b1011))
2104+
print_sys_reg_msg(&params,
2105+
"Unsupported guest access at: %lx\n",
2106+
*vcpu_pc(vcpu));
2107+
kvm_inject_undefined(vcpu);
2108+
return true;
2109+
}
2110+
2111+
*sr_index = tc.sri - 1;
20922112
return false;
20932113

20942114
inject:

arch/arm64/kvm/sys_regs.c

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,12 +3395,6 @@ int kvm_handle_cp14_32(struct kvm_vcpu *vcpu)
33953395
return kvm_handle_cp_32(vcpu, &params, cp14_regs, ARRAY_SIZE(cp14_regs));
33963396
}
33973397

3398-
static bool is_imp_def_sys_reg(struct sys_reg_params *params)
3399-
{
3400-
// See ARM DDI 0487E.a, section D12.3.2
3401-
return params->Op0 == 3 && (params->CRn & 0b1011) == 0b1011;
3402-
}
3403-
34043398
/**
34053399
* emulate_sys_reg - Emulate a guest access to an AArch64 system register
34063400
* @vcpu: The VCPU pointer
@@ -3409,44 +3403,22 @@ static bool is_imp_def_sys_reg(struct sys_reg_params *params)
34093403
* Return: true if the system register access was successful, false otherwise.
34103404
*/
34113405
static bool emulate_sys_reg(struct kvm_vcpu *vcpu,
3412-
struct sys_reg_params *params)
3406+
struct sys_reg_params *params)
34133407
{
34143408
const struct sys_reg_desc *r;
34153409

34163410
r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
3417-
34183411
if (likely(r)) {
34193412
perform_access(vcpu, params, r);
34203413
return true;
34213414
}
34223415

3423-
if (is_imp_def_sys_reg(params)) {
3424-
kvm_inject_undefined(vcpu);
3425-
} else {
3426-
print_sys_reg_msg(params,
3427-
"Unsupported guest sys_reg access at: %lx [%08lx]\n",
3428-
*vcpu_pc(vcpu), *vcpu_cpsr(vcpu));
3429-
kvm_inject_undefined(vcpu);
3430-
}
3431-
return false;
3432-
}
3433-
3434-
static int emulate_sys_instr(struct kvm_vcpu *vcpu, struct sys_reg_params *p)
3435-
{
3436-
const struct sys_reg_desc *r;
3437-
3438-
/* Search from the system instruction table. */
3439-
r = find_reg(p, sys_insn_descs, ARRAY_SIZE(sys_insn_descs));
3416+
print_sys_reg_msg(params,
3417+
"Unsupported guest sys_reg access at: %lx [%08lx]\n",
3418+
*vcpu_pc(vcpu), *vcpu_cpsr(vcpu));
3419+
kvm_inject_undefined(vcpu);
34403420

3441-
if (likely(r)) {
3442-
perform_access(vcpu, p, r);
3443-
} else {
3444-
kvm_err("Unsupported guest sys instruction at: %lx\n",
3445-
*vcpu_pc(vcpu));
3446-
print_sys_reg_instr(p);
3447-
kvm_inject_undefined(vcpu);
3448-
}
3449-
return 1;
3421+
return false;
34503422
}
34513423

34523424
static void kvm_reset_id_regs(struct kvm_vcpu *vcpu)
@@ -3502,31 +3474,34 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
35023474
*/
35033475
int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
35043476
{
3477+
const struct sys_reg_desc *desc = NULL;
35053478
struct sys_reg_params params;
35063479
unsigned long esr = kvm_vcpu_get_esr(vcpu);
35073480
int Rt = kvm_vcpu_sys_get_rt(vcpu);
3481+
int sr_idx;
35083482

35093483
trace_kvm_handle_sys_reg(esr);
35103484

3511-
if (__check_nv_sr_forward(vcpu))
3485+
if (__check_nv_sr_forward(vcpu, &sr_idx))
35123486
return 1;
35133487

35143488
params = esr_sys64_to_params(esr);
35153489
params.regval = vcpu_get_reg(vcpu, Rt);
35163490

35173491
/* System registers have Op0=={2,3}, as per DDI487 J.a C5.1.2 */
3518-
if (params.Op0 == 2 || params.Op0 == 3) {
3519-
if (!emulate_sys_reg(vcpu, &params))
3520-
return 1;
3492+
if (params.Op0 == 2 || params.Op0 == 3)
3493+
desc = &sys_reg_descs[sr_idx];
3494+
else
3495+
desc = &sys_insn_descs[sr_idx];
35213496

3522-
if (!params.is_write)
3523-
vcpu_set_reg(vcpu, Rt, params.regval);
3497+
perform_access(vcpu, &params, desc);
35243498

3525-
return 1;
3526-
}
3499+
/* Read from system register? */
3500+
if (!params.is_write &&
3501+
(params.Op0 == 2 || params.Op0 == 3))
3502+
vcpu_set_reg(vcpu, Rt, params.regval);
35273503

3528-
/* Hints, PSTATE (Op0 == 0) and System instructions (Op0 == 1) */
3529-
return emulate_sys_instr(vcpu, &params);
3504+
return 1;
35303505
}
35313506

35323507
/******************************************************************************

0 commit comments

Comments
 (0)