2 Supporting functions for IA32 architecture.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "DebugAgent.h"
12 Initialize IDT entries to support source level debug.
20 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
21 UINTN InterruptHandler
;
22 IA32_DESCRIPTOR IdtDescriptor
;
27 AsmReadIdtr (&IdtDescriptor
);
30 // Use current CS as the segment selector of interrupt gate in IDT
32 CodeSegment
= AsmReadCs ();
34 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
36 for (Index
= 0; Index
< 20; Index
++) {
37 if (((PcdGet32 (PcdExceptionsIgnoredByDebugger
) & ~(BIT1
| BIT3
)) & (1 << Index
)) != 0) {
39 // If the exception is masked to be reserved except for INT1 and INT3, skip it
43 InterruptHandler
= (UINTN
)&Exception0Handle
+ Index
* ExceptionStubHeaderSize
;
44 IdtEntry
[Index
].Bits
.OffsetLow
= (UINT16
)(UINTN
)InterruptHandler
;
45 IdtEntry
[Index
].Bits
.OffsetHigh
= (UINT16
)((UINTN
)InterruptHandler
>> 16);
46 IdtEntry
[Index
].Bits
.Selector
= CodeSegment
;
47 IdtEntry
[Index
].Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
50 InterruptHandler
= (UINTN
) &TimerInterruptHandle
;
51 IdtEntry
[DEBUG_TIMER_VECTOR
].Bits
.OffsetLow
= (UINT16
)(UINTN
)InterruptHandler
;
52 IdtEntry
[DEBUG_TIMER_VECTOR
].Bits
.OffsetHigh
= (UINT16
)((UINTN
)InterruptHandler
>> 16);
53 IdtEntry
[DEBUG_TIMER_VECTOR
].Bits
.Selector
= CodeSegment
;
54 IdtEntry
[DEBUG_TIMER_VECTOR
].Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
57 // If the CPU supports Debug Extensions(CPUID:01 EDX:BIT2), then
58 // Set DE flag in CR4 to enable IO breakpoint
60 AsmCpuid (1, NULL
, NULL
, NULL
, &RegEdx
);
61 if ((RegEdx
& BIT2
) != 0) {
62 AsmWriteCr4 (AsmReadCr4 () | BIT3
);
67 Retrieve exception handler from IDT table by ExceptionNum.
69 @param[in] ExceptionNum Exception number
71 @return Exception handler
75 GetExceptionHandlerInIdtEntry (
79 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
80 IA32_DESCRIPTOR IdtDescriptor
;
82 AsmReadIdtr (&IdtDescriptor
);
83 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
85 return (VOID
*) (((UINTN
)IdtEntry
[ExceptionNum
].Bits
.OffsetLow
) |
86 (((UINTN
)IdtEntry
[ExceptionNum
].Bits
.OffsetHigh
) << 16));
90 Set exception handler in IDT table by ExceptionNum.
92 @param[in] ExceptionNum Exception number
93 @param[in] ExceptionHandler Exception Handler to be set
97 SetExceptionHandlerInIdtEntry (
98 IN UINTN ExceptionNum
,
99 IN VOID
*ExceptionHandler
102 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
103 IA32_DESCRIPTOR IdtDescriptor
;
105 AsmReadIdtr (&IdtDescriptor
);
106 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) IdtDescriptor
.Base
;
108 IdtEntry
[ExceptionNum
].Bits
.OffsetLow
= (UINT16
)(UINTN
)ExceptionHandler
;
109 IdtEntry
[ExceptionNum
].Bits
.OffsetHigh
= (UINT16
)((UINTN
)ExceptionHandler
>> 16);