]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointer.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / PeiServicesTablePointerLibIdt / PeiServicesTablePointer.c
1 /** @file
2 PEI Services Table Pointer Library for IA-32 and x64.
3
4 According to PI specification, the peiservice pointer is stored prior at IDT
5 table in IA32 and x64 architecture.
6
7 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <PiPei.h>
13
14 #include <Library/BaseLib.h>
15 #include <Library/PeiServicesTablePointerLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18
19 /**
20 Retrieves the cached value of the PEI Services Table pointer.
21
22 Returns the cached value of the PEI Services Table pointer in a CPU specific manner
23 as specified in the CPU binding section of the Platform Initialization Pre-EFI
24 Initialization Core Interface Specification.
25
26 If the cached PEI Services Table pointer is NULL, then ASSERT().
27
28 @return The pointer to PeiServices.
29
30 **/
31 CONST EFI_PEI_SERVICES **
32 EFIAPI
33 GetPeiServicesTablePointer (
34 VOID
35 )
36 {
37 CONST EFI_PEI_SERVICES **PeiServices;
38 IA32_DESCRIPTOR Idtr;
39
40 AsmReadIdtr (&Idtr);
41 PeiServices = (CONST EFI_PEI_SERVICES **)(*(UINTN *)(Idtr.Base - sizeof (UINTN)));
42 ASSERT (PeiServices != NULL);
43 return PeiServices;
44 }
45
46 /**
47 Caches a pointer PEI Services Table.
48
49 Caches the pointer to the PEI Services Table specified by PeiServicesTablePointer
50 in a CPU specific manner as specified in the CPU binding section of the Platform Initialization
51 Pre-EFI Initialization Core Interface Specification.
52 The function set the pointer of PEI services immediately preceding the IDT table
53 according to PI specification.
54
55 If PeiServicesTablePointer is NULL, then ASSERT().
56
57 @param PeiServicesTablePointer The address of PeiServices pointer.
58 **/
59 VOID
60 EFIAPI
61 SetPeiServicesTablePointer (
62 IN CONST EFI_PEI_SERVICES **PeiServicesTablePointer
63 )
64 {
65 IA32_DESCRIPTOR Idtr;
66
67 ASSERT (PeiServicesTablePointer != NULL);
68 AsmReadIdtr (&Idtr);
69 (*(UINTN *)(Idtr.Base - sizeof (UINTN))) = (UINTN)PeiServicesTablePointer;
70 }
71
72 /**
73 Perform CPU specific actions required to migrate the PEI Services Table
74 pointer from temporary RAM to permanent RAM.
75
76 For IA32 CPUs, the PEI Services Table pointer is stored in the 4 bytes
77 immediately preceding the Interrupt Descriptor Table (IDT) in memory.
78 For X64 CPUs, the PEI Services Table pointer is stored in the 8 bytes
79 immediately preceding the Interrupt Descriptor Table (IDT) in memory.
80 For Itanium and ARM CPUs, a the PEI Services Table Pointer is stored in
81 a dedicated CPU register. This means that there is no memory storage
82 associated with storing the PEI Services Table pointer, so no additional
83 migration actions are required for Itanium or ARM CPUs.
84
85 If The cached PEI Services Table pointer is NULL, then ASSERT().
86 If the permanent memory is allocated failed, then ASSERT().
87 **/
88 VOID
89 EFIAPI
90 MigratePeiServicesTablePointer (
91 VOID
92 )
93 {
94 EFI_STATUS Status;
95 IA32_DESCRIPTOR Idtr;
96 EFI_PHYSICAL_ADDRESS IdtBase;
97 CONST EFI_PEI_SERVICES **PeiServices;
98
99 //
100 // Get PEI Services Table pointer
101 //
102 AsmReadIdtr (&Idtr);
103 PeiServices = (CONST EFI_PEI_SERVICES **)(*(UINTN *)(Idtr.Base - sizeof (UINTN)));
104 ASSERT (PeiServices != NULL);
105 //
106 // Allocate the permanent memory.
107 //
108 Status = (*PeiServices)->AllocatePages (
109 PeiServices,
110 EfiBootServicesCode,
111 EFI_SIZE_TO_PAGES (Idtr.Limit + 1 + sizeof (UINTN)),
112 &IdtBase
113 );
114 ASSERT_EFI_ERROR (Status);
115 //
116 // Idt table needs to be migrated into memory.
117 //
118 CopyMem ((VOID *)(UINTN)IdtBase, (VOID *)(Idtr.Base - sizeof (UINTN)), Idtr.Limit + 1 + sizeof (UINTN));
119 Idtr.Base = (UINTN)IdtBase + sizeof (UINTN);
120 AsmWriteIdtr (&Idtr);
121
122 return;
123 }