]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/SmmBaseOnSmmBase2Thunk/SmmBaseOnSmmBase2Thunk.c
Remove the implementation limitation in the SmmBaseHelper driver that it assumes...
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / SmmBaseOnSmmBase2Thunk / SmmBaseOnSmmBase2Thunk.c
CommitLineData
9e620719 1/** @file\r
2 SMM Base Protocol on SMM Base2 Protocol Thunk driver.\r
3\r
4 This driver co-operates with SMM Base Helper SMM driver to provide SMM Base Protocol\r
5 based on SMM Base2 Protocol.\r
6\r
7 This thunk driver is expected to be loaded before PI SMM IPL driver so that\r
8 SMM BASE Protocol can be published immediately after SMM Base2 Protocol is installed to\r
9 make SMM Base Protocol.InSmm() as early as possible.\r
10\r
fb03ca1a 11 Copyright (c) 2009 - 2010, Intel Corporation\r
9e620719 12 All rights reserved. This program and the accompanying materials\r
13 are licensed and made available under the terms and conditions of the BSD License\r
14 which accompanies this distribution. The full text of the license may be found at\r
15 http://opensource.org/licenses/bsd-license.php\r
16\r
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
19\r
20**/\r
21\r
f5501a65 22#include <PiDxe.h>\r
23#include <FrameworkSmm.h>\r
9e620719 24\r
f5501a65 25#include <Protocol/SmmBase2.h>\r
26#include <Protocol/SmmCommunication.h>\r
27#include <Protocol/SmmBaseHelperReady.h>\r
28\r
29#include <Guid/SmmBaseThunkCommunication.h>\r
30#include <Guid/EventGroup.h>\r
31\r
32#include <Library/DebugLib.h>\r
33#include <Library/UefiBootServicesTableLib.h>\r
34#include <Library/UefiDriverEntryPoint.h>\r
35#include <Library/UefiLib.h>\r
36#include <Library/UefiRuntimeLib.h>\r
37\r
f5501a65 38SMMBASETHUNK_COMMUNICATION_DATA mCommunicationData = {\r
9e620719 39 EFI_SMM_BASE_THUNK_COMMUNICATION_GUID,\r
40 sizeof (SMMBASE_FUNCTION_DATA)\r
41};\r
42\r
f5501a65 43EFI_HANDLE mSmmBaseHandle = NULL;\r
9e620719 44EFI_SMM_BASE2_PROTOCOL *mSmmBase2 = NULL;\r
45EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
46EFI_SMM_BASE_HELPER_READY_PROTOCOL *mSmmBaseHelperReady = NULL;\r
47\r
17d2c9a3 48/**\r
49 Determine if in SMM mode.\r
50\r
51 @retval TRUE In SMM mode.\r
52 @retval FALSE Not in SMM mode.\r
53**/\r
9e620719 54BOOLEAN\r
55IsInSmm (\r
56 VOID\r
57 )\r
58{\r
59 EFI_STATUS Status;\r
60 BOOLEAN InSmm;\r
61 \r
62 Status = mSmmBase2->InSmm (mSmmBase2, &InSmm);\r
63 ASSERT_EFI_ERROR (Status);\r
64 return InSmm;\r
65}\r
66\r
67/**\r
68 Invoke services provided by SMM Base Helper SMM driver.\r
69**/\r
70VOID\r
71SmmBaseHelperService (\r
72 VOID\r
73 )\r
74{\r
75 UINTN DataSize;\r
76\r
f5501a65 77 mCommunicationData.FunctionData.Status = EFI_UNSUPPORTED;\r
673c1498 78 mCommunicationData.FunctionData.SmmBaseImageHandle = mSmmBaseHandle;\r
9e620719 79\r
26a76fbc 80 if ((mCommunicationData.FunctionData.Function != SmmBaseFunctionCommunicate) && IsInSmm()) {\r
9e620719 81 ///\r
82 /// If in SMM mode, directly call services in SMM Base Helper.\r
83 ///\r
9e620719 84 DataSize = (UINTN)(sizeof (SMMBASE_FUNCTION_DATA));\r
85 mSmmBaseHelperReady->ServiceEntry (\r
86 NULL,\r
87 NULL,\r
f5501a65 88 &mCommunicationData.FunctionData,\r
9e620719 89 &DataSize\r
90 );\r
91 } else {\r
92 ///\r
bade9bf5 93 /// Call services in SMM Base Helper via SMM Communication Protocol.\r
9e620719 94 ///\r
f5501a65 95 DataSize = (UINTN)(sizeof (mCommunicationData));\r
9e620719 96 mSmmCommunication->Communicate (\r
97 mSmmCommunication,\r
f5501a65 98 &mCommunicationData,\r
9e620719 99 &DataSize\r
100 );\r
101 }\r
102}\r
103\r
104/**\r
105 Register a given driver into SMRAM. This is the equivalent of performing\r
106 the LoadImage/StartImage into System Management Mode.\r
107\r
108 @param[in] This Protocol instance pointer.\r
109 @param[in] FilePath Location of the image to be installed as the handler.\r
110 @param[in] SourceBuffer Optional source buffer in case the image file\r
111 is in memory.\r
112 @param[in] SourceSize Size of the source image file, if in memory.\r
113 @param[out] ImageHandle The handle that the base driver uses to decode \r
114 the handler. Unique among SMM handlers only, \r
115 not unique across DXE/EFI.\r
116 @param[in] LegacyIA32Binary An optional parameter specifying that the associated \r
117 file is a real-mode IA-32 binary.\r
118\r
119 @retval EFI_SUCCESS The operation was successful.\r
120 @retval EFI_OUT_OF_RESOURCES There were no additional SMRAM resources to load the handler\r
121 @retval EFI_UNSUPPORTED This platform does not support 16-bit handlers.\r
122 @retval EFI_UNSUPPORTED Platform is in runtime.\r
123 @retval EFI_INVALID_PARAMETER The handlers was not the correct image type\r
124**/\r
125EFI_STATUS\r
126EFIAPI\r
127SmmBaseRegister (\r
128 IN EFI_SMM_BASE_PROTOCOL *This,\r
129 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
130 IN VOID *SourceBuffer,\r
131 IN UINTN SourceSize,\r
132 OUT EFI_HANDLE *ImageHandle,\r
133 IN BOOLEAN LegacyIA32Binary\r
134 )\r
135{\r
136 if (LegacyIA32Binary) {\r
137 return EFI_UNSUPPORTED;\r
138 }\r
139\r
26a76fbc 140 mCommunicationData.FunctionData.Function = SmmBaseFunctionRegister;\r
f5501a65 141 mCommunicationData.FunctionData.Args.Register.FilePath = FilePath;\r
142 mCommunicationData.FunctionData.Args.Register.SourceBuffer = SourceBuffer;\r
143 mCommunicationData.FunctionData.Args.Register.SourceSize = SourceSize;\r
144 mCommunicationData.FunctionData.Args.Register.ImageHandle = ImageHandle;\r
145 mCommunicationData.FunctionData.Args.Register.LegacyIA32Binary = LegacyIA32Binary;\r
9e620719 146\r
147 SmmBaseHelperService ();\r
f5501a65 148 return mCommunicationData.FunctionData.Status;\r
9e620719 149}\r
150\r
151/**\r
152 Removes a handler from execution within SMRAM. This is the equivalent of performing\r
153 the UnloadImage in System Management Mode.\r
154\r
155 @param[in] This Protocol instance pointer.\r
156 @param[in] ImageHandle The handler to be removed.\r
157\r
158 @retval EFI_SUCCESS The operation was successful\r
159 @retval EFI_INVALID_PARAMETER The handler did not exist\r
160 @retval EFI_UNSUPPORTED Platform is in runtime.\r
161**/\r
162EFI_STATUS\r
163EFIAPI\r
164SmmBaseUnregister (\r
165 IN EFI_SMM_BASE_PROTOCOL *This,\r
166 IN EFI_HANDLE ImageHandle\r
167 )\r
168{\r
26a76fbc 169 mCommunicationData.FunctionData.Function = SmmBaseFunctionUnregister;\r
f5501a65 170 mCommunicationData.FunctionData.Args.UnRegister.ImageHandle = ImageHandle;\r
9e620719 171\r
172 SmmBaseHelperService ();\r
f5501a65 173 return mCommunicationData.FunctionData.Status;\r
9e620719 174}\r
175\r
176/**\r
177 The SMM Inter-module Communicate Service Communicate() function\r
178 provides a service to send/receive messages from a registered\r
179 EFI service. The BASE protocol driver is responsible for doing\r
180 any of the copies such that the data lives in boot-service-accessible RAM.\r
181\r
182 @param[in] This Protocol instance pointer.\r
183 @param[in] ImageHandle The handle of the registered driver.\r
26a76fbc
LG
184 @param[in, out] CommunicationBuffer Pointer to the buffer to convey into SMRAM.\r
185 @param[in, out] BufferSize The size of the data buffer being passed in.\r
9e620719 186 On exit, the size of data being returned.\r
187 Zero if the handler does not wish to reply with any data.\r
188\r
189 @retval EFI_SUCCESS The message was successfully posted\r
190 @retval EFI_INVALID_PARAMETER The buffer was NULL\r
191**/\r
192EFI_STATUS\r
193EFIAPI\r
194SmmBaseCommunicate (\r
195 IN EFI_SMM_BASE_PROTOCOL *This,\r
196 IN EFI_HANDLE ImageHandle,\r
197 IN OUT VOID *CommunicationBuffer,\r
198 IN OUT UINTN *BufferSize\r
199 )\r
200{\r
bade9bf5 201 ///\r
202 /// Note this is a runtime interface\r
203 ///\r
204\r
26a76fbc 205 mCommunicationData.FunctionData.Function = SmmBaseFunctionCommunicate;\r
bade9bf5 206 mCommunicationData.FunctionData.Args.Communicate.ImageHandle = ImageHandle;\r
207 mCommunicationData.FunctionData.Args.Communicate.CommunicationBuffer = CommunicationBuffer;\r
208 mCommunicationData.FunctionData.Args.Communicate.SourceSize = BufferSize;\r
9e620719 209\r
bade9bf5 210 SmmBaseHelperService ();\r
211 return mCommunicationData.FunctionData.Status;\r
9e620719 212}\r
213\r
214/**\r
215 Register a callback to execute within SMM.\r
216 This allows receipt of messages created with EFI_SMM_BASE_PROTOCOL.Communicate().\r
217\r
218 @param[in] This Protocol instance pointer.\r
219 @param[in] SmmImageHandle Handle of the callback service.\r
220 @param[in] CallbackAddress Address of the callback service.\r
221 @param[in] MakeLast If present, will stipulate that the handler is posted to \r
222 be executed last in the dispatch table.\r
223 @param[in] FloatingPointSave An optional parameter that informs the\r
224 EFI_SMM_ACCESS_PROTOCOL Driver core if it needs to save\r
225 the floating point register state. If any handler\r
226 require this, the state will be saved for all handlers.\r
227\r
228 @retval EFI_SUCCESS The operation was successful\r
229 @retval EFI_OUT_OF_RESOURCES Not enough space in the dispatch queue\r
230 @retval EFI_UNSUPPORTED Platform is in runtime.\r
231 @retval EFI_UNSUPPORTED The caller is not in SMM.\r
232**/\r
233EFI_STATUS\r
234EFIAPI\r
235SmmBaseRegisterCallback (\r
236 IN EFI_SMM_BASE_PROTOCOL *This,\r
237 IN EFI_HANDLE SmmImageHandle,\r
238 IN EFI_SMM_CALLBACK_ENTRY_POINT CallbackAddress,\r
239 IN BOOLEAN MakeLast,\r
240 IN BOOLEAN FloatingPointSave\r
241 )\r
242{\r
243 if (!IsInSmm()) {\r
244 return EFI_UNSUPPORTED;\r
245 }\r
246\r
26a76fbc 247 mCommunicationData.FunctionData.Function = SmmBaseFunctionRegisterCallback;\r
f5501a65 248 mCommunicationData.FunctionData.Args.RegisterCallback.SmmImageHandle = SmmImageHandle;\r
249 mCommunicationData.FunctionData.Args.RegisterCallback.CallbackAddress = CallbackAddress;\r
250 mCommunicationData.FunctionData.Args.RegisterCallback.MakeLast = MakeLast;\r
251 mCommunicationData.FunctionData.Args.RegisterCallback.FloatingPointSave = FloatingPointSave;\r
9e620719 252\r
253 SmmBaseHelperService();\r
f5501a65 254 return mCommunicationData.FunctionData.Status;\r
9e620719 255}\r
256\r
257/**\r
258 This routine tells caller if execution context is SMM or not.\r
259\r
260 @param[in] This Protocol instance pointer.\r
261 @param[out] InSmm Whether the caller is inside SMM for IA-32\r
262 or servicing a PMI for the Itanium processor\r
263 family.\r
264\r
265 @retval EFI_SUCCESS The operation was successful\r
266 @retval EFI_INVALID_PARAMETER InSmm was NULL.\r
267**/\r
268EFI_STATUS\r
269EFIAPI\r
270SmmBaseInSmm (\r
271 IN EFI_SMM_BASE_PROTOCOL *This,\r
272 OUT BOOLEAN *InSmm\r
273 )\r
274{\r
275 return mSmmBase2->InSmm (mSmmBase2, InSmm);\r
276}\r
277\r
278/**\r
279 The SmmAllocatePool() function allocates a memory region of Size bytes from memory of\r
280 type PoolType and returns the address of the allocated memory in the location referenced\r
281 by Buffer. This function allocates pages from EFI SMRAM Memory as needed to grow the\r
282 requested pool type. All allocations are eight-byte aligned.\r
283\r
284 @param[in] This Protocol instance pointer.\r
285 @param[in] PoolType The type of pool to allocate.\r
286 The only supported type is EfiRuntimeServicesData;\r
287 the interface will internally map this runtime request to \r
288 SMRAM for IA-32 and leave as this type for the Itanium \r
289 processor family. Other types can be ignored.\r
290 @param[in] Size The number of bytes to allocate from the pool.\r
291 @param[out] Buffer A pointer to a pointer to the allocated buffer if the call\r
292 succeeds; undefined otherwise.\r
293\r
294 @retval EFI_SUCCESS The requested number of bytes was allocated.\r
295 @retval EFI_OUT_OF_RESOURCES The pool requested could not be allocated.\r
296 @retval EFI_UNSUPPORTED Platform is in runtime.\r
297**/\r
298EFI_STATUS\r
299EFIAPI\r
300SmmBaseSmmAllocatePool (\r
301 IN EFI_SMM_BASE_PROTOCOL *This,\r
302 IN EFI_MEMORY_TYPE PoolType,\r
303 IN UINTN Size,\r
304 OUT VOID **Buffer\r
305 )\r
306{\r
26a76fbc 307 mCommunicationData.FunctionData.Function = SmmBaseFunctionAllocatePool;\r
f5501a65 308 mCommunicationData.FunctionData.Args.AllocatePool.PoolType = PoolType;\r
309 mCommunicationData.FunctionData.Args.AllocatePool.Size = Size;\r
310 mCommunicationData.FunctionData.Args.AllocatePool.Buffer = Buffer;\r
9e620719 311\r
312 SmmBaseHelperService ();\r
f5501a65 313 return mCommunicationData.FunctionData.Status;\r
9e620719 314}\r
315\r
316/**\r
317 The SmmFreePool() function returns the memory specified by Buffer to the system.\r
318 On return, the memory's type is EFI SMRAM Memory. The Buffer that is freed must\r
319 have been allocated by SmmAllocatePool().\r
320\r
321 @param[in] This Protocol instance pointer.\r
322 @param[in] Buffer Pointer to the buffer allocation.\r
323\r
324 @retval EFI_SUCCESS The memory was returned to the system.\r
325 @retval EFI_INVALID_PARAMETER Buffer was invalid.\r
326 @retval EFI_UNSUPPORTED Platform is in runtime.\r
327**/\r
328EFI_STATUS\r
329EFIAPI\r
330SmmBaseSmmFreePool (\r
331 IN EFI_SMM_BASE_PROTOCOL *This,\r
332 IN VOID *Buffer\r
333 )\r
334{\r
26a76fbc 335 mCommunicationData.FunctionData.Function = SmmBaseFunctionFreePool;\r
f5501a65 336 mCommunicationData.FunctionData.Args.FreePool.Buffer = Buffer;\r
9e620719 337\r
338 SmmBaseHelperService ();\r
f5501a65 339 return mCommunicationData.FunctionData.Status;\r
9e620719 340}\r
341\r
342/**\r
343 The GetSmstLocation() function returns the location of the System Management\r
344 Service Table. The use of the API is such that a driver can discover the\r
345 location of the SMST in its entry point and then cache it in some driver\r
346 global variable so that the SMST can be invoked in subsequent callbacks.\r
347\r
348 @param[in] This Protocol instance pointer.\r
26a76fbc 349 @param[out] Smst Pointer to the SMST.\r
9e620719 350\r
351 @retval EFI_SUCCESS The operation was successful\r
352 @retval EFI_INVALID_PARAMETER Smst was invalid.\r
353 @retval EFI_UNSUPPORTED Not in SMM.\r
354**/\r
355EFI_STATUS\r
356EFIAPI\r
357SmmBaseGetSmstLocation (\r
358 IN EFI_SMM_BASE_PROTOCOL *This,\r
359 OUT EFI_SMM_SYSTEM_TABLE **Smst\r
360 )\r
361{\r
9e620719 362 if (!IsInSmm ()) {\r
363 return EFI_UNSUPPORTED;\r
364 }\r
365\r
366 if (Smst == NULL) {\r
367 return EFI_INVALID_PARAMETER;\r
368 }\r
369\r
370 *Smst = mSmmBaseHelperReady->FrameworkSmst;\r
371 return EFI_SUCCESS;\r
372}\r
373\r
fb03ca1a 374/**\r
375 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE\r
376\r
377 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
378 It convers pointer to new virtual address.\r
379\r
380 @param Event Event whose notification function is being invoked\r
381 @param Context Pointer to the notification function's context\r
382**/\r
383VOID\r
384EFIAPI\r
385SmmBaseAddressChangeEvent (\r
386 IN EFI_EVENT Event,\r
387 IN VOID *Context\r
388 )\r
389{\r
bade9bf5 390 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
fb03ca1a 391}\r
392\r
26a76fbc
LG
393///\r
394/// SMM Base Protocol instance\r
395///\r
396EFI_SMM_BASE_PROTOCOL mSmmBase = {\r
397 SmmBaseRegister,\r
398 SmmBaseUnregister,\r
399 SmmBaseCommunicate,\r
400 SmmBaseRegisterCallback,\r
401 SmmBaseInSmm,\r
402 SmmBaseSmmAllocatePool,\r
403 SmmBaseSmmFreePool,\r
404 SmmBaseGetSmstLocation\r
405};\r
406\r
9e620719 407/**\r
408 Entry Point for SMM Base Protocol on SMM Base2 Protocol Thunk driver.\r
409\r
410 @param[in] ImageHandle Image handle of this driver.\r
411 @param[in] SystemTable A Pointer to the EFI System Table.\r
412\r
413 @retval EFI_SUCCESS The entry point is executed successfully.\r
414**/\r
415EFI_STATUS\r
416EFIAPI\r
417SmmBaseThunkMain (\r
418 IN EFI_HANDLE ImageHandle,\r
419 IN EFI_SYSTEM_TABLE *SystemTable\r
420 )\r
421{\r
f5501a65 422 EFI_STATUS Status;\r
423 EFI_EVENT Event;\r
9e620719 424\r
673c1498 425 mSmmBaseHandle = ImageHandle;\r
426\r
f5501a65 427 //\r
428 // Assume only one instance of SMM Base2 Protocol in the system\r
429 // Locate SMM Base2 Protocol\r
430 //\r
431 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **) &mSmmBase2);\r
432 ASSERT_EFI_ERROR (Status);\r
433\r
434 //\r
435 // Assume only one instance of SMM Communication Protocol in the system\r
436 // Locate SMM Communication Protocol\r
437 //\r
438 gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
439 ASSERT_EFI_ERROR (Status);\r
440\r
441 //\r
442 // Assume only one instance of SMM Base Helper Ready Protocol in the system\r
443 // Locate SMM Base Helper Ready Protocol\r
444 //\r
445 Status = gBS->LocateProtocol (&gEfiSmmBaseHelperReadyProtocolGuid, NULL, (VOID **) &mSmmBaseHelperReady);\r
446 ASSERT_EFI_ERROR (Status);\r
9e620719 447\r
f5501a65 448 //\r
449 // Create event on SetVirtualAddressMap() to convert mSmmCommunication from a physical address to a virtual address\r
450 //\r
fb03ca1a 451 Status = gBS->CreateEventEx (\r
452 EVT_NOTIFY_SIGNAL,\r
453 TPL_NOTIFY,\r
454 SmmBaseAddressChangeEvent,\r
455 NULL,\r
456 &gEfiEventVirtualAddressChangeGuid,\r
f5501a65 457 &Event\r
fb03ca1a 458 );\r
459 ASSERT_EFI_ERROR (Status);\r
f5501a65 460 \r
461 //\r
462 // Publish Framework SMM BASE Protocol immediately after SMM Base2 Protocol is installed to\r
463 // make SMM Base Protocol.InSmm() available as early as possible.\r
464 //\r
465 Status = gBS->InstallMultipleProtocolInterfaces (\r
466 &mSmmBaseHandle,\r
467 &gEfiSmmBaseProtocolGuid, &mSmmBase,\r
468 NULL\r
469 );\r
470 ASSERT_EFI_ERROR (Status);\r
471 \r
9e620719 472 return EFI_SUCCESS;\r
473}\r