1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012-2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #ifndef __ARM64_KVM_HYP_SYSREG_SR_H__
8 #define __ARM64_KVM_HYP_SYSREG_SR_H__
10 #include <linux/compiler.h>
11 #include <linux/kvm_host.h>
13 #include <asm/kprobes.h>
14 #include <asm/kvm_asm.h>
15 #include <asm/kvm_emulate.h>
16 #include <asm/kvm_hyp.h>
18 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt)
20 ctxt_sys_reg(ctxt, MDSCR_EL1) = read_sysreg(mdscr_el1);
23 static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)
25 ctxt_sys_reg(ctxt, TPIDR_EL0) = read_sysreg(tpidr_el0);
26 ctxt_sys_reg(ctxt, TPIDRRO_EL0) = read_sysreg(tpidrro_el0);
29 static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)
31 ctxt_sys_reg(ctxt, CSSELR_EL1) = read_sysreg(csselr_el1);
32 ctxt_sys_reg(ctxt, SCTLR_EL1) = read_sysreg_el1(SYS_SCTLR);
33 ctxt_sys_reg(ctxt, CPACR_EL1) = read_sysreg_el1(SYS_CPACR);
34 ctxt_sys_reg(ctxt, TTBR0_EL1) = read_sysreg_el1(SYS_TTBR0);
35 ctxt_sys_reg(ctxt, TTBR1_EL1) = read_sysreg_el1(SYS_TTBR1);
36 ctxt_sys_reg(ctxt, TCR_EL1) = read_sysreg_el1(SYS_TCR);
37 ctxt_sys_reg(ctxt, ESR_EL1) = read_sysreg_el1(SYS_ESR);
38 ctxt_sys_reg(ctxt, AFSR0_EL1) = read_sysreg_el1(SYS_AFSR0);
39 ctxt_sys_reg(ctxt, AFSR1_EL1) = read_sysreg_el1(SYS_AFSR1);
40 ctxt_sys_reg(ctxt, FAR_EL1) = read_sysreg_el1(SYS_FAR);
41 ctxt_sys_reg(ctxt, MAIR_EL1) = read_sysreg_el1(SYS_MAIR);
42 ctxt_sys_reg(ctxt, VBAR_EL1) = read_sysreg_el1(SYS_VBAR);
43 ctxt_sys_reg(ctxt, CONTEXTIDR_EL1) = read_sysreg_el1(SYS_CONTEXTIDR);
44 ctxt_sys_reg(ctxt, AMAIR_EL1) = read_sysreg_el1(SYS_AMAIR);
45 ctxt_sys_reg(ctxt, CNTKCTL_EL1) = read_sysreg_el1(SYS_CNTKCTL);
46 ctxt_sys_reg(ctxt, PAR_EL1) = read_sysreg(par_el1);
47 ctxt_sys_reg(ctxt, TPIDR_EL1) = read_sysreg(tpidr_el1);
49 ctxt_sys_reg(ctxt, SP_EL1) = read_sysreg(sp_el1);
50 ctxt_sys_reg(ctxt, ELR_EL1) = read_sysreg_el1(SYS_ELR);
51 ctxt_sys_reg(ctxt, SPSR_EL1) = read_sysreg_el1(SYS_SPSR);
54 static inline void __sysreg_save_el2_return_state(struct kvm_cpu_context *ctxt)
56 ctxt->regs.pc = read_sysreg_el2(SYS_ELR);
57 ctxt->regs.pstate = read_sysreg_el2(SYS_SPSR);
59 if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN))
60 ctxt_sys_reg(ctxt, DISR_EL1) = read_sysreg_s(SYS_VDISR_EL2);
63 static inline void __sysreg_restore_common_state(struct kvm_cpu_context *ctxt)
65 write_sysreg(ctxt_sys_reg(ctxt, MDSCR_EL1), mdscr_el1);
68 static inline void __sysreg_restore_user_state(struct kvm_cpu_context *ctxt)
70 write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL0), tpidr_el0);
71 write_sysreg(ctxt_sys_reg(ctxt, TPIDRRO_EL0), tpidrro_el0);
74 static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt)
76 write_sysreg(ctxt_sys_reg(ctxt, MPIDR_EL1), vmpidr_el2);
77 write_sysreg(ctxt_sys_reg(ctxt, CSSELR_EL1), csselr_el1);
80 !cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
81 write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR);
82 write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1), SYS_TCR);
83 } else if (!ctxt->__hyp_running_vcpu) {
85 * Must only be done for guest registers, hence the context
86 * test. We're coming from the host, so SCTLR.M is already
87 * set. Pairs with nVHE's __activate_traps().
89 write_sysreg_el1((ctxt_sys_reg(ctxt, TCR_EL1) |
90 TCR_EPD1_MASK | TCR_EPD0_MASK),
95 write_sysreg_el1(ctxt_sys_reg(ctxt, CPACR_EL1), SYS_CPACR);
96 write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR0_EL1), SYS_TTBR0);
97 write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR1_EL1), SYS_TTBR1);
98 write_sysreg_el1(ctxt_sys_reg(ctxt, ESR_EL1), SYS_ESR);
99 write_sysreg_el1(ctxt_sys_reg(ctxt, AFSR0_EL1), SYS_AFSR0);
100 write_sysreg_el1(ctxt_sys_reg(ctxt, AFSR1_EL1), SYS_AFSR1);
101 write_sysreg_el1(ctxt_sys_reg(ctxt, FAR_EL1), SYS_FAR);
102 write_sysreg_el1(ctxt_sys_reg(ctxt, MAIR_EL1), SYS_MAIR);
103 write_sysreg_el1(ctxt_sys_reg(ctxt, VBAR_EL1), SYS_VBAR);
104 write_sysreg_el1(ctxt_sys_reg(ctxt, CONTEXTIDR_EL1), SYS_CONTEXTIDR);
105 write_sysreg_el1(ctxt_sys_reg(ctxt, AMAIR_EL1), SYS_AMAIR);
106 write_sysreg_el1(ctxt_sys_reg(ctxt, CNTKCTL_EL1), SYS_CNTKCTL);
107 write_sysreg(ctxt_sys_reg(ctxt, PAR_EL1), par_el1);
108 write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL1), tpidr_el1);
111 cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT) &&
112 ctxt->__hyp_running_vcpu) {
114 * Must only be done for host registers, hence the context
115 * test. Pairs with nVHE's __deactivate_traps().
119 * At this stage, and thanks to the above isb(), S2 is
120 * deconfigured and disabled. We can now restore the host's
121 * S1 configuration: SCTLR, and only then TCR.
123 write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR);
125 write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1), SYS_TCR);
128 write_sysreg(ctxt_sys_reg(ctxt, SP_EL1), sp_el1);
129 write_sysreg_el1(ctxt_sys_reg(ctxt, ELR_EL1), SYS_ELR);
130 write_sysreg_el1(ctxt_sys_reg(ctxt, SPSR_EL1), SYS_SPSR);
133 static inline void __sysreg_restore_el2_return_state(struct kvm_cpu_context *ctxt)
135 u64 pstate = ctxt->regs.pstate;
136 u64 mode = pstate & PSR_AA32_MODE_MASK;
139 * Safety check to ensure we're setting the CPU up to enter the guest
140 * in a less privileged mode.
142 * If we are attempting a return to EL2 or higher in AArch64 state,
143 * program SPSR_EL2 with M=EL2h and the IL bit set which ensures that
144 * we'll take an illegal exception state exception immediately after
145 * the ERET to the guest. Attempts to return to AArch32 Hyp will
146 * result in an illegal exception return because EL2's execution state
147 * is determined by SCR_EL3.RW.
149 if (!(mode & PSR_MODE32_BIT) && mode >= PSR_MODE_EL2t)
150 pstate = PSR_MODE_EL2h | PSR_IL_BIT;
152 write_sysreg_el2(ctxt->regs.pc, SYS_ELR);
153 write_sysreg_el2(pstate, SYS_SPSR);
155 if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN))
156 write_sysreg_s(ctxt_sys_reg(ctxt, DISR_EL1), SYS_VDISR_EL2);
159 static inline void __sysreg32_save_state(struct kvm_vcpu *vcpu)
161 if (!vcpu_el1_is_32bit(vcpu))
164 vcpu->arch.ctxt.spsr_abt = read_sysreg(spsr_abt);
165 vcpu->arch.ctxt.spsr_und = read_sysreg(spsr_und);
166 vcpu->arch.ctxt.spsr_irq = read_sysreg(spsr_irq);
167 vcpu->arch.ctxt.spsr_fiq = read_sysreg(spsr_fiq);
169 __vcpu_sys_reg(vcpu, DACR32_EL2) = read_sysreg(dacr32_el2);
170 __vcpu_sys_reg(vcpu, IFSR32_EL2) = read_sysreg(ifsr32_el2);
172 if (has_vhe() || vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY)
173 __vcpu_sys_reg(vcpu, DBGVCR32_EL2) = read_sysreg(dbgvcr32_el2);
176 static inline void __sysreg32_restore_state(struct kvm_vcpu *vcpu)
178 if (!vcpu_el1_is_32bit(vcpu))
181 write_sysreg(vcpu->arch.ctxt.spsr_abt, spsr_abt);
182 write_sysreg(vcpu->arch.ctxt.spsr_und, spsr_und);
183 write_sysreg(vcpu->arch.ctxt.spsr_irq, spsr_irq);
184 write_sysreg(vcpu->arch.ctxt.spsr_fiq, spsr_fiq);
186 write_sysreg(__vcpu_sys_reg(vcpu, DACR32_EL2), dacr32_el2);
187 write_sysreg(__vcpu_sys_reg(vcpu, IFSR32_EL2), ifsr32_el2);
189 if (has_vhe() || vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY)
190 write_sysreg(__vcpu_sys_reg(vcpu, DBGVCR32_EL2), dbgvcr32_el2);
193 #endif /* __ARM64_KVM_HYP_SYSREG_SR_H__ */