Commit | Line | Data |
---|---|---|
8eb99267 MZ |
1 | /* |
2 | * Copyright (C) 2015 - ARM Ltd | |
3 | * Author: Marc Zyngier <marc.zyngier@arm.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include <linux/compiler.h> | |
19 | #include <linux/kvm_host.h> | |
20 | ||
9d8415d6 | 21 | #include <asm/kvm_asm.h> |
13720a56 | 22 | #include <asm/kvm_hyp.h> |
8eb99267 MZ |
23 | |
24 | #define read_debug(r,n) read_sysreg(r##n##_el1) | |
25 | #define write_debug(v,r,n) write_sysreg(v, r##n##_el1) | |
26 | ||
27 | #define save_debug(ptr,reg,nr) \ | |
28 | switch (nr) { \ | |
29 | case 15: ptr[15] = read_debug(reg, 15); \ | |
30 | case 14: ptr[14] = read_debug(reg, 14); \ | |
31 | case 13: ptr[13] = read_debug(reg, 13); \ | |
32 | case 12: ptr[12] = read_debug(reg, 12); \ | |
33 | case 11: ptr[11] = read_debug(reg, 11); \ | |
34 | case 10: ptr[10] = read_debug(reg, 10); \ | |
35 | case 9: ptr[9] = read_debug(reg, 9); \ | |
36 | case 8: ptr[8] = read_debug(reg, 8); \ | |
37 | case 7: ptr[7] = read_debug(reg, 7); \ | |
38 | case 6: ptr[6] = read_debug(reg, 6); \ | |
39 | case 5: ptr[5] = read_debug(reg, 5); \ | |
40 | case 4: ptr[4] = read_debug(reg, 4); \ | |
41 | case 3: ptr[3] = read_debug(reg, 3); \ | |
42 | case 2: ptr[2] = read_debug(reg, 2); \ | |
43 | case 1: ptr[1] = read_debug(reg, 1); \ | |
44 | default: ptr[0] = read_debug(reg, 0); \ | |
45 | } | |
46 | ||
47 | #define restore_debug(ptr,reg,nr) \ | |
48 | switch (nr) { \ | |
49 | case 15: write_debug(ptr[15], reg, 15); \ | |
50 | case 14: write_debug(ptr[14], reg, 14); \ | |
51 | case 13: write_debug(ptr[13], reg, 13); \ | |
52 | case 12: write_debug(ptr[12], reg, 12); \ | |
53 | case 11: write_debug(ptr[11], reg, 11); \ | |
54 | case 10: write_debug(ptr[10], reg, 10); \ | |
55 | case 9: write_debug(ptr[9], reg, 9); \ | |
56 | case 8: write_debug(ptr[8], reg, 8); \ | |
57 | case 7: write_debug(ptr[7], reg, 7); \ | |
58 | case 6: write_debug(ptr[6], reg, 6); \ | |
59 | case 5: write_debug(ptr[5], reg, 5); \ | |
60 | case 4: write_debug(ptr[4], reg, 4); \ | |
61 | case 3: write_debug(ptr[3], reg, 3); \ | |
62 | case 2: write_debug(ptr[2], reg, 2); \ | |
63 | case 1: write_debug(ptr[1], reg, 1); \ | |
64 | default: write_debug(ptr[0], reg, 0); \ | |
65 | } | |
66 | ||
67 | void __hyp_text __debug_save_state(struct kvm_vcpu *vcpu, | |
68 | struct kvm_guest_debug_arch *dbg, | |
69 | struct kvm_cpu_context *ctxt) | |
70 | { | |
71 | u64 aa64dfr0; | |
72 | int brps, wrps; | |
73 | ||
74 | if (!(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY)) | |
75 | return; | |
76 | ||
77 | aa64dfr0 = read_sysreg(id_aa64dfr0_el1); | |
78 | brps = (aa64dfr0 >> 12) & 0xf; | |
79 | wrps = (aa64dfr0 >> 20) & 0xf; | |
80 | ||
81 | save_debug(dbg->dbg_bcr, dbgbcr, brps); | |
82 | save_debug(dbg->dbg_bvr, dbgbvr, brps); | |
83 | save_debug(dbg->dbg_wcr, dbgwcr, wrps); | |
84 | save_debug(dbg->dbg_wvr, dbgwvr, wrps); | |
85 | ||
86 | ctxt->sys_regs[MDCCINT_EL1] = read_sysreg(mdccint_el1); | |
87 | } | |
88 | ||
89 | void __hyp_text __debug_restore_state(struct kvm_vcpu *vcpu, | |
90 | struct kvm_guest_debug_arch *dbg, | |
91 | struct kvm_cpu_context *ctxt) | |
92 | { | |
93 | u64 aa64dfr0; | |
94 | int brps, wrps; | |
95 | ||
96 | if (!(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY)) | |
97 | return; | |
98 | ||
99 | aa64dfr0 = read_sysreg(id_aa64dfr0_el1); | |
100 | ||
101 | brps = (aa64dfr0 >> 12) & 0xf; | |
102 | wrps = (aa64dfr0 >> 20) & 0xf; | |
103 | ||
104 | restore_debug(dbg->dbg_bcr, dbgbcr, brps); | |
105 | restore_debug(dbg->dbg_bvr, dbgbvr, brps); | |
106 | restore_debug(dbg->dbg_wcr, dbgwcr, wrps); | |
107 | restore_debug(dbg->dbg_wvr, dbgwvr, wrps); | |
108 | ||
109 | write_sysreg(ctxt->sys_regs[MDCCINT_EL1], mdccint_el1); | |
110 | } | |
111 | ||
112 | void __hyp_text __debug_cond_save_host_state(struct kvm_vcpu *vcpu) | |
113 | { | |
114 | /* If any of KDE, MDE or KVM_ARM64_DEBUG_DIRTY is set, perform | |
115 | * a full save/restore cycle. */ | |
116 | if ((vcpu->arch.ctxt.sys_regs[MDSCR_EL1] & DBG_MDSCR_KDE) || | |
117 | (vcpu->arch.ctxt.sys_regs[MDSCR_EL1] & DBG_MDSCR_MDE)) | |
118 | vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY; | |
119 | ||
120 | __debug_save_state(vcpu, &vcpu->arch.host_debug_state, | |
121 | kern_hyp_va(vcpu->arch.host_cpu_context)); | |
122 | } | |
123 | ||
124 | void __hyp_text __debug_cond_restore_host_state(struct kvm_vcpu *vcpu) | |
125 | { | |
126 | __debug_restore_state(vcpu, &vcpu->arch.host_debug_state, | |
127 | kern_hyp_va(vcpu->arch.host_cpu_context)); | |
128 | ||
129 | if (vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY) | |
130 | vcpu->arch.debug_flags &= ~KVM_ARM64_DEBUG_DIRTY; | |
131 | } | |
132 | ||
3ffa75cd | 133 | static u32 __hyp_text __debug_read_mdcr_el2(void) |
8eb99267 MZ |
134 | { |
135 | return read_sysreg(mdcr_el2); | |
136 | } | |
044ac37d | 137 | |
3ffa75cd | 138 | __alias(__debug_read_mdcr_el2) u32 __kvm_get_mdcr_el2(void); |