From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ma-mailsvcp-mx-lapp02.apple.com (ma-mailsvcp-mx-lapp02.apple.com [17.32.222.23]) by mx.groups.io with SMTP id smtpd.web10.1167.1684429728022623196 for ; Thu, 18 May 2023 10:08:48 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@apple.com header.s=20180706 header.b=Ic0STli7; spf=pass (domain: apple.com, ip: 17.32.222.23, mailfrom: afish@apple.com) Received: from rn-mailsvcp-mta-lapp02.rno.apple.com (rn-mailsvcp-mta-lapp02.rno.apple.com [10.225.203.150]) by ma-mailsvcp-mx-lapp02.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) with ESMTPS id <0RUV00WAX6AMOA20@ma-mailsvcp-mx-lapp02.apple.com> for devel@edk2.groups.io; Thu, 18 May 2023 10:08:47 -0700 (PDT) X-Proofpoint-ORIG-GUID: Uv-pYyB2XXkqhS2FeGg_RN829dVINz0n X-Proofpoint-GUID: Uv-pYyB2XXkqhS2FeGg_RN829dVINz0n X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.573,18.0.942 definitions=2023-05-10_04:2023-05-05,2023-05-10 signatures=0 X-Proofpoint-Spam-Details: rule=interactive_user_notspam policy=interactive_user score=0 mlxlogscore=999 phishscore=0 suspectscore=0 spamscore=0 adultscore=0 mlxscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305100143 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=from : message-id : content-type : mime-version : subject : date : in-reply-to : cc : to : references; s=20180706; bh=IVCbV9kFvEgtAvlam0vKm/+TjZJY1HcX2slSYQJMenI=; b=Ic0STli7uJzLaynVBiro2eNAz3q/8YDx0BVDdulZEeLyFFLFobvdbYsJIxP4E5NSANE1 NMHR67zTjytN8/QubEHJeRCGXcKAD+MOPfVQLqbHPEMgDXwe96fg7CHycdTi7EZOIOvW DDu3L/+zWVDrfl2nn1+rSNyyeiNoQ2nAKo8BMvoXhx5snJFwLg1+AS8VPvHdBb36E2fh HvVqKvQIdjnTko94cAbw3DlvFEBdFpDbYla6JtXDVsUO7k2df9OK88c1NwPcinK0ZviY KKbN4PHmPwECDEEFun4kmn1/Ez7AJnRXfSSbgver0fFBCIlbLmq4kQFra5Xw1o/h60SG 7A== Received: from rn-mailsvcp-policy-lapp01.rno.apple.com (rn-mailsvcp-policy-lapp01.rno.apple.com [17.179.253.18]) by rn-mailsvcp-mta-lapp02.rno.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) with ESMTPS id <0RUV00JRS6AM7RM0@rn-mailsvcp-mta-lapp02.rno.apple.com>; Thu, 18 May 2023 10:08:46 -0700 (PDT) Received: from process_milters-daemon.rn-mailsvcp-policy-lapp01.rno.apple.com by rn-mailsvcp-policy-lapp01.rno.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) id <0RUV004005WJU100@rn-mailsvcp-policy-lapp01.rno.apple.com>; Thu, 18 May 2023 10:08:46 -0700 (PDT) X-Va-A: X-Va-T-CD: da3a4df698400084da27c6ab403bcb35 X-Va-E-CD: 156d5ae2ad5b70872c2d8b4f187426ab X-Va-R-CD: faed37d9f0280288bcb510df700d8485 X-Va-ID: decc94e6-3f5a-4192-88cc-3ccb54802c14 X-Va-CD: 0 X-V-A: X-V-T-CD: da3a4df698400084da27c6ab403bcb35 X-V-E-CD: 156d5ae2ad5b70872c2d8b4f187426ab X-V-R-CD: faed37d9f0280288bcb510df700d8485 X-V-ID: a072c79b-8eb1-458a-8eb0-b3e6cc41321a X-V-CD: 0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.573,18.0.957 definitions=2023-05-18_13:2023-05-17,2023-05-18 signatures=0 Received: from smtpclient.apple (unknown [17.115.1.186]) by rn-mailsvcp-policy-lapp01.rno.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) with ESMTPSA id <0RUV00W2F6ALNC00@rn-mailsvcp-policy-lapp01.rno.apple.com>; Thu, 18 May 2023 10:08:46 -0700 (PDT) From: "Andrew Fish" Message-id: MIME-version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) Subject: Re: [edk2-devel] CpuDeadLoop() is optimized by compiler Date: Thu, 18 May 2023 10:08:35 -0700 In-reply-to: Cc: edk2-devel-groups-io , "Ni, Ray" , Rebecca Cran To: Mike Kinney References: <7C9FD4BA-328C-4CFE-AF5A-3A795BB147E4@apple.com> X-Mailer: Apple Mail (2.3731.600.7) Content-type: multipart/alternative; boundary="Apple-Mail=_AEC74D56-8394-4F24-B933-4202D9EA0366" --Apple-Mail=_AEC74D56-8394-4F24-B933-4202D9EA0366 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Mike, Good point, that is why we are using the stack =E2=80=A6. The only other thing I can think of is to pass the address of Index to some= inline assembler, or an asm no op function, to give it a side effect the c= ompiler can=E2=80=99t resolve.=20 Thanks, Andrew Fish > On May 18, 2023, at 10:05 AM, Kinney, Michael D wrote: >=20 > Static global will not work for XIP > =20 > Mike > =20 > From: Andrew (EFI) Fish =20 > Sent: Thursday, May 18, 2023 9:49 AM > To: edk2-devel-groups-io ; Kinney, Michael D > Cc: Ni, Ray ; Rebecca Cran > Subject: Re: [edk2-devel] CpuDeadLoop() is optimized by compiler > =20 > Mike, > =20 > I pinged some compiler experts to see if our code is correct, or if the c= ompiler has an issue. Seems to be trending compiler issue right now, but I= =E2=80=99ve NOT gotten feedback from anyone on the spec committee yet.=20 > =20 > If we move Index to a static global that would likely work around the com= piler issue. > =20 > Thanks, > =20 > Andrew Fish >=20 >=20 > On May 18, 2023, at 8:36 AM, Michael D Kinney > wrote: > =20 > Hi Ray, > =20 > So the code generated does deadloop, but is just not easy to resume from = as we have been able to do in the past. > =20 > We use CpuDeadloop() for 2 purposes. One is a terminal condition with no= reason to ever continue. > =20 > The 2nd is a debug aide for developers to halt the system at a specific l= ocation and then continue from that point, usually with a debugger, to step= through code to an area to evaluate unexpected behavior. > =20 > We may have to do a NASM implementation of CpuDeadloop() to make sure it = meets both use cases. > =20 > Mike > =20 > From: Ni, Ray >=20 > Sent: Thursday, May 18, 2023 3:00 AM > To: devel@edk2.groups.io > Cc: Kinney, Michael D >; Rebecca Cran >;= Ni, Ray > > Subject: CpuDeadLoop() is optimized by compiler > =20 > Hi, > Starting from certain version of Visual Studio C compiler (I don=E2=80=99= t have the exact version. I am using VS2019), CpuDeadLoop is now optimized = quite well by compiler. > =20 > The optimization is so =E2=80=9Cgood=E2=80=9D that it becomes harder for = developers to break out of the deadloop. > =20 > I copied the assembly instructions as below for your reference. > The compiler does not generate instructions that jump out of the loop whe= n the Index is not zero. > So in order to break out of the loop, developers need to: > Manually adjust rsp by increasing 40 > Manually =E2=80=9Cret=E2=80=9D > =20 > I am not sure if anyone has interest to re-write this function so that co= mpiler can be =E2=80=9Cfooled=E2=80=9D again. > Thanks, > Ray > =20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > ; Function compile flags: /Ogspy > ; File e:\work\edk2\MdePkg\Library\BaseLib\CpuDeadLoop.c > ; COMDAT CpuDeadLoop > _TEXT SEGMENT > Index$ =3D 48 > CpuDeadLoop PROC = ; COMDAT > =20 > ; 26 : { > =20 > $LN12: > 00000 48 83 ec 28 sub rsp, 40 = ; 00000028H > =20 > ; 27 : volatile UINTN Index; > ; 28 :=20 > ; 29 : for (Index =3D 0; Index =3D=3D 0;) { > =20 > 00004 48 c7 44 24 30 > 00 00 00 00 mov QWORD PTR Index$[rsp], 0 > $LN10@CpuDeadLoo: > =20 > ; 30 : CpuPause (); > =20 > 0000d 48 8b 44 24 30 mov rax, QWORD PTR Index$[rsp] > 00012 e8 00 00 00 00 call CpuPause > 00017 eb f4 jmp SHORT $LN10@CpuDeadLoo > CpuDeadLoop ENDP > _TEXT ENDS > END > =20 > =20 >=20 --Apple-Mail=_AEC74D56-8394-4F24-B933-4202D9EA0366 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Mike,

Good poi= nt, that is why we are using the stack =E2=80=A6.

= The only other thing I can think of is to pass the address of Index to some= inline assembler, or an asm no op function, to give it a side effect the c= ompiler can=E2=80=99t resolve. 

Thanks,
=

Andrew Fish

= On May 18, 2023, at 10:05 AM, Kinney, Michael D <michael.d.kinney@intel.= com> wrote:

Static global will not work for XIP
&= nbsp;
Mike
 
From: Andre= w (EFI) Fish <afish@apple.com>&= nbsp;
Sent: Thursday, May 18, 2023 9:49 AM
To: edk2-devel-groups-io <devel@edk2.groups.io>; = Kinney, Michael D <michael.d.kinney@intel.com>
Cc: Ni, Ray <ray.ni@intel.com>;= Rebecca Cran <rebecca@bsdio.com>
Subject: Re: [edk2-devel] CpuDeadLoop() is optimiz= ed by compiler
 
M= ike,
 
I ping= ed some compiler experts to see if our code is correct, or if the compiler = has an issue. Seems to be trending compiler issue right now, but I=E2=80=99= ve NOT gotten feedback from anyone on the spec committee yet. 
 
If we move In= dex to a static global that would likely work around the compiler issue.
 
Thanks= ,
 
Andr= ew Fish
From: N= i, Ray <ray.ni@intel.com> 
Sent: Thursday, May 18, 2023 3:00 AM
To: devel@edk2.groups.io=
Cc: Kinney, Mi= chael D <michael.d.kinney@intel.com>; Rebec= ca Cran <rebecca@bsdio.com>; Ni, Ray <ray.ni@intel.com>
Subject: CpuDeadLoop() is optimized by compiler
=
 
Hi,
Starting from certain version of Visual Studio C c= ompiler (I don=E2=80=99t have the exact version. I am using VS2019), CpuDea= dLoop is now optimized quite well by compiler.
<= div style=3D"margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif= ;"> 
The optimization is so =E2=80=9Cgo= od=E2=80=9D that it becomes harder for developers to break out of the deadl= oop.
 
I= copied the assembly instructions as below for your reference.
The compiler does not generate instructions that jump ou= t of the loop when the Index is not zero.
So= in order to break out of the loop, developers need to:
    Manually adjust rsp by increasing 40
  1. Manually =E2=80=9Cret=E2=80=9D<= /o:p>
 
I am not sure= if anyone has interest to re-write this function so that compiler can be = =E2=80=9Cfooled=E2=80=9D again.
Thanks,
R= ay
 
=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
; Function compile flags: /Ogspy
; File e:\work\edk2\MdePkg\Library\BaseLib\CpuDeadLoop.c
;        &= nbsp;     COMDAT CpuDeadLoop
_TEXT    SEGMENT
Index$= =3D 48
CpuDeadLoop PROC   &n= bsp;            = ;            &n= bsp;            = ;            &n= bsp;            = ;  ; COMDAT
 
; 26   : {
 <= /o:p>
$LN12:
  0000= 0  48 83 ec 28         sub&nbs= p;       rsp, 40     = ;            &n= bsp;            = ;  ; 00000028H
 
<= /div>
; 27   :   volatile UINTN  Index;
; 28   : 
; 29   :&n= bsp;  for (Index =3D 0; Index =3D=3D 0;) {
=
 
  00004  48 c7 44 24 30=
       &= nbsp;       00 00 00 00   &nbs= p;    mov      QWORD PTR Index$[rsp= ], 0
$LN10@CpuDeadLoo:
 
; 30   : &nb= sp;   CpuPause ();
 
  0000d  48 8b 44 24 30   mov&nb= sp;     rax, QWORD PTR Index$[rsp]
  00012  e8 00 00 00 00   call  &n= bsp;     CpuPause
 = 00017  eb f4         &nb= sp;           jmp &n= bsp;     SHORT $LN10@CpuDeadLoo
<= div>
CpuDeadLoop ENDP
_TEXT  &n= bsp; ENDS
END
 
 
<= div>

--Apple-Mail=_AEC74D56-8394-4F24-B933-4202D9EA0366--