mm.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_HIGH_LIMIT): lowmem below
  18. * MM_MAX_PHYS, highmem above it. Bitmap: 1 bit/page.
  19. * The ceiling lives in phys.h so the UEFI loader cannot drift; see
  20. * kernel.ld for the loaded-image bound. */
  21. /* Regions the kernel already owns (marked used at init): */
  22. #define MM_RESERVE_LOW 0x100000 /* 0 .. 1 MB: kernel/DMA/UHCI/UMA/boot */
  23. #define MM_SHADOW_BASE 0x100000 /* VGA shadow buffer base */
  24. #define MM_SHADOW_RESERVE 0x800000 /* conservative 8 MB for the shadow */
  25. /* E820 entry type codes. */
  26. #define E820_RAM 1
  27. #define E820_RESERVED 2
  28. /* One E820 entry (ACPI 3.0, 24 bytes). */
  29. struct e820_entry {
  30. unsigned long long base;
  31. unsigned long long length;
  32. unsigned int type;
  33. unsigned int attrs;
  34. } __attribute__((packed));
  35. struct e820_map {
  36. unsigned int magic;
  37. unsigned int count;
  38. struct e820_entry entry[E820_MAX_ENTRIES];
  39. };
  40. void mm_init(void);
  41. /* Return the base address of a free 4 KB lowmem page (identity-mapped,
  42. * below MM_MAX_PHYS), or 0 if none. */
  43. unsigned int mm_alloc_page(void);
  44. /* A frame for a ring-3 mapping, preferring highmem; 0 if none. It may lie
  45. * above MM_MAX_PHYS: only touch it through kmap() or the user mapping. */
  46. unsigned int mm_alloc_user_page(void);
  47. /* Free pages above MM_MAX_PHYS (a slow count, for reporting). */
  48. unsigned int mm_free_high_pages(void);
  49. void mm_free_page(unsigned int addr);
  50. /* Free a page-aligned boot-time reservation (never low 1 MB / the shadow). */
  51. void mm_free_range(unsigned int base, unsigned int size);
  52. /* Return n contiguous lowmem pages (base address), or 0 if unavailable. */
  53. unsigned int mm_alloc_pages(unsigned int n);
  54. unsigned int mm_free_pages(void);
  55. /* Total usable RAM reported by E820, in KB. */
  56. unsigned int mm_total_ram_kb(void);
  57. /* E820 map accessors (count = 0 when the map is unavailable). */
  58. unsigned int mm_e820_count(void);
  59. const struct e820_entry *mm_e820_entry(unsigned int i);
  60. /* Dump the memory map + allocator stats to COM1. */
  61. void mm_dump(void);
  62. #endif