Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1113784imi; Fri, 22 Jul 2022 17:53:50 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tqN1e9uEKEWUCu/lCwKeLThM4MakoGqB8CgaG1wz+BQddsqbg+P2AD8lx5zul2yAwTteDN X-Received: by 2002:a17:906:cc15:b0:72b:5e0d:9a2a with SMTP id ml21-20020a170906cc1500b0072b5e0d9a2amr1784038ejb.614.1658537630706; Fri, 22 Jul 2022 17:53:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658537630; cv=none; d=google.com; s=arc-20160816; b=kdzOORopZJXHNJf/4yP9UZLbaiOfWkS7rBFQJsK2Msd4iVVBFedKAaCJDDimlPXSRP ab/Kir/tECcssQolH81CJzn1zbNbdkJ1edSNYAMsWDi6sF6qeVsCKGwfNPdOKm9Xiz7i 2YpywW03LJod9SvQfdlQyCrrJVSAvh3AlVhy4HnqmpCZSYhoQZFtTFb5vhdCZZ1B+/VK BbDGaG28hxxqOJW/oInmiRx+P1fwHum6UStXcn5McZOUgiCOx3/XquxqJBiJaQArQ8zd yxNgHq8IzdvE74x96A7/mvWTRRPleLEtnd4QOdy2EDZAQ+HqNKini2PChMmm/aOEbyjx 7leQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=v9YL/H3DzLIILzqro/Rlg3LJnOCX+QvAwinAtI7kHXg=; b=af0h3MKwduVBjMtgoKLLcY43xrNCpip9N7X599v9u9ofbMWA7FYYAYn9Rj70Np28W1 DvjES4yzdwaVIDgykyY5hTgmko5YsQp+h+oClKFaBbvu7qh20NWSej4sPy2ROrfil6mw u0SIXt/6NPeQuP93pKx1IbCYlkLiqk6cK+huoPmLkUiOfsYHmcshT8L5rl9I//3Gzghv Bq1RmMffrTc9xrZl7uxKQSDe9krewqlWmrJovsq+unyOoivIRFY87oYBOD8ukRqeuJPE mFnqmYxPDvYfPB/+Y9Jac+ybW/XQkMUaFSY4ArBs2HcVgFr7cKpU5O0ReswXGMPhAJAZ LEeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gn5B7l1M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hs11-20020a1709073e8b00b0072f664f368asi7977585ejc.199.2022.07.22.17.53.25; Fri, 22 Jul 2022 17:53:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gn5B7l1M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236340AbiGWAvu (ORCPT + 99 others); Fri, 22 Jul 2022 20:51:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235822AbiGWAvr (ORCPT ); Fri, 22 Jul 2022 20:51:47 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20B2213E04 for ; Fri, 22 Jul 2022 17:51:46 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id bq15-20020a056a000e0f00b0052af07b6c6bso2491422pfb.21 for ; Fri, 22 Jul 2022 17:51:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=v9YL/H3DzLIILzqro/Rlg3LJnOCX+QvAwinAtI7kHXg=; b=gn5B7l1MV1xM263PvGhMVA5vgyKZt7Z4XHAuiFrUB2VZy6YH6ra5CcrARs/Y6iHcCF +4ZbVN0FULqDMLk1L7xjkrBBOmDMOSEVMq2Tf7EZIE0a2ArROgE64FGBHcIUjxv8URpL og2unMeMb9gJjgiufyZjcpQR1HlCebKsMysIMQUZC9k/71EMFGMeYDjBs8BHze6sVh5g TmNh521eGopMMmnCILeCw9ImkzA3WbKPWL1OuTtomlW8FRQELkpJCRzxh5OQtITnDExJ doVhpmUjxBLNUoOXV9TBsWLcMFCRx6wZkWUTUyZ6e+WTZ0njZPpsm6CQMx1hQ07o0bfn Mtiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=v9YL/H3DzLIILzqro/Rlg3LJnOCX+QvAwinAtI7kHXg=; b=zLwylRIxq+5ssksxWtC8p7M27TJQPjQgXBOxVSceegY3SGs0LVyfAG/e/Tb4NVJGr2 8guHkGSE401vF9C06Z+bJF/DQnlA5NF+dL4iiEdXo8HEfzj5ZTD72b0CBL1VX0lhX0wJ n1FnVpzcWDONqpUNoep5CuWPe5xrk6H3lKQ7f05OkzmlifAtQIMwe9zhXeDMla863AzX SBKlqpJRDBNQDUB5qH7dsl7874k0fM+NTKi/prLXwBh7eCRT3yJha6CV1ThADx4sitKD sebgoFKLjuTZclddkV0GoGVOFBun2mt8/6x2Mi3LfQ5RVpll+b+loZlHN60y6ZeXA0Do 1U5A== X-Gm-Message-State: AJIora/b8w30037INZDDzDGFjF/hSpIORsyNwETL+4ePzUAgt7Fpmy4c aLzOcOGdq3PvIbSj3y2dw+I+FytaDQs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2401:b0:52b:cd67:d997 with SMTP id z1-20020a056a00240100b0052bcd67d997mr2490318pfh.70.1658537505696; Fri, 22 Jul 2022 17:51:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Sat, 23 Jul 2022 00:51:15 +0000 In-Reply-To: <20220723005137.1649592-1-seanjc@google.com> Message-Id: <20220723005137.1649592-3-seanjc@google.com> Mime-Version: 1.0 References: <20220723005137.1649592-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v4 02/24] KVM: VMX: Drop bits 31:16 when shoving exception error code into VMCS From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Maxim Levitsky , Oliver Upton , Peter Shier Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Deliberately truncate the exception error code when shoving it into the VMCS (VM-Entry field for vmcs01 and vmcs02, VM-Exit field for vmcs12). Intel CPUs are incapable of handling 32-bit error codes and will never generate an error code with bits 31:16, but userspace can provide an arbitrary error code via KVM_SET_VCPU_EVENTS. Failure to drop the bits on exception injection results in failed VM-Entry, as VMX disallows setting bits 31:16. Setting the bits on VM-Exit would at best confuse L1, and at worse induce a nested VM-Entry failure, e.g. if L1 decided to reinject the exception back into L2. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Jim Mattson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/vmx/nested.c | 11 ++++++++++- arch/x86/kvm/vmx/vmx.c | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index a980d9cbee60..c6f9fe0b6b33 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3827,7 +3827,16 @@ static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, u32 intr_info = nr | INTR_INFO_VALID_MASK; if (vcpu->arch.exception.has_error_code) { - vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; + /* + * Intel CPUs do not generate error codes with bits 31:16 set, + * and more importantly VMX disallows setting bits 31:16 in the + * injected error code for VM-Entry. Drop the bits to mimic + * hardware and avoid inducing failure on nested VM-Entry if L1 + * chooses to inject the exception back to L2. AMD CPUs _do_ + * generate "full" 32-bit error codes, so KVM allows userspace + * to inject exception error codes with bits 31:16 set. + */ + vmcs12->vm_exit_intr_error_code = (u16)vcpu->arch.exception.error_code; intr_info |= INTR_INFO_DELIVER_CODE_MASK; } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4fd25e1d6ec9..1c72cde600d0 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1621,7 +1621,17 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu) kvm_deliver_exception_payload(vcpu); if (has_error_code) { - vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); + /* + * Despite the error code being architecturally defined as 32 + * bits, and the VMCS field being 32 bits, Intel CPUs and thus + * VMX don't actually supporting setting bits 31:16. Hardware + * will (should) never provide a bogus error code, but AMD CPUs + * do generate error codes with bits 31:16 set, and so KVM's + * ABI lets userspace shove in arbitrary 32-bit values. Drop + * the upper bits to avoid VM-Fail, losing information that + * does't really exist is preferable to killing the VM. + */ + vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, (u16)error_code); intr_info |= INTR_INFO_DELIVER_CODE_MASK; } -- 2.37.1.359.gd136c6c3e2-goog