| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /* 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 <https://www.gnu.org/licenses/>
- */
- #include <kernel/init.h>
- #include <kernel/module.h>
- #include <kernel/mm.h>
- #include <kernel/kmlog.h>
- #include <kernel/kernel.h>
- #include <arch/archinfo.h>
- #include <lib/klist.h>
- #include <sys/types.h>
- 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 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);
- 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");
- }
- 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 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;
- }
|