Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp526940rdg; Thu, 12 Oct 2023 12:32:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+jUDRktyMcn7oFnbmQ5Jm3IwjKHYjphfzP0Qu+a4YOtmXe90wdTZ3Q8iqzjKiEwuiOg8b X-Received: by 2002:a05:6870:588c:b0:1b4:4a2e:33f1 with SMTP id be12-20020a056870588c00b001b44a2e33f1mr29535632oab.29.1697139168484; Thu, 12 Oct 2023 12:32:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697139168; cv=none; d=google.com; s=arc-20160816; b=jceqgYnfRwj4w9QvP9aNbaccefW3U+9WU/4J9pWC4Bwv12/cqZoitFJ6wE9Ch0XEpW Q2uz3I5Ub5BI1evoFEBUTTVLjS4UosQdG9pWhaRIaI3rWdUeByoP/qVkcS/OTS8p+DMJ 2T8OABVmPPPdpU0pFnT/TYA01D5m121LrqeL/ue2zA0Ol/yB4R3DU3i+7GulNt+L8OHd NbwwXJc85dGqwJjPYJ1naeFrz4KcaUhsXNBUR5oOBEe5u8DFkfFT7kx73jfV+Rh+MlNk WAyuUpvgW1o7m//tgWF1wyzunBL6tomea+9ChGo8zmicw6Wc7yXq/vZoVJh+dlRmC/E5 VqfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/dy7MRuQ4nZ6XvzCQf/QE00pJQ5sHlinCaHof97IeUc=; fh=pn+Rqew71LKPylIcCrzfhmFqbupdWkgDEvVr2JdqKAU=; b=jd/DnbvJrnRTXHXMTx9ijiW2BYJJEZihMI3D0hCbN5AytnhD69WeiEFm13SgjuItTU Wu833Mzg+ZSxakrFR3BqbJJDb42AyBC8X/ThCEEULMrlxNsXtu0LpgHnm0EvtGN5B1YT U7df3n0numu3ABHxVelLfOtqbNxgQ9sVhzc9hk+9x9CHRD6krHw89+HunMIh4H1HRgN3 mhvKFbrnf+4yw1mIwvlyuD87fW+RrLtl3XAOILftUATch1nDbkvojyqU0GumzezK6+bW ohjIkcsUqKkzzuL59E+lq0kU/aYc26eaQYPRKjdS9Kq3kKMBBrNp8eJ9GRPtp01UJ3uG HD+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u7EapoPG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id j6-20020a056a00174600b0068e3950c705si15869747pfc.24.2023.10.12.12.32.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 12:32:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u7EapoPG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 84F8C8270055; Thu, 12 Oct 2023 12:32:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442372AbjJLTco (ORCPT + 99 others); Thu, 12 Oct 2023 15:32:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442098AbjJLTci (ORCPT ); Thu, 12 Oct 2023 15:32:38 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB24E4 for ; Thu, 12 Oct 2023 12:32:35 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F03BC433C8; Thu, 12 Oct 2023 19:32:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697139155; bh=d8ayu7kl8vqNo7NKawNbDKeJKZ0xNLEf2Mmx5D5tlHA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u7EapoPG2ZvLutGfmEetupl7EiQpZfP69OEEGMailIC9EGAyMrT7kFssMCox0ZsS1 sJeYDQZGWF9GcId/l5GG7os0U6Z7ltz3h4Jum/C/frTSsxC+OQBIpxFALPrlMvWKeX EuYPNgezzcBYDKLgvBDC0UnUw5ixb2X0YST6d4ZpF5B/75zoDPFgI9YB/NCt1bBO5x cLsWPFgpqcaoNKbWp9kYHgWALyBIjb/8d5GkFV21/sJ4c8YkMETK9mEBY15XCkMNm8 75iJAv7iNSn95fAix87U9E1Uu2D17FpUlfdz86uC/o/pb4qHVQQBYIZw13ObqQvIap W7ZhD0X/ee/kA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 3D0E7CE096F; Thu, 12 Oct 2023 12:32:35 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: gwml@vger.gnuweeb.org, kernel-team@meta.com, w@lwt.eu, Ammar Faizi , Zhangjin Wu , Nicholas Rosenberg , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Alviro Iskandar Setiawan , Willy Tarreau Subject: [PATCH nolibc 01/19] tools/nolibc: i386: Fix a stack misalign bug on _start Date: Thu, 12 Oct 2023 12:32:15 -0700 Message-Id: <20231012193233.207857-1-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 12 Oct 2023 12:32:47 -0700 (PDT) From: Ammar Faizi The ABI mandates that the %esp register must be a multiple of 16 when executing a 'call' instruction. Commit 2ab446336b17 ("tools/nolibc: i386: shrink _start with _start_c") simplified the _start function, but it didn't take care of the %esp alignment, causing SIGSEGV on SSE and AVX programs that use aligned move instruction (e.g., movdqa, movaps, and vmovdqa). The 'and $-16, %esp' aligns the %esp at a multiple of 16. Then 'push %eax' will subtract the %esp by 4; thus, it breaks the 16-byte alignment. Make sure the %esp is correctly aligned after the push by subtracting 12 before the push. Extra: Add 'add $12, %esp' before the 'and $-16, %esp' to avoid over-estimating for particular cases as suggested by Willy. A test program to validate the %esp alignment on _start can be found at: https://lore.kernel.org/lkml/ZOoindMFj1UKqo+s@biznet-home.integral.gnuweeb.org Cc: Zhangjin Wu Fixes: 2ab446336b17aad362c6decee29b4efd83a01979 ("tools/nolibc: i386: shrink _start with _start_c") Reported-by: Nicholas Rosenberg Acked-by: Thomas Weißschuh Signed-off-by: Ammar Faizi Reviewed-by: Alviro Iskandar Setiawan Signed-off-by: Willy Tarreau Signed-off-by: Thomas Weißschuh --- tools/include/nolibc/arch-i386.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i386.h index 64415b9fac77..28c26a00a762 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -167,7 +167,9 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_ __asm__ volatile ( "xor %ebp, %ebp\n" /* zero the stack frame */ "mov %esp, %eax\n" /* save stack pointer to %eax, as arg1 of _start_c */ - "and $-16, %esp\n" /* last pushed argument must be 16-byte aligned */ + "add $12, %esp\n" /* avoid over-estimating after the 'and' & 'sub' below */ + "and $-16, %esp\n" /* the %esp must be 16-byte aligned on 'call' */ + "sub $12, %esp\n" /* sub 12 to keep it aligned after the push %eax */ "push %eax\n" /* push arg1 on stack to support plain stack modes too */ "call _start_c\n" /* transfer to c runtime */ "hlt\n" /* ensure it does not return */ -- 2.40.1