Source code of Windows XP (NT5)
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.

1127 lines
32 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. queryvm.c
  5. Abstract:
  6. This module contains the routines which implement the
  7. NtQueryVirtualMemory service.
  8. Author:
  9. Lou Perazzoli (loup) 21-Aug-1989
  10. Landy Wang (landyw) 02-June-1997
  11. Revision History:
  12. --*/
  13. #include "mi.h"
  14. extern POBJECT_TYPE IoFileObjectType;
  15. NTSTATUS
  16. MiGetWorkingSetInfo (
  17. IN PMEMORY_WORKING_SET_INFORMATION WorkingSetInfo,
  18. IN SIZE_T Length,
  19. IN PEPROCESS Process
  20. );
  21. MMPTE
  22. MiCaptureSystemPte (
  23. IN PMMPTE PointerProtoPte,
  24. IN PEPROCESS Process
  25. );
  26. #if DBG
  27. PEPROCESS MmWatchProcess;
  28. #endif // DBG
  29. ULONG
  30. MiQueryAddressState (
  31. IN PVOID Va,
  32. IN PMMVAD Vad,
  33. IN PEPROCESS TargetProcess,
  34. OUT PULONG ReturnedProtect,
  35. OUT PVOID *NextVaToQuery
  36. );
  37. #ifdef ALLOC_PRAGMA
  38. #pragma alloc_text(PAGE,NtQueryVirtualMemory)
  39. #pragma alloc_text(PAGE,MiQueryAddressState)
  40. #pragma alloc_text(PAGE,MiGetWorkingSetInfo)
  41. #endif
  42. NTSTATUS
  43. NtQueryVirtualMemory (
  44. IN HANDLE ProcessHandle,
  45. IN PVOID BaseAddress,
  46. IN MEMORY_INFORMATION_CLASS MemoryInformationClass,
  47. OUT PVOID MemoryInformation,
  48. IN SIZE_T MemoryInformationLength,
  49. OUT PSIZE_T ReturnLength OPTIONAL
  50. )
  51. /*++
  52. Routine Description:
  53. This function provides the capability to determine the state,
  54. protection, and type of a region of pages within the virtual address
  55. space of the subject process.
  56. The state of the first page within the region is determined and then
  57. subsequent entries in the process address map are scanned from the
  58. base address upward until either the entire range of pages has been
  59. scanned or until a page with a nonmatching set of attributes is
  60. encountered. The region attributes, the length of the region of pages
  61. with matching attributes, and an appropriate status value are
  62. returned.
  63. If the entire region of pages does not have a matching set of
  64. attributes, then the returned length parameter value can be used to
  65. calculate the address and length of the region of pages that was not
  66. scanned.
  67. Arguments:
  68. ProcessHandle - An open handle to a process object.
  69. BaseAddress - The base address of the region of pages to be
  70. queried. This value is rounded down to the next host-page-
  71. address boundary.
  72. MemoryInformationClass - The memory information class about which
  73. to retrieve information.
  74. MemoryInformation - A pointer to a buffer that receives the specified
  75. information. The format and content of the buffer
  76. depend on the specified information class.
  77. MemoryBasicInformation - Data type is PMEMORY_BASIC_INFORMATION.
  78. MEMORY_BASIC_INFORMATION Structure
  79. ULONG RegionSize - The size of the region in bytes beginning at
  80. the base address in which all pages have
  81. identical attributes.
  82. ULONG State - The state of the pages within the region.
  83. State Values
  84. MEM_COMMIT - The state of the pages within the region
  85. is committed.
  86. MEM_FREE - The state of the pages within the region
  87. is free.
  88. MEM_RESERVE - The state of the pages within the
  89. region is reserved.
  90. ULONG Protect - The protection of the pages within the region.
  91. Protect Values
  92. PAGE_NOACCESS - No access to the region of pages is allowed.
  93. An attempt to read, write, or execute within
  94. the region results in an access violation.
  95. PAGE_EXECUTE - Execute access to the region of pages
  96. is allowed. An attempt to read or write within
  97. the region results in an access violation.
  98. PAGE_READONLY - Read-only and execute access to the region
  99. of pages is allowed. An attempt to write within
  100. the region results in an access violation.
  101. PAGE_READWRITE - Read, write, and execute access to the region
  102. of pages is allowed. If write access to the
  103. underlying section is allowed, then a single
  104. copy of the pages are shared. Otherwise,
  105. the pages are shared read-only/copy-on-write.
  106. PAGE_GUARD - Read, write, and execute access to the
  107. region of pages is allowed; however, access to
  108. the region causes a "guard region entered"
  109. condition to be raised in the subject process.
  110. PAGE_NOCACHE - Disable the placement of committed
  111. pages into the data cache.
  112. ULONG Type - The type of pages within the region.
  113. Type Values
  114. MEM_PRIVATE - The pages within the region are private.
  115. MEM_MAPPED - The pages within the region are mapped
  116. into the view of a section.
  117. MEM_IMAGE - The pages within the region are mapped
  118. into the view of an image section.
  119. MemoryInformationLength - Specifies the length in bytes of
  120. the memory information buffer.
  121. ReturnLength - An optional pointer which, if specified, receives the
  122. number of bytes placed in the process information buffer.
  123. Return Value:
  124. NTSTATUS.
  125. Environment:
  126. Kernel mode.
  127. --*/
  128. {
  129. KPROCESSOR_MODE PreviousMode;
  130. PEPROCESS TargetProcess;
  131. NTSTATUS Status;
  132. PMMVAD Vad;
  133. PVOID Va;
  134. PVOID NextVaToQuery;
  135. LOGICAL Found;
  136. SIZE_T TheRegionSize;
  137. ULONG NewProtect;
  138. ULONG NewState;
  139. PVOID FilePointer;
  140. ULONG_PTR BaseVpn;
  141. MEMORY_BASIC_INFORMATION Info;
  142. PMEMORY_BASIC_INFORMATION BasicInfo;
  143. LOGICAL Attached;
  144. LOGICAL Leaped;
  145. ULONG MemoryInformationLengthUlong;
  146. KAPC_STATE ApcState;
  147. PETHREAD CurrentThread;
  148. Found = FALSE;
  149. Leaped = TRUE;
  150. FilePointer = NULL;
  151. //
  152. // Make sure the user's buffer is large enough for the requested operation.
  153. //
  154. // Check argument validity.
  155. //
  156. switch (MemoryInformationClass) {
  157. case MemoryBasicInformation:
  158. if (MemoryInformationLength < sizeof(MEMORY_BASIC_INFORMATION)) {
  159. return STATUS_INFO_LENGTH_MISMATCH;
  160. }
  161. break;
  162. case MemoryWorkingSetInformation:
  163. if (MemoryInformationLength < sizeof(ULONG_PTR)) {
  164. return STATUS_INFO_LENGTH_MISMATCH;
  165. }
  166. break;
  167. case MemoryMappedFilenameInformation:
  168. break;
  169. default:
  170. return STATUS_INVALID_INFO_CLASS;
  171. }
  172. CurrentThread = PsGetCurrentThread ();
  173. PreviousMode = KeGetPreviousModeByThread(&CurrentThread->Tcb);
  174. if (PreviousMode != KernelMode) {
  175. //
  176. // Check arguments.
  177. //
  178. try {
  179. ProbeForWrite(MemoryInformation,
  180. MemoryInformationLength,
  181. sizeof(ULONG_PTR));
  182. if (ARGUMENT_PRESENT(ReturnLength)) {
  183. ProbeForWriteUlong_ptr(ReturnLength);
  184. }
  185. } except (EXCEPTION_EXECUTE_HANDLER) {
  186. //
  187. // If an exception occurs during the probe or capture
  188. // of the initial values, then handle the exception and
  189. // return the exception code as the status value.
  190. //
  191. return GetExceptionCode();
  192. }
  193. }
  194. if (BaseAddress > MM_HIGHEST_USER_ADDRESS) {
  195. return STATUS_INVALID_PARAMETER;
  196. }
  197. if ((BaseAddress >= MM_HIGHEST_VAD_ADDRESS)
  198. #if defined(MM_SHARED_USER_DATA_VA)
  199. ||
  200. (PAGE_ALIGN(BaseAddress) == (PVOID)MM_SHARED_USER_DATA_VA)
  201. #endif
  202. ) {
  203. //
  204. // Indicate a reserved area from this point on.
  205. //
  206. if (MemoryInformationClass == MemoryBasicInformation) {
  207. try {
  208. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->AllocationBase =
  209. (PCHAR) MM_HIGHEST_VAD_ADDRESS + 1;
  210. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->AllocationProtect =
  211. PAGE_READONLY;
  212. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->BaseAddress =
  213. PAGE_ALIGN(BaseAddress);
  214. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->RegionSize =
  215. ((PCHAR)MM_HIGHEST_USER_ADDRESS + 1) -
  216. (PCHAR)PAGE_ALIGN(BaseAddress);
  217. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->State = MEM_RESERVE;
  218. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->Protect = PAGE_NOACCESS;
  219. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->Type = MEM_PRIVATE;
  220. if (ARGUMENT_PRESENT(ReturnLength)) {
  221. *ReturnLength = sizeof(MEMORY_BASIC_INFORMATION);
  222. }
  223. #if defined(MM_SHARED_USER_DATA_VA)
  224. if (PAGE_ALIGN(BaseAddress) == (PVOID)MM_SHARED_USER_DATA_VA) {
  225. //
  226. // This is the page that is double mapped between
  227. // user mode and kernel mode.
  228. //
  229. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->AllocationBase =
  230. (PVOID)MM_SHARED_USER_DATA_VA;
  231. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->Protect =
  232. PAGE_READONLY;
  233. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->RegionSize =
  234. PAGE_SIZE;
  235. ((PMEMORY_BASIC_INFORMATION)MemoryInformation)->State =
  236. MEM_COMMIT;
  237. }
  238. #endif
  239. } except (EXCEPTION_EXECUTE_HANDLER) {
  240. //
  241. // Just return success.
  242. //
  243. }
  244. return STATUS_SUCCESS;
  245. }
  246. else {
  247. return STATUS_INVALID_ADDRESS;
  248. }
  249. }
  250. if (ProcessHandle == NtCurrentProcess()) {
  251. TargetProcess = PsGetCurrentProcessByThread(CurrentThread);
  252. }
  253. else {
  254. Status = ObReferenceObjectByHandle (ProcessHandle,
  255. PROCESS_QUERY_INFORMATION,
  256. PsProcessType,
  257. PreviousMode,
  258. (PVOID *)&TargetProcess,
  259. NULL);
  260. if (!NT_SUCCESS(Status)) {
  261. return Status;
  262. }
  263. }
  264. if (MemoryInformationClass == MemoryWorkingSetInformation) {
  265. Status = MiGetWorkingSetInfo (
  266. (PMEMORY_WORKING_SET_INFORMATION) MemoryInformation,
  267. MemoryInformationLength,
  268. TargetProcess);
  269. if (ProcessHandle != NtCurrentProcess()) {
  270. ObDereferenceObject (TargetProcess);
  271. }
  272. //
  273. // If MiGetWorkingSetInfo failed then inform the caller.
  274. //
  275. if (!NT_SUCCESS(Status)) {
  276. return Status;
  277. }
  278. try {
  279. if (ARGUMENT_PRESENT(ReturnLength)) {
  280. *ReturnLength = ((((PMEMORY_WORKING_SET_INFORMATION)
  281. MemoryInformation)->NumberOfEntries - 1) *
  282. sizeof(ULONG)) +
  283. sizeof(MEMORY_WORKING_SET_INFORMATION);
  284. }
  285. } except (EXCEPTION_EXECUTE_HANDLER) {
  286. }
  287. return STATUS_SUCCESS;
  288. }
  289. //
  290. // If the specified process is not the current process, attach
  291. // to the specified process.
  292. //
  293. if (ProcessHandle != NtCurrentProcess()) {
  294. KeStackAttachProcess (&TargetProcess->Pcb, &ApcState);
  295. Attached = TRUE;
  296. }
  297. else {
  298. Attached = FALSE;
  299. }
  300. //
  301. // Get working set mutex and block APCs.
  302. //
  303. LOCK_ADDRESS_SPACE (TargetProcess);
  304. //
  305. // Make sure the address space was not deleted, if so, return an error.
  306. //
  307. if (TargetProcess->Flags & PS_PROCESS_FLAGS_VM_DELETED) {
  308. UNLOCK_ADDRESS_SPACE (TargetProcess);
  309. if (Attached == TRUE) {
  310. KeUnstackDetachProcess (&ApcState);
  311. ObDereferenceObject (TargetProcess);
  312. }
  313. return STATUS_PROCESS_IS_TERMINATING;
  314. }
  315. //
  316. // Locate the VAD that contains the base address or the VAD
  317. // which follows the base address.
  318. //
  319. Vad = TargetProcess->VadRoot;
  320. BaseVpn = MI_VA_TO_VPN (BaseAddress);
  321. for (;;) {
  322. if (Vad == NULL) {
  323. break;
  324. }
  325. if ((BaseVpn >= Vad->StartingVpn) &&
  326. (BaseVpn <= Vad->EndingVpn)) {
  327. Found = TRUE;
  328. break;
  329. }
  330. if (BaseVpn < Vad->StartingVpn) {
  331. if (Vad->LeftChild == NULL) {
  332. break;
  333. }
  334. Vad = Vad->LeftChild;
  335. }
  336. else {
  337. if (BaseVpn < Vad->EndingVpn) {
  338. break;
  339. }
  340. if (Vad->RightChild == NULL) {
  341. break;
  342. }
  343. Vad = Vad->RightChild;
  344. }
  345. }
  346. if (!Found) {
  347. //
  348. // There is no virtual address allocated at the base
  349. // address. Return the size of the hole starting at
  350. // the base address.
  351. //
  352. if (Vad == NULL) {
  353. TheRegionSize = ((PCHAR)MM_HIGHEST_VAD_ADDRESS + 1) -
  354. (PCHAR)PAGE_ALIGN(BaseAddress);
  355. }
  356. else {
  357. if (Vad->StartingVpn < BaseVpn) {
  358. //
  359. // We are looking at the Vad which occupies the range
  360. // just before the desired range. Get the next Vad.
  361. //
  362. Vad = MiGetNextVad (Vad);
  363. if (Vad == NULL) {
  364. TheRegionSize = ((PCHAR)MM_HIGHEST_VAD_ADDRESS + 1) -
  365. (PCHAR)PAGE_ALIGN(BaseAddress);
  366. }
  367. else {
  368. TheRegionSize = (PCHAR)MI_VPN_TO_VA (Vad->StartingVpn) -
  369. (PCHAR)PAGE_ALIGN(BaseAddress);
  370. }
  371. }
  372. else {
  373. TheRegionSize = (PCHAR)MI_VPN_TO_VA (Vad->StartingVpn) -
  374. (PCHAR)PAGE_ALIGN(BaseAddress);
  375. }
  376. }
  377. UNLOCK_ADDRESS_SPACE (TargetProcess);
  378. if (Attached == TRUE) {
  379. KeUnstackDetachProcess (&ApcState);
  380. ObDereferenceObject (TargetProcess);
  381. }
  382. //
  383. // Establish an exception handler and write the information and
  384. // returned length.
  385. //
  386. if (MemoryInformationClass == MemoryBasicInformation) {
  387. BasicInfo = (PMEMORY_BASIC_INFORMATION) MemoryInformation;
  388. Found = FALSE;
  389. try {
  390. BasicInfo->AllocationBase = NULL;
  391. BasicInfo->AllocationProtect = 0;
  392. BasicInfo->BaseAddress = PAGE_ALIGN(BaseAddress);
  393. BasicInfo->RegionSize = TheRegionSize;
  394. BasicInfo->State = MEM_FREE;
  395. BasicInfo->Protect = PAGE_NOACCESS;
  396. BasicInfo->Type = 0;
  397. Found = TRUE;
  398. if (ARGUMENT_PRESENT(ReturnLength)) {
  399. *ReturnLength = sizeof(MEMORY_BASIC_INFORMATION);
  400. }
  401. } except (EXCEPTION_EXECUTE_HANDLER) {
  402. //
  403. // Just return success if the BasicInfo was successfully
  404. // filled in.
  405. //
  406. if (Found == FALSE) {
  407. return GetExceptionCode ();
  408. }
  409. }
  410. return STATUS_SUCCESS;
  411. }
  412. return STATUS_INVALID_ADDRESS;
  413. }
  414. //
  415. // Found a VAD.
  416. //
  417. Va = PAGE_ALIGN(BaseAddress);
  418. Info.BaseAddress = Va;
  419. //
  420. // There is a page mapped at the base address.
  421. //
  422. if (Vad->u.VadFlags.PrivateMemory) {
  423. Info.Type = MEM_PRIVATE;
  424. }
  425. else {
  426. if (Vad->u.VadFlags.ImageMap == 1) {
  427. Info.Type = MEM_IMAGE;
  428. }
  429. else {
  430. Info.Type = MEM_MAPPED;
  431. }
  432. if (MemoryInformationClass == MemoryMappedFilenameInformation) {
  433. if (Vad->ControlArea) {
  434. FilePointer = Vad->ControlArea->FilePointer;
  435. }
  436. if (FilePointer == NULL) {
  437. FilePointer = (PVOID)1;
  438. }
  439. else {
  440. ObReferenceObject(FilePointer);
  441. }
  442. }
  443. }
  444. LOCK_WS_UNSAFE (TargetProcess);
  445. Info.State = MiQueryAddressState (Va,
  446. Vad,
  447. TargetProcess,
  448. &Info.Protect,
  449. &NextVaToQuery);
  450. Va = NextVaToQuery;
  451. while (MI_VA_TO_VPN (Va) <= Vad->EndingVpn) {
  452. NewState = MiQueryAddressState (Va,
  453. Vad,
  454. TargetProcess,
  455. &NewProtect,
  456. &NextVaToQuery);
  457. if ((NewState != Info.State) || (NewProtect != Info.Protect)) {
  458. //
  459. // The state for this address does not match, calculate
  460. // size and return.
  461. //
  462. Leaped = FALSE;
  463. break;
  464. }
  465. Va = NextVaToQuery;
  466. }
  467. UNLOCK_WS_UNSAFE (TargetProcess);
  468. //
  469. // We may have aggressively leaped past the end of the VAD. Shorten the
  470. // Va here if we did.
  471. //
  472. if (Leaped == TRUE) {
  473. Va = MI_VPN_TO_VA (Vad->EndingVpn + 1);
  474. }
  475. Info.RegionSize = ((PCHAR)Va - (PCHAR)Info.BaseAddress);
  476. Info.AllocationBase = MI_VPN_TO_VA (Vad->StartingVpn);
  477. Info.AllocationProtect = MI_CONVERT_FROM_PTE_PROTECTION (
  478. Vad->u.VadFlags.Protection);
  479. //
  480. // A range has been found, release the mutexes, detach from the
  481. // target process and return the information.
  482. //
  483. #if defined(_MIALT4K_)
  484. if (TargetProcess->Wow64Process != NULL) {
  485. Info.BaseAddress = PAGE_4K_ALIGN(BaseAddress);
  486. MiQueryRegionFor4kPage (Info.BaseAddress,
  487. MI_VPN_TO_VA_ENDING(Vad->EndingVpn),
  488. &Info.RegionSize,
  489. &Info.State,
  490. &Info.Protect,
  491. TargetProcess);
  492. }
  493. #endif
  494. UNLOCK_ADDRESS_SPACE (TargetProcess);
  495. if (Attached == TRUE) {
  496. KeUnstackDetachProcess (&ApcState);
  497. ObDereferenceObject (TargetProcess);
  498. }
  499. if (MemoryInformationClass == MemoryBasicInformation) {
  500. Found = FALSE;
  501. try {
  502. *(PMEMORY_BASIC_INFORMATION)MemoryInformation = Info;
  503. Found = TRUE;
  504. if (ARGUMENT_PRESENT(ReturnLength)) {
  505. *ReturnLength = sizeof(MEMORY_BASIC_INFORMATION);
  506. }
  507. } except (EXCEPTION_EXECUTE_HANDLER) {
  508. //
  509. // Just return success if the BasicInfo was successfully
  510. // filled in.
  511. //
  512. if (Found == FALSE) {
  513. return GetExceptionCode ();
  514. }
  515. }
  516. return STATUS_SUCCESS;
  517. }
  518. //
  519. // Try to return the name of the file that is mapped.
  520. //
  521. if (FilePointer == NULL) {
  522. return STATUS_INVALID_ADDRESS;
  523. }
  524. if (FilePointer == (PVOID)1) {
  525. return STATUS_FILE_INVALID;
  526. }
  527. MemoryInformationLengthUlong = (ULONG)MemoryInformationLength;
  528. if ((SIZE_T)MemoryInformationLengthUlong < MemoryInformationLength) {
  529. return STATUS_INVALID_PARAMETER_5;
  530. }
  531. //
  532. // We have a referenced pointer to the file. Call ObQueryNameString
  533. // and get the file name.
  534. //
  535. Status = ObQueryNameString (FilePointer,
  536. (POBJECT_NAME_INFORMATION) MemoryInformation,
  537. MemoryInformationLengthUlong,
  538. (PULONG)ReturnLength);
  539. ObDereferenceObject (FilePointer);
  540. return Status;
  541. }
  542. ULONG
  543. MiQueryAddressState (
  544. IN PVOID Va,
  545. IN PMMVAD Vad,
  546. IN PEPROCESS TargetProcess,
  547. OUT PULONG ReturnedProtect,
  548. OUT PVOID *NextVaToQuery
  549. )
  550. /*++
  551. Routine Description:
  552. Arguments:
  553. Return Value:
  554. Returns the state (MEM_COMMIT, MEM_RESERVE, MEM_PRIVATE).
  555. Environment:
  556. Kernel mode. Working set lock and address creation lock held.
  557. --*/
  558. {
  559. PMMPTE PointerPte;
  560. PMMPTE PointerPde;
  561. PMMPTE PointerPpe;
  562. PMMPTE PointerPxe;
  563. MMPTE CapturedProtoPte;
  564. PMMPTE ProtoPte;
  565. LOGICAL PteIsZero;
  566. ULONG State;
  567. ULONG Protect;
  568. ULONG Waited;
  569. LOGICAL PteDetected;
  570. PVOID NextVa;
  571. State = MEM_RESERVE;
  572. Protect = 0;
  573. #ifdef LARGE_PAGES
  574. if (Vad->u.VadFlags.LargePages) {
  575. *ReturnedProtect = MI_CONVERT_FROM_PTE_PROTECTION (
  576. Vad->u.VadFlags.Protection);
  577. return MEM_COMMIT;
  578. }
  579. #endif //LARGE_PAGES
  580. PointerPxe = MiGetPxeAddress (Va);
  581. PointerPpe = MiGetPpeAddress (Va);
  582. PointerPde = MiGetPdeAddress (Va);
  583. PointerPte = MiGetPteAddress (Va);
  584. ASSERT ((Vad->StartingVpn <= MI_VA_TO_VPN (Va)) &&
  585. (Vad->EndingVpn >= MI_VA_TO_VPN (Va)));
  586. PteIsZero = TRUE;
  587. PteDetected = FALSE;
  588. *NextVaToQuery = (PVOID)((PCHAR)Va + PAGE_SIZE);
  589. do {
  590. if (!MiDoesPxeExistAndMakeValid (PointerPxe,
  591. TargetProcess,
  592. FALSE,
  593. &Waited)) {
  594. #if (_MI_PAGING_LEVELS >= 4)
  595. NextVa = MiGetVirtualAddressMappedByPte (PointerPxe + 1);
  596. NextVa = MiGetVirtualAddressMappedByPte (NextVa);
  597. NextVa = MiGetVirtualAddressMappedByPte (NextVa);
  598. *NextVaToQuery = MiGetVirtualAddressMappedByPte (NextVa);
  599. #endif
  600. break;
  601. }
  602. #if (_MI_PAGING_LEVELS >= 4)
  603. Waited = 0;
  604. #endif
  605. if (!MiDoesPpeExistAndMakeValid (PointerPpe,
  606. TargetProcess,
  607. FALSE,
  608. &Waited)) {
  609. #if (_MI_PAGING_LEVELS >= 3)
  610. NextVa = MiGetVirtualAddressMappedByPte (PointerPpe + 1);
  611. NextVa = MiGetVirtualAddressMappedByPte (NextVa);
  612. *NextVaToQuery = MiGetVirtualAddressMappedByPte (NextVa);
  613. #endif
  614. break;
  615. }
  616. #if (_MI_PAGING_LEVELS < 4)
  617. Waited = 0;
  618. #endif
  619. if (!MiDoesPdeExistAndMakeValid (PointerPde,
  620. TargetProcess,
  621. FALSE,
  622. &Waited)) {
  623. NextVa = MiGetVirtualAddressMappedByPte (PointerPde + 1);
  624. *NextVaToQuery = MiGetVirtualAddressMappedByPte (NextVa);
  625. break;
  626. }
  627. if (Waited == 0) {
  628. PteDetected = TRUE;
  629. }
  630. } while (Waited != 0);
  631. if (PteDetected == TRUE) {
  632. //
  633. // A PTE exists at this address, see if it is zero.
  634. //
  635. if (PointerPte->u.Long != 0) {
  636. PteIsZero = FALSE;
  637. //
  638. // There is a non-zero PTE at this address, use
  639. // it to build the information block.
  640. //
  641. if (MiIsPteDecommittedPage (PointerPte)) {
  642. ASSERT (Protect == 0);
  643. ASSERT (State == MEM_RESERVE);
  644. }
  645. else {
  646. State = MEM_COMMIT;
  647. if (Vad->u.VadFlags.PhysicalMapping == 1) {
  648. //
  649. // Physical mapping, there is no corresponding
  650. // PFN element to get the page protection from.
  651. //
  652. Protect = MI_CONVERT_FROM_PTE_PROTECTION (
  653. Vad->u.VadFlags.Protection);
  654. }
  655. else {
  656. Protect = MiGetPageProtection (PointerPte,
  657. TargetProcess,
  658. FALSE);
  659. if ((PointerPte->u.Soft.Valid == 0) &&
  660. (PointerPte->u.Soft.Prototype == 1) &&
  661. (Vad->u.VadFlags.PrivateMemory == 0) &&
  662. (Vad->ControlArea != (PCONTROL_AREA)NULL)) {
  663. //
  664. // Make sure the protoPTE is committed.
  665. //
  666. ProtoPte = MiGetProtoPteAddress(Vad,
  667. MI_VA_TO_VPN (Va));
  668. CapturedProtoPte.u.Long = 0;
  669. if (ProtoPte) {
  670. CapturedProtoPte = MiCaptureSystemPte (ProtoPte,
  671. TargetProcess);
  672. }
  673. if (CapturedProtoPte.u.Long == 0) {
  674. State = MEM_RESERVE;
  675. Protect = 0;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. if (PteIsZero) {
  683. //
  684. // There is no PDE at this address, the template from
  685. // the VAD supplies the information unless the VAD is
  686. // for an image file. For image files the individual
  687. // protection is on the prototype PTE.
  688. //
  689. //
  690. // Get the default protection information.
  691. //
  692. State = MEM_RESERVE;
  693. Protect = 0;
  694. if (Vad->u.VadFlags.PhysicalMapping == 1) {
  695. //
  696. // Must be banked memory, just return reserved.
  697. //
  698. NOTHING;
  699. } else if ((Vad->u.VadFlags.PrivateMemory == 0) &&
  700. (Vad->ControlArea != (PCONTROL_AREA)NULL)) {
  701. //
  702. // This VAD refers to a section. Even though the PTE is
  703. // zero, the actual page may be committed in the section.
  704. //
  705. *NextVaToQuery = (PVOID)((PCHAR)Va + PAGE_SIZE);
  706. ProtoPte = MiGetProtoPteAddress(Vad, MI_VA_TO_VPN (Va));
  707. CapturedProtoPte.u.Long = 0;
  708. if (ProtoPte) {
  709. CapturedProtoPte = MiCaptureSystemPte (ProtoPte,
  710. TargetProcess);
  711. }
  712. if (CapturedProtoPte.u.Long != 0) {
  713. State = MEM_COMMIT;
  714. if (Vad->u.VadFlags.ImageMap == 0) {
  715. Protect = MI_CONVERT_FROM_PTE_PROTECTION (
  716. Vad->u.VadFlags.Protection);
  717. }
  718. else {
  719. //
  720. // This is an image file, the protection is in the
  721. // prototype PTE.
  722. //
  723. Protect = MiGetPageProtection (&CapturedProtoPte,
  724. TargetProcess,
  725. TRUE);
  726. }
  727. }
  728. }
  729. else {
  730. //
  731. // Get the protection from the corresponding VAD.
  732. //
  733. if (Vad->u.VadFlags.MemCommit) {
  734. State = MEM_COMMIT;
  735. Protect = MI_CONVERT_FROM_PTE_PROTECTION (
  736. Vad->u.VadFlags.Protection);
  737. }
  738. }
  739. }
  740. *ReturnedProtect = Protect;
  741. return State;
  742. }
  743. NTSTATUS
  744. MiGetWorkingSetInfo (
  745. IN PMEMORY_WORKING_SET_INFORMATION WorkingSetInfo,
  746. IN SIZE_T Length,
  747. IN PEPROCESS Process
  748. )
  749. {
  750. PMDL Mdl;
  751. PMEMORY_WORKING_SET_INFORMATION Info;
  752. PMEMORY_WORKING_SET_BLOCK Entry;
  753. #if DBG
  754. PMEMORY_WORKING_SET_BLOCK LastEntry;
  755. #endif
  756. PMMWSLE Wsle;
  757. PMMWSLE LastWsle;
  758. WSLE_NUMBER WsSize;
  759. PMMPTE PointerPte;
  760. PMMPFN Pfn1;
  761. NTSTATUS status;
  762. LOGICAL Attached;
  763. KAPC_STATE ApcState;
  764. PETHREAD CurrentThread;
  765. //
  766. // Allocate an MDL to map the request.
  767. //
  768. Mdl = ExAllocatePoolWithTag (NonPagedPool,
  769. sizeof(MDL) + sizeof(PFN_NUMBER) +
  770. BYTES_TO_PAGES (Length) * sizeof(PFN_NUMBER),
  771. ' mM');
  772. if (Mdl == NULL) {
  773. return STATUS_INSUFFICIENT_RESOURCES;
  774. }
  775. //
  776. // Initialize the MDL for the request.
  777. //
  778. MmInitializeMdl(Mdl, WorkingSetInfo, Length);
  779. CurrentThread = PsGetCurrentThread ();
  780. try {
  781. MmProbeAndLockPages (Mdl,
  782. KeGetPreviousModeByThread (&CurrentThread->Tcb),
  783. IoWriteAccess);
  784. } except (EXCEPTION_EXECUTE_HANDLER) {
  785. ExFreePool (Mdl);
  786. return GetExceptionCode();
  787. }
  788. Info = MmGetSystemAddressForMdlSafe (Mdl, NormalPagePriority);
  789. if (Info == NULL) {
  790. MmUnlockPages (Mdl);
  791. ExFreePool (Mdl);
  792. return STATUS_INSUFFICIENT_RESOURCES;
  793. }
  794. if (PsGetCurrentProcessByThread (CurrentThread) != Process) {
  795. KeStackAttachProcess (&Process->Pcb, &ApcState);
  796. Attached = TRUE;
  797. }
  798. else {
  799. Attached = FALSE;
  800. }
  801. status = STATUS_SUCCESS;
  802. LOCK_WS (Process);
  803. if (Process->Flags & PS_PROCESS_FLAGS_VM_DELETED) {
  804. status = STATUS_PROCESS_IS_TERMINATING;
  805. }
  806. else {
  807. WsSize = Process->Vm.WorkingSetSize;
  808. ASSERT (WsSize != 0);
  809. Info->NumberOfEntries = WsSize;
  810. if (sizeof(MEMORY_WORKING_SET_INFORMATION) + (WsSize-1) * sizeof(ULONG_PTR) > Length) {
  811. status = STATUS_INFO_LENGTH_MISMATCH;
  812. }
  813. }
  814. if (!NT_SUCCESS(status)) {
  815. UNLOCK_WS (Process);
  816. if (Attached == TRUE) {
  817. KeUnstackDetachProcess (&ApcState);
  818. }
  819. MmUnlockPages (Mdl);
  820. ExFreePool (Mdl);
  821. return status;
  822. }
  823. Wsle = MmWsle;
  824. LastWsle = &MmWsle[MmWorkingSetList->LastEntry];
  825. Entry = &Info->WorkingSetInfo[0];
  826. #if DBG
  827. LastEntry = (PMEMORY_WORKING_SET_BLOCK)(
  828. (PCHAR)Info + (Length & (~(sizeof(ULONG_PTR) - 1))));
  829. #endif
  830. do {
  831. if (Wsle->u1.e1.Valid == 1) {
  832. Entry->VirtualPage = Wsle->u1.e1.VirtualPageNumber;
  833. PointerPte = MiGetPteAddress (Wsle->u1.VirtualAddress);
  834. ASSERT (PointerPte->u.Hard.Valid == 1);
  835. Pfn1 = MI_PFN_ELEMENT (PointerPte->u.Hard.PageFrameNumber);
  836. #if defined(MI_MULTINODE)
  837. Entry->Node = Pfn1->u3.e1.PageColor;
  838. #else
  839. Entry->Node = 0;
  840. #endif
  841. Entry->Shared = Pfn1->u3.e1.PrototypePte;
  842. if (Pfn1->u3.e1.PrototypePte == 0) {
  843. Entry->ShareCount = 0;
  844. Entry->Protection = MI_GET_PROTECTION_FROM_SOFT_PTE(&Pfn1->OriginalPte);
  845. }
  846. else {
  847. if (Pfn1->u2.ShareCount <= 7) {
  848. Entry->ShareCount = Pfn1->u2.ShareCount;
  849. }
  850. else {
  851. Entry->ShareCount = 7;
  852. }
  853. if (Wsle->u1.e1.SameProtectAsProto == 1) {
  854. Entry->Protection = MI_GET_PROTECTION_FROM_SOFT_PTE(&Pfn1->OriginalPte);
  855. }
  856. else {
  857. Entry->Protection = Wsle->u1.e1.Protection;
  858. }
  859. }
  860. Entry += 1;
  861. }
  862. Wsle += 1;
  863. #if DBG
  864. ASSERT ((Entry < LastEntry) || (Wsle > LastWsle));
  865. #endif
  866. } while (Wsle <= LastWsle);
  867. UNLOCK_WS (Process);
  868. if (Attached == TRUE) {
  869. KeUnstackDetachProcess (&ApcState);
  870. }
  871. MmUnlockPages (Mdl);
  872. ExFreePool (Mdl);
  873. return STATUS_SUCCESS;
  874. }