arch_mm.c 3.0 KB

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