From nobody Thu Dec 18 00:45:45 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E6E8CDB46E for ; Thu, 12 Oct 2023 19:32:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442301AbjJLTcq (ORCPT ); Thu, 12 Oct 2023 15:32:46 -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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.gnuwe= eb.org Cc: Zhangjin Wu Fixes: 2ab446336b17aad362c6decee29b4efd83a01979 ("tools/nolibc: i386: shrin= k _start with _start_c") Reported-by: Nicholas Rosenberg Acked-by: Thomas Wei=C3=9Fschuh Signed-off-by: Ammar Faizi Reviewed-by: Alviro Iskandar Setiawan Signed-off-by: Willy Tarreau Signed-off-by: Thomas Wei=C3=9Fschuh --- 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-i= 386.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", "omi= t-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 _sta= rt_c */ - "and $-16, %esp\n" /* last pushed argument must be 16-byte aligne= d */ + "add $12, %esp\n" /* avoid over-estimating after the 'and' & 'su= b' 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 %e= ax */ "push %eax\n" /* push arg1 on stack to support plain stack m= odes too */ "call _start_c\n" /* transfer to c runtime = */ "hlt\n" /* ensure it does not return = */ --=20 2.40.1