]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
CpuException: Add InitializeSeparateExceptionStacks
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / DxeException.c
CommitLineData
e41aad15
JF
1/** @file\r
2 CPU exception handler library implemenation for DXE modules.\r
3\r
2a09527e 4 Copyright (c) 2013 - 2022, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e41aad15
JF
6\r
7**/\r
8\r
9#include <PiDxe.h>\r
10#include "CpuExceptionCommon.h"\r
11#include <Library/DebugLib.h>\r
12#include <Library/MemoryAllocationLib.h>\r
fceafda5 13#include <Library/UefiBootServicesTableLib.h>\r
e41aad15 14\r
053e878b 15CONST UINTN mDoFarReturnFlag = 0;\r
e41aad15 16\r
2fbc5ff0
RN
17RESERVED_VECTORS_DATA mReservedVectorsData[CPU_INTERRUPT_NUM];\r
18EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];\r
34d50512 19EXCEPTION_HANDLER_DATA mExceptionHandlerData = {\r
2a09527e
RN
20 CPU_INTERRUPT_NUM,\r
21 0, // To be fixed\r
34d50512
RN
22 mReservedVectorsData,\r
23 mExternalInterruptHandlerTable\r
24};\r
e41aad15 25\r
053e878b
MK
26UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *\r
27 CPU_KNOWN_GOOD_STACK_SIZE];\r
28UINT8 mNewGdt[CPU_TSS_GDT_SIZE];\r
0ff5aa9c 29\r
44ecbc28
JF
30/**\r
31 Common exception handler.\r
32\r
33 @param ExceptionType Exception type.\r
34 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
35**/\r
36VOID\r
37EFIAPI\r
38CommonExceptionHandler (\r
053e878b
MK
39 IN EFI_EXCEPTION_TYPE ExceptionType,\r
40 IN EFI_SYSTEM_CONTEXT SystemContext\r
44ecbc28
JF
41 )\r
42{\r
43 CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);\r
44}\r
45\r
e41aad15
JF
46/**\r
47 Initializes all CPU exceptions entries and provides the default exception handlers.\r
dd563742 48\r
e41aad15
JF
49 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
50 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 51 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
e41aad15
JF
52 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
53\r
54 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
55\r
56 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
e41aad15
JF
57 with default exception handlers.\r
58 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
59 @retval EFI_UNSUPPORTED This function is not supported.\r
60\r
61**/\r
62EFI_STATUS\r
63EFIAPI\r
64InitializeCpuExceptionHandlers (\r
053e878b 65 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
e41aad15
JF
66 )\r
67{\r
ab95e54d
JF
68 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
69 return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);\r
e41aad15
JF
70}\r
71\r
e41aad15
JF
72/**\r
73 Registers a function to be called from the processor interrupt handler.\r
74\r
dd563742
JF
75 This function registers and enables the handler specified by InterruptHandler for a processor\r
76 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r
77 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r
e41aad15 78 The installed handler is called once for each processor interrupt or exception.\r
2a09527e
RN
79 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,\r
80 otherwise EFI_UNSUPPORTED returned.\r
e41aad15
JF
81\r
82 @param[in] InterruptType Defines which interrupt or exception to hook.\r
83 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
84 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
85 will be uninstalled.\r
86\r
87 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
88 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
89 previously installed.\r
90 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
91 previously installed.\r
92 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r
93 or this function is not supported.\r
3f25e6ea 94**/\r
e41aad15
JF
95EFI_STATUS\r
96EFIAPI\r
97RegisterCpuInterruptHandler (\r
053e878b
MK
98 IN EFI_EXCEPTION_TYPE InterruptType,\r
99 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
e41aad15
JF
100 )\r
101{\r
670f13af 102 return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);\r
e41aad15 103}\r
0ff5aa9c
JW
104\r
105/**\r
e7abb94d 106 Setup separate stacks for certain exception handlers.\r
0ff5aa9c 107\r
e7abb94d 108 InitData is optional and processor arch dependent.\r
0ff5aa9c 109\r
e7abb94d
RN
110 @param[in] InitData Pointer to data optional for information about how\r
111 to assign stacks for certain exception handlers.\r
0ff5aa9c 112\r
e7abb94d
RN
113 @retval EFI_SUCCESS The stacks are assigned successfully.\r
114 @retval EFI_UNSUPPORTED This function is not supported.\r
0ff5aa9c
JW
115\r
116**/\r
117EFI_STATUS\r
118EFIAPI\r
e7abb94d 119InitializeSeparateExceptionStacks (\r
053e878b 120 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r
0ff5aa9c
JW
121 )\r
122{\r
053e878b
MK
123 CPU_EXCEPTION_INIT_DATA EssData;\r
124 IA32_DESCRIPTOR Idtr;\r
125 IA32_DESCRIPTOR Gdtr;\r
0ff5aa9c 126\r
e7abb94d
RN
127 if (InitData == NULL) {\r
128 SetMem (mNewGdt, sizeof (mNewGdt), 0);\r
129\r
130 AsmReadIdtr (&Idtr);\r
131 AsmReadGdtr (&Gdtr);\r
132\r
133 EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
134 EssData.X64.KnownGoodStackTop = (UINTN)mNewStack + sizeof (mNewStack);\r
135 EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;\r
136 EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;\r
137 EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;\r
138 EssData.X64.IdtTable = (VOID *)Idtr.Base;\r
139 EssData.X64.IdtTableSize = Idtr.Limit + 1;\r
140 EssData.X64.GdtTable = mNewGdt;\r
141 EssData.X64.GdtTableSize = sizeof (mNewGdt);\r
142 EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;\r
143 EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;\r
144 EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;\r
145 EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;\r
146\r
147 InitData = &EssData;\r
0ff5aa9c
JW
148 }\r
149\r
e7abb94d 150 return ArchSetupExceptionStack (InitData);\r
0ff5aa9c 151}\r