Skip to contentSkip to conversation

Fix XIP boot and make XIP testable in QEMU

PATCH Applied Frederik Haxel 5 participants 8 messages Dec 12, 2023 – Jan 11, 2024 1 message not in the archive RISC-V

lore
Share & tools
lore
#0 lore raw

cover Fix XIP boot and make XIP testable in QEMU

XIP boot seems to be broken for some time now. A likely reason why no one seems to have noticed this is that XIP is more difficult to test, as it is currently not easily testable with QEMU.

These patches fix the XIP boot and allow an XIP build without BUILTIN_DTB, which in turn makes it easier to test an image with the QEMU virt machine.

Frederik Haxel (3):
riscv: Make XIP bootable again
riscv: Fixed wrong register in XIP_FIXUP_FLASH_OFFSET macro
riscv: Allow disabling of BUILTIN_DTB for XIP

 arch/riscv/Kconfig                 | 6 +++---
 arch/riscv/include/asm/xip_fixup.h | 2 +-
 arch/riscv/kernel/head.S           | 5 ++++-
 arch/riscv/mm/init.c               | 8 ++++++--
 4 files changed, 14 insertions(+), 7 deletions(-)
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH 1/3 riscv: Make XIP bootable again

Currently, the XIP kernel seems to fail to boot due to missing XIP_FIXUP and a wrong page_offset value. A superfluous XIP_FIXUP has also been removed.

---
 arch/riscv/kernel/head.S | 1 +
 arch/riscv/mm/init.c     | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
arch/riscv/kernel/head.S +1 -0
index b77397432403..a2e2f0dd3899 100644
@@ -89,6 +89,7 @@ relocate_enable_mmu:
 	/* Compute satp for kernel page tables, but don't load it yet */
 	srl a2, a0, PAGE_SHIFT
 	la a1, satp_mode
+	XIP_FIXUP_OFFSET a1
 	REG_L a1, 0(a1)
 	or a2, a2, a1
 
arch/riscv/mm/init.c +6 -2
index 2e011cbddf3a..a65937336cdc 100644
@@ -174,6 +174,9 @@ void __init mem_init(void)
 
 /* Limit the memory size via mem. */
 static phys_addr_t memory_limit;
+#ifdef CONFIG_XIP_KERNEL
+#define memory_limit	(*(phys_addr_t *)XIP_FIXUP(&memory_limit))
+#endif /* CONFIG_XIP_KERNEL */
 
 static int __init early_mem(char *p)
 {
@@ -952,7 +955,7 @@ static void __init create_fdt_early_page_table(uintptr_t fix_fdt_va,
 	 * setup_vm_final installs the linear mapping. For 32-bit kernel, as the
 	 * kernel is mapped in the linear mapping, that makes no difference.
 	 */
-	dtb_early_va = kernel_mapping_pa_to_va(XIP_FIXUP(dtb_pa));
+	dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
 #endif
 
 	dtb_early_pa = dtb_pa;
@@ -1055,9 +1058,9 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 #endif
 
 	kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset;
-	kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
 
 #ifdef CONFIG_XIP_KERNEL
+	kernel_map.page_offset = PAGE_OFFSET_L3;
 	kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
 	kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
 
@@ -1067,6 +1070,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 
 	kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom;
 #else
+	kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
 	kernel_map.phys_addr = (uintptr_t)(&_start);
 	kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr;
 #endif
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw

PATCH 2/3 riscv: Fixed wrong register in XIP_FIXUP_FLASH_OFFSET macro

In reply to Frederik Haxel

During the refactoring, a bug was introduced in the rarly used XIP_FIXUP_FLASH_OFFSET macro.

Fixes bee7fbc38579 ("RISC-V CPU Idle Support")
Fixes e7681beba992 ("RISC-V: Split out the XIP fixups into their own file")
---
 arch/riscv/include/asm/xip_fixup.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/xip_fixup.h +1 -1
index d4ffc3c37649..b65bf6306f69 100644
@@ -13,7 +13,7 @@
         add \reg, \reg, t0
 .endm
 .macro XIP_FIXUP_FLASH_OFFSET reg
-	la t1, __data_loc
+	la t0, __data_loc
 	REG_L t1, _xip_phys_offset
 	sub \reg, \reg, t1
 	add \reg, \reg, t0
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#3 lore raw

PATCH 3/3 riscv: Allow disabling of BUILTIN_DTB for XIP

In reply to Frederik Haxel

This enables, among other things, testing with the QEMU virt machine.

To build an XIP kernel for the QEMU virt machine, configure the the kernel as desired and apply the following configuration

```
CONFIG_NONPORTABLE=y
CONFIG_XIP_KERNEL=y
CONFIG_XIP_PHYS_ADDR=0x20000000
CONFIG_PHYS_RAM_BASE=0x80200000
CONFIG_BUILTIN_DTB=n
```

Since the QEMU virt flash memory expects a 32 MB file, the built image must be padded. For example, with
truncate -s 32M arch/riscv/boot/xipImage

The kernel can be started using the following command in QEMU (v8+)

```
qemu-system-riscv64 -M virt,pflash0=pflash0 \
 -blockdev node-name=pflash0,driver=file,read-only=on,\
filename=arch/riscv/boot/xipImage <optional parameters>
```
---
 arch/riscv/Kconfig       | 6 +++---
 arch/riscv/kernel/head.S | 4 +++-
 2 files changed, 6 insertions(+), 4 deletions(-)
arch/riscv/Kconfig +3 -3
index 95a2a06acc6a..72bc31b6eeb9 100644
@@ -904,13 +904,13 @@ config RISCV_ISA_FALLBACK
 	  on the replacement properties, "riscv,isa-base" and
 	  "riscv,isa-extensions".
 
-endmenu # "Boot options"
-
 config BUILTIN_DTB
-	bool
+	bool "Built-in device tree"
 	depends on OF && NONPORTABLE
 	default y if XIP_KERNEL
 
+endmenu # "Boot options"
+
 config PORTABLE
 	bool
 	default !NONPORTABLE
arch/riscv/kernel/head.S +3 -1
index a2e2f0dd3899..a8939558702c 100644
@@ -266,10 +266,12 @@ SYM_CODE_START(_start_kernel)
 	la sp, _end + THREAD_SIZE
 	XIP_FIXUP_OFFSET sp
 	mv s0, a0
+	mv s1, a1
 	call __copy_data
 
-	/* Restore a0 copy */
+	/* Restore a0 & a1 copy */
 	mv a0, s0
+	mv a1, s1
 #endif
 
 #ifndef CONFIG_XIP_KERNEL
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#4 lore raw
In reply to Frederik Haxel
Quoted 6 lines · Frederik Haxel

XIP boot seems to be broken for some time now. A likely reason why no one seems to have noticed this is that XIP is more difficult to test, as it is currently not easily testable with QEMU.

These patches fix the XIP boot and allow an XIP build without BUILTIN_DTB, which in turn makes it easier to test an image with the QEMU virt machine.

Are you actually using XIP in something other than QEMU? The fact that some of the blamed fixes are over 18 months old suggests that you are not actively using XIP builds of the mainline kernel.
There is a desire to remove XIP support (among other things), so if you do actually have a use case for it, speak up.

A message that is not in the archive (sent off-list or not yet received): CAM4kBBKBC=ZSQPzAq5MFQvd1g9=Up_oRAK5CV74oJHt=SbcPqg@mail.gmail.com

#6 lore raw

Firstly, no html mails, they're rejected by the lists and break people's mail flows.

Quoted 10 lines · Vitaly Wool

tis 12 dec. 2023 kl. 14:23 skrev Conor Dooley <[email protected]<mailto:[email protected]>>:

Quoted 9 lines · Frederik Haxel
Quoted 6 lines

XIP boot seems to be broken for some time now. A likely reason why no one seems to have noticed this is that XIP is more difficult to test, as it is currently not easily testable with QEMU.

These patches fix the XIP boot and allow an XIP build without BUILTIN_DTB, which in turn makes it easier to test an image with the QEMU virt machine.

Quoted 7 lines
Quoted 5 lines

Are you actually using XIP in something other than QEMU? The fact that some of the blamed fixes are over 18 months old suggests that you are not actively using XIP builds of the mainline kernel.
There is a desire to remove XIP support (among other things), so if you do actually have a use case for it, speak up.

Yes, we surely do, on K210 for instance. It’s using an older kernel and

The k210 and nommu are on the block for removal too :) Is your use case for either the k210 or XIP something other than "ooh this works"?

Quoted 1 line

I haven’t checked the mainline status for a while

I figured that when the request I sent asking if you could test XIP was ignored. We've been 50-50 on whether it has been broken since Alex put the dtb in the fixmap ~9 months ago.

Quoted 2 lines

but it is likely that I will come up with some XIP updates before Christmas.

May I ask what you intend updating?

Cheers,
Conor.

maintainer
#7 lore raw
Quoted 21 lines · Conor Dooley

Firstly, no html mails, they're rejected by the lists and break people's mail flows.

Quoted 16 lines · Vitaly Wool

tis 12 dec. 2023 kl. 14:23 skrev Conor Dooley <[email protected]<mailto:[email protected]>>:

Quoted 14 lines · Frederik Haxel
Quoted 6 lines

XIP boot seems to be broken for some time now. A likely reason why no one seems to have noticed this is that XIP is more difficult to test, as it is currently not easily testable with QEMU.

These patches fix the XIP boot and allow an XIP build without BUILTIN_DTB, which in turn makes it easier to test an image with the QEMU virt machine.

Are you actually using XIP in something other than QEMU? The fact that some of the blamed fixes are over 18 months old suggests that you are not actively using XIP builds of the mainline kernel.
There is a desire to remove XIP support (among other things), so if you do actually have a use case for it, speak up.

Quoted 5 lines
Quoted 1 line

Yes, we surely do, on K210 for instance. It’s using an older kernel and

The k210 and nommu are on the block for removal too :) Is your use case for either the k210 or XIP something other than "ooh this works"?

Ya, if someone is actually using the XIP (and/or K210 or NOMMU) stuff then I'm fine keeping it. We'd just figured it was probably broken because nobody'd been posting patches and we didn't have any automated tests. Looks like it was broken, which is always a pretty strong sign it's not being used.

Quoted 13 lines
Quoted 1 line

I haven’t checked the mainline status for a while

I figured that when the request I sent asking if you could test XIP was ignored. We've been 50-50 on whether it has been broken since Alex put the dtb in the fixmap ~9 months ago.

Quoted 2 lines

but it is likely that I will come up with some XIP updates before Christmas.

May I ask what you intend updating?

Cheers,
Conor.

#8 lore raw
In reply to Frederik Haxel
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 8 lines · On Tue, 12 Dec 2023 14:01:11 +0100 you wrote:

XIP boot seems to be broken for some time now. A likely reason why no one seems to have noticed this is that XIP is more difficult to test, as it is currently not easily testable with QEMU.

These patches fix the XIP boot and allow an XIP build without BUILTIN_DTB, which in turn makes it easier to test an image with the QEMU virt machine.

[...]

Here is the summary with links:
- [1/3] riscv: Make XIP bootable again

    https://git.kernel.org/riscv/c/66f1e6809397
  - [2/3] riscv: Fixed wrong register in XIP_FIXUP_FLASH_OFFSET macro
    https://git.kernel.org/riscv/c/5daa37264102
  - [3/3] riscv: Allow disabling of BUILTIN_DTB for XIP
    https://git.kernel.org/riscv/c/6c4a2f6329f0

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html