Skip to content

Commit 71e11d0

Browse files
Merge patch series "riscv: kexec: cleanup and fixups"
Song Shuai <songshuaishuai@tinylab.org> says: This series contains a cleanup for riscv_kexec_relocate() and two fixups for KEXEC_FILE and had passed the basic kexec test in my 64bit Qemu-virt. You can use this kexec-tools[3] to test the kexec-file-syscall and these patches. riscv: kexec: Cleanup riscv_kexec_relocate (patch1) ================================================== 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 riscv: kexec: Align the kexeced kernel entry (patch2) ================================================== The current riscv boot protocol requires 2MB alignment for RV64 and 4MB alignment for RV32. In KEXEC_FILE path, the elf_find_pbase() function should align the kexeced kernel entry according to the requirement, otherwise the kexeced kernel would silently BUG at the setup_vm(). riscv: kexec: Remove -fPIE for PURGATORY_CFLAGS (patch3) ================================================== 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. arch/riscv/kernel/elf_kexec.c | 8 ++++- arch/riscv/kernel/kexec_relocate.S | 52 +++++++++++++----------------- arch/riscv/purgatory/Makefile | 4 +++ 3 files changed, 34 insertions(+), 30 deletions(-) * b4-shazam-merge: riscv: kexec: Remove -fPIE for PURGATORY_CFLAGS riscv: kexec: Align the kexeced kernel entry riscv: kexec: Cleanup riscv_kexec_relocate Link: https://lore.kernel.org/r/20230907103304.590739-1-songshuaishuai@tinylab.org Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2 parents 0bb80ec + 0f5f46a commit 71e11d0

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

arch/riscv/kernel/elf_kexec.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ static int elf_find_pbase(struct kimage *image, unsigned long kernel_len,
9898
kbuf.image = image;
9999
kbuf.buf_min = lowest_paddr;
100100
kbuf.buf_max = ULONG_MAX;
101-
kbuf.buf_align = PAGE_SIZE;
101+
102+
/*
103+
* Current riscv boot protocol requires 2MB alignment for
104+
* RV64 and 4MB alignment for RV32
105+
*
106+
*/
107+
kbuf.buf_align = PMD_SIZE;
102108
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
103109
kbuf.memsz = ALIGN(kernel_len, PAGE_SIZE);
104110
kbuf.top_down = false;

arch/riscv/kernel/kexec_relocate.S

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,17 @@ SYM_CODE_START(riscv_kexec_relocate)
1717
* s1: (const) Phys address to jump to after relocation
1818
* s2: (const) Phys address of the FDT image
1919
* s3: (const) The hartid of the current hart
20-
* s4: Pointer to the destination address for the relocation
21-
* s5: (const) Number of words per page
22-
* s6: (const) 1, used for subtraction
23-
* s7: (const) kernel_map.va_pa_offset, used when switching MMU off
24-
* s8: (const) Physical address of the main loop
25-
* s9: (debug) indirection page counter
26-
* s10: (debug) entry counter
27-
* s11: (debug) copied words counter
20+
* s4: (const) kernel_map.va_pa_offset, used when switching MMU off
21+
* s5: Pointer to the destination address for the relocation
22+
* s6: (const) Physical address of the main loop
2823
*/
2924
mv s0, a0
3025
mv s1, a1
3126
mv s2, a2
3227
mv s3, a3
33-
mv s4, zero
34-
li s5, (PAGE_SIZE / RISCV_SZPTR)
35-
li s6, 1
36-
mv s7, a4
37-
mv s8, zero
38-
mv s9, zero
39-
mv s10, zero
40-
mv s11, zero
28+
mv s4, a4
29+
mv s5, zero
30+
mv s6, zero
4131

4232
/* Disable / cleanup interrupts */
4333
csrw CSR_SIE, zero
@@ -52,31 +42,36 @@ SYM_CODE_START(riscv_kexec_relocate)
5242
* the start of the loop below so that we jump there in
5343
* any case.
5444
*/
55-
la s8, 1f
56-
sub s8, s8, s7
57-
csrw CSR_STVEC, s8
45+
la s6, 1f
46+
sub s6, s6, s4
47+
csrw CSR_STVEC, s6
48+
49+
/*
50+
* With C-extension, here we get 42 Bytes and the next
51+
* .align directive would pad zeros here up to 44 Bytes.
52+
* So manually put a nop here to avoid zeros padding.
53+
*/
54+
nop
5855

5956
/* Process entries in a loop */
6057
.align 2
6158
1:
62-
addi s10, s10, 1
6359
REG_L t0, 0(s0) /* t0 = *image->entry */
6460
addi s0, s0, RISCV_SZPTR /* image->entry++ */
6561

6662
/* IND_DESTINATION entry ? -> save destination address */
6763
andi t1, t0, 0x1
6864
beqz t1, 2f
69-
andi s4, t0, ~0x1
65+
andi s5, t0, ~0x1
7066
j 1b
7167

7268
2:
7369
/* IND_INDIRECTION entry ? -> update next entry ptr (PA) */
7470
andi t1, t0, 0x2
7571
beqz t1, 2f
7672
andi s0, t0, ~0x2
77-
addi s9, s9, 1
7873
csrw CSR_SATP, zero
79-
jalr zero, s8, 0
74+
jr s6
8075

8176
2:
8277
/* IND_DONE entry ? -> jump to done label */
@@ -92,14 +87,13 @@ SYM_CODE_START(riscv_kexec_relocate)
9287
andi t1, t0, 0x8
9388
beqz t1, 1b /* Unknown entry type, ignore it */
9489
andi t0, t0, ~0x8
95-
mv t3, s5 /* i = num words per page */
90+
li t3, (PAGE_SIZE / RISCV_SZPTR) /* i = num words per page */
9691
3: /* copy loop */
9792
REG_L t1, (t0) /* t1 = *src_ptr */
98-
REG_S t1, (s4) /* *dst_ptr = *src_ptr */
93+
REG_S t1, (s5) /* *dst_ptr = *src_ptr */
9994
addi t0, t0, RISCV_SZPTR /* stc_ptr++ */
100-
addi s4, s4, RISCV_SZPTR /* dst_ptr++ */
101-
sub t3, t3, s6 /* i-- */
102-
addi s11, s11, 1 /* c++ */
95+
addi s5, s5, RISCV_SZPTR /* dst_ptr++ */
96+
addi t3, t3, -0x1 /* i-- */
10397
beqz t3, 1b /* copy done ? */
10498
j 3b
10599

@@ -146,7 +140,7 @@ SYM_CODE_START(riscv_kexec_relocate)
146140
*/
147141
fence.i
148142

149-
jalr zero, a2, 0
143+
jr a2
150144

151145
SYM_CODE_END(riscv_kexec_relocate)
152146
riscv_kexec_relocate_end:

arch/riscv/purgatory/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ ifdef CONFIG_CFI_CLANG
8181
PURGATORY_CFLAGS_REMOVE += $(CC_FLAGS_CFI)
8282
endif
8383

84+
ifdef CONFIG_RELOCATABLE
85+
PURGATORY_CFLAGS_REMOVE += -fPIE
86+
endif
87+
8488
CFLAGS_REMOVE_purgatory.o += $(PURGATORY_CFLAGS_REMOVE)
8589
CFLAGS_purgatory.o += $(PURGATORY_CFLAGS)
8690

0 commit comments

Comments
 (0)