home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / wqa0495.exe / PHYSMEM.C < prev    next >
C/C++ Source or Header  |  1995-04-01  |  2KB  |  85 lines

  1. #include <windows.h>
  2.  
  3. LPVOID AllocatePhysicalMemoryPtr( DWORD physAddress, DWORD len )
  4. {
  5.     DWORD   linearMappingAddress;
  6.     WORD    mappingSelector;
  7.     WORD    ourDS;
  8.  
  9.     __asm   mov     [ourDS], ds
  10.  
  11.     mappingSelector = AllocSelector( ourDS );
  12.     if ( !mappingSelector )
  13.         return 0;
  14.  
  15.     __asm   mov     ax, 0800h
  16.     __asm   mov     bx, word ptr [physAddress+2]
  17.     __asm   mov     cx, word ptr [physAddress]
  18.     __asm   mov     si, word ptr [len+2]
  19.     __asm   mov     di, word ptr [len]
  20.     __asm   int     31h
  21.     __asm   jc      error
  22.     __asm   mov     word ptr [linearMappingAddress+2], bx
  23.     __asm   mov     word ptr [linearMappingAddress], cx
  24.         
  25.     SetSelectorBase(  mappingSelector, linearMappingAddress );
  26.     SetSelectorLimit( mappingSelector, len );
  27.     
  28.     return MAKELP( mappingSelector, 0 );
  29.     
  30. error:
  31.     FreeSelector( mappingSelector );
  32.     return 0;
  33. }
  34.  
  35. void FreePhysicalMemoryPtr( LPVOID physPtr )
  36. {
  37.     DWORD   linearMappingAddress = GetSelectorBase( SELECTOROF(physPtr) );
  38.  
  39.     __asm   mov     ax, 0801h
  40.     __asm   mov     bx, word ptr [linearMappingAddress+2]
  41.     __asm   mov     cx, word ptr [linearMappingAddress]
  42.     __asm   int     31h
  43.  
  44.     FreeSelector( SELECTOROF(physPtr) );
  45. }
  46.  
  47. DWORD GetCR3(void);     // A helper function prototype
  48.  
  49. int PASCAL WinMain( HANDLE h, HANDLE hPrev, LPSTR lpszCmdLine, int nCmdShow )
  50. {
  51.     DWORD pageDirBasePhys;
  52.     LPDWORD lpPageDirEntry;
  53.     LPDWORD lpPageTableEntry;
  54.     char szBuffer[1024];
  55.     unsigned i;
  56.     LPSTR lpsz;
  57.  
  58.     pageDirBasePhys = GetCR3();
  59.  
  60.     lpPageDirEntry = AllocatePhysicalMemoryPtr( pageDirBasePhys, 0x1000 );
  61.     
  62.     lpPageTableEntry
  63.         = AllocatePhysicalMemoryPtr( *lpPageDirEntry & 0xFFFFF000, 0x1000 );
  64.     
  65.     FreePhysicalMemoryPtr( lpPageDirEntry );
  66.  
  67.     lpsz = szBuffer + wsprintf(szBuffer, "Linear     -> Physical\r\n" );
  68.     
  69.     for ( i = 0; i < 256; i+= 16 )
  70.     {
  71.         lpsz+= wsprintf(lpsz, "%08lX -> %08lX  %s\r\n",
  72.                         (DWORD)i * 0x1000,                  // Linear
  73.                         *lpPageTableEntry & 0xFFFFF000,     // Physical
  74.                         *lpPageTableEntry & 1 ? "present" : "not present");
  75.         lpPageTableEntry += 16;
  76.     }
  77.  
  78.     FreePhysicalMemoryPtr( lpPageTableEntry );
  79.  
  80.     MessageBox( 0, szBuffer, "PHYSMEM", MB_OK );
  81.  
  82.     return 0;
  83. }
  84.  
  85.