Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012-2015 - ARM Ltd |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/kvm_host.h> |
| 9 | |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 10 | #include <asm/kprobes.h> |
Marc Zyngier | 9d8415d | 2015-10-25 19:57:11 +0000 | [diff] [blame] | 11 | #include <asm/kvm_asm.h> |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 12 | #include <asm/kvm_emulate.h> |
Marc Zyngier | 13720a5 | 2016-01-28 13:44:07 +0000 | [diff] [blame] | 13 | #include <asm/kvm_hyp.h> |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 14 | |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 15 | /* |
| 16 | * Non-VHE: Both host and guest must save everything. |
| 17 | * |
Marc Zyngier | 6e97798 | 2020-04-24 14:24:34 +0100 | [diff] [blame] | 18 | * VHE: Host and guest must save mdscr_el1 and sp_el0 (and the PC and |
| 19 | * pstate, which are handled as part of the el2 return state) on every |
| 20 | * switch (sp_el0 is being dealt with in the assembly code). |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 21 | * tpidr_el0 and tpidrro_el0 only need to be switched when going |
| 22 | * to host userspace or a different VCPU. EL1 registers only need to be |
| 23 | * switched when potentially going to run a different VCPU. The latter two |
| 24 | * classes are handled as part of kvm_arch_vcpu_load and kvm_arch_vcpu_put. |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | static void __hyp_text __sysreg_save_common_state(struct kvm_cpu_context *ctxt) |
| 28 | { |
Marc Zyngier | 4c47eb1 | 2016-07-19 13:56:54 +0100 | [diff] [blame] | 29 | ctxt->sys_regs[MDSCR_EL1] = read_sysreg(mdscr_el1); |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Christoffer Dall | 060701f | 2016-03-15 21:41:55 +0100 | [diff] [blame] | 32 | static void __hyp_text __sysreg_save_user_state(struct kvm_cpu_context *ctxt) |
| 33 | { |
| 34 | ctxt->sys_regs[TPIDR_EL0] = read_sysreg(tpidr_el0); |
| 35 | ctxt->sys_regs[TPIDRRO_EL0] = read_sysreg(tpidrro_el0); |
| 36 | } |
| 37 | |
| 38 | static void __hyp_text __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 39 | { |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 40 | ctxt->sys_regs[CSSELR_EL1] = read_sysreg(csselr_el1); |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 41 | ctxt->sys_regs[SCTLR_EL1] = read_sysreg_el1(SYS_SCTLR); |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 42 | ctxt->sys_regs[CPACR_EL1] = read_sysreg_el1(SYS_CPACR); |
| 43 | ctxt->sys_regs[TTBR0_EL1] = read_sysreg_el1(SYS_TTBR0); |
| 44 | ctxt->sys_regs[TTBR1_EL1] = read_sysreg_el1(SYS_TTBR1); |
| 45 | ctxt->sys_regs[TCR_EL1] = read_sysreg_el1(SYS_TCR); |
| 46 | ctxt->sys_regs[ESR_EL1] = read_sysreg_el1(SYS_ESR); |
| 47 | ctxt->sys_regs[AFSR0_EL1] = read_sysreg_el1(SYS_AFSR0); |
| 48 | ctxt->sys_regs[AFSR1_EL1] = read_sysreg_el1(SYS_AFSR1); |
| 49 | ctxt->sys_regs[FAR_EL1] = read_sysreg_el1(SYS_FAR); |
| 50 | ctxt->sys_regs[MAIR_EL1] = read_sysreg_el1(SYS_MAIR); |
| 51 | ctxt->sys_regs[VBAR_EL1] = read_sysreg_el1(SYS_VBAR); |
| 52 | ctxt->sys_regs[CONTEXTIDR_EL1] = read_sysreg_el1(SYS_CONTEXTIDR); |
| 53 | ctxt->sys_regs[AMAIR_EL1] = read_sysreg_el1(SYS_AMAIR); |
| 54 | ctxt->sys_regs[CNTKCTL_EL1] = read_sysreg_el1(SYS_CNTKCTL); |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 55 | ctxt->sys_regs[PAR_EL1] = read_sysreg(par_el1); |
James Morse | 1f74267 | 2018-01-08 15:38:07 +0000 | [diff] [blame] | 56 | ctxt->sys_regs[TPIDR_EL1] = read_sysreg(tpidr_el1); |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 57 | |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 58 | ctxt->gp_regs.sp_el1 = read_sysreg(sp_el1); |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 59 | ctxt->gp_regs.elr_el1 = read_sysreg_el1(SYS_ELR); |
| 60 | ctxt->gp_regs.spsr[KVM_SPSR_EL1]= read_sysreg_el1(SYS_SPSR); |
Christoffer Dall | 0c389d9 | 2017-10-10 22:54:57 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void __hyp_text __sysreg_save_el2_return_state(struct kvm_cpu_context *ctxt) |
| 64 | { |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 65 | ctxt->gp_regs.regs.pc = read_sysreg_el2(SYS_ELR); |
| 66 | ctxt->gp_regs.regs.pstate = read_sysreg_el2(SYS_SPSR); |
James Morse | c773ae2 | 2018-01-15 19:39:02 +0000 | [diff] [blame] | 67 | |
Mark Rutland | b5475d8 | 2020-02-21 14:50:22 +0000 | [diff] [blame] | 68 | if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN)) |
James Morse | c773ae2 | 2018-01-15 19:39:02 +0000 | [diff] [blame] | 69 | ctxt->sys_regs[DISR_EL1] = read_sysreg_s(SYS_VDISR_EL2); |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 72 | void __hyp_text __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt) |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 73 | { |
| 74 | __sysreg_save_el1_state(ctxt); |
| 75 | __sysreg_save_common_state(ctxt); |
| 76 | __sysreg_save_user_state(ctxt); |
Christoffer Dall | 0c389d9 | 2017-10-10 22:54:57 +0200 | [diff] [blame] | 77 | __sysreg_save_el2_return_state(ctxt); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt) |
| 81 | { |
| 82 | __sysreg_save_common_state(ctxt); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 83 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 84 | NOKPROBE_SYMBOL(sysreg_save_host_state_vhe); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 85 | |
| 86 | void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt) |
Marc Zyngier | edef528 | 2015-10-28 12:17:35 +0000 | [diff] [blame] | 87 | { |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 88 | __sysreg_save_common_state(ctxt); |
Christoffer Dall | 0c389d9 | 2017-10-10 22:54:57 +0200 | [diff] [blame] | 89 | __sysreg_save_el2_return_state(ctxt); |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 90 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 91 | NOKPROBE_SYMBOL(sysreg_save_guest_state_vhe); |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 92 | |
| 93 | static void __hyp_text __sysreg_restore_common_state(struct kvm_cpu_context *ctxt) |
| 94 | { |
Marc Zyngier | 4c47eb1 | 2016-07-19 13:56:54 +0100 | [diff] [blame] | 95 | write_sysreg(ctxt->sys_regs[MDSCR_EL1], mdscr_el1); |
Marc Zyngier | edef528 | 2015-10-28 12:17:35 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Christoffer Dall | 060701f | 2016-03-15 21:41:55 +0100 | [diff] [blame] | 98 | static void __hyp_text __sysreg_restore_user_state(struct kvm_cpu_context *ctxt) |
| 99 | { |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 100 | write_sysreg(ctxt->sys_regs[TPIDR_EL0], tpidr_el0); |
| 101 | write_sysreg(ctxt->sys_regs[TPIDRRO_EL0], tpidrro_el0); |
Christoffer Dall | 060701f | 2016-03-15 21:41:55 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void __hyp_text __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt) |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 105 | { |
Marc Zyngier | 094f823 | 2015-10-28 12:56:25 +0000 | [diff] [blame] | 106 | write_sysreg(ctxt->sys_regs[MPIDR_EL1], vmpidr_el2); |
| 107 | write_sysreg(ctxt->sys_regs[CSSELR_EL1], csselr_el1); |
Marc Zyngier | bd22755 | 2019-07-30 11:15:31 +0100 | [diff] [blame] | 108 | |
Andrew Scull | 02ab1f5 | 2020-05-04 10:48:58 +0100 | [diff] [blame] | 109 | if (has_vhe() || |
| 110 | !cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) { |
Marc Zyngier | bd22755 | 2019-07-30 11:15:31 +0100 | [diff] [blame] | 111 | write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], SYS_SCTLR); |
| 112 | write_sysreg_el1(ctxt->sys_regs[TCR_EL1], SYS_TCR); |
| 113 | } else if (!ctxt->__hyp_running_vcpu) { |
| 114 | /* |
| 115 | * Must only be done for guest registers, hence the context |
| 116 | * test. We're coming from the host, so SCTLR.M is already |
| 117 | * set. Pairs with __activate_traps_nvhe(). |
| 118 | */ |
| 119 | write_sysreg_el1((ctxt->sys_regs[TCR_EL1] | |
| 120 | TCR_EPD1_MASK | TCR_EPD0_MASK), |
| 121 | SYS_TCR); |
| 122 | isb(); |
| 123 | } |
| 124 | |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 125 | write_sysreg_el1(ctxt->sys_regs[CPACR_EL1], SYS_CPACR); |
| 126 | write_sysreg_el1(ctxt->sys_regs[TTBR0_EL1], SYS_TTBR0); |
| 127 | write_sysreg_el1(ctxt->sys_regs[TTBR1_EL1], SYS_TTBR1); |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 128 | write_sysreg_el1(ctxt->sys_regs[ESR_EL1], SYS_ESR); |
| 129 | write_sysreg_el1(ctxt->sys_regs[AFSR0_EL1], SYS_AFSR0); |
| 130 | write_sysreg_el1(ctxt->sys_regs[AFSR1_EL1], SYS_AFSR1); |
| 131 | write_sysreg_el1(ctxt->sys_regs[FAR_EL1], SYS_FAR); |
| 132 | write_sysreg_el1(ctxt->sys_regs[MAIR_EL1], SYS_MAIR); |
| 133 | write_sysreg_el1(ctxt->sys_regs[VBAR_EL1], SYS_VBAR); |
| 134 | write_sysreg_el1(ctxt->sys_regs[CONTEXTIDR_EL1],SYS_CONTEXTIDR); |
| 135 | write_sysreg_el1(ctxt->sys_regs[AMAIR_EL1], SYS_AMAIR); |
| 136 | write_sysreg_el1(ctxt->sys_regs[CNTKCTL_EL1], SYS_CNTKCTL); |
Marc Zyngier | 094f823 | 2015-10-28 12:56:25 +0000 | [diff] [blame] | 137 | write_sysreg(ctxt->sys_regs[PAR_EL1], par_el1); |
James Morse | 1f74267 | 2018-01-08 15:38:07 +0000 | [diff] [blame] | 138 | write_sysreg(ctxt->sys_regs[TPIDR_EL1], tpidr_el1); |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 139 | |
Andrew Scull | 02ab1f5 | 2020-05-04 10:48:58 +0100 | [diff] [blame] | 140 | if (!has_vhe() && |
| 141 | cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT) && |
Marc Zyngier | bd22755 | 2019-07-30 11:15:31 +0100 | [diff] [blame] | 142 | ctxt->__hyp_running_vcpu) { |
| 143 | /* |
| 144 | * Must only be done for host registers, hence the context |
| 145 | * test. Pairs with __deactivate_traps_nvhe(). |
| 146 | */ |
| 147 | isb(); |
| 148 | /* |
| 149 | * At this stage, and thanks to the above isb(), S2 is |
| 150 | * deconfigured and disabled. We can now restore the host's |
| 151 | * S1 configuration: SCTLR, and only then TCR. |
| 152 | */ |
| 153 | write_sysreg_el1(ctxt->sys_regs[SCTLR_EL1], SYS_SCTLR); |
| 154 | isb(); |
| 155 | write_sysreg_el1(ctxt->sys_regs[TCR_EL1], SYS_TCR); |
| 156 | } |
| 157 | |
Marc Zyngier | 094f823 | 2015-10-28 12:56:25 +0000 | [diff] [blame] | 158 | write_sysreg(ctxt->gp_regs.sp_el1, sp_el1); |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 159 | write_sysreg_el1(ctxt->gp_regs.elr_el1, SYS_ELR); |
| 160 | write_sysreg_el1(ctxt->gp_regs.spsr[KVM_SPSR_EL1],SYS_SPSR); |
Christoffer Dall | 0c389d9 | 2017-10-10 22:54:57 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void __hyp_text |
| 164 | __sysreg_restore_el2_return_state(struct kvm_cpu_context *ctxt) |
| 165 | { |
Christoffer Dall | e4e11cc | 2018-10-17 20:21:16 +0200 | [diff] [blame] | 166 | u64 pstate = ctxt->gp_regs.regs.pstate; |
| 167 | u64 mode = pstate & PSR_AA32_MODE_MASK; |
| 168 | |
| 169 | /* |
| 170 | * Safety check to ensure we're setting the CPU up to enter the guest |
| 171 | * in a less privileged mode. |
| 172 | * |
| 173 | * If we are attempting a return to EL2 or higher in AArch64 state, |
| 174 | * program SPSR_EL2 with M=EL2h and the IL bit set which ensures that |
| 175 | * we'll take an illegal exception state exception immediately after |
| 176 | * the ERET to the guest. Attempts to return to AArch32 Hyp will |
| 177 | * result in an illegal exception return because EL2's execution state |
| 178 | * is determined by SCR_EL3.RW. |
| 179 | */ |
| 180 | if (!(mode & PSR_MODE32_BIT) && mode >= PSR_MODE_EL2t) |
| 181 | pstate = PSR_MODE_EL2h | PSR_IL_BIT; |
| 182 | |
Dave Martin | fdec2a9 | 2019-04-06 11:29:40 +0100 | [diff] [blame] | 183 | write_sysreg_el2(ctxt->gp_regs.regs.pc, SYS_ELR); |
| 184 | write_sysreg_el2(pstate, SYS_SPSR); |
James Morse | c773ae2 | 2018-01-15 19:39:02 +0000 | [diff] [blame] | 185 | |
Mark Rutland | b5475d8 | 2020-02-21 14:50:22 +0000 | [diff] [blame] | 186 | if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN)) |
James Morse | c773ae2 | 2018-01-15 19:39:02 +0000 | [diff] [blame] | 187 | write_sysreg_s(ctxt->sys_regs[DISR_EL1], SYS_VDISR_EL2); |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 188 | } |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 189 | |
Christoffer Dall | 4cdecab | 2017-10-10 22:40:13 +0200 | [diff] [blame] | 190 | void __hyp_text __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt) |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 191 | { |
| 192 | __sysreg_restore_el1_state(ctxt); |
| 193 | __sysreg_restore_common_state(ctxt); |
| 194 | __sysreg_restore_user_state(ctxt); |
Christoffer Dall | 0c389d9 | 2017-10-10 22:54:57 +0200 | [diff] [blame] | 195 | __sysreg_restore_el2_return_state(ctxt); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt) |
| 199 | { |
| 200 | __sysreg_restore_common_state(ctxt); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 201 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 202 | NOKPROBE_SYMBOL(sysreg_restore_host_state_vhe); |
Christoffer Dall | f837453 | 2017-10-10 22:19:31 +0200 | [diff] [blame] | 203 | |
| 204 | void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt) |
Marc Zyngier | edef528 | 2015-10-28 12:17:35 +0000 | [diff] [blame] | 205 | { |
Marc Zyngier | 9c6c356 | 2015-10-28 12:39:38 +0000 | [diff] [blame] | 206 | __sysreg_restore_common_state(ctxt); |
Christoffer Dall | 0c389d9 | 2017-10-10 22:54:57 +0200 | [diff] [blame] | 207 | __sysreg_restore_el2_return_state(ctxt); |
Marc Zyngier | edef528 | 2015-10-28 12:17:35 +0000 | [diff] [blame] | 208 | } |
James Morse | 7d82602 | 2019-01-24 16:32:54 +0000 | [diff] [blame] | 209 | NOKPROBE_SYMBOL(sysreg_restore_guest_state_vhe); |
Marc Zyngier | edef528 | 2015-10-28 12:17:35 +0000 | [diff] [blame] | 210 | |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 211 | void __hyp_text __sysreg32_save_state(struct kvm_vcpu *vcpu) |
| 212 | { |
| 213 | u64 *spsr, *sysreg; |
| 214 | |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 215 | if (!vcpu_el1_is_32bit(vcpu)) |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 216 | return; |
| 217 | |
| 218 | spsr = vcpu->arch.ctxt.gp_regs.spsr; |
| 219 | sysreg = vcpu->arch.ctxt.sys_regs; |
| 220 | |
| 221 | spsr[KVM_SPSR_ABT] = read_sysreg(spsr_abt); |
| 222 | spsr[KVM_SPSR_UND] = read_sysreg(spsr_und); |
| 223 | spsr[KVM_SPSR_IRQ] = read_sysreg(spsr_irq); |
| 224 | spsr[KVM_SPSR_FIQ] = read_sysreg(spsr_fiq); |
| 225 | |
| 226 | sysreg[DACR32_EL2] = read_sysreg(dacr32_el2); |
| 227 | sysreg[IFSR32_EL2] = read_sysreg(ifsr32_el2); |
| 228 | |
Dave Martin | fa89d31c | 2018-05-08 14:47:23 +0100 | [diff] [blame] | 229 | if (has_vhe() || vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY) |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 230 | sysreg[DBGVCR32_EL2] = read_sysreg(dbgvcr32_el2); |
| 231 | } |
| 232 | |
| 233 | void __hyp_text __sysreg32_restore_state(struct kvm_vcpu *vcpu) |
| 234 | { |
| 235 | u64 *spsr, *sysreg; |
| 236 | |
Christoffer Dall | e72341c | 2017-12-13 22:56:48 +0100 | [diff] [blame] | 237 | if (!vcpu_el1_is_32bit(vcpu)) |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 238 | return; |
| 239 | |
| 240 | spsr = vcpu->arch.ctxt.gp_regs.spsr; |
| 241 | sysreg = vcpu->arch.ctxt.sys_regs; |
| 242 | |
| 243 | write_sysreg(spsr[KVM_SPSR_ABT], spsr_abt); |
| 244 | write_sysreg(spsr[KVM_SPSR_UND], spsr_und); |
| 245 | write_sysreg(spsr[KVM_SPSR_IRQ], spsr_irq); |
| 246 | write_sysreg(spsr[KVM_SPSR_FIQ], spsr_fiq); |
| 247 | |
| 248 | write_sysreg(sysreg[DACR32_EL2], dacr32_el2); |
| 249 | write_sysreg(sysreg[IFSR32_EL2], ifsr32_el2); |
| 250 | |
Dave Martin | fa89d31c | 2018-05-08 14:47:23 +0100 | [diff] [blame] | 251 | if (has_vhe() || vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY) |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 252 | write_sysreg(sysreg[DBGVCR32_EL2], dbgvcr32_el2); |
| 253 | } |
Christoffer Dall | 4464e21 | 2017-10-08 17:01:56 +0200 | [diff] [blame] | 254 | |
Christoffer Dall | bc192ce | 2017-10-10 10:21:18 +0200 | [diff] [blame] | 255 | /** |
| 256 | * kvm_vcpu_load_sysregs - Load guest system registers to the physical CPU |
| 257 | * |
| 258 | * @vcpu: The VCPU pointer |
| 259 | * |
| 260 | * Load system registers that do not affect the host's execution, for |
| 261 | * example EL1 system registers on a VHE system where the host kernel |
| 262 | * runs at EL2. This function is called from KVM's vcpu_load() function |
| 263 | * and loading system register state early avoids having to load them on |
| 264 | * every entry to the VM. |
| 265 | */ |
| 266 | void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu) |
| 267 | { |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 268 | struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt; |
Marc Zyngier | 07da1ff | 2020-06-05 14:08:13 +0100 | [diff] [blame] | 269 | struct kvm_cpu_context *host_ctxt; |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 270 | |
| 271 | if (!has_vhe()) |
| 272 | return; |
| 273 | |
Marc Zyngier | 07da1ff | 2020-06-05 14:08:13 +0100 | [diff] [blame] | 274 | host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt; |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 275 | __sysreg_save_user_state(host_ctxt); |
| 276 | |
Christoffer Dall | b9f8ca4 | 2017-12-27 22:12:12 +0100 | [diff] [blame] | 277 | /* |
| 278 | * Load guest EL1 and user state |
| 279 | * |
| 280 | * We must restore the 32-bit state before the sysregs, thanks |
| 281 | * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72). |
| 282 | */ |
| 283 | __sysreg32_restore_state(vcpu); |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 284 | __sysreg_restore_user_state(guest_ctxt); |
| 285 | __sysreg_restore_el1_state(guest_ctxt); |
| 286 | |
| 287 | vcpu->arch.sysregs_loaded_on_cpu = true; |
Christoffer Dall | a246562 | 2017-08-04 13:47:18 +0200 | [diff] [blame] | 288 | |
| 289 | activate_traps_vhe_load(vcpu); |
Christoffer Dall | bc192ce | 2017-10-10 10:21:18 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * kvm_vcpu_put_sysregs - Restore host system registers to the physical CPU |
| 294 | * |
| 295 | * @vcpu: The VCPU pointer |
| 296 | * |
| 297 | * Save guest system registers that do not affect the host's execution, for |
| 298 | * example EL1 system registers on a VHE system where the host kernel |
| 299 | * runs at EL2. This function is called from KVM's vcpu_put() function |
| 300 | * and deferring saving system register state until we're no longer running the |
| 301 | * VCPU avoids having to save them on every exit from the VM. |
| 302 | */ |
| 303 | void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu) |
| 304 | { |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 305 | struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt; |
Marc Zyngier | 07da1ff | 2020-06-05 14:08:13 +0100 | [diff] [blame] | 306 | struct kvm_cpu_context *host_ctxt; |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 307 | |
| 308 | if (!has_vhe()) |
| 309 | return; |
| 310 | |
Marc Zyngier | 07da1ff | 2020-06-05 14:08:13 +0100 | [diff] [blame] | 311 | host_ctxt = &__hyp_this_cpu_ptr(kvm_host_data)->host_ctxt; |
Christoffer Dall | a246562 | 2017-08-04 13:47:18 +0200 | [diff] [blame] | 312 | deactivate_traps_vhe_put(); |
| 313 | |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 314 | __sysreg_save_el1_state(guest_ctxt); |
| 315 | __sysreg_save_user_state(guest_ctxt); |
Christoffer Dall | b9f8ca4 | 2017-12-27 22:12:12 +0100 | [diff] [blame] | 316 | __sysreg32_save_state(vcpu); |
Christoffer Dall | fc7563b | 2016-03-15 19:43:45 +0100 | [diff] [blame] | 317 | |
| 318 | /* Restore host user state */ |
| 319 | __sysreg_restore_user_state(host_ctxt); |
| 320 | |
| 321 | vcpu->arch.sysregs_loaded_on_cpu = false; |
Christoffer Dall | bc192ce | 2017-10-10 10:21:18 +0200 | [diff] [blame] | 322 | } |
Will Deacon | 7c36447 | 2018-08-08 16:10:54 +0100 | [diff] [blame] | 323 | |
| 324 | void __hyp_text __kvm_enable_ssbs(void) |
| 325 | { |
| 326 | u64 tmp; |
| 327 | |
| 328 | asm volatile( |
| 329 | "mrs %0, sctlr_el2\n" |
| 330 | "orr %0, %0, %1\n" |
| 331 | "msr sctlr_el2, %0" |
| 332 | : "=&r" (tmp) : "L" (SCTLR_ELx_DSSBS)); |
| 333 | } |