]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/SmmBaseHelper/SmmBaseHelper.c
Update SmmBase Communicate Thunk behaivor to be compatible with Framework implemenation.
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / SmmBaseHelper / SmmBaseHelper.c
1 /** @file
2 SMM Base Helper SMM driver.
3
4 This driver is the counterpart of the SMM Base On SMM Base2 Thunk driver. It
5 provides helping services in SMM to the SMM Base On SMM Base2 Thunk driver.
6
7 Copyright (c) 2009 - 2010, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include <PiSmm.h>
19 #include <Library/DebugLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/SmmServicesTableLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/PeCoffLib.h>
25 #include <Library/DevicePathLib.h>
26 #include <Library/CacheMaintenanceLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Guid/SmmBaseThunkCommunication.h>
29 #include <Protocol/SmmBaseHelperReady.h>
30 #include <Protocol/SmmCpu.h>
31 #include <Protocol/LoadedImage.h>
32 #include <Protocol/SmmCpuSaveState.h>
33 #include <Protocol/MpService.h>
34 #include <Protocol/LoadPe32Image.h>
35
36 ///
37 /// Structure for tracking paired information of registered Framework SMI handler
38 /// and correpsonding dispatch handle for SMI handler thunk.
39 ///
40 typedef struct {
41 LIST_ENTRY Link;
42 EFI_HANDLE DispatchHandle;
43 EFI_HANDLE SmmImageHandle;
44 EFI_SMM_CALLBACK_ENTRY_POINT CallbackAddress;
45 VOID *CommunicationBuffer;
46 UINTN *SourceSize;
47 } CALLBACK_INFO;
48
49 typedef struct {
50 ///
51 /// PI SMM CPU Save State register index
52 ///
53 EFI_SMM_SAVE_STATE_REGISTER Register;
54 ///
55 /// Offset in Framework SMST
56 ///
57 UINTN Offset;
58 } CPU_SAVE_STATE_CONVERSION;
59
60 #define CPU_SAVE_STATE_GET_OFFSET(Field) (UINTN)(&(((EFI_SMM_CPU_SAVE_STATE *) 0)->Ia32SaveState.Field))
61
62
63 EFI_HANDLE mDispatchHandle;
64 EFI_SMM_CPU_PROTOCOL *mSmmCpu;
65 EFI_PE32_IMAGE_PROTOCOL *mLoadPe32Image;
66 EFI_GUID mEfiSmmCpuIoGuid = EFI_SMM_CPU_IO_GUID;
67 EFI_SMM_BASE_HELPER_READY_PROTOCOL *mSmmBaseHelperReady;
68 EFI_SMM_SYSTEM_TABLE *mFrameworkSmst;
69 UINTN mNumberOfProcessors;
70
71 LIST_ENTRY mCallbackInfoListHead = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackInfoListHead);
72
73 CPU_SAVE_STATE_CONVERSION mCpuSaveStateConvTable[] = {
74 {EFI_SMM_SAVE_STATE_REGISTER_LDTBASE , CPU_SAVE_STATE_GET_OFFSET(LDTBase)},
75 {EFI_SMM_SAVE_STATE_REGISTER_ES , CPU_SAVE_STATE_GET_OFFSET(ES)},
76 {EFI_SMM_SAVE_STATE_REGISTER_CS , CPU_SAVE_STATE_GET_OFFSET(CS)},
77 {EFI_SMM_SAVE_STATE_REGISTER_SS , CPU_SAVE_STATE_GET_OFFSET(SS)},
78 {EFI_SMM_SAVE_STATE_REGISTER_DS , CPU_SAVE_STATE_GET_OFFSET(DS)},
79 {EFI_SMM_SAVE_STATE_REGISTER_FS , CPU_SAVE_STATE_GET_OFFSET(FS)},
80 {EFI_SMM_SAVE_STATE_REGISTER_GS , CPU_SAVE_STATE_GET_OFFSET(GS)},
81 {EFI_SMM_SAVE_STATE_REGISTER_TR_SEL , CPU_SAVE_STATE_GET_OFFSET(TR)},
82 {EFI_SMM_SAVE_STATE_REGISTER_DR7 , CPU_SAVE_STATE_GET_OFFSET(DR7)},
83 {EFI_SMM_SAVE_STATE_REGISTER_DR6 , CPU_SAVE_STATE_GET_OFFSET(DR6)},
84 {EFI_SMM_SAVE_STATE_REGISTER_RAX , CPU_SAVE_STATE_GET_OFFSET(EAX)},
85 {EFI_SMM_SAVE_STATE_REGISTER_RBX , CPU_SAVE_STATE_GET_OFFSET(EBX)},
86 {EFI_SMM_SAVE_STATE_REGISTER_RCX , CPU_SAVE_STATE_GET_OFFSET(ECX)},
87 {EFI_SMM_SAVE_STATE_REGISTER_RDX , CPU_SAVE_STATE_GET_OFFSET(EDX)},
88 {EFI_SMM_SAVE_STATE_REGISTER_RSP , CPU_SAVE_STATE_GET_OFFSET(ESP)},
89 {EFI_SMM_SAVE_STATE_REGISTER_RBP , CPU_SAVE_STATE_GET_OFFSET(EBP)},
90 {EFI_SMM_SAVE_STATE_REGISTER_RSI , CPU_SAVE_STATE_GET_OFFSET(ESI)},
91 {EFI_SMM_SAVE_STATE_REGISTER_RDI , CPU_SAVE_STATE_GET_OFFSET(EDI)},
92 {EFI_SMM_SAVE_STATE_REGISTER_RIP , CPU_SAVE_STATE_GET_OFFSET(EIP)},
93 {EFI_SMM_SAVE_STATE_REGISTER_RFLAGS , CPU_SAVE_STATE_GET_OFFSET(EFLAGS)},
94 {EFI_SMM_SAVE_STATE_REGISTER_CR0 , CPU_SAVE_STATE_GET_OFFSET(CR0)},
95 {EFI_SMM_SAVE_STATE_REGISTER_CR3 , CPU_SAVE_STATE_GET_OFFSET(CR3)}
96 };
97
98 /**
99 Framework SMST SmmInstallConfigurationTable() Thunk.
100
101 This thunk calls the PI SMM SmmInstallConfigurationTable() and then update the configuration
102 table related fields in the Framework SMST because the PI SMM SmmInstallConfigurationTable()
103 function may modify these fields.
104
105 @param[in] SystemTable A pointer to the SMM System Table.
106 @param[in] Guid A pointer to the GUID for the entry to add, update, or remove.
107 @param[in] Table A pointer to the buffer of the table to add.
108 @param[in] TableSize The size of the table to install.
109
110 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
111 @retval EFI_INVALID_PARAMETER Guid is not valid.
112 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
113 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
114 **/
115 EFI_STATUS
116 EFIAPI
117 SmmInstallConfigurationTable (
118 IN EFI_SMM_SYSTEM_TABLE *SystemTable,
119 IN EFI_GUID *Guid,
120 IN VOID *Table,
121 IN UINTN TableSize
122 )
123 {
124 EFI_STATUS Status;
125
126 Status = gSmst->SmmInstallConfigurationTable (gSmst, Guid, Table, TableSize);
127 if (!EFI_ERROR (Status)) {
128 mFrameworkSmst->NumberOfTableEntries = gSmst->NumberOfTableEntries;
129 mFrameworkSmst->SmmConfigurationTable = gSmst->SmmConfigurationTable;
130 }
131 return Status;
132 }
133
134 /**
135 Construct a Framework SMST based on the PI SMM SMST.
136
137 @return Pointer to the constructed Framework SMST.
138 **/
139 EFI_SMM_SYSTEM_TABLE *
140 ConstructFrameworkSmst (
141 VOID
142 )
143 {
144 EFI_SMM_SYSTEM_TABLE *FrameworkSmst;
145
146 FrameworkSmst = (EFI_SMM_SYSTEM_TABLE *)AllocatePool (sizeof (EFI_SMM_SYSTEM_TABLE));
147 ASSERT (FrameworkSmst != NULL);
148
149 ///
150 /// Copy same things from PI SMST to Framework SMST
151 ///
152 CopyMem (FrameworkSmst, gSmst, (UINTN)(&((EFI_SMM_SYSTEM_TABLE *)0)->SmmIo));
153 CopyMem (
154 &FrameworkSmst->SmmIo,
155 &gSmst->SmmIo,
156 sizeof (EFI_SMM_SYSTEM_TABLE) - (UINTN)(&((EFI_SMM_SYSTEM_TABLE *)0)->SmmIo)
157 );
158
159 ///
160 /// Update Framework SMST
161 ///
162 FrameworkSmst->Hdr.Revision = EFI_SMM_SYSTEM_TABLE_REVISION;
163 CopyGuid (&FrameworkSmst->EfiSmmCpuIoGuid, &mEfiSmmCpuIoGuid);
164
165 FrameworkSmst->CpuSaveState = (EFI_SMM_CPU_SAVE_STATE *)AllocateZeroPool (mNumberOfProcessors * sizeof (EFI_SMM_CPU_SAVE_STATE));
166 ASSERT (FrameworkSmst->CpuSaveState != NULL);
167
168 ///
169 /// Do not support floating point state now
170 ///
171 FrameworkSmst->CpuOptionalFloatingPointState = NULL;
172
173 FrameworkSmst->SmmInstallConfigurationTable = SmmInstallConfigurationTable;
174
175 return FrameworkSmst;
176 }
177
178 /**
179 Load a given Framework SMM driver into SMRAM and invoke its entry point.
180
181 @param[in] ParentImageHandle Parent Image Handle.
182 @param[in] FilePath Location of the image to be installed as the handler.
183 @param[in] SourceBuffer Optional source buffer in case the image file
184 is in memory.
185 @param[in] SourceSize Size of the source image file, if in memory.
186 @param[out] ImageHandle The handle that the base driver uses to decode
187 the handler. Unique among SMM handlers only,
188 not unique across DXE/EFI.
189
190 @retval EFI_SUCCESS The operation was successful.
191 @retval EFI_OUT_OF_RESOURCES There were no additional SMRAM resources to load the handler
192 @retval EFI_UNSUPPORTED Can not find its copy in normal memory.
193 @retval EFI_INVALID_PARAMETER The handlers was not the correct image type
194 **/
195 EFI_STATUS
196 LoadImage (
197 IN EFI_HANDLE ParentImageHandle,
198 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
199 IN VOID *SourceBuffer,
200 IN UINTN SourceSize,
201 OUT EFI_HANDLE *ImageHandle
202 )
203 {
204 EFI_STATUS Status;
205 UINTN PageCount;
206 UINTN OrgPageCount;
207 EFI_PHYSICAL_ADDRESS DstBuffer;
208
209 if (FilePath == NULL || ImageHandle == NULL) {
210 return EFI_INVALID_PARAMETER;
211 }
212
213 PageCount = 1;
214 do {
215 OrgPageCount = PageCount;
216 DstBuffer = (UINTN)-1;
217 Status = gSmst->SmmAllocatePages (
218 AllocateMaxAddress,
219 EfiRuntimeServicesCode,
220 PageCount,
221 &DstBuffer
222 );
223 if (EFI_ERROR (Status)) {
224 return Status;
225 }
226
227 Status = mLoadPe32Image->LoadPeImage (
228 mLoadPe32Image,
229 ParentImageHandle,
230 FilePath,
231 SourceBuffer,
232 SourceSize,
233 DstBuffer,
234 &PageCount,
235 ImageHandle,
236 NULL,
237 EFI_LOAD_PE_IMAGE_ATTRIBUTE_NONE
238 );
239 if (EFI_ERROR (Status)) {
240 FreePages ((VOID *)(UINTN)DstBuffer, OrgPageCount);
241 }
242 } while (Status == EFI_BUFFER_TOO_SMALL);
243
244 if (!EFI_ERROR (Status)) {
245 ///
246 /// Update MP state in Framework SMST before transferring control to Framework SMM driver entry point
247 /// in case it may invoke AP
248 ///
249 mFrameworkSmst->CurrentlyExecutingCpu = gSmst->CurrentlyExecutingCpu;
250
251 Status = gBS->StartImage (*ImageHandle, NULL, NULL);
252 if (EFI_ERROR (Status)) {
253 mLoadPe32Image->UnLoadPeImage (mLoadPe32Image, *ImageHandle);
254 *ImageHandle = NULL;
255 FreePages ((VOID *)(UINTN)DstBuffer, PageCount);
256 }
257 }
258
259 return Status;
260 }
261
262
263 /**
264 Thunk service of EFI_SMM_BASE_PROTOCOL.Register().
265
266 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
267 **/
268 VOID
269 Register (
270 IN OUT SMMBASE_FUNCTION_DATA *FunctionData
271 )
272 {
273 EFI_STATUS Status;
274
275 if (FunctionData->Args.Register.LegacyIA32Binary) {
276 Status = EFI_UNSUPPORTED;
277 } else {
278 Status = LoadImage (
279 FunctionData->SmmBaseImageHandle,
280 FunctionData->Args.Register.FilePath,
281 FunctionData->Args.Register.SourceBuffer,
282 FunctionData->Args.Register.SourceSize,
283 FunctionData->Args.Register.ImageHandle
284 );
285 }
286 FunctionData->Status = Status;
287 }
288
289 /**
290 Thunk service of EFI_SMM_BASE_PROTOCOL.UnRegister().
291
292 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
293 **/
294 VOID
295 UnRegister (
296 IN OUT SMMBASE_FUNCTION_DATA *FunctionData
297 )
298 {
299 ///
300 /// Unregister not supported now
301 ///
302 FunctionData->Status = EFI_UNSUPPORTED;
303 }
304
305 /**
306 Search for Framework SMI handler information according to specific PI SMM dispatch handle.
307
308 @param[in] DispatchHandle The unique handle assigned by SmiHandlerRegister().
309
310 @return Pointer to CALLBACK_INFO. If NULL, no callback info record is found.
311 **/
312 CALLBACK_INFO *
313 GetCallbackInfo (
314 IN EFI_HANDLE DispatchHandle
315 )
316 {
317 LIST_ENTRY *Node;
318
319 Node = GetFirstNode (&mCallbackInfoListHead);
320 while (!IsNull (&mCallbackInfoListHead, Node)) {
321 if (((CALLBACK_INFO *)Node)->DispatchHandle == DispatchHandle) {
322 return (CALLBACK_INFO *)Node;
323 }
324 Node = GetNextNode (&mCallbackInfoListHead, Node);
325 }
326 return NULL;
327 }
328
329 /**
330 Callback thunk for Framework SMI handler.
331
332 This thunk functions calls the Framework SMI handler and converts the return value
333 defined from Framework SMI handlers to a correpsonding return value defined by PI SMM.
334
335 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
336 @param[in] Context Points to an optional handler context which was specified when the
337 handler was registered.
338 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
339 be conveyed from a non-SMM environment into an SMM environment.
340 @param[in, out] CommBufferSize The size of the CommBuffer.
341
342 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
343 should still be called.
344 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
345 still be called.
346 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
347 be called.
348 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
349 **/
350 EFI_STATUS
351 EFIAPI
352 CallbackThunk (
353 IN EFI_HANDLE DispatchHandle,
354 IN CONST VOID *Context OPTIONAL,
355 IN OUT VOID *CommBuffer OPTIONAL,
356 IN OUT UINTN *CommBufferSize OPTIONAL
357 )
358 {
359 EFI_STATUS Status;
360 CALLBACK_INFO *CallbackInfo;
361 UINTN Index;
362 UINTN CpuIndex;
363 EFI_SMM_CPU_STATE *State;
364 EFI_SMI_CPU_SAVE_STATE *SaveState;
365
366 ///
367 /// Before transferring the control into the Framework SMI handler, update CPU Save States
368 /// and MP states in the Framework SMST.
369 ///
370
371 for (CpuIndex = 0; CpuIndex < mNumberOfProcessors; CpuIndex++) {
372 State = (EFI_SMM_CPU_STATE *)gSmst->CpuSaveState[CpuIndex];
373 SaveState = &mFrameworkSmst->CpuSaveState[CpuIndex].Ia32SaveState;
374
375 if (State->x86.SMMRevId < EFI_SMM_MIN_REV_ID_x64) {
376 SaveState->SMBASE = State->x86.SMBASE;
377 SaveState->SMMRevId = State->x86.SMMRevId;
378 SaveState->IORestart = State->x86.IORestart;
379 SaveState->AutoHALTRestart = State->x86.AutoHALTRestart;
380 } else {
381 SaveState->SMBASE = State->x64.SMBASE;
382 SaveState->SMMRevId = State->x64.SMMRevId;
383 SaveState->IORestart = State->x64.IORestart;
384 SaveState->AutoHALTRestart = State->x64.AutoHALTRestart;
385 }
386
387 for (Index = 0; Index < sizeof (mCpuSaveStateConvTable) / sizeof (CPU_SAVE_STATE_CONVERSION); Index++) {
388 ///
389 /// Try to use SMM CPU Protocol to access CPU save states if possible
390 ///
391 Status = mSmmCpu->ReadSaveState (
392 mSmmCpu,
393 (UINTN)sizeof (UINT32),
394 mCpuSaveStateConvTable[Index].Register,
395 CpuIndex,
396 ((UINT8 *)SaveState) + mCpuSaveStateConvTable[Index].Offset
397 );
398 ASSERT_EFI_ERROR (Status);
399 }
400 }
401
402 mFrameworkSmst->CurrentlyExecutingCpu = gSmst->CurrentlyExecutingCpu;
403
404 ///
405 /// Search for Framework SMI handler information
406 ///
407 CallbackInfo = GetCallbackInfo (DispatchHandle);
408 ASSERT (CallbackInfo != NULL);
409
410 ///
411 /// Thunk into original Framwork SMI handler
412 ///
413 Status = (CallbackInfo->CallbackAddress) (
414 CallbackInfo->SmmImageHandle,
415 CallbackInfo->CommunicationBuffer,
416 CallbackInfo->SourceSize
417 );
418 ///
419 /// Save CPU Save States in case any of them was modified
420 ///
421 for (CpuIndex = 0; CpuIndex < mNumberOfProcessors; CpuIndex++) {
422 for (Index = 0; Index < sizeof (mCpuSaveStateConvTable) / sizeof (CPU_SAVE_STATE_CONVERSION); Index++) {
423 Status = mSmmCpu->WriteSaveState (
424 mSmmCpu,
425 (UINTN)sizeof (UINT32),
426 mCpuSaveStateConvTable[Index].Register,
427 CpuIndex,
428 ((UINT8 *)&mFrameworkSmst->CpuSaveState[CpuIndex].Ia32SaveState) +
429 mCpuSaveStateConvTable[Index].Offset
430 );
431 }
432 }
433
434 ///
435 /// Conversion of returned status code
436 ///
437 switch (Status) {
438 case EFI_HANDLER_SUCCESS:
439 Status = EFI_WARN_INTERRUPT_SOURCE_QUIESCED;
440 break;
441 case EFI_HANDLER_CRITICAL_EXIT:
442 case EFI_HANDLER_SOURCE_QUIESCED:
443 Status = EFI_SUCCESS;
444 break;
445 case EFI_HANDLER_SOURCE_PENDING:
446 Status = EFI_WARN_INTERRUPT_SOURCE_PENDING;
447 break;
448 }
449 return Status;
450 }
451
452 /**
453 Thunk service of EFI_SMM_BASE_PROTOCOL.RegisterCallback().
454
455 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
456 **/
457 VOID
458 RegisterCallback (
459 IN OUT SMMBASE_FUNCTION_DATA *FunctionData
460 )
461 {
462 CALLBACK_INFO *Buffer;
463
464 ///
465 /// Note that MakeLast and FloatingPointSave options are not supported in PI SMM
466 ///
467
468 ///
469 /// Allocate buffer for callback thunk information
470 ///
471 Buffer = (CALLBACK_INFO *)AllocateZeroPool (sizeof (CALLBACK_INFO));
472 if (Buffer == NULL) {
473 FunctionData->Status = EFI_OUT_OF_RESOURCES;
474 return;
475 }
476
477 ///
478 /// Fill SmmImageHandle and CallbackAddress into the thunk
479 ///
480 Buffer->SmmImageHandle = FunctionData->Args.RegisterCallback.SmmImageHandle;
481 Buffer->CallbackAddress = FunctionData->Args.RegisterCallback.CallbackAddress;
482
483 ///
484 /// Register the thunk code as a root SMI handler
485 ///
486 FunctionData->Status = gSmst->SmiHandlerRegister (
487 CallbackThunk,
488 NULL,
489 &Buffer->DispatchHandle
490 );
491 if (EFI_ERROR (FunctionData->Status)) {
492 FreePool (Buffer);
493 return;
494 }
495
496 ///
497 /// Save this callback info
498 ///
499 InsertTailList (&mCallbackInfoListHead, &Buffer->Link);
500 }
501
502
503 /**
504 Thunk service of EFI_SMM_BASE_PROTOCOL.SmmAllocatePool().
505
506 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
507 **/
508 VOID
509 HelperAllocatePool (
510 IN OUT SMMBASE_FUNCTION_DATA *FunctionData
511 )
512 {
513 FunctionData->Status = gSmst->SmmAllocatePool (
514 FunctionData->Args.AllocatePool.PoolType,
515 FunctionData->Args.AllocatePool.Size,
516 FunctionData->Args.AllocatePool.Buffer
517 );
518 }
519
520 /**
521 Thunk service of EFI_SMM_BASE_PROTOCOL.SmmFreePool().
522
523 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
524 **/
525 VOID
526 HelperFreePool (
527 IN OUT SMMBASE_FUNCTION_DATA *FunctionData
528 )
529 {
530 FreePool (FunctionData->Args.FreePool.Buffer);
531 FunctionData->Status = EFI_SUCCESS;
532 }
533
534 /**
535 Thunk service of EFI_SMM_BASE_PROTOCOL.Communicate().
536
537 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
538 **/
539 VOID
540 HelperCommunicate (
541 IN OUT SMMBASE_FUNCTION_DATA *FunctionData
542 )
543 {
544 LIST_ENTRY *Node;
545 CALLBACK_INFO *CallbackInfo;
546
547 if (FunctionData->Args.Communicate.CommunicationBuffer == NULL) {
548 FunctionData->Status = EFI_INVALID_PARAMETER;
549 return;
550 }
551
552 Node = GetFirstNode (&mCallbackInfoListHead);
553 while (!IsNull (&mCallbackInfoListHead, Node)) {
554 CallbackInfo = (CALLBACK_INFO *)Node;
555
556 if (FunctionData->Args.Communicate.ImageHandle == CallbackInfo->SmmImageHandle) {
557 CallbackInfo->CommunicationBuffer = FunctionData->Args.Communicate.CommunicationBuffer;
558 CallbackInfo->SourceSize = FunctionData->Args.Communicate.SourceSize;
559
560 ///
561 /// The message was successfully posted.
562 ///
563 FunctionData->Status = EFI_SUCCESS;
564 return;
565 }
566 Node = GetNextNode (&mCallbackInfoListHead, Node);
567 }
568
569 FunctionData->Status = EFI_INVALID_PARAMETER;
570 }
571
572 /**
573 Communication service SMI Handler entry.
574
575 This SMI handler provides services for the SMM Base Thunk driver.
576
577 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
578 @param[in] RegisterContext Points to an optional handler context which was specified when the
579 handler was registered.
580 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
581 be conveyed from a non-SMM environment into an SMM environment.
582 @param[in, out] CommBufferSize The size of the CommBuffer.
583
584 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
585 should still be called.
586 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
587 still be called.
588 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
589 be called.
590 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
591 **/
592 EFI_STATUS
593 EFIAPI
594 SmmHandlerEntry (
595 IN EFI_HANDLE DispatchHandle,
596 IN CONST VOID *RegisterContext,
597 IN OUT VOID *CommBuffer,
598 IN OUT UINTN *CommBufferSize
599 )
600 {
601 SMMBASE_FUNCTION_DATA *FunctionData;
602
603 ASSERT (CommBuffer != NULL);
604 ASSERT (*CommBufferSize == sizeof (SMMBASE_FUNCTION_DATA));
605
606 FunctionData = (SMMBASE_FUNCTION_DATA *)CommBuffer;
607
608 switch (FunctionData->Function) {
609 case SmmBaseFunctionRegister:
610 Register (FunctionData);
611 break;
612 case SmmBaseFunctionUnregister:
613 UnRegister (FunctionData);
614 break;
615 case SmmBaseFunctionRegisterCallback:
616 RegisterCallback (FunctionData);
617 break;
618 case SmmBaseFunctionAllocatePool:
619 HelperAllocatePool (FunctionData);
620 break;
621 case SmmBaseFunctionFreePool:
622 HelperFreePool (FunctionData);
623 break;
624 case SmmBaseFunctionCommunicate:
625 HelperCommunicate (FunctionData);
626 break;
627 default:
628 ASSERT (FALSE);
629 FunctionData->Status = EFI_UNSUPPORTED;
630 }
631 return EFI_SUCCESS;
632 }
633
634 /**
635 Entry point function of the SMM Base Helper SMM driver.
636
637 @param[in] ImageHandle The firmware allocated handle for the EFI image.
638 @param[in] SystemTable A pointer to the EFI System Table.
639
640 @retval EFI_SUCCESS The entry point is executed successfully.
641 @retval other Some error occurs when executing this entry point.
642 **/
643 EFI_STATUS
644 EFIAPI
645 SmmBaseHelperMain (
646 IN EFI_HANDLE ImageHandle,
647 IN EFI_SYSTEM_TABLE *SystemTable
648 )
649 {
650 EFI_STATUS Status;
651 EFI_MP_SERVICES_PROTOCOL *MpServices;
652 EFI_HANDLE Handle;
653 UINTN NumberOfEnabledProcessors;
654
655 Handle = NULL;
656 ///
657 /// Locate SMM CPU Protocol which is used later to retrieve/update CPU Save States
658 ///
659 Status = gSmst->SmmLocateProtocol (&gEfiSmmCpuProtocolGuid, NULL, (VOID **) &mSmmCpu);
660 ASSERT_EFI_ERROR (Status);
661
662 ///
663 /// Locate PE32 Image Protocol which is used later to load Framework SMM driver
664 ///
665 Status = SystemTable->BootServices->LocateProtocol (&gEfiLoadPeImageProtocolGuid, NULL, (VOID **) &mLoadPe32Image);
666 ASSERT_EFI_ERROR (Status);
667
668 //
669 // Get MP Services Protocol
670 //
671 Status = SystemTable->BootServices->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpServices);
672 ASSERT_EFI_ERROR (Status);
673
674 //
675 // Use MP Services Protocol to retrieve the number of processors and number of enabled processors
676 //
677 Status = MpServices->GetNumberOfProcessors (MpServices, &mNumberOfProcessors, &NumberOfEnabledProcessors);
678 ASSERT_EFI_ERROR (Status);
679
680 ///
681 /// Interface structure of SMM BASE Helper Ready Protocol is allocated from UEFI pool
682 /// instead of SMM pool so that SMM Base Thunk driver can access it in Non-SMM mode.
683 ///
684 Status = gBS->AllocatePool (
685 EfiBootServicesData,
686 sizeof (EFI_SMM_BASE_HELPER_READY_PROTOCOL),
687 (VOID **)&mSmmBaseHelperReady
688 );
689 ASSERT_EFI_ERROR (Status);
690
691 ///
692 /// Construct Framework SMST from PI SMST
693 ///
694 mFrameworkSmst = ConstructFrameworkSmst ();
695 mSmmBaseHelperReady->FrameworkSmst = mFrameworkSmst;
696 mSmmBaseHelperReady->ServiceEntry = SmmHandlerEntry;
697
698 ///
699 /// Register SMM Base Helper services for SMM Base Thunk driver
700 ///
701 Status = gSmst->SmiHandlerRegister (SmmHandlerEntry, &gEfiSmmBaseThunkCommunicationGuid, &mDispatchHandle);
702 ASSERT_EFI_ERROR (Status);
703
704 ///
705 /// Install EFI SMM Base Helper Protocol in the UEFI handle database
706 ///
707 Status = gBS->InstallProtocolInterface (
708 &Handle,
709 &gEfiSmmBaseHelperReadyProtocolGuid,
710 EFI_NATIVE_INTERFACE,
711 mSmmBaseHelperReady
712 );
713 ASSERT_EFI_ERROR (Status);
714
715 return Status;
716 }
717