]> rtime.felk.cvut.cz Git - jailhouse.git/commitdiff
core: Add support for aligned page allocation
authorJan Kiszka <jan.kiszka@siemens.com>
Fri, 17 Jun 2016 12:37:30 +0000 (14:37 +0200)
committerJan Kiszka <jan.kiszka@siemens.com>
Sun, 26 Jun 2016 07:16:28 +0000 (09:16 +0200)
Refactor page_alloc to page_alloc_internal which accepts an additional
constraint for its allocation: align_mask. The allocated region will now
have its start page chosen so that page_number & align_mask is zero. If
no alignment is required, align_mask just needs to be set to 0. This is
what page_alloc exploits.

However, the new function page_alloc_aligned is introduces to return
page regions aligned according to their size (num pages will be aligned
by num * PAGE_SIZE). This implied that num needs to be a power of two.

This will be used on the AArch64 port of Jailhouse to support physical
address ranges from 40 to 44 bits: in these configurations, the initial
page table level may take up multiple consecutive pages.

Based on patch by Antonios Motakis.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
hypervisor/arch/arm/include/asm/paging.h
hypervisor/arch/x86/include/asm/paging.h
hypervisor/include/jailhouse/paging.h
hypervisor/paging.c

index 0372b2c9508d97931c8a895857cde73bc0dac702..28ba3e0aedbac02a146846fd3c84f1e639968836 100644 (file)
@@ -18,7 +18,8 @@
 #include <asm/processor.h>
 #include <asm/sysregs.h>
 
-#define PAGE_SIZE              4096
+#define PAGE_SHIFT             12
+#define PAGE_SIZE              (1 << PAGE_SHIFT)
 #define PAGE_MASK              ~(PAGE_SIZE - 1)
 #define PAGE_OFFS_MASK         (PAGE_SIZE - 1)
 
index e90077bc7ffd144c94b8c0bc532d91e097e09e4e..064790cd0645f525fc08fee9621daeead06b99d1 100644 (file)
@@ -16,7 +16,8 @@
 #include <jailhouse/types.h>
 #include <asm/processor.h>
 
-#define PAGE_SIZE              4096
+#define PAGE_SHIFT             12
+#define PAGE_SIZE              (1 << PAGE_SHIFT)
 #define PAGE_MASK              ~(PAGE_SIZE - 1)
 #define PAGE_OFFS_MASK         (PAGE_SIZE - 1)
 
index 27286f0056514eed6c62d379078c5f1ba6c4a153..6c2555f5fa426c12ebd09d9d0a8a5bedd7ccb867 100644 (file)
@@ -183,6 +183,7 @@ extern struct paging_structures hv_paging_structs;
 unsigned long paging_get_phys_invalid(pt_entry_t pte, unsigned long virt);
 
 void *page_alloc(struct page_pool *pool, unsigned int num);
+void *page_alloc_aligned(struct page_pool *pool, unsigned int num);
 void page_free(struct page_pool *pool, void *first_page, unsigned int num);
 
 /**
index f24d56cf342df2527355f9eea3409e98630e0927..1f228878b6f48ca11d8e52f248f6678c0f12de39 100644 (file)
@@ -89,32 +89,44 @@ static unsigned long find_next_free_page(struct page_pool *pool,
 
 /**
  * Allocate consecutive pages from the specified pool.
- * @param pool Page pool to allocate from.
- * @param num  Number of pages.
+ * @param pool         Page pool to allocate from.
+ * @param num          Number of pages.
+ * @param align_mask   Choose start so that start_page_no & align_mask == 0.
  *
  * @return Pointer to first page or NULL if allocation failed.
  *
  * @see page_free
  */
-void *page_alloc(struct page_pool *pool, unsigned int num)
+static void *page_alloc_internal(struct page_pool *pool, unsigned int num,
+                                unsigned long align_mask)
 {
-       unsigned long start, last, next;
+       /* The pool itself might not be aligned as required. */
+       unsigned long aligned_start =
+               ((unsigned long)pool->base_address >> PAGE_SHIFT) & align_mask;
+       unsigned long next = aligned_start;
+       unsigned long start, last;
        unsigned int allocated;
 
-       start = find_next_free_page(pool, 0);
+restart:
+       /* Forward the search start to the next aligned page. */
+       if ((next - aligned_start) & align_mask)
+               next += num - ((next - aligned_start) & align_mask);
+
+       start = next = find_next_free_page(pool, next);
        if (start == INVALID_PAGE_NR || num == 0)
                return NULL;
 
-restart:
+       /* Enforce alignment (none of align_mask is 0). */
+       if ((start - aligned_start) & align_mask)
+               goto restart;
+
        for (allocated = 1, last = start; allocated < num;
             allocated++, last = next) {
                next = find_next_free_page(pool, last + 1);
                if (next == INVALID_PAGE_NR)
                        return NULL;
-               if (next != last + 1) {
-                       start = next;
-                       goto restart;
-               }
+               if (next != last + 1)
+                       goto restart;   /* not consecutive */
        }
 
        for (allocated = 0; allocated < num; allocated++)
@@ -125,6 +137,34 @@ restart:
        return pool->base_address + start * PAGE_SIZE;
 }
 
+/**
+ * Allocate consecutive pages from the specified pool.
+ * @param pool Page pool to allocate from.
+ * @param num  Number of pages.
+ *
+ * @return Pointer to first page or NULL if allocation failed.
+ *
+ * @see page_free
+ */
+void *page_alloc(struct page_pool *pool, unsigned int num)
+{
+       return page_alloc_internal(pool, num, 0);
+}
+
+/**
+ * Allocate aligned consecutive pages from the specified pool.
+ * @param pool Page pool to allocate from.
+ * @param num  Number of pages. Num needs to be a power of 2.
+ *
+ * @return Pointer to first page or NULL if allocation failed.
+ *
+ * @see page_free
+ */
+void *page_alloc_aligned(struct page_pool *pool, unsigned int num)
+{
+       return page_alloc_internal(pool, num, num - 1);
+}
+
 /**
  * Release pages to the specified pool.
  * @param pool Page pool to release to.