Skip to contentSkip to conversation

x86/shim: minor fixes to the pv-shim mode

PATCH In review Roger Pau Monne 4 participants 13 messages Dec 28, 2018 – Jan 8, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover x86/shim: minor fixes to the pv-shim mode

Hello,

This series includes some miscellaneous fixes for the pv-shim mode, specially regarding the handling of the memory map.

It can be found on my git branch:

git://xenbits.xen.org/people/royger/xen.git guest-fixes-v2

Thanks, Roger.

Roger Pau Monne (4):
x86/e820: introduce a function to remove ranges from e820 x86/e820: do not fixup memmap in copy_e820_map
x86/e820: assume memmap provided when booted virtualized is correct x86/shim: only mark special pages as RAM in pvshim mode

 xen/arch/x86/e820.c             | 88 +++++++++++++++++++++++++++------
 xen/arch/x86/guest/xen.c        | 42 ----------------
 xen/arch/x86/pv/shim.c          | 43 ++++++++++++++++
 xen/common/page_alloc.c         |  4 +-
 xen/include/asm-x86/e820.h      |  2 +
 xen/include/asm-x86/guest/xen.h | 13 -----
 xen/include/asm-x86/pv/shim.h   | 12 +++++
 7 files changed, 131 insertions(+), 73 deletions(-)
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 1/4 x86/e820: introduce a function to remove ranges from e820

This function is based on the Linux e820__range_remove function, adjusted to fit Xen.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v1:
- Fix one coding style issue.

---
 xen/arch/x86/e820.c        | 57 ++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/e820.h |  2 ++
 2 files changed, 59 insertions(+)
xen/arch/x86/e820.c +57 -0
index 590ea985ef..ba81b2f2dd 100644
@@ -599,6 +599,63 @@ int __init e820_add_range(
     return 1;
 }
 
+uint64_t __init e820_remove_range(struct e820map *e820, uint64_t start,
+                                  uint64_t end, uint32_t type, bool check_type)
+{
+    unsigned int i;
+    uint64_t real_removed_size = 0;
+
+    ASSERT(end > start);
+
+    for ( i = 0; i < e820->nr_map; i++ )
+    {
+        struct e820entry *entry = &e820->map[i];
+        uint64_t final_start, final_end, entry_end;
+
+        if ( check_type && entry->type != type )
+            continue;
+
+        entry_end = entry->addr + entry->size;
+
+        /* Completely covered? */
+        if ( entry->addr >= start && entry_end <= end )
+        {
+            real_removed_size += entry->size;
+            memset(entry, 0, sizeof(*entry));
+            continue;
+        }
+
+        /* Is the new range completely covered? */
+        if ( entry->addr < start && entry_end > end )
+        {
+            e820_add_range(e820, end, entry_end, entry->type);
+            entry->size = start - entry->addr;
+            real_removed_size += end - start;
+            continue;
+        }
+
+        /* Partially covered: */
+        final_start = max(start, entry->addr);
+        final_end = min(end, entry_end);
+        if ( final_start >= final_end )
+            continue;
+
+        real_removed_size += final_end - final_start;
+
+        /*
+         * Left range could be head or tail, so need to update
+         * the size first:
+         */
+        entry->size -= final_end - final_start;
+        if ( entry->addr < final_start )
+            continue;
+
+        entry->addr = final_end;
+    }
+
+    return real_removed_size;
+}
+
 int __init e820_change_range_type(
     struct e820map *e820, uint64_t s, uint64_t e,
     uint32_t orig_type, uint32_t new_type)
xen/include/asm-x86/e820.h +2 -0
index ee317b17aa..f77b821ae9 100644
@@ -31,6 +31,8 @@ extern int e820_change_range_type(
     uint32_t orig_type, uint32_t new_type);
 extern int e820_add_range(
     struct e820map *, uint64_t s, uint64_t e, uint32_t type);
+extern uint64_t e820_remove_range(struct e820map *e820, uint64_t start,
+                                  uint64_t end, uint32_t type, bool check_type);
 extern unsigned long init_e820(const char *, struct e820map *);
 extern struct e820map e820;
 extern struct e820map e820_raw;
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH v2 1/4] x86/e820: introduce a function to remove ranges from e820

Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 13:04, <[email protected]> wrote:
xen/arch/x86/e820.c +13 -0
@@ -599,6 +599,63 @@ int __init e820_add_range(
     return 1;
 }
 
+uint64_t __init e820_remove_range(struct e820map *e820, uint64_t start,
+                                  uint64_t end, uint32_t type, bool check_type)
+{
+    unsigned int i;
+    uint64_t real_removed_size = 0;
+
+    ASSERT(end > start);
+
+    for ( i = 0; i < e820->nr_map; i++ )
+    {
+        struct e820entry *entry = &e820->map[i];
+        uint64_t final_start, final_end, entry_end;
+

xen/arch/x86/e820.c int __init e820_add_range( · line 624

Quoted 10 lines
xen/arch/x86/e820.c +10 -0
+        if ( check_type && entry->type != type )
+            continue;
+
+        entry_end = entry->addr + entry->size;
+
+        /* Completely covered? */
+        if ( entry->addr >= start && entry_end <= end )
+        {
+            real_removed_size += entry->size;
+            memset(entry, 0, sizeof(*entry));

This will break assumptions by other functions, e.g. the neighboring e820_add_range().

xen/arch/x86/e820.c int __init e820_add_range( · line 629

Quoted 5 lines
xen/arch/x86/e820.c +5 -0
+            continue;
+        }
+
+        /* Is the new range completely covered? */
+        if ( entry->addr < start && entry_end > end )

Why < and > instead of <= and >= ? At the very least this is not in line with the comment.

xen/arch/x86/e820.c int __init e820_add_range( · line 641

Quoted 12 lines
xen/arch/x86/e820.c +12 -0
+        {
+            e820_add_range(e820, end, entry_end, entry->type);
+            entry->size = start - entry->addr;
+            real_removed_size += end - start;
+            continue;
+        }
+
+        /* Partially covered: */
+        final_start = max(start, entry->addr);
+        final_end = min(end, entry_end);
+        if ( final_start >= final_end )
+            continue;

Isn't this supposed to be unreachable?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

PATCH v2 2/4 x86/e820: do not fixup memmap in copy_e820_map

In reply to Roger Pau Monne

And instead use the newly introduced e820_remove_range helper to remove any RAM region from the low 1MB VGA/ROM region afterwards.

No functional change intended.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>

---
 xen/arch/x86/e820.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
xen/arch/x86/e820.c +12 -14
index ba81b2f2dd..5b63667087 100644
@@ -320,20 +320,6 @@ static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
         if (start > end)
             return -1;
 
-        /*
-         * Some BIOSes claim RAM in the 640k - 1M region.
-         * Not right. Fix it up.
-         */
-        if (type == E820_RAM) {
-            if (start < 0x100000ULL && end > 0xA0000ULL) {
-                if (start < 0xA0000ULL)
-                    add_memory_region(start, 0xA0000ULL-start, type);
-                if (end <= 0x100000ULL)
-                    continue;
-                start = 0x100000ULL;
-                size = end - start;
-            }
-        }
         add_memory_region(start, size, type);
     } while (biosmap++,--nr_map);
     return 0;
@@ -510,6 +496,12 @@ static void __init reserve_dmi_region(void)
     }
 }
 
+static void __init reserve_vga_region(void)
+{
+    /* Remove any RAM regions from the VGA hole. */
+    e820_remove_range(&e820, KB(640), MB(1) - 1, E820_RAM, true);
+}
+
 static void __init machine_specific_memory_setup(struct e820map *raw)
 {
     unsigned long mpt_limit, ro_mpt_limit;
@@ -545,6 +537,12 @@ static void __init machine_specific_memory_setup(struct e820map *raw)
 
     reserve_dmi_region();
 
+    /*
+     * Some BIOSes claim RAM in the 640k - 1M region.
+     * Not right. Fix it up.
+     */
+    reserve_vga_region();
+
     top_of_ram = mtrr_top_of_ram();
     if ( top_of_ram )
         clip_to_limit(top_of_ram, "MTRRs do not cover all of memory.");
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 2/4
#4 lore raw

Re: [PATCH v2 2/4] x86/e820: do not fixup memmap in copy_e820_map

Quoted 6 lines · Roger Pau Monne

And instead use the newly introduced e820_remove_range helper to remove any RAM region from the low 1MB VGA/ROM region afterwards.

No functional change intended.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [PATCH v2 2/4] x86/e820: do not fixup memmap in copy_e820_map

In reply to Roger Pau Monne

xen/arch/x86/e820.c static void __init reserve_dmi_region(void) · line 502

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 13:04, <[email protected]> wrote:
xen/arch/x86/e820.c +4 -0
@@ -510,6 +496,12 @@ static void __init reserve_dmi_region(void)
     }
 }
 
+static void __init reserve_vga_region(void)
+{
+    /* Remove any RAM regions from the VGA hole. */
+    e820_remove_range(&e820, KB(640), MB(1) - 1, E820_RAM, true);

From looking at patch 1 I got the impression that the "end" parameter is exclusive. If that's not intended to be that way, then there may be further issues with the use of < / <= / > / >= .

I'm also unconvinced of this getting put in a separate function.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

PATCH v2 3/4 x86/e820: assume memmap provided when booted virtualized is correct

In reply to Roger Pau Monne

This implies there's no need to forcefully reserve the VGA MMIO region, since the memory map provided will be correct.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v1:
- Assume the memory map is always correct when booted as a guest under any hypervisor, not only Xen.

---
 xen/arch/x86/e820.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
xen/arch/x86/e820.c +6 -5
index 5b63667087..2407367c57 100644
@@ -537,11 +537,12 @@ static void __init machine_specific_memory_setup(struct e820map *raw)
 
     reserve_dmi_region();
 
-    /*
-     * Some BIOSes claim RAM in the 640k - 1M region.
-     * Not right. Fix it up.
-     */
-    reserve_vga_region();
+    if ( !cpu_has_hypervisor )
+        /*
+         * Some BIOSes claim RAM in the 640k - 1M region.
+         * Not right. Fix it up.
+         */
+        reserve_vga_region();
 
     top_of_ram = mtrr_top_of_ram();
     if ( top_of_ram )
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 3/4
#7 lore raw

Re: [PATCH v2 3/4] x86/e820: assume memmap provided when booted virtualized is correct

Quoted 5 lines · Roger Pau Monne

This implies there's no need to forcefully reserve the VGA MMIO region, since the memory map provided will be correct.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#8 lore raw

Re: [PATCH v2 3/4] x86/e820: assume memmap provided when booted virtualized is correct

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 02.01.19 at 13:44, <[email protected]> wrote:
Quoted 5 lines · Roger Pau Monne

This implies there's no need to forcefully reserve the VGA MMIO region, since the memory map provided will be correct.

once suitably re-based over whatever changes to the earlier patches will be done.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

PATCH v2 4/4 x86/shim: only mark special pages as RAM in pvshim mode

In reply to Roger Pau Monne

When running Xen as a guest it's not necessary to mark such pages as RAM because they won't be assigned to the initial domain memory map.

While there move the functions to the PV shim specific file and rename them accordingly.

No functional change expected.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>

---
 xen/arch/x86/e820.c             |  4 +--
 xen/arch/x86/guest/xen.c        | 42 --------------------------------
 xen/arch/x86/pv/shim.c          | 43 +++++++++++++++++++++++++++++++++
 xen/common/page_alloc.c         |  4 +--
 xen/include/asm-x86/guest/xen.h | 13 ----------
 xen/include/asm-x86/pv/shim.h   | 12 +++++++++
 6 files changed, 59 insertions(+), 59 deletions(-)
xen/arch/x86/e820.c +2 -2
index 2407367c57..32a4e9a57e 100644
@@ -756,8 +756,8 @@ unsigned long __init init_e820(const char *str, struct e820map *raw)
 
     machine_specific_memory_setup(raw);
 
-    if ( xen_guest )
-        hypervisor_fixup_e820(&e820);
+    if ( pv_shim )
+        pv_shim_fixup_e820(&e820);
 
     printk("%s RAM map:\n", str);
     print_e820_memory_map(e820.map, e820.nr_map);
xen/arch/x86/guest/xen.c +0 -42

54 lines not on this page · raw message

xen/arch/x86/pv/shim.c +43 -0

55 lines not on this page · raw message

xen/common/page_alloc.c +2 -2
index e591601f9c..37a52aaa0d 100644
@@ -348,9 +348,9 @@ void __init init_boot_pages(paddr_t ps, paddr_t pe)
         }
     }
 
-    if ( xen_guest )
+    if ( pv_shim )
     {
-        badpage = hypervisor_reserved_pages(&array_size);
+        badpage = pv_shim_reserved_pages(&array_size);
         if ( badpage )
         {
             for ( i = 0; i < array_size; i++ )
xen/include/asm-x86/guest/xen.h +0 -13
index 6f15e24b6b..7e04e4a7ab 100644
@@ -36,8 +36,6 @@ void hypervisor_setup(void);
 void hypervisor_ap_setup(void);
 int hypervisor_alloc_unused_page(mfn_t *mfn);
 int hypervisor_free_unused_page(mfn_t mfn);
-void hypervisor_fixup_e820(struct e820map *e820);
-const struct platform_bad_page *hypervisor_reserved_pages(unsigned int *size);
 uint32_t hypervisor_cpuid_base(void);
 void hypervisor_resume(void);
 
@@ -60,17 +58,6 @@ static inline void hypervisor_ap_setup(void)
     ASSERT_UNREACHABLE();
 }
 
-static inline void hypervisor_fixup_e820(struct e820map *e820)
-{
-    ASSERT_UNREACHABLE();
-}
-
-static inline const struct platform_bad_page *hypervisor_reserved_pages(unsigned int *size)
-{
-    ASSERT_UNREACHABLE();
-    return NULL;
-}
-
 #endif /* CONFIG_XEN_GUEST */
 #endif /* __X86_GUEST_XEN_H__ */
 
xen/include/asm-x86/pv/shim.h +12 -0
index fb739772df..b8818dfde7 100644
@@ -43,6 +43,8 @@ void pv_shim_online_memory(unsigned int nr, unsigned int order);
 void pv_shim_offline_memory(unsigned int nr, unsigned int order);
 domid_t get_initial_domain_id(void);
 uint64_t pv_shim_mem(uint64_t avail);
+void pv_shim_fixup_e820(struct e820map *e820);
+const struct platform_bad_page *pv_shim_reserved_pages(unsigned int *size);
 
 #else
 
@@ -91,6 +93,16 @@ static inline uint64_t pv_shim_mem(uint64_t avail)
     ASSERT_UNREACHABLE();
     return 0;
 }
+static inline void pv_shim_fixup_e820(struct e820map *e820)
+{
+    ASSERT_UNREACHABLE();
+}
+static inline const struct platform_bad_page
+*pv_shim_reserved_pages(unsigned int *s)
+{
+    ASSERT_UNREACHABLE();
+    return NULL;
+}
 
 #endif
 
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

Re: [PATCH v2 4/4] x86/shim: only mark special pages as RAM in pvshim mode

xen/arch/x86/pv/shim.c uint64_t pv_shim_mem(uint64_t avail) · line 125

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 28.12.18 at 13:04, <[email protected]> wrote:
xen/arch/x86/pv/shim.c +8 -0
@@ -113,6 +115,47 @@ uint64_t pv_shim_mem(uint64_t avail)
     return shim_nrpages;
 }
 
+static void __init mark_pfn_as_ram(struct e820map *e820, uint64_t pfn)
+{
+    if ( !e820_add_range(e820, pfn << PAGE_SHIFT,
+                         (pfn << PAGE_SHIFT) + PAGE_SIZE, E820_RAM) )
+        if ( !e820_change_range_type(e820, pfn << PAGE_SHIFT,
+                                     (pfn << PAGE_SHIFT) + PAGE_SIZE,
+                                     E820_RESERVED, E820_RAM) )
+            panic("Unable to add/change memory type of pfn %#lx to RAM\n", pfn);

While doing the move it would have been nice if you folded the two if()-s. (Arguably pfn_to_paddr() could also be used here.)

xen/arch/x86/pv/shim.c uint64_t pv_shim_mem(uint64_t avail) · line 128

Quoted 1 line
xen/arch/x86/pv/shim.c +1 -0
+void pv_shim_fixup_e820(struct e820map *e820)

You've lost the __init here.

xen/include/asm-x86/pv/shim.h static inline uint64_t pv_shim_mem(uint64_t avail) · line 101

Quoted 10 lines
xen/include/asm-x86/pv/shim.h +6 -0
@@ -91,6 +93,16 @@ static inline uint64_t pv_shim_mem(uint64_t avail)
     ASSERT_UNREACHABLE();
     return 0;
 }
+static inline void pv_shim_fixup_e820(struct e820map *e820)
+{
+    ASSERT_UNREACHABLE();
+}
+static inline const struct platform_bad_page
+*pv_shim_reserved_pages(unsigned int *s)

I can see why you split the previously too long line, but surely it shouldn't be split in the middle of the return type.

I've taken the liberty and made all three modifications in preparation for committing.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw
In reply to Roger Pau Monne
Quoted 4 lines · Roger Pau Monne

Hello,

This series includes some miscellaneous fixes for the pv-shim mode, specially regarding the handling of the memory map.

Unfortunately, something overall is now wonky.  With this series applied, I'm getting:

(d9) (XEN) Initial PVH-e820 RAM map:
(d9) (XEN)  0000000000000000 - 00000000fc000000 (usable) (d9) (XEN)  00000000fc000000 - 00000000fc009040 (ACPI data) (d9) (XEN)  00000000feff8000 - 00000000ff000000 (reserved) (d9) (XEN)  0000000100000000 - 0000000104000400 (usable) (d9) (XEN) Checking MTRR ranges...
(d9) (XEN)  MTRR cap: 508 type: 806
(d9) (XEN) PVH-e820 RAM map:
(d9) (XEN)  0000000000000000 - 00000000000a0000 (usable) (d9) (XEN)  00000000fc000000 - 00000000fc009040 (ACPI data) (d9) (XEN)  00000000feff8000 - 00000000ff000000 (reserved) (d9) (XEN)  0000000100000000 - 0000000104000400 (usable)

which a) is trying to account for the legacy range despite starting as a PVH guest.  (I think that perhaps cpu_has_hypervisor doesn't work that early, and I'll try making a fix for it.)

b) The entire remainder of the RAM block which covered the legacy hole got deleted, so Xen failed to relocate itself and panic()'d

c) Xen really oughtn't to relocate itself in cases like this, but that is way out of scope here.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw
Quoted 22 lines · Andrew Cooper
Quoted 4 lines · Roger Pau Monne

Hello,

This series includes some miscellaneous fixes for the pv-shim mode, specially regarding the handling of the memory map.

Unfortunately, something overall is now wonky.  With this series applied, I'm getting:

(d9) (XEN) Initial PVH-e820 RAM map:
(d9) (XEN)  0000000000000000 - 00000000fc000000 (usable) (d9) (XEN)  00000000fc000000 - 00000000fc009040 (ACPI data) (d9) (XEN)  00000000feff8000 - 00000000ff000000 (reserved) (d9) (XEN)  0000000100000000 - 0000000104000400 (usable) (d9) (XEN) Checking MTRR ranges...
(d9) (XEN)  MTRR cap: 508 type: 806
(d9) (XEN) PVH-e820 RAM map:
(d9) (XEN)  0000000000000000 - 00000000000a0000 (usable) (d9) (XEN)  00000000fc000000 - 00000000fc009040 (ACPI data) (d9) (XEN)  00000000feff8000 - 00000000ff000000 (reserved) (d9) (XEN)  0000000100000000 - 0000000104000400 (usable)

Quoted 3 lines

which a) is trying to account for the legacy range despite starting as a PVH guest.  (I think that perhaps cpu_has_hypervisor doesn't work that early, and I'll try making a fix for it.)

I've likely tested an stall version of the shim binary, sorry.

Quoted 2 lines

b) The entire remainder of the RAM block which covered the legacy hole got deleted, so Xen failed to relocate itself and panic()'d

Hm, so the Linux function doesn't seem to work as expected.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel