_M procs start up at _start/hart_entry for vcore0
[akaros.git] / kern / src / page_alloc.c
index 19f1085..6531c3a 100644 (file)
 #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
 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);
-       cache_color_alloc(llc_cache, global_cache_colors_map);
-       cache_color_alloc(llc_cache, global_cache_colors_map);
-       cache_color_alloc(llc_cache, global_cache_colors_map);
-       cache_color_alloc(llc_cache, global_cache_colors_map);
-       cache_color_alloc(llc_cache, global_cache_colors_map);
+       for(int i = 0; i < llc_cache->num_colors/8; i++)
+               cache_color_alloc(llc_cache, global_cache_colors_map);
 }
 
 /**
@@ -117,7 +115,8 @@ static error_t __page_alloc_specific(page_t** page, size_t ppn)
  * @return ESUCCESS on success
  * @return -ENOMEM  otherwise
  */
-error_t upage_alloc(struct proc* p, page_t** page)
+#ifdef __CONFIG_PAGE_COLORING__
+error_t upage_alloc(struct proc* p, page_t** page, int zero)
 {
        spin_lock_irqsave(&colored_page_free_list_lock);
        ssize_t ret = __colored_page_alloc(p->cache_colors_map, 
@@ -126,23 +125,44 @@ error_t upage_alloc(struct proc* p, page_t** page)
 
        if(ret >= 0)
        {
-               memset(page2kva(*page),0,PGSIZE);
-               p->next_cache_color = (ret + 1) % llc_cache->num_colors;
+               if(zero)
+                       memset(page2kva(*page),0,PGSIZE);
+               p->next_cache_color = (ret + 1) & (llc_cache->num_colors-1);
+               return 0;
        }
        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;
+       }
+       spin_unlock_irqsave(&colored_page_free_list_lock);
+       
+       return ret;
+}
+#endif
 
 error_t kpage_alloc(page_t** page) 
 {
-       static size_t next_color = 0;
        ssize_t ret;
        spin_lock_irqsave(&colored_page_free_list_lock);
-       if((ret = __page_alloc_from_color_range(page, next_color, 
-                                   llc_cache->num_colors)) < 0)
-               ret = __page_alloc_from_color_range(page, 0, next_color);
+       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) {
-               next_color = ret;        
+               global_next_color = ret;        
                page_incref(*page);
                ret = ESUCCESS;
        }
@@ -152,7 +172,7 @@ error_t kpage_alloc(page_t** page)
 }
 
 /**
- * @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
@@ -265,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);