From: Andrew Jones Subject: [kvm-unit-tests PATCH 2/4] x86: lib/alloc: move heap management to common code Date: Mon, 31 Oct 2016 19:51:44 +0100 Message-ID: <1477939906-28802-3-git-send-email-drjones@redhat.com> References: <1477939906-28802-1-git-send-email-drjones@redhat.com> Cc: pbonzini@redhat.com, lvivier@redhat.com, thuth@redhat.com To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:47770 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S945372AbcJaSvx (ORCPT ); Mon, 31 Oct 2016 14:51:53 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2825A4F65C for ; Mon, 31 Oct 2016 18:51:53 +0000 (UTC) In-Reply-To: <1477939906-28802-1-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: This allows other arches to use {alloc,free}_page. Signed-off-by: Andrew Jones --- lib/alloc.c | 32 ++++++++++++++++++++++++++++++++ lib/alloc.h | 10 ++++++++++ lib/x86/vm.c | 33 ++------------------------------- x86/Makefile.common | 1 + 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/alloc.c b/lib/alloc.c index a83c8c5db850..7687c703f40b 100644 --- a/lib/alloc.c +++ b/lib/alloc.c @@ -5,6 +5,7 @@ */ #include "alloc.h" #include "asm/spinlock.h" +#include "asm/page.h" #include "asm/io.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -137,6 +138,37 @@ phys_addr_t phys_zalloc(phys_addr_t size) return phys_zalloc_aligned(size, align_min); } +static void *free_head; +void heap_init(void *start, size_t size) +{ + void *p = (void *)ALIGN((unsigned long)start, PAGE_SIZE); + + while (size >= PAGE_SIZE) { + *(void **)p = free_head; + free_head = p; + p += PAGE_SIZE; + size -= PAGE_SIZE; + } +} + +void *alloc_page(void) +{ + void *p; + + if (!free_head) + return NULL; + + p = free_head; + free_head = *(void **)free_head; + return p; +} + +void free_page(void *page) +{ + *(void **)page = free_head; + free_head = page; +} + static void *early_malloc(size_t size) { phys_addr_t addr = phys_alloc_aligned_safe(size, align_min, true); diff --git a/lib/alloc.h b/lib/alloc.h index c12bd15f7afc..08c047f4bcb7 100644 --- a/lib/alloc.h +++ b/lib/alloc.h @@ -115,4 +115,14 @@ extern phys_addr_t phys_zalloc(phys_addr_t size); */ extern void phys_alloc_show(void); +/* + * Heap page allocator + * + * After initializing with heap_init, {alloc,free}_page can be used + * to easily manage pages. + */ +extern void heap_init(void *start, size_t size); +extern void *alloc_page(void); +extern void free_page(void *page); + #endif /* _ALLOC_H_ */ diff --git a/lib/x86/vm.c b/lib/x86/vm.c index f7e778b3779c..bba8ccee7dd0 100644 --- a/lib/x86/vm.c +++ b/lib/x86/vm.c @@ -1,39 +1,10 @@ #include "fwcfg.h" #include "vm.h" #include "libcflat.h" +#include "alloc.h" -static void *free = 0; static void *vfree_top = 0; -static void free_memory(void *mem, unsigned long size) -{ - while (size >= PAGE_SIZE) { - *(void **)mem = free; - free = mem; - mem += PAGE_SIZE; - size -= PAGE_SIZE; - } -} - -void *alloc_page() -{ - void *p; - - if (!free) - return 0; - - p = free; - free = *(void **)free; - - return p; -} - -void free_page(void *page) -{ - *(void **)page = free; - free = page; -} - extern char edata; static unsigned long end_of_memory; @@ -153,7 +124,7 @@ void setup_vm() { assert(!end_of_memory); end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE); - free_memory(&edata, end_of_memory - (unsigned long)&edata); + heap_init(&edata, end_of_memory - (unsigned long)&edata); setup_mmu(end_of_memory); } diff --git a/x86/Makefile.common b/x86/Makefile.common index 356d879a986b..88038e41af60 100644 --- a/x86/Makefile.common +++ b/x86/Makefile.common @@ -3,6 +3,7 @@ all: test_cases cflatobjs += lib/pci.o +cflatobjs += lib/alloc.o cflatobjs += lib/x86/io.o cflatobjs += lib/x86/smp.o cflatobjs += lib/x86/vm.o -- 2.7.4