dyntrans.c 4.1 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 <asm/cacheflush.h>
  19. #include "commpage.h"
  20. #define SYNCI_TEMPLATE 0x041f0000
  21. #define SYNCI_BASE(x) (((x) >> 21) & 0x1f)
  22. #define SYNCI_OFFSET ((x) & 0xffff)
  23. #define LW_TEMPLATE 0x8c000000
  24. #define CLEAR_TEMPLATE 0x00000020
  25. #define SW_TEMPLATE 0xac000000
  26. int 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. local_flush_icache_range(kseg0_opc, kseg0_opc + 32);
  38. return result;
  39. }
  40. /*
  41. * Address based CACHE instructions are transformed into synci(s). A little
  42. * heavy for just D-cache invalidates, but avoids an expensive trap
  43. */
  44. int kvm_mips_trans_cache_va(uint32_t inst, uint32_t *opc,
  45. struct kvm_vcpu *vcpu)
  46. {
  47. int result = 0;
  48. unsigned long kseg0_opc;
  49. uint32_t synci_inst = SYNCI_TEMPLATE, base, offset;
  50. base = (inst >> 21) & 0x1f;
  51. offset = inst & 0xffff;
  52. synci_inst |= (base << 21);
  53. synci_inst |= offset;
  54. kseg0_opc =
  55. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  56. (vcpu, (unsigned long) opc));
  57. memcpy((void *)kseg0_opc, (void *)&synci_inst, sizeof(uint32_t));
  58. local_flush_icache_range(kseg0_opc, kseg0_opc + 32);
  59. return result;
  60. }
  61. int kvm_mips_trans_mfc0(uint32_t inst, uint32_t *opc, struct kvm_vcpu *vcpu)
  62. {
  63. int32_t rt, rd, sel;
  64. uint32_t mfc0_inst;
  65. unsigned long kseg0_opc, flags;
  66. rt = (inst >> 16) & 0x1f;
  67. rd = (inst >> 11) & 0x1f;
  68. sel = inst & 0x7;
  69. if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  70. mfc0_inst = CLEAR_TEMPLATE;
  71. mfc0_inst |= ((rt & 0x1f) << 16);
  72. } else {
  73. mfc0_inst = LW_TEMPLATE;
  74. mfc0_inst |= ((rt & 0x1f) << 16);
  75. mfc0_inst |=
  76. offsetof(struct mips_coproc,
  77. reg[rd][sel]) + offsetof(struct kvm_mips_commpage,
  78. cop0);
  79. }
  80. if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
  81. kseg0_opc =
  82. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  83. (vcpu, (unsigned long) opc));
  84. memcpy((void *)kseg0_opc, (void *)&mfc0_inst, sizeof(uint32_t));
  85. local_flush_icache_range(kseg0_opc, kseg0_opc + 32);
  86. } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  87. local_irq_save(flags);
  88. memcpy((void *)opc, (void *)&mfc0_inst, sizeof(uint32_t));
  89. local_flush_icache_range((unsigned long)opc,
  90. (unsigned long)opc + 32);
  91. local_irq_restore(flags);
  92. } else {
  93. kvm_err("%s: Invalid address: %p\n", __func__, opc);
  94. return -EFAULT;
  95. }
  96. return 0;
  97. }
  98. int kvm_mips_trans_mtc0(uint32_t inst, uint32_t *opc, struct kvm_vcpu *vcpu)
  99. {
  100. int32_t rt, rd, sel;
  101. uint32_t mtc0_inst = SW_TEMPLATE;
  102. unsigned long kseg0_opc, flags;
  103. rt = (inst >> 16) & 0x1f;
  104. rd = (inst >> 11) & 0x1f;
  105. sel = inst & 0x7;
  106. mtc0_inst |= ((rt & 0x1f) << 16);
  107. mtc0_inst |=
  108. offsetof(struct mips_coproc,
  109. reg[rd][sel]) + offsetof(struct kvm_mips_commpage, cop0);
  110. if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) {
  111. kseg0_opc =
  112. CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa
  113. (vcpu, (unsigned long) opc));
  114. memcpy((void *)kseg0_opc, (void *)&mtc0_inst, sizeof(uint32_t));
  115. local_flush_icache_range(kseg0_opc, kseg0_opc + 32);
  116. } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  117. local_irq_save(flags);
  118. memcpy((void *)opc, (void *)&mtc0_inst, sizeof(uint32_t));
  119. local_flush_icache_range((unsigned long)opc,
  120. (unsigned long)opc + 32);
  121. local_irq_restore(flags);
  122. } else {
  123. kvm_err("%s: Invalid address: %p\n", __func__, opc);
  124. return -EFAULT;
  125. }
  126. return 0;
  127. }