From patchwork Tue Mar 12 08:24: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: 1910885 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=0UNf/n0e; 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=Xkdtp0da; 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 4Tv6FD5x26z1yWy for ; Tue, 12 Mar 2024 19:26:56 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6gGmhq6+R1SPbeKQzWutZqTfLIxzjWlhZ9+UcZ+dDd8=; b=0UNf/n0eSDorrA LDhTqHsyHDaUMaVm+BUFWC1jTbxTF0zPxJlpR5cb81QY8OS3Cir3MTcFzqiJUYP3WSW5aHo+EiWWc LxViyxoUMMx7RTb6bkSF95ZKzyrw3VFhqXehUF9MOqgQPpZX2cEsZQgPtTbFjUYVrvEp90gsbNf0o moaIuWRZiiEnKLtW0kiVKl9cjuYJ6dZl25pX3sPi+AV3zvRkLeiy8ANFI/ZpXubKn1Ue4+B3LTyCv JhCeP8CKgL58nGKNAl1b0JDUvzC0jAgKOU4Jh/jpS/CRgyKeTpja41IxB9vkgua4ETQsKw2Bq6Zwu C9FLCNkLq267YXDmeCAQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rjxTD-00000004sZX-0Dlp; Tue, 12 Mar 2024 08:26:47 +0000 Received: from m16.mail.126.com ([220.197.31.9]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rjxT9-00000004sXn-45UF for opensbi@lists.infradead.org; Tue, 12 Mar 2024 08:26:46 +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=HMOiG 0aIjj7eNFp8iYQJeGhjCNi+9ywEol5o2quhwAE=; b=Xkdtp0daCCUbl8Jw3Ib+L FQUl12xPb+FICmYotw6vvN7rsSqXnhXSdhERr0EiGudInj3qguJoag8NjvMBgHVB XgS6kR+ijzdk2xPsbq20owLRWf6Bi6e8+3oDVVg9R3Y+qELyju3A5lZsKOLx5jGP PE2XErruGvFN7KMAJIHfw0= Received: from T490.lan (unknown [112.83.183.140]) by gzga-smtp-mta-g0-3 (Coremail) with SMTP id _____wD3X4uDEfBlPHKwAA--.22788S3; Tue, 12 Mar 2024 16:25:41 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Subject: [PATCH v3 1/2] firmware: remove copy-base relocation Date: Tue, 12 Mar 2024 16:24:59 +0800 Message-ID: <20240312082504.499847-2-wxjstz@126.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240312082504.499847-1-wxjstz@126.com> References: <20240312082504.499847-1-wxjstz@126.com> MIME-Version: 1.0 X-CM-TRANSID: _____wD3X4uDEfBlPHKwAA--.22788S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3Wr1UZryrAw4rtr1fWFyUJrb_yoW3Ar15pa 1fG3W7tFs5Gw1rXr97u3W8ur4jgws8XrZrurW29FyrA34fZrn3Za48tFZ5Xa4kJ34xAw45 uFy2kr17Aay5AF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRnmRbUUUUU= X-Originating-IP: [112.83.183.140] X-CM-SenderInfo: pz0m23b26rjloofrz/xtbBdQifOmSdL0nx+QABsO X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240312_012644_378905_EC96CA84 X-CRM114-Status: GOOD ( 14.66 ) 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 Reviewed-by: Samuel Holland Tested-by: Samuel Holland --- Makefile | 8 +++- README.md | 3 +- docs/firmware/fw.md | 6 --- firmware/fw_base.S | 98 ++ [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [220.197.31.9 listed in list.dnswl.org] -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_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.1 DKIM_VALID Message has at least one valid DKIM or DK signature 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: , Cc: Xiang W , jrtc27@jrtc27.com 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 Reviewed-by: Anup Patel --- Makefile | 8 +++- README.md | 3 +- docs/firmware/fw.md | 6 --- firmware/fw_base.S | 98 ++------------------------------------------- firmware/objects.mk | 11 ----- 5 files changed, 11 insertions(+), 115 deletions(-) diff --git a/Makefile b/Makefile index 680c19a..50f634e 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 += -fPIE # 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..cbae73a 100644 --- a/README.md +++ b/README.md @@ -276,8 +276,7 @@ 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. +recommended that this combination be avoided. 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