summaryrefslogtreecommitdiffstats
path: root/private/ntos/ex/poolhack.c
blob: e5ea13ee3bcd180c0ced9ad2bcec803e4d750dba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    pool.c

Abstract:

    Implementation of the binary buddy pool allocator for the NT executive.

Author:

    Mark Lucovsky     16-feb-1989

Environment:

    kernel mode only

Revision History:

--*/


#if DBG
//#define TRACE_ALLOC 1

#ifdef i386
#define DEADBEEF 1
#endif

#endif // DBG

#include "exp.h"
#include "..\mm\mi.h"

//
// Global Variables
//

#ifdef TRACE_ALLOC
PULONG RtlpGetFramePointer(VOID);

KSEMAPHORE TracePoolLock;
LIST_ENTRY TracePoolListHead[MaxPoolType];

typedef struct _TRACEBUFF {
    PVOID BufferAddress;
    PETHREAD Thread;
    PULONG xR1;
    PULONG xPrevR1;
} TRACEBUFF, *PTRACEBUFF;

#define MAXTRACE 1024

ULONG NextAllocTrace;
ULONG NextDeallocTrace;

TRACEBUFF AllocTrace[MAXTRACE];
TRACEBUFF DeallocTrace[MAXTRACE];


#endif //TRACE_ALLOC

#define POOL_PAGE_SIZE  0x1000
#define POOL_LOG_PAGE   12
#define POOL_LIST_HEADS 8
#define POOL_LOG_MIN    (POOL_LOG_PAGE - POOL_LIST_HEADS + 1)
#define POOL_MIN_ROUND  ( (1<<POOL_LOG_MIN) - 1 )
#define PAGE_ALIGNED(p) (!(((ULONG)p) & (POOL_PAGE_SIZE - 1)))

CHAR PoolIndexTable[128] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
                             5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
                             6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
                             6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
                           };

//
// This structure exists in the pool descriptor structure.  There is one of
// these for each pool block size
//

typedef struct _POOL_LIST_HEAD {
    ULONG CurrentFreeLength;
    ULONG Reserved;
    LIST_ENTRY ListHead;
} POOL_LIST_HEAD;
typedef POOL_LIST_HEAD *PPOOL_LIST_HEAD;

typedef struct _POOL_DESCRIPTOR {
    POOL_TYPE PoolType;
    ULONG TotalPages;
    ULONG Threshold;
    PVOID LockAddress;
    POOL_LIST_HEAD ListHeads[POOL_LIST_HEADS];
} POOL_DESCRIPTOR;
typedef POOL_DESCRIPTOR *PPOOL_DESCRIPTOR;

typedef struct _POOL_HEADER {
    USHORT LogAllocationSize;
    USHORT PoolType;
#ifdef TRACE_ALLOC
    LIST_ENTRY TraceLinks;
    PULONG xR1;
    PULONG xPrevR1;
#endif // TRACE_ALLOC
    EPROCESS *ProcessBilled;
} POOL_HEADER;
typedef POOL_HEADER *PPOOL_HEADER;

#define POOL_OVERHEAD sizeof(POOL_HEADER)
#define POOL_BUDDY_MAX (POOL_PAGE_SIZE - POOL_OVERHEAD)

POOL_DESCRIPTOR NonPagedPoolDescriptor,PagedPoolDescriptor;
POOL_DESCRIPTOR NonPagedPoolDescriptorMS,PagedPoolDescriptorMS;

KSPIN_LOCK NonPagedPoolLock;
KMUTEX PagedPoolLock;


PPOOL_DESCRIPTOR PoolVector[MaxPoolType] = {
    &NonPagedPoolDescriptor,
    &PagedPoolDescriptor,
    &NonPagedPoolDescriptorMS,
    &PagedPoolDescriptorMS
    };

POOL_TYPE BasePoolTypeTable[MaxPoolType] = {
    NonPagedPool,
    PagedPool,
    NonPagedPool,
    PagedPool
    };

BOOLEAN MustSucceedPoolTable[MaxPoolType] = {
    FALSE,
    FALSE,
    TRUE,
    TRUE
    };


PPOOL_HEADER
AllocatePoolInternal(
    IN PPOOL_DESCRIPTOR PoolDesc,
    IN LONG Index
    );

VOID
DeallocatePoolInternal(
    IN PPOOL_DESCRIPTOR PoolDesc,
    IN PPOOL_HEADER Entry
    );

PLIST_ENTRY
ExpInterlockedTryAllocatePool(
    IN PLIST_ENTRY List,
    IN KSPIN_LOCK Lock,
    IN ULONG Size,
    IN LONG SizeOffset
    );


//
// LOCK_POOL is used as a macro only within this module
//

#define LOCK_POOL(Lock,PoolType,LockHandle)                                   \
    {                                                                         \
        if ( (PoolType) == (NonPagedPool) || (PoolType) == NonPagedPoolMustSucceed ) {                                 \
            KeAcquireSpinLock((PKSPIN_LOCK)Lock,&LockHandle);                 \
        } else {                                                              \
            KeRaiseIrql(APC_LEVEL,&LockHandle);                               \
            KeWaitForSingleObject(                                            \
                Lock,                                                         \
                PoolAllocation,                                               \
                KernelMode,                                                   \
                FALSE,                                                        \
                NULL                                                          \
                );                                                            \
        }                                                                     \
    }

KIRQL
ExLockPool(
    IN POOL_TYPE PoolType
    )

/*++

Routine Description:

    This function locks the pool specified by pool type.

Arguments:

    PoolType - Specifies the pool that should be locked.

Return Value:

    Opaque - Returns a lock handle that must be returned in a subsequent
             call to ExUnlockPool.

--*/

{

    KIRQL Irql;

    if ( PoolType == NonPagedPool || PoolType == NonPagedPoolMustSucceed ) {

        //
        // Spin Lock locking
        //

        KeAcquireSpinLock((PKSPIN_LOCK)PoolVector[PoolType]->LockAddress, &Irql);
        return Irql;
    } else {

        //
        // Mutex Locking
        //

        KeRaiseIrql(APC_LEVEL, &Irql);

        KeWaitForSingleObject(
            PoolVector[PoolType]->LockAddress,
            PoolAllocation,
            KernelMode,
            FALSE,
            NULL
            );

        return Irql;
    }

}

//
// UNLOCK_POOL is used as a macro only within this module
//

#define UNLOCK_POOL(Lock,PoolType,LockHandle,Wait)                               \
    {                                                                       \
        if ( PoolType == NonPagedPool || (PoolType) == NonPagedPoolMustSucceed ) {                                   \
            KeReleaseSpinLock(                                              \
                Lock,                                                       \
                (KIRQL)LockHandle                                           \
                );                                                          \
        } else {                                                            \
            KeReleaseMutex((PKMUTEX)Lock,Wait);                             \
            KeLowerIrql(LockHandle);                                        \
        }                                                                   \
    }

VOID
ExUnlockPool(
    IN POOL_TYPE PoolType,
    IN KIRQL LockHandle,
    IN BOOLEAN Wait
    )

/*++

Routine Description:

    This function unlocks the pool specified by pool type.  If the value
    of the Wait parameter is true, then the pool's lock is released
    using "wait == true".


Arguments:

    PoolType - Specifies the pool that should be unlocked.

    LockHandle - Specifies the lock handle from a previous call to
                 ExLockPool.

    Wait - Supplies a boolean value that signifies whether the call to
           ExUnlockPool will be immediately followed by a call to one of
           the kernel Wait functions.

Return Value:

    None.

--*/

{

    if ( PoolType == NonPagedPool || (PoolType) == NonPagedPoolMustSucceed ) {

        //
        // Spin Lock locking
        //

        KeReleaseSpinLock(
            (PKSPIN_LOCK)PoolVector[PoolType]->LockAddress,
            LockHandle
            );

    } else {

        //
        // Mutex Locking
        //

        KeReleaseMutex((PKMUTEX)PoolVector[PoolType]->LockAddress,Wait);

        //
        // This could be a problem if wait == true is specified !
        //

        KeLowerIrql(LockHandle);
    }

}

VOID
InitializePool(
    IN POOL_TYPE PoolType,
    IN ULONG Threshold
    )

/*++

Routine Description:

    This procedure initializes a pool descriptor for a binary buddy pool
    type.  Once initialized, the pool may be used for allocation and
    deallocation.

    This function should be called once for each base pool type during
    system initialization.

    Each pool descriptor contains an array of list heads for free
    blocks.  Each list head holds blocks of a particular size.  One list
    head contains page-sized blocks.  The other list heads contain 1/2-
    page-sized blocks, 1/4-page-sized blocks....  A threshold is
    associated with the page-sized list head.  The number of free blocks
    on this list will not grow past the specified threshold.  When a
    deallocation occurs that would cause the threshold to be exceeded,
    the page is returned to the page-aliged pool allocator.

Arguments:

    PoolType - Supplies the type of pool being initialized (e.g.
               nonpaged pool, paged pool...).

    Threshold - Supplies the threshold value for the specified pool.

Return Value:

    None.

--*/

{
    int i;
    POOL_TYPE BasePoolType, MustSucceedPoolType;

    if ( MustSucceedPoolTable[PoolType] ) {
        KeBugCheck(PHASE0_INITIALIZATION_FAILED);
    }

    if (PoolType == NonPagedPool) {

        BasePoolType = NonPagedPool;
        MustSucceedPoolType = NonPagedPoolMustSucceed;

        KeInitializeSpinLock(&PsGetCurrentProcess()->StatisticsLock);
        KeInitializeSpinLock(&NonPagedPoolLock);
        NonPagedPoolDescriptor.LockAddress = (PVOID)&NonPagedPoolLock;
        NonPagedPoolDescriptorMS.LockAddress = (PVOID)&NonPagedPoolLock;

        KeInitializeMutex(&PagedPoolLock,MUTEX_LEVEL_EX_PAGED_POOL);
        PagedPoolDescriptor.LockAddress = (PVOID)&PagedPoolLock;
        PagedPoolDescriptorMS.LockAddress = (PVOID)&PagedPoolLock;

#ifdef TRACE_ALLOC

        KeInitializeSemaphore(&TracePoolLock,1L,1L);
        InitializeListHead(&TracePoolListHead[NonPagedPool]);
        InitializeListHead(&TracePoolListHead[PagedPool]);

#endif // TRACE_ALLOC
    } else {
        BasePoolType = PagedPool;
        MustSucceedPoolType = PagedPoolMustSucceed;
    }

    PoolVector[BasePoolType]->TotalPages = 0;
    PoolVector[BasePoolType]->Threshold = Threshold;
    PoolVector[BasePoolType]->PoolType = BasePoolType;
    PoolVector[MustSucceedPoolType]->TotalPages = 0;
    PoolVector[MustSucceedPoolType]->Threshold = 0;
    PoolVector[MustSucceedPoolType]->PoolType = MustSucceedPoolType;
    for (i=0; i<POOL_LIST_HEADS ;i++ ) {
        InitializeListHead(&PoolVector[BasePoolType]->ListHeads[i].ListHead);
        PoolVector[BasePoolType]->ListHeads[i].CurrentFreeLength = 0;
        InitializeListHead(&PoolVector[MustSucceedPoolType]->ListHeads[i].ListHead);
        PoolVector[MustSucceedPoolType]->ListHeads[i].CurrentFreeLength = 0;
    }
    return;
}

PVOID
ExAllocatePool(
    IN POOL_TYPE PoolType,
    IN ULONG NumberOfBytes
    )

/*++

Routine Description:

    This function allocates a block of pool of the specified type and
    returns a pointer to the allocated block.  This function is used to
    access both the page-aligned pools, and the binary buddy (less than
    a page) pools.

    If the number of bytes specifies a size that is too large to be
    satisfied by the appropriate binary buddy pool, then the page-aligned
    pool allocator is used.  The allocated block will be page-aligned
    and a page-sized multiple.

    Otherwise, the appropriate binary buddy pool is used.  The allocated
    block will be 64-bit aligned, but will not be page aligned.  The
    binary buddy allocator calculates the smallest block size that is a
    power of two and that can be used to satisfy the request.  If there
    are no blocks available of this size, then a block of the next
    larger block size is allocated and split in half.  One piece is
    placed back into the pool, and the other piece is used to satisfy
    the request.  If the allocator reaches the paged-sized block list,
    and nothing is there, the page-aligned pool allocator is called.
    The page is added to the binary buddy pool...

Arguments:

    PoolType - Supplies the type of pool to allocate.  If the pool type
        is one of the "MustSucceed" pool types, then this call will
        always succeed and return a pointer to allocated pool.
        Otherwise, if the system can not allocate the requested amount
        of memory a NULL is returned.

    NumberOfBytes - Supplies the number of bytes to allocate.

Return Value:

    NULL - The PoolType is not one of the "MustSucceed" pool types, and
        not enough pool exists to satisfy the request.

    NON-NULL - Returns a pointer to the allocated pool.

--*/

{
    PPOOL_HEADER Entry;
    PEPROCESS CurrentProcess;
    KIRQL LockHandle;
    PPOOL_DESCRIPTOR PoolDesc;
    PVOID Lock;
    LONG Index;
    PVOID Block;

    KIRQL OldIrql;
    PMMPTE PointerPte;
    ULONG PageFrameIndex;
    MMPTE TempPte;
    BOOLEAN ReleaseSpinLock = TRUE;

#if defined(TRACE_ALLOC) || defined (DEADBEEF)

    PULONG BadFood;
#endif //TRACE_ALLOC

#ifdef TRACE_ALLOC
    PULONG xFp, xPrevFp, xR1, xPrevR1, xPrevPrevR1;

    xFp = RtlpGetFramePointer();
    xR1 = (PULONG)*(xFp+1);
    xPrevFp = (PULONG)*xFp;
    xPrevR1 = (PULONG)*(xPrevFp+1);
    xPrevFp = (PULONG)*xPrevFp;
    xPrevPrevR1 = (PULONG)*(xPrevFp+1);

#endif // TRACE_ALLOC



    PoolDesc = PoolVector[PoolType];
    Lock = PoolDesc->LockAddress;

    if (NumberOfBytes > POOL_BUDDY_MAX) {

        LOCK_POOL(Lock,PoolType,LockHandle);

        Entry = (PPOOL_HEADER)MiAllocatePoolPages (
                                        BasePoolTypeTable[PoolType],
                                        NumberOfBytes
                                        );
        if ( !Entry && MustSucceedPoolTable[PoolType] ) {
            Entry = (PPOOL_HEADER)MiAllocatePoolPages (
                                            PoolType,
                                            NumberOfBytes
                                            );
        }
        UNLOCK_POOL(Lock,PoolType,LockHandle,FALSE);

        return Entry;
    }

    if (KeGetCurrentIrql() >= 2) {
        DbgPrint("allocating pool at irql >= 2\n");
        ReleaseSpinLock = FALSE;

    } else {
        KeAcquireSpinLock ( &MmPfnLock, &OldIrql);
    }

    PointerPte = MiReserveSystemPtes (2, SystemPteSpace, 0, 0, TRUE);

            ASSERT (MmAvailablePages > 0);
            PageFrameIndex = MiRemoveAnyPage ();
            TempPte = ValidKernelPte;
            TempPte.u.Hard.PageFrameNumber = PageFrameIndex;
            *PointerPte = TempPte;
            MiInitializePfn (PageFrameIndex, PointerPte, 1L, 1);

    if (ReleaseSpinLock) {
        KeReleaseSpinLock ( &MmPfnLock, OldIrql );
    }

    Entry = (PVOID)MiGetVirtualAddressMappedByPte (PointerPte);

    Entry = (PVOID)(((ULONG)Entry + (PAGE_SIZE - (NumberOfBytes))) &
            0xfffffff8L);

    return Entry;


    Index = ( (NumberOfBytes+POOL_OVERHEAD+POOL_MIN_ROUND) >> POOL_LOG_MIN) - 1;

    Index = PoolIndexTable[Index];

    LOCK_POOL(Lock,PoolType,LockHandle);

    if ( !IsListEmpty(&PoolDesc->ListHeads[Index].ListHead) ) {

        Block = RemoveHeadList(&PoolDesc->ListHeads[Index].ListHead);
        Entry = (PPOOL_HEADER) ((PCH)Block - POOL_OVERHEAD);
        Entry->ProcessBilled = (PEPROCESS)NULL;
        Entry->LogAllocationSize = (USHORT)POOL_LOG_MIN + Index;
        Entry->PoolType = (USHORT)PoolType;
#if defined(TRACE_ALLOC) || defined (DEADBEEF)
        BadFood = (PULONG)Block;
        *BadFood = 0xBAADF00D;
        *(BadFood+1) = 0xBAADF00D;
#endif // TRACE_ALLOC

    } else {

        Entry = AllocatePoolInternal(PoolDesc,Index);

        if ( !Entry ) {
#if DBG
            DbgPrint("EX: ExAllocatePool returning NULL\n");
            DbgBreakPoint();
#endif // DBG
            UNLOCK_POOL(Lock,PoolType,LockHandle,FALSE);
            return NULL;
        }
    }

    UNLOCK_POOL(Lock,PoolType,LockHandle,FALSE);

#ifdef TRACE_ALLOC
        {
                KIRQL xIrql;

                KeRaiseIrql(APC_LEVEL, &xIrql);

                KeWaitForSingleObject(
                    &TracePoolLock,
                    PoolAllocation,
                    KernelMode,
                    FALSE,
                    NULL
                    );

                InsertTailList(&TracePoolListHead[PoolType],&Entry->TraceLinks);

                Entry->xR1 = xR1;
                Entry->xPrevR1 = xPrevR1;
                Entry->ProcessBilled = (PEPROCESS)xPrevPrevR1;

                //Entry->ProcessBilled = (PEPROCESS)NextAllocTrace;

                AllocTrace[NextAllocTrace].BufferAddress = (PCH)Entry + POOL_OVERHEAD;
                AllocTrace[NextAllocTrace].Thread = PsGetCurrentThread();
                AllocTrace[NextAllocTrace].xR1 = xR1;
                AllocTrace[NextAllocTrace++].xPrevR1 = xPrevR1;
                if ( NextAllocTrace >= MAXTRACE ) {
                    NextAllocTrace = 0;
                }


                (VOID) KeReleaseSemaphore(
                            &TracePoolLock,
                            0L,
                            1L,
                            FALSE
                            );
                KeLowerIrql(xIrql);

        }
#endif // TRACE_ALLOC

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
    {
        PULONG NewPool;
        ULONG LongCount;

        Block = (PULONG)((PCH)Entry + POOL_OVERHEAD);
        NewPool = (PULONG) ((PCH)Entry + POOL_OVERHEAD);
        LongCount = (1 << Entry->LogAllocationSize) >> 2;
        LongCount -= (POOL_OVERHEAD>>2);

        while(LongCount--) {
            if ( *NewPool != 0xBAADF00D ) {
                DbgPrint("ExAllocatePool: No BAADF00D Block %lx at %lx\n",
                    Block,
                    NewPool
                    );
                KeBugCheck(0xBADF00D2);
            }
            *NewPool++ = 0xDEADBEEF;
        }
    }
#endif //TRACE_ALLOC

    return ((PCH)Entry + POOL_OVERHEAD);
}

ULONG
ExpAllocatePoolWithQuotaHandler(
    IN NTSTATUS ExceptionCode,
    IN PVOID PoolAddress
    )

/*++

Routine Description:

    This function is called when an exception occurs in ExFreePool
    while quota is being charged to a process.

    Its function is to deallocate the pool block and continue the search
    for an exception handler.

Arguments:

    ExceptionCode - Supplies the exception code that caused this
        function to be entered.

    PoolAddress - Supplies the address of a pool block that needs to be
        deallocated.

Return Value:

    EXCEPTION_CONTINUE_SEARCH - The exception should be propagated to the
        caller of ExAllocatePoolWithQuota.

--*/

{
    if ( PoolAddress ) {
        ASSERT(ExceptionCode == STATUS_QUOTA_EXCEEDED);
        ExFreePool(PoolAddress);
    } else {
        ASSERT(ExceptionCode == STATUS_INSUFFICIENT_RESOURCES);
    }
    return EXCEPTION_CONTINUE_SEARCH;
}



PVOID
ExAllocatePoolWithQuota(
    IN POOL_TYPE PoolType,
    IN ULONG NumberOfBytes
    )

/*++

Routine Description:

    This function allocates a block of pool of the specified type,
    returns a pointer to the allocated block, and if the binary buddy
    allocator was used to satisfy the request, charges pool quota to the
    current process.  This function is used to access both the
    page-aligned pools, and the binary buddy.

    If the number of bytes specifies a size that is too large to be
    satisfied by the appropriate binary buddy pool, then the
    page-aligned pool allocator is used.  The allocated block will be
    page-aligned and a page-sized multiple.  No quota is charged to the
    current process if this is the case.

    Otherwise, the appropriate binary buddy pool is used.  The allocated
    block will be 64-bit aligned, but will not be page aligned.  After
    the allocation completes, an attempt will be made to charge pool
    quota (of the appropriate type) to the current process object.  If
    the quota charge succeeds, then the pool block's header is adjusted
    to point to the current process.  The process object is not
    dereferenced until the pool is deallocated and the appropriate
    amount of quota is returned to the process.  Otherwise, the pool is
    deallocated, a "quota exceeded" condition is raised.

Arguments:

    PoolType - Supplies the type of pool to allocate.  If the pool type
        is one of the "MustSucceed" pool types and sufficient quota
        exists, then this call will always succeed and return a pointer
        to allocated pool.  Otherwise, if the system can not allocate
        the requested amount of memory a STATUS_INSUFFICIENT_RESOURCES
        status is raised.

    NumberOfBytes - Supplies the number of bytes to allocate.

Return Value:

    NON-NULL - Returns a pointer to the allocated pool.

    Unspecified - If insuffient quota exists to complete the pool
        allocation, the return value is unspecified.

--*/

{
    PVOID p;
    PEPROCESS Process;
    PPOOL_HEADER Entry;
    ULONG AllocationSize;

    p = ExAllocatePool(PoolType,NumberOfBytes);


#ifndef TRACE_ALLOC
    if ( p && !PAGE_ALIGNED(p) ) {

        Entry = (PPOOL_HEADER)((PCH)p - POOL_OVERHEAD);

        Process = PsGetCurrentProcess();

        //
        // Catch exception and back out allocation if necessary
        //

        try {

            PsChargePoolQuota(Process,BasePoolTypeTable[PoolType],(1 << Entry->LogAllocationSize));
            ObReferenceObject(Process);
            Entry->ProcessBilled = Process;

        } except ( ExpAllocatePoolWithQuotaHandler(GetExceptionCode(),p)) {
            KeBugCheck(GetExceptionCode());
        }

    } else {
        if ( !p ) {
            ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES);
        }
    }
#endif // TRACE_ALLOC

    return p;
}

PPOOL_HEADER
AllocatePoolInternal(
    IN PPOOL_DESCRIPTOR PoolDesc,
    IN LONG Index
    )

/*++

Routine Description:

    This function implements the guts of the binary buddy pool
    allocator.  It is a recursive function.  It assumes that the caller
    (ExAllocatePool) chose the correct pool descriptor, and that the
    allocation should be satisfied from the binary buddy allocator.

    The binary buddy allocator calculates the smallest block size that
    is a power of two and that can be used to satisfy the request.  If
    there are no blocks available of this size, then a block of the next
    larger block size is allocated and split in half.  One piece is
    placed back into the pool, and the other piece is used to satisfy
    the request.  If the allocator reaches the paged sized block list,
    and nothing is there, the page aligned pool allocator is called.
    The page is added to the binary buddy pool...

Arguments:

    PoolDesc - Supplies the address of the pool descriptor to use to
               satisfy the request.

    Index - Supplies the index into the pool descriptor list head array
            that should satisfy an allocation.

Return Value:

    Non-Null - Returns a pointer to the allocated pool header.  Before
               this can be returned to the caller of ExAllocatePool or
               ExAllocatePoolWithQuota, the value must be adjusted
               (incremented) to account for the 64bit binary buddy pool
               overhead.

--*/

{
    LONG Log2N,ShiftedN;
    PPOOL_HEADER Entry,Buddy;
    PPOOL_LIST_HEAD PoolListHead;

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
    PULONG BadFood;
#endif // TRACE_ALLOC

    Log2N = POOL_LOG_MIN + Index;
    ShiftedN = 1 << Log2N;

    PoolListHead = &PoolDesc->ListHeads[Index];

    //
    // this is the correct list head.  See if anything is on the
    // list if so, delink and mark as allocated.  Otherwise,
    // recurse and split.
    //

    if ( !IsListEmpty(&PoolListHead->ListHead) ) {

        //
        // list has an entry, so grab it
        //

        Entry = (PPOOL_HEADER)RemoveHeadList(
                                &PoolListHead->ListHead);

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
        BadFood = (PULONG)Entry;
        *BadFood = 0xBAADF00D;
        *(BadFood+1) = 0xBAADF00D;
#endif // TRACE_ALLOC

        Entry = (PPOOL_HEADER) ((PCH)Entry - POOL_OVERHEAD);

        //
        // allocated entries have a size field set and pool type set
        //

        Entry->LogAllocationSize = (USHORT)Log2N;
        Entry->PoolType = PoolDesc->PoolType;
        Entry->ProcessBilled = (PEPROCESS)NULL;

        return (PVOID)Entry;

    } else {

        if ( Index != (POOL_LIST_HEADS - 1) ) {

            //
            // This is the right list head, but since it is empty
            // must recurse.  Allocate from the next highest entry.
            // The resulting entry is then split in half.  One half
            // is marked as free and added to the list.  The other
            // half is returned
            //

            Entry = (PPOOL_HEADER)AllocatePoolInternal(PoolDesc,Index+1);

            if ( !Entry ) {
                return NULL;
            }

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
            BadFood = (PULONG)((PCH)Entry + POOL_OVERHEAD);
            *BadFood = 0xBAADF00D;
            *(BadFood+1) = 0xBAADF00D;
#endif // TRACE_ALLOC

            Buddy = (PPOOL_HEADER)((PCH)Entry + ShiftedN);

            //
            // mark buddy as free and entry as allocated
            //

            Entry->LogAllocationSize = (USHORT) Log2N;
            Entry->PoolType = PoolDesc->PoolType;
            Entry->ProcessBilled = (PEPROCESS)NULL;

            Buddy->LogAllocationSize = 0;
            Buddy->PoolType = Index;
            Buddy->ProcessBilled = (PEPROCESS)NULL;

            InsertTailList(
               &PoolListHead->ListHead,
               (PLIST_ENTRY)(((PCH)Buddy + POOL_OVERHEAD))
               );

            return (PVOID)Entry;

        } else {

            //
            // Need to call page allocator for a page to add to the pool
            //

            Entry = (PPOOL_HEADER)MiAllocatePoolPages (
                                            BasePoolTypeTable[PoolDesc->PoolType],
                                            PAGE_SIZE
                                            );

            if ( !Entry ) {
                if ( MustSucceedPoolTable[PoolDesc->PoolType] ) {
                    Entry = (PPOOL_HEADER)MiAllocatePoolPages (
                                            PoolDesc->PoolType,
                                            PAGE_SIZE
                                            );
                    ASSERT(Entry);
                } else {
                    return NULL;
                }
            }

            Entry->LogAllocationSize = (USHORT) Log2N;
            Entry->PoolType = PoolDesc->PoolType;
            Entry->ProcessBilled = (PEPROCESS)NULL;

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
            {
                PULONG NewPool;
                ULONG LongCount;

                NewPool = (PULONG) ((PCH)Entry + POOL_OVERHEAD);
                LongCount = (1 << Entry->LogAllocationSize) >> 2;
                LongCount -= (POOL_OVERHEAD>>2);

                while(LongCount--) {
                    *NewPool++ = 0xBAADF00D;
                }
            }
#endif //TRACE_ALLOC

            PoolDesc->TotalPages++;

            return (PVOID)Entry;

        }
    }
}

VOID
ExFreePool(
    IN PVOID P
    )

/*++

Routine Description:

    This function deallocates a block of pool.  This function is used to
    deallocate to both the page aligned pools, and the binary buddy
    (less than a page) pools.

    If the address of the block being deallocated is page-aligned, then
    the page-aliged pool deallocator is used.

    Otherwise, the binary buddy pool deallocator is used.  Deallocation
    looks at the allocated block's pool header to determine the pool
    type and block size being deallocated.  If the pool was allocated
    using ExAllocatePoolWithQuota, then after the deallocation is
    complete, the appropriate process's pool quota is adjusted to reflect
    the deallocation, and the process object is dereferenced.

Arguments:

    P - Supplies the address of the block of pool being deallocated.

Return Value:

    None.

--*/

{
    PPOOL_HEADER Entry;
    POOL_TYPE PoolType;
    KIRQL LockHandle;
    PVOID Lock;
    PPOOL_DESCRIPTOR PoolDesc;

    KIRQL OldIrql;
    BOOLEAN ReleaseSpinLock = TRUE;
    PMMPTE PointerPte;
    PMMPFN Pfn1;

#ifdef TRACE_ALLOC

    PULONG xFp, xPrevFp, xR1, xPrevR1;

    xFp = RtlpGetFramePointer();
    xR1 = (PULONG)*(xFp+1);
    xPrevFp = (PULONG)*xFp;
    xPrevR1 = (PULONG)*(xPrevFp+1);

#endif // TRACE_ALLOC

    //
    // If Entry is page aligned, then call page aligned pool
    //

    if ( PAGE_ALIGNED(P) ) {

        PoolType = MmDeterminePoolType(P);

        Lock = PoolVector[PoolType]->LockAddress;

        LOCK_POOL(Lock,PoolType,LockHandle);
        MiFreePoolPages (P);
        UNLOCK_POOL(Lock,PoolType,LockHandle,FALSE);
        return;
    }

    PointerPte = MiGetPteAddress (P);

    if (PointerPte->u.Hard.Valid == 0) {
        DbgPrint("bad pool deallocation\n");
        KeBugCheck (12345);
    }

    if (KeGetCurrentIrql() >= 2) {
        DbgPrint("deallocating pool at irql >= 2\n");
        ReleaseSpinLock = FALSE;
    } else {
        KeAcquireSpinLock ( &MmPfnLock, &OldIrql);
    }

    KeSweepDcache(TRUE);

    Pfn1 = MI_PFN_ELEMENT (PointerPte->u.Hard.PageFrameNumber);
    Pfn1->PteAddress = (PMMPTE)MM_EMPTY_LIST;
    MiDecrementShareCountOnly (PointerPte->u.Hard.PageFrameNumber);
    *PointerPte = ZeroPte;
    MiReleaseSystemPtes (PointerPte, 2, SystemPteSpace);
    //
    // BUGBUG Fix for MP.
    //
    KiFlushSingleTb (P,TRUE);
    if (ReleaseSpinLock) {
        KeReleaseSpinLock ( &MmPfnLock, OldIrql );
    }
    return;

    Entry = (PPOOL_HEADER)((PCH)P - POOL_OVERHEAD);

    PoolType = Entry->PoolType;

    PoolDesc = PoolVector[PoolType];
    Lock = PoolDesc->LockAddress;

    //
    // Sanity Check pool header
    //

    if ( (Entry->LogAllocationSize == 0) || (Entry->PoolType >= MaxPoolType) ) {
        DbgPrint("Invalid pool header 0x%lx 0x%lx\n",P,*(PULONG)P);
        KeBugCheck(BAD_POOL_HEADER);
        return;
    }

    if ( (ULONG)P & 0x0000000f != 8 ) {
        DbgPrint("Misaligned Deallocation 0x%lx\n",P);
        KeBugCheck(BAD_POOL_HEADER);
        return;
    }

#ifdef TRACE_ALLOC
    {
        KIRQL xIrql;
        PLIST_ENTRY Next, Target;
        BOOLEAN Found;

        KeRaiseIrql(APC_LEVEL, &xIrql);

        KeWaitForSingleObject(
            &TracePoolLock,
            PoolAllocation,
            KernelMode,
            FALSE,
            NULL
            );

        Found = FALSE;
        Target = &Entry->TraceLinks;
        Next = TracePoolListHead[PoolType].Flink;
        while( Next != &TracePoolListHead[PoolType] ){
            if ( Next == Target ) {

                RemoveEntryList(&Entry->TraceLinks);
                Found = TRUE;
                break;
            }
            Next = Next->Flink;
        }

        if ( !Found ) {
            DbgPrint("Block Not in Allocated Pool List 0x%lx\n",P);
            KeBugCheck(BAD_POOL_HEADER);
            return;
        }

        DeallocTrace[NextDeallocTrace].BufferAddress = P;
        DeallocTrace[NextDeallocTrace].Thread = PsGetCurrentThread();
        DeallocTrace[NextDeallocTrace].xR1 = xR1;
        DeallocTrace[NextDeallocTrace++].xPrevR1 = xPrevR1;
        if ( NextDeallocTrace >= MAXTRACE ) {
            NextDeallocTrace = 0;
        }

        (VOID) KeReleaseSemaphore(
                    &TracePoolLock,
                    0L,
                    1L,
                    FALSE
                    );
        KeLowerIrql(xIrql);

    }
#endif // TRACE_ALLOC

#ifndef TRACE_ALLOC

    //
    // Check ProcessBilled flag to see if quota was charged on
    // this allocation
    //

    if ( Entry->ProcessBilled ) {

        PsReturnPoolQuota(
            Entry->ProcessBilled,
            BasePoolTypeTable[PoolType],
            (1 << Entry->LogAllocationSize)
            );

        ObDereferenceObject(Entry->ProcessBilled);

    }
#endif // TRACE_ALLOC

    LOCK_POOL(Lock,PoolType,LockHandle);

    DeallocatePoolInternal(PoolDesc,Entry);

    UNLOCK_POOL(Lock,PoolType,LockHandle,FALSE);

}

VOID
DeallocatePoolInternal(
    IN PPOOL_DESCRIPTOR PoolDesc,
    IN PPOOL_HEADER Entry
    )

/*++

Routine Description:

    This function implements the guts of the binary buddy pool
    deallocator.  It is a recursive function.  It assumes that the
    caller (ExFreePool) chose the correct pool descriptor, and that
    the deallocation should be done using the binary buddy deallocator.

    The binary buddy deallocator automatically collapses adjacent free
    blocks of the same size into a single free block of the next larger
    size.  This is a recursive operation.  This does not occur on a
    deallocation of a page sized block.  If a page sized block is being
    deallocated, then the pool descriptor's threshold is examined.  If
    the deallocation would cause the threshold to be exceeded, the page
    sized block is returned to the page aligned pool allocator.

Arguments:

    PoolDesc - Supplies the address of the pool descriptor to use to
               satisfy the request.

    Entry - Supplies the address of the pool header of the block of pool
            being deallocated.

Return Value:

    None.

--*/

{
    PPOOL_HEADER Buddy,Base;
    LONG index;
    ULONG EntrySize;

    //
    // Locate buddy of Entry being deallocated.  Page size entries have no
    // buddy
    //


    index = Entry->LogAllocationSize - POOL_LOG_MIN;

    EntrySize = (1L << Entry->LogAllocationSize);

    if ( EntrySize < POOL_PAGE_SIZE ) {

        //
        // buddy of Entry is LogAllocationSize bytes from Entry rounded
        // down on a 2*LogAllocationSize boundry
        //

        Buddy = (PPOOL_HEADER)((ULONG)Entry ^ EntrySize);

        //
        // see if buddy is free.  If so, then join buddy and Entry and
        // deallocate the joined Entry
        //

        if ( Buddy->LogAllocationSize == 0 &&
             Buddy->PoolType == index ) {

            //
            // buddy is free.  Figure out which Entry is the base.
            // Convert the size of the base to next larger size and
            // deallocate the new Entry
            //

            Base = ((ULONG)Entry & EntrySize) ? Buddy : Entry;

            RemoveEntryList((PLIST_ENTRY)((PCH)Buddy + POOL_OVERHEAD));

            //
            // Update statistics for buddy's list head.
            //

            PoolDesc->ListHeads[index].CurrentFreeLength--;

            //
            // mark base as allocated as next size Entry and then deallocate it
            //

            Base->LogAllocationSize = Entry->LogAllocationSize + 1;

            DeallocatePoolInternal(PoolDesc,Base);

        } else {

            //
            // Buddy is not free, so just mark Entry as free and return to
            // appropriate list head
            //

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
            {
                PULONG OldPool;
                ULONG LongCount;

                OldPool = (PULONG)((PCH)Entry + POOL_OVERHEAD);
                LongCount = EntrySize >> 2;
                LongCount -= (POOL_OVERHEAD>>2);

                while(LongCount--) {
                    *OldPool++ = 0xBAADF00D;
                }
            }
#endif //TRACE_ALLOC

            InsertTailList(
                &PoolDesc->ListHeads[index].ListHead,
                (PLIST_ENTRY)((PCH)Entry + POOL_OVERHEAD)
                );

            PoolDesc->ListHeads[index].CurrentFreeLength++;

            Entry->LogAllocationSize = 0;
            Entry->PoolType = index;
        }

    } else {

        //
        // Page Sized Entry.  Check threshold.  If deallocating Entry
        // would cross threshold then give back the page.  Otherwise put
        // it on the free list
        //

        if ( PoolDesc->ListHeads[index].CurrentFreeLength == PoolDesc->Threshold ) {

            MiFreePoolPages (Entry);

        } else {

            //
            // so just mark Entry as free and return to appropriate list head
            //

#if defined(TRACE_ALLOC) || defined (DEADBEEF)
            {
                PULONG OldPool;
                ULONG LongCount;

                OldPool = (PULONG)((PCH)Entry + POOL_OVERHEAD);
                LongCount = EntrySize >> 2;
                LongCount -= (POOL_OVERHEAD>>2);

                while(LongCount--) {
                    *OldPool++ = 0xBAADF00D;
                }
            }
#endif //TRACE_ALLOC

            InsertTailList(
                &PoolDesc->ListHeads[index].ListHead,
                (PLIST_ENTRY)((PCH)Entry + POOL_OVERHEAD)
                );

            PoolDesc->ListHeads[index].CurrentFreeLength++;

            Entry->LogAllocationSize = 0;
            Entry->PoolType = index;

        }
    }
}


VOID
DumpPool(
    IN PSZ s,
    IN POOL_TYPE pt
    )
{
    PPOOL_DESCRIPTOR pd;
    PPOOL_HEADER ph,bph;
    PPOOL_LIST_HEAD plh;
    PLIST_ENTRY lh,next;
    LONG i;
    ULONG size;

    pd = PoolVector[pt];

    DbgPrint("\n\n%s\n",s);

    DbgPrint("PoolType: 0x%lx\n",(ULONG)pd->PoolType);
    DbgPrint("TotalPages: 0x%lx\n",pd->TotalPages);
    DbgPrint("Threshold: 0x%lx\n",pd->Threshold);
    for (i=0; i<POOL_LIST_HEADS; i++ ) {
        plh = &pd->ListHeads[i];
        size = (1 << (i + POOL_LOG_MIN));
        DbgPrint("\npd_list_head[0x%lx] size 0x%lx\n",i,size);
        DbgPrint("\tCurrentFreeLength 0x%lx\n",plh->CurrentFreeLength);
        DbgPrint("\t&ListHead 0x%lx\n",&plh->ListHead);
        lh = &plh->ListHead;
        DbgPrint("\t\tpFlink 0x%lx\n",lh->Flink);
        DbgPrint("\t\tpBlink 0x%lx\n",lh->Blink);
        next = lh->Flink;
        while ( next != lh ) {
            ph = (PPOOL_HEADER)((PCH)next - POOL_OVERHEAD);
            DbgPrint("\t\t\tpool header at 0x%lx list 0x%lx\n",ph,next);
            DbgPrint("\t\t\tLogAllocationSize 0x%lx 0x%lx\n",(ULONG)ph->LogAllocationSize,(ULONG)(1<<ph->LogAllocationSize));
            DbgPrint("\t\t\tPoolType 0x%lx\n",(ULONG)ph->PoolType);
            DbgPrint("\t\t\tProcessBilled 0x%lx\n",ph->ProcessBilled);
            if ( size != POOL_PAGE_SIZE ) {
                bph = (PPOOL_HEADER)((ULONG)ph ^ size);
                DbgPrint("\t\t\t\tBuddy pool header at 0x%lx\n",bph);
                DbgPrint("\t\t\t\tBuddy LogAllocationSize 0x%lx 0x%lx\n",(ULONG)bph->LogAllocationSize,(ULONG)(1<<bph->LogAllocationSize));
                DbgPrint("\t\t\t\tBuddy PoolType 0x%lx\n",(ULONG)bph->PoolType);
                DbgPrint("\t\t\t\tBuddy ProcessBilled 0x%lx\n",bph->ProcessBilled);
            }
            next = next->Flink;
        }
    }
}


VOID
CheckPool()
{
    PPOOL_DESCRIPTOR pd;
    PPOOL_HEADER ph,bph;
    PPOOL_LIST_HEAD plh;
    PLIST_ENTRY lh,next,lh2,next2;
    BOOLEAN buddyinlist;
    LONG i,j;
    ULONG size;

    pd = PoolVector[NonPagedPool];

    if ( pd->PoolType != NonPagedPool ) {
        DbgPrint("pd = %8lx\n", pd);
        KeBugCheck(0x70000001);
    }

    if ( (LONG) pd->TotalPages < 0 ) {
        DbgPrint("pd = %8lx\n", pd);
        KeBugCheck(0x70000002);
    }

    for (i=0; i<POOL_LIST_HEADS; i++ ) {
        plh = &pd->ListHeads[i];
        size = (1 << (i + POOL_LOG_MIN));

        if ( !IsListEmpty(&plh->ListHead) ) {

            lh = &plh->ListHead;
            next = lh->Flink;
            while ( next != lh ) {
                ph = (PPOOL_HEADER)((PCH)next - POOL_OVERHEAD);

                if ( MmDeterminePoolType(ph) != NonPagedPool ) {
                    DbgPrint("ph = %8lx\n", ph);
                    KeBugCheck(0x70000004);
                }
                if ( size != POOL_PAGE_SIZE ) {
                    bph = (PPOOL_HEADER)((ULONG)ph ^ size);
                    if ( bph->LogAllocationSize == 0 &&
                         bph->PoolType == i ) {
                        lh2 = &plh->ListHead;
                        next2 = lh2->Flink;
                        buddyinlist = FALSE;
                        while ( next2 != lh2 ) {
                            ph = (PPOOL_HEADER)((PCH)next - POOL_OVERHEAD);
                            if ( bph == ph ) {
                                buddyinlist = TRUE;
                            }
                            next2 = next2->Flink;
                        }
                        if ( !buddyinlist ) {
                            KeBugCheck(0x70000005);
                        }
                    }
                }
                if ( next == next->Flink ) {
                    DbgPrint("next = %8lx\n", next);
                    KeBugCheck(0x70000006);
                }
                next = next->Flink;
            }
        }

    }
}
VOID
DumpAllocatedPool(
    IN ULONG DumpOrFlush
    )
{
    VOID MiFlushUnusedSections( VOID );
    POOL_TYPE pt;
    ULONG PoolUsage[MaxPoolType];
    ULONG PoolFree[MaxPoolType];

    PPOOL_DESCRIPTOR pd;
    PPOOL_LIST_HEAD plh;
    PLIST_ENTRY lh,next;
    LONG i,j,k;
    ULONG size;

    if ( DumpOrFlush ) {
        MiFlushUnusedSections();
        return;
    }

    DbgPrint ("PoolHack does not work with POOL command\n");

    return;
}

VOID
ExQueryPoolUsage(
    OUT PULONG PagedPoolPages,
    OUT PULONG NonPagedPoolPages
    )
{
    PPOOL_DESCRIPTOR pd;

    pd = PoolVector[PagedPool];
    *PagedPoolPages = pd->TotalPages;
    pd = PoolVector[PagedPoolMustSucceed];
    *PagedPoolPages += pd->TotalPages;

    pd = PoolVector[NonPagedPool];
    *NonPagedPoolPages = pd->TotalPages;
    pd = PoolVector[NonPagedPoolMustSucceed];
    *NonPagedPoolPages += pd->TotalPages;

}