]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointer.c
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / MdePkg / Library / PeiServicesTablePointerLibIdt / PeiServicesTablePointer.c
CommitLineData
bf231ea6 1/** @file\r
030cd1a2 2 PEI Services Table Pointer Library for IA-32 and x64.\r
bf231ea6 3\r
ce02b0b9 4 According to PI specification, the peiservice pointer is stored prior at IDT\r
5 table in IA32 and x64 architecture.\r
bf231ea6 6\r
9095d37b
LG
7 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php.\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
bf231ea6
A
15\r
16**/\r
c7d265a9 17\r
5dd39dc6 18#include <PiPei.h>\r
19\r
20#include <Library/BaseLib.h>\r
21#include <Library/PeiServicesTablePointerLib.h>\r
22#include <Library/DebugLib.h>\r
fcfd5fb0 23#include <Library/BaseMemoryLib.h>\r
c7d265a9 24\r
25/**\r
4b8157f9 26 Retrieves the cached value of the PEI Services Table pointer.\r
f38fdc74 27\r
9095d37b
LG
28 Returns the cached value of the PEI Services Table pointer in a CPU specific manner\r
29 as specified in the CPU binding section of the Platform Initialization Pre-EFI\r
4b8157f9 30 Initialization Core Interface Specification.\r
9095d37b 31\r
4b8157f9 32 If the cached PEI Services Table pointer is NULL, then ASSERT().\r
f38fdc74 33\r
4b8157f9 34 @return The pointer to PeiServices.\r
c7d265a9 35\r
36**/\r
5240b97c 37CONST EFI_PEI_SERVICES **\r
c7d265a9 38EFIAPI\r
39GetPeiServicesTablePointer (\r
40 VOID\r
41 )\r
42{\r
5240b97c 43 CONST EFI_PEI_SERVICES **PeiServices;\r
81c7803c 44 IA32_DESCRIPTOR Idtr;\r
9095d37b 45\r
81c7803c 46 AsmReadIdtr (&Idtr);\r
5240b97c 47 PeiServices = (CONST EFI_PEI_SERVICES **) (*(UINTN*)(Idtr.Base - sizeof (UINTN)));\r
c7d265a9 48 ASSERT (PeiServices != NULL);\r
49 return PeiServices;\r
50}\r
51\r
81c7803c 52/**\r
9095d37b
LG
53 Caches a pointer PEI Services Table.\r
54\r
55 Caches the pointer to the PEI Services Table specified by PeiServicesTablePointer\r
56 in a CPU specific manner as specified in the CPU binding section of the Platform Initialization\r
57 Pre-EFI Initialization Core Interface Specification.\r
f38fdc74 58 The function set the pointer of PEI services immediately preceding the IDT table\r
59 according to PI specification.\r
9095d37b 60\r
4b8157f9 61 If PeiServicesTablePointer is NULL, then ASSERT().\r
9095d37b 62\r
f38fdc74 63 @param PeiServicesTablePointer The address of PeiServices pointer.\r
81c7803c 64**/\r
65VOID\r
66EFIAPI\r
67SetPeiServicesTablePointer (\r
a86fda43 68 IN CONST EFI_PEI_SERVICES ** PeiServicesTablePointer\r
81c7803c 69 )\r
70{\r
fcfd5fb0 71 IA32_DESCRIPTOR Idtr;\r
9095d37b 72\r
4b8157f9 73 ASSERT (PeiServicesTablePointer != NULL);\r
81c7803c 74 AsmReadIdtr (&Idtr);\r
ffdb421c
LG
75 (*(UINTN*)(Idtr.Base - sizeof (UINTN))) = (UINTN)PeiServicesTablePointer;\r
76}\r
77\r
78/**\r
9095d37b 79 Perform CPU specific actions required to migrate the PEI Services Table\r
ffdb421c
LG
80 pointer from temporary RAM to permanent RAM.\r
81\r
9095d37b 82 For IA32 CPUs, the PEI Services Table pointer is stored in the 4 bytes\r
ffdb421c 83 immediately preceding the Interrupt Descriptor Table (IDT) in memory.\r
9095d37b 84 For X64 CPUs, the PEI Services Table pointer is stored in the 8 bytes\r
ffdb421c
LG
85 immediately preceding the Interrupt Descriptor Table (IDT) in memory.\r
86 For Itanium and ARM CPUs, a the PEI Services Table Pointer is stored in\r
9095d37b
LG
87 a dedicated CPU register. This means that there is no memory storage\r
88 associated with storing the PEI Services Table pointer, so no additional\r
ffdb421c
LG
89 migration actions are required for Itanium or ARM CPUs.\r
90\r
91 If The cached PEI Services Table pointer is NULL, then ASSERT().\r
92 If the permanent memory is allocated failed, then ASSERT().\r
93**/\r
94VOID\r
95EFIAPI\r
96MigratePeiServicesTablePointer (\r
8a835340 97 VOID\r
ffdb421c
LG
98 )\r
99{\r
100 EFI_STATUS Status;\r
101 IA32_DESCRIPTOR Idtr;\r
102 EFI_PHYSICAL_ADDRESS IdtBase;\r
103 CONST EFI_PEI_SERVICES **PeiServices;\r
104\r
105 //\r
106 // Get PEI Services Table pointer\r
107 //\r
108 AsmReadIdtr (&Idtr);\r
109 PeiServices = (CONST EFI_PEI_SERVICES **) (*(UINTN*)(Idtr.Base - sizeof (UINTN)));\r
110 ASSERT (PeiServices != NULL);\r
111 //\r
112 // Allocate the permanent memory.\r
113 //\r
114 Status = (*PeiServices)->AllocatePages (\r
9095d37b 115 PeiServices,\r
ffdb421c
LG
116 EfiBootServicesCode,\r
117 EFI_SIZE_TO_PAGES(Idtr.Limit + 1 + sizeof (UINTN)),\r
118 &IdtBase\r
119 );\r
120 ASSERT_EFI_ERROR (Status);\r
121 //\r
122 // Idt table needs to be migrated into memory.\r
123 //\r
124 CopyMem ((VOID *) (UINTN) IdtBase, (VOID *) (Idtr.Base - sizeof (UINTN)), Idtr.Limit + 1 + sizeof (UINTN));\r
125 Idtr.Base = (UINTN) IdtBase + sizeof (UINTN);\r
126 AsmWriteIdtr (&Idtr);\r
9095d37b 127\r
ffdb421c 128 return;\r
81c7803c 129}\r
130\r
81c7803c 131\r