[edk2-devel] CpuDeadLoop() is optimized by compiler

Andrew Fish via groups.io afish=apple.com at groups.io
Thu May 18 17:24:01 UTC 2023


Mike,

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? 

Thanks,

Andrew Fish

> On May 18, 2023, at 10:19 AM, Michael D Kinney <michael.d.kinney at intel.com> wrote:
> 
> Andrew,
>  
> This might work for XIP.  Set non const global to initial value that is expected value to stay in dead loop.
>  
> UINTN  mDeadLoopCount = 0;
>  
> VOID
> CpuDeadLoop(
>   VOID
>   ) 
> {
>   while (mDeadLoopCount == 0) {
>       CpuPause();
>   }
> }
>  
> When deadloop is entered, developer can not change value of mDeadLoopCount, but they can use debugger to force exit loop and return from function.
>  
> Mike
>  
>  
> From: Andrew (EFI) Fish <afish at apple.com <mailto:afish at apple.com>> 
> Sent: Thursday, May 18, 2023 10:09 AM
> To: Kinney, Michael D <michael.d.kinney at intel.com <mailto:michael.d.kinney at intel.com>>
> Cc: edk2-devel-groups-io <devel at edk2.groups.io <mailto:devel at edk2.groups.io>>; Ni, Ray <ray.ni at intel.com <mailto:ray.ni at intel.com>>; Rebecca Cran <rebecca at bsdio.com <mailto:rebecca at bsdio.com>>
> Subject: Re: [edk2-devel] CpuDeadLoop() is optimized by compiler
>  
> Mike,
>  
> Good point, that is why we are using the stack ….
>  
> 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. 
>  
> Thanks,
>  
> Andrew Fish
> 
> 
> On May 18, 2023, at 10:05 AM, Kinney, Michael D <michael.d.kinney at intel.com <mailto:michael.d.kinney at intel.com>> wrote:
>  
> Static global will not work for XIP
>  
> Mike
>  
> From: Andrew (EFI) Fish <afish at apple.com <mailto:afish at apple.com>> 
> Sent: Thursday, May 18, 2023 9:49 AM
> To: edk2-devel-groups-io <devel at edk2.groups.io <mailto:devel at edk2.groups.io>>; Kinney, Michael D <michael.d.kinney at intel.com <mailto:michael.d.kinney at intel.com>>
> Cc: Ni, Ray <ray.ni at intel.com <mailto:ray.ni at intel.com>>; Rebecca Cran <rebecca at bsdio.com <mailto:rebecca at bsdio.com>>
> Subject: Re: [edk2-devel] CpuDeadLoop() is optimized by compiler
>  
> Mike,
>  
> 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. 
>  
> If we move Index to a static global that would likely work around the compiler issue.
>  
> Thanks,
>  
> Andrew Fish
> 
> 
> 
> On May 18, 2023, at 8:36 AM, Michael D Kinney <michael.d.kinney at intel.com <mailto:michael.d.kinney at intel.com>> wrote:
>  
> Hi Ray,
>  
> So the code generated does deadloop, but is just not easy to resume from as we have been able to do in the past.
>  
> We use CpuDeadloop() 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.
>  
> Mike
>  
> From: Ni, Ray <ray.ni at intel.com <mailto:ray.ni at intel.com>> 
> Sent: Thursday, May 18, 2023 3:00 AM
> To: devel at edk2.groups.io <mailto:devel at edk2.groups.io>
> Cc: Kinney, Michael D <michael.d.kinney at intel.com <mailto:michael.d.kinney at intel.com>>; Rebecca Cran <rebecca at bsdio.com <mailto:rebecca at bsdio.com>>; Ni, Ray <ray.ni at intel.com <mailto:ray.ni at intel.com>>
> Subject: CpuDeadLoop() is optimized by compiler
>  
> Hi,
> 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.
>  
> The optimization is so “good” that it becomes harder for developers to break out of 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 out of the loop, developers need to:
> Manually adjust rsp by increasing 40
> Manually “ret”
>  
> I am not sure if anyone has interest to re-write this function so that compiler can be “fooled” again.
> Thanks,
> Ray
>  
> =======================
> ; Function compile flags: /Ogspy
> ; File e:\work\edk2\MdePkg\Library\BaseLib\CpuDeadLoop.c
> ;              COMDAT CpuDeadLoop
> _TEXT    SEGMENT
> Index$ = 48
> CpuDeadLoop PROC                                                                    ; COMDAT
>  
> ; 26   : {
>  
> $LN12:
>   00000  48 83 ec 28         sub        rsp, 40                                ; 00000028H
>  
> ; 27   :   volatile UINTN  Index;
> ; 28   : 
> ; 29   :   for (Index = 0; Index == 0;) {
>  
>   00004  48 c7 44 24 30
>                00 00 00 00        mov      QWORD PTR Index$[rsp], 0
> $LN10 at CpuDeadLoo:
>  
> ; 30   :     CpuPause ();
>  
>   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 at CpuDeadLoo
> CpuDeadLoop ENDP
> _TEXT    ENDS
> END
>  
>  
>  
> 



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#105044): https://edk2.groups.io/g/devel/message/105044
Mute This Topic: https://groups.io/mt/98987896/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3943202/1813853/130120423/xyzzy [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/edk2-devel-archive/attachments/20230518/6e797e23/attachment-0001.htm>


More information about the edk2-devel-archive mailing list