/* Paging: identity-map RAM + the VESA LFB and enable CR0.PG (4 MB pages). */ #ifndef PAGING_H #define PAGING_H void paging_init(void); /* Clear translation controls a BIOS protected-mode call may have left set * (CR4.PAE/PGE/LA57/PCIDE, EFER.LME) before this 32-bit kernel enables its * own page directory. Called by paging_init(); the AP trampoline applies * the same policy inline. */ void paging_prepare_cpu(void); /* 1 when the LFB is mapped for write-combining. */ int paging_lfb_wc(void); /* Kernel address of the boot framebuffer (the identity address when it is * in lowmem, else its ioremap() window address); 0 without an LFB. */ void *paging_lfb_va(void); /* Map physical [phys, phys+size) for kernel access and return the address * of `phys`. Lowmem (below MM_MAX_PHYS) is identity-mapped and returned * as-is (an IOREMAP_MMIO range there has its 4 MB pages made uncacheable); * anything else gets 4 KB pages in the kernel-only window at IOMAP_BASE, * shared by every process directory. IOREMAP_MMIO pages are uncached * except where the LFB's WC MTRR covers them; IOREMAP_RAM pages (firmware * tables, the stolen-VRAM alias) are write-back. A range already covered * by a mapping of the same type reuses it. Mappings are never torn down. * Returns 0 when the window is full or out of page tables. */ #define IOREMAP_MMIO 0 #define IOREMAP_RAM 1 void *ioremap(unsigned int phys, unsigned int size, int type); /* Window address of `phys` if an ioremap() covers it, else 0 (peek/poke). */ void *ioremap_lookup(unsigned int phys); /* Debug access (peek/poke/dbg) to physical byte `pa`: lowmem directly, * highmem RAM through a kmap (*km is set; kunmap(*km) when done), MMIO * through its ioremap() mapping, created on demand (4 KB, uncached). * 0 when it cannot be mapped. */ volatile unsigned char *paging_debug_map(unsigned int pa, void **km); /* Physical address of the page directory currently loaded in CR3. On SMP this * is per-CPU; on one CPU it is the running process's directory. */ unsigned int paging_current_cr3(void); /* Physical address of the bootstrap/kernel page directory that new process * directories clone. */ unsigned int paging_kernel_cr3(void); /* Allocate a fresh page directory that clones the kernel identity map and * return its physical address (0 on out-of-memory). */ unsigned int paging_new_directory(void); /* Page-table entry flags (4 KB pages). */ #define PTE_PRESENT 0x001 #define PTE_WRITABLE 0x002 #define PTE_USER 0x004 /* Temporary kernel access to any frame, including highmem (above * MM_MAX_PHYS). Lowmem returns the identity address; highmem takes a * per-CPU slot in the MM_KMAP_VA window with preemption disabled until the * matching kunmap(). Mappings nest (LIFO, 4 per CPU); do not sleep, yield * or take the BKL in between. */ void *kmap(unsigned int phys); void kunmap(void *addr); /* Map one 4 KB page (va -> phys) in page directory `cr3`, allocating a page * table if needed. Returns 1 on success, 0 on out-of-memory. */ int paging_map_page(unsigned int cr3, unsigned int va, unsigned int phys, unsigned int flags); /* Physical address mapped at `va`, or 0 when the page is not present. */ unsigned int paging_page_phys(unsigned int cr3, unsigned int va); /* Clear one 4 KB page (va) in page directory `cr3` (no-op when the page * table is absent). */ void paging_unmap_page(unsigned int cr3, unsigned int va); /* Release the user mappings in [va, va+size): clear the PTEs, free empty page * tables and, when `free_phys` is set, free the mapped physical pages. With * `free_phys` clear the pages belong to someone else (e.g. a mapped window * canvas) and only the mapping/page tables are released. Used by the process * reaper; ranges should cover the user-owned mappings only. */ void paging_release_user_range(unsigned int cr3, unsigned int va, unsigned int size, int free_phys); /* 1 when every page in [va, va+size) is a user-accessible 4 KB mapping in * `cr3` (and writable when `write` is set). Syscall services use this before * touching a pointer supplied by ring 3. */ int paging_user_range_ok(unsigned int cr3, unsigned int va, unsigned int size, int write); #endif