I am testing a program on a RISC-V core in baremetal. I am using no FPGA but running it in QuestaSim (RTL simulation).
The program consists of the main launching a function.
The problem is that when the function is supposed to return to main, an illegal instruction rises:
Error: Illegal instruction 0x00010413 at PC 0x00000004!
I have inspected:
- Core traces - Here I found this beauty when the execution is failing. From my understanding, the jalr instruction near the end is supposed to return the main, but x1 holds a zero...
78923501 78909 1c0023e6 00008067 jalr x0, x1, 0 x1:1c000a8e
78926501 78912 1c000a8e 00000493 addi x9, x0, 0 x9=00000000
78927501 78913 1c000a92 0060006f jal x0, 6
78929501 78915 1c000a98 01012d03 lw x26, 16(x2) x26=00000000 x2:100041b0 PA:100041c0
78930501 78916 1c000a9a 01412c83 lw x25, 20(x2) x25=00000000 x2:100041b0 PA:100041c4
78931501 78917 1c000a9c 01812c03 lw x24, 24(x2) x24=00000000 x2:100041b0 PA:100041c8
78932501 78918 1c000a9e 01c12b83 lw x23, 28(x2) x23=00000000 x2:100041b0 PA:100041cc
78950501 78936 1c000aa0 02012b03 lw x22, 32(x2) x22=00000000 x2:100041b0 PA:100041d0
78951501 78937 1c000aa2 02412a83 lw x21, 36(x2) x21=00000000 x2:100041b0 PA:100041d4
78952501 78938 1c000aa4 02812a03 lw x20, 40(x2) x20=00000000 x2:100041b0 PA:100041d8
78953501 78939 1c000aa6 02c12983 lw x19, 44(x2) x19=00000000 x2:100041b0 PA:100041dc
78954501 78940 1c000aa8 00900533 add x10, x0, x9 x10=00000000 x9:00000000
78955501 78941 1c000aac 03012903 lw x18, 48(x2) x18=00000000 x2:100041b0 PA:100041e0
78956501 78942 1c000aae 03412483 lw x9, 52(x2) x9=00000000 x2:100041b0 PA:100041e4
78974501 78960 1c000ab0 03812403 lw x8, 56(x2) x8=00000000 x2:100041b0 PA:100041e8
78975501 78961 1c000ab2 03c12083 lw x1, 60(x2) x1=00000000 x2:100041b0 PA:100041ec
78976501 78962 1c000ab4 04010113 addi x2, x2, 64 x2=100041f0 x2:100041b0
78978501 78964 1c000ab6 00008067 jalr x0, x1, 0 x1:00000000
79000501 78986 00000000 0fb12e27 sw x59, 252(x2) f27:00000000 x2:100041f0 PA:100042ec
- Disassembled code - It confirms that jalr is returning to main (its the last instruction before the called function to terminate in the disassembled).
1c000ab6: 8082 ret
Possible issues:
- Function pointers + struct? I have implemented custom structs with parameters and function pointers for class-based behaviour. This might be the issue as I am not sure It is implemented following best practices 100%. Still, the code executes fine. An example:
void api_init(my_struct *_entity){
// -- init my function pointers
_entity->init_streams = (void *)my_init;
_entity->compute = (void *)my_compute;
The application instantiates an _entity, then calls these functions in the following way:
(*_entity)->my_compute();
- Stack? I am allocating lot of pointers to memory buffers, functions, etc. However, I tried to augment it in the linker script, but seems not to be real issue.
MY QUESTIONS
- Does anyone have any experience with this kind of issues?
- Can you suggest any method to debug this?
Thanks in advance to anyone will answer.
Have a nice day!