RDFU  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
IdtGdt.c
Go to the documentation of this file.
1 
12 #include "EfiScannerHeaders.h"
13 #include "EfiScannerLib.h"
14 
15 
16 
17 typedef struct _gdt_type_text
18 {
19  char text[64];
21 
22 
27  { "Data Read-Only" },
28  { "Data Read-Only, accessed" },
29  { "Data Read/Write" },
30  { "Data Read/Write, accessed" },
31  { "Data Read-Only, expand-down" },
32  { "Data Read-Only, expand-down, accessed" },
33  { "Data Read/Write, expand-down" },
34  { "Data Read/Write, expand-down, accessed" },
35  { "Code Execute-Only" },
36  { "Code Execute-Only, accessed" },
37  { "Code Execute/Read" },
38  { "Code Execute/Read, accessed" },
39  { "Code Execute-Only, conforming" },
40  { "Code Execute-Only, conforming, accessed" },
41  { "Code Execute/Read, conforming" },
42  { "Code Execute/Read, conforming, accessed" },
43  { 0 }
44 };
45 
46 
57 {
58  UINTN i;
59  UINT_ARCH GdtBase, EntryBase;
60  USHORT GdtLimit;
61  ULONG EntryLimit;
62  gdt_entry *GdtEntry;
63 
64  GdtBase = ASMGetGdtBase();
65  GdtLimit = ASMGetGdtLimit();
66 
67  LogFullAscii("DumpGDT: base = %016lx Limit=%04x GdEntrySize=%x\r\n",
68  (MAX_PTR)GdtBase,
69  GdtLimit,
70  sizeof(gdt_entry));
71 
72  GdtEntry = (gdt_entry*)GdtBase;
73  for (i = 0; i < (GdtLimit/sizeof(gdt_entry)); i++, GdtEntry++)
74  {
75 
76  EntryBase = GdtEntry->s.Base0 | (GdtEntry->s.Base1 << 16) | (GdtEntry->s.Base2 << 24);
77  EntryLimit = GdtEntry->s.LimitLow | (GdtEntry->s.LimitHigh << 16);
78 
79 
80  LogFullAscii("%04d : base=%08x limit=%08x System=%02d DPL=%02d Present=%02d type=(%03d)%a\r\n",
81  i,
82  EntryBase,
83  EntryLimit,
84  GdtEntry->s.System,
85  GdtEntry->s.DPL,
86  GdtEntry->s.Present,
87  GdtEntry->s.Type,
88  &GdtTypeText[GdtEntry->s.Type]);
89 
90 
91  }
92 
93  return EFI_SUCCESS;
94 }
95 
96 
107 {
108  idt_entry *IdtEntry;
109  IdtEntry = (idt_entry*)ASMGetIdtBase();
110  return &IdtEntry[IdtVectorNum];
111 }
112 
113 
114 
115 #if defined(_WIN64)
116 #define IDT_GET_BASE(IdtE) (UINT_ARCH)(IdtE->OffsetLow | (IdtE->OffsetHigh << 16) | ((UINT_ARCH)IdtE->HighOffset64 << 32))
117 #else
118 #define IDT_GET_BASE(IdtE) (UINT_ARCH)(IdtE->OffsetLow | (IdtE->OffsetHigh << 16))
119 #endif
120 
121 
122 
137 EFI_STATUS IdtHookInterrupt(C_PTR NewIntHandlerAddr, ULONG IdtVectorNum, C_PTR *OutOldIntAddr)
138 {
139  UINT_ARCH OldBase;
140  idt_entry *IdtEntry;
141 
142  IdtEntry = GetIdtEntry(IdtVectorNum);
143  OldBase = IDT_GET_BASE(IdtEntry);
144 
145  *OutOldIntAddr = OldBase;
146 
147 #if DEBUG_ON == 1
148  LogFullAscii("%a: Interrupt: %d OldBase=%016lx NewBase=%016lx\r\n",
149  __FUNCTION__,
150  IdtVectorNum,
151  (MAX_PTR)OldBase,
152  (MAX_PTR)NewIntHandlerAddr);
153 #endif
154 
155  IdtEntry->OffsetHigh = (USHORT)(NewIntHandlerAddr >> 16);
156  IdtEntry->OffsetLow = (USHORT)(NewIntHandlerAddr & 0xFFFF);
157 
158 #if defined(_WIN64)
159  IdtEntry->HighOffset64 = (ULONG)((ULONG64)NewIntHandlerAddr >> 32);
160 #endif
161 
162 
163 #if DEBUG_ON == 1
164  LogFullAscii("IdtHookInterrupt: hooked!\r\n");
165 #endif
166 
167  return EFI_SUCCESS;
168 }
169 
170 
172 {
173  ULONG64 OldAddr;
174 
175  IdtHookInterrupt((C_PTR)&Int03Handler, 3, (C_PTR*)&OldAddr);
176  ASMDbgBreak();
177 
178  return EFI_SUCCESS;
179 }
180 
181 
192 {
193 
194  UINTN i;
195  UINT_ARCH IdtBase, EntryBase;
196  USHORT IdtLimit;
197  idt_entry *IdtEntry;
198 
199  IdtBase = ASMGetIdtBase();
200  IdtLimit = ASMGetIdtLimit();
201 
202  IdtEntry = (idt_entry*)IdtBase;
203 
204 
205  LogFullAscii("%a: base = %016lx Limit=%04x GdEntrySize=%x\r\n",
206  __FUNCTION__,
207  (MAX_PTR)IdtBase,
208  IdtLimit,
209  sizeof(idt_entry));
210 
211 
212  for (i = 0; i < (IdtLimit/sizeof(idt_entry)); i++, IdtEntry++)
213  {
214 
215  EntryBase = IDT_GET_BASE(IdtEntry);
216 
217  LogFullAscii("%04d : base=%016lx Selector=%04x DPL=%02d Present=%02d Type=%04x\r\n",
218  i,
219  (MAX_PTR)EntryBase,
220  IdtEntry->SegmentSelector,
221  IdtEntry->Dpl,
222  IdtEntry->Present,
223  IdtEntry->Type);
224 
225  }
226 
227 
228 
229  return EFI_SUCCESS;
230 }