| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <fs/vfs.h> |
|---|
| 20 | #include <vmm.h> |
|---|
| 21 | #include <locks.h> |
|---|
| 22 | #include <llist.h> |
|---|
| 23 | #include <malloc.h> |
|---|
| 24 | #include <module.h> |
|---|
| 25 | #include <typedefs.h> |
|---|
| 26 | #include <i386/i386.h> |
|---|
| 27 | #include <slab.h> |
|---|
| 28 | #include <user_acc.h> |
|---|
| 29 | |
|---|
| 30 | struct Cache* areaCache,*mapCache; |
|---|
| 31 | SYMBOL_EXPORT(areaCache); |
|---|
| 32 | SYMBOL_EXPORT(mapCache); |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | struct VMArea* VmLookupAddress(struct Process* process,DWORD address) |
|---|
| 48 | { |
|---|
| 49 | struct VMArea* curr, *curr2; |
|---|
| 50 | |
|---|
| 51 | ListForEachEntrySafe(curr, curr2, &process->areaList, list) |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | if (curr->start > address) |
|---|
| 56 | break; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | if (address >= curr->start && address < (curr->start+curr->length)) |
|---|
| 60 | return curr; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | return NULL; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | SYMBOL_EXPORT(VmLookupAddress); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | struct VMMapPage* VmLookupPage(struct VNode* vNode, DWORD offset) |
|---|
| 83 | { |
|---|
| 84 | struct VMMapPage* page = NULL, *curr; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | if (!vNode || vNode->refs == 1 || ListEmpty(&vNode->sharedList)) |
|---|
| 88 | return NULL; |
|---|
| 89 | |
|---|
| 90 | PreemptDisable(); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | ListForEachEntry(curr, &vNode->sharedList, list) |
|---|
| 94 | if (curr->offset == offset) |
|---|
| 95 | { |
|---|
| 96 | page = curr; |
|---|
| 97 | break; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | PreemptFastEnable(); |
|---|
| 101 | |
|---|
| 102 | if (page) |
|---|
| 103 | { |
|---|
| 104 | PageGet(page->page); |
|---|
| 105 | VmWaitForPage(page); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | return page; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | SYMBOL_EXPORT(VmLookupPage); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | struct VMMapPage* VmCreateMappedPage(DWORD offset, struct PhysPage* page) |
|---|
| 127 | { |
|---|
| 128 | struct VMMapPage* mappedPage=(struct VMMapPage*)MemCacheAlloc(mapCache); |
|---|
| 129 | |
|---|
| 130 | if (!mappedPage) |
|---|
| 131 | return NULL; |
|---|
| 132 | |
|---|
| 133 | mappedPage->offset=offset; |
|---|
| 134 | mappedPage->page=page; |
|---|
| 135 | |
|---|
| 136 | return mappedPage; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | SYMBOL_EXPORT(VmCreateMappedPage); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | int VmFreeMappedPage(struct VNode* vNode, DWORD physAddr) |
|---|
| 155 | { |
|---|
| 156 | struct VMMapPage* page=NULL, *page2; |
|---|
| 157 | |
|---|
| 158 | if (!vNode) |
|---|
| 159 | return -EFAULT; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | ListForEachEntrySafe(page, page2, &vNode->sharedList, list) |
|---|
| 164 | if (page->page->physAddr == physAddr) |
|---|
| 165 | { |
|---|
| 166 | VmWaitForPage(page); |
|---|
| 167 | ListRemove(&page->list); |
|---|
| 168 | return 0; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | return -ENOENT; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | SYMBOL_EXPORT(VmFreeMappedPage); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | static void _VmWaitOnPage(struct VMMapPage* page) |
|---|
| 193 | { |
|---|
| 194 | WaitQueue* waitQueue; |
|---|
| 195 | |
|---|
| 196 | PageGet(page->page); |
|---|
| 197 | |
|---|
| 198 | waitQueue=PageGetWaitQueue(page->page); |
|---|
| 199 | |
|---|
| 200 | WAIT_ON(waitQueue, !PageLocked(page)); |
|---|
| 201 | |
|---|
| 202 | PagePut(page->page); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void VmWaitForPage(struct VMMapPage* page) |
|---|
| 206 | { |
|---|
| 207 | if (PageLocked(page)) |
|---|
| 208 | _VmWaitOnPage(page); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | SYMBOL_EXPORT(VmWaitForPage); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | void VmLockPage(struct VMMapPage* page) |
|---|
| 226 | { |
|---|
| 227 | while (BitTestAndSet(&page->flags, PAGE_LOCKED)) |
|---|
| 228 | _VmWaitOnPage(page); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | SYMBOL_EXPORT(VmLockPage); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | void VmUnlockPage(struct VMMapPage* page) |
|---|
| 246 | { |
|---|
| 247 | BitClear(&page->flags, PAGE_LOCKED); |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | WakeUpAll(PageGetWaitQueue(page->page)); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | SYMBOL_EXPORT(VmUnlockPage); |
|---|
| 255 | |
|---|
| 256 | int VirtCheckArea(void* beginAddr,DWORD size,int mask) |
|---|
| 257 | { |
|---|
| 258 | DWORD begin=(DWORD)beginAddr; |
|---|
| 259 | struct VMArea* area,*next; |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | if (!size) |
|---|
| 263 | return 0; |
|---|
| 264 | |
|---|
| 265 | area=VmLookupAddress(current,begin); |
|---|
| 266 | |
|---|
| 267 | if (!area) |
|---|
| 268 | return -EFAULT; |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | if (!(area->protection & PAGE_RW) && (mask & VER_WRITE)) |
|---|
| 272 | return -EFAULT; |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | if ((area->start+area->length)-begin >= size) |
|---|
| 276 | return 0; |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | while (1) |
|---|
| 280 | { |
|---|
| 281 | |
|---|
| 282 | if (!(area->protection & PAGE_RW) && (mask & VER_WRITE)) |
|---|
| 283 | return -EFAULT; |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | if (!(area->protection & PAGE_USER)) |
|---|
| 287 | return -EFAULT; |
|---|
| 288 | |
|---|
| 289 | if ((area->start+area->length)-begin >= size) |
|---|
| 290 | break; |
|---|
| 291 | |
|---|
| 292 | next=ListEntry(area->list.next,struct VMArea,list); |
|---|
| 293 | if (area->list.next == ¤t->areaList || next->start != area->start+area->length) |
|---|
| 294 | return -EFAULT; |
|---|
| 295 | |
|---|
| 296 | area=next; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | return 0; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | SYMBOL_EXPORT(VirtCheckArea); |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | int VmHandleNoPage(struct VMArea* area, DWORD address) |
|---|
| 319 | { |
|---|
| 320 | DWORD offset=(address-area->start)+area->offset; |
|---|
| 321 | struct VMMapPage* mappedPage; |
|---|
| 322 | struct PhysPage* newPage; |
|---|
| 323 | int ret; |
|---|
| 324 | |
|---|
| 325 | if (area->areaOps && area->areaOps->handleNoPage) |
|---|
| 326 | if (!area->areaOps->handleNoPage(area, address, offset)) |
|---|
| 327 | return 0; |
|---|
| 328 | |
|---|
| 329 | if (area->protection & PAGE_RW) |
|---|
| 330 | { |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | if (area->flags & MMAP_SHARED) |
|---|
| 334 | { |
|---|
| 335 | KePrint("address = %#X\n", address); |
|---|
| 336 | MachineHalt(); |
|---|
| 337 | }else{ |
|---|
| 338 | PreemptDisable(); |
|---|
| 339 | newPage=PageAlloc(); |
|---|
| 340 | |
|---|
| 341 | if (!newPage) |
|---|
| 342 | { |
|---|
| 343 | PreemptFastEnable(); |
|---|
| 344 | return -ENOMEM; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | VirtMemMapPage(address, newPage->physAddr, area->protection); |
|---|
| 348 | |
|---|
| 349 | PreemptFastEnable(); |
|---|
| 350 | |
|---|
| 351 | if (area->vNode->fileOps->mMap) |
|---|
| 352 | ret=area->vNode->fileOps->mMap(area->vNode, address, offset); |
|---|
| 353 | else |
|---|
| 354 | ret=FileGenericReadPage(address, area->vNode, offset); |
|---|
| 355 | |
|---|
| 356 | if (ret) |
|---|
| 357 | goto fail; |
|---|
| 358 | } |
|---|
| 359 | }else{ |
|---|
| 360 | mappedPage=VmLookupPage(area->vNode, offset); |
|---|
| 361 | |
|---|
| 362 | if (!mappedPage) |
|---|
| 363 | { |
|---|
| 364 | |
|---|
| 365 | newPage=PageAlloc(); |
|---|
| 366 | |
|---|
| 367 | if (!newPage) |
|---|
| 368 | return -ENOMEM; |
|---|
| 369 | |
|---|
| 370 | VirtMemMapPage(address, newPage->physAddr, |
|---|
| 371 | area->protection | PAGE_ACCESSED | PAGE_SHARED); |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | mappedPage=VmCreateMappedPage(offset, newPage); |
|---|
| 375 | |
|---|
| 376 | if (!mappedPage) |
|---|
| 377 | return -EFAULT; |
|---|
| 378 | |
|---|
| 379 | VmLockPage(mappedPage); |
|---|
| 380 | |
|---|
| 381 | ListAdd(&mappedPage->list, &area->vNode->sharedList); |
|---|
| 382 | |
|---|
| 383 | if (area->vNode->fileOps->mMap) |
|---|
| 384 | ret=area->vNode->fileOps->mMap(area->vNode, address, offset); |
|---|
| 385 | else |
|---|
| 386 | ret=FileGenericReadPage(address,area->vNode,offset); |
|---|
| 387 | |
|---|
| 388 | VmUnlockPage(mappedPage); |
|---|
| 389 | |
|---|
| 390 | if (ret) |
|---|
| 391 | return ret; |
|---|
| 392 | }else{ |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | VirtMemMapPage(address, mappedPage->page->physAddr, |
|---|
| 396 | area->protection | PAGE_ACCESSED | PAGE_SHARED); |
|---|
| 397 | } |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | return 0; |
|---|
| 401 | |
|---|
| 402 | fail: |
|---|
| 403 | |
|---|
| 404 | KePrint("VmHandleNoPage failed: killing process\n"); |
|---|
| 405 | |
|---|
| 406 | return -EIO; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | int VmProtFault(struct VMArea* area,DWORD address) |
|---|
| 426 | { |
|---|
| 427 | KePrint("Protection fault at %#X, area = %#X, area->start = %#X, area->end = %#X (protection = %d)", |
|---|
| 428 | address,area,area->start,area->start+area->length, area->protection); |
|---|
| 429 | |
|---|
| 430 | return -EFAULT; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | int VmInit() |
|---|
| 434 | { |
|---|
| 435 | |
|---|
| 436 | areaCache=MemCacheCreate("VmAreas", sizeof(struct VMArea), NULL, NULL, 0); |
|---|
| 437 | mapCache=MemCacheCreate("MappedPages", sizeof(struct VMMapPage), NULL, NULL, 0); |
|---|
| 438 | |
|---|
| 439 | if (!areaCache || !mapCache) |
|---|
| 440 | return -ENOMEM; |
|---|
| 441 | |
|---|
| 442 | return 0; |
|---|
| 443 | } |
|---|