page.c 2.5 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. /* I think, well, it looks like a buddy, but it's not...
  27. *
  28. * each '->data' points to the start of pages */
  29. struct klist_node buddy_pages = KLIST_NODE_INIT(&buddy_pages);
  30. int __init pages_init(void) {
  31. struct zone *z = kmem_zones;
  32. struct page *p = NULL;
  33. struct klist_node *n;
  34. int i;
  35. p = first_usable_ram;
  36. z = kmem_zones + page_id(p);
  37. n = kmalloc(sizeof(struct klist_node));
  38. if (unlikely(!n))
  39. panic("pages_init: out of memory");
  40. klist_init(n);
  41. n->data = p;
  42. klist_add(&buddy_pages, n);
  43. for (i = 0; i < usable_ram_pages; i++) {
  44. /* now it's linear, update, 2021-2-12 */
  45. if (!p) {
  46. do {
  47. z++;
  48. } while (z->type != ZONETYPE_RAM && z < (struct zone *) kmem_pages);
  49. if (unlikely(z >= (struct zone *) kmem_pages)) {
  50. kmlog(KERN_WARN "pages_init: unexpected end of zones\n");
  51. break;
  52. }
  53. p = z->pages;
  54. /* initial in buddy_pages */
  55. n = kmalloc(sizeof(struct klist_node));
  56. if (unlikely(!n))
  57. panic("pages_init: out of memory");
  58. n->data = p;
  59. klist_add(&buddy_pages, n);
  60. }
  61. ++n->value;
  62. p++;
  63. }
  64. kmlog(KERN_INFO "%d pages total, %d pages usable\n", nr_ram_pages, usable_ram_pages);
  65. return 0;
  66. }
  67. /* NOTE that nr means number of pages but not the order.
  68. *
  69. * support buddy later ... */
  70. struct page *getfreepages(int nr, int gfp_mask) {
  71. struct klist_node *n;
  72. struct page *p;
  73. int i;
  74. for (n = buddy_pages.next; n != &buddy_pages; n = n->next)
  75. if (n->value >= nr)
  76. /* got it */
  77. break;
  78. if (n == &buddy_pages) {
  79. kmlog(KERN_ERR "getfreepage: no enough pages: %d\n", nr);
  80. return NULL;
  81. }
  82. for (p = n->data, i = 0; i < nr; i++, p = p->next)
  83. p->info |= 0xffu;
  84. n->data = p;
  85. n->value -= nr;
  86. return p;
  87. }