Skip to contentSkip to conversation

merge x86 vmalloc and arm/powerpc alloc

RFC PATCH In review Paolo Bonzini 2 participants 15 messages Oct 23, 2017 – Oct 24, 2017

Subsystems (2)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover merge x86 vmalloc and arm/powerpc alloc

This series unifies the memory allocators in kvm-unit-tests:

- x86 startup code sets up phys_alloc using the e801 memory map information from multiboot. phys_alloc remains active until the tests call setup_vm. phys_alloc is very simple and is not able to free any memory.

- a new MMU-friendly alloc_ops implementation is provided based on lib/x86/vm.c. The new alloc_ops relies on a physical page allocator (in lib/alloc_page.c), which is initialized from the final phys_alloc state, and a virtual address allocator. The alloc_ops get physical pages and maps them into a contiguous virtual address space range.

The entry points for the architecture-dependent APIs are setup_mmu, install_page and virt_to_pte_phys. lib/x86/vm.c implements them together with other x86-specific functions.

TODO: test ARM and PPC; implement the new API for ARM/PPC/s390 (probably using a soft TLB approach, similar to Linux, for PPC hashed page tables); support sub-page allocation in the new alloc_ops.

Paolo

Paolo Bonzini (10):
lib: simplify alloc_ops
lib: move vmalloc here from x86
lib: move page allocator here from x86
alloc_page: fix off-by-one
x86: leave VM setup out of vtd_init
x86: prepare for extending setup.c
x86: use alloc_phys
alloc: implement free
x86: turn virt_to_phys_cr3 into virt_to_pte_phys
vmalloc: convert to alloc_ops

 arm/Makefile.common      |   2 +-
 lib/alloc.c              | 203 +++++++++++-----------------------------------
 lib/alloc.h              |  90 ++-------------------
 lib/alloc_page.c         | 116 +++++++++++++++++++++++++++
 lib/alloc_page.h         |  16 ++++
 lib/alloc_phys.c         | 132 ++++++++++++++++++++++++++++++
 lib/alloc_phys.h         |  45 +++++++++++
 lib/arm/setup.c          |   1 +
 lib/libcflat.h           |   2 +
 lib/powerpc/setup.c      |   1 +
 lib/vmalloc.c            |  98 +++++++++++++++++++++++
 lib/vmalloc.h            |  11 +++
 lib/x86/asm/page.h       |   4 +
 lib/x86/intel-iommu.c    |   4 +-
 lib/x86/intel-iommu.h    |   1 -
 lib/x86/io.c             |   2 +-
 lib/x86/isr.c            |   5 +-
 lib/x86/setup.c          |  49 +++++++++---
 lib/x86/vm.c             | 204 ++++-------------------------------------------
 lib/x86/vm.h             |  12 ---
 powerpc/Makefile.common  |   2 +-
 s390x/Makefile           |   2 +-
 x86/Makefile.common      |   4 +
 x86/asyncpf.c            |   6 +-
 x86/cstart.S             |   4 +-
 x86/cstart64.S           |   4 +-
 x86/emulator.c           |   2 +
 x86/eventinj.c           |   4 +-
 x86/hypercall.c          |   1 +
 x86/hyperv_clock.c       |   1 +
 x86/hyperv_connections.c |   1 +
 x86/hyperv_stimer.c      |   1 +
 x86/hyperv_synic.c       |   1 +
 x86/intel-iommu.c        |   5 ++
 x86/pmu.c                |   4 +-
 x86/rmap_chain.c         |   1 +
 x86/sieve.c              |   6 +-
 x86/svm.c                |   1 +
 x86/taskswitch.c         |   1 +
 x86/taskswitch2.c        |   1 +
 x86/vmx.c                |   1 +
 x86/vmx_tests.c          |   2 +
 42 files changed, 575 insertions(+), 478 deletions(-)
 create mode 100644 lib/alloc_page.c
 create mode 100644 lib/alloc_page.h
 create mode 100644 lib/alloc_phys.c
 create mode 100644 lib/alloc_phys.h
 create mode 100644 lib/vmalloc.c
 create mode 100644 lib/vmalloc.h
Signature · 1 line
2.14.2
author maintainer
#1 lore raw

PATCH 1/10 lib: simplify alloc_ops

Layer malloc/calloc/free/memalign over a basic allocator. The implementation of high-level routines remains the basic one from before, but the callback interface is reduced to a single function. Since alloc.c now contains the physical address allocator only, rename it.

---
 arm/Makefile.common           |  2 +-
 lib/alloc.h                   | 77 ++++++-------------------------------------
 lib/{alloc.c => alloc_phys.c} | 62 +++-------------------------------
 lib/alloc_phys.h              | 39 ++++++++++++++++++++++
 lib/arm/setup.c               |  1 +
 lib/powerpc/setup.c           |  1 +
 powerpc/Makefile.common       |  2 +-
 s390x/Makefile                |  2 +-
 8 files changed, 59 insertions(+), 127 deletions(-)
 rename lib/{alloc.c => alloc_phys.c} (68%)
 create mode 100644 lib/alloc_phys.h
arm/Makefile.common +1 -1
index 7e5f527..ded620f 100644
@@ -34,7 +34,7 @@ asm-offsets = lib/$(ARCH)/asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
 
 cflatobjs += lib/util.o
-cflatobjs += lib/alloc.o
+cflatobjs += lib/phys_alloc.o
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/pci.o
 cflatobjs += lib/pci-host-generic.o
lib/alloc.h +10 -67

102 lines not on this page · raw message

lib/alloc_phys.c renamed from lib/alloc.c +5 -57

86 lines not on this page · raw message

lib/alloc_phys.h new file +39 -0
new file mode 100644
index 0000000..f09d667
@@ -0,0 +1,39 @@
+#ifndef _ALLOC_PHYS_H_
+#define _ALLOC_PHYS_H_
+/*
+ * phys_alloc is a very simple allocator which allows physical memory
+ * to be partitioned into regions until all memory is allocated.
+ *
+ * Note: This is such a simple allocator that there is no way to free
+ * a region. For more complicated memory management a single region
+ * can be allocated, but then have its memory managed by a more
+ * sophisticated allocator, e.g. a page allocator.
+ *
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include "libcflat.h"
+
+#define DEFAULT_MINIMUM_ALIGNMENT 32
+
+/*
+ * phys_alloc_init creates the initial free memory region of size @size
+ * at @base. The minimum alignment is set to DEFAULT_MINIMUM_ALIGNMENT.
+ */
+extern void phys_alloc_init(phys_addr_t base, phys_addr_t size);
+
+/*
+ * phys_alloc_set_minimum_alignment sets the minimum alignment to
+ * @align.
+ */
+extern void phys_alloc_set_minimum_alignment(phys_addr_t align);
+
+/*
+ * phys_alloc_show outputs all currently allocated regions with the
+ * following format
+ *   <start_addr>-<end_addr> [<USED|FREE>]
+ */
+extern void phys_alloc_show(void);
+
+#endif /* _ALLOC_PHYS_H_ */
lib/arm/setup.c +1 -0
index a0b1795..a014c48 100644
@@ -14,6 +14,7 @@
 #include <libfdt/libfdt.h>
 #include <devicetree.h>
 #include <alloc.h>
+#include <phys_alloc.h>
 #include <argv.h>
 #include <asm/thread_info.h>
 #include <asm/setup.h>
lib/powerpc/setup.c +1 -0
index 20a1e37..c0e46bb 100644
@@ -14,6 +14,7 @@
 #include <libfdt/libfdt.h>
 #include <devicetree.h>
 #include <alloc.h>
+#include <phys_alloc.h>
 #include <argv.h>
 #include <asm/setup.h>
 #include <asm/page.h>
powerpc/Makefile.common +1 -1
index c4df2e9..89789fe 100644
@@ -30,7 +30,7 @@ asm-offsets = lib/$(ARCH)/asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
 
 cflatobjs += lib/util.o
-cflatobjs += lib/alloc.o
+cflatobjs += lib/phys_alloc.o
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/powerpc/io.o
 cflatobjs += lib/powerpc/hcall.o
s390x/Makefile +1 -1
index 573cba2..213abcd 100644
@@ -21,7 +21,7 @@ asm-offsets = lib/$(ARCH)/asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
 
 cflatobjs += lib/util.o
-cflatobjs += lib/alloc.o
+cflatobjs += lib/phys_alloc.o
 cflatobjs += lib/s390x/io.o
 cflatobjs += lib/s390x/stack.o
 cflatobjs += lib/s390x/sclp-ascii.o
Signature · 1 line
2.14.2
author maintainer
#3 lore raw

PATCH 2/10 lib: move vmalloc here from x86

In reply to Paolo Bonzini

For now, vmalloc provides a primitive that allocates contiguous virtual address. Together with a page allocator that allocates single physical memory pages, it will provide an implementation of "morecore" for when an MMU is enabled.

---
 lib/vmalloc.c       | 31 +++++++++++++++++++++++++++++++
 lib/vmalloc.h       |  8 ++++++++
 lib/x86/vm.c        | 25 ++++++-------------------
 lib/x86/vm.h        |  2 --
 x86/Makefile.common |  1 +
 x86/emulator.c      |  1 +
 x86/eventinj.c      |  1 +
 x86/taskswitch.c    |  1 +
 x86/vmx_tests.c     |  1 +
 9 files changed, 50 insertions(+), 21 deletions(-)
 create mode 100644 lib/vmalloc.c
 create mode 100644 lib/vmalloc.h
lib/vmalloc.c new file +31 -0
new file mode 100644
index 0000000..877c883
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012, 2017, Red Hat Inc.
+ *
+ * This allocator provides contiguous physical addresses with page
+ * granularity.
+ */
+
+#include "libcflat.h"
+#include "asm/spinlock.h"
+#include "asm/page.h"
+
+static struct spinlock lock;
+static void *vfree_top = 0;
+
+void *alloc_vpages(ulong nr)
+{
+	spin_lock(&lock);
+	vfree_top -= PAGE_SIZE * nr;
+	spin_unlock(&lock);
+	return vfree_top;
+}
+
+void *alloc_vpage(void)
+{
+    return alloc_vpages(1);
+}
+
+void init_alloc_vpage(void *top)
+{
+    vfree_top = top;
+}
lib/vmalloc.h new file +8 -0
new file mode 100644
index 0000000..2060f3a
@@ -0,0 +1,8 @@
+#ifndef VMALLOC_H
+#define VMALLOC_H 1
+
+void *alloc_vpages(ulong nr);
+void *alloc_vpage(void);
+void init_alloc_vpage(void *top);
+
+#endif
lib/x86/vm.c +6 -19
index 9b5f922..b975a73 100644
@@ -1,9 +1,9 @@
 #include "fwcfg.h"
 #include "vm.h"
 #include "libcflat.h"
+#include "vmalloc.h"
 
 static void *free = 0;
-static void *vfree_top = 0;
 
 static void free_memory(void *mem, unsigned long size)
 {
@@ -271,7 +271,7 @@ static void setup_mmu(unsigned long len)
     /* 0 - 2G memory, 2G-3G valloc area, 3G-4G mmio */
     setup_mmu_range(cr3, 0, len);
     setup_mmu_range(cr3, 3ul << 30, (1ul << 30));
-    vfree_top = (void*)(3ul << 30);
+    init_alloc_vpage((void*)(3ul << 30));
 #endif
 
     write_cr3(virt_to_phys(cr3));
@@ -302,9 +302,8 @@ void *vmalloc(unsigned long size)
     size += sizeof(unsigned long);
 
     size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
-    vfree_top -= size;
-    mem = p = vfree_top;
     pages = size / PAGE_SIZE;
+    mem = p = alloc_vpages(pages);
     while (pages--) {
 	install_page(phys_to_virt(read_cr3()), virt_to_phys(alloc_page()), p);
 	p += PAGE_SIZE;
@@ -336,11 +335,10 @@ void *vmap(unsigned long long phys, unsigned long size)
     unsigned pages;
 
     size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
-    vfree_top -= size;
-    phys &= ~(unsigned long long)(PAGE_SIZE - 1);
-
-    mem = p = vfree_top;
     pages = size / PAGE_SIZE;
+    mem = p = alloc_vpages(pages);
+
+    phys &= ~(unsigned long long)(PAGE_SIZE - 1);
     while (pages--) {
 	install_page(phys_to_virt(read_cr3()), phys, p);
 	phys += PAGE_SIZE;
@@ -348,14 +346,3 @@ void *vmap(unsigned long long phys, unsigned long size)
     }
     return mem;
 }
-
-void *alloc_vpages(ulong nr)
-{
-	vfree_top -= PAGE_SIZE * nr;
-	return vfree_top;
-}
-
-void *alloc_vpage(void)
-{
-    return alloc_vpages(1);
-}
lib/x86/vm.h +0 -2
index 7457b70..543da33 100644
@@ -11,8 +11,6 @@ void setup_5level_page_table();
 void *vmalloc(unsigned long size);
 void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
-void *alloc_vpage(void);
-void *alloc_vpages(ulong nr);
 uint64_t virt_to_phys_cr3(void *mem);
 
 struct pte_search {
x86/Makefile.common +1 -0
index e96812b..f3f3742 100644
@@ -4,6 +4,7 @@ all: directories test_cases
 
 cflatobjs += lib/pci.o
 cflatobjs += lib/pci-edu.o
+cflatobjs += lib/vmalloc.o
 cflatobjs += lib/x86/setup.o
 cflatobjs += lib/x86/io.o
 cflatobjs += lib/x86/smp.o
x86/emulator.c +1 -0
index 8d262d8..4fb0f32 100644
@@ -4,6 +4,7 @@
 #include "desc.h"
 #include "types.h"
 #include "processor.h"
+#include "vmalloc.h"
 
 #define memset __builtin_memset
 #define TESTDEV_IO_PORT 0xe0
x86/eventinj.c +1 -0
index 665eb61..61e1b96 100644
@@ -5,6 +5,7 @@
 #include "isr.h"
 #include "apic.h"
 #include "apic-defs.h"
+#include "vmalloc.h"
 
 #ifdef __x86_64__
 #  define R "r"
x86/taskswitch.c +1 -0
index 01483a1..889831e 100644
@@ -7,6 +7,7 @@
 
 #include "libcflat.h"
 #include "x86/desc.h"
+#include "vmalloc.h"
 
 #define TSS_RETURN		(FIRST_SPARE_SEL)
 
x86/vmx_tests.c +1 -0
index 4a3e94b..d9bbe10 100644
@@ -12,6 +12,7 @@
 #include "desc.h"
 #include "apic.h"
 #include "types.h"
+#include "vmalloc.h"
 
 #define NONCANONICAL            0xaaaaaaaaaaaaaaaaull
 
Signature · 1 line
2.14.2
author maintainer
#4 lore raw

PATCH 3/10 lib: move page allocator here from x86

In reply to Paolo Bonzini

This is another step in porting the x86 (v)malloc implementation to other architectures.

---
 lib/alloc_page.c         | 115 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/alloc_page.h         |  16 +++++++
 lib/x86/intel-iommu.c    |   2 +
 lib/x86/intel-iommu.h    |   1 -
 lib/x86/vm.c             | 108 +-------------------------------------------
 lib/x86/vm.h             |   4 --
 x86/Makefile.common      |   1 +
 x86/emulator.c           |   1 +
 x86/eventinj.c           |   3 +-
 x86/hypercall.c          |   1 +
 x86/hyperv_clock.c       |   1 +
 x86/hyperv_connections.c |   1 +
 x86/hyperv_stimer.c      |   1 +
 x86/hyperv_synic.c       |   1 +
 x86/intel-iommu.c        |   2 +
 x86/rmap_chain.c         |   1 +
 x86/svm.c                |   1 +
 x86/taskswitch2.c        |   1 +
 x86/vmx.c                |   1 +
 x86/vmx_tests.c          |   1 +
 20 files changed, 151 insertions(+), 112 deletions(-)
 create mode 100644 lib/alloc_page.c
 create mode 100644 lib/alloc_page.h
lib/alloc_page.c new file +115 -0
new file mode 100644
index 0000000..b02dfce
@@ -0,0 +1,115 @@
+/*
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ *
+ * This is a simple allocator that provides contiguous physical addresses
+ * with page granularity.
+ */
+#include "libcflat.h"
+#include <asm/page.h>
+#include <asm/io.h>
+
+static void *free = 0;
+
+void free_pages(void *mem, unsigned long size)
+{
+	void *end;
+
+	assert_msg((unsigned long) mem % PAGE_SIZE == 0,
+		   "mem not page aligned: %p", mem);
+
+	assert_msg(size % PAGE_SIZE == 0, "size not page aligned: %#lx", size);
+
+	assert_msg(size == 0 || mem + size > mem,
+		   "mem + size overflow: %p + %#lx", mem, size);
+
+	if (size == 0) {
+		free = NULL;
+		return;
+	}
+
+	free = mem;
+	end = mem + size;
+	while (mem + PAGE_SIZE != end) {
+		*(void **)mem = (mem + PAGE_SIZE);
+		mem += PAGE_SIZE;
+	}
+
+	*(void **)mem = NULL;
+}
+
+void *alloc_page()
+{
+    void *p;
+
+    if (!free)
+	return 0;
+
+    p = free;
+    free = *(void **)free;
+
+    return p;
+}
+
+/*
+ * Allocates (1 << order) physically contiguous and naturally aligned pages.
+ * Returns NULL if there's no memory left.
+ */
+void *alloc_pages(unsigned long order)
+{
+	/* Generic list traversal. */
+	void *prev;
+	void *curr = NULL;
+	void *next = free;
+
+	/* Looking for a run of length (1 << order). */
+	unsigned long run = 0;
+	const unsigned long n = 1ul << order;
+	const unsigned long align_mask = (n << PAGE_SHIFT) - 1;
+	void *run_start = NULL;
+	void *run_prev = NULL;
+	unsigned long run_next_pa = 0;
+	unsigned long pa;
+
+	assert(order < sizeof(unsigned long) * 8);
+
+	for (;;) {
+		prev = curr;
+		curr = next;
+		next = curr ? *((void **) curr) : NULL;
+
+		if (!curr)
+			return 0;
+
+		pa = virt_to_phys(curr);
+
+		if (run == 0) {
+			if (!(pa & align_mask)) {
+				run_start = curr;
+				run_prev = prev;
+				run_next_pa = pa + PAGE_SIZE;
+				run = 1;
+			}
+		} else if (pa == run_next_pa) {
+			run_next_pa += PAGE_SIZE;
+			run += 1;
+		} else {
+			run = 0;
+		}
+
+		if (run == n) {
+			if (run_prev)
+				*((void **) run_prev) = next;
+			else
+				free = next;
+			return run_start;
+		}
+	}
+}
+
+
+void free_page(void *page)
+{
+    *(void **)page = free;
+    free = page;
+}
+
lib/alloc_page.h new file +16 -0
new file mode 100644
index 0000000..1884c7a
@@ -0,0 +1,16 @@
+/*
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ *
+ * This is a simple allocator that provides contiguous physical addresses
+ * with byte granularity.
+ */
+
+#ifndef ALLOC_PAGE_H
+#define ALLOC_PAGE_H 1
+
+void *alloc_page();
+void *alloc_pages(unsigned long order);
+void free_page(void *page);
+void free_pages(void *mem, unsigned long size);
+
+#endif
lib/x86/intel-iommu.c +2 -0
index 9fdbd3b..af92fab 100644
@@ -12,8 +12,10 @@
 
 #include "intel-iommu.h"
 #include "libcflat.h"
+#include "vm.h"
 #include "pci.h"
 #include "atomic.h"
+#include "alloc_page.h"
 
 /*
  * VT-d in QEMU currently only support 39 bits address width, which is
lib/x86/intel-iommu.h +0 -1
index 885be53..05b9744 100644
@@ -16,7 +16,6 @@
 #define __INTEL_IOMMU_H__
 
 #include "libcflat.h"
-#include "vm.h"
 #include "isr.h"
 #include "smp.h"
 #include "desc.h"
lib/x86/vm.c +2 -106

120 lines not on this page · raw message

lib/x86/vm.h +0 -4
index 543da33..4a7888b 100644
@@ -40,10 +40,6 @@ unsigned long *install_pte(unsigned long *cr3,
                            unsigned long pte,
                            unsigned long *pt_page);
 
-void *alloc_page();
-void *alloc_pages(unsigned long order);
-void free_page(void *page);
-
 unsigned long *install_large_page(unsigned long *cr3,unsigned long phys,
                                   void *virt);
 unsigned long *install_page(unsigned long *cr3, unsigned long phys, void *virt);
x86/Makefile.common +1 -0
index f3f3742..fce0801 100644
@@ -5,6 +5,7 @@ all: directories test_cases
 cflatobjs += lib/pci.o
 cflatobjs += lib/pci-edu.o
 cflatobjs += lib/vmalloc.o
+cflatobjs += lib/alloc_page.o
 cflatobjs += lib/x86/setup.o
 cflatobjs += lib/x86/io.o
 cflatobjs += lib/x86/smp.o
x86/emulator.c +1 -0
index 4fb0f32..e6f27cc 100644
@@ -5,6 +5,7 @@
 #include "types.h"
 #include "processor.h"
 #include "vmalloc.h"
+#include "alloc_page.h"
 
 #define memset __builtin_memset
 #define TESTDEV_IO_PORT 0xe0
x86/eventinj.c +2 -1
index 61e1b96..247604c 100644
@@ -1,11 +1,12 @@
 #include "libcflat.h"
-#include "processor.h"
 #include "vm.h"
+#include "processor.h"
 #include "desc.h"
 #include "isr.h"
 #include "apic.h"
 #include "apic-defs.h"
 #include "vmalloc.h"
+#include "alloc_page.h"
 
 #ifdef __x86_64__
 #  define R "r"
x86/hypercall.c +1 -0
index 9380f78..37fec8e 100644
@@ -1,6 +1,7 @@
 #include "libcflat.h"
 #include "vm.h"
 #include "desc.h"
+#include "alloc_page.h"
 
 #define KVM_HYPERCALL_INTEL ".byte 0x0f,0x01,0xc1"
 #define KVM_HYPERCALL_AMD ".byte 0x0f,0x01,0xd9"
x86/hyperv_clock.c +1 -0
index 9497269..b72e357 100644
@@ -4,6 +4,7 @@
 #include "processor.h"
 #include "hyperv.h"
 #include "vm.h"
+#include "alloc_page.h"
 
 #define MAX_CPU 4
 #define TICKS_PER_SEC (1000000000 / 100)
x86/hyperv_connections.c +1 -0
index b061048..5d541c9 100644
@@ -5,6 +5,7 @@
 #include "atomic.h"
 #include "hyperv.h"
 #include "bitops.h"
+#include "alloc_page.h"
 
 #define MAX_CPUS 64
 
x86/hyperv_stimer.c +1 -0
index bd7f966..b6332fd 100644
@@ -9,6 +9,7 @@
 #include "atomic.h"
 #include "hyperv.h"
 #include "asm/barrier.h"
+#include "alloc_page.h"
 
 #define MAX_CPUS 4
 
x86/hyperv_synic.c +1 -0
index 1ccf1a0..9a5f53f 100644
@@ -8,6 +8,7 @@
 #include "smp.h"
 #include "atomic.h"
 #include "hyperv.h"
+#include "alloc_page.h"
 
 #define MAX_CPUS 4
 
x86/intel-iommu.c +2 -0
index 610cc65..82b8234 100644
@@ -13,6 +13,8 @@
 #include "intel-iommu.h"
 #include "pci-edu.h"
 #include "x86/apic.h"
+#include "vm.h"
+#include "alloc_page.h"
 
 #define VTD_TEST_DMAR_4B ("DMAR 4B memcpy test")
 #define VTD_TEST_IR_MSI ("IR MSI")
x86/rmap_chain.c +1 -0
index 7bf6275..97591f9 100644
@@ -4,6 +4,7 @@
 #include "fwcfg.h"
 #include "vm.h"
 #include "smp.h"
+#include "alloc_page.h"
 
 int main (void)
 {
x86/svm.c +1 -0
index 25ac0ce..162632c 100644
@@ -6,6 +6,7 @@
 #include "vm.h"
 #include "smp.h"
 #include "types.h"
+#include "alloc_page.h"
 
 /* for the nested page table*/
 u64 *pml4e;
x86/taskswitch2.c +1 -0
index bb7345b..22f0b6b 100644
@@ -4,6 +4,7 @@
 #include "apic.h"
 #include "processor.h"
 #include "vm.h"
+#include "alloc_page.h"
 
 #define MAIN_TSS_SEL (FIRST_SPARE_SEL + 0)
 #define VM86_TSS_SEL (FIRST_SPARE_SEL + 8)
x86/vmx.c +1 -0
index 786c70f..cc76b5b 100644
@@ -30,6 +30,7 @@
 
 #include "libcflat.h"
 #include "processor.h"
+#include "alloc_page.h"
 #include "vm.h"
 #include "desc.h"
 #include "vmx.h"
x86/vmx_tests.c +1 -0
index d9bbe10..1c9c747 100644
@@ -13,6 +13,7 @@
 #include "apic.h"
 #include "types.h"
 #include "vmalloc.h"
+#include "alloc_page.h"
 
 #define NONCANONICAL            0xaaaaaaaaaaaaaaaaull
 
Signature · 1 line
2.14.2
author maintainer
#5 lore raw

PATCH 4/10 alloc_page: fix off-by-one

In reply to Paolo Bonzini

It is okay to free up to the very last page of virtual address space, in which case mem+size is zero.

---
 lib/alloc_page.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/alloc_page.c +2 -1
index b02dfce..de6fd6f 100644
@@ -19,7 +19,8 @@ void free_pages(void *mem, unsigned long size)
 
 	assert_msg(size % PAGE_SIZE == 0, "size not page aligned: %#lx", size);
 
-	assert_msg(size == 0 || mem + size > mem,
+	assert_msg(size == 0 || (uintptr_t)mem == -size ||
+		   (uintptr_t)mem + size > (uintptr_t)mem,
 		   "mem + size overflow: %p + %#lx", mem, size);
 
 	if (size == 0) {
Signature · 1 line
2.14.2
author maintainer
#6 lore raw

PATCH 5/10 x86: leave VM setup out of vtd_init

In reply to Paolo Bonzini
---
 lib/x86/intel-iommu.c | 4 ----
 x86/intel-iommu.c     | 3 +++
 2 files changed, 3 insertions(+), 4 deletions(-)
lib/x86/intel-iommu.c +0 -4
index af92fab..3f3f211 100644
@@ -12,7 +12,6 @@
 
 #include "intel-iommu.h"
 #include "libcflat.h"
-#include "vm.h"
 #include "pci.h"
 #include "atomic.h"
 #include "alloc_page.h"
@@ -360,9 +359,6 @@ void vtd_setup_ioapic_irq(struct pci_dev *dev, int vector,
 
 void vtd_init(void)
 {
-	setup_vm();
-	smp_init();
-
 	vtd_reg_base = ioremap(Q35_HOST_BRIDGE_IOMMU_ADDR, PAGE_SIZE);
 
 	vtd_dump_init_info();
x86/intel-iommu.c +3 -0
index 82b8234..f24170d 100644
@@ -132,6 +132,9 @@ static void vtd_test_ir(void)
 
 int main(int argc, char *argv[])
 {
+	setup_vm();
+	smp_init();
+
 	vtd_init();
 
 	report_prefix_push("vtd_init");
Signature · 1 line
2.14.2
author maintainer
#7 lore raw

PATCH 6/10 x86: prepare for extending setup.c

In reply to Paolo Bonzini

Rename setup_get_initrd to setup_multiboot as we will get memory map information from there, and since we are at it setup_environ to setup_libcflat.

Use C structs for multiboot information instead of offsets.

---
 lib/x86/setup.c | 45 ++++++++++++++++++++++++++++++++-------------
 x86/cstart.S    |  4 ++--
 x86/cstart64.S  |  4 ++--
 3 files changed, 36 insertions(+), 17 deletions(-)
lib/x86/setup.c +32 -13
index 804ba3a..22257a7 100644
@@ -6,11 +6,32 @@
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include "libcflat.h"
+#include "fwcfg.h"
+#include "alloc_phys.h"
 
-#define MBI_MODS_COUNT	20
-#define MBI_MODS_ADDR	24
-#define MB_MOD_START	 0
-#define MB_MOD_END	 4
+extern char edata;
+
+struct mbi_bootinfo {
+	u32 flags;
+	u32 mem_lower;
+	u32 mem_upper;
+	u32 boot_device;
+	u32 cmdline;
+	u32 mods_count;
+	u32 mods_addr;
+	u32 reserved[5];   /* 28-47 */
+	u32 mmap_addr;
+	u32 reserved0[3];  /* 52-63 */
+	u32 bootloader;
+	u32 reserved1[5];  /* 68-87 */
+	u32 size;
+};
+
+struct mbi_module {
+	u32 start, end;
+	u32 cmdline;
+	u32 unused;
+};
 
 #define ENV_SIZE 16384
 
@@ -21,22 +42,20 @@ u32 initrd_size;
 
 static char env[ENV_SIZE];
 
-void setup_get_initrd(u8 *bootinfo)
+void setup_multiboot(struct mbi_bootinfo *bootinfo)
 {
-	u32 *mods_addr, *mod_start, *mod_end;
+	struct mbi_module *mods;
 
-	if (*((u32 *)&bootinfo[MBI_MODS_COUNT]) != 1)
+	if (bootinfo->mods_count != 1)
 		return;
 
-	mods_addr = (u32 *)&bootinfo[MBI_MODS_ADDR];
-	mod_start = (u32 *)(ulong)(*mods_addr + MB_MOD_START);
-	mod_end = (u32 *)(ulong)(*mods_addr + MB_MOD_END);
+	mods = (struct mbi_module *)(uintptr_t) bootinfo->mods_addr;
 
-	initrd = (char *)(ulong)*mod_start;
-	initrd_size = *mod_end - *mod_start;
+	initrd = (char *)(uintptr_t) mods->start;
+	initrd_size = mods->end - mods->start;
 }
 
-void setup_environ(void)
+void setup_libcflat(void)
 {
 	if (initrd) {
 		/* environ is currently the only file in the initrd */
x86/cstart.S +2 -2
index a3c26a4..143317e 100644
@@ -95,8 +95,8 @@ MSR_GS_BASE = 0xc0000101
 .globl start
 start:
         push %ebx
-        call setup_get_initrd
-        call setup_environ
+        call setup_multiboot
+        call setup_libcflat
         mov mb_cmdline(%ebx), %eax
         mov %eax, __args
         call __setup_args
x86/cstart64.S +2 -2
index 8e4a1f3..21db10f 100644
@@ -227,8 +227,8 @@ start64:
 	call enable_x2apic
 	mov mb_boot_info(%rip), %rbx
 	mov %rbx, %rdi
-	call setup_get_initrd
-	call setup_environ
+	call setup_multiboot
+	call setup_libcflat
 	mov mb_cmdline(%rbx), %eax
 	mov %rax, __args(%rip)
 	call __setup_args
Signature · 1 line
2.14.2
author maintainer
#8 lore raw

PATCH 7/10 x86: use alloc_phys

In reply to Paolo Bonzini

As the next step in making vmalloc/vfree available to every other architecture, register the available memory with phys_alloc for x86 too, and add a service to return the unused phys_alloc region. This makes setup_vm architecture- independent.

---
 lib/alloc_page.c    |  2 +-
 lib/alloc_phys.c    |  6 ++++++
 lib/alloc_phys.h    |  6 ++++++
 lib/libcflat.h      |  2 ++
 lib/vmalloc.c       | 14 ++++++++++++++
 lib/x86/asm/page.h  |  2 ++
 lib/x86/setup.c     |  4 ++++
 lib/x86/vm.c        | 27 ++++++++-------------------
 lib/x86/vm.h        |  1 -
 x86/Makefile.common |  1 +
 10 files changed, 44 insertions(+), 21 deletions(-)
lib/alloc_page.c +1 -1
index de6fd6f..271b8aa 100644
@@ -5,6 +5,7 @@
  * with page granularity.
  */
 #include "libcflat.h"
+#include "alloc_phys.h"
 #include <asm/page.h>
 #include <asm/io.h>
 
@@ -113,4 +114,3 @@ void free_page(void *page)
     *(void **)page = free;
     free = page;
 }
-
lib/alloc_phys.c +6 -0
index 3972277..6befb5c 100644
@@ -107,6 +107,12 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
 	return addr;
 }
 
+void phys_alloc_get_unused(phys_addr_t *p_base, phys_addr_t *p_top)
+{
+	*p_base = base;
+	*p_top = top;
+}
+
 static void *early_memalign(size_t alignment, size_t size)
 {
 	phys_addr_t addr;
lib/alloc_phys.h +6 -0
index f09d667..848d3db 100644
@@ -36,4 +36,10 @@ extern void phys_alloc_set_minimum_alignment(phys_addr_t align);
  */
 extern void phys_alloc_show(void);
 
+/*
+ * phys_alloc_get_unused returns the addresses for the still-unused part
+ * of the initial free memory region passed to phys_alloc_init.
+ */
+extern void phys_alloc_get_unused(phys_addr_t *p_base, phys_addr_t *p_top);
+
 #endif /* _ALLOC_PHYS_H_ */
lib/libcflat.h +2 -0
index a5c4290..fb46541 100644
@@ -156,4 +156,6 @@ static inline bool is_power_of_2(unsigned long n)
 void binstr(unsigned long x, char out[BINSTR_SZ]);
 void print_binstr(unsigned long x);
 
+extern void setup_vm(void);
+
 #endif
lib/vmalloc.c +14 -0
index 877c883..c2abe3c 100644
@@ -8,9 +8,12 @@
 #include "libcflat.h"
 #include "asm/spinlock.h"
 #include "asm/page.h"
+#include "alloc_phys.h"
+#include "alloc_page.h"
 
 static struct spinlock lock;
 static void *vfree_top = 0;
+static void *page_root;
 
 void *alloc_vpages(ulong nr)
 {
@@ -29,3 +32,14 @@ void init_alloc_vpage(void *top)
 {
     vfree_top = top;
 }
+
+void setup_vm()
+{
+    phys_addr_t base, top;
+    phys_alloc_get_unused(&base, &top);
+    base = (base + PAGE_SIZE - 1) & -PAGE_SIZE;
+    top = top & -PAGE_SIZE;
+
+    free_pages((void *)base, top - base);
+    page_root = setup_mmu(top);
+}
lib/x86/asm/page.h +2 -0
index 562594d..154d45e 100644
@@ -46,5 +46,7 @@
 #define PGDIR_BITS(lvl)        (((lvl) - 1) * PGDIR_WIDTH + PAGE_SHIFT)
 #define PGDIR_OFFSET(va, lvl)  (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK)
 
+extern void *setup_mmu(unsigned long top);
+
 #endif /* !__ASSEMBLY__ */
 #endif
lib/x86/setup.c +4 -0
index 22257a7..f6a8ea8 100644
@@ -53,6 +53,10 @@ void setup_multiboot(struct mbi_bootinfo *bootinfo)
 
 	initrd = (char *)(uintptr_t) mods->start;
 	initrd_size = mods->end - mods->start;
+
+	/* TODO: use e820 */
+	u64 end_of_memory = bootinfo->mem_upper * 1024ull;
+	phys_alloc_init((uintptr_t) &edata, end_of_memory - (uintptr_t)edata);
 }
 
 void setup_libcflat(void)
lib/x86/vm.c +8 -19
index d2a3c59..f6f20e5 100644
@@ -1,12 +1,8 @@
-#include "fwcfg.h"
 #include "vm.h"
 #include "libcflat.h"
 #include "vmalloc.h"
 #include "alloc_page.h"
 
-extern char edata;
-static unsigned long end_of_memory;
-
 unsigned long *install_pte(unsigned long *cr3,
 			   int pte_level,
 			   void *virt,
@@ -149,23 +145,23 @@ static void setup_mmu_range(unsigned long *cr3, unsigned long start,
 	install_pages(cr3, phys, max - phys, (void *)(ulong)phys);
 }
 
-static void setup_mmu(unsigned long len)
+void *setup_mmu(unsigned long end_of_memory)
 {
     unsigned long *cr3 = alloc_page();
 
     memset(cr3, 0, PAGE_SIZE);
 
 #ifdef __x86_64__
-    if (len < (1ul << 32))
-        len = (1ul << 32);  /* map mmio 1:1 */
+    if (end_of_memory < (1ul << 32))
+        end_of_memory = (1ul << 32);  /* map mmio 1:1 */
 
-    setup_mmu_range(cr3, 0, len);
+    setup_mmu_range(cr3, 0, end_of_memory);
 #else
-    if (len > (1ul << 31))
-	    len = (1ul << 31);
+    if (end_of_memory > (1ul << 31))
+	    end_of_memory = (1ul << 31);
 
     /* 0 - 2G memory, 2G-3G valloc area, 3G-4G mmio */
-    setup_mmu_range(cr3, 0, len);
+    setup_mmu_range(cr3, 0, end_of_memory);
     setup_mmu_range(cr3, 3ul << 30, (1ul << 30));
     init_alloc_vpage((void*)(3ul << 30));
 #endif
@@ -180,14 +176,7 @@ static void setup_mmu(unsigned long len)
     printf("cr0 = %lx\n", read_cr0());
     printf("cr3 = %lx\n", read_cr3());
     printf("cr4 = %lx\n", read_cr4());
-}
-
-void setup_vm()
-{
-    assert(!end_of_memory);
-    end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE);
-    free_pages(&edata, end_of_memory - (unsigned long)&edata);
-    setup_mmu(end_of_memory);
+    return cr3;
 }
 
 void *vmalloc(unsigned long size)
lib/x86/vm.h +0 -1
index 4a7888b..c3211eb 100644
@@ -5,7 +5,6 @@
 #include "asm/page.h"
 #include "asm/io.h"
 
-void setup_vm();
 void setup_5level_page_table();
 
 void *vmalloc(unsigned long size);
x86/Makefile.common +1 -0
index fce0801..f0fbadf 100644
@@ -6,6 +6,7 @@ cflatobjs += lib/pci.o
 cflatobjs += lib/pci-edu.o
 cflatobjs += lib/vmalloc.o
 cflatobjs += lib/alloc_page.o
+cflatobjs += lib/alloc_phys.o
 cflatobjs += lib/x86/setup.o
 cflatobjs += lib/x86/io.o
 cflatobjs += lib/x86/smp.o
Signature · 1 line
2.14.2
author maintainer
#9 lore raw

PATCH 8/10 alloc: implement free

In reply to Paolo Bonzini

Track block size in lib/alloc.c and not in alloc_ops. alloc_ops->free gets the size from the malloc implementation.

---
 lib/alloc.c      | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/alloc.h      | 33 ++++++---------------------
 lib/alloc_phys.c | 23 +++++++++----------
 lib/vmalloc.h    |  1 +
 4 files changed, 88 insertions(+), 38 deletions(-)
 create mode 100644 lib/alloc.c
lib/alloc.c new file +69 -0
new file mode 100644
index 0000000..7ce61d2
@@ -0,0 +1,69 @@
+#include "alloc.h"
+
+void *malloc(size_t size)
+{
+	size = (size + sizeof(long) - 1) & -sizeof(long);
+	return memalign(sizeof(long), size);
+}
+
+void *calloc(size_t nmemb, size_t size)
+{
+	void *ptr = malloc(nmemb * size);
+	if (ptr)
+		memset(ptr, 0, nmemb * size);
+	return ptr;
+}
+
+#define METADATA_EXTRA	(2 * sizeof(uintptr_t))
+#define OFS_SLACK	(-2 * sizeof(uintptr_t))
+#define OFS_SIZE	(-sizeof(uintptr_t))
+
+static inline void *block_begin(void *mem)
+{
+	uintptr_t slack = *(uintptr_t *)(mem + OFS_SLACK);
+	return mem - slack;
+}
+
+static inline uintptr_t block_size(void *mem)
+{
+	return *(uintptr_t *)(mem + OFS_SIZE);
+}
+
+void free(void *ptr)
+{
+	if (!alloc_ops->free)
+		return;
+
+	uintptr_t base = (uintptr_t) block_begin(ptr);
+	uintptr_t sz = block_size(ptr);
+
+	alloc_ops->free((void *) base, sz);
+}
+
+void *memalign(size_t alignment, size_t size)
+{
+	void *p;
+	uintptr_t blkalign;
+	uintptr_t mem, slack;
+
+	assert(alloc_ops && alloc_ops->memalign);
+	if (alignment <= sizeof(uintptr_t))
+		alignment = sizeof(uintptr_t);
+	else
+		size += alignment - 1;
+
+	blkalign = MAX(alignment, alloc_ops->align_min);
+	p = alloc_ops->memalign(blkalign, size + METADATA_EXTRA);
+
+	/* Leave room for metadata before aligning the result.  */
+	mem = (uintptr_t)p + METADATA_EXTRA;
+	mem = ALIGN(mem, alignment);
+	slack = mem - (uintptr_t)p;
+	size = ALIGN(mem + size - 1, alloc_ops->align_min) - mem;
+
+	/* Write the metadata */
+	*(uintptr_t *)(mem + OFS_SLACK) = slack;
+	*(uintptr_t *)(mem + OFS_SIZE) = size + slack;
+	return mem;
+}
+
lib/alloc.h +7 -26
index 24f85b4..f560c4a 100644
@@ -24,36 +24,17 @@
 
 struct alloc_ops {
 	void *(*memalign)(size_t alignment, size_t size);
+	void (*free)(void *ptr, size_t size);
+	size_t align_min;
 };
 
 extern struct alloc_ops *alloc_ops;
 
-/*
- * Our malloc implementation is currently so simple that it can just
- * be inlined. :)
- */
-static inline void *malloc(size_t size)
-{
-	assert(alloc_ops && alloc_ops->memalign);
-	return alloc_ops->memalign(sizeof(long), size);
-}
-
-static inline void *calloc(size_t nmemb, size_t size)
-{
-	void *ptr = malloc(nmemb * size);
-	if (ptr)
-		memset(ptr, 0, nmemb * size);
-	return ptr;
-}
+void *malloc(size_t size);
+void *calloc(size_t nmemb, size_t size);
+void free(void *ptr);
+void *memalign(size_t alignment, size_t size);
 
-static inline void free(void *ptr)
-{
-}
-
-static inline void *memalign(size_t alignment, size_t size)
-{
-	assert(alloc_ops && alloc_ops->memalign);
-	return alloc_ops->memalign(alignment, size);
-}
+extern struct alloc_ops *alloc_ops;
 
 #endif /* _ALLOC_H_ */
lib/alloc_phys.c +11 -12
index 6befb5c..c570f6e 100644
@@ -42,16 +42,6 @@ void phys_alloc_show(void)
 	spin_unlock(&lock);
 }
 
-void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
-{
-	spin_lock(&lock);
-	base = base_addr;
-	top = base + size;
-	align_min = DEFAULT_MINIMUM_ALIGNMENT;
-	nr_regions = 0;
-	spin_unlock(&lock);
-}
-
 void phys_alloc_set_minimum_alignment(phys_addr_t align)
 {
 	assert(align && !(align & (align - 1)));
@@ -74,8 +64,6 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
 	if (safe && sizeof(long) == 4)
 		top_safe = MIN(top_safe, 1ULL << 32);
 
-	align = MAX(align, align_min);
-
 	addr = ALIGN(base, align);
 	size += addr - base;
 
@@ -131,3 +119,14 @@ static struct alloc_ops early_alloc_ops = {
 };
 
 struct alloc_ops *alloc_ops = &early_alloc_ops;
+
+void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
+{
+	spin_lock(&lock);
+	base = base_addr;
+	top = base + size;
+	align_min = DEFAULT_MINIMUM_ALIGNMENT;
+	early_alloc_ops.align_min = align_min;
+	nr_regions = 0;
+	spin_unlock(&lock);
+}
lib/vmalloc.h +1 -0
index 2060f3a..d4dd880 100644
@@ -4,5 +4,6 @@
 void *alloc_vpages(ulong nr);
 void *alloc_vpage(void);
 void init_alloc_vpage(void *top);
+void setup_vm();
 
 #endif
Signature · 1 line
2.14.2
author maintainer
#11 lore raw

PATCH 9/10 x86: turn virt_to_phys_cr3 into virt_to_pte_phys

In reply to Paolo Bonzini

For consistency with install_page, make it accept a root page table pointer.

---
 lib/x86/asm/page.h | 1 +
 lib/x86/vm.c       | 4 ++--
 lib/x86/vm.h       | 1 -
 x86/asyncpf.c      | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
lib/x86/asm/page.h +1 -0
index 154d45e..1e6d1a2 100644
@@ -47,6 +47,7 @@
 #define PGDIR_OFFSET(va, lvl)  (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK)
 
 extern void *setup_mmu(unsigned long top);
+phys_addr_t virt_to_pte_phys(void *cr3, void *virt);
 
 #endif /* !__ASSEMBLY__ */
 #endif
lib/x86/vm.c +2 -2
index f6f20e5..9dd7d79 100644
@@ -198,9 +198,9 @@ void *vmalloc(unsigned long size)
     return mem;
 }
 
-uint64_t virt_to_phys_cr3(void *mem)
+phys_addr_t virt_to_pte_phys(void *cr3, void *mem)
 {
-    return (*get_pte(phys_to_virt(read_cr3()), mem) & PT_ADDR_MASK) + ((ulong)mem & (PAGE_SIZE - 1));
+    return (*get_pte(cr3, mem) & PT_ADDR_MASK) + ((ulong)mem & (PAGE_SIZE - 1));
 }
 
 void vfree(void *mem)
lib/x86/vm.h +0 -1
index c3211eb..9f668c6 100644
@@ -10,7 +10,6 @@ void setup_5level_page_table();
 void *vmalloc(unsigned long size);
 void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
-uint64_t virt_to_phys_cr3(void *mem);
 
 struct pte_search {
 	int level;
x86/asyncpf.c +1 -1
index f04d7cb..3b5b4b7 100644
@@ -55,7 +55,7 @@ static void pf_isr(struct ex_regs *r)
 			report("unexpected #PF at %#lx", false, read_cr2());
 			break;
 		case KVM_PV_REASON_PAGE_NOT_PRESENT:
-			phys = virt_to_phys_cr3(virt);
+			phys = virt_to_pte_phys(phys_to_virt(read_cr3()), virt);
 			install_pte(phys_to_virt(read_cr3()), 1, virt, phys, 0);
 			write_cr3(read_cr3());
 			report("Got not present #PF token %lx virt addr %p phys addr %#" PRIx64,
Signature · 1 line
2.14.2
author maintainer
#12 lore raw

PATCH 10/10 vmalloc: convert to alloc_ops

In reply to Paolo Bonzini

The final step moves the vmalloc and vmap implementation to generic code, rewriting vmalloc and vfree as an alloc_ops implementation that is installed by setup_vm.

This way all architectures can benefit from it just by calling setup_vm and providing the implementation of install_page.

---
 lib/alloc.c         |  5 ++---
 lib/alloc.h         |  2 --
 lib/vmalloc.c       | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/vmalloc.h       |  2 ++
 lib/x86/asm/page.h  |  1 +
 lib/x86/io.c        |  2 +-
 lib/x86/isr.c       |  5 +++--
 lib/x86/vm.c        | 50 +-------------------------------------------------
 lib/x86/vm.h        |  4 ----
 x86/Makefile.common |  1 +
 x86/asyncpf.c       |  4 +++-
 x86/pmu.c           |  4 ++--
 x86/sieve.c         |  6 +++---
 13 files changed, 72 insertions(+), 67 deletions(-)
lib/alloc.c +2 -3
index 7ce61d2..9e5ae0a 100644
@@ -1,8 +1,8 @@
 #include "alloc.h"
+#include "asm/page.h"
 
 void *malloc(size_t size)
 {
-	size = (size + sizeof(long) - 1) & -sizeof(long);
 	return memalign(sizeof(long), size);
 }
 
@@ -64,6 +64,5 @@ void *memalign(size_t alignment, size_t size)
 	/* Write the metadata */
 	*(uintptr_t *)(mem + OFS_SLACK) = slack;
 	*(uintptr_t *)(mem + OFS_SIZE) = size + slack;
-	return mem;
+	return (void *)mem;
 }
-
lib/alloc.h +0 -2
index f560c4a..c44d459 100644
@@ -35,6 +35,4 @@ void *calloc(size_t nmemb, size_t size);
 void free(void *ptr);
 void *memalign(size_t alignment, size_t size);
 
-extern struct alloc_ops *alloc_ops;
-
 #endif /* _ALLOC_H_ */
lib/vmalloc.c +53 -0

69 lines not on this page · raw message

lib/vmalloc.h +2 -0
index d4dd880..2a89c33 100644
@@ -6,4 +6,6 @@ void *alloc_vpage(void);
 void init_alloc_vpage(void *top);
 void setup_vm();
 
+void *vmap(phys_addr_t phys, size_t size);
+
 #endif
lib/x86/asm/page.h +1 -0
index 1e6d1a2..1971770 100644
@@ -47,6 +47,7 @@
 #define PGDIR_OFFSET(va, lvl)  (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK)
 
 extern void *setup_mmu(unsigned long top);
+unsigned long *install_page(unsigned long *cr3, phys_addr_t phys, void *virt);
 phys_addr_t virt_to_pte_phys(void *cr3, void *virt);
 
 #endif /* !__ASSEMBLY__ */
lib/x86/io.c +1 -1
index cc5ac58..7e1c16d 100644
@@ -1,8 +1,8 @@
 #include "libcflat.h"
-#include "vm.h"
 #include "smp.h"
 #include "asm/io.h"
 #include "asm/page.h"
+#include "vmalloc.h"
 #ifndef USE_SERIAL
 #define USE_SERIAL
 #endif
lib/x86/isr.c +3 -2
index 9b1d505..53c0c2b 100644
@@ -1,6 +1,7 @@
 #include "libcflat.h"
+#include "processor.h"
 #include "isr.h"
-#include "vm.h"
+#include "alloc.h"
 #include "desc.h"
 
 extern char isr_entry_point[];
@@ -65,7 +66,7 @@ asm (
 
 void handle_irq(unsigned vec, void (*func)(isr_regs_t *regs))
 {
-    u8 *thunk = vmalloc(50);
+    u8 *thunk = malloc(50);
 
     set_idt_entry(vec, thunk, 0);
 
lib/x86/vm.c +1 -49

63 lines not on this page · raw message

lib/x86/vm.h +0 -4
index 9f668c6..9026293 100644
@@ -7,10 +7,6 @@
 
 void setup_5level_page_table();
 
-void *vmalloc(unsigned long size);
-void vfree(void *mem);
-void *vmap(unsigned long long phys, unsigned long size);
-
 struct pte_search {
 	int level;
 	unsigned long *pte;
x86/Makefile.common +1 -0
index f0fbadf..5f7eac4 100644
@@ -4,6 +4,7 @@ all: directories test_cases
 
 cflatobjs += lib/pci.o
 cflatobjs += lib/pci-edu.o
+cflatobjs += lib/alloc.o
 cflatobjs += lib/vmalloc.o
 cflatobjs += lib/alloc_page.o
 cflatobjs += lib/alloc_phys.o
x86/asyncpf.c +3 -1
index 3b5b4b7..0db5732 100644
@@ -22,6 +22,8 @@
 #include "x86/isr.h"
 #include "x86/vm.h"
 
+#include "asm/page.h"
+#include "alloc.h"
 #include "libcflat.h"
 #include <stdint.h>
 
@@ -94,7 +96,7 @@ int main(int ac, char **av)
 	wrmsr(MSR_KVM_ASYNC_PF_EN, virt_to_phys((void*)&apf_reason) |
 			KVM_ASYNC_PF_SEND_ALWAYS | KVM_ASYNC_PF_ENABLED);
 	printf("alloc memory\n");
-	buf = vmalloc(MEM);
+	buf = malloc(MEM);
 	irq_enable();
 	while(loop--) {
 		printf("start loop\n");
x86/pmu.c +2 -2
index b56b61e..12a5880 100644
@@ -5,7 +5,7 @@
 #include "x86/apic.h"
 #include "x86/desc.h"
 #include "x86/isr.h"
-#include "x86/vm.h"
+#include "alloc.h"
 
 #include "libcflat.h"
 #include <stdint.h>
@@ -380,7 +380,7 @@ int main(int ac, char **av)
 	setup_vm();
 	setup_idt();
 	handle_irq(PC_VECTOR, cnt_overflow);
-	buf = vmalloc(N*64);
+	buf = malloc(N*64);
 
 	eax.full = id.a;
 	ebx.full = id.b;
x86/sieve.c +3 -3
index 6cbcd6d..2ee2919 100644
@@ -1,4 +1,4 @@
-#include "vm.h"
+#include "alloc.h"
 #include "libcflat.h"
 
 int sieve(char* data, int size)
@@ -42,9 +42,9 @@ int main()
     setup_vm();
     test_sieve("mapped", static_data, STATIC_SIZE);
     for (i = 0; i < 3; ++i) {
-	v = vmalloc(VSIZE);
+	v = malloc(VSIZE);
 	test_sieve("virtual", v, VSIZE);
-	vfree(v);
+	free(v);
     }
 
     return 0;
Signature · 1 line
2.14.2
#10 lore raw

Re: [PATCH kvm-unit-tests 08/10] alloc: implement free

In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

Track block size in lib/alloc.c and not in alloc_ops. alloc_ops->free gets the size from the malloc implementation.

---
 lib/alloc.c      | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/alloc.h      | 33 ++++++---------------------
 lib/alloc_phys.c | 23 +++++++++----------
 lib/vmalloc.h    |  1 +
 4 files changed, 88 insertions(+), 38 deletions(-)
 create mode 100644 lib/alloc.c

This patch requires adding alloc.o back to arm and powerpc Makefile.common. And one other comment below.

Quoted 64 lines
lib/alloc.c new file +57 -0
new file mode 100644
index 0000000..7ce61d2
@@ -0,0 +1,69 @@
+#include "alloc.h"
+
+void *malloc(size_t size)
+{
+	size = (size + sizeof(long) - 1) & -sizeof(long);
+	return memalign(sizeof(long), size);
+}
+
+void *calloc(size_t nmemb, size_t size)
+{
+	void *ptr = malloc(nmemb * size);
+	if (ptr)
+		memset(ptr, 0, nmemb * size);
+	return ptr;
+}
+
+#define METADATA_EXTRA	(2 * sizeof(uintptr_t))
+#define OFS_SLACK	(-2 * sizeof(uintptr_t))
+#define OFS_SIZE	(-sizeof(uintptr_t))
+
+static inline void *block_begin(void *mem)
+{
+	uintptr_t slack = *(uintptr_t *)(mem + OFS_SLACK);
+	return mem - slack;
+}
+
+static inline uintptr_t block_size(void *mem)
+{
+	return *(uintptr_t *)(mem + OFS_SIZE);
+}
+
+void free(void *ptr)
+{
+	if (!alloc_ops->free)
+		return;
+
+	uintptr_t base = (uintptr_t) block_begin(ptr);
+	uintptr_t sz = block_size(ptr);
+
+	alloc_ops->free((void *) base, sz);
+}
+
+void *memalign(size_t alignment, size_t size)
+{
+	void *p;
+	uintptr_t blkalign;
+	uintptr_t mem, slack;
+
+	assert(alloc_ops && alloc_ops->memalign);
+	if (alignment <= sizeof(uintptr_t))
+		alignment = sizeof(uintptr_t);
+	else
+		size += alignment - 1;
+
+	blkalign = MAX(alignment, alloc_ops->align_min);
+	p = alloc_ops->memalign(blkalign, size + METADATA_EXTRA);
+

lib/alloc.c line 67

Quoted 10 lines
lib/alloc.c new file +10 -0
+	/* Leave room for metadata before aligning the result.  */
+	mem = (uintptr_t)p + METADATA_EXTRA;
+	mem = ALIGN(mem, alignment);
+	slack = mem - (uintptr_t)p;
+	size = ALIGN(mem + size - 1, alloc_ops->align_min) - mem;
+
+	/* Write the metadata */
+	*(uintptr_t *)(mem + OFS_SLACK) = slack;
+	*(uintptr_t *)(mem + OFS_SIZE) = size + slack;
+	return mem;

Need (void *) cast here.

Quoted 110 lines
lib/alloc.c new file +2 -0
+}
+
lib/alloc.h +7 -26
index 24f85b4..f560c4a 100644
@@ -24,36 +24,17 @@
 
 struct alloc_ops {
 	void *(*memalign)(size_t alignment, size_t size);
+	void (*free)(void *ptr, size_t size);
+	size_t align_min;
 };
 
 extern struct alloc_ops *alloc_ops;
 
-/*
- * Our malloc implementation is currently so simple that it can just
- * be inlined. :)
- */
-static inline void *malloc(size_t size)
-{
-	assert(alloc_ops && alloc_ops->memalign);
-	return alloc_ops->memalign(sizeof(long), size);
-}
-
-static inline void *calloc(size_t nmemb, size_t size)
-{
-	void *ptr = malloc(nmemb * size);
-	if (ptr)
-		memset(ptr, 0, nmemb * size);
-	return ptr;
-}
+void *malloc(size_t size);
+void *calloc(size_t nmemb, size_t size);
+void free(void *ptr);
+void *memalign(size_t alignment, size_t size);
 
-static inline void free(void *ptr)
-{
-}
-
-static inline void *memalign(size_t alignment, size_t size)
-{
-	assert(alloc_ops && alloc_ops->memalign);
-	return alloc_ops->memalign(alignment, size);
-}
+extern struct alloc_ops *alloc_ops;
 
 #endif /* _ALLOC_H_ */
lib/alloc_phys.c +11 -12
index 6befb5c..c570f6e 100644
@@ -42,16 +42,6 @@ void phys_alloc_show(void)
 	spin_unlock(&lock);
 }
 
-void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
-{
-	spin_lock(&lock);
-	base = base_addr;
-	top = base + size;
-	align_min = DEFAULT_MINIMUM_ALIGNMENT;
-	nr_regions = 0;
-	spin_unlock(&lock);
-}
-
 void phys_alloc_set_minimum_alignment(phys_addr_t align)
 {
 	assert(align && !(align & (align - 1)));
@@ -74,8 +64,6 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
 	if (safe && sizeof(long) == 4)
 		top_safe = MIN(top_safe, 1ULL << 32);
 
-	align = MAX(align, align_min);
-
 	addr = ALIGN(base, align);
 	size += addr - base;
 
@@ -131,3 +119,14 @@ static struct alloc_ops early_alloc_ops = {
 };
 
 struct alloc_ops *alloc_ops = &early_alloc_ops;
+
+void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
+{
+	spin_lock(&lock);
+	base = base_addr;
+	top = base + size;
+	align_min = DEFAULT_MINIMUM_ALIGNMENT;
+	early_alloc_ops.align_min = align_min;
+	nr_regions = 0;
+	spin_unlock(&lock);
+}
lib/vmalloc.h +1 -0
index 2060f3a..d4dd880 100644
@@ -4,5 +4,6 @@
 void *alloc_vpages(ulong nr);
 void *alloc_vpage(void);
 void init_alloc_vpage(void *top);
+void setup_vm();
 
 #endif
Signature · 1 line
2.14.2

Otherwise looks good to me.

drew

#2 lore raw

Re: [PATCH kvm-unit-tests 01/10] lib: simplify alloc_ops

In reply to Paolo Bonzini

You have some alloc/phys transposes.

Quoted 25 lines · Paolo Bonzini

Layer malloc/calloc/free/memalign over a basic allocator. The implementation of high-level routines remains the basic one from before, but the callback interface is reduced to a single function. Since alloc.c now contains the physical address allocator only, rename it.

---
 arm/Makefile.common           |  2 +-
 lib/alloc.h                   | 77 ++++++-------------------------------------
 lib/{alloc.c => alloc_phys.c} | 62 +++-------------------------------
 lib/alloc_phys.h              | 39 ++++++++++++++++++++++
 lib/arm/setup.c               |  1 +
 lib/powerpc/setup.c           |  1 +
 powerpc/Makefile.common       |  2 +-
 s390x/Makefile                |  2 +-
 8 files changed, 59 insertions(+), 127 deletions(-)
 rename lib/{alloc.c => alloc_phys.c} (68%)
 create mode 100644 lib/alloc_phys.h
arm/Makefile.common +0 -0
index 7e5f527..ded620f 100644

arm/Makefile.common asm-offsets = lib/$(ARCH)/asm-offsets.h · line 37

Quoted 5 lines
arm/Makefile.common +1 -1
@@ -34,7 +34,7 @@ asm-offsets = lib/$(ARCH)/asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
 
 cflatobjs += lib/util.o
-cflatobjs += lib/alloc.o
+cflatobjs += lib/phys_alloc.o

lib/alloc_phys.o

Quoted 258 lines
arm/Makefile.common +0 -0
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/pci.o
 cflatobjs += lib/pci-host-generic.o
lib/alloc.h +10 -67
index 81f5369..24f85b4 100644
@@ -23,33 +23,31 @@
 #include "libcflat.h"
 
 struct alloc_ops {
-	void *(*malloc)(size_t size);
-	void *(*calloc)(size_t nmemb, size_t size);
-	void (*free)(void *ptr);
 	void *(*memalign)(size_t alignment, size_t size);
 };
 
-/*
- * alloc_ops is initialized to early_alloc_ops
- */
 extern struct alloc_ops *alloc_ops;
 
+/*
+ * Our malloc implementation is currently so simple that it can just
+ * be inlined. :)
+ */
 static inline void *malloc(size_t size)
 {
-	assert(alloc_ops && alloc_ops->malloc);
-	return alloc_ops->malloc(size);
+	assert(alloc_ops && alloc_ops->memalign);
+	return alloc_ops->memalign(sizeof(long), size);
 }
 
 static inline void *calloc(size_t nmemb, size_t size)
 {
-	assert(alloc_ops && alloc_ops->calloc);
-	return alloc_ops->calloc(nmemb, size);
+	void *ptr = malloc(nmemb * size);
+	if (ptr)
+		memset(ptr, 0, nmemb * size);
+	return ptr;
 }
 
 static inline void free(void *ptr)
 {
-	assert(alloc_ops && alloc_ops->free);
-	alloc_ops->free(ptr);
 }
 
 static inline void *memalign(size_t alignment, size_t size)
@@ -58,59 +56,4 @@ static inline void *memalign(size_t alignment, size_t size)
 	return alloc_ops->memalign(alignment, size);
 }
 
-/*
- * phys_alloc is a very simple allocator which allows physical memory
- * to be partitioned into regions until all memory is allocated.
- *
- * Note: This is such a simple allocator that there is no way to free
- * a region. For more complicated memory management a single region
- * can be allocated, but then have its memory managed by a more
- * sophisticated allocator, e.g. a page allocator.
- */
-#define DEFAULT_MINIMUM_ALIGNMENT 32
-
-/*
- * phys_alloc_init creates the initial free memory region of size @size
- * at @base. The minimum alignment is set to DEFAULT_MINIMUM_ALIGNMENT.
- */
-extern void phys_alloc_init(phys_addr_t base, phys_addr_t size);
-
-/*
- * phys_alloc_set_minimum_alignment sets the minimum alignment to
- * @align.
- */
-extern void phys_alloc_set_minimum_alignment(phys_addr_t align);
-
-/*
- * phys_alloc_aligned returns the base address of a region of size @size,
- * where the address is aligned to @align, or INVALID_PHYS_ADDR if there
- * isn't enough free memory to satisfy the request.
- */
-extern phys_addr_t phys_alloc_aligned(phys_addr_t size, phys_addr_t align);
-
-/*
- * phys_zalloc_aligned is like phys_alloc_aligned, but zeros the memory
- * before returning the address.
- */
-extern phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align);
-
-/*
- * phys_alloc returns the base address of a region of size @size, or
- * INVALID_PHYS_ADDR if there isn't enough free memory to satisfy the
- * request.
- */
-extern phys_addr_t phys_alloc(phys_addr_t size);
-
-/*
- * phys_zalloc is like phys_alloc, but zeros the memory before returning.
- */
-extern phys_addr_t phys_zalloc(phys_addr_t size);
-
-/*
- * phys_alloc_show outputs all currently allocated regions with the
- * following format
- *   <start_addr>-<end_addr> [<USED|FREE>]
- */
-extern void phys_alloc_show(void);
-
 #endif /* _ALLOC_H_ */
lib/alloc_phys.c renamed from lib/alloc.c +5 -57
similarity index 68%
rename from lib/alloc.c
rename to lib/alloc_phys.c
index d553a7e..3972277 100644
@@ -2,6 +2,9 @@
  * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.
+ *
+ * This is a simple allocator that provides contiguous physical addresses
+ * with byte granularity.
  */
 #include "alloc.h"
 #include "asm/spinlock.h"
@@ -9,6 +12,8 @@
 
 #define PHYS_ALLOC_NR_REGIONS	256
 
+#define DEFAULT_MINIMUM_ALIGNMENT	32
+
 struct phys_alloc_region {
 	phys_addr_t base;
 	phys_addr_t size;
@@ -102,60 +107,6 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
 	return addr;
 }
 
-static phys_addr_t phys_zalloc_aligned_safe(phys_addr_t size,
-					    phys_addr_t align, bool safe)
-{
-	phys_addr_t addr = phys_alloc_aligned_safe(size, align, safe);
-	if (addr == INVALID_PHYS_ADDR)
-		return addr;
-
-	memset(phys_to_virt(addr), 0, size);
-	return addr;
-}
-
-phys_addr_t phys_alloc_aligned(phys_addr_t size, phys_addr_t align)
-{
-	return phys_alloc_aligned_safe(size, align, false);
-}
-
-phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align)
-{
-	return phys_zalloc_aligned_safe(size, align, false);
-}
-
-phys_addr_t phys_alloc(phys_addr_t size)
-{
-	return phys_alloc_aligned(size, align_min);
-}
-
-phys_addr_t phys_zalloc(phys_addr_t size)
-{
-	return phys_zalloc_aligned(size, align_min);
-}
-
-static void *early_malloc(size_t size)
-{
-	phys_addr_t addr = phys_alloc_aligned_safe(size, align_min, true);
-	if (addr == INVALID_PHYS_ADDR)
-		return NULL;
-
-	return phys_to_virt(addr);
-}
-
-static void *early_calloc(size_t nmemb, size_t size)
-{
-	phys_addr_t addr = phys_zalloc_aligned_safe(nmemb * size,
-						    align_min, true);
-	if (addr == INVALID_PHYS_ADDR)
-		return NULL;
-
-	return phys_to_virt(addr);
-}
-
-static void early_free(void *ptr __unused)
-{
-}
-
 static void *early_memalign(size_t alignment, size_t size)
 {
 	phys_addr_t addr;
@@ -170,9 +121,6 @@ static void *early_memalign(size_t alignment, size_t size)
 }
 
 static struct alloc_ops early_alloc_ops = {
-	.malloc = early_malloc,
-	.calloc = early_calloc,
-	.free = early_free,
 	.memalign = early_memalign,
 };
 
lib/alloc_phys.h new file +39 -0
new file mode 100644
index 0000000..f09d667
@@ -0,0 +1,39 @@
+#ifndef _ALLOC_PHYS_H_
+#define _ALLOC_PHYS_H_
+/*
+ * phys_alloc is a very simple allocator which allows physical memory
+ * to be partitioned into regions until all memory is allocated.
+ *
+ * Note: This is such a simple allocator that there is no way to free
+ * a region. For more complicated memory management a single region
+ * can be allocated, but then have its memory managed by a more
+ * sophisticated allocator, e.g. a page allocator.
+ *
+ * Copyright (C) 2014, Red Hat Inc, Andrew Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include "libcflat.h"
+
+#define DEFAULT_MINIMUM_ALIGNMENT 32
+
+/*
+ * phys_alloc_init creates the initial free memory region of size @size
+ * at @base. The minimum alignment is set to DEFAULT_MINIMUM_ALIGNMENT.
+ */
+extern void phys_alloc_init(phys_addr_t base, phys_addr_t size);
+
+/*
+ * phys_alloc_set_minimum_alignment sets the minimum alignment to
+ * @align.
+ */
+extern void phys_alloc_set_minimum_alignment(phys_addr_t align);
+
+/*
+ * phys_alloc_show outputs all currently allocated regions with the
+ * following format
+ *   <start_addr>-<end_addr> [<USED|FREE>]
+ */
+extern void phys_alloc_show(void);
+
+#endif /* _ALLOC_PHYS_H_ */
lib/arm/setup.c +0 -0
index a0b1795..a014c48 100644

lib/arm/setup.c line 17

Quoted 4 lines
lib/arm/setup.c +1 -0
@@ -14,6 +14,7 @@
 #include <libfdt/libfdt.h>
 #include <devicetree.h>
 #include <alloc.h>
+#include <phys_alloc.h>

alloc_phys.h

lib/powerpc/setup.c line 17

Quoted 12 lines
lib/arm/setup.c +0 -0
 #include <argv.h>
 #include <asm/thread_info.h>
 #include <asm/setup.h>
lib/powerpc/setup.c +1 -0
index 20a1e37..c0e46bb 100644
@@ -14,6 +14,7 @@
 #include <libfdt/libfdt.h>
 #include <devicetree.h>
 #include <alloc.h>
+#include <phys_alloc.h>

alloc_phys.h

powerpc/Makefile.common asm-offsets = lib/$(ARCH)/asm-offsets.h · line 33

Quoted 13 lines
lib/powerpc/setup.c +0 -0
 #include <argv.h>
 #include <asm/setup.h>
 #include <asm/page.h>
powerpc/Makefile.common +1 -1
index c4df2e9..89789fe 100644
@@ -30,7 +30,7 @@ asm-offsets = lib/$(ARCH)/asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
 
 cflatobjs += lib/util.o
-cflatobjs += lib/alloc.o
+cflatobjs += lib/phys_alloc.o

alloc_phys.o

s390x/Makefile asm-offsets = lib/$(ARCH)/asm-offsets.h · line 24

Quoted 13 lines
powerpc/Makefile.common +0 -0
 cflatobjs += lib/devicetree.o
 cflatobjs += lib/powerpc/io.o
 cflatobjs += lib/powerpc/hcall.o
s390x/Makefile +1 -1
index 573cba2..213abcd 100644
@@ -21,7 +21,7 @@ asm-offsets = lib/$(ARCH)/asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
 
 cflatobjs += lib/util.o
-cflatobjs += lib/alloc.o
+cflatobjs += lib/phys_alloc.o

alloc_phys.o

Quoted 7 lines
s390x/Makefile +0 -0
 cflatobjs += lib/s390x/io.o
 cflatobjs += lib/s390x/stack.o
 cflatobjs += lib/s390x/sclp-ascii.o
Signature · 1 line
2.14.2

Otherwise looks good to me.

drew

#13 lore raw
In reply to Paolo Bonzini
Quoted 13 lines · Paolo Bonzini

This series unifies the memory allocators in kvm-unit-tests:

- x86 startup code sets up phys_alloc using the e801 memory map information from multiboot. phys_alloc remains active until the tests call setup_vm. phys_alloc is very simple and is not able to free any memory.

- a new MMU-friendly alloc_ops implementation is provided based on lib/x86/vm.c. The new alloc_ops relies on a physical page allocator (in lib/alloc_page.c), which is initialized from the final phys_alloc state, and a virtual address allocator. The alloc_ops get physical pages and maps them into a contiguous virtual address space range.

Quoted 8 lines

The entry points for the architecture-dependent APIs are setup_mmu, install_page and virt_to_pte_phys. lib/x86/vm.c implements them together with other x86-specific functions.

TODO: test ARM and PPC; implement the new API for ARM/PPC/s390 (probably using a soft TLB approach, similar to Linux, for PPC hashed page tables); support sub-page allocation in the new alloc_ops.

Thanks for this Paolo!

I did quick compile and TCG testing of arm64 and ppc64, which led me to a couple comments on a couple patches. I also skimmed over the patches and they look good to me. However, all new code, and any functions that get moved to new files, should have their coding style updated.

drew

author maintainer
#14 lore raw
Quoted 4 lines · Andrew Jones

I did quick compile and TCG testing of arm64 and ppc64, which led me to a couple comments on a couple patches. I also skimmed over the patches and they look good to me. However, all new code, and any functions that get moved to new files, should have their coding style updated.

Ok, will do. I have also ported arm/arm64 in the meanwhile, thus rediscovering all the issues and a few more. :)

Paolo