Page MenuHomeFreeBSD

Trim duplicate CSR swaps from user exceptions.
ClosedPublic

Authored by jhb on Jan 28 2020, 11:18 AM.
Tags
None
Referenced Files
F154337001: D23394.id67534.diff
Mon, Apr 27, 10:45 PM
Unknown Object (File)
Sat, Apr 25, 5:09 PM
Unknown Object (File)
Sat, Apr 25, 1:24 PM
Unknown Object (File)
Fri, Apr 10, 2:11 AM
Unknown Object (File)
Mar 29 2026, 6:36 AM
Unknown Object (File)
Mar 28 2026, 4:28 PM
Unknown Object (File)
Mar 16 2026, 12:09 AM
Unknown Object (File)
Mar 15 2026, 1:21 AM
Subscribers

Details

Summary

The stack pointer is swapped with the sscratch CSR just before the
jump to cpu_exception_handler_user where the first instruction swaps
it again. The two swaps together are a no-op, but the csr swap
instructions can be expensive (e.g. on Bluespec RISC-V cores csr swap
instructions force a full pipeline stall).

Test Plan
  • booted in qemu

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

This revision is now accepted and ready to land.Jan 28 2020, 11:39 AM
sys/riscv/riscv/exception.S
209 ↗(On Diff #67394)

So we check that the sscratch register is empty as a means of determining which mode we came from?

I think this would be better as a check of the SPP bit in sstatus, because it looks like you could eliminate another double sscratch swap in the supervisor case.

sys/riscv/riscv/exception.S
209 ↗(On Diff #67394)

Yes, checking sscratch in this way is pretty typical. You can't check sstatus because you haven't yet saved any GPRs anywhere, so you can't do a 'csrr` to get the value of sstatus and check it. The scheme is that the kernel clears sscratch so that nested kernel exceptions see a 0 value. bbl does the same thing with mscratch FWIW, and I suspect it's probably what Linux does as well. There aren't really any other options when you only have the single scratch register.

This revision was automatically updated to reflect the committed changes.