blob: 7a9ee828e7e30d0c76b5443bccd39cb0784425e1 [file] [log] [blame]
Alexander Graf14ade102013-09-03 20:12:10 +01001/*
2 * AArch64 translation
3 *
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24
25#include "cpu.h"
26#include "tcg-op.h"
27#include "qemu/log.h"
28#include "translate.h"
29#include "qemu/host-utils.h"
30
Peter Maydell40f860c2013-12-17 19:42:31 +000031#include "exec/gen-icount.h"
32
Alexander Graf14ade102013-09-03 20:12:10 +010033#include "helper.h"
34#define GEN_HELPER 1
35#include "helper.h"
36
37static TCGv_i64 cpu_X[32];
38static TCGv_i64 cpu_pc;
Alexander Graf832ffa12013-12-17 19:42:34 +000039static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
Alexander Graf14ade102013-09-03 20:12:10 +010040
41static const char *regnames[] = {
42 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
43 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
44 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
45 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
46};
47
Alexander Graf832ffa12013-12-17 19:42:34 +000048enum a64_shift_type {
49 A64_SHIFT_TYPE_LSL = 0,
50 A64_SHIFT_TYPE_LSR = 1,
51 A64_SHIFT_TYPE_ASR = 2,
52 A64_SHIFT_TYPE_ROR = 3
53};
54
Alexander Graf14ade102013-09-03 20:12:10 +010055/* initialize TCG globals. */
56void a64_translate_init(void)
57{
58 int i;
59
60 cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
61 offsetof(CPUARMState, pc),
62 "pc");
63 for (i = 0; i < 32; i++) {
64 cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
65 offsetof(CPUARMState, xregs[i]),
66 regnames[i]);
67 }
68
Alexander Graf832ffa12013-12-17 19:42:34 +000069 cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
70 cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
71 cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
72 cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
Alexander Graf14ade102013-09-03 20:12:10 +010073}
74
75void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
76 fprintf_function cpu_fprintf, int flags)
77{
78 ARMCPU *cpu = ARM_CPU(cs);
79 CPUARMState *env = &cpu->env;
Peter Maydelld3563122013-12-17 19:42:30 +000080 uint32_t psr = pstate_read(env);
Alexander Graf14ade102013-09-03 20:12:10 +010081 int i;
82
83 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
84 env->pc, env->xregs[31]);
85 for (i = 0; i < 31; i++) {
86 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
87 if ((i % 4) == 3) {
88 cpu_fprintf(f, "\n");
89 } else {
90 cpu_fprintf(f, " ");
91 }
92 }
Peter Maydelld3563122013-12-17 19:42:30 +000093 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
94 psr,
95 psr & PSTATE_N ? 'N' : '-',
96 psr & PSTATE_Z ? 'Z' : '-',
97 psr & PSTATE_C ? 'C' : '-',
98 psr & PSTATE_V ? 'V' : '-');
Alexander Graf14ade102013-09-03 20:12:10 +010099 cpu_fprintf(f, "\n");
100}
101
Peter Maydell4a08d472013-12-22 22:32:27 +0000102static int get_mem_index(DisasContext *s)
103{
104#ifdef CONFIG_USER_ONLY
105 return 1;
106#else
107 return s->user;
108#endif
109}
110
Alexander Graf14ade102013-09-03 20:12:10 +0100111void gen_a64_set_pc_im(uint64_t val)
112{
113 tcg_gen_movi_i64(cpu_pc, val);
114}
115
116static void gen_exception(int excp)
117{
118 TCGv_i32 tmp = tcg_temp_new_i32();
119 tcg_gen_movi_i32(tmp, excp);
120 gen_helper_exception(cpu_env, tmp);
121 tcg_temp_free_i32(tmp);
122}
123
124static void gen_exception_insn(DisasContext *s, int offset, int excp)
125{
126 gen_a64_set_pc_im(s->pc - offset);
127 gen_exception(excp);
Peter Maydell40f860c2013-12-17 19:42:31 +0000128 s->is_jmp = DISAS_EXC;
129}
130
131static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
132{
133 /* No direct tb linking with singlestep or deterministic io */
134 if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
135 return false;
136 }
137
138 /* Only link tbs from inside the same guest page */
139 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
140 return false;
141 }
142
143 return true;
144}
145
146static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
147{
148 TranslationBlock *tb;
149
150 tb = s->tb;
151 if (use_goto_tb(s, n, dest)) {
152 tcg_gen_goto_tb(n);
153 gen_a64_set_pc_im(dest);
154 tcg_gen_exit_tb((tcg_target_long)tb + n);
155 s->is_jmp = DISAS_TB_JUMP;
156 } else {
157 gen_a64_set_pc_im(dest);
158 if (s->singlestep_enabled) {
159 gen_exception(EXCP_DEBUG);
160 }
161 tcg_gen_exit_tb(0);
162 s->is_jmp = DISAS_JUMP;
163 }
Alexander Graf14ade102013-09-03 20:12:10 +0100164}
165
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000166static void unallocated_encoding(DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +0100167{
Alexander Graf14ade102013-09-03 20:12:10 +0100168 gen_exception_insn(s, 4, EXCP_UDEF);
169}
170
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000171#define unsupported_encoding(s, insn) \
172 do { \
173 qemu_log_mask(LOG_UNIMP, \
174 "%s:%d: unsupported instruction encoding 0x%08x " \
175 "at pc=%016" PRIx64 "\n", \
176 __FILE__, __LINE__, insn, s->pc - 4); \
177 unallocated_encoding(s); \
178 } while (0);
Alexander Graf14ade102013-09-03 20:12:10 +0100179
Alexander Graf11e169d2013-12-17 19:42:32 +0000180static void init_tmp_a64_array(DisasContext *s)
181{
182#ifdef CONFIG_DEBUG_TCG
183 int i;
184 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
185 TCGV_UNUSED_I64(s->tmp_a64[i]);
186 }
187#endif
188 s->tmp_a64_count = 0;
189}
190
191static void free_tmp_a64(DisasContext *s)
192{
193 int i;
194 for (i = 0; i < s->tmp_a64_count; i++) {
195 tcg_temp_free_i64(s->tmp_a64[i]);
196 }
197 init_tmp_a64_array(s);
198}
199
200static TCGv_i64 new_tmp_a64(DisasContext *s)
201{
202 assert(s->tmp_a64_count < TMP_A64_MAX);
203 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
204}
205
206static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
207{
208 TCGv_i64 t = new_tmp_a64(s);
209 tcg_gen_movi_i64(t, 0);
210 return t;
211}
212
Alexander Graf71b46082013-12-17 19:42:36 +0000213/*
214 * Register access functions
215 *
216 * These functions are used for directly accessing a register in where
217 * changes to the final register value are likely to be made. If you
218 * need to use a register for temporary calculation (e.g. index type
219 * operations) use the read_* form.
220 *
221 * B1.2.1 Register mappings
222 *
223 * In instruction register encoding 31 can refer to ZR (zero register) or
224 * the SP (stack pointer) depending on context. In QEMU's case we map SP
225 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
226 * This is the point of the _sp forms.
227 */
Alexander Graf11e169d2013-12-17 19:42:32 +0000228static TCGv_i64 cpu_reg(DisasContext *s, int reg)
229{
230 if (reg == 31) {
231 return new_tmp_a64_zero(s);
232 } else {
233 return cpu_X[reg];
234 }
235}
236
Alexander Graf71b46082013-12-17 19:42:36 +0000237/* register access for when 31 == SP */
238static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
239{
240 return cpu_X[reg];
241}
242
Alexander Graf60e53382013-12-17 19:42:33 +0000243/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
244 * representing the register contents. This TCGv is an auto-freed
245 * temporary so it need not be explicitly freed, and may be modified.
246 */
247static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
248{
249 TCGv_i64 v = new_tmp_a64(s);
250 if (reg != 31) {
251 if (sf) {
252 tcg_gen_mov_i64(v, cpu_X[reg]);
253 } else {
254 tcg_gen_ext32u_i64(v, cpu_X[reg]);
255 }
256 } else {
257 tcg_gen_movi_i64(v, 0);
258 }
259 return v;
260}
261
Peter Maydell4a08d472013-12-22 22:32:27 +0000262static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
263{
264 TCGv_i64 v = new_tmp_a64(s);
265 if (sf) {
266 tcg_gen_mov_i64(v, cpu_X[reg]);
267 } else {
268 tcg_gen_ext32u_i64(v, cpu_X[reg]);
269 }
270 return v;
271}
272
Alexander Graf832ffa12013-12-17 19:42:34 +0000273/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
274 * than the 32 bit equivalent.
275 */
276static inline void gen_set_NZ64(TCGv_i64 result)
277{
278 TCGv_i64 flag = tcg_temp_new_i64();
279
280 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
281 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
282 tcg_gen_shri_i64(flag, result, 32);
283 tcg_gen_trunc_i64_i32(cpu_NF, flag);
284 tcg_temp_free_i64(flag);
285}
286
287/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
288static inline void gen_logic_CC(int sf, TCGv_i64 result)
289{
290 if (sf) {
291 gen_set_NZ64(result);
292 } else {
293 tcg_gen_trunc_i64_i32(cpu_ZF, result);
294 tcg_gen_trunc_i64_i32(cpu_NF, result);
295 }
296 tcg_gen_movi_i32(cpu_CF, 0);
297 tcg_gen_movi_i32(cpu_VF, 0);
298}
299
Alex Bennéeb0ff21b2013-12-23 23:27:29 +0000300/* dest = T0 + T1; compute C, N, V and Z flags */
301static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
302{
303 if (sf) {
304 TCGv_i64 result, flag, tmp;
305 result = tcg_temp_new_i64();
306 flag = tcg_temp_new_i64();
307 tmp = tcg_temp_new_i64();
308
309 tcg_gen_movi_i64(tmp, 0);
310 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
311
312 tcg_gen_trunc_i64_i32(cpu_CF, flag);
313
314 gen_set_NZ64(result);
315
316 tcg_gen_xor_i64(flag, result, t0);
317 tcg_gen_xor_i64(tmp, t0, t1);
318 tcg_gen_andc_i64(flag, flag, tmp);
319 tcg_temp_free_i64(tmp);
320 tcg_gen_shri_i64(flag, flag, 32);
321 tcg_gen_trunc_i64_i32(cpu_VF, flag);
322
323 tcg_gen_mov_i64(dest, result);
324 tcg_temp_free_i64(result);
325 tcg_temp_free_i64(flag);
326 } else {
327 /* 32 bit arithmetic */
328 TCGv_i32 t0_32 = tcg_temp_new_i32();
329 TCGv_i32 t1_32 = tcg_temp_new_i32();
330 TCGv_i32 tmp = tcg_temp_new_i32();
331
332 tcg_gen_movi_i32(tmp, 0);
333 tcg_gen_trunc_i64_i32(t0_32, t0);
334 tcg_gen_trunc_i64_i32(t1_32, t1);
335 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
336 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
337 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
338 tcg_gen_xor_i32(tmp, t0_32, t1_32);
339 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
340 tcg_gen_extu_i32_i64(dest, cpu_NF);
341
342 tcg_temp_free_i32(tmp);
343 tcg_temp_free_i32(t0_32);
344 tcg_temp_free_i32(t1_32);
345 }
346}
347
348/* dest = T0 - T1; compute C, N, V and Z flags */
349static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
350{
351 if (sf) {
352 /* 64 bit arithmetic */
353 TCGv_i64 result, flag, tmp;
354
355 result = tcg_temp_new_i64();
356 flag = tcg_temp_new_i64();
357 tcg_gen_sub_i64(result, t0, t1);
358
359 gen_set_NZ64(result);
360
361 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
362 tcg_gen_trunc_i64_i32(cpu_CF, flag);
363
364 tcg_gen_xor_i64(flag, result, t0);
365 tmp = tcg_temp_new_i64();
366 tcg_gen_xor_i64(tmp, t0, t1);
367 tcg_gen_and_i64(flag, flag, tmp);
368 tcg_temp_free_i64(tmp);
369 tcg_gen_shri_i64(flag, flag, 32);
370 tcg_gen_trunc_i64_i32(cpu_VF, flag);
371 tcg_gen_mov_i64(dest, result);
372 tcg_temp_free_i64(flag);
373 tcg_temp_free_i64(result);
374 } else {
375 /* 32 bit arithmetic */
376 TCGv_i32 t0_32 = tcg_temp_new_i32();
377 TCGv_i32 t1_32 = tcg_temp_new_i32();
378 TCGv_i32 tmp;
379
380 tcg_gen_trunc_i64_i32(t0_32, t0);
381 tcg_gen_trunc_i64_i32(t1_32, t1);
382 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
383 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
384 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
385 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
386 tmp = tcg_temp_new_i32();
387 tcg_gen_xor_i32(tmp, t0_32, t1_32);
388 tcg_temp_free_i32(t0_32);
389 tcg_temp_free_i32(t1_32);
390 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
391 tcg_temp_free_i32(tmp);
392 tcg_gen_extu_i32_i64(dest, cpu_NF);
393 }
394}
395
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000396/*
Peter Maydell4a08d472013-12-22 22:32:27 +0000397 * Load/Store generators
398 */
399
400/*
401 * Store from GPR register to memory
402 */
403static void do_gpr_st(DisasContext *s, TCGv_i64 source,
404 TCGv_i64 tcg_addr, int size)
405{
406 g_assert(size <= 3);
407 tcg_gen_qemu_st_i64(source, tcg_addr, get_mem_index(s), MO_TE + size);
408}
409
410/*
411 * Load from memory to GPR register
412 */
413static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
414 int size, bool is_signed, bool extend)
415{
416 TCGMemOp memop = MO_TE + size;
417
418 g_assert(size <= 3);
419
420 if (is_signed) {
421 memop += MO_SIGN;
422 }
423
424 tcg_gen_qemu_ld_i64(dest, tcg_addr, get_mem_index(s), memop);
425
426 if (extend && is_signed) {
427 g_assert(size < 3);
428 tcg_gen_ext32u_i64(dest, dest);
429 }
430}
431
432/*
433 * Store from FP register to memory
434 */
435static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
436{
437 /* This writes the bottom N bits of a 128 bit wide vector to memory */
438 int freg_offs = offsetof(CPUARMState, vfp.regs[srcidx * 2]);
439 TCGv_i64 tmp = tcg_temp_new_i64();
440
441 if (size < 4) {
442 switch (size) {
443 case 0:
444 tcg_gen_ld8u_i64(tmp, cpu_env, freg_offs);
445 break;
446 case 1:
447 tcg_gen_ld16u_i64(tmp, cpu_env, freg_offs);
448 break;
449 case 2:
450 tcg_gen_ld32u_i64(tmp, cpu_env, freg_offs);
451 break;
452 case 3:
453 tcg_gen_ld_i64(tmp, cpu_env, freg_offs);
454 break;
455 }
456 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
457 } else {
458 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
459 tcg_gen_ld_i64(tmp, cpu_env, freg_offs);
460 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
461 tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
462 tcg_gen_ld_i64(tmp, cpu_env, freg_offs + sizeof(float64));
463 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
464 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
465 tcg_temp_free_i64(tcg_hiaddr);
466 }
467
468 tcg_temp_free_i64(tmp);
469}
470
471/*
472 * Load from memory to FP register
473 */
474static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
475{
476 /* This always zero-extends and writes to a full 128 bit wide vector */
477 int freg_offs = offsetof(CPUARMState, vfp.regs[destidx * 2]);
478 TCGv_i64 tmplo = tcg_temp_new_i64();
479 TCGv_i64 tmphi;
480
481 if (size < 4) {
482 TCGMemOp memop = MO_TE + size;
483 tmphi = tcg_const_i64(0);
484 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
485 } else {
486 TCGv_i64 tcg_hiaddr;
487 tmphi = tcg_temp_new_i64();
488 tcg_hiaddr = tcg_temp_new_i64();
489
490 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
491 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
492 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
493 tcg_temp_free_i64(tcg_hiaddr);
494 }
495
496 tcg_gen_st_i64(tmplo, cpu_env, freg_offs);
497 tcg_gen_st_i64(tmphi, cpu_env, freg_offs + sizeof(float64));
498
499 tcg_temp_free_i64(tmplo);
500 tcg_temp_free_i64(tmphi);
501}
502
Alex Bennée229b7a02013-12-23 23:27:29 +0000503/*
504 * This utility function is for doing register extension with an
505 * optional shift. You will likely want to pass a temporary for the
506 * destination register. See DecodeRegExtend() in the ARM ARM.
507 */
508static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
509 int option, unsigned int shift)
510{
511 int extsize = extract32(option, 0, 2);
512 bool is_signed = extract32(option, 2, 1);
513
514 if (is_signed) {
515 switch (extsize) {
516 case 0:
517 tcg_gen_ext8s_i64(tcg_out, tcg_in);
518 break;
519 case 1:
520 tcg_gen_ext16s_i64(tcg_out, tcg_in);
521 break;
522 case 2:
523 tcg_gen_ext32s_i64(tcg_out, tcg_in);
524 break;
525 case 3:
526 tcg_gen_mov_i64(tcg_out, tcg_in);
527 break;
528 }
529 } else {
530 switch (extsize) {
531 case 0:
532 tcg_gen_ext8u_i64(tcg_out, tcg_in);
533 break;
534 case 1:
535 tcg_gen_ext16u_i64(tcg_out, tcg_in);
536 break;
537 case 2:
538 tcg_gen_ext32u_i64(tcg_out, tcg_in);
539 break;
540 case 3:
541 tcg_gen_mov_i64(tcg_out, tcg_in);
542 break;
543 }
544 }
545
546 if (shift) {
547 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
548 }
549}
550
Peter Maydell4a08d472013-12-22 22:32:27 +0000551static inline void gen_check_sp_alignment(DisasContext *s)
552{
553 /* The AArch64 architecture mandates that (if enabled via PSTATE
554 * or SCTLR bits) there is a check that SP is 16-aligned on every
555 * SP-relative load or store (with an exception generated if it is not).
556 * In line with general QEMU practice regarding misaligned accesses,
557 * we omit these checks for the sake of guest program performance.
558 * This function is provided as a hook so we can more easily add these
559 * checks in future (possibly as a "favour catching guest program bugs
560 * over speed" user selectable option).
561 */
562}
563
564/*
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000565 * the instruction disassembly implemented here matches
566 * the instruction encoding classifications in chapter 3 (C3)
567 * of the ARM Architecture Reference Manual (DDI0487A_a)
568 */
569
Alexander Graf11e169d2013-12-17 19:42:32 +0000570/* C3.2.7 Unconditional branch (immediate)
571 * 31 30 26 25 0
572 * +----+-----------+-------------------------------------+
573 * | op | 0 0 1 0 1 | imm26 |
574 * +----+-----------+-------------------------------------+
575 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000576static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
577{
Alexander Graf11e169d2013-12-17 19:42:32 +0000578 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
579
580 if (insn & (1 << 31)) {
581 /* C5.6.26 BL Branch with link */
582 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
583 }
584
585 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
586 gen_goto_tb(s, 0, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000587}
588
Alexander Graf60e53382013-12-17 19:42:33 +0000589/* C3.2.1 Compare & branch (immediate)
590 * 31 30 25 24 23 5 4 0
591 * +----+-------------+----+---------------------+--------+
592 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
593 * +----+-------------+----+---------------------+--------+
594 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000595static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
596{
Alexander Graf60e53382013-12-17 19:42:33 +0000597 unsigned int sf, op, rt;
598 uint64_t addr;
599 int label_match;
600 TCGv_i64 tcg_cmp;
601
602 sf = extract32(insn, 31, 1);
603 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
604 rt = extract32(insn, 0, 5);
605 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
606
607 tcg_cmp = read_cpu_reg(s, rt, sf);
608 label_match = gen_new_label();
609
610 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
611 tcg_cmp, 0, label_match);
612
613 gen_goto_tb(s, 0, s->pc);
614 gen_set_label(label_match);
615 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000616}
617
Alexander Grafdb0f7952013-12-17 19:42:33 +0000618/* C3.2.5 Test & branch (immediate)
619 * 31 30 25 24 23 19 18 5 4 0
620 * +----+-------------+----+-------+-------------+------+
621 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
622 * +----+-------------+----+-------+-------------+------+
623 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000624static void disas_test_b_imm(DisasContext *s, uint32_t insn)
625{
Alexander Grafdb0f7952013-12-17 19:42:33 +0000626 unsigned int bit_pos, op, rt;
627 uint64_t addr;
628 int label_match;
629 TCGv_i64 tcg_cmp;
630
631 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
632 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
633 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
634 rt = extract32(insn, 0, 5);
635
636 tcg_cmp = tcg_temp_new_i64();
637 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
638 label_match = gen_new_label();
639 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
640 tcg_cmp, 0, label_match);
641 tcg_temp_free_i64(tcg_cmp);
642 gen_goto_tb(s, 0, s->pc);
643 gen_set_label(label_match);
644 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000645}
646
Alexander Graf39fb7302013-12-17 19:42:33 +0000647/* C3.2.2 / C5.6.19 Conditional branch (immediate)
648 * 31 25 24 23 5 4 3 0
649 * +---------------+----+---------------------+----+------+
650 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
651 * +---------------+----+---------------------+----+------+
652 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000653static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
654{
Alexander Graf39fb7302013-12-17 19:42:33 +0000655 unsigned int cond;
656 uint64_t addr;
657
658 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
659 unallocated_encoding(s);
660 return;
661 }
662 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
663 cond = extract32(insn, 0, 4);
664
665 if (cond < 0x0e) {
666 /* genuinely conditional branches */
667 int label_match = gen_new_label();
668 arm_gen_test_cc(cond, label_match);
669 gen_goto_tb(s, 0, s->pc);
670 gen_set_label(label_match);
671 gen_goto_tb(s, 1, addr);
672 } else {
673 /* 0xe and 0xf are both "always" conditions */
674 gen_goto_tb(s, 0, addr);
675 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000676}
677
Claudio Fontana87462e02013-12-17 19:42:32 +0000678/* C5.6.68 HINT */
679static void handle_hint(DisasContext *s, uint32_t insn,
680 unsigned int op1, unsigned int op2, unsigned int crm)
681{
682 unsigned int selector = crm << 3 | op2;
683
684 if (op1 != 3) {
685 unallocated_encoding(s);
686 return;
687 }
688
689 switch (selector) {
690 case 0: /* NOP */
691 return;
692 case 1: /* YIELD */
693 case 2: /* WFE */
694 case 3: /* WFI */
695 case 4: /* SEV */
696 case 5: /* SEVL */
697 /* we treat all as NOP at least for now */
698 return;
699 default:
700 /* default specified as NOP equivalent */
701 return;
702 }
703}
704
705/* CLREX, DSB, DMB, ISB */
706static void handle_sync(DisasContext *s, uint32_t insn,
707 unsigned int op1, unsigned int op2, unsigned int crm)
708{
709 if (op1 != 3) {
710 unallocated_encoding(s);
711 return;
712 }
713
714 switch (op2) {
715 case 2: /* CLREX */
716 unsupported_encoding(s, insn);
717 return;
718 case 4: /* DSB */
719 case 5: /* DMB */
720 case 6: /* ISB */
721 /* We don't emulate caches so barriers are no-ops */
722 return;
723 default:
724 unallocated_encoding(s);
725 return;
726 }
727}
728
729/* C5.6.130 MSR (immediate) - move immediate to processor state field */
730static void handle_msr_i(DisasContext *s, uint32_t insn,
731 unsigned int op1, unsigned int op2, unsigned int crm)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000732{
733 unsupported_encoding(s, insn);
734}
735
Peter Maydellfea50522014-01-04 22:15:45 +0000736/* C5.6.129 MRS - move from system register
737 * C5.6.131 MSR (register) - move to system register
738 * C5.6.204 SYS
739 * C5.6.205 SYSL
740 * These are all essentially the same insn in 'read' and 'write'
741 * versions, with varying op0 fields.
742 */
743static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
744 unsigned int op0, unsigned int op1, unsigned int op2,
Claudio Fontana87462e02013-12-17 19:42:32 +0000745 unsigned int crn, unsigned int crm, unsigned int rt)
746{
Peter Maydellfea50522014-01-04 22:15:45 +0000747 const ARMCPRegInfo *ri;
748 TCGv_i64 tcg_rt;
Claudio Fontana87462e02013-12-17 19:42:32 +0000749
Peter Maydellfea50522014-01-04 22:15:45 +0000750 ri = get_arm_cp_reginfo(s->cp_regs,
751 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
752 crn, crm, op0, op1, op2));
Claudio Fontana87462e02013-12-17 19:42:32 +0000753
Peter Maydellfea50522014-01-04 22:15:45 +0000754 if (!ri) {
755 /* Unknown register */
756 unallocated_encoding(s);
757 return;
758 }
759
760 /* Check access permissions */
761 if (!cp_access_ok(s->current_pl, ri, isread)) {
762 unallocated_encoding(s);
763 return;
764 }
765
766 /* Handle special cases first */
767 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
768 case ARM_CP_NOP:
769 return;
770 default:
771 break;
772 }
773
774 if (use_icount && (ri->type & ARM_CP_IO)) {
775 gen_io_start();
776 }
777
778 tcg_rt = cpu_reg(s, rt);
779
780 if (isread) {
781 if (ri->type & ARM_CP_CONST) {
782 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
783 } else if (ri->readfn) {
784 TCGv_ptr tmpptr;
785 gen_a64_set_pc_im(s->pc - 4);
786 tmpptr = tcg_const_ptr(ri);
787 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
788 tcg_temp_free_ptr(tmpptr);
789 } else {
790 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
791 }
792 } else {
793 if (ri->type & ARM_CP_CONST) {
794 /* If not forbidden by access permissions, treat as WI */
795 return;
796 } else if (ri->writefn) {
797 TCGv_ptr tmpptr;
798 gen_a64_set_pc_im(s->pc - 4);
799 tmpptr = tcg_const_ptr(ri);
800 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
801 tcg_temp_free_ptr(tmpptr);
802 } else {
803 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
804 }
805 }
806
807 if (use_icount && (ri->type & ARM_CP_IO)) {
808 /* I/O operations must end the TB here (whether read or write) */
809 gen_io_end();
810 s->is_jmp = DISAS_UPDATE;
811 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
812 /* We default to ending the TB on a coprocessor register write,
813 * but allow this to be suppressed by the register definition
814 * (usually only necessary to work around guest bugs).
815 */
816 s->is_jmp = DISAS_UPDATE;
817 }
Claudio Fontana87462e02013-12-17 19:42:32 +0000818}
819
820/* C3.2.4 System
821 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
822 * +---------------------+---+-----+-----+-------+-------+-----+------+
823 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
824 * +---------------------+---+-----+-----+-------+-------+-----+------+
825 */
826static void disas_system(DisasContext *s, uint32_t insn)
827{
828 unsigned int l, op0, op1, crn, crm, op2, rt;
829 l = extract32(insn, 21, 1);
830 op0 = extract32(insn, 19, 2);
831 op1 = extract32(insn, 16, 3);
832 crn = extract32(insn, 12, 4);
833 crm = extract32(insn, 8, 4);
834 op2 = extract32(insn, 5, 3);
835 rt = extract32(insn, 0, 5);
836
837 if (op0 == 0) {
838 if (l || rt != 31) {
839 unallocated_encoding(s);
840 return;
841 }
842 switch (crn) {
843 case 2: /* C5.6.68 HINT */
844 handle_hint(s, insn, op1, op2, crm);
845 break;
846 case 3: /* CLREX, DSB, DMB, ISB */
847 handle_sync(s, insn, op1, op2, crm);
848 break;
849 case 4: /* C5.6.130 MSR (immediate) */
850 handle_msr_i(s, insn, op1, op2, crm);
851 break;
852 default:
853 unallocated_encoding(s);
854 break;
855 }
856 return;
857 }
Peter Maydellfea50522014-01-04 22:15:45 +0000858 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
Claudio Fontana87462e02013-12-17 19:42:32 +0000859}
860
Alexander Graf9618e802013-12-23 23:27:30 +0000861/* C3.2.3 Exception generation
862 *
863 * 31 24 23 21 20 5 4 2 1 0
864 * +-----------------+-----+------------------------+-----+----+
865 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
866 * +-----------------------+------------------------+----------+
867 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000868static void disas_exc(DisasContext *s, uint32_t insn)
869{
Alexander Graf9618e802013-12-23 23:27:30 +0000870 int opc = extract32(insn, 21, 3);
871 int op2_ll = extract32(insn, 0, 5);
872
873 switch (opc) {
874 case 0:
875 /* SVC, HVC, SMC; since we don't support the Virtualization
876 * or TrustZone extensions these all UNDEF except SVC.
877 */
878 if (op2_ll != 1) {
879 unallocated_encoding(s);
880 break;
881 }
882 gen_exception_insn(s, 0, EXCP_SWI);
883 break;
884 case 1:
885 if (op2_ll != 0) {
886 unallocated_encoding(s);
887 break;
888 }
889 /* BRK */
890 gen_exception_insn(s, 0, EXCP_BKPT);
891 break;
892 case 2:
893 if (op2_ll != 0) {
894 unallocated_encoding(s);
895 break;
896 }
897 /* HLT */
898 unsupported_encoding(s, insn);
899 break;
900 case 5:
901 if (op2_ll < 1 || op2_ll > 3) {
902 unallocated_encoding(s);
903 break;
904 }
905 /* DCPS1, DCPS2, DCPS3 */
906 unsupported_encoding(s, insn);
907 break;
908 default:
909 unallocated_encoding(s);
910 break;
911 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000912}
913
Alexander Grafb001c8c2013-12-17 19:42:33 +0000914/* C3.2.7 Unconditional branch (register)
915 * 31 25 24 21 20 16 15 10 9 5 4 0
916 * +---------------+-------+-------+-------+------+-------+
917 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
918 * +---------------+-------+-------+-------+------+-------+
919 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000920static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
921{
Alexander Grafb001c8c2013-12-17 19:42:33 +0000922 unsigned int opc, op2, op3, rn, op4;
923
924 opc = extract32(insn, 21, 4);
925 op2 = extract32(insn, 16, 5);
926 op3 = extract32(insn, 10, 6);
927 rn = extract32(insn, 5, 5);
928 op4 = extract32(insn, 0, 5);
929
930 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
931 unallocated_encoding(s);
932 return;
933 }
934
935 switch (opc) {
936 case 0: /* BR */
937 case 2: /* RET */
938 break;
939 case 1: /* BLR */
940 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
941 break;
942 case 4: /* ERET */
943 case 5: /* DRPS */
944 if (rn != 0x1f) {
945 unallocated_encoding(s);
946 } else {
947 unsupported_encoding(s, insn);
948 }
949 return;
950 default:
951 unallocated_encoding(s);
952 return;
953 }
954
955 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
956 s->is_jmp = DISAS_JUMP;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000957}
958
959/* C3.2 Branches, exception generating and system instructions */
960static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
961{
962 switch (extract32(insn, 25, 7)) {
963 case 0x0a: case 0x0b:
964 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
965 disas_uncond_b_imm(s, insn);
966 break;
967 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
968 disas_comp_b_imm(s, insn);
969 break;
970 case 0x1b: case 0x5b: /* Test & branch (immediate) */
971 disas_test_b_imm(s, insn);
972 break;
973 case 0x2a: /* Conditional branch (immediate) */
974 disas_cond_b_imm(s, insn);
975 break;
976 case 0x6a: /* Exception generation / System */
977 if (insn & (1 << 24)) {
978 disas_system(s, insn);
979 } else {
980 disas_exc(s, insn);
981 }
982 break;
983 case 0x6b: /* Unconditional branch (register) */
984 disas_uncond_b_reg(s, insn);
985 break;
986 default:
987 unallocated_encoding(s);
988 break;
989 }
990}
991
992/* Load/store exclusive */
993static void disas_ldst_excl(DisasContext *s, uint32_t insn)
994{
995 unsupported_encoding(s, insn);
996}
997
998/* Load register (literal) */
999static void disas_ld_lit(DisasContext *s, uint32_t insn)
1000{
1001 unsupported_encoding(s, insn);
1002}
1003
Peter Maydell4a08d472013-12-22 22:32:27 +00001004/*
1005 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1006 * C5.6.81 LDP (Load Pair - non vector)
1007 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1008 * C5.6.176 STNP (Store Pair - non-temporal hint)
1009 * C5.6.177 STP (Store Pair - non vector)
1010 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1011 * C6.3.165 LDP (Load Pair of SIMD&FP)
1012 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1013 * C6.3.284 STP (Store Pair of SIMD&FP)
1014 *
1015 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1016 * +-----+-------+---+---+-------+---+-----------------------------+
1017 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1018 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1019 *
1020 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1021 * LDPSW 01
1022 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1023 * V: 0 -> GPR, 1 -> Vector
1024 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1025 * 10 -> signed offset, 11 -> pre-index
1026 * L: 0 -> Store 1 -> Load
1027 *
1028 * Rt, Rt2 = GPR or SIMD registers to be stored
1029 * Rn = general purpose register containing address
1030 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1031 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001032static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1033{
Peter Maydell4a08d472013-12-22 22:32:27 +00001034 int rt = extract32(insn, 0, 5);
1035 int rn = extract32(insn, 5, 5);
1036 int rt2 = extract32(insn, 10, 5);
1037 int64_t offset = sextract32(insn, 15, 7);
1038 int index = extract32(insn, 23, 2);
1039 bool is_vector = extract32(insn, 26, 1);
1040 bool is_load = extract32(insn, 22, 1);
1041 int opc = extract32(insn, 30, 2);
1042
1043 bool is_signed = false;
1044 bool postindex = false;
1045 bool wback = false;
1046
1047 TCGv_i64 tcg_addr; /* calculated address */
1048 int size;
1049
1050 if (opc == 3) {
1051 unallocated_encoding(s);
1052 return;
1053 }
1054
1055 if (is_vector) {
1056 size = 2 + opc;
1057 } else {
1058 size = 2 + extract32(opc, 1, 1);
1059 is_signed = extract32(opc, 0, 1);
1060 if (!is_load && is_signed) {
1061 unallocated_encoding(s);
1062 return;
1063 }
1064 }
1065
1066 switch (index) {
1067 case 1: /* post-index */
1068 postindex = true;
1069 wback = true;
1070 break;
1071 case 0:
1072 /* signed offset with "non-temporal" hint. Since we don't emulate
1073 * caches we don't care about hints to the cache system about
1074 * data access patterns, and handle this identically to plain
1075 * signed offset.
1076 */
1077 if (is_signed) {
1078 /* There is no non-temporal-hint version of LDPSW */
1079 unallocated_encoding(s);
1080 return;
1081 }
1082 postindex = false;
1083 break;
1084 case 2: /* signed offset, rn not updated */
1085 postindex = false;
1086 break;
1087 case 3: /* pre-index */
1088 postindex = false;
1089 wback = true;
1090 break;
1091 }
1092
1093 offset <<= size;
1094
1095 if (rn == 31) {
1096 gen_check_sp_alignment(s);
1097 }
1098
1099 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1100
1101 if (!postindex) {
1102 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1103 }
1104
1105 if (is_vector) {
1106 if (is_load) {
1107 do_fp_ld(s, rt, tcg_addr, size);
1108 } else {
1109 do_fp_st(s, rt, tcg_addr, size);
1110 }
1111 } else {
1112 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1113 if (is_load) {
1114 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1115 } else {
1116 do_gpr_st(s, tcg_rt, tcg_addr, size);
1117 }
1118 }
1119 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1120 if (is_vector) {
1121 if (is_load) {
1122 do_fp_ld(s, rt2, tcg_addr, size);
1123 } else {
1124 do_fp_st(s, rt2, tcg_addr, size);
1125 }
1126 } else {
1127 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1128 if (is_load) {
1129 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
1130 } else {
1131 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1132 }
1133 }
1134
1135 if (wback) {
1136 if (postindex) {
1137 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
1138 } else {
1139 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
1140 }
1141 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
1142 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001143}
1144
Alex Bennéed5612f12013-12-23 23:27:28 +00001145/*
Alex Bennéea5e94a92013-12-23 23:27:29 +00001146 * C3.3.8 Load/store (immediate post-indexed)
1147 * C3.3.9 Load/store (immediate pre-indexed)
1148 * C3.3.12 Load/store (unscaled immediate)
1149 *
1150 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
1151 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1152 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
1153 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1154 *
1155 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
1156 * V = 0 -> non-vector
1157 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
1158 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1159 */
1160static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
1161{
1162 int rt = extract32(insn, 0, 5);
1163 int rn = extract32(insn, 5, 5);
1164 int imm9 = sextract32(insn, 12, 9);
1165 int opc = extract32(insn, 22, 2);
1166 int size = extract32(insn, 30, 2);
1167 int idx = extract32(insn, 10, 2);
1168 bool is_signed = false;
1169 bool is_store = false;
1170 bool is_extended = false;
1171 bool is_vector = extract32(insn, 26, 1);
1172 bool post_index;
1173 bool writeback;
1174
1175 TCGv_i64 tcg_addr;
1176
1177 if (is_vector) {
1178 size |= (opc & 2) << 1;
1179 if (size > 4) {
1180 unallocated_encoding(s);
1181 return;
1182 }
1183 is_store = ((opc & 1) == 0);
1184 } else {
1185 if (size == 3 && opc == 2) {
1186 /* PRFM - prefetch */
1187 return;
1188 }
1189 if (opc == 3 && size > 1) {
1190 unallocated_encoding(s);
1191 return;
1192 }
1193 is_store = (opc == 0);
1194 is_signed = opc & (1<<1);
1195 is_extended = (size < 3) && (opc & 1);
1196 }
1197
1198 switch (idx) {
1199 case 0:
1200 post_index = false;
1201 writeback = false;
1202 break;
1203 case 1:
1204 post_index = true;
1205 writeback = true;
1206 break;
1207 case 3:
1208 post_index = false;
1209 writeback = true;
1210 break;
1211 case 2:
1212 g_assert(false);
1213 break;
1214 }
1215
1216 if (rn == 31) {
1217 gen_check_sp_alignment(s);
1218 }
1219 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1220
1221 if (!post_index) {
1222 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1223 }
1224
1225 if (is_vector) {
1226 if (is_store) {
1227 do_fp_st(s, rt, tcg_addr, size);
1228 } else {
1229 do_fp_ld(s, rt, tcg_addr, size);
1230 }
1231 } else {
1232 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1233 if (is_store) {
1234 do_gpr_st(s, tcg_rt, tcg_addr, size);
1235 } else {
1236 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1237 }
1238 }
1239
1240 if (writeback) {
1241 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1242 if (post_index) {
1243 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1244 }
1245 tcg_gen_mov_i64(tcg_rn, tcg_addr);
1246 }
1247}
1248
1249/*
Alex Bennée229b7a02013-12-23 23:27:29 +00001250 * C3.3.10 Load/store (register offset)
1251 *
1252 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
1253 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1254 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
1255 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1256 *
1257 * For non-vector:
1258 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1259 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1260 * For vector:
1261 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1262 * opc<0>: 0 -> store, 1 -> load
1263 * V: 1 -> vector/simd
1264 * opt: extend encoding (see DecodeRegExtend)
1265 * S: if S=1 then scale (essentially index by sizeof(size))
1266 * Rt: register to transfer into/out of
1267 * Rn: address register or SP for base
1268 * Rm: offset register or ZR for offset
1269 */
1270static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
1271{
1272 int rt = extract32(insn, 0, 5);
1273 int rn = extract32(insn, 5, 5);
1274 int shift = extract32(insn, 12, 1);
1275 int rm = extract32(insn, 16, 5);
1276 int opc = extract32(insn, 22, 2);
1277 int opt = extract32(insn, 13, 3);
1278 int size = extract32(insn, 30, 2);
1279 bool is_signed = false;
1280 bool is_store = false;
1281 bool is_extended = false;
1282 bool is_vector = extract32(insn, 26, 1);
1283
1284 TCGv_i64 tcg_rm;
1285 TCGv_i64 tcg_addr;
1286
1287 if (extract32(opt, 1, 1) == 0) {
1288 unallocated_encoding(s);
1289 return;
1290 }
1291
1292 if (is_vector) {
1293 size |= (opc & 2) << 1;
1294 if (size > 4) {
1295 unallocated_encoding(s);
1296 return;
1297 }
1298 is_store = !extract32(opc, 0, 1);
1299 } else {
1300 if (size == 3 && opc == 2) {
1301 /* PRFM - prefetch */
1302 return;
1303 }
1304 if (opc == 3 && size > 1) {
1305 unallocated_encoding(s);
1306 return;
1307 }
1308 is_store = (opc == 0);
1309 is_signed = extract32(opc, 1, 1);
1310 is_extended = (size < 3) && extract32(opc, 0, 1);
1311 }
1312
1313 if (rn == 31) {
1314 gen_check_sp_alignment(s);
1315 }
1316 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1317
1318 tcg_rm = read_cpu_reg(s, rm, 1);
1319 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
1320
1321 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
1322
1323 if (is_vector) {
1324 if (is_store) {
1325 do_fp_st(s, rt, tcg_addr, size);
1326 } else {
1327 do_fp_ld(s, rt, tcg_addr, size);
1328 }
1329 } else {
1330 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1331 if (is_store) {
1332 do_gpr_st(s, tcg_rt, tcg_addr, size);
1333 } else {
1334 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1335 }
1336 }
1337}
1338
1339/*
Alex Bennéed5612f12013-12-23 23:27:28 +00001340 * C3.3.13 Load/store (unsigned immediate)
1341 *
1342 * 31 30 29 27 26 25 24 23 22 21 10 9 5
1343 * +----+-------+---+-----+-----+------------+-------+------+
1344 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
1345 * +----+-------+---+-----+-----+------------+-------+------+
1346 *
1347 * For non-vector:
1348 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1349 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1350 * For vector:
1351 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1352 * opc<0>: 0 -> store, 1 -> load
1353 * Rn: base address register (inc SP)
1354 * Rt: target register
1355 */
1356static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
1357{
1358 int rt = extract32(insn, 0, 5);
1359 int rn = extract32(insn, 5, 5);
1360 unsigned int imm12 = extract32(insn, 10, 12);
1361 bool is_vector = extract32(insn, 26, 1);
1362 int size = extract32(insn, 30, 2);
1363 int opc = extract32(insn, 22, 2);
1364 unsigned int offset;
1365
1366 TCGv_i64 tcg_addr;
1367
1368 bool is_store;
1369 bool is_signed = false;
1370 bool is_extended = false;
1371
1372 if (is_vector) {
1373 size |= (opc & 2) << 1;
1374 if (size > 4) {
1375 unallocated_encoding(s);
1376 return;
1377 }
1378 is_store = !extract32(opc, 0, 1);
1379 } else {
1380 if (size == 3 && opc == 2) {
1381 /* PRFM - prefetch */
1382 return;
1383 }
1384 if (opc == 3 && size > 1) {
1385 unallocated_encoding(s);
1386 return;
1387 }
1388 is_store = (opc == 0);
1389 is_signed = extract32(opc, 1, 1);
1390 is_extended = (size < 3) && extract32(opc, 0, 1);
1391 }
1392
1393 if (rn == 31) {
1394 gen_check_sp_alignment(s);
1395 }
1396 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1397 offset = imm12 << size;
1398 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1399
1400 if (is_vector) {
1401 if (is_store) {
1402 do_fp_st(s, rt, tcg_addr, size);
1403 } else {
1404 do_fp_ld(s, rt, tcg_addr, size);
1405 }
1406 } else {
1407 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1408 if (is_store) {
1409 do_gpr_st(s, tcg_rt, tcg_addr, size);
1410 } else {
1411 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1412 }
1413 }
1414}
1415
Alex Bennéea5e94a92013-12-23 23:27:29 +00001416/* Load/store register (immediate forms) */
1417static void disas_ldst_reg_imm(DisasContext *s, uint32_t insn)
1418{
1419 switch (extract32(insn, 10, 2)) {
1420 case 0: case 1: case 3:
1421 /* Load/store register (unscaled immediate) */
1422 /* Load/store immediate pre/post-indexed */
1423 disas_ldst_reg_imm9(s, insn);
1424 break;
1425 case 2:
1426 /* Load/store register unprivileged */
1427 unsupported_encoding(s, insn);
1428 break;
1429 default:
1430 unallocated_encoding(s);
1431 break;
1432 }
1433}
1434
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001435/* Load/store register (all forms) */
1436static void disas_ldst_reg(DisasContext *s, uint32_t insn)
1437{
Alex Bennéed5612f12013-12-23 23:27:28 +00001438 switch (extract32(insn, 24, 2)) {
1439 case 0:
Alex Bennée229b7a02013-12-23 23:27:29 +00001440 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
1441 disas_ldst_reg_roffset(s, insn);
1442 } else {
Alex Bennéea5e94a92013-12-23 23:27:29 +00001443 disas_ldst_reg_imm(s, insn);
Alex Bennée229b7a02013-12-23 23:27:29 +00001444 }
Alex Bennéed5612f12013-12-23 23:27:28 +00001445 break;
1446 case 1:
1447 disas_ldst_reg_unsigned_imm(s, insn);
1448 break;
1449 default:
1450 unallocated_encoding(s);
1451 break;
1452 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001453}
1454
1455/* AdvSIMD load/store multiple structures */
1456static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
1457{
1458 unsupported_encoding(s, insn);
1459}
1460
1461/* AdvSIMD load/store single structure */
1462static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
1463{
1464 unsupported_encoding(s, insn);
1465}
1466
1467/* C3.3 Loads and stores */
1468static void disas_ldst(DisasContext *s, uint32_t insn)
1469{
1470 switch (extract32(insn, 24, 6)) {
1471 case 0x08: /* Load/store exclusive */
1472 disas_ldst_excl(s, insn);
1473 break;
1474 case 0x18: case 0x1c: /* Load register (literal) */
1475 disas_ld_lit(s, insn);
1476 break;
1477 case 0x28: case 0x29:
1478 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
1479 disas_ldst_pair(s, insn);
1480 break;
1481 case 0x38: case 0x39:
1482 case 0x3c: case 0x3d: /* Load/store register (all forms) */
1483 disas_ldst_reg(s, insn);
1484 break;
1485 case 0x0c: /* AdvSIMD load/store multiple structures */
1486 disas_ldst_multiple_struct(s, insn);
1487 break;
1488 case 0x0d: /* AdvSIMD load/store single structure */
1489 disas_ldst_single_struct(s, insn);
1490 break;
1491 default:
1492 unallocated_encoding(s);
1493 break;
1494 }
1495}
1496
Alexander Graf15bfe8b2013-12-17 19:42:34 +00001497/* C3.4.6 PC-rel. addressing
1498 * 31 30 29 28 24 23 5 4 0
1499 * +----+-------+-----------+-------------------+------+
1500 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
1501 * +----+-------+-----------+-------------------+------+
1502 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001503static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
1504{
Alexander Graf15bfe8b2013-12-17 19:42:34 +00001505 unsigned int page, rd;
1506 uint64_t base;
1507 int64_t offset;
1508
1509 page = extract32(insn, 31, 1);
1510 /* SignExtend(immhi:immlo) -> offset */
1511 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
1512 rd = extract32(insn, 0, 5);
1513 base = s->pc - 4;
1514
1515 if (page) {
1516 /* ADRP (page based) */
1517 base &= ~0xfff;
1518 offset <<= 12;
1519 }
1520
1521 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001522}
1523
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00001524/*
1525 * C3.4.1 Add/subtract (immediate)
1526 *
1527 * 31 30 29 28 24 23 22 21 10 9 5 4 0
1528 * +--+--+--+-----------+-----+-------------+-----+-----+
1529 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
1530 * +--+--+--+-----------+-----+-------------+-----+-----+
1531 *
1532 * sf: 0 -> 32bit, 1 -> 64bit
1533 * op: 0 -> add , 1 -> sub
1534 * S: 1 -> set flags
1535 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
1536 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001537static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
1538{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00001539 int rd = extract32(insn, 0, 5);
1540 int rn = extract32(insn, 5, 5);
1541 uint64_t imm = extract32(insn, 10, 12);
1542 int shift = extract32(insn, 22, 2);
1543 bool setflags = extract32(insn, 29, 1);
1544 bool sub_op = extract32(insn, 30, 1);
1545 bool is_64bit = extract32(insn, 31, 1);
1546
1547 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1548 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
1549 TCGv_i64 tcg_result;
1550
1551 switch (shift) {
1552 case 0x0:
1553 break;
1554 case 0x1:
1555 imm <<= 12;
1556 break;
1557 default:
1558 unallocated_encoding(s);
1559 return;
1560 }
1561
1562 tcg_result = tcg_temp_new_i64();
1563 if (!setflags) {
1564 if (sub_op) {
1565 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
1566 } else {
1567 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
1568 }
1569 } else {
1570 TCGv_i64 tcg_imm = tcg_const_i64(imm);
1571 if (sub_op) {
1572 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
1573 } else {
1574 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
1575 }
1576 tcg_temp_free_i64(tcg_imm);
1577 }
1578
1579 if (is_64bit) {
1580 tcg_gen_mov_i64(tcg_rd, tcg_result);
1581 } else {
1582 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
1583 }
1584
1585 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001586}
1587
Alexander Graf71b46082013-12-17 19:42:36 +00001588/* The input should be a value in the bottom e bits (with higher
1589 * bits zero); returns that value replicated into every element
1590 * of size e in a 64 bit integer.
1591 */
1592static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
1593{
1594 assert(e != 0);
1595 while (e < 64) {
1596 mask |= mask << e;
1597 e *= 2;
1598 }
1599 return mask;
1600}
1601
1602/* Return a value with the bottom len bits set (where 0 < len <= 64) */
1603static inline uint64_t bitmask64(unsigned int length)
1604{
1605 assert(length > 0 && length <= 64);
1606 return ~0ULL >> (64 - length);
1607}
1608
1609/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
1610 * only require the wmask. Returns false if the imms/immr/immn are a reserved
1611 * value (ie should cause a guest UNDEF exception), and true if they are
1612 * valid, in which case the decoded bit pattern is written to result.
1613 */
1614static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
1615 unsigned int imms, unsigned int immr)
1616{
1617 uint64_t mask;
1618 unsigned e, levels, s, r;
1619 int len;
1620
1621 assert(immn < 2 && imms < 64 && immr < 64);
1622
1623 /* The bit patterns we create here are 64 bit patterns which
1624 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
1625 * 64 bits each. Each element contains the same value: a run
1626 * of between 1 and e-1 non-zero bits, rotated within the
1627 * element by between 0 and e-1 bits.
1628 *
1629 * The element size and run length are encoded into immn (1 bit)
1630 * and imms (6 bits) as follows:
1631 * 64 bit elements: immn = 1, imms = <length of run - 1>
1632 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
1633 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
1634 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
1635 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
1636 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
1637 * Notice that immn = 0, imms = 11111x is the only combination
1638 * not covered by one of the above options; this is reserved.
1639 * Further, <length of run - 1> all-ones is a reserved pattern.
1640 *
1641 * In all cases the rotation is by immr % e (and immr is 6 bits).
1642 */
1643
1644 /* First determine the element size */
1645 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
1646 if (len < 1) {
1647 /* This is the immn == 0, imms == 0x11111x case */
1648 return false;
1649 }
1650 e = 1 << len;
1651
1652 levels = e - 1;
1653 s = imms & levels;
1654 r = immr & levels;
1655
1656 if (s == levels) {
1657 /* <length of run - 1> mustn't be all-ones. */
1658 return false;
1659 }
1660
1661 /* Create the value of one element: s+1 set bits rotated
1662 * by r within the element (which is e bits wide)...
1663 */
1664 mask = bitmask64(s + 1);
1665 mask = (mask >> r) | (mask << (e - r));
1666 /* ...then replicate the element over the whole 64 bit value */
1667 mask = bitfield_replicate(mask, e);
1668 *result = mask;
1669 return true;
1670}
1671
1672/* C3.4.4 Logical (immediate)
1673 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
1674 * +----+-----+-------------+---+------+------+------+------+
1675 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
1676 * +----+-----+-------------+---+------+------+------+------+
1677 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001678static void disas_logic_imm(DisasContext *s, uint32_t insn)
1679{
Alexander Graf71b46082013-12-17 19:42:36 +00001680 unsigned int sf, opc, is_n, immr, imms, rn, rd;
1681 TCGv_i64 tcg_rd, tcg_rn;
1682 uint64_t wmask;
1683 bool is_and = false;
1684
1685 sf = extract32(insn, 31, 1);
1686 opc = extract32(insn, 29, 2);
1687 is_n = extract32(insn, 22, 1);
1688 immr = extract32(insn, 16, 6);
1689 imms = extract32(insn, 10, 6);
1690 rn = extract32(insn, 5, 5);
1691 rd = extract32(insn, 0, 5);
1692
1693 if (!sf && is_n) {
1694 unallocated_encoding(s);
1695 return;
1696 }
1697
1698 if (opc == 0x3) { /* ANDS */
1699 tcg_rd = cpu_reg(s, rd);
1700 } else {
1701 tcg_rd = cpu_reg_sp(s, rd);
1702 }
1703 tcg_rn = cpu_reg(s, rn);
1704
1705 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
1706 /* some immediate field values are reserved */
1707 unallocated_encoding(s);
1708 return;
1709 }
1710
1711 if (!sf) {
1712 wmask &= 0xffffffff;
1713 }
1714
1715 switch (opc) {
1716 case 0x3: /* ANDS */
1717 case 0x0: /* AND */
1718 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
1719 is_and = true;
1720 break;
1721 case 0x1: /* ORR */
1722 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
1723 break;
1724 case 0x2: /* EOR */
1725 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
1726 break;
1727 default:
1728 assert(FALSE); /* must handle all above */
1729 break;
1730 }
1731
1732 if (!sf && !is_and) {
1733 /* zero extend final result; we know we can skip this for AND
1734 * since the immediate had the high 32 bits clear.
1735 */
1736 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1737 }
1738
1739 if (opc == 3) { /* ANDS */
1740 gen_logic_CC(sf, tcg_rd);
1741 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001742}
1743
Alex Bennéeed6ec672013-12-23 23:27:29 +00001744/*
1745 * C3.4.5 Move wide (immediate)
1746 *
1747 * 31 30 29 28 23 22 21 20 5 4 0
1748 * +--+-----+-------------+-----+----------------+------+
1749 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
1750 * +--+-----+-------------+-----+----------------+------+
1751 *
1752 * sf: 0 -> 32 bit, 1 -> 64 bit
1753 * opc: 00 -> N, 10 -> Z, 11 -> K
1754 * hw: shift/16 (0,16, and sf only 32, 48)
1755 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001756static void disas_movw_imm(DisasContext *s, uint32_t insn)
1757{
Alex Bennéeed6ec672013-12-23 23:27:29 +00001758 int rd = extract32(insn, 0, 5);
1759 uint64_t imm = extract32(insn, 5, 16);
1760 int sf = extract32(insn, 31, 1);
1761 int opc = extract32(insn, 29, 2);
1762 int pos = extract32(insn, 21, 2) << 4;
1763 TCGv_i64 tcg_rd = cpu_reg(s, rd);
1764 TCGv_i64 tcg_imm;
1765
1766 if (!sf && (pos >= 32)) {
1767 unallocated_encoding(s);
1768 return;
1769 }
1770
1771 switch (opc) {
1772 case 0: /* MOVN */
1773 case 2: /* MOVZ */
1774 imm <<= pos;
1775 if (opc == 0) {
1776 imm = ~imm;
1777 }
1778 if (!sf) {
1779 imm &= 0xffffffffu;
1780 }
1781 tcg_gen_movi_i64(tcg_rd, imm);
1782 break;
1783 case 3: /* MOVK */
1784 tcg_imm = tcg_const_i64(imm);
1785 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
1786 tcg_temp_free_i64(tcg_imm);
1787 if (!sf) {
1788 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1789 }
1790 break;
1791 default:
1792 unallocated_encoding(s);
1793 break;
1794 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001795}
1796
Claudio Fontana88077742013-12-17 19:42:35 +00001797/* C3.4.2 Bitfield
1798 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
1799 * +----+-----+-------------+---+------+------+------+------+
1800 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
1801 * +----+-----+-------------+---+------+------+------+------+
1802 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001803static void disas_bitfield(DisasContext *s, uint32_t insn)
1804{
Claudio Fontana88077742013-12-17 19:42:35 +00001805 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
1806 TCGv_i64 tcg_rd, tcg_tmp;
1807
1808 sf = extract32(insn, 31, 1);
1809 opc = extract32(insn, 29, 2);
1810 n = extract32(insn, 22, 1);
1811 ri = extract32(insn, 16, 6);
1812 si = extract32(insn, 10, 6);
1813 rn = extract32(insn, 5, 5);
1814 rd = extract32(insn, 0, 5);
1815 bitsize = sf ? 64 : 32;
1816
1817 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
1818 unallocated_encoding(s);
1819 return;
1820 }
1821
1822 tcg_rd = cpu_reg(s, rd);
1823 tcg_tmp = read_cpu_reg(s, rn, sf);
1824
1825 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
1826
1827 if (opc != 1) { /* SBFM or UBFM */
1828 tcg_gen_movi_i64(tcg_rd, 0);
1829 }
1830
1831 /* do the bit move operation */
1832 if (si >= ri) {
1833 /* Wd<s-r:0> = Wn<s:r> */
1834 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
1835 pos = 0;
1836 len = (si - ri) + 1;
1837 } else {
1838 /* Wd<32+s-r,32-r> = Wn<s:0> */
1839 pos = bitsize - ri;
1840 len = si + 1;
1841 }
1842
1843 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
1844
1845 if (opc == 0) { /* SBFM - sign extend the destination field */
1846 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
1847 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
1848 }
1849
1850 if (!sf) { /* zero extend final result */
1851 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1852 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001853}
1854
Alexander Grafe801de92013-12-17 19:42:34 +00001855/* C3.4.3 Extract
1856 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
1857 * +----+------+-------------+---+----+------+--------+------+------+
1858 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
1859 * +----+------+-------------+---+----+------+--------+------+------+
1860 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001861static void disas_extract(DisasContext *s, uint32_t insn)
1862{
Alexander Grafe801de92013-12-17 19:42:34 +00001863 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
1864
1865 sf = extract32(insn, 31, 1);
1866 n = extract32(insn, 22, 1);
1867 rm = extract32(insn, 16, 5);
1868 imm = extract32(insn, 10, 6);
1869 rn = extract32(insn, 5, 5);
1870 rd = extract32(insn, 0, 5);
1871 op21 = extract32(insn, 29, 2);
1872 op0 = extract32(insn, 21, 1);
1873 bitsize = sf ? 64 : 32;
1874
1875 if (sf != n || op21 || op0 || imm >= bitsize) {
1876 unallocated_encoding(s);
1877 } else {
1878 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
1879
1880 tcg_rd = cpu_reg(s, rd);
1881
1882 if (imm) {
1883 /* OPTME: we can special case rm==rn as a rotate */
1884 tcg_rm = read_cpu_reg(s, rm, sf);
1885 tcg_rn = read_cpu_reg(s, rn, sf);
1886 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
1887 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
1888 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
1889 if (!sf) {
1890 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1891 }
1892 } else {
1893 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
1894 * so an extract from bit 0 is a special case.
1895 */
1896 if (sf) {
1897 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
1898 } else {
1899 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
1900 }
1901 }
1902
1903 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001904}
1905
1906/* C3.4 Data processing - immediate */
1907static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
1908{
1909 switch (extract32(insn, 23, 6)) {
1910 case 0x20: case 0x21: /* PC-rel. addressing */
1911 disas_pc_rel_adr(s, insn);
1912 break;
1913 case 0x22: case 0x23: /* Add/subtract (immediate) */
1914 disas_add_sub_imm(s, insn);
1915 break;
1916 case 0x24: /* Logical (immediate) */
1917 disas_logic_imm(s, insn);
1918 break;
1919 case 0x25: /* Move wide (immediate) */
1920 disas_movw_imm(s, insn);
1921 break;
1922 case 0x26: /* Bitfield */
1923 disas_bitfield(s, insn);
1924 break;
1925 case 0x27: /* Extract */
1926 disas_extract(s, insn);
1927 break;
1928 default:
1929 unallocated_encoding(s);
1930 break;
1931 }
1932}
1933
Alexander Graf832ffa12013-12-17 19:42:34 +00001934/* Shift a TCGv src by TCGv shift_amount, put result in dst.
1935 * Note that it is the caller's responsibility to ensure that the
1936 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
1937 * mandated semantics for out of range shifts.
1938 */
1939static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
1940 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
1941{
1942 switch (shift_type) {
1943 case A64_SHIFT_TYPE_LSL:
1944 tcg_gen_shl_i64(dst, src, shift_amount);
1945 break;
1946 case A64_SHIFT_TYPE_LSR:
1947 tcg_gen_shr_i64(dst, src, shift_amount);
1948 break;
1949 case A64_SHIFT_TYPE_ASR:
1950 if (!sf) {
1951 tcg_gen_ext32s_i64(dst, src);
1952 }
1953 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
1954 break;
1955 case A64_SHIFT_TYPE_ROR:
1956 if (sf) {
1957 tcg_gen_rotr_i64(dst, src, shift_amount);
1958 } else {
1959 TCGv_i32 t0, t1;
1960 t0 = tcg_temp_new_i32();
1961 t1 = tcg_temp_new_i32();
1962 tcg_gen_trunc_i64_i32(t0, src);
1963 tcg_gen_trunc_i64_i32(t1, shift_amount);
1964 tcg_gen_rotr_i32(t0, t0, t1);
1965 tcg_gen_extu_i32_i64(dst, t0);
1966 tcg_temp_free_i32(t0);
1967 tcg_temp_free_i32(t1);
1968 }
1969 break;
1970 default:
1971 assert(FALSE); /* all shift types should be handled */
1972 break;
1973 }
1974
1975 if (!sf) { /* zero extend final result */
1976 tcg_gen_ext32u_i64(dst, dst);
1977 }
1978}
1979
1980/* Shift a TCGv src by immediate, put result in dst.
1981 * The shift amount must be in range (this should always be true as the
1982 * relevant instructions will UNDEF on bad shift immediates).
1983 */
1984static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
1985 enum a64_shift_type shift_type, unsigned int shift_i)
1986{
1987 assert(shift_i < (sf ? 64 : 32));
1988
1989 if (shift_i == 0) {
1990 tcg_gen_mov_i64(dst, src);
1991 } else {
1992 TCGv_i64 shift_const;
1993
1994 shift_const = tcg_const_i64(shift_i);
1995 shift_reg(dst, src, sf, shift_type, shift_const);
1996 tcg_temp_free_i64(shift_const);
1997 }
1998}
1999
2000/* C3.5.10 Logical (shifted register)
2001 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
2002 * +----+-----+-----------+-------+---+------+--------+------+------+
2003 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
2004 * +----+-----+-----------+-------+---+------+--------+------+------+
2005 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002006static void disas_logic_reg(DisasContext *s, uint32_t insn)
2007{
Alexander Graf832ffa12013-12-17 19:42:34 +00002008 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
2009 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
2010
2011 sf = extract32(insn, 31, 1);
2012 opc = extract32(insn, 29, 2);
2013 shift_type = extract32(insn, 22, 2);
2014 invert = extract32(insn, 21, 1);
2015 rm = extract32(insn, 16, 5);
2016 shift_amount = extract32(insn, 10, 6);
2017 rn = extract32(insn, 5, 5);
2018 rd = extract32(insn, 0, 5);
2019
2020 if (!sf && (shift_amount & (1 << 5))) {
2021 unallocated_encoding(s);
2022 return;
2023 }
2024
2025 tcg_rd = cpu_reg(s, rd);
2026
2027 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
2028 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
2029 * register-register MOV and MVN, so it is worth special casing.
2030 */
2031 tcg_rm = cpu_reg(s, rm);
2032 if (invert) {
2033 tcg_gen_not_i64(tcg_rd, tcg_rm);
2034 if (!sf) {
2035 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2036 }
2037 } else {
2038 if (sf) {
2039 tcg_gen_mov_i64(tcg_rd, tcg_rm);
2040 } else {
2041 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
2042 }
2043 }
2044 return;
2045 }
2046
2047 tcg_rm = read_cpu_reg(s, rm, sf);
2048
2049 if (shift_amount) {
2050 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
2051 }
2052
2053 tcg_rn = cpu_reg(s, rn);
2054
2055 switch (opc | (invert << 2)) {
2056 case 0: /* AND */
2057 case 3: /* ANDS */
2058 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
2059 break;
2060 case 1: /* ORR */
2061 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
2062 break;
2063 case 2: /* EOR */
2064 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
2065 break;
2066 case 4: /* BIC */
2067 case 7: /* BICS */
2068 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
2069 break;
2070 case 5: /* ORN */
2071 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
2072 break;
2073 case 6: /* EON */
2074 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
2075 break;
2076 default:
2077 assert(FALSE);
2078 break;
2079 }
2080
2081 if (!sf) {
2082 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2083 }
2084
2085 if (opc == 3) {
2086 gen_logic_CC(sf, tcg_rd);
2087 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002088}
2089
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002090/*
2091 * C3.5.1 Add/subtract (extended register)
2092 *
2093 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
2094 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2095 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
2096 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2097 *
2098 * sf: 0 -> 32bit, 1 -> 64bit
2099 * op: 0 -> add , 1 -> sub
2100 * S: 1 -> set flags
2101 * opt: 00
2102 * option: extension type (see DecodeRegExtend)
2103 * imm3: optional shift to Rm
2104 *
2105 * Rd = Rn + LSL(extend(Rm), amount)
2106 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002107static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
2108{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002109 int rd = extract32(insn, 0, 5);
2110 int rn = extract32(insn, 5, 5);
2111 int imm3 = extract32(insn, 10, 3);
2112 int option = extract32(insn, 13, 3);
2113 int rm = extract32(insn, 16, 5);
2114 bool setflags = extract32(insn, 29, 1);
2115 bool sub_op = extract32(insn, 30, 1);
2116 bool sf = extract32(insn, 31, 1);
2117
2118 TCGv_i64 tcg_rm, tcg_rn; /* temps */
2119 TCGv_i64 tcg_rd;
2120 TCGv_i64 tcg_result;
2121
2122 if (imm3 > 4) {
2123 unallocated_encoding(s);
2124 return;
2125 }
2126
2127 /* non-flag setting ops may use SP */
2128 if (!setflags) {
2129 tcg_rn = read_cpu_reg_sp(s, rn, sf);
2130 tcg_rd = cpu_reg_sp(s, rd);
2131 } else {
2132 tcg_rn = read_cpu_reg(s, rn, sf);
2133 tcg_rd = cpu_reg(s, rd);
2134 }
2135
2136 tcg_rm = read_cpu_reg(s, rm, sf);
2137 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
2138
2139 tcg_result = tcg_temp_new_i64();
2140
2141 if (!setflags) {
2142 if (sub_op) {
2143 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
2144 } else {
2145 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
2146 }
2147 } else {
2148 if (sub_op) {
2149 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
2150 } else {
2151 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
2152 }
2153 }
2154
2155 if (sf) {
2156 tcg_gen_mov_i64(tcg_rd, tcg_result);
2157 } else {
2158 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2159 }
2160
2161 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002162}
2163
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002164/*
2165 * C3.5.2 Add/subtract (shifted register)
2166 *
2167 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
2168 * +--+--+--+-----------+-----+--+-------+---------+------+------+
2169 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
2170 * +--+--+--+-----------+-----+--+-------+---------+------+------+
2171 *
2172 * sf: 0 -> 32bit, 1 -> 64bit
2173 * op: 0 -> add , 1 -> sub
2174 * S: 1 -> set flags
2175 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
2176 * imm6: Shift amount to apply to Rm before the add/sub
2177 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002178static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
2179{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002180 int rd = extract32(insn, 0, 5);
2181 int rn = extract32(insn, 5, 5);
2182 int imm6 = extract32(insn, 10, 6);
2183 int rm = extract32(insn, 16, 5);
2184 int shift_type = extract32(insn, 22, 2);
2185 bool setflags = extract32(insn, 29, 1);
2186 bool sub_op = extract32(insn, 30, 1);
2187 bool sf = extract32(insn, 31, 1);
2188
2189 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2190 TCGv_i64 tcg_rn, tcg_rm;
2191 TCGv_i64 tcg_result;
2192
2193 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
2194 unallocated_encoding(s);
2195 return;
2196 }
2197
2198 tcg_rn = read_cpu_reg(s, rn, sf);
2199 tcg_rm = read_cpu_reg(s, rm, sf);
2200
2201 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
2202
2203 tcg_result = tcg_temp_new_i64();
2204
2205 if (!setflags) {
2206 if (sub_op) {
2207 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
2208 } else {
2209 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
2210 }
2211 } else {
2212 if (sub_op) {
2213 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
2214 } else {
2215 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
2216 }
2217 }
2218
2219 if (sf) {
2220 tcg_gen_mov_i64(tcg_rd, tcg_result);
2221 } else {
2222 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2223 }
2224
2225 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002226}
2227
Alexander Graf52c8b9a2013-12-23 23:27:30 +00002228/* C3.5.9 Data-processing (3 source)
2229
2230 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
2231 +--+------+-----------+------+------+----+------+------+------+
2232 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
2233 +--+------+-----------+------+------+----+------+------+------+
2234
2235 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002236static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
2237{
Alexander Graf52c8b9a2013-12-23 23:27:30 +00002238 int rd = extract32(insn, 0, 5);
2239 int rn = extract32(insn, 5, 5);
2240 int ra = extract32(insn, 10, 5);
2241 int rm = extract32(insn, 16, 5);
2242 int op_id = (extract32(insn, 29, 3) << 4) |
2243 (extract32(insn, 21, 3) << 1) |
2244 extract32(insn, 15, 1);
2245 bool sf = extract32(insn, 31, 1);
2246 bool is_sub = extract32(op_id, 0, 1);
2247 bool is_high = extract32(op_id, 2, 1);
2248 bool is_signed = false;
2249 TCGv_i64 tcg_op1;
2250 TCGv_i64 tcg_op2;
2251 TCGv_i64 tcg_tmp;
2252
2253 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
2254 switch (op_id) {
2255 case 0x42: /* SMADDL */
2256 case 0x43: /* SMSUBL */
2257 case 0x44: /* SMULH */
2258 is_signed = true;
2259 break;
2260 case 0x0: /* MADD (32bit) */
2261 case 0x1: /* MSUB (32bit) */
2262 case 0x40: /* MADD (64bit) */
2263 case 0x41: /* MSUB (64bit) */
2264 case 0x4a: /* UMADDL */
2265 case 0x4b: /* UMSUBL */
2266 case 0x4c: /* UMULH */
2267 break;
2268 default:
2269 unallocated_encoding(s);
2270 return;
2271 }
2272
2273 if (is_high) {
2274 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
2275 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2276 TCGv_i64 tcg_rn = cpu_reg(s, rn);
2277 TCGv_i64 tcg_rm = cpu_reg(s, rm);
2278
2279 if (is_signed) {
2280 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
2281 } else {
2282 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
2283 }
2284
2285 tcg_temp_free_i64(low_bits);
2286 return;
2287 }
2288
2289 tcg_op1 = tcg_temp_new_i64();
2290 tcg_op2 = tcg_temp_new_i64();
2291 tcg_tmp = tcg_temp_new_i64();
2292
2293 if (op_id < 0x42) {
2294 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
2295 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
2296 } else {
2297 if (is_signed) {
2298 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
2299 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
2300 } else {
2301 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
2302 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
2303 }
2304 }
2305
2306 if (ra == 31 && !is_sub) {
2307 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
2308 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
2309 } else {
2310 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
2311 if (is_sub) {
2312 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
2313 } else {
2314 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
2315 }
2316 }
2317
2318 if (!sf) {
2319 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
2320 }
2321
2322 tcg_temp_free_i64(tcg_op1);
2323 tcg_temp_free_i64(tcg_op2);
2324 tcg_temp_free_i64(tcg_tmp);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002325}
2326
2327/* Add/subtract (with carry) */
2328static void disas_adc_sbc(DisasContext *s, uint32_t insn)
2329{
2330 unsupported_encoding(s, insn);
2331}
2332
2333/* Conditional compare (immediate) */
2334static void disas_cc_imm(DisasContext *s, uint32_t insn)
2335{
2336 unsupported_encoding(s, insn);
2337}
2338
2339/* Conditional compare (register) */
2340static void disas_cc_reg(DisasContext *s, uint32_t insn)
2341{
2342 unsupported_encoding(s, insn);
2343}
2344
Claudio Fontanae952d8c2013-12-17 19:42:33 +00002345/* C3.5.6 Conditional select
2346 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
2347 * +----+----+---+-----------------+------+------+-----+------+------+
2348 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
2349 * +----+----+---+-----------------+------+------+-----+------+------+
2350 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002351static void disas_cond_select(DisasContext *s, uint32_t insn)
2352{
Claudio Fontanae952d8c2013-12-17 19:42:33 +00002353 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
2354 TCGv_i64 tcg_rd, tcg_src;
2355
2356 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
2357 /* S == 1 or op2<1> == 1 */
2358 unallocated_encoding(s);
2359 return;
2360 }
2361 sf = extract32(insn, 31, 1);
2362 else_inv = extract32(insn, 30, 1);
2363 rm = extract32(insn, 16, 5);
2364 cond = extract32(insn, 12, 4);
2365 else_inc = extract32(insn, 10, 1);
2366 rn = extract32(insn, 5, 5);
2367 rd = extract32(insn, 0, 5);
2368
2369 if (rd == 31) {
2370 /* silly no-op write; until we use movcond we must special-case
2371 * this to avoid a dead temporary across basic blocks.
2372 */
2373 return;
2374 }
2375
2376 tcg_rd = cpu_reg(s, rd);
2377
2378 if (cond >= 0x0e) { /* condition "always" */
2379 tcg_src = read_cpu_reg(s, rn, sf);
2380 tcg_gen_mov_i64(tcg_rd, tcg_src);
2381 } else {
2382 /* OPTME: we could use movcond here, at the cost of duplicating
2383 * a lot of the arm_gen_test_cc() logic.
2384 */
2385 int label_match = gen_new_label();
2386 int label_continue = gen_new_label();
2387
2388 arm_gen_test_cc(cond, label_match);
2389 /* nomatch: */
2390 tcg_src = cpu_reg(s, rm);
2391
2392 if (else_inv && else_inc) {
2393 tcg_gen_neg_i64(tcg_rd, tcg_src);
2394 } else if (else_inv) {
2395 tcg_gen_not_i64(tcg_rd, tcg_src);
2396 } else if (else_inc) {
2397 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
2398 } else {
2399 tcg_gen_mov_i64(tcg_rd, tcg_src);
2400 }
2401 if (!sf) {
2402 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2403 }
2404 tcg_gen_br(label_continue);
2405 /* match: */
2406 gen_set_label(label_match);
2407 tcg_src = read_cpu_reg(s, rn, sf);
2408 tcg_gen_mov_i64(tcg_rd, tcg_src);
2409 /* continue: */
2410 gen_set_label(label_continue);
2411 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002412}
2413
Claudio Fontana680ead22013-12-17 19:42:35 +00002414static void handle_clz(DisasContext *s, unsigned int sf,
2415 unsigned int rn, unsigned int rd)
2416{
2417 TCGv_i64 tcg_rd, tcg_rn;
2418 tcg_rd = cpu_reg(s, rd);
2419 tcg_rn = cpu_reg(s, rn);
2420
2421 if (sf) {
2422 gen_helper_clz64(tcg_rd, tcg_rn);
2423 } else {
2424 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
2425 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
2426 gen_helper_clz(tcg_tmp32, tcg_tmp32);
2427 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
2428 tcg_temp_free_i32(tcg_tmp32);
2429 }
2430}
2431
Claudio Fontanae80c5022013-12-17 19:42:35 +00002432static void handle_cls(DisasContext *s, unsigned int sf,
2433 unsigned int rn, unsigned int rd)
2434{
2435 TCGv_i64 tcg_rd, tcg_rn;
2436 tcg_rd = cpu_reg(s, rd);
2437 tcg_rn = cpu_reg(s, rn);
2438
2439 if (sf) {
2440 gen_helper_cls64(tcg_rd, tcg_rn);
2441 } else {
2442 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
2443 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
2444 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
2445 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
2446 tcg_temp_free_i32(tcg_tmp32);
2447 }
2448}
2449
Alexander Graf82e14b02013-12-17 19:42:35 +00002450static void handle_rbit(DisasContext *s, unsigned int sf,
2451 unsigned int rn, unsigned int rd)
2452{
2453 TCGv_i64 tcg_rd, tcg_rn;
2454 tcg_rd = cpu_reg(s, rd);
2455 tcg_rn = cpu_reg(s, rn);
2456
2457 if (sf) {
2458 gen_helper_rbit64(tcg_rd, tcg_rn);
2459 } else {
2460 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
2461 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
2462 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
2463 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
2464 tcg_temp_free_i32(tcg_tmp32);
2465 }
2466}
2467
Claudio Fontana45323202013-12-17 19:42:35 +00002468/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
2469static void handle_rev64(DisasContext *s, unsigned int sf,
2470 unsigned int rn, unsigned int rd)
2471{
2472 if (!sf) {
2473 unallocated_encoding(s);
2474 return;
2475 }
2476 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
2477}
2478
2479/* C5.6.149 REV with sf==0, opcode==2
2480 * C5.6.151 REV32 (sf==1, opcode==2)
2481 */
2482static void handle_rev32(DisasContext *s, unsigned int sf,
2483 unsigned int rn, unsigned int rd)
2484{
2485 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2486
2487 if (sf) {
2488 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2489 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
2490
2491 /* bswap32_i64 requires zero high word */
2492 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
2493 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
2494 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
2495 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
2496 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
2497
2498 tcg_temp_free_i64(tcg_tmp);
2499 } else {
2500 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
2501 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
2502 }
2503}
2504
2505/* C5.6.150 REV16 (opcode==1) */
2506static void handle_rev16(DisasContext *s, unsigned int sf,
2507 unsigned int rn, unsigned int rd)
2508{
2509 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2510 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2511 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
2512
2513 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
2514 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
2515
2516 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
2517 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
2518 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
2519 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
2520
2521 if (sf) {
2522 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
2523 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
2524 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
2525 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
2526
2527 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
2528 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
2529 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
2530 }
2531
2532 tcg_temp_free_i64(tcg_tmp);
2533}
2534
Claudio Fontana680ead22013-12-17 19:42:35 +00002535/* C3.5.7 Data-processing (1 source)
2536 * 31 30 29 28 21 20 16 15 10 9 5 4 0
2537 * +----+---+---+-----------------+---------+--------+------+------+
2538 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
2539 * +----+---+---+-----------------+---------+--------+------+------+
2540 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002541static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
2542{
Claudio Fontana680ead22013-12-17 19:42:35 +00002543 unsigned int sf, opcode, rn, rd;
2544
2545 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
2546 unallocated_encoding(s);
2547 return;
2548 }
2549
2550 sf = extract32(insn, 31, 1);
2551 opcode = extract32(insn, 10, 6);
2552 rn = extract32(insn, 5, 5);
2553 rd = extract32(insn, 0, 5);
2554
2555 switch (opcode) {
2556 case 0: /* RBIT */
Alexander Graf82e14b02013-12-17 19:42:35 +00002557 handle_rbit(s, sf, rn, rd);
2558 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00002559 case 1: /* REV16 */
Claudio Fontana45323202013-12-17 19:42:35 +00002560 handle_rev16(s, sf, rn, rd);
2561 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00002562 case 2: /* REV32 */
Claudio Fontana45323202013-12-17 19:42:35 +00002563 handle_rev32(s, sf, rn, rd);
2564 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00002565 case 3: /* REV64 */
Claudio Fontana45323202013-12-17 19:42:35 +00002566 handle_rev64(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00002567 break;
2568 case 4: /* CLZ */
2569 handle_clz(s, sf, rn, rd);
2570 break;
2571 case 5: /* CLS */
Claudio Fontanae80c5022013-12-17 19:42:35 +00002572 handle_cls(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00002573 break;
2574 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002575}
2576
Alexander Graf8220e912013-12-17 19:42:34 +00002577static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
2578 unsigned int rm, unsigned int rn, unsigned int rd)
2579{
2580 TCGv_i64 tcg_n, tcg_m, tcg_rd;
2581 tcg_rd = cpu_reg(s, rd);
2582
2583 if (!sf && is_signed) {
2584 tcg_n = new_tmp_a64(s);
2585 tcg_m = new_tmp_a64(s);
2586 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
2587 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
2588 } else {
2589 tcg_n = read_cpu_reg(s, rn, sf);
2590 tcg_m = read_cpu_reg(s, rm, sf);
2591 }
2592
2593 if (is_signed) {
2594 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
2595 } else {
2596 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
2597 }
2598
2599 if (!sf) { /* zero extend final result */
2600 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2601 }
2602}
2603
Alexander Graf6c1adc92013-12-17 19:42:34 +00002604/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
2605static void handle_shift_reg(DisasContext *s,
2606 enum a64_shift_type shift_type, unsigned int sf,
2607 unsigned int rm, unsigned int rn, unsigned int rd)
2608{
2609 TCGv_i64 tcg_shift = tcg_temp_new_i64();
2610 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2611 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
2612
2613 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
2614 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
2615 tcg_temp_free_i64(tcg_shift);
2616}
2617
Alexander Graf8220e912013-12-17 19:42:34 +00002618/* C3.5.8 Data-processing (2 source)
2619 * 31 30 29 28 21 20 16 15 10 9 5 4 0
2620 * +----+---+---+-----------------+------+--------+------+------+
2621 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
2622 * +----+---+---+-----------------+------+--------+------+------+
2623 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002624static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
2625{
Alexander Graf8220e912013-12-17 19:42:34 +00002626 unsigned int sf, rm, opcode, rn, rd;
2627 sf = extract32(insn, 31, 1);
2628 rm = extract32(insn, 16, 5);
2629 opcode = extract32(insn, 10, 6);
2630 rn = extract32(insn, 5, 5);
2631 rd = extract32(insn, 0, 5);
2632
2633 if (extract32(insn, 29, 1)) {
2634 unallocated_encoding(s);
2635 return;
2636 }
2637
2638 switch (opcode) {
2639 case 2: /* UDIV */
2640 handle_div(s, false, sf, rm, rn, rd);
2641 break;
2642 case 3: /* SDIV */
2643 handle_div(s, true, sf, rm, rn, rd);
2644 break;
2645 case 8: /* LSLV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00002646 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
2647 break;
Alexander Graf8220e912013-12-17 19:42:34 +00002648 case 9: /* LSRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00002649 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
2650 break;
Alexander Graf8220e912013-12-17 19:42:34 +00002651 case 10: /* ASRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00002652 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
2653 break;
Alexander Graf8220e912013-12-17 19:42:34 +00002654 case 11: /* RORV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00002655 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
2656 break;
Alexander Graf8220e912013-12-17 19:42:34 +00002657 case 16:
2658 case 17:
2659 case 18:
2660 case 19:
2661 case 20:
2662 case 21:
2663 case 22:
2664 case 23: /* CRC32 */
2665 unsupported_encoding(s, insn);
2666 break;
2667 default:
2668 unallocated_encoding(s);
2669 break;
2670 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002671}
2672
2673/* C3.5 Data processing - register */
2674static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
2675{
2676 switch (extract32(insn, 24, 5)) {
2677 case 0x0a: /* Logical (shifted register) */
2678 disas_logic_reg(s, insn);
2679 break;
2680 case 0x0b: /* Add/subtract */
2681 if (insn & (1 << 21)) { /* (extended register) */
2682 disas_add_sub_ext_reg(s, insn);
2683 } else {
2684 disas_add_sub_reg(s, insn);
2685 }
2686 break;
2687 case 0x1b: /* Data-processing (3 source) */
2688 disas_data_proc_3src(s, insn);
2689 break;
2690 case 0x1a:
2691 switch (extract32(insn, 21, 3)) {
2692 case 0x0: /* Add/subtract (with carry) */
2693 disas_adc_sbc(s, insn);
2694 break;
2695 case 0x2: /* Conditional compare */
2696 if (insn & (1 << 11)) { /* (immediate) */
2697 disas_cc_imm(s, insn);
2698 } else { /* (register) */
2699 disas_cc_reg(s, insn);
2700 }
2701 break;
2702 case 0x4: /* Conditional select */
2703 disas_cond_select(s, insn);
2704 break;
2705 case 0x6: /* Data-processing */
2706 if (insn & (1 << 30)) { /* (1 source) */
2707 disas_data_proc_1src(s, insn);
2708 } else { /* (2 source) */
2709 disas_data_proc_2src(s, insn);
2710 }
2711 break;
2712 default:
2713 unallocated_encoding(s);
2714 break;
2715 }
2716 break;
2717 default:
2718 unallocated_encoding(s);
2719 break;
2720 }
2721}
2722
Peter Maydellfaa0ba42013-12-23 23:27:30 +00002723/* C3.6.22 Floating point compare
2724 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
2725 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
2726 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
2727 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
2728 */
2729static void disas_fp_compare(DisasContext *s, uint32_t insn)
2730{
2731 unsupported_encoding(s, insn);
2732}
2733
2734/* C3.6.23 Floating point conditional compare
2735 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
2736 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
2737 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
2738 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
2739 */
2740static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
2741{
2742 unsupported_encoding(s, insn);
2743}
2744
2745/* C3.6.24 Floating point conditional select
2746 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
2747 * +---+---+---+-----------+------+---+------+------+-----+------+------+
2748 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
2749 * +---+---+---+-----------+------+---+------+------+-----+------+------+
2750 */
2751static void disas_fp_csel(DisasContext *s, uint32_t insn)
2752{
2753 unsupported_encoding(s, insn);
2754}
2755
2756/* C3.6.25 Floating point data-processing (1 source)
2757 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
2758 * +---+---+---+-----------+------+---+--------+-----------+------+------+
2759 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
2760 * +---+---+---+-----------+------+---+--------+-----------+------+------+
2761 */
2762static void disas_fp_1src(DisasContext *s, uint32_t insn)
2763{
2764 unsupported_encoding(s, insn);
2765}
2766
2767/* C3.6.26 Floating point data-processing (2 source)
2768 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
2769 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
2770 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
2771 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
2772 */
2773static void disas_fp_2src(DisasContext *s, uint32_t insn)
2774{
2775 unsupported_encoding(s, insn);
2776}
2777
2778/* C3.6.27 Floating point data-processing (3 source)
2779 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
2780 * +---+---+---+-----------+------+----+------+----+------+------+------+
2781 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
2782 * +---+---+---+-----------+------+----+------+----+------+------+------+
2783 */
2784static void disas_fp_3src(DisasContext *s, uint32_t insn)
2785{
2786 unsupported_encoding(s, insn);
2787}
2788
2789/* C3.6.28 Floating point immediate
2790 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
2791 * +---+---+---+-----------+------+---+------------+-------+------+------+
2792 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
2793 * +---+---+---+-----------+------+---+------------+-------+------+------+
2794 */
2795static void disas_fp_imm(DisasContext *s, uint32_t insn)
2796{
2797 unsupported_encoding(s, insn);
2798}
2799
2800/* C3.6.29 Floating point <-> fixed point conversions
2801 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
2802 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
2803 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
2804 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
2805 */
2806static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
2807{
2808 unsupported_encoding(s, insn);
2809}
2810
Peter Maydellce5458e2013-12-23 23:27:31 +00002811static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
2812{
2813 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
2814 * without conversion.
2815 */
2816
2817 if (itof) {
2818 int freg_offs = offsetof(CPUARMState, vfp.regs[rd * 2]);
2819 TCGv_i64 tcg_rn = cpu_reg(s, rn);
2820
2821 switch (type) {
2822 case 0:
2823 {
2824 /* 32 bit */
2825 TCGv_i64 tmp = tcg_temp_new_i64();
2826 tcg_gen_ext32u_i64(tmp, tcg_rn);
2827 tcg_gen_st_i64(tmp, cpu_env, freg_offs);
2828 tcg_gen_movi_i64(tmp, 0);
2829 tcg_gen_st_i64(tmp, cpu_env, freg_offs + sizeof(float64));
2830 tcg_temp_free_i64(tmp);
2831 break;
2832 }
2833 case 1:
2834 {
2835 /* 64 bit */
2836 TCGv_i64 tmp = tcg_const_i64(0);
2837 tcg_gen_st_i64(tcg_rn, cpu_env, freg_offs);
2838 tcg_gen_st_i64(tmp, cpu_env, freg_offs + sizeof(float64));
2839 tcg_temp_free_i64(tmp);
2840 break;
2841 }
2842 case 2:
2843 /* 64 bit to top half. */
2844 tcg_gen_st_i64(tcg_rn, cpu_env, freg_offs + sizeof(float64));
2845 break;
2846 }
2847 } else {
2848 int freg_offs = offsetof(CPUARMState, vfp.regs[rn * 2]);
2849 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2850
2851 switch (type) {
2852 case 0:
2853 /* 32 bit */
2854 tcg_gen_ld32u_i64(tcg_rd, cpu_env, freg_offs);
2855 break;
2856 case 2:
2857 /* 64 bits from top half */
2858 freg_offs += sizeof(float64);
2859 /* fall through */
2860 case 1:
2861 /* 64 bit */
2862 tcg_gen_ld_i64(tcg_rd, cpu_env, freg_offs);
2863 break;
2864 }
2865 }
2866}
2867
Peter Maydellfaa0ba42013-12-23 23:27:30 +00002868/* C3.6.30 Floating point <-> integer conversions
2869 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
2870 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
2871 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
2872 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
2873 */
2874static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
2875{
Peter Maydellce5458e2013-12-23 23:27:31 +00002876 int rd = extract32(insn, 0, 5);
2877 int rn = extract32(insn, 5, 5);
2878 int opcode = extract32(insn, 16, 3);
2879 int rmode = extract32(insn, 19, 2);
2880 int type = extract32(insn, 22, 2);
2881 bool sbit = extract32(insn, 29, 1);
2882 bool sf = extract32(insn, 31, 1);
2883
2884 if (!sbit && (rmode < 2) && (opcode > 5)) {
2885 /* FMOV */
2886 bool itof = opcode & 1;
2887
2888 switch (sf << 3 | type << 1 | rmode) {
2889 case 0x0: /* 32 bit */
2890 case 0xa: /* 64 bit */
2891 case 0xd: /* 64 bit to top half of quad */
2892 break;
2893 default:
2894 /* all other sf/type/rmode combinations are invalid */
2895 unallocated_encoding(s);
2896 break;
2897 }
2898
2899 handle_fmov(s, rd, rn, type, itof);
2900 } else {
2901 /* actual FP conversions */
2902 unsupported_encoding(s, insn);
2903 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00002904}
2905
2906/* FP-specific subcases of table C3-6 (SIMD and FP data processing)
2907 * 31 30 29 28 25 24 0
2908 * +---+---+---+---------+-----------------------------+
2909 * | | 0 | | 1 1 1 1 | |
2910 * +---+---+---+---------+-----------------------------+
2911 */
2912static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
2913{
2914 if (extract32(insn, 24, 1)) {
2915 /* Floating point data-processing (3 source) */
2916 disas_fp_3src(s, insn);
2917 } else if (extract32(insn, 21, 1) == 0) {
2918 /* Floating point to fixed point conversions */
2919 disas_fp_fixed_conv(s, insn);
2920 } else {
2921 switch (extract32(insn, 10, 2)) {
2922 case 1:
2923 /* Floating point conditional compare */
2924 disas_fp_ccomp(s, insn);
2925 break;
2926 case 2:
2927 /* Floating point data-processing (2 source) */
2928 disas_fp_2src(s, insn);
2929 break;
2930 case 3:
2931 /* Floating point conditional select */
2932 disas_fp_csel(s, insn);
2933 break;
2934 case 0:
2935 switch (ctz32(extract32(insn, 12, 4))) {
2936 case 0: /* [15:12] == xxx1 */
2937 /* Floating point immediate */
2938 disas_fp_imm(s, insn);
2939 break;
2940 case 1: /* [15:12] == xx10 */
2941 /* Floating point compare */
2942 disas_fp_compare(s, insn);
2943 break;
2944 case 2: /* [15:12] == x100 */
2945 /* Floating point data-processing (1 source) */
2946 disas_fp_1src(s, insn);
2947 break;
2948 case 3: /* [15:12] == 1000 */
2949 unallocated_encoding(s);
2950 break;
2951 default: /* [15:12] == 0000 */
2952 /* Floating point <-> integer conversions */
2953 disas_fp_int_conv(s, insn);
2954 break;
2955 }
2956 break;
2957 }
2958 }
2959}
2960
2961static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
2962{
2963 /* Note that this is called with all non-FP cases from
2964 * table C3-6 so it must UNDEF for entries not specifically
2965 * allocated to instructions in that table.
2966 */
2967 unsupported_encoding(s, insn);
2968}
2969
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002970/* C3.6 Data processing - SIMD and floating point */
2971static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
2972{
Peter Maydellfaa0ba42013-12-23 23:27:30 +00002973 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
2974 disas_data_proc_fp(s, insn);
2975 } else {
2976 /* SIMD, including crypto */
2977 disas_data_proc_simd(s, insn);
2978 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002979}
2980
2981/* C3.1 A64 instruction index by encoding */
Peter Maydell40f860c2013-12-17 19:42:31 +00002982static void disas_a64_insn(CPUARMState *env, DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +01002983{
2984 uint32_t insn;
2985
2986 insn = arm_ldl_code(env, s->pc, s->bswap_code);
2987 s->insn = insn;
2988 s->pc += 4;
2989
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002990 switch (extract32(insn, 25, 4)) {
2991 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
Alexander Graf14ade102013-09-03 20:12:10 +01002992 unallocated_encoding(s);
2993 break;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002994 case 0x8: case 0x9: /* Data processing - immediate */
2995 disas_data_proc_imm(s, insn);
2996 break;
2997 case 0xa: case 0xb: /* Branch, exception generation and system insns */
2998 disas_b_exc_sys(s, insn);
2999 break;
3000 case 0x4:
3001 case 0x6:
3002 case 0xc:
3003 case 0xe: /* Loads and stores */
3004 disas_ldst(s, insn);
3005 break;
3006 case 0x5:
3007 case 0xd: /* Data processing - register */
3008 disas_data_proc_reg(s, insn);
3009 break;
3010 case 0x7:
3011 case 0xf: /* Data processing - SIMD and floating point */
3012 disas_data_proc_simd_fp(s, insn);
3013 break;
3014 default:
3015 assert(FALSE); /* all 15 cases should be handled above */
3016 break;
Alexander Graf14ade102013-09-03 20:12:10 +01003017 }
Alexander Graf11e169d2013-12-17 19:42:32 +00003018
3019 /* if we allocated any temporaries, free them here */
3020 free_tmp_a64(s);
Peter Maydell40f860c2013-12-17 19:42:31 +00003021}
Alexander Graf14ade102013-09-03 20:12:10 +01003022
Peter Maydell40f860c2013-12-17 19:42:31 +00003023void gen_intermediate_code_internal_a64(ARMCPU *cpu,
3024 TranslationBlock *tb,
3025 bool search_pc)
3026{
3027 CPUState *cs = CPU(cpu);
3028 CPUARMState *env = &cpu->env;
3029 DisasContext dc1, *dc = &dc1;
3030 CPUBreakpoint *bp;
3031 uint16_t *gen_opc_end;
3032 int j, lj;
3033 target_ulong pc_start;
3034 target_ulong next_page_start;
3035 int num_insns;
3036 int max_insns;
3037
3038 pc_start = tb->pc;
3039
3040 dc->tb = tb;
3041
3042 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
3043
3044 dc->is_jmp = DISAS_NEXT;
3045 dc->pc = pc_start;
3046 dc->singlestep_enabled = cs->singlestep_enabled;
3047 dc->condjmp = 0;
3048
3049 dc->aarch64 = 1;
3050 dc->thumb = 0;
3051 dc->bswap_code = 0;
3052 dc->condexec_mask = 0;
3053 dc->condexec_cond = 0;
3054#if !defined(CONFIG_USER_ONLY)
3055 dc->user = 0;
3056#endif
3057 dc->vfp_enabled = 0;
3058 dc->vec_len = 0;
3059 dc->vec_stride = 0;
Peter Maydell60322b32014-01-04 22:15:44 +00003060 dc->cp_regs = cpu->cp_regs;
3061 dc->current_pl = arm_current_pl(env);
Peter Maydell40f860c2013-12-17 19:42:31 +00003062
Alexander Graf11e169d2013-12-17 19:42:32 +00003063 init_tmp_a64_array(dc);
3064
Peter Maydell40f860c2013-12-17 19:42:31 +00003065 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
3066 lj = -1;
3067 num_insns = 0;
3068 max_insns = tb->cflags & CF_COUNT_MASK;
3069 if (max_insns == 0) {
3070 max_insns = CF_COUNT_MASK;
3071 }
3072
3073 gen_tb_start();
3074
3075 tcg_clear_temp_count();
3076
3077 do {
3078 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
3079 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
3080 if (bp->pc == dc->pc) {
3081 gen_exception_insn(dc, 0, EXCP_DEBUG);
3082 /* Advance PC so that clearing the breakpoint will
3083 invalidate this TB. */
3084 dc->pc += 2;
3085 goto done_generating;
3086 }
3087 }
3088 }
3089
3090 if (search_pc) {
3091 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
3092 if (lj < j) {
3093 lj++;
3094 while (lj < j) {
3095 tcg_ctx.gen_opc_instr_start[lj++] = 0;
3096 }
3097 }
3098 tcg_ctx.gen_opc_pc[lj] = dc->pc;
3099 tcg_ctx.gen_opc_instr_start[lj] = 1;
3100 tcg_ctx.gen_opc_icount[lj] = num_insns;
3101 }
3102
3103 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
3104 gen_io_start();
3105 }
3106
3107 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
3108 tcg_gen_debug_insn_start(dc->pc);
3109 }
3110
3111 disas_a64_insn(env, dc);
3112
3113 if (tcg_check_temp_count()) {
3114 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
3115 dc->pc);
3116 }
3117
3118 /* Translation stops when a conditional branch is encountered.
3119 * Otherwise the subsequent code could get translated several times.
3120 * Also stop translation when a page boundary is reached. This
3121 * ensures prefetch aborts occur at the right place.
3122 */
3123 num_insns++;
3124 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
3125 !cs->singlestep_enabled &&
3126 !singlestep &&
3127 dc->pc < next_page_start &&
3128 num_insns < max_insns);
3129
3130 if (tb->cflags & CF_LAST_IO) {
3131 gen_io_end();
3132 }
3133
3134 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
3135 /* Note that this means single stepping WFI doesn't halt the CPU.
3136 * For conditional branch insns this is harmless unreachable code as
3137 * gen_goto_tb() has already handled emitting the debug exception
3138 * (and thus a tb-jump is not possible when singlestepping).
3139 */
3140 assert(dc->is_jmp != DISAS_TB_JUMP);
3141 if (dc->is_jmp != DISAS_JUMP) {
3142 gen_a64_set_pc_im(dc->pc);
3143 }
3144 gen_exception(EXCP_DEBUG);
3145 } else {
3146 switch (dc->is_jmp) {
3147 case DISAS_NEXT:
3148 gen_goto_tb(dc, 1, dc->pc);
3149 break;
3150 default:
Peter Maydell40f860c2013-12-17 19:42:31 +00003151 case DISAS_UPDATE:
Peter Maydellfea50522014-01-04 22:15:45 +00003152 gen_a64_set_pc_im(dc->pc);
3153 /* fall through */
3154 case DISAS_JUMP:
Peter Maydell40f860c2013-12-17 19:42:31 +00003155 /* indicate that the hash table must be used to find the next TB */
3156 tcg_gen_exit_tb(0);
3157 break;
3158 case DISAS_TB_JUMP:
3159 case DISAS_EXC:
3160 case DISAS_SWI:
3161 break;
3162 case DISAS_WFI:
3163 /* This is a special case because we don't want to just halt the CPU
3164 * if trying to debug across a WFI.
3165 */
3166 gen_helper_wfi(cpu_env);
3167 break;
3168 }
3169 }
3170
3171done_generating:
3172 gen_tb_end(tb, num_insns);
3173 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
3174
3175#ifdef DEBUG_DISAS
3176 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
3177 qemu_log("----------------\n");
3178 qemu_log("IN: %s\n", lookup_symbol(pc_start));
3179 log_target_disas(env, pc_start, dc->pc - pc_start,
3180 dc->thumb | (dc->bswap_code << 1));
3181 qemu_log("\n");
3182 }
3183#endif
3184 if (search_pc) {
3185 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
3186 lj++;
3187 while (lj <= j) {
3188 tcg_ctx.gen_opc_instr_start[lj++] = 0;
3189 }
3190 } else {
3191 tb->size = dc->pc - pc_start;
3192 tb->icount = num_insns;
Alexander Graf14ade102013-09-03 20:12:10 +01003193 }
3194}