blob: d35b3aa680abf676878bc0c47799c8451aca82ce [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);
James Morse1f742672018-01-08 15:38:07 +000074 ctxt->gp_regs.regs.pc = read_sysreg_el2(elr);
75 ctxt->gp_regs.regs.pstate = read_sysreg_el2(spsr);
James Morsec773ae22018-01-15 19:39:02 +000076
77 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
78 ctxt->sys_regs[DISR_EL1] = read_sysreg_s(SYS_VDISR_EL2);
Marc Zyngier6d6ec202015-10-19 18:02:48 +010079}
80
Christoffer Dall4cdecab2017-10-10 22:40:13 +020081void __hyp_text __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt)
Christoffer Dallf8374532017-10-10 22:19:31 +020082{
83 __sysreg_save_el1_state(ctxt);
84 __sysreg_save_common_state(ctxt);
85 __sysreg_save_user_state(ctxt);
86}
87
88void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt)
89{
90 __sysreg_save_common_state(ctxt);
91 __sysreg_save_user_state(ctxt);
92}
93
94void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt)
Marc Zyngieredef5282015-10-28 12:17:35 +000095{
Christoffer Dall060701f2016-03-15 21:41:55 +010096 __sysreg_save_el1_state(ctxt);
Marc Zyngier9c6c3562015-10-28 12:39:38 +000097 __sysreg_save_common_state(ctxt);
Christoffer Dall060701f2016-03-15 21:41:55 +010098 __sysreg_save_user_state(ctxt);
Marc Zyngier9c6c3562015-10-28 12:39:38 +000099}
100
101static void __hyp_text __sysreg_restore_common_state(struct kvm_cpu_context *ctxt)
102{
Marc Zyngier4c47eb12016-07-19 13:56:54 +0100103 write_sysreg(ctxt->sys_regs[MDSCR_EL1], mdscr_el1);
Christoffer Dall060701f2016-03-15 21:41:55 +0100104
105 /*
106 * The host arm64 Linux uses sp_el0 to point to 'current' and it must
107 * therefore be saved/restored on every entry/exit to/from the guest.
108 */
Marc Zyngier9c6c3562015-10-28 12:39:38 +0000109 write_sysreg(ctxt->gp_regs.regs.sp, sp_el0);
Marc Zyngieredef5282015-10-28 12:17:35 +0000110}
111
Christoffer Dall060701f2016-03-15 21:41:55 +0100112static void __hyp_text __sysreg_restore_user_state(struct kvm_cpu_context *ctxt)
113{
114 write_sysreg(ctxt->sys_regs[TPIDR_EL0], tpidr_el0);
115 write_sysreg(ctxt->sys_regs[TPIDRRO_EL0], tpidrro_el0);
116}
117
118static void __hyp_text __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100119{
Marc Zyngier094f8232015-10-28 12:56:25 +0000120 write_sysreg(ctxt->sys_regs[MPIDR_EL1], vmpidr_el2);
121 write_sysreg(ctxt->sys_regs[CSSELR_EL1], csselr_el1);
122 write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], sctlr);
Christoffer Dall060701f2016-03-15 21:41:55 +0100123 write_sysreg(ctxt->sys_regs[ACTLR_EL1], actlr_el1);
Marc Zyngier094f8232015-10-28 12:56:25 +0000124 write_sysreg_el1(ctxt->sys_regs[CPACR_EL1], cpacr);
125 write_sysreg_el1(ctxt->sys_regs[TTBR0_EL1], ttbr0);
126 write_sysreg_el1(ctxt->sys_regs[TTBR1_EL1], ttbr1);
127 write_sysreg_el1(ctxt->sys_regs[TCR_EL1], tcr);
128 write_sysreg_el1(ctxt->sys_regs[ESR_EL1], esr);
129 write_sysreg_el1(ctxt->sys_regs[AFSR0_EL1], afsr0);
130 write_sysreg_el1(ctxt->sys_regs[AFSR1_EL1], afsr1);
131 write_sysreg_el1(ctxt->sys_regs[FAR_EL1], far);
132 write_sysreg_el1(ctxt->sys_regs[MAIR_EL1], mair);
133 write_sysreg_el1(ctxt->sys_regs[VBAR_EL1], vbar);
134 write_sysreg_el1(ctxt->sys_regs[CONTEXTIDR_EL1],contextidr);
135 write_sysreg_el1(ctxt->sys_regs[AMAIR_EL1], amair);
136 write_sysreg_el1(ctxt->sys_regs[CNTKCTL_EL1], cntkctl);
137 write_sysreg(ctxt->sys_regs[PAR_EL1], par_el1);
James Morse1f742672018-01-08 15:38:07 +0000138 write_sysreg(ctxt->sys_regs[TPIDR_EL1], tpidr_el1);
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100139
Marc Zyngier094f8232015-10-28 12:56:25 +0000140 write_sysreg(ctxt->gp_regs.sp_el1, sp_el1);
141 write_sysreg_el1(ctxt->gp_regs.elr_el1, elr);
142 write_sysreg_el1(ctxt->gp_regs.spsr[KVM_SPSR_EL1],spsr);
James Morse1f742672018-01-08 15:38:07 +0000143 write_sysreg_el2(ctxt->gp_regs.regs.pc, elr);
144 write_sysreg_el2(ctxt->gp_regs.regs.pstate, spsr);
James Morsec773ae22018-01-15 19:39:02 +0000145
146 if (cpus_have_const_cap(ARM64_HAS_RAS_EXTN))
147 write_sysreg_s(ctxt->sys_regs[DISR_EL1], SYS_VDISR_EL2);
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100148}
Marc Zyngierc209ec82015-10-19 19:28:29 +0100149
Christoffer Dall4cdecab2017-10-10 22:40:13 +0200150void __hyp_text __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt)
Christoffer Dallf8374532017-10-10 22:19:31 +0200151{
152 __sysreg_restore_el1_state(ctxt);
153 __sysreg_restore_common_state(ctxt);
154 __sysreg_restore_user_state(ctxt);
155}
156
157void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt)
158{
159 __sysreg_restore_common_state(ctxt);
160 __sysreg_restore_user_state(ctxt);
161}
162
163void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt)
Marc Zyngieredef5282015-10-28 12:17:35 +0000164{
Christoffer Dall060701f2016-03-15 21:41:55 +0100165 __sysreg_restore_el1_state(ctxt);
Marc Zyngier9c6c3562015-10-28 12:39:38 +0000166 __sysreg_restore_common_state(ctxt);
Christoffer Dall060701f2016-03-15 21:41:55 +0100167 __sysreg_restore_user_state(ctxt);
Marc Zyngieredef5282015-10-28 12:17:35 +0000168}
169
Marc Zyngierc209ec82015-10-19 19:28:29 +0100170void __hyp_text __sysreg32_save_state(struct kvm_vcpu *vcpu)
171{
172 u64 *spsr, *sysreg;
173
Christoffer Dalle72341c2017-12-13 22:56:48 +0100174 if (!vcpu_el1_is_32bit(vcpu))
Marc Zyngierc209ec82015-10-19 19:28:29 +0100175 return;
176
177 spsr = vcpu->arch.ctxt.gp_regs.spsr;
178 sysreg = vcpu->arch.ctxt.sys_regs;
179
180 spsr[KVM_SPSR_ABT] = read_sysreg(spsr_abt);
181 spsr[KVM_SPSR_UND] = read_sysreg(spsr_und);
182 spsr[KVM_SPSR_IRQ] = read_sysreg(spsr_irq);
183 spsr[KVM_SPSR_FIQ] = read_sysreg(spsr_fiq);
184
185 sysreg[DACR32_EL2] = read_sysreg(dacr32_el2);
186 sysreg[IFSR32_EL2] = read_sysreg(ifsr32_el2);
187
Marc Zyngierc13d1682015-10-26 08:34:09 +0000188 if (__fpsimd_enabled())
Marc Zyngierc209ec82015-10-19 19:28:29 +0100189 sysreg[FPEXC32_EL2] = read_sysreg(fpexc32_el2);
190
191 if (vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY)
192 sysreg[DBGVCR32_EL2] = read_sysreg(dbgvcr32_el2);
193}
194
195void __hyp_text __sysreg32_restore_state(struct kvm_vcpu *vcpu)
196{
197 u64 *spsr, *sysreg;
198
Christoffer Dalle72341c2017-12-13 22:56:48 +0100199 if (!vcpu_el1_is_32bit(vcpu))
Marc Zyngierc209ec82015-10-19 19:28:29 +0100200 return;
201
202 spsr = vcpu->arch.ctxt.gp_regs.spsr;
203 sysreg = vcpu->arch.ctxt.sys_regs;
204
205 write_sysreg(spsr[KVM_SPSR_ABT], spsr_abt);
206 write_sysreg(spsr[KVM_SPSR_UND], spsr_und);
207 write_sysreg(spsr[KVM_SPSR_IRQ], spsr_irq);
208 write_sysreg(spsr[KVM_SPSR_FIQ], spsr_fiq);
209
210 write_sysreg(sysreg[DACR32_EL2], dacr32_el2);
211 write_sysreg(sysreg[IFSR32_EL2], ifsr32_el2);
212
213 if (vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY)
214 write_sysreg(sysreg[DBGVCR32_EL2], dbgvcr32_el2);
215}
Christoffer Dall4464e212017-10-08 17:01:56 +0200216
Christoffer Dallbc192ce2017-10-10 10:21:18 +0200217/**
218 * kvm_vcpu_load_sysregs - Load guest system registers to the physical CPU
219 *
220 * @vcpu: The VCPU pointer
221 *
222 * Load system registers that do not affect the host's execution, for
223 * example EL1 system registers on a VHE system where the host kernel
224 * runs at EL2. This function is called from KVM's vcpu_load() function
225 * and loading system register state early avoids having to load them on
226 * every entry to the VM.
227 */
228void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu)
229{
230}
231
232/**
233 * kvm_vcpu_put_sysregs - Restore host system registers to the physical CPU
234 *
235 * @vcpu: The VCPU pointer
236 *
237 * Save guest system registers that do not affect the host's execution, for
238 * example EL1 system registers on a VHE system where the host kernel
239 * runs at EL2. This function is called from KVM's vcpu_put() function
240 * and deferring saving system register state until we're no longer running the
241 * VCPU avoids having to save them on every exit from the VM.
242 */
243void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu)
244{
245}
246
Christoffer Dall4464e212017-10-08 17:01:56 +0200247void __hyp_text __kvm_set_tpidr_el2(u64 tpidr_el2)
248{
249 asm("msr tpidr_el2, %0": : "r" (tpidr_el2));
250}