paging.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Paging: identity-map RAM + the VESA LFB, enable CR0.PG (4 MB pages).
  2. *
  3. * This is the "paging on but flat" stage: every physical address is mapped
  4. * 1:1, so all existing code (kernel, stacks, DMA, UHCI, VGA shadow) keeps
  5. * working unchanged. Per-process page directories come later (Phase 3b/4).
  6. *
  7. * Uses 4 MB pages via CR4.PSE (supported since the original Pentium), so the
  8. * whole map is a single 4 KB page directory.
  9. */
  10. #include "paging.h"
  11. #include "mm.h"
  12. #include "serial.h"
  13. #include "vga.h"
  14. /* Page-directory entry flags (4 MB page). */
  15. #define PDE_PRESENT 0x001
  16. #define PDE_WRITABLE 0x002
  17. #define PDE_USER 0x004
  18. #define PDE_PWT 0x008
  19. #define PDE_PCD 0x010
  20. #define PDE_PS 0x080 /* 4 MB page */
  21. /* How much of the LFB to map. 16 MB covers any mode up to 1280x1024x32
  22. * (~5 MB) with margin, and is independent of the BIOS's VRAM report. */
  23. #define LFB_MAP_SIZE 0x1000000
  24. static unsigned int *page_dir;
  25. void paging_init(void)
  26. {
  27. unsigned int i;
  28. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  29. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  30. __asm__ volatile ("" : "+r"(fb));
  31. page_dir = (unsigned int *)mm_alloc_page();
  32. if (!page_dir) {
  33. serial_puts(SERIAL_COM1, "paging: out of memory\n");
  34. for (;;)
  35. __asm__ volatile ("cli; hlt");
  36. }
  37. /* Zero the whole page directory. mm_alloc_page does NOT clear memory, so
  38. * on real hardware the high PDEs (indices 32..1023, incl. the user region
  39. * at 0x100) would otherwise be garbage. QEMU zeroes guest RAM, which is
  40. * why this only bites on real silicon. */
  41. for (i = 0; i < 1024; i++)
  42. page_dir[i] = 0;
  43. /* Identity-map RAM [0, MM_MAX_PHYS) with 4 MB pages (write-back). */
  44. for (i = 0; i < MM_MAX_PHYS / 0x400000; i++)
  45. page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
  46. /* Identity-map the framebuffer (MMIO, uncacheable) so the screen works. */
  47. if (fb->valid && fb->lfb_base) {
  48. unsigned int base = fb->lfb_base;
  49. unsigned int p0 = base >> 22;
  50. unsigned int p1 = (base + LFB_MAP_SIZE) >> 22;
  51. for (i = p0; i < p1 && i < 1024; i++)
  52. page_dir[i] = (i << 22) | PDE_PS | PDE_PCD |
  53. PDE_WRITABLE | PDE_PRESENT;
  54. }
  55. /* Load CR3, enable PSE, then PG + WP. The jump flushes the prefetch
  56. * queue after the paging flip. */
  57. __asm__ volatile (
  58. "movl %0, %%cr3\n\t"
  59. "movl %%cr4, %%eax\n\t"
  60. "orl $0x10, %%eax\n\t" /* CR4.PSE */
  61. "movl %%eax, %%cr4\n\t"
  62. "movl %%cr0, %%eax\n\t"
  63. "orl $0x80010000, %%eax\n\t" /* CR0.PG | CR0.WP */
  64. "movl %%eax, %%cr0\n\t"
  65. "jmp 1f\n\t"
  66. "1:\n\t"
  67. :
  68. : "r"((unsigned int)page_dir)
  69. : "eax", "memory");
  70. serial_puts(SERIAL_COM1, "paging: enabled (identity map)\n");
  71. }
  72. unsigned int paging_current_cr3(void)
  73. {
  74. return (unsigned int)page_dir;
  75. }
  76. unsigned int paging_new_directory(void)
  77. {
  78. unsigned int *pd = (unsigned int *)mm_alloc_page();
  79. unsigned int i;
  80. if (!pd)
  81. return 0;
  82. for (i = 0; i < 1024; i++)
  83. pd[i] = page_dir[i];
  84. return (unsigned int)pd;
  85. }
  86. int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
  87. unsigned int flags)
  88. {
  89. unsigned int *pd = (unsigned int *)cr3;
  90. unsigned int pde = va >> 22;
  91. unsigned int pte = (va >> 12) & 0x3FF;
  92. unsigned int *pt;
  93. if (!(pd[pde] & PDE_PRESENT)) {
  94. unsigned int pt_phys = mm_alloc_page();
  95. unsigned int i;
  96. if (!pt_phys)
  97. return 0;
  98. pt = (unsigned int *)pt_phys;
  99. for (i = 0; i < 1024; i++)
  100. pt[i] = 0;
  101. pd[pde] = pt_phys | PDE_USER | PDE_WRITABLE | PDE_PRESENT;
  102. } else {
  103. pt = (unsigned int *)(pd[pde] & 0xFFFFF000);
  104. }
  105. pt[pte] = (phys & 0xFFFFF000) | flags;
  106. return 1;
  107. }