blob: 906606dc4e2cf4b2fbd07a6d18de1672b09e9b55 [file] [log] [blame]
Marc Zyngier6d6ec202015-10-19 18:02:48 +01001/*
2 * Copyright (C) 2012-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
Marc Zyngier9d8415d2015-10-25 19:57:11 +000021#include <asm/kvm_asm.h>
Christoffer Dalle72341c2017-12-13 22:56:48 +010022#include <asm/kvm_emulate.h>
Marc Zyngier13720a52016-01-28 13:44:07 +000023#include <asm/kvm_hyp.h>
Marc Zyngier6d6ec202015-10-19 18:02:48 +010024
Marc Zyngier9c6c3562015-10-28 12:39:38 +000025/*
26 * Non-VHE: Both host and guest must save everything.
27 *
Christoffer Dall060701f2016-03-15 21:41:55 +010028 * VHE: Host must save tpidr*_el0, mdscr_el1, sp_el0,
James Morse1f742672018-01-08 15:38:07 +000029 * and guest must save everything.
Marc Zyngier9c6c3562015-10-28 12:39:38 +000030 */
31
32static void __hyp_text __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
33{
Marc Zyngier4c47eb12016-07-19 13:56:54 +010034 ctxt->sys_regs[MDSCR_EL1] = read_sysreg(mdscr_el1);
Christoffer Dall060701f2016-03-15 21:41:55 +010035
36 /*
37 * The host arm64 Linux uses sp_el0 to point to 'current' and it must
38 * therefore be saved/restored on every entry/exit to/from the guest.
39 */
Marc Zyngier9c6c3562015-10-28 12:39:38 +000040 ctxt->gp_regs.regs.sp = read_sysreg(sp_el0);
Marc Zyngier9c6c3562015-10-28 12:39:38 +000041}
42
Christoffer Dall060701f2016-03-15 21:41:55 +010043static void __hyp_text __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
44{
45 ctxt->sys_regs[TPIDR_EL0] = read_sysreg(tpidr_el0);
46 ctxt->sys_regs[TPIDRRO_EL0] = read_sysreg(tpidrro_el0);
47}
48
49static void __hyp_text __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
Marc Zyngier6d6ec202015-10-19 18:02:48 +010050{
51 ctxt->sys_regs[MPIDR_EL1] = read_sysreg(vmpidr_el2);
52 ctxt->sys_regs[CSSELR_EL1] = read_sysreg(csselr_el1);
Marc Zyngier094f8232015-10-28 12:56:25 +000053 ctxt->sys_regs[SCTLR_EL1] = read_sysreg_el1(sctlr);
Christoffer Dall060701f2016-03-15 21:41:55 +010054 ctxt->sys_regs[ACTLR_EL1] = read_sysreg(actlr_el1);
Marc Zyngier094f8232015-10-28 12:56:25 +000055 ctxt->sys_regs[CPACR_EL1] = read_sysreg_el1(cpacr);
56 ctxt->sys_regs[TTBR0_EL1] = read_sysreg_el1(ttbr0);
57 ctxt->sys_regs[TTBR1_EL1] = read_sysreg_el1(ttbr1);
58 ctxt->sys_regs[TCR_EL1] = read_sysreg_el1(tcr);
59 ctxt->sys_regs[ESR_EL1] = read_sysreg_el1(esr);
60 ctxt->sys_regs[AFSR0_EL1] = read_sysreg_el1(afsr0);
61 ctxt->sys_regs[AFSR1_EL1] = read_sysreg_el1(afsr1);
62 ctxt->sys_regs[FAR_EL1] = read_sysreg_el1(far);
63 ctxt->sys_regs[MAIR_EL1] = read_sysreg_el1(mair);
64 ctxt->sys_regs[VBAR_EL1] = read_sysreg_el1(vbar);
65 ctxt->sys_regs[CONTEXTIDR_EL1] = read_sysreg_el1(contextidr);
66 ctxt->sys_regs[AMAIR_EL1] = read_sysreg_el1(amair);
67 ctxt->sys_regs[CNTKCTL_EL1] = read_sysreg_el1(cntkctl);
Marc Zyngier6d6ec202015-10-19 18:02:48 +010068 ctxt->sys_regs[PAR_EL1] = read_sysreg(par_el1);
James Morse1f742672018-01-08 15:38:07 +000069 ctxt->sys_regs[TPIDR_EL1] = read_sysreg(tpidr_el1);
Marc Zyngier6d6ec202015-10-19 18:02:48 +010070
Marc Zyngier6d6ec202015-10-19 18:02:48 +010071 ctxt->gp_regs.sp_el1 = read_sysreg(sp_el1);
Marc Zyngier094f8232015-10-28 12:56:25 +000072 ctxt->gp_regs.elr_el1 = read_sysreg_el1(elr);
73 ctxt->gp_regs.spsr[KVM_SPSR_EL1]= read_sysreg_el1(spsr);
Christoffer Dall0c389d92017-10-10 22:54:57 +020074}
75
76static void __hyp_text __sysreg_save_el2_return_state(struct kvm_cpu_context *ctxt)
77{
James Morse1f742672018-01-08 15:38:07 +000078 ctxt->gp_regs.regs.pc = read_sysreg_el2(elr);
79 ctxt->gp_regs.regs.pstate = read_sysreg_el2(spsr);
James Morsec773ae22018-01-15 19:39:02 +000080
81 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
82 ctxt->sys_regs[DISR_EL1] = read_sysreg_s(SYS_VDISR_EL2);
Marc Zyngier6d6ec202015-10-19 18:02:48 +010083}
84
Christoffer Dall4cdecab2017-10-10 22:40:13 +020085void __hyp_text __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt)
Christoffer Dallf8374532017-10-10 22:19:31 +020086{
87 __sysreg_save_el1_state(ctxt);
88 __sysreg_save_common_state(ctxt);
89 __sysreg_save_user_state(ctxt);
Christoffer Dall0c389d92017-10-10 22:54:57 +020090 __sysreg_save_el2_return_state(ctxt);
Christoffer Dallf8374532017-10-10 22:19:31 +020091}
92
93void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt)
94{
95 __sysreg_save_common_state(ctxt);
96 __sysreg_save_user_state(ctxt);
97}
98
99void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt)
Marc Zyngieredef5282015-10-28 12:17:35 +0000100{
Christoffer Dall060701f2016-03-15 21:41:55 +0100101 __sysreg_save_el1_state(ctxt);
Marc Zyngier9c6c3562015-10-28 12:39:38 +0000102 __sysreg_save_common_state(ctxt);
Christoffer Dall060701f2016-03-15 21:41:55 +0100103 __sysreg_save_user_state(ctxt);
Christoffer Dall0c389d92017-10-10 22:54:57 +0200104 __sysreg_save_el2_return_state(ctxt);
Marc Zyngier9c6c3562015-10-28 12:39:38 +0000105}
106
107static void __hyp_text __sysreg_restore_common_state(struct kvm_cpu_context *ctxt)
108{
Marc Zyngier4c47eb12016-07-19 13:56:54 +0100109 write_sysreg(ctxt->sys_regs[MDSCR_EL1], mdscr_el1);
Christoffer Dall060701f2016-03-15 21:41:55 +0100110
111 /*
112 * The host arm64 Linux uses sp_el0 to point to 'current' and it must
113 * therefore be saved/restored on every entry/exit to/from the guest.
114 */
Marc Zyngier9c6c3562015-10-28 12:39:38 +0000115 write_sysreg(ctxt->gp_regs.regs.sp, sp_el0);
Marc Zyngieredef5282015-10-28 12:17:35 +0000116}
117
Christoffer Dall060701f2016-03-15 21:41:55 +0100118static void __hyp_text __sysreg_restore_user_state(struct kvm_cpu_context *ctxt)
119{
120 write_sysreg(ctxt->sys_regs[TPIDR_EL0], tpidr_el0);
121 write_sysreg(ctxt->sys_regs[TPIDRRO_EL0], tpidrro_el0);
122}
123
124static void __hyp_text __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100125{
Marc Zyngier094f8232015-10-28 12:56:25 +0000126 write_sysreg(ctxt->sys_regs[MPIDR_EL1], vmpidr_el2);
127 write_sysreg(ctxt->sys_regs[CSSELR_EL1], csselr_el1);
128 write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], sctlr);
Christoffer Dall060701f2016-03-15 21:41:55 +0100129 write_sysreg(ctxt->sys_regs[ACTLR_EL1], actlr_el1);
Marc Zyngier094f8232015-10-28 12:56:25 +0000130 write_sysreg_el1(ctxt->sys_regs[CPACR_EL1], cpacr);
131 write_sysreg_el1(ctxt->sys_regs[TTBR0_EL1], ttbr0);
132 write_sysreg_el1(ctxt->sys_regs[TTBR1_EL1], ttbr1);
133 write_sysreg_el1(ctxt->sys_regs[TCR_EL1], tcr);
134 write_sysreg_el1(ctxt->sys_regs[ESR_EL1], esr);
135 write_sysreg_el1(ctxt->sys_regs[AFSR0_EL1], afsr0);
136 write_sysreg_el1(ctxt->sys_regs[AFSR1_EL1], afsr1);
137 write_sysreg_el1(ctxt->sys_regs[FAR_EL1], far);
138 write_sysreg_el1(ctxt->sys_regs[MAIR_EL1], mair);
139 write_sysreg_el1(ctxt->sys_regs[VBAR_EL1], vbar);
140 write_sysreg_el1(ctxt->sys_regs[CONTEXTIDR_EL1],contextidr);
141 write_sysreg_el1(ctxt->sys_regs[AMAIR_EL1], amair);
142 write_sysreg_el1(ctxt->sys_regs[CNTKCTL_EL1], cntkctl);
143 write_sysreg(ctxt->sys_regs[PAR_EL1], par_el1);
James Morse1f742672018-01-08 15:38:07 +0000144 write_sysreg(ctxt->sys_regs[TPIDR_EL1], tpidr_el1);
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100145
Marc Zyngier094f8232015-10-28 12:56:25 +0000146 write_sysreg(ctxt->gp_regs.sp_el1, sp_el1);
147 write_sysreg_el1(ctxt->gp_regs.elr_el1, elr);
148 write_sysreg_el1(ctxt->gp_regs.spsr[KVM_SPSR_EL1],spsr);
Christoffer Dall0c389d92017-10-10 22:54:57 +0200149}
150
151static void __hyp_text
152__sysreg_restore_el2_return_state(struct kvm_cpu_context *ctxt)
153{
James Morse1f742672018-01-08 15:38:07 +0000154 write_sysreg_el2(ctxt->gp_regs.regs.pc, elr);
155 write_sysreg_el2(ctxt->gp_regs.regs.pstate, spsr);
James Morsec773ae22018-01-15 19:39:02 +0000156
157 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
158 write_sysreg_s(ctxt->sys_regs[DISR_EL1], SYS_VDISR_EL2);
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100159}
Marc Zyngierc209ec82015-10-19 19:28:29 +0100160
Christoffer Dall4cdecab2017-10-10 22:40:13 +0200161void __hyp_text __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt)
Christoffer Dallf8374532017-10-10 22:19:31 +0200162{
163 __sysreg_restore_el1_state(ctxt);
164 __sysreg_restore_common_state(ctxt);
165 __sysreg_restore_user_state(ctxt);
Christoffer Dall0c389d92017-10-10 22:54:57 +0200166 __sysreg_restore_el2_return_state(ctxt);
Christoffer Dallf8374532017-10-10 22:19:31 +0200167}
168
169void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt)
170{
171 __sysreg_restore_common_state(ctxt);
172 __sysreg_restore_user_state(ctxt);
173}
174
175void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt)
Marc Zyngieredef5282015-10-28 12:17:35 +0000176{
Christoffer Dall060701f2016-03-15 21:41:55 +0100177 __sysreg_restore_el1_state(ctxt);
Marc Zyngier9c6c3562015-10-28 12:39:38 +0000178 __sysreg_restore_common_state(ctxt);
Christoffer Dall060701f2016-03-15 21:41:55 +0100179 __sysreg_restore_user_state(ctxt);
Christoffer Dall0c389d92017-10-10 22:54:57 +0200180 __sysreg_restore_el2_return_state(ctxt);
Marc Zyngieredef5282015-10-28 12:17:35 +0000181}
182
Marc Zyngierc209ec82015-10-19 19:28:29 +0100183void __hyp_text __sysreg32_save_state(struct kvm_vcpu *vcpu)
184{
185 u64 *spsr, *sysreg;
186
Christoffer Dalle72341c2017-12-13 22:56:48 +0100187 if (!vcpu_el1_is_32bit(vcpu))
Marc Zyngierc209ec82015-10-19 19:28:29 +0100188 return;
189
190 spsr = vcpu->arch.ctxt.gp_regs.spsr;
191 sysreg = vcpu->arch.ctxt.sys_regs;
192
193 spsr[KVM_SPSR_ABT] = read_sysreg(spsr_abt);
194 spsr[KVM_SPSR_UND] = read_sysreg(spsr_und);
195 spsr[KVM_SPSR_IRQ] = read_sysreg(spsr_irq);
196 spsr[KVM_SPSR_FIQ] = read_sysreg(spsr_fiq);
197
198 sysreg[DACR32_EL2] = read_sysreg(dacr32_el2);
199 sysreg[IFSR32_EL2] = read_sysreg(ifsr32_el2);
200
Marc Zyngierc13d1682015-10-26 08:34:09 +0000201 if (__fpsimd_enabled())
Marc Zyngierc209ec82015-10-19 19:28:29 +0100202 sysreg[FPEXC32_EL2] = read_sysreg(fpexc32_el2);
203
204 if (vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY)
205 sysreg[DBGVCR32_EL2] = read_sysreg(dbgvcr32_el2);
206}
207
208void __hyp_text __sysreg32_restore_state(struct kvm_vcpu *vcpu)
209{
210 u64 *spsr, *sysreg;
211
Christoffer Dalle72341c2017-12-13 22:56:48 +0100212 if (!vcpu_el1_is_32bit(vcpu))
Marc Zyngierc209ec82015-10-19 19:28:29 +0100213 return;
214
215 spsr = vcpu->arch.ctxt.gp_regs.spsr;
216 sysreg = vcpu->arch.ctxt.sys_regs;
217
218 write_sysreg(spsr[KVM_SPSR_ABT], spsr_abt);
219 write_sysreg(spsr[KVM_SPSR_UND], spsr_und);
220 write_sysreg(spsr[KVM_SPSR_IRQ], spsr_irq);
221 write_sysreg(spsr[KVM_SPSR_FIQ], spsr_fiq);
222
223 write_sysreg(sysreg[DACR32_EL2], dacr32_el2);
224 write_sysreg(sysreg[IFSR32_EL2], ifsr32_el2);
225
226 if (vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY)
227 write_sysreg(sysreg[DBGVCR32_EL2], dbgvcr32_el2);
228}
Christoffer Dall4464e212017-10-08 17:01:56 +0200229
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200230/**
231 * kvm_vcpu_load_sysregs - Load guest system registers to the physical CPU
232 *
233 * @vcpu: The VCPU pointer
234 *
235 * Load system registers that do not affect the host's execution, for
236 * example EL1 system registers on a VHE system where the host kernel
237 * runs at EL2. This function is called from KVM's vcpu_load() function
238 * and loading system register state early avoids having to load them on
239 * every entry to the VM.
240 */
241void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu)
242{
243}
244
245/**
246 * kvm_vcpu_put_sysregs - Restore host system registers to the physical CPU
247 *
248 * @vcpu: The VCPU pointer
249 *
250 * Save guest system registers that do not affect the host's execution, for
251 * example EL1 system registers on a VHE system where the host kernel
252 * runs at EL2. This function is called from KVM's vcpu_put() function
253 * and deferring saving system register state until we're no longer running the
254 * VCPU avoids having to save them on every exit from the VM.
255 */
256void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu)
257{
258}
259
Christoffer Dall4464e212017-10-08 17:01:56 +0200260void __hyp_text __kvm_set_tpidr_el2(u64 tpidr_el2)
261{
262 asm("msr tpidr_el2, %0": : "r" (tpidr_el2));
263}