1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <hyp/debug-sr.h>
9 #include <linux/compiler.h>
10 #include <linux/kvm_host.h>
12 #include <asm/debug-monitors.h>
13 #include <asm/kvm_asm.h>
14 #include <asm/kvm_hyp.h>
15 #include <asm/kvm_mmu.h>
17 static void __debug_save_spe(u64 *pmscr_el1)
21 /* Clear pmscr in case of early return */
25 * At this point, we know that this CPU implements
26 * SPE and is available to the host.
27 * Check if the host is actually using it ?
29 reg = read_sysreg_s(SYS_PMBLIMITR_EL1);
30 if (!(reg & BIT(SYS_PMBLIMITR_EL1_E_SHIFT)))
33 /* Yes; save the control register and disable data generation */
34 *pmscr_el1 = read_sysreg_s(SYS_PMSCR_EL1);
35 write_sysreg_s(0, SYS_PMSCR_EL1);
38 /* Now drain all buffered data to memory */
43 static void __debug_restore_spe(u64 pmscr_el1)
48 /* The host page table is installed, but not yet synchronised */
51 /* Re-enable data generation */
52 write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1);
55 static void __debug_save_trace(u64 *trfcr_el1)
59 /* Check if the TRBE is enabled */
60 if (!(read_sysreg_s(SYS_TRBLIMITR_EL1) & TRBLIMITR_ENABLE))
63 * Prohibit trace generation while we are in guest.
64 * Since access to TRFCR_EL1 is trapped, the guest can't
65 * modify the filtering set by the host.
67 *trfcr_el1 = read_sysreg_s(SYS_TRFCR_EL1);
68 write_sysreg_s(0, SYS_TRFCR_EL1);
70 /* Drain the trace buffer to memory */
75 static void __debug_restore_trace(u64 trfcr_el1)
80 /* Restore trace filter controls */
81 write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);
84 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
86 /* Disable and flush SPE data generation */
87 if (vcpu->arch.flags & KVM_ARM64_DEBUG_STATE_SAVE_SPE)
88 __debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
89 /* Disable and flush Self-Hosted Trace generation */
90 if (vcpu->arch.flags & KVM_ARM64_DEBUG_STATE_SAVE_TRBE)
91 __debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
94 void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
96 __debug_switch_to_guest_common(vcpu);
99 void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
101 if (vcpu->arch.flags & KVM_ARM64_DEBUG_STATE_SAVE_SPE)
102 __debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
103 if (vcpu->arch.flags & KVM_ARM64_DEBUG_STATE_SAVE_TRBE)
104 __debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
107 void __debug_switch_to_host(struct kvm_vcpu *vcpu)
109 __debug_switch_to_host_common(vcpu);
112 u32 __kvm_get_mdcr_el2(void)
114 return read_sysreg(mdcr_el2);