paging.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "cpu.h"
  12. #include "mm.h"
  13. #include "mtrr.h"
  14. #include "serial.h"
  15. #include "vga.h"
  16. /* CR4 bits that must not survive a BIOS protected-mode call. A BIOS that
  17. * returns with CR4.PAE or EFER.LME set makes the first CR0.PG enable either
  18. * walk the 32-bit page directory as a PAE PDPT or enter long mode, which
  19. * triple-faults before the kernel can print anything. The X201's old
  20. * INT 15h AH=87h rootfs copy was one source of this state; clear the bits
  21. * defensively on every boot path before enabling paging. */
  22. #define CR4_PSE (1u << 4)
  23. #define CR4_PAE (1u << 5)
  24. #define CR4_PGE (1u << 7)
  25. #define CR4_LA57 (1u << 12)
  26. #define CR4_PCIDE (1u << 17)
  27. #define CR4_SMEP (1u << 20)
  28. #define CR4_SMAP (1u << 21)
  29. #define MSR_EFER 0xC0000080u
  30. #define EFER_LME (1u << 8)
  31. /* Page-directory entry flags (4 MB page). */
  32. #define PDE_PRESENT 0x001
  33. #define PDE_WRITABLE 0x002
  34. #define PDE_USER 0x004
  35. #define PDE_PWT 0x008
  36. #define PDE_PCD 0x010
  37. #define PDE_PS 0x080 /* 4 MB page */
  38. /* How much of the LFB to map. 16 MB covers any mode up to 1280x1024x32
  39. * (~5 MB) with margin, and is independent of the BIOS's VRAM report. */
  40. #define LFB_MAP_SIZE 0x1000000
  41. static unsigned int *page_dir;
  42. static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
  43. {
  44. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  45. }
  46. static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
  47. {
  48. __asm__ volatile ("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
  49. }
  50. static int cpu_has_long_mode(void)
  51. {
  52. unsigned int eax, ebx, ecx, edx;
  53. if (!cpu_has_cpuid())
  54. return 0;
  55. eax = 0x80000000u;
  56. __asm__ volatile ("cpuid"
  57. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  58. if (eax < 0x80000001u)
  59. return 0;
  60. eax = 0x80000001u;
  61. __asm__ volatile ("cpuid"
  62. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  63. return (edx & (1u << 29)) != 0; /* LM */
  64. }
  65. /* Put the CPU into the state this 32-bit kernel expects before it installs
  66. * its own page directory. A BIOS protected-mode call (notably the old
  67. * high-pack INT 15h AH=87h path) may leave CR4.PAE/PGE or EFER.LME set;
  68. * CR0.PG is off here, so clearing them is safe on every CPU (reserved bits
  69. * are written as zero). */
  70. void paging_prepare_cpu(void)
  71. {
  72. unsigned int cr4, lo, hi;
  73. __asm__ volatile ("movl %%cr4, %0" : "=r"(cr4));
  74. cr4 &= ~(CR4_PAE | CR4_PGE | CR4_LA57 | CR4_PCIDE |
  75. CR4_SMEP | CR4_SMAP);
  76. cr4 |= CR4_PSE;
  77. __asm__ volatile ("movl %0, %%cr4" : : "r"(cr4) : "memory");
  78. if (cpu_has_long_mode()) {
  79. rdmsr(MSR_EFER, &lo, &hi);
  80. if (lo & EFER_LME) {
  81. lo &= ~EFER_LME;
  82. wrmsr(MSR_EFER, lo, hi);
  83. serial_puts(SERIAL_COM1, "paging: cleared EFER.LME\n");
  84. }
  85. }
  86. }
  87. /* LFB page-type decision, kept so it can be re-applied after other MMIO
  88. * mappings (the GPU BARs cover the framebuffer address) have set PCD. */
  89. static unsigned int lfb_wc_base, lfb_wc_size;
  90. static int lfb_wc_ok;
  91. /* Write the LFB PDEs: WC-covered pages cacheable (effective type WC), the
  92. * rest uncacheable. */
  93. static void lfb_apply_pages(unsigned int *pd)
  94. {
  95. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  96. unsigned int base, p0, p1, i;
  97. __asm__ volatile ("" : "+r"(fb));
  98. if (!fb->valid || !fb->lfb_base)
  99. return;
  100. base = fb->lfb_base;
  101. p0 = base >> 22;
  102. /* Compute the end page from the page index, not from base+size: for a
  103. * base above 0xFF000000 the 32-bit addition wraps to a small number
  104. * and the loop would map nothing, faulting on the first LFB write
  105. * after CR0.PG comes on. */
  106. p1 = p0 + (LFB_MAP_SIZE >> 22);
  107. if (p1 > 1024)
  108. p1 = 1024;
  109. for (i = p0; i < p1; i++) {
  110. unsigned int page = i << 22;
  111. unsigned int flags = PDE_PS | PDE_WRITABLE | PDE_PRESENT;
  112. if (!lfb_wc_ok || page < lfb_wc_base ||
  113. page + 0x400000 > lfb_wc_base + lfb_wc_size)
  114. flags |= PDE_PCD;
  115. pd[i] = page | flags;
  116. }
  117. }
  118. /* Re-apply the framebuffer mapping; call after any MMIO mapping that may have
  119. * covered the same addresses (the NVIDIA BAR3 aperture includes the VBE LFB). */
  120. void paging_lfb_refresh(void)
  121. {
  122. if (!lfb_wc_ok || !page_dir)
  123. return;
  124. lfb_apply_pages(page_dir);
  125. serial_puts(SERIAL_COM1, "paging: LFB write-combining re-applied\n");
  126. }
  127. /* 1 when the LFB is mapped for write-combining (WC MTRR + cacheable pages). */
  128. int paging_lfb_wc(void)
  129. {
  130. return lfb_wc_ok;
  131. }
  132. void paging_map_ram(unsigned int base, unsigned int size)
  133. {
  134. unsigned int p0 = base >> 22;
  135. unsigned int p1 = (base + size + 0x3FFFFFu) >> 22;
  136. unsigned int i;
  137. if (!page_dir)
  138. return;
  139. for (i = p0; i < p1 && i < 1024; i++)
  140. page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
  141. }
  142. void paging_init(void)
  143. {
  144. unsigned int i;
  145. const volatile struct vga_boot_fb_info *fb = VGA_BOOT_FB;
  146. paging_prepare_cpu();
  147. /* Opaque asm hides the fixed 0x600 pointer from -Warray-bounds. */
  148. __asm__ volatile ("" : "+r"(fb));
  149. page_dir = (unsigned int *)mm_alloc_page();
  150. if (!page_dir) {
  151. serial_puts(SERIAL_COM1, "paging: out of memory\n");
  152. for (;;)
  153. __asm__ volatile ("cli; hlt");
  154. }
  155. /* Zero the whole page directory. mm_alloc_page does NOT clear memory, so
  156. * on real hardware the high PDEs (indices 32..1023, incl. the user region
  157. * at 0x100) would otherwise be garbage. QEMU zeroes guest RAM, which is
  158. * why this only bites on real silicon. */
  159. for (i = 0; i < 1024; i++)
  160. page_dir[i] = 0;
  161. /* Identity-map RAM [0, MM_MAX_PHYS) with 4 MB pages (write-back). */
  162. for (i = 0; i < MM_MAX_PHYS / 0x400000; i++)
  163. page_dir[i] = (i << 22) | PDE_PS | PDE_WRITABLE | PDE_PRESENT;
  164. /* Identity-map the framebuffer and make VRAM writes batch. Use the
  165. * actual framebuffer size (not the whole mapping) so the WC range
  166. * cannot swallow unrelated MMIO, and install the MTRR *before* choosing
  167. * the page type: a PCD=1 page is uncacheable no matter what the MTRRs
  168. * say, which made every VRAM dword a separate PCI write (~30 MB/s).
  169. * Only the pages fully covered by the WC entry become cacheable (their
  170. * effective type is then WC); everything else stays UC. */
  171. if (fb->valid && fb->lfb_base) {
  172. unsigned int want = (unsigned int)fb->pitch * fb->height;
  173. if (want == 0 || want > LFB_MAP_SIZE)
  174. want = LFB_MAP_SIZE;
  175. lfb_wc_ok = mtrr_set_wc(fb->lfb_base, want,
  176. &lfb_wc_base, &lfb_wc_size);
  177. lfb_apply_pages(page_dir);
  178. /* Tell the truth about VRAM writes: with cacheable pages the WC
  179. * MTRR applies (fast bursts); otherwise every dword is a separate
  180. * uncached write. */
  181. serial_puts(SERIAL_COM1, lfb_wc_ok
  182. ? "paging: LFB write-combining\n"
  183. : "paging: LFB uncached (no WC MTRR)\n");
  184. }
  185. /* Load CR3, enable PSE, then PG + WP. The jump flushes the prefetch
  186. * queue after the paging flip. */
  187. __asm__ volatile (
  188. "movl %0, %%cr3\n\t"
  189. "movl %%cr4, %%eax\n\t"
  190. "orl $0x10, %%eax\n\t" /* CR4.PSE */
  191. "movl %%eax, %%cr4\n\t"
  192. "movl %%cr0, %%eax\n\t"
  193. "orl $0x80010000, %%eax\n\t" /* CR0.PG | CR0.WP */
  194. "movl %%eax, %%cr0\n\t"
  195. "jmp 1f\n\t"
  196. "1:\n\t"
  197. :
  198. : "r"((unsigned int)page_dir)
  199. : "eax", "memory");
  200. serial_puts(SERIAL_COM1, "paging: enabled (identity map)\n");
  201. }
  202. unsigned int paging_current_cr3(void)
  203. {
  204. unsigned int cr3;
  205. __asm__ volatile ("movl %%cr3, %0" : "=r"(cr3));
  206. return cr3 & 0xFFFFF000u;
  207. }
  208. unsigned int paging_kernel_cr3(void)
  209. {
  210. return (unsigned int)page_dir;
  211. }
  212. unsigned int paging_new_directory(void)
  213. {
  214. unsigned int *pd = (unsigned int *)mm_alloc_page();
  215. unsigned int i;
  216. if (!pd)
  217. return 0;
  218. for (i = 0; i < 1024; i++)
  219. pd[i] = page_dir[i];
  220. return (unsigned int)pd;
  221. }
  222. int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
  223. unsigned int flags)
  224. {
  225. unsigned int *pd = (unsigned int *)cr3;
  226. unsigned int pde = va >> 22;
  227. unsigned int pte = (va >> 12) & 0x3FF;
  228. unsigned int *pt;
  229. if (!(pd[pde] & PDE_PRESENT)) {
  230. unsigned int pt_phys = mm_alloc_page();
  231. unsigned int i;
  232. if (!pt_phys)
  233. return 0;
  234. pt = (unsigned int *)pt_phys;
  235. for (i = 0; i < 1024; i++)
  236. pt[i] = 0;
  237. pd[pde] = pt_phys | PDE_USER | PDE_WRITABLE | PDE_PRESENT;
  238. } else {
  239. pt = (unsigned int *)(pd[pde] & 0xFFFFF000);
  240. }
  241. pt[pte] = (phys & 0xFFFFF000) | flags;
  242. return 1;
  243. }
  244. unsigned int paging_page_phys(unsigned int cr3, unsigned int va)
  245. {
  246. unsigned int *pd = (unsigned int *)cr3;
  247. unsigned int pde = pd[va >> 22];
  248. unsigned int pte = (va >> 12) & 0x3FF;
  249. if (!(pde & PDE_PRESENT) || (pde & PDE_PS))
  250. return 0;
  251. {
  252. unsigned int e = ((unsigned int *)(pde & 0xFFFFF000))[pte];
  253. return (e & PTE_PRESENT) ? (e & 0xFFFFF000) : 0;
  254. }
  255. }
  256. void paging_unmap_page(unsigned int cr3, unsigned int va)
  257. {
  258. unsigned int *pd = (unsigned int *)cr3;
  259. unsigned int pde = va >> 22;
  260. unsigned int pte = (va >> 12) & 0x3FF;
  261. if (!(pd[pde] & PDE_PRESENT))
  262. return;
  263. ((unsigned int *)(pd[pde] & 0xFFFFF000))[pte] = 0;
  264. }
  265. int paging_user_range_ok(unsigned int cr3, unsigned int va,
  266. unsigned int size, int write)
  267. {
  268. unsigned int *pd = (unsigned int *)cr3;
  269. unsigned int end = va + size;
  270. unsigned int p;
  271. if (!pd || size == 0 || end < va)
  272. return 0;
  273. for (p = va & 0xFFFFF000u; p < end; p += PAGE_SIZE) {
  274. unsigned int pde = pd[p >> 22];
  275. unsigned int *pt;
  276. unsigned int pte;
  277. if (!(pde & PDE_PRESENT) || (pde & PDE_PS) || !(pde & PDE_USER))
  278. return 0;
  279. pt = (unsigned int *)(pde & 0xFFFFF000);
  280. pte = pt[(p >> 12) & 0x3FF];
  281. if (!(pte & PTE_PRESENT) || !(pte & PTE_USER))
  282. return 0;
  283. if (write && !(pte & PTE_WRITABLE))
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. void paging_release_user_range(unsigned int cr3, unsigned int va,
  289. unsigned int size, int free_phys)
  290. {
  291. unsigned int *pd = (unsigned int *)cr3;
  292. unsigned int end = va + size;
  293. unsigned int first, last, i, j;
  294. if (!pd || size == 0 || end < va)
  295. return;
  296. first = va >> 22;
  297. last = (end - 1) >> 22;
  298. for (i = first; i <= last && i < 1024; i++) {
  299. unsigned int pde = pd[i];
  300. unsigned int *pt;
  301. int empty = 1;
  302. /* Kernel identity mappings are 4 MB pages and must never be
  303. * touched here. */
  304. if (!(pde & PDE_PRESENT) || (pde & PDE_PS))
  305. continue;
  306. pt = (unsigned int *)(pde & 0xFFFFF000);
  307. for (j = 0; j < 1024; j++) {
  308. unsigned int pva = (i << 22) | (j << 12);
  309. if (pva < va || pva >= end)
  310. continue;
  311. if (pt[j] & PTE_PRESENT) {
  312. /* Only user pages are owned by the process; a
  313. * supervisor mapping in this range is left for
  314. * its owner. */
  315. if (free_phys && (pt[j] & PTE_USER))
  316. mm_free_page(pt[j] & 0xFFFFF000);
  317. pt[j] = 0;
  318. }
  319. }
  320. for (j = 0; j < 1024; j++)
  321. if (pt[j] & PTE_PRESENT) {
  322. empty = 0;
  323. break;
  324. }
  325. if (empty) {
  326. mm_free_page(pde & 0xFFFFF000);
  327. pd[i] = 0;
  328. }
  329. }
  330. }
  331. /* Identity-map an MMIO range uncacheable into one page directory (4 MB pages;
  332. * the range is rounded up to a whole number of them). */
  333. void paging_map_mmio_cr3(unsigned int cr3, unsigned int phys_base, unsigned int size)
  334. {
  335. unsigned int *pd = (unsigned int *)cr3;
  336. unsigned int p0 = phys_base >> 22;
  337. unsigned int p1 = (phys_base + size + 0x3FFFFFu) >> 22;
  338. unsigned int i;
  339. for (i = p0; i < p1 && i < 1024; i++)
  340. pd[i] = (i << 22) | PDE_PS | PDE_PCD |
  341. PDE_WRITABLE | PDE_PRESENT;
  342. }
  343. void paging_map_mmio(unsigned int phys_base, unsigned int size)
  344. {
  345. paging_map_mmio_cr3((unsigned int)page_dir, phys_base, size);
  346. }