blob: 5317ed2d8909d2d0f915fac74d9ef8c6d793f987 [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>
16#include <linux/module.h>
17#include <linux/vmalloc.h>
18#include <linux/fs.h>
19#include <linux/bootmem.h>
James Hoganfacaaec2014-05-29 10:16:25 +010020#include <asm/cacheflush.h>
Sanjay Lal50c83082012-11-21 18:34:16 -080021
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070022#include "commpage.h"
Sanjay Lal50c83082012-11-21 18:34:16 -080023
James Hogand5cd26b2016-06-15 19:29:46 +010024/**
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 Hogan258f3a22016-06-15 19:29:47 +010030static int kvm_mips_trans_replace(struct kvm_vcpu *vcpu, u32 *opc,
31 union mips_instruction replace)
James Hogand5cd26b2016-06-15 19:29:46 +010032{
James Hogan28cc5bd2016-07-08 11:53:22 +010033 unsigned long paddr, flags;
34 void *vaddr;
James Hogand5cd26b2016-06-15 19:29:46 +010035
James Hogan82969632016-07-08 11:53:29 +010036 if (KVM_GUEST_KSEGX((unsigned long)opc) == KVM_GUEST_KSEG0) {
James Hogan28cc5bd2016-07-08 11:53:22 +010037 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 Hogand5cd26b2016-06-15 19:29:46 +010045 } else if (KVM_GUEST_KSEGX((unsigned long) opc) == KVM_GUEST_KSEG23) {
46 local_irq_save(flags);
47 memcpy((void *)opc, (void *)&replace, sizeof(u32));
James Hogan24d1a6e2016-09-01 17:30:14 +010048 __local_flush_icache_user_range((unsigned long)opc,
49 (unsigned long)opc + 32);
James Hogand5cd26b2016-06-15 19:29:46 +010050 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 Hogan258f3a22016-06-15 19:29:47 +010059int kvm_mips_trans_cache_index(union mips_instruction inst, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070060 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080061{
James Hogan258f3a22016-06-15 19:29:47 +010062 union mips_instruction nop_inst = { 0 };
63
Sanjay Lal50c83082012-11-21 18:34:16 -080064 /* Replace the CACHE instruction, with a NOP */
James Hogan258f3a22016-06-15 19:29:47 +010065 return kvm_mips_trans_replace(vcpu, opc, nop_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -080066}
67
68/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070069 * Address based CACHE instructions are transformed into synci(s). A little
70 * heavy for just D-cache invalidates, but avoids an expensive trap
Sanjay Lal50c83082012-11-21 18:34:16 -080071 */
James Hogan258f3a22016-06-15 19:29:47 +010072int kvm_mips_trans_cache_va(union mips_instruction inst, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070073 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080074{
James Hogan258f3a22016-06-15 19:29:47 +010075 union mips_instruction synci_inst = { 0 };
Sanjay Lal50c83082012-11-21 18:34:16 -080076
James Hogan258f3a22016-06-15 19:29:47 +010077 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 Hogan5cc4aaf2016-07-04 19:35:13 +010080 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 Lal50c83082012-11-21 18:34:16 -080084
James Hogand5cd26b2016-06-15 19:29:46 +010085 return kvm_mips_trans_replace(vcpu, opc, synci_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -080086}
87
James Hogan258f3a22016-06-15 19:29:47 +010088int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc,
89 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080090{
James Hogan258f3a22016-06-15 19:29:47 +010091 union mips_instruction mfc0_inst = { 0 };
92 u32 rd, sel;
Sanjay Lal50c83082012-11-21 18:34:16 -080093
James Hogan258f3a22016-06-15 19:29:47 +010094 rd = inst.c0r_format.rd;
95 sel = inst.c0r_format.sel;
Sanjay Lal50c83082012-11-21 18:34:16 -080096
James Hogan258f3a22016-06-15 19:29:47 +010097 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 Lal50c83082012-11-21 18:34:16 -0800101 } else {
James Hogan258f3a22016-06-15 19:29:47 +0100102 mfc0_inst.i_format.opcode = lw_op;
103 mfc0_inst.i_format.rt = inst.c0r_format.rt;
James Hogan42aa12e2016-06-15 19:29:57 +0100104 mfc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
James Hogan258f3a22016-06-15 19:29:47 +0100105 offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
James Hogan5808844f2016-07-08 11:53:27 +0100106#ifdef CONFIG_CPU_BIG_ENDIAN
107 if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8)
108 mfc0_inst.i_format.simmediate |= 4;
109#endif
Sanjay Lal50c83082012-11-21 18:34:16 -0800110 }
111
James Hogand5cd26b2016-06-15 19:29:46 +0100112 return kvm_mips_trans_replace(vcpu, opc, mfc0_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -0800113}
114
James Hogan258f3a22016-06-15 19:29:47 +0100115int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc,
116 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -0800117{
James Hogan258f3a22016-06-15 19:29:47 +0100118 union mips_instruction mtc0_inst = { 0 };
119 u32 rd, sel;
Sanjay Lal50c83082012-11-21 18:34:16 -0800120
James Hogan258f3a22016-06-15 19:29:47 +0100121 rd = inst.c0r_format.rd;
122 sel = inst.c0r_format.sel;
Sanjay Lal50c83082012-11-21 18:34:16 -0800123
James Hogan258f3a22016-06-15 19:29:47 +0100124 mtc0_inst.i_format.opcode = sw_op;
125 mtc0_inst.i_format.rt = inst.c0r_format.rt;
James Hogan42aa12e2016-06-15 19:29:57 +0100126 mtc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
James Hogan258f3a22016-06-15 19:29:47 +0100127 offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
James Hogan5808844f2016-07-08 11:53:27 +0100128#ifdef CONFIG_CPU_BIG_ENDIAN
129 if (sizeof(vcpu->arch.cop0->reg[0][0]) == 8)
130 mtc0_inst.i_format.simmediate |= 4;
131#endif
Sanjay Lal50c83082012-11-21 18:34:16 -0800132
James Hogand5cd26b2016-06-15 19:29:46 +0100133 return kvm_mips_trans_replace(vcpu, opc, mtc0_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -0800134}