Skip to content

Commit f01c7ba

Browse files
author
Marc Zyngier
committed
Merge branch kvm-arm64/nv-debug into kvmarm-master/next
* kvm-arm64/nv-debug: : . : Fix handling of MDSCR_EL1 in NV context, which is unfortunately : mishandled by the architecture. Patches courtesy of Oliver Upton : (20250917203125.283116-2-oliver.upton@linux.dev) : . KVM: arm64: nv: Apply guest's MDCR traps in nested context KVM: arm64: nv: Trap debug registers when in hyp context Signed-off-by: Marc Zyngier <maz@kernel.org>
2 parents d9476fd + 3af1105 commit f01c7ba

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

arch/arm64/include/asm/kvm_nested.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ extern void check_nested_vcpu_requests(struct kvm_vcpu *vcpu);
8383
extern void kvm_nested_flush_hwstate(struct kvm_vcpu *vcpu);
8484
extern void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu);
8585

86+
extern void kvm_nested_setup_mdcr_el2(struct kvm_vcpu *vcpu);
87+
8688
struct kvm_s2_trans {
8789
phys_addr_t output;
8890
unsigned long block_size;

arch/arm64/kvm/debug.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
5656
if (!kvm_guest_owns_debug_regs(vcpu))
5757
vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
5858

59+
if (vcpu_has_nv(vcpu))
60+
kvm_nested_setup_mdcr_el2(vcpu);
61+
5962
/* Write MDCR_EL2 directly if we're already at EL2 */
6063
if (has_vhe())
6164
write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);

arch/arm64/kvm/nested.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,3 +1796,33 @@ void kvm_nested_sync_hwstate(struct kvm_vcpu *vcpu)
17961796
if (unlikely(vcpu_test_and_clear_flag(vcpu, NESTED_SERROR_PENDING)))
17971797
kvm_inject_serror_esr(vcpu, vcpu_get_vsesr(vcpu));
17981798
}
1799+
1800+
/*
1801+
* KVM unconditionally sets most of these traps anyway but use an allowlist
1802+
* to document the guest hypervisor traps that may take precedence and guard
1803+
* against future changes to the non-nested trap configuration.
1804+
*/
1805+
#define NV_MDCR_GUEST_INCLUDE (MDCR_EL2_TDE | \
1806+
MDCR_EL2_TDA | \
1807+
MDCR_EL2_TDRA | \
1808+
MDCR_EL2_TTRF | \
1809+
MDCR_EL2_TPMS | \
1810+
MDCR_EL2_TPM | \
1811+
MDCR_EL2_TPMCR | \
1812+
MDCR_EL2_TDCC | \
1813+
MDCR_EL2_TDOSA)
1814+
1815+
void kvm_nested_setup_mdcr_el2(struct kvm_vcpu *vcpu)
1816+
{
1817+
u64 guest_mdcr = __vcpu_sys_reg(vcpu, MDCR_EL2);
1818+
1819+
/*
1820+
* In yet another example where FEAT_NV2 is fscking broken, accesses
1821+
* to MDSCR_EL1 are redirected to the VNCR despite having an effect
1822+
* at EL2. Use a big hammer to apply sanity.
1823+
*/
1824+
if (is_hyp_ctxt(vcpu))
1825+
vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
1826+
else
1827+
vcpu->arch.mdcr_el2 |= (guest_mdcr & NV_MDCR_GUEST_INCLUDE);
1828+
}

0 commit comments

Comments
 (0)