Statistics
| Branch: | Revision:

root / target-arm / translate-a64.c @ 4a08d475

History | View | Annotate | Download (59.2 kB)

1
/*
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

    
31
#include "exec/gen-icount.h"
32

    
33
#include "helper.h"
34
#define GEN_HELPER 1
35
#include "helper.h"
36

    
37
static TCGv_i64 cpu_X[32];
38
static TCGv_i64 cpu_pc;
39
static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
40

    
41
static 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

    
48
enum 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

    
55
/* initialize TCG globals.  */
56
void 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

    
69
    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");
73
}
74

    
75
void 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;
80
    uint32_t psr = pstate_read(env);
81
    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
    }
93
    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' : '-');
99
    cpu_fprintf(f, "\n");
100
}
101

    
102
static int get_mem_index(DisasContext *s)
103
{
104
#ifdef CONFIG_USER_ONLY
105
    return 1;
106
#else
107
    return s->user;
108
#endif
109
}
110

    
111
void gen_a64_set_pc_im(uint64_t val)
112
{
113
    tcg_gen_movi_i64(cpu_pc, val);
114
}
115

    
116
static void gen_exception(int excp)
117
{
118
    TCGv_i32 tmp = tcg_temp_new_i32();
119
    tcg_gen_movi_i32(tmp, excp);
120
    gen_helper_exception(cpu_env, tmp);
121
    tcg_temp_free_i32(tmp);
122
}
123

    
124
static void gen_exception_insn(DisasContext *s, int offset, int excp)
125
{
126
    gen_a64_set_pc_im(s->pc - offset);
127
    gen_exception(excp);
128
    s->is_jmp = DISAS_EXC;
129
}
130

    
131
static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
132
{
133
    /* No direct tb linking with singlestep or deterministic io */
134
    if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
135
        return false;
136
    }
137

    
138
    /* Only link tbs from inside the same guest page */
139
    if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
140
        return false;
141
    }
142

    
143
    return true;
144
}
145

    
146
static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
147
{
148
    TranslationBlock *tb;
149

    
150
    tb = s->tb;
151
    if (use_goto_tb(s, n, dest)) {
152
        tcg_gen_goto_tb(n);
153
        gen_a64_set_pc_im(dest);
154
        tcg_gen_exit_tb((tcg_target_long)tb + n);
155
        s->is_jmp = DISAS_TB_JUMP;
156
    } else {
157
        gen_a64_set_pc_im(dest);
158
        if (s->singlestep_enabled) {
159
            gen_exception(EXCP_DEBUG);
160
        }
161
        tcg_gen_exit_tb(0);
162
        s->is_jmp = DISAS_JUMP;
163
    }
164
}
165

    
166
static void unallocated_encoding(DisasContext *s)
167
{
168
    gen_exception_insn(s, 4, EXCP_UDEF);
169
}
170

    
171
#define unsupported_encoding(s, insn)                                    \
172
    do {                                                                 \
173
        qemu_log_mask(LOG_UNIMP,                                         \
174
                      "%s:%d: unsupported instruction encoding 0x%08x "  \
175
                      "at pc=%016" PRIx64 "\n",                          \
176
                      __FILE__, __LINE__, insn, s->pc - 4);              \
177
        unallocated_encoding(s);                                         \
178
    } while (0);
179

    
180
static void init_tmp_a64_array(DisasContext *s)
181
{
182
#ifdef CONFIG_DEBUG_TCG
183
    int i;
184
    for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
185
        TCGV_UNUSED_I64(s->tmp_a64[i]);
186
    }
187
#endif
188
    s->tmp_a64_count = 0;
189
}
190

    
191
static void free_tmp_a64(DisasContext *s)
192
{
193
    int i;
194
    for (i = 0; i < s->tmp_a64_count; i++) {
195
        tcg_temp_free_i64(s->tmp_a64[i]);
196
    }
197
    init_tmp_a64_array(s);
198
}
199

    
200
static TCGv_i64 new_tmp_a64(DisasContext *s)
201
{
202
    assert(s->tmp_a64_count < TMP_A64_MAX);
203
    return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
204
}
205

    
206
static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
207
{
208
    TCGv_i64 t = new_tmp_a64(s);
209
    tcg_gen_movi_i64(t, 0);
210
    return t;
211
}
212

    
213
/*
214
 * Register access functions
215
 *
216
 * These functions are used for directly accessing a register in where
217
 * changes to the final register value are likely to be made. If you
218
 * need to use a register for temporary calculation (e.g. index type
219
 * operations) use the read_* form.
220
 *
221
 * B1.2.1 Register mappings
222
 *
223
 * In instruction register encoding 31 can refer to ZR (zero register) or
224
 * the SP (stack pointer) depending on context. In QEMU's case we map SP
225
 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
226
 * This is the point of the _sp forms.
227
 */
228
static TCGv_i64 cpu_reg(DisasContext *s, int reg)
229
{
230
    if (reg == 31) {
231
        return new_tmp_a64_zero(s);
232
    } else {
233
        return cpu_X[reg];
234
    }
235
}
236

    
237
/* register access for when 31 == SP */
238
static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
239
{
240
    return cpu_X[reg];
241
}
242

    
243
/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
244
 * representing the register contents. This TCGv is an auto-freed
245
 * temporary so it need not be explicitly freed, and may be modified.
246
 */
247
static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
248
{
249
    TCGv_i64 v = new_tmp_a64(s);
250
    if (reg != 31) {
251
        if (sf) {
252
            tcg_gen_mov_i64(v, cpu_X[reg]);
253
        } else {
254
            tcg_gen_ext32u_i64(v, cpu_X[reg]);
255
        }
256
    } else {
257
        tcg_gen_movi_i64(v, 0);
258
    }
259
    return v;
260
}
261

    
262
static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
263
{
264
    TCGv_i64 v = new_tmp_a64(s);
265
    if (sf) {
266
        tcg_gen_mov_i64(v, cpu_X[reg]);
267
    } else {
268
        tcg_gen_ext32u_i64(v, cpu_X[reg]);
269
    }
270
    return v;
271
}
272

    
273
/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
274
 * than the 32 bit equivalent.
275
 */
276
static inline void gen_set_NZ64(TCGv_i64 result)
277
{
278
    TCGv_i64 flag = tcg_temp_new_i64();
279

    
280
    tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
281
    tcg_gen_trunc_i64_i32(cpu_ZF, flag);
282
    tcg_gen_shri_i64(flag, result, 32);
283
    tcg_gen_trunc_i64_i32(cpu_NF, flag);
284
    tcg_temp_free_i64(flag);
285
}
286

    
287
/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
288
static inline void gen_logic_CC(int sf, TCGv_i64 result)
289
{
290
    if (sf) {
291
        gen_set_NZ64(result);
292
    } else {
293
        tcg_gen_trunc_i64_i32(cpu_ZF, result);
294
        tcg_gen_trunc_i64_i32(cpu_NF, result);
295
    }
296
    tcg_gen_movi_i32(cpu_CF, 0);
297
    tcg_gen_movi_i32(cpu_VF, 0);
298
}
299

    
300
/*
301
 * Load/Store generators
302
 */
303

    
304
/*
305
 * Store from GPR register to memory
306
 */
307
static void do_gpr_st(DisasContext *s, TCGv_i64 source,
308
                      TCGv_i64 tcg_addr, int size)
309
{
310
    g_assert(size <= 3);
311
    tcg_gen_qemu_st_i64(source, tcg_addr, get_mem_index(s), MO_TE + size);
312
}
313

    
314
/*
315
 * Load from memory to GPR register
316
 */
317
static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
318
                      int size, bool is_signed, bool extend)
319
{
320
    TCGMemOp memop = MO_TE + size;
321

    
322
    g_assert(size <= 3);
323

    
324
    if (is_signed) {
325
        memop += MO_SIGN;
326
    }
327

    
328
    tcg_gen_qemu_ld_i64(dest, tcg_addr, get_mem_index(s), memop);
329

    
330
    if (extend && is_signed) {
331
        g_assert(size < 3);
332
        tcg_gen_ext32u_i64(dest, dest);
333
    }
334
}
335

    
336
/*
337
 * Store from FP register to memory
338
 */
339
static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
340
{
341
    /* This writes the bottom N bits of a 128 bit wide vector to memory */
342
    int freg_offs = offsetof(CPUARMState, vfp.regs[srcidx * 2]);
343
    TCGv_i64 tmp = tcg_temp_new_i64();
344

    
345
    if (size < 4) {
346
        switch (size) {
347
        case 0:
348
            tcg_gen_ld8u_i64(tmp, cpu_env, freg_offs);
349
            break;
350
        case 1:
351
            tcg_gen_ld16u_i64(tmp, cpu_env, freg_offs);
352
            break;
353
        case 2:
354
            tcg_gen_ld32u_i64(tmp, cpu_env, freg_offs);
355
            break;
356
        case 3:
357
            tcg_gen_ld_i64(tmp, cpu_env, freg_offs);
358
            break;
359
        }
360
        tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
361
    } else {
362
        TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
363
        tcg_gen_ld_i64(tmp, cpu_env, freg_offs);
364
        tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
365
        tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
366
        tcg_gen_ld_i64(tmp, cpu_env, freg_offs + sizeof(float64));
367
        tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
368
        tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
369
        tcg_temp_free_i64(tcg_hiaddr);
370
    }
371

    
372
    tcg_temp_free_i64(tmp);
373
}
374

    
375
/*
376
 * Load from memory to FP register
377
 */
378
static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
379
{
380
    /* This always zero-extends and writes to a full 128 bit wide vector */
381
    int freg_offs = offsetof(CPUARMState, vfp.regs[destidx * 2]);
382
    TCGv_i64 tmplo = tcg_temp_new_i64();
383
    TCGv_i64 tmphi;
384

    
385
    if (size < 4) {
386
        TCGMemOp memop = MO_TE + size;
387
        tmphi = tcg_const_i64(0);
388
        tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
389
    } else {
390
        TCGv_i64 tcg_hiaddr;
391
        tmphi = tcg_temp_new_i64();
392
        tcg_hiaddr = tcg_temp_new_i64();
393

    
394
        tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
395
        tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
396
        tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
397
        tcg_temp_free_i64(tcg_hiaddr);
398
    }
399

    
400
    tcg_gen_st_i64(tmplo, cpu_env, freg_offs);
401
    tcg_gen_st_i64(tmphi, cpu_env, freg_offs + sizeof(float64));
402

    
403
    tcg_temp_free_i64(tmplo);
404
    tcg_temp_free_i64(tmphi);
405
}
406

    
407
static inline void gen_check_sp_alignment(DisasContext *s)
408
{
409
    /* The AArch64 architecture mandates that (if enabled via PSTATE
410
     * or SCTLR bits) there is a check that SP is 16-aligned on every
411
     * SP-relative load or store (with an exception generated if it is not).
412
     * In line with general QEMU practice regarding misaligned accesses,
413
     * we omit these checks for the sake of guest program performance.
414
     * This function is provided as a hook so we can more easily add these
415
     * checks in future (possibly as a "favour catching guest program bugs
416
     * over speed" user selectable option).
417
     */
418
}
419

    
420
/*
421
 * the instruction disassembly implemented here matches
422
 * the instruction encoding classifications in chapter 3 (C3)
423
 * of the ARM Architecture Reference Manual (DDI0487A_a)
424
 */
425

    
426
/* C3.2.7 Unconditional branch (immediate)
427
 *   31  30       26 25                                  0
428
 * +----+-----------+-------------------------------------+
429
 * | op | 0 0 1 0 1 |                 imm26               |
430
 * +----+-----------+-------------------------------------+
431
 */
432
static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
433
{
434
    uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
435

    
436
    if (insn & (1 << 31)) {
437
        /* C5.6.26 BL Branch with link */
438
        tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
439
    }
440

    
441
    /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
442
    gen_goto_tb(s, 0, addr);
443
}
444

    
445
/* C3.2.1 Compare & branch (immediate)
446
 *   31  30         25  24  23                  5 4      0
447
 * +----+-------------+----+---------------------+--------+
448
 * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
449
 * +----+-------------+----+---------------------+--------+
450
 */
451
static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
452
{
453
    unsigned int sf, op, rt;
454
    uint64_t addr;
455
    int label_match;
456
    TCGv_i64 tcg_cmp;
457

    
458
    sf = extract32(insn, 31, 1);
459
    op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
460
    rt = extract32(insn, 0, 5);
461
    addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
462

    
463
    tcg_cmp = read_cpu_reg(s, rt, sf);
464
    label_match = gen_new_label();
465

    
466
    tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
467
                        tcg_cmp, 0, label_match);
468

    
469
    gen_goto_tb(s, 0, s->pc);
470
    gen_set_label(label_match);
471
    gen_goto_tb(s, 1, addr);
472
}
473

    
474
/* C3.2.5 Test & branch (immediate)
475
 *   31  30         25  24  23   19 18          5 4    0
476
 * +----+-------------+----+-------+-------------+------+
477
 * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
478
 * +----+-------------+----+-------+-------------+------+
479
 */
480
static void disas_test_b_imm(DisasContext *s, uint32_t insn)
481
{
482
    unsigned int bit_pos, op, rt;
483
    uint64_t addr;
484
    int label_match;
485
    TCGv_i64 tcg_cmp;
486

    
487
    bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
488
    op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
489
    addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
490
    rt = extract32(insn, 0, 5);
491

    
492
    tcg_cmp = tcg_temp_new_i64();
493
    tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
494
    label_match = gen_new_label();
495
    tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
496
                        tcg_cmp, 0, label_match);
497
    tcg_temp_free_i64(tcg_cmp);
498
    gen_goto_tb(s, 0, s->pc);
499
    gen_set_label(label_match);
500
    gen_goto_tb(s, 1, addr);
501
}
502

    
503
/* C3.2.2 / C5.6.19 Conditional branch (immediate)
504
 *  31           25  24  23                  5   4  3    0
505
 * +---------------+----+---------------------+----+------+
506
 * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
507
 * +---------------+----+---------------------+----+------+
508
 */
509
static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
510
{
511
    unsigned int cond;
512
    uint64_t addr;
513

    
514
    if ((insn & (1 << 4)) || (insn & (1 << 24))) {
515
        unallocated_encoding(s);
516
        return;
517
    }
518
    addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
519
    cond = extract32(insn, 0, 4);
520

    
521
    if (cond < 0x0e) {
522
        /* genuinely conditional branches */
523
        int label_match = gen_new_label();
524
        arm_gen_test_cc(cond, label_match);
525
        gen_goto_tb(s, 0, s->pc);
526
        gen_set_label(label_match);
527
        gen_goto_tb(s, 1, addr);
528
    } else {
529
        /* 0xe and 0xf are both "always" conditions */
530
        gen_goto_tb(s, 0, addr);
531
    }
532
}
533

    
534
/* C5.6.68 HINT */
535
static void handle_hint(DisasContext *s, uint32_t insn,
536
                        unsigned int op1, unsigned int op2, unsigned int crm)
537
{
538
    unsigned int selector = crm << 3 | op2;
539

    
540
    if (op1 != 3) {
541
        unallocated_encoding(s);
542
        return;
543
    }
544

    
545
    switch (selector) {
546
    case 0: /* NOP */
547
        return;
548
    case 1: /* YIELD */
549
    case 2: /* WFE */
550
    case 3: /* WFI */
551
    case 4: /* SEV */
552
    case 5: /* SEVL */
553
        /* we treat all as NOP at least for now */
554
        return;
555
    default:
556
        /* default specified as NOP equivalent */
557
        return;
558
    }
559
}
560

    
561
/* CLREX, DSB, DMB, ISB */
562
static void handle_sync(DisasContext *s, uint32_t insn,
563
                        unsigned int op1, unsigned int op2, unsigned int crm)
564
{
565
    if (op1 != 3) {
566
        unallocated_encoding(s);
567
        return;
568
    }
569

    
570
    switch (op2) {
571
    case 2: /* CLREX */
572
        unsupported_encoding(s, insn);
573
        return;
574
    case 4: /* DSB */
575
    case 5: /* DMB */
576
    case 6: /* ISB */
577
        /* We don't emulate caches so barriers are no-ops */
578
        return;
579
    default:
580
        unallocated_encoding(s);
581
        return;
582
    }
583
}
584

    
585
/* C5.6.130 MSR (immediate) - move immediate to processor state field */
586
static void handle_msr_i(DisasContext *s, uint32_t insn,
587
                         unsigned int op1, unsigned int op2, unsigned int crm)
588
{
589
    unsupported_encoding(s, insn);
590
}
591

    
592
/* C5.6.204 SYS */
593
static void handle_sys(DisasContext *s, uint32_t insn, unsigned int l,
594
                       unsigned int op1, unsigned int op2,
595
                       unsigned int crn, unsigned int crm, unsigned int rt)
596
{
597
    unsupported_encoding(s, insn);
598
}
599

    
600
/* C5.6.129 MRS - move from system register */
601
static void handle_mrs(DisasContext *s, uint32_t insn, unsigned int op0,
602
                       unsigned int op1, unsigned int op2,
603
                       unsigned int crn, unsigned int crm, unsigned int rt)
604
{
605
    unsupported_encoding(s, insn);
606
}
607

    
608
/* C5.6.131 MSR (register) - move to system register */
609
static void handle_msr(DisasContext *s, uint32_t insn, unsigned int op0,
610
                       unsigned int op1, unsigned int op2,
611
                       unsigned int crn, unsigned int crm, unsigned int rt)
612
{
613
    unsupported_encoding(s, insn);
614
}
615

    
616
/* C3.2.4 System
617
 *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
618
 * +---------------------+---+-----+-----+-------+-------+-----+------+
619
 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
620
 * +---------------------+---+-----+-----+-------+-------+-----+------+
621
 */
622
static void disas_system(DisasContext *s, uint32_t insn)
623
{
624
    unsigned int l, op0, op1, crn, crm, op2, rt;
625
    l = extract32(insn, 21, 1);
626
    op0 = extract32(insn, 19, 2);
627
    op1 = extract32(insn, 16, 3);
628
    crn = extract32(insn, 12, 4);
629
    crm = extract32(insn, 8, 4);
630
    op2 = extract32(insn, 5, 3);
631
    rt = extract32(insn, 0, 5);
632

    
633
    if (op0 == 0) {
634
        if (l || rt != 31) {
635
            unallocated_encoding(s);
636
            return;
637
        }
638
        switch (crn) {
639
        case 2: /* C5.6.68 HINT */
640
            handle_hint(s, insn, op1, op2, crm);
641
            break;
642
        case 3: /* CLREX, DSB, DMB, ISB */
643
            handle_sync(s, insn, op1, op2, crm);
644
            break;
645
        case 4: /* C5.6.130 MSR (immediate) */
646
            handle_msr_i(s, insn, op1, op2, crm);
647
            break;
648
        default:
649
            unallocated_encoding(s);
650
            break;
651
        }
652
        return;
653
    }
654

    
655
    if (op0 == 1) {
656
        /* C5.6.204 SYS */
657
        handle_sys(s, insn, l, op1, op2, crn, crm, rt);
658
    } else if (l) { /* op0 > 1 */
659
        /* C5.6.129 MRS - move from system register */
660
        handle_mrs(s, insn, op0, op1, op2, crn, crm, rt);
661
    } else {
662
        /* C5.6.131 MSR (register) - move to system register */
663
        handle_msr(s, insn, op0, op1, op2, crn, crm, rt);
664
    }
665
}
666

    
667
/* Exception generation */
668
static void disas_exc(DisasContext *s, uint32_t insn)
669
{
670
    unsupported_encoding(s, insn);
671
}
672

    
673
/* C3.2.7 Unconditional branch (register)
674
 *  31           25 24   21 20   16 15   10 9    5 4     0
675
 * +---------------+-------+-------+-------+------+-------+
676
 * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
677
 * +---------------+-------+-------+-------+------+-------+
678
 */
679
static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
680
{
681
    unsigned int opc, op2, op3, rn, op4;
682

    
683
    opc = extract32(insn, 21, 4);
684
    op2 = extract32(insn, 16, 5);
685
    op3 = extract32(insn, 10, 6);
686
    rn = extract32(insn, 5, 5);
687
    op4 = extract32(insn, 0, 5);
688

    
689
    if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
690
        unallocated_encoding(s);
691
        return;
692
    }
693

    
694
    switch (opc) {
695
    case 0: /* BR */
696
    case 2: /* RET */
697
        break;
698
    case 1: /* BLR */
699
        tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
700
        break;
701
    case 4: /* ERET */
702
    case 5: /* DRPS */
703
        if (rn != 0x1f) {
704
            unallocated_encoding(s);
705
        } else {
706
            unsupported_encoding(s, insn);
707
        }
708
        return;
709
    default:
710
        unallocated_encoding(s);
711
        return;
712
    }
713

    
714
    tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
715
    s->is_jmp = DISAS_JUMP;
716
}
717

    
718
/* C3.2 Branches, exception generating and system instructions */
719
static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
720
{
721
    switch (extract32(insn, 25, 7)) {
722
    case 0x0a: case 0x0b:
723
    case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
724
        disas_uncond_b_imm(s, insn);
725
        break;
726
    case 0x1a: case 0x5a: /* Compare & branch (immediate) */
727
        disas_comp_b_imm(s, insn);
728
        break;
729
    case 0x1b: case 0x5b: /* Test & branch (immediate) */
730
        disas_test_b_imm(s, insn);
731
        break;
732
    case 0x2a: /* Conditional branch (immediate) */
733
        disas_cond_b_imm(s, insn);
734
        break;
735
    case 0x6a: /* Exception generation / System */
736
        if (insn & (1 << 24)) {
737
            disas_system(s, insn);
738
        } else {
739
            disas_exc(s, insn);
740
        }
741
        break;
742
    case 0x6b: /* Unconditional branch (register) */
743
        disas_uncond_b_reg(s, insn);
744
        break;
745
    default:
746
        unallocated_encoding(s);
747
        break;
748
    }
749
}
750

    
751
/* Load/store exclusive */
752
static void disas_ldst_excl(DisasContext *s, uint32_t insn)
753
{
754
    unsupported_encoding(s, insn);
755
}
756

    
757
/* Load register (literal) */
758
static void disas_ld_lit(DisasContext *s, uint32_t insn)
759
{
760
    unsupported_encoding(s, insn);
761
}
762

    
763
/*
764
 * C5.6.80 LDNP (Load Pair - non-temporal hint)
765
 * C5.6.81 LDP (Load Pair - non vector)
766
 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
767
 * C5.6.176 STNP (Store Pair - non-temporal hint)
768
 * C5.6.177 STP (Store Pair - non vector)
769
 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
770
 * C6.3.165 LDP (Load Pair of SIMD&FP)
771
 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
772
 * C6.3.284 STP (Store Pair of SIMD&FP)
773
 *
774
 *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
775
 * +-----+-------+---+---+-------+---+-----------------------------+
776
 * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
777
 * +-----+-------+---+---+-------+---+-------+-------+------+------+
778
 *
779
 * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
780
 *      LDPSW                    01
781
 *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
782
 *   V: 0 -> GPR, 1 -> Vector
783
 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
784
 *      10 -> signed offset, 11 -> pre-index
785
 *   L: 0 -> Store 1 -> Load
786
 *
787
 * Rt, Rt2 = GPR or SIMD registers to be stored
788
 * Rn = general purpose register containing address
789
 * imm7 = signed offset (multiple of 4 or 8 depending on size)
790
 */
791
static void disas_ldst_pair(DisasContext *s, uint32_t insn)
792
{
793
    int rt = extract32(insn, 0, 5);
794
    int rn = extract32(insn, 5, 5);
795
    int rt2 = extract32(insn, 10, 5);
796
    int64_t offset = sextract32(insn, 15, 7);
797
    int index = extract32(insn, 23, 2);
798
    bool is_vector = extract32(insn, 26, 1);
799
    bool is_load = extract32(insn, 22, 1);
800
    int opc = extract32(insn, 30, 2);
801

    
802
    bool is_signed = false;
803
    bool postindex = false;
804
    bool wback = false;
805

    
806
    TCGv_i64 tcg_addr; /* calculated address */
807
    int size;
808

    
809
    if (opc == 3) {
810
        unallocated_encoding(s);
811
        return;
812
    }
813

    
814
    if (is_vector) {
815
        size = 2 + opc;
816
    } else {
817
        size = 2 + extract32(opc, 1, 1);
818
        is_signed = extract32(opc, 0, 1);
819
        if (!is_load && is_signed) {
820
            unallocated_encoding(s);
821
            return;
822
        }
823
    }
824

    
825
    switch (index) {
826
    case 1: /* post-index */
827
        postindex = true;
828
        wback = true;
829
        break;
830
    case 0:
831
        /* signed offset with "non-temporal" hint. Since we don't emulate
832
         * caches we don't care about hints to the cache system about
833
         * data access patterns, and handle this identically to plain
834
         * signed offset.
835
         */
836
        if (is_signed) {
837
            /* There is no non-temporal-hint version of LDPSW */
838
            unallocated_encoding(s);
839
            return;
840
        }
841
        postindex = false;
842
        break;
843
    case 2: /* signed offset, rn not updated */
844
        postindex = false;
845
        break;
846
    case 3: /* pre-index */
847
        postindex = false;
848
        wback = true;
849
        break;
850
    }
851

    
852
    offset <<= size;
853

    
854
    if (rn == 31) {
855
        gen_check_sp_alignment(s);
856
    }
857

    
858
    tcg_addr = read_cpu_reg_sp(s, rn, 1);
859

    
860
    if (!postindex) {
861
        tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
862
    }
863

    
864
    if (is_vector) {
865
        if (is_load) {
866
            do_fp_ld(s, rt, tcg_addr, size);
867
        } else {
868
            do_fp_st(s, rt, tcg_addr, size);
869
        }
870
    } else {
871
        TCGv_i64 tcg_rt = cpu_reg(s, rt);
872
        if (is_load) {
873
            do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
874
        } else {
875
            do_gpr_st(s, tcg_rt, tcg_addr, size);
876
        }
877
    }
878
    tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
879
    if (is_vector) {
880
        if (is_load) {
881
            do_fp_ld(s, rt2, tcg_addr, size);
882
        } else {
883
            do_fp_st(s, rt2, tcg_addr, size);
884
        }
885
    } else {
886
        TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
887
        if (is_load) {
888
            do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
889
        } else {
890
            do_gpr_st(s, tcg_rt2, tcg_addr, size);
891
        }
892
    }
893

    
894
    if (wback) {
895
        if (postindex) {
896
            tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
897
        } else {
898
            tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
899
        }
900
        tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
901
    }
902
}
903

    
904
/* Load/store register (all forms) */
905
static void disas_ldst_reg(DisasContext *s, uint32_t insn)
906
{
907
    unsupported_encoding(s, insn);
908
}
909

    
910
/* AdvSIMD load/store multiple structures */
911
static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
912
{
913
    unsupported_encoding(s, insn);
914
}
915

    
916
/* AdvSIMD load/store single structure */
917
static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
918
{
919
    unsupported_encoding(s, insn);
920
}
921

    
922
/* C3.3 Loads and stores */
923
static void disas_ldst(DisasContext *s, uint32_t insn)
924
{
925
    switch (extract32(insn, 24, 6)) {
926
    case 0x08: /* Load/store exclusive */
927
        disas_ldst_excl(s, insn);
928
        break;
929
    case 0x18: case 0x1c: /* Load register (literal) */
930
        disas_ld_lit(s, insn);
931
        break;
932
    case 0x28: case 0x29:
933
    case 0x2c: case 0x2d: /* Load/store pair (all forms) */
934
        disas_ldst_pair(s, insn);
935
        break;
936
    case 0x38: case 0x39:
937
    case 0x3c: case 0x3d: /* Load/store register (all forms) */
938
        disas_ldst_reg(s, insn);
939
        break;
940
    case 0x0c: /* AdvSIMD load/store multiple structures */
941
        disas_ldst_multiple_struct(s, insn);
942
        break;
943
    case 0x0d: /* AdvSIMD load/store single structure */
944
        disas_ldst_single_struct(s, insn);
945
        break;
946
    default:
947
        unallocated_encoding(s);
948
        break;
949
    }
950
}
951

    
952
/* C3.4.6 PC-rel. addressing
953
 *   31  30   29 28       24 23                5 4    0
954
 * +----+-------+-----------+-------------------+------+
955
 * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
956
 * +----+-------+-----------+-------------------+------+
957
 */
958
static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
959
{
960
    unsigned int page, rd;
961
    uint64_t base;
962
    int64_t offset;
963

    
964
    page = extract32(insn, 31, 1);
965
    /* SignExtend(immhi:immlo) -> offset */
966
    offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
967
    rd = extract32(insn, 0, 5);
968
    base = s->pc - 4;
969

    
970
    if (page) {
971
        /* ADRP (page based) */
972
        base &= ~0xfff;
973
        offset <<= 12;
974
    }
975

    
976
    tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
977
}
978

    
979
/* Add/subtract (immediate) */
980
static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
981
{
982
    unsupported_encoding(s, insn);
983
}
984

    
985
/* The input should be a value in the bottom e bits (with higher
986
 * bits zero); returns that value replicated into every element
987
 * of size e in a 64 bit integer.
988
 */
989
static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
990
{
991
    assert(e != 0);
992
    while (e < 64) {
993
        mask |= mask << e;
994
        e *= 2;
995
    }
996
    return mask;
997
}
998

    
999
/* Return a value with the bottom len bits set (where 0 < len <= 64) */
1000
static inline uint64_t bitmask64(unsigned int length)
1001
{
1002
    assert(length > 0 && length <= 64);
1003
    return ~0ULL >> (64 - length);
1004
}
1005

    
1006
/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
1007
 * only require the wmask. Returns false if the imms/immr/immn are a reserved
1008
 * value (ie should cause a guest UNDEF exception), and true if they are
1009
 * valid, in which case the decoded bit pattern is written to result.
1010
 */
1011
static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
1012
                                   unsigned int imms, unsigned int immr)
1013
{
1014
    uint64_t mask;
1015
    unsigned e, levels, s, r;
1016
    int len;
1017

    
1018
    assert(immn < 2 && imms < 64 && immr < 64);
1019

    
1020
    /* The bit patterns we create here are 64 bit patterns which
1021
     * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
1022
     * 64 bits each. Each element contains the same value: a run
1023
     * of between 1 and e-1 non-zero bits, rotated within the
1024
     * element by between 0 and e-1 bits.
1025
     *
1026
     * The element size and run length are encoded into immn (1 bit)
1027
     * and imms (6 bits) as follows:
1028
     * 64 bit elements: immn = 1, imms = <length of run - 1>
1029
     * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
1030
     * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
1031
     *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
1032
     *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
1033
     *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
1034
     * Notice that immn = 0, imms = 11111x is the only combination
1035
     * not covered by one of the above options; this is reserved.
1036
     * Further, <length of run - 1> all-ones is a reserved pattern.
1037
     *
1038
     * In all cases the rotation is by immr % e (and immr is 6 bits).
1039
     */
1040

    
1041
    /* First determine the element size */
1042
    len = 31 - clz32((immn << 6) | (~imms & 0x3f));
1043
    if (len < 1) {
1044
        /* This is the immn == 0, imms == 0x11111x case */
1045
        return false;
1046
    }
1047
    e = 1 << len;
1048

    
1049
    levels = e - 1;
1050
    s = imms & levels;
1051
    r = immr & levels;
1052

    
1053
    if (s == levels) {
1054
        /* <length of run - 1> mustn't be all-ones. */
1055
        return false;
1056
    }
1057

    
1058
    /* Create the value of one element: s+1 set bits rotated
1059
     * by r within the element (which is e bits wide)...
1060
     */
1061
    mask = bitmask64(s + 1);
1062
    mask = (mask >> r) | (mask << (e - r));
1063
    /* ...then replicate the element over the whole 64 bit value */
1064
    mask = bitfield_replicate(mask, e);
1065
    *result = mask;
1066
    return true;
1067
}
1068

    
1069
/* C3.4.4 Logical (immediate)
1070
 *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
1071
 * +----+-----+-------------+---+------+------+------+------+
1072
 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
1073
 * +----+-----+-------------+---+------+------+------+------+
1074
 */
1075
static void disas_logic_imm(DisasContext *s, uint32_t insn)
1076
{
1077
    unsigned int sf, opc, is_n, immr, imms, rn, rd;
1078
    TCGv_i64 tcg_rd, tcg_rn;
1079
    uint64_t wmask;
1080
    bool is_and = false;
1081

    
1082
    sf = extract32(insn, 31, 1);
1083
    opc = extract32(insn, 29, 2);
1084
    is_n = extract32(insn, 22, 1);
1085
    immr = extract32(insn, 16, 6);
1086
    imms = extract32(insn, 10, 6);
1087
    rn = extract32(insn, 5, 5);
1088
    rd = extract32(insn, 0, 5);
1089

    
1090
    if (!sf && is_n) {
1091
        unallocated_encoding(s);
1092
        return;
1093
    }
1094

    
1095
    if (opc == 0x3) { /* ANDS */
1096
        tcg_rd = cpu_reg(s, rd);
1097
    } else {
1098
        tcg_rd = cpu_reg_sp(s, rd);
1099
    }
1100
    tcg_rn = cpu_reg(s, rn);
1101

    
1102
    if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
1103
        /* some immediate field values are reserved */
1104
        unallocated_encoding(s);
1105
        return;
1106
    }
1107

    
1108
    if (!sf) {
1109
        wmask &= 0xffffffff;
1110
    }
1111

    
1112
    switch (opc) {
1113
    case 0x3: /* ANDS */
1114
    case 0x0: /* AND */
1115
        tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
1116
        is_and = true;
1117
        break;
1118
    case 0x1: /* ORR */
1119
        tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
1120
        break;
1121
    case 0x2: /* EOR */
1122
        tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
1123
        break;
1124
    default:
1125
        assert(FALSE); /* must handle all above */
1126
        break;
1127
    }
1128

    
1129
    if (!sf && !is_and) {
1130
        /* zero extend final result; we know we can skip this for AND
1131
         * since the immediate had the high 32 bits clear.
1132
         */
1133
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1134
    }
1135

    
1136
    if (opc == 3) { /* ANDS */
1137
        gen_logic_CC(sf, tcg_rd);
1138
    }
1139
}
1140

    
1141
/* Move wide (immediate) */
1142
static void disas_movw_imm(DisasContext *s, uint32_t insn)
1143
{
1144
    unsupported_encoding(s, insn);
1145
}
1146

    
1147
/* C3.4.2 Bitfield
1148
 *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
1149
 * +----+-----+-------------+---+------+------+------+------+
1150
 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
1151
 * +----+-----+-------------+---+------+------+------+------+
1152
 */
1153
static void disas_bitfield(DisasContext *s, uint32_t insn)
1154
{
1155
    unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
1156
    TCGv_i64 tcg_rd, tcg_tmp;
1157

    
1158
    sf = extract32(insn, 31, 1);
1159
    opc = extract32(insn, 29, 2);
1160
    n = extract32(insn, 22, 1);
1161
    ri = extract32(insn, 16, 6);
1162
    si = extract32(insn, 10, 6);
1163
    rn = extract32(insn, 5, 5);
1164
    rd = extract32(insn, 0, 5);
1165
    bitsize = sf ? 64 : 32;
1166

    
1167
    if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
1168
        unallocated_encoding(s);
1169
        return;
1170
    }
1171

    
1172
    tcg_rd = cpu_reg(s, rd);
1173
    tcg_tmp = read_cpu_reg(s, rn, sf);
1174

    
1175
    /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
1176

    
1177
    if (opc != 1) { /* SBFM or UBFM */
1178
        tcg_gen_movi_i64(tcg_rd, 0);
1179
    }
1180

    
1181
    /* do the bit move operation */
1182
    if (si >= ri) {
1183
        /* Wd<s-r:0> = Wn<s:r> */
1184
        tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
1185
        pos = 0;
1186
        len = (si - ri) + 1;
1187
    } else {
1188
        /* Wd<32+s-r,32-r> = Wn<s:0> */
1189
        pos = bitsize - ri;
1190
        len = si + 1;
1191
    }
1192

    
1193
    tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
1194

    
1195
    if (opc == 0) { /* SBFM - sign extend the destination field */
1196
        tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
1197
        tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
1198
    }
1199

    
1200
    if (!sf) { /* zero extend final result */
1201
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1202
    }
1203
}
1204

    
1205
/* C3.4.3 Extract
1206
 *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
1207
 * +----+------+-------------+---+----+------+--------+------+------+
1208
 * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
1209
 * +----+------+-------------+---+----+------+--------+------+------+
1210
 */
1211
static void disas_extract(DisasContext *s, uint32_t insn)
1212
{
1213
    unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
1214

    
1215
    sf = extract32(insn, 31, 1);
1216
    n = extract32(insn, 22, 1);
1217
    rm = extract32(insn, 16, 5);
1218
    imm = extract32(insn, 10, 6);
1219
    rn = extract32(insn, 5, 5);
1220
    rd = extract32(insn, 0, 5);
1221
    op21 = extract32(insn, 29, 2);
1222
    op0 = extract32(insn, 21, 1);
1223
    bitsize = sf ? 64 : 32;
1224

    
1225
    if (sf != n || op21 || op0 || imm >= bitsize) {
1226
        unallocated_encoding(s);
1227
    } else {
1228
        TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
1229

    
1230
        tcg_rd = cpu_reg(s, rd);
1231

    
1232
        if (imm) {
1233
            /* OPTME: we can special case rm==rn as a rotate */
1234
            tcg_rm = read_cpu_reg(s, rm, sf);
1235
            tcg_rn = read_cpu_reg(s, rn, sf);
1236
            tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
1237
            tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
1238
            tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
1239
            if (!sf) {
1240
                tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1241
            }
1242
        } else {
1243
            /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
1244
             * so an extract from bit 0 is a special case.
1245
             */
1246
            if (sf) {
1247
                tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
1248
            } else {
1249
                tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
1250
            }
1251
        }
1252

    
1253
    }
1254
}
1255

    
1256
/* C3.4 Data processing - immediate */
1257
static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
1258
{
1259
    switch (extract32(insn, 23, 6)) {
1260
    case 0x20: case 0x21: /* PC-rel. addressing */
1261
        disas_pc_rel_adr(s, insn);
1262
        break;
1263
    case 0x22: case 0x23: /* Add/subtract (immediate) */
1264
        disas_add_sub_imm(s, insn);
1265
        break;
1266
    case 0x24: /* Logical (immediate) */
1267
        disas_logic_imm(s, insn);
1268
        break;
1269
    case 0x25: /* Move wide (immediate) */
1270
        disas_movw_imm(s, insn);
1271
        break;
1272
    case 0x26: /* Bitfield */
1273
        disas_bitfield(s, insn);
1274
        break;
1275
    case 0x27: /* Extract */
1276
        disas_extract(s, insn);
1277
        break;
1278
    default:
1279
        unallocated_encoding(s);
1280
        break;
1281
    }
1282
}
1283

    
1284
/* Shift a TCGv src by TCGv shift_amount, put result in dst.
1285
 * Note that it is the caller's responsibility to ensure that the
1286
 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
1287
 * mandated semantics for out of range shifts.
1288
 */
1289
static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
1290
                      enum a64_shift_type shift_type, TCGv_i64 shift_amount)
1291
{
1292
    switch (shift_type) {
1293
    case A64_SHIFT_TYPE_LSL:
1294
        tcg_gen_shl_i64(dst, src, shift_amount);
1295
        break;
1296
    case A64_SHIFT_TYPE_LSR:
1297
        tcg_gen_shr_i64(dst, src, shift_amount);
1298
        break;
1299
    case A64_SHIFT_TYPE_ASR:
1300
        if (!sf) {
1301
            tcg_gen_ext32s_i64(dst, src);
1302
        }
1303
        tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
1304
        break;
1305
    case A64_SHIFT_TYPE_ROR:
1306
        if (sf) {
1307
            tcg_gen_rotr_i64(dst, src, shift_amount);
1308
        } else {
1309
            TCGv_i32 t0, t1;
1310
            t0 = tcg_temp_new_i32();
1311
            t1 = tcg_temp_new_i32();
1312
            tcg_gen_trunc_i64_i32(t0, src);
1313
            tcg_gen_trunc_i64_i32(t1, shift_amount);
1314
            tcg_gen_rotr_i32(t0, t0, t1);
1315
            tcg_gen_extu_i32_i64(dst, t0);
1316
            tcg_temp_free_i32(t0);
1317
            tcg_temp_free_i32(t1);
1318
        }
1319
        break;
1320
    default:
1321
        assert(FALSE); /* all shift types should be handled */
1322
        break;
1323
    }
1324

    
1325
    if (!sf) { /* zero extend final result */
1326
        tcg_gen_ext32u_i64(dst, dst);
1327
    }
1328
}
1329

    
1330
/* Shift a TCGv src by immediate, put result in dst.
1331
 * The shift amount must be in range (this should always be true as the
1332
 * relevant instructions will UNDEF on bad shift immediates).
1333
 */
1334
static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
1335
                          enum a64_shift_type shift_type, unsigned int shift_i)
1336
{
1337
    assert(shift_i < (sf ? 64 : 32));
1338

    
1339
    if (shift_i == 0) {
1340
        tcg_gen_mov_i64(dst, src);
1341
    } else {
1342
        TCGv_i64 shift_const;
1343

    
1344
        shift_const = tcg_const_i64(shift_i);
1345
        shift_reg(dst, src, sf, shift_type, shift_const);
1346
        tcg_temp_free_i64(shift_const);
1347
    }
1348
}
1349

    
1350
/* C3.5.10 Logical (shifted register)
1351
 *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
1352
 * +----+-----+-----------+-------+---+------+--------+------+------+
1353
 * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
1354
 * +----+-----+-----------+-------+---+------+--------+------+------+
1355
 */
1356
static void disas_logic_reg(DisasContext *s, uint32_t insn)
1357
{
1358
    TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
1359
    unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
1360

    
1361
    sf = extract32(insn, 31, 1);
1362
    opc = extract32(insn, 29, 2);
1363
    shift_type = extract32(insn, 22, 2);
1364
    invert = extract32(insn, 21, 1);
1365
    rm = extract32(insn, 16, 5);
1366
    shift_amount = extract32(insn, 10, 6);
1367
    rn = extract32(insn, 5, 5);
1368
    rd = extract32(insn, 0, 5);
1369

    
1370
    if (!sf && (shift_amount & (1 << 5))) {
1371
        unallocated_encoding(s);
1372
        return;
1373
    }
1374

    
1375
    tcg_rd = cpu_reg(s, rd);
1376

    
1377
    if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
1378
        /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
1379
         * register-register MOV and MVN, so it is worth special casing.
1380
         */
1381
        tcg_rm = cpu_reg(s, rm);
1382
        if (invert) {
1383
            tcg_gen_not_i64(tcg_rd, tcg_rm);
1384
            if (!sf) {
1385
                tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1386
            }
1387
        } else {
1388
            if (sf) {
1389
                tcg_gen_mov_i64(tcg_rd, tcg_rm);
1390
            } else {
1391
                tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
1392
            }
1393
        }
1394
        return;
1395
    }
1396

    
1397
    tcg_rm = read_cpu_reg(s, rm, sf);
1398

    
1399
    if (shift_amount) {
1400
        shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
1401
    }
1402

    
1403
    tcg_rn = cpu_reg(s, rn);
1404

    
1405
    switch (opc | (invert << 2)) {
1406
    case 0: /* AND */
1407
    case 3: /* ANDS */
1408
        tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
1409
        break;
1410
    case 1: /* ORR */
1411
        tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
1412
        break;
1413
    case 2: /* EOR */
1414
        tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
1415
        break;
1416
    case 4: /* BIC */
1417
    case 7: /* BICS */
1418
        tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
1419
        break;
1420
    case 5: /* ORN */
1421
        tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
1422
        break;
1423
    case 6: /* EON */
1424
        tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
1425
        break;
1426
    default:
1427
        assert(FALSE);
1428
        break;
1429
    }
1430

    
1431
    if (!sf) {
1432
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1433
    }
1434

    
1435
    if (opc == 3) {
1436
        gen_logic_CC(sf, tcg_rd);
1437
    }
1438
}
1439

    
1440
/* Add/subtract (extended register) */
1441
static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
1442
{
1443
    unsupported_encoding(s, insn);
1444
}
1445

    
1446
/* Add/subtract (shifted register) */
1447
static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
1448
{
1449
    unsupported_encoding(s, insn);
1450
}
1451

    
1452
/* Data-processing (3 source) */
1453
static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
1454
{
1455
    unsupported_encoding(s, insn);
1456
}
1457

    
1458
/* Add/subtract (with carry) */
1459
static void disas_adc_sbc(DisasContext *s, uint32_t insn)
1460
{
1461
    unsupported_encoding(s, insn);
1462
}
1463

    
1464
/* Conditional compare (immediate) */
1465
static void disas_cc_imm(DisasContext *s, uint32_t insn)
1466
{
1467
    unsupported_encoding(s, insn);
1468
}
1469

    
1470
/* Conditional compare (register) */
1471
static void disas_cc_reg(DisasContext *s, uint32_t insn)
1472
{
1473
    unsupported_encoding(s, insn);
1474
}
1475

    
1476
/* C3.5.6 Conditional select
1477
 *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
1478
 * +----+----+---+-----------------+------+------+-----+------+------+
1479
 * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
1480
 * +----+----+---+-----------------+------+------+-----+------+------+
1481
 */
1482
static void disas_cond_select(DisasContext *s, uint32_t insn)
1483
{
1484
    unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
1485
    TCGv_i64 tcg_rd, tcg_src;
1486

    
1487
    if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
1488
        /* S == 1 or op2<1> == 1 */
1489
        unallocated_encoding(s);
1490
        return;
1491
    }
1492
    sf = extract32(insn, 31, 1);
1493
    else_inv = extract32(insn, 30, 1);
1494
    rm = extract32(insn, 16, 5);
1495
    cond = extract32(insn, 12, 4);
1496
    else_inc = extract32(insn, 10, 1);
1497
    rn = extract32(insn, 5, 5);
1498
    rd = extract32(insn, 0, 5);
1499

    
1500
    if (rd == 31) {
1501
        /* silly no-op write; until we use movcond we must special-case
1502
         * this to avoid a dead temporary across basic blocks.
1503
         */
1504
        return;
1505
    }
1506

    
1507
    tcg_rd = cpu_reg(s, rd);
1508

    
1509
    if (cond >= 0x0e) { /* condition "always" */
1510
        tcg_src = read_cpu_reg(s, rn, sf);
1511
        tcg_gen_mov_i64(tcg_rd, tcg_src);
1512
    } else {
1513
        /* OPTME: we could use movcond here, at the cost of duplicating
1514
         * a lot of the arm_gen_test_cc() logic.
1515
         */
1516
        int label_match = gen_new_label();
1517
        int label_continue = gen_new_label();
1518

    
1519
        arm_gen_test_cc(cond, label_match);
1520
        /* nomatch: */
1521
        tcg_src = cpu_reg(s, rm);
1522

    
1523
        if (else_inv && else_inc) {
1524
            tcg_gen_neg_i64(tcg_rd, tcg_src);
1525
        } else if (else_inv) {
1526
            tcg_gen_not_i64(tcg_rd, tcg_src);
1527
        } else if (else_inc) {
1528
            tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
1529
        } else {
1530
            tcg_gen_mov_i64(tcg_rd, tcg_src);
1531
        }
1532
        if (!sf) {
1533
            tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1534
        }
1535
        tcg_gen_br(label_continue);
1536
        /* match: */
1537
        gen_set_label(label_match);
1538
        tcg_src = read_cpu_reg(s, rn, sf);
1539
        tcg_gen_mov_i64(tcg_rd, tcg_src);
1540
        /* continue: */
1541
        gen_set_label(label_continue);
1542
    }
1543
}
1544

    
1545
static void handle_clz(DisasContext *s, unsigned int sf,
1546
                       unsigned int rn, unsigned int rd)
1547
{
1548
    TCGv_i64 tcg_rd, tcg_rn;
1549
    tcg_rd = cpu_reg(s, rd);
1550
    tcg_rn = cpu_reg(s, rn);
1551

    
1552
    if (sf) {
1553
        gen_helper_clz64(tcg_rd, tcg_rn);
1554
    } else {
1555
        TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
1556
        tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
1557
        gen_helper_clz(tcg_tmp32, tcg_tmp32);
1558
        tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
1559
        tcg_temp_free_i32(tcg_tmp32);
1560
    }
1561
}
1562

    
1563
static void handle_cls(DisasContext *s, unsigned int sf,
1564
                       unsigned int rn, unsigned int rd)
1565
{
1566
    TCGv_i64 tcg_rd, tcg_rn;
1567
    tcg_rd = cpu_reg(s, rd);
1568
    tcg_rn = cpu_reg(s, rn);
1569

    
1570
    if (sf) {
1571
        gen_helper_cls64(tcg_rd, tcg_rn);
1572
    } else {
1573
        TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
1574
        tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
1575
        gen_helper_cls32(tcg_tmp32, tcg_tmp32);
1576
        tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
1577
        tcg_temp_free_i32(tcg_tmp32);
1578
    }
1579
}
1580

    
1581
static void handle_rbit(DisasContext *s, unsigned int sf,
1582
                        unsigned int rn, unsigned int rd)
1583
{
1584
    TCGv_i64 tcg_rd, tcg_rn;
1585
    tcg_rd = cpu_reg(s, rd);
1586
    tcg_rn = cpu_reg(s, rn);
1587

    
1588
    if (sf) {
1589
        gen_helper_rbit64(tcg_rd, tcg_rn);
1590
    } else {
1591
        TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
1592
        tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
1593
        gen_helper_rbit(tcg_tmp32, tcg_tmp32);
1594
        tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
1595
        tcg_temp_free_i32(tcg_tmp32);
1596
    }
1597
}
1598

    
1599
/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
1600
static void handle_rev64(DisasContext *s, unsigned int sf,
1601
                         unsigned int rn, unsigned int rd)
1602
{
1603
    if (!sf) {
1604
        unallocated_encoding(s);
1605
        return;
1606
    }
1607
    tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
1608
}
1609

    
1610
/* C5.6.149 REV with sf==0, opcode==2
1611
 * C5.6.151 REV32 (sf==1, opcode==2)
1612
 */
1613
static void handle_rev32(DisasContext *s, unsigned int sf,
1614
                         unsigned int rn, unsigned int rd)
1615
{
1616
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
1617

    
1618
    if (sf) {
1619
        TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1620
        TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
1621

    
1622
        /* bswap32_i64 requires zero high word */
1623
        tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
1624
        tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
1625
        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
1626
        tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
1627
        tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
1628

    
1629
        tcg_temp_free_i64(tcg_tmp);
1630
    } else {
1631
        tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
1632
        tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
1633
    }
1634
}
1635

    
1636
/* C5.6.150 REV16 (opcode==1) */
1637
static void handle_rev16(DisasContext *s, unsigned int sf,
1638
                         unsigned int rn, unsigned int rd)
1639
{
1640
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
1641
    TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1642
    TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
1643

    
1644
    tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
1645
    tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
1646

    
1647
    tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
1648
    tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
1649
    tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
1650
    tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
1651

    
1652
    if (sf) {
1653
        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
1654
        tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
1655
        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
1656
        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
1657

    
1658
        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
1659
        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
1660
        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
1661
    }
1662

    
1663
    tcg_temp_free_i64(tcg_tmp);
1664
}
1665

    
1666
/* C3.5.7 Data-processing (1 source)
1667
 *   31  30  29  28             21 20     16 15    10 9    5 4    0
1668
 * +----+---+---+-----------------+---------+--------+------+------+
1669
 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
1670
 * +----+---+---+-----------------+---------+--------+------+------+
1671
 */
1672
static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
1673
{
1674
    unsigned int sf, opcode, rn, rd;
1675

    
1676
    if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
1677
        unallocated_encoding(s);
1678
        return;
1679
    }
1680

    
1681
    sf = extract32(insn, 31, 1);
1682
    opcode = extract32(insn, 10, 6);
1683
    rn = extract32(insn, 5, 5);
1684
    rd = extract32(insn, 0, 5);
1685

    
1686
    switch (opcode) {
1687
    case 0: /* RBIT */
1688
        handle_rbit(s, sf, rn, rd);
1689
        break;
1690
    case 1: /* REV16 */
1691
        handle_rev16(s, sf, rn, rd);
1692
        break;
1693
    case 2: /* REV32 */
1694
        handle_rev32(s, sf, rn, rd);
1695
        break;
1696
    case 3: /* REV64 */
1697
        handle_rev64(s, sf, rn, rd);
1698
        break;
1699
    case 4: /* CLZ */
1700
        handle_clz(s, sf, rn, rd);
1701
        break;
1702
    case 5: /* CLS */
1703
        handle_cls(s, sf, rn, rd);
1704
        break;
1705
    }
1706
}
1707

    
1708
static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
1709
                       unsigned int rm, unsigned int rn, unsigned int rd)
1710
{
1711
    TCGv_i64 tcg_n, tcg_m, tcg_rd;
1712
    tcg_rd = cpu_reg(s, rd);
1713

    
1714
    if (!sf && is_signed) {
1715
        tcg_n = new_tmp_a64(s);
1716
        tcg_m = new_tmp_a64(s);
1717
        tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
1718
        tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
1719
    } else {
1720
        tcg_n = read_cpu_reg(s, rn, sf);
1721
        tcg_m = read_cpu_reg(s, rm, sf);
1722
    }
1723

    
1724
    if (is_signed) {
1725
        gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
1726
    } else {
1727
        gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
1728
    }
1729

    
1730
    if (!sf) { /* zero extend final result */
1731
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
1732
    }
1733
}
1734

    
1735
/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
1736
static void handle_shift_reg(DisasContext *s,
1737
                             enum a64_shift_type shift_type, unsigned int sf,
1738
                             unsigned int rm, unsigned int rn, unsigned int rd)
1739
{
1740
    TCGv_i64 tcg_shift = tcg_temp_new_i64();
1741
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
1742
    TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
1743

    
1744
    tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
1745
    shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
1746
    tcg_temp_free_i64(tcg_shift);
1747
}
1748

    
1749
/* C3.5.8 Data-processing (2 source)
1750
 *   31   30  29 28             21 20  16 15    10 9    5 4    0
1751
 * +----+---+---+-----------------+------+--------+------+------+
1752
 * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
1753
 * +----+---+---+-----------------+------+--------+------+------+
1754
 */
1755
static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
1756
{
1757
    unsigned int sf, rm, opcode, rn, rd;
1758
    sf = extract32(insn, 31, 1);
1759
    rm = extract32(insn, 16, 5);
1760
    opcode = extract32(insn, 10, 6);
1761
    rn = extract32(insn, 5, 5);
1762
    rd = extract32(insn, 0, 5);
1763

    
1764
    if (extract32(insn, 29, 1)) {
1765
        unallocated_encoding(s);
1766
        return;
1767
    }
1768

    
1769
    switch (opcode) {
1770
    case 2: /* UDIV */
1771
        handle_div(s, false, sf, rm, rn, rd);
1772
        break;
1773
    case 3: /* SDIV */
1774
        handle_div(s, true, sf, rm, rn, rd);
1775
        break;
1776
    case 8: /* LSLV */
1777
        handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
1778
        break;
1779
    case 9: /* LSRV */
1780
        handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
1781
        break;
1782
    case 10: /* ASRV */
1783
        handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
1784
        break;
1785
    case 11: /* RORV */
1786
        handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
1787
        break;
1788
    case 16:
1789
    case 17:
1790
    case 18:
1791
    case 19:
1792
    case 20:
1793
    case 21:
1794
    case 22:
1795
    case 23: /* CRC32 */
1796
        unsupported_encoding(s, insn);
1797
        break;
1798
    default:
1799
        unallocated_encoding(s);
1800
        break;
1801
    }
1802
}
1803

    
1804
/* C3.5 Data processing - register */
1805
static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
1806
{
1807
    switch (extract32(insn, 24, 5)) {
1808
    case 0x0a: /* Logical (shifted register) */
1809
        disas_logic_reg(s, insn);
1810
        break;
1811
    case 0x0b: /* Add/subtract */
1812
        if (insn & (1 << 21)) { /* (extended register) */
1813
            disas_add_sub_ext_reg(s, insn);
1814
        } else {
1815
            disas_add_sub_reg(s, insn);
1816
        }
1817
        break;
1818
    case 0x1b: /* Data-processing (3 source) */
1819
        disas_data_proc_3src(s, insn);
1820
        break;
1821
    case 0x1a:
1822
        switch (extract32(insn, 21, 3)) {
1823
        case 0x0: /* Add/subtract (with carry) */
1824
            disas_adc_sbc(s, insn);
1825
            break;
1826
        case 0x2: /* Conditional compare */
1827
            if (insn & (1 << 11)) { /* (immediate) */
1828
                disas_cc_imm(s, insn);
1829
            } else {            /* (register) */
1830
                disas_cc_reg(s, insn);
1831
            }
1832
            break;
1833
        case 0x4: /* Conditional select */
1834
            disas_cond_select(s, insn);
1835
            break;
1836
        case 0x6: /* Data-processing */
1837
            if (insn & (1 << 30)) { /* (1 source) */
1838
                disas_data_proc_1src(s, insn);
1839
            } else {            /* (2 source) */
1840
                disas_data_proc_2src(s, insn);
1841
            }
1842
            break;
1843
        default:
1844
            unallocated_encoding(s);
1845
            break;
1846
        }
1847
        break;
1848
    default:
1849
        unallocated_encoding(s);
1850
        break;
1851
    }
1852
}
1853

    
1854
/* C3.6 Data processing - SIMD and floating point */
1855
static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
1856
{
1857
    unsupported_encoding(s, insn);
1858
}
1859

    
1860
/* C3.1 A64 instruction index by encoding */
1861
static void disas_a64_insn(CPUARMState *env, DisasContext *s)
1862
{
1863
    uint32_t insn;
1864

    
1865
    insn = arm_ldl_code(env, s->pc, s->bswap_code);
1866
    s->insn = insn;
1867
    s->pc += 4;
1868

    
1869
    switch (extract32(insn, 25, 4)) {
1870
    case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
1871
        unallocated_encoding(s);
1872
        break;
1873
    case 0x8: case 0x9: /* Data processing - immediate */
1874
        disas_data_proc_imm(s, insn);
1875
        break;
1876
    case 0xa: case 0xb: /* Branch, exception generation and system insns */
1877
        disas_b_exc_sys(s, insn);
1878
        break;
1879
    case 0x4:
1880
    case 0x6:
1881
    case 0xc:
1882
    case 0xe:      /* Loads and stores */
1883
        disas_ldst(s, insn);
1884
        break;
1885
    case 0x5:
1886
    case 0xd:      /* Data processing - register */
1887
        disas_data_proc_reg(s, insn);
1888
        break;
1889
    case 0x7:
1890
    case 0xf:      /* Data processing - SIMD and floating point */
1891
        disas_data_proc_simd_fp(s, insn);
1892
        break;
1893
    default:
1894
        assert(FALSE); /* all 15 cases should be handled above */
1895
        break;
1896
    }
1897

    
1898
    /* if we allocated any temporaries, free them here */
1899
    free_tmp_a64(s);
1900
}
1901

    
1902
void gen_intermediate_code_internal_a64(ARMCPU *cpu,
1903
                                        TranslationBlock *tb,
1904
                                        bool search_pc)
1905
{
1906
    CPUState *cs = CPU(cpu);
1907
    CPUARMState *env = &cpu->env;
1908
    DisasContext dc1, *dc = &dc1;
1909
    CPUBreakpoint *bp;
1910
    uint16_t *gen_opc_end;
1911
    int j, lj;
1912
    target_ulong pc_start;
1913
    target_ulong next_page_start;
1914
    int num_insns;
1915
    int max_insns;
1916

    
1917
    pc_start = tb->pc;
1918

    
1919
    dc->tb = tb;
1920

    
1921
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
1922

    
1923
    dc->is_jmp = DISAS_NEXT;
1924
    dc->pc = pc_start;
1925
    dc->singlestep_enabled = cs->singlestep_enabled;
1926
    dc->condjmp = 0;
1927

    
1928
    dc->aarch64 = 1;
1929
    dc->thumb = 0;
1930
    dc->bswap_code = 0;
1931
    dc->condexec_mask = 0;
1932
    dc->condexec_cond = 0;
1933
#if !defined(CONFIG_USER_ONLY)
1934
    dc->user = 0;
1935
#endif
1936
    dc->vfp_enabled = 0;
1937
    dc->vec_len = 0;
1938
    dc->vec_stride = 0;
1939

    
1940
    init_tmp_a64_array(dc);
1941

    
1942
    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1943
    lj = -1;
1944
    num_insns = 0;
1945
    max_insns = tb->cflags & CF_COUNT_MASK;
1946
    if (max_insns == 0) {
1947
        max_insns = CF_COUNT_MASK;
1948
    }
1949

    
1950
    gen_tb_start();
1951

    
1952
    tcg_clear_temp_count();
1953

    
1954
    do {
1955
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
1956
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1957
                if (bp->pc == dc->pc) {
1958
                    gen_exception_insn(dc, 0, EXCP_DEBUG);
1959
                    /* Advance PC so that clearing the breakpoint will
1960
                       invalidate this TB.  */
1961
                    dc->pc += 2;
1962
                    goto done_generating;
1963
                }
1964
            }
1965
        }
1966

    
1967
        if (search_pc) {
1968
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1969
            if (lj < j) {
1970
                lj++;
1971
                while (lj < j) {
1972
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
1973
                }
1974
            }
1975
            tcg_ctx.gen_opc_pc[lj] = dc->pc;
1976
            tcg_ctx.gen_opc_instr_start[lj] = 1;
1977
            tcg_ctx.gen_opc_icount[lj] = num_insns;
1978
        }
1979

    
1980
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
1981
            gen_io_start();
1982
        }
1983

    
1984
        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
1985
            tcg_gen_debug_insn_start(dc->pc);
1986
        }
1987

    
1988
        disas_a64_insn(env, dc);
1989

    
1990
        if (tcg_check_temp_count()) {
1991
            fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
1992
                    dc->pc);
1993
        }
1994

    
1995
        /* Translation stops when a conditional branch is encountered.
1996
         * Otherwise the subsequent code could get translated several times.
1997
         * Also stop translation when a page boundary is reached.  This
1998
         * ensures prefetch aborts occur at the right place.
1999
         */
2000
        num_insns++;
2001
    } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
2002
             !cs->singlestep_enabled &&
2003
             !singlestep &&
2004
             dc->pc < next_page_start &&
2005
             num_insns < max_insns);
2006

    
2007
    if (tb->cflags & CF_LAST_IO) {
2008
        gen_io_end();
2009
    }
2010

    
2011
    if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
2012
        /* Note that this means single stepping WFI doesn't halt the CPU.
2013
         * For conditional branch insns this is harmless unreachable code as
2014
         * gen_goto_tb() has already handled emitting the debug exception
2015
         * (and thus a tb-jump is not possible when singlestepping).
2016
         */
2017
        assert(dc->is_jmp != DISAS_TB_JUMP);
2018
        if (dc->is_jmp != DISAS_JUMP) {
2019
            gen_a64_set_pc_im(dc->pc);
2020
        }
2021
        gen_exception(EXCP_DEBUG);
2022
    } else {
2023
        switch (dc->is_jmp) {
2024
        case DISAS_NEXT:
2025
            gen_goto_tb(dc, 1, dc->pc);
2026
            break;
2027
        default:
2028
        case DISAS_JUMP:
2029
        case DISAS_UPDATE:
2030
            /* indicate that the hash table must be used to find the next TB */
2031
            tcg_gen_exit_tb(0);
2032
            break;
2033
        case DISAS_TB_JUMP:
2034
        case DISAS_EXC:
2035
        case DISAS_SWI:
2036
            break;
2037
        case DISAS_WFI:
2038
            /* This is a special case because we don't want to just halt the CPU
2039
             * if trying to debug across a WFI.
2040
             */
2041
            gen_helper_wfi(cpu_env);
2042
            break;
2043
        }
2044
    }
2045

    
2046
done_generating:
2047
    gen_tb_end(tb, num_insns);
2048
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
2049

    
2050
#ifdef DEBUG_DISAS
2051
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
2052
        qemu_log("----------------\n");
2053
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
2054
        log_target_disas(env, pc_start, dc->pc - pc_start,
2055
                         dc->thumb | (dc->bswap_code << 1));
2056
        qemu_log("\n");
2057
    }
2058
#endif
2059
    if (search_pc) {
2060
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2061
        lj++;
2062
        while (lj <= j) {
2063
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
2064
        }
2065
    } else {
2066
        tb->size = dc->pc - pc_start;
2067
        tb->icount = num_insns;
2068
    }
2069
}