1 | /** @file DxeExceptionLib.c
|
---|
2 |
|
---|
3 | CPU Exception Handler Library common functions.
|
---|
4 |
|
---|
5 | Copyright (c) 2024, Loongson Technology Corporation Limited. All rights reserved.<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include <Library/BaseLib.h>
|
---|
11 | #include <Library/PeCoffGetEntryPointLib.h>
|
---|
12 | #include <Library/PrintLib.h>
|
---|
13 | #include <Library/SerialPortLib.h>
|
---|
14 | #include <Register/LoongArch64/Csr.h>
|
---|
15 | #include "ExceptionCommon.h"
|
---|
16 |
|
---|
17 | CONST CHAR8 mExceptionReservedStr[] = "Reserved";
|
---|
18 | CONST CHAR8 *mExceptionNameStr[] = {
|
---|
19 | "#INT - Interrupt(CSR.ECFG.VS=0)",
|
---|
20 | "#PIL - Page invalid exception for Load option",
|
---|
21 | "#PIS - Page invalid exception for Store operation",
|
---|
22 | "#PIF - Page invalid exception for Fetch operation",
|
---|
23 | "#PME - Page modification exception",
|
---|
24 | "#PNR - Page non-readable exception",
|
---|
25 | "#PNX - Page non-executable exception",
|
---|
26 | "#PPI - Page privilege level illegal exception",
|
---|
27 | "#ADE - Address error exception",
|
---|
28 | "#ALE - Address alignment fault exception",
|
---|
29 | "#BCE - Bound check exception",
|
---|
30 | "#SYS - System call exception",
|
---|
31 | "#BRK - Beeakpoint exception",
|
---|
32 | "#INE - Instruction non-defined exception",
|
---|
33 | "#IPE - Instruction privilege error exception",
|
---|
34 | "#FPD - Floating-point instruction disable exception",
|
---|
35 | "#SXD - 128-bit vector (SIMD instructions) expansion instruction disable exception",
|
---|
36 | "#ASXD - 256-bit vector (Advanced SIMD instructions) expansion instruction disable exception",
|
---|
37 | "#FPE - Floating-Point error exception",
|
---|
38 | "#WPE - WatchPoint Exception for Fetch watchpoint or Memory load/store watchpoint",
|
---|
39 | "#BTD - Binary Translation expansion instruction Disable exception",
|
---|
40 | "#BTE - Binary Translation related exceptions",
|
---|
41 | "#GSPR - Guest Sensitive Privileged Resource exception",
|
---|
42 | "#HVC - HyperVisor Call exception",
|
---|
43 | "#GCXC - Guest CSR Software/Hardware Change exception",
|
---|
44 | "#TBR - TLB refill exception" // !!! NOTICE: Because the TLB refill exception is not instructed in ECODE, so the TLB refill exception must be the last one!
|
---|
45 | };
|
---|
46 |
|
---|
47 | INTN mExceptionKnownNameNum = (sizeof (mExceptionNameStr) / sizeof (CHAR8 *));
|
---|
48 |
|
---|
49 | /**
|
---|
50 | Get ASCII format string exception name by exception type.
|
---|
51 |
|
---|
52 | @param ExceptionType Exception type.
|
---|
53 |
|
---|
54 | @return ASCII format string exception name.
|
---|
55 |
|
---|
56 | **/
|
---|
57 | CONST CHAR8 *
|
---|
58 | GetExceptionNameStr (
|
---|
59 | IN EFI_EXCEPTION_TYPE ExceptionType
|
---|
60 | )
|
---|
61 | {
|
---|
62 | if ((UINTN)ExceptionType < mExceptionKnownNameNum) {
|
---|
63 | return mExceptionNameStr[ExceptionType];
|
---|
64 | } else {
|
---|
65 | return mExceptionReservedStr;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | Prints a message to the serial port.
|
---|
71 |
|
---|
72 | @param Format Format string for the message to print.
|
---|
73 | @param ... Variable argument list whose contents are accessed
|
---|
74 | based on the format string specified by Format.
|
---|
75 |
|
---|
76 | **/
|
---|
77 | VOID
|
---|
78 | EFIAPI
|
---|
79 | InternalPrintMessage (
|
---|
80 | IN CONST CHAR8 *Format,
|
---|
81 | ...
|
---|
82 | )
|
---|
83 | {
|
---|
84 | CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
|
---|
85 | VA_LIST Marker;
|
---|
86 |
|
---|
87 | //
|
---|
88 | // Convert the message to an ASCII String
|
---|
89 | //
|
---|
90 | VA_START (Marker, Format);
|
---|
91 | AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
|
---|
92 | VA_END (Marker);
|
---|
93 |
|
---|
94 | //
|
---|
95 | // Send the print string to a Serial Port
|
---|
96 | //
|
---|
97 | SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | Find and display image base address and return image base and its entry point.
|
---|
102 |
|
---|
103 | @param CurrentEra Current instruction pointer.
|
---|
104 |
|
---|
105 | **/
|
---|
106 | VOID
|
---|
107 | DumpModuleImageInfo (
|
---|
108 | IN UINTN CurrentEra
|
---|
109 | )
|
---|
110 | {
|
---|
111 | EFI_STATUS Status;
|
---|
112 | UINTN Pe32Data;
|
---|
113 | VOID *PdbPointer;
|
---|
114 | VOID *EntryPoint;
|
---|
115 |
|
---|
116 | Pe32Data = PeCoffSearchImageBase (CurrentEra);
|
---|
117 | if (Pe32Data == 0) {
|
---|
118 | InternalPrintMessage ("!!!! Can't find image information. !!!!\n");
|
---|
119 | } else {
|
---|
120 | //
|
---|
121 | // Find Image Base entry point
|
---|
122 | //
|
---|
123 | Status = PeCoffLoaderGetEntryPoint ((VOID *)Pe32Data, &EntryPoint);
|
---|
124 | if (EFI_ERROR (Status)) {
|
---|
125 | EntryPoint = NULL;
|
---|
126 | }
|
---|
127 |
|
---|
128 | InternalPrintMessage ("!!!! Find image based on IP(0x%x) ", CurrentEra);
|
---|
129 | PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *)Pe32Data);
|
---|
130 | if (PdbPointer != NULL) {
|
---|
131 | InternalPrintMessage ("%a", PdbPointer);
|
---|
132 | } else {
|
---|
133 | InternalPrintMessage ("(No PDB) ");
|
---|
134 | }
|
---|
135 |
|
---|
136 | InternalPrintMessage (
|
---|
137 | " (ImageBase=%016lp, EntryPoint=%016p) !!!!\n",
|
---|
138 | (VOID *)Pe32Data,
|
---|
139 | EntryPoint
|
---|
140 | );
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | Default exception handler.
|
---|
146 |
|
---|
147 | @param ExceptionType Exception type.
|
---|
148 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
|
---|
149 |
|
---|
150 | **/
|
---|
151 | VOID
|
---|
152 | EFIAPI
|
---|
153 | DefaultExceptionHandler (
|
---|
154 | IN EFI_EXCEPTION_TYPE ExceptionType,
|
---|
155 | IN OUT EFI_SYSTEM_CONTEXT SystemContext
|
---|
156 | )
|
---|
157 | {
|
---|
158 | //
|
---|
159 | // Initialize the serial port before dumping.
|
---|
160 | //
|
---|
161 | SerialPortInitialize ();
|
---|
162 | //
|
---|
163 | // Display ExceptionType, CPU information and Image information
|
---|
164 | //
|
---|
165 | DumpImageAndCpuContent (ExceptionType, SystemContext);
|
---|
166 |
|
---|
167 | //
|
---|
168 | // Enter a dead loop.
|
---|
169 | //
|
---|
170 | CpuDeadLoop ();
|
---|
171 | }
|
---|