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
00019 #define VM_ADDRESS_KERNEL_HEAP 0x00000000
00020
00021 #define VM_ADDRESS_KERNEL_HEAP_LIMIT 0x01000000
00022
00023 #define VM_ADDRESS_USER_SPACE 0x01000000
00024
00025 #define VM_ADDRESS_USER_SPACE_LIMIT 0xFE000000
00026
00027 #define VM_ADDRESS_TEMPORARY_MAPPING_1 0xFF000000
00028
00029 #define VM_ADDRESS_TEMPORARY_MAPPING_2 0xFF400000
00030
00031 #define VM_ADDRESS_TEMPORARY_MAPPING_1_PT0 0xFF800000
00032
00033 #define VM_ADDRESS_TEMPORARY_MAPPING_2_PT0 0xFF801000
00034
00035 #define VM_ADDRESS_KERNEL_STACK 0xFF802000
00036
00037 #define VM_ADDRESS_KERNEL_STACK_LIMIT 0x003FE000
00038
00039 #define VM_ADDRESS_PAGE_TABLE 0xFFC00000
00040
00041
00042 #define VM_PROTECTION_READ (0 << 1)
00043
00044 #define VM_PROTECTION_WRITE (1 << 1)
00045
00046 #define VM_PROTECTION_SUPERVISOR (0 << 2)
00047
00048 #define VM_PROTECTION_USER (1 << 2)
00049
00050 #define VM_PROTECTION_MASK (3 << 1)
00051
00052
00053 #define VM_PT0 ((PageTableEntry *) (VM_ADDRESS_PAGE_TABLE + ((VM_ADDRESS_PAGE_TABLE >> VM_PT0_SHIFT) << VM_PT1_SHIFT)))
00054
00055 #define VM_PT0_MASK 0xFFC00000
00056
00057 #define VM_PT0_SHIFT 22
00058
00059 #define VM_PT0_SIZE 1024
00060
00061
00062 #define VM_PT1(index) ((PageTableEntry *) (VM_ADDRESS_PAGE_TABLE + (index) * sizeof (PageTableEntry) * VM_PT1_SIZE))
00063
00064 #define VM_PT1_MASK 0x003FF000
00065
00066 #define VM_PT1_SHIFT 12
00067
00068 #define VM_PT1_SIZE 1024
00069
00070
00071 #define VM_PTE(address) (VM_PT1((address) >> VM_PT0_SHIFT) [((address) & VM_PT1_MASK) >> VM_PT1_SHIFT])
00072
00073
00074 #define VM_PTE_PRESENT (1 << 0)
00075
00076 #define VM_PTE_GLOBAL (1 << 8)
00077
00078
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
00090 typedef struct MemoryRegion
00091 {
00092
00093 ListNode memoryRegionList;
00094
00095 unsigned int protection;
00096
00097 int growsDownward;
00098
00099 MemoryAddress addressStart;
00100
00101 MemoryAddress addressEnd;
00102
00103 unsigned int frameCount;
00104
00105 List frames;
00106
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
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
00243 #define VM_VALIDATE(ptr,type,elems) (vm_validate ((MemoryAddress) (ptr), (MemoryAddress) (ptr) + sizeof (type) * (elems) - 1, VM_PROTECTION_READ))
00244
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
00247 #define VM_VALIDATE_STRING(ptr,type) (vm_validate_string ((MemoryAddress) (ptr), sizeof (type)))
00248
00249 #endif
00250