Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1101202rwo; Wed, 2 Aug 2023 08:44:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlEW+haOLHuow3Vml6qbyrjZqU9DaC61hXzNGr1pOrOFGXh5l6vIqdENxEXRAUwC2o+adeO3 X-Received: by 2002:aa7:df99:0:b0:522:216a:28cd with SMTP id b25-20020aa7df99000000b00522216a28cdmr5319099edy.7.1690991039881; Wed, 02 Aug 2023 08:43:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690991039; cv=none; d=google.com; s=arc-20160816; b=OxipUAtijQXI0P3c09FKuFH9YZnPsld5oL2oCmyEVU72Cl0NSB1nC6/AfZ8PGMvqAp ukmLNIr75RUv5gPL6vwpe/j7PuIIWaClG6PEphZwSvy9+fl7gSlX4TgtUhIdCRHwh2GG dS3uOwyBj+USjpcMp8+XMJ43EpWGz286ut8AB1no1y8xKgxrC8uJAy9rG9wWCqugqLRO aDhBze2Up2WhZi2BGA60EdJj5VpyU1D3AFaTNnYuUAgz2kYntMpOJFYStGLJmiynMQEy fCWpPCkTpNr+aV1jiW6HMX3K1U+e8JJMwo5gMp9AE8qRTXtTFw0BpTvsE1rEnVA+l6+Y htXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=UUMABgRf8+JLrJEPYxIPAcBfEzRgqars/lrXYCb1BUE=; fh=/OeRilf9OQVhmfIPbwulsKpPHdUUb+i4ioYcpliqSBM=; b=Z2nR5R9TZEAfRlM5RUZS1/70NcXavh0Ib82SVZfYXHzBqBXVdAnyvgDAqrOG5Vs/32 jPzHwfSWDOQJlrZS14rKW9NpmgvebpbhJynwgVQ9MMgqinnvwaSqhLZQpsbRTZcDMzt6 GLSxSO3Pl35H2FMOeIwYmixKEjoPhHb5Ed48JvvA8fi2ACxcdK1mGeqUQDjLE9dichh1 t8plNC5Vqe8nzJm+GPbAiKGBloGyCZ+3r0PvrfTqxDY9E/8wqAlxItQNgsWH6fIB/Q7H /9NBFfpBejUr0RVdYSaV2zMCpdlhI6ToGzLjRL2PzVBkA0kpriUHRzXcdIPEDJprfzyg hFWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=rSbGrQHo; 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 u15-20020aa7d88f000000b005221dcc6470si1910424edq.591.2023.08.02.08.43.35; Wed, 02 Aug 2023 08:43:59 -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=20221208 header.b=rSbGrQHo; 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 S234173AbjHBPFz (ORCPT + 99 others); Wed, 2 Aug 2023 11:05:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234756AbjHBPFl (ORCPT ); Wed, 2 Aug 2023 11:05:41 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B006C3AAB for ; Wed, 2 Aug 2023 08:04:44 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d1d9814b89fso6331144276.0 for ; Wed, 02 Aug 2023 08:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690988683; x=1691593483; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=UUMABgRf8+JLrJEPYxIPAcBfEzRgqars/lrXYCb1BUE=; b=rSbGrQHo3VRf0sw+Ax8g0TxoxBZdQnoZh2HFkFM71zPpXZx/A3RCmEzkepLrMt7T3k xPLUVeQYTz7USaEGMHSpxHS+MfkHoKSv4GhxoPCGB6E+IoC64HSHnvrcIHe1J2Uo6KHs p5OvsWht9tX3QU5dCiNxQC1FNRnP8KthSqNyvyPAuBNG8rSshP07rWmGsTj33H1UpuZF UyUCZbphP1jFYX6CJ3iF3GjZJJ2hECe2bfvdUOndflz7Jcp4QY29J09VRVPPTmgYOV28 ZCghnJ0ohRCf16vO0Jf9WIFO0ag+NO1xE594ltOegaPWmFCRa9ZRcmIKgSIWs/Wx8QTY NXSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690988683; x=1691593483; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=UUMABgRf8+JLrJEPYxIPAcBfEzRgqars/lrXYCb1BUE=; b=cDHWI5KRlOoLXKHGsuiRqYsPYCjuI1rsvDmV+3MjuxWM0bHAd/OhEoiP4lQbnLG+Ve 0AfDBqoRTe1NPxIJD1BeIZTKDY3JZ4KdUx+jZQEtgjOU5ciI1cgDahwU/UtNmzh46sSG +kUTV2i+4NAd5BiC+v0YJtd+nN9BLCYp0dhzSsu4W2Z8jWBmh2TNvYyg1o4yhkp83Zxn N5stJxSAtpIVrOGlVw18VJXlJiE93gnArmJV6+cqOIJC4K29YwyEJ7vJyFWfQaWnIpOx bMKK92LAqeVCzkq1ZXbUbKsSkZ4vej56R1/U4aCtpN90QUdVElX3BT2XmXe/KKQQX31s FA8w== X-Gm-Message-State: ABy/qLYgJ8uPilKUrthjepfkyfSD0ZvG6gIY5ig074DCOUijPhMEOM5I P6/m00+NWkHvuPwpo0WzoFqSS6RwC1Y= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:69c6:0:b0:d0f:cf5:3282 with SMTP id e189-20020a2569c6000000b00d0f0cf53282mr123293ybc.1.1690988683739; Wed, 02 Aug 2023 08:04:43 -0700 (PDT) Date: Wed, 2 Aug 2023 08:04:41 -0700 In-Reply-To: <7a4f3f59-1482-49c4-92b2-aa621e9b06b3@amd.com> Mime-Version: 1.0 References: <8eb933fd-2cf3-d7a9-32fe-2a1d82eac42a@mail.ustc.edu.cn> <4ebb3e20-a043-8ad3-ef6c-f64c2443412c@amd.com> <544b7f95-4b34-654d-a57b-3791a6f4fd5f@mail.ustc.edu.cn> <7a4f3f59-1482-49c4-92b2-aa621e9b06b3@amd.com> Message-ID: Subject: Re: [Question] int3 instruction generates a #UD in SEV VM From: Sean Christopherson To: Tom Lendacky Cc: Wu Zongyo , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, x86@kernel.org, linux-coco@lists.linux.dev Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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_BLOCKED,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 On Wed, Aug 02, 2023, Tom Lendacky wrote: > On 8/2/23 09:25, Tom Lendacky wrote: > > On 8/2/23 09:01, Sean Christopherson wrote: > > > > You're right. The #UD is injected by KVM. > > > >=20 > > > > The path I found is: > > > > =C2=A0=C2=A0=C2=A0=C2=A0 svm_vcpu_run > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 svm_complete_inter= rupts > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kvm_requeue_exception //= vector =3D 3 > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = kvm_make_request > > > >=20 > > > > =C2=A0=C2=A0=C2=A0=C2=A0 vcpu_enter_guest > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kvm_check_and_inje= ct_events > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 svm_inject_exception > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = svm_update_soft_interrupt_rip > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = __svm_skip_emulated_instruction > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 x86_emulate_instruction > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 svm_can_emulate_instruction > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kvm_queue_exception(vcpu, = UD_VECTOR) > > > >=20 > > > > Does this mean a #PF intercept occur when the guest try to deliver = a > > > > #BP through the IDT? But why? > > >=20 > > > I doubt it's a #PF.=C2=A0 A #NPF is much more likely, though it could= be > > > something > > > else entirely, but I'm pretty sure that would require bugs in both > > > the host and > > > guest. > > >=20 > > > What is the last exit recorded by trace_kvm_exit() before the #UD is > > > injected? > >=20 > > I'm guessing it was a #NPF, too. Could it be related to the changes tha= t > > went in around svm_update_soft_interrupt_rip()? > >=20 > > 6ef88d6e36c2 ("KVM: SVM: Re-inject INT3/INTO instead of retrying the > > instruction") >=20 > Sorry, that should have been: >=20 > 7e5b5ef8dca3 ("KVM: SVM: Re-inject INTn instead of retrying the insn on "= failure"") >=20 > >=20 > > Before this the !nrips check would prevent the call into > > svm_skip_emulated_instruction(). But now, there is a call to: > >=20 > > =C2=A0 svm_update_soft_interrupt_rip() > > =C2=A0=C2=A0=C2=A0 __svm_skip_emulated_instruction() > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kvm_emulate_instruction() > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 x86_emulate_instruction() (= passed a NULL insn pointer) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kvm_can_emulate= _insn() (passed a NULL insn pointer) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 svm= _can_emulate_instruction() (passed NULL insn pointer) > >=20 > > Because it is an SEV guest, it ends up in the "if (unlikely(!insn))" pa= th > > and injects the #UD. Yeah, my money is on that too. I believe this is the least awful solution: diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d381ad424554..2eace114a934 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -385,6 +385,9 @@ static int __svm_skip_emulated_instruction(struct kvm_v= cpu *vcpu, } =20 if (!svm->next_rip) { + if (sev_guest(vcpu->kvm)) + return 0; + if (unlikely(!commit_side_effects)) old_rflags =3D svm->vmcb->save.rflags; =20 I'll send a formal patch (with a comment) if that solves the problem. Side topic, KVM should require nrips for SEV and beyond, I don't see how SE= V can possibly work if KVM doesn't utilize nrips. E.g. this diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2eace114a934..43e500503d48 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5111,9 +5111,11 @@ static __init int svm_hardware_setup(void) =20 svm_adjust_mmio_mask(); =20 + nrips =3D nrips && boot_cpu_has(X86_FEATURE_NRIPS); + /* * Note, SEV setup consumes npt_enabled and enable_mmio_caching (wh= ich - * may be modified by svm_adjust_mmio_mask()). + * may be modified by svm_adjust_mmio_mask()), as well as nrips. */ sev_hardware_setup(); =20 @@ -5125,11 +5127,6 @@ static __init int svm_hardware_setup(void) goto err; } =20 - if (nrips) { - if (!boot_cpu_has(X86_FEATURE_NRIPS)) - nrips =3D false; - } - enable_apicv =3D avic =3D avic && avic_hardware_setup(); =20 if (!enable_apicv) {