Lucas Morais
Lucas Morais

Reputation: 25

What Linux entity is responsible for generating Illegal Instruction Traps?

I am working on a custom version of Rocket Chip that features some extra instructions that I would like to be properly handled by Linux. Although bare-metal programs using these instructions run fine, Linux makes the same benchmarks crash with "Illegal Instruction" messages.

Does anyone know which software element of Linux - loader, disassembler, something else - is responsible for detecting illegal instructions?

My goal is to modify that piece of software so that Linux stops complaining about my instructions. If anyone knows about an easier way to suppress this kind of error, that would be very useful too.

Upvotes: 0

Views: 815

Answers (1)

Palmer Dabbelt
Palmer Dabbelt

Reputation: 1068

The RISC-V implementation (the processor) raises an illegal instruction trap whenever it encounters an instruction it has not implemented. These illegal instruction traps will be piped through to Linux (either via trap delegation or after being handled by the machine-mode software), which then flow through the standard trap handling flow:

  • strapvec points to Handle_exception, which does a bunch of bookkeeping to avoid trashing userspace and then direct traps to the correct location.
  • For illegal instruction traps, you'll fall through to the excp_vect_table jump table, which handles all the boring traps.
  • This is indexed by scause, which in this case points to do_trap_insn_illegal.
  • do_trap_insn_illegal is just a generic Linux trap handler, it passes SIGILL to whatever caused the trap. This may raise a signal to a userspace task, a kernel task, or just panic the kernel directly.

There are a bunch of levels of indirection here that we're currently not doing anything with, but may serve to emulate unimplemented instructions in the future.

Upvotes: 1

Related Questions