kvm_emulate.h 6.0 KB

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