mm.c 7.1 KB

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