kvm_asm.h 12 KB

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