| 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 <llist.h> |
|---|
| 22 | #include <malloc.h> |
|---|
| 23 | #include <module.h> |
|---|
| 24 | #include <typedefs.h> |
|---|
| 25 | #include <i386/i386.h> |
|---|
| 26 | #include <slab.h> |
|---|
| 27 | #include <user_acc.h> |
|---|
| 28 | #include <sys.h> |
|---|
| 29 | #include <print.h> |
|---|
| 30 | #include <preempt.h> |
|---|
| 31 | |
|---|
| 32 | extern struct Cache* areaCache,*mapCache; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | int MmapHandleFault(struct Process* process, DWORD address, int error) |
|---|
| 37 | { |
|---|
| 38 | struct VMArea* area; |
|---|
| 39 | |
|---|
| 40 | address=PAGE_ALIGN(address); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if (!process || address < PAGE_SIZE) |
|---|
| 44 | return -EFAULT; |
|---|
| 45 | |
|---|
| 46 | area = VmLookupAddress(process, address); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | if (!area) |
|---|
| 50 | return -EFAULT; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | if (!(area->protection & PAGE_RW) && (error & VM_WRITE)) |
|---|
| 56 | return -EFAULT; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | if (!(area->protection & PAGE_USER) && (error & VM_USER)) |
|---|
| 60 | return -EFAULT; |
|---|
| 61 | |
|---|
| 62 | if (error & VM_PROTECTION) |
|---|
| 63 | return VmProtFault(area,address); |
|---|
| 64 | else |
|---|
| 65 | return VmHandleNoPage(area, address); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | static void MMapAddArea(struct Process* process, struct VMArea* area) |
|---|
| 84 | { |
|---|
| 85 | if (ListEmpty(&process->areaList)) |
|---|
| 86 | ListAdd(&area->list,&process->areaList); |
|---|
| 87 | else |
|---|
| 88 | { |
|---|
| 89 | struct VMArea* curr=NULL; |
|---|
| 90 | |
|---|
| 91 | ListForEachEntry(curr,&process->areaList,list) |
|---|
| 92 | { |
|---|
| 93 | if (curr->start > area->start) |
|---|
| 94 | break; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | DoListAdd(&area->list,curr->list.prev,&curr->list); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | void MMapRemoveArea(struct VMArea* area) |
|---|
| 114 | { |
|---|
| 115 | ListRemove(&area->list); |
|---|
| 116 | VNodeRelease(area->vNode); |
|---|
| 117 | MemCacheFree(areaCache,(void*)area); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | void MMapFreePage(DWORD virt) |
|---|
| 136 | { |
|---|
| 137 | struct PhysPage* page; |
|---|
| 138 | struct VMArea* area; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | return; |
|---|
| 143 | |
|---|
| 144 | if (PAGE_IS_PRESENT(virt)) |
|---|
| 145 | { |
|---|
| 146 | DWORD pageAddr; |
|---|
| 147 | |
|---|
| 148 | pageAddr=VirtToPhys(virt); |
|---|
| 149 | page=PageGetStruct(pageAddr); |
|---|
| 150 | |
|---|
| 151 | if (page->refs == 1) |
|---|
| 152 | { |
|---|
| 153 | area = VmLookupAddress(current, virt); |
|---|
| 154 | |
|---|
| 155 | if (area) |
|---|
| 156 | VmFreeMappedPage(area->vNode, page->physAddr); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | if (page->refs <= 0) |
|---|
| 160 | { |
|---|
| 161 | KePrint("%#X, %#X: page->physAddr = %#X (refs = %d) pid = %d\n", |
|---|
| 162 | virt, page, page->physAddr, page->refs, current->pid); |
|---|
| 163 | cli(); hlt(); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | PagePut(page); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void MMapFreePages(DWORD start, DWORD end) |
|---|
| 171 | { |
|---|
| 172 | DWORD i; |
|---|
| 173 | |
|---|
| 174 | for (i = PAGE_ALIGN(start); i < PAGE_ALIGN_UP(end); i += PAGE_SIZE) |
|---|
| 175 | MMapFreePage(i); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | void MmapProcessRemove(struct Process* process) |
|---|
| 192 | { |
|---|
| 193 | struct VMArea* curr, *curr2; |
|---|
| 194 | DWORD i; |
|---|
| 195 | DWORD flags; |
|---|
| 196 | |
|---|
| 197 | IrqSaveFlags(flags); |
|---|
| 198 | |
|---|
| 199 | ListForEachEntrySafe(curr, curr2, &process->areaList, list) |
|---|
| 200 | { |
|---|
| 201 | for (i=curr->start; i< curr->start+curr->length; i+=PAGE_SIZE) |
|---|
| 202 | MMapFreePage(i); |
|---|
| 203 | |
|---|
| 204 | MMapRemoveArea(curr); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | IrqRestoreFlags(flags); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | SYMBOL_EXPORT(MmapProcessRemove); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | DWORD MMapFindAddress(struct Process* process,DWORD length) |
|---|
| 226 | { |
|---|
| 227 | struct VMArea* curr; |
|---|
| 228 | DWORD mapStart=MMAP_BASE,mapEnd; |
|---|
| 229 | |
|---|
| 230 | length=PAGE_ALIGN_UP(length); |
|---|
| 231 | |
|---|
| 232 | ListForEachEntry(curr,&process->areaList,list) |
|---|
| 233 | { |
|---|
| 234 | mapEnd=curr->start; |
|---|
| 235 | |
|---|
| 236 | if (mapStart <= mapEnd && (mapEnd-mapStart) >= length) |
|---|
| 237 | return mapStart; |
|---|
| 238 | |
|---|
| 239 | mapStart=curr->start+PAGE_ALIGN_UP(curr->length); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | if (UNLIKELY(mapStart == MMAP_END)) |
|---|
| 244 | return 0; |
|---|
| 245 | |
|---|
| 246 | return mapStart; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | void MMapMerge(struct VMArea* first, struct VMArea* second) |
|---|
| 265 | { |
|---|
| 266 | if (first->start+first->length != second->start) |
|---|
| 267 | return; |
|---|
| 268 | |
|---|
| 269 | if (first->vNode != second->vNode) |
|---|
| 270 | return; |
|---|
| 271 | |
|---|
| 272 | if (first->areaOps != second->areaOps) |
|---|
| 273 | return; |
|---|
| 274 | |
|---|
| 275 | if (first->flags != second->flags) |
|---|
| 276 | return; |
|---|
| 277 | |
|---|
| 278 | if (first->protection != second->protection) |
|---|
| 279 | return; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | if (!(first->flags & MMAP_ANON) && second->offset != first->offset+first->length) |
|---|
| 283 | return; |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | first->length+=second->length; |
|---|
| 287 | MMapRemoveArea(second); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | void MMapMergeMappings(struct Process* process, struct VMArea* area) |
|---|
| 291 | { |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | struct VMArea* prev,*next; |
|---|
| 295 | |
|---|
| 296 | prev=ListEntry(area->list.prev,struct VMArea,list); |
|---|
| 297 | next=ListEntry(area->list.next,struct VMArea,list); |
|---|
| 298 | |
|---|
| 299 | if (area->list.next != &process->areaList) |
|---|
| 300 | MMapMerge(area, next); |
|---|
| 301 | |
|---|
| 302 | if (area->list.prev != &process->areaList) |
|---|
| 303 | MMapMerge(prev, area); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | DWORD MMapDo(struct Process* process,struct VNode* vNode,DWORD address,DWORD length,int protection,DWORD offset,int flags, struct VmAreaOps* ops) |
|---|
| 327 | { |
|---|
| 328 | struct VMArea* area=(struct VMArea*)MemCacheAlloc(areaCache); |
|---|
| 329 | |
|---|
| 330 | if (!process || !length || !area) |
|---|
| 331 | return 0; |
|---|
| 332 | |
|---|
| 333 | length = PAGE_ALIGN_UP(length); |
|---|
| 334 | |
|---|
| 335 | if (!(flags & MMAP_FIXED)) |
|---|
| 336 | address=MMapFindAddress(process, length); |
|---|
| 337 | |
|---|
| 338 | address = PAGE_ALIGN(address); |
|---|
| 339 | |
|---|
| 340 | if (address < MMAP_BASE || address >= MMAP_END) |
|---|
| 341 | return 0; |
|---|
| 342 | |
|---|
| 343 | if (vNode) |
|---|
| 344 | { |
|---|
| 345 | |
|---|
| 346 | if (flags & (MMAP_GROWDOWN)) |
|---|
| 347 | return 0; |
|---|
| 348 | |
|---|
| 349 | if (!(vNode->mode & VFS_ATTR_FILE)) |
|---|
| 350 | return 0; |
|---|
| 351 | |
|---|
| 352 | }else if (!vNode) |
|---|
| 353 | { |
|---|
| 354 | |
|---|
| 355 | if (NameToVNode(&vNode,DEVICES_PATH "Special/Zero",0, NULL)) |
|---|
| 356 | return 0; |
|---|
| 357 | |
|---|
| 358 | flags |= MMAP_ANON; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | vNode->refs++; |
|---|
| 363 | area->vNode=vNode; |
|---|
| 364 | area->start=address; |
|---|
| 365 | area->length=length; |
|---|
| 366 | area->offset=offset; |
|---|
| 367 | area->process=process; |
|---|
| 368 | area->protection=protection; |
|---|
| 369 | area->flags=flags; |
|---|
| 370 | area->areaOps=ops; |
|---|
| 371 | |
|---|
| 372 | MMapUnmap(process, address, length); |
|---|
| 373 | |
|---|
| 374 | MMapAddArea(process,area); |
|---|
| 375 | MMapMergeMappings(process, area); |
|---|
| 376 | |
|---|
| 377 | return address; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | SYMBOL_EXPORT(MMapDo); |
|---|
| 381 | |
|---|
| 382 | void MMapUnmapArea(struct Process* process,struct VMArea* area,DWORD start,DWORD len) |
|---|
| 383 | { |
|---|
| 384 | DWORD end=start+len; |
|---|
| 385 | struct VMArea* newArea; |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | if (area->start == start && area->length == len) |
|---|
| 389 | return; |
|---|
| 390 | |
|---|
| 391 | if (start >= area->start && end == area->start+area->length) |
|---|
| 392 | { |
|---|
| 393 | area->length-=((area->start+area->length)-start); |
|---|
| 394 | }else if (start == area->start && end <= area->start+area->length) |
|---|
| 395 | { |
|---|
| 396 | area->offset+=(end-area->start); |
|---|
| 397 | area->start=end; |
|---|
| 398 | }else if (start > area->start && end < area->start+area->length) |
|---|
| 399 | { |
|---|
| 400 | newArea=(struct VMArea*)MemCacheAlloc(areaCache); |
|---|
| 401 | if (!newArea) |
|---|
| 402 | return; |
|---|
| 403 | |
|---|
| 404 | memcpy(newArea,area,sizeof(struct VMArea)); |
|---|
| 405 | newArea->offset+=(end-area->start); |
|---|
| 406 | newArea->start=end; |
|---|
| 407 | newArea->vNode->refs++; |
|---|
| 408 | area->length=end-area->start; |
|---|
| 409 | MMapAddArea(process,newArea); |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | newArea=(struct VMArea*)MemCacheAlloc(areaCache); |
|---|
| 413 | |
|---|
| 414 | if (!newArea) |
|---|
| 415 | return; |
|---|
| 416 | |
|---|
| 417 | memcpy(newArea,area,sizeof(struct VMArea)); |
|---|
| 418 | |
|---|
| 419 | MMapAddArea(process,newArea); |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | int MMapUnmap(struct Process* process,DWORD start, DWORD len) |
|---|
| 438 | { |
|---|
| 439 | struct VMArea* area,*next; |
|---|
| 440 | struct ListHead* lNext; |
|---|
| 441 | DWORD currLen=len; |
|---|
| 442 | DWORD end; |
|---|
| 443 | int err=0; |
|---|
| 444 | DWORD flags; |
|---|
| 445 | |
|---|
| 446 | IrqSaveFlags(flags); |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | if (PAGE_OFFSET(start) || start > MMAP_END || len > MMAP_END-start || !len) |
|---|
| 450 | { |
|---|
| 451 | err = -EINVAL; |
|---|
| 452 | goto error; |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | area=VmLookupAddress(process, start); |
|---|
| 456 | |
|---|
| 457 | if (!area) |
|---|
| 458 | { |
|---|
| 459 | err = -EFAULT; |
|---|
| 460 | goto error; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | end = start + len; |
|---|
| 464 | |
|---|
| 465 | if (area->start >= end) |
|---|
| 466 | { |
|---|
| 467 | err = 0; |
|---|
| 468 | goto error; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | while (currLen) |
|---|
| 472 | { |
|---|
| 473 | DWORD currStart=(start < area->start) ? area->start : start; |
|---|
| 474 | end=start+len; |
|---|
| 475 | end=(end > area->start+area->length) ? area->start+area->length : end; |
|---|
| 476 | |
|---|
| 477 | lNext = &area->list.next; |
|---|
| 478 | next=ListEntry(area->list.next, struct VMArea, list); |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | ListRemove(&area->list); |
|---|
| 482 | |
|---|
| 483 | MMapUnmapArea(process, area, currStart, end-currStart); |
|---|
| 484 | |
|---|
| 485 | MemCacheFree(areaCache, area); |
|---|
| 486 | |
|---|
| 487 | if (lNext == &process->areaList && currLen > 0) |
|---|
| 488 | { |
|---|
| 489 | err=-EINVAL; |
|---|
| 490 | goto error; |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | area = next; |
|---|
| 494 | |
|---|
| 495 | currLen-=(end-start); |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | MMapFreePages(start, end); |
|---|
| 499 | |
|---|
| 500 | error: |
|---|
| 501 | IrqRestoreFlags(flags); |
|---|
| 502 | return err; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | DWORD SysMemoryMap(DWORD address,DWORD length,int protection,int fd,DWORD offset,int flags) |
|---|
| 523 | { |
|---|
| 524 | struct File* file=NULL; |
|---|
| 525 | struct VNode* vNode=NULL; |
|---|
| 526 | |
|---|
| 527 | if (fd != -1 && !(file=FileGet(fd))) |
|---|
| 528 | return 0; |
|---|
| 529 | |
|---|
| 530 | if (file) |
|---|
| 531 | vNode=file->vNode; |
|---|
| 532 | |
|---|
| 533 | return MMapDo(current, vNode, PAGE_ALIGN(address), |
|---|
| 534 | PAGE_ALIGN_UP(length), protection | PAGE_USER, offset, flags, NULL); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | int MMapProtectionFixStart(struct VMArea* area, DWORD end, int protection) |
|---|
| 538 | { |
|---|
| 539 | struct VMArea* areaNext; |
|---|
| 540 | |
|---|
| 541 | areaNext = (struct VMArea*)MemCacheAlloc(areaCache); |
|---|
| 542 | |
|---|
| 543 | memcpy(areaNext, area, sizeof(struct VMArea)); |
|---|
| 544 | |
|---|
| 545 | area->length = end - area->start; |
|---|
| 546 | area->protection = protection; |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | areaNext->offset += (end - areaNext->start) >> PAGE_SHIFT; |
|---|
| 551 | areaNext->start = end; |
|---|
| 552 | MMapAddArea(current, areaNext); |
|---|
| 553 | |
|---|
| 554 | return 0; |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | int MMapProtectionFixAll(struct VMArea* area, int protection) |
|---|
| 558 | { |
|---|
| 559 | area->protection=protection; |
|---|
| 560 | return 0; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | int MMapProtectionFixEnd(struct VMArea* area, DWORD start, int protection) |
|---|
| 564 | { |
|---|
| 565 | KePrint("MMapProtectionFixEnd\n"); |
|---|
| 566 | return 0; |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | int MMapProtectionFixMiddle(struct VMArea* area, DWORD start, DWORD end, int protection) |
|---|
| 570 | { |
|---|
| 571 | KePrint("MMapProtectionFixMiddle\n"); |
|---|
| 572 | return 0; |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | int MMapProtectionChange(struct VMArea* area, DWORD start, DWORD end, int protection) |
|---|
| 576 | { |
|---|
| 577 | int error=-ENOTIMPL; |
|---|
| 578 | |
|---|
| 579 | if (protection == area->protection) |
|---|
| 580 | return 0; |
|---|
| 581 | |
|---|
| 582 | if (start == area->start) |
|---|
| 583 | { |
|---|
| 584 | if (end == area->start+area->length) |
|---|
| 585 | error=MMapProtectionFixAll(area, protection); |
|---|
| 586 | else |
|---|
| 587 | error = MMapProtectionFixStart(area, end, protection); |
|---|
| 588 | }else if (end == area->start + area->length) |
|---|
| 589 | { |
|---|
| 590 | error = MMapProtectionFixEnd(area, start, protection); |
|---|
| 591 | }else{ |
|---|
| 592 | error = MMapProtectionFixMiddle(area, start, end, protection); |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | if (error) |
|---|
| 596 | return error; |
|---|
| 597 | |
|---|
| 598 | VirtChangeProtection(start, end, protection); |
|---|
| 599 | |
|---|
| 600 | return 0; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | int SysMemoryProtect(DWORD start, size_t length, int protection) |
|---|
| 604 | { |
|---|
| 605 | int error=-EINVAL; |
|---|
| 606 | size_t end; |
|---|
| 607 | size_t address; |
|---|
| 608 | struct VMArea* area; |
|---|
| 609 | |
|---|
| 610 | protection |= PAGE_USER; |
|---|
| 611 | |
|---|
| 612 | PreemptDisable(); |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | if (PAGE_OFFSET(start)) |
|---|
| 616 | goto out; |
|---|
| 617 | |
|---|
| 618 | length=PAGE_ALIGN_UP(length); |
|---|
| 619 | end=start+length; |
|---|
| 620 | |
|---|
| 621 | if (end < start) |
|---|
| 622 | goto out; |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | error=0; |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | if (start == end) |
|---|
| 630 | goto out; |
|---|
| 631 | |
|---|
| 632 | area=VmLookupAddress(current, start); |
|---|
| 633 | |
|---|
| 634 | error=-EFAULT; |
|---|
| 635 | |
|---|
| 636 | if (!area) |
|---|
| 637 | goto out; |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | address=start; |
|---|
| 641 | |
|---|
| 642 | while (1) |
|---|
| 643 | { |
|---|
| 644 | if ((area->start + area->length) >= end) |
|---|
| 645 | { |
|---|
| 646 | error=MMapProtectionChange(area, address, end, protection); |
|---|
| 647 | break; |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | KePrint("SysMemoryProtect: todo\n"); |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | out: |
|---|
| 654 | PreemptEnable(); |
|---|
| 655 | return error; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | int SysMemoryUnmap(DWORD address,DWORD length) |
|---|
| 672 | { |
|---|
| 673 | return MMapUnmap(current,address, PAGE_ALIGN_UP(length)); |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | void* SysMoreCore(int len) |
|---|
| 678 | { |
|---|
| 679 | len=PAGE_ALIGN(len); |
|---|
| 680 | |
|---|
| 681 | KePrint("SysMoreCore: legacy. No longer supported\n"); |
|---|
| 682 | return NULL; |
|---|
| 683 | |
|---|
| 684 | DWORD flags; |
|---|
| 685 | IrqSaveFlags(flags); |
|---|
| 686 | |
|---|
| 687 | if (len < 0) |
|---|
| 688 | { |
|---|
| 689 | KePrint("TODO: len = %d\n",len); |
|---|
| 690 | |
|---|
| 691 | current->memManager->end=PAGE_ALIGN(current->memManager->end); |
|---|
| 692 | goto end; |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | if (len > 0) |
|---|
| 696 | |
|---|
| 697 | if (!MMapDo(current, NULL, current->memManager->end, len, PAGE_RW | PAGE_USER | PAGE_PRESENT, 0, MMAP_FIXED | MMAP_PRIVATE, NULL)) |
|---|
| 698 | return NULL; |
|---|
| 699 | |
|---|
| 700 | end: |
|---|
| 701 | current->memManager->end+=len; |
|---|
| 702 | IrqRestoreFlags(flags); |
|---|
| 703 | return (void*)(current->memManager->end-len); |
|---|
| 704 | } |
|---|
| 705 | |
|---|
| 706 | struct SysCall mmapSysCalls[]= |
|---|
| 707 | { |
|---|
| 708 | SysEntry(SysMoreCore, sizeof(int)), |
|---|
| 709 | SysEntry(SysMemoryMap, 24), |
|---|
| 710 | SysEntry(SysMemoryProtect, 12), |
|---|
| 711 | SysEntry(SysMemoryUnmap, 8), |
|---|
| 712 | SysEntryEnd(), |
|---|
| 713 | }; |
|---|
| 714 | |
|---|
| 715 | int VmInit(); |
|---|
| 716 | |
|---|
| 717 | int MMapInit() |
|---|
| 718 | { |
|---|
| 719 | VmInit(); |
|---|
| 720 | SysRegisterRange(SYS_MMAP_BASE, mmapSysCalls); |
|---|
| 721 | return 0; |
|---|
| 722 | } |
|---|