2 CPU exception handler library implemenation for DXE modules.
4 Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "CpuExceptionCommon.h"
11 #include <Library/DebugLib.h>
12 #include <Library/MemoryAllocationLib.h>
13 #include <Library/UefiBootServicesTableLib.h>
15 CONST UINTN mDoFarReturnFlag
= 0;
17 RESERVED_VECTORS_DATA mReservedVectorsData
[CPU_INTERRUPT_NUM
];
18 EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable
[CPU_INTERRUPT_NUM
];
19 EXCEPTION_HANDLER_DATA mExceptionHandlerData
= {
23 mExternalInterruptHandlerTable
26 UINT8 mNewStack
[CPU_STACK_SWITCH_EXCEPTION_NUMBER
*
27 CPU_KNOWN_GOOD_STACK_SIZE
];
28 UINT8 mNewGdt
[CPU_TSS_GDT_SIZE
];
31 Common exception handler.
33 @param ExceptionType Exception type.
34 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
38 CommonExceptionHandler (
39 IN EFI_EXCEPTION_TYPE ExceptionType
,
40 IN EFI_SYSTEM_CONTEXT SystemContext
43 CommonExceptionHandlerWorker (ExceptionType
, SystemContext
, &mExceptionHandlerData
);
47 Initializes all CPU exceptions entries and provides the default exception handlers.
49 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
50 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
51 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
52 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
54 @param[in] VectorInfo Pointer to reserved vector list.
56 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
57 with default exception handlers.
58 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
59 @retval EFI_UNSUPPORTED This function is not supported.
64 InitializeCpuExceptionHandlers (
65 IN EFI_VECTOR_HANDOFF_INFO
*VectorInfo OPTIONAL
68 InitializeSpinLock (&mExceptionHandlerData
.DisplayMessageSpinLock
);
69 return InitializeCpuExceptionHandlersWorker (VectorInfo
, &mExceptionHandlerData
);
73 Registers a function to be called from the processor interrupt handler.
75 This function registers and enables the handler specified by InterruptHandler for a processor
76 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
77 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
78 The installed handler is called once for each processor interrupt or exception.
79 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
80 otherwise EFI_UNSUPPORTED returned.
82 @param[in] InterruptType Defines which interrupt or exception to hook.
83 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
84 when a processor interrupt occurs. If this parameter is NULL, then the handler
87 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
88 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
90 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
92 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
93 or this function is not supported.
97 RegisterCpuInterruptHandler (
98 IN EFI_EXCEPTION_TYPE InterruptType
,
99 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
102 return RegisterCpuInterruptHandlerWorker (InterruptType
, InterruptHandler
, &mExceptionHandlerData
);
106 Setup separate stacks for certain exception handlers.
108 InitData is optional and processor arch dependent.
110 @param[in] InitData Pointer to data optional for information about how
111 to assign stacks for certain exception handlers.
113 @retval EFI_SUCCESS The stacks are assigned successfully.
114 @retval EFI_UNSUPPORTED This function is not supported.
119 InitializeSeparateExceptionStacks (
120 IN CPU_EXCEPTION_INIT_DATA
*InitData OPTIONAL
123 CPU_EXCEPTION_INIT_DATA EssData
;
124 IA32_DESCRIPTOR Idtr
;
125 IA32_DESCRIPTOR Gdtr
;
127 if (InitData
== NULL
) {
128 SetMem (mNewGdt
, sizeof (mNewGdt
), 0);
133 EssData
.X64
.Revision
= CPU_EXCEPTION_INIT_DATA_REV
;
134 EssData
.X64
.KnownGoodStackTop
= (UINTN
)mNewStack
+ sizeof (mNewStack
);
135 EssData
.X64
.KnownGoodStackSize
= CPU_KNOWN_GOOD_STACK_SIZE
;
136 EssData
.X64
.StackSwitchExceptions
= CPU_STACK_SWITCH_EXCEPTION_LIST
;
137 EssData
.X64
.StackSwitchExceptionNumber
= CPU_STACK_SWITCH_EXCEPTION_NUMBER
;
138 EssData
.X64
.IdtTable
= (VOID
*)Idtr
.Base
;
139 EssData
.X64
.IdtTableSize
= Idtr
.Limit
+ 1;
140 EssData
.X64
.GdtTable
= mNewGdt
;
141 EssData
.X64
.GdtTableSize
= sizeof (mNewGdt
);
142 EssData
.X64
.ExceptionTssDesc
= mNewGdt
+ Gdtr
.Limit
+ 1;
143 EssData
.X64
.ExceptionTssDescSize
= CPU_TSS_DESC_SIZE
;
144 EssData
.X64
.ExceptionTss
= mNewGdt
+ Gdtr
.Limit
+ 1 + CPU_TSS_DESC_SIZE
;
145 EssData
.X64
.ExceptionTssSize
= CPU_TSS_SIZE
;
150 return ArchSetupExceptionStack (InitData
);