]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationSmm.c
UefiCpuPkg: Apply uncrustify changes
[mirror_edk2.git] / UefiCpuPkg / PiSmmCommunication / PiSmmCommunicationSmm.c
CommitLineData
b3dc26ed
MK
1/** @file\r
2PiSmmCommunication SMM Driver.\r
3\r
8b1d1493 4Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
b3dc26ed
MK
6\r
7**/\r
8\r
9#include <PiSmm.h>\r
10#include <Library/UefiDriverEntryPoint.h>\r
11#include <Library/UefiBootServicesTableLib.h>\r
12#include <Library/UefiRuntimeServicesTableLib.h>\r
13#include <Library/SmmServicesTableLib.h>\r
14#include <Library/BaseLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
b3dc26ed
MK
16#include <Library/DebugLib.h>\r
17#include <Library/SmmMemLib.h>\r
b3dc26ed 18#include <Protocol/SmmSwDispatch2.h>\r
b3dc26ed 19#include <Protocol/SmmCommunication.h>\r
b3dc26ed 20#include <Ppi/SmmCommunication.h>\r
b3dc26ed
MK
21\r
22#include "PiSmmCommunicationPrivate.h"\r
23\r
24EFI_SMM_COMMUNICATION_CONTEXT mSmmCommunicationContext = {\r
25 SMM_COMMUNICATION_SIGNATURE\r
26};\r
27\r
b3dc26ed
MK
28/**\r
29 Set SMM communication context.\r
30**/\r
31VOID\r
32SetCommunicationContext (\r
33 VOID\r
34 )\r
35{\r
36 EFI_STATUS Status;\r
37\r
38 Status = gSmst->SmmInstallConfigurationTable (\r
39 gSmst,\r
40 &gEfiPeiSmmCommunicationPpiGuid,\r
41 &mSmmCommunicationContext,\r
053e878b 42 sizeof (mSmmCommunicationContext)\r
b3dc26ed
MK
43 );\r
44 ASSERT_EFI_ERROR (Status);\r
45}\r
46\r
47/**\r
48 Dispatch function for a Software SMI handler.\r
49\r
50 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
51 @param Context Points to an optional handler context which was specified when the\r
52 handler was registered.\r
53 @param CommBuffer A pointer to a collection of data in memory that will\r
54 be conveyed from a non-SMM environment into an SMM environment.\r
55 @param CommBufferSize The size of the CommBuffer.\r
56\r
57 @retval EFI_SUCCESS Command is handled successfully.\r
58\r
59**/\r
60EFI_STATUS\r
61EFIAPI\r
62PiSmmCommunicationHandler (\r
63 IN EFI_HANDLE DispatchHandle,\r
64 IN CONST VOID *Context OPTIONAL,\r
65 IN OUT VOID *CommBuffer OPTIONAL,\r
66 IN OUT UINTN *CommBufferSize OPTIONAL\r
67 )\r
68{\r
053e878b
MK
69 UINTN CommSize;\r
70 EFI_STATUS Status;\r
71 EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r
72 EFI_PHYSICAL_ADDRESS *BufferPtrAddress;\r
b3dc26ed 73\r
96e1cba5 74 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler Enter\n"));\r
b3dc26ed 75\r
053e878b 76 BufferPtrAddress = (EFI_PHYSICAL_ADDRESS *)(UINTN)mSmmCommunicationContext.BufferPtrAddress;\r
b3dc26ed 77 CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)(UINTN)*BufferPtrAddress;\r
96e1cba5 78 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler CommunicateHeader - %x\n", CommunicateHeader));\r
b3dc26ed 79 if (CommunicateHeader == NULL) {\r
96e1cba5 80 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler is NULL, needn't to call dispatch function\n"));\r
b3dc26ed
MK
81 Status = EFI_SUCCESS;\r
82 } else {\r
83 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicateHeader, OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data))) {\r
96e1cba5 84 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler CommunicateHeader invalid - 0x%x\n", CommunicateHeader));\r
b3dc26ed
MK
85 Status = EFI_SUCCESS;\r
86 goto Done;\r
87 }\r
88\r
89 CommSize = (UINTN)CommunicateHeader->MessageLength;\r
90 if (!SmmIsBufferOutsideSmmValid ((UINTN)&CommunicateHeader->Data[0], CommSize)) {\r
96e1cba5 91 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler CommunicateData invalid - 0x%x\n", &CommunicateHeader->Data[0]));\r
b3dc26ed
MK
92 Status = EFI_SUCCESS;\r
93 goto Done;\r
94 }\r
95\r
96 //\r
97 // Call dispatch function\r
98 //\r
96e1cba5 99 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler Data - %x\n", &CommunicateHeader->Data[0]));\r
b3dc26ed
MK
100 Status = gSmst->SmiManage (\r
101 &CommunicateHeader->HeaderGuid,\r
102 NULL,\r
103 &CommunicateHeader->Data[0],\r
104 &CommSize\r
105 );\r
106 }\r
107\r
108Done:\r
96e1cba5
MK
109 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler %r\n", Status));\r
110 DEBUG ((DEBUG_INFO, "PiSmmCommunicationHandler Exit\n"));\r
b3dc26ed
MK
111\r
112 return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_INTERRUPT_PENDING;\r
113}\r
114\r
115/**\r
116 Allocate EfiACPIMemoryNVS below 4G memory address.\r
117\r
118 This function allocates EfiACPIMemoryNVS below 4G memory address.\r
119\r
120 @param Size Size of memory to allocate.\r
121\r
122 @return Allocated address for output.\r
123\r
124**/\r
053e878b 125VOID *\r
b3dc26ed 126AllocateAcpiNvsMemoryBelow4G (\r
053e878b 127 IN UINTN Size\r
b3dc26ed
MK
128 )\r
129{\r
130 UINTN Pages;\r
131 EFI_PHYSICAL_ADDRESS Address;\r
132 EFI_STATUS Status;\r
053e878b 133 VOID *Buffer;\r
b3dc26ed 134\r
053e878b 135 Pages = EFI_SIZE_TO_PAGES (Size);\r
b3dc26ed
MK
136 Address = 0xffffffff;\r
137\r
053e878b
MK
138 Status = gBS->AllocatePages (\r
139 AllocateMaxAddress,\r
140 EfiACPIMemoryNVS,\r
141 Pages,\r
142 &Address\r
143 );\r
b3dc26ed
MK
144 ASSERT_EFI_ERROR (Status);\r
145\r
053e878b 146 Buffer = (VOID *)(UINTN)Address;\r
b3dc26ed
MK
147 ZeroMem (Buffer, Size);\r
148\r
149 return Buffer;\r
150}\r
151\r
152/**\r
153 Entry Point for PI SMM communication SMM driver.\r
154\r
155 @param[in] ImageHandle Image handle of this driver.\r
156 @param[in] SystemTable A Pointer to the EFI System Table.\r
157\r
ef62da4f 158 @retval EFI_SUCCESS\r
b3dc26ed
MK
159 @return Others Some error occurs.\r
160**/\r
161EFI_STATUS\r
162EFIAPI\r
163PiSmmCommunicationSmmEntryPoint (\r
164 IN EFI_HANDLE ImageHandle,\r
165 IN EFI_SYSTEM_TABLE *SystemTable\r
166 )\r
167{\r
053e878b
MK
168 EFI_STATUS Status;\r
169 EFI_SMM_SW_DISPATCH2_PROTOCOL *SmmSwDispatch2;\r
170 EFI_SMM_SW_REGISTER_CONTEXT SmmSwDispatchContext;\r
171 EFI_HANDLE DispatchHandle;\r
172 EFI_PHYSICAL_ADDRESS *BufferPtrAddress;\r
b3dc26ed 173\r
b3dc26ed
MK
174 //\r
175 // Register software SMI handler\r
176 //\r
177 Status = gSmst->SmmLocateProtocol (\r
178 &gEfiSmmSwDispatch2ProtocolGuid,\r
179 NULL,\r
180 (VOID **)&SmmSwDispatch2\r
181 );\r
182 ASSERT_EFI_ERROR (Status);\r
183\r
184 SmmSwDispatchContext.SwSmiInputValue = (UINTN)-1;\r
053e878b
MK
185 Status = SmmSwDispatch2->Register (\r
186 SmmSwDispatch2,\r
187 PiSmmCommunicationHandler,\r
188 &SmmSwDispatchContext,\r
189 &DispatchHandle\r
190 );\r
b3dc26ed
MK
191 ASSERT_EFI_ERROR (Status);\r
192\r
96e1cba5 193 DEBUG ((DEBUG_INFO, "SmmCommunication SwSmi: %x\n", (UINTN)SmmSwDispatchContext.SwSmiInputValue));\r
b3dc26ed 194\r
053e878b 195 BufferPtrAddress = AllocateAcpiNvsMemoryBelow4G (sizeof (EFI_PHYSICAL_ADDRESS));\r
b3dc26ed 196 ASSERT (BufferPtrAddress != NULL);\r
96e1cba5 197 DEBUG ((DEBUG_INFO, "SmmCommunication BufferPtrAddress: 0x%016lx, BufferPtr: 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)BufferPtrAddress, *BufferPtrAddress));\r
b3dc26ed
MK
198\r
199 //\r
200 // Save context\r
201 //\r
053e878b 202 mSmmCommunicationContext.SwSmiNumber = (UINT32)SmmSwDispatchContext.SwSmiInputValue;\r
8b1d1493 203 mSmmCommunicationContext.BufferPtrAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)BufferPtrAddress;\r
b3dc26ed
MK
204 SetCommunicationContext ();\r
205\r
206 return Status;\r
207}\r