Statistics
| Branch: | Revision:

root / target-arm / helper.c @ 51fb256a

History | View | Annotate | Download (129.9 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 raw_read(CPUARMState *env, const ARMCPRegInfo *ri,
69
                    uint64_t *value)
70
{
71
    if (ri->type & ARM_CP_64BIT) {
72
        *value = CPREG_FIELD64(env, ri);
73
    } else {
74
        *value = CPREG_FIELD32(env, ri);
75
    }
76
    return 0;
77
}
78

    
79
static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
80
                     uint64_t value)
81
{
82
    if (ri->type & ARM_CP_64BIT) {
83
        CPREG_FIELD64(env, ri) = value;
84
    } else {
85
        CPREG_FIELD32(env, ri) = value;
86
    }
87
    return 0;
88
}
89

    
90
static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
91
                            uint64_t *v)
92
{
93
    /* Raw read of a coprocessor register (as needed for migration, etc)
94
     * return true on success, false if the read is impossible for some reason.
95
     */
96
    if (ri->type & ARM_CP_CONST) {
97
        *v = ri->resetvalue;
98
    } else if (ri->raw_readfn) {
99
        return (ri->raw_readfn(env, ri, v) == 0);
100
    } else if (ri->readfn) {
101
        return (ri->readfn(env, ri, v) == 0);
102
    } else {
103
        if (ri->type & ARM_CP_64BIT) {
104
            *v = CPREG_FIELD64(env, ri);
105
        } else {
106
            *v = CPREG_FIELD32(env, ri);
107
        }
108
    }
109
    return true;
110
}
111

    
112
static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
113
                             int64_t v)
114
{
115
    /* Raw write of a coprocessor register (as needed for migration, etc).
116
     * Return true on success, false if the write is impossible for some reason.
117
     * Note that constant registers are treated as write-ignored; the
118
     * caller should check for success by whether a readback gives the
119
     * value written.
120
     */
121
    if (ri->type & ARM_CP_CONST) {
122
        return true;
123
    } else if (ri->raw_writefn) {
124
        return (ri->raw_writefn(env, ri, v) == 0);
125
    } else if (ri->writefn) {
126
        return (ri->writefn(env, ri, v) == 0);
127
    } else {
128
        if (ri->type & ARM_CP_64BIT) {
129
            CPREG_FIELD64(env, ri) = v;
130
        } else {
131
            CPREG_FIELD32(env, ri) = v;
132
        }
133
    }
134
    return true;
135
}
136

    
137
bool write_cpustate_to_list(ARMCPU *cpu)
138
{
139
    /* Write the coprocessor state from cpu->env to the (index,value) list. */
140
    int i;
141
    bool ok = true;
142

    
143
    for (i = 0; i < cpu->cpreg_array_len; i++) {
144
        uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
145
        const ARMCPRegInfo *ri;
146
        uint64_t v;
147
        ri = get_arm_cp_reginfo(cpu, regidx);
148
        if (!ri) {
149
            ok = false;
150
            continue;
151
        }
152
        if (ri->type & ARM_CP_NO_MIGRATE) {
153
            continue;
154
        }
155
        if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
156
            ok = false;
157
            continue;
158
        }
159
        cpu->cpreg_values[i] = v;
160
    }
161
    return ok;
162
}
163

    
164
bool write_list_to_cpustate(ARMCPU *cpu)
165
{
166
    int i;
167
    bool ok = true;
168

    
169
    for (i = 0; i < cpu->cpreg_array_len; i++) {
170
        uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171
        uint64_t v = cpu->cpreg_values[i];
172
        uint64_t readback;
173
        const ARMCPRegInfo *ri;
174

    
175
        ri = get_arm_cp_reginfo(cpu, regidx);
176
        if (!ri) {
177
            ok = false;
178
            continue;
179
        }
180
        if (ri->type & ARM_CP_NO_MIGRATE) {
181
            continue;
182
        }
183
        /* Write value and confirm it reads back as written
184
         * (to catch read-only registers and partially read-only
185
         * registers where the incoming migration value doesn't match)
186
         */
187
        if (!write_raw_cp_reg(&cpu->env, ri, v) ||
188
            !read_raw_cp_reg(&cpu->env, ri, &readback) ||
189
            readback != v) {
190
            ok = false;
191
        }
192
    }
193
    return ok;
194
}
195

    
196
static void add_cpreg_to_list(gpointer key, gpointer opaque)
197
{
198
    ARMCPU *cpu = opaque;
199
    uint64_t regidx;
200
    const ARMCPRegInfo *ri;
201

    
202
    regidx = *(uint32_t *)key;
203
    ri = get_arm_cp_reginfo(cpu, regidx);
204

    
205
    if (!(ri->type & ARM_CP_NO_MIGRATE)) {
206
        cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207
        /* The value array need not be initialized at this point */
208
        cpu->cpreg_array_len++;
209
    }
210
}
211

    
212
static void count_cpreg(gpointer key, gpointer opaque)
213
{
214
    ARMCPU *cpu = opaque;
215
    uint64_t regidx;
216
    const ARMCPRegInfo *ri;
217

    
218
    regidx = *(uint32_t *)key;
219
    ri = get_arm_cp_reginfo(cpu, regidx);
220

    
221
    if (!(ri->type & ARM_CP_NO_MIGRATE)) {
222
        cpu->cpreg_array_len++;
223
    }
224
}
225

    
226
static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
227
{
228
    uint32_t aidx = *(uint32_t *)a;
229
    uint32_t bidx = *(uint32_t *)b;
230

    
231
    return aidx - bidx;
232
}
233

    
234
static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
235
{
236
    GList **plist = udata;
237

    
238
    *plist = g_list_prepend(*plist, key);
239
}
240

    
241
void init_cpreg_list(ARMCPU *cpu)
242
{
243
    /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
244
     * Note that we require cpreg_tuples[] to be sorted by key ID.
245
     */
246
    GList *keys = NULL;
247
    int arraylen;
248

    
249
    g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
250

    
251
    keys = g_list_sort(keys, cpreg_key_compare);
252

    
253
    cpu->cpreg_array_len = 0;
254

    
255
    g_list_foreach(keys, count_cpreg, cpu);
256

    
257
    arraylen = cpu->cpreg_array_len;
258
    cpu->cpreg_indexes = g_new(uint64_t, arraylen);
259
    cpu->cpreg_values = g_new(uint64_t, arraylen);
260
    cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
261
    cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
262
    cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
263
    cpu->cpreg_array_len = 0;
264

    
265
    g_list_foreach(keys, add_cpreg_to_list, cpu);
266

    
267
    assert(cpu->cpreg_array_len == arraylen);
268

    
269
    g_list_free(keys);
270
}
271

    
272
static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
273
{
274
    env->cp15.c3 = value;
275
    tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
276
    return 0;
277
}
278

    
279
static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
280
{
281
    if (env->cp15.c13_fcse != value) {
282
        /* Unlike real hardware the qemu TLB uses virtual addresses,
283
         * not modified virtual addresses, so this causes a TLB flush.
284
         */
285
        tlb_flush(env, 1);
286
        env->cp15.c13_fcse = value;
287
    }
288
    return 0;
289
}
290
static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
291
                            uint64_t value)
292
{
293
    if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
294
        /* For VMSA (when not using the LPAE long descriptor page table
295
         * format) this register includes the ASID, so do a TLB flush.
296
         * For PMSA it is purely a process ID and no action is needed.
297
         */
298
        tlb_flush(env, 1);
299
    }
300
    env->cp15.c13_context = value;
301
    return 0;
302
}
303

    
304
static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
305
                         uint64_t value)
306
{
307
    /* Invalidate all (TLBIALL) */
308
    tlb_flush(env, 1);
309
    return 0;
310
}
311

    
312
static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
313
                         uint64_t value)
314
{
315
    /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
316
    tlb_flush_page(env, value & TARGET_PAGE_MASK);
317
    return 0;
318
}
319

    
320
static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
321
                          uint64_t value)
322
{
323
    /* Invalidate by ASID (TLBIASID) */
324
    tlb_flush(env, value == 0);
325
    return 0;
326
}
327

    
328
static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
329
                          uint64_t value)
330
{
331
    /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
332
    tlb_flush_page(env, value & TARGET_PAGE_MASK);
333
    return 0;
334
}
335

    
336
static const ARMCPRegInfo cp_reginfo[] = {
337
    /* DBGDIDR: just RAZ. In particular this means the "debug architecture
338
     * version" bits will read as a reserved value, which should cause
339
     * Linux to not try to use the debug hardware.
340
     */
341
    { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
342
      .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
343
    /* MMU Domain access control / MPU write buffer control */
344
    { .name = "DACR", .cp = 15,
345
      .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
346
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
347
      .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
348
    { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
349
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
350
      .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
351
    { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
352
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
353
      .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
354
    /* ??? This covers not just the impdef TLB lockdown registers but also
355
     * some v7VMSA registers relating to TEX remap, so it is overly broad.
356
     */
357
    { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
358
      .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
359
    /* MMU TLB control. Note that the wildcarding means we cover not just
360
     * the unified TLB ops but also the dside/iside/inner-shareable variants.
361
     */
362
    { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
363
      .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
364
      .type = ARM_CP_NO_MIGRATE },
365
    { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
366
      .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
367
      .type = ARM_CP_NO_MIGRATE },
368
    { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
369
      .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
370
      .type = ARM_CP_NO_MIGRATE },
371
    { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
372
      .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
373
      .type = ARM_CP_NO_MIGRATE },
374
    /* Cache maintenance ops; some of this space may be overridden later. */
375
    { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
376
      .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
377
      .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
378
    REGINFO_SENTINEL
379
};
380

    
381
static const ARMCPRegInfo not_v6_cp_reginfo[] = {
382
    /* Not all pre-v6 cores implemented this WFI, so this is slightly
383
     * over-broad.
384
     */
385
    { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
386
      .access = PL1_W, .type = ARM_CP_WFI },
387
    REGINFO_SENTINEL
388
};
389

    
390
static const ARMCPRegInfo not_v7_cp_reginfo[] = {
391
    /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
392
     * is UNPREDICTABLE; we choose to NOP as most implementations do).
393
     */
394
    { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
395
      .access = PL1_W, .type = ARM_CP_WFI },
396
    /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
397
     * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
398
     * OMAPCP will override this space.
399
     */
400
    { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
401
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
402
      .resetvalue = 0 },
403
    { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
404
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
405
      .resetvalue = 0 },
406
    /* v6 doesn't have the cache ID registers but Linux reads them anyway */
407
    { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
408
      .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
409
      .resetvalue = 0 },
410
    REGINFO_SENTINEL
411
};
412

    
413
static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
414
{
415
    if (env->cp15.c1_coproc != value) {
416
        env->cp15.c1_coproc = value;
417
        /* ??? Is this safe when called from within a TB?  */
418
        tb_flush(env);
419
    }
420
    return 0;
421
}
422

    
423
static const ARMCPRegInfo v6_cp_reginfo[] = {
424
    /* prefetch by MVA in v6, NOP in v7 */
425
    { .name = "MVA_prefetch",
426
      .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
427
      .access = PL1_W, .type = ARM_CP_NOP },
428
    { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
429
      .access = PL0_W, .type = ARM_CP_NOP },
430
    { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
431
      .access = PL0_W, .type = ARM_CP_NOP },
432
    { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
433
      .access = PL0_W, .type = ARM_CP_NOP },
434
    { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
435
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
436
      .resetvalue = 0, },
437
    /* Watchpoint Fault Address Register : should actually only be present
438
     * for 1136, 1176, 11MPCore.
439
     */
440
    { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
441
      .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
442
    { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
443
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
444
      .resetvalue = 0, .writefn = cpacr_write },
445
    REGINFO_SENTINEL
446
};
447

    
448

    
449
static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
450
                      uint64_t *value)
451
{
452
    /* Generic performance monitor register read function for where
453
     * user access may be allowed by PMUSERENR.
454
     */
455
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
456
        return EXCP_UDEF;
457
    }
458
    *value = CPREG_FIELD32(env, ri);
459
    return 0;
460
}
461

    
462
static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
463
                      uint64_t value)
464
{
465
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
466
        return EXCP_UDEF;
467
    }
468
    /* only the DP, X, D and E bits are writable */
469
    env->cp15.c9_pmcr &= ~0x39;
470
    env->cp15.c9_pmcr |= (value & 0x39);
471
    return 0;
472
}
473

    
474
static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
475
                            uint64_t value)
476
{
477
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
478
        return EXCP_UDEF;
479
    }
480
    value &= (1 << 31);
481
    env->cp15.c9_pmcnten |= value;
482
    return 0;
483
}
484

    
485
static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
486
                            uint64_t value)
487
{
488
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
489
        return EXCP_UDEF;
490
    }
491
    value &= (1 << 31);
492
    env->cp15.c9_pmcnten &= ~value;
493
    return 0;
494
}
495

    
496
static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
497
                        uint64_t value)
498
{
499
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
500
        return EXCP_UDEF;
501
    }
502
    env->cp15.c9_pmovsr &= ~value;
503
    return 0;
504
}
505

    
506
static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
507
                            uint64_t value)
508
{
509
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
510
        return EXCP_UDEF;
511
    }
512
    env->cp15.c9_pmxevtyper = value & 0xff;
513
    return 0;
514
}
515

    
516
static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
517
                            uint64_t value)
518
{
519
    env->cp15.c9_pmuserenr = value & 1;
520
    return 0;
521
}
522

    
523
static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
524
                            uint64_t value)
525
{
526
    /* We have no event counters so only the C bit can be changed */
527
    value &= (1 << 31);
528
    env->cp15.c9_pminten |= value;
529
    return 0;
530
}
531

    
532
static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
533
                            uint64_t value)
534
{
535
    value &= (1 << 31);
536
    env->cp15.c9_pminten &= ~value;
537
    return 0;
538
}
539

    
540
static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
541
                       uint64_t *value)
542
{
543
    ARMCPU *cpu = arm_env_get_cpu(env);
544
    *value = cpu->ccsidr[env->cp15.c0_cssel];
545
    return 0;
546
}
547

    
548
static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
549
                        uint64_t value)
550
{
551
    env->cp15.c0_cssel = value & 0xf;
552
    return 0;
553
}
554

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

    
641
static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
642
{
643
    value &= 1;
644
    env->teecr = value;
645
    return 0;
646
}
647

    
648
static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
649
                       uint64_t *value)
650
{
651
    /* This is a helper function because the user access rights
652
     * depend on the value of the TEECR.
653
     */
654
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
655
        return EXCP_UDEF;
656
    }
657
    *value = env->teehbr;
658
    return 0;
659
}
660

    
661
static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
662
                        uint64_t value)
663
{
664
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
665
        return EXCP_UDEF;
666
    }
667
    env->teehbr = value;
668
    return 0;
669
}
670

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

    
683
static const ARMCPRegInfo v6k_cp_reginfo[] = {
684
    { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
685
      .access = PL0_RW,
686
      .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
687
      .resetvalue = 0 },
688
    { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
689
      .access = PL0_R|PL1_W,
690
      .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
691
      .resetvalue = 0 },
692
    { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
693
      .access = PL1_RW,
694
      .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
695
      .resetvalue = 0 },
696
    REGINFO_SENTINEL
697
};
698

    
699
#ifndef CONFIG_USER_ONLY
700

    
701
static uint64_t gt_get_countervalue(CPUARMState *env)
702
{
703
    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
704
}
705

    
706
static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
707
{
708
    ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
709

    
710
    if (gt->ctl & 1) {
711
        /* Timer enabled: calculate and set current ISTATUS, irq, and
712
         * reset timer to when ISTATUS next has to change
713
         */
714
        uint64_t count = gt_get_countervalue(&cpu->env);
715
        /* Note that this must be unsigned 64 bit arithmetic: */
716
        int istatus = count >= gt->cval;
717
        uint64_t nexttick;
718

    
719
        gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
720
        qemu_set_irq(cpu->gt_timer_outputs[timeridx],
721
                     (istatus && !(gt->ctl & 2)));
722
        if (istatus) {
723
            /* Next transition is when count rolls back over to zero */
724
            nexttick = UINT64_MAX;
725
        } else {
726
            /* Next transition is when we hit cval */
727
            nexttick = gt->cval;
728
        }
729
        /* Note that the desired next expiry time might be beyond the
730
         * signed-64-bit range of a QEMUTimer -- in this case we just
731
         * set the timer for as far in the future as possible. When the
732
         * timer expires we will reset the timer for any remaining period.
733
         */
734
        if (nexttick > INT64_MAX / GTIMER_SCALE) {
735
            nexttick = INT64_MAX / GTIMER_SCALE;
736
        }
737
        timer_mod(cpu->gt_timer[timeridx], nexttick);
738
    } else {
739
        /* Timer disabled: ISTATUS and timer output always clear */
740
        gt->ctl &= ~4;
741
        qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
742
        timer_del(cpu->gt_timer[timeridx]);
743
    }
744
}
745

    
746
static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri,
747
                          uint64_t *value)
748
{
749
    /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
750
    if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
751
        return EXCP_UDEF;
752
    }
753
    *value = env->cp15.c14_cntfrq;
754
    return 0;
755
}
756

    
757
static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
758
{
759
    ARMCPU *cpu = arm_env_get_cpu(env);
760
    int timeridx = ri->opc1 & 1;
761

    
762
    timer_del(cpu->gt_timer[timeridx]);
763
}
764

    
765
static int gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri,
766
                       uint64_t *value)
767
{
768
    int timeridx = ri->opc1 & 1;
769

    
770
    if (arm_current_pl(env) == 0 &&
771
        !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
772
        return EXCP_UDEF;
773
    }
774
    *value = gt_get_countervalue(env);
775
    return 0;
776
}
777

    
778
static int gt_cval_read(CPUARMState *env, const ARMCPRegInfo *ri,
779
                        uint64_t *value)
780
{
781
    int timeridx = ri->opc1 & 1;
782

    
783
    if (arm_current_pl(env) == 0 &&
784
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
785
        return EXCP_UDEF;
786
    }
787
    *value = env->cp15.c14_timer[timeridx].cval;
788
    return 0;
789
}
790

    
791
static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
792
                         uint64_t value)
793
{
794
    int timeridx = ri->opc1 & 1;
795

    
796
    env->cp15.c14_timer[timeridx].cval = value;
797
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
798
    return 0;
799
}
800
static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
801
                        uint64_t *value)
802
{
803
    int timeridx = ri->crm & 1;
804

    
805
    if (arm_current_pl(env) == 0 &&
806
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
807
        return EXCP_UDEF;
808
    }
809
    *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval -
810
                        gt_get_countervalue(env));
811
    return 0;
812
}
813

    
814
static int gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
815
                         uint64_t value)
816
{
817
    int timeridx = ri->crm & 1;
818

    
819
    env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
820
        + sextract64(value, 0, 32);
821
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
822
    return 0;
823
}
824

    
825
static int gt_ctl_read(CPUARMState *env, const ARMCPRegInfo *ri,
826
                       uint64_t *value)
827
{
828
    int timeridx = ri->crm & 1;
829

    
830
    if (arm_current_pl(env) == 0 &&
831
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
832
        return EXCP_UDEF;
833
    }
834
    *value = env->cp15.c14_timer[timeridx].ctl;
835
    return 0;
836
}
837

    
838
static int gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
839
                        uint64_t value)
840
{
841
    ARMCPU *cpu = arm_env_get_cpu(env);
842
    int timeridx = ri->crm & 1;
843
    uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
844

    
845
    env->cp15.c14_timer[timeridx].ctl = value & 3;
846
    if ((oldval ^ value) & 1) {
847
        /* Enable toggled */
848
        gt_recalc_timer(cpu, timeridx);
849
    } else if ((oldval & value) & 2) {
850
        /* IMASK toggled: don't need to recalculate,
851
         * just set the interrupt line based on ISTATUS
852
         */
853
        qemu_set_irq(cpu->gt_timer_outputs[timeridx],
854
                     (oldval & 4) && (value & 2));
855
    }
856
    return 0;
857
}
858

    
859
void arm_gt_ptimer_cb(void *opaque)
860
{
861
    ARMCPU *cpu = opaque;
862

    
863
    gt_recalc_timer(cpu, GTIMER_PHYS);
864
}
865

    
866
void arm_gt_vtimer_cb(void *opaque)
867
{
868
    ARMCPU *cpu = opaque;
869

    
870
    gt_recalc_timer(cpu, GTIMER_VIRT);
871
}
872

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

    
943
#else
944
/* In user-mode none of the generic timer registers are accessible,
945
 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
946
 * so instead just don't register any of them.
947
 */
948
static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
949
    REGINFO_SENTINEL
950
};
951

    
952
#endif
953

    
954
static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
955
{
956
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
957
        env->cp15.c7_par = value;
958
    } else if (arm_feature(env, ARM_FEATURE_V7)) {
959
        env->cp15.c7_par = value & 0xfffff6ff;
960
    } else {
961
        env->cp15.c7_par = value & 0xfffff1ff;
962
    }
963
    return 0;
964
}
965

    
966
#ifndef CONFIG_USER_ONLY
967
/* get_phys_addr() isn't present for user-mode-only targets */
968

    
969
/* Return true if extended addresses are enabled, ie this is an
970
 * LPAE implementation and we are using the long-descriptor translation
971
 * table format because the TTBCR EAE bit is set.
972
 */
973
static inline bool extended_addresses_enabled(CPUARMState *env)
974
{
975
    return arm_feature(env, ARM_FEATURE_LPAE)
976
        && (env->cp15.c2_control & (1U << 31));
977
}
978

    
979
static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
980
{
981
    hwaddr phys_addr;
982
    target_ulong page_size;
983
    int prot;
984
    int ret, is_user = ri->opc2 & 2;
985
    int access_type = ri->opc2 & 1;
986

    
987
    if (ri->opc2 & 4) {
988
        /* Other states are only available with TrustZone */
989
        return EXCP_UDEF;
990
    }
991
    ret = get_phys_addr(env, value, access_type, is_user,
992
                        &phys_addr, &prot, &page_size);
993
    if (extended_addresses_enabled(env)) {
994
        /* ret is a DFSR/IFSR value for the long descriptor
995
         * translation table format, but with WnR always clear.
996
         * Convert it to a 64-bit PAR.
997
         */
998
        uint64_t par64 = (1 << 11); /* LPAE bit always set */
999
        if (ret == 0) {
1000
            par64 |= phys_addr & ~0xfffULL;
1001
            /* We don't set the ATTR or SH fields in the PAR. */
1002
        } else {
1003
            par64 |= 1; /* F */
1004
            par64 |= (ret & 0x3f) << 1; /* FS */
1005
            /* Note that S2WLK and FSTAGE are always zero, because we don't
1006
             * implement virtualization and therefore there can't be a stage 2
1007
             * fault.
1008
             */
1009
        }
1010
        env->cp15.c7_par = par64;
1011
        env->cp15.c7_par_hi = par64 >> 32;
1012
    } else {
1013
        /* ret is a DFSR/IFSR value for the short descriptor
1014
         * translation table format (with WnR always clear).
1015
         * Convert it to a 32-bit PAR.
1016
         */
1017
        if (ret == 0) {
1018
            /* We do not set any attribute bits in the PAR */
1019
            if (page_size == (1 << 24)
1020
                && arm_feature(env, ARM_FEATURE_V7)) {
1021
                env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1022
            } else {
1023
                env->cp15.c7_par = phys_addr & 0xfffff000;
1024
            }
1025
        } else {
1026
            env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1027
                ((ret & (12 << 1)) >> 6) |
1028
                ((ret & 0xf) << 1) | 1;
1029
        }
1030
        env->cp15.c7_par_hi = 0;
1031
    }
1032
    return 0;
1033
}
1034
#endif
1035

    
1036
static const ARMCPRegInfo vapa_cp_reginfo[] = {
1037
    { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1038
      .access = PL1_RW, .resetvalue = 0,
1039
      .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
1040
      .writefn = par_write },
1041
#ifndef CONFIG_USER_ONLY
1042
    { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1043
      .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1044
#endif
1045
    REGINFO_SENTINEL
1046
};
1047

    
1048
/* Return basic MPU access permission bits.  */
1049
static uint32_t simple_mpu_ap_bits(uint32_t val)
1050
{
1051
    uint32_t ret;
1052
    uint32_t mask;
1053
    int i;
1054
    ret = 0;
1055
    mask = 3;
1056
    for (i = 0; i < 16; i += 2) {
1057
        ret |= (val >> i) & mask;
1058
        mask <<= 2;
1059
    }
1060
    return ret;
1061
}
1062

    
1063
/* Pad basic MPU access permission bits to extended format.  */
1064
static uint32_t extended_mpu_ap_bits(uint32_t val)
1065
{
1066
    uint32_t ret;
1067
    uint32_t mask;
1068
    int i;
1069
    ret = 0;
1070
    mask = 3;
1071
    for (i = 0; i < 16; i += 2) {
1072
        ret |= (val & mask) << i;
1073
        mask <<= 2;
1074
    }
1075
    return ret;
1076
}
1077

    
1078
static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1079
                                uint64_t value)
1080
{
1081
    env->cp15.c5_data = extended_mpu_ap_bits(value);
1082
    return 0;
1083
}
1084

    
1085
static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1086
                               uint64_t *value)
1087
{
1088
    *value = simple_mpu_ap_bits(env->cp15.c5_data);
1089
    return 0;
1090
}
1091

    
1092
static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1093
                                uint64_t value)
1094
{
1095
    env->cp15.c5_insn = extended_mpu_ap_bits(value);
1096
    return 0;
1097
}
1098

    
1099
static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1100
                               uint64_t *value)
1101
{
1102
    *value = simple_mpu_ap_bits(env->cp15.c5_insn);
1103
    return 0;
1104
}
1105

    
1106
static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
1107
                            uint64_t *value)
1108
{
1109
    if (ri->crm >= 8) {
1110
        return EXCP_UDEF;
1111
    }
1112
    *value = env->cp15.c6_region[ri->crm];
1113
    return 0;
1114
}
1115

    
1116
static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
1117
                             uint64_t value)
1118
{
1119
    if (ri->crm >= 8) {
1120
        return EXCP_UDEF;
1121
    }
1122
    env->cp15.c6_region[ri->crm] = value;
1123
    return 0;
1124
}
1125

    
1126
static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1127
    { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1128
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1129
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
1130
      .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1131
    { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1132
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1133
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
1134
      .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1135
    { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1136
      .access = PL1_RW,
1137
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1138
    { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1139
      .access = PL1_RW,
1140
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1141
    { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1142
      .access = PL1_RW,
1143
      .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1144
    { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1145
      .access = PL1_RW,
1146
      .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1147
    /* Protection region base and size registers */
1148
    { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
1149
      .opc2 = CP_ANY, .access = PL1_RW,
1150
      .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
1151
    REGINFO_SENTINEL
1152
};
1153

    
1154
static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1155
                                uint64_t value)
1156
{
1157
    int maskshift = extract32(value, 0, 3);
1158

    
1159
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1160
        value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1161
    } else {
1162
        value &= 7;
1163
    }
1164
    /* Note that we always calculate c2_mask and c2_base_mask, but
1165
     * they are only used for short-descriptor tables (ie if EAE is 0);
1166
     * for long-descriptor tables the TTBCR fields are used differently
1167
     * and the c2_mask and c2_base_mask values are meaningless.
1168
     */
1169
    env->cp15.c2_control = value;
1170
    env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1171
    env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
1172
    return 0;
1173
}
1174

    
1175
static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1176
                            uint64_t value)
1177
{
1178
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1179
        /* With LPAE the TTBCR could result in a change of ASID
1180
         * via the TTBCR.A1 bit, so do a TLB flush.
1181
         */
1182
        tlb_flush(env, 1);
1183
    }
1184
    return vmsa_ttbcr_raw_write(env, ri, value);
1185
}
1186

    
1187
static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1188
{
1189
    env->cp15.c2_base_mask = 0xffffc000u;
1190
    env->cp15.c2_control = 0;
1191
    env->cp15.c2_mask = 0;
1192
}
1193

    
1194
static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1195
    { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1196
      .access = PL1_RW,
1197
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1198
    { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1199
      .access = PL1_RW,
1200
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1201
    { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1202
      .access = PL1_RW,
1203
      .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
1204
    { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1205
      .access = PL1_RW,
1206
      .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
1207
    { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1208
      .access = PL1_RW, .writefn = vmsa_ttbcr_write,
1209
      .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
1210
      .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
1211
    { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1212
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
1213
      .resetvalue = 0, },
1214
    REGINFO_SENTINEL
1215
};
1216

    
1217
static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1218
                               uint64_t value)
1219
{
1220
    env->cp15.c15_ticonfig = value & 0xe7;
1221
    /* The OS_TYPE bit in this register changes the reported CPUID! */
1222
    env->cp15.c0_cpuid = (value & (1 << 5)) ?
1223
        ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1224
    return 0;
1225
}
1226

    
1227
static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1228
                               uint64_t value)
1229
{
1230
    env->cp15.c15_threadid = value & 0xffff;
1231
    return 0;
1232
}
1233

    
1234
static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1235
                          uint64_t value)
1236
{
1237
    /* Wait-for-interrupt (deprecated) */
1238
    cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1239
    return 0;
1240
}
1241

    
1242
static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1243
                                 uint64_t value)
1244
{
1245
    /* On OMAP there are registers indicating the max/min index of dcache lines
1246
     * containing a dirty line; cache flush operations have to reset these.
1247
     */
1248
    env->cp15.c15_i_max = 0x000;
1249
    env->cp15.c15_i_min = 0xff0;
1250
    return 0;
1251
}
1252

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

    
1292
static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1293
                             uint64_t value)
1294
{
1295
    value &= 0x3fff;
1296
    if (env->cp15.c15_cpar != value) {
1297
        /* Changes cp0 to cp13 behavior, so needs a TB flush.  */
1298
        tb_flush(env);
1299
        env->cp15.c15_cpar = value;
1300
    }
1301
    return 0;
1302
}
1303

    
1304
static const ARMCPRegInfo xscale_cp_reginfo[] = {
1305
    { .name = "XSCALE_CPAR",
1306
      .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1307
      .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1308
      .writefn = xscale_cpar_write, },
1309
    { .name = "XSCALE_AUXCR",
1310
      .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1311
      .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1312
      .resetvalue = 0, },
1313
    REGINFO_SENTINEL
1314
};
1315

    
1316
static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1317
    /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1318
     * implementation of this implementation-defined space.
1319
     * Ideally this should eventually disappear in favour of actually
1320
     * implementing the correct behaviour for all cores.
1321
     */
1322
    { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1323
      .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1324
      .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1325
      .resetvalue = 0 },
1326
    REGINFO_SENTINEL
1327
};
1328

    
1329
static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1330
    /* Cache status: RAZ because we have no cache so it's always clean */
1331
    { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1332
      .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1333
      .resetvalue = 0 },
1334
    REGINFO_SENTINEL
1335
};
1336

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

    
1358
static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1359
    /* The cache test-and-clean instructions always return (1 << 30)
1360
     * to indicate that there are no dirty cache lines.
1361
     */
1362
    { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
1363
      .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1364
      .resetvalue = (1 << 30) },
1365
    { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
1366
      .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1367
      .resetvalue = (1 << 30) },
1368
    REGINFO_SENTINEL
1369
};
1370

    
1371
static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1372
    /* Ignore ReadBuffer accesses */
1373
    { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1374
      .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1375
      .access = PL1_RW, .resetvalue = 0,
1376
      .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
1377
    REGINFO_SENTINEL
1378
};
1379

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

    
1400
static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1401
    { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1402
      .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1403
    REGINFO_SENTINEL
1404
};
1405

    
1406
static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1407
{
1408
    *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1409
    return 0;
1410
}
1411

    
1412
static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1413
{
1414
    env->cp15.c7_par_hi = value >> 32;
1415
    env->cp15.c7_par = value;
1416
    return 0;
1417
}
1418

    
1419
static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1420
{
1421
    env->cp15.c7_par_hi = 0;
1422
    env->cp15.c7_par = 0;
1423
}
1424

    
1425
static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1426
                        uint64_t *value)
1427
{
1428
    *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1429
    return 0;
1430
}
1431

    
1432
static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1433
                             uint64_t value)
1434
{
1435
    env->cp15.c2_base0_hi = value >> 32;
1436
    env->cp15.c2_base0 = value;
1437
    return 0;
1438
}
1439

    
1440
static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1441
                         uint64_t value)
1442
{
1443
    /* Writes to the 64 bit format TTBRs may change the ASID */
1444
    tlb_flush(env, 1);
1445
    return ttbr064_raw_write(env, ri, value);
1446
}
1447

    
1448
static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1449
{
1450
    env->cp15.c2_base0_hi = 0;
1451
    env->cp15.c2_base0 = 0;
1452
}
1453

    
1454
static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1455
                        uint64_t *value)
1456
{
1457
    *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1458
    return 0;
1459
}
1460

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

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

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

    
1503
static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1504
{
1505
    env->cp15.c1_sys = value;
1506
    /* ??? Lots of these bits are not implemented.  */
1507
    /* This may enable/disable the MMU, so do a TLB flush.  */
1508
    tlb_flush(env, 1);
1509
    return 0;
1510
}
1511

    
1512
void register_cp_regs_for_features(ARMCPU *cpu)
1513
{
1514
    /* Register all the coprocessor registers based on feature bits */
1515
    CPUARMState *env = &cpu->env;
1516
    if (arm_feature(env, ARM_FEATURE_M)) {
1517
        /* M profile has no coprocessor registers */
1518
        return;
1519
    }
1520

    
1521
    define_arm_cp_regs(cpu, cp_reginfo);
1522
    if (arm_feature(env, ARM_FEATURE_V6)) {
1523
        /* The ID registers all have impdef reset values */
1524
        ARMCPRegInfo v6_idregs[] = {
1525
            { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1,
1526
              .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1527
              .resetvalue = cpu->id_pfr0 },
1528
            { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1,
1529
              .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1530
              .resetvalue = cpu->id_pfr1 },
1531
            { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1,
1532
              .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1533
              .resetvalue = cpu->id_dfr0 },
1534
            { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1,
1535
              .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1536
              .resetvalue = cpu->id_afr0 },
1537
            { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1,
1538
              .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1539
              .resetvalue = cpu->id_mmfr0 },
1540
            { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1,
1541
              .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1542
              .resetvalue = cpu->id_mmfr1 },
1543
            { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1,
1544
              .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1545
              .resetvalue = cpu->id_mmfr2 },
1546
            { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1,
1547
              .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1548
              .resetvalue = cpu->id_mmfr3 },
1549
            { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2,
1550
              .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST,
1551
              .resetvalue = cpu->id_isar0 },
1552
            { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2,
1553
              .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
1554
              .resetvalue = cpu->id_isar1 },
1555
            { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2,
1556
              .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST,
1557
              .resetvalue = cpu->id_isar2 },
1558
            { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2,
1559
              .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST,
1560
              .resetvalue = cpu->id_isar3 },
1561
            { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2,
1562
              .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST,
1563
              .resetvalue = cpu->id_isar4 },
1564
            { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2,
1565
              .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST,
1566
              .resetvalue = cpu->id_isar5 },
1567
            /* 6..7 are as yet unallocated and must RAZ */
1568
            { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
1569
              .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
1570
              .resetvalue = 0 },
1571
            { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
1572
              .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
1573
              .resetvalue = 0 },
1574
            REGINFO_SENTINEL
1575
        };
1576
        define_arm_cp_regs(cpu, v6_idregs);
1577
        define_arm_cp_regs(cpu, v6_cp_reginfo);
1578
    } else {
1579
        define_arm_cp_regs(cpu, not_v6_cp_reginfo);
1580
    }
1581
    if (arm_feature(env, ARM_FEATURE_V6K)) {
1582
        define_arm_cp_regs(cpu, v6k_cp_reginfo);
1583
    }
1584
    if (arm_feature(env, ARM_FEATURE_V7)) {
1585
        /* v7 performance monitor control register: same implementor
1586
         * field as main ID register, and we implement no event counters.
1587
         */
1588
        ARMCPRegInfo pmcr = {
1589
            .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1590
            .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
1591
            .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
1592
            .readfn = pmreg_read, .writefn = pmcr_write,
1593
            .raw_readfn = raw_read, .raw_writefn = raw_write,
1594
        };
1595
        ARMCPRegInfo clidr = {
1596
            .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1597
            .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1598
        };
1599
        define_one_arm_cp_reg(cpu, &pmcr);
1600
        define_one_arm_cp_reg(cpu, &clidr);
1601
        define_arm_cp_regs(cpu, v7_cp_reginfo);
1602
    } else {
1603
        define_arm_cp_regs(cpu, not_v7_cp_reginfo);
1604
    }
1605
    if (arm_feature(env, ARM_FEATURE_MPU)) {
1606
        /* These are the MPU registers prior to PMSAv6. Any new
1607
         * PMSA core later than the ARM946 will require that we
1608
         * implement the PMSAv6 or PMSAv7 registers, which are
1609
         * completely different.
1610
         */
1611
        assert(!arm_feature(env, ARM_FEATURE_V6));
1612
        define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
1613
    } else {
1614
        define_arm_cp_regs(cpu, vmsa_cp_reginfo);
1615
    }
1616
    if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
1617
        define_arm_cp_regs(cpu, t2ee_cp_reginfo);
1618
    }
1619
    if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1620
        define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
1621
    }
1622
    if (arm_feature(env, ARM_FEATURE_VAPA)) {
1623
        define_arm_cp_regs(cpu, vapa_cp_reginfo);
1624
    }
1625
    if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
1626
        define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
1627
    }
1628
    if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
1629
        define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
1630
    }
1631
    if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
1632
        define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
1633
    }
1634
    if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1635
        define_arm_cp_regs(cpu, omap_cp_reginfo);
1636
    }
1637
    if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
1638
        define_arm_cp_regs(cpu, strongarm_cp_reginfo);
1639
    }
1640
    if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1641
        define_arm_cp_regs(cpu, xscale_cp_reginfo);
1642
    }
1643
    if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
1644
        define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
1645
    }
1646
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1647
        define_arm_cp_regs(cpu, lpae_cp_reginfo);
1648
    }
1649
    /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1650
     * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1651
     * be read-only (ie write causes UNDEF exception).
1652
     */
1653
    {
1654
        ARMCPRegInfo id_cp_reginfo[] = {
1655
            /* Note that the MIDR isn't a simple constant register because
1656
             * of the TI925 behaviour where writes to another register can
1657
             * cause the MIDR value to change.
1658
             *
1659
             * Unimplemented registers in the c15 0 0 0 space default to
1660
             * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
1661
             * and friends override accordingly.
1662
             */
1663
            { .name = "MIDR",
1664
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
1665
              .access = PL1_R, .resetvalue = cpu->midr,
1666
              .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
1667
              .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1668
              .type = ARM_CP_OVERRIDE },
1669
            { .name = "CTR",
1670
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1671
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1672
            { .name = "TCMTR",
1673
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1674
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1675
            { .name = "TLBTR",
1676
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
1677
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1678
            /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
1679
            { .name = "DUMMY",
1680
              .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
1681
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1682
            { .name = "DUMMY",
1683
              .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
1684
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1685
            { .name = "DUMMY",
1686
              .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
1687
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1688
            { .name = "DUMMY",
1689
              .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
1690
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1691
            { .name = "DUMMY",
1692
              .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
1693
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1694
            REGINFO_SENTINEL
1695
        };
1696
        ARMCPRegInfo crn0_wi_reginfo = {
1697
            .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
1698
            .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
1699
            .type = ARM_CP_NOP | ARM_CP_OVERRIDE
1700
        };
1701
        if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
1702
            arm_feature(env, ARM_FEATURE_STRONGARM)) {
1703
            ARMCPRegInfo *r;
1704
            /* Register the blanket "writes ignored" value first to cover the
1705
             * whole space. Then update the specific ID registers to allow write
1706
             * access, so that they ignore writes rather than causing them to
1707
             * UNDEF.
1708
             */
1709
            define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
1710
            for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
1711
                r->access = PL1_RW;
1712
            }
1713
        }
1714
        define_arm_cp_regs(cpu, id_cp_reginfo);
1715
    }
1716

    
1717
    if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1718
        define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1719
    }
1720

    
1721
    if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1722
        ARMCPRegInfo auxcr = {
1723
            .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1724
            .access = PL1_RW, .type = ARM_CP_CONST,
1725
            .resetvalue = cpu->reset_auxcr
1726
        };
1727
        define_one_arm_cp_reg(cpu, &auxcr);
1728
    }
1729

    
1730
    /* Generic registers whose values depend on the implementation */
1731
    {
1732
        ARMCPRegInfo sctlr = {
1733
            .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1734
            .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
1735
            .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1736
            .raw_writefn = raw_write,
1737
        };
1738
        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1739
            /* Normally we would always end the TB on an SCTLR write, but Linux
1740
             * arch/arm/mach-pxa/sleep.S expects two instructions following
1741
             * an MMU enable to execute from cache.  Imitate this behaviour.
1742
             */
1743
            sctlr.type |= ARM_CP_SUPPRESS_TB_END;
1744
        }
1745
        define_one_arm_cp_reg(cpu, &sctlr);
1746
    }
1747
}
1748

    
1749
ARMCPU *cpu_arm_init(const char *cpu_model)
1750
{
1751
    ARMCPU *cpu;
1752
    ObjectClass *oc;
1753

    
1754
    oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1755
    if (!oc) {
1756
        return NULL;
1757
    }
1758
    cpu = ARM_CPU(object_new(object_class_get_name(oc)));
1759

    
1760
    /* TODO this should be set centrally, once possible */
1761
    object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
1762

    
1763
    return cpu;
1764
}
1765

    
1766
void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1767
{
1768
    CPUState *cs = CPU(cpu);
1769
    CPUARMState *env = &cpu->env;
1770

    
1771
    if (arm_feature(env, ARM_FEATURE_NEON)) {
1772
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1773
                                 51, "arm-neon.xml", 0);
1774
    } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
1775
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1776
                                 35, "arm-vfp3.xml", 0);
1777
    } else if (arm_feature(env, ARM_FEATURE_VFP)) {
1778
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1779
                                 19, "arm-vfp.xml", 0);
1780
    }
1781
}
1782

    
1783
/* Sort alphabetically by type name, except for "any". */
1784
static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
1785
{
1786
    ObjectClass *class_a = (ObjectClass *)a;
1787
    ObjectClass *class_b = (ObjectClass *)b;
1788
    const char *name_a, *name_b;
1789

    
1790
    name_a = object_class_get_name(class_a);
1791
    name_b = object_class_get_name(class_b);
1792
    if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
1793
        return 1;
1794
    } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
1795
        return -1;
1796
    } else {
1797
        return strcmp(name_a, name_b);
1798
    }
1799
}
1800

    
1801
static void arm_cpu_list_entry(gpointer data, gpointer user_data)
1802
{
1803
    ObjectClass *oc = data;
1804
    CPUListState *s = user_data;
1805
    const char *typename;
1806
    char *name;
1807

    
1808
    typename = object_class_get_name(oc);
1809
    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
1810
    (*s->cpu_fprintf)(s->file, "  %s\n",
1811
                      name);
1812
    g_free(name);
1813
}
1814

    
1815
void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1816
{
1817
    CPUListState s = {
1818
        .file = f,
1819
        .cpu_fprintf = cpu_fprintf,
1820
    };
1821
    GSList *list;
1822

    
1823
    list = object_class_get_list(TYPE_ARM_CPU, false);
1824
    list = g_slist_sort(list, arm_cpu_list_compare);
1825
    (*cpu_fprintf)(f, "Available CPUs:\n");
1826
    g_slist_foreach(list, arm_cpu_list_entry, &s);
1827
    g_slist_free(list);
1828
}
1829

    
1830
static void arm_cpu_add_definition(gpointer data, gpointer user_data)
1831
{
1832
    ObjectClass *oc = data;
1833
    CpuDefinitionInfoList **cpu_list = user_data;
1834
    CpuDefinitionInfoList *entry;
1835
    CpuDefinitionInfo *info;
1836
    const char *typename;
1837

    
1838
    typename = object_class_get_name(oc);
1839
    info = g_malloc0(sizeof(*info));
1840
    info->name = g_strndup(typename,
1841
                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
1842

    
1843
    entry = g_malloc0(sizeof(*entry));
1844
    entry->value = info;
1845
    entry->next = *cpu_list;
1846
    *cpu_list = entry;
1847
}
1848

    
1849
CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1850
{
1851
    CpuDefinitionInfoList *cpu_list = NULL;
1852
    GSList *list;
1853

    
1854
    list = object_class_get_list(TYPE_ARM_CPU, false);
1855
    g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
1856
    g_slist_free(list);
1857

    
1858
    return cpu_list;
1859
}
1860

    
1861
void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1862
                                       const ARMCPRegInfo *r, void *opaque)
1863
{
1864
    /* Define implementations of coprocessor registers.
1865
     * We store these in a hashtable because typically
1866
     * there are less than 150 registers in a space which
1867
     * is 16*16*16*8*8 = 262144 in size.
1868
     * Wildcarding is supported for the crm, opc1 and opc2 fields.
1869
     * If a register is defined twice then the second definition is
1870
     * used, so this can be used to define some generic registers and
1871
     * then override them with implementation specific variations.
1872
     * At least one of the original and the second definition should
1873
     * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
1874
     * against accidental use.
1875
     */
1876
    int crm, opc1, opc2;
1877
    int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
1878
    int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
1879
    int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
1880
    int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
1881
    int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
1882
    int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
1883
    /* 64 bit registers have only CRm and Opc1 fields */
1884
    assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
1885
    /* Check that the register definition has enough info to handle
1886
     * reads and writes if they are permitted.
1887
     */
1888
    if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
1889
        if (r->access & PL3_R) {
1890
            assert(r->fieldoffset || r->readfn);
1891
        }
1892
        if (r->access & PL3_W) {
1893
            assert(r->fieldoffset || r->writefn);
1894
        }
1895
    }
1896
    /* Bad type field probably means missing sentinel at end of reg list */
1897
    assert(cptype_valid(r->type));
1898
    for (crm = crmmin; crm <= crmmax; crm++) {
1899
        for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
1900
            for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
1901
                uint32_t *key = g_new(uint32_t, 1);
1902
                ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1903
                int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
1904
                *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
1905
                if (opaque) {
1906
                    r2->opaque = opaque;
1907
                }
1908
                /* Make sure reginfo passed to helpers for wildcarded regs
1909
                 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1910
                 */
1911
                r2->crm = crm;
1912
                r2->opc1 = opc1;
1913
                r2->opc2 = opc2;
1914
                /* By convention, for wildcarded registers only the first
1915
                 * entry is used for migration; the others are marked as
1916
                 * NO_MIGRATE so we don't try to transfer the register
1917
                 * multiple times. Special registers (ie NOP/WFI) are
1918
                 * never migratable.
1919
                 */
1920
                if ((r->type & ARM_CP_SPECIAL) ||
1921
                    ((r->crm == CP_ANY) && crm != 0) ||
1922
                    ((r->opc1 == CP_ANY) && opc1 != 0) ||
1923
                    ((r->opc2 == CP_ANY) && opc2 != 0)) {
1924
                    r2->type |= ARM_CP_NO_MIGRATE;
1925
                }
1926

    
1927
                /* Overriding of an existing definition must be explicitly
1928
                 * requested.
1929
                 */
1930
                if (!(r->type & ARM_CP_OVERRIDE)) {
1931
                    ARMCPRegInfo *oldreg;
1932
                    oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1933
                    if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1934
                        fprintf(stderr, "Register redefined: cp=%d %d bit "
1935
                                "crn=%d crm=%d opc1=%d opc2=%d, "
1936
                                "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1937
                                r2->crn, r2->crm, r2->opc1, r2->opc2,
1938
                                oldreg->name, r2->name);
1939
                        g_assert_not_reached();
1940
                    }
1941
                }
1942
                g_hash_table_insert(cpu->cp_regs, key, r2);
1943
            }
1944
        }
1945
    }
1946
}
1947

    
1948
void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1949
                                    const ARMCPRegInfo *regs, void *opaque)
1950
{
1951
    /* Define a whole list of registers */
1952
    const ARMCPRegInfo *r;
1953
    for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1954
        define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1955
    }
1956
}
1957

    
1958
const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1959
{
1960
    return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1961
}
1962

    
1963
int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1964
                        uint64_t value)
1965
{
1966
    /* Helper coprocessor write function for write-ignore registers */
1967
    return 0;
1968
}
1969

    
1970
int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1971
{
1972
    /* Helper coprocessor write function for read-as-zero registers */
1973
    *value = 0;
1974
    return 0;
1975
}
1976

    
1977
static int bad_mode_switch(CPUARMState *env, int mode)
1978
{
1979
    /* Return true if it is not valid for us to switch to
1980
     * this CPU mode (ie all the UNPREDICTABLE cases in
1981
     * the ARM ARM CPSRWriteByInstr pseudocode).
1982
     */
1983
    switch (mode) {
1984
    case ARM_CPU_MODE_USR:
1985
    case ARM_CPU_MODE_SYS:
1986
    case ARM_CPU_MODE_SVC:
1987
    case ARM_CPU_MODE_ABT:
1988
    case ARM_CPU_MODE_UND:
1989
    case ARM_CPU_MODE_IRQ:
1990
    case ARM_CPU_MODE_FIQ:
1991
        return 0;
1992
    default:
1993
        return 1;
1994
    }
1995
}
1996

    
1997
uint32_t cpsr_read(CPUARMState *env)
1998
{
1999
    int ZF;
2000
    ZF = (env->ZF == 0);
2001
    return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2002
        (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2003
        | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2004
        | ((env->condexec_bits & 0xfc) << 8)
2005
        | (env->GE << 16);
2006
}
2007

    
2008
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2009
{
2010
    if (mask & CPSR_NZCV) {
2011
        env->ZF = (~val) & CPSR_Z;
2012
        env->NF = val;
2013
        env->CF = (val >> 29) & 1;
2014
        env->VF = (val << 3) & 0x80000000;
2015
    }
2016
    if (mask & CPSR_Q)
2017
        env->QF = ((val & CPSR_Q) != 0);
2018
    if (mask & CPSR_T)
2019
        env->thumb = ((val & CPSR_T) != 0);
2020
    if (mask & CPSR_IT_0_1) {
2021
        env->condexec_bits &= ~3;
2022
        env->condexec_bits |= (val >> 25) & 3;
2023
    }
2024
    if (mask & CPSR_IT_2_7) {
2025
        env->condexec_bits &= 3;
2026
        env->condexec_bits |= (val >> 8) & 0xfc;
2027
    }
2028
    if (mask & CPSR_GE) {
2029
        env->GE = (val >> 16) & 0xf;
2030
    }
2031

    
2032
    if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
2033
        if (bad_mode_switch(env, val & CPSR_M)) {
2034
            /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2035
             * We choose to ignore the attempt and leave the CPSR M field
2036
             * untouched.
2037
             */
2038
            mask &= ~CPSR_M;
2039
        } else {
2040
            switch_mode(env, val & CPSR_M);
2041
        }
2042
    }
2043
    mask &= ~CACHED_CPSR_BITS;
2044
    env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2045
}
2046

    
2047
/* Sign/zero extend */
2048
uint32_t HELPER(sxtb16)(uint32_t x)
2049
{
2050
    uint32_t res;
2051
    res = (uint16_t)(int8_t)x;
2052
    res |= (uint32_t)(int8_t)(x >> 16) << 16;
2053
    return res;
2054
}
2055

    
2056
uint32_t HELPER(uxtb16)(uint32_t x)
2057
{
2058
    uint32_t res;
2059
    res = (uint16_t)(uint8_t)x;
2060
    res |= (uint32_t)(uint8_t)(x >> 16) << 16;
2061
    return res;
2062
}
2063

    
2064
uint32_t HELPER(clz)(uint32_t x)
2065
{
2066
    return clz32(x);
2067
}
2068

    
2069
int32_t HELPER(sdiv)(int32_t num, int32_t den)
2070
{
2071
    if (den == 0)
2072
      return 0;
2073
    if (num == INT_MIN && den == -1)
2074
      return INT_MIN;
2075
    return num / den;
2076
}
2077

    
2078
uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
2079
{
2080
    if (den == 0)
2081
      return 0;
2082
    return num / den;
2083
}
2084

    
2085
uint32_t HELPER(rbit)(uint32_t x)
2086
{
2087
    x =  ((x & 0xff000000) >> 24)
2088
       | ((x & 0x00ff0000) >> 8)
2089
       | ((x & 0x0000ff00) << 8)
2090
       | ((x & 0x000000ff) << 24);
2091
    x =  ((x & 0xf0f0f0f0) >> 4)
2092
       | ((x & 0x0f0f0f0f) << 4);
2093
    x =  ((x & 0x88888888) >> 3)
2094
       | ((x & 0x44444444) >> 1)
2095
       | ((x & 0x22222222) << 1)
2096
       | ((x & 0x11111111) << 3);
2097
    return x;
2098
}
2099

    
2100
#if defined(CONFIG_USER_ONLY)
2101

    
2102
void arm_cpu_do_interrupt(CPUState *cs)
2103
{
2104
    ARMCPU *cpu = ARM_CPU(cs);
2105
    CPUARMState *env = &cpu->env;
2106

    
2107
    env->exception_index = -1;
2108
}
2109

    
2110
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
2111
                              int mmu_idx)
2112
{
2113
    if (rw == 2) {
2114
        env->exception_index = EXCP_PREFETCH_ABORT;
2115
        env->cp15.c6_insn = address;
2116
    } else {
2117
        env->exception_index = EXCP_DATA_ABORT;
2118
        env->cp15.c6_data = address;
2119
    }
2120
    return 1;
2121
}
2122

    
2123
/* These should probably raise undefined insn exceptions.  */
2124
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2125
{
2126
    cpu_abort(env, "v7m_mrs %d\n", reg);
2127
}
2128

    
2129
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2130
{
2131
    cpu_abort(env, "v7m_mrs %d\n", reg);
2132
    return 0;
2133
}
2134

    
2135
void switch_mode(CPUARMState *env, int mode)
2136
{
2137
    if (mode != ARM_CPU_MODE_USR)
2138
        cpu_abort(env, "Tried to switch out of user mode\n");
2139
}
2140

    
2141
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2142
{
2143
    cpu_abort(env, "banked r13 write\n");
2144
}
2145

    
2146
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2147
{
2148
    cpu_abort(env, "banked r13 read\n");
2149
    return 0;
2150
}
2151

    
2152
#else
2153

    
2154
/* Map CPU modes onto saved register banks.  */
2155
int bank_number(int mode)
2156
{
2157
    switch (mode) {
2158
    case ARM_CPU_MODE_USR:
2159
    case ARM_CPU_MODE_SYS:
2160
        return 0;
2161
    case ARM_CPU_MODE_SVC:
2162
        return 1;
2163
    case ARM_CPU_MODE_ABT:
2164
        return 2;
2165
    case ARM_CPU_MODE_UND:
2166
        return 3;
2167
    case ARM_CPU_MODE_IRQ:
2168
        return 4;
2169
    case ARM_CPU_MODE_FIQ:
2170
        return 5;
2171
    }
2172
    hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
2173
}
2174

    
2175
void switch_mode(CPUARMState *env, int mode)
2176
{
2177
    int old_mode;
2178
    int i;
2179

    
2180
    old_mode = env->uncached_cpsr & CPSR_M;
2181
    if (mode == old_mode)
2182
        return;
2183

    
2184
    if (old_mode == ARM_CPU_MODE_FIQ) {
2185
        memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
2186
        memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
2187
    } else if (mode == ARM_CPU_MODE_FIQ) {
2188
        memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
2189
        memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
2190
    }
2191

    
2192
    i = bank_number(old_mode);
2193
    env->banked_r13[i] = env->regs[13];
2194
    env->banked_r14[i] = env->regs[14];
2195
    env->banked_spsr[i] = env->spsr;
2196

    
2197
    i = bank_number(mode);
2198
    env->regs[13] = env->banked_r13[i];
2199
    env->regs[14] = env->banked_r14[i];
2200
    env->spsr = env->banked_spsr[i];
2201
}
2202

    
2203
static void v7m_push(CPUARMState *env, uint32_t val)
2204
{
2205
    env->regs[13] -= 4;
2206
    stl_phys(env->regs[13], val);
2207
}
2208

    
2209
static uint32_t v7m_pop(CPUARMState *env)
2210
{
2211
    uint32_t val;
2212
    val = ldl_phys(env->regs[13]);
2213
    env->regs[13] += 4;
2214
    return val;
2215
}
2216

    
2217
/* Switch to V7M main or process stack pointer.  */
2218
static void switch_v7m_sp(CPUARMState *env, int process)
2219
{
2220
    uint32_t tmp;
2221
    if (env->v7m.current_sp != process) {
2222
        tmp = env->v7m.other_sp;
2223
        env->v7m.other_sp = env->regs[13];
2224
        env->regs[13] = tmp;
2225
        env->v7m.current_sp = process;
2226
    }
2227
}
2228

    
2229
static void do_v7m_exception_exit(CPUARMState *env)
2230
{
2231
    uint32_t type;
2232
    uint32_t xpsr;
2233

    
2234
    type = env->regs[15];
2235
    if (env->v7m.exception != 0)
2236
        armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
2237

    
2238
    /* Switch to the target stack.  */
2239
    switch_v7m_sp(env, (type & 4) != 0);
2240
    /* Pop registers.  */
2241
    env->regs[0] = v7m_pop(env);
2242
    env->regs[1] = v7m_pop(env);
2243
    env->regs[2] = v7m_pop(env);
2244
    env->regs[3] = v7m_pop(env);
2245
    env->regs[12] = v7m_pop(env);
2246
    env->regs[14] = v7m_pop(env);
2247
    env->regs[15] = v7m_pop(env);
2248
    xpsr = v7m_pop(env);
2249
    xpsr_write(env, xpsr, 0xfffffdff);
2250
    /* Undo stack alignment.  */
2251
    if (xpsr & 0x200)
2252
        env->regs[13] |= 4;
2253
    /* ??? The exception return type specifies Thread/Handler mode.  However
2254
       this is also implied by the xPSR value. Not sure what to do
2255
       if there is a mismatch.  */
2256
    /* ??? Likewise for mismatches between the CONTROL register and the stack
2257
       pointer.  */
2258
}
2259

    
2260
/* Exception names for debug logging; note that not all of these
2261
 * precisely correspond to architectural exceptions.
2262
 */
2263
static const char * const excnames[] = {
2264
    [EXCP_UDEF] = "Undefined Instruction",
2265
    [EXCP_SWI] = "SVC",
2266
    [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
2267
    [EXCP_DATA_ABORT] = "Data Abort",
2268
    [EXCP_IRQ] = "IRQ",
2269
    [EXCP_FIQ] = "FIQ",
2270
    [EXCP_BKPT] = "Breakpoint",
2271
    [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
2272
    [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
2273
    [EXCP_STREX] = "QEMU intercept of STREX",
2274
};
2275

    
2276
static inline void arm_log_exception(int idx)
2277
{
2278
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
2279
        const char *exc = NULL;
2280

    
2281
        if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
2282
            exc = excnames[idx];
2283
        }
2284
        if (!exc) {
2285
            exc = "unknown";
2286
        }
2287
        qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2288
    }
2289
}
2290

    
2291
void arm_v7m_cpu_do_interrupt(CPUState *cs)
2292
{
2293
    ARMCPU *cpu = ARM_CPU(cs);
2294
    CPUARMState *env = &cpu->env;
2295
    uint32_t xpsr = xpsr_read(env);
2296
    uint32_t lr;
2297
    uint32_t addr;
2298

    
2299
    arm_log_exception(env->exception_index);
2300

    
2301
    lr = 0xfffffff1;
2302
    if (env->v7m.current_sp)
2303
        lr |= 4;
2304
    if (env->v7m.exception == 0)
2305
        lr |= 8;
2306

    
2307
    /* For exceptions we just mark as pending on the NVIC, and let that
2308
       handle it.  */
2309
    /* TODO: Need to escalate if the current priority is higher than the
2310
       one we're raising.  */
2311
    switch (env->exception_index) {
2312
    case EXCP_UDEF:
2313
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
2314
        return;
2315
    case EXCP_SWI:
2316
        /* The PC already points to the next instruction.  */
2317
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
2318
        return;
2319
    case EXCP_PREFETCH_ABORT:
2320
    case EXCP_DATA_ABORT:
2321
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
2322
        return;
2323
    case EXCP_BKPT:
2324
        if (semihosting_enabled) {
2325
            int nr;
2326
            nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2327
            if (nr == 0xab) {
2328
                env->regs[15] += 2;
2329
                env->regs[0] = do_arm_semihosting(env);
2330
                qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2331
                return;
2332
            }
2333
        }
2334
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
2335
        return;
2336
    case EXCP_IRQ:
2337
        env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
2338
        break;
2339
    case EXCP_EXCEPTION_EXIT:
2340
        do_v7m_exception_exit(env);
2341
        return;
2342
    default:
2343
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2344
        return; /* Never happens.  Keep compiler happy.  */
2345
    }
2346

    
2347
    /* Align stack pointer.  */
2348
    /* ??? Should only do this if Configuration Control Register
2349
       STACKALIGN bit is set.  */
2350
    if (env->regs[13] & 4) {
2351
        env->regs[13] -= 4;
2352
        xpsr |= 0x200;
2353
    }
2354
    /* Switch to the handler mode.  */
2355
    v7m_push(env, xpsr);
2356
    v7m_push(env, env->regs[15]);
2357
    v7m_push(env, env->regs[14]);
2358
    v7m_push(env, env->regs[12]);
2359
    v7m_push(env, env->regs[3]);
2360
    v7m_push(env, env->regs[2]);
2361
    v7m_push(env, env->regs[1]);
2362
    v7m_push(env, env->regs[0]);
2363
    switch_v7m_sp(env, 0);
2364
    /* Clear IT bits */
2365
    env->condexec_bits = 0;
2366
    env->regs[14] = lr;
2367
    addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
2368
    env->regs[15] = addr & 0xfffffffe;
2369
    env->thumb = addr & 1;
2370
}
2371

    
2372
/* Handle a CPU exception.  */
2373
void arm_cpu_do_interrupt(CPUState *cs)
2374
{
2375
    ARMCPU *cpu = ARM_CPU(cs);
2376
    CPUARMState *env = &cpu->env;
2377
    uint32_t addr;
2378
    uint32_t mask;
2379
    int new_mode;
2380
    uint32_t offset;
2381

    
2382
    assert(!IS_M(env));
2383

    
2384
    arm_log_exception(env->exception_index);
2385

    
2386
    /* TODO: Vectored interrupt controller.  */
2387
    switch (env->exception_index) {
2388
    case EXCP_UDEF:
2389
        new_mode = ARM_CPU_MODE_UND;
2390
        addr = 0x04;
2391
        mask = CPSR_I;
2392
        if (env->thumb)
2393
            offset = 2;
2394
        else
2395
            offset = 4;
2396
        break;
2397
    case EXCP_SWI:
2398
        if (semihosting_enabled) {
2399
            /* Check for semihosting interrupt.  */
2400
            if (env->thumb) {
2401
                mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
2402
                    & 0xff;
2403
            } else {
2404
                mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
2405
                    & 0xffffff;
2406
            }
2407
            /* Only intercept calls from privileged modes, to provide some
2408
               semblance of security.  */
2409
            if (((mask == 0x123456 && !env->thumb)
2410
                    || (mask == 0xab && env->thumb))
2411
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2412
                env->regs[0] = do_arm_semihosting(env);
2413
                qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2414
                return;
2415
            }
2416
        }
2417
        new_mode = ARM_CPU_MODE_SVC;
2418
        addr = 0x08;
2419
        mask = CPSR_I;
2420
        /* The PC already points to the next instruction.  */
2421
        offset = 0;
2422
        break;
2423
    case EXCP_BKPT:
2424
        /* See if this is a semihosting syscall.  */
2425
        if (env->thumb && semihosting_enabled) {
2426
            mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
2427
            if (mask == 0xab
2428
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
2429
                env->regs[15] += 2;
2430
                env->regs[0] = do_arm_semihosting(env);
2431
                qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
2432
                return;
2433
            }
2434
        }
2435
        env->cp15.c5_insn = 2;
2436
        /* Fall through to prefetch abort.  */
2437
    case EXCP_PREFETCH_ABORT:
2438
        qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
2439
                      env->cp15.c5_insn, env->cp15.c6_insn);
2440
        new_mode = ARM_CPU_MODE_ABT;
2441
        addr = 0x0c;
2442
        mask = CPSR_A | CPSR_I;
2443
        offset = 4;
2444
        break;
2445
    case EXCP_DATA_ABORT:
2446
        qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
2447
                      env->cp15.c5_data, env->cp15.c6_data);
2448
        new_mode = ARM_CPU_MODE_ABT;
2449
        addr = 0x10;
2450
        mask = CPSR_A | CPSR_I;
2451
        offset = 8;
2452
        break;
2453
    case EXCP_IRQ:
2454
        new_mode = ARM_CPU_MODE_IRQ;
2455
        addr = 0x18;
2456
        /* Disable IRQ and imprecise data aborts.  */
2457
        mask = CPSR_A | CPSR_I;
2458
        offset = 4;
2459
        break;
2460
    case EXCP_FIQ:
2461
        new_mode = ARM_CPU_MODE_FIQ;
2462
        addr = 0x1c;
2463
        /* Disable FIQ, IRQ and imprecise data aborts.  */
2464
        mask = CPSR_A | CPSR_I | CPSR_F;
2465
        offset = 4;
2466
        break;
2467
    default:
2468
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
2469
        return; /* Never happens.  Keep compiler happy.  */
2470
    }
2471
    /* High vectors.  */
2472
    if (env->cp15.c1_sys & (1 << 13)) {
2473
        addr += 0xffff0000;
2474
    }
2475
    switch_mode (env, new_mode);
2476
    env->spsr = cpsr_read(env);
2477
    /* Clear IT bits.  */
2478
    env->condexec_bits = 0;
2479
    /* Switch to the new mode, and to the correct instruction set.  */
2480
    env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
2481
    env->uncached_cpsr |= mask;
2482
    /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2483
     * and we should just guard the thumb mode on V4 */
2484
    if (arm_feature(env, ARM_FEATURE_V4T)) {
2485
        env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
2486
    }
2487
    env->regs[14] = env->regs[15] + offset;
2488
    env->regs[15] = addr;
2489
    cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
2490
}
2491

    
2492
/* Check section/page access permissions.
2493
   Returns the page protection flags, or zero if the access is not
2494
   permitted.  */
2495
static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
2496
                           int access_type, int is_user)
2497
{
2498
  int prot_ro;
2499

    
2500
  if (domain_prot == 3) {
2501
    return PAGE_READ | PAGE_WRITE;
2502
  }
2503

    
2504
  if (access_type == 1)
2505
      prot_ro = 0;
2506
  else
2507
      prot_ro = PAGE_READ;
2508

    
2509
  switch (ap) {
2510
  case 0:
2511
      if (access_type == 1)
2512
          return 0;
2513
      switch ((env->cp15.c1_sys >> 8) & 3) {
2514
      case 1:
2515
          return is_user ? 0 : PAGE_READ;
2516
      case 2:
2517
          return PAGE_READ;
2518
      default:
2519
          return 0;
2520
      }
2521
  case 1:
2522
      return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2523
  case 2:
2524
      if (is_user)
2525
          return prot_ro;
2526
      else
2527
          return PAGE_READ | PAGE_WRITE;
2528
  case 3:
2529
      return PAGE_READ | PAGE_WRITE;
2530
  case 4: /* Reserved.  */
2531
      return 0;
2532
  case 5:
2533
      return is_user ? 0 : prot_ro;
2534
  case 6:
2535
      return prot_ro;
2536
  case 7:
2537
      if (!arm_feature (env, ARM_FEATURE_V6K))
2538
          return 0;
2539
      return prot_ro;
2540
  default:
2541
      abort();
2542
  }
2543
}
2544

    
2545
static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
2546
{
2547
    uint32_t table;
2548

    
2549
    if (address & env->cp15.c2_mask)
2550
        table = env->cp15.c2_base1 & 0xffffc000;
2551
    else
2552
        table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2553

    
2554
    table |= (address >> 18) & 0x3ffc;
2555
    return table;
2556
}
2557

    
2558
static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
2559
                            int is_user, hwaddr *phys_ptr,
2560
                            int *prot, target_ulong *page_size)
2561
{
2562
    int code;
2563
    uint32_t table;
2564
    uint32_t desc;
2565
    int type;
2566
    int ap;
2567
    int domain;
2568
    int domain_prot;
2569
    hwaddr phys_addr;
2570

    
2571
    /* Pagetable walk.  */
2572
    /* Lookup l1 descriptor.  */
2573
    table = get_level1_table_address(env, address);
2574
    desc = ldl_phys(table);
2575
    type = (desc & 3);
2576
    domain = (desc >> 5) & 0x0f;
2577
    domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2578
    if (type == 0) {
2579
        /* Section translation fault.  */
2580
        code = 5;
2581
        goto do_fault;
2582
    }
2583
    if (domain_prot == 0 || domain_prot == 2) {
2584
        if (type == 2)
2585
            code = 9; /* Section domain fault.  */
2586
        else
2587
            code = 11; /* Page domain fault.  */
2588
        goto do_fault;
2589
    }
2590
    if (type == 2) {
2591
        /* 1Mb section.  */
2592
        phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2593
        ap = (desc >> 10) & 3;
2594
        code = 13;
2595
        *page_size = 1024 * 1024;
2596
    } else {
2597
        /* Lookup l2 entry.  */
2598
        if (type == 1) {
2599
            /* Coarse pagetable.  */
2600
            table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2601
        } else {
2602
            /* Fine pagetable.  */
2603
            table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2604
        }
2605
        desc = ldl_phys(table);
2606
        switch (desc & 3) {
2607
        case 0: /* Page translation fault.  */
2608
            code = 7;
2609
            goto do_fault;
2610
        case 1: /* 64k page.  */
2611
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2612
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2613
            *page_size = 0x10000;
2614
            break;
2615
        case 2: /* 4k page.  */
2616
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2617
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
2618
            *page_size = 0x1000;
2619
            break;
2620
        case 3: /* 1k page.  */
2621
            if (type == 1) {
2622
                if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2623
                    phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2624
                } else {
2625
                    /* Page translation fault.  */
2626
                    code = 7;
2627
                    goto do_fault;
2628
                }
2629
            } else {
2630
                phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
2631
            }
2632
            ap = (desc >> 4) & 3;
2633
            *page_size = 0x400;
2634
            break;
2635
        default:
2636
            /* Never happens, but compiler isn't smart enough to tell.  */
2637
            abort();
2638
        }
2639
        code = 15;
2640
    }
2641
    *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2642
    if (!*prot) {
2643
        /* Access permission fault.  */
2644
        goto do_fault;
2645
    }
2646
    *prot |= PAGE_EXEC;
2647
    *phys_ptr = phys_addr;
2648
    return 0;
2649
do_fault:
2650
    return code | (domain << 4);
2651
}
2652

    
2653
static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
2654
                            int is_user, hwaddr *phys_ptr,
2655
                            int *prot, target_ulong *page_size)
2656
{
2657
    int code;
2658
    uint32_t table;
2659
    uint32_t desc;
2660
    uint32_t xn;
2661
    uint32_t pxn = 0;
2662
    int type;
2663
    int ap;
2664
    int domain = 0;
2665
    int domain_prot;
2666
    hwaddr phys_addr;
2667

    
2668
    /* Pagetable walk.  */
2669
    /* Lookup l1 descriptor.  */
2670
    table = get_level1_table_address(env, address);
2671
    desc = ldl_phys(table);
2672
    type = (desc & 3);
2673
    if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2674
        /* Section translation fault, or attempt to use the encoding
2675
         * which is Reserved on implementations without PXN.
2676
         */
2677
        code = 5;
2678
        goto do_fault;
2679
    }
2680
    if ((type == 1) || !(desc & (1 << 18))) {
2681
        /* Page or Section.  */
2682
        domain = (desc >> 5) & 0x0f;
2683
    }
2684
    domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2685
    if (domain_prot == 0 || domain_prot == 2) {
2686
        if (type != 1) {
2687
            code = 9; /* Section domain fault.  */
2688
        } else {
2689
            code = 11; /* Page domain fault.  */
2690
        }
2691
        goto do_fault;
2692
    }
2693
    if (type != 1) {
2694
        if (desc & (1 << 18)) {
2695
            /* Supersection.  */
2696
            phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
2697
            *page_size = 0x1000000;
2698
        } else {
2699
            /* Section.  */
2700
            phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
2701
            *page_size = 0x100000;
2702
        }
2703
        ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
2704
        xn = desc & (1 << 4);
2705
        pxn = desc & 1;
2706
        code = 13;
2707
    } else {
2708
        if (arm_feature(env, ARM_FEATURE_PXN)) {
2709
            pxn = (desc >> 2) & 1;
2710
        }
2711
        /* Lookup l2 entry.  */
2712
        table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2713
        desc = ldl_phys(table);
2714
        ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2715
        switch (desc & 3) {
2716
        case 0: /* Page translation fault.  */
2717
            code = 7;
2718
            goto do_fault;
2719
        case 1: /* 64k page.  */
2720
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
2721
            xn = desc & (1 << 15);
2722
            *page_size = 0x10000;
2723
            break;
2724
        case 2: case 3: /* 4k page.  */
2725
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2726
            xn = desc & 1;
2727
            *page_size = 0x1000;
2728
            break;
2729
        default:
2730
            /* Never happens, but compiler isn't smart enough to tell.  */
2731
            abort();
2732
        }
2733
        code = 15;
2734
    }
2735
    if (domain_prot == 3) {
2736
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2737
    } else {
2738
        if (pxn && !is_user) {
2739
            xn = 1;
2740
        }
2741
        if (xn && access_type == 2)
2742
            goto do_fault;
2743

    
2744
        /* The simplified model uses AP[0] as an access control bit.  */
2745
        if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
2746
            /* Access flag fault.  */
2747
            code = (code == 15) ? 6 : 3;
2748
            goto do_fault;
2749
        }
2750
        *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2751
        if (!*prot) {
2752
            /* Access permission fault.  */
2753
            goto do_fault;
2754
        }
2755
        if (!xn) {
2756
            *prot |= PAGE_EXEC;
2757
        }
2758
    }
2759
    *phys_ptr = phys_addr;
2760
    return 0;
2761
do_fault:
2762
    return code | (domain << 4);
2763
}
2764

    
2765
/* Fault type for long-descriptor MMU fault reporting; this corresponds
2766
 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2767
 */
2768
typedef enum {
2769
    translation_fault = 1,
2770
    access_fault = 2,
2771
    permission_fault = 3,
2772
} MMUFaultType;
2773

    
2774
static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2775
                              int access_type, int is_user,
2776
                              hwaddr *phys_ptr, int *prot,
2777
                              target_ulong *page_size_ptr)
2778
{
2779
    /* Read an LPAE long-descriptor translation table. */
2780
    MMUFaultType fault_type = translation_fault;
2781
    uint32_t level = 1;
2782
    uint32_t epd;
2783
    uint32_t tsz;
2784
    uint64_t ttbr;
2785
    int ttbr_select;
2786
    int n;
2787
    hwaddr descaddr;
2788
    uint32_t tableattrs;
2789
    target_ulong page_size;
2790
    uint32_t attrs;
2791

    
2792
    /* Determine whether this address is in the region controlled by
2793
     * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2794
     * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2795
     * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2796
     */
2797
    uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2798
    uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2799
    if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2800
        /* there is a ttbr0 region and we are in it (high bits all zero) */
2801
        ttbr_select = 0;
2802
    } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
2803
        /* there is a ttbr1 region and we are in it (high bits all one) */
2804
        ttbr_select = 1;
2805
    } else if (!t0sz) {
2806
        /* ttbr0 region is "everything not in the ttbr1 region" */
2807
        ttbr_select = 0;
2808
    } else if (!t1sz) {
2809
        /* ttbr1 region is "everything not in the ttbr0 region" */
2810
        ttbr_select = 1;
2811
    } else {
2812
        /* in the gap between the two regions, this is a Translation fault */
2813
        fault_type = translation_fault;
2814
        goto do_fault;
2815
    }
2816

    
2817
    /* Note that QEMU ignores shareability and cacheability attributes,
2818
     * so we don't need to do anything with the SH, ORGN, IRGN fields
2819
     * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
2820
     * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
2821
     * implement any ASID-like capability so we can ignore it (instead
2822
     * we will always flush the TLB any time the ASID is changed).
2823
     */
2824
    if (ttbr_select == 0) {
2825
        ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
2826
        epd = extract32(env->cp15.c2_control, 7, 1);
2827
        tsz = t0sz;
2828
    } else {
2829
        ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
2830
        epd = extract32(env->cp15.c2_control, 23, 1);
2831
        tsz = t1sz;
2832
    }
2833

    
2834
    if (epd) {
2835
        /* Translation table walk disabled => Translation fault on TLB miss */
2836
        goto do_fault;
2837
    }
2838

    
2839
    /* If the region is small enough we will skip straight to a 2nd level
2840
     * lookup. This affects the number of bits of the address used in
2841
     * combination with the TTBR to find the first descriptor. ('n' here
2842
     * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
2843
     * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
2844
     */
2845
    if (tsz > 1) {
2846
        level = 2;
2847
        n = 14 - tsz;
2848
    } else {
2849
        n = 5 - tsz;
2850
    }
2851

    
2852
    /* Clear the vaddr bits which aren't part of the within-region address,
2853
     * so that we don't have to special case things when calculating the
2854
     * first descriptor address.
2855
     */
2856
    address &= (0xffffffffU >> tsz);
2857

    
2858
    /* Now we can extract the actual base address from the TTBR */
2859
    descaddr = extract64(ttbr, 0, 40);
2860
    descaddr &= ~((1ULL << n) - 1);
2861

    
2862
    tableattrs = 0;
2863
    for (;;) {
2864
        uint64_t descriptor;
2865

    
2866
        descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
2867
        descriptor = ldq_phys(descaddr);
2868
        if (!(descriptor & 1) ||
2869
            (!(descriptor & 2) && (level == 3))) {
2870
            /* Invalid, or the Reserved level 3 encoding */
2871
            goto do_fault;
2872
        }
2873
        descaddr = descriptor & 0xfffffff000ULL;
2874

    
2875
        if ((descriptor & 2) && (level < 3)) {
2876
            /* Table entry. The top five bits are attributes which  may
2877
             * propagate down through lower levels of the table (and
2878
             * which are all arranged so that 0 means "no effect", so
2879
             * we can gather them up by ORing in the bits at each level).
2880
             */
2881
            tableattrs |= extract64(descriptor, 59, 5);
2882
            level++;
2883
            continue;
2884
        }
2885
        /* Block entry at level 1 or 2, or page entry at level 3.
2886
         * These are basically the same thing, although the number
2887
         * of bits we pull in from the vaddr varies.
2888
         */
2889
        page_size = (1 << (39 - (9 * level)));
2890
        descaddr |= (address & (page_size - 1));
2891
        /* Extract attributes from the descriptor and merge with table attrs */
2892
        attrs = extract64(descriptor, 2, 10)
2893
            | (extract64(descriptor, 52, 12) << 10);
2894
        attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
2895
        attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
2896
        /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
2897
         * means "force PL1 access only", which means forcing AP[1] to 0.
2898
         */
2899
        if (extract32(tableattrs, 2, 1)) {
2900
            attrs &= ~(1 << 4);
2901
        }
2902
        /* Since we're always in the Non-secure state, NSTable is ignored. */
2903
        break;
2904
    }
2905
    /* Here descaddr is the final physical address, and attributes
2906
     * are all in attrs.
2907
     */
2908
    fault_type = access_fault;
2909
    if ((attrs & (1 << 8)) == 0) {
2910
        /* Access flag */
2911
        goto do_fault;
2912
    }
2913
    fault_type = permission_fault;
2914
    if (is_user && !(attrs & (1 << 4))) {
2915
        /* Unprivileged access not enabled */
2916
        goto do_fault;
2917
    }
2918
    *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2919
    if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) {
2920
        /* XN or PXN */
2921
        if (access_type == 2) {
2922
            goto do_fault;
2923
        }
2924
        *prot &= ~PAGE_EXEC;
2925
    }
2926
    if (attrs & (1 << 5)) {
2927
        /* Write access forbidden */
2928
        if (access_type == 1) {
2929
            goto do_fault;
2930
        }
2931
        *prot &= ~PAGE_WRITE;
2932
    }
2933

    
2934
    *phys_ptr = descaddr;
2935
    *page_size_ptr = page_size;
2936
    return 0;
2937

    
2938
do_fault:
2939
    /* Long-descriptor format IFSR/DFSR value */
2940
    return (1 << 9) | (fault_type << 2) | level;
2941
}
2942

    
2943
static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
2944
                             int access_type, int is_user,
2945
                             hwaddr *phys_ptr, int *prot)
2946
{
2947
    int n;
2948
    uint32_t mask;
2949
    uint32_t base;
2950

    
2951
    *phys_ptr = address;
2952
    for (n = 7; n >= 0; n--) {
2953
        base = env->cp15.c6_region[n];
2954
        if ((base & 1) == 0)
2955
            continue;
2956
        mask = 1 << ((base >> 1) & 0x1f);
2957
        /* Keep this shift separate from the above to avoid an
2958
           (undefined) << 32.  */
2959
        mask = (mask << 1) - 1;
2960
        if (((base ^ address) & ~mask) == 0)
2961
            break;
2962
    }
2963
    if (n < 0)
2964
        return 2;
2965

    
2966
    if (access_type == 2) {
2967
        mask = env->cp15.c5_insn;
2968
    } else {
2969
        mask = env->cp15.c5_data;
2970
    }
2971
    mask = (mask >> (n * 4)) & 0xf;
2972
    switch (mask) {
2973
    case 0:
2974
        return 1;
2975
    case 1:
2976
        if (is_user)
2977
          return 1;
2978
        *prot = PAGE_READ | PAGE_WRITE;
2979
        break;
2980
    case 2:
2981
        *prot = PAGE_READ;
2982
        if (!is_user)
2983
            *prot |= PAGE_WRITE;
2984
        break;
2985
    case 3:
2986
        *prot = PAGE_READ | PAGE_WRITE;
2987
        break;
2988
    case 5:
2989
        if (is_user)
2990
            return 1;
2991
        *prot = PAGE_READ;
2992
        break;
2993
    case 6:
2994
        *prot = PAGE_READ;
2995
        break;
2996
    default:
2997
        /* Bad permission.  */
2998
        return 1;
2999
    }
3000
    *prot |= PAGE_EXEC;
3001
    return 0;
3002
}
3003

    
3004
/* get_phys_addr - get the physical address for this virtual address
3005
 *
3006
 * Find the physical address corresponding to the given virtual address,
3007
 * by doing a translation table walk on MMU based systems or using the
3008
 * MPU state on MPU based systems.
3009
 *
3010
 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3011
 * prot and page_size are not filled in, and the return value provides
3012
 * information on why the translation aborted, in the format of a
3013
 * DFSR/IFSR fault register, with the following caveats:
3014
 *  * we honour the short vs long DFSR format differences.
3015
 *  * the WnR bit is never set (the caller must do this).
3016
 *  * for MPU based systems we don't bother to return a full FSR format
3017
 *    value.
3018
 *
3019
 * @env: CPUARMState
3020
 * @address: virtual address to get physical address for
3021
 * @access_type: 0 for read, 1 for write, 2 for execute
3022
 * @is_user: 0 for privileged access, 1 for user
3023
 * @phys_ptr: set to the physical address corresponding to the virtual address
3024
 * @prot: set to the permissions for the page containing phys_ptr
3025
 * @page_size: set to the size of the page containing phys_ptr
3026
 */
3027
static inline int get_phys_addr(CPUARMState *env, uint32_t address,
3028
                                int access_type, int is_user,
3029
                                hwaddr *phys_ptr, int *prot,
3030
                                target_ulong *page_size)
3031
{
3032
    /* Fast Context Switch Extension.  */
3033
    if (address < 0x02000000)
3034
        address += env->cp15.c13_fcse;
3035

    
3036
    if ((env->cp15.c1_sys & 1) == 0) {
3037
        /* MMU/MPU disabled.  */
3038
        *phys_ptr = address;
3039
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3040
        *page_size = TARGET_PAGE_SIZE;
3041
        return 0;
3042
    } else if (arm_feature(env, ARM_FEATURE_MPU)) {
3043
        *page_size = TARGET_PAGE_SIZE;
3044
        return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
3045
                                 prot);
3046
    } else if (extended_addresses_enabled(env)) {
3047
        return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3048
                                  prot, page_size);
3049
    } else if (env->cp15.c1_sys & (1 << 23)) {
3050
        return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
3051
                                prot, page_size);
3052
    } else {
3053
        return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
3054
                                prot, page_size);
3055
    }
3056
}
3057

    
3058
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
3059
                              int access_type, int mmu_idx)
3060
{
3061
    hwaddr phys_addr;
3062
    target_ulong page_size;
3063
    int prot;
3064
    int ret, is_user;
3065

    
3066
    is_user = mmu_idx == MMU_USER_IDX;
3067
    ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
3068
                        &page_size);
3069
    if (ret == 0) {
3070
        /* Map a single [sub]page.  */
3071
        phys_addr &= ~(hwaddr)0x3ff;
3072
        address &= ~(uint32_t)0x3ff;
3073
        tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
3074
        return 0;
3075
    }
3076

    
3077
    if (access_type == 2) {
3078
        env->cp15.c5_insn = ret;
3079
        env->cp15.c6_insn = address;
3080
        env->exception_index = EXCP_PREFETCH_ABORT;
3081
    } else {
3082
        env->cp15.c5_data = ret;
3083
        if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3084
            env->cp15.c5_data |= (1 << 11);
3085
        env->cp15.c6_data = address;
3086
        env->exception_index = EXCP_DATA_ABORT;
3087
    }
3088
    return 1;
3089
}
3090

    
3091
hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
3092
{
3093
    ARMCPU *cpu = ARM_CPU(cs);
3094
    hwaddr phys_addr;
3095
    target_ulong page_size;
3096
    int prot;
3097
    int ret;
3098

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

    
3101
    if (ret != 0) {
3102
        return -1;
3103
    }
3104

    
3105
    return phys_addr;
3106
}
3107

    
3108
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
3109
{
3110
    if ((env->uncached_cpsr & CPSR_M) == mode) {
3111
        env->regs[13] = val;
3112
    } else {
3113
        env->banked_r13[bank_number(mode)] = val;
3114
    }
3115
}
3116

    
3117
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
3118
{
3119
    if ((env->uncached_cpsr & CPSR_M) == mode) {
3120
        return env->regs[13];
3121
    } else {
3122
        return env->banked_r13[bank_number(mode)];
3123
    }
3124
}
3125

    
3126
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3127
{
3128
    switch (reg) {
3129
    case 0: /* APSR */
3130
        return xpsr_read(env) & 0xf8000000;
3131
    case 1: /* IAPSR */
3132
        return xpsr_read(env) & 0xf80001ff;
3133
    case 2: /* EAPSR */
3134
        return xpsr_read(env) & 0xff00fc00;
3135
    case 3: /* xPSR */
3136
        return xpsr_read(env) & 0xff00fdff;
3137
    case 5: /* IPSR */
3138
        return xpsr_read(env) & 0x000001ff;
3139
    case 6: /* EPSR */
3140
        return xpsr_read(env) & 0x0700fc00;
3141
    case 7: /* IEPSR */
3142
        return xpsr_read(env) & 0x0700edff;
3143
    case 8: /* MSP */
3144
        return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
3145
    case 9: /* PSP */
3146
        return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3147
    case 16: /* PRIMASK */
3148
        return (env->uncached_cpsr & CPSR_I) != 0;
3149
    case 17: /* BASEPRI */
3150
    case 18: /* BASEPRI_MAX */
3151
        return env->v7m.basepri;
3152
    case 19: /* FAULTMASK */
3153
        return (env->uncached_cpsr & CPSR_F) != 0;
3154
    case 20: /* CONTROL */
3155
        return env->v7m.control;
3156
    default:
3157
        /* ??? For debugging only.  */
3158
        cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
3159
        return 0;
3160
    }
3161
}
3162

    
3163
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3164
{
3165
    switch (reg) {
3166
    case 0: /* APSR */
3167
        xpsr_write(env, val, 0xf8000000);
3168
        break;
3169
    case 1: /* IAPSR */
3170
        xpsr_write(env, val, 0xf8000000);
3171
        break;
3172
    case 2: /* EAPSR */
3173
        xpsr_write(env, val, 0xfe00fc00);
3174
        break;
3175
    case 3: /* xPSR */
3176
        xpsr_write(env, val, 0xfe00fc00);
3177
        break;
3178
    case 5: /* IPSR */
3179
        /* IPSR bits are readonly.  */
3180
        break;
3181
    case 6: /* EPSR */
3182
        xpsr_write(env, val, 0x0600fc00);
3183
        break;
3184
    case 7: /* IEPSR */
3185
        xpsr_write(env, val, 0x0600fc00);
3186
        break;
3187
    case 8: /* MSP */
3188
        if (env->v7m.current_sp)
3189
            env->v7m.other_sp = val;
3190
        else
3191
            env->regs[13] = val;
3192
        break;
3193
    case 9: /* PSP */
3194
        if (env->v7m.current_sp)
3195
            env->regs[13] = val;
3196
        else
3197
            env->v7m.other_sp = val;
3198
        break;
3199
    case 16: /* PRIMASK */
3200
        if (val & 1)
3201
            env->uncached_cpsr |= CPSR_I;
3202
        else
3203
            env->uncached_cpsr &= ~CPSR_I;
3204
        break;
3205
    case 17: /* BASEPRI */
3206
        env->v7m.basepri = val & 0xff;
3207
        break;
3208
    case 18: /* BASEPRI_MAX */
3209
        val &= 0xff;
3210
        if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
3211
            env->v7m.basepri = val;
3212
        break;
3213
    case 19: /* FAULTMASK */
3214
        if (val & 1)
3215
            env->uncached_cpsr |= CPSR_F;
3216
        else
3217
            env->uncached_cpsr &= ~CPSR_F;
3218
        break;
3219
    case 20: /* CONTROL */
3220
        env->v7m.control = val & 3;
3221
        switch_v7m_sp(env, (val & 2) != 0);
3222
        break;
3223
    default:
3224
        /* ??? For debugging only.  */
3225
        cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
3226
        return;
3227
    }
3228
}
3229

    
3230
#endif
3231

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

    
3236
/* Signed saturating arithmetic.  */
3237

    
3238
/* Perform 16-bit signed saturating addition.  */
3239
static inline uint16_t add16_sat(uint16_t a, uint16_t b)
3240
{
3241
    uint16_t res;
3242

    
3243
    res = a + b;
3244
    if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
3245
        if (a & 0x8000)
3246
            res = 0x8000;
3247
        else
3248
            res = 0x7fff;
3249
    }
3250
    return res;
3251
}
3252

    
3253
/* Perform 8-bit signed saturating addition.  */
3254
static inline uint8_t add8_sat(uint8_t a, uint8_t b)
3255
{
3256
    uint8_t res;
3257

    
3258
    res = a + b;
3259
    if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
3260
        if (a & 0x80)
3261
            res = 0x80;
3262
        else
3263
            res = 0x7f;
3264
    }
3265
    return res;
3266
}
3267

    
3268
/* Perform 16-bit signed saturating subtraction.  */
3269
static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
3270
{
3271
    uint16_t res;
3272

    
3273
    res = a - b;
3274
    if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
3275
        if (a & 0x8000)
3276
            res = 0x8000;
3277
        else
3278
            res = 0x7fff;
3279
    }
3280
    return res;
3281
}
3282

    
3283
/* Perform 8-bit signed saturating subtraction.  */
3284
static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3285
{
3286
    uint8_t res;
3287

    
3288
    res = a - b;
3289
    if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3290
        if (a & 0x80)
3291
            res = 0x80;
3292
        else
3293
            res = 0x7f;
3294
    }
3295
    return res;
3296
}
3297

    
3298
#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3299
#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3300
#define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
3301
#define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
3302
#define PFX q
3303

    
3304
#include "op_addsub.h"
3305

    
3306
/* Unsigned saturating arithmetic.  */
3307
static inline uint16_t add16_usat(uint16_t a, uint16_t b)
3308
{
3309
    uint16_t res;
3310
    res = a + b;
3311
    if (res < a)
3312
        res = 0xffff;
3313
    return res;
3314
}
3315

    
3316
static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
3317
{
3318
    if (a > b)
3319
        return a - b;
3320
    else
3321
        return 0;
3322
}
3323

    
3324
static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3325
{
3326
    uint8_t res;
3327
    res = a + b;
3328
    if (res < a)
3329
        res = 0xff;
3330
    return res;
3331
}
3332

    
3333
static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3334
{
3335
    if (a > b)
3336
        return a - b;
3337
    else
3338
        return 0;
3339
}
3340

    
3341
#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3342
#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3343
#define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
3344
#define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
3345
#define PFX uq
3346

    
3347
#include "op_addsub.h"
3348

    
3349
/* Signed modulo arithmetic.  */
3350
#define SARITH16(a, b, n, op) do { \
3351
    int32_t sum; \
3352
    sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
3353
    RESULT(sum, n, 16); \
3354
    if (sum >= 0) \
3355
        ge |= 3 << (n * 2); \
3356
    } while(0)
3357

    
3358
#define SARITH8(a, b, n, op) do { \
3359
    int32_t sum; \
3360
    sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
3361
    RESULT(sum, n, 8); \
3362
    if (sum >= 0) \
3363
        ge |= 1 << n; \
3364
    } while(0)
3365

    
3366

    
3367
#define ADD16(a, b, n) SARITH16(a, b, n, +)
3368
#define SUB16(a, b, n) SARITH16(a, b, n, -)
3369
#define ADD8(a, b, n)  SARITH8(a, b, n, +)
3370
#define SUB8(a, b, n)  SARITH8(a, b, n, -)
3371
#define PFX s
3372
#define ARITH_GE
3373

    
3374
#include "op_addsub.h"
3375

    
3376
/* Unsigned modulo arithmetic.  */
3377
#define ADD16(a, b, n) do { \
3378
    uint32_t sum; \
3379
    sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3380
    RESULT(sum, n, 16); \
3381
    if ((sum >> 16) == 1) \
3382
        ge |= 3 << (n * 2); \
3383
    } while(0)
3384

    
3385
#define ADD8(a, b, n) do { \
3386
    uint32_t sum; \
3387
    sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3388
    RESULT(sum, n, 8); \
3389
    if ((sum >> 8) == 1) \
3390
        ge |= 1 << n; \
3391
    } while(0)
3392

    
3393
#define SUB16(a, b, n) do { \
3394
    uint32_t sum; \
3395
    sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3396
    RESULT(sum, n, 16); \
3397
    if ((sum >> 16) == 0) \
3398
        ge |= 3 << (n * 2); \
3399
    } while(0)
3400

    
3401
#define SUB8(a, b, n) do { \
3402
    uint32_t sum; \
3403
    sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3404
    RESULT(sum, n, 8); \
3405
    if ((sum >> 8) == 0) \
3406
        ge |= 1 << n; \
3407
    } while(0)
3408

    
3409
#define PFX u
3410
#define ARITH_GE
3411

    
3412
#include "op_addsub.h"
3413

    
3414
/* Halved signed arithmetic.  */
3415
#define ADD16(a, b, n) \
3416
  RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3417
#define SUB16(a, b, n) \
3418
  RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3419
#define ADD8(a, b, n) \
3420
  RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3421
#define SUB8(a, b, n) \
3422
  RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3423
#define PFX sh
3424

    
3425
#include "op_addsub.h"
3426

    
3427
/* Halved unsigned arithmetic.  */
3428
#define ADD16(a, b, n) \
3429
  RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3430
#define SUB16(a, b, n) \
3431
  RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3432
#define ADD8(a, b, n) \
3433
  RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3434
#define SUB8(a, b, n) \
3435
  RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3436
#define PFX uh
3437

    
3438
#include "op_addsub.h"
3439

    
3440
static inline uint8_t do_usad(uint8_t a, uint8_t b)
3441
{
3442
    if (a > b)
3443
        return a - b;
3444
    else
3445
        return b - a;
3446
}
3447

    
3448
/* Unsigned sum of absolute byte differences.  */
3449
uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3450
{
3451
    uint32_t sum;
3452
    sum = do_usad(a, b);
3453
    sum += do_usad(a >> 8, b >> 8);
3454
    sum += do_usad(a >> 16, b >>16);
3455
    sum += do_usad(a >> 24, b >> 24);
3456
    return sum;
3457
}
3458

    
3459
/* For ARMv6 SEL instruction.  */
3460
uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3461
{
3462
    uint32_t mask;
3463

    
3464
    mask = 0;
3465
    if (flags & 1)
3466
        mask |= 0xff;
3467
    if (flags & 2)
3468
        mask |= 0xff00;
3469
    if (flags & 4)
3470
        mask |= 0xff0000;
3471
    if (flags & 8)
3472
        mask |= 0xff000000;
3473
    return (a & mask) | (b & ~mask);
3474
}
3475

    
3476
/* VFP support.  We follow the convention used for VFP instructions:
3477
   Single precision routines have a "s" suffix, double precision a
3478
   "d" suffix.  */
3479

    
3480
/* Convert host exception flags to vfp form.  */
3481
static inline int vfp_exceptbits_from_host(int host_bits)
3482
{
3483
    int target_bits = 0;
3484

    
3485
    if (host_bits & float_flag_invalid)
3486
        target_bits |= 1;
3487
    if (host_bits & float_flag_divbyzero)
3488
        target_bits |= 2;
3489
    if (host_bits & float_flag_overflow)
3490
        target_bits |= 4;
3491
    if (host_bits & (float_flag_underflow | float_flag_output_denormal))
3492
        target_bits |= 8;
3493
    if (host_bits & float_flag_inexact)
3494
        target_bits |= 0x10;
3495
    if (host_bits & float_flag_input_denormal)
3496
        target_bits |= 0x80;
3497
    return target_bits;
3498
}
3499

    
3500
uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
3501
{
3502
    int i;
3503
    uint32_t fpscr;
3504

    
3505
    fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3506
            | (env->vfp.vec_len << 16)
3507
            | (env->vfp.vec_stride << 20);
3508
    i = get_float_exception_flags(&env->vfp.fp_status);
3509
    i |= get_float_exception_flags(&env->vfp.standard_fp_status);
3510
    fpscr |= vfp_exceptbits_from_host(i);
3511
    return fpscr;
3512
}
3513

    
3514
uint32_t vfp_get_fpscr(CPUARMState *env)
3515
{
3516
    return HELPER(vfp_get_fpscr)(env);
3517
}
3518

    
3519
/* Convert vfp exception flags to target form.  */
3520
static inline int vfp_exceptbits_to_host(int target_bits)
3521
{
3522
    int host_bits = 0;
3523

    
3524
    if (target_bits & 1)
3525
        host_bits |= float_flag_invalid;
3526
    if (target_bits & 2)
3527
        host_bits |= float_flag_divbyzero;
3528
    if (target_bits & 4)
3529
        host_bits |= float_flag_overflow;
3530
    if (target_bits & 8)
3531
        host_bits |= float_flag_underflow;
3532
    if (target_bits & 0x10)
3533
        host_bits |= float_flag_inexact;
3534
    if (target_bits & 0x80)
3535
        host_bits |= float_flag_input_denormal;
3536
    return host_bits;
3537
}
3538

    
3539
void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
3540
{
3541
    int i;
3542
    uint32_t changed;
3543

    
3544
    changed = env->vfp.xregs[ARM_VFP_FPSCR];
3545
    env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3546
    env->vfp.vec_len = (val >> 16) & 7;
3547
    env->vfp.vec_stride = (val >> 20) & 3;
3548

    
3549
    changed ^= val;
3550
    if (changed & (3 << 22)) {
3551
        i = (val >> 22) & 3;
3552
        switch (i) {
3553
        case 0:
3554
            i = float_round_nearest_even;
3555
            break;
3556
        case 1:
3557
            i = float_round_up;
3558
            break;
3559
        case 2:
3560
            i = float_round_down;
3561
            break;
3562
        case 3:
3563
            i = float_round_to_zero;
3564
            break;
3565
        }
3566
        set_float_rounding_mode(i, &env->vfp.fp_status);
3567
    }
3568
    if (changed & (1 << 24)) {
3569
        set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3570
        set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3571
    }
3572
    if (changed & (1 << 25))
3573
        set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
3574

    
3575
    i = vfp_exceptbits_to_host(val);
3576
    set_float_exception_flags(i, &env->vfp.fp_status);
3577
    set_float_exception_flags(0, &env->vfp.standard_fp_status);
3578
}
3579

    
3580
void vfp_set_fpscr(CPUARMState *env, uint32_t val)
3581
{
3582
    HELPER(vfp_set_fpscr)(env, val);
3583
}
3584

    
3585
#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3586

    
3587
#define VFP_BINOP(name) \
3588
float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
3589
{ \
3590
    float_status *fpst = fpstp; \
3591
    return float32_ ## name(a, b, fpst); \
3592
} \
3593
float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
3594
{ \
3595
    float_status *fpst = fpstp; \
3596
    return float64_ ## name(a, b, fpst); \
3597
}
3598
VFP_BINOP(add)
3599
VFP_BINOP(sub)
3600
VFP_BINOP(mul)
3601
VFP_BINOP(div)
3602
#undef VFP_BINOP
3603

    
3604
float32 VFP_HELPER(neg, s)(float32 a)
3605
{
3606
    return float32_chs(a);
3607
}
3608

    
3609
float64 VFP_HELPER(neg, d)(float64 a)
3610
{
3611
    return float64_chs(a);
3612
}
3613

    
3614
float32 VFP_HELPER(abs, s)(float32 a)
3615
{
3616
    return float32_abs(a);
3617
}
3618

    
3619
float64 VFP_HELPER(abs, d)(float64 a)
3620
{
3621
    return float64_abs(a);
3622
}
3623

    
3624
float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
3625
{
3626
    return float32_sqrt(a, &env->vfp.fp_status);
3627
}
3628

    
3629
float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
3630
{
3631
    return float64_sqrt(a, &env->vfp.fp_status);
3632
}
3633

    
3634
/* XXX: check quiet/signaling case */
3635
#define DO_VFP_cmp(p, type) \
3636
void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env)  \
3637
{ \
3638
    uint32_t flags; \
3639
    switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3640
    case 0: flags = 0x6; break; \
3641
    case -1: flags = 0x8; break; \
3642
    case 1: flags = 0x2; break; \
3643
    default: case 2: flags = 0x3; break; \
3644
    } \
3645
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3646
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3647
} \
3648
void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
3649
{ \
3650
    uint32_t flags; \
3651
    switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3652
    case 0: flags = 0x6; break; \
3653
    case -1: flags = 0x8; break; \
3654
    case 1: flags = 0x2; break; \
3655
    default: case 2: flags = 0x3; break; \
3656
    } \
3657
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3658
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3659
}
3660
DO_VFP_cmp(s, float32)
3661
DO_VFP_cmp(d, float64)
3662
#undef DO_VFP_cmp
3663

    
3664
/* Integer to float and float to integer conversions */
3665

    
3666
#define CONV_ITOF(name, fsz, sign) \
3667
    float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3668
{ \
3669
    float_status *fpst = fpstp; \
3670
    return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
3671
}
3672

    
3673
#define CONV_FTOI(name, fsz, sign, round) \
3674
uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3675
{ \
3676
    float_status *fpst = fpstp; \
3677
    if (float##fsz##_is_any_nan(x)) { \
3678
        float_raise(float_flag_invalid, fpst); \
3679
        return 0; \
3680
    } \
3681
    return float##fsz##_to_##sign##int32##round(x, fpst); \
3682
}
3683

    
3684
#define FLOAT_CONVS(name, p, fsz, sign) \
3685
CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3686
CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3687
CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
3688

    
3689
FLOAT_CONVS(si, s, 32, )
3690
FLOAT_CONVS(si, d, 64, )
3691
FLOAT_CONVS(ui, s, 32, u)
3692
FLOAT_CONVS(ui, d, 64, u)
3693

    
3694
#undef CONV_ITOF
3695
#undef CONV_FTOI
3696
#undef FLOAT_CONVS
3697

    
3698
/* floating point conversion */
3699
float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
3700
{
3701
    float64 r = float32_to_float64(x, &env->vfp.fp_status);
3702
    /* ARM requires that S<->D conversion of any kind of NaN generates
3703
     * a quiet NaN by forcing the most significant frac bit to 1.
3704
     */
3705
    return float64_maybe_silence_nan(r);
3706
}
3707

    
3708
float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
3709
{
3710
    float32 r =  float64_to_float32(x, &env->vfp.fp_status);
3711
    /* ARM requires that S<->D conversion of any kind of NaN generates
3712
     * a quiet NaN by forcing the most significant frac bit to 1.
3713
     */
3714
    return float32_maybe_silence_nan(r);
3715
}
3716

    
3717
/* VFP3 fixed point conversion.  */
3718
#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
3719
float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t  x, uint32_t shift, \
3720
                                    void *fpstp) \
3721
{ \
3722
    float_status *fpst = fpstp; \
3723
    float##fsz tmp; \
3724
    tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
3725
    return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
3726
} \
3727
uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
3728
                                       void *fpstp) \
3729
{ \
3730
    float_status *fpst = fpstp; \
3731
    float##fsz tmp; \
3732
    if (float##fsz##_is_any_nan(x)) { \
3733
        float_raise(float_flag_invalid, fpst); \
3734
        return 0; \
3735
    } \
3736
    tmp = float##fsz##_scalbn(x, shift, fpst); \
3737
    return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
3738
}
3739

    
3740
VFP_CONV_FIX(sh, d, 64, int16, )
3741
VFP_CONV_FIX(sl, d, 64, int32, )
3742
VFP_CONV_FIX(uh, d, 64, uint16, u)
3743
VFP_CONV_FIX(ul, d, 64, uint32, u)
3744
VFP_CONV_FIX(sh, s, 32, int16, )
3745
VFP_CONV_FIX(sl, s, 32, int32, )
3746
VFP_CONV_FIX(uh, s, 32, uint16, u)
3747
VFP_CONV_FIX(ul, s, 32, uint32, u)
3748
#undef VFP_CONV_FIX
3749

    
3750
/* Half precision conversions.  */
3751
static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
3752
{
3753
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3754
    float32 r = float16_to_float32(make_float16(a), ieee, s);
3755
    if (ieee) {
3756
        return float32_maybe_silence_nan(r);
3757
    }
3758
    return r;
3759
}
3760

    
3761
static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
3762
{
3763
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3764
    float16 r = float32_to_float16(a, ieee, s);
3765
    if (ieee) {
3766
        r = float16_maybe_silence_nan(r);
3767
    }
3768
    return float16_val(r);
3769
}
3770

    
3771
float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3772
{
3773
    return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
3774
}
3775

    
3776
uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3777
{
3778
    return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
3779
}
3780

    
3781
float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3782
{
3783
    return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
3784
}
3785

    
3786
uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3787
{
3788
    return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
3789
}
3790

    
3791
#define float32_two make_float32(0x40000000)
3792
#define float32_three make_float32(0x40400000)
3793
#define float32_one_point_five make_float32(0x3fc00000)
3794

    
3795
float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
3796
{
3797
    float_status *s = &env->vfp.standard_fp_status;
3798
    if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3799
        (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3800
        if (!(float32_is_zero(a) || float32_is_zero(b))) {
3801
            float_raise(float_flag_input_denormal, s);
3802
        }
3803
        return float32_two;
3804
    }
3805
    return float32_sub(float32_two, float32_mul(a, b, s), s);
3806
}
3807

    
3808
float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
3809
{
3810
    float_status *s = &env->vfp.standard_fp_status;
3811
    float32 product;
3812
    if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3813
        (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3814
        if (!(float32_is_zero(a) || float32_is_zero(b))) {
3815
            float_raise(float_flag_input_denormal, s);
3816
        }
3817
        return float32_one_point_five;
3818
    }
3819
    product = float32_mul(a, b, s);
3820
    return float32_div(float32_sub(float32_three, product, s), float32_two, s);
3821
}
3822

    
3823
/* NEON helpers.  */
3824

    
3825
/* Constants 256 and 512 are used in some helpers; we avoid relying on
3826
 * int->float conversions at run-time.  */
3827
#define float64_256 make_float64(0x4070000000000000LL)
3828
#define float64_512 make_float64(0x4080000000000000LL)
3829

    
3830
/* The algorithm that must be used to calculate the estimate
3831
 * is specified by the ARM ARM.
3832
 */
3833
static float64 recip_estimate(float64 a, CPUARMState *env)
3834
{
3835
    /* These calculations mustn't set any fp exception flags,
3836
     * so we use a local copy of the fp_status.
3837
     */
3838
    float_status dummy_status = env->vfp.standard_fp_status;
3839
    float_status *s = &dummy_status;
3840
    /* q = (int)(a * 512.0) */
3841
    float64 q = float64_mul(float64_512, a, s);
3842
    int64_t q_int = float64_to_int64_round_to_zero(q, s);
3843

    
3844
    /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3845
    q = int64_to_float64(q_int, s);
3846
    q = float64_add(q, float64_half, s);
3847
    q = float64_div(q, float64_512, s);
3848
    q = float64_div(float64_one, q, s);
3849

    
3850
    /* s = (int)(256.0 * r + 0.5) */
3851
    q = float64_mul(q, float64_256, s);
3852
    q = float64_add(q, float64_half, s);
3853
    q_int = float64_to_int64_round_to_zero(q, s);
3854

    
3855
    /* return (double)s / 256.0 */
3856
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
3857
}
3858

    
3859
float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
3860
{
3861
    float_status *s = &env->vfp.standard_fp_status;
3862
    float64 f64;
3863
    uint32_t val32 = float32_val(a);
3864

    
3865
    int result_exp;
3866
    int a_exp = (val32  & 0x7f800000) >> 23;
3867
    int sign = val32 & 0x80000000;
3868

    
3869
    if (float32_is_any_nan(a)) {
3870
        if (float32_is_signaling_nan(a)) {
3871
            float_raise(float_flag_invalid, s);
3872
        }
3873
        return float32_default_nan;
3874
    } else if (float32_is_infinity(a)) {
3875
        return float32_set_sign(float32_zero, float32_is_neg(a));
3876
    } else if (float32_is_zero_or_denormal(a)) {
3877
        if (!float32_is_zero(a)) {
3878
            float_raise(float_flag_input_denormal, s);
3879
        }
3880
        float_raise(float_flag_divbyzero, s);
3881
        return float32_set_sign(float32_infinity, float32_is_neg(a));
3882
    } else if (a_exp >= 253) {
3883
        float_raise(float_flag_underflow, s);
3884
        return float32_set_sign(float32_zero, float32_is_neg(a));
3885
    }
3886

    
3887
    f64 = make_float64((0x3feULL << 52)
3888
                       | ((int64_t)(val32 & 0x7fffff) << 29));
3889

    
3890
    result_exp = 253 - a_exp;
3891

    
3892
    f64 = recip_estimate(f64, env);
3893

    
3894
    val32 = sign
3895
        | ((result_exp & 0xff) << 23)
3896
        | ((float64_val(f64) >> 29) & 0x7fffff);
3897
    return make_float32(val32);
3898
}
3899

    
3900
/* The algorithm that must be used to calculate the estimate
3901
 * is specified by the ARM ARM.
3902
 */
3903
static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3904
{
3905
    /* These calculations mustn't set any fp exception flags,
3906
     * so we use a local copy of the fp_status.
3907
     */
3908
    float_status dummy_status = env->vfp.standard_fp_status;
3909
    float_status *s = &dummy_status;
3910
    float64 q;
3911
    int64_t q_int;
3912

    
3913
    if (float64_lt(a, float64_half, s)) {
3914
        /* range 0.25 <= a < 0.5 */
3915

    
3916
        /* a in units of 1/512 rounded down */
3917
        /* q0 = (int)(a * 512.0);  */
3918
        q = float64_mul(float64_512, a, s);
3919
        q_int = float64_to_int64_round_to_zero(q, s);
3920

    
3921
        /* reciprocal root r */
3922
        /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0);  */
3923
        q = int64_to_float64(q_int, s);
3924
        q = float64_add(q, float64_half, s);
3925
        q = float64_div(q, float64_512, s);
3926
        q = float64_sqrt(q, s);
3927
        q = float64_div(float64_one, q, s);
3928
    } else {
3929
        /* range 0.5 <= a < 1.0 */
3930

    
3931
        /* a in units of 1/256 rounded down */
3932
        /* q1 = (int)(a * 256.0); */
3933
        q = float64_mul(float64_256, a, s);
3934
        int64_t q_int = float64_to_int64_round_to_zero(q, s);
3935

    
3936
        /* reciprocal root r */
3937
        /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3938
        q = int64_to_float64(q_int, s);
3939
        q = float64_add(q, float64_half, s);
3940
        q = float64_div(q, float64_256, s);
3941
        q = float64_sqrt(q, s);
3942
        q = float64_div(float64_one, q, s);
3943
    }
3944
    /* r in units of 1/256 rounded to nearest */
3945
    /* s = (int)(256.0 * r + 0.5); */
3946

    
3947
    q = float64_mul(q, float64_256,s );
3948
    q = float64_add(q, float64_half, s);
3949
    q_int = float64_to_int64_round_to_zero(q, s);
3950

    
3951
    /* return (double)s / 256.0;*/
3952
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
3953
}
3954

    
3955
float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3956
{
3957
    float_status *s = &env->vfp.standard_fp_status;
3958
    int result_exp;
3959
    float64 f64;
3960
    uint32_t val;
3961
    uint64_t val64;
3962

    
3963
    val = float32_val(a);
3964

    
3965
    if (float32_is_any_nan(a)) {
3966
        if (float32_is_signaling_nan(a)) {
3967
            float_raise(float_flag_invalid, s);
3968
        }
3969
        return float32_default_nan;
3970
    } else if (float32_is_zero_or_denormal(a)) {
3971
        if (!float32_is_zero(a)) {
3972
            float_raise(float_flag_input_denormal, s);
3973
        }
3974
        float_raise(float_flag_divbyzero, s);
3975
        return float32_set_sign(float32_infinity, float32_is_neg(a));
3976
    } else if (float32_is_neg(a)) {
3977
        float_raise(float_flag_invalid, s);
3978
        return float32_default_nan;
3979
    } else if (float32_is_infinity(a)) {
3980
        return float32_zero;
3981
    }
3982

    
3983
    /* Normalize to a double-precision value between 0.25 and 1.0,
3984
     * preserving the parity of the exponent.  */
3985
    if ((val & 0x800000) == 0) {
3986
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3987
                           | (0x3feULL << 52)
3988
                           | ((uint64_t)(val & 0x7fffff) << 29));
3989
    } else {
3990
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3991
                           | (0x3fdULL << 52)
3992
                           | ((uint64_t)(val & 0x7fffff) << 29));
3993
    }
3994

    
3995
    result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3996

    
3997
    f64 = recip_sqrt_estimate(f64, env);
3998

    
3999
    val64 = float64_val(f64);
4000

    
4001
    val = ((result_exp & 0xff) << 23)
4002
        | ((val64 >> 29)  & 0x7fffff);
4003
    return make_float32(val);
4004
}
4005

    
4006
uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
4007
{
4008
    float64 f64;
4009

    
4010
    if ((a & 0x80000000) == 0) {
4011
        return 0xffffffff;
4012
    }
4013

    
4014
    f64 = make_float64((0x3feULL << 52)
4015
                       | ((int64_t)(a & 0x7fffffff) << 21));
4016

    
4017
    f64 = recip_estimate (f64, env);
4018

    
4019
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4020
}
4021

    
4022
uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
4023
{
4024
    float64 f64;
4025

    
4026
    if ((a & 0xc0000000) == 0) {
4027
        return 0xffffffff;
4028
    }
4029

    
4030
    if (a & 0x80000000) {
4031
        f64 = make_float64((0x3feULL << 52)
4032
                           | ((uint64_t)(a & 0x7fffffff) << 21));
4033
    } else { /* bits 31-30 == '01' */
4034
        f64 = make_float64((0x3fdULL << 52)
4035
                           | ((uint64_t)(a & 0x3fffffff) << 22));
4036
    }
4037

    
4038
    f64 = recip_sqrt_estimate(f64, env);
4039

    
4040
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4041
}
4042

    
4043
/* VFPv4 fused multiply-accumulate */
4044
float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
4045
{
4046
    float_status *fpst = fpstp;
4047
    return float32_muladd(a, b, c, 0, fpst);
4048
}
4049

    
4050
float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
4051
{
4052
    float_status *fpst = fpstp;
4053
    return float64_muladd(a, b, c, 0, fpst);
4054
}