summaryrefslogtreecommitdiff
path: root/UefiCpuPkg/Library/CpuExceptionHandlerLib/AArch64/CpuExceptionHandlerLib.c
blob: 9116ba9d96a8dbbee095724cb379d19a38d89b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/** @file
  Default exception handler

  Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
  Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
  Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Uefi.h>
#include <Library/ArmLib.h>
#include <Library/BaseLib.h>
#include <Library/CpuExceptionHandlerLib.h>
#include <Library/DebugLib.h>
#include <Library/PeCoffGetEntryPointLib.h>
#include <Library/PrintLib.h>
#include <Library/SerialPortLib.h>
#include <Protocol/DebugSupport.h>

#include <CpuExceptionHelpers.h>
#include "ExceptionSupport.h"

//
// Maximum number of characters to print to serial (UINT8s) and to console if
// available (as UINT16s)
//
#define MAX_PRINT_CHARS  100

UINTN                   mMaxExceptionNumber                           = MAX_AARCH64_EXCEPTION;
EFI_EXCEPTION_CALLBACK  mExceptionHandlers[MAX_AARCH64_EXCEPTION + 1] = { 0 };
PHYSICAL_ADDRESS        mExceptionVectorAlignmentMask                 = ARM_VECTOR_TABLE_ALIGNMENT;

#define EL0_STACK_SIZE  EFI_PAGES_TO_SIZE(2)
STATIC UINTN  mNewStackBase[EL0_STACK_SIZE / sizeof (UINTN)];

STATIC CHAR8  *mExceptionTypeString[] = {
  "Synchronous",
  "IRQ",
  "FIQ",
  "SError"
};

STATIC BOOLEAN  mRecursiveException;

/**
  Decodes and logs the fault cause of an Instruction or Data Abort
  by mapping the Fault Status Code bits [5:0] of the ISS field to
  a human-readable description.

  @param[in]  AbortType  Null-terminated string identifying the abort
                         type (e.g., "Instruction Abort", "Data Abort").
  @param[in]  Iss        The Instruction Specific Syndrome (ISS) field
                         extracted from ESR_ELx. Bits [5:0] are used as
                         the Fault Status Code (IFSC/DFSC).
**/
STATIC
VOID
DescribeInstructionOrDataAbort (
  IN CHAR8  *AbortType,
  IN UINTN  Iss
  )
{
  CHAR8  *AbortCause;

  switch (Iss & 0x3f) {
    case 0x0: AbortCause = "Address size fault, zeroth level of translation or translation table base register";
      break;
    case 0x1: AbortCause = "Address size fault, first level";
      break;
    case 0x2: AbortCause = "Address size fault, second level";
      break;
    case 0x3: AbortCause = "Address size fault, third level";
      break;
    case 0x4: AbortCause = "Translation fault, zeroth level";
      break;
    case 0x5: AbortCause = "Translation fault, first level";
      break;
    case 0x6: AbortCause = "Translation fault, second level";
      break;
    case 0x7: AbortCause = "Translation fault, third level";
      break;
    case 0x9: AbortCause = "Access flag fault, first level";
      break;
    case 0xa: AbortCause = "Access flag fault, second level";
      break;
    case 0xb: AbortCause = "Access flag fault, third level";
      break;
    case 0xd: AbortCause = "Permission fault, first level";
      break;
    case 0xe: AbortCause = "Permission fault, second level";
      break;
    case 0xf: AbortCause = "Permission fault, third level";
      break;
    case 0x10: AbortCause = "Synchronous external abort";
      break;
    case 0x18: AbortCause = "Synchronous parity error on memory access";
      break;
    case 0x11: AbortCause = "Asynchronous external abort";
      break;
    case 0x19: AbortCause = "Asynchronous parity error on memory access";
      break;
    case 0x14: AbortCause = "Synchronous external abort on translation table walk, zeroth level";
      break;
    case 0x15: AbortCause = "Synchronous external abort on translation table walk, first level";
      break;
    case 0x16: AbortCause = "Synchronous external abort on translation table walk, second level";
      break;
    case 0x17: AbortCause = "Synchronous external abort on translation table walk, third level";
      break;
    case 0x1c: AbortCause = "Synchronous parity error on memory access on translation table walk, zeroth level";
      break;
    case 0x1d: AbortCause = "Synchronous parity error on memory access on translation table walk, first level";
      break;
    case 0x1e: AbortCause = "Synchronous parity error on memory access on translation table walk, second level";
      break;
    case 0x1f: AbortCause = "Synchronous parity error on memory access on translation table walk, third level";
      break;
    case 0x21: AbortCause = "Alignment fault";
      break;
    case 0x22: AbortCause = "Debug event";
      break;
    case 0x30: AbortCause = "TLB conflict abort";
      break;
    case 0x33:
    case 0x34: AbortCause = "IMPLEMENTATION DEFINED";
      break;
    case 0x35:
    case 0x36: AbortCause = "Domain fault";
      break;
    default: AbortCause = "";
      break;
  }

  DEBUG ((DEBUG_ERROR, "\n%a: %a\n", AbortType, AbortCause));
}

/**
  Decodes and logs the cause of an AArch64 exception by inspecting the
  Exception Class (EC) field of the Exception Syndrome Register (ESR_ELx).
  For Instruction and Data Aborts, delegates to
  DescribeInstructionOrDataAbort () for further ISS decoding.

  @param[in]  Esr  The 64-bit value of ESR_ELx captured at the time of
                   the exception. Bits [31:26] are used as the Exception
                   Class (EC) and bits [24:0] as the Instruction Specific
                   Syndrome (ISS).
**/
STATIC
VOID
DescribeExceptionSyndrome (
  IN UINT64  Esr
  )
{
  CHAR8  *Message;
  UINTN  Ec;
  UINTN  Iss;

  Ec  = Esr >> 26;
  Iss = Esr & 0x00ffffff;

  switch (Ec) {
    case 0x15: Message = "SVC executed in AArch64";
      break;
    case 0x20:
    case 0x21: DescribeInstructionOrDataAbort ("Instruction abort", Iss);
      return;
    case 0x22: Message = "PC alignment fault";
      break;
    case 0x23: Message = "SP alignment fault";
      break;
    case 0x24:
    case 0x25: DescribeInstructionOrDataAbort ("Data abort", Iss);
      return;
    default: return;
  }

  DEBUG ((DEBUG_ERROR, "\n %a \n", Message));
}

/**
  Returns a pointer to the filename component of a full file path by
  scanning backwards for the last '/' or '\\' path separator.
  If no separator is found, the original string is returned as-is.

  @param[in]  FullName  Null-terminated ASCII string containing the full
                        file path to extract the base name from.

  @retval  Pointer to the base filename within FullName. This is not a
           newly allocated string — the returned pointer references
           memory within the original FullName buffer.
**/
STATIC
CONST CHAR8 *
BaseName (
  IN  CONST CHAR8  *FullName
  )
{
  CONST CHAR8  *Str;

  Str = FullName + AsciiStrLen (FullName);

  while (--Str > FullName) {
    if ((*Str == '/') || (*Str == '\\')) {
      return Str + 1;
    }
  }

  return Str;
}

/**
  Register EL0 stack and perform architecture specific configuration to ensure
  exceptions are routed to correct EL.

  @param[in]  VectorBaseAddress  Base address of the exception vector table
                                 to be configured.

  @retval  EFI_SUCCESS  Vector environment configured successfully.
**/
EFI_STATUS
ArchVectorConfig (
  IN  UINTN  VectorBaseAddress
  )
{
  UINTN  HcrReg;

  // Round down sp by 16 bytes alignment
  RegisterEl0Stack (
    (VOID *)(((UINTN)mNewStackBase + EL0_STACK_SIZE) & ~0xFULL)
    );

  if (ArmReadCurrentEL () == AARCH64_EL2) {
    HcrReg = ArmReadHcr ();

    // Trap General Exceptions. All exceptions that would be routed to EL1 are routed to EL2
    HcrReg |= ARM_HCR_TGE;

    ArmWriteHcr (HcrReg);
  }

  return EFI_SUCCESS;
}

/**
  Common C-level exception handler dispatched from ASM exception entry points.
  Invokes a registered handler for the given ExceptionType if one exists,
  otherwise dumps the CPU context. Asserts on unknown exception types.

  @param[in]      ExceptionType  AArch64 exception type or interrupt vector number.
  @param[in,out]  SystemContext  Saved CPU context at the time of the exception.
**/
VOID
EFIAPI
CommonCExceptionHandler (
  IN     EFI_EXCEPTION_TYPE  ExceptionType,
  IN OUT EFI_SYSTEM_CONTEXT  SystemContext
  )
{
  if ((UINTN)ExceptionType <= mMaxExceptionNumber) {
    if (mExceptionHandlers[ExceptionType] != NULL) {
      mExceptionHandlers[ExceptionType](ExceptionType, SystemContext);
      return;
    }
  } else {
    DEBUG ((DEBUG_ERROR, "Unknown exception type %d\n", ExceptionType));
    ASSERT (FALSE);
  }

  DumpCpuContext (ExceptionType, SystemContext);
}

//
// CpuExceptionHandler Implementation
//

/**
  Dumps the full AArch64 CPU context to serial and ConOut on an unexpected
  exception, then asserts and halts. Detects recursive calls and short-circuits
  to a minimal serial write followed by CpuDeadLoop ().

  Do not allocate memory or perform complex operations from within this function.

  @param[in]      ExceptionType  AArch64 exception type or interrupt vector number.
  @param[in,out]  SystemContext  Saved CPU context at the time of the exception.
**/
VOID
DumpCpuContext (
  IN     EFI_EXCEPTION_TYPE  ExceptionType,
  IN OUT EFI_SYSTEM_CONTEXT  SystemContext
  )
{
  CHAR8   Buffer[MAX_PRINT_CHARS];
  CHAR16  UnicodeBuffer[MAX_PRINT_CHARS];
  UINTN   CharCount;
  INT32   Offset;

  if (mRecursiveException) {
    STATIC CHAR8 CONST  Message[] = "\nRecursive exception occurred while dumping the CPU state\n";
    SerialPortWrite ((UINT8 *)Message, sizeof Message - 1);
    CpuDeadLoop ();
  }

  mRecursiveException = TRUE;

  CharCount = AsciiSPrint (Buffer, sizeof (Buffer), "\n\n%a Exception at 0x%016lx\n", mExceptionTypeString[ExceptionType], SystemContext.SystemContextAArch64->ELR);
  SerialPortWrite ((UINT8 *)Buffer, CharCount);

  // Prepare a unicode buffer for ConOut, if applicable, in case the buffer
  // gets reused.
  UnicodeSPrintAsciiFormat (UnicodeBuffer, MAX_PRINT_CHARS, Buffer);

  DEBUG_CODE_BEGIN ();
  CHAR8   *Pdb, *PrevPdb;
  UINTN   ImageBase;
  UINTN   PeCoffSizeOfHeader;
  UINT64  *Fp;
  UINT64  RootFp[2];
  UINTN   Idx;

  PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);
  if (Pdb != NULL) {
    DEBUG ((
      DEBUG_ERROR,
      "PC 0x%012lx (0x%012lx+0x%08x) [ 0] %a\n",
      SystemContext.SystemContextAArch64->ELR,
      ImageBase,
      SystemContext.SystemContextAArch64->ELR - ImageBase,
      BaseName (Pdb)
      ));
  } else {
    DEBUG ((DEBUG_ERROR, "PC 0x%012lx\n", SystemContext.SystemContextAArch64->ELR));
  }

  if ((UINT64 *)SystemContext.SystemContextAArch64->FP != 0) {
    Idx = 0;

    RootFp[0] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[0];
    RootFp[1] = ((UINT64 *)SystemContext.SystemContextAArch64->FP)[1];
    if (RootFp[1] != SystemContext.SystemContextAArch64->LR) {
      RootFp[0] = SystemContext.SystemContextAArch64->FP;
      RootFp[1] = SystemContext.SystemContextAArch64->LR;
    }

    for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {
      Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);
      if (Pdb != NULL) {
        if (Pdb != PrevPdb) {
          Idx++;
          PrevPdb = Pdb;
        }

        DEBUG ((
          DEBUG_ERROR,
          "PC 0x%012lx (0x%012lx+0x%08x) [% 2d] %a\n",
          Fp[1],
          ImageBase,
          Fp[1] - ImageBase,
          Idx,
          BaseName (Pdb)
          ));
      } else {
        DEBUG ((DEBUG_ERROR, "PC 0x%012lx\n", Fp[1]));
      }
    }

    PrevPdb = Pdb = GetImageName (SystemContext.SystemContextAArch64->ELR, &ImageBase, &PeCoffSizeOfHeader);
    if (Pdb != NULL) {
      DEBUG ((DEBUG_ERROR, "\n[ 0] %a\n", Pdb));
    }

    Idx = 0;
    for (Fp = RootFp; Fp[0] != 0; Fp = (UINT64 *)Fp[0]) {
      Pdb = GetImageName (Fp[1], &ImageBase, &PeCoffSizeOfHeader);
      if ((Pdb != NULL) && (Pdb != PrevPdb)) {
        DEBUG ((DEBUG_ERROR, "[% 2d] %a\n", ++Idx, Pdb));
        PrevPdb = Pdb;
      }
    }
  }

  DEBUG_CODE_END ();

  DEBUG ((DEBUG_ERROR, "\n  X0 0x%016lx   X1 0x%016lx   X2 0x%016lx   X3 0x%016lx\n", SystemContext.SystemContextAArch64->X0, SystemContext.SystemContextAArch64->X1, SystemContext.SystemContextAArch64->X2, SystemContext.SystemContextAArch64->X3));
  DEBUG ((DEBUG_ERROR, "  X4 0x%016lx   X5 0x%016lx   X6 0x%016lx   X7 0x%016lx\n", SystemContext.SystemContextAArch64->X4, SystemContext.SystemContextAArch64->X5, SystemContext.SystemContextAArch64->X6, SystemContext.SystemContextAArch64->X7));
  DEBUG ((DEBUG_ERROR, "  X8 0x%016lx   X9 0x%016lx  X10 0x%016lx  X11 0x%016lx\n", SystemContext.SystemContextAArch64->X8, SystemContext.SystemContextAArch64->X9, SystemContext.SystemContextAArch64->X10, SystemContext.SystemContextAArch64->X11));
  DEBUG ((DEBUG_ERROR, " X12 0x%016lx  X13 0x%016lx  X14 0x%016lx  X15 0x%016lx\n", SystemContext.SystemContextAArch64->X12, SystemContext.SystemContextAArch64->X13, SystemContext.SystemContextAArch64->X14, SystemContext.SystemContextAArch64->X15));
  DEBUG ((DEBUG_ERROR, " X16 0x%016lx  X17 0x%016lx  X18 0x%016lx  X19 0x%016lx\n", SystemContext.SystemContextAArch64->X16, SystemContext.SystemContextAArch64->X17, SystemContext.SystemContextAArch64->X18, SystemContext.SystemContextAArch64->X19));
  DEBUG ((DEBUG_ERROR, " X20 0x%016lx  X21 0x%016lx  X22 0x%016lx  X23 0x%016lx\n", SystemContext.SystemContextAArch64->X20, SystemContext.SystemContextAArch64->X21, SystemContext.SystemContextAArch64->X22, SystemContext.SystemContextAArch64->X23));
  DEBUG ((DEBUG_ERROR, " X24 0x%016lx  X25 0x%016lx  X26 0x%016lx  X27 0x%016lx\n", SystemContext.SystemContextAArch64->X24, SystemContext.SystemContextAArch64->X25, SystemContext.SystemContextAArch64->X26, SystemContext.SystemContextAArch64->X27));
  DEBUG ((DEBUG_ERROR, " X28 0x%016lx   FP 0x%016lx   LR 0x%016lx  \n", SystemContext.SystemContextAArch64->X28, SystemContext.SystemContextAArch64->FP, SystemContext.SystemContextAArch64->LR));

  /* We save these as 128bit numbers, but have to print them as two 64bit numbers,
     so swap the 64bit words to correctly represent a 128bit number.  */
  DEBUG ((DEBUG_ERROR, "\n  V0 0x%016lx %016lx   V1 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V0[1], SystemContext.SystemContextAArch64->V0[0], SystemContext.SystemContextAArch64->V1[1], SystemContext.SystemContextAArch64->V1[0]));
  DEBUG ((DEBUG_ERROR, "  V2 0x%016lx %016lx   V3 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V2[1], SystemContext.SystemContextAArch64->V2[0], SystemContext.SystemContextAArch64->V3[1], SystemContext.SystemContextAArch64->V3[0]));
  DEBUG ((DEBUG_ERROR, "  V4 0x%016lx %016lx   V5 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V4[1], SystemContext.SystemContextAArch64->V4[0], SystemContext.SystemContextAArch64->V5[1], SystemContext.SystemContextAArch64->V5[0]));
  DEBUG ((DEBUG_ERROR, "  V6 0x%016lx %016lx   V7 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V6[1], SystemContext.SystemContextAArch64->V6[0], SystemContext.SystemContextAArch64->V7[1], SystemContext.SystemContextAArch64->V7[0]));
  DEBUG ((DEBUG_ERROR, "  V8 0x%016lx %016lx   V9 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V8[1], SystemContext.SystemContextAArch64->V8[0], SystemContext.SystemContextAArch64->V9[1], SystemContext.SystemContextAArch64->V9[0]));
  DEBUG ((DEBUG_ERROR, " V10 0x%016lx %016lx  V11 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V10[1], SystemContext.SystemContextAArch64->V10[0], SystemContext.SystemContextAArch64->V11[1], SystemContext.SystemContextAArch64->V11[0]));
  DEBUG ((DEBUG_ERROR, " V12 0x%016lx %016lx  V13 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V12[1], SystemContext.SystemContextAArch64->V12[0], SystemContext.SystemContextAArch64->V13[1], SystemContext.SystemContextAArch64->V13[0]));
  DEBUG ((DEBUG_ERROR, " V14 0x%016lx %016lx  V15 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V14[1], SystemContext.SystemContextAArch64->V14[0], SystemContext.SystemContextAArch64->V15[1], SystemContext.SystemContextAArch64->V15[0]));
  DEBUG ((DEBUG_ERROR, " V16 0x%016lx %016lx  V17 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V16[1], SystemContext.SystemContextAArch64->V16[0], SystemContext.SystemContextAArch64->V17[1], SystemContext.SystemContextAArch64->V17[0]));
  DEBUG ((DEBUG_ERROR, " V18 0x%016lx %016lx  V19 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V18[1], SystemContext.SystemContextAArch64->V18[0], SystemContext.SystemContextAArch64->V19[1], SystemContext.SystemContextAArch64->V19[0]));
  DEBUG ((DEBUG_ERROR, " V20 0x%016lx %016lx  V21 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V20[1], SystemContext.SystemContextAArch64->V20[0], SystemContext.SystemContextAArch64->V21[1], SystemContext.SystemContextAArch64->V21[0]));
  DEBUG ((DEBUG_ERROR, " V22 0x%016lx %016lx  V23 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V22[1], SystemContext.SystemContextAArch64->V22[0], SystemContext.SystemContextAArch64->V23[1], SystemContext.SystemContextAArch64->V23[0]));
  DEBUG ((DEBUG_ERROR, " V24 0x%016lx %016lx  V25 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V24[1], SystemContext.SystemContextAArch64->V24[0], SystemContext.SystemContextAArch64->V25[1], SystemContext.SystemContextAArch64->V25[0]));
  DEBUG ((DEBUG_ERROR, " V26 0x%016lx %016lx  V27 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V26[1], SystemContext.SystemContextAArch64->V26[0], SystemContext.SystemContextAArch64->V27[1], SystemContext.SystemContextAArch64->V27[0]));
  DEBUG ((DEBUG_ERROR, " V28 0x%016lx %016lx  V29 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V28[1], SystemContext.SystemContextAArch64->V28[0], SystemContext.SystemContextAArch64->V29[1], SystemContext.SystemContextAArch64->V29[0]));
  DEBUG ((DEBUG_ERROR, " V30 0x%016lx %016lx  V31 0x%016lx %016lx\n", SystemContext.SystemContextAArch64->V30[1], SystemContext.SystemContextAArch64->V30[0], SystemContext.SystemContextAArch64->V31[1], SystemContext.SystemContextAArch64->V31[0]));

  DEBUG ((DEBUG_ERROR, "\n  SP 0x%016lx  ELR 0x%016lx  SPSR 0x%08lx  FPSR 0x%08lx\n ESR 0x%08lx          FAR 0x%016lx\n", SystemContext.SystemContextAArch64->SP, SystemContext.SystemContextAArch64->ELR, SystemContext.SystemContextAArch64->SPSR, SystemContext.SystemContextAArch64->FPSR, SystemContext.SystemContextAArch64->ESR, SystemContext.SystemContextAArch64->FAR));

  DEBUG ((DEBUG_ERROR, "\n ESR : EC 0x%02x  IL 0x%x  ISS 0x%08x\n", (SystemContext.SystemContextAArch64->ESR & 0xFC000000) >> 26, (SystemContext.SystemContextAArch64->ESR >> 25) & 0x1, SystemContext.SystemContextAArch64->ESR & 0x1FFFFFF));

  DescribeExceptionSyndrome (SystemContext.SystemContextAArch64->ESR);

  DEBUG ((DEBUG_ERROR, "\nStack dump:\n"));
  for (Offset = -256; Offset < 256; Offset += 32) {
    DEBUG ((
      DEBUG_ERROR,
      "%c %013lx: %016lx %016lx %016lx %016lx\n",
      Offset == 0 ? '>' : ' ',
      SystemContext.SystemContextAArch64->SP + Offset,
      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset),
      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 8),
      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 16),
      *(UINT64 *)(SystemContext.SystemContextAArch64->SP + Offset + 24)
      ));
  }

  // Attempt to print that we had a synchronous exception to ConOut.  We do
  // this after the serial logging as ConOut's logging is more complex and we
  // aren't guaranteed to succeed.
  LogToConsole (UnicodeBuffer);

  ASSERT (FALSE);
  CpuDeadLoop ();
}

/**
Initializes all CPU exceptions entries and provides the default exception handlers.

Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.

@param[in]  VectorInfo    Pointer to reserved vector list.

@retval EFI_SUCCESS           CPU Exception Entries have been successfully initialized
                              with default exception handlers.
@retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
@retval EFI_UNSUPPORTED       This function is not supported.

**/
EFI_STATUS
EFIAPI
InitializeCpuExceptionHandlers (
  IN EFI_VECTOR_HANDOFF_INFO  *VectorInfo OPTIONAL
  )
{
  UINT64  VectorBase;

  // use VBAR to point to where our exception handlers are

  // The vector table must be aligned for the architecture.  If this
  // assertion fails ensure the appropriate FFS alignment is in effect,
  // which can be accomplished by ensuring the proper Align=X statement
  // in the platform packaging rules.  For AArch64 Align=4K is required.
  // Align=Auto can be used but this is known to cause an issue with
  // populating the reset vector area for encapsulated FVs.
  ASSERT (((UINTN)ExceptionHandlersStart & mExceptionVectorAlignmentMask) == 0);

  VectorBase = (UINT64)(UINTN)ExceptionHandlersStart;

  // call the architecture-specific routine to prepare for the new vector
  // configuration to take effect
  ArchVectorConfig ((UINTN)VectorBase);

  ArmWriteVBar ((UINTN)VectorBase);

  return EFI_SUCCESS;
}

/**
Registers a function to be called from the processor exception handler. (On AArch64 this only
provides exception handlers, not interrupt handling which is provided through the Hardware Interrupt
Protocol.)

This function registers and enables the handler specified by ExceptionHandler for a processor
interrupt or exception type specified by ExceptionType. If ExceptionHandler is NULL, then the
handler for the processor interrupt or exception type specified by ExceptionType is uninstalled.
The installed handler is called once for each processor interrupt or exception.
NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
otherwise EFI_UNSUPPORTED returned.

@param[in]  ExceptionType     Defines which interrupt or exception to hook.
@param[in]  ExceptionHandler  A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
when a processor interrupt occurs. If this parameter is NULL, then the handler
will be uninstalled.

@retval EFI_SUCCESS           The handler for the processor interrupt was successfully installed or uninstalled.
@retval EFI_ALREADY_STARTED   ExceptionHandler is not NULL, and a handler for ExceptionType was
previously installed.
@retval EFI_INVALID_PARAMETER ExceptionHandler is NULL, and a handler for ExceptionType was not
previously installed.
@retval EFI_UNSUPPORTED       The interrupt specified by ExceptionType is not supported,
or this function is not supported.
**/
EFI_STATUS
EFIAPI
RegisterCpuInterruptHandler (
  IN EFI_EXCEPTION_TYPE         ExceptionType,
  IN EFI_CPU_INTERRUPT_HANDLER  ExceptionHandler
  )
{
  if ((UINTN)ExceptionType > mMaxExceptionNumber) {
    return EFI_UNSUPPORTED;
  }

  if ((ExceptionHandler != NULL) && (mExceptionHandlers[ExceptionType] != NULL)) {
    return EFI_ALREADY_STARTED;
  }

  mExceptionHandlers[ExceptionType] = ExceptionHandler;

  return EFI_SUCCESS;
}

/**
  Setup separate stacks for certain exception handlers.
  If the input Buffer and BufferSize are both NULL, use global variable if possible.

  @param[in]       Buffer        Point to buffer used to separate exception stack.
  @param[in, out]  BufferSize    On input, it indicates the byte size of Buffer.
                                 If the size is not enough, the return status will
                                 be EFI_BUFFER_TOO_SMALL, and output BufferSize
                                 will be the size it needs.

  @retval EFI_SUCCESS             The stacks are assigned successfully.
  @retval EFI_UNSUPPORTED         This function is not supported.
  @retval EFI_BUFFER_TOO_SMALL    This BufferSize is too small.
**/
EFI_STATUS
EFIAPI
InitializeSeparateExceptionStacks (
  IN     VOID   *Buffer,
  IN OUT UINTN  *BufferSize
  )
{
  return EFI_SUCCESS;
}