From patchwork Mon Mar 11 16:03:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiang W X-Patchwork-Id: 1910513 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=faTCeGGB; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=qdE9PRaK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tthc23l6mz1yWy for ; Tue, 12 Mar 2024 03:11:46 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=hwEyXnQ/V+TYVodCQGWOD/6II5jybx3/xUfFl1IuJ38=; b=faTCeGGBjWfU5/ qDlUgPRiOFECEBR8hhSuD26daWWvRCJUNjoeD2/ghc7QQZ96klfIAucCmWJhaKaAfvBKP9jRiWZ3L 4zPDuuRJQouUcMWBnG9LdGryvF1eLBDgBrNWOn/T05/LGvH+LvUvrEXQYGCKdQEld/aQ37zdx19Ap Dt/3pyBz1pegAJNRE5lMHJOTLY/XDgjgOInzOsPHEXnXp/sTZdp0NvK5oBwpAmOd0QyHzLpMZsaui OsIUWvXM3k5LjEIHfj2zFAVlWD3WgESMNF2RnPbtKvHzcsmPgQDhOzSukulg7pdSiFDrYyrgTwaro NEU8dnCFM/MOEBQGWRhA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rjiFV-00000002Ee1-483F; Mon, 11 Mar 2024 16:11:37 +0000 Received: from m16.mail.126.com ([117.135.210.8]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rjiFM-00000002EVB-491S for opensbi@lists.infradead.org; Mon, 11 Mar 2024 16:11:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=WQPJa AMW5D5G5A1X0701SQAi2hPvi1SOl4MpWbhf9JY=; b=qdE9PRaKWmz31Lwep4wFJ 11cAsnzGuoOtQPA1+m/AQUlhZ/pChjEeV2AuCGoCMN2KJLC3GbQspvCee7I3zD+B t1cj5Rgjg0fVRTzlYwloFmZ1m8mPmzWdAI6T9yvBKAbYT+huqpL6pZo5NKbWKWe5 18Du1wuOnaV1ssxCHX+vl4= Received: from T490.lan (unknown [112.83.183.140]) by gzga-smtp-mta-g0-3 (Coremail) with SMTP id _____wDX_+QlLe9laPmWAA--.21587S2; Tue, 12 Mar 2024 00:11:18 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Cc: Xiang W Subject: [PATCH v2] firmware: remove copy-base relocation Date: Tue, 12 Mar 2024 00:03:59 +0800 Message-ID: <20240311161114.201904-1-wxjstz@126.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-CM-TRANSID: _____wDX_+QlLe9laPmWAA--.21587S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxtF4kCw1DJFWfurWxJr17GFg_yoW3AF4Upw 4fG3W7tFs5Gw1rX3s7ua18ur4jgws8XrZrurW29FyrA34fZrn3Za48tFZ5Xa4kJ34xAw45 uFy2kr17Aa45AF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pR6T5LUUUUU= X-Originating-IP: [112.83.183.140] X-CM-SenderInfo: pz0m23b26rjloofrz/xtbBcweeOmSQAn1n1wAAsr X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240311_091129_723203_D2E58692 X-CRM114-Status: GOOD ( 14.87 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Remove copy-base relocations that are no longer needed. Signed-off-by: Xiang W --- v2 changes: - Split from "[PATCH v5 0/8] Improvements to fw_base.S" - rebase to the latest master branch Makefile | 8 +++- README.md | 5 --- docs/firmware/fw.md | 6 --- firmware/fw_base.S | 98 ++ firmware/objects.mk | 11 ----- 5 files changed, 10 insertions(+), [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [117.135.210.8 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [wxjstz(at)126.com] -0.0 T_SCC_BODY_TEXT_LINE No description available. X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Remove copy-base relocations that are no longer needed. Signed-off-by: Xiang W Reviewed-by: Samuel Holland Tested-by: Samuel Holland --- v2 changes: - Split from "[PATCH v5 0/8] Improvements to fw_base.S" - rebase to the latest master branch Makefile | 8 +++- README.md | 5 --- docs/firmware/fw.md | 6 --- firmware/fw_base.S | 98 ++------------------------------------------- firmware/objects.mk | 11 ----- 5 files changed, 10 insertions(+), 118 deletions(-) diff --git a/Makefile b/Makefile index 680c19a..d8cffa6 100644 --- a/Makefile +++ b/Makefile @@ -179,6 +179,10 @@ CC_SUPPORT_STRICT_ALIGN := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib # Check whether the assembler and the compiler support the Zicsr and Zifencei extensions CC_SUPPORT_ZICSR_ZIFENCEI := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)imafd_zicsr_zifencei -x c /dev/null -o /dev/null 2>&1 | grep "zicsr\|zifencei" > /dev/null && echo n || echo y) +ifneq ($(OPENSBI_LD_PIE),y) +$(error Your linker does not support creating PIEs, opensbi requires this.) +endif + # Build Info: # OPENSBI_BUILD_TIME_STAMP -- the compilation time stamp # OPENSBI_BUILD_COMPILER_VERSION -- the compiler version info @@ -356,7 +360,7 @@ CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL) CFLAGS += $(RELAX_FLAG) CFLAGS += $(GENFLAGS) CFLAGS += $(platform-cflags-y) -CFLAGS += -fno-pie -no-pie +CFLAGS += -fPIE -pie CFLAGS += $(firmware-cflags-y) CPPFLAGS += $(GENFLAGS) @@ -365,6 +369,7 @@ CPPFLAGS += $(firmware-cppflags-y) ASFLAGS = -g -Wall -nostdlib ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls +ASFLAGS += -fpic # Optionally supported flags ifeq ($(CC_SUPPORT_SAVE_RESTORE),y) ASFLAGS += -mno-save-restore @@ -391,6 +396,7 @@ ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y) ELFFLAGS += -Wl,--exclude-libs,ALL endif ELFFLAGS += -Wl,--build-id=none +ELFFLAGS += -Wl,--no-dynamic-linker -Wl,-pie ELFFLAGS += $(platform-ldflags-y) ELFFLAGS += $(firmware-ldflags-y) diff --git a/README.md b/README.md index 73de8ea..7a24801 100644 --- a/README.md +++ b/README.md @@ -274,11 +274,6 @@ make CC=riscv64-linux-gnu-gcc LLVM=1 These variables must be passed for all the make invocations described in this document. -NOTE: Using Clang with a `riscv*-linux-gnu` GNU binutils linker has been seen -to produce broken binaries with missing relocations; it is therefore currently -recommended that this combination be avoided or *FW_PIC=n* be used to disable -building OpenSBI as a position-independent binary. - Building with timestamp and compiler info ----------------------------------------- diff --git a/docs/firmware/fw.md b/docs/firmware/fw.md index 38351c8..2f4deb5 100644 --- a/docs/firmware/fw.md +++ b/docs/firmware/fw.md @@ -69,12 +69,6 @@ parameters: argument by the prior booting stage. * **FW_FDT_PADDING** - Optional zero bytes padding to the embedded flattened device tree binary file specified by **FW_FDT_PATH** option. -* **FW_PIC** - "FW_PIC=y" generates position independent executable firmware - images. OpenSBI can run at arbitrary address with appropriate alignment. - Therefore, the original relocation mechanism ("FW_PIC=n") will be skipped. - In other words, OpenSBI will directly run at the load address without any - code movement. This option requires a toolchain with PIE support, and it - is on by default. Additionally, each firmware type as a set of type specific configuration parameters. Detailed information for each firmware type can be found in the diff --git a/firmware/fw_base.S b/firmware/fw_base.S index 126b067..6013db3 100644 --- a/firmware/fw_base.S +++ b/firmware/fw_base.S @@ -15,8 +15,7 @@ #include #define BOOT_STATUS_LOTTERY_DONE 1 -#define BOOT_STATUS_RELOCATE_DONE 2 -#define BOOT_STATUS_BOOT_HART_DONE 3 +#define BOOT_STATUS_BOOT_HART_DONE 2 .macro MOV_3R __d0, __s0, __d1, __s1, __d2, __s2 add \__d0, \__s0, zero @@ -32,17 +31,6 @@ add \__d4, \__s4, zero .endm -/* - * If __start_reg <= __check_reg and __check_reg < __end_reg then - * jump to __pass - */ -.macro BRANGE __start_reg, __end_reg, __check_reg, __jump_lable - blt \__check_reg, \__start_reg, 999f - bge \__check_reg, \__end_reg, 999f - j \__jump_lable -999: -.endm - .section .entry, "ax", %progbits .align 3 .globl _start @@ -56,15 +44,14 @@ _start: li a7, -1 beq a6, a7, _try_lottery /* Jump to relocation wait loop if we are not boot hart */ - bne a0, a6, _wait_relocate_copy_done + bne a0, a6, _wait_for_boot_hart _try_lottery: /* Jump to relocation wait loop if we don't get relocation lottery */ lla a6, _boot_status li a7, BOOT_STATUS_LOTTERY_DONE amoswap.w a6, a7, (a6) - bnez a6, _wait_relocate_copy_done + bnez a6, _wait_for_boot_hart -#ifdef FW_PIC /* relocate the global table content */ li t0, FW_TEXT_START /* link start */ lla t1, _fw_start /* load start */ @@ -85,86 +72,7 @@ _try_lottery: 3: addi t0, t0, (REGBYTES * 3) blt t0, t1, 2b - j _relocate_done -_wait_relocate_copy_done: - j _wait_for_boot_hart -#else - /* Relocate if load address != link address */ -_relocate: - li t0, FW_TEXT_START /* link start */ - lla t2, _fw_start /* load start */ - lla t3, _fw_reloc_end /* load end */ - sub t6, t2, t0 /* load offset */ - sub t1, t3, t6 /* link end */ - beq t0, t2, _relocate_done - lla t4, _relocate_done - sub t4, t4, t6 - blt t2, t0, _relocate_copy_to_upper -_relocate_copy_to_lower: - ble t1, t2, _relocate_copy_to_lower_loop - lla t3, _boot_status - BRANGE t2, t1, t3, _start_hang - lla t3, _relocate - lla t5, _relocate_done - BRANGE t2, t1, t3, _start_hang - BRANGE t2, t1, t5, _start_hang - BRANGE t3, t5, t2, _start_hang -_relocate_copy_to_lower_loop: - REG_L t3, 0(t2) - REG_S t3, 0(t0) - add t0, t0, __SIZEOF_POINTER__ - add t2, t2, __SIZEOF_POINTER__ - blt t0, t1, _relocate_copy_to_lower_loop - jr t4 -_relocate_copy_to_upper: - ble t3, t0, _relocate_copy_to_upper_loop - lla t2, _boot_status - BRANGE t0, t3, t2, _start_hang - lla t2, _relocate - lla t5, _relocate_done - BRANGE t0, t3, t2, _start_hang - BRANGE t0, t3, t5, _start_hang - BRANGE t2, t5, t0, _start_hang -_relocate_copy_to_upper_loop: - add t3, t3, -__SIZEOF_POINTER__ - add t1, t1, -__SIZEOF_POINTER__ - REG_L t2, 0(t3) - REG_S t2, 0(t1) - blt t0, t1, _relocate_copy_to_upper_loop - jr t4 -_wait_relocate_copy_done: - lla t0, _fw_start - li t1, FW_TEXT_START - beq t0, t1, _wait_for_boot_hart - lla t2, _boot_status - lla t3, _wait_for_boot_hart - sub t3, t3, t0 - add t3, t3, t1 -1: - /* waitting for relocate copy done (_boot_status == 1) */ - li t4, BOOT_STATUS_RELOCATE_DONE - REG_L t5, 0(t2) - /* Reduce the bus traffic so that boot hart may proceed faster */ - nop - nop - nop - bgt t4, t5, 1b - jr t3 -#endif _relocate_done: - - /* - * Mark relocate copy done - * Use _boot_status copy relative to the load address - */ - lla t0, _boot_status -#ifndef FW_PIC - add t0, t0, t6 -#endif - li t1, BOOT_STATUS_RELOCATE_DONE - REG_S t1, 0(t0) - fence rw, rw - /* At this point we are running from link address */ /* Reset all registers except ra, a0, a1, a2, a3 and a4 for boot HART */ diff --git a/firmware/objects.mk b/firmware/objects.mk index 3ae0a28..e6b364b 100644 --- a/firmware/objects.mk +++ b/firmware/objects.mk @@ -13,17 +13,6 @@ firmware-cflags-y += firmware-asflags-y += firmware-ldflags-y += -ifndef FW_PIC -FW_PIC := $(OPENSBI_LD_PIE) -endif - -ifeq ($(FW_PIC),y) -firmware-genflags-y += -DFW_PIC -firmware-asflags-y += -fpic -firmware-cflags-y += -fPIE -pie -firmware-ldflags-y += -Wl,--no-dynamic-linker -Wl,-pie -endif - ifdef FW_TEXT_START firmware-genflags-y += -DFW_TEXT_START=$(FW_TEXT_START) endif