Skip to content

csr: validate CSRRS/CSRRC writes by register index - #299

Merged
LekKit merged 1 commit into
LekKit:stagingfrom
carlosqwqqwq:fix/csr-readonly-rs1-index
Oct 12, 2026
Merged

LekKit merged 1 commit into
LekKit:stagingfrom
carlosqwqqwq:fix/csr-readonly-rs1-index

Conversation

@carlosqwqqwq

@carlosqwqqwq carlosqwqqwq commented Sep 5, 2026 •

Copy link
Copy Markdown

csr: validate CSRRS/CSRRC writes by register index

Fixes #293

Commit message

csr: validate CSRRS/CSRRC writes by register index

Description

The CSR helper decides whether CSRRS/CSRRC writes from the runtime value loaded from rs1. A nonzero rs1 register containing zero is therefore mistakenly treated as a read, allowing writes to read-only time. Carry the architectural write-intent bit from the decoder into the helper: only rs1=x0/zimm=0 forms are read-only accesses.

The write-intent bit now also reaches the shared riscv_csr_topei() AIA path; it no longer reconstructs intent from the source value and operation code.

Validation

  • Native RISC-V hardware and QEMU trap csrrs/csrrc with rs1 = t0 (value zero) against read-only time and accept only the canonical rs1 = x0 read form; the patched build matches both.

The revised patch was applied to current staging and rebuilt with -Werror. The write-form witness traps in both RVVM execution modes with the expected illegal-instruction cause and CSR instruction tval; the canonical rs1=x0 read form remains covered by the existing reference witness.

@LekKit

LekKit commented Sep 22, 2026

Copy link
Copy Markdown
Owner

The riscv_csr_topei() helper still uses riscv_is_csr_write() instead of the write argument. This should be fixed, then riscv_is_csr_write() can be dropped as its sole caller is gone. Also worth renaming write -> is_write because it collides with a POSIX function name otherwise.

@carlosqwqqwq
carlosqwqqwq force-pushed the fix/csr-readonly-rs1-index branch from cfdba22 to 4332526 Compare October 8, 2026 14:10
@LekKit
LekKit merged commit e7214ef into LekKit:staging Oct 12, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSRRS/CSRRC to read-only time accepted when rs1 != x0 holds zero

2 participants