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