arch_mm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* arch/x86/kernel/arch_mm.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/kernel.h>
  22. #include <kernel/kmlog.h>
  23. #include <arch/archinfo.h>
  24. #include <arch/mm.h>
  25. #include <arch/string.h>
  26. #include <sys/types.h>
  27. #include <config.h>
  28. static int __init init_mapping_32m(void);
  29. static int __init init_kmlog_buf(void);
  30. int setpageattr(void *p, int attr);
  31. __archptr_t freeptr;
  32. void *kernel_end;
  33. /* the most important work is: get total size of ram,
  34. * and initiallize page structures */
  35. int arch_mm_init(void) {
  36. struct e820_entry *e;
  37. struct zone *z;
  38. struct page *p, *last;
  39. int i;
  40. __archptr_t addr, total_pages;
  41. total_pages = 0;
  42. init_mapping_32m();
  43. init_kmlog_buf();
  44. kernel_end = (void *) (KERNEL_OFFSET | (__archptr_t) &_end);
  45. /* put them there, after that, are the page structures. */
  46. z = kmem_zones;
  47. memset(z, 0, sizeof(struct zone) * 16);
  48. for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
  49. long npages;
  50. if (e->type > E820_TYPE_MAX || !e->type)
  51. break;
  52. e->type = e->type == E820_TYPE_RAM ? ZONETYPE_RAM : ZONETYPE_OTHER;
  53. addr = PAGE_ALIGN(HIGH_AND_LOW(e->addrh, e->addrl, 32));
  54. npages = PAGE_NR(HIGH_AND_LOW(e->lenh, e->lenl, 32));
  55. z->start_addr = (void *) addr;
  56. z->type = e->type;
  57. z->total_pages += npages;
  58. /* any page */
  59. total_pages += npages;
  60. if (z->type == ZONETYPE_RAM)
  61. nr_ram_pages += npages;
  62. ++z;
  63. }
  64. p = kmem_pages = (struct page *) z;
  65. z = kmem_zones;
  66. while (z < (struct zone *) kmem_pages) {
  67. if (z->type != ZONETYPE_RAM && z->type != ZONETYPE_OTHER)
  68. break;
  69. z->pages = p;
  70. for (i = 0; i < z->total_pages; i++) {
  71. last = p;
  72. p->info = (z - kmem_zones) << 24;
  73. p->next = p + 1;
  74. p++;
  75. }
  76. last->next = NULL;
  77. z++;
  78. }
  79. first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) p));
  80. usable_ram_pages = nr_ram_pages - (first_usable_ram - kmem_pages);
  81. return 0;
  82. }
  83. /* for a 4gb memory space, 12m used to create struct page */
  84. static int __init init_mapping_32m(void) {
  85. #if CONFIG_64BIT
  86. freeptr = 0x3000;
  87. /* already mapped to 1g */
  88. return 0;
  89. #else
  90. __archptr_t *lvl1 = (void *) 0,
  91. *lvl2 = (void *) 4096;
  92. __archptr_t addr = 4096 + 7;
  93. int i, j;
  94. for (i = 0; i < 6; i++, addr += 4096) {
  95. lvl1[i] = addr;
  96. lvl1[0x300 + i] = addr;
  97. }
  98. addr = 7;
  99. for (i = 0; i < 6; i++) {
  100. lvl2 = (void *) (lvl1[i] & ~0xff);
  101. for (j = 0; j < 1024; j++, addr += 4096)
  102. lvl2[j] = addr;
  103. }
  104. /* 16 kib, oh dear, my dear 16 kib :-( */
  105. freeptr = 0x7000;
  106. /* no need to invalidate() */
  107. return 0;
  108. #endif
  109. }
  110. static int __init init_kmlog_buf(void) {
  111. kmlog_buf = (char *) freeptr;
  112. freeptr += KMLOG_SIZE;
  113. *kmlog_buf = 0;
  114. return 0;
  115. }
  116. /* stack - 8192 */
  117. #define END 0x8e000
  118. void *early_kmalloc(int nbytes, int align) {
  119. void *p;
  120. freeptr = (freeptr + align - 1) & ~(align - 1);
  121. if (freeptr + nbytes > END)
  122. return NULL;
  123. p = (void *) freeptr;
  124. freeptr += nbytes;
  125. return p;
  126. }
  127. int early_free(void *p, int nbytes) {
  128. if (p + nbytes != (void *) freeptr)
  129. /* cannot free */
  130. return 1;
  131. freeptr -= nbytes;
  132. return 0;
  133. }
  134. /* map page s (virtual) to d... okay, what am i doing? */
  135. void *__mapto(void *s, void *d, int attr) {
  136. #if CONFIG_64BIT
  137. __archptr_t *lvl1, *lvl2, *lvl3, *p;
  138. __archptr_t dest_addr;
  139. dest_addr = (__archptr_t) d;
  140. lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 39) & 4095];
  141. if (!lvl1) {
  142. if (!(lvl1 = early_kmalloc(4096, 4096)))
  143. panic("mapto: Out of memory\n");
  144. LVL1_PT[(dest_addr >> 39) & 4095] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
  145. memset(lvl1, 0, 4096);
  146. }
  147. p = (__archptr_t *) ((__archptr_t) lvl1 & ~0xfffu);
  148. lvl2 = (__archptr_t *) p[(dest_addr >> 30) & 4095];
  149. if (!lvl2) {
  150. if (!(lvl2 = early_kmalloc(4096, 4096)))
  151. panic("mapto: Out of memory\n");
  152. p[(dest_addr >> 30) & 4095] = (__archptr_t) lvl2 | DEFAULT_PAGE_ATTR;
  153. memset(lvl2, 0, 4096);
  154. }
  155. lvl3 = (__archptr_t *) ((__archptr_t) lvl2 & ~0xfffu);
  156. if (s)
  157. lvl3[(dest_addr >> 21) & 4095] = (__archptr_t) s | attr;
  158. else
  159. lvl3[(dest_addr >> 21) & 4095] &= ~0xfffu | attr;
  160. return d;
  161. #else
  162. __archptr_t dest_addr = (__archptr_t) d;
  163. __archptr_t *lvl1;
  164. __archptr_t *lvl2;
  165. dest_addr &= ~PAGE_MASK;
  166. /* 10 lvl1 10 lvl2 12 offset */
  167. lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22];
  168. if (!lvl1) {
  169. if (!(lvl1 = early_kmalloc(4096, 4096)))
  170. panic("mapto: Out of memory\n");
  171. LVL1_PT[dest_addr >> 22] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
  172. memset(lvl1, 0, 4096);
  173. }
  174. lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu);
  175. if (!s)
  176. /* clear attributes and then set */
  177. lvl2[(dest_addr >> 12) & 1023] &= ~0xff | attr;
  178. else
  179. lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | attr;
  180. return (void *) dest_addr;
  181. #endif
  182. }
  183. void *mapto(void *s, void *d) {
  184. return __mapto(s, d, DEFAULT_PAGE_ATTR);
  185. }
  186. int setpageattr(void *p, int attr) {
  187. __mapto(NULL, p, attr);
  188. return 0;
  189. }
  190. arch_init(arch_mm_init);