From patchwork Sat Sep 15 13:28:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 146773 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp1805144ljw; Sat, 15 Sep 2018 06:29:20 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbabZy00/WWiCE5qYNJCqyMZvepb/JAJXW7ypKyJDJoZgabqztJS7bH72Xe7ztQeKjCcB60 X-Received: by 2002:a17:902:6f10:: with SMTP id w16-v6mr16636684plk.216.1537018160790; Sat, 15 Sep 2018 06:29:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537018160; cv=none; d=google.com; s=arc-20160816; b=zQ88FwclMWqUqwICkDHeTwn53u+xYGSI+71WzAzr2BDu2XOipMpiYgmoAtGltuGcC2 9V4WbDXlFzuN91sqkYHMmUeL+u7T2t9aQ9pkevElobXqNpKQNnDQmu0XnBhgoDRn2nrM XCM0vMAfDr/ztAp2GTOigCWsxEaTqevX8YnzVC6gy5pCG2rmzAuFKcGug83TvF5NKjEL hl1ae09qZt6WE52RjKXGOz26aZi2mI7Ho5uGTdwQpn31CRn49KyVWIoV8dyOl7slaEks gobImN1kO7qQwv8eubNDNcYS827IWiqvk2bFK3FH8Zu2xmllPHE3fxqAQG40yx67ZcPi BgtQ== 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=GZUQ79W3Yy9rW9TT5VbllSffbwHmo1Sk3Fe0VkO8+oo=; b=Ll8oNPjJGLlQMSpeJBdfpt3/8zJmiaX5OcttkFZLYPKoeb+xPDAWbgdk2XSVAkTAKi AGNG6HERZbzBWOd+OF7FuEEtoR+yqy0x4WzaFcHydinJyrAR1/lMPpz8LPOuzeypDYVu LDTQgEvU7QXNIwGeMUYnCANFJ43nr7641UNuQkf6giJdtmxI16TV3FTJ/MyymvnvsBPt UiAKmwBZ9gsNfyydHJ03bzf4wLkvf9PxP7wRVqJ0mTy8fW9xvoVR0eHewFXPIdKezHT2 R6XZsqpZC4GMJ3rxmdATGsXKzPsjWCtusymlOaSwBjHDv9ey75Vx2ZWIXJ+m4LDnQ0HM FY5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=KOXNBTqA; 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 e37-v6si9997785plb.313.2018.09.15.06.29.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Sep 2018 06:29:20 -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=KOXNBTqA; 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 B7EBB211350F1; Sat, 15 Sep 2018 06:29:15 -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=2a00:1450:4864:20::533; helo=mail-ed1-x533.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) (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 6B871211350F1 for ; Sat, 15 Sep 2018 06:29:14 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id z27-v6so9488176edb.10 for ; Sat, 15 Sep 2018 06:29:14 -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=nj4K3NwqmcW9kLr0mTuGyiKIhHw8TkGtlSCfGV+GoTY=; b=KOXNBTqAJb+7smSUCtFoW4SNVRrli+TQLEsYfG3kfu4REMxaRbjrPNgPpRe5UtuAlB sYmoex9GSG0/UpZXlCzVLq2zxqN+ww22llIvWYoltYbcQDtfi1rUbIxYO6jvQfrudaBb 1xSIRBuA2vHzDP97hvEJwAOQpcPH587IOiFh4= 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=nj4K3NwqmcW9kLr0mTuGyiKIhHw8TkGtlSCfGV+GoTY=; b=Dlg9JD1uniEeF7bezdAi8/U4KC1ovGN2E6pOvIvIwlPwaWpr7mTLnB8pXp8a5juGNC klYa6+xepPQYAaJBWIcjAPoqB9Gv7N5W3/q2hCw5LROed8QRxpn2r1UraGfXv3Ns1t8V uYF6wcFd6nq5ZRi8rsELYXylJMUG5vmpEozU8q1HzXvbbG48MIfdsU6rYHeytE8fze4t lDwrdKXkMpKmFe4L62uH94u+5RV45cw5qdjZzeCiuDCFNhnV88jq3vV2whbUoFze6NJM hl2yIID2qroZSgRawFO3KjupiyrFtHCGYK1gFWu4BNEN6mz2wpcLEWan1NuLl06wU5FT ba4A== X-Gm-Message-State: APzg51CIoCWptIQC9yT48N8lopPlbXPdSEnRKkc1NKaAP0QsMO+1AxX4 PTiEjzV5VLC29GNudaULFUeIjPGzUr1Yhg== X-Received: by 2002:aa7:c708:: with SMTP id i8-v6mr28223255edq.231.1537018152577; Sat, 15 Sep 2018 06:29:12 -0700 (PDT) Received: from mba13.kuq.prv ([194.138.39.100]) by smtp.gmail.com with ESMTPSA id j23-v6sm4449262edh.29.2018.09.15.06.29.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Sep 2018 06:29:11 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Sat, 15 Sep 2018 15:28:57 +0200 Message-Id: <20180915132859.25727-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180915132859.25727-1-ard.biesheuvel@linaro.org> References: <20180915132859.25727-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v2 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 | 124 ++++++++++++++++++++ MdeModulePkg/Universal/EbcDxe/EbcInt.h | 3 + 3 files changed, 130 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..fbd9003565be 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcInt.c +++ b/MdeModulePkg/Universal/EbcDxe/EbcInt.c @@ -349,6 +349,120 @@ 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. + + @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 + ) +{ + 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 +563,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