:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / utils / pice / module / hooks.c
1 /*++
2
3 Copyright (c) 1998-2001 Klaus P. Gerlicher
4
5 Module Name:
6
7     hooks.c
8
9 Abstract:
10
11     hooking of interrupts
12
13 Environment:
14
15     Kernel mode only
16
17 Author:
18
19     Klaus P. Gerlicher
20
21 Revision History:
22
23     16-Jul-1998:        created
24     15-Nov-2000:    general cleanup of source files
25
26 Copyright notice:
27
28   This file may be distributed under the terms of the GNU Public License.
29
30 --*/
31
32 ////////////////////////////////////////////////////
33 // INCLUDES
34 ////
35 #include "remods.h"
36 #include "precomp.h"
37
38 ////////////////////////////////////////////////////
39 // PROTOTYPES
40 ////
41 void DeinstallHooks(void);
42
43 ////////////////////////////////////////////////////
44 // DEFINES
45 ////
46
47 ////////////////////////////////////////////////////
48 // GLOBALS
49 ////
50
51 // IDT entries
52 //PIDTENTRY pidt[256];
53 IDTENTRY oldidt[256]={{0},};
54
55 IDTENTRY idt_snapshot[256]={{0},};
56
57 // processor flag for interrupt suspension
58 ULONG ulOldFlags;
59
60 ////////////////////////////////////////////////////
61 // PROCEDURES
62 ////
63
64 //*************************************************************************
65 // MaskIrqs()
66 //
67 //*************************************************************************
68 void MaskIrqs(void)
69 {
70     ENTER_FUNC();
71
72     save_flags(ulOldFlags);
73     cli();
74
75     LEAVE_FUNC();
76 }
77
78 //*************************************************************************
79 // UnmaskIrqs()
80 //
81 //*************************************************************************
82 void UnmaskIrqs(void)
83 {
84     ENTER_FUNC();
85
86     restore_flags(ulOldFlags);
87
88     LEAVE_FUNC();
89 }
90
91 //*************************************************************************
92 // SetGlobalInt()
93 //
94 //*************************************************************************
95 ULONG SetGlobalInt(ULONG dwInt,ULONG NewIntHandler)
96 {
97     ULONG idt[2];
98     ULONG OldIntHandler;
99     struct IdtEntry* pidt;
100     struct IdtEntry oldidt;
101
102     ENTER_FUNC();
103
104         // get linear location of IDT
105         __asm__("sidt %0":"=m" (idt));
106
107         // get pointer to idte for int 3
108         pidt=((struct IdtEntry*)((idt[1]<<16)|((idt[0]>>16)&0x0000FFFF)))+dwInt;
109
110         oldidt=*pidt;
111
112     // set new handler address
113         pidt->HiOffset=(USHORT)(((ULONG)NewIntHandler)>>16);
114         pidt->LoOffset=(USHORT)(((ULONG)NewIntHandler)&0x0000FFFF);
115
116     DPRINT((0,"new INT(%0.2x) handler = %0.4x:%x\n",dwInt,pidt->SegSel,(pidt->HiOffset<<16)|(pidt->LoOffset&0x0000FFFF)));
117
118         OldIntHandler=(oldidt.HiOffset<<16)|(oldidt.LoOffset&0x0000FFFF);
119
120     DPRINT((0,"old INT(%0.2x) handler = %0.4x:%x\n",dwInt,pidt->SegSel,OldIntHandler));
121
122     LEAVE_FUNC();
123
124         return OldIntHandler;
125 }
126
127 //*************************************************************************
128 // TakeIdtSnapshot()
129 //
130 //*************************************************************************
131 void TakeIdtSnapshot(void)
132 {
133     ULONG idt[2],i;
134     struct IdtEntry* pidt;
135
136         __asm__("sidt %0":"=m" (idt));
137
138         // get pointer to idte for int 3
139         pidt=((struct IdtEntry*)((idt[1]<<16)|((idt[0]>>16)&0x0000FFFF)));
140
141     for(i=0;i<256;i++)
142     {
143         DPRINT((11,"TakeIdtSnapShot(): saving vector %u\n",i));
144         if(IsRangeValid((ULONG)pidt,sizeof(*pidt)) )
145         {
146             DPRINT((11,"TakeIdtSnapShot(): vector %u valid\n",i));
147             idt_snapshot[i] = *pidt++;
148         }
149     }
150 }
151
152 //*************************************************************************
153 // RestoreIdt()
154 //
155 //*************************************************************************
156 void RestoreIdt(void)
157 {
158     ULONG idt[2],i;
159     struct IdtEntry* pidt;
160
161         __asm__("sidt %0":"=m" (idt));
162
163     // get pointer to idte for int 3
164         pidt=((struct IdtEntry*)((idt[1]<<16)|((idt[0]>>16)&0x0000FFFF)));
165
166     for(i=0;i<256;i++)
167     {
168         DPRINT((11,"TakeIdtSnapShot(): restoring vector %u\n",i));
169         if(IsRangeValid((ULONG)pidt,sizeof(*pidt)) )
170         {
171             DPRINT((11,"TakeIdtSnapShot(): vector %u valid\n",i));
172             *pidt++ = idt_snapshot[i];
173         }
174     }
175 }
176
177 // EOF