]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
CpuException: Add InitializeSeparateExceptionStacks
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / DxeException.c
1 /** @file
2 CPU exception handler library implemenation for DXE modules.
3
4 Copyright (c) 2013 - 2022, 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_INTERRUPT_NUM];
18 EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_INTERRUPT_NUM];
19 EXCEPTION_HANDLER_DATA mExceptionHandlerData = {
20 CPU_INTERRUPT_NUM,
21 0, // To be fixed
22 mReservedVectorsData,
23 mExternalInterruptHandlerTable
24 };
25
26 UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *
27 CPU_KNOWN_GOOD_STACK_SIZE];
28 UINT8 mNewGdt[CPU_TSS_GDT_SIZE];
29
30 /**
31 Common exception handler.
32
33 @param ExceptionType Exception type.
34 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
35 **/
36 VOID
37 EFIAPI
38 CommonExceptionHandler (
39 IN EFI_EXCEPTION_TYPE ExceptionType,
40 IN EFI_SYSTEM_CONTEXT SystemContext
41 )
42 {
43 CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);
44 }
45
46 /**
47 Initializes all CPU exceptions entries and provides the default exception handlers.
48
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.
53
54 @param[in] VectorInfo Pointer to reserved vector list.
55
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.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 InitializeCpuExceptionHandlers (
65 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
66 )
67 {
68 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
69 return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);
70 }
71
72 /**
73 Registers a function to be called from the processor interrupt handler.
74
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.
81
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
85 will be uninstalled.
86
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
89 previously installed.
90 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
91 previously installed.
92 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
93 or this function is not supported.
94 **/
95 EFI_STATUS
96 EFIAPI
97 RegisterCpuInterruptHandler (
98 IN EFI_EXCEPTION_TYPE InterruptType,
99 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
100 )
101 {
102 return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);
103 }
104
105 /**
106 Setup separate stacks for certain exception handlers.
107
108 InitData is optional and processor arch dependent.
109
110 @param[in] InitData Pointer to data optional for information about how
111 to assign stacks for certain exception handlers.
112
113 @retval EFI_SUCCESS The stacks are assigned successfully.
114 @retval EFI_UNSUPPORTED This function is not supported.
115
116 **/
117 EFI_STATUS
118 EFIAPI
119 InitializeSeparateExceptionStacks (
120 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
121 )
122 {
123 CPU_EXCEPTION_INIT_DATA EssData;
124 IA32_DESCRIPTOR Idtr;
125 IA32_DESCRIPTOR Gdtr;
126
127 if (InitData == NULL) {
128 SetMem (mNewGdt, sizeof (mNewGdt), 0);
129
130 AsmReadIdtr (&Idtr);
131 AsmReadGdtr (&Gdtr);
132
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;
146
147 InitData = &EssData;
148 }
149
150 return ArchSetupExceptionStack (InitData);
151 }