1f476f177SLIU Zhiwei /* 2f476f177SLIU Zhiwei * QEMU RISC-V CPU -- internal functions and types 3f476f177SLIU Zhiwei * 4f476f177SLIU Zhiwei * Copyright (c) 2020 T-Head Semiconductor Co., Ltd. All rights reserved. 5f476f177SLIU Zhiwei * 6f476f177SLIU Zhiwei * This program is free software; you can redistribute it and/or modify it 7f476f177SLIU Zhiwei * under the terms and conditions of the GNU General Public License, 8f476f177SLIU Zhiwei * version 2 or later, as published by the Free Software Foundation. 9f476f177SLIU Zhiwei * 10f476f177SLIU Zhiwei * This program is distributed in the hope it will be useful, but WITHOUT 11f476f177SLIU Zhiwei * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12f476f177SLIU Zhiwei * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13f476f177SLIU Zhiwei * more details. 14f476f177SLIU Zhiwei * 15f476f177SLIU Zhiwei * You should have received a copy of the GNU General Public License along with 16f476f177SLIU Zhiwei * this program. If not, see <http://www.gnu.org/licenses/>. 17f476f177SLIU Zhiwei */ 18f476f177SLIU Zhiwei 19f476f177SLIU Zhiwei #ifndef RISCV_CPU_INTERNALS_H 20f476f177SLIU Zhiwei #define RISCV_CPU_INTERNALS_H 21f476f177SLIU Zhiwei 22fcea54c2SPhilippe Mathieu-Daudé #include "exec/cpu-common.h" 23f476f177SLIU Zhiwei #include "hw/registerfields.h" 24fcea54c2SPhilippe Mathieu-Daudé #include "fpu/softfloat-types.h" 25fcea54c2SPhilippe Mathieu-Daudé #include "target/riscv/cpu_bits.h" 26f476f177SLIU Zhiwei 27c8f8a995SFei Wu /* 28c8f8a995SFei Wu * The current MMU Modes are: 29c8f8a995SFei Wu * - U 0b000 30c8f8a995SFei Wu * - S 0b001 31c8f8a995SFei Wu * - S+SUM 0b010 32c8f8a995SFei Wu * - M 0b011 333df44173SRichard Henderson * - U+2STAGE 0b100 343df44173SRichard Henderson * - S+2STAGE 0b101 353df44173SRichard Henderson * - S+SUM+2STAGE 0b110 36669b4867SDeepak Gupta * - Shadow stack+U 0b1000 37669b4867SDeepak Gupta * - Shadow stack+S 0b1001 38c8f8a995SFei Wu */ 39c8f8a995SFei Wu #define MMUIdx_U 0 40c8f8a995SFei Wu #define MMUIdx_S 1 41c8f8a995SFei Wu #define MMUIdx_S_SUM 2 42c8f8a995SFei Wu #define MMUIdx_M 3 433df44173SRichard Henderson #define MMU_2STAGE_BIT (1 << 2) 44669b4867SDeepak Gupta #define MMU_IDX_SS_WRITE (1 << 3) 45c8f8a995SFei Wu 46340b5805SRichard Henderson static inline int mmuidx_priv(int mmu_idx) 47340b5805SRichard Henderson { 48340b5805SRichard Henderson int ret = mmu_idx & 3; 49340b5805SRichard Henderson if (ret == MMUIdx_S_SUM) { 50340b5805SRichard Henderson ret = PRV_S; 51340b5805SRichard Henderson } 52340b5805SRichard Henderson return ret; 53340b5805SRichard Henderson } 54340b5805SRichard Henderson 554005a799SRichard Henderson static inline bool mmuidx_sum(int mmu_idx) 564005a799SRichard Henderson { 574005a799SRichard Henderson return (mmu_idx & 3) == MMUIdx_S_SUM; 584005a799SRichard Henderson } 594005a799SRichard Henderson 6002369f79SRichard Henderson static inline bool mmuidx_2stage(int mmu_idx) 6102369f79SRichard Henderson { 6202369f79SRichard Henderson return mmu_idx & MMU_2STAGE_BIT; 6302369f79SRichard Henderson } 6402369f79SRichard Henderson 65751538d5SLIU Zhiwei /* share data between vector helpers and decode code */ 66f9298de5SFrank Chang FIELD(VDATA, VM, 0, 1) 67f9298de5SFrank Chang FIELD(VDATA, LMUL, 1, 3) 68f1eed927SeopXD FIELD(VDATA, VTA, 4, 1) 695c19fc15SeopXD FIELD(VDATA, VTA_ALL_1S, 5, 1) 70355d5584SYueh-Ting (eop) Chen FIELD(VDATA, VMA, 6, 1) 71355d5584SYueh-Ting (eop) Chen FIELD(VDATA, NF, 7, 4) 72355d5584SYueh-Ting (eop) Chen FIELD(VDATA, WD, 7, 1) 73121ddbb3SLIU Zhiwei 74121ddbb3SLIU Zhiwei /* float point classify helpers */ 75121ddbb3SLIU Zhiwei target_ulong fclass_h(uint64_t frs1); 76121ddbb3SLIU Zhiwei target_ulong fclass_s(uint64_t frs1); 77121ddbb3SLIU Zhiwei target_ulong fclass_d(uint64_t frs1); 789fc08be6SLIU Zhiwei 79f7697f0eSYifei Jiang #ifndef CONFIG_USER_ONLY 80f7697f0eSYifei Jiang extern const VMStateDescription vmstate_riscv_cpu; 81f7697f0eSYifei Jiang #endif 82f7697f0eSYifei Jiang 83986c895dSFrank Chang enum { 84986c895dSFrank Chang RISCV_FRM_RNE = 0, /* Round to Nearest, ties to Even */ 85986c895dSFrank Chang RISCV_FRM_RTZ = 1, /* Round towards Zero */ 86986c895dSFrank Chang RISCV_FRM_RDN = 2, /* Round Down */ 87986c895dSFrank Chang RISCV_FRM_RUP = 3, /* Round Up */ 88986c895dSFrank Chang RISCV_FRM_RMM = 4, /* Round to Nearest, ties to Max Magnitude */ 89986c895dSFrank Chang RISCV_FRM_DYN = 7, /* Dynamic rounding mode */ 9075804f71SFrank Chang RISCV_FRM_ROD = 8, /* Round to Odd */ 91986c895dSFrank Chang }; 92986c895dSFrank Chang 93e1a29bbdSWeiwei Li static inline uint64_t nanbox_s(CPURISCVState *env, float32 f) 949921e3d3SRichard Henderson { 95e1a29bbdSWeiwei Li /* the value is sign-extended instead of NaN-boxing for zfinx */ 96edf67fb4SPhilippe Mathieu-Daudé if (env_archcpu(env)->cfg.ext_zfinx) { 97e1a29bbdSWeiwei Li return (int32_t)f; 98e1a29bbdSWeiwei Li } else { 999921e3d3SRichard Henderson return f | MAKE_64BIT_MASK(32, 32); 1009921e3d3SRichard Henderson } 101e1a29bbdSWeiwei Li } 1029921e3d3SRichard Henderson 103e1a29bbdSWeiwei Li static inline float32 check_nanbox_s(CPURISCVState *env, uint64_t f) 10400e925c5SRichard Henderson { 105e1a29bbdSWeiwei Li /* Disable NaN-boxing check when enable zfinx */ 106edf67fb4SPhilippe Mathieu-Daudé if (env_archcpu(env)->cfg.ext_zfinx) { 107e1a29bbdSWeiwei Li return (uint32_t)f; 108e1a29bbdSWeiwei Li } 109e1a29bbdSWeiwei Li 11000e925c5SRichard Henderson uint64_t mask = MAKE_64BIT_MASK(32, 32); 11100e925c5SRichard Henderson 11200e925c5SRichard Henderson if (likely((f & mask) == mask)) { 11300e925c5SRichard Henderson return (uint32_t)f; 11400e925c5SRichard Henderson } else { 11500e925c5SRichard Henderson return 0x7fc00000u; /* default qnan */ 11600e925c5SRichard Henderson } 11700e925c5SRichard Henderson } 11800e925c5SRichard Henderson 119a2464a4cSWeiwei Li static inline uint64_t nanbox_h(CPURISCVState *env, float16 f) 12000c1899fSKito Cheng { 121a2464a4cSWeiwei Li /* the value is sign-extended instead of NaN-boxing for zfinx */ 122edf67fb4SPhilippe Mathieu-Daudé if (env_archcpu(env)->cfg.ext_zfinx) { 123a2464a4cSWeiwei Li return (int16_t)f; 124a2464a4cSWeiwei Li } else { 12500c1899fSKito Cheng return f | MAKE_64BIT_MASK(16, 48); 12600c1899fSKito Cheng } 127a2464a4cSWeiwei Li } 12800c1899fSKito Cheng 129a2464a4cSWeiwei Li static inline float16 check_nanbox_h(CPURISCVState *env, uint64_t f) 13000c1899fSKito Cheng { 131a2464a4cSWeiwei Li /* Disable nanbox check when enable zfinx */ 132edf67fb4SPhilippe Mathieu-Daudé if (env_archcpu(env)->cfg.ext_zfinx) { 133a2464a4cSWeiwei Li return (uint16_t)f; 134a2464a4cSWeiwei Li } 135a2464a4cSWeiwei Li 13600c1899fSKito Cheng uint64_t mask = MAKE_64BIT_MASK(16, 48); 13700c1899fSKito Cheng 13800c1899fSKito Cheng if (likely((f & mask) == mask)) { 13900c1899fSKito Cheng return (uint16_t)f; 14000c1899fSKito Cheng } else { 14100c1899fSKito Cheng return 0x7E00u; /* default qnan */ 14200c1899fSKito Cheng } 14300c1899fSKito Cheng } 14400c1899fSKito Cheng 1454f7b1ecbSPeter Maydell /* Our implementation of CPUClass::has_work */ 1464f7b1ecbSPeter Maydell bool riscv_cpu_has_work(CPUState *cs); 1474f7b1ecbSPeter Maydell 148*4d160093SAlexey Baturo /* Zjpm addr masking routine */ 149*4d160093SAlexey Baturo static inline target_ulong adjust_addr_body(CPURISCVState *env, 150*4d160093SAlexey Baturo target_ulong addr, 151*4d160093SAlexey Baturo bool is_virt_addr) 152*4d160093SAlexey Baturo { 153*4d160093SAlexey Baturo RISCVPmPmm pmm = PMM_FIELD_DISABLED; 154*4d160093SAlexey Baturo uint32_t pmlen = 0; 155*4d160093SAlexey Baturo bool signext = false; 156*4d160093SAlexey Baturo 157*4d160093SAlexey Baturo /* do nothing for rv32 mode */ 158*4d160093SAlexey Baturo if (riscv_cpu_mxl(env) == MXL_RV32) { 159*4d160093SAlexey Baturo return addr; 160*4d160093SAlexey Baturo } 161*4d160093SAlexey Baturo 162*4d160093SAlexey Baturo /* get pmm field depending on whether addr is */ 163*4d160093SAlexey Baturo if (is_virt_addr) { 164*4d160093SAlexey Baturo pmm = riscv_pm_get_virt_pmm(env); 165*4d160093SAlexey Baturo } else { 166*4d160093SAlexey Baturo pmm = riscv_pm_get_pmm(env); 167*4d160093SAlexey Baturo } 168*4d160093SAlexey Baturo 169*4d160093SAlexey Baturo /* if pointer masking is disabled, return original addr */ 170*4d160093SAlexey Baturo if (pmm == PMM_FIELD_DISABLED) { 171*4d160093SAlexey Baturo return addr; 172*4d160093SAlexey Baturo } 173*4d160093SAlexey Baturo 174*4d160093SAlexey Baturo if (!is_virt_addr) { 175*4d160093SAlexey Baturo signext = riscv_cpu_virt_mem_enabled(env); 176*4d160093SAlexey Baturo } 177*4d160093SAlexey Baturo addr = addr << pmlen; 178*4d160093SAlexey Baturo pmlen = riscv_pm_get_pmlen(pmm); 179*4d160093SAlexey Baturo 180*4d160093SAlexey Baturo /* sign/zero extend masked address by N-1 bit */ 181*4d160093SAlexey Baturo if (signext) { 182*4d160093SAlexey Baturo addr = (target_long)addr >> pmlen; 183*4d160093SAlexey Baturo } else { 184*4d160093SAlexey Baturo addr = addr >> pmlen; 185*4d160093SAlexey Baturo } 186*4d160093SAlexey Baturo 187*4d160093SAlexey Baturo return addr; 188*4d160093SAlexey Baturo } 189*4d160093SAlexey Baturo 190*4d160093SAlexey Baturo static inline target_ulong adjust_addr(CPURISCVState *env, 191*4d160093SAlexey Baturo target_ulong addr) 192*4d160093SAlexey Baturo { 193*4d160093SAlexey Baturo return adjust_addr_body(env, addr, false); 194*4d160093SAlexey Baturo } 195*4d160093SAlexey Baturo 196*4d160093SAlexey Baturo static inline target_ulong adjust_addr_virt(CPURISCVState *env, 197*4d160093SAlexey Baturo target_ulong addr) 198*4d160093SAlexey Baturo { 199*4d160093SAlexey Baturo return adjust_addr_body(env, addr, true); 200*4d160093SAlexey Baturo } 201*4d160093SAlexey Baturo 202f476f177SLIU Zhiwei #endif 203