paging.c 13 KB

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