summaryrefslogtreecommitdiff
path: root/include/sbi/riscv_encoding.h
AgeCommit message (Collapse)Author
2026-06-16include: sbi: Add GET_RDS_NUM/SET(_FP32/_FP64)_RDS macrosBo Gan
These macros can be used to decode rd' and set rd' in RVC instructions Signed-off-by: Bo Gan <[email protected]> Reviewed-by: Anup Patel <[email protected]> Tested-by: Anirudh Srinivasan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2026-06-16include: sbi: Add more mstatus and instruction encodingBo Gan
- Add MXL encoding for calculating XLEN. - Add instruction encoding for c.lbu/c.sb, and imm encoding for multiple RVC insn. Signed-off-by: Bo Gan <[email protected]> Reviewed-by: Anup Patel <[email protected]> Tested-by: Anirudh Srinivasan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2026-05-09lib: sbi: Add Smrnmi extension macros for registers and bitsEvgeny Voevodin
Add CSR definitions (MNSCRATCH, MNSTATUS, MNEPC, MNCAUSE) and bit definitions (MNSTATUS_NMIE, MNSTATUS_MNPV, MNSTATUS_MNPP). Also add SBI_HART_EXT_SMRNMI to the hart extension enumeration. Signed-off-by: Evgeny Voevodin <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/1c6feb6d359b9827b3c2ad8f4f0e0a4dfd1de911.1778176768.git.evvoevod@tenstorrent.com Signed-off-by: Anup Patel <[email protected]>
2026-02-20firmware: Initialize stack guard via ZkrXiang W
Try to initialize stack protection guard via the zkr extension. Signed-off-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-12-15include: sbi: Ignore rs1 and rd fields in FENCE.TSO.Benedikt Freisen
While FENCE.TSO is only specified with them set to zero, it is a special case of FENCE, which needs to ignore these otherwise reserved fields, but in some implementations, namely XuanTie C906 and C910, apparently does not. See the RISCVuzz paper by Thomas et al. for details. Signed-off-by: Benedikt Freisen <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-12-15include: sbi: Fix tab alignment.Benedikt Freisen
A previous editor or formatter script appears to have been confused by a diff view, where the prepended + or - changes the way tabs are displayed. Since it is the file itself that matters, adjust that accordingly. Signed-off-by: Benedikt Freisen <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-12-08lib: sbi: Enable Ssqosid Ext using mstateen0Chen Pei
The QoS Identifiers extension (Ssqosid) introduces the srmcfg register, which configures a hart with two identifiers: a Resource Control ID (RCID) and a Monitoring Counter ID (MCID). These identifiers accompany each request issued by the hart to shared resource controllers. If extension Smstateen is implemented together with Ssqosid, then Ssqosid also requires the SRMCFG bit in mstateen0 to be implemented. If mstateen0.SRMCFG is 0, attempts to access srmcfg in privilege modes less privileged than M-mode raise an illegal-instruction exception. If mstateen0.SRMCFG is 1 or if extension Smstateen is not implemented, attempts to access srmcfg when V=1 raise a virtual-instruction exception. This extension can be found in the RISC-V Instruction Set Manual: https://github.com/riscv/riscv-isa-manual Changes in v5: - Remove SBI_HART_EXT_SSQOSID dependency SBI_HART_PRIV_VER_1_12 Changes in v4: - Remove extraneous parentheses around SMSTATEEN0_SRMCFG Changes in v3: - Check SBI_HART_EXT_SSQOSID when swapping SRMCFG Changes in v2: - Remove trap-n-detect - Context switch CSR_SRMCFG Signed-off-by: Chen Pei <[email protected]> Reviewed-by: Radim Krčmář <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-10-21lib: Allow custom CSRs in csr_read_num() and csr_write_num()Anup Patel
Some of the platforms use platform specific CSR access functions for configuring implementation specific CSRs (such as PMA registers). Extend the common csr_read_num() and csr_write_num() to allow custom CSRs so that platform specific CSR access functions are not needed. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-07-22include: sbi: Remove unused (LOG_)REGBYTESJessica Clarke
These are no longer used, so remove them. Signed-off-by: Jessica Clarke <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-07-22include: sbi: Use array for struct sbi_trap_regs and GET/SET macrosJessica Clarke
Rather than hand-rolling scaled pointer arithmetic with casts and shifts, let the compiler do so by indexing an array of GPRs, taking advantage of the language's type system to scale based on whatever type the register happens to be. This makes it easier to support CHERI where the registers are capabilities, not plain integers, and so this pointer arithmetic would need to change (and currently REGBYTES is both the size of a register and the size of an integer word upstream). Signed-off-by: Jessica Clarke <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-05-20lib: Emulate AMO instructions when Zaamo is not availableChao-ying Fu
The AMO instructions are very critical for Linux so allow low-end RISC-V implementations without Zaamo to boot Linux by emulating AMO instructions using Zalrsc when OpenSBI is compiled without Zaamo. Signed-off-by: Chao-ying Fu <[email protected]> Signed-off-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-13lib: sbi: Enable Control Transfer Records (CTR) Ext using xstateen.Rajnesh Kanwal
The Control Transfer Records (CTR) extension provides a method to record a limited branch history in register-accessible internal chip storage. This extension is similar to Arch LBR in x86 and BRBE in ARM. The Extension has been stable and the latest release can be found here https://github.com/riscv/riscv-control-transfer-records/release Signed-off-by: Rajnesh Kanwal <[email protected]> Reviewed-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-04-13lib: sbi_trap: Add support for vectored interruptsSamuel Holland
When redirecting an exception to S-mode, transform the (v)stvec CSR value as described in the privileged spec to derive the S-mode PC. Since OpenSBI never redirects interrupts, only synchronous exceptions, the only action needed is to mask out the (v)stvec.MODE field. Reported-by: Jan Reinhard <[email protected]> Closes: https://github.com/riscv-software-src/opensbi/issues/391 Signed-off-by: Samuel Holland <[email protected]> Reviwed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-03-27lib: sbi: sse: Add support for SSTATUS.SDTClément Léger
Similarly to what is done for SPELP, handle SSTATUS.SDT upon event injection. In order to mimick an interrupt, set SDT to 1 for injection and save its previous value in interrupted_flags[5:5]. Restore it upon completion. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Andrew Jones <[email protected]>
2024-12-06lib: sbi_misaligned_ldst: Add handling of vector load/storeNylon Chen
Add misaligned load/store handling for the vector extension to the sbi_misaligned_ldst library. This implementation is inspired from the misaligned_vec_ldst implementation in the riscv-pk project. Co-developed-by: Zong Li <[email protected]> Signed-off-by: Zong Li <[email protected]> Signed-off-by: Nylon Chen <[email protected]> Reviewed-by: Andy Chiu <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-10-26lib: sbi: add Smdbltrp ISA extension supportClément Léger
Add support for the Smdbltrp[1] ISA extension. First thing to do is clearing MDT on entry after setting the first MTVEC (since MDT is reset to 1). Additionally, during trap handling, clear MDT once all critical CSRs have been saved and in return path, restore MSTATUS/H before restoring MEPC to avoid taking another trap which would clobber it. Link: https://github.com/riscv/riscv-double-trap/releases/download/v0.56/riscv-double-trap.pdf [1] Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Samuel Holland <[email protected]>
2024-10-25lib: sbi: implement firmware feature SBI_FWFT_DOUBLE_TRAPClément Léger
Add support for double trap firmware feature. Link: https://lists.riscv.org/g/tech-prs/message/985 [1] Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Samuel Holland <[email protected]>
2024-10-25lib: sbi: fwft: factorize menvcfg read/writeClément Léger
MENVCFG access will be used as well for double trap, landing pad and shadow stack fwft support. Factorize that in a common function. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Samuel Holland <[email protected]>
2024-10-25lib: sbi: add Ssdbltrp ISA extension supportClément Léger
Add Ssdbltrp trap handler support for S-mode double trap handling. If the trap is received while in VS-mode, then the trap is redirected to S-mode. If caught while in HS-mode, then an error is returned to the top trap handler which will panic. Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Samuel Holland <[email protected]>
2024-09-23include: adding support for Zicfilp / Zicfiss encodingsDeepak Gupta
Zicfilp / Zicfiss extension (see link) introduces b2 (LPE) in menvcfg CSR to enable landing pads and b3 (SSE) in menvcfg CSR to enable shadow stack and landing pad for privilege less than M. Additionally extension introduces new bits in *status for recording landing pad state and a new exception type `software check exception` with cause=0x12. Link: https://github.com/riscv/riscv-cfi Signed-off-by: Deepak Gupta <[email protected]> Reviewed-by: Atish Patra <[email protected]> Reviewed-by: Samuel Holland <[email protected]>
2024-09-23lib: sbi: fwft: add support for SBI_FWFT_POINTER_MASKING_PMLENSamuel Holland
Add support for controlling the pointer masking mode on harts which support the Smnpm extension. This extension can only exist on harts where XLEN >= 64 bits. This implementation selects the mode with the smallest PMLEN that satisfies the caller's requested lower bound. Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-08-23lib: sbi: Enhance CSR Handling in system_opcode_insnDongdong Zhang
- Completed TODO in `system_opcode_insn` to ensure CSR read/write instruction handling. - Refactored to use new macros `GET_RS1_NUM` and `GET_CSR_NUM`. - Updated `GET_RM` macro and replaced hardcoded funct3 values with constants (`CSRRW`, `CSRRS`, `CSRRC`, etc.). - Removed redundant `GET_RM` from `riscv_fp.h`. - Improved validation and error handling for CSR instructions. This patch enhances the clarity and correctness of CSR handling in `system_opcode_insn`. Signed-off-by: Dongdong Zhang <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-08-02include: Adjust Sscofpmf mhpmevent mask for upper 8 bitsEric Lin
Currently, OpenSBI reserves the upper 16 bits in mhpmevent for the Sscofpmf extension. However, according to the Sscofpmf extension specification[1], it only defines the upper 8 bits in mhpmevent for privilege mode inhibit and counter overflow disable. Other bits are defined by the platform for event selection. Since vendors might define raw event encoding exceeding 48 bits in mhpmevent, we should adjust the MHPMEVENT_SSCOF_MASK to support it. Link: https://github.com/riscvarchive/riscv-count-overflow [1] Signed-off-by: Eric Lin <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-06-18lib: sbi: Add support for Svade and Svadu extensionsYong-Xuan Wang
Add support for Svade and Svadu extensions. When both are present in the device tree, the M-mode firmware should select the Svade extension to comply with the RVA23 profile, which mandates Svade and lists Svadu as an optional extension. Signed-off-by: Yong-Xuan Wang <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-03-19lib: sbi: Remove regs parameter from trap irq handling functionsAnup Patel
The trap irq handling functions no longer require regs parameter so remove it. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Samuel Holland <[email protected]> Tested-by: Samuel Holland <[email protected]>
2024-03-05lib: sbi_misaligned_ldst: Add handling of C.LHU/C.LH and C.SHNylon Chen
Added exception handling for compressed instructions C.LHU, C.LH, and C.SH from the zcb extension to the sbi_misaligned_ldst library. Signed-off-by: Nylon Chen <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-03-04lib: sbi: Add support for smcsrind and smcdelegAtish Patra
Smcsrind allows generic indirect CSR access mechanism while Smcdeleg allows delegating hpmcounters in Supervisor mode. Enable both extensions and set the appropriate bits in mstateen and menvcfg. Co-developed-by: Kaiwen Xue <[email protected]> Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2024-01-10include: sbi: Add TINFO debug trigger CSRHimanshu Chauhan
Add the missing TINFO debug trigger CSR. Signed-off-by: Himanshu Chauhan <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2023-12-19lib: sbi: Refactor the code for enable extensions in menvfg CSRYong-Xuan Wang
Use 1 variable to store the value of menvcfg. Signed-off-by: Yong-Xuan Wang <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2023-11-17include: sbi: macros for mseccfg.sseed and .useedHeinrich Schuchardt
Define macros to access the sseed and the useed bit in the machine security configuration register (mseccfg). Signed-off-by: Heinrich Schuchardt <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2023-10-06lib: sbi: Add support for mconfigptrYangjie Zhang
RISC-V privileged specification v1.12 introduced the mconfigptr CSR which will hold the physical address of a configuration data structure. Signed-off-by: Yangjie Zhang <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2023-08-18lib: sbi: Add support for smcntrpmfKaiwen Xue
This adds the support for ISA extension smcntrpmf. When some inhibit flags are set by a lower privilege mode for new CSRs added by smcntrpmf, OpenSBI sets the appropriate values correspondingly. Signed-off-by: Kaiwen Xue <[email protected]> Signed-off-by: Kaiwen Xue <[email protected]> Reviewed-by: Anup Patel <[email protected]> Reviewed-by: Atish Patra <[email protected]>
2023-07-31include: sbi: fix CSR define of mseccfgXiang W
Because the CSR names in the spec are mseccfg and mseccfgh. Remove CSR_MSECCFG_LOWER and CSR_MSECCFG_UPPER and directly define CSR_MSECCFG and CSR_MSECCFGH. Signed-off-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2023-07-13include: sbi: Add macro definitions for mseccfg CSRHimanshu Chauhan
- Add macros for Machine Security Configuration (mseccfg) CSR - Add macros to access/manipulate bits in msecfg CSR Signed-off-by: Himanshu Chauhan <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2023-02-27lib: sbi_hart: Enable hcontext and scontextNylon Chen
According to the description in "riscv-state-enable[0]", to access h/scontext in S-Mode, we need to enable the 57th bit. If it is not enabled, an "illegal instruction" error will occur. Link: https://github.com/riscv/riscv-state-enable/blob/a28bfae443f350d5b4c42874f428367d5b322ffe/content.adoc [0] Signed-off-by: Nylon Chen <[email protected]> Reviewed-by: Zong Li <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-09-01include: Remove sideleg and sedelegRahul Pathak
sideleg and sedeleg csrs are not part of riscv isa spec anymore, these csrs were part of N extension which is removed from the riscv isa specification. These commits removed all traces of these csrs from riscv spec (https://github.com/riscv/riscv-isa-manual) - commit f8d27f805b65 ("Remove or downgrade more references to N extension (#674)") commit b6cade07034d ("Remove N extension chapter for now") Signed-off-by: Rahul Pathak <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-08-22include: Add mstatus[h].GVA encodingsVivian Wang
The machine mode GVA field is in mstatus for RV64 and mstatush for RV32, and is available if the hypervisor extension is available. If an exception occurs, we may need to redirect the trap to HS-mode, in which case, hstatus.GVA should be set to same as the machine mode GVA bit. Add MSTATUS_GVA for RV64, MSTATUSH_GVA for RV32, and their SHIFT encodings. The SHIFT index is helpful in assembly code, since field extraction can be implemented in only one register. In pseudocode: - For RV32: gva = (mstatus >> MSTATUS_GVA_SHIFT) & 1; - For RV64: gva = (mstatush >> MSTATUSH_GVA_SHIFT) & 1; Signed-off-by: Vivian Wang <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-06-21lib: utils: Remove CSRs that set/clear an IMSIC interrupt file bitsAnup Patel
Based on architecture review committee feedback, the [m|s|vs]seteienum, [m|s|vs]clreienum, [m|s|vs]seteipnum, and [m|s|vs]clreipnum CSRs are removed in the latest AIA draft v0.3.0 specification. (Refer, https://github.com/riscv/riscv-aia/releases/tag/0.3.0-draft.31) These CSRs were mostly for software convenience and software can always use [m|s|vs]iselect and [m|s|vs]ireg CSRs to update the IMSIC interrupt file bits. We update the IMSIC programming as-per above to match the latest AIA draft specification. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Atish Patra <[email protected]>
2022-06-21include: sbi: Add mtinst/htinst psuedoinstructionsdramforever
Add psuedoinstruction encodings written to mtinst/htinst for faults caused by implicit memory access for VS-stage address translation Signed-off-by: dramforever <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-06-01lib: sbi_illegal_insn: Add emulation for fence.tsoSamuel Holland
While OpenC906 appears to properly decode `fence.tso` as a fence instruction[1], the version of the C906 taped out in the Allwinner D1 does not, and raises illegal instruction. Handle this errata by emulating `fence.tso` as `fence rw, rw`. [1]: https://github.com/T-head-Semi/openc906/blob/30827e7f/C906_RTL_FACTORY/gen_rtl/idu/rtl/aq_idu_id_decd.v#L2097 Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-04-28lib: sbi: Implement Sstc extensionAtish Patra
Recently, Sstc extension was ratified. It defines stimecmp which allows the supervisor mode to directly update the timecmp value without the need of the SBI call. The hardware also can inject the S-mode timer interrupt direclty to the supervisor without going through the M-mode. To maintain backward compatibility with the older software, SBI call now uses stimecmp directly if the hardware supports. Implement the Sstc extension. Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-04-17include: correct the definition of MSTATUS_VSVincent Chen
Accordind to the RISC-V privileged specification, the VS filed is mstatus[10:9] instead of mstatus[24:23]. Modify the MSTATUS_VS to the correct value. Reported-by: I-Cheng Cheng <[email protected]> Signed-off-by: Vincent Chen <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-04-11lib: sbi: Add Smstateen extension definesMayuresh Chitale
Smstateen extension provides a mechanism to plug potential covert channels which are opened by extensions that add to processor state that may not get context-switched. Signed-off-by: Mayuresh Chitale <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-04-05include: Add defines for [m|h|s]envcfg CSRsAnup Patel
The latest RISC-V privileged specification introduces xenvcfg CSRs to enable/disable certain features/extensions for lower privilege modes. This patch adds defines for these new [m|h|s]envcfg CSRs. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Atish Patra <[email protected]> Reviewed-by: Xiang W <[email protected]>
2022-02-15include: sbi: Add AIA related CSR definesAnup Patel
The RISC-V AIA specification improves handling of per-HART local interrupts in a backward compatible manner. This patch adds defines for the new RISC-V AIA CSRs. Signed-off-by: Anup Patel <[email protected]> Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Atish Patra <[email protected]>
2022-02-04lib: sbi: Disable interrupt during config matchingAtish Patra
PMU overflow interrupt should be disabled durinig initial configuration of counters. They should be enabled while starting counters. Fixes: 730f01bb41a6 ("lib: sbi: Support sscofpmf extension in OpenSBI") Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2021-11-11lib: sbi: Support sscofpmf extension in OpenSBIAtish Patra
This patch adds sscofpmf extension in pmu module which includes following things. 1. Enable overflow irq when starting a counter. 2. Setting the correct event filters passed from supervisor. 3. Delegating the overflow interrupt to the supervisor. 4. Add RV32 support for sscofpmf. Reviewed-by: Anup Patel <[email protected]> Reviewed-by: Bin Meng <[email protected]> Signed-off-by: Atish Patra <[email protected]>
2021-11-11lib: sbi: Delegate PMU counter overflow interrupt to S modeAtish Patra
OpenSBI doesn't handle PMU counters for now. Delegate the overflow counter to S-mode always. Reviewed-by: Bin Meng <[email protected]> Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Atish Patra <[email protected]>
2021-11-11riscv: Add new CSRs introduced by Sscofpmf[1] extensionAtish Patra
[1] https://drive.google.com/file/d/1KcjgbLM5L1ZKY8934aJl8aQwGlMz6Cbo/view Reviewed-by: Xiang W <[email protected]> Reviewed-by: Bin Meng <[email protected]> Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Atish Patra <[email protected]>
2020-09-09include: Rename ECALL defines to match latest RISC-V specAnup Patel
We rename CAUSE_HYPERVISOR_ECALL to CAUSE_SUPERVISOR_ECALL and CAUSE_SUPERVISOR_ECALL to CAUSE_VIRTUAL_SUPERVISOR_ECALL so that it matches latest RISC-V privilege spec. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Atish Patra <[email protected]>