kvm_mips_dyntrans.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * KVM/MIPS: Binary Patching for privileged instructions, reduces traps.
  7. *
  8. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  9. * Authors: Sanjay Lal <sanjayl@kymasys.com>
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/kvm_host.h>
  14. #include <linux/module.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/fs.h>
  17. #include <linux/bootmem.h>
  18. #include "kvm_mips_comm.h"
  19. #define SYNCI_TEMPLATE 0x041f0000
  20. #define SYNCI_BASE(x) (((x) >> 21) & 0x1f)
  21. #define SYNCI_OFFSET ((x) & 0xffff)
  22. #define LW_TEMPLATE 0x8c000000
  23. #define CLEAR_TEMPLATE 0x00000020
  24. #define SW_TEMPLATE 0xac000000
  25. int
  26. kvm_mips_trans_cache_index(uint32_t inst, uint32_t *opc,
  27. struct kvm_vcpu *vcpu)
  28. {
  29. int result = 0;
  30. unsigned long kseg0_opc;
  31. uint32_t synci_inst = 0x0;
  32. /* Replace the CACHE instruction, with a NOP */
  33. kseg0_opc =
  34. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  35. (vcpu, (unsigned long) opc));
  36. memcpy((void *)kseg0_opc, (void *)&synci_inst, sizeof(uint32_t));
  37. mips32_SyncICache(kseg0_opc, 32);
  38. return result;
  39. }
  40. /*
  41. * Address based CACHE instructions are transformed into synci(s). A little heavy
  42. * for just D-cache invalidates, but avoids an expensive trap
  43. */
  44. int
  45. kvm_mips_trans_cache_va(uint32_t inst, uint32_t *opc,
  46. struct kvm_vcpu *vcpu)
  47. {
  48. int result = 0;
  49. unsigned long kseg0_opc;
  50. uint32_t synci_inst = SYNCI_TEMPLATE, base, offset;
  51. base = (inst >> 21) & 0x1f;
  52. offset = inst & 0xffff;
  53. synci_inst |= (base << 21);
  54. synci_inst |= offset;
  55. kseg0_opc =
  56. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  57. (vcpu, (unsigned long) opc));
  58. memcpy((void *)kseg0_opc, (void *)&synci_inst, sizeof(uint32_t));
  59. mips32_SyncICache(kseg0_opc, 32);
  60. return result;
  61. }
  62. int
  63. kvm_mips_trans_mfc0(uint32_t inst, uint32_t *opc, struct kvm_vcpu *vcpu)
  64. {
  65. int32_t rt, rd, sel;
  66. uint32_t mfc0_inst;
  67. unsigned long kseg0_opc, flags;
  68. rt = (inst >> 16) & 0x1f;
  69. rd = (inst >> 11) & 0x1f;
  70. sel = inst & 0x7;
  71. if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  72. mfc0_inst = CLEAR_TEMPLATE;
  73. mfc0_inst |= ((rt & 0x1f) << 16);
  74. } else {
  75. mfc0_inst = LW_TEMPLATE;
  76. mfc0_inst |= ((rt & 0x1f) << 16);
  77. mfc0_inst |=
  78. offsetof(struct mips_coproc,
  79. reg[rd][sel]) + offsetof(struct kvm_mips_commpage,
  80. cop0);
  81. }
  82. if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
  83. kseg0_opc =
  84. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  85. (vcpu, (unsigned long) opc));
  86. memcpy((void *)kseg0_opc, (void *)&mfc0_inst, sizeof(uint32_t));
  87. mips32_SyncICache(kseg0_opc, 32);
  88. } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  89. local_irq_save(flags);
  90. memcpy((void *)opc, (void *)&mfc0_inst, sizeof(uint32_t));
  91. mips32_SyncICache((unsigned long) opc, 32);
  92. local_irq_restore(flags);
  93. } else {
  94. kvm_err("%s: Invalid address: %p\n", __func__, opc);
  95. return -EFAULT;
  96. }
  97. return 0;
  98. }
  99. int
  100. kvm_mips_trans_mtc0(uint32_t inst, uint32_t *opc, struct kvm_vcpu *vcpu)
  101. {
  102. int32_t rt, rd, sel;
  103. uint32_t mtc0_inst = SW_TEMPLATE;
  104. unsigned long kseg0_opc, flags;
  105. rt = (inst >> 16) & 0x1f;
  106. rd = (inst >> 11) & 0x1f;
  107. sel = inst & 0x7;
  108. mtc0_inst |= ((rt & 0x1f) << 16);
  109. mtc0_inst |=
  110. offsetof(struct mips_coproc,
  111. reg[rd][sel]) + offsetof(struct kvm_mips_commpage, cop0);
  112. if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
  113. kseg0_opc =
  114. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  115. (vcpu, (unsigned long) opc));
  116. memcpy((void *)kseg0_opc, (void *)&mtc0_inst, sizeof(uint32_t));
  117. mips32_SyncICache(kseg0_opc, 32);
  118. } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  119. local_irq_save(flags);
  120. memcpy((void *)opc, (void *)&mtc0_inst, sizeof(uint32_t));
  121. mips32_SyncICache((unsigned long) opc, 32);
  122. local_irq_restore(flags);
  123. } else {
  124. kvm_err("%s: Invalid address: %p\n", __func__, opc);
  125. return -EFAULT;
  126. }
  127. return 0;
  128. }