]> git.proxmox.com Git - mirror_edk2.git/blame - Vlv2TbltDevicePkg/FspSupport/Library/SecFspPlatformSecLibVlv2/SecTempRamSupport.c
ArmPkg/CompilerIntrinsicsLib: Add uread, uwrite GCC assembly sources
[mirror_edk2.git] / Vlv2TbltDevicePkg / FspSupport / Library / SecFspPlatformSecLibVlv2 / SecTempRamSupport.c
CommitLineData
3cbfba02
DW
1/** @file\r
2 C functions in SEC\r
3\r
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
9dc8036d 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
3cbfba02
DW
6\r
7**/\r
8\r
9#include <PiPei.h>\r
10\r
11#include <Ppi/TemporaryRamSupport.h>\r
12\r
13#include <Library/BaseMemoryLib.h>\r
14#include <Library/DebugLib.h>\r
15#include <Library/PcdLib.h>\r
16#include <Library/DebugAgentLib.h>\r
17\r
18/**\r
19 Switch the stack in the temporary memory to the one in the permanent memory.\r
20\r
21 This function must be invoked after the memory migration immediately. The relative\r
22 position of the stack in the temporary and permanent memory is same.\r
23\r
24 @param TemporaryMemoryBase Base address of the temporary memory.\r
25 @param PermenentMemoryBase Base address of the permanent memory.\r
26**/\r
27VOID\r
28EFIAPI\r
29SecSwitchStack (\r
30 UINT32 TemporaryMemoryBase,\r
31 UINT32 PermenentMemoryBase\r
32 );\r
33\r
34/**\r
35 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into\r
36 permanent memory.\r
37\r
38 @param PeiServices Pointer to the PEI Services Table.\r
39 @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the\r
40 Temporary RAM contents.\r
41 @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the\r
42 Temporary RAM contents.\r
43 @param CopySize Amount of memory to migrate from temporary to permanent memory.\r
44\r
45 @retval EFI_SUCCESS The data was successfully returned.\r
46 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when\r
47 TemporaryMemoryBase > PermanentMemoryBase.\r
48\r
49**/\r
50EFI_STATUS\r
51EFIAPI\r
52SecTemporaryRamSupport (\r
53 IN CONST EFI_PEI_SERVICES **PeiServices,\r
54 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r
55 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r
56 IN UINTN CopySize\r
57 )\r
58{\r
59 IA32_DESCRIPTOR IdtDescriptor;\r
60 VOID* OldHeap;\r
61 VOID* NewHeap;\r
62 VOID* OldStack;\r
63 VOID* NewStack;\r
64 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext;\r
65 BOOLEAN OldStatus;\r
66 UINTN PeiStackSize;\r
67\r
68 PeiStackSize = (UINTN)PcdGet32 (PcdPeiTemporaryRamStackSize);\r
69 if (PeiStackSize == 0) {\r
70 PeiStackSize = (CopySize >> 1);\r
71 }\r
72\r
73 ASSERT (PeiStackSize < CopySize);\r
74\r
75 //\r
76 // |-------------------|---->\r
77 // | Stack | PeiStackSize\r
78 // |-------------------|---->\r
79 // | Heap | PeiTemporayRamSize\r
80 // |-------------------|----> TempRamBase\r
81 //\r
82 // |-------------------|---->\r
83 // | Heap | PeiTemporayRamSize\r
84 // |-------------------|---->\r
85 // | Stack | PeiStackSize\r
86 // |-------------------|----> PermanentMemoryBase\r
87 //\r
88\r
89 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;\r
90 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + PeiStackSize);\r
91\r
92 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize - PeiStackSize);\r
93 NewStack = (VOID*)(UINTN)PermanentMemoryBase;\r
94\r
95 DebugAgentContext.HeapMigrateOffset = (UINTN)NewHeap - (UINTN)OldHeap;\r
96 DebugAgentContext.StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;\r
97\r
98 OldStatus = SaveAndSetDebugTimerInterrupt (FALSE);\r
99\r
100 //\r
101 // Initialize Debug Agent to support source level debug in PEI phase after memory ready.\r
102 // It will build HOB and fix up the pointer in IDT table.\r
103 //\r
104 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);\r
105\r
106 //\r
107 // Migrate Heap\r
108 //\r
109 CopyMem (NewHeap, OldHeap, CopySize - PeiStackSize);\r
110\r
111 //\r
112 // Migrate Stack\r
113 //\r
114 CopyMem (NewStack, OldStack, PeiStackSize);\r
115\r
116\r
117 //\r
118 // We need *not* fix the return address because currently,\r
119 // The PeiCore is executed in flash.\r
120 //\r
121\r
122 //\r
123 // Rebase IDT table in permanent memory\r
124 //\r
125 AsmReadIdtr (&IdtDescriptor);\r
126 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;\r
127\r
128 AsmWriteIdtr (&IdtDescriptor);\r
129\r
130\r
131 //\r
132 // Program MTRR\r
133 //\r
134\r
135 //\r
136 // SecSwitchStack function must be invoked after the memory migration\r
2e182e30
GL
137 // immediately, also we need fixup the stack change caused by new call into\r
138 // permanent memory.\r
3cbfba02
DW
139 //\r
140 SecSwitchStack (\r
141 (UINT32) (UINTN) OldStack,\r
142 (UINT32) (UINTN) NewStack\r
143 );\r
144\r
145 SaveAndSetDebugTimerInterrupt (OldStatus);\r
146\r
147 return EFI_SUCCESS;\r
148}\r
149\r