+}
+
+/* Populates the base page directory and page table with the
+ kernel virtual mapping, and then sets up the CPU to use the
+ new page directory. Points base_page_dir to the page
+ directory it creates.
+
+ At the time this function is called, the active page table
+ (set up by start.S) only maps the first 4 MB of RAM, so we
+ should not try to access memory beyond that limit.
+ Fortunately, there is no need to do so. */
+static void
+paging_init (void)
+{
+ uint32_t *pd, *pt;
+ size_t page;
+
+ pd = base_page_dir = ptov (LOADER_PD_BASE);
+ pt = ptov (LOADER_PT_BASE);
+ for (page = 0; page < ram_pages; page++)
+ {
+ uintptr_t paddr = page * PGSIZE;
+ void *vaddr = ptov (paddr);
+ size_t pde_idx = pd_no (vaddr);
+ size_t pte_idx = pt_no (vaddr);
+
+ if (pd[pde_idx] == 0)
+ {
+ pt += PGSIZE / sizeof *pt;
+ memset (pt, 0, PGSIZE);
+ pd[pde_idx] = pde_create (pt);
+ }
+
+ pt[pte_idx] = pte_create_kernel (vaddr, true);
+ }