_M procs start up at _start/hart_entry for vcore0
[akaros.git] / kern / src / page_alloc.c
index ed80e07..6531c3a 100644 (file)
 #endif
 
 #include <sys/queue.h>
+#include <arch/bitmask.h>
 #include <page_alloc.h>
 #include <pmap.h>
 #include <string.h>
+#include <kmalloc.h>
 
 #define l1 (available_caches.l1)
 #define l2 (available_caches.l2)
 #define l3 (available_caches.l3)
 
-static void __page_decref(page_t *page);
-static void __page_incref(page_t *page);
+static void __page_decref(page_t *CT(1) page);
+static void __page_incref(page_t *CT(1) page);
 static error_t __page_alloc_specific(page_t** page, size_t ppn);
-static error_t __page_free(page_t* page);
+static error_t __page_free(page_t *CT(1) page);
 
 // Global list of colors allocated to the general purpose memory allocator
-static uint8_t* global_colors_map;
+uint8_t* global_cache_colors_map;
+size_t global_next_color = 0;
+
+void colored_page_alloc_init()
+{
+       global_cache_colors_map = 
+              kmalloc(BYTES_FOR_BITMASK(llc_cache->num_colors), 0);
+       CLR_BITMASK(global_cache_colors_map, llc_cache->num_colors);
+       for(int i = 0; i < llc_cache->num_colors/8; i++)
+               cache_color_alloc(llc_cache, global_cache_colors_map);
+}
 
 /**
  * @brief Clear a Page structure.
@@ -32,41 +44,70 @@ static uint8_t* global_colors_map;
  * The result has null links and 0 refcount.
  * Note that the corresponding physical page is NOT initialized!
  */
-static void page_clear(page_t *SAFE page)
+static void __page_clear(page_t *SAFE page)
 {
        memset(page, 0, sizeof(page_t));
 }
 
-error_t page_alloc_from_color_range(page_t** page,  
-                                    uint16_t base_color,
-                                    uint16_t range) {
-
-       // Find first available color with pages available
-    //  in the proper range
-       int i = base_color;
-       spin_lock_irqsave(&colored_page_free_list_lock);
-       //for(i; i < (base_color+range); i++) {
-       for (i; i < (base_color+range); i++) {
-               if(!LIST_EMPTY(&colored_page_free_list[i]))
-                       break;
-       }
-       // Alocate a page from that color
-       if(i < (base_color+range)) {
-               *page = LIST_FIRST(&colored_page_free_list[i]);
-               LIST_REMOVE(*page, page_link);
-               page_clear(*page);
-               spin_unlock_irqsave(&colored_page_free_list_lock);
-               return ESUCCESS;
-       }
-       spin_unlock_irqsave(&colored_page_free_list_lock);
+#define __PAGE_ALLOC_FROM_RANGE_GENERIC(page, base_color, range, predicate) \
+       /* Find first available color with pages available */                   \
+    /* in the given range */                                                \
+       int i = base_color;                                                     \
+       for (i; i < (base_color+range); i++) {                                  \
+               if((predicate))                                                     \
+                       break;                                                          \
+       }                                                                       \
+       /* Allocate a page from that color */                                   \
+       if(i < (base_color+range)) {                                            \
+               *page = LIST_FIRST(&colored_page_free_list[i]);                     \
+               LIST_REMOVE(*page, page_link);                                      \
+               __page_clear(*page);                                                \
+               return i;                                                           \
+       }                                                                       \
        return -ENOMEM;
+
+static ssize_t __page_alloc_from_color_range(page_t** page,  
+                                           uint16_t base_color,
+                                           uint16_t range) 
+{
+       __PAGE_ALLOC_FROM_RANGE_GENERIC(page, base_color, range, 
+                        !LIST_EMPTY(&colored_page_free_list[i]));
+}
+
+static ssize_t __page_alloc_from_color_map_range(page_t** page, uint8_t* map, 
+                                              size_t base_color, size_t range)
+{  
+       __PAGE_ALLOC_FROM_RANGE_GENERIC(page, base_color, range, 
+                   GET_BITMASK_BIT(map, i) && !LIST_EMPTY(&colored_page_free_list[i]))
+}
+
+static ssize_t __colored_page_alloc(uint8_t* map, page_t** page, 
+                                               size_t next_color)
+{
+       ssize_t ret;
+       if((ret = __page_alloc_from_color_map_range(page, map, 
+                                  next_color, llc_cache->num_colors - next_color)) < 0)
+               ret = __page_alloc_from_color_map_range(page, map, 0, next_color);
+       return ret;
+}
+
+/* Internal version of page_alloc_specific.  Grab the lock first. */
+static error_t __page_alloc_specific(page_t** page, size_t ppn)
+{
+       page_t* sp_page = ppn2page(ppn);
+       if( sp_page->page_ref != 0 )
+               return -ENOMEM;
+       *page = sp_page;
+       LIST_REMOVE(*page, page_link);
+
+       __page_clear(*page);
+       return 0;
 }
 
 /**
- * @brief Allocates a physical page from a pool of unused physical memory
+ * @brief Allocates a physical page from a pool of unused physical memory.
  *
- * Does NOT set the contents of the physical page to zero -
- * the caller must do that if necessary.
+ * Zeroes the page.
  *
  * @param[out] page  set to point to the Page struct
  *                   of the newly allocated page
@@ -74,29 +115,64 @@ error_t page_alloc_from_color_range(page_t** page,
  * @return ESUCCESS on success
  * @return -ENOMEM  otherwise
  */
-error_t page_alloc(page_t** page) 
+#ifdef __CONFIG_PAGE_COLORING__
+error_t upage_alloc(struct proc* p, page_t** page, int zero)
 {
-       static size_t next_color = 0;
-       error_t e;
-       for(int i=next_color; i<llc_cache->num_colors; i++) {
-               e = page_alloc_from_color_range(page, i, 1);
-               if(e == ESUCCESS) {
-                       next_color = i+1;
-                       return e;
-               }
+       spin_lock_irqsave(&colored_page_free_list_lock);
+       ssize_t ret = __colored_page_alloc(p->cache_colors_map, 
+                                            page, p->next_cache_color);
+       spin_unlock_irqsave(&colored_page_free_list_lock);
+
+       if(ret >= 0)
+       {
+               if(zero)
+                       memset(page2kva(*page),0,PGSIZE);
+               p->next_cache_color = (ret + 1) & (llc_cache->num_colors-1);
+               return 0;
        }
-       for(int i=0; i<next_color; i++) {
-               e = page_alloc_from_color_range(page, i, 1);
-               if(e == ESUCCESS) {
-                       next_color = i+1;
-                       return e;
-               }
+       return ret;
+}
+#else 
+error_t upage_alloc(struct proc* p, page_t** page, int zero)
+{
+       ssize_t ret;
+       spin_lock_irqsave(&colored_page_free_list_lock);
+       if((ret = __page_alloc_from_color_range(page, global_next_color, 
+                                   llc_cache->num_colors - global_next_color)) < 0)
+               ret = __page_alloc_from_color_range(page, 0, global_next_color);
+
+       if(ret >= 0) {
+               if(zero)
+                       memset(page2kva(*page),0,PGSIZE);
+               global_next_color = ret;        
+               ret = ESUCCESS;
        }
-       return -ENOMEM;
+       spin_unlock_irqsave(&colored_page_free_list_lock);
+       
+       return ret;
+}
+#endif
+
+error_t kpage_alloc(page_t** page) 
+{
+       ssize_t ret;
+       spin_lock_irqsave(&colored_page_free_list_lock);
+       if((ret = __page_alloc_from_color_range(page, global_next_color, 
+                                   llc_cache->num_colors - global_next_color)) < 0)
+               ret = __page_alloc_from_color_range(page, 0, global_next_color);
+
+       if(ret >= 0) {
+               global_next_color = ret;        
+               page_incref(*page);
+               ret = ESUCCESS;
+       }
+       spin_unlock_irqsave(&colored_page_free_list_lock);
+       
+       return ret;
 }
 
 /**
- * @brief Allocated 2^order contiguous physical pages.  Will incrememnt the
+ * @brief Allocated 2^order contiguous physical pages.  Will increment the
  * reference count for the pages.
  *
  * @param[in] order order of the allocation
@@ -152,18 +228,11 @@ void free_cont_pages(void *buf, size_t order)
 }
 
 /*
- * This macro defines multiple functions of the form:
- * error_t _cache##_page_alloc(page_t** page, size_t color)
- *
- * Each of these functions operates on a different level of 
- * of the cache heirarchy, and allocates a physical page
- * from the list of pages corresponding to the supplied 
- * color for the given cache.  
- * 
+ * Allocates a specific physical page.
  * Does NOT set the contents of the physical page to zero -
  * the caller must do that if necessary.
  *
- * color       -- the color from which to allocate a page
+ * ppn         -- the page number to allocate
  * *page       -- is set to point to the Page struct 
  *                of the newly allocated page
  *
@@ -171,69 +240,19 @@ void free_cont_pages(void *buf, size_t order)
  *   ESUCCESS  -- on success
  *   -ENOMEM   -- otherwise 
  */
-error_t l1_page_alloc(page_t** page, size_t color)
-{
-       if(l1)
-       {
-               uint16_t range = llc_cache->num_colors / get_cache_num_page_colors(l1);
-               uint16_t base_color = color*range;
-               return page_alloc_from_color_range(page, base_color, range);
-       }
-       return -ENOCACHE;
-}
-
-error_t l2_page_alloc(page_t** page, size_t color)
-{
-       if(l2)
-       {
-               uint16_t range = llc_cache->num_colors / get_cache_num_page_colors(l2);
-               uint16_t base_color = color*range;
-               return page_alloc_from_color_range(page, base_color, range);
-       }
-       return -ENOCACHE;
-}
-
-error_t l3_page_alloc(page_t** page, size_t color)
+error_t upage_alloc_specific(struct proc* p, page_t** page, size_t ppn)
 {
-       if(l3)
-       {
-               uint16_t range = llc_cache->num_colors / get_cache_num_page_colors(l3);
-               uint16_t base_color = color*range;
-               return page_alloc_from_color_range(page, base_color, range);
-       }
-       return -ENOCACHE;
-}
-
-/* Internal version of page_alloc_specific.  Grab the lock first. */
-static error_t __page_alloc_specific(page_t** page, size_t ppn)
-{
-       page_t* sp_page = ppn2page(ppn);
-       if( sp_page->page_ref != 0 )
-               return -ENOMEM;
-       *page = sp_page;
-       LIST_REMOVE(*page, page_link);
-
-       page_clear(*page);
+       spin_lock_irqsave(&colored_page_free_list_lock);
+       __page_alloc_specific(page, ppn);
+       spin_unlock_irqsave(&colored_page_free_list_lock);
        return 0;
 }
 
-/*
- * Allocates a specific physical page.
- * Does NOT set the contents of the physical page to zero -
- * the caller must do that if necessary.
- *
- * ppn         -- the page number to allocate
- * *page       -- is set to point to the Page struct 
- *                of the newly allocated page
- *
- * RETURNS 
- *   ESUCCESS  -- on success
- *   -ENOMEM   -- otherwise 
- */
-error_t page_alloc_specific(page_t** page, size_t ppn)
+error_t kpage_alloc_specific(page_t** page, size_t ppn)
 {
        spin_lock_irqsave(&colored_page_free_list_lock);
        __page_alloc_specific(page, ppn);
+       page_incref(*page);
        spin_unlock_irqsave(&colored_page_free_list_lock);
        return 0;
 }
@@ -245,7 +264,7 @@ error_t page_alloc_specific(page_t** page, size_t ppn)
  */
 static error_t __page_free(page_t* page) 
 {
-       page_clear(page);
+       __page_clear(page);
 
        LIST_INSERT_HEAD(
           &(colored_page_free_list[get_page_color(page2ppn(page), llc_cache)]),
@@ -266,7 +285,7 @@ error_t page_free(page_t *SAFE page)
 }
 
 /*
- * Check if a page with the given pyhysical page # is free
+ * Check if a page with the given physical page # is free
  */
 int page_is_free(size_t ppn) {
        page_t* page = ppn2page(ppn);