Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1974207rwo; Thu, 3 Aug 2023 02:52:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlGKUqdE11SybtTSRA0fX4SDo4fkgvCYDKwWwLScdc2z1Wi7g5I6A3l6HYOptS2O1eXp+6ar X-Received: by 2002:a05:6a21:778c:b0:134:a478:5e4a with SMTP id bd12-20020a056a21778c00b00134a4785e4amr17225350pzc.17.1691056345702; Thu, 03 Aug 2023 02:52:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691056345; cv=none; d=google.com; s=arc-20160816; b=BJLA4hKQMF9sAIUQWEHFgSaH0s++p7SKR6JcZ+jYNsi0c1xeuQqP8ZXp6fsH2uxdJU WCnmZZCwhj4JMFKcR2XtlG+8BIhoFyDEyvDleBwMMC+v+QfbVfnuPSnleSfwHxM27Mp7 9FK/E0KjFobOECcXhaXNHm+3vAA3KvfpgyZ05bibeoPqy7ySoQO635bHAjKqlXvS035q 9thwj3Td7+QRWOdZ/w/f+B8OfEF1MegSuVnT4h4J8lAdHhK2ieOz3EhYBiEkE7H8+zTO FV3gcJSNP1Wo9fFc7KgdeGtptNwwnypb3uiwUDYPpY5Dqd+IGpgUgvx4Nzadfi3mEhnG d4qg== 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:reply-to:message-id:subject:to:from:date:dkim-signature; bh=yXnEgMyB+c/lKx03qRo/gn5A2aJhN2Ua96JMPHGvbm8=; fh=zTxwIzMdWQv5BlQG86dz0cetFykC65zbIa5AkE+vFUw=; b=DN5Xw+zRzqlIMG7MRZzkXieWub+wpXT4sIO/U87zFfMtVPLBizqCcDN8zaoIjLBsRR XtPa1U5yRlD01bmNffG2A3F54vnARCUOrFmjl1Ww/d1fRQ2yTNXi6AD5I4igpebX5kcj tHMC/X9nYuoZKPmQr57PdtT7hXu0J5nFswc/KE8CEHMR40K6CWMBW0M2Xk8FAAKXJU1l ibvrdcXvpQ7pM3OYmS6cXXLELzkykYdvGg3HPynKIRSRMVdcTlHGMEqJsMA05ALY2dYI DfRlINOsLX871uH3HCwj1SYCvqelTcU2SSpXuWvSN9GlSkHEDmsVzLdPQvoNJeFEMn35 2jtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mail.ustc.edu.cn header.s=dkim header.b=sd+r0iqT; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mail.ustc.edu.cn Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w184-20020a6282c1000000b00679a6ce03f6si8699285pfd.59.2023.08.03.02.52.13; Thu, 03 Aug 2023 02:52:25 -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=@mail.ustc.edu.cn header.s=dkim header.b=sd+r0iqT; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mail.ustc.edu.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234475AbjHCIo0 (ORCPT + 99 others); Thu, 3 Aug 2023 04:44:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232634AbjHCIoY (ORCPT ); Thu, 3 Aug 2023 04:44:24 -0400 Received: from ustc.edu.cn (email.ustc.edu.cn [IPv6:2001:da8:d800::8]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A190FDA; Thu, 3 Aug 2023 01:44:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ustc.edu.cn; s=dkim; h=Received:Date:From:To:Subject: Message-ID:Reply-To:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=yXnEgMyB+c/lKx03qRo/gn5A2aJh N2Ua96JMPHGvbm8=; b=sd+r0iqT9xJ6UUOW8AsRNOz5v/B21/0i9IhD0VfQOb3Y I7tulBEl0AUysnMflLMbQJCTVjgupVYn3CgsRqDgZzP3uLUSeGwVcFFpv0fXoRZd xECT/T4/yjpIgIr8cXdJ7buk3QzR9LiawPOC4ggwasebs5rvyJe0vEKUnhhiH+I= Received: from localhost (unknown [139.224.204.105]) by newmailweb.ustc.edu.cn (Coremail) with SMTP id LkAmygDHmqbZaMtkmg8nAA--.458S2; Thu, 03 Aug 2023 16:44:09 +0800 (CST) Date: Thu, 3 Aug 2023 16:44:09 +0800 From: Wu Zongyo To: Tom Lendacky , Sean Christopherson , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, x86@kernel.org, linux-coco@lists.linux.dev Subject: Re: [Question] int3 instruction generates a #UD in SEV VM Message-ID: Reply-To: Wu Zongyo 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-CM-TRANSID: LkAmygDHmqbZaMtkmg8nAA--.458S2 X-Coremail-Antispam: 1UD129KBjvJXoWxKr4UKry7GrykJr4xuF43trb_yoW7trWxpF W8G3WDtr4UJr18AryUtr4kAryYyr4fAr4jqr1UAw13Ja4qk3Wrtr4xtrW5CF1UCr4fAr1U tr10q3srWryUArDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUy2b7Iv0xC_KF4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I 8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI 64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8Jw Am72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E 4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGV WUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_ Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rV WrZr1j6s0DMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_ Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jOb18UUUUU= X-CM-SenderInfo: pzx200xj1rqzxdloh3xvwfhvlgxou0/ X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Thu, Aug 03, 2023 at 11:27:12AM +0800, Wu Zongyo wrote: > On Wed, Aug 02, 2023 at 03:03:45PM -0500, Tom Lendacky wrote: > > On 8/2/23 09:33, Tom Lendacky wrote: > > > On 8/2/23 09:25, Tom Lendacky wrote: > > > > On 8/2/23 09:01, Sean Christopherson wrote: > > > > > On Wed, Aug 02, 2023, Wu Zongyo wrote: > > > > > > On Mon, Jul 31, 2023 at 11:45:29PM +0800, wuzongyong wrote: > > > > > > > > > > > > > > On 2023/7/31 23:03, Tom Lendacky wrote: > > > > > > > > On 7/31/23 09:30, Sean Christopherson wrote: > > > > > > > > > On Sat, Jul 29, 2023, wuzongyong wrote: > > > > > > > > > > Hi, > > > > > > > > > > I am writing a firmware in Rust to support > > > > > > > > > > SEV based on project td-shim[1]. > > > > > > > > > > But when I create a SEV VM (just SEV, no > > > > > > > > > > SEV-ES and no SEV-SNP) with the firmware, > > > > > > > > > > the linux kernel crashed because the int3 > > > > > > > > > > instruction in int3_selftest() cause a > > > > > > > > > > #UD. > > > > > > > > > > > > > > > > > > ... > > > > > > > > > > > > > > > > > > > BTW, if a create a normal VM without SEV by > > > > > > > > > > qemu & OVMF, the int3 instruction always > > > > > > > > > > generates a > > > > > > > > > > #BP. > > > > > > > > > > So I am confused now about the behaviour of > > > > > > > > > > int3 instruction, could anyone help to > > > > > > > > > > explain the behaviour? > > > > > > > > > > Any suggestion is appreciated! > > > > > > > > > > > > > > > > > > Have you tried my suggestions from the other thread[*]? > > > > > > > Firstly, I'm sorry for sending muliple mails with the > > > > > > > same content. I thought the mails I sent previously > > > > > > > didn't be sent successfully. > > > > > > > And let's talk the problem here. > > > > > > > > > > > > > > > > > > ??? : > > I'm curious how this happend. I cannot > > > > > > > > > find any condition that would > > > > > > > > > ??? : > > cause the int3 instruction generate a > > > > > > > > > #UD according to the AMD's spec. > > > > > > > > > ??? : > > > > > > > > > ??? : One possibility is that the value from > > > > > > > > > memory that gets executed diverges from the > > > > > > > > > ??? : value that is read out be the #UD handler, > > > > > > > > > e.g. due to patching (doesn't seem to > > > > > > > > > ??? : be the case in this test), stale cache/tlb entries, etc. > > > > > > > > > ??? : > > > > > > > > > ??? : > > BTW, it worked nomarlly with qemu and ovmf. > > > > > > > > > ??? : > > > > > > > > > > ??? : > Does this happen every time you boot the > > > > > > > > > guest with your firmware? What > > > > > > > > > ??? : > processor are you running on? > > > > > > > > > ??? : > > > > > > > Yes, every time. > > > > > > > The processor I used is EPYC 7T83. > > > > > > > > > ??? : And have you ruled out KVM as the > > > > > > > > > culprit?? I.e. verified that KVM is NOT > > > > > > > > > injecting > > > > > > > > > ??? : a #UD.? That obviously shouldn't happen, > > > > > > > > > but it should be easy to check via KVM > > > > > > > > > ??? : tracepoints. > > > > > > > > > > > > > > > > I have a feeling that KVM is injecting the #UD, but > > > > > > > > it will take instrumenting KVM to see which path the > > > > > > > > #UD is being injected from. > > > > > > > > > > > > > > > > Wu Zongyo, can you add some instrumentation to > > > > > > > > figure that out if the trace points towards KVM > > > > > > > > injecting the #UD? > > > > > > > Ok, I will try to do that. > > > > > > You're right. The #UD is injected by KVM. > > > > > > > > > > > > The path I found is: > > > > > > ???? svm_vcpu_run > > > > > > ???????? svm_complete_interrupts > > > > > > ??????? kvm_requeue_exception // vector = 3 > > > > > > ??????????? kvm_make_request > > > > > > > > > > > > ???? vcpu_enter_guest > > > > > > ???????? kvm_check_and_inject_events > > > > > > ??????? svm_inject_exception > > > > > > ??????????? svm_update_soft_interrupt_rip > > > > > > ??????????? __svm_skip_emulated_instruction > > > > > > ??????????????? x86_emulate_instruction > > > > > > ??????????????? svm_can_emulate_instruction > > > > > > ??????????????????? kvm_queue_exception(vcpu, UD_VECTOR) > > > > > > > > > > > > Does this mean a #PF intercept occur when the guest try to deliver a > > > > > > #BP through the IDT? But why? > > > > > > > > > > I doubt it's a #PF.? 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. > > > > > > > > > > What is the last exit recorded by trace_kvm_exit() before the > > > > > #UD is injected? > > > > > > > > I'm guessing it was a #NPF, too. Could it be related to the changes that > > > > went in around svm_update_soft_interrupt_rip()? > Yes, it's a #NPF with exit code 0x400. > > There must be something I didn't handle corretly since it behave normally with > qemu & ovmf If I don't add int3 before mcheck_cpu_init(). > > So it'a about memory, is there something I need to pay special attention > to? > > Thanks I check the fault address of #NPF, and it is the IDT entry address of the guest kernel. The NPT page table is not constructed for the IDT entry and the #NPF is generated when guest try to access IDT. With qemu & ovmf, I didn't see the #NPF when guest invoke the int3 handler. That means the NPT page table has already been constructed, but when? > > > > > > > > 6ef88d6e36c2 ("KVM: SVM: Re-inject INT3/INTO instead of retrying the > > > > instruction") > > > > > > Sorry, that should have been: > > > > > > 7e5b5ef8dca3 ("KVM: SVM: Re-inject INTn instead of retrying the insn on > > > "failure"") > > > > Doh! I was right the first time... sigh > > > > 6ef88d6e36c2 ("KVM: SVM: Re-inject INT3/INTO instead of retrying the instruction") > > > > Thanks, > > Tom > > > > > > > > > > > > > Before this the !nrips check would prevent the call into > > > > svm_skip_emulated_instruction(). But now, there is a call to: > > > > > > > > ?? svm_update_soft_interrupt_rip() > > > > ???? __svm_skip_emulated_instruction() > > > > ?????? kvm_emulate_instruction() > > > > ???????? x86_emulate_instruction() (passed a NULL insn pointer) > > > > ?????????? kvm_can_emulate_insn() (passed a NULL insn pointer) > > > > ???????????? svm_can_emulate_instruction() (passed NULL insn pointer) > > > > > > > > Because it is an SEV guest, it ends up in the "if (unlikely(!insn))" path > > > > and injects the #UD. > > > > > > > > Thanks, > > > > Tom > > > >