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 90320AC1A74 for ; Mon, 31 Jul 2023 22:04:35 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=VNBP1m4tH5AwISuybdag4ehCq1mfr9WsFI01o+p6aq8=; c=relaxed/simple; d=groups.io; h=X-Received:X-Received:X-Received:DKIM-Filter: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:X-Gm-Message-State:Content-Transfer-Encoding; s=20140610; t=1690841074; v=1; b=el96HHobtESkTPXU18LEHuav0fxcVjtpwYnLEFUDy+3OAE1LNnJQyojr8QvFjjFz/Uy73Vg0 9Xa3Hy7n9LrmjBewSSJHobCn3lfXus7QdRTnciJVwaFMsOImGBKHQ5z1+qGIwO8EXTvWD8LnAVN cTeL1mEDc9sbyrD5/0+PhH5s= X-Received: by 127.0.0.2 with SMTP id fo8mYY7687511x3ePlc3iT9s; Mon, 31 Jul 2023 15:04:34 -0700 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.5723.1690841073562572234 for ; Mon, 31 Jul 2023 15:04:33 -0700 X-Received: from OSD-Desktop.redmond.corp.microsoft.com (unknown [131.107.1.171]) by linux.microsoft.com (Postfix) with ESMTPSA id D840923857A1; Mon, 31 Jul 2023 15:04:32 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D840923857A1 From: "Oliver Smith-Denny" To: devel@edk2.groups.io Cc: Leif Lindholm , Ard Biesheuvel , Sami Mujawar , Gerd Hoffmann Subject: [edk2-devel][PATCH v1 1/1] ArmPkg: DefaultExceptionHandlerLib: Do Not Allocate Memory Date: Mon, 31 Jul 2023 15:04:28 -0700 Message-Id: <20230731220428.23002-1-osde@linux.microsoft.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,osde@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: t25AYLr7OYlVEmeXaDFUmfCGx7686176AA= 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=el96HHob; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (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 If gST->ConOut is available when Arm's DefaultExceptionHandler=0D is running, AsciiPrint will get called to attempt to print to=0D ConOut, in addition to the serial output.=0D =0D AsciiPrint calls AsciiInternalPrint in UefiLibPrint.c which=0D in turn calls AllocatePool to allocate a buffer to convert=0D the Ascii input string to a Unicode string to pass to=0D ConOut->OutputString.=0D =0D Per the comment on DefaultExceptionHandler, we should not be=0D allocating memory in the exception handler, as this can cause=0D the exception handler to fail if we had a memory exception or=0D the system state is such that we cannot allocate memory.=0D =0D It has been observed on ArmVirtQemu that exceptions generated=0D in the memory handling code will fail to output the stack dump=0D and CPU state that is critical to debugging because the=0D AllocatePool will fail.=0D =0D This patch fixes the Arm and AARCH64 DefaultExceptionHandlers to=0D not allocate memory when ConOut is available and instead use=0D stack memory to convert the Ascii string needed for SerialPortWrite=0D to the Unicode string needed for ConOut->OutputString. Correspondingly,=0D ArmVirtQemu can now output the stack dump and CPU state when hitting=0D an exception in memory code.=0D =0D GitHub PR: https://github.com/tianocore/edk2/pull/4703=0D =0D Cc: Leif Lindholm =0D Cc: Ard Biesheuvel =0D Cc: Sami Mujawar =0D Cc: Gerd Hoffmann =0D =0D Signed-off-by: Oliver Smith-Denny =0D ---=0D ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultExceptionHandler.= c | 18 +++++++++++++-----=0D ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler.c = | 11 +++++++++--=0D 2 files changed, 22 insertions(+), 7 deletions(-)=0D =0D diff --git a/ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultExcep= tionHandler.c b/ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultEx= ceptionHandler.c=0D index f2bca5d74005..07c8aade1c5f 100644=0D --- a/ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultExceptionHan= dler.c=0D +++ b/ArmPkg/Library/DefaultExceptionHandlerLib/AArch64/DefaultExceptionHan= dler.c=0D @@ -22,6 +22,11 @@=0D #include =0D #include =0D =0D +//=0D +// Maximum number of characters to print to serial (UINT8s) and to console= if available (as UINT16s)=0D +//=0D +#define MAX_PRINT_CHARS 100=0D +=0D STATIC CHAR8 *gExceptionTypeString[] =3D {=0D "Synchronous",=0D "IRQ",=0D @@ -188,16 +193,18 @@ DefaultExceptionHandler (=0D IN OUT EFI_SYSTEM_CONTEXT SystemContext=0D )=0D {=0D - CHAR8 Buffer[100];=0D - UINTN CharCount;=0D - INT32 Offset;=0D + CHAR8 Buffer[MAX_PRINT_CHARS];=0D + CHAR16 UnicodeBuffer[MAX_PRINT_CHARS];=0D + UINTN CharCount;=0D + INT32 Offset;=0D =0D if (mRecursiveException) {=0D STATIC CHAR8 CONST Message[] =3D "\nRecursive exception occurred whil= e dumping the CPU state\n";=0D =0D SerialPortWrite ((UINT8 *)Message, sizeof Message - 1);=0D if (gST->ConOut !=3D NULL) {=0D - AsciiPrint (Message);=0D + UnicodeSPrintAsciiFormat (UnicodeBuffer, MAX_PRINT_CHARS, Buffer);=0D + gST->ConOut->OutputString (gST->ConOut, UnicodeBuffer);=0D }=0D =0D CpuDeadLoop ();=0D @@ -208,7 +215,8 @@ DefaultExceptionHandler (=0D CharCount =3D AsciiSPrint (Buffer, sizeof (Buffer), "\n\n%a Exception at= 0x%016lx\n", gExceptionTypeString[ExceptionType], SystemContext.SystemCont= extAArch64->ELR);=0D SerialPortWrite ((UINT8 *)Buffer, CharCount);=0D if (gST->ConOut !=3D NULL) {=0D - AsciiPrint (Buffer);=0D + UnicodeSPrintAsciiFormat (UnicodeBuffer, MAX_PRINT_CHARS, Buffer);=0D + gST->ConOut->OutputString (gST->ConOut, UnicodeBuffer);=0D }=0D =0D DEBUG_CODE_BEGIN ();=0D diff --git a/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultException= Handler.c b/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionH= andler.c=0D index 13b321e45615..08d62c0dbfa2 100644=0D --- a/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler= .c=0D +++ b/ArmPkg/Library/DefaultExceptionHandlerLib/Arm/DefaultExceptionHandler= .c=0D @@ -23,6 +23,11 @@=0D #include =0D #include =0D =0D +//=0D +// Maximum number of characters to print to serial (UINT8s) and to console= if available (as UINT16s)=0D +//=0D +#define MAX_PRINT_CHARS 100=0D +=0D //=0D // The number of elements in a CHAR8 array, including the terminating NUL,= that=0D // is meant to hold the string rendering of the CPSR.=0D @@ -198,7 +203,8 @@ DefaultExceptionHandler (=0D IN OUT EFI_SYSTEM_CONTEXT SystemContext=0D )=0D {=0D - CHAR8 Buffer[100];=0D + CHAR8 Buffer[MAX_PRINT_CHARS];=0D + CHAR16 UnicodeBuffer[MAX_PRINT_CHARS];=0D UINTN CharCount;=0D UINT32 DfsrStatus;=0D UINT32 IfsrStatus;=0D @@ -217,7 +223,8 @@ DefaultExceptionHandler (=0D );=0D SerialPortWrite ((UINT8 *)Buffer, CharCount);=0D if (gST->ConOut !=3D NULL) {=0D - AsciiPrint (Buffer);=0D + UnicodeSPrintAsciiFormat (UnicodeBuffer, MAX_PRINT_CHARS, Buffer);=0D + gST->ConOut->OutputString (gST->ConOut, UnicodeBuffer);=0D }=0D =0D DEBUG_CODE_BEGIN ();=0D -- =0D 2.40.1=0D =0D -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#107412): https://edk2.groups.io/g/devel/message/107412 Mute This Topic: https://groups.io/mt/100472023/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-