From patchwork Mon Apr 1 03:27:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 161515 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp4874665jan; Sun, 31 Mar 2019 20:27:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqzcJmZ+8aBm5O+fphiHTM9c2+e237w9sm5Y4Vwl+sCtkVEphy2qILqV+nOdjK6CuO37NNbe X-Received: by 2002:a17:902:728e:: with SMTP id d14mr59542137pll.327.1554089260107; Sun, 31 Mar 2019 20:27:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554089260; cv=none; d=google.com; s=arc-20160816; b=DtKTF8Kr4/pv7yWG+ftnZuIYIU2yquudmkcvY/gC/xlZqeZv/NzUgDVxzFO1DuDBRS Cs8AGk1XWyQeoX+grWr1aQnWj0xjCZmS41LspVg17fFS8Lzu9uJOQuBtvUiiho2y6Voy ej6ex9e2RPzHN1/KW93s3h6nJc6svo9y2vhLKHnFw8ogDzty/YtP2GpBFS/PSqUNLZZO Nj3RVnlFzPZO1q4F21oZJfjDVqUIFIPf/XDcDX+L1Unyd/+tshSARV3yg4qxBK8YGkQB QLzPZC2M2FTPZAPFllIU2bDmAUjl8M94eBU6l1fhyocg2g/XjVgT3DS8yU8lnUjV3YZy QG+g== 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=LRm6gByYJG2gobAWbSyqo4T/lFOEYkTMwhoYRqhFwwQ=; b=t6GBB/+xErnp26J79RLJ9ERHmJohKM131+QC8nvv+mmc0RyuXrL5NYE4Xd86FMTdG5 luQTz/P0+paThSg3egro/fWWV/w1lrBHe3+wo9YsmT820/kKgIIEyh/Dx+xqqeMc92tx Abchnb5oWa1MwATXOGFTSHWgyNaWqE3U9DE40qjCgUKGi6g64FXCsUOPRS8zCb94epXr kxa1klMnfl19/Dxtb13L9rI5obTvkoHh3+zwLrN1uEhfFj82e/f1xU3G3eepOYasezq6 8XOwOX2zVIwfyPfM9wCSfX/n8uDh0rJOllCxOH9PFmJWcnpuSRQrkgWGlMqRsXuB5ORp 791g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b="l8/7/ciX"; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 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. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id f131si7948116pfc.92.2019.03.31.20.27.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Mar 2019 20:27:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b="l8/7/ciX"; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 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 B1CBA211EABD2; Sun, 31 Mar 2019 20:27:39 -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::441; helo=mail-pf1-x441.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) (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 98F41211E011B for ; Sun, 31 Mar 2019 20:27:38 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id b3so3824693pfd.1 for ; Sun, 31 Mar 2019 20:27:38 -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=6PFcI+JfAhwfsNi3wGhsqZzZwydtIb10w5ieK+xmkjQ=; b=l8/7/ciXjjh2+GeXXqwhv2K0pFRLehg2MKLw7BJ3Xj3vtT6k89sLHG1yo6shXyIt7D nAXLU6Bd+vsqav3Q1jNOw8xN9pIbiXwJFT3XbmTrCwLRszLNEttBOuriIRaxggWzIAJG 0MKDZ8jcMYUbWHR3Yacbr7uc5YduSGcAv2hdsPL87oO2WAoo/ZdebuUlUaEB8vbDeV6s t8ou2UipGG18ErFJPDQJSgUCaHZ8cjhVFhLqqz+NF9qIp7w5Po76kemAdzU2MM3c14W0 rEI7Fk1v+Z6StLfPPQBVGDe6AOEkWrmmaiXQNULfGlT+qEMj0Ai/bH4E65/avWJcPLVA +HJQ== 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=6PFcI+JfAhwfsNi3wGhsqZzZwydtIb10w5ieK+xmkjQ=; b=hPQrmTLnc1O1RUzFeI7BUQFAgZ74dqx+QnzSHAK8nEIpuwK9vcjkthmGuBbAU5MPfh smfd7HQ1XRptDApso8bukLqLWkQHXFYghr6CTcbiQBF+G4mmvC15ULfOU6q51X92D8XK VHYoTiHRNtThs7dyb/cDgTQTXG8x9a6mcWOIRADT3/Eq6+VExl31Nhsi7lcDsaZVUsRQ fxdXV6AkPtuj5URaO7AAyHMckK2RosnBsJtldpvJsIEo+D8toZkIX5FGVFB6G74a47CW BzQQyBrwCb48AsHBM/8VllfgAoW8J1nmcee5MuieE31P4dgndXxySOFhtdBiGt/txcI4 rUbw== X-Gm-Message-State: APjAAAU6gfeB3HrnINOP1EH07xd5XVFWgsvSn8gTwiO4eBMXOEV+T96H bc26rcJY1MHakfTWLdne2uXa5HPrYaxuUA== X-Received: by 2002:aa7:8190:: with SMTP id g16mr33119268pfi.92.1554089257571; Sun, 31 Mar 2019 20:27:37 -0700 (PDT) Received: from mba13.imgcgcw.net ([147.50.13.10]) by smtp.gmail.com with ESMTPSA id 16sm12758092pfz.106.2019.03.31.20.27.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Mar 2019 20:27:36 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Mon, 1 Apr 2019 10:27:07 +0700 Message-Id: <20190401032709.14787-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190401032709.14787-1-ard.biesheuvel@linaro.org> References: <20190401032709.14787-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v4 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 , 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/EbcDebugger.inf | 3 + MdeModulePkg/Universal/EbcDxe/EbcDxe.inf | 3 + MdeModulePkg/Universal/EbcDxe/EbcInt.c | 123 ++++++++++++++++++++ MdeModulePkg/Universal/EbcDxe/EbcInt.h | 3 + 4 files changed, 132 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/EbcDebugger.inf b/MdeModulePkg/Universal/EbcDxe/EbcDebugger.inf index 8f293f5c7c29..c7a9d519b080 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcDebugger.inf +++ b/MdeModulePkg/Universal/EbcDxe/EbcDebugger.inf @@ -89,6 +89,8 @@ BaseMemoryLib DebugLib BaseLib + CacheMaintenanceLib + PeCoffLib [Protocols] gEfiDebugSupportProtocolGuid ## PRODUCES @@ -98,6 +100,7 @@ gEfiEbcSimpleDebuggerProtocolGuid ## SOMETIMES_CONSUMES gEfiPciRootBridgeIoProtocolGuid ## SOMETIMES_CONSUMES gEfiSimpleFileSystemProtocolGuid ## SOMETIMES_CONSUMES + gEdkiiPeCoffImageEmulatorProtocolGuid ## PRODUCES [Guids] gEfiFileInfoGuid ## SOMETIMES_CONSUMES ## GUID diff --git a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf index d6ee6194a0c8..ecccf2c57ffe 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf +++ b/MdeModulePkg/Universal/EbcDxe/EbcDxe.inf @@ -57,7 +57,9 @@ MdeModulePkg/MdeModulePkg.dec [LibraryClasses] + CacheMaintenanceLib MemoryAllocationLib + PeCoffLib UefiBootServicesTableLib BaseMemoryLib UefiDriverEntryPoint @@ -68,6 +70,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..051eb0aaa07b 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcInt.c +++ b/MdeModulePkg/Universal/EbcDxe/EbcInt.c @@ -349,6 +349,119 @@ 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] 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 ImageType, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL + ) +{ + 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, + EDKII_PECOFF_IMAGE_EMULATOR_VERSION, + EFI_IMAGE_MACHINE_EBC +}; /** Initializes the VM EFI interface. Allocates memory for the VM interface @@ -449,6 +562,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