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 14 #define ARM_EXIT_WITH_SERROR_BIT 31 15 #define ARM_EXCEPTION_CODE(x) ((x) & ~(1U << ARM_EXIT_WITH_SERROR_BIT)) 16 #define ARM_EXCEPTION_IS_TRAP(x) (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP) 17 #define ARM_SERROR_PENDING(x) !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT)) 18 19 #define ARM_EXCEPTION_IRQ 0 20 #define ARM_EXCEPTION_EL1_SERROR 1 21 #define ARM_EXCEPTION_TRAP 2 22 #define ARM_EXCEPTION_IL 3 23 /* The hyp-stub will return this for any kvm_call_hyp() call */ 24 #define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR 25 26 #define kvm_arm_exception_type \ 27 {ARM_EXCEPTION_IRQ, "IRQ" }, \ 28 {ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \ 29 {ARM_EXCEPTION_TRAP, "TRAP" }, \ 30 {ARM_EXCEPTION_HYP_GONE, "HYP_GONE" } 31 32 /* 33 * Size of the HYP vectors preamble. kvm_patch_vector_branch() generates code 34 * that jumps over this. 35 */ 36 #define KVM_VECTOR_PREAMBLE (2 * AARCH64_INSN_SIZE) 37 38 #define KVM_HOST_SMCCC_ID(id) \ 39 ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 40 ARM_SMCCC_SMC_64, \ 41 ARM_SMCCC_OWNER_VENDOR_HYP, \ 42 (id)) 43 44 #define KVM_HOST_SMCCC_FUNC(name) KVM_HOST_SMCCC_ID(__KVM_HOST_SMCCC_FUNC_##name) 45 46 #define __KVM_HOST_SMCCC_FUNC___kvm_hyp_init 0 47 48 #ifndef __ASSEMBLY__ 49 50 #include <linux/mm.h> 51 52 enum __kvm_host_smccc_func { 53 /* Hypercalls available only prior to pKVM finalisation */ 54 /* __KVM_HOST_SMCCC_FUNC___kvm_hyp_init */ 55 __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 = __KVM_HOST_SMCCC_FUNC___kvm_hyp_init + 1, 56 __KVM_HOST_SMCCC_FUNC___pkvm_init, 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___kvm_flush_vm_context, 63 __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_ipa, 64 __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid, 65 __KVM_HOST_SMCCC_FUNC___kvm_flush_cpu_context, 66 __KVM_HOST_SMCCC_FUNC___pkvm_alloc_module_va, 67 __KVM_HOST_SMCCC_FUNC___pkvm_map_module_page, 68 __KVM_HOST_SMCCC_FUNC___pkvm_unmap_module_page, 69 __KVM_HOST_SMCCC_FUNC___pkvm_init_module, 70 __KVM_HOST_SMCCC_FUNC___pkvm_register_hcall, 71 __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize, 72 73 /* Hypercalls available after pKVM finalisation */ 74 __KVM_HOST_SMCCC_FUNC___pkvm_host_share_hyp, 75 __KVM_HOST_SMCCC_FUNC___pkvm_host_unshare_hyp, 76 __KVM_HOST_SMCCC_FUNC___pkvm_host_map_guest, 77 __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc, 78 __KVM_HOST_SMCCC_FUNC___kvm_vcpu_run, 79 __KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntvoff, 80 __KVM_HOST_SMCCC_FUNC___vgic_v3_save_vmcr_aprs, 81 __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs, 82 __KVM_HOST_SMCCC_FUNC___pkvm_init_vm, 83 __KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu, 84 __KVM_HOST_SMCCC_FUNC___pkvm_start_teardown_vm, 85 __KVM_HOST_SMCCC_FUNC___pkvm_finalize_teardown_vm, 86 __KVM_HOST_SMCCC_FUNC___pkvm_reclaim_dying_guest_page, 87 __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load, 88 __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put, 89 __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_sync_state, 90 __KVM_HOST_SMCCC_FUNC___pkvm_iommu_driver_init, 91 __KVM_HOST_SMCCC_FUNC___pkvm_iommu_register, 92 __KVM_HOST_SMCCC_FUNC___pkvm_iommu_pm_notify, 93 __KVM_HOST_SMCCC_FUNC___pkvm_iommu_finalize, 94 __KVM_HOST_SMCCC_FUNC___pkvm_load_tracing, 95 __KVM_HOST_SMCCC_FUNC___pkvm_teardown_tracing, 96 __KVM_HOST_SMCCC_FUNC___pkvm_enable_tracing, 97 __KVM_HOST_SMCCC_FUNC___pkvm_rb_swap_reader_page, 98 __KVM_HOST_SMCCC_FUNC___pkvm_rb_update_footers, 99 __KVM_HOST_SMCCC_FUNC___pkvm_enable_event, 100 #ifdef CONFIG_ANDROID_ARM64_WORKAROUND_DMA_BEYOND_POC 101 __KVM_HOST_SMCCC_FUNC___pkvm_host_set_stage2_memattr, 102 #endif 103 104 /* 105 * Start of the dynamically registered hypercalls. Start a bit 106 * further, just in case some modules... 107 */ 108 __KVM_HOST_SMCCC_FUNC___dynamic_hcalls = 128, 109 }; 110 111 #define DECLARE_KVM_VHE_SYM(sym) extern char sym[] 112 #define DECLARE_KVM_NVHE_SYM(sym) extern char kvm_nvhe_sym(sym)[] 113 114 /* 115 * Define a pair of symbols sharing the same name but one defined in 116 * VHE and the other in nVHE hyp implementations. 117 */ 118 #define DECLARE_KVM_HYP_SYM(sym) \ 119 DECLARE_KVM_VHE_SYM(sym); \ 120 DECLARE_KVM_NVHE_SYM(sym) 121 122 #define DECLARE_KVM_VHE_PER_CPU(type, sym) \ 123 DECLARE_PER_CPU(type, sym) 124 #define DECLARE_KVM_NVHE_PER_CPU(type, sym) \ 125 DECLARE_PER_CPU(type, kvm_nvhe_sym(sym)) 126 127 #define DECLARE_KVM_HYP_PER_CPU(type, sym) \ 128 DECLARE_KVM_VHE_PER_CPU(type, sym); \ 129 DECLARE_KVM_NVHE_PER_CPU(type, sym) 130 131 /* 132 * Compute pointer to a symbol defined in nVHE percpu region. 133 * Returns NULL if percpu memory has not been allocated yet. 134 */ 135 #define this_cpu_ptr_nvhe_sym(sym) per_cpu_ptr_nvhe_sym(sym, smp_processor_id()) 136 #define per_cpu_ptr_nvhe_sym(sym, cpu) \ 137 ({ \ 138 unsigned long base, off; \ 139 base = kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; \ 140 off = (unsigned long)&CHOOSE_NVHE_SYM(sym) - \ 141 (unsigned long)&CHOOSE_NVHE_SYM(__per_cpu_start); \ 142 base ? (typeof(CHOOSE_NVHE_SYM(sym))*)(base + off) : NULL; \ 143 }) 144 145 #if defined(__KVM_NVHE_HYPERVISOR__) 146 147 #define CHOOSE_NVHE_SYM(sym) sym 148 #define CHOOSE_HYP_SYM(sym) CHOOSE_NVHE_SYM(sym) 149 150 /* The nVHE hypervisor shouldn't even try to access VHE symbols */ 151 extern void *__nvhe_undefined_symbol; 152 #define CHOOSE_VHE_SYM(sym) __nvhe_undefined_symbol 153 #define this_cpu_ptr_hyp_sym(sym) (&__nvhe_undefined_symbol) 154 #define per_cpu_ptr_hyp_sym(sym, cpu) (&__nvhe_undefined_symbol) 155 156 /* 157 * pKVM uses the module_ops struct to expose services to modules but 158 * doesn't allow fine-grained definition of the license for each export, 159 * and doesn't have a way to check the license of the loaded module. 160 * Given that said module may be proprietary, let's seek GPL compliance 161 * by preventing the accidental export of GPL symbols to hyp modules via 162 * pKVM's module_ops struct. 163 */ 164 #ifdef EXPORT_SYMBOL_GPL 165 #undef EXPORT_SYMBOL_GPL 166 #endif 167 #define EXPORT_SYMBOL_GPL(sym) BUILD_BUG() 168 169 #elif defined(__KVM_VHE_HYPERVISOR__) 170 171 #define CHOOSE_VHE_SYM(sym) sym 172 #define CHOOSE_HYP_SYM(sym) CHOOSE_VHE_SYM(sym) 173 174 /* The VHE hypervisor shouldn't even try to access nVHE symbols */ 175 extern void *__vhe_undefined_symbol; 176 #define CHOOSE_NVHE_SYM(sym) __vhe_undefined_symbol 177 #define this_cpu_ptr_hyp_sym(sym) (&__vhe_undefined_symbol) 178 #define per_cpu_ptr_hyp_sym(sym, cpu) (&__vhe_undefined_symbol) 179 180 #else 181 182 /* 183 * BIG FAT WARNINGS: 184 * 185 * - Don't be tempted to change the following is_kernel_in_hyp_mode() 186 * to has_vhe(). has_vhe() is implemented as a *final* capability, 187 * while this is used early at boot time, when the capabilities are 188 * not final yet.... 189 * 190 * - Don't let the nVHE hypervisor have access to this, as it will 191 * pick the *wrong* symbol (yes, it runs at EL2...). 192 */ 193 #define CHOOSE_HYP_SYM(sym) (is_kernel_in_hyp_mode() \ 194 ? CHOOSE_VHE_SYM(sym) \ 195 : CHOOSE_NVHE_SYM(sym)) 196 197 #define this_cpu_ptr_hyp_sym(sym) (is_kernel_in_hyp_mode() \ 198 ? this_cpu_ptr(&sym) \ 199 : this_cpu_ptr_nvhe_sym(sym)) 200 201 #define per_cpu_ptr_hyp_sym(sym, cpu) (is_kernel_in_hyp_mode() \ 202 ? per_cpu_ptr(&sym, cpu) \ 203 : per_cpu_ptr_nvhe_sym(sym, cpu)) 204 205 #define CHOOSE_VHE_SYM(sym) sym 206 #define CHOOSE_NVHE_SYM(sym) kvm_nvhe_sym(sym) 207 208 #endif 209 210 struct kvm_nvhe_init_params { 211 unsigned long mair_el2; 212 unsigned long tcr_el2; 213 unsigned long tpidr_el2; 214 unsigned long stack_hyp_va; 215 unsigned long stack_pa; 216 phys_addr_t pgd_pa; 217 unsigned long hcr_el2; 218 unsigned long hfgwtr_el2; 219 unsigned long vttbr; 220 unsigned long vtcr; 221 }; 222 223 /* 224 * Used by the host in EL1 to dump the nVHE hypervisor backtrace on 225 * hyp_panic() in non-protected mode. 226 * 227 * @stack_base: hyp VA of the hyp_stack base. 228 * @overflow_stack_base: hyp VA of the hyp_overflow_stack base. 229 * @fp: hyp FP where the backtrace begins. 230 * @pc: hyp PC where the backtrace begins. 231 */ 232 struct kvm_nvhe_stacktrace_info { 233 unsigned long stack_base; 234 unsigned long overflow_stack_base; 235 unsigned long fp; 236 unsigned long pc; 237 }; 238 239 /* Translate a kernel address @ptr into its equivalent linear mapping */ 240 #define kvm_ksym_ref(ptr) \ 241 ({ \ 242 void *val = (ptr); \ 243 if (!is_kernel_in_hyp_mode()) \ 244 val = lm_alias((ptr)); \ 245 val; \ 246 }) 247 #define kvm_ksym_ref_nvhe(sym) kvm_ksym_ref(kvm_nvhe_sym(sym)) 248 249 struct kvm; 250 struct kvm_vcpu; 251 struct kvm_s2_mmu; 252 253 DECLARE_KVM_NVHE_SYM(__kvm_hyp_init); 254 DECLARE_KVM_HYP_SYM(__kvm_hyp_vector); 255 #define __kvm_hyp_init CHOOSE_NVHE_SYM(__kvm_hyp_init) 256 #define __kvm_hyp_vector CHOOSE_HYP_SYM(__kvm_hyp_vector) 257 258 extern unsigned long kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[]; 259 DECLARE_KVM_NVHE_SYM(__per_cpu_start); 260 DECLARE_KVM_NVHE_SYM(__per_cpu_end); 261 262 extern unsigned long kvm_nvhe_sym(kvm_arm_hyp_host_fp_state)[]; 263 264 DECLARE_KVM_HYP_SYM(__bp_harden_hyp_vecs); 265 #define __bp_harden_hyp_vecs CHOOSE_HYP_SYM(__bp_harden_hyp_vecs) 266 267 extern void __kvm_flush_vm_context(void); 268 extern void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu); 269 extern void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa, 270 int level); 271 extern void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu); 272 273 extern void __kvm_timer_set_cntvoff(u64 cntvoff); 274 275 extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu); 276 277 extern void __kvm_adjust_pc(struct kvm_vcpu *vcpu); 278 279 extern u64 __vgic_v3_get_gic_config(void); 280 extern void __vgic_v3_init_lrs(void); 281 282 extern u64 __kvm_get_mdcr_el2(void); 283 284 #define __KVM_EXTABLE(from, to) \ 285 " .pushsection __kvm_ex_table, \"a\"\n" \ 286 " .align 3\n" \ 287 " .long (" #from " - .), (" #to " - .)\n" \ 288 " .popsection\n" 289 290 291 #define __kvm_at(at_op, addr) \ 292 ( { \ 293 int __kvm_at_err = 0; \ 294 u64 spsr, elr; \ 295 asm volatile( \ 296 " mrs %1, spsr_el2\n" \ 297 " mrs %2, elr_el2\n" \ 298 "1: at "at_op", %3\n" \ 299 " isb\n" \ 300 " b 9f\n" \ 301 "2: msr spsr_el2, %1\n" \ 302 " msr elr_el2, %2\n" \ 303 " mov %w0, %4\n" \ 304 "9:\n" \ 305 __KVM_EXTABLE(1b, 2b) \ 306 : "+r" (__kvm_at_err), "=&r" (spsr), "=&r" (elr) \ 307 : "r" (addr), "i" (-EFAULT)); \ 308 __kvm_at_err; \ 309 } ) 310 311 312 #else /* __ASSEMBLY__ */ 313 314 .macro get_host_ctxt reg, tmp 315 adr_this_cpu \reg, kvm_host_data, \tmp 316 add \reg, \reg, #HOST_DATA_CONTEXT 317 .endm 318 319 .macro get_vcpu_ptr vcpu, ctxt 320 get_host_ctxt \ctxt, \vcpu 321 ldr \vcpu, [\ctxt, #HOST_CONTEXT_VCPU] 322 .endm 323 324 .macro get_loaded_vcpu vcpu, ctxt 325 adr_this_cpu \ctxt, kvm_hyp_ctxt, \vcpu 326 ldr \vcpu, [\ctxt, #HOST_CONTEXT_VCPU] 327 .endm 328 329 .macro set_loaded_vcpu vcpu, ctxt, tmp 330 adr_this_cpu \ctxt, kvm_hyp_ctxt, \tmp 331 str \vcpu, [\ctxt, #HOST_CONTEXT_VCPU] 332 .endm 333 334 /* 335 * KVM extable for unexpected exceptions. 336 * In the same format _asm_extable, but output to a different section so that 337 * it can be mapped to EL2. The KVM version is not sorted. The caller must 338 * ensure: 339 * x18 has the hypervisor value to allow any Shadow-Call-Stack instrumented 340 * code to write to it, and that SPSR_EL2 and ELR_EL2 are restored by the fixup. 341 */ 342 .macro _kvm_extable, from, to 343 .pushsection __kvm_ex_table, "a" 344 .align 3 345 .long (\from - .), (\to - .) 346 .popsection 347 .endm 348 349 #define CPU_XREG_OFFSET(x) (CPU_USER_PT_REGS + 8*x) 350 #define CPU_LR_OFFSET CPU_XREG_OFFSET(30) 351 #define CPU_SP_EL0_OFFSET (CPU_LR_OFFSET + 8) 352 353 /* 354 * We treat x18 as callee-saved as the host may use it as a platform 355 * register (e.g. for shadow call stack). 356 */ 357 .macro save_callee_saved_regs ctxt 358 str x18, [\ctxt, #CPU_XREG_OFFSET(18)] 359 stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 360 stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 361 stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 362 stp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 363 stp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 364 stp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 365 .endm 366 367 .macro restore_callee_saved_regs ctxt 368 // We require \ctxt is not x18-x28 369 ldr x18, [\ctxt, #CPU_XREG_OFFSET(18)] 370 ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 371 ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 372 ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 373 ldp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 374 ldp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 375 ldp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 376 .endm 377 378 .macro save_sp_el0 ctxt, tmp 379 mrs \tmp, sp_el0 380 str \tmp, [\ctxt, #CPU_SP_EL0_OFFSET] 381 .endm 382 383 .macro restore_sp_el0 ctxt, tmp 384 ldr \tmp, [\ctxt, #CPU_SP_EL0_OFFSET] 385 msr sp_el0, \tmp 386 .endm 387 388 #endif 389 390 #endif /* __ARM_KVM_ASM_H__ */ 391