2 * Paging (virtual memory) support
3 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
4 * Copyright (c) 2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
7 * This is free software. You are permitted to use,
8 * redistribute, and modify it as specified in the file "COPYING".
11 #include <geekos/string.h>
12 #include <geekos/int.h>
13 #include <geekos/idt.h>
14 #include <geekos/kthread.h>
15 #include <geekos/kassert.h>
16 #include <geekos/screen.h>
17 #include <geekos/mem.h>
18 #include <geekos/malloc.h>
19 #include <geekos/gdt.h>
20 #include <geekos/segment.h>
21 //#include <geekos/user.h>
22 //#include <geekos/vfs.h>
23 #include <geekos/crc32.h>
24 #include <geekos/paging.h>
25 #include <geekos/serial.h>
26 #include <geekos/debug.h>
28 /* ----------------------------------------------------------------------
30 * ---------------------------------------------------------------------- */
32 /* ----------------------------------------------------------------------
33 * Private functions/data
34 * ---------------------------------------------------------------------- */
36 #define SECTORS_PER_PAGE (PAGE_SIZE / SECTOR_SIZE)
39 * flag to indicate if debugging paging code
42 #define Debug(args...) if (debugFaults) Print(args)
46 void SerialPrintPD(pde_t *pde)
50 SerialPrint("Page Directory at %p:\n",pde);
51 for (i=0;i<NUM_PAGE_DIR_ENTRIES && pde[i].present;i++) {
52 SerialPrintPDE((void*)(PAGE_SIZE*NUM_PAGE_TABLE_ENTRIES*i),&(pde[i]));
56 void SerialPrintPT(void *starting_address, pte_t *pte)
60 SerialPrint("Page Table at %p:\n",pte);
61 for (i=0;i<NUM_PAGE_TABLE_ENTRIES && pte[i].present;i++) {
62 SerialPrintPTE(starting_address + PAGE_SIZE*i,&(pte[i]));
67 void SerialPrintPDE(void *virtual_address, pde_t *pde)
69 SerialPrint("PDE %p -> %p : present=%x, flags=%x, accessed=%x, reserved=%x, largePages=%x, globalPage=%x, kernelInfo=%x\n",
71 (void*) (pde->pageTableBaseAddr << PAGE_POWER),
81 void SerialPrintPTE(void *virtual_address, pte_t *pte)
83 SerialPrint("PTE %p -> %p : present=%x, flags=%x, accessed=%x, dirty=%x, pteAttribute=%x, globalPage=%x, kernelInfo=%x\n",
85 (void*)(pte->pageBaseAddr << PAGE_POWER),
96 void SerialDumpPageTables(pde_t *pde)
100 SerialPrint("Dumping the pages starting with the pde page at %p\n",pde);
102 for (i=0;i<NUM_PAGE_DIR_ENTRIES && pde[i].present;i++) {
103 SerialPrintPDE((void*)(PAGE_SIZE*NUM_PAGE_TABLE_ENTRIES*i),&(pde[i]));
104 SerialPrintPT((void*)(PAGE_SIZE*NUM_PAGE_TABLE_ENTRIES*i),(void*)(pde[i].pageTableBaseAddr<<PAGE_POWER));
115 __asm__ __volatile__( "movl %%cr0, %0" : "=a" (reg));
116 Print("Paging on ? : %d\n", (reg & (1<<31)) != 0);
117 return (reg & (1<<31)) != 0;
122 * Print diagnostic information for a page fault.
124 static void Print_Fault_Info(uint_t address, faultcode_t faultCode)
126 extern uint_t g_freePageCount;
130 SerialPrintLevel(100,"Pid %d, Page Fault received, at address %x (%d pages free)\n",
131 g_currentThread->pid, address, g_freePageCount);
132 if (faultCode.protectionViolation)
133 SerialPrintLevel(100," Protection Violation, ");
135 SerialPrintLevel(100," Non-present page, ");
136 if (faultCode.writeFault)
137 SerialPrintLevel(100,"Write Fault, ");
139 SerialPrintLevel(100,"Read Fault, ");
140 if (faultCode.userModeFault)
141 SerialPrintLevel(100,"in User Mode\n");
143 SerialPrintLevel(100,"in Supervisor Mode\n");
147 * Handler for page faults.
148 * You should call the Install_Interrupt_Handler() function to
149 * register this function as the handler for interrupt 14.
151 /*static*/ void Page_Fault_Handler(struct Interrupt_State* state)
154 faultcode_t faultCode;
156 KASSERT(!Interrupts_Enabled());
158 /* Get the address that caused the page fault */
159 address = Get_Page_Fault_Address();
160 Debug("Page fault @%lx\n", address);
162 /* Get the fault code */
163 faultCode = *((faultcode_t *) &(state->errorCode));
165 /* rest of your handling code here */
166 SerialPrintLevel(100,"Unexpected Page Fault received\n");
167 Print_Fault_Info(address, faultCode);
168 Dump_Interrupt_State(state);
169 //SerialPrint_VMCS_ALL();
170 /* user faults just kill the process */
171 if (!faultCode.userModeFault) KASSERT(0);
173 /* For now, just kill the thread/process. */
177 /* ----------------------------------------------------------------------
179 * ---------------------------------------------------------------------- */
184 * Initialize virtual memory by building page tables
185 * for the kernel and physical memory.
187 void Init_VM(struct Boot_Info *bootInfo)
196 PrintBoth("Intitialing Virtual Memory\n");
199 SerialPrintLevel(100,"Paging is currently ON\n");
203 SerialPrintLevel(100,"Paging is currently OFF - initializing the pages for a 1-1 map\n");
205 numpages=bootInfo->memSizeKB / (PAGE_SIZE/1024);
206 numpagetables = numpages / NUM_PAGE_TABLE_ENTRIES + ((numpages % NUM_PAGE_TABLE_ENTRIES) != 0 );
208 SerialPrintLevel(100,"We need %d pages, and thus %d page tables, and one page directory\n",numpages, numpagetables);
210 pd = (pde_t*)Alloc_Page();
213 SerialPrintLevel(100,"We are giving up since we can't allocate a page directory!\n");
216 SerialPrintLevel(100,"Our PDE is at physical address %p\n",pd);
219 for (i=0;i<NUM_PAGE_DIR_ENTRIES;i++) {
220 if (i>=numpagetables) {
228 pd[i].pageTableBaseAddr=0;
230 pt = (pte_t*)Alloc_Page();
232 SerialPrintLevel(100,"We are giving up since we can't allocate page table %d\n",i);
234 //SerialPrintLevel(100,"Page Table %d is at physical address %p\n",i,pt);
237 pd[i].flags= VM_READ | VM_WRITE | VM_EXEC | VM_USER;
243 pd[i].pageTableBaseAddr = PAGE_ALLIGNED_ADDR(pt);
245 for (j=0;j<NUM_PAGE_TABLE_ENTRIES;j++) {
246 if (i*NUM_PAGE_TABLE_ENTRIES + j >= numpages) {
251 pt[j].pteAttribute=0;
254 pt[j].pageBaseAddr=0;
257 pt[j].flags=VM_READ | VM_WRITE | VM_EXEC | VM_USER;
260 pt[j].pteAttribute=0;
263 pt[j].pageBaseAddr=(i*NUM_PAGE_TABLE_ENTRIES + j);
268 SerialPrintLevel(100,"Done creating 1<->1 initial page tables\n");
269 SerialPrintLevel(100,"Now installing page fault handler\n");
270 // SerialDumpPageTables(pd);
271 Install_Interrupt_Handler(14,Page_Fault_Handler);
272 SerialPrintLevel(100,"Now turning on the paging bit!\n");
274 SerialPrintLevel(100,"We are still alive after paging turned on!\n");
275 SerialPrintLevel(100,"checkPaging returns %d\n",checkPaging());
280 pte_t *LookupPage(void *vaddr)
282 uint_t pde_offset = ((uint_t)vaddr) >> 22;
283 uint_t pte_offset = (((uint_t)vaddr) >> 12) & 0x3ff;
285 pde_t *pde = Get_PDBR();
291 if (!(pde->present)) {
295 pte = (pte_t *)((pde->pageTableBaseAddr)<<12);
303 pte_t *CreateAndAddPageTable(void *vaddr, uint_t flags)
307 KASSERT(!(PAGE_OFFSET(vaddr)));
309 pte_t *pt = Alloc_Page();
313 for (i=0;i<NUM_PAGE_TABLE_ENTRIES;i++) {
317 pde_t *pde = Get_PDBR();
319 pde=&(pde[PAGE_DIRECTORY_INDEX(vaddr)]);
321 KASSERT(!(pde->present));
330 pde->pageTableBaseAddr = PAGE_ALLIGNED_ADDR(pt);
335 pte_t *MapPage(void *vaddr, pte_t *pte, int alloc_pde)
337 pte_t *oldpte = LookupPage(vaddr);
341 CreateAndAddPageTable(vaddr,pte->flags);
342 oldpte = LookupPage(vaddr);
354 pte_t *UnMapPage(void *vaddr)
356 pte_t *oldpte = LookupPage(vaddr);
369 * Initialize paging file data structures.
370 * All filesystems should be mounted before this function
371 * is called, to ensure that the paging file is available.
373 void Init_Paging(void)
375 PrintBoth("Initializing Paging\n");
379 * Find a free bit of disk on the paging file for this page.
380 * Interrupts must be disabled.
381 * @return index of free page sized chunk of disk space in
382 * the paging file, or -1 if the paging file is full
384 int Find_Space_On_Paging_File(void)
386 KASSERT(!Interrupts_Enabled());
387 TODO("Find free page in paging file");
391 * Free a page-sized chunk of disk space in the paging file.
392 * Interrupts must be disabled.
393 * @param pagefileIndex index of the chunk of disk space
395 void Free_Space_On_Paging_File(int pagefileIndex)
397 KASSERT(!Interrupts_Enabled());
398 TODO("Free page in paging file");
402 * Write the contents of given page to the indicated block
403 * of space in the paging file.
404 * @param paddr a pointer to the physical memory of the page
405 * @param vaddr virtual address where page is mapped in user memory
406 * @param pagefileIndex the index of the page sized chunk of space
409 void Write_To_Paging_File(void *paddr, ulong_t vaddr, int pagefileIndex)
411 struct Page *page = Get_Page((ulong_t) paddr);
412 KASSERT(!(page->flags & PAGE_PAGEABLE)); /* Page must be locked! */
413 TODO("Write page data to paging file");
417 * Read the contents of the indicated block
418 * of space in the paging file into the given page.
419 * @param paddr a pointer to the physical memory of the page
420 * @param vaddr virtual address where page will be re-mapped in
422 * @param pagefileIndex the index of the page sized chunk of space
425 void Read_From_Paging_File(void *paddr, ulong_t vaddr, int pagefileIndex)
427 struct Page *page = Get_Page((ulong_t) paddr);
428 KASSERT(!(page->flags & PAGE_PAGEABLE)); /* Page must be locked! */
429 TODO("Read page data from paging file");