RISC - V

RISC - V (RISC - Five)

  • RISC-V is the open source ISA available that you are free to use in your designs. No licenses, royalty, etc. Website for RISC-V is here: https://riscv.org/
  • It only defines ISA. How to implement the core or the processor using this ISA is entirely up to the person using the ISA, there's nothing dictated by RISC-V
  • Many cores have been implemented using this ISA. You can download source code for Cores, SOC or even buy the processors which are built using this ISA. Link to download here: https://github.com/riscv/riscv-cores-list
  • SiFive is one the front running company building processors based on RISC-V ISA. Lot of info on: https://sifive.com
  • A video on basics of  RISC-V ISA: https://www.youtube.com/watch?v=m8DqCTogb8w

 

RISC-V ISA:

Official RISC-V ISA manual: https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf

Very good ISA intro => https://www.allaboutcircuits.com/technical-articles/introductions-to-risc-v-instruction-set-understanding-this-open-instruction-set-architecture/

RISC-V ISA is extensible so that it can be used for very basic microcontrollers, and also for complex super computers. In order to figure out what extension of ISA we are working with, we have a naming convention.

RV[###][ab..yz] => RV means RISC-V arch, [###] indicates width of reg file and size of user addr space, it may be 32, 64 or 128 bits. [ab...yz] indicates the set of extensions supported by an implementaion.

Extensions supported are as below: Number of reg and their width depends on the extension.

  • I = Integer => This inst set is the only reqd extension in RISC-V, and defines 47 instructions.
  • M = Integer Multiplication and Division
  • G = General purpose which includes inst from I,M,A,F,D extensions
  • C = an alternative compressed 16 bit encoding for a special subset of existing inst.
  • A = Atomic extensions and many more std extension
  • Many non-std extensions allowed too, and denoted as Xext.

ex:

  • RV32I => Most basic RISC-V implementation. It's reg file is 32 bit, and supports Integer only.
  • RV64GCXext => 64 bit (G=general purpose IMAFDC, C=16 bit compressed inst) + a non std extension
  • RV32I/RV64I => they have 32 Int reg. F and D ext have 32 FP reg too. RV32E has only 16 integer reg for use in embedded devices.

RISC-V yields smallest code size for 32 and 64 bit processors compared to x86, ARM, MIPS, etc. RV32C or compressed version of ISA is the smallest code.

 

ISA Format and Reg File:

Reg: x0-x31. x0 is a "zero reg" hard wired to 0. NOTE: The ABI (application Binary i/f) names of the reg are different, and shown in link above.

Instruction format: Different Extensions have different set of inst, but mandatory 47 inst in RV32I need to be supported by all implementations.

RV32I: There are 47 Base Integer ISA cmds (encoded in 32 bits). RV32I defines six layouts of the same 32 bits.Map is here:

 

 

Instr. type Description Immediate value encoding
R-type register-register ALU ops None
I-type register-immediate integer ALU ops and JALR. 12 bits, sign expansion
S-type store 12 bits, sign expansion
B-type branch 12 bits, sign expansion, upper [31:1] (bit 0 is 0)
U-type LUI,AUIPC 20 bits, upper 31:12 (bits [11:0] are 0)
J-type JAL

12 bits, sign expansion, upper [31:1] (bit 0 is 0)

 

All inst for RV32 shown here:  

Above inst can be classified into few basic operations.

  • ALU: There are 2 variants: Reg to reg op, or reg and immediate op.
    • Logic: AND, OR, XOR,Shift (Left/right)
    • Maths: ADD, SUB (Even though Subtraction is just ADD with 2's complement of an operand, it's provided as an inst. MUL/DIV and REM (remainder of integer div) are provided as extra op in extended ISA)
  • Memory: Load and store (byte, halfword and word variants)
    • Load: Load is from memory to reg (5 variants), 
    • Store: store is from reg to memory (3 variants)
  • Branch: 6 branch variants are conditional jumps, that depend on a test on two registers.
  • Jump: Jump is put separately from Branch inst, as it relates to modifying the PC (pgm counter). JAL, JALR and AUIPC are the inst here.

More inst:

  • FENCE inst form a barrier. All inst preceeding FENCE must complete before proceeding. These are used t enforce program order.
  • CSR (ctl and Status reg) inst. These reg are sitting in processor, and are used by ecall/ebreak inst. 12 bit addr available, so max of 4096 such reg possible.
    • MM has ~17 CSR. (ex: Misa(m/c ISA reg), mstatus(m/c status), etc)
  • Inst in other extensions:
    • AMO (atomic mem op) perform read-modify write in a single atomic op. These are only in the A extension of ISA
    • ECALL/EBREAK: These are used for environment call and environment break to change privilege modes. These inst are needed only when we need pieces of code running in multiple modes.
      • Modes are privilege levels supported by RISC-V. We have 3 modes. These modes are also put as levels which correspond to diff combo of modes -  level 1 to level 4.
        • Mode 3 or Machine mode (MM) is the highest privilege mode and the only required mode.
        • Mode 2=Hypervisor mode,
        • Mode 1=Supervisor mode (SM). Larger processors running Linux use SM for the OS kernel instead of MM.
        • Mode 0=User/application mode.
      • At Power-On (Boot): Every RISC-V processor starts in MM (highest privilege). In this mode, all hardware locks are open. 
      • Bare Metal Embedded systems: Many small microcontrollers stay in Machine Mode forever. In that setup, your application and drivers are just one big program, you use normal function calls.
      • Protected Systems (RTOS or Linux): Here there's an OS running and the user pgms that are run via the OS. We want to protect boot/OS memory from ever getting corrupted by the user pgm. Before running untrusted application code, the boot code configures the hardware locks (PMP/MMU) to hide sensitive flash/RAM addresses. All this sensitive work can only be done in MM. We have 2 bit privilege mode (bits [12:11]) called as MPP (machine previous privilege) in mstatus CSR reg. It has a value of 11 at powerup, and this value is also the one copied into the CPU's hidden internal privilege-mode register (we will call this current mode (CM) reg as they denote current mode). So, while the OS is running, all these locks can be modified, as it's in MM mode, but once it switches to user mode, no further changes can be made and locks ar enforced so that user code can't write into boot/OS code This is the seq of steps when switching modes:
        • MM mode to User mode: When user pgm is called, the OS writes 00 to MPP. Then it writes the starting memory address of the user program into the mepc (Machine Exception Program Counter) register. Then OS executes "mret" inst, which switches it to user mode => it copies 00 from MPP directly into CM which instantly switches the core into User Mode. Then it copies mepc reg into the Program Counter causing the very next instruction fetch to happen in User Mode.
        • User mode to MM mode: When user-space programs request privileged services from an operating system or hypervisor, a trap is fired in user mode (due to ecall inst (to print on screen, access hardware, etc), fault, interrupt), then cpu pauses current inst and executes 5 atomic updates in a single cycle. Basically, the hardware copies the address of the interrupted instruction into the register so the OS knows where to return later. It copies current mode from CM into MPP bits so that mret knows which mode to restore to, once OS finishes. It flips CM bits to 11 which switches it to MM and copies the address stored in mtvec reg into PC reg. The very next instruction fetched by the CPU is the first instruction of the OS trap handler, now running with full Machine Mode privileges.

 

Memory/Interrupts: 

RISC-V also supports Virtual memory allowing for sophisticated mem mgmt and OS ctl (as Linux, etc). This requires an S-Mode implementation - Sv32, Sv39, Sv48 corresponding to 32 bit, 39 bit and 48 bit virtual mem. Physical mem protection is also supported for up to 16 regions, where we can restrict access to those regions.

RISC-V supports interrupts. CSR reg used to enable/disable specific interrupts or to see the status of any interrupt.