[NTOSKRNL] Add support for large allocations in !poolfind
[reactos.git] / ntoskrnl / mm / ARM3 / kdbg.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/kdbg.c
5 * PURPOSE: ARM Memory Manager Kernel Debugger routines
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 * Pierre Schweitzer
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 #define MODULE_INVOLVED_IN_ARM3
17 #include <mm/ARM3/miarm.h>
18
19 /* GLOBALS ********************************************************************/
20
21 typedef struct _IRP_FIND_CTXT
22 {
23 ULONG_PTR RestartAddress;
24 ULONG_PTR SData;
25 ULONG Criteria;
26 } IRP_FIND_CTXT, *PIRP_FIND_CTXT;
27
28 extern PVOID MmNonPagedPoolEnd0;
29 extern SIZE_T PoolBigPageTableSize;
30 extern PPOOL_TRACKER_BIG_PAGES PoolBigPageTable;
31
32 #define POOL_BIG_TABLE_ENTRY_FREE 0x1
33
34 /* Pool block/header/list access macros */
35 #define POOL_ENTRY(x) (PPOOL_HEADER)((ULONG_PTR)(x) - sizeof(POOL_HEADER))
36 #define POOL_FREE_BLOCK(x) (PLIST_ENTRY)((ULONG_PTR)(x) + sizeof(POOL_HEADER))
37 #define POOL_BLOCK(x, i) (PPOOL_HEADER)((ULONG_PTR)(x) + ((i) * POOL_BLOCK_SIZE))
38 #define POOL_NEXT_BLOCK(x) POOL_BLOCK((x), (x)->BlockSize)
39 #define POOL_PREV_BLOCK(x) POOL_BLOCK((x), -((x)->PreviousSize))
40
41 VOID MiDumpPoolConsumers(BOOLEAN CalledFromDbg, ULONG Tag, ULONG Mask, ULONG Flags);
42
43 /* PRIVATE FUNCTIONS **********************************************************/
44
45 #if DBG && defined(KDBG)
46
47 BOOLEAN
48 ExpKdbgExtPool(
49 ULONG Argc,
50 PCHAR Argv[])
51 {
52 ULONG_PTR Address = 0, Flags = 0;
53 PVOID PoolPage;
54 PPOOL_HEADER Entry;
55 BOOLEAN ThisOne;
56 PULONG Data;
57
58 if (Argc > 1)
59 {
60 /* Get address */
61 if (!KdbpGetHexNumber(Argv[1], &Address))
62 {
63 KdbpPrint("Invalid parameter: %s\n", Argv[0]);
64 return TRUE;
65 }
66 }
67
68 if (Argc > 2)
69 {
70 /* Get address */
71 if (!KdbpGetHexNumber(Argv[1], &Flags))
72 {
73 KdbpPrint("Invalid parameter: %s\n", Argv[0]);
74 return TRUE;
75 }
76 }
77
78 /* Check if we got an address */
79 if (Address != 0)
80 {
81 /* Get the base page */
82 PoolPage = PAGE_ALIGN(Address);
83 }
84 else
85 {
86 KdbpPrint("Heap is unimplemented\n");
87 return TRUE;
88 }
89
90 /* No paging support! */
91 if (!MmIsAddressValid(PoolPage))
92 {
93 KdbpPrint("Address not accessible!\n");
94 return TRUE;
95 }
96
97 /* Get pool type */
98 if ((Address >= (ULONG_PTR)MmPagedPoolStart) && (Address <= (ULONG_PTR)MmPagedPoolEnd))
99 KdbpPrint("Allocation is from PagedPool region\n");
100 else if ((Address >= (ULONG_PTR)MmNonPagedPoolStart) && (Address <= (ULONG_PTR)MmNonPagedPoolEnd))
101 KdbpPrint("Allocation is from NonPagedPool region\n");
102 else
103 {
104 KdbpPrint("Address 0x%p is not within any pool!\n", (PVOID)Address);
105 return TRUE;
106 }
107
108 /* Loop all entries of that page */
109 Entry = PoolPage;
110 do
111 {
112 /* Check if the address is within that entry */
113 ThisOne = ((Address >= (ULONG_PTR)Entry) &&
114 (Address < (ULONG_PTR)(Entry + Entry->BlockSize)));
115
116 if (!(Flags & 1) || ThisOne)
117 {
118 /* Print the line */
119 KdbpPrint("%c%p size: %4d previous size: %4d %s %.4s\n",
120 ThisOne ? '*' : ' ', Entry, Entry->BlockSize, Entry->PreviousSize,
121 (Flags & 0x80000000) ? "" : (Entry->PoolType ? "(Allocated)" : "(Free) "),
122 (Flags & 0x80000000) ? "" : (PCHAR)&Entry->PoolTag);
123 }
124
125 if (Flags & 1)
126 {
127 Data = (PULONG)(Entry + 1);
128 KdbpPrint(" %p %08lx %08lx %08lx %08lx\n"
129 " %p %08lx %08lx %08lx %08lx\n",
130 &Data[0], Data[0], Data[1], Data[2], Data[3],
131 &Data[4], Data[4], Data[5], Data[6], Data[7]);
132 }
133
134 /* Go to next entry */
135 Entry = POOL_BLOCK(Entry, Entry->BlockSize);
136 }
137 while ((Entry->BlockSize != 0) && ((ULONG_PTR)Entry < (ULONG_PTR)PoolPage + PAGE_SIZE));
138
139 return TRUE;
140 }
141
142 static
143 VOID
144 ExpKdbgExtPoolUsedGetTag(PCHAR Arg, PULONG Tag, PULONG Mask)
145 {
146 CHAR Tmp[4];
147 ULONG Len;
148 USHORT i;
149
150 /* Get the tag */
151 Len = strlen(Arg);
152 if (Len > 4)
153 {
154 Len = 4;
155 }
156
157 /* Generate the mask to have wildcards support */
158 for (i = 0; i < Len; ++i)
159 {
160 Tmp[i] = Arg[i];
161 if (Tmp[i] != '?')
162 {
163 *Mask |= (0xFF << i * 8);
164 }
165 }
166
167 /* Get the tag in the ulong form */
168 *Tag = *((PULONG)Tmp);
169 }
170
171 BOOLEAN
172 ExpKdbgExtPoolUsed(
173 ULONG Argc,
174 PCHAR Argv[])
175 {
176 ULONG Tag = 0;
177 ULONG Mask = 0;
178 ULONG Flags = 0;
179
180 if (Argc > 1)
181 {
182 /* If we have 2+ args, easy: flags then tag */
183 if (Argc > 2)
184 {
185 ExpKdbgExtPoolUsedGetTag(Argv[2], &Tag, &Mask);
186 if (!KdbpGetHexNumber(Argv[1], &Flags))
187 {
188 KdbpPrint("Invalid parameter: %s\n", Argv[0]);
189 }
190 }
191 else
192 {
193 /* Otherwise, try to find out whether that's flags */
194 if (strlen(Argv[1]) == 1 ||
195 (strlen(Argv[1]) == 3 && Argv[1][0] == '0' && Argv[1][1] == 'x'))
196 {
197 /* Fallback: if reading flags failed, assume it's a tag */
198 if (!KdbpGetHexNumber(Argv[1], &Flags))
199 {
200 ExpKdbgExtPoolUsedGetTag(Argv[1], &Tag, &Mask);
201 }
202 }
203 /* Or tag */
204 else
205 {
206 ExpKdbgExtPoolUsedGetTag(Argv[1], &Tag, &Mask);
207 }
208 }
209 }
210
211 /* Call the dumper */
212 MiDumpPoolConsumers(TRUE, Tag, Mask, Flags);
213
214 return TRUE;
215 }
216
217 static
218 VOID
219 ExpKdbgExtPoolFindLargePool(
220 ULONG Tag,
221 ULONG Mask)
222 {
223 ULONG i;
224
225 KdbpPrint("Scanning large pool allocation table for Tag: %.4s (%p : %p)\n", (PCHAR)&Tag, &PoolBigPageTable[0], &PoolBigPageTable[PoolBigPageTableSize - 1]);
226
227 for (i = 0; i < PoolBigPageTableSize; i++)
228 {
229 /* Free entry? */
230 if ((ULONG_PTR)PoolBigPageTable[i].Va & POOL_BIG_TABLE_ENTRY_FREE)
231 {
232 continue;
233 }
234
235 if ((PoolBigPageTable[i].Key & Mask) == (Tag & Mask))
236 {
237 /* Print the line */
238 KdbpPrint("%p: tag %.4s, size: %I64x\n",
239 PoolBigPageTable[i].Va, (PCHAR)&PoolBigPageTable[i].Key,
240 PoolBigPageTable[i].NumberOfPages << PAGE_SHIFT);
241 }
242 }
243 }
244
245 static
246 BOOLEAN
247 ExpKdbgExtValidatePoolHeader(
248 PVOID BaseVa,
249 PPOOL_HEADER Entry,
250 POOL_TYPE BasePoolTye)
251 {
252 /* Block size cannot be NULL or negative and it must cover the page */
253 if (Entry->BlockSize <= 0)
254 {
255 return FALSE;
256 }
257 if (Entry->BlockSize * 8 + (ULONG_PTR)Entry - (ULONG_PTR)BaseVa > PAGE_SIZE)
258 {
259 return FALSE;
260 }
261
262 /*
263 * PreviousSize cannot be 0 unless on page begin
264 * And it cannot be bigger that our current
265 * position in page
266 */
267 if (Entry->PreviousSize == 0 && BaseVa != Entry)
268 {
269 return FALSE;
270 }
271 if (Entry->PreviousSize * 8 > (ULONG_PTR)Entry - (ULONG_PTR)BaseVa)
272 {
273 return FALSE;
274 }
275
276 /* Must be paged pool */
277 if (((Entry->PoolType - 1) & BASE_POOL_TYPE_MASK) != BasePoolTye)
278 {
279 return FALSE;
280 }
281
282 /* Match tag mask */
283 if ((Entry->PoolTag & 0x00808080) != 0)
284 {
285 return FALSE;
286 }
287
288 return TRUE;
289 }
290
291 static
292 VOID
293 ExpKdbgExtPoolFindPagedPool(
294 ULONG Tag,
295 ULONG Mask,
296 VOID (NTAPI* FoundCallback)(PPOOL_HEADER, PVOID),
297 PVOID CallbackContext)
298 {
299 ULONG i = 0;
300 PPOOL_HEADER Entry;
301 PVOID BaseVa;
302 PMMPDE PointerPde;
303
304 KdbpPrint("Searching Paged pool (%p : %p) for Tag: %.4s\n", MmPagedPoolStart, MmPagedPoolEnd, (PCHAR)&Tag);
305
306 /*
307 * To speed up paged pool search, we will use the allocation bipmap.
308 * This is possible because we live directly in the kernel :-)
309 */
310 i = RtlFindSetBits(MmPagedPoolInfo.PagedPoolAllocationMap, 1, 0);
311 while (i != 0xFFFFFFFF)
312 {
313 BaseVa = (PVOID)((ULONG_PTR)MmPagedPoolStart + (i << PAGE_SHIFT));
314 Entry = BaseVa;
315
316 /* Validate our address */
317 if ((ULONG_PTR)BaseVa > (ULONG_PTR)MmPagedPoolEnd || (ULONG_PTR)BaseVa + PAGE_SIZE > (ULONG_PTR)MmPagedPoolEnd)
318 {
319 break;
320 }
321
322 /* Check whether we are beyond expansion */
323 PointerPde = MiAddressToPde(BaseVa);
324 if (PointerPde >= MmPagedPoolInfo.NextPdeForPagedPoolExpansion)
325 {
326 break;
327 }
328
329 /* Check if allocation is valid */
330 if (MmIsAddressValid(BaseVa))
331 {
332 for (Entry = BaseVa;
333 (ULONG_PTR)Entry + sizeof(POOL_HEADER) < (ULONG_PTR)BaseVa + PAGE_SIZE;
334 Entry = (PVOID)((ULONG_PTR)Entry + 8))
335 {
336 /* Try to find whether we have a pool entry */
337 if (!ExpKdbgExtValidatePoolHeader(BaseVa, Entry, PagedPool))
338 {
339 continue;
340 }
341
342 if ((Entry->PoolTag & Mask) == (Tag & Mask))
343 {
344 if (FoundCallback != NULL)
345 {
346 FoundCallback(Entry, CallbackContext);
347 }
348 else
349 {
350 /* Print the line */
351 KdbpPrint("%p size: %4d previous size: %4d %s %.4s\n",
352 Entry, Entry->BlockSize, Entry->PreviousSize,
353 Entry->PoolType ? "(Allocated)" : "(Free) ",
354 (PCHAR)&Entry->PoolTag);
355 }
356 }
357 }
358 }
359
360 i = RtlFindSetBits(MmPagedPoolInfo.PagedPoolAllocationMap, 1, i + 1);
361 }
362 }
363
364 static
365 VOID
366 ExpKdbgExtPoolFindNonPagedPool(
367 ULONG Tag,
368 ULONG Mask,
369 VOID (NTAPI* FoundCallback)(PPOOL_HEADER, PVOID),
370 PVOID CallbackContext)
371 {
372 PPOOL_HEADER Entry;
373 PVOID BaseVa;
374
375 KdbpPrint("Searching NonPaged pool (%p : %p) for Tag: %.4s\n", MmNonPagedPoolStart, MmNonPagedPoolEnd0, (PCHAR)&Tag);
376
377 /* Brute force search: start browsing the whole non paged pool */
378 for (BaseVa = MmNonPagedPoolStart;
379 (ULONG_PTR)BaseVa + PAGE_SIZE <= (ULONG_PTR)MmNonPagedPoolEnd0;
380 BaseVa = (PVOID)((ULONG_PTR)BaseVa + PAGE_SIZE))
381 {
382 Entry = BaseVa;
383
384 /* Check whether we are beyond expansion */
385 if (BaseVa >= MmNonPagedPoolExpansionStart)
386 {
387 break;
388 }
389
390 /* Check if allocation is valid */
391 if (!MmIsAddressValid(BaseVa))
392 {
393 continue;
394 }
395
396 for (Entry = BaseVa;
397 (ULONG_PTR)Entry + sizeof(POOL_HEADER) < (ULONG_PTR)BaseVa + PAGE_SIZE;
398 Entry = (PVOID)((ULONG_PTR)Entry + 8))
399 {
400 /* Try to find whether we have a pool entry */
401 if (!ExpKdbgExtValidatePoolHeader(BaseVa, Entry, NonPagedPool))
402 {
403 continue;
404 }
405
406 if ((Entry->PoolTag & Mask) == (Tag & Mask))
407 {
408 if (FoundCallback != NULL)
409 {
410 FoundCallback(Entry, CallbackContext);
411 }
412 else
413 {
414 /* Print the line */
415 KdbpPrint("%p size: %4d previous size: %4d %s %.4s\n",
416 Entry, Entry->BlockSize, Entry->PreviousSize,
417 Entry->PoolType ? "(Allocated)" : "(Free) ",
418 (PCHAR)&Entry->PoolTag);
419 }
420 }
421 }
422 }
423 }
424
425 BOOLEAN
426 ExpKdbgExtPoolFind(
427 ULONG Argc,
428 PCHAR Argv[])
429 {
430 ULONG Tag = 0;
431 ULONG Mask = 0;
432 ULONG PoolType = NonPagedPool;
433
434 if (Argc == 1)
435 {
436 KdbpPrint("Specify a tag string\n");
437 return TRUE;
438 }
439
440 /* First arg is tag */
441 if (strlen(Argv[1]) != 1 || Argv[1][0] != '*')
442 {
443 ExpKdbgExtPoolUsedGetTag(Argv[1], &Tag, &Mask);
444 }
445
446 /* Second arg might be pool to search */
447 if (Argc > 2)
448 {
449 PoolType = strtoul(Argv[2], NULL, 0);
450
451 if (PoolType > 1)
452 {
453 KdbpPrint("Only (non) paged pool are supported\n");
454 return TRUE;
455 }
456 }
457
458 /* First search for large allocations */
459 ExpKdbgExtPoolFindLargePool(Tag, Mask);
460
461 if (PoolType == NonPagedPool)
462 {
463 ExpKdbgExtPoolFindNonPagedPool(Tag, Mask, NULL, NULL);
464 }
465 else if (PoolType == PagedPool)
466 {
467 ExpKdbgExtPoolFindPagedPool(Tag, Mask, NULL, NULL);
468 }
469
470 return TRUE;
471 }
472
473 VOID
474 NTAPI
475 ExpKdbgExtIrpFindPrint(
476 PPOOL_HEADER Entry,
477 PVOID Context)
478 {
479 PIRP Irp;
480 BOOLEAN IsComplete = FALSE;
481 PIRP_FIND_CTXT FindCtxt = Context;
482 PIO_STACK_LOCATION IoStack = NULL;
483 PUNICODE_STRING DriverName = NULL;
484 ULONG_PTR SData = FindCtxt->SData;
485 ULONG Criteria = FindCtxt->Criteria;
486
487 /* Free entry, ignore */
488 if (Entry->PoolType == 0)
489 {
490 return;
491 }
492
493 /* Get the IRP */
494 Irp = (PIRP)POOL_FREE_BLOCK(Entry);
495
496 /* Bail out if not matching restart address */
497 if ((ULONG_PTR)Irp < FindCtxt->RestartAddress)
498 {
499 return;
500 }
501
502 /* Avoid bogus IRP stack locations */
503 if (Irp->CurrentLocation <= Irp->StackCount + 1)
504 {
505 IoStack = IoGetCurrentIrpStackLocation(Irp);
506
507 /* Get associated driver */
508 if (IoStack->DeviceObject && IoStack->DeviceObject->DriverObject)
509 DriverName = &IoStack->DeviceObject->DriverObject->DriverName;
510 }
511 else
512 {
513 IsComplete = TRUE;
514 }
515
516 /* Display if: no data, no criteria or if criteria matches data */
517 if (SData == 0 || Criteria == 0 ||
518 (Criteria & 0x1 && IoStack && SData == (ULONG_PTR)IoStack->DeviceObject) ||
519 (Criteria & 0x2 && SData == (ULONG_PTR)Irp->Tail.Overlay.OriginalFileObject) ||
520 (Criteria & 0x4 && Irp->MdlAddress && SData == (ULONG_PTR)Irp->MdlAddress->Process) ||
521 (Criteria & 0x8 && SData == (ULONG_PTR)Irp->Tail.Overlay.Thread) ||
522 (Criteria & 0x10 && SData == (ULONG_PTR)Irp->UserEvent))
523 {
524 if (!IsComplete)
525 {
526 KdbpPrint("%p Thread %p current stack (%x, %x) belongs to %wZ\n", Irp, Irp->Tail.Overlay.Thread, IoStack->MajorFunction, IoStack->MinorFunction, DriverName);
527 }
528 else
529 {
530 KdbpPrint("%p Thread %p is complete (CurrentLocation %d > StackCount %d)\n", Irp, Irp->Tail.Overlay.Thread, Irp->CurrentLocation, Irp->StackCount + 1);
531 }
532 }
533 }
534
535 BOOLEAN
536 ExpKdbgExtIrpFind(
537 ULONG Argc,
538 PCHAR Argv[])
539 {
540 ULONG PoolType = NonPagedPool;
541 IRP_FIND_CTXT FindCtxt;
542
543 /* Pool type */
544 if (Argc > 1)
545 {
546 PoolType = strtoul(Argv[1], NULL, 0);
547
548 if (PoolType > 1)
549 {
550 KdbpPrint("Only (non) paged pool are supported\n");
551 return TRUE;
552 }
553 }
554
555 RtlZeroMemory(&FindCtxt, sizeof(IRP_FIND_CTXT));
556
557 /* Restart address */
558 if (Argc > 2)
559 {
560 if (!KdbpGetHexNumber(Argv[2], &FindCtxt.RestartAddress))
561 {
562 KdbpPrint("Invalid parameter: %s\n", Argv[0]);
563 FindCtxt.RestartAddress = 0;
564 }
565 }
566
567 if (Argc > 4)
568 {
569 if (!KdbpGetHexNumber(Argv[4], &FindCtxt.SData))
570 {
571 FindCtxt.SData = 0;
572 }
573 else
574 {
575 if (strcmp(Argv[3], "device") == 0)
576 {
577 FindCtxt.Criteria = 0x1;
578 }
579 else if (strcmp(Argv[3], "fileobject") == 0)
580 {
581 FindCtxt.Criteria = 0x2;
582 }
583 else if (strcmp(Argv[3], "mdlprocess") == 0)
584 {
585 FindCtxt.Criteria = 0x4;
586 }
587 else if (strcmp(Argv[3], "thread") == 0)
588 {
589 FindCtxt.Criteria = 0x8;
590 }
591 else if (strcmp(Argv[3], "userevent") == 0)
592 {
593 FindCtxt.Criteria = 0x10;
594 }
595 else if (strcmp(Argv[3], "arg") == 0)
596 {
597 FindCtxt.Criteria = 0x1f;
598 }
599 }
600 }
601
602 if (PoolType == NonPagedPool)
603 {
604 ExpKdbgExtPoolFindNonPagedPool(TAG_IRP, 0xFFFFFFFF, ExpKdbgExtIrpFindPrint, &FindCtxt);
605 }
606 else if (PoolType == PagedPool)
607 {
608 ExpKdbgExtPoolFindPagedPool(TAG_IRP, 0xFFFFFFFF, ExpKdbgExtIrpFindPrint, &FindCtxt);
609 }
610
611 return TRUE;
612 }
613
614 #endif // DBG && KDBG
615
616 /* EOF */