Main Page | Data Structures | File List | Globals | Related Pages

vm.h

Go to the documentation of this file.
00001 
00011 #ifndef __KERN_VIRTUAL_MEMORY_H__
00012 #define __KERN_VIRTUAL_MEMORY_H__
00013 
00014 #include <list.h>
00015 #include <task.h>
00016 #include <register.h>
00017 
00018 /* the starting address of the kernel heap */
00019 #define VM_ADDRESS_KERNEL_HEAP                  0x00000000
00020 /* the size of the kernel heap */
00021 #define VM_ADDRESS_KERNEL_HEAP_LIMIT            0x01000000
00022 /* the starting address of the user space memory */
00023 #define VM_ADDRESS_USER_SPACE                   0x01000000
00024 /* the size of the user space memory */
00025 #define VM_ADDRESS_USER_SPACE_LIMIT             0xFE000000
00026 /* the address of the first temporary mapping space, which is one superpage in size */
00027 #define VM_ADDRESS_TEMPORARY_MAPPING_1          0xFF000000
00028 /* the address of the second temporary mapping space, which is one superpage in size */
00029 #define VM_ADDRESS_TEMPORARY_MAPPING_2          0xFF400000
00030 /* the address of the first temporary page directory mapping */
00031 #define VM_ADDRESS_TEMPORARY_MAPPING_1_PT0      0xFF800000
00032 /* the address of the second temporary page directory mapping */
00033 #define VM_ADDRESS_TEMPORARY_MAPPING_2_PT0      0xFF801000
00034 /* the starting address of the reserved kernel stack space */
00035 #define VM_ADDRESS_KERNEL_STACK                 0xFF802000
00036 /* the size of the reserved kernel stack space */
00037 #define VM_ADDRESS_KERNEL_STACK_LIMIT           0x003FE000
00038 /* the address of the self-mapped page table structures, which is one superpage in size */
00039 #define VM_ADDRESS_PAGE_TABLE                   0xFFC00000
00040 
00041 /* memory protection flag for readable memory regions */
00042 #define VM_PROTECTION_READ              (0 << 1)
00043 /* memory protection flag for writeable memory regions */
00044 #define VM_PROTECTION_WRITE             (1 << 1)
00045 /* memory protection flag for supervisor memory regions */
00046 #define VM_PROTECTION_SUPERVISOR        (0 << 2)
00047 /* memory protection flag for user memory regions */
00048 #define VM_PROTECTION_USER              (1 << 2)
00049 /* mask of memory protection flags */
00050 #define VM_PROTECTION_MASK              (3 << 1)
00051 
00052 /* the address of the page directory */
00053 #define VM_PT0                          ((PageTableEntry *) (VM_ADDRESS_PAGE_TABLE + ((VM_ADDRESS_PAGE_TABLE >> VM_PT0_SHIFT) << VM_PT1_SHIFT)))
00054 /* the address mask for page directory entries */
00055 #define VM_PT0_MASK                     0xFFC00000
00056 /* the offset of the address mask for page directory entries */
00057 #define VM_PT0_SHIFT                    22
00058 /* the number of entries in a page directory */
00059 #define VM_PT0_SIZE                     1024
00060 
00061 /* the address of the page table at the specified index in the page directory */
00062 #define VM_PT1(index)                   ((PageTableEntry *) (VM_ADDRESS_PAGE_TABLE + (index) * sizeof (PageTableEntry) * VM_PT1_SIZE))
00063 /* the address mask for page table entries */
00064 #define VM_PT1_MASK                     0x003FF000
00065 /* the offset of the address mask for page table entries */
00066 #define VM_PT1_SHIFT                    12
00067 /* the number of entries in a page table */
00068 #define VM_PT1_SIZE                     1024
00069 
00070 /* the page table entry associated with a given address */
00071 #define VM_PTE(address)                 (VM_PT1((address) >> VM_PT0_SHIFT) [((address) & VM_PT1_MASK) >> VM_PT1_SHIFT])
00072 
00073 /* the page table entry flag for marking a PTE as present */
00074 #define VM_PTE_PRESENT                  (1 << 0)
00075 /* the page table entry flag for marking a PTE as global */
00076 #define VM_PTE_GLOBAL                   (1 << 8)
00077 
00078 /* the acceptable distance from a page fault to the current top of the stack that is tolerated for growing the stack */
00079 #define VM_STACK_TOLERANCE              (1 << VM_PT0_SHIFT)
00080         
00081 typedef unsigned int PageTableEntry;
00082 
00083 typedef unsigned long int MemoryAddress;
00084 
00085 struct MemoryRegion;
00086 
00087 typedef int (* MemoryFaultHandler) (struct MemoryRegion *, MemoryAddress, Registers *);
00088 
00089 /* a memory region in a task's address space */ 
00090 typedef struct MemoryRegion
00091 {
00092         /* the position in a task's memory region list */
00093         ListNode memoryRegionList;      
00094         /* the memory protection of the region */
00095         unsigned int protection;
00096         /* if the memory region grows downward, the amount of distance from the page fault to the base of the region it will tolerate */
00097         int growsDownward;
00098         /* the address of the starting page in the task's address space */
00099         MemoryAddress addressStart;
00100         /* the address of the ending page in the task's address space */
00101         MemoryAddress addressEnd;
00102         /* the number of frames allocated to the region */
00103         unsigned int frameCount;
00104         /* the list of frames allocated to the region */
00105         List frames;
00106         /* a handler to be called when a page fault occurs within the region or its tolerance */
00107         MemoryFaultHandler faultHandler;
00108 } MemoryRegion;
00109 
00119 extern int vm_initialize (Task * task);
00120 
00132 extern int vm_validate (MemoryAddress, MemoryAddress, unsigned int protection);
00144 extern int vm_validate_string (MemoryAddress start, unsigned int size);
00145 
00154 extern void vm_context_switch (Task * task);
00155 
00166 extern int vm_map (Task * task, MemoryRegion * region);
00177 extern int vm_unmap (Task * task, MemoryRegion * region);
00178 
00179 
00189 extern int vm_fork (Task * task);
00202 extern int vm_copy (Task * task, MemoryAddress dst, char * src, unsigned int size);
00203 
00204 /* memory region fault handler for creating stacks */
00205 extern int stack_fault_handler (MemoryRegion *, MemoryAddress, Registers *);
00206 
00220 extern MemoryRegion * memory_region_create (Task * task, unsigned int protection, MemoryAddress start, MemoryAddress end, MemoryFaultHandler handler);
00229 extern void memory_region_destroy (MemoryRegion * region);
00240 extern MemoryRegion * memory_region_find (Task * task, MemoryAddress address);
00241 
00242 /* macro that automatically calculates the end address for vm_validate */
00243 #define VM_VALIDATE(ptr,type,elems)     (vm_validate ((MemoryAddress) (ptr), (MemoryAddress) (ptr) + sizeof (type) * (elems) - 1, VM_PROTECTION_READ))
00244 /* macro that automatically calculates the end address for vm_validate, also checks for writeable memory */
00245 #define VM_VALIDATE_WRITE(ptr,type,elems)     (vm_validate ((MemoryAddress) (ptr), (MemoryAddress) (ptr) + sizeof (type) * (elems) - 1, VM_PROTECTION_READ | VM_PROTECTION_WRITE))
00246 /* convenient macro for vm_validate_string that passes in the right type size */
00247 #define VM_VALIDATE_STRING(ptr,type)    (vm_validate_string ((MemoryAddress) (ptr), sizeof (type))) 
00248 
00249 #endif /* __KERN_VIRTUAL_MEMORY_H__ */
00250 

Generated on Fri Apr 9 21:59:16 2004 for 15-410 Project 3 by doxygen 1.3.2