updated to use the term “temporary memory” but not CAR
[edk2.git] / EdkCompatibilityPkg / Foundation / Library / Pei / PeiLib / Ia32 / PeiServicePointer.c
blob45250dba151349ad4588b3c24116ce1674a9eb09
1 /*++
3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 Module Name:
14 PeiServicePointer.c
16 Abstract:
18 --*/
20 #include "Tiano.h"
21 #include "PeiApi.h"
22 #include "PeiLib.h"
24 #if (PI_SPECIFICATION_VERSION >= 0x00010000)
26 #ifdef EFI_NT_EMULATOR
27 EFI_PEI_SERVICES **gPeiServices;
28 #endif
31 VOID
32 SetPeiServicesTablePointer (
33 IN EFI_PEI_SERVICES **PeiServices
35 /*++
37 Routine Description:
39 Save PeiService pointer so that it can be retrieved anywhere.
41 Arguments:
43 PeiServices - The direct pointer to PeiServiceTable.
44 PhyscialAddress - The physcial address of variable PeiServices.
46 Returns:
47 NONE
49 --*/
52 #ifdef EFI_NT_EMULATOR
55 // For NT32, set EFI_PEI_SERVICES** to global variable.
57 gPeiServices = PeiServices;
58 #else
61 // For X86 processor,the EFI_PEI_SERVICES** is stored in the
62 // 4 bytes immediately preceding the Interrupt Descriptor Table.
64 UINTN IdtBaseAddress;
65 IdtBaseAddress = (UINTN)ReadIdtBase();
66 *(UINTN*)(IdtBaseAddress - 4) = (UINTN)PeiServices;
68 #endif
72 EFI_PEI_SERVICES **
73 GetPeiServicesTablePointer (
74 VOID
76 /*++
78 Routine Description:
80 Get PeiService pointer.
82 Arguments:
84 NONE.
86 Returns:
87 The direct pointer to PeiServiceTable.
89 --*/
91 EFI_PEI_SERVICES **PeiServices;
93 #ifdef EFI_NT_EMULATOR
96 // For NT32, set EFI_PEI_SERVICES** to global variable.
98 PeiServices = gPeiServices;
99 #else
102 // For X86 processor,the EFI_PEI_SERVICES** is stored in the
103 // 4 bytes immediately preceding the Interrupt Descriptor Table.
105 UINTN IdtBaseAddress;
106 IdtBaseAddress = (UINTN)ReadIdtBase();
107 PeiServices = (EFI_PEI_SERVICES **)(UINTN)(*(UINTN*)(IdtBaseAddress - 4));
108 #endif
109 return PeiServices;
113 VOID
114 MigrateIdtTable (
115 IN EFI_PEI_SERVICES **PeiServices
117 /*++
119 Routine Description:
121 Migrate IDT from temporary memory to real memory where preceded with 4 bytes for
122 storing PeiService pointer.
124 Arguments:
126 PeiServices - The direct pointer to PeiServiceTable.
128 Returns:
130 NONE.
132 --*/
134 #ifndef EFI_NT_EMULATOR
135 UINT16 IdtEntrySize;
136 UINTN OldIdtBase;
137 UINTN Size;
138 VOID *NewIdtBase;
139 EFI_STATUS Status;
141 IdtEntrySize = ReadIdtLimit();
142 OldIdtBase = ReadIdtBase();
143 Size = sizeof(PEI_IDT_TABLE) + (IdtEntrySize + 1);
144 Status = (*PeiServices)->AllocatePool (PeiServices, Size, &NewIdtBase);
145 ASSERT_PEI_ERROR (PeiServices, Status);
146 (*PeiServices)->CopyMem ((VOID*)((UINTN)NewIdtBase + sizeof(PEI_IDT_TABLE)), (VOID*)OldIdtBase, (IdtEntrySize + 1));
147 SetIdtBase(((UINTN)NewIdtBase + sizeof(PEI_IDT_TABLE)), IdtEntrySize);
148 SetPeiServicesTablePointer(PeiServices);
149 #endif
152 #endif