mmio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <linux/kvm_host.h>
  19. #include <asm/kvm_mmio.h>
  20. #include <asm/kvm_emulate.h>
  21. #include <trace/events/kvm.h>
  22. #include "trace.h"
  23. void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data)
  24. {
  25. void *datap = NULL;
  26. union {
  27. u8 byte;
  28. u16 hword;
  29. u32 word;
  30. u64 dword;
  31. } tmp;
  32. switch (len) {
  33. case 1:
  34. tmp.byte = data;
  35. datap = &tmp.byte;
  36. break;
  37. case 2:
  38. tmp.hword = data;
  39. datap = &tmp.hword;
  40. break;
  41. case 4:
  42. tmp.word = data;
  43. datap = &tmp.word;
  44. break;
  45. case 8:
  46. tmp.dword = data;
  47. datap = &tmp.dword;
  48. break;
  49. }
  50. memcpy(buf, datap, len);
  51. }
  52. unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len)
  53. {
  54. unsigned long data = 0;
  55. union {
  56. u16 hword;
  57. u32 word;
  58. u64 dword;
  59. } tmp;
  60. switch (len) {
  61. case 1:
  62. data = *(u8 *)buf;
  63. break;
  64. case 2:
  65. memcpy(&tmp.hword, buf, len);
  66. data = tmp.hword;
  67. break;
  68. case 4:
  69. memcpy(&tmp.word, buf, len);
  70. data = tmp.word;
  71. break;
  72. case 8:
  73. memcpy(&tmp.dword, buf, len);
  74. data = tmp.dword;
  75. break;
  76. }
  77. return data;
  78. }
  79. /**
  80. * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
  81. * or in-kernel IO emulation
  82. *
  83. * @vcpu: The VCPU pointer
  84. * @run: The VCPU run struct containing the mmio data
  85. */
  86. int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
  87. {
  88. unsigned long data;
  89. unsigned int len;
  90. int mask;
  91. /* Detect an already handled MMIO return */
  92. if (unlikely(!vcpu->mmio_needed))
  93. return 0;
  94. vcpu->mmio_needed = 0;
  95. if (!run->mmio.is_write) {
  96. len = run->mmio.len;
  97. if (len > sizeof(unsigned long))
  98. return -EINVAL;
  99. data = kvm_mmio_read_buf(run->mmio.data, len);
  100. if (vcpu->arch.mmio_decode.sign_extend &&
  101. len < sizeof(unsigned long)) {
  102. mask = 1U << ((len * 8) - 1);
  103. data = (data ^ mask) - mask;
  104. }
  105. if (!vcpu->arch.mmio_decode.sixty_four)
  106. data = data & 0xffffffff;
  107. trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
  108. &data);
  109. data = vcpu_data_host_to_guest(vcpu, data, len);
  110. vcpu_set_reg(vcpu, vcpu->arch.mmio_decode.rt, data);
  111. }
  112. /*
  113. * The MMIO instruction is emulated and should not be re-executed
  114. * in the guest.
  115. */
  116. kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  117. return 0;
  118. }
  119. static int decode_hsr(struct kvm_vcpu *vcpu, bool *is_write, int *len)
  120. {
  121. unsigned long rt;
  122. int access_size;
  123. bool sign_extend;
  124. bool sixty_four;
  125. if (kvm_vcpu_dabt_iss1tw(vcpu)) {
  126. /* page table accesses IO mem: tell guest to fix its TTBR */
  127. kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  128. return 1;
  129. }
  130. access_size = kvm_vcpu_dabt_get_as(vcpu);
  131. if (unlikely(access_size < 0))
  132. return access_size;
  133. *is_write = kvm_vcpu_dabt_iswrite(vcpu);
  134. sign_extend = kvm_vcpu_dabt_issext(vcpu);
  135. sixty_four = kvm_vcpu_dabt_issf(vcpu);
  136. rt = kvm_vcpu_dabt_get_rd(vcpu);
  137. *len = access_size;
  138. vcpu->arch.mmio_decode.sign_extend = sign_extend;
  139. vcpu->arch.mmio_decode.rt = rt;
  140. vcpu->arch.mmio_decode.sixty_four = sixty_four;
  141. return 0;
  142. }
  143. int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
  144. phys_addr_t fault_ipa)
  145. {
  146. unsigned long data;
  147. unsigned long rt;
  148. int ret;
  149. bool is_write;
  150. int len;
  151. u8 data_buf[8];
  152. /*
  153. * Prepare MMIO operation. First decode the syndrome data we get
  154. * from the CPU. Then try if some in-kernel emulation feels
  155. * responsible, otherwise let user space do its magic.
  156. */
  157. if (kvm_vcpu_dabt_isvalid(vcpu)) {
  158. ret = decode_hsr(vcpu, &is_write, &len);
  159. if (ret)
  160. return ret;
  161. } else {
  162. kvm_err("load/store instruction decoding not implemented\n");
  163. return -ENOSYS;
  164. }
  165. rt = vcpu->arch.mmio_decode.rt;
  166. if (is_write) {
  167. data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
  168. len);
  169. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
  170. kvm_mmio_write_buf(data_buf, len, data);
  171. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
  172. data_buf);
  173. } else {
  174. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
  175. fault_ipa, NULL);
  176. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
  177. data_buf);
  178. }
  179. /* Now prepare kvm_run for the potential return to userland. */
  180. run->mmio.is_write = is_write;
  181. run->mmio.phys_addr = fault_ipa;
  182. run->mmio.len = len;
  183. vcpu->mmio_needed = 1;
  184. if (!ret) {
  185. /* We handled the access successfully in the kernel. */
  186. if (!is_write)
  187. memcpy(run->mmio.data, data_buf, len);
  188. vcpu->stat.mmio_exit_kernel++;
  189. kvm_handle_mmio_return(vcpu, run);
  190. return 1;
  191. }
  192. if (is_write)
  193. memcpy(run->mmio.data, data_buf, len);
  194. vcpu->stat.mmio_exit_user++;
  195. run->exit_reason = KVM_EXIT_MMIO;
  196. return 0;
  197. }