kvm_emulate.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef __ARM_KVM_EMULATE_H__
  19. #define __ARM_KVM_EMULATE_H__
  20. #include <linux/kvm_host.h>
  21. #include <asm/kvm_asm.h>
  22. #include <asm/kvm_mmio.h>
  23. #include <asm/kvm_arm.h>
  24. #include <asm/cputype.h>
  25. /* arm64 compatibility macros */
  26. #define PSR_AA32_MODE_FIQ FIQ_MODE
  27. #define PSR_AA32_MODE_SVC SVC_MODE
  28. #define PSR_AA32_MODE_ABT ABT_MODE
  29. #define PSR_AA32_MODE_UND UND_MODE
  30. #define PSR_AA32_T_BIT PSR_T_BIT
  31. #define PSR_AA32_F_BIT PSR_F_BIT
  32. #define PSR_AA32_I_BIT PSR_I_BIT
  33. #define PSR_AA32_A_BIT PSR_A_BIT
  34. #define PSR_AA32_E_BIT PSR_E_BIT
  35. #define PSR_AA32_IT_MASK PSR_IT_MASK
  36. #define PSR_AA32_GE_MASK 0x000f0000
  37. #define PSR_AA32_DIT_BIT 0x00200000
  38. #define PSR_AA32_PAN_BIT 0x00400000
  39. #define PSR_AA32_SSBS_BIT 0x00800000
  40. #define PSR_AA32_Q_BIT PSR_Q_BIT
  41. #define PSR_AA32_V_BIT PSR_V_BIT
  42. #define PSR_AA32_C_BIT PSR_C_BIT
  43. #define PSR_AA32_Z_BIT PSR_Z_BIT
  44. #define PSR_AA32_N_BIT PSR_N_BIT
  45. unsigned long *vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num);
  46. static inline unsigned long *vcpu_reg32(struct kvm_vcpu *vcpu, u8 reg_num)
  47. {
  48. return vcpu_reg(vcpu, reg_num);
  49. }
  50. unsigned long *__vcpu_spsr(struct kvm_vcpu *vcpu);
  51. static inline unsigned long vpcu_read_spsr(struct kvm_vcpu *vcpu)
  52. {
  53. return *__vcpu_spsr(vcpu);
  54. }
  55. static inline void vcpu_write_spsr(struct kvm_vcpu *vcpu, unsigned long v)
  56. {
  57. *__vcpu_spsr(vcpu) = v;
  58. }
  59. static inline unsigned long host_spsr_to_spsr32(unsigned long spsr)
  60. {
  61. return spsr;
  62. }
  63. static inline unsigned long vcpu_get_reg(struct kvm_vcpu *vcpu,
  64. u8 reg_num)
  65. {
  66. return *vcpu_reg(vcpu, reg_num);
  67. }
  68. static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
  69. unsigned long val)
  70. {
  71. *vcpu_reg(vcpu, reg_num) = val;
  72. }
  73. bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
  74. void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
  75. void kvm_inject_undef32(struct kvm_vcpu *vcpu);
  76. void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr);
  77. void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr);
  78. void kvm_inject_vabt(struct kvm_vcpu *vcpu);
  79. static inline void kvm_inject_undefined(struct kvm_vcpu *vcpu)
  80. {
  81. kvm_inject_undef32(vcpu);
  82. }
  83. static inline void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr)
  84. {
  85. kvm_inject_dabt32(vcpu, addr);
  86. }
  87. static inline void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr)
  88. {
  89. kvm_inject_pabt32(vcpu, addr);
  90. }
  91. static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
  92. {
  93. return kvm_condition_valid32(vcpu);
  94. }
  95. static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
  96. {
  97. kvm_skip_instr32(vcpu, is_wide_instr);
  98. }
  99. static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
  100. {
  101. vcpu->arch.hcr = HCR_GUEST_MASK;
  102. }
  103. static inline unsigned long *vcpu_hcr(const struct kvm_vcpu *vcpu)
  104. {
  105. return (unsigned long *)&vcpu->arch.hcr;
  106. }
  107. static inline void vcpu_clear_wfe_traps(struct kvm_vcpu *vcpu)
  108. {
  109. vcpu->arch.hcr &= ~HCR_TWE;
  110. }
  111. static inline void vcpu_set_wfe_traps(struct kvm_vcpu *vcpu)
  112. {
  113. vcpu->arch.hcr |= HCR_TWE;
  114. }
  115. static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
  116. {
  117. return true;
  118. }
  119. static inline unsigned long *vcpu_pc(struct kvm_vcpu *vcpu)
  120. {
  121. return &vcpu->arch.ctxt.gp_regs.usr_regs.ARM_pc;
  122. }
  123. static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
  124. {
  125. return (unsigned long *)&vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr;
  126. }
  127. static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
  128. {
  129. *vcpu_cpsr(vcpu) |= PSR_T_BIT;
  130. }
  131. static inline bool mode_has_spsr(struct kvm_vcpu *vcpu)
  132. {
  133. unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
  134. return (cpsr_mode > USR_MODE && cpsr_mode < SYSTEM_MODE);
  135. }
  136. static inline bool vcpu_mode_priv(struct kvm_vcpu *vcpu)
  137. {
  138. unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
  139. return cpsr_mode > USR_MODE;
  140. }
  141. static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu)
  142. {
  143. return vcpu->arch.fault.hsr;
  144. }
  145. static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
  146. {
  147. u32 hsr = kvm_vcpu_get_hsr(vcpu);
  148. if (hsr & HSR_CV)
  149. return (hsr & HSR_COND) >> HSR_COND_SHIFT;
  150. return -1;
  151. }
  152. static inline unsigned long kvm_vcpu_get_hfar(struct kvm_vcpu *vcpu)
  153. {
  154. return vcpu->arch.fault.hxfar;
  155. }
  156. static inline phys_addr_t kvm_vcpu_get_fault_ipa(struct kvm_vcpu *vcpu)
  157. {
  158. return ((phys_addr_t)vcpu->arch.fault.hpfar & HPFAR_MASK) << 8;
  159. }
  160. static inline bool kvm_vcpu_dabt_isvalid(struct kvm_vcpu *vcpu)
  161. {
  162. return kvm_vcpu_get_hsr(vcpu) & HSR_ISV;
  163. }
  164. static inline bool kvm_vcpu_dabt_iswrite(struct kvm_vcpu *vcpu)
  165. {
  166. return kvm_vcpu_get_hsr(vcpu) & HSR_WNR;
  167. }
  168. static inline bool kvm_vcpu_dabt_issext(struct kvm_vcpu *vcpu)
  169. {
  170. return kvm_vcpu_get_hsr(vcpu) & HSR_SSE;
  171. }
  172. static inline bool kvm_vcpu_dabt_issf(const struct kvm_vcpu *vcpu)
  173. {
  174. return false;
  175. }
  176. static inline int kvm_vcpu_dabt_get_rd(struct kvm_vcpu *vcpu)
  177. {
  178. return (kvm_vcpu_get_hsr(vcpu) & HSR_SRT_MASK) >> HSR_SRT_SHIFT;
  179. }
  180. static inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu)
  181. {
  182. return kvm_vcpu_get_hsr(vcpu) & HSR_DABT_S1PTW;
  183. }
  184. static inline bool kvm_vcpu_dabt_is_cm(struct kvm_vcpu *vcpu)
  185. {
  186. return !!(kvm_vcpu_get_hsr(vcpu) & HSR_DABT_CM);
  187. }
  188. /* Get Access Size from a data abort */
  189. static inline int kvm_vcpu_dabt_get_as(struct kvm_vcpu *vcpu)
  190. {
  191. switch ((kvm_vcpu_get_hsr(vcpu) >> 22) & 0x3) {
  192. case 0:
  193. return 1;
  194. case 1:
  195. return 2;
  196. case 2:
  197. return 4;
  198. default:
  199. kvm_err("Hardware is weird: SAS 0b11 is reserved\n");
  200. return -EFAULT;
  201. }
  202. }
  203. /* This one is not specific to Data Abort */
  204. static inline bool kvm_vcpu_trap_il_is32bit(struct kvm_vcpu *vcpu)
  205. {
  206. return kvm_vcpu_get_hsr(vcpu) & HSR_IL;
  207. }
  208. static inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu)
  209. {
  210. return kvm_vcpu_get_hsr(vcpu) >> HSR_EC_SHIFT;
  211. }
  212. static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu)
  213. {
  214. return kvm_vcpu_trap_get_class(vcpu) == HSR_EC_IABT;
  215. }
  216. static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu)
  217. {
  218. return kvm_vcpu_trap_is_iabt(vcpu) && !kvm_vcpu_abt_iss1tw(vcpu);
  219. }
  220. static inline u8 kvm_vcpu_trap_get_fault(struct kvm_vcpu *vcpu)
  221. {
  222. return kvm_vcpu_get_hsr(vcpu) & HSR_FSC;
  223. }
  224. static inline u8 kvm_vcpu_trap_get_fault_type(struct kvm_vcpu *vcpu)
  225. {
  226. return kvm_vcpu_get_hsr(vcpu) & HSR_FSC_TYPE;
  227. }
  228. static inline bool kvm_vcpu_dabt_isextabt(struct kvm_vcpu *vcpu)
  229. {
  230. switch (kvm_vcpu_trap_get_fault(vcpu)) {
  231. case FSC_SEA:
  232. case FSC_SEA_TTW0:
  233. case FSC_SEA_TTW1:
  234. case FSC_SEA_TTW2:
  235. case FSC_SEA_TTW3:
  236. case FSC_SECC:
  237. case FSC_SECC_TTW0:
  238. case FSC_SECC_TTW1:
  239. case FSC_SECC_TTW2:
  240. case FSC_SECC_TTW3:
  241. return true;
  242. default:
  243. return false;
  244. }
  245. }
  246. static inline u32 kvm_vcpu_hvc_get_imm(struct kvm_vcpu *vcpu)
  247. {
  248. return kvm_vcpu_get_hsr(vcpu) & HSR_HVC_IMM_MASK;
  249. }
  250. static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
  251. {
  252. return vcpu_cp15(vcpu, c0_MPIDR) & MPIDR_HWID_BITMASK;
  253. }
  254. static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
  255. {
  256. *vcpu_cpsr(vcpu) |= PSR_E_BIT;
  257. }
  258. static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
  259. {
  260. return !!(*vcpu_cpsr(vcpu) & PSR_E_BIT);
  261. }
  262. static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
  263. unsigned long data,
  264. unsigned int len)
  265. {
  266. if (kvm_vcpu_is_be(vcpu)) {
  267. switch (len) {
  268. case 1:
  269. return data & 0xff;
  270. case 2:
  271. return be16_to_cpu(data & 0xffff);
  272. default:
  273. return be32_to_cpu(data);
  274. }
  275. } else {
  276. switch (len) {
  277. case 1:
  278. return data & 0xff;
  279. case 2:
  280. return le16_to_cpu(data & 0xffff);
  281. default:
  282. return le32_to_cpu(data);
  283. }
  284. }
  285. }
  286. static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
  287. unsigned long data,
  288. unsigned int len)
  289. {
  290. if (kvm_vcpu_is_be(vcpu)) {
  291. switch (len) {
  292. case 1:
  293. return data & 0xff;
  294. case 2:
  295. return cpu_to_be16(data & 0xffff);
  296. default:
  297. return cpu_to_be32(data);
  298. }
  299. } else {
  300. switch (len) {
  301. case 1:
  302. return data & 0xff;
  303. case 2:
  304. return cpu_to_le16(data & 0xffff);
  305. default:
  306. return cpu_to_le32(data);
  307. }
  308. }
  309. }
  310. #endif /* __ARM_KVM_EMULATE_H__ */