arch_mm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. usable_ram_pages = nr_ram_pages - (first_usable_ram - kmem_pages);
  80. return 0;
  81. }
  82. /* for a 4gb memory space, 12m used to create struct page */
  83. static int __init init_mapping_32m(void) {
  84. #if CONFIG_64BIT
  85. freeptr = 0x3000;
  86. /* already mapped to 1g */
  87. return 0;
  88. #else
  89. __archptr_t *lvl1 = (void *) 0,
  90. *lvl2 = (void *) 4096;
  91. __archptr_t addr = 4096 + 7;
  92. int i, j;
  93. for (i = 0; i < 6; i++, addr += 4096) {
  94. lvl1[i] = addr;
  95. lvl1[0x300 + i] = addr;
  96. }
  97. addr = 7;
  98. for (i = 0; i < 6; i++) {
  99. lvl2 = (void *) (lvl1[i] & ~0xff);
  100. for (j = 0; j < 1024; j++, addr += 4096)
  101. lvl2[j] = addr;
  102. }
  103. /* 16 kib, oh dear, my dear 16 kib :-( */
  104. freeptr = 0x7000;
  105. /* no need to invalidate() */
  106. return 0;
  107. #endif
  108. }
  109. static int __init init_kmlog_buf(void) {
  110. kmlog_buf = (char *) freeptr;
  111. freeptr += KMLOG_SIZE;
  112. *kmlog_buf = 0;
  113. return 0;
  114. }
  115. /* stack - 8192 */
  116. #define END 0x8e000
  117. void *early_kmalloc(int nbytes, int align) {
  118. void *p;
  119. freeptr = (freeptr + align - 1) & ~(align - 1);
  120. if (freeptr + nbytes > END)
  121. return NULL;
  122. p = (void *) freeptr;
  123. freeptr += nbytes;
  124. return p;
  125. }
  126. int early_free(void *p, int nbytes) {
  127. if (p + nbytes != (void *) freeptr)
  128. /* cannot free */
  129. return 1;
  130. freeptr -= nbytes;
  131. return 0;
  132. }
  133. /* map page s (virtual) to d */
  134. void *mapto(void *s, void *d) {
  135. #if CONFIG_64BIT
  136. __archptr_t *lvl1;
  137. __archptr_t dest_addr;
  138. dest_addr = (__archptr_t) d;
  139. lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 36) & 4095];
  140. if (!lvl1)
  141. /* we need 2m to do that, but now
  142. * the low memory pool has only about 0.5m.
  143. * abandon, man */
  144. panic("mapto: what time can we support a getfreepage()?");
  145. return d;
  146. #else
  147. __archptr_t dest_addr = (__archptr_t) d;
  148. __archptr_t *lvl1;
  149. __archptr_t *lvl2;
  150. dest_addr &= ~PAGE_MASK;
  151. /* 10 lvl1 10 lvl2 12 offset */
  152. lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22];
  153. if (!lvl1) {
  154. if (!(lvl1 = early_kmalloc(4096, 4096)))
  155. panic("mapto: Out of memory\n");
  156. LVL1_PT[dest_addr >> 22] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR;
  157. memset(lvl1, 0, 4096);
  158. }
  159. lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu);
  160. lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | DEFAULT_PAGE_ATTR;
  161. return (void *) dest_addr;
  162. #endif
  163. }
  164. arch_init(arch_mm_init);