arch_mm.c 3.1 KB

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