arch_mm.c 4.2 KB

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