kvm_asm.h 12 KB

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