2 * Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "qemu/osdep.h"
21 #include "tcg/tcg-op.h"
24 #include "translate.h"
25 #define QEMU_GENERATE /* Used internally by macros.h */
30 static inline void gen_log_predicated_reg_write(int rnum
, TCGv val
, int slot
)
32 TCGv zero
= tcg_const_tl(0);
33 TCGv slot_mask
= tcg_temp_new();
35 tcg_gen_andi_tl(slot_mask
, hex_slot_cancelled
, 1 << slot
);
36 tcg_gen_movcond_tl(TCG_COND_EQ
, hex_new_value
[rnum
], slot_mask
, zero
,
37 val
, hex_new_value
[rnum
]);
40 * Do this so HELPER(debug_commit_end) will know
42 * Note that slot_mask indicates the value is not written
43 * (i.e., slot was cancelled), so we create a true/false value before
44 * or'ing with hex_reg_written[rnum].
46 tcg_gen_setcond_tl(TCG_COND_EQ
, slot_mask
, slot_mask
, zero
);
47 tcg_gen_or_tl(hex_reg_written
[rnum
], hex_reg_written
[rnum
], slot_mask
);
51 tcg_temp_free(slot_mask
);
54 static inline void gen_log_reg_write(int rnum
, TCGv val
)
56 tcg_gen_mov_tl(hex_new_value
[rnum
], val
);
58 /* Do this so HELPER(debug_commit_end) will know */
59 tcg_gen_movi_tl(hex_reg_written
[rnum
], 1);
63 static void gen_log_predicated_reg_write_pair(int rnum
, TCGv_i64 val
, int slot
)
65 TCGv val32
= tcg_temp_new();
66 TCGv zero
= tcg_const_tl(0);
67 TCGv slot_mask
= tcg_temp_new();
69 tcg_gen_andi_tl(slot_mask
, hex_slot_cancelled
, 1 << slot
);
71 tcg_gen_extrl_i64_i32(val32
, val
);
72 tcg_gen_movcond_tl(TCG_COND_EQ
, hex_new_value
[rnum
],
74 val32
, hex_new_value
[rnum
]);
76 tcg_gen_extrh_i64_i32(val32
, val
);
77 tcg_gen_movcond_tl(TCG_COND_EQ
, hex_new_value
[rnum
+ 1],
79 val32
, hex_new_value
[rnum
+ 1]);
82 * Do this so HELPER(debug_commit_end) will know
84 * Note that slot_mask indicates the value is not written
85 * (i.e., slot was cancelled), so we create a true/false value before
86 * or'ing with hex_reg_written[rnum].
88 tcg_gen_setcond_tl(TCG_COND_EQ
, slot_mask
, slot_mask
, zero
);
89 tcg_gen_or_tl(hex_reg_written
[rnum
], hex_reg_written
[rnum
], slot_mask
);
90 tcg_gen_or_tl(hex_reg_written
[rnum
+ 1], hex_reg_written
[rnum
+ 1],
96 tcg_temp_free(slot_mask
);
99 static void gen_log_reg_write_pair(int rnum
, TCGv_i64 val
)
102 tcg_gen_extrl_i64_i32(hex_new_value
[rnum
], val
);
104 /* Do this so HELPER(debug_commit_end) will know */
105 tcg_gen_movi_tl(hex_reg_written
[rnum
], 1);
109 tcg_gen_extrh_i64_i32(hex_new_value
[rnum
+ 1], val
);
111 /* Do this so HELPER(debug_commit_end) will know */
112 tcg_gen_movi_tl(hex_reg_written
[rnum
+ 1], 1);
116 static inline void gen_log_pred_write(DisasContext
*ctx
, int pnum
, TCGv val
)
118 TCGv base_val
= tcg_temp_new();
120 tcg_gen_andi_tl(base_val
, val
, 0xff);
123 * Section 6.1.3 of the Hexagon V67 Programmer's Reference Manual
125 * Multiple writes to the same preg are and'ed together
126 * If this is the first predicate write in the packet, do a
127 * straight assignment. Otherwise, do an and.
129 if (!test_bit(pnum
, ctx
->pregs_written
)) {
130 tcg_gen_mov_tl(hex_new_pred_value
[pnum
], base_val
);
132 tcg_gen_and_tl(hex_new_pred_value
[pnum
],
133 hex_new_pred_value
[pnum
], base_val
);
135 tcg_gen_ori_tl(hex_pred_written
, hex_pred_written
, 1 << pnum
);
137 tcg_temp_free(base_val
);
140 static inline void gen_read_p3_0(TCGv control_reg
)
142 tcg_gen_movi_tl(control_reg
, 0);
143 for (int i
= 0; i
< NUM_PREGS
; i
++) {
144 tcg_gen_deposit_tl(control_reg
, control_reg
, hex_pred
[i
], i
* 8, 8);
149 * Certain control registers require special handling on read
150 * HEX_REG_P3_0 aliased to the predicate registers
151 * -> concat the 4 predicate registers together
152 * HEX_REG_PC actual value stored in DisasContext
153 * -> assign from ctx->base.pc_next
154 * HEX_REG_QEMU_*_CNT changes in current TB in DisasContext
155 * -> add current TB changes to existing reg value
157 static inline void gen_read_ctrl_reg(DisasContext
*ctx
, const int reg_num
,
160 if (reg_num
== HEX_REG_P3_0
) {
162 } else if (reg_num
== HEX_REG_PC
) {
163 tcg_gen_movi_tl(dest
, ctx
->base
.pc_next
);
164 } else if (reg_num
== HEX_REG_QEMU_PKT_CNT
) {
165 tcg_gen_addi_tl(dest
, hex_gpr
[HEX_REG_QEMU_PKT_CNT
],
167 } else if (reg_num
== HEX_REG_QEMU_INSN_CNT
) {
168 tcg_gen_addi_tl(dest
, hex_gpr
[HEX_REG_QEMU_INSN_CNT
],
171 tcg_gen_mov_tl(dest
, hex_gpr
[reg_num
]);
175 static inline void gen_read_ctrl_reg_pair(DisasContext
*ctx
, const int reg_num
,
178 if (reg_num
== HEX_REG_P3_0
) {
179 TCGv p3_0
= tcg_temp_new();
181 tcg_gen_concat_i32_i64(dest
, p3_0
, hex_gpr
[reg_num
+ 1]);
183 } else if (reg_num
== HEX_REG_PC
- 1) {
184 TCGv pc
= tcg_const_tl(ctx
->base
.pc_next
);
185 tcg_gen_concat_i32_i64(dest
, hex_gpr
[reg_num
], pc
);
187 } else if (reg_num
== HEX_REG_QEMU_PKT_CNT
) {
188 TCGv pkt_cnt
= tcg_temp_new();
189 TCGv insn_cnt
= tcg_temp_new();
190 tcg_gen_addi_tl(pkt_cnt
, hex_gpr
[HEX_REG_QEMU_PKT_CNT
],
192 tcg_gen_addi_tl(insn_cnt
, hex_gpr
[HEX_REG_QEMU_INSN_CNT
],
194 tcg_gen_concat_i32_i64(dest
, pkt_cnt
, insn_cnt
);
195 tcg_temp_free(pkt_cnt
);
196 tcg_temp_free(insn_cnt
);
198 tcg_gen_concat_i32_i64(dest
,
200 hex_gpr
[reg_num
+ 1]);
204 static inline void gen_write_p3_0(TCGv control_reg
)
206 for (int i
= 0; i
< NUM_PREGS
; i
++) {
207 tcg_gen_extract_tl(hex_pred
[i
], control_reg
, i
* 8, 8);
212 * Certain control registers require special handling on write
213 * HEX_REG_P3_0 aliased to the predicate registers
214 * -> break the value across 4 predicate registers
215 * HEX_REG_QEMU_*_CNT changes in current TB in DisasContext
216 * -> clear the changes
218 static inline void gen_write_ctrl_reg(DisasContext
*ctx
, int reg_num
,
221 if (reg_num
== HEX_REG_P3_0
) {
224 gen_log_reg_write(reg_num
, val
);
225 ctx_log_reg_write(ctx
, reg_num
);
226 if (reg_num
== HEX_REG_QEMU_PKT_CNT
) {
227 ctx
->num_packets
= 0;
229 if (reg_num
== HEX_REG_QEMU_INSN_CNT
) {
235 static inline void gen_write_ctrl_reg_pair(DisasContext
*ctx
, int reg_num
,
238 if (reg_num
== HEX_REG_P3_0
) {
239 TCGv val32
= tcg_temp_new();
240 tcg_gen_extrl_i64_i32(val32
, val
);
241 gen_write_p3_0(val32
);
242 tcg_gen_extrh_i64_i32(val32
, val
);
243 gen_log_reg_write(reg_num
+ 1, val32
);
244 tcg_temp_free(val32
);
245 ctx_log_reg_write(ctx
, reg_num
+ 1);
247 gen_log_reg_write_pair(reg_num
, val
);
248 ctx_log_reg_write_pair(ctx
, reg_num
);
249 if (reg_num
== HEX_REG_QEMU_PKT_CNT
) {
250 ctx
->num_packets
= 0;
256 static TCGv
gen_get_byte(TCGv result
, int N
, TCGv src
, bool sign
)
259 tcg_gen_sextract_tl(result
, src
, N
* 8, 8);
261 tcg_gen_extract_tl(result
, src
, N
* 8, 8);
266 static TCGv
gen_get_byte_i64(TCGv result
, int N
, TCGv_i64 src
, bool sign
)
268 TCGv_i64 res64
= tcg_temp_new_i64();
270 tcg_gen_sextract_i64(res64
, src
, N
* 8, 8);
272 tcg_gen_extract_i64(res64
, src
, N
* 8, 8);
274 tcg_gen_extrl_i64_i32(result
, res64
);
275 tcg_temp_free_i64(res64
);
280 static inline TCGv
gen_get_half(TCGv result
, int N
, TCGv src
, bool sign
)
283 tcg_gen_sextract_tl(result
, src
, N
* 16, 16);
285 tcg_gen_extract_tl(result
, src
, N
* 16, 16);
290 static inline void gen_set_half(int N
, TCGv result
, TCGv src
)
292 tcg_gen_deposit_tl(result
, result
, src
, N
* 16, 16);
295 static inline void gen_set_half_i64(int N
, TCGv_i64 result
, TCGv src
)
297 TCGv_i64 src64
= tcg_temp_new_i64();
298 tcg_gen_extu_i32_i64(src64
, src
);
299 tcg_gen_deposit_i64(result
, result
, src64
, N
* 16, 16);
300 tcg_temp_free_i64(src64
);
303 static void gen_set_byte_i64(int N
, TCGv_i64 result
, TCGv src
)
305 TCGv_i64 src64
= tcg_temp_new_i64();
306 tcg_gen_extu_i32_i64(src64
, src
);
307 tcg_gen_deposit_i64(result
, result
, src64
, N
* 8, 8);
308 tcg_temp_free_i64(src64
);
311 static inline void gen_load_locked4u(TCGv dest
, TCGv vaddr
, int mem_index
)
313 tcg_gen_qemu_ld32u(dest
, vaddr
, mem_index
);
314 tcg_gen_mov_tl(hex_llsc_addr
, vaddr
);
315 tcg_gen_mov_tl(hex_llsc_val
, dest
);
318 static inline void gen_load_locked8u(TCGv_i64 dest
, TCGv vaddr
, int mem_index
)
320 tcg_gen_qemu_ld64(dest
, vaddr
, mem_index
);
321 tcg_gen_mov_tl(hex_llsc_addr
, vaddr
);
322 tcg_gen_mov_i64(hex_llsc_val_i64
, dest
);
325 static inline void gen_store_conditional4(DisasContext
*ctx
,
326 TCGv pred
, TCGv vaddr
, TCGv src
)
328 TCGLabel
*fail
= gen_new_label();
329 TCGLabel
*done
= gen_new_label();
332 tcg_gen_brcond_tl(TCG_COND_NE
, vaddr
, hex_llsc_addr
, fail
);
334 one
= tcg_const_tl(0xff);
335 zero
= tcg_const_tl(0);
336 tmp
= tcg_temp_new();
337 tcg_gen_atomic_cmpxchg_tl(tmp
, hex_llsc_addr
, hex_llsc_val
, src
,
338 ctx
->mem_idx
, MO_32
);
339 tcg_gen_movcond_tl(TCG_COND_EQ
, pred
, tmp
, hex_llsc_val
,
347 tcg_gen_movi_tl(pred
, 0);
350 tcg_gen_movi_tl(hex_llsc_addr
, ~0);
353 static inline void gen_store_conditional8(DisasContext
*ctx
,
354 TCGv pred
, TCGv vaddr
, TCGv_i64 src
)
356 TCGLabel
*fail
= gen_new_label();
357 TCGLabel
*done
= gen_new_label();
358 TCGv_i64 one
, zero
, tmp
;
360 tcg_gen_brcond_tl(TCG_COND_NE
, vaddr
, hex_llsc_addr
, fail
);
362 one
= tcg_const_i64(0xff);
363 zero
= tcg_const_i64(0);
364 tmp
= tcg_temp_new_i64();
365 tcg_gen_atomic_cmpxchg_i64(tmp
, hex_llsc_addr
, hex_llsc_val_i64
, src
,
366 ctx
->mem_idx
, MO_64
);
367 tcg_gen_movcond_i64(TCG_COND_EQ
, tmp
, tmp
, hex_llsc_val_i64
,
369 tcg_gen_extrl_i64_i32(pred
, tmp
);
370 tcg_temp_free_i64(one
);
371 tcg_temp_free_i64(zero
);
372 tcg_temp_free_i64(tmp
);
376 tcg_gen_movi_tl(pred
, 0);
379 tcg_gen_movi_tl(hex_llsc_addr
, ~0);
382 static inline void gen_store32(TCGv vaddr
, TCGv src
, int width
, int slot
)
384 tcg_gen_mov_tl(hex_store_addr
[slot
], vaddr
);
385 tcg_gen_movi_tl(hex_store_width
[slot
], width
);
386 tcg_gen_mov_tl(hex_store_val32
[slot
], src
);
389 static inline void gen_store1(TCGv_env cpu_env
, TCGv vaddr
, TCGv src
,
390 DisasContext
*ctx
, int slot
)
392 gen_store32(vaddr
, src
, 1, slot
);
393 ctx
->store_width
[slot
] = 1;
396 static inline void gen_store1i(TCGv_env cpu_env
, TCGv vaddr
, int32_t src
,
397 DisasContext
*ctx
, int slot
)
399 TCGv tmp
= tcg_const_tl(src
);
400 gen_store1(cpu_env
, vaddr
, tmp
, ctx
, slot
);
404 static inline void gen_store2(TCGv_env cpu_env
, TCGv vaddr
, TCGv src
,
405 DisasContext
*ctx
, int slot
)
407 gen_store32(vaddr
, src
, 2, slot
);
408 ctx
->store_width
[slot
] = 2;
411 static inline void gen_store2i(TCGv_env cpu_env
, TCGv vaddr
, int32_t src
,
412 DisasContext
*ctx
, int slot
)
414 TCGv tmp
= tcg_const_tl(src
);
415 gen_store2(cpu_env
, vaddr
, tmp
, ctx
, slot
);
419 static inline void gen_store4(TCGv_env cpu_env
, TCGv vaddr
, TCGv src
,
420 DisasContext
*ctx
, int slot
)
422 gen_store32(vaddr
, src
, 4, slot
);
423 ctx
->store_width
[slot
] = 4;
426 static inline void gen_store4i(TCGv_env cpu_env
, TCGv vaddr
, int32_t src
,
427 DisasContext
*ctx
, int slot
)
429 TCGv tmp
= tcg_const_tl(src
);
430 gen_store4(cpu_env
, vaddr
, tmp
, ctx
, slot
);
434 static inline void gen_store8(TCGv_env cpu_env
, TCGv vaddr
, TCGv_i64 src
,
435 DisasContext
*ctx
, int slot
)
437 tcg_gen_mov_tl(hex_store_addr
[slot
], vaddr
);
438 tcg_gen_movi_tl(hex_store_width
[slot
], 8);
439 tcg_gen_mov_i64(hex_store_val64
[slot
], src
);
440 ctx
->store_width
[slot
] = 8;
443 static inline void gen_store8i(TCGv_env cpu_env
, TCGv vaddr
, int64_t src
,
444 DisasContext
*ctx
, int slot
)
446 TCGv_i64 tmp
= tcg_const_i64(src
);
447 gen_store8(cpu_env
, vaddr
, tmp
, ctx
, slot
);
448 tcg_temp_free_i64(tmp
);
451 static TCGv
gen_8bitsof(TCGv result
, TCGv value
)
453 TCGv zero
= tcg_const_tl(0);
454 TCGv ones
= tcg_const_tl(0xff);
455 tcg_gen_movcond_tl(TCG_COND_NE
, result
, value
, zero
, ones
, zero
);
462 #include "tcg_funcs_generated.c.inc"
463 #include "tcg_func_table_generated.c.inc"