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

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