mm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* Physical memory manager: parse the E820 map and hand out 4 KB pages. */
  2. #include "mm.h"
  3. #include "bootinfo.h"
  4. #include "serial.h"
  5. /* One bit per 4 KB page; bit set = in use. */
  6. #define MM_BITMAP_BITS (MM_MAX_PHYS / PAGE_SIZE)
  7. #define MM_BITMAP_WORDS (MM_BITMAP_BITS / 32)
  8. static unsigned int bitmap[MM_BITMAP_WORDS];
  9. static unsigned int free_pages;
  10. static unsigned int total_ram_kb;
  11. /* ---- bitmap helpers ---- */
  12. static inline void bit_set(unsigned int idx)
  13. {
  14. bitmap[idx / 32] |= (1u << (idx % 32));
  15. }
  16. static inline void bit_clear(unsigned int idx)
  17. {
  18. bitmap[idx / 32] &= ~(1u << (idx % 32));
  19. }
  20. static inline int bit_test(unsigned int idx)
  21. {
  22. return (bitmap[idx / 32] >> (idx % 32)) & 1;
  23. }
  24. /* ---- interrupt flag save/restore (allocator is not IRQ-safe) ---- */
  25. static inline unsigned int irq_disable_save(void)
  26. {
  27. unsigned int flags;
  28. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  29. return flags;
  30. }
  31. static inline void irq_restore(unsigned int flags)
  32. {
  33. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  34. }
  35. /* Mark a physical byte range [base, base+len) as used in the bitmap. */
  36. static void mark_used(unsigned long long base, unsigned long long len)
  37. {
  38. unsigned long long end = base + len;
  39. unsigned int p0, p1, p;
  40. if (base >= MM_MAX_PHYS)
  41. return;
  42. if (end > MM_MAX_PHYS)
  43. end = MM_MAX_PHYS;
  44. p0 = (unsigned int)(base >> 12);
  45. p1 = (unsigned int)((end + PAGE_SIZE - 1) >> 12);
  46. for (p = p0; p < p1; p++)
  47. bit_set(p);
  48. }
  49. /* Mark a physical byte range [base, base+len) as free (usable RAM). */
  50. static void mark_free(unsigned long long base, unsigned long long len)
  51. {
  52. unsigned long long end = base + len;
  53. unsigned int p0, p1, p;
  54. if (base >= MM_MAX_PHYS)
  55. return;
  56. if (end > MM_MAX_PHYS)
  57. end = MM_MAX_PHYS;
  58. p0 = (unsigned int)(base >> 12);
  59. p1 = (unsigned int)(end >> 12);
  60. for (p = p0; p < p1; p++)
  61. bit_clear(p);
  62. }
  63. /* Mark the whole bitmap as used, then walk the E820 map and free RAM. */
  64. void mm_init(void)
  65. {
  66. const struct e820_map *map = (const struct e820_map *)E820_MAP_ADDR;
  67. unsigned int i, n;
  68. /* Everything starts in-use. */
  69. for (i = 0; i < MM_BITMAP_WORDS; i++)
  70. bitmap[i] = 0xFFFFFFFFu;
  71. total_ram_kb = 0;
  72. if (map->magic == E820_MAGIC && map->count <= E820_MAX_ENTRIES) {
  73. n = map->count;
  74. for (i = 0; i < n; i++) {
  75. const struct e820_entry *e = &map->entry[i];
  76. if (e->type == E820_RAM && e->length != 0) {
  77. mark_free(e->base, e->length);
  78. total_ram_kb += (unsigned int)(e->length >> 10);
  79. }
  80. }
  81. } else {
  82. /* No E820 map (very old BIOS, or first kexec onto an old build):
  83. * assume 16 MB of RAM from 1 MB up. The process-stack fallback
  84. * pool keeps the system bootable if this guess is wrong. */
  85. mark_free(0x100000ULL, 0x1000000ULL - 0x100000ULL);
  86. total_ram_kb = (0x1000000ULL - 0x100000ULL) >> 10; /* 15 MB */
  87. }
  88. /* Re-reserve what the kernel already owns. */
  89. mark_used(0, MM_RESERVE_LOW);
  90. mark_used(MM_SHADOW_BASE, MM_SHADOW_RESERVE);
  91. /* The boot path copied the command-pack rootfs into high RAM before
  92. * the kernel ran. Keep it away from the allocator until ramfs_init()
  93. * has taken its own copy. */
  94. {
  95. unsigned int pa = bootinfo_pack_addr();
  96. unsigned int ps = bootinfo_pack_size();
  97. if (pa >= MM_RESERVE_LOW && pa < MM_MAX_PHYS && ps != 0 &&
  98. ps <= MM_MAX_PHYS && pa + ps > pa &&
  99. pa + ps <= MM_MAX_PHYS)
  100. mark_used(pa, ps);
  101. }
  102. /* Count free pages. */
  103. free_pages = 0;
  104. for (i = 0; i < MM_BITMAP_BITS; i++) {
  105. if (!bit_test(i))
  106. free_pages++;
  107. }
  108. }
  109. unsigned int mm_alloc_page(void)
  110. {
  111. unsigned int i, w;
  112. unsigned int flags = irq_disable_save();
  113. for (w = 0; w < MM_BITMAP_WORDS; w++) {
  114. if (bitmap[w] != 0xFFFFFFFFu) {
  115. unsigned int word = bitmap[w];
  116. for (i = 0; i < 32; i++) {
  117. if (!(word & (1u << i))) {
  118. unsigned int idx = w * 32 + i;
  119. bitmap[w] |= (1u << i);
  120. free_pages--;
  121. irq_restore(flags);
  122. return idx * PAGE_SIZE;
  123. }
  124. }
  125. }
  126. }
  127. irq_restore(flags);
  128. return 0;
  129. }
  130. unsigned int mm_alloc_pages(unsigned int n)
  131. {
  132. unsigned int start = 0, run = 0;
  133. unsigned int p;
  134. unsigned int flags;
  135. if (n == 0)
  136. return 0;
  137. flags = irq_disable_save();
  138. for (p = 0; p < MM_BITMAP_BITS; p++) {
  139. if (!bit_test(p)) {
  140. if (run == 0)
  141. start = p;
  142. run++;
  143. if (run == n) {
  144. unsigned int i;
  145. for (i = start; i < start + n; i++)
  146. bit_set(i);
  147. free_pages -= n;
  148. irq_restore(flags);
  149. return start * PAGE_SIZE;
  150. }
  151. } else {
  152. run = 0;
  153. }
  154. }
  155. irq_restore(flags);
  156. return 0;
  157. }
  158. void mm_free_page(unsigned int addr)
  159. {
  160. unsigned int idx = addr / PAGE_SIZE;
  161. unsigned int flags;
  162. if (addr == 0 || idx >= MM_BITMAP_BITS)
  163. return;
  164. flags = irq_disable_save();
  165. if (bit_test(idx)) {
  166. bit_clear(idx);
  167. free_pages++;
  168. }
  169. irq_restore(flags);
  170. }
  171. /* Return a boot-time reservation (the rootfs pack region) to the allocator.
  172. * Low memory and the VGA shadow reserve always stay reserved. */
  173. void mm_free_range(unsigned int base, unsigned int size)
  174. {
  175. unsigned int end, p;
  176. if (base == 0 || size == 0)
  177. return;
  178. base &= ~(PAGE_SIZE - 1u);
  179. if (base + size < base)
  180. return;
  181. end = (base + size + PAGE_SIZE - 1u) & ~(PAGE_SIZE - 1u);
  182. if (base < MM_SHADOW_BASE + MM_SHADOW_RESERVE)
  183. return;
  184. for (p = base; p < end; p += PAGE_SIZE)
  185. mm_free_page(p);
  186. }
  187. unsigned int mm_free_pages(void)
  188. {
  189. return free_pages;
  190. }
  191. unsigned int mm_total_ram_kb(void)
  192. {
  193. return total_ram_kb;
  194. }
  195. unsigned int mm_e820_count(void)
  196. {
  197. const struct e820_map *map = (const struct e820_map *)E820_MAP_ADDR;
  198. if (map->magic == E820_MAGIC && map->count <= E820_MAX_ENTRIES)
  199. return map->count;
  200. return 0;
  201. }
  202. const struct e820_entry *mm_e820_entry(unsigned int i)
  203. {
  204. const struct e820_map *map = (const struct e820_map *)E820_MAP_ADDR;
  205. if (map->magic == E820_MAGIC && map->count <= E820_MAX_ENTRIES &&
  206. i < map->count)
  207. return &map->entry[i];
  208. return 0;
  209. }
  210. void mm_dump(void)
  211. {
  212. const struct e820_map *map = (const struct e820_map *)E820_MAP_ADDR;
  213. unsigned int i;
  214. serial_puts(SERIAL_COM1, "mm: E820 ");
  215. if (map->magic == E820_MAGIC && map->count <= E820_MAX_ENTRIES) {
  216. serial_putdec(SERIAL_COM1, map->count);
  217. serial_puts(SERIAL_COM1, " entries, ");
  218. for (i = 0; i < map->count; i++) {
  219. serial_puts(SERIAL_COM1, "\n [");
  220. serial_puthex(SERIAL_COM1, (unsigned int)map->entry[i].base);
  221. serial_puts(SERIAL_COM1, " +");
  222. serial_puthex(SERIAL_COM1, (unsigned int)map->entry[i].length);
  223. serial_puts(SERIAL_COM1, "] type ");
  224. serial_putdec(SERIAL_COM1, map->entry[i].type);
  225. }
  226. } else {
  227. serial_puts(SERIAL_COM1, "unavailable (fallback 16 MB)");
  228. }
  229. serial_puts(SERIAL_COM1, "\nmm: total ");
  230. serial_putdec(SERIAL_COM1, total_ram_kb);
  231. serial_puts(SERIAL_COM1, " KB, free ");
  232. serial_putdec(SERIAL_COM1, free_pages * (PAGE_SIZE / 1024));
  233. serial_puts(SERIAL_COM1, " KB\n");
  234. }