Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp3326533pxb; Tue, 12 Jan 2021 11:43:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJxSTGlFfsGy+n3wKBBQ7838D4l1vzxVVCxJufmb2p0fLdEf0Ll9Eofdj7vfKuWlwaw0hRim X-Received: by 2002:a17:906:2681:: with SMTP id t1mr287424ejc.29.1610480596110; Tue, 12 Jan 2021 11:43:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610480596; cv=none; d=google.com; s=arc-20160816; b=ymLpVVwHzqlCFnvcRpLK2W7aMxnS/hi+NrQjBQvCWg4mKEzrlTV5+NPGVA+nSTMn+c nwlN3frizHRN9bcwP5iDPU7mdgIo9ELemDO+iFlKGKAcvTBGraQZixWLbEfpUt13KDFe F0jm7Vs6Q+6hDGI8RMtf4K7lLykr4d4PtPySNHJ8g2G/oM2j8FaeEUk7/bd0xvqcxTRN DjPIIdXQfHvaI0ZqE62Dzm9eNyVKHiMastsYo8/SakKyIisLuzVL0sP0j9wYm/OFntEu gZra0ZOKaLDW/EKXhteWg+mx29ff3ohyBjvpbVYQNrVkHjgpTXJgMGDQlFX3AuorriHc 3Oeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=hPaJJwg8MTOSNiRBibVEGrx1HwOpvfxrWoAXKh+WulM=; b=mi7L9+LilL276mN3mXzlLH0XnyYhGnIbrG9W2JfRlle9jnPDJpbXpgjkvlcIzcdDyd xzQeBoUdcT2Y3WW0904VNWF3YGCVUag5M/L2JE7KJT88lyBPzDCVD8e1HnbxyLnWG3WJ 0ClsfxxRBv+XXtby7pVwSeEOZ3qZHgBCcEU8y2+4X2bjeIeeN/fHPRFLKFDvrNXDj5lh qhh7KOBZaqnOslt8MjhsKGJrjdgPzbQmXXf+zH2QlUt1rEx+xzU+NoKcQ6ifNaozkRfq vn6M7RViwT2+MQNaQ4n+0KLLN8QuW+r+FIiwMbg1jBnHzgwkMso0GN0UdidxLVAQqnPw Gm7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=auBKf9s7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q10si1669070eds.293.2021.01.12.11.42.52; Tue, 12 Jan 2021 11:43:16 -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=@google.com header.s=20161025 header.b=auBKf9s7; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393117AbhALTlh (ORCPT + 99 others); Tue, 12 Jan 2021 14:41:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392138AbhALTlg (ORCPT ); Tue, 12 Jan 2021 14:41:36 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BC6BC061794 for ; Tue, 12 Jan 2021 11:40:56 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id cq1so2070541pjb.4 for ; Tue, 12 Jan 2021 11:40:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=hPaJJwg8MTOSNiRBibVEGrx1HwOpvfxrWoAXKh+WulM=; b=auBKf9s7D1W2kYZXznvNER4bC+hHK8sBoZQbhpUb9hh1MrRreiXgOlF+E39eW6y2oB o2aRwPVdeqslvRwRESPvicu0U57HODD/AZn1jPSzT6MiW5ESsLEaeF0f157rOLcyLZZq vItrN7/M9EZbN0wsGcTiFn9ZItaffsjhqHyr5ltmu3PXBuqGqY/xnufQq7xRLCGT3nqE 668JqSlaokH5ixIGZpwEf9FmOhwD/wHih4WpZnK65GeBwdWtwv3PI7J/BDkJ2LbbvYq0 0meCDAsLnj30hZpLgLwkF6NbcopW0Q929Lbsr35UF7CLeKSUZSQRbjlAnk1cnsc4e1NB I7Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=hPaJJwg8MTOSNiRBibVEGrx1HwOpvfxrWoAXKh+WulM=; b=LavzZnBqHSooQhYsf/rp5kNPpeF6l4pdOxXESd7n5K8EiHitUwV3chTs0pumdlutmf ySPZ5AQ1RkDymPqy414JhKXDjy+rhz8pWQKeDeGZgmZmoGJmB1XFaEsx2JgA+x3lZhbF CzOAS8hF/VlMWCSQ0zyinnS6YB3QoX8YAzd5IYxrPdSdVXlBb/d+dn18B/V86sQM3NLk ZfXiqCRCk9InGq/fIJnO4T9jOHtZ7puM+0U7Nkox7hh+6aSDsaZO+lH7iu0csQAWUdfA hXuHVgZmaIjdmUyMuyFQutFMVYxXE+egfUhsSH+XZV243dXhne361gThmoxhQBj/oUBw HN/Q== X-Gm-Message-State: AOAM533UbZqtrEBAF8C0/EqAzsxTNbUFHwrgEX5sQVTMWpDxW6X+YeX/ H3E58hrAe0okL+VkJei0kIuiLA== X-Received: by 2002:a17:902:724b:b029:de:229a:47f1 with SMTP id c11-20020a170902724bb02900de229a47f1mr791709pll.10.1610480455876; Tue, 12 Jan 2021 11:40:55 -0800 (PST) Received: from google.com ([2620:15c:f:10:1ea0:b8ff:fe73:50f5]) by smtp.gmail.com with ESMTPSA id r20sm4608042pgb.3.2021.01.12.11.40.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jan 2021 11:40:55 -0800 (PST) Date: Tue, 12 Jan 2021 11:40:48 -0800 From: Sean Christopherson To: Wei Huang Cc: 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, bsd@redhat.com, dgilbert@redhat.com, mlevitsk@redhat.com Subject: Re: [PATCH 1/2] KVM: x86: Add emulation support for #GP triggered by VM instructions Message-ID: References: <20210112063703.539893-1-wei.huang2@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210112063703.539893-1-wei.huang2@amd.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 12, 2021, Wei Huang wrote: > +/* Emulate SVM VM execution instructions */ > +static int svm_emulate_vm_instr(struct kvm_vcpu *vcpu, u8 modrm) > +{ > + struct vcpu_svm *svm = to_svm(vcpu); > + > + switch (modrm) { > + case 0xd8: /* VMRUN */ > + return vmrun_interception(svm); > + case 0xda: /* VMLOAD */ > + return vmload_interception(svm); > + case 0xdb: /* VMSAVE */ > + return vmsave_interception(svm); > + default: > + /* inject a #GP for all other cases */ > + kvm_queue_exception_e(vcpu, GP_VECTOR, 0); > + return 1; > + } > +} v> + > static int gp_interception(struct vcpu_svm *svm) > { > struct kvm_vcpu *vcpu = &svm->vcpu; > u32 error_code = svm->vmcb->control.exit_info_1; > - > - WARN_ON_ONCE(!enable_vmware_backdoor); > + int rc; > > /* > - * VMware backdoor emulation on #GP interception only handles IN{S}, > - * OUT{S}, and RDPMC, none of which generate a non-zero error code. > + * Only VMware backdoor and SVM VME errata are handled. Neither of > + * them has non-zero error codes. > */ > if (error_code) { > kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); > return 1; > } > - return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP); > + > + rc = kvm_emulate_instruction(vcpu, EMULTYPE_PARAVIRT_GP); > + if (rc > 1) > + rc = svm_emulate_vm_instr(vcpu, rc); > + return rc; > } ... > +static int is_vm_instr_opcode(struct x86_emulate_ctxt *ctxt) > +{ > + unsigned long rax; > + > + if (ctxt->b != 0x1) > + return 0; > + > + switch (ctxt->modrm) { > + case 0xd8: /* VMRUN */ > + case 0xda: /* VMLOAD */ > + case 0xdb: /* VMSAVE */ > + rax = kvm_register_read(emul_to_vcpu(ctxt), VCPU_REGS_RAX); > + if (!kvm_is_host_reserved_region(rax)) > + return 0; > + break; > + default: > + return 0; > + } > + > + return ctxt->modrm; > +} > + > static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) > { > switch (ctxt->opcode_len) { > @@ -7305,6 +7327,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, > struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; > bool writeback = true; > bool write_fault_to_spt; > + int vminstr; > > if (unlikely(!kvm_x86_ops.can_emulate_instruction(vcpu, insn, insn_len))) > return 1; > @@ -7367,10 +7390,14 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, > } > } > > - 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. > } > > /* > -- > 2.27.0 >