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

  1. #include <windows.h>
  2. #include "descript.h"
  3.  
  4. BOOL FAR PASCAL __export DllEntryPoint (DWORD dwReason,
  5.                                WORD  hInst,
  6.                                WORD  wDS,
  7.                                WORD  wHeapSize,
  8.                                DWORD dwReserved1,
  9.                                WORD  wReserved2);
  10.  
  11. BOOL FAR PASCAL phystk_ThunkConnect16(  LPSTR pszDll16,
  12.                                         LPSTR pszDll32,
  13.                                         WORD  hInst,
  14.                                         DWORD dwReason);
  15.  
  16. int FAR PASCAL LibMain (HANDLE hInstance,
  17.                         WORD   wDataSeg,
  18.                         WORD   wHeapSize,
  19.                         LPSTR  lpszCmdLine)
  20. {
  21.     if (wHeapSize != 0)   // If DLL data seg is MOVEABLE
  22.         UnlockData (0);
  23.  
  24.     return (1);
  25. }
  26.  
  27. BOOL FAR PASCAL __export DllEntryPoint (DWORD dwReason,
  28.                                WORD  hInst,
  29.                                WORD  wDS,
  30.                                WORD  wHeapSize,
  31.                                DWORD dwReserved1,
  32.                                WORD  wReserved2)
  33. {
  34.     OutputDebugString("In 16bit DllEntryPoint: Calling phystk_ThunkConnect16\r\n");
  35.     if (!phystk_ThunkConnect16( "PHYS16.DLL",
  36.                                 "PHYS32.DLL",
  37.                                 hInst,
  38.                                 dwReason))
  39.     {
  40.         OutputDebugString("In 16bit DllEntryPoint: phystk_ThunkConnect16 ret FALSE\r\n");
  41.         return FALSE;
  42.     }
  43.  
  44.     OutputDebugString("In 16bit DllEntryPoint: phystk_ThunkConnect16 ret TRUE\r\n");
  45.     return TRUE;
  46. }
  47.  
  48. LPVOID AllocatePhysicalMemoryPtr( DWORD physAddress, DWORD len )
  49. {
  50.     DWORD   linearMappingAddress;
  51.     WORD    mappingSelector;
  52.     WORD    ourDS;
  53.  
  54.     __asm   mov     [ourDS], ds
  55.  
  56.     mappingSelector = AllocSelector( ourDS );
  57.     if ( !mappingSelector )
  58.         return 0;
  59.  
  60.     __asm   mov     ax, 0800h
  61.     __asm   mov     bx, word ptr [physAddress+2]
  62.     __asm   mov     cx, word ptr [physAddress]
  63.     __asm   mov     si, word ptr [len+2]
  64.     __asm   mov     di, word ptr [len]
  65.     __asm   int     31h
  66.     __asm   jc      error
  67.     __asm   mov     word ptr [linearMappingAddress+2], bx
  68.     __asm   mov     word ptr [linearMappingAddress], cx
  69.         
  70.     SetSelectorBase(  mappingSelector, linearMappingAddress );
  71.     SetSelectorLimit( mappingSelector, len );
  72.     
  73.     return MAKELP( mappingSelector, 0 );
  74.     
  75. error:
  76.     FreeSelector( mappingSelector );
  77.     return 0;
  78. }
  79.  
  80. void FreePhysicalMemoryPtr( LPVOID physPtr )
  81. {
  82.     DWORD   linearMappingAddress = GetSelectorBase( SELECTOROF(physPtr) );
  83.  
  84.     __asm   mov     ax, 0801h
  85.     __asm   mov     bx, word ptr [linearMappingAddress+2]
  86.     __asm   mov     cx, word ptr [linearMappingAddress]
  87.     __asm   int     31h
  88.  
  89.     FreeSelector( SELECTOROF(physPtr) );
  90. }
  91.  
  92. DWORD GetCR3(void);     // A helper function prototype
  93.  
  94. DWORD WINAPI __export GetPhysicalAddrFromLinear( DWORD linear )
  95. {
  96.     DWORD pageDirBasePhys;
  97.     LPDWORD lpPageDirBase;
  98.     LPDWORD lpPageDir;
  99.     LPDWORD lpPageTableBase;
  100.     LPDWORD lpPageTable;
  101.     DWORD retValue;
  102.  
  103.     pageDirBasePhys = GetCR3();
  104.     lpPageDirBase = AllocatePhysicalMemoryPtr( pageDirBasePhys, 0x1000 );
  105.     lpPageDir = &lpPageDirBase[ linear >> 22 ];
  106.     lpPageTableBase
  107.         = AllocatePhysicalMemoryPtr( *lpPageDir & 0xFFFFF000, 0x1000 );
  108.     FreePhysicalMemoryPtr( lpPageDirBase );
  109.     lpPageTable = &lpPageTableBase[ (linear >> 12) & 0x000003FF ];
  110.     retValue = (*lpPageTable & 0xFFFFF000) + (linear & 0x00000FFF);
  111.     FreePhysicalMemoryPtr( lpPageTableBase );
  112.     return retValue;
  113. }
  114.  
  115. DWORD WINAPI __export GetPageAttributes( DWORD linear )
  116. {
  117.     DWORD pageDirBasePhys;
  118.     LPDWORD lpPageDirBase;
  119.     LPDWORD lpPageDir;
  120.     LPDWORD lpPageTableBase;
  121.     LPDWORD lpPageTable;
  122.     DWORD retValue;
  123.     
  124.     pageDirBasePhys = GetCR3();
  125.     lpPageDirBase = AllocatePhysicalMemoryPtr( pageDirBasePhys, 0x1000 );
  126.     lpPageDir = &lpPageDirBase[ linear >> 22 ];
  127.     lpPageTableBase
  128.         = AllocatePhysicalMemoryPtr( *lpPageDir & 0xFFFFF000, 0x1000 );
  129.     FreePhysicalMemoryPtr( lpPageDirBase );
  130.     lpPageTable = &lpPageTableBase[ (linear >> 12) & 0x000003FF ];
  131.     retValue = *lpPageTable & 0x00000FFF;
  132.     FreePhysicalMemoryPtr( lpPageTableBase );
  133.     return retValue;
  134. }
  135.