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