]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Universal/Ebc/Dxe/EbcInt.c
Add some definitions for efi event in Uefi/UefiSpec.h to follow spec.
[mirror_edk2.git] / EdkModulePkg / Universal / Ebc / Dxe / EbcInt.c
index cf683295ea03e65c189277180f2c45c727d1bf96..2b647d9bae3792c8a0d29b590d0f95db85bdb88f 100644 (file)
@@ -1,24 +1,24 @@
 /*++\r
 \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
 \r
-Module Name:  \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
 \r
   EbcInt.c\r
-  \r
+\r
 Abstract:\r
 \r
   Top level module for the EBC virtual machine implementation.\r
   Provides auxilliary support routines for the VM. That is, routines\r
   that are not particularly related to VM execution of EBC instructions.\r
-  \r
+\r
 --*/\r
 \r
 #include "EbcInt.h"\r
@@ -178,6 +178,10 @@ static EFI_PERIODIC_CALLBACK  mDebugPeriodicCallback
 static EFI_EXCEPTION_CALLBACK mDebugExceptionCallback[MAX_EBC_EXCEPTION + 1] = {NULL};\r
 static EFI_GUID               mEfiEbcVmTestProtocolGuid = EFI_EBC_VM_TEST_PROTOCOL_GUID;\r
 \r
+static VOID*      mStackBuffer[MAX_STACK_NUM];\r
+static EFI_HANDLE mStackBufferIndex[MAX_STACK_NUM];\r
+static UINTN      mStackNum = 0;\r
+\r
 //\r
 // Event for Periodic callback\r
 //\r
@@ -192,17 +196,17 @@ InitializeEbcDriver (
   )\r
 /*++\r
 \r
-Routine Description: \r
+Routine Description:\r
 \r
-  Initializes the VM EFI interface.  Allocates memory for the VM interface \r
+  Initializes the VM EFI interface.  Allocates memory for the VM interface\r
   and registers the VM protocol.\r
 \r
-Arguments:  \r
+Arguments:\r
 \r
   ImageHandle - EFI image handle.\r
   SystemTable - Pointer to the EFI system table.\r
 \r
-Returns:  \r
+Returns:\r
   Standard EFI status code.\r
 \r
 --*/\r
@@ -222,12 +226,9 @@ Returns:
   //\r
   // Allocate memory for our protocol. Then fill in the blanks.\r
   //\r
-  Status = gBS->AllocatePool (\r
-                  EfiBootServicesData,\r
-                  sizeof (EFI_EBC_PROTOCOL),\r
-                  (VOID **) &EbcProtocol\r
-                  );\r
-  if (Status != EFI_SUCCESS) {\r
+  EbcProtocol = AllocatePool (sizeof (EFI_EBC_PROTOCOL));\r
+\r
+  if (EbcProtocol == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
@@ -273,7 +274,7 @@ Returns:
   }\r
 \r
   if (HandleBuffer != NULL) {\r
-    gBS->FreePool (HandleBuffer);\r
+    FreePool (HandleBuffer);\r
     HandleBuffer = NULL;\r
   }\r
   //\r
@@ -287,19 +288,22 @@ Returns:
                     EbcProtocol\r
                     );\r
     if (EFI_ERROR (Status)) {\r
-      gBS->FreePool (EbcProtocol);\r
+      FreePool (EbcProtocol);\r
       return Status;\r
     }\r
   }\r
+\r
+  Status = InitEBCStack();\r
+  if (EFI_ERROR(Status)) {\r
+    goto ErrorExit;\r
+  }\r
+\r
   //\r
   // Allocate memory for our debug protocol. Then fill in the blanks.\r
   //\r
-  Status = gBS->AllocatePool (\r
-                  EfiBootServicesData,\r
-                  sizeof (EFI_DEBUG_SUPPORT_PROTOCOL),\r
-                  (VOID **) &EbcDebugProtocol\r
-                  );\r
-  if (Status != EFI_SUCCESS) {\r
+  EbcDebugProtocol = AllocatePool (sizeof (EFI_DEBUG_SUPPORT_PROTOCOL));\r
+\r
+  if (EbcDebugProtocol == NULL) {\r
     goto ErrorExit;\r
   }\r
 \r
@@ -322,7 +326,7 @@ Returns:
   // This is recoverable, so free the memory and continue.\r
   //\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (EbcDebugProtocol);\r
+    FreePool (EbcDebugProtocol);\r
     goto ErrorExit;\r
   }\r
   //\r
@@ -341,6 +345,7 @@ Returns:
   return EFI_SUCCESS;\r
 \r
 ErrorExit:\r
+  FreeEBCStack();\r
   HandleBuffer  = NULL;\r
   Status = gBS->LocateHandleBuffer (\r
                   ByProtocol,\r
@@ -370,11 +375,11 @@ ErrorExit:
   }\r
 \r
   if (HandleBuffer != NULL) {\r
-    gBS->FreePool (HandleBuffer);\r
+    FreePool (HandleBuffer);\r
     HandleBuffer = NULL;\r
   }\r
 \r
-  gBS->FreePool (EbcProtocol);\r
+  FreePool (EbcProtocol);\r
 \r
   return Status;\r
 }\r
@@ -391,9 +396,9 @@ EbcCreateThunk (
 /*++\r
 \r
 Routine Description:\r
-  \r
+\r
   This is the top-level routine plugged into the EBC protocol. Since thunks\r
-  are very processor-specific, from here we dispatch directly to the very \r
+  are very processor-specific, from here we dispatch directly to the very\r
   processor-specific routine EbcCreateThunks().\r
 \r
 Arguments:\r
@@ -433,7 +438,7 @@ EbcDebugGetMaximumProcessorIndex (
 /*++\r
 \r
 Routine Description:\r
-  \r
+\r
   This EBC debugger protocol service is called by the debug agent\r
 \r
 Arguments:\r
@@ -441,7 +446,7 @@ Arguments:
   This              - pointer to the caller's debug support protocol interface\r
   MaxProcessorIndex - pointer to a caller allocated UINTN in which the maximum\r
                       processor index is returned.\r
-                                               \r
+\r
 Returns:\r
 \r
   Standard EFI_STATUS\r
@@ -463,10 +468,10 @@ EbcDebugRegisterPeriodicCallback (
 /*++\r
 \r
 Routine Description:\r
-  \r
+\r
   This protocol service is called by the debug agent to register a function\r
   for us to call on a periodic basis.\r
-  \r
+\r
 \r
 Arguments:\r
 \r
@@ -485,7 +490,7 @@ Returns:
   if ((mDebugPeriodicCallback != NULL) && (PeriodicCallback != NULL)) {\r
     return EFI_ALREADY_STARTED;\r
   }\r
-       \r
+\r
   mDebugPeriodicCallback = PeriodicCallback;\r
   return EFI_SUCCESS;\r
 }\r
@@ -502,15 +507,15 @@ EbcDebugRegisterExceptionCallback (
 /*++\r
 \r
 Routine Description:\r
-  \r
+\r
   This protocol service is called by the debug agent to register a function\r
   for us to call when we detect an exception.\r
-  \r
+\r
 \r
 Arguments:\r
 \r
   This              - pointer to the caller's debug support protocol interface\r
-  PeriodicCallback  - pointer to the function to call periodically\r
+  ExceptionCallback - pointer to the function to the exception\r
 \r
 Returns:\r
 \r
@@ -543,12 +548,12 @@ EbcDebugInvalidateInstructionCache (
 /*++\r
 \r
 Routine Description:\r
-  \r
+\r
   This EBC debugger protocol service is called by the debug agent.  Required\r
   for DebugSupport compliance but is only stubbed out for EBC.\r
 \r
 Arguments:\r
-                                               \r
+\r
 Returns:\r
 \r
   EFI_SUCCESS\r
@@ -569,7 +574,7 @@ EbcDebugSignalException (
 Routine Description:\r
 \r
   The VM interpreter calls this function when an exception is detected.\r
-  \r
+\r
 Arguments:\r
 \r
   VmPtr - pointer to a VM context for passing info to the EFI debugger.\r
@@ -577,7 +582,7 @@ Arguments:
 Returns:\r
 \r
   EFI_SUCCESS if it returns at all\r
-  \r
+\r
 --*/\r
 {\r
   EFI_SYSTEM_CONTEXT_EBC  EbcContext;\r
@@ -636,7 +641,7 @@ Returns:
     VmPtr->Ip    = (VMIP)(UINTN)EbcContext.Ip;\r
     VmPtr->Flags = EbcContext.Flags;\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -650,7 +655,7 @@ InitializeEbcCallback (
 Routine Description:\r
 \r
   To install default Callback function for the VM interpreter.\r
-  \r
+\r
 Arguments:\r
 \r
   This - pointer to the instance of DebugSupport protocol\r
@@ -658,7 +663,7 @@ Arguments:
 Returns:\r
 \r
   None\r
-  \r
+\r
 --*/\r
 {\r
   INTN       Index;\r
@@ -680,8 +685,8 @@ Returns:
   // For PeriodicCallback\r
   //\r
   Status = gBS->CreateEvent (\r
-                  EFI_EVENT_TIMER | EFI_EVENT_NOTIFY_SIGNAL,\r
-                  EFI_TPL_NOTIFY,\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
                   EbcPeriodicNotifyFunction,\r
                   &mVmPtr,\r
                   &mEbcPeriodicEvent\r
@@ -715,7 +720,7 @@ Routine Description:
   The default Exception Callback for the VM interpreter.\r
   In this function, we report status code, and print debug information\r
   about EBC_CONTEXT, then dead loop.\r
-  \r
+\r
 Arguments:\r
 \r
   InterruptType - Interrupt type.\r
@@ -724,7 +729,7 @@ Arguments:
 Returns:\r
 \r
   None\r
-  \r
+\r
 --*/\r
 {\r
   //\r
@@ -748,7 +753,7 @@ Routine Description:
 \r
   The periodic callback function for EBC VM interpreter, which is used\r
   to support the EFI debug support protocol.\r
-  \r
+\r
 Arguments:\r
 \r
   Event   - The Periodic Callback Event.\r
@@ -757,7 +762,7 @@ Arguments:
 Returns:\r
 \r
   None.\r
-  \r
+\r
 --*/\r
 {\r
   VM_CONTEXT *VmPtr;\r
@@ -782,7 +787,7 @@ Routine Description:
 \r
   The VM interpreter calls this function on a periodic basis to support\r
   the EFI debug support protocol.\r
-  \r
+\r
 Arguments:\r
 \r
   VmPtr - pointer to a VM context for passing info to the debugger.\r
@@ -790,12 +795,12 @@ Arguments:
 Returns:\r
 \r
   Standard EFI status.\r
-  \r
+\r
 --*/\r
 {\r
   EFI_SYSTEM_CONTEXT_EBC   EbcContext;\r
   EFI_SYSTEM_CONTEXT       SystemContext;\r
-  \r
+\r
   //\r
   // If someone's registered for periodic callbacks, then call them.\r
   //\r
@@ -833,7 +838,7 @@ Returns:
     VmPtr->Ip    = (VMIP)(UINTN)EbcContext.Ip;\r
     VmPtr->Flags = EbcContext.Flags;\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -847,8 +852,8 @@ EbcUnloadImage (
 /*++\r
 \r
 Routine Description:\r
-  \r
-  This routine is called by the core when an image is being unloaded from \r
+\r
+  This routine is called by the core when an image is being unloaded from\r
   memory. Basically we now have the opportunity to do any necessary cleanup.\r
   Typically this will include freeing any memory allocated for thunk-creation.\r
 \r
@@ -873,6 +878,7 @@ Returns:
   // First go through our list of known image handles and see if we've already\r
   // created an image list element for this image handle.\r
   //\r
+  ReturnEBCStackByHandle(ImageHandle);\r
   PrevImageList = NULL;\r
   for (ImageList = mEbcImageList; ImageList != NULL; ImageList = ImageList->Next) {\r
     if (ImageList->ImageHandle == ImageHandle) {\r
@@ -894,8 +900,8 @@ Returns:
   ThunkList = ImageList->ThunkList;\r
   while (ThunkList != NULL) {\r
     NextThunkList = ThunkList->Next;\r
-    gBS->FreePool (ThunkList->ThunkBuffer);\r
-    gBS->FreePool (ThunkList);\r
+    FreePool (ThunkList->ThunkBuffer);\r
+    FreePool (ThunkList);\r
     ThunkList = NextThunkList;\r
   }\r
   //\r
@@ -912,7 +918,7 @@ Returns:
   //\r
   // Now free up the image list element\r
   //\r
-  gBS->FreePool (ImageList);\r
+  FreePool (ImageList);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -925,8 +931,8 @@ EbcAddImageThunk (
 /*++\r
 \r
 Routine Description:\r
-  \r
-  Add a thunk to our list of thunks for a given image handle. \r
+\r
+  Add a thunk to our list of thunks for a given image handle.\r
   Also flush the instruction cache since we've written thunk code\r
   to memory that will be executed eventually.\r
 \r
@@ -937,7 +943,7 @@ Arguments:
   ThunkSize    - the size of the thunk memory allocated\r
 \r
 Returns:\r
\r
+\r
   EFI_OUT_OF_RESOURCES    - memory allocation failed\r
   EFI_SUCCESS             - successful completion\r
 \r
@@ -970,12 +976,9 @@ Returns:
     //\r
     // Allocate a new one\r
     //\r
-    Status = gBS->AllocatePool (\r
-                    EfiBootServicesData,\r
-                    sizeof (EBC_IMAGE_LIST),\r
-                    (VOID **) &ImageList\r
-                    );\r
-    if (Status != EFI_SUCCESS) {\r
+    ImageList = AllocatePool (sizeof (EBC_IMAGE_LIST));\r
+\r
+    if (ImageList == NULL) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
@@ -987,12 +990,9 @@ Returns:
   //\r
   // Ok, now create a new thunk element to add to the list\r
   //\r
-  Status = gBS->AllocatePool (\r
-                  EfiBootServicesData,\r
-                  sizeof (EBC_THUNK_LIST),\r
-                  (VOID **) &ThunkList\r
-                  );\r
-  if (Status != EFI_SUCCESS) {\r
+  ThunkList = AllocatePool (sizeof (EBC_THUNK_LIST));\r
+\r
+  if (ThunkList == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
@@ -1032,6 +1032,87 @@ EbcGetVersion (
   return EFI_SUCCESS;\r
 }\r
 \r
+EFI_STATUS\r
+GetEBCStack(\r
+  EFI_HANDLE Handle,\r
+  VOID       **StackBuffer,\r
+  UINTN      *BufferIndex\r
+  )\r
+{\r
+  UINTN   Index;\r
+  EFI_TPL OldTpl;\r
+  OldTpl = gBS->RaiseTPL(TPL_HIGH_LEVEL);\r
+  for (Index = 0; Index < mStackNum; Index ++) {\r
+    if (mStackBufferIndex[Index] == NULL) {\r
+      mStackBufferIndex[Index] = Handle;\r
+      break;\r
+    }\r
+  }\r
+  gBS->RestoreTPL(OldTpl);\r
+  if (Index == mStackNum) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  *BufferIndex = Index;\r
+  *StackBuffer = mStackBuffer[Index];\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+ReturnEBCStack(\r
+  UINTN Index\r
+  )\r
+{\r
+  mStackBufferIndex[Index] =NULL;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+ReturnEBCStackByHandle(\r
+  EFI_HANDLE Handle\r
+  )\r
+{\r
+  UINTN Index;\r
+  for (Index = 0; Index < mStackNum; Index ++) {\r
+    if (mStackBufferIndex[Index] == Handle) {\r
+      break;\r
+    }\r
+  }\r
+  if (Index == mStackNum) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  mStackBufferIndex[Index] = NULL;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+InitEBCStack (\r
+  VOID\r
+  )\r
+{\r
+  for (mStackNum = 0; mStackNum < MAX_STACK_NUM; mStackNum ++) {\r
+    mStackBuffer[mStackNum] = AllocatePool(STACK_POOL_SIZE);\r
+    mStackBufferIndex[mStackNum] = NULL;\r
+    if (mStackBuffer[mStackNum] == NULL) {\r
+      break;\r
+    }\r
+  }\r
+  if (mStackNum == 0) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FreeEBCStack(\r
+  VOID\r
+  )\r
+{\r
+  UINTN Index;\r
+  for (Index = 0; Index < mStackNum; Index ++) {\r
+    FreePool(mStackBuffer[Index]);\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
 STATIC\r
 EFI_STATUS\r
 InitEbcVmTestProtocol (\r
@@ -1040,7 +1121,7 @@ InitEbcVmTestProtocol (
 /*++\r
 \r
 Routine Description:\r
-  \r
+\r
   Produce an EBC VM test protocol that can be used for regression tests.\r
 \r
 Arguments:\r
@@ -1061,8 +1142,8 @@ Returns:
   //\r
   // Allocate memory for the protocol, then fill in the fields\r
   //\r
-  Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_EBC_VM_TEST_PROTOCOL), (VOID **) &EbcVmTestProtocol);\r
-  if (Status != EFI_SUCCESS) {\r
+  EbcVmTestProtocol = AllocatePool (sizeof (EFI_EBC_VM_TEST_PROTOCOL));\r
+  if (EbcVmTestProtocol == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   EbcVmTestProtocol->Execute      = (EBC_VM_TEST_EXECUTE) EbcExecuteInstructions;\r
@@ -1078,7 +1159,7 @@ Returns:
   Handle  = NULL;\r
   Status  = gBS->InstallProtocolInterface (&Handle, &mEfiEbcVmTestProtocolGuid, EFI_NATIVE_INTERFACE, EbcVmTestProtocol);\r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (EbcVmTestProtocol);\r
+    FreePool (EbcVmTestProtocol);\r
   }\r
   return Status;\r
 }\r