From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0ADDC433F5 for ; Thu, 9 Sep 2021 12:00:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9B349632A0 for ; Thu, 9 Sep 2021 12:00:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244590AbhIIMBF (ORCPT ); Thu, 9 Sep 2021 08:01:05 -0400 Received: from out4436.biz.mail.alibaba.com ([47.88.44.36]:19780 "EHLO out4436.biz.mail.alibaba.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240169AbhIIL4t (ORCPT ); Thu, 9 Sep 2021 07:56:49 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R451e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04420;MF=houwenlong93@linux.alibaba.com;NM=1;PH=DS;RN=15;SR=0;TI=SMTPD_---0UnnCNDA_1631188526; Received: from localhost(mailfrom:houwenlong93@linux.alibaba.com fp:SMTPD_---0UnnCNDA_1631188526) by smtp.aliyun-inc.com(127.0.0.1); Thu, 09 Sep 2021 19:55:27 +0800 From: Hou Wenlong To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), "H. Peter Anvin" , Jan Kiszka , Avi Kivity , linux-kernel@vger.kernel.org (open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)) Subject: [PATCH v2 1/3] kvm: x86: Introduce hypercall x86 ops for handling hypercall not in cpl0 Date: Thu, 9 Sep 2021 19:55:23 +0800 Message-Id: <04a337801ad5aaa54144dc57df8ee2fc32bc9c4e.1631188011.git.houwenlong93@linux.alibaba.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Per Intel's SDM, use vmcall instruction in non VMX operation for cpl3 it should trigger a #UD. And in VMX root operation, it should trigger a #GP for cpl3. So hypervisor could inject such exceptions for guest cpl3 to act like host. Per AMD's APM, no cpl check for vmmcall instruction. But use it in host can trigger a #UD, so hypervisor is suitable to inject a #UD. Fixes: 07708c4af1346 ("KVM: x86: Disallow hypercalls for guest callers in rings > 0") Signed-off-by: Hou Wenlong --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 9 +++++++++ arch/x86/kvm/x86.c | 6 +++--- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index cefe1d81e2e8..00a8b8c80cb0 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -60,6 +60,7 @@ KVM_X86_OP_NULL(update_emulated_instruction) KVM_X86_OP(set_interrupt_shadow) KVM_X86_OP(get_interrupt_shadow) KVM_X86_OP(patch_hypercall) +KVM_X86_OP(handle_hypercall_fail) KVM_X86_OP(set_irq) KVM_X86_OP(set_nmi) KVM_X86_OP(queue_exception) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f8f48a7ec577..3548c8047820 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1369,6 +1369,7 @@ struct kvm_x86_ops { u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu); void (*patch_hypercall)(struct kvm_vcpu *vcpu, unsigned char *hypercall_addr); + void (*handle_hypercall_fail)(struct kvm_vcpu *vcpu); void (*set_irq)(struct kvm_vcpu *vcpu); void (*set_nmi)(struct kvm_vcpu *vcpu); void (*queue_exception)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 1a70e11f0487..a8048e5b2aff 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3944,6 +3944,11 @@ svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) hypercall[2] = 0xd9; } +static void svm_handle_hypercall_fail(struct kvm_vcpu *vcpu) +{ + kvm_queue_exception(vcpu, UD_VECTOR); +} + static int __init svm_check_processor_compat(void) { return 0; @@ -4563,6 +4568,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .set_interrupt_shadow = svm_set_interrupt_shadow, .get_interrupt_shadow = svm_get_interrupt_shadow, .patch_hypercall = svm_patch_hypercall, + .handle_hypercall_fail = svm_handle_hypercall_fail, .set_irq = svm_set_irq, .set_nmi = svm_inject_nmi, .queue_exception = svm_queue_exception, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0c2c0d5ae873..3bd66eb46309 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4921,6 +4921,14 @@ vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) hypercall[2] = 0xc1; } +static void vmx_handle_hypercall_fail(struct kvm_vcpu *vcpu) +{ + if (to_vmx(vcpu)->nested.vmxon) + kvm_queue_exception_e(vcpu, GP_VECTOR, 0); + else + kvm_queue_exception(vcpu, UD_VECTOR); +} + /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) { @@ -7606,6 +7614,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .set_interrupt_shadow = vmx_set_interrupt_shadow, .get_interrupt_shadow = vmx_get_interrupt_shadow, .patch_hypercall = vmx_patch_hypercall, + .handle_hypercall_fail = vmx_handle_hypercall_fail, .set_irq = vmx_inject_irq, .set_nmi = vmx_inject_nmi, .queue_exception = vmx_queue_exception, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 28ef14155726..4e2836b94a01 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8665,8 +8665,8 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) } if (static_call(kvm_x86_get_cpl)(vcpu) != 0) { - ret = -KVM_EPERM; - goto out; + static_call(kvm_x86_handle_hypercall_fail)(vcpu); + return 1; } ret = -KVM_ENOSYS; @@ -8727,7 +8727,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret = -KVM_ENOSYS; break; } -out: + if (!op_64_bit) ret = (u32)ret; kvm_rax_write(vcpu, ret); -- 2.31.1