blob: 36d6758d580bb5b3c8155f26ce2f903605e2226d [file] [log] [blame]
Marc Zyngierc76a0a62015-10-21 10:09:49 +01001/*
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 Zyngier915ccd12015-10-28 12:00:00 +000028#define read_sysreg_elx(r,nvh,vh) \
29 ({ \
30 u64 reg; \
31 asm volatile(ALTERNATIVE("mrs %0, " __stringify(r##nvh),\
Alex Matveev79b4e3c2017-11-14 15:06:17 -080032 DEFINE_MRS_S \
33 "mrs_s %0, " __stringify(r##vh) "\n"\
34 UNDEFINE_MRS_S, \
Marc Zyngier915ccd12015-10-28 12:00:00 +000035 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 Matveev79b4e3c2017-11-14 15:06:17 -080044 DEFINE_MSR_S \
45 "msr_s " __stringify(r##vh) ", %x0\n"\
46 UNDEFINE_MSR_S, \
Marc Zyngier915ccd12015-10-28 12:00:00 +000047 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 Zyngierc1bf6e12015-10-28 08:45:37 +0000104/**
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) \
118typeof(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 Zyngier06282fd2015-10-19 15:50:37 +0100128void __vgic_v2_save_state(struct kvm_vcpu *vcpu);
129void __vgic_v2_restore_state(struct kvm_vcpu *vcpu);
Marc Zyngier3272f0d2016-09-06 14:02:17 +0100130int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu);
Marc Zyngier06282fd2015-10-19 15:50:37 +0100131
Marc Zyngierf68d2b12015-10-19 15:50:58 +0100132void __vgic_v3_save_state(struct kvm_vcpu *vcpu);
133void __vgic_v3_restore_state(struct kvm_vcpu *vcpu);
134
Marc Zyngier1431af32015-10-19 16:32:20 +0100135void __timer_save_state(struct kvm_vcpu *vcpu);
136void __timer_restore_state(struct kvm_vcpu *vcpu);
137
Marc Zyngieredef5282015-10-28 12:17:35 +0000138void __sysreg_save_host_state(struct kvm_cpu_context *ctxt);
139void __sysreg_restore_host_state(struct kvm_cpu_context *ctxt);
140void __sysreg_save_guest_state(struct kvm_cpu_context *ctxt);
141void __sysreg_restore_guest_state(struct kvm_cpu_context *ctxt);
Marc Zyngierc209ec82015-10-19 19:28:29 +0100142void __sysreg32_save_state(struct kvm_vcpu *vcpu);
143void __sysreg32_restore_state(struct kvm_vcpu *vcpu);
Marc Zyngier6d6ec202015-10-19 18:02:48 +0100144
Marc Zyngier8eb99262015-10-19 21:02:46 +0100145void __debug_save_state(struct kvm_vcpu *vcpu,
146 struct kvm_guest_debug_arch *dbg,
147 struct kvm_cpu_context *ctxt);
148void __debug_restore_state(struct kvm_vcpu *vcpu,
149 struct kvm_guest_debug_arch *dbg,
150 struct kvm_cpu_context *ctxt);
151void __debug_cond_save_host_state(struct kvm_vcpu *vcpu);
152void __debug_cond_restore_host_state(struct kvm_vcpu *vcpu);
153
Marc Zyngierc13d1682015-10-26 08:34:09 +0000154void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
155void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs);
Marc Zyngier32876222015-10-28 14:15:45 +0000156bool __fpsimd_enabled(void);
Marc Zyngierc13d1682015-10-26 08:34:09 +0000157
Marc Zyngierb97b66c2015-10-22 08:32:18 +0100158u64 __guest_enter(struct kvm_vcpu *vcpu, struct kvm_cpu_context *host_ctxt);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000159void __noreturn __hyp_do_panic(unsigned long, ...);
Marc Zyngierb97b66c2015-10-22 08:32:18 +0100160
Marc Zyngierc76a0a62015-10-21 10:09:49 +0100161#endif /* __ARM64_KVM_HYP_H__ */
162