Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1120 lines
29 KiB

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