From patchwork Thu Sep 20 23:01:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 147172 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp100130ljw; Thu, 20 Sep 2018 16:02:08 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYSSM+639m7eX+9n0f82OHmBP17sqQZu3rjBizZHBp6J/j6PxIldOidI6XzC7CFgR/NeCOu X-Received: by 2002:a63:fa49:: with SMTP id g9-v6mr26837914pgk.18.1537484528073; Thu, 20 Sep 2018 16:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537484528; cv=none; d=google.com; s=arc-20160816; b=J+0kzRnEAaEpRNmt+PTfl7MlfnJOEdjnRRzs4rTag2X39ZhiDsr6gC7zXYg+QrvIHY fHQC7KcH9hc1KF4dCM/hi2Xt4jIch8dW999D8ScEz7RQRitWd11bzHqhp48qTFoiUKIZ aanR6SvA4CMBeyVaXED3QMMp+2ogz6hQ5hirK6NIAm8v7o0bpYTuz8mRcjl6yFnhCnex aTICpDumeM0JclnCa5H7jr+9lpwQhy8xilBvO9/Xa9i6NyH7suI3WUgv8Qqgcly3v7FV zRVMEnXYqJZm8VleVST1umcxekoWqL/vqOQ6RU7+fSpvx03BjQqFiXRQaLSC2TLeSXyU nVsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=2SD1NByA8VD7L6n94QwMaYxVuipHOrEGZE5ilaOwziI=; b=O18WLyZsqSz2gzVwmeOpYUFrWeCBzbJLtFVqzh1EbFzCYfh6N5YB2vLS1RRzzkPu/L k6rY7nRINeRdxljWoZG8NRi7qCP9kO5lq3a2CuMuYn7RbV4Kd4U4Rm93SUSImikVVyfF kutiSFdHxvj99BXbiUt1L8cVtimRNwDObrkKNbqqZtFUMwWkk5ntOYQugLV2SLkHumQ0 IaCQ7mJYFZXTjX7m+vG9xh4QYPwzFHGyl01EWYfjz9cKRM758q1Xy//gP/K2edZ5vq/R z71rObh8eLD2DJu/FmYjtuMv3hdSwbtO0QXt5I4U2NMXlNhY3uG0PS3h32QZwAO0GamT K3pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=RdwdkFqS; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id f94-v6si17992109plb.10.2018.09.20.16.02.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 16:02:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=RdwdkFqS; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 061932194D3B8; Thu, 20 Sep 2018 16:02:00 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:4864:20::543; helo=mail-pg1-x543.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 5DDEF2114A6A1 for ; Thu, 20 Sep 2018 16:01:57 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id q19-v6so4248050pgn.10 for ; Thu, 20 Sep 2018 16:01:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QZsxm0ylxN5Ie88kSOXV1CPT8vEwy5q94Cy3QGVEpvA=; b=RdwdkFqSFHHGIWVf9Ki6Ve3AOwj7G/HagrROkt/lkQX/GGNEnQOrrygagT7mJRS253 umI/2/zR4GXc3RCDOI0kfatcGA5HcrQsvrQz/o3F/WtFeXRoHpVsZAJZjG+8bj/kzeNu qQ0ZqDbE8gmFBwPKHjttBCfy5dcbNiZYBuhF8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QZsxm0ylxN5Ie88kSOXV1CPT8vEwy5q94Cy3QGVEpvA=; b=TfYLKOAQdYMvxr3RV5ouecc6nifsSe9gFQ/4JZ5Qsy8Sh970IyAjugdiCLgb6dM/Yc ofi6NGI+K8GnGBqvSFKIN8/ktQ07HptPxAzhAadKLr1q2g374NkUCoD7rgDBMBr+sv47 61MyynRdlVqcUQniVXXAdDidM19Oi4MPLUvAaFGcNBo49ydE6F1X6ogkS/ZRNvOUtv+O 1pcnGhdYZQ8p1XuKk+T1lukQavaCpnfuplRTax0Viv73X5/wZiTORy6ra4/waZTfWRpY +id02wmwdQVCArvFPI9DH++3/z0acnRtfLiFDv8MZuRfpbDHtPruc0wStrzx47wrYpJx bZuw== X-Gm-Message-State: APzg51DRRiX7jIF9VzSGLLAHnVzetm12S/BCWbPW6kBZNwgS5iJ/52Dc sHB+dPXrTlpjq9j+oCj5qsia7nO0Xsc= X-Received: by 2002:a62:c8d2:: with SMTP id i79-v6mr43174697pfk.35.1537484516770; Thu, 20 Sep 2018 16:01:56 -0700 (PDT) Received: from localhost.localdomain ([209.121.128.187]) by smtp.gmail.com with ESMTPSA id p4-v6sm37507823pfd.65.2018.09.20.16.01.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 16:01:55 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Thu, 20 Sep 2018 16:01:43 -0700 Message-Id: <20180920230145.7565-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180920230145.7565-1-ard.biesheuvel@linaro.org> References: <20180920230145.7565-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v3 5/7] MdeModulePkg/EbcDxe: implement the PE/COFF emulator protocol X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ruiyu Ni , Vincent Zimmer , Eric Dong , Andrew Fish , Jaben Carsey , Brian Richardson , Liming Gao , Michael D Kinney , Star Zeng MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Implement the new EDK2 PE/COFF image emulator protocol so that we can remove the EBC specific handling in the DXE core and other places in the core code. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Universal/EbcDxe/EbcDxe.inf | 3 + MdeModulePkg/Universal/EbcDxe/EbcInt.c | 127 ++++++++++++++++++++ MdeModulePkg/Universal/EbcDxe/EbcInt.h | 3 + 3 files changed, 133 insertions(+) -- 2.17.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf index 8f128b121d0b..9cba1d0a917b 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf +++ b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf @@ -62,7 +62,9 @@ MdeModulePkg/MdeModulePkg.dec [LibraryClasses] + CacheMaintenanceLib MemoryAllocationLib + PeCoffLib UefiBootServicesTableLib BaseMemoryLib UefiDriverEntryPoint @@ -73,6 +75,7 @@ [Protocols] gEfiDebugSupportProtocolGuid ## PRODUCES gEfiEbcProtocolGuid ## PRODUCES + gEdkiiPeCoffImageEmulatorProtocolGuid ## PRODUCES gEfiEbcVmTestProtocolGuid ## SOMETIMES_PRODUCES gEfiEbcSimpleDebuggerProtocolGuid ## SOMETIMES_CONSUMES diff --git a/MdeModulePkg/Universal/EbcDxe/EbcInt.c b/MdeModulePkg/Universal/EbcDxe/EbcInt.c index 727ba8bcae44..45e5da1823e7 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcInt.c +++ b/MdeModulePkg/Universal/EbcDxe/EbcInt.c @@ -349,6 +349,123 @@ UINTN mStackNum = 0; EFI_EVENT mEbcPeriodicEvent; VM_CONTEXT *mVmPtr = NULL; +/** + Check whether the emulator supports executing a certain PE/COFF image + + @param[in] This This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL + structure + @param[in] MachineType The machine type for which the image was built + @param[in] ImageType Whether the image is an application, a boot time + driver or a runtime driver. + @param[in] DevicePath Path to device where the image originated + (e.g., a PCI option ROM) + + @retval TRUE The image is supported by the emulator + @retval FALSE The image is not supported by the emulator. +**/ +BOOLEAN +EFIAPI +EbcIsImageSupported ( + IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This, + IN UINT16 MachineType, + IN UINT16 ImageType, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL + ) +{ + if (MachineType != EFI_IMAGE_MACHINE_EBC) { + return FALSE; + } + + if (ImageType != EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION && + ImageType != EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) { + return FALSE; + } + return TRUE; +} + +/** + Register a supported PE/COFF image with the emulator. After this call + completes successfully, the PE/COFF image may be started as usual, and + it is the responsibility of the emulator implementation that any branch + into the code section of the image (including returns from functions called + from the foreign code) is executed as if it were running on the machine + type it was built for. + + @param[in] This This pointer for + EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL structure + @param[in] ImageBase The base address in memory of the PE/COFF image + @param[in] ImageSize The size in memory of the PE/COFF image + @param[in,out] EntryPoint The entry point of the PE/COFF image. Passed by + reference so that the emulator may modify it. + + @retval EFI_SUCCESS The image was registered with the emulator and + can be started as usual. + @retval other The image could not be registered. + + If the PE/COFF machine type or image type are not supported by the emulator, + then ASSERT(). +**/ +EFI_STATUS +EFIAPI +EbcRegisterImage ( + IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS ImageBase, + IN UINT64 ImageSize, + IN OUT EFI_IMAGE_ENTRY_POINT *EntryPoint + ) +{ + DEBUG_CODE_BEGIN (); + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + EFI_STATUS Status; + + ZeroMem (&ImageContext, sizeof (ImageContext)); + + ImageContext.Handle = (VOID *)(UINTN)ImageBase; + ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory; + + Status = PeCoffLoaderGetImageInfo (&ImageContext); + if (EFI_ERROR (Status)) { + return Status; + } + + ASSERT (ImageContext.Machine == EFI_IMAGE_MACHINE_EBC); + ASSERT (ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION || + ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER); + DEBUG_CODE_END (); + + EbcRegisterICacheFlush (NULL, + (EBC_ICACHE_FLUSH)InvalidateInstructionCacheRange); + + return EbcCreateThunk (NULL, (VOID *)(UINTN)ImageBase, *EntryPoint, + (VOID **)EntryPoint); +} + +/** + Unregister a PE/COFF image that has been registered with the emulator. + This should be done before the image is unloaded from memory. + + @param[in] This This pointer for EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL + structure + @param[in] ImageBase The base address in memory of the PE/COFF image + + @retval EFI_SUCCESS The image was unregistered with the emulator. + @retval other Image could not be unloaded. +**/ +EFI_STATUS +EFIAPI +EbcUnregisterImage ( + IN EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS ImageBase + ) +{ + return EbcUnloadImage (NULL, (VOID *)(UINTN)ImageBase); +} + +EDKII_PECOFF_IMAGE_EMULATOR_PROTOCOL mPeCoffEmuProtocol = { + EbcIsImageSupported, + EbcRegisterImage, + EbcUnregisterImage +}; /** Initializes the VM EFI interface. Allocates memory for the VM interface @@ -449,6 +566,16 @@ InitializeEbcDriver ( } } + Status = gBS->InstallProtocolInterface ( + &ImageHandle, + &gEdkiiPeCoffImageEmulatorProtocolGuid, + EFI_NATIVE_INTERFACE, + &mPeCoffEmuProtocol + ); + if (EFI_ERROR(Status)) { + goto ErrorExit; + } + Status = InitEBCStack(); if (EFI_ERROR(Status)) { goto ErrorExit; diff --git a/MdeModulePkg/Universal/EbcDxe/EbcInt.h b/MdeModulePkg/Universal/EbcDxe/EbcInt.h index 8aa7a4abbd63..9b25e91f951c 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcInt.h +++ b/MdeModulePkg/Universal/EbcDxe/EbcInt.h @@ -23,9 +23,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #include +#include #include +#include #include #include #include