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> |
James Hogan | dacc3ed | 2016-08-19 15:27:22 +0100 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 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 | dacc3ed | 2016-08-19 15:27:22 +0100 | [diff] [blame] | 33 | unsigned long vaddr = (unsigned long)opc; |
| 34 | int err; |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame] | 35 | |
James Hogan | dacc3ed | 2016-08-19 15:27:22 +0100 | [diff] [blame] | 36 | err = put_user(replace.word, opc); |
| 37 | if (unlikely(err)) { |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame] | 38 | kvm_err("%s: Invalid address: %p\n", __func__, opc); |
James Hogan | dacc3ed | 2016-08-19 15:27:22 +0100 | [diff] [blame] | 39 | return err; |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame] | 40 | } |
James Hogan | dacc3ed | 2016-08-19 15:27:22 +0100 | [diff] [blame] | 41 | __local_flush_icache_user_range(vaddr, vaddr + 4); |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame] | 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 46 | 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] | 47 | struct kvm_vcpu *vcpu) |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 48 | { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 49 | union mips_instruction nop_inst = { 0 }; |
| 50 | |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 51 | /* Replace the CACHE instruction, with a NOP */ |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 52 | return kvm_mips_trans_replace(vcpu, opc, nop_inst); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 56 | * Address based CACHE instructions are transformed into synci(s). A little |
| 57 | * heavy for just D-cache invalidates, but avoids an expensive trap |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 58 | */ |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 59 | 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] | 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 synci_inst = { 0 }; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 63 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 64 | synci_inst.i_format.opcode = bcond_op; |
| 65 | synci_inst.i_format.rs = inst.i_format.rs; |
| 66 | synci_inst.i_format.rt = synci_op; |
James Hogan | 5cc4aaf | 2016-07-04 19:35:13 +0100 | [diff] [blame] | 67 | if (cpu_has_mips_r6) |
| 68 | synci_inst.i_format.simmediate = inst.spec3_format.simmediate; |
| 69 | else |
| 70 | synci_inst.i_format.simmediate = inst.i_format.simmediate; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 71 | |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame] | 72 | return kvm_mips_trans_replace(vcpu, opc, synci_inst); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 73 | } |
| 74 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 75 | int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc, |
| 76 | struct kvm_vcpu *vcpu) |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 77 | { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 78 | union mips_instruction mfc0_inst = { 0 }; |
| 79 | u32 rd, sel; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 80 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 81 | rd = inst.c0r_format.rd; |
| 82 | sel = inst.c0r_format.sel; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 83 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 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; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 88 | } else { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 89 | mfc0_inst.i_format.opcode = lw_op; |
| 90 | mfc0_inst.i_format.rt = inst.c0r_format.rt; |
James Hogan | 42aa12e | 2016-06-15 19:29:57 +0100 | [diff] [blame] | 91 | mfc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 92 | offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]); |
James Hogan | 5808844f | 2016-07-08 11:53:27 +0100 | [diff] [blame] | 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 |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 97 | } |
| 98 | |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame] | 99 | return kvm_mips_trans_replace(vcpu, opc, mfc0_inst); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 100 | } |
| 101 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 102 | int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc, |
| 103 | struct kvm_vcpu *vcpu) |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 104 | { |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 105 | union mips_instruction mtc0_inst = { 0 }; |
| 106 | u32 rd, sel; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 107 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 108 | rd = inst.c0r_format.rd; |
| 109 | sel = inst.c0r_format.sel; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 110 | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 111 | mtc0_inst.i_format.opcode = sw_op; |
| 112 | mtc0_inst.i_format.rt = inst.c0r_format.rt; |
James Hogan | 42aa12e | 2016-06-15 19:29:57 +0100 | [diff] [blame] | 113 | mtc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR | |
James Hogan | 258f3a2 | 2016-06-15 19:29:47 +0100 | [diff] [blame] | 114 | offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]); |
James Hogan | 5808844f | 2016-07-08 11:53:27 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 116 | if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8) |
| 117 | mtc0_inst.i_format.simmediate |= 4; |
| 118 | #endif |
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 | } |