blob: 010cef2406880e8742a389575d9c54bb1c004da7 [file] [log] [blame]
Sanjay Lal50c83082012-11-21 18:34:16 -08001/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002 * 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 Lal50c83082012-11-21 18:34:16 -080011
12#include <linux/errno.h>
13#include <linux/err.h>
James Hogan28cc5bd2016-07-08 11:53:22 +010014#include <linux/highmem.h>
Sanjay Lal50c83082012-11-21 18:34:16 -080015#include <linux/kvm_host.h>
Sanjay Lal50c83082012-11-21 18:34:16 -080016#include <linux/vmalloc.h>
17#include <linux/fs.h>
18#include <linux/bootmem.h>
James Hoganfacaaec2014-05-29 10:16:25 +010019#include <asm/cacheflush.h>
Sanjay Lal50c83082012-11-21 18:34:16 -080020
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070021#include "commpage.h"
Sanjay Lal50c83082012-11-21 18:34:16 -080022
James Hogand5cd26b2016-06-15 19:29:46 +010023/**
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 Hogan258f3a22016-06-15 19:29:47 +010029static int kvm_mips_trans_replace(struct kvm_vcpu *vcpu, u32 *opc,
30 union mips_instruction replace)
James Hogand5cd26b2016-06-15 19:29:46 +010031{
James Hogan28cc5bd2016-07-08 11:53:22 +010032 unsigned long paddr, flags;
33 void *vaddr;
James Hogand5cd26b2016-06-15 19:29:46 +010034
James Hogan82969632016-07-08 11:53:29 +010035 if (KVM_GUEST_KSEGX((unsigned long)opc) == KVM_GUEST_KSEG0) {
James Hogan28cc5bd2016-07-08 11:53:22 +010036 paddr = kvm_mips_translate_guest_kseg0_to_hpa(vcpu,
37 (unsigned long)opc);
38 vaddr = kmap_atomic(pfn_to_page(PHYS_PFN(paddr)));
39 vaddr += paddr & ~PAGE_MASK;
40 memcpy(vaddr, (void *)&replace, sizeof(u32));
41 local_flush_icache_range((unsigned long)vaddr,
42 (unsigned long)vaddr + 32);
43 kunmap_atomic(vaddr);
James Hogand5cd26b2016-06-15 19:29:46 +010044 } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
45 local_irq_save(flags);
46 memcpy((void *)opc, (void *)&replace, sizeof(u32));
James Hogan24d1a6e2016-09-01 17:30:14 +010047 __local_flush_icache_user_range((unsigned long)opc,
48 (unsigned long)opc + 32);
James Hogand5cd26b2016-06-15 19:29:46 +010049 local_irq_restore(flags);
50 } else {
51 kvm_err("%s: Invalid address: %p\n", __func__, opc);
52 return -EFAULT;
53 }
54
55 return 0;
56}
57
James Hogan258f3a22016-06-15 19:29:47 +010058int kvm_mips_trans_cache_index(union mips_instruction inst, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070059 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080060{
James Hogan258f3a22016-06-15 19:29:47 +010061 union mips_instruction nop_inst = { 0 };
62
Sanjay Lal50c83082012-11-21 18:34:16 -080063 /* Replace the CACHE instruction, with a NOP */
James Hogan258f3a22016-06-15 19:29:47 +010064 return kvm_mips_trans_replace(vcpu, opc, nop_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -080065}
66
67/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070068 * Address based CACHE instructions are transformed into synci(s). A little
69 * heavy for just D-cache invalidates, but avoids an expensive trap
Sanjay Lal50c83082012-11-21 18:34:16 -080070 */
James Hogan258f3a22016-06-15 19:29:47 +010071int kvm_mips_trans_cache_va(union mips_instruction inst, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070072 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080073{
James Hogan258f3a22016-06-15 19:29:47 +010074 union mips_instruction synci_inst = { 0 };
Sanjay Lal50c83082012-11-21 18:34:16 -080075
James Hogan258f3a22016-06-15 19:29:47 +010076 synci_inst.i_format.opcode = bcond_op;
77 synci_inst.i_format.rs = inst.i_format.rs;
78 synci_inst.i_format.rt = synci_op;
James Hogan5cc4aaf2016-07-04 19:35:13 +010079 if (cpu_has_mips_r6)
80 synci_inst.i_format.simmediate = inst.spec3_format.simmediate;
81 else
82 synci_inst.i_format.simmediate = inst.i_format.simmediate;
Sanjay Lal50c83082012-11-21 18:34:16 -080083
James Hogand5cd26b2016-06-15 19:29:46 +010084 return kvm_mips_trans_replace(vcpu, opc, synci_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -080085}
86
James Hogan258f3a22016-06-15 19:29:47 +010087int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc,
88 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080089{
James Hogan258f3a22016-06-15 19:29:47 +010090 union mips_instruction mfc0_inst = { 0 };
91 u32 rd, sel;
Sanjay Lal50c83082012-11-21 18:34:16 -080092
James Hogan258f3a22016-06-15 19:29:47 +010093 rd = inst.c0r_format.rd;
94 sel = inst.c0r_format.sel;
Sanjay Lal50c83082012-11-21 18:34:16 -080095
James Hogan258f3a22016-06-15 19:29:47 +010096 if (rd == MIPS_CP0_ERRCTL && sel == 0) {
97 mfc0_inst.r_format.opcode = spec_op;
98 mfc0_inst.r_format.rd = inst.c0r_format.rt;
99 mfc0_inst.r_format.func = add_op;
Sanjay Lal50c83082012-11-21 18:34:16 -0800100 } else {
James Hogan258f3a22016-06-15 19:29:47 +0100101 mfc0_inst.i_format.opcode = lw_op;
102 mfc0_inst.i_format.rt = inst.c0r_format.rt;
James Hogan42aa12e2016-06-15 19:29:57 +0100103 mfc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
James Hogan258f3a22016-06-15 19:29:47 +0100104 offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
James Hogan5808844f2016-07-08 11:53:27 +0100105#ifdef CONFIG_CPU_BIG_ENDIAN
106 if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8)
107 mfc0_inst.i_format.simmediate |= 4;
108#endif
Sanjay Lal50c83082012-11-21 18:34:16 -0800109 }
110
James Hogand5cd26b2016-06-15 19:29:46 +0100111 return kvm_mips_trans_replace(vcpu, opc, mfc0_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -0800112}
113
James Hogan258f3a22016-06-15 19:29:47 +0100114int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc,
115 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -0800116{
James Hogan258f3a22016-06-15 19:29:47 +0100117 union mips_instruction mtc0_inst = { 0 };
118 u32 rd, sel;
Sanjay Lal50c83082012-11-21 18:34:16 -0800119
James Hogan258f3a22016-06-15 19:29:47 +0100120 rd = inst.c0r_format.rd;
121 sel = inst.c0r_format.sel;
Sanjay Lal50c83082012-11-21 18:34:16 -0800122
James Hogan258f3a22016-06-15 19:29:47 +0100123 mtc0_inst.i_format.opcode = sw_op;
124 mtc0_inst.i_format.rt = inst.c0r_format.rt;
James Hogan42aa12e2016-06-15 19:29:57 +0100125 mtc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
James Hogan258f3a22016-06-15 19:29:47 +0100126 offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
James Hogan5808844f2016-07-08 11:53:27 +0100127#ifdef CONFIG_CPU_BIG_ENDIAN
128 if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8)
129 mtc0_inst.i_format.simmediate |= 4;
130#endif
Sanjay Lal50c83082012-11-21 18:34:16 -0800131
James Hogand5cd26b2016-06-15 19:29:46 +0100132 return kvm_mips_trans_replace(vcpu, opc, mtc0_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -0800133}