page.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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. for (j = 0; j < pages_in_levels[i]; j++) {
  42. if (unlikely(!p)) {
  43. do {
  44. ++z;
  45. } while (z->type != ZONETYPE_RAM &&
  46. z < (struct zone *) kmem_pages);
  47. if (z == (struct zone *) kmem_pages)
  48. panic("pages_init: broken zone descriptor");
  49. p = z->pages;
  50. if (p < first_usable_ram)
  51. p = first_usable_ram;
  52. if (unlikely(!p))
  53. panic("pages_init: broken zone descriptor");
  54. }
  55. if (!n) {
  56. n = kmalloc(sizeof(struct klist_node));
  57. if (unlikely(!n))
  58. panic("pages_init: out of memory");
  59. klist_init(n);
  60. n->value = 0;
  61. n->data = p;
  62. klist_add(buddy_pages + i, n);
  63. }
  64. p = p->next;
  65. /* this stores the number of pages */
  66. ++n->value;
  67. }
  68. }
  69. kmlog(KERN_INFO "%d pages total, %d pages usable\n", nr_ram_pages, usable_ram_pages);
  70. return 0;
  71. }
  72. int pages_in_levels_init(void) {
  73. /* a bit math, very healthy :-) */
  74. register int an, d, i, check = 0;
  75. /* first we must get 'd', by:
  76. * S = (a1 + an) * n / 2
  77. * an = 1 + a1 + (n - 1) * d
  78. *
  79. * a1 = 1 (levels[BUDDY_LEVELS - 1])
  80. * n is BUDDY_LEVELS
  81. * */
  82. an = (usable_ram_pages << 1) / BUDDY_LEVELS - 1;
  83. d = (an - 1) / (BUDDY_LEVELS - 1);
  84. for (i = BUDDY_LEVELS; i > 0; i--)
  85. check += pages_in_levels[i - 1] = 1 + d * (BUDDY_LEVELS - i);
  86. if (check != usable_ram_pages)
  87. /* this thing always happens */
  88. pages_in_levels[0] += usable_ram_pages - check;
  89. return 0;
  90. }