From nobody Fri May 17 07:47:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+109019+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+109019+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1695422986; cv=none; d=zohomail.com; s=zohoarc; b=m+qdwuMxtaeC7FZuM0cH7BuSJzKFNoqQwZhjw9mIxQ/c8a6YsTZL0i7Is2dh6FFoZXCNFPny96lEVWLgurCcy31Rysvk5q56m4bTaIa6Ti2kqUFiNFRQsAU8q8jUMXo8ZmGRK/KFIQ05f43G2estwE/+bRULnFrdXiaR0UPtT30= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695422986; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=Y/lbMaFkfC5igD3yAWF+aWzx+WpUi1T29H7dQigI22I=; b=LHREHLzZCiqh1YbwikHN78Txp5fsE3pubI9q7iJ/GoKgYTswqV1slF1csW/02liUPd+CaEI6dADCqCbaAi4FU+IUgmonZcG9xQqvjk6WZfCyJn5QgYYEawPWvPq+Hab6xAfShRISLDuYSIm4Pju+eXfra0PbM+99dUdC5cUx9uA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+109019+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1695422986587799.569811170317; Fri, 22 Sep 2023 15:49:46 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=tbsBkUEjojNgJ2zRPD9sPrQUcFItFAaDb4M1Xy00zoU=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1695422986; v=1; b=xUE22mIKjsRdhmheuG2C0w32BPoALUBVvs3KAXDdvbMXoHAiDTPJaaEadR2aaM0hYAf/jaHn M+lJKPuS4ZVHlsXiyxqnkKwg0AO9niwyNeV37qUZ7aytwtlq8uGqQWyqvos2AETHyuzhRPM4iVw J8J0IMbvRXOx6NGXrxup84IU= X-Received: by 127.0.0.2 with SMTP id 1CUTYY1788612xGOlt9SyaIX; Fri, 22 Sep 2023 15:49:46 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web11.10660.1695422985204436681 for ; Fri, 22 Sep 2023 15:49:45 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="378243579" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="378243579" X-Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2023 15:49:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="747698653" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="747698653" X-Received: from nldesimo-desk.amr.corp.intel.com ([10.241.240.72]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2023 15:49:44 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Andrew Fish , Ray Ni , Michael D Kinney , Chasel Chiu Subject: [edk2-devel] [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows Date: Fri, 22 Sep 2023 15:49:23 -0700 Message-Id: <20230922224923.1978-1-nathaniel.l.desimone@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,nathaniel.l.desimone@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: UXXAiPvZvdirW3G7CfJTgWYnx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1695422987893100001 Content-Type: text/plain; charset="utf-8" The Visual Studio Windows debugger will only load symbols for PE/COFF images that Windows is aware of. Therefore, to enable source level debugging, all PEI/DXE modules must be loaded via LoadLibrary() or LoadLibraryEx() and the the instance in memory created by LoadLibrary() must be the one that is actually executed. The current source level debug implementation in EmulatorPkg for Windows is inherited from the old Nt32Pkg. This implementation makes the assumption th= at all PEI/DXE modules have a DLL export tables with a symbol named InitializeDriver. Therefore, this source level debug implementation requires all modules to be linked in a non-PI spec defined manner. Support for adding the InitializeDriver symbol was removed in EmulatorPkg, which broke source level debugging. To fix this, the source level debugging implementation has been modified to use the PE/COFF entry point directly. This brings the implementation into compliance with the PI spec and should work with any PEIM/DXE driver. Implementing this requires parsing the in-memory instance of the PE/COFF im= age created by Windows to find the entrypoint and since PEIMs/DXE drivers are n= ot garunteed to have 4KB aligned sections, it also requires explicit configura= tion of the page table using VirtualProtect(). With this fix, the debugging experience is now so good it is unprecedented! In Visual Studio Code, add the following to launch.json: { "version": "0.2.0", "configurations": [ { "name": "EmulatorPkg Launch", "type": "cppvsdbg", "request": "launch", "program": "${workspaceFolder}//Build/EmulatorX64/DEBU= G_/X64/WinHost", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}//Build/EmulatorX64/DEBUG_/X64/", "environment": [], "console": false, } ] } Make modifications to the above template as nessesary and build EmulatorPkg. Now, just add breakpoints directly in Visual Studio Code the way you would = with any other software project. When you start the debugger, it will halt at the breakpoint automatically without any extra configuration required. Cc: Andrew Fish Cc: Ray Ni Cc: Michael D Kinney Cc: Chasel Chiu Signed-off-by: Nate DeSimone Reviewed-by: Michael D Kinney --- EmulatorPkg/Win/Host/WinHost.c | 206 +++++++++++++++++++++++++++++---- 1 file changed, 182 insertions(+), 24 deletions(-) diff --git a/EmulatorPkg/Win/Host/WinHost.c b/EmulatorPkg/Win/Host/WinHost.c index 193a947fbd..e414da6c55 100644 --- a/EmulatorPkg/Win/Host/WinHost.c +++ b/EmulatorPkg/Win/Host/WinHost.c @@ -8,7 +8,7 @@ This code produces 128 K of temporary memory for the SEC stack by direct= ly allocate memory space with ReadWrite and Execute attribute. =20 -Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.
(C) Copyright 2016-2020 Hewlett Packard Enterprise Development LP
SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -977,7 +977,7 @@ AddModHandle ( for (Index =3D 0; Index < mPdbNameModHandleArraySize; Index++, Array++) { if (Array->PdbPointer =3D=3D NULL) { // - // Make a copy of the stirng and store the ModHandle + // Make a copy of the string and store the ModHandle // Handle =3D GetProcessHeap (); Size =3D AsciiStrLen (ImageContext->PdbPointer) + 1; @@ -1056,26 +1056,45 @@ RemoveModHandle ( return NULL; } =20 +typedef struct { + UINTN Base; + UINT32 Size; + UINT32 Flags; +} IMAGE_SECTION_DATA; + VOID EFIAPI PeCoffLoaderRelocateImageExtraAction ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ) { - EFI_STATUS Status; - VOID *DllEntryPoint; - CHAR16 *DllFileName; - HMODULE Library; - UINTN Index; + EFI_STATUS Status; + VOID *DllEntryPoint; + CHAR16 *DllFileName; + HMODULE Library; + UINTN Index; + PE_COFF_LOADER_IMAGE_CONTEXT PeCoffImageContext; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_SECTION_HEADER *FirstSection; + EFI_IMAGE_SECTION_HEADER *Section; + IMAGE_SECTION_DATA *SectionData; + UINTN NumberOfSections; + UINTN Base; + UINTN End; + UINTN RegionBase; + UINTN RegionSize; + UINT32 Flags; + DWORD NewProtection; + DWORD OldProtection; =20 ASSERT (ImageContext !=3D NULL); // - // If we load our own PE COFF images the Windows debugger can not source - // level debug our code. If a valid PDB pointer exists use it to load - // the *.dll file as a library using Windows* APIs. This allows - // source level debug. The image is still loaded and relocated - // in the Framework memory space like on a real system (by the code abo= ve), - // but the entry point points into the DLL loaded by the code below. + // If we load our own PE/COFF images the Windows debugger can not source + // level debug our code. If a valid PDB pointer exists use it to load + // the *.dll file as a library using Windows* APIs. This allows + // source level debug. The image is still loaded and relocated + // in the Framework memory space like on a real system (by the code abov= e), + // but the entry point points into the DLL loaded by the code below. // =20 DllEntryPoint =3D NULL; @@ -1106,27 +1125,166 @@ PeCoffLoaderRelocateImageExtraAction ( } =20 // - // Replace .PDB with .DLL on the filename + // Replace .PDB with .DLL in the filename // DllFileName[Index - 3] =3D 'D'; DllFileName[Index - 2] =3D 'L'; DllFileName[Index - 1] =3D 'L'; =20 // - // Load the .DLL file into the user process's address space for source - // level debug + // Load the .DLL file into the process's address space for source level + // debug. + // + // EFI modules use the PE32 entry point for a different purpose than + // Windows. For Windows DLLs, the PE entry point is used for the DllMa= in() + // function. DllMain() has a very specific purpose; it initializes run= time + // libraries, instance data, and thread local storage. LoadLibrary()/ + // LoadLibraryEx() will run the PE32 entry point and assume it to be a + // DllMain() implementation by default. By passing the + // DONT_RESOLVE_DLL_REFERENCES argument to LoadLibraryEx(), the execut= ion + // of the entry point as a DllMain() function will be suppressed. This + // also prevents other modules that are referenced by the DLL from bei= ng + // loaded. We use LoadLibraryEx() to create a copy of the PE32 + // image that the OS (and therefore the debugger) is aware of. + // Source level debugging is the only reason to do this. // Library =3D LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFEREN= CES); if (Library !=3D NULL) { // - // InitializeDriver is the entry point we put in all our EFI DLL's. = The - // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresse= s the - // normal DLL entry point of DllMain, and prevents other modules tha= t are - // referenced in side the DllFileName from being loaded. There is no= error - // checking as the we can point to the PE32 image loaded by Tiano. T= his - // step is only needed for source level debugging + // Parse the PE32 image loaded by the OS and find the entry point // - DllEntryPoint =3D (VOID *)(UINTN)GetProcAddress (Library, "Initializ= eDriver"); + ZeroMem (&PeCoffImageContext, sizeof (PeCoffImageContext)); + PeCoffImageContext.Handle =3D Library; + PeCoffImageContext.ImageRead =3D PeCoffLoaderImageReadFromMemory; + Status =3D PeCoffLoaderGetImageInfo (&PeCoffImageContext); + if (EFI_ERROR (Status) || (PeCoffImageContext.ImageError !=3D IMAGE_= ERROR_SUCCESS)) { + SecPrint ("DLL is not a valid PE/COFF image.\n\r"); + FreeLibrary (Library); + Library =3D NULL; + } else { + Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Library + (UINTN)Pe= CoffImageContext.PeCoffHeaderOffset); + if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HD= R32_MAGIC) { + // + // Use PE32 offset + // + DllEntryPoint =3D (VOID *) ((UINTN)Library + (UINTN)Hdr.Pe32->Op= tionalHeader.AddressOfEntryPoint); + } else { + // + // Use PE32+ offset + // + DllEntryPoint =3D (VOID *) ((UINTN)Library + (UINTN)Hdr.Pe32Plus= ->OptionalHeader.AddressOfEntryPoint); + } + // + // Now we need to configure memory access for the copy of the PE32= image + // loaded by the OS. + // + // Most Windows DLLs are linked with sections 4KB aligned but EFI + // modules are not to reduce size. Because of this we need to comp= ute + // the union of memory access attributes and explicitly configure + // each page. + // + FirstSection =3D (EFI_IMAGE_SECTION_HEADER *)( + (UINTN)Library + + PeCoffImageContext.PeCoffHeade= rOffset + + sizeof (UINT32) + + sizeof (EFI_IMAGE_FILE_HEADER)= + + Hdr.Pe32->FileHeader.SizeOfOpt= ionalHeader + ); + NumberOfSections =3D (UINTN)(Hdr.Pe32->FileHeader.NumberOfSections= ); + Section =3D FirstSection; + SectionData =3D malloc (NumberOfSections * sizeof (IMAGE_SECTION_D= ATA)); + if (SectionData =3D=3D NULL) { + FreeLibrary (Library); + Library =3D NULL; + DllEntryPoint =3D NULL; + } + ZeroMem (SectionData, NumberOfSections * sizeof (IMAGE_SECTION_DAT= A)); + // + // Extract the section data from the PE32 image + // + for (Index =3D 0; Index < NumberOfSections; Index++) { + SectionData[Index].Base =3D (UINTN)Library + Section->VirtualAdd= ress; + SectionData[Index].Size =3D Section->Misc.VirtualSize; + if (SectionData[Index].Size =3D=3D 0) { + SectionData[Index].Size =3D Section->SizeOfRawData; + } + SectionData[Index].Flags =3D (Section->Characteristics & + (EFI_IMAGE_SCN_MEM_EXECUTE | EFI_IMA= GE_SCN_MEM_WRITE)); + Section +=3D 1; + } + // + // Loop over every DWORD in memory and compute the union of the me= mory + // access bits. + // + End =3D (UINTN)Library + (UINTN)PeCoffImageContext.ImageSize; + RegionBase =3D (UINTN)Library; + RegionSize =3D 0; + Flags =3D 0; + for (Base =3D (UINTN)Library + sizeof (UINT32); Base < End; Base += =3D sizeof (UINT32)) { + for (Index =3D 0; Index < NumberOfSections; Index++) { + if (SectionData[Index].Base <=3D Base && + (SectionData[Index].Base + SectionData[Index].Size) > Base= ) { + Flags |=3D SectionData[Index].Flags; + } + } + // + // When a new page is reached configure the memory access for the + // previous page. + // + if (Base % SIZE_4KB =3D=3D 0) { + RegionSize +=3D SIZE_4KB; + if ((Flags & EFI_IMAGE_SCN_MEM_WRITE) =3D=3D EFI_IMAGE_SCN_MEM= _WRITE) { + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D EFI_IMAGE_SCN= _MEM_EXECUTE) { + NewProtection =3D PAGE_EXECUTE_READWRITE; + } else { + NewProtection =3D PAGE_READWRITE; + } + } else { + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D EFI_IMAGE_SCN= _MEM_EXECUTE) { + NewProtection =3D PAGE_EXECUTE_READ; + } else { + NewProtection =3D PAGE_READONLY; + } + } + if (!VirtualProtect ((LPVOID)RegionBase, (SIZE_T) RegionSize, = NewProtection, &OldProtection)) { + SecPrint ("Setting PE32 Section Access Failed\n\r"); + FreeLibrary (Library); + free (SectionData); + Library =3D NULL; + DllEntryPoint =3D NULL; + break; + } + Flags =3D 0; + RegionBase =3D Base; + RegionSize =3D 0; + } + } + free (SectionData); + // + // Configure the last partial page + // + if (Library !=3D NULL && (End - RegionBase) > 0) { + if ((Flags & EFI_IMAGE_SCN_MEM_WRITE) =3D=3D EFI_IMAGE_SCN_MEM_W= RITE) { + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D EFI_IMAGE_SCN_M= EM_EXECUTE) { + NewProtection =3D PAGE_EXECUTE_READWRITE; + } else { + NewProtection =3D PAGE_READWRITE; + } + } else { + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D EFI_IMAGE_SCN_M= EM_EXECUTE) { + NewProtection =3D PAGE_EXECUTE_READ; + } else { + NewProtection =3D PAGE_READONLY; + } + } + if (!VirtualProtect ((LPVOID)RegionBase, (SIZE_T) (End - RegionB= ase), NewProtection, &OldProtection)) { + SecPrint ("Setting PE32 Section Access Failed\n\r"); + FreeLibrary (Library); + Library =3D NULL; + DllEntryPoint =3D NULL; + } + } + } } =20 if ((Library !=3D NULL) && (DllEntryPoint !=3D NULL)) { @@ -1142,7 +1300,7 @@ PeCoffLoaderRelocateImageExtraAction ( // This DLL is not already loaded, so source level debugging is su= pported. // ImageContext->EntryPoint =3D (EFI_PHYSICAL_ADDRESS)(UINTN)DllEntry= Point; - SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_RE= FERENCES)\n\r", DllFileName); + SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_RE= FERENCES) @ 0x%X\n\r", DllFileName, (int) (UINTN) Library); } } else { SecPrint ("WARNING: No source level debug %S. \n\r", DllFileName); --=20 2.39.2.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#109019): https://edk2.groups.io/g/devel/message/109019 Mute This Topic: https://groups.io/mt/101531560/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-