Message ID | 20230121001542.2472357-9-ackerleytng@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp496334wrn; Fri, 20 Jan 2023 16:18:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXtXjiFOweIBayHRDTp/SqgmvJojplMUHbIwBe83pEaowdH42DhDKjIjuYzxY7SoDzFJ8NfV X-Received: by 2002:a05:6a20:8f0f:b0:ac:9d6b:c1f0 with SMTP id b15-20020a056a208f0f00b000ac9d6bc1f0mr22673151pzk.40.1674260287107; Fri, 20 Jan 2023 16:18:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674260287; cv=none; d=google.com; s=arc-20160816; b=rFRSvLCyhUBvrg9UPVdlyQYEyJGmySctsxjZGIs7nYnApzAwVH/5GqmvDxjGK4owdn FeGv1evR6dqEWeA0hjN/PV2zT/AtjmAt4qnO74EV5ArVWI55Xmldq6E8Dj/A4Ce+ICB1 DV0ysM01/329bsaeSMjSyZGj7UteVJ2YZryme1u0JXLW9jbeHuBSfNDhYhBv3w7VDC7V uzWpEenbPfc5jOh6RxjF65mGVD1guhTwTrdCwIGUkjgMU+nc4W93+j8U2VscGG3KQR/W pSZll9pKCMw/kqtRQaYNoeNzPwjRz9ueT+9p0001N9UI1ntUHpoY4vZUFpvao8Lm9Enx VeIg== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=iSSiGqnlhzGiDLCzSAtleJIHOm3o/jPruEIVdPoSPmM=; b=HSRHA6bEpT6I6o64fHVg4spNTZoyv8x+B1QVPHtxtTMptwdIA4hpZKCMWnTdY5FY1a Pkje1tqLzbc9l/oJzHmcdcR5OLDgzgN1MW4KbGExJWZjoNLASiJL1IbihtCH/Gj9XelC TNjn3O2QXc3Dyde5Pxw39Dde4MCBI8MXsxAu3Nt+F0fnISAFyem6lKvdmCmsx6RTL8CA lfZNt6gRtmV77wjHsrjbyoBMklMj8Cj9XS6x/3KnR3x+FzYQPZzxaNCP6P1/5t6jMFER G/x1DL3wqdPxEFYZNUhsSSEzkiBn/Dr3tSu59syxcNFm93JrcjXMb216RnD+0TjFeAeH b2bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=i3bQf8fO; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q145-20020a632a97000000b004ce5301ee08si14249423pgq.346.2023.01.20.16.17.55; Fri, 20 Jan 2023 16:18:07 -0800 (PST) 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=i3bQf8fO; 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 S229604AbjAUARc (ORCPT <rfc822;forouhar.linux@gmail.com> + 99 others); Fri, 20 Jan 2023 19:17:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbjAUARV (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 20 Jan 2023 19:17:21 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E3B0BAF19 for <linux-kernel@vger.kernel.org>; Fri, 20 Jan 2023 16:16:54 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id a14-20020a5b0ace000000b007bf99065fcbso7437957ybr.2 for <linux-kernel@vger.kernel.org>; Fri, 20 Jan 2023 16:16:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iSSiGqnlhzGiDLCzSAtleJIHOm3o/jPruEIVdPoSPmM=; b=i3bQf8fOpOKYn4DsIRRzCUmnsCj+LMJV96+HJVERlyQV50GGhU4m9vph9dFvXLxh33 a6PijOok3GOHph4F2aF0Uo8zDnzeIuzhlwcNn/lmpe5ODxgUHA6krkenLMHAVsTMVG8A DQOBErL8T8utFKaa2hBwsfFZN+8NkBWs2+uZyhMMxhKrvH43FYJ7I3M1J/lUsHYsC3JO fw9Xc1e8mfpAWmMp8YsemFubhIbnl6sBYlN9R8OgKxypq4dN68xuIBEA7FNjAMYzR1gV Yu5F1WkIRQy/QQlujBYRghEm0b6qOSbdB7fBbpYVu8C0LSyi/b3WnM5jtqRfovPdsPgx hPeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=iSSiGqnlhzGiDLCzSAtleJIHOm3o/jPruEIVdPoSPmM=; b=aiVLeTYVPKLFTBOqHSjYXV8XkOBDXwXj08bzJ54nXnLnnHpHzLpa+0flxTerwd5mhM kzrGIi5u0gEms/GLrxYYiewjeZ30uwXD7Cny99vx/MOodqE5X/AGYi2yJ+QJ5ndelHPd 1v+UK1LemwTFgvdS1bO80DmN73yPqirM9UsTBAaRZfBbCeNRdohz2pbNG0RXuM5A1Laj +rYI0rA8dakH0gs3KR4ipU3rkap1XH4fhxCEEGRCdIrclfI1cQGbUcypVMd8bJKENxXQ gP8KXV2CVJbSXYnzWabC/Q46r6RPsEtZbrHug3T37mB9Xn9xBJmleFCYNF82OBozKJ5q vK9Q== X-Gm-Message-State: AFqh2kovC8Zgjy243aNk5moxIqzqlkf9uFk5Kmbuinbz7ykt230gEtRY 0JQdaJ4ZI4mAQW7BnFCX8VaVA5ITbJ6TMyU//Q== X-Received: from ackerleytng-cloudtop-sg.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:b30]) (user=ackerleytng job=sendgmr) by 2002:a81:bc9:0:b0:471:d0:fcdf with SMTP id 192-20020a810bc9000000b0047100d0fcdfmr1932256ywl.108.1674260206107; Fri, 20 Jan 2023 16:16:46 -0800 (PST) Date: Sat, 21 Jan 2023 00:15:19 +0000 In-Reply-To: <20230121001542.2472357-1-ackerleytng@google.com> Mime-Version: 1.0 References: <20230121001542.2472357-1-ackerleytng@google.com> X-Mailer: git-send-email 2.39.0.246.g2a6d74b583-goog Message-ID: <20230121001542.2472357-9-ackerleytng@google.com> Subject: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry From: Ackerley Tng <ackerleytng@google.com> To: linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, sagis@google.com, erdemaktas@google.com, afranji@google.com, runanwang@google.com, shuah@kernel.org, drjones@redhat.com, maz@kernel.org, bgardon@google.com, jmattson@google.com, dmatlack@google.com, peterx@redhat.com, oupton@google.com, ricarkol@google.com, yang.zhong@intel.com, wei.w.wang@intel.com, xiaoyao.li@intel.com, pgonda@google.com, marcorr@google.com, eesposit@redhat.com, borntraeger@de.ibm.com, eric.auger@redhat.com, wangyanan55@huawei.com, aaronlewis@google.com, vkuznets@redhat.com, pshier@google.com, axelrasmussen@google.com, zhenzhong.duan@intel.com, maciej.szmigiero@oracle.com, like.xu@linux.intel.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Ackerley Tng <ackerleytng@google.com> 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,URIBL_BLOCKED,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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755589154974545845?= X-GMAIL-MSGID: =?utf-8?q?1755589154974545845?= |
Series |
TDX KVM selftests
|
|
Commit Message
Ackerley Tng
Jan. 21, 2023, 12:15 a.m. UTC
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles
assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination
results in a #GP in guests.
Adding this compiler flag will generate an alternate prologue and
epilogue to realign the runtime stack, which makes selftest code
slower and bigger, but this is okay since we do not need selftest code
to be extremely performant.
Similar issue discussed at
https://lore.kernel.org/all/CAGtprH9yKvuaF5yruh3BupQe4BxDGiBQk3ExtY2m39yP-tppsg@mail.gmail.com/
Signed-off-by: Ackerley Tng <ackerleytng@google.com>
---
tools/testing/selftests/kvm/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Sat, Jan 21, 2023, Ackerley Tng wrote: > Some SSE instructions assume a 16-byte aligned stack, and GCC compiles > assuming the stack is aligned: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination > results in a #GP in guests. > > Adding this compiler flag will generate an alternate prologue and > epilogue to realign the runtime stack, which makes selftest code > slower and bigger, but this is okay since we do not need selftest code > to be extremely performant. Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm. We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment. > Similar issue discussed at > https://lore.kernel.org/all/CAGtprH9yKvuaF5yruh3BupQe4BxDGiBQk3ExtY2m39yP-tppsg@mail.gmail.com/ > > Signed-off-by: Ackerley Tng <ackerleytng@google.com> > --- > tools/testing/selftests/kvm/Makefile | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile > index 317927d9c55bd..5f9cc1e6ee67e 100644 > --- a/tools/testing/selftests/kvm/Makefile > +++ b/tools/testing/selftests/kvm/Makefile > @@ -205,7 +205,7 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include > else > LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include > endif > -CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ > +CFLAGS += -mstackrealign -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ > -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ > -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ > -I$(<D) -Iinclude/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \ > -- > 2.39.0.246.g2a6d74b583-goog >
On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson <seanjc@google.com> wrote: > > On Sat, Jan 21, 2023, Ackerley Tng wrote: > > Some SSE instructions assume a 16-byte aligned stack, and GCC compiles > > assuming the stack is aligned: > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination > > results in a #GP in guests. > > > > Adding this compiler flag will generate an alternate prologue and > > epilogue to realign the runtime stack, which makes selftest code > > slower and bigger, but this is okay since we do not need selftest code > > to be extremely performant. > > Huh, I had completely forgotten that this is why SSE is problematic. I ran into > this with the base UPM selftests and just disabled SSE. /facepalm. > > We should figure out exactly what is causing a misaligned stack. As you've noted, > the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), > the starting stack should be page aligned, which means something is causing the > stack to become unaligned at runtime. I'd rather hunt down that something than > paper over it by having the compiler force realignment. Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack. > > > Similar issue discussed at > > https://lore.kernel.org/all/CAGtprH9yKvuaF5yruh3BupQe4BxDGiBQk3ExtY2m39yP-tppsg@mail.gmail.com/ > > > > Signed-off-by: Ackerley Tng <ackerleytng@google.com> > > --- > > tools/testing/selftests/kvm/Makefile | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile > > index 317927d9c55bd..5f9cc1e6ee67e 100644 > > --- a/tools/testing/selftests/kvm/Makefile > > +++ b/tools/testing/selftests/kvm/Makefile > > @@ -205,7 +205,7 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include > > else > > LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include > > endif > > -CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ > > +CFLAGS += -mstackrealign -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ > > -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ > > -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ > > -I$(<D) -Iinclude/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \ > > -- > > 2.39.0.246.g2a6d74b583-goog > >
On 23.01.2023 19:30, Erdem Aktas wrote: > On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson <seanjc@google.com> wrote: >> >> On Sat, Jan 21, 2023, Ackerley Tng wrote: >>> Some SSE instructions assume a 16-byte aligned stack, and GCC compiles >>> assuming the stack is aligned: >>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination >>> results in a #GP in guests. >>> >>> Adding this compiler flag will generate an alternate prologue and >>> epilogue to realign the runtime stack, which makes selftest code >>> slower and bigger, but this is okay since we do not need selftest code >>> to be extremely performant. >> >> Huh, I had completely forgotten that this is why SSE is problematic. I ran into >> this with the base UPM selftests and just disabled SSE. /facepalm. >> >> We should figure out exactly what is causing a misaligned stack. As you've noted, >> the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), >> the starting stack should be page aligned, which means something is causing the >> stack to become unaligned at runtime. I'd rather hunt down that something than >> paper over it by having the compiler force realignment. > > Is not it due to the 32bit execution part of the guest code at boot > time. Any push/pop of 32bit registers might make it a 16-byte > unaligned stack. 32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - see [1] chapter 2.2.2 "The Stack Frame" Thanks, Maciej [1]: https://www.uclibc.org/docs/psABI-i386.pdf
On Mon, Jan 23, 2023, Maciej S. Szmigiero wrote: > On 23.01.2023 19:30, Erdem Aktas wrote: > > On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson <seanjc@google.com> wrote: > > > > > > On Sat, Jan 21, 2023, Ackerley Tng wrote: > > > > Some SSE instructions assume a 16-byte aligned stack, and GCC compiles > > > > assuming the stack is aligned: > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination > > > > results in a #GP in guests. > > > > > > > > Adding this compiler flag will generate an alternate prologue and > > > > epilogue to realign the runtime stack, which makes selftest code > > > > slower and bigger, but this is okay since we do not need selftest code > > > > to be extremely performant. > > > > > > Huh, I had completely forgotten that this is why SSE is problematic. I ran into > > > this with the base UPM selftests and just disabled SSE. /facepalm. > > > > > > We should figure out exactly what is causing a misaligned stack. As you've noted, > > > the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), > > > the starting stack should be page aligned, which means something is causing the > > > stack to become unaligned at runtime. I'd rather hunt down that something than > > > paper over it by having the compiler force realignment. > > > > Is not it due to the 32bit execution part of the guest code at boot > > time. Any push/pop of 32bit registers might make it a 16-byte > > unaligned stack. > > 32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - > see [1] chapter 2.2.2 "The Stack Frame" And this showing up in the non-TDX selftests rules that out as the sole problem; the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code.
On Mon, Jan 23, 2023 at 10:53 AM Sean Christopherson <seanjc@google.com> wrote: > > On Mon, Jan 23, 2023, Maciej S. Szmigiero wrote: > > On 23.01.2023 19:30, Erdem Aktas wrote: > > > On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson <seanjc@google.com> wrote: > > > > > > > > On Sat, Jan 21, 2023, Ackerley Tng wrote: > > > > > Some SSE instructions assume a 16-byte aligned stack, and GCC compiles > > > > > assuming the stack is aligned: > > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination > > > > > results in a #GP in guests. > > > > > > > > > > Adding this compiler flag will generate an alternate prologue and > > > > > epilogue to realign the runtime stack, which makes selftest code > > > > > slower and bigger, but this is okay since we do not need selftest code > > > > > to be extremely performant. > > > > > > > > Huh, I had completely forgotten that this is why SSE is problematic. I ran into > > > > this with the base UPM selftests and just disabled SSE. /facepalm. > > > > > > > > We should figure out exactly what is causing a misaligned stack. As you've noted, > > > > the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), > > > > the starting stack should be page aligned, which means something is causing the > > > > stack to become unaligned at runtime. I'd rather hunt down that something than > > > > paper over it by having the compiler force realignment. > > > > > > Is not it due to the 32bit execution part of the guest code at boot > > > time. Any push/pop of 32bit registers might make it a 16-byte > > > unaligned stack. > > > > 32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - > > see [1] chapter 2.2.2 "The Stack Frame" > > And this showing up in the non-TDX selftests rules that out as the sole problem; > the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code. Thanks Maciej and Sean for the clarification. I was suspecting the hand-coded assembly part that we have for TDX tests but it being happening in the non-TDX selftests disproves it.
On Mon, Jan 23, 2023, Erdem Aktas wrote: > On Mon, Jan 23, 2023 at 10:53 AM Sean Christopherson <seanjc@google.com> wrote: > > > > On Mon, Jan 23, 2023, Maciej S. Szmigiero wrote: > > > On 23.01.2023 19:30, Erdem Aktas wrote: > > > > On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson <seanjc@google.com> wrote: > > > > > > > > > > On Sat, Jan 21, 2023, Ackerley Tng wrote: > > > > > > Some SSE instructions assume a 16-byte aligned stack, and GCC compiles > > > > > > assuming the stack is aligned: > > > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination > > > > > > results in a #GP in guests. > > > > > > > > > > > > Adding this compiler flag will generate an alternate prologue and > > > > > > epilogue to realign the runtime stack, which makes selftest code > > > > > > slower and bigger, but this is okay since we do not need selftest code > > > > > > to be extremely performant. > > > > > > > > > > Huh, I had completely forgotten that this is why SSE is problematic. I ran into > > > > > this with the base UPM selftests and just disabled SSE. /facepalm. > > > > > > > > > > We should figure out exactly what is causing a misaligned stack. As you've noted, > > > > > the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), > > > > > the starting stack should be page aligned, which means something is causing the > > > > > stack to become unaligned at runtime. I'd rather hunt down that something than > > > > > paper over it by having the compiler force realignment. > > > > > > > > Is not it due to the 32bit execution part of the guest code at boot > > > > time. Any push/pop of 32bit registers might make it a 16-byte > > > > unaligned stack. > > > > > > 32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - > > > see [1] chapter 2.2.2 "The Stack Frame" > > > > And this showing up in the non-TDX selftests rules that out as the sole problem; > > the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code. > > Thanks Maciej and Sean for the clarification. I was suspecting the > hand-coded assembly part that we have for TDX tests but it being > happening in the non-TDX selftests disproves it. Not necessarily, it could be both. Goofs in the handcoded assembly and PEBKAC on my end :-)
> On Mon, Jan 23, 2023, Erdem Aktas wrote: > > On Mon, Jan 23, 2023 at 10:53 AM Sean Christopherson > <seanjc@google.com> wrote: > > > > > > On Mon, Jan 23, 2023, Maciej S. Szmigiero wrote: > > > > On 23.01.2023 19:30, Erdem Aktas wrote: > > > > > On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson > <seanjc@google.com> wrote: > > > > > > > > > > > > On Sat, Jan 21, 2023, Ackerley Tng wrote: > > > > > > > Some SSE instructions assume a 16-byte aligned stack, and GCC > compiles > > > > > > > assuming the stack is aligned: > > > > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This > combination > > > > > > > results in a #GP in guests. > > > > > > > > > > > > > > Adding this compiler flag will generate an alternate prologue > and > > > > > > > epilogue to realign the runtime stack, which makes selftest > code > > > > > > > slower and bigger, but this is okay since we do not need > selftest code > > > > > > > to be extremely performant. > > > > > > > > > > > > Huh, I had completely forgotten that this is why SSE is > problematic. I ran into > > > > > > this with the base UPM selftests and just disabled SSE. > /facepalm. > > > > > > > > > > > > We should figure out exactly what is causing a misaligned > stack. As you've noted, > > > > > > the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm > misreading vm_arch_vcpu_add(), > > > > > > the starting stack should be page aligned, which means > something is causing the > > > > > > stack to become unaligned at runtime. I'd rather hunt down > that something than > > > > > > paper over it by having the compiler force realignment. > > > > > > > > > > Is not it due to the 32bit execution part of the guest code at > boot > > > > > time. Any push/pop of 32bit registers might make it a 16-byte > > > > > unaligned stack. > > > > > > > > 32-bit stack needs to be 16-byte aligned, too (at function call > boundaries) - > > > > see [1] chapter 2.2.2 "The Stack Frame" > > > > > > And this showing up in the non-TDX selftests rules that out as the > sole problem; > > > the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code. > > > > Thanks Maciej and Sean for the clarification. I was suspecting the > > hand-coded assembly part that we have for TDX tests but it being > > happening in the non-TDX selftests disproves it. > Not necessarily, it could be both. Goofs in the handcoded assembly and > PEBKAC > on my end :-) I figured it out! GCC assumes that the stack is 16-byte aligned **before** the call instruction. Since call pushes rip to the stack, GCC will compile code assuming that on entrance to the function, the stack is -8 from a 16-byte aligned address. Since for TDs we do a ljmp to guest code, providing a function's address, the stack was not modified by a call instruction pushing rip to the stack, so the stack is 16-byte aligned when the guest code starts running, instead of 16-byte aligned -8 that GCC expects. For VMs, we set rip to a function pointer, and the VM starts running with a 16-byte algined stack too. To fix this, I propose that in vm_arch_vcpu_add(), we align the allocated stack address and then subtract 8 from that: @@ -573,10 +573,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); vcpu_setup(vm, vcpu); + stack_vaddr += (DEFAULT_STACK_PGS * getpagesize()); + stack_vaddr = ALIGN_DOWN(stack_vaddr, 16) - 8; + /* Setup guest general purpose registers */ vcpu_regs_get(vcpu, ®s); regs.rflags = regs.rflags | 0x2; - regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize()); + regs.rsp = stack_vaddr; regs.rip = (unsigned long) guest_code; vcpu_regs_set(vcpu, ®s);
On 15.02.2023 01:50, Ackerley Tng wrote: > >> On Mon, Jan 23, 2023, Erdem Aktas wrote: >> > On Mon, Jan 23, 2023 at 10:53 AM Sean Christopherson <seanjc@google.com> wrote: >> > > >> > > On Mon, Jan 23, 2023, Maciej S. Szmigiero wrote: >> > > > On 23.01.2023 19:30, Erdem Aktas wrote: >> > > > > On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson <seanjc@google.com> wrote: >> > > > > > >> > > > > > On Sat, Jan 21, 2023, Ackerley Tng wrote: >> > > > > > > Some SSE instructions assume a 16-byte aligned stack, and GCC compiles >> > > > > > > assuming the stack is aligned: >> > > > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination >> > > > > > > results in a #GP in guests. >> > > > > > > >> > > > > > > Adding this compiler flag will generate an alternate prologue and >> > > > > > > epilogue to realign the runtime stack, which makes selftest code >> > > > > > > slower and bigger, but this is okay since we do not need selftest code >> > > > > > > to be extremely performant. >> > > > > > >> > > > > > Huh, I had completely forgotten that this is why SSE is problematic. I ran into >> > > > > > this with the base UPM selftests and just disabled SSE. /facepalm. >> > > > > > >> > > > > > We should figure out exactly what is causing a misaligned stack. As you've noted, >> > > > > > the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), >> > > > > > the starting stack should be page aligned, which means something is causing the >> > > > > > stack to become unaligned at runtime. I'd rather hunt down that something than >> > > > > > paper over it by having the compiler force realignment. >> > > > > >> > > > > Is not it due to the 32bit execution part of the guest code at boot >> > > > > time. Any push/pop of 32bit registers might make it a 16-byte >> > > > > unaligned stack. >> > > > >> > > > 32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - >> > > > see [1] chapter 2.2.2 "The Stack Frame" >> > > >> > > And this showing up in the non-TDX selftests rules that out as the sole problem; >> > > the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code. >> > >> > Thanks Maciej and Sean for the clarification. I was suspecting the >> > hand-coded assembly part that we have for TDX tests but it being >> > happening in the non-TDX selftests disproves it. > >> Not necessarily, it could be both. Goofs in the handcoded assembly and PEBKAC >> on my end :-) > > I figured it out! > > GCC assumes that the stack is 16-byte aligned **before** the call > instruction. Since call pushes rip to the stack, GCC will compile code > assuming that on entrance to the function, the stack is -8 from a > 16-byte aligned address. > > Since for TDs we do a ljmp to guest code, providing a function's > address, the stack was not modified by a call instruction pushing rip to > the stack, so the stack is 16-byte aligned when the guest code starts > running, instead of 16-byte aligned -8 that GCC expects. > > For VMs, we set rip to a function pointer, and the VM starts running > with a 16-byte algined stack too. > > To fix this, I propose that in vm_arch_vcpu_add(), we align the > allocated stack address and then subtract 8 from that: > Note that if this code is ever used to launch a vCPU with 32-bit entry point it will need to subtract 4 bytes instead of 8 bytes. I think it would be worthwhile to at least place a comment mentioning this near the stack aligning expression so nobody misses this fact. Thanks, Maciej
On Wed, Feb 15, 2023, Maciej S. Szmigiero wrote: > On 15.02.2023 01:50, Ackerley Tng wrote: > > To fix this, I propose that in vm_arch_vcpu_add(), we align the > > allocated stack address and then subtract 8 from that: > > Note that if this code is ever used to launch a vCPU with 32-bit entry > point it will need to subtract 4 bytes instead of 8 bytes. > > I think it would be worthwhile to at least place a comment mentioning > this near the stack aligning expression so nobody misses this fact. Heh, I've no objection to a comment, though this really is the tip of the iceberg if we want to add 32-bit guest support in selftests.
On Wed, Feb 15, 2023, Ackerley Tng wrote: > I figured it out! > > GCC assumes that the stack is 16-byte aligned **before** the call > instruction. Since call pushes rip to the stack, GCC will compile code > assuming that on entrance to the function, the stack is -8 from a > 16-byte aligned address. > > Since for TDs we do a ljmp to guest code, providing a function's > address, the stack was not modified by a call instruction pushing rip to > the stack, so the stack is 16-byte aligned when the guest code starts > running, instead of 16-byte aligned -8 that GCC expects. > > For VMs, we set rip to a function pointer, and the VM starts running > with a 16-byte algined stack too. > > To fix this, I propose that in vm_arch_vcpu_add(), we align the > allocated stack address and then subtract 8 from that: > > @@ -573,10 +573,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, > uint32_t vcpu_id, > vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); > vcpu_setup(vm, vcpu); > > + stack_vaddr += (DEFAULT_STACK_PGS * getpagesize()); > + stack_vaddr = ALIGN_DOWN(stack_vaddr, 16) - 8; The ALIGN_DOWN should be unnecessary, we've got larger issues if getpagesize() isn't 16-byte aligned and/or if __vm_vaddr_alloc() returns anything but a page-aligned address. Maybe add a TEST_ASSERT() sanity check that stack_vaddr is page-aligned at this point? And in addition to the comment suggested by Maciej, can you also add a comment explaining the -8 adjust? Yeah, someone can go read the changelog, but I think this is worth explicitly documenting in code. Lastly, can you post it as a standalone patch? Many thanks!
> > I figured it out! > > > > GCC assumes that the stack is 16-byte aligned **before** the call > > instruction. Since call pushes rip to the stack, GCC will compile code > > assuming that on entrance to the function, the stack is -8 from a > > 16-byte aligned address. > > > > Since for TDs we do a ljmp to guest code, providing a function's > > address, the stack was not modified by a call instruction pushing rip to > > the stack, so the stack is 16-byte aligned when the guest code starts > > running, instead of 16-byte aligned -8 that GCC expects. > > > > For VMs, we set rip to a function pointer, and the VM starts running > > with a 16-byte algined stack too. > > > > To fix this, I propose that in vm_arch_vcpu_add(), we align the > > allocated stack address and then subtract 8 from that: > > > > @@ -573,10 +573,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm > *vm, > > uint32_t vcpu_id, > > vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); > > vcpu_setup(vm, vcpu); > > > > + stack_vaddr += (DEFAULT_STACK_PGS * getpagesize()); > > + stack_vaddr = ALIGN_DOWN(stack_vaddr, 16) - 8; > The ALIGN_DOWN should be unnecessary, we've got larger issues if > getpagesize() isn't > 16-byte aligned and/or if __vm_vaddr_alloc() returns anything but a > page-aligned > address. Maybe add a TEST_ASSERT() sanity check that stack_vaddr is > page-aligned > at this point? > And in addition to the comment suggested by Maciej, can you also add a > comment > explaining the -8 adjust? Yeah, someone can go read the changelog, but I > think > this is worth explicitly documenting in code. > Lastly, can you post it as a standalone patch? > Many thanks! Thanks Maciej and Sean, I've made the changes you requested and posted it as a standalone patch at https://lore.kernel.org/lkml/32866e5d00174697730d6231d2fb81f6b8d98c8a.1676659352.git.ackerleytng@google.com/
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 317927d9c55bd..5f9cc1e6ee67e 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -205,7 +205,7 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include else LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include endif -CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ +CFLAGS += -mstackrealign -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ -I$(<D) -Iinclude/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \