Skip to content

loader: set exception handler for RISC-V#484

Merged
Ivan-Velickovic merged 1 commit intomainfrom
riscv_loader_exception_handler
Apr 30, 2026
Merged

loader: set exception handler for RISC-V#484
Ivan-Velickovic merged 1 commit intomainfrom
riscv_loader_exception_handler

Conversation

@Ivan-Velickovic
Copy link
Copy Markdown
Collaborator

Closes #480.

#include "../cutil.h"
#include "../uart.h"

#if __riscv_xlen == 32
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we bother doing this when everything else assumes 64-bit?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did it since it's trivial

Comment thread loader/src/riscv/exceptions.c Outdated

static inline void write_stvec(void *value)
{
asm volatile("csrw stvec, %0" :: "rK"(value));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"rK" here means A 5-bit unsigned immediate for CSR access instructions.. Was there a reason why rK was used?

Also, don't these need a : "memory" barrier otherwise the compile is free to reorder them?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer: "rK" means register or 5-bit immediate; binutils in opcodes/riscv-opc.c defines:

{"csrw",       0, INSN_CLASS_ZICSR, "E,s",   MATCH_CSRRW, MASK_CSRRW|MASK_RD, match_opcode, INSN_ALIAS },
{"csrw",       0, INSN_CLASS_ZICSR, "E,Z",   MATCH_CSRRWI, MASK_CSRRWI|MASK_RD, match_opcode, INSN_ALIAS },

so it is clever enough to encode a csrw in either the csrrw (register) variant or the csrrwi (immediate) variants based on which kind the %0 fits in.

	    case 'Z': /* CSRRxI immediate.  */
	      my_getExpression (imm_expr, asarg, force_reloc);
	      check_absolute_expr (ip, imm_expr, false);
	      if ((unsigned long) imm_expr->X_add_number > 31)
		as_bad (_("improper CSRxI immediate (%"PRIu64")"),
			imm_expr->X_add_number);
	      INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
	      imm_expr->X_op = O_absent;
	      asarg = expr_parse_end;
	      continue;

So this is GCC magically handling immediates as an optimisation.

Signed-off-by: Ivan Velickovic <i.velickovic@unsw.edu.au>
@Ivan-Velickovic Ivan-Velickovic force-pushed the riscv_loader_exception_handler branch from cb1220a to be97d8a Compare April 30, 2026 01:48
@Ivan-Velickovic Ivan-Velickovic merged commit e81cffb into main Apr 30, 2026
11 checks passed
@Ivan-Velickovic Ivan-Velickovic deleted the riscv_loader_exception_handler branch April 30, 2026 01:58
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.

riscv64: feature-request: page fault handler

2 participants