From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=17.171.2.60; helo=ma1-aaemail-dr-lapp01.apple.com; envelope-from=afish@apple.com; receiver=edk2-devel@lists.01.org Received: from ma1-aaemail-dr-lapp01.apple.com (ma1-aaemail-dr-lapp01.apple.com [17.171.2.60]) (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 C50502117B552 for ; Fri, 30 Nov 2018 12:37:35 -0800 (PST) Received: from pps.filterd (ma1-aaemail-dr-lapp01.apple.com [127.0.0.1]) by ma1-aaemail-dr-lapp01.apple.com (8.16.0.22/8.16.0.22) with SMTP id wAUKacop062125; Fri, 30 Nov 2018 12:37:31 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=mime-version : content-type : sender : from : message-id : subject : date : in-reply-to : cc : to : references; s=20180706; bh=95NeKfXN8zyCEiagCnIlNd+b2xBHWwDUYeHKOp+xURs=; b=T5NHgjfULsJZF+dkK9qC21MFkrOpZq7n74wKNiM2sB+P6bx/jaOjUtflEDV1AHSKXzcd L0OrzlKbLe6n3BdhtLpNkkEXJ0XG9CaRINvH2vChPz8BofJbfLTQxNkVMU8PkTzobRzx i/Fx0bTOxAbe6ZY0aEbmq0NfEQCVPsXr5aBMS4Kyg2Q8AKXoMOR8fD7RlkWD+ZNfhC5W B3jX4KQZm4GQtLPAbRDpQ6wAGwbK6AKiNB3iipPJV0/CE7J0j0D3YSx8MbzxRgRSPqlT KYQ3wMUrVXPpW3fCIWANwZT2XLXjLH1lwUcp4/5o5aCdCu8aBYRvxoT3/5YYP3qpXci1 cg== Received: from mr2-mtap-s03.rno.apple.com (mr2-mtap-s03.rno.apple.com [17.179.226.135]) by ma1-aaemail-dr-lapp01.apple.com with ESMTP id 2ny66g63ua-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Fri, 30 Nov 2018 12:37:31 -0800 MIME-version: 1.0 Received: from ma1-mmpp-sz09.apple.com (ma1-mmpp-sz09.apple.com [17.171.128.183]) by mr2-mtap-s03.rno.apple.com (Oracle Communications Messaging Server 8.0.2.3.20180614 64bit (built Jun 14 2018)) with ESMTPS id <0PJ00077LXAGJZ00@mr2-mtap-s03.rno.apple.com>; Fri, 30 Nov 2018 12:37:29 -0800 (PST) Received: from process_viserion-daemon.ma1-mmpp-sz09.apple.com by ma1-mmpp-sz09.apple.com (Oracle Communications Messaging Server 8.0.2.3.20180614 64bit (built Jun 14 2018)) id <0PJ000A00VS9YI00@ma1-mmpp-sz09.apple.com>; Fri, 30 Nov 2018 12:37:28 -0800 (PST) X-Va-A: X-Va-T-CD: 8ebb95a2298bcf8bc7f8181a19f1a792 X-Va-E-CD: 94cb4e03de4b5ede834de82d14c6e12e X-Va-R-CD: 3e88c1804dbc623ad489085685eafb6b X-Va-CD: 0 X-Va-ID: f10b9e8a-79ed-4e4d-811c-5621e667bc0d X-V-A: X-V-T-CD: 72d6207661ea50341670b271e193a674 X-V-E-CD: 94cb4e03de4b5ede834de82d14c6e12e X-V-R-CD: 3e88c1804dbc623ad489085685eafb6b X-V-CD: 0 X-V-ID: f255fecf-aa94-429b-adf9-e62f5e403486 Received: from process_milters-daemon.ma1-mmpp-sz09.apple.com by ma1-mmpp-sz09.apple.com (Oracle Communications Messaging Server 8.0.2.3.20180614 64bit (built Jun 14 2018)) id <0PJ000B00X6JOA00@ma1-mmpp-sz09.apple.com>; Fri, 30 Nov 2018 12:37:28 -0800 (PST) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-11-30_09:,, signatures=0 Received: from [17.234.131.134] (unknown [17.234.131.134]) by ma1-mmpp-sz09.apple.com (Oracle Communications Messaging Server 8.0.2.3.20180614 64bit (built Jun 14 2018)) with ESMTPSA id <0PJ000LWYXAC8520@ma1-mmpp-sz09.apple.com>; Fri, 30 Nov 2018 12:37:28 -0800 (PST) Sender: afish@apple.com From: Andrew Fish Message-id: Date: Fri, 30 Nov 2018 12:37:15 -0800 In-reply-to: <9333E191E0D52B4999CE63A99BA663A00302C3FB5A@atlms1.us.megatrends.com> Cc: "Ni, Ruiyu" , "edk2-devel@lists.01.org" , "lersek@redhat.com" , "Dong, Eric" To: Felix Poludov References: <20181130003546.28252-1-felixp@ami.com> <734D49CCEBEEF84792F5B80ED585239D5BF35F8E@SHSMSX104.ccr.corp.intel.com> <9333E191E0D52B4999CE63A99BA663A00302C3FB5A@atlms1.us.megatrends.com> X-Mailer: Apple Mail (2.3445.6.18) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-11-30_09:, , signatures=0 X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: Re: [Patch] UefiCpuPkg/MpLib: Fix PEI Services Table pointer on AP X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Nov 2018 20:37:36 -0000 Content-Type: text/plain; CHARSET=US-ASCII Content-Transfer-Encoding: 7BIT Felix, I agree the obfuscation may not really be helpful. I've got a couple of thoughts. 1) I agree that I don't think the DebugLib is inherently safe on APs. I wonder if we could make a library class that was DebugLibMpSafe and have the same API as DebugLib. That way the library class matches how the module was coded. 2) If we want to enforce the rules we should add code to the PEI or DXE Core to CpuBreakpoint() etc. if an AP calls a core service. Adding detection code is possible, but it is not trivial. For example if you have remembered the BSP and if the WhoAmI returns something different you need to check to see if some one changed the BSP. I guess for PEI the other option could be to have a fake PEI Services Table that stubs out all the functions with CpuBreakpoint() or some such? Thanks, Andrew Fish > On Nov 30, 2018, at 6:33 AM, Felix Polyudov wrote: > > Ray, > > I agree with the premise that calling PEI services from AP should generally be avoided. > However, the PEI services can be used on AP under certain special circumstances. > A couple of examples: > 1. For debugging purposes. The MpInitLib contains 12 DEBUG calls and 19 ASSERT calls. Depending on the DebugLib instance used in the project, these calls may lead to PEI services invocation. > 2. MpInitLib provides ability to call AP in a serialized manner (only one AP is running, other APs and BSP are waiting), when it is safe to call PEI services. > > Additionally, I think even if PEI services should not be used on AP, there is still a reason to keep PEI services table pointer initialized. > On one hand, given the complexity of modern firmware projects comprised of modules coming from multiple vendors, making sure PEI services are not used on AP can be a challenge. > For example, in my case the call was coming from the chipset reference code. > On the other hand, with the current implementation, when somebody does try to use PEI services on AP the behavior is unpredictable. > Depending on the content of the uninitialized PEI service table pointer, the system may either crash with one of the handful of different exceptions, > or it may start executing code from a random location. It's very difficult to debug such issues. One can spend weeks chasing a problem like this. > > > -----Original Message----- > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org ] On Behalf Of Ni, Ruiyu > Sent: Thursday, November 29, 2018 10:43 PM > To: Felix Polyudov; edk2-devel@lists.01.org > Cc: lersek@redhat.com ; Dong, Eric > Subject: Re: [edk2] [Patch] UefiCpuPkg/MpLib: Fix PEI Services Table pointer on AP > > Felix, > I disagree:) Sorry about that. :) > > The commit you mentioned might be made by me (didn't checked). > Because I aimed to avoid calling PEI services from AP. That's a violation of PI spec and not safe by design. > > The AP calling standard services concern was raised by Andrew initially. > > Thanks, > Ray > >> -----Original Message----- >> From: Felix Polyudov [mailto:felixp@ami.com] >> Sent: Friday, November 30, 2018 8:36 AM >> To: edk2-devel@lists.01.org >> Cc: Dong, Eric ; Ni, Ruiyu ; >> lersek@redhat.com >> Subject: [Patch] UefiCpuPkg/MpLib: Fix PEI Services Table pointer on AP >> >> According to PI specification PEI Services table pointer is stored right before ITD >> base. Starting from commit c563077a380437c1 BSP and AP have different IDT >> instances. >> PEI Services table pointer was not initialized in the AP IDT instance. >> As a result, any attempt to use functions from PeiServicesTablePointerLib or >> PeiServicesLib on AP caused CPU exception. >> >> Contributed-under: TianoCore Contribution Agreement 1.1 >> Signed-off-by: Felix Polyudov >> --- >> UefiCpuPkg/Library/MpInitLib/MpLib.c | 10 +++++++++- >> 1 file changed, 9 insertions(+), 1 deletion(-) >> >> diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c >> b/UefiCpuPkg/Library/MpInitLib/MpLib.c >> index 7f4d6e6..0e3e362 100644 >> --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c >> +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c >> @@ -1567,6 +1567,7 @@ MpInitLibInitialize ( >> BufferSize = ApStackSize * MaxLogicalProcessorNumber; >> BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber; >> BufferSize += ApResetVectorSize; >> + BufferSize += sizeof(UINTN); >> BufferSize = ALIGN_VALUE (BufferSize, 8); >> BufferSize += VolatileRegisters.Idtr.Limit + 1; >> BufferSize += sizeof (CPU_MP_DATA); >> @@ -1587,6 +1588,8 @@ MpInitLibInitialize ( >> // Backup Buffer >> // +--------------------+ >> // Padding >> + // +--------------------+ >> + // PEI Services Table Pointer >> // +--------------------+ <-- ApIdtBase (8-byte boundary) >> // AP IDT All APs share one separate IDT. So AP can get address of >> CPU_MP_DATA from IDT Base. >> // +--------------------+ <-- CpuMpData >> @@ -1599,7 +1602,7 @@ MpInitLibInitialize ( >> // >> MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * >> MaxLogicalProcessorNumber); >> BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * >> MaxLogicalProcessorNumber; >> - ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8); >> + ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize + >> sizeof(UINTN), 8); >> CpuMpData = (CPU_MP_DATA *) (ApIdtBase + VolatileRegisters.Idtr.Limit + >> 1); >> CpuMpData->Buffer = Buffer; >> CpuMpData->CpuApStackSize = ApStackSize; >> @@ -1653,6 +1656,11 @@ MpInitLibInitialize ( >> Buffer + BufferSize); >> >> // >> + // Initialize PEI Services table pointer. Copy the address from BSP. >> + // >> + *(UINTN*)(ApIdtBase - sizeof(UINTN)) = >> + *(UINTN*)(VolatileRegisters.Idtr.Base - sizeof (UINTN)); >> + >> + // >> // Duplicate BSP's IDT to APs. >> // All APs share one separate IDT. So AP can get the address of CpuMpData by >> using IDTR.BASE + IDTR.LIMIT + 1 >> // >> -- >> 2.10.0.windows.1 >> >> >> >> Please consider the environment before printing this email. >> >> The information contained in this message may be confidential and proprietary >> to American Megatrends, Inc. This communication is intended to be read only >> by the individual or entity to whom it is addressed or by their designee. If the >> reader of this message is not the intended recipient, you are on notice that any >> distribution of this message, in any form, is strictly prohibited. Please promptly >> notify the sender by reply e-mail or by telephone at 770-246-8600, and then >> delete or destroy all copies of the transmission. > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel > > Please consider the environment before printing this email. > > The information contained in this message may be confidential and proprietary to American Megatrends, Inc. This communication is intended to be read only by the individual or entity to whom it is addressed or by their designee. If the reader of this message is not the intended recipient, you are on notice that any distribution of this message, in any form, is strictly prohibited. Please promptly notify the sender by reply e-mail or by telephone at 770-246-8600, and then delete or destroy all copies of the transmission. > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel