Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2360308rdb; Sun, 3 Dec 2023 13:38:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IHcMQ0wvKiqgAoUEIvYPG5dcEhKRq6AWw0OJi1kNa6HxhpiRaPJCVheIpvVu+0RV/vkV6y9 X-Received: by 2002:a05:6a21:6da9:b0:18f:97c:8a1c with SMTP id wl41-20020a056a216da900b0018f097c8a1cmr4256432pzb.71.1701639492974; Sun, 03 Dec 2023 13:38:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701639492; cv=none; d=google.com; s=arc-20160816; b=NSXAkXy8GH70ulP33Xm4Qbz73IG9OX5NT9RbqK+JtYYvP0NBuVCqmWtvYJ/gqXo+kI TnPYjfAxP4btyN3KMbDWVZZB9RzC1Wgn5mX83TFPg0XaPj12NPCsaYTMeuhOk7uss++G +B4wm4zaq2xQjpWXZllf51j/6xvzDcwQGLNO5kk8oQ9mmbMjgtuAEt/5br9QjVqmLSfF B+aMZ632L56YPioD+8nIFLUb9+b/kPU4LXfjC9NuWHsZ8Hv1N6mPGbUSoX8U+mYd0Bk1 hmZRqZo4HChU0F0q0ryzukki5jzH7RMmYZLuhKlUUQbFR9FIkAWLblyd54h25/jeU+zd UyBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=79IpPb2XlUYzeD4/NtahSkQN28wIW0zFEU0h5B0xobs=; fh=qFPSbSEpDfbw/BY5tMTpSGTFHsbwX3bkhHyVTKhqtbE=; b=VPeoM+u0YRcHZeYQwXuPctfR8cYl64/sHClUtCfGgVOStY8f0l0OgDE1D39gAzVqwe WUaQq6VWYCxR2LENGhkEMCW8G1t/ur+YDafv6vss+Zwi3OWncEzP7XbnZtcXbpiMSxNk Xax9mHt5jxZNbuOLxDy3t3AKOw9ivWMIs9YvOgYt8BmhLXbxDOIgX7nTvPqIUz9wPfWT zJWlDonSAmj0Zrkqn5iBs2hmOLi+uXIo5rQ5LY35Y6swsE5kN6c/R0HeCaYzV8MvH2Xv Nsmdfsfk9JhdDjHMQe4mm/HQeKmaqK0/Tr0GtJ537N1QROxe0bUvNxpFEp+bwy3Vfw/w q+Dg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id d8-20020a63ed08000000b005c61d17e6aasi6749582pgi.122.2023.12.03.13.38.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Dec 2023 13:38:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 836FD80739E6; Sun, 3 Dec 2023 13:38:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233945AbjLCVhr (ORCPT + 99 others); Sun, 3 Dec 2023 16:37:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjLCVhq (ORCPT ); Sun, 3 Dec 2023 16:37:46 -0500 Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5DE2BC2 for ; Sun, 3 Dec 2023 13:37:52 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3B3LbnMF021009; Sun, 3 Dec 2023 22:37:49 +0100 Date: Sun, 3 Dec 2023 22:37:49 +0100 From: Willy Tarreau To: Alexey Dobriyan Cc: Thomas =?iso-8859-1?Q?Wei=DFschuh?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH] nolibc: optimise _start() on x86_64 Message-ID: <20231203213749.GA20917@1wt.eu> References: <38183c29-9b7f-4960-8702-d71ce816cf80@p183> <20231202132359.GA15040@1wt.eu> <4d12b353-7417-4776-90de-0f04462cc221@p183> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4d12b353-7417-4776-90de-0f04462cc221@p183> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email 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 (morse.vger.email [0.0.0.0]); Sun, 03 Dec 2023 13:38:10 -0800 (PST) On Sun, Dec 03, 2023 at 03:00:48PM +0300, Alexey Dobriyan wrote: > On Sat, Dec 02, 2023 at 02:23:59PM +0100, Willy Tarreau wrote: > > Hi Alexey, > > > > On Sat, Dec 02, 2023 at 03:45:13PM +0300, Alexey Dobriyan wrote: > > > Just jump into _start_c, it is not going to return anyway. > > > > Thanks, but what's upper in the stack there ? > > argc > > (gdb) break _start > (gdb) run > > (gdb) x/20gx $sp > 0x7fffffffdae0: 0x0000000000000004 0x00007fffffffdf33 > 0x7fffffffdaf0: 0x00007fffffffdf49 0x00007fffffffdf4b > 0x7fffffffdb00: 0x00007fffffffdf4d 0x0000000000000000 > 0x7fffffffdb10: 0x00007fffffffdf4f 0x00007fffffffdf70 > 0x7fffffffdb20: 0x00007fffffffdf80 0x00007fffffffdfce > > (gdb) x/s 0x00007fffffffdf33 > 0x7fffffffdf33: "/home/ad/s-test/a.out" > > > I'm trying to make sure > > that if _start_c returns we don't get a random behavior. > > Yes, it should segfault executing from very small address. > I tested with > > .intel_syntax noprefix > .globl _start > _start: > ret > mov eax, 231 > xor edi, edi > syscall Well, this could possibly be acceptable then but the ABI also says that we need rsp to be 16-byte aligned before the call, so it's supposed to be 8 on top of this, so this would actually require more code to maintain this guarantee, since a sub rsp,8 is longer than just the hlt we're saving. > > If we get a > > systematic crash (e.g. 0 always there) that's fine, what would be > > annoying would be random infinite loops etc. In the psABI description > > (table 3.9) I'm seeing "undefined" before argc, which I don't find > > much appealing. > > > > > Signed-off-by: Alexey Dobriyan > > > --- > > > > > > Also, kernel clears all registers before starting process, > > > I'm not sure why > > > > > > xor ebp, ebp > > > > > > was added. > > > > Hmmm psABI says: > > > > Only the registers listed below have specied values at process entry: > > > > %rbp The content of this register is unspecied at process initialization > > time, but the user code should mark the deepest stack frame by setting > > the frame pointer to zero. > > > > %rsp The stack pointer holds the address of the byte with lowest address > > which is part of the stack. It is guaranteed to be 16-byte aligned at > > process entry. > > > > %rdx a function pointer that the application should register with atexit (BA_OS). > > > > Thus apparently it's documented as being our job to clear it :-/ > > I meant, ELF loader clears all registers except rsp and aligns the stack to 16 bytes. > There were problems with stack aligning, but registers, I think, were always zeroed. But there's a strong difference between what's observed and what's specified. If you get the x86_64 ABI spec to reflect this, it becomes the standard and we can rely on it. Otherwise the standard remains what is documented, and what is implemented may change while remaining within the specs above. Willy