Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp460382pxb; Thu, 9 Sep 2021 05:06:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8x7MpGtTSLPckHCljvUDXNMP60zoyG4TW6rUsIcmfhKGPMqFlX7H/4sYayemkVXw3W7Z1 X-Received: by 2002:a17:906:c246:: with SMTP id bl6mr3077753ejb.80.1631189184518; Thu, 09 Sep 2021 05:06:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631189184; cv=none; d=google.com; s=arc-20160816; b=vX/lNPW+0mbl8uEljWYMCpRO6MZQZayTE7AxU928MwHXOY4jOmWEp0HNvPb0kz9AJh z5aRU4unRpU4anuIXtbuVBOKp8szpR7BFyWuyBZt43uVk7gsper+RIZFNc30rv7a5viI LeXdHAl9ivFjG+3ssV7WCMUJUOlxbWaDMyLPRYtfbRd5H6hH1VmVe3iTQOXM5EcSje2+ +Y6IkOkvTBeyH2kvfpGVpazfrdwFO8Jlnpw7gt7i6mZCudyzDOYUyzsPFlFwjiuSQvqy K84aILdYmZWAgeK6L77RQ0YvrvRDgMdwM9AXj2uyTzXsXIUM6bBfj+RMDAYBdrBf7/he Kv9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=vaddbmbAUyoZdYO+TvfOyrjuYi75qolrdgDGXWSBU5Y=; b=EFqD6smvtShOUQdwxoGiQgQ9RImvqhiGeE0vvZYIGfIY2+JPvXlwojfPdFOBIPJqjW XmQ3hEQDc/8yHEsuG52Boo7tx5FoQdShsotFZHDD3YFsxv8MtDMxg2qtQjP1HW1PtFPb XxkoA6V+xygOU8dhiMOoMVbI6xYmyobc+tF6Pf4MjoLMDocxCcpLRYyZ1QhR1vluKL4M NeyhduhzeXCX5rIInr9uJij/CV/2lSUfgH8Njnj45yMt27bnjwALMRW8mtIiaTQXRgPW Pg4DirmJQlHxl2pG1BzRqm5G2Lm9ZfXHVy1uRrX7/kdScVTYE81G8N4xn+TnFbjRFnXX 3yPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jz19si1720106ejb.599.2021.09.09.05.05.44; Thu, 09 Sep 2021 05:06:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242103AbhIIMBE (ORCPT + 99 others); Thu, 9 Sep 2021 08:01:04 -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