qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

Merge remote-tracking branch 'remotes/riscv/tags/riscv-qemu-upstream-v8.2' into staging

QEMU RISC-V Emulation Support (RV64GC, RV32GC)

This release renames the SiFive machines to sifive_e and sifive_u
to represent the SiFive Everywhere and SiFive Unleashed platforms.
SiFive has configurable soft-core IP, so it is intended that these
machines will be extended to enable a variety of SiFive IP blocks.
The CPU definition infrastructure has been improved and there are
now vendor CPU modules including the SiFiVe E31, E51, U34 and U54
cores. The emulation accuracy for the E series has been improved
by disabling the MMU for the E series. S mode has been disabled on
cores that only support M mode and U mode. The two Spike machines
that support two privileged ISA versions have been coalesced into
one file. This series has Signed-off-by from the core contributors.

*** Known Issues ***

* Disassembler has some checkpatch warnings for the sake of code brevity
* scripts/qemu-binfmt-conf.sh has checkpatch warnings due to line length
* PMP (Physical Memory Protection) is as-of-yet unused and needs testing

*** Changelog ***

v8.2

* Rebase

v8.1

* Fix missed case of renaming spike_v1.9 to spike_v1.9.1

v8

* Added linux-user/riscv/target_elf.h during rebase
* Make resetvec configurable and clear mpp and mie on reset
* Use SiFive E31, E51, U34 and U54 cores in SiFive machines
* Define SiFive E31, E51, U34 and U54 cores
* Refactor CPU core definition in preparation for vendor cores
* Prevent S or U mode unless S or U extensions are present
* SiFive E Series cores have no MMU
* SiFive E Series cores have U mode
* Make privileged ISA v1.10 implicit in CPU types
* Remove DRAM_BASE and EXT_IO_BASE as they vary by machine
* Correctly handle mtvec and stvec alignment with respect to RVC
* Print more machine mode state in riscv_cpu_dump_state
* Make riscv_isa_string use compact extension order method
* Fix bug introduced in v6 RISCV_CPU_TYPE_NAME macro change
* Parameterize spike v1.9.1 config string
* Coalesce spike_v1.9.1 and spike_v1.10 machines
* Rename sifive_e300 to sifive_e, and sifive_u500 to sifive_u

v7

* Make spike_v1.10 the default machine
* Rename spike_v1.9 to spike_v1.9.1 to match privileged spec version
* Remove empty target/riscv/trace-events file
* Monitor ROM 32-bit reset code needs to be target endian
* Add TARGET_TIOCGPTPEER to linux-user/riscv/termbits.h
* Add -initrd support to the virt board
* Fix naming in spike machine interface header
* Update copyright notice on RISC-V Spike machines
* Update copyright notice on RISC-V HTIF Console device
* Change CPU Core and translator to GPLv2+
* Change RISC-V Disassembler to GPLv2+
* Change SiFive Test Finisher to GPLv2+
* Change SiFive CLINT to GPLv2+
* Change SiFive PRCI to GPLv2+
* Change SiFive PLIC to GPLv2+
* Change RISC-V spike machines to GPLv2+
* Change RISC-V virt machine to GPLv2+
* Change SiFive E300 machine to GPLv2+
* Change SiFive U500 machine to GPLv2+
* Change RISC-V Hart Array to GPLv2+
* Change RISC-V HTIF device to GPLv2+
* Change SiFiveUART device to GPLv2+

v6

* Drop IEEE 754-201x minimumNumber/maximumNumber for fmin/fmax
* Remove some unnecessary commented debug statements
* Change RISCV_CPU_TYPE_NAME to use riscv-cpu suffix
* Define all CPU variants for linux-user
* qemu_log calls require trailing \n
* Replace PLIC printfs with qemu_log
* Tear out unused HTIF code and eliminate shouting debug messages
* Fix illegal instruction when sfence.vma is passed (rs2) arguments
* Make updates to PTE accessed and dirty bits atomic
* Only require atomic PTE updates on MTTCG enabled guests
* Page fault if accessed or dirty bits can't be updated
* Fix get_physical_address PTE reads and writes on riscv32
* Remove erroneous comments from the PLIC
* Default enable MTTCG
* Make WFI less conservative
* Unify local interrupt handling
* Expunge HTIF interrupts
* Always access mstatus.mip under a lock
* Don't implement rdtime/rdtimeh in system mode (bbl emulates them)
* Implement insreth/cycleh for rv32 and always enable user-mode counters
* Add GDB stub support for reading and writing CSRs
* Rename ENABLE_CHARDEV #ifdef from HTIF code
* Replace bad HTIF ELF code with load_elf symbol callback
* Convert chained if else fault handlers to switch statements
* Use RISCV exception codes for linux-user page faults

v5

* Implement NaN-boxing for flw, set high order bits to 1
* Use float_muladd_negate_* flags to floatXX_muladd
* Use IEEE 754-201x minimumNumber/maximumNumber for fmin/fmax
* Fix TARGET_NR_syscalls
* Update linux-user/riscv/syscall_nr.h
* Fix FENCE.I, needs to terminate translation block
* Adjust unusual convention for interruptno >= 0

v4

* Add @riscv: since 2.12 to CpuInfoArch
* Remove misleading little-endian comment from load_kernel
* Rename cpu-model property to cpu-type
* Drop some unnecessary inline function attributes
* Don't allow GDB to set value of x0 register
* Remove unnecessary empty property lists
* Add Test Finisher device to implement poweroff in virt machine
* Implement priv ISA v1.10 trap and sret/mret xPIE/xIE behavior
* Store fflags data in fp_status
* Purge runtime users of helper_raise_exception
* Fix validate_csr
* Tidy gen_jalr
* Tidy immediate shifts
* Add gen_exception_inst_addr_mis
* Add gen_exception_debug
* Add gen_exception_illegal
* Tidy helper_fclass_*
* Split rounding mode setting to a new function
* Enforce MSTATUS_FS via TB flags
* Implement acquire/release barrier semantics
* Use atomic operations as required
* Fix FENCE and FENCE_I
* Remove commented code from spike machines
* PAGE_WRITE permissions can be set on loads if page is already dirty
* The result of format conversion on an NaN must be a quiet NaN
* Add missing process_queued_cpu_work to riscv linux-user
* Remove float(32|64)_classify from cpu.h
* Removed nonsensical unions aliasing the same type
* Use uintN_t instead of uintN_fast_t in fpu_helper.c
* Use macros for FPU exception values in softfloat_flags_to_riscv
* Move code to set round mode into set_fp_round_mode function
* Convert set_fp_exceptions from a macro to an inline function
* Convert round mode helper into an inline function
* Make fpu_helper ieee_rm array static const
* Include cpu_mmu_index in cpu_get_tb_cpu_state flags
* Eliminate MPRV influence on mmu_index
* Remove unrecoverable do_unassigned_access function
* Only update PTE accessed and dirty bits if necessary
* Remove unnecessary tlb_flush in set_mode as mode is in mmu_idx
* Remove buggy support for misa writes. misa writes are optional
and are not implemented in any known hardware
* Always set PTE read or execute permissions during page walk
* Reorder helper function declarations to match order in helper.c
* Remove redundant variable declaration in get_physical_address
* Remove duplicated code from get_physical_address
* Use mmu_idx instead of mem_idx in riscv_cpu_get_phys_page_debug

v3

* Fix indentation in PMP and HTIF debug macros
* Fix disassembler checkpatch open brace '{' on next line errors
* Fix trailing statements on next line in decode_inst_decompress
* NOTE: the other checkpatch issues have been reviewed previously

v2

* Remove redundant NULL terminators from disassembler register arrays
* Change disassembler register name arrays to const
* Refine disassembler internal function names
* Update dates in disassembler copyright message
* Remove #ifdef CONFIG_USER_ONLY version of cpu_has_work
* Use ULL suffix on 64-bit constants
* Move riscv_cpu_mmu_index from cpu.h to helper.c
* Move riscv_cpu_hw_interrupts_pending from cpu.h to helper.c
* Remove redundant TARGET_HAS_ICE from cpu.h
* Use qemu_irq instead of void* for irq definition in cpu.h
* Remove duplicate typedef from struct CPURISCVState
* Remove redundant g_strdup from cpu_register
* Remove redundant tlb_flush from riscv_cpu_reset
* Remove redundant mode calculation from get_physical_address
* Remove redundant debug mode printf and dcsr comment
* Remove redundant clearing of MSB for bare physical addresses
* Use g_assert_not_reached for invalid mode in get_physical_address
* Use g_assert_not_reached for unreachable checks in get_physical_address
* Use g_assert_not_reached for unreachable type in raise_mmu_exception
* Return exception instead of aborting for misaligned fetches
* Move exception defines from cpu.h to cpu_bits.h
* Remove redundant breakpoint control definitions from cpu_bits.h
* Implement riscv_cpu_unassigned_access exception handling
* Log and raise exceptions for unimplemented CSRs
* Match Spike HTIF exit behavior - don’t print TEST-PASSED
* Make frm,fflags,fcsr writes trap when mstatus.FS is clear
* Use g_assert_not_reached for unreachable invalid mode
* Make hret,uret,dret generate illegal instructions
* Move riscv_cpu_dump_state and int/fpr regnames to cpu.c
* Lift interrupt flag and mask into constants in cpu_bits.h
* Change trap debugging to use qemu_log_mask LOG_TRACE
* Change CSR debugging to use qemu_log_mask LOG_TRACE
* Change PMP debugging to use qemu_log_mask LOG_TRACE
* Remove commented code from pmp.c
* Change CpuInfoRISCV qapi schema docs to Since 2.12
* Change RV feature macro to use target_ulong cast
* Remove riscv_feature and instead use misa extension flags
* Make riscv_flush_icache_syscall a no-op
* Undo checkpatch whitespace fixes in unrelated linux-user code
* Remove redudant constants and tidy up cpu_bits.h
* Make helper_fence_i a no-op
* Move include "exec/cpu-all" to end of cpu.h
* Rename set_privilege to riscv_set_mode
* Move redundant forward declaration for cpu_riscv_translate_address
* Remove TCGV_UNUSED from riscv_translate_init
* Add comment to pmp.c stating the code is untested and currently unused
* Use ctz to simplify decoding of PMP NAPOT address ranges
* Change pmp_is_in_range to use than equal for end addresses
* Fix off by one error in pmp_update_rule
* Rearrange PMP_DEBUG so that formatting is compile-time checked
* Rearrange trap debugging so that formatting is compile-time checked
* Rearrange PLIC debugging so that formatting is compile-time checked
* Use qemu_log/qemu_log_mask for HTIF logging and debugging
* Move exception and interrupt names into cpu.c
* Add Palmer Dabbelt as a RISC-V Maintainer
* Rebase against current qemu master branch

v1

* initial version based on forward port from riscv-qemu repository

*** Background ***

"RISC-V is an open, free ISA enabling a new era of processor innovation
through open standard collaboration. Born in academia and research,
RISC-V ISA delivers a new level of free, extensible software and
hardware freedom on architecture, paving the way for the next 50 years
of computing design and innovation."

The QEMU RISC-V port has been developed and maintained out-of-tree for
several years by Sagar Karandikar and Bastian Koppelmann. The RISC-V
Privileged specification has evolved substantially over this period but
has recently been solidifying. The RISC-V Base ISA has been frozon for
some time and the Privileged ISA, GCC toolchain and Linux ABI are now
quite stable. I have recently joined Sagar and Bastian as a RISC-V QEMU
Maintainer and hope to support upstreaming the port.

There are multiple vendors taping out, preparing to ship, or shipping
silicon that implements the RISC-V Privileged ISA Version 1.10. There
are also several RISC-V Soft-IP cores implementing Privileged ISA
Version 1.10 that run on FPGA such as SiFive's Freedom U500 Platform
and the U54‑MC RISC-V Core IP, among many more implementations from a
variety of vendors. See https://riscv.org/ for more details.

RISC-V support was upstreamed in binutils 2.28 and GCC 7.1 in the first
half of 2016. RISC-V support is now available in LLVM top-of-tree and
the RISC-V Linux port was accepted into Linux 4.15-rc1 late last year
and is available in the Linux 4.15 release. GLIBC 2.27 added support
for the RISC-V ISA running on Linux (requires at least binutils-2.30,
gcc-7.3.0, and linux-4.15). We believe it is timely to submit the
RISC-V QEMU port for upstream review with the goal of incorporating
RISC-V support into the upcoming QEMU 2.12 release.

The RISC-V QEMU port is still under active development, mostly with
respect to device emulation, the addition of Hypervisor support as
specified in the RISC-V Draft Privileged ISA Version 1.11, and Vector
support once the first draft is finalized later this year. We believe
now is the appropriate time for RISC-V QEMU development to be carried
out in the main QEMU repository as the code will benefit from more
rigorous review. The RISC-V QEMU port currently supports all the ISA
extensions that have been finalized and frozen in the Base ISA.

Blog post about recent additions to RISC-V QEMU: https://goo.gl/fJ4zgk

The RISC-V QEMU wiki: https://github.com/riscv/riscv-qemu/wiki

Instructions for building a busybox+dropbear root image, BBL (Berkeley
Boot Loader) and linux kernel image for use with the RISC-V QEMU
'virt' machine: https://github.com/michaeljclark/busybear-linux

*** Overview ***

The RISC-V QEMU port implements the following specifications:

* RISC-V Instruction Set Manual Volume I: User-Level ISA Version 2.2
* RISC-V Instruction Set Manual Volume II: Privileged ISA Version 1.9.1
* RISC-V Instruction Set Manual Volume II: Privileged ISA Version 1.10

The RISC-V QEMU port supports the following instruction set extensions:

* RV32GC with Supervisor-mode and User-mode (RV32IMAFDCSU)
* RV64GC with Supervisor-mode and User-mode (RV64IMAFDCSU)

The RISC-V QEMU port adds the following targets to QEMU:

* riscv32-softmmu
* riscv64-softmmu
* riscv32-linux-user
* riscv64-linux-user

The RISC-V QEMU port supports the following hardware:

* HTIF Console (Host Target Interface)
* SiFive CLINT (Core Local Interruptor) for Timer interrupts and IPIs
* SiFive PLIC (Platform Level Interrupt Controller)
* SiFive Test (Test Finisher) for exiting simulation
* SiFive UART, PRCI, AON, PWM, QSPI support is partially implemented
* VirtIO MMIO (GPEX PCI support will be added in a future patch)
* Generic 16550A UART emulation using 'hw/char/serial.c'
* MTTCG and SMP support (PLIC and CLINT) on the 'virt' machine

The RISC-V QEMU full system emulator supports 5 machines:

* 'spike_v1.9.1', CLINT, PLIC, HTIF console, config-string, Priv v1.9.1
* 'spike_v1.10', CLINT, PLIC, HTIF console, device-tree, Priv v1.10
* 'sifive_e', CLINT, PLIC, SiFive UART, HiFive1 compat, Priv v1.10
* 'sifive_u', CLINT, PLIC, SiFive UART, device-tree, Priv v1.10
* 'virt', CLINT, PLIC, 16550A UART, VirtIO, device-tree, Priv v1.10

This is a list of RISC-V QEMU Port Contributors:

* Alex Suykov
* Andreas Schwab
* Antony Pavlov
* Bastian Koppelmann
* Bruce Hoult
* Chih-Min Chao
* Daire McNamara
* Darius Rad
* David Abdurachmanov
* Hesham Almatary
* Ivan Griffin
* Jim Wilson
* Kito Cheng
* Michael Clark
* Palmer Dabbelt
* Richard Henderson
* Sagar Karandikar
* Shea Levy
* Stefan O'Rear

Notes:

* contributor email addresses available off-list on request.
* checkpatch has been run on all 23 patches.
* checkpatch exceptions are noted in patches that have errors.
* passes "make check" on full build for all targets
* tested riscv-linux-4.6.2 on 'spike_v1.9.1' machine
* tested riscv-linux-4.15 on 'spike_v1.10' and 'virt' machines
* tested SiFive HiFive1 binaries in 'sifive_e' machine
* tested RV64 on 32-bit i386

This patch series includes the following patches:

# gpg: Signature made Thu 08 Mar 2018 19:40:20 GMT
# gpg: using DSA key 6BF1D7B357EF3E4F
# gpg: Good signature from "Michael Clark <michaeljclark@mac.com>"
# gpg: aka "Michael Clark <mjc@sifive.com>"
# gpg: aka "Michael Clark <michael@metaparadigm.com>"
# gpg: WARNING: This key is not certified with a trusted signature!
# gpg: There is no indication that the signature belongs to the owner.
# Primary key fingerprint: 7C99 930E B17C D8BA 073D 5EFA 6BF1 D7B3 57EF 3E4F

* remotes/riscv/tags/riscv-qemu-upstream-v8.2: (23 commits)
RISC-V Build Infrastructure
SiFive Freedom U Series RISC-V Machine
SiFive Freedom E Series RISC-V Machine
SiFive RISC-V PRCI Block
SiFive RISC-V UART Device
RISC-V VirtIO Machine
SiFive RISC-V Test Finisher
RISC-V Spike Machines
SiFive RISC-V PLIC Block
SiFive RISC-V CLINT Block
RISC-V HART Array
RISC-V HTIF Console
Add symbol table callback interface to load_elf
RISC-V Linux User Emulation
RISC-V Physical Memory Protection
RISC-V TCG Code Generation
RISC-V GDB Stub
RISC-V FPU Support
RISC-V CPU Helpers
RISC-V Disassembler
...

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

+13310 -27
+11
MAINTAINERS
··· 233 233 F: include/hw/ppc/ 234 234 F: disas/ppc.c 235 235 236 + RISC-V 237 + M: Michael Clark <mjc@sifive.com> 238 + M: Palmer Dabbelt <palmer@sifive.com> 239 + M: Sagar Karandikar <sagark@eecs.berkeley.edu> 240 + M: Bastian Koppelmann <kbastian@mail.uni-paderborn.de> 241 + S: Maintained 242 + F: target/riscv/ 243 + F: hw/riscv/ 244 + F: include/hw/riscv/ 245 + F: disas/riscv.c 246 + 236 247 S390 237 248 M: Richard Henderson <rth@twiddle.net> 238 249 M: Alexander Graf <agraf@suse.de>
+2
arch_init.c
··· 71 71 #define QEMU_ARCH QEMU_ARCH_OPENRISC 72 72 #elif defined(TARGET_PPC) 73 73 #define QEMU_ARCH QEMU_ARCH_PPC 74 + #elif defined(TARGET_RISCV) 75 + #define QEMU_ARCH QEMU_ARCH_RISCV 74 76 #elif defined(TARGET_S390X) 75 77 #define QEMU_ARCH QEMU_ARCH_S390X 76 78 #elif defined(TARGET_SH4)
+13
configure
··· 6797 6797 echo "TARGET_ABI32=y" >> $config_target_mak 6798 6798 gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml power-vsx.xml" 6799 6799 ;; 6800 + riscv32) 6801 + TARGET_BASE_ARCH=riscv 6802 + TARGET_ABI_DIR=riscv 6803 + mttcg=yes 6804 + ;; 6805 + riscv64) 6806 + TARGET_BASE_ARCH=riscv 6807 + TARGET_ABI_DIR=riscv 6808 + mttcg=yes 6809 + ;; 6800 6810 sh4|sh4eb) 6801 6811 TARGET_ARCH=sh4 6802 6812 bflt="yes" ··· 6965 6975 ;; 6966 6976 ppc*) 6967 6977 disas_config "PPC" 6978 + ;; 6979 + riscv) 6980 + disas_config "RISCV" 6968 6981 ;; 6969 6982 s390*) 6970 6983 disas_config "S390"
+6
cpus.c
··· 2081 2081 #elif defined(TARGET_SPARC) 2082 2082 SPARCCPU *sparc_cpu = SPARC_CPU(cpu); 2083 2083 CPUSPARCState *env = &sparc_cpu->env; 2084 + #elif defined(TARGET_RISCV) 2085 + RISCVCPU *riscv_cpu = RISCV_CPU(cpu); 2086 + CPURISCVState *env = &riscv_cpu->env; 2084 2087 #elif defined(TARGET_MIPS) 2085 2088 MIPSCPU *mips_cpu = MIPS_CPU(cpu); 2086 2089 CPUMIPSState *env = &mips_cpu->env; ··· 2120 2123 #elif defined(TARGET_S390X) 2121 2124 info->value->arch = CPU_INFO_ARCH_S390; 2122 2125 info->value->u.s390.cpu_state = env->cpu_state; 2126 + #elif defined(TARGET_RISCV) 2127 + info->value->arch = CPU_INFO_ARCH_RISCV; 2128 + info->value->u.riscv.pc = env->pc; 2123 2129 #else 2124 2130 info->value->arch = CPU_INFO_ARCH_OTHER; 2125 2131 #endif
+1
default-configs/riscv32-linux-user.mak
··· 1 + # Default configuration for riscv-linux-user
+4
default-configs/riscv32-softmmu.mak
··· 1 + # Default configuration for riscv-softmmu 2 + 3 + CONFIG_SERIAL=y 4 + CONFIG_VIRTIO=y
+1
default-configs/riscv64-linux-user.mak
··· 1 + # Default configuration for riscv-linux-user
+4
default-configs/riscv64-softmmu.mak
··· 1 + # Default configuration for riscv-softmmu 2 + 3 + CONFIG_SERIAL=y 4 + CONFIG_VIRTIO=y
+2
disas.c
··· 522 522 # ifdef _ARCH_PPC64 523 523 s.info.cap_mode = CS_MODE_64; 524 524 # endif 525 + #elif defined(__riscv__) 526 + print_insn = print_insn_riscv; 525 527 #elif defined(__aarch64__) && defined(CONFIG_ARM_A64_DIS) 526 528 print_insn = print_insn_arm_a64; 527 529 s.info.cap_arch = CS_ARCH_ARM64;
+1
disas/Makefile.objs
··· 17 17 common-obj-$(CONFIG_NIOS2_DIS) += nios2.o 18 18 common-obj-$(CONFIG_MOXIE_DIS) += moxie.o 19 19 common-obj-$(CONFIG_PPC_DIS) += ppc.o 20 + common-obj-$(CONFIG_RISCV_DIS) += riscv.o 20 21 common-obj-$(CONFIG_S390_DIS) += s390.o 21 22 common-obj-$(CONFIG_SH4_DIS) += sh4.o 22 23 common-obj-$(CONFIG_SPARC_DIS) += sparc.o
+3048
disas/riscv.c
··· 1 + /* 2 + * QEMU RISC-V Disassembler 3 + * 4 + * Copyright (c) 2016-2017 Michael Clark <michaeljclark@mac.com> 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #include "qemu/osdep.h" 21 + #include "disas/bfd.h" 22 + 23 + 24 + /* types */ 25 + 26 + typedef uint64_t rv_inst; 27 + typedef uint16_t rv_opcode; 28 + 29 + /* enums */ 30 + 31 + typedef enum { 32 + rv32, 33 + rv64, 34 + rv128 35 + } rv_isa; 36 + 37 + typedef enum { 38 + rv_rm_rne = 0, 39 + rv_rm_rtz = 1, 40 + rv_rm_rdn = 2, 41 + rv_rm_rup = 3, 42 + rv_rm_rmm = 4, 43 + rv_rm_dyn = 7, 44 + } rv_rm; 45 + 46 + typedef enum { 47 + rv_fence_i = 8, 48 + rv_fence_o = 4, 49 + rv_fence_r = 2, 50 + rv_fence_w = 1, 51 + } rv_fence; 52 + 53 + typedef enum { 54 + rv_ireg_zero, 55 + rv_ireg_ra, 56 + rv_ireg_sp, 57 + rv_ireg_gp, 58 + rv_ireg_tp, 59 + rv_ireg_t0, 60 + rv_ireg_t1, 61 + rv_ireg_t2, 62 + rv_ireg_s0, 63 + rv_ireg_s1, 64 + rv_ireg_a0, 65 + rv_ireg_a1, 66 + rv_ireg_a2, 67 + rv_ireg_a3, 68 + rv_ireg_a4, 69 + rv_ireg_a5, 70 + rv_ireg_a6, 71 + rv_ireg_a7, 72 + rv_ireg_s2, 73 + rv_ireg_s3, 74 + rv_ireg_s4, 75 + rv_ireg_s5, 76 + rv_ireg_s6, 77 + rv_ireg_s7, 78 + rv_ireg_s8, 79 + rv_ireg_s9, 80 + rv_ireg_s10, 81 + rv_ireg_s11, 82 + rv_ireg_t3, 83 + rv_ireg_t4, 84 + rv_ireg_t5, 85 + rv_ireg_t6, 86 + } rv_ireg; 87 + 88 + typedef enum { 89 + rvc_end, 90 + rvc_simm_6, 91 + rvc_imm_6, 92 + rvc_imm_7, 93 + rvc_imm_8, 94 + rvc_imm_9, 95 + rvc_imm_10, 96 + rvc_imm_12, 97 + rvc_imm_18, 98 + rvc_imm_nz, 99 + rvc_imm_x2, 100 + rvc_imm_x4, 101 + rvc_imm_x8, 102 + rvc_imm_x16, 103 + rvc_rd_b3, 104 + rvc_rs1_b3, 105 + rvc_rs2_b3, 106 + rvc_rd_eq_rs1, 107 + rvc_rd_eq_ra, 108 + rvc_rd_eq_sp, 109 + rvc_rd_eq_x0, 110 + rvc_rs1_eq_sp, 111 + rvc_rs1_eq_x0, 112 + rvc_rs2_eq_x0, 113 + rvc_rd_ne_x0_x2, 114 + rvc_rd_ne_x0, 115 + rvc_rs1_ne_x0, 116 + rvc_rs2_ne_x0, 117 + rvc_rs2_eq_rs1, 118 + rvc_rs1_eq_ra, 119 + rvc_imm_eq_zero, 120 + rvc_imm_eq_n1, 121 + rvc_imm_eq_p1, 122 + rvc_csr_eq_0x001, 123 + rvc_csr_eq_0x002, 124 + rvc_csr_eq_0x003, 125 + rvc_csr_eq_0xc00, 126 + rvc_csr_eq_0xc01, 127 + rvc_csr_eq_0xc02, 128 + rvc_csr_eq_0xc80, 129 + rvc_csr_eq_0xc81, 130 + rvc_csr_eq_0xc82, 131 + } rvc_constraint; 132 + 133 + typedef enum { 134 + rv_codec_illegal, 135 + rv_codec_none, 136 + rv_codec_u, 137 + rv_codec_uj, 138 + rv_codec_i, 139 + rv_codec_i_sh5, 140 + rv_codec_i_sh6, 141 + rv_codec_i_sh7, 142 + rv_codec_i_csr, 143 + rv_codec_s, 144 + rv_codec_sb, 145 + rv_codec_r, 146 + rv_codec_r_m, 147 + rv_codec_r4_m, 148 + rv_codec_r_a, 149 + rv_codec_r_l, 150 + rv_codec_r_f, 151 + rv_codec_cb, 152 + rv_codec_cb_imm, 153 + rv_codec_cb_sh5, 154 + rv_codec_cb_sh6, 155 + rv_codec_ci, 156 + rv_codec_ci_sh5, 157 + rv_codec_ci_sh6, 158 + rv_codec_ci_16sp, 159 + rv_codec_ci_lwsp, 160 + rv_codec_ci_ldsp, 161 + rv_codec_ci_lqsp, 162 + rv_codec_ci_li, 163 + rv_codec_ci_lui, 164 + rv_codec_ci_none, 165 + rv_codec_ciw_4spn, 166 + rv_codec_cj, 167 + rv_codec_cj_jal, 168 + rv_codec_cl_lw, 169 + rv_codec_cl_ld, 170 + rv_codec_cl_lq, 171 + rv_codec_cr, 172 + rv_codec_cr_mv, 173 + rv_codec_cr_jalr, 174 + rv_codec_cr_jr, 175 + rv_codec_cs, 176 + rv_codec_cs_sw, 177 + rv_codec_cs_sd, 178 + rv_codec_cs_sq, 179 + rv_codec_css_swsp, 180 + rv_codec_css_sdsp, 181 + rv_codec_css_sqsp, 182 + } rv_codec; 183 + 184 + typedef enum { 185 + rv_op_illegal = 0, 186 + rv_op_lui = 1, 187 + rv_op_auipc = 2, 188 + rv_op_jal = 3, 189 + rv_op_jalr = 4, 190 + rv_op_beq = 5, 191 + rv_op_bne = 6, 192 + rv_op_blt = 7, 193 + rv_op_bge = 8, 194 + rv_op_bltu = 9, 195 + rv_op_bgeu = 10, 196 + rv_op_lb = 11, 197 + rv_op_lh = 12, 198 + rv_op_lw = 13, 199 + rv_op_lbu = 14, 200 + rv_op_lhu = 15, 201 + rv_op_sb = 16, 202 + rv_op_sh = 17, 203 + rv_op_sw = 18, 204 + rv_op_addi = 19, 205 + rv_op_slti = 20, 206 + rv_op_sltiu = 21, 207 + rv_op_xori = 22, 208 + rv_op_ori = 23, 209 + rv_op_andi = 24, 210 + rv_op_slli = 25, 211 + rv_op_srli = 26, 212 + rv_op_srai = 27, 213 + rv_op_add = 28, 214 + rv_op_sub = 29, 215 + rv_op_sll = 30, 216 + rv_op_slt = 31, 217 + rv_op_sltu = 32, 218 + rv_op_xor = 33, 219 + rv_op_srl = 34, 220 + rv_op_sra = 35, 221 + rv_op_or = 36, 222 + rv_op_and = 37, 223 + rv_op_fence = 38, 224 + rv_op_fence_i = 39, 225 + rv_op_lwu = 40, 226 + rv_op_ld = 41, 227 + rv_op_sd = 42, 228 + rv_op_addiw = 43, 229 + rv_op_slliw = 44, 230 + rv_op_srliw = 45, 231 + rv_op_sraiw = 46, 232 + rv_op_addw = 47, 233 + rv_op_subw = 48, 234 + rv_op_sllw = 49, 235 + rv_op_srlw = 50, 236 + rv_op_sraw = 51, 237 + rv_op_ldu = 52, 238 + rv_op_lq = 53, 239 + rv_op_sq = 54, 240 + rv_op_addid = 55, 241 + rv_op_sllid = 56, 242 + rv_op_srlid = 57, 243 + rv_op_sraid = 58, 244 + rv_op_addd = 59, 245 + rv_op_subd = 60, 246 + rv_op_slld = 61, 247 + rv_op_srld = 62, 248 + rv_op_srad = 63, 249 + rv_op_mul = 64, 250 + rv_op_mulh = 65, 251 + rv_op_mulhsu = 66, 252 + rv_op_mulhu = 67, 253 + rv_op_div = 68, 254 + rv_op_divu = 69, 255 + rv_op_rem = 70, 256 + rv_op_remu = 71, 257 + rv_op_mulw = 72, 258 + rv_op_divw = 73, 259 + rv_op_divuw = 74, 260 + rv_op_remw = 75, 261 + rv_op_remuw = 76, 262 + rv_op_muld = 77, 263 + rv_op_divd = 78, 264 + rv_op_divud = 79, 265 + rv_op_remd = 80, 266 + rv_op_remud = 81, 267 + rv_op_lr_w = 82, 268 + rv_op_sc_w = 83, 269 + rv_op_amoswap_w = 84, 270 + rv_op_amoadd_w = 85, 271 + rv_op_amoxor_w = 86, 272 + rv_op_amoor_w = 87, 273 + rv_op_amoand_w = 88, 274 + rv_op_amomin_w = 89, 275 + rv_op_amomax_w = 90, 276 + rv_op_amominu_w = 91, 277 + rv_op_amomaxu_w = 92, 278 + rv_op_lr_d = 93, 279 + rv_op_sc_d = 94, 280 + rv_op_amoswap_d = 95, 281 + rv_op_amoadd_d = 96, 282 + rv_op_amoxor_d = 97, 283 + rv_op_amoor_d = 98, 284 + rv_op_amoand_d = 99, 285 + rv_op_amomin_d = 100, 286 + rv_op_amomax_d = 101, 287 + rv_op_amominu_d = 102, 288 + rv_op_amomaxu_d = 103, 289 + rv_op_lr_q = 104, 290 + rv_op_sc_q = 105, 291 + rv_op_amoswap_q = 106, 292 + rv_op_amoadd_q = 107, 293 + rv_op_amoxor_q = 108, 294 + rv_op_amoor_q = 109, 295 + rv_op_amoand_q = 110, 296 + rv_op_amomin_q = 111, 297 + rv_op_amomax_q = 112, 298 + rv_op_amominu_q = 113, 299 + rv_op_amomaxu_q = 114, 300 + rv_op_ecall = 115, 301 + rv_op_ebreak = 116, 302 + rv_op_uret = 117, 303 + rv_op_sret = 118, 304 + rv_op_hret = 119, 305 + rv_op_mret = 120, 306 + rv_op_dret = 121, 307 + rv_op_sfence_vm = 122, 308 + rv_op_sfence_vma = 123, 309 + rv_op_wfi = 124, 310 + rv_op_csrrw = 125, 311 + rv_op_csrrs = 126, 312 + rv_op_csrrc = 127, 313 + rv_op_csrrwi = 128, 314 + rv_op_csrrsi = 129, 315 + rv_op_csrrci = 130, 316 + rv_op_flw = 131, 317 + rv_op_fsw = 132, 318 + rv_op_fmadd_s = 133, 319 + rv_op_fmsub_s = 134, 320 + rv_op_fnmsub_s = 135, 321 + rv_op_fnmadd_s = 136, 322 + rv_op_fadd_s = 137, 323 + rv_op_fsub_s = 138, 324 + rv_op_fmul_s = 139, 325 + rv_op_fdiv_s = 140, 326 + rv_op_fsgnj_s = 141, 327 + rv_op_fsgnjn_s = 142, 328 + rv_op_fsgnjx_s = 143, 329 + rv_op_fmin_s = 144, 330 + rv_op_fmax_s = 145, 331 + rv_op_fsqrt_s = 146, 332 + rv_op_fle_s = 147, 333 + rv_op_flt_s = 148, 334 + rv_op_feq_s = 149, 335 + rv_op_fcvt_w_s = 150, 336 + rv_op_fcvt_wu_s = 151, 337 + rv_op_fcvt_s_w = 152, 338 + rv_op_fcvt_s_wu = 153, 339 + rv_op_fmv_x_s = 154, 340 + rv_op_fclass_s = 155, 341 + rv_op_fmv_s_x = 156, 342 + rv_op_fcvt_l_s = 157, 343 + rv_op_fcvt_lu_s = 158, 344 + rv_op_fcvt_s_l = 159, 345 + rv_op_fcvt_s_lu = 160, 346 + rv_op_fld = 161, 347 + rv_op_fsd = 162, 348 + rv_op_fmadd_d = 163, 349 + rv_op_fmsub_d = 164, 350 + rv_op_fnmsub_d = 165, 351 + rv_op_fnmadd_d = 166, 352 + rv_op_fadd_d = 167, 353 + rv_op_fsub_d = 168, 354 + rv_op_fmul_d = 169, 355 + rv_op_fdiv_d = 170, 356 + rv_op_fsgnj_d = 171, 357 + rv_op_fsgnjn_d = 172, 358 + rv_op_fsgnjx_d = 173, 359 + rv_op_fmin_d = 174, 360 + rv_op_fmax_d = 175, 361 + rv_op_fcvt_s_d = 176, 362 + rv_op_fcvt_d_s = 177, 363 + rv_op_fsqrt_d = 178, 364 + rv_op_fle_d = 179, 365 + rv_op_flt_d = 180, 366 + rv_op_feq_d = 181, 367 + rv_op_fcvt_w_d = 182, 368 + rv_op_fcvt_wu_d = 183, 369 + rv_op_fcvt_d_w = 184, 370 + rv_op_fcvt_d_wu = 185, 371 + rv_op_fclass_d = 186, 372 + rv_op_fcvt_l_d = 187, 373 + rv_op_fcvt_lu_d = 188, 374 + rv_op_fmv_x_d = 189, 375 + rv_op_fcvt_d_l = 190, 376 + rv_op_fcvt_d_lu = 191, 377 + rv_op_fmv_d_x = 192, 378 + rv_op_flq = 193, 379 + rv_op_fsq = 194, 380 + rv_op_fmadd_q = 195, 381 + rv_op_fmsub_q = 196, 382 + rv_op_fnmsub_q = 197, 383 + rv_op_fnmadd_q = 198, 384 + rv_op_fadd_q = 199, 385 + rv_op_fsub_q = 200, 386 + rv_op_fmul_q = 201, 387 + rv_op_fdiv_q = 202, 388 + rv_op_fsgnj_q = 203, 389 + rv_op_fsgnjn_q = 204, 390 + rv_op_fsgnjx_q = 205, 391 + rv_op_fmin_q = 206, 392 + rv_op_fmax_q = 207, 393 + rv_op_fcvt_s_q = 208, 394 + rv_op_fcvt_q_s = 209, 395 + rv_op_fcvt_d_q = 210, 396 + rv_op_fcvt_q_d = 211, 397 + rv_op_fsqrt_q = 212, 398 + rv_op_fle_q = 213, 399 + rv_op_flt_q = 214, 400 + rv_op_feq_q = 215, 401 + rv_op_fcvt_w_q = 216, 402 + rv_op_fcvt_wu_q = 217, 403 + rv_op_fcvt_q_w = 218, 404 + rv_op_fcvt_q_wu = 219, 405 + rv_op_fclass_q = 220, 406 + rv_op_fcvt_l_q = 221, 407 + rv_op_fcvt_lu_q = 222, 408 + rv_op_fcvt_q_l = 223, 409 + rv_op_fcvt_q_lu = 224, 410 + rv_op_fmv_x_q = 225, 411 + rv_op_fmv_q_x = 226, 412 + rv_op_c_addi4spn = 227, 413 + rv_op_c_fld = 228, 414 + rv_op_c_lw = 229, 415 + rv_op_c_flw = 230, 416 + rv_op_c_fsd = 231, 417 + rv_op_c_sw = 232, 418 + rv_op_c_fsw = 233, 419 + rv_op_c_nop = 234, 420 + rv_op_c_addi = 235, 421 + rv_op_c_jal = 236, 422 + rv_op_c_li = 237, 423 + rv_op_c_addi16sp = 238, 424 + rv_op_c_lui = 239, 425 + rv_op_c_srli = 240, 426 + rv_op_c_srai = 241, 427 + rv_op_c_andi = 242, 428 + rv_op_c_sub = 243, 429 + rv_op_c_xor = 244, 430 + rv_op_c_or = 245, 431 + rv_op_c_and = 246, 432 + rv_op_c_subw = 247, 433 + rv_op_c_addw = 248, 434 + rv_op_c_j = 249, 435 + rv_op_c_beqz = 250, 436 + rv_op_c_bnez = 251, 437 + rv_op_c_slli = 252, 438 + rv_op_c_fldsp = 253, 439 + rv_op_c_lwsp = 254, 440 + rv_op_c_flwsp = 255, 441 + rv_op_c_jr = 256, 442 + rv_op_c_mv = 257, 443 + rv_op_c_ebreak = 258, 444 + rv_op_c_jalr = 259, 445 + rv_op_c_add = 260, 446 + rv_op_c_fsdsp = 261, 447 + rv_op_c_swsp = 262, 448 + rv_op_c_fswsp = 263, 449 + rv_op_c_ld = 264, 450 + rv_op_c_sd = 265, 451 + rv_op_c_addiw = 266, 452 + rv_op_c_ldsp = 267, 453 + rv_op_c_sdsp = 268, 454 + rv_op_c_lq = 269, 455 + rv_op_c_sq = 270, 456 + rv_op_c_lqsp = 271, 457 + rv_op_c_sqsp = 272, 458 + rv_op_nop = 273, 459 + rv_op_mv = 274, 460 + rv_op_not = 275, 461 + rv_op_neg = 276, 462 + rv_op_negw = 277, 463 + rv_op_sext_w = 278, 464 + rv_op_seqz = 279, 465 + rv_op_snez = 280, 466 + rv_op_sltz = 281, 467 + rv_op_sgtz = 282, 468 + rv_op_fmv_s = 283, 469 + rv_op_fabs_s = 284, 470 + rv_op_fneg_s = 285, 471 + rv_op_fmv_d = 286, 472 + rv_op_fabs_d = 287, 473 + rv_op_fneg_d = 288, 474 + rv_op_fmv_q = 289, 475 + rv_op_fabs_q = 290, 476 + rv_op_fneg_q = 291, 477 + rv_op_beqz = 292, 478 + rv_op_bnez = 293, 479 + rv_op_blez = 294, 480 + rv_op_bgez = 295, 481 + rv_op_bltz = 296, 482 + rv_op_bgtz = 297, 483 + rv_op_ble = 298, 484 + rv_op_bleu = 299, 485 + rv_op_bgt = 300, 486 + rv_op_bgtu = 301, 487 + rv_op_j = 302, 488 + rv_op_ret = 303, 489 + rv_op_jr = 304, 490 + rv_op_rdcycle = 305, 491 + rv_op_rdtime = 306, 492 + rv_op_rdinstret = 307, 493 + rv_op_rdcycleh = 308, 494 + rv_op_rdtimeh = 309, 495 + rv_op_rdinstreth = 310, 496 + rv_op_frcsr = 311, 497 + rv_op_frrm = 312, 498 + rv_op_frflags = 313, 499 + rv_op_fscsr = 314, 500 + rv_op_fsrm = 315, 501 + rv_op_fsflags = 316, 502 + rv_op_fsrmi = 317, 503 + rv_op_fsflagsi = 318, 504 + } rv_op; 505 + 506 + /* structures */ 507 + 508 + typedef struct { 509 + uint64_t pc; 510 + uint64_t inst; 511 + int32_t imm; 512 + uint16_t op; 513 + uint8_t codec; 514 + uint8_t rd; 515 + uint8_t rs1; 516 + uint8_t rs2; 517 + uint8_t rs3; 518 + uint8_t rm; 519 + uint8_t pred; 520 + uint8_t succ; 521 + uint8_t aq; 522 + uint8_t rl; 523 + } rv_decode; 524 + 525 + typedef struct { 526 + const int op; 527 + const rvc_constraint *constraints; 528 + } rv_comp_data; 529 + 530 + typedef struct { 531 + const char * const name; 532 + const rv_codec codec; 533 + const char * const format; 534 + const rv_comp_data *pseudo; 535 + const int decomp_rv32; 536 + const int decomp_rv64; 537 + const int decomp_rv128; 538 + } rv_opcode_data; 539 + 540 + /* register names */ 541 + 542 + static const char rv_ireg_name_sym[32][5] = { 543 + "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", 544 + "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", 545 + "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", 546 + "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", 547 + }; 548 + 549 + static const char rv_freg_name_sym[32][5] = { 550 + "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", 551 + "fs0", "fs1", "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", 552 + "fa6", "fa7", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", 553 + "fs8", "fs9", "fs10", "fs11", "ft8", "ft9", "ft10", "ft11", 554 + }; 555 + 556 + /* instruction formats */ 557 + 558 + #define rv_fmt_none "O\t" 559 + #define rv_fmt_rs1 "O\t1" 560 + #define rv_fmt_offset "O\to" 561 + #define rv_fmt_pred_succ "O\tp,s" 562 + #define rv_fmt_rs1_rs2 "O\t1,2" 563 + #define rv_fmt_rd_imm "O\t0,i" 564 + #define rv_fmt_rd_offset "O\t0,o" 565 + #define rv_fmt_rd_rs1_rs2 "O\t0,1,2" 566 + #define rv_fmt_frd_rs1 "O\t3,1" 567 + #define rv_fmt_rd_frs1 "O\t0,4" 568 + #define rv_fmt_rd_frs1_frs2 "O\t0,4,5" 569 + #define rv_fmt_frd_frs1_frs2 "O\t3,4,5" 570 + #define rv_fmt_rm_frd_frs1 "O\tr,3,4" 571 + #define rv_fmt_rm_frd_rs1 "O\tr,3,1" 572 + #define rv_fmt_rm_rd_frs1 "O\tr,0,4" 573 + #define rv_fmt_rm_frd_frs1_frs2 "O\tr,3,4,5" 574 + #define rv_fmt_rm_frd_frs1_frs2_frs3 "O\tr,3,4,5,6" 575 + #define rv_fmt_rd_rs1_imm "O\t0,1,i" 576 + #define rv_fmt_rd_rs1_offset "O\t0,1,i" 577 + #define rv_fmt_rd_offset_rs1 "O\t0,i(1)" 578 + #define rv_fmt_frd_offset_rs1 "O\t3,i(1)" 579 + #define rv_fmt_rd_csr_rs1 "O\t0,c,1" 580 + #define rv_fmt_rd_csr_zimm "O\t0,c,7" 581 + #define rv_fmt_rs2_offset_rs1 "O\t2,i(1)" 582 + #define rv_fmt_frs2_offset_rs1 "O\t5,i(1)" 583 + #define rv_fmt_rs1_rs2_offset "O\t1,2,o" 584 + #define rv_fmt_rs2_rs1_offset "O\t2,1,o" 585 + #define rv_fmt_aqrl_rd_rs2_rs1 "OAR\t0,2,(1)" 586 + #define rv_fmt_aqrl_rd_rs1 "OAR\t0,(1)" 587 + #define rv_fmt_rd "O\t0" 588 + #define rv_fmt_rd_zimm "O\t0,7" 589 + #define rv_fmt_rd_rs1 "O\t0,1" 590 + #define rv_fmt_rd_rs2 "O\t0,2" 591 + #define rv_fmt_rs1_offset "O\t1,o" 592 + #define rv_fmt_rs2_offset "O\t2,o" 593 + 594 + /* pseudo-instruction constraints */ 595 + 596 + static const rvc_constraint rvcc_jal[] = { rvc_rd_eq_ra, rvc_end }; 597 + static const rvc_constraint rvcc_jalr[] = { rvc_rd_eq_ra, rvc_imm_eq_zero, rvc_end }; 598 + static const rvc_constraint rvcc_nop[] = { rvc_rd_eq_x0, rvc_rs1_eq_x0, rvc_imm_eq_zero, rvc_end }; 599 + static const rvc_constraint rvcc_mv[] = { rvc_imm_eq_zero, rvc_end }; 600 + static const rvc_constraint rvcc_not[] = { rvc_imm_eq_n1, rvc_end }; 601 + static const rvc_constraint rvcc_neg[] = { rvc_rs1_eq_x0, rvc_end }; 602 + static const rvc_constraint rvcc_negw[] = { rvc_rs1_eq_x0, rvc_end }; 603 + static const rvc_constraint rvcc_sext_w[] = { rvc_rs2_eq_x0, rvc_end }; 604 + static const rvc_constraint rvcc_seqz[] = { rvc_imm_eq_p1, rvc_end }; 605 + static const rvc_constraint rvcc_snez[] = { rvc_rs1_eq_x0, rvc_end }; 606 + static const rvc_constraint rvcc_sltz[] = { rvc_rs2_eq_x0, rvc_end }; 607 + static const rvc_constraint rvcc_sgtz[] = { rvc_rs1_eq_x0, rvc_end }; 608 + static const rvc_constraint rvcc_fmv_s[] = { rvc_rs2_eq_rs1, rvc_end }; 609 + static const rvc_constraint rvcc_fabs_s[] = { rvc_rs2_eq_rs1, rvc_end }; 610 + static const rvc_constraint rvcc_fneg_s[] = { rvc_rs2_eq_rs1, rvc_end }; 611 + static const rvc_constraint rvcc_fmv_d[] = { rvc_rs2_eq_rs1, rvc_end }; 612 + static const rvc_constraint rvcc_fabs_d[] = { rvc_rs2_eq_rs1, rvc_end }; 613 + static const rvc_constraint rvcc_fneg_d[] = { rvc_rs2_eq_rs1, rvc_end }; 614 + static const rvc_constraint rvcc_fmv_q[] = { rvc_rs2_eq_rs1, rvc_end }; 615 + static const rvc_constraint rvcc_fabs_q[] = { rvc_rs2_eq_rs1, rvc_end }; 616 + static const rvc_constraint rvcc_fneg_q[] = { rvc_rs2_eq_rs1, rvc_end }; 617 + static const rvc_constraint rvcc_beqz[] = { rvc_rs2_eq_x0, rvc_end }; 618 + static const rvc_constraint rvcc_bnez[] = { rvc_rs2_eq_x0, rvc_end }; 619 + static const rvc_constraint rvcc_blez[] = { rvc_rs1_eq_x0, rvc_end }; 620 + static const rvc_constraint rvcc_bgez[] = { rvc_rs2_eq_x0, rvc_end }; 621 + static const rvc_constraint rvcc_bltz[] = { rvc_rs2_eq_x0, rvc_end }; 622 + static const rvc_constraint rvcc_bgtz[] = { rvc_rs1_eq_x0, rvc_end }; 623 + static const rvc_constraint rvcc_ble[] = { rvc_end }; 624 + static const rvc_constraint rvcc_bleu[] = { rvc_end }; 625 + static const rvc_constraint rvcc_bgt[] = { rvc_end }; 626 + static const rvc_constraint rvcc_bgtu[] = { rvc_end }; 627 + static const rvc_constraint rvcc_j[] = { rvc_rd_eq_x0, rvc_end }; 628 + static const rvc_constraint rvcc_ret[] = { rvc_rd_eq_x0, rvc_rs1_eq_ra, rvc_end }; 629 + static const rvc_constraint rvcc_jr[] = { rvc_rd_eq_x0, rvc_imm_eq_zero, rvc_end }; 630 + static const rvc_constraint rvcc_rdcycle[] = { rvc_rs1_eq_x0, rvc_csr_eq_0xc00, rvc_end }; 631 + static const rvc_constraint rvcc_rdtime[] = { rvc_rs1_eq_x0, rvc_csr_eq_0xc01, rvc_end }; 632 + static const rvc_constraint rvcc_rdinstret[] = { rvc_rs1_eq_x0, rvc_csr_eq_0xc02, rvc_end }; 633 + static const rvc_constraint rvcc_rdcycleh[] = { rvc_rs1_eq_x0, rvc_csr_eq_0xc80, rvc_end }; 634 + static const rvc_constraint rvcc_rdtimeh[] = { rvc_rs1_eq_x0, rvc_csr_eq_0xc81, rvc_end }; 635 + static const rvc_constraint rvcc_rdinstreth[] = { rvc_rs1_eq_x0, rvc_csr_eq_0xc80, rvc_end }; 636 + static const rvc_constraint rvcc_frcsr[] = { rvc_rs1_eq_x0, rvc_csr_eq_0x003, rvc_end }; 637 + static const rvc_constraint rvcc_frrm[] = { rvc_rs1_eq_x0, rvc_csr_eq_0x002, rvc_end }; 638 + static const rvc_constraint rvcc_frflags[] = { rvc_rs1_eq_x0, rvc_csr_eq_0x001, rvc_end }; 639 + static const rvc_constraint rvcc_fscsr[] = { rvc_csr_eq_0x003, rvc_end }; 640 + static const rvc_constraint rvcc_fsrm[] = { rvc_csr_eq_0x002, rvc_end }; 641 + static const rvc_constraint rvcc_fsflags[] = { rvc_csr_eq_0x001, rvc_end }; 642 + static const rvc_constraint rvcc_fsrmi[] = { rvc_csr_eq_0x002, rvc_end }; 643 + static const rvc_constraint rvcc_fsflagsi[] = { rvc_csr_eq_0x001, rvc_end }; 644 + 645 + /* pseudo-instruction metadata */ 646 + 647 + static const rv_comp_data rvcp_jal[] = { 648 + { rv_op_j, rvcc_j }, 649 + { rv_op_jal, rvcc_jal }, 650 + { rv_op_illegal, NULL } 651 + }; 652 + 653 + static const rv_comp_data rvcp_jalr[] = { 654 + { rv_op_ret, rvcc_ret }, 655 + { rv_op_jr, rvcc_jr }, 656 + { rv_op_jalr, rvcc_jalr }, 657 + { rv_op_illegal, NULL } 658 + }; 659 + 660 + static const rv_comp_data rvcp_beq[] = { 661 + { rv_op_beqz, rvcc_beqz }, 662 + { rv_op_illegal, NULL } 663 + }; 664 + 665 + static const rv_comp_data rvcp_bne[] = { 666 + { rv_op_bnez, rvcc_bnez }, 667 + { rv_op_illegal, NULL } 668 + }; 669 + 670 + static const rv_comp_data rvcp_blt[] = { 671 + { rv_op_bltz, rvcc_bltz }, 672 + { rv_op_bgtz, rvcc_bgtz }, 673 + { rv_op_bgt, rvcc_bgt }, 674 + { rv_op_illegal, NULL } 675 + }; 676 + 677 + static const rv_comp_data rvcp_bge[] = { 678 + { rv_op_blez, rvcc_blez }, 679 + { rv_op_bgez, rvcc_bgez }, 680 + { rv_op_ble, rvcc_ble }, 681 + { rv_op_illegal, NULL } 682 + }; 683 + 684 + static const rv_comp_data rvcp_bltu[] = { 685 + { rv_op_bgtu, rvcc_bgtu }, 686 + { rv_op_illegal, NULL } 687 + }; 688 + 689 + static const rv_comp_data rvcp_bgeu[] = { 690 + { rv_op_bleu, rvcc_bleu }, 691 + { rv_op_illegal, NULL } 692 + }; 693 + 694 + static const rv_comp_data rvcp_addi[] = { 695 + { rv_op_nop, rvcc_nop }, 696 + { rv_op_mv, rvcc_mv }, 697 + { rv_op_illegal, NULL } 698 + }; 699 + 700 + static const rv_comp_data rvcp_sltiu[] = { 701 + { rv_op_seqz, rvcc_seqz }, 702 + { rv_op_illegal, NULL } 703 + }; 704 + 705 + static const rv_comp_data rvcp_xori[] = { 706 + { rv_op_not, rvcc_not }, 707 + { rv_op_illegal, NULL } 708 + }; 709 + 710 + static const rv_comp_data rvcp_sub[] = { 711 + { rv_op_neg, rvcc_neg }, 712 + { rv_op_illegal, NULL } 713 + }; 714 + 715 + static const rv_comp_data rvcp_slt[] = { 716 + { rv_op_sltz, rvcc_sltz }, 717 + { rv_op_sgtz, rvcc_sgtz }, 718 + { rv_op_illegal, NULL } 719 + }; 720 + 721 + static const rv_comp_data rvcp_sltu[] = { 722 + { rv_op_snez, rvcc_snez }, 723 + { rv_op_illegal, NULL } 724 + }; 725 + 726 + static const rv_comp_data rvcp_addiw[] = { 727 + { rv_op_sext_w, rvcc_sext_w }, 728 + { rv_op_illegal, NULL } 729 + }; 730 + 731 + static const rv_comp_data rvcp_subw[] = { 732 + { rv_op_negw, rvcc_negw }, 733 + { rv_op_illegal, NULL } 734 + }; 735 + 736 + static const rv_comp_data rvcp_csrrw[] = { 737 + { rv_op_fscsr, rvcc_fscsr }, 738 + { rv_op_fsrm, rvcc_fsrm }, 739 + { rv_op_fsflags, rvcc_fsflags }, 740 + { rv_op_illegal, NULL } 741 + }; 742 + 743 + static const rv_comp_data rvcp_csrrs[] = { 744 + { rv_op_rdcycle, rvcc_rdcycle }, 745 + { rv_op_rdtime, rvcc_rdtime }, 746 + { rv_op_rdinstret, rvcc_rdinstret }, 747 + { rv_op_rdcycleh, rvcc_rdcycleh }, 748 + { rv_op_rdtimeh, rvcc_rdtimeh }, 749 + { rv_op_rdinstreth, rvcc_rdinstreth }, 750 + { rv_op_frcsr, rvcc_frcsr }, 751 + { rv_op_frrm, rvcc_frrm }, 752 + { rv_op_frflags, rvcc_frflags }, 753 + { rv_op_illegal, NULL } 754 + }; 755 + 756 + static const rv_comp_data rvcp_csrrwi[] = { 757 + { rv_op_fsrmi, rvcc_fsrmi }, 758 + { rv_op_fsflagsi, rvcc_fsflagsi }, 759 + { rv_op_illegal, NULL } 760 + }; 761 + 762 + static const rv_comp_data rvcp_fsgnj_s[] = { 763 + { rv_op_fmv_s, rvcc_fmv_s }, 764 + { rv_op_illegal, NULL } 765 + }; 766 + 767 + static const rv_comp_data rvcp_fsgnjn_s[] = { 768 + { rv_op_fneg_s, rvcc_fneg_s }, 769 + { rv_op_illegal, NULL } 770 + }; 771 + 772 + static const rv_comp_data rvcp_fsgnjx_s[] = { 773 + { rv_op_fabs_s, rvcc_fabs_s }, 774 + { rv_op_illegal, NULL } 775 + }; 776 + 777 + static const rv_comp_data rvcp_fsgnj_d[] = { 778 + { rv_op_fmv_d, rvcc_fmv_d }, 779 + { rv_op_illegal, NULL } 780 + }; 781 + 782 + static const rv_comp_data rvcp_fsgnjn_d[] = { 783 + { rv_op_fneg_d, rvcc_fneg_d }, 784 + { rv_op_illegal, NULL } 785 + }; 786 + 787 + static const rv_comp_data rvcp_fsgnjx_d[] = { 788 + { rv_op_fabs_d, rvcc_fabs_d }, 789 + { rv_op_illegal, NULL } 790 + }; 791 + 792 + static const rv_comp_data rvcp_fsgnj_q[] = { 793 + { rv_op_fmv_q, rvcc_fmv_q }, 794 + { rv_op_illegal, NULL } 795 + }; 796 + 797 + static const rv_comp_data rvcp_fsgnjn_q[] = { 798 + { rv_op_fneg_q, rvcc_fneg_q }, 799 + { rv_op_illegal, NULL } 800 + }; 801 + 802 + static const rv_comp_data rvcp_fsgnjx_q[] = { 803 + { rv_op_fabs_q, rvcc_fabs_q }, 804 + { rv_op_illegal, NULL } 805 + }; 806 + 807 + /* instruction metadata */ 808 + 809 + const rv_opcode_data opcode_data[] = { 810 + { "illegal", rv_codec_illegal, rv_fmt_none, NULL, 0, 0, 0 }, 811 + { "lui", rv_codec_u, rv_fmt_rd_imm, NULL, 0, 0, 0 }, 812 + { "auipc", rv_codec_u, rv_fmt_rd_offset, NULL, 0, 0, 0 }, 813 + { "jal", rv_codec_uj, rv_fmt_rd_offset, rvcp_jal, 0, 0, 0 }, 814 + { "jalr", rv_codec_i, rv_fmt_rd_rs1_offset, rvcp_jalr, 0, 0, 0 }, 815 + { "beq", rv_codec_sb, rv_fmt_rs1_rs2_offset, rvcp_beq, 0, 0, 0 }, 816 + { "bne", rv_codec_sb, rv_fmt_rs1_rs2_offset, rvcp_bne, 0, 0, 0 }, 817 + { "blt", rv_codec_sb, rv_fmt_rs1_rs2_offset, rvcp_blt, 0, 0, 0 }, 818 + { "bge", rv_codec_sb, rv_fmt_rs1_rs2_offset, rvcp_bge, 0, 0, 0 }, 819 + { "bltu", rv_codec_sb, rv_fmt_rs1_rs2_offset, rvcp_bltu, 0, 0, 0 }, 820 + { "bgeu", rv_codec_sb, rv_fmt_rs1_rs2_offset, rvcp_bgeu, 0, 0, 0 }, 821 + { "lb", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 822 + { "lh", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 823 + { "lw", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 824 + { "lbu", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 825 + { "lhu", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 826 + { "sb", rv_codec_s, rv_fmt_rs2_offset_rs1, NULL, 0, 0, 0 }, 827 + { "sh", rv_codec_s, rv_fmt_rs2_offset_rs1, NULL, 0, 0, 0 }, 828 + { "sw", rv_codec_s, rv_fmt_rs2_offset_rs1, NULL, 0, 0, 0 }, 829 + { "addi", rv_codec_i, rv_fmt_rd_rs1_imm, rvcp_addi, 0, 0, 0 }, 830 + { "slti", rv_codec_i, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 831 + { "sltiu", rv_codec_i, rv_fmt_rd_rs1_imm, rvcp_sltiu, 0, 0, 0 }, 832 + { "xori", rv_codec_i, rv_fmt_rd_rs1_imm, rvcp_xori, 0, 0, 0 }, 833 + { "ori", rv_codec_i, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 834 + { "andi", rv_codec_i, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 835 + { "slli", rv_codec_i_sh7, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 836 + { "srli", rv_codec_i_sh7, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 837 + { "srai", rv_codec_i_sh7, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 838 + { "add", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 839 + { "sub", rv_codec_r, rv_fmt_rd_rs1_rs2, rvcp_sub, 0, 0, 0 }, 840 + { "sll", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 841 + { "slt", rv_codec_r, rv_fmt_rd_rs1_rs2, rvcp_slt, 0, 0, 0 }, 842 + { "sltu", rv_codec_r, rv_fmt_rd_rs1_rs2, rvcp_sltu, 0, 0, 0 }, 843 + { "xor", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 844 + { "srl", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 845 + { "sra", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 846 + { "or", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 847 + { "and", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 848 + { "fence", rv_codec_r_f, rv_fmt_pred_succ, NULL, 0, 0, 0 }, 849 + { "fence.i", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 850 + { "lwu", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 851 + { "ld", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 852 + { "sd", rv_codec_s, rv_fmt_rs2_offset_rs1, NULL, 0, 0, 0 }, 853 + { "addiw", rv_codec_i, rv_fmt_rd_rs1_imm, rvcp_addiw, 0, 0, 0 }, 854 + { "slliw", rv_codec_i_sh5, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 855 + { "srliw", rv_codec_i_sh5, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 856 + { "sraiw", rv_codec_i_sh5, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 857 + { "addw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 858 + { "subw", rv_codec_r, rv_fmt_rd_rs1_rs2, rvcp_subw, 0, 0, 0 }, 859 + { "sllw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 860 + { "srlw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 861 + { "sraw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 862 + { "ldu", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 863 + { "lq", rv_codec_i, rv_fmt_rd_offset_rs1, NULL, 0, 0, 0 }, 864 + { "sq", rv_codec_s, rv_fmt_rs2_offset_rs1, NULL, 0, 0, 0 }, 865 + { "addid", rv_codec_i, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 866 + { "sllid", rv_codec_i_sh6, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 867 + { "srlid", rv_codec_i_sh6, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 868 + { "sraid", rv_codec_i_sh6, rv_fmt_rd_rs1_imm, NULL, 0, 0, 0 }, 869 + { "addd", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 870 + { "subd", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 871 + { "slld", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 872 + { "srld", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 873 + { "srad", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 874 + { "mul", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 875 + { "mulh", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 876 + { "mulhsu", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 877 + { "mulhu", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 878 + { "div", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 879 + { "divu", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 880 + { "rem", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 881 + { "remu", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 882 + { "mulw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 883 + { "divw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 884 + { "divuw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 885 + { "remw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 886 + { "remuw", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 887 + { "muld", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 888 + { "divd", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 889 + { "divud", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 890 + { "remd", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 891 + { "remud", rv_codec_r, rv_fmt_rd_rs1_rs2, NULL, 0, 0, 0 }, 892 + { "lr.w", rv_codec_r_l, rv_fmt_aqrl_rd_rs1, NULL, 0, 0, 0 }, 893 + { "sc.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 894 + { "amoswap.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 895 + { "amoadd.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 896 + { "amoxor.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 897 + { "amoor.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 898 + { "amoand.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 899 + { "amomin.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 900 + { "amomax.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 901 + { "amominu.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 902 + { "amomaxu.w", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 903 + { "lr.d", rv_codec_r_l, rv_fmt_aqrl_rd_rs1, NULL, 0, 0, 0 }, 904 + { "sc.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 905 + { "amoswap.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 906 + { "amoadd.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 907 + { "amoxor.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 908 + { "amoor.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 909 + { "amoand.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 910 + { "amomin.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 911 + { "amomax.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 912 + { "amominu.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 913 + { "amomaxu.d", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 914 + { "lr.q", rv_codec_r_l, rv_fmt_aqrl_rd_rs1, NULL, 0, 0, 0 }, 915 + { "sc.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 916 + { "amoswap.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 917 + { "amoadd.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 918 + { "amoxor.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 919 + { "amoor.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 920 + { "amoand.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 921 + { "amomin.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 922 + { "amomax.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 923 + { "amominu.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 924 + { "amomaxu.q", rv_codec_r_a, rv_fmt_aqrl_rd_rs2_rs1, NULL, 0, 0, 0 }, 925 + { "ecall", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 926 + { "ebreak", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 927 + { "uret", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 928 + { "sret", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 929 + { "hret", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 930 + { "mret", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 931 + { "dret", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 932 + { "sfence.vm", rv_codec_r, rv_fmt_rs1, NULL, 0, 0, 0 }, 933 + { "sfence.vma", rv_codec_r, rv_fmt_rs1_rs2, NULL, 0, 0, 0 }, 934 + { "wfi", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 }, 935 + { "csrrw", rv_codec_i_csr, rv_fmt_rd_csr_rs1, rvcp_csrrw, 0, 0, 0 }, 936 + { "csrrs", rv_codec_i_csr, rv_fmt_rd_csr_rs1, rvcp_csrrs, 0, 0, 0 }, 937 + { "csrrc", rv_codec_i_csr, rv_fmt_rd_csr_rs1, NULL, 0, 0, 0 }, 938 + { "csrrwi", rv_codec_i_csr, rv_fmt_rd_csr_zimm, rvcp_csrrwi, 0, 0, 0 }, 939 + { "csrrsi", rv_codec_i_csr, rv_fmt_rd_csr_zimm, NULL, 0, 0, 0 }, 940 + { "csrrci", rv_codec_i_csr, rv_fmt_rd_csr_zimm, NULL, 0, 0, 0 }, 941 + { "flw", rv_codec_i, rv_fmt_frd_offset_rs1, NULL, 0, 0, 0 }, 942 + { "fsw", rv_codec_s, rv_fmt_frs2_offset_rs1, NULL, 0, 0, 0 }, 943 + { "fmadd.s", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 944 + { "fmsub.s", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 945 + { "fnmsub.s", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 946 + { "fnmadd.s", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 947 + { "fadd.s", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 948 + { "fsub.s", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 949 + { "fmul.s", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 950 + { "fdiv.s", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 951 + { "fsgnj.s", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnj_s, 0, 0, 0 }, 952 + { "fsgnjn.s", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnjn_s, 0, 0, 0 }, 953 + { "fsgnjx.s", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnjx_s, 0, 0, 0 }, 954 + { "fmin.s", rv_codec_r, rv_fmt_frd_frs1_frs2, NULL, 0, 0, 0 }, 955 + { "fmax.s", rv_codec_r, rv_fmt_frd_frs1_frs2, NULL, 0, 0, 0 }, 956 + { "fsqrt.s", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 957 + { "fle.s", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 958 + { "flt.s", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 959 + { "feq.s", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 960 + { "fcvt.w.s", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 961 + { "fcvt.wu.s", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 962 + { "fcvt.s.w", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 963 + { "fcvt.s.wu", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 964 + { "fmv.x.s", rv_codec_r, rv_fmt_rd_frs1, NULL, 0, 0, 0 }, 965 + { "fclass.s", rv_codec_r, rv_fmt_rd_frs1, NULL, 0, 0, 0 }, 966 + { "fmv.s.x", rv_codec_r, rv_fmt_frd_rs1, NULL, 0, 0, 0 }, 967 + { "fcvt.l.s", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 968 + { "fcvt.lu.s", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 969 + { "fcvt.s.l", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 970 + { "fcvt.s.lu", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 971 + { "fld", rv_codec_i, rv_fmt_frd_offset_rs1, NULL, 0, 0, 0 }, 972 + { "fsd", rv_codec_s, rv_fmt_frs2_offset_rs1, NULL, 0, 0, 0 }, 973 + { "fmadd.d", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 974 + { "fmsub.d", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 975 + { "fnmsub.d", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 976 + { "fnmadd.d", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 977 + { "fadd.d", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 978 + { "fsub.d", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 979 + { "fmul.d", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 980 + { "fdiv.d", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 981 + { "fsgnj.d", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnj_d, 0, 0, 0 }, 982 + { "fsgnjn.d", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnjn_d, 0, 0, 0 }, 983 + { "fsgnjx.d", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnjx_d, 0, 0, 0 }, 984 + { "fmin.d", rv_codec_r, rv_fmt_frd_frs1_frs2, NULL, 0, 0, 0 }, 985 + { "fmax.d", rv_codec_r, rv_fmt_frd_frs1_frs2, NULL, 0, 0, 0 }, 986 + { "fcvt.s.d", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 987 + { "fcvt.d.s", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 988 + { "fsqrt.d", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 989 + { "fle.d", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 990 + { "flt.d", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 991 + { "feq.d", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 992 + { "fcvt.w.d", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 993 + { "fcvt.wu.d", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 994 + { "fcvt.d.w", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 995 + { "fcvt.d.wu", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 996 + { "fclass.d", rv_codec_r, rv_fmt_rd_frs1, NULL, 0, 0, 0 }, 997 + { "fcvt.l.d", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 998 + { "fcvt.lu.d", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 999 + { "fmv.x.d", rv_codec_r, rv_fmt_rd_frs1, NULL, 0, 0, 0 }, 1000 + { "fcvt.d.l", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 1001 + { "fcvt.d.lu", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 1002 + { "fmv.d.x", rv_codec_r, rv_fmt_frd_rs1, NULL, 0, 0, 0 }, 1003 + { "flq", rv_codec_i, rv_fmt_frd_offset_rs1, NULL, 0, 0, 0 }, 1004 + { "fsq", rv_codec_s, rv_fmt_frs2_offset_rs1, NULL, 0, 0, 0 }, 1005 + { "fmadd.q", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 1006 + { "fmsub.q", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 1007 + { "fnmsub.q", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 1008 + { "fnmadd.q", rv_codec_r4_m, rv_fmt_rm_frd_frs1_frs2_frs3, NULL, 0, 0, 0 }, 1009 + { "fadd.q", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 1010 + { "fsub.q", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 1011 + { "fmul.q", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 1012 + { "fdiv.q", rv_codec_r_m, rv_fmt_rm_frd_frs1_frs2, NULL, 0, 0, 0 }, 1013 + { "fsgnj.q", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnj_q, 0, 0, 0 }, 1014 + { "fsgnjn.q", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnjn_q, 0, 0, 0 }, 1015 + { "fsgnjx.q", rv_codec_r, rv_fmt_frd_frs1_frs2, rvcp_fsgnjx_q, 0, 0, 0 }, 1016 + { "fmin.q", rv_codec_r, rv_fmt_frd_frs1_frs2, NULL, 0, 0, 0 }, 1017 + { "fmax.q", rv_codec_r, rv_fmt_frd_frs1_frs2, NULL, 0, 0, 0 }, 1018 + { "fcvt.s.q", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 1019 + { "fcvt.q.s", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 1020 + { "fcvt.d.q", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 1021 + { "fcvt.q.d", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 1022 + { "fsqrt.q", rv_codec_r_m, rv_fmt_rm_frd_frs1, NULL, 0, 0, 0 }, 1023 + { "fle.q", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 1024 + { "flt.q", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 1025 + { "feq.q", rv_codec_r, rv_fmt_rd_frs1_frs2, NULL, 0, 0, 0 }, 1026 + { "fcvt.w.q", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 1027 + { "fcvt.wu.q", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 1028 + { "fcvt.q.w", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 1029 + { "fcvt.q.wu", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 1030 + { "fclass.q", rv_codec_r, rv_fmt_rd_frs1, NULL, 0, 0, 0 }, 1031 + { "fcvt.l.q", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 1032 + { "fcvt.lu.q", rv_codec_r_m, rv_fmt_rm_rd_frs1, NULL, 0, 0, 0 }, 1033 + { "fcvt.q.l", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 1034 + { "fcvt.q.lu", rv_codec_r_m, rv_fmt_rm_frd_rs1, NULL, 0, 0, 0 }, 1035 + { "fmv.x.q", rv_codec_r, rv_fmt_rd_frs1, NULL, 0, 0, 0 }, 1036 + { "fmv.q.x", rv_codec_r, rv_fmt_frd_rs1, NULL, 0, 0, 0 }, 1037 + { "c.addi4spn", rv_codec_ciw_4spn, rv_fmt_rd_rs1_imm, NULL, rv_op_addi, rv_op_addi, rv_op_addi }, 1038 + { "c.fld", rv_codec_cl_ld, rv_fmt_frd_offset_rs1, NULL, rv_op_fld, rv_op_fld, 0 }, 1039 + { "c.lw", rv_codec_cl_lw, rv_fmt_rd_offset_rs1, NULL, rv_op_lw, rv_op_lw, rv_op_lw }, 1040 + { "c.flw", rv_codec_cl_lw, rv_fmt_frd_offset_rs1, NULL, rv_op_flw, 0, 0 }, 1041 + { "c.fsd", rv_codec_cs_sd, rv_fmt_frs2_offset_rs1, NULL, rv_op_fsd, rv_op_fsd, 0 }, 1042 + { "c.sw", rv_codec_cs_sw, rv_fmt_rs2_offset_rs1, NULL, rv_op_sw, rv_op_sw, rv_op_sw }, 1043 + { "c.fsw", rv_codec_cs_sw, rv_fmt_frs2_offset_rs1, NULL, rv_op_fsw, 0, 0 }, 1044 + { "c.nop", rv_codec_ci_none, rv_fmt_none, NULL, rv_op_addi, rv_op_addi, rv_op_addi }, 1045 + { "c.addi", rv_codec_ci, rv_fmt_rd_rs1_imm, NULL, rv_op_addi, rv_op_addi, rv_op_addi }, 1046 + { "c.jal", rv_codec_cj_jal, rv_fmt_rd_offset, NULL, rv_op_jal, 0, 0 }, 1047 + { "c.li", rv_codec_ci_li, rv_fmt_rd_rs1_imm, NULL, rv_op_addi, rv_op_addi, rv_op_addi }, 1048 + { "c.addi16sp", rv_codec_ci_16sp, rv_fmt_rd_rs1_imm, NULL, rv_op_addi, rv_op_addi, rv_op_addi }, 1049 + { "c.lui", rv_codec_ci_lui, rv_fmt_rd_imm, NULL, rv_op_lui, rv_op_lui, rv_op_lui }, 1050 + { "c.srli", rv_codec_cb_sh6, rv_fmt_rd_rs1_imm, NULL, rv_op_srli, rv_op_srli, rv_op_srli }, 1051 + { "c.srai", rv_codec_cb_sh6, rv_fmt_rd_rs1_imm, NULL, rv_op_srai, rv_op_srai, rv_op_srai }, 1052 + { "c.andi", rv_codec_cb_imm, rv_fmt_rd_rs1_imm, NULL, rv_op_andi, rv_op_andi, rv_op_andi }, 1053 + { "c.sub", rv_codec_cs, rv_fmt_rd_rs1_rs2, NULL, rv_op_sub, rv_op_sub, rv_op_sub }, 1054 + { "c.xor", rv_codec_cs, rv_fmt_rd_rs1_rs2, NULL, rv_op_xor, rv_op_xor, rv_op_xor }, 1055 + { "c.or", rv_codec_cs, rv_fmt_rd_rs1_rs2, NULL, rv_op_or, rv_op_or, rv_op_or }, 1056 + { "c.and", rv_codec_cs, rv_fmt_rd_rs1_rs2, NULL, rv_op_and, rv_op_and, rv_op_and }, 1057 + { "c.subw", rv_codec_cs, rv_fmt_rd_rs1_rs2, NULL, rv_op_subw, rv_op_subw, rv_op_subw }, 1058 + { "c.addw", rv_codec_cs, rv_fmt_rd_rs1_rs2, NULL, rv_op_addw, rv_op_addw, rv_op_addw }, 1059 + { "c.j", rv_codec_cj, rv_fmt_rd_offset, NULL, rv_op_jal, rv_op_jal, rv_op_jal }, 1060 + { "c.beqz", rv_codec_cb, rv_fmt_rs1_rs2_offset, NULL, rv_op_beq, rv_op_beq, rv_op_beq }, 1061 + { "c.bnez", rv_codec_cb, rv_fmt_rs1_rs2_offset, NULL, rv_op_bne, rv_op_bne, rv_op_bne }, 1062 + { "c.slli", rv_codec_ci_sh6, rv_fmt_rd_rs1_imm, NULL, rv_op_slli, rv_op_slli, rv_op_slli }, 1063 + { "c.fldsp", rv_codec_ci_ldsp, rv_fmt_frd_offset_rs1, NULL, rv_op_fld, rv_op_fld, rv_op_fld }, 1064 + { "c.lwsp", rv_codec_ci_lwsp, rv_fmt_rd_offset_rs1, NULL, rv_op_lw, rv_op_lw, rv_op_lw }, 1065 + { "c.flwsp", rv_codec_ci_lwsp, rv_fmt_frd_offset_rs1, NULL, rv_op_flw, 0, 0 }, 1066 + { "c.jr", rv_codec_cr_jr, rv_fmt_rd_rs1_offset, NULL, rv_op_jalr, rv_op_jalr, rv_op_jalr }, 1067 + { "c.mv", rv_codec_cr_mv, rv_fmt_rd_rs1_rs2, NULL, rv_op_addi, rv_op_addi, rv_op_addi }, 1068 + { "c.ebreak", rv_codec_ci_none, rv_fmt_none, NULL, rv_op_ebreak, rv_op_ebreak, rv_op_ebreak }, 1069 + { "c.jalr", rv_codec_cr_jalr, rv_fmt_rd_rs1_offset, NULL, rv_op_jalr, rv_op_jalr, rv_op_jalr }, 1070 + { "c.add", rv_codec_cr, rv_fmt_rd_rs1_rs2, NULL, rv_op_add, rv_op_add, rv_op_add }, 1071 + { "c.fsdsp", rv_codec_css_sdsp, rv_fmt_frs2_offset_rs1, NULL, rv_op_fsd, rv_op_fsd, rv_op_fsd }, 1072 + { "c.swsp", rv_codec_css_swsp, rv_fmt_rs2_offset_rs1, NULL, rv_op_sw, rv_op_sw, rv_op_sw }, 1073 + { "c.fswsp", rv_codec_css_swsp, rv_fmt_frs2_offset_rs1, NULL, rv_op_fsw, 0, 0 }, 1074 + { "c.ld", rv_codec_cl_ld, rv_fmt_rd_offset_rs1, NULL, 0, rv_op_ld, rv_op_ld }, 1075 + { "c.sd", rv_codec_cs_sd, rv_fmt_rs2_offset_rs1, NULL, 0, rv_op_sd, rv_op_sd }, 1076 + { "c.addiw", rv_codec_ci, rv_fmt_rd_rs1_imm, NULL, 0, rv_op_addiw, rv_op_addiw }, 1077 + { "c.ldsp", rv_codec_ci_ldsp, rv_fmt_rd_offset_rs1, NULL, 0, rv_op_ld, rv_op_ld }, 1078 + { "c.sdsp", rv_codec_css_sdsp, rv_fmt_rs2_offset_rs1, NULL, 0, rv_op_sd, rv_op_sd }, 1079 + { "c.lq", rv_codec_cl_lq, rv_fmt_rd_offset_rs1, NULL, 0, 0, rv_op_lq }, 1080 + { "c.sq", rv_codec_cs_sq, rv_fmt_rs2_offset_rs1, NULL, 0, 0, rv_op_sq }, 1081 + { "c.lqsp", rv_codec_ci_lqsp, rv_fmt_rd_offset_rs1, NULL, 0, 0, rv_op_lq }, 1082 + { "c.sqsp", rv_codec_css_sqsp, rv_fmt_rs2_offset_rs1, NULL, 0, 0, rv_op_sq }, 1083 + { "nop", rv_codec_i, rv_fmt_none, NULL, 0, 0, 0 }, 1084 + { "mv", rv_codec_i, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1085 + { "not", rv_codec_i, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1086 + { "neg", rv_codec_r, rv_fmt_rd_rs2, NULL, 0, 0, 0 }, 1087 + { "negw", rv_codec_r, rv_fmt_rd_rs2, NULL, 0, 0, 0 }, 1088 + { "sext.w", rv_codec_i, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1089 + { "seqz", rv_codec_i, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1090 + { "snez", rv_codec_r, rv_fmt_rd_rs2, NULL, 0, 0, 0 }, 1091 + { "sltz", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1092 + { "sgtz", rv_codec_r, rv_fmt_rd_rs2, NULL, 0, 0, 0 }, 1093 + { "fmv.s", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1094 + { "fabs.s", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1095 + { "fneg.s", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1096 + { "fmv.d", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1097 + { "fabs.d", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1098 + { "fneg.d", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1099 + { "fmv.q", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1100 + { "fabs.q", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1101 + { "fneg.q", rv_codec_r, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1102 + { "beqz", rv_codec_sb, rv_fmt_rs1_offset, NULL, 0, 0, 0 }, 1103 + { "bnez", rv_codec_sb, rv_fmt_rs1_offset, NULL, 0, 0, 0 }, 1104 + { "blez", rv_codec_sb, rv_fmt_rs2_offset, NULL, 0, 0, 0 }, 1105 + { "bgez", rv_codec_sb, rv_fmt_rs1_offset, NULL, 0, 0, 0 }, 1106 + { "bltz", rv_codec_sb, rv_fmt_rs1_offset, NULL, 0, 0, 0 }, 1107 + { "bgtz", rv_codec_sb, rv_fmt_rs2_offset, NULL, 0, 0, 0 }, 1108 + { "ble", rv_codec_sb, rv_fmt_rs2_rs1_offset, NULL, 0, 0, 0 }, 1109 + { "bleu", rv_codec_sb, rv_fmt_rs2_rs1_offset, NULL, 0, 0, 0 }, 1110 + { "bgt", rv_codec_sb, rv_fmt_rs2_rs1_offset, NULL, 0, 0, 0 }, 1111 + { "bgtu", rv_codec_sb, rv_fmt_rs2_rs1_offset, NULL, 0, 0, 0 }, 1112 + { "j", rv_codec_uj, rv_fmt_offset, NULL, 0, 0, 0 }, 1113 + { "ret", rv_codec_i, rv_fmt_none, NULL, 0, 0, 0 }, 1114 + { "jr", rv_codec_i, rv_fmt_rs1, NULL, 0, 0, 0 }, 1115 + { "rdcycle", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1116 + { "rdtime", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1117 + { "rdinstret", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1118 + { "rdcycleh", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1119 + { "rdtimeh", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1120 + { "rdinstreth", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1121 + { "frcsr", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1122 + { "frrm", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1123 + { "frflags", rv_codec_i_csr, rv_fmt_rd, NULL, 0, 0, 0 }, 1124 + { "fscsr", rv_codec_i_csr, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1125 + { "fsrm", rv_codec_i_csr, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1126 + { "fsflags", rv_codec_i_csr, rv_fmt_rd_rs1, NULL, 0, 0, 0 }, 1127 + { "fsrmi", rv_codec_i_csr, rv_fmt_rd_zimm, NULL, 0, 0, 0 }, 1128 + { "fsflagsi", rv_codec_i_csr, rv_fmt_rd_zimm, NULL, 0, 0, 0 }, 1129 + }; 1130 + 1131 + /* CSR names */ 1132 + 1133 + static const char *csr_name(int csrno) 1134 + { 1135 + switch (csrno) { 1136 + case 0x0000: return "ustatus"; 1137 + case 0x0001: return "fflags"; 1138 + case 0x0002: return "frm"; 1139 + case 0x0003: return "fcsr"; 1140 + case 0x0004: return "uie"; 1141 + case 0x0005: return "utvec"; 1142 + case 0x0040: return "uscratch"; 1143 + case 0x0041: return "uepc"; 1144 + case 0x0042: return "ucause"; 1145 + case 0x0043: return "utval"; 1146 + case 0x0044: return "uip"; 1147 + case 0x0100: return "sstatus"; 1148 + case 0x0102: return "sedeleg"; 1149 + case 0x0103: return "sideleg"; 1150 + case 0x0104: return "sie"; 1151 + case 0x0105: return "stvec"; 1152 + case 0x0106: return "scounteren"; 1153 + case 0x0140: return "sscratch"; 1154 + case 0x0141: return "sepc"; 1155 + case 0x0142: return "scause"; 1156 + case 0x0143: return "stval"; 1157 + case 0x0144: return "sip"; 1158 + case 0x0180: return "satp"; 1159 + case 0x0200: return "hstatus"; 1160 + case 0x0202: return "hedeleg"; 1161 + case 0x0203: return "hideleg"; 1162 + case 0x0204: return "hie"; 1163 + case 0x0205: return "htvec"; 1164 + case 0x0240: return "hscratch"; 1165 + case 0x0241: return "hepc"; 1166 + case 0x0242: return "hcause"; 1167 + case 0x0243: return "hbadaddr"; 1168 + case 0x0244: return "hip"; 1169 + case 0x0300: return "mstatus"; 1170 + case 0x0301: return "misa"; 1171 + case 0x0302: return "medeleg"; 1172 + case 0x0303: return "mideleg"; 1173 + case 0x0304: return "mie"; 1174 + case 0x0305: return "mtvec"; 1175 + case 0x0306: return "mcounteren"; 1176 + case 0x0320: return "mucounteren"; 1177 + case 0x0321: return "mscounteren"; 1178 + case 0x0322: return "mhcounteren"; 1179 + case 0x0323: return "mhpmevent3"; 1180 + case 0x0324: return "mhpmevent4"; 1181 + case 0x0325: return "mhpmevent5"; 1182 + case 0x0326: return "mhpmevent6"; 1183 + case 0x0327: return "mhpmevent7"; 1184 + case 0x0328: return "mhpmevent8"; 1185 + case 0x0329: return "mhpmevent9"; 1186 + case 0x032a: return "mhpmevent10"; 1187 + case 0x032b: return "mhpmevent11"; 1188 + case 0x032c: return "mhpmevent12"; 1189 + case 0x032d: return "mhpmevent13"; 1190 + case 0x032e: return "mhpmevent14"; 1191 + case 0x032f: return "mhpmevent15"; 1192 + case 0x0330: return "mhpmevent16"; 1193 + case 0x0331: return "mhpmevent17"; 1194 + case 0x0332: return "mhpmevent18"; 1195 + case 0x0333: return "mhpmevent19"; 1196 + case 0x0334: return "mhpmevent20"; 1197 + case 0x0335: return "mhpmevent21"; 1198 + case 0x0336: return "mhpmevent22"; 1199 + case 0x0337: return "mhpmevent23"; 1200 + case 0x0338: return "mhpmevent24"; 1201 + case 0x0339: return "mhpmevent25"; 1202 + case 0x033a: return "mhpmevent26"; 1203 + case 0x033b: return "mhpmevent27"; 1204 + case 0x033c: return "mhpmevent28"; 1205 + case 0x033d: return "mhpmevent29"; 1206 + case 0x033e: return "mhpmevent30"; 1207 + case 0x033f: return "mhpmevent31"; 1208 + case 0x0340: return "mscratch"; 1209 + case 0x0341: return "mepc"; 1210 + case 0x0342: return "mcause"; 1211 + case 0x0343: return "mtval"; 1212 + case 0x0344: return "mip"; 1213 + case 0x0380: return "mbase"; 1214 + case 0x0381: return "mbound"; 1215 + case 0x0382: return "mibase"; 1216 + case 0x0383: return "mibound"; 1217 + case 0x0384: return "mdbase"; 1218 + case 0x0385: return "mdbound"; 1219 + case 0x03a0: return "pmpcfg3"; 1220 + case 0x03b0: return "pmpaddr0"; 1221 + case 0x03b1: return "pmpaddr1"; 1222 + case 0x03b2: return "pmpaddr2"; 1223 + case 0x03b3: return "pmpaddr3"; 1224 + case 0x03b4: return "pmpaddr4"; 1225 + case 0x03b5: return "pmpaddr5"; 1226 + case 0x03b6: return "pmpaddr6"; 1227 + case 0x03b7: return "pmpaddr7"; 1228 + case 0x03b8: return "pmpaddr8"; 1229 + case 0x03b9: return "pmpaddr9"; 1230 + case 0x03ba: return "pmpaddr10"; 1231 + case 0x03bb: return "pmpaddr11"; 1232 + case 0x03bc: return "pmpaddr12"; 1233 + case 0x03bd: return "pmpaddr14"; 1234 + case 0x03be: return "pmpaddr13"; 1235 + case 0x03bf: return "pmpaddr15"; 1236 + case 0x0780: return "mtohost"; 1237 + case 0x0781: return "mfromhost"; 1238 + case 0x0782: return "mreset"; 1239 + case 0x0783: return "mipi"; 1240 + case 0x0784: return "miobase"; 1241 + case 0x07a0: return "tselect"; 1242 + case 0x07a1: return "tdata1"; 1243 + case 0x07a2: return "tdata2"; 1244 + case 0x07a3: return "tdata3"; 1245 + case 0x07b0: return "dcsr"; 1246 + case 0x07b1: return "dpc"; 1247 + case 0x07b2: return "dscratch"; 1248 + case 0x0b00: return "mcycle"; 1249 + case 0x0b01: return "mtime"; 1250 + case 0x0b02: return "minstret"; 1251 + case 0x0b03: return "mhpmcounter3"; 1252 + case 0x0b04: return "mhpmcounter4"; 1253 + case 0x0b05: return "mhpmcounter5"; 1254 + case 0x0b06: return "mhpmcounter6"; 1255 + case 0x0b07: return "mhpmcounter7"; 1256 + case 0x0b08: return "mhpmcounter8"; 1257 + case 0x0b09: return "mhpmcounter9"; 1258 + case 0x0b0a: return "mhpmcounter10"; 1259 + case 0x0b0b: return "mhpmcounter11"; 1260 + case 0x0b0c: return "mhpmcounter12"; 1261 + case 0x0b0d: return "mhpmcounter13"; 1262 + case 0x0b0e: return "mhpmcounter14"; 1263 + case 0x0b0f: return "mhpmcounter15"; 1264 + case 0x0b10: return "mhpmcounter16"; 1265 + case 0x0b11: return "mhpmcounter17"; 1266 + case 0x0b12: return "mhpmcounter18"; 1267 + case 0x0b13: return "mhpmcounter19"; 1268 + case 0x0b14: return "mhpmcounter20"; 1269 + case 0x0b15: return "mhpmcounter21"; 1270 + case 0x0b16: return "mhpmcounter22"; 1271 + case 0x0b17: return "mhpmcounter23"; 1272 + case 0x0b18: return "mhpmcounter24"; 1273 + case 0x0b19: return "mhpmcounter25"; 1274 + case 0x0b1a: return "mhpmcounter26"; 1275 + case 0x0b1b: return "mhpmcounter27"; 1276 + case 0x0b1c: return "mhpmcounter28"; 1277 + case 0x0b1d: return "mhpmcounter29"; 1278 + case 0x0b1e: return "mhpmcounter30"; 1279 + case 0x0b1f: return "mhpmcounter31"; 1280 + case 0x0b80: return "mcycleh"; 1281 + case 0x0b81: return "mtimeh"; 1282 + case 0x0b82: return "minstreth"; 1283 + case 0x0b83: return "mhpmcounter3h"; 1284 + case 0x0b84: return "mhpmcounter4h"; 1285 + case 0x0b85: return "mhpmcounter5h"; 1286 + case 0x0b86: return "mhpmcounter6h"; 1287 + case 0x0b87: return "mhpmcounter7h"; 1288 + case 0x0b88: return "mhpmcounter8h"; 1289 + case 0x0b89: return "mhpmcounter9h"; 1290 + case 0x0b8a: return "mhpmcounter10h"; 1291 + case 0x0b8b: return "mhpmcounter11h"; 1292 + case 0x0b8c: return "mhpmcounter12h"; 1293 + case 0x0b8d: return "mhpmcounter13h"; 1294 + case 0x0b8e: return "mhpmcounter14h"; 1295 + case 0x0b8f: return "mhpmcounter15h"; 1296 + case 0x0b90: return "mhpmcounter16h"; 1297 + case 0x0b91: return "mhpmcounter17h"; 1298 + case 0x0b92: return "mhpmcounter18h"; 1299 + case 0x0b93: return "mhpmcounter19h"; 1300 + case 0x0b94: return "mhpmcounter20h"; 1301 + case 0x0b95: return "mhpmcounter21h"; 1302 + case 0x0b96: return "mhpmcounter22h"; 1303 + case 0x0b97: return "mhpmcounter23h"; 1304 + case 0x0b98: return "mhpmcounter24h"; 1305 + case 0x0b99: return "mhpmcounter25h"; 1306 + case 0x0b9a: return "mhpmcounter26h"; 1307 + case 0x0b9b: return "mhpmcounter27h"; 1308 + case 0x0b9c: return "mhpmcounter28h"; 1309 + case 0x0b9d: return "mhpmcounter29h"; 1310 + case 0x0b9e: return "mhpmcounter30h"; 1311 + case 0x0b9f: return "mhpmcounter31h"; 1312 + case 0x0c00: return "cycle"; 1313 + case 0x0c01: return "time"; 1314 + case 0x0c02: return "instret"; 1315 + case 0x0c80: return "cycleh"; 1316 + case 0x0c81: return "timeh"; 1317 + case 0x0c82: return "instreth"; 1318 + case 0x0d00: return "scycle"; 1319 + case 0x0d01: return "stime"; 1320 + case 0x0d02: return "sinstret"; 1321 + case 0x0d80: return "scycleh"; 1322 + case 0x0d81: return "stimeh"; 1323 + case 0x0d82: return "sinstreth"; 1324 + case 0x0e00: return "hcycle"; 1325 + case 0x0e01: return "htime"; 1326 + case 0x0e02: return "hinstret"; 1327 + case 0x0e80: return "hcycleh"; 1328 + case 0x0e81: return "htimeh"; 1329 + case 0x0e82: return "hinstreth"; 1330 + case 0x0f11: return "mvendorid"; 1331 + case 0x0f12: return "marchid"; 1332 + case 0x0f13: return "mimpid"; 1333 + case 0x0f14: return "mhartid"; 1334 + default: return NULL; 1335 + } 1336 + } 1337 + 1338 + /* decode opcode */ 1339 + 1340 + static void decode_inst_opcode(rv_decode *dec, rv_isa isa) 1341 + { 1342 + rv_inst inst = dec->inst; 1343 + rv_opcode op = rv_op_illegal; 1344 + switch (((inst >> 0) & 0b11)) { 1345 + case 0: 1346 + switch (((inst >> 13) & 0b111)) { 1347 + case 0: op = rv_op_c_addi4spn; break; 1348 + case 1: 1349 + if (isa == rv128) { 1350 + op = rv_op_c_lq; 1351 + } else { 1352 + op = rv_op_c_fld; 1353 + } 1354 + break; 1355 + case 2: op = rv_op_c_lw; break; 1356 + case 3: 1357 + if (isa == rv32) { 1358 + op = rv_op_c_flw; 1359 + } else { 1360 + op = rv_op_c_ld; 1361 + } 1362 + break; 1363 + case 5: 1364 + if (isa == rv128) { 1365 + op = rv_op_c_sq; 1366 + } else { 1367 + op = rv_op_c_fsd; 1368 + } 1369 + break; 1370 + case 6: op = rv_op_c_sw; break; 1371 + case 7: 1372 + if (isa == rv32) { 1373 + op = rv_op_c_fsw; 1374 + } else { 1375 + op = rv_op_c_sd; 1376 + } 1377 + break; 1378 + } 1379 + break; 1380 + case 1: 1381 + switch (((inst >> 13) & 0b111)) { 1382 + case 0: 1383 + switch (((inst >> 2) & 0b11111111111)) { 1384 + case 0: op = rv_op_c_nop; break; 1385 + default: op = rv_op_c_addi; break; 1386 + } 1387 + break; 1388 + case 1: 1389 + if (isa == rv32) { 1390 + op = rv_op_c_jal; 1391 + } else { 1392 + op = rv_op_c_addiw; 1393 + } 1394 + break; 1395 + case 2: op = rv_op_c_li; break; 1396 + case 3: 1397 + switch (((inst >> 7) & 0b11111)) { 1398 + case 2: op = rv_op_c_addi16sp; break; 1399 + default: op = rv_op_c_lui; break; 1400 + } 1401 + break; 1402 + case 4: 1403 + switch (((inst >> 10) & 0b11)) { 1404 + case 0: 1405 + op = rv_op_c_srli; 1406 + break; 1407 + case 1: 1408 + op = rv_op_c_srai; 1409 + break; 1410 + case 2: op = rv_op_c_andi; break; 1411 + case 3: 1412 + switch (((inst >> 10) & 0b100) | ((inst >> 5) & 0b011)) { 1413 + case 0: op = rv_op_c_sub; break; 1414 + case 1: op = rv_op_c_xor; break; 1415 + case 2: op = rv_op_c_or; break; 1416 + case 3: op = rv_op_c_and; break; 1417 + case 4: op = rv_op_c_subw; break; 1418 + case 5: op = rv_op_c_addw; break; 1419 + } 1420 + break; 1421 + } 1422 + break; 1423 + case 5: op = rv_op_c_j; break; 1424 + case 6: op = rv_op_c_beqz; break; 1425 + case 7: op = rv_op_c_bnez; break; 1426 + } 1427 + break; 1428 + case 2: 1429 + switch (((inst >> 13) & 0b111)) { 1430 + case 0: 1431 + op = rv_op_c_slli; 1432 + break; 1433 + case 1: 1434 + if (isa == rv128) { 1435 + op = rv_op_c_lqsp; 1436 + } else { 1437 + op = rv_op_c_fldsp; 1438 + } 1439 + break; 1440 + case 2: op = rv_op_c_lwsp; break; 1441 + case 3: 1442 + if (isa == rv32) { 1443 + op = rv_op_c_flwsp; 1444 + } else { 1445 + op = rv_op_c_ldsp; 1446 + } 1447 + break; 1448 + case 4: 1449 + switch (((inst >> 12) & 0b1)) { 1450 + case 0: 1451 + switch (((inst >> 2) & 0b11111)) { 1452 + case 0: op = rv_op_c_jr; break; 1453 + default: op = rv_op_c_mv; break; 1454 + } 1455 + break; 1456 + case 1: 1457 + switch (((inst >> 2) & 0b11111)) { 1458 + case 0: 1459 + switch (((inst >> 7) & 0b11111)) { 1460 + case 0: op = rv_op_c_ebreak; break; 1461 + default: op = rv_op_c_jalr; break; 1462 + } 1463 + break; 1464 + default: op = rv_op_c_add; break; 1465 + } 1466 + break; 1467 + } 1468 + break; 1469 + case 5: 1470 + if (isa == rv128) { 1471 + op = rv_op_c_sqsp; 1472 + } else { 1473 + op = rv_op_c_fsdsp; break; 1474 + } 1475 + case 6: op = rv_op_c_swsp; break; 1476 + case 7: 1477 + if (isa == rv32) { 1478 + op = rv_op_c_fswsp; 1479 + } else { 1480 + op = rv_op_c_sdsp; 1481 + } 1482 + break; 1483 + } 1484 + break; 1485 + case 3: 1486 + switch (((inst >> 2) & 0b11111)) { 1487 + case 0: 1488 + switch (((inst >> 12) & 0b111)) { 1489 + case 0: op = rv_op_lb; break; 1490 + case 1: op = rv_op_lh; break; 1491 + case 2: op = rv_op_lw; break; 1492 + case 3: op = rv_op_ld; break; 1493 + case 4: op = rv_op_lbu; break; 1494 + case 5: op = rv_op_lhu; break; 1495 + case 6: op = rv_op_lwu; break; 1496 + case 7: op = rv_op_ldu; break; 1497 + } 1498 + break; 1499 + case 1: 1500 + switch (((inst >> 12) & 0b111)) { 1501 + case 2: op = rv_op_flw; break; 1502 + case 3: op = rv_op_fld; break; 1503 + case 4: op = rv_op_flq; break; 1504 + } 1505 + break; 1506 + case 3: 1507 + switch (((inst >> 12) & 0b111)) { 1508 + case 0: op = rv_op_fence; break; 1509 + case 1: op = rv_op_fence_i; break; 1510 + case 2: op = rv_op_lq; break; 1511 + } 1512 + break; 1513 + case 4: 1514 + switch (((inst >> 12) & 0b111)) { 1515 + case 0: op = rv_op_addi; break; 1516 + case 1: 1517 + switch (((inst >> 27) & 0b11111)) { 1518 + case 0: op = rv_op_slli; break; 1519 + } 1520 + break; 1521 + case 2: op = rv_op_slti; break; 1522 + case 3: op = rv_op_sltiu; break; 1523 + case 4: op = rv_op_xori; break; 1524 + case 5: 1525 + switch (((inst >> 27) & 0b11111)) { 1526 + case 0: op = rv_op_srli; break; 1527 + case 8: op = rv_op_srai; break; 1528 + } 1529 + break; 1530 + case 6: op = rv_op_ori; break; 1531 + case 7: op = rv_op_andi; break; 1532 + } 1533 + break; 1534 + case 5: op = rv_op_auipc; break; 1535 + case 6: 1536 + switch (((inst >> 12) & 0b111)) { 1537 + case 0: op = rv_op_addiw; break; 1538 + case 1: 1539 + switch (((inst >> 25) & 0b1111111)) { 1540 + case 0: op = rv_op_slliw; break; 1541 + } 1542 + break; 1543 + case 5: 1544 + switch (((inst >> 25) & 0b1111111)) { 1545 + case 0: op = rv_op_srliw; break; 1546 + case 32: op = rv_op_sraiw; break; 1547 + } 1548 + break; 1549 + } 1550 + break; 1551 + case 8: 1552 + switch (((inst >> 12) & 0b111)) { 1553 + case 0: op = rv_op_sb; break; 1554 + case 1: op = rv_op_sh; break; 1555 + case 2: op = rv_op_sw; break; 1556 + case 3: op = rv_op_sd; break; 1557 + case 4: op = rv_op_sq; break; 1558 + } 1559 + break; 1560 + case 9: 1561 + switch (((inst >> 12) & 0b111)) { 1562 + case 2: op = rv_op_fsw; break; 1563 + case 3: op = rv_op_fsd; break; 1564 + case 4: op = rv_op_fsq; break; 1565 + } 1566 + break; 1567 + case 11: 1568 + switch (((inst >> 24) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1569 + case 2: op = rv_op_amoadd_w; break; 1570 + case 3: op = rv_op_amoadd_d; break; 1571 + case 4: op = rv_op_amoadd_q; break; 1572 + case 10: op = rv_op_amoswap_w; break; 1573 + case 11: op = rv_op_amoswap_d; break; 1574 + case 12: op = rv_op_amoswap_q; break; 1575 + case 18: 1576 + switch (((inst >> 20) & 0b11111)) { 1577 + case 0: op = rv_op_lr_w; break; 1578 + } 1579 + break; 1580 + case 19: 1581 + switch (((inst >> 20) & 0b11111)) { 1582 + case 0: op = rv_op_lr_d; break; 1583 + } 1584 + break; 1585 + case 20: 1586 + switch (((inst >> 20) & 0b11111)) { 1587 + case 0: op = rv_op_lr_q; break; 1588 + } 1589 + break; 1590 + case 26: op = rv_op_sc_w; break; 1591 + case 27: op = rv_op_sc_d; break; 1592 + case 28: op = rv_op_sc_q; break; 1593 + case 34: op = rv_op_amoxor_w; break; 1594 + case 35: op = rv_op_amoxor_d; break; 1595 + case 36: op = rv_op_amoxor_q; break; 1596 + case 66: op = rv_op_amoor_w; break; 1597 + case 67: op = rv_op_amoor_d; break; 1598 + case 68: op = rv_op_amoor_q; break; 1599 + case 98: op = rv_op_amoand_w; break; 1600 + case 99: op = rv_op_amoand_d; break; 1601 + case 100: op = rv_op_amoand_q; break; 1602 + case 130: op = rv_op_amomin_w; break; 1603 + case 131: op = rv_op_amomin_d; break; 1604 + case 132: op = rv_op_amomin_q; break; 1605 + case 162: op = rv_op_amomax_w; break; 1606 + case 163: op = rv_op_amomax_d; break; 1607 + case 164: op = rv_op_amomax_q; break; 1608 + case 194: op = rv_op_amominu_w; break; 1609 + case 195: op = rv_op_amominu_d; break; 1610 + case 196: op = rv_op_amominu_q; break; 1611 + case 226: op = rv_op_amomaxu_w; break; 1612 + case 227: op = rv_op_amomaxu_d; break; 1613 + case 228: op = rv_op_amomaxu_q; break; 1614 + } 1615 + break; 1616 + case 12: 1617 + switch (((inst >> 22) & 0b1111111000) | ((inst >> 12) & 0b0000000111)) { 1618 + case 0: op = rv_op_add; break; 1619 + case 1: op = rv_op_sll; break; 1620 + case 2: op = rv_op_slt; break; 1621 + case 3: op = rv_op_sltu; break; 1622 + case 4: op = rv_op_xor; break; 1623 + case 5: op = rv_op_srl; break; 1624 + case 6: op = rv_op_or; break; 1625 + case 7: op = rv_op_and; break; 1626 + case 8: op = rv_op_mul; break; 1627 + case 9: op = rv_op_mulh; break; 1628 + case 10: op = rv_op_mulhsu; break; 1629 + case 11: op = rv_op_mulhu; break; 1630 + case 12: op = rv_op_div; break; 1631 + case 13: op = rv_op_divu; break; 1632 + case 14: op = rv_op_rem; break; 1633 + case 15: op = rv_op_remu; break; 1634 + case 256: op = rv_op_sub; break; 1635 + case 261: op = rv_op_sra; break; 1636 + } 1637 + break; 1638 + case 13: op = rv_op_lui; break; 1639 + case 14: 1640 + switch (((inst >> 22) & 0b1111111000) | ((inst >> 12) & 0b0000000111)) { 1641 + case 0: op = rv_op_addw; break; 1642 + case 1: op = rv_op_sllw; break; 1643 + case 5: op = rv_op_srlw; break; 1644 + case 8: op = rv_op_mulw; break; 1645 + case 12: op = rv_op_divw; break; 1646 + case 13: op = rv_op_divuw; break; 1647 + case 14: op = rv_op_remw; break; 1648 + case 15: op = rv_op_remuw; break; 1649 + case 256: op = rv_op_subw; break; 1650 + case 261: op = rv_op_sraw; break; 1651 + } 1652 + break; 1653 + case 16: 1654 + switch (((inst >> 25) & 0b11)) { 1655 + case 0: op = rv_op_fmadd_s; break; 1656 + case 1: op = rv_op_fmadd_d; break; 1657 + case 3: op = rv_op_fmadd_q; break; 1658 + } 1659 + break; 1660 + case 17: 1661 + switch (((inst >> 25) & 0b11)) { 1662 + case 0: op = rv_op_fmsub_s; break; 1663 + case 1: op = rv_op_fmsub_d; break; 1664 + case 3: op = rv_op_fmsub_q; break; 1665 + } 1666 + break; 1667 + case 18: 1668 + switch (((inst >> 25) & 0b11)) { 1669 + case 0: op = rv_op_fnmsub_s; break; 1670 + case 1: op = rv_op_fnmsub_d; break; 1671 + case 3: op = rv_op_fnmsub_q; break; 1672 + } 1673 + break; 1674 + case 19: 1675 + switch (((inst >> 25) & 0b11)) { 1676 + case 0: op = rv_op_fnmadd_s; break; 1677 + case 1: op = rv_op_fnmadd_d; break; 1678 + case 3: op = rv_op_fnmadd_q; break; 1679 + } 1680 + break; 1681 + case 20: 1682 + switch (((inst >> 25) & 0b1111111)) { 1683 + case 0: op = rv_op_fadd_s; break; 1684 + case 1: op = rv_op_fadd_d; break; 1685 + case 3: op = rv_op_fadd_q; break; 1686 + case 4: op = rv_op_fsub_s; break; 1687 + case 5: op = rv_op_fsub_d; break; 1688 + case 7: op = rv_op_fsub_q; break; 1689 + case 8: op = rv_op_fmul_s; break; 1690 + case 9: op = rv_op_fmul_d; break; 1691 + case 11: op = rv_op_fmul_q; break; 1692 + case 12: op = rv_op_fdiv_s; break; 1693 + case 13: op = rv_op_fdiv_d; break; 1694 + case 15: op = rv_op_fdiv_q; break; 1695 + case 16: 1696 + switch (((inst >> 12) & 0b111)) { 1697 + case 0: op = rv_op_fsgnj_s; break; 1698 + case 1: op = rv_op_fsgnjn_s; break; 1699 + case 2: op = rv_op_fsgnjx_s; break; 1700 + } 1701 + break; 1702 + case 17: 1703 + switch (((inst >> 12) & 0b111)) { 1704 + case 0: op = rv_op_fsgnj_d; break; 1705 + case 1: op = rv_op_fsgnjn_d; break; 1706 + case 2: op = rv_op_fsgnjx_d; break; 1707 + } 1708 + break; 1709 + case 19: 1710 + switch (((inst >> 12) & 0b111)) { 1711 + case 0: op = rv_op_fsgnj_q; break; 1712 + case 1: op = rv_op_fsgnjn_q; break; 1713 + case 2: op = rv_op_fsgnjx_q; break; 1714 + } 1715 + break; 1716 + case 20: 1717 + switch (((inst >> 12) & 0b111)) { 1718 + case 0: op = rv_op_fmin_s; break; 1719 + case 1: op = rv_op_fmax_s; break; 1720 + } 1721 + break; 1722 + case 21: 1723 + switch (((inst >> 12) & 0b111)) { 1724 + case 0: op = rv_op_fmin_d; break; 1725 + case 1: op = rv_op_fmax_d; break; 1726 + } 1727 + break; 1728 + case 23: 1729 + switch (((inst >> 12) & 0b111)) { 1730 + case 0: op = rv_op_fmin_q; break; 1731 + case 1: op = rv_op_fmax_q; break; 1732 + } 1733 + break; 1734 + case 32: 1735 + switch (((inst >> 20) & 0b11111)) { 1736 + case 1: op = rv_op_fcvt_s_d; break; 1737 + case 3: op = rv_op_fcvt_s_q; break; 1738 + } 1739 + break; 1740 + case 33: 1741 + switch (((inst >> 20) & 0b11111)) { 1742 + case 0: op = rv_op_fcvt_d_s; break; 1743 + case 3: op = rv_op_fcvt_d_q; break; 1744 + } 1745 + break; 1746 + case 35: 1747 + switch (((inst >> 20) & 0b11111)) { 1748 + case 0: op = rv_op_fcvt_q_s; break; 1749 + case 1: op = rv_op_fcvt_q_d; break; 1750 + } 1751 + break; 1752 + case 44: 1753 + switch (((inst >> 20) & 0b11111)) { 1754 + case 0: op = rv_op_fsqrt_s; break; 1755 + } 1756 + break; 1757 + case 45: 1758 + switch (((inst >> 20) & 0b11111)) { 1759 + case 0: op = rv_op_fsqrt_d; break; 1760 + } 1761 + break; 1762 + case 47: 1763 + switch (((inst >> 20) & 0b11111)) { 1764 + case 0: op = rv_op_fsqrt_q; break; 1765 + } 1766 + break; 1767 + case 80: 1768 + switch (((inst >> 12) & 0b111)) { 1769 + case 0: op = rv_op_fle_s; break; 1770 + case 1: op = rv_op_flt_s; break; 1771 + case 2: op = rv_op_feq_s; break; 1772 + } 1773 + break; 1774 + case 81: 1775 + switch (((inst >> 12) & 0b111)) { 1776 + case 0: op = rv_op_fle_d; break; 1777 + case 1: op = rv_op_flt_d; break; 1778 + case 2: op = rv_op_feq_d; break; 1779 + } 1780 + break; 1781 + case 83: 1782 + switch (((inst >> 12) & 0b111)) { 1783 + case 0: op = rv_op_fle_q; break; 1784 + case 1: op = rv_op_flt_q; break; 1785 + case 2: op = rv_op_feq_q; break; 1786 + } 1787 + break; 1788 + case 96: 1789 + switch (((inst >> 20) & 0b11111)) { 1790 + case 0: op = rv_op_fcvt_w_s; break; 1791 + case 1: op = rv_op_fcvt_wu_s; break; 1792 + case 2: op = rv_op_fcvt_l_s; break; 1793 + case 3: op = rv_op_fcvt_lu_s; break; 1794 + } 1795 + break; 1796 + case 97: 1797 + switch (((inst >> 20) & 0b11111)) { 1798 + case 0: op = rv_op_fcvt_w_d; break; 1799 + case 1: op = rv_op_fcvt_wu_d; break; 1800 + case 2: op = rv_op_fcvt_l_d; break; 1801 + case 3: op = rv_op_fcvt_lu_d; break; 1802 + } 1803 + break; 1804 + case 99: 1805 + switch (((inst >> 20) & 0b11111)) { 1806 + case 0: op = rv_op_fcvt_w_q; break; 1807 + case 1: op = rv_op_fcvt_wu_q; break; 1808 + case 2: op = rv_op_fcvt_l_q; break; 1809 + case 3: op = rv_op_fcvt_lu_q; break; 1810 + } 1811 + break; 1812 + case 104: 1813 + switch (((inst >> 20) & 0b11111)) { 1814 + case 0: op = rv_op_fcvt_s_w; break; 1815 + case 1: op = rv_op_fcvt_s_wu; break; 1816 + case 2: op = rv_op_fcvt_s_l; break; 1817 + case 3: op = rv_op_fcvt_s_lu; break; 1818 + } 1819 + break; 1820 + case 105: 1821 + switch (((inst >> 20) & 0b11111)) { 1822 + case 0: op = rv_op_fcvt_d_w; break; 1823 + case 1: op = rv_op_fcvt_d_wu; break; 1824 + case 2: op = rv_op_fcvt_d_l; break; 1825 + case 3: op = rv_op_fcvt_d_lu; break; 1826 + } 1827 + break; 1828 + case 107: 1829 + switch (((inst >> 20) & 0b11111)) { 1830 + case 0: op = rv_op_fcvt_q_w; break; 1831 + case 1: op = rv_op_fcvt_q_wu; break; 1832 + case 2: op = rv_op_fcvt_q_l; break; 1833 + case 3: op = rv_op_fcvt_q_lu; break; 1834 + } 1835 + break; 1836 + case 112: 1837 + switch (((inst >> 17) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1838 + case 0: op = rv_op_fmv_x_s; break; 1839 + case 1: op = rv_op_fclass_s; break; 1840 + } 1841 + break; 1842 + case 113: 1843 + switch (((inst >> 17) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1844 + case 0: op = rv_op_fmv_x_d; break; 1845 + case 1: op = rv_op_fclass_d; break; 1846 + } 1847 + break; 1848 + case 115: 1849 + switch (((inst >> 17) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1850 + case 0: op = rv_op_fmv_x_q; break; 1851 + case 1: op = rv_op_fclass_q; break; 1852 + } 1853 + break; 1854 + case 120: 1855 + switch (((inst >> 17) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1856 + case 0: op = rv_op_fmv_s_x; break; 1857 + } 1858 + break; 1859 + case 121: 1860 + switch (((inst >> 17) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1861 + case 0: op = rv_op_fmv_d_x; break; 1862 + } 1863 + break; 1864 + case 123: 1865 + switch (((inst >> 17) & 0b11111000) | ((inst >> 12) & 0b00000111)) { 1866 + case 0: op = rv_op_fmv_q_x; break; 1867 + } 1868 + break; 1869 + } 1870 + break; 1871 + case 22: 1872 + switch (((inst >> 12) & 0b111)) { 1873 + case 0: op = rv_op_addid; break; 1874 + case 1: 1875 + switch (((inst >> 26) & 0b111111)) { 1876 + case 0: op = rv_op_sllid; break; 1877 + } 1878 + break; 1879 + case 5: 1880 + switch (((inst >> 26) & 0b111111)) { 1881 + case 0: op = rv_op_srlid; break; 1882 + case 16: op = rv_op_sraid; break; 1883 + } 1884 + break; 1885 + } 1886 + break; 1887 + case 24: 1888 + switch (((inst >> 12) & 0b111)) { 1889 + case 0: op = rv_op_beq; break; 1890 + case 1: op = rv_op_bne; break; 1891 + case 4: op = rv_op_blt; break; 1892 + case 5: op = rv_op_bge; break; 1893 + case 6: op = rv_op_bltu; break; 1894 + case 7: op = rv_op_bgeu; break; 1895 + } 1896 + break; 1897 + case 25: 1898 + switch (((inst >> 12) & 0b111)) { 1899 + case 0: op = rv_op_jalr; break; 1900 + } 1901 + break; 1902 + case 27: op = rv_op_jal; break; 1903 + case 28: 1904 + switch (((inst >> 12) & 0b111)) { 1905 + case 0: 1906 + switch (((inst >> 20) & 0b111111100000) | ((inst >> 7) & 0b000000011111)) { 1907 + case 0: 1908 + switch (((inst >> 15) & 0b1111111111)) { 1909 + case 0: op = rv_op_ecall; break; 1910 + case 32: op = rv_op_ebreak; break; 1911 + case 64: op = rv_op_uret; break; 1912 + } 1913 + break; 1914 + case 256: 1915 + switch (((inst >> 20) & 0b11111)) { 1916 + case 2: 1917 + switch (((inst >> 15) & 0b11111)) { 1918 + case 0: op = rv_op_sret; break; 1919 + } 1920 + break; 1921 + case 4: op = rv_op_sfence_vm; break; 1922 + case 5: 1923 + switch (((inst >> 15) & 0b11111)) { 1924 + case 0: op = rv_op_wfi; break; 1925 + } 1926 + break; 1927 + } 1928 + break; 1929 + case 288: op = rv_op_sfence_vma; break; 1930 + case 512: 1931 + switch (((inst >> 15) & 0b1111111111)) { 1932 + case 64: op = rv_op_hret; break; 1933 + } 1934 + break; 1935 + case 768: 1936 + switch (((inst >> 15) & 0b1111111111)) { 1937 + case 64: op = rv_op_mret; break; 1938 + } 1939 + break; 1940 + case 1952: 1941 + switch (((inst >> 15) & 0b1111111111)) { 1942 + case 576: op = rv_op_dret; break; 1943 + } 1944 + break; 1945 + } 1946 + break; 1947 + case 1: op = rv_op_csrrw; break; 1948 + case 2: op = rv_op_csrrs; break; 1949 + case 3: op = rv_op_csrrc; break; 1950 + case 5: op = rv_op_csrrwi; break; 1951 + case 6: op = rv_op_csrrsi; break; 1952 + case 7: op = rv_op_csrrci; break; 1953 + } 1954 + break; 1955 + case 30: 1956 + switch (((inst >> 22) & 0b1111111000) | ((inst >> 12) & 0b0000000111)) { 1957 + case 0: op = rv_op_addd; break; 1958 + case 1: op = rv_op_slld; break; 1959 + case 5: op = rv_op_srld; break; 1960 + case 8: op = rv_op_muld; break; 1961 + case 12: op = rv_op_divd; break; 1962 + case 13: op = rv_op_divud; break; 1963 + case 14: op = rv_op_remd; break; 1964 + case 15: op = rv_op_remud; break; 1965 + case 256: op = rv_op_subd; break; 1966 + case 261: op = rv_op_srad; break; 1967 + } 1968 + break; 1969 + } 1970 + break; 1971 + } 1972 + dec->op = op; 1973 + } 1974 + 1975 + /* operand extractors */ 1976 + 1977 + static uint32_t operand_rd(rv_inst inst) 1978 + { 1979 + return (inst << 52) >> 59; 1980 + } 1981 + 1982 + static uint32_t operand_rs1(rv_inst inst) 1983 + { 1984 + return (inst << 44) >> 59; 1985 + } 1986 + 1987 + static uint32_t operand_rs2(rv_inst inst) 1988 + { 1989 + return (inst << 39) >> 59; 1990 + } 1991 + 1992 + static uint32_t operand_rs3(rv_inst inst) 1993 + { 1994 + return (inst << 32) >> 59; 1995 + } 1996 + 1997 + static uint32_t operand_aq(rv_inst inst) 1998 + { 1999 + return (inst << 37) >> 63; 2000 + } 2001 + 2002 + static uint32_t operand_rl(rv_inst inst) 2003 + { 2004 + return (inst << 38) >> 63; 2005 + } 2006 + 2007 + static uint32_t operand_pred(rv_inst inst) 2008 + { 2009 + return (inst << 36) >> 60; 2010 + } 2011 + 2012 + static uint32_t operand_succ(rv_inst inst) 2013 + { 2014 + return (inst << 40) >> 60; 2015 + } 2016 + 2017 + static uint32_t operand_rm(rv_inst inst) 2018 + { 2019 + return (inst << 49) >> 61; 2020 + } 2021 + 2022 + static uint32_t operand_shamt5(rv_inst inst) 2023 + { 2024 + return (inst << 39) >> 59; 2025 + } 2026 + 2027 + static uint32_t operand_shamt6(rv_inst inst) 2028 + { 2029 + return (inst << 38) >> 58; 2030 + } 2031 + 2032 + static uint32_t operand_shamt7(rv_inst inst) 2033 + { 2034 + return (inst << 37) >> 57; 2035 + } 2036 + 2037 + static uint32_t operand_crdq(rv_inst inst) 2038 + { 2039 + return (inst << 59) >> 61; 2040 + } 2041 + 2042 + static uint32_t operand_crs1q(rv_inst inst) 2043 + { 2044 + return (inst << 54) >> 61; 2045 + } 2046 + 2047 + static uint32_t operand_crs1rdq(rv_inst inst) 2048 + { 2049 + return (inst << 54) >> 61; 2050 + } 2051 + 2052 + static uint32_t operand_crs2q(rv_inst inst) 2053 + { 2054 + return (inst << 59) >> 61; 2055 + } 2056 + 2057 + static uint32_t operand_crd(rv_inst inst) 2058 + { 2059 + return (inst << 52) >> 59; 2060 + } 2061 + 2062 + static uint32_t operand_crs1(rv_inst inst) 2063 + { 2064 + return (inst << 52) >> 59; 2065 + } 2066 + 2067 + static uint32_t operand_crs1rd(rv_inst inst) 2068 + { 2069 + return (inst << 52) >> 59; 2070 + } 2071 + 2072 + static uint32_t operand_crs2(rv_inst inst) 2073 + { 2074 + return (inst << 57) >> 59; 2075 + } 2076 + 2077 + static uint32_t operand_cimmsh5(rv_inst inst) 2078 + { 2079 + return (inst << 57) >> 59; 2080 + } 2081 + 2082 + static uint32_t operand_csr12(rv_inst inst) 2083 + { 2084 + return (inst << 32) >> 52; 2085 + } 2086 + 2087 + static int32_t operand_imm12(rv_inst inst) 2088 + { 2089 + return ((int64_t)inst << 32) >> 52; 2090 + } 2091 + 2092 + static int32_t operand_imm20(rv_inst inst) 2093 + { 2094 + return (((int64_t)inst << 32) >> 44) << 12; 2095 + } 2096 + 2097 + static int32_t operand_jimm20(rv_inst inst) 2098 + { 2099 + return (((int64_t)inst << 32) >> 63) << 20 | 2100 + ((inst << 33) >> 54) << 1 | 2101 + ((inst << 43) >> 63) << 11 | 2102 + ((inst << 44) >> 56) << 12; 2103 + } 2104 + 2105 + static int32_t operand_simm12(rv_inst inst) 2106 + { 2107 + return (((int64_t)inst << 32) >> 57) << 5 | 2108 + (inst << 52) >> 59; 2109 + } 2110 + 2111 + static int32_t operand_sbimm12(rv_inst inst) 2112 + { 2113 + return (((int64_t)inst << 32) >> 63) << 12 | 2114 + ((inst << 33) >> 58) << 5 | 2115 + ((inst << 52) >> 60) << 1 | 2116 + ((inst << 56) >> 63) << 11; 2117 + } 2118 + 2119 + static uint32_t operand_cimmsh6(rv_inst inst) 2120 + { 2121 + return ((inst << 51) >> 63) << 5 | 2122 + (inst << 57) >> 59; 2123 + } 2124 + 2125 + static int32_t operand_cimmi(rv_inst inst) 2126 + { 2127 + return (((int64_t)inst << 51) >> 63) << 5 | 2128 + (inst << 57) >> 59; 2129 + } 2130 + 2131 + static int32_t operand_cimmui(rv_inst inst) 2132 + { 2133 + return (((int64_t)inst << 51) >> 63) << 17 | 2134 + ((inst << 57) >> 59) << 12; 2135 + } 2136 + 2137 + static uint32_t operand_cimmlwsp(rv_inst inst) 2138 + { 2139 + return ((inst << 51) >> 63) << 5 | 2140 + ((inst << 57) >> 61) << 2 | 2141 + ((inst << 60) >> 62) << 6; 2142 + } 2143 + 2144 + static uint32_t operand_cimmldsp(rv_inst inst) 2145 + { 2146 + return ((inst << 51) >> 63) << 5 | 2147 + ((inst << 57) >> 62) << 3 | 2148 + ((inst << 59) >> 61) << 6; 2149 + } 2150 + 2151 + static uint32_t operand_cimmlqsp(rv_inst inst) 2152 + { 2153 + return ((inst << 51) >> 63) << 5 | 2154 + ((inst << 57) >> 63) << 4 | 2155 + ((inst << 58) >> 60) << 6; 2156 + } 2157 + 2158 + static int32_t operand_cimm16sp(rv_inst inst) 2159 + { 2160 + return (((int64_t)inst << 51) >> 63) << 9 | 2161 + ((inst << 57) >> 63) << 4 | 2162 + ((inst << 58) >> 63) << 6 | 2163 + ((inst << 59) >> 62) << 7 | 2164 + ((inst << 61) >> 63) << 5; 2165 + } 2166 + 2167 + static int32_t operand_cimmj(rv_inst inst) 2168 + { 2169 + return (((int64_t)inst << 51) >> 63) << 11 | 2170 + ((inst << 52) >> 63) << 4 | 2171 + ((inst << 53) >> 62) << 8 | 2172 + ((inst << 55) >> 63) << 10 | 2173 + ((inst << 56) >> 63) << 6 | 2174 + ((inst << 57) >> 63) << 7 | 2175 + ((inst << 58) >> 61) << 1 | 2176 + ((inst << 61) >> 63) << 5; 2177 + } 2178 + 2179 + static int32_t operand_cimmb(rv_inst inst) 2180 + { 2181 + return (((int64_t)inst << 51) >> 63) << 8 | 2182 + ((inst << 52) >> 62) << 3 | 2183 + ((inst << 57) >> 62) << 6 | 2184 + ((inst << 59) >> 62) << 1 | 2185 + ((inst << 61) >> 63) << 5; 2186 + } 2187 + 2188 + static uint32_t operand_cimmswsp(rv_inst inst) 2189 + { 2190 + return ((inst << 51) >> 60) << 2 | 2191 + ((inst << 55) >> 62) << 6; 2192 + } 2193 + 2194 + static uint32_t operand_cimmsdsp(rv_inst inst) 2195 + { 2196 + return ((inst << 51) >> 61) << 3 | 2197 + ((inst << 54) >> 61) << 6; 2198 + } 2199 + 2200 + static uint32_t operand_cimmsqsp(rv_inst inst) 2201 + { 2202 + return ((inst << 51) >> 62) << 4 | 2203 + ((inst << 53) >> 60) << 6; 2204 + } 2205 + 2206 + static uint32_t operand_cimm4spn(rv_inst inst) 2207 + { 2208 + return ((inst << 51) >> 62) << 4 | 2209 + ((inst << 53) >> 60) << 6 | 2210 + ((inst << 57) >> 63) << 2 | 2211 + ((inst << 58) >> 63) << 3; 2212 + } 2213 + 2214 + static uint32_t operand_cimmw(rv_inst inst) 2215 + { 2216 + return ((inst << 51) >> 61) << 3 | 2217 + ((inst << 57) >> 63) << 2 | 2218 + ((inst << 58) >> 63) << 6; 2219 + } 2220 + 2221 + static uint32_t operand_cimmd(rv_inst inst) 2222 + { 2223 + return ((inst << 51) >> 61) << 3 | 2224 + ((inst << 57) >> 62) << 6; 2225 + } 2226 + 2227 + static uint32_t operand_cimmq(rv_inst inst) 2228 + { 2229 + return ((inst << 51) >> 62) << 4 | 2230 + ((inst << 53) >> 63) << 8 | 2231 + ((inst << 57) >> 62) << 6; 2232 + } 2233 + 2234 + /* decode operands */ 2235 + 2236 + static void decode_inst_operands(rv_decode *dec) 2237 + { 2238 + rv_inst inst = dec->inst; 2239 + dec->codec = opcode_data[dec->op].codec; 2240 + switch (dec->codec) { 2241 + case rv_codec_none: 2242 + dec->rd = dec->rs1 = dec->rs2 = rv_ireg_zero; 2243 + dec->imm = 0; 2244 + break; 2245 + case rv_codec_u: 2246 + dec->rd = operand_rd(inst); 2247 + dec->rs1 = dec->rs2 = rv_ireg_zero; 2248 + dec->imm = operand_imm20(inst); 2249 + break; 2250 + case rv_codec_uj: 2251 + dec->rd = operand_rd(inst); 2252 + dec->rs1 = dec->rs2 = rv_ireg_zero; 2253 + dec->imm = operand_jimm20(inst); 2254 + break; 2255 + case rv_codec_i: 2256 + dec->rd = operand_rd(inst); 2257 + dec->rs1 = operand_rs1(inst); 2258 + dec->rs2 = rv_ireg_zero; 2259 + dec->imm = operand_imm12(inst); 2260 + break; 2261 + case rv_codec_i_sh5: 2262 + dec->rd = operand_rd(inst); 2263 + dec->rs1 = operand_rs1(inst); 2264 + dec->rs2 = rv_ireg_zero; 2265 + dec->imm = operand_shamt5(inst); 2266 + break; 2267 + case rv_codec_i_sh6: 2268 + dec->rd = operand_rd(inst); 2269 + dec->rs1 = operand_rs1(inst); 2270 + dec->rs2 = rv_ireg_zero; 2271 + dec->imm = operand_shamt6(inst); 2272 + break; 2273 + case rv_codec_i_sh7: 2274 + dec->rd = operand_rd(inst); 2275 + dec->rs1 = operand_rs1(inst); 2276 + dec->rs2 = rv_ireg_zero; 2277 + dec->imm = operand_shamt7(inst); 2278 + break; 2279 + case rv_codec_i_csr: 2280 + dec->rd = operand_rd(inst); 2281 + dec->rs1 = operand_rs1(inst); 2282 + dec->rs2 = rv_ireg_zero; 2283 + dec->imm = operand_csr12(inst); 2284 + break; 2285 + case rv_codec_s: 2286 + dec->rd = rv_ireg_zero; 2287 + dec->rs1 = operand_rs1(inst); 2288 + dec->rs2 = operand_rs2(inst); 2289 + dec->imm = operand_simm12(inst); 2290 + break; 2291 + case rv_codec_sb: 2292 + dec->rd = rv_ireg_zero; 2293 + dec->rs1 = operand_rs1(inst); 2294 + dec->rs2 = operand_rs2(inst); 2295 + dec->imm = operand_sbimm12(inst); 2296 + break; 2297 + case rv_codec_r: 2298 + dec->rd = operand_rd(inst); 2299 + dec->rs1 = operand_rs1(inst); 2300 + dec->rs2 = operand_rs2(inst); 2301 + dec->imm = 0; 2302 + break; 2303 + case rv_codec_r_m: 2304 + dec->rd = operand_rd(inst); 2305 + dec->rs1 = operand_rs1(inst); 2306 + dec->rs2 = operand_rs2(inst); 2307 + dec->imm = 0; 2308 + dec->rm = operand_rm(inst); 2309 + break; 2310 + case rv_codec_r4_m: 2311 + dec->rd = operand_rd(inst); 2312 + dec->rs1 = operand_rs1(inst); 2313 + dec->rs2 = operand_rs2(inst); 2314 + dec->rs3 = operand_rs3(inst); 2315 + dec->imm = 0; 2316 + dec->rm = operand_rm(inst); 2317 + break; 2318 + case rv_codec_r_a: 2319 + dec->rd = operand_rd(inst); 2320 + dec->rs1 = operand_rs1(inst); 2321 + dec->rs2 = operand_rs2(inst); 2322 + dec->imm = 0; 2323 + dec->aq = operand_aq(inst); 2324 + dec->rl = operand_rl(inst); 2325 + break; 2326 + case rv_codec_r_l: 2327 + dec->rd = operand_rd(inst); 2328 + dec->rs1 = operand_rs1(inst); 2329 + dec->rs2 = rv_ireg_zero; 2330 + dec->imm = 0; 2331 + dec->aq = operand_aq(inst); 2332 + dec->rl = operand_rl(inst); 2333 + break; 2334 + case rv_codec_r_f: 2335 + dec->rd = dec->rs1 = dec->rs2 = rv_ireg_zero; 2336 + dec->pred = operand_pred(inst); 2337 + dec->succ = operand_succ(inst); 2338 + dec->imm = 0; 2339 + break; 2340 + case rv_codec_cb: 2341 + dec->rd = rv_ireg_zero; 2342 + dec->rs1 = operand_crs1q(inst) + 8; 2343 + dec->rs2 = rv_ireg_zero; 2344 + dec->imm = operand_cimmb(inst); 2345 + break; 2346 + case rv_codec_cb_imm: 2347 + dec->rd = dec->rs1 = operand_crs1rdq(inst) + 8; 2348 + dec->rs2 = rv_ireg_zero; 2349 + dec->imm = operand_cimmi(inst); 2350 + break; 2351 + case rv_codec_cb_sh5: 2352 + dec->rd = dec->rs1 = operand_crs1rdq(inst) + 8; 2353 + dec->rs2 = rv_ireg_zero; 2354 + dec->imm = operand_cimmsh5(inst); 2355 + break; 2356 + case rv_codec_cb_sh6: 2357 + dec->rd = dec->rs1 = operand_crs1rdq(inst) + 8; 2358 + dec->rs2 = rv_ireg_zero; 2359 + dec->imm = operand_cimmsh6(inst); 2360 + break; 2361 + case rv_codec_ci: 2362 + dec->rd = dec->rs1 = operand_crs1rd(inst); 2363 + dec->rs2 = rv_ireg_zero; 2364 + dec->imm = operand_cimmi(inst); 2365 + break; 2366 + case rv_codec_ci_sh5: 2367 + dec->rd = dec->rs1 = operand_crs1rd(inst); 2368 + dec->rs2 = rv_ireg_zero; 2369 + dec->imm = operand_cimmsh5(inst); 2370 + break; 2371 + case rv_codec_ci_sh6: 2372 + dec->rd = dec->rs1 = operand_crs1rd(inst); 2373 + dec->rs2 = rv_ireg_zero; 2374 + dec->imm = operand_cimmsh6(inst); 2375 + break; 2376 + case rv_codec_ci_16sp: 2377 + dec->rd = rv_ireg_sp; 2378 + dec->rs1 = rv_ireg_sp; 2379 + dec->rs2 = rv_ireg_zero; 2380 + dec->imm = operand_cimm16sp(inst); 2381 + break; 2382 + case rv_codec_ci_lwsp: 2383 + dec->rd = operand_crd(inst); 2384 + dec->rs1 = rv_ireg_sp; 2385 + dec->rs2 = rv_ireg_zero; 2386 + dec->imm = operand_cimmlwsp(inst); 2387 + break; 2388 + case rv_codec_ci_ldsp: 2389 + dec->rd = operand_crd(inst); 2390 + dec->rs1 = rv_ireg_sp; 2391 + dec->rs2 = rv_ireg_zero; 2392 + dec->imm = operand_cimmldsp(inst); 2393 + break; 2394 + case rv_codec_ci_lqsp: 2395 + dec->rd = operand_crd(inst); 2396 + dec->rs1 = rv_ireg_sp; 2397 + dec->rs2 = rv_ireg_zero; 2398 + dec->imm = operand_cimmlqsp(inst); 2399 + break; 2400 + case rv_codec_ci_li: 2401 + dec->rd = operand_crd(inst); 2402 + dec->rs1 = rv_ireg_zero; 2403 + dec->rs2 = rv_ireg_zero; 2404 + dec->imm = operand_cimmi(inst); 2405 + break; 2406 + case rv_codec_ci_lui: 2407 + dec->rd = operand_crd(inst); 2408 + dec->rs1 = rv_ireg_zero; 2409 + dec->rs2 = rv_ireg_zero; 2410 + dec->imm = operand_cimmui(inst); 2411 + break; 2412 + case rv_codec_ci_none: 2413 + dec->rd = dec->rs1 = dec->rs2 = rv_ireg_zero; 2414 + dec->imm = 0; 2415 + break; 2416 + case rv_codec_ciw_4spn: 2417 + dec->rd = operand_crdq(inst) + 8; 2418 + dec->rs1 = rv_ireg_sp; 2419 + dec->rs2 = rv_ireg_zero; 2420 + dec->imm = operand_cimm4spn(inst); 2421 + break; 2422 + case rv_codec_cj: 2423 + dec->rd = dec->rs1 = dec->rs2 = rv_ireg_zero; 2424 + dec->imm = operand_cimmj(inst); 2425 + break; 2426 + case rv_codec_cj_jal: 2427 + dec->rd = rv_ireg_ra; 2428 + dec->rs1 = dec->rs2 = rv_ireg_zero; 2429 + dec->imm = operand_cimmj(inst); 2430 + break; 2431 + case rv_codec_cl_lw: 2432 + dec->rd = operand_crdq(inst) + 8; 2433 + dec->rs1 = operand_crs1q(inst) + 8; 2434 + dec->rs2 = rv_ireg_zero; 2435 + dec->imm = operand_cimmw(inst); 2436 + break; 2437 + case rv_codec_cl_ld: 2438 + dec->rd = operand_crdq(inst) + 8; 2439 + dec->rs1 = operand_crs1q(inst) + 8; 2440 + dec->rs2 = rv_ireg_zero; 2441 + dec->imm = operand_cimmd(inst); 2442 + break; 2443 + case rv_codec_cl_lq: 2444 + dec->rd = operand_crdq(inst) + 8; 2445 + dec->rs1 = operand_crs1q(inst) + 8; 2446 + dec->rs2 = rv_ireg_zero; 2447 + dec->imm = operand_cimmq(inst); 2448 + break; 2449 + case rv_codec_cr: 2450 + dec->rd = dec->rs1 = operand_crs1rd(inst); 2451 + dec->rs2 = operand_crs2(inst); 2452 + dec->imm = 0; 2453 + break; 2454 + case rv_codec_cr_mv: 2455 + dec->rd = operand_crd(inst); 2456 + dec->rs1 = operand_crs2(inst); 2457 + dec->rs2 = rv_ireg_zero; 2458 + dec->imm = 0; 2459 + break; 2460 + case rv_codec_cr_jalr: 2461 + dec->rd = rv_ireg_ra; 2462 + dec->rs1 = operand_crs1(inst); 2463 + dec->rs2 = rv_ireg_zero; 2464 + dec->imm = 0; 2465 + break; 2466 + case rv_codec_cr_jr: 2467 + dec->rd = rv_ireg_zero; 2468 + dec->rs1 = operand_crs1(inst); 2469 + dec->rs2 = rv_ireg_zero; 2470 + dec->imm = 0; 2471 + break; 2472 + case rv_codec_cs: 2473 + dec->rd = dec->rs1 = operand_crs1rdq(inst) + 8; 2474 + dec->rs2 = operand_crs2q(inst) + 8; 2475 + dec->imm = 0; 2476 + break; 2477 + case rv_codec_cs_sw: 2478 + dec->rd = rv_ireg_zero; 2479 + dec->rs1 = operand_crs1q(inst) + 8; 2480 + dec->rs2 = operand_crs2q(inst) + 8; 2481 + dec->imm = operand_cimmw(inst); 2482 + break; 2483 + case rv_codec_cs_sd: 2484 + dec->rd = rv_ireg_zero; 2485 + dec->rs1 = operand_crs1q(inst) + 8; 2486 + dec->rs2 = operand_crs2q(inst) + 8; 2487 + dec->imm = operand_cimmd(inst); 2488 + break; 2489 + case rv_codec_cs_sq: 2490 + dec->rd = rv_ireg_zero; 2491 + dec->rs1 = operand_crs1q(inst) + 8; 2492 + dec->rs2 = operand_crs2q(inst) + 8; 2493 + dec->imm = operand_cimmq(inst); 2494 + break; 2495 + case rv_codec_css_swsp: 2496 + dec->rd = rv_ireg_zero; 2497 + dec->rs1 = rv_ireg_sp; 2498 + dec->rs2 = operand_crs2(inst); 2499 + dec->imm = operand_cimmswsp(inst); 2500 + break; 2501 + case rv_codec_css_sdsp: 2502 + dec->rd = rv_ireg_zero; 2503 + dec->rs1 = rv_ireg_sp; 2504 + dec->rs2 = operand_crs2(inst); 2505 + dec->imm = operand_cimmsdsp(inst); 2506 + break; 2507 + case rv_codec_css_sqsp: 2508 + dec->rd = rv_ireg_zero; 2509 + dec->rs1 = rv_ireg_sp; 2510 + dec->rs2 = operand_crs2(inst); 2511 + dec->imm = operand_cimmsqsp(inst); 2512 + break; 2513 + }; 2514 + } 2515 + 2516 + /* check constraint */ 2517 + 2518 + static bool check_constraints(rv_decode *dec, const rvc_constraint *c) 2519 + { 2520 + int32_t imm = dec->imm; 2521 + uint8_t rd = dec->rd, rs1 = dec->rs1, rs2 = dec->rs2; 2522 + while (*c != rvc_end) { 2523 + switch (*c) { 2524 + case rvc_simm_6: 2525 + if (!(imm >= -32 && imm < 32)) { 2526 + return false; 2527 + } 2528 + break; 2529 + case rvc_imm_6: 2530 + if (!(imm <= 63)) { 2531 + return false; 2532 + } 2533 + break; 2534 + case rvc_imm_7: 2535 + if (!(imm <= 127)) { 2536 + return false; 2537 + } 2538 + break; 2539 + case rvc_imm_8: 2540 + if (!(imm <= 255)) { 2541 + return false; 2542 + } 2543 + break; 2544 + case rvc_imm_9: 2545 + if (!(imm <= 511)) { 2546 + return false; 2547 + } 2548 + break; 2549 + case rvc_imm_10: 2550 + if (!(imm <= 1023)) { 2551 + return false; 2552 + } 2553 + break; 2554 + case rvc_imm_12: 2555 + if (!(imm <= 4095)) { 2556 + return false; 2557 + } 2558 + break; 2559 + case rvc_imm_18: 2560 + if (!(imm <= 262143)) { 2561 + return false; 2562 + } 2563 + break; 2564 + case rvc_imm_nz: 2565 + if (!(imm != 0)) { 2566 + return false; 2567 + } 2568 + break; 2569 + case rvc_imm_x2: 2570 + if (!((imm & 0b1) == 0)) { 2571 + return false; 2572 + } 2573 + break; 2574 + case rvc_imm_x4: 2575 + if (!((imm & 0b11) == 0)) { 2576 + return false; 2577 + } 2578 + break; 2579 + case rvc_imm_x8: 2580 + if (!((imm & 0b111) == 0)) { 2581 + return false; 2582 + } 2583 + break; 2584 + case rvc_imm_x16: 2585 + if (!((imm & 0b1111) == 0)) { 2586 + return false; 2587 + } 2588 + break; 2589 + case rvc_rd_b3: 2590 + if (!(rd >= 8 && rd <= 15)) { 2591 + return false; 2592 + } 2593 + break; 2594 + case rvc_rs1_b3: 2595 + if (!(rs1 >= 8 && rs1 <= 15)) { 2596 + return false; 2597 + } 2598 + break; 2599 + case rvc_rs2_b3: 2600 + if (!(rs2 >= 8 && rs2 <= 15)) { 2601 + return false; 2602 + } 2603 + break; 2604 + case rvc_rd_eq_rs1: 2605 + if (!(rd == rs1)) { 2606 + return false; 2607 + } 2608 + break; 2609 + case rvc_rd_eq_ra: 2610 + if (!(rd == 1)) { 2611 + return false; 2612 + } 2613 + break; 2614 + case rvc_rd_eq_sp: 2615 + if (!(rd == 2)) { 2616 + return false; 2617 + } 2618 + break; 2619 + case rvc_rd_eq_x0: 2620 + if (!(rd == 0)) { 2621 + return false; 2622 + } 2623 + break; 2624 + case rvc_rs1_eq_sp: 2625 + if (!(rs1 == 2)) { 2626 + return false; 2627 + } 2628 + break; 2629 + case rvc_rs1_eq_x0: 2630 + if (!(rs1 == 0)) { 2631 + return false; 2632 + } 2633 + break; 2634 + case rvc_rs2_eq_x0: 2635 + if (!(rs2 == 0)) { 2636 + return false; 2637 + } 2638 + break; 2639 + case rvc_rd_ne_x0_x2: 2640 + if (!(rd != 0 && rd != 2)) { 2641 + return false; 2642 + } 2643 + break; 2644 + case rvc_rd_ne_x0: 2645 + if (!(rd != 0)) { 2646 + return false; 2647 + } 2648 + break; 2649 + case rvc_rs1_ne_x0: 2650 + if (!(rs1 != 0)) { 2651 + return false; 2652 + } 2653 + break; 2654 + case rvc_rs2_ne_x0: 2655 + if (!(rs2 != 0)) { 2656 + return false; 2657 + } 2658 + break; 2659 + case rvc_rs2_eq_rs1: 2660 + if (!(rs2 == rs1)) { 2661 + return false; 2662 + } 2663 + break; 2664 + case rvc_rs1_eq_ra: 2665 + if (!(rs1 == 1)) { 2666 + return false; 2667 + } 2668 + break; 2669 + case rvc_imm_eq_zero: 2670 + if (!(imm == 0)) { 2671 + return false; 2672 + } 2673 + break; 2674 + case rvc_imm_eq_n1: 2675 + if (!(imm == -1)) { 2676 + return false; 2677 + } 2678 + break; 2679 + case rvc_imm_eq_p1: 2680 + if (!(imm == 1)) { 2681 + return false; 2682 + } 2683 + break; 2684 + case rvc_csr_eq_0x001: 2685 + if (!(imm == 0x001)) { 2686 + return false; 2687 + } 2688 + break; 2689 + case rvc_csr_eq_0x002: 2690 + if (!(imm == 0x002)) { 2691 + return false; 2692 + } 2693 + break; 2694 + case rvc_csr_eq_0x003: 2695 + if (!(imm == 0x003)) { 2696 + return false; 2697 + } 2698 + break; 2699 + case rvc_csr_eq_0xc00: 2700 + if (!(imm == 0xc00)) { 2701 + return false; 2702 + } 2703 + break; 2704 + case rvc_csr_eq_0xc01: 2705 + if (!(imm == 0xc01)) { 2706 + return false; 2707 + } 2708 + break; 2709 + case rvc_csr_eq_0xc02: 2710 + if (!(imm == 0xc02)) { 2711 + return false; 2712 + } 2713 + break; 2714 + case rvc_csr_eq_0xc80: 2715 + if (!(imm == 0xc80)) { 2716 + return false; 2717 + } 2718 + break; 2719 + case rvc_csr_eq_0xc81: 2720 + if (!(imm == 0xc81)) { 2721 + return false; 2722 + } 2723 + break; 2724 + case rvc_csr_eq_0xc82: 2725 + if (!(imm == 0xc82)) { 2726 + return false; 2727 + } 2728 + break; 2729 + default: break; 2730 + } 2731 + c++; 2732 + } 2733 + return true; 2734 + } 2735 + 2736 + /* instruction length */ 2737 + 2738 + static size_t inst_length(rv_inst inst) 2739 + { 2740 + /* NOTE: supports maximum instruction size of 64-bits */ 2741 + 2742 + /* instruction length coding 2743 + * 2744 + * aa - 16 bit aa != 11 2745 + * bbb11 - 32 bit bbb != 111 2746 + * 011111 - 48 bit 2747 + * 0111111 - 64 bit 2748 + */ 2749 + 2750 + return (inst & 0b11) != 0b11 ? 2 2751 + : (inst & 0b11100) != 0b11100 ? 4 2752 + : (inst & 0b111111) == 0b011111 ? 6 2753 + : (inst & 0b1111111) == 0b0111111 ? 8 2754 + : 0; 2755 + } 2756 + 2757 + /* format instruction */ 2758 + 2759 + static void append(char *s1, const char *s2, size_t n) 2760 + { 2761 + size_t l1 = strlen(s1); 2762 + if (n - l1 - 1 > 0) { 2763 + strncat(s1, s2, n - l1); 2764 + } 2765 + } 2766 + 2767 + static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec) 2768 + { 2769 + char tmp[64]; 2770 + const char *fmt; 2771 + 2772 + if (dec->op == rv_op_illegal) { 2773 + size_t len = inst_length(dec->inst); 2774 + switch (len) { 2775 + case 2: 2776 + snprintf(buf, buflen, "(0x%04" PRIx64 ")", dec->inst); 2777 + break; 2778 + case 4: 2779 + snprintf(buf, buflen, "(0x%08" PRIx64 ")", dec->inst); 2780 + break; 2781 + case 6: 2782 + snprintf(buf, buflen, "(0x%012" PRIx64 ")", dec->inst); 2783 + break; 2784 + default: 2785 + snprintf(buf, buflen, "(0x%016" PRIx64 ")", dec->inst); 2786 + break; 2787 + } 2788 + return; 2789 + } 2790 + 2791 + fmt = opcode_data[dec->op].format; 2792 + while (*fmt) { 2793 + switch (*fmt) { 2794 + case 'O': 2795 + append(buf, opcode_data[dec->op].name, buflen); 2796 + break; 2797 + case '(': 2798 + append(buf, "(", buflen); 2799 + break; 2800 + case ',': 2801 + append(buf, ",", buflen); 2802 + break; 2803 + case ')': 2804 + append(buf, ")", buflen); 2805 + break; 2806 + case '0': 2807 + append(buf, rv_ireg_name_sym[dec->rd], buflen); 2808 + break; 2809 + case '1': 2810 + append(buf, rv_ireg_name_sym[dec->rs1], buflen); 2811 + break; 2812 + case '2': 2813 + append(buf, rv_ireg_name_sym[dec->rs2], buflen); 2814 + break; 2815 + case '3': 2816 + append(buf, rv_freg_name_sym[dec->rd], buflen); 2817 + break; 2818 + case '4': 2819 + append(buf, rv_freg_name_sym[dec->rs1], buflen); 2820 + break; 2821 + case '5': 2822 + append(buf, rv_freg_name_sym[dec->rs2], buflen); 2823 + break; 2824 + case '6': 2825 + append(buf, rv_freg_name_sym[dec->rs3], buflen); 2826 + break; 2827 + case '7': 2828 + snprintf(tmp, sizeof(tmp), "%d", dec->rs1); 2829 + append(buf, tmp, buflen); 2830 + break; 2831 + case 'i': 2832 + snprintf(tmp, sizeof(tmp), "%d", dec->imm); 2833 + append(buf, tmp, buflen); 2834 + break; 2835 + case 'o': 2836 + snprintf(tmp, sizeof(tmp), "%d", dec->imm); 2837 + append(buf, tmp, buflen); 2838 + while (strlen(buf) < tab * 2) { 2839 + append(buf, " ", buflen); 2840 + } 2841 + snprintf(tmp, sizeof(tmp), "# 0x%" PRIx64, 2842 + dec->pc + dec->imm); 2843 + append(buf, tmp, buflen); 2844 + break; 2845 + case 'c': { 2846 + const char *name = csr_name(dec->imm & 0xfff); 2847 + if (name) { 2848 + append(buf, name, buflen); 2849 + } else { 2850 + snprintf(tmp, sizeof(tmp), "0x%03x", dec->imm & 0xfff); 2851 + append(buf, tmp, buflen); 2852 + } 2853 + break; 2854 + } 2855 + case 'r': 2856 + switch (dec->rm) { 2857 + case rv_rm_rne: 2858 + append(buf, "rne", buflen); 2859 + break; 2860 + case rv_rm_rtz: 2861 + append(buf, "rtz", buflen); 2862 + break; 2863 + case rv_rm_rdn: 2864 + append(buf, "rdn", buflen); 2865 + break; 2866 + case rv_rm_rup: 2867 + append(buf, "rup", buflen); 2868 + break; 2869 + case rv_rm_rmm: 2870 + append(buf, "rmm", buflen); 2871 + break; 2872 + case rv_rm_dyn: 2873 + append(buf, "dyn", buflen); 2874 + break; 2875 + default: 2876 + append(buf, "inv", buflen); 2877 + break; 2878 + } 2879 + break; 2880 + case 'p': 2881 + if (dec->pred & rv_fence_i) { 2882 + append(buf, "i", buflen); 2883 + } 2884 + if (dec->pred & rv_fence_o) { 2885 + append(buf, "o", buflen); 2886 + } 2887 + if (dec->pred & rv_fence_r) { 2888 + append(buf, "r", buflen); 2889 + } 2890 + if (dec->pred & rv_fence_w) { 2891 + append(buf, "w", buflen); 2892 + } 2893 + break; 2894 + case 's': 2895 + if (dec->succ & rv_fence_i) { 2896 + append(buf, "i", buflen); 2897 + } 2898 + if (dec->succ & rv_fence_o) { 2899 + append(buf, "o", buflen); 2900 + } 2901 + if (dec->succ & rv_fence_r) { 2902 + append(buf, "r", buflen); 2903 + } 2904 + if (dec->succ & rv_fence_w) { 2905 + append(buf, "w", buflen); 2906 + } 2907 + break; 2908 + case '\t': 2909 + while (strlen(buf) < tab) { 2910 + append(buf, " ", buflen); 2911 + } 2912 + break; 2913 + case 'A': 2914 + if (dec->aq) { 2915 + append(buf, ".aq", buflen); 2916 + } 2917 + break; 2918 + case 'R': 2919 + if (dec->rl) { 2920 + append(buf, ".rl", buflen); 2921 + } 2922 + break; 2923 + default: 2924 + break; 2925 + } 2926 + fmt++; 2927 + } 2928 + } 2929 + 2930 + /* lift instruction to pseudo-instruction */ 2931 + 2932 + static void decode_inst_lift_pseudo(rv_decode *dec) 2933 + { 2934 + const rv_comp_data *comp_data = opcode_data[dec->op].pseudo; 2935 + if (!comp_data) { 2936 + return; 2937 + } 2938 + while (comp_data->constraints) { 2939 + if (check_constraints(dec, comp_data->constraints)) { 2940 + dec->op = comp_data->op; 2941 + dec->codec = opcode_data[dec->op].codec; 2942 + return; 2943 + } 2944 + comp_data++; 2945 + } 2946 + } 2947 + 2948 + /* decompress instruction */ 2949 + 2950 + static void decode_inst_decompress_rv32(rv_decode *dec) 2951 + { 2952 + int decomp_op = opcode_data[dec->op].decomp_rv32; 2953 + if (decomp_op != rv_op_illegal) { 2954 + dec->op = decomp_op; 2955 + dec->codec = opcode_data[decomp_op].codec; 2956 + } 2957 + } 2958 + 2959 + static void decode_inst_decompress_rv64(rv_decode *dec) 2960 + { 2961 + int decomp_op = opcode_data[dec->op].decomp_rv64; 2962 + if (decomp_op != rv_op_illegal) { 2963 + dec->op = decomp_op; 2964 + dec->codec = opcode_data[decomp_op].codec; 2965 + } 2966 + } 2967 + 2968 + static void decode_inst_decompress_rv128(rv_decode *dec) 2969 + { 2970 + int decomp_op = opcode_data[dec->op].decomp_rv128; 2971 + if (decomp_op != rv_op_illegal) { 2972 + dec->op = decomp_op; 2973 + dec->codec = opcode_data[decomp_op].codec; 2974 + } 2975 + } 2976 + 2977 + static void decode_inst_decompress(rv_decode *dec, rv_isa isa) 2978 + { 2979 + switch (isa) { 2980 + case rv32: 2981 + decode_inst_decompress_rv32(dec); 2982 + break; 2983 + case rv64: 2984 + decode_inst_decompress_rv64(dec); 2985 + break; 2986 + case rv128: 2987 + decode_inst_decompress_rv128(dec); 2988 + break; 2989 + } 2990 + } 2991 + 2992 + /* disassemble instruction */ 2993 + 2994 + static void 2995 + disasm_inst(char *buf, size_t buflen, rv_isa isa, uint64_t pc, rv_inst inst) 2996 + { 2997 + rv_decode dec = { 0 }; 2998 + dec.pc = pc; 2999 + dec.inst = inst; 3000 + decode_inst_opcode(&dec, isa); 3001 + decode_inst_operands(&dec); 3002 + decode_inst_decompress(&dec, isa); 3003 + decode_inst_lift_pseudo(&dec); 3004 + format_inst(buf, buflen, 16, &dec); 3005 + } 3006 + 3007 + static int 3008 + print_insn_riscv(bfd_vma memaddr, struct disassemble_info *info, rv_isa isa) 3009 + { 3010 + char buf[128] = { 0 }; 3011 + bfd_byte packet[2]; 3012 + rv_inst inst = 0; 3013 + size_t len = 2; 3014 + bfd_vma n; 3015 + int status; 3016 + 3017 + /* Instructions are made of 2-byte packets in little-endian order */ 3018 + for (n = 0; n < len; n += 2) { 3019 + status = (*info->read_memory_func)(memaddr + n, packet, 2, info); 3020 + if (status != 0) { 3021 + /* Don't fail just because we fell off the end. */ 3022 + if (n > 0) { 3023 + break; 3024 + } 3025 + (*info->memory_error_func)(status, memaddr, info); 3026 + return status; 3027 + } 3028 + inst |= ((rv_inst) bfd_getl16(packet)) << (8 * n); 3029 + if (n == 0) { 3030 + len = inst_length(inst); 3031 + } 3032 + } 3033 + 3034 + disasm_inst(buf, sizeof(buf), isa, memaddr, inst); 3035 + (*info->fprintf_func)(info->stream, "%s", buf); 3036 + 3037 + return len; 3038 + } 3039 + 3040 + int print_insn_riscv32(bfd_vma memaddr, struct disassemble_info *info) 3041 + { 3042 + return print_insn_riscv(memaddr, info, rv32); 3043 + } 3044 + 3045 + int print_insn_riscv64(bfd_vma memaddr, struct disassemble_info *info) 3046 + { 3047 + return print_insn_riscv(memaddr, info, rv64); 3048 + }
+4 -3
fpu/softfloat-specialize.h
··· 114 114 #if defined(TARGET_SPARC) || defined(TARGET_M68K) 115 115 return const_float32(0x7FFFFFFF); 116 116 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \ 117 - defined(TARGET_XTENSA) || defined(TARGET_S390X) || defined(TARGET_TRICORE) 117 + defined(TARGET_XTENSA) || defined(TARGET_S390X) || \ 118 + defined(TARGET_TRICORE) || defined(TARGET_RISCV) 118 119 return const_float32(0x7FC00000); 119 120 #elif defined(TARGET_HPPA) 120 121 return const_float32(0x7FA00000); ··· 139 140 #if defined(TARGET_SPARC) || defined(TARGET_M68K) 140 141 return const_float64(LIT64(0x7FFFFFFFFFFFFFFF)); 141 142 #elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA) || \ 142 - defined(TARGET_S390X) 143 + defined(TARGET_S390X) || defined(TARGET_RISCV) 143 144 return const_float64(LIT64(0x7FF8000000000000)); 144 145 #elif defined(TARGET_HPPA) 145 146 return const_float64(LIT64(0x7FF4000000000000)); ··· 203 204 r.high = LIT64(0x7FFF7FFFFFFFFFFF); 204 205 } else { 205 206 r.low = LIT64(0x0000000000000000); 206 - #if defined(TARGET_S390X) || defined(TARGET_PPC) 207 + #if defined(TARGET_S390X) || defined(TARGET_PPC) || defined(TARGET_RISCV) 207 208 r.high = LIT64(0x7FFF800000000000); 208 209 #else 209 210 r.high = LIT64(0xFFFF800000000000);
+16 -2
hw/core/loader.c
··· 450 450 int clear_lsb, int data_swab, AddressSpace *as, 451 451 bool load_rom) 452 452 { 453 + return load_elf_ram_sym(filename, translate_fn, translate_opaque, 454 + pentry, lowaddr, highaddr, big_endian, 455 + elf_machine, clear_lsb, data_swab, as, 456 + load_rom, NULL); 457 + } 458 + 459 + /* return < 0 if error, otherwise the number of bytes loaded in memory */ 460 + int load_elf_ram_sym(const char *filename, 461 + uint64_t (*translate_fn)(void *, uint64_t), 462 + void *translate_opaque, uint64_t *pentry, 463 + uint64_t *lowaddr, uint64_t *highaddr, int big_endian, 464 + int elf_machine, int clear_lsb, int data_swab, 465 + AddressSpace *as, bool load_rom, symbol_fn_t sym_cb) 466 + { 453 467 int fd, data_order, target_data_order, must_swab, ret = ELF_LOAD_FAILED; 454 468 uint8_t e_ident[EI_NIDENT]; 455 469 ··· 488 502 if (e_ident[EI_CLASS] == ELFCLASS64) { 489 503 ret = load_elf64(filename, fd, translate_fn, translate_opaque, must_swab, 490 504 pentry, lowaddr, highaddr, elf_machine, clear_lsb, 491 - data_swab, as, load_rom); 505 + data_swab, as, load_rom, sym_cb); 492 506 } else { 493 507 ret = load_elf32(filename, fd, translate_fn, translate_opaque, must_swab, 494 508 pentry, lowaddr, highaddr, elf_machine, clear_lsb, 495 - data_swab, as, load_rom); 509 + data_swab, as, load_rom, sym_cb); 496 510 } 497 511 498 512 fail:
+11
hw/riscv/Makefile.objs
··· 1 + obj-y += riscv_htif.o 2 + obj-y += riscv_hart.o 3 + obj-y += sifive_e.o 4 + obj-y += sifive_clint.o 5 + obj-y += sifive_prci.o 6 + obj-y += sifive_plic.o 7 + obj-y += sifive_test.o 8 + obj-y += sifive_u.o 9 + obj-y += sifive_uart.o 10 + obj-y += spike.o 11 + obj-y += virt.o
+89
hw/riscv/riscv_hart.c
··· 1 + /* 2 + * QEMU RISCV Hart Array 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * Holds the state of a heterogenous array of RISC-V harts 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #include "qemu/osdep.h" 22 + #include "qapi/error.h" 23 + #include "hw/sysbus.h" 24 + #include "target/riscv/cpu.h" 25 + #include "hw/riscv/riscv_hart.h" 26 + 27 + static Property riscv_harts_props[] = { 28 + DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts, 1), 29 + DEFINE_PROP_STRING("cpu-type", RISCVHartArrayState, cpu_type), 30 + DEFINE_PROP_END_OF_LIST(), 31 + }; 32 + 33 + static void riscv_harts_cpu_reset(void *opaque) 34 + { 35 + RISCVCPU *cpu = opaque; 36 + cpu_reset(CPU(cpu)); 37 + } 38 + 39 + static void riscv_harts_realize(DeviceState *dev, Error **errp) 40 + { 41 + RISCVHartArrayState *s = RISCV_HART_ARRAY(dev); 42 + Error *err = NULL; 43 + int n; 44 + 45 + s->harts = g_new0(RISCVCPU, s->num_harts); 46 + 47 + for (n = 0; n < s->num_harts; n++) { 48 + 49 + object_initialize(&s->harts[n], sizeof(RISCVCPU), s->cpu_type); 50 + s->harts[n].env.mhartid = n; 51 + object_property_add_child(OBJECT(s), "harts[*]", OBJECT(&s->harts[n]), 52 + &error_abort); 53 + qemu_register_reset(riscv_harts_cpu_reset, &s->harts[n]); 54 + object_property_set_bool(OBJECT(&s->harts[n]), true, 55 + "realized", &err); 56 + if (err) { 57 + error_propagate(errp, err); 58 + return; 59 + } 60 + } 61 + } 62 + 63 + static void riscv_harts_class_init(ObjectClass *klass, void *data) 64 + { 65 + DeviceClass *dc = DEVICE_CLASS(klass); 66 + 67 + dc->props = riscv_harts_props; 68 + dc->realize = riscv_harts_realize; 69 + } 70 + 71 + static void riscv_harts_init(Object *obj) 72 + { 73 + /* RISCVHartArrayState *s = SIFIVE_COREPLEX(obj); */ 74 + } 75 + 76 + static const TypeInfo riscv_harts_info = { 77 + .name = TYPE_RISCV_HART_ARRAY, 78 + .parent = TYPE_SYS_BUS_DEVICE, 79 + .instance_size = sizeof(RISCVHartArrayState), 80 + .instance_init = riscv_harts_init, 81 + .class_init = riscv_harts_class_init, 82 + }; 83 + 84 + static void riscv_harts_register_types(void) 85 + { 86 + type_register_static(&riscv_harts_info); 87 + } 88 + 89 + type_init(riscv_harts_register_types)
+258
hw/riscv/riscv_htif.c
··· 1 + /* 2 + * QEMU RISC-V Host Target Interface (HTIF) Emulation 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This provides HTIF device emulation for QEMU. At the moment this allows 8 + * for identical copies of bbl/linux to run on both spike and QEMU. 9 + * 10 + * This program is free software; you can redistribute it and/or modify it 11 + * under the terms and conditions of the GNU General Public License, 12 + * version 2 or later, as published by the Free Software Foundation. 13 + * 14 + * This program is distributed in the hope it will be useful, but WITHOUT 15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 17 + * more details. 18 + * 19 + * You should have received a copy of the GNU General Public License along with 20 + * this program. If not, see <http://www.gnu.org/licenses/>. 21 + */ 22 + 23 + #include "qemu/osdep.h" 24 + #include "qapi/error.h" 25 + #include "qemu/log.h" 26 + #include "hw/sysbus.h" 27 + #include "hw/char/serial.h" 28 + #include "chardev/char.h" 29 + #include "chardev/char-fe.h" 30 + #include "hw/riscv/riscv_htif.h" 31 + #include "qemu/timer.h" 32 + #include "exec/address-spaces.h" 33 + #include "qemu/error-report.h" 34 + 35 + #define RISCV_DEBUG_HTIF 0 36 + #define HTIF_DEBUG(fmt, ...) \ 37 + do { \ 38 + if (RISCV_DEBUG_HTIF) { \ 39 + qemu_log_mask(LOG_TRACE, "%s: " fmt "\n", __func__, ##__VA_ARGS__);\ 40 + } \ 41 + } while (0) 42 + 43 + static uint64_t fromhost_addr, tohost_addr; 44 + 45 + void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value, 46 + uint64_t st_size) 47 + { 48 + if (strcmp("fromhost", st_name) == 0) { 49 + fromhost_addr = st_value; 50 + if (st_size != 8) { 51 + error_report("HTIF fromhost must be 8 bytes"); 52 + exit(1); 53 + } 54 + } else if (strcmp("tohost", st_name) == 0) { 55 + tohost_addr = st_value; 56 + if (st_size != 8) { 57 + error_report("HTIF tohost must be 8 bytes"); 58 + exit(1); 59 + } 60 + } 61 + } 62 + 63 + /* 64 + * Called by the char dev to see if HTIF is ready to accept input. 65 + */ 66 + static int htif_can_recv(void *opaque) 67 + { 68 + return 1; 69 + } 70 + 71 + /* 72 + * Called by the char dev to supply input to HTIF console. 73 + * We assume that we will receive one character at a time. 74 + */ 75 + static void htif_recv(void *opaque, const uint8_t *buf, int size) 76 + { 77 + HTIFState *htifstate = opaque; 78 + 79 + if (size != 1) { 80 + return; 81 + } 82 + 83 + /* TODO - we need to check whether mfromhost is zero which indicates 84 + the device is ready to receive. The current implementation 85 + will drop characters */ 86 + 87 + uint64_t val_written = htifstate->pending_read; 88 + uint64_t resp = 0x100 | *buf; 89 + 90 + htifstate->env->mfromhost = (val_written >> 48 << 48) | (resp << 16 >> 16); 91 + } 92 + 93 + /* 94 + * Called by the char dev to supply special events to the HTIF console. 95 + * Not used for HTIF. 96 + */ 97 + static void htif_event(void *opaque, int event) 98 + { 99 + 100 + } 101 + 102 + static int htif_be_change(void *opaque) 103 + { 104 + HTIFState *s = opaque; 105 + 106 + qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, 107 + htif_be_change, s, NULL, true); 108 + 109 + return 0; 110 + } 111 + 112 + static void htif_handle_tohost_write(HTIFState *htifstate, uint64_t val_written) 113 + { 114 + uint8_t device = val_written >> 56; 115 + uint8_t cmd = val_written >> 48; 116 + uint64_t payload = val_written & 0xFFFFFFFFFFFFULL; 117 + int resp = 0; 118 + 119 + HTIF_DEBUG("mtohost write: device: %d cmd: %d what: %02" PRIx64 120 + " -payload: %016" PRIx64 "\n", device, cmd, payload & 0xFF, payload); 121 + 122 + /* 123 + * Currently, there is a fixed mapping of devices: 124 + * 0: riscv-tests Pass/Fail Reporting Only (no syscall proxy) 125 + * 1: Console 126 + */ 127 + if (unlikely(device == 0x0)) { 128 + /* frontend syscall handler, shutdown and exit code support */ 129 + if (cmd == 0x0) { 130 + if (payload & 0x1) { 131 + /* exit code */ 132 + int exit_code = payload >> 1; 133 + exit(exit_code); 134 + } else { 135 + qemu_log_mask(LOG_UNIMP, "pk syscall proxy not supported\n"); 136 + } 137 + } else { 138 + qemu_log("HTIF device %d: unknown command\n", device); 139 + } 140 + } else if (likely(device == 0x1)) { 141 + /* HTIF Console */ 142 + if (cmd == 0x0) { 143 + /* this should be a queue, but not yet implemented as such */ 144 + htifstate->pending_read = val_written; 145 + htifstate->env->mtohost = 0; /* clear to indicate we read */ 146 + return; 147 + } else if (cmd == 0x1) { 148 + qemu_chr_fe_write(&htifstate->chr, (uint8_t *)&payload, 1); 149 + resp = 0x100 | (uint8_t)payload; 150 + } else { 151 + qemu_log("HTIF device %d: unknown command\n", device); 152 + } 153 + } else { 154 + qemu_log("HTIF unknown device or command\n"); 155 + HTIF_DEBUG("device: %d cmd: %d what: %02" PRIx64 156 + " payload: %016" PRIx64, device, cmd, payload & 0xFF, payload); 157 + } 158 + /* 159 + * - latest bbl does not set fromhost to 0 if there is a value in tohost 160 + * - with this code enabled, qemu hangs waiting for fromhost to go to 0 161 + * - with this code disabled, qemu works with bbl priv v1.9.1 and v1.10 162 + * - HTIF needs protocol documentation and a more complete state machine 163 + 164 + while (!htifstate->fromhost_inprogress && 165 + htifstate->env->mfromhost != 0x0) { 166 + } 167 + */ 168 + htifstate->env->mfromhost = (val_written >> 48 << 48) | (resp << 16 >> 16); 169 + htifstate->env->mtohost = 0; /* clear to indicate we read */ 170 + } 171 + 172 + #define TOHOST_OFFSET1 (htifstate->tohost_offset) 173 + #define TOHOST_OFFSET2 (htifstate->tohost_offset + 4) 174 + #define FROMHOST_OFFSET1 (htifstate->fromhost_offset) 175 + #define FROMHOST_OFFSET2 (htifstate->fromhost_offset + 4) 176 + 177 + /* CPU wants to read an HTIF register */ 178 + static uint64_t htif_mm_read(void *opaque, hwaddr addr, unsigned size) 179 + { 180 + HTIFState *htifstate = opaque; 181 + if (addr == TOHOST_OFFSET1) { 182 + return htifstate->env->mtohost & 0xFFFFFFFF; 183 + } else if (addr == TOHOST_OFFSET2) { 184 + return (htifstate->env->mtohost >> 32) & 0xFFFFFFFF; 185 + } else if (addr == FROMHOST_OFFSET1) { 186 + return htifstate->env->mfromhost & 0xFFFFFFFF; 187 + } else if (addr == FROMHOST_OFFSET2) { 188 + return (htifstate->env->mfromhost >> 32) & 0xFFFFFFFF; 189 + } else { 190 + qemu_log("Invalid htif read: address %016" PRIx64 "\n", 191 + (uint64_t)addr); 192 + return 0; 193 + } 194 + } 195 + 196 + /* CPU wrote to an HTIF register */ 197 + static void htif_mm_write(void *opaque, hwaddr addr, 198 + uint64_t value, unsigned size) 199 + { 200 + HTIFState *htifstate = opaque; 201 + if (addr == TOHOST_OFFSET1) { 202 + if (htifstate->env->mtohost == 0x0) { 203 + htifstate->allow_tohost = 1; 204 + htifstate->env->mtohost = value & 0xFFFFFFFF; 205 + } else { 206 + htifstate->allow_tohost = 0; 207 + } 208 + } else if (addr == TOHOST_OFFSET2) { 209 + if (htifstate->allow_tohost) { 210 + htifstate->env->mtohost |= value << 32; 211 + htif_handle_tohost_write(htifstate, htifstate->env->mtohost); 212 + } 213 + } else if (addr == FROMHOST_OFFSET1) { 214 + htifstate->fromhost_inprogress = 1; 215 + htifstate->env->mfromhost = value & 0xFFFFFFFF; 216 + } else if (addr == FROMHOST_OFFSET2) { 217 + htifstate->env->mfromhost |= value << 32; 218 + htifstate->fromhost_inprogress = 0; 219 + } else { 220 + qemu_log("Invalid htif write: address %016" PRIx64 "\n", 221 + (uint64_t)addr); 222 + } 223 + } 224 + 225 + static const MemoryRegionOps htif_mm_ops = { 226 + .read = htif_mm_read, 227 + .write = htif_mm_write, 228 + }; 229 + 230 + HTIFState *htif_mm_init(MemoryRegion *address_space, MemoryRegion *main_mem, 231 + CPURISCVState *env, Chardev *chr) 232 + { 233 + uint64_t base = MIN(tohost_addr, fromhost_addr); 234 + uint64_t size = MAX(tohost_addr + 8, fromhost_addr + 8) - base; 235 + uint64_t tohost_offset = tohost_addr - base; 236 + uint64_t fromhost_offset = fromhost_addr - base; 237 + 238 + HTIFState *s = g_malloc0(sizeof(HTIFState)); 239 + s->address_space = address_space; 240 + s->main_mem = main_mem; 241 + s->main_mem_ram_ptr = memory_region_get_ram_ptr(main_mem); 242 + s->env = env; 243 + s->tohost_offset = tohost_offset; 244 + s->fromhost_offset = fromhost_offset; 245 + s->pending_read = 0; 246 + s->allow_tohost = 0; 247 + s->fromhost_inprogress = 0; 248 + qemu_chr_fe_init(&s->chr, chr, &error_abort); 249 + qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, 250 + htif_be_change, s, NULL, true); 251 + if (base) { 252 + memory_region_init_io(&s->mmio, NULL, &htif_mm_ops, s, 253 + TYPE_HTIF_UART, size); 254 + memory_region_add_subregion(address_space, base, &s->mmio); 255 + } 256 + 257 + return s; 258 + }
+254
hw/riscv/sifive_clint.c
··· 1 + /* 2 + * SiFive CLINT (Core Local Interruptor) 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017 SiFive, Inc. 6 + * 7 + * This provides real-time clock, timer and interprocessor interrupts. 8 + * 9 + * This program is free software; you can redistribute it and/or modify it 10 + * under the terms and conditions of the GNU General Public License, 11 + * version 2 or later, as published by the Free Software Foundation. 12 + * 13 + * This program is distributed in the hope it will be useful, but WITHOUT 14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 + * more details. 17 + * 18 + * You should have received a copy of the GNU General Public License along with 19 + * this program. If not, see <http://www.gnu.org/licenses/>. 20 + */ 21 + 22 + #include "qemu/osdep.h" 23 + #include "qemu/error-report.h" 24 + #include "hw/sysbus.h" 25 + #include "target/riscv/cpu.h" 26 + #include "hw/riscv/sifive_clint.h" 27 + #include "qemu/timer.h" 28 + 29 + /* See: riscv-pk/machine/sbi_entry.S and arch/riscv/kernel/time.c */ 30 + #define TIMER_FREQ (10 * 1000 * 1000) 31 + 32 + static uint64_t cpu_riscv_read_rtc(void) 33 + { 34 + return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), TIMER_FREQ, 35 + NANOSECONDS_PER_SECOND); 36 + } 37 + 38 + /* 39 + * Called when timecmp is written to update the QEMU timer or immediately 40 + * trigger timer interrupt if mtimecmp <= current timer value. 41 + */ 42 + static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value) 43 + { 44 + uint64_t next; 45 + uint64_t diff; 46 + 47 + uint64_t rtc_r = cpu_riscv_read_rtc(); 48 + 49 + cpu->env.timecmp = value; 50 + if (cpu->env.timecmp <= rtc_r) { 51 + /* if we're setting an MTIMECMP value in the "past", 52 + immediately raise the timer interrupt */ 53 + riscv_set_local_interrupt(cpu, MIP_MTIP, 1); 54 + return; 55 + } 56 + 57 + /* otherwise, set up the future timer interrupt */ 58 + riscv_set_local_interrupt(cpu, MIP_MTIP, 0); 59 + diff = cpu->env.timecmp - rtc_r; 60 + /* back to ns (note args switched in muldiv64) */ 61 + next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 62 + muldiv64(diff, NANOSECONDS_PER_SECOND, TIMER_FREQ); 63 + timer_mod(cpu->env.timer, next); 64 + } 65 + 66 + /* 67 + * Callback used when the timer set using timer_mod expires. 68 + * Should raise the timer interrupt line 69 + */ 70 + static void sifive_clint_timer_cb(void *opaque) 71 + { 72 + RISCVCPU *cpu = opaque; 73 + riscv_set_local_interrupt(cpu, MIP_MTIP, 1); 74 + } 75 + 76 + /* CPU wants to read rtc or timecmp register */ 77 + static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size) 78 + { 79 + SiFiveCLINTState *clint = opaque; 80 + if (addr >= clint->sip_base && 81 + addr < clint->sip_base + (clint->num_harts << 2)) { 82 + size_t hartid = (addr - clint->sip_base) >> 2; 83 + CPUState *cpu = qemu_get_cpu(hartid); 84 + CPURISCVState *env = cpu ? cpu->env_ptr : NULL; 85 + if (!env) { 86 + error_report("clint: invalid timecmp hartid: %zu", hartid); 87 + } else if ((addr & 0x3) == 0) { 88 + return (env->mip & MIP_MSIP) > 0; 89 + } else { 90 + error_report("clint: invalid read: %08x", (uint32_t)addr); 91 + return 0; 92 + } 93 + } else if (addr >= clint->timecmp_base && 94 + addr < clint->timecmp_base + (clint->num_harts << 3)) { 95 + size_t hartid = (addr - clint->timecmp_base) >> 3; 96 + CPUState *cpu = qemu_get_cpu(hartid); 97 + CPURISCVState *env = cpu ? cpu->env_ptr : NULL; 98 + if (!env) { 99 + error_report("clint: invalid timecmp hartid: %zu", hartid); 100 + } else if ((addr & 0x7) == 0) { 101 + /* timecmp_lo */ 102 + uint64_t timecmp = env->timecmp; 103 + return timecmp & 0xFFFFFFFF; 104 + } else if ((addr & 0x7) == 4) { 105 + /* timecmp_hi */ 106 + uint64_t timecmp = env->timecmp; 107 + return (timecmp >> 32) & 0xFFFFFFFF; 108 + } else { 109 + error_report("clint: invalid read: %08x", (uint32_t)addr); 110 + return 0; 111 + } 112 + } else if (addr == clint->time_base) { 113 + /* time_lo */ 114 + return cpu_riscv_read_rtc() & 0xFFFFFFFF; 115 + } else if (addr == clint->time_base + 4) { 116 + /* time_hi */ 117 + return (cpu_riscv_read_rtc() >> 32) & 0xFFFFFFFF; 118 + } 119 + 120 + error_report("clint: invalid read: %08x", (uint32_t)addr); 121 + return 0; 122 + } 123 + 124 + /* CPU wrote to rtc or timecmp register */ 125 + static void sifive_clint_write(void *opaque, hwaddr addr, uint64_t value, 126 + unsigned size) 127 + { 128 + SiFiveCLINTState *clint = opaque; 129 + 130 + if (addr >= clint->sip_base && 131 + addr < clint->sip_base + (clint->num_harts << 2)) { 132 + size_t hartid = (addr - clint->sip_base) >> 2; 133 + CPUState *cpu = qemu_get_cpu(hartid); 134 + CPURISCVState *env = cpu ? cpu->env_ptr : NULL; 135 + if (!env) { 136 + error_report("clint: invalid timecmp hartid: %zu", hartid); 137 + } else if ((addr & 0x3) == 0) { 138 + riscv_set_local_interrupt(RISCV_CPU(cpu), MIP_MSIP, value != 0); 139 + } else { 140 + error_report("clint: invalid sip write: %08x", (uint32_t)addr); 141 + } 142 + return; 143 + } else if (addr >= clint->timecmp_base && 144 + addr < clint->timecmp_base + (clint->num_harts << 3)) { 145 + size_t hartid = (addr - clint->timecmp_base) >> 3; 146 + CPUState *cpu = qemu_get_cpu(hartid); 147 + CPURISCVState *env = cpu ? cpu->env_ptr : NULL; 148 + if (!env) { 149 + error_report("clint: invalid timecmp hartid: %zu", hartid); 150 + } else if ((addr & 0x7) == 0) { 151 + /* timecmp_lo */ 152 + uint64_t timecmp = env->timecmp; 153 + sifive_clint_write_timecmp(RISCV_CPU(cpu), 154 + timecmp << 32 | (value & 0xFFFFFFFF)); 155 + return; 156 + } else if ((addr & 0x7) == 4) { 157 + /* timecmp_hi */ 158 + uint64_t timecmp = env->timecmp; 159 + sifive_clint_write_timecmp(RISCV_CPU(cpu), 160 + value << 32 | (timecmp & 0xFFFFFFFF)); 161 + } else { 162 + error_report("clint: invalid timecmp write: %08x", (uint32_t)addr); 163 + } 164 + return; 165 + } else if (addr == clint->time_base) { 166 + /* time_lo */ 167 + error_report("clint: time_lo write not implemented"); 168 + return; 169 + } else if (addr == clint->time_base + 4) { 170 + /* time_hi */ 171 + error_report("clint: time_hi write not implemented"); 172 + return; 173 + } 174 + 175 + error_report("clint: invalid write: %08x", (uint32_t)addr); 176 + } 177 + 178 + static const MemoryRegionOps sifive_clint_ops = { 179 + .read = sifive_clint_read, 180 + .write = sifive_clint_write, 181 + .endianness = DEVICE_LITTLE_ENDIAN, 182 + .valid = { 183 + .min_access_size = 4, 184 + .max_access_size = 4 185 + } 186 + }; 187 + 188 + static Property sifive_clint_properties[] = { 189 + DEFINE_PROP_UINT32("num-harts", SiFiveCLINTState, num_harts, 0), 190 + DEFINE_PROP_UINT32("sip-base", SiFiveCLINTState, sip_base, 0), 191 + DEFINE_PROP_UINT32("timecmp-base", SiFiveCLINTState, timecmp_base, 0), 192 + DEFINE_PROP_UINT32("time-base", SiFiveCLINTState, time_base, 0), 193 + DEFINE_PROP_UINT32("aperture-size", SiFiveCLINTState, aperture_size, 0), 194 + DEFINE_PROP_END_OF_LIST(), 195 + }; 196 + 197 + static void sifive_clint_realize(DeviceState *dev, Error **errp) 198 + { 199 + SiFiveCLINTState *s = SIFIVE_CLINT(dev); 200 + memory_region_init_io(&s->mmio, OBJECT(dev), &sifive_clint_ops, s, 201 + TYPE_SIFIVE_CLINT, s->aperture_size); 202 + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio); 203 + } 204 + 205 + static void sifive_clint_class_init(ObjectClass *klass, void *data) 206 + { 207 + DeviceClass *dc = DEVICE_CLASS(klass); 208 + dc->realize = sifive_clint_realize; 209 + dc->props = sifive_clint_properties; 210 + } 211 + 212 + static const TypeInfo sifive_clint_info = { 213 + .name = TYPE_SIFIVE_CLINT, 214 + .parent = TYPE_SYS_BUS_DEVICE, 215 + .instance_size = sizeof(SiFiveCLINTState), 216 + .class_init = sifive_clint_class_init, 217 + }; 218 + 219 + static void sifive_clint_register_types(void) 220 + { 221 + type_register_static(&sifive_clint_info); 222 + } 223 + 224 + type_init(sifive_clint_register_types) 225 + 226 + 227 + /* 228 + * Create CLINT device. 229 + */ 230 + DeviceState *sifive_clint_create(hwaddr addr, hwaddr size, uint32_t num_harts, 231 + uint32_t sip_base, uint32_t timecmp_base, uint32_t time_base) 232 + { 233 + int i; 234 + for (i = 0; i < num_harts; i++) { 235 + CPUState *cpu = qemu_get_cpu(i); 236 + CPURISCVState *env = cpu ? cpu->env_ptr : NULL; 237 + if (!env) { 238 + continue; 239 + } 240 + env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 241 + &sifive_clint_timer_cb, cpu); 242 + env->timecmp = 0; 243 + } 244 + 245 + DeviceState *dev = qdev_create(NULL, TYPE_SIFIVE_CLINT); 246 + qdev_prop_set_uint32(dev, "num-harts", num_harts); 247 + qdev_prop_set_uint32(dev, "sip-base", sip_base); 248 + qdev_prop_set_uint32(dev, "timecmp-base", timecmp_base); 249 + qdev_prop_set_uint32(dev, "time-base", time_base); 250 + qdev_prop_set_uint32(dev, "aperture-size", size); 251 + qdev_init_nofail(dev); 252 + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); 253 + return dev; 254 + }
+234
hw/riscv/sifive_e.c
··· 1 + /* 2 + * QEMU RISC-V Board Compatible with SiFive Freedom E SDK 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * Provides a board compatible with the SiFive Freedom E SDK: 7 + * 8 + * 0) UART 9 + * 1) CLINT (Core Level Interruptor) 10 + * 2) PLIC (Platform Level Interrupt Controller) 11 + * 3) PRCI (Power, Reset, Clock, Interrupt) 12 + * 4) Registers emulated as RAM: AON, GPIO, QSPI, PWM 13 + * 5) Flash memory emulated as RAM 14 + * 15 + * The Mask ROM reset vector jumps to the flash payload at 0x2040_0000. 16 + * The OTP ROM and Flash boot code will be emulated in a future version. 17 + * 18 + * This program is free software; you can redistribute it and/or modify it 19 + * under the terms and conditions of the GNU General Public License, 20 + * version 2 or later, as published by the Free Software Foundation. 21 + * 22 + * This program is distributed in the hope it will be useful, but WITHOUT 23 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 24 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 25 + * more details. 26 + * 27 + * You should have received a copy of the GNU General Public License along with 28 + * this program. If not, see <http://www.gnu.org/licenses/>. 29 + */ 30 + 31 + #include "qemu/osdep.h" 32 + #include "qemu/log.h" 33 + #include "qemu/error-report.h" 34 + #include "qapi/error.h" 35 + #include "hw/hw.h" 36 + #include "hw/boards.h" 37 + #include "hw/loader.h" 38 + #include "hw/sysbus.h" 39 + #include "hw/char/serial.h" 40 + #include "target/riscv/cpu.h" 41 + #include "hw/riscv/riscv_hart.h" 42 + #include "hw/riscv/sifive_plic.h" 43 + #include "hw/riscv/sifive_clint.h" 44 + #include "hw/riscv/sifive_prci.h" 45 + #include "hw/riscv/sifive_uart.h" 46 + #include "hw/riscv/sifive_e.h" 47 + #include "chardev/char.h" 48 + #include "sysemu/arch_init.h" 49 + #include "exec/address-spaces.h" 50 + #include "elf.h" 51 + 52 + static const struct MemmapEntry { 53 + hwaddr base; 54 + hwaddr size; 55 + } sifive_e_memmap[] = { 56 + [SIFIVE_E_DEBUG] = { 0x0, 0x100 }, 57 + [SIFIVE_E_MROM] = { 0x1000, 0x2000 }, 58 + [SIFIVE_E_OTP] = { 0x20000, 0x2000 }, 59 + [SIFIVE_E_CLINT] = { 0x2000000, 0x10000 }, 60 + [SIFIVE_E_PLIC] = { 0xc000000, 0x4000000 }, 61 + [SIFIVE_E_AON] = { 0x10000000, 0x8000 }, 62 + [SIFIVE_E_PRCI] = { 0x10008000, 0x8000 }, 63 + [SIFIVE_E_OTP_CTRL] = { 0x10010000, 0x1000 }, 64 + [SIFIVE_E_GPIO0] = { 0x10012000, 0x1000 }, 65 + [SIFIVE_E_UART0] = { 0x10013000, 0x1000 }, 66 + [SIFIVE_E_QSPI0] = { 0x10014000, 0x1000 }, 67 + [SIFIVE_E_PWM0] = { 0x10015000, 0x1000 }, 68 + [SIFIVE_E_UART1] = { 0x10023000, 0x1000 }, 69 + [SIFIVE_E_QSPI1] = { 0x10024000, 0x1000 }, 70 + [SIFIVE_E_PWM1] = { 0x10025000, 0x1000 }, 71 + [SIFIVE_E_QSPI2] = { 0x10034000, 0x1000 }, 72 + [SIFIVE_E_PWM2] = { 0x10035000, 0x1000 }, 73 + [SIFIVE_E_XIP] = { 0x20000000, 0x20000000 }, 74 + [SIFIVE_E_DTIM] = { 0x80000000, 0x4000 } 75 + }; 76 + 77 + static void copy_le32_to_phys(hwaddr pa, uint32_t *rom, size_t len) 78 + { 79 + int i; 80 + for (i = 0; i < (len >> 2); i++) { 81 + stl_phys(&address_space_memory, pa + (i << 2), rom[i]); 82 + } 83 + } 84 + 85 + static uint64_t identity_translate(void *opaque, uint64_t addr) 86 + { 87 + return addr; 88 + } 89 + 90 + static uint64_t load_kernel(const char *kernel_filename) 91 + { 92 + uint64_t kernel_entry, kernel_high; 93 + 94 + if (load_elf(kernel_filename, identity_translate, NULL, 95 + &kernel_entry, NULL, &kernel_high, 96 + 0, ELF_MACHINE, 1, 0) < 0) { 97 + error_report("qemu: could not load kernel '%s'", kernel_filename); 98 + exit(1); 99 + } 100 + return kernel_entry; 101 + } 102 + 103 + static void sifive_mmio_emulate(MemoryRegion *parent, const char *name, 104 + uintptr_t offset, uintptr_t length) 105 + { 106 + MemoryRegion *mock_mmio = g_new(MemoryRegion, 1); 107 + memory_region_init_ram(mock_mmio, NULL, name, length, &error_fatal); 108 + memory_region_add_subregion(parent, offset, mock_mmio); 109 + } 110 + 111 + static void riscv_sifive_e_init(MachineState *machine) 112 + { 113 + const struct MemmapEntry *memmap = sifive_e_memmap; 114 + 115 + SiFiveEState *s = g_new0(SiFiveEState, 1); 116 + MemoryRegion *sys_mem = get_system_memory(); 117 + MemoryRegion *main_mem = g_new(MemoryRegion, 1); 118 + MemoryRegion *mask_rom = g_new(MemoryRegion, 1); 119 + MemoryRegion *xip_mem = g_new(MemoryRegion, 1); 120 + 121 + /* Initialize SOC */ 122 + object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 123 + object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 124 + &error_abort); 125 + object_property_set_str(OBJECT(&s->soc), SIFIVE_E_CPU, "cpu-type", 126 + &error_abort); 127 + object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 128 + &error_abort); 129 + object_property_set_bool(OBJECT(&s->soc), true, "realized", 130 + &error_abort); 131 + 132 + /* Data Tightly Integrated Memory */ 133 + memory_region_init_ram(main_mem, NULL, "riscv.sifive.e.ram", 134 + memmap[SIFIVE_E_DTIM].size, &error_fatal); 135 + memory_region_add_subregion(sys_mem, 136 + memmap[SIFIVE_E_DTIM].base, main_mem); 137 + 138 + /* Mask ROM */ 139 + memory_region_init_ram(mask_rom, NULL, "riscv.sifive.e.mrom", 140 + memmap[SIFIVE_E_MROM].size, &error_fatal); 141 + memory_region_add_subregion(sys_mem, 142 + memmap[SIFIVE_E_MROM].base, mask_rom); 143 + 144 + /* MMIO */ 145 + s->plic = sifive_plic_create(memmap[SIFIVE_E_PLIC].base, 146 + (char *)SIFIVE_E_PLIC_HART_CONFIG, 147 + SIFIVE_E_PLIC_NUM_SOURCES, 148 + SIFIVE_E_PLIC_NUM_PRIORITIES, 149 + SIFIVE_E_PLIC_PRIORITY_BASE, 150 + SIFIVE_E_PLIC_PENDING_BASE, 151 + SIFIVE_E_PLIC_ENABLE_BASE, 152 + SIFIVE_E_PLIC_ENABLE_STRIDE, 153 + SIFIVE_E_PLIC_CONTEXT_BASE, 154 + SIFIVE_E_PLIC_CONTEXT_STRIDE, 155 + memmap[SIFIVE_E_PLIC].size); 156 + sifive_clint_create(memmap[SIFIVE_E_CLINT].base, 157 + memmap[SIFIVE_E_CLINT].size, smp_cpus, 158 + SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 159 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.aon", 160 + memmap[SIFIVE_E_AON].base, memmap[SIFIVE_E_AON].size); 161 + sifive_prci_create(memmap[SIFIVE_E_PRCI].base); 162 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.gpio0", 163 + memmap[SIFIVE_E_GPIO0].base, memmap[SIFIVE_E_GPIO0].size); 164 + sifive_uart_create(sys_mem, memmap[SIFIVE_E_UART0].base, 165 + serial_hds[0], SIFIVE_PLIC(s->plic)->irqs[SIFIVE_E_UART0_IRQ]); 166 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.qspi0", 167 + memmap[SIFIVE_E_QSPI0].base, memmap[SIFIVE_E_QSPI0].size); 168 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.pwm0", 169 + memmap[SIFIVE_E_PWM0].base, memmap[SIFIVE_E_PWM0].size); 170 + /* sifive_uart_create(sys_mem, memmap[SIFIVE_E_UART1].base, 171 + serial_hds[1], SIFIVE_PLIC(s->plic)->irqs[SIFIVE_E_UART1_IRQ]); */ 172 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.qspi1", 173 + memmap[SIFIVE_E_QSPI1].base, memmap[SIFIVE_E_QSPI1].size); 174 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.pwm1", 175 + memmap[SIFIVE_E_PWM1].base, memmap[SIFIVE_E_PWM1].size); 176 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.qspi2", 177 + memmap[SIFIVE_E_QSPI2].base, memmap[SIFIVE_E_QSPI2].size); 178 + sifive_mmio_emulate(sys_mem, "riscv.sifive.e.pwm2", 179 + memmap[SIFIVE_E_PWM2].base, memmap[SIFIVE_E_PWM2].size); 180 + 181 + /* Flash memory */ 182 + memory_region_init_ram(xip_mem, NULL, "riscv.sifive.e.xip", 183 + memmap[SIFIVE_E_XIP].size, &error_fatal); 184 + memory_region_set_readonly(xip_mem, true); 185 + memory_region_add_subregion(sys_mem, memmap[SIFIVE_E_XIP].base, xip_mem); 186 + 187 + /* Mask ROM reset vector */ 188 + uint32_t reset_vec[2] = { 189 + 0x204002b7, /* 0x1000: lui t0,0x20400 */ 190 + 0x00028067, /* 0x1004: jr t0 */ 191 + }; 192 + 193 + /* copy in the reset vector */ 194 + copy_le32_to_phys(memmap[SIFIVE_E_MROM].base, reset_vec, sizeof(reset_vec)); 195 + memory_region_set_readonly(mask_rom, true); 196 + 197 + if (machine->kernel_filename) { 198 + load_kernel(machine->kernel_filename); 199 + } 200 + } 201 + 202 + static int riscv_sifive_e_sysbus_device_init(SysBusDevice *sysbusdev) 203 + { 204 + return 0; 205 + } 206 + 207 + static void riscv_sifive_e_class_init(ObjectClass *klass, void *data) 208 + { 209 + SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 210 + k->init = riscv_sifive_e_sysbus_device_init; 211 + } 212 + 213 + static const TypeInfo riscv_sifive_e_device = { 214 + .name = TYPE_SIFIVE_E, 215 + .parent = TYPE_SYS_BUS_DEVICE, 216 + .instance_size = sizeof(SiFiveEState), 217 + .class_init = riscv_sifive_e_class_init, 218 + }; 219 + 220 + static void riscv_sifive_e_machine_init(MachineClass *mc) 221 + { 222 + mc->desc = "RISC-V Board compatible with SiFive E SDK"; 223 + mc->init = riscv_sifive_e_init; 224 + mc->max_cpus = 1; 225 + } 226 + 227 + DEFINE_MACHINE("sifive_e", riscv_sifive_e_machine_init) 228 + 229 + static void riscv_sifive_e_register_types(void) 230 + { 231 + type_register_static(&riscv_sifive_e_device); 232 + } 233 + 234 + type_init(riscv_sifive_e_register_types);
+505
hw/riscv/sifive_plic.c
··· 1 + /* 2 + * SiFive PLIC (Platform Level Interrupt Controller) 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This provides a parameterizable interrupt controller based on SiFive's PLIC. 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #include "qemu/osdep.h" 22 + #include "qemu/log.h" 23 + #include "qemu/error-report.h" 24 + #include "hw/sysbus.h" 25 + #include "target/riscv/cpu.h" 26 + #include "hw/riscv/sifive_plic.h" 27 + 28 + #define RISCV_DEBUG_PLIC 0 29 + 30 + static PLICMode char_to_mode(char c) 31 + { 32 + switch (c) { 33 + case 'U': return PLICMode_U; 34 + case 'S': return PLICMode_S; 35 + case 'H': return PLICMode_H; 36 + case 'M': return PLICMode_M; 37 + default: 38 + error_report("plic: invalid mode '%c'", c); 39 + exit(1); 40 + } 41 + } 42 + 43 + static char mode_to_char(PLICMode m) 44 + { 45 + switch (m) { 46 + case PLICMode_U: return 'U'; 47 + case PLICMode_S: return 'S'; 48 + case PLICMode_H: return 'H'; 49 + case PLICMode_M: return 'M'; 50 + default: return '?'; 51 + } 52 + } 53 + 54 + static void sifive_plic_print_state(SiFivePLICState *plic) 55 + { 56 + int i; 57 + int addrid; 58 + 59 + /* pending */ 60 + qemu_log("pending : "); 61 + for (i = plic->bitfield_words - 1; i >= 0; i--) { 62 + qemu_log("%08x", plic->pending[i]); 63 + } 64 + qemu_log("\n"); 65 + 66 + /* pending */ 67 + qemu_log("claimed : "); 68 + for (i = plic->bitfield_words - 1; i >= 0; i--) { 69 + qemu_log("%08x", plic->claimed[i]); 70 + } 71 + qemu_log("\n"); 72 + 73 + for (addrid = 0; addrid < plic->num_addrs; addrid++) { 74 + qemu_log("hart%d-%c enable: ", 75 + plic->addr_config[addrid].hartid, 76 + mode_to_char(plic->addr_config[addrid].mode)); 77 + for (i = plic->bitfield_words - 1; i >= 0; i--) { 78 + qemu_log("%08x", plic->enable[addrid * plic->bitfield_words + i]); 79 + } 80 + qemu_log("\n"); 81 + } 82 + } 83 + 84 + static 85 + void sifive_plic_set_pending(SiFivePLICState *plic, int irq, bool pending) 86 + { 87 + qemu_mutex_lock(&plic->lock); 88 + uint32_t word = irq >> 5; 89 + if (pending) { 90 + plic->pending[word] |= (1 << (irq & 31)); 91 + } else { 92 + plic->pending[word] &= ~(1 << (irq & 31)); 93 + } 94 + qemu_mutex_unlock(&plic->lock); 95 + } 96 + 97 + static 98 + void sifive_plic_set_claimed(SiFivePLICState *plic, int irq, bool claimed) 99 + { 100 + qemu_mutex_lock(&plic->lock); 101 + uint32_t word = irq >> 5; 102 + if (claimed) { 103 + plic->claimed[word] |= (1 << (irq & 31)); 104 + } else { 105 + plic->claimed[word] &= ~(1 << (irq & 31)); 106 + } 107 + qemu_mutex_unlock(&plic->lock); 108 + } 109 + 110 + static 111 + int sifive_plic_num_irqs_pending(SiFivePLICState *plic, uint32_t addrid) 112 + { 113 + int i, j, count = 0; 114 + for (i = 0; i < plic->bitfield_words; i++) { 115 + uint32_t pending_enabled_not_claimed = 116 + (plic->pending[i] & ~plic->claimed[i]) & 117 + plic->enable[addrid * plic->bitfield_words + i]; 118 + if (!pending_enabled_not_claimed) { 119 + continue; 120 + } 121 + for (j = 0; j < 32; j++) { 122 + int irq = (i << 5) + j; 123 + uint32_t prio = plic->source_priority[irq]; 124 + int enabled = pending_enabled_not_claimed & (1 << j); 125 + if (enabled && prio > plic->target_priority[addrid]) { 126 + count++; 127 + } 128 + } 129 + } 130 + return count; 131 + } 132 + 133 + static void sifive_plic_update(SiFivePLICState *plic) 134 + { 135 + int addrid; 136 + 137 + /* raise irq on harts where this irq is enabled */ 138 + for (addrid = 0; addrid < plic->num_addrs; addrid++) { 139 + uint32_t hartid = plic->addr_config[addrid].hartid; 140 + PLICMode mode = plic->addr_config[addrid].mode; 141 + CPUState *cpu = qemu_get_cpu(hartid); 142 + CPURISCVState *env = cpu ? cpu->env_ptr : NULL; 143 + if (!env) { 144 + continue; 145 + } 146 + int level = sifive_plic_num_irqs_pending(plic, addrid) > 0; 147 + switch (mode) { 148 + case PLICMode_M: 149 + riscv_set_local_interrupt(RISCV_CPU(cpu), MIP_MEIP, level); 150 + break; 151 + case PLICMode_S: 152 + riscv_set_local_interrupt(RISCV_CPU(cpu), MIP_SEIP, level); 153 + break; 154 + default: 155 + break; 156 + } 157 + } 158 + 159 + if (RISCV_DEBUG_PLIC) { 160 + sifive_plic_print_state(plic); 161 + } 162 + } 163 + 164 + void sifive_plic_raise_irq(SiFivePLICState *plic, uint32_t irq) 165 + { 166 + sifive_plic_set_pending(plic, irq, true); 167 + sifive_plic_update(plic); 168 + } 169 + 170 + void sifive_plic_lower_irq(SiFivePLICState *plic, uint32_t irq) 171 + { 172 + sifive_plic_set_pending(plic, irq, false); 173 + sifive_plic_update(plic); 174 + } 175 + 176 + static uint32_t sifive_plic_claim(SiFivePLICState *plic, uint32_t addrid) 177 + { 178 + int i, j; 179 + for (i = 0; i < plic->bitfield_words; i++) { 180 + uint32_t pending_enabled_not_claimed = 181 + (plic->pending[i] & ~plic->claimed[i]) & 182 + plic->enable[addrid * plic->bitfield_words + i]; 183 + if (!pending_enabled_not_claimed) { 184 + continue; 185 + } 186 + for (j = 0; j < 32; j++) { 187 + int irq = (i << 5) + j; 188 + uint32_t prio = plic->source_priority[irq]; 189 + int enabled = pending_enabled_not_claimed & (1 << j); 190 + if (enabled && prio > plic->target_priority[addrid]) { 191 + sifive_plic_set_pending(plic, irq, false); 192 + sifive_plic_set_claimed(plic, irq, true); 193 + return irq; 194 + } 195 + } 196 + } 197 + return 0; 198 + } 199 + 200 + static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) 201 + { 202 + SiFivePLICState *plic = opaque; 203 + 204 + /* writes must be 4 byte words */ 205 + if ((addr & 0x3) != 0) { 206 + goto err; 207 + } 208 + 209 + if (addr >= plic->priority_base && /* 4 bytes per source */ 210 + addr < plic->priority_base + (plic->num_sources << 2)) 211 + { 212 + uint32_t irq = (addr - plic->priority_base) >> 2; 213 + if (RISCV_DEBUG_PLIC) { 214 + qemu_log("plic: read priority: irq=%d priority=%d\n", 215 + irq, plic->source_priority[irq]); 216 + } 217 + return plic->source_priority[irq]; 218 + } else if (addr >= plic->pending_base && /* 1 bit per source */ 219 + addr < plic->pending_base + (plic->num_sources >> 3)) 220 + { 221 + uint32_t word = (addr - plic->priority_base) >> 2; 222 + if (RISCV_DEBUG_PLIC) { 223 + qemu_log("plic: read pending: word=%d value=%d\n", 224 + word, plic->pending[word]); 225 + } 226 + return plic->pending[word]; 227 + } else if (addr >= plic->enable_base && /* 1 bit per source */ 228 + addr < plic->enable_base + plic->num_addrs * plic->enable_stride) 229 + { 230 + uint32_t addrid = (addr - plic->enable_base) / plic->enable_stride; 231 + uint32_t wordid = (addr & (plic->enable_stride - 1)) >> 2; 232 + if (wordid < plic->bitfield_words) { 233 + if (RISCV_DEBUG_PLIC) { 234 + qemu_log("plic: read enable: hart%d-%c word=%d value=%x\n", 235 + plic->addr_config[addrid].hartid, 236 + mode_to_char(plic->addr_config[addrid].mode), wordid, 237 + plic->enable[addrid * plic->bitfield_words + wordid]); 238 + } 239 + return plic->enable[addrid * plic->bitfield_words + wordid]; 240 + } 241 + } else if (addr >= plic->context_base && /* 1 bit per source */ 242 + addr < plic->context_base + plic->num_addrs * plic->context_stride) 243 + { 244 + uint32_t addrid = (addr - plic->context_base) / plic->context_stride; 245 + uint32_t contextid = (addr & (plic->context_stride - 1)); 246 + if (contextid == 0) { 247 + if (RISCV_DEBUG_PLIC) { 248 + qemu_log("plic: read priority: hart%d-%c priority=%x\n", 249 + plic->addr_config[addrid].hartid, 250 + mode_to_char(plic->addr_config[addrid].mode), 251 + plic->target_priority[addrid]); 252 + } 253 + return plic->target_priority[addrid]; 254 + } else if (contextid == 4) { 255 + uint32_t value = sifive_plic_claim(plic, addrid); 256 + if (RISCV_DEBUG_PLIC) { 257 + qemu_log("plic: read claim: hart%d-%c irq=%x\n", 258 + plic->addr_config[addrid].hartid, 259 + mode_to_char(plic->addr_config[addrid].mode), 260 + value); 261 + sifive_plic_print_state(plic); 262 + } 263 + return value; 264 + } 265 + } 266 + 267 + err: 268 + error_report("plic: invalid register read: %08x", (uint32_t)addr); 269 + return 0; 270 + } 271 + 272 + static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, 273 + unsigned size) 274 + { 275 + SiFivePLICState *plic = opaque; 276 + 277 + /* writes must be 4 byte words */ 278 + if ((addr & 0x3) != 0) { 279 + goto err; 280 + } 281 + 282 + if (addr >= plic->priority_base && /* 4 bytes per source */ 283 + addr < plic->priority_base + (plic->num_sources << 2)) 284 + { 285 + uint32_t irq = (addr - plic->priority_base) >> 2; 286 + plic->source_priority[irq] = value & 7; 287 + if (RISCV_DEBUG_PLIC) { 288 + qemu_log("plic: write priority: irq=%d priority=%d\n", 289 + irq, plic->source_priority[irq]); 290 + } 291 + return; 292 + } else if (addr >= plic->pending_base && /* 1 bit per source */ 293 + addr < plic->pending_base + (plic->num_sources >> 3)) 294 + { 295 + error_report("plic: invalid pending write: %08x", (uint32_t)addr); 296 + return; 297 + } else if (addr >= plic->enable_base && /* 1 bit per source */ 298 + addr < plic->enable_base + plic->num_addrs * plic->enable_stride) 299 + { 300 + uint32_t addrid = (addr - plic->enable_base) / plic->enable_stride; 301 + uint32_t wordid = (addr & (plic->enable_stride - 1)) >> 2; 302 + if (wordid < plic->bitfield_words) { 303 + plic->enable[addrid * plic->bitfield_words + wordid] = value; 304 + if (RISCV_DEBUG_PLIC) { 305 + qemu_log("plic: write enable: hart%d-%c word=%d value=%x\n", 306 + plic->addr_config[addrid].hartid, 307 + mode_to_char(plic->addr_config[addrid].mode), wordid, 308 + plic->enable[addrid * plic->bitfield_words + wordid]); 309 + } 310 + return; 311 + } 312 + } else if (addr >= plic->context_base && /* 4 bytes per reg */ 313 + addr < plic->context_base + plic->num_addrs * plic->context_stride) 314 + { 315 + uint32_t addrid = (addr - plic->context_base) / plic->context_stride; 316 + uint32_t contextid = (addr & (plic->context_stride - 1)); 317 + if (contextid == 0) { 318 + if (RISCV_DEBUG_PLIC) { 319 + qemu_log("plic: write priority: hart%d-%c priority=%x\n", 320 + plic->addr_config[addrid].hartid, 321 + mode_to_char(plic->addr_config[addrid].mode), 322 + plic->target_priority[addrid]); 323 + } 324 + if (value <= plic->num_priorities) { 325 + plic->target_priority[addrid] = value; 326 + sifive_plic_update(plic); 327 + } 328 + return; 329 + } else if (contextid == 4) { 330 + if (RISCV_DEBUG_PLIC) { 331 + qemu_log("plic: write claim: hart%d-%c irq=%x\n", 332 + plic->addr_config[addrid].hartid, 333 + mode_to_char(plic->addr_config[addrid].mode), 334 + (uint32_t)value); 335 + } 336 + if (value < plic->num_sources) { 337 + sifive_plic_set_claimed(plic, value, false); 338 + sifive_plic_update(plic); 339 + } 340 + return; 341 + } 342 + } 343 + 344 + err: 345 + error_report("plic: invalid register write: %08x", (uint32_t)addr); 346 + } 347 + 348 + static const MemoryRegionOps sifive_plic_ops = { 349 + .read = sifive_plic_read, 350 + .write = sifive_plic_write, 351 + .endianness = DEVICE_LITTLE_ENDIAN, 352 + .valid = { 353 + .min_access_size = 4, 354 + .max_access_size = 4 355 + } 356 + }; 357 + 358 + static Property sifive_plic_properties[] = { 359 + DEFINE_PROP_STRING("hart-config", SiFivePLICState, hart_config), 360 + DEFINE_PROP_UINT32("num-sources", SiFivePLICState, num_sources, 0), 361 + DEFINE_PROP_UINT32("num-priorities", SiFivePLICState, num_priorities, 0), 362 + DEFINE_PROP_UINT32("priority-base", SiFivePLICState, priority_base, 0), 363 + DEFINE_PROP_UINT32("pending-base", SiFivePLICState, pending_base, 0), 364 + DEFINE_PROP_UINT32("enable-base", SiFivePLICState, enable_base, 0), 365 + DEFINE_PROP_UINT32("enable-stride", SiFivePLICState, enable_stride, 0), 366 + DEFINE_PROP_UINT32("context-base", SiFivePLICState, context_base, 0), 367 + DEFINE_PROP_UINT32("context-stride", SiFivePLICState, context_stride, 0), 368 + DEFINE_PROP_UINT32("aperture-size", SiFivePLICState, aperture_size, 0), 369 + DEFINE_PROP_END_OF_LIST(), 370 + }; 371 + 372 + /* 373 + * parse PLIC hart/mode address offset config 374 + * 375 + * "M" 1 hart with M mode 376 + * "MS,MS" 2 harts, 0-1 with M and S mode 377 + * "M,MS,MS,MS,MS" 5 harts, 0 with M mode, 1-5 with M and S mode 378 + */ 379 + static void parse_hart_config(SiFivePLICState *plic) 380 + { 381 + int addrid, hartid, modes; 382 + const char *p; 383 + char c; 384 + 385 + /* count and validate hart/mode combinations */ 386 + addrid = 0, hartid = 0, modes = 0; 387 + p = plic->hart_config; 388 + while ((c = *p++)) { 389 + if (c == ',') { 390 + addrid += __builtin_popcount(modes); 391 + modes = 0; 392 + hartid++; 393 + } else { 394 + int m = 1 << char_to_mode(c); 395 + if (modes == (modes | m)) { 396 + error_report("plic: duplicate mode '%c' in config: %s", 397 + c, plic->hart_config); 398 + exit(1); 399 + } 400 + modes |= m; 401 + } 402 + } 403 + if (modes) { 404 + addrid += __builtin_popcount(modes); 405 + } 406 + hartid++; 407 + 408 + /* store hart/mode combinations */ 409 + plic->num_addrs = addrid; 410 + plic->addr_config = g_new(PLICAddr, plic->num_addrs); 411 + addrid = 0, hartid = 0; 412 + p = plic->hart_config; 413 + while ((c = *p++)) { 414 + if (c == ',') { 415 + hartid++; 416 + } else { 417 + plic->addr_config[addrid].addrid = addrid; 418 + plic->addr_config[addrid].hartid = hartid; 419 + plic->addr_config[addrid].mode = char_to_mode(c); 420 + addrid++; 421 + } 422 + } 423 + } 424 + 425 + static void sifive_plic_irq_request(void *opaque, int irq, int level) 426 + { 427 + SiFivePLICState *plic = opaque; 428 + if (RISCV_DEBUG_PLIC) { 429 + qemu_log("sifive_plic_irq_request: irq=%d level=%d\n", irq, level); 430 + } 431 + sifive_plic_set_pending(plic, irq, level > 0); 432 + sifive_plic_update(plic); 433 + } 434 + 435 + static void sifive_plic_realize(DeviceState *dev, Error **errp) 436 + { 437 + SiFivePLICState *plic = SIFIVE_PLIC(dev); 438 + int i; 439 + 440 + memory_region_init_io(&plic->mmio, OBJECT(dev), &sifive_plic_ops, plic, 441 + TYPE_SIFIVE_PLIC, plic->aperture_size); 442 + parse_hart_config(plic); 443 + qemu_mutex_init(&plic->lock); 444 + plic->bitfield_words = (plic->num_sources + 31) >> 5; 445 + plic->source_priority = g_new0(uint32_t, plic->num_sources); 446 + plic->target_priority = g_new(uint32_t, plic->num_addrs); 447 + plic->pending = g_new0(uint32_t, plic->bitfield_words); 448 + plic->claimed = g_new0(uint32_t, plic->bitfield_words); 449 + plic->enable = g_new0(uint32_t, plic->bitfield_words * plic->num_addrs); 450 + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &plic->mmio); 451 + plic->irqs = g_new0(qemu_irq, plic->num_sources + 1); 452 + for (i = 0; i <= plic->num_sources; i++) { 453 + plic->irqs[i] = qemu_allocate_irq(sifive_plic_irq_request, plic, i); 454 + } 455 + } 456 + 457 + static void sifive_plic_class_init(ObjectClass *klass, void *data) 458 + { 459 + DeviceClass *dc = DEVICE_CLASS(klass); 460 + 461 + dc->props = sifive_plic_properties; 462 + dc->realize = sifive_plic_realize; 463 + } 464 + 465 + static const TypeInfo sifive_plic_info = { 466 + .name = TYPE_SIFIVE_PLIC, 467 + .parent = TYPE_SYS_BUS_DEVICE, 468 + .instance_size = sizeof(SiFivePLICState), 469 + .class_init = sifive_plic_class_init, 470 + }; 471 + 472 + static void sifive_plic_register_types(void) 473 + { 474 + type_register_static(&sifive_plic_info); 475 + } 476 + 477 + type_init(sifive_plic_register_types) 478 + 479 + /* 480 + * Create PLIC device. 481 + */ 482 + DeviceState *sifive_plic_create(hwaddr addr, char *hart_config, 483 + uint32_t num_sources, uint32_t num_priorities, 484 + uint32_t priority_base, uint32_t pending_base, 485 + uint32_t enable_base, uint32_t enable_stride, 486 + uint32_t context_base, uint32_t context_stride, 487 + uint32_t aperture_size) 488 + { 489 + DeviceState *dev = qdev_create(NULL, TYPE_SIFIVE_PLIC); 490 + assert(enable_stride == (enable_stride & -enable_stride)); 491 + assert(context_stride == (context_stride & -context_stride)); 492 + qdev_prop_set_string(dev, "hart-config", hart_config); 493 + qdev_prop_set_uint32(dev, "num-sources", num_sources); 494 + qdev_prop_set_uint32(dev, "num-priorities", num_priorities); 495 + qdev_prop_set_uint32(dev, "priority-base", priority_base); 496 + qdev_prop_set_uint32(dev, "pending-base", pending_base); 497 + qdev_prop_set_uint32(dev, "enable-base", enable_base); 498 + qdev_prop_set_uint32(dev, "enable-stride", enable_stride); 499 + qdev_prop_set_uint32(dev, "context-base", context_base); 500 + qdev_prop_set_uint32(dev, "context-stride", context_stride); 501 + qdev_prop_set_uint32(dev, "aperture-size", aperture_size); 502 + qdev_init_nofail(dev); 503 + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); 504 + return dev; 505 + }
+89
hw/riscv/sifive_prci.c
··· 1 + /* 2 + * QEMU SiFive PRCI (Power, Reset, Clock, Interrupt) 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * Simple model of the PRCI to emulate register reads made by the SDK BSP 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #include "qemu/osdep.h" 22 + #include "hw/sysbus.h" 23 + #include "target/riscv/cpu.h" 24 + #include "hw/riscv/sifive_prci.h" 25 + 26 + /* currently implements enough to mock freedom-e-sdk BSP clock programming */ 27 + 28 + static uint64_t sifive_prci_read(void *opaque, hwaddr addr, unsigned int size) 29 + { 30 + if (addr == 0 /* PRCI_HFROSCCFG */) { 31 + return 1 << 31; /* ROSC_RDY */ 32 + } 33 + if (addr == 8 /* PRCI_PLLCFG */) { 34 + return 1 << 31; /* PLL_LOCK */ 35 + } 36 + hw_error("%s: read: addr=0x%x\n", __func__, (int)addr); 37 + return 0; 38 + } 39 + 40 + static void sifive_prci_write(void *opaque, hwaddr addr, 41 + uint64_t val64, unsigned int size) 42 + { 43 + /* discard writes */ 44 + } 45 + 46 + static const MemoryRegionOps sifive_prci_ops = { 47 + .read = sifive_prci_read, 48 + .write = sifive_prci_write, 49 + .endianness = DEVICE_NATIVE_ENDIAN, 50 + .valid = { 51 + .min_access_size = 4, 52 + .max_access_size = 4 53 + } 54 + }; 55 + 56 + static void sifive_prci_init(Object *obj) 57 + { 58 + SiFivePRCIState *s = SIFIVE_PRCI(obj); 59 + 60 + memory_region_init_io(&s->mmio, obj, &sifive_prci_ops, s, 61 + TYPE_SIFIVE_PRCI, 0x8000); 62 + sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); 63 + } 64 + 65 + static const TypeInfo sifive_prci_info = { 66 + .name = TYPE_SIFIVE_PRCI, 67 + .parent = TYPE_SYS_BUS_DEVICE, 68 + .instance_size = sizeof(SiFivePRCIState), 69 + .instance_init = sifive_prci_init, 70 + }; 71 + 72 + static void sifive_prci_register_types(void) 73 + { 74 + type_register_static(&sifive_prci_info); 75 + } 76 + 77 + type_init(sifive_prci_register_types) 78 + 79 + 80 + /* 81 + * Create PRCI device. 82 + */ 83 + DeviceState *sifive_prci_create(hwaddr addr) 84 + { 85 + DeviceState *dev = qdev_create(NULL, TYPE_SIFIVE_PRCI); 86 + qdev_init_nofail(dev); 87 + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); 88 + return dev; 89 + }
+93
hw/riscv/sifive_test.c
··· 1 + /* 2 + * QEMU SiFive Test Finisher 3 + * 4 + * Copyright (c) 2018 SiFive, Inc. 5 + * 6 + * Test finisher memory mapped device used to exit simulation 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #include "qemu/osdep.h" 22 + #include "hw/sysbus.h" 23 + #include "target/riscv/cpu.h" 24 + #include "hw/riscv/sifive_test.h" 25 + 26 + static uint64_t sifive_test_read(void *opaque, hwaddr addr, unsigned int size) 27 + { 28 + return 0; 29 + } 30 + 31 + static void sifive_test_write(void *opaque, hwaddr addr, 32 + uint64_t val64, unsigned int size) 33 + { 34 + if (addr == 0) { 35 + int status = val64 & 0xffff; 36 + int code = (val64 >> 16) & 0xffff; 37 + switch (status) { 38 + case FINISHER_FAIL: 39 + exit(code); 40 + case FINISHER_PASS: 41 + exit(0); 42 + default: 43 + break; 44 + } 45 + } 46 + hw_error("%s: write: addr=0x%x val=0x%016" PRIx64 "\n", 47 + __func__, (int)addr, val64); 48 + } 49 + 50 + static const MemoryRegionOps sifive_test_ops = { 51 + .read = sifive_test_read, 52 + .write = sifive_test_write, 53 + .endianness = DEVICE_NATIVE_ENDIAN, 54 + .valid = { 55 + .min_access_size = 4, 56 + .max_access_size = 4 57 + } 58 + }; 59 + 60 + static void sifive_test_init(Object *obj) 61 + { 62 + SiFiveTestState *s = SIFIVE_TEST(obj); 63 + 64 + memory_region_init_io(&s->mmio, obj, &sifive_test_ops, s, 65 + TYPE_SIFIVE_TEST, 0x1000); 66 + sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); 67 + } 68 + 69 + static const TypeInfo sifive_test_info = { 70 + .name = TYPE_SIFIVE_TEST, 71 + .parent = TYPE_SYS_BUS_DEVICE, 72 + .instance_size = sizeof(SiFiveTestState), 73 + .instance_init = sifive_test_init, 74 + }; 75 + 76 + static void sifive_test_register_types(void) 77 + { 78 + type_register_static(&sifive_test_info); 79 + } 80 + 81 + type_init(sifive_test_register_types) 82 + 83 + 84 + /* 85 + * Create Test device. 86 + */ 87 + DeviceState *sifive_test_create(hwaddr addr) 88 + { 89 + DeviceState *dev = qdev_create(NULL, TYPE_SIFIVE_TEST); 90 + qdev_init_nofail(dev); 91 + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); 92 + return dev; 93 + }
+339
hw/riscv/sifive_u.c
··· 1 + /* 2 + * QEMU RISC-V Board Compatible with SiFive Freedom U SDK 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017 SiFive, Inc. 6 + * 7 + * Provides a board compatible with the SiFive Freedom U SDK: 8 + * 9 + * 0) UART 10 + * 1) CLINT (Core Level Interruptor) 11 + * 2) PLIC (Platform Level Interrupt Controller) 12 + * 13 + * This board currently uses a hardcoded devicetree that indicates one hart. 14 + * 15 + * This program is free software; you can redistribute it and/or modify it 16 + * under the terms and conditions of the GNU General Public License, 17 + * version 2 or later, as published by the Free Software Foundation. 18 + * 19 + * This program is distributed in the hope it will be useful, but WITHOUT 20 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 21 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 22 + * more details. 23 + * 24 + * You should have received a copy of the GNU General Public License along with 25 + * this program. If not, see <http://www.gnu.org/licenses/>. 26 + */ 27 + 28 + #include "qemu/osdep.h" 29 + #include "qemu/log.h" 30 + #include "qemu/error-report.h" 31 + #include "qapi/error.h" 32 + #include "hw/hw.h" 33 + #include "hw/boards.h" 34 + #include "hw/loader.h" 35 + #include "hw/sysbus.h" 36 + #include "hw/char/serial.h" 37 + #include "target/riscv/cpu.h" 38 + #include "hw/riscv/riscv_hart.h" 39 + #include "hw/riscv/sifive_plic.h" 40 + #include "hw/riscv/sifive_clint.h" 41 + #include "hw/riscv/sifive_uart.h" 42 + #include "hw/riscv/sifive_prci.h" 43 + #include "hw/riscv/sifive_u.h" 44 + #include "chardev/char.h" 45 + #include "sysemu/arch_init.h" 46 + #include "sysemu/device_tree.h" 47 + #include "exec/address-spaces.h" 48 + #include "elf.h" 49 + 50 + static const struct MemmapEntry { 51 + hwaddr base; 52 + hwaddr size; 53 + } sifive_u_memmap[] = { 54 + [SIFIVE_U_DEBUG] = { 0x0, 0x100 }, 55 + [SIFIVE_U_MROM] = { 0x1000, 0x2000 }, 56 + [SIFIVE_U_CLINT] = { 0x2000000, 0x10000 }, 57 + [SIFIVE_U_PLIC] = { 0xc000000, 0x4000000 }, 58 + [SIFIVE_U_UART0] = { 0x10013000, 0x1000 }, 59 + [SIFIVE_U_UART1] = { 0x10023000, 0x1000 }, 60 + [SIFIVE_U_DRAM] = { 0x80000000, 0x0 }, 61 + }; 62 + 63 + static void copy_le32_to_phys(hwaddr pa, uint32_t *rom, size_t len) 64 + { 65 + int i; 66 + for (i = 0; i < (len >> 2); i++) { 67 + stl_phys(&address_space_memory, pa + (i << 2), rom[i]); 68 + } 69 + } 70 + 71 + static uint64_t identity_translate(void *opaque, uint64_t addr) 72 + { 73 + return addr; 74 + } 75 + 76 + static uint64_t load_kernel(const char *kernel_filename) 77 + { 78 + uint64_t kernel_entry, kernel_high; 79 + 80 + if (load_elf(kernel_filename, identity_translate, NULL, 81 + &kernel_entry, NULL, &kernel_high, 82 + 0, ELF_MACHINE, 1, 0) < 0) { 83 + error_report("qemu: could not load kernel '%s'", kernel_filename); 84 + exit(1); 85 + } 86 + return kernel_entry; 87 + } 88 + 89 + static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap, 90 + uint64_t mem_size, const char *cmdline) 91 + { 92 + void *fdt; 93 + int cpu; 94 + uint32_t *cells; 95 + char *nodename; 96 + uint32_t plic_phandle; 97 + 98 + fdt = s->fdt = create_device_tree(&s->fdt_size); 99 + if (!fdt) { 100 + error_report("create_device_tree() failed"); 101 + exit(1); 102 + } 103 + 104 + qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu"); 105 + qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev"); 106 + qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 107 + qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 108 + 109 + qemu_fdt_add_subnode(fdt, "/soc"); 110 + qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 111 + qemu_fdt_setprop_string(fdt, "/soc", "compatible", "ucbbar,spike-bare-soc"); 112 + qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 113 + qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 114 + 115 + nodename = g_strdup_printf("/memory@%lx", 116 + (long)memmap[SIFIVE_U_DRAM].base); 117 + qemu_fdt_add_subnode(fdt, nodename); 118 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 119 + memmap[SIFIVE_U_DRAM].base >> 32, memmap[SIFIVE_U_DRAM].base, 120 + mem_size >> 32, mem_size); 121 + qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 122 + g_free(nodename); 123 + 124 + qemu_fdt_add_subnode(fdt, "/cpus"); 125 + qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 10000000); 126 + qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 127 + qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 128 + 129 + for (cpu = s->soc.num_harts - 1; cpu >= 0; cpu--) { 130 + nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 131 + char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 132 + char *isa = riscv_isa_string(&s->soc.harts[cpu]); 133 + qemu_fdt_add_subnode(fdt, nodename); 134 + qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 1000000000); 135 + qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 136 + qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 137 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 138 + qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 139 + qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 140 + qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 141 + qemu_fdt_add_subnode(fdt, intc); 142 + qemu_fdt_setprop_cell(fdt, intc, "phandle", 1); 143 + qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", 1); 144 + qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 145 + qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 146 + qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 147 + g_free(isa); 148 + g_free(intc); 149 + g_free(nodename); 150 + } 151 + 152 + cells = g_new0(uint32_t, s->soc.num_harts * 4); 153 + for (cpu = 0; cpu < s->soc.num_harts; cpu++) { 154 + nodename = 155 + g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 156 + uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 157 + cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 158 + cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 159 + cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 160 + cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 161 + g_free(nodename); 162 + } 163 + nodename = g_strdup_printf("/soc/clint@%lx", 164 + (long)memmap[SIFIVE_U_CLINT].base); 165 + qemu_fdt_add_subnode(fdt, nodename); 166 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 167 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 168 + 0x0, memmap[SIFIVE_U_CLINT].base, 169 + 0x0, memmap[SIFIVE_U_CLINT].size); 170 + qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 171 + cells, s->soc.num_harts * sizeof(uint32_t) * 4); 172 + g_free(cells); 173 + g_free(nodename); 174 + 175 + cells = g_new0(uint32_t, s->soc.num_harts * 4); 176 + for (cpu = 0; cpu < s->soc.num_harts; cpu++) { 177 + nodename = 178 + g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 179 + uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 180 + cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 181 + cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT); 182 + cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 183 + cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT); 184 + g_free(nodename); 185 + } 186 + nodename = g_strdup_printf("/soc/interrupt-controller@%lx", 187 + (long)memmap[SIFIVE_U_PLIC].base); 188 + qemu_fdt_add_subnode(fdt, nodename); 189 + qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); 190 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); 191 + qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 192 + qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 193 + cells, s->soc.num_harts * sizeof(uint32_t) * 4); 194 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 195 + 0x0, memmap[SIFIVE_U_PLIC].base, 196 + 0x0, memmap[SIFIVE_U_PLIC].size); 197 + qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 198 + qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7); 199 + qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 4); 200 + qemu_fdt_setprop_cells(fdt, nodename, "phandle", 2); 201 + qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", 2); 202 + plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 203 + g_free(cells); 204 + g_free(nodename); 205 + 206 + nodename = g_strdup_printf("/uart@%lx", 207 + (long)memmap[SIFIVE_U_UART0].base); 208 + qemu_fdt_add_subnode(fdt, nodename); 209 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); 210 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 211 + 0x0, memmap[SIFIVE_U_UART0].base, 212 + 0x0, memmap[SIFIVE_U_UART0].size); 213 + qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 214 + qemu_fdt_setprop_cells(fdt, nodename, "interrupts", 1); 215 + 216 + qemu_fdt_add_subnode(fdt, "/chosen"); 217 + qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 218 + qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 219 + g_free(nodename); 220 + } 221 + 222 + static void riscv_sifive_u_init(MachineState *machine) 223 + { 224 + const struct MemmapEntry *memmap = sifive_u_memmap; 225 + 226 + SiFiveUState *s = g_new0(SiFiveUState, 1); 227 + MemoryRegion *sys_memory = get_system_memory(); 228 + MemoryRegion *main_mem = g_new(MemoryRegion, 1); 229 + MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 230 + 231 + /* Initialize SOC */ 232 + object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 233 + object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 234 + &error_abort); 235 + object_property_set_str(OBJECT(&s->soc), SIFIVE_U_CPU, "cpu-type", 236 + &error_abort); 237 + object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 238 + &error_abort); 239 + object_property_set_bool(OBJECT(&s->soc), true, "realized", 240 + &error_abort); 241 + 242 + /* register RAM */ 243 + memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", 244 + machine->ram_size, &error_fatal); 245 + memory_region_add_subregion(sys_memory, memmap[SIFIVE_U_DRAM].base, 246 + main_mem); 247 + 248 + /* create device tree */ 249 + create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 250 + 251 + /* boot rom */ 252 + memory_region_init_ram(boot_rom, NULL, "riscv.sifive.u.mrom", 253 + memmap[SIFIVE_U_MROM].base, &error_fatal); 254 + memory_region_set_readonly(boot_rom, true); 255 + memory_region_add_subregion(sys_memory, 0x0, boot_rom); 256 + 257 + if (machine->kernel_filename) { 258 + load_kernel(machine->kernel_filename); 259 + } 260 + 261 + /* reset vector */ 262 + uint32_t reset_vec[8] = { 263 + 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 264 + 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 265 + 0xf1402573, /* csrr a0, mhartid */ 266 + #if defined(TARGET_RISCV32) 267 + 0x0182a283, /* lw t0, 24(t0) */ 268 + #elif defined(TARGET_RISCV64) 269 + 0x0182b283, /* ld t0, 24(t0) */ 270 + #endif 271 + 0x00028067, /* jr t0 */ 272 + 0x00000000, 273 + memmap[SIFIVE_U_DRAM].base, /* start: .dword DRAM_BASE */ 274 + 0x00000000, 275 + /* dtb: */ 276 + }; 277 + 278 + /* copy in the reset vector */ 279 + copy_le32_to_phys(memmap[SIFIVE_U_MROM].base, reset_vec, sizeof(reset_vec)); 280 + 281 + /* copy in the device tree */ 282 + qemu_fdt_dumpdtb(s->fdt, s->fdt_size); 283 + cpu_physical_memory_write(memmap[SIFIVE_U_MROM].base + 284 + sizeof(reset_vec), s->fdt, s->fdt_size); 285 + 286 + /* MMIO */ 287 + s->plic = sifive_plic_create(memmap[SIFIVE_U_PLIC].base, 288 + (char *)SIFIVE_U_PLIC_HART_CONFIG, 289 + SIFIVE_U_PLIC_NUM_SOURCES, 290 + SIFIVE_U_PLIC_NUM_PRIORITIES, 291 + SIFIVE_U_PLIC_PRIORITY_BASE, 292 + SIFIVE_U_PLIC_PENDING_BASE, 293 + SIFIVE_U_PLIC_ENABLE_BASE, 294 + SIFIVE_U_PLIC_ENABLE_STRIDE, 295 + SIFIVE_U_PLIC_CONTEXT_BASE, 296 + SIFIVE_U_PLIC_CONTEXT_STRIDE, 297 + memmap[SIFIVE_U_PLIC].size); 298 + sifive_uart_create(sys_memory, memmap[SIFIVE_U_UART0].base, 299 + serial_hds[0], SIFIVE_PLIC(s->plic)->irqs[SIFIVE_U_UART0_IRQ]); 300 + /* sifive_uart_create(sys_memory, memmap[SIFIVE_U_UART1].base, 301 + serial_hds[1], SIFIVE_PLIC(s->plic)->irqs[SIFIVE_U_UART1_IRQ]); */ 302 + sifive_clint_create(memmap[SIFIVE_U_CLINT].base, 303 + memmap[SIFIVE_U_CLINT].size, smp_cpus, 304 + SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 305 + } 306 + 307 + static int riscv_sifive_u_sysbus_device_init(SysBusDevice *sysbusdev) 308 + { 309 + return 0; 310 + } 311 + 312 + static void riscv_sifive_u_class_init(ObjectClass *klass, void *data) 313 + { 314 + SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 315 + k->init = riscv_sifive_u_sysbus_device_init; 316 + } 317 + 318 + static const TypeInfo riscv_sifive_u_device = { 319 + .name = TYPE_SIFIVE_U, 320 + .parent = TYPE_SYS_BUS_DEVICE, 321 + .instance_size = sizeof(SiFiveUState), 322 + .class_init = riscv_sifive_u_class_init, 323 + }; 324 + 325 + static void riscv_sifive_u_register_types(void) 326 + { 327 + type_register_static(&riscv_sifive_u_device); 328 + } 329 + 330 + type_init(riscv_sifive_u_register_types); 331 + 332 + static void riscv_sifive_u_machine_init(MachineClass *mc) 333 + { 334 + mc->desc = "RISC-V Board compatible with SiFive U SDK"; 335 + mc->init = riscv_sifive_u_init; 336 + mc->max_cpus = 1; 337 + } 338 + 339 + DEFINE_MACHINE("sifive_u", riscv_sifive_u_machine_init)
+176
hw/riscv/sifive_uart.c
··· 1 + /* 2 + * QEMU model of the UART on the SiFive E300 and U500 series SOCs. 3 + * 4 + * Copyright (c) 2016 Stefan O'Rear 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #include "qemu/osdep.h" 20 + #include "qapi/error.h" 21 + #include "hw/sysbus.h" 22 + #include "chardev/char.h" 23 + #include "chardev/char-fe.h" 24 + #include "target/riscv/cpu.h" 25 + #include "hw/riscv/sifive_uart.h" 26 + 27 + /* 28 + * Not yet implemented: 29 + * 30 + * Transmit FIFO using "qemu/fifo8.h" 31 + * SIFIVE_UART_IE_TXWM interrupts 32 + * SIFIVE_UART_IE_RXWM interrupts must honor fifo watermark 33 + * Rx FIFO watermark interrupt trigger threshold 34 + * Tx FIFO watermark interrupt trigger threshold. 35 + */ 36 + 37 + static void update_irq(SiFiveUARTState *s) 38 + { 39 + int cond = 0; 40 + if ((s->ie & SIFIVE_UART_IE_RXWM) && s->rx_fifo_len) { 41 + cond = 1; 42 + } 43 + if (cond) { 44 + qemu_irq_raise(s->irq); 45 + } else { 46 + qemu_irq_lower(s->irq); 47 + } 48 + } 49 + 50 + static uint64_t 51 + uart_read(void *opaque, hwaddr addr, unsigned int size) 52 + { 53 + SiFiveUARTState *s = opaque; 54 + unsigned char r; 55 + switch (addr) { 56 + case SIFIVE_UART_RXFIFO: 57 + if (s->rx_fifo_len) { 58 + r = s->rx_fifo[0]; 59 + memmove(s->rx_fifo, s->rx_fifo + 1, s->rx_fifo_len - 1); 60 + s->rx_fifo_len--; 61 + qemu_chr_fe_accept_input(&s->chr); 62 + update_irq(s); 63 + return r; 64 + } 65 + return 0x80000000; 66 + 67 + case SIFIVE_UART_TXFIFO: 68 + return 0; /* Should check tx fifo */ 69 + case SIFIVE_UART_IE: 70 + return s->ie; 71 + case SIFIVE_UART_IP: 72 + return s->rx_fifo_len ? SIFIVE_UART_IP_RXWM : 0; 73 + case SIFIVE_UART_TXCTRL: 74 + return s->txctrl; 75 + case SIFIVE_UART_RXCTRL: 76 + return s->rxctrl; 77 + case SIFIVE_UART_DIV: 78 + return s->div; 79 + } 80 + 81 + hw_error("%s: bad read: addr=0x%x\n", 82 + __func__, (int)addr); 83 + return 0; 84 + } 85 + 86 + static void 87 + uart_write(void *opaque, hwaddr addr, 88 + uint64_t val64, unsigned int size) 89 + { 90 + SiFiveUARTState *s = opaque; 91 + uint32_t value = val64; 92 + unsigned char ch = value; 93 + 94 + switch (addr) { 95 + case SIFIVE_UART_TXFIFO: 96 + qemu_chr_fe_write(&s->chr, &ch, 1); 97 + return; 98 + case SIFIVE_UART_IE: 99 + s->ie = val64; 100 + update_irq(s); 101 + return; 102 + case SIFIVE_UART_TXCTRL: 103 + s->txctrl = val64; 104 + return; 105 + case SIFIVE_UART_RXCTRL: 106 + s->rxctrl = val64; 107 + return; 108 + case SIFIVE_UART_DIV: 109 + s->div = val64; 110 + return; 111 + } 112 + hw_error("%s: bad write: addr=0x%x v=0x%x\n", 113 + __func__, (int)addr, (int)value); 114 + } 115 + 116 + static const MemoryRegionOps uart_ops = { 117 + .read = uart_read, 118 + .write = uart_write, 119 + .endianness = DEVICE_NATIVE_ENDIAN, 120 + .valid = { 121 + .min_access_size = 4, 122 + .max_access_size = 4 123 + } 124 + }; 125 + 126 + static void uart_rx(void *opaque, const uint8_t *buf, int size) 127 + { 128 + SiFiveUARTState *s = opaque; 129 + 130 + /* Got a byte. */ 131 + if (s->rx_fifo_len >= sizeof(s->rx_fifo)) { 132 + printf("WARNING: UART dropped char.\n"); 133 + return; 134 + } 135 + s->rx_fifo[s->rx_fifo_len++] = *buf; 136 + 137 + update_irq(s); 138 + } 139 + 140 + static int uart_can_rx(void *opaque) 141 + { 142 + SiFiveUARTState *s = opaque; 143 + 144 + return s->rx_fifo_len < sizeof(s->rx_fifo); 145 + } 146 + 147 + static void uart_event(void *opaque, int event) 148 + { 149 + } 150 + 151 + static int uart_be_change(void *opaque) 152 + { 153 + SiFiveUARTState *s = opaque; 154 + 155 + qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx, uart_event, 156 + uart_be_change, s, NULL, true); 157 + 158 + return 0; 159 + } 160 + 161 + /* 162 + * Create UART device. 163 + */ 164 + SiFiveUARTState *sifive_uart_create(MemoryRegion *address_space, hwaddr base, 165 + Chardev *chr, qemu_irq irq) 166 + { 167 + SiFiveUARTState *s = g_malloc0(sizeof(SiFiveUARTState)); 168 + s->irq = irq; 169 + qemu_chr_fe_init(&s->chr, chr, &error_abort); 170 + qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx, uart_event, 171 + uart_be_change, s, NULL, true); 172 + memory_region_init_io(&s->mmio, NULL, &uart_ops, s, 173 + TYPE_SIFIVE_UART, SIFIVE_UART_MAX); 174 + memory_region_add_subregion(address_space, base, &s->mmio); 175 + return s; 176 + }
+376
hw/riscv/spike.c
··· 1 + /* 2 + * QEMU RISC-V Spike Board 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This provides a RISC-V Board with the following devices: 8 + * 9 + * 0) HTIF Console and Poweroff 10 + * 1) CLINT (Timer and IPI) 11 + * 2) PLIC (Platform Level Interrupt Controller) 12 + * 13 + * This program is free software; you can redistribute it and/or modify it 14 + * under the terms and conditions of the GNU General Public License, 15 + * version 2 or later, as published by the Free Software Foundation. 16 + * 17 + * This program is distributed in the hope it will be useful, but WITHOUT 18 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 20 + * more details. 21 + * 22 + * You should have received a copy of the GNU General Public License along with 23 + * this program. If not, see <http://www.gnu.org/licenses/>. 24 + */ 25 + 26 + #include "qemu/osdep.h" 27 + #include "qemu/log.h" 28 + #include "qemu/error-report.h" 29 + #include "qapi/error.h" 30 + #include "hw/hw.h" 31 + #include "hw/boards.h" 32 + #include "hw/loader.h" 33 + #include "hw/sysbus.h" 34 + #include "target/riscv/cpu.h" 35 + #include "hw/riscv/riscv_htif.h" 36 + #include "hw/riscv/riscv_hart.h" 37 + #include "hw/riscv/sifive_clint.h" 38 + #include "hw/riscv/spike.h" 39 + #include "chardev/char.h" 40 + #include "sysemu/arch_init.h" 41 + #include "sysemu/device_tree.h" 42 + #include "exec/address-spaces.h" 43 + #include "elf.h" 44 + 45 + static const struct MemmapEntry { 46 + hwaddr base; 47 + hwaddr size; 48 + } spike_memmap[] = { 49 + [SPIKE_MROM] = { 0x1000, 0x2000 }, 50 + [SPIKE_CLINT] = { 0x2000000, 0x10000 }, 51 + [SPIKE_DRAM] = { 0x80000000, 0x0 }, 52 + }; 53 + 54 + static void copy_le32_to_phys(hwaddr pa, uint32_t *rom, size_t len) 55 + { 56 + int i; 57 + for (i = 0; i < (len >> 2); i++) { 58 + stl_phys(&address_space_memory, pa + (i << 2), rom[i]); 59 + } 60 + } 61 + 62 + static uint64_t identity_translate(void *opaque, uint64_t addr) 63 + { 64 + return addr; 65 + } 66 + 67 + static uint64_t load_kernel(const char *kernel_filename) 68 + { 69 + uint64_t kernel_entry, kernel_high; 70 + 71 + if (load_elf_ram_sym(kernel_filename, identity_translate, NULL, 72 + &kernel_entry, NULL, &kernel_high, 0, ELF_MACHINE, 1, 0, 73 + NULL, true, htif_symbol_callback) < 0) { 74 + error_report("qemu: could not load kernel '%s'", kernel_filename); 75 + exit(1); 76 + } 77 + return kernel_entry; 78 + } 79 + 80 + static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap, 81 + uint64_t mem_size, const char *cmdline) 82 + { 83 + void *fdt; 84 + int cpu; 85 + uint32_t *cells; 86 + char *nodename; 87 + 88 + fdt = s->fdt = create_device_tree(&s->fdt_size); 89 + if (!fdt) { 90 + error_report("create_device_tree() failed"); 91 + exit(1); 92 + } 93 + 94 + qemu_fdt_setprop_string(fdt, "/", "model", "ucbbar,spike-bare,qemu"); 95 + qemu_fdt_setprop_string(fdt, "/", "compatible", "ucbbar,spike-bare-dev"); 96 + qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 97 + qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 98 + 99 + qemu_fdt_add_subnode(fdt, "/htif"); 100 + qemu_fdt_setprop_string(fdt, "/htif", "compatible", "ucb,htif0"); 101 + 102 + qemu_fdt_add_subnode(fdt, "/soc"); 103 + qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 104 + qemu_fdt_setprop_string(fdt, "/soc", "compatible", "ucbbar,spike-bare-soc"); 105 + qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 106 + qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 107 + 108 + nodename = g_strdup_printf("/memory@%lx", 109 + (long)memmap[SPIKE_DRAM].base); 110 + qemu_fdt_add_subnode(fdt, nodename); 111 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 112 + memmap[SPIKE_DRAM].base >> 32, memmap[SPIKE_DRAM].base, 113 + mem_size >> 32, mem_size); 114 + qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 115 + g_free(nodename); 116 + 117 + qemu_fdt_add_subnode(fdt, "/cpus"); 118 + qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 10000000); 119 + qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 120 + qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 121 + 122 + for (cpu = s->soc.num_harts - 1; cpu >= 0; cpu--) { 123 + nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 124 + char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 125 + char *isa = riscv_isa_string(&s->soc.harts[cpu]); 126 + qemu_fdt_add_subnode(fdt, nodename); 127 + qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 1000000000); 128 + qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 129 + qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 130 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 131 + qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 132 + qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 133 + qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 134 + qemu_fdt_add_subnode(fdt, intc); 135 + qemu_fdt_setprop_cell(fdt, intc, "phandle", 1); 136 + qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", 1); 137 + qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 138 + qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 139 + qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 140 + g_free(isa); 141 + g_free(intc); 142 + g_free(nodename); 143 + } 144 + 145 + cells = g_new0(uint32_t, s->soc.num_harts * 4); 146 + for (cpu = 0; cpu < s->soc.num_harts; cpu++) { 147 + nodename = 148 + g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 149 + uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 150 + cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 151 + cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 152 + cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 153 + cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 154 + g_free(nodename); 155 + } 156 + nodename = g_strdup_printf("/soc/clint@%lx", 157 + (long)memmap[SPIKE_CLINT].base); 158 + qemu_fdt_add_subnode(fdt, nodename); 159 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 160 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 161 + 0x0, memmap[SPIKE_CLINT].base, 162 + 0x0, memmap[SPIKE_CLINT].size); 163 + qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 164 + cells, s->soc.num_harts * sizeof(uint32_t) * 4); 165 + g_free(cells); 166 + g_free(nodename); 167 + 168 + qemu_fdt_add_subnode(fdt, "/chosen"); 169 + qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 170 + } 171 + 172 + static void spike_v1_10_0_board_init(MachineState *machine) 173 + { 174 + const struct MemmapEntry *memmap = spike_memmap; 175 + 176 + SpikeState *s = g_new0(SpikeState, 1); 177 + MemoryRegion *system_memory = get_system_memory(); 178 + MemoryRegion *main_mem = g_new(MemoryRegion, 1); 179 + MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 180 + 181 + /* Initialize SOC */ 182 + object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 183 + object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 184 + &error_abort); 185 + object_property_set_str(OBJECT(&s->soc), SPIKE_V1_10_0_CPU, "cpu-type", 186 + &error_abort); 187 + object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 188 + &error_abort); 189 + object_property_set_bool(OBJECT(&s->soc), true, "realized", 190 + &error_abort); 191 + 192 + /* register system main memory (actual RAM) */ 193 + memory_region_init_ram(main_mem, NULL, "riscv.spike.ram", 194 + machine->ram_size, &error_fatal); 195 + memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base, 196 + main_mem); 197 + 198 + /* create device tree */ 199 + create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 200 + 201 + /* boot rom */ 202 + memory_region_init_ram(boot_rom, NULL, "riscv.spike.bootrom", 203 + s->fdt_size + 0x2000, &error_fatal); 204 + memory_region_add_subregion(system_memory, 0x0, boot_rom); 205 + 206 + if (machine->kernel_filename) { 207 + load_kernel(machine->kernel_filename); 208 + } 209 + 210 + /* reset vector */ 211 + uint32_t reset_vec[8] = { 212 + 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 213 + 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 214 + 0xf1402573, /* csrr a0, mhartid */ 215 + #if defined(TARGET_RISCV32) 216 + 0x0182a283, /* lw t0, 24(t0) */ 217 + #elif defined(TARGET_RISCV64) 218 + 0x0182b283, /* ld t0, 24(t0) */ 219 + #endif 220 + 0x00028067, /* jr t0 */ 221 + 0x00000000, 222 + memmap[SPIKE_DRAM].base, /* start: .dword DRAM_BASE */ 223 + 0x00000000, 224 + /* dtb: */ 225 + }; 226 + 227 + /* copy in the reset vector */ 228 + copy_le32_to_phys(memmap[SPIKE_MROM].base, reset_vec, sizeof(reset_vec)); 229 + 230 + /* copy in the device tree */ 231 + qemu_fdt_dumpdtb(s->fdt, s->fdt_size); 232 + cpu_physical_memory_write(memmap[SPIKE_MROM].base + sizeof(reset_vec), 233 + s->fdt, s->fdt_size); 234 + 235 + /* initialize HTIF using symbols found in load_kernel */ 236 + htif_mm_init(system_memory, boot_rom, &s->soc.harts[0].env, serial_hds[0]); 237 + 238 + /* Core Local Interruptor (timer and IPI) */ 239 + sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size, 240 + smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 241 + } 242 + 243 + static void spike_v1_09_1_board_init(MachineState *machine) 244 + { 245 + const struct MemmapEntry *memmap = spike_memmap; 246 + 247 + SpikeState *s = g_new0(SpikeState, 1); 248 + MemoryRegion *system_memory = get_system_memory(); 249 + MemoryRegion *main_mem = g_new(MemoryRegion, 1); 250 + MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 251 + 252 + /* Initialize SOC */ 253 + object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 254 + object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 255 + &error_abort); 256 + object_property_set_str(OBJECT(&s->soc), SPIKE_V1_09_1_CPU, "cpu-type", 257 + &error_abort); 258 + object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 259 + &error_abort); 260 + object_property_set_bool(OBJECT(&s->soc), true, "realized", 261 + &error_abort); 262 + 263 + /* register system main memory (actual RAM) */ 264 + memory_region_init_ram(main_mem, NULL, "riscv.spike.ram", 265 + machine->ram_size, &error_fatal); 266 + memory_region_add_subregion(system_memory, memmap[SPIKE_DRAM].base, 267 + main_mem); 268 + 269 + /* boot rom */ 270 + memory_region_init_ram(boot_rom, NULL, "riscv.spike.bootrom", 271 + 0x40000, &error_fatal); 272 + memory_region_add_subregion(system_memory, 0x0, boot_rom); 273 + 274 + if (machine->kernel_filename) { 275 + load_kernel(machine->kernel_filename); 276 + } 277 + 278 + /* reset vector */ 279 + uint32_t reset_vec[8] = { 280 + 0x297 + memmap[SPIKE_DRAM].base - memmap[SPIKE_MROM].base, /* lui */ 281 + 0x00028067, /* jump to DRAM_BASE */ 282 + 0x00000000, /* reserved */ 283 + memmap[SPIKE_MROM].base + sizeof(reset_vec), /* config string pointer */ 284 + 0, 0, 0, 0 /* trap vector */ 285 + }; 286 + 287 + /* part one of config string - before memory size specified */ 288 + const char *config_string_tmpl = 289 + "platform {\n" 290 + " vendor ucb;\n" 291 + " arch spike;\n" 292 + "};\n" 293 + "rtc {\n" 294 + " addr 0x%" PRIx64 "x;\n" 295 + "};\n" 296 + "ram {\n" 297 + " 0 {\n" 298 + " addr 0x%" PRIx64 "x;\n" 299 + " size 0x%" PRIx64 "x;\n" 300 + " };\n" 301 + "};\n" 302 + "core {\n" 303 + " 0" " {\n" 304 + " " "0 {\n" 305 + " isa %s;\n" 306 + " timecmp 0x%" PRIx64 "x;\n" 307 + " ipi 0x%" PRIx64 "x;\n" 308 + " };\n" 309 + " };\n" 310 + "};\n"; 311 + 312 + /* build config string with supplied memory size */ 313 + char *isa = riscv_isa_string(&s->soc.harts[0]); 314 + size_t config_string_size = strlen(config_string_tmpl) + 48; 315 + char *config_string = malloc(config_string_size); 316 + snprintf(config_string, config_string_size, config_string_tmpl, 317 + (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_TIME_BASE, 318 + (uint64_t)memmap[SPIKE_DRAM].base, 319 + (uint64_t)ram_size, isa, 320 + (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_TIMECMP_BASE, 321 + (uint64_t)memmap[SPIKE_CLINT].base + SIFIVE_SIP_BASE); 322 + g_free(isa); 323 + size_t config_string_len = strlen(config_string); 324 + 325 + /* copy in the reset vector */ 326 + copy_le32_to_phys(memmap[SPIKE_MROM].base, reset_vec, sizeof(reset_vec)); 327 + 328 + /* copy in the config string */ 329 + cpu_physical_memory_write(memmap[SPIKE_MROM].base + sizeof(reset_vec), 330 + config_string, config_string_len); 331 + 332 + /* initialize HTIF using symbols found in load_kernel */ 333 + htif_mm_init(system_memory, boot_rom, &s->soc.harts[0].env, serial_hds[0]); 334 + 335 + /* Core Local Interruptor (timer and IPI) */ 336 + sifive_clint_create(memmap[SPIKE_CLINT].base, memmap[SPIKE_CLINT].size, 337 + smp_cpus, SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 338 + } 339 + 340 + static const TypeInfo spike_v_1_09_1_device = { 341 + .name = TYPE_RISCV_SPIKE_V1_09_1_BOARD, 342 + .parent = TYPE_SYS_BUS_DEVICE, 343 + .instance_size = sizeof(SpikeState), 344 + }; 345 + 346 + static const TypeInfo spike_v_1_10_0_device = { 347 + .name = TYPE_RISCV_SPIKE_V1_10_0_BOARD, 348 + .parent = TYPE_SYS_BUS_DEVICE, 349 + .instance_size = sizeof(SpikeState), 350 + }; 351 + 352 + static void spike_v1_09_1_machine_init(MachineClass *mc) 353 + { 354 + mc->desc = "RISC-V Spike Board (Privileged ISA v1.9.1)"; 355 + mc->init = spike_v1_09_1_board_init; 356 + mc->max_cpus = 1; 357 + } 358 + 359 + static void spike_v1_10_0_machine_init(MachineClass *mc) 360 + { 361 + mc->desc = "RISC-V Spike Board (Privileged ISA v1.10)"; 362 + mc->init = spike_v1_10_0_board_init; 363 + mc->max_cpus = 1; 364 + mc->is_default = 1; 365 + } 366 + 367 + DEFINE_MACHINE("spike_v1.9.1", spike_v1_09_1_machine_init) 368 + DEFINE_MACHINE("spike_v1.10", spike_v1_10_0_machine_init) 369 + 370 + static void riscv_spike_board_register_types(void) 371 + { 372 + type_register_static(&spike_v_1_09_1_device); 373 + type_register_static(&spike_v_1_10_0_device); 374 + } 375 + 376 + type_init(riscv_spike_board_register_types);
+420
hw/riscv/virt.c
··· 1 + /* 2 + * QEMU RISC-V VirtIO Board 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * RISC-V machine with 16550a UART and VirtIO MMIO 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #include "qemu/osdep.h" 22 + #include "qemu/log.h" 23 + #include "qemu/error-report.h" 24 + #include "qapi/error.h" 25 + #include "hw/hw.h" 26 + #include "hw/boards.h" 27 + #include "hw/loader.h" 28 + #include "hw/sysbus.h" 29 + #include "hw/char/serial.h" 30 + #include "target/riscv/cpu.h" 31 + #include "hw/riscv/riscv_htif.h" 32 + #include "hw/riscv/riscv_hart.h" 33 + #include "hw/riscv/sifive_plic.h" 34 + #include "hw/riscv/sifive_clint.h" 35 + #include "hw/riscv/sifive_test.h" 36 + #include "hw/riscv/virt.h" 37 + #include "chardev/char.h" 38 + #include "sysemu/arch_init.h" 39 + #include "sysemu/device_tree.h" 40 + #include "exec/address-spaces.h" 41 + #include "elf.h" 42 + 43 + static const struct MemmapEntry { 44 + hwaddr base; 45 + hwaddr size; 46 + } virt_memmap[] = { 47 + [VIRT_DEBUG] = { 0x0, 0x100 }, 48 + [VIRT_MROM] = { 0x1000, 0x2000 }, 49 + [VIRT_TEST] = { 0x4000, 0x1000 }, 50 + [VIRT_CLINT] = { 0x2000000, 0x10000 }, 51 + [VIRT_PLIC] = { 0xc000000, 0x4000000 }, 52 + [VIRT_UART0] = { 0x10000000, 0x100 }, 53 + [VIRT_VIRTIO] = { 0x10001000, 0x1000 }, 54 + [VIRT_DRAM] = { 0x80000000, 0x0 }, 55 + }; 56 + 57 + static void copy_le32_to_phys(hwaddr pa, uint32_t *rom, size_t len) 58 + { 59 + int i; 60 + for (i = 0; i < (len >> 2); i++) { 61 + stl_phys(&address_space_memory, pa + (i << 2), rom[i]); 62 + } 63 + } 64 + 65 + static uint64_t identity_translate(void *opaque, uint64_t addr) 66 + { 67 + return addr; 68 + } 69 + 70 + static uint64_t load_kernel(const char *kernel_filename) 71 + { 72 + uint64_t kernel_entry, kernel_high; 73 + 74 + if (load_elf(kernel_filename, identity_translate, NULL, 75 + &kernel_entry, NULL, &kernel_high, 76 + 0, ELF_MACHINE, 1, 0) < 0) { 77 + error_report("qemu: could not load kernel '%s'", kernel_filename); 78 + exit(1); 79 + } 80 + return kernel_entry; 81 + } 82 + 83 + static hwaddr load_initrd(const char *filename, uint64_t mem_size, 84 + uint64_t kernel_entry, hwaddr *start) 85 + { 86 + int size; 87 + 88 + /* We want to put the initrd far enough into RAM that when the 89 + * kernel is uncompressed it will not clobber the initrd. However 90 + * on boards without much RAM we must ensure that we still leave 91 + * enough room for a decent sized initrd, and on boards with large 92 + * amounts of RAM we must avoid the initrd being so far up in RAM 93 + * that it is outside lowmem and inaccessible to the kernel. 94 + * So for boards with less than 256MB of RAM we put the initrd 95 + * halfway into RAM, and for boards with 256MB of RAM or more we put 96 + * the initrd at 128MB. 97 + */ 98 + *start = kernel_entry + MIN(mem_size / 2, 128 * 1024 * 1024); 99 + 100 + size = load_ramdisk(filename, *start, mem_size - *start); 101 + if (size == -1) { 102 + size = load_image_targphys(filename, *start, mem_size - *start); 103 + if (size == -1) { 104 + error_report("qemu: could not load ramdisk '%s'", filename); 105 + exit(1); 106 + } 107 + } 108 + return *start + size; 109 + } 110 + 111 + static void *create_fdt(RISCVVirtState *s, const struct MemmapEntry *memmap, 112 + uint64_t mem_size, const char *cmdline) 113 + { 114 + void *fdt; 115 + int cpu; 116 + uint32_t *cells; 117 + char *nodename; 118 + uint32_t plic_phandle, phandle = 1; 119 + int i; 120 + 121 + fdt = s->fdt = create_device_tree(&s->fdt_size); 122 + if (!fdt) { 123 + error_report("create_device_tree() failed"); 124 + exit(1); 125 + } 126 + 127 + qemu_fdt_setprop_string(fdt, "/", "model", "riscv-virtio,qemu"); 128 + qemu_fdt_setprop_string(fdt, "/", "compatible", "riscv-virtio"); 129 + qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); 130 + qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); 131 + 132 + qemu_fdt_add_subnode(fdt, "/soc"); 133 + qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); 134 + qemu_fdt_setprop_string(fdt, "/soc", "compatible", "riscv-virtio-soc"); 135 + qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); 136 + qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); 137 + 138 + nodename = g_strdup_printf("/memory@%lx", 139 + (long)memmap[VIRT_DRAM].base); 140 + qemu_fdt_add_subnode(fdt, nodename); 141 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 142 + memmap[VIRT_DRAM].base >> 32, memmap[VIRT_DRAM].base, 143 + mem_size >> 32, mem_size); 144 + qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); 145 + g_free(nodename); 146 + 147 + qemu_fdt_add_subnode(fdt, "/cpus"); 148 + qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", 10000000); 149 + qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); 150 + qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); 151 + 152 + for (cpu = s->soc.num_harts - 1; cpu >= 0; cpu--) { 153 + int cpu_phandle = phandle++; 154 + nodename = g_strdup_printf("/cpus/cpu@%d", cpu); 155 + char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 156 + char *isa = riscv_isa_string(&s->soc.harts[cpu]); 157 + qemu_fdt_add_subnode(fdt, nodename); 158 + qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 1000000000); 159 + qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); 160 + qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); 161 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); 162 + qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); 163 + qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); 164 + qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); 165 + qemu_fdt_add_subnode(fdt, intc); 166 + qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); 167 + qemu_fdt_setprop_cell(fdt, intc, "linux,phandle", cpu_phandle); 168 + qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); 169 + qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); 170 + qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); 171 + g_free(isa); 172 + g_free(intc); 173 + g_free(nodename); 174 + } 175 + 176 + cells = g_new0(uint32_t, s->soc.num_harts * 4); 177 + for (cpu = 0; cpu < s->soc.num_harts; cpu++) { 178 + nodename = 179 + g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 180 + uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 181 + cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 182 + cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); 183 + cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 184 + cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); 185 + g_free(nodename); 186 + } 187 + nodename = g_strdup_printf("/soc/clint@%lx", 188 + (long)memmap[VIRT_CLINT].base); 189 + qemu_fdt_add_subnode(fdt, nodename); 190 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,clint0"); 191 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 192 + 0x0, memmap[VIRT_CLINT].base, 193 + 0x0, memmap[VIRT_CLINT].size); 194 + qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 195 + cells, s->soc.num_harts * sizeof(uint32_t) * 4); 196 + g_free(cells); 197 + g_free(nodename); 198 + 199 + plic_phandle = phandle++; 200 + cells = g_new0(uint32_t, s->soc.num_harts * 4); 201 + for (cpu = 0; cpu < s->soc.num_harts; cpu++) { 202 + nodename = 203 + g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); 204 + uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); 205 + cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); 206 + cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_EXT); 207 + cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); 208 + cells[cpu * 4 + 3] = cpu_to_be32(IRQ_S_EXT); 209 + g_free(nodename); 210 + } 211 + nodename = g_strdup_printf("/soc/interrupt-controller@%lx", 212 + (long)memmap[VIRT_PLIC].base); 213 + qemu_fdt_add_subnode(fdt, nodename); 214 + qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); 215 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv,plic0"); 216 + qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); 217 + qemu_fdt_setprop(fdt, nodename, "interrupts-extended", 218 + cells, s->soc.num_harts * sizeof(uint32_t) * 4); 219 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 220 + 0x0, memmap[VIRT_PLIC].base, 221 + 0x0, memmap[VIRT_PLIC].size); 222 + qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); 223 + qemu_fdt_setprop_cell(fdt, nodename, "riscv,max-priority", 7); 224 + qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", VIRTIO_NDEV); 225 + qemu_fdt_setprop_cells(fdt, nodename, "phandle", plic_phandle); 226 + qemu_fdt_setprop_cells(fdt, nodename, "linux,phandle", plic_phandle); 227 + plic_phandle = qemu_fdt_get_phandle(fdt, nodename); 228 + g_free(cells); 229 + g_free(nodename); 230 + 231 + for (i = 0; i < VIRTIO_COUNT; i++) { 232 + nodename = g_strdup_printf("/virtio_mmio@%lx", 233 + (long)(memmap[VIRT_VIRTIO].base + i * memmap[VIRT_VIRTIO].size)); 234 + qemu_fdt_add_subnode(fdt, nodename); 235 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "virtio,mmio"); 236 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 237 + 0x0, memmap[VIRT_VIRTIO].base + i * memmap[VIRT_VIRTIO].size, 238 + 0x0, memmap[VIRT_VIRTIO].size); 239 + qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 240 + qemu_fdt_setprop_cells(fdt, nodename, "interrupts", VIRTIO_IRQ + i); 241 + g_free(nodename); 242 + } 243 + 244 + nodename = g_strdup_printf("/test@%lx", 245 + (long)memmap[VIRT_TEST].base); 246 + qemu_fdt_add_subnode(fdt, nodename); 247 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,test0"); 248 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 249 + 0x0, memmap[VIRT_TEST].base, 250 + 0x0, memmap[VIRT_TEST].size); 251 + 252 + nodename = g_strdup_printf("/uart@%lx", 253 + (long)memmap[VIRT_UART0].base); 254 + qemu_fdt_add_subnode(fdt, nodename); 255 + qemu_fdt_setprop_string(fdt, nodename, "compatible", "ns16550a"); 256 + qemu_fdt_setprop_cells(fdt, nodename, "reg", 257 + 0x0, memmap[VIRT_UART0].base, 258 + 0x0, memmap[VIRT_UART0].size); 259 + qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", 3686400); 260 + qemu_fdt_setprop_cells(fdt, nodename, "interrupt-parent", plic_phandle); 261 + qemu_fdt_setprop_cells(fdt, nodename, "interrupts", UART0_IRQ); 262 + 263 + qemu_fdt_add_subnode(fdt, "/chosen"); 264 + qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); 265 + qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); 266 + g_free(nodename); 267 + 268 + return fdt; 269 + } 270 + 271 + static void riscv_virt_board_init(MachineState *machine) 272 + { 273 + const struct MemmapEntry *memmap = virt_memmap; 274 + 275 + RISCVVirtState *s = g_new0(RISCVVirtState, 1); 276 + MemoryRegion *system_memory = get_system_memory(); 277 + MemoryRegion *main_mem = g_new(MemoryRegion, 1); 278 + MemoryRegion *boot_rom = g_new(MemoryRegion, 1); 279 + char *plic_hart_config; 280 + size_t plic_hart_config_len; 281 + int i; 282 + void *fdt; 283 + 284 + /* Initialize SOC */ 285 + object_initialize(&s->soc, sizeof(s->soc), TYPE_RISCV_HART_ARRAY); 286 + object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 287 + &error_abort); 288 + object_property_set_str(OBJECT(&s->soc), VIRT_CPU, "cpu-type", 289 + &error_abort); 290 + object_property_set_int(OBJECT(&s->soc), smp_cpus, "num-harts", 291 + &error_abort); 292 + object_property_set_bool(OBJECT(&s->soc), true, "realized", 293 + &error_abort); 294 + 295 + /* register system main memory (actual RAM) */ 296 + memory_region_init_ram(main_mem, NULL, "riscv_virt_board.ram", 297 + machine->ram_size, &error_fatal); 298 + memory_region_add_subregion(system_memory, memmap[VIRT_DRAM].base, 299 + main_mem); 300 + 301 + /* create device tree */ 302 + fdt = create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline); 303 + 304 + /* boot rom */ 305 + memory_region_init_ram(boot_rom, NULL, "riscv_virt_board.bootrom", 306 + s->fdt_size + 0x2000, &error_fatal); 307 + memory_region_add_subregion(system_memory, 0x0, boot_rom); 308 + 309 + if (machine->kernel_filename) { 310 + uint64_t kernel_entry = load_kernel(machine->kernel_filename); 311 + 312 + if (machine->initrd_filename) { 313 + hwaddr start; 314 + hwaddr end = load_initrd(machine->initrd_filename, 315 + machine->ram_size, kernel_entry, 316 + &start); 317 + qemu_fdt_setprop_cell(fdt, "/chosen", 318 + "linux,initrd-start", start); 319 + qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", 320 + end); 321 + } 322 + } 323 + 324 + /* reset vector */ 325 + uint32_t reset_vec[8] = { 326 + 0x00000297, /* 1: auipc t0, %pcrel_hi(dtb) */ 327 + 0x02028593, /* addi a1, t0, %pcrel_lo(1b) */ 328 + 0xf1402573, /* csrr a0, mhartid */ 329 + #if defined(TARGET_RISCV32) 330 + 0x0182a283, /* lw t0, 24(t0) */ 331 + #elif defined(TARGET_RISCV64) 332 + 0x0182b283, /* ld t0, 24(t0) */ 333 + #endif 334 + 0x00028067, /* jr t0 */ 335 + 0x00000000, 336 + memmap[VIRT_DRAM].base, /* start: .dword memmap[VIRT_DRAM].base */ 337 + 0x00000000, 338 + /* dtb: */ 339 + }; 340 + 341 + /* copy in the reset vector */ 342 + copy_le32_to_phys(ROM_BASE, reset_vec, sizeof(reset_vec)); 343 + 344 + /* copy in the device tree */ 345 + qemu_fdt_dumpdtb(s->fdt, s->fdt_size); 346 + cpu_physical_memory_write(ROM_BASE + sizeof(reset_vec), 347 + s->fdt, s->fdt_size); 348 + 349 + /* create PLIC hart topology configuration string */ 350 + plic_hart_config_len = (strlen(VIRT_PLIC_HART_CONFIG) + 1) * smp_cpus; 351 + plic_hart_config = g_malloc0(plic_hart_config_len); 352 + for (i = 0; i < smp_cpus; i++) { 353 + if (i != 0) { 354 + strncat(plic_hart_config, ",", plic_hart_config_len); 355 + } 356 + strncat(plic_hart_config, VIRT_PLIC_HART_CONFIG, plic_hart_config_len); 357 + plic_hart_config_len -= (strlen(VIRT_PLIC_HART_CONFIG) + 1); 358 + } 359 + 360 + /* MMIO */ 361 + s->plic = sifive_plic_create(memmap[VIRT_PLIC].base, 362 + plic_hart_config, 363 + VIRT_PLIC_NUM_SOURCES, 364 + VIRT_PLIC_NUM_PRIORITIES, 365 + VIRT_PLIC_PRIORITY_BASE, 366 + VIRT_PLIC_PENDING_BASE, 367 + VIRT_PLIC_ENABLE_BASE, 368 + VIRT_PLIC_ENABLE_STRIDE, 369 + VIRT_PLIC_CONTEXT_BASE, 370 + VIRT_PLIC_CONTEXT_STRIDE, 371 + memmap[VIRT_PLIC].size); 372 + sifive_clint_create(memmap[VIRT_CLINT].base, 373 + memmap[VIRT_CLINT].size, smp_cpus, 374 + SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE); 375 + sifive_test_create(memmap[VIRT_TEST].base); 376 + 377 + for (i = 0; i < VIRTIO_COUNT; i++) { 378 + sysbus_create_simple("virtio-mmio", 379 + memmap[VIRT_VIRTIO].base + i * memmap[VIRT_VIRTIO].size, 380 + SIFIVE_PLIC(s->plic)->irqs[VIRTIO_IRQ + i]); 381 + } 382 + 383 + serial_mm_init(system_memory, memmap[VIRT_UART0].base, 384 + 0, SIFIVE_PLIC(s->plic)->irqs[UART0_IRQ], 399193, 385 + serial_hds[0], DEVICE_LITTLE_ENDIAN); 386 + } 387 + 388 + static int riscv_virt_board_sysbus_device_init(SysBusDevice *sysbusdev) 389 + { 390 + return 0; 391 + } 392 + 393 + static void riscv_virt_board_class_init(ObjectClass *klass, void *data) 394 + { 395 + SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 396 + k->init = riscv_virt_board_sysbus_device_init; 397 + } 398 + 399 + static const TypeInfo riscv_virt_board_device = { 400 + .name = TYPE_RISCV_VIRT_BOARD, 401 + .parent = TYPE_SYS_BUS_DEVICE, 402 + .instance_size = sizeof(RISCVVirtState), 403 + .class_init = riscv_virt_board_class_init, 404 + }; 405 + 406 + static void riscv_virt_board_machine_init(MachineClass *mc) 407 + { 408 + mc->desc = "RISC-V VirtIO Board (Privileged spec v1.10)"; 409 + mc->init = riscv_virt_board_init; 410 + mc->max_cpus = 8; /* hardcoded limit in BBL */ 411 + } 412 + 413 + DEFINE_MACHINE("virt", riscv_virt_board_machine_init) 414 + 415 + static void riscv_virt_board_register_types(void) 416 + { 417 + type_register_static(&riscv_virt_board_device); 418 + } 419 + 420 + type_init(riscv_virt_board_register_types);
+2
include/disas/bfd.h
··· 429 429 int print_insn_big_nios2 (bfd_vma, disassemble_info*); 430 430 int print_insn_little_nios2 (bfd_vma, disassemble_info*); 431 431 int print_insn_xtensa (bfd_vma, disassemble_info*); 432 + int print_insn_riscv32 (bfd_vma, disassemble_info*); 433 + int print_insn_riscv64 (bfd_vma, disassemble_info*); 432 434 433 435 #if 0 434 436 /* Fetch the disassembler for a given BFD, if that support is available. */
+2
include/elf.h
··· 119 119 120 120 #define EM_UNICORE32 110 /* UniCore32 */ 121 121 122 + #define EM_RISCV 243 /* RISC-V */ 123 + 122 124 /* 123 125 * This is an interim value that we will use until the committee comes 124 126 * up with a final number.
+21 -13
include/hw/elf_ops.h
··· 105 105 } 106 106 107 107 static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab, 108 - int clear_lsb) 108 + int clear_lsb, symbol_fn_t sym_cb) 109 109 { 110 110 struct elf_shdr *symtab, *strtab, *shdr_table = NULL; 111 111 struct elf_sym *syms = NULL; ··· 133 133 134 134 nsyms = symtab->sh_size / sizeof(struct elf_sym); 135 135 136 + /* String table */ 137 + if (symtab->sh_link >= ehdr->e_shnum) { 138 + goto fail; 139 + } 140 + strtab = &shdr_table[symtab->sh_link]; 141 + 142 + str = load_at(fd, strtab->sh_offset, strtab->sh_size); 143 + if (!str) { 144 + goto fail; 145 + } 146 + 136 147 i = 0; 137 148 while (i < nsyms) { 138 - if (must_swab) 149 + if (must_swab) { 139 150 glue(bswap_sym, SZ)(&syms[i]); 151 + } 152 + if (sym_cb) { 153 + sym_cb(str + syms[i].st_name, syms[i].st_info, 154 + syms[i].st_value, syms[i].st_size); 155 + } 140 156 /* We are only interested in function symbols. 141 157 Throw everything else away. */ 142 158 if (syms[i].st_shndx == SHN_UNDEF || ··· 163 179 } 164 180 } 165 181 166 - /* String table */ 167 - if (symtab->sh_link >= ehdr->e_shnum) 168 - goto fail; 169 - strtab = &shdr_table[symtab->sh_link]; 170 - 171 - str = load_at(fd, strtab->sh_offset, strtab->sh_size); 172 - if (!str) 173 - goto fail; 174 - 175 182 /* Commit */ 176 183 s = g_malloc0(sizeof(*s)); 177 184 s->lookup_symbol = glue(lookup_symbol, SZ); ··· 264 271 int must_swab, uint64_t *pentry, 265 272 uint64_t *lowaddr, uint64_t *highaddr, 266 273 int elf_machine, int clear_lsb, int data_swab, 267 - AddressSpace *as, bool load_rom) 274 + AddressSpace *as, bool load_rom, 275 + symbol_fn_t sym_cb) 268 276 { 269 277 struct elfhdr ehdr; 270 278 struct elf_phdr *phdr = NULL, *ph; ··· 329 337 if (pentry) 330 338 *pentry = (uint64_t)(elf_sword)ehdr.e_entry; 331 339 332 - glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb); 340 + glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb, sym_cb); 333 341 334 342 size = ehdr.e_phnum * sizeof(phdr[0]); 335 343 if (lseek(fd, ehdr.e_phoff, SEEK_SET) != ehdr.e_phoff) {
+16 -1
include/hw/loader.h
··· 64 64 #define ELF_LOAD_WRONG_ENDIAN -4 65 65 const char *load_elf_strerror(int error); 66 66 67 - /** load_elf_ram: 67 + /** load_elf_ram_sym: 68 68 * @filename: Path of ELF file 69 69 * @translate_fn: optional function to translate load addresses 70 70 * @translate_opaque: opaque data passed to @translate_fn ··· 81 81 * @as: The AddressSpace to load the ELF to. The value of address_space_memory 82 82 * is used if nothing is supplied here. 83 83 * @load_rom : Load ELF binary as ROM 84 + * @sym_cb: Callback function for symbol table entries 84 85 * 85 86 * Load an ELF file's contents to the emulated system's address space. 86 87 * Clients may optionally specify a callback to perform address ··· 92 93 * their particular values for @elf_machine are set. 93 94 * If @elf_machine is EM_NONE then the machine type will be read from the 94 95 * ELF header and no checks will be carried out against the machine type. 96 + */ 97 + typedef void (*symbol_fn_t)(const char *st_name, int st_info, 98 + uint64_t st_value, uint64_t st_size); 99 + 100 + int load_elf_ram_sym(const char *filename, 101 + uint64_t (*translate_fn)(void *, uint64_t), 102 + void *translate_opaque, uint64_t *pentry, 103 + uint64_t *lowaddr, uint64_t *highaddr, int big_endian, 104 + int elf_machine, int clear_lsb, int data_swab, 105 + AddressSpace *as, bool load_rom, symbol_fn_t sym_cb); 106 + 107 + /** load_elf_ram: 108 + * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a 109 + * symbol callback function 95 110 */ 96 111 int load_elf_ram(const char *filename, 97 112 uint64_t (*translate_fn)(void *, uint64_t),
+39
include/hw/riscv/riscv_hart.h
··· 1 + /* 2 + * QEMU RISC-V Hart Array interface 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * Holds the state of a heterogenous array of RISC-V harts 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #ifndef HW_RISCV_HART_H 22 + #define HW_RISCV_HART_H 23 + 24 + #define TYPE_RISCV_HART_ARRAY "riscv.hart_array" 25 + 26 + #define RISCV_HART_ARRAY(obj) \ 27 + OBJECT_CHECK(RISCVHartArrayState, (obj), TYPE_RISCV_HART_ARRAY) 28 + 29 + typedef struct RISCVHartArrayState { 30 + /*< private >*/ 31 + SysBusDevice parent_obj; 32 + 33 + /*< public >*/ 34 + uint32_t num_harts; 35 + char *cpu_type; 36 + RISCVCPU *harts; 37 + } RISCVHartArrayState; 38 + 39 + #endif
+61
include/hw/riscv/riscv_htif.h
··· 1 + /* 2 + * QEMU RISCV Host Target Interface (HTIF) Emulation 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #ifndef HW_RISCV_HTIF_H 21 + #define HW_RISCV_HTIF_H 22 + 23 + #include "hw/hw.h" 24 + #include "chardev/char.h" 25 + #include "chardev/char-fe.h" 26 + #include "sysemu/sysemu.h" 27 + #include "exec/memory.h" 28 + #include "target/riscv/cpu.h" 29 + 30 + #define TYPE_HTIF_UART "riscv.htif.uart" 31 + 32 + typedef struct HTIFState { 33 + int allow_tohost; 34 + int fromhost_inprogress; 35 + 36 + hwaddr tohost_offset; 37 + hwaddr fromhost_offset; 38 + uint64_t tohost_size; 39 + uint64_t fromhost_size; 40 + MemoryRegion mmio; 41 + MemoryRegion *address_space; 42 + MemoryRegion *main_mem; 43 + void *main_mem_ram_ptr; 44 + 45 + CPURISCVState *env; 46 + CharBackend chr; 47 + uint64_t pending_read; 48 + } HTIFState; 49 + 50 + extern const VMStateDescription vmstate_htif; 51 + extern const MemoryRegionOps htif_io_ops; 52 + 53 + /* HTIF symbol callback */ 54 + void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value, 55 + uint64_t st_size); 56 + 57 + /* legacy pre qom */ 58 + HTIFState *htif_mm_init(MemoryRegion *address_space, MemoryRegion *main_mem, 59 + CPURISCVState *env, Chardev *chr); 60 + 61 + #endif
+50
include/hw/riscv/sifive_clint.h
··· 1 + /* 2 + * SiFive CLINT (Core Local Interruptor) interface 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #ifndef HW_SIFIVE_CLINT_H 21 + #define HW_SIFIVE_CLINT_H 22 + 23 + #define TYPE_SIFIVE_CLINT "riscv.sifive.clint" 24 + 25 + #define SIFIVE_CLINT(obj) \ 26 + OBJECT_CHECK(SiFiveCLINTState, (obj), TYPE_SIFIVE_CLINT) 27 + 28 + typedef struct SiFiveCLINTState { 29 + /*< private >*/ 30 + SysBusDevice parent_obj; 31 + 32 + /*< public >*/ 33 + MemoryRegion mmio; 34 + uint32_t num_harts; 35 + uint32_t sip_base; 36 + uint32_t timecmp_base; 37 + uint32_t time_base; 38 + uint32_t aperture_size; 39 + } SiFiveCLINTState; 40 + 41 + DeviceState *sifive_clint_create(hwaddr addr, hwaddr size, uint32_t num_harts, 42 + uint32_t sip_base, uint32_t timecmp_base, uint32_t time_base); 43 + 44 + enum { 45 + SIFIVE_SIP_BASE = 0x0, 46 + SIFIVE_TIMECMP_BASE = 0x4000, 47 + SIFIVE_TIME_BASE = 0xBFF8 48 + }; 49 + 50 + #endif
+79
include/hw/riscv/sifive_e.h
··· 1 + /* 2 + * SiFive E series machine interface 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #ifndef HW_SIFIVE_E_H 20 + #define HW_SIFIVE_E_H 21 + 22 + #define TYPE_SIFIVE_E "riscv.sifive_e" 23 + 24 + #define SIFIVE_E(obj) \ 25 + OBJECT_CHECK(SiFiveEState, (obj), TYPE_SIFIVE_E) 26 + 27 + typedef struct SiFiveEState { 28 + /*< private >*/ 29 + SysBusDevice parent_obj; 30 + 31 + /*< public >*/ 32 + RISCVHartArrayState soc; 33 + DeviceState *plic; 34 + } SiFiveEState; 35 + 36 + enum { 37 + SIFIVE_E_DEBUG, 38 + SIFIVE_E_MROM, 39 + SIFIVE_E_OTP, 40 + SIFIVE_E_CLINT, 41 + SIFIVE_E_PLIC, 42 + SIFIVE_E_AON, 43 + SIFIVE_E_PRCI, 44 + SIFIVE_E_OTP_CTRL, 45 + SIFIVE_E_GPIO0, 46 + SIFIVE_E_UART0, 47 + SIFIVE_E_QSPI0, 48 + SIFIVE_E_PWM0, 49 + SIFIVE_E_UART1, 50 + SIFIVE_E_QSPI1, 51 + SIFIVE_E_PWM1, 52 + SIFIVE_E_QSPI2, 53 + SIFIVE_E_PWM2, 54 + SIFIVE_E_XIP, 55 + SIFIVE_E_DTIM 56 + }; 57 + 58 + enum { 59 + SIFIVE_E_UART0_IRQ = 3, 60 + SIFIVE_E_UART1_IRQ = 4 61 + }; 62 + 63 + #define SIFIVE_E_PLIC_HART_CONFIG "M" 64 + #define SIFIVE_E_PLIC_NUM_SOURCES 127 65 + #define SIFIVE_E_PLIC_NUM_PRIORITIES 7 66 + #define SIFIVE_E_PLIC_PRIORITY_BASE 0x0 67 + #define SIFIVE_E_PLIC_PENDING_BASE 0x1000 68 + #define SIFIVE_E_PLIC_ENABLE_BASE 0x2000 69 + #define SIFIVE_E_PLIC_ENABLE_STRIDE 0x80 70 + #define SIFIVE_E_PLIC_CONTEXT_BASE 0x200000 71 + #define SIFIVE_E_PLIC_CONTEXT_STRIDE 0x1000 72 + 73 + #if defined(TARGET_RISCV32) 74 + #define SIFIVE_E_CPU TYPE_RISCV_CPU_SIFIVE_E31 75 + #elif defined(TARGET_RISCV64) 76 + #define SIFIVE_E_CPU TYPE_RISCV_CPU_SIFIVE_E51 77 + #endif 78 + 79 + #endif
+85
include/hw/riscv/sifive_plic.h
··· 1 + /* 2 + * SiFive PLIC (Platform Level Interrupt Controller) interface 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This provides a RISC-V PLIC device 7 + * 8 + * This program is free software; you can redistribute it and/or modify it 9 + * under the terms and conditions of the GNU General Public License, 10 + * version 2 or later, as published by the Free Software Foundation. 11 + * 12 + * This program is distributed in the hope it will be useful, but WITHOUT 13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 + * more details. 16 + * 17 + * You should have received a copy of the GNU General Public License along with 18 + * this program. If not, see <http://www.gnu.org/licenses/>. 19 + */ 20 + 21 + #ifndef HW_SIFIVE_PLIC_H 22 + #define HW_SIFIVE_PLIC_H 23 + 24 + #include "hw/irq.h" 25 + 26 + #define TYPE_SIFIVE_PLIC "riscv.sifive.plic" 27 + 28 + #define SIFIVE_PLIC(obj) \ 29 + OBJECT_CHECK(SiFivePLICState, (obj), TYPE_SIFIVE_PLIC) 30 + 31 + typedef enum PLICMode { 32 + PLICMode_U, 33 + PLICMode_S, 34 + PLICMode_H, 35 + PLICMode_M 36 + } PLICMode; 37 + 38 + typedef struct PLICAddr { 39 + uint32_t addrid; 40 + uint32_t hartid; 41 + PLICMode mode; 42 + } PLICAddr; 43 + 44 + typedef struct SiFivePLICState { 45 + /*< private >*/ 46 + SysBusDevice parent_obj; 47 + 48 + /*< public >*/ 49 + MemoryRegion mmio; 50 + uint32_t num_addrs; 51 + uint32_t bitfield_words; 52 + PLICAddr *addr_config; 53 + uint32_t *source_priority; 54 + uint32_t *target_priority; 55 + uint32_t *pending; 56 + uint32_t *claimed; 57 + uint32_t *enable; 58 + QemuMutex lock; 59 + qemu_irq *irqs; 60 + 61 + /* config */ 62 + char *hart_config; 63 + uint32_t num_sources; 64 + uint32_t num_priorities; 65 + uint32_t priority_base; 66 + uint32_t pending_base; 67 + uint32_t enable_base; 68 + uint32_t enable_stride; 69 + uint32_t context_base; 70 + uint32_t context_stride; 71 + uint32_t aperture_size; 72 + } SiFivePLICState; 73 + 74 + void sifive_plic_raise_irq(SiFivePLICState *plic, uint32_t irq); 75 + void sifive_plic_lower_irq(SiFivePLICState *plic, uint32_t irq); 76 + 77 + DeviceState *sifive_plic_create(hwaddr addr, char *hart_config, 78 + uint32_t num_sources, uint32_t num_priorities, 79 + uint32_t priority_base, uint32_t pending_base, 80 + uint32_t enable_base, uint32_t enable_stride, 81 + uint32_t context_base, uint32_t context_stride, 82 + uint32_t aperture_size); 83 + 84 + #endif 85 +
+37
include/hw/riscv/sifive_prci.h
··· 1 + /* 2 + * QEMU SiFive PRCI (Power, Reset, Clock, Interrupt) interface 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #ifndef HW_SIFIVE_PRCI_H 20 + #define HW_SIFIVE_PRCI_H 21 + 22 + #define TYPE_SIFIVE_PRCI "riscv.sifive.prci" 23 + 24 + #define SIFIVE_PRCI(obj) \ 25 + OBJECT_CHECK(SiFivePRCIState, (obj), TYPE_SIFIVE_PRCI) 26 + 27 + typedef struct SiFivePRCIState { 28 + /*< private >*/ 29 + SysBusDevice parent_obj; 30 + 31 + /*< public >*/ 32 + MemoryRegion mmio; 33 + } SiFivePRCIState; 34 + 35 + DeviceState *sifive_prci_create(hwaddr addr); 36 + 37 + #endif
+42
include/hw/riscv/sifive_test.h
··· 1 + /* 2 + * QEMU Test Finisher interface 3 + * 4 + * Copyright (c) 2018 SiFive, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #ifndef HW_SIFIVE_TEST_H 20 + #define HW_SIFIVE_TEST_H 21 + 22 + #define TYPE_SIFIVE_TEST "riscv.sifive.test" 23 + 24 + #define SIFIVE_TEST(obj) \ 25 + OBJECT_CHECK(SiFiveTestState, (obj), TYPE_SIFIVE_TEST) 26 + 27 + typedef struct SiFiveTestState { 28 + /*< private >*/ 29 + SysBusDevice parent_obj; 30 + 31 + /*< public >*/ 32 + MemoryRegion mmio; 33 + } SiFiveTestState; 34 + 35 + enum { 36 + FINISHER_FAIL = 0x3333, 37 + FINISHER_PASS = 0x5555 38 + }; 39 + 40 + DeviceState *sifive_test_create(hwaddr addr); 41 + 42 + #endif
+69
include/hw/riscv/sifive_u.h
··· 1 + /* 2 + * SiFive U series machine interface 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #ifndef HW_SIFIVE_U_H 20 + #define HW_SIFIVE_U_H 21 + 22 + #define TYPE_SIFIVE_U "riscv.sifive_u" 23 + 24 + #define SIFIVE_U(obj) \ 25 + OBJECT_CHECK(SiFiveUState, (obj), TYPE_SIFIVE_U) 26 + 27 + typedef struct SiFiveUState { 28 + /*< private >*/ 29 + SysBusDevice parent_obj; 30 + 31 + /*< public >*/ 32 + RISCVHartArrayState soc; 33 + DeviceState *plic; 34 + void *fdt; 35 + int fdt_size; 36 + } SiFiveUState; 37 + 38 + enum { 39 + SIFIVE_U_DEBUG, 40 + SIFIVE_U_MROM, 41 + SIFIVE_U_CLINT, 42 + SIFIVE_U_PLIC, 43 + SIFIVE_U_UART0, 44 + SIFIVE_U_UART1, 45 + SIFIVE_U_DRAM 46 + }; 47 + 48 + enum { 49 + SIFIVE_U_UART0_IRQ = 3, 50 + SIFIVE_U_UART1_IRQ = 4 51 + }; 52 + 53 + #define SIFIVE_U_PLIC_HART_CONFIG "MS" 54 + #define SIFIVE_U_PLIC_NUM_SOURCES 127 55 + #define SIFIVE_U_PLIC_NUM_PRIORITIES 7 56 + #define SIFIVE_U_PLIC_PRIORITY_BASE 0x0 57 + #define SIFIVE_U_PLIC_PENDING_BASE 0x1000 58 + #define SIFIVE_U_PLIC_ENABLE_BASE 0x2000 59 + #define SIFIVE_U_PLIC_ENABLE_STRIDE 0x80 60 + #define SIFIVE_U_PLIC_CONTEXT_BASE 0x200000 61 + #define SIFIVE_U_PLIC_CONTEXT_STRIDE 0x1000 62 + 63 + #if defined(TARGET_RISCV32) 64 + #define SIFIVE_U_CPU TYPE_RISCV_CPU_SIFIVE_U34 65 + #elif defined(TARGET_RISCV64) 66 + #define SIFIVE_U_CPU TYPE_RISCV_CPU_SIFIVE_U54 67 + #endif 68 + 69 + #endif
+71
include/hw/riscv/sifive_uart.h
··· 1 + /* 2 + * SiFive UART interface 3 + * 4 + * Copyright (c) 2016 Stefan O'Rear 5 + * Copyright (c) 2017 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #ifndef HW_SIFIVE_UART_H 21 + #define HW_SIFIVE_UART_H 22 + 23 + enum { 24 + SIFIVE_UART_TXFIFO = 0, 25 + SIFIVE_UART_RXFIFO = 4, 26 + SIFIVE_UART_TXCTRL = 8, 27 + SIFIVE_UART_TXMARK = 10, 28 + SIFIVE_UART_RXCTRL = 12, 29 + SIFIVE_UART_RXMARK = 14, 30 + SIFIVE_UART_IE = 16, 31 + SIFIVE_UART_IP = 20, 32 + SIFIVE_UART_DIV = 24, 33 + SIFIVE_UART_MAX = 32 34 + }; 35 + 36 + enum { 37 + SIFIVE_UART_IE_TXWM = 1, /* Transmit watermark interrupt enable */ 38 + SIFIVE_UART_IE_RXWM = 2 /* Receive watermark interrupt enable */ 39 + }; 40 + 41 + enum { 42 + SIFIVE_UART_IP_TXWM = 1, /* Transmit watermark interrupt pending */ 43 + SIFIVE_UART_IP_RXWM = 2 /* Receive watermark interrupt pending */ 44 + }; 45 + 46 + #define TYPE_SIFIVE_UART "riscv.sifive.uart" 47 + 48 + #define SIFIVE_UART(obj) \ 49 + OBJECT_CHECK(SiFiveUARTState, (obj), TYPE_SIFIVE_UART) 50 + 51 + typedef struct SiFiveUARTState { 52 + /*< private >*/ 53 + SysBusDevice parent_obj; 54 + 55 + /*< public >*/ 56 + qemu_irq irq; 57 + MemoryRegion mmio; 58 + CharBackend chr; 59 + uint8_t rx_fifo[8]; 60 + unsigned int rx_fifo_len; 61 + uint32_t ie; 62 + uint32_t ip; 63 + uint32_t txctrl; 64 + uint32_t rxctrl; 65 + uint32_t div; 66 + } SiFiveUARTState; 67 + 68 + SiFiveUARTState *sifive_uart_create(MemoryRegion *address_space, hwaddr base, 69 + Chardev *chr, qemu_irq irq); 70 + 71 + #endif
+53
include/hw/riscv/spike.h
··· 1 + /* 2 + * Spike machine interface 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #ifndef HW_SPIKE_H 20 + #define HW_SPIKE_H 21 + 22 + #define TYPE_RISCV_SPIKE_V1_09_1_BOARD "riscv.spike_v1_9_1" 23 + #define TYPE_RISCV_SPIKE_V1_10_0_BOARD "riscv.spike_v1_10" 24 + 25 + #define SPIKE(obj) \ 26 + OBJECT_CHECK(SpikeState, (obj), TYPE_RISCV_SPIKE_BOARD) 27 + 28 + typedef struct { 29 + /*< private >*/ 30 + SysBusDevice parent_obj; 31 + 32 + /*< public >*/ 33 + RISCVHartArrayState soc; 34 + void *fdt; 35 + int fdt_size; 36 + } SpikeState; 37 + 38 + 39 + enum { 40 + SPIKE_MROM, 41 + SPIKE_CLINT, 42 + SPIKE_DRAM 43 + }; 44 + 45 + #if defined(TARGET_RISCV32) 46 + #define SPIKE_V1_09_1_CPU TYPE_RISCV_CPU_RV32GCSU_V1_09_1 47 + #define SPIKE_V1_10_0_CPU TYPE_RISCV_CPU_RV32GCSU_V1_10_0 48 + #elif defined(TARGET_RISCV64) 49 + #define SPIKE_V1_09_1_CPU TYPE_RISCV_CPU_RV64GCSU_V1_09_1 50 + #define SPIKE_V1_10_0_CPU TYPE_RISCV_CPU_RV64GCSU_V1_10_0 51 + #endif 52 + 53 + #endif
+74
include/hw/riscv/virt.h
··· 1 + /* 2 + * SiFive VirtIO Board 3 + * 4 + * Copyright (c) 2017 SiFive, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #ifndef HW_VIRT_H 20 + #define HW_VIRT_H 21 + 22 + #define TYPE_RISCV_VIRT_BOARD "riscv.virt" 23 + #define VIRT(obj) \ 24 + OBJECT_CHECK(RISCVVirtState, (obj), TYPE_RISCV_VIRT_BOARD) 25 + 26 + enum { ROM_BASE = 0x1000 }; 27 + 28 + typedef struct { 29 + /*< private >*/ 30 + SysBusDevice parent_obj; 31 + 32 + /*< public >*/ 33 + RISCVHartArrayState soc; 34 + DeviceState *plic; 35 + void *fdt; 36 + int fdt_size; 37 + } RISCVVirtState; 38 + 39 + enum { 40 + VIRT_DEBUG, 41 + VIRT_MROM, 42 + VIRT_TEST, 43 + VIRT_CLINT, 44 + VIRT_PLIC, 45 + VIRT_UART0, 46 + VIRT_VIRTIO, 47 + VIRT_DRAM 48 + }; 49 + 50 + 51 + enum { 52 + UART0_IRQ = 10, 53 + VIRTIO_IRQ = 1, /* 1 to 8 */ 54 + VIRTIO_COUNT = 8, 55 + VIRTIO_NDEV = 10 56 + }; 57 + 58 + #define VIRT_PLIC_HART_CONFIG "MS" 59 + #define VIRT_PLIC_NUM_SOURCES 127 60 + #define VIRT_PLIC_NUM_PRIORITIES 7 61 + #define VIRT_PLIC_PRIORITY_BASE 0x0 62 + #define VIRT_PLIC_PENDING_BASE 0x1000 63 + #define VIRT_PLIC_ENABLE_BASE 0x2000 64 + #define VIRT_PLIC_ENABLE_STRIDE 0x80 65 + #define VIRT_PLIC_CONTEXT_BASE 0x200000 66 + #define VIRT_PLIC_CONTEXT_STRIDE 0x1000 67 + 68 + #if defined(TARGET_RISCV32) 69 + #define VIRT_CPU TYPE_RISCV_CPU_RV32GCSU_V1_10_0 70 + #elif defined(TARGET_RISCV64) 71 + #define VIRT_CPU TYPE_RISCV_CPU_RV64GCSU_V1_10_0 72 + #endif 73 + 74 + #endif
+1
include/sysemu/arch_init.h
··· 24 24 QEMU_ARCH_TRICORE = (1 << 16), 25 25 QEMU_ARCH_NIOS2 = (1 << 17), 26 26 QEMU_ARCH_HPPA = (1 << 18), 27 + QEMU_ARCH_RISCV = (1 << 19), 27 28 }; 28 29 29 30 extern const uint32_t arch_type;
+22
linux-user/elfload.c
··· 1295 1295 1296 1296 #endif /* TARGET_TILEGX */ 1297 1297 1298 + #ifdef TARGET_RISCV 1299 + 1300 + #define ELF_START_MMAP 0x80000000 1301 + #define ELF_ARCH EM_RISCV 1302 + 1303 + #ifdef TARGET_RISCV32 1304 + #define ELF_CLASS ELFCLASS32 1305 + #else 1306 + #define ELF_CLASS ELFCLASS64 1307 + #endif 1308 + 1309 + static inline void init_thread(struct target_pt_regs *regs, 1310 + struct image_info *infop) 1311 + { 1312 + regs->sepc = infop->entry; 1313 + regs->sp = infop->start_stack; 1314 + } 1315 + 1316 + #define ELF_EXEC_PAGESIZE 4096 1317 + 1318 + #endif /* TARGET_RISCV */ 1319 + 1298 1320 #ifdef TARGET_HPPA 1299 1321 1300 1322 #define ELF_START_MMAP 0x80000000
+99
linux-user/main.c
··· 3653 3653 3654 3654 #endif 3655 3655 3656 + #ifdef TARGET_RISCV 3657 + 3658 + void cpu_loop(CPURISCVState *env) 3659 + { 3660 + CPUState *cs = CPU(riscv_env_get_cpu(env)); 3661 + int trapnr, signum, sigcode; 3662 + target_ulong sigaddr; 3663 + target_ulong ret; 3664 + 3665 + for (;;) { 3666 + cpu_exec_start(cs); 3667 + trapnr = cpu_exec(cs); 3668 + cpu_exec_end(cs); 3669 + process_queued_cpu_work(cs); 3670 + 3671 + signum = 0; 3672 + sigcode = 0; 3673 + sigaddr = 0; 3674 + 3675 + switch (trapnr) { 3676 + case EXCP_INTERRUPT: 3677 + /* just indicate that signals should be handled asap */ 3678 + break; 3679 + case EXCP_ATOMIC: 3680 + cpu_exec_step_atomic(cs); 3681 + break; 3682 + case RISCV_EXCP_U_ECALL: 3683 + env->pc += 4; 3684 + if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) { 3685 + /* riscv_flush_icache_syscall is a no-op in QEMU as 3686 + self-modifying code is automatically detected */ 3687 + ret = 0; 3688 + } else { 3689 + ret = do_syscall(env, 3690 + env->gpr[xA7], 3691 + env->gpr[xA0], 3692 + env->gpr[xA1], 3693 + env->gpr[xA2], 3694 + env->gpr[xA3], 3695 + env->gpr[xA4], 3696 + env->gpr[xA5], 3697 + 0, 0); 3698 + } 3699 + if (ret == -TARGET_ERESTARTSYS) { 3700 + env->pc -= 4; 3701 + } else if (ret != -TARGET_QEMU_ESIGRETURN) { 3702 + env->gpr[xA0] = ret; 3703 + } 3704 + if (cs->singlestep_enabled) { 3705 + goto gdbstep; 3706 + } 3707 + break; 3708 + case RISCV_EXCP_ILLEGAL_INST: 3709 + signum = TARGET_SIGILL; 3710 + sigcode = TARGET_ILL_ILLOPC; 3711 + break; 3712 + case RISCV_EXCP_BREAKPOINT: 3713 + signum = TARGET_SIGTRAP; 3714 + sigcode = TARGET_TRAP_BRKPT; 3715 + sigaddr = env->pc; 3716 + break; 3717 + case RISCV_EXCP_INST_PAGE_FAULT: 3718 + case RISCV_EXCP_LOAD_PAGE_FAULT: 3719 + case RISCV_EXCP_STORE_PAGE_FAULT: 3720 + signum = TARGET_SIGSEGV; 3721 + sigcode = TARGET_SEGV_MAPERR; 3722 + break; 3723 + case EXCP_DEBUG: 3724 + gdbstep: 3725 + signum = gdb_handlesig(cs, TARGET_SIGTRAP); 3726 + sigcode = TARGET_TRAP_BRKPT; 3727 + break; 3728 + default: 3729 + EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", 3730 + trapnr); 3731 + exit(EXIT_FAILURE); 3732 + } 3733 + 3734 + if (signum) { 3735 + target_siginfo_t info = { 3736 + .si_signo = signum, 3737 + .si_errno = 0, 3738 + .si_code = sigcode, 3739 + ._sifields._sigfault._addr = sigaddr 3740 + }; 3741 + queue_signal(env, info.si_signo, QEMU_SI_KILL, &info); 3742 + } 3743 + 3744 + process_pending_signals(env); 3745 + } 3746 + } 3747 + 3748 + #endif /* TARGET_RISCV */ 3749 + 3656 3750 #ifdef TARGET_HPPA 3657 3751 3658 3752 static abi_ulong hppa_lws(CPUHPPAState *env) ··· 4802 4896 } 4803 4897 env->pc = regs->pc; 4804 4898 cpu_set_sr(env, regs->sr); 4899 + } 4900 + #elif defined(TARGET_RISCV) 4901 + { 4902 + env->pc = regs->sepc; 4903 + env->gpr[xSP] = regs->sp; 4805 4904 } 4806 4905 #elif defined(TARGET_SH4) 4807 4906 {
+287
linux-user/riscv/syscall_nr.h
··· 1 + /* 2 + * Syscall numbers from asm-generic, common for most 3 + * of recently-added arches including RISC-V. 4 + */ 5 + 6 + #define TARGET_NR_io_setup 0 7 + #define TARGET_NR_io_destroy 1 8 + #define TARGET_NR_io_submit 2 9 + #define TARGET_NR_io_cancel 3 10 + #define TARGET_NR_io_getevents 4 11 + #define TARGET_NR_setxattr 5 12 + #define TARGET_NR_lsetxattr 6 13 + #define TARGET_NR_fsetxattr 7 14 + #define TARGET_NR_getxattr 8 15 + #define TARGET_NR_lgetxattr 9 16 + #define TARGET_NR_fgetxattr 10 17 + #define TARGET_NR_listxattr 11 18 + #define TARGET_NR_llistxattr 12 19 + #define TARGET_NR_flistxattr 13 20 + #define TARGET_NR_removexattr 14 21 + #define TARGET_NR_lremovexattr 15 22 + #define TARGET_NR_fremovexattr 16 23 + #define TARGET_NR_getcwd 17 24 + #define TARGET_NR_lookup_dcookie 18 25 + #define TARGET_NR_eventfd2 19 26 + #define TARGET_NR_epoll_create1 20 27 + #define TARGET_NR_epoll_ctl 21 28 + #define TARGET_NR_epoll_pwait 22 29 + #define TARGET_NR_dup 23 30 + #define TARGET_NR_dup3 24 31 + #ifdef TARGET_RISCV32 32 + #define TARGET_NR_fcntl64 25 33 + #else 34 + #define TARGET_NR_fcntl 25 35 + #endif 36 + #define TARGET_NR_inotify_init1 26 37 + #define TARGET_NR_inotify_add_watch 27 38 + #define TARGET_NR_inotify_rm_watch 28 39 + #define TARGET_NR_ioctl 29 40 + #define TARGET_NR_ioprio_set 30 41 + #define TARGET_NR_ioprio_get 31 42 + #define TARGET_NR_flock 32 43 + #define TARGET_NR_mknodat 33 44 + #define TARGET_NR_mkdirat 34 45 + #define TARGET_NR_unlinkat 35 46 + #define TARGET_NR_symlinkat 36 47 + #define TARGET_NR_linkat 37 48 + #define TARGET_NR_renameat 38 49 + #define TARGET_NR_umount2 39 50 + #define TARGET_NR_mount 40 51 + #define TARGET_NR_pivot_root 41 52 + #define TARGET_NR_nfsservctl 42 53 + #define TARGET_NR_statfs 43 54 + #define TARGET_NR_fstatfs 44 55 + #define TARGET_NR_truncate 45 56 + #define TARGET_NR_ftruncate 46 57 + #define TARGET_NR_fallocate 47 58 + #define TARGET_NR_faccessat 48 59 + #define TARGET_NR_chdir 49 60 + #define TARGET_NR_fchdir 50 61 + #define TARGET_NR_chroot 51 62 + #define TARGET_NR_fchmod 52 63 + #define TARGET_NR_fchmodat 53 64 + #define TARGET_NR_fchownat 54 65 + #define TARGET_NR_fchown 55 66 + #define TARGET_NR_openat 56 67 + #define TARGET_NR_close 57 68 + #define TARGET_NR_vhangup 58 69 + #define TARGET_NR_pipe2 59 70 + #define TARGET_NR_quotactl 60 71 + #define TARGET_NR_getdents64 61 72 + #define TARGET_NR_lseek 62 73 + #define TARGET_NR_read 63 74 + #define TARGET_NR_write 64 75 + #define TARGET_NR_readv 65 76 + #define TARGET_NR_writev 66 77 + #define TARGET_NR_pread64 67 78 + #define TARGET_NR_pwrite64 68 79 + #define TARGET_NR_preadv 69 80 + #define TARGET_NR_pwritev 70 81 + #define TARGET_NR_sendfile 71 82 + #define TARGET_NR_pselect6 72 83 + #define TARGET_NR_ppoll 73 84 + #define TARGET_NR_signalfd4 74 85 + #define TARGET_NR_vmsplice 75 86 + #define TARGET_NR_splice 76 87 + #define TARGET_NR_tee 77 88 + #define TARGET_NR_readlinkat 78 89 + #define TARGET_NR_newfstatat 79 90 + #define TARGET_NR_fstat 80 91 + #define TARGET_NR_sync 81 92 + #define TARGET_NR_fsync 82 93 + #define TARGET_NR_fdatasync 83 94 + #define TARGET_NR_sync_file_range 84 95 + #define TARGET_NR_timerfd_create 85 96 + #define TARGET_NR_timerfd_settime 86 97 + #define TARGET_NR_timerfd_gettime 87 98 + #define TARGET_NR_utimensat 88 99 + #define TARGET_NR_acct 89 100 + #define TARGET_NR_capget 90 101 + #define TARGET_NR_capset 91 102 + #define TARGET_NR_personality 92 103 + #define TARGET_NR_exit 93 104 + #define TARGET_NR_exit_group 94 105 + #define TARGET_NR_waitid 95 106 + #define TARGET_NR_set_tid_address 96 107 + #define TARGET_NR_unshare 97 108 + #define TARGET_NR_futex 98 109 + #define TARGET_NR_set_robust_list 99 110 + #define TARGET_NR_get_robust_list 100 111 + #define TARGET_NR_nanosleep 101 112 + #define TARGET_NR_getitimer 102 113 + #define TARGET_NR_setitimer 103 114 + #define TARGET_NR_kexec_load 104 115 + #define TARGET_NR_init_module 105 116 + #define TARGET_NR_delete_module 106 117 + #define TARGET_NR_timer_create 107 118 + #define TARGET_NR_timer_gettime 108 119 + #define TARGET_NR_timer_getoverrun 109 120 + #define TARGET_NR_timer_settime 110 121 + #define TARGET_NR_timer_delete 111 122 + #define TARGET_NR_clock_settime 112 123 + #define TARGET_NR_clock_gettime 113 124 + #define TARGET_NR_clock_getres 114 125 + #define TARGET_NR_clock_nanosleep 115 126 + #define TARGET_NR_syslog 116 127 + #define TARGET_NR_ptrace 117 128 + #define TARGET_NR_sched_setparam 118 129 + #define TARGET_NR_sched_setscheduler 119 130 + #define TARGET_NR_sched_getscheduler 120 131 + #define TARGET_NR_sched_getparam 121 132 + #define TARGET_NR_sched_setaffinity 122 133 + #define TARGET_NR_sched_getaffinity 123 134 + #define TARGET_NR_sched_yield 124 135 + #define TARGET_NR_sched_get_priority_max 125 136 + #define TARGET_NR_sched_get_priority_min 126 137 + #define TARGET_NR_sched_rr_get_interval 127 138 + #define TARGET_NR_restart_syscall 128 139 + #define TARGET_NR_kill 129 140 + #define TARGET_NR_tkill 130 141 + #define TARGET_NR_tgkill 131 142 + #define TARGET_NR_sigaltstack 132 143 + #define TARGET_NR_rt_sigsuspend 133 144 + #define TARGET_NR_rt_sigaction 134 145 + #define TARGET_NR_rt_sigprocmask 135 146 + #define TARGET_NR_rt_sigpending 136 147 + #define TARGET_NR_rt_sigtimedwait 137 148 + #define TARGET_NR_rt_sigqueueinfo 138 149 + #define TARGET_NR_rt_sigreturn 139 150 + #define TARGET_NR_setpriority 140 151 + #define TARGET_NR_getpriority 141 152 + #define TARGET_NR_reboot 142 153 + #define TARGET_NR_setregid 143 154 + #define TARGET_NR_setgid 144 155 + #define TARGET_NR_setreuid 145 156 + #define TARGET_NR_setuid 146 157 + #define TARGET_NR_setresuid 147 158 + #define TARGET_NR_getresuid 148 159 + #define TARGET_NR_setresgid 149 160 + #define TARGET_NR_getresgid 150 161 + #define TARGET_NR_setfsuid 151 162 + #define TARGET_NR_setfsgid 152 163 + #define TARGET_NR_times 153 164 + #define TARGET_NR_setpgid 154 165 + #define TARGET_NR_getpgid 155 166 + #define TARGET_NR_getsid 156 167 + #define TARGET_NR_setsid 157 168 + #define TARGET_NR_getgroups 158 169 + #define TARGET_NR_setgroups 159 170 + #define TARGET_NR_uname 160 171 + #define TARGET_NR_sethostname 161 172 + #define TARGET_NR_setdomainname 162 173 + #define TARGET_NR_getrlimit 163 174 + #define TARGET_NR_setrlimit 164 175 + #define TARGET_NR_getrusage 165 176 + #define TARGET_NR_umask 166 177 + #define TARGET_NR_prctl 167 178 + #define TARGET_NR_getcpu 168 179 + #define TARGET_NR_gettimeofday 169 180 + #define TARGET_NR_settimeofday 170 181 + #define TARGET_NR_adjtimex 171 182 + #define TARGET_NR_getpid 172 183 + #define TARGET_NR_getppid 173 184 + #define TARGET_NR_getuid 174 185 + #define TARGET_NR_geteuid 175 186 + #define TARGET_NR_getgid 176 187 + #define TARGET_NR_getegid 177 188 + #define TARGET_NR_gettid 178 189 + #define TARGET_NR_sysinfo 179 190 + #define TARGET_NR_mq_open 180 191 + #define TARGET_NR_mq_unlink 181 192 + #define TARGET_NR_mq_timedsend 182 193 + #define TARGET_NR_mq_timedreceive 183 194 + #define TARGET_NR_mq_notify 184 195 + #define TARGET_NR_mq_getsetattr 185 196 + #define TARGET_NR_msgget 186 197 + #define TARGET_NR_msgctl 187 198 + #define TARGET_NR_msgrcv 188 199 + #define TARGET_NR_msgsnd 189 200 + #define TARGET_NR_semget 190 201 + #define TARGET_NR_semctl 191 202 + #define TARGET_NR_semtimedop 192 203 + #define TARGET_NR_semop 193 204 + #define TARGET_NR_shmget 194 205 + #define TARGET_NR_shmctl 195 206 + #define TARGET_NR_shmat 196 207 + #define TARGET_NR_shmdt 197 208 + #define TARGET_NR_socket 198 209 + #define TARGET_NR_socketpair 199 210 + #define TARGET_NR_bind 200 211 + #define TARGET_NR_listen 201 212 + #define TARGET_NR_accept 202 213 + #define TARGET_NR_connect 203 214 + #define TARGET_NR_getsockname 204 215 + #define TARGET_NR_getpeername 205 216 + #define TARGET_NR_sendto 206 217 + #define TARGET_NR_recvfrom 207 218 + #define TARGET_NR_setsockopt 208 219 + #define TARGET_NR_getsockopt 209 220 + #define TARGET_NR_shutdown 210 221 + #define TARGET_NR_sendmsg 211 222 + #define TARGET_NR_recvmsg 212 223 + #define TARGET_NR_readahead 213 224 + #define TARGET_NR_brk 214 225 + #define TARGET_NR_munmap 215 226 + #define TARGET_NR_mremap 216 227 + #define TARGET_NR_add_key 217 228 + #define TARGET_NR_request_key 218 229 + #define TARGET_NR_keyctl 219 230 + #define TARGET_NR_clone 220 231 + #define TARGET_NR_execve 221 232 + #ifdef TARGET_RISCV32 233 + #define TARGET_NR_mmap2 222 234 + #define TARGET_NR_fadvise64_64 223 235 + #else 236 + #define TARGET_NR_mmap 222 237 + #define TARGET_NR_fadvise64 223 238 + #endif 239 + #define TARGET_NR_swapon 224 240 + #define TARGET_NR_swapoff 225 241 + #define TARGET_NR_mprotect 226 242 + #define TARGET_NR_msync 227 243 + #define TARGET_NR_mlock 228 244 + #define TARGET_NR_munlock 229 245 + #define TARGET_NR_mlockall 230 246 + #define TARGET_NR_munlockall 231 247 + #define TARGET_NR_mincore 232 248 + #define TARGET_NR_madvise 233 249 + #define TARGET_NR_remap_file_pages 234 250 + #define TARGET_NR_mbind 235 251 + #define TARGET_NR_get_mempolicy 236 252 + #define TARGET_NR_set_mempolicy 237 253 + #define TARGET_NR_migrate_pages 238 254 + #define TARGET_NR_move_pages 239 255 + #define TARGET_NR_rt_tgsigqueueinfo 240 256 + #define TARGET_NR_perf_event_open 241 257 + #define TARGET_NR_accept4 242 258 + #define TARGET_NR_recvmmsg 243 259 + #define TARGET_NR_arch_specific_syscall 244 260 + #define TARGET_NR_wait4 260 261 + #define TARGET_NR_prlimit64 261 262 + #define TARGET_NR_fanotify_init 262 263 + #define TARGET_NR_fanotify_mark 263 264 + #define TARGET_NR_name_to_handle_at 264 265 + #define TARGET_NR_open_by_handle_at 265 266 + #define TARGET_NR_clock_adjtime 266 267 + #define TARGET_NR_syncfs 267 268 + #define TARGET_NR_setns 268 269 + #define TARGET_NR_sendmmsg 269 270 + #define TARGET_NR_process_vm_readv 270 271 + #define TARGET_NR_process_vm_writev 271 272 + #define TARGET_NR_kcmp 272 273 + #define TARGET_NR_finit_module 273 274 + #define TARGET_NR_sched_setattr 274 275 + #define TARGET_NR_sched_getattr 275 276 + #define TARGET_NR_renameat2 276 277 + #define TARGET_NR_seccomp 277 278 + #define TARGET_NR_getrandom 278 279 + #define TARGET_NR_memfd_create 279 280 + #define TARGET_NR_bpf 280 281 + #define TARGET_NR_execveat 281 282 + #define TARGET_NR_userfaultfd 282 283 + #define TARGET_NR_membarrier 283 284 + #define TARGET_NR_mlock2 284 285 + #define TARGET_NR_copy_file_range 285 286 + 287 + #define TARGET_NR_syscalls (TARGET_NR_copy_file_range + 1)
+18
linux-user/riscv/target_cpu.h
··· 1 + #ifndef TARGET_CPU_H 2 + #define TARGET_CPU_H 3 + 4 + static inline void cpu_clone_regs(CPURISCVState *env, target_ulong newsp) 5 + { 6 + if (newsp) { 7 + env->gpr[xSP] = newsp; 8 + } 9 + 10 + env->gpr[xA0] = 0; 11 + } 12 + 13 + static inline void cpu_set_tls(CPURISCVState *env, target_ulong newtls) 14 + { 15 + env->gpr[xTP] = newtls; 16 + } 17 + 18 + #endif
+14
linux-user/riscv/target_elf.h
··· 1 + /* 2 + * This program is free software; you can redistribute it and/or modify 3 + * it under the terms of the GNU General Public License version 2 as 4 + * published by the Free Software Foundation, or (at your option) any 5 + * later version. See the COPYING file in the top-level directory. 6 + */ 7 + 8 + #ifndef RISCV_TARGET_ELF_H 9 + #define RISCV_TARGET_ELF_H 10 + static inline const char *cpu_get_model(uint32_t eflags) 11 + { 12 + return "any"; 13 + } 14 + #endif
+23
linux-user/riscv/target_signal.h
··· 1 + #ifndef TARGET_SIGNAL_H 2 + #define TARGET_SIGNAL_H 3 + 4 + #include "cpu.h" 5 + 6 + typedef struct target_sigaltstack { 7 + abi_ulong ss_sp; 8 + abi_int ss_flags; 9 + abi_ulong ss_size; 10 + } target_stack_t; 11 + 12 + #define TARGET_SS_ONSTACK 1 13 + #define TARGET_SS_DISABLE 2 14 + 15 + #define TARGET_MINSIGSTKSZ 2048 16 + #define TARGET_SIGSTKSZ 8192 17 + 18 + static inline abi_ulong get_sp_from_cpustate(CPURISCVState *state) 19 + { 20 + return state->gpr[xSP]; 21 + } 22 + 23 + #endif /* TARGET_SIGNAL_H */
+46
linux-user/riscv/target_structs.h
··· 1 + /* 2 + * RISC-V specific structures for linux-user 3 + * 4 + * This is a copy of ../aarch64/target_structs.h atm. 5 + * 6 + */ 7 + #ifndef TARGET_STRUCTS_H 8 + #define TARGET_STRUCTS_H 9 + 10 + struct target_ipc_perm { 11 + abi_int __key; /* Key. */ 12 + abi_uint uid; /* Owner's user ID. */ 13 + abi_uint gid; /* Owner's group ID. */ 14 + abi_uint cuid; /* Creator's user ID. */ 15 + abi_uint cgid; /* Creator's group ID. */ 16 + abi_ushort mode; /* Read/write permission. */ 17 + abi_ushort __pad1; 18 + abi_ushort __seq; /* Sequence number. */ 19 + abi_ushort __pad2; 20 + abi_ulong __unused1; 21 + abi_ulong __unused2; 22 + }; 23 + 24 + struct target_shmid_ds { 25 + struct target_ipc_perm shm_perm; /* operation permission struct */ 26 + abi_long shm_segsz; /* size of segment in bytes */ 27 + abi_ulong shm_atime; /* time of last shmat() */ 28 + #if TARGET_ABI_BITS == 32 29 + abi_ulong __unused1; 30 + #endif 31 + abi_ulong shm_dtime; /* time of last shmdt() */ 32 + #if TARGET_ABI_BITS == 32 33 + abi_ulong __unused2; 34 + #endif 35 + abi_ulong shm_ctime; /* time of last change by shmctl() */ 36 + #if TARGET_ABI_BITS == 32 37 + abi_ulong __unused3; 38 + #endif 39 + abi_int shm_cpid; /* pid of creator */ 40 + abi_int shm_lpid; /* pid of last shmop */ 41 + abi_ulong shm_nattch; /* number of current attaches */ 42 + abi_ulong __unused4; 43 + abi_ulong __unused5; 44 + }; 45 + 46 + #endif
+56
linux-user/riscv/target_syscall.h
··· 1 + /* 2 + * This struct defines the way the registers are stored on the 3 + * stack during a system call. 4 + * 5 + * Reference: linux/arch/riscv/include/uapi/asm/ptrace.h 6 + */ 7 + 8 + struct target_pt_regs { 9 + abi_long sepc; 10 + abi_long ra; 11 + abi_long sp; 12 + abi_long gp; 13 + abi_long tp; 14 + abi_long t0; 15 + abi_long t1; 16 + abi_long t2; 17 + abi_long s0; 18 + abi_long s1; 19 + abi_long a0; 20 + abi_long a1; 21 + abi_long a2; 22 + abi_long a3; 23 + abi_long a4; 24 + abi_long a5; 25 + abi_long a6; 26 + abi_long a7; 27 + abi_long s2; 28 + abi_long s3; 29 + abi_long s4; 30 + abi_long s5; 31 + abi_long s6; 32 + abi_long s7; 33 + abi_long s8; 34 + abi_long s9; 35 + abi_long s10; 36 + abi_long s11; 37 + abi_long t3; 38 + abi_long t4; 39 + abi_long t5; 40 + abi_long t6; 41 + }; 42 + 43 + #ifdef TARGET_RISCV32 44 + #define UNAME_MACHINE "riscv32" 45 + #else 46 + #define UNAME_MACHINE "riscv64" 47 + #endif 48 + #define UNAME_MINIMUM_RELEASE "3.8.0" 49 + 50 + #define TARGET_MINSIGSTKSZ 2048 51 + #define TARGET_MLOCKALL_MCL_CURRENT 1 52 + #define TARGET_MLOCKALL_MCL_FUTURE 2 53 + 54 + /* clone(flags, newsp, ptidptr, tls, ctidptr) for RISC-V */ 55 + /* This comes from linux/kernel/fork.c, CONFIG_CLONE_BACKWARDS */ 56 + #define TARGET_CLONE_BACKWARDS
+222
linux-user/riscv/termbits.h
··· 1 + /* from asm/termbits.h */ 2 + /* NOTE: exactly the same as i386 */ 3 + 4 + #define TARGET_NCCS 19 5 + 6 + struct target_termios { 7 + unsigned int c_iflag; /* input mode flags */ 8 + unsigned int c_oflag; /* output mode flags */ 9 + unsigned int c_cflag; /* control mode flags */ 10 + unsigned int c_lflag; /* local mode flags */ 11 + unsigned char c_line; /* line discipline */ 12 + unsigned char c_cc[TARGET_NCCS]; /* control characters */ 13 + }; 14 + 15 + /* c_iflag bits */ 16 + #define TARGET_IGNBRK 0000001 17 + #define TARGET_BRKINT 0000002 18 + #define TARGET_IGNPAR 0000004 19 + #define TARGET_PARMRK 0000010 20 + #define TARGET_INPCK 0000020 21 + #define TARGET_ISTRIP 0000040 22 + #define TARGET_INLCR 0000100 23 + #define TARGET_IGNCR 0000200 24 + #define TARGET_ICRNL 0000400 25 + #define TARGET_IUCLC 0001000 26 + #define TARGET_IXON 0002000 27 + #define TARGET_IXANY 0004000 28 + #define TARGET_IXOFF 0010000 29 + #define TARGET_IMAXBEL 0020000 30 + #define TARGET_IUTF8 0040000 31 + 32 + /* c_oflag bits */ 33 + #define TARGET_OPOST 0000001 34 + #define TARGET_OLCUC 0000002 35 + #define TARGET_ONLCR 0000004 36 + #define TARGET_OCRNL 0000010 37 + #define TARGET_ONOCR 0000020 38 + #define TARGET_ONLRET 0000040 39 + #define TARGET_OFILL 0000100 40 + #define TARGET_OFDEL 0000200 41 + #define TARGET_NLDLY 0000400 42 + #define TARGET_NL0 0000000 43 + #define TARGET_NL1 0000400 44 + #define TARGET_CRDLY 0003000 45 + #define TARGET_CR0 0000000 46 + #define TARGET_CR1 0001000 47 + #define TARGET_CR2 0002000 48 + #define TARGET_CR3 0003000 49 + #define TARGET_TABDLY 0014000 50 + #define TARGET_TAB0 0000000 51 + #define TARGET_TAB1 0004000 52 + #define TARGET_TAB2 0010000 53 + #define TARGET_TAB3 0014000 54 + #define TARGET_XTABS 0014000 55 + #define TARGET_BSDLY 0020000 56 + #define TARGET_BS0 0000000 57 + #define TARGET_BS1 0020000 58 + #define TARGET_VTDLY 0040000 59 + #define TARGET_VT0 0000000 60 + #define TARGET_VT1 0040000 61 + #define TARGET_FFDLY 0100000 62 + #define TARGET_FF0 0000000 63 + #define TARGET_FF1 0100000 64 + 65 + /* c_cflag bit meaning */ 66 + #define TARGET_CBAUD 0010017 67 + #define TARGET_B0 0000000 /* hang up */ 68 + #define TARGET_B50 0000001 69 + #define TARGET_B75 0000002 70 + #define TARGET_B110 0000003 71 + #define TARGET_B134 0000004 72 + #define TARGET_B150 0000005 73 + #define TARGET_B200 0000006 74 + #define TARGET_B300 0000007 75 + #define TARGET_B600 0000010 76 + #define TARGET_B1200 0000011 77 + #define TARGET_B1800 0000012 78 + #define TARGET_B2400 0000013 79 + #define TARGET_B4800 0000014 80 + #define TARGET_B9600 0000015 81 + #define TARGET_B19200 0000016 82 + #define TARGET_B38400 0000017 83 + #define TARGET_EXTA B19200 84 + #define TARGET_EXTB B38400 85 + #define TARGET_CSIZE 0000060 86 + #define TARGET_CS5 0000000 87 + #define TARGET_CS6 0000020 88 + #define TARGET_CS7 0000040 89 + #define TARGET_CS8 0000060 90 + #define TARGET_CSTOPB 0000100 91 + #define TARGET_CREAD 0000200 92 + #define TARGET_PARENB 0000400 93 + #define TARGET_PARODD 0001000 94 + #define TARGET_HUPCL 0002000 95 + #define TARGET_CLOCAL 0004000 96 + #define TARGET_CBAUDEX 0010000 97 + #define TARGET_B57600 0010001 98 + #define TARGET_B115200 0010002 99 + #define TARGET_B230400 0010003 100 + #define TARGET_B460800 0010004 101 + #define TARGET_CIBAUD 002003600000 /* input baud rate (not used) */ 102 + #define TARGET_CMSPAR 010000000000 /* mark or space (stick) parity */ 103 + #define TARGET_CRTSCTS 020000000000 /* flow control */ 104 + 105 + /* c_lflag bits */ 106 + #define TARGET_ISIG 0000001 107 + #define TARGET_ICANON 0000002 108 + #define TARGET_XCASE 0000004 109 + #define TARGET_ECHO 0000010 110 + #define TARGET_ECHOE 0000020 111 + #define TARGET_ECHOK 0000040 112 + #define TARGET_ECHONL 0000100 113 + #define TARGET_NOFLSH 0000200 114 + #define TARGET_TOSTOP 0000400 115 + #define TARGET_ECHOCTL 0001000 116 + #define TARGET_ECHOPRT 0002000 117 + #define TARGET_ECHOKE 0004000 118 + #define TARGET_FLUSHO 0010000 119 + #define TARGET_PENDIN 0040000 120 + #define TARGET_IEXTEN 0100000 121 + 122 + /* c_cc character offsets */ 123 + #define TARGET_VINTR 0 124 + #define TARGET_VQUIT 1 125 + #define TARGET_VERASE 2 126 + #define TARGET_VKILL 3 127 + #define TARGET_VEOF 4 128 + #define TARGET_VTIME 5 129 + #define TARGET_VMIN 6 130 + #define TARGET_VSWTC 7 131 + #define TARGET_VSTART 8 132 + #define TARGET_VSTOP 9 133 + #define TARGET_VSUSP 10 134 + #define TARGET_VEOL 11 135 + #define TARGET_VREPRINT 12 136 + #define TARGET_VDISCARD 13 137 + #define TARGET_VWERASE 14 138 + #define TARGET_VLNEXT 15 139 + #define TARGET_VEOL2 16 140 + 141 + /* ioctls */ 142 + 143 + #define TARGET_TCGETS 0x5401 144 + #define TARGET_TCSETS 0x5402 145 + #define TARGET_TCSETSW 0x5403 146 + #define TARGET_TCSETSF 0x5404 147 + #define TARGET_TCGETA 0x5405 148 + #define TARGET_TCSETA 0x5406 149 + #define TARGET_TCSETAW 0x5407 150 + #define TARGET_TCSETAF 0x5408 151 + #define TARGET_TCSBRK 0x5409 152 + #define TARGET_TCXONC 0x540A 153 + #define TARGET_TCFLSH 0x540B 154 + 155 + #define TARGET_TIOCEXCL 0x540C 156 + #define TARGET_TIOCNXCL 0x540D 157 + #define TARGET_TIOCSCTTY 0x540E 158 + #define TARGET_TIOCGPGRP 0x540F 159 + #define TARGET_TIOCSPGRP 0x5410 160 + #define TARGET_TIOCOUTQ 0x5411 161 + #define TARGET_TIOCSTI 0x5412 162 + #define TARGET_TIOCGWINSZ 0x5413 163 + #define TARGET_TIOCSWINSZ 0x5414 164 + #define TARGET_TIOCMGET 0x5415 165 + #define TARGET_TIOCMBIS 0x5416 166 + #define TARGET_TIOCMBIC 0x5417 167 + #define TARGET_TIOCMSET 0x5418 168 + #define TARGET_TIOCGSOFTCAR 0x5419 169 + #define TARGET_TIOCSSOFTCAR 0x541A 170 + #define TARGET_FIONREAD 0x541B 171 + #define TARGET_TIOCINQ TARGET_FIONREAD 172 + #define TARGET_TIOCLINUX 0x541C 173 + #define TARGET_TIOCCONS 0x541D 174 + #define TARGET_TIOCGSERIAL 0x541E 175 + #define TARGET_TIOCSSERIAL 0x541F 176 + #define TARGET_TIOCPKT 0x5420 177 + #define TARGET_FIONBIO 0x5421 178 + #define TARGET_TIOCNOTTY 0x5422 179 + #define TARGET_TIOCSETD 0x5423 180 + #define TARGET_TIOCGETD 0x5424 181 + #define TARGET_TCSBRKP 0x5425 /* Needed for POSIX tcsendbreak() */ 182 + #define TARGET_TIOCTTYGSTRUCT 0x5426 /* For debugging only */ 183 + #define TARGET_TIOCSBRK 0x5427 /* BSD compatibility */ 184 + #define TARGET_TIOCCBRK 0x5428 /* BSD compatibility */ 185 + #define TARGET_TIOCGSID 0x5429 /* Return the session ID of FD */ 186 + #define TARGET_TIOCGPTN TARGET_IOR('T', 0x30, unsigned int) 187 + /* Get Pty Number (of pty-mux device) */ 188 + #define TARGET_TIOCSPTLCK TARGET_IOW('T', 0x31, int) 189 + /* Lock/unlock Pty */ 190 + #define TARGET_TIOCGPTPEER TARGET_IO('T', 0x41) 191 + /* Safely open the slave */ 192 + 193 + #define TARGET_FIONCLEX 0x5450 /* these numbers need to be adjusted. */ 194 + #define TARGET_FIOCLEX 0x5451 195 + #define TARGET_FIOASYNC 0x5452 196 + #define TARGET_TIOCSERCONFIG 0x5453 197 + #define TARGET_TIOCSERGWILD 0x5454 198 + #define TARGET_TIOCSERSWILD 0x5455 199 + #define TARGET_TIOCGLCKTRMIOS 0x5456 200 + #define TARGET_TIOCSLCKTRMIOS 0x5457 201 + #define TARGET_TIOCSERGSTRUCT 0x5458 /* For debugging only */ 202 + #define TARGET_TIOCSERGETLSR 0x5459 /* Get line status register */ 203 + #define TARGET_TIOCSERGETMULTI 0x545A /* Get multiport config */ 204 + #define TARGET_TIOCSERSETMULTI 0x545B /* Set multiport config */ 205 + 206 + #define TARGET_TIOCMIWAIT 0x545C 207 + /* wait for a change on serial input line(s) */ 208 + #define TARGET_TIOCGICOUNT 0x545D 209 + /* read serial port inline interrupt counts */ 210 + #define TARGET_TIOCGHAYESESP 0x545E /* Get Hayes ESP configuration */ 211 + #define TARGET_TIOCSHAYESESP 0x545F /* Set Hayes ESP configuration */ 212 + 213 + /* Used for packet mode */ 214 + #define TARGET_TIOCPKT_DATA 0 215 + #define TARGET_TIOCPKT_FLUSHREAD 1 216 + #define TARGET_TIOCPKT_FLUSHWRITE 2 217 + #define TARGET_TIOCPKT_STOP 4 218 + #define TARGET_TIOCPKT_START 8 219 + #define TARGET_TIOCPKT_NOSTOP 16 220 + #define TARGET_TIOCPKT_DOSTOP 32 221 + 222 + #define TARGET_TIOCSER_TEMT 0x01 /* Transmitter physically empty */
+202 -1
linux-user/signal.c
··· 535 535 * up the signal frame. oldsig is the signal we were trying to handle 536 536 * at the point of failure. 537 537 */ 538 + #if !defined(TARGET_RISCV) 538 539 static void force_sigsegv(int oldsig) 539 540 { 540 541 if (oldsig == SIGSEGV) { ··· 545 546 } 546 547 force_sig(TARGET_SIGSEGV); 547 548 } 549 + #endif 550 + 548 551 #endif 549 552 550 553 /* abort execution with signal */ ··· 6385 6388 return -TARGET_QEMU_ESIGRETURN; 6386 6389 } 6387 6390 6391 + #elif defined(TARGET_RISCV) 6392 + 6393 + /* Signal handler invocation must be transparent for the code being 6394 + interrupted. Complete CPU (hart) state is saved on entry and restored 6395 + before returning from the handler. Process sigmask is also saved to block 6396 + signals while the handler is running. The handler gets its own stack, 6397 + which also doubles as storage for the CPU state and sigmask. 6398 + 6399 + The code below is qemu re-implementation of arch/riscv/kernel/signal.c */ 6400 + 6401 + struct target_sigcontext { 6402 + abi_long pc; 6403 + abi_long gpr[31]; /* x0 is not present, so all offsets must be -1 */ 6404 + uint64_t fpr[32]; 6405 + uint32_t fcsr; 6406 + }; /* cf. riscv-linux:arch/riscv/include/uapi/asm/ptrace.h */ 6407 + 6408 + struct target_ucontext { 6409 + unsigned long uc_flags; 6410 + struct target_ucontext *uc_link; 6411 + target_stack_t uc_stack; 6412 + struct target_sigcontext uc_mcontext; 6413 + target_sigset_t uc_sigmask; 6414 + }; 6415 + 6416 + struct target_rt_sigframe { 6417 + uint32_t tramp[2]; /* not in kernel, which uses VDSO instead */ 6418 + struct target_siginfo info; 6419 + struct target_ucontext uc; 6420 + }; 6421 + 6422 + static abi_ulong get_sigframe(struct target_sigaction *ka, 6423 + CPURISCVState *regs, size_t framesize) 6424 + { 6425 + abi_ulong sp = regs->gpr[xSP]; 6426 + int onsigstack = on_sig_stack(sp); 6427 + 6428 + /* redzone */ 6429 + /* This is the X/Open sanctioned signal stack switching. */ 6430 + if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !onsigstack) { 6431 + sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 6432 + } 6433 + 6434 + sp -= framesize; 6435 + sp &= ~3UL; /* align sp on 4-byte boundary */ 6436 + 6437 + /* If we are on the alternate signal stack and would overflow it, don't. 6438 + Return an always-bogus address instead so we will die with SIGSEGV. */ 6439 + if (onsigstack && !likely(on_sig_stack(sp))) { 6440 + return -1L; 6441 + } 6442 + 6443 + return sp; 6444 + } 6445 + 6446 + static void setup_sigcontext(struct target_sigcontext *sc, CPURISCVState *env) 6447 + { 6448 + int i; 6449 + 6450 + __put_user(env->pc, &sc->pc); 6451 + 6452 + for (i = 1; i < 32; i++) { 6453 + __put_user(env->gpr[i], &sc->gpr[i - 1]); 6454 + } 6455 + for (i = 0; i < 32; i++) { 6456 + __put_user(env->fpr[i], &sc->fpr[i]); 6457 + } 6458 + 6459 + uint32_t fcsr = csr_read_helper(env, CSR_FCSR); /*riscv_get_fcsr(env);*/ 6460 + __put_user(fcsr, &sc->fcsr); 6461 + } 6462 + 6463 + static void setup_ucontext(struct target_ucontext *uc, 6464 + CPURISCVState *env, target_sigset_t *set) 6465 + { 6466 + abi_ulong ss_sp = (target_ulong)target_sigaltstack_used.ss_sp; 6467 + abi_ulong ss_flags = sas_ss_flags(env->gpr[xSP]); 6468 + abi_ulong ss_size = target_sigaltstack_used.ss_size; 6469 + 6470 + __put_user(0, &(uc->uc_flags)); 6471 + __put_user(0, &(uc->uc_link)); 6472 + 6473 + __put_user(ss_sp, &(uc->uc_stack.ss_sp)); 6474 + __put_user(ss_flags, &(uc->uc_stack.ss_flags)); 6475 + __put_user(ss_size, &(uc->uc_stack.ss_size)); 6476 + 6477 + int i; 6478 + for (i = 0; i < TARGET_NSIG_WORDS; i++) { 6479 + __put_user(set->sig[i], &(uc->uc_sigmask.sig[i])); 6480 + } 6481 + 6482 + setup_sigcontext(&uc->uc_mcontext, env); 6483 + } 6484 + 6485 + static inline void install_sigtramp(uint32_t *tramp) 6486 + { 6487 + __put_user(0x08b00893, tramp + 0); /* li a7, 139 = __NR_rt_sigreturn */ 6488 + __put_user(0x00000073, tramp + 1); /* ecall */ 6489 + } 6490 + 6491 + static void setup_rt_frame(int sig, struct target_sigaction *ka, 6492 + target_siginfo_t *info, 6493 + target_sigset_t *set, CPURISCVState *env) 6494 + { 6495 + abi_ulong frame_addr; 6496 + struct target_rt_sigframe *frame; 6497 + 6498 + frame_addr = get_sigframe(ka, env, sizeof(*frame)); 6499 + trace_user_setup_rt_frame(env, frame_addr); 6500 + 6501 + if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 6502 + goto badframe; 6503 + } 6504 + 6505 + setup_ucontext(&frame->uc, env, set); 6506 + tswap_siginfo(&frame->info, info); 6507 + install_sigtramp(frame->tramp); 6508 + 6509 + env->pc = ka->_sa_handler; 6510 + env->gpr[xSP] = frame_addr; 6511 + env->gpr[xA0] = sig; 6512 + env->gpr[xA1] = frame_addr + offsetof(struct target_rt_sigframe, info); 6513 + env->gpr[xA2] = frame_addr + offsetof(struct target_rt_sigframe, uc); 6514 + env->gpr[xRA] = frame_addr + offsetof(struct target_rt_sigframe, tramp); 6515 + 6516 + return; 6517 + 6518 + badframe: 6519 + unlock_user_struct(frame, frame_addr, 1); 6520 + if (sig == TARGET_SIGSEGV) { 6521 + ka->_sa_handler = TARGET_SIG_DFL; 6522 + } 6523 + force_sig(TARGET_SIGSEGV); 6524 + } 6525 + 6526 + static void restore_sigcontext(CPURISCVState *env, struct target_sigcontext *sc) 6527 + { 6528 + int i; 6529 + 6530 + __get_user(env->pc, &sc->pc); 6531 + 6532 + for (i = 1; i < 32; ++i) { 6533 + __get_user(env->gpr[i], &sc->gpr[i - 1]); 6534 + } 6535 + for (i = 0; i < 32; ++i) { 6536 + __get_user(env->fpr[i], &sc->fpr[i]); 6537 + } 6538 + 6539 + uint32_t fcsr; 6540 + __get_user(fcsr, &sc->fcsr); 6541 + csr_write_helper(env, fcsr, CSR_FCSR); 6542 + } 6543 + 6544 + static void restore_ucontext(CPURISCVState *env, struct target_ucontext *uc) 6545 + { 6546 + sigset_t blocked; 6547 + target_sigset_t target_set; 6548 + int i; 6549 + 6550 + target_sigemptyset(&target_set); 6551 + for (i = 0; i < TARGET_NSIG_WORDS; i++) { 6552 + __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i])); 6553 + } 6554 + 6555 + target_to_host_sigset_internal(&blocked, &target_set); 6556 + set_sigmask(&blocked); 6557 + 6558 + restore_sigcontext(env, &uc->uc_mcontext); 6559 + } 6560 + 6561 + long do_rt_sigreturn(CPURISCVState *env) 6562 + { 6563 + struct target_rt_sigframe *frame; 6564 + abi_ulong frame_addr; 6565 + 6566 + frame_addr = env->gpr[xSP]; 6567 + trace_user_do_sigreturn(env, frame_addr); 6568 + if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 6569 + goto badframe; 6570 + } 6571 + 6572 + restore_ucontext(env, &frame->uc); 6573 + 6574 + if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe, 6575 + uc.uc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT) { 6576 + goto badframe; 6577 + } 6578 + 6579 + unlock_user_struct(frame, frame_addr, 0); 6580 + return -TARGET_QEMU_ESIGRETURN; 6581 + 6582 + badframe: 6583 + unlock_user_struct(frame, frame_addr, 0); 6584 + force_sig(TARGET_SIGSEGV); 6585 + return 0; 6586 + } 6587 + 6388 6588 #elif defined(TARGET_HPPA) 6389 6589 6390 6590 struct target_sigcontext { ··· 6676 6876 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \ 6677 6877 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX) \ 6678 6878 || defined(TARGET_PPC64) || defined(TARGET_HPPA) \ 6679 - || defined(TARGET_NIOS2) || defined(TARGET_X86_64) 6879 + || defined(TARGET_NIOS2) || defined(TARGET_X86_64) \ 6880 + || defined(TARGET_RISCV) 6680 6881 /* These targets do not have traditional signals. */ 6681 6882 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env); 6682 6883 #else
+2
linux-user/syscall.c
··· 8555 8555 case TARGET_NR_ioctl: 8556 8556 ret = do_ioctl(arg1, arg2, arg3); 8557 8557 break; 8558 + #ifdef TARGET_NR_fcntl 8558 8559 case TARGET_NR_fcntl: 8559 8560 ret = do_fcntl(arg1, arg2, arg3); 8560 8561 break; 8562 + #endif 8561 8563 #ifdef TARGET_NR_mpx 8562 8564 case TARGET_NR_mpx: 8563 8565 goto unimplemented;
+8 -5
linux-user/syscall_defs.h
··· 71 71 || defined(TARGET_M68K) || defined(TARGET_CRIS) \ 72 72 || defined(TARGET_UNICORE32) || defined(TARGET_S390X) \ 73 73 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX) \ 74 - || defined(TARGET_NIOS2) 74 + || defined(TARGET_NIOS2) || defined(TARGET_RISCV) 75 75 76 76 #define TARGET_IOC_SIZEBITS 14 77 77 #define TARGET_IOC_DIRBITS 2 ··· 435 435 || defined(TARGET_M68K) || defined(TARGET_ALPHA) || defined(TARGET_CRIS) \ 436 436 || defined(TARGET_MICROBLAZE) || defined(TARGET_UNICORE32) \ 437 437 || defined(TARGET_S390X) || defined(TARGET_OPENRISC) \ 438 - || defined(TARGET_TILEGX) || defined(TARGET_HPPA) || defined(TARGET_NIOS2) 438 + || defined(TARGET_TILEGX) || defined(TARGET_HPPA) || defined(TARGET_NIOS2) \ 439 + || defined(TARGET_RISCV) 439 440 440 441 #if defined(TARGET_SPARC) 441 442 #define TARGET_SA_NOCLDSTOP 8u ··· 2093 2094 unsigned int __unused[2]; 2094 2095 }; 2095 2096 #elif defined(TARGET_OPENRISC) || defined(TARGET_TILEGX) || \ 2096 - defined(TARGET_NIOS2) 2097 + defined(TARGET_NIOS2) || defined(TARGET_RISCV) 2097 2098 2098 2099 /* These are the asm-generic versions of the stat and stat64 structures */ 2099 2100 ··· 2120 2121 unsigned int __unused5; 2121 2122 }; 2122 2123 2124 + #if !defined(TARGET_RISCV64) 2123 2125 #define TARGET_HAS_STRUCT_STAT64 2124 2126 struct target_stat64 { 2125 2127 uint64_t st_dev; ··· 2143 2145 unsigned int __unused4; 2144 2146 unsigned int __unused5; 2145 2147 }; 2148 + #endif 2146 2149 2147 2150 #elif defined(TARGET_HPPA) 2148 2151 ··· 2258 2261 uint32_t f_spare[6]; 2259 2262 }; 2260 2263 #elif (defined(TARGET_PPC64) || defined(TARGET_X86_64) || \ 2261 - defined(TARGET_SPARC64) || defined(TARGET_AARCH64)) && \ 2262 - !defined(TARGET_ABI32) 2264 + defined(TARGET_SPARC64) || defined(TARGET_AARCH64) || \ 2265 + defined(TARGET_RISCV)) && !defined(TARGET_ABI32) 2263 2266 struct target_statfs { 2264 2267 abi_long f_type; 2265 2268 abi_long f_bsize;
+16 -1
qapi/misc.json
··· 320 320 # 321 321 # @s390: since 2.12 322 322 # 323 + # @riscv: since 2.12 324 + # 323 325 # Since: 2.6 324 326 ## 325 327 { 'enum': 'CpuInfoArch', 326 - 'data': ['x86', 'sparc', 'ppc', 'mips', 'tricore', 's390', 'other' ] } 328 + 'data': ['x86', 'sparc', 'ppc', 'mips', 'tricore', 's390', 'riscv', 'other' ] } 327 329 328 330 ## 329 331 # @CpuInfo: ··· 363 365 'mips': 'CpuInfoMIPS', 364 366 'tricore': 'CpuInfoTricore', 365 367 's390': 'CpuInfoS390', 368 + 'riscv': 'CpuInfoRISCV', 366 369 'other': 'CpuInfoOther' } } 367 370 368 371 ## ··· 423 426 { 'struct': 'CpuInfoTricore', 'data': { 'PC': 'int' } } 424 427 425 428 ## 429 + # @CpuInfoRISCV: 430 + # 431 + # Additional information about a virtual RISCV CPU 432 + # 433 + # @pc: the instruction pointer 434 + # 435 + # Since 2.12 436 + ## 437 + { 'struct': 'CpuInfoRISCV', 'data': { 'pc': 'int' } } 438 + 439 + ## 426 440 # @CpuInfoOther: 427 441 # 428 442 # No additional information is available about the virtual CPU ··· 533 547 'mips': 'CpuInfoOther', 534 548 'tricore': 'CpuInfoOther', 535 549 's390': 'CpuInfoS390', 550 + 'riscv': 'CpuInfoRISCV', 536 551 'other': 'CpuInfoOther' } } 537 552 538 553 ##
+12 -1
scripts/qemu-binfmt-conf.sh
··· 4 4 5 5 qemu_target_list="i386 i486 alpha arm armeb sparc32plus ppc ppc64 ppc64le m68k \ 6 6 mips mipsel mipsn32 mipsn32el mips64 mips64el \ 7 - sh4 sh4eb s390x aarch64 aarch64_be hppa" 7 + sh4 sh4eb s390x aarch64 aarch64_be hppa riscv32 riscv64" 8 8 9 9 i386_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00' 10 10 i386_mask='\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' ··· 100 100 hppa_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff' 101 101 hppa_family=hppa 102 102 103 + riscv32_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00' 104 + riscv32_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' 105 + riscv32_family=riscv 106 + 107 + riscv64_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00' 108 + riscv64_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' 109 + riscv64_family=riscv 110 + 103 111 qemu_get_family() { 104 112 cpu=${HOST_ARCH:-$(uname -m)} 105 113 case "$cpu" in ··· 123 131 ;; 124 132 sparc*) 125 133 echo "sparc" 134 + ;; 135 + riscv*) 136 + echo "riscv" 126 137 ;; 127 138 *) 128 139 echo "$cpu"
+1
target/riscv/Makefile.objs
··· 1 + obj-y += translate.o op_helper.o helper.o cpu.o fpu_helper.o gdbstub.o pmp.o
+432
target/riscv/cpu.c
··· 1 + /* 2 + * QEMU RISC-V CPU 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #include "qemu/osdep.h" 21 + #include "qemu/log.h" 22 + #include "cpu.h" 23 + #include "exec/exec-all.h" 24 + #include "qapi/error.h" 25 + #include "migration/vmstate.h" 26 + 27 + /* RISC-V CPU definitions */ 28 + 29 + static const char riscv_exts[26] = "IMAFDQECLBJTPVNSUHKORWXYZG"; 30 + 31 + const char * const riscv_int_regnames[] = { 32 + "zero", "ra ", "sp ", "gp ", "tp ", "t0 ", "t1 ", "t2 ", 33 + "s0 ", "s1 ", "a0 ", "a1 ", "a2 ", "a3 ", "a4 ", "a5 ", 34 + "a6 ", "a7 ", "s2 ", "s3 ", "s4 ", "s5 ", "s6 ", "s7 ", 35 + "s8 ", "s9 ", "s10 ", "s11 ", "t3 ", "t4 ", "t5 ", "t6 " 36 + }; 37 + 38 + const char * const riscv_fpr_regnames[] = { 39 + "ft0 ", "ft1 ", "ft2 ", "ft3 ", "ft4 ", "ft5 ", "ft6 ", "ft7 ", 40 + "fs0 ", "fs1 ", "fa0 ", "fa1 ", "fa2 ", "fa3 ", "fa4 ", "fa5 ", 41 + "fa6 ", "fa7 ", "fs2 ", "fs3 ", "fs4 ", "fs5 ", "fs6 ", "fs7 ", 42 + "fs8 ", "fs9 ", "fs10", "fs11", "ft8 ", "ft9 ", "ft10", "ft11" 43 + }; 44 + 45 + const char * const riscv_excp_names[] = { 46 + "misaligned_fetch", 47 + "fault_fetch", 48 + "illegal_instruction", 49 + "breakpoint", 50 + "misaligned_load", 51 + "fault_load", 52 + "misaligned_store", 53 + "fault_store", 54 + "user_ecall", 55 + "supervisor_ecall", 56 + "hypervisor_ecall", 57 + "machine_ecall", 58 + "exec_page_fault", 59 + "load_page_fault", 60 + "reserved", 61 + "store_page_fault" 62 + }; 63 + 64 + const char * const riscv_intr_names[] = { 65 + "u_software", 66 + "s_software", 67 + "h_software", 68 + "m_software", 69 + "u_timer", 70 + "s_timer", 71 + "h_timer", 72 + "m_timer", 73 + "u_external", 74 + "s_external", 75 + "h_external", 76 + "m_external", 77 + "coprocessor", 78 + "host" 79 + }; 80 + 81 + typedef struct RISCVCPUInfo { 82 + const int bit_widths; 83 + const char *name; 84 + void (*initfn)(Object *obj); 85 + } RISCVCPUInfo; 86 + 87 + static void set_misa(CPURISCVState *env, target_ulong misa) 88 + { 89 + env->misa = misa; 90 + } 91 + 92 + static void set_versions(CPURISCVState *env, int user_ver, int priv_ver) 93 + { 94 + env->user_ver = user_ver; 95 + env->priv_ver = priv_ver; 96 + } 97 + 98 + static void set_feature(CPURISCVState *env, int feature) 99 + { 100 + env->features |= (1ULL << feature); 101 + } 102 + 103 + static void set_resetvec(CPURISCVState *env, int resetvec) 104 + { 105 + #ifndef CONFIG_USER_ONLY 106 + env->resetvec = resetvec; 107 + #endif 108 + } 109 + 110 + static void riscv_any_cpu_init(Object *obj) 111 + { 112 + CPURISCVState *env = &RISCV_CPU(obj)->env; 113 + set_misa(env, RVXLEN | RVI | RVM | RVA | RVF | RVD | RVC | RVU); 114 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_10_0); 115 + set_resetvec(env, DEFAULT_RSTVEC); 116 + } 117 + 118 + static void rv32gcsu_priv1_09_1_cpu_init(Object *obj) 119 + { 120 + CPURISCVState *env = &RISCV_CPU(obj)->env; 121 + set_misa(env, RV32 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 122 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_09_1); 123 + set_resetvec(env, DEFAULT_RSTVEC); 124 + set_feature(env, RISCV_FEATURE_MMU); 125 + } 126 + 127 + static void rv32gcsu_priv1_10_0_cpu_init(Object *obj) 128 + { 129 + CPURISCVState *env = &RISCV_CPU(obj)->env; 130 + set_misa(env, RV32 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 131 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_10_0); 132 + set_resetvec(env, DEFAULT_RSTVEC); 133 + set_feature(env, RISCV_FEATURE_MMU); 134 + } 135 + 136 + static void rv32imacu_nommu_cpu_init(Object *obj) 137 + { 138 + CPURISCVState *env = &RISCV_CPU(obj)->env; 139 + set_misa(env, RV32 | RVI | RVM | RVA | RVC | RVU); 140 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_10_0); 141 + set_resetvec(env, DEFAULT_RSTVEC); 142 + } 143 + 144 + static void rv64gcsu_priv1_09_1_cpu_init(Object *obj) 145 + { 146 + CPURISCVState *env = &RISCV_CPU(obj)->env; 147 + set_misa(env, RV64 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 148 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_09_1); 149 + set_resetvec(env, DEFAULT_RSTVEC); 150 + set_feature(env, RISCV_FEATURE_MMU); 151 + } 152 + 153 + static void rv64gcsu_priv1_10_0_cpu_init(Object *obj) 154 + { 155 + CPURISCVState *env = &RISCV_CPU(obj)->env; 156 + set_misa(env, RV64 | RVI | RVM | RVA | RVF | RVD | RVC | RVS | RVU); 157 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_10_0); 158 + set_resetvec(env, DEFAULT_RSTVEC); 159 + set_feature(env, RISCV_FEATURE_MMU); 160 + } 161 + 162 + static void rv64imacu_nommu_cpu_init(Object *obj) 163 + { 164 + CPURISCVState *env = &RISCV_CPU(obj)->env; 165 + set_misa(env, RV64 | RVI | RVM | RVA | RVC | RVU); 166 + set_versions(env, USER_VERSION_2_02_0, PRIV_VERSION_1_10_0); 167 + set_resetvec(env, DEFAULT_RSTVEC); 168 + } 169 + 170 + static const RISCVCPUInfo riscv_cpus[] = { 171 + { 96, TYPE_RISCV_CPU_ANY, riscv_any_cpu_init }, 172 + { 32, TYPE_RISCV_CPU_RV32GCSU_V1_09_1, rv32gcsu_priv1_09_1_cpu_init }, 173 + { 32, TYPE_RISCV_CPU_RV32GCSU_V1_10_0, rv32gcsu_priv1_10_0_cpu_init }, 174 + { 32, TYPE_RISCV_CPU_RV32IMACU_NOMMU, rv32imacu_nommu_cpu_init }, 175 + { 32, TYPE_RISCV_CPU_SIFIVE_E31, rv32imacu_nommu_cpu_init }, 176 + { 32, TYPE_RISCV_CPU_SIFIVE_U34, rv32gcsu_priv1_10_0_cpu_init }, 177 + { 64, TYPE_RISCV_CPU_RV64GCSU_V1_09_1, rv64gcsu_priv1_09_1_cpu_init }, 178 + { 64, TYPE_RISCV_CPU_RV64GCSU_V1_10_0, rv64gcsu_priv1_10_0_cpu_init }, 179 + { 64, TYPE_RISCV_CPU_RV64IMACU_NOMMU, rv64imacu_nommu_cpu_init }, 180 + { 64, TYPE_RISCV_CPU_SIFIVE_E51, rv64imacu_nommu_cpu_init }, 181 + { 64, TYPE_RISCV_CPU_SIFIVE_U54, rv64gcsu_priv1_10_0_cpu_init }, 182 + { 0, NULL, NULL } 183 + }; 184 + 185 + static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model) 186 + { 187 + ObjectClass *oc; 188 + char *typename; 189 + char **cpuname; 190 + 191 + cpuname = g_strsplit(cpu_model, ",", 1); 192 + typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname[0]); 193 + oc = object_class_by_name(typename); 194 + g_strfreev(cpuname); 195 + g_free(typename); 196 + if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) || 197 + object_class_is_abstract(oc)) { 198 + return NULL; 199 + } 200 + return oc; 201 + } 202 + 203 + static void riscv_cpu_dump_state(CPUState *cs, FILE *f, 204 + fprintf_function cpu_fprintf, int flags) 205 + { 206 + RISCVCPU *cpu = RISCV_CPU(cs); 207 + CPURISCVState *env = &cpu->env; 208 + int i; 209 + 210 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "pc ", env->pc); 211 + #ifndef CONFIG_USER_ONLY 212 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mhartid ", env->mhartid); 213 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mstatus ", env->mstatus); 214 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mip ", 215 + (target_ulong)atomic_read(&env->mip)); 216 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mie ", env->mie); 217 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mideleg ", env->mideleg); 218 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "medeleg ", env->medeleg); 219 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mtvec ", env->mtvec); 220 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mepc ", env->mepc); 221 + cpu_fprintf(f, " %s " TARGET_FMT_lx "\n", "mcause ", env->mcause); 222 + #endif 223 + 224 + for (i = 0; i < 32; i++) { 225 + cpu_fprintf(f, " %s " TARGET_FMT_lx, 226 + riscv_int_regnames[i], env->gpr[i]); 227 + if ((i & 3) == 3) { 228 + cpu_fprintf(f, "\n"); 229 + } 230 + } 231 + for (i = 0; i < 32; i++) { 232 + cpu_fprintf(f, " %s %016" PRIx64, 233 + riscv_fpr_regnames[i], env->fpr[i]); 234 + if ((i & 3) == 3) { 235 + cpu_fprintf(f, "\n"); 236 + } 237 + } 238 + } 239 + 240 + static void riscv_cpu_set_pc(CPUState *cs, vaddr value) 241 + { 242 + RISCVCPU *cpu = RISCV_CPU(cs); 243 + CPURISCVState *env = &cpu->env; 244 + env->pc = value; 245 + } 246 + 247 + static void riscv_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb) 248 + { 249 + RISCVCPU *cpu = RISCV_CPU(cs); 250 + CPURISCVState *env = &cpu->env; 251 + env->pc = tb->pc; 252 + } 253 + 254 + static bool riscv_cpu_has_work(CPUState *cs) 255 + { 256 + #ifndef CONFIG_USER_ONLY 257 + RISCVCPU *cpu = RISCV_CPU(cs); 258 + CPURISCVState *env = &cpu->env; 259 + /* 260 + * Definition of the WFI instruction requires it to ignore the privilege 261 + * mode and delegation registers, but respect individual enables 262 + */ 263 + return (atomic_read(&env->mip) & env->mie) != 0; 264 + #else 265 + return true; 266 + #endif 267 + } 268 + 269 + void restore_state_to_opc(CPURISCVState *env, TranslationBlock *tb, 270 + target_ulong *data) 271 + { 272 + env->pc = data[0]; 273 + } 274 + 275 + static void riscv_cpu_reset(CPUState *cs) 276 + { 277 + RISCVCPU *cpu = RISCV_CPU(cs); 278 + RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(cpu); 279 + CPURISCVState *env = &cpu->env; 280 + 281 + mcc->parent_reset(cs); 282 + #ifndef CONFIG_USER_ONLY 283 + env->priv = PRV_M; 284 + env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV); 285 + env->mcause = 0; 286 + env->pc = env->resetvec; 287 + #endif 288 + cs->exception_index = EXCP_NONE; 289 + set_default_nan_mode(1, &env->fp_status); 290 + } 291 + 292 + static void riscv_cpu_disas_set_info(CPUState *s, disassemble_info *info) 293 + { 294 + #if defined(TARGET_RISCV32) 295 + info->print_insn = print_insn_riscv32; 296 + #elif defined(TARGET_RISCV64) 297 + info->print_insn = print_insn_riscv64; 298 + #endif 299 + } 300 + 301 + static void riscv_cpu_realize(DeviceState *dev, Error **errp) 302 + { 303 + CPUState *cs = CPU(dev); 304 + RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(dev); 305 + Error *local_err = NULL; 306 + 307 + cpu_exec_realizefn(cs, &local_err); 308 + if (local_err != NULL) { 309 + error_propagate(errp, local_err); 310 + return; 311 + } 312 + 313 + qemu_init_vcpu(cs); 314 + cpu_reset(cs); 315 + 316 + mcc->parent_realize(dev, errp); 317 + } 318 + 319 + static void riscv_cpu_init(Object *obj) 320 + { 321 + CPUState *cs = CPU(obj); 322 + RISCVCPU *cpu = RISCV_CPU(obj); 323 + 324 + cs->env_ptr = &cpu->env; 325 + } 326 + 327 + static const VMStateDescription vmstate_riscv_cpu = { 328 + .name = "cpu", 329 + .unmigratable = 1, 330 + }; 331 + 332 + static void riscv_cpu_class_init(ObjectClass *c, void *data) 333 + { 334 + RISCVCPUClass *mcc = RISCV_CPU_CLASS(c); 335 + CPUClass *cc = CPU_CLASS(c); 336 + DeviceClass *dc = DEVICE_CLASS(c); 337 + 338 + mcc->parent_realize = dc->realize; 339 + dc->realize = riscv_cpu_realize; 340 + 341 + mcc->parent_reset = cc->reset; 342 + cc->reset = riscv_cpu_reset; 343 + 344 + cc->class_by_name = riscv_cpu_class_by_name; 345 + cc->has_work = riscv_cpu_has_work; 346 + cc->do_interrupt = riscv_cpu_do_interrupt; 347 + cc->cpu_exec_interrupt = riscv_cpu_exec_interrupt; 348 + cc->dump_state = riscv_cpu_dump_state; 349 + cc->set_pc = riscv_cpu_set_pc; 350 + cc->synchronize_from_tb = riscv_cpu_synchronize_from_tb; 351 + cc->gdb_read_register = riscv_cpu_gdb_read_register; 352 + cc->gdb_write_register = riscv_cpu_gdb_write_register; 353 + cc->gdb_num_core_regs = 65; 354 + cc->gdb_stop_before_watchpoint = true; 355 + cc->disas_set_info = riscv_cpu_disas_set_info; 356 + #ifdef CONFIG_USER_ONLY 357 + cc->handle_mmu_fault = riscv_cpu_handle_mmu_fault; 358 + #else 359 + cc->do_unaligned_access = riscv_cpu_do_unaligned_access; 360 + cc->get_phys_page_debug = riscv_cpu_get_phys_page_debug; 361 + #endif 362 + #ifdef CONFIG_TCG 363 + cc->tcg_initialize = riscv_translate_init; 364 + #endif 365 + /* For now, mark unmigratable: */ 366 + cc->vmsd = &vmstate_riscv_cpu; 367 + } 368 + 369 + static void cpu_register(const RISCVCPUInfo *info) 370 + { 371 + TypeInfo type_info = { 372 + .name = info->name, 373 + .parent = TYPE_RISCV_CPU, 374 + .instance_size = sizeof(RISCVCPU), 375 + .instance_init = info->initfn, 376 + }; 377 + 378 + type_register(&type_info); 379 + } 380 + 381 + static const TypeInfo riscv_cpu_type_info = { 382 + .name = TYPE_RISCV_CPU, 383 + .parent = TYPE_CPU, 384 + .instance_size = sizeof(RISCVCPU), 385 + .instance_init = riscv_cpu_init, 386 + .abstract = false, 387 + .class_size = sizeof(RISCVCPUClass), 388 + .class_init = riscv_cpu_class_init, 389 + }; 390 + 391 + char *riscv_isa_string(RISCVCPU *cpu) 392 + { 393 + int i; 394 + size_t maxlen = 5 + ctz32(cpu->env.misa); 395 + char *isa_string = g_new0(char, maxlen); 396 + snprintf(isa_string, maxlen, "rv%d", TARGET_LONG_BITS); 397 + for (i = 0; i < sizeof(riscv_exts); i++) { 398 + if (cpu->env.misa & RV(riscv_exts[i])) { 399 + isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a'; 400 + 401 + } 402 + } 403 + return isa_string; 404 + } 405 + 406 + void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf) 407 + { 408 + const RISCVCPUInfo *info = riscv_cpus; 409 + 410 + while (info->name) { 411 + if (info->bit_widths & TARGET_LONG_BITS) { 412 + (*cpu_fprintf)(f, "%s\n", info->name); 413 + } 414 + info++; 415 + } 416 + } 417 + 418 + static void riscv_cpu_register_types(void) 419 + { 420 + const RISCVCPUInfo *info = riscv_cpus; 421 + 422 + type_register_static(&riscv_cpu_type_info); 423 + 424 + while (info->name) { 425 + if (info->bit_widths & TARGET_LONG_BITS) { 426 + cpu_register(info); 427 + } 428 + info++; 429 + } 430 + } 431 + 432 + type_init(riscv_cpu_register_types)
+296
target/riscv/cpu.h
··· 1 + /* 2 + * QEMU RISC-V CPU 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #ifndef RISCV_CPU_H 21 + #define RISCV_CPU_H 22 + 23 + /* QEMU addressing/paging config */ 24 + #define TARGET_PAGE_BITS 12 /* 4 KiB Pages */ 25 + #if defined(TARGET_RISCV64) 26 + #define TARGET_LONG_BITS 64 27 + #define TARGET_PHYS_ADDR_SPACE_BITS 50 28 + #define TARGET_VIRT_ADDR_SPACE_BITS 39 29 + #elif defined(TARGET_RISCV32) 30 + #define TARGET_LONG_BITS 32 31 + #define TARGET_PHYS_ADDR_SPACE_BITS 34 32 + #define TARGET_VIRT_ADDR_SPACE_BITS 32 33 + #endif 34 + 35 + #define TCG_GUEST_DEFAULT_MO 0 36 + 37 + #define ELF_MACHINE EM_RISCV 38 + #define CPUArchState struct CPURISCVState 39 + 40 + #include "qemu-common.h" 41 + #include "qom/cpu.h" 42 + #include "exec/cpu-defs.h" 43 + #include "fpu/softfloat.h" 44 + 45 + #define TYPE_RISCV_CPU "riscv-cpu" 46 + 47 + #define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU 48 + #define RISCV_CPU_TYPE_NAME(name) (name RISCV_CPU_TYPE_SUFFIX) 49 + 50 + #define TYPE_RISCV_CPU_ANY RISCV_CPU_TYPE_NAME("any") 51 + #define TYPE_RISCV_CPU_RV32GCSU_V1_09_1 RISCV_CPU_TYPE_NAME("rv32gcsu-v1.9.1") 52 + #define TYPE_RISCV_CPU_RV32GCSU_V1_10_0 RISCV_CPU_TYPE_NAME("rv32gcsu-v1.10.0") 53 + #define TYPE_RISCV_CPU_RV32IMACU_NOMMU RISCV_CPU_TYPE_NAME("rv32imacu-nommu") 54 + #define TYPE_RISCV_CPU_RV64GCSU_V1_09_1 RISCV_CPU_TYPE_NAME("rv64gcsu-v1.9.1") 55 + #define TYPE_RISCV_CPU_RV64GCSU_V1_10_0 RISCV_CPU_TYPE_NAME("rv64gcsu-v1.10.0") 56 + #define TYPE_RISCV_CPU_RV64IMACU_NOMMU RISCV_CPU_TYPE_NAME("rv64imacu-nommu") 57 + #define TYPE_RISCV_CPU_SIFIVE_E31 RISCV_CPU_TYPE_NAME("sifive-e31") 58 + #define TYPE_RISCV_CPU_SIFIVE_E51 RISCV_CPU_TYPE_NAME("sifive-e51") 59 + #define TYPE_RISCV_CPU_SIFIVE_U34 RISCV_CPU_TYPE_NAME("sifive-u34") 60 + #define TYPE_RISCV_CPU_SIFIVE_U54 RISCV_CPU_TYPE_NAME("sifive-u54") 61 + 62 + #define RV32 ((target_ulong)1 << (TARGET_LONG_BITS - 2)) 63 + #define RV64 ((target_ulong)2 << (TARGET_LONG_BITS - 2)) 64 + 65 + #if defined(TARGET_RISCV32) 66 + #define RVXLEN RV32 67 + #elif defined(TARGET_RISCV64) 68 + #define RVXLEN RV64 69 + #endif 70 + 71 + #define RV(x) ((target_ulong)1 << (x - 'A')) 72 + 73 + #define RVI RV('I') 74 + #define RVM RV('M') 75 + #define RVA RV('A') 76 + #define RVF RV('F') 77 + #define RVD RV('D') 78 + #define RVC RV('C') 79 + #define RVS RV('S') 80 + #define RVU RV('U') 81 + 82 + /* S extension denotes that Supervisor mode exists, however it is possible 83 + to have a core that support S mode but does not have an MMU and there 84 + is currently no bit in misa to indicate whether an MMU exists or not 85 + so a cpu features bitfield is required */ 86 + enum { 87 + RISCV_FEATURE_MMU 88 + }; 89 + 90 + #define USER_VERSION_2_02_0 0x00020200 91 + #define PRIV_VERSION_1_09_1 0x00010901 92 + #define PRIV_VERSION_1_10_0 0x00011000 93 + 94 + #define TRANSLATE_FAIL 1 95 + #define TRANSLATE_SUCCESS 0 96 + #define NB_MMU_MODES 4 97 + #define MMU_USER_IDX 3 98 + 99 + #define MAX_RISCV_PMPS (16) 100 + 101 + typedef struct CPURISCVState CPURISCVState; 102 + 103 + #include "pmp.h" 104 + 105 + struct CPURISCVState { 106 + target_ulong gpr[32]; 107 + uint64_t fpr[32]; /* assume both F and D extensions */ 108 + target_ulong pc; 109 + target_ulong load_res; 110 + target_ulong load_val; 111 + 112 + target_ulong frm; 113 + 114 + target_ulong badaddr; 115 + 116 + target_ulong user_ver; 117 + target_ulong priv_ver; 118 + target_ulong misa; 119 + 120 + uint32_t features; 121 + 122 + #ifndef CONFIG_USER_ONLY 123 + target_ulong priv; 124 + target_ulong resetvec; 125 + 126 + target_ulong mhartid; 127 + target_ulong mstatus; 128 + /* 129 + * CAUTION! Unlike the rest of this struct, mip is accessed asynchonously 130 + * by I/O threads and other vCPUs, so hold the iothread mutex before 131 + * operating on it. CPU_INTERRUPT_HARD should be in effect iff this is 132 + * non-zero. Use riscv_cpu_set_local_interrupt. 133 + */ 134 + uint32_t mip; /* allow atomic_read for >= 32-bit hosts */ 135 + target_ulong mie; 136 + target_ulong mideleg; 137 + 138 + target_ulong sptbr; /* until: priv-1.9.1 */ 139 + target_ulong satp; /* since: priv-1.10.0 */ 140 + target_ulong sbadaddr; 141 + target_ulong mbadaddr; 142 + target_ulong medeleg; 143 + 144 + target_ulong stvec; 145 + target_ulong sepc; 146 + target_ulong scause; 147 + 148 + target_ulong mtvec; 149 + target_ulong mepc; 150 + target_ulong mcause; 151 + target_ulong mtval; /* since: priv-1.10.0 */ 152 + 153 + uint32_t mucounteren; 154 + uint32_t mscounteren; 155 + target_ulong scounteren; /* since: priv-1.10.0 */ 156 + target_ulong mcounteren; /* since: priv-1.10.0 */ 157 + 158 + target_ulong sscratch; 159 + target_ulong mscratch; 160 + 161 + /* temporary htif regs */ 162 + uint64_t mfromhost; 163 + uint64_t mtohost; 164 + uint64_t timecmp; 165 + 166 + /* physical memory protection */ 167 + pmp_table_t pmp_state; 168 + #endif 169 + 170 + float_status fp_status; 171 + 172 + /* QEMU */ 173 + CPU_COMMON 174 + 175 + /* Fields from here on are preserved across CPU reset. */ 176 + QEMUTimer *timer; /* Internal timer */ 177 + }; 178 + 179 + #define RISCV_CPU_CLASS(klass) \ 180 + OBJECT_CLASS_CHECK(RISCVCPUClass, (klass), TYPE_RISCV_CPU) 181 + #define RISCV_CPU(obj) \ 182 + OBJECT_CHECK(RISCVCPU, (obj), TYPE_RISCV_CPU) 183 + #define RISCV_CPU_GET_CLASS(obj) \ 184 + OBJECT_GET_CLASS(RISCVCPUClass, (obj), TYPE_RISCV_CPU) 185 + 186 + /** 187 + * RISCVCPUClass: 188 + * @parent_realize: The parent class' realize handler. 189 + * @parent_reset: The parent class' reset handler. 190 + * 191 + * A RISCV CPU model. 192 + */ 193 + typedef struct RISCVCPUClass { 194 + /*< private >*/ 195 + CPUClass parent_class; 196 + /*< public >*/ 197 + DeviceRealize parent_realize; 198 + void (*parent_reset)(CPUState *cpu); 199 + } RISCVCPUClass; 200 + 201 + /** 202 + * RISCVCPU: 203 + * @env: #CPURISCVState 204 + * 205 + * A RISCV CPU. 206 + */ 207 + typedef struct RISCVCPU { 208 + /*< private >*/ 209 + CPUState parent_obj; 210 + /*< public >*/ 211 + CPURISCVState env; 212 + } RISCVCPU; 213 + 214 + static inline RISCVCPU *riscv_env_get_cpu(CPURISCVState *env) 215 + { 216 + return container_of(env, RISCVCPU, env); 217 + } 218 + 219 + static inline int riscv_has_ext(CPURISCVState *env, target_ulong ext) 220 + { 221 + return (env->misa & ext) != 0; 222 + } 223 + 224 + static inline bool riscv_feature(CPURISCVState *env, int feature) 225 + { 226 + return env->features & (1ULL << feature); 227 + } 228 + 229 + #include "cpu_user.h" 230 + #include "cpu_bits.h" 231 + 232 + extern const char * const riscv_int_regnames[]; 233 + extern const char * const riscv_fpr_regnames[]; 234 + extern const char * const riscv_excp_names[]; 235 + extern const char * const riscv_intr_names[]; 236 + 237 + #define ENV_GET_CPU(e) CPU(riscv_env_get_cpu(e)) 238 + #define ENV_OFFSET offsetof(RISCVCPU, env) 239 + 240 + void riscv_cpu_do_interrupt(CPUState *cpu); 241 + int riscv_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg); 242 + int riscv_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg); 243 + bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request); 244 + int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch); 245 + hwaddr riscv_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr); 246 + void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, 247 + MMUAccessType access_type, int mmu_idx, 248 + uintptr_t retaddr); 249 + int riscv_cpu_handle_mmu_fault(CPUState *cpu, vaddr address, int size, 250 + int rw, int mmu_idx); 251 + 252 + char *riscv_isa_string(RISCVCPU *cpu); 253 + void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf); 254 + 255 + #define cpu_init(cpu_model) cpu_generic_init(TYPE_RISCV_CPU, cpu_model) 256 + #define cpu_signal_handler cpu_riscv_signal_handler 257 + #define cpu_list riscv_cpu_list 258 + #define cpu_mmu_index riscv_cpu_mmu_index 259 + 260 + void riscv_set_mode(CPURISCVState *env, target_ulong newpriv); 261 + 262 + void riscv_translate_init(void); 263 + RISCVCPU *cpu_riscv_init(const char *cpu_model); 264 + int cpu_riscv_signal_handler(int host_signum, void *pinfo, void *puc); 265 + void QEMU_NORETURN do_raise_exception_err(CPURISCVState *env, 266 + uint32_t exception, uintptr_t pc); 267 + 268 + target_ulong cpu_riscv_get_fflags(CPURISCVState *env); 269 + void cpu_riscv_set_fflags(CPURISCVState *env, target_ulong); 270 + 271 + #define TB_FLAGS_MMU_MASK 3 272 + #define TB_FLAGS_FP_ENABLE MSTATUS_FS 273 + 274 + static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc, 275 + target_ulong *cs_base, uint32_t *flags) 276 + { 277 + *pc = env->pc; 278 + *cs_base = 0; 279 + #ifdef CONFIG_USER_ONLY 280 + *flags = TB_FLAGS_FP_ENABLE; 281 + #else 282 + *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS); 283 + #endif 284 + } 285 + 286 + void csr_write_helper(CPURISCVState *env, target_ulong val_to_write, 287 + target_ulong csrno); 288 + target_ulong csr_read_helper(CPURISCVState *env, target_ulong csrno); 289 + 290 + #ifndef CONFIG_USER_ONLY 291 + void riscv_set_local_interrupt(RISCVCPU *cpu, target_ulong mask, int value); 292 + #endif 293 + 294 + #include "exec/cpu-all.h" 295 + 296 + #endif /* RISCV_CPU_H */
+411
target/riscv/cpu_bits.h
··· 1 + /* RISC-V ISA constants */ 2 + 3 + #define get_field(reg, mask) (((reg) & \ 4 + (target_ulong)(mask)) / ((mask) & ~((mask) << 1))) 5 + #define set_field(reg, mask, val) (((reg) & ~(target_ulong)(mask)) | \ 6 + (((target_ulong)(val) * ((mask) & ~((mask) << 1))) & \ 7 + (target_ulong)(mask))) 8 + 9 + #define PGSHIFT 12 10 + 11 + #define FSR_RD_SHIFT 5 12 + #define FSR_RD (0x7 << FSR_RD_SHIFT) 13 + 14 + #define FPEXC_NX 0x01 15 + #define FPEXC_UF 0x02 16 + #define FPEXC_OF 0x04 17 + #define FPEXC_DZ 0x08 18 + #define FPEXC_NV 0x10 19 + 20 + #define FSR_AEXC_SHIFT 0 21 + #define FSR_NVA (FPEXC_NV << FSR_AEXC_SHIFT) 22 + #define FSR_OFA (FPEXC_OF << FSR_AEXC_SHIFT) 23 + #define FSR_UFA (FPEXC_UF << FSR_AEXC_SHIFT) 24 + #define FSR_DZA (FPEXC_DZ << FSR_AEXC_SHIFT) 25 + #define FSR_NXA (FPEXC_NX << FSR_AEXC_SHIFT) 26 + #define FSR_AEXC (FSR_NVA | FSR_OFA | FSR_UFA | FSR_DZA | FSR_NXA) 27 + 28 + /* CSR numbers */ 29 + #define CSR_FFLAGS 0x1 30 + #define CSR_FRM 0x2 31 + #define CSR_FCSR 0x3 32 + #define CSR_CYCLE 0xc00 33 + #define CSR_TIME 0xc01 34 + #define CSR_INSTRET 0xc02 35 + #define CSR_HPMCOUNTER3 0xc03 36 + #define CSR_HPMCOUNTER4 0xc04 37 + #define CSR_HPMCOUNTER5 0xc05 38 + #define CSR_HPMCOUNTER6 0xc06 39 + #define CSR_HPMCOUNTER7 0xc07 40 + #define CSR_HPMCOUNTER8 0xc08 41 + #define CSR_HPMCOUNTER9 0xc09 42 + #define CSR_HPMCOUNTER10 0xc0a 43 + #define CSR_HPMCOUNTER11 0xc0b 44 + #define CSR_HPMCOUNTER12 0xc0c 45 + #define CSR_HPMCOUNTER13 0xc0d 46 + #define CSR_HPMCOUNTER14 0xc0e 47 + #define CSR_HPMCOUNTER15 0xc0f 48 + #define CSR_HPMCOUNTER16 0xc10 49 + #define CSR_HPMCOUNTER17 0xc11 50 + #define CSR_HPMCOUNTER18 0xc12 51 + #define CSR_HPMCOUNTER19 0xc13 52 + #define CSR_HPMCOUNTER20 0xc14 53 + #define CSR_HPMCOUNTER21 0xc15 54 + #define CSR_HPMCOUNTER22 0xc16 55 + #define CSR_HPMCOUNTER23 0xc17 56 + #define CSR_HPMCOUNTER24 0xc18 57 + #define CSR_HPMCOUNTER25 0xc19 58 + #define CSR_HPMCOUNTER26 0xc1a 59 + #define CSR_HPMCOUNTER27 0xc1b 60 + #define CSR_HPMCOUNTER28 0xc1c 61 + #define CSR_HPMCOUNTER29 0xc1d 62 + #define CSR_HPMCOUNTER30 0xc1e 63 + #define CSR_HPMCOUNTER31 0xc1f 64 + #define CSR_SSTATUS 0x100 65 + #define CSR_SIE 0x104 66 + #define CSR_STVEC 0x105 67 + #define CSR_SCOUNTEREN 0x106 68 + #define CSR_SSCRATCH 0x140 69 + #define CSR_SEPC 0x141 70 + #define CSR_SCAUSE 0x142 71 + #define CSR_SBADADDR 0x143 72 + #define CSR_SIP 0x144 73 + #define CSR_SPTBR 0x180 74 + #define CSR_SATP 0x180 75 + #define CSR_MSTATUS 0x300 76 + #define CSR_MISA 0x301 77 + #define CSR_MEDELEG 0x302 78 + #define CSR_MIDELEG 0x303 79 + #define CSR_MIE 0x304 80 + #define CSR_MTVEC 0x305 81 + #define CSR_MCOUNTEREN 0x306 82 + #define CSR_MSCRATCH 0x340 83 + #define CSR_MEPC 0x341 84 + #define CSR_MCAUSE 0x342 85 + #define CSR_MBADADDR 0x343 86 + #define CSR_MIP 0x344 87 + #define CSR_PMPCFG0 0x3a0 88 + #define CSR_PMPCFG1 0x3a1 89 + #define CSR_PMPCFG2 0x3a2 90 + #define CSR_PMPCFG3 0x3a3 91 + #define CSR_PMPADDR0 0x3b0 92 + #define CSR_PMPADDR1 0x3b1 93 + #define CSR_PMPADDR2 0x3b2 94 + #define CSR_PMPADDR3 0x3b3 95 + #define CSR_PMPADDR4 0x3b4 96 + #define CSR_PMPADDR5 0x3b5 97 + #define CSR_PMPADDR6 0x3b6 98 + #define CSR_PMPADDR7 0x3b7 99 + #define CSR_PMPADDR8 0x3b8 100 + #define CSR_PMPADDR9 0x3b9 101 + #define CSR_PMPADDR10 0x3ba 102 + #define CSR_PMPADDR11 0x3bb 103 + #define CSR_PMPADDR12 0x3bc 104 + #define CSR_PMPADDR13 0x3bd 105 + #define CSR_PMPADDR14 0x3be 106 + #define CSR_PMPADDR15 0x3bf 107 + #define CSR_TSELECT 0x7a0 108 + #define CSR_TDATA1 0x7a1 109 + #define CSR_TDATA2 0x7a2 110 + #define CSR_TDATA3 0x7a3 111 + #define CSR_DCSR 0x7b0 112 + #define CSR_DPC 0x7b1 113 + #define CSR_DSCRATCH 0x7b2 114 + #define CSR_MCYCLE 0xb00 115 + #define CSR_MINSTRET 0xb02 116 + #define CSR_MHPMCOUNTER3 0xb03 117 + #define CSR_MHPMCOUNTER4 0xb04 118 + #define CSR_MHPMCOUNTER5 0xb05 119 + #define CSR_MHPMCOUNTER6 0xb06 120 + #define CSR_MHPMCOUNTER7 0xb07 121 + #define CSR_MHPMCOUNTER8 0xb08 122 + #define CSR_MHPMCOUNTER9 0xb09 123 + #define CSR_MHPMCOUNTER10 0xb0a 124 + #define CSR_MHPMCOUNTER11 0xb0b 125 + #define CSR_MHPMCOUNTER12 0xb0c 126 + #define CSR_MHPMCOUNTER13 0xb0d 127 + #define CSR_MHPMCOUNTER14 0xb0e 128 + #define CSR_MHPMCOUNTER15 0xb0f 129 + #define CSR_MHPMCOUNTER16 0xb10 130 + #define CSR_MHPMCOUNTER17 0xb11 131 + #define CSR_MHPMCOUNTER18 0xb12 132 + #define CSR_MHPMCOUNTER19 0xb13 133 + #define CSR_MHPMCOUNTER20 0xb14 134 + #define CSR_MHPMCOUNTER21 0xb15 135 + #define CSR_MHPMCOUNTER22 0xb16 136 + #define CSR_MHPMCOUNTER23 0xb17 137 + #define CSR_MHPMCOUNTER24 0xb18 138 + #define CSR_MHPMCOUNTER25 0xb19 139 + #define CSR_MHPMCOUNTER26 0xb1a 140 + #define CSR_MHPMCOUNTER27 0xb1b 141 + #define CSR_MHPMCOUNTER28 0xb1c 142 + #define CSR_MHPMCOUNTER29 0xb1d 143 + #define CSR_MHPMCOUNTER30 0xb1e 144 + #define CSR_MHPMCOUNTER31 0xb1f 145 + #define CSR_MUCOUNTEREN 0x320 146 + #define CSR_MSCOUNTEREN 0x321 147 + #define CSR_MHPMEVENT3 0x323 148 + #define CSR_MHPMEVENT4 0x324 149 + #define CSR_MHPMEVENT5 0x325 150 + #define CSR_MHPMEVENT6 0x326 151 + #define CSR_MHPMEVENT7 0x327 152 + #define CSR_MHPMEVENT8 0x328 153 + #define CSR_MHPMEVENT9 0x329 154 + #define CSR_MHPMEVENT10 0x32a 155 + #define CSR_MHPMEVENT11 0x32b 156 + #define CSR_MHPMEVENT12 0x32c 157 + #define CSR_MHPMEVENT13 0x32d 158 + #define CSR_MHPMEVENT14 0x32e 159 + #define CSR_MHPMEVENT15 0x32f 160 + #define CSR_MHPMEVENT16 0x330 161 + #define CSR_MHPMEVENT17 0x331 162 + #define CSR_MHPMEVENT18 0x332 163 + #define CSR_MHPMEVENT19 0x333 164 + #define CSR_MHPMEVENT20 0x334 165 + #define CSR_MHPMEVENT21 0x335 166 + #define CSR_MHPMEVENT22 0x336 167 + #define CSR_MHPMEVENT23 0x337 168 + #define CSR_MHPMEVENT24 0x338 169 + #define CSR_MHPMEVENT25 0x339 170 + #define CSR_MHPMEVENT26 0x33a 171 + #define CSR_MHPMEVENT27 0x33b 172 + #define CSR_MHPMEVENT28 0x33c 173 + #define CSR_MHPMEVENT29 0x33d 174 + #define CSR_MHPMEVENT30 0x33e 175 + #define CSR_MHPMEVENT31 0x33f 176 + #define CSR_MVENDORID 0xf11 177 + #define CSR_MARCHID 0xf12 178 + #define CSR_MIMPID 0xf13 179 + #define CSR_MHARTID 0xf14 180 + #define CSR_CYCLEH 0xc80 181 + #define CSR_TIMEH 0xc81 182 + #define CSR_INSTRETH 0xc82 183 + #define CSR_HPMCOUNTER3H 0xc83 184 + #define CSR_HPMCOUNTER4H 0xc84 185 + #define CSR_HPMCOUNTER5H 0xc85 186 + #define CSR_HPMCOUNTER6H 0xc86 187 + #define CSR_HPMCOUNTER7H 0xc87 188 + #define CSR_HPMCOUNTER8H 0xc88 189 + #define CSR_HPMCOUNTER9H 0xc89 190 + #define CSR_HPMCOUNTER10H 0xc8a 191 + #define CSR_HPMCOUNTER11H 0xc8b 192 + #define CSR_HPMCOUNTER12H 0xc8c 193 + #define CSR_HPMCOUNTER13H 0xc8d 194 + #define CSR_HPMCOUNTER14H 0xc8e 195 + #define CSR_HPMCOUNTER15H 0xc8f 196 + #define CSR_HPMCOUNTER16H 0xc90 197 + #define CSR_HPMCOUNTER17H 0xc91 198 + #define CSR_HPMCOUNTER18H 0xc92 199 + #define CSR_HPMCOUNTER19H 0xc93 200 + #define CSR_HPMCOUNTER20H 0xc94 201 + #define CSR_HPMCOUNTER21H 0xc95 202 + #define CSR_HPMCOUNTER22H 0xc96 203 + #define CSR_HPMCOUNTER23H 0xc97 204 + #define CSR_HPMCOUNTER24H 0xc98 205 + #define CSR_HPMCOUNTER25H 0xc99 206 + #define CSR_HPMCOUNTER26H 0xc9a 207 + #define CSR_HPMCOUNTER27H 0xc9b 208 + #define CSR_HPMCOUNTER28H 0xc9c 209 + #define CSR_HPMCOUNTER29H 0xc9d 210 + #define CSR_HPMCOUNTER30H 0xc9e 211 + #define CSR_HPMCOUNTER31H 0xc9f 212 + #define CSR_MCYCLEH 0xb80 213 + #define CSR_MINSTRETH 0xb82 214 + #define CSR_MHPMCOUNTER3H 0xb83 215 + #define CSR_MHPMCOUNTER4H 0xb84 216 + #define CSR_MHPMCOUNTER5H 0xb85 217 + #define CSR_MHPMCOUNTER6H 0xb86 218 + #define CSR_MHPMCOUNTER7H 0xb87 219 + #define CSR_MHPMCOUNTER8H 0xb88 220 + #define CSR_MHPMCOUNTER9H 0xb89 221 + #define CSR_MHPMCOUNTER10H 0xb8a 222 + #define CSR_MHPMCOUNTER11H 0xb8b 223 + #define CSR_MHPMCOUNTER12H 0xb8c 224 + #define CSR_MHPMCOUNTER13H 0xb8d 225 + #define CSR_MHPMCOUNTER14H 0xb8e 226 + #define CSR_MHPMCOUNTER15H 0xb8f 227 + #define CSR_MHPMCOUNTER16H 0xb90 228 + #define CSR_MHPMCOUNTER17H 0xb91 229 + #define CSR_MHPMCOUNTER18H 0xb92 230 + #define CSR_MHPMCOUNTER19H 0xb93 231 + #define CSR_MHPMCOUNTER20H 0xb94 232 + #define CSR_MHPMCOUNTER21H 0xb95 233 + #define CSR_MHPMCOUNTER22H 0xb96 234 + #define CSR_MHPMCOUNTER23H 0xb97 235 + #define CSR_MHPMCOUNTER24H 0xb98 236 + #define CSR_MHPMCOUNTER25H 0xb99 237 + #define CSR_MHPMCOUNTER26H 0xb9a 238 + #define CSR_MHPMCOUNTER27H 0xb9b 239 + #define CSR_MHPMCOUNTER28H 0xb9c 240 + #define CSR_MHPMCOUNTER29H 0xb9d 241 + #define CSR_MHPMCOUNTER30H 0xb9e 242 + #define CSR_MHPMCOUNTER31H 0xb9f 243 + 244 + /* mstatus bits */ 245 + #define MSTATUS_UIE 0x00000001 246 + #define MSTATUS_SIE 0x00000002 247 + #define MSTATUS_HIE 0x00000004 248 + #define MSTATUS_MIE 0x00000008 249 + #define MSTATUS_UPIE 0x00000010 250 + #define MSTATUS_SPIE 0x00000020 251 + #define MSTATUS_HPIE 0x00000040 252 + #define MSTATUS_MPIE 0x00000080 253 + #define MSTATUS_SPP 0x00000100 254 + #define MSTATUS_HPP 0x00000600 255 + #define MSTATUS_MPP 0x00001800 256 + #define MSTATUS_FS 0x00006000 257 + #define MSTATUS_XS 0x00018000 258 + #define MSTATUS_MPRV 0x00020000 259 + #define MSTATUS_PUM 0x00040000 /* until: priv-1.9.1 */ 260 + #define MSTATUS_SUM 0x00040000 /* since: priv-1.10 */ 261 + #define MSTATUS_MXR 0x00080000 262 + #define MSTATUS_VM 0x1F000000 /* until: priv-1.9.1 */ 263 + #define MSTATUS_TVM 0x00100000 /* since: priv-1.10 */ 264 + #define MSTATUS_TW 0x20000000 /* since: priv-1.10 */ 265 + #define MSTATUS_TSR 0x40000000 /* since: priv-1.10 */ 266 + 267 + #define MSTATUS64_UXL 0x0000000300000000ULL 268 + #define MSTATUS64_SXL 0x0000000C00000000ULL 269 + 270 + #define MSTATUS32_SD 0x80000000 271 + #define MSTATUS64_SD 0x8000000000000000ULL 272 + 273 + #if defined(TARGET_RISCV32) 274 + #define MSTATUS_SD MSTATUS32_SD 275 + #elif defined(TARGET_RISCV64) 276 + #define MSTATUS_SD MSTATUS64_SD 277 + #endif 278 + 279 + /* sstatus bits */ 280 + #define SSTATUS_UIE 0x00000001 281 + #define SSTATUS_SIE 0x00000002 282 + #define SSTATUS_UPIE 0x00000010 283 + #define SSTATUS_SPIE 0x00000020 284 + #define SSTATUS_SPP 0x00000100 285 + #define SSTATUS_FS 0x00006000 286 + #define SSTATUS_XS 0x00018000 287 + #define SSTATUS_PUM 0x00040000 /* until: priv-1.9.1 */ 288 + #define SSTATUS_SUM 0x00040000 /* since: priv-1.10 */ 289 + #define SSTATUS_MXR 0x00080000 290 + 291 + #define SSTATUS32_SD 0x80000000 292 + #define SSTATUS64_SD 0x8000000000000000ULL 293 + 294 + #if defined(TARGET_RISCV32) 295 + #define SSTATUS_SD SSTATUS32_SD 296 + #elif defined(TARGET_RISCV64) 297 + #define SSTATUS_SD SSTATUS64_SD 298 + #endif 299 + 300 + /* irqs */ 301 + #define MIP_SSIP (1 << IRQ_S_SOFT) 302 + #define MIP_HSIP (1 << IRQ_H_SOFT) 303 + #define MIP_MSIP (1 << IRQ_M_SOFT) 304 + #define MIP_STIP (1 << IRQ_S_TIMER) 305 + #define MIP_HTIP (1 << IRQ_H_TIMER) 306 + #define MIP_MTIP (1 << IRQ_M_TIMER) 307 + #define MIP_SEIP (1 << IRQ_S_EXT) 308 + #define MIP_HEIP (1 << IRQ_H_EXT) 309 + #define MIP_MEIP (1 << IRQ_M_EXT) 310 + 311 + #define SIP_SSIP MIP_SSIP 312 + #define SIP_STIP MIP_STIP 313 + #define SIP_SEIP MIP_SEIP 314 + 315 + #define PRV_U 0 316 + #define PRV_S 1 317 + #define PRV_H 2 318 + #define PRV_M 3 319 + 320 + /* privileged ISA 1.9.1 VM modes (mstatus.vm) */ 321 + #define VM_1_09_MBARE 0 322 + #define VM_1_09_MBB 1 323 + #define VM_1_09_MBBID 2 324 + #define VM_1_09_SV32 8 325 + #define VM_1_09_SV39 9 326 + #define VM_1_09_SV48 10 327 + 328 + /* privileged ISA 1.10.0 VM modes (satp.mode) */ 329 + #define VM_1_10_MBARE 0 330 + #define VM_1_10_SV32 1 331 + #define VM_1_10_SV39 8 332 + #define VM_1_10_SV48 9 333 + #define VM_1_10_SV57 10 334 + #define VM_1_10_SV64 11 335 + 336 + /* privileged ISA interrupt causes */ 337 + #define IRQ_U_SOFT 0 /* since: priv-1.10 */ 338 + #define IRQ_S_SOFT 1 339 + #define IRQ_H_SOFT 2 /* until: priv-1.9.1 */ 340 + #define IRQ_M_SOFT 3 /* until: priv-1.9.1 */ 341 + #define IRQ_U_TIMER 4 /* since: priv-1.10 */ 342 + #define IRQ_S_TIMER 5 343 + #define IRQ_H_TIMER 6 /* until: priv-1.9.1 */ 344 + #define IRQ_M_TIMER 7 /* until: priv-1.9.1 */ 345 + #define IRQ_U_EXT 8 /* since: priv-1.10 */ 346 + #define IRQ_S_EXT 9 347 + #define IRQ_H_EXT 10 /* until: priv-1.9.1 */ 348 + #define IRQ_M_EXT 11 /* until: priv-1.9.1 */ 349 + #define IRQ_X_COP 12 /* non-standard */ 350 + 351 + /* Default addresses */ 352 + #define DEFAULT_RSTVEC 0x00001000 353 + 354 + /* RV32 satp field masks */ 355 + #define SATP32_MODE 0x80000000 356 + #define SATP32_ASID 0x7fc00000 357 + #define SATP32_PPN 0x003fffff 358 + 359 + /* RV64 satp field masks */ 360 + #define SATP64_MODE 0xF000000000000000ULL 361 + #define SATP64_ASID 0x0FFFF00000000000ULL 362 + #define SATP64_PPN 0x00000FFFFFFFFFFFULL 363 + 364 + #if defined(TARGET_RISCV32) 365 + #define SATP_MODE SATP32_MODE 366 + #define SATP_ASID SATP32_ASID 367 + #define SATP_PPN SATP32_PPN 368 + #endif 369 + #if defined(TARGET_RISCV64) 370 + #define SATP_MODE SATP64_MODE 371 + #define SATP_ASID SATP64_ASID 372 + #define SATP_PPN SATP64_PPN 373 + #endif 374 + 375 + /* RISCV Exception Codes */ 376 + #define EXCP_NONE -1 /* not a real RISCV exception code */ 377 + #define RISCV_EXCP_INST_ADDR_MIS 0x0 378 + #define RISCV_EXCP_INST_ACCESS_FAULT 0x1 379 + #define RISCV_EXCP_ILLEGAL_INST 0x2 380 + #define RISCV_EXCP_BREAKPOINT 0x3 381 + #define RISCV_EXCP_LOAD_ADDR_MIS 0x4 382 + #define RISCV_EXCP_LOAD_ACCESS_FAULT 0x5 383 + #define RISCV_EXCP_STORE_AMO_ADDR_MIS 0x6 384 + #define RISCV_EXCP_STORE_AMO_ACCESS_FAULT 0x7 385 + #define RISCV_EXCP_U_ECALL 0x8 /* for convenience, report all 386 + ECALLs as this, handler 387 + fixes */ 388 + #define RISCV_EXCP_S_ECALL 0x9 389 + #define RISCV_EXCP_H_ECALL 0xa 390 + #define RISCV_EXCP_M_ECALL 0xb 391 + #define RISCV_EXCP_INST_PAGE_FAULT 0xc /* since: priv-1.10.0 */ 392 + #define RISCV_EXCP_LOAD_PAGE_FAULT 0xd /* since: priv-1.10.0 */ 393 + #define RISCV_EXCP_STORE_PAGE_FAULT 0xf /* since: priv-1.10.0 */ 394 + 395 + #define RISCV_EXCP_INT_FLAG 0x80000000 396 + #define RISCV_EXCP_INT_MASK 0x7fffffff 397 + 398 + /* page table entry (PTE) fields */ 399 + #define PTE_V 0x001 /* Valid */ 400 + #define PTE_R 0x002 /* Read */ 401 + #define PTE_W 0x004 /* Write */ 402 + #define PTE_X 0x008 /* Execute */ 403 + #define PTE_U 0x010 /* User */ 404 + #define PTE_G 0x020 /* Global */ 405 + #define PTE_A 0x040 /* Accessed */ 406 + #define PTE_D 0x080 /* Dirty */ 407 + #define PTE_SOFT 0x300 /* Reserved for Software */ 408 + 409 + #define PTE_PPN_SHIFT 10 410 + 411 + #define PTE_TABLE(PTE) (((PTE) & (PTE_V | PTE_R | PTE_W | PTE_X)) == PTE_V)
+13
target/riscv/cpu_user.h
··· 1 + #define xRA 1 /* return address (aka link register) */ 2 + #define xSP 2 /* stack pointer */ 3 + #define xGP 3 /* global pointer */ 4 + #define xTP 4 /* thread pointer */ 5 + 6 + #define xA0 10 /* gpr[10-17] are syscall arguments */ 7 + #define xA1 11 8 + #define xA2 12 9 + #define xA3 13 10 + #define xA4 14 11 + #define xA5 15 12 + #define xA6 16 13 + #define xA7 17 /* syscall number goes here */
+373
target/riscv/fpu_helper.c
··· 1 + /* 2 + * RISC-V FPU Emulation Helpers for QEMU. 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #include "qemu/osdep.h" 20 + #include <stdlib.h> 21 + #include "cpu.h" 22 + #include "qemu/host-utils.h" 23 + #include "exec/exec-all.h" 24 + #include "exec/helper-proto.h" 25 + 26 + target_ulong cpu_riscv_get_fflags(CPURISCVState *env) 27 + { 28 + int soft = get_float_exception_flags(&env->fp_status); 29 + target_ulong hard = 0; 30 + 31 + hard |= (soft & float_flag_inexact) ? FPEXC_NX : 0; 32 + hard |= (soft & float_flag_underflow) ? FPEXC_UF : 0; 33 + hard |= (soft & float_flag_overflow) ? FPEXC_OF : 0; 34 + hard |= (soft & float_flag_divbyzero) ? FPEXC_DZ : 0; 35 + hard |= (soft & float_flag_invalid) ? FPEXC_NV : 0; 36 + 37 + return hard; 38 + } 39 + 40 + void cpu_riscv_set_fflags(CPURISCVState *env, target_ulong hard) 41 + { 42 + int soft = 0; 43 + 44 + soft |= (hard & FPEXC_NX) ? float_flag_inexact : 0; 45 + soft |= (hard & FPEXC_UF) ? float_flag_underflow : 0; 46 + soft |= (hard & FPEXC_OF) ? float_flag_overflow : 0; 47 + soft |= (hard & FPEXC_DZ) ? float_flag_divbyzero : 0; 48 + soft |= (hard & FPEXC_NV) ? float_flag_invalid : 0; 49 + 50 + set_float_exception_flags(soft, &env->fp_status); 51 + } 52 + 53 + void helper_set_rounding_mode(CPURISCVState *env, uint32_t rm) 54 + { 55 + int softrm; 56 + 57 + if (rm == 7) { 58 + rm = env->frm; 59 + } 60 + switch (rm) { 61 + case 0: 62 + softrm = float_round_nearest_even; 63 + break; 64 + case 1: 65 + softrm = float_round_to_zero; 66 + break; 67 + case 2: 68 + softrm = float_round_down; 69 + break; 70 + case 3: 71 + softrm = float_round_up; 72 + break; 73 + case 4: 74 + softrm = float_round_ties_away; 75 + break; 76 + default: 77 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 78 + } 79 + 80 + set_float_rounding_mode(softrm, &env->fp_status); 81 + } 82 + 83 + uint64_t helper_fmadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 84 + uint64_t frs3) 85 + { 86 + return float32_muladd(frs1, frs2, frs3, 0, &env->fp_status); 87 + } 88 + 89 + uint64_t helper_fmadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 90 + uint64_t frs3) 91 + { 92 + return float64_muladd(frs1, frs2, frs3, 0, &env->fp_status); 93 + } 94 + 95 + uint64_t helper_fmsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 96 + uint64_t frs3) 97 + { 98 + return float32_muladd(frs1, frs2, frs3, float_muladd_negate_c, 99 + &env->fp_status); 100 + } 101 + 102 + uint64_t helper_fmsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 103 + uint64_t frs3) 104 + { 105 + return float64_muladd(frs1, frs2, frs3, float_muladd_negate_c, 106 + &env->fp_status); 107 + } 108 + 109 + uint64_t helper_fnmsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 110 + uint64_t frs3) 111 + { 112 + return float32_muladd(frs1, frs2, frs3, float_muladd_negate_product, 113 + &env->fp_status); 114 + } 115 + 116 + uint64_t helper_fnmsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 117 + uint64_t frs3) 118 + { 119 + return float64_muladd(frs1, frs2, frs3, float_muladd_negate_product, 120 + &env->fp_status); 121 + } 122 + 123 + uint64_t helper_fnmadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 124 + uint64_t frs3) 125 + { 126 + return float32_muladd(frs1, frs2, frs3, float_muladd_negate_c | 127 + float_muladd_negate_product, &env->fp_status); 128 + } 129 + 130 + uint64_t helper_fnmadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2, 131 + uint64_t frs3) 132 + { 133 + return float64_muladd(frs1, frs2, frs3, float_muladd_negate_c | 134 + float_muladd_negate_product, &env->fp_status); 135 + } 136 + 137 + uint64_t helper_fadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 138 + { 139 + return float32_add(frs1, frs2, &env->fp_status); 140 + } 141 + 142 + uint64_t helper_fsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 143 + { 144 + return float32_sub(frs1, frs2, &env->fp_status); 145 + } 146 + 147 + uint64_t helper_fmul_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 148 + { 149 + return float32_mul(frs1, frs2, &env->fp_status); 150 + } 151 + 152 + uint64_t helper_fdiv_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 153 + { 154 + return float32_div(frs1, frs2, &env->fp_status); 155 + } 156 + 157 + uint64_t helper_fmin_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 158 + { 159 + return float32_minnum(frs1, frs2, &env->fp_status); 160 + } 161 + 162 + uint64_t helper_fmax_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 163 + { 164 + return float32_maxnum(frs1, frs2, &env->fp_status); 165 + } 166 + 167 + uint64_t helper_fsqrt_s(CPURISCVState *env, uint64_t frs1) 168 + { 169 + return float32_sqrt(frs1, &env->fp_status); 170 + } 171 + 172 + target_ulong helper_fle_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 173 + { 174 + return float32_le(frs1, frs2, &env->fp_status); 175 + } 176 + 177 + target_ulong helper_flt_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 178 + { 179 + return float32_lt(frs1, frs2, &env->fp_status); 180 + } 181 + 182 + target_ulong helper_feq_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 183 + { 184 + return float32_eq_quiet(frs1, frs2, &env->fp_status); 185 + } 186 + 187 + target_ulong helper_fcvt_w_s(CPURISCVState *env, uint64_t frs1) 188 + { 189 + return float32_to_int32(frs1, &env->fp_status); 190 + } 191 + 192 + target_ulong helper_fcvt_wu_s(CPURISCVState *env, uint64_t frs1) 193 + { 194 + return (int32_t)float32_to_uint32(frs1, &env->fp_status); 195 + } 196 + 197 + #if defined(TARGET_RISCV64) 198 + uint64_t helper_fcvt_l_s(CPURISCVState *env, uint64_t frs1) 199 + { 200 + return float32_to_int64(frs1, &env->fp_status); 201 + } 202 + 203 + uint64_t helper_fcvt_lu_s(CPURISCVState *env, uint64_t frs1) 204 + { 205 + return float32_to_uint64(frs1, &env->fp_status); 206 + } 207 + #endif 208 + 209 + uint64_t helper_fcvt_s_w(CPURISCVState *env, target_ulong rs1) 210 + { 211 + return int32_to_float32((int32_t)rs1, &env->fp_status); 212 + } 213 + 214 + uint64_t helper_fcvt_s_wu(CPURISCVState *env, target_ulong rs1) 215 + { 216 + return uint32_to_float32((uint32_t)rs1, &env->fp_status); 217 + } 218 + 219 + #if defined(TARGET_RISCV64) 220 + uint64_t helper_fcvt_s_l(CPURISCVState *env, uint64_t rs1) 221 + { 222 + return int64_to_float32(rs1, &env->fp_status); 223 + } 224 + 225 + uint64_t helper_fcvt_s_lu(CPURISCVState *env, uint64_t rs1) 226 + { 227 + return uint64_to_float32(rs1, &env->fp_status); 228 + } 229 + #endif 230 + 231 + target_ulong helper_fclass_s(uint64_t frs1) 232 + { 233 + float32 f = frs1; 234 + bool sign = float32_is_neg(f); 235 + 236 + if (float32_is_infinity(f)) { 237 + return sign ? 1 << 0 : 1 << 7; 238 + } else if (float32_is_zero(f)) { 239 + return sign ? 1 << 3 : 1 << 4; 240 + } else if (float32_is_zero_or_denormal(f)) { 241 + return sign ? 1 << 2 : 1 << 5; 242 + } else if (float32_is_any_nan(f)) { 243 + float_status s = { }; /* for snan_bit_is_one */ 244 + return float32_is_quiet_nan(f, &s) ? 1 << 9 : 1 << 8; 245 + } else { 246 + return sign ? 1 << 1 : 1 << 6; 247 + } 248 + } 249 + 250 + uint64_t helper_fadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 251 + { 252 + return float64_add(frs1, frs2, &env->fp_status); 253 + } 254 + 255 + uint64_t helper_fsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 256 + { 257 + return float64_sub(frs1, frs2, &env->fp_status); 258 + } 259 + 260 + uint64_t helper_fmul_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 261 + { 262 + return float64_mul(frs1, frs2, &env->fp_status); 263 + } 264 + 265 + uint64_t helper_fdiv_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 266 + { 267 + return float64_div(frs1, frs2, &env->fp_status); 268 + } 269 + 270 + uint64_t helper_fmin_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 271 + { 272 + return float64_minnum(frs1, frs2, &env->fp_status); 273 + } 274 + 275 + uint64_t helper_fmax_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 276 + { 277 + return float64_maxnum(frs1, frs2, &env->fp_status); 278 + } 279 + 280 + uint64_t helper_fcvt_s_d(CPURISCVState *env, uint64_t rs1) 281 + { 282 + rs1 = float64_to_float32(rs1, &env->fp_status); 283 + return float32_maybe_silence_nan(rs1, &env->fp_status); 284 + } 285 + 286 + uint64_t helper_fcvt_d_s(CPURISCVState *env, uint64_t rs1) 287 + { 288 + rs1 = float32_to_float64(rs1, &env->fp_status); 289 + return float64_maybe_silence_nan(rs1, &env->fp_status); 290 + } 291 + 292 + uint64_t helper_fsqrt_d(CPURISCVState *env, uint64_t frs1) 293 + { 294 + return float64_sqrt(frs1, &env->fp_status); 295 + } 296 + 297 + target_ulong helper_fle_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 298 + { 299 + return float64_le(frs1, frs2, &env->fp_status); 300 + } 301 + 302 + target_ulong helper_flt_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 303 + { 304 + return float64_lt(frs1, frs2, &env->fp_status); 305 + } 306 + 307 + target_ulong helper_feq_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2) 308 + { 309 + return float64_eq_quiet(frs1, frs2, &env->fp_status); 310 + } 311 + 312 + target_ulong helper_fcvt_w_d(CPURISCVState *env, uint64_t frs1) 313 + { 314 + return float64_to_int32(frs1, &env->fp_status); 315 + } 316 + 317 + target_ulong helper_fcvt_wu_d(CPURISCVState *env, uint64_t frs1) 318 + { 319 + return (int32_t)float64_to_uint32(frs1, &env->fp_status); 320 + } 321 + 322 + #if defined(TARGET_RISCV64) 323 + uint64_t helper_fcvt_l_d(CPURISCVState *env, uint64_t frs1) 324 + { 325 + return float64_to_int64(frs1, &env->fp_status); 326 + } 327 + 328 + uint64_t helper_fcvt_lu_d(CPURISCVState *env, uint64_t frs1) 329 + { 330 + return float64_to_uint64(frs1, &env->fp_status); 331 + } 332 + #endif 333 + 334 + uint64_t helper_fcvt_d_w(CPURISCVState *env, target_ulong rs1) 335 + { 336 + return int32_to_float64((int32_t)rs1, &env->fp_status); 337 + } 338 + 339 + uint64_t helper_fcvt_d_wu(CPURISCVState *env, target_ulong rs1) 340 + { 341 + return uint32_to_float64((uint32_t)rs1, &env->fp_status); 342 + } 343 + 344 + #if defined(TARGET_RISCV64) 345 + uint64_t helper_fcvt_d_l(CPURISCVState *env, uint64_t rs1) 346 + { 347 + return int64_to_float64(rs1, &env->fp_status); 348 + } 349 + 350 + uint64_t helper_fcvt_d_lu(CPURISCVState *env, uint64_t rs1) 351 + { 352 + return uint64_to_float64(rs1, &env->fp_status); 353 + } 354 + #endif 355 + 356 + target_ulong helper_fclass_d(uint64_t frs1) 357 + { 358 + float64 f = frs1; 359 + bool sign = float64_is_neg(f); 360 + 361 + if (float64_is_infinity(f)) { 362 + return sign ? 1 << 0 : 1 << 7; 363 + } else if (float64_is_zero(f)) { 364 + return sign ? 1 << 3 : 1 << 4; 365 + } else if (float64_is_zero_or_denormal(f)) { 366 + return sign ? 1 << 2 : 1 << 5; 367 + } else if (float64_is_any_nan(f)) { 368 + float_status s = { }; /* for snan_bit_is_one */ 369 + return float64_is_quiet_nan(f, &s) ? 1 << 9 : 1 << 8; 370 + } else { 371 + return sign ? 1 << 1 : 1 << 6; 372 + } 373 + }
+62
target/riscv/gdbstub.c
··· 1 + /* 2 + * RISC-V GDB Server Stub 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #include "qemu/osdep.h" 20 + #include "qemu-common.h" 21 + #include "exec/gdbstub.h" 22 + #include "cpu.h" 23 + 24 + int riscv_cpu_gdb_read_register(CPUState *cs, uint8_t *mem_buf, int n) 25 + { 26 + RISCVCPU *cpu = RISCV_CPU(cs); 27 + CPURISCVState *env = &cpu->env; 28 + 29 + if (n < 32) { 30 + return gdb_get_regl(mem_buf, env->gpr[n]); 31 + } else if (n == 32) { 32 + return gdb_get_regl(mem_buf, env->pc); 33 + } else if (n < 65) { 34 + return gdb_get_reg64(mem_buf, env->fpr[n - 33]); 35 + } else if (n < 4096 + 65) { 36 + return gdb_get_regl(mem_buf, csr_read_helper(env, n - 65)); 37 + } 38 + return 0; 39 + } 40 + 41 + int riscv_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) 42 + { 43 + RISCVCPU *cpu = RISCV_CPU(cs); 44 + CPURISCVState *env = &cpu->env; 45 + 46 + if (n == 0) { 47 + /* discard writes to x0 */ 48 + return sizeof(target_ulong); 49 + } else if (n < 32) { 50 + env->gpr[n] = ldtul_p(mem_buf); 51 + return sizeof(target_ulong); 52 + } else if (n == 32) { 53 + env->pc = ldtul_p(mem_buf); 54 + return sizeof(target_ulong); 55 + } else if (n < 65) { 56 + env->fpr[n - 33] = ldq_p(mem_buf); /* always 64-bit */ 57 + return sizeof(uint64_t); 58 + } else if (n < 4096 + 65) { 59 + csr_write_helper(env, ldtul_p(mem_buf), n - 65); 60 + } 61 + return 0; 62 + }
+503
target/riscv/helper.c
··· 1 + /* 2 + * RISC-V emulation helpers for qemu. 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #include "qemu/osdep.h" 21 + #include "qemu/log.h" 22 + #include "cpu.h" 23 + #include "exec/exec-all.h" 24 + #include "tcg-op.h" 25 + 26 + #define RISCV_DEBUG_INTERRUPT 0 27 + 28 + int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch) 29 + { 30 + #ifdef CONFIG_USER_ONLY 31 + return 0; 32 + #else 33 + return env->priv; 34 + #endif 35 + } 36 + 37 + #ifndef CONFIG_USER_ONLY 38 + /* 39 + * Return RISC-V IRQ number if an interrupt should be taken, else -1. 40 + * Used in cpu-exec.c 41 + * 42 + * Adapted from Spike's processor_t::take_interrupt() 43 + */ 44 + static int riscv_cpu_hw_interrupts_pending(CPURISCVState *env) 45 + { 46 + target_ulong pending_interrupts = atomic_read(&env->mip) & env->mie; 47 + 48 + target_ulong mie = get_field(env->mstatus, MSTATUS_MIE); 49 + target_ulong m_enabled = env->priv < PRV_M || (env->priv == PRV_M && mie); 50 + target_ulong enabled_interrupts = pending_interrupts & 51 + ~env->mideleg & -m_enabled; 52 + 53 + target_ulong sie = get_field(env->mstatus, MSTATUS_SIE); 54 + target_ulong s_enabled = env->priv < PRV_S || (env->priv == PRV_S && sie); 55 + enabled_interrupts |= pending_interrupts & env->mideleg & 56 + -s_enabled; 57 + 58 + if (enabled_interrupts) { 59 + return ctz64(enabled_interrupts); /* since non-zero */ 60 + } else { 61 + return EXCP_NONE; /* indicates no pending interrupt */ 62 + } 63 + } 64 + #endif 65 + 66 + bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 67 + { 68 + #if !defined(CONFIG_USER_ONLY) 69 + if (interrupt_request & CPU_INTERRUPT_HARD) { 70 + RISCVCPU *cpu = RISCV_CPU(cs); 71 + CPURISCVState *env = &cpu->env; 72 + int interruptno = riscv_cpu_hw_interrupts_pending(env); 73 + if (interruptno >= 0) { 74 + cs->exception_index = RISCV_EXCP_INT_FLAG | interruptno; 75 + riscv_cpu_do_interrupt(cs); 76 + return true; 77 + } 78 + } 79 + #endif 80 + return false; 81 + } 82 + 83 + #if !defined(CONFIG_USER_ONLY) 84 + 85 + /* get_physical_address - get the physical address for this virtual address 86 + * 87 + * Do a page table walk to obtain the physical address corresponding to a 88 + * virtual address. Returns 0 if the translation was successful 89 + * 90 + * Adapted from Spike's mmu_t::translate and mmu_t::walk 91 + * 92 + */ 93 + static int get_physical_address(CPURISCVState *env, hwaddr *physical, 94 + int *prot, target_ulong addr, 95 + int access_type, int mmu_idx) 96 + { 97 + /* NOTE: the env->pc value visible here will not be 98 + * correct, but the value visible to the exception handler 99 + * (riscv_cpu_do_interrupt) is correct */ 100 + 101 + int mode = mmu_idx; 102 + 103 + if (mode == PRV_M && access_type != MMU_INST_FETCH) { 104 + if (get_field(env->mstatus, MSTATUS_MPRV)) { 105 + mode = get_field(env->mstatus, MSTATUS_MPP); 106 + } 107 + } 108 + 109 + if (mode == PRV_M || !riscv_feature(env, RISCV_FEATURE_MMU)) { 110 + *physical = addr; 111 + *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 112 + return TRANSLATE_SUCCESS; 113 + } 114 + 115 + *prot = 0; 116 + 117 + target_ulong base; 118 + int levels, ptidxbits, ptesize, vm, sum; 119 + int mxr = get_field(env->mstatus, MSTATUS_MXR); 120 + 121 + if (env->priv_ver >= PRIV_VERSION_1_10_0) { 122 + base = get_field(env->satp, SATP_PPN) << PGSHIFT; 123 + sum = get_field(env->mstatus, MSTATUS_SUM); 124 + vm = get_field(env->satp, SATP_MODE); 125 + switch (vm) { 126 + case VM_1_10_SV32: 127 + levels = 2; ptidxbits = 10; ptesize = 4; break; 128 + case VM_1_10_SV39: 129 + levels = 3; ptidxbits = 9; ptesize = 8; break; 130 + case VM_1_10_SV48: 131 + levels = 4; ptidxbits = 9; ptesize = 8; break; 132 + case VM_1_10_SV57: 133 + levels = 5; ptidxbits = 9; ptesize = 8; break; 134 + case VM_1_10_MBARE: 135 + *physical = addr; 136 + *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 137 + return TRANSLATE_SUCCESS; 138 + default: 139 + g_assert_not_reached(); 140 + } 141 + } else { 142 + base = env->sptbr << PGSHIFT; 143 + sum = !get_field(env->mstatus, MSTATUS_PUM); 144 + vm = get_field(env->mstatus, MSTATUS_VM); 145 + switch (vm) { 146 + case VM_1_09_SV32: 147 + levels = 2; ptidxbits = 10; ptesize = 4; break; 148 + case VM_1_09_SV39: 149 + levels = 3; ptidxbits = 9; ptesize = 8; break; 150 + case VM_1_09_SV48: 151 + levels = 4; ptidxbits = 9; ptesize = 8; break; 152 + case VM_1_09_MBARE: 153 + *physical = addr; 154 + *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 155 + return TRANSLATE_SUCCESS; 156 + default: 157 + g_assert_not_reached(); 158 + } 159 + } 160 + 161 + CPUState *cs = CPU(riscv_env_get_cpu(env)); 162 + int va_bits = PGSHIFT + levels * ptidxbits; 163 + target_ulong mask = (1L << (TARGET_LONG_BITS - (va_bits - 1))) - 1; 164 + target_ulong masked_msbs = (addr >> (va_bits - 1)) & mask; 165 + if (masked_msbs != 0 && masked_msbs != mask) { 166 + return TRANSLATE_FAIL; 167 + } 168 + 169 + int ptshift = (levels - 1) * ptidxbits; 170 + int i; 171 + 172 + #if !TCG_OVERSIZED_GUEST 173 + restart: 174 + #endif 175 + for (i = 0; i < levels; i++, ptshift -= ptidxbits) { 176 + target_ulong idx = (addr >> (PGSHIFT + ptshift)) & 177 + ((1 << ptidxbits) - 1); 178 + 179 + /* check that physical address of PTE is legal */ 180 + target_ulong pte_addr = base + idx * ptesize; 181 + #if defined(TARGET_RISCV32) 182 + target_ulong pte = ldl_phys(cs->as, pte_addr); 183 + #elif defined(TARGET_RISCV64) 184 + target_ulong pte = ldq_phys(cs->as, pte_addr); 185 + #endif 186 + target_ulong ppn = pte >> PTE_PPN_SHIFT; 187 + 188 + if (PTE_TABLE(pte)) { /* next level of page table */ 189 + base = ppn << PGSHIFT; 190 + } else if ((pte & PTE_U) ? (mode == PRV_S) && !sum : !(mode == PRV_S)) { 191 + break; 192 + } else if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W))) { 193 + break; 194 + } else if (access_type == MMU_INST_FETCH ? !(pte & PTE_X) : 195 + access_type == MMU_DATA_LOAD ? !(pte & PTE_R) && 196 + !(mxr && (pte & PTE_X)) : !((pte & PTE_R) && (pte & PTE_W))) { 197 + break; 198 + } else { 199 + /* if necessary, set accessed and dirty bits. */ 200 + target_ulong updated_pte = pte | PTE_A | 201 + (access_type == MMU_DATA_STORE ? PTE_D : 0); 202 + 203 + /* Page table updates need to be atomic with MTTCG enabled */ 204 + if (updated_pte != pte) { 205 + /* if accessed or dirty bits need updating, and the PTE is 206 + * in RAM, then we do so atomically with a compare and swap. 207 + * if the PTE is in IO space, then it can't be updated. 208 + * if the PTE changed, then we must re-walk the page table 209 + as the PTE is no longer valid */ 210 + MemoryRegion *mr; 211 + hwaddr l = sizeof(target_ulong), addr1; 212 + mr = address_space_translate(cs->as, pte_addr, 213 + &addr1, &l, false); 214 + if (memory_access_is_direct(mr, true)) { 215 + target_ulong *pte_pa = 216 + qemu_map_ram_ptr(mr->ram_block, addr1); 217 + #if TCG_OVERSIZED_GUEST 218 + /* MTTCG is not enabled on oversized TCG guests so 219 + * page table updates do not need to be atomic */ 220 + *pte_pa = pte = updated_pte; 221 + #else 222 + target_ulong old_pte = 223 + atomic_cmpxchg(pte_pa, pte, updated_pte); 224 + if (old_pte != pte) { 225 + goto restart; 226 + } else { 227 + pte = updated_pte; 228 + } 229 + #endif 230 + } else { 231 + /* misconfigured PTE in ROM (AD bits are not preset) or 232 + * PTE is in IO space and can't be updated atomically */ 233 + return TRANSLATE_FAIL; 234 + } 235 + } 236 + 237 + /* for superpage mappings, make a fake leaf PTE for the TLB's 238 + benefit. */ 239 + target_ulong vpn = addr >> PGSHIFT; 240 + *physical = (ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT; 241 + 242 + if ((pte & PTE_R)) { 243 + *prot |= PAGE_READ; 244 + } 245 + if ((pte & PTE_X)) { 246 + *prot |= PAGE_EXEC; 247 + } 248 + /* only add write permission on stores or if the page 249 + is already dirty, so that we don't miss further 250 + page table walks to update the dirty bit */ 251 + if ((pte & PTE_W) && 252 + (access_type == MMU_DATA_STORE || (pte & PTE_D))) { 253 + *prot |= PAGE_WRITE; 254 + } 255 + return TRANSLATE_SUCCESS; 256 + } 257 + } 258 + return TRANSLATE_FAIL; 259 + } 260 + 261 + static void raise_mmu_exception(CPURISCVState *env, target_ulong address, 262 + MMUAccessType access_type) 263 + { 264 + CPUState *cs = CPU(riscv_env_get_cpu(env)); 265 + int page_fault_exceptions = 266 + (env->priv_ver >= PRIV_VERSION_1_10_0) && 267 + get_field(env->satp, SATP_MODE) != VM_1_10_MBARE; 268 + switch (access_type) { 269 + case MMU_INST_FETCH: 270 + cs->exception_index = page_fault_exceptions ? 271 + RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT; 272 + break; 273 + case MMU_DATA_LOAD: 274 + cs->exception_index = page_fault_exceptions ? 275 + RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT; 276 + break; 277 + case MMU_DATA_STORE: 278 + cs->exception_index = page_fault_exceptions ? 279 + RISCV_EXCP_STORE_PAGE_FAULT : RISCV_EXCP_STORE_AMO_ACCESS_FAULT; 280 + break; 281 + default: 282 + g_assert_not_reached(); 283 + } 284 + env->badaddr = address; 285 + } 286 + 287 + hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) 288 + { 289 + RISCVCPU *cpu = RISCV_CPU(cs); 290 + hwaddr phys_addr; 291 + int prot; 292 + int mmu_idx = cpu_mmu_index(&cpu->env, false); 293 + 294 + if (get_physical_address(&cpu->env, &phys_addr, &prot, addr, 0, mmu_idx)) { 295 + return -1; 296 + } 297 + return phys_addr; 298 + } 299 + 300 + void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, 301 + MMUAccessType access_type, int mmu_idx, 302 + uintptr_t retaddr) 303 + { 304 + RISCVCPU *cpu = RISCV_CPU(cs); 305 + CPURISCVState *env = &cpu->env; 306 + switch (access_type) { 307 + case MMU_INST_FETCH: 308 + cs->exception_index = RISCV_EXCP_INST_ADDR_MIS; 309 + break; 310 + case MMU_DATA_LOAD: 311 + cs->exception_index = RISCV_EXCP_LOAD_ADDR_MIS; 312 + break; 313 + case MMU_DATA_STORE: 314 + cs->exception_index = RISCV_EXCP_STORE_AMO_ADDR_MIS; 315 + break; 316 + default: 317 + g_assert_not_reached(); 318 + } 319 + env->badaddr = addr; 320 + do_raise_exception_err(env, cs->exception_index, retaddr); 321 + } 322 + 323 + /* called by qemu's softmmu to fill the qemu tlb */ 324 + void tlb_fill(CPUState *cs, target_ulong addr, int size, 325 + MMUAccessType access_type, int mmu_idx, uintptr_t retaddr) 326 + { 327 + int ret; 328 + ret = riscv_cpu_handle_mmu_fault(cs, addr, size, access_type, mmu_idx); 329 + if (ret == TRANSLATE_FAIL) { 330 + RISCVCPU *cpu = RISCV_CPU(cs); 331 + CPURISCVState *env = &cpu->env; 332 + do_raise_exception_err(env, cs->exception_index, retaddr); 333 + } 334 + } 335 + 336 + #endif 337 + 338 + int riscv_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, 339 + int rw, int mmu_idx) 340 + { 341 + RISCVCPU *cpu = RISCV_CPU(cs); 342 + CPURISCVState *env = &cpu->env; 343 + #if !defined(CONFIG_USER_ONLY) 344 + hwaddr pa = 0; 345 + int prot; 346 + #endif 347 + int ret = TRANSLATE_FAIL; 348 + 349 + qemu_log_mask(CPU_LOG_MMU, 350 + "%s pc " TARGET_FMT_lx " ad %" VADDR_PRIx " rw %d mmu_idx \ 351 + %d\n", __func__, env->pc, address, rw, mmu_idx); 352 + 353 + #if !defined(CONFIG_USER_ONLY) 354 + ret = get_physical_address(env, &pa, &prot, address, rw, mmu_idx); 355 + qemu_log_mask(CPU_LOG_MMU, 356 + "%s address=%" VADDR_PRIx " ret %d physical " TARGET_FMT_plx 357 + " prot %d\n", __func__, address, ret, pa, prot); 358 + if (!pmp_hart_has_privs(env, pa, TARGET_PAGE_SIZE, 1 << rw)) { 359 + ret = TRANSLATE_FAIL; 360 + } 361 + if (ret == TRANSLATE_SUCCESS) { 362 + tlb_set_page(cs, address & TARGET_PAGE_MASK, pa & TARGET_PAGE_MASK, 363 + prot, mmu_idx, TARGET_PAGE_SIZE); 364 + } else if (ret == TRANSLATE_FAIL) { 365 + raise_mmu_exception(env, address, rw); 366 + } 367 + #else 368 + switch (rw) { 369 + case MMU_INST_FETCH: 370 + cs->exception_index = RISCV_EXCP_INST_PAGE_FAULT; 371 + break; 372 + case MMU_DATA_LOAD: 373 + cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT; 374 + break; 375 + case MMU_DATA_STORE: 376 + cs->exception_index = RISCV_EXCP_STORE_PAGE_FAULT; 377 + break; 378 + } 379 + #endif 380 + return ret; 381 + } 382 + 383 + /* 384 + * Handle Traps 385 + * 386 + * Adapted from Spike's processor_t::take_trap. 387 + * 388 + */ 389 + void riscv_cpu_do_interrupt(CPUState *cs) 390 + { 391 + #if !defined(CONFIG_USER_ONLY) 392 + 393 + RISCVCPU *cpu = RISCV_CPU(cs); 394 + CPURISCVState *env = &cpu->env; 395 + 396 + if (RISCV_DEBUG_INTERRUPT) { 397 + int log_cause = cs->exception_index & RISCV_EXCP_INT_MASK; 398 + if (cs->exception_index & RISCV_EXCP_INT_FLAG) { 399 + qemu_log_mask(LOG_TRACE, "core 0: trap %s, epc 0x" TARGET_FMT_lx, 400 + riscv_intr_names[log_cause], env->pc); 401 + } else { 402 + qemu_log_mask(LOG_TRACE, "core 0: intr %s, epc 0x" TARGET_FMT_lx, 403 + riscv_excp_names[log_cause], env->pc); 404 + } 405 + } 406 + 407 + target_ulong fixed_cause = 0; 408 + if (cs->exception_index & (RISCV_EXCP_INT_FLAG)) { 409 + /* hacky for now. the MSB (bit 63) indicates interrupt but cs->exception 410 + index is only 32 bits wide */ 411 + fixed_cause = cs->exception_index & RISCV_EXCP_INT_MASK; 412 + fixed_cause |= ((target_ulong)1) << (TARGET_LONG_BITS - 1); 413 + } else { 414 + /* fixup User ECALL -> correct priv ECALL */ 415 + if (cs->exception_index == RISCV_EXCP_U_ECALL) { 416 + switch (env->priv) { 417 + case PRV_U: 418 + fixed_cause = RISCV_EXCP_U_ECALL; 419 + break; 420 + case PRV_S: 421 + fixed_cause = RISCV_EXCP_S_ECALL; 422 + break; 423 + case PRV_H: 424 + fixed_cause = RISCV_EXCP_H_ECALL; 425 + break; 426 + case PRV_M: 427 + fixed_cause = RISCV_EXCP_M_ECALL; 428 + break; 429 + } 430 + } else { 431 + fixed_cause = cs->exception_index; 432 + } 433 + } 434 + 435 + target_ulong backup_epc = env->pc; 436 + 437 + target_ulong bit = fixed_cause; 438 + target_ulong deleg = env->medeleg; 439 + 440 + int hasbadaddr = 441 + (fixed_cause == RISCV_EXCP_INST_ADDR_MIS) || 442 + (fixed_cause == RISCV_EXCP_INST_ACCESS_FAULT) || 443 + (fixed_cause == RISCV_EXCP_LOAD_ADDR_MIS) || 444 + (fixed_cause == RISCV_EXCP_STORE_AMO_ADDR_MIS) || 445 + (fixed_cause == RISCV_EXCP_LOAD_ACCESS_FAULT) || 446 + (fixed_cause == RISCV_EXCP_STORE_AMO_ACCESS_FAULT) || 447 + (fixed_cause == RISCV_EXCP_INST_PAGE_FAULT) || 448 + (fixed_cause == RISCV_EXCP_LOAD_PAGE_FAULT) || 449 + (fixed_cause == RISCV_EXCP_STORE_PAGE_FAULT); 450 + 451 + if (bit & ((target_ulong)1 << (TARGET_LONG_BITS - 1))) { 452 + deleg = env->mideleg; 453 + bit &= ~((target_ulong)1 << (TARGET_LONG_BITS - 1)); 454 + } 455 + 456 + if (env->priv <= PRV_S && bit < 64 && ((deleg >> bit) & 1)) { 457 + /* handle the trap in S-mode */ 458 + /* No need to check STVEC for misaligned - lower 2 bits cannot be set */ 459 + env->pc = env->stvec; 460 + env->scause = fixed_cause; 461 + env->sepc = backup_epc; 462 + 463 + if (hasbadaddr) { 464 + if (RISCV_DEBUG_INTERRUPT) { 465 + qemu_log_mask(LOG_TRACE, "core " TARGET_FMT_ld 466 + ": badaddr 0x" TARGET_FMT_lx, env->mhartid, env->badaddr); 467 + } 468 + env->sbadaddr = env->badaddr; 469 + } 470 + 471 + target_ulong s = env->mstatus; 472 + s = set_field(s, MSTATUS_SPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? 473 + get_field(s, MSTATUS_SIE) : get_field(s, MSTATUS_UIE << env->priv)); 474 + s = set_field(s, MSTATUS_SPP, env->priv); 475 + s = set_field(s, MSTATUS_SIE, 0); 476 + csr_write_helper(env, s, CSR_MSTATUS); 477 + riscv_set_mode(env, PRV_S); 478 + } else { 479 + /* No need to check MTVEC for misaligned - lower 2 bits cannot be set */ 480 + env->pc = env->mtvec; 481 + env->mepc = backup_epc; 482 + env->mcause = fixed_cause; 483 + 484 + if (hasbadaddr) { 485 + if (RISCV_DEBUG_INTERRUPT) { 486 + qemu_log_mask(LOG_TRACE, "core " TARGET_FMT_ld 487 + ": badaddr 0x" TARGET_FMT_lx, env->mhartid, env->badaddr); 488 + } 489 + env->mbadaddr = env->badaddr; 490 + } 491 + 492 + target_ulong s = env->mstatus; 493 + s = set_field(s, MSTATUS_MPIE, env->priv_ver >= PRIV_VERSION_1_10_0 ? 494 + get_field(s, MSTATUS_MIE) : get_field(s, MSTATUS_UIE << env->priv)); 495 + s = set_field(s, MSTATUS_MPP, env->priv); 496 + s = set_field(s, MSTATUS_MIE, 0); 497 + csr_write_helper(env, s, CSR_MSTATUS); 498 + riscv_set_mode(env, PRV_M); 499 + } 500 + /* TODO yield load reservation */ 501 + #endif 502 + cs->exception_index = EXCP_NONE; /* mark handled to qemu */ 503 + }
+78
target/riscv/helper.h
··· 1 + /* Exceptions */ 2 + DEF_HELPER_2(raise_exception, noreturn, env, i32) 3 + 4 + /* Floating Point - rounding mode */ 5 + DEF_HELPER_FLAGS_2(set_rounding_mode, TCG_CALL_NO_WG, void, env, i32) 6 + 7 + /* Floating Point - fused */ 8 + DEF_HELPER_FLAGS_4(fmadd_s, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 9 + DEF_HELPER_FLAGS_4(fmadd_d, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 10 + DEF_HELPER_FLAGS_4(fmsub_s, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 11 + DEF_HELPER_FLAGS_4(fmsub_d, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 12 + DEF_HELPER_FLAGS_4(fnmsub_s, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 13 + DEF_HELPER_FLAGS_4(fnmsub_d, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 14 + DEF_HELPER_FLAGS_4(fnmadd_s, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 15 + DEF_HELPER_FLAGS_4(fnmadd_d, TCG_CALL_NO_RWG, i64, env, i64, i64, i64) 16 + 17 + /* Floating Point - Single Precision */ 18 + DEF_HELPER_FLAGS_3(fadd_s, TCG_CALL_NO_RWG, i64, env, i64, i64) 19 + DEF_HELPER_FLAGS_3(fsub_s, TCG_CALL_NO_RWG, i64, env, i64, i64) 20 + DEF_HELPER_FLAGS_3(fmul_s, TCG_CALL_NO_RWG, i64, env, i64, i64) 21 + DEF_HELPER_FLAGS_3(fdiv_s, TCG_CALL_NO_RWG, i64, env, i64, i64) 22 + DEF_HELPER_FLAGS_3(fmin_s, TCG_CALL_NO_RWG, i64, env, i64, i64) 23 + DEF_HELPER_FLAGS_3(fmax_s, TCG_CALL_NO_RWG, i64, env, i64, i64) 24 + DEF_HELPER_FLAGS_2(fsqrt_s, TCG_CALL_NO_RWG, i64, env, i64) 25 + DEF_HELPER_FLAGS_3(fle_s, TCG_CALL_NO_RWG, tl, env, i64, i64) 26 + DEF_HELPER_FLAGS_3(flt_s, TCG_CALL_NO_RWG, tl, env, i64, i64) 27 + DEF_HELPER_FLAGS_3(feq_s, TCG_CALL_NO_RWG, tl, env, i64, i64) 28 + DEF_HELPER_FLAGS_2(fcvt_w_s, TCG_CALL_NO_RWG, tl, env, i64) 29 + DEF_HELPER_FLAGS_2(fcvt_wu_s, TCG_CALL_NO_RWG, tl, env, i64) 30 + #if defined(TARGET_RISCV64) 31 + DEF_HELPER_FLAGS_2(fcvt_l_s, TCG_CALL_NO_RWG, tl, env, i64) 32 + DEF_HELPER_FLAGS_2(fcvt_lu_s, TCG_CALL_NO_RWG, tl, env, i64) 33 + #endif 34 + DEF_HELPER_FLAGS_2(fcvt_s_w, TCG_CALL_NO_RWG, i64, env, tl) 35 + DEF_HELPER_FLAGS_2(fcvt_s_wu, TCG_CALL_NO_RWG, i64, env, tl) 36 + #if defined(TARGET_RISCV64) 37 + DEF_HELPER_FLAGS_2(fcvt_s_l, TCG_CALL_NO_RWG, i64, env, tl) 38 + DEF_HELPER_FLAGS_2(fcvt_s_lu, TCG_CALL_NO_RWG, i64, env, tl) 39 + #endif 40 + DEF_HELPER_FLAGS_1(fclass_s, TCG_CALL_NO_RWG_SE, tl, i64) 41 + 42 + /* Floating Point - Double Precision */ 43 + DEF_HELPER_FLAGS_3(fadd_d, TCG_CALL_NO_RWG, i64, env, i64, i64) 44 + DEF_HELPER_FLAGS_3(fsub_d, TCG_CALL_NO_RWG, i64, env, i64, i64) 45 + DEF_HELPER_FLAGS_3(fmul_d, TCG_CALL_NO_RWG, i64, env, i64, i64) 46 + DEF_HELPER_FLAGS_3(fdiv_d, TCG_CALL_NO_RWG, i64, env, i64, i64) 47 + DEF_HELPER_FLAGS_3(fmin_d, TCG_CALL_NO_RWG, i64, env, i64, i64) 48 + DEF_HELPER_FLAGS_3(fmax_d, TCG_CALL_NO_RWG, i64, env, i64, i64) 49 + DEF_HELPER_FLAGS_2(fcvt_s_d, TCG_CALL_NO_RWG, i64, env, i64) 50 + DEF_HELPER_FLAGS_2(fcvt_d_s, TCG_CALL_NO_RWG, i64, env, i64) 51 + DEF_HELPER_FLAGS_2(fsqrt_d, TCG_CALL_NO_RWG, i64, env, i64) 52 + DEF_HELPER_FLAGS_3(fle_d, TCG_CALL_NO_RWG, tl, env, i64, i64) 53 + DEF_HELPER_FLAGS_3(flt_d, TCG_CALL_NO_RWG, tl, env, i64, i64) 54 + DEF_HELPER_FLAGS_3(feq_d, TCG_CALL_NO_RWG, tl, env, i64, i64) 55 + DEF_HELPER_FLAGS_2(fcvt_w_d, TCG_CALL_NO_RWG, tl, env, i64) 56 + DEF_HELPER_FLAGS_2(fcvt_wu_d, TCG_CALL_NO_RWG, tl, env, i64) 57 + #if defined(TARGET_RISCV64) 58 + DEF_HELPER_FLAGS_2(fcvt_l_d, TCG_CALL_NO_RWG, tl, env, i64) 59 + DEF_HELPER_FLAGS_2(fcvt_lu_d, TCG_CALL_NO_RWG, tl, env, i64) 60 + #endif 61 + DEF_HELPER_FLAGS_2(fcvt_d_w, TCG_CALL_NO_RWG, i64, env, tl) 62 + DEF_HELPER_FLAGS_2(fcvt_d_wu, TCG_CALL_NO_RWG, i64, env, tl) 63 + #if defined(TARGET_RISCV64) 64 + DEF_HELPER_FLAGS_2(fcvt_d_l, TCG_CALL_NO_RWG, i64, env, tl) 65 + DEF_HELPER_FLAGS_2(fcvt_d_lu, TCG_CALL_NO_RWG, i64, env, tl) 66 + #endif 67 + DEF_HELPER_FLAGS_1(fclass_d, TCG_CALL_NO_RWG_SE, tl, i64) 68 + 69 + /* Special functions */ 70 + DEF_HELPER_3(csrrw, tl, env, tl, tl) 71 + DEF_HELPER_4(csrrs, tl, env, tl, tl, tl) 72 + DEF_HELPER_4(csrrc, tl, env, tl, tl, tl) 73 + #ifndef CONFIG_USER_ONLY 74 + DEF_HELPER_2(sret, tl, env, tl) 75 + DEF_HELPER_2(mret, tl, env, tl) 76 + DEF_HELPER_1(wfi, void, env) 77 + DEF_HELPER_1(tlb_flush, void, env) 78 + #endif
+364
target/riscv/instmap.h
··· 1 + /* 2 + * RISC-V emulation for qemu: Instruction decode helpers 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #define MASK_OP_MAJOR(op) (op & 0x7F) 20 + enum { 21 + /* rv32i, rv64i, rv32m */ 22 + OPC_RISC_LUI = (0x37), 23 + OPC_RISC_AUIPC = (0x17), 24 + OPC_RISC_JAL = (0x6F), 25 + OPC_RISC_JALR = (0x67), 26 + OPC_RISC_BRANCH = (0x63), 27 + OPC_RISC_LOAD = (0x03), 28 + OPC_RISC_STORE = (0x23), 29 + OPC_RISC_ARITH_IMM = (0x13), 30 + OPC_RISC_ARITH = (0x33), 31 + OPC_RISC_FENCE = (0x0F), 32 + OPC_RISC_SYSTEM = (0x73), 33 + 34 + /* rv64i, rv64m */ 35 + OPC_RISC_ARITH_IMM_W = (0x1B), 36 + OPC_RISC_ARITH_W = (0x3B), 37 + 38 + /* rv32a, rv64a */ 39 + OPC_RISC_ATOMIC = (0x2F), 40 + 41 + /* floating point */ 42 + OPC_RISC_FP_LOAD = (0x7), 43 + OPC_RISC_FP_STORE = (0x27), 44 + 45 + OPC_RISC_FMADD = (0x43), 46 + OPC_RISC_FMSUB = (0x47), 47 + OPC_RISC_FNMSUB = (0x4B), 48 + OPC_RISC_FNMADD = (0x4F), 49 + 50 + OPC_RISC_FP_ARITH = (0x53), 51 + }; 52 + 53 + #define MASK_OP_ARITH(op) (MASK_OP_MAJOR(op) | (op & ((0x7 << 12) | \ 54 + (0x7F << 25)))) 55 + enum { 56 + OPC_RISC_ADD = OPC_RISC_ARITH | (0x0 << 12) | (0x00 << 25), 57 + OPC_RISC_SUB = OPC_RISC_ARITH | (0x0 << 12) | (0x20 << 25), 58 + OPC_RISC_SLL = OPC_RISC_ARITH | (0x1 << 12) | (0x00 << 25), 59 + OPC_RISC_SLT = OPC_RISC_ARITH | (0x2 << 12) | (0x00 << 25), 60 + OPC_RISC_SLTU = OPC_RISC_ARITH | (0x3 << 12) | (0x00 << 25), 61 + OPC_RISC_XOR = OPC_RISC_ARITH | (0x4 << 12) | (0x00 << 25), 62 + OPC_RISC_SRL = OPC_RISC_ARITH | (0x5 << 12) | (0x00 << 25), 63 + OPC_RISC_SRA = OPC_RISC_ARITH | (0x5 << 12) | (0x20 << 25), 64 + OPC_RISC_OR = OPC_RISC_ARITH | (0x6 << 12) | (0x00 << 25), 65 + OPC_RISC_AND = OPC_RISC_ARITH | (0x7 << 12) | (0x00 << 25), 66 + 67 + /* RV64M */ 68 + OPC_RISC_MUL = OPC_RISC_ARITH | (0x0 << 12) | (0x01 << 25), 69 + OPC_RISC_MULH = OPC_RISC_ARITH | (0x1 << 12) | (0x01 << 25), 70 + OPC_RISC_MULHSU = OPC_RISC_ARITH | (0x2 << 12) | (0x01 << 25), 71 + OPC_RISC_MULHU = OPC_RISC_ARITH | (0x3 << 12) | (0x01 << 25), 72 + 73 + OPC_RISC_DIV = OPC_RISC_ARITH | (0x4 << 12) | (0x01 << 25), 74 + OPC_RISC_DIVU = OPC_RISC_ARITH | (0x5 << 12) | (0x01 << 25), 75 + OPC_RISC_REM = OPC_RISC_ARITH | (0x6 << 12) | (0x01 << 25), 76 + OPC_RISC_REMU = OPC_RISC_ARITH | (0x7 << 12) | (0x01 << 25), 77 + }; 78 + 79 + 80 + #define MASK_OP_ARITH_IMM(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 81 + enum { 82 + OPC_RISC_ADDI = OPC_RISC_ARITH_IMM | (0x0 << 12), 83 + OPC_RISC_SLTI = OPC_RISC_ARITH_IMM | (0x2 << 12), 84 + OPC_RISC_SLTIU = OPC_RISC_ARITH_IMM | (0x3 << 12), 85 + OPC_RISC_XORI = OPC_RISC_ARITH_IMM | (0x4 << 12), 86 + OPC_RISC_ORI = OPC_RISC_ARITH_IMM | (0x6 << 12), 87 + OPC_RISC_ANDI = OPC_RISC_ARITH_IMM | (0x7 << 12), 88 + OPC_RISC_SLLI = OPC_RISC_ARITH_IMM | (0x1 << 12), /* additional part of 89 + IMM */ 90 + OPC_RISC_SHIFT_RIGHT_I = OPC_RISC_ARITH_IMM | (0x5 << 12) /* SRAI, SRLI */ 91 + }; 92 + 93 + #define MASK_OP_BRANCH(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 94 + enum { 95 + OPC_RISC_BEQ = OPC_RISC_BRANCH | (0x0 << 12), 96 + OPC_RISC_BNE = OPC_RISC_BRANCH | (0x1 << 12), 97 + OPC_RISC_BLT = OPC_RISC_BRANCH | (0x4 << 12), 98 + OPC_RISC_BGE = OPC_RISC_BRANCH | (0x5 << 12), 99 + OPC_RISC_BLTU = OPC_RISC_BRANCH | (0x6 << 12), 100 + OPC_RISC_BGEU = OPC_RISC_BRANCH | (0x7 << 12) 101 + }; 102 + 103 + enum { 104 + OPC_RISC_ADDIW = OPC_RISC_ARITH_IMM_W | (0x0 << 12), 105 + OPC_RISC_SLLIW = OPC_RISC_ARITH_IMM_W | (0x1 << 12), /* additional part of 106 + IMM */ 107 + OPC_RISC_SHIFT_RIGHT_IW = OPC_RISC_ARITH_IMM_W | (0x5 << 12) /* SRAI, SRLI 108 + */ 109 + }; 110 + 111 + enum { 112 + OPC_RISC_ADDW = OPC_RISC_ARITH_W | (0x0 << 12) | (0x00 << 25), 113 + OPC_RISC_SUBW = OPC_RISC_ARITH_W | (0x0 << 12) | (0x20 << 25), 114 + OPC_RISC_SLLW = OPC_RISC_ARITH_W | (0x1 << 12) | (0x00 << 25), 115 + OPC_RISC_SRLW = OPC_RISC_ARITH_W | (0x5 << 12) | (0x00 << 25), 116 + OPC_RISC_SRAW = OPC_RISC_ARITH_W | (0x5 << 12) | (0x20 << 25), 117 + 118 + /* RV64M */ 119 + OPC_RISC_MULW = OPC_RISC_ARITH_W | (0x0 << 12) | (0x01 << 25), 120 + OPC_RISC_DIVW = OPC_RISC_ARITH_W | (0x4 << 12) | (0x01 << 25), 121 + OPC_RISC_DIVUW = OPC_RISC_ARITH_W | (0x5 << 12) | (0x01 << 25), 122 + OPC_RISC_REMW = OPC_RISC_ARITH_W | (0x6 << 12) | (0x01 << 25), 123 + OPC_RISC_REMUW = OPC_RISC_ARITH_W | (0x7 << 12) | (0x01 << 25), 124 + }; 125 + 126 + #define MASK_OP_LOAD(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 127 + enum { 128 + OPC_RISC_LB = OPC_RISC_LOAD | (0x0 << 12), 129 + OPC_RISC_LH = OPC_RISC_LOAD | (0x1 << 12), 130 + OPC_RISC_LW = OPC_RISC_LOAD | (0x2 << 12), 131 + OPC_RISC_LD = OPC_RISC_LOAD | (0x3 << 12), 132 + OPC_RISC_LBU = OPC_RISC_LOAD | (0x4 << 12), 133 + OPC_RISC_LHU = OPC_RISC_LOAD | (0x5 << 12), 134 + OPC_RISC_LWU = OPC_RISC_LOAD | (0x6 << 12), 135 + }; 136 + 137 + #define MASK_OP_STORE(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 138 + enum { 139 + OPC_RISC_SB = OPC_RISC_STORE | (0x0 << 12), 140 + OPC_RISC_SH = OPC_RISC_STORE | (0x1 << 12), 141 + OPC_RISC_SW = OPC_RISC_STORE | (0x2 << 12), 142 + OPC_RISC_SD = OPC_RISC_STORE | (0x3 << 12), 143 + }; 144 + 145 + #define MASK_OP_JALR(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 146 + /* no enum since OPC_RISC_JALR is the actual value */ 147 + 148 + #define MASK_OP_ATOMIC(op) \ 149 + (MASK_OP_MAJOR(op) | (op & ((0x7 << 12) | (0x7F << 25)))) 150 + #define MASK_OP_ATOMIC_NO_AQ_RL_SZ(op) \ 151 + (MASK_OP_MAJOR(op) | (op & (0x1F << 27))) 152 + 153 + enum { 154 + OPC_RISC_LR = OPC_RISC_ATOMIC | (0x02 << 27), 155 + OPC_RISC_SC = OPC_RISC_ATOMIC | (0x03 << 27), 156 + OPC_RISC_AMOSWAP = OPC_RISC_ATOMIC | (0x01 << 27), 157 + OPC_RISC_AMOADD = OPC_RISC_ATOMIC | (0x00 << 27), 158 + OPC_RISC_AMOXOR = OPC_RISC_ATOMIC | (0x04 << 27), 159 + OPC_RISC_AMOAND = OPC_RISC_ATOMIC | (0x0C << 27), 160 + OPC_RISC_AMOOR = OPC_RISC_ATOMIC | (0x08 << 27), 161 + OPC_RISC_AMOMIN = OPC_RISC_ATOMIC | (0x10 << 27), 162 + OPC_RISC_AMOMAX = OPC_RISC_ATOMIC | (0x14 << 27), 163 + OPC_RISC_AMOMINU = OPC_RISC_ATOMIC | (0x18 << 27), 164 + OPC_RISC_AMOMAXU = OPC_RISC_ATOMIC | (0x1C << 27), 165 + }; 166 + 167 + #define MASK_OP_SYSTEM(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 168 + enum { 169 + OPC_RISC_ECALL = OPC_RISC_SYSTEM | (0x0 << 12), 170 + OPC_RISC_EBREAK = OPC_RISC_SYSTEM | (0x0 << 12), 171 + OPC_RISC_ERET = OPC_RISC_SYSTEM | (0x0 << 12), 172 + OPC_RISC_MRTS = OPC_RISC_SYSTEM | (0x0 << 12), 173 + OPC_RISC_MRTH = OPC_RISC_SYSTEM | (0x0 << 12), 174 + OPC_RISC_HRTS = OPC_RISC_SYSTEM | (0x0 << 12), 175 + OPC_RISC_WFI = OPC_RISC_SYSTEM | (0x0 << 12), 176 + OPC_RISC_SFENCEVM = OPC_RISC_SYSTEM | (0x0 << 12), 177 + 178 + OPC_RISC_CSRRW = OPC_RISC_SYSTEM | (0x1 << 12), 179 + OPC_RISC_CSRRS = OPC_RISC_SYSTEM | (0x2 << 12), 180 + OPC_RISC_CSRRC = OPC_RISC_SYSTEM | (0x3 << 12), 181 + OPC_RISC_CSRRWI = OPC_RISC_SYSTEM | (0x5 << 12), 182 + OPC_RISC_CSRRSI = OPC_RISC_SYSTEM | (0x6 << 12), 183 + OPC_RISC_CSRRCI = OPC_RISC_SYSTEM | (0x7 << 12), 184 + }; 185 + 186 + #define MASK_OP_FP_LOAD(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 187 + enum { 188 + OPC_RISC_FLW = OPC_RISC_FP_LOAD | (0x2 << 12), 189 + OPC_RISC_FLD = OPC_RISC_FP_LOAD | (0x3 << 12), 190 + }; 191 + 192 + #define MASK_OP_FP_STORE(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12))) 193 + enum { 194 + OPC_RISC_FSW = OPC_RISC_FP_STORE | (0x2 << 12), 195 + OPC_RISC_FSD = OPC_RISC_FP_STORE | (0x3 << 12), 196 + }; 197 + 198 + #define MASK_OP_FP_FMADD(op) (MASK_OP_MAJOR(op) | (op & (0x3 << 25))) 199 + enum { 200 + OPC_RISC_FMADD_S = OPC_RISC_FMADD | (0x0 << 25), 201 + OPC_RISC_FMADD_D = OPC_RISC_FMADD | (0x1 << 25), 202 + }; 203 + 204 + #define MASK_OP_FP_FMSUB(op) (MASK_OP_MAJOR(op) | (op & (0x3 << 25))) 205 + enum { 206 + OPC_RISC_FMSUB_S = OPC_RISC_FMSUB | (0x0 << 25), 207 + OPC_RISC_FMSUB_D = OPC_RISC_FMSUB | (0x1 << 25), 208 + }; 209 + 210 + #define MASK_OP_FP_FNMADD(op) (MASK_OP_MAJOR(op) | (op & (0x3 << 25))) 211 + enum { 212 + OPC_RISC_FNMADD_S = OPC_RISC_FNMADD | (0x0 << 25), 213 + OPC_RISC_FNMADD_D = OPC_RISC_FNMADD | (0x1 << 25), 214 + }; 215 + 216 + #define MASK_OP_FP_FNMSUB(op) (MASK_OP_MAJOR(op) | (op & (0x3 << 25))) 217 + enum { 218 + OPC_RISC_FNMSUB_S = OPC_RISC_FNMSUB | (0x0 << 25), 219 + OPC_RISC_FNMSUB_D = OPC_RISC_FNMSUB | (0x1 << 25), 220 + }; 221 + 222 + #define MASK_OP_FP_ARITH(op) (MASK_OP_MAJOR(op) | (op & (0x7F << 25))) 223 + enum { 224 + /* float */ 225 + OPC_RISC_FADD_S = OPC_RISC_FP_ARITH | (0x0 << 25), 226 + OPC_RISC_FSUB_S = OPC_RISC_FP_ARITH | (0x4 << 25), 227 + OPC_RISC_FMUL_S = OPC_RISC_FP_ARITH | (0x8 << 25), 228 + OPC_RISC_FDIV_S = OPC_RISC_FP_ARITH | (0xC << 25), 229 + 230 + OPC_RISC_FSGNJ_S = OPC_RISC_FP_ARITH | (0x10 << 25), 231 + OPC_RISC_FSGNJN_S = OPC_RISC_FP_ARITH | (0x10 << 25), 232 + OPC_RISC_FSGNJX_S = OPC_RISC_FP_ARITH | (0x10 << 25), 233 + 234 + OPC_RISC_FMIN_S = OPC_RISC_FP_ARITH | (0x14 << 25), 235 + OPC_RISC_FMAX_S = OPC_RISC_FP_ARITH | (0x14 << 25), 236 + 237 + OPC_RISC_FSQRT_S = OPC_RISC_FP_ARITH | (0x2C << 25), 238 + 239 + OPC_RISC_FEQ_S = OPC_RISC_FP_ARITH | (0x50 << 25), 240 + OPC_RISC_FLT_S = OPC_RISC_FP_ARITH | (0x50 << 25), 241 + OPC_RISC_FLE_S = OPC_RISC_FP_ARITH | (0x50 << 25), 242 + 243 + OPC_RISC_FCVT_W_S = OPC_RISC_FP_ARITH | (0x60 << 25), 244 + OPC_RISC_FCVT_WU_S = OPC_RISC_FP_ARITH | (0x60 << 25), 245 + OPC_RISC_FCVT_L_S = OPC_RISC_FP_ARITH | (0x60 << 25), 246 + OPC_RISC_FCVT_LU_S = OPC_RISC_FP_ARITH | (0x60 << 25), 247 + 248 + OPC_RISC_FCVT_S_W = OPC_RISC_FP_ARITH | (0x68 << 25), 249 + OPC_RISC_FCVT_S_WU = OPC_RISC_FP_ARITH | (0x68 << 25), 250 + OPC_RISC_FCVT_S_L = OPC_RISC_FP_ARITH | (0x68 << 25), 251 + OPC_RISC_FCVT_S_LU = OPC_RISC_FP_ARITH | (0x68 << 25), 252 + 253 + OPC_RISC_FMV_X_S = OPC_RISC_FP_ARITH | (0x70 << 25), 254 + OPC_RISC_FCLASS_S = OPC_RISC_FP_ARITH | (0x70 << 25), 255 + 256 + OPC_RISC_FMV_S_X = OPC_RISC_FP_ARITH | (0x78 << 25), 257 + 258 + /* double */ 259 + OPC_RISC_FADD_D = OPC_RISC_FP_ARITH | (0x1 << 25), 260 + OPC_RISC_FSUB_D = OPC_RISC_FP_ARITH | (0x5 << 25), 261 + OPC_RISC_FMUL_D = OPC_RISC_FP_ARITH | (0x9 << 25), 262 + OPC_RISC_FDIV_D = OPC_RISC_FP_ARITH | (0xD << 25), 263 + 264 + OPC_RISC_FSGNJ_D = OPC_RISC_FP_ARITH | (0x11 << 25), 265 + OPC_RISC_FSGNJN_D = OPC_RISC_FP_ARITH | (0x11 << 25), 266 + OPC_RISC_FSGNJX_D = OPC_RISC_FP_ARITH | (0x11 << 25), 267 + 268 + OPC_RISC_FMIN_D = OPC_RISC_FP_ARITH | (0x15 << 25), 269 + OPC_RISC_FMAX_D = OPC_RISC_FP_ARITH | (0x15 << 25), 270 + 271 + OPC_RISC_FCVT_S_D = OPC_RISC_FP_ARITH | (0x20 << 25), 272 + 273 + OPC_RISC_FCVT_D_S = OPC_RISC_FP_ARITH | (0x21 << 25), 274 + 275 + OPC_RISC_FSQRT_D = OPC_RISC_FP_ARITH | (0x2D << 25), 276 + 277 + OPC_RISC_FEQ_D = OPC_RISC_FP_ARITH | (0x51 << 25), 278 + OPC_RISC_FLT_D = OPC_RISC_FP_ARITH | (0x51 << 25), 279 + OPC_RISC_FLE_D = OPC_RISC_FP_ARITH | (0x51 << 25), 280 + 281 + OPC_RISC_FCVT_W_D = OPC_RISC_FP_ARITH | (0x61 << 25), 282 + OPC_RISC_FCVT_WU_D = OPC_RISC_FP_ARITH | (0x61 << 25), 283 + OPC_RISC_FCVT_L_D = OPC_RISC_FP_ARITH | (0x61 << 25), 284 + OPC_RISC_FCVT_LU_D = OPC_RISC_FP_ARITH | (0x61 << 25), 285 + 286 + OPC_RISC_FCVT_D_W = OPC_RISC_FP_ARITH | (0x69 << 25), 287 + OPC_RISC_FCVT_D_WU = OPC_RISC_FP_ARITH | (0x69 << 25), 288 + OPC_RISC_FCVT_D_L = OPC_RISC_FP_ARITH | (0x69 << 25), 289 + OPC_RISC_FCVT_D_LU = OPC_RISC_FP_ARITH | (0x69 << 25), 290 + 291 + OPC_RISC_FMV_X_D = OPC_RISC_FP_ARITH | (0x71 << 25), 292 + OPC_RISC_FCLASS_D = OPC_RISC_FP_ARITH | (0x71 << 25), 293 + 294 + OPC_RISC_FMV_D_X = OPC_RISC_FP_ARITH | (0x79 << 25), 295 + }; 296 + 297 + #define GET_B_IMM(inst) ((extract32(inst, 8, 4) << 1) \ 298 + | (extract32(inst, 25, 6) << 5) \ 299 + | (extract32(inst, 7, 1) << 11) \ 300 + | (sextract64(inst, 31, 1) << 12)) 301 + 302 + #define GET_STORE_IMM(inst) ((extract32(inst, 7, 5)) \ 303 + | (sextract64(inst, 25, 7) << 5)) 304 + 305 + #define GET_JAL_IMM(inst) ((extract32(inst, 21, 10) << 1) \ 306 + | (extract32(inst, 20, 1) << 11) \ 307 + | (extract32(inst, 12, 8) << 12) \ 308 + | (sextract64(inst, 31, 1) << 20)) 309 + 310 + #define GET_RM(inst) extract32(inst, 12, 3) 311 + #define GET_RS3(inst) extract32(inst, 27, 5) 312 + #define GET_RS1(inst) extract32(inst, 15, 5) 313 + #define GET_RS2(inst) extract32(inst, 20, 5) 314 + #define GET_RD(inst) extract32(inst, 7, 5) 315 + #define GET_IMM(inst) sextract64(inst, 20, 12) 316 + 317 + /* RVC decoding macros */ 318 + #define GET_C_IMM(inst) (extract32(inst, 2, 5) \ 319 + | (sextract64(inst, 12, 1) << 5)) 320 + #define GET_C_ZIMM(inst) (extract32(inst, 2, 5) \ 321 + | (extract32(inst, 12, 1) << 5)) 322 + #define GET_C_ADDI4SPN_IMM(inst) ((extract32(inst, 6, 1) << 2) \ 323 + | (extract32(inst, 5, 1) << 3) \ 324 + | (extract32(inst, 11, 2) << 4) \ 325 + | (extract32(inst, 7, 4) << 6)) 326 + #define GET_C_ADDI16SP_IMM(inst) ((extract32(inst, 6, 1) << 4) \ 327 + | (extract32(inst, 2, 1) << 5) \ 328 + | (extract32(inst, 5, 1) << 6) \ 329 + | (extract32(inst, 3, 2) << 7) \ 330 + | (sextract64(inst, 12, 1) << 9)) 331 + #define GET_C_LWSP_IMM(inst) ((extract32(inst, 4, 3) << 2) \ 332 + | (extract32(inst, 12, 1) << 5) \ 333 + | (extract32(inst, 2, 2) << 6)) 334 + #define GET_C_LDSP_IMM(inst) ((extract32(inst, 5, 2) << 3) \ 335 + | (extract32(inst, 12, 1) << 5) \ 336 + | (extract32(inst, 2, 3) << 6)) 337 + #define GET_C_SWSP_IMM(inst) ((extract32(inst, 9, 4) << 2) \ 338 + | (extract32(inst, 7, 2) << 6)) 339 + #define GET_C_SDSP_IMM(inst) ((extract32(inst, 10, 3) << 3) \ 340 + | (extract32(inst, 7, 3) << 6)) 341 + #define GET_C_LW_IMM(inst) ((extract32(inst, 6, 1) << 2) \ 342 + | (extract32(inst, 10, 3) << 3) \ 343 + | (extract32(inst, 5, 1) << 6)) 344 + #define GET_C_LD_IMM(inst) ((extract32(inst, 10, 3) << 3) \ 345 + | (extract32(inst, 5, 2) << 6)) 346 + #define GET_C_J_IMM(inst) ((extract32(inst, 3, 3) << 1) \ 347 + | (extract32(inst, 11, 1) << 4) \ 348 + | (extract32(inst, 2, 1) << 5) \ 349 + | (extract32(inst, 7, 1) << 6) \ 350 + | (extract32(inst, 6, 1) << 7) \ 351 + | (extract32(inst, 9, 2) << 8) \ 352 + | (extract32(inst, 8, 1) << 10) \ 353 + | (sextract64(inst, 12, 1) << 11)) 354 + #define GET_C_B_IMM(inst) ((extract32(inst, 3, 2) << 1) \ 355 + | (extract32(inst, 10, 2) << 3) \ 356 + | (extract32(inst, 2, 1) << 5) \ 357 + | (extract32(inst, 5, 2) << 6) \ 358 + | (sextract64(inst, 12, 1) << 8)) 359 + #define GET_C_SIMM3(inst) extract32(inst, 10, 3) 360 + #define GET_C_RD(inst) GET_RD(inst) 361 + #define GET_C_RS1(inst) GET_RD(inst) 362 + #define GET_C_RS2(inst) extract32(inst, 2, 5) 363 + #define GET_C_RS1S(inst) (8 + extract32(inst, 7, 3)) 364 + #define GET_C_RS2S(inst) (8 + extract32(inst, 2, 3))
+669
target/riscv/op_helper.c
··· 1 + /* 2 + * RISC-V Emulation Helpers for QEMU. 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * Copyright (c) 2017-2018 SiFive, Inc. 6 + * 7 + * This program is free software; you can redistribute it and/or modify it 8 + * under the terms and conditions of the GNU General Public License, 9 + * version 2 or later, as published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope it will be useful, but WITHOUT 12 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 + * more details. 15 + * 16 + * You should have received a copy of the GNU General Public License along with 17 + * this program. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #include "qemu/osdep.h" 21 + #include "qemu/log.h" 22 + #include "cpu.h" 23 + #include "qemu/main-loop.h" 24 + #include "exec/exec-all.h" 25 + #include "exec/helper-proto.h" 26 + 27 + #ifndef CONFIG_USER_ONLY 28 + 29 + #if defined(TARGET_RISCV32) 30 + static const char valid_vm_1_09[16] = { 31 + [VM_1_09_MBARE] = 1, 32 + [VM_1_09_SV32] = 1, 33 + }; 34 + static const char valid_vm_1_10[16] = { 35 + [VM_1_10_MBARE] = 1, 36 + [VM_1_10_SV32] = 1 37 + }; 38 + #elif defined(TARGET_RISCV64) 39 + static const char valid_vm_1_09[16] = { 40 + [VM_1_09_MBARE] = 1, 41 + [VM_1_09_SV39] = 1, 42 + [VM_1_09_SV48] = 1, 43 + }; 44 + static const char valid_vm_1_10[16] = { 45 + [VM_1_10_MBARE] = 1, 46 + [VM_1_10_SV39] = 1, 47 + [VM_1_10_SV48] = 1, 48 + [VM_1_10_SV57] = 1 49 + }; 50 + #endif 51 + 52 + static int validate_vm(CPURISCVState *env, target_ulong vm) 53 + { 54 + return (env->priv_ver >= PRIV_VERSION_1_10_0) ? 55 + valid_vm_1_10[vm & 0xf] : valid_vm_1_09[vm & 0xf]; 56 + } 57 + 58 + #endif 59 + 60 + /* Exceptions processing helpers */ 61 + void QEMU_NORETURN do_raise_exception_err(CPURISCVState *env, 62 + uint32_t exception, uintptr_t pc) 63 + { 64 + CPUState *cs = CPU(riscv_env_get_cpu(env)); 65 + qemu_log_mask(CPU_LOG_INT, "%s: %d\n", __func__, exception); 66 + cs->exception_index = exception; 67 + cpu_loop_exit_restore(cs, pc); 68 + } 69 + 70 + void helper_raise_exception(CPURISCVState *env, uint32_t exception) 71 + { 72 + do_raise_exception_err(env, exception, 0); 73 + } 74 + 75 + static void validate_mstatus_fs(CPURISCVState *env, uintptr_t ra) 76 + { 77 + #ifndef CONFIG_USER_ONLY 78 + if (!(env->mstatus & MSTATUS_FS)) { 79 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, ra); 80 + } 81 + #endif 82 + } 83 + 84 + /* 85 + * Handle writes to CSRs and any resulting special behavior 86 + * 87 + * Adapted from Spike's processor_t::set_csr 88 + */ 89 + void csr_write_helper(CPURISCVState *env, target_ulong val_to_write, 90 + target_ulong csrno) 91 + { 92 + #ifndef CONFIG_USER_ONLY 93 + uint64_t delegable_ints = MIP_SSIP | MIP_STIP | MIP_SEIP | (1 << IRQ_X_COP); 94 + uint64_t all_ints = delegable_ints | MIP_MSIP | MIP_MTIP; 95 + #endif 96 + 97 + switch (csrno) { 98 + case CSR_FFLAGS: 99 + validate_mstatus_fs(env, GETPC()); 100 + cpu_riscv_set_fflags(env, val_to_write & (FSR_AEXC >> FSR_AEXC_SHIFT)); 101 + break; 102 + case CSR_FRM: 103 + validate_mstatus_fs(env, GETPC()); 104 + env->frm = val_to_write & (FSR_RD >> FSR_RD_SHIFT); 105 + break; 106 + case CSR_FCSR: 107 + validate_mstatus_fs(env, GETPC()); 108 + env->frm = (val_to_write & FSR_RD) >> FSR_RD_SHIFT; 109 + cpu_riscv_set_fflags(env, (val_to_write & FSR_AEXC) >> FSR_AEXC_SHIFT); 110 + break; 111 + #ifndef CONFIG_USER_ONLY 112 + case CSR_MSTATUS: { 113 + target_ulong mstatus = env->mstatus; 114 + target_ulong mask = 0; 115 + target_ulong mpp = get_field(val_to_write, MSTATUS_MPP); 116 + 117 + /* flush tlb on mstatus fields that affect VM */ 118 + if (env->priv_ver <= PRIV_VERSION_1_09_1) { 119 + if ((val_to_write ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | 120 + MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_VM)) { 121 + helper_tlb_flush(env); 122 + } 123 + mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | 124 + MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM | 125 + MSTATUS_MPP | MSTATUS_MXR | 126 + (validate_vm(env, get_field(val_to_write, MSTATUS_VM)) ? 127 + MSTATUS_VM : 0); 128 + } 129 + if (env->priv_ver >= PRIV_VERSION_1_10_0) { 130 + if ((val_to_write ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | 131 + MSTATUS_MPRV | MSTATUS_SUM)) { 132 + helper_tlb_flush(env); 133 + } 134 + mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | 135 + MSTATUS_SPP | MSTATUS_FS | MSTATUS_MPRV | MSTATUS_SUM | 136 + MSTATUS_MPP | MSTATUS_MXR; 137 + } 138 + 139 + /* silenty discard mstatus.mpp writes for unsupported modes */ 140 + if (mpp == PRV_H || 141 + (!riscv_has_ext(env, RVS) && mpp == PRV_S) || 142 + (!riscv_has_ext(env, RVU) && mpp == PRV_U)) { 143 + mask &= ~MSTATUS_MPP; 144 + } 145 + 146 + mstatus = (mstatus & ~mask) | (val_to_write & mask); 147 + int dirty = (mstatus & MSTATUS_FS) == MSTATUS_FS; 148 + dirty |= (mstatus & MSTATUS_XS) == MSTATUS_XS; 149 + mstatus = set_field(mstatus, MSTATUS_SD, dirty); 150 + env->mstatus = mstatus; 151 + break; 152 + } 153 + case CSR_MIP: { 154 + /* 155 + * Since the writeable bits in MIP are not set asynchrously by the 156 + * CLINT, no additional locking is needed for read-modifiy-write 157 + * CSR operations 158 + */ 159 + qemu_mutex_lock_iothread(); 160 + RISCVCPU *cpu = riscv_env_get_cpu(env); 161 + riscv_set_local_interrupt(cpu, MIP_SSIP, 162 + (val_to_write & MIP_SSIP) != 0); 163 + riscv_set_local_interrupt(cpu, MIP_STIP, 164 + (val_to_write & MIP_STIP) != 0); 165 + /* 166 + * csrs, csrc on mip.SEIP is not decomposable into separate read and 167 + * write steps, so a different implementation is needed 168 + */ 169 + qemu_mutex_unlock_iothread(); 170 + break; 171 + } 172 + case CSR_MIE: { 173 + env->mie = (env->mie & ~all_ints) | 174 + (val_to_write & all_ints); 175 + break; 176 + } 177 + case CSR_MIDELEG: 178 + env->mideleg = (env->mideleg & ~delegable_ints) 179 + | (val_to_write & delegable_ints); 180 + break; 181 + case CSR_MEDELEG: { 182 + target_ulong mask = 0; 183 + mask |= 1ULL << (RISCV_EXCP_INST_ADDR_MIS); 184 + mask |= 1ULL << (RISCV_EXCP_INST_ACCESS_FAULT); 185 + mask |= 1ULL << (RISCV_EXCP_ILLEGAL_INST); 186 + mask |= 1ULL << (RISCV_EXCP_BREAKPOINT); 187 + mask |= 1ULL << (RISCV_EXCP_LOAD_ADDR_MIS); 188 + mask |= 1ULL << (RISCV_EXCP_LOAD_ACCESS_FAULT); 189 + mask |= 1ULL << (RISCV_EXCP_STORE_AMO_ADDR_MIS); 190 + mask |= 1ULL << (RISCV_EXCP_STORE_AMO_ACCESS_FAULT); 191 + mask |= 1ULL << (RISCV_EXCP_U_ECALL); 192 + mask |= 1ULL << (RISCV_EXCP_S_ECALL); 193 + mask |= 1ULL << (RISCV_EXCP_H_ECALL); 194 + mask |= 1ULL << (RISCV_EXCP_M_ECALL); 195 + mask |= 1ULL << (RISCV_EXCP_INST_PAGE_FAULT); 196 + mask |= 1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT); 197 + mask |= 1ULL << (RISCV_EXCP_STORE_PAGE_FAULT); 198 + env->medeleg = (env->medeleg & ~mask) 199 + | (val_to_write & mask); 200 + break; 201 + } 202 + case CSR_MINSTRET: 203 + qemu_log_mask(LOG_UNIMP, "CSR_MINSTRET: write not implemented"); 204 + goto do_illegal; 205 + case CSR_MCYCLE: 206 + qemu_log_mask(LOG_UNIMP, "CSR_MCYCLE: write not implemented"); 207 + goto do_illegal; 208 + case CSR_MINSTRETH: 209 + qemu_log_mask(LOG_UNIMP, "CSR_MINSTRETH: write not implemented"); 210 + goto do_illegal; 211 + case CSR_MCYCLEH: 212 + qemu_log_mask(LOG_UNIMP, "CSR_MCYCLEH: write not implemented"); 213 + goto do_illegal; 214 + case CSR_MUCOUNTEREN: 215 + env->mucounteren = val_to_write; 216 + break; 217 + case CSR_MSCOUNTEREN: 218 + env->mscounteren = val_to_write; 219 + break; 220 + case CSR_SSTATUS: { 221 + target_ulong ms = env->mstatus; 222 + target_ulong mask = SSTATUS_SIE | SSTATUS_SPIE | SSTATUS_UIE 223 + | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS 224 + | SSTATUS_SUM | SSTATUS_MXR | SSTATUS_SD; 225 + ms = (ms & ~mask) | (val_to_write & mask); 226 + csr_write_helper(env, ms, CSR_MSTATUS); 227 + break; 228 + } 229 + case CSR_SIP: { 230 + qemu_mutex_lock_iothread(); 231 + target_ulong next_mip = (env->mip & ~env->mideleg) 232 + | (val_to_write & env->mideleg); 233 + qemu_mutex_unlock_iothread(); 234 + csr_write_helper(env, next_mip, CSR_MIP); 235 + break; 236 + } 237 + case CSR_SIE: { 238 + target_ulong next_mie = (env->mie & ~env->mideleg) 239 + | (val_to_write & env->mideleg); 240 + csr_write_helper(env, next_mie, CSR_MIE); 241 + break; 242 + } 243 + case CSR_SATP: /* CSR_SPTBR */ { 244 + if (!riscv_feature(env, RISCV_FEATURE_MMU)) { 245 + goto do_illegal; 246 + } 247 + if (env->priv_ver <= PRIV_VERSION_1_09_1 && (val_to_write ^ env->sptbr)) 248 + { 249 + helper_tlb_flush(env); 250 + env->sptbr = val_to_write & (((target_ulong) 251 + 1 << (TARGET_PHYS_ADDR_SPACE_BITS - PGSHIFT)) - 1); 252 + } 253 + if (env->priv_ver >= PRIV_VERSION_1_10_0 && 254 + validate_vm(env, get_field(val_to_write, SATP_MODE)) && 255 + ((val_to_write ^ env->satp) & (SATP_MODE | SATP_ASID | SATP_PPN))) 256 + { 257 + helper_tlb_flush(env); 258 + env->satp = val_to_write; 259 + } 260 + break; 261 + } 262 + case CSR_SEPC: 263 + env->sepc = val_to_write; 264 + break; 265 + case CSR_STVEC: 266 + if (val_to_write & 1) { 267 + qemu_log_mask(LOG_UNIMP, "CSR_STVEC: vectored traps not supported"); 268 + goto do_illegal; 269 + } 270 + env->stvec = val_to_write >> 2 << 2; 271 + break; 272 + case CSR_SCOUNTEREN: 273 + env->scounteren = val_to_write; 274 + break; 275 + case CSR_SSCRATCH: 276 + env->sscratch = val_to_write; 277 + break; 278 + case CSR_SCAUSE: 279 + env->scause = val_to_write; 280 + break; 281 + case CSR_SBADADDR: 282 + env->sbadaddr = val_to_write; 283 + break; 284 + case CSR_MEPC: 285 + env->mepc = val_to_write; 286 + break; 287 + case CSR_MTVEC: 288 + if (val_to_write & 1) { 289 + qemu_log_mask(LOG_UNIMP, "CSR_MTVEC: vectored traps not supported"); 290 + goto do_illegal; 291 + } 292 + env->mtvec = val_to_write >> 2 << 2; 293 + break; 294 + case CSR_MCOUNTEREN: 295 + env->mcounteren = val_to_write; 296 + break; 297 + case CSR_MSCRATCH: 298 + env->mscratch = val_to_write; 299 + break; 300 + case CSR_MCAUSE: 301 + env->mcause = val_to_write; 302 + break; 303 + case CSR_MBADADDR: 304 + env->mbadaddr = val_to_write; 305 + break; 306 + case CSR_MISA: { 307 + qemu_log_mask(LOG_UNIMP, "CSR_MISA: misa writes not supported"); 308 + goto do_illegal; 309 + } 310 + case CSR_PMPCFG0: 311 + case CSR_PMPCFG1: 312 + case CSR_PMPCFG2: 313 + case CSR_PMPCFG3: 314 + pmpcfg_csr_write(env, csrno - CSR_PMPCFG0, val_to_write); 315 + break; 316 + case CSR_PMPADDR0: 317 + case CSR_PMPADDR1: 318 + case CSR_PMPADDR2: 319 + case CSR_PMPADDR3: 320 + case CSR_PMPADDR4: 321 + case CSR_PMPADDR5: 322 + case CSR_PMPADDR6: 323 + case CSR_PMPADDR7: 324 + case CSR_PMPADDR8: 325 + case CSR_PMPADDR9: 326 + case CSR_PMPADDR10: 327 + case CSR_PMPADDR11: 328 + case CSR_PMPADDR12: 329 + case CSR_PMPADDR13: 330 + case CSR_PMPADDR14: 331 + case CSR_PMPADDR15: 332 + pmpaddr_csr_write(env, csrno - CSR_PMPADDR0, val_to_write); 333 + break; 334 + do_illegal: 335 + #endif 336 + default: 337 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 338 + } 339 + } 340 + 341 + /* 342 + * Handle reads to CSRs and any resulting special behavior 343 + * 344 + * Adapted from Spike's processor_t::get_csr 345 + */ 346 + target_ulong csr_read_helper(CPURISCVState *env, target_ulong csrno) 347 + { 348 + #ifndef CONFIG_USER_ONLY 349 + target_ulong ctr_en = env->priv == PRV_U ? env->mucounteren : 350 + env->priv == PRV_S ? env->mscounteren : -1U; 351 + #else 352 + target_ulong ctr_en = -1; 353 + #endif 354 + target_ulong ctr_ok = (ctr_en >> (csrno & 31)) & 1; 355 + 356 + if (csrno >= CSR_HPMCOUNTER3 && csrno <= CSR_HPMCOUNTER31) { 357 + if (ctr_ok) { 358 + return 0; 359 + } 360 + } 361 + #if defined(TARGET_RISCV32) 362 + if (csrno >= CSR_HPMCOUNTER3H && csrno <= CSR_HPMCOUNTER31H) { 363 + if (ctr_ok) { 364 + return 0; 365 + } 366 + } 367 + #endif 368 + if (csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31) { 369 + return 0; 370 + } 371 + #if defined(TARGET_RISCV32) 372 + if (csrno >= CSR_MHPMCOUNTER3 && csrno <= CSR_MHPMCOUNTER31) { 373 + return 0; 374 + } 375 + #endif 376 + if (csrno >= CSR_MHPMEVENT3 && csrno <= CSR_MHPMEVENT31) { 377 + return 0; 378 + } 379 + 380 + switch (csrno) { 381 + case CSR_FFLAGS: 382 + validate_mstatus_fs(env, GETPC()); 383 + return cpu_riscv_get_fflags(env); 384 + case CSR_FRM: 385 + validate_mstatus_fs(env, GETPC()); 386 + return env->frm; 387 + case CSR_FCSR: 388 + validate_mstatus_fs(env, GETPC()); 389 + return (cpu_riscv_get_fflags(env) << FSR_AEXC_SHIFT) 390 + | (env->frm << FSR_RD_SHIFT); 391 + /* rdtime/rdtimeh is trapped and emulated by bbl in system mode */ 392 + #ifdef CONFIG_USER_ONLY 393 + case CSR_TIME: 394 + return cpu_get_host_ticks(); 395 + #if defined(TARGET_RISCV32) 396 + case CSR_TIMEH: 397 + return cpu_get_host_ticks() >> 32; 398 + #endif 399 + #endif 400 + case CSR_INSTRET: 401 + case CSR_CYCLE: 402 + if (ctr_ok) { 403 + return cpu_get_host_ticks(); 404 + } 405 + break; 406 + #if defined(TARGET_RISCV32) 407 + case CSR_INSTRETH: 408 + case CSR_CYCLEH: 409 + if (ctr_ok) { 410 + return cpu_get_host_ticks() >> 32; 411 + } 412 + break; 413 + #endif 414 + #ifndef CONFIG_USER_ONLY 415 + case CSR_MINSTRET: 416 + case CSR_MCYCLE: 417 + return cpu_get_host_ticks(); 418 + case CSR_MINSTRETH: 419 + case CSR_MCYCLEH: 420 + #if defined(TARGET_RISCV32) 421 + return cpu_get_host_ticks() >> 32; 422 + #endif 423 + break; 424 + case CSR_MUCOUNTEREN: 425 + return env->mucounteren; 426 + case CSR_MSCOUNTEREN: 427 + return env->mscounteren; 428 + case CSR_SSTATUS: { 429 + target_ulong mask = SSTATUS_SIE | SSTATUS_SPIE | SSTATUS_UIE 430 + | SSTATUS_UPIE | SSTATUS_SPP | SSTATUS_FS | SSTATUS_XS 431 + | SSTATUS_SUM | SSTATUS_SD; 432 + if (env->priv_ver >= PRIV_VERSION_1_10_0) { 433 + mask |= SSTATUS_MXR; 434 + } 435 + return env->mstatus & mask; 436 + } 437 + case CSR_SIP: { 438 + qemu_mutex_lock_iothread(); 439 + target_ulong tmp = env->mip & env->mideleg; 440 + qemu_mutex_unlock_iothread(); 441 + return tmp; 442 + } 443 + case CSR_SIE: 444 + return env->mie & env->mideleg; 445 + case CSR_SEPC: 446 + return env->sepc; 447 + case CSR_SBADADDR: 448 + return env->sbadaddr; 449 + case CSR_STVEC: 450 + return env->stvec; 451 + case CSR_SCOUNTEREN: 452 + return env->scounteren; 453 + case CSR_SCAUSE: 454 + return env->scause; 455 + case CSR_SPTBR: 456 + if (env->priv_ver >= PRIV_VERSION_1_10_0) { 457 + return env->satp; 458 + } else { 459 + return env->sptbr; 460 + } 461 + case CSR_SSCRATCH: 462 + return env->sscratch; 463 + case CSR_MSTATUS: 464 + return env->mstatus; 465 + case CSR_MIP: { 466 + qemu_mutex_lock_iothread(); 467 + target_ulong tmp = env->mip; 468 + qemu_mutex_unlock_iothread(); 469 + return tmp; 470 + } 471 + case CSR_MIE: 472 + return env->mie; 473 + case CSR_MEPC: 474 + return env->mepc; 475 + case CSR_MSCRATCH: 476 + return env->mscratch; 477 + case CSR_MCAUSE: 478 + return env->mcause; 479 + case CSR_MBADADDR: 480 + return env->mbadaddr; 481 + case CSR_MISA: 482 + return env->misa; 483 + case CSR_MARCHID: 484 + return 0; /* as spike does */ 485 + case CSR_MIMPID: 486 + return 0; /* as spike does */ 487 + case CSR_MVENDORID: 488 + return 0; /* as spike does */ 489 + case CSR_MHARTID: 490 + return env->mhartid; 491 + case CSR_MTVEC: 492 + return env->mtvec; 493 + case CSR_MCOUNTEREN: 494 + return env->mcounteren; 495 + case CSR_MEDELEG: 496 + return env->medeleg; 497 + case CSR_MIDELEG: 498 + return env->mideleg; 499 + case CSR_PMPCFG0: 500 + case CSR_PMPCFG1: 501 + case CSR_PMPCFG2: 502 + case CSR_PMPCFG3: 503 + return pmpcfg_csr_read(env, csrno - CSR_PMPCFG0); 504 + case CSR_PMPADDR0: 505 + case CSR_PMPADDR1: 506 + case CSR_PMPADDR2: 507 + case CSR_PMPADDR3: 508 + case CSR_PMPADDR4: 509 + case CSR_PMPADDR5: 510 + case CSR_PMPADDR6: 511 + case CSR_PMPADDR7: 512 + case CSR_PMPADDR8: 513 + case CSR_PMPADDR9: 514 + case CSR_PMPADDR10: 515 + case CSR_PMPADDR11: 516 + case CSR_PMPADDR12: 517 + case CSR_PMPADDR13: 518 + case CSR_PMPADDR14: 519 + case CSR_PMPADDR15: 520 + return pmpaddr_csr_read(env, csrno - CSR_PMPADDR0); 521 + #endif 522 + } 523 + /* used by e.g. MTIME read */ 524 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 525 + } 526 + 527 + /* 528 + * Check that CSR access is allowed. 529 + * 530 + * Adapted from Spike's decode.h:validate_csr 531 + */ 532 + static void validate_csr(CPURISCVState *env, uint64_t which, 533 + uint64_t write, uintptr_t ra) 534 + { 535 + #ifndef CONFIG_USER_ONLY 536 + unsigned csr_priv = get_field((which), 0x300); 537 + unsigned csr_read_only = get_field((which), 0xC00) == 3; 538 + if (((write) && csr_read_only) || (env->priv < csr_priv)) { 539 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, ra); 540 + } 541 + #endif 542 + } 543 + 544 + target_ulong helper_csrrw(CPURISCVState *env, target_ulong src, 545 + target_ulong csr) 546 + { 547 + validate_csr(env, csr, 1, GETPC()); 548 + uint64_t csr_backup = csr_read_helper(env, csr); 549 + csr_write_helper(env, src, csr); 550 + return csr_backup; 551 + } 552 + 553 + target_ulong helper_csrrs(CPURISCVState *env, target_ulong src, 554 + target_ulong csr, target_ulong rs1_pass) 555 + { 556 + validate_csr(env, csr, rs1_pass != 0, GETPC()); 557 + uint64_t csr_backup = csr_read_helper(env, csr); 558 + if (rs1_pass != 0) { 559 + csr_write_helper(env, src | csr_backup, csr); 560 + } 561 + return csr_backup; 562 + } 563 + 564 + target_ulong helper_csrrc(CPURISCVState *env, target_ulong src, 565 + target_ulong csr, target_ulong rs1_pass) 566 + { 567 + validate_csr(env, csr, rs1_pass != 0, GETPC()); 568 + uint64_t csr_backup = csr_read_helper(env, csr); 569 + if (rs1_pass != 0) { 570 + csr_write_helper(env, (~src) & csr_backup, csr); 571 + } 572 + return csr_backup; 573 + } 574 + 575 + #ifndef CONFIG_USER_ONLY 576 + 577 + /* iothread_mutex must be held */ 578 + void riscv_set_local_interrupt(RISCVCPU *cpu, target_ulong mask, int value) 579 + { 580 + target_ulong old_mip = cpu->env.mip; 581 + cpu->env.mip = (old_mip & ~mask) | (value ? mask : 0); 582 + 583 + if (cpu->env.mip && !old_mip) { 584 + cpu_interrupt(CPU(cpu), CPU_INTERRUPT_HARD); 585 + } else if (!cpu->env.mip && old_mip) { 586 + cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_HARD); 587 + } 588 + } 589 + 590 + void riscv_set_mode(CPURISCVState *env, target_ulong newpriv) 591 + { 592 + if (newpriv > PRV_M) { 593 + g_assert_not_reached(); 594 + } 595 + if (newpriv == PRV_H) { 596 + newpriv = PRV_U; 597 + } 598 + /* tlb_flush is unnecessary as mode is contained in mmu_idx */ 599 + env->priv = newpriv; 600 + } 601 + 602 + target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb) 603 + { 604 + if (!(env->priv >= PRV_S)) { 605 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 606 + } 607 + 608 + target_ulong retpc = env->sepc; 609 + if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { 610 + do_raise_exception_err(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); 611 + } 612 + 613 + target_ulong mstatus = env->mstatus; 614 + target_ulong prev_priv = get_field(mstatus, MSTATUS_SPP); 615 + mstatus = set_field(mstatus, 616 + env->priv_ver >= PRIV_VERSION_1_10_0 ? 617 + MSTATUS_SIE : MSTATUS_UIE << prev_priv, 618 + get_field(mstatus, MSTATUS_SPIE)); 619 + mstatus = set_field(mstatus, MSTATUS_SPIE, 0); 620 + mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U); 621 + riscv_set_mode(env, prev_priv); 622 + csr_write_helper(env, mstatus, CSR_MSTATUS); 623 + 624 + return retpc; 625 + } 626 + 627 + target_ulong helper_mret(CPURISCVState *env, target_ulong cpu_pc_deb) 628 + { 629 + if (!(env->priv >= PRV_M)) { 630 + do_raise_exception_err(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); 631 + } 632 + 633 + target_ulong retpc = env->mepc; 634 + if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { 635 + do_raise_exception_err(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); 636 + } 637 + 638 + target_ulong mstatus = env->mstatus; 639 + target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); 640 + mstatus = set_field(mstatus, 641 + env->priv_ver >= PRIV_VERSION_1_10_0 ? 642 + MSTATUS_MIE : MSTATUS_UIE << prev_priv, 643 + get_field(mstatus, MSTATUS_MPIE)); 644 + mstatus = set_field(mstatus, MSTATUS_MPIE, 0); 645 + mstatus = set_field(mstatus, MSTATUS_MPP, PRV_U); 646 + riscv_set_mode(env, prev_priv); 647 + csr_write_helper(env, mstatus, CSR_MSTATUS); 648 + 649 + return retpc; 650 + } 651 + 652 + 653 + void helper_wfi(CPURISCVState *env) 654 + { 655 + CPUState *cs = CPU(riscv_env_get_cpu(env)); 656 + 657 + cs->halted = 1; 658 + cs->exception_index = EXCP_HLT; 659 + cpu_loop_exit(cs); 660 + } 661 + 662 + void helper_tlb_flush(CPURISCVState *env) 663 + { 664 + RISCVCPU *cpu = riscv_env_get_cpu(env); 665 + CPUState *cs = CPU(cpu); 666 + tlb_flush(cs); 667 + } 668 + 669 + #endif /* !CONFIG_USER_ONLY */
+380
target/riscv/pmp.c
··· 1 + /* 2 + * QEMU RISC-V PMP (Physical Memory Protection) 3 + * 4 + * Author: Daire McNamara, daire.mcnamara@emdalo.com 5 + * Ivan Griffin, ivan.griffin@emdalo.com 6 + * 7 + * This provides a RISC-V Physical Memory Protection implementation 8 + * 9 + * This program is free software; you can redistribute it and/or modify it 10 + * under the terms and conditions of the GNU General Public License, 11 + * version 2 or later, as published by the Free Software Foundation. 12 + * 13 + * This program is distributed in the hope it will be useful, but WITHOUT 14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 + * more details. 17 + * 18 + * You should have received a copy of the GNU General Public License along with 19 + * this program. If not, see <http://www.gnu.org/licenses/>. 20 + */ 21 + 22 + /* 23 + * PMP (Physical Memory Protection) is as-of-yet unused and needs testing. 24 + */ 25 + 26 + #include "qemu/osdep.h" 27 + #include "qemu/log.h" 28 + #include "qapi/error.h" 29 + #include "cpu.h" 30 + #include "qemu-common.h" 31 + 32 + #ifndef CONFIG_USER_ONLY 33 + 34 + #define RISCV_DEBUG_PMP 0 35 + #define PMP_DEBUG(fmt, ...) \ 36 + do { \ 37 + if (RISCV_DEBUG_PMP) { \ 38 + qemu_log_mask(LOG_TRACE, "%s: " fmt "\n", __func__, ##__VA_ARGS__);\ 39 + } \ 40 + } while (0) 41 + 42 + static void pmp_write_cfg(CPURISCVState *env, uint32_t addr_index, 43 + uint8_t val); 44 + static uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t addr_index); 45 + static void pmp_update_rule(CPURISCVState *env, uint32_t pmp_index); 46 + 47 + /* 48 + * Accessor method to extract address matching type 'a field' from cfg reg 49 + */ 50 + static inline uint8_t pmp_get_a_field(uint8_t cfg) 51 + { 52 + uint8_t a = cfg >> 3; 53 + return a & 0x3; 54 + } 55 + 56 + /* 57 + * Check whether a PMP is locked or not. 58 + */ 59 + static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index) 60 + { 61 + 62 + if (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_LOCK) { 63 + return 1; 64 + } 65 + 66 + /* Top PMP has no 'next' to check */ 67 + if ((pmp_index + 1u) >= MAX_RISCV_PMPS) { 68 + return 0; 69 + } 70 + 71 + /* In TOR mode, need to check the lock bit of the next pmp 72 + * (if there is a next) 73 + */ 74 + const uint8_t a_field = 75 + pmp_get_a_field(env->pmp_state.pmp[pmp_index + 1].cfg_reg); 76 + if ((env->pmp_state.pmp[pmp_index + 1u].cfg_reg & PMP_LOCK) && 77 + (PMP_AMATCH_TOR == a_field)) { 78 + return 1; 79 + } 80 + 81 + return 0; 82 + } 83 + 84 + /* 85 + * Count the number of active rules. 86 + */ 87 + static inline uint32_t pmp_get_num_rules(CPURISCVState *env) 88 + { 89 + return env->pmp_state.num_rules; 90 + } 91 + 92 + /* 93 + * Accessor to get the cfg reg for a specific PMP/HART 94 + */ 95 + static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index) 96 + { 97 + if (pmp_index < MAX_RISCV_PMPS) { 98 + return env->pmp_state.pmp[pmp_index].cfg_reg; 99 + } 100 + 101 + return 0; 102 + } 103 + 104 + 105 + /* 106 + * Accessor to set the cfg reg for a specific PMP/HART 107 + * Bounds checks and relevant lock bit. 108 + */ 109 + static void pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val) 110 + { 111 + if (pmp_index < MAX_RISCV_PMPS) { 112 + if (!pmp_is_locked(env, pmp_index)) { 113 + env->pmp_state.pmp[pmp_index].cfg_reg = val; 114 + pmp_update_rule(env, pmp_index); 115 + } else { 116 + PMP_DEBUG("ignoring write - locked"); 117 + } 118 + } else { 119 + PMP_DEBUG("ignoring write - out of bounds"); 120 + } 121 + } 122 + 123 + static void pmp_decode_napot(target_ulong a, target_ulong *sa, target_ulong *ea) 124 + { 125 + /* 126 + aaaa...aaa0 8-byte NAPOT range 127 + aaaa...aa01 16-byte NAPOT range 128 + aaaa...a011 32-byte NAPOT range 129 + ... 130 + aa01...1111 2^XLEN-byte NAPOT range 131 + a011...1111 2^(XLEN+1)-byte NAPOT range 132 + 0111...1111 2^(XLEN+2)-byte NAPOT range 133 + 1111...1111 Reserved 134 + */ 135 + if (a == -1) { 136 + *sa = 0u; 137 + *ea = -1; 138 + return; 139 + } else { 140 + target_ulong t1 = ctz64(~a); 141 + target_ulong base = (a & ~(((target_ulong)1 << t1) - 1)) << 3; 142 + target_ulong range = ((target_ulong)1 << (t1 + 3)) - 1; 143 + *sa = base; 144 + *ea = base + range; 145 + } 146 + } 147 + 148 + 149 + /* Convert cfg/addr reg values here into simple 'sa' --> start address and 'ea' 150 + * end address values. 151 + * This function is called relatively infrequently whereas the check that 152 + * an address is within a pmp rule is called often, so optimise that one 153 + */ 154 + static void pmp_update_rule(CPURISCVState *env, uint32_t pmp_index) 155 + { 156 + int i; 157 + 158 + env->pmp_state.num_rules = 0; 159 + 160 + uint8_t this_cfg = env->pmp_state.pmp[pmp_index].cfg_reg; 161 + target_ulong this_addr = env->pmp_state.pmp[pmp_index].addr_reg; 162 + target_ulong prev_addr = 0u; 163 + target_ulong sa = 0u; 164 + target_ulong ea = 0u; 165 + 166 + if (pmp_index >= 1u) { 167 + prev_addr = env->pmp_state.pmp[pmp_index - 1].addr_reg; 168 + } 169 + 170 + switch (pmp_get_a_field(this_cfg)) { 171 + case PMP_AMATCH_OFF: 172 + sa = 0u; 173 + ea = -1; 174 + break; 175 + 176 + case PMP_AMATCH_TOR: 177 + sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */ 178 + ea = (this_addr << 2) - 1u; 179 + break; 180 + 181 + case PMP_AMATCH_NA4: 182 + sa = this_addr << 2; /* shift up from [xx:0] to [xx+2:2] */ 183 + ea = (this_addr + 4u) - 1u; 184 + break; 185 + 186 + case PMP_AMATCH_NAPOT: 187 + pmp_decode_napot(this_addr, &sa, &ea); 188 + break; 189 + 190 + default: 191 + sa = 0u; 192 + ea = 0u; 193 + break; 194 + } 195 + 196 + env->pmp_state.addr[pmp_index].sa = sa; 197 + env->pmp_state.addr[pmp_index].ea = ea; 198 + 199 + for (i = 0; i < MAX_RISCV_PMPS; i++) { 200 + const uint8_t a_field = 201 + pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg); 202 + if (PMP_AMATCH_OFF != a_field) { 203 + env->pmp_state.num_rules++; 204 + } 205 + } 206 + } 207 + 208 + static int pmp_is_in_range(CPURISCVState *env, int pmp_index, target_ulong addr) 209 + { 210 + int result = 0; 211 + 212 + if ((addr >= env->pmp_state.addr[pmp_index].sa) 213 + && (addr <= env->pmp_state.addr[pmp_index].ea)) { 214 + result = 1; 215 + } else { 216 + result = 0; 217 + } 218 + 219 + return result; 220 + } 221 + 222 + 223 + /* 224 + * Public Interface 225 + */ 226 + 227 + /* 228 + * Check if the address has required RWX privs to complete desired operation 229 + */ 230 + bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, 231 + target_ulong size, pmp_priv_t privs) 232 + { 233 + int i = 0; 234 + int ret = -1; 235 + target_ulong s = 0; 236 + target_ulong e = 0; 237 + pmp_priv_t allowed_privs = 0; 238 + 239 + /* Short cut if no rules */ 240 + if (0 == pmp_get_num_rules(env)) { 241 + return true; 242 + } 243 + 244 + /* 1.10 draft priv spec states there is an implicit order 245 + from low to high */ 246 + for (i = 0; i < MAX_RISCV_PMPS; i++) { 247 + s = pmp_is_in_range(env, i, addr); 248 + e = pmp_is_in_range(env, i, addr + size); 249 + 250 + /* partially inside */ 251 + if ((s + e) == 1) { 252 + PMP_DEBUG("pmp violation - access is partially inside"); 253 + ret = 0; 254 + break; 255 + } 256 + 257 + /* fully inside */ 258 + const uint8_t a_field = 259 + pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg); 260 + if ((s + e) == 2) { 261 + if (PMP_AMATCH_OFF == a_field) { 262 + return 1; 263 + } 264 + 265 + allowed_privs = PMP_READ | PMP_WRITE | PMP_EXEC; 266 + if ((env->priv != PRV_M) || pmp_is_locked(env, i)) { 267 + allowed_privs &= env->pmp_state.pmp[i].cfg_reg; 268 + } 269 + 270 + if ((privs & allowed_privs) == privs) { 271 + ret = 1; 272 + break; 273 + } else { 274 + ret = 0; 275 + break; 276 + } 277 + } 278 + } 279 + 280 + /* No rule matched */ 281 + if (ret == -1) { 282 + if (env->priv == PRV_M) { 283 + ret = 1; /* Privileged spec v1.10 states if no PMP entry matches an 284 + * M-Mode access, the access succeeds */ 285 + } else { 286 + ret = 0; /* Other modes are not allowed to succeed if they don't 287 + * match a rule, but there are rules. We've checked for 288 + * no rule earlier in this function. */ 289 + } 290 + } 291 + 292 + return ret == 1 ? true : false; 293 + } 294 + 295 + 296 + /* 297 + * Handle a write to a pmpcfg CSP 298 + */ 299 + void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, 300 + target_ulong val) 301 + { 302 + int i; 303 + uint8_t cfg_val; 304 + 305 + PMP_DEBUG("hart " TARGET_FMT_ld ": reg%d, val: 0x" TARGET_FMT_lx, 306 + env->mhartid, reg_index, val); 307 + 308 + if ((reg_index & 1) && (sizeof(target_ulong) == 8)) { 309 + PMP_DEBUG("ignoring write - incorrect address"); 310 + return; 311 + } 312 + 313 + for (i = 0; i < sizeof(target_ulong); i++) { 314 + cfg_val = (val >> 8 * i) & 0xff; 315 + pmp_write_cfg(env, (reg_index * sizeof(target_ulong)) + i, 316 + cfg_val); 317 + } 318 + } 319 + 320 + 321 + /* 322 + * Handle a read from a pmpcfg CSP 323 + */ 324 + target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index) 325 + { 326 + int i; 327 + target_ulong cfg_val = 0; 328 + uint8_t val = 0; 329 + 330 + for (i = 0; i < sizeof(target_ulong); i++) { 331 + val = pmp_read_cfg(env, (reg_index * sizeof(target_ulong)) + i); 332 + cfg_val |= (val << (i * 8)); 333 + } 334 + 335 + PMP_DEBUG("hart " TARGET_FMT_ld ": reg%d, val: 0x" TARGET_FMT_lx, 336 + env->mhartid, reg_index, cfg_val); 337 + 338 + return cfg_val; 339 + } 340 + 341 + 342 + /* 343 + * Handle a write to a pmpaddr CSP 344 + */ 345 + void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, 346 + target_ulong val) 347 + { 348 + PMP_DEBUG("hart " TARGET_FMT_ld ": addr%d, val: 0x" TARGET_FMT_lx, 349 + env->mhartid, addr_index, val); 350 + 351 + if (addr_index < MAX_RISCV_PMPS) { 352 + if (!pmp_is_locked(env, addr_index)) { 353 + env->pmp_state.pmp[addr_index].addr_reg = val; 354 + pmp_update_rule(env, addr_index); 355 + } else { 356 + PMP_DEBUG("ignoring write - locked"); 357 + } 358 + } else { 359 + PMP_DEBUG("ignoring write - out of bounds"); 360 + } 361 + } 362 + 363 + 364 + /* 365 + * Handle a read from a pmpaddr CSP 366 + */ 367 + target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index) 368 + { 369 + PMP_DEBUG("hart " TARGET_FMT_ld ": addr%d, val: 0x" TARGET_FMT_lx, 370 + env->mhartid, addr_index, 371 + env->pmp_state.pmp[addr_index].addr_reg); 372 + if (addr_index < MAX_RISCV_PMPS) { 373 + return env->pmp_state.pmp[addr_index].addr_reg; 374 + } else { 375 + PMP_DEBUG("ignoring read - out of bounds"); 376 + return 0; 377 + } 378 + } 379 + 380 + #endif
+64
target/riscv/pmp.h
··· 1 + /* 2 + * QEMU RISC-V PMP (Physical Memory Protection) 3 + * 4 + * Author: Daire McNamara, daire.mcnamara@emdalo.com 5 + * Ivan Griffin, ivan.griffin@emdalo.com 6 + * 7 + * This provides a RISC-V Physical Memory Protection interface 8 + * 9 + * This program is free software; you can redistribute it and/or modify it 10 + * under the terms and conditions of the GNU General Public License, 11 + * version 2 or later, as published by the Free Software Foundation. 12 + * 13 + * This program is distributed in the hope it will be useful, but WITHOUT 14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 + * more details. 17 + * 18 + * You should have received a copy of the GNU General Public License along with 19 + * this program. If not, see <http://www.gnu.org/licenses/>. 20 + */ 21 + 22 + #ifndef _RISCV_PMP_H_ 23 + #define _RISCV_PMP_H_ 24 + 25 + typedef enum { 26 + PMP_READ = 1 << 0, 27 + PMP_WRITE = 1 << 1, 28 + PMP_EXEC = 1 << 2, 29 + PMP_LOCK = 1 << 7 30 + } pmp_priv_t; 31 + 32 + typedef enum { 33 + PMP_AMATCH_OFF, /* Null (off) */ 34 + PMP_AMATCH_TOR, /* Top of Range */ 35 + PMP_AMATCH_NA4, /* Naturally aligned four-byte region */ 36 + PMP_AMATCH_NAPOT /* Naturally aligned power-of-two region */ 37 + } pmp_am_t; 38 + 39 + typedef struct { 40 + target_ulong addr_reg; 41 + uint8_t cfg_reg; 42 + } pmp_entry_t; 43 + 44 + typedef struct { 45 + target_ulong sa; 46 + target_ulong ea; 47 + } pmp_addr_t; 48 + 49 + typedef struct { 50 + pmp_entry_t pmp[MAX_RISCV_PMPS]; 51 + pmp_addr_t addr[MAX_RISCV_PMPS]; 52 + uint32_t num_rules; 53 + } pmp_table_t; 54 + 55 + void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, 56 + target_ulong val); 57 + target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index); 58 + void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, 59 + target_ulong val); 60 + target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index); 61 + bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, 62 + target_ulong size, pmp_priv_t priv); 63 + 64 + #endif
+1978
target/riscv/translate.c
··· 1 + /* 2 + * RISC-V emulation for qemu: main translation routines. 3 + * 4 + * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2 or later, as published by the Free Software Foundation. 9 + * 10 + * This program is distributed in the hope it will be useful, but WITHOUT 11 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 + * more details. 14 + * 15 + * You should have received a copy of the GNU General Public License along with 16 + * this program. If not, see <http://www.gnu.org/licenses/>. 17 + */ 18 + 19 + #include "qemu/osdep.h" 20 + #include "qemu/log.h" 21 + #include "cpu.h" 22 + #include "tcg-op.h" 23 + #include "disas/disas.h" 24 + #include "exec/cpu_ldst.h" 25 + #include "exec/exec-all.h" 26 + #include "exec/helper-proto.h" 27 + #include "exec/helper-gen.h" 28 + 29 + #include "exec/log.h" 30 + 31 + #include "instmap.h" 32 + 33 + /* global register indices */ 34 + static TCGv cpu_gpr[32], cpu_pc; 35 + static TCGv_i64 cpu_fpr[32]; /* assume F and D extensions */ 36 + static TCGv load_res; 37 + static TCGv load_val; 38 + 39 + #include "exec/gen-icount.h" 40 + 41 + typedef struct DisasContext { 42 + struct TranslationBlock *tb; 43 + target_ulong pc; 44 + target_ulong next_pc; 45 + uint32_t opcode; 46 + uint32_t flags; 47 + uint32_t mem_idx; 48 + int singlestep_enabled; 49 + int bstate; 50 + /* Remember the rounding mode encoded in the previous fp instruction, 51 + which we have already installed into env->fp_status. Or -1 for 52 + no previous fp instruction. Note that we exit the TB when writing 53 + to any system register, which includes CSR_FRM, so we do not have 54 + to reset this known value. */ 55 + int frm; 56 + } DisasContext; 57 + 58 + enum { 59 + BS_NONE = 0, /* When seen outside of translation while loop, indicates 60 + need to exit tb due to end of page. */ 61 + BS_STOP = 1, /* Need to exit tb for syscall, sret, etc. */ 62 + BS_BRANCH = 2, /* Need to exit tb for branch, jal, etc. */ 63 + }; 64 + 65 + /* convert riscv funct3 to qemu memop for load/store */ 66 + static const int tcg_memop_lookup[8] = { 67 + [0 ... 7] = -1, 68 + [0] = MO_SB, 69 + [1] = MO_TESW, 70 + [2] = MO_TESL, 71 + [4] = MO_UB, 72 + [5] = MO_TEUW, 73 + #ifdef TARGET_RISCV64 74 + [3] = MO_TEQ, 75 + [6] = MO_TEUL, 76 + #endif 77 + }; 78 + 79 + #ifdef TARGET_RISCV64 80 + #define CASE_OP_32_64(X) case X: case glue(X, W) 81 + #else 82 + #define CASE_OP_32_64(X) case X 83 + #endif 84 + 85 + static void generate_exception(DisasContext *ctx, int excp) 86 + { 87 + tcg_gen_movi_tl(cpu_pc, ctx->pc); 88 + TCGv_i32 helper_tmp = tcg_const_i32(excp); 89 + gen_helper_raise_exception(cpu_env, helper_tmp); 90 + tcg_temp_free_i32(helper_tmp); 91 + ctx->bstate = BS_BRANCH; 92 + } 93 + 94 + static void generate_exception_mbadaddr(DisasContext *ctx, int excp) 95 + { 96 + tcg_gen_movi_tl(cpu_pc, ctx->pc); 97 + tcg_gen_st_tl(cpu_pc, cpu_env, offsetof(CPURISCVState, badaddr)); 98 + TCGv_i32 helper_tmp = tcg_const_i32(excp); 99 + gen_helper_raise_exception(cpu_env, helper_tmp); 100 + tcg_temp_free_i32(helper_tmp); 101 + ctx->bstate = BS_BRANCH; 102 + } 103 + 104 + static void gen_exception_debug(void) 105 + { 106 + TCGv_i32 helper_tmp = tcg_const_i32(EXCP_DEBUG); 107 + gen_helper_raise_exception(cpu_env, helper_tmp); 108 + tcg_temp_free_i32(helper_tmp); 109 + } 110 + 111 + static void gen_exception_illegal(DisasContext *ctx) 112 + { 113 + generate_exception(ctx, RISCV_EXCP_ILLEGAL_INST); 114 + } 115 + 116 + static void gen_exception_inst_addr_mis(DisasContext *ctx) 117 + { 118 + generate_exception_mbadaddr(ctx, RISCV_EXCP_INST_ADDR_MIS); 119 + } 120 + 121 + static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest) 122 + { 123 + if (unlikely(ctx->singlestep_enabled)) { 124 + return false; 125 + } 126 + 127 + #ifndef CONFIG_USER_ONLY 128 + return (ctx->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK); 129 + #else 130 + return true; 131 + #endif 132 + } 133 + 134 + static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest) 135 + { 136 + if (use_goto_tb(ctx, dest)) { 137 + /* chaining is only allowed when the jump is to the same page */ 138 + tcg_gen_goto_tb(n); 139 + tcg_gen_movi_tl(cpu_pc, dest); 140 + tcg_gen_exit_tb((uintptr_t)ctx->tb + n); 141 + } else { 142 + tcg_gen_movi_tl(cpu_pc, dest); 143 + if (ctx->singlestep_enabled) { 144 + gen_exception_debug(); 145 + } else { 146 + tcg_gen_exit_tb(0); 147 + } 148 + } 149 + } 150 + 151 + /* Wrapper for getting reg values - need to check of reg is zero since 152 + * cpu_gpr[0] is not actually allocated 153 + */ 154 + static inline void gen_get_gpr(TCGv t, int reg_num) 155 + { 156 + if (reg_num == 0) { 157 + tcg_gen_movi_tl(t, 0); 158 + } else { 159 + tcg_gen_mov_tl(t, cpu_gpr[reg_num]); 160 + } 161 + } 162 + 163 + /* Wrapper for setting reg values - need to check of reg is zero since 164 + * cpu_gpr[0] is not actually allocated. this is more for safety purposes, 165 + * since we usually avoid calling the OP_TYPE_gen function if we see a write to 166 + * $zero 167 + */ 168 + static inline void gen_set_gpr(int reg_num_dst, TCGv t) 169 + { 170 + if (reg_num_dst != 0) { 171 + tcg_gen_mov_tl(cpu_gpr[reg_num_dst], t); 172 + } 173 + } 174 + 175 + static void gen_mulhsu(TCGv ret, TCGv arg1, TCGv arg2) 176 + { 177 + TCGv rl = tcg_temp_new(); 178 + TCGv rh = tcg_temp_new(); 179 + 180 + tcg_gen_mulu2_tl(rl, rh, arg1, arg2); 181 + /* fix up for one negative */ 182 + tcg_gen_sari_tl(rl, arg1, TARGET_LONG_BITS - 1); 183 + tcg_gen_and_tl(rl, rl, arg2); 184 + tcg_gen_sub_tl(ret, rh, rl); 185 + 186 + tcg_temp_free(rl); 187 + tcg_temp_free(rh); 188 + } 189 + 190 + static void gen_fsgnj(DisasContext *ctx, uint32_t rd, uint32_t rs1, 191 + uint32_t rs2, int rm, uint64_t min) 192 + { 193 + switch (rm) { 194 + case 0: /* fsgnj */ 195 + if (rs1 == rs2) { /* FMOV */ 196 + tcg_gen_mov_i64(cpu_fpr[rd], cpu_fpr[rs1]); 197 + } else { 198 + tcg_gen_deposit_i64(cpu_fpr[rd], cpu_fpr[rs2], cpu_fpr[rs1], 199 + 0, min == INT32_MIN ? 31 : 63); 200 + } 201 + break; 202 + case 1: /* fsgnjn */ 203 + if (rs1 == rs2) { /* FNEG */ 204 + tcg_gen_xori_i64(cpu_fpr[rd], cpu_fpr[rs1], min); 205 + } else { 206 + TCGv_i64 t0 = tcg_temp_new_i64(); 207 + tcg_gen_not_i64(t0, cpu_fpr[rs2]); 208 + tcg_gen_deposit_i64(cpu_fpr[rd], t0, cpu_fpr[rs1], 209 + 0, min == INT32_MIN ? 31 : 63); 210 + tcg_temp_free_i64(t0); 211 + } 212 + break; 213 + case 2: /* fsgnjx */ 214 + if (rs1 == rs2) { /* FABS */ 215 + tcg_gen_andi_i64(cpu_fpr[rd], cpu_fpr[rs1], ~min); 216 + } else { 217 + TCGv_i64 t0 = tcg_temp_new_i64(); 218 + tcg_gen_andi_i64(t0, cpu_fpr[rs2], min); 219 + tcg_gen_xor_i64(cpu_fpr[rd], cpu_fpr[rs1], t0); 220 + tcg_temp_free_i64(t0); 221 + } 222 + break; 223 + default: 224 + gen_exception_illegal(ctx); 225 + } 226 + } 227 + 228 + static void gen_arith(DisasContext *ctx, uint32_t opc, int rd, int rs1, 229 + int rs2) 230 + { 231 + TCGv source1, source2, cond1, cond2, zeroreg, resultopt1; 232 + source1 = tcg_temp_new(); 233 + source2 = tcg_temp_new(); 234 + gen_get_gpr(source1, rs1); 235 + gen_get_gpr(source2, rs2); 236 + 237 + switch (opc) { 238 + CASE_OP_32_64(OPC_RISC_ADD): 239 + tcg_gen_add_tl(source1, source1, source2); 240 + break; 241 + CASE_OP_32_64(OPC_RISC_SUB): 242 + tcg_gen_sub_tl(source1, source1, source2); 243 + break; 244 + #if defined(TARGET_RISCV64) 245 + case OPC_RISC_SLLW: 246 + tcg_gen_andi_tl(source2, source2, 0x1F); 247 + tcg_gen_shl_tl(source1, source1, source2); 248 + break; 249 + #endif 250 + case OPC_RISC_SLL: 251 + tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1); 252 + tcg_gen_shl_tl(source1, source1, source2); 253 + break; 254 + case OPC_RISC_SLT: 255 + tcg_gen_setcond_tl(TCG_COND_LT, source1, source1, source2); 256 + break; 257 + case OPC_RISC_SLTU: 258 + tcg_gen_setcond_tl(TCG_COND_LTU, source1, source1, source2); 259 + break; 260 + case OPC_RISC_XOR: 261 + tcg_gen_xor_tl(source1, source1, source2); 262 + break; 263 + #if defined(TARGET_RISCV64) 264 + case OPC_RISC_SRLW: 265 + /* clear upper 32 */ 266 + tcg_gen_ext32u_tl(source1, source1); 267 + tcg_gen_andi_tl(source2, source2, 0x1F); 268 + tcg_gen_shr_tl(source1, source1, source2); 269 + break; 270 + #endif 271 + case OPC_RISC_SRL: 272 + tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1); 273 + tcg_gen_shr_tl(source1, source1, source2); 274 + break; 275 + #if defined(TARGET_RISCV64) 276 + case OPC_RISC_SRAW: 277 + /* first, trick to get it to act like working on 32 bits (get rid of 278 + upper 32, sign extend to fill space) */ 279 + tcg_gen_ext32s_tl(source1, source1); 280 + tcg_gen_andi_tl(source2, source2, 0x1F); 281 + tcg_gen_sar_tl(source1, source1, source2); 282 + break; 283 + /* fall through to SRA */ 284 + #endif 285 + case OPC_RISC_SRA: 286 + tcg_gen_andi_tl(source2, source2, TARGET_LONG_BITS - 1); 287 + tcg_gen_sar_tl(source1, source1, source2); 288 + break; 289 + case OPC_RISC_OR: 290 + tcg_gen_or_tl(source1, source1, source2); 291 + break; 292 + case OPC_RISC_AND: 293 + tcg_gen_and_tl(source1, source1, source2); 294 + break; 295 + CASE_OP_32_64(OPC_RISC_MUL): 296 + tcg_gen_mul_tl(source1, source1, source2); 297 + break; 298 + case OPC_RISC_MULH: 299 + tcg_gen_muls2_tl(source2, source1, source1, source2); 300 + break; 301 + case OPC_RISC_MULHSU: 302 + gen_mulhsu(source1, source1, source2); 303 + break; 304 + case OPC_RISC_MULHU: 305 + tcg_gen_mulu2_tl(source2, source1, source1, source2); 306 + break; 307 + #if defined(TARGET_RISCV64) 308 + case OPC_RISC_DIVW: 309 + tcg_gen_ext32s_tl(source1, source1); 310 + tcg_gen_ext32s_tl(source2, source2); 311 + /* fall through to DIV */ 312 + #endif 313 + case OPC_RISC_DIV: 314 + /* Handle by altering args to tcg_gen_div to produce req'd results: 315 + * For overflow: want source1 in source1 and 1 in source2 316 + * For div by zero: want -1 in source1 and 1 in source2 -> -1 result */ 317 + cond1 = tcg_temp_new(); 318 + cond2 = tcg_temp_new(); 319 + zeroreg = tcg_const_tl(0); 320 + resultopt1 = tcg_temp_new(); 321 + 322 + tcg_gen_movi_tl(resultopt1, (target_ulong)-1); 323 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, (target_ulong)(~0L)); 324 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source1, 325 + ((target_ulong)1) << (TARGET_LONG_BITS - 1)); 326 + tcg_gen_and_tl(cond1, cond1, cond2); /* cond1 = overflow */ 327 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, 0); /* cond2 = div 0 */ 328 + /* if div by zero, set source1 to -1, otherwise don't change */ 329 + tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond2, zeroreg, source1, 330 + resultopt1); 331 + /* if overflow or div by zero, set source2 to 1, else don't change */ 332 + tcg_gen_or_tl(cond1, cond1, cond2); 333 + tcg_gen_movi_tl(resultopt1, (target_ulong)1); 334 + tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2, 335 + resultopt1); 336 + tcg_gen_div_tl(source1, source1, source2); 337 + 338 + tcg_temp_free(cond1); 339 + tcg_temp_free(cond2); 340 + tcg_temp_free(zeroreg); 341 + tcg_temp_free(resultopt1); 342 + break; 343 + #if defined(TARGET_RISCV64) 344 + case OPC_RISC_DIVUW: 345 + tcg_gen_ext32u_tl(source1, source1); 346 + tcg_gen_ext32u_tl(source2, source2); 347 + /* fall through to DIVU */ 348 + #endif 349 + case OPC_RISC_DIVU: 350 + cond1 = tcg_temp_new(); 351 + zeroreg = tcg_const_tl(0); 352 + resultopt1 = tcg_temp_new(); 353 + 354 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0); 355 + tcg_gen_movi_tl(resultopt1, (target_ulong)-1); 356 + tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, source1, 357 + resultopt1); 358 + tcg_gen_movi_tl(resultopt1, (target_ulong)1); 359 + tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2, 360 + resultopt1); 361 + tcg_gen_divu_tl(source1, source1, source2); 362 + 363 + tcg_temp_free(cond1); 364 + tcg_temp_free(zeroreg); 365 + tcg_temp_free(resultopt1); 366 + break; 367 + #if defined(TARGET_RISCV64) 368 + case OPC_RISC_REMW: 369 + tcg_gen_ext32s_tl(source1, source1); 370 + tcg_gen_ext32s_tl(source2, source2); 371 + /* fall through to REM */ 372 + #endif 373 + case OPC_RISC_REM: 374 + cond1 = tcg_temp_new(); 375 + cond2 = tcg_temp_new(); 376 + zeroreg = tcg_const_tl(0); 377 + resultopt1 = tcg_temp_new(); 378 + 379 + tcg_gen_movi_tl(resultopt1, 1L); 380 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond2, source2, (target_ulong)-1); 381 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source1, 382 + (target_ulong)1 << (TARGET_LONG_BITS - 1)); 383 + tcg_gen_and_tl(cond2, cond1, cond2); /* cond1 = overflow */ 384 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0); /* cond2 = div 0 */ 385 + /* if overflow or div by zero, set source2 to 1, else don't change */ 386 + tcg_gen_or_tl(cond2, cond1, cond2); 387 + tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond2, zeroreg, source2, 388 + resultopt1); 389 + tcg_gen_rem_tl(resultopt1, source1, source2); 390 + /* if div by zero, just return the original dividend */ 391 + tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, resultopt1, 392 + source1); 393 + 394 + tcg_temp_free(cond1); 395 + tcg_temp_free(cond2); 396 + tcg_temp_free(zeroreg); 397 + tcg_temp_free(resultopt1); 398 + break; 399 + #if defined(TARGET_RISCV64) 400 + case OPC_RISC_REMUW: 401 + tcg_gen_ext32u_tl(source1, source1); 402 + tcg_gen_ext32u_tl(source2, source2); 403 + /* fall through to REMU */ 404 + #endif 405 + case OPC_RISC_REMU: 406 + cond1 = tcg_temp_new(); 407 + zeroreg = tcg_const_tl(0); 408 + resultopt1 = tcg_temp_new(); 409 + 410 + tcg_gen_movi_tl(resultopt1, (target_ulong)1); 411 + tcg_gen_setcondi_tl(TCG_COND_EQ, cond1, source2, 0); 412 + tcg_gen_movcond_tl(TCG_COND_EQ, source2, cond1, zeroreg, source2, 413 + resultopt1); 414 + tcg_gen_remu_tl(resultopt1, source1, source2); 415 + /* if div by zero, just return the original dividend */ 416 + tcg_gen_movcond_tl(TCG_COND_EQ, source1, cond1, zeroreg, resultopt1, 417 + source1); 418 + 419 + tcg_temp_free(cond1); 420 + tcg_temp_free(zeroreg); 421 + tcg_temp_free(resultopt1); 422 + break; 423 + default: 424 + gen_exception_illegal(ctx); 425 + return; 426 + } 427 + 428 + if (opc & 0x8) { /* sign extend for W instructions */ 429 + tcg_gen_ext32s_tl(source1, source1); 430 + } 431 + 432 + gen_set_gpr(rd, source1); 433 + tcg_temp_free(source1); 434 + tcg_temp_free(source2); 435 + } 436 + 437 + static void gen_arith_imm(DisasContext *ctx, uint32_t opc, int rd, 438 + int rs1, target_long imm) 439 + { 440 + TCGv source1 = tcg_temp_new(); 441 + int shift_len = TARGET_LONG_BITS; 442 + int shift_a; 443 + 444 + gen_get_gpr(source1, rs1); 445 + 446 + switch (opc) { 447 + case OPC_RISC_ADDI: 448 + #if defined(TARGET_RISCV64) 449 + case OPC_RISC_ADDIW: 450 + #endif 451 + tcg_gen_addi_tl(source1, source1, imm); 452 + break; 453 + case OPC_RISC_SLTI: 454 + tcg_gen_setcondi_tl(TCG_COND_LT, source1, source1, imm); 455 + break; 456 + case OPC_RISC_SLTIU: 457 + tcg_gen_setcondi_tl(TCG_COND_LTU, source1, source1, imm); 458 + break; 459 + case OPC_RISC_XORI: 460 + tcg_gen_xori_tl(source1, source1, imm); 461 + break; 462 + case OPC_RISC_ORI: 463 + tcg_gen_ori_tl(source1, source1, imm); 464 + break; 465 + case OPC_RISC_ANDI: 466 + tcg_gen_andi_tl(source1, source1, imm); 467 + break; 468 + #if defined(TARGET_RISCV64) 469 + case OPC_RISC_SLLIW: 470 + shift_len = 32; 471 + /* FALLTHRU */ 472 + #endif 473 + case OPC_RISC_SLLI: 474 + if (imm >= shift_len) { 475 + goto do_illegal; 476 + } 477 + tcg_gen_shli_tl(source1, source1, imm); 478 + break; 479 + #if defined(TARGET_RISCV64) 480 + case OPC_RISC_SHIFT_RIGHT_IW: 481 + shift_len = 32; 482 + /* FALLTHRU */ 483 + #endif 484 + case OPC_RISC_SHIFT_RIGHT_I: 485 + /* differentiate on IMM */ 486 + shift_a = imm & 0x400; 487 + imm &= 0x3ff; 488 + if (imm >= shift_len) { 489 + goto do_illegal; 490 + } 491 + if (imm != 0) { 492 + if (shift_a) { 493 + /* SRAI[W] */ 494 + tcg_gen_sextract_tl(source1, source1, imm, shift_len - imm); 495 + } else { 496 + /* SRLI[W] */ 497 + tcg_gen_extract_tl(source1, source1, imm, shift_len - imm); 498 + } 499 + /* No further sign-extension needed for W instructions. */ 500 + opc &= ~0x8; 501 + } 502 + break; 503 + default: 504 + do_illegal: 505 + gen_exception_illegal(ctx); 506 + return; 507 + } 508 + 509 + if (opc & 0x8) { /* sign-extend for W instructions */ 510 + tcg_gen_ext32s_tl(source1, source1); 511 + } 512 + 513 + gen_set_gpr(rd, source1); 514 + tcg_temp_free(source1); 515 + } 516 + 517 + static void gen_jal(CPURISCVState *env, DisasContext *ctx, int rd, 518 + target_ulong imm) 519 + { 520 + target_ulong next_pc; 521 + 522 + /* check misaligned: */ 523 + next_pc = ctx->pc + imm; 524 + if (!riscv_has_ext(env, RVC)) { 525 + if ((next_pc & 0x3) != 0) { 526 + gen_exception_inst_addr_mis(ctx); 527 + return; 528 + } 529 + } 530 + if (rd != 0) { 531 + tcg_gen_movi_tl(cpu_gpr[rd], ctx->next_pc); 532 + } 533 + 534 + gen_goto_tb(ctx, 0, ctx->pc + imm); /* must use this for safety */ 535 + ctx->bstate = BS_BRANCH; 536 + } 537 + 538 + static void gen_jalr(CPURISCVState *env, DisasContext *ctx, uint32_t opc, 539 + int rd, int rs1, target_long imm) 540 + { 541 + /* no chaining with JALR */ 542 + TCGLabel *misaligned = NULL; 543 + TCGv t0 = tcg_temp_new(); 544 + 545 + switch (opc) { 546 + case OPC_RISC_JALR: 547 + gen_get_gpr(cpu_pc, rs1); 548 + tcg_gen_addi_tl(cpu_pc, cpu_pc, imm); 549 + tcg_gen_andi_tl(cpu_pc, cpu_pc, (target_ulong)-2); 550 + 551 + if (!riscv_has_ext(env, RVC)) { 552 + misaligned = gen_new_label(); 553 + tcg_gen_andi_tl(t0, cpu_pc, 0x2); 554 + tcg_gen_brcondi_tl(TCG_COND_NE, t0, 0x0, misaligned); 555 + } 556 + 557 + if (rd != 0) { 558 + tcg_gen_movi_tl(cpu_gpr[rd], ctx->next_pc); 559 + } 560 + tcg_gen_exit_tb(0); 561 + 562 + if (misaligned) { 563 + gen_set_label(misaligned); 564 + gen_exception_inst_addr_mis(ctx); 565 + } 566 + ctx->bstate = BS_BRANCH; 567 + break; 568 + 569 + default: 570 + gen_exception_illegal(ctx); 571 + break; 572 + } 573 + tcg_temp_free(t0); 574 + } 575 + 576 + static void gen_branch(CPURISCVState *env, DisasContext *ctx, uint32_t opc, 577 + int rs1, int rs2, target_long bimm) 578 + { 579 + TCGLabel *l = gen_new_label(); 580 + TCGv source1, source2; 581 + source1 = tcg_temp_new(); 582 + source2 = tcg_temp_new(); 583 + gen_get_gpr(source1, rs1); 584 + gen_get_gpr(source2, rs2); 585 + 586 + switch (opc) { 587 + case OPC_RISC_BEQ: 588 + tcg_gen_brcond_tl(TCG_COND_EQ, source1, source2, l); 589 + break; 590 + case OPC_RISC_BNE: 591 + tcg_gen_brcond_tl(TCG_COND_NE, source1, source2, l); 592 + break; 593 + case OPC_RISC_BLT: 594 + tcg_gen_brcond_tl(TCG_COND_LT, source1, source2, l); 595 + break; 596 + case OPC_RISC_BGE: 597 + tcg_gen_brcond_tl(TCG_COND_GE, source1, source2, l); 598 + break; 599 + case OPC_RISC_BLTU: 600 + tcg_gen_brcond_tl(TCG_COND_LTU, source1, source2, l); 601 + break; 602 + case OPC_RISC_BGEU: 603 + tcg_gen_brcond_tl(TCG_COND_GEU, source1, source2, l); 604 + break; 605 + default: 606 + gen_exception_illegal(ctx); 607 + return; 608 + } 609 + tcg_temp_free(source1); 610 + tcg_temp_free(source2); 611 + 612 + gen_goto_tb(ctx, 1, ctx->next_pc); 613 + gen_set_label(l); /* branch taken */ 614 + if (!riscv_has_ext(env, RVC) && ((ctx->pc + bimm) & 0x3)) { 615 + /* misaligned */ 616 + gen_exception_inst_addr_mis(ctx); 617 + } else { 618 + gen_goto_tb(ctx, 0, ctx->pc + bimm); 619 + } 620 + ctx->bstate = BS_BRANCH; 621 + } 622 + 623 + static void gen_load(DisasContext *ctx, uint32_t opc, int rd, int rs1, 624 + target_long imm) 625 + { 626 + TCGv t0 = tcg_temp_new(); 627 + TCGv t1 = tcg_temp_new(); 628 + gen_get_gpr(t0, rs1); 629 + tcg_gen_addi_tl(t0, t0, imm); 630 + int memop = tcg_memop_lookup[(opc >> 12) & 0x7]; 631 + 632 + if (memop < 0) { 633 + gen_exception_illegal(ctx); 634 + return; 635 + } 636 + 637 + tcg_gen_qemu_ld_tl(t1, t0, ctx->mem_idx, memop); 638 + gen_set_gpr(rd, t1); 639 + tcg_temp_free(t0); 640 + tcg_temp_free(t1); 641 + } 642 + 643 + static void gen_store(DisasContext *ctx, uint32_t opc, int rs1, int rs2, 644 + target_long imm) 645 + { 646 + TCGv t0 = tcg_temp_new(); 647 + TCGv dat = tcg_temp_new(); 648 + gen_get_gpr(t0, rs1); 649 + tcg_gen_addi_tl(t0, t0, imm); 650 + gen_get_gpr(dat, rs2); 651 + int memop = tcg_memop_lookup[(opc >> 12) & 0x7]; 652 + 653 + if (memop < 0) { 654 + gen_exception_illegal(ctx); 655 + return; 656 + } 657 + 658 + tcg_gen_qemu_st_tl(dat, t0, ctx->mem_idx, memop); 659 + tcg_temp_free(t0); 660 + tcg_temp_free(dat); 661 + } 662 + 663 + static void gen_fp_load(DisasContext *ctx, uint32_t opc, int rd, 664 + int rs1, target_long imm) 665 + { 666 + TCGv t0; 667 + 668 + if (!(ctx->flags & TB_FLAGS_FP_ENABLE)) { 669 + gen_exception_illegal(ctx); 670 + return; 671 + } 672 + 673 + t0 = tcg_temp_new(); 674 + gen_get_gpr(t0, rs1); 675 + tcg_gen_addi_tl(t0, t0, imm); 676 + 677 + switch (opc) { 678 + case OPC_RISC_FLW: 679 + tcg_gen_qemu_ld_i64(cpu_fpr[rd], t0, ctx->mem_idx, MO_TEUL); 680 + /* RISC-V requires NaN-boxing of narrower width floating point values */ 681 + tcg_gen_ori_i64(cpu_fpr[rd], cpu_fpr[rd], 0xffffffff00000000ULL); 682 + break; 683 + case OPC_RISC_FLD: 684 + tcg_gen_qemu_ld_i64(cpu_fpr[rd], t0, ctx->mem_idx, MO_TEQ); 685 + break; 686 + default: 687 + gen_exception_illegal(ctx); 688 + break; 689 + } 690 + tcg_temp_free(t0); 691 + } 692 + 693 + static void gen_fp_store(DisasContext *ctx, uint32_t opc, int rs1, 694 + int rs2, target_long imm) 695 + { 696 + TCGv t0; 697 + 698 + if (!(ctx->flags & TB_FLAGS_FP_ENABLE)) { 699 + gen_exception_illegal(ctx); 700 + return; 701 + } 702 + 703 + t0 = tcg_temp_new(); 704 + gen_get_gpr(t0, rs1); 705 + tcg_gen_addi_tl(t0, t0, imm); 706 + 707 + switch (opc) { 708 + case OPC_RISC_FSW: 709 + tcg_gen_qemu_st_i64(cpu_fpr[rs2], t0, ctx->mem_idx, MO_TEUL); 710 + break; 711 + case OPC_RISC_FSD: 712 + tcg_gen_qemu_st_i64(cpu_fpr[rs2], t0, ctx->mem_idx, MO_TEQ); 713 + break; 714 + default: 715 + gen_exception_illegal(ctx); 716 + break; 717 + } 718 + 719 + tcg_temp_free(t0); 720 + } 721 + 722 + static void gen_atomic(DisasContext *ctx, uint32_t opc, 723 + int rd, int rs1, int rs2) 724 + { 725 + TCGv src1, src2, dat; 726 + TCGLabel *l1, *l2; 727 + TCGMemOp mop; 728 + TCGCond cond; 729 + bool aq, rl; 730 + 731 + /* Extract the size of the atomic operation. */ 732 + switch (extract32(opc, 12, 3)) { 733 + case 2: /* 32-bit */ 734 + mop = MO_ALIGN | MO_TESL; 735 + break; 736 + #if defined(TARGET_RISCV64) 737 + case 3: /* 64-bit */ 738 + mop = MO_ALIGN | MO_TEQ; 739 + break; 740 + #endif 741 + default: 742 + gen_exception_illegal(ctx); 743 + return; 744 + } 745 + rl = extract32(opc, 25, 1); 746 + aq = extract32(opc, 26, 1); 747 + 748 + src1 = tcg_temp_new(); 749 + src2 = tcg_temp_new(); 750 + 751 + switch (MASK_OP_ATOMIC_NO_AQ_RL_SZ(opc)) { 752 + case OPC_RISC_LR: 753 + /* Put addr in load_res, data in load_val. */ 754 + gen_get_gpr(src1, rs1); 755 + if (rl) { 756 + tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL); 757 + } 758 + tcg_gen_qemu_ld_tl(load_val, src1, ctx->mem_idx, mop); 759 + if (aq) { 760 + tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ); 761 + } 762 + tcg_gen_mov_tl(load_res, src1); 763 + gen_set_gpr(rd, load_val); 764 + break; 765 + 766 + case OPC_RISC_SC: 767 + l1 = gen_new_label(); 768 + l2 = gen_new_label(); 769 + dat = tcg_temp_new(); 770 + 771 + gen_get_gpr(src1, rs1); 772 + tcg_gen_brcond_tl(TCG_COND_NE, load_res, src1, l1); 773 + 774 + gen_get_gpr(src2, rs2); 775 + /* Note that the TCG atomic primitives are SC, 776 + so we can ignore AQ/RL along this path. */ 777 + tcg_gen_atomic_cmpxchg_tl(src1, load_res, load_val, src2, 778 + ctx->mem_idx, mop); 779 + tcg_gen_setcond_tl(TCG_COND_NE, dat, src1, load_val); 780 + gen_set_gpr(rd, dat); 781 + tcg_gen_br(l2); 782 + 783 + gen_set_label(l1); 784 + /* Address comparion failure. However, we still need to 785 + provide the memory barrier implied by AQ/RL. */ 786 + tcg_gen_mb(TCG_MO_ALL + aq * TCG_BAR_LDAQ + rl * TCG_BAR_STRL); 787 + tcg_gen_movi_tl(dat, 1); 788 + gen_set_gpr(rd, dat); 789 + 790 + gen_set_label(l2); 791 + tcg_temp_free(dat); 792 + break; 793 + 794 + case OPC_RISC_AMOSWAP: 795 + /* Note that the TCG atomic primitives are SC, 796 + so we can ignore AQ/RL along this path. */ 797 + gen_get_gpr(src1, rs1); 798 + gen_get_gpr(src2, rs2); 799 + tcg_gen_atomic_xchg_tl(src2, src1, src2, ctx->mem_idx, mop); 800 + gen_set_gpr(rd, src2); 801 + break; 802 + case OPC_RISC_AMOADD: 803 + gen_get_gpr(src1, rs1); 804 + gen_get_gpr(src2, rs2); 805 + tcg_gen_atomic_fetch_add_tl(src2, src1, src2, ctx->mem_idx, mop); 806 + gen_set_gpr(rd, src2); 807 + break; 808 + case OPC_RISC_AMOXOR: 809 + gen_get_gpr(src1, rs1); 810 + gen_get_gpr(src2, rs2); 811 + tcg_gen_atomic_fetch_xor_tl(src2, src1, src2, ctx->mem_idx, mop); 812 + gen_set_gpr(rd, src2); 813 + break; 814 + case OPC_RISC_AMOAND: 815 + gen_get_gpr(src1, rs1); 816 + gen_get_gpr(src2, rs2); 817 + tcg_gen_atomic_fetch_and_tl(src2, src1, src2, ctx->mem_idx, mop); 818 + gen_set_gpr(rd, src2); 819 + break; 820 + case OPC_RISC_AMOOR: 821 + gen_get_gpr(src1, rs1); 822 + gen_get_gpr(src2, rs2); 823 + tcg_gen_atomic_fetch_or_tl(src2, src1, src2, ctx->mem_idx, mop); 824 + gen_set_gpr(rd, src2); 825 + break; 826 + 827 + case OPC_RISC_AMOMIN: 828 + cond = TCG_COND_LT; 829 + goto do_minmax; 830 + case OPC_RISC_AMOMAX: 831 + cond = TCG_COND_GT; 832 + goto do_minmax; 833 + case OPC_RISC_AMOMINU: 834 + cond = TCG_COND_LTU; 835 + goto do_minmax; 836 + case OPC_RISC_AMOMAXU: 837 + cond = TCG_COND_GTU; 838 + goto do_minmax; 839 + do_minmax: 840 + /* Handle the RL barrier. The AQ barrier is handled along the 841 + parallel path by the SC atomic cmpxchg. On the serial path, 842 + of course, barriers do not matter. */ 843 + if (rl) { 844 + tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL); 845 + } 846 + if (tb_cflags(ctx->tb) & CF_PARALLEL) { 847 + l1 = gen_new_label(); 848 + gen_set_label(l1); 849 + } else { 850 + l1 = NULL; 851 + } 852 + 853 + gen_get_gpr(src1, rs1); 854 + gen_get_gpr(src2, rs2); 855 + if ((mop & MO_SSIZE) == MO_SL) { 856 + /* Sign-extend the register comparison input. */ 857 + tcg_gen_ext32s_tl(src2, src2); 858 + } 859 + dat = tcg_temp_local_new(); 860 + tcg_gen_qemu_ld_tl(dat, src1, ctx->mem_idx, mop); 861 + tcg_gen_movcond_tl(cond, src2, dat, src2, dat, src2); 862 + 863 + if (tb_cflags(ctx->tb) & CF_PARALLEL) { 864 + /* Parallel context. Make this operation atomic by verifying 865 + that the memory didn't change while we computed the result. */ 866 + tcg_gen_atomic_cmpxchg_tl(src2, src1, dat, src2, ctx->mem_idx, mop); 867 + 868 + /* If the cmpxchg failed, retry. */ 869 + /* ??? There is an assumption here that this will eventually 870 + succeed, such that we don't live-lock. This is not unlike 871 + a similar loop that the compiler would generate for e.g. 872 + __atomic_fetch_and_xor, so don't worry about it. */ 873 + tcg_gen_brcond_tl(TCG_COND_NE, dat, src2, l1); 874 + } else { 875 + /* Serial context. Directly store the result. */ 876 + tcg_gen_qemu_st_tl(src2, src1, ctx->mem_idx, mop); 877 + } 878 + gen_set_gpr(rd, dat); 879 + tcg_temp_free(dat); 880 + break; 881 + 882 + default: 883 + gen_exception_illegal(ctx); 884 + break; 885 + } 886 + 887 + tcg_temp_free(src1); 888 + tcg_temp_free(src2); 889 + } 890 + 891 + static void gen_set_rm(DisasContext *ctx, int rm) 892 + { 893 + TCGv_i32 t0; 894 + 895 + if (ctx->frm == rm) { 896 + return; 897 + } 898 + ctx->frm = rm; 899 + t0 = tcg_const_i32(rm); 900 + gen_helper_set_rounding_mode(cpu_env, t0); 901 + tcg_temp_free_i32(t0); 902 + } 903 + 904 + static void gen_fp_fmadd(DisasContext *ctx, uint32_t opc, int rd, 905 + int rs1, int rs2, int rs3, int rm) 906 + { 907 + switch (opc) { 908 + case OPC_RISC_FMADD_S: 909 + gen_set_rm(ctx, rm); 910 + gen_helper_fmadd_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 911 + cpu_fpr[rs2], cpu_fpr[rs3]); 912 + break; 913 + case OPC_RISC_FMADD_D: 914 + gen_set_rm(ctx, rm); 915 + gen_helper_fmadd_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 916 + cpu_fpr[rs2], cpu_fpr[rs3]); 917 + break; 918 + default: 919 + gen_exception_illegal(ctx); 920 + break; 921 + } 922 + } 923 + 924 + static void gen_fp_fmsub(DisasContext *ctx, uint32_t opc, int rd, 925 + int rs1, int rs2, int rs3, int rm) 926 + { 927 + switch (opc) { 928 + case OPC_RISC_FMSUB_S: 929 + gen_set_rm(ctx, rm); 930 + gen_helper_fmsub_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 931 + cpu_fpr[rs2], cpu_fpr[rs3]); 932 + break; 933 + case OPC_RISC_FMSUB_D: 934 + gen_set_rm(ctx, rm); 935 + gen_helper_fmsub_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 936 + cpu_fpr[rs2], cpu_fpr[rs3]); 937 + break; 938 + default: 939 + gen_exception_illegal(ctx); 940 + break; 941 + } 942 + } 943 + 944 + static void gen_fp_fnmsub(DisasContext *ctx, uint32_t opc, int rd, 945 + int rs1, int rs2, int rs3, int rm) 946 + { 947 + switch (opc) { 948 + case OPC_RISC_FNMSUB_S: 949 + gen_set_rm(ctx, rm); 950 + gen_helper_fnmsub_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 951 + cpu_fpr[rs2], cpu_fpr[rs3]); 952 + break; 953 + case OPC_RISC_FNMSUB_D: 954 + gen_set_rm(ctx, rm); 955 + gen_helper_fnmsub_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 956 + cpu_fpr[rs2], cpu_fpr[rs3]); 957 + break; 958 + default: 959 + gen_exception_illegal(ctx); 960 + break; 961 + } 962 + } 963 + 964 + static void gen_fp_fnmadd(DisasContext *ctx, uint32_t opc, int rd, 965 + int rs1, int rs2, int rs3, int rm) 966 + { 967 + switch (opc) { 968 + case OPC_RISC_FNMADD_S: 969 + gen_set_rm(ctx, rm); 970 + gen_helper_fnmadd_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 971 + cpu_fpr[rs2], cpu_fpr[rs3]); 972 + break; 973 + case OPC_RISC_FNMADD_D: 974 + gen_set_rm(ctx, rm); 975 + gen_helper_fnmadd_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], 976 + cpu_fpr[rs2], cpu_fpr[rs3]); 977 + break; 978 + default: 979 + gen_exception_illegal(ctx); 980 + break; 981 + } 982 + } 983 + 984 + static void gen_fp_arith(DisasContext *ctx, uint32_t opc, int rd, 985 + int rs1, int rs2, int rm) 986 + { 987 + TCGv t0 = NULL; 988 + 989 + if (!(ctx->flags & TB_FLAGS_FP_ENABLE)) { 990 + goto do_illegal; 991 + } 992 + 993 + switch (opc) { 994 + case OPC_RISC_FADD_S: 995 + gen_set_rm(ctx, rm); 996 + gen_helper_fadd_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 997 + break; 998 + case OPC_RISC_FSUB_S: 999 + gen_set_rm(ctx, rm); 1000 + gen_helper_fsub_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1001 + break; 1002 + case OPC_RISC_FMUL_S: 1003 + gen_set_rm(ctx, rm); 1004 + gen_helper_fmul_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1005 + break; 1006 + case OPC_RISC_FDIV_S: 1007 + gen_set_rm(ctx, rm); 1008 + gen_helper_fdiv_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1009 + break; 1010 + case OPC_RISC_FSQRT_S: 1011 + gen_set_rm(ctx, rm); 1012 + gen_helper_fsqrt_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1]); 1013 + break; 1014 + case OPC_RISC_FSGNJ_S: 1015 + gen_fsgnj(ctx, rd, rs1, rs2, rm, INT32_MIN); 1016 + break; 1017 + 1018 + case OPC_RISC_FMIN_S: 1019 + /* also handles: OPC_RISC_FMAX_S */ 1020 + switch (rm) { 1021 + case 0x0: 1022 + gen_helper_fmin_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1023 + break; 1024 + case 0x1: 1025 + gen_helper_fmax_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1026 + break; 1027 + default: 1028 + goto do_illegal; 1029 + } 1030 + break; 1031 + 1032 + case OPC_RISC_FEQ_S: 1033 + /* also handles: OPC_RISC_FLT_S, OPC_RISC_FLE_S */ 1034 + t0 = tcg_temp_new(); 1035 + switch (rm) { 1036 + case 0x0: 1037 + gen_helper_fle_s(t0, cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1038 + break; 1039 + case 0x1: 1040 + gen_helper_flt_s(t0, cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1041 + break; 1042 + case 0x2: 1043 + gen_helper_feq_s(t0, cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1044 + break; 1045 + default: 1046 + goto do_illegal; 1047 + } 1048 + gen_set_gpr(rd, t0); 1049 + tcg_temp_free(t0); 1050 + break; 1051 + 1052 + case OPC_RISC_FCVT_W_S: 1053 + /* also OPC_RISC_FCVT_WU_S, OPC_RISC_FCVT_L_S, OPC_RISC_FCVT_LU_S */ 1054 + t0 = tcg_temp_new(); 1055 + switch (rs2) { 1056 + case 0: /* FCVT_W_S */ 1057 + gen_set_rm(ctx, rm); 1058 + gen_helper_fcvt_w_s(t0, cpu_env, cpu_fpr[rs1]); 1059 + break; 1060 + case 1: /* FCVT_WU_S */ 1061 + gen_set_rm(ctx, rm); 1062 + gen_helper_fcvt_wu_s(t0, cpu_env, cpu_fpr[rs1]); 1063 + break; 1064 + #if defined(TARGET_RISCV64) 1065 + case 2: /* FCVT_L_S */ 1066 + gen_set_rm(ctx, rm); 1067 + gen_helper_fcvt_l_s(t0, cpu_env, cpu_fpr[rs1]); 1068 + break; 1069 + case 3: /* FCVT_LU_S */ 1070 + gen_set_rm(ctx, rm); 1071 + gen_helper_fcvt_lu_s(t0, cpu_env, cpu_fpr[rs1]); 1072 + break; 1073 + #endif 1074 + default: 1075 + goto do_illegal; 1076 + } 1077 + gen_set_gpr(rd, t0); 1078 + tcg_temp_free(t0); 1079 + break; 1080 + 1081 + case OPC_RISC_FCVT_S_W: 1082 + /* also OPC_RISC_FCVT_S_WU, OPC_RISC_FCVT_S_L, OPC_RISC_FCVT_S_LU */ 1083 + t0 = tcg_temp_new(); 1084 + gen_get_gpr(t0, rs1); 1085 + switch (rs2) { 1086 + case 0: /* FCVT_S_W */ 1087 + gen_set_rm(ctx, rm); 1088 + gen_helper_fcvt_s_w(cpu_fpr[rd], cpu_env, t0); 1089 + break; 1090 + case 1: /* FCVT_S_WU */ 1091 + gen_set_rm(ctx, rm); 1092 + gen_helper_fcvt_s_wu(cpu_fpr[rd], cpu_env, t0); 1093 + break; 1094 + #if defined(TARGET_RISCV64) 1095 + case 2: /* FCVT_S_L */ 1096 + gen_set_rm(ctx, rm); 1097 + gen_helper_fcvt_s_l(cpu_fpr[rd], cpu_env, t0); 1098 + break; 1099 + case 3: /* FCVT_S_LU */ 1100 + gen_set_rm(ctx, rm); 1101 + gen_helper_fcvt_s_lu(cpu_fpr[rd], cpu_env, t0); 1102 + break; 1103 + #endif 1104 + default: 1105 + goto do_illegal; 1106 + } 1107 + tcg_temp_free(t0); 1108 + break; 1109 + 1110 + case OPC_RISC_FMV_X_S: 1111 + /* also OPC_RISC_FCLASS_S */ 1112 + t0 = tcg_temp_new(); 1113 + switch (rm) { 1114 + case 0: /* FMV */ 1115 + #if defined(TARGET_RISCV64) 1116 + tcg_gen_ext32s_tl(t0, cpu_fpr[rs1]); 1117 + #else 1118 + tcg_gen_extrl_i64_i32(t0, cpu_fpr[rs1]); 1119 + #endif 1120 + break; 1121 + case 1: 1122 + gen_helper_fclass_s(t0, cpu_fpr[rs1]); 1123 + break; 1124 + default: 1125 + goto do_illegal; 1126 + } 1127 + gen_set_gpr(rd, t0); 1128 + tcg_temp_free(t0); 1129 + break; 1130 + 1131 + case OPC_RISC_FMV_S_X: 1132 + t0 = tcg_temp_new(); 1133 + gen_get_gpr(t0, rs1); 1134 + #if defined(TARGET_RISCV64) 1135 + tcg_gen_mov_i64(cpu_fpr[rd], t0); 1136 + #else 1137 + tcg_gen_extu_i32_i64(cpu_fpr[rd], t0); 1138 + #endif 1139 + tcg_temp_free(t0); 1140 + break; 1141 + 1142 + /* double */ 1143 + case OPC_RISC_FADD_D: 1144 + gen_set_rm(ctx, rm); 1145 + gen_helper_fadd_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1146 + break; 1147 + case OPC_RISC_FSUB_D: 1148 + gen_set_rm(ctx, rm); 1149 + gen_helper_fsub_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1150 + break; 1151 + case OPC_RISC_FMUL_D: 1152 + gen_set_rm(ctx, rm); 1153 + gen_helper_fmul_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1154 + break; 1155 + case OPC_RISC_FDIV_D: 1156 + gen_set_rm(ctx, rm); 1157 + gen_helper_fdiv_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1158 + break; 1159 + case OPC_RISC_FSQRT_D: 1160 + gen_set_rm(ctx, rm); 1161 + gen_helper_fsqrt_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1]); 1162 + break; 1163 + case OPC_RISC_FSGNJ_D: 1164 + gen_fsgnj(ctx, rd, rs1, rs2, rm, INT64_MIN); 1165 + break; 1166 + 1167 + case OPC_RISC_FMIN_D: 1168 + /* also OPC_RISC_FMAX_D */ 1169 + switch (rm) { 1170 + case 0: 1171 + gen_helper_fmin_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1172 + break; 1173 + case 1: 1174 + gen_helper_fmax_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1175 + break; 1176 + default: 1177 + goto do_illegal; 1178 + } 1179 + break; 1180 + 1181 + case OPC_RISC_FCVT_S_D: 1182 + switch (rs2) { 1183 + case 1: 1184 + gen_set_rm(ctx, rm); 1185 + gen_helper_fcvt_s_d(cpu_fpr[rd], cpu_env, cpu_fpr[rs1]); 1186 + break; 1187 + default: 1188 + goto do_illegal; 1189 + } 1190 + break; 1191 + 1192 + case OPC_RISC_FCVT_D_S: 1193 + switch (rs2) { 1194 + case 0: 1195 + gen_set_rm(ctx, rm); 1196 + gen_helper_fcvt_d_s(cpu_fpr[rd], cpu_env, cpu_fpr[rs1]); 1197 + break; 1198 + default: 1199 + goto do_illegal; 1200 + } 1201 + break; 1202 + 1203 + case OPC_RISC_FEQ_D: 1204 + /* also OPC_RISC_FLT_D, OPC_RISC_FLE_D */ 1205 + t0 = tcg_temp_new(); 1206 + switch (rm) { 1207 + case 0: 1208 + gen_helper_fle_d(t0, cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1209 + break; 1210 + case 1: 1211 + gen_helper_flt_d(t0, cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1212 + break; 1213 + case 2: 1214 + gen_helper_feq_d(t0, cpu_env, cpu_fpr[rs1], cpu_fpr[rs2]); 1215 + break; 1216 + default: 1217 + goto do_illegal; 1218 + } 1219 + gen_set_gpr(rd, t0); 1220 + tcg_temp_free(t0); 1221 + break; 1222 + 1223 + case OPC_RISC_FCVT_W_D: 1224 + /* also OPC_RISC_FCVT_WU_D, OPC_RISC_FCVT_L_D, OPC_RISC_FCVT_LU_D */ 1225 + t0 = tcg_temp_new(); 1226 + switch (rs2) { 1227 + case 0: 1228 + gen_set_rm(ctx, rm); 1229 + gen_helper_fcvt_w_d(t0, cpu_env, cpu_fpr[rs1]); 1230 + break; 1231 + case 1: 1232 + gen_set_rm(ctx, rm); 1233 + gen_helper_fcvt_wu_d(t0, cpu_env, cpu_fpr[rs1]); 1234 + break; 1235 + #if defined(TARGET_RISCV64) 1236 + case 2: 1237 + gen_set_rm(ctx, rm); 1238 + gen_helper_fcvt_l_d(t0, cpu_env, cpu_fpr[rs1]); 1239 + break; 1240 + case 3: 1241 + gen_set_rm(ctx, rm); 1242 + gen_helper_fcvt_lu_d(t0, cpu_env, cpu_fpr[rs1]); 1243 + break; 1244 + #endif 1245 + default: 1246 + goto do_illegal; 1247 + } 1248 + gen_set_gpr(rd, t0); 1249 + tcg_temp_free(t0); 1250 + break; 1251 + 1252 + case OPC_RISC_FCVT_D_W: 1253 + /* also OPC_RISC_FCVT_D_WU, OPC_RISC_FCVT_D_L, OPC_RISC_FCVT_D_LU */ 1254 + t0 = tcg_temp_new(); 1255 + gen_get_gpr(t0, rs1); 1256 + switch (rs2) { 1257 + case 0: 1258 + gen_set_rm(ctx, rm); 1259 + gen_helper_fcvt_d_w(cpu_fpr[rd], cpu_env, t0); 1260 + break; 1261 + case 1: 1262 + gen_set_rm(ctx, rm); 1263 + gen_helper_fcvt_d_wu(cpu_fpr[rd], cpu_env, t0); 1264 + break; 1265 + #if defined(TARGET_RISCV64) 1266 + case 2: 1267 + gen_set_rm(ctx, rm); 1268 + gen_helper_fcvt_d_l(cpu_fpr[rd], cpu_env, t0); 1269 + break; 1270 + case 3: 1271 + gen_set_rm(ctx, rm); 1272 + gen_helper_fcvt_d_lu(cpu_fpr[rd], cpu_env, t0); 1273 + break; 1274 + #endif 1275 + default: 1276 + goto do_illegal; 1277 + } 1278 + tcg_temp_free(t0); 1279 + break; 1280 + 1281 + #if defined(TARGET_RISCV64) 1282 + case OPC_RISC_FMV_X_D: 1283 + /* also OPC_RISC_FCLASS_D */ 1284 + switch (rm) { 1285 + case 0: /* FMV */ 1286 + gen_set_gpr(rd, cpu_fpr[rs1]); 1287 + break; 1288 + case 1: 1289 + t0 = tcg_temp_new(); 1290 + gen_helper_fclass_d(t0, cpu_fpr[rs1]); 1291 + gen_set_gpr(rd, t0); 1292 + tcg_temp_free(t0); 1293 + break; 1294 + default: 1295 + goto do_illegal; 1296 + } 1297 + break; 1298 + 1299 + case OPC_RISC_FMV_D_X: 1300 + t0 = tcg_temp_new(); 1301 + gen_get_gpr(t0, rs1); 1302 + tcg_gen_mov_tl(cpu_fpr[rd], t0); 1303 + tcg_temp_free(t0); 1304 + break; 1305 + #endif 1306 + 1307 + default: 1308 + do_illegal: 1309 + if (t0) { 1310 + tcg_temp_free(t0); 1311 + } 1312 + gen_exception_illegal(ctx); 1313 + break; 1314 + } 1315 + } 1316 + 1317 + static void gen_system(CPURISCVState *env, DisasContext *ctx, uint32_t opc, 1318 + int rd, int rs1, int csr) 1319 + { 1320 + TCGv source1, csr_store, dest, rs1_pass, imm_rs1; 1321 + source1 = tcg_temp_new(); 1322 + csr_store = tcg_temp_new(); 1323 + dest = tcg_temp_new(); 1324 + rs1_pass = tcg_temp_new(); 1325 + imm_rs1 = tcg_temp_new(); 1326 + gen_get_gpr(source1, rs1); 1327 + tcg_gen_movi_tl(cpu_pc, ctx->pc); 1328 + tcg_gen_movi_tl(rs1_pass, rs1); 1329 + tcg_gen_movi_tl(csr_store, csr); /* copy into temp reg to feed to helper */ 1330 + 1331 + #ifndef CONFIG_USER_ONLY 1332 + /* Extract funct7 value and check whether it matches SFENCE.VMA */ 1333 + if ((opc == OPC_RISC_ECALL) && ((csr >> 5) == 9)) { 1334 + /* sfence.vma */ 1335 + /* TODO: handle ASID specific fences */ 1336 + gen_helper_tlb_flush(cpu_env); 1337 + return; 1338 + } 1339 + #endif 1340 + 1341 + switch (opc) { 1342 + case OPC_RISC_ECALL: 1343 + switch (csr) { 1344 + case 0x0: /* ECALL */ 1345 + /* always generates U-level ECALL, fixed in do_interrupt handler */ 1346 + generate_exception(ctx, RISCV_EXCP_U_ECALL); 1347 + tcg_gen_exit_tb(0); /* no chaining */ 1348 + ctx->bstate = BS_BRANCH; 1349 + break; 1350 + case 0x1: /* EBREAK */ 1351 + generate_exception(ctx, RISCV_EXCP_BREAKPOINT); 1352 + tcg_gen_exit_tb(0); /* no chaining */ 1353 + ctx->bstate = BS_BRANCH; 1354 + break; 1355 + #ifndef CONFIG_USER_ONLY 1356 + case 0x002: /* URET */ 1357 + gen_exception_illegal(ctx); 1358 + break; 1359 + case 0x102: /* SRET */ 1360 + if (riscv_has_ext(env, RVS)) { 1361 + gen_helper_sret(cpu_pc, cpu_env, cpu_pc); 1362 + tcg_gen_exit_tb(0); /* no chaining */ 1363 + ctx->bstate = BS_BRANCH; 1364 + } else { 1365 + gen_exception_illegal(ctx); 1366 + } 1367 + break; 1368 + case 0x202: /* HRET */ 1369 + gen_exception_illegal(ctx); 1370 + break; 1371 + case 0x302: /* MRET */ 1372 + gen_helper_mret(cpu_pc, cpu_env, cpu_pc); 1373 + tcg_gen_exit_tb(0); /* no chaining */ 1374 + ctx->bstate = BS_BRANCH; 1375 + break; 1376 + case 0x7b2: /* DRET */ 1377 + gen_exception_illegal(ctx); 1378 + break; 1379 + case 0x105: /* WFI */ 1380 + tcg_gen_movi_tl(cpu_pc, ctx->next_pc); 1381 + gen_helper_wfi(cpu_env); 1382 + break; 1383 + case 0x104: /* SFENCE.VM */ 1384 + gen_helper_tlb_flush(cpu_env); 1385 + break; 1386 + #endif 1387 + default: 1388 + gen_exception_illegal(ctx); 1389 + break; 1390 + } 1391 + break; 1392 + default: 1393 + tcg_gen_movi_tl(imm_rs1, rs1); 1394 + switch (opc) { 1395 + case OPC_RISC_CSRRW: 1396 + gen_helper_csrrw(dest, cpu_env, source1, csr_store); 1397 + break; 1398 + case OPC_RISC_CSRRS: 1399 + gen_helper_csrrs(dest, cpu_env, source1, csr_store, rs1_pass); 1400 + break; 1401 + case OPC_RISC_CSRRC: 1402 + gen_helper_csrrc(dest, cpu_env, source1, csr_store, rs1_pass); 1403 + break; 1404 + case OPC_RISC_CSRRWI: 1405 + gen_helper_csrrw(dest, cpu_env, imm_rs1, csr_store); 1406 + break; 1407 + case OPC_RISC_CSRRSI: 1408 + gen_helper_csrrs(dest, cpu_env, imm_rs1, csr_store, rs1_pass); 1409 + break; 1410 + case OPC_RISC_CSRRCI: 1411 + gen_helper_csrrc(dest, cpu_env, imm_rs1, csr_store, rs1_pass); 1412 + break; 1413 + default: 1414 + gen_exception_illegal(ctx); 1415 + return; 1416 + } 1417 + gen_set_gpr(rd, dest); 1418 + /* end tb since we may be changing priv modes, to get mmu_index right */ 1419 + tcg_gen_movi_tl(cpu_pc, ctx->next_pc); 1420 + tcg_gen_exit_tb(0); /* no chaining */ 1421 + ctx->bstate = BS_BRANCH; 1422 + break; 1423 + } 1424 + tcg_temp_free(source1); 1425 + tcg_temp_free(csr_store); 1426 + tcg_temp_free(dest); 1427 + tcg_temp_free(rs1_pass); 1428 + tcg_temp_free(imm_rs1); 1429 + } 1430 + 1431 + static void decode_RV32_64C0(DisasContext *ctx) 1432 + { 1433 + uint8_t funct3 = extract32(ctx->opcode, 13, 3); 1434 + uint8_t rd_rs2 = GET_C_RS2S(ctx->opcode); 1435 + uint8_t rs1s = GET_C_RS1S(ctx->opcode); 1436 + 1437 + switch (funct3) { 1438 + case 0: 1439 + /* illegal */ 1440 + if (ctx->opcode == 0) { 1441 + gen_exception_illegal(ctx); 1442 + } else { 1443 + /* C.ADDI4SPN -> addi rd', x2, zimm[9:2]*/ 1444 + gen_arith_imm(ctx, OPC_RISC_ADDI, rd_rs2, 2, 1445 + GET_C_ADDI4SPN_IMM(ctx->opcode)); 1446 + } 1447 + break; 1448 + case 1: 1449 + /* C.FLD -> fld rd', offset[7:3](rs1')*/ 1450 + gen_fp_load(ctx, OPC_RISC_FLD, rd_rs2, rs1s, 1451 + GET_C_LD_IMM(ctx->opcode)); 1452 + /* C.LQ(RV128) */ 1453 + break; 1454 + case 2: 1455 + /* C.LW -> lw rd', offset[6:2](rs1') */ 1456 + gen_load(ctx, OPC_RISC_LW, rd_rs2, rs1s, 1457 + GET_C_LW_IMM(ctx->opcode)); 1458 + break; 1459 + case 3: 1460 + #if defined(TARGET_RISCV64) 1461 + /* C.LD(RV64/128) -> ld rd', offset[7:3](rs1')*/ 1462 + gen_load(ctx, OPC_RISC_LD, rd_rs2, rs1s, 1463 + GET_C_LD_IMM(ctx->opcode)); 1464 + #else 1465 + /* C.FLW (RV32) -> flw rd', offset[6:2](rs1')*/ 1466 + gen_fp_load(ctx, OPC_RISC_FLW, rd_rs2, rs1s, 1467 + GET_C_LW_IMM(ctx->opcode)); 1468 + #endif 1469 + break; 1470 + case 4: 1471 + /* reserved */ 1472 + gen_exception_illegal(ctx); 1473 + break; 1474 + case 5: 1475 + /* C.FSD(RV32/64) -> fsd rs2', offset[7:3](rs1') */ 1476 + gen_fp_store(ctx, OPC_RISC_FSD, rs1s, rd_rs2, 1477 + GET_C_LD_IMM(ctx->opcode)); 1478 + /* C.SQ (RV128) */ 1479 + break; 1480 + case 6: 1481 + /* C.SW -> sw rs2', offset[6:2](rs1')*/ 1482 + gen_store(ctx, OPC_RISC_SW, rs1s, rd_rs2, 1483 + GET_C_LW_IMM(ctx->opcode)); 1484 + break; 1485 + case 7: 1486 + #if defined(TARGET_RISCV64) 1487 + /* C.SD (RV64/128) -> sd rs2', offset[7:3](rs1')*/ 1488 + gen_store(ctx, OPC_RISC_SD, rs1s, rd_rs2, 1489 + GET_C_LD_IMM(ctx->opcode)); 1490 + #else 1491 + /* C.FSW (RV32) -> fsw rs2', offset[6:2](rs1')*/ 1492 + gen_fp_store(ctx, OPC_RISC_FSW, rs1s, rd_rs2, 1493 + GET_C_LW_IMM(ctx->opcode)); 1494 + #endif 1495 + break; 1496 + } 1497 + } 1498 + 1499 + static void decode_RV32_64C1(CPURISCVState *env, DisasContext *ctx) 1500 + { 1501 + uint8_t funct3 = extract32(ctx->opcode, 13, 3); 1502 + uint8_t rd_rs1 = GET_C_RS1(ctx->opcode); 1503 + uint8_t rs1s, rs2s; 1504 + uint8_t funct2; 1505 + 1506 + switch (funct3) { 1507 + case 0: 1508 + /* C.ADDI -> addi rd, rd, nzimm[5:0] */ 1509 + gen_arith_imm(ctx, OPC_RISC_ADDI, rd_rs1, rd_rs1, 1510 + GET_C_IMM(ctx->opcode)); 1511 + break; 1512 + case 1: 1513 + #if defined(TARGET_RISCV64) 1514 + /* C.ADDIW (RV64/128) -> addiw rd, rd, imm[5:0]*/ 1515 + gen_arith_imm(ctx, OPC_RISC_ADDIW, rd_rs1, rd_rs1, 1516 + GET_C_IMM(ctx->opcode)); 1517 + #else 1518 + /* C.JAL(RV32) -> jal x1, offset[11:1] */ 1519 + gen_jal(env, ctx, 1, GET_C_J_IMM(ctx->opcode)); 1520 + #endif 1521 + break; 1522 + case 2: 1523 + /* C.LI -> addi rd, x0, imm[5:0]*/ 1524 + gen_arith_imm(ctx, OPC_RISC_ADDI, rd_rs1, 0, GET_C_IMM(ctx->opcode)); 1525 + break; 1526 + case 3: 1527 + if (rd_rs1 == 2) { 1528 + /* C.ADDI16SP -> addi x2, x2, nzimm[9:4]*/ 1529 + gen_arith_imm(ctx, OPC_RISC_ADDI, 2, 2, 1530 + GET_C_ADDI16SP_IMM(ctx->opcode)); 1531 + } else if (rd_rs1 != 0) { 1532 + /* C.LUI (rs1/rd =/= {0,2}) -> lui rd, nzimm[17:12]*/ 1533 + tcg_gen_movi_tl(cpu_gpr[rd_rs1], 1534 + GET_C_IMM(ctx->opcode) << 12); 1535 + } 1536 + break; 1537 + case 4: 1538 + funct2 = extract32(ctx->opcode, 10, 2); 1539 + rs1s = GET_C_RS1S(ctx->opcode); 1540 + switch (funct2) { 1541 + case 0: /* C.SRLI(RV32) -> srli rd', rd', shamt[5:0] */ 1542 + gen_arith_imm(ctx, OPC_RISC_SHIFT_RIGHT_I, rs1s, rs1s, 1543 + GET_C_ZIMM(ctx->opcode)); 1544 + /* C.SRLI64(RV128) */ 1545 + break; 1546 + case 1: 1547 + /* C.SRAI -> srai rd', rd', shamt[5:0]*/ 1548 + gen_arith_imm(ctx, OPC_RISC_SHIFT_RIGHT_I, rs1s, rs1s, 1549 + GET_C_ZIMM(ctx->opcode) | 0x400); 1550 + /* C.SRAI64(RV128) */ 1551 + break; 1552 + case 2: 1553 + /* C.ANDI -> andi rd', rd', imm[5:0]*/ 1554 + gen_arith_imm(ctx, OPC_RISC_ANDI, rs1s, rs1s, 1555 + GET_C_IMM(ctx->opcode)); 1556 + break; 1557 + case 3: 1558 + funct2 = extract32(ctx->opcode, 5, 2); 1559 + rs2s = GET_C_RS2S(ctx->opcode); 1560 + switch (funct2) { 1561 + case 0: 1562 + /* C.SUB -> sub rd', rd', rs2' */ 1563 + if (extract32(ctx->opcode, 12, 1) == 0) { 1564 + gen_arith(ctx, OPC_RISC_SUB, rs1s, rs1s, rs2s); 1565 + } 1566 + #if defined(TARGET_RISCV64) 1567 + else { 1568 + gen_arith(ctx, OPC_RISC_SUBW, rs1s, rs1s, rs2s); 1569 + } 1570 + #endif 1571 + break; 1572 + case 1: 1573 + /* C.XOR -> xor rs1', rs1', rs2' */ 1574 + if (extract32(ctx->opcode, 12, 1) == 0) { 1575 + gen_arith(ctx, OPC_RISC_XOR, rs1s, rs1s, rs2s); 1576 + } 1577 + #if defined(TARGET_RISCV64) 1578 + else { 1579 + /* C.ADDW (RV64/128) */ 1580 + gen_arith(ctx, OPC_RISC_ADDW, rs1s, rs1s, rs2s); 1581 + } 1582 + #endif 1583 + break; 1584 + case 2: 1585 + /* C.OR -> or rs1', rs1', rs2' */ 1586 + gen_arith(ctx, OPC_RISC_OR, rs1s, rs1s, rs2s); 1587 + break; 1588 + case 3: 1589 + /* C.AND -> and rs1', rs1', rs2' */ 1590 + gen_arith(ctx, OPC_RISC_AND, rs1s, rs1s, rs2s); 1591 + break; 1592 + } 1593 + break; 1594 + } 1595 + break; 1596 + case 5: 1597 + /* C.J -> jal x0, offset[11:1]*/ 1598 + gen_jal(env, ctx, 0, GET_C_J_IMM(ctx->opcode)); 1599 + break; 1600 + case 6: 1601 + /* C.BEQZ -> beq rs1', x0, offset[8:1]*/ 1602 + rs1s = GET_C_RS1S(ctx->opcode); 1603 + gen_branch(env, ctx, OPC_RISC_BEQ, rs1s, 0, GET_C_B_IMM(ctx->opcode)); 1604 + break; 1605 + case 7: 1606 + /* C.BNEZ -> bne rs1', x0, offset[8:1]*/ 1607 + rs1s = GET_C_RS1S(ctx->opcode); 1608 + gen_branch(env, ctx, OPC_RISC_BNE, rs1s, 0, GET_C_B_IMM(ctx->opcode)); 1609 + break; 1610 + } 1611 + } 1612 + 1613 + static void decode_RV32_64C2(CPURISCVState *env, DisasContext *ctx) 1614 + { 1615 + uint8_t rd, rs2; 1616 + uint8_t funct3 = extract32(ctx->opcode, 13, 3); 1617 + 1618 + 1619 + rd = GET_RD(ctx->opcode); 1620 + 1621 + switch (funct3) { 1622 + case 0: /* C.SLLI -> slli rd, rd, shamt[5:0] 1623 + C.SLLI64 -> */ 1624 + gen_arith_imm(ctx, OPC_RISC_SLLI, rd, rd, GET_C_ZIMM(ctx->opcode)); 1625 + break; 1626 + case 1: /* C.FLDSP(RV32/64DC) -> fld rd, offset[8:3](x2) */ 1627 + gen_fp_load(ctx, OPC_RISC_FLD, rd, 2, GET_C_LDSP_IMM(ctx->opcode)); 1628 + break; 1629 + case 2: /* C.LWSP -> lw rd, offset[7:2](x2) */ 1630 + gen_load(ctx, OPC_RISC_LW, rd, 2, GET_C_LWSP_IMM(ctx->opcode)); 1631 + break; 1632 + case 3: 1633 + #if defined(TARGET_RISCV64) 1634 + /* C.LDSP(RVC64) -> ld rd, offset[8:3](x2) */ 1635 + gen_load(ctx, OPC_RISC_LD, rd, 2, GET_C_LDSP_IMM(ctx->opcode)); 1636 + #else 1637 + /* C.FLWSP(RV32FC) -> flw rd, offset[7:2](x2) */ 1638 + gen_fp_load(ctx, OPC_RISC_FLW, rd, 2, GET_C_LWSP_IMM(ctx->opcode)); 1639 + #endif 1640 + break; 1641 + case 4: 1642 + rs2 = GET_C_RS2(ctx->opcode); 1643 + 1644 + if (extract32(ctx->opcode, 12, 1) == 0) { 1645 + if (rs2 == 0) { 1646 + /* C.JR -> jalr x0, rs1, 0*/ 1647 + gen_jalr(env, ctx, OPC_RISC_JALR, 0, rd, 0); 1648 + } else { 1649 + /* C.MV -> add rd, x0, rs2 */ 1650 + gen_arith(ctx, OPC_RISC_ADD, rd, 0, rs2); 1651 + } 1652 + } else { 1653 + if (rd == 0) { 1654 + /* C.EBREAK -> ebreak*/ 1655 + gen_system(env, ctx, OPC_RISC_ECALL, 0, 0, 0x1); 1656 + } else { 1657 + if (rs2 == 0) { 1658 + /* C.JALR -> jalr x1, rs1, 0*/ 1659 + gen_jalr(env, ctx, OPC_RISC_JALR, 1, rd, 0); 1660 + } else { 1661 + /* C.ADD -> add rd, rd, rs2 */ 1662 + gen_arith(ctx, OPC_RISC_ADD, rd, rd, rs2); 1663 + } 1664 + } 1665 + } 1666 + break; 1667 + case 5: 1668 + /* C.FSDSP -> fsd rs2, offset[8:3](x2)*/ 1669 + gen_fp_store(ctx, OPC_RISC_FSD, 2, GET_C_RS2(ctx->opcode), 1670 + GET_C_SDSP_IMM(ctx->opcode)); 1671 + /* C.SQSP */ 1672 + break; 1673 + case 6: /* C.SWSP -> sw rs2, offset[7:2](x2)*/ 1674 + gen_store(ctx, OPC_RISC_SW, 2, GET_C_RS2(ctx->opcode), 1675 + GET_C_SWSP_IMM(ctx->opcode)); 1676 + break; 1677 + case 7: 1678 + #if defined(TARGET_RISCV64) 1679 + /* C.SDSP(Rv64/128) -> sd rs2, offset[8:3](x2)*/ 1680 + gen_store(ctx, OPC_RISC_SD, 2, GET_C_RS2(ctx->opcode), 1681 + GET_C_SDSP_IMM(ctx->opcode)); 1682 + #else 1683 + /* C.FSWSP(RV32) -> fsw rs2, offset[7:2](x2) */ 1684 + gen_fp_store(ctx, OPC_RISC_FSW, 2, GET_C_RS2(ctx->opcode), 1685 + GET_C_SWSP_IMM(ctx->opcode)); 1686 + #endif 1687 + break; 1688 + } 1689 + } 1690 + 1691 + static void decode_RV32_64C(CPURISCVState *env, DisasContext *ctx) 1692 + { 1693 + uint8_t op = extract32(ctx->opcode, 0, 2); 1694 + 1695 + switch (op) { 1696 + case 0: 1697 + decode_RV32_64C0(ctx); 1698 + break; 1699 + case 1: 1700 + decode_RV32_64C1(env, ctx); 1701 + break; 1702 + case 2: 1703 + decode_RV32_64C2(env, ctx); 1704 + break; 1705 + } 1706 + } 1707 + 1708 + static void decode_RV32_64G(CPURISCVState *env, DisasContext *ctx) 1709 + { 1710 + int rs1; 1711 + int rs2; 1712 + int rd; 1713 + uint32_t op; 1714 + target_long imm; 1715 + 1716 + /* We do not do misaligned address check here: the address should never be 1717 + * misaligned at this point. Instructions that set PC must do the check, 1718 + * since epc must be the address of the instruction that caused us to 1719 + * perform the misaligned instruction fetch */ 1720 + 1721 + op = MASK_OP_MAJOR(ctx->opcode); 1722 + rs1 = GET_RS1(ctx->opcode); 1723 + rs2 = GET_RS2(ctx->opcode); 1724 + rd = GET_RD(ctx->opcode); 1725 + imm = GET_IMM(ctx->opcode); 1726 + 1727 + switch (op) { 1728 + case OPC_RISC_LUI: 1729 + if (rd == 0) { 1730 + break; /* NOP */ 1731 + } 1732 + tcg_gen_movi_tl(cpu_gpr[rd], sextract64(ctx->opcode, 12, 20) << 12); 1733 + break; 1734 + case OPC_RISC_AUIPC: 1735 + if (rd == 0) { 1736 + break; /* NOP */ 1737 + } 1738 + tcg_gen_movi_tl(cpu_gpr[rd], (sextract64(ctx->opcode, 12, 20) << 12) + 1739 + ctx->pc); 1740 + break; 1741 + case OPC_RISC_JAL: 1742 + imm = GET_JAL_IMM(ctx->opcode); 1743 + gen_jal(env, ctx, rd, imm); 1744 + break; 1745 + case OPC_RISC_JALR: 1746 + gen_jalr(env, ctx, MASK_OP_JALR(ctx->opcode), rd, rs1, imm); 1747 + break; 1748 + case OPC_RISC_BRANCH: 1749 + gen_branch(env, ctx, MASK_OP_BRANCH(ctx->opcode), rs1, rs2, 1750 + GET_B_IMM(ctx->opcode)); 1751 + break; 1752 + case OPC_RISC_LOAD: 1753 + gen_load(ctx, MASK_OP_LOAD(ctx->opcode), rd, rs1, imm); 1754 + break; 1755 + case OPC_RISC_STORE: 1756 + gen_store(ctx, MASK_OP_STORE(ctx->opcode), rs1, rs2, 1757 + GET_STORE_IMM(ctx->opcode)); 1758 + break; 1759 + case OPC_RISC_ARITH_IMM: 1760 + #if defined(TARGET_RISCV64) 1761 + case OPC_RISC_ARITH_IMM_W: 1762 + #endif 1763 + if (rd == 0) { 1764 + break; /* NOP */ 1765 + } 1766 + gen_arith_imm(ctx, MASK_OP_ARITH_IMM(ctx->opcode), rd, rs1, imm); 1767 + break; 1768 + case OPC_RISC_ARITH: 1769 + #if defined(TARGET_RISCV64) 1770 + case OPC_RISC_ARITH_W: 1771 + #endif 1772 + if (rd == 0) { 1773 + break; /* NOP */ 1774 + } 1775 + gen_arith(ctx, MASK_OP_ARITH(ctx->opcode), rd, rs1, rs2); 1776 + break; 1777 + case OPC_RISC_FP_LOAD: 1778 + gen_fp_load(ctx, MASK_OP_FP_LOAD(ctx->opcode), rd, rs1, imm); 1779 + break; 1780 + case OPC_RISC_FP_STORE: 1781 + gen_fp_store(ctx, MASK_OP_FP_STORE(ctx->opcode), rs1, rs2, 1782 + GET_STORE_IMM(ctx->opcode)); 1783 + break; 1784 + case OPC_RISC_ATOMIC: 1785 + gen_atomic(ctx, MASK_OP_ATOMIC(ctx->opcode), rd, rs1, rs2); 1786 + break; 1787 + case OPC_RISC_FMADD: 1788 + gen_fp_fmadd(ctx, MASK_OP_FP_FMADD(ctx->opcode), rd, rs1, rs2, 1789 + GET_RS3(ctx->opcode), GET_RM(ctx->opcode)); 1790 + break; 1791 + case OPC_RISC_FMSUB: 1792 + gen_fp_fmsub(ctx, MASK_OP_FP_FMSUB(ctx->opcode), rd, rs1, rs2, 1793 + GET_RS3(ctx->opcode), GET_RM(ctx->opcode)); 1794 + break; 1795 + case OPC_RISC_FNMSUB: 1796 + gen_fp_fnmsub(ctx, MASK_OP_FP_FNMSUB(ctx->opcode), rd, rs1, rs2, 1797 + GET_RS3(ctx->opcode), GET_RM(ctx->opcode)); 1798 + break; 1799 + case OPC_RISC_FNMADD: 1800 + gen_fp_fnmadd(ctx, MASK_OP_FP_FNMADD(ctx->opcode), rd, rs1, rs2, 1801 + GET_RS3(ctx->opcode), GET_RM(ctx->opcode)); 1802 + break; 1803 + case OPC_RISC_FP_ARITH: 1804 + gen_fp_arith(ctx, MASK_OP_FP_ARITH(ctx->opcode), rd, rs1, rs2, 1805 + GET_RM(ctx->opcode)); 1806 + break; 1807 + case OPC_RISC_FENCE: 1808 + #ifndef CONFIG_USER_ONLY 1809 + if (ctx->opcode & 0x1000) { 1810 + /* FENCE_I is a no-op in QEMU, 1811 + * however we need to end the translation block */ 1812 + tcg_gen_movi_tl(cpu_pc, ctx->next_pc); 1813 + tcg_gen_exit_tb(0); 1814 + ctx->bstate = BS_BRANCH; 1815 + } else { 1816 + /* FENCE is a full memory barrier. */ 1817 + tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC); 1818 + } 1819 + #endif 1820 + break; 1821 + case OPC_RISC_SYSTEM: 1822 + gen_system(env, ctx, MASK_OP_SYSTEM(ctx->opcode), rd, rs1, 1823 + (ctx->opcode & 0xFFF00000) >> 20); 1824 + break; 1825 + default: 1826 + gen_exception_illegal(ctx); 1827 + break; 1828 + } 1829 + } 1830 + 1831 + static void decode_opc(CPURISCVState *env, DisasContext *ctx) 1832 + { 1833 + /* check for compressed insn */ 1834 + if (extract32(ctx->opcode, 0, 2) != 3) { 1835 + if (!riscv_has_ext(env, RVC)) { 1836 + gen_exception_illegal(ctx); 1837 + } else { 1838 + ctx->next_pc = ctx->pc + 2; 1839 + decode_RV32_64C(env, ctx); 1840 + } 1841 + } else { 1842 + ctx->next_pc = ctx->pc + 4; 1843 + decode_RV32_64G(env, ctx); 1844 + } 1845 + } 1846 + 1847 + void gen_intermediate_code(CPUState *cs, TranslationBlock *tb) 1848 + { 1849 + CPURISCVState *env = cs->env_ptr; 1850 + DisasContext ctx; 1851 + target_ulong pc_start; 1852 + target_ulong next_page_start; 1853 + int num_insns; 1854 + int max_insns; 1855 + pc_start = tb->pc; 1856 + next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE; 1857 + ctx.pc = pc_start; 1858 + 1859 + /* once we have GDB, the rest of the translate.c implementation should be 1860 + ready for singlestep */ 1861 + ctx.singlestep_enabled = cs->singlestep_enabled; 1862 + 1863 + ctx.tb = tb; 1864 + ctx.bstate = BS_NONE; 1865 + ctx.flags = tb->flags; 1866 + ctx.mem_idx = tb->flags & TB_FLAGS_MMU_MASK; 1867 + ctx.frm = -1; /* unknown rounding mode */ 1868 + 1869 + num_insns = 0; 1870 + max_insns = tb->cflags & CF_COUNT_MASK; 1871 + if (max_insns == 0) { 1872 + max_insns = CF_COUNT_MASK; 1873 + } 1874 + if (max_insns > TCG_MAX_INSNS) { 1875 + max_insns = TCG_MAX_INSNS; 1876 + } 1877 + gen_tb_start(tb); 1878 + 1879 + while (ctx.bstate == BS_NONE) { 1880 + tcg_gen_insn_start(ctx.pc); 1881 + num_insns++; 1882 + 1883 + if (unlikely(cpu_breakpoint_test(cs, ctx.pc, BP_ANY))) { 1884 + tcg_gen_movi_tl(cpu_pc, ctx.pc); 1885 + ctx.bstate = BS_BRANCH; 1886 + gen_exception_debug(); 1887 + /* The address covered by the breakpoint must be included in 1888 + [tb->pc, tb->pc + tb->size) in order to for it to be 1889 + properly cleared -- thus we increment the PC here so that 1890 + the logic setting tb->size below does the right thing. */ 1891 + ctx.pc += 4; 1892 + goto done_generating; 1893 + } 1894 + 1895 + if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) { 1896 + gen_io_start(); 1897 + } 1898 + 1899 + ctx.opcode = cpu_ldl_code(env, ctx.pc); 1900 + decode_opc(env, &ctx); 1901 + ctx.pc = ctx.next_pc; 1902 + 1903 + if (cs->singlestep_enabled) { 1904 + break; 1905 + } 1906 + if (ctx.pc >= next_page_start) { 1907 + break; 1908 + } 1909 + if (tcg_op_buf_full()) { 1910 + break; 1911 + } 1912 + if (num_insns >= max_insns) { 1913 + break; 1914 + } 1915 + if (singlestep) { 1916 + break; 1917 + } 1918 + 1919 + } 1920 + if (tb->cflags & CF_LAST_IO) { 1921 + gen_io_end(); 1922 + } 1923 + switch (ctx.bstate) { 1924 + case BS_STOP: 1925 + gen_goto_tb(&ctx, 0, ctx.pc); 1926 + break; 1927 + case BS_NONE: /* handle end of page - DO NOT CHAIN. See gen_goto_tb. */ 1928 + tcg_gen_movi_tl(cpu_pc, ctx.pc); 1929 + if (cs->singlestep_enabled) { 1930 + gen_exception_debug(); 1931 + } else { 1932 + tcg_gen_exit_tb(0); 1933 + } 1934 + break; 1935 + case BS_BRANCH: /* ops using BS_BRANCH generate own exit seq */ 1936 + default: 1937 + break; 1938 + } 1939 + done_generating: 1940 + gen_tb_end(tb, num_insns); 1941 + tb->size = ctx.pc - pc_start; 1942 + tb->icount = num_insns; 1943 + 1944 + #ifdef DEBUG_DISAS 1945 + if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) 1946 + && qemu_log_in_addr_range(pc_start)) { 1947 + qemu_log("IN: %s\n", lookup_symbol(pc_start)); 1948 + log_target_disas(cs, pc_start, ctx.pc - pc_start); 1949 + qemu_log("\n"); 1950 + } 1951 + #endif 1952 + } 1953 + 1954 + void riscv_translate_init(void) 1955 + { 1956 + int i; 1957 + 1958 + /* cpu_gpr[0] is a placeholder for the zero register. Do not use it. */ 1959 + /* Use the gen_set_gpr and gen_get_gpr helper functions when accessing */ 1960 + /* registers, unless you specifically block reads/writes to reg 0 */ 1961 + cpu_gpr[0] = NULL; 1962 + 1963 + for (i = 1; i < 32; i++) { 1964 + cpu_gpr[i] = tcg_global_mem_new(cpu_env, 1965 + offsetof(CPURISCVState, gpr[i]), riscv_int_regnames[i]); 1966 + } 1967 + 1968 + for (i = 0; i < 32; i++) { 1969 + cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env, 1970 + offsetof(CPURISCVState, fpr[i]), riscv_fpr_regnames[i]); 1971 + } 1972 + 1973 + cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, pc), "pc"); 1974 + load_res = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_res), 1975 + "load_res"); 1976 + load_val = tcg_global_mem_new(cpu_env, offsetof(CPURISCVState, load_val), 1977 + "load_val"); 1978 + }