#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)
{
#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
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;
.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));
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
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
--- /dev/null
+/* -*- 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*/
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_ */
#include <mini-os/types.h>
#include <mini-os/lib.h>
#include <mini-os/xmalloc.h>
+#include <mini-os/e820.h>
/*********************
* ALLOCATION BITMAP
*/
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];
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;
-
-
- 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;
+ 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;
+
+ 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();
/*
* 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");