summaryrefslogtreecommitdiff
path: root/ReferenceCode/Haswell/CpuInit/Dxe/x64/Exception.c
blob: bcd306bf2eb5ac55eda44218ab0e2cd87121fb1c (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
/** @file
  EM64T Exception Handler

@copyright
  Copyright (c) 2006 - 2012 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement

**/
#if !defined(EDK_RELEASE_VERSION) || (EDK_RELEASE_VERSION < 0x00020000)
#include "EdkIIGlueDxe.h"
#include "CpuInitDxe.h"
#include "MpCommon.h"
#include "Exception.h"
#endif

typedef
VOID
(*EFI_INSTALL_EXCEPTION)(
  IN UINT32 InterruptType,
  IN VOID   *SystemContext
  );

typedef struct {
  UINT32 ErrorMessage;
  UINT8 Interrupt;
} EFI_EXCEPTION_HANDLER;

///
/// Error code flag indicating whether or not an error code will be
/// pushed on the stack if an exception occurs.
///
/// 1 means an error code will be pushed, otherwise 0
///
/// bit 0 - exception 0
/// bit 1 - exception 1
/// etc.
///
UINT32 mErrorCodeFlag = 0x00027d00;

///
/// Local Table
///
EFI_EXCEPTION_HANDLER mExceptionTable[] = {
  {
    EFI_SW_EC_IA32_DIVIDE_ERROR,
    INTERRUPT_HANDLER_DIVIDE_ZERO
  },
  {
    EFI_SW_EC_IA32_DEBUG,
    INTERRUPT_HANDLER_DEBUG
  },
  {
    EFI_SW_EC_IA32_NMI,
    INTERRUPT_HANDLER_NMI
  },
  {
    EFI_SW_EC_IA32_BREAKPOINT,
    INTERRUPT_HANDLER_BREAKPOINT
  },
  {
    EFI_SW_EC_IA32_OVERFLOW,
    INTERRUPT_HANDLER_OVERFLOW
  },
  {
    EFI_SW_EC_IA32_BOUND,
    INTERRUPT_HANDLER_BOUND
  },
  {
    EFI_SW_EC_IA32_INVALID_OPCODE,
    INTERRUPT_HANDLER_INVALID_OPCODE
  },
  ///
  /// Interrupt 7, 9, 15 not defined in the debug support protocol. Hence no status codes for them!
  ///
  {
    EFI_SW_EC_IA32_DOUBLE_FAULT,
    INTERRUPT_HANDLER_DOUBLE_FAULT
  },
  {
    EFI_SW_EC_IA32_INVALID_TSS,
    INTERRUPT_HANDLER_INVALID_TSS
  },
  {
    EFI_SW_EC_IA32_SEG_NOT_PRESENT,
    INTERRUPT_HANDLER_SEGMENT_NOT_PRESENT
  },
  {
    EFI_SW_EC_IA32_STACK_FAULT,
    INTERRUPT_HANDLER_STACK_SEGMENT_FAULT
  },
  {
    EFI_SW_EC_IA32_GP_FAULT,
    INTERRUPT_HANDLER_GP_FAULT
  },
  {
    EFI_SW_EC_IA32_PAGE_FAULT,
    INTERRUPT_HANDLER_PAGE_FAULT
  },
  {
    EFI_SW_EC_IA32_FP_ERROR,
    INTERRUPT_HANDLER_MATH_FAULT
  },
  {
    EFI_SW_EC_IA32_ALIGNMENT_CHECK,
    INTERRUPT_HANDLER_ALIGNMENT_FAULT
  },
  {
    EFI_SW_EC_IA32_MACHINE_CHECK,
    INTERRUPT_HANDLER_MACHINE_CHECK
  },
  {
    EFI_SW_EC_IA32_SIMD,
    INTERRUPT_HANDLER_STREAMING_SIMD
  }
};

UINTN mExceptionNumber = sizeof (mExceptionTable) / sizeof (EFI_EXCEPTION_HANDLER);

CPU_STATUS_CODE_TEMPLATE mStatusCodeData = {
  {
    sizeof (EFI_STATUS_CODE_DATA),
    sizeof (EFI_SYSTEM_CONTEXT_X64),
    EFI_STATUS_CODE_DATA_TYPE_EXCEPTION_HANDLER_GUID
  },
  {
    0
  }
};

UINT8 mExceptionLock = 0;

/**
  Report StatusCode for Exception

  @param[in] InterruptType - Interrupt type
  @param[in] SystemContext - EFI_SYSTEM_CONTEXT

  @retval EFI_SUCCESS
**/
EFI_STATUS
ReportData (
  IN EFI_EXCEPTION_TYPE InterruptType,
  IN EFI_SYSTEM_CONTEXT SystemContext
  )
{
  UINT32 ErrorMessage;
  UINT32 Index;

  CopyMem (
          &mStatusCodeData.SystemContext.SystemContextX64,
          SystemContext.SystemContextX64,
          sizeof (EFI_SYSTEM_CONTEXT_X64)
          );

  ErrorMessage = EFI_SOFTWARE_DXE_BS_DRIVER;
  for (Index = 0; Index < mExceptionNumber; Index++) {
    if (mExceptionTable[Index].Interrupt == InterruptType) {
      ErrorMessage |= mExceptionTable[Index].ErrorMessage;
      break;
    }
  }

  ReportStatusCode (
          (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
          EFI_SOFTWARE_UNSPECIFIED | ErrorMessage
          );

  return EFI_SUCCESS;
}

/**
  Common exception handler

  @param[in] InterruptType - Exception type
  @param[in] SystemContext - EFI_SYSTEM_CONTEXT
**/
VOID
EFIAPI
CommonExceptionHandler (
  IN EFI_EXCEPTION_TYPE InterruptType,
  IN EFI_SYSTEM_CONTEXT SystemContext
  )
{
  AsmAcquireMPLock (&mExceptionLock);

  DEBUG (
          (EFI_D_ERROR,
           "!!!! X64 Exception Type - %016lx    CPU Apic ID - %08x!!!!\n",
           InterruptType,
           GetApicID (NULL,
                      NULL))
          );
  DEBUG (
          (EFI_D_ERROR,
           "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
           SystemContext.SystemContextX64->Rip,
           SystemContext.SystemContextX64->Cs,
           SystemContext.SystemContextX64->Rflags)
          );
  if (mErrorCodeFlag & (1 << InterruptType)) {
    DEBUG (
            (EFI_D_ERROR,
             "ExceptionData - %016lx\n",
             SystemContext.SystemContextX64->ExceptionData)
            );
  }

  DEBUG (
          (EFI_D_ERROR,
           "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
           SystemContext.SystemContextX64->Rax,
           SystemContext.SystemContextX64->Rcx,
           SystemContext.SystemContextX64->Rdx)
          );
  DEBUG (
          (EFI_D_ERROR,
           "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
           SystemContext.SystemContextX64->Rbx,
           SystemContext.SystemContextX64->Rsp,
           SystemContext.SystemContextX64->Rbp)
          );
  DEBUG (
          (EFI_D_ERROR,
           "RSI - %016lx, RDI - %016lx\n",
           SystemContext.SystemContextX64->Rsi,
           SystemContext.SystemContextX64->Rdi)
          );
  DEBUG (
          (EFI_D_ERROR,
           "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
           SystemContext.SystemContextX64->R8,
           SystemContext.SystemContextX64->R9,
           SystemContext.SystemContextX64->R10)
          );
  DEBUG (
          (EFI_D_ERROR,
           "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
           SystemContext.SystemContextX64->R11,
           SystemContext.SystemContextX64->R12,
           SystemContext.SystemContextX64->R13)
          );
  DEBUG (
          (EFI_D_ERROR,
           "R14 - %016lx, R15 - %016lx\n",
           SystemContext.SystemContextX64->R14,
           SystemContext.SystemContextX64->R15)
          );
  DEBUG (
          (EFI_D_ERROR,
           "DS - %016lx, ES - %016lx, FS - %016lx\n",
           SystemContext.SystemContextX64->Ds,
           SystemContext.SystemContextX64->Es,
           SystemContext.SystemContextX64->Fs)
          );
  DEBUG (
          (EFI_D_ERROR,
           "GS - %016lx, SS - %016lx\n",
           SystemContext.SystemContextX64->Gs,
           SystemContext.SystemContextX64->Ss)
          );
  DEBUG (
          (EFI_D_ERROR,
           "GDTR - %016lx %016lx, LDTR - %016lx\n",
           SystemContext.SystemContextX64->Gdtr[0],
           SystemContext.SystemContextX64->Gdtr[1],
           SystemContext.SystemContextX64->Ldtr)
          );
  DEBUG (
          (EFI_D_ERROR,
           "IDTR - %016lx %016lx, TR - %016lx\n",
           SystemContext.SystemContextX64->Idtr[0],
           SystemContext.SystemContextX64->Idtr[1],
           SystemContext.SystemContextX64->Tr)
          );
  DEBUG (
          (EFI_D_ERROR,
           "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
           SystemContext.SystemContextX64->Cr0,
           SystemContext.SystemContextX64->Cr2,
           SystemContext.SystemContextX64->Cr3)
          );
  DEBUG (
          (EFI_D_ERROR,
           "CR4 - %016lx, CR8 - %016lx\n",
           SystemContext.SystemContextX64->Cr4,
           SystemContext.SystemContextX64->Cr8)
          );
  DEBUG (
          (EFI_D_ERROR,
           "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
           SystemContext.SystemContextX64->Dr0,
           SystemContext.SystemContextX64->Dr1,
           SystemContext.SystemContextX64->Dr2)
          );
  DEBUG (
          (EFI_D_ERROR,
           "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
           SystemContext.SystemContextX64->Dr3,
           SystemContext.SystemContextX64->Dr6,
           SystemContext.SystemContextX64->Dr7)
          );

  ///
  /// Report Status Code
  ///
  ReportData (InterruptType, SystemContext);
  AsmReleaseMPLock (&mExceptionLock);

  ///
  /// Use this macro to hang so that the compiler does not optimize out
  /// the following RET instructions. This allows us to return if we
  /// have a debugger attached.
  ///
  EFI_DEADLOOP ();

  return;
}

/**
  Install the IA-32 EM64T Exception Handler.
  The current operation (which likely will change) will uninstall all the
  pertinent exception handlers (0-7, 10-14, 16-19) except for Int8 which the timer
  is currently sitting on (or soon will be).

  It then installs all the appropriate handlers for each exception.

  The handler then calls gRT->ReportStatusCode with a specific progress code.  The
  progress codes for now start at 0x200 for IA-32 processors. See Status Code
  Specification for details. The Status code Specification uses the enumeration from
  the EFI 1.1 Debug Support Protocol.

  @param[in] CpuProtocol - Instance of CPU Arch Protocol

  @retval EFI_SUCCESS - This function always return success after registering handlers.
**/
EFI_STATUS
InitializeException (
  IN EFI_CPU_ARCH_PROTOCOL *CpuProtocol
  )
{
  EFI_STATUS Status;
  UINTN      Index;

  CpuProtocol->DisableInterrupt (CpuProtocol);

  for (Index = 0; Index < mExceptionNumber; Index++) {
    Status = CpuProtocol->RegisterInterruptHandler (CpuProtocol, mExceptionTable[Index].Interrupt, NULL);
    ///
    /// Add in our handler
    ///
    Status = CpuProtocol->RegisterInterruptHandler (
                            CpuProtocol,
                            mExceptionTable[Index].Interrupt,
                            CommonExceptionHandler
                            );
    ASSERT_EFI_ERROR (Status);
  }

  return EFI_SUCCESS;
}