From nobody Mon Dec 23 13:27:02 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 151597584722753.700867889423535; Sun, 14 Jan 2018 16:24:07 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id E3492222DDBEA; Sun, 14 Jan 2018 16:18:45 -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 D33F1222DDBE3 for ; Sun, 14 Jan 2018 16:18:42 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id DAA06C08891; Mon, 15 Jan 2018 00:23:55 +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 ieG1rWPY3v13; Mon, 15 Jan 2018 00:23:53 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 784FBC78F41; Mon, 15 Jan 2018 00:23:51 +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 784FBC78F41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975833; bh=f8i6sucGEbRNKBrN2JadLYIswJyF3Ck4LqzRlG/IgUU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=GWqocJR6JDct3k2kEca1qlhYaLaM1P+HhdZ2MUlpz/4/8+nt84a5bvKimOezVHAba 4X8Bhb6dhLIdACjhvxkb6yYdK6kVsHxH3zq7p39Xf9P3ZT8RjE2Wf4csU/uJKtLqLo CHHAC+URVY8Rw+X/HLM0tOqsfzfAfiM+fiNDN1tg= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:29 -0200 Message-Id: <076e74849f88219927d8c53dde61ff66477df094.1515974582.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 1/8] 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 | 401= +++++++++++++++++++- 1 file changed, 393 insertions(+), 8 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 56180f4c17..4db9f6465e 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. @@ -399,20 +404,281 @@ DumpCpuContext ( } =20 /** - Display CPU information. + Get absolute path and file name of PDB file in PE/COFF image. =20 - @param ExceptionType Exception type. - @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @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 -DumpImageAndCpuContent ( +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] ExceptionType Exception type. + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. +**/ +STATIC +VOID +DumpImageModuleNames ( IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext ) { - DumpCpuContext (ExceptionType, SystemContext); + EFI_STATUS Status; + UINT64 Rip; + UINTN ImageBase; + VOID *EntryPoint; + CHAR8 *PdbAbsoluteFilePath; + CHAR8 *PdbFileName; + UINT64 Rbp; + UINTN LastImageBase; + + // + // Set current RIP address + // + if ((ExceptionType =3D=3D EXCEPT_IA32_PAGE_FAULT) && + ((SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_ID) != =3D 0)) { + // + // The RIP in SystemContext could not be used + // if it is page fault with I/D set. + // + Rip =3D *(UINT64 *)(UINTN)SystemContext.SystemContextX64->Rsp; + } else { + 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] ExceptionType Exception type. + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @param[out] UnwoundStacksCount Count of unwound stack frames. +**/ +STATIC +VOID +DumpStacktrace ( + IN EFI_EXCEPTION_TYPE ExceptionType, + IN EFI_SYSTEM_CONTEXT SystemContext, + OUT INTN *UnwoundStacksCount + ) +{ + UINT64 Rip; + UINT64 Rbp; + UINTN ImageBase; + CHAR8 *PdbFileName; + // - // Dump module image base and module entry point by RIP + // Set current RIP address // if ((ExceptionType =3D=3D EXCEPT_IA32_PAGE_FAULT) && ((SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_ID) != =3D 0)) { @@ -420,8 +686,127 @@ DumpImageAndCpuContent ( // The RIP in SystemContext could not be used // if it is page fault with I/D set. // - DumpModuleImageInfo ((*(UINTN *)(UINTN)SystemContext.SystemContextX64-= >Rsp)); + Rip =3D *(UINT64 *)(UINTN)SystemContext.SystemContextX64->Rsp; } else { - DumpModuleImageInfo (SystemContext.SystemContextX64->Rip); + 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. + + @param ExceptionType Exception type. + @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. +**/ +VOID +DumpImageAndCpuContent ( + IN EFI_EXCEPTION_TYPE ExceptionType, + IN EFI_SYSTEM_CONTEXT SystemContext + ) +{ + INTN UnwoundStacksCount; + + // + // Dump CPU context + // + DumpCpuContext (ExceptionType, SystemContext); + + // + // Dump stack trace + // + DumpStacktrace (ExceptionType, SystemContext, &UnwoundStacksCount); + + // + // Dump image module names + // + DumpImageModuleNames (ExceptionType, SystemContext); + + // + // Dump stack contents + // + 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 Mon Dec 23 13:27:02 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 1515975847207439.0212135528385; Sun, 14 Jan 2018 16:24:07 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 722B5222DDBE7; Sun, 14 Jan 2018 16:18:45 -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 D144B222DDBE2 for ; Sun, 14 Jan 2018 16:18:42 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 7314CC08892; Mon, 15 Jan 2018 00:23:58 +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 YpWwF9zR64YC; Mon, 15 Jan 2018 00:23:55 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 87795C0888F; Mon, 15 Jan 2018 00:23:53 +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 87795C0888F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975835; bh=ONhcVAes+Gj4kA9MNmVvWEiwaUUgLPZy5sXR9WUVRzo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=Rrv7wmlXHHrjxej2pdzkDTVj/HFVJESYfb/fJylf+916RJtu0Qir1Ke0TDvgxeBX0 ADHJCE0s0nezgQrrZpvp0WkoiA9yTxLje5skoATqV3ICLJuYm/CWgSQOx7UEY1KKz3 tKpr9ST7nu8KmTEKxF3CZj7/PNuTguYWNNwI5x1Y= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:30 -0200 Message-Id: <0791b20a070a1e49d0d908aca8e6ddd5191ce071.1515974582.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 2/8] 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 01b0610364..d9abbd772d 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 4db9f6465e..523dce95c9 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 Mon Dec 23 13:27:02 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 151597585077915.990203778791852; Sun, 14 Jan 2018 16:24:10 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 62A0D222DDBED; Sun, 14 Jan 2018 16:18:46 -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 39AB2222DDBE5 for ; Sun, 14 Jan 2018 16:18:44 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 2E331C08893; Mon, 15 Jan 2018 00:24:01 +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 P1aQb0cpkZiu; Mon, 15 Jan 2018 00:23:58 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 AA86FC08890; Mon, 15 Jan 2018 00:23:55 +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 AA86FC08890 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975837; bh=ahvsruYTlD3CjLl3PcPmIkMlwdmY5YCFxaHi6mY0XRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=I1ky0sjSYKurKwObi9DgiPz6vA11iFWJc90g+NIgWzOZhTnzIm7c8oPP51oiQJ290 idlq3vsVElV4QLNjhOcdKBZGIPjTMokQyIBreM0SpW+mvJIU95IBs5pR0l2O9Bvsoe UO2i0MbEZSbpQQ9HB3y03bLgPuEU9AfKRDlozGLE= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:31 -0200 Message-Id: <457cabcc8075d638e5a27eb7794736cde02a6bdf.1515974582.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 3/8] 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 | 33= 5 +++++++++++++++++++- 3 files changed, 327 insertions(+), 61 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index d9abbd772d..66892320c8 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 based on IP(0x%x) ", CurrentEip= ); - 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 04f2ab593c..c5d6ea0939 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -399,20 +399,156 @@ DumpCpuContext ( } =20 /** - Display CPU information. + Dump stack trace. =20 - @param ExceptionType Exception type. - @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @param[in] ExceptionType Exception type. + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. + @param[out] UnwoundStacksCount Count of unwound stack frames. **/ +STATIC VOID -DumpImageAndCpuContent ( +DumpStacktrace ( + IN EFI_EXCEPTION_TYPE ExceptionType, + IN EFI_SYSTEM_CONTEXT SystemContext, + OUT INTN *UnwoundStacksCount + ) +{ + UINT32 Eip; + UINT32 Ebp; + UINTN ImageBase; + CHAR8 *PdbFileName; + + // + // Set current EIP address + // + if ((ExceptionType =3D=3D EXCEPT_IA32_PAGE_FAULT) && + ((SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_ID) != =3D 0)) { + // + // The EIP in SystemContext could not be used + // if it is page fault with I/D set. + // + Eip =3D *(UINT32 *)(UINTN)SystemContext.SystemContextIa32->Esp; + } else { + 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] ExceptionType Exception type. + @param[in] SystemContext Pointer to EFI_SYSTEM_CONTEXT. +**/ +STATIC +VOID +DumpImageModuleNames ( IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext ) { - DumpCpuContext (ExceptionType, SystemContext); + EFI_STATUS Status; + UINT32 Eip; + UINT32 Ebp; + UINTN ImageBase; + VOID *EntryPoint; + CHAR8 *PdbAbsoluteFilePath; + CHAR8 *PdbFileName; + UINTN LastImageBase; + // - // Dump module image base and module entry point by EIP + // Set current EIP address // if ((ExceptionType =3D=3D EXCEPT_IA32_PAGE_FAULT) && ((SystemContext.SystemContextIa32->ExceptionData & IA32_PF_EC_ID) != =3D 0)) { @@ -420,8 +556,191 @@ DumpImageAndCpuContent ( // The EIP in SystemContext could not be used // if it is page fault with I/D set. // - DumpModuleImageInfo ((*(UINTN *)(UINTN)SystemContext.SystemContextIa32= ->Esp)); + Eip =3D *(UINT32 *)(UINTN)SystemContext.SystemContextIa32->Esp; } else { - DumpModuleImageInfo (SystemContext.SystemContextIa32->Eip); + 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. + + @param ExceptionType Exception type. + @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. +**/ +VOID +DumpImageAndCpuContent ( + IN EFI_EXCEPTION_TYPE ExceptionType, + IN EFI_SYSTEM_CONTEXT SystemContext + ) +{ + INTN UnwoundStacksCount; + + // + // Dump CPU context + // + DumpCpuContext (ExceptionType, SystemContext); + + // + // Dump stack trace + // + DumpStacktrace (ExceptionType, SystemContext, &UnwoundStacksCount); + + // + // Dump image module names + // + DumpImageModuleNames (ExceptionType, SystemContext); + + // + // Dump stack contents + // + 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 Mon Dec 23 13:27:02 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 1515975854755169.38168010728361; Sun, 14 Jan 2018 16:24:14 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id CD62A222DDBF0; Sun, 14 Jan 2018 16:18:49 -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 A9ADE222DDBE5 for ; Sun, 14 Jan 2018 16:18:47 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id C7E98C08892; Mon, 15 Jan 2018 00:24: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 uE_2-nIa9KYa; Mon, 15 Jan 2018 00:24:01 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 10E6BC78F41; Mon, 15 Jan 2018 00:23:57 +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 10E6BC78F41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975839; bh=a9oWZ3avU/247GCQuP9NxA+XNjLpw7wrTq3e62HJK7M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=hDQDmvjCY4PyfNRd1Za+ztr43Ur8sPyE0OW43yY5Dg+ANpTvvbBmZ09KoO6jlKleB ti9cmy64Rn0JXophOd85XiteAv5/WdlkdI/yXoiu7dYzG9njfK+Cg20f34d8PS3Z5m oW0PtUxrBxNmBNNjmnP8IU3BKcm92bKNwa8Gfae8= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:32 -0200 Message-Id: <10dcd403b44f50ba71007b2eb7f719c47777d51e.1515974582.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 4/8] UefiCpuPkg/CpuExceptionHandlerLib: Add helper to validate 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 | 395 +++++= +++++++++++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h | 16 + 2 files changed, 411 insertions(+) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index 66892320c8..7ac13640de 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -14,6 +14,8 @@ =20 #include "CpuExceptionCommon.h" =20 +#include + // // Error code flag indicating whether or not an error code will be // pushed on the stack if an exception occurs. @@ -59,6 +61,24 @@ CONST CHAR8 *mExceptionNameStr[] =3D { // GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mUnknownPdbFileName =3D "????"; =20 +// +// IA32 virtual memory bit definitions +// +#define IA32_PG_P BIT0 +#define IA32_PG_PS BIT7 + +// +// IA32 control register bit definitions +// +#define IA32_CR0_PG BIT31 +#define IA32_CR4_PAE BIT5 +#define IA32_CR0_PE BIT0 + +// +// IA32 CPUID 01h EDX bit definitions +// +#define IA32_CPUID1_EDX_PAE BIT6 + /** Get ASCII format string exception name by exception type. =20 @@ -194,3 +214,378 @@ 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 MAXPHYADDR bits. + @param[in] LinearAddress Linear address to be checked. +**/ +STATIC +BOOLEAN +Do4LevelPagingModeCheck ( + IN UINTN Cr3, + IN UINT8 MaxPhyAddrBits, + IN UINTN LinearAddress + ) +{ + UINT64 PhysicalAddressMask; + UINTN IndexMask; + UINTN Index; + UINT64 *Pml4Table; + UINT64 *TableEntry; + UINT64 *PageDirPtrTable; + UINT64 *PageDirTable; + UINT64 *PageTable; + + // + // In 4-level paging mode, linear addresses are 48 bits wide + // + if ((UINT64)LinearAddress > 0xFFFFFFFFFFFFULL) { + return FALSE; + } + + // + // Calculate physical address mask (bits M-1:12) + // + PhysicalAddressMask =3D (LShiftU64 (1, MaxPhyAddrBits) - 1) & ~0xFFF; + // + // 9 bits for masking page table indexes out of linear addresses + // + IndexMask =3D 0x1FF; + + // + // Calculate physical address of PML4 table and index of PML4E + // + Pml4Table =3D (UINT64 *)(UINTN)((UINT64)Cr3 & PhysicalAddressMask); + Index =3D (UINTN)(RShiftU64 ((UINT64)LinearAddress, 39) & IndexMask); + + // + // Select PML4E + // + TableEntry =3D &Pml4Table[Index]; + + // + // Check if a PDPTE is present + // + if ((*TableEntry & IA32_PG_P) =3D=3D 0) { + return FALSE; + } + + // + // Calculate physical address of page-directory-pointer table and index = of + // PDPTE. + // + PageDirPtrTable =3D (UINT64 *)(UINTN)(*TableEntry & PhysicalAddressMask); + Index =3D (UINTN)(RShiftU64 ((UINT64)LinearAddress, 30) & IndexMask); + + // + // Select PDPTE + // + TableEntry =3D &PageDirPtrTable[Index]; + + // + // Check whether a PDPTE or 1GiB page entry is present + // + if ((*TableEntry & IA32_PG_P) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDPTE maps an 1GiB page + // + if ((*TableEntry & IA32_PG_PS) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of page directory table and index of PDE + // + PageDirTable =3D (UINT64 *)(UINTN)(*TableEntry & PhysicalAddressMask); + Index =3D (UINTN)(RShiftU64 ((UINT64)LinearAddress, 21) & IndexMask); + + // + // Select PDE + // + TableEntry =3D &PageDirTable[Index]; + + // + // Check whether a PDE or a 2MiB page entry is present + // + if ((*TableEntry & IA32_PG_P) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDE maps a 2MiB page + // + if ((*TableEntry & IA32_PG_PS) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of page table and index of PTE + // + PageTable =3D (UINT64 *)(UINTN)(*TableEntry & PhysicalAddressMask); + Index =3D (UINTN)(RShiftU64 ((UINT64)LinearAddress, 12) & IndexMask); + + // + // Select PTE + // + TableEntry =3D &PageTable[Index]; + + // + // Check if PTE maps a 4KiB page + // + if ((*TableEntry & IA32_PG_P) =3D=3D 0) { + return FALSE; + } + + return TRUE; +} + +/** + Check if a linear address is valid by walking the page tables in 32-bit = paging + mode. + + NOTE: Current UEFI implementations do not support IA32 non-PAE paging mo= de. + + @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 + ) +{ + InternalPrintMessage ("!!!! Unsupported IA32 non-PAE paging mode !!!!\n"= ); + return FALSE; +} + +/** + 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 MAXPHYADDR bits. + @param[in] LinearAddress Linear address to be checked. +**/ +STATIC +BOOLEAN +DoPAEPagingModeCheck ( + IN UINTN Cr3, + IN UINT8 MaxPhyAddrBits, + IN UINTN LinearAddress + ) +{ + UINT64 *PageDirPtrTable; + UINTN Index; + UINT64 *PageDirTable; + UINT64 PhysicalAddressMask; + UINTN IndexMask; + UINT64 *TableEntry; + UINT64 *PageTable; + + // + // In 32-bit PAE paging mode, linear addresses are 32 bits wide + // + if (LinearAddress > 0xFFFFFFFF) { + return FALSE; + } + + // + // Calculate physical address of page-directory-pointer table and index = of + // PDPTE register. + // + PageDirPtrTable =3D (UINT64 *)(UINTN)(Cr3 & ~0x1F); + Index =3D (UINTN)((UINT32)LinearAddress >> 30); + + // + // Select PDPTE register + // + TableEntry =3D &PageDirPtrTable[Index]; + + // + // Check if PDE is present + // + if ((*TableEntry & IA32_PG_P) =3D=3D 0) { + return FALSE; + } + + // + // Calculate physical address mask (bits M-1:12) + // + PhysicalAddressMask =3D (LShiftU64 (1, MaxPhyAddrBits) - 1) & ~0xFFF; + // + // 9 bits for masking page table indexes out of linear addresses + // + IndexMask =3D 0x1FF; + + // + // Calculate physical address of page directory table and index of PDE + // + PageDirTable =3D (UINT64 *)(UINTN)(*TableEntry & PhysicalAddressMask); + Index =3D (UINTN)(RShiftU64 ((UINT64)LinearAddress, 21) & IndexMask); + + // + // Select PDE + // + TableEntry =3D &PageDirTable[Index]; + + // + // Check whether a PTE or a 2MiB page is present + // + if ((*TableEntry & IA32_PG_P) =3D=3D 0) { + return FALSE; + } + + // + // Check if PDE maps a 2MiB page + // + if ((*TableEntry & IA32_PG_PS) !=3D 0) { + return TRUE; + } + + // + // Calculate physical address of page table and index of PTE + // + PageTable =3D (UINT64 *)(UINTN)(*TableEntry & PhysicalAddressMask); + Index =3D (UINTN)(RShiftU64 ((UINT64)LinearAddress, 12) & IndexMask); + + // + // Select PTE + // + TableEntry =3D &PageTable[Index]; + + // + // Check if PTE maps a 4KiB page + // + if ((*TableEntry & IA32_PG_P) =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 & IA32_CR0_PG) =3D=3D 0) { + // + // If CR4.PAE bit is set, then the linear (or physical) address suppor= ts + // only up to 36 bits. + // + if ((UINT64)LinearAddress > 0xFFFFFFFFFULL || + ((Cr4 & IA32_CR4_PAE) =3D=3D 0 && LinearAddress > 0xFFFFFFFF)) { + return FALSE; + } + + return TRUE; + } + + // + // Paging can be enabled only if CR0.PE bit is set + // + if ((Cr0 & IA32_CR0_PE) =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 & IA32_CPUID1_EDX_PAE) !=3D 0) { + MaxPhyAddrBits =3D 36; + } else { + MaxPhyAddrBits =3D 32; + } + } + + // + // Check if CR4.PAE bit is not set + // + if ((Cr4 & IA32_CR4_PAE) =3D=3D 0) { + // + // Check if linear address is valid in 32-bit paging mode + // + AddressValid =3D Do32BitPagingModeCheck (Cr3, Cr4, LinearAddress); + } else { + // + // In either PAE or 4-level paging mode, physical addresses can hold o= nly + // up to 52 bits. + // + if (MaxPhyAddrBits > 52) { + return FALSE; + } + + // + // Read IA32_EFER MSR register + // + Msr.Uint64 =3D AsmReadMsr64 (MSR_IA32_EFER); + + // + // Check if IA32_EFER.LME bit is not set (e.g. PAE paging mode) + // + 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 Mon Dec 23 13:27:02 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 151597585819163.39440088359834; Sun, 14 Jan 2018 16:24:18 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 4377C222DDBE5; Sun, 14 Jan 2018 16:18:51 -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 4205F222DDBE5 for ; Sun, 14 Jan 2018 16:18:50 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 56CD7C08892; Mon, 15 Jan 2018 00:24:07 +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 oNcuhf-aEiZ0; Mon, 15 Jan 2018 00:24:04 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 7BBD0C0888F; Mon, 15 Jan 2018 00:24: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 7BBD0C0888F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975841; bh=+0u0SitUTFfsMcMFH40UAXyNLeZjh9CnuXDF236fhrM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=L/6BFmE0a5XQi2F0159hP1A/TEYxxlS/SvMYgrEzI3SZPxcoCbw93AA5Y+SdPea0j TwIvF4BBLGWokry9vMLy1y6EJShL4l558lAHTfGYr7wB4VYBITKm0aDNFwJS3Ugblz oJEX8afjMpA77o4CrCnH3o15bLDCg1CF+efJeRNc= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:33 -0200 Message-Id: <5cc6968f2c67232ca29b99cd81f8ac5d754ba8dc.1515974582.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 5/8] 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= 9 +++++++++++++++++++- UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 7= 5 +++++++++- 2 files changed, 216 insertions(+), 8 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHa= ndler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandl= er.c index c5d6ea0939..3b92512b92 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -14,6 +14,11 @@ =20 #include "CpuExceptionCommon.h" =20 +// +// IA32 Segment Selector bit definitions +// +#define IA32_SEGSEL_TI BIT2 + /** Return address map of exception handler template so that C code can gene= rate exception tables. @@ -398,6 +403,97 @@ 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; + UINT64 SegDescLimitInBytes; + + // + // Check for valid input parameters + // + if (SegmentSelector =3D=3D 0 || Offset =3D=3D 0) { + return FALSE; + } + + // + // Look for a segment descriptor in a GDT or LDT table depending on TI + // (Table Indicator) bit in segment selector. + // + if ((SegmentSelector & IA32_SEGSEL_TI) =3D=3D 0) { + // + // Get segment descriptor from GDT table + // + SegmentDescriptor =3D + (IA32_SEGMENT_DESCRIPTOR *)( + (UINTN)SystemContext.SystemContextIa32->Gdtr[0] + + (SegmentSelector & ~7) + ); + } else { + // + // Get segment descriptor from LDT table + // + SegmentDescriptor =3D + (IA32_SEGMENT_DESCRIPTOR *)( + (UINTN)SystemContext.SystemContextIa32->Ldtr + + (SegmentSelector & ~7) + ); + } + + // + // 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 (UINT64)SegDescLimit * SIZE_4KB + (SIZE_4KB - = 1); + } else { + SegDescLimitInBytes =3D SegDescLimit; + } + + // + // Make sure to not access beyond a segment limit boundary + // + if ((UINT64)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 @@ -470,6 +566,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: // @@ -610,6 +720,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 // @@ -673,16 +793,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 // @@ -696,6 +823,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, @@ -742,5 +883,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 523dce95c9..c81f4c00eb 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, @@ -459,6 +486,9 @@ DumpImageModuleNames ( CHAR8 *PdbFileName; UINT64 Rbp; UINTN LastImageBase; + UINTN Cr0; + UINTN Cr3; + UINTN Cr4; =20 // // Set current RIP address @@ -527,10 +557,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 // @@ -617,6 +664,9 @@ DumpStacktrace ( UINT64 Rbp; UINTN ImageBase; CHAR8 *PdbFileName; + UINTN Cr0; + UINTN Cr3; + UINTN Cr4; =20 // // Set current RIP address @@ -656,12 +706,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: // @@ -749,5 +816,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 Mon Dec 23 13:27:02 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 1515975861408566.4226681972522; Sun, 14 Jan 2018 16:24:21 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 9F984222DDBF6; Sun, 14 Jan 2018 16:18:53 -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 CFA57222DDBE2 for ; Sun, 14 Jan 2018 16:18:51 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id E295DC0888F; Mon, 15 Jan 2018 00:24: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 TktBOVuvgCpy; Mon, 15 Jan 2018 00:24:07 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 53496C08890; Mon, 15 Jan 2018 00:24: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 53496C08890 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975843; bh=qyqpBs9/+HQVJL3SnKMv/pfmvGqqufFMJ6MYOaa0VTA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=jElQVxRMdTUFnDogybzw+SqMbhkumoa2XdxGuVuN2TgP7wLy35/EV63gJNXBUZ5sp 1jLAPnyGbzgvqjKE2/51emEMHm9eKdpaBV0WXn8r3wavwU7DWHgtIXsGo8t1C0Lzse 7LoWq/vmRAVxWH+Mk2JCGV9DPaEuLee9zJDvcM6c= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:34 -0200 Message-Id: <492123ce74345558b42bcd9905d4a1d986380f0a.1515974582.git.paulo@paulo.ac> X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 6/8] 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 3b92512b92..31fbd4a164 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -590,7 +590,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 c81f4c00eb..71d2d2f5d4 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -739,7 +739,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 From nobody Mon Dec 23 13:27:02 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 151597586446954.995773189865076; Sun, 14 Jan 2018 16:24:24 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1AD74222DDBF4; Sun, 14 Jan 2018 16:18:56 -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 205C1222DDBEF for ; Sun, 14 Jan 2018 16:18:54 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 3AA6BC0888F; Mon, 15 Jan 2018 00:24: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 KOqW0UjV4Uny; Mon, 15 Jan 2018 00:24:08 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 656BCC08891; Mon, 15 Jan 2018 00:24: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 656BCC08891 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975846; bh=Q+mwrfhWLb6d1pSB8tAKdl9SZZTuiGaQs78zX3ALDbc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=Zrps696/zD2RbCgiRkcjrzdeSULR6tAXFJJ6OYcHjQJaP5YUHwA8MmL+GyxRSKkQK rr1FUyG9Ccb6k+hqqwENZbQ1Jl9sIqSW0Fa58KVPBLQCQoymTw3FIv27OrdyKQyF8K 7EUUYWZO0nbFQVFK44wu1JpUsm6XvghkmNYcRzIo= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:35 -0200 Message-Id: X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 7/8] UefiCpuPkg/CpuExceptionHandlerLib: Validate memory address ranges 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 a new IsLinearAddressRangeValid() function to validate a given address range and check whether or not it is valid. This function is useful for validating ranges of memory addresses during stack traces in X64. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Requested-by: Brian Johnson Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c | 40 = ++++++++++++++++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h | 18 = +++++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 40 = ++++++++++++-------- 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index 7ac13640de..e1dd054259 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -589,3 +589,43 @@ IsLinearAddressValid ( =20 return AddressValid; } + +/** + Check if a linear address range is valid. + + @param[in] Cr0 CR0 control register. + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddressStart Linear address start. + @param[in] LinearAddressEnd Linear address end. +**/ +BOOLEAN +IsLinearAddressRangeValid ( + IN UINTN Cr0, + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddressStart, + IN UINTN LinearAddressEnd + ) +{ + // + // Check for valid input parameters + // + if (LinearAddressStart =3D=3D 0 || LinearAddressEnd =3D=3D 0 || + LinearAddressStart > LinearAddressEnd) { + return FALSE; + } + + // + // Validate all linear addresses within the given range + // + for (LinearAddressStart &=3D ~(SIZE_4KB - 1); + LinearAddressStart <=3D LinearAddressEnd; + LinearAddressStart +=3D SIZE_4KB) { + if (!IsLinearAddressValid (Cr0, Cr3, Cr4, LinearAddressStart)) { + return FALSE; + } + } + + return TRUE; +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index 1b51034c25..075f668290 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -346,5 +346,23 @@ IsLinearAddressValid ( IN UINTN LinearAddress ); =20 +/** + Check if a linear address range is valid. + + @param[in] Cr0 CR0 control register. + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddressStart Linear address start. + @param[in] LinearAddressEnd Linear address end. +**/ +BOOLEAN +IsLinearAddressRangeValid ( + IN UINTN Cr0, + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddressStart, + IN UINTN LinearAddressEnd + ); + #endif =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 71d2d2f5d4..4d8c9b0a89 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -415,6 +415,8 @@ DumpStackContents ( UINTN Cr0; UINTN Cr3; UINTN Cr4; + UINTN RspAddressStart; + UINTN RspAddressEnd; =20 // // Get current stack pointer @@ -436,21 +438,29 @@ DumpStackContents ( Cr3 =3D SystemContext.SystemContextX64->Cr3; Cr4 =3D SystemContext.SystemContextX64->Cr4; =20 + // + // Calculate address range of the stack pointers + // + RspAddressStart =3D (UINTN)CurrentRsp; + RspAddressEnd =3D + RspAddressStart + (UINTN)UnwoundStacksCount * CPU_STACK_ALIGNMENT; + + // + // Validate address range of stack pointers + // + if (!IsLinearAddressRangeValid (Cr0, Cr3, Cr4, RspAddressStart, + RspAddressEnd)) { + InternalPrintMessage ("%a: attempted to dereference an invalid stack " + "pointer at 0x%016lx - 0x%016lx\n", __FUNCTION__, + RspAddressStart, RspAddressEnd); + return; + } + // // 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, @@ -459,7 +469,7 @@ DumpStackContents ( ); =20 // - // Point to next stack + // Point to next stack pointer // CurrentRsp +=3D CPU_STACK_ALIGNMENT; } @@ -571,8 +581,8 @@ DumpImageModuleNames ( // // Check for a valid frame pointer // - if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp + 8) || - !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp)) { + if (!IsLinearAddressRangeValid (Cr0, Cr3, Cr4, (UINTN)Rbp, + (UINTN)Rbp + 8)) { InternalPrintMessage ("%a: attempted to dereference an invalid frame= " "pointer at 0x%016lx\n", __FUNCTION__, Rbp); break; @@ -722,8 +732,8 @@ DumpStacktrace ( // // Check for valid frame pointer // - if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp + 8) || - !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp)) { + if (!IsLinearAddressRangeValid (Cr0, Cr3, Cr4, (UINTN)Rbp, + (UINTN)Rbp + 8)) { InternalPrintMessage ("%a: attempted to dereference an invalid frame= " "pointer at 0x%016lx\n", __FUNCTION__, Rbp); break; --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Mon Dec 23 13:27:02 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 1515975867579620.0246832604179; Sun, 14 Jan 2018 16:24:27 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 8657A222DDBFA; Sun, 14 Jan 2018 16:18:57 -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 5F638222DDBEF for ; Sun, 14 Jan 2018 16:18:55 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 7B66DC08890; Mon, 15 Jan 2018 00:24:12 +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 di0REKi9cHnS; Mon, 15 Jan 2018 00:24:11 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (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 B81F6C78F41; Mon, 15 Jan 2018 00:24: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 B81F6C78F41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975848; bh=P3yNyXi4SecmzornniLLT/wG3+E0RVMUEbdorzmZqPE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=UjYWL7xbPtogQlRYOhdkN8oFSirPGwe0OWORBV2lT+iYUnDngujL8ScHSOnYVKmJ1 eMYF5Q5ccwCtZ3S0OZlfEe/bUuWeIIGVC7oxvNltbh6V6PzmHp7o0JwaIu1y3CF6xC sm0yJQYyuhQiYF4V1UZg11UiqPNZ8I9wuXgTbFQk= From: Paulo Alcantara To: edk2-devel@lists.01.org Date: Sun, 14 Jan 2018 22:23:36 -0200 Message-Id: X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [RFC v5 8/8] UefiCpuPkg/CpuExceptionHandlerLib: Add early check in DumpStackContents 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" Add an early check in DumpStackContens() to abort in case of no unwound stacks. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c | 7 = +++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 7 = +++++++ 2 files changed, 14 insertions(+) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHa= ndler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandl= er.c index 31fbd4a164..ac3801f704 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -805,6 +805,13 @@ DumpStackContents ( { UINT32 CurrentEsp; =20 + // + // Do nothing in case there wasn't any unwound stack. + // + if (UnwoundStacksCount =3D=3D 0) { + return; + } + // // Get current stack pointer // diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 4d8c9b0a89..6c3bad01a6 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -418,6 +418,13 @@ DumpStackContents ( UINTN RspAddressStart; UINTN RspAddressEnd; =20 + // + // Do nothing in case there wasn't any unwound stack. + // + if (UnwoundStacksCount =3D=3D 0) { + return; + } + // // Get current stack pointer // --=20 2.14.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel