Adding a custom instruction in the RVI subset

Custom opcodes

In the existing instruction encoding, there are some opcodes available for custom instructions as shown in Figure 1.

_images/base_opcode.png

Figure 1 RISC-V base opcode map [risd]

Custom instructions in the RISC-V ISA

Figure 2 is a reminder base instructions format that can be found in the documentation.

_images/base_formats.png

Figure 2 RISC-V base instruction formats [risd]

Chapter 26 of the RISC-V specification [risd] includes several details about RISC-V extensions.

  • According to [risd], the base ISA is defined in a 30-bits space (bits 31-2 of the instruction).

  • A prefix refer to the bits to the right of an encoding space. In this case, the prefix of the standard base ISA is only 2-bits wide and usually equal to 11 (that is the reason why there is no inst[1:0] in Figure 1).

Example of the M extension

Here is the instructions set of the M extension:

_images/rv32m.png

Figure 3 RV32M Standard Extension [risd]

This extension is composed of 8 instructions in the R format (4 for multiplication, 4 for division). The opcode 0110011 is also used for some instructions of RVI :

  • Add and sub between registers: add, sub

  • “set if less than”: slt, sltu

  • Boolean arithmetics: xor, or, and

  • Shift-related operations: sll, srl, sra

RVM differs from RVI mainly due to different funct7 values. It is also interesting to see that neither RVI nor RVM uses custom opcodes shown in Figure 1 (conclusion should be the same for other ratified extensions): it confirms that it can be used for custom instructions.

Example of the cryptography extension

The study is based on the latest release available at the time of writing (Scalar Cryptograph v1.0.0-rc6, chapter 5 may be interesting for constant-time related projects). There are several extensions in this specification:

  • Zbkb for bit manipulations

  • Zbkc for carry-less multiply

  • Zbkx for crossbar permutation

  • Zknd for AES decryption

  • Zkne for AES encryption

  • Zknh for hash functions

  • Zksed for SM4 block cipher

  • Zksh for SM3 hash function

  • Zkr for entropy-related operations

A subset of instructions is shown in Figure 4.

_images/crypto.png

Figure 4 Crypto extension subset [risb]

In this case, two opcodes are used (0110011 and 0010011):

  • The first is similar to the M extension.

  • The second one is used for others RVI instructions such as addi, xori, slti and so on.

As for the M extension, it mainly relies on funct7 and funct3 to create new instructions.