Zicfiss: sspopchk raises a software-check exception where a store/AMO access fault must take priority

When sspopchk targets a page whose permission check fails, QEMU reports a software-check exception (cause 18, tval = 3 "shadow stack fault") instead of the store/AMO access fault (cause 7). The permission check fails before the load can complete, so the value comparison that produces a software check must never happen.

The unprivileged spec is explicit that the comparison loses this race:

[norm:sspop_exception] If the value loaded from the address in ssp does not match the value in rs1, a software-check exception (cause=18) is raised with xtval set to "shadow stack fault (code=3)". The software-check exception caused by sspopchk or c.sspopchk is lower in priority than a load/store/AMO access-fault exception.

Environment

  • QEMU: 11.0.50 (v11.0.0-2506-g30e8a06b64)
  • Toolchain: riscv64-unknown-elf-gcc (g5115c7e44) 15.2.0

Reproducer

ss_fault.S builds with -DSCENARIO=3. In M-mode it enables Zicfiss (menvcfg.SSE/senvcfg.SSE), installs a permissive PMP entry, builds an Sv39 mapping where VA 0x140300000 points at an ordinary read/write page (pte.xwr = 011, not a shadow stack page), then drops to S-mode and does:

    li      t0, 0x140300800
    csrw    ssp, t0             # ssp -> ordinary RW page
    li      x1, 0x0BADF00D      # mismatches whatever would be loaded
    sspopchk x1

Because the page is not an SS page and not read-only, [norm:ssmp_ss_page_illegeal_access] requires a store/AMO access fault. The mismatching x1 is there deliberately so that a software check is also "available" — that is what makes this a priority test.

riscv64-unknown-elf-gcc -march=rv64ima_zicsr_zicfiss_zimop -mabi=lp64 \
    -nostdlib -mcmodel=medany -O0 -g -T link.ld -DSCENARIO=3 -o ss_fault_s3.elf ss_fault.S

qemu-system-riscv64 -nographic -semihosting -icount shift=1 -machine virt \
    -cpu max,smpmpmt=false,pmu-mask=0xfffffff8,smdbltrp=true,ssdbltrp=true \
    -bios ss_fault_s3.elf -d int -D qemu_s3.log

QEMU's -d int line:

riscv_cpu_do_interrupt: hart:0, async:0, cause:0000000000000012, epc:0x80000128,
                        tval:0x0000000000000003, desc=sw_check

Attached

qemu-zicfiss-bug.zip