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