Closed andreiw closed 11 months ago
https://github.com/intel/MultiArchUefiPkg/commit/5a9c89b0bbdb7688e0369aeaea337eb9365ea924
LTO support depends on edk2 support for LTO on RISCV
-mrelax may be dangerous since gp isn't loaded anywhere (UEFI is built with -mno-relax). Worse, building with -Os causes a regression with the CpuExitImage test.
Somehow the RaiseTPL from CpuExitImage call is getting corrupted.
FATAL ERROR - RaiseTpl with OldTpl(0x1F) > NewTpl(0x10)
ASSERT [DxeCore] /home/andreiw/src/edk2/MdeModulePkg/Core/Dxe/Event/Tpl.c(66): ((BOOLEAN)(0==1))
Okay that assert is unrelated (it's caused by using a ConOut-based DebugLib with a high TPL, separate issue to file).
The real issue is due -Os implying -fomit-frame-pointer.
After calling CpuExitImage we hit a different assert with this backtrace:
LineNumber=LineNumber@entry=1242, Description=Description@entry=0x17e17a508 "Cpu->Contexts >= 0")
at /home/andreiw/src/edk2/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c:235
at /home/andreiw/src/edk2/MultiArchUefiPkg/Drivers/Emulator/Cpu.c:1517
at /home/andreiw/src/edk2/MdeModulePkg/Core/Dxe/Image/Image.c:1702
Environment=0x0, StartImageStatus=0x83fff7f8) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/ShellProtocol.c:1619
CommandStatus=0x0) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:2648
CommandStatus=0x0) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:2733
at /home/andreiw/src/edk2/Build/RiscVVirtQemu/DEBUG_GCC5/RISCV64/ShellPkg/Application/Shell/Shell/DEBUG/AutoGen.c:1148
at /home/andreiw/src/edk2/MdePkg/Library/UefiApplicationEntryPoint/ApplicationEntryPoint.c:58
at /home/andreiw/src/edk2/MdeModulePkg/Core/Dxe/Image/Image.c:1702
at /home/andreiw/src/edk2/MdeModulePkg/Universal/BdsDxe/BdsEntry.c:414
at /home/andreiw/src/edk2/Build/RiscVVirtQemu/DEBUG_GCC5/RISCV64/MdeModulePkg/Core/Dxe/DxeMain/DEBUG/AutoGen.c:590
...so basically SetJmp returns 0 after the LongJump in CpuExitImage, and we don't exit, but try to run. This is because LongJumpInternal implementation is broken and it just so happens that with -Os x8 is mostly 0 (at that point).
See for fix https://github.com/andreiw/edk2-rv-wip/commit/56871e7dc80c9090a695effcd51d632f7b2ebb20. Meanwhile 06753ee1fe8d3b4341041ea89987de2ba0142188 builds with -Os and -fno-omit-frame-pointer for now.
Keeping this open...
Fix has been upstreamed.
RISCV64 build is 2x the size of AARCH64 build, when build with comparable options.
The difference almost comes from the -Os compared to the aarch64 parts. The RISC-V parts will have an even smaller code size with the below options enabled.
Also, look into LTO.