kvm_emulate.h 5.8 KB

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