kvm_emulate.h 5.7 KB

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