lockvm.c
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 27k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /*++
  2. Copyright (c) 1989  Microsoft Corporation
  3. Module Name:
  4.    lockvm.c
  5. Abstract:
  6.     This module contains the routines which implement the
  7.     NtLockVirtualMemory service.
  8. Author:
  9.     Lou Perazzoli (loup) 20-August-1989
  10. Revision History:
  11.     Landy Wang (landyw) 08-April-1998 : Modifications for 3-level 64-bit NT.
  12. --*/
  13. #include "mi.h"
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE,NtLockVirtualMemory)
  16. #pragma alloc_text(PAGE,NtUnlockVirtualMemory)
  17. #endif
  18. NTSTATUS
  19. NtLockVirtualMemory (
  20.     IN HANDLE ProcessHandle,
  21.     IN OUT PVOID *BaseAddress,
  22.     IN OUT PSIZE_T RegionSize,
  23.     IN ULONG MapType
  24.      )
  25. /*++
  26. Routine Description:
  27.     This function locks a region of pages within the working set list
  28.     of a subject process.
  29.     The caller of this function must have PROCESS_VM_OPERATION access
  30.     to the target process.  The caller must also have SeLockMemoryPrivilege.
  31. Arguments:
  32.    ProcessHandle - Supplies an open handle to a process object.
  33.    BaseAddress - The base address of the region of pages
  34.         to be locked. This value is rounded down to the
  35.         next host page address boundary.
  36.    RegionSize - A pointer to a variable that will receive
  37.         the actual size in bytes of the locked region of
  38.         pages. The initial value of this argument is
  39.         rounded up to the next host page size boundary.
  40.    MapType - A set of flags that describe the type of locking to
  41.             perform.  One of MAP_PROCESS or MAP_SYSTEM.
  42. Return Value:
  43.     Returns the status
  44.     STATUS_PRIVILEGE_NOT_HELD - The caller did not have sufficient
  45.         privilege to perform the requested operation.
  46.     TBS
  47. --*/
  48. {
  49.     PVOID Va;
  50.     PVOID EndingAddress;
  51.     PMMPTE PointerPte;
  52.     PMMPTE PointerPte1;
  53.     PMMPFN Pfn1;
  54.     PMMPTE PointerPde;
  55.     PMMPTE PointerPpe;
  56.     ULONG_PTR CapturedRegionSize;
  57.     PVOID CapturedBase;
  58.     PEPROCESS TargetProcess;
  59.     NTSTATUS Status;
  60.     BOOLEAN WasLocked;
  61.     KPROCESSOR_MODE PreviousMode;
  62.     ULONG Entry;
  63.     ULONG SwapEntry;
  64.     SIZE_T NumberOfAlreadyLocked;
  65.     SIZE_T NumberToLock;
  66.     ULONG WorkingSetIndex;
  67.     PMMVAD Vad;
  68.     PVOID LastVa;
  69.     MMLOCK_CONFLICT Conflict;
  70.     ULONG Waited;
  71.     LOGICAL Attached;
  72. #if defined(_MIALT4K_)
  73.     BOOLEAN IsWow64Process = FALSE;
  74. #endif
  75.     PAGED_CODE();
  76.     WasLocked = FALSE;
  77.     LastVa = NULL;
  78.     //
  79.     // Validate the flags in MapType.
  80.     //
  81.     if ((MapType & ~(MAP_PROCESS | MAP_SYSTEM)) != 0) {
  82.         return STATUS_INVALID_PARAMETER;
  83.     }
  84.     if ((MapType & (MAP_PROCESS | MAP_SYSTEM)) == 0) {
  85.         return STATUS_INVALID_PARAMETER;
  86.     }
  87.     PreviousMode = KeGetPreviousMode();
  88.     try {
  89.         if (PreviousMode != KernelMode) {
  90.             ProbeForWritePointer ((PULONG)BaseAddress);
  91.             ProbeForWriteUlong_ptr (RegionSize);
  92.         }
  93.         //
  94.         // Capture the base address.
  95.         //
  96.         CapturedBase = *BaseAddress;
  97.         //
  98.         // Capture the region size.
  99.         //
  100.         CapturedRegionSize = *RegionSize;
  101.     } except (ExSystemExceptionFilter()) {
  102.         //
  103.         // If an exception occurs during the probe or capture
  104.         // of the initial values, then handle the exception and
  105.         // return the exception code as the status value.
  106.         //
  107.         return GetExceptionCode();
  108.     }
  109.     //
  110.     // Make sure the specified starting and ending addresses are
  111.     // within the user part of the virtual address space.
  112.     //
  113.     if (CapturedBase > MM_HIGHEST_USER_ADDRESS) {
  114.         //
  115.         // Invalid base address.
  116.         //
  117.         return STATUS_INVALID_PARAMETER;
  118.     }
  119.     if ((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (ULONG_PTR)CapturedBase <
  120.                                                         CapturedRegionSize) {
  121.         //
  122.         // Invalid region size;
  123.         //
  124.         return STATUS_INVALID_PARAMETER;
  125.     }
  126.     if (CapturedRegionSize == 0) {
  127.         return STATUS_INVALID_PARAMETER;
  128.     }
  129.     //
  130.     // Reference the specified process.
  131.     //
  132.     Status = ObReferenceObjectByHandle ( ProcessHandle,
  133.                                          PROCESS_VM_OPERATION,
  134.                                          PsProcessType,
  135.                                          PreviousMode,
  136.                                          (PVOID *)&TargetProcess,
  137.                                          NULL );
  138.     if (!NT_SUCCESS(Status)) {
  139.         return Status;
  140.     }
  141.     if ((MapType & MAP_SYSTEM) != 0) {
  142.         //
  143.         // In addition to PROCESS_VM_OPERATION access to the target
  144.         // process, the caller must have SE_LOCK_MEMORY_PRIVILEGE.
  145.         //
  146.         if (!SeSinglePrivilegeCheck(
  147.                            SeLockMemoryPrivilege,
  148.                            PreviousMode
  149.                            )) {
  150.             ObDereferenceObject( TargetProcess );
  151.             return( STATUS_PRIVILEGE_NOT_HELD );
  152.         }
  153.     }
  154.     //
  155.     // Attach to the specified process.
  156.     //
  157.     if (ProcessHandle != NtCurrentProcess()) {
  158.         KeAttachProcess (&TargetProcess->Pcb);
  159.         Attached = TRUE;
  160.     }
  161.     else {
  162.         Attached = FALSE;
  163.     }
  164.     //
  165.     // Get address creation mutex, this prevents the
  166.     // address range from being modified while it is examined.  Raise
  167.     // to APC level to prevent an APC routine from acquiring the
  168.     // address creation mutex.  Get the working set mutex so the
  169.     // number of already locked pages in the request can be determined.
  170.     //
  171. #if defined(_MIALT4K_)
  172.     //
  173.     // changing to 4k aligned should not change the correctness.
  174.     //
  175.     EndingAddress = PAGE_4K_ALIGN((PCHAR)CapturedBase + CapturedRegionSize - 1);
  176. #else
  177.     EndingAddress = PAGE_ALIGN((PCHAR)CapturedBase + CapturedRegionSize - 1);
  178. #endif
  179.     Va = PAGE_ALIGN (CapturedBase);
  180.     NumberOfAlreadyLocked = 0;
  181.     NumberToLock = ((ULONG_PTR)EndingAddress - (ULONG_PTR)Va) >> PAGE_SHIFT;
  182.     LOCK_WS_AND_ADDRESS_SPACE (TargetProcess);
  183.     //
  184.     // Make sure the address space was not deleted, if so, return an error.
  185.     //
  186.     if (TargetProcess->AddressSpaceDeleted != 0) {
  187.         Status = STATUS_PROCESS_IS_TERMINATING;
  188.         goto ErrorReturn;
  189.     }
  190.     if (NumberToLock + MM_FLUID_WORKING_SET >
  191.                                     TargetProcess->Vm.MinimumWorkingSetSize) {
  192.         Status = STATUS_WORKING_SET_QUOTA;
  193.         goto ErrorReturn;
  194.     }
  195.     while (Va <= EndingAddress) {
  196.         if (Va > LastVa) {
  197.             //
  198.             // Don't lock physically mapped views.
  199.             //
  200.             Vad = MiLocateAddress (Va);
  201.             if (Vad == NULL) {
  202.                 Status = STATUS_ACCESS_VIOLATION;
  203.                 goto ErrorReturn;
  204.             }
  205.             if ((Vad->u.VadFlags.PhysicalMapping == 1) ||
  206.                 (Vad->u.VadFlags.UserPhysicalPages == 1)) {
  207.                 Status = STATUS_INCOMPATIBLE_FILE_MAP;
  208.                 goto ErrorReturn;
  209.             }
  210.             LastVa = MI_VPN_TO_VA (Vad->EndingVpn);
  211.         }
  212.         if (MmIsAddressValid (Va)) {
  213.             //
  214.             // The page is valid, therefore it is in the working set.
  215.             // Locate the WSLE for the page and see if it is locked.
  216.             //
  217.             PointerPte1 = MiGetPteAddress (Va);
  218.             Pfn1 = MI_PFN_ELEMENT (PointerPte1->u.Hard.PageFrameNumber);
  219.             WorkingSetIndex = MiLocateWsle (Va,
  220.                                             MmWorkingSetList,
  221.                                             Pfn1->u1.WsIndex);
  222.             ASSERT (WorkingSetIndex != WSLE_NULL_INDEX);
  223.             if (WorkingSetIndex < MmWorkingSetList->FirstDynamic) {
  224.                 //
  225.                 // This page is locked in the working set.
  226.                 //
  227.                 NumberOfAlreadyLocked += 1;
  228.                 //
  229.                 // Check to see if the WAS_LOCKED status should be returned.
  230.                 //
  231.                 if ((MapType & MAP_PROCESS) &&
  232.                         (MmWsle[WorkingSetIndex].u1.e1.LockedInWs == 1)) {
  233.                     WasLocked = TRUE;
  234.                 }
  235.                 if ((MapType & MAP_SYSTEM) &&
  236.                         (MmWsle[WorkingSetIndex].u1.e1.LockedInMemory == 1)) {
  237.                     WasLocked = TRUE;
  238.                 }
  239.             }
  240.         }
  241.         Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  242.     }
  243.     UNLOCK_WS_UNSAFE (TargetProcess);
  244.     //
  245.     // Check to ensure the working set list is still fluid after
  246.     // the requested number of pages are locked.
  247.     //
  248.     if (TargetProcess->Vm.MinimumWorkingSetSize <
  249.           ((MmWorkingSetList->FirstDynamic + NumberToLock +
  250.                       MM_FLUID_WORKING_SET) - NumberOfAlreadyLocked)) {
  251.         Status = STATUS_WORKING_SET_QUOTA;
  252.         UNLOCK_ADDRESS_SPACE (TargetProcess);
  253.         goto ErrorReturn1;
  254.     }
  255.     Va = PAGE_ALIGN (CapturedBase);
  256. #if defined(_MIALT4K_)
  257.     if (TargetProcess->Wow64Process != NULL) {
  258.         IsWow64Process = TRUE;
  259.         Va = PAGE_4K_ALIGN (CapturedBase);
  260.     }
  261. #endif
  262.     //
  263.     // Set up an exception handler and touch each page in the specified
  264.     // range.
  265.     //
  266.     MiInsertConflictInList (&Conflict);
  267.     try {
  268.         while (Va <= EndingAddress) {
  269.             *(volatile ULONG *)Va;
  270.             Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  271.         }
  272.     } except (EXCEPTION_EXECUTE_HANDLER) {
  273.         Status = GetExceptionCode();
  274.         MiRemoveConflictFromList (&Conflict);
  275.         UNLOCK_ADDRESS_SPACE (TargetProcess);
  276.         goto ErrorReturn1;
  277.     }
  278.     MiRemoveConflictFromList (&Conflict);
  279.     //
  280.     // The complete address range is accessible, lock the pages into
  281.     // the working set.
  282.     //
  283.     PointerPte = MiGetPteAddress (CapturedBase);
  284.     Va = PAGE_ALIGN (CapturedBase);
  285. #if defined(_MIALT4K_)
  286.     if (IsWow64Process) {
  287.         Va = PAGE_4K_ALIGN (CapturedBase);
  288.     }
  289. #endif
  290.     //
  291.     // Acquire the working set mutex, no page faults are allowed.
  292.     //
  293.     LOCK_WS_UNSAFE (TargetProcess);
  294.     while (Va <= EndingAddress) {
  295.         //
  296.         // Make sure the PDE is valid.
  297.         //
  298.         PointerPde = MiGetPdeAddress (Va);
  299.         PointerPpe = MiGetPteAddress (PointerPde);
  300.         do {
  301.             (VOID)MiDoesPpeExistAndMakeValid (PointerPpe,
  302.                                               TargetProcess,
  303.                                               FALSE,
  304.                                               &Waited);
  305.             Waited = 0;
  306.             (VOID)MiDoesPdeExistAndMakeValid (PointerPde,
  307.                                               TargetProcess,
  308.                                               FALSE,
  309.                                               &Waited);
  310.         } while (Waited != 0);
  311.         //
  312.         // Make sure the page is in the working set.
  313.         //
  314.         while (PointerPte->u.Hard.Valid == 0) {
  315.             //
  316.             // Release the working set mutex and fault in the page.
  317.             //
  318.             UNLOCK_WS_UNSAFE (TargetProcess);
  319.             //
  320.             // Page in the PDE and make the PTE valid.
  321.             //
  322.             *(volatile ULONG *)Va;
  323.             //
  324.             // Reacquire the working set mutex.
  325.             //
  326.             LOCK_WS_UNSAFE (TargetProcess);
  327.             //
  328.             // Make sure the page directory & table pages are still valid.
  329.             // Trimming could occur if either of the pages that were just
  330.             // made valid were removed from the working set before the
  331.             // working set lock was acquired.
  332.             //
  333.             do {
  334.                 (VOID)MiDoesPpeExistAndMakeValid (PointerPpe,
  335.                                                   TargetProcess,
  336.                                                   FALSE,
  337.                                                   &Waited);
  338.                 Waited = 0;
  339.                 (VOID)MiDoesPdeExistAndMakeValid (PointerPde,
  340.                                                   TargetProcess,
  341.                                                   FALSE,
  342.                                                   &Waited);
  343.             } while (Waited != 0);
  344.         }
  345.         //
  346.         // The page is now in the working set, lock the page into
  347.         // the working set.
  348.         //
  349.         PointerPte1 = MiGetPteAddress (Va);
  350.         Pfn1 = MI_PFN_ELEMENT (PointerPte1->u.Hard.PageFrameNumber);
  351.         Entry = MiLocateWsle (Va, MmWorkingSetList, Pfn1->u1.WsIndex);
  352.         if (Entry >= MmWorkingSetList->FirstDynamic) {
  353.             SwapEntry = MmWorkingSetList->FirstDynamic;
  354.             if (Entry != MmWorkingSetList->FirstDynamic) {
  355.                 //
  356.                 // Swap this entry with the one at first dynamic.
  357.                 //
  358.                 MiSwapWslEntries (Entry, SwapEntry, &TargetProcess->Vm);
  359.             }
  360.             MmWorkingSetList->FirstDynamic += 1;
  361.         } else {
  362.             SwapEntry = Entry;
  363.         }
  364.         //
  365.         // Indicate that the page is locked.
  366.         //
  367.         if (MapType & MAP_PROCESS) {
  368.             MmWsle[SwapEntry].u1.e1.LockedInWs = 1;
  369.         }
  370.         if (MapType & MAP_SYSTEM) {
  371.             MmWsle[SwapEntry].u1.e1.LockedInMemory = 1;
  372.         }
  373.         //
  374.         // Increment to the next va and PTE.
  375.         //
  376.         PointerPte += 1;
  377.         Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  378.     }
  379. #if !(defined(_MIALT4K_))
  380.     UNLOCK_WS_AND_ADDRESS_SPACE (TargetProcess);
  381. #else
  382.     UNLOCK_WS_UNSAFE (TargetProcess);
  383.     if (IsWow64Process) {
  384.         MiLockFor4kPage(CapturedBase, CapturedRegionSize, TargetProcess);
  385.     }
  386.     UNLOCK_ADDRESS_SPACE (TargetProcess);
  387. #endif
  388.     if (Attached == TRUE) {
  389.         KeDetachProcess();
  390.     }
  391.     ObDereferenceObject (TargetProcess);
  392.     //
  393.     // Update return arguments.
  394.     //
  395.     //
  396.     // Establish an exception handler and write the size and base
  397.     // address.
  398.     //
  399.     try {
  400. #if defined(_MIALT4K_)
  401.         if (IsWow64Process) { 
  402.             *RegionSize = ((PCHAR)EndingAddress -
  403.                         (PCHAR)PAGE_4K_ALIGN(CapturedBase)) + PAGE_4K;
  404.             *BaseAddress = PAGE_4K_ALIGN(CapturedBase);
  405.         } else {    
  406. #endif
  407.         *RegionSize = ((PCHAR)EndingAddress - (PCHAR)PAGE_ALIGN(CapturedBase)) +
  408.                                                                     PAGE_SIZE;
  409.         *BaseAddress = PAGE_ALIGN(CapturedBase);
  410. #if defined(_MIALT4K_)
  411.         }
  412. #endif
  413.     } except (EXCEPTION_EXECUTE_HANDLER) {
  414.         return GetExceptionCode();
  415.     }
  416.     if (WasLocked) {
  417.         return STATUS_WAS_LOCKED;
  418.     }
  419.     return STATUS_SUCCESS;
  420. ErrorReturn:
  421.         UNLOCK_WS_AND_ADDRESS_SPACE (TargetProcess);
  422. ErrorReturn1:
  423.         if (Attached == TRUE) {
  424.             KeDetachProcess();
  425.         }
  426.         ObDereferenceObject (TargetProcess);
  427.         return Status;
  428. }
  429. NTSTATUS
  430. NtUnlockVirtualMemory (
  431.     IN HANDLE ProcessHandle,
  432.     IN OUT PVOID *BaseAddress,
  433.     IN OUT PSIZE_T RegionSize,
  434.     IN ULONG MapType
  435.     )
  436. /*++
  437. Routine Description:
  438.     This function unlocks a region of pages within the working set list
  439.     of a subject process.
  440.     As a side effect, any pages which are not locked and are in the
  441.     process's working set are removed from the process's working set.
  442.     This allows NtUnlockVirtualMemory to remove a range of pages
  443.     from the working set.
  444.     The caller of this function must have PROCESS_VM_OPERATION access
  445.     to the target process.
  446.     The caller must also have SeLockMemoryPrivilege for MAP_SYSTEM.
  447. Arguments:
  448.    ProcessHandle - Supplies an open handle to a process object.
  449.    BaseAddress - The base address of the region of pages
  450.         to be unlocked. This value is rounded down to the
  451.         next host page address boundary.
  452.    RegionSize - A pointer to a variable that will receive
  453.         the actual size in bytes of the unlocked region of
  454.         pages. The initial value of this argument is
  455.         rounded up to the next host page size boundary.
  456.    MapType - A set of flags that describe the type of unlocking to
  457.             perform.  One of MAP_PROCESS or MAP_SYSTEM.
  458. Return Value:
  459.     Returns the status
  460.     TBS
  461. --*/
  462. {
  463.     PVOID Va;
  464.     PVOID EndingAddress;
  465.     SIZE_T CapturedRegionSize;
  466.     PVOID CapturedBase;
  467.     PEPROCESS TargetProcess;
  468.     NTSTATUS Status;
  469.     KPROCESSOR_MODE PreviousMode;
  470.     ULONG Entry;
  471.     PMMPTE PointerPte;
  472.     PMMPFN Pfn1;
  473.     PMMVAD Vad;
  474.     PVOID LastVa;
  475.     LOGICAL Attached;
  476. #if defined(_MIALT4K_)
  477.     BOOLEAN IsWow64Process = FALSE;
  478. #endif
  479.     PAGED_CODE();
  480.     LastVa = NULL;
  481.     //
  482.     // Validate the flags in MapType.
  483.     //
  484.     if ((MapType & ~(MAP_PROCESS | MAP_SYSTEM)) != 0) {
  485.         return STATUS_INVALID_PARAMETER;
  486.     }
  487.     if ((MapType & (MAP_PROCESS | MAP_SYSTEM)) == 0) {
  488.         return STATUS_INVALID_PARAMETER;
  489.     }
  490.     PreviousMode = KeGetPreviousMode();
  491.     try {
  492.         if (PreviousMode != KernelMode) {
  493.             ProbeForWritePointer (BaseAddress);
  494.             ProbeForWriteUlong_ptr (RegionSize);
  495.         }
  496.         //
  497.         // Capture the base address.
  498.         //
  499.         CapturedBase = *BaseAddress;
  500.         //
  501.         // Capture the region size.
  502.         //
  503.         CapturedRegionSize = *RegionSize;
  504.     } except (ExSystemExceptionFilter()) {
  505.         //
  506.         // If an exception occurs during the probe or capture
  507.         // of the initial values, then handle the exception and
  508.         // return the exception code as the status value.
  509.         //
  510.         return GetExceptionCode();
  511.     }
  512.     //
  513.     // Make sure the specified starting and ending addresses are
  514.     // within the user part of the virtual address space.
  515.     //
  516.     if (CapturedBase > MM_HIGHEST_USER_ADDRESS) {
  517.         //
  518.         // Invalid base address.
  519.         //
  520.         return STATUS_INVALID_PARAMETER;
  521.     }
  522.     if ((ULONG_PTR)MM_HIGHEST_USER_ADDRESS - (ULONG_PTR)CapturedBase <
  523.                                                         CapturedRegionSize) {
  524.         //
  525.         // Invalid region size;
  526.         //
  527.         return STATUS_INVALID_PARAMETER;
  528.     }
  529.     if (CapturedRegionSize == 0) {
  530.         return STATUS_INVALID_PARAMETER;
  531.     }
  532.     Status = ObReferenceObjectByHandle ( ProcessHandle,
  533.                                          PROCESS_VM_OPERATION,
  534.                                          PsProcessType,
  535.                                          PreviousMode,
  536.                                          (PVOID *)&TargetProcess,
  537.                                          NULL );
  538.     if (!NT_SUCCESS(Status)) {
  539.         return Status;
  540.     }
  541.     if ((MapType & MAP_SYSTEM) != 0) {
  542.         //
  543.         // In addition to PROCESS_VM_OPERATION access to the target
  544.         // process, the caller must have SE_LOCK_MEMORY_PRIVILEGE.
  545.         //
  546.         if (!SeSinglePrivilegeCheck(
  547.                            SeLockMemoryPrivilege,
  548.                            PreviousMode
  549.                            )) {
  550.             ObDereferenceObject( TargetProcess );
  551.             return STATUS_PRIVILEGE_NOT_HELD;
  552.         }
  553.     }
  554.     //
  555.     // Attach to the specified process.
  556.     //
  557.     if (ProcessHandle != NtCurrentProcess()) {
  558.         KeAttachProcess (&TargetProcess->Pcb);
  559.         Attached = TRUE;
  560.     }
  561.     else {
  562.         Attached = FALSE;
  563.     }
  564.     //
  565.     // Get address creation mutex, this prevents the
  566.     // address range from being modified while it is examined.
  567.     // Block APCs so an APC routine can't get a page fault and
  568.     // corrupt the working set list, etc.
  569.     //
  570.     LOCK_WS_AND_ADDRESS_SPACE (TargetProcess);
  571.     //
  572.     // Make sure the address space was not deleted, if so, return an error.
  573.     //
  574.     if (TargetProcess->AddressSpaceDeleted != 0) {
  575.         Status = STATUS_PROCESS_IS_TERMINATING;
  576.         goto ErrorReturn;
  577.     }
  578.     EndingAddress = PAGE_ALIGN((PCHAR)CapturedBase + CapturedRegionSize - 1);
  579.     Va = PAGE_ALIGN (CapturedBase);
  580.     while (Va <= EndingAddress) {
  581.         //
  582.         // Check to ensure all the specified pages are locked.
  583.         //
  584.         //
  585.         // Don't unlock physically mapped views.
  586.         //
  587.         if (Va > LastVa) {
  588.             Vad = MiLocateAddress (Va);
  589.             if (Vad == NULL) {
  590.                 Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  591.                 Status = STATUS_NOT_LOCKED;
  592.                 break;
  593.             }
  594.             if ((Vad->u.VadFlags.PhysicalMapping == 1) ||
  595.                 (Vad->u.VadFlags.UserPhysicalPages == 1)) {
  596.                 Va = MI_VPN_TO_VA (Vad->EndingVpn);
  597.                 break;
  598.             }
  599.             LastVa = MI_VPN_TO_VA (Vad->EndingVpn);
  600.         }
  601.         if (!MmIsAddressValid (Va)) {
  602.             //
  603.             // This page is not valid, therefore not in working set.
  604.             //
  605.             Status = STATUS_NOT_LOCKED;
  606.         } else {
  607.             PointerPte = MiGetPteAddress (Va);
  608.             ASSERT (PointerPte->u.Hard.Valid != 0);
  609.             Pfn1 = MI_PFN_ELEMENT (PointerPte->u.Hard.PageFrameNumber);
  610.             Entry = MiLocateWsle (Va, MmWorkingSetList, Pfn1->u1.WsIndex);
  611.             ASSERT (Entry != WSLE_NULL_INDEX);
  612.             if ((MmWsle[Entry].u1.e1.LockedInWs == 0) &&
  613.                 (MmWsle[Entry].u1.e1.LockedInMemory == 0)) {
  614.                 //
  615.                 // Not locked in memory or system, remove from working
  616.                 // set.
  617.                 //
  618.                 PERFINFO_PAGE_INFO_DECL();
  619.                 PERFINFO_GET_PAGE_INFO(PointerPte);
  620.                 if (MiFreeWsle (Entry, &TargetProcess->Vm, PointerPte)) {
  621.                     PERFINFO_LOG_WS_REMOVAL(PERFINFO_LOG_TYPE_OUTWS_EMPTYQ, &TargetProcess->Vm);
  622.                 }
  623.                 Status = STATUS_NOT_LOCKED;
  624.             } else if (MapType & MAP_PROCESS) {
  625.                 if (MmWsle[Entry].u1.e1.LockedInWs == 0)  {
  626.                     //
  627.                     // This page is not locked.
  628.                     //
  629.                     Status = STATUS_NOT_LOCKED;
  630.                 }
  631.             } else {
  632.                 if (MmWsle[Entry].u1.e1.LockedInMemory == 0)  {
  633.                     //
  634.                     // This page is not locked.
  635.                     //
  636.                     Status = STATUS_NOT_LOCKED;
  637.                 }
  638.             }
  639.         }
  640.         Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  641.     } // end while
  642. #if defined(_MIALT4K_)
  643.     if (TargetProcess->Wow64Process != NULL) {
  644.         
  645.         IsWow64Process = TRUE;
  646.         Status = MiUnlockFor4kPage(CapturedBase, CapturedRegionSize, TargetProcess);
  647.     }
  648. #endif
  649.     if (Status == STATUS_NOT_LOCKED) {
  650.         goto ErrorReturn;
  651.     }
  652.     //
  653.     // The complete address range is locked, unlock them.
  654.     //
  655.     Va = PAGE_ALIGN (CapturedBase);
  656.     LastVa = NULL;
  657.     while (Va <= EndingAddress) {
  658. #if defined(_MIALT4K_)
  659.     if (IsWow64Process) {
  660.         if (!MiShouldBeUnlockedFor4kPage(Va, TargetProcess)) {
  661.             //
  662.             // The other 4k pages in the native page still hold the page lock.
  663.             // Should skip unlocking.
  664.             Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  665.             continue;
  666.         }
  667.     }
  668. #endif
  669.         //
  670.         // Don't unlock physically mapped views.
  671.         //
  672.         if (Va > LastVa) {
  673.             Vad = MiLocateAddress (Va);
  674.             ASSERT (Vad != NULL);
  675.             if ((Vad->u.VadFlags.PhysicalMapping == 1) ||
  676.                 (Vad->u.VadFlags.UserPhysicalPages == 1)) {
  677.                 Va = MI_VPN_TO_VA (Vad->EndingVpn);
  678.                 break;
  679.             }
  680.             LastVa = MI_VPN_TO_VA (Vad->EndingVpn);
  681.         }
  682.         PointerPte = MiGetPteAddress (Va);
  683.         ASSERT (PointerPte->u.Hard.Valid == 1);
  684.         Pfn1 = MI_PFN_ELEMENT (PointerPte->u.Hard.PageFrameNumber);
  685.         Entry = MiLocateWsle (Va, MmWorkingSetList, Pfn1->u1.WsIndex);
  686.         if (MapType & MAP_PROCESS) {
  687.             MmWsle[Entry].u1.e1.LockedInWs = 0;
  688.         }
  689.         if (MapType & MAP_SYSTEM) {
  690.             MmWsle[Entry].u1.e1.LockedInMemory = 0;
  691.         }
  692.         if ((MmWsle[Entry].u1.e1.LockedInMemory == 0) &&
  693.              MmWsle[Entry].u1.e1.LockedInWs == 0) {
  694.             //
  695.             // The page is no longer should be locked, move
  696.             // it to the dynamic part of the working set.
  697.             //
  698.             MmWorkingSetList->FirstDynamic -= 1;
  699.             if (Entry != MmWorkingSetList->FirstDynamic) {
  700.                 //
  701.                 // Swap this element with the last locked page, making
  702.                 // this element the new first dynamic entry.
  703.                 //
  704.                 MiSwapWslEntries (Entry,
  705.                                   MmWorkingSetList->FirstDynamic,
  706.                                   &TargetProcess->Vm);
  707.             }
  708.         }
  709.         Va = (PVOID)((PCHAR)Va + PAGE_SIZE);
  710.     }
  711.     UNLOCK_WS_AND_ADDRESS_SPACE (TargetProcess);
  712.     if (Attached == TRUE) {
  713.         KeDetachProcess();
  714.     }
  715.     ObDereferenceObject (TargetProcess);
  716.     //
  717.     // Update return arguments.
  718.     //
  719.     //
  720.     // Establish an exception handler and write the size and base
  721.     // address.
  722.     //
  723.     try {
  724. #if defined(_MIALT4K_)
  725.         if (IsWow64Process) { 
  726.             *RegionSize = ((PCHAR)EndingAddress -
  727.                         (PCHAR)PAGE_4K_ALIGN(CapturedBase)) + PAGE_4K;
  728.             *BaseAddress = PAGE_4K_ALIGN(CapturedBase);
  729.         } else {    
  730. #endif
  731.         *RegionSize = ((PCHAR)EndingAddress -
  732.                         (PCHAR)PAGE_ALIGN(CapturedBase)) + PAGE_SIZE;
  733.         *BaseAddress = PAGE_ALIGN(CapturedBase);
  734. #if defined(_MIALT4K_)
  735.         }
  736. #endif
  737.     } except (EXCEPTION_EXECUTE_HANDLER) {
  738.         return GetExceptionCode();
  739.     }
  740.     return STATUS_SUCCESS;
  741. ErrorReturn:
  742.         UNLOCK_WS_AND_ADDRESS_SPACE (TargetProcess);
  743.         if (Attached == TRUE) {
  744.             KeDetachProcess();
  745.         }
  746.         ObDereferenceObject (TargetProcess);
  747.         return Status;
  748. }
  749. //
  750. // Nonpagable routines.
  751. //
  752. VOID
  753. MiInsertConflictInList (
  754.     PMMLOCK_CONFLICT Conflict
  755.     )
  756. {
  757.     KIRQL OldIrql;
  758.     Conflict->Thread = PsGetCurrentThread();
  759.     ExAcquireSpinLock (&MmChargeCommitmentLock, &OldIrql);
  760.     InsertHeadList ( &MmLockConflictList,
  761.                      &Conflict->List);
  762.     ExReleaseSpinLock (&MmChargeCommitmentLock, OldIrql);
  763.     return;
  764. }
  765. VOID
  766. MiRemoveConflictFromList (
  767.     PMMLOCK_CONFLICT Conflict
  768.     )
  769. {
  770.     KIRQL OldIrql;
  771.     ExAcquireSpinLock (&MmChargeCommitmentLock, &OldIrql);
  772.     RemoveEntryList (&Conflict->List);
  773.     ExReleaseSpinLock (&MmChargeCommitmentLock, OldIrql);
  774.     return;
  775. }