From patchwork Wed Oct 11 08:31:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Shuai X-Patchwork-Id: 151203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp385189vqb; Wed, 11 Oct 2023 01:34:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzfOHe8Zm2nddcf4Dqft79YKDjkeCEgq48v/y7f+aAviWys3W6x3WmwJDQKHSu+YPPjkly X-Received: by 2002:a17:902:e885:b0:1c0:bf60:ba82 with SMTP id w5-20020a170902e88500b001c0bf60ba82mr23436056plg.5.1697013279876; Wed, 11 Oct 2023 01:34:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697013279; cv=none; d=google.com; s=arc-20160816; b=eqkQO1QkqKhvlVwIWuI/b6n9ck7khS9lyHZHd7753R/nGFm/Nd0ZXtbd6eBuZz2aR9 3CqpKHWcuDnXMV2ABeB/FbD0cMVfIGM0BQCEPrLd4jFQgIqPWXDOoeLyfHQK5HwBk1MY cPHyVUSiUG+urMC2LjJqkb5K2ob0eKuQAXCWYhe70xu53n7ezrwrOzn7x8DuHBSg5uTS EsbsG8bARziuaAdSoVvdbGkqUEqiQ5cfGESszvBdoTz/YbxPO4suujafjcwNC212Ydck cOjbdfWaLE5DXawfwuJ6ZgzcKUhvhnZudwBobvw7WlijaDL8aXLGhsDFk8+1Ezegauvi dttQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=/ZBLF1FrTOp3iwFuTVUZN1BonqR9/05J2/TFDy5UDgg=; fh=DlYutkg/6Wkat5B72kLfp99kToTYZ2djuWDJ1GjZ1/g=; b=M5+IiculcZBehdXO4yLbOxLNm+M0zIjbhS7h7HaeUZWrsaLsQ4NAWv0t9IG4gOWctB AfyY4IH0oXdtDq3u9nhrLADl/mOXTE+BlB3EdH9Wei6pdECD0NqZjpv1m+oN+n1OLnmN gNe9c9v+D96lQZp5XG8SZM5vEFM6oplmhmcKJDnwxRvQyrEzClXvEIYgLlPHbhSqUrzj trvis0bXm3vdNGCY4PswTCI7zqQvJSAHhLCPfyalRLFA5peSU+5JbEKbki+TQ/0ezK2J 34aRpp8fJ/ip2BrjyKknh35U4+1ydGpwUBeA9RcLYBdca2NIiY6i4t9QU/31XK1M8MCY Xueg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id g11-20020a1709026b4b00b001c426636990si13194087plt.637.2023.10.11.01.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 01:34:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id 993548132A68; Wed, 11 Oct 2023 01:34:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233082AbjJKIeQ (ORCPT + 19 others); Wed, 11 Oct 2023 04:34:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230392AbjJKIeD (ORCPT ); Wed, 11 Oct 2023 04:34:03 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4AAB98 for ; Wed, 11 Oct 2023 01:34:01 -0700 (PDT) X-QQ-mid: bizesmtp86t1697013186tw7nlhwm Received: from localhost.localdomain ( [58.240.82.166]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 11 Oct 2023 16:33:04 +0800 (CST) X-QQ-SSF: 01200000000000B0B000000A0000000 X-QQ-FEAT: DpiahrQ8wyJg9GMX+5CGpkVkHTz8Lz+LJrrpYtf7D73/ry6DrR747E+FghQW5 oRfhUKD/daG8ib41PZLKypfiA15MjMNzpNJcN7b9TeoNlyAsjLntA3SiTG/Aegv2eu46FYQ ML+3IzlgjZJzeHFxNOx6fpFv+6OuY2ANQffMIsToUB3sW0RlgKkwD5ufbiVmJeIfJ0wTzBf hXoA8IWPnZ/q/GVVT7ZaXYE/e7769qWdhciPKxZ/5TtYfGSXCHHQgsscy6BCzN19bAlOIcT 4+poDAX0vDzH1UEnYJ+3Ck1UYh8nancQhSqDqGzTN7yqikEH8t577Rrzg5m2FtRiQ4ECeOO jUIVdSkjNfCWRiZwQzGyw9PBJoaI8bDUrbUrkmpBe22KYXSDDppoRtcfqNtAw== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 18166619308426436661 From: Song Shuai To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, conor.dooley@microchip.com, lihuafei1@huawei.com, songshuaishuai@tinylab.org, petr.tesarik.ext@huawei.com, duwe@suse.de, ribalda@chromium.org, akpm@linux-foundation.org, keescook@chromium.org, hi@alyssa.is, heiko@sntech.de, alexghiti@rivosinc.com, schwab@suse.de Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org Subject: [PATCH 1/2] riscv: kexec: Cleanup riscv_kexec_relocate Date: Wed, 11 Oct 2023 16:31:31 +0800 Message-Id: <20231011083132.2428720-2-songshuaishuai@tinylab.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231011083132.2428720-1-songshuaishuai@tinylab.org> References: <20231011083132.2428720-1-songshuaishuai@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz4a-0 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Wed, 11 Oct 2023 01:34:36 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779447397211382321 X-GMAIL-MSGID: 1779447397211382321 For readability and simplicity, cleanup the riscv_kexec_relocate code: - Re-sort the first 4 `mv` instructions against `riscv_kexec_method()` - Eliminate registers for debugging (s9,s10,s11) and storing const-value (s5,s6) - Replace `jalr` with `jr` for no-link jump I tested this on Qemu virt machine and works as it was. Signed-off-by: Song Shuai --- arch/riscv/kernel/kexec_relocate.S | 54 +++++++++++++----------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/arch/riscv/kernel/kexec_relocate.S b/arch/riscv/kernel/kexec_relocate.S index 059c5e216ae7..ece5591abc4e 100644 --- a/arch/riscv/kernel/kexec_relocate.S +++ b/arch/riscv/kernel/kexec_relocate.S @@ -17,27 +17,17 @@ SYM_CODE_START(riscv_kexec_relocate) * s1: (const) Phys address to jump to after relocation * s2: (const) Phys address of the FDT image * s3: (const) The hartid of the current hart - * s4: Pointer to the destination address for the relocation - * s5: (const) Number of words per page - * s6: (const) 1, used for subtraction - * s7: (const) kernel_map.va_pa_offset, used when switching MMU off - * s8: (const) Physical address of the main loop - * s9: (debug) indirection page counter - * s10: (debug) entry counter - * s11: (debug) copied words counter + * s4: (const) kernel_map.va_pa_offset, used when switching MMU off + * s5: Pointer to the destination address for the relocation + * s6: (const) Physical address of the main loop */ mv s0, a0 mv s1, a1 mv s2, a2 mv s3, a3 - mv s4, zero - li s5, (PAGE_SIZE / RISCV_SZPTR) - li s6, 1 - mv s7, a4 - mv s8, zero - mv s9, zero - mv s10, zero - mv s11, zero + mv s4, a4 + mv s5, zero + mv s6, zero /* Disable / cleanup interrupts */ csrw CSR_SIE, zero @@ -52,21 +42,27 @@ SYM_CODE_START(riscv_kexec_relocate) * the start of the loop below so that we jump there in * any case. */ - la s8, 1f - sub s8, s8, s7 - csrw CSR_STVEC, s8 + la s6, 1f + sub s6, s6, s4 + csrw CSR_STVEC, s6 + + /* + * With C-extension, here we get 42 Bytes and the next + * .balign directive would pad zeros here up to 44 Bytes. + * So manually put a nop here to avoid zeros padding. + */ + nop /* Process entries in a loop */ -.align 2 +.balign 4 1: - addi s10, s10, 1 REG_L t0, 0(s0) /* t0 = *image->entry */ addi s0, s0, RISCV_SZPTR /* image->entry++ */ /* IND_DESTINATION entry ? -> save destination address */ andi t1, t0, 0x1 beqz t1, 2f - andi s4, t0, ~0x1 + andi s5, t0, ~0x1 j 1b 2: @@ -74,9 +70,8 @@ SYM_CODE_START(riscv_kexec_relocate) andi t1, t0, 0x2 beqz t1, 2f andi s0, t0, ~0x2 - addi s9, s9, 1 csrw CSR_SATP, zero - jalr zero, s8, 0 + jr s6 2: /* IND_DONE entry ? -> jump to done label */ @@ -92,14 +87,13 @@ SYM_CODE_START(riscv_kexec_relocate) andi t1, t0, 0x8 beqz t1, 1b /* Unknown entry type, ignore it */ andi t0, t0, ~0x8 - mv t3, s5 /* i = num words per page */ + li t3, (PAGE_SIZE / RISCV_SZPTR) /* i = num words per page */ 3: /* copy loop */ REG_L t1, (t0) /* t1 = *src_ptr */ - REG_S t1, (s4) /* *dst_ptr = *src_ptr */ + REG_S t1, (s5) /* *dst_ptr = *src_ptr */ addi t0, t0, RISCV_SZPTR /* stc_ptr++ */ - addi s4, s4, RISCV_SZPTR /* dst_ptr++ */ - sub t3, t3, s6 /* i-- */ - addi s11, s11, 1 /* c++ */ + addi s5, s5, RISCV_SZPTR /* dst_ptr++ */ + addi t3, t3, -0x1 /* i-- */ beqz t3, 1b /* copy done ? */ j 3b @@ -146,7 +140,7 @@ SYM_CODE_START(riscv_kexec_relocate) */ fence.i - jalr zero, a2, 0 + jr a2 SYM_CODE_END(riscv_kexec_relocate) riscv_kexec_relocate_end: From patchwork Wed Oct 11 08:31:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Shuai X-Patchwork-Id: 151207 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp385735vqb; Wed, 11 Oct 2023 01:36:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHrlDLqu85MtRVP6S9i8Lm+GlGsKbePXBLUvQxsOSbttdWYi/NEvHPXXT1s5znRto5HriyT X-Received: by 2002:a17:90b:1496:b0:27c:f88f:11a5 with SMTP id js22-20020a17090b149600b0027cf88f11a5mr2785594pjb.2.1697013362630; Wed, 11 Oct 2023 01:36:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697013362; cv=none; d=google.com; s=arc-20160816; b=HMjmNYH1Ic3QJl+m2NSjF23aNSZ/LmRghiybosHON0QBQKKpZ7/0xNelYBNcWTYB8p 9p7UdFrjz9WUs1xOg76XNpDkAqedsPWagWXDerSdXdW80z6sT/GNPIkS1lwGyo5HVFEB DP3BixMzHoWtM9pd8jmQDpG42pFWXSluo2NZYYfVoCY7a4odOEief/SXpQZ2KakK8GJB Yqy+0BVgSdAWIBPgDn2R22e8zSF3Fm6FA65ktbxlj4tppUN75Mm089KN6r3mXQyCmPuC ySUkRcCM5ADCcLkQq2bnGTBgJS0e719hCvx9Kzz58b/zNmBWhPhiEKaghBOvmxGFpPpS YDpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=e12JwzCEw+EC6YVE/g3DVUHyLDjdVNVsMT22TZvcnAo=; fh=DlYutkg/6Wkat5B72kLfp99kToTYZ2djuWDJ1GjZ1/g=; b=Ko7/wlVuBCK1zxl97TR1OAEOcWyvMDHerUHIrGbC7peEtbuVbZ//+xlo9py/EivWqN /rZ0OgFFpB6SGZcB+LrJJxbvP0H2uPvulolS5BSM4jL5Ampcm0ie2xicjHFpLU7u/Kkc 2QPWp0ANi2SQ2DN9nMzQ86Q3LRpOjw4eurWb7pwTsvK/G3FMn23tJGCNew7k5mCn1UBY hpQ4FZYEcbNTdcuaQwR/ml1ZKF4rfnQPY+gE+2C7vpHBsg9g5TSd1H8GWZ5r19BmCeIm gcY0/bcdJoJ+3vrND9CTQ4+/c0fuWi2061wCvm4RgIulSB9JEOlFIkEeWbN4jQ9rfmaf +aXQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id nu17-20020a17090b1b1100b00277751629e4si10788008pjb.121.2023.10.11.01.36.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 01:36:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 0AD2E8093D44; Wed, 11 Oct 2023 01:35:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230413AbjJKIfn (ORCPT + 19 others); Wed, 11 Oct 2023 04:35:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229957AbjJKIfm (ORCPT ); Wed, 11 Oct 2023 04:35:42 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2042D93 for ; Wed, 11 Oct 2023 01:35:38 -0700 (PDT) X-QQ-mid: bizesmtp71t1697013251tdcqqafe Received: from localhost.localdomain ( [58.240.82.166]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 11 Oct 2023 16:34:09 +0800 (CST) X-QQ-SSF: 01200000000000B0B000000A0000000 X-QQ-FEAT: FwowAM4HOqAVKxvoHOg+FFtkzyhqthXdQT4dyvq8RM0ldk9sx097/DTTVtTym zDrumLi3wKzCr8HeAtYLjJAO0L0+aIbGeOa415C5O9jPe7FMnKkoUVvHuiA9aKuYc+9Q1aM 9cJ/snUP8/szBAD7Bjs3eaWzuqEX+BnI7N+51e2OV79N001z7CTBNMWbJqV+bykxn7ic2Q7 RRHq54a8wsMzE6aZmUluVyilqnAsv/dg/rJUBkGm4OBESBJw3+EzrA5i0dRI+0hK+/Dazvv F6v2yTOubJMkx7Sf6/j1qLEQN3zoG54wpS3sggddPKE3seMGmKHz2Dx4zAl25vrsVzJFfqU bs1DClkTvFRJjSLK+rmaGFnDXt4enoq7xPY3bzlojOIb1ZqDxL5jep7gSIHFg== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 10839386821577447536 From: Song Shuai To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, conor.dooley@microchip.com, lihuafei1@huawei.com, songshuaishuai@tinylab.org, petr.tesarik.ext@huawei.com, duwe@suse.de, ribalda@chromium.org, akpm@linux-foundation.org, keescook@chromium.org, hi@alyssa.is, heiko@sntech.de, alexghiti@rivosinc.com, schwab@suse.de Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org Subject: [PATCH 2/2] riscv: kexec: Remove -fPIE for PURGATORY_CFLAGS Date: Wed, 11 Oct 2023 16:31:32 +0800 Message-Id: <20231011083132.2428720-3-songshuaishuai@tinylab.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231011083132.2428720-1-songshuaishuai@tinylab.org> References: <20231011083132.2428720-1-songshuaishuai@tinylab.org> MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz4a-0 X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 11 Oct 2023 01:35:57 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779447483757329953 X-GMAIL-MSGID: 1779447483757329953 With CONFIG_RELOCATABLE enabled, KBUILD_CFLAGS had a -fPIE option and then the purgatory/string.o was built to reference _ctype symbol via R_RISCV_GOT_HI20 relocations which can't be handled by purgatory. As a consequence, the kernel failed kexec_load_file() with: [ 880.386562] kexec_image: The entry point of kernel at 0x80200000 [ 880.388650] kexec_image: Unknown rela relocation: 20 [ 880.389173] kexec_image: Error loading purgatory ret=-8 So remove the -fPIE option for PURGATORY_CFLAGS to generate R_RISCV_PCREL_HI20 relocations type making puragtory work as it was. Fixes: 39b33072941f ("riscv: Introduce CONFIG_RELOCATABLE") Signed-off-by: Song Shuai --- arch/riscv/purgatory/Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile index 9e6476719abb..982fc8e77108 100644 --- a/arch/riscv/purgatory/Makefile +++ b/arch/riscv/purgatory/Makefile @@ -81,6 +81,10 @@ ifdef CONFIG_CFI_CLANG PURGATORY_CFLAGS_REMOVE += $(CC_FLAGS_CFI) endif +ifdef CONFIG_RELOCATABLE +PURGATORY_CFLAGS_REMOVE += -fPIE +endif + CFLAGS_REMOVE_purgatory.o += $(PURGATORY_CFLAGS_REMOVE) CFLAGS_purgatory.o += $(PURGATORY_CFLAGS)