]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Compatibility/SmmBaseOnSmmBase2Thunk/SmmBaseOnSmmBase2Thunk.c
Per Framework SMM CIS spec, SMM Base Protocol.Communicate() is availabe in runtime...
[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
22#include "SmmBaseOnSmmBase2Thunk.h"\r
23\r
24EFI_SMM_BASE_PROTOCOL gSmmBase = {\r
25 SmmBaseRegister,\r
26 SmmBaseUnregister,\r
27 SmmBaseCommunicate,\r
28 SmmBaseRegisterCallback,\r
29 SmmBaseInSmm,\r
30 SmmBaseSmmAllocatePool,\r
31 SmmBaseSmmFreePool,\r
32 SmmBaseGetSmstLocation\r
33};\r
34\r
35SMMBASETHUNK_COMMUNICATION_DATA gCommunicationData = {\r
36 EFI_SMM_BASE_THUNK_COMMUNICATION_GUID,\r
37 sizeof (SMMBASE_FUNCTION_DATA)\r
38};\r
39\r
40EFI_HANDLE mImageHandle;\r
41EFI_SMM_BASE2_PROTOCOL *mSmmBase2 = NULL;\r
42EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;\r
43EFI_SMM_BASE_HELPER_READY_PROTOCOL *mSmmBaseHelperReady = NULL;\r
44\r
17d2c9a3 45/**\r
46 Determine if in SMM mode.\r
47\r
48 @retval TRUE In SMM mode.\r
49 @retval FALSE Not in SMM mode.\r
50**/\r
9e620719 51BOOLEAN\r
52IsInSmm (\r
53 VOID\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57 BOOLEAN InSmm;\r
58 \r
59 Status = mSmmBase2->InSmm (mSmmBase2, &InSmm);\r
60 ASSERT_EFI_ERROR (Status);\r
61 return InSmm;\r
62}\r
63\r
64/**\r
65 Invoke services provided by SMM Base Helper SMM driver.\r
66**/\r
67VOID\r
68SmmBaseHelperService (\r
69 VOID\r
70 )\r
71{\r
72 UINTN DataSize;\r
73\r
74 gCommunicationData.FunctionData.Status = EFI_UNSUPPORTED;\r
75\r
76 if (IsInSmm()) {\r
77 ///\r
78 /// If in SMM mode, directly call services in SMM Base Helper.\r
79 ///\r
80 if (mSmmBaseHelperReady == NULL) {\r
81 ASSERT (FALSE);\r
82 return;\r
83 }\r
84\r
85 DataSize = (UINTN)(sizeof (SMMBASE_FUNCTION_DATA));\r
86 mSmmBaseHelperReady->ServiceEntry (\r
87 NULL,\r
88 NULL,\r
89 &gCommunicationData.FunctionData,\r
90 &DataSize\r
91 );\r
92 } else {\r
93 ///\r
94 /// If in non-SMM mode, call services in SMM Base Helper via SMM Communication Protocol.\r
95 ///\r
96 if (mSmmCommunication == NULL) {\r
97 ASSERT (FALSE);\r
98 return;\r
99 }\r
100\r
101 DataSize = (UINTN)(sizeof (gCommunicationData));\r
102 mSmmCommunication->Communicate (\r
103 mSmmCommunication,\r
104 &gCommunicationData,\r
105 &DataSize\r
106 );\r
107 }\r
108}\r
109\r
110/**\r
111 Register a given driver into SMRAM. This is the equivalent of performing\r
112 the LoadImage/StartImage into System Management Mode.\r
113\r
114 @param[in] This Protocol instance pointer.\r
115 @param[in] FilePath Location of the image to be installed as the handler.\r
116 @param[in] SourceBuffer Optional source buffer in case the image file\r
117 is in memory.\r
118 @param[in] SourceSize Size of the source image file, if in memory.\r
119 @param[out] ImageHandle The handle that the base driver uses to decode \r
120 the handler. Unique among SMM handlers only, \r
121 not unique across DXE/EFI.\r
122 @param[in] LegacyIA32Binary An optional parameter specifying that the associated \r
123 file is a real-mode IA-32 binary.\r
124\r
125 @retval EFI_SUCCESS The operation was successful.\r
126 @retval EFI_OUT_OF_RESOURCES There were no additional SMRAM resources to load the handler\r
127 @retval EFI_UNSUPPORTED This platform does not support 16-bit handlers.\r
128 @retval EFI_UNSUPPORTED Platform is in runtime.\r
129 @retval EFI_INVALID_PARAMETER The handlers was not the correct image type\r
130**/\r
131EFI_STATUS\r
132EFIAPI\r
133SmmBaseRegister (\r
134 IN EFI_SMM_BASE_PROTOCOL *This,\r
135 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
136 IN VOID *SourceBuffer,\r
137 IN UINTN SourceSize,\r
138 OUT EFI_HANDLE *ImageHandle,\r
139 IN BOOLEAN LegacyIA32Binary\r
140 )\r
141{\r
142 if (LegacyIA32Binary) {\r
143 return EFI_UNSUPPORTED;\r
144 }\r
145\r
146 gCommunicationData.FunctionData.Function = SMMBASE_REGISTER;\r
147 gCommunicationData.FunctionData.Args.Register.FilePath = FilePath;\r
148 gCommunicationData.FunctionData.Args.Register.SourceBuffer = SourceBuffer;\r
149 gCommunicationData.FunctionData.Args.Register.SourceSize = SourceSize;\r
150 gCommunicationData.FunctionData.Args.Register.ImageHandle = ImageHandle;\r
151 gCommunicationData.FunctionData.Args.Register.LegacyIA32Binary = LegacyIA32Binary;\r
152\r
153 SmmBaseHelperService ();\r
154 return gCommunicationData.FunctionData.Status;\r
155}\r
156\r
157/**\r
158 Removes a handler from execution within SMRAM. This is the equivalent of performing\r
159 the UnloadImage in System Management Mode.\r
160\r
161 @param[in] This Protocol instance pointer.\r
162 @param[in] ImageHandle The handler to be removed.\r
163\r
164 @retval EFI_SUCCESS The operation was successful\r
165 @retval EFI_INVALID_PARAMETER The handler did not exist\r
166 @retval EFI_UNSUPPORTED Platform is in runtime.\r
167**/\r
168EFI_STATUS\r
169EFIAPI\r
170SmmBaseUnregister (\r
171 IN EFI_SMM_BASE_PROTOCOL *This,\r
172 IN EFI_HANDLE ImageHandle\r
173 )\r
174{\r
175 gCommunicationData.FunctionData.Function = SMMBASE_UNREGISTER;\r
176 gCommunicationData.FunctionData.Args.UnRegister.ImageHandle = ImageHandle;\r
177\r
178 SmmBaseHelperService ();\r
179 return gCommunicationData.FunctionData.Status;\r
180}\r
181\r
182/**\r
183 The SMM Inter-module Communicate Service Communicate() function\r
184 provides a service to send/receive messages from a registered\r
185 EFI service. The BASE protocol driver is responsible for doing\r
186 any of the copies such that the data lives in boot-service-accessible RAM.\r
187\r
188 @param[in] This Protocol instance pointer.\r
189 @param[in] ImageHandle The handle of the registered driver.\r
190 @param[in,out] CommunicationBuffer Pointer to the buffer to convey into SMRAM.\r
191 @param[in,out] BufferSize The size of the data buffer being passed in.\r
192 On exit, the size of data being returned.\r
193 Zero if the handler does not wish to reply with any data.\r
194\r
195 @retval EFI_SUCCESS The message was successfully posted\r
196 @retval EFI_INVALID_PARAMETER The buffer was NULL\r
197**/\r
198EFI_STATUS\r
199EFIAPI\r
200SmmBaseCommunicate (\r
201 IN EFI_SMM_BASE_PROTOCOL *This,\r
202 IN EFI_HANDLE ImageHandle,\r
203 IN OUT VOID *CommunicationBuffer,\r
204 IN OUT UINTN *BufferSize\r
205 )\r
206{\r
207 if (mSmmCommunication == NULL) {\r
208 ASSERT (FALSE);\r
209 return EFI_UNSUPPORTED;\r
210 }\r
211\r
212 return mSmmCommunication->Communicate (\r
213 mSmmCommunication,\r
214 CommunicationBuffer,\r
215 BufferSize\r
216 );\r
217}\r
218\r
219/**\r
220 Register a callback to execute within SMM.\r
221 This allows receipt of messages created with EFI_SMM_BASE_PROTOCOL.Communicate().\r
222\r
223 @param[in] This Protocol instance pointer.\r
224 @param[in] SmmImageHandle Handle of the callback service.\r
225 @param[in] CallbackAddress Address of the callback service.\r
226 @param[in] MakeLast If present, will stipulate that the handler is posted to \r
227 be executed last in the dispatch table.\r
228 @param[in] FloatingPointSave An optional parameter that informs the\r
229 EFI_SMM_ACCESS_PROTOCOL Driver core if it needs to save\r
230 the floating point register state. If any handler\r
231 require this, the state will be saved for all handlers.\r
232\r
233 @retval EFI_SUCCESS The operation was successful\r
234 @retval EFI_OUT_OF_RESOURCES Not enough space in the dispatch queue\r
235 @retval EFI_UNSUPPORTED Platform is in runtime.\r
236 @retval EFI_UNSUPPORTED The caller is not in SMM.\r
237**/\r
238EFI_STATUS\r
239EFIAPI\r
240SmmBaseRegisterCallback (\r
241 IN EFI_SMM_BASE_PROTOCOL *This,\r
242 IN EFI_HANDLE SmmImageHandle,\r
243 IN EFI_SMM_CALLBACK_ENTRY_POINT CallbackAddress,\r
244 IN BOOLEAN MakeLast,\r
245 IN BOOLEAN FloatingPointSave\r
246 )\r
247{\r
248 if (!IsInSmm()) {\r
249 return EFI_UNSUPPORTED;\r
250 }\r
251\r
252 gCommunicationData.FunctionData.Function = SMMBASE_REGISTER_CALLBACK;\r
253 gCommunicationData.FunctionData.Args.RegisterCallback.SmmImageHandle = SmmImageHandle;\r
254 gCommunicationData.FunctionData.Args.RegisterCallback.CallbackAddress = CallbackAddress;\r
255 gCommunicationData.FunctionData.Args.RegisterCallback.MakeLast = MakeLast;\r
256 gCommunicationData.FunctionData.Args.RegisterCallback.FloatingPointSave = FloatingPointSave;\r
257\r
258 SmmBaseHelperService();\r
259 return gCommunicationData.FunctionData.Status;\r
260}\r
261\r
262/**\r
263 This routine tells caller if execution context is SMM or not.\r
264\r
265 @param[in] This Protocol instance pointer.\r
266 @param[out] InSmm Whether the caller is inside SMM for IA-32\r
267 or servicing a PMI for the Itanium processor\r
268 family.\r
269\r
270 @retval EFI_SUCCESS The operation was successful\r
271 @retval EFI_INVALID_PARAMETER InSmm was NULL.\r
272**/\r
273EFI_STATUS\r
274EFIAPI\r
275SmmBaseInSmm (\r
276 IN EFI_SMM_BASE_PROTOCOL *This,\r
277 OUT BOOLEAN *InSmm\r
278 )\r
279{\r
280 return mSmmBase2->InSmm (mSmmBase2, InSmm);\r
281}\r
282\r
283/**\r
284 The SmmAllocatePool() function allocates a memory region of Size bytes from memory of\r
285 type PoolType and returns the address of the allocated memory in the location referenced\r
286 by Buffer. This function allocates pages from EFI SMRAM Memory as needed to grow the\r
287 requested pool type. All allocations are eight-byte aligned.\r
288\r
289 @param[in] This Protocol instance pointer.\r
290 @param[in] PoolType The type of pool to allocate.\r
291 The only supported type is EfiRuntimeServicesData;\r
292 the interface will internally map this runtime request to \r
293 SMRAM for IA-32 and leave as this type for the Itanium \r
294 processor family. Other types can be ignored.\r
295 @param[in] Size The number of bytes to allocate from the pool.\r
296 @param[out] Buffer A pointer to a pointer to the allocated buffer if the call\r
297 succeeds; undefined otherwise.\r
298\r
299 @retval EFI_SUCCESS The requested number of bytes was allocated.\r
300 @retval EFI_OUT_OF_RESOURCES The pool requested could not be allocated.\r
301 @retval EFI_UNSUPPORTED Platform is in runtime.\r
302**/\r
303EFI_STATUS\r
304EFIAPI\r
305SmmBaseSmmAllocatePool (\r
306 IN EFI_SMM_BASE_PROTOCOL *This,\r
307 IN EFI_MEMORY_TYPE PoolType,\r
308 IN UINTN Size,\r
309 OUT VOID **Buffer\r
310 )\r
311{\r
312 gCommunicationData.FunctionData.Function = SMMBASE_ALLOCATE_POOL;\r
313 gCommunicationData.FunctionData.Args.AllocatePool.PoolType = PoolType;\r
314 gCommunicationData.FunctionData.Args.AllocatePool.Size = Size;\r
315 gCommunicationData.FunctionData.Args.AllocatePool.Buffer = Buffer;\r
316\r
317 SmmBaseHelperService ();\r
318 return gCommunicationData.FunctionData.Status;\r
319}\r
320\r
321/**\r
322 The SmmFreePool() function returns the memory specified by Buffer to the system.\r
323 On return, the memory's type is EFI SMRAM Memory. The Buffer that is freed must\r
324 have been allocated by SmmAllocatePool().\r
325\r
326 @param[in] This Protocol instance pointer.\r
327 @param[in] Buffer Pointer to the buffer allocation.\r
328\r
329 @retval EFI_SUCCESS The memory was returned to the system.\r
330 @retval EFI_INVALID_PARAMETER Buffer was invalid.\r
331 @retval EFI_UNSUPPORTED Platform is in runtime.\r
332**/\r
333EFI_STATUS\r
334EFIAPI\r
335SmmBaseSmmFreePool (\r
336 IN EFI_SMM_BASE_PROTOCOL *This,\r
337 IN VOID *Buffer\r
338 )\r
339{\r
340 gCommunicationData.FunctionData.Function = SMMBASE_FREE_POOL;\r
341 gCommunicationData.FunctionData.Args.FreePool.Buffer = Buffer;\r
342\r
343 SmmBaseHelperService ();\r
344 return gCommunicationData.FunctionData.Status;\r
345}\r
346\r
347/**\r
348 The GetSmstLocation() function returns the location of the System Management\r
349 Service Table. The use of the API is such that a driver can discover the\r
350 location of the SMST in its entry point and then cache it in some driver\r
351 global variable so that the SMST can be invoked in subsequent callbacks.\r
352\r
353 @param[in] This Protocol instance pointer.\r
354 @param[in] Smst Pointer to the SMST.\r
355\r
356 @retval EFI_SUCCESS The operation was successful\r
357 @retval EFI_INVALID_PARAMETER Smst was invalid.\r
358 @retval EFI_UNSUPPORTED Not in SMM.\r
359**/\r
360EFI_STATUS\r
361EFIAPI\r
362SmmBaseGetSmstLocation (\r
363 IN EFI_SMM_BASE_PROTOCOL *This,\r
364 OUT EFI_SMM_SYSTEM_TABLE **Smst\r
365 )\r
366{\r
367 if (mSmmBaseHelperReady == NULL) {\r
368 ASSERT (FALSE);\r
369 return EFI_UNSUPPORTED;\r
370 }\r
371\r
372 if (!IsInSmm ()) {\r
373 return EFI_UNSUPPORTED;\r
374 }\r
375\r
376 if (Smst == NULL) {\r
377 return EFI_INVALID_PARAMETER;\r
378 }\r
379\r
380 *Smst = mSmmBaseHelperReady->FrameworkSmst;\r
381 return EFI_SUCCESS;\r
382}\r
383\r
384/**\r
385 SMM Base Protocol notification event handler.\r
386\r
387 @param[in] Event Event whose notification function is being invoked.\r
388 @param[in] Context Pointer to the notification function's context.\r
389**/\r
390VOID\r
391EFIAPI\r
392SmmBaseProtocolNotification (\r
393 IN EFI_EVENT Event,\r
394 IN VOID *Context\r
395 )\r
396{\r
397 EFI_STATUS Status;\r
398\r
399 ///\r
400 /// Assume only one instance of SMM Base2 Protocol in the system\r
401 /// Locate SMM Base2 Protocol\r
402 ///\r
403 Status = gBS->LocateProtocol (&gEfiSmmBase2ProtocolGuid, NULL, (VOID **) &mSmmBase2);\r
404 if (!EFI_ERROR (Status)) {\r
405 ///\r
406 /// Publish Framework SMM BASE Protocol immediately after SMM Base2 Protocol is installed to\r
407 /// make SMM Base Protocol.InSmm() available as early as possible.\r
408 ///\r
409 Status = gBS->InstallProtocolInterface (\r
410 &mImageHandle,\r
411 &gEfiSmmBaseProtocolGuid,\r
412 EFI_NATIVE_INTERFACE,\r
413 &gSmmBase\r
414 );\r
415 ASSERT_EFI_ERROR (Status);\r
416 }\r
417}\r
418\r
419/**\r
420 SMM Communication Protocol notification event handler.\r
421\r
422 @param[in] Event Event whose notification function is being invoked.\r
423 @param[in] Context Pointer to the notification function's context.\r
424**/\r
425VOID\r
426EFIAPI\r
427SmmCommunicationProtocolNotification (\r
428 IN EFI_EVENT Event,\r
429 IN VOID *Context\r
430 )\r
431{\r
432 ///\r
433 /// Assume only one instance of SMM Communication Protocol in the system\r
434 /// Locate SMM Communication Protocol\r
435 ///\r
436 gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
437}\r
438\r
439/**\r
440 SMM Base Helper Ready Protocol notification event handler.\r
441\r
442 @param[in] Event Event whose notification function is being invoked.\r
443 @param[in] Context Pointer to the notification function's context.\r
444**/\r
445VOID\r
446EFIAPI\r
447SmmBaseHelperReadyProtocolNotification (\r
448 IN EFI_EVENT Event,\r
449 IN VOID *Context\r
450 )\r
451{\r
452 ///\r
453 /// Assume only one instance of SMM Base Helper Ready Protocol in the system\r
454 /// Locate SMM Base Helper Ready Protocol\r
455 ///\r
456 gBS->LocateProtocol (&gEfiSmmBaseHelperReadyProtocolGuid, NULL, (VOID **) &mSmmBaseHelperReady);\r
457}\r
458\r
fb03ca1a 459/**\r
460 Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE\r
461\r
462 This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.\r
463 It convers pointer to new virtual address.\r
464\r
465 @param Event Event whose notification function is being invoked\r
466 @param Context Pointer to the notification function's context\r
467**/\r
468VOID\r
469EFIAPI\r
470SmmBaseAddressChangeEvent (\r
471 IN EFI_EVENT Event,\r
472 IN VOID *Context\r
473 )\r
474{\r
475 if (mSmmCommunication != NULL) {\r
476 EfiConvertPointer (0x0, (VOID **) &mSmmCommunication);\r
477 }\r
478}\r
479\r
9e620719 480/**\r
481 Entry Point for SMM Base Protocol on SMM Base2 Protocol Thunk driver.\r
482\r
483 @param[in] ImageHandle Image handle of this driver.\r
484 @param[in] SystemTable A Pointer to the EFI System Table.\r
485\r
486 @retval EFI_SUCCESS The entry point is executed successfully.\r
487**/\r
488EFI_STATUS\r
489EFIAPI\r
490SmmBaseThunkMain (\r
491 IN EFI_HANDLE ImageHandle,\r
492 IN EFI_SYSTEM_TABLE *SystemTable\r
493 )\r
494{\r
fb03ca1a 495 VOID *Registration;\r
496 EFI_EVENT VirtualAddressChangeEvent;\r
497 EFI_STATUS Status;\r
9e620719 498\r
499 mImageHandle = ImageHandle;\r
500\r
501 ///\r
502 /// Install notifications for required protocols\r
503 ///\r
504 /// Note we use protocol notifications here so as that this thunk driver can be\r
505 /// loaded before PI SMM IPL driver. Framework SMM BASE Protocol will be published \r
506 /// immediately after SMM Base2 Protocol is installed to make SMM Base Protocol.InSmm()\r
507 /// available as early as possible because some Framework code's behavior depends on\r
508 /// correct detection of SMM mode via SMM Base Protocol.InSmm().\r
509 ///\r
510 /// Also SMM Base Helper driver is expected to be dispatched\r
511 /// in the earliest round of SMM driver dispatch just after SMM IPL driver loads SMM Foundation.\r
512 /// So the full functionality of SMM Base Protocol is ready immediately after SMM IPL driver is\r
513 /// loaded. Since that point Framework SMM driver can be succesufully supported.\r
514 ///\r
515 EfiCreateProtocolNotifyEvent (\r
516 &gEfiSmmBase2ProtocolGuid,\r
517 TPL_CALLBACK,\r
518 SmmBaseProtocolNotification,\r
519 NULL,\r
520 &Registration\r
521 );\r
522\r
523 EfiCreateProtocolNotifyEvent (\r
524 &gEfiSmmCommunicationProtocolGuid,\r
525 TPL_CALLBACK,\r
526 SmmCommunicationProtocolNotification,\r
527 NULL,\r
528 &Registration\r
529 );\r
530\r
531 EfiCreateProtocolNotifyEvent (\r
532 &gEfiSmmBaseHelperReadyProtocolGuid,\r
533 TPL_CALLBACK,\r
534 SmmBaseHelperReadyProtocolNotification,\r
535 NULL,\r
536 &Registration\r
537 );\r
538\r
fb03ca1a 539 Status = gBS->CreateEventEx (\r
540 EVT_NOTIFY_SIGNAL,\r
541 TPL_NOTIFY,\r
542 SmmBaseAddressChangeEvent,\r
543 NULL,\r
544 &gEfiEventVirtualAddressChangeGuid,\r
545 &VirtualAddressChangeEvent\r
546 );\r
547 ASSERT_EFI_ERROR (Status);\r
548\r
9e620719 549 return EFI_SUCCESS;\r
550}\r
551\r