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.

918 lines
23 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. vadtree.c
  5. Abstract:
  6. This module contains the routine to manipulate the virtual address
  7. descriptor tree.
  8. Author:
  9. Lou Perazzoli (loup) 19-May-1989
  10. Landy Wang (landyw) 02-June-1997
  11. Environment:
  12. Kernel mode only, working set mutex held, APCs disabled.
  13. Revision History:
  14. --*/
  15. #include "mi.h"
  16. VOID
  17. VadTreeWalk (
  18. VOID
  19. );
  20. #ifdef ALLOC_PRAGMA
  21. #pragma alloc_text(PAGE,MiInsertVad)
  22. #pragma alloc_text(PAGE,MiRemoveVad)
  23. #pragma alloc_text(PAGE,MiFindEmptyAddressRange)
  24. #pragma alloc_text(PAGE, MmPerfVadTreeWalk)
  25. #if DBG
  26. #pragma alloc_text(PAGE,VadTreeWalk)
  27. #endif
  28. #endif
  29. NTSTATUS
  30. MiInsertVad (
  31. IN PMMVAD Vad
  32. )
  33. /*++
  34. Routine Description:
  35. This function inserts a virtual address descriptor into the tree and
  36. reorders the splay tree as appropriate.
  37. Arguments:
  38. Vad - Supplies a pointer to a virtual address descriptor.
  39. Return Value:
  40. NTSTATUS.
  41. --*/
  42. {
  43. ULONG StartBit;
  44. ULONG EndBit;
  45. PMMADDRESS_NODE *Root;
  46. PEPROCESS CurrentProcess;
  47. SIZE_T RealCharge;
  48. SIZE_T PageCharge;
  49. SIZE_T PagesReallyCharged;
  50. ULONG FirstPage;
  51. ULONG LastPage;
  52. SIZE_T PagedPoolCharge;
  53. LOGICAL ChargedJobCommit;
  54. NTSTATUS Status;
  55. RTL_BITMAP VadBitMap;
  56. #if (_MI_PAGING_LEVELS >= 3)
  57. ULONG FirstPdPage;
  58. ULONG LastPdPage;
  59. #endif
  60. #if (_MI_PAGING_LEVELS >= 4)
  61. ULONG FirstPpPage;
  62. ULONG LastPpPage;
  63. #endif
  64. ASSERT (Vad->EndingVpn >= Vad->StartingVpn);
  65. CurrentProcess = PsGetCurrentProcess();
  66. //
  67. // Commit charge of MAX_COMMIT means don't charge quota.
  68. //
  69. if (Vad->u.VadFlags.CommitCharge != MM_MAX_COMMIT) {
  70. PageCharge = 0;
  71. PagedPoolCharge = 0;
  72. ChargedJobCommit = FALSE;
  73. //
  74. // Charge quota for the nonpaged pool for the VAD. This is
  75. // done here rather than by using ExAllocatePoolWithQuota
  76. // so the process object is not referenced by the quota charge.
  77. //
  78. Status = PsChargeProcessNonPagedPoolQuota (CurrentProcess, sizeof(MMVAD));
  79. if (!NT_SUCCESS(Status)) {
  80. return STATUS_COMMITMENT_LIMIT;
  81. }
  82. //
  83. // Charge quota for the prototype PTEs if this is a mapped view.
  84. //
  85. if ((Vad->u.VadFlags.PrivateMemory == 0) &&
  86. (Vad->ControlArea != NULL)) {
  87. PagedPoolCharge =
  88. (Vad->EndingVpn - Vad->StartingVpn + 1) << PTE_SHIFT;
  89. Status = PsChargeProcessPagedPoolQuota (CurrentProcess,
  90. PagedPoolCharge);
  91. if (!NT_SUCCESS(Status)) {
  92. PagedPoolCharge = 0;
  93. RealCharge = 0;
  94. goto Failed;
  95. }
  96. }
  97. //
  98. // Add in the charge for page table pages.
  99. //
  100. FirstPage = MiGetPdeIndex (MI_VPN_TO_VA (Vad->StartingVpn));
  101. LastPage = MiGetPdeIndex (MI_VPN_TO_VA (Vad->EndingVpn));
  102. while (FirstPage <= LastPage) {
  103. if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageTables,
  104. FirstPage)) {
  105. PageCharge += 1;
  106. }
  107. FirstPage += 1;
  108. }
  109. #if (_MI_PAGING_LEVELS >= 4)
  110. //
  111. // Add in the charge for page directory parent pages.
  112. //
  113. FirstPpPage = MiGetPxeIndex (MI_VPN_TO_VA (Vad->StartingVpn));
  114. LastPpPage = MiGetPxeIndex (MI_VPN_TO_VA (Vad->EndingVpn));
  115. while (FirstPpPage <= LastPpPage) {
  116. if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageDirectoryParents,
  117. FirstPpPage)) {
  118. PageCharge += 1;
  119. }
  120. FirstPpPage += 1;
  121. }
  122. #endif
  123. #if (_MI_PAGING_LEVELS >= 3)
  124. //
  125. // Add in the charge for page directory pages.
  126. //
  127. FirstPdPage = MiGetPpeIndex (MI_VPN_TO_VA (Vad->StartingVpn));
  128. LastPdPage = MiGetPpeIndex (MI_VPN_TO_VA (Vad->EndingVpn));
  129. while (FirstPdPage <= LastPdPage) {
  130. if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageDirectories,
  131. FirstPdPage)) {
  132. PageCharge += 1;
  133. }
  134. FirstPdPage += 1;
  135. }
  136. #endif
  137. RealCharge = Vad->u.VadFlags.CommitCharge + PageCharge;
  138. if (RealCharge != 0) {
  139. Status = PsChargeProcessPageFileQuota (CurrentProcess, RealCharge);
  140. if (!NT_SUCCESS (Status)) {
  141. RealCharge = 0;
  142. goto Failed;
  143. }
  144. if (CurrentProcess->CommitChargeLimit) {
  145. if (CurrentProcess->CommitCharge + RealCharge > CurrentProcess->CommitChargeLimit) {
  146. if (CurrentProcess->Job) {
  147. PsReportProcessMemoryLimitViolation ();
  148. }
  149. goto Failed;
  150. }
  151. }
  152. if (CurrentProcess->JobStatus & PS_JOB_STATUS_REPORT_COMMIT_CHANGES) {
  153. if (PsChangeJobMemoryUsage(RealCharge) == FALSE) {
  154. goto Failed;
  155. }
  156. ChargedJobCommit = TRUE;
  157. }
  158. if (MiChargeCommitment (RealCharge, CurrentProcess) == FALSE) {
  159. goto Failed;
  160. }
  161. CurrentProcess->CommitCharge += RealCharge;
  162. if (CurrentProcess->CommitCharge > CurrentProcess->CommitChargePeak) {
  163. CurrentProcess->CommitChargePeak = CurrentProcess->CommitCharge;
  164. }
  165. MI_INCREMENT_TOTAL_PROCESS_COMMIT (RealCharge);
  166. ASSERT (RealCharge == Vad->u.VadFlags.CommitCharge + PageCharge);
  167. MM_TRACK_COMMIT (MM_DBG_COMMIT_INSERT_VAD, Vad->u.VadFlags.CommitCharge);
  168. MM_TRACK_COMMIT (MM_DBG_COMMIT_INSERT_VAD_PT, PageCharge);
  169. }
  170. if (PageCharge != 0) {
  171. //
  172. // Since the commitment was successful, charge the page
  173. // table pages.
  174. //
  175. PagesReallyCharged = 0;
  176. FirstPage = MiGetPdeIndex (MI_VPN_TO_VA (Vad->StartingVpn));
  177. while (FirstPage <= LastPage) {
  178. if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageTables,
  179. FirstPage)) {
  180. MI_SET_BIT (MmWorkingSetList->CommittedPageTables,
  181. FirstPage);
  182. MmWorkingSetList->NumberOfCommittedPageTables += 1;
  183. ASSERT32 (MmWorkingSetList->NumberOfCommittedPageTables <
  184. PD_PER_SYSTEM * PDE_PER_PAGE);
  185. PagesReallyCharged += 1;
  186. }
  187. FirstPage += 1;
  188. }
  189. #if (_MI_PAGING_LEVELS >= 3)
  190. //
  191. // Charge the page directory pages.
  192. //
  193. FirstPdPage = MiGetPpeIndex (MI_VPN_TO_VA (Vad->StartingVpn));
  194. while (FirstPdPage <= LastPdPage) {
  195. if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageDirectories,
  196. FirstPdPage)) {
  197. MI_SET_BIT (MmWorkingSetList->CommittedPageDirectories,
  198. FirstPdPage);
  199. MmWorkingSetList->NumberOfCommittedPageDirectories += 1;
  200. ASSERT (MmWorkingSetList->NumberOfCommittedPageDirectories <
  201. PDE_PER_PAGE);
  202. PagesReallyCharged += 1;
  203. }
  204. FirstPdPage += 1;
  205. }
  206. #endif
  207. #if (_MI_PAGING_LEVELS >= 4)
  208. //
  209. // Charge the page directory parent pages.
  210. //
  211. FirstPpPage = MiGetPxeIndex (MI_VPN_TO_VA (Vad->StartingVpn));
  212. while (FirstPpPage <= LastPpPage) {
  213. if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageDirectoryParents,
  214. FirstPpPage)) {
  215. MI_SET_BIT (MmWorkingSetList->CommittedPageDirectoryParents,
  216. FirstPpPage);
  217. MmWorkingSetList->NumberOfCommittedPageDirectoryParents += 1;
  218. ASSERT (MmWorkingSetList->NumberOfCommittedPageDirectoryParents <
  219. PDE_PER_PAGE);
  220. PagesReallyCharged += 1;
  221. }
  222. FirstPpPage += 1;
  223. }
  224. #endif
  225. ASSERT (PageCharge == PagesReallyCharged);
  226. }
  227. }
  228. Root = (PMMADDRESS_NODE *)&CurrentProcess->VadRoot;
  229. //
  230. // Set the relevant fields in the Vad bitmap.
  231. //
  232. StartBit = (ULONG)(((ULONG_PTR) MI_64K_ALIGN (MI_VPN_TO_VA (Vad->StartingVpn))) / X64K);
  233. EndBit = (ULONG) (((ULONG_PTR) MI_64K_ALIGN (MI_VPN_TO_VA (Vad->EndingVpn))) / X64K);
  234. //
  235. // Initialize the bitmap inline for speed.
  236. //
  237. VadBitMap.SizeOfBitMap = MiLastVadBit + 1;
  238. VadBitMap.Buffer = VAD_BITMAP_SPACE;
  239. //
  240. // Note VADs like the PEB & TEB start on page (not 64K) boundaries so
  241. // for these, the relevant bits may already be set.
  242. //
  243. #if defined (_WIN64) || defined (_X86PAE_)
  244. if (EndBit > MiLastVadBit) {
  245. EndBit = MiLastVadBit;
  246. }
  247. //
  248. // Only the first (PAGE_SIZE*8*64K) of VA space on NT64 is bitmapped.
  249. //
  250. if (StartBit <= MiLastVadBit) {
  251. RtlSetBits (&VadBitMap, StartBit, EndBit - StartBit + 1);
  252. }
  253. #else
  254. RtlSetBits (&VadBitMap, StartBit, EndBit - StartBit + 1);
  255. #endif
  256. if (MmWorkingSetList->VadBitMapHint == StartBit) {
  257. MmWorkingSetList->VadBitMapHint = EndBit + 1;
  258. }
  259. //
  260. // Set the hint field in the process to this Vad.
  261. //
  262. CurrentProcess->VadHint = Vad;
  263. if (CurrentProcess->VadFreeHint != NULL) {
  264. if (((ULONG)((PMMVAD)CurrentProcess->VadFreeHint)->EndingVpn +
  265. MI_VA_TO_VPN (X64K)) >=
  266. Vad->StartingVpn) {
  267. CurrentProcess->VadFreeHint = Vad;
  268. }
  269. }
  270. MiInsertNode ((PMMADDRESS_NODE)Vad, Root);
  271. CurrentProcess->NumberOfVads += 1;
  272. return STATUS_SUCCESS;
  273. Failed:
  274. //
  275. // Return any quotas charged thus far.
  276. //
  277. PsReturnProcessNonPagedPoolQuota (CurrentProcess, sizeof(MMVAD));
  278. if (PagedPoolCharge != 0) {
  279. PsReturnProcessPagedPoolQuota (CurrentProcess, PagedPoolCharge);
  280. }
  281. if (RealCharge != 0) {
  282. PsReturnProcessPageFileQuota (CurrentProcess, RealCharge);
  283. }
  284. if (ChargedJobCommit == TRUE) {
  285. PsChangeJobMemoryUsage(-(SSIZE_T)RealCharge);
  286. }
  287. return STATUS_COMMITMENT_LIMIT;
  288. }
  289. VOID
  290. MiRemoveVad (
  291. IN PMMVAD Vad
  292. )
  293. /*++
  294. Routine Description:
  295. This function removes a virtual address descriptor from the tree and
  296. reorders the splay tree as appropriate. If any quota or commitment
  297. was charged by the VAD (as indicated by the CommitCharge field) it
  298. is released.
  299. Arguments:
  300. Vad - Supplies a pointer to a virtual address descriptor.
  301. Return Value:
  302. None.
  303. --*/
  304. {
  305. PMMADDRESS_NODE *Root;
  306. PEPROCESS CurrentProcess;
  307. SIZE_T RealCharge;
  308. PLIST_ENTRY Next;
  309. PMMSECURE_ENTRY Entry;
  310. CurrentProcess = PsGetCurrentProcess();
  311. #if defined(_MIALT4K_)
  312. if (((Vad->u.VadFlags.PrivateMemory) && (Vad->u.VadFlags.NoChange == 0))
  313. ||
  314. (Vad->u2.VadFlags2.LongVad == 0)) {
  315. NOTHING;
  316. }
  317. else {
  318. ASSERT ((((PMMVAD_LONG)Vad)->AliasInformation == NULL) || (CurrentProcess->Wow64Process != NULL));
  319. }
  320. #endif
  321. //
  322. // Commit charge of MAX_COMMIT means don't charge quota.
  323. //
  324. if (Vad->u.VadFlags.CommitCharge != MM_MAX_COMMIT) {
  325. //
  326. // Return the quota charge to the process.
  327. //
  328. PsReturnProcessNonPagedPoolQuota (CurrentProcess, sizeof(MMVAD));
  329. if ((Vad->u.VadFlags.PrivateMemory == 0) &&
  330. (Vad->ControlArea != NULL)) {
  331. PsReturnProcessPagedPoolQuota (CurrentProcess,
  332. (Vad->EndingVpn - Vad->StartingVpn + 1) << PTE_SHIFT);
  333. }
  334. RealCharge = Vad->u.VadFlags.CommitCharge;
  335. if (RealCharge != 0) {
  336. PsReturnProcessPageFileQuota (CurrentProcess, RealCharge);
  337. if ((Vad->u.VadFlags.PrivateMemory == 0) &&
  338. (Vad->ControlArea != NULL)) {
  339. #if 0 //commented out so page file quota is meaningful.
  340. if (Vad->ControlArea->FilePointer == NULL) {
  341. //
  342. // Don't release commitment for the page file space
  343. // occupied by a page file section. This will be charged
  344. // as the shared memory is committed.
  345. //
  346. RealCharge -= BYTES_TO_PAGES ((ULONG)Vad->EndingVa -
  347. (ULONG)Vad->StartingVa);
  348. }
  349. #endif
  350. }
  351. MiReturnCommitment (RealCharge);
  352. MM_TRACK_COMMIT (MM_DBG_COMMIT_RETURN_VAD, RealCharge);
  353. if (CurrentProcess->JobStatus & PS_JOB_STATUS_REPORT_COMMIT_CHANGES) {
  354. PsChangeJobMemoryUsage(-(SSIZE_T)RealCharge);
  355. }
  356. CurrentProcess->CommitCharge -= RealCharge;
  357. MI_INCREMENT_TOTAL_PROCESS_COMMIT (0 - RealCharge);
  358. }
  359. }
  360. if (Vad == CurrentProcess->VadFreeHint) {
  361. CurrentProcess->VadFreeHint = MiGetPreviousVad (Vad);
  362. }
  363. Root = (PMMADDRESS_NODE *)&CurrentProcess->VadRoot;
  364. MiRemoveNode ( (PMMADDRESS_NODE)Vad, Root);
  365. ASSERT (CurrentProcess->NumberOfVads >= 1);
  366. CurrentProcess->NumberOfVads -= 1;
  367. if (Vad->u.VadFlags.NoChange) {
  368. if (Vad->u2.VadFlags2.MultipleSecured) {
  369. //
  370. // Free the oustanding pool allocations.
  371. //
  372. Next = ((PMMVAD_LONG) Vad)->u3.List.Flink;
  373. do {
  374. Entry = CONTAINING_RECORD( Next,
  375. MMSECURE_ENTRY,
  376. List);
  377. Next = Entry->List.Flink;
  378. ExFreePool (Entry);
  379. } while (Next != &((PMMVAD_LONG)Vad)->u3.List);
  380. }
  381. }
  382. //
  383. // If the VadHint was the removed Vad, change the Hint.
  384. if (CurrentProcess->VadHint == Vad) {
  385. CurrentProcess->VadHint = CurrentProcess->VadRoot;
  386. }
  387. return;
  388. }
  389. PMMVAD
  390. FASTCALL
  391. MiLocateAddress (
  392. IN PVOID VirtualAddress
  393. )
  394. /*++
  395. Routine Description:
  396. The function locates the virtual address descriptor which describes
  397. a given address.
  398. Arguments:
  399. VirtualAddress - Supplies the virtual address to locate a descriptor
  400. for.
  401. Return Value:
  402. Returns a pointer to the virtual address descriptor which contains
  403. the supplied virtual address or NULL if none was located.
  404. --*/
  405. {
  406. PMMVAD FoundVad;
  407. PEPROCESS CurrentProcess;
  408. ULONG_PTR Vpn;
  409. CurrentProcess = PsGetCurrentProcess();
  410. if (CurrentProcess->VadHint == NULL) {
  411. return NULL;
  412. }
  413. Vpn = MI_VA_TO_VPN (VirtualAddress);
  414. if ((Vpn >= ((PMMADDRESS_NODE)CurrentProcess->VadHint)->StartingVpn) &&
  415. (Vpn <= ((PMMADDRESS_NODE)CurrentProcess->VadHint)->EndingVpn)) {
  416. return (PMMVAD)CurrentProcess->VadHint;
  417. }
  418. FoundVad = (PMMVAD)MiLocateAddressInTree ( Vpn,
  419. (PMMADDRESS_NODE *)&(CurrentProcess->VadRoot));
  420. if (FoundVad != NULL) {
  421. CurrentProcess->VadHint = (PVOID)FoundVad;
  422. }
  423. return FoundVad;
  424. }
  425. NTSTATUS
  426. MiFindEmptyAddressRange (
  427. IN SIZE_T SizeOfRange,
  428. IN ULONG_PTR Alignment,
  429. IN ULONG QuickCheck,
  430. IN PVOID *Base
  431. )
  432. /*++
  433. Routine Description:
  434. The function examines the virtual address descriptors to locate
  435. an unused range of the specified size and returns the starting
  436. address of the range.
  437. Arguments:
  438. SizeOfRange - Supplies the size in bytes of the range to locate.
  439. Alignment - Supplies the alignment for the address. Must be
  440. a power of 2 and greater than the page_size.
  441. QuickCheck - Supplies a zero if a quick check for free memory
  442. after the VadFreeHint exists, non-zero if checking
  443. should start at the lowest address.
  444. Base - Receives the starting address of a suitable range on success.
  445. Return Value:
  446. NTSTATUS.
  447. --*/
  448. {
  449. ULONG FirstBitValue;
  450. ULONG StartPosition;
  451. ULONG BitsNeeded;
  452. PMMVAD NextVad;
  453. PMMVAD FreeHint;
  454. PEPROCESS CurrentProcess;
  455. PVOID StartingVa;
  456. PVOID EndingVa;
  457. NTSTATUS Status;
  458. RTL_BITMAP VadBitMap;
  459. CurrentProcess = PsGetCurrentProcess();
  460. if (QuickCheck == 0) {
  461. //
  462. // Initialize the bitmap inline for speed.
  463. //
  464. VadBitMap.SizeOfBitMap = MiLastVadBit + 1;
  465. VadBitMap.Buffer = VAD_BITMAP_SPACE;
  466. //
  467. // Skip the first bit here as we don't generally recommend
  468. // that applications map virtual address zero.
  469. //
  470. FirstBitValue = *((PULONG)VAD_BITMAP_SPACE);
  471. *((PULONG)VAD_BITMAP_SPACE) = (FirstBitValue | 0x1);
  472. BitsNeeded = (ULONG) ((MI_ROUND_TO_64K (SizeOfRange)) / X64K);
  473. StartPosition = RtlFindClearBits (&VadBitMap,
  474. BitsNeeded,
  475. MmWorkingSetList->VadBitMapHint);
  476. if (FirstBitValue & 0x1) {
  477. FirstBitValue = (ULONG)-1;
  478. }
  479. else {
  480. FirstBitValue = (ULONG)~0x1;
  481. }
  482. *((PULONG)VAD_BITMAP_SPACE) &= FirstBitValue;
  483. if (StartPosition != NO_BITS_FOUND) {
  484. *Base = (PVOID) (((ULONG_PTR)StartPosition) * X64K);
  485. #if DBG
  486. if (MiCheckForConflictingVad (CurrentProcess, *Base, (ULONG_PTR)*Base + SizeOfRange - 1) != NULL) {
  487. DbgPrint ("MiFindEmptyAddressRange: overlapping VAD %p %p\n", *Base, SizeOfRange);
  488. DbgBreakPoint ();
  489. }
  490. #endif
  491. return STATUS_SUCCESS;
  492. }
  493. FreeHint = CurrentProcess->VadFreeHint;
  494. if (FreeHint != NULL) {
  495. EndingVa = MI_VPN_TO_VA_ENDING (FreeHint->EndingVpn);
  496. NextVad = MiGetNextVad (FreeHint);
  497. if (NextVad == NULL) {
  498. if (SizeOfRange <
  499. (((ULONG_PTR)MM_HIGHEST_USER_ADDRESS + 1) -
  500. MI_ROUND_TO_SIZE((ULONG_PTR)EndingVa, Alignment))) {
  501. *Base = (PVOID) MI_ROUND_TO_SIZE((ULONG_PTR)EndingVa,
  502. Alignment);
  503. return STATUS_SUCCESS;
  504. }
  505. }
  506. else {
  507. StartingVa = MI_VPN_TO_VA (NextVad->StartingVpn);
  508. if (SizeOfRange <
  509. ((ULONG_PTR)StartingVa -
  510. MI_ROUND_TO_SIZE((ULONG_PTR)EndingVa, Alignment))) {
  511. //
  512. // Check to ensure that the ending address aligned upwards
  513. // is not greater than the starting address.
  514. //
  515. if ((ULONG_PTR)StartingVa >
  516. MI_ROUND_TO_SIZE((ULONG_PTR)EndingVa,Alignment)) {
  517. *Base = (PVOID)MI_ROUND_TO_SIZE((ULONG_PTR)EndingVa,
  518. Alignment);
  519. return STATUS_SUCCESS;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. Status = MiFindEmptyAddressRangeInTree (
  526. SizeOfRange,
  527. Alignment,
  528. (PMMADDRESS_NODE)(CurrentProcess->VadRoot),
  529. (PMMADDRESS_NODE *)&CurrentProcess->VadFreeHint,
  530. Base);
  531. return Status;
  532. }
  533. #if DBG
  534. VOID
  535. VadTreeWalk (
  536. VOID
  537. )
  538. {
  539. NodeTreeWalk ( (PMMADDRESS_NODE)(PsGetCurrentProcess()->VadRoot));
  540. return;
  541. }
  542. #endif
  543. LOGICAL
  544. MiCheckForConflictingVadExistence (
  545. IN PEPROCESS Process,
  546. IN PVOID StartingAddress,
  547. IN PVOID EndingAddress
  548. )
  549. /*++
  550. Routine Description:
  551. The function determines if any addresses between a given starting and
  552. ending address is contained within a virtual address descriptor.
  553. Arguments:
  554. StartingAddress - Supplies the virtual address to locate a containing
  555. descriptor.
  556. EndingAddress - Supplies the virtual address to locate a containing
  557. descriptor.
  558. Return Value:
  559. TRUE if the VAD if found, FALSE if not.
  560. Environment:
  561. Kernel mode, process address creation mutex held.
  562. --*/
  563. {
  564. #if 0
  565. ULONG StartBit;
  566. ULONG EndBit;
  567. if (MiLastVadBit != 0) {
  568. StartBit = (ULONG) (((ULONG_PTR) MI_64K_ALIGN (StartingAddress)) / X64K);
  569. EndBit = (ULONG) (((ULONG_PTR) MI_64K_ALIGN (EndingAddress)) / X64K);
  570. ASSERT (StartBit <= EndBit);
  571. if (EndBit > MiLastVadBit) {
  572. ASSERT (FALSE);
  573. EndBit = MiLastVadBit;
  574. if (StartBit > MiLastVadBit) {
  575. StartBit = MiLastVadBit;
  576. }
  577. }
  578. while (StartBit <= EndBit) {
  579. if (MI_CHECK_BIT (((PULONG)VAD_BITMAP_SPACE), StartBit) != 0) {
  580. return TRUE;
  581. }
  582. StartBit += 1;
  583. }
  584. ASSERT (MiCheckForConflictingVad (Process, StartingAddress, EndingAddress) == NULL);
  585. return FALSE;
  586. }
  587. #endif
  588. if (MiCheckForConflictingVad (Process, StartingAddress, EndingAddress) != NULL) {
  589. return TRUE;
  590. }
  591. return FALSE;
  592. }
  593. PFILE_OBJECT *
  594. MmPerfVadTreeWalk (
  595. IN PEPROCESS Process
  596. )
  597. /*++
  598. Routine Description:
  599. This routine walks through the VAD tree to find all files mapped
  600. into the specified process. It returns a pointer to a pool allocation
  601. containing the referenced file object pointers.
  602. Arguments:
  603. Process - Supplies the process to walk.
  604. Return Value:
  605. Returns a pointer to a NULL terminated pool allocation containing
  606. the file object pointers which have been referenced in the process,
  607. NULL if the memory could not be allocated.
  608. It is also the responsibility of the caller to dereference each
  609. file object in the list and then free the returned pool.
  610. Environment:
  611. PASSIVE_LEVEL, arbitrary thread context.
  612. --*/
  613. {
  614. PMMVAD Vad;
  615. PMMVAD NextVad;
  616. ULONG VadCount;
  617. PFILE_OBJECT *File;
  618. PFILE_OBJECT *FileObjects;
  619. ASSERT (KeGetCurrentIrql () == PASSIVE_LEVEL);
  620. LOCK_ADDRESS_SPACE(Process);
  621. Vad = Process->VadRoot;
  622. if (Vad == NULL) {
  623. ASSERT (Process->NumberOfVads == 0);
  624. UNLOCK_ADDRESS_SPACE (Process);
  625. return NULL;
  626. }
  627. ASSERT (Process->NumberOfVads != 0);
  628. //
  629. // Allocate one additional entry for the NULL terminator.
  630. //
  631. VadCount = Process->NumberOfVads + 1;
  632. FileObjects = (PFILE_OBJECT *) ExAllocatePoolWithTag (
  633. PagedPool,
  634. VadCount * sizeof(PFILE_OBJECT),
  635. '01pM');
  636. if (FileObjects == NULL) {
  637. UNLOCK_ADDRESS_SPACE (Process);
  638. return NULL;
  639. }
  640. File = FileObjects;
  641. while (Vad->LeftChild != NULL) {
  642. Vad = Vad->LeftChild;
  643. }
  644. if ((!Vad->u.VadFlags.PrivateMemory) &&
  645. (Vad->ControlArea != NULL) &&
  646. (Vad->ControlArea->FilePointer != NULL)) {
  647. *File = Vad->ControlArea->FilePointer;
  648. ObReferenceObject (*File);
  649. File += 1;
  650. }
  651. for (;;) {
  652. NextVad = (PMMVAD) MiGetNextNode ((PMMADDRESS_NODE)Vad);
  653. if (NextVad == NULL) {
  654. break;
  655. }
  656. Vad = (PMMVAD) NextVad;
  657. if ((!Vad->u.VadFlags.PrivateMemory) &&
  658. (Vad->ControlArea != NULL) &&
  659. (Vad->ControlArea->FilePointer != NULL)) {
  660. *File = Vad->ControlArea->FilePointer;
  661. ObReferenceObject (*File);
  662. File += 1;
  663. }
  664. Vad = NextVad;
  665. }
  666. ASSERT (File < FileObjects + VadCount);
  667. UNLOCK_ADDRESS_SPACE(Process);
  668. *File = NULL;
  669. return FileObjects;
  670. }