Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Monitoring NT Debug Services


February 2000/Monitoring NT Debug Services/Listing 3

Listing 3: idt.h — Code to hook interrupt

#ifndef _IDT_H_
#define _IDT_H_

#define    DISABLE_INTS    KIRQL __Dioldirql__; \
                        KeRaiseIrql( HIGH_LEVEL, &__Dioldirql__ ); \
                        _asm    { pushfd    }     \
                        _asm    { cli    }     
                        
#define    ENABLE_INTS        {_asm    popfd    }     \
                        KeLowerIrql(__Dioldirql__);

// NT Uses the following values for it's 32-bit flat selectors
#define NT_CS   (0x8)
#define NT_UCS  (0x1B)
#define NT_DS   (0x23)
#define NT_FS   (0x30)

// x86 specific constants used to TEST where an interrupt originated
#define  X86_VM   (0x20000) // V8086 mode
#define  X86_USER (0x1)     // Selector with bit 1 or 2
                            // set => user mode

#pragma pack( push, PREIDT )
typedef struct NT_IDT
{
    WORD    wLoOfs;        // Low Word of the ISR's offset
    WORD    wSelector;     // ISR's selector....should be 8 under NT
    WORD    wFlags;        // Flags...should almost always be 8E00 
                           // for 32-bit code on NT.
    WORD    wHiOfs;        // Hi Word of ISR's offset

    // This hooks an IDT Entry to point to a new offset.
    // It leaves the current protection settings as they
    // are. Additionally, it returns the flat offset to the
    // old ISR handler.  To set completely new protection 
    // flags use Set() below.

    void Hook( PVOID newOfs, PVOID pOldOfs=NULL )
    {        
        DISABLE_INTS

        if ( pOldOfs != NULL )
            *(PDWORD)pOldOfs =    (wHiOfs<<16) + wLoOfs ;

        wLoOfs    =    WORD(newOfs) ;
        wHiOfs    =    WORD(((DWORD)newOfs)>>16) ;

        ENABLE_INTS
    }

    // This hooks an IDT Entry to a completely new interupt gate.
    // Unlike NT_IDT::Hook(), this explicitly sets the gate's
    // protection, both at the gate level & implicitly with the
    // code selector passed.
    void Set( PVOID ofs, WORD protection=0x8E00, WORD sel=NT_CS) 
    {
    DISABLE_INTS

    wLoOfs        = WORD(ofs) ;
    wSelector    = sel;
        wFlags        = protection;
        wHiOfs        = WORD(((DWORD)ofs)>>16)  ;

        ENABLE_INTS
    }

    NT_IDT operator=(NT_IDT *r_idt)
    {
        DISABLE_INTS

        wLoOfs        = r_idt->wLoOfs;
        wSelector    = r_idt->wSelector;
        wFlags        = r_idt->wFlags; 
        wHiOfs        = r_idt->wHiOfs;

        ENABLE_INTS

        return *this;
    }

    NT_IDT operator=(NT_IDT r_idt)
    {
        DISABLE_INTS

        wLoOfs        = r_idt.wLoOfs;
        wSelector    = r_idt.wSelector;
        wFlags        = r_idt.wFlags; 
        wHiOfs        = r_idt.wHiOfs;

        ENABLE_INTS

        return *this;
    }

    BOOL IsValid() const
    {
        return ( (wHiOfs) && (wSelector==NT_CS) );
    }

} NT_IDT, *PNT_IDT;
#pragma pack( pop, PREIDT )


#pragma warning( disable : 4035 )    // Turn off no return
                                     // value warning

// Get's the  IDT base for the current processor
inline    PNT_IDT    __fastcall GetIDTBase()
{
    __asm {  mov eax, _PCR KPCR.IDT  }    
}
      
#pragma warning( default : 4035 )

#endif
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.