Marc Zyngier | c76a0a6 | 2015-10-21 10:09:49 +0100 | [diff] [blame] | 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 | #ifndef __ARM64_KVM_HYP_H__ |
| 19 | #define __ARM64_KVM_HYP_H__ |
| 20 | |
| 21 | #include <linux/compiler.h> |
| 22 | #include <linux/kvm_host.h> |
| 23 | #include <asm/kvm_mmu.h> |
| 24 | #include <asm/sysreg.h> |
| 25 | |
| 26 | #define __hyp_text __section(.hyp.text) notrace |
| 27 | |
Marc Zyngier | 915ccd1 | 2015-10-28 12:00:00 +0000 | [diff] [blame] | 28 | #define read_sysreg_elx(r,nvh,vh) \ |
| 29 | ({ \ |
| 30 | u64 reg; \ |
| 31 | asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\ |
Alex Matveev | 79b4e3c | 2017-11-14 15:06:17 -0800 | [diff] [blame] | 32 | DEFINE_MRS_S \ |
| 33 | "mrs_s %0, " __stringify(r##vh) "\n"\ |
| 34 | UNDEFINE_MRS_S, \ |
Marc Zyngier | 915ccd1 | 2015-10-28 12:00:00 +0000 | [diff] [blame] | 35 | ARM64_HAS_VIRT_HOST_EXTN) \ |
| 36 | : "=r" (reg)); \ |
| 37 | reg; \ |
| 38 | }) |
| 39 | |
| 40 | #define write_sysreg_elx(v,r,nvh,vh) \ |
| 41 | do { \ |
| 42 | u64 __val = (u64)(v); \ |
| 43 | asm volatile(ALTERNATIVE("msr " __stringify(r##nvh) ", %x0",\ |
Alex Matveev | 79b4e3c | 2017-11-14 15:06:17 -0800 | [diff] [blame] | 44 | DEFINE_MSR_S \ |
| 45 | "msr_s " __stringify(r##vh) ", %x0\n"\ |
| 46 | UNDEFINE_MSR_S, \ |
Marc Zyngier | 915ccd1 | 2015-10-28 12:00:00 +0000 | [diff] [blame] | 47 | ARM64_HAS_VIRT_HOST_EXTN) \ |
| 48 | : : "rZ" (__val)); \ |
| 49 | } while (0) |
| 50 | |
| 51 | /* |
| 52 | * Unified accessors for registers that have a different encoding |
| 53 | * between VHE and non-VHE. They must be specified without their "ELx" |
| 54 | * encoding. |
| 55 | */ |
| 56 | #define read_sysreg_el2(r) \ |
| 57 | ({ \ |
| 58 | u64 reg; \ |
| 59 | asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##_EL2),\ |
| 60 | "mrs %0, " __stringify(r##_EL1),\ |
| 61 | ARM64_HAS_VIRT_HOST_EXTN) \ |
| 62 | : "=r" (reg)); \ |
| 63 | reg; \ |
| 64 | }) |
| 65 | |
| 66 | #define write_sysreg_el2(v,r) \ |
| 67 | do { \ |
| 68 | u64 __val = (u64)(v); \ |
| 69 | asm volatile(ALTERNATIVE("msr " __stringify(r##_EL2) ", %x0",\ |
| 70 | "msr " __stringify(r##_EL1) ", %x0",\ |
| 71 | ARM64_HAS_VIRT_HOST_EXTN) \ |
| 72 | : : "rZ" (__val)); \ |
| 73 | } while (0) |
| 74 | |
| 75 | #define read_sysreg_el0(r) read_sysreg_elx(r, _EL0, _EL02) |
| 76 | #define write_sysreg_el0(v,r) write_sysreg_elx(v, r, _EL0, _EL02) |
| 77 | #define read_sysreg_el1(r) read_sysreg_elx(r, _EL1, _EL12) |
| 78 | #define write_sysreg_el1(v,r) write_sysreg_elx(v, r, _EL1, _EL12) |
| 79 | |
| 80 | /* The VHE specific system registers and their encoding */ |
| 81 | #define sctlr_EL12 sys_reg(3, 5, 1, 0, 0) |
| 82 | #define cpacr_EL12 sys_reg(3, 5, 1, 0, 2) |
| 83 | #define ttbr0_EL12 sys_reg(3, 5, 2, 0, 0) |
| 84 | #define ttbr1_EL12 sys_reg(3, 5, 2, 0, 1) |
| 85 | #define tcr_EL12 sys_reg(3, 5, 2, 0, 2) |
| 86 | #define afsr0_EL12 sys_reg(3, 5, 5, 1, 0) |
| 87 | #define afsr1_EL12 sys_reg(3, 5, 5, 1, 1) |
| 88 | #define esr_EL12 sys_reg(3, 5, 5, 2, 0) |
| 89 | #define far_EL12 sys_reg(3, 5, 6, 0, 0) |
| 90 | #define mair_EL12 sys_reg(3, 5, 10, 2, 0) |
| 91 | #define amair_EL12 sys_reg(3, 5, 10, 3, 0) |
| 92 | #define vbar_EL12 sys_reg(3, 5, 12, 0, 0) |
| 93 | #define contextidr_EL12 sys_reg(3, 5, 13, 0, 1) |
| 94 | #define cntkctl_EL12 sys_reg(3, 5, 14, 1, 0) |
| 95 | #define cntp_tval_EL02 sys_reg(3, 5, 14, 2, 0) |
| 96 | #define cntp_ctl_EL02 sys_reg(3, 5, 14, 2, 1) |
| 97 | #define cntp_cval_EL02 sys_reg(3, 5, 14, 2, 2) |
| 98 | #define cntv_tval_EL02 sys_reg(3, 5, 14, 3, 0) |
| 99 | #define cntv_ctl_EL02 sys_reg(3, 5, 14, 3, 1) |
| 100 | #define cntv_cval_EL02 sys_reg(3, 5, 14, 3, 2) |
| 101 | #define spsr_EL12 sys_reg(3, 5, 4, 0, 0) |
| 102 | #define elr_EL12 sys_reg(3, 5, 4, 0, 1) |
| 103 | |
Marc Zyngier | c1bf6e1 | 2015-10-28 08:45:37 +0000 | [diff] [blame] | 104 | /** |
| 105 | * hyp_alternate_select - Generates patchable code sequences that are |
| 106 | * used to switch between two implementations of a function, depending |
| 107 | * on the availability of a feature. |
| 108 | * |
| 109 | * @fname: a symbol name that will be defined as a function returning a |
| 110 | * function pointer whose type will match @orig and @alt |
| 111 | * @orig: A pointer to the default function, as returned by @fname when |
| 112 | * @cond doesn't hold |
| 113 | * @alt: A pointer to the alternate function, as returned by @fname |
| 114 | * when @cond holds |
| 115 | * @cond: a CPU feature (as described in asm/cpufeature.h) |
| 116 | */ |
| 117 | #define hyp_alternate_select(fname, orig, alt, cond) \ |
| 118 | typeof(orig) * __hyp_text fname(void) \ |
| 119 | { \ |
| 120 | typeof(alt) *val = orig; \ |
| 121 | asm volatile(ALTERNATIVE("nop \n", \ |
| 122 | "mov %0, %1 \n", \ |
| 123 | cond) \ |
| 124 | : "+r" (val) : "r" (alt)); \ |
| 125 | return val; \ |
| 126 | } |
| 127 | |
Marc Zyngier | 06282fd | 2015-10-19 15:50:37 +0100 | [diff] [blame] | 128 | void __vgic_v2_save_state(struct kvm_vcpu *vcpu); |
| 129 | void __vgic_v2_restore_state(struct kvm_vcpu *vcpu); |
Marc Zyngier | 3272f0d | 2016-09-06 14:02:17 +0100 | [diff] [blame] | 130 | int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu); |
Marc Zyngier | 06282fd | 2015-10-19 15:50:37 +0100 | [diff] [blame] | 131 | |
Marc Zyngier | f68d2b1 | 2015-10-19 15:50:58 +0100 | [diff] [blame] | 132 | void __vgic_v3_save_state(struct kvm_vcpu *vcpu); |
| 133 | void __vgic_v3_restore_state(struct kvm_vcpu *vcpu); |
| 134 | |
Marc Zyngier | 1431af3 | 2015-10-19 16:32:20 +0100 | [diff] [blame] | 135 | void __timer_save_state(struct kvm_vcpu *vcpu); |
| 136 | void __timer_restore_state(struct kvm_vcpu *vcpu); |
| 137 | |
Marc Zyngier | edef528 | 2015-10-28 12:17:35 +0000 | [diff] [blame] | 138 | void __sysreg_save_host_state(struct kvm_cpu_context *ctxt); |
| 139 | void __sysreg_restore_host_state(struct kvm_cpu_context *ctxt); |
| 140 | void __sysreg_save_guest_state(struct kvm_cpu_context *ctxt); |
| 141 | void __sysreg_restore_guest_state(struct kvm_cpu_context *ctxt); |
Marc Zyngier | c209ec8 | 2015-10-19 19:28:29 +0100 | [diff] [blame] | 142 | void __sysreg32_save_state(struct kvm_vcpu *vcpu); |
| 143 | void __sysreg32_restore_state(struct kvm_vcpu *vcpu); |
Marc Zyngier | 6d6ec20 | 2015-10-19 18:02:48 +0100 | [diff] [blame] | 144 | |
Marc Zyngier | 8eb9926 | 2015-10-19 21:02:46 +0100 | [diff] [blame] | 145 | void __debug_save_state(struct kvm_vcpu *vcpu, |
| 146 | struct kvm_guest_debug_arch *dbg, |
| 147 | struct kvm_cpu_context *ctxt); |
| 148 | void __debug_restore_state(struct kvm_vcpu *vcpu, |
| 149 | struct kvm_guest_debug_arch *dbg, |
| 150 | struct kvm_cpu_context *ctxt); |
| 151 | void __debug_cond_save_host_state(struct kvm_vcpu *vcpu); |
| 152 | void __debug_cond_restore_host_state(struct kvm_vcpu *vcpu); |
| 153 | |
Marc Zyngier | c13d168 | 2015-10-26 08:34:09 +0000 | [diff] [blame] | 154 | void __fpsimd_save_state(struct user_fpsimd_state *fp_regs); |
| 155 | void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs); |
Marc Zyngier | 3287622 | 2015-10-28 14:15:45 +0000 | [diff] [blame] | 156 | bool __fpsimd_enabled(void); |
Marc Zyngier | c13d168 | 2015-10-26 08:34:09 +0000 | [diff] [blame] | 157 | |
Marc Zyngier | b97b66c | 2015-10-22 08:32:18 +0100 | [diff] [blame] | 158 | u64 __guest_enter(struct kvm_vcpu *vcpu, struct kvm_cpu_context *host_ctxt); |
Marc Zyngier | 53fd5b6 | 2015-10-25 15:21:52 +0000 | [diff] [blame] | 159 | void __noreturn __hyp_do_panic(unsigned long, ...); |
Marc Zyngier | b97b66c | 2015-10-22 08:32:18 +0100 | [diff] [blame] | 160 | |
Marc Zyngier | c76a0a6 | 2015-10-21 10:09:49 +0100 | [diff] [blame] | 161 | #endif /* __ARM64_KVM_HYP_H__ */ |
| 162 | |