1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <linux/compiler.h>
8 #include <linux/kvm_host.h>
10 #include <asm/debug-monitors.h>
11 #include <asm/kvm_asm.h>
12 #include <asm/kvm_hyp.h>
13 #include <asm/kvm_mmu.h>
15 #define read_debug(r,n) read_sysreg(r##n##_el1)
16 #define write_debug(v,r,n) write_sysreg(v, r##n##_el1)
18 #define save_debug(ptr,reg,nr) \
20 case 15: ptr[15] = read_debug(reg, 15); \
22 case 14: ptr[14] = read_debug(reg, 14); \
24 case 13: ptr[13] = read_debug(reg, 13); \
26 case 12: ptr[12] = read_debug(reg, 12); \
28 case 11: ptr[11] = read_debug(reg, 11); \
30 case 10: ptr[10] = read_debug(reg, 10); \
32 case 9: ptr[9] = read_debug(reg, 9); \
34 case 8: ptr[8] = read_debug(reg, 8); \
36 case 7: ptr[7] = read_debug(reg, 7); \
38 case 6: ptr[6] = read_debug(reg, 6); \
40 case 5: ptr[5] = read_debug(reg, 5); \
42 case 4: ptr[4] = read_debug(reg, 4); \
44 case 3: ptr[3] = read_debug(reg, 3); \
46 case 2: ptr[2] = read_debug(reg, 2); \
48 case 1: ptr[1] = read_debug(reg, 1); \
50 default: ptr[0] = read_debug(reg, 0); \
53 #define restore_debug(ptr,reg,nr) \
55 case 15: write_debug(ptr[15], reg, 15); \
57 case 14: write_debug(ptr[14], reg, 14); \
59 case 13: write_debug(ptr[13], reg, 13); \
61 case 12: write_debug(ptr[12], reg, 12); \
63 case 11: write_debug(ptr[11], reg, 11); \
65 case 10: write_debug(ptr[10], reg, 10); \
67 case 9: write_debug(ptr[9], reg, 9); \
69 case 8: write_debug(ptr[8], reg, 8); \
71 case 7: write_debug(ptr[7], reg, 7); \
73 case 6: write_debug(ptr[6], reg, 6); \
75 case 5: write_debug(ptr[5], reg, 5); \
77 case 4: write_debug(ptr[4], reg, 4); \
79 case 3: write_debug(ptr[3], reg, 3); \
81 case 2: write_debug(ptr[2], reg, 2); \
83 case 1: write_debug(ptr[1], reg, 1); \
85 default: write_debug(ptr[0], reg, 0); \
88 static void __hyp_text
__debug_save_spe_nvhe(u64
*pmscr_el1
)
92 /* Clear pmscr in case of early return */
95 /* SPE present on this CPU? */
96 if (!cpuid_feature_extract_unsigned_field(read_sysreg(id_aa64dfr0_el1
),
97 ID_AA64DFR0_PMSVER_SHIFT
))
100 /* Yes; is it owned by EL3? */
101 reg
= read_sysreg_s(SYS_PMBIDR_EL1
);
102 if (reg
& BIT(SYS_PMBIDR_EL1_P_SHIFT
))
105 /* No; is the host actually using the thing? */
106 reg
= read_sysreg_s(SYS_PMBLIMITR_EL1
);
107 if (!(reg
& BIT(SYS_PMBLIMITR_EL1_E_SHIFT
)))
110 /* Yes; save the control register and disable data generation */
111 *pmscr_el1
= read_sysreg_s(SYS_PMSCR_EL1
);
112 write_sysreg_s(0, SYS_PMSCR_EL1
);
115 /* Now drain all buffered data to memory */
120 static void __hyp_text
__debug_restore_spe_nvhe(u64 pmscr_el1
)
125 /* The host page table is installed, but not yet synchronised */
128 /* Re-enable data generation */
129 write_sysreg_s(pmscr_el1
, SYS_PMSCR_EL1
);
132 static void __hyp_text
__debug_save_state(struct kvm_vcpu
*vcpu
,
133 struct kvm_guest_debug_arch
*dbg
,
134 struct kvm_cpu_context
*ctxt
)
139 aa64dfr0
= read_sysreg(id_aa64dfr0_el1
);
140 brps
= (aa64dfr0
>> 12) & 0xf;
141 wrps
= (aa64dfr0
>> 20) & 0xf;
143 save_debug(dbg
->dbg_bcr
, dbgbcr
, brps
);
144 save_debug(dbg
->dbg_bvr
, dbgbvr
, brps
);
145 save_debug(dbg
->dbg_wcr
, dbgwcr
, wrps
);
146 save_debug(dbg
->dbg_wvr
, dbgwvr
, wrps
);
148 ctxt
->sys_regs
[MDCCINT_EL1
] = read_sysreg(mdccint_el1
);
151 static void __hyp_text
__debug_restore_state(struct kvm_vcpu
*vcpu
,
152 struct kvm_guest_debug_arch
*dbg
,
153 struct kvm_cpu_context
*ctxt
)
158 aa64dfr0
= read_sysreg(id_aa64dfr0_el1
);
160 brps
= (aa64dfr0
>> 12) & 0xf;
161 wrps
= (aa64dfr0
>> 20) & 0xf;
163 restore_debug(dbg
->dbg_bcr
, dbgbcr
, brps
);
164 restore_debug(dbg
->dbg_bvr
, dbgbvr
, brps
);
165 restore_debug(dbg
->dbg_wcr
, dbgwcr
, wrps
);
166 restore_debug(dbg
->dbg_wvr
, dbgwvr
, wrps
);
168 write_sysreg(ctxt
->sys_regs
[MDCCINT_EL1
], mdccint_el1
);
171 void __hyp_text
__debug_switch_to_guest(struct kvm_vcpu
*vcpu
)
173 struct kvm_cpu_context
*host_ctxt
;
174 struct kvm_cpu_context
*guest_ctxt
;
175 struct kvm_guest_debug_arch
*host_dbg
;
176 struct kvm_guest_debug_arch
*guest_dbg
;
179 * Non-VHE: Disable and flush SPE data generation
180 * VHE: The vcpu can run, but it can't hide.
183 __debug_save_spe_nvhe(&vcpu
->arch
.host_debug_state
.pmscr_el1
);
185 if (!(vcpu
->arch
.flags
& KVM_ARM64_DEBUG_DIRTY
))
188 host_ctxt
= kern_hyp_va(vcpu
->arch
.host_cpu_context
);
189 guest_ctxt
= &vcpu
->arch
.ctxt
;
190 host_dbg
= &vcpu
->arch
.host_debug_state
.regs
;
191 guest_dbg
= kern_hyp_va(vcpu
->arch
.debug_ptr
);
193 __debug_save_state(vcpu
, host_dbg
, host_ctxt
);
194 __debug_restore_state(vcpu
, guest_dbg
, guest_ctxt
);
197 void __hyp_text
__debug_switch_to_host(struct kvm_vcpu
*vcpu
)
199 struct kvm_cpu_context
*host_ctxt
;
200 struct kvm_cpu_context
*guest_ctxt
;
201 struct kvm_guest_debug_arch
*host_dbg
;
202 struct kvm_guest_debug_arch
*guest_dbg
;
205 __debug_restore_spe_nvhe(vcpu
->arch
.host_debug_state
.pmscr_el1
);
207 if (!(vcpu
->arch
.flags
& KVM_ARM64_DEBUG_DIRTY
))
210 host_ctxt
= kern_hyp_va(vcpu
->arch
.host_cpu_context
);
211 guest_ctxt
= &vcpu
->arch
.ctxt
;
212 host_dbg
= &vcpu
->arch
.host_debug_state
.regs
;
213 guest_dbg
= kern_hyp_va(vcpu
->arch
.debug_ptr
);
215 __debug_save_state(vcpu
, guest_dbg
, guest_ctxt
);
216 __debug_restore_state(vcpu
, host_dbg
, host_ctxt
);
218 vcpu
->arch
.flags
&= ~KVM_ARM64_DEBUG_DIRTY
;
221 u32 __hyp_text
__kvm_get_mdcr_el2(void)
223 return read_sysreg(mdcr_el2
);