<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Mike,<div><br></div><div>I guess my other question… If this turns out to be a compiler bug should we scope the change to the broken toolchain. I’m not sure what the right answer is for that, but I want to ask the question? </div><div><br></div><div>Thanks,</div><div><br></div><div>Andrew Fish<br><div><br><blockquote type="cite"><div>On May 18, 2023, at 10:19 AM, Michael D Kinney <michael.d.kinney@intel.com> wrote:</div><br class="Apple-interchange-newline"><div><meta charset="UTF-8"><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Andrew,<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">This might work for XIP.  Set non const global to initial value that is expected value to stay in dead loop.<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">UINTN  mDeadLoopCount = 0;<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">VOID<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">CpuDeadLoop(<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  VOID<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  )<span class="Apple-converted-space"> </span><o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">{<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  while (mDeadLoopCount == 0) {<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">      CpuPause();<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  }<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">}<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">When deadloop is entered, developer can not change value of mDeadLoopCount, but they can use debugger to force exit loop and return from function.<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Mike<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="border-width: medium medium medium 1.5pt; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor blue; border-image: none; padding: 0in 0in 0in 4pt;"><div><div style="border-width: 1pt medium medium; border-style: solid none none; border-color: rgb(225, 225, 225) currentcolor currentcolor; border-image: none; padding: 3pt 0in 0in;"><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><b>From:</b><span class="Apple-converted-space"> </span>Andrew (EFI) Fish <<a href="mailto:afish@apple.com" style="color: blue; text-decoration: underline;">afish@apple.com</a>><span class="Apple-converted-space"> </span><br><b>Sent:</b><span class="Apple-converted-space"> </span>Thursday, May 18, 2023 10:09 AM<br><b>To:</b><span class="Apple-converted-space"> </span>Kinney, Michael D <<a href="mailto:michael.d.kinney@intel.com" style="color: blue; text-decoration: underline;">michael.d.kinney@intel.com</a>><br><b>Cc:</b><span class="Apple-converted-space"> </span>edk2-devel-groups-io <<a href="mailto:devel@edk2.groups.io" style="color: blue; text-decoration: underline;">devel@edk2.groups.io</a>>; Ni, Ray <<a href="mailto:ray.ni@intel.com" style="color: blue; text-decoration: underline;">ray.ni@intel.com</a>>; Rebecca Cran <<a href="mailto:rebecca@bsdio.com" style="color: blue; text-decoration: underline;">rebecca@bsdio.com</a>><br><b>Subject:</b><span class="Apple-converted-space"> </span>Re: [edk2-devel] CpuDeadLoop() is optimized by compiler<o:p></o:p></div></div></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Mike,<o:p></o:p></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Good point, that is why we are using the stack ….<o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">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 compiler can’t resolve. <o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Thanks,<o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Andrew Fish<o:p></o:p></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><br><br><o:p></o:p></div><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">On May 18, 2023, at 10:05 AM, Kinney, Michael D <<a href="mailto:michael.d.kinney@intel.com" style="color: blue; text-decoration: underline;">michael.d.kinney@intel.com</a>> wrote:<o:p></o:p></div></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Static global will not work for XIP<o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Mike<o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div><div style="border-width: medium medium medium 1.5pt; border-style: none none none solid; padding: 0in 0in 0in 4pt; border-color: currentcolor currentcolor currentcolor blue; border-image: none;"><div><div style="border-width: 1pt medium medium; border-style: solid none none; padding: 3pt 0in 0in; border-color: currentcolor; border-image: none;"><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><b>From:</b><span class="apple-converted-space"> </span>Andrew (EFI) Fish <<a href="mailto:afish@apple.com" style="color: blue; text-decoration: underline;">afish@apple.com</a>><span class="apple-converted-space"> </span><br><b>Sent:</b><span class="apple-converted-space"> </span>Thursday, May 18, 2023 9:49 AM<br><b>To:</b><span class="apple-converted-space"> </span>edk2-devel-groups-io <<a href="mailto:devel@edk2.groups.io" style="color: blue; text-decoration: underline;">devel@edk2.groups.io</a>>; Kinney, Michael D <<a href="mailto:michael.d.kinney@intel.com" style="color: blue; text-decoration: underline;">michael.d.kinney@intel.com</a>><br><b>Cc:</b><span class="apple-converted-space"> </span>Ni, Ray <<a href="mailto:ray.ni@intel.com" style="color: blue; text-decoration: underline;">ray.ni@intel.com</a>>; Rebecca Cran <<a href="mailto:rebecca@bsdio.com" style="color: blue; text-decoration: underline;">rebecca@bsdio.com</a>><br><b>Subject:</b><span class="apple-converted-space"> </span>Re: [edk2-devel] CpuDeadLoop() is optimized by compiler<o:p></o:p></div></div></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Mike,<o:p></o:p></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">I pinged 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’ve NOT gotten feedback from anyone on the spec committee yet. <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">If we move Index to a static global that would likely work around the compiler issue.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Thanks,<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Andrew Fish<o:p></o:p></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><br><br><br><o:p></o:p></div></div><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">On May 18, 2023, at 8:36 AM, Michael D Kinney <<a href="mailto:michael.d.kinney@intel.com" style="color: blue; text-decoration: underline;">michael.d.kinney@intel.com</a>> wrote:<o:p></o:p></div></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div><div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Hi Ray,<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">So the code generated does deadloop, but is just not easy to resume from as we have been able to do in the past.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">We use CpuDeadloop() for 2 purposes.  One is a terminal condition with no reason to ever continue.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">The 2<sup>nd</sup><span class="apple-converted-space"> </span>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.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">We may have to do a NASM implementation of CpuDeadloop() to make sure it meets both use cases.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Mike<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div style="border-width: medium medium medium 1.5pt; border-style: none none none solid; padding: 0in 0in 0in 4pt; border-color: currentcolor currentcolor currentcolor blue; border-image: none;"><div><div style="border-width: 1pt medium medium; border-style: solid none none; padding: 3pt 0in 0in; border-color: currentcolor; border-image: none;"><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><b>From:</b><span class="apple-converted-space"> </span>Ni, Ray <<a href="mailto:ray.ni@intel.com" style="color: blue; text-decoration: underline;">ray.ni@intel.com</a>><span class="apple-converted-space"> </span><br><b>Sent:</b><span class="apple-converted-space"> </span>Thursday, May 18, 2023 3:00 AM<br><b>To:</b><span class="apple-converted-space"> </span><a href="mailto:devel@edk2.groups.io" style="color: blue; text-decoration: underline;">devel@edk2.groups.io</a><br><b>Cc:</b><span class="apple-converted-space"> </span>Kinney, Michael D <<a href="mailto:michael.d.kinney@intel.com" style="color: blue; text-decoration: underline;">michael.d.kinney@intel.com</a>>; Rebecca Cran <<a href="mailto:rebecca@bsdio.com" style="color: blue; text-decoration: underline;">rebecca@bsdio.com</a>>; Ni, Ray <<a href="mailto:ray.ni@intel.com" style="color: blue; text-decoration: underline;">ray.ni@intel.com</a>><br><b>Subject:</b><span class="apple-converted-space"> </span>CpuDeadLoop() is optimized by compiler<o:p></o:p></div></div></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Hi,<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Starting from certain version of Visual Studio C compiler (I don’t have the exact version. I am using VS2019), CpuDeadLoop is now optimized quite well by compiler.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">The optimization is so “good” that it becomes harder for developers to break out of the deadloop.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">I copied the assembly instructions as below for your reference.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">The compiler does not generate instructions that jump out of the loop when the Index is not zero.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">So in order to break out of the loop, developers need to:<o:p></o:p></div></div></div><ol start="1" type="1" style="margin-bottom: 0in; margin-top: 0in;"><li class="MsoListParagraph" style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Manually adjust rsp by increasing 40<o:p></o:p></li><li class="MsoListParagraph" style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Manually “ret”<o:p></o:p></li></ol><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">I am not sure if anyone has interest to re-write this function so that compiler can be “fooled” again.<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Thanks,<br>Ray<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">=======================<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; Function compile flags: /Ogspy<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; File e:\work\edk2\MdePkg\Library\BaseLib\CpuDeadLoop.c<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">;              COMDAT CpuDeadLoop<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">_TEXT    SEGMENT<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Index$ = 48<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">CpuDeadLoop PROC                                                                    ; COMDAT<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; 26   : {<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">$LN12:<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  00000  48 83 ec 28         sub        rsp, 40                                ; 00000028H<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; 27   :   volatile UINTN  Index;<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; 28   :<span class="apple-converted-space"> </span><o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; 29   :   for (Index = 0; Index == 0;) {<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  00004  48 c7 44 24 30<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">               00 00 00 00        mov      QWORD PTR Index$[rsp], 0<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">$LN10@CpuDeadLoo:<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">; 30   :     CpuPause ();<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  0000d  48 8b 44 24 30   mov      rax, QWORD PTR Index$[rsp]<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  00012  e8 00 00 00 00   call        CpuPause<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">  00017  eb f4                     jmp       SHORT $LN10@CpuDeadLoo<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">CpuDeadLoop ENDP<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">_TEXT    ENDS<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">END<o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"> <o:p></o:p></div></div></div></div><div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p></o:p></div></div></div></div></blockquote></div></div></div></div></blockquote></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div></div></div></div></div></blockquote></div><br></div></body></html>


<div width="1" style="color:white;clear:both">_._,_._,_</div>
<hr>


Groups.io Links:<p>


  
    You receive all messages sent to this group.
  
  


<p>
<a target="_blank" href="https://edk2.groups.io/g/devel/message/105044">View/Reply Online (#105044)</a> |


  

|

  <a target="_blank" href="https://groups.io/mt/98987896/1813853">Mute This Topic</a>

| <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>




<a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> |
<a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |

<a href="https://edk2.groups.io/g/devel/leave/3943202/1813853/130120423/xyzzy">Unsubscribe</a>

 [edk2-devel-archive@redhat.com]<br>
<div width="1" style="color:white;clear:both">_._,_._,_</div>