Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * LoongArch emulation for QEMU - main translation routines. |
| 4 | * |
| 5 | * Copyright (c) 2021 Loongson Technology Corporation Limited |
| 6 | */ |
| 7 | |
| 8 | #include "qemu/osdep.h" |
| 9 | #include "cpu.h" |
| 10 | #include "tcg/tcg-op.h" |
| 11 | #include "exec/translator.h" |
| 12 | #include "exec/helper-proto.h" |
| 13 | #include "exec/helper-gen.h" |
| 14 | |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 15 | #include "exec/log.h" |
| 16 | #include "qemu/qemu-print.h" |
Song Gao | d578ca6 | 2022-06-06 20:43:00 +0800 | [diff] [blame] | 17 | #include "fpu/softfloat.h" |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 18 | #include "translate.h" |
| 19 | #include "internals.h" |
| 20 | |
| 21 | /* Global register indices */ |
| 22 | TCGv cpu_gpr[32], cpu_pc; |
| 23 | static TCGv cpu_lladdr, cpu_llval; |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 24 | TCGv_i64 cpu_fpr[32]; |
| 25 | |
Xiaojuan Yang | f9bf507 | 2022-06-06 20:43:19 +0800 | [diff] [blame] | 26 | #include "exec/gen-icount.h" |
| 27 | |
Xiaojuan Yang | 5b1dedf | 2022-06-06 20:43:15 +0800 | [diff] [blame] | 28 | #define DISAS_STOP DISAS_TARGET_0 |
| 29 | #define DISAS_EXIT DISAS_TARGET_1 |
| 30 | #define DISAS_EXIT_UPDATE DISAS_TARGET_2 |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 31 | |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 32 | static inline int plus_1(DisasContext *ctx, int x) |
| 33 | { |
| 34 | return x + 1; |
| 35 | } |
| 36 | |
Song Gao | bb79174 | 2022-06-06 20:42:57 +0800 | [diff] [blame] | 37 | static inline int shl_2(DisasContext *ctx, int x) |
| 38 | { |
| 39 | return x << 2; |
| 40 | } |
| 41 | |
Song Gao | d578ca6 | 2022-06-06 20:43:00 +0800 | [diff] [blame] | 42 | /* |
| 43 | * LoongArch the upper 32 bits are undefined ("can be any value"). |
| 44 | * QEMU chooses to nanbox, because it is most likely to show guest bugs early. |
| 45 | */ |
| 46 | static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in) |
| 47 | { |
| 48 | tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32)); |
| 49 | } |
| 50 | |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 51 | void generate_exception(DisasContext *ctx, int excp) |
| 52 | { |
| 53 | tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next); |
| 54 | gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp)); |
| 55 | ctx->base.is_jmp = DISAS_NORETURN; |
| 56 | } |
| 57 | |
| 58 | static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest) |
| 59 | { |
| 60 | if (translator_use_goto_tb(&ctx->base, dest)) { |
| 61 | tcg_gen_goto_tb(n); |
| 62 | tcg_gen_movi_tl(cpu_pc, dest); |
| 63 | tcg_gen_exit_tb(ctx->base.tb, n); |
| 64 | } else { |
| 65 | tcg_gen_movi_tl(cpu_pc, dest); |
| 66 | tcg_gen_lookup_and_goto_ptr(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void loongarch_tr_init_disas_context(DisasContextBase *dcbase, |
| 71 | CPUState *cs) |
| 72 | { |
| 73 | int64_t bound; |
| 74 | DisasContext *ctx = container_of(dcbase, DisasContext, base); |
| 75 | |
| 76 | ctx->page_start = ctx->base.pc_first & TARGET_PAGE_MASK; |
Rui Wang | c8885b8 | 2022-11-07 10:45:25 +0800 | [diff] [blame] | 77 | ctx->plv = ctx->base.tb->flags & HW_FLAGS_PLV_MASK; |
Rui Wang | b4bda20 | 2022-11-04 12:05:16 +0800 | [diff] [blame] | 78 | if (ctx->base.tb->flags & HW_FLAGS_CRMD_PG) { |
Rui Wang | c8885b8 | 2022-11-07 10:45:25 +0800 | [diff] [blame] | 79 | ctx->mem_idx = ctx->plv; |
Rui Wang | b4bda20 | 2022-11-04 12:05:16 +0800 | [diff] [blame] | 80 | } else { |
Rui Wang | c8885b8 | 2022-11-07 10:45:25 +0800 | [diff] [blame] | 81 | ctx->mem_idx = MMU_IDX_DA; |
Rui Wang | b4bda20 | 2022-11-04 12:05:16 +0800 | [diff] [blame] | 82 | } |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 83 | |
| 84 | /* Bound the number of insns to execute to those left on the page. */ |
| 85 | bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4; |
| 86 | ctx->base.max_insns = MIN(ctx->base.max_insns, bound); |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 87 | |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 88 | ctx->zero = tcg_constant_tl(0); |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void loongarch_tr_tb_start(DisasContextBase *dcbase, CPUState *cs) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | static void loongarch_tr_insn_start(DisasContextBase *dcbase, CPUState *cs) |
| 96 | { |
| 97 | DisasContext *ctx = container_of(dcbase, DisasContext, base); |
| 98 | |
| 99 | tcg_gen_insn_start(ctx->base.pc_next); |
| 100 | } |
| 101 | |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 102 | /* |
| 103 | * Wrappers for getting reg values. |
| 104 | * |
| 105 | * The $zero register does not have cpu_gpr[0] allocated -- we supply the |
| 106 | * constant zero as a source, and an uninitialized sink as destination. |
| 107 | * |
| 108 | * Further, we may provide an extension for word operations. |
| 109 | */ |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 110 | static TCGv gpr_src(DisasContext *ctx, int reg_num, DisasExtend src_ext) |
| 111 | { |
| 112 | TCGv t; |
| 113 | |
| 114 | if (reg_num == 0) { |
| 115 | return ctx->zero; |
| 116 | } |
| 117 | |
| 118 | switch (src_ext) { |
| 119 | case EXT_NONE: |
| 120 | return cpu_gpr[reg_num]; |
| 121 | case EXT_SIGN: |
Richard Henderson | 60a7e25 | 2023-02-24 19:05:39 -1000 | [diff] [blame] | 122 | t = tcg_temp_new(); |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 123 | tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]); |
| 124 | return t; |
| 125 | case EXT_ZERO: |
Richard Henderson | 60a7e25 | 2023-02-24 19:05:39 -1000 | [diff] [blame] | 126 | t = tcg_temp_new(); |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 127 | tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]); |
| 128 | return t; |
| 129 | } |
| 130 | g_assert_not_reached(); |
| 131 | } |
| 132 | |
| 133 | static TCGv gpr_dst(DisasContext *ctx, int reg_num, DisasExtend dst_ext) |
| 134 | { |
| 135 | if (reg_num == 0 || dst_ext) { |
Richard Henderson | 60a7e25 | 2023-02-24 19:05:39 -1000 | [diff] [blame] | 136 | return tcg_temp_new(); |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 137 | } |
| 138 | return cpu_gpr[reg_num]; |
| 139 | } |
| 140 | |
| 141 | static void gen_set_gpr(int reg_num, TCGv t, DisasExtend dst_ext) |
| 142 | { |
| 143 | if (reg_num != 0) { |
| 144 | switch (dst_ext) { |
| 145 | case EXT_NONE: |
| 146 | tcg_gen_mov_tl(cpu_gpr[reg_num], t); |
| 147 | break; |
| 148 | case EXT_SIGN: |
| 149 | tcg_gen_ext32s_tl(cpu_gpr[reg_num], t); |
| 150 | break; |
| 151 | case EXT_ZERO: |
| 152 | tcg_gen_ext32u_tl(cpu_gpr[reg_num], t); |
| 153 | break; |
| 154 | default: |
| 155 | g_assert_not_reached(); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #include "decode-insns.c.inc" |
| 161 | #include "insn_trans/trans_arith.c.inc" |
Song Gao | 63cfcd4 | 2022-06-06 20:42:55 +0800 | [diff] [blame] | 162 | #include "insn_trans/trans_shift.c.inc" |
Song Gao | ad08cb3 | 2022-06-06 20:42:56 +0800 | [diff] [blame] | 163 | #include "insn_trans/trans_bit.c.inc" |
Song Gao | bb79174 | 2022-06-06 20:42:57 +0800 | [diff] [blame] | 164 | #include "insn_trans/trans_memory.c.inc" |
Song Gao | 94b02d5 | 2022-06-06 20:42:58 +0800 | [diff] [blame] | 165 | #include "insn_trans/trans_atomic.c.inc" |
Song Gao | 8708a04 | 2022-06-06 20:42:59 +0800 | [diff] [blame] | 166 | #include "insn_trans/trans_extra.c.inc" |
Song Gao | d578ca6 | 2022-06-06 20:43:00 +0800 | [diff] [blame] | 167 | #include "insn_trans/trans_farith.c.inc" |
Song Gao | 9b74107 | 2022-06-06 20:43:01 +0800 | [diff] [blame] | 168 | #include "insn_trans/trans_fcmp.c.inc" |
Song Gao | 7c1f887 | 2022-06-06 20:43:02 +0800 | [diff] [blame] | 169 | #include "insn_trans/trans_fcnv.c.inc" |
Song Gao | b7dabd5 | 2022-06-06 20:43:03 +0800 | [diff] [blame] | 170 | #include "insn_trans/trans_fmov.c.inc" |
Song Gao | e616bdf | 2022-06-06 20:43:04 +0800 | [diff] [blame] | 171 | #include "insn_trans/trans_fmemory.c.inc" |
Song Gao | ee86bd5 | 2022-06-06 20:43:05 +0800 | [diff] [blame] | 172 | #include "insn_trans/trans_branch.c.inc" |
Xiaojuan Yang | 5b1dedf | 2022-06-06 20:43:15 +0800 | [diff] [blame] | 173 | #include "insn_trans/trans_privileged.c.inc" |
Song Gao | 143d678 | 2022-06-06 20:42:54 +0800 | [diff] [blame] | 174 | |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 175 | static void loongarch_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs) |
| 176 | { |
| 177 | CPULoongArchState *env = cs->env_ptr; |
| 178 | DisasContext *ctx = container_of(dcbase, DisasContext, base); |
| 179 | |
| 180 | ctx->opcode = cpu_ldl_code(env, ctx->base.pc_next); |
| 181 | |
| 182 | if (!decode(ctx, ctx->opcode)) { |
| 183 | qemu_log_mask(LOG_UNIMP, "Error: unknown opcode. " |
| 184 | TARGET_FMT_lx ": 0x%x\n", |
| 185 | ctx->base.pc_next, ctx->opcode); |
| 186 | generate_exception(ctx, EXCCODE_INE); |
| 187 | } |
| 188 | |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 189 | ctx->base.pc_next += 4; |
| 190 | } |
| 191 | |
| 192 | static void loongarch_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs) |
| 193 | { |
| 194 | DisasContext *ctx = container_of(dcbase, DisasContext, base); |
| 195 | |
| 196 | switch (ctx->base.is_jmp) { |
| 197 | case DISAS_STOP: |
| 198 | tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next); |
| 199 | tcg_gen_lookup_and_goto_ptr(); |
| 200 | break; |
| 201 | case DISAS_TOO_MANY: |
| 202 | gen_goto_tb(ctx, 0, ctx->base.pc_next); |
| 203 | break; |
| 204 | case DISAS_NORETURN: |
| 205 | break; |
Xiaojuan Yang | 5b1dedf | 2022-06-06 20:43:15 +0800 | [diff] [blame] | 206 | case DISAS_EXIT_UPDATE: |
| 207 | tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next); |
| 208 | QEMU_FALLTHROUGH; |
| 209 | case DISAS_EXIT: |
| 210 | tcg_gen_exit_tb(NULL, 0); |
| 211 | break; |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 212 | default: |
| 213 | g_assert_not_reached(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void loongarch_tr_disas_log(const DisasContextBase *dcbase, |
| 218 | CPUState *cpu, FILE *logfile) |
| 219 | { |
| 220 | qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first)); |
| 221 | target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size); |
| 222 | } |
| 223 | |
| 224 | static const TranslatorOps loongarch_tr_ops = { |
| 225 | .init_disas_context = loongarch_tr_init_disas_context, |
| 226 | .tb_start = loongarch_tr_tb_start, |
| 227 | .insn_start = loongarch_tr_insn_start, |
| 228 | .translate_insn = loongarch_tr_translate_insn, |
| 229 | .tb_stop = loongarch_tr_tb_stop, |
| 230 | .disas_log = loongarch_tr_disas_log, |
| 231 | }; |
| 232 | |
Richard Henderson | 597f9b2 | 2023-01-28 15:19:22 -1000 | [diff] [blame] | 233 | void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns, |
Richard Henderson | 306c872 | 2022-08-11 13:48:03 -0700 | [diff] [blame] | 234 | target_ulong pc, void *host_pc) |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 235 | { |
| 236 | DisasContext ctx; |
| 237 | |
Richard Henderson | 306c872 | 2022-08-11 13:48:03 -0700 | [diff] [blame] | 238 | translator_loop(cs, tb, max_insns, pc, host_pc, |
| 239 | &loongarch_tr_ops, &ctx.base); |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void loongarch_translate_init(void) |
| 243 | { |
| 244 | int i; |
| 245 | |
| 246 | cpu_gpr[0] = NULL; |
| 247 | for (i = 1; i < 32; i++) { |
| 248 | cpu_gpr[i] = tcg_global_mem_new(cpu_env, |
| 249 | offsetof(CPULoongArchState, gpr[i]), |
| 250 | regnames[i]); |
| 251 | } |
| 252 | |
| 253 | for (i = 0; i < 32; i++) { |
| 254 | int off = offsetof(CPULoongArchState, fpr[i]); |
| 255 | cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env, off, fregnames[i]); |
| 256 | } |
| 257 | |
| 258 | cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPULoongArchState, pc), "pc"); |
Song Gao | f8da88d | 2022-06-06 20:42:53 +0800 | [diff] [blame] | 259 | cpu_lladdr = tcg_global_mem_new(cpu_env, |
| 260 | offsetof(CPULoongArchState, lladdr), "lladdr"); |
| 261 | cpu_llval = tcg_global_mem_new(cpu_env, |
| 262 | offsetof(CPULoongArchState, llval), "llval"); |
| 263 | } |