]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Core/Dxe/Image/Image.c
Change the EfiAquireLock with RaiseTPL as the call to Boot Service UnloadImage can...
[mirror_edk2.git] / EdkModulePkg / Core / Dxe / Image / Image.c
index 7810e6b09393291c5e1a8d5bd5bf35114d72b2fc..3696400072e6f6be71090466699a6ef877228256 100644 (file)
@@ -1,6 +1,6 @@
 /*++\r
 \r
-Copyright (c) 2006, Intel Corporation\r
+Copyright (c) 2006 - 2007, 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
@@ -24,11 +24,6 @@ Abstract:
 // Module Globals\r
 //\r
 \r
-//\r
-// LIST of runtime images that need to be relocated.\r
-//\r
-LIST_ENTRY                 mRuntimeImageList = INITIALIZE_LIST_HEAD_VARIABLE (mRuntimeImageList);\r
-\r
 LOADED_IMAGE_PRIVATE_DATA  *mCurrentImage = NULL;\r
 \r
 LOAD_PE32_IMAGE_PRIVATE_DATA  mLoadPe32PrivateData = {\r
@@ -78,9 +73,7 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage  = {
   NULL,                       // JumpContext\r
   0,                          // Machine\r
   NULL,                       // Ebc\r
-  FALSE,                      // RuntimeFixupValid\r
-  NULL,                       // RuntimeFixup\r
-  { NULL, NULL },             // Link\r
+  NULL,                       // RuntimeData\r
 };\r
 \r
 \r
@@ -173,52 +166,6 @@ Returns:
            );\r
 }\r
 \r
-\r
-EFI_STATUS\r
-CoreShutdownImageServices (\r
-  VOID\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Transfer control of runtime images to runtime service\r
-\r
-Arguments:\r
-\r
-  None\r
-\r
-Returns:\r
-\r
-  EFI_SUCCESS       - Function successfully returned\r
-\r
---*/\r
-{\r
-  LIST_ENTRY                  *Link;\r
-  LOADED_IMAGE_PRIVATE_DATA   *Image;\r
-\r
-  //\r
-  // The Runtime AP is required for the core to function!\r
-  //\r
-  ASSERT (gRuntime != NULL);\r
-\r
-  for (Link = mRuntimeImageList.ForwardLink; Link != &mRuntimeImageList; Link = Link->ForwardLink) {\r
-    Image = CR (Link, LOADED_IMAGE_PRIVATE_DATA, Link, LOADED_IMAGE_PRIVATE_DATA_SIGNATURE);\r
-    if (Image->RuntimeFixupValid) {\r
-      gRuntime->RegisterImage (\r
-                  gRuntime,\r
-                  (UINT64)(UINTN)(Image->Info.ImageBase),\r
-                  (EFI_SIZE_TO_PAGES ((UINTN)Image->Info.ImageSize)),\r
-                  Image->RuntimeFixup\r
-                  );\r
-    }\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-\r
 EFI_STATUS\r
 CoreLoadPeImage (\r
   IN VOID                        *Pe32Handle,\r
@@ -253,8 +200,9 @@ Returns:
 \r
 --*/\r
 {\r
-  EFI_STATUS                            Status;\r
-  UINTN                                 Size;\r
+  EFI_STATUS      Status;\r
+  BOOLEAN         DstBufAlocated;\r
+  UINTN           Size;\r
 \r
   ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));\r
 \r
@@ -271,8 +219,8 @@ Returns:
 \r
   if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->ImageContext.Machine)) {\r
     //\r
-    // The PE/COFF loader can support loading image types that can be executed. \r
-    // If we loaded an image type that we can not execute return EFI_UNSUPORTED. \r
+    // The PE/COFF loader can support loading image types that can be executed.\r
+    // If we loaded an image type that we can not execute return EFI_UNSUPORTED.\r
     //\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -281,7 +229,7 @@ Returns:
   //\r
   // Allocate memory of the correct memory type aligned on the required image boundry\r
   //\r
-\r
+  DstBufAlocated = FALSE;\r
   if (DstBuffer == 0) {\r
     //\r
     // Allocate Destination Buffer as caller did not pass it in\r
@@ -299,18 +247,30 @@ Returns:
     // If the image relocations have not been stripped, then load at any address.\r
     // Otherwise load at the address at which it was linked.\r
     //\r
-    Status = CoreAllocatePages (\r
-               (Image->ImageContext.RelocationsStripped) ? AllocateAddress : AllocateAnyPages,\r
-               Image->ImageContext.ImageCodeMemoryType,\r
-               Image->NumberOfPages,\r
-               &Image->ImageContext.ImageAddress\r
-               );\r
+    // Memory below 1MB should be treated reserved for CSM and there should be\r
+    // no modules whose preferred load addresses are below 1MB.\r
+    //\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) {\r
+      Status = CoreAllocatePages (\r
+                 AllocateAddress,\r
+                 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
+                 Image->NumberOfPages,\r
+                 &Image->ImageContext.ImageAddress\r
+                 );\r
+    }\r
+    if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {\r
+      Status = CoreAllocatePages (\r
+                 AllocateAnyPages,\r
+                 (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType),\r
+                 Image->NumberOfPages,\r
+                 &Image->ImageContext.ImageAddress\r
+                 );\r
+    }\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-\r
-    Image->ImageBasePage = Image->ImageContext.ImageAddress;\r
-\r
+    DstBufAlocated = TRUE;\r
   } else {\r
     //\r
     // Caller provided the destination buffer\r
@@ -334,9 +294,9 @@ Returns:
 \r
     Image->NumberOfPages = EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image->ImageContext.SectionAlignment);\r
     Image->ImageContext.ImageAddress = DstBuffer;\r
-    Image->ImageBasePage = Image->ImageContext.ImageAddress;\r
   }\r
 \r
+  Image->ImageBasePage = Image->ImageContext.ImageAddress;\r
   Image->ImageContext.ImageAddress =\r
       (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) &\r
       ~((UINTN)Image->ImageContext.SectionAlignment - 1);\r
@@ -346,7 +306,7 @@ Returns:
   //\r
   Status = gEfiPeiPeCoffLoader->LoadImage (gEfiPeiPeCoffLoader, &Image->ImageContext);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -361,13 +321,6 @@ Returns:
         Status = EFI_OUT_OF_RESOURCES;\r
         goto Done;\r
       }\r
-\r
-      //\r
-      // Make a list off all the RT images so we can let the RT AP know about them\r
-      //\r
-      Image->RuntimeFixupValid = TRUE;\r
-      Image->RuntimeFixup = Image->ImageContext.FixupData;\r
-      InsertTailList (&mRuntimeImageList, &Image->Link);\r
     }\r
   }\r
 \r
@@ -376,7 +329,7 @@ Returns:
   //\r
   Status = gEfiPeiPeCoffLoader->RelocateImage (gEfiPeiPeCoffLoader, &Image->ImageContext);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -436,8 +389,24 @@ Returns:
   Image->Type               = Image->ImageContext.ImageType;\r
   Image->Info.ImageBase     = (VOID *)(UINTN)Image->ImageContext.ImageAddress;\r
   Image->Info.ImageSize     = Image->ImageContext.ImageSize;\r
-  Image->Info.ImageCodeType = Image->ImageContext.ImageCodeMemoryType;\r
-  Image->Info.ImageDataType = Image->ImageContext.ImageDataMemoryType;\r
+  Image->Info.ImageCodeType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType);\r
+  Image->Info.ImageDataType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageDataMemoryType);\r
+  if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) {\r
+    if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {\r
+      //\r
+      // Make a list off all the RT images so we can let the RT AP know about them.\r
+      //\r
+      Image->RuntimeData = CoreAllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY));\r
+      if (Image->RuntimeData == NULL) {\r
+        goto Done;\r
+      }\r
+      Image->RuntimeData->ImageBase      = Image->Info.ImageBase;\r
+      Image->RuntimeData->ImageSize      = (UINT64) (Image->Info.ImageSize);\r
+      Image->RuntimeData->RelocationData = Image->ImageContext.FixupData;\r
+      Image->RuntimeData->Handle         = Image->Handle;\r
+      InsertTailList (&gRuntime->ImageHead, &Image->RuntimeData->Link);\r
+    }\r
+  }\r
 \r
   //\r
   // Fill in the entry point of the image if it is available\r
@@ -451,12 +420,12 @@ Returns:
   //\r
 \r
   DEBUG_CODE_BEGIN ();\r
\r
+\r
     UINTN Index;\r
     UINTN StartIndex;\r
     CHAR8 EfiFileName[256];\r
 \r
-    DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading driver at 0x%08x EntryPoint=0x%08x ", (UINTN)Image->ImageContext.ImageAddress, (UINTN)Image->ImageContext.EntryPoint));\r
+    DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading driver at 0x%10p EntryPoint=0x%10p ", (VOID *)(UINTN)Image->ImageContext.ImageAddress, (VOID *)(UINTN)Image->ImageContext.EntryPoint));\r
     if (Image->ImageContext.PdbPointer != NULL) {\r
       StartIndex = 0;\r
       for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) {\r
@@ -483,16 +452,25 @@ Returns:
       DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
     }\r
     DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
-  \r
+\r
   DEBUG_CODE_END ();\r
 \r
   return EFI_SUCCESS;\r
 \r
 Done:\r
+\r
   //\r
-  // Free memory\r
+  // Free memory.\r
   //\r
-  CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);\r
+\r
+  if (DstBufAlocated) {\r
+    CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);\r
+  }\r
+\r
+  if (Image->ImageContext.FixupData != NULL) {\r
+    CoreFreePool (Image->ImageContext.FixupData);\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -536,7 +514,7 @@ Returns:
   return Image;\r
 }\r
 \r
-\r
+STATIC\r
 EFI_STATUS\r
 CoreLoadImageCommon (\r
   IN  BOOLEAN                          BootPolicy,\r
@@ -953,17 +931,17 @@ Returns:
   // JumpContext must be aligned on a CPU specific boundary.\r
   // Overallocate the buffer and force the required alignment\r
   //\r
-  Image->JumpBuffer = CoreAllocateBootServicesPool (sizeof (*Image->JumpContext) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
+  Image->JumpBuffer = CoreAllocateBootServicesPool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
   if (Image->JumpBuffer == NULL) {\r
     PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  Image->JumpContext = (VOID *)((UINTN)(ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT)) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
+  Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT);\r
 \r
   SetJumpFlag = SetJump (Image->JumpContext);\r
   //\r
-  // The initial call to SetJump() must always return 0.  \r
-  // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump(). \r
+  // The initial call to SetJump() must always return 0.\r
+  // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump().\r
   //\r
   if (!SetJumpFlag) {\r
     //\r
@@ -979,7 +957,7 @@ Returns:
     //\r
     DEBUG_CODE_BEGIN ();\r
       if (EFI_ERROR (Image->Status)) {\r
-        DEBUG ((EFI_D_ERROR, "Error: Image at %08X start failed: %x\n", Image->Info.ImageBase, Image->Status));\r
+        DEBUG ((EFI_D_ERROR, "Error: Image at %10p start failed: %r\n", Image->Info.ImageBase, Image->Status));\r
       }\r
     DEBUG_CODE_END ();\r
 \r
@@ -1168,11 +1146,14 @@ Returns:
                );\r
   }\r
 \r
-  if (Image->RuntimeFixupValid) {\r
-    //\r
-    // Remove the Image from the Runtime Image list as we are about to Free it!\r
-    //\r
-    RemoveEntryList (&Image->Link);\r
+  if (Image->RuntimeData != NULL) {\r
+    if (Image->RuntimeData->Link.ForwardLink != NULL) {\r
+      //\r
+      // Remove the Image from the Runtime Image list as we are about to Free it!\r
+      //\r
+      RemoveEntryList (&Image->RuntimeData->Link);\r
+    }\r
+    CoreFreePool (Image->RuntimeData);\r
   }\r
 \r
   //\r
@@ -1237,10 +1218,18 @@ Returns:
 --*/\r
 {\r
   LOADED_IMAGE_PRIVATE_DATA  *Image;\r
+  EFI_TPL                    OldTpl;\r
 \r
+  //\r
+  // Prevent possible reentrance to this function\r
+  // for the same ImageHandle\r
+  // \r
+  OldTpl = CoreRaiseTpl (EFI_TPL_NOTIFY); \r
\r
   Image = CoreLoadedImageInfo (ImageHandle);\r
   if (Image == NULL_HANDLE) {\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   }\r
 \r
   if (!Image->Started) {\r
@@ -1248,7 +1237,8 @@ Returns:
     // The image has not been started so just free its resources\r
     //\r
     CoreUnloadAndCloseImage (Image, TRUE);\r
-    return EFI_SUCCESS;\r
+    Status = EFI_SUCCESS;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -1256,7 +1246,8 @@ Returns:
   //\r
   if (Image != mCurrentImage) {\r
     DEBUG ((EFI_D_LOAD|EFI_D_ERROR, "Exit: Image is not exitable image\n"));\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -1271,11 +1262,13 @@ Returns:
     Image->ExitDataSize = ExitDataSize;\r
     Image->ExitData = CoreAllocateBootServicesPool (Image->ExitDataSize);\r
     if (Image->ExitData == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Done;\r
     }\r
     CopyMem (Image->ExitData, ExitData, Image->ExitDataSize);\r
   }\r
 \r
+  CoreRestoreTpl (OldTpl);\r
   //\r
   // return to StartImage\r
   //\r
@@ -1285,7 +1278,10 @@ Returns:
   // If we return from LongJump, then it is an error\r
   //\r
   ASSERT (FALSE);\r
-  return EFI_ACCESS_DENIED;\r
+  Status = EFI_ACCESS_DENIED;\r
+Done:\r
+  CoreRestoreTpl (OldTpl);\r
+  return Status;\r
 }\r
 \r
 \r
@@ -1315,13 +1311,21 @@ Returns:
 {\r
   EFI_STATUS                 Status;\r
   LOADED_IMAGE_PRIVATE_DATA  *Image;\r
+  EFI_TPL                    OldTpl;\r
 \r
+  //\r
+  // Prevent possible reentrance to this function\r
+  // for the same ImageHandle\r
+  // \r
+  OldTpl = CoreRaiseTpl (EFI_TPL_NOTIFY);\r
\r
   Image = CoreLoadedImageInfo (ImageHandle);\r
   if (Image == NULL ) {\r
     //\r
     // The image handle is not valid\r
     //\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   }\r
 \r
   if (Image->Started) {\r
@@ -1348,6 +1352,8 @@ Returns:
     CoreUnloadAndCloseImage (Image, TRUE);\r
   }\r
 \r
+Done:\r
+  CoreRestoreTpl (OldTpl);\r
   return Status;\r
 }\r
 \r