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>
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 /* user faults just kill the process */
170 if (!faultCode.userModeFault) KASSERT(0);
172 /* For now, just kill the thread/process. */
176 /* ----------------------------------------------------------------------
178 * ---------------------------------------------------------------------- */
183 * Initialize virtual memory by building page tables
184 * for the kernel and physical memory.
186 void Init_VM(struct Boot_Info *bootInfo)
195 PrintBoth("Intitialing Virtual Memory\n");
198 SerialPrintLevel(100,"Paging is currently ON\n");
202 SerialPrintLevel(100,"Paging is currently OFF - initializing the pages for a 1-1 map\n");
204 numpages=bootInfo->memSizeKB / (PAGE_SIZE/1024);
205 numpagetables = numpages / NUM_PAGE_TABLE_ENTRIES + ((numpages % NUM_PAGE_TABLE_ENTRIES) != 0 );
207 SerialPrintLevel(100,"We need %d pages, and thus %d page tables, and one page directory\n",numpages, numpagetables);
209 pd = (pde_t*)Alloc_Page();
212 SerialPrintLevel(100,"We are giving up since we can't allocate a page directory!\n");
215 SerialPrintLevel(100,"Our PDE is at physical address %p\n",pd);
218 for (i=0;i<NUM_PAGE_DIR_ENTRIES;i++) {
219 if (i>=numpagetables) {
227 pd[i].pageTableBaseAddr=0;
229 pt = (pte_t*)Alloc_Page();
231 SerialPrintLevel(100,"We are giving up since we can't allocate page table %d\n",i);
233 //SerialPrintLevel(100,"Page Table %d is at physical address %p\n",i,pt);
236 pd[i].flags= VM_READ | VM_WRITE | VM_EXEC | VM_USER;
242 pd[i].pageTableBaseAddr = PAGE_ALLIGNED_ADDR(pt);
244 for (j=0;j<NUM_PAGE_TABLE_ENTRIES;j++) {
245 if (i*NUM_PAGE_TABLE_ENTRIES + j >= numpages) {
250 pt[j].pteAttribute=0;
253 pt[j].pageBaseAddr=0;
256 pt[j].flags=VM_READ | VM_WRITE | VM_EXEC | VM_USER;
259 pt[j].pteAttribute=0;
262 pt[j].pageBaseAddr=(i*NUM_PAGE_TABLE_ENTRIES + j);
267 SerialPrintLevel(100,"Done creating 1<->1 initial page tables\n");
268 SerialPrintLevel(100,"Now installing page fault handler\n");
269 // SerialDumpPageTables(pd);
270 Install_Interrupt_Handler(14,Page_Fault_Handler);
271 SerialPrintLevel(100,"Now turning on the paging bit!\n");
273 SerialPrintLevel(100,"We are still alive after paging turned on!\n");
274 SerialPrintLevel(100,"checkPaging returns %d\n",checkPaging());
279 pte_t *LookupPage(void *vaddr)
281 uint_t pde_offset = ((uint_t)vaddr) >> 22;
282 uint_t pte_offset = (((uint_t)vaddr) >> 12) & 0x3ff;
284 pde_t *pde = Get_PDBR();
290 if (!(pde->present)) {
294 pte = (pte_t *)((pde->pageTableBaseAddr)<<12);
302 pte_t *CreateAndAddPageTable(void *vaddr, uint_t flags)
306 KASSERT(!(PAGE_OFFSET(vaddr)));
308 pte_t *pt = Alloc_Page();
312 for (i=0;i<NUM_PAGE_TABLE_ENTRIES;i++) {
316 pde_t *pde = Get_PDBR();
318 pde=&(pde[PAGE_DIRECTORY_INDEX(vaddr)]);
320 KASSERT(!(pde->present));
329 pde->pageTableBaseAddr = PAGE_ALLIGNED_ADDR(pt);
334 pte_t *MapPage(void *vaddr, pte_t *pte, int alloc_pde)
336 pte_t *oldpte = LookupPage(vaddr);
340 CreateAndAddPageTable(vaddr,pte->flags);
341 oldpte = LookupPage(vaddr);
353 pte_t *UnMapPage(void *vaddr)
355 pte_t *oldpte = LookupPage(vaddr);
368 * Initialize paging file data structures.
369 * All filesystems should be mounted before this function
370 * is called, to ensure that the paging file is available.
372 void Init_Paging(void)
374 PrintBoth("Initializing Paging\n");
378 * Find a free bit of disk on the paging file for this page.
379 * Interrupts must be disabled.
380 * @return index of free page sized chunk of disk space in
381 * the paging file, or -1 if the paging file is full
383 int Find_Space_On_Paging_File(void)
385 KASSERT(!Interrupts_Enabled());
386 TODO("Find free page in paging file");
390 * Free a page-sized chunk of disk space in the paging file.
391 * Interrupts must be disabled.
392 * @param pagefileIndex index of the chunk of disk space
394 void Free_Space_On_Paging_File(int pagefileIndex)
396 KASSERT(!Interrupts_Enabled());
397 TODO("Free page in paging file");
401 * Write the contents of given page to the indicated block
402 * of space in the paging file.
403 * @param paddr a pointer to the physical memory of the page
404 * @param vaddr virtual address where page is mapped in user memory
405 * @param pagefileIndex the index of the page sized chunk of space
408 void Write_To_Paging_File(void *paddr, ulong_t vaddr, int pagefileIndex)
410 struct Page *page = Get_Page((ulong_t) paddr);
411 KASSERT(!(page->flags & PAGE_PAGEABLE)); /* Page must be locked! */
412 TODO("Write page data to paging file");
416 * Read the contents of the indicated block
417 * of space in the paging file into the given page.
418 * @param paddr a pointer to the physical memory of the page
419 * @param vaddr virtual address where page will be re-mapped in
421 * @param pagefileIndex the index of the page sized chunk of space
424 void Read_From_Paging_File(void *paddr, ulong_t vaddr, int pagefileIndex)
426 struct Page *page = Get_Page((ulong_t) paddr);
427 KASSERT(!(page->flags & PAGE_PAGEABLE)); /* Page must be locked! */
428 TODO("Read page data from paging file");