From: AceVest Date: Fri, 4 Apr 2014 15:38:54 +0000 (+0800) Subject: prebuild buddy system from bootmem X-Git-Tag: 0.3.0~89 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/CHANGES?a=commitdiff_plain;h=ba73fec329e25f8ef66dbeccd65e3466ebe0acb1;p=kernel.git prebuild buddy system from bootmem --- diff --git a/include/mm.h b/include/mm.h index dbf8596..8991274 100644 --- a/include/mm.h +++ b/include/mm.h @@ -11,7 +11,11 @@ #include +#define BOOTMEM_PAGE_FREE 0 +#define BOOTMEM_PAGE_USED 1 + void *alloc_bootmem(unsigned long size, unsigned long align); +unsigned long bootmem_total_pages(); +unsigned long bootmem_page_state(unsigned long pfn); #define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE) - diff --git a/include/page.h b/include/page.h index 61911eb..d1cdc2d 100644 --- a/include/page.h +++ b/include/page.h @@ -63,14 +63,18 @@ typedef unsigned long pte_t; #define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde))) -#define MAX_ORDER (11) +#define MAX_OLD_ORDER (11) typedef struct page { - - + unsigned long index; } page_t; +typedef struct free_area +{ + unsigned long count; +} free_area_t; + // TODO Remove typedef struct page_ { diff --git a/mm/buddy.c b/mm/buddy.c index 55fec7f..eab076c 100644 --- a/mm/buddy.c +++ b/mm/buddy.c @@ -8,8 +8,42 @@ */ #include +#define MAX_ORDER 4 +struct buddy_system +{ + page_t **page_map; + free_area_t free_area[MAX_ORDER + 1]; +}; + +struct buddy_system buddy_system; + void init_buddy_system() { + unsigned long pfn_cnt = bootmem_total_pages(); + + buddy_system.page_map = alloc_bootmem(pfn_cnt*sizeof(page_t*), sizeof(page_t*)); + if(0 == buddy_system.page_map) { + printk("can not go on playing...\n"); + while(1); + } + unsigned long i; + for(i=0; iindex = i; + } } diff --git a/mm/kmalloc.c b/mm/kmalloc.c index bd5d996..6fdd2f4 100644 --- a/mm/kmalloc.c +++ b/mm/kmalloc.c @@ -22,7 +22,7 @@ static get_order(size_t size) //printk(" %08x\n", size); int i; int n = size>>PAGE_SHIFT; - for(i=0; itype != E820_RAM) continue; - bgn_pfn = pa2pfn(p->addr); - end_pfn = pa2pfn(p->addr + p->size); + bgn_pfn = PFN_UP(p->addr); + end_pfn = PFN_DW(p->addr + p->size); if(bootmem_data.max_pfn < end_pfn) bootmem_data.max_pfn = end_pfn; @@ -110,7 +119,7 @@ void e820_init_bootmem_data() bootmem_data.min_pfn = 0; // limit max_pfn - unsigned long max_support_pfn = pa2pfn(MAX_SUPT_PHYMM_SIZE); + unsigned long max_support_pfn = PFN_DW(MAX_SUPT_PHYMM_SIZE); if(bootmem_data.max_pfn > max_support_pfn) { bootmem_data.max_pfn = max_support_pfn; @@ -137,7 +146,6 @@ void register_bootmem_pages() test_and_clear_bit(j, bootmem_data.bitmap); } } - } void reserve_bootmem(unsigned long bgn_pfn, unsigned long end_pfn) @@ -240,7 +248,7 @@ find_block: for(i=bgn_pfn; ibitmap) != 0) { // space not enough + if(bootmem_page_state(i) != BOOTMEM_PAGE_FREE) { // space not enough bgn_pfn = ALIGN(i, step); if(bgn_pfn == i) bgn_pfn += step; @@ -328,4 +336,5 @@ void init_mm() printk("init global paging...\n"); init_paging(); printk("init buddy system...\n"); + init_buddy_system(); } diff --git a/mm/page.c b/mm/page.c index e81f06c..e0c4d65 100644 --- a/mm/page.c +++ b/mm/page.c @@ -20,11 +20,12 @@ #include + #define get_page_from_list(pList) list_entry(pList, Page, list) #define add_page2list(page, order) \ list_add(&page->list, &freeArea[order].freeList) -FreeArea freeArea[MAX_ORDER]; +FreeArea freeArea[MAX_OLD_ORDER]; #if 1 void do_no_page(void *addr) @@ -133,7 +134,7 @@ void do_wp_page(void *addr) inline pPage __alloc_pages(unsigned int order, unsigned int alloc_order) { - assert(0 <= order && order=MAX_ORDER) + if(order<0 || order>=MAX_OLD_ORDER) return NULL; int i; - for(i=order; imapNR, order, 1UL<order; - assert(0<=order && order", bpage->mapNR, bpage->order); - if(bpage == NULL || order == MAX_ORDER-1) + if(bpage == NULL || order == MAX_OLD_ORDER-1) { add_page2list(page, order); } @@ -264,7 +265,7 @@ void free_pages(pPage page) inline pPage __alloc_pages(unsigned int order, unsigned int alloc_order) { - assert(0 <= order && order= MAX_ORDER) + if(order >= MAX_OLD_ORDER) return NULL; int i; - for(i=order; imapNR>>(order+1); //printk("#########%d %d\n",page->mapNR, variable_test_bit( nr, (unsigned long *)freeArea[order].map)); - if(order == MAX_ORDER -1 + if(order == MAX_OLD_ORDER -1 || !variable_test_bit( nr, (unsigned long *)freeArea[order].map)) { change_bit( nr, (unsigned long *)freeArea[order].map); @@ -353,7 +354,7 @@ void free_pages(pPage page, unsigned int order) void disp_free_area() { int i; - for(i=0; i