Statistics
| Branch: | Revision:

root / target-arm / helper.c @ 8641136c

History | View | Annotate | Download (130.8 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 vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
541
                      uint64_t value)
542
{
543
    env->cp15.c12_vbar = value & ~0x1Ful;
544
    return 0;
545
}
546

    
547
static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
548
                       uint64_t *value)
549
{
550
    ARMCPU *cpu = arm_env_get_cpu(env);
551
    *value = cpu->ccsidr[env->cp15.c0_cssel];
552
    return 0;
553
}
554

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

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

    
652
static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
653
{
654
    value &= 1;
655
    env->teecr = value;
656
    return 0;
657
}
658

    
659
static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
660
                       uint64_t *value)
661
{
662
    /* This is a helper function because the user access rights
663
     * depend on the value of the TEECR.
664
     */
665
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
666
        return EXCP_UDEF;
667
    }
668
    *value = env->teehbr;
669
    return 0;
670
}
671

    
672
static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
673
                        uint64_t value)
674
{
675
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
676
        return EXCP_UDEF;
677
    }
678
    env->teehbr = value;
679
    return 0;
680
}
681

    
682
static const ARMCPRegInfo t2ee_cp_reginfo[] = {
683
    { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
684
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
685
      .resetvalue = 0,
686
      .writefn = teecr_write },
687
    { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
688
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
689
      .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write,
690
      .readfn = teehbr_read, .writefn = teehbr_write },
691
    REGINFO_SENTINEL
692
};
693

    
694
static const ARMCPRegInfo v6k_cp_reginfo[] = {
695
    { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
696
      .access = PL0_RW,
697
      .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
698
      .resetvalue = 0 },
699
    { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
700
      .access = PL0_R|PL1_W,
701
      .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
702
      .resetvalue = 0 },
703
    { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
704
      .access = PL1_RW,
705
      .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
706
      .resetvalue = 0 },
707
    REGINFO_SENTINEL
708
};
709

    
710
#ifndef CONFIG_USER_ONLY
711

    
712
static uint64_t gt_get_countervalue(CPUARMState *env)
713
{
714
    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
715
}
716

    
717
static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
718
{
719
    ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
720

    
721
    if (gt->ctl & 1) {
722
        /* Timer enabled: calculate and set current ISTATUS, irq, and
723
         * reset timer to when ISTATUS next has to change
724
         */
725
        uint64_t count = gt_get_countervalue(&cpu->env);
726
        /* Note that this must be unsigned 64 bit arithmetic: */
727
        int istatus = count >= gt->cval;
728
        uint64_t nexttick;
729

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

    
757
static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri,
758
                          uint64_t *value)
759
{
760
    /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
761
    if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
762
        return EXCP_UDEF;
763
    }
764
    *value = env->cp15.c14_cntfrq;
765
    return 0;
766
}
767

    
768
static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
769
{
770
    ARMCPU *cpu = arm_env_get_cpu(env);
771
    int timeridx = ri->opc1 & 1;
772

    
773
    timer_del(cpu->gt_timer[timeridx]);
774
}
775

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

    
781
    if (arm_current_pl(env) == 0 &&
782
        !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
783
        return EXCP_UDEF;
784
    }
785
    *value = gt_get_countervalue(env);
786
    return 0;
787
}
788

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

    
794
    if (arm_current_pl(env) == 0 &&
795
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
796
        return EXCP_UDEF;
797
    }
798
    *value = env->cp15.c14_timer[timeridx].cval;
799
    return 0;
800
}
801

    
802
static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
803
                         uint64_t value)
804
{
805
    int timeridx = ri->opc1 & 1;
806

    
807
    env->cp15.c14_timer[timeridx].cval = value;
808
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
809
    return 0;
810
}
811
static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
812
                        uint64_t *value)
813
{
814
    int timeridx = ri->crm & 1;
815

    
816
    if (arm_current_pl(env) == 0 &&
817
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
818
        return EXCP_UDEF;
819
    }
820
    *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval -
821
                        gt_get_countervalue(env));
822
    return 0;
823
}
824

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

    
830
    env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
831
        + sextract64(value, 0, 32);
832
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
833
    return 0;
834
}
835

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

    
841
    if (arm_current_pl(env) == 0 &&
842
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
843
        return EXCP_UDEF;
844
    }
845
    *value = env->cp15.c14_timer[timeridx].ctl;
846
    return 0;
847
}
848

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

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

    
870
void arm_gt_ptimer_cb(void *opaque)
871
{
872
    ARMCPU *cpu = opaque;
873

    
874
    gt_recalc_timer(cpu, GTIMER_PHYS);
875
}
876

    
877
void arm_gt_vtimer_cb(void *opaque)
878
{
879
    ARMCPU *cpu = opaque;
880

    
881
    gt_recalc_timer(cpu, GTIMER_VIRT);
882
}
883

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

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

    
963
#endif
964

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

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

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

    
990
static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
991
{
992
    hwaddr phys_addr;
993
    target_ulong page_size;
994
    int prot;
995
    int ret, is_user = ri->opc2 & 2;
996
    int access_type = ri->opc2 & 1;
997

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

    
1047
static const ARMCPRegInfo vapa_cp_reginfo[] = {
1048
    { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1049
      .access = PL1_RW, .resetvalue = 0,
1050
      .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
1051
      .writefn = par_write },
1052
#ifndef CONFIG_USER_ONLY
1053
    { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1054
      .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1055
#endif
1056
    REGINFO_SENTINEL
1057
};
1058

    
1059
/* Return basic MPU access permission bits.  */
1060
static uint32_t simple_mpu_ap_bits(uint32_t val)
1061
{
1062
    uint32_t ret;
1063
    uint32_t mask;
1064
    int i;
1065
    ret = 0;
1066
    mask = 3;
1067
    for (i = 0; i < 16; i += 2) {
1068
        ret |= (val >> i) & mask;
1069
        mask <<= 2;
1070
    }
1071
    return ret;
1072
}
1073

    
1074
/* Pad basic MPU access permission bits to extended format.  */
1075
static uint32_t extended_mpu_ap_bits(uint32_t val)
1076
{
1077
    uint32_t ret;
1078
    uint32_t mask;
1079
    int i;
1080
    ret = 0;
1081
    mask = 3;
1082
    for (i = 0; i < 16; i += 2) {
1083
        ret |= (val & mask) << i;
1084
        mask <<= 2;
1085
    }
1086
    return ret;
1087
}
1088

    
1089
static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1090
                                uint64_t value)
1091
{
1092
    env->cp15.c5_data = extended_mpu_ap_bits(value);
1093
    return 0;
1094
}
1095

    
1096
static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1097
                               uint64_t *value)
1098
{
1099
    *value = simple_mpu_ap_bits(env->cp15.c5_data);
1100
    return 0;
1101
}
1102

    
1103
static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1104
                                uint64_t value)
1105
{
1106
    env->cp15.c5_insn = extended_mpu_ap_bits(value);
1107
    return 0;
1108
}
1109

    
1110
static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1111
                               uint64_t *value)
1112
{
1113
    *value = simple_mpu_ap_bits(env->cp15.c5_insn);
1114
    return 0;
1115
}
1116

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

    
1127
static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
1128
                             uint64_t value)
1129
{
1130
    if (ri->crm >= 8) {
1131
        return EXCP_UDEF;
1132
    }
1133
    env->cp15.c6_region[ri->crm] = value;
1134
    return 0;
1135
}
1136

    
1137
static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1138
    { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1139
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1140
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
1141
      .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1142
    { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1143
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1144
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
1145
      .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1146
    { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1147
      .access = PL1_RW,
1148
      .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
1149
    { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1150
      .access = PL1_RW,
1151
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1152
    { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1153
      .access = PL1_RW,
1154
      .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1155
    { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1156
      .access = PL1_RW,
1157
      .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1158
    /* Protection region base and size registers */
1159
    { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
1160
      .opc2 = CP_ANY, .access = PL1_RW,
1161
      .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
1162
    REGINFO_SENTINEL
1163
};
1164

    
1165
static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1166
                                uint64_t value)
1167
{
1168
    int maskshift = extract32(value, 0, 3);
1169

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1417
static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1418
{
1419
    *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1420
    return 0;
1421
}
1422

    
1423
static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1424
{
1425
    env->cp15.c7_par_hi = value >> 32;
1426
    env->cp15.c7_par = value;
1427
    return 0;
1428
}
1429

    
1430
static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1431
{
1432
    env->cp15.c7_par_hi = 0;
1433
    env->cp15.c7_par = 0;
1434
}
1435

    
1436
static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1437
                        uint64_t *value)
1438
{
1439
    *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1440
    return 0;
1441
}
1442

    
1443
static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1444
                             uint64_t value)
1445
{
1446
    env->cp15.c2_base0_hi = value >> 32;
1447
    env->cp15.c2_base0 = value;
1448
    return 0;
1449
}
1450

    
1451
static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1452
                         uint64_t value)
1453
{
1454
    /* Writes to the 64 bit format TTBRs may change the ASID */
1455
    tlb_flush(env, 1);
1456
    return ttbr064_raw_write(env, ri, value);
1457
}
1458

    
1459
static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1460
{
1461
    env->cp15.c2_base0_hi = 0;
1462
    env->cp15.c2_base0 = 0;
1463
}
1464

    
1465
static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1466
                        uint64_t *value)
1467
{
1468
    *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1469
    return 0;
1470
}
1471

    
1472
static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1473
                         uint64_t value)
1474
{
1475
    env->cp15.c2_base1_hi = value >> 32;
1476
    env->cp15.c2_base1 = value;
1477
    return 0;
1478
}
1479

    
1480
static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1481
{
1482
    env->cp15.c2_base1_hi = 0;
1483
    env->cp15.c2_base1 = 0;
1484
}
1485

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

    
1514
static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1515
{
1516
    env->cp15.c1_sys = value;
1517
    /* ??? Lots of these bits are not implemented.  */
1518
    /* This may enable/disable the MMU, so do a TLB flush.  */
1519
    tlb_flush(env, 1);
1520
    return 0;
1521
}
1522

    
1523
void register_cp_regs_for_features(ARMCPU *cpu)
1524
{
1525
    /* Register all the coprocessor registers based on feature bits */
1526
    CPUARMState *env = &cpu->env;
1527
    if (arm_feature(env, ARM_FEATURE_M)) {
1528
        /* M profile has no coprocessor registers */
1529
        return;
1530
    }
1531

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

    
1728
    if (arm_feature(env, ARM_FEATURE_MPIDR)) {
1729
        define_arm_cp_regs(cpu, mpidr_cp_reginfo);
1730
    }
1731

    
1732
    if (arm_feature(env, ARM_FEATURE_AUXCR)) {
1733
        ARMCPRegInfo auxcr = {
1734
            .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
1735
            .access = PL1_RW, .type = ARM_CP_CONST,
1736
            .resetvalue = cpu->reset_auxcr
1737
        };
1738
        define_one_arm_cp_reg(cpu, &auxcr);
1739
    }
1740

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

    
1760
ARMCPU *cpu_arm_init(const char *cpu_model)
1761
{
1762
    ARMCPU *cpu;
1763
    ObjectClass *oc;
1764

    
1765
    oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1766
    if (!oc) {
1767
        return NULL;
1768
    }
1769
    cpu = ARM_CPU(object_new(object_class_get_name(oc)));
1770

    
1771
    /* TODO this should be set centrally, once possible */
1772
    object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
1773

    
1774
    return cpu;
1775
}
1776

    
1777
void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
1778
{
1779
    CPUState *cs = CPU(cpu);
1780
    CPUARMState *env = &cpu->env;
1781

    
1782
    if (arm_feature(env, ARM_FEATURE_NEON)) {
1783
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1784
                                 51, "arm-neon.xml", 0);
1785
    } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
1786
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1787
                                 35, "arm-vfp3.xml", 0);
1788
    } else if (arm_feature(env, ARM_FEATURE_VFP)) {
1789
        gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
1790
                                 19, "arm-vfp.xml", 0);
1791
    }
1792
}
1793

    
1794
/* Sort alphabetically by type name, except for "any". */
1795
static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
1796
{
1797
    ObjectClass *class_a = (ObjectClass *)a;
1798
    ObjectClass *class_b = (ObjectClass *)b;
1799
    const char *name_a, *name_b;
1800

    
1801
    name_a = object_class_get_name(class_a);
1802
    name_b = object_class_get_name(class_b);
1803
    if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
1804
        return 1;
1805
    } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
1806
        return -1;
1807
    } else {
1808
        return strcmp(name_a, name_b);
1809
    }
1810
}
1811

    
1812
static void arm_cpu_list_entry(gpointer data, gpointer user_data)
1813
{
1814
    ObjectClass *oc = data;
1815
    CPUListState *s = user_data;
1816
    const char *typename;
1817
    char *name;
1818

    
1819
    typename = object_class_get_name(oc);
1820
    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
1821
    (*s->cpu_fprintf)(s->file, "  %s\n",
1822
                      name);
1823
    g_free(name);
1824
}
1825

    
1826
void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1827
{
1828
    CPUListState s = {
1829
        .file = f,
1830
        .cpu_fprintf = cpu_fprintf,
1831
    };
1832
    GSList *list;
1833

    
1834
    list = object_class_get_list(TYPE_ARM_CPU, false);
1835
    list = g_slist_sort(list, arm_cpu_list_compare);
1836
    (*cpu_fprintf)(f, "Available CPUs:\n");
1837
    g_slist_foreach(list, arm_cpu_list_entry, &s);
1838
    g_slist_free(list);
1839
}
1840

    
1841
static void arm_cpu_add_definition(gpointer data, gpointer user_data)
1842
{
1843
    ObjectClass *oc = data;
1844
    CpuDefinitionInfoList **cpu_list = user_data;
1845
    CpuDefinitionInfoList *entry;
1846
    CpuDefinitionInfo *info;
1847
    const char *typename;
1848

    
1849
    typename = object_class_get_name(oc);
1850
    info = g_malloc0(sizeof(*info));
1851
    info->name = g_strndup(typename,
1852
                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
1853

    
1854
    entry = g_malloc0(sizeof(*entry));
1855
    entry->value = info;
1856
    entry->next = *cpu_list;
1857
    *cpu_list = entry;
1858
}
1859

    
1860
CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
1861
{
1862
    CpuDefinitionInfoList *cpu_list = NULL;
1863
    GSList *list;
1864

    
1865
    list = object_class_get_list(TYPE_ARM_CPU, false);
1866
    g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
1867
    g_slist_free(list);
1868

    
1869
    return cpu_list;
1870
}
1871

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

    
1938
                /* Overriding of an existing definition must be explicitly
1939
                 * requested.
1940
                 */
1941
                if (!(r->type & ARM_CP_OVERRIDE)) {
1942
                    ARMCPRegInfo *oldreg;
1943
                    oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1944
                    if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1945
                        fprintf(stderr, "Register redefined: cp=%d %d bit "
1946
                                "crn=%d crm=%d opc1=%d opc2=%d, "
1947
                                "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1948
                                r2->crn, r2->crm, r2->opc1, r2->opc2,
1949
                                oldreg->name, r2->name);
1950
                        g_assert_not_reached();
1951
                    }
1952
                }
1953
                g_hash_table_insert(cpu->cp_regs, key, r2);
1954
            }
1955
        }
1956
    }
1957
}
1958

    
1959
void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1960
                                    const ARMCPRegInfo *regs, void *opaque)
1961
{
1962
    /* Define a whole list of registers */
1963
    const ARMCPRegInfo *r;
1964
    for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1965
        define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1966
    }
1967
}
1968

    
1969
const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1970
{
1971
    return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1972
}
1973

    
1974
int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1975
                        uint64_t value)
1976
{
1977
    /* Helper coprocessor write function for write-ignore registers */
1978
    return 0;
1979
}
1980

    
1981
int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1982
{
1983
    /* Helper coprocessor write function for read-as-zero registers */
1984
    *value = 0;
1985
    return 0;
1986
}
1987

    
1988
static int bad_mode_switch(CPUARMState *env, int mode)
1989
{
1990
    /* Return true if it is not valid for us to switch to
1991
     * this CPU mode (ie all the UNPREDICTABLE cases in
1992
     * the ARM ARM CPSRWriteByInstr pseudocode).
1993
     */
1994
    switch (mode) {
1995
    case ARM_CPU_MODE_USR:
1996
    case ARM_CPU_MODE_SYS:
1997
    case ARM_CPU_MODE_SVC:
1998
    case ARM_CPU_MODE_ABT:
1999
    case ARM_CPU_MODE_UND:
2000
    case ARM_CPU_MODE_IRQ:
2001
    case ARM_CPU_MODE_FIQ:
2002
        return 0;
2003
    default:
2004
        return 1;
2005
    }
2006
}
2007

    
2008
uint32_t cpsr_read(CPUARMState *env)
2009
{
2010
    int ZF;
2011
    ZF = (env->ZF == 0);
2012
    return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2013
        (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2014
        | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2015
        | ((env->condexec_bits & 0xfc) << 8)
2016
        | (env->GE << 16);
2017
}
2018

    
2019
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2020
{
2021
    if (mask & CPSR_NZCV) {
2022
        env->ZF = (~val) & CPSR_Z;
2023
        env->NF = val;
2024
        env->CF = (val >> 29) & 1;
2025
        env->VF = (val << 3) & 0x80000000;
2026
    }
2027
    if (mask & CPSR_Q)
2028
        env->QF = ((val & CPSR_Q) != 0);
2029
    if (mask & CPSR_T)
2030
        env->thumb = ((val & CPSR_T) != 0);
2031
    if (mask & CPSR_IT_0_1) {
2032
        env->condexec_bits &= ~3;
2033
        env->condexec_bits |= (val >> 25) & 3;
2034
    }
2035
    if (mask & CPSR_IT_2_7) {
2036
        env->condexec_bits &= 3;
2037
        env->condexec_bits |= (val >> 8) & 0xfc;
2038
    }
2039
    if (mask & CPSR_GE) {
2040
        env->GE = (val >> 16) & 0xf;
2041
    }
2042

    
2043
    if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
2044
        if (bad_mode_switch(env, val & CPSR_M)) {
2045
            /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2046
             * We choose to ignore the attempt and leave the CPSR M field
2047
             * untouched.
2048
             */
2049
            mask &= ~CPSR_M;
2050
        } else {
2051
            switch_mode(env, val & CPSR_M);
2052
        }
2053
    }
2054
    mask &= ~CACHED_CPSR_BITS;
2055
    env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2056
}
2057

    
2058
/* Sign/zero extend */
2059
uint32_t HELPER(sxtb16)(uint32_t x)
2060
{
2061
    uint32_t res;
2062
    res = (uint16_t)(int8_t)x;
2063
    res |= (uint32_t)(int8_t)(x >> 16) << 16;
2064
    return res;
2065
}
2066

    
2067
uint32_t HELPER(uxtb16)(uint32_t x)
2068
{
2069
    uint32_t res;
2070
    res = (uint16_t)(uint8_t)x;
2071
    res |= (uint32_t)(uint8_t)(x >> 16) << 16;
2072
    return res;
2073
}
2074

    
2075
uint32_t HELPER(clz)(uint32_t x)
2076
{
2077
    return clz32(x);
2078
}
2079

    
2080
int32_t HELPER(sdiv)(int32_t num, int32_t den)
2081
{
2082
    if (den == 0)
2083
      return 0;
2084
    if (num == INT_MIN && den == -1)
2085
      return INT_MIN;
2086
    return num / den;
2087
}
2088

    
2089
uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
2090
{
2091
    if (den == 0)
2092
      return 0;
2093
    return num / den;
2094
}
2095

    
2096
uint32_t HELPER(rbit)(uint32_t x)
2097
{
2098
    x =  ((x & 0xff000000) >> 24)
2099
       | ((x & 0x00ff0000) >> 8)
2100
       | ((x & 0x0000ff00) << 8)
2101
       | ((x & 0x000000ff) << 24);
2102
    x =  ((x & 0xf0f0f0f0) >> 4)
2103
       | ((x & 0x0f0f0f0f) << 4);
2104
    x =  ((x & 0x88888888) >> 3)
2105
       | ((x & 0x44444444) >> 1)
2106
       | ((x & 0x22222222) << 1)
2107
       | ((x & 0x11111111) << 3);
2108
    return x;
2109
}
2110

    
2111
#if defined(CONFIG_USER_ONLY)
2112

    
2113
void arm_cpu_do_interrupt(CPUState *cs)
2114
{
2115
    ARMCPU *cpu = ARM_CPU(cs);
2116
    CPUARMState *env = &cpu->env;
2117

    
2118
    env->exception_index = -1;
2119
}
2120

    
2121
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
2122
                              int mmu_idx)
2123
{
2124
    if (rw == 2) {
2125
        env->exception_index = EXCP_PREFETCH_ABORT;
2126
        env->cp15.c6_insn = address;
2127
    } else {
2128
        env->exception_index = EXCP_DATA_ABORT;
2129
        env->cp15.c6_data = address;
2130
    }
2131
    return 1;
2132
}
2133

    
2134
/* These should probably raise undefined insn exceptions.  */
2135
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2136
{
2137
    cpu_abort(env, "v7m_mrs %d\n", reg);
2138
}
2139

    
2140
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2141
{
2142
    cpu_abort(env, "v7m_mrs %d\n", reg);
2143
    return 0;
2144
}
2145

    
2146
void switch_mode(CPUARMState *env, int mode)
2147
{
2148
    if (mode != ARM_CPU_MODE_USR)
2149
        cpu_abort(env, "Tried to switch out of user mode\n");
2150
}
2151

    
2152
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2153
{
2154
    cpu_abort(env, "banked r13 write\n");
2155
}
2156

    
2157
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2158
{
2159
    cpu_abort(env, "banked r13 read\n");
2160
    return 0;
2161
}
2162

    
2163
#else
2164

    
2165
/* Map CPU modes onto saved register banks.  */
2166
int bank_number(int mode)
2167
{
2168
    switch (mode) {
2169
    case ARM_CPU_MODE_USR:
2170
    case ARM_CPU_MODE_SYS:
2171
        return 0;
2172
    case ARM_CPU_MODE_SVC:
2173
        return 1;
2174
    case ARM_CPU_MODE_ABT:
2175
        return 2;
2176
    case ARM_CPU_MODE_UND:
2177
        return 3;
2178
    case ARM_CPU_MODE_IRQ:
2179
        return 4;
2180
    case ARM_CPU_MODE_FIQ:
2181
        return 5;
2182
    }
2183
    hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
2184
}
2185

    
2186
void switch_mode(CPUARMState *env, int mode)
2187
{
2188
    int old_mode;
2189
    int i;
2190

    
2191
    old_mode = env->uncached_cpsr & CPSR_M;
2192
    if (mode == old_mode)
2193
        return;
2194

    
2195
    if (old_mode == ARM_CPU_MODE_FIQ) {
2196
        memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
2197
        memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
2198
    } else if (mode == ARM_CPU_MODE_FIQ) {
2199
        memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
2200
        memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
2201
    }
2202

    
2203
    i = bank_number(old_mode);
2204
    env->banked_r13[i] = env->regs[13];
2205
    env->banked_r14[i] = env->regs[14];
2206
    env->banked_spsr[i] = env->spsr;
2207

    
2208
    i = bank_number(mode);
2209
    env->regs[13] = env->banked_r13[i];
2210
    env->regs[14] = env->banked_r14[i];
2211
    env->spsr = env->banked_spsr[i];
2212
}
2213

    
2214
static void v7m_push(CPUARMState *env, uint32_t val)
2215
{
2216
    env->regs[13] -= 4;
2217
    stl_phys(env->regs[13], val);
2218
}
2219

    
2220
static uint32_t v7m_pop(CPUARMState *env)
2221
{
2222
    uint32_t val;
2223
    val = ldl_phys(env->regs[13]);
2224
    env->regs[13] += 4;
2225
    return val;
2226
}
2227

    
2228
/* Switch to V7M main or process stack pointer.  */
2229
static void switch_v7m_sp(CPUARMState *env, int process)
2230
{
2231
    uint32_t tmp;
2232
    if (env->v7m.current_sp != process) {
2233
        tmp = env->v7m.other_sp;
2234
        env->v7m.other_sp = env->regs[13];
2235
        env->regs[13] = tmp;
2236
        env->v7m.current_sp = process;
2237
    }
2238
}
2239

    
2240
static void do_v7m_exception_exit(CPUARMState *env)
2241
{
2242
    uint32_t type;
2243
    uint32_t xpsr;
2244

    
2245
    type = env->regs[15];
2246
    if (env->v7m.exception != 0)
2247
        armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
2248

    
2249
    /* Switch to the target stack.  */
2250
    switch_v7m_sp(env, (type & 4) != 0);
2251
    /* Pop registers.  */
2252
    env->regs[0] = v7m_pop(env);
2253
    env->regs[1] = v7m_pop(env);
2254
    env->regs[2] = v7m_pop(env);
2255
    env->regs[3] = v7m_pop(env);
2256
    env->regs[12] = v7m_pop(env);
2257
    env->regs[14] = v7m_pop(env);
2258
    env->regs[15] = v7m_pop(env);
2259
    xpsr = v7m_pop(env);
2260
    xpsr_write(env, xpsr, 0xfffffdff);
2261
    /* Undo stack alignment.  */
2262
    if (xpsr & 0x200)
2263
        env->regs[13] |= 4;
2264
    /* ??? The exception return type specifies Thread/Handler mode.  However
2265
       this is also implied by the xPSR value. Not sure what to do
2266
       if there is a mismatch.  */
2267
    /* ??? Likewise for mismatches between the CONTROL register and the stack
2268
       pointer.  */
2269
}
2270

    
2271
/* Exception names for debug logging; note that not all of these
2272
 * precisely correspond to architectural exceptions.
2273
 */
2274
static const char * const excnames[] = {
2275
    [EXCP_UDEF] = "Undefined Instruction",
2276
    [EXCP_SWI] = "SVC",
2277
    [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
2278
    [EXCP_DATA_ABORT] = "Data Abort",
2279
    [EXCP_IRQ] = "IRQ",
2280
    [EXCP_FIQ] = "FIQ",
2281
    [EXCP_BKPT] = "Breakpoint",
2282
    [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
2283
    [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
2284
    [EXCP_STREX] = "QEMU intercept of STREX",
2285
};
2286

    
2287
static inline void arm_log_exception(int idx)
2288
{
2289
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
2290
        const char *exc = NULL;
2291

    
2292
        if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
2293
            exc = excnames[idx];
2294
        }
2295
        if (!exc) {
2296
            exc = "unknown";
2297
        }
2298
        qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
2299
    }
2300
}
2301

    
2302
void arm_v7m_cpu_do_interrupt(CPUState *cs)
2303
{
2304
    ARMCPU *cpu = ARM_CPU(cs);
2305
    CPUARMState *env = &cpu->env;
2306
    uint32_t xpsr = xpsr_read(env);
2307
    uint32_t lr;
2308
    uint32_t addr;
2309

    
2310
    arm_log_exception(env->exception_index);
2311

    
2312
    lr = 0xfffffff1;
2313
    if (env->v7m.current_sp)
2314
        lr |= 4;
2315
    if (env->v7m.exception == 0)
2316
        lr |= 8;
2317

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

    
2358
    /* Align stack pointer.  */
2359
    /* ??? Should only do this if Configuration Control Register
2360
       STACKALIGN bit is set.  */
2361
    if (env->regs[13] & 4) {
2362
        env->regs[13] -= 4;
2363
        xpsr |= 0x200;
2364
    }
2365
    /* Switch to the handler mode.  */
2366
    v7m_push(env, xpsr);
2367
    v7m_push(env, env->regs[15]);
2368
    v7m_push(env, env->regs[14]);
2369
    v7m_push(env, env->regs[12]);
2370
    v7m_push(env, env->regs[3]);
2371
    v7m_push(env, env->regs[2]);
2372
    v7m_push(env, env->regs[1]);
2373
    v7m_push(env, env->regs[0]);
2374
    switch_v7m_sp(env, 0);
2375
    /* Clear IT bits */
2376
    env->condexec_bits = 0;
2377
    env->regs[14] = lr;
2378
    addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
2379
    env->regs[15] = addr & 0xfffffffe;
2380
    env->thumb = addr & 1;
2381
}
2382

    
2383
/* Handle a CPU exception.  */
2384
void arm_cpu_do_interrupt(CPUState *cs)
2385
{
2386
    ARMCPU *cpu = ARM_CPU(cs);
2387
    CPUARMState *env = &cpu->env;
2388
    uint32_t addr;
2389
    uint32_t mask;
2390
    int new_mode;
2391
    uint32_t offset;
2392

    
2393
    assert(!IS_M(env));
2394

    
2395
    arm_log_exception(env->exception_index);
2396

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

    
2513
/* Check section/page access permissions.
2514
   Returns the page protection flags, or zero if the access is not
2515
   permitted.  */
2516
static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
2517
                           int access_type, int is_user)
2518
{
2519
  int prot_ro;
2520

    
2521
  if (domain_prot == 3) {
2522
    return PAGE_READ | PAGE_WRITE;
2523
  }
2524

    
2525
  if (access_type == 1)
2526
      prot_ro = 0;
2527
  else
2528
      prot_ro = PAGE_READ;
2529

    
2530
  switch (ap) {
2531
  case 0:
2532
      if (access_type == 1)
2533
          return 0;
2534
      switch ((env->cp15.c1_sys >> 8) & 3) {
2535
      case 1:
2536
          return is_user ? 0 : PAGE_READ;
2537
      case 2:
2538
          return PAGE_READ;
2539
      default:
2540
          return 0;
2541
      }
2542
  case 1:
2543
      return is_user ? 0 : PAGE_READ | PAGE_WRITE;
2544
  case 2:
2545
      if (is_user)
2546
          return prot_ro;
2547
      else
2548
          return PAGE_READ | PAGE_WRITE;
2549
  case 3:
2550
      return PAGE_READ | PAGE_WRITE;
2551
  case 4: /* Reserved.  */
2552
      return 0;
2553
  case 5:
2554
      return is_user ? 0 : prot_ro;
2555
  case 6:
2556
      return prot_ro;
2557
  case 7:
2558
      if (!arm_feature (env, ARM_FEATURE_V6K))
2559
          return 0;
2560
      return prot_ro;
2561
  default:
2562
      abort();
2563
  }
2564
}
2565

    
2566
static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
2567
{
2568
    uint32_t table;
2569

    
2570
    if (address & env->cp15.c2_mask)
2571
        table = env->cp15.c2_base1 & 0xffffc000;
2572
    else
2573
        table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
2574

    
2575
    table |= (address >> 18) & 0x3ffc;
2576
    return table;
2577
}
2578

    
2579
static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
2580
                            int is_user, hwaddr *phys_ptr,
2581
                            int *prot, target_ulong *page_size)
2582
{
2583
    int code;
2584
    uint32_t table;
2585
    uint32_t desc;
2586
    int type;
2587
    int ap;
2588
    int domain;
2589
    int domain_prot;
2590
    hwaddr phys_addr;
2591

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

    
2674
static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
2675
                            int is_user, hwaddr *phys_ptr,
2676
                            int *prot, target_ulong *page_size)
2677
{
2678
    int code;
2679
    uint32_t table;
2680
    uint32_t desc;
2681
    uint32_t xn;
2682
    uint32_t pxn = 0;
2683
    int type;
2684
    int ap;
2685
    int domain = 0;
2686
    int domain_prot;
2687
    hwaddr phys_addr;
2688

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

    
2765
        /* The simplified model uses AP[0] as an access control bit.  */
2766
        if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
2767
            /* Access flag fault.  */
2768
            code = (code == 15) ? 6 : 3;
2769
            goto do_fault;
2770
        }
2771
        *prot = check_ap(env, ap, domain_prot, access_type, is_user);
2772
        if (!*prot) {
2773
            /* Access permission fault.  */
2774
            goto do_fault;
2775
        }
2776
        if (!xn) {
2777
            *prot |= PAGE_EXEC;
2778
        }
2779
    }
2780
    *phys_ptr = phys_addr;
2781
    return 0;
2782
do_fault:
2783
    return code | (domain << 4);
2784
}
2785

    
2786
/* Fault type for long-descriptor MMU fault reporting; this corresponds
2787
 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
2788
 */
2789
typedef enum {
2790
    translation_fault = 1,
2791
    access_fault = 2,
2792
    permission_fault = 3,
2793
} MMUFaultType;
2794

    
2795
static int get_phys_addr_lpae(CPUARMState *env, uint32_t address,
2796
                              int access_type, int is_user,
2797
                              hwaddr *phys_ptr, int *prot,
2798
                              target_ulong *page_size_ptr)
2799
{
2800
    /* Read an LPAE long-descriptor translation table. */
2801
    MMUFaultType fault_type = translation_fault;
2802
    uint32_t level = 1;
2803
    uint32_t epd;
2804
    uint32_t tsz;
2805
    uint64_t ttbr;
2806
    int ttbr_select;
2807
    int n;
2808
    hwaddr descaddr;
2809
    uint32_t tableattrs;
2810
    target_ulong page_size;
2811
    uint32_t attrs;
2812

    
2813
    /* Determine whether this address is in the region controlled by
2814
     * TTBR0 or TTBR1 (or if it is in neither region and should fault).
2815
     * This is a Non-secure PL0/1 stage 1 translation, so controlled by
2816
     * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
2817
     */
2818
    uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3);
2819
    uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3);
2820
    if (t0sz && !extract32(address, 32 - t0sz, t0sz)) {
2821
        /* there is a ttbr0 region and we are in it (high bits all zero) */
2822
        ttbr_select = 0;
2823
    } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) {
2824
        /* there is a ttbr1 region and we are in it (high bits all one) */
2825
        ttbr_select = 1;
2826
    } else if (!t0sz) {
2827
        /* ttbr0 region is "everything not in the ttbr1 region" */
2828
        ttbr_select = 0;
2829
    } else if (!t1sz) {
2830
        /* ttbr1 region is "everything not in the ttbr0 region" */
2831
        ttbr_select = 1;
2832
    } else {
2833
        /* in the gap between the two regions, this is a Translation fault */
2834
        fault_type = translation_fault;
2835
        goto do_fault;
2836
    }
2837

    
2838
    /* Note that QEMU ignores shareability and cacheability attributes,
2839
     * so we don't need to do anything with the SH, ORGN, IRGN fields
2840
     * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
2841
     * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
2842
     * implement any ASID-like capability so we can ignore it (instead
2843
     * we will always flush the TLB any time the ASID is changed).
2844
     */
2845
    if (ttbr_select == 0) {
2846
        ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
2847
        epd = extract32(env->cp15.c2_control, 7, 1);
2848
        tsz = t0sz;
2849
    } else {
2850
        ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
2851
        epd = extract32(env->cp15.c2_control, 23, 1);
2852
        tsz = t1sz;
2853
    }
2854

    
2855
    if (epd) {
2856
        /* Translation table walk disabled => Translation fault on TLB miss */
2857
        goto do_fault;
2858
    }
2859

    
2860
    /* If the region is small enough we will skip straight to a 2nd level
2861
     * lookup. This affects the number of bits of the address used in
2862
     * combination with the TTBR to find the first descriptor. ('n' here
2863
     * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are
2864
     * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero).
2865
     */
2866
    if (tsz > 1) {
2867
        level = 2;
2868
        n = 14 - tsz;
2869
    } else {
2870
        n = 5 - tsz;
2871
    }
2872

    
2873
    /* Clear the vaddr bits which aren't part of the within-region address,
2874
     * so that we don't have to special case things when calculating the
2875
     * first descriptor address.
2876
     */
2877
    address &= (0xffffffffU >> tsz);
2878

    
2879
    /* Now we can extract the actual base address from the TTBR */
2880
    descaddr = extract64(ttbr, 0, 40);
2881
    descaddr &= ~((1ULL << n) - 1);
2882

    
2883
    tableattrs = 0;
2884
    for (;;) {
2885
        uint64_t descriptor;
2886

    
2887
        descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
2888
        descriptor = ldq_phys(descaddr);
2889
        if (!(descriptor & 1) ||
2890
            (!(descriptor & 2) && (level == 3))) {
2891
            /* Invalid, or the Reserved level 3 encoding */
2892
            goto do_fault;
2893
        }
2894
        descaddr = descriptor & 0xfffffff000ULL;
2895

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

    
2955
    *phys_ptr = descaddr;
2956
    *page_size_ptr = page_size;
2957
    return 0;
2958

    
2959
do_fault:
2960
    /* Long-descriptor format IFSR/DFSR value */
2961
    return (1 << 9) | (fault_type << 2) | level;
2962
}
2963

    
2964
static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
2965
                             int access_type, int is_user,
2966
                             hwaddr *phys_ptr, int *prot)
2967
{
2968
    int n;
2969
    uint32_t mask;
2970
    uint32_t base;
2971

    
2972
    *phys_ptr = address;
2973
    for (n = 7; n >= 0; n--) {
2974
        base = env->cp15.c6_region[n];
2975
        if ((base & 1) == 0)
2976
            continue;
2977
        mask = 1 << ((base >> 1) & 0x1f);
2978
        /* Keep this shift separate from the above to avoid an
2979
           (undefined) << 32.  */
2980
        mask = (mask << 1) - 1;
2981
        if (((base ^ address) & ~mask) == 0)
2982
            break;
2983
    }
2984
    if (n < 0)
2985
        return 2;
2986

    
2987
    if (access_type == 2) {
2988
        mask = env->cp15.c5_insn;
2989
    } else {
2990
        mask = env->cp15.c5_data;
2991
    }
2992
    mask = (mask >> (n * 4)) & 0xf;
2993
    switch (mask) {
2994
    case 0:
2995
        return 1;
2996
    case 1:
2997
        if (is_user)
2998
          return 1;
2999
        *prot = PAGE_READ | PAGE_WRITE;
3000
        break;
3001
    case 2:
3002
        *prot = PAGE_READ;
3003
        if (!is_user)
3004
            *prot |= PAGE_WRITE;
3005
        break;
3006
    case 3:
3007
        *prot = PAGE_READ | PAGE_WRITE;
3008
        break;
3009
    case 5:
3010
        if (is_user)
3011
            return 1;
3012
        *prot = PAGE_READ;
3013
        break;
3014
    case 6:
3015
        *prot = PAGE_READ;
3016
        break;
3017
    default:
3018
        /* Bad permission.  */
3019
        return 1;
3020
    }
3021
    *prot |= PAGE_EXEC;
3022
    return 0;
3023
}
3024

    
3025
/* get_phys_addr - get the physical address for this virtual address
3026
 *
3027
 * Find the physical address corresponding to the given virtual address,
3028
 * by doing a translation table walk on MMU based systems or using the
3029
 * MPU state on MPU based systems.
3030
 *
3031
 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
3032
 * prot and page_size are not filled in, and the return value provides
3033
 * information on why the translation aborted, in the format of a
3034
 * DFSR/IFSR fault register, with the following caveats:
3035
 *  * we honour the short vs long DFSR format differences.
3036
 *  * the WnR bit is never set (the caller must do this).
3037
 *  * for MPU based systems we don't bother to return a full FSR format
3038
 *    value.
3039
 *
3040
 * @env: CPUARMState
3041
 * @address: virtual address to get physical address for
3042
 * @access_type: 0 for read, 1 for write, 2 for execute
3043
 * @is_user: 0 for privileged access, 1 for user
3044
 * @phys_ptr: set to the physical address corresponding to the virtual address
3045
 * @prot: set to the permissions for the page containing phys_ptr
3046
 * @page_size: set to the size of the page containing phys_ptr
3047
 */
3048
static inline int get_phys_addr(CPUARMState *env, uint32_t address,
3049
                                int access_type, int is_user,
3050
                                hwaddr *phys_ptr, int *prot,
3051
                                target_ulong *page_size)
3052
{
3053
    /* Fast Context Switch Extension.  */
3054
    if (address < 0x02000000)
3055
        address += env->cp15.c13_fcse;
3056

    
3057
    if ((env->cp15.c1_sys & 1) == 0) {
3058
        /* MMU/MPU disabled.  */
3059
        *phys_ptr = address;
3060
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3061
        *page_size = TARGET_PAGE_SIZE;
3062
        return 0;
3063
    } else if (arm_feature(env, ARM_FEATURE_MPU)) {
3064
        *page_size = TARGET_PAGE_SIZE;
3065
        return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
3066
                                 prot);
3067
    } else if (extended_addresses_enabled(env)) {
3068
        return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3069
                                  prot, page_size);
3070
    } else if (env->cp15.c1_sys & (1 << 23)) {
3071
        return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
3072
                                prot, page_size);
3073
    } else {
3074
        return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
3075
                                prot, page_size);
3076
    }
3077
}
3078

    
3079
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
3080
                              int access_type, int mmu_idx)
3081
{
3082
    hwaddr phys_addr;
3083
    target_ulong page_size;
3084
    int prot;
3085
    int ret, is_user;
3086

    
3087
    is_user = mmu_idx == MMU_USER_IDX;
3088
    ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
3089
                        &page_size);
3090
    if (ret == 0) {
3091
        /* Map a single [sub]page.  */
3092
        phys_addr &= ~(hwaddr)0x3ff;
3093
        address &= ~(uint32_t)0x3ff;
3094
        tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
3095
        return 0;
3096
    }
3097

    
3098
    if (access_type == 2) {
3099
        env->cp15.c5_insn = ret;
3100
        env->cp15.c6_insn = address;
3101
        env->exception_index = EXCP_PREFETCH_ABORT;
3102
    } else {
3103
        env->cp15.c5_data = ret;
3104
        if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3105
            env->cp15.c5_data |= (1 << 11);
3106
        env->cp15.c6_data = address;
3107
        env->exception_index = EXCP_DATA_ABORT;
3108
    }
3109
    return 1;
3110
}
3111

    
3112
hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
3113
{
3114
    ARMCPU *cpu = ARM_CPU(cs);
3115
    hwaddr phys_addr;
3116
    target_ulong page_size;
3117
    int prot;
3118
    int ret;
3119

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

    
3122
    if (ret != 0) {
3123
        return -1;
3124
    }
3125

    
3126
    return phys_addr;
3127
}
3128

    
3129
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
3130
{
3131
    if ((env->uncached_cpsr & CPSR_M) == mode) {
3132
        env->regs[13] = val;
3133
    } else {
3134
        env->banked_r13[bank_number(mode)] = val;
3135
    }
3136
}
3137

    
3138
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
3139
{
3140
    if ((env->uncached_cpsr & CPSR_M) == mode) {
3141
        return env->regs[13];
3142
    } else {
3143
        return env->banked_r13[bank_number(mode)];
3144
    }
3145
}
3146

    
3147
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3148
{
3149
    switch (reg) {
3150
    case 0: /* APSR */
3151
        return xpsr_read(env) & 0xf8000000;
3152
    case 1: /* IAPSR */
3153
        return xpsr_read(env) & 0xf80001ff;
3154
    case 2: /* EAPSR */
3155
        return xpsr_read(env) & 0xff00fc00;
3156
    case 3: /* xPSR */
3157
        return xpsr_read(env) & 0xff00fdff;
3158
    case 5: /* IPSR */
3159
        return xpsr_read(env) & 0x000001ff;
3160
    case 6: /* EPSR */
3161
        return xpsr_read(env) & 0x0700fc00;
3162
    case 7: /* IEPSR */
3163
        return xpsr_read(env) & 0x0700edff;
3164
    case 8: /* MSP */
3165
        return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
3166
    case 9: /* PSP */
3167
        return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3168
    case 16: /* PRIMASK */
3169
        return (env->uncached_cpsr & CPSR_I) != 0;
3170
    case 17: /* BASEPRI */
3171
    case 18: /* BASEPRI_MAX */
3172
        return env->v7m.basepri;
3173
    case 19: /* FAULTMASK */
3174
        return (env->uncached_cpsr & CPSR_F) != 0;
3175
    case 20: /* CONTROL */
3176
        return env->v7m.control;
3177
    default:
3178
        /* ??? For debugging only.  */
3179
        cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
3180
        return 0;
3181
    }
3182
}
3183

    
3184
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3185
{
3186
    switch (reg) {
3187
    case 0: /* APSR */
3188
        xpsr_write(env, val, 0xf8000000);
3189
        break;
3190
    case 1: /* IAPSR */
3191
        xpsr_write(env, val, 0xf8000000);
3192
        break;
3193
    case 2: /* EAPSR */
3194
        xpsr_write(env, val, 0xfe00fc00);
3195
        break;
3196
    case 3: /* xPSR */
3197
        xpsr_write(env, val, 0xfe00fc00);
3198
        break;
3199
    case 5: /* IPSR */
3200
        /* IPSR bits are readonly.  */
3201
        break;
3202
    case 6: /* EPSR */
3203
        xpsr_write(env, val, 0x0600fc00);
3204
        break;
3205
    case 7: /* IEPSR */
3206
        xpsr_write(env, val, 0x0600fc00);
3207
        break;
3208
    case 8: /* MSP */
3209
        if (env->v7m.current_sp)
3210
            env->v7m.other_sp = val;
3211
        else
3212
            env->regs[13] = val;
3213
        break;
3214
    case 9: /* PSP */
3215
        if (env->v7m.current_sp)
3216
            env->regs[13] = val;
3217
        else
3218
            env->v7m.other_sp = val;
3219
        break;
3220
    case 16: /* PRIMASK */
3221
        if (val & 1)
3222
            env->uncached_cpsr |= CPSR_I;
3223
        else
3224
            env->uncached_cpsr &= ~CPSR_I;
3225
        break;
3226
    case 17: /* BASEPRI */
3227
        env->v7m.basepri = val & 0xff;
3228
        break;
3229
    case 18: /* BASEPRI_MAX */
3230
        val &= 0xff;
3231
        if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
3232
            env->v7m.basepri = val;
3233
        break;
3234
    case 19: /* FAULTMASK */
3235
        if (val & 1)
3236
            env->uncached_cpsr |= CPSR_F;
3237
        else
3238
            env->uncached_cpsr &= ~CPSR_F;
3239
        break;
3240
    case 20: /* CONTROL */
3241
        env->v7m.control = val & 3;
3242
        switch_v7m_sp(env, (val & 2) != 0);
3243
        break;
3244
    default:
3245
        /* ??? For debugging only.  */
3246
        cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
3247
        return;
3248
    }
3249
}
3250

    
3251
#endif
3252

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

    
3257
/* Signed saturating arithmetic.  */
3258

    
3259
/* Perform 16-bit signed saturating addition.  */
3260
static inline uint16_t add16_sat(uint16_t a, uint16_t b)
3261
{
3262
    uint16_t res;
3263

    
3264
    res = a + b;
3265
    if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
3266
        if (a & 0x8000)
3267
            res = 0x8000;
3268
        else
3269
            res = 0x7fff;
3270
    }
3271
    return res;
3272
}
3273

    
3274
/* Perform 8-bit signed saturating addition.  */
3275
static inline uint8_t add8_sat(uint8_t a, uint8_t b)
3276
{
3277
    uint8_t res;
3278

    
3279
    res = a + b;
3280
    if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
3281
        if (a & 0x80)
3282
            res = 0x80;
3283
        else
3284
            res = 0x7f;
3285
    }
3286
    return res;
3287
}
3288

    
3289
/* Perform 16-bit signed saturating subtraction.  */
3290
static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
3291
{
3292
    uint16_t res;
3293

    
3294
    res = a - b;
3295
    if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
3296
        if (a & 0x8000)
3297
            res = 0x8000;
3298
        else
3299
            res = 0x7fff;
3300
    }
3301
    return res;
3302
}
3303

    
3304
/* Perform 8-bit signed saturating subtraction.  */
3305
static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
3306
{
3307
    uint8_t res;
3308

    
3309
    res = a - b;
3310
    if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
3311
        if (a & 0x80)
3312
            res = 0x80;
3313
        else
3314
            res = 0x7f;
3315
    }
3316
    return res;
3317
}
3318

    
3319
#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
3320
#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
3321
#define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
3322
#define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
3323
#define PFX q
3324

    
3325
#include "op_addsub.h"
3326

    
3327
/* Unsigned saturating arithmetic.  */
3328
static inline uint16_t add16_usat(uint16_t a, uint16_t b)
3329
{
3330
    uint16_t res;
3331
    res = a + b;
3332
    if (res < a)
3333
        res = 0xffff;
3334
    return res;
3335
}
3336

    
3337
static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
3338
{
3339
    if (a > b)
3340
        return a - b;
3341
    else
3342
        return 0;
3343
}
3344

    
3345
static inline uint8_t add8_usat(uint8_t a, uint8_t b)
3346
{
3347
    uint8_t res;
3348
    res = a + b;
3349
    if (res < a)
3350
        res = 0xff;
3351
    return res;
3352
}
3353

    
3354
static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
3355
{
3356
    if (a > b)
3357
        return a - b;
3358
    else
3359
        return 0;
3360
}
3361

    
3362
#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
3363
#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
3364
#define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
3365
#define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
3366
#define PFX uq
3367

    
3368
#include "op_addsub.h"
3369

    
3370
/* Signed modulo arithmetic.  */
3371
#define SARITH16(a, b, n, op) do { \
3372
    int32_t sum; \
3373
    sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
3374
    RESULT(sum, n, 16); \
3375
    if (sum >= 0) \
3376
        ge |= 3 << (n * 2); \
3377
    } while(0)
3378

    
3379
#define SARITH8(a, b, n, op) do { \
3380
    int32_t sum; \
3381
    sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
3382
    RESULT(sum, n, 8); \
3383
    if (sum >= 0) \
3384
        ge |= 1 << n; \
3385
    } while(0)
3386

    
3387

    
3388
#define ADD16(a, b, n) SARITH16(a, b, n, +)
3389
#define SUB16(a, b, n) SARITH16(a, b, n, -)
3390
#define ADD8(a, b, n)  SARITH8(a, b, n, +)
3391
#define SUB8(a, b, n)  SARITH8(a, b, n, -)
3392
#define PFX s
3393
#define ARITH_GE
3394

    
3395
#include "op_addsub.h"
3396

    
3397
/* Unsigned modulo arithmetic.  */
3398
#define ADD16(a, b, n) do { \
3399
    uint32_t sum; \
3400
    sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
3401
    RESULT(sum, n, 16); \
3402
    if ((sum >> 16) == 1) \
3403
        ge |= 3 << (n * 2); \
3404
    } while(0)
3405

    
3406
#define ADD8(a, b, n) do { \
3407
    uint32_t sum; \
3408
    sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
3409
    RESULT(sum, n, 8); \
3410
    if ((sum >> 8) == 1) \
3411
        ge |= 1 << n; \
3412
    } while(0)
3413

    
3414
#define SUB16(a, b, n) do { \
3415
    uint32_t sum; \
3416
    sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
3417
    RESULT(sum, n, 16); \
3418
    if ((sum >> 16) == 0) \
3419
        ge |= 3 << (n * 2); \
3420
    } while(0)
3421

    
3422
#define SUB8(a, b, n) do { \
3423
    uint32_t sum; \
3424
    sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
3425
    RESULT(sum, n, 8); \
3426
    if ((sum >> 8) == 0) \
3427
        ge |= 1 << n; \
3428
    } while(0)
3429

    
3430
#define PFX u
3431
#define ARITH_GE
3432

    
3433
#include "op_addsub.h"
3434

    
3435
/* Halved signed arithmetic.  */
3436
#define ADD16(a, b, n) \
3437
  RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
3438
#define SUB16(a, b, n) \
3439
  RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
3440
#define ADD8(a, b, n) \
3441
  RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
3442
#define SUB8(a, b, n) \
3443
  RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
3444
#define PFX sh
3445

    
3446
#include "op_addsub.h"
3447

    
3448
/* Halved unsigned arithmetic.  */
3449
#define ADD16(a, b, n) \
3450
  RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3451
#define SUB16(a, b, n) \
3452
  RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
3453
#define ADD8(a, b, n) \
3454
  RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3455
#define SUB8(a, b, n) \
3456
  RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
3457
#define PFX uh
3458

    
3459
#include "op_addsub.h"
3460

    
3461
static inline uint8_t do_usad(uint8_t a, uint8_t b)
3462
{
3463
    if (a > b)
3464
        return a - b;
3465
    else
3466
        return b - a;
3467
}
3468

    
3469
/* Unsigned sum of absolute byte differences.  */
3470
uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
3471
{
3472
    uint32_t sum;
3473
    sum = do_usad(a, b);
3474
    sum += do_usad(a >> 8, b >> 8);
3475
    sum += do_usad(a >> 16, b >>16);
3476
    sum += do_usad(a >> 24, b >> 24);
3477
    return sum;
3478
}
3479

    
3480
/* For ARMv6 SEL instruction.  */
3481
uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
3482
{
3483
    uint32_t mask;
3484

    
3485
    mask = 0;
3486
    if (flags & 1)
3487
        mask |= 0xff;
3488
    if (flags & 2)
3489
        mask |= 0xff00;
3490
    if (flags & 4)
3491
        mask |= 0xff0000;
3492
    if (flags & 8)
3493
        mask |= 0xff000000;
3494
    return (a & mask) | (b & ~mask);
3495
}
3496

    
3497
/* VFP support.  We follow the convention used for VFP instructions:
3498
   Single precision routines have a "s" suffix, double precision a
3499
   "d" suffix.  */
3500

    
3501
/* Convert host exception flags to vfp form.  */
3502
static inline int vfp_exceptbits_from_host(int host_bits)
3503
{
3504
    int target_bits = 0;
3505

    
3506
    if (host_bits & float_flag_invalid)
3507
        target_bits |= 1;
3508
    if (host_bits & float_flag_divbyzero)
3509
        target_bits |= 2;
3510
    if (host_bits & float_flag_overflow)
3511
        target_bits |= 4;
3512
    if (host_bits & (float_flag_underflow | float_flag_output_denormal))
3513
        target_bits |= 8;
3514
    if (host_bits & float_flag_inexact)
3515
        target_bits |= 0x10;
3516
    if (host_bits & float_flag_input_denormal)
3517
        target_bits |= 0x80;
3518
    return target_bits;
3519
}
3520

    
3521
uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
3522
{
3523
    int i;
3524
    uint32_t fpscr;
3525

    
3526
    fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
3527
            | (env->vfp.vec_len << 16)
3528
            | (env->vfp.vec_stride << 20);
3529
    i = get_float_exception_flags(&env->vfp.fp_status);
3530
    i |= get_float_exception_flags(&env->vfp.standard_fp_status);
3531
    fpscr |= vfp_exceptbits_from_host(i);
3532
    return fpscr;
3533
}
3534

    
3535
uint32_t vfp_get_fpscr(CPUARMState *env)
3536
{
3537
    return HELPER(vfp_get_fpscr)(env);
3538
}
3539

    
3540
/* Convert vfp exception flags to target form.  */
3541
static inline int vfp_exceptbits_to_host(int target_bits)
3542
{
3543
    int host_bits = 0;
3544

    
3545
    if (target_bits & 1)
3546
        host_bits |= float_flag_invalid;
3547
    if (target_bits & 2)
3548
        host_bits |= float_flag_divbyzero;
3549
    if (target_bits & 4)
3550
        host_bits |= float_flag_overflow;
3551
    if (target_bits & 8)
3552
        host_bits |= float_flag_underflow;
3553
    if (target_bits & 0x10)
3554
        host_bits |= float_flag_inexact;
3555
    if (target_bits & 0x80)
3556
        host_bits |= float_flag_input_denormal;
3557
    return host_bits;
3558
}
3559

    
3560
void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
3561
{
3562
    int i;
3563
    uint32_t changed;
3564

    
3565
    changed = env->vfp.xregs[ARM_VFP_FPSCR];
3566
    env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
3567
    env->vfp.vec_len = (val >> 16) & 7;
3568
    env->vfp.vec_stride = (val >> 20) & 3;
3569

    
3570
    changed ^= val;
3571
    if (changed & (3 << 22)) {
3572
        i = (val >> 22) & 3;
3573
        switch (i) {
3574
        case 0:
3575
            i = float_round_nearest_even;
3576
            break;
3577
        case 1:
3578
            i = float_round_up;
3579
            break;
3580
        case 2:
3581
            i = float_round_down;
3582
            break;
3583
        case 3:
3584
            i = float_round_to_zero;
3585
            break;
3586
        }
3587
        set_float_rounding_mode(i, &env->vfp.fp_status);
3588
    }
3589
    if (changed & (1 << 24)) {
3590
        set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3591
        set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
3592
    }
3593
    if (changed & (1 << 25))
3594
        set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
3595

    
3596
    i = vfp_exceptbits_to_host(val);
3597
    set_float_exception_flags(i, &env->vfp.fp_status);
3598
    set_float_exception_flags(0, &env->vfp.standard_fp_status);
3599
}
3600

    
3601
void vfp_set_fpscr(CPUARMState *env, uint32_t val)
3602
{
3603
    HELPER(vfp_set_fpscr)(env, val);
3604
}
3605

    
3606
#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
3607

    
3608
#define VFP_BINOP(name) \
3609
float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
3610
{ \
3611
    float_status *fpst = fpstp; \
3612
    return float32_ ## name(a, b, fpst); \
3613
} \
3614
float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
3615
{ \
3616
    float_status *fpst = fpstp; \
3617
    return float64_ ## name(a, b, fpst); \
3618
}
3619
VFP_BINOP(add)
3620
VFP_BINOP(sub)
3621
VFP_BINOP(mul)
3622
VFP_BINOP(div)
3623
#undef VFP_BINOP
3624

    
3625
float32 VFP_HELPER(neg, s)(float32 a)
3626
{
3627
    return float32_chs(a);
3628
}
3629

    
3630
float64 VFP_HELPER(neg, d)(float64 a)
3631
{
3632
    return float64_chs(a);
3633
}
3634

    
3635
float32 VFP_HELPER(abs, s)(float32 a)
3636
{
3637
    return float32_abs(a);
3638
}
3639

    
3640
float64 VFP_HELPER(abs, d)(float64 a)
3641
{
3642
    return float64_abs(a);
3643
}
3644

    
3645
float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
3646
{
3647
    return float32_sqrt(a, &env->vfp.fp_status);
3648
}
3649

    
3650
float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
3651
{
3652
    return float64_sqrt(a, &env->vfp.fp_status);
3653
}
3654

    
3655
/* XXX: check quiet/signaling case */
3656
#define DO_VFP_cmp(p, type) \
3657
void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env)  \
3658
{ \
3659
    uint32_t flags; \
3660
    switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
3661
    case 0: flags = 0x6; break; \
3662
    case -1: flags = 0x8; break; \
3663
    case 1: flags = 0x2; break; \
3664
    default: case 2: flags = 0x3; break; \
3665
    } \
3666
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3667
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3668
} \
3669
void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
3670
{ \
3671
    uint32_t flags; \
3672
    switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
3673
    case 0: flags = 0x6; break; \
3674
    case -1: flags = 0x8; break; \
3675
    case 1: flags = 0x2; break; \
3676
    default: case 2: flags = 0x3; break; \
3677
    } \
3678
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
3679
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
3680
}
3681
DO_VFP_cmp(s, float32)
3682
DO_VFP_cmp(d, float64)
3683
#undef DO_VFP_cmp
3684

    
3685
/* Integer to float and float to integer conversions */
3686

    
3687
#define CONV_ITOF(name, fsz, sign) \
3688
    float##fsz HELPER(name)(uint32_t x, void *fpstp) \
3689
{ \
3690
    float_status *fpst = fpstp; \
3691
    return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
3692
}
3693

    
3694
#define CONV_FTOI(name, fsz, sign, round) \
3695
uint32_t HELPER(name)(float##fsz x, void *fpstp) \
3696
{ \
3697
    float_status *fpst = fpstp; \
3698
    if (float##fsz##_is_any_nan(x)) { \
3699
        float_raise(float_flag_invalid, fpst); \
3700
        return 0; \
3701
    } \
3702
    return float##fsz##_to_##sign##int32##round(x, fpst); \
3703
}
3704

    
3705
#define FLOAT_CONVS(name, p, fsz, sign) \
3706
CONV_ITOF(vfp_##name##to##p, fsz, sign) \
3707
CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
3708
CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
3709

    
3710
FLOAT_CONVS(si, s, 32, )
3711
FLOAT_CONVS(si, d, 64, )
3712
FLOAT_CONVS(ui, s, 32, u)
3713
FLOAT_CONVS(ui, d, 64, u)
3714

    
3715
#undef CONV_ITOF
3716
#undef CONV_FTOI
3717
#undef FLOAT_CONVS
3718

    
3719
/* floating point conversion */
3720
float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
3721
{
3722
    float64 r = float32_to_float64(x, &env->vfp.fp_status);
3723
    /* ARM requires that S<->D conversion of any kind of NaN generates
3724
     * a quiet NaN by forcing the most significant frac bit to 1.
3725
     */
3726
    return float64_maybe_silence_nan(r);
3727
}
3728

    
3729
float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
3730
{
3731
    float32 r =  float64_to_float32(x, &env->vfp.fp_status);
3732
    /* ARM requires that S<->D conversion of any kind of NaN generates
3733
     * a quiet NaN by forcing the most significant frac bit to 1.
3734
     */
3735
    return float32_maybe_silence_nan(r);
3736
}
3737

    
3738
/* VFP3 fixed point conversion.  */
3739
#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
3740
float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t  x, uint32_t shift, \
3741
                                    void *fpstp) \
3742
{ \
3743
    float_status *fpst = fpstp; \
3744
    float##fsz tmp; \
3745
    tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
3746
    return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
3747
} \
3748
uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
3749
                                       void *fpstp) \
3750
{ \
3751
    float_status *fpst = fpstp; \
3752
    float##fsz tmp; \
3753
    if (float##fsz##_is_any_nan(x)) { \
3754
        float_raise(float_flag_invalid, fpst); \
3755
        return 0; \
3756
    } \
3757
    tmp = float##fsz##_scalbn(x, shift, fpst); \
3758
    return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
3759
}
3760

    
3761
VFP_CONV_FIX(sh, d, 64, int16, )
3762
VFP_CONV_FIX(sl, d, 64, int32, )
3763
VFP_CONV_FIX(uh, d, 64, uint16, u)
3764
VFP_CONV_FIX(ul, d, 64, uint32, u)
3765
VFP_CONV_FIX(sh, s, 32, int16, )
3766
VFP_CONV_FIX(sl, s, 32, int32, )
3767
VFP_CONV_FIX(uh, s, 32, uint16, u)
3768
VFP_CONV_FIX(ul, s, 32, uint32, u)
3769
#undef VFP_CONV_FIX
3770

    
3771
/* Half precision conversions.  */
3772
static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
3773
{
3774
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3775
    float32 r = float16_to_float32(make_float16(a), ieee, s);
3776
    if (ieee) {
3777
        return float32_maybe_silence_nan(r);
3778
    }
3779
    return r;
3780
}
3781

    
3782
static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
3783
{
3784
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
3785
    float16 r = float32_to_float16(a, ieee, s);
3786
    if (ieee) {
3787
        r = float16_maybe_silence_nan(r);
3788
    }
3789
    return float16_val(r);
3790
}
3791

    
3792
float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3793
{
3794
    return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
3795
}
3796

    
3797
uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3798
{
3799
    return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
3800
}
3801

    
3802
float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
3803
{
3804
    return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
3805
}
3806

    
3807
uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
3808
{
3809
    return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
3810
}
3811

    
3812
#define float32_two make_float32(0x40000000)
3813
#define float32_three make_float32(0x40400000)
3814
#define float32_one_point_five make_float32(0x3fc00000)
3815

    
3816
float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
3817
{
3818
    float_status *s = &env->vfp.standard_fp_status;
3819
    if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3820
        (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3821
        if (!(float32_is_zero(a) || float32_is_zero(b))) {
3822
            float_raise(float_flag_input_denormal, s);
3823
        }
3824
        return float32_two;
3825
    }
3826
    return float32_sub(float32_two, float32_mul(a, b, s), s);
3827
}
3828

    
3829
float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
3830
{
3831
    float_status *s = &env->vfp.standard_fp_status;
3832
    float32 product;
3833
    if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
3834
        (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
3835
        if (!(float32_is_zero(a) || float32_is_zero(b))) {
3836
            float_raise(float_flag_input_denormal, s);
3837
        }
3838
        return float32_one_point_five;
3839
    }
3840
    product = float32_mul(a, b, s);
3841
    return float32_div(float32_sub(float32_three, product, s), float32_two, s);
3842
}
3843

    
3844
/* NEON helpers.  */
3845

    
3846
/* Constants 256 and 512 are used in some helpers; we avoid relying on
3847
 * int->float conversions at run-time.  */
3848
#define float64_256 make_float64(0x4070000000000000LL)
3849
#define float64_512 make_float64(0x4080000000000000LL)
3850

    
3851
/* The algorithm that must be used to calculate the estimate
3852
 * is specified by the ARM ARM.
3853
 */
3854
static float64 recip_estimate(float64 a, CPUARMState *env)
3855
{
3856
    /* These calculations mustn't set any fp exception flags,
3857
     * so we use a local copy of the fp_status.
3858
     */
3859
    float_status dummy_status = env->vfp.standard_fp_status;
3860
    float_status *s = &dummy_status;
3861
    /* q = (int)(a * 512.0) */
3862
    float64 q = float64_mul(float64_512, a, s);
3863
    int64_t q_int = float64_to_int64_round_to_zero(q, s);
3864

    
3865
    /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3866
    q = int64_to_float64(q_int, s);
3867
    q = float64_add(q, float64_half, s);
3868
    q = float64_div(q, float64_512, s);
3869
    q = float64_div(float64_one, q, s);
3870

    
3871
    /* s = (int)(256.0 * r + 0.5) */
3872
    q = float64_mul(q, float64_256, s);
3873
    q = float64_add(q, float64_half, s);
3874
    q_int = float64_to_int64_round_to_zero(q, s);
3875

    
3876
    /* return (double)s / 256.0 */
3877
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
3878
}
3879

    
3880
float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
3881
{
3882
    float_status *s = &env->vfp.standard_fp_status;
3883
    float64 f64;
3884
    uint32_t val32 = float32_val(a);
3885

    
3886
    int result_exp;
3887
    int a_exp = (val32  & 0x7f800000) >> 23;
3888
    int sign = val32 & 0x80000000;
3889

    
3890
    if (float32_is_any_nan(a)) {
3891
        if (float32_is_signaling_nan(a)) {
3892
            float_raise(float_flag_invalid, s);
3893
        }
3894
        return float32_default_nan;
3895
    } else if (float32_is_infinity(a)) {
3896
        return float32_set_sign(float32_zero, float32_is_neg(a));
3897
    } else if (float32_is_zero_or_denormal(a)) {
3898
        if (!float32_is_zero(a)) {
3899
            float_raise(float_flag_input_denormal, s);
3900
        }
3901
        float_raise(float_flag_divbyzero, s);
3902
        return float32_set_sign(float32_infinity, float32_is_neg(a));
3903
    } else if (a_exp >= 253) {
3904
        float_raise(float_flag_underflow, s);
3905
        return float32_set_sign(float32_zero, float32_is_neg(a));
3906
    }
3907

    
3908
    f64 = make_float64((0x3feULL << 52)
3909
                       | ((int64_t)(val32 & 0x7fffff) << 29));
3910

    
3911
    result_exp = 253 - a_exp;
3912

    
3913
    f64 = recip_estimate(f64, env);
3914

    
3915
    val32 = sign
3916
        | ((result_exp & 0xff) << 23)
3917
        | ((float64_val(f64) >> 29) & 0x7fffff);
3918
    return make_float32(val32);
3919
}
3920

    
3921
/* The algorithm that must be used to calculate the estimate
3922
 * is specified by the ARM ARM.
3923
 */
3924
static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3925
{
3926
    /* These calculations mustn't set any fp exception flags,
3927
     * so we use a local copy of the fp_status.
3928
     */
3929
    float_status dummy_status = env->vfp.standard_fp_status;
3930
    float_status *s = &dummy_status;
3931
    float64 q;
3932
    int64_t q_int;
3933

    
3934
    if (float64_lt(a, float64_half, s)) {
3935
        /* range 0.25 <= a < 0.5 */
3936

    
3937
        /* a in units of 1/512 rounded down */
3938
        /* q0 = (int)(a * 512.0);  */
3939
        q = float64_mul(float64_512, a, s);
3940
        q_int = float64_to_int64_round_to_zero(q, s);
3941

    
3942
        /* reciprocal root r */
3943
        /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0);  */
3944
        q = int64_to_float64(q_int, s);
3945
        q = float64_add(q, float64_half, s);
3946
        q = float64_div(q, float64_512, s);
3947
        q = float64_sqrt(q, s);
3948
        q = float64_div(float64_one, q, s);
3949
    } else {
3950
        /* range 0.5 <= a < 1.0 */
3951

    
3952
        /* a in units of 1/256 rounded down */
3953
        /* q1 = (int)(a * 256.0); */
3954
        q = float64_mul(float64_256, a, s);
3955
        int64_t q_int = float64_to_int64_round_to_zero(q, s);
3956

    
3957
        /* reciprocal root r */
3958
        /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3959
        q = int64_to_float64(q_int, s);
3960
        q = float64_add(q, float64_half, s);
3961
        q = float64_div(q, float64_256, s);
3962
        q = float64_sqrt(q, s);
3963
        q = float64_div(float64_one, q, s);
3964
    }
3965
    /* r in units of 1/256 rounded to nearest */
3966
    /* s = (int)(256.0 * r + 0.5); */
3967

    
3968
    q = float64_mul(q, float64_256,s );
3969
    q = float64_add(q, float64_half, s);
3970
    q_int = float64_to_int64_round_to_zero(q, s);
3971

    
3972
    /* return (double)s / 256.0;*/
3973
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
3974
}
3975

    
3976
float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3977
{
3978
    float_status *s = &env->vfp.standard_fp_status;
3979
    int result_exp;
3980
    float64 f64;
3981
    uint32_t val;
3982
    uint64_t val64;
3983

    
3984
    val = float32_val(a);
3985

    
3986
    if (float32_is_any_nan(a)) {
3987
        if (float32_is_signaling_nan(a)) {
3988
            float_raise(float_flag_invalid, s);
3989
        }
3990
        return float32_default_nan;
3991
    } else if (float32_is_zero_or_denormal(a)) {
3992
        if (!float32_is_zero(a)) {
3993
            float_raise(float_flag_input_denormal, s);
3994
        }
3995
        float_raise(float_flag_divbyzero, s);
3996
        return float32_set_sign(float32_infinity, float32_is_neg(a));
3997
    } else if (float32_is_neg(a)) {
3998
        float_raise(float_flag_invalid, s);
3999
        return float32_default_nan;
4000
    } else if (float32_is_infinity(a)) {
4001
        return float32_zero;
4002
    }
4003

    
4004
    /* Normalize to a double-precision value between 0.25 and 1.0,
4005
     * preserving the parity of the exponent.  */
4006
    if ((val & 0x800000) == 0) {
4007
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
4008
                           | (0x3feULL << 52)
4009
                           | ((uint64_t)(val & 0x7fffff) << 29));
4010
    } else {
4011
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
4012
                           | (0x3fdULL << 52)
4013
                           | ((uint64_t)(val & 0x7fffff) << 29));
4014
    }
4015

    
4016
    result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
4017

    
4018
    f64 = recip_sqrt_estimate(f64, env);
4019

    
4020
    val64 = float64_val(f64);
4021

    
4022
    val = ((result_exp & 0xff) << 23)
4023
        | ((val64 >> 29)  & 0x7fffff);
4024
    return make_float32(val);
4025
}
4026

    
4027
uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
4028
{
4029
    float64 f64;
4030

    
4031
    if ((a & 0x80000000) == 0) {
4032
        return 0xffffffff;
4033
    }
4034

    
4035
    f64 = make_float64((0x3feULL << 52)
4036
                       | ((int64_t)(a & 0x7fffffff) << 21));
4037

    
4038
    f64 = recip_estimate (f64, env);
4039

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

    
4043
uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
4044
{
4045
    float64 f64;
4046

    
4047
    if ((a & 0xc0000000) == 0) {
4048
        return 0xffffffff;
4049
    }
4050

    
4051
    if (a & 0x80000000) {
4052
        f64 = make_float64((0x3feULL << 52)
4053
                           | ((uint64_t)(a & 0x7fffffff) << 21));
4054
    } else { /* bits 31-30 == '01' */
4055
        f64 = make_float64((0x3fdULL << 52)
4056
                           | ((uint64_t)(a & 0x3fffffff) << 22));
4057
    }
4058

    
4059
    f64 = recip_sqrt_estimate(f64, env);
4060

    
4061
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4062
}
4063

    
4064
/* VFPv4 fused multiply-accumulate */
4065
float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
4066
{
4067
    float_status *fpst = fpstp;
4068
    return float32_muladd(a, b, c, 0, fpst);
4069
}
4070

    
4071
float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
4072
{
4073
    float_status *fpst = fpstp;
4074
    return float64_muladd(a, b, c, 0, fpst);
4075
}