intel / MultiArchUefiPkg

Multi-Architecture UEFI Environment Driver
GNU Lesser General Public License v2.1
54 stars 12 forks source link

Investigate code size for RISCV64 build vs AARCH64 #2

Closed andreiw closed 11 months ago

andreiw commented 1 year ago

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.

andreiw commented 1 year ago

https://github.com/intel/MultiArchUefiPkg/commit/5a9c89b0bbdb7688e0369aeaea337eb9365ea924

LTO support depends on edk2 support for LTO on RISCV

andreiw commented 1 year ago

-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))
andreiw commented 1 year ago

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:

0 0x000000017e177d5e in RiscVCpuPause ()

1 0x000000017e177ab4 in CpuDeadLoop () at /home/andreiw/src/edk2/MdePkg/Library/BaseLib/CpuDeadLoop.c:30

2 0x000000017e11f8f0 in DebugAssert (FileName=FileName@entry=0x17e17a1a8 "/home/andreiw/src/edk2/MultiArchUefiPkg/Drivers/Emulator/Cpu.c",

LineNumber=LineNumber@entry=1242, Description=Description@entry=0x17e17a508 "Cpu->Contexts >= 0")
at /home/andreiw/src/edk2/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c:235

3 0x000000017e11d770 in CpuDetectOrphanContexts (Context=0x17ed60198) at /home/andreiw/src/edk2/MultiArchUefiPkg/Drivers/Emulator/Cpu.c:1242

4 CpuRunCtxOnPrivateStack (Context=Context@entry=0x17ed60198) at /home/andreiw/src/edk2/MultiArchUefiPkg/Drivers/Emulator/Cpu.c:1291

5 0x000000017e11dbee in CpuRunCtx (Context=Context@entry=0x17ed60198) at /home/andreiw/src/edk2/MultiArchUefiPkg/Drivers/Emulator/Cpu.c:1366

6 0x000000017e11dea6 in CpuRunImage (ImageHandle=0x17ed60e18, SystemTable=)

at /home/andreiw/src/edk2/MultiArchUefiPkg/Drivers/Emulator/Cpu.c:1517

7 0x000000008333f910 in CoreStartImage (ImageHandle=0x17ed60e18, ExitDataSize=0x0, ExitData=0x0)

at /home/andreiw/src/edk2/MdeModulePkg/Core/Dxe/Image/Image.c:1702

8 0x000000017e1b555a in InternalShellExecuteDevicePath (ParentImageHandle=0x17e2c8580 , DevicePath=0x17ed98018, CommandLine=0x17ed9b918,

Environment=0x0, StartImageStatus=0x83fff7f8) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/ShellProtocol.c:1619

9 0x000000017e1af714 in RunCommandOrFile (Type=Efi_Application, CmdLine=0x17ed9b918, FirstParameter=0x17ed98318, ParamProtocol=0x17ed9cf18,

CommandStatus=0x0) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:2648

10 0x000000017e1af808 in SetupAndRunCommandOrFile (Type=Efi_Application, CmdLine=0x17ed9b918, FirstParameter=0x17ed98318, ParamProtocol=0x17ed9cf18,

CommandStatus=0x0) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:2733

11 0x000000017e1afb46 in RunShellCommand (CmdLine=0x17ed71018, CommandStatus=0x0) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:2858

12 0x000000017e1afc3c in RunCommand (CmdLine=0x17ed71018) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:2911

13 0x000000017e1ada18 in DoShellPrompt () at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:1456

14 0x000000017e1ac428 in UefiMain (ImageHandle=0x17f3de018, SystemTable=0x17fffe018) at /home/andreiw/src/edk2/ShellPkg/Application/Shell/Shell.c:651

15 0x000000017e1ab10a in ProcessModuleEntryPointList (ImageHandle=0x17f3de018, SystemTable=0x17fffe018)

at /home/andreiw/src/edk2/Build/RiscVVirtQemu/DEBUG_GCC5/RISCV64/ShellPkg/Application/Shell/Shell/DEBUG/AutoGen.c:1148

16 0x000000017e1aa298 in _ModuleEntryPoint (ImageHandle=0x17f3de018, SystemTable=0x17fffe018)

at /home/andreiw/src/edk2/MdePkg/Library/UefiApplicationEntryPoint/ApplicationEntryPoint.c:58

17 0x000000008333f910 in CoreStartImage (ImageHandle=0x17f3de018, ExitDataSize=0x17ef0bc98, ExitData=0x17ef0bc90)

at /home/andreiw/src/edk2/MdeModulePkg/Core/Dxe/Image/Image.c:1702

18 0x000000017fcec90a in EfiBootManagerBoot (BootOption=0x17ef0bc48) at /home/andreiw/src/edk2/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c:2039

19 0x000000017fcde0fe in BootBootOptions (BootOptions=0x17ef0bb98, BootOptionCount=3, BootManagerMenu=0x83fffc08)

at /home/andreiw/src/edk2/MdeModulePkg/Universal/BdsDxe/BdsEntry.c:414

20 0x000000017fcdf46a in BdsEntry (This=0x17fd05f88 ) at /home/andreiw/src/edk2/MdeModulePkg/Universal/BdsDxe/BdsEntry.c:1094

21 0x000000008333b5d8 in DxeMain (HobStart=0x17fa53018) at /home/andreiw/src/edk2/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c:575

22 0x000000008333a6a2 in ProcessModuleEntryPointList (HobStart=0x82000000)

at /home/andreiw/src/edk2/Build/RiscVVirtQemu/DEBUG_GCC5/RISCV64/MdeModulePkg/Core/Dxe/DxeMain/DEBUG/AutoGen.c:590

23 0x000000008333a266 in _ModuleEntryPoint (HobStart=0x82000000) at /home/andreiw/src/edk2/MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.c:46

24 0x00000000200062f0 in ?? ()

...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...

andreiw commented 11 months ago

Fix has been upstreamed.