processor.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * AArch64 processor specific defines
  4. *
  5. * Copyright (C) 2018, Red Hat, Inc.
  6. */
  7. #ifndef SELFTEST_KVM_PROCESSOR_H
  8. #define SELFTEST_KVM_PROCESSOR_H
  9. #include "kvm_util.h"
  10. #include "ucall_common.h"
  11. #include <linux/stringify.h>
  12. #include <linux/types.h>
  13. #include <asm/sysreg.h>
  14. #define ARM64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
  15. KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
  16. /*
  17. * KVM_ARM64_SYS_REG(sys_reg_id): Helper macro to convert
  18. * SYS_* register definitions in asm/sysreg.h to use in KVM
  19. * calls such as vcpu_get_reg() and vcpu_set_reg().
  20. */
  21. #define KVM_ARM64_SYS_REG(sys_reg_id) \
  22. ARM64_SYS_REG(sys_reg_Op0(sys_reg_id), \
  23. sys_reg_Op1(sys_reg_id), \
  24. sys_reg_CRn(sys_reg_id), \
  25. sys_reg_CRm(sys_reg_id), \
  26. sys_reg_Op2(sys_reg_id))
  27. /*
  28. * Default MAIR
  29. * index attribute
  30. * DEVICE_nGnRnE 0 0000:0000
  31. * DEVICE_nGnRE 1 0000:0100
  32. * DEVICE_GRE 2 0000:1100
  33. * NORMAL_NC 3 0100:0100
  34. * NORMAL 4 1111:1111
  35. * NORMAL_WT 5 1011:1011
  36. */
  37. /* Linux doesn't use these memory types, so let's define them. */
  38. #define MAIR_ATTR_DEVICE_GRE UL(0x0c)
  39. #define MAIR_ATTR_NORMAL_WT UL(0xbb)
  40. #define MT_DEVICE_nGnRnE 0
  41. #define MT_DEVICE_nGnRE 1
  42. #define MT_DEVICE_GRE 2
  43. #define MT_NORMAL_NC 3
  44. #define MT_NORMAL 4
  45. #define MT_NORMAL_WT 5
  46. #define DEFAULT_MAIR_EL1 \
  47. (MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRnE, MT_DEVICE_nGnRnE) | \
  48. MAIR_ATTRIDX(MAIR_ATTR_DEVICE_nGnRE, MT_DEVICE_nGnRE) | \
  49. MAIR_ATTRIDX(MAIR_ATTR_DEVICE_GRE, MT_DEVICE_GRE) | \
  50. MAIR_ATTRIDX(MAIR_ATTR_NORMAL_NC, MT_NORMAL_NC) | \
  51. MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL) | \
  52. MAIR_ATTRIDX(MAIR_ATTR_NORMAL_WT, MT_NORMAL_WT))
  53. void aarch64_vcpu_setup(struct kvm_vcpu *vcpu, struct kvm_vcpu_init *init);
  54. struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
  55. struct kvm_vcpu_init *init, void *guest_code);
  56. struct ex_regs {
  57. u64 regs[31];
  58. u64 sp;
  59. u64 pc;
  60. u64 pstate;
  61. };
  62. #define VECTOR_NUM 16
  63. enum {
  64. VECTOR_SYNC_CURRENT_SP0,
  65. VECTOR_IRQ_CURRENT_SP0,
  66. VECTOR_FIQ_CURRENT_SP0,
  67. VECTOR_ERROR_CURRENT_SP0,
  68. VECTOR_SYNC_CURRENT,
  69. VECTOR_IRQ_CURRENT,
  70. VECTOR_FIQ_CURRENT,
  71. VECTOR_ERROR_CURRENT,
  72. VECTOR_SYNC_LOWER_64,
  73. VECTOR_IRQ_LOWER_64,
  74. VECTOR_FIQ_LOWER_64,
  75. VECTOR_ERROR_LOWER_64,
  76. VECTOR_SYNC_LOWER_32,
  77. VECTOR_IRQ_LOWER_32,
  78. VECTOR_FIQ_LOWER_32,
  79. VECTOR_ERROR_LOWER_32,
  80. };
  81. #define VECTOR_IS_SYNC(v) ((v) == VECTOR_SYNC_CURRENT_SP0 || \
  82. (v) == VECTOR_SYNC_CURRENT || \
  83. (v) == VECTOR_SYNC_LOWER_64 || \
  84. (v) == VECTOR_SYNC_LOWER_32)
  85. #define ESR_EC_NUM 64
  86. #define ESR_EC_SHIFT 26
  87. #define ESR_EC_MASK (ESR_EC_NUM - 1)
  88. #define ESR_EC_UNKNOWN 0x0
  89. #define ESR_EC_SVC64 0x15
  90. #define ESR_EC_IABT 0x21
  91. #define ESR_EC_DABT 0x25
  92. #define ESR_EC_HW_BP_CURRENT 0x31
  93. #define ESR_EC_SSTEP_CURRENT 0x33
  94. #define ESR_EC_WP_CURRENT 0x35
  95. #define ESR_EC_BRK_INS 0x3c
  96. /* Access flag */
  97. #define PTE_AF (1ULL << 10)
  98. /* Access flag update enable/disable */
  99. #define TCR_EL1_HA (1ULL << 39)
  100. void aarch64_get_supported_page_sizes(uint32_t ipa, uint32_t *ipa4k,
  101. uint32_t *ipa16k, uint32_t *ipa64k);
  102. void vm_init_descriptor_tables(struct kvm_vm *vm);
  103. void vcpu_init_descriptor_tables(struct kvm_vcpu *vcpu);
  104. typedef void(*handler_fn)(struct ex_regs *);
  105. void vm_install_exception_handler(struct kvm_vm *vm,
  106. int vector, handler_fn handler);
  107. void vm_install_sync_handler(struct kvm_vm *vm,
  108. int vector, int ec, handler_fn handler);
  109. uint64_t *virt_get_pte_hva(struct kvm_vm *vm, vm_vaddr_t gva);
  110. static inline void cpu_relax(void)
  111. {
  112. asm volatile("yield" ::: "memory");
  113. }
  114. #define isb() asm volatile("isb" : : : "memory")
  115. #define dsb(opt) asm volatile("dsb " #opt : : : "memory")
  116. #define dmb(opt) asm volatile("dmb " #opt : : : "memory")
  117. #define dma_wmb() dmb(oshst)
  118. #define __iowmb() dma_wmb()
  119. #define dma_rmb() dmb(oshld)
  120. #define __iormb(v) \
  121. ({ \
  122. unsigned long tmp; \
  123. \
  124. dma_rmb(); \
  125. \
  126. /* \
  127. * Courtesy of arch/arm64/include/asm/io.h: \
  128. * Create a dummy control dependency from the IO read to any \
  129. * later instructions. This ensures that a subsequent call \
  130. * to udelay() will be ordered due to the ISB in __delay(). \
  131. */ \
  132. asm volatile("eor %0, %1, %1\n" \
  133. "cbnz %0, ." \
  134. : "=r" (tmp) : "r" ((unsigned long)(v)) \
  135. : "memory"); \
  136. })
  137. static __always_inline void __raw_writel(u32 val, volatile void *addr)
  138. {
  139. asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr));
  140. }
  141. static __always_inline u32 __raw_readl(const volatile void *addr)
  142. {
  143. u32 val;
  144. asm volatile("ldr %w0, [%1]" : "=r" (val) : "r" (addr));
  145. return val;
  146. }
  147. static __always_inline void __raw_writeq(u64 val, volatile void *addr)
  148. {
  149. asm volatile("str %0, [%1]" : : "rZ" (val), "r" (addr));
  150. }
  151. static __always_inline u64 __raw_readq(const volatile void *addr)
  152. {
  153. u64 val;
  154. asm volatile("ldr %0, [%1]" : "=r" (val) : "r" (addr));
  155. return val;
  156. }
  157. #define writel_relaxed(v,c) ((void)__raw_writel((__force u32)cpu_to_le32(v),(c)))
  158. #define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32)__raw_readl(c)); __r; })
  159. #define writeq_relaxed(v,c) ((void)__raw_writeq((__force u64)cpu_to_le64(v),(c)))
  160. #define readq_relaxed(c) ({ u64 __r = le64_to_cpu((__force __le64)__raw_readq(c)); __r; })
  161. #define writel(v,c) ({ __iowmb(); writel_relaxed((v),(c));})
  162. #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(__v); __v; })
  163. #define writeq(v,c) ({ __iowmb(); writeq_relaxed((v),(c));})
  164. #define readq(c) ({ u64 __v = readq_relaxed(c); __iormb(__v); __v; })
  165. static inline void local_irq_enable(void)
  166. {
  167. asm volatile("msr daifclr, #3" : : : "memory");
  168. }
  169. static inline void local_irq_disable(void)
  170. {
  171. asm volatile("msr daifset, #3" : : : "memory");
  172. }
  173. /**
  174. * struct arm_smccc_res - Result from SMC/HVC call
  175. * @a0-a3 result values from registers 0 to 3
  176. */
  177. struct arm_smccc_res {
  178. unsigned long a0;
  179. unsigned long a1;
  180. unsigned long a2;
  181. unsigned long a3;
  182. };
  183. /**
  184. * smccc_hvc - Invoke a SMCCC function using the hvc conduit
  185. * @function_id: the SMCCC function to be called
  186. * @arg0-arg6: SMCCC function arguments, corresponding to registers x1-x7
  187. * @res: pointer to write the return values from registers x0-x3
  188. *
  189. */
  190. void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
  191. uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
  192. uint64_t arg6, struct arm_smccc_res *res);
  193. /**
  194. * smccc_smc - Invoke a SMCCC function using the smc conduit
  195. * @function_id: the SMCCC function to be called
  196. * @arg0-arg6: SMCCC function arguments, corresponding to registers x1-x7
  197. * @res: pointer to write the return values from registers x0-x3
  198. *
  199. */
  200. void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
  201. uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
  202. uint64_t arg6, struct arm_smccc_res *res);
  203. /* Execute a Wait For Interrupt instruction. */
  204. void wfi(void);
  205. #endif /* SELFTEST_KVM_PROCESSOR_H */