paging.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Paging: identity-map RAM + the VESA LFB and enable CR0.PG (4 MB pages). */
  2. #ifndef PAGING_H
  3. #define PAGING_H
  4. void paging_init(void);
  5. /* Clear translation controls a BIOS protected-mode call may have left set
  6. * (CR4.PAE/PGE/LA57/PCIDE, EFER.LME) before this 32-bit kernel enables its
  7. * own page directory. Called by paging_init(); the AP trampoline applies
  8. * the same policy inline. */
  9. void paging_prepare_cpu(void);
  10. /* 1 when the LFB is mapped for write-combining. */
  11. int paging_lfb_wc(void);
  12. /* Kernel address of the boot framebuffer (the identity address when it is
  13. * in lowmem, else its ioremap() window address); 0 without an LFB. */
  14. void *paging_lfb_va(void);
  15. /* Map physical [phys, phys+size) for kernel access and return the address
  16. * of `phys`. Lowmem (below MM_MAX_PHYS) is identity-mapped and returned
  17. * as-is (an IOREMAP_MMIO range there has its 4 MB pages made uncacheable);
  18. * anything else gets 4 KB pages in the kernel-only window at IOMAP_BASE,
  19. * shared by every process directory. IOREMAP_MMIO pages are uncached
  20. * except where the LFB's WC MTRR covers them; IOREMAP_RAM pages (firmware
  21. * tables, the stolen-VRAM alias) are write-back. A range already covered
  22. * by a mapping of the same type reuses it. Mappings are never torn down.
  23. * Returns 0 when the window is full or out of page tables. */
  24. #define IOREMAP_MMIO 0
  25. #define IOREMAP_RAM 1
  26. void *ioremap(unsigned int phys, unsigned int size, int type);
  27. /* Window address of `phys` if an ioremap() covers it, else 0 (peek/poke). */
  28. void *ioremap_lookup(unsigned int phys);
  29. /* Debug access (peek/poke/dbg) to physical byte `pa`: lowmem directly,
  30. * highmem RAM through a kmap (*km is set; kunmap(*km) when done), MMIO
  31. * through its ioremap() mapping, created on demand (4 KB, uncached).
  32. * 0 when it cannot be mapped. */
  33. volatile unsigned char *paging_debug_map(unsigned int pa, void **km);
  34. /* Physical address of the page directory currently loaded in CR3. On SMP this
  35. * is per-CPU; on one CPU it is the running process's directory. */
  36. unsigned int paging_current_cr3(void);
  37. /* Physical address of the bootstrap/kernel page directory that new process
  38. * directories clone. */
  39. unsigned int paging_kernel_cr3(void);
  40. /* Allocate a fresh page directory that clones the kernel identity map and
  41. * return its physical address (0 on out-of-memory). */
  42. unsigned int paging_new_directory(void);
  43. /* Page-table entry flags (4 KB pages). */
  44. #define PTE_PRESENT 0x001
  45. #define PTE_WRITABLE 0x002
  46. #define PTE_USER 0x004
  47. /* Temporary kernel access to any frame, including highmem (above
  48. * MM_MAX_PHYS). Lowmem returns the identity address; highmem takes a
  49. * per-CPU slot in the MM_KMAP_VA window with preemption disabled until the
  50. * matching kunmap(). Mappings nest (LIFO, 4 per CPU); do not sleep, yield
  51. * or take the BKL in between. */
  52. void *kmap(unsigned int phys);
  53. void kunmap(void *addr);
  54. /* Map one 4 KB page (va -> phys) in page directory `cr3`, allocating a page
  55. * table if needed. Returns 1 on success, 0 on out-of-memory. */
  56. int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
  57. unsigned int flags);
  58. /* Physical address mapped at `va`, or 0 when the page is not present. */
  59. unsigned int paging_page_phys(unsigned int cr3, unsigned int va);
  60. /* Clear one 4 KB page (va) in page directory `cr3` (no-op when the page
  61. * table is absent). */
  62. void paging_unmap_page(unsigned int cr3, unsigned int va);
  63. /* Release the user mappings in [va, va+size): clear the PTEs, free empty page
  64. * tables and, when `free_phys` is set, free the mapped physical pages. With
  65. * `free_phys` clear the pages belong to someone else (e.g. a mapped window
  66. * canvas) and only the mapping/page tables are released. Used by the process
  67. * reaper; ranges should cover the user-owned mappings only. */
  68. void paging_release_user_range(unsigned int cr3, unsigned int va,
  69. unsigned int size, int free_phys);
  70. /* 1 when every page in [va, va+size) is a user-accessible 4 KB mapping in
  71. * `cr3` (and writable when `write` is set). Syscall services use this before
  72. * touching a pointer supplied by ring 3. */
  73. int paging_user_range_ok(unsigned int cr3, unsigned int va,
  74. unsigned int size, int write);
  75. #endif