Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp202453pxb; Thu, 14 Jan 2021 03:51:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJzEWtIkV1h8qXyD3mXBq886SmR+Yp7dgiL4E03tcWhnB+K54s6rAYwetUDFEB5SnFeK+diA X-Received: by 2002:a17:906:3094:: with SMTP id 20mr4866953ejv.218.1610625109987; Thu, 14 Jan 2021 03:51:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610625109; cv=none; d=google.com; s=arc-20160816; b=W5el412SmsxtYqq7Kwullk1w/MDUkwCdHHz9Y7OLm5PDPacnQrrOzZE+EV7X3rgOUb QbnSGtpzut8rK5cFFczSeBF5OidqMvL2IllV9tYkAL8VfmAeyjSGFmJg7Sz3Y5KGd6Z9 7lAQMjVKjh5wKX1J4Xq7/xmxlS/UsSbu0ne2lPXdVt1EUZBy8esskNfEAusSqMcuddOh ST+07edtJB5gqDb+9y4ReRdDi+9bRtfPeP+2OecvWZ89RnJCjFEfM1oSI9+6FaMx26Cr oGeT++oYbA71t8LTlg+sBHG6AaCL0WY5BgYZ4X10cEYIsMY5UJ3nzDceIJNSR/e5nmCO ZPXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :date:cc:to:from:subject:message-id:dkim-signature; bh=KJCiyPYZTNhq1kKkty7sXVGcoXJ6M0psKDMUOgP2lPE=; b=yZyEcYFNS3mDpa9kZIwTyZYfQsgisFnBRmnt7fs571g4+Q5WCkfIlbWGIdZOOmZM5y g/DLVPcEKWlNGv8zPeOhbVYBxwxIqB5bBidU4DBSQMzfWmNY+EjUrB3IHRvcTg28JLNb BuUdCSbKC2UJNBae7M3T1S6hgrHkOy7jDgYKQXdxopT4t9KB1NNs7qswm3tXxOS1WdZ1 Y6dStkC/b/fT+RamZBqU/eM24PjYUzdbll3pGNPlqTA76M6dw3XuZsinIbeP3V1Bsm3Y ZlJjet1zBRd+i6nJhOZFeSDeff8tzuFuKzqKpAwrbFamNeMldKZHJJBzXAdCAxj+GwAZ eMGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dun9pJN5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dp16si2130298ejc.564.2021.01.14.03.51.23; Thu, 14 Jan 2021 03:51:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dun9pJN5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727878AbhANLtI (ORCPT + 99 others); Thu, 14 Jan 2021 06:49:08 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:37513 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727202AbhANLtI (ORCPT ); Thu, 14 Jan 2021 06:49:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610624861; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=KJCiyPYZTNhq1kKkty7sXVGcoXJ6M0psKDMUOgP2lPE=; b=Dun9pJN5a/2FavC97gb7kEAL4ttni50IOwuhHnXHi5zRVHcm0vwI7Jhpo3ovMUSo0aRQVc W6Z2x8/XCGG9gHF4fnitPX0xJF/KrzNlom4oyi755vEKZpj9FROhClFT9o6dEYj+KB9CBB hDdItp1gJIjjWN9rg/rCLjg1J1ZuiXU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-576-XaxpEonTP4O0k4dRUyyHdg-1; Thu, 14 Jan 2021 06:47:39 -0500 X-MC-Unique: XaxpEonTP4O0k4dRUyyHdg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A9693107ACF8; Thu, 14 Jan 2021 11:47:37 +0000 (UTC) Received: from starship (unknown [10.35.206.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id E77E45D6AD; Thu, 14 Jan 2021 11:47:30 +0000 (UTC) Message-ID: Subject: Re: [PATCH 1/2] KVM: x86: Add emulation support for #GP triggered by VM instructions From: Maxim Levitsky To: Bandan Das , Sean Christopherson Cc: Wei Huang , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, vkuznets@redhat.com, joro@8bytes.org, bp@alien8.de, tglx@linutronix.de, mingo@redhat.com, x86@kernel.org, jmattson@google.com, wanpengli@tencent.com, dgilbert@redhat.com Date: Thu, 14 Jan 2021 13:47:28 +0200 In-Reply-To: References: <20210112063703.539893-1-wei.huang2@amd.com> Content-Type: multipart/mixed; boundary="=-EPygeQZyFJqIITg/+qQu" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --=-EPygeQZyFJqIITg/+qQu Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit On Tue, 2021-01-12 at 15:00 -0500, Bandan Das wrote: > Sean Christopherson writes: > ... > > > - if ((emulation_type & EMULTYPE_VMWARE_GP) && > > > - !is_vmware_backdoor_opcode(ctxt)) { > > > - kvm_queue_exception_e(vcpu, GP_VECTOR, 0); > > > - return 1; > > > + if (emulation_type & EMULTYPE_PARAVIRT_GP) { > > > + vminstr = is_vm_instr_opcode(ctxt); > > > + if (!vminstr && !is_vmware_backdoor_opcode(ctxt)) { > > > + kvm_queue_exception_e(vcpu, GP_VECTOR, 0); > > > + return 1; > > > + } > > > + if (vminstr) > > > + return vminstr; > > > > I'm pretty sure this doesn't correctly handle a VM-instr in L2 that hits a bad > > L0 GPA and that L1 wants to intercept. The intercept bitmap isn't checked until > > x86_emulate_insn(), and the vm*_interception() helpers expect nested VM-Exits to > > be handled further up the stack. Actually IMHO this exactly what we want. We want L0 to always intercept these #GPs, and hide them from the guest. What we do need to do (and I prepared and attached a patch for that, is that if we run a guest, we want to inject corresponding vmexit (like SVM_EXIT_VMRUN) instead of emulating the instruction. The attached patch does this, and it made my kvm unit test pass, even if the test was run in a VM (with an unpatched kernel). This together with setting that X86_FEATURE_SVME_ADDR_CHK bit for the guest will allow us to hide that errata completely from the guest which is a very good thing. (for example for guests that we can't modify) Best regards, Maxim Levitsky > > > So, the condition is that L2 executes a vmload and #GPs on a reserved address, jumps to L0 - L0 doesn't > check if L1 has asked for the instruction to be intercepted and goes on with emulating > vmload and returning back to L2 ? > > > > } > > > > > > /* > > > -- > > > 2.27.0 > > > --=-EPygeQZyFJqIITg/+qQu Content-Disposition: attachment; filename="patch.diff" Content-Type: text/x-patch; name="patch.diff"; charset="UTF-8" Content-Transfer-Encoding: base64 Y29tbWl0IDI4YWI4OWFhYTExMzgwMzA2YmFmYmY0OTI2NTIyMmYyYTJkYTcxZGEKQXV0aG9yOiBN YXhpbSBMZXZpdHNreSA8bWxldml0c2tAcmVkaGF0LmNvbT4KRGF0ZTogICBUaHUgSmFuIDE0IDEw OjUzOjI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