blob: 60ebf5862d2bb52168d689c690b62094d1e3ab72 [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>
James Hogandacc3ed2016-08-19 15:27:22 +010016#include <linux/uaccess.h>
Sanjay Lal50c83082012-11-21 18:34:16 -080017#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 Hogandacc3ed2016-08-19 15:27:22 +010033 unsigned long vaddr = (unsigned long)opc;
34 int err;
James Hogand5cd26b2016-06-15 19:29:46 +010035
James Hogandacc3ed2016-08-19 15:27:22 +010036 err = put_user(replace.word, opc);
37 if (unlikely(err)) {
James Hogand5cd26b2016-06-15 19:29:46 +010038 kvm_err("%s: Invalid address: %p\n", __func__, opc);
James Hogandacc3ed2016-08-19 15:27:22 +010039 return err;
James Hogand5cd26b2016-06-15 19:29:46 +010040 }
James Hogandacc3ed2016-08-19 15:27:22 +010041 __local_flush_icache_user_range(vaddr, vaddr + 4);
James Hogand5cd26b2016-06-15 19:29:46 +010042
43 return 0;
44}
45
James Hogan258f3a22016-06-15 19:29:47 +010046int kvm_mips_trans_cache_index(union mips_instruction inst, u32 *opc,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070047 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080048{
James Hogan258f3a22016-06-15 19:29:47 +010049 union mips_instruction nop_inst = { 0 };
50
Sanjay Lal50c83082012-11-21 18:34:16 -080051 /* Replace the CACHE instruction, with a NOP */
James Hogan258f3a22016-06-15 19:29:47 +010052 return kvm_mips_trans_replace(vcpu, opc, nop_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -080053}
54
55/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070056 * Address based CACHE instructions are transformed into synci(s). A little
57 * heavy for just D-cache invalidates, but avoids an expensive trap
Sanjay Lal50c83082012-11-21 18:34:16 -080058 */
James Hogan258f3a22016-06-15 19:29:47 +010059int kvm_mips_trans_cache_va(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 synci_inst = { 0 };
Sanjay Lal50c83082012-11-21 18:34:16 -080063
James Hogan258f3a22016-06-15 19:29:47 +010064 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 Hogan5cc4aaf2016-07-04 19:35:13 +010067 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 Lal50c83082012-11-21 18:34:16 -080071
James Hogand5cd26b2016-06-15 19:29:46 +010072 return kvm_mips_trans_replace(vcpu, opc, synci_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -080073}
74
James Hogan258f3a22016-06-15 19:29:47 +010075int kvm_mips_trans_mfc0(union mips_instruction inst, u32 *opc,
76 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -080077{
James Hogan258f3a22016-06-15 19:29:47 +010078 union mips_instruction mfc0_inst = { 0 };
79 u32 rd, sel;
Sanjay Lal50c83082012-11-21 18:34:16 -080080
James Hogan258f3a22016-06-15 19:29:47 +010081 rd = inst.c0r_format.rd;
82 sel = inst.c0r_format.sel;
Sanjay Lal50c83082012-11-21 18:34:16 -080083
James Hogan258f3a22016-06-15 19:29:47 +010084 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 Lal50c83082012-11-21 18:34:16 -080088 } else {
James Hogan258f3a22016-06-15 19:29:47 +010089 mfc0_inst.i_format.opcode = lw_op;
90 mfc0_inst.i_format.rt = inst.c0r_format.rt;
James Hogan42aa12e2016-06-15 19:29:57 +010091 mfc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
James Hogan258f3a22016-06-15 19:29:47 +010092 offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
James Hogan5808844f2016-07-08 11:53:27 +010093#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 Lal50c83082012-11-21 18:34:16 -080097 }
98
James Hogand5cd26b2016-06-15 19:29:46 +010099 return kvm_mips_trans_replace(vcpu, opc, mfc0_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -0800100}
101
James Hogan258f3a22016-06-15 19:29:47 +0100102int kvm_mips_trans_mtc0(union mips_instruction inst, u32 *opc,
103 struct kvm_vcpu *vcpu)
Sanjay Lal50c83082012-11-21 18:34:16 -0800104{
James Hogan258f3a22016-06-15 19:29:47 +0100105 union mips_instruction mtc0_inst = { 0 };
106 u32 rd, sel;
Sanjay Lal50c83082012-11-21 18:34:16 -0800107
James Hogan258f3a22016-06-15 19:29:47 +0100108 rd = inst.c0r_format.rd;
109 sel = inst.c0r_format.sel;
Sanjay Lal50c83082012-11-21 18:34:16 -0800110
James Hogan258f3a22016-06-15 19:29:47 +0100111 mtc0_inst.i_format.opcode = sw_op;
112 mtc0_inst.i_format.rt = inst.c0r_format.rt;
James Hogan42aa12e2016-06-15 19:29:57 +0100113 mtc0_inst.i_format.simmediate = KVM_GUEST_COMMPAGE_ADDR |
James Hogan258f3a22016-06-15 19:29:47 +0100114 offsetof(struct kvm_mips_commpage, cop0.reg[rd][sel]);
James Hogan5808844f2016-07-08 11:53:27 +0100115#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 Lal50c83082012-11-21 18:34:16 -0800119
James Hogand5cd26b2016-06-15 19:29:46 +0100120 return kvm_mips_trans_replace(vcpu, opc, mtc0_inst);
Sanjay Lal50c83082012-11-21 18:34:16 -0800121}