sys_regs.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * Derived from arch/arm/kvm/coproc.h
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Authors: Christoffer Dall <c.dall@virtualopensystems.com>
  9. */
  10. #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
  11. #define __ARM64_KVM_SYS_REGS_LOCAL_H__
  12. #include <linux/bsearch.h>
  13. #define reg_to_encoding(x) \
  14. sys_reg((u32)(x)->Op0, (u32)(x)->Op1, \
  15. (u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2)
  16. struct sys_reg_params {
  17. u8 Op0;
  18. u8 Op1;
  19. u8 CRn;
  20. u8 CRm;
  21. u8 Op2;
  22. u64 regval;
  23. bool is_write;
  24. };
  25. #define encoding_to_params(reg) \
  26. ((struct sys_reg_params){ .Op0 = sys_reg_Op0(reg), \
  27. .Op1 = sys_reg_Op1(reg), \
  28. .CRn = sys_reg_CRn(reg), \
  29. .CRm = sys_reg_CRm(reg), \
  30. .Op2 = sys_reg_Op2(reg) })
  31. #define esr_sys64_to_params(esr) \
  32. ((struct sys_reg_params){ .Op0 = ((esr) >> 20) & 3, \
  33. .Op1 = ((esr) >> 14) & 0x7, \
  34. .CRn = ((esr) >> 10) & 0xf, \
  35. .CRm = ((esr) >> 1) & 0xf, \
  36. .Op2 = ((esr) >> 17) & 0x7, \
  37. .is_write = !((esr) & 1) })
  38. #define esr_cp1x_32_to_params(esr) \
  39. ((struct sys_reg_params){ .Op1 = ((esr) >> 14) & 0x7, \
  40. .CRn = ((esr) >> 10) & 0xf, \
  41. .CRm = ((esr) >> 1) & 0xf, \
  42. .Op2 = ((esr) >> 17) & 0x7, \
  43. .is_write = !((esr) & 1) })
  44. /*
  45. * The Feature ID space is defined as the System register space in AArch64
  46. * with op0==3, op1=={0, 1, 3}, CRn==0, CRm=={0-7}, op2=={0-7}.
  47. */
  48. static inline bool in_feat_id_space(struct sys_reg_params *p)
  49. {
  50. return (p->Op0 == 3 && !(p->Op1 & 0b100) && p->Op1 != 2 &&
  51. p->CRn == 0 && !(p->CRm & 0b1000));
  52. }
  53. struct sys_reg_desc {
  54. /* Sysreg string for debug */
  55. const char *name;
  56. enum {
  57. AA32_DIRECT,
  58. AA32_LO,
  59. AA32_HI,
  60. } aarch32_map;
  61. /* MRS/MSR instruction which accesses it. */
  62. u8 Op0;
  63. u8 Op1;
  64. u8 CRn;
  65. u8 CRm;
  66. u8 Op2;
  67. /* Trapped access from guest, if non-NULL. */
  68. bool (*access)(struct kvm_vcpu *,
  69. struct sys_reg_params *,
  70. const struct sys_reg_desc *);
  71. /*
  72. * Initialization for vcpu. Return initialized value, or KVM
  73. * sanitized value for ID registers.
  74. */
  75. u64 (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
  76. /* Index into sys_reg[], or 0 if we don't need to save it. */
  77. int reg;
  78. /* Value (usually reset value), or write mask for idregs */
  79. u64 val;
  80. /* Custom get/set_user functions, fallback to generic if NULL */
  81. int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
  82. u64 *val);
  83. int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
  84. u64 val);
  85. /* Return mask of REG_* runtime visibility overrides */
  86. unsigned int (*visibility)(const struct kvm_vcpu *vcpu,
  87. const struct sys_reg_desc *rd);
  88. };
  89. #define REG_HIDDEN (1 << 0) /* hidden from userspace and guest */
  90. #define REG_RAZ (1 << 1) /* RAZ from userspace and guest */
  91. #define REG_USER_WI (1 << 2) /* WI from userspace only */
  92. static __printf(2, 3)
  93. inline void print_sys_reg_msg(const struct sys_reg_params *p,
  94. char *fmt, ...)
  95. {
  96. va_list va;
  97. va_start(va, fmt);
  98. /* Look, we even formatted it for you to paste into the table! */
  99. kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
  100. &(struct va_format){ fmt, &va },
  101. p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, str_write_read(p->is_write));
  102. va_end(va);
  103. }
  104. static inline void print_sys_reg_instr(const struct sys_reg_params *p)
  105. {
  106. /* GCC warns on an empty format string */
  107. print_sys_reg_msg(p, "%s", "");
  108. }
  109. static inline bool ignore_write(struct kvm_vcpu *vcpu,
  110. const struct sys_reg_params *p)
  111. {
  112. return true;
  113. }
  114. static inline bool read_zero(struct kvm_vcpu *vcpu,
  115. struct sys_reg_params *p)
  116. {
  117. p->regval = 0;
  118. return true;
  119. }
  120. /* Reset functions */
  121. static inline u64 reset_unknown(struct kvm_vcpu *vcpu,
  122. const struct sys_reg_desc *r)
  123. {
  124. BUG_ON(!r->reg);
  125. BUG_ON(r->reg >= NR_SYS_REGS);
  126. __vcpu_assign_sys_reg(vcpu, r->reg, 0x1de7ec7edbadc0deULL);
  127. return __vcpu_sys_reg(vcpu, r->reg);
  128. }
  129. static inline u64 reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
  130. {
  131. BUG_ON(!r->reg);
  132. BUG_ON(r->reg >= NR_SYS_REGS);
  133. __vcpu_assign_sys_reg(vcpu, r->reg, r->val);
  134. return __vcpu_sys_reg(vcpu, r->reg);
  135. }
  136. static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
  137. const struct sys_reg_desc *r)
  138. {
  139. if (likely(!r->visibility))
  140. return 0;
  141. return r->visibility(vcpu, r);
  142. }
  143. static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
  144. const struct sys_reg_desc *r)
  145. {
  146. return sysreg_visibility(vcpu, r) & REG_HIDDEN;
  147. }
  148. static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
  149. const struct sys_reg_desc *r)
  150. {
  151. return sysreg_visibility(vcpu, r) & REG_RAZ;
  152. }
  153. static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
  154. const struct sys_reg_desc *r)
  155. {
  156. return sysreg_visibility(vcpu, r) & REG_USER_WI;
  157. }
  158. static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
  159. const struct sys_reg_desc *i2)
  160. {
  161. BUG_ON(i1 == i2);
  162. if (!i1)
  163. return 1;
  164. else if (!i2)
  165. return -1;
  166. if (i1->Op0 != i2->Op0)
  167. return i1->Op0 - i2->Op0;
  168. if (i1->Op1 != i2->Op1)
  169. return i1->Op1 - i2->Op1;
  170. if (i1->CRn != i2->CRn)
  171. return i1->CRn - i2->CRn;
  172. if (i1->CRm != i2->CRm)
  173. return i1->CRm - i2->CRm;
  174. return i1->Op2 - i2->Op2;
  175. }
  176. static inline int match_sys_reg(const void *key, const void *elt)
  177. {
  178. const unsigned long pval = (unsigned long)key;
  179. const struct sys_reg_desc *r = elt;
  180. return pval - reg_to_encoding(r);
  181. }
  182. static inline const struct sys_reg_desc *
  183. find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
  184. unsigned int num)
  185. {
  186. unsigned long pval = reg_to_encoding(params);
  187. return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
  188. }
  189. const struct sys_reg_desc *get_reg_by_id(u64 id,
  190. const struct sys_reg_desc table[],
  191. unsigned int num);
  192. int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  193. int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
  194. int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
  195. const struct sys_reg_desc table[], unsigned int num);
  196. int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
  197. const struct sys_reg_desc table[], unsigned int num);
  198. bool triage_sysreg_trap(struct kvm_vcpu *vcpu, int *sr_index);
  199. int kvm_finalize_sys_regs(struct kvm_vcpu *vcpu);
  200. #define AA32(_x) .aarch32_map = AA32_##_x
  201. #define Op0(_x) .Op0 = _x
  202. #define Op1(_x) .Op1 = _x
  203. #define CRn(_x) .CRn = _x
  204. #define CRm(_x) .CRm = _x
  205. #define Op2(_x) .Op2 = _x
  206. #define SYS_DESC(reg) \
  207. .name = #reg, \
  208. Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)), \
  209. CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \
  210. Op2(sys_reg_Op2(reg))
  211. #define CP15_SYS_DESC(reg) \
  212. .name = #reg, \
  213. .aarch32_map = AA32_DIRECT, \
  214. Op0(0), Op1(sys_reg_Op1(reg)), \
  215. CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \
  216. Op2(sys_reg_Op2(reg))
  217. #define ID_REG_LIMIT_FIELD_ENUM(val, reg, field, limit) \
  218. ({ \
  219. u64 __f_val = FIELD_GET(reg##_##field##_MASK, val); \
  220. (val) &= ~reg##_##field##_MASK; \
  221. (val) |= FIELD_PREP(reg##_##field##_MASK, \
  222. min(__f_val, \
  223. (u64)SYS_FIELD_VALUE(reg, field, limit))); \
  224. (val); \
  225. })
  226. #define TO_ARM64_SYS_REG(r) ARM64_SYS_REG(sys_reg_Op0(SYS_ ## r), \
  227. sys_reg_Op1(SYS_ ## r), \
  228. sys_reg_CRn(SYS_ ## r), \
  229. sys_reg_CRm(SYS_ ## r), \
  230. sys_reg_Op2(SYS_ ## r))
  231. #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */