arch_mm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* arch/x86/kernel/arch_mm.c
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #include <kernel/init.h>
  21. #include <kernel/module.h>
  22. #include <kernel/mm.h>
  23. #include <kernel/kernel.h>
  24. #include <kernel/kmlog.h>
  25. #include <arch/archinfo.h>
  26. #include <arch/mm.h>
  27. #include <arch/string.h>
  28. #include <sys/types.h>
  29. #include <config.h>
  30. static int __init init_mapping_32m(void);
  31. static int __init init_kmlog_buf(void);
  32. int setpageattr(void *p, int attr);
  33. __archptr_t freeptr;
  34. void *kernel_end;
  35. /* the most important work is: get total size of ram,
  36. * and initiallize page structures */
  37. int arch_mm_init(void) {
  38. struct e820_entry *e;
  39. struct zone *z;
  40. struct page *p, *last;
  41. int i;
  42. __archptr_t addr, total_pages;
  43. total_pages = 0;
  44. init_mapping_32m();
  45. init_kmlog_buf();
  46. kernel_end = (void *) (KERNEL_OFFSET | (__archptr_t) &_end);
  47. /* put them there, after that, are the page structures. */
  48. z = kmem_zones;
  49. memset(z, 0, sizeof(struct zone) * 16);
  50. for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
  51. long npages;
  52. if (e->type > E820_TYPE_MAX || !e->type)
  53. break;
  54. e->type = e->type == E820_TYPE_RAM ? ZONETYPE_RAM : ZONETYPE_OTHER;
  55. addr = PAGE_ALIGN(HIGH_AND_LOW(e->addrh, e->addrl, 32));
  56. npages = PAGE_NR(HIGH_AND_LOW(e->lenh, e->lenl, 32));
  57. z->start_addr = (void *) addr;
  58. z->type = e->type;
  59. z->total_pages += npages;
  60. /* any page */
  61. total_pages += npages;
  62. if (z->type == ZONETYPE_RAM)
  63. nr_ram_pages += npages;
  64. ++z;
  65. }
  66. p = kmem_pages = (struct page *) z;
  67. z = kmem_zones;
  68. while (z < (struct zone *) kmem_pages) {
  69. if (z->type != ZONETYPE_RAM && z->type != ZONETYPE_OTHER)
  70. break;
  71. z->pages = p;
  72. for (i = 0; i < z->total_pages; i++) {
  73. last = p;
  74. p->info = (z - kmem_zones) << 24;
  75. p->next = p + 1;
  76. p++;
  77. }
  78. last->next = NULL;
  79. z++;
  80. }
  81. first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) p));
  82. usable_ram_pages = nr_ram_pages - (first_usable_ram - kmem_pages);
  83. return 0;
  84. }
  85. /* for a 4gb memory space, 12m used to create struct page */
  86. static int __init init_mapping_32m(void) {
  87. #if CONFIG_64BIT
  88. freeptr = 0x3000;
  89. /* already mapped to 1g */
  90. return 0;
  91. #else
  92. __archptr_t *lvl1 = (void *) 0,
  93. *lvl2 = (void *) 4096;
  94. __archptr_t addr = 4096 + 7;
  95. int i, j;
  96. for (i = 0; i < 6; i++, addr += 4096) {
  97. lvl1[i] = addr;
  98. lvl1[0x300 + i] = addr;
  99. }
  100. addr = 7;
  101. for (i = 0; i < 6; i++) {
  102. lvl2 = (void *) (lvl1[i] & ~0xff);
  103. for (j = 0; j < 1024; j++, addr += 4096)
  104. lvl2[j] = addr;
  105. }
  106. /* 16 kib, oh dear, my dear 16 kib :-( */
  107. freeptr = 0x7000;
  108. /* no need to invalidate() */
  109. return 0;
  110. #endif
  111. }
  112. static int __init init_kmlog_buf(void) {
  113. kmlog_buf = (char *) freeptr;
  114. freeptr += KMLOG_SIZE;
  115. *kmlog_buf = 0;
  116. return 0;
  117. }
  118. /* stack - 8192 */
  119. #define END 0x8e000
  120. void *early_kmalloc(int nbytes, int align) {
  121. void *p;
  122. freeptr = (freeptr + align - 1) & ~(align - 1);
  123. if (freeptr + nbytes > END)
  124. return NULL;
  125. p = (void *) freeptr;
  126. freeptr += nbytes;
  127. return p;
  128. }
  129. int early_free(void *p, int nbytes) {
  130. if (p + nbytes != (void *) freeptr)
  131. /* cannot free */
  132. return 1;
  133. freeptr -= nbytes;
  134. return 0;
  135. }
  136. /* map page s (virtual) to d... okay, what am i doing? */
  137. void *__mapto(void *s, void *d, int attr) {
  138. #if CONFIG_64BIT
  139. __archptr_t *lvl1, *lvl2, *lvl3, *p;
  140. __archptr_t dest_addr;
  141. dest_addr = (__archptr_t) d;
  142. lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 39) & 4095];
  143. if (!lvl1) {
  144. if (!(lvl1 = early_kmalloc(4096, 4096)))
  145. panic("mapto: Out of memory\n");
  146. LVL1_PT[(dest_addr >> 39) & 4095] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
  147. memset(lvl1, 0, 4096);
  148. }
  149. p = (__archptr_t *) ((__archptr_t) lvl1 & ~0xfffu);
  150. lvl2 = (__archptr_t *) p[(dest_addr >> 30) & 4095];
  151. if (!lvl2) {
  152. if (!(lvl2 = early_kmalloc(4096, 4096)))
  153. panic("mapto: Out of memory\n");
  154. p[(dest_addr >> 30) & 4095] = (__archptr_t) lvl2 | DEFAULT_PAGE_ATTR;
  155. memset(lvl2, 0, 4096);
  156. }
  157. lvl3 = (__archptr_t *) ((__archptr_t) lvl2 & ~0xfffu);
  158. if (s)
  159. lvl3[(dest_addr >> 21) & 4095] = (__archptr_t) s | attr;
  160. else
  161. lvl3[(dest_addr >> 21) & 4095] &= ~0xfffu | attr;
  162. return d;
  163. #else
  164. __archptr_t dest_addr = (__archptr_t) d;
  165. __archptr_t *lvl1;
  166. __archptr_t *lvl2;
  167. dest_addr &= ~PAGE_MASK;
  168. /* 10 lvl1 10 lvl2 12 offset */
  169. lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22];
  170. if (!lvl1) {
  171. if (!(lvl1 = early_kmalloc(4096, 4096)))
  172. panic("mapto: Out of memory\n");
  173. LVL1_PT[dest_addr >> 22] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
  174. memset(lvl1, 0, 4096);
  175. }
  176. lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu);
  177. if (!s)
  178. /* clear attributes and then set */
  179. lvl2[(dest_addr >> 12) & 1023] &= ~0xff | attr;
  180. else
  181. lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | attr;
  182. return (void *) dest_addr;
  183. #endif
  184. }
  185. void *mapto(void *s, void *d) {
  186. return __mapto(s, d, DEFAULT_PAGE_ATTR);
  187. }
  188. int setpageattr(void *p, int attr) {
  189. __mapto(NULL, p, attr);
  190. return 0;
  191. }
  192. arch_init(arch_mm_init);