/* kernel/page.c
*
* This file is part of evx.
*
* evx is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* evx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with evx. If not, see
*/
#include
#include
#include
#include
#include
#include
#include
#include
static int pages_in_levels_init(void);
/* I think, well, it looks like a buddy, but it's not...
*
* each '->data' points to the start of pages */
struct klist_node buddy_pages[BUDDY_LEVELS];
int pages_in_levels[BUDDY_LEVELS];
int __init pages_init(void) {
struct zone *z = kmem_zones;
struct page *p;
struct klist_node *n;
int i, j;
pages_in_levels_init();
for (i = 0; i < BUDDY_LEVELS; i++) {
n = NULL;
klist_init(buddy_pages + i);
/* '->data' in buddy_pages' entry points to the first usable page */
for (j = 0; j < pages_in_levels[i]; j++) {
if (unlikely(!p)) {
do {
++z;
} while (z->type != ZONETYPE_RAM &&
z < (struct zone *) kmem_pages);
if (z == (struct zone *) kmem_pages)
panic("pages_init: broken zone descriptor");
p = z->pages;
if (p < first_usable_ram)
p = first_usable_ram;
if (unlikely(!p))
panic("pages_init: broken zone descriptor");
buddy_pages[i].data = n;
}
if (!n) {
n = kmalloc(sizeof(struct klist_node));
if (unlikely(!n))
panic("pages_init: out of memory");
klist_init(n);
n->value = 0;
n->data = p;
klist_add(buddy_pages + i, n);
}
p = p->next;
/* this stores the number of pages */
++n->value;
}
}
kmlog(KERN_INFO "%d pages total, %d pages usable\n", nr_ram_pages, usable_ram_pages);
return 0;
}
int __init pages_in_levels_init(void) {
/* a bit math, very healthy :-) */
register int an, d, i, check = 0;
/* first we must get 'd', by:
* S = (a1 + an) * n / 2
* an = 1 + a1 + (n - 1) * d
*
* a1 = 1 (levels[BUDDY_LEVELS - 1])
* n is BUDDY_LEVELS
* */
an = (usable_ram_pages << 1) / BUDDY_LEVELS - 1;
d = (an - 1) / (BUDDY_LEVELS - 1);
for (i = BUDDY_LEVELS; i > 0; i--)
check += pages_in_levels[i - 1] = 1 + d * (BUDDY_LEVELS - i);
if (check != usable_ram_pages)
/* this thing always happens */
pages_in_levels[0] += usable_ram_pages - check;
return 0;
}
/* this is not interesting, and we will not get so better, so just use
* this and we can do other things i think more interesting such as
* writing some drivers
*
* WARNING that the returned value maybe unmapped. you should map it by
* yourself.
* */
struct page *getfreepage(int order, int gfp_mask) {
struct klist_node *list = buddy_pages + order, *n;
struct page *page, *t;
/* find the next. if no page else, get from its parent */
if (!(n = list->data))
return NULL;
/* find next usable */
page = n->data;
for (n = list->next; n != list; n = n->next) {
t = n->data;
while (t && page_count(t) && t != page)
t = t->next;
if (!t)
/* change it later */
panic("getfreepage: no pages left");
}
page->info |= 0xff;
list->data = t;
return page;
}
int freepage(struct page *p, int order) {
struct klist_node *list = buddy_pages + order, *n;
struct page *t;
for (n = list->next; n != list; n = n->next)
if (page_id(p) == page_id((struct page *) n->data))
break;
if (n == list)
panic("freepage: page not found");
/* find the struct page */
for (t = n->data; t; t = t->next)
if (t == p)
break;
if (!t)
panic("freepage: broken system");
if (!page_count(t))
panic("freepage: trying to free a free-page");
t->info &= ~0xff;
return 0;
}