From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id DC5667803E5 for ; Tue, 29 Aug 2023 13:29:35 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=ddyU0aVs8PV6psm4l6ufwU14ZNBEyyKNHPg29QqtKnI=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1693315774; v=1; b=s7Xdnb6Ujz/JVe8CSNlQP0a5ByZZXsJTVoC+pjbWXJnltSOUwJqz0XgQ1bsUiQAnQiVKhJjm 9vga3Y9Kt4sZCbe4LU5EL0k9AfqNfc+uWdNa8RcHxpSIthFA974c/VP6tpP8EaPMpUmwwDnV0U1 hISXLOumGIN2sYzFlVGZSkYE= X-Received: by 127.0.0.2 with SMTP id wNbKYY7687511xpmqSbCv3Ee; Tue, 29 Aug 2023 06:29:34 -0700 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.17411.1693315773813187678 for ; Tue, 29 Aug 2023 06:29:34 -0700 X-Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0DB4F6156E; Tue, 29 Aug 2023 13:29:33 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1940FC433C7; Tue, 29 Aug 2023 13:29:30 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: quic_llindhol@quicinc.com, lersek@redhat.com, Ard Biesheuvel Subject: [edk2-devel] [PATCH 1/1] ArmPkg/ExceptionSupport: Support backtrace through an exception Date: Tue, 29 Aug 2023 15:29:21 +0200 Message-Id: <20230829132921.123407-1-ardb@kernel.org> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ardb@kernel.org List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 2Rw1BsKnkbi7y2HePjrHK7Jkx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=s7Xdnb6U; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Laszlo reports that the efi_gdb.py script fails to produce a full backtrace when attaching it to an ARM firmware build that has halted on an unhandled exception. The reason is that the asm code that processes the exception was not implemented with this in mind, and therefore lacks any handling of it. So let's add this: create a dummy frame record suitable for chasing the frame pointer, and add the CFI metadata to describe where the return value can be found on the stack. When using a GCC5 build, this produces a stack trace such as (gdb) bt #0 0x000000007fd4537c in CpuDeadLoop () at /home/ardb/build/edk2/MdePkg/= Library/BaseLib/CpuDeadLoop.c:30 #1 0x000000007fd454f8 in DebugAssert ( FileName=3DFileName@entry=3D0x7fd4a8a8 "/h= ome/ardb/build/edk2/ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/Defau= ltExceptionHandler.c", LineNumber=3DLineNumber@entry=3D343, Description=3DDescription@entry= =3D0x7fd4a896 "((BOOLEAN)(0=3D=3D1))") at /home/ardb/build/edk2/MdePkg/Library/BaseDebugLibSerialPort/DebugL= ib.c:235 #2 0x000000007fd479ec in DefaultExceptionHandler (ExceptionType=3D, SystemContext=3D...) at /home/ardb/build/edk2/ArmPkg/Library/DefaultExceptionHandlerLib/AA= rch64/DefaultExceptionHandler.c:343 #3 0x000000007fd48eb8 in ExceptionHandlersEnd () #4 0x000000007fcde944 in QemuLoadKernelImage (ImageHandle=3D) at /home/ardb/build/edk2/OvmfPkg/Library/GenericQemuLoadImageLib/G= enericQemuLoadImageLib.c:201 #5 TryRunningQemuKernel () at /home/ardb/build/edk2/ArmVirtPkg/Library/P= latformBootManagerLib/QemuKernel.c:46 #6 PlatformBootManagerAfterConsole () at /home/ardb/build/edk2/ArmVirtPk= g/Library/PlatformBootManagerLib/PlatformBm.c:1139 #7 BdsEntry (This=3D) at /home/ardb/build/edk2/MdeModuleP= kg/Universal/BdsDxe/BdsEntry.c:931 #8 0x000000007ffd0018 in ?? () Backtrace stopped: previous frame inner to this frame (corrupt stack?) when QemuLoadKernelImage() has been tweaked to trigger an exception, as is shown by GDB when walking the call stack: | 0x7fcde938 b.ne 0x7fcdf134 //= b.any | 0x7fcde93c mov x0, #0x40 = // #64 | 0x7fcde940 bl 0x7fcd7aec | > 0x7fcde944 brk #0x4d2 | 0x7fcde948 bl 0x7fce4354 | 0x7fcde94c tbz x0, #63, 0x7fcde954 | 0x7fcde950 bl 0x7fcd844c | 0x7fcde954 bl 0x7fcd990c --- ArmPkg/Library/ArmExceptionLib/AArch64/ExceptionSupport.S | 18 +++++++++++= ++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ArmPkg/Library/ArmExceptionLib/AArch64/ExceptionSupport.S b/Ar= mPkg/Library/ArmExceptionLib/AArch64/ExceptionSupport.S index cd9437b6aab8..345b566932bb 100644 --- a/ArmPkg/Library/ArmExceptionLib/AArch64/ExceptionSupport.S +++ b/ArmPkg/Library/ArmExceptionLib/AArch64/ExceptionSupport.S @@ -259,6 +259,8 @@ ASM_PFX(ExceptionHandlersEnd): =0D =0D ASM_PFX(CommonExceptionEntry):=0D + .cfi_sections .debug_frame=0D + .cfi_startproc=0D =0D EL1_OR_EL2_OR_EL3(x1)=0D 1:mrs x2, elr_el1 // Exception Link Register=0D @@ -280,6 +282,13 @@ ASM_PFX(CommonExceptionEntry): =0D 4:mrs x4, fpsr // Floating point Status Register 32bit=0D =0D + // Create a dummy frame record using the ELR as the return address=0D + stp x29, x2, [sp, #-16]!=0D + .cfi_def_cfa_offset (GP_CONTEXT_SIZE + FP_CONTEXT_SIZE + SYS_CONTEXT_SIZ= E + 16)=0D + .cfi_rel_offset x29, 0=0D + .cfi_rel_offset x30, 8=0D + mov x29, sp=0D +=0D // Save the SYS regs=0D stp x2, x3, [x28, #-SYS_CONTEXT_SIZE]!=0D stp x4, x5, [x28, #0x10]=0D @@ -305,7 +314,7 @@ ASM_PFX(CommonExceptionEntry): =0D // x0 still holds the exception type.=0D // Set x1 to point to the top of our struct on the Stack=0D - mov x1, sp=0D + add x1, sp, #16=0D =0D // CommonCExceptionHandler (=0D // IN EFI_EXCEPTION_TYPE ExceptionType, R0=0D @@ -318,6 +327,9 @@ ASM_PFX(CommonExceptionEntry): // We do not try to recover.=0D bl ASM_PFX(CommonCExceptionHandler) // Call exception handler=0D =0D + // Pop dummy frame record=0D + add sp, sp, #16=0D +=0D // Pop as many GP regs as we can before entering the critical section be= low=0D ldp x2, x3, [sp, #0x10]=0D ldp x4, x5, [sp, #0x20]=0D @@ -378,13 +390,17 @@ ASM_PFX(CommonExceptionEntry): =0D // pop remaining GP regs and return from exception.=0D ldr x30, [sp, #0xf0 - 0xe0]=0D + .cfi_restore 30=0D ldp x28, x29, [sp], #GP_CONTEXT_SIZE - 0xe0=0D + .cfi_restore 29=0D =0D // Adjust SP to be where we started from when we came into the handler.= =0D // The handler can not change the SP.=0D add sp, sp, #FP_CONTEXT_SIZE + SYS_CONTEXT_SIZE=0D + .cfi_def_cfa_offset 0=0D =0D eret=0D + .cfi_endproc=0D =0D ASM_FUNC(RegisterEl0Stack)=0D msr sp_el0, x0=0D --=20 2.42.0.rc2.253.gd59a3bf2b4-goog -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#108092): https://edk2.groups.io/g/devel/message/108092 Mute This Topic: https://groups.io/mt/101030910/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-