1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012,2013 - ARM Ltd 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 */ 6 7 #ifndef __ARM_KVM_ASM_H__ 8 #define __ARM_KVM_ASM_H__ 9 10 #include <asm/hyp_image.h> 11 #include <asm/insn.h> 12 #include <asm/virt.h> 13 #include <asm/sysreg.h> 14 15 #define ARM_EXIT_WITH_SERROR_BIT 31 16 #define ARM_EXCEPTION_CODE(x) ((x) & ~(1U << ARM_EXIT_WITH_SERROR_BIT)) 17 #define ARM_EXCEPTION_IS_TRAP(x) (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP) 18 #define ARM_SERROR_PENDING(x) !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT)) 19 20 #define ARM_EXCEPTION_IRQ 0 21 #define ARM_EXCEPTION_EL1_SERROR 1 22 #define ARM_EXCEPTION_TRAP 2 23 #define ARM_EXCEPTION_IL 3 24 /* The hyp-stub will return this for any kvm_call_hyp() call */ 25 #define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR 26 27 #define kvm_arm_exception_type \ 28 {ARM_EXCEPTION_IRQ, "IRQ" }, \ 29 {ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \ 30 {ARM_EXCEPTION_TRAP, "TRAP" }, \ 31 {ARM_EXCEPTION_HYP_GONE, "HYP_GONE" } 32 33 /* 34 * Size of the HYP vectors preamble. kvm_patch_vector_branch() generates code 35 * that jumps over this. 36 */ 37 #define KVM_VECTOR_PREAMBLE (2 * AARCH64_INSN_SIZE) 38 39 #define KVM_HOST_SMCCC_ID(id) \ 40 ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 41 ARM_SMCCC_SMC_64, \ 42 ARM_SMCCC_OWNER_VENDOR_HYP, \ 43 (id)) 44 45 #define KVM_HOST_SMCCC_FUNC(name) KVM_HOST_SMCCC_ID(__KVM_HOST_SMCCC_FUNC_##name) 46 47 #define __KVM_HOST_SMCCC_FUNC___kvm_hyp_init 0 48 49 #ifndef __ASSEMBLER__ 50 51 #include <linux/mm.h> 52 53 enum __kvm_host_smccc_func { 54 /* Hypercalls that are unavailable once pKVM has finalised. */ 55 /* __KVM_HOST_SMCCC_FUNC___kvm_hyp_init */ 56 __KVM_HOST_SMCCC_FUNC___pkvm_init = __KVM_HOST_SMCCC_FUNC___kvm_hyp_init + 1, 57 __KVM_HOST_SMCCC_FUNC___pkvm_create_private_mapping, 58 __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector, 59 __KVM_HOST_SMCCC_FUNC___kvm_enable_ssbs, 60 __KVM_HOST_SMCCC_FUNC___vgic_v3_init_lrs, 61 __KVM_HOST_SMCCC_FUNC___vgic_v3_get_gic_config, 62 __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize, 63 __KVM_HOST_SMCCC_FUNC_MIN_PKVM = __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize, 64 65 /* Hypercalls that are always available and common to [nh]VHE/pKVM. */ 66 __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, 67 __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, 68 __KVM_HOST_SMCCC_FUNC___kvm_flush_vm_context, 69 __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_ipa, 70 __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_ipa_nsh, 71 __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid, 72 __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_range, 73 __KVM_HOST_SMCCC_FUNC___kvm_flush_cpu_context, 74 __KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntvoff, 75 __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs, 76 __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs, 77 __KVM_HOST_SMCCC_FUNC___vgic_v5_save_apr, 78 __KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr, 79 __KVM_HOST_SMCCC_FUNC_MAX_NO_PKVM = __KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr, 80 81 /* Hypercalls that are available only when pKVM has finalised. */ 82 __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, 83 __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, 84 __KVM_HOST_SMCCC_FUNC___pkvm_host_donate_guest, 85 __KVM_HOST_SMCCC_FUNC___pkvm_host_share_guest, 86 __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_guest, 87 __KVM_HOST_SMCCC_FUNC___pkvm_host_relax_perms_guest, 88 __KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest, 89 __KVM_HOST_SMCCC_FUNC___pkvm_host_test_clear_young_guest, 90 __KVM_HOST_SMCCC_FUNC___pkvm_host_mkyoung_guest, 91 __KVM_HOST_SMCCC_FUNC___pkvm_reserve_vm, 92 __KVM_HOST_SMCCC_FUNC___pkvm_unreserve_vm, 93 __KVM_HOST_SMCCC_FUNC___pkvm_init_vm, 94 __KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu, 95 __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_in_poison_fault, 96 __KVM_HOST_SMCCC_FUNC___pkvm_force_reclaim_guest_page, 97 __KVM_HOST_SMCCC_FUNC___pkvm_reclaim_dying_guest_page, 98 __KVM_HOST_SMCCC_FUNC___pkvm_start_teardown_vm, 99 __KVM_HOST_SMCCC_FUNC___pkvm_finalize_teardown_vm, 100 __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, 101 __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, 102 __KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid, 103 __KVM_HOST_SMCCC_FUNC___tracing_load, 104 __KVM_HOST_SMCCC_FUNC___tracing_unload, 105 __KVM_HOST_SMCCC_FUNC___tracing_enable, 106 __KVM_HOST_SMCCC_FUNC___tracing_swap_reader, 107 __KVM_HOST_SMCCC_FUNC___tracing_update_clock, 108 __KVM_HOST_SMCCC_FUNC___tracing_reset, 109 __KVM_HOST_SMCCC_FUNC___tracing_enable_event, 110 __KVM_HOST_SMCCC_FUNC___tracing_write_event, 111 }; 112 113 #define DECLARE_KVM_VHE_SYM(sym) extern char sym[] 114 #define DECLARE_KVM_NVHE_SYM(sym) extern char kvm_nvhe_sym(sym)[] 115 116 /* 117 * Define a pair of symbols sharing the same name but one defined in 118 * VHE and the other in nVHE hyp implementations. 119 */ 120 #define DECLARE_KVM_HYP_SYM(sym) \ 121 DECLARE_KVM_VHE_SYM(sym); \ 122 DECLARE_KVM_NVHE_SYM(sym) 123 124 #define DECLARE_KVM_VHE_PER_CPU(type, sym) \ 125 DECLARE_PER_CPU(type, sym) 126 #define DECLARE_KVM_NVHE_PER_CPU(type, sym) \ 127 DECLARE_PER_CPU(type, kvm_nvhe_sym(sym)) 128 129 #define DECLARE_KVM_HYP_PER_CPU(type, sym) \ 130 DECLARE_KVM_VHE_PER_CPU(type, sym); \ 131 DECLARE_KVM_NVHE_PER_CPU(type, sym) 132 133 /* 134 * Compute pointer to a symbol defined in nVHE percpu region. 135 * Returns NULL if percpu memory has not been allocated yet. 136 */ 137 #define this_cpu_ptr_nvhe_sym(sym) per_cpu_ptr_nvhe_sym(sym, smp_processor_id()) 138 #define per_cpu_ptr_nvhe_sym(sym, cpu) \ 139 ({ \ 140 unsigned long base, off; \ 141 base = kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; \ 142 off = (unsigned long)&CHOOSE_NVHE_SYM(sym) - \ 143 (unsigned long)&CHOOSE_NVHE_SYM(__per_cpu_start); \ 144 base ? (typeof(CHOOSE_NVHE_SYM(sym))*)(base + off) : NULL; \ 145 }) 146 147 #if defined(__KVM_NVHE_HYPERVISOR__) 148 149 #define CHOOSE_NVHE_SYM(sym) sym 150 #define CHOOSE_HYP_SYM(sym) CHOOSE_NVHE_SYM(sym) 151 152 /* The nVHE hypervisor shouldn't even try to access VHE symbols */ 153 extern void *__nvhe_undefined_symbol; 154 #define CHOOSE_VHE_SYM(sym) __nvhe_undefined_symbol 155 #define this_cpu_ptr_hyp_sym(sym) (&__nvhe_undefined_symbol) 156 #define per_cpu_ptr_hyp_sym(sym, cpu) (&__nvhe_undefined_symbol) 157 158 #elif defined(__KVM_VHE_HYPERVISOR__) 159 160 #define CHOOSE_VHE_SYM(sym) sym 161 #define CHOOSE_HYP_SYM(sym) CHOOSE_VHE_SYM(sym) 162 163 /* The VHE hypervisor shouldn't even try to access nVHE symbols */ 164 extern void *__vhe_undefined_symbol; 165 #define CHOOSE_NVHE_SYM(sym) __vhe_undefined_symbol 166 #define this_cpu_ptr_hyp_sym(sym) (&__vhe_undefined_symbol) 167 #define per_cpu_ptr_hyp_sym(sym, cpu) (&__vhe_undefined_symbol) 168 169 #else 170 171 /* 172 * BIG FAT WARNINGS: 173 * 174 * - Don't be tempted to change the following is_kernel_in_hyp_mode() 175 * to has_vhe(). has_vhe() is implemented as a *final* capability, 176 * while this is used early at boot time, when the capabilities are 177 * not final yet.... 178 * 179 * - Don't let the nVHE hypervisor have access to this, as it will 180 * pick the *wrong* symbol (yes, it runs at EL2...). 181 */ 182 #define CHOOSE_HYP_SYM(sym) (is_kernel_in_hyp_mode() \ 183 ? CHOOSE_VHE_SYM(sym) \ 184 : CHOOSE_NVHE_SYM(sym)) 185 186 #define this_cpu_ptr_hyp_sym(sym) (is_kernel_in_hyp_mode() \ 187 ? this_cpu_ptr(&sym) \ 188 : this_cpu_ptr_nvhe_sym(sym)) 189 190 #define per_cpu_ptr_hyp_sym(sym, cpu) (is_kernel_in_hyp_mode() \ 191 ? per_cpu_ptr(&sym, cpu) \ 192 : per_cpu_ptr_nvhe_sym(sym, cpu)) 193 194 #define CHOOSE_VHE_SYM(sym) sym 195 #define CHOOSE_NVHE_SYM(sym) kvm_nvhe_sym(sym) 196 197 #endif 198 199 struct kvm_nvhe_init_params { 200 unsigned long mair_el2; 201 unsigned long tcr_el2; 202 unsigned long tpidr_el2; 203 unsigned long stack_hyp_va; 204 unsigned long stack_pa; 205 phys_addr_t pgd_pa; 206 unsigned long hcr_el2; 207 unsigned long vttbr; 208 unsigned long vtcr; 209 unsigned long tmp; 210 }; 211 212 /* 213 * Used by the host in EL1 to dump the nVHE hypervisor backtrace on 214 * hyp_panic() in non-protected mode. 215 * 216 * @stack_base: hyp VA of the hyp_stack base. 217 * @overflow_stack_base: hyp VA of the hyp_overflow_stack base. 218 * @fp: hyp FP where the backtrace begins. 219 * @pc: hyp PC where the backtrace begins. 220 */ 221 struct kvm_nvhe_stacktrace_info { 222 unsigned long stack_base; 223 unsigned long overflow_stack_base; 224 unsigned long fp; 225 unsigned long pc; 226 }; 227 228 /* Translate a kernel address @ptr into its equivalent linear mapping */ 229 #define kvm_ksym_ref(ptr) \ 230 ({ \ 231 void *val = (ptr); \ 232 if (!is_kernel_in_hyp_mode()) \ 233 val = lm_alias((ptr)); \ 234 val; \ 235 }) 236 #define kvm_ksym_ref_nvhe(sym) kvm_ksym_ref(kvm_nvhe_sym(sym)) 237 238 struct kvm; 239 struct kvm_vcpu; 240 struct kvm_s2_mmu; 241 242 DECLARE_KVM_NVHE_SYM(__kvm_hyp_init); 243 DECLARE_KVM_HYP_SYM(__kvm_hyp_vector); 244 #define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init) 245 #define __kvm_hyp_vector CHOOSE_HYP_SYM(__kvm_hyp_vector) 246 247 extern unsigned long kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[]; 248 DECLARE_KVM_NVHE_SYM(__per_cpu_start); 249 DECLARE_KVM_NVHE_SYM(__per_cpu_end); 250 251 DECLARE_KVM_HYP_SYM(__bp_harden_hyp_vecs); 252 #define __bp_harden_hyp_vecs CHOOSE_HYP_SYM(__bp_harden_hyp_vecs) 253 254 extern void __kvm_flush_vm_context(void); 255 extern void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu); 256 extern void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa, 257 int level); 258 extern void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu, 259 phys_addr_t ipa, 260 int level); 261 extern void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu, 262 phys_addr_t start, unsigned long pages); 263 extern void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu); 264 265 extern int __kvm_tlbi_s1e2(struct kvm_s2_mmu *mmu, u64 va, u64 sys_encoding); 266 267 extern void __kvm_timer_set_cntvoff(u64 cntvoff); 268 extern int __kvm_at_s1e01(struct kvm_vcpu *vcpu, u32 op, u64 vaddr); 269 extern int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr); 270 extern int __kvm_at_s12(struct kvm_vcpu *vcpu, u32 op, u64 vaddr); 271 272 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu); 273 274 extern void __kvm_adjust_pc(struct kvm_vcpu *vcpu); 275 276 extern u64 __vgic_v3_get_gic_config(void); 277 extern void __vgic_v3_init_lrs(void); 278 279 #define __KVM_EXTABLE(from, to) \ 280 " .pushsection __kvm_ex_table, \"a\"\n" \ 281 " .align 3\n" \ 282 " .long (" #from " - .), (" #to " - .)\n" \ 283 " .popsection\n" 284 285 286 #define __kvm_at(at_op, addr) \ 287 ( { \ 288 int __kvm_at_err = 0; \ 289 u64 spsr, elr; \ 290 asm volatile( \ 291 " mrs %1, spsr_el2\n" \ 292 " mrs %2, elr_el2\n" \ 293 "1: " __msr_s(at_op, "%3") "\n" \ 294 " isb\n" \ 295 " b 9f\n" \ 296 "2: msr spsr_el2, %1\n" \ 297 " msr elr_el2, %2\n" \ 298 " mov %w0, %4\n" \ 299 "9:\n" \ 300 __KVM_EXTABLE(1b, 2b) \ 301 : "+r" (__kvm_at_err), "=&r" (spsr), "=&r" (elr) \ 302 : "r" (addr), "i" (-EFAULT)); \ 303 __kvm_at_err; \ 304 } ) 305 306 void __noreturn hyp_panic(void); 307 asmlinkage void kvm_unexpected_el2_exception(void); 308 asmlinkage void __noreturn hyp_panic(void); 309 asmlinkage void __noreturn hyp_panic_bad_stack(void); 310 asmlinkage void kvm_unexpected_el2_exception(void); 311 struct kvm_cpu_context; 312 void handle_trap(struct kvm_cpu_context *host_ctxt); 313 asmlinkage void __noreturn __kvm_host_psci_cpu_on_entry(void); 314 asmlinkage void __noreturn __kvm_host_psci_cpu_resume_entry(void); 315 void __noreturn __pkvm_init_finalise(void); 316 void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc); 317 void kvm_patch_vector_branch(struct alt_instr *alt, 318 __le32 *origptr, __le32 *updptr, int nr_inst); 319 void kvm_get_kimage_voffset(struct alt_instr *alt, 320 __le32 *origptr, __le32 *updptr, int nr_inst); 321 void kvm_compute_final_ctr_el0(struct alt_instr *alt, 322 __le32 *origptr, __le32 *updptr, int nr_inst); 323 void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr, u64 elr_virt, 324 u64 elr_phys, u64 par, uintptr_t vcpu, u64 far, u64 hpfar); 325 326 #else /* __ASSEMBLER__ */ 327 328 .macro get_host_ctxt reg, tmp 329 adr_this_cpu \reg, kvm_host_data, \tmp 330 add \reg, \reg, #HOST_DATA_CONTEXT 331 .endm 332 333 .macro get_vcpu_ptr vcpu, ctxt 334 get_host_ctxt \ctxt, \vcpu 335 ldr \vcpu, [\ctxt, #HOST_CONTEXT_VCPU] 336 .endm 337 338 .macro get_loaded_vcpu vcpu, ctxt 339 adr_this_cpu \ctxt, kvm_hyp_ctxt, \vcpu 340 ldr \vcpu, [\ctxt, #HOST_CONTEXT_VCPU] 341 .endm 342 343 .macro set_loaded_vcpu vcpu, ctxt, tmp 344 adr_this_cpu \ctxt, kvm_hyp_ctxt, \tmp 345 str \vcpu, [\ctxt, #HOST_CONTEXT_VCPU] 346 .endm 347 348 /* 349 * KVM extable for unexpected exceptions. 350 * Create a struct kvm_exception_table_entry output to a section that can be 351 * mapped by EL2. The table is not sorted. 352 * 353 * The caller must ensure: 354 * x18 has the hypervisor value to allow any Shadow-Call-Stack instrumented 355 * code to write to it, and that SPSR_EL2 and ELR_EL2 are restored by the fixup. 356 */ 357 .macro _kvm_extable, from, to 358 .pushsection __kvm_ex_table, "a" 359 .align 3 360 .long (\from - .), (\to - .) 361 .popsection 362 .endm 363 364 #define CPU_XREG_OFFSET(x) (CPU_USER_PT_REGS + 8*x) 365 #define CPU_LR_OFFSET CPU_XREG_OFFSET(30) 366 #define CPU_SP_EL0_OFFSET (CPU_LR_OFFSET + 8) 367 368 /* 369 * We treat x18 as callee-saved as the host may use it as a platform 370 * register (e.g. for shadow call stack). 371 */ 372 .macro save_callee_saved_regs ctxt 373 str x18, [\ctxt, #CPU_XREG_OFFSET(18)] 374 stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 375 stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 376 stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 377 stp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 378 stp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 379 stp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 380 .endm 381 382 .macro restore_callee_saved_regs ctxt 383 // We require \ctxt is not x18-x28 384 ldr x18, [\ctxt, #CPU_XREG_OFFSET(18)] 385 ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 386 ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 387 ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 388 ldp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 389 ldp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 390 ldp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 391 .endm 392 393 .macro save_sp_el0 ctxt, tmp 394 mrs \tmp, sp_el0 395 str \tmp, [\ctxt, #CPU_SP_EL0_OFFSET] 396 .endm 397 398 .macro restore_sp_el0 ctxt, tmp 399 ldr \tmp, [\ctxt, #CPU_SP_EL0_OFFSET] 400 msr sp_el0, \tmp 401 .endm 402 403 #endif 404 405 #endif /* __ARM_KVM_ASM_H__ */ 406