arch_mm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <arch/archinfo.h>
  22. #include <arch/mm.h>
  23. #include <arch/string.h>
  24. #include <sys/types.h>
  25. #include <config.h>
  26. static int __init init_mapping_32m(void);
  27. __archptr_t freeptr;
  28. void *kernel_end;
  29. /* the most important work is: get total size of ram,
  30. * and initiallize page structures */
  31. int arch_mm_init(void) {
  32. struct e820_entry *e;
  33. struct zone *z;
  34. struct page *p;
  35. int i;
  36. __archptr_t addr, total_pages;
  37. total_pages = 0;
  38. init_mapping_32m();
  39. kernel_end = (void *) (KERNEL_OFFSET | (__archptr_t) &_end);
  40. /* put them there, after that, are the page structures. */
  41. z = kmem_zones;
  42. p = kmem_pages;
  43. memset(z, 0, sizeof(struct zone) * 2);
  44. z[0].type = ZONETYPE_RAM;
  45. z[1].type = ZONETYPE_OTHER;
  46. for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
  47. long npages;
  48. if (e->type > E820_TYPE_MAX || !e->type)
  49. break;
  50. e->type = e->type == E820_TYPE_RAM ? ZONETYPE_RAM : ZONETYPE_OTHER;
  51. addr = PAGE_ALIGN(HIGH_AND_LOW(e->addrh, e->addrl, 32));
  52. if (z[e->type].start_addr > (void *) addr)
  53. z[e->type].start_addr = (void *) addr;
  54. npages = PAGE_NR(HIGH_AND_LOW(e->lenh, e->lenl, 32));
  55. z[e->type].total_pages += npages;
  56. /* any page */
  57. total_pages += npages;
  58. /* now initiallize page structures */
  59. for (i = 0; i < npages; i++) {
  60. p->count = 0;
  61. p->next = p + 1;
  62. ++p;
  63. }
  64. }
  65. first_usable_ram = page_descr(PAGE_ALIGN((__archptr_t) (kmem_pages + total_pages)));
  66. return 0;
  67. }
  68. /* for a 4gb memory space, 12m used to create struct page */
  69. static int __init init_mapping_32m(void) {
  70. #if CONFIG_64BIT
  71. freeptr = 0x3000;
  72. /* already mapped to 1g */
  73. return 0;
  74. #else
  75. __archptr_t *lvl1 = (void *) 0,
  76. *lvl2 = (void *) 4096;
  77. __archptr_t addr = 4096 + 7;
  78. int i, j;
  79. for (i = 0; i < 6; i++, addr += 4096) {
  80. lvl1[i] = addr;
  81. lvl1[0x300 + i] = addr;
  82. }
  83. addr = 7;
  84. for (i = 0; i < 6; i++) {
  85. lvl2 = (void *) (lvl1[i] & ~0xff);
  86. for (j = 0; j < 1024; j++, addr += 4096)
  87. lvl2[j] = addr;
  88. }
  89. /* 16 kib, oh dear, my dear 16 kib :-( */
  90. freeptr = 0x7000;
  91. /* no need to invalidate() */
  92. return 0;
  93. #endif
  94. }
  95. /* stack - 8192 */
  96. #define END 0x8e000
  97. void *early_kmalloc(int nbytes, int align) {
  98. void *p;
  99. freeptr = (freeptr + align - 1) & ~(align - 1);
  100. if (freeptr + nbytes > END)
  101. return NULL;
  102. p = (void *) freeptr;
  103. freeptr += nbytes;
  104. return p;
  105. }
  106. int early_free(void *p, int nbytes) {
  107. if (p + nbytes != (void *) freeptr)
  108. /* cannot free */
  109. return 1;
  110. freeptr -= nbytes;
  111. return 0;
  112. }
  113. arch_init(arch_mm_init);