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