Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: MIPS: Implement HYPCALL emulation

Emulate the HYPCALL instruction added in the VZ ASE and used by the MIPS
paravirtualised guest support that is already merged. The new hypcall.c
handles arguments and the return value. No actual hypercalls are yet
supported, but this still allows us to safely step over hypercalls and
set an error code in the return value for forward compatibility.

Non-zero HYPCALL codes are not handled.

We also document the hypercall ABI which asm/kvm_para.h uses.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Acked-by: Ralf Baechle <ralf@linux-mips.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Andreas Herrmann <andreas.herrmann@caviumnetworks.com>
Cc: David Daney <david.daney@cavium.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org
Cc: linux-doc@vger.kernel.org

+74 -1
+5
Documentation/virtual/kvm/hypercalls.txt
··· 28 28 property inside the device tree's /hypervisor node. 29 29 For more information refer to Documentation/virtual/kvm/ppc-pv.txt 30 30 31 + MIPS: 32 + KVM hypercalls use the HYPCALL instruction with code 0 and the hypercall 33 + number in $2 (v0). Up to four arguments may be placed in $4-$7 (a0-a3) and 34 + the return value is placed in $2 (v0). 35 + 31 36 KVM Hypercalls Documentation 32 37 =========================== 33 38 The template for each hypercall is:
+7
arch/mips/include/asm/kvm_host.h
··· 229 229 EMULATE_WAIT, /* WAIT instruction */ 230 230 EMULATE_PRIV_FAIL, 231 231 EMULATE_EXCEPT, /* A guest exception has been generated */ 232 + EMULATE_HYPERCALL, /* HYPCALL instruction */ 232 233 }; 233 234 234 235 #define mips3_paddr_to_tlbpfn(x) \ ··· 832 831 unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu); 833 832 unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu); 834 833 unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu); 834 + 835 + /* Hypercalls (hypcall.c) */ 836 + 837 + enum emulation_result kvm_mips_emul_hypcall(struct kvm_vcpu *vcpu, 838 + union mips_instruction inst); 839 + int kvm_mips_handle_hypcall(struct kvm_vcpu *vcpu); 835 840 836 841 /* Dynamic binary translation */ 837 842 extern int kvm_mips_trans_cache_index(union mips_instruction inst,
+1 -1
arch/mips/include/uapi/asm/inst.h
··· 179 179 tlbr_op = 0x01, tlbwi_op = 0x02, 180 180 tlbwr_op = 0x06, tlbp_op = 0x08, 181 181 rfe_op = 0x10, eret_op = 0x18, 182 - wait_op = 0x20, 182 + wait_op = 0x20, hypcall_op = 0x28 183 183 }; 184 184 185 185 /*
+1
arch/mips/kvm/Makefile
··· 10 10 kvm-objs := $(common-objs-y) mips.o emulate.o entry.o \ 11 11 interrupt.o stats.o commpage.o \ 12 12 dyntrans.o trap_emul.o fpu.o 13 + kvm-objs += hypcall.o 13 14 kvm-objs += mmu.o 14 15 15 16 obj-$(CONFIG_KVM) += kvm.o
+3
arch/mips/kvm/emulate.c
··· 1143 1143 case wait_op: 1144 1144 er = kvm_mips_emul_wait(vcpu); 1145 1145 break; 1146 + case hypcall_op: 1147 + er = kvm_mips_emul_hypcall(vcpu, inst); 1148 + break; 1146 1149 } 1147 1150 } else { 1148 1151 rt = inst.c0r_format.rt;
+53
arch/mips/kvm/hypcall.c
··· 1 + /* 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: Hypercall handling. 7 + * 8 + * Copyright (C) 2015 Imagination Technologies Ltd. 9 + */ 10 + 11 + #include <linux/kernel.h> 12 + #include <linux/kvm_host.h> 13 + #include <linux/kvm_para.h> 14 + 15 + #define MAX_HYPCALL_ARGS 4 16 + 17 + enum emulation_result kvm_mips_emul_hypcall(struct kvm_vcpu *vcpu, 18 + union mips_instruction inst) 19 + { 20 + unsigned int code = (inst.co_format.code >> 5) & 0x3ff; 21 + 22 + kvm_debug("[%#lx] HYPCALL %#03x\n", vcpu->arch.pc, code); 23 + 24 + switch (code) { 25 + case 0: 26 + return EMULATE_HYPERCALL; 27 + default: 28 + return EMULATE_FAIL; 29 + }; 30 + } 31 + 32 + static int kvm_mips_hypercall(struct kvm_vcpu *vcpu, unsigned long num, 33 + const unsigned long *args, unsigned long *hret) 34 + { 35 + /* Report unimplemented hypercall to guest */ 36 + *hret = -KVM_ENOSYS; 37 + return RESUME_GUEST; 38 + } 39 + 40 + int kvm_mips_handle_hypcall(struct kvm_vcpu *vcpu) 41 + { 42 + unsigned long num, args[MAX_HYPCALL_ARGS]; 43 + 44 + /* read hypcall number and arguments */ 45 + num = vcpu->arch.gprs[2]; /* v0 */ 46 + args[0] = vcpu->arch.gprs[4]; /* a0 */ 47 + args[1] = vcpu->arch.gprs[5]; /* a1 */ 48 + args[2] = vcpu->arch.gprs[6]; /* a2 */ 49 + args[3] = vcpu->arch.gprs[7]; /* a3 */ 50 + 51 + return kvm_mips_hypercall(vcpu, num, 52 + args, &vcpu->arch.gprs[2] /* v0 */); 53 + }
+4
arch/mips/kvm/trap_emul.c
··· 82 82 ret = RESUME_HOST; 83 83 break; 84 84 85 + case EMULATE_HYPERCALL: 86 + ret = kvm_mips_handle_hypcall(vcpu); 87 + break; 88 + 85 89 default: 86 90 BUG(); 87 91 }