Ld, sd alignment?

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:

  1. The program tries to do an unaligned read/write
  2. This event gets noticed and trapped by the CPU, switches to M-mode
  3. The OpenSBI trap handler gets called (opensbi/sbi_trap.c at master · riscv-software-src/opensbi · GitHub)
  4. The trap handler determines the trap is because of misalignment, and calls the appropriate function
  5. This function “emulates” the misaligned access using aligned reads/writes and shifts
  6. CPU switches back to U-mode and program execution resumes

Is there anything i’ve missed?

I guess this already explains a lot :slight_smile: