1 /** @file
2   CPU exception handler library implemenation for DXE modules.
3 
4   Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <PiDxe.h>
10 #include "CpuExceptionCommon.h"
11 #include <Library/DebugLib.h>
12 #include <Library/MemoryAllocationLib.h>
13 #include <Library/UefiBootServicesTableLib.h>
14 
15 CONST UINTN    mDoFarReturnFlag  = 0;
16 
17 RESERVED_VECTORS_DATA       mReservedVectorsData[CPU_EXCEPTION_NUM];
18 EFI_CPU_INTERRUPT_HANDLER   mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
19 UINTN                       mEnabledInterruptNum = 0;
20 
21 EXCEPTION_HANDLER_DATA      mExceptionHandlerData;
22 
23 UINT8                       mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *
24                                       CPU_KNOWN_GOOD_STACK_SIZE];
25 UINT8                       mNewGdt[CPU_TSS_GDT_SIZE];
26 
27 /**
28   Common exception handler.
29 
30   @param ExceptionType  Exception type.
31   @param SystemContext  Pointer to EFI_SYSTEM_CONTEXT.
32 **/
33 VOID
34 EFIAPI
CommonExceptionHandler(IN EFI_EXCEPTION_TYPE ExceptionType,IN EFI_SYSTEM_CONTEXT SystemContext)35 CommonExceptionHandler (
36   IN EFI_EXCEPTION_TYPE          ExceptionType,
37   IN EFI_SYSTEM_CONTEXT          SystemContext
38   )
39 {
40   CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);
41 }
42 
43 /**
44   Initializes all CPU exceptions entries and provides the default exception handlers.
45 
46   Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
47   persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
48   If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
49   If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
50 
51   @param[in]  VectorInfo    Pointer to reserved vector list.
52 
53   @retval EFI_SUCCESS           CPU Exception Entries have been successfully initialized
54                                 with default exception handlers.
55   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
56   @retval EFI_UNSUPPORTED       This function is not supported.
57 
58 **/
59 EFI_STATUS
60 EFIAPI
InitializeCpuExceptionHandlers(IN EFI_VECTOR_HANDOFF_INFO * VectorInfo OPTIONAL)61 InitializeCpuExceptionHandlers (
62   IN EFI_VECTOR_HANDOFF_INFO       *VectorInfo OPTIONAL
63   )
64 {
65   mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;
66   mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;
67   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
68   return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);
69 }
70 
71 /**
72   Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
73 
74   Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
75   persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
76   If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
77   If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
78 
79   @param[in]  VectorInfo    Pointer to reserved vector list.
80 
81   @retval EFI_SUCCESS           All CPU interrupt/exception entries have been successfully initialized
82                                 with default interrupt/exception handlers.
83   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
84   @retval EFI_UNSUPPORTED       This function is not supported.
85 
86 **/
87 EFI_STATUS
88 EFIAPI
InitializeCpuInterruptHandlers(IN EFI_VECTOR_HANDOFF_INFO * VectorInfo OPTIONAL)89 InitializeCpuInterruptHandlers (
90   IN EFI_VECTOR_HANDOFF_INFO       *VectorInfo OPTIONAL
91   )
92 {
93   EFI_STATUS                         Status;
94   IA32_IDT_GATE_DESCRIPTOR           *IdtTable;
95   IA32_DESCRIPTOR                    IdtDescriptor;
96   UINTN                              IdtEntryCount;
97   EXCEPTION_HANDLER_TEMPLATE_MAP     TemplateMap;
98   UINTN                              Index;
99   UINTN                              InterruptEntry;
100   UINT8                              *InterruptEntryCode;
101   RESERVED_VECTORS_DATA              *ReservedVectors;
102   EFI_CPU_INTERRUPT_HANDLER          *ExternalInterruptHandler;
103 
104   Status = gBS->AllocatePool (
105                   EfiBootServicesCode,
106                   sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,
107                   (VOID **)&ReservedVectors
108                   );
109   ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);
110   SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);
111   if (VectorInfo != NULL) {
112     Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);
113     if (EFI_ERROR (Status)) {
114       FreePool (ReservedVectors);
115       return EFI_INVALID_PARAMETER;
116     }
117   }
118 
119   ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);
120   ASSERT (ExternalInterruptHandler != NULL);
121 
122   //
123   // Read IDT descriptor and calculate IDT size
124   //
125   AsmReadIdtr (&IdtDescriptor);
126   IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
127   if (IdtEntryCount > CPU_INTERRUPT_NUM) {
128     IdtEntryCount = CPU_INTERRUPT_NUM;
129   }
130   //
131   // Create Interrupt Descriptor Table and Copy the old IDT table in
132   //
133   IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);
134   ASSERT (IdtTable != NULL);
135   CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);
136 
137   AsmGetTemplateAddressMap (&TemplateMap);
138   ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
139 
140   Status = gBS->AllocatePool (
141                   EfiBootServicesCode,
142                   TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,
143                   (VOID **)&InterruptEntryCode
144                   );
145   ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);
146 
147   InterruptEntry = (UINTN) InterruptEntryCode;
148   for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {
149     CopyMem (
150       (VOID *) InterruptEntry,
151       (VOID *) TemplateMap.ExceptionStart,
152       TemplateMap.ExceptionStubHeaderSize
153       );
154     AsmVectorNumFixup ((VOID *) InterruptEntry,  (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);
155     InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
156   }
157 
158   TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;
159   mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;
160   mExceptionHandlerData.ReservedVectors          = ReservedVectors;
161   mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
162   InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
163 
164   UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);
165 
166   //
167   // Load Interrupt Descriptor Table
168   //
169   IdtDescriptor.Base  = (UINTN) IdtTable;
170   IdtDescriptor.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);
171   AsmWriteIdtr ((IA32_DESCRIPTOR *) &IdtDescriptor);
172 
173   return EFI_SUCCESS;
174 }
175 
176 /**
177   Registers a function to be called from the processor interrupt handler.
178 
179   This function registers and enables the handler specified by InterruptHandler for a processor
180   interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
181   handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
182   The installed handler is called once for each processor interrupt or exception.
183   NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
184   InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
185 
186   @param[in]  InterruptType     Defines which interrupt or exception to hook.
187   @param[in]  InterruptHandler  A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
188                                 when a processor interrupt occurs. If this parameter is NULL, then the handler
189                                 will be uninstalled.
190 
191   @retval EFI_SUCCESS           The handler for the processor interrupt was successfully installed or uninstalled.
192   @retval EFI_ALREADY_STARTED   InterruptHandler is not NULL, and a handler for InterruptType was
193                                 previously installed.
194   @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
195                                 previously installed.
196   @retval EFI_UNSUPPORTED       The interrupt specified by InterruptType is not supported,
197                                 or this function is not supported.
198 **/
199 EFI_STATUS
200 EFIAPI
RegisterCpuInterruptHandler(IN EFI_EXCEPTION_TYPE InterruptType,IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler)201 RegisterCpuInterruptHandler (
202   IN EFI_EXCEPTION_TYPE            InterruptType,
203   IN EFI_CPU_INTERRUPT_HANDLER     InterruptHandler
204   )
205 {
206   return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);
207 }
208 
209 /**
210   Initializes CPU exceptions entries and setup stack switch for given exceptions.
211 
212   This method will call InitializeCpuExceptionHandlers() to setup default
213   exception handlers unless indicated not to do it explicitly.
214 
215   If InitData is passed with NULL, this method will use the resource reserved
216   by global variables to initialize it; Otherwise it will use data in InitData
217   to setup stack switch. This is for the different use cases in DxeCore and
218   Cpu MP exception initialization.
219 
220   @param[in]  VectorInfo    Pointer to reserved vector list.
221   @param[in]  InitData      Pointer to data required to setup stack switch for
222                             given exceptions.
223 
224   @retval EFI_SUCCESS             The exceptions have been successfully
225                                   initialized.
226   @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid
227                                   content.
228 
229 **/
230 EFI_STATUS
231 EFIAPI
InitializeCpuExceptionHandlersEx(IN EFI_VECTOR_HANDOFF_INFO * VectorInfo OPTIONAL,IN CPU_EXCEPTION_INIT_DATA * InitData OPTIONAL)232 InitializeCpuExceptionHandlersEx (
233   IN EFI_VECTOR_HANDOFF_INFO            *VectorInfo OPTIONAL,
234   IN CPU_EXCEPTION_INIT_DATA            *InitData OPTIONAL
235   )
236 {
237   EFI_STATUS                        Status;
238   CPU_EXCEPTION_INIT_DATA           EssData;
239   IA32_DESCRIPTOR                   Idtr;
240   IA32_DESCRIPTOR                   Gdtr;
241 
242   //
243   // To avoid repeat initialization of default handlers, the caller should pass
244   // an extended init data with InitDefaultHandlers set to FALSE. There's no
245   // need to call this method to just initialize default handlers. Call non-ex
246   // version instead; or this method must be implemented as a simple wrapper of
247   // non-ex version of it, if this version has to be called.
248   //
249   if (InitData == NULL || InitData->X64.InitDefaultHandlers) {
250     Status = InitializeCpuExceptionHandlers (VectorInfo);
251   } else {
252     Status = EFI_SUCCESS;
253   }
254 
255   if (!EFI_ERROR (Status)) {
256     //
257     // Initializing stack switch is only necessary for Stack Guard functionality.
258     //
259     if (PcdGetBool (PcdCpuStackGuard)) {
260       if (InitData == NULL) {
261         SetMem (mNewGdt, sizeof (mNewGdt), 0);
262 
263         AsmReadIdtr (&Idtr);
264         AsmReadGdtr (&Gdtr);
265 
266         EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;
267         EssData.X64.KnownGoodStackTop = (UINTN)mNewStack + sizeof (mNewStack);
268         EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;
269         EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;
270         EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;
271         EssData.X64.IdtTable = (VOID *)Idtr.Base;
272         EssData.X64.IdtTableSize = Idtr.Limit + 1;
273         EssData.X64.GdtTable = mNewGdt;
274         EssData.X64.GdtTableSize = sizeof (mNewGdt);
275         EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;
276         EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;
277         EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;
278         EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;
279 
280         InitData = &EssData;
281       }
282       Status = ArchSetupExceptionStack (InitData);
283     }
284   }
285 
286   return  Status;
287 }
288