18b453a2bSTaylor Simpson /* 2e2be9a5cSTaylor Simpson * Copyright(c) 2019-2022 Qualcomm Innovation Center, Inc. All Rights Reserved. 38b453a2bSTaylor Simpson * 48b453a2bSTaylor Simpson * This program is free software; you can redistribute it and/or modify 58b453a2bSTaylor Simpson * it under the terms of the GNU General Public License as published by 68b453a2bSTaylor Simpson * the Free Software Foundation; either version 2 of the License, or 78b453a2bSTaylor Simpson * (at your option) any later version. 88b453a2bSTaylor Simpson * 98b453a2bSTaylor Simpson * This program is distributed in the hope that it will be useful, 108b453a2bSTaylor Simpson * but WITHOUT ANY WARRANTY; without even the implied warranty of 118b453a2bSTaylor Simpson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128b453a2bSTaylor Simpson * GNU General Public License for more details. 138b453a2bSTaylor Simpson * 148b453a2bSTaylor Simpson * You should have received a copy of the GNU General Public License 158b453a2bSTaylor Simpson * along with this program; if not, see <http://www.gnu.org/licenses/>. 168b453a2bSTaylor Simpson */ 178b453a2bSTaylor Simpson 188b453a2bSTaylor Simpson #define QEMU_GENERATE 198b453a2bSTaylor Simpson #include "qemu/osdep.h" 208b453a2bSTaylor Simpson #include "cpu.h" 218b453a2bSTaylor Simpson #include "tcg/tcg-op.h" 22a82dd548STaylor Simpson #include "tcg/tcg-op-gvec.h" 238b453a2bSTaylor Simpson #include "exec/cpu_ldst.h" 248b453a2bSTaylor Simpson #include "exec/log.h" 258b453a2bSTaylor Simpson #include "internal.h" 268b453a2bSTaylor Simpson #include "attribs.h" 278b453a2bSTaylor Simpson #include "insn.h" 288b453a2bSTaylor Simpson #include "decode.h" 298b453a2bSTaylor Simpson #include "translate.h" 308b453a2bSTaylor Simpson #include "printinsn.h" 318b453a2bSTaylor Simpson 328b453a2bSTaylor Simpson TCGv hex_gpr[TOTAL_PER_THREAD_REGS]; 338b453a2bSTaylor Simpson TCGv hex_pred[NUM_PREGS]; 348b453a2bSTaylor Simpson TCGv hex_next_PC; 358b453a2bSTaylor Simpson TCGv hex_this_PC; 368b453a2bSTaylor Simpson TCGv hex_slot_cancelled; 378b453a2bSTaylor Simpson TCGv hex_branch_taken; 388b453a2bSTaylor Simpson TCGv hex_new_value[TOTAL_PER_THREAD_REGS]; 398b453a2bSTaylor Simpson TCGv hex_reg_written[TOTAL_PER_THREAD_REGS]; 408b453a2bSTaylor Simpson TCGv hex_new_pred_value[NUM_PREGS]; 418b453a2bSTaylor Simpson TCGv hex_pred_written; 428b453a2bSTaylor Simpson TCGv hex_store_addr[STORES_MAX]; 438b453a2bSTaylor Simpson TCGv hex_store_width[STORES_MAX]; 448b453a2bSTaylor Simpson TCGv hex_store_val32[STORES_MAX]; 458b453a2bSTaylor Simpson TCGv_i64 hex_store_val64[STORES_MAX]; 468b453a2bSTaylor Simpson TCGv hex_pkt_has_store_s1; 478b453a2bSTaylor Simpson TCGv hex_dczero_addr; 488b453a2bSTaylor Simpson TCGv hex_llsc_addr; 498b453a2bSTaylor Simpson TCGv hex_llsc_val; 508b453a2bSTaylor Simpson TCGv_i64 hex_llsc_val_i64; 51a82dd548STaylor Simpson TCGv hex_VRegs_updated; 52a82dd548STaylor Simpson TCGv hex_QRegs_updated; 53a82dd548STaylor Simpson TCGv hex_vstore_addr[VSTORES_MAX]; 54a82dd548STaylor Simpson TCGv hex_vstore_size[VSTORES_MAX]; 55a82dd548STaylor Simpson TCGv hex_vstore_pending[VSTORES_MAX]; 568b453a2bSTaylor Simpson 578b453a2bSTaylor Simpson static const char * const hexagon_prednames[] = { 588b453a2bSTaylor Simpson "p0", "p1", "p2", "p3" 598b453a2bSTaylor Simpson }; 608b453a2bSTaylor Simpson 61a82dd548STaylor Simpson intptr_t ctx_future_vreg_off(DisasContext *ctx, int regnum, 62a82dd548STaylor Simpson int num, bool alloc_ok) 63a82dd548STaylor Simpson { 64a82dd548STaylor Simpson intptr_t offset; 65a82dd548STaylor Simpson 66a82dd548STaylor Simpson /* See if it is already allocated */ 67a82dd548STaylor Simpson for (int i = 0; i < ctx->future_vregs_idx; i++) { 68a82dd548STaylor Simpson if (ctx->future_vregs_num[i] == regnum) { 69a82dd548STaylor Simpson return offsetof(CPUHexagonState, future_VRegs[i]); 70a82dd548STaylor Simpson } 71a82dd548STaylor Simpson } 72a82dd548STaylor Simpson 73a82dd548STaylor Simpson g_assert(alloc_ok); 74a82dd548STaylor Simpson offset = offsetof(CPUHexagonState, future_VRegs[ctx->future_vregs_idx]); 75a82dd548STaylor Simpson for (int i = 0; i < num; i++) { 76a82dd548STaylor Simpson ctx->future_vregs_num[ctx->future_vregs_idx + i] = regnum++; 77a82dd548STaylor Simpson } 78a82dd548STaylor Simpson ctx->future_vregs_idx += num; 79a82dd548STaylor Simpson g_assert(ctx->future_vregs_idx <= VECTOR_TEMPS_MAX); 80a82dd548STaylor Simpson return offset; 81a82dd548STaylor Simpson } 82a82dd548STaylor Simpson 83a82dd548STaylor Simpson intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum, 84a82dd548STaylor Simpson int num, bool alloc_ok) 85a82dd548STaylor Simpson { 86a82dd548STaylor Simpson intptr_t offset; 87a82dd548STaylor Simpson 88a82dd548STaylor Simpson /* See if it is already allocated */ 89a82dd548STaylor Simpson for (int i = 0; i < ctx->tmp_vregs_idx; i++) { 90a82dd548STaylor Simpson if (ctx->tmp_vregs_num[i] == regnum) { 91a82dd548STaylor Simpson return offsetof(CPUHexagonState, tmp_VRegs[i]); 92a82dd548STaylor Simpson } 93a82dd548STaylor Simpson } 94a82dd548STaylor Simpson 95a82dd548STaylor Simpson g_assert(alloc_ok); 96a82dd548STaylor Simpson offset = offsetof(CPUHexagonState, tmp_VRegs[ctx->tmp_vregs_idx]); 97a82dd548STaylor Simpson for (int i = 0; i < num; i++) { 98a82dd548STaylor Simpson ctx->tmp_vregs_num[ctx->tmp_vregs_idx + i] = regnum++; 99a82dd548STaylor Simpson } 100a82dd548STaylor Simpson ctx->tmp_vregs_idx += num; 101a82dd548STaylor Simpson g_assert(ctx->tmp_vregs_idx <= VECTOR_TEMPS_MAX); 102a82dd548STaylor Simpson return offset; 103a82dd548STaylor Simpson } 104a82dd548STaylor Simpson 105743debbcSTaylor Simpson static void gen_exception_raw(int excp) 1068b453a2bSTaylor Simpson { 10723803bbeSPhilippe Mathieu-Daudé gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp)); 1088b453a2bSTaylor Simpson } 1098b453a2bSTaylor Simpson 110743debbcSTaylor Simpson static void gen_exec_counters(DisasContext *ctx) 1118b453a2bSTaylor Simpson { 112743debbcSTaylor Simpson tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT], 113743debbcSTaylor Simpson hex_gpr[HEX_REG_QEMU_PKT_CNT], ctx->num_packets); 114743debbcSTaylor Simpson tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_INSN_CNT], 115743debbcSTaylor Simpson hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns); 116a82dd548STaylor Simpson tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT], 117a82dd548STaylor Simpson hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns); 118743debbcSTaylor Simpson } 119743debbcSTaylor Simpson 120743debbcSTaylor Simpson static void gen_end_tb(DisasContext *ctx) 121743debbcSTaylor Simpson { 122743debbcSTaylor Simpson gen_exec_counters(ctx); 123743debbcSTaylor Simpson tcg_gen_mov_tl(hex_gpr[HEX_REG_PC], hex_next_PC); 124743debbcSTaylor Simpson tcg_gen_exit_tb(NULL, 0); 125743debbcSTaylor Simpson ctx->base.is_jmp = DISAS_NORETURN; 126743debbcSTaylor Simpson } 127743debbcSTaylor Simpson 128743debbcSTaylor Simpson static void gen_exception_end_tb(DisasContext *ctx, int excp) 129743debbcSTaylor Simpson { 130743debbcSTaylor Simpson gen_exec_counters(ctx); 131743debbcSTaylor Simpson tcg_gen_mov_tl(hex_gpr[HEX_REG_PC], hex_next_PC); 132743debbcSTaylor Simpson gen_exception_raw(excp); 133743debbcSTaylor Simpson ctx->base.is_jmp = DISAS_NORETURN; 134743debbcSTaylor Simpson 1358b453a2bSTaylor Simpson } 1368b453a2bSTaylor Simpson 1378b453a2bSTaylor Simpson #define PACKET_BUFFER_LEN 1028 1388b453a2bSTaylor Simpson static void print_pkt(Packet *pkt) 1398b453a2bSTaylor Simpson { 1408b453a2bSTaylor Simpson GString *buf = g_string_sized_new(PACKET_BUFFER_LEN); 1418b453a2bSTaylor Simpson snprint_a_pkt_debug(buf, pkt); 1428b453a2bSTaylor Simpson HEX_DEBUG_LOG("%s", buf->str); 1438b453a2bSTaylor Simpson g_string_free(buf, true); 1448b453a2bSTaylor Simpson } 14585580a65STaylor Simpson #define HEX_DEBUG_PRINT_PKT(pkt) \ 14685580a65STaylor Simpson do { \ 14785580a65STaylor Simpson if (HEX_DEBUG) { \ 14885580a65STaylor Simpson print_pkt(pkt); \ 14985580a65STaylor Simpson } \ 15085580a65STaylor Simpson } while (0) 1518b453a2bSTaylor Simpson 1528b453a2bSTaylor Simpson static int read_packet_words(CPUHexagonState *env, DisasContext *ctx, 1538b453a2bSTaylor Simpson uint32_t words[]) 1548b453a2bSTaylor Simpson { 1558b453a2bSTaylor Simpson bool found_end = false; 1568b453a2bSTaylor Simpson int nwords, max_words; 1578b453a2bSTaylor Simpson 1588b453a2bSTaylor Simpson memset(words, 0, PACKET_WORDS_MAX * sizeof(uint32_t)); 1598b453a2bSTaylor Simpson for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) { 160a27c100cSTaylor Simpson words[nwords] = 1614e116893SIlya Leoshkevich translator_ldl(env, &ctx->base, 1624e116893SIlya Leoshkevich ctx->base.pc_next + nwords * sizeof(uint32_t)); 1638b453a2bSTaylor Simpson found_end = is_packet_end(words[nwords]); 1648b453a2bSTaylor Simpson } 1658b453a2bSTaylor Simpson if (!found_end) { 1668b453a2bSTaylor Simpson /* Read too many words without finding the end */ 1678b453a2bSTaylor Simpson return 0; 1688b453a2bSTaylor Simpson } 1698b453a2bSTaylor Simpson 1708b453a2bSTaylor Simpson /* Check for page boundary crossing */ 1718b453a2bSTaylor Simpson max_words = -(ctx->base.pc_next | TARGET_PAGE_MASK) / sizeof(uint32_t); 1728b453a2bSTaylor Simpson if (nwords > max_words) { 1738b453a2bSTaylor Simpson /* We can only cross a page boundary at the beginning of a TB */ 1748b453a2bSTaylor Simpson g_assert(ctx->base.num_insns == 1); 1758b453a2bSTaylor Simpson } 1768b453a2bSTaylor Simpson 1778b453a2bSTaylor Simpson HEX_DEBUG_LOG("decode_packet: pc = 0x%x\n", ctx->base.pc_next); 1788b453a2bSTaylor Simpson HEX_DEBUG_LOG(" words = { "); 1798b453a2bSTaylor Simpson for (int i = 0; i < nwords; i++) { 1808b453a2bSTaylor Simpson HEX_DEBUG_LOG("0x%x, ", words[i]); 1818b453a2bSTaylor Simpson } 1828b453a2bSTaylor Simpson HEX_DEBUG_LOG("}\n"); 1838b453a2bSTaylor Simpson 1848b453a2bSTaylor Simpson return nwords; 1858b453a2bSTaylor Simpson } 1868b453a2bSTaylor Simpson 1878b453a2bSTaylor Simpson static bool check_for_attrib(Packet *pkt, int attrib) 1888b453a2bSTaylor Simpson { 1898b453a2bSTaylor Simpson for (int i = 0; i < pkt->num_insns; i++) { 1908b453a2bSTaylor Simpson if (GET_ATTRIB(pkt->insn[i].opcode, attrib)) { 1918b453a2bSTaylor Simpson return true; 1928b453a2bSTaylor Simpson } 1938b453a2bSTaylor Simpson } 1948b453a2bSTaylor Simpson return false; 1958b453a2bSTaylor Simpson } 1968b453a2bSTaylor Simpson 1978b453a2bSTaylor Simpson static bool need_pc(Packet *pkt) 1988b453a2bSTaylor Simpson { 1998b453a2bSTaylor Simpson return check_for_attrib(pkt, A_IMPLICIT_READS_PC); 2008b453a2bSTaylor Simpson } 2018b453a2bSTaylor Simpson 2028b453a2bSTaylor Simpson static bool need_slot_cancelled(Packet *pkt) 2038b453a2bSTaylor Simpson { 2048b453a2bSTaylor Simpson return check_for_attrib(pkt, A_CONDEXEC); 2058b453a2bSTaylor Simpson } 2068b453a2bSTaylor Simpson 2078b453a2bSTaylor Simpson static bool need_pred_written(Packet *pkt) 2088b453a2bSTaylor Simpson { 2098b453a2bSTaylor Simpson return check_for_attrib(pkt, A_WRITES_PRED_REG); 2108b453a2bSTaylor Simpson } 2118b453a2bSTaylor Simpson 212*1e536334STaylor Simpson static void gen_start_packet(DisasContext *ctx) 2138b453a2bSTaylor Simpson { 214*1e536334STaylor Simpson Packet *pkt = ctx->pkt; 2158b453a2bSTaylor Simpson target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes; 2168b453a2bSTaylor Simpson int i; 2178b453a2bSTaylor Simpson 2188b453a2bSTaylor Simpson /* Clear out the disassembly context */ 2198b453a2bSTaylor Simpson ctx->reg_log_idx = 0; 2208b453a2bSTaylor Simpson bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS); 2218b453a2bSTaylor Simpson ctx->preg_log_idx = 0; 2226c677c60STaylor Simpson bitmap_zero(ctx->pregs_written, NUM_PREGS); 223a82dd548STaylor Simpson ctx->future_vregs_idx = 0; 224a82dd548STaylor Simpson ctx->tmp_vregs_idx = 0; 225a82dd548STaylor Simpson ctx->vreg_log_idx = 0; 226a82dd548STaylor Simpson bitmap_zero(ctx->vregs_updated_tmp, NUM_VREGS); 227a82dd548STaylor Simpson bitmap_zero(ctx->vregs_updated, NUM_VREGS); 228a82dd548STaylor Simpson bitmap_zero(ctx->vregs_select, NUM_VREGS); 229a82dd548STaylor Simpson ctx->qreg_log_idx = 0; 2308b453a2bSTaylor Simpson for (i = 0; i < STORES_MAX; i++) { 2318b453a2bSTaylor Simpson ctx->store_width[i] = 0; 2328b453a2bSTaylor Simpson } 2338b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_pkt_has_store_s1, pkt->pkt_has_store_s1); 23492cfa25fSTaylor Simpson ctx->s1_store_processed = false; 235a82dd548STaylor Simpson ctx->pre_commit = true; 2368b453a2bSTaylor Simpson 23785580a65STaylor Simpson if (HEX_DEBUG) { 2388b453a2bSTaylor Simpson /* Handy place to set a breakpoint before the packet executes */ 2398b453a2bSTaylor Simpson gen_helper_debug_start_packet(cpu_env); 2408b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_this_PC, ctx->base.pc_next); 24185580a65STaylor Simpson } 2428b453a2bSTaylor Simpson 2438b453a2bSTaylor Simpson /* Initialize the runtime state for packet semantics */ 2448b453a2bSTaylor Simpson if (need_pc(pkt)) { 2458b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next); 2468b453a2bSTaylor Simpson } 2478b453a2bSTaylor Simpson if (need_slot_cancelled(pkt)) { 2488b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_slot_cancelled, 0); 2498b453a2bSTaylor Simpson } 2508b453a2bSTaylor Simpson if (pkt->pkt_has_cof) { 2518b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_branch_taken, 0); 2528b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_next_PC, next_PC); 2538b453a2bSTaylor Simpson } 2548b453a2bSTaylor Simpson if (need_pred_written(pkt)) { 2558b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_pred_written, 0); 2568b453a2bSTaylor Simpson } 257a82dd548STaylor Simpson 258a82dd548STaylor Simpson if (pkt->pkt_has_hvx) { 259a82dd548STaylor Simpson tcg_gen_movi_tl(hex_VRegs_updated, 0); 260a82dd548STaylor Simpson tcg_gen_movi_tl(hex_QRegs_updated, 0); 261a82dd548STaylor Simpson } 262a82dd548STaylor Simpson } 263a82dd548STaylor Simpson 264*1e536334STaylor Simpson bool is_gather_store_insn(DisasContext *ctx) 265a82dd548STaylor Simpson { 266*1e536334STaylor Simpson Packet *pkt = ctx->pkt; 267*1e536334STaylor Simpson Insn *insn = ctx->insn; 268a82dd548STaylor Simpson if (GET_ATTRIB(insn->opcode, A_CVI_NEW) && 269a82dd548STaylor Simpson insn->new_value_producer_slot == 1) { 270a82dd548STaylor Simpson /* Look for gather instruction */ 271a82dd548STaylor Simpson for (int i = 0; i < pkt->num_insns; i++) { 272a82dd548STaylor Simpson Insn *in = &pkt->insn[i]; 273a82dd548STaylor Simpson if (GET_ATTRIB(in->opcode, A_CVI_GATHER) && in->slot == 1) { 274a82dd548STaylor Simpson return true; 275a82dd548STaylor Simpson } 276a82dd548STaylor Simpson } 277a82dd548STaylor Simpson } 278a82dd548STaylor Simpson return false; 2798b453a2bSTaylor Simpson } 2808b453a2bSTaylor Simpson 2818b453a2bSTaylor Simpson /* 2828b453a2bSTaylor Simpson * The LOG_*_WRITE macros mark most of the writes in a packet 2838b453a2bSTaylor Simpson * However, there are some implicit writes marked as attributes 2848b453a2bSTaylor Simpson * of the applicable instructions. 2858b453a2bSTaylor Simpson */ 286*1e536334STaylor Simpson static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum) 2878b453a2bSTaylor Simpson { 288*1e536334STaylor Simpson uint16_t opcode = ctx->insn->opcode; 289*1e536334STaylor Simpson if (GET_ATTRIB(opcode, attrib)) { 290b9dd6ff9STaylor Simpson /* 291b9dd6ff9STaylor Simpson * USR is used to set overflow and FP exceptions, 292b9dd6ff9STaylor Simpson * so treat it as conditional 293b9dd6ff9STaylor Simpson */ 294*1e536334STaylor Simpson bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) || 295b9dd6ff9STaylor Simpson rnum == HEX_REG_USR; 2968b453a2bSTaylor Simpson if (is_predicated && !is_preloaded(ctx, rnum)) { 2978b453a2bSTaylor Simpson tcg_gen_mov_tl(hex_new_value[rnum], hex_gpr[rnum]); 2988b453a2bSTaylor Simpson } 2998b453a2bSTaylor Simpson 3008b453a2bSTaylor Simpson ctx_log_reg_write(ctx, rnum); 3018b453a2bSTaylor Simpson } 3028b453a2bSTaylor Simpson } 3038b453a2bSTaylor Simpson 304*1e536334STaylor Simpson static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum) 3058b453a2bSTaylor Simpson { 306*1e536334STaylor Simpson if (GET_ATTRIB(ctx->insn->opcode, attrib)) { 3078b453a2bSTaylor Simpson ctx_log_pred_write(ctx, pnum); 3088b453a2bSTaylor Simpson } 3098b453a2bSTaylor Simpson } 3108b453a2bSTaylor Simpson 311*1e536334STaylor Simpson static void mark_implicit_reg_writes(DisasContext *ctx) 3128b453a2bSTaylor Simpson { 313*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP, HEX_REG_FP); 314*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SP, HEX_REG_SP); 315*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LR, HEX_REG_LR); 316*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC0, HEX_REG_LC0); 317*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0); 318*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1); 319*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1); 320*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR); 321*1e536334STaylor Simpson mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR); 3226c677c60STaylor Simpson } 3238b453a2bSTaylor Simpson 324*1e536334STaylor Simpson static void mark_implicit_pred_writes(DisasContext *ctx) 3256c677c60STaylor Simpson { 326*1e536334STaylor Simpson mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P0, 0); 327*1e536334STaylor Simpson mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P1, 1); 328*1e536334STaylor Simpson mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P2, 2); 329*1e536334STaylor Simpson mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P3, 3); 3308b453a2bSTaylor Simpson } 3318b453a2bSTaylor Simpson 332*1e536334STaylor Simpson static void mark_store_width(DisasContext *ctx) 333661ad999STaylor Simpson { 334*1e536334STaylor Simpson uint16_t opcode = ctx->insn->opcode; 335*1e536334STaylor Simpson uint32_t slot = ctx->insn->slot; 336661ad999STaylor Simpson uint8_t width = 0; 337661ad999STaylor Simpson 338661ad999STaylor Simpson if (GET_ATTRIB(opcode, A_SCALAR_STORE)) { 339661ad999STaylor Simpson if (GET_ATTRIB(opcode, A_MEMSIZE_1B)) { 340661ad999STaylor Simpson width |= 1; 341661ad999STaylor Simpson } 342661ad999STaylor Simpson if (GET_ATTRIB(opcode, A_MEMSIZE_2B)) { 343661ad999STaylor Simpson width |= 2; 344661ad999STaylor Simpson } 345661ad999STaylor Simpson if (GET_ATTRIB(opcode, A_MEMSIZE_4B)) { 346661ad999STaylor Simpson width |= 4; 347661ad999STaylor Simpson } 348661ad999STaylor Simpson if (GET_ATTRIB(opcode, A_MEMSIZE_8B)) { 349661ad999STaylor Simpson width |= 8; 350661ad999STaylor Simpson } 351661ad999STaylor Simpson tcg_debug_assert(is_power_of_2(width)); 352661ad999STaylor Simpson ctx->store_width[slot] = width; 353661ad999STaylor Simpson } 354661ad999STaylor Simpson } 355661ad999STaylor Simpson 356*1e536334STaylor Simpson static void gen_insn(DisasContext *ctx) 3578b453a2bSTaylor Simpson { 358*1e536334STaylor Simpson if (ctx->insn->generate) { 359*1e536334STaylor Simpson mark_implicit_reg_writes(ctx); 360*1e536334STaylor Simpson ctx->insn->generate(ctx); 361*1e536334STaylor Simpson mark_implicit_pred_writes(ctx); 362*1e536334STaylor Simpson mark_store_width(ctx); 3638b453a2bSTaylor Simpson } else { 364743debbcSTaylor Simpson gen_exception_end_tb(ctx, HEX_EXCP_INVALID_OPCODE); 3658b453a2bSTaylor Simpson } 3668b453a2bSTaylor Simpson } 3678b453a2bSTaylor Simpson 3688b453a2bSTaylor Simpson /* 3698b453a2bSTaylor Simpson * Helpers for generating the packet commit 3708b453a2bSTaylor Simpson */ 3718b453a2bSTaylor Simpson static void gen_reg_writes(DisasContext *ctx) 3728b453a2bSTaylor Simpson { 3738b453a2bSTaylor Simpson int i; 3748b453a2bSTaylor Simpson 3758b453a2bSTaylor Simpson for (i = 0; i < ctx->reg_log_idx; i++) { 3768b453a2bSTaylor Simpson int reg_num = ctx->reg_log[i]; 3778b453a2bSTaylor Simpson 3788b453a2bSTaylor Simpson tcg_gen_mov_tl(hex_gpr[reg_num], hex_new_value[reg_num]); 3798b453a2bSTaylor Simpson } 3808b453a2bSTaylor Simpson } 3818b453a2bSTaylor Simpson 382*1e536334STaylor Simpson static void gen_pred_writes(DisasContext *ctx) 3838b453a2bSTaylor Simpson { 3848b453a2bSTaylor Simpson int i; 3858b453a2bSTaylor Simpson 3868b453a2bSTaylor Simpson /* Early exit if the log is empty */ 3878b453a2bSTaylor Simpson if (!ctx->preg_log_idx) { 3888b453a2bSTaylor Simpson return; 3898b453a2bSTaylor Simpson } 3908b453a2bSTaylor Simpson 3918b453a2bSTaylor Simpson /* 3928b453a2bSTaylor Simpson * Only endloop instructions will conditionally 3938b453a2bSTaylor Simpson * write a predicate. If there are no endloop 3948b453a2bSTaylor Simpson * instructions, we can use the non-conditional 3958b453a2bSTaylor Simpson * write of the predicates. 3968b453a2bSTaylor Simpson */ 397*1e536334STaylor Simpson if (ctx->pkt->pkt_has_endloop) { 39823803bbeSPhilippe Mathieu-Daudé TCGv zero = tcg_constant_tl(0); 3998b453a2bSTaylor Simpson TCGv pred_written = tcg_temp_new(); 4008b453a2bSTaylor Simpson for (i = 0; i < ctx->preg_log_idx; i++) { 4018b453a2bSTaylor Simpson int pred_num = ctx->preg_log[i]; 4028b453a2bSTaylor Simpson 4038b453a2bSTaylor Simpson tcg_gen_andi_tl(pred_written, hex_pred_written, 1 << pred_num); 4048b453a2bSTaylor Simpson tcg_gen_movcond_tl(TCG_COND_NE, hex_pred[pred_num], 4058b453a2bSTaylor Simpson pred_written, zero, 4068b453a2bSTaylor Simpson hex_new_pred_value[pred_num], 4078b453a2bSTaylor Simpson hex_pred[pred_num]); 4088b453a2bSTaylor Simpson } 4098b453a2bSTaylor Simpson tcg_temp_free(pred_written); 4108b453a2bSTaylor Simpson } else { 4118b453a2bSTaylor Simpson for (i = 0; i < ctx->preg_log_idx; i++) { 4128b453a2bSTaylor Simpson int pred_num = ctx->preg_log[i]; 4138b453a2bSTaylor Simpson tcg_gen_mov_tl(hex_pred[pred_num], hex_new_pred_value[pred_num]); 41485580a65STaylor Simpson if (HEX_DEBUG) { 4158b453a2bSTaylor Simpson /* Do this so HELPER(debug_commit_end) will know */ 41685580a65STaylor Simpson tcg_gen_ori_tl(hex_pred_written, hex_pred_written, 41785580a65STaylor Simpson 1 << pred_num); 41885580a65STaylor Simpson } 4198b453a2bSTaylor Simpson } 4208b453a2bSTaylor Simpson } 4218b453a2bSTaylor Simpson } 4228b453a2bSTaylor Simpson 423a27c100cSTaylor Simpson static void gen_check_store_width(DisasContext *ctx, int slot_num) 4248b453a2bSTaylor Simpson { 42585580a65STaylor Simpson if (HEX_DEBUG) { 42623803bbeSPhilippe Mathieu-Daudé TCGv slot = tcg_constant_tl(slot_num); 42723803bbeSPhilippe Mathieu-Daudé TCGv check = tcg_constant_tl(ctx->store_width[slot_num]); 4288b453a2bSTaylor Simpson gen_helper_debug_check_store_width(cpu_env, slot, check); 42985580a65STaylor Simpson } 430a27c100cSTaylor Simpson } 4318b453a2bSTaylor Simpson 4328b453a2bSTaylor Simpson static bool slot_is_predicated(Packet *pkt, int slot_num) 4338b453a2bSTaylor Simpson { 4348b453a2bSTaylor Simpson for (int i = 0; i < pkt->num_insns; i++) { 4358b453a2bSTaylor Simpson if (pkt->insn[i].slot == slot_num) { 4368b453a2bSTaylor Simpson return GET_ATTRIB(pkt->insn[i].opcode, A_CONDEXEC); 4378b453a2bSTaylor Simpson } 4388b453a2bSTaylor Simpson } 4398b453a2bSTaylor Simpson /* If we get to here, we didn't find an instruction in the requested slot */ 4408b453a2bSTaylor Simpson g_assert_not_reached(); 4418b453a2bSTaylor Simpson } 4428b453a2bSTaylor Simpson 443*1e536334STaylor Simpson void process_store(DisasContext *ctx, int slot_num) 4448b453a2bSTaylor Simpson { 445*1e536334STaylor Simpson bool is_predicated = slot_is_predicated(ctx->pkt, slot_num); 4468b453a2bSTaylor Simpson TCGLabel *label_end = NULL; 4478b453a2bSTaylor Simpson 4488b453a2bSTaylor Simpson /* 4498b453a2bSTaylor Simpson * We may have already processed this store 4508b453a2bSTaylor Simpson * See CHECK_NOSHUF in macros.h 4518b453a2bSTaylor Simpson */ 4528b453a2bSTaylor Simpson if (slot_num == 1 && ctx->s1_store_processed) { 4538b453a2bSTaylor Simpson return; 4548b453a2bSTaylor Simpson } 45592cfa25fSTaylor Simpson ctx->s1_store_processed = true; 4568b453a2bSTaylor Simpson 4578b453a2bSTaylor Simpson if (is_predicated) { 4588b453a2bSTaylor Simpson TCGv cancelled = tcg_temp_new(); 4598b453a2bSTaylor Simpson label_end = gen_new_label(); 4608b453a2bSTaylor Simpson 4618b453a2bSTaylor Simpson /* Don't do anything if the slot was cancelled */ 4628b453a2bSTaylor Simpson tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1); 4638b453a2bSTaylor Simpson tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end); 4648b453a2bSTaylor Simpson tcg_temp_free(cancelled); 4658b453a2bSTaylor Simpson } 4668b453a2bSTaylor Simpson { 4678b453a2bSTaylor Simpson TCGv address = tcg_temp_local_new(); 4688b453a2bSTaylor Simpson tcg_gen_mov_tl(address, hex_store_addr[slot_num]); 4698b453a2bSTaylor Simpson 4708b453a2bSTaylor Simpson /* 4718b453a2bSTaylor Simpson * If we know the width from the DisasContext, we can 4728b453a2bSTaylor Simpson * generate much cleaner code. 4738b453a2bSTaylor Simpson * Unfortunately, not all instructions execute the fSTORE 4748b453a2bSTaylor Simpson * macro during code generation. Anything that uses the 4758b453a2bSTaylor Simpson * generic helper will have this problem. Instructions 4768b453a2bSTaylor Simpson * that use fWRAP to generate proper TCG code will be OK. 4778b453a2bSTaylor Simpson */ 4788b453a2bSTaylor Simpson switch (ctx->store_width[slot_num]) { 4798b453a2bSTaylor Simpson case 1: 480a27c100cSTaylor Simpson gen_check_store_width(ctx, slot_num); 4818b453a2bSTaylor Simpson tcg_gen_qemu_st8(hex_store_val32[slot_num], 4828b453a2bSTaylor Simpson hex_store_addr[slot_num], 4838b453a2bSTaylor Simpson ctx->mem_idx); 4848b453a2bSTaylor Simpson break; 4858b453a2bSTaylor Simpson case 2: 486a27c100cSTaylor Simpson gen_check_store_width(ctx, slot_num); 4878b453a2bSTaylor Simpson tcg_gen_qemu_st16(hex_store_val32[slot_num], 4888b453a2bSTaylor Simpson hex_store_addr[slot_num], 4898b453a2bSTaylor Simpson ctx->mem_idx); 4908b453a2bSTaylor Simpson break; 4918b453a2bSTaylor Simpson case 4: 492a27c100cSTaylor Simpson gen_check_store_width(ctx, slot_num); 4938b453a2bSTaylor Simpson tcg_gen_qemu_st32(hex_store_val32[slot_num], 4948b453a2bSTaylor Simpson hex_store_addr[slot_num], 4958b453a2bSTaylor Simpson ctx->mem_idx); 4968b453a2bSTaylor Simpson break; 4978b453a2bSTaylor Simpson case 8: 498a27c100cSTaylor Simpson gen_check_store_width(ctx, slot_num); 4998b453a2bSTaylor Simpson tcg_gen_qemu_st64(hex_store_val64[slot_num], 5008b453a2bSTaylor Simpson hex_store_addr[slot_num], 5018b453a2bSTaylor Simpson ctx->mem_idx); 5028b453a2bSTaylor Simpson break; 5038b453a2bSTaylor Simpson default: 5048b453a2bSTaylor Simpson { 5058b453a2bSTaylor Simpson /* 5068b453a2bSTaylor Simpson * If we get to here, we don't know the width at 5078b453a2bSTaylor Simpson * TCG generation time, we'll use a helper to 5088b453a2bSTaylor Simpson * avoid branching based on the width at runtime. 5098b453a2bSTaylor Simpson */ 51023803bbeSPhilippe Mathieu-Daudé TCGv slot = tcg_constant_tl(slot_num); 5118b453a2bSTaylor Simpson gen_helper_commit_store(cpu_env, slot); 5128b453a2bSTaylor Simpson } 5138b453a2bSTaylor Simpson } 5148b453a2bSTaylor Simpson tcg_temp_free(address); 5158b453a2bSTaylor Simpson } 5168b453a2bSTaylor Simpson if (is_predicated) { 5178b453a2bSTaylor Simpson gen_set_label(label_end); 5188b453a2bSTaylor Simpson } 5198b453a2bSTaylor Simpson } 5208b453a2bSTaylor Simpson 521*1e536334STaylor Simpson static void process_store_log(DisasContext *ctx) 5228b453a2bSTaylor Simpson { 5238b453a2bSTaylor Simpson /* 5248b453a2bSTaylor Simpson * When a packet has two stores, the hardware processes 525c23b5764STaylor Simpson * slot 1 and then slot 0. This will be important when 5268b453a2bSTaylor Simpson * the memory accesses overlap. 5278b453a2bSTaylor Simpson */ 528*1e536334STaylor Simpson Packet *pkt = ctx->pkt; 529e2be9a5cSTaylor Simpson if (pkt->pkt_has_store_s1) { 530e2be9a5cSTaylor Simpson g_assert(!pkt->pkt_has_dczeroa); 531*1e536334STaylor Simpson process_store(ctx, 1); 5328b453a2bSTaylor Simpson } 533e2be9a5cSTaylor Simpson if (pkt->pkt_has_store_s0) { 534e2be9a5cSTaylor Simpson g_assert(!pkt->pkt_has_dczeroa); 535*1e536334STaylor Simpson process_store(ctx, 0); 5368b453a2bSTaylor Simpson } 5378b453a2bSTaylor Simpson } 5388b453a2bSTaylor Simpson 5398b453a2bSTaylor Simpson /* Zero out a 32-bit cache line */ 540*1e536334STaylor Simpson static void process_dczeroa(DisasContext *ctx) 5418b453a2bSTaylor Simpson { 542*1e536334STaylor Simpson if (ctx->pkt->pkt_has_dczeroa) { 5438b453a2bSTaylor Simpson /* Store 32 bytes of zero starting at (addr & ~0x1f) */ 5448b453a2bSTaylor Simpson TCGv addr = tcg_temp_new(); 54523803bbeSPhilippe Mathieu-Daudé TCGv_i64 zero = tcg_constant_i64(0); 5468b453a2bSTaylor Simpson 5478b453a2bSTaylor Simpson tcg_gen_andi_tl(addr, hex_dczero_addr, ~0x1f); 5488b453a2bSTaylor Simpson tcg_gen_qemu_st64(zero, addr, ctx->mem_idx); 5498b453a2bSTaylor Simpson tcg_gen_addi_tl(addr, addr, 8); 5508b453a2bSTaylor Simpson tcg_gen_qemu_st64(zero, addr, ctx->mem_idx); 5518b453a2bSTaylor Simpson tcg_gen_addi_tl(addr, addr, 8); 5528b453a2bSTaylor Simpson tcg_gen_qemu_st64(zero, addr, ctx->mem_idx); 5538b453a2bSTaylor Simpson tcg_gen_addi_tl(addr, addr, 8); 5548b453a2bSTaylor Simpson tcg_gen_qemu_st64(zero, addr, ctx->mem_idx); 5558b453a2bSTaylor Simpson 5568b453a2bSTaylor Simpson tcg_temp_free(addr); 5578b453a2bSTaylor Simpson } 5588b453a2bSTaylor Simpson } 5598b453a2bSTaylor Simpson 560a82dd548STaylor Simpson static bool pkt_has_hvx_store(Packet *pkt) 561a82dd548STaylor Simpson { 562a82dd548STaylor Simpson int i; 563a82dd548STaylor Simpson for (i = 0; i < pkt->num_insns; i++) { 564a82dd548STaylor Simpson int opcode = pkt->insn[i].opcode; 565a82dd548STaylor Simpson if (GET_ATTRIB(opcode, A_CVI) && GET_ATTRIB(opcode, A_STORE)) { 566a82dd548STaylor Simpson return true; 567a82dd548STaylor Simpson } 568a82dd548STaylor Simpson } 569a82dd548STaylor Simpson return false; 570a82dd548STaylor Simpson } 571a82dd548STaylor Simpson 572*1e536334STaylor Simpson static void gen_commit_hvx(DisasContext *ctx) 573a82dd548STaylor Simpson { 574a82dd548STaylor Simpson int i; 575a82dd548STaylor Simpson 576a82dd548STaylor Simpson /* 577a82dd548STaylor Simpson * for (i = 0; i < ctx->vreg_log_idx; i++) { 578a82dd548STaylor Simpson * int rnum = ctx->vreg_log[i]; 579a82dd548STaylor Simpson * if (ctx->vreg_is_predicated[i]) { 580a82dd548STaylor Simpson * if (env->VRegs_updated & (1 << rnum)) { 581a82dd548STaylor Simpson * env->VRegs[rnum] = env->future_VRegs[rnum]; 582a82dd548STaylor Simpson * } 583a82dd548STaylor Simpson * } else { 584a82dd548STaylor Simpson * env->VRegs[rnum] = env->future_VRegs[rnum]; 585a82dd548STaylor Simpson * } 586a82dd548STaylor Simpson * } 587a82dd548STaylor Simpson */ 588a82dd548STaylor Simpson for (i = 0; i < ctx->vreg_log_idx; i++) { 589a82dd548STaylor Simpson int rnum = ctx->vreg_log[i]; 590a82dd548STaylor Simpson bool is_predicated = ctx->vreg_is_predicated[i]; 591a82dd548STaylor Simpson intptr_t dstoff = offsetof(CPUHexagonState, VRegs[rnum]); 592a82dd548STaylor Simpson intptr_t srcoff = ctx_future_vreg_off(ctx, rnum, 1, false); 593a82dd548STaylor Simpson size_t size = sizeof(MMVector); 594a82dd548STaylor Simpson 595a82dd548STaylor Simpson if (is_predicated) { 596a82dd548STaylor Simpson TCGv cmp = tcg_temp_new(); 597a82dd548STaylor Simpson TCGLabel *label_skip = gen_new_label(); 598a82dd548STaylor Simpson 599a82dd548STaylor Simpson tcg_gen_andi_tl(cmp, hex_VRegs_updated, 1 << rnum); 600a82dd548STaylor Simpson tcg_gen_brcondi_tl(TCG_COND_EQ, cmp, 0, label_skip); 601a82dd548STaylor Simpson tcg_temp_free(cmp); 602a82dd548STaylor Simpson tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size); 603a82dd548STaylor Simpson gen_set_label(label_skip); 604a82dd548STaylor Simpson } else { 605a82dd548STaylor Simpson tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size); 606a82dd548STaylor Simpson } 607a82dd548STaylor Simpson } 608a82dd548STaylor Simpson 609a82dd548STaylor Simpson /* 610a82dd548STaylor Simpson * for (i = 0; i < ctx->qreg_log_idx; i++) { 611a82dd548STaylor Simpson * int rnum = ctx->qreg_log[i]; 612a82dd548STaylor Simpson * if (ctx->qreg_is_predicated[i]) { 613a82dd548STaylor Simpson * if (env->QRegs_updated) & (1 << rnum)) { 614a82dd548STaylor Simpson * env->QRegs[rnum] = env->future_QRegs[rnum]; 615a82dd548STaylor Simpson * } 616a82dd548STaylor Simpson * } else { 617a82dd548STaylor Simpson * env->QRegs[rnum] = env->future_QRegs[rnum]; 618a82dd548STaylor Simpson * } 619a82dd548STaylor Simpson * } 620a82dd548STaylor Simpson */ 621a82dd548STaylor Simpson for (i = 0; i < ctx->qreg_log_idx; i++) { 622a82dd548STaylor Simpson int rnum = ctx->qreg_log[i]; 623a82dd548STaylor Simpson bool is_predicated = ctx->qreg_is_predicated[i]; 624a82dd548STaylor Simpson intptr_t dstoff = offsetof(CPUHexagonState, QRegs[rnum]); 625a82dd548STaylor Simpson intptr_t srcoff = offsetof(CPUHexagonState, future_QRegs[rnum]); 626a82dd548STaylor Simpson size_t size = sizeof(MMQReg); 627a82dd548STaylor Simpson 628a82dd548STaylor Simpson if (is_predicated) { 629a82dd548STaylor Simpson TCGv cmp = tcg_temp_new(); 630a82dd548STaylor Simpson TCGLabel *label_skip = gen_new_label(); 631a82dd548STaylor Simpson 632a82dd548STaylor Simpson tcg_gen_andi_tl(cmp, hex_QRegs_updated, 1 << rnum); 633a82dd548STaylor Simpson tcg_gen_brcondi_tl(TCG_COND_EQ, cmp, 0, label_skip); 634a82dd548STaylor Simpson tcg_temp_free(cmp); 635a82dd548STaylor Simpson tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size); 636a82dd548STaylor Simpson gen_set_label(label_skip); 637a82dd548STaylor Simpson } else { 638a82dd548STaylor Simpson tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size); 639a82dd548STaylor Simpson } 640a82dd548STaylor Simpson } 641a82dd548STaylor Simpson 642*1e536334STaylor Simpson if (pkt_has_hvx_store(ctx->pkt)) { 643a82dd548STaylor Simpson gen_helper_commit_hvx_stores(cpu_env); 644a82dd548STaylor Simpson } 645a82dd548STaylor Simpson } 646a82dd548STaylor Simpson 647*1e536334STaylor Simpson static void update_exec_counters(DisasContext *ctx) 6488b453a2bSTaylor Simpson { 649*1e536334STaylor Simpson Packet *pkt = ctx->pkt; 6508b453a2bSTaylor Simpson int num_insns = pkt->num_insns; 6518b453a2bSTaylor Simpson int num_real_insns = 0; 652a82dd548STaylor Simpson int num_hvx_insns = 0; 6538b453a2bSTaylor Simpson 6548b453a2bSTaylor Simpson for (int i = 0; i < num_insns; i++) { 6558b453a2bSTaylor Simpson if (!pkt->insn[i].is_endloop && 6568b453a2bSTaylor Simpson !pkt->insn[i].part1 && 6578b453a2bSTaylor Simpson !GET_ATTRIB(pkt->insn[i].opcode, A_IT_NOP)) { 6588b453a2bSTaylor Simpson num_real_insns++; 6598b453a2bSTaylor Simpson } 660a82dd548STaylor Simpson if (GET_ATTRIB(pkt->insn[i].opcode, A_CVI)) { 661a82dd548STaylor Simpson num_hvx_insns++; 662a82dd548STaylor Simpson } 6638b453a2bSTaylor Simpson } 6648b453a2bSTaylor Simpson 6658b453a2bSTaylor Simpson ctx->num_packets++; 6668b453a2bSTaylor Simpson ctx->num_insns += num_real_insns; 667a82dd548STaylor Simpson ctx->num_hvx_insns += num_hvx_insns; 6688b453a2bSTaylor Simpson } 6698b453a2bSTaylor Simpson 670*1e536334STaylor Simpson static void gen_commit_packet(DisasContext *ctx) 6718b453a2bSTaylor Simpson { 672c23b5764STaylor Simpson /* 673c23b5764STaylor Simpson * If there is more than one store in a packet, make sure they are all OK 674c23b5764STaylor Simpson * before proceeding with the rest of the packet commit. 675c23b5764STaylor Simpson * 676c23b5764STaylor Simpson * dczeroa has to be the only store operation in the packet, so we go 677c23b5764STaylor Simpson * ahead and process that first. 678c23b5764STaylor Simpson * 679a82dd548STaylor Simpson * When there is an HVX store, there can also be a scalar store in either 680a82dd548STaylor Simpson * slot 0 or slot1, so we create a mask for the helper to indicate what 681a82dd548STaylor Simpson * work to do. 682a82dd548STaylor Simpson * 683c23b5764STaylor Simpson * When there are two scalar stores, we probe the one in slot 0. 684c23b5764STaylor Simpson * 685c23b5764STaylor Simpson * Note that we don't call the probe helper for packets with only one 686c23b5764STaylor Simpson * store. Therefore, we call process_store_log before anything else 687c23b5764STaylor Simpson * involved in committing the packet. 688c23b5764STaylor Simpson */ 689*1e536334STaylor Simpson Packet *pkt = ctx->pkt; 690c23b5764STaylor Simpson bool has_store_s0 = pkt->pkt_has_store_s0; 691c23b5764STaylor Simpson bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed); 692a82dd548STaylor Simpson bool has_hvx_store = pkt_has_hvx_store(pkt); 693c23b5764STaylor Simpson if (pkt->pkt_has_dczeroa) { 694c23b5764STaylor Simpson /* 695c23b5764STaylor Simpson * The dczeroa will be the store in slot 0, check that we don't have 696a82dd548STaylor Simpson * a store in slot 1 or an HVX store. 697c23b5764STaylor Simpson */ 698e2be9a5cSTaylor Simpson g_assert(!has_store_s1 && !has_hvx_store); 699*1e536334STaylor Simpson process_dczeroa(ctx); 700a82dd548STaylor Simpson } else if (has_hvx_store) { 701a82dd548STaylor Simpson TCGv mem_idx = tcg_constant_tl(ctx->mem_idx); 702a82dd548STaylor Simpson 703a82dd548STaylor Simpson if (!has_store_s0 && !has_store_s1) { 704a82dd548STaylor Simpson gen_helper_probe_hvx_stores(cpu_env, mem_idx); 705a82dd548STaylor Simpson } else { 706a82dd548STaylor Simpson int mask = 0; 707a82dd548STaylor Simpson TCGv mask_tcgv; 708a82dd548STaylor Simpson 709a82dd548STaylor Simpson if (has_store_s0) { 710a82dd548STaylor Simpson mask |= (1 << 0); 711a82dd548STaylor Simpson } 712a82dd548STaylor Simpson if (has_store_s1) { 713a82dd548STaylor Simpson mask |= (1 << 1); 714a82dd548STaylor Simpson } 715a82dd548STaylor Simpson if (has_hvx_store) { 716a82dd548STaylor Simpson mask |= (1 << 2); 717a82dd548STaylor Simpson } 718a82dd548STaylor Simpson mask_tcgv = tcg_constant_tl(mask); 719a82dd548STaylor Simpson gen_helper_probe_pkt_scalar_hvx_stores(cpu_env, mask_tcgv, mem_idx); 720a82dd548STaylor Simpson } 721c23b5764STaylor Simpson } else if (has_store_s0 && has_store_s1) { 722c23b5764STaylor Simpson /* 723c23b5764STaylor Simpson * process_store_log will execute the slot 1 store first, 724c23b5764STaylor Simpson * so we only have to probe the store in slot 0 725c23b5764STaylor Simpson */ 726f448397aSTaylor Simpson TCGv mem_idx = tcg_constant_tl(ctx->mem_idx); 727c23b5764STaylor Simpson gen_helper_probe_pkt_scalar_store_s0(cpu_env, mem_idx); 728c23b5764STaylor Simpson } 729c23b5764STaylor Simpson 730*1e536334STaylor Simpson process_store_log(ctx); 731c23b5764STaylor Simpson 7328b453a2bSTaylor Simpson gen_reg_writes(ctx); 733*1e536334STaylor Simpson gen_pred_writes(ctx); 734a82dd548STaylor Simpson if (pkt->pkt_has_hvx) { 735*1e536334STaylor Simpson gen_commit_hvx(ctx); 736a82dd548STaylor Simpson } 737*1e536334STaylor Simpson update_exec_counters(ctx); 73885580a65STaylor Simpson if (HEX_DEBUG) { 7398b453a2bSTaylor Simpson TCGv has_st0 = 74023803bbeSPhilippe Mathieu-Daudé tcg_constant_tl(pkt->pkt_has_store_s0 && !pkt->pkt_has_dczeroa); 7418b453a2bSTaylor Simpson TCGv has_st1 = 74223803bbeSPhilippe Mathieu-Daudé tcg_constant_tl(pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa); 7438b453a2bSTaylor Simpson 7448b453a2bSTaylor Simpson /* Handy place to set a breakpoint at the end of execution */ 7458b453a2bSTaylor Simpson gen_helper_debug_commit_end(cpu_env, has_st0, has_st1); 7468b453a2bSTaylor Simpson } 7478b453a2bSTaylor Simpson 748a82dd548STaylor Simpson if (pkt->vhist_insn != NULL) { 749a82dd548STaylor Simpson ctx->pre_commit = false; 750*1e536334STaylor Simpson ctx->insn = pkt->vhist_insn; 751*1e536334STaylor Simpson pkt->vhist_insn->generate(ctx); 752a82dd548STaylor Simpson } 753a82dd548STaylor Simpson 7548b453a2bSTaylor Simpson if (pkt->pkt_has_cof) { 755743debbcSTaylor Simpson gen_end_tb(ctx); 7568b453a2bSTaylor Simpson } 7578b453a2bSTaylor Simpson } 7588b453a2bSTaylor Simpson 7598b453a2bSTaylor Simpson static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx) 7608b453a2bSTaylor Simpson { 7618b453a2bSTaylor Simpson uint32_t words[PACKET_WORDS_MAX]; 7628b453a2bSTaylor Simpson int nwords; 7638b453a2bSTaylor Simpson Packet pkt; 7648b453a2bSTaylor Simpson int i; 7658b453a2bSTaylor Simpson 7668b453a2bSTaylor Simpson nwords = read_packet_words(env, ctx, words); 7678b453a2bSTaylor Simpson if (!nwords) { 768743debbcSTaylor Simpson gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET); 7698b453a2bSTaylor Simpson return; 7708b453a2bSTaylor Simpson } 7718b453a2bSTaylor Simpson 7728b453a2bSTaylor Simpson if (decode_packet(nwords, words, &pkt, false) > 0) { 7738b453a2bSTaylor Simpson HEX_DEBUG_PRINT_PKT(&pkt); 774*1e536334STaylor Simpson ctx->pkt = &pkt; 775*1e536334STaylor Simpson gen_start_packet(ctx); 7768b453a2bSTaylor Simpson for (i = 0; i < pkt.num_insns; i++) { 777*1e536334STaylor Simpson ctx->insn = &pkt.insn[i]; 778*1e536334STaylor Simpson gen_insn(ctx); 7798b453a2bSTaylor Simpson } 780*1e536334STaylor Simpson gen_commit_packet(ctx); 7818b453a2bSTaylor Simpson ctx->base.pc_next += pkt.encod_pkt_size_in_bytes; 7828b453a2bSTaylor Simpson } else { 783743debbcSTaylor Simpson gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET); 7848b453a2bSTaylor Simpson } 7858b453a2bSTaylor Simpson } 7868b453a2bSTaylor Simpson 7878b453a2bSTaylor Simpson static void hexagon_tr_init_disas_context(DisasContextBase *dcbase, 7888b453a2bSTaylor Simpson CPUState *cs) 7898b453a2bSTaylor Simpson { 7908b453a2bSTaylor Simpson DisasContext *ctx = container_of(dcbase, DisasContext, base); 7918b453a2bSTaylor Simpson 7928b453a2bSTaylor Simpson ctx->mem_idx = MMU_USER_IDX; 7938b453a2bSTaylor Simpson ctx->num_packets = 0; 7948b453a2bSTaylor Simpson ctx->num_insns = 0; 795a82dd548STaylor Simpson ctx->num_hvx_insns = 0; 7968b453a2bSTaylor Simpson } 7978b453a2bSTaylor Simpson 7988b453a2bSTaylor Simpson static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu) 7998b453a2bSTaylor Simpson { 8008b453a2bSTaylor Simpson } 8018b453a2bSTaylor Simpson 8028b453a2bSTaylor Simpson static void hexagon_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu) 8038b453a2bSTaylor Simpson { 8048b453a2bSTaylor Simpson DisasContext *ctx = container_of(dcbase, DisasContext, base); 8058b453a2bSTaylor Simpson 8068b453a2bSTaylor Simpson tcg_gen_insn_start(ctx->base.pc_next); 8078b453a2bSTaylor Simpson } 8088b453a2bSTaylor Simpson 8098b453a2bSTaylor Simpson static bool pkt_crosses_page(CPUHexagonState *env, DisasContext *ctx) 8108b453a2bSTaylor Simpson { 8118b453a2bSTaylor Simpson target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK; 8128b453a2bSTaylor Simpson bool found_end = false; 8138b453a2bSTaylor Simpson int nwords; 8148b453a2bSTaylor Simpson 8158b453a2bSTaylor Simpson for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) { 8168b453a2bSTaylor Simpson uint32_t word = cpu_ldl_code(env, 8178b453a2bSTaylor Simpson ctx->base.pc_next + nwords * sizeof(uint32_t)); 8188b453a2bSTaylor Simpson found_end = is_packet_end(word); 8198b453a2bSTaylor Simpson } 8208b453a2bSTaylor Simpson uint32_t next_ptr = ctx->base.pc_next + nwords * sizeof(uint32_t); 8218b453a2bSTaylor Simpson return found_end && next_ptr - page_start >= TARGET_PAGE_SIZE; 8228b453a2bSTaylor Simpson } 8238b453a2bSTaylor Simpson 8248b453a2bSTaylor Simpson static void hexagon_tr_translate_packet(DisasContextBase *dcbase, CPUState *cpu) 8258b453a2bSTaylor Simpson { 8268b453a2bSTaylor Simpson DisasContext *ctx = container_of(dcbase, DisasContext, base); 8278b453a2bSTaylor Simpson CPUHexagonState *env = cpu->env_ptr; 8288b453a2bSTaylor Simpson 8298b453a2bSTaylor Simpson decode_and_translate_packet(env, ctx); 8308b453a2bSTaylor Simpson 8318b453a2bSTaylor Simpson if (ctx->base.is_jmp == DISAS_NEXT) { 8328b453a2bSTaylor Simpson target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK; 8338b453a2bSTaylor Simpson target_ulong bytes_max = PACKET_WORDS_MAX * sizeof(target_ulong); 8348b453a2bSTaylor Simpson 8358b453a2bSTaylor Simpson if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE || 8368b453a2bSTaylor Simpson (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE - bytes_max && 8378b453a2bSTaylor Simpson pkt_crosses_page(env, ctx))) { 8388b453a2bSTaylor Simpson ctx->base.is_jmp = DISAS_TOO_MANY; 8398b453a2bSTaylor Simpson } 8408b453a2bSTaylor Simpson 8418b453a2bSTaylor Simpson /* 8428b453a2bSTaylor Simpson * The CPU log is used to compare against LLDB single stepping, 8438b453a2bSTaylor Simpson * so end the TLB after every packet. 8448b453a2bSTaylor Simpson */ 8457d9ab202STaylor Simpson HexagonCPU *hex_cpu = env_archcpu(env); 8468b453a2bSTaylor Simpson if (hex_cpu->lldb_compat && qemu_loglevel_mask(CPU_LOG_TB_CPU)) { 8478b453a2bSTaylor Simpson ctx->base.is_jmp = DISAS_TOO_MANY; 8488b453a2bSTaylor Simpson } 8498b453a2bSTaylor Simpson } 8508b453a2bSTaylor Simpson } 8518b453a2bSTaylor Simpson 8528b453a2bSTaylor Simpson static void hexagon_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu) 8538b453a2bSTaylor Simpson { 8548b453a2bSTaylor Simpson DisasContext *ctx = container_of(dcbase, DisasContext, base); 8558b453a2bSTaylor Simpson 8568b453a2bSTaylor Simpson switch (ctx->base.is_jmp) { 8578b453a2bSTaylor Simpson case DISAS_TOO_MANY: 8588b453a2bSTaylor Simpson gen_exec_counters(ctx); 8598b453a2bSTaylor Simpson tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next); 8608b453a2bSTaylor Simpson tcg_gen_exit_tb(NULL, 0); 8618b453a2bSTaylor Simpson break; 8628b453a2bSTaylor Simpson case DISAS_NORETURN: 8638b453a2bSTaylor Simpson break; 8648b453a2bSTaylor Simpson default: 8658b453a2bSTaylor Simpson g_assert_not_reached(); 8668b453a2bSTaylor Simpson } 8678b453a2bSTaylor Simpson } 8688b453a2bSTaylor Simpson 8698eb806a7SRichard Henderson static void hexagon_tr_disas_log(const DisasContextBase *dcbase, 8708eb806a7SRichard Henderson CPUState *cpu, FILE *logfile) 8718b453a2bSTaylor Simpson { 8728eb806a7SRichard Henderson fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first)); 8738eb806a7SRichard Henderson target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size); 8748b453a2bSTaylor Simpson } 8758b453a2bSTaylor Simpson 8768b453a2bSTaylor Simpson 8778b453a2bSTaylor Simpson static const TranslatorOps hexagon_tr_ops = { 8788b453a2bSTaylor Simpson .init_disas_context = hexagon_tr_init_disas_context, 8798b453a2bSTaylor Simpson .tb_start = hexagon_tr_tb_start, 8808b453a2bSTaylor Simpson .insn_start = hexagon_tr_insn_start, 8818b453a2bSTaylor Simpson .translate_insn = hexagon_tr_translate_packet, 8828b453a2bSTaylor Simpson .tb_stop = hexagon_tr_tb_stop, 8838b453a2bSTaylor Simpson .disas_log = hexagon_tr_disas_log, 8848b453a2bSTaylor Simpson }; 8858b453a2bSTaylor Simpson 886306c8721SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns, 887306c8721SRichard Henderson target_ulong pc, void *host_pc) 8888b453a2bSTaylor Simpson { 8898b453a2bSTaylor Simpson DisasContext ctx; 8908b453a2bSTaylor Simpson 891306c8721SRichard Henderson translator_loop(cs, tb, max_insns, pc, host_pc, 892306c8721SRichard Henderson &hexagon_tr_ops, &ctx.base); 8938b453a2bSTaylor Simpson } 8948b453a2bSTaylor Simpson 8958b453a2bSTaylor Simpson #define NAME_LEN 64 8968b453a2bSTaylor Simpson static char new_value_names[TOTAL_PER_THREAD_REGS][NAME_LEN]; 8978b453a2bSTaylor Simpson static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN]; 8988b453a2bSTaylor Simpson static char new_pred_value_names[NUM_PREGS][NAME_LEN]; 8998b453a2bSTaylor Simpson static char store_addr_names[STORES_MAX][NAME_LEN]; 9008b453a2bSTaylor Simpson static char store_width_names[STORES_MAX][NAME_LEN]; 9018b453a2bSTaylor Simpson static char store_val32_names[STORES_MAX][NAME_LEN]; 9028b453a2bSTaylor Simpson static char store_val64_names[STORES_MAX][NAME_LEN]; 903a82dd548STaylor Simpson static char vstore_addr_names[VSTORES_MAX][NAME_LEN]; 904a82dd548STaylor Simpson static char vstore_size_names[VSTORES_MAX][NAME_LEN]; 905a82dd548STaylor Simpson static char vstore_pending_names[VSTORES_MAX][NAME_LEN]; 9068b453a2bSTaylor Simpson 9078b453a2bSTaylor Simpson void hexagon_translate_init(void) 9088b453a2bSTaylor Simpson { 9098b453a2bSTaylor Simpson int i; 9108b453a2bSTaylor Simpson 9118b453a2bSTaylor Simpson opcode_init(); 9128b453a2bSTaylor Simpson 9138b453a2bSTaylor Simpson for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) { 9148b453a2bSTaylor Simpson hex_gpr[i] = tcg_global_mem_new(cpu_env, 9158b453a2bSTaylor Simpson offsetof(CPUHexagonState, gpr[i]), 9168b453a2bSTaylor Simpson hexagon_regnames[i]); 9178b453a2bSTaylor Simpson 9188b453a2bSTaylor Simpson snprintf(new_value_names[i], NAME_LEN, "new_%s", hexagon_regnames[i]); 9198b453a2bSTaylor Simpson hex_new_value[i] = tcg_global_mem_new(cpu_env, 9208b453a2bSTaylor Simpson offsetof(CPUHexagonState, new_value[i]), 9218b453a2bSTaylor Simpson new_value_names[i]); 9228b453a2bSTaylor Simpson 92385580a65STaylor Simpson if (HEX_DEBUG) { 9248b453a2bSTaylor Simpson snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s", 9258b453a2bSTaylor Simpson hexagon_regnames[i]); 9268b453a2bSTaylor Simpson hex_reg_written[i] = tcg_global_mem_new(cpu_env, 9278b453a2bSTaylor Simpson offsetof(CPUHexagonState, reg_written[i]), 9288b453a2bSTaylor Simpson reg_written_names[i]); 92985580a65STaylor Simpson } 9308b453a2bSTaylor Simpson } 9318b453a2bSTaylor Simpson for (i = 0; i < NUM_PREGS; i++) { 9328b453a2bSTaylor Simpson hex_pred[i] = tcg_global_mem_new(cpu_env, 9338b453a2bSTaylor Simpson offsetof(CPUHexagonState, pred[i]), 9348b453a2bSTaylor Simpson hexagon_prednames[i]); 9358b453a2bSTaylor Simpson 9368b453a2bSTaylor Simpson snprintf(new_pred_value_names[i], NAME_LEN, "new_pred_%s", 9378b453a2bSTaylor Simpson hexagon_prednames[i]); 9388b453a2bSTaylor Simpson hex_new_pred_value[i] = tcg_global_mem_new(cpu_env, 9398b453a2bSTaylor Simpson offsetof(CPUHexagonState, new_pred_value[i]), 9408b453a2bSTaylor Simpson new_pred_value_names[i]); 9418b453a2bSTaylor Simpson } 9428b453a2bSTaylor Simpson hex_pred_written = tcg_global_mem_new(cpu_env, 9438b453a2bSTaylor Simpson offsetof(CPUHexagonState, pred_written), "pred_written"); 9448b453a2bSTaylor Simpson hex_next_PC = tcg_global_mem_new(cpu_env, 9458b453a2bSTaylor Simpson offsetof(CPUHexagonState, next_PC), "next_PC"); 9468b453a2bSTaylor Simpson hex_this_PC = tcg_global_mem_new(cpu_env, 9478b453a2bSTaylor Simpson offsetof(CPUHexagonState, this_PC), "this_PC"); 9488b453a2bSTaylor Simpson hex_slot_cancelled = tcg_global_mem_new(cpu_env, 9498b453a2bSTaylor Simpson offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled"); 9508b453a2bSTaylor Simpson hex_branch_taken = tcg_global_mem_new(cpu_env, 9518b453a2bSTaylor Simpson offsetof(CPUHexagonState, branch_taken), "branch_taken"); 9528b453a2bSTaylor Simpson hex_pkt_has_store_s1 = tcg_global_mem_new(cpu_env, 9538b453a2bSTaylor Simpson offsetof(CPUHexagonState, pkt_has_store_s1), "pkt_has_store_s1"); 9548b453a2bSTaylor Simpson hex_dczero_addr = tcg_global_mem_new(cpu_env, 9558b453a2bSTaylor Simpson offsetof(CPUHexagonState, dczero_addr), "dczero_addr"); 9568b453a2bSTaylor Simpson hex_llsc_addr = tcg_global_mem_new(cpu_env, 9578b453a2bSTaylor Simpson offsetof(CPUHexagonState, llsc_addr), "llsc_addr"); 9588b453a2bSTaylor Simpson hex_llsc_val = tcg_global_mem_new(cpu_env, 9598b453a2bSTaylor Simpson offsetof(CPUHexagonState, llsc_val), "llsc_val"); 9608b453a2bSTaylor Simpson hex_llsc_val_i64 = tcg_global_mem_new_i64(cpu_env, 9618b453a2bSTaylor Simpson offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64"); 962a82dd548STaylor Simpson hex_VRegs_updated = tcg_global_mem_new(cpu_env, 963a82dd548STaylor Simpson offsetof(CPUHexagonState, VRegs_updated), "VRegs_updated"); 964a82dd548STaylor Simpson hex_QRegs_updated = tcg_global_mem_new(cpu_env, 965a82dd548STaylor Simpson offsetof(CPUHexagonState, QRegs_updated), "QRegs_updated"); 9668b453a2bSTaylor Simpson for (i = 0; i < STORES_MAX; i++) { 9678b453a2bSTaylor Simpson snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i); 9688b453a2bSTaylor Simpson hex_store_addr[i] = tcg_global_mem_new(cpu_env, 9698b453a2bSTaylor Simpson offsetof(CPUHexagonState, mem_log_stores[i].va), 9708b453a2bSTaylor Simpson store_addr_names[i]); 9718b453a2bSTaylor Simpson 9728b453a2bSTaylor Simpson snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i); 9738b453a2bSTaylor Simpson hex_store_width[i] = tcg_global_mem_new(cpu_env, 9748b453a2bSTaylor Simpson offsetof(CPUHexagonState, mem_log_stores[i].width), 9758b453a2bSTaylor Simpson store_width_names[i]); 9768b453a2bSTaylor Simpson 9778b453a2bSTaylor Simpson snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i); 9788b453a2bSTaylor Simpson hex_store_val32[i] = tcg_global_mem_new(cpu_env, 9798b453a2bSTaylor Simpson offsetof(CPUHexagonState, mem_log_stores[i].data32), 9808b453a2bSTaylor Simpson store_val32_names[i]); 9818b453a2bSTaylor Simpson 9828b453a2bSTaylor Simpson snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i); 9838b453a2bSTaylor Simpson hex_store_val64[i] = tcg_global_mem_new_i64(cpu_env, 9848b453a2bSTaylor Simpson offsetof(CPUHexagonState, mem_log_stores[i].data64), 9858b453a2bSTaylor Simpson store_val64_names[i]); 9868b453a2bSTaylor Simpson } 987a82dd548STaylor Simpson for (int i = 0; i < VSTORES_MAX; i++) { 988a82dd548STaylor Simpson snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i); 989a82dd548STaylor Simpson hex_vstore_addr[i] = tcg_global_mem_new(cpu_env, 990a82dd548STaylor Simpson offsetof(CPUHexagonState, vstore[i].va), 991a82dd548STaylor Simpson vstore_addr_names[i]); 992a82dd548STaylor Simpson 993a82dd548STaylor Simpson snprintf(vstore_size_names[i], NAME_LEN, "vstore_size_%d", i); 994a82dd548STaylor Simpson hex_vstore_size[i] = tcg_global_mem_new(cpu_env, 995a82dd548STaylor Simpson offsetof(CPUHexagonState, vstore[i].size), 996a82dd548STaylor Simpson vstore_size_names[i]); 997a82dd548STaylor Simpson 998a82dd548STaylor Simpson snprintf(vstore_pending_names[i], NAME_LEN, "vstore_pending_%d", i); 999a82dd548STaylor Simpson hex_vstore_pending[i] = tcg_global_mem_new(cpu_env, 1000a82dd548STaylor Simpson offsetof(CPUHexagonState, vstore_pending[i]), 1001a82dd548STaylor Simpson vstore_pending_names[i]); 1002a82dd548STaylor Simpson } 10038b453a2bSTaylor Simpson } 1004