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

  1. #include <windows.h>
  2. #pragma pack(1)
  3. #include "descript.h"
  4. #pragma pack()
  5.  
  6. static char MS_DOS_STR[] = "MS-DOS";
  7.  
  8. unsigned short GetLDTAlias(void)
  9. {
  10.     unsigned short  LDT_alias;
  11.     unsigned short  (far * dpmiproc)(void);
  12.     _asm     mov     si, offset MS_DOS_STR   // DS:SI = "MS-DOS"
  13.     _asm     mov     ax, 168Ah
  14.     _asm     int     2Fh
  15.     _asm     cmp     al, 8Ah
  16.     _asm     je      extensions_not_found
  17.     _asm     mov     word ptr [dpmiproc], di
  18.     _asm     mov     word ptr [dpmiproc+2], es
  19.     _asm     mov     ax, 100h
  20.     LDT_alias = dpmiproc();
  21.     _asm     jc      extensions_not_found;
  22.     return  LDT_alias;
  23. extensions_not_found:   // We get here if something failed
  24.     return  0;
  25. }
  26.  
  27. DWORD CreateCallgate(void far * func_address, unsigned params)
  28. {
  29.     CALLGATE_DESCRIPTOR far *callgate_desc;
  30.     CODE_SEG_DESCRIPTOR far *ring0_desc;
  31.     unsigned short ldt_alias;
  32.     unsigned short ring_0_alias;
  33.     unsigned short callgate_selector;
  34.     if ( (ldt_alias = GetLDTAlias()) == 0 )
  35.         return 0;
  36.     if ((ring_0_alias = AllocSelector(HIWORD(func_address)))==0)
  37.         return 0;
  38.     ring0_desc = MAKELP( ldt_alias, ring_0_alias & 0xFFF8 );
  39.     ring0_desc->dpl = 0;
  40.     if ( (callgate_selector= AllocSelector(0)) == 0 )
  41.     {
  42.         FreeSelector( ring_0_alias );
  43.         return 0;
  44.     }
  45.     callgate_desc = MAKELP( ldt_alias, callgate_selector & 0xFFF8 );
  46.     callgate_desc->offset_0_15 = LOWORD( func_address );
  47.     callgate_desc->selector = ring_0_alias;
  48.     callgate_desc->param_count = params;
  49.     callgate_desc->some_bits = 0;
  50.     callgate_desc->type = 4;            // 286 call gate
  51.     callgate_desc->app_system = 0;      // A system descriptor
  52.     callgate_desc->dpl = 3;             // Ring 3 code can call
  53.     callgate_desc->present = 1;
  54.     callgate_desc->offset_16_31 = 0;
  55.     return (DWORD)MAKELP( callgate_selector, 0 );
  56. }
  57.  
  58. void FreeCallgate( DWORD callgate_ptr )
  59. {
  60.     CALLGATE_DESCRIPTOR far *callgate_desc;
  61.     unsigned short ldt_alias;
  62.  
  63.     if ( (ldt_alias = GetLDTAlias()) == 0 )
  64.         return;
  65.     callgate_desc = MAKELP( ldt_alias, HIWORD(callgate_ptr) & 0xFFF8 );
  66.     FreeSelector( callgate_desc->selector );
  67.     FreeSelector( HIWORD(callgate_ptr) );
  68. }
  69.  
  70. DWORD far _GetCR3(void);
  71.  
  72. DWORD GetCR3(void)
  73. {
  74.     typedef DWORD (FAR PASCAL * DWORD_FARPROC)(void);
  75.     DWORD_FARPROC lpfnGetCR3;
  76.     DWORD cr3;
  77.     WORD ourCS;
  78.     __asm   mov [ourCS], cs
  79.     GlobalPageLock(ourCS);
  80.     lpfnGetCR3 = (DWORD_FARPROC)CreateCallgate( _GetCR3, 0 );
  81.     if ( !lpfnGetCR3 )
  82.         return 0;
  83.     _asm    cli
  84.     cr3 = lpfnGetCR3();
  85.     _asm    sti
  86.     GlobalPageUnlock(ourCS);
  87.     FreeCallgate( (DWORD)lpfnGetCR3 );
  88.     return cr3; 
  89. }
  90.