summaryrefslogtreecommitdiff
path: root/ReferenceCode/Haswell/CpuInit/Dxe/x64/MemoryOperation.c
blob: 5fae5066702f9188a73944ae88a1a443a1a80b35 (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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/** @file
  Memory Operation Functions for IA32 Architecture.

@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 "CpuLib.h"
#include "MpCommon.h"
#include "VirtualMemory.h"
#include "MemoryAttribute.h"
#endif

VOID
InitializeExternalVectorTablePtr (
  EFI_CPU_INTERRUPT_HANDLER *VectorTable
  );

extern EFI_CPU_INTERRUPT_HANDLER mExternalVectorTable[];
extern EFI_PHYSICAL_ADDRESS      mBackupBuffer;

UINT8 *mPageStore     = NULL;
UINTN mPageStoreSize  = 16;
UINTN mPageStoreIndex = 0;

UINT64 mValidMtrrAddressMask;
UINT64 mValidMtrrBitsMask;

///
/// BugBug: Non Portable
///
#if defined (__GNUC__)
#define ALIGN_16BYTE_BOUNDRY  __attribute__ ((aligned (16)))
#else
#define ALIGN_16BYTE_BOUNDRY  __declspec (align (16))
#endif

#pragma pack(1)
typedef struct {
  UINT16 LimitLow;
  UINT16 BaseLow;
  UINT8 BaseMiddle;
  UINT8 Attributes1;
  UINT8 Attributes2;
  UINT8 BaseHigh;
} SEGMENT_DESCRIPTOR_x64;

typedef struct {
  UINT16 Limit;
  UINTN Base;
} PSEUDO_DESCRIPTOR_x64;

#pragma pack()

ALIGN_16BYTE_BOUNDRY SEGMENT_DESCRIPTOR_x64 gGdt[] = {
  {    /// NULL Selector: selector[0]
    0, /// limit 15:0
    0, /// base  15:0
    0, /// base  23:16
    0, ///
    0, /// type & limit 19:16
    0, /// base  31:24
    ///   0,  /// base  63:32
    ///   0   /// reserved
    ///
  },
  {         /// Linear Selector: selector[8]
    0xffff, /// limit 15:0
    0,      /// base  15:0
    0,      /// base  23:16
    0x93,   /// present, ring 0, data, expand-up writable
    0xcf,   /// type & limit 19:16
    0,      /// base  31:24
    ///   0,      /// base  63:32
    ///   0       /// reserved
    ///
  },
  {         /// Linear code Selector: selector[10]
    0xffff, /// limit 15:0
    0,      /// base  15:0
    0,      /// base  23:16
    0x9b,   /// present, ring 0, code, expand-up writable
    0xcf,   /// type & limit 19:16
    0,      /// base  31:24
    ///  0,      /// base  63:32
    ///  0       /// reserved
    ///
  },
  {         /// Compatibility mode data Selector: selector[18]
    0xffff, /// limit 15:0
    0,      /// base  15:0
    0,      /// base  23:16
    0x93,   /// type & limit 19:16
    0xcf,
    0,      /// base  31:24
    /// 0,      /// base  63:32
    /// 0       /// reserved
    ///
  },
  {         /// Compatibility code Selector: selector[20]
    0xffff, /// limit 15:0
    0,      /// base  15:0
    0,      /// base  23:16
    0x9b,   /// type & limit 19:16
    0xcf,
    0,      /// base  31:24
    ///  0,      /// base  63:32
    ///  0       /// reserved
    ///
  },
  {     /// Spare3 Selector: selector[28]
    0,  /// limit 15:0
    0,  /// base  15:0
    0,  /// base  23:16
    0,  /// type & limit 19:16
    0,  /// base  31:24
    0,
    ///
    ///  0,  /// base  63:32
    ///  0   /// reserved
    ///
  },
  {         /// 64-bit data Selector:selector[30]
    0xffff, /// limit 15:0
    0,      /// base  15:0
    0,      /// base  23:16
    0x93,   /// type & limit 19:16
    0xcf,
    0,      /// base  31:24
    ///  0,  /// base  63:32
    ///  0   /// reserved
    ///
  },
  {         /// 64-bit code Selector: selector[38]
    0xffff, /// limit 15:0
    0,      /// base  15:0
    0,      /// base  23:16
    0x9b,   /// type & limit 19:16
    0xaf,
    0,      /// base  31:24
    ///  0,  /// base  63:32
    ///  0   /// reserved
    ///
  },
  {     /// Spare3 Selector: selector[40]
    0,  /// limit 15:0
    0,  /// base  15:0
    0,  /// base  23:16
    0,  /// type & limit 19:16
    0,  /// base  31:24
    0,
    ///
    ///  0,  /// base  63:32
    ///  0   /// reserved
    ///
  }
};

ALIGN_16BYTE_BOUNDRY PSEUDO_DESCRIPTOR_x64 gGdtPseudoDescriptor = {
  sizeof (gGdt) - 1,
  (UINTN) gGdt
};

INTERRUPT_GATE_DESCRIPTOR gIdtTable[INTERRUPT_VECTOR_NUMBER] = { 0 };

ALIGN_16BYTE_BOUNDRY PSEUDO_DESCRIPTOR_x64 gLidtPseudoDescriptor = {
  sizeof (gIdtTable) - 1,
  (UINTN) gIdtTable
};

/**
  Init Global Descriptor table
**/
VOID
InitializeSelectors (
  VOID
  )
{
  CpuLoadGlobalDescriptorTable (&gGdtPseudoDescriptor);
}

/**
  Generic IDT Vector Handlers for the Host
**/
VOID
AsmIdtVector00 (
  VOID
  );

/**
  Initialize Interrupt descriptor Tables
**/
VOID
InitializeInterruptTables (
  VOID
  )
{
  UINT16                    CodeSegment;
  INTERRUPT_GATE_DESCRIPTOR *IdtEntry;
  UINT8                     *CurrentHandler;
  UINT32                    Index;

  CodeSegment     = CpuCodeSegment ();

  IdtEntry        = gIdtTable;
  CurrentHandler  = (UINT8 *) (UINTN) AsmIdtVector00;
  for (Index = 0; Index < INTERRUPT_VECTOR_NUMBER; Index++) {
    IdtEntry[Index].Offset15To0     = (UINT16) (UINTN) CurrentHandler;
    IdtEntry[Index].SegmentSelector = CodeSegment;
    IdtEntry[Index].Attributes      = INTERRUPT_GATE_ATTRIBUTE;
    ///
    /// 8e00;
    ///
    IdtEntry[Index].Offset31To16  = (UINT16) ((UINTN) CurrentHandler >> 16);
    IdtEntry[Index].Offset63To32  = (UINT32) ((UINTN) CurrentHandler >> 32);

    CurrentHandler += 0x8;
    ///
  }

  CpuLoadInterruptDescriptorTable (&gLidtPseudoDescriptor);

  return;
}

/**
  Initialize cache attributes based on MTRR
**/
VOID
InitailizeCacheAttributes (
  VOID
  )
{
  EFI_STATUS           Status;
  EFI_PHYSICAL_ADDRESS Page;
  EFI_CPUID_REGISTER   FeatureInfo;
  EFI_CPUID_REGISTER   FunctionInfo;
  UINT8                PhysicalAddressBits;
  UINT32               MsrNum;
  UINT64               TempQword;
  UINT64               ComplementBits;
  UINT32               VariableMtrrLimit;

  VariableMtrrLimit = (UINT32) (AsmReadMsr64 (IA32_MTRR_CAP) & B_IA32_MTRR_VARIABLE_SUPPORT);

  ///
  /// Allocate 16 pages
  ///
  Status            = (gBS->AllocatePages)(AllocateAnyPages, EfiBootServicesData, mPageStoreSize, &Page);
  ASSERT_EFI_ERROR (Status);

  mPageStore = (UINT8 *) (UINTN) Page;

  ZeroMem (mPageStore, 0x1000 * mPageStoreSize);

  ///
  /// Check returned value of Eax for extended CPUID functions
  ///
  AsmCpuid (
    CPUID_EXTENDED_FUNCTION,
    &FunctionInfo.RegEax,
    &FunctionInfo.RegEbx,
    &FunctionInfo.RegEcx,
    &FunctionInfo.RegEdx
    );

  PhysicalAddressBits = 36;

  ///
  /// If CPU supports extended functions, get the Physical Address size by reading EAX[7:0]
  ///
  if (FunctionInfo.RegEax > CPUID_EXTENDED_FUNCTION) {
    AsmCpuid (
      CPUID_VIR_PHY_ADDRESS_SIZE,
      &FeatureInfo.RegEax,
      &FeatureInfo.RegEbx,
      &FeatureInfo.RegEcx,
      &FeatureInfo.RegEdx
      );
    PhysicalAddressBits = (UINT8) FeatureInfo.RegEax;
  }

  mValidMtrrBitsMask    = (((UINT64) 1) << PhysicalAddressBits) - 1;
  mValidMtrrAddressMask = mValidMtrrBitsMask & 0xfffffffffffff000;

  ComplementBits        = mValidMtrrBitsMask & 0xfffffff000000000;
  if (ComplementBits != 0) {
    ///
    /// Disable cache and clear the corresponding MTRR bits
    ///
    PreMtrrChange ();
    for (MsrNum = CACHE_VARIABLE_MTRR_BASE;
         MsrNum < (CACHE_VARIABLE_MTRR_BASE + VariableMtrrLimit * 2 - 1);
         MsrNum += 2
         ) {
      TempQword = AsmReadMsr64 (MsrNum + 1);
      if ((TempQword & B_CACHE_MTRR_VALID) != 0) {
        ///
        /// MTRR Physical Mask
        ///
        TempQword = TempQword | ComplementBits;
        AsmWriteMsr64 (MsrNum + 1, TempQword);
      }
    }

    ///
    /// Enable Cache and set the corresponding MTRR bits
    ///
    PostMtrrChange ();
  }
}

/**
  Allocate zeroed pages

  @retval Pointer to the page buffer
**/
VOID *
AllocateZeroedPage (
  VOID
  )
{
  if (mPageStoreIndex >= mPageStoreSize) {
    ///
    /// We are out of space
    ///
    return NULL;
  }

  return (VOID *) (UINTN) &mPageStore[0x1000 * mPageStoreIndex++];
}

/**
  Convert 2MB page tables to 4KB page tables

  @param[in] PageAddress             - Page address to convert
  @param[in] PageDirectoryToConvert  - Page table that will be converted
**/
VOID
Convert2MBPageTo4KPages (
  IN EFI_PHYSICAL_ADDRESS     PageAddress,
  IN OUT x64_PAGE_TABLE_ENTRY **PageDirectoryToConvert
  )
{
  UINTN                   Index;
  EFI_PHYSICAL_ADDRESS    WorkingAddress;
  x64_PAGE_TABLE_ENTRY_4K *PageTableEntry;
  x64_PAGE_TABLE_ENTRY    Attributes;

  ///
  /// Save the attributes of the 2MB table
  ///
  Attributes.Page2Mb.Uint64 = (*PageDirectoryToConvert)->Page2Mb.Uint64;

  ///
  /// Convert PageDirectoryEntry2MB into a 4K Page Directory
  ///
  PageTableEntry = AllocateZeroedPage ();
  if (PageTableEntry == NULL) {
    return;
  }
  (*PageDirectoryToConvert)->Page2Mb.Uint64         = (UINT64) PageTableEntry;
  (*PageDirectoryToConvert)->Page2Mb.Bits.ReadWrite = 1;
  (*PageDirectoryToConvert)->Page2Mb.Bits.Present   = 1;

  WorkingAddress = PageAddress;
  for (Index = 0; Index < 512; Index++, PageTableEntry++, WorkingAddress += 0x1000) {
    PageTableEntry->Uint64        = (UINT64) WorkingAddress;
    PageTableEntry->Bits.Present  = 1;

    ///
    /// Update the new page to have the same attributes as the 2MB page
    ///
    PageTableEntry->Bits.ReadWrite      = Attributes.Common.ReadWrite;
    PageTableEntry->Bits.CacheDisabled  = Attributes.Common.CacheDisabled;
    PageTableEntry->Bits.WriteThrough   = Attributes.Common.WriteThrough;

    if (WorkingAddress == PageAddress) {
      ///
      /// Return back the 4K page that matches the Working addresss
      ///
      *PageDirectoryToConvert = (x64_PAGE_TABLE_ENTRY *) PageTableEntry;
    }
  }
}

/**
  Get current memory mapping information

  @param[in] BaseAddress - get current memory mapping by this Base address
  @param[in] PageTable   - page table that translated this base address
  @param[in] Page2MBytes - TRUE if this is 2MBytes page table

  @retval EFI_NOT_FOUND - page table not found
  @retval EFI_SUCCESS   - page table found
**/
EFI_STATUS
GetCurrentMapping (
  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
  OUT x64_PAGE_TABLE_ENTRY **PageTable,
  OUT BOOLEAN              *Page2MBytes
  )
{
  UINT64                                    Cr3;
  x64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *PageMapLevel4Entry;
  x64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *PageDirectoryPointerEntry;
  x64_PAGE_TABLE_ENTRY_2M                   *PageTableEntry2Mb;
  x64_PAGE_DIRECTORY_ENTRY_4K               *PageDirectoryEntry4k;
  x64_PAGE_TABLE_ENTRY_4K                   *PageTableEntry4k;
  UINTN                                     Pml4Index;
  UINTN                                     PdpIndex;
  UINTN                                     Pde2MbIndex;
  UINTN                                     PteIndex;

  Cr3                 = AsmReadCr3 ();

  PageMapLevel4Entry  = (x64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *) (Cr3 & 0x000ffffffffff000);

  Pml4Index           = (UINTN) RShiftU64 (BaseAddress, 39) & 0x1ff;
  if (PageMapLevel4Entry[Pml4Index].Bits.Present == 0) {
    return EFI_NOT_FOUND;
  }

  PageDirectoryPointerEntry = (x64_PAGE_MAP_AND_DIRECTORY_POINTER_2MB_4K *) (PageMapLevel4Entry[Pml4Index].Uint64 & 0x000ffffffffff000);
  PdpIndex = (UINTN) RShiftU64 (BaseAddress, 30) & 0x1ff;
  if (PageDirectoryPointerEntry[PdpIndex].Bits.Present == 0) {
    return EFI_NOT_FOUND;
  }

  PageTableEntry2Mb = (x64_PAGE_TABLE_ENTRY_2M *) (PageDirectoryPointerEntry[PdpIndex].Uint64 & 0x000ffffffffff000);
  Pde2MbIndex       = (UINTN) RShiftU64 (BaseAddress, 21) & 0x1ff;
  if (PageTableEntry2Mb[Pde2MbIndex].Bits.Present == 0) {
    return EFI_NOT_FOUND;
  }

  if (PageTableEntry2Mb[Pde2MbIndex].Bits.MustBe1 == 1) {
    ///
    /// We found a 2MByte page so lets return it
    ///
    *Page2MBytes  = TRUE;
    *PageTable    = (x64_PAGE_TABLE_ENTRY *) &PageTableEntry2Mb[Pde2MbIndex].Uint64;
    return EFI_SUCCESS;
  }

  ///
  /// 4K page so keep walking
  ///
  PageDirectoryEntry4k  = (x64_PAGE_DIRECTORY_ENTRY_4K *) &PageTableEntry2Mb[Pde2MbIndex].Uint64;

  PageTableEntry4k      = (x64_PAGE_TABLE_ENTRY_4K *) (PageDirectoryEntry4k[Pde2MbIndex].Uint64 & 0x000ffffffffff000);
  PteIndex              = (UINTN) RShiftU64 (BaseAddress, 12) & 0x1ff;
  if (PageTableEntry4k[PteIndex].Bits.Present == 0) {
    return EFI_NOT_FOUND;
  }

  *Page2MBytes  = FALSE;
  *PageTable    = (x64_PAGE_TABLE_ENTRY *) &PageTableEntry4k[PteIndex];
  return EFI_SUCCESS;
}

/**
  Prepare memory for essential system tables.

  @retval EFI_SUCCESS              - Memory successfully prepared.
**/
EFI_STATUS
PrepareMemory (
  VOID
  )
{
  ///
  /// Allocate space to convert 2MB page tables to 4K tables.
  ///  This can not be done at call time as the TPL level will
  ///  not be correct.
  ///
  InitailizeCacheAttributes ();

  InitializeExternalVectorTablePtr (mExternalVectorTable);
  ///
  /// Initialize the Interrupt Descriptor Table
  ///
  InitializeInterruptTables ();

  return EFI_SUCCESS;
}

/**
  Prepare Wakeup Buffer and stack for APs.

  @param[in] WakeUpBuffer             - Pointer to the address of wakeup buffer for output.
  @param[in] StackAddressStart        - Pointer to the stack address of APs for output.
  @param[in] MaximumCPUsForThisSystem - Maximum CPUs in this system.

  @retval EFI_SUCCESS              - Memory successfully prepared for APs.
  @retval Other                    - Error occurred while allocating memory.
**/
EFI_STATUS
PrepareMemoryForAPs (
  OUT EFI_PHYSICAL_ADDRESS *WakeUpBuffer,
  OUT VOID                 **StackAddressStart,
  IN UINTN                 MaximumCPUsForThisSystem
  )
{
  EFI_STATUS              Status;
  MP_ASSEMBLY_ADDRESS_MAP AddressMap;

  ///
  /// Release All APs with a lock and wait for them to retire to rendezvous procedure.
  /// We need a page (4KB) of memory for IA-32 to use broadcast APIs, on a temporary basis.
  ///
  Status = AllocateWakeUpBuffer (WakeUpBuffer);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  ///
  /// Claim memory for AP stack
  ///
  Status = AllocateReservedMemoryBelow4G (
            MaximumCPUsForThisSystem * STACK_SIZE_PER_PROC,
            StackAddressStart
            );

  if (EFI_ERROR (Status)) {
    (gBS->FreePages)(*WakeUpBuffer, 1);
    return Status;
  }

  AsmGetAddressMap (&AddressMap);
  CopyMem ((VOID *) (UINTN) *WakeUpBuffer, AddressMap.RendezvousFunnelAddress, AddressMap.Size);
  *(UINT32 *) (UINTN) (*WakeUpBuffer + AddressMap.FlatJumpOffset + 3) = (UINT32) (*WakeUpBuffer + AddressMap.PModeEntryOffset);
  *(UINT32 *) (UINTN) (*WakeUpBuffer + AddressMap.LongJumpOffset + 2) = (UINT32) (*WakeUpBuffer + AddressMap.LModeEntryOffset);

  return EFI_SUCCESS;
}

/**
  Prepare exchange information for APs.

  @param[in] ExchangeInfo      - Pointer to the exchange info buffer for output.
  @param[in] StackAddressStart - Start address of APs' stacks.
  @param[in] ApFunction        - Address of function assigned to AP.
  @param[in] WakeUpBuffer      - Pointer to the address of wakeup buffer.

  @retval EFI_SUCCESS       - Exchange Info successfully prepared for APs.
**/
EFI_STATUS
PrepareExchangeInfo (
  OUT MP_CPU_EXCHANGE_INFO *ExchangeInfo,
  IN VOID                  *StackAddressStart,
  IN VOID                  *ApFunction,
  IN EFI_PHYSICAL_ADDRESS  WakeUpBuffer
  )
{
  ZeroMem ((VOID *) ExchangeInfo, EFI_PAGE_SIZE - MP_CPU_EXCHANGE_INFO_OFFSET);

  ExchangeInfo->Lock        = VacantFlag;
  ExchangeInfo->StackStart  = StackAddressStart;
  ExchangeInfo->StackSize   = STACK_SIZE_PER_PROC;
  ExchangeInfo->ApFunction  = ApFunction;

  CopyMem (
          (VOID *) (UINTN) &ExchangeInfo->GdtrProfile,
          (VOID *) (UINTN) mAcpiCpuData->GdtrProfile,
          sizeof (PSEUDO_DESCRIPTOR)
          );
  CopyMem (
          (VOID *) (UINTN) &ExchangeInfo->IdtrProfile,
          (VOID *) (UINTN) mAcpiCpuData->IdtrProfile,
          sizeof (PSEUDO_DESCRIPTOR)
          );

  ExchangeInfo->BufferStart = (UINT32) WakeUpBuffer;
  ExchangeInfo->Cr3         = (UINT32) (AsmReadCr3 ());
  ExchangeInfo->InitFlag    = 1;

  return EFI_SUCCESS;
}

/**
  Prepare Wakeup Buffer and stack for APs.

  @param[in] WakeUpBuffer      - Pointer to the address of wakeup buffer for output.
  @param[in] StackAddressStart - Pointer to the stack address of APs for output.

  @retval EFI_SUCCESS       - Memory successfully prepared for APs.
**/
EFI_STATUS
S3PrepareMemoryForAPs (
  OUT EFI_PHYSICAL_ADDRESS *WakeUpBuffer,
  OUT VOID                 **StackAddressStart
  )
{
  return EFI_SUCCESS;
}

/**
  Prepare exchange information for APs.

  @param[in] ExchangeInfo      - Pointer to the exchange info for output.
  @param[in] StackAddressStart - Start address of APs' stacks.
  @param[in] ApFunction        - Address of function assigned to AP.
  @param[in] WakeUpBuffer      - Pointer to the address of wakeup buffer.

  @retval EFI_SUCCESS       - Exchange Info successfully prepared for APs.
**/
EFI_STATUS
S3PrepareExchangeInfo (
  OUT MP_CPU_EXCHANGE_INFO *ExchangeInfo,
  IN VOID                  *StackAddressStart,
  IN VOID                  *ApFunction,
  IN EFI_PHYSICAL_ADDRESS  WakeUpBuffer
  )
{
  return EFI_SUCCESS;
}

/**
  Dynamically write the far jump destination in APs' wakeup buffer,
  in order to refresh APs' CS registers for mode switching.
**/
VOID
RedirectFarJump (
  VOID
  )
{
  MP_ASSEMBLY_ADDRESS_MAP AddressMap;

  AsmGetAddressMap (&AddressMap);
  *(UINT32 *) (UINTN) (mAcpiCpuData->WakeUpBuffer + AddressMap.FlatJumpOffset + 3) = (UINT32) (mAcpiCpuData->WakeUpBuffer + AddressMap.PModeEntryOffset);
  *(UINT32 *) (UINTN) (mAcpiCpuData->WakeUpBuffer + AddressMap.LongJumpOffset + 2) = (UINT32) (mAcpiCpuData->WakeUpBuffer + AddressMap.LModeEntryOffset);

  return;
}

/**
  Prepare GDTR and IDTR for AP

  @param[in] Gdtr  - The GDTR profile
  @param[in] Idtr  - The IDTR profile

  @retval EFI_STATUS  - status returned by each sub-routine
  @retval EFI_SUCCESS - GDTR and IDTR has been prepared for AP
**/
EFI_STATUS
PrepareGdtIdtForAP (
  OUT PSEUDO_DESCRIPTOR *Gdtr,
  OUT PSEUDO_DESCRIPTOR *Idtr
  )
{
  INTERRUPT_GATE_DESCRIPTOR *IdtForAP;
  SEGMENT_DESCRIPTOR        *GdtForAP;

  PSEUDO_DESCRIPTOR *IdtrForBSP;
  PSEUDO_DESCRIPTOR *GdtrForBSP;

  UINT16     *MceHandler;
  EFI_STATUS Status;

  AsmGetGdtrIdtr (&GdtrForBSP, &IdtrForBSP);

  ///
  /// Allocate reserved memory for IDT
  ///
  Status = AllocateAlignedReservedMemory (
            IdtrForBSP->Limit + 1,
            8,
            (VOID **) &IdtForAP
            );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  ///
  /// Allocate reserved memory for GDT
  ///
  Status = AllocateAlignedReservedMemory (
            GdtrForBSP->Limit + 1,
            8,
            (VOID **) &GdtForAP
            );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = (gBS->AllocatePool)(EfiReservedMemoryType, SIZE_OF_MCE_HANDLER, (VOID **) &MceHandler);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  ///
  /// McheHandler content: iret (opcode = 0xcf)
  ///
  *MceHandler = 0xCF48;

  CopyMem (GdtForAP, (VOID *) GdtrForBSP->Base, GdtrForBSP->Limit + 1);
  CopyMem (IdtForAP, (VOID *) IdtrForBSP->Base, IdtrForBSP->Limit + 1);

  IdtForAP[INTERRUPT_HANDLER_MACHINE_CHECK].Offset15To0   = (UINT16) (UINTN) MceHandler;
  IdtForAP[INTERRUPT_HANDLER_MACHINE_CHECK].Offset31To16  = (UINT16) ((UINTN) MceHandler >> 16);
  IdtForAP[INTERRUPT_HANDLER_MACHINE_CHECK].Offset63To32  = (UINT32) ((UINTN) MceHandler >> 32);

  ///
  /// Create Gdtr, IDTR profile
  ///
  Gdtr->Base  = (UINTN) GdtForAP;
  Gdtr->Limit = GdtrForBSP->Limit;

  Idtr->Base  = (UINTN) IdtForAP;
  Idtr->Limit = IdtrForBSP->Limit;

  return EFI_SUCCESS;
}