Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1846179iog; Tue, 14 Jun 2022 14:55:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vrFocLGOVP+l8O6kNzhRQezFXas8bq3jWSOp5jm80BK2sbuBiXuNXohxmvqkyvEUrWjcmt X-Received: by 2002:a05:6a00:ad0:b0:4f7:a357:6899 with SMTP id c16-20020a056a000ad000b004f7a3576899mr6368499pfl.80.1655243740855; Tue, 14 Jun 2022 14:55:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655243740; cv=none; d=google.com; s=arc-20160816; b=pM1Z5REOjx1/uUPyv6JnHU3qODvaxHInkD/IO0Qcar1QoAX63k7i4zvPH/j1gtl2+p efI/gvz8dX1IH544QgFKfe6FjJw6FEpS9YzkKI1WUiRHY+pvN9Ttm7HRCokuBn97iWWU DX0yKxmraYOGxbf1txsQiosZXN042z6L4Tkp/JE6zMj5hVhSDMncoxOyOYr4uj0iTRl1 MipmgJ30YouRFooU414gvjOvwlEVZshsS8NXM6+Kl7Qu+O26dpef0EREx9KXWnpVmw3f IVUcEqXP35V7dz3FE59mPvOztj3EDD2Hu+iEFxqAjbBcLHh6dR4wmsLNzfHPXAC1sJP7 RbDw== 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=u8bzNiBps6E2wSU5SkQtzvlTmzoTVHSDY5lFfXr39Zk=; b=xIC3ymnx0GuyrvFp87I8yjV8LUKOOVnn8rdJKclFCxfxd8wj2iQwozQgdDbUwTziUm VsjLCW4e7vkxlg/FHChltMqmnxIeiMBs8WIYT4BHLHa/66Z6Cqe8+R50//SbUEII+k3L /a2rF6Jkt2ea9UtuRfR37AVJjAAYZ8CDi31OhS4v0gO8tNIv8YOElBbygdxka8ttuc2w SsH53KKOu3dccLxa2T5COtMlW3PiA9AwM57NNry6nj3R07iBLJuLwNJ7Crwy2NUBXXtk jkNJi0wvlE3MHSzb91H4aL/OAy1HZ2JMoKuKdSk/x+Ri+OFujo6mgS57zR4amacKS03S y3Fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="NW/smZki"; 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 q15-20020a056a00084f00b005109299b02fsi17206864pfk.113.2022.06.14.14.55.26; Tue, 14 Jun 2022 14:55:40 -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="NW/smZki"; 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 S243785AbiFNUrw (ORCPT + 99 others); Tue, 14 Jun 2022 16:47:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235090AbiFNUrk (ORCPT ); Tue, 14 Jun 2022 16:47:40 -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 1A5481E3E1 for ; Tue, 14 Jun 2022 13:47:40 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id b9-20020a656689000000b003f672946300so5467580pgw.16 for ; Tue, 14 Jun 2022 13:47:40 -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=u8bzNiBps6E2wSU5SkQtzvlTmzoTVHSDY5lFfXr39Zk=; b=NW/smZkiDd1uJ4BP5Z2HhFtWDNoo8oNHVv9ysPZG6omGQQshynXbpsBgtihy5M542x mwXglKnK2f2nwTv5CH3kFCD1UDuWlOElGHVpVE2G/8JtD3YQkgKB6lgReLoR8GfVa0/v 6Y/HdUGgTvmwTUqcQeC1HOermxW17NFfypftkD+cMAQmCGOINwFC0iDLwJ0UOZpdvy0B LDrBdZnaRP5NsJHvfcxmdaDRZ6P7L5jcOVSl67ru+mE0T3H0x6OGKkDk4JlXlpX15oXK Xfc2ixz71VShPaTQPjHvTtzJaJzuxLBIhPttmyV/aWVswn9Po2Xwh8lcdPQFChj2SJA1 K25g== 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=u8bzNiBps6E2wSU5SkQtzvlTmzoTVHSDY5lFfXr39Zk=; b=W1RwLOr4k+MuEwo6dFs+atKnGXpExvJopVYXFCtI1aiDlGmN/gj1htLIJ/5topjssC gtojqclLePgNvYs28BXXsu2R4sxgdij8OUcP30cTfMem1IvUlu7ZenO3RuWwlWdyzdfJ Pos/VE+7i+X92Sppv1tkNlnqLZMOv3jipXd+1vmg/XpZY0PC+uOrDpLcfTWTs1kH5XRg uC12kHmNeVsJIJksFn1GpRdAZ33sLoknDQ6r7qeXTgFm8rL9fddvuPmmEY2fsYDCjagI WkJjgAgxe0BRfG33FUPHyY7uPTp4hlaaEaMUmgn1VK90IQpzWKBmmxuXMgvv0yUoHxez KH9Q== X-Gm-Message-State: AJIora+K3O8Dn/Zq0yUTe5PN76Yb0NshTkmQLfRykrhiL9IeIxmF8zt+ 8gv2v8KNYZzGb44B3XOTX5MPjTXDkRI= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:902:930c:b0:167:8960:2c39 with SMTP id bc12-20020a170902930c00b0016789602c39mr6024164plb.33.1655239659566; Tue, 14 Jun 2022 13:47:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Jun 2022 20:47:11 +0000 In-Reply-To: <20220614204730.3359543-1-seanjc@google.com> Message-Id: <20220614204730.3359543-3-seanjc@google.com> Mime-Version: 1.0 References: <20220614204730.3359543-1-seanjc@google.com> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH v2 02/21] KVM: VMX: Drop bits 31:16 when shoving exception error code into VMCS From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, 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,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham 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 --- arch/x86/kvm/vmx/nested.c | 9 ++++++++- arch/x86/kvm/vmx/vmx.c | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index ee6f27dffdba..33ffc8bcf9cd 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3833,7 +3833,14 @@ 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 will never generate an error code 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. + */ + 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 5e14e4c40007..ec98992024e2 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1621,7 +1621,16 @@ 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 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.36.1.476.g0c4daa206d-goog