1 /** @file
2 Sample to provide SecTemporaryRamSupport function.
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiPei.h>
16
17 #include <Ppi/TemporaryRamSupport.h>
18
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/DebugAgentLib.h>
23
24 /**
25 Switch the stack in the temporary memory to the one in the permanent memory.
26
27 This function must be invoked after the memory migration immediately. The relative
28 position of the stack in the temporary and permanent memory is same.
29
30 @param[in] TemporaryMemoryBase Base address of the temporary memory.
31 @param[in] PermenentMemoryBase Base address of the permanent memory.
32 **/
33 VOID
34 EFIAPI
35 SecSwitchStack (
36 IN UINT32 TemporaryMemoryBase,
37 IN UINT32 PermenentMemoryBase
38 );
39
40 /**
41 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
42 permanent memory.
43
44 @param[in] PeiServices Pointer to the PEI Services Table.
45 @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
46 Temporary RAM contents.
47 @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
48 Temporary RAM contents.
49 @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.
50
51 @retval EFI_SUCCESS The data was successfully returned.
52 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
53 TemporaryMemoryBase > PermanentMemoryBase.
54
55 **/
56 EFI_STATUS
57 EFIAPI
SecTemporaryRamSupport(IN CONST EFI_PEI_SERVICES ** PeiServices,IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,IN UINTN CopySize)58 SecTemporaryRamSupport (
59 IN CONST EFI_PEI_SERVICES **PeiServices,
60 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
61 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
62 IN UINTN CopySize
63 )
64 {
65 IA32_DESCRIPTOR IdtDescriptor;
66 VOID* OldHeap;
67 VOID* NewHeap;
68 VOID* OldStack;
69 VOID* NewStack;
70 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext;
71 BOOLEAN OldStatus;
72 UINTN PeiStackSize;
73
74 PeiStackSize = (UINTN)PcdGet32 (PcdPeiTemporaryRamStackSize);
75 if (PeiStackSize == 0) {
76 PeiStackSize = (CopySize >> 1);
77 }
78
79 ASSERT (PeiStackSize < CopySize);
80
81 //
82 // |-------------------|---->
83 // | Stack | PeiStackSize
84 // |-------------------|---->
85 // | Heap | PeiTemporayRamSize
86 // |-------------------|----> TempRamBase
87 //
88 // |-------------------|---->
89 // | Heap | PeiTemporayRamSize
90 // |-------------------|---->
91 // | Stack | PeiStackSize
92 // |-------------------|----> PermanentMemoryBase
93 //
94
95 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;
96 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + PeiStackSize);
97
98 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize - PeiStackSize);
99 NewStack = (VOID*)(UINTN)PermanentMemoryBase;
100
101 DebugAgentContext.HeapMigrateOffset = (UINTN)NewHeap - (UINTN)OldHeap;
102 DebugAgentContext.StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;
103
104 OldStatus = SaveAndSetDebugTimerInterrupt (FALSE);
105 //
106 // Initialize Debug Agent to support source level debug in PEI phase after memory ready.
107 // It will build HOB and fix up the pointer in IDT table.
108 //
109 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);
110
111 //
112 // Migrate Heap
113 //
114 CopyMem (NewHeap, OldHeap, CopySize - PeiStackSize);
115
116 //
117 // Migrate Stack
118 //
119 CopyMem (NewStack, OldStack, PeiStackSize);
120
121
122 //
123 // We need *not* fix the return address because currently,
124 // The PeiCore is executed in flash.
125 //
126
127 //
128 // Rebase IDT table in permanent memory
129 //
130 AsmReadIdtr (&IdtDescriptor);
131 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
132
133 AsmWriteIdtr (&IdtDescriptor);
134
135
136 //
137 // Program MTRR
138 //
139
140 //
141 // SecSwitchStack function must be invoked after the memory migration
142 // immediatly, also we need fixup the stack change caused by new call into
143 // permenent memory.
144 //
145 SecSwitchStack (
146 (UINT32) (UINTN) OldStack,
147 (UINT32) (UINTN) NewStack
148 );
149
150 SaveAndSetDebugTimerInterrupt (OldStatus);
151
152 return EFI_SUCCESS;
153 }
154
155