From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=lersek@redhat.com; receiver=edk2-devel@lists.01.org Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (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 C7869209574FB for ; Mon, 5 Mar 2018 00:40:40 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A068140FB64A; Mon, 5 Mar 2018 08:46:52 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-120-64.rdu2.redhat.com [10.10.120.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id B05D52026E03; Mon, 5 Mar 2018 08:46:51 +0000 (UTC) To: Jian J Wang , edk2-devel@lists.01.org Cc: Ruiyu Ni , Eric Dong References: <20180303090343.6788-1-jian.j.wang@intel.com> From: Laszlo Ersek Message-ID: Date: Mon, 5 Mar 2018 09:46:50 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180303090343.6788-1-jian.j.wang@intel.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 05 Mar 2018 08:46:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 05 Mar 2018 08:46:52 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'lersek@redhat.com' RCPT:'' Subject: Re: [PATCH v2] UefiCpuPkg/MpInitLib: put mReservedApLoopFunc in executable memory 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: , X-List-Received-Date: Mon, 05 Mar 2018 08:40:41 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 03/03/18 10:03, Jian J Wang wrote: >> v2 changes: >> a. Reserve memory of mReservedApLoopFunc and mReservedTopOfApStack >> separately to avoid making mReservedTopOfApStack executable. > > if PcdDxeNxMemoryProtectionPolicy is enabled for EfiReservedMemoryType > of memory, #PF will be triggered for each APs after ExitBootServices > in SCRT test. The root cause is that AP wakeup code executed at that > time is stored in memory of type EfiReservedMemoryType (referenced by > global mReservedApLoopFunc), which is marked as non-executable. > > This patch fixes this issue by setting memory of mReservedApLoopFunc to > be executable immediately after allocation. > > Cc: Ruiyu Ni > Cc: Eric Dong > Cc: Laszlo Ersek > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Jian J Wang > --- > UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 38 +++++++++++++++++++++++++++++---- > 1 file changed, 34 insertions(+), 4 deletions(-) > > diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c > index fd2317924f..e7ed21c6cd 100644 > --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c > +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c > @@ -388,9 +388,9 @@ InitMpGlobalData ( > // Allocating it in advance since memory services are not available in > // Exit Boot Services callback function. > // > - ApSafeBufferSize = CpuMpData->AddressMap.RelocateApLoopFuncSize; > - ApSafeBufferSize += CpuMpData->CpuCount * AP_SAFE_STACK_SIZE; > - > + ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES ( > + CpuMpData->AddressMap.RelocateApLoopFuncSize > + )); > Address = BASE_4GB - 1; > Status = gBS->AllocatePages ( > AllocateMaxAddress, > @@ -399,9 +399,39 @@ InitMpGlobalData ( > &Address > ); > ASSERT_EFI_ERROR (Status); > + > mReservedApLoopFunc = (VOID *) (UINTN) Address; > ASSERT (mReservedApLoopFunc != NULL); > - mReservedTopOfApStack = (UINTN) Address + EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (ApSafeBufferSize)); > + > + // > + // Make sure that the buffer memory is executable if NX protection is enabled > + // for EfiReservedMemoryType. > + // > + // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD > + // service. > + // > + Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc); > + if (!EFI_ERROR (Status)) { > + gDS->SetMemorySpaceAttributes ( > + Address, > + ApSafeBufferSize, > + MemDesc.Attributes & (~EFI_MEMORY_XP) > + ); > + } > + > + ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES ( > + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE > + )); > + Address = BASE_4GB - 1; > + Status = gBS->AllocatePages ( > + AllocateMaxAddress, > + EfiReservedMemoryType, > + EFI_SIZE_TO_PAGES (ApSafeBufferSize), > + &Address > + ); > + ASSERT_EFI_ERROR (Status); > + > + mReservedTopOfApStack = (UINTN) Address + ApSafeBufferSize; > ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0); > CopyMem ( > mReservedApLoopFunc, > I didn't verify whether or not the AP wakeup code relies on the stack following directly the AP loop func. Since you must have tested this patch successfully, I guess there is no such dependency (it would be wrong anyway, IMO). Furthermore, the comment is helpful -- we don't call PcdGet, but the comment seems helpful in finding the PCD after all. (The commit message does name the PCD, and we can run "git blame" on the comment.) Reviewed-by: Laszlo Ersek Thanks! Laszlo