dyntrans.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/highmem.h>
  14. #include <linux/kvm_host.h>
  15. #include <linux/module.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/fs.h>
  18. #include <linux/bootmem.h>
  19. #include <asm/cacheflush.h>
  20. #include "commpage.h"
  21. /**
  22. * kvm_mips_trans_replace() - Replace trapping instruction in guest memory.
  23. * @vcpu: Virtual CPU.
  24. * @opc: PC of instruction to replace.
  25. * @replace: Instruction to write
  26. */
  27. static int kvm_mips_trans_replace(struct kvm_vcpu *vcpu, u32 *opc,
  28. union mips_instruction replace)
  29. {
  30. unsigned long paddr, flags;
  31. void *vaddr;
  32. if (KVM_GUEST_KSEGX((unsigned long)opc) == KVM_GUEST_KSEG0) {
  33. paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
  34. (unsigned long)opc);
  35. vaddr = kmap_atomic(pfn_to_page(PHYS_PFN(paddr)));
  36. vaddr += paddr & ~PAGE_MASK;
  37. memcpy(vaddr, (void *)&replace, sizeof(u32));
  38. local_flush_icache_range((unsigned long)vaddr,
  39. (unsigned long)vaddr + 32);
  40. kunmap_atomic(vaddr);
  41. } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
  42. local_irq_save(flags);
  43. memcpy((void *)opc, (void *)&replace, sizeof(u32));
  44. local_flush_icache_range((unsigned long)opc,
  45. (unsigned long)opc + 32);
  46. local_irq_restore(flags);
  47. } else {
  48. kvm_err("%s: Invalid address: %p\n", __func__, opc);
  49. return -EFAULT;
  50. }
  51. return 0;
  52. }
  53. int kvm_mips_trans_cache_index(union mips_instruction inst, u32 *opc,
  54. struct kvm_vcpu *vcpu)
  55. {
  56. union mips_instruction nop_inst = { 0 };
  57. /* Replace the CACHE instruction, with a NOP */
  58. return kvm_mips_trans_replace(vcpu, opc, nop_inst);
  59. }
  60. /*
  61. * Address based CACHE instructions are transformed into synci(s). A little
  62. * heavy for just D-cache invalidates, but avoids an expensive trap
  63. */
  64. int kvm_mips_trans_cache_va(union mips_instruction inst, u32 *opc,
  65. struct kvm_vcpu *vcpu)
  66. {
  67. union mips_instruction synci_inst = { 0 };
  68. synci_inst.i_format.opcode = bcond_op;
  69. synci_inst.i_format.rs = inst.i_format.rs;
  70. synci_inst.i_format.rt = synci_op;
  71. if (cpu_has_mips_r6)
  72. synci_inst.i_format.simmediate = inst.spec3_format.simmediate;
  73. else
  74. synci_inst.i_format.simmediate = inst.i_format.simmediate;
  75. return kvm_mips_trans_replace(vcpu, opc, synci_inst);
  76. }
  77. int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc,
  78. struct kvm_vcpu *vcpu)
  79. {
  80. union mips_instruction mfc0_inst = { 0 };
  81. u32 rd, sel;
  82. rd = inst.c0r_format.rd;
  83. sel = inst.c0r_format.sel;
  84. if (rd == MIPS_CP0_ERRCTL && sel == 0) {
  85. mfc0_inst.r_format.opcode = spec_op;
  86. mfc0_inst.r_format.rd = inst.c0r_format.rt;
  87. mfc0_inst.r_format.func = add_op;
  88. } else {
  89. mfc0_inst.i_format.opcode = lw_op;
  90. mfc0_inst.i_format.rt = inst.c0r_format.rt;
  91. mfc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
  92. offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
  93. #ifdef CONFIG_CPU_BIG_ENDIAN
  94. if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8)
  95. mfc0_inst.i_format.simmediate |= 4;
  96. #endif
  97. }
  98. return kvm_mips_trans_replace(vcpu, opc, mfc0_inst);
  99. }
  100. int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc,
  101. struct kvm_vcpu *vcpu)
  102. {
  103. union mips_instruction mtc0_inst = { 0 };
  104. u32 rd, sel;
  105. rd = inst.c0r_format.rd;
  106. sel = inst.c0r_format.sel;
  107. mtc0_inst.i_format.opcode = sw_op;
  108. mtc0_inst.i_format.rt = inst.c0r_format.rt;
  109. mtc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
  110. offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
  111. #ifdef CONFIG_CPU_BIG_ENDIAN
  112. if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8)
  113. mtc0_inst.i_format.simmediate |= 4;
  114. #endif
  115. return kvm_mips_trans_replace(vcpu, opc, mtc0_inst);
  116. }