| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | #include <bitmap.h> |
|---|
| 26 | #include <pg_alloc.h> |
|---|
| 27 | #include <i386/i386.h> |
|---|
| 28 | #include <i386/virtual.h> |
|---|
| 29 | #include <vmm.h> |
|---|
| 30 | #include <string.h> |
|---|
| 31 | #include <console.h> |
|---|
| 32 | #include <sections.h> |
|---|
| 33 | #include <module.h> |
|---|
| 34 | #include <panic.h> |
|---|
| 35 | |
|---|
| 36 | static BYTE* allocatedBitmap; |
|---|
| 37 | DWORD maxPfn; |
|---|
| 38 | WaitQueue* waitQueues; |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | struct PageStack |
|---|
| 42 | { |
|---|
| 43 | DWORD numPages; |
|---|
| 44 | DWORD maxPages; |
|---|
| 45 | struct ListHead list; |
|---|
| 46 | WaitQueue* waitQueues; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | static struct PageStack normal; |
|---|
| 50 | static struct PageStack isa; |
|---|
| 51 | |
|---|
| 52 | #define PSTACK_START_ADDRESS 0x100000 |
|---|
| 53 | struct PhysPage* pageArrayPtr=(struct PhysPage*)(PSTACK_START_ADDRESS); |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | int PageEarlyInit(DWORD endPfn) |
|---|
| 57 | { |
|---|
| 58 | DWORD endAddress,numBytes; |
|---|
| 59 | |
|---|
| 60 | maxPfn=endPfn; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | endAddress=(maxPfn << PAGE_SHIFT); |
|---|
| 64 | numBytes=((maxPfn+7)/8); |
|---|
| 65 | endAddress-=numBytes; |
|---|
| 66 | allocatedBitmap=(BYTE*)endAddress; |
|---|
| 67 | ZeroMemory(allocatedBitmap,numBytes); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | PageReserveArea(0, PAGE_SIZE); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | PageReserveArea((DWORD)code,(DWORD)end-(DWORD)code); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | PageReserveArea((DWORD)PSTACK_START_ADDRESS, maxPfn*sizeof(struct PhysPage)); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | PageReserveArea((DWORD)PAGE_ALIGN_UP((PSTACK_START_ADDRESS+(maxPfn*sizeof(struct PhysPage)))),((maxPfn+1023) >> 10)*sizeof(WaitQueue)); |
|---|
| 80 | |
|---|
| 81 | return 0; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int PageInit() |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | DWORD i; |
|---|
| 92 | struct PhysPage* pageArray=pageArrayPtr; |
|---|
| 93 | |
|---|
| 94 | isa.maxPages=(maxPfn > 4096 ? 4096 : maxPfn); |
|---|
| 95 | isa.numPages=0; |
|---|
| 96 | INIT_LIST_HEAD(&isa.list); |
|---|
| 97 | for (i=0; i<isa.maxPages; i++) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | pageArray[i].physAddr=i << 12; |
|---|
| 101 | if (!BmapGetBit(allocatedBitmap,i)) |
|---|
| 102 | { |
|---|
| 103 | ListAdd(&pageArray[i].list,&isa.list); |
|---|
| 104 | ++isa.numPages; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | normal.numPages=0; |
|---|
| 109 | INIT_LIST_HEAD(&normal.list); |
|---|
| 110 | |
|---|
| 111 | if (isa.maxPages < maxPfn) |
|---|
| 112 | { |
|---|
| 113 | normal.maxPages=maxPfn-4096; |
|---|
| 114 | |
|---|
| 115 | for (i=4096; i<maxPfn; i++) |
|---|
| 116 | { |
|---|
| 117 | pageArray[i].physAddr=i << 12; |
|---|
| 118 | |
|---|
| 119 | ListAdd(&pageArray[i].list,&normal.list); |
|---|
| 120 | ++normal.numPages; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | KePrint("Number of ISA DMA pages = %u, number available = %u\nNumber of normal memory pages = %u, number available = %u\n",isa.maxPages,isa.numPages,normal.maxPages,normal.numPages); |
|---|
| 125 | allocatedBitmap=NULL; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | waitQueues=(WaitQueue*)PAGE_ALIGN_UP((PSTACK_START_ADDRESS+(maxPfn*sizeof(struct PhysPage)))); |
|---|
| 129 | |
|---|
| 130 | for (i=0; i<((maxPfn+1023) >> 10); i++) |
|---|
| 131 | INIT_WAITQUEUE_HEAD(&waitQueues[i]); |
|---|
| 132 | |
|---|
| 133 | return 0; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | struct PhysPage* PageListGetHead(struct PageStack* stack) |
|---|
| 137 | { |
|---|
| 138 | struct PhysPage* page; |
|---|
| 139 | DWORD flags; |
|---|
| 140 | |
|---|
| 141 | IrqSaveFlags(flags); |
|---|
| 142 | |
|---|
| 143 | page=ListEntry(stack->list.next,struct PhysPage,list); |
|---|
| 144 | --stack->numPages; |
|---|
| 145 | ListRemove(stack->list.next); |
|---|
| 146 | page->refs=1; |
|---|
| 147 | |
|---|
| 148 | IrqRestoreFlags(flags); |
|---|
| 149 | |
|---|
| 150 | return page; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | struct PhysPage* PageGetStruct(DWORD address) |
|---|
| 154 | { |
|---|
| 155 | return &pageArrayPtr[address >> 12]; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | SYMBOL_EXPORT(PageGetStruct); |
|---|
| 159 | |
|---|
| 160 | struct PhysPage* PageAlloc() |
|---|
| 161 | { |
|---|
| 162 | struct PhysPage* page; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | if (normal.numPages) |
|---|
| 166 | page=PageListGetHead(&normal); |
|---|
| 167 | else |
|---|
| 168 | page=PageAllocLow(); |
|---|
| 169 | |
|---|
| 170 | return page; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | SYMBOL_EXPORT(PageAlloc); |
|---|
| 174 | |
|---|
| 175 | struct PhysPage* PageAllocLow() |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | if (!isa.numPages) |
|---|
| 179 | KernelPanic("System out of memory"); |
|---|
| 180 | |
|---|
| 181 | return PageListGetHead(&isa); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | SYMBOL_EXPORT(PageAllocLow); |
|---|
| 185 | |
|---|
| 186 | void PageListPutPage(struct PageStack* stack,struct PhysPage* page) |
|---|
| 187 | { |
|---|
| 188 | DWORD flags; |
|---|
| 189 | IrqSaveFlags(flags); |
|---|
| 190 | |
|---|
| 191 | page->refs=0; |
|---|
| 192 | ++stack->numPages; |
|---|
| 193 | ListAdd(&page->list,&stack->list); |
|---|
| 194 | |
|---|
| 195 | IrqRestoreFlags(flags); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | void PageFree(struct PhysPage* page) |
|---|
| 199 | { |
|---|
| 200 | DWORD pagePfn=page->physAddr >> 12; |
|---|
| 201 | |
|---|
| 202 | if (pagePfn > 4096) |
|---|
| 203 | { |
|---|
| 204 | if (pagePfn >= (normal.maxPages+4096)) |
|---|
| 205 | { |
|---|
| 206 | KePrint("PageFree : illegal free\n"); |
|---|
| 207 | return; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | PageListPutPage(&normal,page); |
|---|
| 211 | }else{ |
|---|
| 212 | if (pagePfn >= isa.maxPages) |
|---|
| 213 | { |
|---|
| 214 | KePrint("PageFree : isa illegal free\n"); |
|---|
| 215 | return; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | PageListPutPage(&isa,page); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | SYMBOL_EXPORT(PageFree); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | void PageReserveArea(DWORD start,DWORD size) |
|---|
| 227 | { |
|---|
| 228 | start=PAGE_ALIGN(start); |
|---|
| 229 | size=PAGE_ALIGN_UP(size); |
|---|
| 230 | |
|---|
| 231 | if (!allocatedBitmap) |
|---|
| 232 | { |
|---|
| 233 | KePrint("PageReserveArea: can't reserve memory!\n"); |
|---|
| 234 | MachineHalt(); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | while (size) |
|---|
| 239 | { |
|---|
| 240 | BmapSetBit(allocatedBitmap,(start >> 12),true); |
|---|
| 241 | start+=PAGE_SIZE; |
|---|
| 242 | size-=PAGE_SIZE; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | WaitQueue* PageGetWaitQueue(struct PhysPage* page) |
|---|
| 248 | { |
|---|
| 249 | return &waitQueues[page->physAddr >> 22]; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | SYMBOL_EXPORT(PageGetWaitQueue); |
|---|