]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/FspSupport/Library/SecFspPlatformSecLibVlv2/SecTempRamSupport.c
Upload BSD-licensed Vlv2TbltDevicePkg and Vlv2DeviceRefCodePkg to
[mirror_edk2.git] / Vlv2TbltDevicePkg / FspSupport / Library / SecFspPlatformSecLibVlv2 / SecTempRamSupport.c
1 /** @file
2 C functions in SEC
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made
6 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 TemporaryMemoryBase Base address of the temporary memory.
31 @param PermenentMemoryBase Base address of the permanent memory.
32 **/
33 VOID
34 EFIAPI
35 SecSwitchStack (
36 UINT32 TemporaryMemoryBase,
37 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 PeiServices Pointer to the PEI Services Table.
45 @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
46 Temporary RAM contents.
47 @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
48 Temporary RAM contents.
49 @param 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
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 //
107 // Initialize Debug Agent to support source level debug in PEI phase after memory ready.
108 // It will build HOB and fix up the pointer in IDT table.
109 //
110 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);
111
112 //
113 // Migrate Heap
114 //
115 CopyMem (NewHeap, OldHeap, CopySize - PeiStackSize);
116
117 //
118 // Migrate Stack
119 //
120 CopyMem (NewStack, OldStack, PeiStackSize);
121
122
123 //
124 // We need *not* fix the return address because currently,
125 // The PeiCore is executed in flash.
126 //
127
128 //
129 // Rebase IDT table in permanent memory
130 //
131 AsmReadIdtr (&IdtDescriptor);
132 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
133
134 AsmWriteIdtr (&IdtDescriptor);
135
136
137 //
138 // Program MTRR
139 //
140
141 //
142 // SecSwitchStack function must be invoked after the memory migration
143 // immediatly, also we need fixup the stack change caused by new call into
144 // permenent memory.
145 //
146 SecSwitchStack (
147 (UINT32) (UINTN) OldStack,
148 (UINT32) (UINTN) NewStack
149 );
150
151 SaveAndSetDebugTimerInterrupt (OldStatus);
152
153 return EFI_SUCCESS;
154 }
155