Skip to contentSkip to conversation

xen: arm: rework placement of fdt in initial dom0 memory map

PATCH Superseded Ian Campbell 2 participants 5 messages Sep 13, 2013 – Sep 17, 2013

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen: arm: rework placement of fdt in initial dom0 memory map

The 32-bit Linux kernel uses its lowmem direct mapping to access the FDT. The lowmem mapping is around 0.75GiB but varies depending on the kernel's .config. Our current scheme of loading the FDT as high as 4GB therefore fails with larger amounts of dom0 RAM.

The upstream documentation has recently been update to provide more guidance
<http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=7824/1>. In accordance with this load the kernel just below 128MiB (aligned to 2MB) and the FDT just above, or if there is less RAM available then as high as possible.

---
v2: Use the kernels own reported size rather than the size of the module. This
is consistent with the amount we will actually copy (zimage.len)
v1: This is technically v2 of "xen: arm: load FDT below 0.5G"

---
 xen/arch/arm/domain_build.c |   16 +++++++++++-----
 xen/arch/arm/kernel.c       |   27 ++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 8 deletions(-)
xen/arch/arm/domain_build.c +11 -5
index 77e2a61..172c985 100644
@@ -503,14 +503,20 @@ static int prepare_dtb(struct domain *d, struct kernel_info *kinfo)
     if ( ret < 0 )
         goto err;
 
+    /* Actual new size */
+    new_size = fdt_totalsize(kinfo->fdt);
+
     /*
-     * DTB must be load below 4GiB and far enough from linux (Linux uses
-     * the space after it to decompress)
-     * Load the DTB at the end of the first bank, while ensuring it is
-     * also below 4G
+     * DTB must be loaded such that it does not conflict with the
+     * kernel decompressor. For 32-bit Linux Documentation/arm/Booting
+     * recommends just after the 128MB boundary while for 64-bit Linux
+     * the recommendation in Documentation/arm64/booting.txt is below
+     * 512MB. Place at 128MB, (or, if we have less RAM, as high as
+     * possible) in order to satisfy both.
      */
     end = kinfo->mem.bank[0].start + kinfo->mem.bank[0].size;
-    end = MIN(1ull << 32, end);
+    end = MIN(kinfo->mem.bank[0].start + (128<<20) + new_size, end);
+
     kinfo->dtb_paddr = end - fdt_totalsize(kinfo->fdt);
     /* Align the address to 2Mb. Linux only requires 4 byte alignment */
     kinfo->dtb_paddr &= ~((2 << 20) - 1);
xen/arch/arm/kernel.c +24 -3
index f12f895..e4c0981 100644
@@ -211,11 +211,32 @@ static int kernel_try_zimage32_prepare(struct kernel_info *info,
     info->zimage.kernel_addr = addr;
 
     /*
-     * If start is zero, the zImage is position independent -- load it
-     * at 32k from start of RAM.
+     * If start is zero, the zImage is position independent, in this
+     * case Documentation/arm/Booting recommends loading below 128MiB
+     * and above 32MiB. Load it as high as possible within these
+     * constraints, while also avoiding the DTB.
      */
     if (start == 0)
-        info->zimage.load_addr = info->mem.bank[0].start + 0x8000;
+    {
+        paddr_t load_end;
+
+        load_end = info->mem.bank[0].start + info->mem.bank[0].size;
+        load_end = MIN(info->mem.bank[0].start + (128<<20), load_end);
+
+        /*
+         * FDT is loaded above 128M or as high as possible, so the
+         * only way we can clash is if we have <=128MB, in which case
+         * FDT will be right at the end and so dtb_paddr will be below
+         * the proposed kernel load address. Move the kernel down if
+         * necessary.
+         */
+        if ( load_end >= info->dtb_paddr )
+            load_end = info->dtb_paddr;
+
+        info->zimage.load_addr = load_end - end + start;
+        /* Align to 2MB */
+        info->zimage.load_addr &= ~((2 << 20) - 1);
+    }
     else
         info->zimage.load_addr = start;
     info->zimage.len = end - start;
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 39 lines · Ian Campbell

The 32-bit Linux kernel uses its lowmem direct mapping to access the FDT. The lowmem mapping is around 0.75GiB but varies depending on the kernel's .config. Our current scheme of loading the FDT as high as 4GB therefore fails with larger amounts of dom0 RAM.

The upstream documentation has recently been update to provide more guidance
<http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=7824/1>. In accordance with this load the kernel just below 128MiB (aligned to 2MB) and the FDT just above, or if there is less RAM available then as high as possible.

---
v2: Use the kernels own reported size rather than the size of the module. This
is consistent with the amount we will actually copy (zimage.len)
v1: This is technically v2 of "xen: arm: load FDT below 0.5G"

---
 xen/arch/arm/domain_build.c |   16 +++++++++++-----
 xen/arch/arm/kernel.c       |   27 ++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 8 deletions(-)
xen/arch/arm/domain_build.c +5 -4
index 77e2a61..172c985 100644
@@ -503,14 +503,20 @@ static int prepare_dtb(struct domain *d, struct kernel_info *kinfo)
     if ( ret < 0 )
         goto err;
 
+    /* Actual new size */
+    new_size = fdt_totalsize(kinfo->fdt);
+
     /*
-     * DTB must be load below 4GiB and far enough from linux (Linux uses
-     * the space after it to decompress)
-     * Load the DTB at the end of the first bank, while ensuring it is
-     * also below 4G
+     * DTB must be loaded such that it does not conflict with the
+     * kernel decompressor. For 32-bit Linux Documentation/arm/Booting

xen/arch/arm/domain_build.c static int prepare_dtb(struct domain *d, struct kernel_info *kinfo) · line 520

Quoted 10 lines
xen/arch/arm/domain_build.c +6 -1
+     * recommends just after the 128MB boundary while for 64-bit Linux
+     * the recommendation in Documentation/arm64/booting.txt is below
+     * 512MB. Place at 128MB, (or, if we have less RAM, as high as
+     * possible) in order to satisfy both.
      */
     end = kinfo->mem.bank[0].start + kinfo->mem.bank[0].size;
-    end = MIN(1ull << 32, end);
+    end = MIN(kinfo->mem.bank[0].start + (128<<20) + new_size, end);
+
     kinfo->dtb_paddr = end - fdt_totalsize(kinfo->fdt);

Can you use new_size here?

xen/arch/arm/domain_build.c static int prepare_dtb(struct domain *d, struct kernel_info *kinfo) · line 522

Quoted 2 lines
xen/arch/arm/domain_build.c +0 -0
     /* Align the address to 2Mb. Linux only requires 4 byte alignment */
     kinfo->dtb_paddr &= ~((2 << 20) - 1);

I have noticed that the check below is wrong
if ( fdt_totalsize(...) > end )

Can you fix the check in this patch?

Quoted 24 lines
xen/arch/arm/kernel.c +11 -3
index f12f895..e4c0981 100644
@@ -211,11 +211,32 @@ static int kernel_try_zimage32_prepare(struct kernel_info *info,
     info->zimage.kernel_addr = addr;
 
     /*
-     * If start is zero, the zImage is position independent -- load it
-     * at 32k from start of RAM.
+     * If start is zero, the zImage is position independent, in this
+     * case Documentation/arm/Booting recommends loading below 128MiB
+     * and above 32MiB. Load it as high as possible within these
+     * constraints, while also avoiding the DTB.
      */
     if (start == 0)
-        info->zimage.load_addr = info->mem.bank[0].start + 0x8000;
+    {
+        paddr_t load_end;
+
+        load_end = info->mem.bank[0].start + info->mem.bank[0].size;
+        load_end = MIN(info->mem.bank[0].start + (128<<20), load_end);
+
+        /*

xen/arch/arm/kernel.c static int kernel_try_zimage32_prepare(struct kernel_info *info, · line 236

Quoted 10 lines
xen/arch/arm/kernel.c +10 -0
+         * FDT is loaded above 128M or as high as possible, so the
+         * only way we can clash is if we have <=128MB, in which case
+         * FDT will be right at the end and so dtb_paddr will be below
+         * the proposed kernel load address. Move the kernel down if
+         * necessary.
+         */
+        if ( load_end >= info->dtb_paddr )
+            load_end = info->dtb_paddr;
+
+        info->zimage.load_addr = load_end - end + start;

Actually start is always equals to 0, so you don't need to add it.

In the future, we will need some check here to verify the kernel belongs to bank 0 and start won't.

Quoted 7 lines
xen/arch/arm/kernel.c +3 -0
+        /* Align to 2MB */
+        info->zimage.load_addr &= ~((2 << 20) - 1);
+    }
     else
         info->zimage.load_addr = start;
     info->zimage.len = end - start;
Signature · 1 line
Julien Grall
#2 lore raw
Quoted 44 lines · Julien Grall
Quoted 43 lines · Ian Campbell

The 32-bit Linux kernel uses its lowmem direct mapping to access the FDT. The lowmem mapping is around 0.75GiB but varies depending on the kernel's .config. Our current scheme of loading the FDT as high as 4GB therefore fails with larger amounts of dom0 RAM.

The upstream documentation has recently been update to provide more guidance
<http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=7824/1>. In accordance with this load the kernel just below 128MiB (aligned to 2MB) and the FDT just above, or if there is less RAM available then as high as possible.

---
v2: Use the kernels own reported size rather than the size of the module. This
is consistent with the amount we will actually copy (zimage.len)
v1: This is technically v2 of "xen: arm: load FDT below 0.5G"

---
 xen/arch/arm/domain_build.c |   16 +++++++++++-----
 xen/arch/arm/kernel.c       |   27 ++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 8 deletions(-)
xen/arch/arm/domain_build.c +9 -4
index 77e2a61..172c985 100644
@@ -503,14 +503,20 @@ static int prepare_dtb(struct domain *d, struct kernel_info *kinfo)
     if ( ret < 0 )
         goto err;
 
+    /* Actual new size */
+    new_size = fdt_totalsize(kinfo->fdt);
+
     /*
-     * DTB must be load below 4GiB and far enough from linux (Linux uses
-     * the space after it to decompress)
-     * Load the DTB at the end of the first bank, while ensuring it is
-     * also below 4G
+     * DTB must be loaded such that it does not conflict with the
+     * kernel decompressor. For 32-bit Linux Documentation/arm/Booting
+     * recommends just after the 128MB boundary while for 64-bit Linux
+     * the recommendation in Documentation/arm64/booting.txt is below
+     * 512MB. Place at 128MB, (or, if we have less RAM, as high as
+     * possible) in order to satisfy both.
Quoted 8 lines

xen/arch/arm/domain_build.c static int prepare_dtb(struct domain *d, struct kernel_info *kinfo) · line 520

Quoted 6 lines
xen/arch/arm/domain_build.c +2 -1
      */
     end = kinfo->mem.bank[0].start + kinfo->mem.bank[0].size;
-    end = MIN(1ull << 32, end);
+    end = MIN(kinfo->mem.bank[0].start + (128<<20) + new_size, end);
+
     kinfo->dtb_paddr = end - fdt_totalsize(kinfo->fdt);

Can you use new_size here?

Yes.

Quoted 8 lines

xen/arch/arm/domain_build.c static int prepare_dtb(struct domain *d, struct kernel_info *kinfo) · line 522

Quoted 3 lines
xen/arch/arm/domain_build.c +0 -0
     /* Align the address to 2Mb. Linux only requires 4 byte alignment */
     kinfo->dtb_paddr &= ~((2 << 20) - 1);

I have noticed that the check below is wrong
if ( fdt_totalsize(...) > end )

Can you fix the check in this patch?

What's wrong with it?

if ( fdt_totalsize(...) > end ) then the dtb_paddr will have underflowed and we panic. Or is that not what you are referring to?

Quoted 29 lines
Quoted 29 lines
xen/arch/arm/kernel.c +15 -3
index f12f895..e4c0981 100644
@@ -211,11 +211,32 @@ static int kernel_try_zimage32_prepare(struct kernel_info *info,
     info->zimage.kernel_addr = addr;
 
     /*
-     * If start is zero, the zImage is position independent -- load it
-     * at 32k from start of RAM.
+     * If start is zero, the zImage is position independent, in this
+     * case Documentation/arm/Booting recommends loading below 128MiB
+     * and above 32MiB. Load it as high as possible within these
+     * constraints, while also avoiding the DTB.
      */
     if (start == 0)
-        info->zimage.load_addr = info->mem.bank[0].start + 0x8000;
+    {
+        paddr_t load_end;
+
+        load_end = info->mem.bank[0].start + info->mem.bank[0].size;
+        load_end = MIN(info->mem.bank[0].start + (128<<20), load_end);
+
+        /*
+         * FDT is loaded above 128M or as high as possible, so the
+         * only way we can clash is if we have <=128MB, in which case
+         * FDT will be right at the end and so dtb_paddr will be below
+         * the proposed kernel load address. Move the kernel down if
Quoted 8 lines

xen/arch/arm/kernel.c static int kernel_try_zimage32_prepare(struct kernel_info *info, · line 236

Quoted 6 lines
xen/arch/arm/kernel.c +6 -0
+         * necessary.
+         */
+        if ( load_end >= info->dtb_paddr )
+            load_end = info->dtb_paddr;
+
+        info->zimage.load_addr = load_end - end + start;

Actually start is always equals to 0, so you don't need to add it.

Oh yes.

Quoted 13 lines

In the future, we will need some check here to verify the kernel belongs to bank 0 and start won't.

Quoted 7 lines
xen/arch/arm/kernel.c +3 -0
+        /* Align to 2MB */
+        info->zimage.load_addr &= ~((2 << 20) - 1);
+    }
     else
         info->zimage.load_addr = start;
     info->zimage.len = end - start;
#3 lore raw
Quoted 63 lines · Ian Campbell
Quoted 44 lines · Julien Grall
Quoted 43 lines · Ian Campbell

The 32-bit Linux kernel uses its lowmem direct mapping to access the FDT. The lowmem mapping is around 0.75GiB but varies depending on the kernel's .config. Our current scheme of loading the FDT as high as 4GB therefore fails with larger amounts of dom0 RAM.

The upstream documentation has recently been update to provide more guidance
<http://www.arm.linux.org.uk/developer/patches/viewpatch.php?id=7824/1>. In accordance with this load the kernel just below 128MiB (aligned to 2MB) and the FDT just above, or if there is less RAM available then as high as possible.

---
v2: Use the kernels own reported size rather than the size of the module. This
is consistent with the amount we will actually copy (zimage.len)
v1: This is technically v2 of "xen: arm: load FDT below 0.5G"

---
 xen/arch/arm/domain_build.c |   16 +++++++++++-----
 xen/arch/arm/kernel.c       |   27 ++++++++++++++++++++++++---
 2 files changed, 35 insertions(+), 8 deletions(-)
xen/arch/arm/domain_build.c +9 -4
index 77e2a61..172c985 100644
@@ -503,14 +503,20 @@ static int prepare_dtb(struct domain *d, struct kernel_info *kinfo)
     if ( ret < 0 )
         goto err;
 
+    /* Actual new size */
+    new_size = fdt_totalsize(kinfo->fdt);
+
     /*
-     * DTB must be load below 4GiB and far enough from linux (Linux uses
-     * the space after it to decompress)
-     * Load the DTB at the end of the first bank, while ensuring it is
-     * also below 4G
+     * DTB must be loaded such that it does not conflict with the
+     * kernel decompressor. For 32-bit Linux Documentation/arm/Booting
+     * recommends just after the 128MB boundary while for 64-bit Linux
+     * the recommendation in Documentation/arm64/booting.txt is below
+     * 512MB. Place at 128MB, (or, if we have less RAM, as high as
+     * possible) in order to satisfy both.
Quoted 8 lines

xen/arch/arm/domain_build.c static int prepare_dtb(struct domain *d, struct kernel_info *kinfo) · line 520

Quoted 6 lines
xen/arch/arm/domain_build.c +2 -1
      */
     end = kinfo->mem.bank[0].start + kinfo->mem.bank[0].size;
-    end = MIN(1ull << 32, end);
+    end = MIN(kinfo->mem.bank[0].start + (128<<20) + new_size, end);
+
     kinfo->dtb_paddr = end - fdt_totalsize(kinfo->fdt);

Can you use new_size here?

Yes.

Quoted 7 lines

xen/arch/arm/domain_build.c static int prepare_dtb(struct domain *d, struct kernel_info *kinfo) · line 522

Quoted 3 lines
xen/arch/arm/domain_build.c +0 -0
     /* Align the address to 2Mb. Linux only requires 4 byte alignment */
     kinfo->dtb_paddr &= ~((2 << 20) - 1);

I have noticed that the check below is wrong
if ( fdt_totalsize(...) > end )

Quoted 6 lines
Quoted 1 line

Can you fix the check in this patch?

What's wrong with it?

if ( fdt_totalsize(...) > end ) then the dtb_paddr will have underflowed and we panic. Or is that not what you are referring to?

end is an absolute address and fdt_totalsize(...) is relative.

I think the check should be
(fdt_totalsize(...) + mem.bank[0].start) > end

Quoted 56 lines
Quoted 30 lines
Quoted 30 lines
xen/arch/arm/kernel.c +15 -3
index f12f895..e4c0981 100644
@@ -211,11 +211,32 @@ static int kernel_try_zimage32_prepare(struct kernel_info *info,
     info->zimage.kernel_addr = addr;
 
     /*
-     * If start is zero, the zImage is position independent -- load it
-     * at 32k from start of RAM.
+     * If start is zero, the zImage is position independent, in this
+     * case Documentation/arm/Booting recommends loading below 128MiB
+     * and above 32MiB. Load it as high as possible within these
+     * constraints, while also avoiding the DTB.
      */
     if (start == 0)
-        info->zimage.load_addr = info->mem.bank[0].start + 0x8000;
+    {
+        paddr_t load_end;
+
+        load_end = info->mem.bank[0].start + info->mem.bank[0].size;
+        load_end = MIN(info->mem.bank[0].start + (128<<20), load_end);
+
+        /*
+         * FDT is loaded above 128M or as high as possible, so the
+         * only way we can clash is if we have <=128MB, in which case
+         * FDT will be right at the end and so dtb_paddr will be below
+         * the proposed kernel load address. Move the kernel down if
Quoted 8 lines

xen/arch/arm/kernel.c static int kernel_try_zimage32_prepare(struct kernel_info *info, · line 236

Quoted 6 lines
xen/arch/arm/kernel.c +6 -0
+         * necessary.
+         */
+        if ( load_end >= info->dtb_paddr )
+            load_end = info->dtb_paddr;
+
+        info->zimage.load_addr = load_end - end + start;

Actually start is always equals to 0, so you don't need to add it.

Oh yes.

Quoted 13 lines

In the future, we will need some check here to verify the kernel belongs to bank 0 and start won't.

Quoted 7 lines
xen/arch/arm/kernel.c +3 -0
+        /* Align to 2MB */
+        info->zimage.load_addr &= ~((2 << 20) - 1);
+    }
     else
         info->zimage.load_addr = start;
     info->zimage.len = end - start;
Signature · 1 line
Julien Grall
#4 lore raw
Quoted 7 lines · Julien Grall
Quoted 7 lines
Quoted 4 lines

I have noticed that the check below is wrong
if ( fdt_totalsize(...) > end )

Can you fix the check in this patch?

What's wrong with it?

Quoted 7 lines
Quoted 2 lines

if ( fdt_totalsize(...) > end ) then the dtb_paddr will have underflowed and we panic. Or is that not what you are referring to?

end is an absolute address and fdt_totalsize(...) is relative.

I think the check should be
(fdt_totalsize(...) + mem.bank[0].start) > end

I think we also want
dtb_paddr < mem.bank[0].start
to cope with underflow.

Quoted 59 lines
Quoted 57 lines
Quoted 31 lines
Quoted 31 lines
xen/arch/arm/kernel.c +15 -3
index f12f895..e4c0981 100644
@@ -211,11 +211,32 @@ static int kernel_try_zimage32_prepare(struct kernel_info *info,
     info->zimage.kernel_addr = addr;
 
     /*
-     * If start is zero, the zImage is position independent -- load it
-     * at 32k from start of RAM.
+     * If start is zero, the zImage is position independent, in this
+     * case Documentation/arm/Booting recommends loading below 128MiB
+     * and above 32MiB. Load it as high as possible within these
+     * constraints, while also avoiding the DTB.
      */
     if (start == 0)
-        info->zimage.load_addr = info->mem.bank[0].start + 0x8000;
+    {
+        paddr_t load_end;
+
+        load_end = info->mem.bank[0].start + info->mem.bank[0].size;
+        load_end = MIN(info->mem.bank[0].start + (128<<20), load_end);
+
+        /*
+         * FDT is loaded above 128M or as high as possible, so the
+         * only way we can clash is if we have <=128MB, in which case
+         * FDT will be right at the end and so dtb_paddr will be below
+         * the proposed kernel load address. Move the kernel down if
Quoted 8 lines

xen/arch/arm/kernel.c static int kernel_try_zimage32_prepare(struct kernel_info *info, · line 236

Quoted 6 lines
xen/arch/arm/kernel.c +6 -0
+         * necessary.
+         */
+        if ( load_end >= info->dtb_paddr )
+            load_end = info->dtb_paddr;
+
+        info->zimage.load_addr = load_end - end + start;

Actually start is always equals to 0, so you don't need to add it.

Oh yes.

Quoted 13 lines

In the future, we will need some check here to verify the kernel belongs to bank 0 and start won't.

Quoted 7 lines
xen/arch/arm/kernel.c +3 -0
+        /* Align to 2MB */
+        info->zimage.load_addr &= ~((2 << 20) - 1);
+    }
     else
         info->zimage.load_addr = start;
     info->zimage.len = end - start;