]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: Select debug state to save/restore based on debug owner
authorOliver Upton <oliver.upton@linux.dev>
Thu, 19 Dec 2024 22:41:05 +0000 (14:41 -0800)
committerMarc Zyngier <maz@kernel.org>
Fri, 20 Dec 2024 09:01:25 +0000 (09:01 +0000)
Select the set of debug registers to use based on the owner rather than
relying on debug_ptr. Besides the code cleanup, this allows us to
eliminate a couple instances kern_hyp_va() as well.

Tested-by: James Clark <james.clark@linaro.org>
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
Link: https://lore.kernel.org/r/20241219224116.3941496-9-oliver.upton@linux.dev
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/include/asm/kvm_host.h
arch/arm64/kvm/hyp/include/hyp/debug-sr.h

index f65b30bab0abf63ac980e5022ae359f71e3cfd49..5ef1b2f69e89d91b2d3fec3cd331ea4e0ae5ff99 100644 (file)
@@ -1357,6 +1357,8 @@ void kvm_debug_set_guest_ownership(struct kvm_vcpu *vcpu);
 #define kvm_vcpu_os_lock_enabled(vcpu)         \
        (!!(__vcpu_sys_reg(vcpu, OSLSR_EL1) & OSLSR_EL1_OSLK))
 
+#define kvm_debug_regs_in_use(vcpu)            \
+       ((vcpu)->arch.debug_owner != VCPU_DEBUG_FREE)
 #define kvm_host_owns_debug_regs(vcpu)         \
        ((vcpu)->arch.debug_owner == VCPU_DEBUG_HOST_OWNED)
 
index d00093699aaf1fb8c886b14687867e49f85b0da3..9e3051225039cfcf12b1c1cac71df9d870983d4b 100644 (file)
        default:        write_debug(ptr[0], reg, 0);                    \
        }
 
+static struct kvm_guest_debug_arch *__vcpu_debug_regs(struct kvm_vcpu *vcpu)
+{
+       switch (vcpu->arch.debug_owner) {
+       case VCPU_DEBUG_FREE:
+               WARN_ON_ONCE(1);
+               fallthrough;
+       case VCPU_DEBUG_GUEST_OWNED:
+               return &vcpu->arch.vcpu_debug_state;
+       case VCPU_DEBUG_HOST_OWNED:
+               return &vcpu->arch.external_debug_state;
+       }
+
+       return NULL;
+}
+
 static void __debug_save_state(struct kvm_guest_debug_arch *dbg,
                               struct kvm_cpu_context *ctxt)
 {
@@ -132,13 +147,13 @@ static inline void __debug_switch_to_guest_common(struct kvm_vcpu *vcpu)
        struct kvm_guest_debug_arch *host_dbg;
        struct kvm_guest_debug_arch *guest_dbg;
 
-       if (!vcpu_get_flag(vcpu, DEBUG_DIRTY))
+       if (!kvm_debug_regs_in_use(vcpu))
                return;
 
        host_ctxt = host_data_ptr(host_ctxt);
        guest_ctxt = &vcpu->arch.ctxt;
        host_dbg = host_data_ptr(host_debug_state.regs);
-       guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
+       guest_dbg = __vcpu_debug_regs(vcpu);
 
        __debug_save_state(host_dbg, host_ctxt);
        __debug_restore_state(guest_dbg, guest_ctxt);
@@ -151,13 +166,13 @@ static inline void __debug_switch_to_host_common(struct kvm_vcpu *vcpu)
        struct kvm_guest_debug_arch *host_dbg;
        struct kvm_guest_debug_arch *guest_dbg;
 
-       if (!vcpu_get_flag(vcpu, DEBUG_DIRTY))
+       if (!kvm_debug_regs_in_use(vcpu))
                return;
 
        host_ctxt = host_data_ptr(host_ctxt);
        guest_ctxt = &vcpu->arch.ctxt;
        host_dbg = host_data_ptr(host_debug_state.regs);
-       guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
+       guest_dbg = __vcpu_debug_regs(vcpu);
 
        __debug_save_state(guest_dbg, guest_ctxt);
        __debug_restore_state(host_dbg, host_ctxt);