paging.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /* Re-apply the framebuffer page types (after GPU BAR mappings that cover it). */
  11. void paging_lfb_refresh(void);
  12. /* 1 when the LFB is mapped for write-combining. */
  13. int paging_lfb_wc(void);
  14. /* Map a physical window identity-mapped with caching enabled (4 MB pages).
  15. * Used for the stolen-VRAM alias, which is ordinary DRAM; the effective type
  16. * is whatever the MTRRs say (write-combining when we added an entry). */
  17. void paging_map_ram(unsigned int base, unsigned int size);
  18. /* Physical address of the page directory currently loaded in CR3. On SMP this
  19. * is per-CPU; on one CPU it is the running process's directory. */
  20. unsigned int paging_current_cr3(void);
  21. /* Physical address of the bootstrap/kernel page directory that new process
  22. * directories clone. */
  23. unsigned int paging_kernel_cr3(void);
  24. /* Allocate a fresh page directory that clones the kernel identity map and
  25. * return its physical address (0 on out-of-memory). */
  26. unsigned int paging_new_directory(void);
  27. /* Page-table entry flags (4 KB pages). */
  28. #define PTE_PRESENT 0x001
  29. #define PTE_WRITABLE 0x002
  30. #define PTE_USER 0x004
  31. /* Temporary kernel access to any frame, including highmem (above
  32. * MM_MAX_PHYS). Lowmem returns the identity address; highmem takes a
  33. * per-CPU slot in the MM_KMAP_VA window with preemption disabled until the
  34. * matching kunmap(). Mappings nest (LIFO, 4 per CPU); do not sleep, yield
  35. * or take the BKL in between. */
  36. void *kmap(unsigned int phys);
  37. void kunmap(void *addr);
  38. /* Map one 4 KB page (va -> phys) in page directory `cr3`, allocating a page
  39. * table if needed. Returns 1 on success, 0 on out-of-memory. */
  40. int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys,
  41. unsigned int flags);
  42. /* Physical address mapped at `va`, or 0 when the page is not present. */
  43. unsigned int paging_page_phys(unsigned int cr3, unsigned int va);
  44. /* Clear one 4 KB page (va) in page directory `cr3` (no-op when the page
  45. * table is absent). */
  46. void paging_unmap_page(unsigned int cr3, unsigned int va);
  47. /* Release the user mappings in [va, va+size): clear the PTEs, free empty page
  48. * tables and, when `free_phys` is set, free the mapped physical pages. With
  49. * `free_phys` clear the pages belong to someone else (e.g. a mapped window
  50. * canvas) and only the mapping/page tables are released. Used by the process
  51. * reaper; ranges should cover the user-owned mappings only. */
  52. void paging_release_user_range(unsigned int cr3, unsigned int va,
  53. unsigned int size, int free_phys);
  54. /* 1 when every page in [va, va+size) is a user-accessible 4 KB mapping in
  55. * `cr3` (and writable when `write` is set). Syscall services use this before
  56. * touching a pointer supplied by ring 3. */
  57. int paging_user_range_ok(unsigned int cr3, unsigned int va,
  58. unsigned int size, int write);
  59. /* Identity-map an MMIO range (e.g. a GPU register aperture) uncacheable into
  60. * the current page directory. Uses 4 MB pages; the range is rounded up to a
  61. * whole number of 4 MB pages. */
  62. void paging_map_mmio(unsigned int phys_base, unsigned int size);
  63. /* Same, but into an explicit page directory (used to update per-process
  64. * directories; see process_map_mmio_all()). */
  65. void paging_map_mmio_cr3(unsigned int cr3, unsigned int phys_base,
  66. unsigned int size);
  67. #endif