00075 :
00076
00077 This function deletes a region of pages within
the virtual address
00078 space of a subject process.
00079
00080 Arguments:
00081
00082 ProcessHandle - An open handle to a process object.
00083
00084 BaseAddress - The base address of
the region of pages
00085 to be freed. This value
is rounded down to
the
00086 next host page address boundary.
00087
00088 RegionSize -
A pointer to a variable that will receive
00089
the actual size in bytes of
the freed region of
00090 pages. The initial value of
this argument
is
00091 rounded up to
the next host page size boundary.
00092
00093 FreeType -
A set of flags that describe
the type of
00094 free that
is to be performed
for the specified
00095 region of pages.
00096
00097
00098 FreeType Flags
00099
00100
00101 MEM_DECOMMIT - The specified region of pages
is to
00102 be decommitted.
00103
00104 MEM_RELEASE - The specified region of pages
is to
00105 be released.
00106
00107
00108 Return Value:
00109
00110 Returns
the status
00111
00112 --*/
00113
00114 {
00115
PMMVAD_SHORT Vad;
00116
PMMVAD_SHORT NewVad;
00117
PMMVAD PreviousVad;
00118
PMMVAD NextVad;
00119
PEPROCESS Process;
00120
KPROCESSOR_MODE PreviousMode;
00121 PVOID StartingAddress;
00122 PVOID EndingAddress;
00123
NTSTATUS Status;
00124 ULONG Attached =
FALSE;
00125 SIZE_T CapturedRegionSize;
00126 PVOID CapturedBase;
00127
PMMPTE StartingPte;
00128
PMMPTE EndingPte;
00129 SIZE_T OldQuota;
00130 SIZE_T QuotaCharge;
00131 SIZE_T CommitReduction;
00132 ULONG_PTR OldEnd;
00133 LOGICAL UserPhysicalPages;
00134
#if defined(_MIALT4K_)
00135
PVOID OriginalStartingAddress;
00136 PVOID OriginalEndingAddress;
00137 BOOLEAN EmulationFor4kPage =
FALSE;
00138
#endif
00139
00140
PAGED_CODE();
00141
00142
00143
00144
00145
00146
if ((FreeType & ~(MEM_DECOMMIT | MEM_RELEASE)) != 0) {
00147
return STATUS_INVALID_PARAMETER_4;
00148 }
00149
00150
00151
00152
00153
00154
if (((FreeType & (MEM_DECOMMIT | MEM_RELEASE)) == 0) ||
00155 ((FreeType & (MEM_DECOMMIT | MEM_RELEASE)) ==
00156 (MEM_DECOMMIT | MEM_RELEASE))) {
00157
return STATUS_INVALID_PARAMETER_4;
00158 }
00159
00160 PreviousMode = KeGetPreviousMode();
00161
00162
00163
00164
00165
00166
00167
try {
00168
00169
if (PreviousMode !=
KernelMode) {
00170
00171
ProbeForWritePointer (BaseAddress);
00172
ProbeForWriteUlong_ptr (RegionSize);
00173 }
00174
00175
00176
00177
00178
00179 CapturedBase = *BaseAddress;
00180
00181
00182
00183
00184
00185 CapturedRegionSize = *RegionSize;
00186
00187 } except (
ExSystemExceptionFilter()) {
00188
00189
00190
00191
00192
00193
00194
00195
return GetExceptionCode();
00196 }
00197
00198
#if DBG
00199
if (MmDebug &
MM_DBG_SHOW_NT_CALLS) {
00200
if ( !MmWatchProcess ) {
00201
DbgPrint(
"freevm processhandle %lx base %lx size %lx type %lx\n",
00202 ProcessHandle, CapturedBase, CapturedRegionSize, FreeType);
00203 }
00204 }
00205
#endif
00206
00207
00208
00209
00210
00211
00212
if (CapturedBase > MM_HIGHEST_USER_ADDRESS) {
00213
00214
00215
00216
00217
00218
return STATUS_INVALID_PARAMETER_2;
00219 }
00220
00221
if ((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (ULONG_PTR)CapturedBase <
00222 CapturedRegionSize) {
00223
00224
00225
00226
00227
00228
return STATUS_INVALID_PARAMETER_3;
00229
00230 }
00231
00232 EndingAddress = (PVOID)(((LONG_PTR)CapturedBase + CapturedRegionSize - 1) |
00233 (
PAGE_SIZE - 1));
00234
00235 StartingAddress = (PVOID)
PAGE_ALIGN(CapturedBase);
00236
00237
if ( ProcessHandle == NtCurrentProcess() ) {
00238 Process =
PsGetCurrentProcess();
00239 }
else {
00240
00241
00242
00243
00244
Status =
ObReferenceObjectByHandle ( ProcessHandle,
00245 PROCESS_VM_OPERATION,
00246 PsProcessType,
00247 PreviousMode,
00248 (PVOID *)&Process,
00249 NULL );
00250
00251
if (!
NT_SUCCESS(Status)) {
00252
return Status;
00253 }
00254 }
00255
00256
00257
00258
00259
00260
00261
if (
PsGetCurrentProcess() != Process) {
00262
KeAttachProcess (&Process->
Pcb);
00263 Attached =
TRUE;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
LOCK_WS_AND_ADDRESS_SPACE (Process);
00275
00276
00277
00278
00279
00280
if (Process->
AddressSpaceDeleted != 0) {
00281
Status = STATUS_PROCESS_IS_TERMINATING;
00282
goto ErrorReturn;
00283 }
00284
00285
#if defined(_MIALT4K_)
00286
00287
if (CapturedRegionSize != 0) {
00288
00289 OriginalStartingAddress = (PVOID)
PAGE_4K_ALIGN (CapturedBase);
00290
00291 OriginalEndingAddress = (PVOID)(((LONG_PTR)CapturedBase + CapturedRegionSize - 1) |
00292 (
PAGE_4K - 1));
00293
00294
if (Process->
Wow64Process !=
NULL) {
00295
00296 EmulationFor4kPage =
TRUE;
00297
00298
00299
00300
00301
00302 StartingAddress =
PAGE_NEXT_ALIGN(OriginalStartingAddress);
00303
00304 EndingAddress =
00305 (PVOID)((ULONG_PTR)
PAGE_ALIGN ((ULONG_PTR)OriginalEndingAddress + PAGE_4K) - 1);
00306
00307
if (StartingAddress > EndingAddress) {
00308
00309
00310
00311
00312
00313
UNLOCK_WS_UNSAFE (Process);
00314
00315
goto perform_free4kpages;
00316
00317 }
00318 }
00319 }
00320
00321
#endif
00322
00323 Vad = (
PMMVAD_SHORT)
MiLocateAddress (StartingAddress);
00324
00325
if (Vad ==
NULL) {
00326
00327
00328
00329
00330
00331
Status = STATUS_MEMORY_NOT_ALLOCATED;
00332
goto ErrorReturn;
00333 }
00334
00335
00336
00337
00338
00339
if (Vad->
EndingVpn <
MI_VA_TO_VPN (EndingAddress)) {
00340
00341
00342
00343
00344
00345
00346
Status = STATUS_UNABLE_TO_FREE_VM;
00347
goto ErrorReturn;
00348 }
00349
00350
00351
00352
00353
00354
00355
if ((Vad->
u.VadFlags.PrivateMemory == 0) ||
00356 (Vad->
u.VadFlags.PhysicalMapping == 1)) {
00357
Status = STATUS_UNABLE_TO_DELETE_SECTION;
00358
goto ErrorReturn;
00359 }
00360
00361
if (Vad->
u.VadFlags.NoChange == 1) {
00362
00363
00364
00365
00366
00367
00368
if (FreeType & MEM_RELEASE) {
00369
00370
00371
00372
00373
00374
00375
00376
Status =
MiCheckSecuredVad ((
PMMVAD)Vad,
00377 MI_VPN_TO_VA (Vad->
StartingVpn),
00378 ((Vad->
EndingVpn - Vad->
StartingVpn) << PAGE_SHIFT) +
00379 (PAGE_SIZE - 1),
00380 MM_SECURE_DELETE_CHECK);
00381
00382 }
else {
00383
Status =
MiCheckSecuredVad ((
PMMVAD)Vad,
00384 CapturedBase,
00385 CapturedRegionSize,
00386 MM_SECURE_DELETE_CHECK);
00387 }
00388
if (!
NT_SUCCESS (Status)) {
00389
goto ErrorReturn;
00390 }
00391 }
00392
00393 UserPhysicalPages =
FALSE;
00394
00395 PreviousVad =
MiGetPreviousVad (Vad);
00396 NextVad =
MiGetNextVad (Vad);
00397
if (FreeType & MEM_RELEASE) {
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
if (CapturedRegionSize == 0) {
00415
00416
00417
00418
00419
00420
00421
if (
MI_VA_TO_VPN (CapturedBase) != Vad->
StartingVpn) {
00422
Status = STATUS_FREE_VM_NOT_AT_BASE;
00423
goto ErrorReturn;
00424 }
00425
00426
00427
00428
00429
00430 StartingAddress =
MI_VPN_TO_VA (Vad->
StartingVpn);
00431 EndingAddress =
MI_VPN_TO_VA_ENDING (Vad->
EndingVpn);
00432
00433
00434
00435
00436
00437
00438
00439
if (Vad->
u.VadFlags.UserPhysicalPages == 1) {
00440
MiPhysicalViewRemover (Process, (
PMMVAD)Vad);
00441
MiRemoveUserPhysicalPagesVad (Vad);
00442 UserPhysicalPages =
TRUE;
00443 }
00444
else if (Vad->
u.VadFlags.WriteWatch == 1) {
00445
MiPhysicalViewRemover (Process, (
PMMVAD)Vad);
00446 }
00447
00448
MiRemoveVad ((
PMMVAD)Vad);
00449
ExFreePool (Vad);
00450
00451
#if defined(_MIALT4K_)
00452
00453 OriginalStartingAddress = StartingAddress;
00454 OriginalEndingAddress = EndingAddress;
00455
00456
if (Process->
Wow64Process !=
NULL) {
00457
00458 EmulationFor4kPage =
TRUE;
00459
00460 }
else {
00461
00462 EmulationFor4kPage =
FALSE;
00463
00464 }
00465
00466
#endif
00467
00468 }
else {
00469
00470
00471
00472
00473
00474
00475
if (
MI_VA_TO_VPN (StartingAddress) == Vad->
StartingVpn) {
00476
if (
MI_VA_TO_VPN (EndingAddress) == Vad->
EndingVpn) {
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
if (Vad->
u.VadFlags.UserPhysicalPages == 1) {
00489
MiPhysicalViewRemover (Process, (
PMMVAD)Vad);
00490
MiRemoveUserPhysicalPagesVad (Vad);
00491 UserPhysicalPages =
TRUE;
00492 }
00493
else if (Vad->
u.VadFlags.WriteWatch == 1) {
00494
MiPhysicalViewRemover (Process, (
PMMVAD)Vad);
00495 }
00496
00497
MiRemoveVad ((
PMMVAD)Vad);
00498
ExFreePool (Vad);
00499
00500 }
else {
00501
00502
if ((Vad->
u.VadFlags.UserPhysicalPages == 1) ||
00503 (Vad->
u.VadFlags.WriteWatch == 1)) {
00504
00505
00506
00507
00508
00509
00510
Status = STATUS_FREE_VM_NOT_AT_BASE;
00511
goto ErrorReturn;
00512 }
00513
00514
00515
00516
00517
00518
00519 CommitReduction =
MiCalculatePageCommitment (
00520 StartingAddress,
00521 EndingAddress,
00522 (
PMMVAD)Vad,
00523 Process );
00524
00525 Vad->
StartingVpn =
MI_VA_TO_VPN ((PCHAR)EndingAddress + 1);
00526 Vad->
u.VadFlags.CommitCharge -= CommitReduction;
00527
ASSERT ((SSIZE_T)Vad->
u.VadFlags.CommitCharge >= 0);
00528
MiReturnPageFileQuota (CommitReduction, Process);
00529
MiReturnCommitment (CommitReduction);
00530
if (Process->
JobStatus &
PS_JOB_STATUS_REPORT_COMMIT_CHANGES) {
00531
PsChangeJobMemoryUsage (-(SSIZE_T)CommitReduction);
00532 }
00533
MM_TRACK_COMMIT (MM_DBG_COMMIT_RETURN_NTFREEVM1,
00534 CommitReduction);
00535 Process->
CommitCharge -= CommitReduction;
00536 NextVad = (
PMMVAD)Vad;
00537 }
00538
00539 }
else {
00540
00541
if ((Vad->
u.VadFlags.UserPhysicalPages == 1) ||
00542 (Vad->
u.VadFlags.WriteWatch == 1)) {
00543
00544
00545
00546
00547
00548
00549
Status = STATUS_FREE_VM_NOT_AT_BASE;
00550
goto ErrorReturn;
00551 }
00552
00553
00554
00555
00556
00557
if (
MI_VA_TO_VPN (EndingAddress) == Vad->
EndingVpn) {
00558
00559
00560
00561
00562
00563 CommitReduction =
MiCalculatePageCommitment (
00564 StartingAddress,
00565 EndingAddress,
00566 (
PMMVAD)Vad,
00567 Process );
00568
00569 Vad->
u.VadFlags.CommitCharge -= CommitReduction;
00570
MiReturnPageFileQuota (CommitReduction, Process);
00571
MiReturnCommitment (CommitReduction);
00572
if (Process->
JobStatus &
PS_JOB_STATUS_REPORT_COMMIT_CHANGES) {
00573
PsChangeJobMemoryUsage (-(SSIZE_T)CommitReduction);
00574 }
00575
MM_TRACK_COMMIT (MM_DBG_COMMIT_RETURN_NTFREEVM2,
00576 CommitReduction);
00577 Process->
CommitCharge -= CommitReduction;
00578
00579 Vad->
EndingVpn =
MI_VA_TO_VPN ((PCHAR)StartingAddress - 1);
00580 PreviousVad = (
PMMVAD)Vad;
00581
00582 }
else {
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 NewVad =
ExAllocatePoolWithTag (NonPagedPool,
00594
sizeof(
MMVAD_SHORT),
00595 'SdaV');
00596
if ( NewVad ==
NULL ) {
00597
Status = STATUS_INSUFFICIENT_RESOURCES;
00598
goto ErrorReturn;
00599 }
00600
00601 CommitReduction =
MiCalculatePageCommitment (
00602 StartingAddress,
00603 EndingAddress,
00604 (
PMMVAD)Vad,
00605 Process );
00606
00607 OldQuota = Vad->
u.VadFlags.CommitCharge - CommitReduction;
00608 OldEnd = Vad->
EndingVpn;
00609
00610 *NewVad = *Vad;
00611
00612 Vad->
EndingVpn =
MI_VA_TO_VPN ((PCHAR)StartingAddress - 1);
00613 NewVad->
StartingVpn =
MI_VA_TO_VPN ((PCHAR)EndingAddress + 1);
00614
00615
00616
00617
00618
00619
00620 NewVad->
u.VadFlags.CommitCharge = 0;
00621
00622
try {
00623
00624
00625
00626
00627
00628
00629
MiInsertVad ((
PMMVAD)NewVad);
00630
00631 } except (EXCEPTION_EXECUTE_HANDLER) {
00632
00633
00634
00635
00636
00637
00638 Vad->EndingVpn = OldEnd;
00639
00640
ExFreePool (NewVad);
00641
Status = GetExceptionCode();
00642
goto ErrorReturn;
00643 }
00644
00645 Vad->u.VadFlags.CommitCharge -= CommitReduction;
00646
MiReturnPageFileQuota (CommitReduction, Process);
00647
MiReturnCommitment (CommitReduction);
00648
if (Process->
JobStatus &
PS_JOB_STATUS_REPORT_COMMIT_CHANGES) {
00649
PsChangeJobMemoryUsage (-(SSIZE_T)CommitReduction);
00650 }
00651
MM_TRACK_COMMIT (MM_DBG_COMMIT_RETURN_NTFREEVM3,
00652 CommitReduction);
00653 Process->
CommitCharge -= CommitReduction;
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 QuotaCharge =
MiCalculatePageCommitment (MI_VPN_TO_VA (Vad->StartingVpn),
00664 (PCHAR)StartingAddress - 1,
00665 (
PMMVAD)Vad,
00666 Process );
00667
00668 Vad->u.VadFlags.CommitCharge = QuotaCharge;
00669
00670
00671
00672
00673
00674 NewVad->
u.VadFlags.CommitCharge = OldQuota - QuotaCharge;
00675 PreviousVad = (
PMMVAD)Vad;
00676 NextVad = (
PMMVAD)NewVad;
00677 }
00678 }
00679 }
00680
00681
00682
00683
00684
00685
MiReturnPageTablePageCommitment (StartingAddress,
00686 EndingAddress,
00687 Process,
00688 PreviousVad,
00689 NextVad);
00690
00691
if (UserPhysicalPages ==
TRUE) {
00692
MiDeletePageTablesForPhysicalRange (StartingAddress, EndingAddress);
00693 }
00694
else {
00695
00696
00697
00698
00699
00700
MiDeleteFreeVm (StartingAddress, EndingAddress);
00701 }
00702
00703
UNLOCK_WS_UNSAFE (Process);
00704
00705 CapturedRegionSize = 1 + (PCHAR)EndingAddress - (PCHAR)StartingAddress;
00706
00707
00708
00709
00710
00711 Process->
VirtualSize -= CapturedRegionSize;
00712
00713
#if defined(_MIALT4K_)
00714
if (EmulationFor4kPage ==
TRUE) {
00715
00716
goto perform_free4kpages;
00717
00718 }
00719
#endif
00720
00721
UNLOCK_ADDRESS_SPACE (Process);
00722
00723
if (Attached) {
00724
KeDetachProcess();
00725 }
00726
00727
if ( ProcessHandle != NtCurrentProcess() ) {
00728
ObDereferenceObject (Process);
00729 }
00730
00731
00732
00733
00734
00735
try {
00736
00737 *RegionSize = CapturedRegionSize;
00738 *BaseAddress = StartingAddress;
00739
00740 } except (EXCEPTION_EXECUTE_HANDLER) {
00741
00742
00743
00744
00745
00746 }
00747
00748
#if DBG
00749
if (MmDebug &
MM_DBG_SHOW_NT_CALLS) {
00750
if ( MmWatchProcess ) {
00751
if ( MmWatchProcess ==
PsGetCurrentProcess() ) {
00752
DbgPrint(
"\n--- FREE Type 0x%lx Base %lx Size %lx\n",
00753 FreeType, StartingAddress, CapturedRegionSize);
00754 MmFooBar();
00755 }
00756 }
00757 }
00758
#endif
00759
00760
return STATUS_SUCCESS;
00761 }
00762
00763
if (Vad->u.VadFlags.UserPhysicalPages == 1) {
00764
00765
00766
00767
00768
00769
00770
Status = STATUS_MEMORY_NOT_ALLOCATED;
00771
goto ErrorReturn;
00772 }
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
if (CapturedRegionSize == 0) {
00787
00788
if (
MI_VA_TO_VPN (CapturedBase) != Vad->StartingVpn) {
00789
Status = STATUS_FREE_VM_NOT_AT_BASE;
00790
goto ErrorReturn;
00791 }
00792 EndingAddress =
MI_VPN_TO_VA_ENDING (Vad->EndingVpn);
00793 }
00794
00795
#if defined(_MIALT4K_)
00796
00797 OriginalStartingAddress = StartingAddress;
00798 OriginalEndingAddress = EndingAddress;
00799
00800
if (Process->
Wow64Process !=
NULL) {
00801
00802 EmulationFor4kPage =
TRUE;
00803
00804 }
else {
00805
00806 EmulationFor4kPage =
FALSE;
00807
00808 }
00809
00810
#endif
00811
00812
#if 0
00813
if (FreeType &
MEM_CHECK_COMMIT_STATE) {
00814
if ( !
MiIsEntireRangeCommitted(StartingAddress,
00815 EndingAddress,
00816 Vad,
00817 Process)) {
00818
00819
00820
00821
00822
00823
00824
Status = STATUS_UNABLE_TO_DECOMMIT_VM;
00825
goto ErrorReturn;
00826 }
00827 }
00828
#endif //0
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838 StartingPte =
MiGetPteAddress (StartingAddress);
00839 EndingPte =
MiGetPteAddress (EndingAddress);
00840
00841 CommitReduction = 1 + EndingPte - StartingPte;
00842
00843
00844
00845
00846
00847
00848 CommitReduction -=
MiDecommitPages (StartingAddress,
00849 EndingPte,
00850 Process,
00851 Vad);
00852
00853
00854
00855
00856
00857
ASSERT ((LONG)CommitReduction >= 0);
00858
MiReturnPageFileQuota (CommitReduction, Process);
00859
MiReturnCommitment (CommitReduction);
00860
MM_TRACK_COMMIT (MM_DBG_COMMIT_RETURN_NTFREEVM4, CommitReduction);
00861 Vad->u.VadFlags.CommitCharge -= CommitReduction;
00862
if (Process->
JobStatus &
PS_JOB_STATUS_REPORT_COMMIT_CHANGES) {
00863
PsChangeJobMemoryUsage (-(SSIZE_T)CommitReduction);
00864 }
00865 Process->
CommitCharge -= CommitReduction;
00866
ASSERT ((LONG)Vad->u.VadFlags.CommitCharge >= 0);
00867
00868
00869
#if !(defined(_MIALT4K_))
00870
UNLOCK_WS_AND_ADDRESS_SPACE (Process);
00871
#else
00872
UNLOCK_WS_UNSAFE(Process);
00873
00874 perform_free4kpages:
00875
00876
if (EmulationFor4kPage ==
TRUE) {
00877
00878
if (FreeType & MEM_RELEASE) {
00879
00880
MiReleaseFor4kPage(OriginalStartingAddress,
00881 OriginalEndingAddress,
00882 Process);
00883
00884 }
else {
00885
00886
MiDecommitFor4kPage(OriginalStartingAddress,
00887 OriginalEndingAddress,
00888 Process);
00889
00890 }
00891
00892 StartingAddress = OriginalStartingAddress;
00893 EndingAddress = OriginalEndingAddress;
00894 }
00895
00896
UNLOCK_ADDRESS_SPACE (Process);
00897
00898
#endif
00899
00900
if (Attached) {
00901
KeDetachProcess();
00902 }
00903
if ( ProcessHandle != NtCurrentProcess() ) {
00904
ObDereferenceObject (Process);
00905 }
00906
00907
00908
00909
00910
00911
00912
try {
00913
00914 *RegionSize = 1 + (PCHAR)EndingAddress - (PCHAR)StartingAddress;
00915 *BaseAddress = StartingAddress;
00916
00917 } except (EXCEPTION_EXECUTE_HANDLER) {
00918 NOTHING;
00919 }
00920
00921
return STATUS_SUCCESS;
00922
00923 ErrorReturn:
00924
UNLOCK_WS_AND_ADDRESS_SPACE (Process);
00925
00926
if (Attached) {
00927
KeDetachProcess();
00928 }
00929
00930
if ( ProcessHandle != NtCurrentProcess() ) {
00931
ObDereferenceObject (Process);
00932 }
00933
return Status;
00934 }