/****************************************************************************** * page_alloc.c * * Simple buddy allocator for Xenoserver hypervisor. * * Copyright (c) 2002 K A Fraser */ #include #include #include #include #include #include static spinlock_t alloc_lock = SPIN_LOCK_UNLOCKED; /********************* * ALLOCATION BITMAP * One bit per page of memory. Bit set => page is allocated. */ static unsigned long *alloc_bitmap; #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8) #define allocated_in_map(_pn) \ (alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] & (1<<((_pn)&(PAGES_PER_MAPWORD-1)))) /* * Hint regarding bitwise arithmetic in map_{alloc,free}: * -(1<= n. * (1<next == NULL) #define round_pgdown(_p) ((_p)&PAGE_MASK) #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK) /* Initialise allocator, placing addresses [@min,@max] in free pool. */ void __init init_page_allocator(unsigned long min, unsigned long max) { int i; unsigned long range, bitmap_size; chunk_head_t *ch; chunk_tail_t *ct; for ( i = 0; i < FREELIST_SIZE; i++ ) { free_list[i] = &free_tail[i]; free_tail[i].pprev = &free_list[i]; free_tail[i].next = NULL; } min = round_pgup (min); max = round_pgdown(max); /* Allocate space for the allocation bitmap. */ bitmap_size = (max+1) >> (PAGE_SHIFT+3); bitmap_size = round_pgup(bitmap_size); alloc_bitmap = (unsigned long *)__va(min); min += bitmap_size; range = max - min; /* All allocated by default. */ memset(alloc_bitmap, ~0, bitmap_size); /* Free up the memory we've been given to play with. */ map_free(min>>PAGE_SHIFT, range>>PAGE_SHIFT); /* The buddy lists are addressed in high memory. */ min += PAGE_OFFSET; max += PAGE_OFFSET; while ( range != 0 ) { /* * Next chunk is limited by alignment of min, but also * must not be bigger than remaining range. */ for ( i = PAGE_SHIFT; (1<<(i+1)) <= range; i++ ) if ( min & (1<level = i; ch->next = free_list[i]; ch->pprev = &free_list[i]; ch->next->pprev = &ch->next; free_list[i] = ch; ct->level = i; } } /* Allocate 2^@order contiguous pages. */ unsigned long __get_free_pages(int mask, int order) { int i; chunk_head_t *alloc_ch, *spare_ch; chunk_tail_t *spare_ct; unsigned long flags; spin_lock_irqsave(&alloc_lock, flags); /* Find smallest order which can satisfy the request. */ for ( i = order; i < FREELIST_SIZE; i++ ) { if ( !FREELIST_EMPTY(free_list[i]) ) break; } if ( i == FREELIST_SIZE ) { printk("Cannot handle page request order %d!\n", order); return NULL; } /* Unlink a chunk. */ alloc_ch = free_list[i]; free_list[i] = alloc_ch->next; alloc_ch->next->pprev = alloc_ch->pprev; /* We may have to break the chunk a number of times. */ while ( i != order ) { /* Split into two equal parts. */ i--; spare_ch = (chunk_head_t *)((char *)alloc_ch + (1<<(i+PAGE_SHIFT))); spare_ct = (chunk_tail_t *)((char *)spare_ch + (1<<(i+PAGE_SHIFT)))-1; /* Create new header for spare chunk. */ spare_ch->level = i; spare_ch->next = free_list[i]; spare_ch->pprev = &free_list[i]; spare_ct->level = i; /* Link in the spare chunk. */ spare_ch->next->pprev = &spare_ch->next; free_list[i] = spare_ch; } map_alloc(__pa(alloc_ch)>>PAGE_SHIFT, 1<> PAGE_SHIFT; spin_lock_irqsave(&alloc_lock, flags); map_free(pagenr, 1<level != order ) break; ch = (chunk_head_t *)(p - size); p -= size; } else { /* Merge with successor block? */ if ( allocated_in_map(pagenr+(1<level != order ) break; } /* Okay, unlink the neighbour. */ *ch->pprev = ch->next; ch->next->pprev = ch->pprev; order++; size <<= 1; } /* Okay, add the final chunk to the appropriate free list. */ ch = (chunk_head_t *)p; ct = (chunk_tail_t *)(p+size)-1; ct->level = order; ch->level = order; ch->pprev = &free_list[order]; ch->next = free_list[order]; ch->next->pprev = &ch->next; free_list[order] = ch; spin_unlock_irqrestore(&alloc_lock, flags); }