blob: 59bedc40f19b1707ba32a2ce28bcac35b4fbf090 [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
Alexander Graf06905b52013-12-03 15:12:19 +0000196/* read a cpu register in 32bit/64bit mode to dst */
197static void read_cpu_reg(DisasContext *s, TCGv_i64 dst, int reg, int sf)
198{
199 if (reg == 31) {
200 tcg_gen_movi_i64(dst, 0);
201 } else if (sf) {
202 tcg_gen_mov_i64(dst, cpu_X[reg]);
203 } else { /* (!sf) */
204 tcg_gen_ext32u_i64(dst, cpu_X[reg]);
205 }
206}
207
Claudio Fontanad41620e2013-12-03 15:12:19 +0000208/* this matches the ARM target semantic for flag variables,
209 but it's not optimal for Aarch64. */
210static inline void gen_logic_CC(int sf, TCGv_i64 result)
211{
212 if (sf) {
213 TCGv_i64 flag = tcg_temp_new_i64();
214 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
215 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
216
217 tcg_gen_shri_i64(flag, result, 32);
218 tcg_gen_trunc_i64_i32(cpu_NF, flag);
219 tcg_temp_free_i64(flag);
220 } else {
221 tcg_gen_trunc_i64_i32(cpu_ZF, result);
222 tcg_gen_trunc_i64_i32(cpu_NF, result);
223 }
224 tcg_gen_movi_i32(cpu_CF, 0);
225 tcg_gen_movi_i32(cpu_VF, 0);
226}
227
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000228/*
229 * the instruction disassembly implemented here matches
230 * the instruction encoding classifications in chapter 3 (C3)
231 * of the ARM Architecture Reference Manual (DDI0487A_a)
232 */
233
Alexander Grafeeed5002013-12-03 15:12:18 +0000234/* C3.2.7 Unconditional branch (immediate)
235 * 31 30 26 25 0
236 * +----+-----------+-------------------------------------+
237 * | op | 0 0 1 0 1 | imm26 |
238 * +----+-----------+-------------------------------------+
239 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000240static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
241{
Alexander Grafeeed5002013-12-03 15:12:18 +0000242 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
243
244 if (insn & (1 << 31)) {
245 /* C5.6.26 BL Branch with link */
246 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
247 }
248
249 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
250 gen_goto_tb(s, 0, addr);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000251}
252
Alexander Graf06905b52013-12-03 15:12:19 +0000253/* C3.2.1 Compare & branch (immediate)
254 * 31 30 25 24 23 5 4 0
255 * +----+-------------+----+---------------------+--------+
256 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
257 * +----+-------------+----+---------------------+--------+
258 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000259static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
260{
Alexander Graf06905b52013-12-03 15:12:19 +0000261 unsigned int sf, op, rt;
262 uint64_t addr;
263 int label_nomatch;
264 TCGv_i64 tcg_cmp;
265
266 sf = extract32(insn, 31, 1);
267 op = extract32(insn, 24, 1);
268 rt = extract32(insn, 0, 5);
269 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
270
271 tcg_cmp = tcg_temp_new_i64();
272 read_cpu_reg(s, tcg_cmp, rt, sf);
273 label_nomatch = gen_new_label();
274
275 if (op) { /* CBNZ */
276 tcg_gen_brcondi_i64(TCG_COND_EQ, tcg_cmp, 0, label_nomatch);
277 } else { /* CBZ */
278 tcg_gen_brcondi_i64(TCG_COND_NE, tcg_cmp, 0, label_nomatch);
279 }
280
281 tcg_temp_free_i64(tcg_cmp);
282
283 gen_goto_tb(s, 0, addr);
284 gen_set_label(label_nomatch);
285 gen_goto_tb(s, 1, s->pc);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000286}
287
Alexander Grafee52d8c2013-12-03 15:12:19 +0000288/* C3.2.5 Test & branch (immediate)
289 * 31 30 25 24 23 19 18 5 4 0
290 * +----+-------------+----+-------+-------------+------+
291 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
292 * +----+-------------+----+-------+-------------+------+
293 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000294static void disas_test_b_imm(DisasContext *s, uint32_t insn)
295{
Alexander Grafee52d8c2013-12-03 15:12:19 +0000296 unsigned int bit_pos, op, rt;
297 uint64_t addr;
298 int label_nomatch;
299 TCGv_i64 tcg_cmp;
300
301 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
302 op = extract32(insn, 24, 1);
303 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
304 rt = extract32(insn, 0, 5);
305
306 tcg_cmp = tcg_temp_new_i64();
307 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
308 label_nomatch = gen_new_label();
309 if (op) { /* TBNZ */
310 tcg_gen_brcondi_i64(TCG_COND_EQ, tcg_cmp, 0, label_nomatch);
311 } else { /* TBZ */
312 tcg_gen_brcondi_i64(TCG_COND_NE, tcg_cmp, 0, label_nomatch);
313 }
314 tcg_temp_free_i64(tcg_cmp);
315 gen_goto_tb(s, 0, addr);
316 gen_set_label(label_nomatch);
317 gen_goto_tb(s, 1, s->pc);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000318}
319
Alexander Grafd0deb6c2013-12-03 15:12:18 +0000320/* C3.2.2 / C5.6.19 Conditional branch (immediate)
321 * 31 25 24 23 5 4 3 0
322 * +---------------+----+---------------------+----+------+
323 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
324 * +---------------+----+---------------------+----+------+
325 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000326static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
327{
Alexander Grafd0deb6c2013-12-03 15:12:18 +0000328 unsigned int cond;
329 uint64_t addr;
330
331 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
332 unallocated_encoding(s);
333 return;
334 }
335 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
336 cond = extract32(insn, 0, 4);
337
338 if (cond < 0x0e) {
339 /* genuinely conditional branches */
340 int label_nomatch = gen_new_label();
341 arm_gen_test_cc(cond ^ 1, label_nomatch);
342 gen_goto_tb(s, 0, addr);
343 gen_set_label(label_nomatch);
344 gen_goto_tb(s, 1, s->pc);
345 } else {
346 /* 0xe and 0xf are both "always" conditions */
347 gen_goto_tb(s, 0, addr);
348 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000349}
350
Claudio Fontana20b3f312013-12-03 15:12:18 +0000351/* C5.6.68 HINT */
352static void handle_hint(DisasContext *s, uint32_t insn,
353 unsigned int op1, unsigned int op2, unsigned int crm)
354{
355 unsigned int selector = crm << 3 | op2;
356
357 if (op1 != 3) {
358 unallocated_encoding(s);
359 return;
360 }
361
362 switch (selector) {
363 case 0: /* NOP */
364 return;
365 case 1: /* YIELD */
366 case 2: /* WFE */
367 case 3: /* WFI */
368 case 4: /* SEV */
369 case 5: /* SEVL */
370 /* we treat all as NOP at least for now */
371 return;
372 default:
373 /* default specified as NOP equivalent */
374 return;
375 }
376}
377
378/* CLREX, DSB, DMB, ISB */
379static void handle_sync(DisasContext *s, uint32_t insn,
380 unsigned int op1, unsigned int op2, unsigned int crm)
381{
382 if (op1 != 3) {
383 unallocated_encoding(s);
384 return;
385 }
386
387 switch (op2) {
388 case 2: /* CLREX */
389 unsupported_encoding(s, insn);
390 return;
391 case 4: /* DSB */
392 case 5: /* DMB */
393 case 6: /* ISB */
394 /* We don't emulate caches so barriers are no-ops */
395 return;
396 default:
397 unallocated_encoding(s);
398 return;
399 }
400}
401
402/* C5.6.130 MSR (immediate) - move immediate to processor state field */
403static void handle_msr_i(DisasContext *s, uint32_t insn,
404 unsigned int op1, unsigned int op2, unsigned int crm)
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000405{
406 unsupported_encoding(s, insn);
407}
408
Claudio Fontana20b3f312013-12-03 15:12:18 +0000409/* C5.6.204 SYS */
410static void handle_sys(DisasContext *s, uint32_t insn, unsigned int l,
411 unsigned int op1, unsigned int op2,
412 unsigned int crn, unsigned int crm, unsigned int rt)
413{
414 unsupported_encoding(s, insn);
415}
416
417/* C5.6.129 MRS - move from system register */
418static void handle_mrs(DisasContext *s, uint32_t insn, unsigned int op0,
419 unsigned int op1, unsigned int op2,
420 unsigned int crn, unsigned int crm, unsigned int rt)
421{
422 unsupported_encoding(s, insn);
423}
424
425/* C5.6.131 MSR (register) - move to system register */
426static void handle_msr(DisasContext *s, uint32_t insn, unsigned int op0,
427 unsigned int op1, unsigned int op2,
428 unsigned int crn, unsigned int crm, unsigned int rt)
429{
430 unsupported_encoding(s, insn);
431}
432
433/* C3.2.4 System */
434static void disas_system(DisasContext *s, uint32_t insn)
435{
436 /*
437 * 31 30 29 28 27 26 25 24 23 22 21 20 19 18 16 15 12 11 8 7 5 4 0
438 * 1 1 0 1 0 1 0 1 0 0 L op0 op1 CRn CRm op2 Rt
439 */
440 unsigned int l, op0, op1, crn, crm, op2, rt;
441 l = extract32(insn, 21, 1);
442 op0 = extract32(insn, 19, 2);
443 op1 = extract32(insn, 16, 3);
444 crn = extract32(insn, 12, 4);
445 crm = extract32(insn, 8, 4);
446 op2 = extract32(insn, 5, 3);
447 rt = extract32(insn, 0, 5);
448
449 if (op0 == 0) {
450 if (l || rt != 31) {
451 unallocated_encoding(s);
452 return;
453 }
454 switch (crn) {
455 case 2: /* C5.6.68 HINT */
456 handle_hint(s, insn, op1, op2, crm);
457 break;
458 case 3: /* CLREX, DSB, DMB, ISB */
459 handle_sync(s, insn, op1, op2, crm);
460 break;
461 case 4: /* C5.6.130 MSR (immediate) */
462 handle_msr_i(s, insn, op1, op2, crm);
463 break;
464 default:
465 unallocated_encoding(s);
466 break;
467 }
468 return;
469 }
470
471 if (op0 == 1) {
472 /* C5.6.204 SYS */
473 handle_sys(s, insn, l, op1, op2, crn, crm, rt);
474 } else if (l) { /* op0 > 1 */
475 /* C5.6.129 MRS - move from system register */
476 handle_mrs(s, insn, op0, op1, op2, crn, crm, rt);
477 } else {
478 /* C5.6.131 MSR (register) - move to system register */
479 handle_msr(s, insn, op0, op1, op2, crn, crm, rt);
480 }
481}
482
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000483/* Exception generation */
484static void disas_exc(DisasContext *s, uint32_t insn)
485{
486 unsupported_encoding(s, insn);
487}
488
Alexander Graf37699832013-12-03 15:12:18 +0000489/* C3.2.7 Unconditional branch (register)
490 * 31 25 24 21 20 16 15 10 9 5 4 0
491 * +---------------+-------+-------+-------+------+-------+
492 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
493 * +---------------+-------+-------+-------+------+-------+
494 */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000495static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
496{
Alexander Graf37699832013-12-03 15:12:18 +0000497 unsigned int opc, op2, op3, rn, op4;
498
499 opc = extract32(insn, 21, 4);
500 op2 = extract32(insn, 16, 5);
501 op3 = extract32(insn, 10, 6);
502 rn = extract32(insn, 5, 5);
503 op4 = extract32(insn, 0, 5);
504
505 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
506 unallocated_encoding(s);
507 return;
508 }
509
510 switch (opc) {
511 case 0: /* BR */
512 case 2: /* RET */
513 break;
514 case 1: /* BLR */
515 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
516 break;
517 case 4: /* ERET */
518 case 5: /* DRPS */
519 if (rn != 0x1f) {
520 unallocated_encoding(s);
521 } else {
522 unsupported_encoding(s, insn);
523 }
524 return;
525 default:
526 unallocated_encoding(s);
527 return;
528 }
529
530 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
531 s->is_jmp = DISAS_JUMP;
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000532}
533
534/* C3.2 Branches, exception generating and system instructions */
535static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
536{
537 switch (extract32(insn, 25, 7)) {
538 case 0x0a: case 0x0b:
539 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
540 disas_uncond_b_imm(s, insn);
541 break;
542 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
543 disas_comp_b_imm(s, insn);
544 break;
545 case 0x1b: case 0x5b: /* Test & branch (immediate) */
546 disas_test_b_imm(s, insn);
547 break;
548 case 0x2a: /* Conditional branch (immediate) */
549 disas_cond_b_imm(s, insn);
550 break;
551 case 0x6a: /* Exception generation / System */
552 if (insn & (1 << 24)) {
553 disas_system(s, insn);
554 } else {
555 disas_exc(s, insn);
556 }
557 break;
558 case 0x6b: /* Unconditional branch (register) */
559 disas_uncond_b_reg(s, insn);
560 break;
561 default:
562 unallocated_encoding(s);
563 break;
564 }
565}
566
567/* Load/store exclusive */
568static void disas_ldst_excl(DisasContext *s, uint32_t insn)
569{
570 unsupported_encoding(s, insn);
571}
572
573/* Load register (literal) */
574static void disas_ld_lit(DisasContext *s, uint32_t insn)
575{
576 unsupported_encoding(s, insn);
577}
578
579/* Load/store pair (all forms) */
580static void disas_ldst_pair(DisasContext *s, uint32_t insn)
581{
582 unsupported_encoding(s, insn);
583}
584
585/* Load/store register (all forms) */
586static void disas_ldst_reg(DisasContext *s, uint32_t insn)
587{
588 unsupported_encoding(s, insn);
589}
590
591/* AdvSIMD load/store multiple structures */
592static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
593{
594 unsupported_encoding(s, insn);
595}
596
597/* AdvSIMD load/store single structure */
598static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
599{
600 unsupported_encoding(s, insn);
601}
602
603/* C3.3 Loads and stores */
604static void disas_ldst(DisasContext *s, uint32_t insn)
605{
606 switch (extract32(insn, 24, 6)) {
607 case 0x08: /* Load/store exclusive */
608 disas_ldst_excl(s, insn);
609 break;
610 case 0x18: case 0x1c: /* Load register (literal) */
611 disas_ld_lit(s, insn);
612 break;
613 case 0x28: case 0x29:
614 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
615 disas_ldst_pair(s, insn);
616 break;
617 case 0x38: case 0x39:
618 case 0x3c: case 0x3d: /* Load/store register (all forms) */
619 disas_ldst_reg(s, insn);
620 break;
621 case 0x0c: /* AdvSIMD load/store multiple structures */
622 disas_ldst_multiple_struct(s, insn);
623 break;
624 case 0x0d: /* AdvSIMD load/store single structure */
625 disas_ldst_single_struct(s, insn);
626 break;
627 default:
628 unallocated_encoding(s);
629 break;
630 }
631}
632
Claudio Fontana8ff4c2f2013-12-03 15:12:19 +0000633/* C3.4.6 PC-rel. addressing */
634
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000635static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
636{
Claudio Fontana8ff4c2f2013-12-03 15:12:19 +0000637 /*
638 * 31 30 29 28 27 26 25 24 23 5 4 0
639 * op immlo 1 0 0 0 0 immhi Rd
640 */
641 unsigned int page, rd; /* op -> page */
642 uint64_t base;
643 int64_t offset; /* SignExtend(immhi:immlo) -> offset */
644
645 page = insn & (1 << 31) ? 1 : 0;
646 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
647 rd = extract32(insn, 0, 5);
648 base = s->pc - 4;
649
650 if (page) {
651 /* ADRP (page based) */
652 base &= ~0xfff;
653 offset <<= 12; /* apply Zeros */
654 }
655
656 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000657}
658
659/* Add/subtract (immediate) */
660static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
661{
662 unsupported_encoding(s, insn);
663}
664
665/* Logical (immediate) */
666static void disas_logic_imm(DisasContext *s, uint32_t insn)
667{
668 unsupported_encoding(s, insn);
669}
670
671/* Move wide (immediate) */
672static void disas_movw_imm(DisasContext *s, uint32_t insn)
673{
674 unsupported_encoding(s, insn);
675}
676
677/* Bitfield */
678static void disas_bitfield(DisasContext *s, uint32_t insn)
679{
680 unsupported_encoding(s, insn);
681}
682
Claudio Fontana6e7015312013-12-03 15:12:19 +0000683/* C3.4.3 Extract */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000684static void disas_extract(DisasContext *s, uint32_t insn)
685{
Claudio Fontana6e7015312013-12-03 15:12:19 +0000686 /*
687 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
688 * sf [op21] 1 0 0 1 1 1 N o0 Rm imm Rn Rd
689 * [0 0] [0]
690 */
691 unsigned int sf, n, rm, imm, rn, rd, bitsize, op;
692 sf = insn & (1 << 31) ? 1 : 0;
693 n = insn & (1 << 22) ? 1 : 0;
694 rm = extract32(insn, 16, 5);
695 imm = extract32(insn, 10, 6);
696 rn = extract32(insn, 5, 5);
697 rd = extract32(insn, 0, 5);
698 op = insn & (0x3 << 29 | 1 << 21);
699 bitsize = sf ? 64 : 32;
700
701 if (sf != n || op || imm >= bitsize) {
702 unallocated_encoding(s);
703 } else {
704 TCGv_i64 tcg_tmp, tcg_rd;
705 tcg_tmp = tcg_temp_new_i64();
706 tcg_rd = cpu_reg(s, rd);
707
708 read_cpu_reg(s, tcg_tmp, rm, sf);
709 tcg_gen_shri_i64(tcg_rd, tcg_tmp, imm);
710 tcg_gen_shli_i64(tcg_tmp, cpu_reg(s, rn), bitsize - imm);
711 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
712
713 tcg_temp_free_i64(tcg_tmp);
714 if (!sf) {
715 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
716 }
717 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000718}
719
720/* C3.4 Data processing - immediate */
721static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
722{
723 switch (extract32(insn, 23, 6)) {
724 case 0x20: case 0x21: /* PC-rel. addressing */
725 disas_pc_rel_adr(s, insn);
726 break;
727 case 0x22: case 0x23: /* Add/subtract (immediate) */
728 disas_add_sub_imm(s, insn);
729 break;
730 case 0x24: /* Logical (immediate) */
731 disas_logic_imm(s, insn);
732 break;
733 case 0x25: /* Move wide (immediate) */
734 disas_movw_imm(s, insn);
735 break;
736 case 0x26: /* Bitfield */
737 disas_bitfield(s, insn);
738 break;
739 case 0x27: /* Extract */
740 disas_extract(s, insn);
741 break;
742 default:
743 unallocated_encoding(s);
744 break;
745 }
746}
747
Claudio Fontanad41620e2013-12-03 15:12:19 +0000748/* shift a TCGv src by TCGv shift_amount, put result in dst. */
749static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
750 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
751{
752 switch (shift_type) {
753 case A64_SHIFT_TYPE_LSL:
754 tcg_gen_shl_i64(dst, src, shift_amount);
755 break;
756 case A64_SHIFT_TYPE_LSR:
757 tcg_gen_shr_i64(dst, src, shift_amount);
758 break;
759 case A64_SHIFT_TYPE_ASR:
760 if (!sf) {
761 tcg_gen_ext32s_i64(dst, src);
762 }
763 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
764 break;
765 case A64_SHIFT_TYPE_ROR:
766 if (sf) {
767 tcg_gen_rotr_i64(dst, src, shift_amount);
768 } else {
769 TCGv_i32 t0, t1;
770 t0 = tcg_temp_new_i32();
771 t1 = tcg_temp_new_i32();
772 tcg_gen_trunc_i64_i32(t0, src);
773 tcg_gen_trunc_i64_i32(t1, shift_amount);
774 tcg_gen_rotr_i32(t0, t0, t1);
775 tcg_gen_extu_i32_i64(dst, t0);
776 tcg_temp_free_i32(t0);
777 tcg_temp_free_i32(t1);
778 }
779 break;
780 default:
781 assert(FALSE); /* all shift types should be handled */
782 break;
783 }
784
785 if (!sf) { /* zero extend final result */
786 tcg_gen_ext32u_i64(dst, dst);
787 }
788}
789
790/* shift a TCGv src by immediate, put result in dst. */
791static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
792 enum a64_shift_type shift_type, unsigned int shift_i)
793{
794 shift_i = shift_i & (sf ? 63 : 31);
795
796 if (shift_i == 0) {
797 tcg_gen_mov_i64(dst, src);
798 } else {
799 TCGv_i64 shift_const;
800 shift_const = tcg_const_i64(shift_i);
801 shift_reg(dst, src, sf, shift_type, shift_const);
802 tcg_temp_free_i64(shift_const);
803 }
804}
805
806/* C3.5.10 Logical (shifted register) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000807static void disas_logic_reg(DisasContext *s, uint32_t insn)
808{
Claudio Fontanad41620e2013-12-03 15:12:19 +0000809 /*
810 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
811 * sf opc 0 1 0 1 0 shift N Rm imm6 Rn Rd
812 */
813 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
814 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
815 sf = (insn & (1 << 31)) ? 1 : 0;
816 opc = extract32(insn, 29, 2);
817 shift_type = extract32(insn, 22, 2);
818 invert = (insn & (1 << 21)) ? 1 : 0;
819 rm = extract32(insn, 16, 5);
820 shift_amount = extract32(insn, 10, 6);
821 rn = extract32(insn, 5, 5);
822 rd = extract32(insn, 0, 5);
823
824 if (!sf && (shift_amount & (1 << 5))) {
825 unallocated_encoding(s);
826 return;
827 }
828
829 tcg_rm = tcg_temp_new_i64();
830 read_cpu_reg(s, tcg_rm, rm, sf);
831
832 if (shift_amount) {
833 shift_reg_imm(tcg_rm, tcg_rm, sf,
834 shift_type, shift_amount);
835 }
836
837 if (invert) {
838 tcg_gen_not_i64(tcg_rm, tcg_rm);
839 /* we zero extend later on (!sf) */
840 }
841
842 tcg_rd = cpu_reg(s, rd);
843 tcg_rn = cpu_reg(s, rn);
844
845 switch (opc) {
846 case 0: /* AND, BIC */
847 case 3: /* ANDS, BICS */
848 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
849 break;
850 case 1: /* ORR, ORN */
851 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
852 break;
853 case 2: /* EOR, EON */
854 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
855 break;
856 default:
857 assert(FALSE); /* must handle all in switch */
858 break;
859 }
860
861 if (!sf) {
862 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
863 }
864
865 if (opc == 3) {
866 gen_logic_CC(sf, tcg_rd);
867 }
868
869 tcg_temp_free_i64(tcg_rm);
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000870}
871
872/* Add/subtract (extended register) */
873static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
874{
875 unsupported_encoding(s, insn);
876}
877
878/* Add/subtract (shifted register) */
879static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
880{
881 unsupported_encoding(s, insn);
882}
883
884/* Data-processing (3 source) */
885static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
886{
887 unsupported_encoding(s, insn);
888}
889
890/* Add/subtract (with carry) */
891static void disas_adc_sbc(DisasContext *s, uint32_t insn)
892{
893 unsupported_encoding(s, insn);
894}
895
896/* Conditional compare (immediate) */
897static void disas_cc_imm(DisasContext *s, uint32_t insn)
898{
899 unsupported_encoding(s, insn);
900}
901
902/* Conditional compare (register) */
903static void disas_cc_reg(DisasContext *s, uint32_t insn)
904{
905 unsupported_encoding(s, insn);
906}
907
Claudio Fontana926f3f32013-12-03 15:12:19 +0000908/* C3.5.6 Conditional select */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000909static void disas_cond_select(DisasContext *s, uint32_t insn)
910{
Claudio Fontana926f3f32013-12-03 15:12:19 +0000911 /*
912 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 0
913 * sf op S 1 1 0 1 0 1 0 0 Rm cond op2 Rn Rd
914 * [0]
915 * op -> else_inv, op2 -> else_inc
916 */
917 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
918 TCGv_i64 tcg_rd;
919 if (extract32(insn, 21, 9) != 0x0d4 || (insn & (1 << 11))) {
920 unallocated_encoding(s);
921 return;
922 }
923 sf = (insn & (1 << 31)) ? 1 : 0;
924 else_inv = extract32(insn, 30, 1);
925 rm = extract32(insn, 16, 5);
926 cond = extract32(insn, 12, 4);
927 else_inc = extract32(insn, 10, 1);
928 rn = extract32(insn, 5, 5);
929 rd = extract32(insn, 0, 5);
930 tcg_rd = cpu_reg(s, rd);
931
932 if (cond >= 0x0e) { /* condition "always" */
933 read_cpu_reg(s, tcg_rd, rn, sf);
934 } else {
935 int label_nomatch, label_continue;
936 label_nomatch = gen_new_label();
937 label_continue = gen_new_label();
938
939 arm_gen_test_cc(cond ^ 1, label_nomatch);
940 /* match: */
941 read_cpu_reg(s, tcg_rd, rn, sf);
942 tcg_gen_br(label_continue);
943 /* nomatch: */
944 gen_set_label(label_nomatch);
945 read_cpu_reg(s, tcg_rd, rm, sf);
946 if (else_inv) {
947 tcg_gen_not_i64(tcg_rd, tcg_rd);
948 }
949 if (else_inc) {
950 tcg_gen_addi_i64(tcg_rd, tcg_rd, 1);
951 }
952 if (!sf) {
953 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
954 }
955 /* continue: */
956 gen_set_label(label_continue);
957 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000958}
959
Claudio Fontana4d3b1c32013-12-03 15:12:20 +0000960static void handle_clz(DisasContext *s, unsigned int sf,
961 unsigned int rn, unsigned int rd)
962{
963 TCGv_i64 tcg_rd, tcg_rn;
964 tcg_rd = cpu_reg(s, rd);
965 tcg_rn = cpu_reg(s, rn);
966
967 if (sf) {
968 gen_helper_clz64(tcg_rd, tcg_rn);
969 } else {
970 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
971 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
972 gen_helper_clz(tcg_tmp32, tcg_tmp32);
973 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
974 tcg_temp_free_i32(tcg_tmp32);
975 }
976}
977
Claudio Fontana071b11d2013-12-03 15:12:20 +0000978static void handle_rbit(DisasContext *s, unsigned int sf,
979 unsigned int rn, unsigned int rd)
980{
981 TCGv_i64 tcg_rd, tcg_rn;
982 tcg_rd = cpu_reg(s, rd);
983 tcg_rn = cpu_reg(s, rn);
984
985 if (sf) {
986 gen_helper_rbit64(tcg_rd, tcg_rn);
987 } else {
988 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
989 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
990 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
991 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
992 tcg_temp_free_i32(tcg_tmp32);
993 }
994}
995
Claudio Fontana4d3b1c32013-12-03 15:12:20 +0000996/* C3.5.7 Data-processing (1 source) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +0000997static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
998{
Claudio Fontana4d3b1c32013-12-03 15:12:20 +0000999 /*
1000 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
1001 * sf 1 S 1 1 0 1 0 1 1 0 opcode2 opcode Rn Rd
1002 * [0] [0 0 0 0 0]
1003 */
1004 unsigned int sf, opcode, rn, rd;
1005 if (extract32(insn, 16, 15) != 0x5ac0) {
1006 unallocated_encoding(s);
1007 return;
1008 }
1009 sf = insn & (1 << 31) ? 1 : 0;
1010 opcode = extract32(insn, 10, 6);
1011 rn = extract32(insn, 5, 5);
1012 rd = extract32(insn, 0, 5);
1013
1014 switch (opcode) {
1015 case 0: /* RBIT */
Claudio Fontana071b11d2013-12-03 15:12:20 +00001016 handle_rbit(s, sf, rn, rd);
1017 break;
Claudio Fontana4d3b1c32013-12-03 15:12:20 +00001018 case 1: /* REV16 */
1019 case 2: /* REV32 */
1020 case 3: /* REV64 */
1021 unsupported_encoding(s, insn);
1022 break;
1023 case 4: /* CLZ */
1024 handle_clz(s, sf, rn, rd);
1025 break;
1026 case 5: /* CLS */
1027 unsupported_encoding(s, insn);
1028 break;
1029 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001030}
1031
Claudio Fontana11861fc2013-12-03 15:12:20 +00001032static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
1033 unsigned int rm, unsigned int rn, unsigned int rd)
1034{
1035 TCGv_i64 tcg_n, tcg_m, tcg_rd;
1036 tcg_n = tcg_temp_new_i64();
1037 tcg_m = tcg_temp_new_i64();
1038 tcg_rd = cpu_reg(s, rd);
1039
1040 if (!sf && is_signed) {
1041 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
1042 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
1043 } else {
1044 read_cpu_reg(s, tcg_n, rn, sf);
1045 read_cpu_reg(s, tcg_m, rm, sf);
1046 }
1047
1048 if (is_signed) {
1049 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
1050 } else {
1051 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
1052 }
1053
1054 tcg_temp_free_i64(tcg_n);
1055 tcg_temp_free_i64(tcg_m);
1056
1057 if (!sf) { /* zero extend final result */
1058 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1059 }
1060}
1061
Claudio Fontanae03cad52013-12-03 15:12:20 +00001062/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
1063static void handle_shift_reg(DisasContext *s,
1064 enum a64_shift_type shift_type, unsigned int sf,
1065 unsigned int rm, unsigned int rn, unsigned int rd)
1066{
1067 TCGv_i64 tcg_shift = tcg_temp_new_i64();
1068 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
1069 shift_reg(cpu_reg(s, rd), cpu_reg(s, rn), sf, shift_type, tcg_shift);
1070 tcg_temp_free_i64(tcg_shift);
1071}
1072
Claudio Fontana11861fc2013-12-03 15:12:20 +00001073/* C3.5.8 Data-processing (2 source) */
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001074static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
1075{
Claudio Fontana11861fc2013-12-03 15:12:20 +00001076 /*
1077 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
1078 * sf 0 S 1 1 0 1 0 1 1 0 Rm opcode Rn Rd
1079 * [0]
1080 */
1081 unsigned int sf, rm, opcode, rn, rd;
1082 sf = insn & (1 << 31) ? 1 : 0;
1083 rm = extract32(insn, 16, 5);
1084 opcode = extract32(insn, 10, 6);
1085 rn = extract32(insn, 5, 5);
1086 rd = extract32(insn, 0, 5);
1087
1088 if (extract32(insn, 21, 10) != 0x0d6) {
1089 unallocated_encoding(s);
1090 return;
1091 }
1092
1093 switch (opcode) {
1094 case 2: /* UDIV */
1095 handle_div(s, FALSE, sf, rm, rn, rd);
1096 break;
1097 case 3: /* SDIV */
1098 handle_div(s, TRUE, sf, rm, rn, rd);
1099 break;
1100 case 8: /* LSLV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001101 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
1102 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001103 case 9: /* LSRV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001104 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
1105 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001106 case 10: /* ASRV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001107 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
1108 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001109 case 11: /* RORV */
Claudio Fontanae03cad52013-12-03 15:12:20 +00001110 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
1111 break;
Claudio Fontana11861fc2013-12-03 15:12:20 +00001112 case 16:
1113 case 17:
1114 case 18:
1115 case 19:
1116 case 20:
1117 case 21:
1118 case 22:
1119 case 23: /* CRC32 */
1120 unsupported_encoding(s, insn);
1121 break;
1122 default:
1123 unallocated_encoding(s);
1124 break;
1125 }
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001126}
1127
1128/* C3.5 Data processing - register */
1129static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
1130{
1131 switch (extract32(insn, 24, 5)) {
1132 case 0x0a: /* Logical (shifted register) */
1133 disas_logic_reg(s, insn);
1134 break;
1135 case 0x0b: /* Add/subtract */
1136 if (insn & (1 << 21)) { /* (extended register) */
1137 disas_add_sub_ext_reg(s, insn);
1138 } else {
1139 disas_add_sub_reg(s, insn);
1140 }
1141 break;
1142 case 0x1b: /* Data-processing (3 source) */
1143 disas_data_proc_3src(s, insn);
1144 break;
1145 case 0x1a:
1146 switch (extract32(insn, 21, 3)) {
1147 case 0x0: /* Add/subtract (with carry) */
1148 disas_adc_sbc(s, insn);
1149 break;
1150 case 0x2: /* Conditional compare */
1151 if (insn & (1 << 11)) { /* (immediate) */
1152 disas_cc_imm(s, insn);
1153 } else { /* (register) */
1154 disas_cc_reg(s, insn);
1155 }
1156 break;
1157 case 0x4: /* Conditional select */
1158 disas_cond_select(s, insn);
1159 break;
1160 case 0x6: /* Data-processing */
1161 if (insn & (1 << 30)) { /* (1 source) */
1162 disas_data_proc_1src(s, insn);
1163 } else { /* (2 source) */
1164 disas_data_proc_2src(s, insn);
1165 }
1166 break;
1167 default:
1168 unallocated_encoding(s);
1169 break;
1170 }
1171 break;
1172 default:
1173 unallocated_encoding(s);
1174 break;
1175 }
1176}
1177
1178/* C3.6 Data processing - SIMD and floating point */
1179static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
1180{
1181 unsupported_encoding(s, insn);
1182}
1183
1184/* C3.1 A64 instruction index by encoding */
Peter Maydell089a8d92013-12-03 15:26:18 +00001185static void disas_a64_insn(CPUARMState *env, DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +01001186{
1187 uint32_t insn;
1188
1189 insn = arm_ldl_code(env, s->pc, s->bswap_code);
1190 s->insn = insn;
1191 s->pc += 4;
1192
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001193 switch (extract32(insn, 25, 4)) {
1194 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
Alexander Graf14ade102013-09-03 20:12:10 +01001195 unallocated_encoding(s);
1196 break;
Claudio Fontanaea5ca532013-12-03 15:12:18 +00001197 case 0x8: case 0x9: /* Data processing - immediate */
1198 disas_data_proc_imm(s, insn);
1199 break;
1200 case 0xa: case 0xb: /* Branch, exception generation and system insns */
1201 disas_b_exc_sys(s, insn);
1202 break;
1203 case 0x4:
1204 case 0x6:
1205 case 0xc:
1206 case 0xe: /* Loads and stores */
1207 disas_ldst(s, insn);
1208 break;
1209 case 0x5:
1210 case 0xd: /* Data processing - register */
1211 disas_data_proc_reg(s, insn);
1212 break;
1213 case 0x7:
1214 case 0xf: /* Data processing - SIMD and floating point */
1215 disas_data_proc_simd_fp(s, insn);
1216 break;
1217 default:
1218 assert(FALSE); /* all 15 cases should be handled above */
1219 break;
Alexander Graf14ade102013-09-03 20:12:10 +01001220 }
Alexander Grafeeed5002013-12-03 15:12:18 +00001221
1222 /* if we allocated any temporaries, free them here */
1223 free_tmp_a64(s);
Peter Maydell089a8d92013-12-03 15:26:18 +00001224}
Alexander Graf14ade102013-09-03 20:12:10 +01001225
Peter Maydell089a8d92013-12-03 15:26:18 +00001226void gen_intermediate_code_internal_a64(ARMCPU *cpu,
1227 TranslationBlock *tb,
1228 bool search_pc)
1229{
1230 CPUState *cs = CPU(cpu);
1231 CPUARMState *env = &cpu->env;
1232 DisasContext dc1, *dc = &dc1;
1233 CPUBreakpoint *bp;
1234 uint16_t *gen_opc_end;
1235 int j, lj;
1236 target_ulong pc_start;
1237 target_ulong next_page_start;
1238 int num_insns;
1239 int max_insns;
1240
1241 pc_start = tb->pc;
1242
1243 dc->tb = tb;
1244
1245 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
1246
1247 dc->is_jmp = DISAS_NEXT;
1248 dc->pc = pc_start;
1249 dc->singlestep_enabled = cs->singlestep_enabled;
1250 dc->condjmp = 0;
1251
1252 dc->aarch64 = 1;
Alexander Grafeeed5002013-12-03 15:12:18 +00001253 dc->tmp_a64_count = 0;
Peter Maydell089a8d92013-12-03 15:26:18 +00001254 dc->thumb = 0;
1255 dc->bswap_code = 0;
1256 dc->condexec_mask = 0;
1257 dc->condexec_cond = 0;
1258#if !defined(CONFIG_USER_ONLY)
1259 dc->user = 0;
1260#endif
1261 dc->vfp_enabled = 0;
1262 dc->vec_len = 0;
1263 dc->vec_stride = 0;
1264
1265 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1266 lj = -1;
1267 num_insns = 0;
1268 max_insns = tb->cflags & CF_COUNT_MASK;
1269 if (max_insns == 0) {
1270 max_insns = CF_COUNT_MASK;
1271 }
1272
1273 gen_tb_start();
1274
1275 tcg_clear_temp_count();
1276
1277 do {
1278 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
1279 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1280 if (bp->pc == dc->pc) {
1281 gen_exception_insn(dc, 0, EXCP_DEBUG);
1282 /* Advance PC so that clearing the breakpoint will
1283 invalidate this TB. */
1284 dc->pc += 2;
1285 goto done_generating;
1286 }
1287 }
1288 }
1289
1290 if (search_pc) {
1291 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1292 if (lj < j) {
1293 lj++;
1294 while (lj < j) {
1295 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1296 }
1297 }
1298 tcg_ctx.gen_opc_pc[lj] = dc->pc;
1299 tcg_ctx.gen_opc_instr_start[lj] = 1;
1300 tcg_ctx.gen_opc_icount[lj] = num_insns;
1301 }
1302
1303 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
1304 gen_io_start();
1305 }
1306
1307 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
1308 tcg_gen_debug_insn_start(dc->pc);
1309 }
1310
1311 disas_a64_insn(env, dc);
1312
1313 if (tcg_check_temp_count()) {
1314 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
1315 dc->pc);
1316 }
1317
1318 /* Translation stops when a conditional branch is encountered.
1319 * Otherwise the subsequent code could get translated several times.
1320 * Also stop translation when a page boundary is reached. This
1321 * ensures prefetch aborts occur at the right place.
1322 */
1323 num_insns++;
1324 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
1325 !cs->singlestep_enabled &&
1326 !singlestep &&
1327 dc->pc < next_page_start &&
1328 num_insns < max_insns);
1329
1330 if (tb->cflags & CF_LAST_IO) {
1331 gen_io_end();
1332 }
1333
1334 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
1335 /* Note that this means single stepping WFI doesn't halt the CPU.
1336 * For conditional branch insns this is harmless unreachable code as
1337 * gen_goto_tb() has already handled emitting the debug exception
1338 * (and thus a tb-jump is not possible when singlestepping).
1339 */
1340 assert(dc->is_jmp != DISAS_TB_JUMP);
1341 if (dc->is_jmp != DISAS_JUMP) {
1342 gen_a64_set_pc_im(dc->pc);
1343 }
1344 gen_exception(EXCP_DEBUG);
1345 } else {
1346 switch (dc->is_jmp) {
1347 case DISAS_NEXT:
1348 gen_goto_tb(dc, 1, dc->pc);
1349 break;
1350 default:
1351 case DISAS_JUMP:
1352 case DISAS_UPDATE:
1353 /* indicate that the hash table must be used to find the next TB */
1354 tcg_gen_exit_tb(0);
1355 break;
1356 case DISAS_TB_JUMP:
1357 case DISAS_EXC:
1358 case DISAS_SWI:
1359 break;
1360 case DISAS_WFI:
1361 /* This is a special case because we don't want to just halt the CPU
1362 * if trying to debug across a WFI.
1363 */
1364 gen_helper_wfi(cpu_env);
1365 break;
1366 }
1367 }
1368
1369done_generating:
1370 gen_tb_end(tb, num_insns);
1371 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
1372
1373#ifdef DEBUG_DISAS
1374 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1375 qemu_log("----------------\n");
1376 qemu_log("IN: %s\n", lookup_symbol(pc_start));
1377 log_target_disas(env, pc_start, dc->pc - pc_start,
1378 dc->thumb | (dc->bswap_code << 1));
1379 qemu_log("\n");
1380 }
1381#endif
1382 if (search_pc) {
1383 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1384 lj++;
1385 while (lj <= j) {
1386 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1387 }
1388 } else {
1389 tb->size = dc->pc - pc_start;
1390 tb->icount = num_insns;
Alexander Graf14ade102013-09-03 20:12:10 +01001391 }
1392}