From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ma-mailsvcp-mx-lapp01.apple.com (ma-mailsvcp-mx-lapp01.apple.com [17.32.222.22]) by mx.groups.io with SMTP id smtpd.web11.1559.1684430583249986444 for ; Thu, 18 May 2023 10:23:03 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@apple.com header.s=20180706 header.b=Mcfnd6Gu; spf=pass (domain: apple.com, ip: 17.32.222.22, mailfrom: afish@apple.com) Received: from rn-mailsvcp-mta-lapp01.rno.apple.com (rn-mailsvcp-mta-lapp01.rno.apple.com [10.225.203.149]) by ma-mailsvcp-mx-lapp01.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) with ESMTPS id <0RUV00NHX6YBHF30@ma-mailsvcp-mx-lapp01.apple.com> for devel@edk2.groups.io; Thu, 18 May 2023 10:23:02 -0700 (PDT) X-Proofpoint-ORIG-GUID: 6OnJDtdZNrHUA3rnFp9V6DfAdT6qlOdw X-Proofpoint-GUID: 6OnJDtdZNrHUA3rnFp9V6DfAdT6qlOdw 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 suspectscore=0 adultscore=0 mlxscore=0 spamscore=0 bulkscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305100145 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=LKyOGXoSCNJZQssv13o4MbSzP6ygVAURn0TK0xA1TDE=; b=Mcfnd6GuH+DaOGMcQnfdnfYvjCQeFPtpIihFKIMeiBU3jTVeU8XLxEuQkYFULwUrMNCc uKO+eIS+bAD3mln2857YYZ98p3JRIKEhXsl5QFs2pyVSzrQygN+sMGUQOJ3+tLJ7gvmj TaL9DUbe/6mzM3VuDCoR6PXMo/nVjsyue5Skfx5Pv4PsO0bp5dC0G/B3P40PpdVYEkZi ufOyUkzT7Ly9Ih38M+L8dhFLJtq/tO/r7F+IKjDF/gJ8HfNDe/vd9y4n7C0Ch4EEzbJP 3xA2QDsb5ckaB8wncX0ou/oIGOeHC7lYvZPwZiCJgASXrcjhoXcOAWZSEdCKU27DFHW+ Vg== Received: from rn-mailsvcp-policy-lapp01.rno.apple.com (rn-mailsvcp-policy-lapp01.rno.apple.com [17.179.253.18]) by rn-mailsvcp-mta-lapp01.rno.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) with ESMTPS id <0RUV007QU6YB4DE0@rn-mailsvcp-mta-lapp01.rno.apple.com>; Thu, 18 May 2023 10:22:59 -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 <0RUV00Z006TWF000@rn-mailsvcp-policy-lapp01.rno.apple.com>; Thu, 18 May 2023 10:22:59 -0700 (PDT) X-Va-A: X-Va-T-CD: da3a4df698400084da27c6ab403bcb35 X-Va-E-CD: 156d5ae2ad5b70872c2d8b4f187426ab X-Va-R-CD: faed37d9f0280288bcb510df700d8485 X-Va-ID: f8956b9d-7752-4840-ba1b-5ddf6b2fba90 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: 37ff4d37-b88d-44e3-b1c6-8d6a0b69a60c 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 <0RUV001206YBN800@rn-mailsvcp-policy-lapp01.rno.apple.com>; Thu, 18 May 2023 10:22:59 -0700 (PDT) From: "Andrew Fish" Message-id: <1098DEB5-028F-46F2-8CF8-A965E8401F88@apple.com> 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:22:49 -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=_353A6109-D925-4887-ABEC-E20DCC6C6B8A" --Apple-Mail=_353A6109-D925-4887-ABEC-E20DCC6C6B8A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Yea, but I think you want static and volatile on the global. But good idea= as for the non XIP case you can just modify the global. It might be a good= idea to document the debugging flow in the header for CpuDeadLoop()... Thanks, Andrew Fish > On May 18, 2023, at 10:19 AM, Kinney, Michael D wrote: >=20 > Andrew, > =20 > This might work for XIP. Set non const global to initial value that is e= xpected value to stay in dead loop. > =20 > UINTN mDeadLoopCount =3D 0; > =20 > VOID > CpuDeadLoop( > VOID > )=20 > { > while (mDeadLoopCount =3D=3D 0) { > CpuPause(); > } > } > =20 > When deadloop is entered, developer can not change value of mDeadLoopCoun= t, but they can use debugger to force exit loop and return from function. > =20 > Mike > =20 > =20 > From: Andrew (EFI) Fish =20 > Sent: Thursday, May 18, 2023 10:09 AM > To: Kinney, Michael D > Cc: edk2-devel-groups-io ; Ni, Ray ; Rebecca Cran > Subject: Re: [edk2-devel] CpuDeadLoop() is optimized by compiler > =20 > Mike, > =20 > Good point, that is why we are using the stack =E2=80=A6. > =20 > The only other thing I can think of is to pass the address of Index to so= me inline assembler, or an asm no op function, to give it a side effect the= compiler can=E2=80=99t resolve.=20 > =20 > Thanks, > =20 > Andrew Fish >=20 >=20 > 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 >=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=_353A6109-D925-4887-ABEC-E20DCC6C6B8A Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Yea,  but I think you want s= tatic and volatile on the global. But good idea as for the non XIP case you= can just modify the global. It might be a good idea to document the debugg= ing flow in the header for CpuDeadLoop()...

Thanks,

Andrew Fish

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

Andrew,
 
This m= ight work for XIP.  Set non const global to initial value that is expe= cted value to stay in dead loop.
 
UINTN  mDeadLoopCount =3D 0;
 
VOID
CpuDeadLoop(
 = VOID
  )&nb= sp;
{
  while (mDeadLoo= pCount =3D=3D 0) {
      CpuPa= use();
  }
}
<= div style=3D"margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif= ;"> 
When deadloop is entered, developer can not c= hange value of mDeadLoopCount, but they can use debugger to force exit loop= and return from function.
 
M= ike
 
 
<= div style=3D"border-width: medium medium medium 1.5pt; border-style: none n= one none solid; border-color: currentcolor currentcolor currentcolor blue; = border-image: none; padding: 0in 0in 0in 4pt;">
From: = Andrew (EFI) Fish <afish@apple.com> 
Sent:&nbs= p;Thursday, May 18, 2023 10:09 AM
To: Kinney, Michael D <michael.d.kinney@intel.= com>
Cc: edk= 2-devel-groups-io <devel@edk2.groups.io>; Ni, Ray <ray.ni@intel.co= m>; Rebecca Cran <rebecca@bsdio.com>
Subject: Re: [edk2-devel] CpuDeadLoop() is o= ptimized by compiler
 
Mike,
 
G= ood point, that is why we are using the stack =E2=80=A6.
 
The only other thing I c= an 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 compiler can=E2=80=99t re= solve. 
 
Thanks,
 
=
Andrew Fish


On May 18, = 2023, at 10:05 AM, Kinney, Michael D <michael.d.ki= nney@intel.com> wrote:
 
Static global will not work for XIP
 
Mike
=
 
From:&= nbsp;Andrew (EFI) Fish <afish@apple.com> 
Sent: Thursday, May 18, 2023 9:49 AM
T= o: edk2-devel-groups-= io <devel@edk2.groups.io>; Kinney, Michael D <= michael.d.kinney@intel.com>
Cc: Ni, Ray <ray.n= i@intel.com>; Rebecca Cran <rebecca@bsdio.com>= ;
Subject: Re: = [edk2-devel] CpuDeadLoop() is optimized by compiler
<= /div>
 
Mike,<= /div>
 
I= pinged some compiler experts to see if our code is correct, or if the comp= iler 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. 
 
If we move Index to a static global that would likely work a= round the compiler issue.
 =
Thanks,
 
Andrew= Fish



= On May 18, 2023, at 8:36 AM, Michael D Kinney <mic= hael.d.kinney@intel.com> wrote:
 
Hi Ray,
 
So= the code generated does deadloop, but is just not easy to resume from as w= e have been able to do in the past.
<= div style=3D"margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif= ;"> 
We use CpuDeadloop() fo= r 2 purposes.  One is a terminal condition with no reason to ever cont= inue.
 
The 2nd is a debug aide for developers to halt the system at a spec= ific location and then continue from that point, usually with a debugger, t= o step through code to an area to evaluate unexpected behavior.<= /div>
 
We may have to do a NASM implementation of CpuDeadloop() to make sure = it meets both use cases.
 =
Mike
 
From: Ni, Ray <ray.ni@intel.com&g= t; 
Sent: Thursday, May 18, 2023 3:00 AM=
To: devel@edk2.groups.io
Cc: Kinney, Michael D <michael.d.k= inney@intel.com>; Rebecca Cran <rebecca@bsdio.com>; Ni, Ray <ray.ni@intel.com>
Subject:<= span class=3D"apple-converted-space"> CpuDeadLoop() is optimize= d by compiler
 =
Hi,
=
Starting from certain version of Visual Studio C compiler (I = don=E2=80=99t have the exact version. I am using VS2019), CpuDeadLoop is no= w optimized quite well by compiler.
<= div style=3D"margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif= ;"> 
The optimization is so = =E2=80=9Cgood=E2=80=9D that it becomes harder for developers to break out o= f the deadloop.
 =
I copied the assembly instructions as below= for your reference.
The compiler= does not generate instructions that jump out of the loop when the Index is= not zero.
So in order to break o= ut of the loop, developers need to:
  1. Manually adjust rsp by increasing 40
  2. Manually =E2=80=9Cret=E2=80=9D<= /ol>
     
    I am n= ot sure if anyone has interest to re-write this function so that compiler c= an be =E2=80=9Cfooled=E2=80=9D again.
    Thanks,
    Ray
 
=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
;        &nbs= p;     COMDAT CpuDeadLoop
<= div>
_TEXT    SEGMENT
Index$ =3D 48
CpuDea= dLoop PROC           = ;            &n= bsp;            = ;            &n= bsp;            = ;       ; COMDAT
=
 
; 26 &n= bsp; : {
 
<= /div>
$LN12:
&= nbsp; 00000  48 83 ec 28       &nbs= p; sub        rsp, 40   &= nbsp;           &nbs= p;            &= nbsp;   ; 00000028H
&nb= sp;
; 27   :  = ; volatile UINTN  Index;
; 2= 8   : 
; 29   :   for (Index= =3D 0; Index =3D=3D 0;) {
 =
  00004  48 c7 44 24 3= 0
     &= nbsp;         00 00 00 00 &nbs= p;      mov      QWORD PT= R Index$[rsp], 0
$LN10@CpuDeadLoo= :
 
; 30   :     CpuPause ();
 
  0000d  48 8b 44 24 30   mov  = ;    rax, QWORD PTR Index$[rsp]
=
  00012  e8 00 00 00 00   call  = ;      CpuPause
<= div>
  00017  eb f4       &= nbsp;           &nbs= p; jmp       SHORT $LN10@CpuDeadLoo
CpuDeadLoop ENDP
_TEXT    ENDS
<= div>
END
 
 
<= div>
<= /blockquote>

--Apple-Mail=_353A6109-D925-4887-ABEC-E20DCC6C6B8A--