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 #ifndef GEEKOS_PAGING_H
12 #define GEEKOS_PAGING_H
14 #include <geekos/ktypes.h>
15 #include <geekos/defs.h>
16 #include <geekos/bootinfo.h>
17 #include <geekos/list.h>
22 #define NUM_PAGE_TABLE_ENTRIES 1024
23 #define NUM_PAGE_DIR_ENTRIES 1024
25 #define PAGE_DIRECTORY_INDEX(x) ((((uint_t)x) >> 22) & 0x3ff)
26 #define PAGE_TABLE_INDEX(x) ((((uint_t)x) >> 12) & 0x3ff)
27 #define PAGE_OFFSET(x) ((((uint_t)x) & 0xfff))
29 #define PAGE_ALLIGNED_ADDR(x) (((uint_t) (x)) >> 12)
30 #define PAGE_ADDR(x) (PAGE_ALLIGNED_ADDR(x) << 12)
33 * Bits for flags field of pde_t and pte_t.
35 #define VM_WRITE 1 /* Memory is writable */
36 #define VM_USER 2 /* Memory is accessible to user code */
37 #define VM_NOCACHE 8 /* Memory should not be cached */
38 #define VM_READ 0 /* Memory can be read (ignored for x86) */
39 #define VM_EXEC 0 /* Memory can be executed (ignored for x86) */
43 * Page directory entry datatype.
44 * If marked as present, it specifies the physical address
45 * and permissions of a page table.
55 uint_t pageTableBaseAddr:20;
59 * Page table entry datatype.
60 * If marked as present, it specifies the physical address
61 * and permissions of a page of memory.
68 uint_t pteAttribute:1;
71 uint_t pageBaseAddr:20;
75 * Datatype representing the hardware error code
76 * pushed onto the stack by the processor on a page fault.
77 * The error code is stored in the "errorCode" field
78 * of the Interrupt_State struct.
81 uint_t protectionViolation:1;
83 uint_t userModeFault:1;
84 uint_t reservedBitFault:1;
89 * Bits used in the kernelInfo field of the PTE's:
91 #define KINFO_PAGE_ON_DISK 0x4 /* Page not present; contents in paging file */
93 void Init_VM(struct Boot_Info *bootInfo);
94 void Init_Paging(void);
96 extern void Flush_TLB(void);
97 extern void Set_PDBR(pde_t *pageDir);
98 extern pde_t *Get_PDBR(void);
99 extern void Enable_Paging(pde_t *pageDir);
102 * Return the address that caused a page fault.
104 static __inline__ ulong_t Get_Page_Fault_Address(void)
106 ulong_t faultAddress;
107 __asm__ __volatile__ (
109 : "=r" (faultAddress)
114 void SerialPrintPD(pde_t *pde);
115 void SerialPrintPT(void *starting_address, pte_t *pte);
116 void SerialPrintPDE(void *virtual_address, pde_t *pde);
117 void SerialPrintPTE(void *virtual_address,pte_t *pte);
118 void SerialDumpPageTables(pde_t *pde);
120 pte_t *LookupPage(void *vaddr);
122 pte_t *MapPage(void *vaddr, pte_t *pte, int alloc_pde);
123 pte_t *UnMapPage(void *vaddr);
125 int Find_Space_On_Paging_File(void);
126 void Free_Space_On_Paging_File(int pagefileIndex);
127 void Write_To_Paging_File(void *paddr, ulong_t vaddr, int pagefileIndex);
128 void Read_From_Paging_File(void *paddr, ulong_t vaddr, int pagefileIndex);