From nobody Fri Apr 26 02:15:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1514522468345508.5817302043125; Thu, 28 Dec 2017 20:41:08 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 738AA22280C3B; Thu, 28 Dec 2017 20:36:09 -0800 (PST) Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 58FDE22280C21 for ; Thu, 28 Dec 2017 20:36:07 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 8A5C5C791B7; Fri, 29 Dec 2017 04:41:04 +0000 (UTC) Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Hr8gSbi5EpoR; Fri, 29 Dec 2017 04:41:02 +0000 (UTC) Received: from thor.domain.name (unknown [177.97.125.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id 92DE2C790F4; Fri, 29 Dec 2017 04:41:00 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac 92DE2C790F4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1514522462; bh=OHTtZ2gU7puCqR36KmP1VTQZIbTuRdknFhq6qVugmNM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=KJV0iGBODl7yt6ju/4C47kQEBgSdBGlhbmqUC2WcUNdMjnWVy9WvgA20vffdneaIl fGfFgOnvqqAAZKIBmolEGzouO5lvLZinRegCijTRnF7Y01kiFfV5M04VzurvapMTz4 PZtevBUAHfIkd7ESY9/HRMAbrmT7/2Sm7r2zLWW8= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Fri, 29 Dec 2017 02:39:34 -0200 Message-Id: X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v4 1/6] UefiCpuPkg/CpuExceptionHandlerLib/X64: Add stack trace support X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch adds stack trace support during a X64 CPU exception. It will dump out back trace, stack contents as well as image module names that were part of the call stack. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 376= +++++++++++++++++++- 1 file changed, 374 insertions(+), 2 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 1dcf4277de..19bfaa329a 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -14,6 +14,11 @@ =20 #include "CpuExceptionCommon.h" =20 +// +// Unknown PDB file name +// +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mUnknownPdbFileName =3D "????"; + /** Return address map of exception handler template so that C code can gene= rate exception tables. @@ -398,6 +403,357 @@ DumpCpuContext ( ); } =20 +/** + Get absolute path and file name of PDB file in PE/COFF image. + + @param[in] ImageBase Base address of PE/COFF image. + @param[out] PdbAbsoluteFilePath Absolute path of PDB file. + @param[out] PdbFileName File name of PDB file. +**/ +STATIC +VOID +GetPdbFileName ( + IN UINTN ImageBase, + OUT CHAR8 **PdbAbsoluteFilePath, + OUT CHAR8 **PdbFileName + ) +{ + VOID *PdbPointer; + CHAR8 *Str; + + // + // Get PDB file name from PE/COFF image + // + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)ImageBase); + if (PdbPointer =3D=3D NULL) { + // + // No PDB file name found. Set it to an unknown file name. + // + *PdbFileName =3D (CHAR8 *)mUnknownPdbFileName; + if (PdbAbsoluteFilePath !=3D NULL) { + *PdbAbsoluteFilePath =3D NULL; + } + } else { + // + // Get file name portion out of PDB file in PE/COFF image + // + Str =3D (CHAR8 *)((UINTN)PdbPointer + + AsciiStrLen ((CHAR8 *)PdbPointer) - sizeof *Str); + for (; *Str !=3D '/' && *Str !=3D '\\'; Str--) { + ; + } + + // + // Set PDB file name (also skip trailing path separator: '/' or '\\') + // + *PdbFileName =3D Str + 1; + + if (PdbAbsoluteFilePath !=3D NULL) { + // + // Set absolute file path of PDB file + // + *PdbAbsoluteFilePath =3D PdbPointer; + } + } +} + +/** + Dump stack contents. + + @param[in] CurrentRsp Current stack pointer address. + @param[in] UnwoundStacksCount Count of unwound stack frames. +**/ +STATIC +VOID +DumpStackContents ( + IN UINT64 CurrentRsp, + IN INTN UnwoundStacksCount + ) +{ + // + // Check for proper stack pointer alignment + // + if (((UINTN)CurrentRsp & (CPU_STACK_ALIGNMENT - 1)) !=3D 0) { + InternalPrintMessage ("!!!! Unaligned stack pointer. !!!!\n"); + return; + } + + // + // Dump out stack contents + // + InternalPrintMessage ("\nStack dump:\n"); + while (UnwoundStacksCount-- > 0) { + InternalPrintMessage ( + "0x%016lx: %016lx %016lx\n", + CurrentRsp, + *(UINT64 *)CurrentRsp, + *(UINT64 *)((UINTN)CurrentRsp + 8) + ); + + // + // Point to next stack + // + CurrentRsp +=3D CPU_STACK_ALIGNMENT; + } +} + +/** + Dump all image module names from call stack. + + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. +**/ +STATIC +VOID +DumpImageModuleNames ( + IN EFI_SYSTEM_CONTEXT SystemContext + ) +{ + EFI_STATUS Status; + UINT64 Rip; + UINTN ImageBase; + VOID *EntryPoint; + CHAR8 *PdbAbsoluteFilePath; + CHAR8 *PdbFileName; + UINT64 Rbp; + UINTN LastImageBase; + + // + // Set current RIP address + // + Rip =3D SystemContext.SystemContextX64->Rip; + + // + // Set current frame pointer address + // + Rbp =3D SystemContext.SystemContextX64->Rbp; + + // + // Check for proper frame pointer alignment + // + if (((UINTN)Rbp & (CPU_STACK_ALIGNMENT - 1)) !=3D 0) { + InternalPrintMessage ("!!!! Unaligned frame pointer. !!!!\n"); + return; + } + + // + // Get initial PE/COFF image base address from current RIP + // + ImageBase =3D PeCoffSearchImageBase (Rip); + if (ImageBase =3D=3D 0) { + InternalPrintMessage ("!!!! Could not find image module names. !!!!"); + return; + } + + // + // Set last PE/COFF image base address + // + LastImageBase =3D ImageBase; + + // + // Get initial PE/COFF image's entry point + // + Status =3D PeCoffLoaderGetEntryPoint ((VOID *)ImageBase, &EntryPoint); + if (EFI_ERROR (Status)) { + EntryPoint =3D NULL; + } + + // + // Get file name and absolute path of initial PDB file + // + GetPdbFileName (ImageBase, &PdbAbsoluteFilePath, &PdbFileName); + + // + // Print out initial image module name (if any) + // + if (PdbAbsoluteFilePath !=3D NULL) { + InternalPrintMessage ( + "\n%a (ImageBase=3D0x%016lx, EntryPoint=3D0x%016lx):\n", + PdbFileName, + ImageBase, + (UINTN)EntryPoint + ); + InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); + } + + // + // Walk through call stack and find next module names + // + for (;;) { + // + // Set RIP with return address from current stack frame + // + Rip =3D *(UINT64 *)((UINTN)Rbp + 8); + + // + // If RIP is zero, then stop unwinding the stack + // + if (Rip =3D=3D 0) { + break; + } + + // + // Search for the respective PE/COFF image based on RIP + // + ImageBase =3D PeCoffSearchImageBase (Rip); + if (ImageBase =3D=3D 0) { + // + // Stop stack trace + // + break; + } + + // + // If RIP points to another PE/COFF image, then find its respective PD= B file + // name. + // + if (LastImageBase !=3D ImageBase) { + // + // Get PE/COFF image's entry point + // + Status =3D PeCoffLoaderGetEntryPoint ((VOID *)ImageBase, &EntryPoint= ); + if (EFI_ERROR (Status)) { + EntryPoint =3D NULL; + } + + // + // Get file name and absolute path of PDB file + // + GetPdbFileName (ImageBase, &PdbAbsoluteFilePath, &PdbFileName); + + // + // Print out image module name (if any) + // + if (PdbAbsoluteFilePath !=3D NULL) { + InternalPrintMessage ( + "%a (ImageBase=3D0x%016lx, EntryPoint=3D0x%016lx):\n", + PdbFileName, + ImageBase, + (UINTN)EntryPoint + ); + InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); + } + + // + // Save last PE/COFF image base address + // + LastImageBase =3D ImageBase; + } + + // + // Unwind the stack + // + Rbp =3D *(UINT64 *)(UINTN)Rbp; + } +} + +/** + Dump stack trace. + + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @param[out] UnwoundStacksCount Count of unwound stack frames. +**/ +STATIC +VOID +DumpStackTrace ( + IN EFI_SYSTEM_CONTEXT SystemContext, + OUT INTN *UnwoundStacksCount + ) +{ + UINT64 Rip; + UINT64 Rbp; + UINTN ImageBase; + CHAR8 *PdbFileName; + + // + // Set current RIP address + // + Rip =3D SystemContext.SystemContextX64->Rip; + + // + // Set current frame pointer address + // + Rbp =3D SystemContext.SystemContextX64->Rbp; + + // + // Get initial PE/COFF image base address from current RIP + // + ImageBase =3D PeCoffSearchImageBase (Rip); + if (ImageBase =3D=3D 0) { + InternalPrintMessage ("!!!! Could not find backtrace information. !!!!= "); + return; + } + + // + // Get PDB file name from initial PE/COFF image + // + GetPdbFileName (ImageBase, NULL, &PdbFileName); + + // + // Initialize count of unwound stacks + // + *UnwoundStacksCount =3D 1; + + // + // Print out back trace + // + InternalPrintMessage ("\nCall trace:\n"); + + for (;;) { + // + // Print stack frame in the following format: + // + // # @ + (RBP) in [ | ????] + // + InternalPrintMessage ( + "%d 0x%016lx @ 0x%016lx+0x%x (0x%016lx) in %a\n", + *UnwoundStacksCount - 1, + Rip, + ImageBase, + Rip - ImageBase - 1, + Rbp, + PdbFileName + ); + + // + // Set RIP with return address from current stack frame + // + Rip =3D *(UINT64 *)((UINTN)Rbp + 8); + + // + // If RIP is zero, then stop unwinding the stack + // + if (Rip =3D=3D 0) { + break; + } + + // + // Search for the respective PE/COFF image based on RIP + // + ImageBase =3D PeCoffSearchImageBase (Rip); + if (ImageBase =3D=3D 0) { + // + // Stop stack trace + // + break; + } + + // + // Get PDB file name + // + GetPdbFileName (ImageBase, NULL, &PdbFileName); + + // + // Unwind the stack + // + Rbp =3D *(UINT64 *)(UINTN)Rbp; + + // + // Increment count of unwound stacks + // + (*UnwoundStacksCount)++; + } +} + /** Display CPU information. =20 @@ -410,9 +766,25 @@ DumpImageAndCpuContent ( IN EFI_SYSTEM_CONTEXT SystemContext ) { + INTN UnwoundStacksCount; + + // + // Dump CPU context + // DumpCpuContext (ExceptionType, SystemContext); + + // + // Dump stack trace + // + DumpStackTrace (SystemContext, &UnwoundStacksCount); + + // + // Dump image module names + // + DumpImageModuleNames (SystemContext); + // - // Dump module image base and module entry point by RIP + // Dump stack contents // - DumpModuleImageInfo (SystemContext.SystemContextX64->Rip); + DumpStackContents (SystemContext.SystemContextX64->Rsp, UnwoundStacksCou= nt); } --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri Apr 26 02:15:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 151452247030821.450429448197724; Thu, 28 Dec 2017 20:41:10 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id CDE2522280C3D; Thu, 28 Dec 2017 20:36:11 -0800 (PST) Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 7B02C22280C3D for ; Thu, 28 Dec 2017 20:36:09 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 4F42DC78911; Fri, 29 Dec 2017 04:41:06 +0000 (UTC) Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fPAgrvrMKx_G; Fri, 29 Dec 2017 04:41:04 +0000 (UTC) Received: from thor.domain.name (unknown [177.97.125.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id 883D0C791AD; Fri, 29 Dec 2017 04:41:02 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac 883D0C791AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1514522464; bh=7tegzzs/X4XjKwL03uWZdTIxxQA9Znv0oV+kDfg//RQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=M7jbCKSsguKehwOw+4nx4+qcM/GbESVLKn+FPgS//f4Xx1RlpFeSGKVXk/Nw5JmgT u9l+ScseuA+R0w2pd54GDfFp32s+1iopPwbEy5GkQNlAJ7FxdD88MS1LptGn8ypdZx utWhG8Oad1DpOXcuUu30dPX7SBKUZTh68kQtaJVE= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Fri, 29 Dec 2017 02:39:35 -0200 Message-Id: <9d5c44bae9ec6fccd7501d847baef185bd1e71a6.1514517573.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v4 2/6] UefiCpuPkg/CpuExceptionHandlerLib: Export GetPdbFileName() X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This function will be used by both IA32 and X64 exception handling in order to print out image module names during stack unwinding. Cc: Eric Dong Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c | 60 = +++++++++++++++++++- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h | 14 = +++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 59 = ------------------- 3 files changed, 73 insertions(+), 60 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index dbfaae1d30..f62ab8c48c 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -54,6 +54,11 @@ CONST CHAR8 *mExceptionNameStr[] =3D { =20 #define EXCEPTION_KNOWN_NAME_NUM (sizeof (mExceptionNameStr) / sizeof (CH= AR8 *)) =20 +// +// Unknown PDB file name +// +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mUnknownPdbFileName =3D "????"; + /** Get ASCII format string exception name by exception type. =20 @@ -177,4 +182,57 @@ ReadAndVerifyVectorInfo ( VectorInfo ++; } return EFI_SUCCESS; -} \ No newline at end of file +} + +/** + Get absolute path and file name of PDB file in PE/COFF image. + + @param[in] ImageBase Base address of PE/COFF image. + @param[out] PdbAbsoluteFilePath Absolute path of PDB file. + @param[out] PdbFileName File name of PDB file. +**/ +VOID +GetPdbFileName ( + IN UINTN ImageBase, + OUT CHAR8 **PdbAbsoluteFilePath, + OUT CHAR8 **PdbFileName + ) +{ + VOID *PdbPointer; + CHAR8 *Str; + + // + // Get PDB file name from PE/COFF image + // + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)ImageBase); + if (PdbPointer =3D=3D NULL) { + // + // No PDB file name found. Set it to an unknown file name. + // + *PdbFileName =3D (CHAR8 *)mUnknownPdbFileName; + if (PdbAbsoluteFilePath !=3D NULL) { + *PdbAbsoluteFilePath =3D NULL; + } + } else { + // + // Get file name portion out of PDB file in PE/COFF image + // + Str =3D (CHAR8 *)((UINTN)PdbPointer + + AsciiStrLen ((CHAR8 *)PdbPointer) - sizeof *Str); + for (; *Str !=3D '/' && *Str !=3D '\\'; Str--) { + ; + } + + // + // Set PDB file name (also skip trailing path separator: '/' or '\\') + // + *PdbFileName =3D Str + 1; + + if (PdbAbsoluteFilePath !=3D NULL) { + // + // Set absolute file path of PDB file + // + *PdbAbsoluteFilePath =3D PdbPointer; + } + } +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index e10d9379d5..64c7094513 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -327,5 +327,19 @@ AsmGetTssTemplateMap ( OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap ); =20 +/** + Get absolute path and file name of PDB file in PE/COFF image. + + @param[in] ImageBase Base address of PE/COFF image. + @param[out] PdbAbsoluteFilePath Absolute path of PDB file. + @param[out] PdbFileName File name of PDB file. +**/ +VOID +GetPdbFileName ( + IN UINTN ImageBase, + OUT CHAR8 **PdbAbsoluteFilePath, + OUT CHAR8 **PdbFileName + ); + #endif =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 19bfaa329a..d3a3878b3d 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -14,11 +14,6 @@ =20 #include "CpuExceptionCommon.h" =20 -// -// Unknown PDB file name -// -GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mUnknownPdbFileName =3D "????"; - /** Return address map of exception handler template so that C code can gene= rate exception tables. @@ -403,60 +398,6 @@ DumpCpuContext ( ); } =20 -/** - Get absolute path and file name of PDB file in PE/COFF image. - - @param[in] ImageBase Base address of PE/COFF image. - @param[out] PdbAbsoluteFilePath Absolute path of PDB file. - @param[out] PdbFileName File name of PDB file. -**/ -STATIC -VOID -GetPdbFileName ( - IN UINTN ImageBase, - OUT CHAR8 **PdbAbsoluteFilePath, - OUT CHAR8 **PdbFileName - ) -{ - VOID *PdbPointer; - CHAR8 *Str; - - // - // Get PDB file name from PE/COFF image - // - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)ImageBase); - if (PdbPointer =3D=3D NULL) { - // - // No PDB file name found. Set it to an unknown file name. - // - *PdbFileName =3D (CHAR8 *)mUnknownPdbFileName; - if (PdbAbsoluteFilePath !=3D NULL) { - *PdbAbsoluteFilePath =3D NULL; - } - } else { - // - // Get file name portion out of PDB file in PE/COFF image - // - Str =3D (CHAR8 *)((UINTN)PdbPointer + - AsciiStrLen ((CHAR8 *)PdbPointer) - sizeof *Str); - for (; *Str !=3D '/' && *Str !=3D '\\'; Str--) { - ; - } - - // - // Set PDB file name (also skip trailing path separator: '/' or '\\') - // - *PdbFileName =3D Str + 1; - - if (PdbAbsoluteFilePath !=3D NULL) { - // - // Set absolute file path of PDB file - // - *PdbAbsoluteFilePath =3D PdbPointer; - } - } -} - /** Dump stack contents. =20 --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri Apr 26 02:15:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1514522472393988.7200707848635; Thu, 28 Dec 2017 20:41:12 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 43FE422280C45; Thu, 28 Dec 2017 20:36:13 -0800 (PST) Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 01B0E22280C41 for ; Thu, 28 Dec 2017 20:36:11 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id A775DC78911; Fri, 29 Dec 2017 04:41:08 +0000 (UTC) Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id M5Tmx2AUFr-a; Fri, 29 Dec 2017 04:41:06 +0000 (UTC) Received: from thor.domain.name (unknown [177.97.125.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id AB90EC790F4; Fri, 29 Dec 2017 04:41:04 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac AB90EC790F4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1514522466; bh=OstrFoPpxLjZtvKXmjHyfKOW66Dk0iO4lqS7sTRyijk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=bOGffQVHjRrEKXc7Ljcv/v+DRdVT0TFTFOLmv9egir8WODkgYT+NQ9tBLr66AeIZ1 V6c+XuYyIrddPan7tn0xIzSNicVTXFX0wogg+nj0xYqdJ0rlbDI3Yn0KrZHDmJx3Ul X1n/3VVGw9uxE2IZctW7fxDWLMhUlkaTZhycYz+M= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Fri, 29 Dec 2017 02:39:36 -0200 Message-Id: <4dcd7c7fe381d7693c5c8185a8734fcc1be1905c.1514517573.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v4 3/6] UefiCpuPkg/CpuExceptionHandlerLib/Ia32: Add stack trace support X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch adds stack trace support during a IA32 CPU exception. It will dump out back trace, stack contents as well as image module names that were part of the call stack. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c | 4= 2 --- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h | 1= 1 - UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c | 31= 0 +++++++++++++++++++- 3 files changed, 308 insertions(+), 55 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index f62ab8c48c..867c5c01d6 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -109,48 +109,6 @@ InternalPrintMessage ( SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer)); } =20 -/** - Find and display image base address and return image base and its entry = point. - - @param CurrentEip Current instruction pointer. - -**/ -VOID -DumpModuleImageInfo ( - IN UINTN CurrentEip - ) -{ - EFI_STATUS Status; - UINTN Pe32Data; - VOID *PdbPointer; - VOID *EntryPoint; - - Pe32Data =3D PeCoffSearchImageBase (CurrentEip); - if (Pe32Data =3D=3D 0) { - InternalPrintMessage ("!!!! Can't find image information. !!!!\n"); - } else { - // - // Find Image Base entry point - // - Status =3D PeCoffLoaderGetEntryPoint ((VOID *) Pe32Data, &EntryPoint); - if (EFI_ERROR (Status)) { - EntryPoint =3D NULL; - } - InternalPrintMessage ("!!!! Find image "); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *) Pe32Data); - if (PdbPointer !=3D NULL) { - InternalPrintMessage ("%a", PdbPointer); - } else { - InternalPrintMessage ("(No PDB) " ); - } - InternalPrintMessage ( - " (ImageBase=3D%016lp, EntryPoint=3D%016p) !!!!\n", - (VOID *) Pe32Data, - EntryPoint - ); - } -} - /** Read and save reserved vector information =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index 64c7094513..ec46c2d9d3 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -130,17 +130,6 @@ InternalPrintMessage ( ... ); =20 -/** - Find and display image base address and return image base and its entry = point. - - @param CurrentEip Current instruction pointer. - -**/ -VOID -DumpModuleImageInfo ( - IN UINTN CurrentEip - ); - /** Display CPU information. =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHa= ndler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandl= er.c index 6ac8549839..25e02fbbc1 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -398,6 +398,296 @@ DumpCpuContext ( ); } =20 +/** + Dump stack trace. + + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @param[out] UnwoundStacksCount Count of unwound stack frames. +**/ +STATIC +VOID +DumpStackTrace ( + IN EFI_SYSTEM_CONTEXT SystemContext, + OUT INTN *UnwoundStacksCount + ) +{ + UINT32 Eip; + UINT32 Ebp; + UINTN ImageBase; + CHAR8 *PdbFileName; + + // + // Set current EIP address + // + Eip =3D SystemContext.SystemContextIa32->Eip; + + // + // Set current frame pointer address + // + Ebp =3D SystemContext.SystemContextIa32->Ebp; + + // + // Check for proper frame pointer alignment + // + if (((UINTN)Ebp & (CPU_STACK_ALIGNMENT - 1)) !=3D 0) { + InternalPrintMessage ("!!!! Unaligned frame pointer. !!!!\n"); + return; + } + + // + // Get initial PE/COFF image base address from current EIP + // + ImageBase =3D PeCoffSearchImageBase (Eip); + if (ImageBase =3D=3D 0) { + InternalPrintMessage ("!!!! Could not find backtrace information. !!!!= "); + return; + } + + // + // Get PDB file name from initial PE/COFF image + // + GetPdbFileName (ImageBase, NULL, &PdbFileName); + + // + // Initialize count of unwound stacks + // + *UnwoundStacksCount =3D 1; + + // + // Print out back trace + // + InternalPrintMessage ("\nCall trace:\n"); + + for (;;) { + // + // Print stack frame in the following format: + // + // # @ + (EBP) in [ | ????] + // + InternalPrintMessage ( + "%d 0x%08x @ 0x%08x+0x%x (0x%08x) in %a\n", + *UnwoundStacksCount - 1, + Eip, + ImageBase, + Eip - ImageBase - 1, + Ebp, + PdbFileName + ); + + // + // Set EIP with return address from current stack frame + // + Eip =3D *(UINT32 *)((UINTN)Ebp + 4); + + // + // If EIP is zero, then stop unwinding the stack + // + if (Eip =3D=3D 0) { + break; + } + + // + // Search for the respective PE/COFF image based on EIP + // + ImageBase =3D PeCoffSearchImageBase (Eip); + if (ImageBase =3D=3D 0) { + // + // Stop stack trace + // + break; + } + + // + // Get PDB file name + // + GetPdbFileName (ImageBase, NULL, &PdbFileName); + + // + // Unwind the stack + // + Ebp =3D *(UINT32 *)(UINTN)Ebp; + + // + // Increment count of unwound stacks + // + (*UnwoundStacksCount)++; + } +} + +/** + Dump all image module names from call stack. + + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. +**/ +STATIC +VOID +DumpImageModuleNames ( + IN EFI_SYSTEM_CONTEXT SystemContext + ) +{ + EFI_STATUS Status; + UINT32 Eip; + UINT32 Ebp; + UINTN ImageBase; + VOID *EntryPoint; + CHAR8 *PdbAbsoluteFilePath; + CHAR8 *PdbFileName; + UINTN LastImageBase; + + // + // Set current EIP address + // + Eip =3D SystemContext.SystemContextIa32->Eip; + + // + // Set current frame pointer address + // + Ebp =3D SystemContext.SystemContextIa32->Ebp; + + // + // Get initial PE/COFF image base address from current EIP + // + ImageBase =3D PeCoffSearchImageBase (Eip); + if (ImageBase =3D=3D 0) { + InternalPrintMessage ("!!!! Could not find image module names. !!!!"); + return; + } + + // + // Set last PE/COFF image base address + // + LastImageBase =3D ImageBase; + + // + // Get initial PE/COFF image's entry point + // + Status =3D PeCoffLoaderGetEntryPoint ((VOID *)ImageBase, &EntryPoint); + if (EFI_ERROR (Status)) { + EntryPoint =3D NULL; + } + + // + // Get file name and absolute path of initial PDB file + // + GetPdbFileName (ImageBase, &PdbAbsoluteFilePath, &PdbFileName); + + // + // Print out initial image module name (if any) + // + if (PdbAbsoluteFilePath !=3D NULL) { + InternalPrintMessage ( + "\n%a (ImageBase=3D0x%08x, EntryPoint=3D0x%08x):\n", + PdbFileName, + ImageBase, + (UINTN)EntryPoint + ); + InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); + } + + // + // Walk through call stack and find next module names + // + for (;;) { + // + // Set EIP with return address from current stack frame + // + Eip =3D *(UINT32 *)((UINTN)Ebp + 4); + + // + // Search for the respective PE/COFF image based on Eip + // + ImageBase =3D PeCoffSearchImageBase (Eip); + if (ImageBase =3D=3D 0) { + // + // Stop stack trace + // + break; + } + + // + // If EIP points to another PE/COFF image, then find its respective PD= B file + // name. + // + if (LastImageBase !=3D ImageBase) { + // + // Get PE/COFF image's entry point + // + Status =3D PeCoffLoaderGetEntryPoint ((VOID *)ImageBase, &EntryPoint= ); + if (EFI_ERROR (Status)) { + EntryPoint =3D NULL; + } + + // + // Get file name and absolute path of PDB file + // + GetPdbFileName (ImageBase, &PdbAbsoluteFilePath, &PdbFileName); + + // + // Print out image module name (if any) + // + if (PdbAbsoluteFilePath !=3D NULL) { + InternalPrintMessage ( + "%a (ImageBase=3D0x%08x, EntryPoint=3D0x%08x):\n", + PdbFileName, + ImageBase, + (UINTN)EntryPoint + ); + InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); + } + + // + // Save last PE/COFF image base address + // + LastImageBase =3D ImageBase; + } + + // + // Unwind the stack + // + Ebp =3D *(UINT32 *)(UINTN)Ebp; + } +} + +/** + Dump stack contents. + + @param[in] CurrentEsp Current stack pointer address. + @param[in] UnwoundStacksCount Count of unwound stack frames. +**/ +STATIC +VOID +DumpStackContents ( + IN UINT32 CurrentEsp, + IN INTN UnwoundStacksCount + ) +{ + // + // Check for proper stack alignment + // + if (((UINTN)CurrentEsp & (CPU_STACK_ALIGNMENT - 1)) !=3D 0) { + InternalPrintMessage ("!!!! Unaligned stack pointer. !!!!\n"); + return; + } + + // + // Dump out stack contents + // + InternalPrintMessage ("\nStack dump:\n"); + while (UnwoundStacksCount-- > 0) { + InternalPrintMessage ( + "0x%08x: %08x %08x\n", + CurrentEsp, + *(UINT32 *)CurrentEsp, + *(UINT32 *)((UINTN)CurrentEsp + 4) + ); + + // + // Point to next stack + // + CurrentEsp +=3D CPU_STACK_ALIGNMENT; + } +} + /** Display CPU information. =20 @@ -410,9 +700,25 @@ DumpImageAndCpuContent ( IN EFI_SYSTEM_CONTEXT SystemContext ) { + INTN UnwoundStacksCount; + + // + // Dump CPU context + // DumpCpuContext (ExceptionType, SystemContext); + + // + // Dump stack trace + // + DumpStackTrace (SystemContext, &UnwoundStacksCount); + + // + // Dump image module names + // + DumpImageModuleNames (SystemContext); + // - // Dump module image base and module entry point by EIP + // Dump stack contents // - DumpModuleImageInfo (SystemContext.SystemContextIa32->Eip); + DumpStackContents (SystemContext.SystemContextIa32->Esp, UnwoundStacksCo= unt); } --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri Apr 26 02:15:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1514522475095525.0335590014059; Thu, 28 Dec 2017 20:41:15 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A6CA322280C47; Thu, 28 Dec 2017 20:36:16 -0800 (PST) Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 269B422280C21 for ; Thu, 28 Dec 2017 20:36:15 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id F25F1C78911; Fri, 29 Dec 2017 04:41:11 +0000 (UTC) Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id x0COjER3X4iN; Fri, 29 Dec 2017 04:41:08 +0000 (UTC) Received: from thor.domain.name (unknown [177.97.125.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id CF548C791AD; Fri, 29 Dec 2017 04:41:06 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac CF548C791AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1514522468; bh=TTy1l4MbtMiSFpcHnKCfVbuCiL4P5b7nqeKDRQBZoXc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=lxhW0B+q4QWKqScfpyun5ok0ZsllUydG8esB1kddmj4kBzlcjPxhrHkS/MRiwE2YO OYq6LQnhrZHuwTLw0Cgv2/1hyQFWwrUw1ZhJvwnInJO9HFiq3JmovdOyi/Z/nQVhFr lTony2G0Yht9VhAJhDdL5RK3kIVenK6E6H24yIEc= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Fri, 29 Dec 2017 02:39:37 -0200 Message-Id: <32f06077006939f71560970f6abcbbb2062ea5c3.1514517573.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v4 4/6] UefiCpuPkg/CpuExceptionHandlerLib: Add helper to valid memory addresses X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Introduce IsLinearAddressValid() function that will be used for validating memory addresses that would get dereferenced during stack traces in IA32 and X64 CPU exceptions. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Requested-by: Brian Johnson Requested-by: Jiewen Yao Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c | 382 +++++= +++++++++++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h | 16 + 2 files changed, 398 insertions(+) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index 867c5c01d6..52b3eb1463 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -14,6 +14,9 @@ =20 #include "CpuExceptionCommon.h" =20 +#include +#include + // // Error code flag indicating whether or not an error code will be // pushed on the stack if an exception occurs. @@ -194,3 +197,382 @@ GetPdbFileName ( } } } + +/** + Check if a linear address is valid by walking the page tables in 4-level + paging mode. + + @param[in] Cr3 CR3 control register. + @param[in] MaxPhyAddrBits MAXPHYADDRBITS bits. + @param[in] LinearAddress Linear address to be checked. +**/ +STATIC +BOOLEAN +Do4LevelPagingModeCheck ( + IN UINTN Cr3, + IN UINT8 MaxPhyAddrBits, + IN UINTN LinearAddress + ) +{ + UINT64 PhysicalAddress; + UINT64 *Pml4TableEntry; + UINT64 *PageDirPtrTableEntry; + UINT64 *PageDirEntry; + UINT64 *PageTableEntry; + + // + // In 4-level paging mode, linear addresses are 48 bits wide + // + if ((UINT64)LinearAddress > (1ULL << 48) - 1) { + return FALSE; + } + + // + // Calculate physical address of PML4E + // + PhysicalAddress =3D (UINT64)Cr3 & (((1ULL << MaxPhyAddrBits) - 1) << 12); + PhysicalAddress |=3D (((UINT64)LinearAddress >> 39) & 0x1FF) << 3; + + ASSERT ((PhysicalAddress & (sizeof (*Pml4TableEntry) - 1)) =3D=3D 0); + + Pml4TableEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check if a PDPTE is present + // + if ((*Pml4TableEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + // + // Calculate physical address of PDPTE + // + PhysicalAddress =3D *Pml4TableEntry & (((1ULL << MaxPhyAddrBits) - 1) <<= 12); + PhysicalAddress |=3D (((UINT64)LinearAddress >> 30) & 0x1FF) << 3; + + ASSERT ((PhysicalAddress & (sizeof (*PageDirPtrTableEntry) - 1)) =3D=3D = 0); + + PageDirPtrTableEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check whether a PDPTE or 1GiB page entry is present + // + if ((*PageDirPtrTableEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDPTE maps an 1GiB page + // + if ((*PageDirPtrTableEntry & BIT7) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of PDE + // + PhysicalAddress =3D *PageDirPtrTableEntry & (((1ULL << MaxPhyAddrBits) -= 1) << + 12); + PhysicalAddress |=3D (((UINT64)LinearAddress >> 21) & 0x1FF) << 3; + + ASSERT ((PhysicalAddress & (sizeof (*PageDirEntry) - 1)) =3D=3D 0); + + PageDirEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check whether a PDE or a 2MiB page entry is present + // + if ((*PageDirEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDE maps a 2MiB page + // + if ((*PageDirEntry & BIT7) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of PTE + // + PhysicalAddress =3D *PageDirEntry & (((1ULL << MaxPhyAddrBits) - 1) << 1= 2); + PhysicalAddress |=3D (((UINT64)LinearAddress >> 12) & 0x1FF) << 3; + + ASSERT ((PhysicalAddress & (sizeof (*PageTableEntry) - 1)) =3D=3D 0); + + PageTableEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check if PTE maps a 4KiB page + // + if ((*PageTableEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + return TRUE; +} + +/** + Check if a linear address is valid by walking the page tables in 32-bit = paging + mode. + + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddress Linear address to be checked. +**/ +STATIC +BOOLEAN +Do32BitPagingModeCheck ( + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddress + ) +{ + UINT64 PhysicalAddress; + UINT32 *PageDirEntry; + UINT32 *PageTableEntry; + + if (LinearAddress > MAX_UINT32) { + return FALSE; + } + + // + // Calculate physical address of PDE + // + PhysicalAddress =3D (UINT32)Cr3 & (((1ULL << 20) - 1) << 12); + PhysicalAddress |=3D (((UINT32)LinearAddress >> 22) & 0x3FF) << 2; + + ASSERT ((PhysicalAddress & (sizeof (*PageDirEntry) - 1)) =3D=3D 0); + + PageDirEntry =3D (UINT32 *)(UINTN)PhysicalAddress; + + // + // Check whether a PTE or a 4MiB page is present + // + if ((*PageDirEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDE maps a 4MiB page + // + if ((Cr4 & BIT4) !=3D 0 && (*PageDirEntry & BIT7) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of PTE + // + PhysicalAddress =3D *PageDirEntry & (((1ULL << 20) - 1) << 12); + PhysicalAddress |=3D (((UINT32)LinearAddress >> 12) & 0x3FF) << 2; + + ASSERT ((PhysicalAddress & (sizeof (*PageTableEntry) - 1)) =3D=3D 0); + + PageTableEntry =3D (UINT32 *)(UINTN)PhysicalAddress; + + // + // Check if PTE maps a 4KiB page + // + if ((*PageTableEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + return TRUE; +} + +/** + Check if a linear address is valid by walking the page tables in PAE pag= ing + mode. + + @param[in] Cr3 CR3 control register. + @param[in] MaxPhyAddrBits MAXPHYADDRBITS bits. + @param[in] LinearAddress Linear address to be checked. +**/ +STATIC +BOOLEAN +DoPAEPagingModeCheck ( + IN UINTN Cr3, + IN UINT8 MaxPhyAddrBits, + IN UINTN LinearAddress + ) +{ + UINT64 PhysicalAddress; + UINT64 *PageDirPtrTableEntry; + UINT64 *PageDirEntry; + UINT64 *PageTableEntry; + + if (LinearAddress > MAX_UINT32) { + return FALSE; + } + + // + // Calculate physical address of PDPTE + // + PhysicalAddress =3D (UINT32)Cr3 >> 5; + + // + // Select PDPTE register + // + PhysicalAddress +=3D + ((UINT32)LinearAddress >> 30) * sizeof (*PageDirPtrTableEntry); + + PageDirPtrTableEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check if PDE is present + // + if ((*PageDirPtrTableEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + PhysicalAddress =3D *PageDirPtrTableEntry & (((1ULL << MaxPhyAddrBits) -= 1) << + 12); + PhysicalAddress |=3D ((LinearAddress >> 21) & 0x1FF) << 3; + ASSERT ((PhysicalAddress & (sizeof (*PageDirEntry) - 1)) =3D=3D 0); + + PageDirEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check whether a PTE or a 2MiB page is present + // + if ((*PageDirEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDE maps a 2MiB page + // + if ((*PageDirEntry & BIT7) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of PTE + // + PhysicalAddress =3D *PageDirEntry & (((1ULL << MaxPhyAddrBits) - 1) << 1= 2); + PhysicalAddress |=3D ((LinearAddress >> 12) & 0x1FF) << 3; + ASSERT ((PhysicalAddress & (sizeof (*PageTableEntry) - 1)) =3D=3D 0); + + PageTableEntry =3D (UINT64 *)(UINTN)PhysicalAddress; + + // + // Check if PTE maps a 4KiB page + // + if ((*PageTableEntry & BIT0) =3D=3D 0) { + return FALSE; + } + + return TRUE; +} + +/** + Check if a linear address is valid. + + @param[in] Cr0 CR0 control register. + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddress Linear address to be checked. +**/ +BOOLEAN +IsLinearAddressValid ( + IN UINTN Cr0, + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddress + ) +{ + UINT32 Eax; + UINT32 Edx; + UINT8 MaxPhyAddrBits; + MSR_IA32_EFER_REGISTER Msr; + BOOLEAN AddressValid; + + // + // Check for valid input parameters + // + if (Cr0 =3D=3D 0 || Cr4 =3D=3D 0 || LinearAddress =3D=3D 0) { + return FALSE; + } + + // + // Check if paging is disabled + // + if ((Cr0 & BIT31) =3D=3D 0) { + // + // If CR4.PAE bit is set, then the linear (or physical) address suppor= ts + // only up to 36 bits. + // + if (((Cr4 & BIT5) !=3D 0 && (UINT64)LinearAddress > 0xFFFFFFFFFULL) || + LinearAddress > 0xFFFFFFFF) { + return FALSE; + } + + return TRUE; + } + + // + // Paging can be enabled only if CR0.PE bit is set + // + if ((Cr0 & BIT0) =3D=3D 0) { + return FALSE; + } + + // + // CR3 register cannot be zero if paging is enabled + // + if (Cr3 =3D=3D 0) { + return FALSE; + } + + // + // Get MAXPHYADDR bits + // + AsmCpuid (0x80000000, &Eax, NULL, NULL, NULL); + if (Eax >=3D 0x80000008) { + AsmCpuid (0x80000008, &Eax, NULL, NULL, NULL); + MaxPhyAddrBits =3D (UINT8)Eax; + } else { + AsmCpuid (1, NULL, NULL, NULL, &Edx); + if ((Edx & BIT6) !=3D 0) { + MaxPhyAddrBits =3D 36; + } else { + MaxPhyAddrBits =3D 32; + } + } + + ASSERT (MaxPhyAddrBits > 0); + + AddressValid =3D FALSE; + + // + // check if CR4.PAE bit is not set + // + if ((Cr4 & BIT5) =3D=3D 0) { + // + // Check if linear address is valid in 32-bit paging mode + // + AddressValid =3D Do32BitPagingModeCheck (Cr3, Cr4, LinearAddress); + } else { + if (MaxPhyAddrBits > 52) { + return FALSE; + } + + Msr.Uint64 =3D AsmReadMsr64 (MSR_IA32_EFER); + + if (Msr.Bits.LME =3D=3D 0) { + // + // Check if linear address is valid in PAE paging mode + // + AddressValid =3D DoPAEPagingModeCheck (Cr3, MaxPhyAddrBits, LinearAd= dress); + } else { + // + // Check if linear address is valid in 4-level paging mode + // + AddressValid =3D Do4LevelPagingModeCheck (Cr3, MaxPhyAddrBits, + LinearAddress); + } + } + + return AddressValid; +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index ec46c2d9d3..1b51034c25 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -330,5 +330,21 @@ GetPdbFileName ( OUT CHAR8 **PdbFileName ); =20 +/** + Check if a linear address is valid. + + @param[in] Cr0 CR0 control register. + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddress Linear address to be checked. +**/ +BOOLEAN +IsLinearAddressValid ( + IN UINTN Cr0, + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddress + ); + #endif =20 --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri Apr 26 02:15:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1514522478540290.9911283613369; Thu, 28 Dec 2017 20:41:18 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1F9E322280C41; Thu, 28 Dec 2017 20:36:20 -0800 (PST) Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B25F922280C21 for ; Thu, 28 Dec 2017 20:36:17 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 6488AC791AD; Fri, 29 Dec 2017 04:41:14 +0000 (UTC) Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lb4anycn0Z8g; Fri, 29 Dec 2017 04:41:12 +0000 (UTC) Received: from thor.domain.name (unknown [177.97.125.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id 8F716C791B7; Fri, 29 Dec 2017 04:41:08 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac 8F716C791B7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1514522470; bh=CebxLJ5Mg2YY5MCsLBfSrXFGe5U6GYBB9ypWeyOHzSI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=eoOcMwDu2cwEsaJIZhqzt0NZ6CaAoTU9kGp3apm4+v92g5qI5xxyIMYgJD5MSNlAN u+Yc2YBiTBwvd9BWImQX0dxS8hmrdRCRxfcb7ErTbRlUq700fMXT3leU/UxyPPut+N ypZl3HT3qa2KCcpHJBs+1vLU+XBZ3MYQPAh+4hMo= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Fri, 29 Dec 2017 02:39:38 -0200 Message-Id: <836f7f2205e91c16d7c427c5b6e127f4a4dfa62e.1514517573.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v4 5/6] UefiCpuPkg/CpuExceptionHandlerLib: Ensure valid frame/stack pointers X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Validate all possible memory dereferences during stack traces in IA32 and X64 CPU exceptions. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Requested-by: Brian Johnson Requested-by: Jiewen Yao Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c | 14= 3 +++++++++++++++++++- UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 7= 5 +++++++++- 2 files changed, 210 insertions(+), 8 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHa= ndler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandl= er.c index 25e02fbbc1..9b52d4f6d2 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -398,6 +398,96 @@ DumpCpuContext ( ); } =20 +/** + Check if a logical address is valid. + + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @param[in] SegmentSelector Segment selector. + @param[in] Offset Offset or logical address. +**/ +STATIC +BOOLEAN +IsLogicalAddressValid ( + IN EFI_SYSTEM_CONTEXT SystemContext, + IN UINT16 SegmentSelector, + IN UINTN Offset + ) +{ + IA32_SEGMENT_DESCRIPTOR *SegmentDescriptor; + UINT32 SegDescBase; + UINT32 SegDescLimit; + UINTN SegDescLimitInBytes; + + // + // Check for valid input parameters + // + if (SegmentSelector =3D=3D 0 || Offset =3D=3D 0) { + return FALSE; + } + + // + // Check whether to look for a segment descriptor in GDT or LDT table + // + if ((SegmentSelector & BIT2) =3D=3D 0) { + // + // Get segment descriptor from GDT table + // + SegmentDescriptor =3D + (IA32_SEGMENT_DESCRIPTOR *)( + (UINTN)SystemContext.SystemContextIa32->Gdtr[0] + + ((SegmentSelector >> 3) * 8) + ); + } else { + // + // Get segment descriptor from LDT table + // + SegmentDescriptor =3D + (IA32_SEGMENT_DESCRIPTOR *)( + (UINTN)SystemContext.SystemContextIa32->Ldtr + + ((SegmentSelector >> 3) * 8) + ); + } + + // + // Get segment descriptor's base address + // + SegDescBase =3D SegmentDescriptor->Bits.BaseLow | + (SegmentDescriptor->Bits.BaseMid << 16) | + (SegmentDescriptor->Bits.BaseHigh << 24); + + // + // Get segment descriptor's limit + // + SegDescLimit =3D SegmentDescriptor->Bits.LimitLow | + (SegmentDescriptor->Bits.LimitHigh << 16); + + // + // Calculate segment descriptor's limit in bytes + // + if (SegmentDescriptor->Bits.G =3D=3D 1) { + SegDescLimitInBytes =3D (UINTN)SegDescLimit * SIZE_4KB; + } else { + SegDescLimitInBytes =3D SegDescLimit; + } + + // + // Make sure to not access beyond a segment limit boundary + // + if (Offset + SegDescBase > SegDescLimitInBytes) { + return FALSE; + } + + // + // Check if the translated logical address (or linear address) is valid + // + return IsLinearAddressValid ( + SystemContext.SystemContextIa32->Cr0, + SystemContext.SystemContextIa32->Cr3, + SystemContext.SystemContextIa32->Cr4, + Offset + SegDescBase + ); +} + /** Dump stack trace. =20 @@ -459,6 +549,20 @@ DumpStackTrace ( InternalPrintMessage ("\nCall trace:\n"); =20 for (;;) { + // + // Check for valid frame pointer + // + if (!IsLogicalAddressValid (SystemContext, + SystemContext.SystemContextIa32->Ss, + (UINTN)Ebp + 4) || + !IsLogicalAddressValid (SystemContext, + SystemContext.SystemContextIa32->Ss, + (UINTN)Ebp)) { + InternalPrintMessage ("%a: attempted to dereference an invalid frame= " + "pointer at 0x%08x\n", __FUNCTION__, Ebp); + break; + } + // // Print stack frame in the following format: // @@ -588,6 +692,16 @@ DumpImageModuleNames ( // Walk through call stack and find next module names // for (;;) { + if (!IsLogicalAddressValid (SystemContext, + SystemContext.SystemContextIa32->Ss, + (UINTN)Ebp) || + !IsLogicalAddressValid (SystemContext, + SystemContext.SystemContextIa32->Ss, + (UINTN)Ebp + 4)) { + InternalPrintMessage ("%a: attempted to dereference an invalid frame= " + "pointer at 0x%08x\n", __FUNCTION__, Ebp); + } + // // Set EIP with return address from current stack frame // @@ -651,16 +765,23 @@ DumpImageModuleNames ( /** Dump stack contents. =20 - @param[in] CurrentEsp Current stack pointer address. + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. @param[in] UnwoundStacksCount Count of unwound stack frames. **/ STATIC VOID DumpStackContents ( - IN UINT32 CurrentEsp, - IN INTN UnwoundStacksCount + IN EFI_SYSTEM_CONTEXT SystemContext, + IN INTN UnwoundStacksCount ) { + UINT32 CurrentEsp; + + // + // Get current stack pointer + // + CurrentEsp =3D SystemContext.SystemContextIa32->Esp; + // // Check for proper stack alignment // @@ -674,6 +795,20 @@ DumpStackContents ( // InternalPrintMessage ("\nStack dump:\n"); while (UnwoundStacksCount-- > 0) { + // + // Check for a valid stack pointer address + // + if (!IsLogicalAddressValid (SystemContext, + SystemContext.SystemContextIa32->Ss, + (UINTN)CurrentEsp) || + !IsLogicalAddressValid (SystemContext, + SystemContext.SystemContextIa32->Ss, + (UINTN)CurrentEsp + 4)) { + InternalPrintMessage ("%a: attempted to dereference an invalid stack= " + "pointer at 0x%08x\n", __FUNCTION__, CurrentEs= p); + break; + } + InternalPrintMessage ( "0x%08x: %08x %08x\n", CurrentEsp, @@ -720,5 +855,5 @@ DumpImageAndCpuContent ( // // Dump stack contents // - DumpStackContents (SystemContext.SystemContextIa32->Esp, UnwoundStacksCo= unt); + DumpStackContents (SystemContext, UnwoundStacksCount); } diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index d3a3878b3d..8067c34122 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -401,16 +401,26 @@ DumpCpuContext ( /** Dump stack contents. =20 - @param[in] CurrentRsp Current stack pointer address. + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. @param[in] UnwoundStacksCount Count of unwound stack frames. **/ STATIC VOID DumpStackContents ( - IN UINT64 CurrentRsp, - IN INTN UnwoundStacksCount + IN EFI_SYSTEM_CONTEXT SystemContext, + IN INTN UnwoundStacksCount ) { + UINT64 CurrentRsp; + UINTN Cr0; + UINTN Cr3; + UINTN Cr4; + + // + // Get current stack pointer + // + CurrentRsp =3D SystemContext.SystemContextX64->Rsp; + // // Check for proper stack pointer alignment // @@ -419,11 +429,28 @@ DumpStackContents ( return; } =20 + // + // Get system control registers + // + Cr0 =3D SystemContext.SystemContextX64->Cr0; + Cr3 =3D SystemContext.SystemContextX64->Cr3; + Cr4 =3D SystemContext.SystemContextX64->Cr4; + // // Dump out stack contents // InternalPrintMessage ("\nStack dump:\n"); while (UnwoundStacksCount-- > 0) { + // + // Check for a valid stack pointer address + // + if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)CurrentRsp) || + !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)CurrentRsp + 8)) { + InternalPrintMessage ("%a: attempted to dereference an invalid stack= " + "pointer at 0x%016lx\n", __FUNCTION__, Current= Rsp); + break; + } + InternalPrintMessage ( "0x%016lx: %016lx %016lx\n", CurrentRsp, @@ -457,6 +484,9 @@ DumpImageModuleNames ( CHAR8 *PdbFileName; UINT64 Rbp; UINTN LastImageBase; + UINTN Cr0; + UINTN Cr3; + UINTN Cr4; =20 // // Set current RIP address @@ -516,10 +546,27 @@ DumpImageModuleNames ( InternalPrintMessage ("%a\n", PdbAbsoluteFilePath); } =20 + // + // Get system control registers + // + Cr0 =3D SystemContext.SystemContextX64->Cr0; + Cr3 =3D SystemContext.SystemContextX64->Cr3; + Cr4 =3D SystemContext.SystemContextX64->Cr4; + // // Walk through call stack and find next module names // for (;;) { + // + // Check for a valid frame pointer + // + if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp + 8) || + !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp)) { + InternalPrintMessage ("%a: attempted to dereference an invalid frame= " + "pointer at 0x%016lx\n", __FUNCTION__, Rbp); + break; + } + // // Set RIP with return address from current stack frame // @@ -604,6 +651,9 @@ DumpStackTrace ( UINT64 Rbp; UINTN ImageBase; CHAR8 *PdbFileName; + UINTN Cr0; + UINTN Cr3; + UINTN Cr4; =20 // // Set current RIP address @@ -634,12 +684,29 @@ DumpStackTrace ( // *UnwoundStacksCount =3D 1; =20 + // + // Get system control registers + // + Cr0 =3D SystemContext.SystemContextX64->Cr0; + Cr3 =3D SystemContext.SystemContextX64->Cr3; + Cr4 =3D SystemContext.SystemContextX64->Cr4; + // // Print out back trace // InternalPrintMessage ("\nCall trace:\n"); =20 for (;;) { + // + // Check for valid frame pointer + // + if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp + 8) || + !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp)) { + InternalPrintMessage ("%a: attempted to dereference an invalid frame= " + "pointer at 0x%016lx\n", __FUNCTION__, Rbp); + break; + } + // // Print stack frame in the following format: // @@ -727,5 +794,5 @@ DumpImageAndCpuContent ( // // Dump stack contents // - DumpStackContents (SystemContext.SystemContextX64->Rsp, UnwoundStacksCou= nt); + DumpStackContents (SystemContext, UnwoundStacksCount); } --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Fri Apr 26 02:15:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1514522481147957.5118351344875; Thu, 28 Dec 2017 20:41:21 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 75FDC22280C50; Thu, 28 Dec 2017 20:36:21 -0800 (PST) Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id CAD2422280C21 for ; Thu, 28 Dec 2017 20:36:18 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 8BFC5C791B7; Fri, 29 Dec 2017 04:41:15 +0000 (UTC) Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7-y-6WAr_ahF; Fri, 29 Dec 2017 04:41:14 +0000 (UTC) Received: from thor.domain.name (unknown [177.97.125.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id CF9C8C790F4; Fri, 29 Dec 2017 04:41:10 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac CF9C8C790F4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1514522472; bh=/NvTOTxscAuq0mmvFZkUrGLPTETuS/w9o3/o3EndU5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=FsQxBQ0rUp8L2yBY5iXH2I6Xbub/Zaehyk0d+6Tug/QGCLrNj9wCCV7bIHfXnelHl EBcy+3eA+tmwE4aiUStsQytALNMEug5FHgbA0kDBj2ZGxIkdORdvqJQ4QlWqzfW3Kn DHuPIaqi0VTLxf3ldqlyIYJ6PaEv9HS8rlNRkX40= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Fri, 29 Dec 2017 02:39:39 -0200 Message-Id: <7405ac1dcf60165ad4b044a140b5c7c2ed10902c.1514517573.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v4 6/6] UefiCpuPkg/CpuExceptionHandlerLib: Correctly print IP addresses X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Remove the supurious '- 1' when calculating the IP addresses during the stack traces. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Requested-by: Jeff Fan Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c | 2 = +- UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 2 = +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHa= ndler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandl= er.c index 9b52d4f6d2..e2a3341286 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -573,7 +573,7 @@ DumpStackTrace ( *UnwoundStacksCount - 1, Eip, ImageBase, - Eip - ImageBase - 1, + Eip - ImageBase, Ebp, PdbFileName ); diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 8067c34122..5ae3aa4e73 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -717,7 +717,7 @@ DumpStackTrace ( *UnwoundStacksCount - 1, Rip, ImageBase, - Rip - ImageBase - 1, + Rip - ImageBase, Rbp, PdbFileName ); --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel