Skip to contentSkip to conversation

hw/arm/boot: Increase compliance with kernel arm64 boot protocol.

PATCH Superseded Stewart Hildebrand 4 participants 5 messages Oct 15, 2018 – Oct 16, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/boot: Increase compliance with kernel arm64 boot protocol.

"The Image must be placed text_offset bytes from a 2MB aligned base address anywhere in usable system RAM and called there."

For the virt board, we write our startup bootloader at the very bottom of RAM, so that bit can't be used for the image. To avoid overlap in case the image requests to be loaded at an offset smaller than our bootloader, we increment the load offset to the next 2MB.

This fixes a boot failure for Xen AArch64.

---
 hw/arm/boot.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/arm/boot.c +10 -0
index 20c71d7d96..559ddbcd53 100644
@@ -919,6 +919,16 @@ static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
         memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
         if (hdrvals[1] != 0) {
             kernel_load_offset = le64_to_cpu(hdrvals[0]);
+
+            /* For the virt board, we write our startup "bootloader" at the very
+             * bottom of RAM, so that bit can't be used for the image. To avoid
+             * overlap in case the image requests to be loaded at an offset
+             * smaller than our bootloader, we increment the load offset to the
+             * next 2MB.
+             */
+            if (kernel_load_offset < FIXUP_MAX) {
+                kernel_load_offset += 2 << 20;
+            }
         }
     }
 
Signature · 1 line
2.17.1
#1 lore raw

Hi Stewart,

Quoted 22 lines · Stewart Hildebrand

"The Image must be placed text_offset bytes from a 2MB aligned base address anywhere in usable system RAM and called there."

For the virt board, we write our startup bootloader at the very bottom of RAM, so that bit can't be used for the image. To avoid overlap in case the image requests to be loaded at an offset smaller than our bootloader, we increment the load offset to the next 2MB.

This fixes a boot failure for Xen AArch64.

---
 hw/arm/boot.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/arm/boot.c +0 -0
index 20c71d7d96..559ddbcd53 100644
@@ -919,6 +919,16 @@ static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
         memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 929

Quoted 10 lines
hw/arm/boot.c +8 -0
         if (hdrvals[1] != 0) {
             kernel_load_offset = le64_to_cpu(hdrvals[0]);
+
+            /* For the virt board, we write our startup "bootloader" at the very
+             * bottom of RAM, so that bit can't be used for the image. To avoid
+             * overlap in case the image requests to be loaded at an offset
+             * smaller than our bootloader, we increment the load offset to the
+             * next 2MB.
+             */
+            if (kernel_load_offset < FIXUP_MAX) {

I don't understand how this is related to FIXUP_MAX...

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 930

Quoted 1 line
hw/arm/boot.c +1 -0
+                kernel_load_offset += 2 << 20;

You can use += 2 * MiB; which is easier to review.

Quoted 5 lines
hw/arm/boot.c +1 -0
+            }
         }
     }
 
#2 lore raw

Hi Philippe,

Quoted 11 lines · Philippe Mathieu-Daudé

Hi Stewart,

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 929

Quoted 7 lines · Stewart Hildebrand
hw/arm/boot.c +7 -0
+            /* For the virt board, we write our startup "bootloader" at the very
+             * bottom of RAM, so that bit can't be used for the image. To avoid
+             * overlap in case the image requests to be loaded at an offset
+             * smaller than our bootloader, we increment the load offset to the
+             * next 2MB.
+             */
+            if (kernel_load_offset < FIXUP_MAX) {
Quoted 1 line

I don't understand how this is related to FIXUP_MAX...

You're right, my apologies, it's not directly related. write_bootloader() calculates the size of the bootloader like so:

    len = 0;
    while (insns[len].fixup != FIXUP_TERMINATOR) {
        len++;
    }

The size of the bootloader then would be len * sizeof(uint32_t)

It would be nice not to have to repeat that logic in load_aarch64_image(). I'll send out a v2 after I take some time to wrap my head around it...

Quoted 4 lines
Quoted 2 lines

+ kernel_load_offset += 2 << 20;

You can use += 2 * MiB; which is easier to review.

OK, I will include this in v2.

#3 lore raw

On Tue, 16 Oct 2018 01:19:35 +0000
Stewart Hildebrand <[email protected]> wrote:

Hi,

Stewart, thanks a lot for picking this up!

Quoted 25 lines · Philippe Mathieu-Daudé
Quoted 15 lines

Hi Stewart,

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 929

Quoted 11 lines · Stewart Hildebrand

+ /* For the virt board, we write our startup
"bootloader" at the very
+ * bottom of RAM, so that bit can't be used for the
image. To avoid
+ * overlap in case the image requests to be loaded
at an offset
+ * smaller than our bootloader, we increment the
load offset to the

hw/arm/boot.c +3 -0
+             * next 2MB.
+             */
+            if (kernel_load_offset < FIXUP_MAX) {  
Quoted 1 line

I don't understand how this is related to FIXUP_MAX...

You're right, my apologies, it's not directly related. write_bootloader() calculates the size of the bootloader like so:

    len = 0;
    while (insns[len].fixup != FIXUP_TERMINATOR) {
        len++;
    }
Quoted 5 lines

The size of the bootloader then would be len * sizeof(uint32_t)

It would be nice not to have to repeat that logic in load_aarch64_image(). I'll send out a v2 after I take some time to wrap my head around it...

I wonder if this could be done much easier, since TEXT_OFFSET must actually be page aligned. So all we would need is an *upper bound* for the bootloader size, as long as that is below 4K we wouldn't even loose anything.

Cheers,
Andre.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

+ kernel_load_offset += 2 << 20;

You can use += 2 * MiB; which is easier to review.

OK, I will include this in v2.

#4 lore raw
Quoted 34 lines · Andre Przywara

On Tue, 16 Oct 2018 01:19:35 +0000
Stewart Hildebrand <[email protected]> wrote:

Hi,

Stewart, thanks a lot for picking this up!

Quoted 27 lines · Philippe Mathieu-Daudé
Quoted 15 lines

Hi Stewart,

hw/arm/boot.c static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base, · line 929

Quoted 11 lines · Stewart Hildebrand

+ /* For the virt board, we write our startup
"bootloader" at the very
+ * bottom of RAM, so that bit can't be used for the
image. To avoid
+ * overlap in case the image requests to be loaded
at an offset
+ * smaller than our bootloader, we increment the
load offset to the

hw/arm/boot.c +3 -0
+             * next 2MB.
+             */
+            if (kernel_load_offset < FIXUP_MAX) {
Quoted 1 line

I don't understand how this is related to FIXUP_MAX...

You're right, my apologies, it's not directly related. write_bootloader() calculates the size of the bootloader like so:

    len = 0;
    while (insns[len].fixup != FIXUP_TERMINATOR) {
        len++;
    }

The size of the bootloader then would be len * sizeof(uint32_t)

Quoted 8 lines
Quoted 3 lines

It would be nice not to have to repeat that logic in load_aarch64_image(). I'll send out a v2 after I take some time to wrap my head around it...

I wonder if this could be done much easier, since TEXT_OFFSET must actually be page aligned. So all we would need is an *upper bound* for the bootloader size, as long as that is below 4K we wouldn't even loose anything.

Yes, I think it would be fine to just set a max bootloader size of 4K and check against that. (If so, assert() in write_bootloader that we did stay below the max.)

thanks
-- PMM