paging.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "mtrr.h"
  13. #include "serial.h"
  14. #include "vga.h"
  15. /* Page-directory entry flags (4 MB page). */
  16. #define PDE_PRESENT 0x001
  17. #define PDE_WRITABLE 0x002
  18. #define PDE_USER 0x004
  19. #define PDE_PWT 0x008
  20. #define PDE_PCD 0x010
  21. #define PDE_PS 0x080 /* 4 MB page */
  22. /* How much of the LFB to map. 16 MB covers any mode up to 1280x1024x32
  23. * (~5 MB) with margin, and is independent of the BIOS's VRAM report. */
  24. #define LFB_MAP_SIZE 0x1000000
  25. static unsigned int *page_dir;
  26. /* LFB page-type decision, kept so it can be re-applied after other MMIO
  27. * mappings (the GPU BARs cover the framebuffer address) have set PCD. */
  28. static unsigned int lfb_wc_base, lfb_wc_size;
  29. static int lfb_wc_ok;
  30. /* Write the LFB PDEs: WC-covered pages cacheable (effective type WC), the
  31. * rest uncacheable. */
  32. static void lfb_apply_pages(unsigned int *pd)
  33. {
  34. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  35. unsigned int base, p0, p1, i;
  36. __asm__ volatile ("" : "+r"(fb));
  37. if (!fb->valid || !fb->lfb_base)
  38. return;
  39. base = fb->lfb_base;
  40. p0 = base >> 22;
  41. p1 = (base + LFB_MAP_SIZE) >> 22;
  42. for (i = p0; i < p1 && i < 1024; i++) {
  43. unsigned int page = i << 22;
  44. unsigned int flags = PDE_PS | PDE_WRITABLE | PDE_PRESENT;
  45. if (!lfb_wc_ok || page < lfb_wc_base ||
  46. page + 0x400000 > lfb_wc_base + lfb_wc_size)
  47. flags |= PDE_PCD;
  48. pd[i] = page | flags;
  49. }
  50. }
  51. /* Re-apply the framebuffer mapping; call after any MMIO mapping that may have
  52. * covered the same addresses (the NVIDIA BAR3 aperture includes the VBE LFB). */
  53. void paging_lfb_refresh(void)
  54. {
  55. if (!lfb_wc_ok || !page_dir)
  56. return;
  57. lfb_apply_pages(page_dir);
  58. serial_puts(SERIAL_COM1, "paging: LFB write-combining re-applied\n");
  59. }
  60. /* 1 when the LFB is mapped for write-combining (WC MTRR + cacheable pages). */
  61. int paging_lfb_wc(void)
  62. {
  63. return lfb_wc_ok;
  64. }
  65. void paging_init(void)
  66. {
  67. unsigned int i;
  68. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  69. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  70. __asm__ volatile ("" : "+r"(fb));
  71. page_dir = (unsigned int *)mm_alloc_page();
  72. if (!page_dir) {
  73. serial_puts(SERIAL_COM1, "paging: out of memory\n");
  74. for (;;)
  75. __asm__ volatile ("cli; hlt");
  76. }
  77. /* Zero the whole page directory. mm_alloc_page does NOT clear memory, so
  78. * on real hardware the high PDEs (indices 32..1023, incl. the user region
  79. * at 0x100) would otherwise be garbage. QEMU zeroes guest RAM, which is
  80. * why this only bites on real silicon. */
  81. for (i = 0; i < 1024; i++)
  82. page_dir[i] = 0;
  83. /* Identity-map RAM [0, MM_MAX_PHYS) with 4 MB pages (write-back). */
  84. for (i = 0; i < MM_MAX_PHYS / 0x400000; i++)
  85. page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
  86. /* Identity-map the framebuffer and make VRAM writes batch. Use the
  87. * actual framebuffer size (not the whole mapping) so the WC range
  88. * cannot swallow unrelated MMIO, and install the MTRR *before* choosing
  89. * the page type: a PCD=1 page is uncacheable no matter what the MTRRs
  90. * say, which made every VRAM dword a separate PCI write (~30 MB/s).
  91. * Only the pages fully covered by the WC entry become cacheable (their
  92. * effective type is then WC); everything else stays UC. */
  93. if (fb->valid && fb->lfb_base) {
  94. unsigned int want = (unsigned int)fb->pitch * fb->height;
  95. if (want == 0 || want > LFB_MAP_SIZE)
  96. want = LFB_MAP_SIZE;
  97. lfb_wc_ok = mtrr_set_wc(fb->lfb_base, want,
  98. &lfb_wc_base, &lfb_wc_size);
  99. lfb_apply_pages(page_dir);
  100. /* Tell the truth about VRAM writes: with cacheable pages the WC
  101. * MTRR applies (fast bursts); otherwise every dword is a separate
  102. * uncached write. */
  103. serial_puts(SERIAL_COM1, lfb_wc_ok
  104. ? "paging: LFB write-combining\n"
  105. : "paging: LFB uncached (no WC MTRR)\n");
  106. }
  107. /* Load CR3, enable PSE, then PG + WP. The jump flushes the prefetch
  108. * queue after the paging flip. */
  109. __asm__ volatile (
  110. "movl %0, %%cr3\n\t"
  111. "movl %%cr4, %%eax\n\t"
  112. "orl $0x10, %%eax\n\t" /* CR4.PSE */
  113. "movl %%eax, %%cr4\n\t"
  114. "movl %%cr0, %%eax\n\t"
  115. "orl $0x80010000, %%eax\n\t" /* CR0.PG | CR0.WP */
  116. "movl %%eax, %%cr0\n\t"
  117. "jmp 1f\n\t"
  118. "1:\n\t"
  119. :
  120. : "r"((unsigned int)page_dir)
  121. : "eax", "memory");
  122. serial_puts(SERIAL_COM1, "paging: enabled (identity map)\n");
  123. }
  124. unsigned int paging_current_cr3(void)
  125. {
  126. return (unsigned int)page_dir;
  127. }
  128. unsigned int paging_new_directory(void)
  129. {
  130. unsigned int *pd = (unsigned int *)mm_alloc_page();
  131. unsigned int i;
  132. if (!pd)
  133. return 0;
  134. for (i = 0; i < 1024; i++)
  135. pd[i] = page_dir[i];
  136. return (unsigned int)pd;
  137. }
  138. int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
  139. unsigned int flags)
  140. {
  141. unsigned int *pd = (unsigned int *)cr3;
  142. unsigned int pde = va >> 22;
  143. unsigned int pte = (va >> 12) & 0x3FF;
  144. unsigned int *pt;
  145. if (!(pd[pde] & PDE_PRESENT)) {
  146. unsigned int pt_phys = mm_alloc_page();
  147. unsigned int i;
  148. if (!pt_phys)
  149. return 0;
  150. pt = (unsigned int *)pt_phys;
  151. for (i = 0; i < 1024; i++)
  152. pt[i] = 0;
  153. pd[pde] = pt_phys | PDE_USER | PDE_WRITABLE | PDE_PRESENT;
  154. } else {
  155. pt = (unsigned int *)(pd[pde] & 0xFFFFF000);
  156. }
  157. pt[pte] = (phys & 0xFFFFF000) | flags;
  158. return 1;
  159. }
  160. void paging_unmap_page(unsigned int cr3, unsigned int va)
  161. {
  162. unsigned int *pd = (unsigned int *)cr3;
  163. unsigned int pde = va >> 22;
  164. unsigned int pte = (va >> 12) & 0x3FF;
  165. if (!(pd[pde] & PDE_PRESENT))
  166. return;
  167. ((unsigned int *)(pd[pde] & 0xFFFFF000))[pte] = 0;
  168. }
  169. /* Identity-map an MMIO range uncacheable into one page directory (4 MB pages;
  170. * the range is rounded up to a whole number of them). */
  171. void paging_map_mmio_cr3(unsigned int cr3, unsigned int phys_base, unsigned int size)
  172. {
  173. unsigned int *pd = (unsigned int *)cr3;
  174. unsigned int p0 = phys_base >> 22;
  175. unsigned int p1 = (phys_base + size + 0x3FFFFFu) >> 22;
  176. unsigned int i;
  177. for (i = p0; i < p1 && i < 1024; i++)
  178. pd[i] = (i << 22) | PDE_PS | PDE_PCD |
  179. PDE_WRITABLE | PDE_PRESENT;
  180. }
  181. void paging_map_mmio(unsigned int phys_base, unsigned int size)
  182. {
  183. paging_map_mmio_cr3((unsigned int)page_dir, phys_base, size);
  184. }