Leaked source code of windows server 2003
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.

153 lines
3.2 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #define ONE_MB (1024*1024)
  5. #define TWO_MB (2*ONE_MB)
  6. #define PAGE_SIZE (4096)
  7. #define PAGESPERTWO_MB (TWO_MB/PAGE_SIZE)
  8. #define TWO_MBREGIONS (8)
  9. #define NEW_CELL(id,i) ( (id)<<20 | ((i)&0x000fffff) )
  10. CRITICAL_SECTION ErrorCrit;
  11. PUCHAR RegionBase;
  12. DWORD
  13. SelectPage(
  14. void
  15. )
  16. {
  17. DWORD PageNum;
  18. PageNum = GetTickCount();
  19. PageNum = PageNum >> 3;
  20. PageNum = PageNum & (PAGESPERTWO_MB-1);
  21. return PageNum;
  22. }
  23. void
  24. CellError(
  25. DWORD TwoMegRegion,
  26. DWORD *Address,
  27. DWORD ThreadId,
  28. DWORD OriginalCell,
  29. DWORD CurrentCell,
  30. DWORD Iteration
  31. )
  32. {
  33. EnterCriticalSection(&ErrorCrit);
  34. printf("PAGEIT: Cell Error at %x %p %02lx %08x vs %08x (iter %d)\n",
  35. TwoMegRegion,
  36. Address,
  37. ThreadId,
  38. OriginalCell,
  39. CurrentCell,
  40. Iteration
  41. );
  42. DebugBreak();
  43. LeaveCriticalSection(&ErrorCrit);
  44. }
  45. void
  46. PrintHeartBeat(
  47. DWORD Id,
  48. DWORD Counter
  49. )
  50. {
  51. EnterCriticalSection(&ErrorCrit);
  52. printf("PAGEIT: HeartBeat Id %3d iter %8d\n",
  53. Id,
  54. Counter
  55. );
  56. LeaveCriticalSection(&ErrorCrit);
  57. }
  58. #if _MSC_FULL_VER >= 13008827
  59. #pragma warning(push)
  60. #pragma warning(disable:4715) // Not all control paths return (due to infinite loop)
  61. #endif
  62. DWORD
  63. ThreadRoutine(
  64. PVOID Unused
  65. )
  66. {
  67. DWORD Id;
  68. DWORD Counter;
  69. DWORD PageNumber;
  70. DWORD CellOffset;
  71. DWORD *CellAddress;
  72. DWORD OriginalCell;
  73. DWORD NewCell;
  74. DWORD i;
  75. SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_LOWEST);
  76. Id = GetCurrentThreadId();
  77. Counter = 0;
  78. for(;;) {
  79. PageNumber = SelectPage();
  80. CellOffset = PageNumber*PAGE_SIZE + Id*sizeof(DWORD);
  81. CellAddress = (DWORD *)(RegionBase + CellOffset);
  82. OriginalCell = *CellAddress;
  83. NewCell = NEW_CELL(Id,Counter);
  84. for(i=0;i<TWO_MBREGIONS;i++) {
  85. CellAddress = (DWORD *)(RegionBase + i*TWO_MB + CellOffset);
  86. if ( OriginalCell != *CellAddress ) {
  87. CellError(i,CellAddress,Id,OriginalCell,*CellAddress,Counter);
  88. }
  89. *CellAddress = NewCell;
  90. }
  91. Counter++;
  92. if ( (Counter/50) * 50 == Counter ) {
  93. Sleep(500);
  94. }
  95. if ( (Counter/1024) * 1024 == Counter ) {
  96. PrintHeartBeat(Id,Counter);
  97. }
  98. }
  99. return 0;
  100. }
  101. #if _MSC_FULL_VER >= 13008827
  102. #pragma warning(pop)
  103. #endif
  104. DWORD
  105. _cdecl
  106. main(
  107. int argc,
  108. char *argv[],
  109. char *envp[]
  110. )
  111. {
  112. DWORD i;
  113. DWORD Id;
  114. HANDLE Thread;
  115. InitializeCriticalSection(&ErrorCrit);
  116. RegionBase = VirtualAlloc(NULL,TWO_MBREGIONS*TWO_MB,MEM_COMMIT,PAGE_READWRITE);
  117. if ( !RegionBase ) {
  118. printf("PAGEIT: VirtualAlloc Failed %d\n",GetLastError());
  119. ExitProcess(1);
  120. }
  121. for (i=0;i<(TWO_MBREGIONS-1);i++){
  122. Thread = CreateThread(NULL,0,ThreadRoutine,NULL,0,&Id);
  123. if ( Thread ) {
  124. CloseHandle(Thread);
  125. }
  126. }
  127. ThreadRoutine(NULL);
  128. return 0;
  129. }