hw/arm: Fix raspi, aspeed bootloaders on big-endian hosts
cover hw/arm: Fix raspi, aspeed bootloaders on big-endian hosts
Both the raspi and aspeed boards load their secondary CPU bootloader code in a way that only works on little-endian hosts. This patchset fixes that by making them both use the write_bootloader() function in boot.c, which gets endianness-handling right.
Patches 1 and 2 are essentially a patch from Cédric from a few months ago:
https://patchew.org/QEMU/[email protected]/[email protected]/
I've split it into two patches and tweaked it a bit.
These fixes let us run the avocado tests for these boards on big-endian hosts.
thanks
-- PMM
Cédric Le Goater (2):
hw/arm/boot: Make write_bootloader() public as arm_write_bootloader() hw/arm/aspeed: Use arm_write_bootloader() to write the bootloader
Peter Maydell (1):
hw/arm/raspi: Use arm_write_bootloader() to write boot code
include/hw/arm/boot.h | 49 +++++++++++++++++++++++++++++++++ hw/arm/aspeed.c | 38 +++++++++++++------------ hw/arm/boot.c | 35 ++++++----------------- hw/arm/raspi.c | 64 +++++++++++++++++++++++-------------------- 4 files changed, 111 insertions(+), 75 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 hw/arm/boot: Make write_bootloader() public as arm_write_bootloader()
From: Cédric Le Goater <[email protected]>
The arm boot.c code includes a utility function write_bootloader() which assists in writing a boot-code fragment into guest memory, including handling endianness and fixing it up with entry point addresses and similar things. This is useful not just for the boot.c code but also in board model code, so rename it to arm_write_bootloader() and make it globally visible.
Since we are making it public, make its API a little neater: move the AddressSpace* argument to be next to the hwaddr argument, and allow the fixupcontext array to be const, since we never modify it in this function.
--- include/hw/arm/boot.h | 49 +++++++++++++++++++++++++++++++++++++++++++ hw/arm/boot.c | 35 +++++++------------------------ 2 files changed, 57 insertions(+), 27 deletions(-)
include/hw/arm/boot.h +49 -0
@@ -183,4 +183,53 @@ void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,const struct arm_boot_info *info, hwaddr mvbar_addr); +typedef enum { +FIXUP_NONE =0 ,/* do nothing */ +FIXUP_TERMINATOR ,/* end of insns */ +FIXUP_BOARDID ,/* overwrite with board ID number */ +FIXUP_BOARD_SETUP ,/* overwrite with board specific setup code address */ +FIXUP_ARGPTR_LO ,/* overwrite with pointer to kernel args */ +FIXUP_ARGPTR_HI ,/* overwrite with pointer to kernel args (high half) */ +FIXUP_ENTRYPOINT_LO ,/* overwrite with kernel entry point */ +FIXUP_ENTRYPOINT_HI ,/* overwrite with kernel entry point (high half) */ +FIXUP_GIC_CPU_IF ,/* overwrite with GIC CPU interface address */ +FIXUP_BOOTREG ,/* overwrite with boot register address */ +FIXUP_DSB ,/* overwrite with correct DSB insn for cpu */ +FIXUP_MAX , +}FixupType ; + +typedef struct ARMInsnFixup { +uint32_t insn; +FixupType fixup; +}ARMInsnFixup ; + +/** +* arm_write_bootloader - write a bootloader to guest memory +* @name: name of the bootloader blob +* @as: AddressSpace to write the bootloader +* @addr: guest address to write it +* @insns: the blob to be loaded +* @fixupcontext: context to be used for any fixups in @insns +* +* Write a bootloader to guest memory at address @addr in the address +* space @as. @name is the name to use for the resulting ROM blob, so +* it should be unique in the system and reasonably identifiable for debugging. +* +* @insns must be an array of ARMInsnFixup structs, each of which has +* one 32-bit value to be written to the guest memory, and a fixup to be +* applied to the value. FIXUP_NONE (do nothing) is value 0, so effectively +* the fixup is optional when writing a struct initializer. +* The final entry in the array must be { 0, FIXUP_TERMINATOR }. +* +* All other supported fixup types have the semantics "ignore insn +* and instead use the value from the array element @fixupcontext[fixup]". +* The caller should therefore provide @fixupcontext as an array of +* size FIXUP_MAX whose elements have been initialized for at least +* the entries that @insns refers to. +*/ +void arm_write_bootloader (const char *name, +AddressSpace *as,hwaddr addr, +const ARMInsnFixup *insns, +const uint32_t *fixupcontext); +#endif /* HW_ARM_BOOT_H */
hw/arm/boot.c +8 -27
@@ -60,26 +60,6 @@ AddressSpace *arm_boot_address_space(ARMCPU *cpu,return cpu_get_address_space (cs, asidx); } -typedef enum { -FIXUP_NONE =0 ,/* do nothing */ -FIXUP_TERMINATOR ,/* end of insns */ -FIXUP_BOARDID ,/* overwrite with board ID number */ -FIXUP_BOARD_SETUP ,/* overwrite with board specific setup code address */ -FIXUP_ARGPTR_LO ,/* overwrite with pointer to kernel args */ -FIXUP_ARGPTR_HI ,/* overwrite with pointer to kernel args (high half) */ -FIXUP_ENTRYPOINT_LO ,/* overwrite with kernel entry point */ -FIXUP_ENTRYPOINT_HI ,/* overwrite with kernel entry point (high half) */ -FIXUP_GIC_CPU_IF ,/* overwrite with GIC CPU interface address */ -FIXUP_BOOTREG ,/* overwrite with boot register address */ -FIXUP_DSB ,/* overwrite with correct DSB insn for cpu */ -FIXUP_MAX , -}FixupType ; - -typedef struct ARMInsnFixup { -uint32_t insn; -FixupType fixup; -}ARMInsnFixup ; -static const ARMInsnFixup bootloader_aarch64[] = { {0x580000c0 },/* ldr x0, arg ; Load the lower 32-bits of DTB */ {0xaa1f03e1 },/* mov x1, xzr */
@@ -150,9 +130,10 @@ static const ARMInsnFixup smpboot[] = { { 0, FIXUP_TERMINATOR } }; -static void write_bootloader (const char *name,hwaddr addr, -const ARMInsnFixup *insns,uint32_t *fixupcontext, -AddressSpace *as) +void arm_write_bootloader (const char *name, +AddressSpace *as,hwaddr addr, +const ARMInsnFixup *insns, +const uint32_t *fixupcontext) {/* Fix up the specified bootloader fragment and write it into * guest memory using rom_add_blob_fixed(). fixupcontext is
@@ -214,8 +195,8 @@ static void default_write_secondary(ARMCPU *cpu, fixupcontext[FIXUP_DSB ] =CP15_DSB_INSN ; } -write_bootloader ("smpboot" , info->smp_loader_start, - smpboot, fixupcontext, as); +arm_write_bootloader ("smpboot" , as, info->smp_loader_start, + smpboot, fixupcontext); } void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
@@ -1186,8 +1167,8 @@ static void arm_setup_direct_kernel_boot(ARMCPU *cpu, fixupcontext[FIXUP_ENTRYPOINT_LO ] = entry; fixupcontext[FIXUP_ENTRYPOINT_HI ] = entry >>32 ; -write_bootloader ("bootloader" , info->loader_start, - primary_loader, fixupcontext, as); +arm_write_bootloader ("bootloader" , as, info->loader_start, + primary_loader, fixupcontext);if (info->write_board_setup) { info->write_board_setup (cpu, info);
Signature · 1 line
2.34.1
PATCH 2/3 hw/arm/aspeed: Use arm_write_bootloader() to write the bootloader
In reply to Peter MaydellFrom: Cédric Le Goater <[email protected]>
When writing the secondary-CPU stub boot loader code to the guest, use arm_write_bootloader() instead of directly calling rom_add_blob_fixed(). This fixes a bug on big-endian hosts, because arm_write_bootloader() will correctly byte-swap the host-byte-order array values into the guest-byte-order to write into the guest memory.
[PMM: Moved the "make arm_write_bootloader() function public" part
to its own patch; updated commit message to note that this fixes an actual bug; adjust to the API changes noted in previous commit]
--- hw/arm/aspeed.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-)
hw/arm/aspeed.c +20 -18
@@ -200,33 +200,35 @@ struct AspeedMachineState {static void aspeed_write_smpboot (ARMCPU *cpu,const struct arm_boot_info *info) { -static const uint32_t poll_mailbox_ready[] = { +AddressSpace *as =arm_boot_address_space (cpu, info); +static const ARMInsnFixup poll_mailbox_ready[] = {/* * r2 = per-cpu go sign value * r1 = AST_SMP_MBOX_FIELD_ENTRY * r0 = AST_SMP_MBOX_FIELD_GOSIGN */ -0xee100fb0 ,/* mrc p15, 0, r0, c0, c0, 5 */ -0xe21000ff ,/* ands r0, r0, #255 */ -0xe59f201c ,/* ldr r2, [pc, #28] */ -0xe1822000 ,/* orr r2, r2, r0 */ + {0xee100fb0 },/* mrc p15, 0, r0, c0, c0, 5 */ + {0xe21000ff },/* ands r0, r0, #255 */ + {0xe59f201c },/* ldr r2, [pc, #28] */ + {0xe1822000 },/* orr r2, r2, r0 */ -0xe59f1018 ,/* ldr r1, [pc, #24] */ -0xe59f0018 ,/* ldr r0, [pc, #24] */ + {0xe59f1018 },/* ldr r1, [pc, #24] */ + {0xe59f0018 },/* ldr r0, [pc, #24] */ -0xe320f002 ,/* wfe */ -0xe5904000 ,/* ldr r4, [r0] */ -0xe1520004 ,/* cmp r2, r4 */ -0x1afffffb ,/* bne <wfe> */ -0xe591f000 ,/* ldr pc, [r1] */ -AST_SMP_MBOX_GOSIGN , -AST_SMP_MBOX_FIELD_ENTRY , -AST_SMP_MBOX_FIELD_GOSIGN , + {0xe320f002 },/* wfe */ + {0xe5904000 },/* ldr r4, [r0] */ + {0xe1520004 },/* cmp r2, r4 */ + {0x1afffffb },/* bne <wfe> */ + {0xe591f000 },/* ldr pc, [r1] */ + {AST_SMP_MBOX_GOSIGN }, + {AST_SMP_MBOX_FIELD_ENTRY }, + {AST_SMP_MBOX_FIELD_GOSIGN }, + {0 ,FIXUP_TERMINATOR } }; +static const uint32_t fixupcontext[FIXUP_MAX ] = {0 }; -rom_add_blob_fixed ("aspeed.smpboot" , poll_mailbox_ready, -sizeof (poll_mailbox_ready), - info->smp_loader_start); +arm_write_bootloader ("aspeed.smpboot" , as, info->smp_loader_start, + poll_mailbox_ready, fixupcontext); } static void aspeed_reset_secondary(ARMCPU *cpu,
Signature · 1 line
2.34.1
PATCH 3/3 hw/arm/raspi: Use arm_write_bootloader() to write boot code
In reply to Peter MaydellWhen writing the secondary-CPU stub boot loader code to the guest, use arm_write_bootloader() instead of directly calling rom_add_blob_fixed(). This fixes a bug on big-endian hosts, because arm_write_bootloader() will correctly byte-swap the host-byte-order array values into the guest-byte-order to write into the guest memory.
--- hw/arm/raspi.c | 64 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-)
hw/arm/raspi.c +34 -30
@@ -16,6 +16,7 @@#include "qemu/units.h" #include "qemu/cutils.h" #include "qapi/error.h" +#include "hw/arm/boot.h" #include "hw/arm/bcm2836.h" #include "hw/registerfields.h" #include "qemu/error-report.h"
@@ -124,20 +125,22 @@ static const char *board_type(uint32_t board_rev)static void write_smpboot (ARMCPU *cpu,const struct arm_boot_info *info) { -static const uint32_t smpboot[] = { -0xe1a0e00f ,/* mov lr, pc */ -0xe3a0fe00 + (BOARDSETUP_ADDR >>4 ),/* mov pc, BOARDSETUP_ADDR */ -0xee100fb0 ,/* mrc p15, 0, r0, c0, c0, 5;get core ID */ -0xe7e10050 ,/* ubfx r0, r0, #0, #2 ;extract LSB */ -0xe59f5014 ,/* ldr r5, =0x400000CC ;load mbox base */ -0xe320f001 ,/* 1: yield */ -0xe7953200 ,/* ldr r3, [r5, r0, lsl #4] ;read mbox for our core*/ -0xe3530000 ,/* cmp r3, #0 ;spin while zero */ -0x0afffffb ,/* beq 1b */ -0xe7853200 ,/* str r3, [r5, r0, lsl #4] ;clear mbox */ -0xe12fff13 ,/* bx r3 ;jump to target */ -0x400000cc ,/* (constant: mailbox 3 read/clear base) */ +static const ARMInsnFixup smpboot[] = { + {0xe1a0e00f },/* mov lr, pc */ + {0xe3a0fe00 + (BOARDSETUP_ADDR >>4 ) },/* mov pc, BOARDSETUP_ADDR */ + {0xee100fb0 },/* mrc p15, 0, r0, c0, c0, 5;get core ID */ + {0xe7e10050 },/* ubfx r0, r0, #0, #2 ;extract LSB */ + {0xe59f5014 },/* ldr r5, =0x400000CC ;load mbox base */ + {0xe320f001 },/* 1: yield */ + {0xe7953200 },/* ldr r3, [r5, r0, lsl #4] ;read mbox for our core */ + {0xe3530000 },/* cmp r3, #0 ;spin while zero */ + {0x0afffffb },/* beq 1b */ + {0xe7853200 },/* str r3, [r5, r0, lsl #4] ;clear mbox */ + {0xe12fff13 },/* bx r3 ;jump to target */ + {0x400000cc },/* (constant: mailbox 3 read/clear base) */ + {0 ,FIXUP_TERMINATOR } }; +static const uint32_t fixupcontext[FIXUP_MAX ] = {0 };/* check that we don't overrun board setup vectors */ QEMU_BUILD_BUG_ON (SMPBOOT_ADDR +sizeof (smpboot) >MVBAR_ADDR );
@@ -145,9 +148,8 @@ static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)QEMU_BUILD_BUG_ON ((BOARDSETUP_ADDR &0xf ) !=0 || (BOARDSETUP_ADDR >>4 ) >=0x100 ); -rom_add_blob_fixed_as ("raspi_smpboot" , smpboot,sizeof (smpboot), - info->smp_loader_start, -arm_boot_address_space (cpu, info)); +arm_write_bootloader ("raspi_smpboot" ,arm_boot_address_space (cpu, info), + info->smp_loader_start, smpboot, fixupcontext); }static void write_smpboot64 (ARMCPU *cpu,const struct arm_boot_info *info)
@@ -161,26 +163,28 @@ static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info)* the primary CPU goes into the kernel. We put these variables inside * a rom blob, so that the reset for ROM contents zeroes them for us. */ -static const uint32_t smpboot[] = { -0xd2801b05 ,/* mov x5, 0xd8 */ -0xd53800a6 ,/* mrs x6, mpidr_el1 */ -0x924004c6 ,/* and x6, x6, #0x3 */ -0xd503205f ,/* spin: wfe */ -0xf86678a4 ,/* ldr x4, [x5,x6,lsl #3] */ -0xb4ffffc4 ,/* cbz x4, spin */ -0xd2800000 ,/* mov x0, #0x0 */ -0xd2800001 ,/* mov x1, #0x0 */ -0xd2800002 ,/* mov x2, #0x0 */ -0xd2800003 ,/* mov x3, #0x0 */ -0xd61f0080 ,/* br x4 */ +static const ARMInsnFixup smpboot[] = { + {0xd2801b05 },/* mov x5, 0xd8 */ + {0xd53800a6 },/* mrs x6, mpidr_el1 */ + {0x924004c6 },/* and x6, x6, #0x3 */ + {0xd503205f },/* spin: wfe */ + {0xf86678a4 },/* ldr x4, [x5,x6,lsl #3] */ + {0xb4ffffc4 },/* cbz x4, spin */ + {0xd2800000 },/* mov x0, #0x0 */ + {0xd2800001 },/* mov x1, #0x0 */ + {0xd2800002 },/* mov x2, #0x0 */ + {0xd2800003 },/* mov x3, #0x0 */ + {0xd61f0080 },/* br x4 */ + {0 ,FIXUP_TERMINATOR } }; +static const uint32_t fixupcontext[FIXUP_MAX ] = {0 };static const uint64_t spintables[] = {0 ,0 ,0 ,0 }; -rom_add_blob_fixed_as ("raspi_smpboot" , smpboot,sizeof (smpboot), - info->smp_loader_start, as); +arm_write_bootloader ("raspi_smpboot" , as, info->smp_loader_start, + smpboot, fixupcontext);rom_add_blob_fixed_as ("raspi_spintables" , spintables,sizeof (spintables),SPINTABLE_ADDR , as); }
Signature · 1 line
2.34.1
Quoted 5 lines · Peter Maydell
Both the raspi and aspeed boards load their secondary CPU bootloader code in a way that only works on little-endian hosts. This patchset fixes that by making them both use the
write_bootloader()function inboot.c, which gets endianness-handling right.
Quoted 7 lines
Patches 1 and 2 are essentially a patch from Cédric from a few months ago:
https://patchew.org/QEMU/[email protected]/[email protected]/
I've split it into two patches and tweaked it a bit.These fixes let us run the avocado tests for these boards on big-endian hosts.
LGTM, the aspeed tests ran fine on a ppc64/debian host (pseries VM).
Thanks,
C.
Quoted 16 lines
thanks
-- PMMCédric Le Goater (2):
hw/arm/boot: Makewrite_bootloader()public asarm_write_bootloader()hw/arm/aspeed: Usearm_write_bootloader()to write the bootloaderPeter Maydell (1):
hw/arm/raspi: Usearm_write_bootloader()to write boot codeinclude/hw/arm/boot.h | 49 +++++++++++++++++++++++++++++++++ hw/arm/aspeed.c | 38 +++++++++++++------------ hw/arm/boot.c | 35 ++++++----------------- hw/arm/raspi.c | 64 +++++++++++++++++++++++-------------------- 4 files changed, 111 insertions(+), 75 deletions(-)
Quoted 4 lines
Cédric Le Goater (2):
hw/arm/boot: Makewrite_bootloader()public asarm_write_bootloader()hw/arm/aspeed: Usearm_write_bootloader()to write the bootloader
Quoted 8 lines
Peter Maydell (1):
hw/arm/raspi: Usearm_write_bootloader()to write boot codeinclude/hw/arm/boot.h | 49 +++++++++++++++++++++++++++++++++ hw/arm/aspeed.c | 38 +++++++++++++------------ hw/arm/boot.c | 35 ++++++----------------- hw/arm/raspi.c | 64 +++++++++++++++++++++++-------------------- 4 files changed, 111 insertions(+), 75 deletions(-)
Peter Maydell
AppliedQuoted 6 lines · Peter Maydell
Both the raspi and aspeed boards load their secondary CPU bootloader code in a way that only works on little-endian hosts. This patchset fixes that by making them both use the
write_bootloader()function inboot.c, which gets endianness-handling right.
Quoted 7 lines
Patches 1 and 2 are essentially a patch from Cédric from a few months ago:
https://patchew.org/QEMU/[email protected]/[email protected]/
I've split it into two patches and tweaked it a bit.These fixes let us run the avocado tests for these boards on big-endian hosts.
Applied to target-arm.next, thanks.
-- PMM