blob: fccc31cceeccad011b9651ae18282c7c4afcdb14 [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 Maydell089a8d92013-12-03 15:26:18 +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;
Claudio Fontanad41620e2013-12-03 15:12:19 +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
Claudio Fontanad41620e2013-12-03 15:12:19 +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
Claudio Fontanad41620e2013-12-03 15:12:19 +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 Maydell6cd096b2013-11-26 17:21:48 +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 Maydell6cd096b2013-11-26 17:21:48 +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
102void gen_a64_set_pc_im(uint64_t val)
103{
104 tcg_gen_movi_i64(cpu_pc, val);
105}
106
107static void gen_exception(int excp)
108{
109 TCGv_i32 tmp = tcg_temp_new_i32();
110 tcg_gen_movi_i32(tmp, excp);
111 gen_helper_exception(cpu_env, tmp);
112 tcg_temp_free_i32(tmp);
113}
114
115static void gen_exception_insn(DisasContext *s, int offset, int excp)
116{
117 gen_a64_set_pc_im(s->pc - offset);
118 gen_exception(excp);
Peter Maydell089a8d92013-12-03 15:26:18 +0000119 s->is_jmp = DISAS_EXC;
120}
121
122static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
123{
124 /* No direct tb linking with singlestep or deterministic io */
125 if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
126 return false;
127 }
128
129 /* Only link tbs from inside the same guest page */
130 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
131 return false;
132 }
133
134 return true;
135}
136
137static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
138{
139 TranslationBlock *tb;
140
141 tb = s->tb;
142 if (use_goto_tb(s, n, dest)) {
143 tcg_gen_goto_tb(n);
144 gen_a64_set_pc_im(dest);
145 tcg_gen_exit_tb((tcg_target_long)tb + n);
146 s->is_jmp = DISAS_TB_JUMP;
147 } else {
148 gen_a64_set_pc_im(dest);
149 if (s->singlestep_enabled) {
150 gen_exception(EXCP_DEBUG);
151 }
152 tcg_gen_exit_tb(0);
153 s->is_jmp = DISAS_JUMP;
154 }
Alexander Graf14ade102013-09-03 20:12:10 +0100155}
156
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000157static void unallocated_encoding(DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +0100158{
Alexander Graf14ade102013-09-03 20:12:10 +0100159 gen_exception_insn(s, 4, EXCP_UDEF);
160}
161
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000162#define unsupported_encoding(s, insn) \
163 do { \
164 qemu_log_mask(LOG_UNIMP, \
165 "%s:%d: unsupported instruction encoding 0x%08x " \
166 "at pc=%016" PRIx64 "\n", \
167 __FILE__, __LINE__, insn, s->pc - 4); \
168 unallocated_encoding(s); \
169 } while (0);
Alexander Graf14ade102013-09-03 20:12:10 +0100170
Alexander Grafeeed5002013-12-03 15:12:18 +0000171static void free_tmp_a64(DisasContext *s)
172{
173 int i;
174 for (i = 0; i < s->tmp_a64_count; i++) {
175 tcg_temp_free_i64(s->tmp_a64[i]);
176 }
177 s->tmp_a64_count = 0;
178}
179
180static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
181{
182 assert(s->tmp_a64_count < TMP_A64_MAX);
183 return s->tmp_a64[s->tmp_a64_count++] = tcg_const_i64(0);
184}
185
Claudio Fontanad41620e2013-12-03 15:12:19 +0000186/* for accessing a register in 64 bit mode (r/w) */
Alexander Grafeeed5002013-12-03 15:12:18 +0000187static TCGv_i64 cpu_reg(DisasContext *s, int reg)
188{
189 if (reg == 31) {
190 return new_tmp_a64_zero(s);
191 } else {
192 return cpu_X[reg];
193 }
194}
195
Claudio Fontanab5a339a2013-12-03 15:12:21 +0000196/* register access for when 31 == SP */
197static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
198{
199 return cpu_X[reg];
200}
201
Alexander Graf06905b52013-12-03 15:12:19 +0000202/* read a cpu register in 32bit/64bit mode to dst */
203static void read_cpu_reg(DisasContext *s, TCGv_i64 dst, int reg, int sf)
204{
205 if (reg == 31) {
206 tcg_gen_movi_i64(dst, 0);
207 } else if (sf) {
208 tcg_gen_mov_i64(dst, cpu_X[reg]);
209 } else { /* (!sf) */
210 tcg_gen_ext32u_i64(dst, cpu_X[reg]);
211 }
212}
213
Claudio Fontanad41620e2013-12-03 15:12:19 +0000214/* this matches the ARM target semantic for flag variables,
215 but it's not optimal for Aarch64. */
216static inline void gen_logic_CC(int sf, TCGv_i64 result)
217{
218 if (sf) {
219 TCGv_i64 flag = tcg_temp_new_i64();
220 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
221 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
222
223 tcg_gen_shri_i64(flag, result, 32);
224 tcg_gen_trunc_i64_i32(cpu_NF, flag);
225 tcg_temp_free_i64(flag);
226 } else {
227 tcg_gen_trunc_i64_i32(cpu_ZF, result);
228 tcg_gen_trunc_i64_i32(cpu_NF, result);
229 }
230 tcg_gen_movi_i32(cpu_CF, 0);
231 tcg_gen_movi_i32(cpu_VF, 0);
232}
233
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000234/*
235 * the instruction disassembly implemented here matches
236 * the instruction encoding classifications in chapter 3 (C3)
237 * of the ARM Architecture Reference Manual (DDI0487A_a)
238 */
239
Alexander Grafeeed5002013-12-03 15:12:18 +0000240/* C3.2.7 Unconditional branch (immediate)
241 * 31 30 26 25 0
242 * +----+-----------+-------------------------------------+
243 * | op | 0 0 1 0 1 | imm26 |
244 * +----+-----------+-------------------------------------+
245 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000246static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
247{
Alexander Grafeeed5002013-12-03 15:12:18 +0000248 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
249
250 if (insn & (1 << 31)) {
251 /* C5.6.26 BL Branch with link */
252 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
253 }
254
255 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
256 gen_goto_tb(s, 0, addr);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000257}
258
Alexander Graf06905b52013-12-03 15:12:19 +0000259/* C3.2.1 Compare & branch (immediate)
260 * 31 30 25 24 23 5 4 0
261 * +----+-------------+----+---------------------+--------+
262 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
263 * +----+-------------+----+---------------------+--------+
264 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000265static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
266{
Alexander Graf06905b52013-12-03 15:12:19 +0000267 unsigned int sf, op, rt;
268 uint64_t addr;
269 int label_nomatch;
270 TCGv_i64 tcg_cmp;
271
272 sf = extract32(insn, 31, 1);
273 op = extract32(insn, 24, 1);
274 rt = extract32(insn, 0, 5);
275 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
276
277 tcg_cmp = tcg_temp_new_i64();
278 read_cpu_reg(s, tcg_cmp, rt, sf);
279 label_nomatch = gen_new_label();
280
281 if (op) { /* CBNZ */
282 tcg_gen_brcondi_i64(TCG_COND_EQ, tcg_cmp, 0, label_nomatch);
283 } else { /* CBZ */
284 tcg_gen_brcondi_i64(TCG_COND_NE, tcg_cmp, 0, label_nomatch);
285 }
286
287 tcg_temp_free_i64(tcg_cmp);
288
289 gen_goto_tb(s, 0, addr);
290 gen_set_label(label_nomatch);
291 gen_goto_tb(s, 1, s->pc);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000292}
293
Alexander Grafee52d8c2013-12-03 15:12:19 +0000294/* C3.2.5 Test & branch (immediate)
295 * 31 30 25 24 23 19 18 5 4 0
296 * +----+-------------+----+-------+-------------+------+
297 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
298 * +----+-------------+----+-------+-------------+------+
299 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000300static void disas_test_b_imm(DisasContext *s, uint32_t insn)
301{
Alexander Grafee52d8c2013-12-03 15:12:19 +0000302 unsigned int bit_pos, op, rt;
303 uint64_t addr;
304 int label_nomatch;
305 TCGv_i64 tcg_cmp;
306
307 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
308 op = extract32(insn, 24, 1);
309 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
310 rt = extract32(insn, 0, 5);
311
312 tcg_cmp = tcg_temp_new_i64();
313 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
314 label_nomatch = gen_new_label();
315 if (op) { /* TBNZ */
316 tcg_gen_brcondi_i64(TCG_COND_EQ, tcg_cmp, 0, label_nomatch);
317 } else { /* TBZ */
318 tcg_gen_brcondi_i64(TCG_COND_NE, tcg_cmp, 0, label_nomatch);
319 }
320 tcg_temp_free_i64(tcg_cmp);
321 gen_goto_tb(s, 0, addr);
322 gen_set_label(label_nomatch);
323 gen_goto_tb(s, 1, s->pc);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000324}
325
Alexander Grafd0deb6c2013-12-03 15:12:18 +0000326/* C3.2.2 / C5.6.19 Conditional branch (immediate)
327 * 31 25 24 23 5 4 3 0
328 * +---------------+----+---------------------+----+------+
329 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
330 * +---------------+----+---------------------+----+------+
331 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000332static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
333{
Alexander Grafd0deb6c2013-12-03 15:12:18 +0000334 unsigned int cond;
335 uint64_t addr;
336
337 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
338 unallocated_encoding(s);
339 return;
340 }
341 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
342 cond = extract32(insn, 0, 4);
343
344 if (cond < 0x0e) {
345 /* genuinely conditional branches */
346 int label_nomatch = gen_new_label();
347 arm_gen_test_cc(cond ^ 1, label_nomatch);
348 gen_goto_tb(s, 0, addr);
349 gen_set_label(label_nomatch);
350 gen_goto_tb(s, 1, s->pc);
351 } else {
352 /* 0xe and 0xf are both "always" conditions */
353 gen_goto_tb(s, 0, addr);
354 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000355}
356
Claudio Fontana20b3f312013-12-03 15:12:18 +0000357/* C5.6.68 HINT */
358static void handle_hint(DisasContext *s, uint32_t insn,
359 unsigned int op1, unsigned int op2, unsigned int crm)
360{
361 unsigned int selector = crm << 3 | op2;
362
363 if (op1 != 3) {
364 unallocated_encoding(s);
365 return;
366 }
367
368 switch (selector) {
369 case 0: /* NOP */
370 return;
371 case 1: /* YIELD */
372 case 2: /* WFE */
373 case 3: /* WFI */
374 case 4: /* SEV */
375 case 5: /* SEVL */
376 /* we treat all as NOP at least for now */
377 return;
378 default:
379 /* default specified as NOP equivalent */
380 return;
381 }
382}
383
384/* CLREX, DSB, DMB, ISB */
385static void handle_sync(DisasContext *s, uint32_t insn,
386 unsigned int op1, unsigned int op2, unsigned int crm)
387{
388 if (op1 != 3) {
389 unallocated_encoding(s);
390 return;
391 }
392
393 switch (op2) {
394 case 2: /* CLREX */
395 unsupported_encoding(s, insn);
396 return;
397 case 4: /* DSB */
398 case 5: /* DMB */
399 case 6: /* ISB */
400 /* We don't emulate caches so barriers are no-ops */
401 return;
402 default:
403 unallocated_encoding(s);
404 return;
405 }
406}
407
408/* C5.6.130 MSR (immediate) - move immediate to processor state field */
409static void handle_msr_i(DisasContext *s, uint32_t insn,
410 unsigned int op1, unsigned int op2, unsigned int crm)
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000411{
412 unsupported_encoding(s, insn);
413}
414
Claudio Fontana20b3f312013-12-03 15:12:18 +0000415/* C5.6.204 SYS */
416static void handle_sys(DisasContext *s, uint32_t insn, unsigned int l,
417 unsigned int op1, unsigned int op2,
418 unsigned int crn, unsigned int crm, unsigned int rt)
419{
420 unsupported_encoding(s, insn);
421}
422
423/* C5.6.129 MRS - move from system register */
424static void handle_mrs(DisasContext *s, uint32_t insn, unsigned int op0,
425 unsigned int op1, unsigned int op2,
426 unsigned int crn, unsigned int crm, unsigned int rt)
427{
428 unsupported_encoding(s, insn);
429}
430
431/* C5.6.131 MSR (register) - move to system register */
432static void handle_msr(DisasContext *s, uint32_t insn, unsigned int op0,
433 unsigned int op1, unsigned int op2,
434 unsigned int crn, unsigned int crm, unsigned int rt)
435{
436 unsupported_encoding(s, insn);
437}
438
439/* C3.2.4 System */
440static void disas_system(DisasContext *s, uint32_t insn)
441{
442 /*
443 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 16 15 12 11 8 7 5 4 0
444 * 1 1 0 1 0 1 0 1 0 0 L op0 op1 CRn CRm op2 Rt
445 */
446 unsigned int l, op0, op1, crn, crm, op2, rt;
447 l = extract32(insn, 21, 1);
448 op0 = extract32(insn, 19, 2);
449 op1 = extract32(insn, 16, 3);
450 crn = extract32(insn, 12, 4);
451 crm = extract32(insn, 8, 4);
452 op2 = extract32(insn, 5, 3);
453 rt = extract32(insn, 0, 5);
454
455 if (op0 == 0) {
456 if (l || rt != 31) {
457 unallocated_encoding(s);
458 return;
459 }
460 switch (crn) {
461 case 2: /* C5.6.68 HINT */
462 handle_hint(s, insn, op1, op2, crm);
463 break;
464 case 3: /* CLREX, DSB, DMB, ISB */
465 handle_sync(s, insn, op1, op2, crm);
466 break;
467 case 4: /* C5.6.130 MSR (immediate) */
468 handle_msr_i(s, insn, op1, op2, crm);
469 break;
470 default:
471 unallocated_encoding(s);
472 break;
473 }
474 return;
475 }
476
477 if (op0 == 1) {
478 /* C5.6.204 SYS */
479 handle_sys(s, insn, l, op1, op2, crn, crm, rt);
480 } else if (l) { /* op0 > 1 */
481 /* C5.6.129 MRS - move from system register */
482 handle_mrs(s, insn, op0, op1, op2, crn, crm, rt);
483 } else {
484 /* C5.6.131 MSR (register) - move to system register */
485 handle_msr(s, insn, op0, op1, op2, crn, crm, rt);
486 }
487}
488
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000489/* Exception generation */
490static void disas_exc(DisasContext *s, uint32_t insn)
491{
492 unsupported_encoding(s, insn);
493}
494
Alexander Graf37699832013-12-03 15:12:18 +0000495/* C3.2.7 Unconditional branch (register)
496 * 31 25 24 21 20 16 15 10 9 5 4 0
497 * +---------------+-------+-------+-------+------+-------+
498 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
499 * +---------------+-------+-------+-------+------+-------+
500 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000501static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
502{
Alexander Graf37699832013-12-03 15:12:18 +0000503 unsigned int opc, op2, op3, rn, op4;
504
505 opc = extract32(insn, 21, 4);
506 op2 = extract32(insn, 16, 5);
507 op3 = extract32(insn, 10, 6);
508 rn = extract32(insn, 5, 5);
509 op4 = extract32(insn, 0, 5);
510
511 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
512 unallocated_encoding(s);
513 return;
514 }
515
516 switch (opc) {
517 case 0: /* BR */
518 case 2: /* RET */
519 break;
520 case 1: /* BLR */
521 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
522 break;
523 case 4: /* ERET */
524 case 5: /* DRPS */
525 if (rn != 0x1f) {
526 unallocated_encoding(s);
527 } else {
528 unsupported_encoding(s, insn);
529 }
530 return;
531 default:
532 unallocated_encoding(s);
533 return;
534 }
535
536 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
537 s->is_jmp = DISAS_JUMP;
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000538}
539
540/* C3.2 Branches, exception generating and system instructions */
541static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
542{
543 switch (extract32(insn, 25, 7)) {
544 case 0x0a: case 0x0b:
545 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
546 disas_uncond_b_imm(s, insn);
547 break;
548 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
549 disas_comp_b_imm(s, insn);
550 break;
551 case 0x1b: case 0x5b: /* Test & branch (immediate) */
552 disas_test_b_imm(s, insn);
553 break;
554 case 0x2a: /* Conditional branch (immediate) */
555 disas_cond_b_imm(s, insn);
556 break;
557 case 0x6a: /* Exception generation / System */
558 if (insn & (1 << 24)) {
559 disas_system(s, insn);
560 } else {
561 disas_exc(s, insn);
562 }
563 break;
564 case 0x6b: /* Unconditional branch (register) */
565 disas_uncond_b_reg(s, insn);
566 break;
567 default:
568 unallocated_encoding(s);
569 break;
570 }
571}
572
573/* Load/store exclusive */
574static void disas_ldst_excl(DisasContext *s, uint32_t insn)
575{
576 unsupported_encoding(s, insn);
577}
578
579/* Load register (literal) */
580static void disas_ld_lit(DisasContext *s, uint32_t insn)
581{
582 unsupported_encoding(s, insn);
583}
584
585/* Load/store pair (all forms) */
586static void disas_ldst_pair(DisasContext *s, uint32_t insn)
587{
588 unsupported_encoding(s, insn);
589}
590
591/* Load/store register (all forms) */
592static void disas_ldst_reg(DisasContext *s, uint32_t insn)
593{
594 unsupported_encoding(s, insn);
595}
596
597/* AdvSIMD load/store multiple structures */
598static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
599{
600 unsupported_encoding(s, insn);
601}
602
603/* AdvSIMD load/store single structure */
604static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
605{
606 unsupported_encoding(s, insn);
607}
608
609/* C3.3 Loads and stores */
610static void disas_ldst(DisasContext *s, uint32_t insn)
611{
612 switch (extract32(insn, 24, 6)) {
613 case 0x08: /* Load/store exclusive */
614 disas_ldst_excl(s, insn);
615 break;
616 case 0x18: case 0x1c: /* Load register (literal) */
617 disas_ld_lit(s, insn);
618 break;
619 case 0x28: case 0x29:
620 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
621 disas_ldst_pair(s, insn);
622 break;
623 case 0x38: case 0x39:
624 case 0x3c: case 0x3d: /* Load/store register (all forms) */
625 disas_ldst_reg(s, insn);
626 break;
627 case 0x0c: /* AdvSIMD load/store multiple structures */
628 disas_ldst_multiple_struct(s, insn);
629 break;
630 case 0x0d: /* AdvSIMD load/store single structure */
631 disas_ldst_single_struct(s, insn);
632 break;
633 default:
634 unallocated_encoding(s);
635 break;
636 }
637}
638
Claudio Fontana8ff4c2f2013-12-03 15:12:19 +0000639/* C3.4.6 PC-rel. addressing */
640
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000641static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
642{
Claudio Fontana8ff4c2f2013-12-03 15:12:19 +0000643 /*
644 * 31 30 29 28 27 26 25 24 23 5 4 0
645 * op immlo 1 0 0 0 0 immhi Rd
646 */
647 unsigned int page, rd; /* op -> page */
648 uint64_t base;
649 int64_t offset; /* SignExtend(immhi:immlo) -> offset */
650
651 page = insn & (1 << 31) ? 1 : 0;
652 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
653 rd = extract32(insn, 0, 5);
654 base = s->pc - 4;
655
656 if (page) {
657 /* ADRP (page based) */
658 base &= ~0xfff;
659 offset <<= 12; /* apply Zeros */
660 }
661
662 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000663}
664
665/* Add/subtract (immediate) */
666static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
667{
668 unsupported_encoding(s, insn);
669}
670
Claudio Fontanab5a339a2013-12-03 15:12:21 +0000671static uint64_t logic_imm_replicate(uint64_t mask, unsigned int esize)
672{
673 int i;
674 uint64_t out_mask = 0;
675 for (i = 0; (i * esize) < 64; i++) {
676 out_mask = out_mask | (mask << (i * esize));
677 }
678 return out_mask;
679}
680
681static inline uint64_t logic_imm_bitmask(unsigned int len)
682{
683 if (len == 64) {
684 return -1;
685 }
686 return (1ULL << len) - 1;
687}
688
689static uint64_t logic_imm_decode_wmask(unsigned int immn,
690 unsigned int imms, unsigned int immr)
691{
692 uint64_t mask;
693 unsigned len, esize, levels, s, r;
694
695 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
696 esize = 1 << len;
697 levels = (esize - 1) & 0x3f;
698 s = imms & levels;
699 r = immr & levels;
700
701 mask = logic_imm_bitmask(s + 1);
702 mask = (mask >> r) | (mask << (esize - r));
703 mask &= logic_imm_bitmask(esize);
704 mask = logic_imm_replicate(mask, esize);
705 return mask;
706}
707
708/* C3.4.4 Logical (immediate) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000709static void disas_logic_imm(DisasContext *s, uint32_t insn)
710{
Claudio Fontanab5a339a2013-12-03 15:12:21 +0000711 /*
712 * 31 30 29 28 27 26 25 24 23 22 21 16 15 10 9 5 4 0
713 * sf opc 1 0 0 1 0 0 N immr imms Rn Rd
714 */
715 unsigned int sf, opc, is_n, immr, imms, rn, rd;
716 TCGv_i64 tcg_rd, tcg_rn;
717 uint64_t wmask;
718 sf = insn & (1 << 31) ? 1 : 0;
719 opc = extract32(insn, 29, 2);
720 is_n = insn & (1 << 22) ? 1 : 0;
721 immr = extract32(insn, 16, 6);
722 imms = extract32(insn, 10, 6);
723 rn = extract32(insn, 5, 5);
724 rd = extract32(insn, 0, 5);
725
726 if (!sf && is_n) {
727 unallocated_encoding(s);
728 return;
729 }
730
731 if (opc == 0x3) { /* ANDS */
732 tcg_rd = cpu_reg(s, rd);
733 } else {
734 tcg_rd = cpu_reg_sp(s, rd);
735 }
736 tcg_rn = cpu_reg(s, rn);
737
738 wmask = logic_imm_decode_wmask(is_n, imms, immr);
739 if (!sf) {
740 wmask &= 0xffffffff;
741 }
742
743 switch (opc) {
744 case 0x3: /* ANDS */
745 case 0x0: /* AND */
746 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
747 break;
748 case 0x1: /* ORR */
749 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
750 break;
751 case 0x2: /* EOR */
752 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
753 break;
754 default:
755 assert(FALSE); /* must handle all above */
756 break;
757 }
758
759 if (!sf) { /* zero extend final result */
760 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
761 }
762
763 if (opc == 3) { /* ANDS */
764 gen_logic_CC(sf, tcg_rd);
765 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000766}
767
768/* Move wide (immediate) */
769static void disas_movw_imm(DisasContext *s, uint32_t insn)
770{
771 unsupported_encoding(s, insn);
772}
773
Claudio Fontana18f20eb2013-12-03 15:12:21 +0000774/* C3.4.2 Bitfield */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000775static void disas_bitfield(DisasContext *s, uint32_t insn)
776{
Claudio Fontana18f20eb2013-12-03 15:12:21 +0000777 /*
778 * 31 30 29 28 27 26 25 24 23 22 21 16 15 10 9 5 4 0
779 * sf opc 1 0 0 1 1 0 N immr imms Rn Rd
780 */
781 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
782 TCGv_i64 tcg_rd, tcg_tmp;
783 sf = insn & (1 << 31) ? 1 : 0;
784 opc = extract32(insn, 29, 2);
785 n = insn & (1 << 22) ? 1 : 0;
786 ri = extract32(insn, 16, 6);
787 si = extract32(insn, 10, 6);
788 rn = extract32(insn, 5, 5);
789 rd = extract32(insn, 0, 5);
790 bitsize = sf ? 64 : 32;
791
792 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
793 unallocated_encoding(s);
794 return;
795 }
796
797 tcg_rd = cpu_reg(s, rd);
798 tcg_tmp = tcg_temp_new_i64();
799 read_cpu_reg(s, tcg_tmp, rn, sf);
800
801 if (opc != 1) { /* SBFM or UBFM */
802 tcg_gen_movi_i64(tcg_rd, 0);
803 }
804
805 /* do the bit move operation */
806 if (si >= ri) {
807 /* Wd<s-r:0> = Wn<s:r> */
808 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
809 pos = 0;
810 len = (si - ri) + 1;
811 } else {
812 /* Wd<32+s-r,32-r> = Wn<s:0> */
813 pos = bitsize - ri;
814 len = si + 1;
815 }
816
817 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
818 tcg_temp_free_i64(tcg_tmp);
819
820 if (opc == 0) { /* SBFM - sign extend the destination field */
821 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
822 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
823 }
824
825 if (!sf) { /* zero extend final result */
826 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
827 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000828}
829
Claudio Fontana6e7015312013-12-03 15:12:19 +0000830/* C3.4.3 Extract */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000831static void disas_extract(DisasContext *s, uint32_t insn)
832{
Claudio Fontana6e7015312013-12-03 15:12:19 +0000833 /*
834 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
835 * sf [op21] 1 0 0 1 1 1 N o0 Rm imm Rn Rd
836 * [0 0] [0]
837 */
838 unsigned int sf, n, rm, imm, rn, rd, bitsize, op;
839 sf = insn & (1 << 31) ? 1 : 0;
840 n = insn & (1 << 22) ? 1 : 0;
841 rm = extract32(insn, 16, 5);
842 imm = extract32(insn, 10, 6);
843 rn = extract32(insn, 5, 5);
844 rd = extract32(insn, 0, 5);
845 op = insn & (0x3 << 29 | 1 << 21);
846 bitsize = sf ? 64 : 32;
847
848 if (sf != n || op || imm >= bitsize) {
849 unallocated_encoding(s);
850 } else {
851 TCGv_i64 tcg_tmp, tcg_rd;
852 tcg_tmp = tcg_temp_new_i64();
853 tcg_rd = cpu_reg(s, rd);
854
855 read_cpu_reg(s, tcg_tmp, rm, sf);
856 tcg_gen_shri_i64(tcg_rd, tcg_tmp, imm);
857 tcg_gen_shli_i64(tcg_tmp, cpu_reg(s, rn), bitsize - imm);
858 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
859
860 tcg_temp_free_i64(tcg_tmp);
861 if (!sf) {
862 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
863 }
864 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000865}
866
867/* C3.4 Data processing - immediate */
868static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
869{
870 switch (extract32(insn, 23, 6)) {
871 case 0x20: case 0x21: /* PC-rel. addressing */
872 disas_pc_rel_adr(s, insn);
873 break;
874 case 0x22: case 0x23: /* Add/subtract (immediate) */
875 disas_add_sub_imm(s, insn);
876 break;
877 case 0x24: /* Logical (immediate) */
878 disas_logic_imm(s, insn);
879 break;
880 case 0x25: /* Move wide (immediate) */
881 disas_movw_imm(s, insn);
882 break;
883 case 0x26: /* Bitfield */
884 disas_bitfield(s, insn);
885 break;
886 case 0x27: /* Extract */
887 disas_extract(s, insn);
888 break;
889 default:
890 unallocated_encoding(s);
891 break;
892 }
893}
894
Claudio Fontanad41620e2013-12-03 15:12:19 +0000895/* shift a TCGv src by TCGv shift_amount, put result in dst. */
896static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
897 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
898{
899 switch (shift_type) {
900 case A64_SHIFT_TYPE_LSL:
901 tcg_gen_shl_i64(dst, src, shift_amount);
902 break;
903 case A64_SHIFT_TYPE_LSR:
904 tcg_gen_shr_i64(dst, src, shift_amount);
905 break;
906 case A64_SHIFT_TYPE_ASR:
907 if (!sf) {
908 tcg_gen_ext32s_i64(dst, src);
909 }
910 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
911 break;
912 case A64_SHIFT_TYPE_ROR:
913 if (sf) {
914 tcg_gen_rotr_i64(dst, src, shift_amount);
915 } else {
916 TCGv_i32 t0, t1;
917 t0 = tcg_temp_new_i32();
918 t1 = tcg_temp_new_i32();
919 tcg_gen_trunc_i64_i32(t0, src);
920 tcg_gen_trunc_i64_i32(t1, shift_amount);
921 tcg_gen_rotr_i32(t0, t0, t1);
922 tcg_gen_extu_i32_i64(dst, t0);
923 tcg_temp_free_i32(t0);
924 tcg_temp_free_i32(t1);
925 }
926 break;
927 default:
928 assert(FALSE); /* all shift types should be handled */
929 break;
930 }
931
932 if (!sf) { /* zero extend final result */
933 tcg_gen_ext32u_i64(dst, dst);
934 }
935}
936
937/* shift a TCGv src by immediate, put result in dst. */
938static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
939 enum a64_shift_type shift_type, unsigned int shift_i)
940{
941 shift_i = shift_i & (sf ? 63 : 31);
942
943 if (shift_i == 0) {
944 tcg_gen_mov_i64(dst, src);
945 } else {
946 TCGv_i64 shift_const;
947 shift_const = tcg_const_i64(shift_i);
948 shift_reg(dst, src, sf, shift_type, shift_const);
949 tcg_temp_free_i64(shift_const);
950 }
951}
952
953/* C3.5.10 Logical (shifted register) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000954static void disas_logic_reg(DisasContext *s, uint32_t insn)
955{
Claudio Fontanad41620e2013-12-03 15:12:19 +0000956 /*
957 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
958 * sf opc 0 1 0 1 0 shift N Rm imm6 Rn Rd
959 */
960 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
961 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
962 sf = (insn & (1 << 31)) ? 1 : 0;
963 opc = extract32(insn, 29, 2);
964 shift_type = extract32(insn, 22, 2);
965 invert = (insn & (1 << 21)) ? 1 : 0;
966 rm = extract32(insn, 16, 5);
967 shift_amount = extract32(insn, 10, 6);
968 rn = extract32(insn, 5, 5);
969 rd = extract32(insn, 0, 5);
970
971 if (!sf && (shift_amount & (1 << 5))) {
972 unallocated_encoding(s);
973 return;
974 }
975
976 tcg_rm = tcg_temp_new_i64();
977 read_cpu_reg(s, tcg_rm, rm, sf);
978
979 if (shift_amount) {
980 shift_reg_imm(tcg_rm, tcg_rm, sf,
981 shift_type, shift_amount);
982 }
983
984 if (invert) {
985 tcg_gen_not_i64(tcg_rm, tcg_rm);
986 /* we zero extend later on (!sf) */
987 }
988
989 tcg_rd = cpu_reg(s, rd);
990 tcg_rn = cpu_reg(s, rn);
991
992 switch (opc) {
993 case 0: /* AND, BIC */
994 case 3: /* ANDS, BICS */
995 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
996 break;
997 case 1: /* ORR, ORN */
998 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
999 break;
1000 case 2: /* EOR, EON */
1001 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
1002 break;
1003 default:
1004 assert(FALSE); /* must handle all in switch */
1005 break;
1006 }
1007
1008 if (!sf) {
1009 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1010 }
1011
1012 if (opc == 3) {
1013 gen_logic_CC(sf, tcg_rd);
1014 }
1015
1016 tcg_temp_free_i64(tcg_rm);
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001017}
1018
1019/* Add/subtract (extended register) */
1020static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
1021{
1022 unsupported_encoding(s, insn);
1023}
1024
1025/* Add/subtract (shifted register) */
1026static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
1027{
1028 unsupported_encoding(s, insn);
1029}
1030
1031/* Data-processing (3 source) */
1032static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
1033{
1034 unsupported_encoding(s, insn);
1035}
1036
1037/* Add/subtract (with carry) */
1038static void disas_adc_sbc(DisasContext *s, uint32_t insn)
1039{
1040 unsupported_encoding(s, insn);
1041}
1042
1043/* Conditional compare (immediate) */
1044static void disas_cc_imm(DisasContext *s, uint32_t insn)
1045{
1046 unsupported_encoding(s, insn);
1047}
1048
1049/* Conditional compare (register) */
1050static void disas_cc_reg(DisasContext *s, uint32_t insn)
1051{
1052 unsupported_encoding(s, insn);
1053}
1054
Claudio Fontana926f3f32013-12-03 15:12:19 +00001055/* C3.5.6 Conditional select */
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001056static void disas_cond_select(DisasContext *s, uint32_t insn)
1057{
Claudio Fontana926f3f32013-12-03 15:12:19 +00001058 /*
1059 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 0
1060 * sf op S 1 1 0 1 0 1 0 0 Rm cond op2 Rn Rd
1061 * [0]
1062 * op -> else_inv, op2 -> else_inc
1063 */
1064 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
1065 TCGv_i64 tcg_rd;
1066 if (extract32(insn, 21, 9) != 0x0d4 || (insn & (1 << 11))) {
1067 unallocated_encoding(s);
1068 return;
1069 }
1070 sf = (insn & (1 << 31)) ? 1 : 0;
1071 else_inv = extract32(insn, 30, 1);
1072 rm = extract32(insn, 16, 5);
1073 cond = extract32(insn, 12, 4);
1074 else_inc = extract32(insn, 10, 1);
1075 rn = extract32(insn, 5, 5);
1076 rd = extract32(insn, 0, 5);
1077 tcg_rd = cpu_reg(s, rd);
1078
1079 if (cond >= 0x0e) { /* condition "always" */
1080 read_cpu_reg(s, tcg_rd, rn, sf);
1081 } else {
1082 int label_nomatch, label_continue;
1083 label_nomatch = gen_new_label();
1084 label_continue = gen_new_label();
1085
1086 arm_gen_test_cc(cond ^ 1, label_nomatch);
1087 /* match: */
1088 read_cpu_reg(s, tcg_rd, rn, sf);
1089 tcg_gen_br(label_continue);
1090 /* nomatch: */
1091 gen_set_label(label_nomatch);
1092 read_cpu_reg(s, tcg_rd, rm, sf);
1093 if (else_inv) {
1094 tcg_gen_not_i64(tcg_rd, tcg_rd);
1095 }
1096 if (else_inc) {
1097 tcg_gen_addi_i64(tcg_rd, tcg_rd, 1);
1098 }
1099 if (!sf) {
1100 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1101 }
1102 /* continue: */
1103 gen_set_label(label_continue);
1104 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001105}
1106
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001107static void handle_clz(DisasContext *s, unsigned int sf,
1108 unsigned int rn, unsigned int rd)
1109{
1110 TCGv_i64 tcg_rd, tcg_rn;
1111 tcg_rd = cpu_reg(s, rd);
1112 tcg_rn = cpu_reg(s, rn);
1113
1114 if (sf) {
1115 gen_helper_clz64(tcg_rd, tcg_rn);
1116 } else {
1117 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
1118 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
1119 gen_helper_clz(tcg_tmp32, tcg_tmp32);
1120 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
1121 tcg_temp_free_i32(tcg_tmp32);
1122 }
1123}
1124
Claudio Fontanaded37772013-12-03 15:12:21 +00001125static void handle_cls(DisasContext *s, unsigned int sf,
1126 unsigned int rn, unsigned int rd)
1127{
1128 TCGv_i64 tcg_rd, tcg_rn;
1129 tcg_rd = cpu_reg(s, rd);
1130 tcg_rn = cpu_reg(s, rn);
1131
1132 if (sf) {
1133 gen_helper_cls64(tcg_rd, tcg_rn);
1134 } else {
1135 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
1136 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
1137 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
1138 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
1139 tcg_temp_free_i32(tcg_tmp32);
1140 }
1141}
1142
Claudio Fontana071b11d2013-12-03 15:12:20 +00001143static void handle_rbit(DisasContext *s, unsigned int sf,
1144 unsigned int rn, unsigned int rd)
1145{
1146 TCGv_i64 tcg_rd, tcg_rn;
1147 tcg_rd = cpu_reg(s, rd);
1148 tcg_rn = cpu_reg(s, rn);
1149
1150 if (sf) {
1151 gen_helper_rbit64(tcg_rd, tcg_rn);
1152 } else {
1153 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
1154 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
1155 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
1156 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
1157 tcg_temp_free_i32(tcg_tmp32);
1158 }
1159}
1160
Claudio Fontanacdd4f722013-12-03 15:12:20 +00001161/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
1162static void handle_rev64(DisasContext *s, unsigned int sf,
1163 unsigned int rn, unsigned int rd)
1164{
1165 if (!sf) {
1166 unallocated_encoding(s);
1167 return;
1168 }
1169 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
1170}
1171
1172/* C5.6.149 REV with sf==0, opcode==2 */
1173/* C5.6.151 REV32 (sf==1, opcode==2) */
1174static void handle_rev32(DisasContext *s, unsigned int sf,
1175 unsigned int rn, unsigned int rd)
1176{
1177 TCGv_i64 tcg_rd, tcg_rn;
1178 tcg_rd = cpu_reg(s, rd);
1179 tcg_rn = cpu_reg(s, rn);
1180
1181 if (sf) {
1182 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1183 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffffffff);
1184 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
1185 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
1186 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
1187 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 32);
1188 tcg_temp_free_i64(tcg_tmp);
1189 } else {
1190 tcg_gen_ext32u_i64(tcg_rd, tcg_rn);
1191 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
1192 }
1193}
1194
1195/* C5.6.150 REV16 (opcode==1) */
1196static void handle_rev16(DisasContext *s, unsigned int sf,
1197 unsigned int rn, unsigned int rd)
1198{
1199 TCGv_i64 tcg_rd, tcg_rn, tcg_tmp;
1200 tcg_rd = cpu_reg(s, rd);
1201 tcg_rn = cpu_reg(s, rn);
1202
1203 tcg_tmp = tcg_temp_new_i64();
1204 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
1205 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
1206
1207 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
1208 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
1209 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
1210 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
1211
1212 if (!sf) { /* done */
1213 tcg_temp_free_i64(tcg_tmp);
1214 return;
1215 }
1216
1217 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
1218 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
1219 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
1220 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
1221
1222 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
1223 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
1224 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
1225
1226 tcg_temp_free_i64(tcg_tmp);
1227}
1228
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001229/* C3.5.7 Data-processing (1 source) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001230static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
1231{
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001232 /*
1233 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
1234 * sf 1 S 1 1 0 1 0 1 1 0 opcode2 opcode Rn Rd
1235 * [0] [0 0 0 0 0]
1236 */
1237 unsigned int sf, opcode, rn, rd;
1238 if (extract32(insn, 16, 15) != 0x5ac0) {
1239 unallocated_encoding(s);
1240 return;
1241 }
1242 sf = insn & (1 << 31) ? 1 : 0;
1243 opcode = extract32(insn, 10, 6);
1244 rn = extract32(insn, 5, 5);
1245 rd = extract32(insn, 0, 5);
1246
1247 switch (opcode) {
1248 case 0: /* RBIT */
Claudio Fontana071b11d2013-12-03 15:12:20 +00001249 handle_rbit(s, sf, rn, rd);
1250 break;
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001251 case 1: /* REV16 */
Claudio Fontanacdd4f722013-12-03 15:12:20 +00001252 handle_rev16(s, sf, rn, rd);
1253 break;
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001254 case 2: /* REV32 */
Claudio Fontanacdd4f722013-12-03 15:12:20 +00001255 handle_rev32(s, sf, rn, rd);
1256 break;
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001257 case 3: /* REV64 */
Claudio Fontanacdd4f722013-12-03 15:12:20 +00001258 handle_rev64(s, sf, rn, rd);
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001259 break;
1260 case 4: /* CLZ */
1261 handle_clz(s, sf, rn, rd);
1262 break;
1263 case 5: /* CLS */
Claudio Fontanaded37772013-12-03 15:12:21 +00001264 handle_cls(s, sf, rn, rd);
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001265 break;
1266 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001267}
1268
Claudio Fontana11861fc2013-12-03 15:12:20 +00001269static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
1270 unsigned int rm, unsigned int rn, unsigned int rd)
1271{
1272 TCGv_i64 tcg_n, tcg_m, tcg_rd;
1273 tcg_n = tcg_temp_new_i64();
1274 tcg_m = tcg_temp_new_i64();
1275 tcg_rd = cpu_reg(s, rd);
1276
1277 if (!sf && is_signed) {
1278 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
1279 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
1280 } else {
1281 read_cpu_reg(s, tcg_n, rn, sf);
1282 read_cpu_reg(s, tcg_m, rm, sf);
1283 }
1284
1285 if (is_signed) {
1286 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
1287 } else {
1288 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
1289 }
1290
1291 tcg_temp_free_i64(tcg_n);
1292 tcg_temp_free_i64(tcg_m);
1293
1294 if (!sf) { /* zero extend final result */
1295 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1296 }
1297}
1298
Claudio Fontanae03cad52013-12-03 15:12:20 +00001299/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
1300static void handle_shift_reg(DisasContext *s,
1301 enum a64_shift_type shift_type, unsigned int sf,
1302 unsigned int rm, unsigned int rn, unsigned int rd)
1303{
1304 TCGv_i64 tcg_shift = tcg_temp_new_i64();
1305 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
1306 shift_reg(cpu_reg(s, rd), cpu_reg(s, rn), sf, shift_type, tcg_shift);
1307 tcg_temp_free_i64(tcg_shift);
1308}
1309
Claudio Fontana11861fc2013-12-03 15:12:20 +00001310/* C3.5.8 Data-processing (2 source) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001311static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
1312{
Claudio Fontana11861fc2013-12-03 15:12:20 +00001313 /*
1314 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
1315 * sf 0 S 1 1 0 1 0 1 1 0 Rm opcode Rn Rd
1316 * [0]
1317 */
1318 unsigned int sf, rm, opcode, rn, rd;
1319 sf = insn & (1 << 31) ? 1 : 0;
1320 rm = extract32(insn, 16, 5);
1321 opcode = extract32(insn, 10, 6);
1322 rn = extract32(insn, 5, 5);
1323 rd = extract32(insn, 0, 5);
1324
1325 if (extract32(insn, 21, 10) != 0x0d6) {
1326 unallocated_encoding(s);
1327 return;
1328 }
1329
1330 switch (opcode) {
1331 case 2: /* UDIV */
1332 handle_div(s, FALSE, sf, rm, rn, rd);
1333 break;
1334 case 3: /* SDIV */
1335 handle_div(s, TRUE, sf, rm, rn, rd);
1336 break;
1337 case 8: /* LSLV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001338 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
1339 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001340 case 9: /* LSRV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001341 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
1342 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001343 case 10: /* ASRV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001344 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
1345 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001346 case 11: /* RORV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001347 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
1348 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001349 case 16:
1350 case 17:
1351 case 18:
1352 case 19:
1353 case 20:
1354 case 21:
1355 case 22:
1356 case 23: /* CRC32 */
1357 unsupported_encoding(s, insn);
1358 break;
1359 default:
1360 unallocated_encoding(s);
1361 break;
1362 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001363}
1364
1365/* C3.5 Data processing - register */
1366static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
1367{
1368 switch (extract32(insn, 24, 5)) {
1369 case 0x0a: /* Logical (shifted register) */
1370 disas_logic_reg(s, insn);
1371 break;
1372 case 0x0b: /* Add/subtract */
1373 if (insn & (1 << 21)) { /* (extended register) */
1374 disas_add_sub_ext_reg(s, insn);
1375 } else {
1376 disas_add_sub_reg(s, insn);
1377 }
1378 break;
1379 case 0x1b: /* Data-processing (3 source) */
1380 disas_data_proc_3src(s, insn);
1381 break;
1382 case 0x1a:
1383 switch (extract32(insn, 21, 3)) {
1384 case 0x0: /* Add/subtract (with carry) */
1385 disas_adc_sbc(s, insn);
1386 break;
1387 case 0x2: /* Conditional compare */
1388 if (insn & (1 << 11)) { /* (immediate) */
1389 disas_cc_imm(s, insn);
1390 } else { /* (register) */
1391 disas_cc_reg(s, insn);
1392 }
1393 break;
1394 case 0x4: /* Conditional select */
1395 disas_cond_select(s, insn);
1396 break;
1397 case 0x6: /* Data-processing */
1398 if (insn & (1 << 30)) { /* (1 source) */
1399 disas_data_proc_1src(s, insn);
1400 } else { /* (2 source) */
1401 disas_data_proc_2src(s, insn);
1402 }
1403 break;
1404 default:
1405 unallocated_encoding(s);
1406 break;
1407 }
1408 break;
1409 default:
1410 unallocated_encoding(s);
1411 break;
1412 }
1413}
1414
1415/* C3.6 Data processing - SIMD and floating point */
1416static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
1417{
1418 unsupported_encoding(s, insn);
1419}
1420
1421/* C3.1 A64 instruction index by encoding */
Peter Maydell089a8d92013-12-03 15:26:18 +00001422static void disas_a64_insn(CPUARMState *env, DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +01001423{
1424 uint32_t insn;
1425
1426 insn = arm_ldl_code(env, s->pc, s->bswap_code);
1427 s->insn = insn;
1428 s->pc += 4;
1429
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001430 switch (extract32(insn, 25, 4)) {
1431 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
Alexander Graf14ade102013-09-03 20:12:10 +01001432 unallocated_encoding(s);
1433 break;
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001434 case 0x8: case 0x9: /* Data processing - immediate */
1435 disas_data_proc_imm(s, insn);
1436 break;
1437 case 0xa: case 0xb: /* Branch, exception generation and system insns */
1438 disas_b_exc_sys(s, insn);
1439 break;
1440 case 0x4:
1441 case 0x6:
1442 case 0xc:
1443 case 0xe: /* Loads and stores */
1444 disas_ldst(s, insn);
1445 break;
1446 case 0x5:
1447 case 0xd: /* Data processing - register */
1448 disas_data_proc_reg(s, insn);
1449 break;
1450 case 0x7:
1451 case 0xf: /* Data processing - SIMD and floating point */
1452 disas_data_proc_simd_fp(s, insn);
1453 break;
1454 default:
1455 assert(FALSE); /* all 15 cases should be handled above */
1456 break;
Alexander Graf14ade102013-09-03 20:12:10 +01001457 }
Alexander Grafeeed5002013-12-03 15:12:18 +00001458
1459 /* if we allocated any temporaries, free them here */
1460 free_tmp_a64(s);
Peter Maydell089a8d92013-12-03 15:26:18 +00001461}
Alexander Graf14ade102013-09-03 20:12:10 +01001462
Peter Maydell089a8d92013-12-03 15:26:18 +00001463void gen_intermediate_code_internal_a64(ARMCPU *cpu,
1464 TranslationBlock *tb,
1465 bool search_pc)
1466{
1467 CPUState *cs = CPU(cpu);
1468 CPUARMState *env = &cpu->env;
1469 DisasContext dc1, *dc = &dc1;
1470 CPUBreakpoint *bp;
1471 uint16_t *gen_opc_end;
1472 int j, lj;
1473 target_ulong pc_start;
1474 target_ulong next_page_start;
1475 int num_insns;
1476 int max_insns;
1477
1478 pc_start = tb->pc;
1479
1480 dc->tb = tb;
1481
1482 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
1483
1484 dc->is_jmp = DISAS_NEXT;
1485 dc->pc = pc_start;
1486 dc->singlestep_enabled = cs->singlestep_enabled;
1487 dc->condjmp = 0;
1488
1489 dc->aarch64 = 1;
Alexander Grafeeed5002013-12-03 15:12:18 +00001490 dc->tmp_a64_count = 0;
Peter Maydell089a8d92013-12-03 15:26:18 +00001491 dc->thumb = 0;
1492 dc->bswap_code = 0;
1493 dc->condexec_mask = 0;
1494 dc->condexec_cond = 0;
1495#if !defined(CONFIG_USER_ONLY)
1496 dc->user = 0;
1497#endif
1498 dc->vfp_enabled = 0;
1499 dc->vec_len = 0;
1500 dc->vec_stride = 0;
1501
1502 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1503 lj = -1;
1504 num_insns = 0;
1505 max_insns = tb->cflags & CF_COUNT_MASK;
1506 if (max_insns == 0) {
1507 max_insns = CF_COUNT_MASK;
1508 }
1509
1510 gen_tb_start();
1511
1512 tcg_clear_temp_count();
1513
1514 do {
1515 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
1516 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1517 if (bp->pc == dc->pc) {
1518 gen_exception_insn(dc, 0, EXCP_DEBUG);
1519 /* Advance PC so that clearing the breakpoint will
1520 invalidate this TB. */
1521 dc->pc += 2;
1522 goto done_generating;
1523 }
1524 }
1525 }
1526
1527 if (search_pc) {
1528 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1529 if (lj < j) {
1530 lj++;
1531 while (lj < j) {
1532 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1533 }
1534 }
1535 tcg_ctx.gen_opc_pc[lj] = dc->pc;
1536 tcg_ctx.gen_opc_instr_start[lj] = 1;
1537 tcg_ctx.gen_opc_icount[lj] = num_insns;
1538 }
1539
1540 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
1541 gen_io_start();
1542 }
1543
1544 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
1545 tcg_gen_debug_insn_start(dc->pc);
1546 }
1547
1548 disas_a64_insn(env, dc);
1549
1550 if (tcg_check_temp_count()) {
1551 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
1552 dc->pc);
1553 }
1554
1555 /* Translation stops when a conditional branch is encountered.
1556 * Otherwise the subsequent code could get translated several times.
1557 * Also stop translation when a page boundary is reached. This
1558 * ensures prefetch aborts occur at the right place.
1559 */
1560 num_insns++;
1561 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
1562 !cs->singlestep_enabled &&
1563 !singlestep &&
1564 dc->pc < next_page_start &&
1565 num_insns < max_insns);
1566
1567 if (tb->cflags & CF_LAST_IO) {
1568 gen_io_end();
1569 }
1570
1571 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
1572 /* Note that this means single stepping WFI doesn't halt the CPU.
1573 * For conditional branch insns this is harmless unreachable code as
1574 * gen_goto_tb() has already handled emitting the debug exception
1575 * (and thus a tb-jump is not possible when singlestepping).
1576 */
1577 assert(dc->is_jmp != DISAS_TB_JUMP);
1578 if (dc->is_jmp != DISAS_JUMP) {
1579 gen_a64_set_pc_im(dc->pc);
1580 }
1581 gen_exception(EXCP_DEBUG);
1582 } else {
1583 switch (dc->is_jmp) {
1584 case DISAS_NEXT:
1585 gen_goto_tb(dc, 1, dc->pc);
1586 break;
1587 default:
1588 case DISAS_JUMP:
1589 case DISAS_UPDATE:
1590 /* indicate that the hash table must be used to find the next TB */
1591 tcg_gen_exit_tb(0);
1592 break;
1593 case DISAS_TB_JUMP:
1594 case DISAS_EXC:
1595 case DISAS_SWI:
1596 break;
1597 case DISAS_WFI:
1598 /* This is a special case because we don't want to just halt the CPU
1599 * if trying to debug across a WFI.
1600 */
1601 gen_helper_wfi(cpu_env);
1602 break;
1603 }
1604 }
1605
1606done_generating:
1607 gen_tb_end(tb, num_insns);
1608 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
1609
1610#ifdef DEBUG_DISAS
1611 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1612 qemu_log("----------------\n");
1613 qemu_log("IN: %s\n", lookup_symbol(pc_start));
1614 log_target_disas(env, pc_start, dc->pc - pc_start,
1615 dc->thumb | (dc->bswap_code << 1));
1616 qemu_log("\n");
1617 }
1618#endif
1619 if (search_pc) {
1620 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1621 lj++;
1622 while (lj <= j) {
1623 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1624 }
1625 } else {
1626 tb->size = dc->pc - pc_start;
1627 tb->icount = num_insns;
Alexander Graf14ade102013-09-03 20:12:10 +01001628 }
1629}