diff options
author | Chao-ying Fu <icebergfu@gmail.com> | 2025-05-19 17:42:06 +0530 |
---|---|---|
committer | Anup Patel <anup@brainfault.org> | 2025-05-20 09:18:03 +0530 |
commit | 8a3071222ae57e7a1736c6e953fa1ab3628e599e (patch) | |
tree | ef106bf950acf1308fca99c24043e49c13fdf918 /lib | |
parent | 017a161788310ef515831821f14597e657512f97 (diff) | |
download | opensbi-8a3071222ae57e7a1736c6e953fa1ab3628e599e.zip opensbi-8a3071222ae57e7a1736c6e953fa1ab3628e599e.tar.gz opensbi-8a3071222ae57e7a1736c6e953fa1ab3628e599e.tar.bz2 |
lib: Emulate AMO instructions when Zaamo is not available
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 <cfu@mips.com>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Link: https://lore.kernel.org/r/20250519121207.976949-1-apatel@ventanamicro.com
Signed-off-by: Anup Patel <anup@brainfault.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sbi/objects.mk | 1 | ||||
-rw-r--r-- | lib/sbi/sbi_illegal_atomic.c | 664 | ||||
-rw-r--r-- | lib/sbi/sbi_illegal_insn.c | 7 |
3 files changed, 668 insertions, 4 deletions
diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk index 9cb2842..8abe1e8 100644 --- a/lib/sbi/objects.mk +++ b/lib/sbi/objects.mk @@ -79,6 +79,7 @@ libsbi-objs-y += sbi_heap.o libsbi-objs-y += sbi_math.o libsbi-objs-y += sbi_hfence.o libsbi-objs-y += sbi_hsm.o +libsbi-objs-y += sbi_illegal_atomic.o libsbi-objs-y += sbi_illegal_insn.o libsbi-objs-y += sbi_init.o libsbi-objs-y += sbi_ipi.o diff --git a/lib/sbi/sbi_illegal_atomic.c b/lib/sbi/sbi_illegal_atomic.c new file mode 100644 index 0000000..2161b23 --- /dev/null +++ b/lib/sbi/sbi_illegal_atomic.c @@ -0,0 +1,664 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 MIPS + * + */ + +#include <sbi/riscv_asm.h> +#include <sbi/sbi_hart.h> +#include <sbi/sbi_trap.h> +#include <sbi/sbi_illegal_atomic.h> +#include <sbi/sbi_illegal_insn.h> + +#if !defined(__riscv_atomic) && !defined(__riscv_zalrsc) +#error "opensbi strongly relies on the A extension of RISC-V" +#endif + +#ifdef __riscv_atomic + +int sbi_illegal_atomic(ulong insn, struct sbi_trap_regs *regs) +{ + return truly_illegal_insn(insn, regs); +} + +#elif __riscv_zalrsc + +#define DEFINE_UNPRIVILEGED_LR_FUNCTION(type, aqrl, insn) \ + static type lr_##type##aqrl(const type *addr, \ + struct sbi_trap_info *trap) \ + { \ + register ulong tinfo asm("a3"); \ + register ulong mstatus = 0; \ + register ulong mtvec = sbi_hart_expected_trap_addr(); \ + type ret = 0; \ + trap->cause = 0; \ + asm volatile( \ + "add %[tinfo], %[taddr], zero\n" \ + "csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \ + "csrrs %[mstatus], " STR(CSR_MSTATUS) ", %[mprv]\n" \ + ".option push\n" \ + ".option norvc\n" \ + #insn " %[ret], %[addr]\n" \ + ".option pop\n" \ + "csrw " STR(CSR_MSTATUS) ", %[mstatus]\n" \ + "csrw " STR(CSR_MTVEC) ", %[mtvec]" \ + : [mstatus] "+&r"(mstatus), [mtvec] "+&r"(mtvec), \ + [tinfo] "+&r"(tinfo), [ret] "=&r"(ret) \ + : [addr] "m"(*addr), [mprv] "r"(MSTATUS_MPRV), \ + [taddr] "r"((ulong)trap) \ + : "a4", "memory"); \ + return ret; \ + } + +#define DEFINE_UNPRIVILEGED_SC_FUNCTION(type, aqrl, insn) \ + static type sc_##type##aqrl(type *addr, type val, \ + struct sbi_trap_info *trap) \ + { \ + register ulong tinfo asm("a3"); \ + register ulong mstatus = 0; \ + register ulong mtvec = sbi_hart_expected_trap_addr(); \ + type ret = 0; \ + trap->cause = 0; \ + asm volatile( \ + "add %[tinfo], %[taddr], zero\n" \ + "csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \ + "csrrs %[mstatus], " STR(CSR_MSTATUS) ", %[mprv]\n" \ + ".option push\n" \ + ".option norvc\n" \ + #insn " %[ret], %[val], %[addr]\n" \ + ".option pop\n" \ + "csrw " STR(CSR_MSTATUS) ", %[mstatus]\n" \ + "csrw " STR(CSR_MTVEC) ", %[mtvec]" \ + : [mstatus] "+&r"(mstatus), [mtvec] "+&r"(mtvec), \ + [tinfo] "+&r"(tinfo), [ret] "=&r"(ret) \ + : [addr] "m"(*addr), [mprv] "r"(MSTATUS_MPRV), \ + [val] "r"(val), [taddr] "r"((ulong)trap) \ + : "a4", "memory"); \ + return ret; \ + } + +DEFINE_UNPRIVILEGED_LR_FUNCTION(s32, , lr.w); +DEFINE_UNPRIVILEGED_LR_FUNCTION(s32, _aq, lr.w.aq); +DEFINE_UNPRIVILEGED_LR_FUNCTION(s32, _rl, lr.w.rl); +DEFINE_UNPRIVILEGED_LR_FUNCTION(s32, _aqrl, lr.w.aqrl); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s32, , sc.w); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s32, _aq, sc.w.aq); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s32, _rl, sc.w.rl); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s32, _aqrl, sc.w.aqrl); +#if __riscv_xlen == 64 +DEFINE_UNPRIVILEGED_LR_FUNCTION(s64, , lr.d); +DEFINE_UNPRIVILEGED_LR_FUNCTION(s64, _aq, lr.d.aq); +DEFINE_UNPRIVILEGED_LR_FUNCTION(s64, _rl, lr.d.rl); +DEFINE_UNPRIVILEGED_LR_FUNCTION(s64, _aqrl, lr.d.aqrl); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s64, , sc.d); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s64, _aq, sc.d.aq); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s64, _rl, sc.d.rl); +DEFINE_UNPRIVILEGED_SC_FUNCTION(s64, _aqrl, sc.d.aqrl); +#endif + +#define DEFINE_ATOMIC_FUNCTION(name, type, func) \ + static int atomic_##name(ulong insn, struct sbi_trap_regs *regs) \ + { \ + struct sbi_trap_info uptrap; \ + ulong addr = GET_RS1(insn, regs); \ + ulong val = GET_RS2(insn, regs); \ + ulong rd_val = 0; \ + ulong fail = 1; \ + while (fail) { \ + rd_val = lr_##type((void *)addr, &uptrap); \ + if (uptrap.cause) { \ + return sbi_trap_redirect(regs, &uptrap); \ + } \ + fail = sc_##type((void *)addr, func, &uptrap); \ + if (uptrap.cause) { \ + return sbi_trap_redirect(regs, &uptrap); \ + } \ + } \ + SET_RD(insn, regs, rd_val); \ + regs->mepc += 4; \ + return 0; \ + } + +DEFINE_ATOMIC_FUNCTION(add_w, s32, rd_val + val); +DEFINE_ATOMIC_FUNCTION(add_w_aq, s32_aq, rd_val + val); +DEFINE_ATOMIC_FUNCTION(add_w_rl, s32_rl, rd_val + val); +DEFINE_ATOMIC_FUNCTION(add_w_aqrl, s32_aqrl, rd_val + val); +DEFINE_ATOMIC_FUNCTION(and_w, s32, rd_val & val); +DEFINE_ATOMIC_FUNCTION(and_w_aq, s32_aq, rd_val & val); +DEFINE_ATOMIC_FUNCTION(and_w_rl, s32_rl, rd_val & val); +DEFINE_ATOMIC_FUNCTION(and_w_aqrl, s32_aqrl, rd_val & val); +DEFINE_ATOMIC_FUNCTION(or_w, s32, rd_val | val); +DEFINE_ATOMIC_FUNCTION(or_w_aq, s32_aq, rd_val | val); +DEFINE_ATOMIC_FUNCTION(or_w_rl, s32_rl, rd_val | val); +DEFINE_ATOMIC_FUNCTION(or_w_aqrl, s32_aqrl, rd_val | val); +DEFINE_ATOMIC_FUNCTION(xor_w, s32, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(xor_w_aq, s32_aq, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(xor_w_rl, s32_rl, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(xor_w_aqrl, s32_aqrl, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(swap_w, s32, val); +DEFINE_ATOMIC_FUNCTION(swap_w_aq, s32_aq, val); +DEFINE_ATOMIC_FUNCTION(swap_w_rl, s32_rl, val); +DEFINE_ATOMIC_FUNCTION(swap_w_aqrl, s32_aqrl, val); +DEFINE_ATOMIC_FUNCTION(max_w, s32, (s32)rd_val > (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(max_w_aq, s32_aq, (s32)rd_val > (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(max_w_rl, s32_rl, (s32)rd_val > (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(max_w_aqrl, s32_aqrl, (s32)rd_val > (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_w, s32, (u32)rd_val > (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_w_aq, s32_aq, (u32)rd_val > (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_w_rl, s32_rl, (u32)rd_val > (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_w_aqrl, s32_aqrl, (u32)rd_val > (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_w, s32, (s32)rd_val < (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_w_aq, s32_aq, (s32)rd_val < (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_w_rl, s32_rl, (s32)rd_val < (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_w_aqrl, s32_aqrl, (s32)rd_val < (s32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_w, s32, (u32)rd_val < (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_w_aq, s32_aq, (u32)rd_val < (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_w_rl, s32_rl, (u32)rd_val < (u32)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_w_aqrl, s32_aqrl, (u32)rd_val < (u32)val ? rd_val : val); + +#if __riscv_xlen == 64 +DEFINE_ATOMIC_FUNCTION(add_d, s64, rd_val + val); +DEFINE_ATOMIC_FUNCTION(add_d_aq, s64_aq, rd_val + val); +DEFINE_ATOMIC_FUNCTION(add_d_rl, s64_rl, rd_val + val); +DEFINE_ATOMIC_FUNCTION(add_d_aqrl, s64_aqrl, rd_val + val); +DEFINE_ATOMIC_FUNCTION(and_d, s64, rd_val & val); +DEFINE_ATOMIC_FUNCTION(and_d_aq, s64_aq, rd_val & val); +DEFINE_ATOMIC_FUNCTION(and_d_rl, s64_rl, rd_val & val); +DEFINE_ATOMIC_FUNCTION(and_d_aqrl, s64_aqrl, rd_val & val); +DEFINE_ATOMIC_FUNCTION(or_d, s64, rd_val | val); +DEFINE_ATOMIC_FUNCTION(or_d_aq, s64_aq, rd_val | val); +DEFINE_ATOMIC_FUNCTION(or_d_rl, s64_rl, rd_val | val); +DEFINE_ATOMIC_FUNCTION(or_d_aqrl, s64_aqrl, rd_val | val); +DEFINE_ATOMIC_FUNCTION(xor_d, s64, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(xor_d_aq, s64_aq, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(xor_d_rl, s64_rl, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(xor_d_aqrl, s64_aqrl, rd_val ^ val); +DEFINE_ATOMIC_FUNCTION(swap_d, s64, val); +DEFINE_ATOMIC_FUNCTION(swap_d_aq, s64_aq, val); +DEFINE_ATOMIC_FUNCTION(swap_d_rl, s64_rl, val); +DEFINE_ATOMIC_FUNCTION(swap_d_aqrl, s64_aqrl, val); +DEFINE_ATOMIC_FUNCTION(max_d, s64, (s64)rd_val > (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(max_d_aq, s64_aq, (s64)rd_val > (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(max_d_rl, s64_rl, (s64)rd_val > (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(max_d_aqrl, s64_aqrl, (s64)rd_val > (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_d, s64, (u64)rd_val > (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_d_aq, s64_aq, (u64)rd_val > (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_d_rl, s64_rl, (u64)rd_val > (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(maxu_d_aqrl, s64_aqrl, (u64)rd_val > (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_d, s64, (s64)rd_val < (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_d_aq, s64_aq, (s64)rd_val < (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_d_rl, s64_rl, (s64)rd_val < (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(min_d_aqrl, s64_aqrl, (s64)rd_val < (s64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_d, s64, (u64)rd_val < (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_d_aq, s64_aq, (u64)rd_val < (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_d_rl, s64_rl, (u64)rd_val < (u64)val ? rd_val : val); +DEFINE_ATOMIC_FUNCTION(minu_d_aqrl, s64_aqrl, (u64)rd_val < (u64)val ? rd_val : val); +#endif + +static const illegal_insn_func amoadd_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_add_w, /* 8 */ + atomic_add_w_rl, /* 9 */ + atomic_add_w_aq, /* 10 */ + atomic_add_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_add_d, /* 12 */ + atomic_add_d_rl, /* 13 */ + atomic_add_d_aq, /* 14 */ + atomic_add_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amoswap_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_swap_w, /* 8 */ + atomic_swap_w_rl, /* 9 */ + atomic_swap_w_aq, /* 10 */ + atomic_swap_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_swap_d, /* 12 */ + atomic_swap_d_rl, /* 13 */ + atomic_swap_d_aq, /* 14 */ + atomic_swap_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amoxor_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_xor_w, /* 8 */ + atomic_xor_w_rl, /* 9 */ + atomic_xor_w_aq, /* 10 */ + atomic_xor_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_xor_d, /* 12 */ + atomic_xor_d_rl, /* 13 */ + atomic_xor_d_aq, /* 14 */ + atomic_xor_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amoor_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_or_w, /* 8 */ + atomic_or_w_rl, /* 9 */ + atomic_or_w_aq, /* 10 */ + atomic_or_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_or_d, /* 12 */ + atomic_or_d_rl, /* 13 */ + atomic_or_d_aq, /* 14 */ + atomic_or_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amoand_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_and_w, /* 8 */ + atomic_and_w_rl, /* 9 */ + atomic_and_w_aq, /* 10 */ + atomic_and_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_and_d, /* 12 */ + atomic_and_d_rl, /* 13 */ + atomic_and_d_aq, /* 14 */ + atomic_and_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amomin_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_min_w, /* 8 */ + atomic_min_w_rl, /* 9 */ + atomic_min_w_aq, /* 10 */ + atomic_min_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_min_d, /* 12 */ + atomic_min_d_rl, /* 13 */ + atomic_min_d_aq, /* 14 */ + atomic_min_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amomax_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_max_w, /* 8 */ + atomic_max_w_rl, /* 9 */ + atomic_max_w_aq, /* 10 */ + atomic_max_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_max_d, /* 12 */ + atomic_max_d_rl, /* 13 */ + atomic_max_d_aq, /* 14 */ + atomic_max_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amominu_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_minu_w, /* 8 */ + atomic_minu_w_rl, /* 9 */ + atomic_minu_w_aq, /* 10 */ + atomic_minu_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_minu_d, /* 12 */ + atomic_minu_d_rl, /* 13 */ + atomic_minu_d_aq, /* 14 */ + atomic_minu_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static const illegal_insn_func amomaxu_table[32] = { + truly_illegal_insn, /* 0 */ + truly_illegal_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + truly_illegal_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + atomic_maxu_w, /* 8 */ + atomic_maxu_w_rl, /* 9 */ + atomic_maxu_w_aq, /* 10 */ + atomic_maxu_w_aqrl, /* 11 */ +#if __riscv_xlen == 64 + atomic_maxu_d, /* 12 */ + atomic_maxu_d_rl, /* 13 */ + atomic_maxu_d_aq, /* 14 */ + atomic_maxu_d_aqrl, /* 15 */ +#else + truly_illegal_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ +#endif + truly_illegal_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + truly_illegal_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + truly_illegal_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + truly_illegal_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn, /* 31 */ +}; + +static int amoadd_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amoadd_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amoswap_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amoswap_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amoxor_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amoxor_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amoor_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amoor_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amoand_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amoand_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amomin_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amomin_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amomax_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amomax_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amominu_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amominu_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static int amomaxu_insn(ulong insn, struct sbi_trap_regs *regs) +{ + return amomaxu_table[(GET_FUNC3(insn) << 2) + GET_AQRL(insn)](insn, regs); +} + +static const illegal_insn_func amo_insn_table[32] = { + amoadd_insn, /* 0 */ + amoswap_insn, /* 1 */ + truly_illegal_insn, /* 2 */ + truly_illegal_insn, /* 3 */ + amoxor_insn, /* 4 */ + truly_illegal_insn, /* 5 */ + truly_illegal_insn, /* 6 */ + truly_illegal_insn, /* 7 */ + amoor_insn, /* 8 */ + truly_illegal_insn, /* 9 */ + truly_illegal_insn, /* 10 */ + truly_illegal_insn, /* 11 */ + amoand_insn, /* 12 */ + truly_illegal_insn, /* 13 */ + truly_illegal_insn, /* 14 */ + truly_illegal_insn, /* 15 */ + amomin_insn, /* 16 */ + truly_illegal_insn, /* 17 */ + truly_illegal_insn, /* 18 */ + truly_illegal_insn, /* 19 */ + amomax_insn, /* 20 */ + truly_illegal_insn, /* 21 */ + truly_illegal_insn, /* 22 */ + truly_illegal_insn, /* 23 */ + amominu_insn, /* 24 */ + truly_illegal_insn, /* 25 */ + truly_illegal_insn, /* 26 */ + truly_illegal_insn, /* 27 */ + amomaxu_insn, /* 28 */ + truly_illegal_insn, /* 29 */ + truly_illegal_insn, /* 30 */ + truly_illegal_insn /* 31 */ +}; + +int sbi_illegal_atomic(ulong insn, struct sbi_trap_regs *regs) +{ + return amo_insn_table[(insn >> 27) & 0x1f](insn, regs); +} + +#else +#error "need a or zalrsc" +#endif diff --git a/lib/sbi/sbi_illegal_insn.c b/lib/sbi/sbi_illegal_insn.c index 784bc4c..fa82264 100644 --- a/lib/sbi/sbi_illegal_insn.c +++ b/lib/sbi/sbi_illegal_insn.c @@ -13,15 +13,14 @@ #include <sbi/sbi_bitops.h> #include <sbi/sbi_emulate_csr.h> #include <sbi/sbi_error.h> +#include <sbi/sbi_illegal_atomic.h> #include <sbi/sbi_illegal_insn.h> #include <sbi/sbi_pmu.h> #include <sbi/sbi_trap.h> #include <sbi/sbi_unpriv.h> #include <sbi/sbi_console.h> -typedef int (*illegal_insn_func)(ulong insn, struct sbi_trap_regs *regs); - -static int truly_illegal_insn(ulong insn, struct sbi_trap_regs *regs) +int truly_illegal_insn(ulong insn, struct sbi_trap_regs *regs) { struct sbi_trap_info trap; @@ -123,7 +122,7 @@ static const illegal_insn_func illegal_insn_table[32] = { truly_illegal_insn, /* 8 */ truly_illegal_insn, /* 9 */ truly_illegal_insn, /* 10 */ - truly_illegal_insn, /* 11 */ + sbi_illegal_atomic, /* 11 */ truly_illegal_insn, /* 12 */ truly_illegal_insn, /* 13 */ truly_illegal_insn, /* 14 */ |