-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusermodeMemoryManager.c
More file actions
2853 lines (1739 loc) · 58.6 KB
/
usermodeMemoryManager.c
File metadata and controls
2853 lines (1739 loc) · 58.6 KB
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
/*
* Usermode virtual memory program intended as a rudimentary simulation
* of Windows OS kernelmode memory management
*
* Jason Wang, August 2020
*/
#include "usermodeMemoryManager.h"
#include "./infrastructure/enqueue-dequeue.h"
#include "./infrastructure/jLock.h"
#include "./coreFunctions/pageFile.h"
#include "./coreFunctions/getPage.h"
#include "./coreFunctions/pageFault.h"
#include "./coreFunctions/pageTrade.h"
#include "./dataStructures/PTEpermissions.h"
#include "./dataStructures/VApermissions.h"
#include "./dataStructures/VADNodes.h"
/******************************************************
*********************** GLOBALS **********************
*****************************************************/
void* leafVABlock; // starting address of virtual memory block
void* leafVABlockEnd; // ending address of virtual memory block
PPFNdata PFNarray; // starting address of PFN metadata array
PPTE PTEarray; // starting address of page table
ULONG64 totalCommittedPages; // count of committed pages (initialized to zero)
ULONG_PTR totalMemoryPageLimit = NUM_PAGES + (PAGEFILE_SIZE >> PAGE_SHIFT); // limit of committed pages (memory block + pagefile space)
void* pageFileVABlock; // starting address of pagefile "disk" (memory)
#ifdef PAGEFILE_PFN_CHECK
PPageFileDebug pageFileDebugArray;
#else
ULONG_PTR pageFileBitArray[PAGEFILE_PAGES/(8*sizeof(ULONG_PTR))];
#endif
#ifdef CHECK_PFNS
PULONG_PTR aPFNs;
#endif
// Execute-Write-Read (bit ordering)
ULONG_PTR permissionMasks[] = { 0, readMask, (readMask | writeMask), (readMask | executeMask), (readMask | writeMask | executeMask) };
/************ List declarations *****************/
listData listHeads[ACTIVE]; // page listHeads array
listData zeroVAListHead; // listHead of zeroVAs used for zeroing PFNs (via AWE mapping)
listData writeVAListHead; // listHead of writeVAs used for writing to page file
listData readPFVAListHead; // listHead of pagefile read VAs used for reading from pagefile
listData pageTradeVAListHead;
listData VADListHead; // list of VADs
listData readInProgEventListHead; // list of events
/********** Locks ************/
PCRITICAL_SECTION PTELockArray;
CRITICAL_SECTION pageFileLock;
CRITICAL_SECTION VADWriteLock;
HANDLE physicalPageHandle; // for multi-mapped pages (to support multithreading)
HANDLE wakeTrimHandle;
HANDLE wakeModifiedWriterHandle;
ULONG_PTR numPagesReturned;
ULONG_PTR virtualMemPages;
PULONG_PTR VADBitArray;
BOOLEAN debugMode; // toggled by -v flag on cmd line
BOOL
LoggedSetLockPagesPrivilege ( HANDLE hProcess,
BOOL bEnable)
{
struct {
DWORD Count;
LUID_AND_ATTRIBUTES Privilege [1];
} Info;
HANDLE Token;
BOOL Result;
// Open the token.
Result = OpenProcessToken ( hProcess,
TOKEN_ADJUST_PRIVILEGES,
& Token);
if( Result != TRUE )
{
_tprintf( _T("Cannot open process token.\n") );
return FALSE;
}
// Enable or disable?
Info.Count = 1;
if( bEnable )
{
Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
}
else
{
Info.Privilege[0].Attributes = 0;
}
// Get the LUID.
Result = LookupPrivilegeValue ( NULL,
SE_LOCK_MEMORY_NAME,
&(Info.Privilege[0].Luid));
if( Result != TRUE )
{
_tprintf( _T("Cannot get privilege for %s.\n"), SE_LOCK_MEMORY_NAME );
return FALSE;
}
// Adjust the privilege.
Result = AdjustTokenPrivileges ( Token, FALSE,
(PTOKEN_PRIVILEGES) &Info,
0, NULL, NULL);
// Check the result.
if( Result != TRUE )
{
_tprintf (_T("Cannot adjust token privileges (%u)\n"), GetLastError() );
return FALSE;
}
else
{
if( GetLastError() != ERROR_SUCCESS )
{
_tprintf (_T("Cannot enable the SE_LOCK_MEMORY_NAME privilege; "));
_tprintf (_T("please check the local policy.\n"));
return FALSE;
}
}
CloseHandle( Token );
return TRUE;
}
BOOL
getPrivilege()
{
BOOL bResult;
bResult = LoggedSetLockPagesPrivilege( GetCurrentProcess(), TRUE );
return bResult;
}
VOID
initVABlock(ULONG_PTR numPages)
{
#ifdef MULTIPLE_MAPPINGS
MEM_EXTENDED_PARAMETER extendedParameters = {0};
extendedParameters.Type = MemExtendedParameterUserPhysicalHandle;
extendedParameters.Handle = physicalPageHandle;
leafVABlock = VirtualAlloc2(NULL, NULL, numPages << PAGE_SHIFT, MEM_RESERVE | MEM_PHYSICAL, PAGE_READWRITE, &extendedParameters, 1); // equiv to numVAs*PAGE_SIZE
#else
// creates a VAD node that we can define (i.e. is not pagefaulted by underlying kernel mm)
leafVABlock = VirtualAlloc(NULL, numPages*pageSize, MEM_RESERVE | MEM_PHYSICAL, PAGE_READWRITE);
#endif
if (leafVABlock == NULL) {
PRINT_ERROR("[initVABlock] unable to initialize VA block\n");
exit(-1);
}
// Does not need to be checked for overflow since Virtual Alloc will not return a value
leafVABlockEnd = (PVOID) ( (ULONG_PTR)leafVABlock + ( numPages << PAGE_SHIFT ) ); // equiv to numPages*PAGE_SIZE
}
VOID
initPFNarray(PULONG_PTR arrayPFNs, ULONG_PTR numPages)
{
PVOID commitCheckVA;
ULONG_PTR maxPFN;
//
// Initialize initial "maximum" value to 0 (replaced in subsequent loop)
//
maxPFN = 0;
//
// Loop through arrayPFNs (from AllocateUserPhysicalPages) to find largest numerical PFN
//
for (int i = 0; i < numPages; i++) {
//
// If current PFN is larger than current maximum, replace
//
if (maxPFN < arrayPFNs[i]) {
maxPFN = arrayPFNs[i];
}
}
//
// If the maximum numerical PFN cannot fit within 40 bits allocated for PFN index in the PTE,
// print an error message and return
//
if ( ( (ULONG_PTR)1 << PFN_BITS) < maxPFN ) {
PRINT_ERROR("Insufficient PFN bits in PTE (cannot represent maximum PFN value returned)\n");
exit(-1);
}
//
// Virtual Alloc (with MEM_RESERVE) PFN metadata array
//
PFNarray = VirtualAlloc(NULL, (maxPFN+1)*(sizeof(PFNdata)), MEM_RESERVE, PAGE_READWRITE);
if (PFNarray == NULL) {
PRINT_ERROR("Could not allocate for PFN metadata array\n");
exit(-1);
}
//
// Loop through all PFNs, MEM_COMMITTING PFN subsections and enqueueing to free for each page
//
for (int i = 0; i < numPages; i++) {
PPFNdata newPFN;
newPFN = PFNarray + arrayPFNs[i];
//
// Since these are merely committed sections of the larger PFNarray,
// it is freed singularly when the PFNarray itself is freed.
//
commitCheckVA = VirtualAlloc(newPFN, sizeof (PFNdata), MEM_COMMIT, PAGE_READWRITE);
if (commitCheckVA == NULL) {
PRINT_ERROR("failed to commit subsection of PFN array at PFN %d\n", i);
exit(-1);
}
//
// Note: no lock needed functionally (simply to satisfy assert in enqueuePage)
//
acquireJLock(&newPFN->lockBits);
//
// "Reset"/clear pagefile offset & refcount PFN fields
//
newPFN->pageFileOffset = INVALID_BITARRAY_INDEX;
newPFN->refCount = 0;
//
// add page to free list
//
enqueuePage(&freeListHead, newPFN);
releaseJLock(&newPFN->lockBits);
}
}
VOID
initPTEarray(ULONG_PTR numPages)
{
//
// Virtual Alloc (with MEM_RESERVE | MEM_COMMIT) for PTE array
//
PTEarray = VirtualAlloc(NULL, numPages*(sizeof(PTE)), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (PTEarray == NULL) {
PRINT_ERROR("Could not allocate for PTEarray\n");
exit(-1);
}
}
VOID
initPageFile(ULONG_PTR diskSize)
{
//
// Virtual Alloc (with MEM_RESERVE | MEM_COMMIT) for pageFileVABlock
//
pageFileVABlock = VirtualAlloc(NULL, diskSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (pageFileVABlock == NULL) {
PRINT_ERROR("Could not allocate for pageFile\n");
exit(-1);
}
#ifndef PAGEFILE_OFF
#ifdef PAGE_TRADE
//
// Used as a "working" page for page trading in order to avoid deadlock
//
InterlockedIncrement64(&totalCommittedPages);
#endif
#else
for (int i = 0; i < PAGEFILE_PAGES; i++) {
InterlockedIncrement64(&totalCommittedPages); // (currently used as a "working " page)
}
#endif
}
ULONG_PTR
allocatePhysPages(ULONG_PTR numPages, PULONG_PTR arrayPFNs)
{
BOOL bResult;
ULONG_PTR numPagesAllocated;
//
// Secure privilege for the code
//
bResult = getPrivilege();
if (bResult != TRUE) {
PRINT_ERROR("could not get privilege successfully \n");
exit(-1);
}
numPagesAllocated = numPages;
#ifdef MULTIPLE_MAPPINGS
MEM_EXTENDED_PARAMETER extendedParameters = {0};
extendedParameters.Type = MemSectionExtendedParameterUserPhysicalFlags;
extendedParameters.ULong64 = 0;
physicalPageHandle = CreateFileMapping2(NULL,
NULL,
SECTION_MAP_READ | SECTION_MAP_WRITE,
PAGE_READWRITE,
SEC_RESERVE,
0,
NULL,
&extendedParameters,
1 );
if (physicalPageHandle == NULL) {
PRINT_ERROR("could not create file mapping\n");
exit(-1);
}
#else
physicalPageHandle = GetCurrentProcess();
#endif
bResult = AllocateUserPhysicalPages(physicalPageHandle, &numPagesAllocated, arrayPFNs);
if (bResult != TRUE) {
PRINT_ERROR("could not allocate pages successfully \n");
exit(-1);
}
if (numPagesAllocated != numPages) {
PRINT("allocated only %llu pages out of %u pages requested\n", numPagesAllocated, NUM_PAGES);
}
return numPagesAllocated;
}
BOOLEAN
zeroPage(ULONG_PTR PFN)
{
PVOID zeroVA;
PVANode zeroVANode;
zeroVANode = dequeueLockedVA(&zeroVAListHead);
while (zeroVANode == NULL) {
PRINT("[zeroPage] Waiting for release of event node (list empty)\n");
WaitForSingleObject(zeroVAListHead.newPagesEvent, INFINITE);
zeroVANode = dequeueLockedVA(&zeroVAListHead);
}
zeroVA = zeroVANode->VA;
//
// Map PFN to the "zero" VA
//
if (!MapUserPhysicalPages(zeroVA, 1, &PFN)) {
enqueueVA(&zeroVAListHead, zeroVANode);
PRINT_ERROR("error remapping zeroVA\n");
return FALSE;
}
//
// Zero page
//
memset(zeroVA, 0, PAGE_SIZE);
//
// Unmap zeroVA from page - PFN is now ready to be alloc'd
//
if (!MapUserPhysicalPages(zeroVA, 1, NULL)) {
enqueueVA(&zeroVAListHead, zeroVANode);
PRINT_ERROR("error zeroing page\n");
return FALSE;
}
enqueueVA(&zeroVAListHead, zeroVANode);
return TRUE;
}
BOOLEAN
zeroPageWriter()
{
PPFNdata PFNtoZero;
ULONG_PTR pageNumtoZero;
//
// Acquires & releases list lock, but retains
// PFN lock
//
PFNtoZero = dequeueLockedPage(&freeListHead, TRUE);
if (PFNtoZero == NULL) {
PRINT("free list empty - could not write out\n");
return FALSE;
}
//
// Set overloaded "writeinprogress" bit (to signify page is currently being zeroed)
// Note: write in progress is also set by modified page writer, but in a different context
//
PFNtoZero->writeInProgressBit = 1;
releaseJLock(&PFNtoZero->lockBits);
//
// Derive physical page number from metadata
//
pageNumtoZero = PFNtoZero - PFNarray;
//
// zero the page contents, does not update status bits in PFN metadata
// note: can be page traded within this time, where status bits could change from ZERO to AWAITING_QUARANTINE
//
zeroPage(pageNumtoZero);
acquireJLock(&PFNtoZero->lockBits);
//
// Clear "writeinprogress" bit (to signify page has been zeroed and can now be traded, once lock is released)
//
PFNtoZero->writeInProgressBit = 0;
if (PFNtoZero->statusBits == AWAITING_QUARANTINE) {
enqueuePage(&quarantineListHead, PFNtoZero);
PRINT(" - moved page from free -> quarantine\n");
} else {
#ifdef PAGEFILE_PFN_CHECK
enqueuePageBasic(&zeroListHead, PFNtoZero);
#else
// enqueue to zeroList (updates status bits in PFN metadata)
enqueuePage(&zeroListHead, PFNtoZero);
#endif
}
releaseJLock(&PFNtoZero->lockBits);
PRINT(" - Moved page from free -> zero \n");
return TRUE;
}
DWORD WINAPI
zeroPageThread(HANDLE terminationHandle)
{
int numZeroed;
int numWaited;
HANDLE handleArray[2];
//
// Initialize numZeroed/waited counters to zero
//
numZeroed = 0;
numWaited = 0;
//
// Create local handle array
//
handleArray[0] = terminationHandle;
handleArray[1] = freeListHead.newPagesEvent;
//
// Zeroes pages until none remaining on free list
//
while (TRUE) {
BOOLEAN bres;
//
// zeroPageWriter returns false on empty zero page list.
// Thus, in the case it returns false, it will wait for either
// a new page event or termination handle.
//
bres = zeroPageWriter();
if (bres == FALSE) {
DWORD retVal;
DWORD index;
//
// Wait for either termination event or more pages added to free list
//
retVal = WaitForMultipleObjects(2, handleArray, FALSE, INFINITE);
index = retVal - WAIT_OBJECT_0;
//
// If event set is the terminate threads event, return from function
//
if (index == 0) {
PRINT_ALWAYS("zeropagethread - numPages moved to zero list : %d, numWaited : %d\n", numZeroed, numWaited);
return 0;
}
numWaited++;
continue;
}
numZeroed++;
}
}
BOOLEAN
freePageTestWriter()
{
PPFNdata PFNtoFree;
//
// Dequeue page from zero list to enqueue to free list
//
PFNtoFree = dequeueLockedPage(&zeroListHead, FALSE);
if (PFNtoFree == NULL) {
PRINT("zero list empty - could not write out\n");
return FALSE;
}
acquireJLock(&PFNtoFree->lockBits);
#ifdef PAGEFILE_PFN_CHECK
//
// Simplified version of enqueuePage, only used for
// when pagefile PFNs are checked
//
enqueuePageBasic(&freeListHead, PFNtoFree);
#else
//
// enqueue to freeList (updates status bits in PFN metadata)
//
enqueuePage(&freeListHead, PFNtoFree);
#endif
releaseJLock(&PFNtoFree->lockBits);
return TRUE;
}
DWORD WINAPI
freePageTestThread(HANDLE terminationHandle)
{
int numFreed;
int numWaited;
HANDLE handleArray[2];
//
// Initialize numFreed/Waited counters to zero
//
numFreed = 0;
numWaited= 0;
//
// Create local handle array
//
handleArray[0] = terminationHandle;
handleArray[1] = zeroListHead.newPagesEvent;
//
// Moves pages from zero list to free list until none remaining on
// zero list.
//
// Important note: this DETRACTS functionally from the program
// (since zeroing pages takes time), and is simply to increase
// the artificial load on the program in testing.
//
while (TRUE) {
BOOLEAN bres;
bres = freePageTestWriter();
if (bres == FALSE) {
DWORD retVal;
DWORD index;
//
// Wait for either terminate threads event to be set or for new pages
// to be enqueued to the zero list
//
retVal = WaitForMultipleObjects(2, handleArray, FALSE, INFINITE);
index = retVal - WAIT_OBJECT_0;
//
// If event set is the terminate threads event, return from function
//
if (index == 0) {
PRINT_ALWAYS("freepagetestthread - numPages moved to free list: %d, numWaited : %d \n", numFreed, numWaited);
return 0;
}
numWaited++;
continue;
}
numFreed++;
}
}
VOID
releaseAwaitingFreePFN(PPFNdata PFNtoFree)
{
//
// Verify page lock is held
//
ASSERT(PFNtoFree->lockBits != 0);
//
// Clear index in pagefile, pagefile offset field in PFN,
// and remodified bit in PFN.
//
clearPFBitIndex(PFNtoFree->pageFileOffset);
PFNtoFree->pageFileOffset = INVALID_BITARRAY_INDEX;
PFNtoFree->remodifiedBit = 0;
//
// Enqueue Page to free list
//
enqueuePage(&freeListHead, PFNtoFree);
PRINT("[releaseAwaitingFreePFN] VA decommitted during PF write\n");
}
BOOLEAN
modifiedPageWriter()
{
BOOLEAN wakeModifiedWriter;
PPFNdata PFNtoWrite;
BOOLEAN bResult;
wakeModifiedWriter = FALSE;
PRINT("[modifiedPageWriter] modifiedListCount == %llu\n", modifiedListHead.count);
//
// Return page with lock bits set
//
PFNtoWrite = dequeueLockedPage(&modifiedListHead, TRUE);
if (PFNtoWrite == NULL) {
PRINT("[modifiedPageWriter] modified list empty - could not write out\n");
return FALSE;
}
//
// Lock has previuosly been acquired - assert that PFN has no associated pagefile
// offset ad that write in progress bit is zero. Then set write in progress bit to 1
//
ASSERT(PFNtoWrite->pageFileOffset == INVALID_BITARRAY_INDEX);
ASSERT(PFNtoWrite->writeInProgressBit == 0);
PFNtoWrite->writeInProgressBit = 1;
//
// Revert PFN status to modified so that it can once again be faulted
//
PFNtoWrite->statusBits = MODIFIED;
//
// Clear PFN's remodified bit (since it will be written/attempted to be written
// to pagefile), and can now be re-set once page lock is released
//
PFNtoWrite->remodifiedBit = 0;
//
// Release PFN lock: write in progress bit has been set, status bits have
// been set to modified, PFN dirty bit has been cleared so that another
// faulter can reset it upon checking that write in progress bit is 1.
// Page can be now decommitted or re-modified
//
releaseJLock(&PFNtoWrite->lockBits);
//
// If address verification is enabled (asserts that the contents
// of a given page corresponds to the virtual address it is mapped
// or standby at)
//
ULONG_PTR expectedSig;
#ifdef TESTING_VERIFY_ADDRESSES
expectedSig = (ULONG_PTR) leafVABlock + ( ( PFNtoWrite->PTEindex ) << PAGE_SHIFT );
#else
expectedSig = 0;
#endif
//
// Write page to pagefile - can no longer be accessed since write in progress is set
//
bResult = writePageToFileSystem(PFNtoWrite, expectedSig);
//
// Re-acquire PFN lock post-pagefile write
//
acquireJLock(&PFNtoWrite->lockBits);
//
// Since we've marked the PFN as write in progress, it CANNOT be in zero or free state
// - would require a decommit (via decommitVA), which checks the writeInProgressBit
// - so, we can assert that it is in neither free nor zero
//
ASSERT (PFNtoWrite->statusBits != FREE && PFNtoWrite->statusBits != ZERO);
//
// Clear write in progress bit (since page write has completed)
//
ASSERT(PFNtoWrite->writeInProgressBit == 1);
PFNtoWrite->writeInProgressBit = 0;
#ifdef PTE_CHANGE_LOG
PTE logPTE;
PTE zeroPTE;
logPTE.u1.ulongPTE = bResult;
zeroPTE.u1.ulongPTE = 0;
logEntry( PTEarray + PFNtoWrite->PTEindex, logPTE, zeroPTE, PFNtoWrite);
#endif
//
// If PFN has been decommitted during the pagefile write
// (while page lock was released), release the awaiting free
// PFN
//
if (PFNtoWrite->statusBits == AWAITING_FREE) {
releaseAwaitingFreePFN(PFNtoWrite);
releaseJLock(&PFNtoWrite->lockBits);
return TRUE;
}
//
// If filesystem write fails
//
if (bResult != TRUE) {
//
// Since write has failed, re-set PFN dirty bit, clear
// PF bit index and clear pagefile offset out of PFN
//
clearPFBitIndex(PFNtoWrite->pageFileOffset);
PFNtoWrite->pageFileOffset = INVALID_BITARRAY_INDEX;
//
// If the page has not since been faulted in, re-enqueue to modified list
// (since page has failed the write to pagefile, cannnot be enqueued to
// standby list)
//
if (PFNtoWrite->statusBits != ACTIVE) {
PFNtoWrite->remodifiedBit = 0;
wakeModifiedWriter = enqueuePage(&modifiedListHead, PFNtoWrite);
} else {
PFNtoWrite->remodifiedBit = 1;
}
releaseJLock(&PFNtoWrite->lockBits);
PRINT("[modifiedPageWriter] error writing out page\n");
return FALSE;
}
//
// If page has been re-modified
//
if (PFNtoWrite->remodifiedBit == 1) {