home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n04
/
memman.exe
/
GETCR3.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-01
|
3KB
|
90 lines
#include <windows.h>
#pragma pack(1)
#include "descript.h"
#pragma pack()
static char MS_DOS_STR[] = "MS-DOS";
unsigned short GetLDTAlias(void)
{
unsigned short LDT_alias;
unsigned short (far * dpmiproc)(void);
_asm mov si, offset MS_DOS_STR // DS:SI = "MS-DOS"
_asm mov ax, 168Ah
_asm int 2Fh
_asm cmp al, 8Ah
_asm je extensions_not_found
_asm mov word ptr [dpmiproc], di
_asm mov word ptr [dpmiproc+2], es
_asm mov ax, 100h
LDT_alias = dpmiproc();
_asm jc extensions_not_found;
return LDT_alias;
extensions_not_found: // We get here if something failed
return 0;
}
DWORD CreateCallgate(void far * func_address, unsigned params)
{
CALLGATE_DESCRIPTOR far *callgate_desc;
CODE_SEG_DESCRIPTOR far *ring0_desc;
unsigned short ldt_alias;
unsigned short ring_0_alias;
unsigned short callgate_selector;
if ( (ldt_alias = GetLDTAlias()) == 0 )
return 0;
if ((ring_0_alias = AllocSelector(HIWORD(func_address)))==0)
return 0;
ring0_desc = MAKELP( ldt_alias, ring_0_alias & 0xFFF8 );
ring0_desc->dpl = 0;
if ( (callgate_selector= AllocSelector(0)) == 0 )
{
FreeSelector( ring_0_alias );
return 0;
}
callgate_desc = MAKELP( ldt_alias, callgate_selector & 0xFFF8 );
callgate_desc->offset_0_15 = LOWORD( func_address );
callgate_desc->selector = ring_0_alias;
callgate_desc->param_count = params;
callgate_desc->some_bits = 0;
callgate_desc->type = 4; // 286 call gate
callgate_desc->app_system = 0; // A system descriptor
callgate_desc->dpl = 3; // Ring 3 code can call
callgate_desc->present = 1;
callgate_desc->offset_16_31 = 0;
return (DWORD)MAKELP( callgate_selector, 0 );
}
void FreeCallgate( DWORD callgate_ptr )
{
CALLGATE_DESCRIPTOR far *callgate_desc;
unsigned short ldt_alias;
if ( (ldt_alias = GetLDTAlias()) == 0 )
return;
callgate_desc = MAKELP( ldt_alias, HIWORD(callgate_ptr) & 0xFFF8 );
FreeSelector( callgate_desc->selector );
FreeSelector( HIWORD(callgate_ptr) );
}
DWORD far _GetCR3(void);
DWORD GetCR3(void)
{
typedef DWORD (FAR PASCAL * DWORD_FARPROC)(void);
DWORD_FARPROC lpfnGetCR3;
DWORD cr3;
WORD ourCS;
__asm mov [ourCS], cs
GlobalPageLock(ourCS);
lpfnGetCR3 = (DWORD_FARPROC)CreateCallgate( _GetCR3, 0 );
if ( !lpfnGetCR3 )
return 0;
_asm cli
cr3 = lpfnGetCR3();
_asm sti
GlobalPageUnlock(ourCS);
FreeCallgate( (DWORD)lpfnGetCR3 );
return cr3;
}