I was trying to make sense of this huge impact of unaligned memory access, and with Jessica’s hint that OpenSBI handles this i found the actual code that handles this: opensbi/sbi_misaligned_ldst.c at master · riscv-software-src/opensbi · GitHub
I’m trying to figure out what happens to see if i can explain the huge impact:
- The program tries to do an unaligned read/write
- This event gets noticed and trapped by the CPU, switches to M-mode
- The OpenSBI trap handler gets called (opensbi/sbi_trap.c at master · riscv-software-src/opensbi · GitHub)
- The trap handler determines the trap is because of misalignment, and calls the appropriate function
- This function “emulates” the misaligned access using aligned reads/writes and shifts
- CPU switches back to U-mode and program execution resumes
Is there anything i’ve missed?
I guess this already explains a lot