Does anyone know why my code halts and raise error when in AA64,RV64,LA64 UEFI? lazarus / 64-FPC development ---------------------------------------------------------------------- TYDQ · 2024-08-19 10:32 My full pascal code is in https://github.com/TYDQSoft/UEFIPascalOS/tree/main(Contains my pascal OS code and elf2efi code) I have used my elf2efi utility (Pascal-based program running on linux,which programmed by me) to converts my elf file to UEFI pecoff executable.To simulate the elf file executation as a pecoff file,all position relation(Not absolute address) will not changed by this program. This is the my error pascal code segment about where the error occurs in LA64(LoongArch64,due to its error raising is friendly and according the error exception,I positioned this error directly on this code): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure e fi_initialize(InputImageHandle:efi_handle;InputSystemTable:Pefi_system _table);[public,alias:'EFI_INITIALIZE'];begin ParentImageHandle:=Input ImageHandle; GlobalSystemTable:=InputSystemTable;end;And that is where the error occurs in the main program(Start of the program to where the error occurs): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program uefiloader; {$MODE FPC} uses uefi,binarybase,bootconfig,graphics;    function efi_main(ImageHandle:efi_handle;systemtable:Pefi_system_table ):efi_status;{$ifdef cpux86_64}MS_ABI_Default;{$endif}{$ifdef cpui386}cdecl;{$endif}[public,alias:'_start'];var {For checking elf file}    sfsp:Pefi_simple_file_system_protocol;    fp:Pefi_file_protocol;    efsl:efi_file_system_list;    i,j,count,procindex:natuint;    status:efi_status;    finfo:efi_file_info;    finfosize:natuint;    proccontent:Pointer;    procsize:natuint;    partstr:PWideChar;    gpl:efi_graphics_list;    isgraphics:boolean;    loaderscreenconfig:screen_config;    {For elf structure}    header:elf64_header;    program_headers:^elf64_program_header;    LowAddress,HighAddress:qword;    PageCount:qword;    KernelRelocateBase:qword;    RelocateOffset:qword;    ZeroStart:^qword;    SourceStart,DestStart:^byte;    KernelEntry:Pointer;    bool:array[1..4] of boolean;    {For loading elf files}    initparam:Psys_parameter_item;    param:sys_parameter;  func:sys_parameter_function;    funcandparam:sys_parameter_function_and_parameter;    res:Pointer;    {For memory initializtion}    memorymap:efi_memory_map;    memoryavailable:efi_memory_result;    addressoffset:natuint;    allocaddress:natuint;begin   {Initialize the uefi loader} efi_initialize(ImageHandle,systemtable);I know FPC in other architecture uses default calling convention and UEFI in architecture which is not x64 uses this too,Using default calling convention is not wrong.However,while reading the assembly code generated,I was confused about why my efi_initialize have error after I convert the elf file to UEFI pecoff file.Does anyone know why my code raise the error(It is raised by UEFI internal debugger) and how can I fix it?(Due to my code cannot pass when in Aarch64,LoongArch,RiscV UEFI) ---------------------------------------------------------------------- TYDQ · 2024-08-21 12:25 Is it too hard for us to respond? ---------------------------------------------------------------------- Laksen · 2024-08-21 22:38 Maybe if you said what failed, and gave examples of the disassembly on the address where it failed? ---------------------------------------------------------------------- TYDQ · 2024-08-22 03:15 Quote from: Laksen on August 22, 2024, 12:38:59 amMaybe if you said what failed, and gave examples of the disassembly on the address where it failed? This is generated assembly code from fpc,I will mark where the error occurs: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .section .text.n_uefi_$$_efi_initialize$pointer$pefi_system_table,"ax"        .balign 8.globl  UEFI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE      .type   UEFI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE,@functionUE FI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE:.globl  EFI_INITIALIZE        .type   EFI_INITIALIZE,@functionEFI_INITIALIZE:.Lc14:# [2874] begin        addi.d  $sp,$sp,-32.Lc15:        st.d    $fp,$sp,16.Lc16:        addi.d  $fp,$sp,32.Lc17:# Var InputImageHandle located at $r3+0, size=OS_64# Var InputSystemTable located at $r3+8, size=OS_64        st.d    $a0,$sp,0        st.d    $a1,$sp,8# [2875] ParentImageHandle:=InputImageHandle; GlobalSystemTable:=InputSystemTable;        move    $t0,$sp        pcalau12i       $t1,%got_pc_hi20(TC_$UEFI_$$_PARENTIMAGEHANDLE)      ld.d    $t1,$t1,%got_pc_lo12(TC_$UEFI_$$_PARENTIMAGEHANDLE)        ld.d    $t0,$t0,0        st.d    $t0,$t1,0 #That is where the error occurs.        addi.d  $t1,$sp,8        pcalau12i       $t0,%got_pc_hi20(TC_$UEFI_$$_GLOBALSYSTEMTABLE)        ld.d    $t0,$t0,%got_pc_lo12(TC_$UEFI_$$_GLOBALSYSTEMTABLE)        ld.d    $t1,$t1,0        st.d    $t1,$t0,0# [2876] end;        addi.d  $sp,$fp,-32        ld.d    $fp,$sp,16AArch64 and Riscv64 UEFI internal debugger don't show the ImageBase and EntryPoint address,So I don't have the way to find out where the error occurs in disassembly. ---------------------------------------------------------------------- TYDQ · 2024-08-22 07:19 Quote from: Laksen on August 22, 2024, 12:38:59 amMaybe if you said what failed, and gave examples of the disassembly on the address where it failed? I have read the assembly in aarch64 target: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .section .text.n_uefi_$$_efi_console_get_max_row_and_max_column,"ax"        .balign 8.globl  UEFI_$$_EFI_CONSOLE_GET_MAX_ROW_AND_MAX_COLUMN      .type   UEFI_$$_EFI_CONSOLE_GET_MAX_ROW_AND_MAX_COLUMN,@functionUEFI_$ $_EFI_CONSOLE_GET_MAX_ROW_AND_MAX_COLUMN:.globl  EFI_CONSOLE_GET_MAX_ROW_AND_MAX_COLUMN        .type   EFI_CONSOLE_GET_ MAX_ROW_AND_MAX_COLUMN,@functionEFI_CONSOLE_GET_MAX_ROW_AND_MAX_COLUMN :.Lc90:// [3330] begin        stp     x29,x30,[sp, #-16]!.Lc91:      mov     x29,sp.Lc92:        sub     sp,sp,#64// Var maxc located at sp+0, size=OS_64// Var maxr located at sp+8, size=OS_64// Var maxcharsize located at sp+16, size=OS_64// Var rescolumn located at sp+24, size=OS_64// Var resrow located at sp+32, size=OS_64// Var maxcharindex located at sp+40, size=OS_64// Var status located at sp+48, size=OS_S64// Var i located at sp+56, size=OS_8// [3331] i:=0; status:=efi_success; maxcharsize:=2000; maxcharindex:=1; rescolumn:=80; resrow:=25;        strb    wzr,[sp, #56]        str   xzr,[sp, #48]        movz    x0,#2000        str     x0,[sp, #16]      movz    x0,#1        str     x0,[sp, #40]        movz    x0,#80      str     x0,[sp, #24]        movz    x0,#25        str     x0,[sp, #32]// [3332] GlobalSystemTable^.ConOut^.SetMode(GlobalSystemTable^.ConOut,0);      adrp    x0,TC_$UEFI_$$_GLOBALSYSTEMTABLE        add     x0,x0,:lo12:TC_$UEFI_$$_GLOBALSYSTEMTABLE        ldr     x0,[x0]      ldr     x0,[x0, #64] #that is where error occurs in assembly        movz    x1,#0        adrp    x2,TC_$UEFI_$$_GLOBALSYSTEMTABLE        add     x2,x2,:lo12:TC_$UEFI_$$_GLOBALSYSTEMTABLE        ldr     x2,[x2]        ldr     x2,[x2, #64]        ldr     x2,[x2, #32]      blr     x2 ---------------------------------------------------------------------- Laksen · 2024-08-22 09:48 On la64, what's the value of $t1 when it crashes? Does it match the value you have updated in the .got? ---------------------------------------------------------------------- TYDQ · 2024-08-22 10:15 Quote from: Laksen on August 22, 2024, 11:48:15 amOn la64, what's the value of $t1 when it crashes? Does it match the value you have updated in the .got? I don't know,the LoongArch UEFI internal debugger don't show me this.It show me just: PROGRESS CODE: V03058001 I0 CRMD   0xB0 PRMD   0x4 ECFG  0x800 ESTAT   0x40000 ERA    0xD85C458(The error position) BADV    0x0(The fault address) BADI 0x29C00184(The error machine code) PC 0x00000D85C458(The error position) ---------------------------------------------------------------------- Laksen · 2024-08-22 10:37 Attach a debugger and break on that address? ---------------------------------------------------------------------- TYDQ · 2024-08-22 11:17 Quote from: Laksen on August 22, 2024, 12:37:17 pmAttach a debugger and break on that address? UEFI has its internal debugger,but its function doesn't abundant. ---------------------------------------------------------------------- Laksen · 2024-08-22 12:17 If you are using QEMU you can easily use gdb ---------------------------------------------------------------------- TYDQ · 2024-08-22 13:15 Quote from: Laksen on August 22, 2024, 02:17:13 pmIf you are using QEMU you can easily use gdb I'm using libvirt and QEMU. ---------------------------------------------------------------------- Laksen · 2024-08-22 14:20 Okay that should work fine I guess. Try and search for how to use gdb with QEMU ---------------------------------------------------------------------- TYDQ · 2024-08-27 03:59 Quote from: Laksen on August 22, 2024, 04:20:28 pmOkay that should work fine I guess. Try and search for how to use gdb with QEMU However,after I edited my elf2efi,the error still occurs in RV64(Risc-V 64),other three platforms are running successfully. UEFI internal debugger told me this and this is where error occurs: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i .section .text.n_uefi_$$_efi_initialize$pointer$pefi_system_table,"ax"        .balign 8.globl  UEFI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE      .type   UEFI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE,@functionUE FI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE:.globl  EFI_INITIALIZE        .type   EFI_INITIALIZE,@functionEFI_INITIALIZE:.Lc8:# [2858] begin        addi    x2,x2,-16        sd      x1,8(x2)        sd      x8,0(x2)        addi    x8,x2,16        addi    x2,x2,-104# Var InputImageHandle located in register x10# Var InputSystemTable located in register x11# Var InputImageHandle located in register x10.La1:# [2859] ParentImageHandle:=InputImageHandle; GlobalSystemTable:=InputSystemTable;        auipc   x12,%got_pcrel_hi(TC_$UEFI_$$_PARENTIMAGEHANDLE)        ld      x12,%pcrel_lo(.La1)(x12)        sd      x10,0(x12) # where the error occurs# Var InputSystemTable located in register x11.La2:        auipc   x10,%got_pcrel_hi(TC_$UEFI_$$_GLOBALSYSTEMTABLE)        ld      x10,%pcrel_lo(.La2)(x10)        sd      x11,0(x10)# [2860] end;      ld      x8,104(x2)        ld      x1,112(x2)        addi    x2,x2,120        jalr    x0,x1.Lc7:.Le3:        .size   UEFI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE, .Le3 - UEFI_$$_EFI_INITIALIZE$POINTER$PEFI_SYSTEM_TABLE This is the error type: !!!! RISCV64 Exception Type - 000000000000000F(EXCEPT_RISCV_STORE_ACCESS_PAGE_FAULT) !!!!      t0 = 0x00000000000001000        t1 = 0x00000000083FFF620                                                                   t2 = 0x00000000000001000        t3 = 0x0FFFFFFFFBC62157E                                                   t4 = 0x00000000000003E33        t5 = 0x00000000000004FEC                                  t6 = 0x00000000000000004        s0 = 0x00000000083FFF610                   s1 = 0x00000000000000013        s2 = 0x00000000000000000                                                                                 s3 = 0x00000000000000000        s4 = 0x000000000BFE00000                                                                   s5 = 0x0000000017FFFE018        s6 = 0x08000000A00006800                                                   s7 = 0x00000000000000004        s8 = 0x00000000000002000                                  s9 = 0x00000000080043710       s10 = 0x00000000000000000                  s11 = 0x00000000000000000        a0 = 0x0000000017F2AB998                                                                                 a1 = 0x0000000017FFFE018        a2 = 0x00000000000000000                                                                   a3 = 0x00000000000000000        a4 = 0x0000000017FFFE018                                                   a5 = 0x0000000017E2DFA88        a6 = 0x00000000000000006                                  a7 = 0x0000000000000000E      zero = 0x00000000000000000                   ra = 0x0000000017E2DFAD0        sp = 0x0000000017FE14D78                                                                                 gp = 0x00000000000000000        tp = 0x0000000008004E000                                                                 sepc = 0x0000000017E2E06BC   sstatus = 0x08000000200006120                                                stval = 0x00000000000000000 ---------------------------------------------------------------------- Laksen · 2024-08-27 11:04 x12 is 0 Looks like there's a mix of pcrel and got_pcrel ---------------------------------------------------------------------- Laksen · 2024-08-27 15:34 Nevermind, that's probably not the issue. You need to dig deeper. I would advice setting a breakpoint with GDB. It sounds like that inbuilt debugger is quite tedious to work with ---------------------------------------------------------------------- TYDQ · 2024-08-28 03:54 Quote from: Laksen on August 27, 2024, 05:34:39 pmNevermind, that's probably not the issue. You need to dig deeper. I would advice setting a breakpoint with GDB. It sounds like that inbuilt debugger is quite tedious to work with OK,I will try to do this.According to UEFI spec,ImageHandle is in x10(a0),SystemTable is in x11(a1).This can't be errorous.I will dig into it deeper.