arch_mm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. __archptr_t freeptr;
  31. void *kernel_end;
  32. /* the most important work is: get total size of ram,
  33. * and initiallize page structures */
  34. int arch_mm_init(void) {
  35. struct e820_entry *e;
  36. struct zone *z;
  37. struct page *p, *last;
  38. int i;
  39. __archptr_t addr, total_pages;
  40. total_pages = 0;
  41. init_mapping_32m();
  42. init_kmlog_buf();
  43. kernel_end = (void *) (KERNEL_OFFSET | (__archptr_t) &_end);
  44. /* put them there, after that, are the page structures. */
  45. z = kmem_zones;
  46. memset(z, 0, sizeof(struct zone) * 16);
  47. for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
  48. long npages;
  49. if (e->type > E820_TYPE_MAX || !e->type)
  50. break;
  51. e->type = e->type == E820_TYPE_RAM ? ZONETYPE_RAM : ZONETYPE_OTHER;
  52. addr = PAGE_ALIGN(HIGH_AND_LOW(e->addrh, e->addrl, 32));
  53. npages = PAGE_NR(HIGH_AND_LOW(e->lenh, e->lenl, 32));
  54. z->start_addr = (void *) addr;
  55. z->type = e->type;
  56. z->total_pages += npages;
  57. /* any page */
  58. total_pages += npages;
  59. if (z->type == ZONETYPE_RAM)
  60. nr_ram_pages += npages;
  61. ++z;
  62. }
  63. p = kmem_pages = (struct page *) z;
  64. z = kmem_zones;
  65. for (;;) {
  66. if (z->type != ZONETYPE_RAM && z->type != ZONETYPE_OTHER)
  67. break;
  68. z->pages = p;
  69. for (i = 0; i < z->total_pages; i++) {
  70. last = p;
  71. p->count = 0;
  72. p->next = p + 1;
  73. p++;
  74. }
  75. last->next = NULL;
  76. z++;
  77. }
  78. first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) p));
  79. return 0;
  80. }
  81. /* for a 4gb memory space, 12m used to create struct page */
  82. static int __init init_mapping_32m(void) {
  83. #if CONFIG_64BIT
  84. freeptr = 0x3000;
  85. /* already mapped to 1g */
  86. return 0;
  87. #else
  88. __archptr_t *lvl1 = (void *) 0,
  89. *lvl2 = (void *) 4096;
  90. __archptr_t addr = 4096 + 7;
  91. int i, j;
  92. for (i = 0; i < 6; i++, addr += 4096) {
  93. lvl1[i] = addr;
  94. lvl1[0x300 + i] = addr;
  95. }
  96. addr = 7;
  97. for (i = 0; i < 6; i++) {
  98. lvl2 = (void *) (lvl1[i] & ~0xff);
  99. for (j = 0; j < 1024; j++, addr += 4096)
  100. lvl2[j] = addr;
  101. }
  102. /* 16 kib, oh dear, my dear 16 kib :-( */
  103. freeptr = 0x7000;
  104. /* no need to invalidate() */
  105. return 0;
  106. #endif
  107. }
  108. static int __init init_kmlog_buf(void) {
  109. kmlog_buf = (char *) freeptr;
  110. freeptr += KMLOG_SIZE;
  111. *kmlog_buf = 0;
  112. return 0;
  113. }
  114. /* stack - 8192 */
  115. #define END 0x8e000
  116. void *early_kmalloc(int nbytes, int align) {
  117. void *p;
  118. freeptr = (freeptr + align - 1) & ~(align - 1);
  119. if (freeptr + nbytes > END)
  120. return NULL;
  121. p = (void *) freeptr;
  122. freeptr += nbytes;
  123. return p;
  124. }
  125. int early_free(void *p, int nbytes) {
  126. if (p + nbytes != (void *) freeptr)
  127. /* cannot free */
  128. return 1;
  129. freeptr -= nbytes;
  130. return 0;
  131. }
  132. /* map page s (virtual) to d */
  133. void *mapto(void *s, void *d) {
  134. #if CONFIG_64BIT
  135. __archptr_t *lvl1;
  136. __archptr_t dest_addr;
  137. dest_addr = (__archptr_t) d;
  138. lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 36) & 4095];
  139. if (!lvl1)
  140. /* we need 2m to do that, but now
  141. * the low memory pool has only about 0.5m.
  142. * abandon, man */
  143. panic("mapto: what time can we support a getfreepage()?");
  144. return d;
  145. #else
  146. __archptr_t dest_addr = (__archptr_t) d;
  147. __archptr_t *lvl1;
  148. __archptr_t *lvl2;
  149. dest_addr &= ~PAGE_MASK;
  150. /* 10 lvl1 10 lvl2 12 offset */
  151. lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22];
  152. if (!lvl1) {
  153. if (!(lvl1 = early_kmalloc(4096, 4096)))
  154. panic("mapto: Out of memory\n");
  155. LVL1_PT[dest_addr >> 22] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
  156. memset(lvl1, 0, 4096);
  157. }
  158. lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu);
  159. lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | DEFAULT_PAGE_ATTR;
  160. return (void *) dest_addr;
  161. #endif
  162. }
  163. arch_init(arch_mm_init);