page.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* kernel/page.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <kernel/init.h>
  19. #include <kernel/module.h>
  20. #include <kernel/mm.h>
  21. #include <kernel/kmlog.h>
  22. #include <kernel/kernel.h>
  23. #include <arch/archinfo.h>
  24. #include <lib/klist.h>
  25. #include <sys/types.h>
  26. static int pages_in_levels_init(void);
  27. /* I think, well, it looks like a buddy, but it's not...
  28. *
  29. * each '->data' points to the start of pages */
  30. struct klist_node buddy_pages[BUDDY_LEVELS];
  31. int pages_in_levels[BUDDY_LEVELS];
  32. int __init pages_init(void) {
  33. struct zone *z = kmem_zones;
  34. struct page *p;
  35. struct klist_node *n;
  36. int i, j;
  37. pages_in_levels_init();
  38. for (i = 0; i < BUDDY_LEVELS; i++) {
  39. n = NULL;
  40. klist_init(buddy_pages + i);
  41. /* '->data' in buddy_pages' entry points to the first usable page.
  42. *
  43. * I know bugs here, I know, okay */
  44. for (j = 0; j < pages_in_levels[i]; j++) {
  45. if (unlikely(!p)) {
  46. do {
  47. ++z;
  48. } while (z->type != ZONETYPE_RAM &&
  49. z < (struct zone *) kmem_pages);
  50. if (z == (struct zone *) kmem_pages)
  51. panic("pages_init: broken zone descriptor");
  52. p = z->pages;
  53. if (p < first_usable_ram)
  54. p = first_usable_ram;
  55. if (unlikely(!p))
  56. panic("pages_init: broken zone descriptor");
  57. buddy_pages[i].data = n;
  58. }
  59. if (!n) {
  60. n = kmalloc(sizeof(struct klist_node));
  61. if (unlikely(!n))
  62. panic("pages_init: out of memory");
  63. klist_init(n);
  64. n->value = 0;
  65. n->data = p;
  66. klist_add(buddy_pages + i, n);
  67. }
  68. p = p->next;
  69. /* this stores the number of pages */
  70. ++n->value;
  71. }
  72. }
  73. kmlog(KERN_INFO "%d pages total, %d pages usable\n", nr_ram_pages, usable_ram_pages);
  74. return 0;
  75. }
  76. int __init pages_in_levels_init(void) {
  77. /* a bit math, very healthy :-) */
  78. register int an, d, i, check = 0;
  79. /* first we must get 'd', by:
  80. * S = (a1 + an) * n / 2
  81. * an = 1 + a1 + (n - 1) * d
  82. *
  83. * a1 = 1 (levels[BUDDY_LEVELS - 1])
  84. * n is BUDDY_LEVELS
  85. * */
  86. an = (usable_ram_pages << 1) / BUDDY_LEVELS - 1;
  87. d = (an - 1) / (BUDDY_LEVELS - 1);
  88. for (i = BUDDY_LEVELS; i > 0; i--)
  89. check += pages_in_levels[i - 1] = 1 + d * (BUDDY_LEVELS - i);
  90. if (check != usable_ram_pages)
  91. /* this thing always happens */
  92. pages_in_levels[0] += usable_ram_pages - check;
  93. return 0;
  94. }
  95. /* this is not interesting, and we will not get so better, so just use
  96. * this and we can do other things i think more interesting such as
  97. * writing some drivers
  98. *
  99. * WARNING that the returned value maybe unmapped. you should map it by
  100. * yourself.
  101. * */
  102. struct page *getfreepage(int order, int gfp_mask) {
  103. struct klist_node *list = buddy_pages + order, *n;
  104. struct page *page, *t;
  105. /* find the next. if no page else, get from its parent */
  106. if (!(n = list->data))
  107. return NULL;
  108. /* find next usable */
  109. page = n->data;
  110. for (n = list->next; n != list; n = n->next) {
  111. t = n->data;
  112. while (t && page_count(t) && t != page)
  113. t = t->next;
  114. if (!t)
  115. /* change it later */
  116. panic("getfreepage: no pages left");
  117. }
  118. page->info |= 0xff;
  119. list->data = t;
  120. return page;
  121. }
  122. int freepage(struct page *p, int order) {
  123. struct klist_node *list = buddy_pages + order, *n;
  124. struct page *t;
  125. for (n = list->next; n != list; n = n->next)
  126. if (page_id(p) == page_id((struct page *) n->data))
  127. break;
  128. if (n == list)
  129. panic("freepage: page not found");
  130. /* find the struct page */
  131. for (t = n->data; t; t = t->next)
  132. if (t == p)
  133. break;
  134. if (!t)
  135. panic("freepage: broken system");
  136. if (!page_count(t))
  137. panic("freepage: trying to free a free-page");
  138. t->info &= ~0xff;
  139. return 0;
  140. }