All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, lvivier@redhat.com, thuth@redhat.com
Subject: [kvm-unit-tests PATCH 4/4] lib/x86/vm: enable malloc and friends
Date: Mon, 31 Oct 2016 19:51:46 +0100	[thread overview]
Message-ID: <1477939906-28802-5-git-send-email-drjones@redhat.com> (raw)
In-Reply-To: <1477939906-28802-1-git-send-email-drjones@redhat.com>

We've had malloc, calloc, free, and memalign available for quite a
while, and arm and powerpc make use of them. x86 hasn't yet, but
let's give it the option. arm and powerpc use the early_alloc_ops,
built on phys_alloc, but x86 already has virtual memory management,
so we can build on that instead.

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 lib/x86/vm.c | 26 +++++++++++++++++++++++++-
 lib/x86/vm.h |  2 +-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/lib/x86/vm.c b/lib/x86/vm.c
index 0882e0a478ca..1e1b74bfb645 100644
--- a/lib/x86/vm.c
+++ b/lib/x86/vm.c
@@ -118,15 +118,39 @@ static void setup_mmu(unsigned long len)
     printf("cr4 = %lx\n", read_cr4());
 }
 
+static void *vcalloc(size_t nmemb, size_t size)
+{
+    return vmalloc(nmemb * size);
+}
+
+static void *vmemalign(size_t alignment, size_t size)
+{
+    size_t size_aligned;
+    void *addr;
+
+    assert(alignment && !(alignment & (alignment - 1)));
+    size_aligned = ALIGN(size, alignment);
+    addr = vmalloc(size_aligned);
+    return addr + (size_aligned - size);
+}
+
+static struct alloc_ops vm_alloc_ops = {
+    .malloc = vmalloc,
+    .calloc = vcalloc,
+    .free = vfree,
+    .memalign = vmemalign,
+};
+
 void setup_vm()
 {
     assert(!end_of_memory);
     end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE);
     heap_init(&edata, end_of_memory - (unsigned long)&edata);
     setup_mmu(end_of_memory);
+    alloc_ops = &vm_alloc_ops;
 }
 
-void *vmalloc(unsigned long size)
+void *vmalloc(size_t size)
 {
     void *mem, *p;
     unsigned pages;
diff --git a/lib/x86/vm.h b/lib/x86/vm.h
index 6a4384f5a48d..3c9a71d03cf9 100644
--- a/lib/x86/vm.h
+++ b/lib/x86/vm.h
@@ -7,7 +7,7 @@
 
 void setup_vm();
 
-void *vmalloc(unsigned long size);
+void *vmalloc(size_t size);
 void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
-- 
2.7.4


  parent reply	other threads:[~2016-10-31 18:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-31 18:51 [kvm-unit-tests PATCH 0/4] x86: enable malloc and friends Andrew Jones
2016-10-31 18:51 ` [kvm-unit-tests PATCH 1/4] lib/alloc: improve robustness Andrew Jones
2016-11-02  6:08   ` Thomas Huth
2016-11-02 10:24     ` Andrew Jones
2016-10-31 18:51 ` [kvm-unit-tests PATCH 2/4] x86: lib/alloc: move heap management to common code Andrew Jones
2016-11-02  6:14   ` Thomas Huth
2016-10-31 18:51 ` [kvm-unit-tests PATCH 3/4] lib/alloc: return a zeroed page from alloc_page Andrew Jones
2016-11-01 14:47   ` Paolo Bonzini
2016-10-31 18:51 ` Andrew Jones [this message]
2016-11-01 14:48   ` [kvm-unit-tests PATCH 4/4] lib/x86/vm: enable malloc and friends Paolo Bonzini
2016-11-01 15:49     ` Andrew Jones
2016-11-01  8:58 ` [kvm-unit-tests PATCH 0/4] x86: " Andrew Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1477939906-28802-5-git-send-email-drjones@redhat.com \
    --to=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.