page.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. for (j = 0; j < pages_in_levels[i]; j++) {
  43. if (unlikely(!p)) {
  44. do {
  45. ++z;
  46. } while (z->type != ZONETYPE_RAM &&
  47. z < (struct zone *) kmem_pages);
  48. if (z == (struct zone *) kmem_pages)
  49. panic("pages_init: broken zone descriptor");
  50. p = z->pages;
  51. if (p < first_usable_ram)
  52. p = first_usable_ram;
  53. if (unlikely(!p))
  54. panic("pages_init: broken zone descriptor");
  55. buddy_pages[i].data = n;
  56. }
  57. if (!n) {
  58. n = kmalloc(sizeof(struct klist_node));
  59. if (unlikely(!n))
  60. panic("pages_init: out of memory");
  61. klist_init(n);
  62. n->value = 0;
  63. n->data = p;
  64. klist_add(buddy_pages + i, n);
  65. }
  66. p = p->next;
  67. /* this stores the number of pages */
  68. ++n->value;
  69. }
  70. }
  71. kmlog(KERN_INFO "%d pages total, %d pages usable\n", nr_ram_pages, usable_ram_pages);
  72. return 0;
  73. }
  74. int __init pages_in_levels_init(void) {
  75. /* a bit math, very healthy :-) */
  76. register int an, d, i, check = 0;
  77. /* first we must get 'd', by:
  78. * S = (a1 + an) * n / 2
  79. * an = 1 + a1 + (n - 1) * d
  80. *
  81. * a1 = 1 (levels[BUDDY_LEVELS - 1])
  82. * n is BUDDY_LEVELS
  83. * */
  84. an = (usable_ram_pages << 1) / BUDDY_LEVELS - 1;
  85. d = (an - 1) / (BUDDY_LEVELS - 1);
  86. for (i = BUDDY_LEVELS; i > 0; i--)
  87. check += pages_in_levels[i - 1] = 1 + d * (BUDDY_LEVELS - i);
  88. if (check != usable_ram_pages)
  89. /* this thing always happens */
  90. pages_in_levels[0] += usable_ram_pages - check;
  91. return 0;
  92. }
  93. /* this is not interesting, and we will not get so better, so just use
  94. * this and we can do other things i think more interesting such as
  95. * writing some drivers
  96. *
  97. * WARNING that the returned value maybe unmapped. you should map it by
  98. * yourself.
  99. * */
  100. struct page *getfreepage(int order, int gfp_mask) {
  101. struct klist_node *list = buddy_pages + order, *n;
  102. struct page *page, *t;
  103. /* find the next. if no page else, get from its parent */
  104. if (!(n = list->data))
  105. return NULL;
  106. /* find next usable */
  107. page = n->data;
  108. for (n = list->next; n != list; n = n->next) {
  109. t = n->data;
  110. while (t && page_count(t) && t != page)
  111. t = t->next;
  112. if (!t)
  113. /* change it later */
  114. panic("getfreepage: no pages left");
  115. }
  116. page->info |= 0xff;
  117. list->data = t;
  118. return page;
  119. }
  120. int freepage(struct page *p, int order) {
  121. struct klist_node *list = buddy_pages + order, *n;
  122. struct page *t;
  123. for (n = list->next; n != list; n = n->next)
  124. if (page_id(p) == page_id((struct page *) n->data))
  125. break;
  126. if (n == list)
  127. panic("freepage: page not found");
  128. /* find the struct page */
  129. for (t = n->data; t; t = t->next)
  130. if (t == p)
  131. break;
  132. if (!t)
  133. panic("freepage: broken system");
  134. if (!page_count(t))
  135. panic("freepage: trying to free a free-page");
  136. t->info &= ~0xff;
  137. return 0;
  138. }