From patchwork Fri Jun 4 17:26:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300357 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 28319C4743D for ; Fri, 4 Jun 2021 17:27:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1054061405 for ; Fri, 4 Jun 2021 17:27:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230261AbhFDR3P (ORCPT ); Fri, 4 Jun 2021 13:29:15 -0400 Received: from mail-pj1-f73.google.com ([209.85.216.73]:53067 "EHLO mail-pj1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbhFDR3P (ORCPT ); Fri, 4 Jun 2021 13:29:15 -0400 Received: by mail-pj1-f73.google.com with SMTP id pf14-20020a17090b1d8eb029015c31e36747so5620041pjb.2 for ; Fri, 04 Jun 2021 10:27:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=p5DAeovxZ7wrgmDRjP+sOlFBRY+PyiE6SmRmHOZJBYY=; b=RVGm+2FC7bIETqrHZ4snFv9hATJXMrwKRNf2M7TawTlRXmjDbeV4QBbrJAD7etmVtT VPH4vuH92+JaMz65UJ/fErWbFwk2dV5z2R5aG+QIJ+lLEFCtV4X/TDRpFJ6SyGteMAt/ zrRvjqN4zITmypX0IpPqO3ILSlGoK7dxXWcJlHpV8a8Y3VBfzuycl7HUkDLcEs40KBmR VuW18U+N85pUK3FgXkz6QwIunLIxmTcjMYXCjMNQG1zv0lxjtMRF/PSHKI64mVCUl5Fs 6TT8VcTM7u6OMOBXcsQ6w1xJO49D7pW4UVZaUEZoJWtN0swd5+NgLyusau7SZIv0WNIC 7ZDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=p5DAeovxZ7wrgmDRjP+sOlFBRY+PyiE6SmRmHOZJBYY=; b=DJrj2S2uuOjnX0aEtPgjPDz6sCm6dvGJUnOS5FO3FMT7kEcXikazZi2hyCSOu8/oGs NMJ8mr2Mv2uCFfm9iWku+TbtEN/uH7ZI4vN7x3wjO9soiBskyy5NEcucatyd2oXVtvE+ v9BO3AO/Qf/oR7fW/7BZ72jcKyTlIIXe1JanhpMQdxQTpX3n6t9kPnDUxPwkbdmCebkC KJMxRjIQqUhGjJcDEJiABNVCt9+BcHDZRParP82ZsCq1nQqJkwrbcEPQvLE3AkoT7ZJD 5nzDYvDzt0Rw4vSwc1Oyi3BzGz5oYBJuqHiqy7NOZXzSLIzp0haaYWHiowM1ceWHwSDn UZ5g== X-Gm-Message-State: AOAM530+DLKXAYt+EqYgh8dw0T6cxMUOunSsIVq+ZfgZeh5LelutADby Ivj43RQxYg0aQ+bntudy2jLZ5bb0cc6RNA2GYY2j5XGbUUP7v71WWQOUWwfRPhAIj1Udb4/xa7M Sv+bKO3eyfzAFcHeP/GZFjCNfm5VyEl5lkmNGtKCvpR7C3FjSsGJceuzutcTenms= X-Google-Smtp-Source: ABdhPJzhKqG5tmYLa5uJHQ8cmrddNJFo0oukfoU8R5YBbZKpbh7rDh/bc0d7qYZFc5wgbyw/F+s1N5JIhvW4gg== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a63:b955:: with SMTP id v21mr6140950pgo.230.1622827588623; Fri, 04 Jun 2021 10:26:28 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:00 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-2-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 01/12] KVM: x86: Remove guest mode check from kvm_check_nested_events From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org A survey of the callsites reveals that they all ensure the vCPU is in guest mode before calling kvm_check_nested_events. Remove this dead code so that the only negative value this function returns (at the moment) is -EBUSY. Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b594275d49b5..882457e92679 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8535,9 +8535,6 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu) int kvm_check_nested_events(struct kvm_vcpu *vcpu) { - if (WARN_ON_ONCE(!is_guest_mode(vcpu))) - return -EIO; - if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { kvm_x86_ops.nested_ops->triple_fault(vcpu); return 1; From patchwork Fri Jun 4 17:26:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300359 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 33756C4743C for ; Fri, 4 Jun 2021 17:27:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F43F61402 for ; Fri, 4 Jun 2021 17:27:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230321AbhFDR3S (ORCPT ); Fri, 4 Jun 2021 13:29:18 -0400 Received: from mail-yb1-f201.google.com ([209.85.219.201]:41541 "EHLO mail-yb1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbhFDR3R (ORCPT ); Fri, 4 Jun 2021 13:29:17 -0400 Received: by mail-yb1-f201.google.com with SMTP id j7-20020a258b870000b029052360b1e3e2so12649135ybl.8 for ; Fri, 04 Jun 2021 10:27:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=HULabYlXk0tW3Y4DYJCJlDwv7KKOGlzX9lsq/vIfMJQ=; b=DpMAqgReqQDxRs5hKv1q9sPsnWBtRmQvXpHpvUo6jHi0jTJ9FIe+Ig1v9akWL1pQu4 Sjw4N0HoVGxj5HAhJRyPVaGgVDfwJv0ViHPtrITkSAUw3iOF3IC10mx/ts7siU2f6VpT BU0459Ay6aSGyc4RKGbFJAD60le9XqUFbrTpg4s+rN8SR6dLzYBXQjgYXVPBV+tpWCfg ZX+DD/d/XACzFVT+L+yRVaGcJfe3wW933H3ySz7DUFeSar977NGLfLrOqggtJCUD+YOV LN3DXHwSNChWaDZm4cuknbvEpXqigf+c8y8m1fuByn0qowHb8ArRd1EjWRwJMbgnDcdj JU5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=HULabYlXk0tW3Y4DYJCJlDwv7KKOGlzX9lsq/vIfMJQ=; b=sTDfP/+4EwSJgKAV3PsyRgYSw5vx/2cBFAZGwKwbPq8IrzOG0aggdL4iFR14/tYEPE 5MfmKXuiVebQsib17PL7+uUZPr2yz2UaVOkj3lJ1Aw7yfYPaDyrIDab6MsmFt6eidRPe p/FcxygfrrXn7ft+xJQx2YbFKbqqg9/WSVKD/AxYOmLeHW4YeSFDN0+b/+glayAXDrPi 44iUYvwMiF00/eoTLQ1eiScqmggHXO2HDNdRYEJRvOYF5jApOo0ZvtQDyYFJwekShAmP kT0LRkW18m/ihBlbTUwxoYmCaHfxoFm9VCg0bNCgilWz5eiRR3nEfSjw30kpIBZFR03I bQZQ== X-Gm-Message-State: AOAM532buQMEN1xSq2qA27Oca3+J9h5uN+mP3gaNPtC8UL3p7ShKmQYj k0de+6YzTqomy0lBUGxmI5Z+x5qsWeMNnStltcouZw0n1acW0k0u2lVSHlOAoMTuOH57iEYRTyC 6EZfPzYywB3xZv4mOIRMOjDhQDrSqEq0aZtDUij8wMej+C2Jg29uXX0uN2YNIqFU= X-Google-Smtp-Source: ABdhPJxBYVuPepeePEQ/83cwzxTgXNNW7XmRqVaFSG6wB5hxB33E9sepRngJimlf4PnS32cDuV5Op9icVdGpkA== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a25:c547:: with SMTP id v68mr6706528ybe.361.1622827590598; Fri, 04 Jun 2021 10:26:30 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:01 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-3-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 02/12] KVM: x86: Wake up a vCPU when kvm_check_nested_events fails From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org At present, there are two reasons why kvm_check_nested_events may return a non-zero value: 1) we just emulated a shutdown VM-exit from L2 to L1. 2) we need to perform an immediate VM-exit from vmcs02. In either case, transition the vCPU to "running." Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 882457e92679..83bc0a5b1aab 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9471,8 +9471,8 @@ static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) { - if (is_guest_mode(vcpu)) - kvm_check_nested_events(vcpu); + if (is_guest_mode(vcpu) && kvm_check_nested_events(vcpu)) + return true; return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && !vcpu->arch.apf.halted); From patchwork Fri Jun 4 17:26:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300345 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=-23.5 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 661D7C4743E for ; Fri, 4 Jun 2021 17:26:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E56461402 for ; Fri, 4 Jun 2021 17:26:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230445AbhFDR2e (ORCPT ); Fri, 4 Jun 2021 13:28:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230411AbhFDR2d (ORCPT ); Fri, 4 Jun 2021 13:28:33 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC2CDC061766 for ; Fri, 4 Jun 2021 10:26:32 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id n22-20020a6372160000b0290220c022078cso2695624pgc.17 for ; Fri, 04 Jun 2021 10:26:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=FX10PQdLzDz16GdUbiM5sgnv+3jGZM7Tq2+dVV1y+FU=; b=oITuushgDMrGVmDQZkWx+majdGJ85wT6icbDWQMZtklgfjfrXrlUjCZBbRlFL2BTPa pcDmjrI6mgQIiLcl9OYKh5Jd6LRs4rIqI1xXAyBumiCLhP4AUGNU9qOGvnfqgTMVFW54 QdLWKynUHl11q2IZTrKp3OEkBe2v+LNn1LvoZ1ht0whNUzYxzAwknRWv1gGiBt0ad1ct tkf3E9Xz1Oa4VMWkO+fNu3xCMAFnaHXdMdm2aT9h1YcgMxgSo2XgFj8a7UzImaAF2NTj cyuVPUZVZHtO4zeKD98AyS97iQiCQJcbD5bg4WKe98Zjnz64mEq2RWd+1bGVj3Mg0Tby IV2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=FX10PQdLzDz16GdUbiM5sgnv+3jGZM7Tq2+dVV1y+FU=; b=hdpA4gZsrxwoYZiNcgh9wc01gowICmE4VMjHpJr0y0oHOm5C74Hzv9eGnUqPfZCYNd bHhV8xXunmemTzffiGKCUc7WGxb8VnLAVkq1DpkKWo2qJXqmiWmU4XuQr5XNgmEUIB4U Ji6wi4OUlR+lJpBmvAX7zuh+AvXXtUu05ZSFw30O01is7mvzC+OLWunebVexMBm3ZIsm y7xkd0HDZwUeZHIFHVplNQ0Tw1dDGPNAfXnKLKXFkXgAmknJEJDGZAl1EpCgT5741F+o YqvhJ6JHk9+ayN69pxrHEbuouMmCPmK2EVQRk9dRFn+vhmBEdzd+sf72hcxwmqK7m3DH kbNw== X-Gm-Message-State: AOAM5339jThSolqLD4ldpCAxx3P6GbxfoO3rbX8bUQJv6tAmVedf+vDe sUaib7CggmVvL7FkGk9+u16jct4AE2YhW9SDkj7iy7qBvMzmGq2ygNH1aBlJhzjwtQMsjADfZx+ QxnZ/cAY2mw+pw58sJhj5h+92tjfS5xe1JlTC/bXNwuo7CKSpXtxrpObi66NPJqQ= X-Google-Smtp-Source: ABdhPJwbGFq+vz1fxQkZjZ+qO5HhG/X/iQzGnGVVoEcSDTC2l+UiSXMm2sQH/frmb72TndXlY/wsY6Q13gOR0w== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a62:1a49:0:b029:2eb:6de0:9890 with SMTP id a70-20020a621a490000b02902eb6de09890mr5394966pfa.39.1622827592354; Fri, 04 Jun 2021 10:26:32 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:02 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-4-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 03/12] KVM: nVMX: Add a return code to vmx_complete_nested_posted_interrupt From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org No functional change intended. Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- arch/x86/kvm/vmx/nested.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 6058a65a6ede..7646e6e561ad 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3682,7 +3682,7 @@ void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) } } -static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) +static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); int max_irr; @@ -3690,17 +3690,17 @@ static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) u16 status; if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) - return; + return 0; vmx->nested.pi_pending = false; if (!pi_test_and_clear_on(vmx->nested.pi_desc)) - return; + return 0; max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); if (max_irr != 256) { vapic_page = vmx->nested.virtual_apic_map.hva; if (!vapic_page) - return; + return 0; __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page, &max_irr); @@ -3713,6 +3713,7 @@ static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) } nested_mark_vmcs12_pages_dirty(vcpu); + return 0; } static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, @@ -3887,8 +3888,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu) } no_vmexit: - vmx_complete_nested_posted_interrupt(vcpu); - return 0; + return vmx_complete_nested_posted_interrupt(vcpu); } static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) From patchwork Fri Jun 4 17:26:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300361 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 20DFEC4743C for ; Fri, 4 Jun 2021 17:27:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0BCDC61402 for ; Fri, 4 Jun 2021 17:27:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230129AbhFDR3V (ORCPT ); Fri, 4 Jun 2021 13:29:21 -0400 Received: from mail-pj1-f74.google.com ([209.85.216.74]:43663 "EHLO mail-pj1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229778AbhFDR3V (ORCPT ); Fri, 4 Jun 2021 13:29:21 -0400 Received: by mail-pj1-f74.google.com with SMTP id lb16-20020a17090b4a50b029016c399441a6so1563687pjb.8 for ; Fri, 04 Jun 2021 10:27:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Afz60ODUyVuGs6cT0xPAEAAlQEDiab7pQP9o9bn171c=; b=jq5pmzzHyboRqtjbcyihIJHwv4HNkLGLSm7x4ZNim1ITmtsR/zk8TVwMESJzhd+a/3 mSXx1kxdIsA/wPeSW3mSK3+H88LlvGF03mxu0gk5/8vocy0jf43XWZz/khsisiiOb8HO qTq5RgcflORARQv5qJFKtArGFOZt6RJ1vkwsoDrymbkC1FM+IXpCZM0KzhPBfKmfn620 +b5Vns+m8VUwE2CuM/qIAJ3hTfsC68MbTdeWdW2o4JgNrOD+dahd3O9n2DdFZmoXyoTQ jh0rxtKOzbeDSMtEi67CjscRoYL4QtHbmpfDH2NgC671jrtzEwLuWM8e+pQwZDx3XeUQ P9fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Afz60ODUyVuGs6cT0xPAEAAlQEDiab7pQP9o9bn171c=; b=eJi5UP/MHKn3p5Lq7DsuNlIMrTqSizwuF7pPjbhGR2RC3Mp7wIOux94Nem5OfPU5Yw /hI6c+UhBNLVjF7a+ETT/hSczv5wParCZyZS8+x2aFSnD4va4pVDTJ9CtWMZByLTIkbu 63+BfXqtc53Q4pIdCeZWe3yUptLQDRJRdpWCJF6iqN+QUvfZRqjie7WseDW+JYYa3iSG xjc5Ka+XE898OBhFCtfRddLOB+tjMM4iVJVkG/SBbe3uR6+ZUnBuyP7WRlIrO2CESL4/ RE0zTYe3KPUhsPdbAAtTMnQRJ8FnocvPC/9RsDFWjW8oxqZsFFUHtjPlbezvws97ugnG Ijhg== X-Gm-Message-State: AOAM532pHBfYn7LQOrilwdIGntrhWL27VS/w0XhimJ7ZINQ8Ms1HEsTb NRDxGf19lEpMuN9+5m1Tapin7V1uo2pEDJVq+n3sxKs+BBHg4NnOQ4tJOgbr3LH1MRFkZ3H3gB1 eLXB5xraQqpSSu0NDvovQ1U5W+b9W5AMTQnW4tovbj84FVJ8SpjO7XyniDDYe6rw= X-Google-Smtp-Source: ABdhPJzourejcuLTj7/DSLVmJKeiPgsSnrAOzh2u8MRhD23sv23J9NsnKoiJTSLy7jW4sSqJQxYOQI5l3beHWg== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a17:90b:3b41:: with SMTP id ot1mr1545544pjb.1.1622827594107; Fri, 04 Jun 2021 10:26:34 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:03 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-5-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 04/12] KVM: x86: Add a return code to inject_pending_event From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org No functional change intended. At present, 'r' will always be -EBUSY on a control transfer to the 'out' label. Signed-off-by: Jim Mattson --- arch/x86/kvm/x86.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 83bc0a5b1aab..f9b3ea916344 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8550,7 +8550,7 @@ static void kvm_inject_exception(struct kvm_vcpu *vcpu) static_call(kvm_x86_queue_exception)(vcpu); } -static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) +static int inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) { int r; bool can_inject = true; @@ -8597,7 +8597,7 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit if (is_guest_mode(vcpu)) { r = kvm_check_nested_events(vcpu); if (r < 0) - goto busy; + goto out; } /* try to inject new event if pending */ @@ -8639,7 +8639,7 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit if (vcpu->arch.smi_pending) { r = can_inject ? static_call(kvm_x86_smi_allowed)(vcpu, true) : -EBUSY; if (r < 0) - goto busy; + goto out; if (r) { vcpu->arch.smi_pending = false; ++vcpu->arch.smi_count; @@ -8652,7 +8652,7 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit if (vcpu->arch.nmi_pending) { r = can_inject ? static_call(kvm_x86_nmi_allowed)(vcpu, true) : -EBUSY; if (r < 0) - goto busy; + goto out; if (r) { --vcpu->arch.nmi_pending; vcpu->arch.nmi_injected = true; @@ -8667,7 +8667,7 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit if (kvm_cpu_has_injectable_intr(vcpu)) { r = can_inject ? static_call(kvm_x86_interrupt_allowed)(vcpu, true) : -EBUSY; if (r < 0) - goto busy; + goto out; if (r) { kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); static_call(kvm_x86_set_irq)(vcpu); @@ -8683,11 +8683,14 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit *req_immediate_exit = true; WARN_ON(vcpu->arch.exception.pending); - return; + return 0; -busy: - *req_immediate_exit = true; - return; +out: + if (r == -EBUSY) { + *req_immediate_exit = true; + r = 0; + } + return r; } static void process_nmi(struct kvm_vcpu *vcpu) @@ -9248,7 +9251,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto out; } - inject_pending_event(vcpu, &req_immediate_exit); + r = inject_pending_event(vcpu, &req_immediate_exit); + if (r < 0) { + r = 0; + goto out; + } if (req_int_win) static_call(kvm_x86_enable_irq_window)(vcpu); From patchwork Fri Jun 4 17:26:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300369 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 404BAC4743D for ; Fri, 4 Jun 2021 17:27:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F39B61405 for ; Fri, 4 Jun 2021 17:27:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230465AbhFDR3h (ORCPT ); Fri, 4 Jun 2021 13:29:37 -0400 Received: from mail-qt1-f201.google.com ([209.85.160.201]:54083 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbhFDR3g (ORCPT ); Fri, 4 Jun 2021 13:29:36 -0400 Received: by mail-qt1-f201.google.com with SMTP id i12-20020ac860cc0000b02901cb6d022744so5586433qtm.20 for ; Fri, 04 Jun 2021 10:27:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=XlUk4pd7ZXaHcyBXLC2fZswqyhx828J/Q4tmnaU+kr4=; b=sFU9tmBV/um9g06KK4DLM6sqpMu9jDxyML/HjlJ4fM2DPDpXFLIRY0qBGKvXj50O6T VON3PaDO3sIm7k92Aiqj2y4PGphPSpftp0PdRfAY0VEbmfrWEb0IPye5vmX8nOj3XOKV WpeyDNp5SXQy9cpL4792sSRVJ00I+uJHrWyz4033zKERuYrrVqv7akZy82rsYKUpaHl5 1L1WpWPK6rJlWr1PXD/HEAAkmOfkieMWtaKq71av9+Mbg4YqIo33Sg4kexbsJCDq3DO7 U5osZ2KoO44H5B8d0ghshSmGxi8n+hdMX8i535aABDMUlDfxCsdNZmRvWrxtoV3OMjNH F1yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XlUk4pd7ZXaHcyBXLC2fZswqyhx828J/Q4tmnaU+kr4=; b=m1e3n/T/+28JztflN8Kp+dO2ZlyMKCqlVyHXaEZQwUoUH07YIsLQfuXDULO7CkroH+ 1AsE9jfffd3VTTx599xbq59lYW3r82KWSd155J8a0pu6NAPeFBU+Ap1BLi6X+KGd8obE 4yta7eXkjlJ30T15nway2UZYTq/MdoANZG15y23H8UAB9tJkMwa2juD1BpzJENbphZNI a7yeVwoXBY7A8l20bwkEq+gpD56sNGG+/89WgGairhASgTYi1DyUDTb/OZS3u6Rhowzr nEfogsaWkRF1HWkW6YvtRhcG1US/YFogS3SNG8miMBZI3yoNTay4EYuoMJTrLE+alAXR X7iQ== X-Gm-Message-State: AOAM532t96g0/ATEw5WqaJRpaCfroJnSgiigrw7UAaQU+1hzt2E7IQHQ 37N+iv2rw3V+ThJ9HkvrGs3iWLglZ3dKVh5wBVB/AAXhsozvwZ4chAdRHt2T4IM85zfa8mFwrI1 rt5wquLZgchVxmfv8SoWYw7AVbxqJNihWJMICPLBo7Kp1QvTT+87Gd5PYNmPoE+o= X-Google-Smtp-Source: ABdhPJx3vuhC/RFsamaEmoMfknzBiNbGM73yxM8nJL/uu/cx/b/u6xaS5tiPJgvphwrEzWf9nRjlzrxxYGdA5w== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:ad4:4e2e:: with SMTP id dm14mr5916921qvb.33.1622827595917; Fri, 04 Jun 2021 10:26:35 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:04 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-6-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 05/12] KVM: x86: Add a return code to kvm_apic_accept_events From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org No functional change intended. At present, the only negative value returned by kvm_check_nested_events is -EBUSY. Signed-off-by: Jim Mattson --- arch/x86/kvm/lapic.c | 11 ++++++----- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/x86.c | 25 ++++++++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 8120e8614b92..6a315ade6c41 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2864,7 +2864,7 @@ int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len) return kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len); } -void kvm_apic_accept_events(struct kvm_vcpu *vcpu) +int kvm_apic_accept_events(struct kvm_vcpu *vcpu) { struct kvm_lapic *apic = vcpu->arch.apic; u8 sipi_vector; @@ -2872,7 +2872,7 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) unsigned long pe; if (!lapic_in_kernel(vcpu)) - return; + return 0; /* * Read pending events before calling the check_events @@ -2880,12 +2880,12 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) */ pe = smp_load_acquire(&apic->pending_events); if (!pe) - return; + return 0; if (is_guest_mode(vcpu)) { r = kvm_check_nested_events(vcpu); if (r < 0) - return; + return r == -EBUSY ? 0 : r; /* * If an event has happened and caused a vmexit, * we know INITs are latched and therefore @@ -2906,7 +2906,7 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED); if (test_bit(KVM_APIC_SIPI, &pe)) clear_bit(KVM_APIC_SIPI, &apic->pending_events); - return; + return 0; } if (test_bit(KVM_APIC_INIT, &pe)) { @@ -2927,6 +2927,7 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; } } + return 0; } void kvm_lapic_exit(void) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 997c45a5963a..d7c25d0c1354 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -76,7 +76,7 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu); int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu); int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu); int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu); -void kvm_apic_accept_events(struct kvm_vcpu *vcpu); +int kvm_apic_accept_events(struct kvm_vcpu *vcpu); void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event); u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f9b3ea916344..51d3b9ff4d96 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9245,7 +9245,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || kvm_xen_has_interrupt(vcpu)) { ++vcpu->stat.req_event; - kvm_apic_accept_events(vcpu); + r = kvm_apic_accept_events(vcpu); + if (r < 0) { + r = 0; + goto out; + } if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { r = 1; goto out; @@ -9457,7 +9461,8 @@ static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) return 1; } - kvm_apic_accept_events(vcpu); + if (kvm_apic_accept_events(vcpu) < 0) + return 0; switch(vcpu->arch.mp_state) { case KVM_MP_STATE_HALTED: case KVM_MP_STATE_AP_RESET_HOLD: @@ -9681,7 +9686,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) goto out; } kvm_vcpu_block(vcpu); - kvm_apic_accept_events(vcpu); + if (kvm_apic_accept_events(vcpu) < 0) { + r = 0; + goto out; + } kvm_clear_request(KVM_REQ_UNHALT, vcpu); r = -EAGAIN; if (signal_pending(current)) { @@ -9883,11 +9891,17 @@ int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { + int r; + vcpu_load(vcpu); if (kvm_mpx_supported()) kvm_load_guest_fpu(vcpu); - kvm_apic_accept_events(vcpu); + r = kvm_apic_accept_events(vcpu); + if (r < 0) + goto out; + r = 0; + if ((vcpu->arch.mp_state == KVM_MP_STATE_HALTED || vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD) && vcpu->arch.pv.pv_unhalted) @@ -9895,10 +9909,11 @@ int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, else mp_state->mp_state = vcpu->arch.mp_state; +out: if (kvm_mpx_supported()) kvm_put_guest_fpu(vcpu); vcpu_put(vcpu); - return 0; + return r; } int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, From patchwork Fri Jun 4 17:26:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300363 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 DC7AEC4743D for ; Fri, 4 Jun 2021 17:27:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C6E1F61405 for ; Fri, 4 Jun 2021 17:27:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231208AbhFDR3Y (ORCPT ); Fri, 4 Jun 2021 13:29:24 -0400 Received: from mail-pj1-f74.google.com ([209.85.216.74]:51033 "EHLO mail-pj1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230314AbhFDR3Y (ORCPT ); Fri, 4 Jun 2021 13:29:24 -0400 Received: by mail-pj1-f74.google.com with SMTP id s5-20020a17090a7645b029016d923cccbeso664304pjl.0 for ; Fri, 04 Jun 2021 10:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=hPqg2zEw8xWDxZPbY6MJzuzKRTdbGJVq1JFdi5WkfCk=; b=OhJpnQ64jlGrRhwDsKiuMuzPBsFqtfKAZefM5r1f5A6dQVyzU/xQRqqIoE+giSHCDq 8QVISi1tQF9uWXLZWGaYNd/vwtYR6XPX2y/jbb6n9Ij3UfR2aDFFfuVQDdF87JbvY26v YkYzEoNN+GyelnjBzV8xyXrxn+K6eeVgWdmvBRwNW4SI/ADUO1YLGz4ok2gw2uKWB7s6 un8XWPTlNOo2EgaQkoQh54mhkPdRN+Ul0ZqaLsFdUaBnZJJDHapFp2o77YvNbyW5anix FPLKbF2Qo6NwfqO3tqkZUJMQ3i7YjCrBm9OdXty4H2w94EiSiyqpv8L/g6qrRPihsX6t yAJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=hPqg2zEw8xWDxZPbY6MJzuzKRTdbGJVq1JFdi5WkfCk=; b=iphON5ayPsUJ9NsOI6RyE45no7kxxE8w8p3auuQpuwbdJeYhDxgct+vAPqNnS5hT0G wGdtF/6H+wd+HAkuVHR40ARdbHX3ibxjS+5Wx1YILkXHWmc3AuTbc7zohNrUY6djyew+ Lv9juZbMeDO4bdkp/hmDtbxXGJkIUiDtjDOzPGMFvc6N5/Vj+/bVpzXiNlzhet+7JOGm 5mDMQlmTi89CdaUMIJD5dqc32pFYpZn1uQQVY3ul0gzPlPTGjxlYo4zuoBx/VgRK/RO7 ur5Q4iB6QIJRFustrerRuoatGL3/Mz63fn84VPyprkTAgh8x1ODGmTpyKOkaegF4CvUq iuVg== X-Gm-Message-State: AOAM533a9Gfk2A007WyJo/2VQu6VFhBtIms9qM+tY3neWkC5RfNO03Wp LtW0ICW7wl8woHoRDzSzvBlxnm752dTnCRsOhxsu3lTF9m1kRG6FOR9YRnle7TOz2E4gOyiyxzY uB4NohO5RVhHQw/qaKgbTmPchf7Sfis0+OaL6VcQ3/RvXWhAqIq3KQ3CYVkigdg8= X-Google-Smtp-Source: ABdhPJwyzqPV8+hSeY9vyX5P/zbx6JKJG+QpCLyVuNOjGanv0TwxWmx4830vzff8SWcl92nvzbsNPsOGgvYmTA== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a17:902:ce84:b029:10f:a857:1e73 with SMTP id f4-20020a170902ce84b029010fa8571e73mr2654870plg.72.1622827597477; Fri, 04 Jun 2021 10:26:37 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:05 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-7-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 06/12] KVM: nVMX: Fail on MMIO completion for nested posted interrupts From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When the kernel has no mapping for the vmcs02 virtual APIC page, userspace MMIO completion is necessary to process nested posted interrupts. This is not a configuration that KVM supports. Rather than silently ignoring the problem, try to exit to userspace with KVM_INTERNAL_ERROR. Note that the event that triggers this error is consumed as a side-effect of a call to kvm_check_nested_events. On some paths (notably through kvm_vcpu_check_block), the error is dropped. In any case, this is an incremental improvement over always ignoring the error. Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx/nested.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 7646e6e561ad..706c31821362 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3700,7 +3700,7 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) if (max_irr != 256) { vapic_page = vmx->nested.virtual_apic_map.hva; if (!vapic_page) - return 0; + goto mmio_needed; __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page, &max_irr); @@ -3714,6 +3714,10 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) nested_mark_vmcs12_pages_dirty(vcpu); return 0; + +mmio_needed: + kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); + return -ENXIO; } static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, From patchwork Fri Jun 4 17:26:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300347 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 8D663C48BCF for ; Fri, 4 Jun 2021 17:26:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 79500613AD for ; Fri, 4 Jun 2021 17:26:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230411AbhFDR2i (ORCPT ); Fri, 4 Jun 2021 13:28:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231197AbhFDR2h (ORCPT ); Fri, 4 Jun 2021 13:28:37 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6A2FC061767 for ; Fri, 4 Jun 2021 10:26:39 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id jw3-20020a17090b4643b029016606f04954so6306287pjb.9 for ; Fri, 04 Jun 2021 10:26:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=rDhx1/I3uVd50WQu8s0ajnL0RFAgeDpmkbIdgj9rtVE=; b=i1V18uA4nxcgCUA6QIyUCnyBNwreGmHFm9Uh3Kr3ubb42mWqdXI1y1UtndN5VbACgh reRN+FsaRJEYUQkGJbnNVsWJmMs6gJqshJZPJJxYVJcQhjN7lrw+vADNHMgsOeRf6mvP MK+BKwjRHTA28pN1qSgOIGRBT/pXuLnuox38GOj6XaECX4qBwGU8w5++mnVPpNAWZEAE 3RJ9WGzx11vRZyhkHdGj3HrL4OvfpdL8wX9OUWcUbsDvLQRo7QEaSnKKFOUbkMhrvyfR b2MIGkAO47oYJVHWLPKSNxAU5XH788zqbJCyckC+UvIjq9w6T5aAPGpLiDJN+iMyTdHB g6pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=rDhx1/I3uVd50WQu8s0ajnL0RFAgeDpmkbIdgj9rtVE=; b=EA4TUys1+a+tLv6PiDodF7AsUeFzWlM7PSUNKE2ZPX2l1KSANhzNvO9GHBzDZDGc2j 4LQEELm9xSb0Zn69viQszvR6iTJ/YAx3fV/3T4JK8el3ovAMd1W91w29/MWMw4N+TJqX 6HYvzqbYXV0ewbTJ3Hm6xJetbD2WPlEJstOLcT4ElJ4NfCLESW2PnzYP1cAUDcfubX1N S1nL710HSiSw9LlDbbY5g3yMza+lgwwdjlEAQPZcwjIi1j+PdRRvUxHtXX1Yh7GTxdOo pg7Yvk7FSwK/W9fH5ZOFzM4uhQJ+nM0U97gRcm3cAPDJCs9Dd6a9CBs7L5DfUeeysU15 TM+A== X-Gm-Message-State: AOAM5301ddg9MfSYNEnIng8d9YJVUnRnsc6iN0W3GsG4oeHhLVamidgl rJv0elFHBzdy6nIZuijIo+TBDm7FsBgieHrtL8hE0g+6NEsbOp/kwnQkkklsyjMavTv4aKqTjNU eefAzDvCVUWDsF/Ee6QvBvy6LKCrN5e97hc0uy0OcQHDPmSbMgFnMIdJVWKcfPqU= X-Google-Smtp-Source: ABdhPJxpw06jOL/HSpoJoRz7FAh9kXbS1RFfsPh5kL1fb8L77jMaESKysGIrHb9an5g0uiriHFgrY3kE8IiWBA== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a17:90b:4504:: with SMTP id iu4mr6096631pjb.110.1622827599139; Fri, 04 Jun 2021 10:26:39 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:06 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-8-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 07/12] KVM: nVMX: Disable vmcs02 posted interrupts if vmcs12 PID isn't mappable From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Don't allow posted interrupts to modify a stale posted interrupt descriptor (including the initial value of 0). Empirical tests on real hardware reveal that a posted interrupt descriptor referencing an unbacked address has PCI bus error semantics (reads as all 1's; writes are ignored). However, kvm can't distinguish unbacked addresses from device-backed (MMIO) addresses, so it should really ask userspace for an MMIO completion. That's overly complicated, so just punt with KVM_INTERNAL_ERROR. Don't return the error until the posted interrupt descriptor is actually accessed. We don't want to break the existing kvm-unit-tests that assume they can launch an L2 VM with a posted interrupt descriptor that references MMIO space in L1. Fixes: 6beb7bd52e48 ("kvm: nVMX: Refactor nested_get_vmcs12_pages()") Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx/nested.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 706c31821362..632f0abfe154 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3175,6 +3175,15 @@ static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) offset_in_page(vmcs12->posted_intr_desc_addr)); vmcs_write64(POSTED_INTR_DESC_ADDR, pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr)); + } else { + /* + * Defer the KVM_INTERNAL_EXIT until KVM tries to + * access the contents of the VMCS12 posted interrupt + * descriptor. (Note that KVM may do this when it + * should not, per the architectural specification.) + */ + vmx->nested.pi_desc = NULL; + pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); } } if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) @@ -3689,10 +3698,14 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) void *vapic_page; u16 status; - if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) + if (!vmx->nested.pi_pending) return 0; + if (!vmx->nested.pi_desc) + goto mmio_needed; + vmx->nested.pi_pending = false; + if (!pi_test_and_clear_on(vmx->nested.pi_desc)) return 0; From patchwork Fri Jun 4 17:26:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300365 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 A0BC4C4743D for ; Fri, 4 Jun 2021 17:27:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D2326140C for ; Fri, 4 Jun 2021 17:27:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231273AbhFDR32 (ORCPT ); Fri, 4 Jun 2021 13:29:28 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:55197 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbhFDR31 (ORCPT ); Fri, 4 Jun 2021 13:29:27 -0400 Received: by mail-pf1-f202.google.com with SMTP id d17-20020aa781510000b02902e921bdea05so5722364pfn.21 for ; Fri, 04 Jun 2021 10:27:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=1dLh43sRzXKlMYZMKws7XnkKCPB3IC+/0BCvd5bfEb4=; b=wEB+aiv3QlRV5Bl2C3a2vo2Oq9fjvPOCJq1Cu3SUTlEn4tBSwkruAqOSFGHAq8IU2u hOunnmbirTIF0GzNy4Tf4X0sn8R38nMBXRByn/EwoeMLQLSl4yhZ0CzyyAe1N8+RGqwT XLtHPGon+A6Zorct87mCOl0O5TXYnAAGNuND/kWpIap4cdwS8M793N/mldRUeKyxML9E IMPW7BbfqrFPv/tVmbURE/TdEGgmldulnZrOt0fcKujSRm45CAzSO0cFz3XLfKZIFRt+ 9+/FaqEtPiU2jCgjtQ3v+QDlmfuKARS/OxgAM9eotPOGthK/ycvMVSZWwSqBvhmS6eD3 /Ndw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=1dLh43sRzXKlMYZMKws7XnkKCPB3IC+/0BCvd5bfEb4=; b=BXblUrrTPt7NybnRYWEnfs+gWy7QSxqeLM7Hzr+egmBTZcAYOJa+hX4Xn+9jpDHS/g TIOB+hpO32lveCivzhoTFBipGV4WzSuI6hPoK8wa5wieZF8r+P9keuvbpU64+Z5lGi8q aNeaHGbz2BDEP2Jh3sbOqQCSFuJ0WbyvgjI76Ye1h7Ls2hk7yXkxac0X2aMr8UZg33sb rkVwQ3P3uc1jY/JVuW/y5IbU3/QeGVMqEIy/kkODbzyEqqGqOwHFinMz0frrdgdU1qIT NjaeoxEjNPJFJTlg5p7JdAEHBqDXJydagrcyNhvOfDsSO3P/MahYBBcn//vu6nw2xNtk A5yQ== X-Gm-Message-State: AOAM533R000/kEjIQ7dR+rLBl/DW2JX6eFY61WNVbHGd+zm6DTxaO4du evcedTG67ZzaUNVl9p+Bt8z8x8jg3Eu1IDN2KW00Qi+G0JkYFD/ABgm35AeGqDjuOPJH3ZIWjSa pdF+Upmh+yETP74AaBGZVpW8wn6YJJl9qwVh9/oh32zZwDQgb2Emx7scj/e85/8g= X-Google-Smtp-Source: ABdhPJzOac3kB7ozYzik4KIRn8hYkBcZDDtzdZGrKylyWXy2cJohiKrCx7Ss4hhPLieuzIDtzhlDNPdJQVGpSA== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a05:6a00:a1d:b029:2e9:3aec:89c2 with SMTP id p29-20020a056a000a1db02902e93aec89c2mr5466087pfh.68.1622827600904; Fri, 04 Jun 2021 10:26:40 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:07 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-9-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 08/12] KVM: selftests: Move APIC definitions into a separate file From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Processor.h is a hodgepodge of definitions. Though the local APIC is technically built into the CPU these days, move the APIC definitions into a new header file: apic.h. Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- .../selftests/kvm/include/x86_64/apic.h | 58 +++++++++++++++++++ .../selftests/kvm/include/x86_64/processor.h | 47 --------------- .../selftests/kvm/include/x86_64/vmx.h | 1 + 3 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/apic.h diff --git a/tools/testing/selftests/kvm/include/x86_64/apic.h b/tools/testing/selftests/kvm/include/x86_64/apic.h new file mode 100644 index 000000000000..0d0e35c8866b --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/apic.h @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * tools/testing/selftests/kvm/include/x86_64/apic.h + * + * Copyright (C) 2021, Google LLC. + */ + +#ifndef SELFTEST_KVM_APIC_H +#define SELFTEST_KVM_APIC_H + +#define APIC_DEFAULT_GPA 0xfee00000ULL + +/* APIC base address MSR and fields */ +#define MSR_IA32_APICBASE 0x0000001b +#define MSR_IA32_APICBASE_BSP (1<<8) +#define MSR_IA32_APICBASE_EXTD (1<<10) +#define MSR_IA32_APICBASE_ENABLE (1<<11) +#define MSR_IA32_APICBASE_BASE (0xfffff<<12) +#define GET_APIC_BASE(x) (((x) >> 12) << 12) + +#define APIC_BASE_MSR 0x800 +#define X2APIC_ENABLE (1UL << 10) +#define APIC_ID 0x20 +#define APIC_LVR 0x30 +#define GET_APIC_ID_FIELD(x) (((x) >> 24) & 0xFF) +#define APIC_TASKPRI 0x80 +#define APIC_PROCPRI 0xA0 +#define APIC_EOI 0xB0 +#define APIC_SPIV 0xF0 +#define APIC_SPIV_FOCUS_DISABLED (1 << 9) +#define APIC_SPIV_APIC_ENABLED (1 << 8) +#define APIC_ICR 0x300 +#define APIC_DEST_SELF 0x40000 +#define APIC_DEST_ALLINC 0x80000 +#define APIC_DEST_ALLBUT 0xC0000 +#define APIC_ICR_RR_MASK 0x30000 +#define APIC_ICR_RR_INVALID 0x00000 +#define APIC_ICR_RR_INPROG 0x10000 +#define APIC_ICR_RR_VALID 0x20000 +#define APIC_INT_LEVELTRIG 0x08000 +#define APIC_INT_ASSERT 0x04000 +#define APIC_ICR_BUSY 0x01000 +#define APIC_DEST_LOGICAL 0x00800 +#define APIC_DEST_PHYSICAL 0x00000 +#define APIC_DM_FIXED 0x00000 +#define APIC_DM_FIXED_MASK 0x00700 +#define APIC_DM_LOWEST 0x00100 +#define APIC_DM_SMI 0x00200 +#define APIC_DM_REMRD 0x00300 +#define APIC_DM_NMI 0x00400 +#define APIC_DM_INIT 0x00500 +#define APIC_DM_STARTUP 0x00600 +#define APIC_DM_EXTINT 0x00700 +#define APIC_VECTOR_MASK 0x000FF +#define APIC_ICR2 0x310 +#define SET_APIC_DEST_FIELD(x) ((x) << 24) + +#endif /* SELFTEST_KVM_APIC_H */ diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 0b30b4e15c38..a4729d9032ce 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -425,53 +425,6 @@ struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpui #define X86_CR0_CD (1UL<<30) /* Cache Disable */ #define X86_CR0_PG (1UL<<31) /* Paging */ -#define APIC_DEFAULT_GPA 0xfee00000ULL - -/* APIC base address MSR and fields */ -#define MSR_IA32_APICBASE 0x0000001b -#define MSR_IA32_APICBASE_BSP (1<<8) -#define MSR_IA32_APICBASE_EXTD (1<<10) -#define MSR_IA32_APICBASE_ENABLE (1<<11) -#define MSR_IA32_APICBASE_BASE (0xfffff<<12) -#define GET_APIC_BASE(x) (((x) >> 12) << 12) - -#define APIC_BASE_MSR 0x800 -#define X2APIC_ENABLE (1UL << 10) -#define APIC_ID 0x20 -#define APIC_LVR 0x30 -#define GET_APIC_ID_FIELD(x) (((x) >> 24) & 0xFF) -#define APIC_TASKPRI 0x80 -#define APIC_PROCPRI 0xA0 -#define APIC_EOI 0xB0 -#define APIC_SPIV 0xF0 -#define APIC_SPIV_FOCUS_DISABLED (1 << 9) -#define APIC_SPIV_APIC_ENABLED (1 << 8) -#define APIC_ICR 0x300 -#define APIC_DEST_SELF 0x40000 -#define APIC_DEST_ALLINC 0x80000 -#define APIC_DEST_ALLBUT 0xC0000 -#define APIC_ICR_RR_MASK 0x30000 -#define APIC_ICR_RR_INVALID 0x00000 -#define APIC_ICR_RR_INPROG 0x10000 -#define APIC_ICR_RR_VALID 0x20000 -#define APIC_INT_LEVELTRIG 0x08000 -#define APIC_INT_ASSERT 0x04000 -#define APIC_ICR_BUSY 0x01000 -#define APIC_DEST_LOGICAL 0x00800 -#define APIC_DEST_PHYSICAL 0x00000 -#define APIC_DM_FIXED 0x00000 -#define APIC_DM_FIXED_MASK 0x00700 -#define APIC_DM_LOWEST 0x00100 -#define APIC_DM_SMI 0x00200 -#define APIC_DM_REMRD 0x00300 -#define APIC_DM_NMI 0x00400 -#define APIC_DM_INIT 0x00500 -#define APIC_DM_STARTUP 0x00600 -#define APIC_DM_EXTINT 0x00700 -#define APIC_VECTOR_MASK 0x000FF -#define APIC_ICR2 0x310 -#define SET_APIC_DEST_FIELD(x) ((x) << 24) - /* VMX_EPT_VPID_CAP bits */ #define VMX_EPT_VPID_CAP_AD_BITS (1ULL << 21) diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h index 65eb1079a161..516c81d86353 100644 --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h @@ -10,6 +10,7 @@ #include #include "processor.h" +#include "apic.h" /* * Definitions of Primary Processor-Based VM-Execution Controls. From patchwork Fri Jun 4 17:26:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300367 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 8B90CC4743C for ; Fri, 4 Jun 2021 17:27:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 74C1D61405 for ; Fri, 4 Jun 2021 17:27:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231281AbhFDR3a (ORCPT ); Fri, 4 Jun 2021 13:29:30 -0400 Received: from mail-yb1-f201.google.com ([209.85.219.201]:56078 "EHLO mail-yb1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbhFDR33 (ORCPT ); Fri, 4 Jun 2021 13:29:29 -0400 Received: by mail-yb1-f201.google.com with SMTP id m194-20020a2526cb0000b02905375d41acd7so12657432ybm.22 for ; Fri, 04 Jun 2021 10:27:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=fI8pxJuBvcTSJcYb+gcMX6vyXH4iVi6Ryn52o35lufE=; b=OIMxMmMcMDTEikfdbYs4QFgl7LSKbguFsO6botGFgO5a7m4A4LlhG3Nm5M7UZmIQk2 0PNq0dy2dMje1KR0G1ZnWapzieOzhBAlZ6G4ZjIZI0Xjg4sUy7pZ25Uys58wzm4M+Rhx T4DxxjSM7OAm/Ln5fwbscWfIt2T+QqpoZt/USk2rBK/MmyYSIBNldMxSgRdnFelXTY9j QlSB7YZJpeIMsLbSSwsu4kUhUauRtQMOVM8iXOs5Kd54nyFloxxNRuMSol5doyR/txrA Y8sQ1xnC3pbgunK7/1sAfIP5tcsdaCVc80s7WcTIu9z9ncNk+lYNFVGSzPhaq74wmD6i at8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=fI8pxJuBvcTSJcYb+gcMX6vyXH4iVi6Ryn52o35lufE=; b=KgEE8KYNJS9PT8kphODMX6v3/a13slyD1nqqqR+7YgljM8tAiZOXJD/TIBh1a9mFUR 5XO7nyTftqxsL30Gdgoam84RbRvZJXj2rQjksCVmBhlpPQDMrkG1LkCuixS/8L1WR3qF 6lBGHl30gnMfGcZHyUXOkPMU6iEGXvMMyyJRx52Dc00pKu+wo5J9baSNCDfkuetd/mrG WkxcUazA+fPTjFhwpErHyzlBUNuWpZerBhdFxNlav3DDt0pleU5Ypcpoa/8kXIv0ErhV skSNimvcQuaPUrIJp23J4fkNz15u0aC6DeBMgre9IPltop1Sb/OxPDgj4t5rwWHMjO7/ QkPg== X-Gm-Message-State: AOAM530+BIMxud5YWt46AjGAlWobwGgwkAwNjA0Mxsm27piGwprjsYQ4 Drcp9w6B8mW10P3vv9UL/TT4ZkOA1QzFGPap8ty1Qe6AhMXiSI3H6csTtC0Qx5iVwEPvpJnZ0Ls 5NBfNVP+sJksg0SpY0sTO3wU5v6dFiIpKa0/y2xnJa5/LES7Z9YVwwL2iWjurTxk= X-Google-Smtp-Source: ABdhPJxJNCDeRnKQMFXsvk9dp76wEZ4IjRH915e701jQYOq0+1GHkgVn+vrJZP0X/DcovQh4Mu65xz135fPPWw== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a25:2681:: with SMTP id m123mr5185657ybm.121.1622827602319; Fri, 04 Jun 2021 10:26:42 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:08 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-10-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 09/12] KVM: selftests: Hoist APIC functions out of individual tests From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move the APIC functions into the library to encourage code reuse and to avoid unintended deviations. Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- tools/testing/selftests/kvm/Makefile | 2 +- .../selftests/kvm/include/x86_64/apic.h | 23 ++++++++ .../selftests/kvm/include/x86_64/processor.h | 2 + tools/testing/selftests/kvm/lib/x86_64/apic.c | 46 +++++++++++++++ .../testing/selftests/kvm/x86_64/evmcs_test.c | 11 +--- .../selftests/kvm/x86_64/set_boot_cpu_id.c | 6 +- .../selftests/kvm/x86_64/xapic_ipi_test.c | 59 +++---------------- 7 files changed, 83 insertions(+), 66 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/x86_64/apic.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index daaee1888b12..5e9051dd4336 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -34,7 +34,7 @@ ifeq ($(ARCH),s390) endif LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/rbtree.c lib/sparsebit.c lib/test_util.c lib/guest_modes.c lib/perf_test_util.c -LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S +LIBKVM_x86_64 = lib/x86_64/apic.c lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/svm.c lib/x86_64/ucall.c lib/x86_64/handlers.S LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c lib/s390x/diag318_test_handler.c diff --git a/tools/testing/selftests/kvm/include/x86_64/apic.h b/tools/testing/selftests/kvm/include/x86_64/apic.h index 0d0e35c8866b..e5a9fe040a6c 100644 --- a/tools/testing/selftests/kvm/include/x86_64/apic.h +++ b/tools/testing/selftests/kvm/include/x86_64/apic.h @@ -8,6 +8,10 @@ #ifndef SELFTEST_KVM_APIC_H #define SELFTEST_KVM_APIC_H +#include + +#include "processor.h" + #define APIC_DEFAULT_GPA 0xfee00000ULL /* APIC base address MSR and fields */ @@ -55,4 +59,23 @@ #define APIC_ICR2 0x310 #define SET_APIC_DEST_FIELD(x) ((x) << 24) +void apic_disable(void); +void xapic_enable(void); +void x2apic_enable(void); + +static inline uint32_t get_bsp_flag(void) +{ + return rdmsr(MSR_IA32_APICBASE) & MSR_IA32_APICBASE_BSP; +} + +static inline uint32_t xapic_read_reg(unsigned int reg) +{ + return ((volatile uint32_t *)APIC_DEFAULT_GPA)[reg >> 2]; +} + +static inline void xapic_write_reg(unsigned int reg, uint32_t val) +{ + ((volatile uint32_t *)APIC_DEFAULT_GPA)[reg >> 2] = val; +} + #endif /* SELFTEST_KVM_APIC_H */ diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index a4729d9032ce..9a5b47d2d5d6 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -13,6 +13,8 @@ #include +#include "../kvm_util.h" + #define X86_EFLAGS_FIXED (1u << 1) #define X86_CR4_VME (1ul << 0) diff --git a/tools/testing/selftests/kvm/lib/x86_64/apic.c b/tools/testing/selftests/kvm/lib/x86_64/apic.c new file mode 100644 index 000000000000..31f318ac67ba --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/apic.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * tools/testing/selftests/kvm/lib/x86_64/processor.c + * + * Copyright (C) 2021, Google LLC. + */ + +#include "apic.h" + +void apic_disable(void) +{ + wrmsr(MSR_IA32_APICBASE, + rdmsr(MSR_IA32_APICBASE) & + ~(MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD)); +} + +void xapic_enable(void) +{ + uint64_t val = rdmsr(MSR_IA32_APICBASE); + + /* Per SDM: to enable xAPIC when in x2APIC must first disable APIC */ + if (val & MSR_IA32_APICBASE_EXTD) { + apic_disable(); + wrmsr(MSR_IA32_APICBASE, + rdmsr(MSR_IA32_APICBASE) | MSR_IA32_APICBASE_ENABLE); + } else if (!(val & MSR_IA32_APICBASE_ENABLE)) { + wrmsr(MSR_IA32_APICBASE, val | MSR_IA32_APICBASE_ENABLE); + } + + /* + * Per SDM: reset value of spurious interrupt vector register has the + * APIC software enabled bit=0. It must be enabled in addition to the + * enable bit in the MSR. + */ + val = xapic_read_reg(APIC_SPIV) | APIC_SPIV_APIC_ENABLED; + xapic_write_reg(APIC_SPIV, val); +} + +void x2apic_enable(void) +{ + uint32_t spiv_reg = APIC_BASE_MSR + (APIC_SPIV >> 4); + + wrmsr(MSR_IA32_APICBASE, rdmsr(MSR_IA32_APICBASE) | + MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD); + wrmsr(spiv_reg, rdmsr(spiv_reg) | APIC_SPIV_APIC_ENABLED); +} diff --git a/tools/testing/selftests/kvm/x86_64/evmcs_test.c b/tools/testing/selftests/kvm/x86_64/evmcs_test.c index 63096cea26c6..d058d9e428c6 100644 --- a/tools/testing/selftests/kvm/x86_64/evmcs_test.c +++ b/tools/testing/selftests/kvm/x86_64/evmcs_test.c @@ -22,15 +22,6 @@ static int ud_count; -void enable_x2apic(void) -{ - uint32_t spiv_reg = APIC_BASE_MSR + (APIC_SPIV >> 4); - - wrmsr(MSR_IA32_APICBASE, rdmsr(MSR_IA32_APICBASE) | - MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD); - wrmsr(spiv_reg, rdmsr(spiv_reg) | APIC_SPIV_APIC_ENABLED); -} - static void guest_ud_handler(struct ex_regs *regs) { ud_count++; @@ -59,7 +50,7 @@ void guest_code(struct vmx_pages *vmx_pages) #define L2_GUEST_STACK_SIZE 64 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; - enable_x2apic(); + x2apic_enable(); GUEST_SYNC(1); GUEST_SYNC(2); diff --git a/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c b/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c index 12c558fc8074..5f8dd74d415f 100644 --- a/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +++ b/tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c @@ -14,16 +14,12 @@ #include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "apic.h" #define N_VCPU 2 #define VCPU_ID0 0 #define VCPU_ID1 1 -static uint32_t get_bsp_flag(void) -{ - return rdmsr(MSR_IA32_APICBASE) & MSR_IA32_APICBASE_BSP; -} - static void guest_bsp_vcpu(void *arg) { GUEST_SYNC(1); diff --git a/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c b/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c index 2f964cdc273c..21b22718a9db 100644 --- a/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c +++ b/tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c @@ -42,8 +42,6 @@ #define HALTER_VCPU_ID 0 #define SENDER_VCPU_ID 1 -volatile uint32_t *apic_base = (volatile uint32_t *)APIC_DEFAULT_GPA; - /* * Vector for IPI from sender vCPU to halting vCPU. * Value is arbitrary and was chosen for the alternating bit pattern. Any @@ -86,45 +84,6 @@ struct thread_params { uint64_t *pipis_rcvd; /* host address of ipis_rcvd global */ }; -uint32_t read_apic_reg(uint reg) -{ - return apic_base[reg >> 2]; -} - -void write_apic_reg(uint reg, uint32_t val) -{ - apic_base[reg >> 2] = val; -} - -void disable_apic(void) -{ - wrmsr(MSR_IA32_APICBASE, - rdmsr(MSR_IA32_APICBASE) & - ~(MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD)); -} - -void enable_xapic(void) -{ - uint64_t val = rdmsr(MSR_IA32_APICBASE); - - /* Per SDM: to enable xAPIC when in x2APIC must first disable APIC */ - if (val & MSR_IA32_APICBASE_EXTD) { - disable_apic(); - wrmsr(MSR_IA32_APICBASE, - rdmsr(MSR_IA32_APICBASE) | MSR_IA32_APICBASE_ENABLE); - } else if (!(val & MSR_IA32_APICBASE_ENABLE)) { - wrmsr(MSR_IA32_APICBASE, val | MSR_IA32_APICBASE_ENABLE); - } - - /* - * Per SDM: reset value of spurious interrupt vector register has the - * APIC software enabled bit=0. It must be enabled in addition to the - * enable bit in the MSR. - */ - val = read_apic_reg(APIC_SPIV) | APIC_SPIV_APIC_ENABLED; - write_apic_reg(APIC_SPIV, val); -} - void verify_apic_base_addr(void) { uint64_t msr = rdmsr(MSR_IA32_APICBASE); @@ -136,10 +95,10 @@ void verify_apic_base_addr(void) static void halter_guest_code(struct test_data_page *data) { verify_apic_base_addr(); - enable_xapic(); + xapic_enable(); - data->halter_apic_id = GET_APIC_ID_FIELD(read_apic_reg(APIC_ID)); - data->halter_lvr = read_apic_reg(APIC_LVR); + data->halter_apic_id = GET_APIC_ID_FIELD(xapic_read_reg(APIC_ID)); + data->halter_lvr = xapic_read_reg(APIC_LVR); /* * Loop forever HLTing and recording halts & wakes. Disable interrupts @@ -150,8 +109,8 @@ static void halter_guest_code(struct test_data_page *data) * TPR and PPR for diagnostic purposes in case the test fails. */ for (;;) { - data->halter_tpr = read_apic_reg(APIC_TASKPRI); - data->halter_ppr = read_apic_reg(APIC_PROCPRI); + data->halter_tpr = xapic_read_reg(APIC_TASKPRI); + data->halter_ppr = xapic_read_reg(APIC_PROCPRI); data->hlt_count++; asm volatile("sti; hlt; cli"); data->wake_count++; @@ -166,7 +125,7 @@ static void halter_guest_code(struct test_data_page *data) static void guest_ipi_handler(struct ex_regs *regs) { ipis_rcvd++; - write_apic_reg(APIC_EOI, 77); + xapic_write_reg(APIC_EOI, 77); } static void sender_guest_code(struct test_data_page *data) @@ -179,7 +138,7 @@ static void sender_guest_code(struct test_data_page *data) uint64_t tsc_start; verify_apic_base_addr(); - enable_xapic(); + xapic_enable(); /* * Init interrupt command register for sending IPIs @@ -206,8 +165,8 @@ static void sender_guest_code(struct test_data_page *data) * First IPI can be sent unconditionally because halter vCPU * starts earlier. */ - write_apic_reg(APIC_ICR2, icr2_val); - write_apic_reg(APIC_ICR, icr_val); + xapic_write_reg(APIC_ICR2, icr2_val); + xapic_write_reg(APIC_ICR, icr_val); data->ipis_sent++; /* From patchwork Fri Jun 4 17:26:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300343 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 C8CA7C4743D for ; Fri, 4 Jun 2021 17:26:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A4775613AD for ; Fri, 4 Jun 2021 17:26:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230396AbhFDR2b (ORCPT ); Fri, 4 Jun 2021 13:28:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230355AbhFDR2b (ORCPT ); Fri, 4 Jun 2021 13:28:31 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF971C061768 for ; Fri, 4 Jun 2021 10:26:44 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id k193-20020a633dca0000b029021ff326b222so6430347pga.9 for ; Fri, 04 Jun 2021 10:26:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=qle/BNdTsfUk9UPuCz2u36p9QAxi+ijtW0jx0slFuCQ=; b=COw+PsrE7byOvgE6GdmSD2nRs48lDa83gAU6lL+vzZYbPC9KPdGSi5MeBxshRl6EVc M5UPMuIGgY4lUmRChuFCGbqMQ78INWEF5YGa7r9DrB6jEHWHnj1qLiBC/oSpE3Omk1yS 2qWV8sJd7+yV5yHuizTsvuimJETNNmQrf6iAxq4/9Yt19W8H2HZ/dW74lhqjEStgy8UA rR7w+y6POa5OXE6yFsVK+6pZdfOCViff+mTzbLexdbezvq4TMPjkoqcX5mso2RXdbTOG MQeLEBz7CpAflYNJ+ZaOHKvlb4bHVwNlehwX/nIX/1V0p2pwrbDvia3fyeXdBBI20rib Yftg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=qle/BNdTsfUk9UPuCz2u36p9QAxi+ijtW0jx0slFuCQ=; b=P3wjbxMjnRj+bQ4u/0R07lXex9oZG6iFPuFSKazuBo0vQNy13z8A/EWZ4ASko2mC1s +eXRQn79Whx64PHTFLZwT8G8Y7VM1NV9gZFXjpjclO46ew3eks6Kn5u7JXOaSvDyT0dv D5zuHV8wFUhYWu6t3yO/yr8IcG5KDhXPDjtO3E7zPX6PXk/PI45L+cAu490K51UKF1mB JjY1WzNaXKdTCdNhNlRlTFOcyA+YvpfDeTRjq5G1EHyUcfv77nte/Mltr1bH31N3HLQk +all6bIuDN+EBwLkCyNI/hNBxc35Ifm9pyxPgvRqEwqjMXORHvMiPC7CrmftSi4E5QH5 IBmg== X-Gm-Message-State: AOAM532TX0OueCPxN9e8ydpAlXiHKuEqy0xOQ8+yGl88Oy/gSNGvfkZB 81x37rJzzNE0KA+RFSVF/CqXzVylR7yYXYrNB1dg3BJ7dxey0MtfIpxp0zIOXIMCaqb/uF/6Sqt fyOZx/c66zCBqh7wMf3OUcsn0D4qd8RONS/Tmc1on+sFosPP+83Ewt+zZ+hdIk54= X-Google-Smtp-Source: ABdhPJyG0OcP8QKXb9UQ9ScShWjdr3sbPvg7jBnl/z0aHJUwM6tcUx3OahwhPL0L2n4koEDSA2nJ6p+ICn1UdA== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a17:902:8695:b029:fd:6105:c936 with SMTP id g21-20020a1709028695b02900fd6105c936mr5397405plo.25.1622827604058; Fri, 04 Jun 2021 10:26:44 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:09 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-11-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 10/12] KVM: selftests: Introduce x2APIC register manipulation functions From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Standardize reads and writes of the x2APIC MSRs. Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- tools/testing/selftests/kvm/include/x86_64/apic.h | 10 ++++++++++ tools/testing/selftests/kvm/lib/x86_64/apic.c | 5 ++--- tools/testing/selftests/kvm/x86_64/smm_test.c | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/apic.h b/tools/testing/selftests/kvm/include/x86_64/apic.h index e5a9fe040a6c..0be4757f1f20 100644 --- a/tools/testing/selftests/kvm/include/x86_64/apic.h +++ b/tools/testing/selftests/kvm/include/x86_64/apic.h @@ -78,4 +78,14 @@ static inline void xapic_write_reg(unsigned int reg, uint32_t val) ((volatile uint32_t *)APIC_DEFAULT_GPA)[reg >> 2] = val; } +static inline uint64_t x2apic_read_reg(unsigned int reg) +{ + return rdmsr(APIC_BASE_MSR + (reg >> 4)); +} + +static inline void x2apic_write_reg(unsigned int reg, uint64_t value) +{ + wrmsr(APIC_BASE_MSR + (reg >> 4), value); +} + #endif /* SELFTEST_KVM_APIC_H */ diff --git a/tools/testing/selftests/kvm/lib/x86_64/apic.c b/tools/testing/selftests/kvm/lib/x86_64/apic.c index 31f318ac67ba..7168e25c194e 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/apic.c +++ b/tools/testing/selftests/kvm/lib/x86_64/apic.c @@ -38,9 +38,8 @@ void xapic_enable(void) void x2apic_enable(void) { - uint32_t spiv_reg = APIC_BASE_MSR + (APIC_SPIV >> 4); - wrmsr(MSR_IA32_APICBASE, rdmsr(MSR_IA32_APICBASE) | MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD); - wrmsr(spiv_reg, rdmsr(spiv_reg) | APIC_SPIV_APIC_ENABLED); + x2apic_write_reg(APIC_SPIV, + x2apic_read_reg(APIC_SPIV) | APIC_SPIV_APIC_ENABLED); } diff --git a/tools/testing/selftests/kvm/x86_64/smm_test.c b/tools/testing/selftests/kvm/x86_64/smm_test.c index 613c42c5a9b8..c1f831803ad2 100644 --- a/tools/testing/selftests/kvm/x86_64/smm_test.c +++ b/tools/testing/selftests/kvm/x86_64/smm_test.c @@ -55,8 +55,8 @@ static inline void sync_with_host(uint64_t phase) void self_smi(void) { - wrmsr(APIC_BASE_MSR + (APIC_ICR >> 4), - APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI); + x2apic_write_reg(APIC_ICR, + APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI); } void guest_code(void *arg) From patchwork Fri Jun 4 17:26:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300371 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 79F9FC4743C for ; Fri, 4 Jun 2021 17:28:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 529FD61402 for ; Fri, 4 Jun 2021 17:28:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230291AbhFDR3s (ORCPT ); Fri, 4 Jun 2021 13:29:48 -0400 Received: from mail-yb1-f201.google.com ([209.85.219.201]:39756 "EHLO mail-yb1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229926AbhFDR3r (ORCPT ); Fri, 4 Jun 2021 13:29:47 -0400 Received: by mail-yb1-f201.google.com with SMTP id r5-20020a2582850000b02905381b1b616eso12731843ybk.6 for ; Fri, 04 Jun 2021 10:27:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=IlVve+6Rq2S9fS+3CXZhYRmldp994V/4F8spl4ldNqk=; b=Bgdq248awNvgUajbXS3gn2iTmfXwB27JqRfXLKkobr1WqKbIaxNmmZBDhkIf0WGY5H k0Ku0KpLJPJIUniD5uV/XLCiqUMzORZzesL7Ne42rL+ZjPy/i39G38nw8bpIQE8utXYu GiQiofZ/ywGp6KG8YskZIdwGmBD8b9cKxljEvQWb6EjKn+o8yOWXbFiWbjKHFBmglrXY Dy1Oa0uOG4uMz9EOcghNzfL3KjSksgkaDWDEFFyHVQBImj+E6NKowhFMdrEs/YqQhUQU Zz4ZVSFQCuCKH0Jz5dz1bgN4apVvklP6goCqlFlNBBv9KFfjunQoOpKZp1cvAczvUSWP DFlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IlVve+6Rq2S9fS+3CXZhYRmldp994V/4F8spl4ldNqk=; b=KNPeJYa/Okm8HPF/lbgqPpADtVK/Xw0w4eyufvY3auXCS8IBfygtdRtevv3dYLfkH/ LKcy/PjWpjqislDcWoXC0c46FNsMILdd5xJqOAel317zj+0uXRRLnZebZukSfwRlg8y0 LnZQUOawVinWXncF9sbQ3VjAwZiBUXODJ5cfbDKArmvIm6sNqy07FitUINbik+FReehS 1S9tglLsyQZQ/BAuT8uDfXWd38u3TEUESgifRJHIUoufOUM691E9WLk7wEz8yjr21uFp viGbTYyG1vdOj6PrAzEhABmjyHsfikSvUkqVrsPvzLzLgu9x2FHLJZ+wmBRb3AXJKZs6 msyA== X-Gm-Message-State: AOAM533xroMAOuxqakI22FcxL++ejY0w71RdrC7zyJCkb9sA59WKPHuh Xab8C8mjz+yws3uiT3Pnx2+funqJ3PqqwkcLoA+mz+lRwoHwRbE/6PfQC418O6wzT4DVYGHyI3p h0uxXSW7SMxcMeXaBGksxeCZaiDIWw8zdpZbgJ2WLG8cv2DZnSAlr/lLqsI0jrGo= X-Google-Smtp-Source: ABdhPJztOrcK32XGrANtG1zz5Iu25D8q2P3Aelk5X5/sx/nmW3jGVoxhj+KOtOE+gXsmJKZj0/Wo0EZjgF5/Wg== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:a25:ccd2:: with SMTP id l201mr6829888ybf.35.1622827605724; Fri, 04 Jun 2021 10:26:45 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:10 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-12-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 11/12] KVM: selftests: Introduce prepare_tpr_shadow From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add support for yet another page to hang from the VMCS12 for nested VMX testing: the virtual APIC page. This page is necessary for a VMCS12 to be launched with the "use TPR shadow" VM-execution control set (except in some oddball circumstances permitted by KVM). Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- tools/testing/selftests/kvm/include/x86_64/vmx.h | 5 +++++ tools/testing/selftests/kvm/lib/x86_64/vmx.c | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h index 516c81d86353..83ccb096b966 100644 --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h @@ -574,6 +574,10 @@ struct vmx_pages { void *apic_access_hva; uint64_t apic_access_gpa; void *apic_access; + + void *virtual_apic_hva; + uint64_t virtual_apic_gpa; + void *virtual_apic; }; union vmx_basic { @@ -618,5 +622,6 @@ void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm, uint32_t eptp_memslot); void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm, uint32_t eptp_memslot); +void prepare_tpr_shadow(struct vmx_pages *vmx, struct kvm_vm *vm); #endif /* SELFTEST_KVM_VMX_H */ diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c index 2448b30e8efa..1023760d1bf7 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c @@ -551,3 +551,11 @@ void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm, vmx->apic_access_hva = addr_gva2hva(vm, (uintptr_t)vmx->apic_access); vmx->apic_access_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->apic_access); } + +void prepare_tpr_shadow(struct vmx_pages *vmx, struct kvm_vm *vm) +{ + vmx->virtual_apic = (void *)vm_vaddr_alloc(vm, getpagesize(), + 0x10000, 0, 0); + vmx->virtual_apic_hva = addr_gva2hva(vm, (uintptr_t)vmx->virtual_apic); + vmx->virtual_apic_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->virtual_apic); +} From patchwork Fri Jun 4 17:26:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 12300373 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 CA9D4C4743D for ; Fri, 4 Jun 2021 17:28:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A9EB661417 for ; Fri, 4 Jun 2021 17:28:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230319AbhFDR3s (ORCPT ); Fri, 4 Jun 2021 13:29:48 -0400 Received: from mail-qk1-f202.google.com ([209.85.222.202]:55290 "EHLO mail-qk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230123AbhFDR3s (ORCPT ); Fri, 4 Jun 2021 13:29:48 -0400 Received: by mail-qk1-f202.google.com with SMTP id s20-20020ae9f7140000b02903a98afb6313so7014849qkg.21 for ; Fri, 04 Jun 2021 10:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ExzF5GAgx6kdyb5TlE2fN5Do28wdaAIZFuxM/uKeTy8=; b=oTtsq5IW2ni/24kBjQAIJGHrAOYBthQZ5M9bPjTxe3mBed72/vN3heBsHjWSuKT4n0 ksjnlsc2p0aq5iWnHa8vEmURr3HitG5rGUIrsr2wuP9rqpPp6cLH73aLL74cxAZA22Cj s4WET+MuHn48Rm2S0c/YDwWtwDiD2D9su5FWkYcXESHslRk1epwEZmZMHBfqzbehQ3F0 uE7LdFuuBdCHWDBV7Sy0UvavNsbajzkqZGU/nlcrRNRgOt8TBoekaHgcblNliArMi76O k2XgMuONVmn+IMTHnWLajVL3D84KNrnuAjL4TzpPLEg8RujMIjfIcjPTabq+EP3PU20B aOcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ExzF5GAgx6kdyb5TlE2fN5Do28wdaAIZFuxM/uKeTy8=; b=JDwGyAcojUC0x/HwNWfTEUtsLedPSAByby5yY5N/F2sRYJcq0JBwju4drO3MqLJ5Mb wU6k730GrY9UAHHJbxEmPlbNBpbcilFCXlSSFWnQm+dvsuKtkgUbLG5fckAVfvICjHTh AsGMILdXKy+1AraUtjDe8TE6e7922y4QNx2QIWmGYwyFmpbcwTf7P1TZabgY+dn3kAbE 05UBk+UhSrjnMCFU4K91UdlkU9AlHFCFwcoZk71xg2qpBVJMKTwgCUZTPHhtJUBD+Oks m09DxhSz57hwUUz/aP4UYMmjB8vav+DdYQxNNXEikJM1iRGo7zNKmEECCuSaM5fCLNpJ cbMg== X-Gm-Message-State: AOAM530hZaPK06nuZOdt+u+mIZfperxKT8Qk4kyOPoHlckKQRWZtp5e6 DhEqPJvhz/gJ0RDzq+KutcRStEb1DKbX7pS72l9BM0rWVJRNhCrkuY+oYA01NrblnNF7JtmowMC M6fawQuJmhpO1UzvNTXkw/JtQZREi6Vsy+2x+uLUyn5jANLbjOxNkER5BFrw9uGc= X-Google-Smtp-Source: ABdhPJyxmmRPoM6BqCirHb+vSxZ5QfRXd0h0ATukglcW0OtqTgvYw34p6dtqQS0z4cMRZJahraXreP72CYzYQw== X-Received: from tortoise.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:1a0d]) (user=jmattson job=sendgmr) by 2002:ad4:5569:: with SMTP id w9mr5546688qvy.59.1622827607531; Fri, 04 Jun 2021 10:26:47 -0700 (PDT) Date: Fri, 4 Jun 2021 10:26:11 -0700 In-Reply-To: <20210604172611.281819-1-jmattson@google.com> Message-Id: <20210604172611.281819-13-jmattson@google.com> Mime-Version: 1.0 References: <20210604172611.281819-1-jmattson@google.com> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog Subject: [PATCH v2 12/12] KVM: selftests: Add a test of an unbacked nested PI descriptor From: Jim Mattson To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: Jim Mattson , Oliver Upton Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a regression test for the unsupported configuration of a VMCS12 posted interrupt descriptor that has no backing memory in L1. KVM should exit to userspace with KVM_INTERNAL_ERROR rather than just silently doing something wrong. Signed-off-by: Jim Mattson Reviewed-by: Oliver Upton --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/vmx_pi_mmio_test.c | 252 ++++++++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/vmx_pi_mmio_test.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 524c857a049c..43fe1bb037ac 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -34,6 +34,7 @@ /x86_64/xen_vmcall_test /x86_64/xss_msr_test /x86_64/vmx_pmu_msrs_test +/x86_64/vmx_pi_mmio_test /demand_paging_test /dirty_log_test /dirty_log_perf_test diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 5e9051dd4336..d7b7d6e641f0 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -67,6 +67,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test +TEST_GEN_PROGS_x86_64 += x86_64/vmx_pi_mmio_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_perf_test diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pi_mmio_test.c b/tools/testing/selftests/kvm/x86_64/vmx_pi_mmio_test.c new file mode 100644 index 000000000000..2246899d8988 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/vmx_pi_mmio_test.c @@ -0,0 +1,252 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * vmx_pi_mmio_test + * + * Copyright (C) 2021, Google LLC. + * + * This work is licensed under the terms of the GNU GPL, version 2. + * + * Test that an L2 vCPU can be launched with an unbacked posted + * interrupt descriptor, but that any attempt to send that vCPU its + * posted interrupt notification vector will result in an exit to + * userspace with KVM_INTERNAL_ERROR. + * + */ + +#define _GNU_SOURCE /* for program_invocation_short_name */ +#include +#include +#include + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" +#include "vmx.h" + +#include "kselftest.h" + +#define RECEIVER_VCPU_ID 0 +#define SENDER_VCPU_ID 1 + +#define L2_GUEST_STACK_SIZE 64 + +#define TIMEOUT_SECS 10 + +#define L1_PI_VECTOR 33 + +static struct kvm_vm *vm; + +static bool l2_active; + +static void l2_guest_code(void) +{ + l2_active = true; + __asm__ __volatile__("hlt"); + /* NOT REACHED */ +} + +static void l1_receiver_code(struct vmx_pages *vmx_pages, + unsigned long high_gpa) +{ + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; + uint32_t control; + + x2apic_enable(); + + GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages)); + GUEST_ASSERT(load_vmcs(vmx_pages)); + + prepare_vmcs(vmx_pages, l2_guest_code, + &l2_guest_stack[L2_GUEST_STACK_SIZE]); + control = vmreadz(PIN_BASED_VM_EXEC_CONTROL); + control |= PIN_BASED_EXT_INTR_MASK | + PIN_BASED_POSTED_INTR; + vmwrite(PIN_BASED_VM_EXEC_CONTROL, control); + + control = vmreadz(CPU_BASED_VM_EXEC_CONTROL); + control |= CPU_BASED_TPR_SHADOW | + CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; + vmwrite(CPU_BASED_VM_EXEC_CONTROL, control); + + control = vmreadz(SECONDARY_VM_EXEC_CONTROL); + control |= SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; + vmwrite(SECONDARY_VM_EXEC_CONTROL, control); + + control = vmreadz(VM_EXIT_CONTROLS); + control |= VM_EXIT_ACK_INTR_ON_EXIT; + vmwrite(VM_EXIT_CONTROLS, control); + + vmwrite(VIRTUAL_APIC_PAGE_ADDR, vmx_pages->virtual_apic_gpa); + vmwrite(POSTED_INTR_NV, L1_PI_VECTOR); + vmwrite(POSTED_INTR_DESC_ADDR, high_gpa); + + GUEST_ASSERT(!vmlaunch()); + GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL); + + GUEST_DONE(); +} + +static void l1_sender_code(void *arg) +{ + x2apic_enable(); + + x2apic_write_reg(APIC_ICR, + APIC_INT_ASSERT | APIC_DEST_PHYSICAL | + APIC_DM_FIXED | L1_PI_VECTOR | + ((uint64_t)RECEIVER_VCPU_ID << 32)); + + GUEST_DONE(); +} + +static bool vcpu_run_loop(int vcpu_id) +{ + volatile struct kvm_run *run = vcpu_state(vm, vcpu_id); + bool done = false; + struct ucall uc; + + while (!done) { + vcpu_run(vm, vcpu_id); + + if (run->exit_reason != KVM_EXIT_IO) + break; + + switch (get_ucall(vm, vcpu_id, &uc)) { + case UCALL_ABORT: + TEST_FAIL("vCPU %d: %s at %s:%ld", vcpu_id, + (const char *)uc.args[0], __FILE__, + uc.args[1]); + /* NOT REACHED */ + case UCALL_SYNC: + break; + case UCALL_DONE: + done = true; + break; + default: + TEST_FAIL("vCPU %d: Unknown ucall %lu", + vcpu_id, uc.cmd); + /* NOT REACHED */ + } + } + + return done; +} + +static void *receiver(void *arg) +{ + volatile struct kvm_run *run = vcpu_state(vm, RECEIVER_VCPU_ID); + unsigned long high_gpa = *(unsigned long *)arg; + vm_vaddr_t vmx_pages_gva; + struct vmx_pages *vmx; + bool success; + + vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva); + prepare_tpr_shadow(vmx, vm); + vcpu_args_set(vm, RECEIVER_VCPU_ID, 2, vmx_pages_gva, high_gpa); + + success = vcpu_run_loop(RECEIVER_VCPU_ID); + TEST_ASSERT(!success, "Receiver didn't fail as expected.\n"); + TEST_ASSERT(run->exit_reason == + KVM_EXIT_INTERNAL_ERROR, + "Exit reason isn't KVM_EXIT_INTERNAL_ERROR: %u (%s).\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + TEST_ASSERT(run->internal.suberror == + KVM_INTERNAL_ERROR_EMULATION, + "Internal suberror isn't KVM_INTERNAL_ERROR_EMULATION: %u.\n", + run->internal.suberror); + + return NULL; +} + +static void sender(void) +{ + volatile struct kvm_run *run = vcpu_state(vm, SENDER_VCPU_ID); + bool success; + + success = vcpu_run_loop(SENDER_VCPU_ID); + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "Sender didn't exit with KVM_EXIT_IO: %u (%s).\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + TEST_ASSERT(success, "Sender didn't complete successfully.\n"); +} + +void check_constraints(void) +{ + uint64_t msr; + + nested_vmx_check_supported(); + + msr = kvm_get_feature_msr(MSR_IA32_VMX_PINBASED_CTLS) >> 32; + if (!(msr & PIN_BASED_EXT_INTR_MASK)) { + print_skip("Cannot enable \"external-interrupt exiting\""); + exit(KSFT_SKIP); + } + if (!(msr & PIN_BASED_POSTED_INTR)) { + print_skip("Cannot enable \"process posted interrupts\""); + exit(KSFT_SKIP); + } + + msr = kvm_get_feature_msr(MSR_IA32_VMX_PROCBASED_CTLS) >> 32; + if (!(msr & CPU_BASED_TPR_SHADOW)) { + print_skip("Cannot enable \"use TPR shadow\""); + exit(KSFT_SKIP); + } + if (!(msr & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { + print_skip("Cannot enable \"activate secondary controls\""); + exit(KSFT_SKIP); + } + + msr = kvm_get_feature_msr(MSR_IA32_VMX_PROCBASED_CTLS2) >> 32; + if (!(msr & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) { + print_skip("Cannot enable \"virtual-interrupt delivery\""); + exit(KSFT_SKIP); + } + + msr = kvm_get_feature_msr(MSR_IA32_VMX_EXIT_CTLS) >> 32; + if (!(msr & VM_EXIT_ACK_INTR_ON_EXIT)) { + print_skip("Cannot enable \"acknowledge interrupt on exit\""); + exit(KSFT_SKIP); + } +} + +int main(int argc, char *argv[]) +{ + unsigned int paddr_width; + unsigned int vaddr_width; + unsigned long high_gpa; + pthread_t thread; + bool *l2_active_hva; + int r; + + kvm_get_cpu_address_width(&paddr_width, &vaddr_width); + high_gpa = (1ul << paddr_width) - getpagesize(); + if ((unsigned long)DEFAULT_GUEST_PHY_PAGES * getpagesize() > high_gpa) { + print_skip("No unbacked physical page available"); + exit(KSFT_SKIP); + } + + check_constraints(); + + vm = vm_create_default(RECEIVER_VCPU_ID, 0, (void *)l1_receiver_code); + vm_vcpu_add_default(vm, SENDER_VCPU_ID, (void *)l1_sender_code); + vcpu_set_cpuid(vm, SENDER_VCPU_ID, kvm_get_supported_cpuid()); + + r = pthread_create(&thread, NULL, receiver, &high_gpa); + TEST_ASSERT(r == 0, + "pthread_create failed errno=%d", errno); + + alarm(TIMEOUT_SECS); + l2_active_hva = (bool *)addr_gva2hva(vm, (vm_vaddr_t)&l2_active); + while (!*l2_active_hva) + pthread_yield(); + + sender(); + + r = pthread_join(thread, NULL); + TEST_ASSERT(r == 0, "pthread_join failed with errno=%d", r); + + kvm_vm_free(vm); + + return 0; +}