mm.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Physical memory manager: E820 detection + page-frame allocator.
  2. *
  3. * The real-mode VESA stage (vesa_stage.S) probes INT 15h E820 and stores the
  4. * result at E820_MAP_ADDR (0x1000, below the kernel, so it survives kexec).
  5. * mm_init() parses that map and builds a bitmap over physical RAM, reserving
  6. * the low 1 MB (kernel, DMA, UHCI, UMA, boot structures) plus the VGA shadow
  7. * buffer region. Everything above that is handed out 4 KB pages at a time.
  8. */
  9. #ifndef MM_H
  10. #define MM_H
  11. #define PAGE_SIZE 0x1000
  12. /* E820 map storage (written by vesa_stage.S in real mode). */
  13. #define E820_MAP_ADDR 0x1000
  14. #define E820_MAGIC 0xE820E820
  15. #define E820_MAX_ENTRIES 32
  16. /* Frame allocator covers physical RAM [0, MM_MAX_PHYS). Bitmap: 1 bit/page.
  17. * 128 MB matches this machine and QEMU's default; raise it (and move the
  18. * kernel upper bound, see kernel.ld) if larger machines are targeted. */
  19. #define MM_MAX_PHYS (128u * 1024 * 1024) /* 128 MB ceiling */
  20. /* Regions the kernel already owns (marked used at init): */
  21. #define MM_RESERVE_LOW 0x100000 /* 0 .. 1 MB: kernel/DMA/UHCI/UMA/boot */
  22. #define MM_SHADOW_BASE 0x100000 /* VGA shadow buffer base */
  23. #define MM_SHADOW_RESERVE 0x800000 /* conservative 8 MB for the shadow */
  24. /* E820 entry type codes. */
  25. #define E820_RAM 1
  26. #define E820_RESERVED 2
  27. /* One E820 entry (ACPI 3.0, 24 bytes). */
  28. struct e820_entry {
  29. unsigned long long base;
  30. unsigned long long length;
  31. unsigned int type;
  32. unsigned int attrs;
  33. } __attribute__((packed));
  34. struct e820_map {
  35. unsigned int magic;
  36. unsigned int count;
  37. struct e820_entry entry[E820_MAX_ENTRIES];
  38. };
  39. void mm_init(void);
  40. /* Return the base address of a free 4 KB page, or 0 if none. */
  41. unsigned int mm_alloc_page(void);
  42. void mm_free_page(unsigned int addr);
  43. /* Return n contiguous pages (base address), or 0 if unavailable. */
  44. unsigned int mm_alloc_pages(unsigned int n);
  45. unsigned int mm_free_pages(void);
  46. /* Total usable RAM reported by E820, in KB. */
  47. unsigned int mm_total_ram_kb(void);
  48. /* E820 map accessors (count = 0 when the map is unavailable). */
  49. unsigned int mm_e820_count(void);
  50. const struct e820_entry *mm_e820_entry(unsigned int i);
  51. /* Dump the memory map + allocator stats to COM1. */
  52. void mm_dump(void);
  53. #endif