Why does the BIOS entry point start with a WBINVD instruction?

前端 未结 3 1632
一个人的身影
一个人的身影 2021-02-07 09:03

I\'m investigating the BIOS code in my machine (x86_64 Linux, IvyBridge). I use the following procedure to dump the BIOS code:

$ sudo cat /proc/iomem | grep ROM
         


        
3条回答
  •  时光说笑
    2021-02-07 09:34

    Albeit hard to reason about, remember that the load mov al, byte es:[0xfff0] is not reading from the the BIOS first instruction, even though es is set to 0xf000.

    The first instruction is read from 0xfffffff0, the PCH will also probably alias 0xf0000-0xfffff to 0xffff0000-0xffffffff at reset, so when the BSP is booted it will execute the code you dumped.
    IIRC, the APs don't boot unless explicitly waken up.

    The BSP will then will proceed with initialising the HW (judging from the dump).
    At some point it will set the attribute map for the 0xf0000-0xfffff to steer reads and writes (or just writes and then reads) to memory.
    The end result is that when a processor (an HW thread) boots it will execute the code from the flash until it perform a far jump.
    At the point the cs base is correctly computed as per real-mode rules (pretty much like the unreal mode) and the instruction will be fetched from the 0xf0000-0xfffff (i.e. from the RAM).
    All of this while the cs segment value didn't actually change.

    The BSP at some point will start its multiprocessor initialisation routine, where it broadcasts to everyone (including himself) an INIT-SIPI-SIPI that will result in a sleep for the APs and a ljmp 0xf000:0xfff0 for the BSP.
    The trick here is that the target of the jump, 0xf000:0xfff0, is not the same bus address of the wbinvd instruction.
    There could be something else there, probably another initialisation routine.

    At the end of the initialisation the BIOS could simply reset the attributes of the 0xf0000-0xfffff to fall through to the flash (so a software reset is possible), preventing (not intentionally) a dump of the intermediary code.

    This is not very efficient, but BIOSes are not usually masterpieces of code.

    I don't have enough element to be sure what's going on, my point is that the ljmp 0xf000:0xfff0 and the mov al, byte es:[0xfff0] doesn't have to read from the same region they reside in.
    With this in mind, all bets are off.
    Only a proper reverse engineering will tell.

    Regarding the wbinvd, I suggested in the comment it could be related to the warm boot facility and Peter Cordes suggested that it may specifically have to do with cache-as-RAM.
    It makes sense, I guess will never be sure though.
    It could as well be a case of cargo cult, where a programmer deemed the instruction necessary based rumors.

提交回复
热议问题