Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp5870459rwl; Mon, 9 Jan 2023 00:33:16 -0800 (PST) X-Google-Smtp-Source: AMrXdXvwl/Iw8bFs/ypYSgnJg8rRkJk6rL6Qy8/erUT/bDfb67jbnzkx0yEdFzVnG58PX1laKJ68 X-Received: by 2002:a17:90a:d58a:b0:219:7d75:de7b with SMTP id v10-20020a17090ad58a00b002197d75de7bmr64992039pju.35.1673253196684; Mon, 09 Jan 2023 00:33:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673253196; cv=none; d=google.com; s=arc-20160816; b=ZggjGDTSp6icraxy1uFNn1P3GJCgLYLqW5N9T4g+/fFVm1avssCqFBfXf/SRAwaleu hwG+u2iNevhYjodx1AmH6tnJf6DINjXNT4Z+2ccgMVbdWg9N1R6izKm2uI/pCibxAH+N /KyaSafAPC5+XbYRW+1RefYjGqnAJXqU2B+hgK+nX0YauWjr6GGYhy+SmWzWnqPaquSo 7NbnkQW71Xbzc3kbewjBjmfGpisrr1eWhszp+px4KH+zWbYSo9n7qi0JqvPzmNtc1bM7 7lDfXubiFetEp2T213DYy7Rd+SVTpr06NNEWPLkyfhkjVdhKx+6TJQvIGRp4MdsPSDwV mbdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=qMvOXmjYVrX9jQomSokd6YO83sksTD8p/f61cr4ob04=; b=Z0oWkde7Qc5JoULxr4K3PGoGTEYEH0Ek89PRJU1sbyxf3uZp7AZ+9TkKOxb5eHHbsa ldkBXGEzMgpqmwFiAZ6psgBVcNIde/PZs/z6/JBHLE5u6j/jXU374CPBEM/pEQ3vuuKA CiFN6HE16W/DqSxB5zhpEA2hGSL2M2GC1TzbtUb6suwhLJT9SpUjSc5WkRnKJqquGzeQ /zwqJKaxBKsIn25qHFBHKcw0WwUEwBLbo8B7bhp8vpQK4NhV28J2suwO1b7XjVn5fUNH 57y/FHnnfWLci/BAKixJX8IhKoOA4IK6v1sx8DkwRDRz+oST6HMOLEMQCJpYc+4qsBDG Hs3g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w186-20020a6382c3000000b0047701a23907si9270028pgd.252.2023.01.09.00.33.09; Mon, 09 Jan 2023 00:33:16 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233367AbjAIHz5 (ORCPT + 54 others); Mon, 9 Jan 2023 02:55:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235659AbjAIHz3 (ORCPT ); Mon, 9 Jan 2023 02:55:29 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A844FDF3B for ; Sun, 8 Jan 2023 23:55:20 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 3097sot6026017; Mon, 9 Jan 2023 08:54:50 +0100 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 3/6] tools/nolibc: restore mips branch ordering in the _start block Date: Mon, 9 Jan 2023 08:54:39 +0100 Message-Id: <20230109075442.25963-4-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230109075442.25963-1-w@1wt.eu> References: <20230109075442.25963-1-w@1wt.eu> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, 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 Depending on the compiler used and the optimization options, the sbrk() test was crashing, both on real hardware (mips-24kc) and in qemu. One such example is kernel.org toolchain in version 11.3 optimizing at -Os. Inspecting the sys_brk() call shows the following code: 0040047c : 40047c: 24020fcd li v0,4045 400480: 27bdffe0 addiu sp,sp,-32 400484: 0000000c syscall 400488: 27bd0020 addiu sp,sp,32 40048c: 10e00001 beqz a3,400494 400490: 00021023 negu v0,v0 400494: 03e00008 jr ra It is obviously wrong, the "negu" instruction is placed in beqz's delayed slot, and worse, there's no nop nor instruction after the return, so the next function's first instruction (addiu sip,sip,-32) will also be executed as part of the delayed slot that follows the return. This is caused by the ".set noreorder" directive in the _start block, that applies to the whole program. The compiler emits code without the delayed slots and relies on the compiler to swap instructions when this option is not set. Removing the option would require to change the startup code in a way that wouldn't make it look like the resulting code, which would not be easy to debug. Instead let's just save the default ordering before changing it, and restore it at the end of the _start block. Now the code is correct: 0040047c : 40047c: 24020fcd li v0,4045 400480: 27bdffe0 addiu sp,sp,-32 400484: 0000000c syscall 400488: 10e00002 beqz a3,400494 40048c: 27bd0020 addiu sp,sp,32 400490: 00021023 negu v0,v0 400494: 03e00008 jr ra 400498: 00000000 nop Fixes: 66b6f755ad45 ("rcutorture: Import a copy of nolibc") #5.0 Signed-off-by: Willy Tarreau --- tools/include/nolibc/arch-mips.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h index 5fc5b8029bff..7380093ba9e7 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -192,6 +192,7 @@ struct sys_stat_struct { __asm__ (".section .text\n" ".weak __start\n" ".set nomips16\n" + ".set push\n" ".set noreorder\n" ".option pic0\n" ".ent __start\n" @@ -210,6 +211,7 @@ __asm__ (".section .text\n" "li $v0, 4001\n" // NR_exit == 4001 "syscall\n" ".end __start\n" + ".set pop\n" ""); #endif // _NOLIBC_ARCH_MIPS_H -- 2.35.3