Skip to contentSkip to conversation

Support position independent execution

PATCH New Vincent Chen 3 participants 8 messages Mar 2, 2021 – Mar 5, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Support position independent execution

This patch set enables OpenSBI to support position-independent execution so that one OpenSBI generic firmware can run on multiple platforms, even if they have different memory base addresses. However, the PIC code will decrease a bit of performance. The 2nd and 3rd patches are used to reduce this impact of the PIC.

The performance loss of PIC comes from two additional operations. One is from the symbol relocation in the beginning stage, and another one is from the GOT reference (auipc + load GOT) in the OpenSBI lifetime. The former only affects the boot time, but the latter will affect the performance of the OpenSBI payload such as the Linux kernel. After analyzing the fw_payload.elf of the generic platform, 27 symbols use the GOT reference method to get their address. Fortunately, 17 of these symbols are accessed once during the initialized step. Only the remaining 10 symbols may be called repeatedly during the lifetime, which are
1. hartid_to_domain_table
2. hartid_to_scratch_table
3. sbi_hart_expected_trap
4. sbi_tlb_local_hfence_vvma
5. sbi_tlb_local_hfence_gvma
6. sbi_tlb_local_sfence_vma_asid
7. sbi_tlb_local_fence_i
8. sbi_tlb_local_hfence_gvma
9. sbi_tlb_local_hfence_vvma
10. sbi_tlb_local_sfence_vma
From these 10 symbols, I think the IPI handler may be one of the critical and commonly used paths affected by the PIC mode. Therefore, I create two kernel modules, One keeps issuing the REMOTE_FENCE_I request and the other one keeps issuing REMOTE_SFENCE_VMA request. These two modules are executed on the unleashed board to measure the consumed time of the sbi_tlb_request(). I found the PIC causes a 1.6% and 4.1% performance loss in addressing REMOTE_FENCE_I and REMOTE_SFENCE_VMA, respectively. In addition, to observe the impact of PIC mode in the general usage cases, I use SPEC2006 to benchmark performance. However, I could not observe any performance impact from the results. Based on the above two results, I think the performance impact of the PIC may not be obvious in general usage. Just in case, this patch still creates an FW_PIC option for users to configure the build options.

Vincent Chen (3):
firmware: Support position independent execution
firmware: fw_base: Use lla to access the global symbols defined in
fw_base.S
firware: Use lla to access all global symbols

 Makefile                |  10 +++++
 firmware/fw_base.S      | 116 ++++++++++++++++++++++++++++++++++++------------
 firmware/fw_base.ldS    |  13 ++++++
 firmware/fw_dynamic.S   |  18 ++++----
 firmware/fw_jump.S      |  12 ++++-
 firmware/fw_payload.S   |   8 +++-
 include/sbi/riscv_elf.h |   9 ++++
 7 files changed, 147 insertions(+), 39 deletions(-)
 create mode 100644 include/sbi/riscv_elf.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 firmware: Support position independent execution

Enable OpenSBI to support position independent execution. Because the position independent code will cause an additional GOT reference when accessing the global variables, it will reduce performance a bit. Therefore, the position independent execution is disabled by default. Users can through specifying "FW_PIC=y" on the make command to enable this feature.

In theory, after enabling position-independent execution, the OpenSBI can run at arbitrary address with appropriate alignment. Therefore, the original relocation mechanism will be skipped. In other words, OpenSBI will directly run at the load address without any code movement.

---
 Makefile                | 10 +++++++
 firmware/fw_base.S      | 70 +++++++++++++++++++++++++++++++++++++++++++++----
 firmware/fw_base.ldS    | 13 +++++++++
 firmware/fw_jump.S      | 10 +++++++
 firmware/fw_payload.S   |  6 +++++
 include/sbi/riscv_elf.h |  9 +++++++
 6 files changed, 113 insertions(+), 5 deletions(-)
 create mode 100644 include/sbi/riscv_elf.h
Makefile +10 -0
index d6f097d..0bebdf3 100644
@@ -202,6 +202,9 @@ endif
 GENFLAGS	+=	$(libsbiutils-genflags-y)
 GENFLAGS	+=	$(platform-genflags-y)
 GENFLAGS	+=	$(firmware-genflags-y)
+ifeq ($(FW_PIC),y)
+GENFLAGS	+=	-DFW_PIC
+endif
 
 CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-strict-aliasing -O2
 CFLAGS		+=	-fno-omit-frame-pointer -fno-optimize-sibling-calls
@@ -211,7 +214,11 @@ CFLAGS		+=	-mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
 CFLAGS		+=	$(GENFLAGS)
 CFLAGS		+=	$(platform-cflags-y)
 CFLAGS		+=	$(firmware-cflags-y)
+ifeq ($(FW_PIC),y)
+CFLAGS		+=	-fPIE -pie
+else
 CFLAGS		+=	-fno-pie -no-pie
+endif
 
 CPPFLAGS	+=	$(GENFLAGS)
 CPPFLAGS	+=	$(platform-cppflags-y)
@@ -231,6 +238,9 @@ ARFLAGS		=	rcs
 ELFFLAGS	+=	-Wl,--build-id=none -N -static-libgcc -lgcc
 ELFFLAGS	+=	$(platform-ldflags-y)
 ELFFLAGS	+=	$(firmware-ldflags-y)
+ifeq ($(FW_PIC),y)
+ELFFLAGS	+=	-Wl,--no-dynamic-linker
+endif
 
 MERGEFLAGS	+=	-r
 MERGEFLAGS	+=	-b elf$(PLATFORM_RISCV_XLEN)-littleriscv
firmware/fw_base.S +65 -5

131 lines not on this page · raw message

firmware/fw_base.ldS +13 -0
index 0ac75f2..3904959 100644
@@ -61,6 +61,19 @@
 		PROVIDE(_data_end = .);
 	}
 
+	.dynsym : {
+	        PROVIDE(__dyn_sym_start = .);
+	        *(.dynsym)
+	        PROVIDE(__dyn_sym_end = .);
+	}
+
+	.rela.dyn : {
+	        PROVIDE(__rel_dyn_start = .);
+	        *(.rela*)
+		. = ALIGN(8);
+	        PROVIDE(__rel_dyn_end = .);
+	}
+
 	. = ALIGN(0x1000); /* Ensure next section is page aligned */
 
 	.bss :
firmware/fw_jump.S +10 -0
index 8553f8c..3cce3f7 100644
@@ -46,6 +46,11 @@ fw_save_info:
 fw_next_arg1:
 #ifdef FW_JUMP_FDT_ADDR
 	li	a0, FW_JUMP_FDT_ADDR
+#ifdef FW_PIC
+	la	a1, _runtime_offset
+	REG_L	a1, (a1)
+	add	a0, a0, a1
+#endif
 #else
 	add	a0, a1, zero
 #endif
@@ -61,6 +66,11 @@ fw_next_arg1:
 fw_next_addr:
 	la	a0, _jump_addr
 	REG_L	a0, (a0)
+#ifdef FW_PIC
+	la	a1, _runtime_offset
+	REG_L	a1, (a1)
+	add	a0, a0, a1
+#endif
 	ret
 
 	.section .entry, "ax", %progbits
firmware/fw_payload.S +6 -0
index 1ef121e..18b51d0 100644
@@ -46,6 +46,12 @@ fw_save_info:
 fw_next_arg1:
 #ifdef FW_PAYLOAD_FDT_ADDR
 	li	a0, FW_PAYLOAD_FDT_ADDR
+#ifdef FW_PIC
+	la      a1, _runtime_offset
+	REG_L   a1, (a1)
+	add     a0, a0, a1
+#endif
+
 #else
 	add	a0, a1, zero
 #endif
include/sbi/riscv_elf.h new file +9 -0
new file mode 100644
index 0000000..cf211ac
@@ -0,0 +1,9 @@
+#include <sbi/riscv_asm.h>
+
+#define R_RISCV_32		1
+#define R_RISCV_64		2
+#define R_RISCV_RELATIVE	3
+
+#define RELOC_TYPE		__REG_SEL(R_RISCV_64, R_RISCV_32)
+#define SYM_INDEX		__REG_SEL(0x20,	0x8)
+#define SYM_SIZE		__REG_SEL(0x18,0x10)
Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/3 firmware: Support position independent execution

Quoted 17 lines · Vincent Chen

Enable OpenSBI to support position independent execution. Because the position independent code will cause an additional GOT reference when accessing the global variables, it will reduce performance a bit. Therefore, the position independent execution is disabled by default. Users can through specifying "FW_PIC=y" on the make command to enable this feature.

In theory, after enabling position-independent execution, the OpenSBI can run at arbitrary address with appropriate alignment. Therefore, the original relocation mechanism will be skipped. In other words, OpenSBI will directly run at the load address without any code movement.

---
index ab33e11..8f8e5dc 100644

firmware/fw_base.S line 47

Quoted 3 lines
firmware/fw_base.S +3 -0
+#ifdef FW_PIC
+	.option pic
+#endif

This should be implied if you use the right flags when assembling.

Jess

#3 lore raw

PATCH 1/3 firmware: Support position independent execution

Quoted 25 lines · Jessica Clarke
Quoted 17 lines · Vincent Chen

Enable OpenSBI to support position independent execution. Because the position independent code will cause an additional GOT reference when accessing the global variables, it will reduce performance a bit. Therefore, the position independent execution is disabled by default. Users can through specifying "FW_PIC=y" on the make command to enable this feature.

In theory, after enabling position-independent execution, the OpenSBI can run at arbitrary address with appropriate alignment. Therefore, the original relocation mechanism will be skipped. In other words, OpenSBI will directly run at the load address without any code movement.

---
index ab33e11..8f8e5dc 100644

firmware/fw_base.S line 47

Quoted 3 lines
firmware/fw_base.S +3 -0
+#ifdef FW_PIC
+     .option pic
+#endif

This should be implied if you use the right flags when assembling.

Quoted 1 line

Jess

You are right. The ".option pic" directive is unneeded if I add "-fpic" option to the ASFLAGS.
I will modify it in my next version patch. Thank you very much for
this suggestion.

#4 lore raw

PATCH 2/3 firmware: fw_base: Use lla to access the global symbols defined in fw_base.S

In reply to Vincent Chen

In fw_base.S, no matter where the symbols are defined, it always uses "la" to obtain the symbol address. In fact, if the access symbols are defined in fw_base.S, we also can use "lla" to get their address instead of "la". When fw_base.S is compiled as a PIC object, using "lla" can allow the assembler to use the PC-relative method to get the symbol address instead of GOT reference, thereby avoiding performance loss.

---
 firmware/fw_base.S | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
firmware/fw_base.S +10 -10
index 8f8e5dc..fe4af45 100644
@@ -199,7 +199,7 @@ _relocate_done:
 	 * Mark relocate copy done
 	 * Use _boot_status copy relative to the load address
 	 */
-	la	t0, _boot_status
+	lla	t0, _boot_status
 #ifndef FW_PIC
 	la	t1, _link_start
 	REG_L	t1, 0(t1)
@@ -227,7 +227,7 @@ _bss_zero:
 	blt	s4, s5, _bss_zero
 
 	/* Setup temporary trap handler */
-	la	s4, _start_hang
+	lla	s4, _start_hang
 	csrw	CSR_MTVEC, s4
 
 	/* Setup temporary stack */
@@ -311,16 +311,16 @@ _scratch_init:
 	REG_S	a0, SBI_SCRATCH_NEXT_MODE_OFFSET(tp)
 	MOV_3R	a0, s0, a1, s1, a2, s2
 	/* Store warm_boot address in scratch space */
-	la	a4, _start_warm
+	lla	a4, _start_warm
 	REG_S	a4, SBI_SCRATCH_WARMBOOT_ADDR_OFFSET(tp)
 	/* Store platform address in scratch space */
 	la	a4, platform
 	REG_S	a4, SBI_SCRATCH_PLATFORM_ADDR_OFFSET(tp)
 	/* Store hartid-to-scratch function address in scratch space */
-	la	a4, _hartid_to_scratch
+	lla	a4, _hartid_to_scratch
 	REG_S	a4, SBI_SCRATCH_HARTID_TO_SCRATCH_OFFSET(tp)
 	/* Store trap-exit function address in scratch space */
-	la	a4, _trap_exit
+	lla	a4, _trap_exit
 	REG_S	a4, SBI_SCRATCH_TRAP_EXIT_OFFSET(tp)
 	/* Clear tmp0 in scratch space */
 	REG_S	zero, SBI_SCRATCH_TMP0_OFFSET(tp)
@@ -401,7 +401,7 @@ _fdt_reloc_done:
 
 	/* mark boot hart done */
 	li	t0, BOOT_STATUS_BOOT_HART_DONE
-	la	t1, _boot_status
+	lla	t1, _boot_status
 	REG_S	t0, 0(t1)
 	fence	rw, rw
 	j	_start_warm
@@ -409,7 +409,7 @@ _fdt_reloc_done:
 	/* waiting for boot hart to be done (_boot_status == 2) */
 _wait_for_boot_hart:
 	li	t0, BOOT_STATUS_BOOT_HART_DONE
-	la	t1, _boot_status
+	lla	t1, _boot_status
 	REG_L	t1, 0(t1)
 	/* Reduce the bus traffic so that boot hart may proceed faster */
 	nop
@@ -473,13 +473,13 @@ _start_warm:
 	add	sp, tp, zero
 
 	/* Setup trap handler */
-	la	a4, _trap_handler
+	lla	a4, _trap_handler
 #if __riscv_xlen == 32
 	csrr	a5, CSR_MISA
 	srli	a5, a5, ('H' - 'A')
 	andi	a5, a5, 0x1
 	beq	a5, zero, _skip_trap_handler_rv32_hyp
-	la	a4, _trap_handler_rv32_hyp
+	lla	a4, _trap_handler_rv32_hyp
 _skip_trap_handler_rv32_hyp:
 #endif
 	csrw	CSR_MTVEC, a4
@@ -490,7 +490,7 @@ _skip_trap_handler_rv32_hyp:
 	srli	a5, a5, ('H' - 'A')
 	andi	a5, a5, 0x1
 	beq	a5, zero, _skip_trap_exit_rv32_hyp
-	la	a4, _trap_exit_rv32_hyp
+	lla	a4, _trap_exit_rv32_hyp
 	csrr	a5, CSR_MSCRATCH
 	REG_S	a4, SBI_SCRATCH_TRAP_EXIT_OFFSET(a5)
 _skip_trap_exit_rv32_hyp:
Signature · 1 line
2.7.4
#5 lore raw

PATCH 3/3 firware: Use lla to access all global symbols

In reply to Vincent Chen

When OpenSBI is compiled as fPIE mode, the assembler will translate "la" to GOT reference pattern. It will cause to cost an additional load instruction when obtaining the symbol address. However, if the symbol locates within the positive or negative 2GB region, we can use "lla" instead of "la" to avoid unneeded GOT references. This patch assumes that the OpenSBI image excluding the payload does not exceed 2GB. Based on this assumption, all "la" instructions are replaced by "lla" to avoid performance degradation when compiling as fPIE mode.

---
 firmware/fw_base.S    | 26 +++++++++++++-------------
 firmware/fw_dynamic.S | 18 +++++++++---------
 firmware/fw_jump.S    |  6 +++---
 firmware/fw_payload.S |  4 ++--
 4 files changed, 27 insertions(+), 27 deletions(-)
firmware/fw_base.S +13 -13
index fe4af45..c159dfc 100644
@@ -219,8 +219,8 @@ _relocate_done:
 	call	_reset_regs
 
 	/* Zero-out BSS */
-	la	s4, _bss_start
-	la	s5, _bss_end
+	lla	s4, _bss_start
+	lla	s5, _bss_end
 _bss_zero:
 	REG_S	zero, (s4)
 	add	s4, s4, __SIZEOF_POINTER__
@@ -231,7 +231,7 @@ _bss_zero:
 	csrw	CSR_MTVEC, s4
 
 	/* Setup temporary stack */
-	la	s4, _fw_end
+	lla	s4, _fw_end
 	li	s5, (SBI_SCRATCH_SIZE * 2)
 	add	sp, s4, s5
 
@@ -242,7 +242,7 @@ _bss_zero:
 
 #ifdef FW_FDT_PATH
 	/* Override previous arg1 */
-	la	a1, fw_fdt_bin
+	lla	a1, fw_fdt_bin
 #endif
 
 	/*
@@ -260,7 +260,7 @@ _bss_zero:
 	 * s7 -> HART Count
 	 * s8 -> HART Stack Size
 	 */
-	la	a4, platform
+	lla	a4, platform
 #if __riscv_xlen == 64
 	lwu	s7, SBI_PLATFORM_HART_COUNT_OFFSET(a4)
 	lwu	s8, SBI_PLATFORM_HART_STACK_SIZE_OFFSET(a4)
@@ -270,7 +270,7 @@ _bss_zero:
 #endif
 
 	/* Setup scratch space for all the HARTs*/
-	la	tp, _fw_end
+	lla	tp, _fw_end
 	mul	a5, s7, s8
 	add	tp, tp, a5
 	/* Keep a copy of tp */
@@ -288,8 +288,8 @@ _scratch_init:
 
 	/* Initialize scratch space */
 	/* Store fw_start and fw_size in scratch space */
-	la	a4, _fw_start
-	la	a5, _fw_end
+	lla	a4, _fw_start
+	lla	a5, _fw_end
 	mul	t0, s7, s8
 	add	a5, a5, t0
 	sub	a5, a5, a4
@@ -314,7 +314,7 @@ _scratch_init:
 	lla	a4, _start_warm
 	REG_S	a4, SBI_SCRATCH_WARMBOOT_ADDR_OFFSET(tp)
 	/* Store platform address in scratch space */
-	la	a4, platform
+	lla	a4, platform
 	REG_S	a4, SBI_SCRATCH_PLATFORM_ADDR_OFFSET(tp)
 	/* Store hartid-to-scratch function address in scratch space */
 	lla	a4, _hartid_to_scratch
@@ -427,7 +427,7 @@ _start_warm:
 	csrw	CSR_MIP, zero
 
 	/* Find HART count and HART stack size */
-	la	a4, platform
+	lla	a4, platform
 #if __riscv_xlen == 64
 	lwu	s7, SBI_PLATFORM_HART_COUNT_OFFSET(a4)
 	lwu	s8, SBI_PLATFORM_HART_STACK_SIZE_OFFSET(a4)
@@ -458,7 +458,7 @@ _start_warm:
 3:	bge	s6, s7, _start_hang
 
 	/* Find the scratch space based on HART index */
-	la	tp, _fw_end
+	lla	tp, _fw_end
 	mul	a5, s7, s8
 	add	tp, tp, a5
 	mul	a5, s8, s6
@@ -528,7 +528,7 @@ _hartid_to_scratch:
 	 * t1 -> HART Stack End
 	 * t2 -> Temporary
 	 */
-	la	t2, platform
+	lla	t2, platform
 #if __riscv_xlen == 64
 	lwu	t0, SBI_PLATFORM_HART_STACK_SIZE_OFFSET(t2)
 	lwu	t2, SBI_PLATFORM_HART_COUNT_OFFSET(t2)
@@ -538,7 +538,7 @@ _hartid_to_scratch:
 #endif
 	sub	t2, t2, a1
 	mul	t2, t2, t0
-	la	t1, _fw_end
+	lla	t1, _fw_end
 	add	t1, t1, t2
 	li	t2, SBI_SCRATCH_SIZE
 	sub	a0, t1, t2
firmware/fw_dynamic.S +9 -9
index 8b56947..0705e63 100644
@@ -54,7 +54,7 @@ fw_boot_hart:
 	 */
 fw_save_info:
 	/* Save next arg1 in 'a1' */
-	la	a4, _dynamic_next_arg1
+	lla	a4, _dynamic_next_arg1
 	REG_S	a1, (a4)
 
 	/* Sanity checks */
@@ -66,13 +66,13 @@ fw_save_info:
 	bgt	a3, a4, _bad_dynamic_info
 
 	/* Save version == 0x1 fields */
-	la	a4, _dynamic_next_addr
+	lla	a4, _dynamic_next_addr
 	REG_L	a3, FW_DYNAMIC_INFO_NEXT_ADDR_OFFSET(a2)
 	REG_S	a3, (a4)
-	la	a4, _dynamic_next_mode
+	lla	a4, _dynamic_next_mode
 	REG_L	a3, FW_DYNAMIC_INFO_NEXT_MODE_OFFSET(a2)
 	REG_S	a3, (a4)
-	la	a4, _dynamic_options
+	lla	a4, _dynamic_options
 	REG_L	a3, FW_DYNAMIC_INFO_OPTIONS_OFFSET(a2)
 	REG_S	a3, (a4)
 
@@ -80,7 +80,7 @@ fw_save_info:
 	li	a4, 0x2
 	REG_L	a3, FW_DYNAMIC_INFO_VERSION_OFFSET(a2)
 	blt	a3, a4, 2f
-	la	a4, _dynamic_boot_hart
+	lla	a4, _dynamic_boot_hart
 	REG_L	a3, FW_DYNAMIC_INFO_BOOT_HART_OFFSET(a2)
 	REG_S	a3, (a4)
 2:
@@ -96,7 +96,7 @@ fw_save_info:
 	 * The next arg1 should be returned in 'a0'.
 	 */
 fw_next_arg1:
-	la	a0, _dynamic_next_arg1
+	lla	a0, _dynamic_next_arg1
 	REG_L	a0, (a0)
 	ret
 
@@ -108,7 +108,7 @@ fw_next_arg1:
 	 * The next address should be returned in 'a0'.
 	 */
 fw_next_addr:
-	la	a0, _dynamic_next_addr
+	lla	a0, _dynamic_next_addr
 	REG_L	a0, (a0)
 	ret
 
@@ -120,7 +120,7 @@ fw_next_addr:
 	 * The next address should be returned in 'a0'
 	 */
 fw_next_mode:
-	la	a0, _dynamic_next_mode
+	lla	a0, _dynamic_next_mode
 	REG_L	a0, (a0)
 	ret
 
@@ -133,7 +133,7 @@ fw_next_mode:
 	 * The next address should be returned in 'a0'.
 	 */
 fw_options:
-	la	a0, _dynamic_options
+	lla	a0, _dynamic_options
 	REG_L	a0, (a0)
 	ret
 
firmware/fw_jump.S +3 -3
index 3cce3f7..4fb3e0c 100644
@@ -47,7 +47,7 @@ fw_next_arg1:
 #ifdef FW_JUMP_FDT_ADDR
 	li	a0, FW_JUMP_FDT_ADDR
 #ifdef FW_PIC
-	la	a1, _runtime_offset
+	lla	a1, _runtime_offset
 	REG_L	a1, (a1)
 	add	a0, a0, a1
 #endif
@@ -64,10 +64,10 @@ fw_next_arg1:
 	 * The next address should be returned in 'a0'.
 	 */
 fw_next_addr:
-	la	a0, _jump_addr
+	lla	a0, _jump_addr
 	REG_L	a0, (a0)
 #ifdef FW_PIC
-	la	a1, _runtime_offset
+	lla	a1, _runtime_offset
 	REG_L	a1, (a1)
 	add	a0, a0, a1
 #endif
firmware/fw_payload.S +2 -2
index 18b51d0..00c7cb7 100644
@@ -47,7 +47,7 @@ fw_next_arg1:
 #ifdef FW_PAYLOAD_FDT_ADDR
 	li	a0, FW_PAYLOAD_FDT_ADDR
 #ifdef FW_PIC
-	la      a1, _runtime_offset
+	lla	a1, _runtime_offset
 	REG_L   a1, (a1)
 	add     a0, a0, a1
 #endif
@@ -65,7 +65,7 @@ fw_next_arg1:
 	 * The next address should be returned in 'a0'.
 	 */
 fw_next_addr:
-	la	a0, payload_bin
+	lla	a0, payload_bin
 	ret
 
 	.section .entry, "ax", %progbits
Signature · 1 line
2.7.4
#6 lore raw

cover Support position independent execution

In reply to Vincent Chen

Hi Vincent,

Overall the changes are in quite good shape.

I have few high-level comments before I can review fw_base.S in detail:
1) No need to change toplevel Makefile for appending compiler and linker

    options. It is better to update $(firmware-genflags-y), $(firmware-cflags-y)
    and $(firmware-ldflags-y) in firmware/objects.mk

2) I would suggest to squash PATCH2 and PATCH3 into one PATCH and make this PATCH as PATCH1
3) The current PATCH1 can be PATCH2

Quoted 60 lines

-----Original Message-----
From: opensbi <[email protected]> On Behalf Of Vincent Chen
Sent: 02 March 2021 20:56
To: opensbi at lists.infradead.org
Cc: vincent.chen at sifive.com
Subject: [PATCH 0/3] Support position independent execution

This patch set enables OpenSBI to support position-independent execution so that one OpenSBI generic firmware can run on multiple platforms, even if they have different memory base addresses. However, the PIC code will decrease a bit of performance. The 2nd and 3rd patches are used to reduce this impact of the PIC.

The performance loss of PIC comes from two additional operations. One is from the symbol relocation in the beginning stage, and another one is from the GOT reference (auipc + load GOT) in the OpenSBI lifetime. The former only affects the boot time, but the latter will affect the performance of the OpenSBI payload such as the Linux kernel. After analyzing the fw_payload.elf of the generic platform, 27 symbols use the GOT reference method to get their address. Fortunately, 17 of these symbols are accessed once during the initialized step. Only the remaining
10 symbols may be called repeatedly during the lifetime, which are 1. hartid_to_domain_table 2. hartid_to_scratch_table 3. sbi_hart_expected_trap 4. sbi_tlb_local_hfence_vvma 5. sbi_tlb_local_hfence_gvma 6. sbi_tlb_local_sfence_vma_asid 7. sbi_tlb_local_fence_i 8. sbi_tlb_local_hfence_gvma 9. sbi_tlb_local_hfence_vvma 10. sbi_tlb_local_sfence_vma From these 10 symbols, I think the IPI handler may be one of the critical and commonly used paths affected by the PIC mode. Therefore, I create two kernel modules, One keeps issuing the REMOTE_FENCE_I request and the other one keeps issuing REMOTE_SFENCE_VMA request. These two modules are executed on the unleashed board to measure the consumed time of the sbi_tlb_request().
I found the PIC causes a 1.6% and 4.1% performance loss in addressing REMOTE_FENCE_I and REMOTE_SFENCE_VMA, respectively. In addition, to observe the impact of PIC mode in the general usage cases, I use SPEC2006 to benchmark performance. However, I could not observe any performance impact from the results. Based on the above two results, I think the performance impact of the PIC may not be obvious in general usage. Just in case, this patch still creates an FW_PIC option for users to configure the build options.

Vincent Chen (3):
firmware: Support position independent execution
firmware: fw_base: Use lla to access the global symbols defined in
fw_base.S
firware: Use lla to access all global symbols

 Makefile                |  10 +++++
 firmware/fw_base.S      | 116
++++++++++++++++++++++++++++++++++++------------
 firmware/fw_base.ldS    |  13 ++++++
 firmware/fw_dynamic.S   |  18 ++++----
 firmware/fw_jump.S      |  12 ++++-
 firmware/fw_payload.S   |   8 +++-
 include/sbi/riscv_elf.h |   9 ++++
 7 files changed, 147 insertions(+), 39 deletions(-)  create mode 100644

include/sbi/riscv_elf.h

Quoted 8 lines

--
2.7.4

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

#7 lore raw

cover Support position independent execution

Quoted 5 lines · Anup Patel

Hi Vincent,

Overall the changes are in quite good shape.

Quoted 8 lines

I have few high-level comments before I can review fw_base.S in detail:
1) No need to change toplevel Makefile for appending compiler and linker

    options. It is better to update $(firmware-genflags-y), $(firmware-cflags-y)
    and $(firmware-ldflags-y) in firmware/objects.mk

2) I would suggest to squash PATCH2 and PATCH3 into one PATCH and make this PATCH as PATCH1
3) The current PATCH1 can be PATCH2

OK, I got it. I will follow your suggestions to modify this patch. Thank you.