From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ma-mailsvcp-mx-lapp03.apple.com (ma-mailsvcp-mx-lapp03.apple.com [17.32.222.24]) by mx.groups.io with SMTP id smtpd.web11.572.1684428563305241966 for ; Thu, 18 May 2023 09:49:23 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@apple.com header.s=20180706 header.b=cajyqm2l; spf=pass (domain: apple.com, ip: 17.32.222.24, 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-lapp03.apple.com (Oracle Communications Messaging Server 8.1.0.22.20230228 64bit (built Feb 28 2023)) with ESMTPS id <0RUV001JN5E9LK00@ma-mailsvcp-mx-lapp03.apple.com> for devel@edk2.groups.io; Thu, 18 May 2023 09:49:22 -0700 (PDT) X-Proofpoint-ORIG-GUID: KFRAE6nWlr87KSnUb1RNbTHhSMYAfO8A X-Proofpoint-GUID: KFRAE6nWlr87KSnUb1RNbTHhSMYAfO8A 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=8Uz/bwlVnZvYJqm+r5F/azEKSs2Usu0Pu2BXEEmt3sU=; b=cajyqm2li/PRY7V+Eb8GhoHiIeOmZhsOZxf561ACy7zt6He4K7TgkRYSoJU5ufp39jS0 y+aYz736EJVf08iFZdxklbztxyyOXMFD5juFdsjuOMgApSN1kg2iS/96wx5NLqsfNydX lSgypxzczgRzbsPF7uGspNQg4aSDyMc4O4K+di4G7h7QkdFffrHoTJsItVcqLS6lY7a8 2igIK61gRhNxZ1asD15ZutUiJId7Dz9uldCDwVqz7LCueHcmsgjxLYWOaMN4bU1S4dT6 4faXrYJH1bgtq/A+Uj1Ga3hOQR1lx9csWSBTsdxszupLjry3sclfk13YgXL1NFEVkDlx 9Q== 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 <0RUV00M6A5E8HY60@rn-mailsvcp-mta-lapp01.rno.apple.com>; Thu, 18 May 2023 09:49:21 -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 <0RUV00X005C0HB00@rn-mailsvcp-policy-lapp01.rno.apple.com>; Thu, 18 May 2023 09:49:20 -0700 (PDT) X-Va-A: X-Va-T-CD: 70a38c3f5b1d46c4b8dccb3b011be358 X-Va-E-CD: 156d5ae2ad5b70872c2d8b4f187426ab X-Va-R-CD: faed37d9f0280288bcb510df700d8485 X-Va-ID: e8da95dc-be0f-43c5-ab90-956019029f9f X-Va-CD: 0 X-V-A: X-V-T-CD: 70a38c3f5b1d46c4b8dccb3b011be358 X-V-E-CD: 156d5ae2ad5b70872c2d8b4f187426ab X-V-R-CD: faed37d9f0280288bcb510df700d8485 X-V-ID: f2b73a6b-7c19-44b1-abe3-32b224469ec0 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 <0RUV011IN5E8BU00@rn-mailsvcp-policy-lapp01.rno.apple.com>; Thu, 18 May 2023 09:49:20 -0700 (PDT) From: "Andrew Fish" Message-id: <7C9FD4BA-328C-4CFE-AF5A-3A795BB147E4@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 09:49:09 -0700 In-reply-to: Cc: "Ni, Ray" , Rebecca Cran To: edk2-devel-groups-io , Mike Kinney References: X-Mailer: Apple Mail (2.3731.600.7) Content-type: multipart/alternative; boundary="Apple-Mail=_3BFA4DF9-19DB-49FC-8A4B-604000E047A3" --Apple-Mail=_3BFA4DF9-19DB-49FC-8A4B-604000E047A3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Mike, I pinged some compiler experts to see if our code is correct, or if the com= piler 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 If we move Index to a static global that would likely work around the compi= ler issue. Thanks, Andrew Fish > 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=_3BFA4DF9-19DB-49FC-8A4B-604000E047A3 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Mike,

I pinged= some compiler experts to see if our code is correct, or if the compiler ha= s 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 = around the compiler issue.

Thanks,

<= /div>
Andrew Fish

On May 18,= 2023, at 8:36 AM, Michael D Kinney <michael.d.kinney@intel.com> wrot= e:

= Hi Ray,
 
So the code generate= d does deadloop, but is just not easy to resume from as we have been able t= o do in the past.
 
We use Cpu= Deadloop() for 2 purposes.  One is a terminal condition with no reason= to ever continue.
 
The 2nd is a debug aide= for developers to halt the system at a specific location and then continue= from that point, usually with a debugger, to step through code to an area = to evaluate unexpected behavior.
 
We may have to do a NASM implementation of CpuDeadloop() to make sure = it meets both use cases.
 
Mik= e
 
From: Ni, Ray &l= t;ray.ni@intel.com> 
Sent: Thursday, May 18, 2023 3:00 AM
To: devel@edk2.groups.io
Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Rebecca Cran &l= t;rebecca@bsdio.com>; Ni, Ray &= lt;ray.ni@intel.com>
Subje= ct: CpuDeadLoop() is = optimized by compiler
 
Hi,
Starting from certain version of Visua= l Studio C compiler (I don=E2=80=99t have the exact version. I am using VS2= 019), CpuDeadLoop is now optimized quite well by compiler.
=
 
The optimization is so =E2=80=9Cgood=E2=80= =9D that it becomes harder for developers to break out of the deadloop.
 
I copied the assembly instructi= ons 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 out of the loop, developers need = to:
  1. Manually adjust rsp by inc= reasing 40
  2. Manually =E2=80=9Cr= et=E2=80=9D
 
I am not sur= e if anyone has interest to re-write this function so that compiler can 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 fl= ags: /Ogspy
; File e:\work\edk2\MdePkg\Library\BaseLib\= CpuDeadLoop.c
;      &nbs= p;       COMDAT CpuDeadLoop
<= div style=3D"margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif= ;">_TEXT    SEGMENT
Index$ =3D 48
CpuDeadLoop PROC       &nb= sp;            =             &nb= sp;            =             &nb= sp;          ; COMDAT
 
; 26   : {
 
$LN12:
  00000&nbs= p; 48 83 ec 28         sub &nb= sp;      rsp, 40     &nbs= p;            &= nbsp;           &nbs= p; ; 00000028H
 
; 27 &nb= sp; :   volatile UINTN  Index;
; 28 = ;  : 
; 29   :   for (Index =3D 0; Index =3D=3D 0;) {
 
  00004  48 c7 44 2= 4 30
        &n= bsp;      00 00 00 00     = ;   mov      QWORD PTR Index$[rsp], 0
$LN10@CpuDeadLoo:
 
; 30   :     CpuPause ();<= /div>
 
  0000d  48 8b 44 24 30 =   mov      rax, QWORD PTR Index$[rsp]
  00012  e8 00 00 00 00   call  =       CpuPause
  00017&nb= sp; eb f4           =           jmp   = ;    SHORT $LN10@CpuDeadLoo
CpuDeadLoop = ENDP
_TEXT    ENDS
E= ND
 
 

--Apple-Mail=_3BFA4DF9-19DB-49FC-8A4B-604000E047A3--