[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v2 21/22] mini-os: get physical memory map



On HVMlite we have to look at the physical memory map to know which
memory frames are usable.

In order to make life easier we define a dummy memory map for other
domain types (pv and arm) which has just one entry with a maximum
memory size.

Signed-off-by: Juergen Gross <jgross@xxxxxxxx>
Reviewed-by: Samuel Thibault <samuel.thibault@xxxxxxxxxxxx>
---
 arch/arm/mm.c         |  8 ++++++
 arch/x86/mm.c         | 72 ++++++++++++++++++++++++++++++++++++++++++++++
 arch/x86/setup.c      |  1 +
 include/e820.h        | 48 +++++++++++++++++++++++++++++++
 include/x86/arch_mm.h |  4 +++
 mm.c                  | 79 ++++++++++++++++++++++++++++++++-------------------
 6 files changed, 183 insertions(+), 29 deletions(-)
 create mode 100644 include/e820.h

diff --git a/arch/arm/mm.c b/arch/arm/mm.c
index dbde162..8c156c4 100644
--- a/arch/arm/mm.c
+++ b/arch/arm/mm.c
@@ -7,6 +7,14 @@
 #include <lib.h>
 
 uint32_t physical_address_offset;
+struct e820entry e820_map[1] = {
+    {
+        .addr = 0,
+        .size = ULONG_MAX - 1,
+        .type = E820_RAM
+    }
+};
+unsigned e820_entries = 1;
 
 unsigned long allocate_ondemand(unsigned long n, unsigned long alignment)
 {
diff --git a/arch/x86/mm.c b/arch/x86/mm.c
index 762599d..8dd90b8 100644
--- a/arch/x86/mm.c
+++ b/arch/x86/mm.c
@@ -43,6 +43,7 @@
 #include <mini-os/types.h>
 #include <mini-os/lib.h>
 #include <mini-os/xmalloc.h>
+#include <mini-os/e820.h>
 #include <xen/memory.h>
 
 #ifdef MM_DEBUG
@@ -63,6 +64,15 @@ extern char stack[];
 extern void page_walk(unsigned long va);
 
 #ifdef CONFIG_PARAVIRT
+struct e820entry e820_map[1] = {
+    {
+        .addr = 0,
+        .size = ULONG_MAX - 1,
+        .type = E820_RAM
+    }
+};
+unsigned e820_entries = 1;
+
 void arch_mm_preinit(void *p)
 {
     start_info_t *si = p;
@@ -102,10 +112,25 @@ desc_ptr idt_ptr =
     .base = (unsigned long)&idt,
 };
 
+struct e820entry e820_map[E820_MAX];
+unsigned e820_entries;
+
+static char *e820_types[E820_TYPES] = {
+    [E820_RAM]      = "RAM",
+    [E820_RESERVED] = "Reserved",
+    [E820_ACPI]     = "ACPI",
+    [E820_NVS]      = "NVS",
+    [E820_UNUSABLE] = "Unusable",
+    [E820_PMEM]     = "PMEM"
+};
+
 void arch_mm_preinit(void *p)
 {
     long ret;
     domid_t domid = DOMID_SELF;
+    struct xen_memory_map memmap;
+    int i;
+    unsigned long pfn, max = 0;
 
     pt_base = page_table_base;
     first_free_pfn = PFN_UP(to_phys(&_end));
@@ -116,6 +141,53 @@ void arch_mm_preinit(void *p)
         do_exit();
     }
     last_free_pfn = ret;
+
+    memmap.nr_entries = E820_MAX;
+    set_xen_guest_handle(memmap.buffer, e820_map);
+    ret = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
+    if ( ret < 0 )
+    {
+        xprintk("could not get memory map\n");
+        do_exit();
+    }
+    e820_entries = memmap.nr_entries;
+
+    for ( i = 0; i < e820_entries; i++ )
+    {
+        if ( e820_map[i].type != E820_RAM )
+            continue;
+        pfn = (e820_map[i].addr + e820_map[i].size) >> PAGE_SHIFT;
+        if ( pfn > max )
+            max = pfn;
+    }
+
+    if ( max < last_free_pfn )
+        last_free_pfn = max;
+}
+
+void arch_print_memmap(void)
+{
+    int i;
+    unsigned long from, to;
+    char *type;
+    char buf[12];
+
+    printk("Memory map:\n");
+    for ( i = 0; i < e820_entries; i++ )
+    {
+        if ( e820_map[i].type >= E820_TYPES || !e820_types[e820_map[i].type] )
+        {
+            snprintf(buf, sizeof(buf), "%8x", e820_map[i].type);
+            type = buf;
+        }
+        else
+        {
+            type = e820_types[e820_map[i].type];
+        }
+        from = e820_map[i].addr;
+        to = from + e820_map[i].size - 1;
+        printk("%012lx-%012lx: %s\n", from, to, type);
+    }
 }
 #endif
 
diff --git a/arch/x86/setup.c b/arch/x86/setup.c
index f422a96..86955cf 100644
--- a/arch/x86/setup.c
+++ b/arch/x86/setup.c
@@ -151,6 +151,7 @@ static void print_start_of_day(void *p)
     printk("       flags: 0x%x\n", (unsigned int)si->flags);
     printk("    cmd_line: %s\n", cmdline);
     printk("       stack: %p-%p\n", stack, stack + sizeof(stack));
+    arch_print_memmap();
 }
 #endif
 
diff --git a/include/e820.h b/include/e820.h
new file mode 100644
index 0000000..920551c
--- /dev/null
+++ b/include/e820.h
@@ -0,0 +1,48 @@
+/* -*-  Mode:C; c-basic-offset:4; tab-width:4 -*-
+ *
+ * (C) 2016 - Juergen Gross, SUSE Linux GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __E820_HEADER
+#define __E820_HEADER
+
+/* PC BIOS standard E820 types and structure. */
+#define E820_RAM          1
+#define E820_RESERVED     2
+#define E820_ACPI         3
+#define E820_NVS          4
+#define E820_UNUSABLE     5
+#define E820_PMEM         7
+#define E820_TYPES        8
+
+struct __packed e820entry {
+    uint64_t addr;
+    uint64_t size;
+    uint32_t type;
+};
+
+/* Maximum number of entries. */
+#define E820_MAX          128
+
+extern struct e820entry e820_map[];
+extern unsigned e820_entries;
+
+#endif /*__E820_HEADER*/
diff --git a/include/x86/arch_mm.h b/include/x86/arch_mm.h
index 9372f1e..ab8a53e 100644
--- a/include/x86/arch_mm.h
+++ b/include/x86/arch_mm.h
@@ -279,6 +279,10 @@ pgentry_t *need_pgt(unsigned long addr);
 void arch_mm_preinit(void *p);
 unsigned long alloc_virt_kernel(unsigned n_pages);
 
+#ifndef CONFIG_PARAVIRT
+void arch_print_memmap(void);
+#endif
+
 #endif
 
 #endif /* _ARCH_MM_H_ */
diff --git a/mm.c b/mm.c
index c76be7f..932ceeb 100644
--- a/mm.c
+++ b/mm.c
@@ -43,6 +43,7 @@
 #include <mini-os/types.h>
 #include <mini-os/lib.h>
 #include <mini-os/xmalloc.h>
+#include <mini-os/e820.h>
 
 /*********************
  * ALLOCATION BITMAP
@@ -147,10 +148,14 @@ static chunk_head_t  free_tail[FREELIST_SIZE];
  */
 static void init_page_allocator(unsigned long min, unsigned long max)
 {
-    int i;
+    int i, m;
     unsigned long range;
+    unsigned long r_min, r_max;
     chunk_head_t *ch;
     chunk_tail_t *ct;
+
+    printk("MM: Initialise page allocator for %lx(%lx)-%lx(%lx)\n",
+           (u_long)to_virt(min), min, (u_long)to_virt(max), max);
     for ( i = 0; i < FREELIST_SIZE; i++ )
     {
         free_head[i]       = &free_tail[i];
@@ -166,38 +171,57 @@ static void init_page_allocator(unsigned long min, 
unsigned long max)
     mm_alloc_bitmap_size  = round_pgup(mm_alloc_bitmap_size);
     mm_alloc_bitmap = (unsigned long *)to_virt(min);
     min         += mm_alloc_bitmap_size;
-    range        = max - min;
 
     /* All allocated by default. */
     memset(mm_alloc_bitmap, ~0, mm_alloc_bitmap_size);
-    /* Free up the memory we've been given to play with. */
-    map_free(PHYS_PFN(min), range>>PAGE_SHIFT);
 
-    /* The buddy lists are addressed in high memory. */
-    min = (unsigned long) to_virt(min);
-    max = (unsigned long) to_virt(max);
-
-    while ( range != 0 )
+    for ( m = 0; m < e820_entries; m++ )
     {
-        /*
-         * Next chunk is limited by alignment of min, but also
-         * must not be bigger than remaining range.
-         */
-        for ( i = PAGE_SHIFT; (1UL<<(i+1)) <= range; i++ )
-            if ( min & (1UL<<i) ) break;
+        if ( e820_map[m].type != E820_RAM )
+            continue;
+        if ( e820_map[m].addr + e820_map[m].size >= ULONG_MAX )
+            BUG();
 
+        r_min = e820_map[m].addr;
+        r_max = r_min + e820_map[m].size;
+        if ( r_max <= min || r_min >= max )
+            continue;
+        if ( r_min < min )
+            r_min = min;
+        if ( r_max > max )
+            r_max = max;
 
-        ch = (chunk_head_t *)min;
-        min   += (1UL<<i);
-        range -= (1UL<<i);
-        ct = (chunk_tail_t *)min-1;
-        i -= PAGE_SHIFT;
-        ch->level       = i;
-        ch->next        = free_head[i];
-        ch->pprev       = &free_head[i];
-        ch->next->pprev = &ch->next;
-        free_head[i]    = ch;
-        ct->level       = i;
+        printk("    Adding memory range %lx-%lx\n", r_min, r_max);
+
+        /* The buddy lists are addressed in high memory. */
+        r_min = (unsigned long)to_virt(r_min);
+        r_max = (unsigned long)to_virt(r_max);
+        range = r_max - r_min;
+
+        /* Free up the memory we've been given to play with. */
+        map_free(PHYS_PFN(r_min), range >> PAGE_SHIFT);
+
+        while ( range != 0 )
+        {
+            /*
+             * Next chunk is limited by alignment of min, but also
+             * must not be bigger than remaining range.
+             */
+            for ( i = PAGE_SHIFT; (1UL << (i + 1)) <= range; i++ )
+                if ( r_min & (1UL << i) ) break;
+
+            ch = (chunk_head_t *)r_min;
+            r_min += 1UL << i;
+            range -= 1UL << i;
+            ct = (chunk_tail_t *)r_min - 1;
+            i -= PAGE_SHIFT;
+            ch->level       = i;
+            ch->next        = free_head[i];
+            ch->pprev       = &free_head[i];
+            ch->next->pprev = &ch->next;
+            free_head[i]    = ch;
+            ct->level       = i;
+        }
     }
 
     mm_alloc_bitmap_remap();
@@ -377,9 +401,6 @@ void init_mm(void)
     /*
      * now we can initialise the page allocator
      */
-    printk("MM: Initialise page allocator for %lx(%lx)-%lx(%lx)\n",
-           (u_long)to_virt(PFN_PHYS(start_pfn)), (u_long)PFN_PHYS(start_pfn),
-           (u_long)to_virt(PFN_PHYS(max_pfn)), (u_long)PFN_PHYS(max_pfn));
     init_page_allocator(PFN_PHYS(start_pfn), PFN_PHYS(max_pfn));
     printk("MM: done\n");
 
-- 
2.6.6


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.