Expected behavior of the go command
Hi all,
I was trying to understand how the go command is expected to work when used to load custom OSes. My use case requires getting a proprietary OS to boot using the go command on a TI K3 platform. The OS expects caches to be flushed and the mmu to be disabled before we jump to it.
The go command (do_go func in cmd/boot.c) calls the do_go_exec function underneath which does not seem to have a consistent implementation across various architectures.
In RISC-V the implementation (arch/riscv/lib/boot.c:10) calls cleanup_before_linux before jumping to the loaded binary. This behaviour would enable the direct use of the go command for booting a custom OS (like in my use case). Though no such generic implementation exists for ARM64.
I was wondering why we have the call to cleanup_before_linux for RISCV and not ARM64?
Also, is booting a custom OS using the go command a supported use-case? If yes, it might be better to rename the function to be more generic like cleanup_before_os from *_linux and call it for each invocation of the go command.
Regards,
Anshul
Hi Anshul,
Quoted 18 lines · Anshul Dalal
Hi all,
I was trying to understand how the go command is expected to work when used to load custom OSes. My use case requires getting a proprietary OS to boot using the go command on a TI K3 platform. The OS expects caches to be flushed and the mmu to be disabled before we jump to it.
The go command (
do_gofunc incmd/boot.c) calls thedo_go_execfunction underneath which does not seem to have a consistent implementation across various architectures.In RISC-V the implementation (
arch/riscv/lib/boot.c:10) callscleanup_before_linuxbefore jumping to the loaded binary. This behaviour would enable the direct use of the go command for booting a custom OS (like in my use case). Though no such generic implementation exists for ARM64.
Quoted 7 lines
I was wondering why we have the call to
cleanup_before_linuxfor RISCV and not ARM64?Also, is booting a custom OS using the go command a supported use-case? If yes, it might be better to rename the function to be more generic like
cleanup_before_osfrom*_linuxand call it for each invocation of the go command.
Yes it is supposed to do what you say. You could rationalise the code a bit, so that all archs do a similar thing, or perhaps just RISC-V and ARM for now.
Regards,
Simon
Hi,
Quoted 24 lines · Simon Glass
Hi Anshul,
Quoted 21 lines · Anshul Dalal
Hi all,
I was trying to understand how the go command is expected to work when used to load custom OSes. My use case requires getting a proprietary OS to boot using the go command on a TI K3 platform. The OS expects caches to be flushed and the mmu to be disabled before we jump to it.
The go command (
do_gofunc incmd/boot.c) calls thedo_go_execfunction underneath which does not seem to have a consistent implementation across various architectures.In RISC-V the implementation (
arch/riscv/lib/boot.c:10) callscleanup_before_linuxbefore jumping to the loaded binary. This behaviour would enable the direct use of the go command for booting a custom OS (like in my use case). Though no such generic implementation exists for ARM64.I was wondering why we have the call to
cleanup_before_linuxfor RISCV and not ARM64?
Quoted 8 lines
Quoted 4 lines
Also, is booting a custom OS using the go command a supported use-case? If yes, it might be better to rename the function to be more generic like
cleanup_before_osfrom*_linuxand call it for each invocation of the go command.Yes it is supposed to do what you say. You could rationalise the code a bit, so that all archs do a similar thing, or perhaps just RISC-V and ARM for now.
I ran into issues with go too trying to start barebox and I always assumed that go is rather meant to start programs that call back into U-Boot using the jump table mechanism. In that case, you likely don't want to turn off caches.
What I settled on is having the barebox build generate a FIT image that can be used from U-Boot, barebox, coreboot ...etc.
Maybe the same would be also suitable for you? Alternatively, you could compile your custom OS as an ELF image and boot that properly as if it were a kernel. Both ways would also allow you to specify a load address if your OS is not relocatable and pass along arguments.
I went with the FIT route for barebox, because we can now ship device trees for all enabled boards in the same FIT image alongside one generic second stage barebox with DT support. If you want to support different boards with your OS image that might be of interest to you as well.
Cheers,
Ahmad
Quoted 4 lines
Regards,
Simon
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Quoted 32 lines · Ahmad Fatoum
Hi,
Quoted 29 lines · Simon Glass
Hi Anshul,
Quoted 25 lines · Anshul Dalal
Hi all,
I was trying to understand how the go command is expected to work when used to load custom OSes. My use case requires getting a proprietary OS to boot using the go command on a TI K3 platform. The OS expects caches to be flushed and the mmu to be disabled before we jump to it.
The go command (
do_gofunc incmd/boot.c) calls thedo_go_execfunction underneath which does not seem to have a consistent implementation across various architectures.In RISC-V the implementation (
arch/riscv/lib/boot.c:10) callscleanup_before_linuxbefore jumping to the loaded binary. This behaviour would enable the direct use of the go command for booting a custom OS (like in my use case). Though no such generic implementation exists for ARM64.I was wondering why we have the call to
cleanup_before_linuxfor RISCV and not ARM64?Also, is booting a custom OS using the go command a supported use-case? If yes, it might be better to rename the function to be more generic like
cleanup_before_osfrom*_linuxand call it for each invocation of the go command.
Quoted 8 lines
Quoted 3 lines
Yes it is supposed to do what you say. You could rationalise the code a bit, so that all archs do a similar thing, or perhaps just RISC-V and ARM for now.
I ran into issues with go too trying to start barebox and I always assumed that go is rather meant to start programs that call back into U-Boot using the jump table mechanism. In that case, you likely don't want to turn off caches.
Exactly. "go" is really just "go $addr now", and so if things like caches need to be flushed / disabled, that needs to happen via a command first. RISC-V being helpful here is an unfortunate mis-implementation.
Quoted 8 lines
What I settled on is having the barebox build generate a FIT image that can be used from U-Boot, barebox, coreboot ...etc.
Maybe the same would be also suitable for you? Alternatively, you could compile your custom OS as an ELF image and boot that properly as if it were a kernel. Both ways would also allow you to specify a load address if your OS is not relocatable and pass along arguments.
Quoted 4 lines
I went with the FIT route for barebox, because we can now ship device trees for all enabled boards in the same FIT image alongside one generic second stage barebox with DT support. If you want to support different boards with your OS image that might be of interest to you as well.
A FIT image is a good idea. It's also possible and I think more commonly how custom OSes are done, to use "bootelf" instead.
Signature · 1 line
Tom
Hi,
Quoted 50 lines · Ahmad Fatoum
Hi,
Quoted 24 lines · Simon Glass
Hi Anshul,
Quoted 21 lines · Anshul Dalal
Hi all,
I was trying to understand how the go command is expected to work when used to load custom OSes. My use case requires getting a proprietary OS to boot using the go command on a TI K3 platform. The OS expects caches to be flushed and the mmu to be disabled before we jump to it.
The go command (
do_gofunc incmd/boot.c) calls thedo_go_execfunction underneath which does not seem to have a consistent implementation across various architectures.In RISC-V the implementation (
arch/riscv/lib/boot.c:10) callscleanup_before_linuxbefore jumping to the loaded binary. This behaviour would enable the direct use of the go command for booting a custom OS (like in my use case). Though no such generic implementation exists for ARM64.I was wondering why we have the call to
cleanup_before_linuxfor RISCV and not ARM64?Quoted 8 lines
Quoted 4 lines
Also, is booting a custom OS using the go command a supported use-case? If yes, it might be better to rename the function to be more generic like
cleanup_before_osfrom*_linuxand call it for each invocation of the go command.Yes it is supposed to do what you say. You could rationalise the code a bit, so that all archs do a similar thing, or perhaps just RISC-V and ARM for now.
I ran into issues with go too trying to start barebox and I always assumed that go is rather meant to start programs that call back into U-Boot using the jump table mechanism. In that case, you likely don't want to turn off caches.
What I settled on is having the barebox build generate a FIT image that can be used from U-Boot, barebox, coreboot ...etc.
Maybe the same would be also suitable for you? Alternatively, you could compile your custom OS as an ELF image and boot that properly as if it were a kernel. Both ways would also allow you to specify a load address if your OS is not relocatable and pass along arguments.
Quoted 4 lines
I went with the FIT route for barebox, because we can now ship device trees for all enabled boards in the same FIT image alongside one generic second stage barebox with DT support. If you want to support different boards with your OS image that might be of interest to you as well.
Yes, FIT is a better route and we can even add an OS type IH_OS_xxx
There's also the option of adding a flag to the 'go' command so that it can
Regards,
Simon
Quoted 46 lines · Ahmad Fatoum
Hi,
Quoted 23 lines · Simon Glass
Hi Anshul,
Quoted 20 lines · Anshul Dalal
Hi all,
I was trying to understand how the go command is expected to work when used to load custom OSes. My use case requires getting a proprietary OS to boot using the go command on a TI K3 platform. The OS expects caches to be flushed and the mmu to be disabled before we jump to it.
The go command (
do_gofunc incmd/boot.c) calls thedo_go_execfunction underneath which does not seem to have a consistent implementation across various architectures.In RISC-V the implementation (
arch/riscv/lib/boot.c:10) callscleanup_before_linuxbefore jumping to the loaded binary. This behaviour would enable the direct use of the go command for booting a custom OS (like in my use case). Though no such generic implementation exists for ARM64.I was wondering why we have the call to
cleanup_before_linuxfor RISCV and not ARM64?Quoted 7 lines
Quoted 4 lines
Also, is booting a custom OS using the go command a supported use-case? If yes, it might be better to rename the function to be more generic like
cleanup_before_osfrom*_linuxand call it for each invocation of the go command.Yes it is supposed to do what you say. You could rationalise the code a bit, so that all archs do a similar thing, or perhaps just RISC-V and ARM for now.
I ran into issues with go too trying to start barebox and I always assumed that go is rather meant to start programs that call back into U-Boot using the jump table mechanism. In that case, you likely don't want to turn off caches.
What I settled on is having the barebox build generate a FIT image that can be used from U-Boot, barebox, coreboot ...etc.
Maybe the same would be also suitable for you? Alternatively, you could compile your custom OS as an ELF image and boot that properly as if it were a kernel. Both ways would also allow you to specify a load address if your OS is not relocatable and pass along arguments.
Quoted 4 lines
I went with the FIT route for barebox, because we can now ship device trees for all enabled boards in the same FIT image alongside one generic second stage barebox with DT support. If you want to support different boards with your OS image that might be of interest to you as well.
Sorry for novice question.
But trying to understand how FIT fixes problem for you .
at the end, its a jump command to OS/other code be it through u-boot go or fit way.
In case OS/other code handles caches, then should not be an issue. But this put requirement on next stage to take care of caches [0]
[0] : https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/head.S#L105
Quoted 7 lines
Cheers,
AhmadQuoted 3 lines
Regards,
Simon
Hi Udit,
Quoted 7 lines · Udit
Quoted 4 lines · Ahmad Fatoum
I went with the FIT route for barebox, because we can now ship device trees for all enabled boards in the same FIT image alongside one generic second stage barebox with DT support. If you want to support different boards with your OS image that might be of interest to you as well.
Quoted 5 lines
Sorry for novice question.
But trying to understand how FIT fixes problem for you .
at the end, its a jump command to OS/other code be it through u-boot go or fit way.
The difference is whether, prior to the jump, the caches are flushed and the MMU is disabled and U-Boot's go does neither.
Thinking about it, I am wondering whether U-Boot go is inherently broken, even for the jump-table use case, if caches are enabled:
Because go doesn't do any cache maintenance, what guarantee do we have that the I-Cache actually reflects the data that was freshly loaded?
It might only work because the region you go to happens to not be resident in the I-Cache yet...
FWIW, barebox go does cache maintenance and disables the MMU, which is why I mistakenly thought U-Boot's go would behave similarly. barebox didn't inherit the jump table mechanism.
Quoted 2 lines
In case OS/other code handles caches, then should not be an issue. But this put requirement on next stage to take care of caches [0]
In the fit case, MMU is disabled and so the next stage is free to not do any cache maintenance if it doesn't need to enable the MMU. Linux and barebox want to use the MMU/caches by default, so that's why they have to do cache maintenance.
If they assume MMU is disabled on startup, it follows the lines in the cache are stale and unimportant and then memory corruption occurs once they are discarded.
Cheers,
Ahmad
Quoted 13 lines
[0] : https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/head.S#L105
Quoted 7 lines
Cheers,
AhmadQuoted 3 lines
Regards,
Simon
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Thanks Ahmad
Quoted 10 lines · Ahmad Fatoum
Hi Udit,
Quoted 7 lines · Udit
Quoted 4 lines · Ahmad Fatoum
I went with the FIT route for barebox, because we can now ship device trees for all enabled boards in the same FIT image alongside one generic second stage barebox with DT support. If you want to support different boards with your OS image that might be of interest to you as well.
Sorry for novice question.
Quoted 8 lines
Quoted 3 lines
But trying to understand how FIT fixes problem for you .
at the end, its a jump command to OS/other code be it through u-boot go or fit way.
The difference is whether, prior to the jump, the caches are flushed and the MMU is disabled and U-Boot's go does neither.
Thinking about it, I am wondering whether U-Boot go is inherently broken, even for the jump-table use case, if caches are enabled:
Correct, this is what I think . Before jumping to new program go command should make sure
things are in correct order by cache flush. Kind of below
cmd/boot.c +0 -0
@@ -18,6 +18,7 @@ __attribute__((weak))
unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc, char *const argv[])
{
+ flush_cache(...)
return entry (argc, argv);
}
I see even ARM64, few board choose to over-ride default option (board/xilinx/versal/board.c)
do_go_exec does dcache_disable()
Quoted 2 lines
Because go doesn't do any cache maintenance, what guarantee do we have that the I-Cache actually reflects the data that was freshly loaded?
correct , depends upon address etc , it may or may not work
Quoted 7 lines
It might only work because the region you go to happens to not be resident in the I-Cache yet...
FWIW, barebox go does cache maintenance and disables the MMU, which is why I mistakenly thought U-Boot's go would behave similarly. barebox didn't inherit the jump table mechanism.
Quoted 6 lines
Quoted 2 lines
In case OS/other code handles caches, then should not be an issue. But this put requirement on next stage to take care of caches [0]
In the fit case, MMU is disabled and so the next stage is free to not do any cache maintenance if it doesn't need to enable the MMU. Linux and barebox want to use the MMU/caches by default, so that's why they have to do cache maintenance.
Sorry I was not clear in previous message,
Here OS take care of caching , irrespective of how boot is done through fit
or kind of efi or other method.
Quoted 18 lines
If they assume MMU is disabled on startup, it follows the lines in the cache are stale and unimportant and then memory corruption occurs once they are discarded.
Cheers,
AhmadQuoted 10 lines
[0] : https://github.com/torvalds/linux/blob/master/arch/arm64/kernel/head.S#L105
Quoted 6 lines
Cheers,
AhmadQuoted 3 lines
Regards,
Simon
Hi Udit,
Quoted 14 lines · Udit
Quoted 2 lines · Ahmad Fatoum
Thinking about it, I am wondering whether U-Boot go is inherently broken, even for the jump-table use case, if caches are enabled:
Correct, this is what I think . Before jumping to new program go command should make sure
things are in correct order by cache flush. Kind of below
cmd/boot.c +0
-0
cmd/boot.c __attribute__((weak)) · line 21
Quoted 4 lines
cmd/boot.c +1
-0@@ -18,6 +18,7 @@ __attribute__((weak)) unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc, char *const argv[]) { + flush_cache(...)
yes, even with MMU left enabled, it looks like there should be an I-Cache invalidate and D-Cache flush/clean here[1].
Quoted 4 lines
I see even ARM64, few board choose to over-ride default option (board/
xilinx/versal/board.c)
do_go_execdoesdcache_disable()
I think __weak is a fundamental mistake and upstream boards abusing it to not have to fix things generally shows that.
Quoted 3 lines
Sorry I was not clear in previous message,
Here OS take care of caching , irrespective of how boot is done through fit
Yes, but with the assumption that MMU was disabled at start.
Quoted 1 line
or kind of efi or other method.
Interestingly, with EFI MMU should be left enabled. Linux even warns about the case that MMU was disabled when entering the stub.
Cheers,
Ahmad
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Hi Ahmad
Quoted 11 lines · Ahmad Fatoum
Hi Udit,
Quoted 8 lines · Udit
Quoted 2 lines · Ahmad Fatoum
Thinking about it, I am wondering whether U-Boot go is inherently broken, even for the jump-table use case, if caches are enabled:
Correct, this is what I think . Before jumping to new program go command should make sure
things are in correct order by cache flush. Kind of below
Quoted 11 lines
cmd/boot.c __attribute__((weak)) · line 21
Quoted 9 lines
cmd/boot.c +1
-0@@ -18,6 +18,7 @@ __attribute__((weak)) unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc, char *const argv[]) { + flush_cache(...)yes, even with MMU left enabled, it looks like there should be an I-Cache invalidate and D-Cache flush/clean here[1].
Yes, correct . This should be extended with above
Quoted 20 lines
Quoted 5 lines
I see even ARM64, few board choose to over-ride default option (board/
xilinx/versal/board.c)
do_go_execdoesdcache_disable()I think
__weakis a fundamental mistake and upstream boards abusing it to not have to fix things generally shows that.Quoted 3 lines
Sorry I was not clear in previous message,
Here OS take care of caching , irrespective of how boot is done through fit
Yes, but with the assumption that MMU was disabled at start.
Quoted 1 line
or kind of efi or other method.
Interestingly, with EFI MMU should be left enabled. Linux even warns about the case that MMU was disabled when entering the stub.
Cheers,
Ahmad