kvm_emulate.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. unsigned long *vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num);
  26. unsigned long *vcpu_spsr(struct kvm_vcpu *vcpu);
  27. static inline unsigned long vcpu_get_reg(struct kvm_vcpu *vcpu,
  28. u8 reg_num)
  29. {
  30. return *vcpu_reg(vcpu, reg_num);
  31. }
  32. static inline void vcpu_set_reg(struct kvm_vcpu *vcpu, u8 reg_num,
  33. unsigned long val)
  34. {
  35. *vcpu_reg(vcpu, reg_num) = val;
  36. }
  37. bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
  38. void kvm_skip_instr32(struct kvm_vcpu *vcpu, bool is_wide_instr);
  39. void kvm_inject_undefined(struct kvm_vcpu *vcpu);
  40. void kvm_inject_vabt(struct kvm_vcpu *vcpu);
  41. void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr);
  42. void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr);
  43. static inline bool kvm_condition_valid(const struct kvm_vcpu *vcpu)
  44. {
  45. return kvm_condition_valid32(vcpu);
  46. }
  47. static inline void kvm_skip_instr(struct kvm_vcpu *vcpu, bool is_wide_instr)
  48. {
  49. kvm_skip_instr32(vcpu, is_wide_instr);
  50. }
  51. static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
  52. {
  53. vcpu->arch.hcr = HCR_GUEST_MASK;
  54. }
  55. static inline unsigned long vcpu_get_hcr(const struct kvm_vcpu *vcpu)
  56. {
  57. return vcpu->arch.hcr;
  58. }
  59. static inline void vcpu_set_hcr(struct kvm_vcpu *vcpu, unsigned long hcr)
  60. {
  61. vcpu->arch.hcr = hcr;
  62. }
  63. static inline bool vcpu_mode_is_32bit(const struct kvm_vcpu *vcpu)
  64. {
  65. return 1;
  66. }
  67. static inline unsigned long *vcpu_pc(struct kvm_vcpu *vcpu)
  68. {
  69. return &vcpu->arch.ctxt.gp_regs.usr_regs.ARM_pc;
  70. }
  71. static inline unsigned long *vcpu_cpsr(const struct kvm_vcpu *vcpu)
  72. {
  73. return (unsigned long *)&vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr;
  74. }
  75. static inline void vcpu_set_thumb(struct kvm_vcpu *vcpu)
  76. {
  77. *vcpu_cpsr(vcpu) |= PSR_T_BIT;
  78. }
  79. static inline bool mode_has_spsr(struct kvm_vcpu *vcpu)
  80. {
  81. unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
  82. return (cpsr_mode > USR_MODE && cpsr_mode < SYSTEM_MODE);
  83. }
  84. static inline bool vcpu_mode_priv(struct kvm_vcpu *vcpu)
  85. {
  86. unsigned long cpsr_mode = vcpu->arch.ctxt.gp_regs.usr_regs.ARM_cpsr & MODE_MASK;
  87. return cpsr_mode > USR_MODE;;
  88. }
  89. static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu)
  90. {
  91. return vcpu->arch.fault.hsr;
  92. }
  93. static inline int kvm_vcpu_get_condition(const struct kvm_vcpu *vcpu)
  94. {
  95. u32 hsr = kvm_vcpu_get_hsr(vcpu);
  96. if (hsr & HSR_CV)
  97. return (hsr & HSR_COND) >> HSR_COND_SHIFT;
  98. return -1;
  99. }
  100. static inline unsigned long kvm_vcpu_get_hfar(struct kvm_vcpu *vcpu)
  101. {
  102. return vcpu->arch.fault.hxfar;
  103. }
  104. static inline phys_addr_t kvm_vcpu_get_fault_ipa(struct kvm_vcpu *vcpu)
  105. {
  106. return ((phys_addr_t)vcpu->arch.fault.hpfar & HPFAR_MASK) << 8;
  107. }
  108. static inline bool kvm_vcpu_dabt_isvalid(struct kvm_vcpu *vcpu)
  109. {
  110. return kvm_vcpu_get_hsr(vcpu) & HSR_ISV;
  111. }
  112. static inline bool kvm_vcpu_dabt_iswrite(struct kvm_vcpu *vcpu)
  113. {
  114. return kvm_vcpu_get_hsr(vcpu) & HSR_WNR;
  115. }
  116. static inline bool kvm_vcpu_dabt_issext(struct kvm_vcpu *vcpu)
  117. {
  118. return kvm_vcpu_get_hsr(vcpu) & HSR_SSE;
  119. }
  120. static inline int kvm_vcpu_dabt_get_rd(struct kvm_vcpu *vcpu)
  121. {
  122. return (kvm_vcpu_get_hsr(vcpu) & HSR_SRT_MASK) >> HSR_SRT_SHIFT;
  123. }
  124. static inline bool kvm_vcpu_dabt_isextabt(struct kvm_vcpu *vcpu)
  125. {
  126. return kvm_vcpu_get_hsr(vcpu) & HSR_DABT_EA;
  127. }
  128. static inline bool kvm_vcpu_dabt_iss1tw(struct kvm_vcpu *vcpu)
  129. {
  130. return kvm_vcpu_get_hsr(vcpu) & HSR_DABT_S1PTW;
  131. }
  132. static inline bool kvm_vcpu_dabt_is_cm(struct kvm_vcpu *vcpu)
  133. {
  134. return !!(kvm_vcpu_get_hsr(vcpu) & HSR_DABT_CM);
  135. }
  136. /* Get Access Size from a data abort */
  137. static inline int kvm_vcpu_dabt_get_as(struct kvm_vcpu *vcpu)
  138. {
  139. switch ((kvm_vcpu_get_hsr(vcpu) >> 22) & 0x3) {
  140. case 0:
  141. return 1;
  142. case 1:
  143. return 2;
  144. case 2:
  145. return 4;
  146. default:
  147. kvm_err("Hardware is weird: SAS 0b11 is reserved\n");
  148. return -EFAULT;
  149. }
  150. }
  151. /* This one is not specific to Data Abort */
  152. static inline bool kvm_vcpu_trap_il_is32bit(struct kvm_vcpu *vcpu)
  153. {
  154. return kvm_vcpu_get_hsr(vcpu) & HSR_IL;
  155. }
  156. static inline u8 kvm_vcpu_trap_get_class(struct kvm_vcpu *vcpu)
  157. {
  158. return kvm_vcpu_get_hsr(vcpu) >> HSR_EC_SHIFT;
  159. }
  160. static inline bool kvm_vcpu_trap_is_iabt(struct kvm_vcpu *vcpu)
  161. {
  162. return kvm_vcpu_trap_get_class(vcpu) == HSR_EC_IABT;
  163. }
  164. static inline u8 kvm_vcpu_trap_get_fault(struct kvm_vcpu *vcpu)
  165. {
  166. return kvm_vcpu_get_hsr(vcpu) & HSR_FSC;
  167. }
  168. static inline u8 kvm_vcpu_trap_get_fault_type(struct kvm_vcpu *vcpu)
  169. {
  170. return kvm_vcpu_get_hsr(vcpu) & HSR_FSC_TYPE;
  171. }
  172. static inline u32 kvm_vcpu_hvc_get_imm(struct kvm_vcpu *vcpu)
  173. {
  174. return kvm_vcpu_get_hsr(vcpu) & HSR_HVC_IMM_MASK;
  175. }
  176. static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu)
  177. {
  178. return vcpu_cp15(vcpu, c0_MPIDR) & MPIDR_HWID_BITMASK;
  179. }
  180. static inline void kvm_vcpu_set_be(struct kvm_vcpu *vcpu)
  181. {
  182. *vcpu_cpsr(vcpu) |= PSR_E_BIT;
  183. }
  184. static inline bool kvm_vcpu_is_be(struct kvm_vcpu *vcpu)
  185. {
  186. return !!(*vcpu_cpsr(vcpu) & PSR_E_BIT);
  187. }
  188. static inline unsigned long vcpu_data_guest_to_host(struct kvm_vcpu *vcpu,
  189. unsigned long data,
  190. unsigned int len)
  191. {
  192. if (kvm_vcpu_is_be(vcpu)) {
  193. switch (len) {
  194. case 1:
  195. return data & 0xff;
  196. case 2:
  197. return be16_to_cpu(data & 0xffff);
  198. default:
  199. return be32_to_cpu(data);
  200. }
  201. } else {
  202. switch (len) {
  203. case 1:
  204. return data & 0xff;
  205. case 2:
  206. return le16_to_cpu(data & 0xffff);
  207. default:
  208. return le32_to_cpu(data);
  209. }
  210. }
  211. }
  212. static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
  213. unsigned long data,
  214. unsigned int len)
  215. {
  216. if (kvm_vcpu_is_be(vcpu)) {
  217. switch (len) {
  218. case 1:
  219. return data & 0xff;
  220. case 2:
  221. return cpu_to_be16(data & 0xffff);
  222. default:
  223. return cpu_to_be32(data);
  224. }
  225. } else {
  226. switch (len) {
  227. case 1:
  228. return data & 0xff;
  229. case 2:
  230. return cpu_to_le16(data & 0xffff);
  231. default:
  232. return cpu_to_le32(data);
  233. }
  234. }
  235. }
  236. #endif /* __ARM_KVM_EMULATE_H__ */