Statistics
| Branch: | Revision:

root / target-arm / helper.c @ c4241c7d

History | View | Annotate | Download (143.1 kB)

1
#include "cpu.h"
2
#include "exec/gdbstub.h"
3
#include "helper.h"
4
#include "qemu/host-utils.h"
5
#include "sysemu/arch_init.h"
6
#include "sysemu/sysemu.h"
7
#include "qemu/bitops.h"
8

    
9
#ifndef CONFIG_USER_ONLY
10
static inline int get_phys_addr(CPUARMState *env, uint32_t address,
11
                                int access_type, int is_user,
12
                                hwaddr *phys_ptr, int *prot,
13
                                target_ulong *page_size);
14
#endif
15

    
16
static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
17
{
18
    int nregs;
19

    
20
    /* VFP data registers are always little-endian.  */
21
    nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
22
    if (reg < nregs) {
23
        stfq_le_p(buf, env->vfp.regs[reg]);
24
        return 8;
25
    }
26
    if (arm_feature(env, ARM_FEATURE_NEON)) {
27
        /* Aliases for Q regs.  */
28
        nregs += 16;
29
        if (reg < nregs) {
30
            stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
31
            stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
32
            return 16;
33
        }
34
    }
35
    switch (reg - nregs) {
36
    case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
37
    case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
38
    case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
39
    }
40
    return 0;
41
}
42

    
43
static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
44
{
45
    int nregs;
46

    
47
    nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
48
    if (reg < nregs) {
49
        env->vfp.regs[reg] = ldfq_le_p(buf);
50
        return 8;
51
    }
52
    if (arm_feature(env, ARM_FEATURE_NEON)) {
53
        nregs += 16;
54
        if (reg < nregs) {
55
            env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
56
            env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
57
            return 16;
58
        }
59
    }
60
    switch (reg - nregs) {
61
    case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
62
    case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
63
    case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
64
    }
65
    return 0;
66
}
67

    
68
static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
69
{
70
    switch (reg) {
71
    case 0 ... 31:
72
        /* 128 bit FP register */
73
        stfq_le_p(buf, env->vfp.regs[reg * 2]);
74
        stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
75
        return 16;
76
    case 32:
77
        /* FPSR */
78
        stl_p(buf, vfp_get_fpsr(env));
79
        return 4;
80
    case 33:
81
        /* FPCR */
82
        stl_p(buf, vfp_get_fpcr(env));
83
        return 4;
84
    default:
85
        return 0;
86
    }
87
}
88

    
89
static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
90
{
91
    switch (reg) {
92
    case 0 ... 31:
93
        /* 128 bit FP register */
94
        env->vfp.regs[reg * 2] = ldfq_le_p(buf);
95
        env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
96
        return 16;
97
    case 32:
98
        /* FPSR */
99
        vfp_set_fpsr(env, ldl_p(buf));
100
        return 4;
101
    case 33:
102
        /* FPCR */
103
        vfp_set_fpcr(env, ldl_p(buf));
104
        return 4;
105
    default:
106
        return 0;
107
    }
108
}
109

    
110
static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
111
{
112
    if (ri->type & ARM_CP_64BIT) {
113
        return CPREG_FIELD64(env, ri);
114
    } else {
115
        return CPREG_FIELD32(env, ri);
116
    }
117
}
118

    
119
static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
120
                      uint64_t value)
121
{
122
    if (ri->type & ARM_CP_64BIT) {
123
        CPREG_FIELD64(env, ri) = value;
124
    } else {
125
        CPREG_FIELD32(env, ri) = value;
126
    }
127
}
128

    
129
static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
130
                            uint64_t *v)
131
{
132
    /* Raw read of a coprocessor register (as needed for migration, etc)
133
     * return true on success, false if the read is impossible for some reason.
134
     */
135
    if (ri->type & ARM_CP_CONST) {
136
        *v = ri->resetvalue;
137
    } else if (ri->raw_readfn) {
138
        *v = ri->raw_readfn(env, ri);
139
    } else if (ri->readfn) {
140
        *v = ri->readfn(env, ri);
141
    } else {
142
        *v = raw_read(env, ri);
143
    }
144
    return true;
145
}
146

    
147
static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
148
                             int64_t v)
149
{
150
    /* Raw write of a coprocessor register (as needed for migration, etc).
151
     * Return true on success, false if the write is impossible for some reason.
152
     * Note that constant registers are treated as write-ignored; the
153
     * caller should check for success by whether a readback gives the
154
     * value written.
155
     */
156
    if (ri->type & ARM_CP_CONST) {
157
        return true;
158
    } else if (ri->raw_writefn) {
159
        ri->raw_writefn(env, ri, v);
160
    } else if (ri->writefn) {
161
        ri->writefn(env, ri, v);
162
    } else {
163
        raw_write(env, ri, v);
164
    }
165
    return true;
166
}
167

    
168
bool write_cpustate_to_list(ARMCPU *cpu)
169
{
170
    /* Write the coprocessor state from cpu->env to the (index,value) list. */
171
    int i;
172
    bool ok = true;
173

    
174
    for (i = 0; i < cpu->cpreg_array_len; i++) {
175
        uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
176
        const ARMCPRegInfo *ri;
177
        uint64_t v;
178
        ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
179
        if (!ri) {
180
            ok = false;
181
            continue;
182
        }
183
        if (ri->type & ARM_CP_NO_MIGRATE) {
184
            continue;
185
        }
186
        if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
187
            ok = false;
188
            continue;
189
        }
190
        cpu->cpreg_values[i] = v;
191
    }
192
    return ok;
193
}
194

    
195
bool write_list_to_cpustate(ARMCPU *cpu)
196
{
197
    int i;
198
    bool ok = true;
199

    
200
    for (i = 0; i < cpu->cpreg_array_len; i++) {
201
        uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
202
        uint64_t v = cpu->cpreg_values[i];
203
        uint64_t readback;
204
        const ARMCPRegInfo *ri;
205

    
206
        ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
207
        if (!ri) {
208
            ok = false;
209
            continue;
210
        }
211
        if (ri->type & ARM_CP_NO_MIGRATE) {
212
            continue;
213
        }
214
        /* Write value and confirm it reads back as written
215
         * (to catch read-only registers and partially read-only
216
         * registers where the incoming migration value doesn't match)
217
         */
218
        if (!write_raw_cp_reg(&cpu->env, ri, v) ||
219
            !read_raw_cp_reg(&cpu->env, ri, &readback) ||
220
            readback != v) {
221
            ok = false;
222
        }
223
    }
224
    return ok;
225
}
226

    
227
static void add_cpreg_to_list(gpointer key, gpointer opaque)
228
{
229
    ARMCPU *cpu = opaque;
230
    uint64_t regidx;
231
    const ARMCPRegInfo *ri;
232

    
233
    regidx = *(uint32_t *)key;
234
    ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
235

    
236
    if (!(ri->type & ARM_CP_NO_MIGRATE)) {
237
        cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
238
        /* The value array need not be initialized at this point */
239
        cpu->cpreg_array_len++;
240
    }
241
}
242

    
243
static void count_cpreg(gpointer key, gpointer opaque)
244
{
245
    ARMCPU *cpu = opaque;
246
    uint64_t regidx;
247
    const ARMCPRegInfo *ri;
248

    
249
    regidx = *(uint32_t *)key;
250
    ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
251

    
252
    if (!(ri->type & ARM_CP_NO_MIGRATE)) {
253
        cpu->cpreg_array_len++;
254
    }
255
}
256

    
257
static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
258
{
259
    uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
260
    uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
261

    
262
    if (aidx > bidx) {
263
        return 1;
264
    }
265
    if (aidx < bidx) {
266
        return -1;
267
    }
268
    return 0;
269
}
270

    
271
static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
272
{
273
    GList **plist = udata;
274

    
275
    *plist = g_list_prepend(*plist, key);
276
}
277

    
278
void init_cpreg_list(ARMCPU *cpu)
279
{
280
    /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
281
     * Note that we require cpreg_tuples[] to be sorted by key ID.
282
     */
283
    GList *keys = NULL;
284
    int arraylen;
285

    
286
    g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
287

    
288
    keys = g_list_sort(keys, cpreg_key_compare);
289

    
290
    cpu->cpreg_array_len = 0;
291

    
292
    g_list_foreach(keys, count_cpreg, cpu);
293

    
294
    arraylen = cpu->cpreg_array_len;
295
    cpu->cpreg_indexes = g_new(uint64_t, arraylen);
296
    cpu->cpreg_values = g_new(uint64_t, arraylen);
297
    cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
298
    cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
299
    cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
300
    cpu->cpreg_array_len = 0;
301

    
302
    g_list_foreach(keys, add_cpreg_to_list, cpu);
303

    
304
    assert(cpu->cpreg_array_len == arraylen);
305

    
306
    g_list_free(keys);
307
}
308

    
309
static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
310
{
311
    env->cp15.c3 = value;
312
    tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
313
}
314

    
315
static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
316
{
317
    if (env->cp15.c13_fcse != value) {
318
        /* Unlike real hardware the qemu TLB uses virtual addresses,
319
         * not modified virtual addresses, so this causes a TLB flush.
320
         */
321
        tlb_flush(env, 1);
322
        env->cp15.c13_fcse = value;
323
    }
324
}
325

    
326
static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
327
                             uint64_t value)
328
{
329
    if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
330
        /* For VMSA (when not using the LPAE long descriptor page table
331
         * format) this register includes the ASID, so do a TLB flush.
332
         * For PMSA it is purely a process ID and no action is needed.
333
         */
334
        tlb_flush(env, 1);
335
    }
336
    env->cp15.c13_context = value;
337
}
338

    
339
static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
340
                          uint64_t value)
341
{
342
    /* Invalidate all (TLBIALL) */
343
    tlb_flush(env, 1);
344
}
345

    
346
static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
347
                          uint64_t value)
348
{
349
    /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
350
    tlb_flush_page(env, value & TARGET_PAGE_MASK);
351
}
352

    
353
static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
354
                           uint64_t value)
355
{
356
    /* Invalidate by ASID (TLBIASID) */
357
    tlb_flush(env, value == 0);
358
}
359

    
360
static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
361
                           uint64_t value)
362
{
363
    /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
364
    tlb_flush_page(env, value & TARGET_PAGE_MASK);
365
}
366

    
367
static const ARMCPRegInfo cp_reginfo[] = {
368
    /* DBGDIDR: just RAZ. In particular this means the "debug architecture
369
     * version" bits will read as a reserved value, which should cause
370
     * Linux to not try to use the debug hardware.
371
     */
372
    { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
373
      .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
374
    /* MMU Domain access control / MPU write buffer control */
375
    { .name = "DACR", .cp = 15,
376
      .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
377
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
378
      .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
379
    { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
380
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
381
      .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
382
    { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
383
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_context),
384
      .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
385
    /* ??? This covers not just the impdef TLB lockdown registers but also
386
     * some v7VMSA registers relating to TEX remap, so it is overly broad.
387
     */
388
    { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
389
      .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
390
    /* MMU TLB control. Note that the wildcarding means we cover not just
391
     * the unified TLB ops but also the dside/iside/inner-shareable variants.
392
     */
393
    { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
394
      .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
395
      .type = ARM_CP_NO_MIGRATE },
396
    { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
397
      .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
398
      .type = ARM_CP_NO_MIGRATE },
399
    { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
400
      .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
401
      .type = ARM_CP_NO_MIGRATE },
402
    { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
403
      .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
404
      .type = ARM_CP_NO_MIGRATE },
405
    /* Cache maintenance ops; some of this space may be overridden later. */
406
    { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
407
      .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
408
      .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
409
    REGINFO_SENTINEL
410
};
411

    
412
static const ARMCPRegInfo not_v6_cp_reginfo[] = {
413
    /* Not all pre-v6 cores implemented this WFI, so this is slightly
414
     * over-broad.
415
     */
416
    { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
417
      .access = PL1_W, .type = ARM_CP_WFI },
418
    REGINFO_SENTINEL
419
};
420

    
421
static const ARMCPRegInfo not_v7_cp_reginfo[] = {
422
    /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
423
     * is UNPREDICTABLE; we choose to NOP as most implementations do).
424
     */
425
    { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
426
      .access = PL1_W, .type = ARM_CP_WFI },
427
    /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
428
     * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
429
     * OMAPCP will override this space.
430
     */
431
    { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
432
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
433
      .resetvalue = 0 },
434
    { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
435
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
436
      .resetvalue = 0 },
437
    /* v6 doesn't have the cache ID registers but Linux reads them anyway */
438
    { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
439
      .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
440
      .resetvalue = 0 },
441
    REGINFO_SENTINEL
442
};
443

    
444
static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
445
                        uint64_t value)
446
{
447
    if (env->cp15.c1_coproc != value) {
448
        env->cp15.c1_coproc = value;
449
        /* ??? Is this safe when called from within a TB?  */
450
        tb_flush(env);
451
    }
452
}
453

    
454
static const ARMCPRegInfo v6_cp_reginfo[] = {
455
    /* prefetch by MVA in v6, NOP in v7 */
456
    { .name = "MVA_prefetch",
457
      .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
458
      .access = PL1_W, .type = ARM_CP_NOP },
459
    { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
460
      .access = PL0_W, .type = ARM_CP_NOP },
461
    { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
462
      .access = PL0_W, .type = ARM_CP_NOP },
463
    { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
464
      .access = PL0_W, .type = ARM_CP_NOP },
465
    { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
466
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
467
      .resetvalue = 0, },
468
    /* Watchpoint Fault Address Register : should actually only be present
469
     * for 1136, 1176, 11MPCore.
470
     */
471
    { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
472
      .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
473
    { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
474
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
475
      .resetvalue = 0, .writefn = cpacr_write },
476
    REGINFO_SENTINEL
477
};
478

    
479
static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
480
{
481
    /* Perfomance monitor registers user accessibility is controlled
482
     * by PMUSERENR.
483
     */
484
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
485
        return CP_ACCESS_TRAP;
486
    }
487
    return CP_ACCESS_OK;
488
}
489

    
490
static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
491
                       uint64_t value)
492
{
493
    /* only the DP, X, D and E bits are writable */
494
    env->cp15.c9_pmcr &= ~0x39;
495
    env->cp15.c9_pmcr |= (value & 0x39);
496
}
497

    
498
static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
499
                            uint64_t value)
500
{
501
    value &= (1 << 31);
502
    env->cp15.c9_pmcnten |= value;
503
}
504

    
505
static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
506
                             uint64_t value)
507
{
508
    value &= (1 << 31);
509
    env->cp15.c9_pmcnten &= ~value;
510
}
511

    
512
static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
513
                         uint64_t value)
514
{
515
    env->cp15.c9_pmovsr &= ~value;
516
}
517

    
518
static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
519
                             uint64_t value)
520
{
521
    env->cp15.c9_pmxevtyper = value & 0xff;
522
}
523

    
524
static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
525
                            uint64_t value)
526
{
527
    env->cp15.c9_pmuserenr = value & 1;
528
}
529

    
530
static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
531
                             uint64_t value)
532
{
533
    /* We have no event counters so only the C bit can be changed */
534
    value &= (1 << 31);
535
    env->cp15.c9_pminten |= value;
536
}
537

    
538
static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
539
                             uint64_t value)
540
{
541
    value &= (1 << 31);
542
    env->cp15.c9_pminten &= ~value;
543
}
544

    
545
static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
546
                       uint64_t value)
547
{
548
    env->cp15.c12_vbar = value & ~0x1Ful;
549
}
550

    
551
static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
552
{
553
    ARMCPU *cpu = arm_env_get_cpu(env);
554
    return cpu->ccsidr[env->cp15.c0_cssel];
555
}
556

    
557
static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
558
                         uint64_t value)
559
{
560
    env->cp15.c0_cssel = value & 0xf;
561
}
562

    
563
static const ARMCPRegInfo v7_cp_reginfo[] = {
564
    /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
565
     * debug components
566
     */
567
    { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
568
      .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
569
    { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
570
      .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
571
    /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
572
    { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
573
      .access = PL1_W, .type = ARM_CP_NOP },
574
    /* Performance monitors are implementation defined in v7,
575
     * but with an ARM recommended set of registers, which we
576
     * follow (although we don't actually implement any counters)
577
     *
578
     * Performance registers fall into three categories:
579
     *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
580
     *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
581
     *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
582
     * For the cases controlled by PMUSERENR we must set .access to PL0_RW
583
     * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
584
     */
585
    { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
586
      .access = PL0_RW, .resetvalue = 0,
587
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
588
      .writefn = pmcntenset_write,
589
      .accessfn = pmreg_access,
590
      .raw_writefn = raw_write },
591
    { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
592
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
593
      .accessfn = pmreg_access,
594
      .writefn = pmcntenclr_write,
595
      .type = ARM_CP_NO_MIGRATE },
596
    { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
597
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
598
      .accessfn = pmreg_access,
599
      .writefn = pmovsr_write,
600
      .raw_writefn = raw_write },
601
    /* Unimplemented so WI. */
602
    { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
603
      .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
604
    /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
605
     * We choose to RAZ/WI.
606
     */
607
    { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
608
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
609
      .accessfn = pmreg_access },
610
    /* Unimplemented, RAZ/WI. */
611
    { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
612
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
613
      .accessfn = pmreg_access },
614
    { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
615
      .access = PL0_RW,
616
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
617
      .accessfn = pmreg_access, .writefn = pmxevtyper_write,
618
      .raw_writefn = raw_write },
619
    /* Unimplemented, RAZ/WI. */
620
    { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
621
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
622
      .accessfn = pmreg_access },
623
    { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
624
      .access = PL0_R | PL1_RW,
625
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
626
      .resetvalue = 0,
627
      .writefn = pmuserenr_write, .raw_writefn = raw_write },
628
    { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
629
      .access = PL1_RW,
630
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
631
      .resetvalue = 0,
632
      .writefn = pmintenset_write, .raw_writefn = raw_write },
633
    { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
634
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
635
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
636
      .resetvalue = 0, .writefn = pmintenclr_write, },
637
    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
638
      .access = PL1_RW, .writefn = vbar_write,
639
      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
640
      .resetvalue = 0 },
641
    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
642
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
643
      .resetvalue = 0, },
644
    { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
645
      .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
646
    { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
647
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
648
      .writefn = csselr_write, .resetvalue = 0 },
649
    /* Auxiliary ID register: this actually has an IMPDEF value but for now
650
     * just RAZ for all cores:
651
     */
652
    { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7,
653
      .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
654
    REGINFO_SENTINEL
655
};
656

    
657
static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
658
                        uint64_t value)
659
{
660
    value &= 1;
661
    env->teecr = value;
662
}
663

    
664
static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
665
{
666
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
667
        return CP_ACCESS_TRAP;
668
    }
669
    return CP_ACCESS_OK;
670
}
671

    
672
static const ARMCPRegInfo t2ee_cp_reginfo[] = {
673
    { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
674
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
675
      .resetvalue = 0,
676
      .writefn = teecr_write },
677
    { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
678
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
679
      .accessfn = teehbr_access, .resetvalue = 0 },
680
    REGINFO_SENTINEL
681
};
682

    
683
static const ARMCPRegInfo v6k_cp_reginfo[] = {
684
    { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
685
      .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
686
      .access = PL0_RW,
687
      .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el0), .resetvalue = 0 },
688
    { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
689
      .access = PL0_RW,
690
      .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidr_el0),
691
      .resetfn = arm_cp_reset_ignore },
692
    { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
693
      .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
694
      .access = PL0_R|PL1_W,
695
      .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el0), .resetvalue = 0 },
696
    { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
697
      .access = PL0_R|PL1_W,
698
      .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidrro_el0),
699
      .resetfn = arm_cp_reset_ignore },
700
    { .name = "TPIDR_EL1", .state = ARM_CP_STATE_BOTH,
701
      .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
702
      .access = PL1_RW,
703
      .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el1), .resetvalue = 0 },
704
    REGINFO_SENTINEL
705
};
706

    
707
#ifndef CONFIG_USER_ONLY
708

    
709
static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
710
{
711
    /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
712
    if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
713
        return CP_ACCESS_TRAP;
714
    }
715
    return CP_ACCESS_OK;
716
}
717

    
718
static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
719
{
720
    /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
721
    if (arm_current_pl(env) == 0 &&
722
        !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
723
        return CP_ACCESS_TRAP;
724
    }
725
    return CP_ACCESS_OK;
726
}
727

    
728
static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
729
{
730
    /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
731
     * EL0[PV]TEN is zero.
732
     */
733
    if (arm_current_pl(env) == 0 &&
734
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
735
        return CP_ACCESS_TRAP;
736
    }
737
    return CP_ACCESS_OK;
738
}
739

    
740
static CPAccessResult gt_pct_access(CPUARMState *env,
741
                                         const ARMCPRegInfo *ri)
742
{
743
    return gt_counter_access(env, GTIMER_PHYS);
744
}
745

    
746
static CPAccessResult gt_vct_access(CPUARMState *env,
747
                                         const ARMCPRegInfo *ri)
748
{
749
    return gt_counter_access(env, GTIMER_VIRT);
750
}
751

    
752
static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
753
{
754
    return gt_timer_access(env, GTIMER_PHYS);
755
}
756

    
757
static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
758
{
759
    return gt_timer_access(env, GTIMER_VIRT);
760
}
761

    
762
static uint64_t gt_get_countervalue(CPUARMState *env)
763
{
764
    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
765
}
766

    
767
static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
768
{
769
    ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
770

    
771
    if (gt->ctl & 1) {
772
        /* Timer enabled: calculate and set current ISTATUS, irq, and
773
         * reset timer to when ISTATUS next has to change
774
         */
775
        uint64_t count = gt_get_countervalue(&cpu->env);
776
        /* Note that this must be unsigned 64 bit arithmetic: */
777
        int istatus = count >= gt->cval;
778
        uint64_t nexttick;
779

    
780
        gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
781
        qemu_set_irq(cpu->gt_timer_outputs[timeridx],
782
                     (istatus && !(gt->ctl & 2)));
783
        if (istatus) {
784
            /* Next transition is when count rolls back over to zero */
785
            nexttick = UINT64_MAX;
786
        } else {
787
            /* Next transition is when we hit cval */
788
            nexttick = gt->cval;
789
        }
790
        /* Note that the desired next expiry time might be beyond the
791
         * signed-64-bit range of a QEMUTimer -- in this case we just
792
         * set the timer for as far in the future as possible. When the
793
         * timer expires we will reset the timer for any remaining period.
794
         */
795
        if (nexttick > INT64_MAX / GTIMER_SCALE) {
796
            nexttick = INT64_MAX / GTIMER_SCALE;
797
        }
798
        timer_mod(cpu->gt_timer[timeridx], nexttick);
799
    } else {
800
        /* Timer disabled: ISTATUS and timer output always clear */
801
        gt->ctl &= ~4;
802
        qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
803
        timer_del(cpu->gt_timer[timeridx]);
804
    }
805
}
806

    
807
static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
808
{
809
    ARMCPU *cpu = arm_env_get_cpu(env);
810
    int timeridx = ri->opc1 & 1;
811

    
812
    timer_del(cpu->gt_timer[timeridx]);
813
}
814

    
815
static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
816
{
817
    return gt_get_countervalue(env);
818
}
819

    
820
static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
821
                          uint64_t value)
822
{
823
    int timeridx = ri->opc1 & 1;
824

    
825
    env->cp15.c14_timer[timeridx].cval = value;
826
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
827
}
828

    
829
static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
830
{
831
    int timeridx = ri->crm & 1;
832

    
833
    return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
834
                      gt_get_countervalue(env));
835
}
836

    
837
static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
838
                          uint64_t value)
839
{
840
    int timeridx = ri->crm & 1;
841

    
842
    env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
843
        + sextract64(value, 0, 32);
844
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
845
}
846

    
847
static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
848
                         uint64_t value)
849
{
850
    ARMCPU *cpu = arm_env_get_cpu(env);
851
    int timeridx = ri->crm & 1;
852
    uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
853

    
854
    env->cp15.c14_timer[timeridx].ctl = value & 3;
855
    if ((oldval ^ value) & 1) {
856
        /* Enable toggled */
857
        gt_recalc_timer(cpu, timeridx);
858
    } else if ((oldval & value) & 2) {
859
        /* IMASK toggled: don't need to recalculate,
860
         * just set the interrupt line based on ISTATUS
861
         */
862
        qemu_set_irq(cpu->gt_timer_outputs[timeridx],
863
                     (oldval & 4) && (value & 2));
864
    }
865
}
866

    
867
void arm_gt_ptimer_cb(void *opaque)
868
{
869
    ARMCPU *cpu = opaque;
870

    
871
    gt_recalc_timer(cpu, GTIMER_PHYS);
872
}
873

    
874
void arm_gt_vtimer_cb(void *opaque)
875
{
876
    ARMCPU *cpu = opaque;
877

    
878
    gt_recalc_timer(cpu, GTIMER_VIRT);
879
}
880

    
881
static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
882
    /* Note that CNTFRQ is purely reads-as-written for the benefit
883
     * of software; writing it doesn't actually change the timer frequency.
884
     * Our reset value matches the fixed frequency we implement the timer at.
885
     */
886
    { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
887
      .access = PL1_RW | PL0_R,
888
      .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
889
      .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
890
      .accessfn = gt_cntfrq_access,
891
    },
892
    /* overall control: mostly access permissions */
893
    { .name = "CNTKCTL", .cp = 15, .crn = 14, .crm = 1, .opc1 = 0, .opc2 = 0,
894
      .access = PL1_RW,
895
      .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
896
      .resetvalue = 0,
897
    },
898
    /* per-timer control */
899
    { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
900
      .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
901
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
902
      .resetvalue = 0,
903
      .accessfn = gt_ptimer_access,
904
      .writefn = gt_ctl_write, .raw_writefn = raw_write,
905
    },
906
    { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
907
      .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
908
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
909
      .resetvalue = 0,
910
      .accessfn = gt_vtimer_access,
911
      .writefn = gt_ctl_write, .raw_writefn = raw_write,
912
    },
913
    /* TimerValue views: a 32 bit downcounting view of the underlying state */
914
    { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
915
      .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
916
      .accessfn = gt_ptimer_access,
917
      .readfn = gt_tval_read, .writefn = gt_tval_write,
918
    },
919
    { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
920
      .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
921
      .accessfn = gt_vtimer_access,
922
      .readfn = gt_tval_read, .writefn = gt_tval_write,
923
    },
924
    /* The counter itself */
925
    { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
926
      .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
927
      .accessfn = gt_pct_access,
928
      .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
929
    },
930
    { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
931
      .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
932
      .accessfn = gt_vct_access,
933
      .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
934
    },
935
    /* Comparison value, indicating when the timer goes off */
936
    { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
937
      .access = PL1_RW | PL0_R,
938
      .type = ARM_CP_64BIT | ARM_CP_IO,
939
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
940
      .resetvalue = 0,
941
      .accessfn = gt_ptimer_access,
942
      .writefn = gt_cval_write, .raw_writefn = raw_write,
943
    },
944
    { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
945
      .access = PL1_RW | PL0_R,
946
      .type = ARM_CP_64BIT | ARM_CP_IO,
947
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
948
      .resetvalue = 0,
949
      .accessfn = gt_vtimer_access,
950
      .writefn = gt_cval_write, .raw_writefn = raw_write,
951
    },
952
    REGINFO_SENTINEL
953
};
954

    
955
#else
956
/* In user-mode none of the generic timer registers are accessible,
957
 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
958
 * so instead just don't register any of them.
959
 */
960
static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
961
    REGINFO_SENTINEL
962
};
963

    
964
#endif
965

    
966
static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
967
{
968
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
969
        env->cp15.c7_par = value;
970
    } else if (arm_feature(env, ARM_FEATURE_V7)) {
971
        env->cp15.c7_par = value & 0xfffff6ff;
972
    } else {
973
        env->cp15.c7_par = value & 0xfffff1ff;
974
    }
975
}
976

    
977
#ifndef CONFIG_USER_ONLY
978
/* get_phys_addr() isn't present for user-mode-only targets */
979

    
980
/* Return true if extended addresses are enabled, ie this is an
981
 * LPAE implementation and we are using the long-descriptor translation
982
 * table format because the TTBCR EAE bit is set.
983
 */
984
static inline bool extended_addresses_enabled(CPUARMState *env)
985
{
986
    return arm_feature(env, ARM_FEATURE_LPAE)
987
        && (env->cp15.c2_control & (1U << 31));
988
}
989

    
990
static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
991
{
992
    if (ri->opc2 & 4) {
993
        /* Other states are only available with TrustZone; in
994
         * a non-TZ implementation these registers don't exist
995
         * at all, which is an Uncategorized trap. This underdecoding
996
         * is safe because the reginfo is NO_MIGRATE.
997
         */
998
        return CP_ACCESS_TRAP_UNCATEGORIZED;
999
    }
1000
    return CP_ACCESS_OK;
1001
}
1002

    
1003
static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1004
{
1005
    hwaddr phys_addr;
1006
    target_ulong page_size;
1007
    int prot;
1008
    int ret, is_user = ri->opc2 & 2;
1009
    int access_type = ri->opc2 & 1;
1010

    
1011
    ret = get_phys_addr(env, value, access_type, is_user,
1012
                        &phys_addr, &prot, &page_size);
1013
    if (extended_addresses_enabled(env)) {
1014
        /* ret is a DFSR/IFSR value for the long descriptor
1015
         * translation table format, but with WnR always clear.
1016
         * Convert it to a 64-bit PAR.
1017
         */
1018
        uint64_t par64 = (1 << 11); /* LPAE bit always set */
1019
        if (ret == 0) {
1020
            par64 |= phys_addr & ~0xfffULL;
1021
            /* We don't set the ATTR or SH fields in the PAR. */
1022
        } else {
1023
            par64 |= 1; /* F */
1024
            par64 |= (ret & 0x3f) << 1; /* FS */
1025
            /* Note that S2WLK and FSTAGE are always zero, because we don't
1026
             * implement virtualization and therefore there can't be a stage 2
1027
             * fault.
1028
             */
1029
        }
1030
        env->cp15.c7_par = par64;
1031
        env->cp15.c7_par_hi = par64 >> 32;
1032
    } else {
1033
        /* ret is a DFSR/IFSR value for the short descriptor
1034
         * translation table format (with WnR always clear).
1035
         * Convert it to a 32-bit PAR.
1036
         */
1037
        if (ret == 0) {
1038
            /* We do not set any attribute bits in the PAR */
1039
            if (page_size == (1 << 24)
1040
                && arm_feature(env, ARM_FEATURE_V7)) {
1041
                env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1042
            } else {
1043
                env->cp15.c7_par = phys_addr & 0xfffff000;
1044
            }
1045
        } else {
1046
            env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1047
                ((ret & (12 << 1)) >> 6) |
1048
                ((ret & 0xf) << 1) | 1;
1049
        }
1050
        env->cp15.c7_par_hi = 0;
1051
    }
1052
}
1053
#endif
1054

    
1055
static const ARMCPRegInfo vapa_cp_reginfo[] = {
1056
    { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1057
      .access = PL1_RW, .resetvalue = 0,
1058
      .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
1059
      .writefn = par_write },
1060
#ifndef CONFIG_USER_ONLY
1061
    { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1062
      .access = PL1_W, .accessfn = ats_access,
1063
      .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1064
#endif
1065
    REGINFO_SENTINEL
1066
};
1067

    
1068
/* Return basic MPU access permission bits.  */
1069
static uint32_t simple_mpu_ap_bits(uint32_t val)
1070
{
1071
    uint32_t ret;
1072
    uint32_t mask;
1073
    int i;
1074
    ret = 0;
1075
    mask = 3;
1076
    for (i = 0; i < 16; i += 2) {
1077
        ret |= (val >> i) & mask;
1078
        mask <<= 2;
1079
    }
1080
    return ret;
1081
}
1082

    
1083
/* Pad basic MPU access permission bits to extended format.  */
1084
static uint32_t extended_mpu_ap_bits(uint32_t val)
1085
{
1086
    uint32_t ret;
1087
    uint32_t mask;
1088
    int i;
1089
    ret = 0;
1090
    mask = 3;
1091
    for (i = 0; i < 16; i += 2) {
1092
        ret |= (val & mask) << i;
1093
        mask <<= 2;
1094
    }
1095
    return ret;
1096
}
1097

    
1098
static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1099
                                 uint64_t value)
1100
{
1101
    env->cp15.c5_data = extended_mpu_ap_bits(value);
1102
}
1103

    
1104
static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1105
{
1106
    return simple_mpu_ap_bits(env->cp15.c5_data);
1107
}
1108

    
1109
static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1110
                                 uint64_t value)
1111
{
1112
    env->cp15.c5_insn = extended_mpu_ap_bits(value);
1113
}
1114

    
1115
static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1116
{
1117
    return simple_mpu_ap_bits(env->cp15.c5_insn);
1118
}
1119

    
1120
static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1121
    { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1122
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1123
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
1124
      .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1125
    { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1126
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1127
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
1128
      .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1129
    { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1130
      .access = PL1_RW,
1131
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1132
    { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1133
      .access = PL1_RW,
1134
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1135
    { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1136
      .access = PL1_RW,
1137
      .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1138
    { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1139
      .access = PL1_RW,
1140
      .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1141
    /* Protection region base and size registers */
1142
    { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
1143
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1144
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
1145
    { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
1146
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1147
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
1148
    { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
1149
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1150
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
1151
    { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
1152
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1153
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
1154
    { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
1155
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1156
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
1157
    { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
1158
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1159
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
1160
    { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
1161
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1162
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
1163
    { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
1164
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1165
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
1166
    REGINFO_SENTINEL
1167
};
1168

    
1169
static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1170
                                 uint64_t value)
1171
{
1172
    int maskshift = extract32(value, 0, 3);
1173

    
1174
    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {
1175
        value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1176
    } else {
1177
        value &= 7;
1178
    }
1179
    /* Note that we always calculate c2_mask and c2_base_mask, but
1180
     * they are only used for short-descriptor tables (ie if EAE is 0);
1181
     * for long-descriptor tables the TTBCR fields are used differently
1182
     * and the c2_mask and c2_base_mask values are meaningless.
1183
     */
1184
    env->cp15.c2_control = value;
1185
    env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1186
    env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
1187
}
1188

    
1189
static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1190
                             uint64_t value)
1191
{
1192
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1193
        /* With LPAE the TTBCR could result in a change of ASID
1194
         * via the TTBCR.A1 bit, so do a TLB flush.
1195
         */
1196
        tlb_flush(env, 1);
1197
    }
1198
    vmsa_ttbcr_raw_write(env, ri, value);
1199
}
1200

    
1201
static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1202
{
1203
    env->cp15.c2_base_mask = 0xffffc000u;
1204
    env->cp15.c2_control = 0;
1205
    env->cp15.c2_mask = 0;
1206
}
1207

    
1208
static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1209
    { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1210
      .access = PL1_RW,
1211
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1212
    { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1213
      .access = PL1_RW,
1214
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1215
    { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1216
      .access = PL1_RW,
1217
      .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
1218
    { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1219
      .access = PL1_RW,
1220
      .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
1221
    { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1222
      .access = PL1_RW, .writefn = vmsa_ttbcr_write,
1223
      .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
1224
      .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
1225
    { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1226
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
1227
      .resetvalue = 0, },
1228
    REGINFO_SENTINEL
1229
};
1230

    
1231
static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1232
                                uint64_t value)
1233
{
1234
    env->cp15.c15_ticonfig = value & 0xe7;
1235
    /* The OS_TYPE bit in this register changes the reported CPUID! */
1236
    env->cp15.c0_cpuid = (value & (1 << 5)) ?
1237
        ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1238
}
1239

    
1240
static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1241
                                uint64_t value)
1242
{
1243
    env->cp15.c15_threadid = value & 0xffff;
1244
}
1245

    
1246
static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1247
                           uint64_t value)
1248
{
1249
    /* Wait-for-interrupt (deprecated) */
1250
    cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1251
}
1252

    
1253
static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1254
                                  uint64_t value)
1255
{
1256
    /* On OMAP there are registers indicating the max/min index of dcache lines
1257
     * containing a dirty line; cache flush operations have to reset these.
1258
     */
1259
    env->cp15.c15_i_max = 0x000;
1260
    env->cp15.c15_i_min = 0xff0;
1261
}
1262

    
1263
static const ARMCPRegInfo omap_cp_reginfo[] = {
1264
    { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1265
      .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1266
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1267
    { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1268
      .access = PL1_RW, .type = ARM_CP_NOP },
1269
    { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1270
      .access = PL1_RW,
1271
      .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1272
      .writefn = omap_ticonfig_write },
1273
    { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1274
      .access = PL1_RW,
1275
      .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1276
    { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1277
      .access = PL1_RW, .resetvalue = 0xff0,
1278
      .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1279
    { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1280
      .access = PL1_RW,
1281
      .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1282
      .writefn = omap_threadid_write },
1283
    { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1284
      .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1285
      .type = ARM_CP_NO_MIGRATE,
1286
      .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1287
    /* TODO: Peripheral port remap register:
1288
     * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1289
     * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1290
     * when MMU is off.
1291
     */
1292
    { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
1293
      .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1294
      .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
1295
      .writefn = omap_cachemaint_write },
1296
    { .name = "C9", .cp = 15, .crn = 9,
1297
      .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1298
      .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1299
    REGINFO_SENTINEL
1300
};
1301

    
1302
static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1303
                              uint64_t value)
1304
{
1305
    value &= 0x3fff;
1306
    if (env->cp15.c15_cpar != value) {
1307
        /* Changes cp0 to cp13 behavior, so needs a TB flush.  */
1308
        tb_flush(env);
1309
        env->cp15.c15_cpar = value;
1310
    }
1311
}
1312

    
1313
static const ARMCPRegInfo xscale_cp_reginfo[] = {
1314
    { .name = "XSCALE_CPAR",
1315
      .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1316
      .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1317
      .writefn = xscale_cpar_write, },
1318
    { .name = "XSCALE_AUXCR",
1319
      .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1320
      .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1321
      .resetvalue = 0, },
1322
    REGINFO_SENTINEL
1323
};
1324

    
1325
static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1326
    /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1327
     * implementation of this implementation-defined space.
1328
     * Ideally this should eventually disappear in favour of actually
1329
     * implementing the correct behaviour for all cores.
1330
     */
1331
    { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1332
      .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1333
      .access = PL1_RW,
1334
      .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE,
1335
      .resetvalue = 0 },
1336
    REGINFO_SENTINEL
1337
};
1338

    
1339
static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1340
    /* Cache status: RAZ because we have no cache so it's always clean */
1341
    { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1342
      .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1343
      .resetvalue = 0 },
1344
    REGINFO_SENTINEL
1345
};
1346

    
1347
static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1348
    /* We never have a a block transfer operation in progress */
1349
    { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
1350
      .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1351
      .resetvalue = 0 },
1352
    /* The cache ops themselves: these all NOP for QEMU */
1353
    { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1354
      .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1355
    { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1356
      .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1357
    { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1358
      .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1359
    { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1360
      .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1361
    { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1362
      .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1363
    { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1364
      .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1365
    REGINFO_SENTINEL
1366
};
1367

    
1368
static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1369
    /* The cache test-and-clean instructions always return (1 << 30)
1370
     * to indicate that there are no dirty cache lines.
1371
     */
1372
    { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
1373
      .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1374
      .resetvalue = (1 << 30) },
1375
    { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
1376
      .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1377
      .resetvalue = (1 << 30) },
1378
    REGINFO_SENTINEL
1379
};
1380

    
1381
static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1382
    /* Ignore ReadBuffer accesses */
1383
    { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1384
      .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1385
      .access = PL1_RW, .resetvalue = 0,
1386
      .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
1387
    REGINFO_SENTINEL
1388
};
1389

    
1390
static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1391
{
1392
    CPUState *cs = CPU(arm_env_get_cpu(env));
1393
    uint32_t mpidr = cs->cpu_index;
1394
    /* We don't support setting cluster ID ([8..11])
1395
     * so these bits always RAZ.
1396
     */
1397
    if (arm_feature(env, ARM_FEATURE_V7MP)) {
1398
        mpidr |= (1U << 31);
1399
        /* Cores which are uniprocessor (non-coherent)
1400
         * but still implement the MP extensions set
1401
         * bit 30. (For instance, A9UP.) However we do
1402
         * not currently model any of those cores.
1403
         */
1404
    }
1405
    return mpidr;
1406
}
1407

    
1408
static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1409
    { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1410
      .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1411
    REGINFO_SENTINEL
1412
};
1413

    
1414
static uint64_t par64_read(CPUARMState *env, const ARMCPRegInfo *ri)
1415
{
1416
    return ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1417
}
1418

    
1419
static void par64_write(CPUARMState *env, const ARMCPRegInfo *ri,
1420
                        uint64_t value)
1421
{
1422
    env->cp15.c7_par_hi = value >> 32;
1423
    env->cp15.c7_par = value;
1424
}
1425

    
1426
static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1427
{
1428
    env->cp15.c7_par_hi = 0;
1429
    env->cp15.c7_par = 0;
1430
}
1431

    
1432
static uint64_t ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri)
1433
{
1434
    return ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1435
}
1436

    
1437
static void ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1438
                              uint64_t value)
1439
{
1440
    env->cp15.c2_base0_hi = value >> 32;
1441
    env->cp15.c2_base0 = value;
1442
}
1443

    
1444
static void ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1445
                          uint64_t value)
1446
{
1447
    /* Writes to the 64 bit format TTBRs may change the ASID */
1448
    tlb_flush(env, 1);
1449
    ttbr064_raw_write(env, ri, value);
1450
}
1451

    
1452
static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1453
{
1454
    env->cp15.c2_base0_hi = 0;
1455
    env->cp15.c2_base0 = 0;
1456
}
1457

    
1458
static uint64_t ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri)
1459
{
1460
    return ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1461
}
1462

    
1463
static void ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1464
                          uint64_t value)
1465
{
1466
    env->cp15.c2_base1_hi = value >> 32;
1467
    env->cp15.c2_base1 = value;
1468
}
1469

    
1470
static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1471
{
1472
    env->cp15.c2_base1_hi = 0;
1473
    env->cp15.c2_base1 = 0;
1474
}
1475

    
1476
static const ARMCPRegInfo lpae_cp_reginfo[] = {
1477
    /* NOP AMAIR0/1: the override is because these clash with the rather
1478
     * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1479
     */
1480
    { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1481
      .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1482
      .resetvalue = 0 },
1483
    { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1484
      .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1485
      .resetvalue = 0 },
1486
    /* 64 bit access versions of the (dummy) debug registers */
1487
    { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
1488
      .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1489
    { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
1490
      .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
1491
    { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1492
      .access = PL1_RW, .type = ARM_CP_64BIT,
1493
      .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset },
1494
    { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1495
      .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read,
1496
      .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write,
1497
      .resetfn = ttbr064_reset },
1498
    { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1499
      .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read,
1500
      .writefn = ttbr164_write, .resetfn = ttbr164_reset },
1501
    REGINFO_SENTINEL
1502
};
1503

    
1504
static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1505
{
1506
    return vfp_get_fpcr(env);
1507
}
1508

    
1509
static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1510
                            uint64_t value)
1511
{
1512
    vfp_set_fpcr(env, value);
1513
}
1514

    
1515
static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1516
{
1517
    return vfp_get_fpsr(env);
1518
}
1519

    
1520
static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521
                            uint64_t value)
1522
{
1523
    vfp_set_fpsr(env, value);
1524
}
1525

    
1526
static const ARMCPRegInfo v8_cp_reginfo[] = {
1527
    /* Minimal set of EL0-visible registers. This will need to be expanded
1528
     * significantly for system emulation of AArch64 CPUs.
1529
     */
1530
    { .name = "NZCV", .state = ARM_CP_STATE_AA64,
1531
      .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
1532
      .access = PL0_RW, .type = ARM_CP_NZCV },
1533
    { .name = "FPCR", .state = ARM_CP_STATE_AA64,
1534
      .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
1535
      .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
1536
    { .name = "FPSR", .state = ARM_CP_STATE_AA64,
1537
      .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
1538
      .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
1539
    /* This claims a 32 byte cacheline size for icache and dcache, VIPT icache.
1540
     * It will eventually need to have a CPU-specified reset value.
1541
     */
1542
    { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
1543
      .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
1544
      .access = PL0_R, .type = ARM_CP_CONST,
1545
      .resetvalue = 0x80030003 },
1546
    /* Prohibit use of DC ZVA. OPTME: implement DC ZVA and allow its use.
1547
     * For system mode the DZP bit here will need to be computed, not constant.
1548
     */
1549
    { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
1550
      .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
1551
      .access = PL0_R, .type = ARM_CP_CONST,
1552
      .resetvalue = 0x10 },
1553
    REGINFO_SENTINEL
1554
};
1555

    
1556
static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1557
                        uint64_t value)
1558
{
1559
    env->cp15.c1_sys = value;
1560
    /* ??? Lots of these bits are not implemented.  */
1561
    /* This may enable/disable the MMU, so do a TLB flush.  */
1562
    tlb_flush(env, 1);
1563
}
1564

    
1565
void register_cp_regs_for_features(ARMCPU *cpu)
1566
{
1567
    /* Register all the coprocessor registers based on feature bits */
1568
    CPUARMState *env = &cpu->env;
1569
    if (arm_feature(env, ARM_FEATURE_M)) {
1570
        /* M profile has no coprocessor registers */
1571
        return;
1572
    }
1573

    
1574
    define_arm_cp_regs(cpu, cp_reginfo);
1575
    if (arm_feature(env, ARM_FEATURE_V6)) {
1576
        /* The ID registers all have impdef reset values */
1577
        ARMCPRegInfo v6_idregs[] = {
1578
            { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1,
1579
              .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1580
              .resetvalue = cpu->id_pfr0 },
1581
            { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1,
1582
              .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1583
              .resetvalue = cpu->id_pfr1 },
1584
            { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1,
1585
              .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1586
              .resetvalue = cpu->id_dfr0 },
1587
            { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1,
1588
              .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1589
              .resetvalue = cpu->id_afr0 },
1590
            { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1,
1591
              .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1592
              .resetvalue = cpu->id_mmfr0 },
1593
            { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1,
1594
              .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1595
              .resetvalue = cpu->id_mmfr1 },
1596
            { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1,
1597
              .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1598
              .resetvalue = cpu->id_mmfr2 },
1599
            { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1,
1600
              .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1601
              .resetvalue = cpu->id_mmfr3 },
1602
            { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2,
1603
              .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1604
              .resetvalue = cpu->id_isar0 },
1605
            { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2,
1606
              .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1607
              .resetvalue = cpu->id_isar1 },
1608
            { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2,
1609
              .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1610
              .resetvalue = cpu->id_isar2 },
1611
            { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2,
1612
              .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1613
              .resetvalue = cpu->id_isar3 },
1614
            { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2,
1615
              .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1616
              .resetvalue = cpu->id_isar4 },
1617
            { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2,
1618
              .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1619
              .resetvalue = cpu->id_isar5 },
1620
            /* 6..7 are as yet unallocated and must RAZ */
1621
            { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
1622
              .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1623
              .resetvalue = 0 },
1624
            { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
1625
              .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1626
              .resetvalue = 0 },
1627
            REGINFO_SENTINEL
1628
        };
1629
        define_arm_cp_regs(cpu, v6_idregs);
1630
        define_arm_cp_regs(cpu, v6_cp_reginfo);
1631
    } else {
1632
        define_arm_cp_regs(cpu, not_v6_cp_reginfo);
1633
    }
1634
    if (arm_feature(env, ARM_FEATURE_V6K)) {
1635
        define_arm_cp_regs(cpu, v6k_cp_reginfo);
1636
    }
1637
    if (arm_feature(env, ARM_FEATURE_V7)) {
1638
        /* v7 performance monitor control register: same implementor
1639
         * field as main ID register, and we implement no event counters.
1640
         */
1641
        ARMCPRegInfo pmcr = {
1642
            .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1643
            .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
1644
            .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
1645
            .accessfn = pmreg_access, .writefn = pmcr_write,
1646
            .raw_writefn = raw_write,
1647
        };
1648
        ARMCPRegInfo clidr = {
1649
            .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1650
            .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1651
        };
1652
        define_one_arm_cp_reg(cpu, &pmcr);
1653
        define_one_arm_cp_reg(cpu, &clidr);
1654
        define_arm_cp_regs(cpu, v7_cp_reginfo);
1655
    } else {
1656
        define_arm_cp_regs(cpu, not_v7_cp_reginfo);
1657
    }
1658
    if (arm_feature(env, ARM_FEATURE_V8)) {
1659
        define_arm_cp_regs(cpu, v8_cp_reginfo);
1660
    }
1661
    if (arm_feature(env, ARM_FEATURE_MPU)) {
1662
        /* These are the MPU registers prior to PMSAv6. Any new
1663
         * PMSA core later than the ARM946 will require that we
1664
         * implement the PMSAv6 or PMSAv7 registers, which are
1665
         * completely different.
1666
         */
1667
        assert(!arm_feature(env, ARM_FEATURE_V6));
1668
        define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
1669
    } else {
1670
        define_arm_cp_regs(cpu, vmsa_cp_reginfo);
1671
    }
1672
    if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
1673
        define_arm_cp_regs(cpu, t2ee_cp_reginfo);
1674
    }
1675
    if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1676
        define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
1677
    }
1678
    if (arm_feature(env, ARM_FEATURE_VAPA)) {
1679
        define_arm_cp_regs(cpu, vapa_cp_reginfo);
1680
    }
1681
    if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
1682
        define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
1683
    }
1684
    if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
1685
        define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
1686
    }
1687
    if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
1688
        define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
1689
    }
1690
    if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1691
        define_arm_cp_regs(cpu, omap_cp_reginfo);
1692
    }
1693
    if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
1694
        define_arm_cp_regs(cpu, strongarm_cp_reginfo);
1695
    }
1696
    if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1697
        define_arm_cp_regs(cpu, xscale_cp_reginfo);
1698
    }
1699
    if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
1700
        define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
1701
    }
1702
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1703
        define_arm_cp_regs(cpu, lpae_cp_reginfo);
1704
    }
1705
    /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1706
     * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1707
     * be read-only (ie write causes UNDEF exception).
1708
     */
1709
    {
1710
        ARMCPRegInfo id_cp_reginfo[] = {
1711
            /* Note that the MIDR isn't a simple constant register because
1712
             * of the TI925 behaviour where writes to another register can
1713
             * cause the MIDR value to change.
1714
             *
1715
             * Unimplemented registers in the c15 0 0 0 space default to
1716
             * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
1717
             * and friends override accordingly.
1718
             */
1719
            { .name = "MIDR",
1720
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
1721
              .access = PL1_R, .resetvalue = cpu->midr,
1722
              .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
1723
              .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1724
              .type = ARM_CP_OVERRIDE },
1725
            { .name = "CTR",
1726
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1727
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1728
            { .name = "TCMTR",
1729
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1730
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1731
            { .name = "TLBTR",
1732
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
1733
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1734
            /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
1735
            { .name = "DUMMY",
1736
              .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
1737
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1738
            { .name = "DUMMY",
1739
              .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
1740
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1741
            { .name = "DUMMY",
1742
              .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
1743
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1744
            { .name = "DUMMY",
1745
              .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
1746
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1747
            { .name = "DUMMY",
1748
              .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
1749
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1750
            REGINFO_SENTINEL
1751
        };
1752
        ARMCPRegInfo crn0_wi_reginfo = {
1753
            .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
1754
            .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
1755
            .type = ARM_CP_NOP | ARM_CP_OVERRIDE
1756
        };
1757
        if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
1758
            arm_feature(env, ARM_FEATURE_STRONGARM)) {
1759
            ARMCPRegInfo *r;
1760
            /* Register the blanket "writes ignored" value first to cover the
1761
             * whole space. Then update the specific ID registers to allow write
1762
             * access, so that they ignore writes rather than causing them to
1763
             * UNDEF.
1764
             */
1765
            define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
1766
            for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
1767
                r->access = PL1_RW;
1768
            }
1769
        }
1770
        define_arm_cp_regs(cpu, id_cp_reginfo);
1771
    }
1772

    
1773
    if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1774
        define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1775
    }
1776

    
1777
    if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1778
        ARMCPRegInfo auxcr = {
1779
            .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1780
            .access = PL1_RW, .type = ARM_CP_CONST,
1781
            .resetvalue = cpu->reset_auxcr
1782
        };
1783
        define_one_arm_cp_reg(cpu, &auxcr);
1784
    }
1785

    
1786
    if (arm_feature(env, ARM_FEATURE_CBAR)) {
1787
        ARMCPRegInfo cbar = {
1788
            .name = "CBAR", .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
1789
            .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
1790
            .fieldoffset = offsetof(CPUARMState, cp15.c15_config_base_address)
1791
        };
1792
        define_one_arm_cp_reg(cpu, &cbar);
1793
    }
1794

    
1795
    /* Generic registers whose values depend on the implementation */
1796
    {
1797
        ARMCPRegInfo sctlr = {
1798
            .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1799
            .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
1800
            .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1801
            .raw_writefn = raw_write,
1802
        };
1803
        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1804
            /* Normally we would always end the TB on an SCTLR write, but Linux
1805
             * arch/arm/mach-pxa/sleep.S expects two instructions following
1806
             * an MMU enable to execute from cache.  Imitate this behaviour.
1807
             */
1808
            sctlr.type |= ARM_CP_SUPPRESS_TB_END;
1809
        }
1810
        define_one_arm_cp_reg(cpu, &sctlr);
1811
    }
1812
}
1813

    
1814
ARMCPU *cpu_arm_init(const char *cpu_model)
1815
{
1816
    ARMCPU *cpu;
1817
    ObjectClass *oc;
1818

    
1819
    oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1820
    if (!oc) {
1821
        return NULL;
1822
    }
1823
    cpu = ARM_CPU(object_new(object_class_get_name(oc)));
1824

    
1825
    /* TODO this should be set centrally, once possible */
1826
    object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
1827

    
1828
    return cpu;
1829
}
1830

    
1831
void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1832
{
1833
    CPUState *cs = CPU(cpu);
1834
    CPUARMState *env = &cpu->env;
1835

    
1836
    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
1837
        gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
1838
                                 aarch64_fpu_gdb_set_reg,
1839
                                 34, "aarch64-fpu.xml", 0);
1840
    } else if (arm_feature(env, ARM_FEATURE_NEON)) {
1841
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1842
                                 51, "arm-neon.xml", 0);
1843
    } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
1844
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1845
                                 35, "arm-vfp3.xml", 0);
1846
    } else if (arm_feature(env, ARM_FEATURE_VFP)) {
1847
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1848
                                 19, "arm-vfp.xml", 0);
1849
    }
1850
}
1851

    
1852
/* Sort alphabetically by type name, except for "any". */
1853
static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
1854
{
1855
    ObjectClass *class_a = (ObjectClass *)a;
1856
    ObjectClass *class_b = (ObjectClass *)b;
1857
    const char *name_a, *name_b;
1858

    
1859
    name_a = object_class_get_name(class_a);
1860
    name_b = object_class_get_name(class_b);
1861
    if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
1862
        return 1;
1863
    } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
1864
        return -1;
1865
    } else {
1866
        return strcmp(name_a, name_b);
1867
    }
1868
}
1869

    
1870
static void arm_cpu_list_entry(gpointer data, gpointer user_data)
1871
{
1872
    ObjectClass *oc = data;
1873
    CPUListState *s = user_data;
1874
    const char *typename;
1875
    char *name;
1876

    
1877
    typename = object_class_get_name(oc);
1878
    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
1879
    (*s->cpu_fprintf)(s->file, "  %s\n",
1880
                      name);
1881
    g_free(name);
1882
}
1883

    
1884
void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1885
{
1886
    CPUListState s = {
1887
        .file = f,
1888
        .cpu_fprintf = cpu_fprintf,
1889
    };
1890
    GSList *list;
1891

    
1892
    list = object_class_get_list(TYPE_ARM_CPU, false);
1893
    list = g_slist_sort(list, arm_cpu_list_compare);
1894
    (*cpu_fprintf)(f, "Available CPUs:\n");
1895
    g_slist_foreach(list, arm_cpu_list_entry, &s);
1896
    g_slist_free(list);
1897
#ifdef CONFIG_KVM
1898
    /* The 'host' CPU type is dynamically registered only if KVM is
1899
     * enabled, so we have to special-case it here:
1900
     */
1901
    (*cpu_fprintf)(f, "  host (only available in KVM mode)\n");
1902
#endif
1903
}
1904

    
1905
static void arm_cpu_add_definition(gpointer data, gpointer user_data)
1906
{
1907
    ObjectClass *oc = data;
1908
    CpuDefinitionInfoList **cpu_list = user_data;
1909
    CpuDefinitionInfoList *entry;
1910
    CpuDefinitionInfo *info;
1911
    const char *typename;
1912

    
1913
    typename = object_class_get_name(oc);
1914
    info = g_malloc0(sizeof(*info));
1915
    info->name = g_strndup(typename,
1916
                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
1917

    
1918
    entry = g_malloc0(sizeof(*entry));
1919
    entry->value = info;
1920
    entry->next = *cpu_list;
1921
    *cpu_list = entry;
1922
}
1923

    
1924
CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1925
{
1926
    CpuDefinitionInfoList *cpu_list = NULL;
1927
    GSList *list;
1928

    
1929
    list = object_class_get_list(TYPE_ARM_CPU, false);
1930
    g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
1931
    g_slist_free(list);
1932

    
1933
    return cpu_list;
1934
}
1935

    
1936
static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
1937
                                   void *opaque, int state,
1938
                                   int crm, int opc1, int opc2)
1939
{
1940
    /* Private utility function for define_one_arm_cp_reg_with_opaque():
1941
     * add a single reginfo struct to the hash table.
1942
     */
1943
    uint32_t *key = g_new(uint32_t, 1);
1944
    ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1945
    int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
1946
    if (r->state == ARM_CP_STATE_BOTH && state == ARM_CP_STATE_AA32) {
1947
        /* The AArch32 view of a shared register sees the lower 32 bits
1948
         * of a 64 bit backing field. It is not migratable as the AArch64
1949
         * view handles that. AArch64 also handles reset.
1950
         * We assume it is a cp15 register.
1951
         */
1952
        r2->cp = 15;
1953
        r2->type |= ARM_CP_NO_MIGRATE;
1954
        r2->resetfn = arm_cp_reset_ignore;
1955
#ifdef HOST_WORDS_BIGENDIAN
1956
        if (r2->fieldoffset) {
1957
            r2->fieldoffset += sizeof(uint32_t);
1958
        }
1959
#endif
1960
    }
1961
    if (state == ARM_CP_STATE_AA64) {
1962
        /* To allow abbreviation of ARMCPRegInfo
1963
         * definitions, we treat cp == 0 as equivalent to
1964
         * the value for "standard guest-visible sysreg".
1965
         */
1966
        if (r->cp == 0) {
1967
            r2->cp = CP_REG_ARM64_SYSREG_CP;
1968
        }
1969
        *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
1970
                                  r2->opc0, opc1, opc2);
1971
    } else {
1972
        *key = ENCODE_CP_REG(r2->cp, is64, r2->crn, crm, opc1, opc2);
1973
    }
1974
    if (opaque) {
1975
        r2->opaque = opaque;
1976
    }
1977
    /* Make sure reginfo passed to helpers for wildcarded regs
1978
     * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1979
     */
1980
    r2->crm = crm;
1981
    r2->opc1 = opc1;
1982
    r2->opc2 = opc2;
1983
    /* By convention, for wildcarded registers only the first
1984
     * entry is used for migration; the others are marked as
1985
     * NO_MIGRATE so we don't try to transfer the register
1986
     * multiple times. Special registers (ie NOP/WFI) are
1987
     * never migratable.
1988
     */
1989
    if ((r->type & ARM_CP_SPECIAL) ||
1990
        ((r->crm == CP_ANY) && crm != 0) ||
1991
        ((r->opc1 == CP_ANY) && opc1 != 0) ||
1992
        ((r->opc2 == CP_ANY) && opc2 != 0)) {
1993
        r2->type |= ARM_CP_NO_MIGRATE;
1994
    }
1995

    
1996
    /* Overriding of an existing definition must be explicitly
1997
     * requested.
1998
     */
1999
    if (!(r->type & ARM_CP_OVERRIDE)) {
2000
        ARMCPRegInfo *oldreg;
2001
        oldreg = g_hash_table_lookup(cpu->cp_regs, key);
2002
        if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
2003
            fprintf(stderr, "Register redefined: cp=%d %d bit "
2004
                    "crn=%d crm=%d opc1=%d opc2=%d, "
2005
                    "was %s, now %s\n", r2->cp, 32 + 32 * is64,
2006
                    r2->crn, r2->crm, r2->opc1, r2->opc2,
2007
                    oldreg->name, r2->name);
2008
            g_assert_not_reached();
2009
        }
2010
    }
2011
    g_hash_table_insert(cpu->cp_regs, key, r2);
2012
}
2013

    
2014

    
2015
void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
2016
                                       const ARMCPRegInfo *r, void *opaque)
2017
{
2018
    /* Define implementations of coprocessor registers.
2019
     * We store these in a hashtable because typically
2020
     * there are less than 150 registers in a space which
2021
     * is 16*16*16*8*8 = 262144 in size.
2022
     * Wildcarding is supported for the crm, opc1 and opc2 fields.
2023
     * If a register is defined twice then the second definition is
2024
     * used, so this can be used to define some generic registers and
2025
     * then override them with implementation specific variations.
2026
     * At least one of the original and the second definition should
2027
     * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
2028
     * against accidental use.
2029
     *
2030
     * The state field defines whether the register is to be
2031
     * visible in the AArch32 or AArch64 execution state. If the
2032
     * state is set to ARM_CP_STATE_BOTH then we synthesise a
2033
     * reginfo structure for the AArch32 view, which sees the lower
2034
     * 32 bits of the 64 bit register.
2035
     *
2036
     * Only registers visible in AArch64 may set r->opc0; opc0 cannot
2037
     * be wildcarded. AArch64 registers are always considered to be 64
2038
     * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
2039
     * the register, if any.
2040
     */
2041
    int crm, opc1, opc2, state;
2042
    int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
2043
    int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
2044
    int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
2045
    int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
2046
    int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
2047
    int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
2048
    /* 64 bit registers have only CRm and Opc1 fields */
2049
    assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
2050
    /* op0 only exists in the AArch64 encodings */
2051
    assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
2052
    /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
2053
    assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
2054
    /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
2055
     * encodes a minimum access level for the register. We roll this
2056
     * runtime check into our general permission check code, so check
2057
     * here that the reginfo's specified permissions are strict enough
2058
     * to encompass the generic architectural permission check.
2059
     */
2060
    if (r->state != ARM_CP_STATE_AA32) {
2061
        int mask = 0;
2062
        switch (r->opc1) {
2063
        case 0: case 1: case 2:
2064
            /* min_EL EL1 */
2065
            mask = PL1_RW;
2066
            break;
2067
        case 3:
2068
            /* min_EL EL0 */
2069
            mask = PL0_RW;
2070
            break;
2071
        case 4:
2072
            /* min_EL EL2 */
2073
            mask = PL2_RW;
2074
            break;
2075
        case 5:
2076
            /* unallocated encoding, so not possible */
2077
            assert(false);
2078
            break;
2079
        case 6:
2080
            /* min_EL EL3 */
2081
            mask = PL3_RW;
2082
            break;
2083
        case 7:
2084
            /* min_EL EL1, secure mode only (we don't check the latter) */
2085
            mask = PL1_RW;
2086
            break;
2087
        default:
2088
            /* broken reginfo with out-of-range opc1 */
2089
            assert(false);
2090
            break;
2091
        }
2092
        /* assert our permissions are not too lax (stricter is fine) */
2093
        assert((r->access & ~mask) == 0);
2094
    }
2095

    
2096
    /* Check that the register definition has enough info to handle
2097
     * reads and writes if they are permitted.
2098
     */
2099
    if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
2100
        if (r->access & PL3_R) {
2101
            assert(r->fieldoffset || r->readfn);
2102
        }
2103
        if (r->access & PL3_W) {
2104
            assert(r->fieldoffset || r->writefn);
2105
        }
2106
    }
2107
    /* Bad type field probably means missing sentinel at end of reg list */
2108
    assert(cptype_valid(r->type));
2109
    for (crm = crmmin; crm <= crmmax; crm++) {
2110
        for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
2111
            for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
2112
                for (state = ARM_CP_STATE_AA32;
2113
                     state <= ARM_CP_STATE_AA64; state++) {
2114
                    if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
2115
                        continue;
2116
                    }
2117
                    add_cpreg_to_hashtable(cpu, r, opaque, state,
2118
                                           crm, opc1, opc2);
2119
                }
2120
            }
2121
        }
2122
    }
2123
}
2124

    
2125
void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
2126
                                    const ARMCPRegInfo *regs, void *opaque)
2127
{
2128
    /* Define a whole list of registers */
2129
    const ARMCPRegInfo *r;
2130
    for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
2131
        define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
2132
    }
2133
}
2134

    
2135
const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
2136
{
2137
    return g_hash_table_lookup(cpregs, &encoded_cp);
2138
}
2139

    
2140
void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
2141
                         uint64_t value)
2142
{
2143
    /* Helper coprocessor write function for write-ignore registers */
2144
}
2145

    
2146
uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
2147
{
2148
    /* Helper coprocessor write function for read-as-zero registers */
2149
    return 0;
2150
}
2151

    
2152
void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
2153
{
2154
    /* Helper coprocessor reset function for do-nothing-on-reset registers */
2155
}
2156

    
2157
static int bad_mode_switch(CPUARMState *env, int mode)
2158
{
2159
    /* Return true if it is not valid for us to switch to
2160
     * this CPU mode (ie all the UNPREDICTABLE cases in
2161
     * the ARM ARM CPSRWriteByInstr pseudocode).
2162
     */
2163
    switch (mode) {
2164
    case ARM_CPU_MODE_USR:
2165
    case ARM_CPU_MODE_SYS:
2166
    case ARM_CPU_MODE_SVC:
2167
    case ARM_CPU_MODE_ABT:
2168
    case ARM_CPU_MODE_UND:
2169
    case ARM_CPU_MODE_IRQ:
2170
    case ARM_CPU_MODE_FIQ:
2171
        return 0;
2172
    default:
2173
        return 1;
2174
    }
2175
}
2176

    
2177
uint32_t cpsr_read(CPUARMState *env)
2178
{
2179
    int ZF;
2180
    ZF = (env->ZF == 0);
2181
    return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2182
        (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2183
        | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2184
        | ((env->condexec_bits & 0xfc) << 8)
2185
        | (env->GE << 16);
2186
}
2187

    
2188
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2189
{
2190
    if (mask & CPSR_NZCV) {
2191
        env->ZF = (~val) & CPSR_Z;
2192
        env->NF = val;
2193
        env->CF = (val >> 29) & 1;
2194
        env->VF = (val << 3) & 0x80000000;
2195
    }
2196
    if (mask & CPSR_Q)
2197
        env->QF = ((val & CPSR_Q) != 0);
2198
    if (mask & CPSR_T)
2199
        env->thumb = ((val & CPSR_T) != 0);
2200
    if (mask & CPSR_IT_0_1) {
2201
        env->condexec_bits &= ~3;
2202
        env->condexec_bits |= (val >> 25) & 3;
2203
    }
2204
    if (mask & CPSR_IT_2_7) {
2205
        env->condexec_bits &= 3;
2206
        env->condexec_bits |= (val >> 8) & 0xfc;
2207
    }
2208
    if (mask & CPSR_GE) {
2209
        env->GE = (val >> 16) & 0xf;
2210
    }
2211

    
2212
    if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
2213
        if (bad_mode_switch(env, val & CPSR_M)) {
2214
            /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2215
             * We choose to ignore the attempt and leave the CPSR M field
2216
             * untouched.
2217
             */
2218
            mask &= ~CPSR_M;
2219
        } else {
2220
            switch_mode(env, val & CPSR_M);
2221
        }
2222
    }
2223
    mask &= ~CACHED_CPSR_BITS;
2224
    env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2225
}
2226

    
2227
/* Sign/zero extend */
2228
uint32_t HELPER(sxtb16)(uint32_t x)
2229
{
2230
    uint32_t res;
2231
    res = (uint16_t)(int8_t)x;
2232
    res |= (uint32_t)(int8_t)(x >> 16) << 16;
2233
    return res;
2234
}
2235

    
2236
uint32_t HELPER(uxtb16)(uint32_t x)
2237
{
2238
    uint32_t res;
2239
    res = (uint16_t)(uint8_t)x;
2240
    res |= (uint32_t)(uint8_t)(x >> 16) << 16;
2241
    return res;
2242
}
2243

    
2244
uint32_t HELPER(clz)(uint32_t x)
2245
{
2246
    return clz32(x);
2247
}
2248

    
2249
int32_t HELPER(sdiv)(int32_t num, int32_t den)
2250
{
2251
    if (den == 0)
2252
      return 0;
2253
    if (num == INT_MIN && den == -1)
2254
      return INT_MIN;
2255
    return num / den;
2256
}
2257

    
2258
uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
2259
{
2260
    if (den == 0)
2261
      return 0;
2262
    return num / den;
2263
}
2264

    
2265
uint32_t HELPER(rbit)(uint32_t x)
2266
{
2267
    x =  ((x & 0xff000000) >> 24)
2268
       | ((x & 0x00ff0000) >> 8)
2269
       | ((x & 0x0000ff00) << 8)
2270
       | ((x & 0x000000ff) << 24);
2271
    x =  ((x & 0xf0f0f0f0) >> 4)
2272
       | ((x & 0x0f0f0f0f) << 4);
2273
    x =  ((x & 0x88888888) >> 3)
2274
       | ((x & 0x44444444) >> 1)
2275
       | ((x & 0x22222222) << 1)
2276
       | ((x & 0x11111111) << 3);
2277
    return x;
2278
}
2279

    
2280
#if defined(CONFIG_USER_ONLY)
2281

    
2282
void arm_cpu_do_interrupt(CPUState *cs)
2283
{
2284
    ARMCPU *cpu = ARM_CPU(cs);
2285
    CPUARMState *env = &cpu->env;
2286

    
2287
    env->exception_index = -1;
2288
}
2289

    
2290
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
2291
                              int mmu_idx)
2292
{
2293
    if (rw == 2) {
2294
        env->exception_index = EXCP_PREFETCH_ABORT;
2295
        env->cp15.c6_insn = address;
2296
    } else {
2297
        env->exception_index = EXCP_DATA_ABORT;
2298
        env->cp15.c6_data = address;
2299
    }
2300
    return 1;
2301
}
2302

    
2303
/* These should probably raise undefined insn exceptions.  */
2304
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2305
{
2306
    cpu_abort(env, "v7m_mrs %d\n", reg);
2307
}
2308

    
2309
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2310
{
2311
    cpu_abort(env, "v7m_mrs %d\n", reg);
2312
    return 0;
2313
}
2314

    
2315
void switch_mode(CPUARMState *env, int mode)
2316
{
2317
    if (mode != ARM_CPU_MODE_USR)
2318
        cpu_abort(env, "Tried to switch out of user mode\n");
2319
}
2320

    
2321
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2322
{
2323
    cpu_abort(env, "banked r13 write\n");
2324
}
2325

    
2326
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2327
{
2328
    cpu_abort(env, "banked r13 read\n");
2329
    return 0;
2330
}
2331

    
2332
#else
2333

    
2334
/* Map CPU modes onto saved register banks.  */
2335
int bank_number(int mode)
2336
{
2337
    switch (mode) {
2338
    case ARM_CPU_MODE_USR:
2339
    case ARM_CPU_MODE_SYS:
2340
        return 0;
2341
    case ARM_CPU_MODE_SVC:
2342
        return 1;
2343
    case ARM_CPU_MODE_ABT:
2344
        return 2;
2345
    case ARM_CPU_MODE_UND:
2346
        return 3;
2347
    case ARM_CPU_MODE_IRQ:
2348
        return 4;
2349
    case ARM_CPU_MODE_FIQ:
2350
        return 5;
2351
    }
2352
    hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
2353
}
2354

    
2355
void switch_mode(CPUARMState *env, int mode)
2356
{
2357
    int old_mode;
2358
    int i;
2359

    
2360
    old_mode = env->uncached_cpsr & CPSR_M;
2361
    if (mode == old_mode)
2362
        return;
2363

    
2364
    if (old_mode == ARM_CPU_MODE_FIQ) {
2365
        memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
2366
        memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
2367
    } else if (mode == ARM_CPU_MODE_FIQ) {
2368
        memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
2369
        memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
2370
    }
2371

    
2372
    i = bank_number(old_mode);
2373
    env->banked_r13[i] = env->regs[13];
2374
    env->banked_r14[i] = env->regs[14];
2375
    env->banked_spsr[i] = env->spsr;
2376

    
2377
    i = bank_number(mode);
2378
    env->regs[13] = env->banked_r13[i];
2379
    env->regs[14] = env->banked_r14[i];
2380
    env->spsr = env->banked_spsr[i];
2381
}
2382

    
2383
static void v7m_push(CPUARMState *env, uint32_t val)
2384
{
2385
    CPUState *cs = ENV_GET_CPU(env);
2386
    env->regs[13] -= 4;
2387
    stl_phys(cs->as, env->regs[13], val);
2388
}
2389

    
2390
static uint32_t v7m_pop(CPUARMState *env)
2391
{
2392
    CPUState *cs = ENV_GET_CPU(env);
2393
    uint32_t val;
2394
    val = ldl_phys(cs->as, env->regs[13]);
2395
    env->regs[13] += 4;
2396
    return val;
2397
}
2398

    
2399
/* Switch to V7M main or process stack pointer.  */
2400
static void switch_v7m_sp(CPUARMState *env, int process)
2401
{
2402
    uint32_t tmp;
2403
    if (env->v7m.current_sp != process) {
2404
        tmp = env->v7m.other_sp;
2405
        env->v7m.other_sp = env->regs[13];
2406
        env->regs[13] = tmp;
2407
        env->v7m.current_sp = process;
2408
    }
2409
}
2410

    
2411
static void do_v7m_exception_exit(CPUARMState *env)
2412
{
2413
    uint32_t type;
2414
    uint32_t xpsr;
2415

    
2416
    type = env->regs[15];
2417
    if (env->v7m.exception != 0)
2418
        armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
2419

    
2420
    /* Switch to the target stack.  */
2421
    switch_v7m_sp(env, (type & 4) != 0);
2422
    /* Pop registers.  */
2423
    env->regs[0] = v7m_pop(env);
2424
    env->regs[1] = v7m_pop(env);
2425
    env->regs[2] = v7m_pop(env);
2426
    env->regs[3] = v7m_pop(env);
2427
    env->regs[12] = v7m_pop(env);
2428
    env->regs[14] = v7m_pop(env);
2429
    env->regs[15] = v7m_pop(env);
2430
    xpsr = v7m_pop(env);
2431
    xpsr_write(env, xpsr, 0xfffffdff);
2432
    /* Undo stack alignment.  */
2433
    if (xpsr & 0x200)
2434
        env->regs[13] |= 4;
2435
    /* ??? The exception return type specifies Thread/Handler mode.  However
2436
       this is also implied by the xPSR value. Not sure what to do
2437
       if there is a mismatch.  */
2438
    /* ??? Likewise for mismatches between the CONTROL register and the stack
2439
       pointer.  */
2440
}
2441

    
2442
/* Exception names for debug logging; note that not all of these
2443
 * precisely correspond to architectural exceptions.
2444
 */
2445
static const char * const excnames[] = {
2446
    [EXCP_UDEF] = "Undefined Instruction",
2447
    [EXCP_SWI] = "SVC",
2448
    [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
2449
    [EXCP_DATA_ABORT] = "Data Abort",
2450
    [EXCP_IRQ] = "IRQ",
2451
    [EXCP_FIQ] = "FIQ",
2452
    [EXCP_BKPT] = "Breakpoint",
2453
    [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
2454
    [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
2455
    [EXCP_STREX] = "QEMU intercept of STREX",
2456
};
2457

    
2458
static inline void arm_log_exception(int idx)
2459
{
2460
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
2461
        const char *exc = NULL;
2462

    
2463
        if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
2464
            exc = excnames[idx];
2465
        }
2466
        if (!exc) {
2467
            exc = "unknown";
2468
        }
2469
        qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2470
    }
2471
}
2472

    
2473
void arm_v7m_cpu_do_interrupt(CPUState *cs)
2474
{
2475
    ARMCPU *cpu = ARM_CPU(cs);
2476
    CPUARMState *env = &cpu->env;
2477
    uint32_t xpsr = xpsr_read(env);
2478
    uint32_t lr;
2479
    uint32_t addr;
2480

    
2481
    arm_log_exception(env->exception_index);
2482

    
2483
    lr = 0xfffffff1;
2484
    if (env->v7m.current_sp)
2485
        lr |= 4;
2486
    if (env->v7m.exception == 0)
2487
        lr |= 8;
2488

    
2489
    /* For exceptions we just mark as pending on the NVIC, and let that
2490
       handle it.  */
2491
    /* TODO: Need to escalate if the current priority is higher than the
2492
       one we're raising.  */
2493
    switch (env->exception_index) {
2494
    case EXCP_UDEF:
2495
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
2496
        return;
2497
    case EXCP_SWI:
2498
        /* The PC already points to the next instruction.  */
2499
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
2500
        return;
2501
    case EXCP_PREFETCH_ABORT:
2502
    case EXCP_DATA_ABORT:
2503
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
2504
        return;
2505
    case EXCP_BKPT:
2506
        if (semihosting_enabled) {
2507
            int nr;
2508
            nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2509
            if (nr == 0xab) {
2510
                env->regs[15] += 2;
2511
                env->regs[0] = do_arm_semihosting(env);
2512
                qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2513
                return;
2514
            }
2515
        }
2516
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
2517
        return;
2518
    case EXCP_IRQ:
2519
        env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
2520
        break;
2521
    case EXCP_EXCEPTION_EXIT:
2522
        do_v7m_exception_exit(env);
2523
        return;
2524
    default:
2525
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2526
        return; /* Never happens.  Keep compiler happy.  */
2527
    }
2528

    
2529
    /* Align stack pointer.  */
2530
    /* ??? Should only do this if Configuration Control Register
2531
       STACKALIGN bit is set.  */
2532
    if (env->regs[13] & 4) {
2533
        env->regs[13] -= 4;
2534
        xpsr |= 0x200;
2535
    }
2536
    /* Switch to the handler mode.  */
2537
    v7m_push(env, xpsr);
2538
    v7m_push(env, env->regs[15]);
2539
    v7m_push(env, env->regs[14]);
2540
    v7m_push(env, env->regs[12]);
2541
    v7m_push(env, env->regs[3]);
2542
    v7m_push(env, env->regs[2]);
2543
    v7m_push(env, env->regs[1]);
2544
    v7m_push(env, env->regs[0]);
2545
    switch_v7m_sp(env, 0);
2546
    /* Clear IT bits */
2547
    env->condexec_bits = 0;
2548
    env->regs[14] = lr;
2549
    addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
2550
    env->regs[15] = addr & 0xfffffffe;
2551
    env->thumb = addr & 1;
2552
}
2553

    
2554
/* Handle a CPU exception.  */
2555
void arm_cpu_do_interrupt(CPUState *cs)
2556
{
2557
    ARMCPU *cpu = ARM_CPU(cs);
2558
    CPUARMState *env = &cpu->env;
2559
    uint32_t addr;
2560
    uint32_t mask;
2561
    int new_mode;
2562
    uint32_t offset;
2563

    
2564
    assert(!IS_M(env));
2565

    
2566
    arm_log_exception(env->exception_index);
2567

    
2568
    /* TODO: Vectored interrupt controller.  */
2569
    switch (env->exception_index) {
2570
    case EXCP_UDEF:
2571
        new_mode = ARM_CPU_MODE_UND;
2572
        addr = 0x04;
2573
        mask = CPSR_I;
2574
        if (env->thumb)
2575
            offset = 2;
2576
        else
2577
            offset = 4;
2578
        break;
2579
    case EXCP_SWI:
2580
        if (semihosting_enabled) {
2581
            /* Check for semihosting interrupt.  */
2582
            if (env->thumb) {
2583
                mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
2584
                    & 0xff;
2585
            } else {
2586
                mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
2587
                    & 0xffffff;
2588
            }
2589
            /* Only intercept calls from privileged modes, to provide some
2590
               semblance of security.  */
2591
            if (((mask == 0x123456 && !env->thumb)
2592
                    || (mask == 0xab && env->thumb))
2593
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2594
                env->regs[0] = do_arm_semihosting(env);
2595
                qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2596
                return;
2597
            }
2598
        }
2599
        new_mode = ARM_CPU_MODE_SVC;
2600
        addr = 0x08;
2601
        mask = CPSR_I;
2602
        /* The PC already points to the next instruction.  */
2603
        offset = 0;
2604
        break;
2605
    case EXCP_BKPT:
2606
        /* See if this is a semihosting syscall.  */
2607
        if (env->thumb && semihosting_enabled) {
2608
            mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2609
            if (mask == 0xab
2610
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2611
                env->regs[15] += 2;
2612
                env->regs[0] = do_arm_semihosting(env);
2613
                qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2614
                return;
2615
            }
2616
        }
2617
        env->cp15.c5_insn = 2;
2618
        /* Fall through to prefetch abort.  */
2619
    case EXCP_PREFETCH_ABORT:
2620
        qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
2621
                      env->cp15.c5_insn, env->cp15.c6_insn);
2622
        new_mode = ARM_CPU_MODE_ABT;
2623
        addr = 0x0c;
2624
        mask = CPSR_A | CPSR_I;
2625
        offset = 4;
2626
        break;
2627
    case EXCP_DATA_ABORT:
2628
        qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
2629
                      env->cp15.c5_data, env->cp15.c6_data);
2630
        new_mode = ARM_CPU_MODE_ABT;
2631
        addr = 0x10;
2632
        mask = CPSR_A | CPSR_I;
2633
        offset = 8;
2634
        break;
2635
    case EXCP_IRQ:
2636
        new_mode = ARM_CPU_MODE_IRQ;
2637
        addr = 0x18;
2638
        /* Disable IRQ and imprecise data aborts.  */
2639
        mask = CPSR_A | CPSR_I;
2640
        offset = 4;
2641
        break;
2642
    case EXCP_FIQ:
2643
        new_mode = ARM_CPU_MODE_FIQ;
2644
        addr = 0x1c;
2645
        /* Disable FIQ, IRQ and imprecise data aborts.  */
2646
        mask = CPSR_A | CPSR_I | CPSR_F;
2647
        offset = 4;
2648
        break;
2649
    default:
2650
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2651
        return; /* Never happens.  Keep compiler happy.  */
2652
    }
2653
    /* High vectors.  */
2654
    if (env->cp15.c1_sys & SCTLR_V) {
2655
        /* when enabled, base address cannot be remapped.  */
2656
        addr += 0xffff0000;
2657
    } else {
2658
        /* ARM v7 architectures provide a vector base address register to remap
2659
         * the interrupt vector table.
2660
         * This register is only followed in non-monitor mode, and has a secure
2661
         * and un-secure copy. Since the cpu is always in a un-secure operation
2662
         * and is never in monitor mode this feature is always active.
2663
         * Note: only bits 31:5 are valid.
2664
         */
2665
        addr += env->cp15.c12_vbar;
2666
    }
2667
    switch_mode (env, new_mode);
2668
    env->spsr = cpsr_read(env);
2669
    /* Clear IT bits.  */
2670
    env->condexec_bits = 0;
2671
    /* Switch to the new mode, and to the correct instruction set.  */
2672
    env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
2673
    env->uncached_cpsr |= mask;
2674
    /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2675
     * and we should just guard the thumb mode on V4 */
2676
    if (arm_feature(env, ARM_FEATURE_V4T)) {
2677
        env->thumb = (env->cp15.c1_sys & SCTLR_TE) != 0;
2678
    }
2679
    env->regs[14] = env->regs[15] + offset;
2680
    env->regs[15] = addr;
2681
    cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
2682
}
2683

    
2684
/* Check section/page access permissions.
2685
   Returns the page protection flags, or zero if the access is not
2686
   permitted.  */
2687
static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
2688
                           int access_type, int is_user)
2689
{
2690
  int prot_ro;
2691

    
2692
  if (domain_prot == 3) {
2693
    return PAGE_READ | PAGE_WRITE;
2694
  }
2695

    
2696
  if (access_type == 1)
2697
      prot_ro = 0;
2698
  else
2699
      prot_ro = PAGE_READ;
2700

    
2701
  switch (ap) {
2702
  case 0:
2703
      if (arm_feature(env, ARM_FEATURE_V7)) {
2704
          return 0;
2705
      }
2706
      if (access_type == 1)
2707
          return 0;
2708
      switch (env->cp15.c1_sys & (SCTLR_S | SCTLR_R)) {
2709
      case SCTLR_S:
2710
          return is_user ? 0 : PAGE_READ;
2711
      case SCTLR_R:
2712
          return PAGE_READ;
2713
      default:
2714
          return 0;
2715
      }
2716
  case 1:
2717
      return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2718
  case 2:
2719
      if (is_user)
2720
          return prot_ro;
2721
      else
2722
          return PAGE_READ | PAGE_WRITE;
2723
  case 3:
2724
      return PAGE_READ | PAGE_WRITE;
2725
  case 4: /* Reserved.  */
2726
      return 0;
2727
  case 5:
2728
      return is_user ? 0 : prot_ro;
2729
  case 6:
2730
      return prot_ro;
2731
  case 7:
2732
      if (!arm_feature (env, ARM_FEATURE_V6K))
2733
          return 0;
2734
      return prot_ro;
2735
  default:
2736
      abort();
2737
  }
2738
}
2739

    
2740
static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
2741
{
2742
    uint32_t table;
2743

    
2744
    if (address & env->cp15.c2_mask)
2745
        table = env->cp15.c2_base1 & 0xffffc000;
2746
    else
2747
        table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2748

    
2749
    table |= (address >> 18) & 0x3ffc;
2750
    return table;
2751
}
2752

    
2753
static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
2754
                            int is_user, hwaddr *phys_ptr,
2755
                            int *prot, target_ulong *page_size)
2756
{
2757
    CPUState *cs = ENV_GET_CPU(env);
2758
    int code;
2759
    uint32_t table;
2760
    uint32_t desc;
2761
    int type;
2762
    int ap;
2763
    int domain;
2764
    int domain_prot;
2765
    hwaddr phys_addr;
2766

    
2767
    /* Pagetable walk.  */
2768
    /* Lookup l1 descriptor.  */
2769
    table = get_level1_table_address(env, address);
2770
    desc = ldl_phys(cs->as, table);
2771
    type = (desc & 3);
2772
    domain = (desc >> 5) & 0x0f;
2773
    domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2774
    if (type == 0) {
2775
        /* Section translation fault.  */
2776
        code = 5;
2777
        goto do_fault;
2778
    }
2779
    if (domain_prot == 0 || domain_prot == 2) {
2780
        if (type == 2)
2781
            code = 9; /* Section domain fault.  */
2782
        else
2783
            code = 11; /* Page domain fault.  */
2784
        goto do_fault;
2785
    }
2786
    if (type == 2) {
2787
        /* 1Mb section.  */
2788
        phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2789
        ap = (desc >> 10) & 3;
2790
        code = 13;
2791
        *page_size = 1024 * 1024;
2792
    } else {
2793
        /* Lookup l2 entry.  */
2794
        if (type == 1) {
2795
            /* Coarse pagetable.  */
2796
            table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2797
        } else {
2798
            /* Fine pagetable.  */
2799
            table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2800
        }
2801
        desc = ldl_phys(cs->as, table);
2802
        switch (desc & 3) {
2803
        case 0: /* Page translation fault.  */
2804
            code = 7;
2805
            goto do_fault;
2806
        case 1: /* 64k page.  */
2807
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2808
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2809
            *page_size = 0x10000;
2810
            break;
2811
        case 2: /* 4k page.  */
2812
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2813
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2814
            *page_size = 0x1000;
2815
            break;
2816
        case 3: /* 1k page.  */
2817
            if (type == 1) {
2818
                if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2819
                    phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2820
                } else {
2821
                    /* Page translation fault.  */
2822
                    code = 7;
2823
                    goto do_fault;
2824
                }
2825
            } else {
2826
                phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
2827
            }
2828
            ap = (desc >> 4) & 3;
2829
            *page_size = 0x400;
2830
            break;
2831
        default:
2832
            /* Never happens, but compiler isn't smart enough to tell.  */
2833
            abort();
2834
        }
2835
        code = 15;
2836
    }
2837
    *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2838
    if (!*prot) {
2839
        /* Access permission fault.  */
2840
        goto do_fault;
2841
    }
2842
    *prot |= PAGE_EXEC;
2843
    *phys_ptr = phys_addr;
2844
    return 0;
2845
do_fault:
2846
    return code | (domain << 4);
2847
}
2848

    
2849
static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
2850
                            int is_user, hwaddr *phys_ptr,
2851
                            int *prot, target_ulong *page_size)
2852
{
2853
    CPUState *cs = ENV_GET_CPU(env);
2854
    int code;
2855
    uint32_t table;
2856
    uint32_t desc;
2857
    uint32_t xn;
2858
    uint32_t pxn = 0;
2859
    int type;
2860
    int ap;
2861
    int domain = 0;
2862
    int domain_prot;
2863
    hwaddr phys_addr;
2864

    
2865
    /* Pagetable walk.  */
2866
    /* Lookup l1 descriptor.  */
2867
    table = get_level1_table_address(env, address);
2868
    desc = ldl_phys(cs->as, table);
2869
    type = (desc & 3);
2870
    if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2871
        /* Section translation fault, or attempt to use the encoding
2872
         * which is Reserved on implementations without PXN.
2873
         */
2874
        code = 5;
2875
        goto do_fault;
2876
    }
2877
    if ((type == 1) || !(desc & (1 << 18))) {
2878
        /* Page or Section.  */
2879
        domain = (desc >> 5) & 0x0f;
2880
    }
2881
    domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2882
    if (domain_prot == 0 || domain_prot == 2) {
2883
        if (type != 1) {
2884
            code = 9; /* Section domain fault.  */
2885
        } else {
2886
            code = 11; /* Page domain fault.  */
2887
        }
2888
        goto do_fault;
2889
    }
2890
    if (type != 1) {
2891
        if (desc & (1 << 18)) {
2892
            /* Supersection.  */
2893
            phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
2894
            *page_size = 0x1000000;
2895
        } else {
2896
            /* Section.  */
2897
            phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2898
            *page_size = 0x100000;
2899
        }
2900
        ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
2901
        xn = desc & (1 << 4);
2902
        pxn = desc & 1;
2903
        code = 13;
2904
    } else {
2905
        if (arm_feature(env, ARM_FEATURE_PXN)) {
2906
            pxn = (desc >> 2) & 1;
2907
        }
2908
        /* Lookup l2 entry.  */
2909
        table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2910
        desc = ldl_phys(cs->as, table);
2911
        ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2912
        switch (desc & 3) {
2913
        case 0: /* Page translation fault.  */
2914
            code = 7;
2915
            goto do_fault;
2916
        case 1: /* 64k page.  */
2917
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2918
            xn = desc & (1 << 15);
2919
            *page_size = 0x10000;
2920
            break;
2921
        case 2: case 3: /* 4k page.  */
2922
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2923
            xn = desc & 1;
2924
            *page_size = 0x1000;
2925
            break;
2926
        default:
2927
            /* Never happens, but compiler isn't smart enough to tell.  */
2928
            abort();
2929
        }
2930
        code = 15;
2931
    }
2932
    if (domain_prot == 3) {
2933
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2934
    } else {
2935
        if (pxn && !is_user) {
2936
            xn = 1;
2937
        }
2938
        if (xn && access_type == 2)
2939
            goto do_fault;
2940

    
2941
        /* The simplified model uses AP[0] as an access control bit.  */
2942
        if ((env->cp15.c1_sys & SCTLR_AFE) && (ap & 1) == 0) {
2943
            /* Access flag fault.  */
2944
            code = (code == 15) ? 6 : 3;
2945
            goto do_fault;
2946
        }
2947
        *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2948
        if (!*prot) {
2949
            /* Access permission fault.  */
2950
            goto do_fault;
2951
        }
2952
        if (!xn) {
2953
            *prot |= PAGE_EXEC;
2954
        }
2955
    }
2956
    *phys_ptr = phys_addr;
2957
    return 0;
2958
do_fault:
2959
    return code | (domain << 4);
2960
}
2961

    
2962
/* Fault type for long-descriptor MMU fault reporting; this corresponds
2963
 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2964
 */
2965
typedef enum {
2966
    translation_fault = 1,
2967
    access_fault = 2,
2968
    permission_fault = 3,
2969
} MMUFaultType;
2970

    
2971
static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2972
                              int access_type, int is_user,
2973
                              hwaddr *phys_ptr, int *prot,
2974
                              target_ulong *page_size_ptr)
2975
{
2976
    CPUState *cs = ENV_GET_CPU(env);
2977
    /* Read an LPAE long-descriptor translation table. */
2978
    MMUFaultType fault_type = translation_fault;
2979
    uint32_t level = 1;
2980
    uint32_t epd;
2981
    uint32_t tsz;
2982
    uint64_t ttbr;
2983
    int ttbr_select;
2984
    int n;
2985
    hwaddr descaddr;
2986
    uint32_t tableattrs;
2987
    target_ulong page_size;
2988
    uint32_t attrs;
2989

    
2990
    /* Determine whether this address is in the region controlled by
2991
     * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2992
     * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2993
     * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2994
     */
2995
    uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2996
    uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2997
    if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2998
        /* there is a ttbr0 region and we are in it (high bits all zero) */
2999
        ttbr_select = 0;
3000
    } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
3001
        /* there is a ttbr1 region and we are in it (high bits all one) */
3002
        ttbr_select = 1;
3003
    } else if (!t0sz) {
3004
        /* ttbr0 region is "everything not in the ttbr1 region" */
3005
        ttbr_select = 0;
3006
    } else if (!t1sz) {
3007
        /* ttbr1 region is "everything not in the ttbr0 region" */
3008
        ttbr_select = 1;
3009
    } else {
3010
        /* in the gap between the two regions, this is a Translation fault */
3011
        fault_type = translation_fault;
3012
        goto do_fault;
3013
    }
3014

    
3015
    /* Note that QEMU ignores shareability and cacheability attributes,
3016
     * so we don't need to do anything with the SH, ORGN, IRGN fields
3017
     * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
3018
     * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
3019
     * implement any ASID-like capability so we can ignore it (instead
3020
     * we will always flush the TLB any time the ASID is changed).
3021
     */
3022
    if (ttbr_select == 0) {
3023
        ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
3024
        epd = extract32(env->cp15.c2_control, 7, 1);
3025
        tsz = t0sz;
3026
    } else {
3027
        ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
3028
        epd = extract32(env->cp15.c2_control, 23, 1);
3029
        tsz = t1sz;
3030
    }
3031

    
3032
    if (epd) {
3033
        /* Translation table walk disabled => Translation fault on TLB miss */
3034
        goto do_fault;
3035
    }
3036

    
3037
    /* If the region is small enough we will skip straight to a 2nd level
3038
     * lookup. This affects the number of bits of the address used in
3039
     * combination with the TTBR to find the first descriptor. ('n' here
3040
     * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
3041
     * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
3042
     */
3043
    if (tsz > 1) {
3044
        level = 2;
3045
        n = 14 - tsz;
3046
    } else {
3047
        n = 5 - tsz;
3048
    }
3049

    
3050
    /* Clear the vaddr bits which aren't part of the within-region address,
3051
     * so that we don't have to special case things when calculating the
3052
     * first descriptor address.
3053
     */
3054
    address &= (0xffffffffU >> tsz);
3055

    
3056
    /* Now we can extract the actual base address from the TTBR */
3057
    descaddr = extract64(ttbr, 0, 40);
3058
    descaddr &= ~((1ULL << n) - 1);
3059

    
3060
    tableattrs = 0;
3061
    for (;;) {
3062
        uint64_t descriptor;
3063

    
3064
        descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
3065
        descriptor = ldq_phys(cs->as, descaddr);
3066
        if (!(descriptor & 1) ||
3067
            (!(descriptor & 2) && (level == 3))) {
3068
            /* Invalid, or the Reserved level 3 encoding */
3069
            goto do_fault;
3070
        }
3071
        descaddr = descriptor & 0xfffffff000ULL;
3072

    
3073
        if ((descriptor & 2) && (level < 3)) {
3074
            /* Table entry. The top five bits are attributes which  may
3075
             * propagate down through lower levels of the table (and
3076
             * which are all arranged so that 0 means "no effect", so
3077
             * we can gather them up by ORing in the bits at each level).
3078
             */
3079
            tableattrs |= extract64(descriptor, 59, 5);
3080
            level++;
3081
            continue;
3082
        }
3083
        /* Block entry at level 1 or 2, or page entry at level 3.
3084
         * These are basically the same thing, although the number
3085
         * of bits we pull in from the vaddr varies.
3086
         */
3087
        page_size = (1 << (39 - (9 * level)));
3088
        descaddr |= (address & (page_size - 1));
3089
        /* Extract attributes from the descriptor and merge with table attrs */
3090
        attrs = extract64(descriptor, 2, 10)
3091
            | (extract64(descriptor, 52, 12) << 10);
3092
        attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3093
        attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
3094
        /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
3095
         * means "force PL1 access only", which means forcing AP[1] to 0.
3096
         */
3097
        if (extract32(tableattrs, 2, 1)) {
3098
            attrs &= ~(1 << 4);
3099
        }
3100
        /* Since we're always in the Non-secure state, NSTable is ignored. */
3101
        break;
3102
    }
3103
    /* Here descaddr is the final physical address, and attributes
3104
     * are all in attrs.
3105
     */
3106
    fault_type = access_fault;
3107
    if ((attrs & (1 << 8)) == 0) {
3108
        /* Access flag */
3109
        goto do_fault;
3110
    }
3111
    fault_type = permission_fault;
3112
    if (is_user && !(attrs & (1 << 4))) {
3113
        /* Unprivileged access not enabled */
3114
        goto do_fault;
3115
    }
3116
    *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3117
    if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
3118
        /* XN or PXN */
3119
        if (access_type == 2) {
3120
            goto do_fault;
3121
        }
3122
        *prot &= ~PAGE_EXEC;
3123
    }
3124
    if (attrs & (1 << 5)) {
3125
        /* Write access forbidden */
3126
        if (access_type == 1) {
3127
            goto do_fault;
3128
        }
3129
        *prot &= ~PAGE_WRITE;
3130
    }
3131

    
3132
    *phys_ptr = descaddr;
3133
    *page_size_ptr = page_size;
3134
    return 0;
3135

    
3136
do_fault:
3137
    /* Long-descriptor format IFSR/DFSR value */
3138
    return (1 << 9) | (fault_type << 2) | level;
3139
}
3140

    
3141
static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
3142
                             int access_type, int is_user,
3143
                             hwaddr *phys_ptr, int *prot)
3144
{
3145
    int n;
3146
    uint32_t mask;
3147
    uint32_t base;
3148

    
3149
    *phys_ptr = address;
3150
    for (n = 7; n >= 0; n--) {
3151
        base = env->cp15.c6_region[n];
3152
        if ((base & 1) == 0)
3153
            continue;
3154
        mask = 1 << ((base >> 1) & 0x1f);
3155
        /* Keep this shift separate from the above to avoid an
3156
           (undefined) << 32.  */
3157
        mask = (mask << 1) - 1;
3158
        if (((base ^ address) & ~mask) == 0)
3159
            break;
3160
    }
3161
    if (n < 0)
3162
        return 2;
3163

    
3164
    if (access_type == 2) {
3165
        mask = env->cp15.c5_insn;
3166
    } else {
3167
        mask = env->cp15.c5_data;
3168
    }
3169
    mask = (mask >> (n * 4)) & 0xf;
3170
    switch (mask) {
3171
    case 0:
3172
        return 1;
3173
    case 1:
3174
        if (is_user)
3175
          return 1;
3176
        *prot = PAGE_READ | PAGE_WRITE;
3177
        break;
3178
    case 2:
3179
        *prot = PAGE_READ;
3180
        if (!is_user)
3181
            *prot |= PAGE_WRITE;
3182
        break;
3183
    case 3:
3184
        *prot = PAGE_READ | PAGE_WRITE;
3185
        break;
3186
    case 5:
3187
        if (is_user)
3188
            return 1;
3189
        *prot = PAGE_READ;
3190
        break;
3191
    case 6:
3192
        *prot = PAGE_READ;
3193
        break;
3194
    default:
3195
        /* Bad permission.  */
3196
        return 1;
3197
    }
3198
    *prot |= PAGE_EXEC;
3199
    return 0;
3200
}
3201

    
3202
/* get_phys_addr - get the physical address for this virtual address
3203
 *
3204
 * Find the physical address corresponding to the given virtual address,
3205
 * by doing a translation table walk on MMU based systems or using the
3206
 * MPU state on MPU based systems.
3207
 *
3208
 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3209
 * prot and page_size are not filled in, and the return value provides
3210
 * information on why the translation aborted, in the format of a
3211
 * DFSR/IFSR fault register, with the following caveats:
3212
 *  * we honour the short vs long DFSR format differences.
3213
 *  * the WnR bit is never set (the caller must do this).
3214
 *  * for MPU based systems we don't bother to return a full FSR format
3215
 *    value.
3216
 *
3217
 * @env: CPUARMState
3218
 * @address: virtual address to get physical address for
3219
 * @access_type: 0 for read, 1 for write, 2 for execute
3220
 * @is_user: 0 for privileged access, 1 for user
3221
 * @phys_ptr: set to the physical address corresponding to the virtual address
3222
 * @prot: set to the permissions for the page containing phys_ptr
3223
 * @page_size: set to the size of the page containing phys_ptr
3224
 */
3225
static inline int get_phys_addr(CPUARMState *env, uint32_t address,
3226
                                int access_type, int is_user,
3227
                                hwaddr *phys_ptr, int *prot,
3228
                                target_ulong *page_size)
3229
{
3230
    /* Fast Context Switch Extension.  */
3231
    if (address < 0x02000000)
3232
        address += env->cp15.c13_fcse;
3233

    
3234
    if ((env->cp15.c1_sys & SCTLR_M) == 0) {
3235
        /* MMU/MPU disabled.  */
3236
        *phys_ptr = address;
3237
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3238
        *page_size = TARGET_PAGE_SIZE;
3239
        return 0;
3240
    } else if (arm_feature(env, ARM_FEATURE_MPU)) {
3241
        *page_size = TARGET_PAGE_SIZE;
3242
        return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
3243
                                 prot);
3244
    } else if (extended_addresses_enabled(env)) {
3245
        return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3246
                                  prot, page_size);
3247
    } else if (env->cp15.c1_sys & SCTLR_XP) {
3248
        return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
3249
                                prot, page_size);
3250
    } else {
3251
        return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
3252
                                prot, page_size);
3253
    }
3254
}
3255

    
3256
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
3257
                              int access_type, int mmu_idx)
3258
{
3259
    hwaddr phys_addr;
3260
    target_ulong page_size;
3261
    int prot;
3262
    int ret, is_user;
3263

    
3264
    is_user = mmu_idx == MMU_USER_IDX;
3265
    ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
3266
                        &page_size);
3267
    if (ret == 0) {
3268
        /* Map a single [sub]page.  */
3269
        phys_addr &= ~(hwaddr)0x3ff;
3270
        address &= ~(uint32_t)0x3ff;
3271
        tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
3272
        return 0;
3273
    }
3274

    
3275
    if (access_type == 2) {
3276
        env->cp15.c5_insn = ret;
3277
        env->cp15.c6_insn = address;
3278
        env->exception_index = EXCP_PREFETCH_ABORT;
3279
    } else {
3280
        env->cp15.c5_data = ret;
3281
        if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3282
            env->cp15.c5_data |= (1 << 11);
3283
        env->cp15.c6_data = address;
3284
        env->exception_index = EXCP_DATA_ABORT;
3285
    }
3286
    return 1;
3287
}
3288

    
3289
hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
3290
{
3291
    ARMCPU *cpu = ARM_CPU(cs);
3292
    hwaddr phys_addr;
3293
    target_ulong page_size;
3294
    int prot;
3295
    int ret;
3296

    
3297
    ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
3298

    
3299
    if (ret != 0) {
3300
        return -1;
3301
    }
3302

    
3303
    return phys_addr;
3304
}
3305

    
3306
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
3307
{
3308
    if ((env->uncached_cpsr & CPSR_M) == mode) {
3309
        env->regs[13] = val;
3310
    } else {
3311
        env->banked_r13[bank_number(mode)] = val;
3312
    }
3313
}
3314

    
3315
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
3316
{
3317
    if ((env->uncached_cpsr & CPSR_M) == mode) {
3318
        return env->regs[13];
3319
    } else {
3320
        return env->banked_r13[bank_number(mode)];
3321
    }
3322
}
3323

    
3324
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3325
{
3326
    switch (reg) {
3327
    case 0: /* APSR */
3328
        return xpsr_read(env) & 0xf8000000;
3329
    case 1: /* IAPSR */
3330
        return xpsr_read(env) & 0xf80001ff;
3331
    case 2: /* EAPSR */
3332
        return xpsr_read(env) & 0xff00fc00;
3333
    case 3: /* xPSR */
3334
        return xpsr_read(env) & 0xff00fdff;
3335
    case 5: /* IPSR */
3336
        return xpsr_read(env) & 0x000001ff;
3337
    case 6: /* EPSR */
3338
        return xpsr_read(env) & 0x0700fc00;
3339
    case 7: /* IEPSR */
3340
        return xpsr_read(env) & 0x0700edff;
3341
    case 8: /* MSP */
3342
        return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
3343
    case 9: /* PSP */
3344
        return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3345
    case 16: /* PRIMASK */
3346
        return (env->uncached_cpsr & CPSR_I) != 0;
3347
    case 17: /* BASEPRI */
3348
    case 18: /* BASEPRI_MAX */
3349
        return env->v7m.basepri;
3350
    case 19: /* FAULTMASK */
3351
        return (env->uncached_cpsr & CPSR_F) != 0;
3352
    case 20: /* CONTROL */
3353
        return env->v7m.control;
3354
    default:
3355
        /* ??? For debugging only.  */
3356
        cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
3357
        return 0;
3358
    }
3359
}
3360

    
3361
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3362
{
3363
    switch (reg) {
3364
    case 0: /* APSR */
3365
        xpsr_write(env, val, 0xf8000000);
3366
        break;
3367
    case 1: /* IAPSR */
3368
        xpsr_write(env, val, 0xf8000000);
3369
        break;
3370
    case 2: /* EAPSR */
3371
        xpsr_write(env, val, 0xfe00fc00);
3372
        break;
3373
    case 3: /* xPSR */
3374
        xpsr_write(env, val, 0xfe00fc00);
3375
        break;
3376
    case 5: /* IPSR */
3377
        /* IPSR bits are readonly.  */
3378
        break;
3379
    case 6: /* EPSR */
3380
        xpsr_write(env, val, 0x0600fc00);
3381
        break;
3382
    case 7: /* IEPSR */
3383
        xpsr_write(env, val, 0x0600fc00);
3384
        break;
3385
    case 8: /* MSP */
3386
        if (env->v7m.current_sp)
3387
            env->v7m.other_sp = val;
3388
        else
3389
            env->regs[13] = val;
3390
        break;
3391
    case 9: /* PSP */
3392
        if (env->v7m.current_sp)
3393
            env->regs[13] = val;
3394
        else
3395
            env->v7m.other_sp = val;
3396
        break;
3397
    case 16: /* PRIMASK */
3398
        if (val & 1)
3399
            env->uncached_cpsr |= CPSR_I;
3400
        else
3401
            env->uncached_cpsr &= ~CPSR_I;
3402
        break;
3403
    case 17: /* BASEPRI */
3404
        env->v7m.basepri = val & 0xff;
3405
        break;
3406
    case 18: /* BASEPRI_MAX */
3407
        val &= 0xff;
3408
        if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
3409
            env->v7m.basepri = val;
3410
        break;
3411
    case 19: /* FAULTMASK */
3412
        if (val & 1)
3413
            env->uncached_cpsr |= CPSR_F;
3414
        else
3415
            env->uncached_cpsr &= ~CPSR_F;
3416
        break;
3417
    case 20: /* CONTROL */
3418
        env->v7m.control = val & 3;
3419
        switch_v7m_sp(env, (val & 2) != 0);
3420
        break;
3421
    default:
3422
        /* ??? For debugging only.  */
3423
        cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
3424
        return;
3425
    }
3426
}
3427

    
3428
#endif
3429

    
3430
/* Note that signed overflow is undefined in C.  The following routines are
3431
   careful to use unsigned types where modulo arithmetic is required.
3432
   Failure to do so _will_ break on newer gcc.  */
3433

    
3434
/* Signed saturating arithmetic.  */
3435

    
3436
/* Perform 16-bit signed saturating addition.  */
3437
static inline uint16_t add16_sat(uint16_t a, uint16_t b)
3438
{
3439
    uint16_t res;
3440

    
3441
    res = a + b;
3442
    if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
3443
        if (a & 0x8000)
3444
            res = 0x8000;
3445
        else
3446
            res = 0x7fff;
3447
    }
3448
    return res;
3449
}
3450

    
3451
/* Perform 8-bit signed saturating addition.  */
3452
static inline uint8_t add8_sat(uint8_t a, uint8_t b)
3453
{
3454
    uint8_t res;
3455

    
3456
    res = a + b;
3457
    if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
3458
        if (a & 0x80)
3459
            res = 0x80;
3460
        else
3461
            res = 0x7f;
3462
    }
3463
    return res;
3464
}
3465

    
3466
/* Perform 16-bit signed saturating subtraction.  */
3467
static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
3468
{
3469
    uint16_t res;
3470

    
3471
    res = a - b;
3472
    if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
3473
        if (a & 0x8000)
3474
            res = 0x8000;
3475
        else
3476
            res = 0x7fff;
3477
    }
3478
    return res;
3479
}
3480

    
3481
/* Perform 8-bit signed saturating subtraction.  */
3482
static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3483
{
3484
    uint8_t res;
3485

    
3486
    res = a - b;
3487
    if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3488
        if (a & 0x80)
3489
            res = 0x80;
3490
        else
3491
            res = 0x7f;
3492
    }
3493
    return res;
3494
}
3495

    
3496
#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3497
#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3498
#define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
3499
#define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
3500
#define PFX q
3501

    
3502
#include "op_addsub.h"
3503

    
3504
/* Unsigned saturating arithmetic.  */
3505
static inline uint16_t add16_usat(uint16_t a, uint16_t b)
3506
{
3507
    uint16_t res;
3508
    res = a + b;
3509
    if (res < a)
3510
        res = 0xffff;
3511
    return res;
3512
}
3513

    
3514
static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
3515
{
3516
    if (a > b)
3517
        return a - b;
3518
    else
3519
        return 0;
3520
}
3521

    
3522
static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3523
{
3524
    uint8_t res;
3525
    res = a + b;
3526
    if (res < a)
3527
        res = 0xff;
3528
    return res;
3529
}
3530

    
3531
static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3532
{
3533
    if (a > b)
3534
        return a - b;
3535
    else
3536
        return 0;
3537
}
3538

    
3539
#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3540
#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3541
#define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
3542
#define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
3543
#define PFX uq
3544

    
3545
#include "op_addsub.h"
3546

    
3547
/* Signed modulo arithmetic.  */
3548
#define SARITH16(a, b, n, op) do { \
3549
    int32_t sum; \
3550
    sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
3551
    RESULT(sum, n, 16); \
3552
    if (sum >= 0) \
3553
        ge |= 3 << (n * 2); \
3554
    } while(0)
3555

    
3556
#define SARITH8(a, b, n, op) do { \
3557
    int32_t sum; \
3558
    sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
3559
    RESULT(sum, n, 8); \
3560
    if (sum >= 0) \
3561
        ge |= 1 << n; \
3562
    } while(0)
3563

    
3564

    
3565
#define ADD16(a, b, n) SARITH16(a, b, n, +)
3566
#define SUB16(a, b, n) SARITH16(a, b, n, -)
3567
#define ADD8(a, b, n)  SARITH8(a, b, n, +)
3568
#define SUB8(a, b, n)  SARITH8(a, b, n, -)
3569
#define PFX s
3570
#define ARITH_GE
3571

    
3572
#include "op_addsub.h"
3573

    
3574
/* Unsigned modulo arithmetic.  */
3575
#define ADD16(a, b, n) do { \
3576
    uint32_t sum; \
3577
    sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3578
    RESULT(sum, n, 16); \
3579
    if ((sum >> 16) == 1) \
3580
        ge |= 3 << (n * 2); \
3581
    } while(0)
3582

    
3583
#define ADD8(a, b, n) do { \
3584
    uint32_t sum; \
3585
    sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3586
    RESULT(sum, n, 8); \
3587
    if ((sum >> 8) == 1) \
3588
        ge |= 1 << n; \
3589
    } while(0)
3590

    
3591
#define SUB16(a, b, n) do { \
3592
    uint32_t sum; \
3593
    sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3594
    RESULT(sum, n, 16); \
3595
    if ((sum >> 16) == 0) \
3596
        ge |= 3 << (n * 2); \
3597
    } while(0)
3598

    
3599
#define SUB8(a, b, n) do { \
3600
    uint32_t sum; \
3601
    sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3602
    RESULT(sum, n, 8); \
3603
    if ((sum >> 8) == 0) \
3604
        ge |= 1 << n; \
3605
    } while(0)
3606

    
3607
#define PFX u
3608
#define ARITH_GE
3609

    
3610
#include "op_addsub.h"
3611

    
3612
/* Halved signed arithmetic.  */
3613
#define ADD16(a, b, n) \
3614
  RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3615
#define SUB16(a, b, n) \
3616
  RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3617
#define ADD8(a, b, n) \
3618
  RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3619
#define SUB8(a, b, n) \
3620
  RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3621
#define PFX sh
3622

    
3623
#include "op_addsub.h"
3624

    
3625
/* Halved unsigned arithmetic.  */
3626
#define ADD16(a, b, n) \
3627
  RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3628
#define SUB16(a, b, n) \
3629
  RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3630
#define ADD8(a, b, n) \
3631
  RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3632
#define SUB8(a, b, n) \
3633
  RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3634
#define PFX uh
3635

    
3636
#include "op_addsub.h"
3637

    
3638
static inline uint8_t do_usad(uint8_t a, uint8_t b)
3639
{
3640
    if (a > b)
3641
        return a - b;
3642
    else
3643
        return b - a;
3644
}
3645

    
3646
/* Unsigned sum of absolute byte differences.  */
3647
uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3648
{
3649
    uint32_t sum;
3650
    sum = do_usad(a, b);
3651
    sum += do_usad(a >> 8, b >> 8);
3652
    sum += do_usad(a >> 16, b >>16);
3653
    sum += do_usad(a >> 24, b >> 24);
3654
    return sum;
3655
}
3656

    
3657
/* For ARMv6 SEL instruction.  */
3658
uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3659
{
3660
    uint32_t mask;
3661

    
3662
    mask = 0;
3663
    if (flags & 1)
3664
        mask |= 0xff;
3665
    if (flags & 2)
3666
        mask |= 0xff00;
3667
    if (flags & 4)
3668
        mask |= 0xff0000;
3669
    if (flags & 8)
3670
        mask |= 0xff000000;
3671
    return (a & mask) | (b & ~mask);
3672
}
3673

    
3674
/* VFP support.  We follow the convention used for VFP instructions:
3675
   Single precision routines have a "s" suffix, double precision a
3676
   "d" suffix.  */
3677

    
3678
/* Convert host exception flags to vfp form.  */
3679
static inline int vfp_exceptbits_from_host(int host_bits)
3680
{
3681
    int target_bits = 0;
3682

    
3683
    if (host_bits & float_flag_invalid)
3684
        target_bits |= 1;
3685
    if (host_bits & float_flag_divbyzero)
3686
        target_bits |= 2;
3687
    if (host_bits & float_flag_overflow)
3688
        target_bits |= 4;
3689
    if (host_bits & (float_flag_underflow | float_flag_output_denormal))
3690
        target_bits |= 8;
3691
    if (host_bits & float_flag_inexact)
3692
        target_bits |= 0x10;
3693
    if (host_bits & float_flag_input_denormal)
3694
        target_bits |= 0x80;
3695
    return target_bits;
3696
}
3697

    
3698
uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
3699
{
3700
    int i;
3701
    uint32_t fpscr;
3702

    
3703
    fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3704
            | (env->vfp.vec_len << 16)
3705
            | (env->vfp.vec_stride << 20);
3706
    i = get_float_exception_flags(&env->vfp.fp_status);
3707
    i |= get_float_exception_flags(&env->vfp.standard_fp_status);
3708
    fpscr |= vfp_exceptbits_from_host(i);
3709
    return fpscr;
3710
}
3711

    
3712
uint32_t vfp_get_fpscr(CPUARMState *env)
3713
{
3714
    return HELPER(vfp_get_fpscr)(env);
3715
}
3716

    
3717
/* Convert vfp exception flags to target form.  */
3718
static inline int vfp_exceptbits_to_host(int target_bits)
3719
{
3720
    int host_bits = 0;
3721

    
3722
    if (target_bits & 1)
3723
        host_bits |= float_flag_invalid;
3724
    if (target_bits & 2)
3725
        host_bits |= float_flag_divbyzero;
3726
    if (target_bits & 4)
3727
        host_bits |= float_flag_overflow;
3728
    if (target_bits & 8)
3729
        host_bits |= float_flag_underflow;
3730
    if (target_bits & 0x10)
3731
        host_bits |= float_flag_inexact;
3732
    if (target_bits & 0x80)
3733
        host_bits |= float_flag_input_denormal;
3734
    return host_bits;
3735
}
3736

    
3737
void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
3738
{
3739
    int i;
3740
    uint32_t changed;
3741

    
3742
    changed = env->vfp.xregs[ARM_VFP_FPSCR];
3743
    env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3744
    env->vfp.vec_len = (val >> 16) & 7;
3745
    env->vfp.vec_stride = (val >> 20) & 3;
3746

    
3747
    changed ^= val;
3748
    if (changed & (3 << 22)) {
3749
        i = (val >> 22) & 3;
3750
        switch (i) {
3751
        case FPROUNDING_TIEEVEN:
3752
            i = float_round_nearest_even;
3753
            break;
3754
        case FPROUNDING_POSINF:
3755
            i = float_round_up;
3756
            break;
3757
        case FPROUNDING_NEGINF:
3758
            i = float_round_down;
3759
            break;
3760
        case FPROUNDING_ZERO:
3761
            i = float_round_to_zero;
3762
            break;
3763
        }
3764
        set_float_rounding_mode(i, &env->vfp.fp_status);
3765
    }
3766
    if (changed & (1 << 24)) {
3767
        set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3768
        set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3769
    }
3770
    if (changed & (1 << 25))
3771
        set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
3772

    
3773
    i = vfp_exceptbits_to_host(val);
3774
    set_float_exception_flags(i, &env->vfp.fp_status);
3775
    set_float_exception_flags(0, &env->vfp.standard_fp_status);
3776
}
3777

    
3778
void vfp_set_fpscr(CPUARMState *env, uint32_t val)
3779
{
3780
    HELPER(vfp_set_fpscr)(env, val);
3781
}
3782

    
3783
#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3784

    
3785
#define VFP_BINOP(name) \
3786
float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
3787
{ \
3788
    float_status *fpst = fpstp; \
3789
    return float32_ ## name(a, b, fpst); \
3790
} \
3791
float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
3792
{ \
3793
    float_status *fpst = fpstp; \
3794
    return float64_ ## name(a, b, fpst); \
3795
}
3796
VFP_BINOP(add)
3797
VFP_BINOP(sub)
3798
VFP_BINOP(mul)
3799
VFP_BINOP(div)
3800
VFP_BINOP(min)
3801
VFP_BINOP(max)
3802
VFP_BINOP(minnum)
3803
VFP_BINOP(maxnum)
3804
#undef VFP_BINOP
3805

    
3806
float32 VFP_HELPER(neg, s)(float32 a)
3807
{
3808
    return float32_chs(a);
3809
}
3810

    
3811
float64 VFP_HELPER(neg, d)(float64 a)
3812
{
3813
    return float64_chs(a);
3814
}
3815

    
3816
float32 VFP_HELPER(abs, s)(float32 a)
3817
{
3818
    return float32_abs(a);
3819
}
3820

    
3821
float64 VFP_HELPER(abs, d)(float64 a)
3822
{
3823
    return float64_abs(a);
3824
}
3825

    
3826
float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
3827
{
3828
    return float32_sqrt(a, &env->vfp.fp_status);
3829
}
3830

    
3831
float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
3832
{
3833
    return float64_sqrt(a, &env->vfp.fp_status);
3834
}
3835

    
3836
/* XXX: check quiet/signaling case */
3837
#define DO_VFP_cmp(p, type) \
3838
void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env)  \
3839
{ \
3840
    uint32_t flags; \
3841
    switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3842
    case 0: flags = 0x6; break; \
3843
    case -1: flags = 0x8; break; \
3844
    case 1: flags = 0x2; break; \
3845
    default: case 2: flags = 0x3; break; \
3846
    } \
3847
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3848
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3849
} \
3850
void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
3851
{ \
3852
    uint32_t flags; \
3853
    switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3854
    case 0: flags = 0x6; break; \
3855
    case -1: flags = 0x8; break; \
3856
    case 1: flags = 0x2; break; \
3857
    default: case 2: flags = 0x3; break; \
3858
    } \
3859
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3860
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3861
}
3862
DO_VFP_cmp(s, float32)
3863
DO_VFP_cmp(d, float64)
3864
#undef DO_VFP_cmp
3865

    
3866
/* Integer to float and float to integer conversions */
3867

    
3868
#define CONV_ITOF(name, fsz, sign) \
3869
    float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3870
{ \
3871
    float_status *fpst = fpstp; \
3872
    return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
3873
}
3874

    
3875
#define CONV_FTOI(name, fsz, sign, round) \
3876
uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3877
{ \
3878
    float_status *fpst = fpstp; \
3879
    if (float##fsz##_is_any_nan(x)) { \
3880
        float_raise(float_flag_invalid, fpst); \
3881
        return 0; \
3882
    } \
3883
    return float##fsz##_to_##sign##int32##round(x, fpst); \
3884
}
3885

    
3886
#define FLOAT_CONVS(name, p, fsz, sign) \
3887
CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3888
CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3889
CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
3890

    
3891
FLOAT_CONVS(si, s, 32, )
3892
FLOAT_CONVS(si, d, 64, )
3893
FLOAT_CONVS(ui, s, 32, u)
3894
FLOAT_CONVS(ui, d, 64, u)
3895

    
3896
#undef CONV_ITOF
3897
#undef CONV_FTOI
3898
#undef FLOAT_CONVS
3899

    
3900
/* floating point conversion */
3901
float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
3902
{
3903
    float64 r = float32_to_float64(x, &env->vfp.fp_status);
3904
    /* ARM requires that S<->D conversion of any kind of NaN generates
3905
     * a quiet NaN by forcing the most significant frac bit to 1.
3906
     */
3907
    return float64_maybe_silence_nan(r);
3908
}
3909

    
3910
float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
3911
{
3912
    float32 r =  float64_to_float32(x, &env->vfp.fp_status);
3913
    /* ARM requires that S<->D conversion of any kind of NaN generates
3914
     * a quiet NaN by forcing the most significant frac bit to 1.
3915
     */
3916
    return float32_maybe_silence_nan(r);
3917
}
3918

    
3919
/* VFP3 fixed point conversion.  */
3920
#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
3921
float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t  x, uint32_t shift, \
3922
                                     void *fpstp) \
3923
{ \
3924
    float_status *fpst = fpstp; \
3925
    float##fsz tmp; \
3926
    tmp = itype##_to_##float##fsz(x, fpst); \
3927
    return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
3928
}
3929

    
3930
/* Notice that we want only input-denormal exception flags from the
3931
 * scalbn operation: the other possible flags (overflow+inexact if
3932
 * we overflow to infinity, output-denormal) aren't correct for the
3933
 * complete scale-and-convert operation.
3934
 */
3935
#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
3936
uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
3937
                                             uint32_t shift, \
3938
                                             void *fpstp) \
3939
{ \
3940
    float_status *fpst = fpstp; \
3941
    int old_exc_flags = get_float_exception_flags(fpst); \
3942
    float##fsz tmp; \
3943
    if (float##fsz##_is_any_nan(x)) { \
3944
        float_raise(float_flag_invalid, fpst); \
3945
        return 0; \
3946
    } \
3947
    tmp = float##fsz##_scalbn(x, shift, fpst); \
3948
    old_exc_flags |= get_float_exception_flags(fpst) \
3949
        & float_flag_input_denormal; \
3950
    set_float_exception_flags(old_exc_flags, fpst); \
3951
    return float##fsz##_to_##itype##round(tmp, fpst); \
3952
}
3953

    
3954
#define VFP_CONV_FIX(name, p, fsz, isz, itype)                   \
3955
VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype)                     \
3956
VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
3957
VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
3958

    
3959
#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype)               \
3960
VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype)                     \
3961
VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
3962

    
3963
VFP_CONV_FIX(sh, d, 64, 64, int16)
3964
VFP_CONV_FIX(sl, d, 64, 64, int32)
3965
VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
3966
VFP_CONV_FIX(uh, d, 64, 64, uint16)
3967
VFP_CONV_FIX(ul, d, 64, 64, uint32)
3968
VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
3969
VFP_CONV_FIX(sh, s, 32, 32, int16)
3970
VFP_CONV_FIX(sl, s, 32, 32, int32)
3971
VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
3972
VFP_CONV_FIX(uh, s, 32, 32, uint16)
3973
VFP_CONV_FIX(ul, s, 32, 32, uint32)
3974
VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
3975
#undef VFP_CONV_FIX
3976
#undef VFP_CONV_FIX_FLOAT
3977
#undef VFP_CONV_FLOAT_FIX_ROUND
3978

    
3979
/* Set the current fp rounding mode and return the old one.
3980
 * The argument is a softfloat float_round_ value.
3981
 */
3982
uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
3983
{
3984
    float_status *fp_status = &env->vfp.fp_status;
3985

    
3986
    uint32_t prev_rmode = get_float_rounding_mode(fp_status);
3987
    set_float_rounding_mode(rmode, fp_status);
3988

    
3989
    return prev_rmode;
3990
}
3991

    
3992
/* Set the current fp rounding mode in the standard fp status and return
3993
 * the old one. This is for NEON instructions that need to change the
3994
 * rounding mode but wish to use the standard FPSCR values for everything
3995
 * else. Always set the rounding mode back to the correct value after
3996
 * modifying it.
3997
 * The argument is a softfloat float_round_ value.
3998
 */
3999
uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
4000
{
4001
    float_status *fp_status = &env->vfp.standard_fp_status;
4002

    
4003
    uint32_t prev_rmode = get_float_rounding_mode(fp_status);
4004
    set_float_rounding_mode(rmode, fp_status);
4005

    
4006
    return prev_rmode;
4007
}
4008

    
4009
/* Half precision conversions.  */
4010
static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
4011
{
4012
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
4013
    float32 r = float16_to_float32(make_float16(a), ieee, s);
4014
    if (ieee) {
4015
        return float32_maybe_silence_nan(r);
4016
    }
4017
    return r;
4018
}
4019

    
4020
static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
4021
{
4022
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
4023
    float16 r = float32_to_float16(a, ieee, s);
4024
    if (ieee) {
4025
        r = float16_maybe_silence_nan(r);
4026
    }
4027
    return float16_val(r);
4028
}
4029

    
4030
float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
4031
{
4032
    return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
4033
}
4034

    
4035
uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
4036
{
4037
    return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
4038
}
4039

    
4040
float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
4041
{
4042
    return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
4043
}
4044

    
4045
uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
4046
{
4047
    return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
4048
}
4049

    
4050
float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
4051
{
4052
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
4053
    float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
4054
    if (ieee) {
4055
        return float64_maybe_silence_nan(r);
4056
    }
4057
    return r;
4058
}
4059

    
4060
uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
4061
{
4062
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
4063
    float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
4064
    if (ieee) {
4065
        r = float16_maybe_silence_nan(r);
4066
    }
4067
    return float16_val(r);
4068
}
4069

    
4070
#define float32_two make_float32(0x40000000)
4071
#define float32_three make_float32(0x40400000)
4072
#define float32_one_point_five make_float32(0x3fc00000)
4073

    
4074
float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
4075
{
4076
    float_status *s = &env->vfp.standard_fp_status;
4077
    if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
4078
        (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
4079
        if (!(float32_is_zero(a) || float32_is_zero(b))) {
4080
            float_raise(float_flag_input_denormal, s);
4081
        }
4082
        return float32_two;
4083
    }
4084
    return float32_sub(float32_two, float32_mul(a, b, s), s);
4085
}
4086

    
4087
float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
4088
{
4089
    float_status *s = &env->vfp.standard_fp_status;
4090
    float32 product;
4091
    if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
4092
        (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
4093
        if (!(float32_is_zero(a) || float32_is_zero(b))) {
4094
            float_raise(float_flag_input_denormal, s);
4095
        }
4096
        return float32_one_point_five;
4097
    }
4098
    product = float32_mul(a, b, s);
4099
    return float32_div(float32_sub(float32_three, product, s), float32_two, s);
4100
}
4101

    
4102
/* NEON helpers.  */
4103

    
4104
/* Constants 256 and 512 are used in some helpers; we avoid relying on
4105
 * int->float conversions at run-time.  */
4106
#define float64_256 make_float64(0x4070000000000000LL)
4107
#define float64_512 make_float64(0x4080000000000000LL)
4108

    
4109
/* The algorithm that must be used to calculate the estimate
4110
 * is specified by the ARM ARM.
4111
 */
4112
static float64 recip_estimate(float64 a, CPUARMState *env)
4113
{
4114
    /* These calculations mustn't set any fp exception flags,
4115
     * so we use a local copy of the fp_status.
4116
     */
4117
    float_status dummy_status = env->vfp.standard_fp_status;
4118
    float_status *s = &dummy_status;
4119
    /* q = (int)(a * 512.0) */
4120
    float64 q = float64_mul(float64_512, a, s);
4121
    int64_t q_int = float64_to_int64_round_to_zero(q, s);
4122

    
4123
    /* r = 1.0 / (((double)q + 0.5) / 512.0) */
4124
    q = int64_to_float64(q_int, s);
4125
    q = float64_add(q, float64_half, s);
4126
    q = float64_div(q, float64_512, s);
4127
    q = float64_div(float64_one, q, s);
4128

    
4129
    /* s = (int)(256.0 * r + 0.5) */
4130
    q = float64_mul(q, float64_256, s);
4131
    q = float64_add(q, float64_half, s);
4132
    q_int = float64_to_int64_round_to_zero(q, s);
4133

    
4134
    /* return (double)s / 256.0 */
4135
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
4136
}
4137

    
4138
float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
4139
{
4140
    float_status *s = &env->vfp.standard_fp_status;
4141
    float64 f64;
4142
    uint32_t val32 = float32_val(a);
4143

    
4144
    int result_exp;
4145
    int a_exp = (val32  & 0x7f800000) >> 23;
4146
    int sign = val32 & 0x80000000;
4147

    
4148
    if (float32_is_any_nan(a)) {
4149
        if (float32_is_signaling_nan(a)) {
4150
            float_raise(float_flag_invalid, s);
4151
        }
4152
        return float32_default_nan;
4153
    } else if (float32_is_infinity(a)) {
4154
        return float32_set_sign(float32_zero, float32_is_neg(a));
4155
    } else if (float32_is_zero_or_denormal(a)) {
4156
        if (!float32_is_zero(a)) {
4157
            float_raise(float_flag_input_denormal, s);
4158
        }
4159
        float_raise(float_flag_divbyzero, s);
4160
        return float32_set_sign(float32_infinity, float32_is_neg(a));
4161
    } else if (a_exp >= 253) {
4162
        float_raise(float_flag_underflow, s);
4163
        return float32_set_sign(float32_zero, float32_is_neg(a));
4164
    }
4165

    
4166
    f64 = make_float64((0x3feULL << 52)
4167
                       | ((int64_t)(val32 & 0x7fffff) << 29));
4168

    
4169
    result_exp = 253 - a_exp;
4170

    
4171
    f64 = recip_estimate(f64, env);
4172

    
4173
    val32 = sign
4174
        | ((result_exp & 0xff) << 23)
4175
        | ((float64_val(f64) >> 29) & 0x7fffff);
4176
    return make_float32(val32);
4177
}
4178

    
4179
/* The algorithm that must be used to calculate the estimate
4180
 * is specified by the ARM ARM.
4181
 */
4182
static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
4183
{
4184
    /* These calculations mustn't set any fp exception flags,
4185
     * so we use a local copy of the fp_status.
4186
     */
4187
    float_status dummy_status = env->vfp.standard_fp_status;
4188
    float_status *s = &dummy_status;
4189
    float64 q;
4190
    int64_t q_int;
4191

    
4192
    if (float64_lt(a, float64_half, s)) {
4193
        /* range 0.25 <= a < 0.5 */
4194

    
4195
        /* a in units of 1/512 rounded down */
4196
        /* q0 = (int)(a * 512.0);  */
4197
        q = float64_mul(float64_512, a, s);
4198
        q_int = float64_to_int64_round_to_zero(q, s);
4199

    
4200
        /* reciprocal root r */
4201
        /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0);  */
4202
        q = int64_to_float64(q_int, s);
4203
        q = float64_add(q, float64_half, s);
4204
        q = float64_div(q, float64_512, s);
4205
        q = float64_sqrt(q, s);
4206
        q = float64_div(float64_one, q, s);
4207
    } else {
4208
        /* range 0.5 <= a < 1.0 */
4209

    
4210
        /* a in units of 1/256 rounded down */
4211
        /* q1 = (int)(a * 256.0); */
4212
        q = float64_mul(float64_256, a, s);
4213
        int64_t q_int = float64_to_int64_round_to_zero(q, s);
4214

    
4215
        /* reciprocal root r */
4216
        /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
4217
        q = int64_to_float64(q_int, s);
4218
        q = float64_add(q, float64_half, s);
4219
        q = float64_div(q, float64_256, s);
4220
        q = float64_sqrt(q, s);
4221
        q = float64_div(float64_one, q, s);
4222
    }
4223
    /* r in units of 1/256 rounded to nearest */
4224
    /* s = (int)(256.0 * r + 0.5); */
4225

    
4226
    q = float64_mul(q, float64_256,s );
4227
    q = float64_add(q, float64_half, s);
4228
    q_int = float64_to_int64_round_to_zero(q, s);
4229

    
4230
    /* return (double)s / 256.0;*/
4231
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
4232
}
4233

    
4234
float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
4235
{
4236
    float_status *s = &env->vfp.standard_fp_status;
4237
    int result_exp;
4238
    float64 f64;
4239
    uint32_t val;
4240
    uint64_t val64;
4241

    
4242
    val = float32_val(a);
4243

    
4244
    if (float32_is_any_nan(a)) {
4245
        if (float32_is_signaling_nan(a)) {
4246
            float_raise(float_flag_invalid, s);
4247
        }
4248
        return float32_default_nan;
4249
    } else if (float32_is_zero_or_denormal(a)) {
4250
        if (!float32_is_zero(a)) {
4251
            float_raise(float_flag_input_denormal, s);
4252
        }
4253
        float_raise(float_flag_divbyzero, s);
4254
        return float32_set_sign(float32_infinity, float32_is_neg(a));
4255
    } else if (float32_is_neg(a)) {
4256
        float_raise(float_flag_invalid, s);
4257
        return float32_default_nan;
4258
    } else if (float32_is_infinity(a)) {
4259
        return float32_zero;
4260
    }
4261

    
4262
    /* Normalize to a double-precision value between 0.25 and 1.0,
4263
     * preserving the parity of the exponent.  */
4264
    if ((val & 0x800000) == 0) {
4265
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
4266
                           | (0x3feULL << 52)
4267
                           | ((uint64_t)(val & 0x7fffff) << 29));
4268
    } else {
4269
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
4270
                           | (0x3fdULL << 52)
4271
                           | ((uint64_t)(val & 0x7fffff) << 29));
4272
    }
4273

    
4274
    result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
4275

    
4276
    f64 = recip_sqrt_estimate(f64, env);
4277

    
4278
    val64 = float64_val(f64);
4279

    
4280
    val = ((result_exp & 0xff) << 23)
4281
        | ((val64 >> 29)  & 0x7fffff);
4282
    return make_float32(val);
4283
}
4284

    
4285
uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
4286
{
4287
    float64 f64;
4288

    
4289
    if ((a & 0x80000000) == 0) {
4290
        return 0xffffffff;
4291
    }
4292

    
4293
    f64 = make_float64((0x3feULL << 52)
4294
                       | ((int64_t)(a & 0x7fffffff) << 21));
4295

    
4296
    f64 = recip_estimate (f64, env);
4297

    
4298
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4299
}
4300

    
4301
uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
4302
{
4303
    float64 f64;
4304

    
4305
    if ((a & 0xc0000000) == 0) {
4306
        return 0xffffffff;
4307
    }
4308

    
4309
    if (a & 0x80000000) {
4310
        f64 = make_float64((0x3feULL << 52)
4311
                           | ((uint64_t)(a & 0x7fffffff) << 21));
4312
    } else { /* bits 31-30 == '01' */
4313
        f64 = make_float64((0x3fdULL << 52)
4314
                           | ((uint64_t)(a & 0x3fffffff) << 22));
4315
    }
4316

    
4317
    f64 = recip_sqrt_estimate(f64, env);
4318

    
4319
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4320
}
4321

    
4322
/* VFPv4 fused multiply-accumulate */
4323
float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
4324
{
4325
    float_status *fpst = fpstp;
4326
    return float32_muladd(a, b, c, 0, fpst);
4327
}
4328

    
4329
float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
4330
{
4331
    float_status *fpst = fpstp;
4332
    return float64_muladd(a, b, c, 0, fpst);
4333
}
4334

    
4335
/* ARMv8 round to integral */
4336
float32 HELPER(rints_exact)(float32 x, void *fp_status)
4337
{
4338
    return float32_round_to_int(x, fp_status);
4339
}
4340

    
4341
float64 HELPER(rintd_exact)(float64 x, void *fp_status)
4342
{
4343
    return float64_round_to_int(x, fp_status);
4344
}
4345

    
4346
float32 HELPER(rints)(float32 x, void *fp_status)
4347
{
4348
    int old_flags = get_float_exception_flags(fp_status), new_flags;
4349
    float32 ret;
4350

    
4351
    ret = float32_round_to_int(x, fp_status);
4352

    
4353
    /* Suppress any inexact exceptions the conversion produced */
4354
    if (!(old_flags & float_flag_inexact)) {
4355
        new_flags = get_float_exception_flags(fp_status);
4356
        set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
4357
    }
4358

    
4359
    return ret;
4360
}
4361

    
4362
float64 HELPER(rintd)(float64 x, void *fp_status)
4363
{
4364
    int old_flags = get_float_exception_flags(fp_status), new_flags;
4365
    float64 ret;
4366

    
4367
    ret = float64_round_to_int(x, fp_status);
4368

    
4369
    new_flags = get_float_exception_flags(fp_status);
4370

    
4371
    /* Suppress any inexact exceptions the conversion produced */
4372
    if (!(old_flags & float_flag_inexact)) {
4373
        new_flags = get_float_exception_flags(fp_status);
4374
        set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
4375
    }
4376

    
4377
    return ret;
4378
}
4379

    
4380
/* Convert ARM rounding mode to softfloat */
4381
int arm_rmode_to_sf(int rmode)
4382
{
4383
    switch (rmode) {
4384
    case FPROUNDING_TIEAWAY:
4385
        rmode = float_round_ties_away;
4386
        break;
4387
    case FPROUNDING_ODD:
4388
        /* FIXME: add support for TIEAWAY and ODD */
4389
        qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
4390
                      rmode);
4391
    case FPROUNDING_TIEEVEN:
4392
    default:
4393
        rmode = float_round_nearest_even;
4394
        break;
4395
    case FPROUNDING_POSINF:
4396
        rmode = float_round_up;
4397
        break;
4398
    case FPROUNDING_NEGINF:
4399
        rmode = float_round_down;
4400
        break;
4401
    case FPROUNDING_ZERO:
4402
        rmode = float_round_to_zero;
4403
        break;
4404
    }
4405
    return rmode;
4406
}