kvm_asm.h 12 KB

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