mm.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "phys.h"
  12. #define PAGE_SIZE 0x1000
  13. /* E820 map storage (written by vesa_stage.S in real mode). */
  14. #define E820_MAP_ADDR 0x1000
  15. #define E820_MAGIC 0xE820E820
  16. #define E820_MAX_ENTRIES 32
  17. /* Frame allocator covers physical RAM [0, MM_MAX_PHYS). Bitmap: 1 bit/page.
  18. * The ceiling lives in phys.h so the UEFI loader cannot drift; see
  19. * kernel.ld for the loaded-image bound. */
  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. /* Free a page-aligned boot-time reservation (never low 1 MB / the shadow). */
  44. void mm_free_range(unsigned int base, unsigned int size);
  45. /* Return n contiguous pages (base address), or 0 if unavailable. */
  46. unsigned int mm_alloc_pages(unsigned int n);
  47. unsigned int mm_free_pages(void);
  48. /* Total usable RAM reported by E820, in KB. */
  49. unsigned int mm_total_ram_kb(void);
  50. /* E820 map accessors (count = 0 when the map is unavailable). */
  51. unsigned int mm_e820_count(void);
  52. const struct e820_entry *mm_e820_entry(unsigned int i);
  53. /* Dump the memory map + allocator stats to COM1. */
  54. void mm_dump(void);
  55. #endif