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 | |
| 23 | #define SYNCI_TEMPLATE 0x041f0000 |
| 24 | #define SYNCI_BASE(x) (((x) >> 21) & 0x1f) |
| 25 | #define SYNCI_OFFSET ((x) & 0xffff) |
| 26 | |
| 27 | #define LW_TEMPLATE 0x8c000000 |
| 28 | #define CLEAR_TEMPLATE 0x00000020 |
| 29 | #define SW_TEMPLATE 0xac000000 |
| 30 | |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame^] | 31 | /** |
| 32 | * kvm_mips_trans_replace() - Replace trapping instruction in guest memory. |
| 33 | * @vcpu: Virtual CPU. |
| 34 | * @opc: PC of instruction to replace. |
| 35 | * @replace: Instruction to write |
| 36 | */ |
| 37 | static int kvm_mips_trans_replace(struct kvm_vcpu *vcpu, u32 *opc, u32 replace) |
| 38 | { |
| 39 | unsigned long kseg0_opc, flags; |
| 40 | |
| 41 | if (KVM_GUEST_KSEGX(opc) == KVM_GUEST_KSEG0) { |
| 42 | kseg0_opc = |
| 43 | CKSEG0ADDR(kvm_mips_translate_guest_kseg0_to_hpa |
| 44 | (vcpu, (unsigned long) opc)); |
| 45 | memcpy((void *)kseg0_opc, (void *)&replace, sizeof(u32)); |
| 46 | local_flush_icache_range(kseg0_opc, kseg0_opc + 32); |
| 47 | } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) { |
| 48 | local_irq_save(flags); |
| 49 | memcpy((void *)opc, (void *)&replace, sizeof(u32)); |
| 50 | local_flush_icache_range((unsigned long)opc, |
| 51 | (unsigned long)opc + 32); |
| 52 | local_irq_restore(flags); |
| 53 | } else { |
| 54 | kvm_err("%s: Invalid address: %p\n", __func__, opc); |
| 55 | return -EFAULT; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 61 | int kvm_mips_trans_cache_index(u32 inst, u32 *opc, |
Deng-Cheng Zhu | d116e81 | 2014-06-26 12:11:34 -0700 | [diff] [blame] | 62 | struct kvm_vcpu *vcpu) |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 63 | { |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 64 | /* Replace the CACHE instruction, with a NOP */ |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame^] | 65 | return kvm_mips_trans_replace(vcpu, opc, 0x00000000); |
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 | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 72 | int kvm_mips_trans_cache_va(u32 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 | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 75 | u32 synci_inst = SYNCI_TEMPLATE, base, offset; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 76 | |
| 77 | base = (inst >> 21) & 0x1f; |
| 78 | offset = inst & 0xffff; |
| 79 | synci_inst |= (base << 21); |
| 80 | synci_inst |= offset; |
| 81 | |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame^] | 82 | return kvm_mips_trans_replace(vcpu, opc, synci_inst); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 83 | } |
| 84 | |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 85 | int kvm_mips_trans_mfc0(u32 inst, u32 *opc, struct kvm_vcpu *vcpu) |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 86 | { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 87 | u32 rt, rd, sel; |
| 88 | u32 mfc0_inst; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 89 | |
| 90 | rt = (inst >> 16) & 0x1f; |
| 91 | rd = (inst >> 11) & 0x1f; |
| 92 | sel = inst & 0x7; |
| 93 | |
| 94 | if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) { |
| 95 | mfc0_inst = CLEAR_TEMPLATE; |
James Hogan | 66ffc50 | 2016-06-15 19:29:45 +0100 | [diff] [blame] | 96 | mfc0_inst |= ((rt & 0x1f) << 11); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 97 | } else { |
| 98 | mfc0_inst = LW_TEMPLATE; |
| 99 | mfc0_inst |= ((rt & 0x1f) << 16); |
James Hogan | 088ec20 | 2015-12-16 23:49:31 +0000 | [diff] [blame] | 100 | mfc0_inst |= offsetof(struct kvm_mips_commpage, |
| 101 | cop0.reg[rd][sel]); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 102 | } |
| 103 | |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame^] | 104 | return kvm_mips_trans_replace(vcpu, opc, mfc0_inst); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 105 | } |
| 106 | |
James Hogan | bdb7ed8 | 2016-06-09 14:19:07 +0100 | [diff] [blame] | 107 | int kvm_mips_trans_mtc0(u32 inst, u32 *opc, struct kvm_vcpu *vcpu) |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 108 | { |
James Hogan | 8cffd19 | 2016-06-09 14:19:08 +0100 | [diff] [blame] | 109 | u32 rt, rd, sel; |
| 110 | u32 mtc0_inst = SW_TEMPLATE; |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 111 | |
| 112 | rt = (inst >> 16) & 0x1f; |
| 113 | rd = (inst >> 11) & 0x1f; |
| 114 | sel = inst & 0x7; |
| 115 | |
| 116 | mtc0_inst |= ((rt & 0x1f) << 16); |
James Hogan | 088ec20 | 2015-12-16 23:49:31 +0000 | [diff] [blame] | 117 | mtc0_inst |= offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 118 | |
James Hogan | d5cd26b | 2016-06-15 19:29:46 +0100 | [diff] [blame^] | 119 | return kvm_mips_trans_replace(vcpu, opc, mtc0_inst); |
Sanjay Lal | 50c8308 | 2012-11-21 18:34:16 -0800 | [diff] [blame] | 120 | } |