~ubuntu-branches/ubuntu/vivid/qemu/vivid

« back to all changes in this revision

Viewing changes to target-arm/helper.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-25 22:31:43 UTC
  • mfrom: (1.8.5)
  • Revision ID: package-import@ubuntu.com-20140225223143-odhqxfc60wxrjl15
Tags: 2.0.0~rc1+dfsg-0ubuntu1
* Merge 2.0.0-rc1
* debian/rules: consolidate ppc filter entries.
* Move qemu-system-arch64 into qemu-system-arm
* debian/patches/define-trusty-machine-type.patch: define a trusty machine
  type, currently the same as pc-i440fx-2.0, to put is in a better position
  to enable live migrations from trusty onward.  (LP: #1294823)
* debian/control: build-dep on libfdt >= 1.4.0  (LP: #1295072)
* Merge latest upstream git to commit dc9528f
* Debian/rules:
  - remove -enable-uname-release=2.6.32
  - don't make the aarch64 target Ubuntu-specific.
* Remove patches which are now upstream:
  - fix-smb-security-share.patch
  - slirp-smb-redirect-port-445-too.patch 
  - linux-user-Implement-sendmmsg-syscall.patch (better version is upstream)
  - signal-added-a-wrapper-for-sigprocmask-function.patch
  - ubuntu/signal-sigsegv-protection-on-do_sigprocmask.patch
  - ubuntu/Don-t-block-SIGSEGV-at-more-places.patch
  - ubuntu/ppc-force-cpu-threads-count-to-be-power-of-2.patch
* add link for /usr/share/qemu/bios-256k.bin
* Remove all linaro patches.
* Remove all arm64/ patches.  Many but not all are upstream.
* Remove CVE-2013-4377.patch which is upstream.
* debian/control-in: don't make qemu-system-aarch64 ubuntu-specific

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#include "sysemu/arch_init.h"
6
6
#include "sysemu/sysemu.h"
7
7
#include "qemu/bitops.h"
 
8
#include "qemu/crc32c.h"
 
9
#include <zlib.h> /* For crc32 */
8
10
 
9
11
#ifndef CONFIG_USER_ONLY
10
12
static inline int get_phys_addr(CPUARMState *env, uint32_t address,
11
13
                                int access_type, int is_user,
12
14
                                hwaddr *phys_ptr, int *prot,
13
15
                                target_ulong *page_size);
 
16
 
 
17
/* Definitions for the PMCCNTR and PMCR registers */
 
18
#define PMCRD   0x8
 
19
#define PMCRC   0x4
 
20
#define PMCRE   0x1
14
21
#endif
15
22
 
16
23
static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
107
114
    }
108
115
}
109
116
 
110
 
static int raw_read(CPUARMState *env, const ARMCPRegInfo *ri,
111
 
                    uint64_t *value)
 
117
static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
112
118
{
113
 
    if (ri->type & ARM_CP_64BIT) {
114
 
        *value = CPREG_FIELD64(env, ri);
 
119
    if (cpreg_field_is_64bit(ri)) {
 
120
        return CPREG_FIELD64(env, ri);
115
121
    } else {
116
 
        *value = CPREG_FIELD32(env, ri);
 
122
        return CPREG_FIELD32(env, ri);
117
123
    }
118
 
    return 0;
119
124
}
120
125
 
121
 
static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
122
 
                     uint64_t value)
 
126
static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
127
                      uint64_t value)
123
128
{
124
 
    if (ri->type & ARM_CP_64BIT) {
 
129
    if (cpreg_field_is_64bit(ri)) {
125
130
        CPREG_FIELD64(env, ri) = value;
126
131
    } else {
127
132
        CPREG_FIELD32(env, ri) = value;
128
133
    }
129
 
    return 0;
130
134
}
131
135
 
132
 
static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
133
 
                            uint64_t *v)
 
136
static uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
134
137
{
135
 
    /* Raw read of a coprocessor register (as needed for migration, etc)
136
 
     * return true on success, false if the read is impossible for some reason.
137
 
     */
 
138
    /* Raw read of a coprocessor register (as needed for migration, etc). */
138
139
    if (ri->type & ARM_CP_CONST) {
139
 
        *v = ri->resetvalue;
 
140
        return ri->resetvalue;
140
141
    } else if (ri->raw_readfn) {
141
 
        return (ri->raw_readfn(env, ri, v) == 0);
 
142
        return ri->raw_readfn(env, ri);
142
143
    } else if (ri->readfn) {
143
 
        return (ri->readfn(env, ri, v) == 0);
 
144
        return ri->readfn(env, ri);
144
145
    } else {
145
 
        raw_read(env, ri, v);
 
146
        return raw_read(env, ri);
146
147
    }
147
 
    return true;
148
148
}
149
149
 
150
 
static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
151
 
                             int64_t v)
 
150
static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
 
151
                             uint64_t v)
152
152
{
153
153
    /* Raw write of a coprocessor register (as needed for migration, etc).
154
 
     * Return true on success, false if the write is impossible for some reason.
155
154
     * Note that constant registers are treated as write-ignored; the
156
155
     * caller should check for success by whether a readback gives the
157
156
     * value written.
158
157
     */
159
158
    if (ri->type & ARM_CP_CONST) {
160
 
        return true;
 
159
        return;
161
160
    } else if (ri->raw_writefn) {
162
 
        return (ri->raw_writefn(env, ri, v) == 0);
 
161
        ri->raw_writefn(env, ri, v);
163
162
    } else if (ri->writefn) {
164
 
        return (ri->writefn(env, ri, v) == 0);
 
163
        ri->writefn(env, ri, v);
165
164
    } else {
166
165
        raw_write(env, ri, v);
167
166
    }
168
 
    return true;
169
167
}
170
168
 
171
169
bool write_cpustate_to_list(ARMCPU *cpu)
177
175
    for (i = 0; i < cpu->cpreg_array_len; i++) {
178
176
        uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
179
177
        const ARMCPRegInfo *ri;
180
 
        uint64_t v;
 
178
 
181
179
        ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
182
180
        if (!ri) {
183
181
            ok = false;
186
184
        if (ri->type & ARM_CP_NO_MIGRATE) {
187
185
            continue;
188
186
        }
189
 
        if (!read_raw_cp_reg(&cpu->env, ri, &v)) {
190
 
            ok = false;
191
 
            continue;
192
 
        }
193
 
        cpu->cpreg_values[i] = v;
 
187
        cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
194
188
    }
195
189
    return ok;
196
190
}
203
197
    for (i = 0; i < cpu->cpreg_array_len; i++) {
204
198
        uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
205
199
        uint64_t v = cpu->cpreg_values[i];
206
 
        uint64_t readback;
207
200
        const ARMCPRegInfo *ri;
208
201
 
209
202
        ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
218
211
         * (to catch read-only registers and partially read-only
219
212
         * registers where the incoming migration value doesn't match)
220
213
         */
221
 
        if (!write_raw_cp_reg(&cpu->env, ri, v) ||
222
 
            !read_raw_cp_reg(&cpu->env, ri, &readback) ||
223
 
            readback != v) {
 
214
        write_raw_cp_reg(&cpu->env, ri, v);
 
215
        if (read_raw_cp_reg(&cpu->env, ri) != v) {
224
216
            ok = false;
225
217
        }
226
218
    }
309
301
    g_list_free(keys);
310
302
}
311
303
 
312
 
static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
304
static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
313
305
{
 
306
    ARMCPU *cpu = arm_env_get_cpu(env);
 
307
 
314
308
    env->cp15.c3 = value;
315
 
    tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
316
 
    return 0;
 
309
    tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
317
310
}
318
311
 
319
 
static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
312
static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
320
313
{
 
314
    ARMCPU *cpu = arm_env_get_cpu(env);
 
315
 
321
316
    if (env->cp15.c13_fcse != value) {
322
317
        /* Unlike real hardware the qemu TLB uses virtual addresses,
323
318
         * not modified virtual addresses, so this causes a TLB flush.
324
319
         */
325
 
        tlb_flush(env, 1);
 
320
        tlb_flush(CPU(cpu), 1);
326
321
        env->cp15.c13_fcse = value;
327
322
    }
328
 
    return 0;
329
323
}
330
 
static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
331
 
                            uint64_t value)
 
324
 
 
325
static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
326
                             uint64_t value)
332
327
{
 
328
    ARMCPU *cpu = arm_env_get_cpu(env);
 
329
 
333
330
    if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
334
331
        /* For VMSA (when not using the LPAE long descriptor page table
335
332
         * format) this register includes the ASID, so do a TLB flush.
336
333
         * For PMSA it is purely a process ID and no action is needed.
337
334
         */
338
 
        tlb_flush(env, 1);
 
335
        tlb_flush(CPU(cpu), 1);
339
336
    }
340
337
    env->cp15.c13_context = value;
341
 
    return 0;
342
338
}
343
339
 
344
 
static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
345
 
                         uint64_t value)
 
340
static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
341
                          uint64_t value)
346
342
{
347
343
    /* Invalidate all (TLBIALL) */
348
 
    tlb_flush(env, 1);
349
 
    return 0;
 
344
    ARMCPU *cpu = arm_env_get_cpu(env);
 
345
 
 
346
    tlb_flush(CPU(cpu), 1);
350
347
}
351
348
 
352
 
static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
353
 
                         uint64_t value)
 
349
static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
350
                          uint64_t value)
354
351
{
355
352
    /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
356
 
    tlb_flush_page(env, value & TARGET_PAGE_MASK);
357
 
    return 0;
 
353
    ARMCPU *cpu = arm_env_get_cpu(env);
 
354
 
 
355
    tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
358
356
}
359
357
 
360
 
static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
361
 
                          uint64_t value)
 
358
static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
359
                           uint64_t value)
362
360
{
363
361
    /* Invalidate by ASID (TLBIASID) */
364
 
    tlb_flush(env, value == 0);
365
 
    return 0;
 
362
    ARMCPU *cpu = arm_env_get_cpu(env);
 
363
 
 
364
    tlb_flush(CPU(cpu), value == 0);
366
365
}
367
366
 
368
 
static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
369
 
                          uint64_t value)
 
367
static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
368
                           uint64_t value)
370
369
{
371
370
    /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
372
 
    tlb_flush_page(env, value & TARGET_PAGE_MASK);
373
 
    return 0;
 
371
    ARMCPU *cpu = arm_env_get_cpu(env);
 
372
 
 
373
    tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
374
374
}
375
375
 
376
376
static const ARMCPRegInfo cp_reginfo[] = {
389
389
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
390
390
      .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
391
391
    { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
392
 
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
 
392
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_context),
393
393
      .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
394
394
    /* ??? This covers not just the impdef TLB lockdown registers but also
395
395
     * some v7VMSA registers relating to TEX remap, so it is overly broad.
450
450
    REGINFO_SENTINEL
451
451
};
452
452
 
453
 
static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
453
static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
454
                        uint64_t value)
454
455
{
455
456
    if (env->cp15.c1_coproc != value) {
456
457
        env->cp15.c1_coproc = value;
457
458
        /* ??? Is this safe when called from within a TB?  */
458
459
        tb_flush(env);
459
460
    }
460
 
    return 0;
461
461
}
462
462
 
463
463
static const ARMCPRegInfo v6_cp_reginfo[] = {
479
479
     */
480
480
    { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
481
481
      .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
482
 
    { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
 
482
    { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
 
483
      .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
483
484
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
484
485
      .resetvalue = 0, .writefn = cpacr_write },
485
486
    REGINFO_SENTINEL
486
487
};
487
488
 
488
 
 
489
 
static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
490
 
                      uint64_t *value)
 
489
static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
491
490
{
492
 
    /* Generic performance monitor register read function for where
493
 
     * user access may be allowed by PMUSERENR.
 
491
    /* Performance monitor registers user accessibility is controlled
 
492
     * by PMUSERENR.
494
493
     */
495
494
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
496
 
        return EXCP_UDEF;
 
495
        return CP_ACCESS_TRAP;
497
496
    }
498
 
    *value = CPREG_FIELD32(env, ri);
499
 
    return 0;
 
497
    return CP_ACCESS_OK;
500
498
}
501
499
 
502
 
static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
503
 
                      uint64_t value)
 
500
#ifndef CONFIG_USER_ONLY
 
501
static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
502
                       uint64_t value)
504
503
{
505
 
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
506
 
        return EXCP_UDEF;
507
 
    }
 
504
    /* Don't computer the number of ticks in user mode */
 
505
    uint32_t temp_ticks;
 
506
 
 
507
    temp_ticks = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) *
 
508
                  get_ticks_per_sec() / 1000000;
 
509
 
 
510
    if (env->cp15.c9_pmcr & PMCRE) {
 
511
        /* If the counter is enabled */
 
512
        if (env->cp15.c9_pmcr & PMCRD) {
 
513
            /* Increment once every 64 processor clock cycles */
 
514
            env->cp15.c15_ccnt = (temp_ticks/64) - env->cp15.c15_ccnt;
 
515
        } else {
 
516
            env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
 
517
        }
 
518
    }
 
519
 
 
520
    if (value & PMCRC) {
 
521
        /* The counter has been reset */
 
522
        env->cp15.c15_ccnt = 0;
 
523
    }
 
524
 
508
525
    /* only the DP, X, D and E bits are writable */
509
526
    env->cp15.c9_pmcr &= ~0x39;
510
527
    env->cp15.c9_pmcr |= (value & 0x39);
511
 
    return 0;
512
 
}
513
 
 
514
 
static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
528
 
 
529
    if (env->cp15.c9_pmcr & PMCRE) {
 
530
        if (env->cp15.c9_pmcr & PMCRD) {
 
531
            /* Increment once every 64 processor clock cycles */
 
532
            temp_ticks /= 64;
 
533
        }
 
534
        env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
 
535
    }
 
536
}
 
537
 
 
538
static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 
539
{
 
540
    uint32_t total_ticks;
 
541
 
 
542
    if (!(env->cp15.c9_pmcr & PMCRE)) {
 
543
        /* Counter is disabled, do not change value */
 
544
        return env->cp15.c15_ccnt;
 
545
    }
 
546
 
 
547
    total_ticks = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) *
 
548
                  get_ticks_per_sec() / 1000000;
 
549
 
 
550
    if (env->cp15.c9_pmcr & PMCRD) {
 
551
        /* Increment once every 64 processor clock cycles */
 
552
        total_ticks /= 64;
 
553
    }
 
554
    return total_ticks - env->cp15.c15_ccnt;
 
555
}
 
556
 
 
557
static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
558
                        uint64_t value)
 
559
{
 
560
    uint32_t total_ticks;
 
561
 
 
562
    if (!(env->cp15.c9_pmcr & PMCRE)) {
 
563
        /* Counter is disabled, set the absolute value */
 
564
        env->cp15.c15_ccnt = value;
 
565
        return;
 
566
    }
 
567
 
 
568
    total_ticks = qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) *
 
569
                  get_ticks_per_sec() / 1000000;
 
570
 
 
571
    if (env->cp15.c9_pmcr & PMCRD) {
 
572
        /* Increment once every 64 processor clock cycles */
 
573
        total_ticks /= 64;
 
574
    }
 
575
    env->cp15.c15_ccnt = total_ticks - value;
 
576
}
 
577
#endif
 
578
 
 
579
static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
515
580
                            uint64_t value)
516
581
{
517
 
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
518
 
        return EXCP_UDEF;
519
 
    }
520
582
    value &= (1 << 31);
521
583
    env->cp15.c9_pmcnten |= value;
522
 
    return 0;
523
584
}
524
585
 
525
 
static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
526
 
                            uint64_t value)
 
586
static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
587
                             uint64_t value)
527
588
{
528
 
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
529
 
        return EXCP_UDEF;
530
 
    }
531
589
    value &= (1 << 31);
532
590
    env->cp15.c9_pmcnten &= ~value;
533
 
    return 0;
534
591
}
535
592
 
536
 
static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
537
 
                        uint64_t value)
 
593
static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
594
                         uint64_t value)
538
595
{
539
 
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
540
 
        return EXCP_UDEF;
541
 
    }
542
596
    env->cp15.c9_pmovsr &= ~value;
543
 
    return 0;
544
597
}
545
598
 
546
 
static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
547
 
                            uint64_t value)
 
599
static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
600
                             uint64_t value)
548
601
{
549
 
    if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
550
 
        return EXCP_UDEF;
551
 
    }
552
602
    env->cp15.c9_pmxevtyper = value & 0xff;
553
 
    return 0;
554
603
}
555
604
 
556
 
static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
605
static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
557
606
                            uint64_t value)
558
607
{
559
608
    env->cp15.c9_pmuserenr = value & 1;
560
 
    return 0;
561
609
}
562
610
 
563
 
static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
564
 
                            uint64_t value)
 
611
static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
612
                             uint64_t value)
565
613
{
566
614
    /* We have no event counters so only the C bit can be changed */
567
615
    value &= (1 << 31);
568
616
    env->cp15.c9_pminten |= value;
569
 
    return 0;
570
617
}
571
618
 
572
 
static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
573
 
                            uint64_t value)
 
619
static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
620
                             uint64_t value)
574
621
{
575
622
    value &= (1 << 31);
576
623
    env->cp15.c9_pminten &= ~value;
577
 
    return 0;
578
 
}
579
 
 
580
 
static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
581
 
                       uint64_t *value)
 
624
}
 
625
 
 
626
static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
627
                       uint64_t value)
 
628
{
 
629
    /* Note that even though the AArch64 view of this register has bits
 
630
     * [10:0] all RES0 we can only mask the bottom 5, to comply with the
 
631
     * architectural requirements for bits which are RES0 only in some
 
632
     * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
 
633
     * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
 
634
     */
 
635
    env->cp15.c12_vbar = value & ~0x1Ful;
 
636
}
 
637
 
 
638
static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
582
639
{
583
640
    ARMCPU *cpu = arm_env_get_cpu(env);
584
 
    *value = cpu->ccsidr[env->cp15.c0_cssel];
585
 
    return 0;
 
641
    return cpu->ccsidr[env->cp15.c0_cssel];
586
642
}
587
643
 
588
 
static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
589
 
                        uint64_t value)
 
644
static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
645
                         uint64_t value)
590
646
{
591
647
    env->cp15.c0_cssel = value & 0xf;
592
 
    return 0;
593
648
}
594
649
 
595
650
static const ARMCPRegInfo v7_cp_reginfo[] = {
617
672
    { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
618
673
      .access = PL0_RW, .resetvalue = 0,
619
674
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
620
 
      .readfn = pmreg_read, .writefn = pmcntenset_write,
621
 
      .raw_readfn = raw_read, .raw_writefn = raw_write },
 
675
      .writefn = pmcntenset_write,
 
676
      .accessfn = pmreg_access,
 
677
      .raw_writefn = raw_write },
622
678
    { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
623
679
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
624
 
      .readfn = pmreg_read, .writefn = pmcntenclr_write,
 
680
      .accessfn = pmreg_access,
 
681
      .writefn = pmcntenclr_write,
625
682
      .type = ARM_CP_NO_MIGRATE },
626
683
    { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
627
684
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
628
 
      .readfn = pmreg_read, .writefn = pmovsr_write,
629
 
      .raw_readfn = raw_read, .raw_writefn = raw_write },
630
 
    /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
631
 
     * respect PMUSERENR.
632
 
     */
 
685
      .accessfn = pmreg_access,
 
686
      .writefn = pmovsr_write,
 
687
      .raw_writefn = raw_write },
 
688
    /* Unimplemented so WI. */
633
689
    { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
634
 
      .access = PL0_W, .type = ARM_CP_NOP },
 
690
      .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
635
691
    /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
636
 
     * We choose to RAZ/WI. XXX should respect PMUSERENR.
 
692
     * We choose to RAZ/WI.
637
693
     */
638
694
    { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
639
 
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
640
 
    /* Unimplemented, RAZ/WI. XXX PMUSERENR */
 
695
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
 
696
      .accessfn = pmreg_access },
 
697
#ifndef CONFIG_USER_ONLY
641
698
    { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
642
 
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
 
699
      .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
 
700
      .readfn = pmccntr_read, .writefn = pmccntr_write,
 
701
      .accessfn = pmreg_access },
 
702
#endif
643
703
    { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
644
704
      .access = PL0_RW,
645
705
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
646
 
      .readfn = pmreg_read, .writefn = pmxevtyper_write,
647
 
      .raw_readfn = raw_read, .raw_writefn = raw_write },
648
 
    /* Unimplemented, RAZ/WI. XXX PMUSERENR */
 
706
      .accessfn = pmreg_access, .writefn = pmxevtyper_write,
 
707
      .raw_writefn = raw_write },
 
708
    /* Unimplemented, RAZ/WI. */
649
709
    { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
650
 
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
 
710
      .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
 
711
      .accessfn = pmreg_access },
651
712
    { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
652
713
      .access = PL0_R | PL1_RW,
653
714
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
662
723
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
663
724
      .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
664
725
      .resetvalue = 0, .writefn = pmintenclr_write, },
665
 
    { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
 
726
    { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
 
727
      .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
 
728
      .access = PL1_RW, .writefn = vbar_write,
 
729
      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
 
730
      .resetvalue = 0 },
 
731
    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
 
732
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
 
733
      .resetvalue = 0, },
 
734
    { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
 
735
      .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
666
736
      .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
667
 
    { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
 
737
    { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
 
738
      .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
668
739
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
669
740
      .writefn = csselr_write, .resetvalue = 0 },
670
741
    /* Auxiliary ID register: this actually has an IMPDEF value but for now
672
743
     */
673
744
    { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7,
674
745
      .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
 
746
    /* MAIR can just read-as-written because we don't implement caches
 
747
     * and so don't need to care about memory attributes.
 
748
     */
 
749
    { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
 
750
      .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
 
751
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el1),
 
752
      .resetvalue = 0 },
 
753
    /* For non-long-descriptor page tables these are PRRR and NMRR;
 
754
     * regardless they still act as reads-as-written for QEMU.
 
755
     * The override is necessary because of the overly-broad TLB_LOCKDOWN
 
756
     * definition.
 
757
     */
 
758
    { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
 
759
      .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
 
760
      .fieldoffset = offsetoflow32(CPUARMState, cp15.mair_el1),
 
761
      .resetfn = arm_cp_reset_ignore },
 
762
    { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
 
763
      .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
 
764
      .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el1),
 
765
      .resetfn = arm_cp_reset_ignore },
675
766
    REGINFO_SENTINEL
676
767
};
677
768
 
678
 
static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
769
static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
770
                        uint64_t value)
679
771
{
680
772
    value &= 1;
681
773
    env->teecr = value;
682
 
    return 0;
683
 
}
684
 
 
685
 
static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
686
 
                       uint64_t *value)
687
 
{
688
 
    /* This is a helper function because the user access rights
689
 
     * depend on the value of the TEECR.
690
 
     */
691
 
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
692
 
        return EXCP_UDEF;
693
 
    }
694
 
    *value = env->teehbr;
695
 
    return 0;
696
 
}
697
 
 
698
 
static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
699
 
                        uint64_t value)
700
 
{
701
 
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
702
 
        return EXCP_UDEF;
703
 
    }
704
 
    env->teehbr = value;
705
 
    return 0;
 
774
}
 
775
 
 
776
static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
 
777
{
 
778
    if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
 
779
        return CP_ACCESS_TRAP;
 
780
    }
 
781
    return CP_ACCESS_OK;
706
782
}
707
783
 
708
784
static const ARMCPRegInfo t2ee_cp_reginfo[] = {
712
788
      .writefn = teecr_write },
713
789
    { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
714
790
      .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
715
 
      .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write,
716
 
      .readfn = teehbr_read, .writefn = teehbr_write },
 
791
      .accessfn = teehbr_access, .resetvalue = 0 },
717
792
    REGINFO_SENTINEL
718
793
};
719
794
 
743
818
 
744
819
#ifndef CONFIG_USER_ONLY
745
820
 
 
821
static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
 
822
{
 
823
    /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
 
824
    if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
 
825
        return CP_ACCESS_TRAP;
 
826
    }
 
827
    return CP_ACCESS_OK;
 
828
}
 
829
 
 
830
static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
 
831
{
 
832
    /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
 
833
    if (arm_current_pl(env) == 0 &&
 
834
        !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
 
835
        return CP_ACCESS_TRAP;
 
836
    }
 
837
    return CP_ACCESS_OK;
 
838
}
 
839
 
 
840
static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
 
841
{
 
842
    /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
 
843
     * EL0[PV]TEN is zero.
 
844
     */
 
845
    if (arm_current_pl(env) == 0 &&
 
846
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
 
847
        return CP_ACCESS_TRAP;
 
848
    }
 
849
    return CP_ACCESS_OK;
 
850
}
 
851
 
 
852
static CPAccessResult gt_pct_access(CPUARMState *env,
 
853
                                         const ARMCPRegInfo *ri)
 
854
{
 
855
    return gt_counter_access(env, GTIMER_PHYS);
 
856
}
 
857
 
 
858
static CPAccessResult gt_vct_access(CPUARMState *env,
 
859
                                         const ARMCPRegInfo *ri)
 
860
{
 
861
    return gt_counter_access(env, GTIMER_VIRT);
 
862
}
 
863
 
 
864
static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
 
865
{
 
866
    return gt_timer_access(env, GTIMER_PHYS);
 
867
}
 
868
 
 
869
static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
 
870
{
 
871
    return gt_timer_access(env, GTIMER_VIRT);
 
872
}
 
873
 
746
874
static uint64_t gt_get_countervalue(CPUARMState *env)
747
875
{
748
876
    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
788
916
    }
789
917
}
790
918
 
791
 
static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri,
792
 
                          uint64_t *value)
793
 
{
794
 
    /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
795
 
    if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
796
 
        return EXCP_UDEF;
797
 
    }
798
 
    *value = env->cp15.c14_cntfrq;
799
 
    return 0;
800
 
}
801
 
 
802
919
static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
803
920
{
804
921
    ARMCPU *cpu = arm_env_get_cpu(env);
807
924
    timer_del(cpu->gt_timer[timeridx]);
808
925
}
809
926
 
810
 
static int gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri,
811
 
                       uint64_t *value)
812
 
{
813
 
    int timeridx = ri->opc1 & 1;
814
 
 
815
 
    if (arm_current_pl(env) == 0 &&
816
 
        !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
817
 
        return EXCP_UDEF;
818
 
    }
819
 
    *value = gt_get_countervalue(env);
820
 
    return 0;
821
 
}
822
 
 
823
 
static int gt_cval_read(CPUARMState *env, const ARMCPRegInfo *ri,
824
 
                        uint64_t *value)
825
 
{
826
 
    int timeridx = ri->opc1 & 1;
827
 
 
828
 
    if (arm_current_pl(env) == 0 &&
829
 
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
830
 
        return EXCP_UDEF;
831
 
    }
832
 
    *value = env->cp15.c14_timer[timeridx].cval;
833
 
    return 0;
834
 
}
835
 
 
836
 
static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
837
 
                         uint64_t value)
 
927
static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
 
928
{
 
929
    return gt_get_countervalue(env);
 
930
}
 
931
 
 
932
static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
933
                          uint64_t value)
838
934
{
839
935
    int timeridx = ri->opc1 & 1;
840
936
 
841
937
    env->cp15.c14_timer[timeridx].cval = value;
842
938
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
843
 
    return 0;
844
939
}
845
 
static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
846
 
                        uint64_t *value)
 
940
 
 
941
static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
847
942
{
848
943
    int timeridx = ri->crm & 1;
849
944
 
850
 
    if (arm_current_pl(env) == 0 &&
851
 
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
852
 
        return EXCP_UDEF;
853
 
    }
854
 
    *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval -
855
 
                        gt_get_countervalue(env));
856
 
    return 0;
 
945
    return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
 
946
                      gt_get_countervalue(env));
857
947
}
858
948
 
859
 
static int gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
860
 
                         uint64_t value)
 
949
static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
950
                          uint64_t value)
861
951
{
862
952
    int timeridx = ri->crm & 1;
863
953
 
864
954
    env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
865
955
        + sextract64(value, 0, 32);
866
956
    gt_recalc_timer(arm_env_get_cpu(env), timeridx);
867
 
    return 0;
868
 
}
869
 
 
870
 
static int gt_ctl_read(CPUARMState *env, const ARMCPRegInfo *ri,
871
 
                       uint64_t *value)
872
 
{
873
 
    int timeridx = ri->crm & 1;
874
 
 
875
 
    if (arm_current_pl(env) == 0 &&
876
 
        !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
877
 
        return EXCP_UDEF;
878
 
    }
879
 
    *value = env->cp15.c14_timer[timeridx].ctl;
880
 
    return 0;
881
 
}
882
 
 
883
 
static int gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
884
 
                        uint64_t value)
 
957
}
 
958
 
 
959
static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
960
                         uint64_t value)
885
961
{
886
962
    ARMCPU *cpu = arm_env_get_cpu(env);
887
963
    int timeridx = ri->crm & 1;
898
974
        qemu_set_irq(cpu->gt_timer_outputs[timeridx],
899
975
                     (oldval & 4) && (value & 2));
900
976
    }
901
 
    return 0;
902
977
}
903
978
 
904
979
void arm_gt_ptimer_cb(void *opaque)
921
996
     * Our reset value matches the fixed frequency we implement the timer at.
922
997
     */
923
998
    { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
924
 
      .access = PL1_RW | PL0_R,
 
999
      .type = ARM_CP_NO_MIGRATE,
 
1000
      .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
 
1001
      .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
 
1002
      .resetfn = arm_cp_reset_ignore,
 
1003
    },
 
1004
    { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
 
1005
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
 
1006
      .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
925
1007
      .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
926
1008
      .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
927
 
      .readfn = gt_cntfrq_read, .raw_readfn = raw_read,
928
1009
    },
929
1010
    /* overall control: mostly access permissions */
930
 
    { .name = "CNTKCTL", .cp = 15, .crn = 14, .crm = 1, .opc1 = 0, .opc2 = 0,
 
1011
    { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
 
1012
      .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
931
1013
      .access = PL1_RW,
932
1014
      .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
933
1015
      .resetvalue = 0,
934
1016
    },
935
1017
    /* per-timer control */
936
1018
    { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
 
1019
      .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
 
1020
      .accessfn = gt_ptimer_access,
 
1021
      .fieldoffset = offsetoflow32(CPUARMState,
 
1022
                                   cp15.c14_timer[GTIMER_PHYS].ctl),
 
1023
      .resetfn = arm_cp_reset_ignore,
 
1024
      .writefn = gt_ctl_write, .raw_writefn = raw_write,
 
1025
    },
 
1026
    { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
 
1027
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
937
1028
      .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
 
1029
      .accessfn = gt_ptimer_access,
938
1030
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
939
1031
      .resetvalue = 0,
940
 
      .readfn = gt_ctl_read, .writefn = gt_ctl_write,
941
 
      .raw_readfn = raw_read, .raw_writefn = raw_write,
 
1032
      .writefn = gt_ctl_write, .raw_writefn = raw_write,
942
1033
    },
943
1034
    { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
 
1035
      .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
 
1036
      .accessfn = gt_vtimer_access,
 
1037
      .fieldoffset = offsetoflow32(CPUARMState,
 
1038
                                   cp15.c14_timer[GTIMER_VIRT].ctl),
 
1039
      .resetfn = arm_cp_reset_ignore,
 
1040
      .writefn = gt_ctl_write, .raw_writefn = raw_write,
 
1041
    },
 
1042
    { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
 
1043
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
944
1044
      .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
 
1045
      .accessfn = gt_vtimer_access,
945
1046
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
946
1047
      .resetvalue = 0,
947
 
      .readfn = gt_ctl_read, .writefn = gt_ctl_write,
948
 
      .raw_readfn = raw_read, .raw_writefn = raw_write,
 
1048
      .writefn = gt_ctl_write, .raw_writefn = raw_write,
949
1049
    },
950
1050
    /* TimerValue views: a 32 bit downcounting view of the underlying state */
951
1051
    { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
952
1052
      .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
 
1053
      .accessfn = gt_ptimer_access,
 
1054
      .readfn = gt_tval_read, .writefn = gt_tval_write,
 
1055
    },
 
1056
    { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
 
1057
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
 
1058
      .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
953
1059
      .readfn = gt_tval_read, .writefn = gt_tval_write,
954
1060
    },
955
1061
    { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
956
1062
      .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
 
1063
      .accessfn = gt_vtimer_access,
 
1064
      .readfn = gt_tval_read, .writefn = gt_tval_write,
 
1065
    },
 
1066
    { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
 
1067
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
 
1068
      .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
957
1069
      .readfn = gt_tval_read, .writefn = gt_tval_write,
958
1070
    },
959
1071
    /* The counter itself */
960
1072
    { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
961
1073
      .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
 
1074
      .accessfn = gt_pct_access,
 
1075
      .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
 
1076
    },
 
1077
    { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
 
1078
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
 
1079
      .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
 
1080
      .accessfn = gt_pct_access,
962
1081
      .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
963
1082
    },
964
1083
    { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
965
1084
      .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
 
1085
      .accessfn = gt_vct_access,
 
1086
      .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
 
1087
    },
 
1088
    { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
 
1089
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
 
1090
      .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
 
1091
      .accessfn = gt_vct_access,
966
1092
      .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
967
1093
    },
968
1094
    /* Comparison value, indicating when the timer goes off */
969
1095
    { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
970
1096
      .access = PL1_RW | PL0_R,
971
 
      .type = ARM_CP_64BIT | ARM_CP_IO,
972
 
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
973
 
      .resetvalue = 0,
974
 
      .readfn = gt_cval_read, .writefn = gt_cval_write,
975
 
      .raw_readfn = raw_read, .raw_writefn = raw_write,
 
1097
      .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
 
1098
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
 
1099
      .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore,
 
1100
      .writefn = gt_cval_write, .raw_writefn = raw_write,
 
1101
    },
 
1102
    { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
 
1103
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
 
1104
      .access = PL1_RW | PL0_R,
 
1105
      .type = ARM_CP_IO,
 
1106
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
 
1107
      .resetvalue = 0, .accessfn = gt_vtimer_access,
 
1108
      .writefn = gt_cval_write, .raw_writefn = raw_write,
976
1109
    },
977
1110
    { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
978
1111
      .access = PL1_RW | PL0_R,
979
 
      .type = ARM_CP_64BIT | ARM_CP_IO,
980
 
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
981
 
      .resetvalue = 0,
982
 
      .readfn = gt_cval_read, .writefn = gt_cval_write,
983
 
      .raw_readfn = raw_read, .raw_writefn = raw_write,
 
1112
      .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
 
1113
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
 
1114
      .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore,
 
1115
      .writefn = gt_cval_write, .raw_writefn = raw_write,
 
1116
    },
 
1117
    { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
 
1118
      .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
 
1119
      .access = PL1_RW | PL0_R,
 
1120
      .type = ARM_CP_IO,
 
1121
      .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
 
1122
      .resetvalue = 0, .accessfn = gt_vtimer_access,
 
1123
      .writefn = gt_cval_write, .raw_writefn = raw_write,
984
1124
    },
985
1125
    REGINFO_SENTINEL
986
1126
};
996
1136
 
997
1137
#endif
998
1138
 
999
 
static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
1139
static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1000
1140
{
1001
1141
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1002
1142
        env->cp15.c7_par = value;
1005
1145
    } else {
1006
1146
        env->cp15.c7_par = value & 0xfffff1ff;
1007
1147
    }
1008
 
    return 0;
1009
1148
}
1010
1149
 
1011
1150
#ifndef CONFIG_USER_ONLY
1021
1160
        && (env->cp15.c2_control & (1U << 31));
1022
1161
}
1023
1162
 
1024
 
static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
1163
static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
 
1164
{
 
1165
    if (ri->opc2 & 4) {
 
1166
        /* Other states are only available with TrustZone; in
 
1167
         * a non-TZ implementation these registers don't exist
 
1168
         * at all, which is an Uncategorized trap. This underdecoding
 
1169
         * is safe because the reginfo is NO_MIGRATE.
 
1170
         */
 
1171
        return CP_ACCESS_TRAP_UNCATEGORIZED;
 
1172
    }
 
1173
    return CP_ACCESS_OK;
 
1174
}
 
1175
 
 
1176
static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1025
1177
{
1026
1178
    hwaddr phys_addr;
1027
1179
    target_ulong page_size;
1029
1181
    int ret, is_user = ri->opc2 & 2;
1030
1182
    int access_type = ri->opc2 & 1;
1031
1183
 
1032
 
    if (ri->opc2 & 4) {
1033
 
        /* Other states are only available with TrustZone */
1034
 
        return EXCP_UDEF;
1035
 
    }
1036
1184
    ret = get_phys_addr(env, value, access_type, is_user,
1037
1185
                        &phys_addr, &prot, &page_size);
1038
1186
    if (extended_addresses_enabled(env)) {
1068
1216
                env->cp15.c7_par = phys_addr & 0xfffff000;
1069
1217
            }
1070
1218
        } else {
1071
 
            env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1072
 
                ((ret & (12 << 1)) >> 6) |
 
1219
            env->cp15.c7_par = ((ret & (1 << 10)) >> 5) |
 
1220
                ((ret & (1 << 12)) >> 6) |
1073
1221
                ((ret & 0xf) << 1) | 1;
1074
1222
        }
1075
1223
        env->cp15.c7_par_hi = 0;
1076
1224
    }
1077
 
    return 0;
1078
1225
}
1079
1226
#endif
1080
1227
 
1085
1232
      .writefn = par_write },
1086
1233
#ifndef CONFIG_USER_ONLY
1087
1234
    { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1088
 
      .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
 
1235
      .access = PL1_W, .accessfn = ats_access,
 
1236
      .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1089
1237
#endif
1090
1238
    REGINFO_SENTINEL
1091
1239
};
1120
1268
    return ret;
1121
1269
}
1122
1270
 
1123
 
static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1124
 
                                uint64_t value)
 
1271
static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1272
                                 uint64_t value)
1125
1273
{
1126
1274
    env->cp15.c5_data = extended_mpu_ap_bits(value);
1127
 
    return 0;
1128
1275
}
1129
1276
 
1130
 
static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1131
 
                               uint64_t *value)
 
1277
static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1132
1278
{
1133
 
    *value = simple_mpu_ap_bits(env->cp15.c5_data);
1134
 
    return 0;
 
1279
    return simple_mpu_ap_bits(env->cp15.c5_data);
1135
1280
}
1136
1281
 
1137
 
static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1138
 
                                uint64_t value)
 
1282
static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1283
                                 uint64_t value)
1139
1284
{
1140
1285
    env->cp15.c5_insn = extended_mpu_ap_bits(value);
1141
 
    return 0;
1142
 
}
1143
 
 
1144
 
static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
1145
 
                               uint64_t *value)
1146
 
{
1147
 
    *value = simple_mpu_ap_bits(env->cp15.c5_insn);
1148
 
    return 0;
1149
 
}
1150
 
 
1151
 
static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
1152
 
                            uint64_t *value)
1153
 
{
1154
 
    if (ri->crm >= 8) {
1155
 
        return EXCP_UDEF;
1156
 
    }
1157
 
    *value = env->cp15.c6_region[ri->crm];
1158
 
    return 0;
1159
 
}
1160
 
 
1161
 
static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
1162
 
                             uint64_t value)
1163
 
{
1164
 
    if (ri->crm >= 8) {
1165
 
        return EXCP_UDEF;
1166
 
    }
1167
 
    env->cp15.c6_region[ri->crm] = value;
1168
 
    return 0;
 
1286
}
 
1287
 
 
1288
static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
 
1289
{
 
1290
    return simple_mpu_ap_bits(env->cp15.c5_insn);
1169
1291
}
1170
1292
 
1171
1293
static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1190
1312
      .access = PL1_RW,
1191
1313
      .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1192
1314
    /* Protection region base and size registers */
1193
 
    { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
1194
 
      .opc2 = CP_ANY, .access = PL1_RW,
1195
 
      .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
 
1315
    { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
 
1316
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1317
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
 
1318
    { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
 
1319
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1320
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
 
1321
    { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
 
1322
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1323
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
 
1324
    { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
 
1325
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1326
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
 
1327
    { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
 
1328
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1329
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
 
1330
    { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
 
1331
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1332
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
 
1333
    { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
 
1334
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1335
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
 
1336
    { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
 
1337
      .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
 
1338
      .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
1196
1339
    REGINFO_SENTINEL
1197
1340
};
1198
1341
 
1199
 
static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1200
 
                                uint64_t value)
 
1342
static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1343
                                 uint64_t value)
1201
1344
{
1202
1345
    int maskshift = extract32(value, 0, 3);
1203
1346
 
1204
 
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
 
1347
    if (arm_feature(env, ARM_FEATURE_LPAE) && (value & (1 << 31))) {
1205
1348
        value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1206
1349
    } else {
1207
1350
        value &= 7;
1214
1357
    env->cp15.c2_control = value;
1215
1358
    env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1216
1359
    env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
1217
 
    return 0;
1218
1360
}
1219
1361
 
1220
 
static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1221
 
                            uint64_t value)
 
1362
static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1363
                             uint64_t value)
1222
1364
{
 
1365
    ARMCPU *cpu = arm_env_get_cpu(env);
 
1366
 
1223
1367
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1224
1368
        /* With LPAE the TTBCR could result in a change of ASID
1225
1369
         * via the TTBCR.A1 bit, so do a TLB flush.
1226
1370
         */
1227
 
        tlb_flush(env, 1);
 
1371
        tlb_flush(CPU(cpu), 1);
1228
1372
    }
1229
 
    return vmsa_ttbcr_raw_write(env, ri, value);
 
1373
    vmsa_ttbcr_raw_write(env, ri, value);
1230
1374
}
1231
1375
 
1232
1376
static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1236
1380
    env->cp15.c2_mask = 0;
1237
1381
}
1238
1382
 
 
1383
static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1384
                               uint64_t value)
 
1385
{
 
1386
    ARMCPU *cpu = arm_env_get_cpu(env);
 
1387
 
 
1388
    /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
 
1389
    tlb_flush(CPU(cpu), 1);
 
1390
    env->cp15.c2_control = value;
 
1391
}
 
1392
 
 
1393
static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1394
                            uint64_t value)
 
1395
{
 
1396
    /* 64 bit accesses to the TTBRs can change the ASID and so we
 
1397
     * must flush the TLB.
 
1398
     */
 
1399
    if (cpreg_field_is_64bit(ri)) {
 
1400
        ARMCPU *cpu = arm_env_get_cpu(env);
 
1401
 
 
1402
        tlb_flush(CPU(cpu), 1);
 
1403
    }
 
1404
    raw_write(env, ri, value);
 
1405
}
 
1406
 
1239
1407
static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1240
1408
    { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1241
1409
      .access = PL1_RW,
1243
1411
    { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1244
1412
      .access = PL1_RW,
1245
1413
      .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
1246
 
    { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1247
 
      .access = PL1_RW,
1248
 
      .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
1249
 
    { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1250
 
      .access = PL1_RW,
1251
 
      .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, },
 
1414
    { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
 
1415
      .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
 
1416
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
 
1417
      .writefn = vmsa_ttbr_write, .resetvalue = 0 },
 
1418
    { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
 
1419
      .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
 
1420
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
 
1421
      .writefn = vmsa_ttbr_write, .resetvalue = 0 },
 
1422
    { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
 
1423
      .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
 
1424
      .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
 
1425
      .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
 
1426
      .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
1252
1427
    { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1253
 
      .access = PL1_RW, .writefn = vmsa_ttbcr_write,
1254
 
      .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
1255
 
      .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
 
1428
      .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, .writefn = vmsa_ttbcr_write,
 
1429
      .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write,
 
1430
      .fieldoffset = offsetoflow32(CPUARMState, cp15.c2_control) },
1256
1431
    { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1257
1432
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
1258
1433
      .resetvalue = 0, },
1259
1434
    REGINFO_SENTINEL
1260
1435
};
1261
1436
 
1262
 
static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1263
 
                               uint64_t value)
 
1437
static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1438
                                uint64_t value)
1264
1439
{
1265
1440
    env->cp15.c15_ticonfig = value & 0xe7;
1266
1441
    /* The OS_TYPE bit in this register changes the reported CPUID! */
1267
1442
    env->cp15.c0_cpuid = (value & (1 << 5)) ?
1268
1443
        ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1269
 
    return 0;
1270
1444
}
1271
1445
 
1272
 
static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1273
 
                               uint64_t value)
 
1446
static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1447
                                uint64_t value)
1274
1448
{
1275
1449
    env->cp15.c15_threadid = value & 0xffff;
1276
 
    return 0;
1277
1450
}
1278
1451
 
1279
 
static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1280
 
                          uint64_t value)
 
1452
static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1453
                           uint64_t value)
1281
1454
{
1282
1455
    /* Wait-for-interrupt (deprecated) */
1283
1456
    cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1284
 
    return 0;
1285
1457
}
1286
1458
 
1287
 
static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1288
 
                                 uint64_t value)
 
1459
static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1460
                                  uint64_t value)
1289
1461
{
1290
1462
    /* On OMAP there are registers indicating the max/min index of dcache lines
1291
1463
     * containing a dirty line; cache flush operations have to reset these.
1292
1464
     */
1293
1465
    env->cp15.c15_i_max = 0x000;
1294
1466
    env->cp15.c15_i_min = 0xff0;
1295
 
    return 0;
1296
1467
}
1297
1468
 
1298
1469
static const ARMCPRegInfo omap_cp_reginfo[] = {
1334
1505
    REGINFO_SENTINEL
1335
1506
};
1336
1507
 
1337
 
static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1338
 
                             uint64_t value)
 
1508
static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1509
                              uint64_t value)
1339
1510
{
1340
1511
    value &= 0x3fff;
1341
1512
    if (env->cp15.c15_cpar != value) {
1343
1514
        tb_flush(env);
1344
1515
        env->cp15.c15_cpar = value;
1345
1516
    }
1346
 
    return 0;
1347
1517
}
1348
1518
 
1349
1519
static const ARMCPRegInfo xscale_cp_reginfo[] = {
1366
1536
     */
1367
1537
    { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1368
1538
      .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1369
 
      .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
 
1539
      .access = PL1_RW,
 
1540
      .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE,
1370
1541
      .resetvalue = 0 },
1371
1542
    REGINFO_SENTINEL
1372
1543
};
1422
1593
    REGINFO_SENTINEL
1423
1594
};
1424
1595
 
1425
 
static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1426
 
                      uint64_t *value)
 
1596
static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1427
1597
{
1428
1598
    CPUState *cs = CPU(arm_env_get_cpu(env));
1429
1599
    uint32_t mpidr = cs->cpu_index;
1430
 
    /* We don't support setting cluster ID ([8..11])
 
1600
    /* We don't support setting cluster ID ([8..11]) (known as Aff1
 
1601
     * in later ARM ARM versions), or any of the higher affinity level fields,
1431
1602
     * so these bits always RAZ.
1432
1603
     */
1433
1604
    if (arm_feature(env, ARM_FEATURE_V7MP)) {
1438
1609
         * not currently model any of those cores.
1439
1610
         */
1440
1611
    }
1441
 
    *value = mpidr;
1442
 
    return 0;
 
1612
    return mpidr;
1443
1613
}
1444
1614
 
1445
1615
static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1446
 
    { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
 
1616
    { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
 
1617
      .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1447
1618
      .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1448
1619
    REGINFO_SENTINEL
1449
1620
};
1450
1621
 
1451
 
static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
 
1622
static uint64_t par64_read(CPUARMState *env, const ARMCPRegInfo *ri)
1452
1623
{
1453
 
    *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1454
 
    return 0;
 
1624
    return ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par;
1455
1625
}
1456
1626
 
1457
 
static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
1627
static void par64_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1628
                        uint64_t value)
1458
1629
{
1459
1630
    env->cp15.c7_par_hi = value >> 32;
1460
1631
    env->cp15.c7_par = value;
1461
 
    return 0;
1462
1632
}
1463
1633
 
1464
1634
static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1467
1637
    env->cp15.c7_par = 0;
1468
1638
}
1469
1639
 
1470
 
static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri,
1471
 
                        uint64_t *value)
1472
 
{
1473
 
    *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
1474
 
    return 0;
1475
 
}
1476
 
 
1477
 
static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1478
 
                             uint64_t value)
1479
 
{
1480
 
    env->cp15.c2_base0_hi = value >> 32;
1481
 
    env->cp15.c2_base0 = value;
1482
 
    return 0;
1483
 
}
1484
 
 
1485
 
static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri,
1486
 
                         uint64_t value)
1487
 
{
1488
 
    /* Writes to the 64 bit format TTBRs may change the ASID */
1489
 
    tlb_flush(env, 1);
1490
 
    return ttbr064_raw_write(env, ri, value);
1491
 
}
1492
 
 
1493
 
static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1494
 
{
1495
 
    env->cp15.c2_base0_hi = 0;
1496
 
    env->cp15.c2_base0 = 0;
1497
 
}
1498
 
 
1499
 
static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri,
1500
 
                        uint64_t *value)
1501
 
{
1502
 
    *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
1503
 
    return 0;
1504
 
}
1505
 
 
1506
 
static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri,
1507
 
                         uint64_t value)
1508
 
{
1509
 
    env->cp15.c2_base1_hi = value >> 32;
1510
 
    env->cp15.c2_base1 = value;
1511
 
    return 0;
1512
 
}
1513
 
 
1514
 
static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1515
 
{
1516
 
    env->cp15.c2_base1_hi = 0;
1517
 
    env->cp15.c2_base1 = 0;
1518
 
}
1519
 
 
1520
1640
static const ARMCPRegInfo lpae_cp_reginfo[] = {
1521
1641
    /* NOP AMAIR0/1: the override is because these clash with the rather
1522
1642
     * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1523
1643
     */
1524
 
    { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
 
1644
    { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
 
1645
      .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1525
1646
      .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1526
1647
      .resetvalue = 0 },
 
1648
    /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
1527
1649
    { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1528
1650
      .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1529
1651
      .resetvalue = 0 },
1536
1658
      .access = PL1_RW, .type = ARM_CP_64BIT,
1537
1659
      .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset },
1538
1660
    { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1539
 
      .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read,
1540
 
      .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write,
1541
 
      .resetfn = ttbr064_reset },
 
1661
      .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
 
1662
      .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
 
1663
      .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
1542
1664
    { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1543
 
      .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read,
1544
 
      .writefn = ttbr164_write, .resetfn = ttbr164_reset },
1545
 
    REGINFO_SENTINEL
1546
 
};
1547
 
 
1548
 
static int vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1549
 
{
1550
 
    CPREG_FIELD32(env, ri) = value & ~0x1f;
1551
 
    return 0;
1552
 
}
1553
 
 
1554
 
static const ARMCPRegInfo trustzone_cp_reginfo[] = {
1555
 
    /* Dummy implementations of registers; we don't enforce the
1556
 
     * 'secure mode only' access checks. TODO: revisit as part of
1557
 
     * proper fake-trustzone support.
1558
 
     */
1559
 
    { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
1560
 
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
1561
 
      .resetvalue = 0 },
1562
 
    { .name = "SDER", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 1,
1563
 
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sedbg),
1564
 
      .resetvalue = 0 },
1565
 
    { .name = "NSACR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 2,
1566
 
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_nseac),
1567
 
      .resetvalue = 0 },
1568
 
    { .name = "VBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
1569
 
      .access = PL1_RW, .writefn = vbar_write,
1570
 
      .fieldoffset = offsetof(CPUARMState, cp15.c12_vbar),
1571
 
      .resetvalue = 0 },
1572
 
    { .name = "MVBAR", .cp = 15, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 1,
1573
 
      .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c12_mvbar),
1574
 
      .writefn = vbar_write, .resetvalue = 0 },
1575
 
    REGINFO_SENTINEL
1576
 
};
1577
 
 
1578
 
static int aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1579
 
                          uint64_t *value)
1580
 
{
1581
 
    *value = vfp_get_fpcr(env);
1582
 
    return 0;
1583
 
}
1584
 
 
1585
 
static int aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1586
 
                           uint64_t value)
 
1665
      .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
 
1666
      .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
 
1667
      .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
 
1668
    REGINFO_SENTINEL
 
1669
};
 
1670
 
 
1671
static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 
1672
{
 
1673
    return vfp_get_fpcr(env);
 
1674
}
 
1675
 
 
1676
static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1677
                            uint64_t value)
1587
1678
{
1588
1679
    vfp_set_fpcr(env, value);
1589
 
    return 0;
1590
1680
}
1591
1681
 
1592
 
static int aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1593
 
                          uint64_t *value)
 
1682
static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1594
1683
{
1595
 
    *value = vfp_get_fpsr(env);
1596
 
    return 0;
 
1684
    return vfp_get_fpsr(env);
1597
1685
}
1598
1686
 
1599
 
static int aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1600
 
                           uint64_t value)
 
1687
static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1688
                            uint64_t value)
1601
1689
{
1602
1690
    vfp_set_fpsr(env, value);
1603
 
    return 0;
 
1691
}
 
1692
 
 
1693
static CPAccessResult aa64_cacheop_access(CPUARMState *env,
 
1694
                                          const ARMCPRegInfo *ri)
 
1695
{
 
1696
    /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
 
1697
     * SCTLR_EL1.UCI is set.
 
1698
     */
 
1699
    if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCI)) {
 
1700
        return CP_ACCESS_TRAP;
 
1701
    }
 
1702
    return CP_ACCESS_OK;
 
1703
}
 
1704
 
 
1705
static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1706
                               uint64_t value)
 
1707
{
 
1708
    /* Invalidate by VA (AArch64 version) */
 
1709
    ARMCPU *cpu = arm_env_get_cpu(env);
 
1710
    uint64_t pageaddr = value << 12;
 
1711
    tlb_flush_page(CPU(cpu), pageaddr);
 
1712
}
 
1713
 
 
1714
static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1715
                                uint64_t value)
 
1716
{
 
1717
    /* Invalidate by VA, all ASIDs (AArch64 version) */
 
1718
    ARMCPU *cpu = arm_env_get_cpu(env);
 
1719
    uint64_t pageaddr = value << 12;
 
1720
    tlb_flush_page(CPU(cpu), pageaddr);
 
1721
}
 
1722
 
 
1723
static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1724
                                 uint64_t value)
 
1725
{
 
1726
    /* Invalidate by ASID (AArch64 version) */
 
1727
    ARMCPU *cpu = arm_env_get_cpu(env);
 
1728
    int asid = extract64(value, 48, 16);
 
1729
    tlb_flush(CPU(cpu), asid == 0);
1604
1730
}
1605
1731
 
1606
1732
static const ARMCPRegInfo v8_cp_reginfo[] = {
1616
1742
    { .name = "FPSR", .state = ARM_CP_STATE_AA64,
1617
1743
      .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
1618
1744
      .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
1619
 
    /* This claims a 32 byte cacheline size for icache and dcache, VIPT icache.
1620
 
     * It will eventually need to have a CPU-specified reset value.
1621
 
     */
1622
 
    { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
1623
 
      .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
1624
 
      .access = PL0_R, .type = ARM_CP_CONST,
1625
 
      .resetvalue = 0x80030003 },
1626
1745
    /* Prohibit use of DC ZVA. OPTME: implement DC ZVA and allow its use.
1627
1746
     * For system mode the DZP bit here will need to be computed, not constant.
1628
1747
     */
1630
1749
      .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
1631
1750
      .access = PL0_R, .type = ARM_CP_CONST,
1632
1751
      .resetvalue = 0x10 },
 
1752
    { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
 
1753
      .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
 
1754
      .access = PL1_R, .type = ARM_CP_CURRENTEL },
 
1755
    /* Cache ops: all NOPs since we don't emulate caches */
 
1756
    { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
 
1757
      .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
 
1758
      .access = PL1_W, .type = ARM_CP_NOP },
 
1759
    { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
 
1760
      .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
 
1761
      .access = PL1_W, .type = ARM_CP_NOP },
 
1762
    { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
 
1763
      .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
 
1764
      .access = PL0_W, .type = ARM_CP_NOP,
 
1765
      .accessfn = aa64_cacheop_access },
 
1766
    { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
 
1767
      .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
 
1768
      .access = PL1_W, .type = ARM_CP_NOP },
 
1769
    { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
 
1770
      .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
 
1771
      .access = PL1_W, .type = ARM_CP_NOP },
 
1772
    { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
 
1773
      .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
 
1774
      .access = PL0_W, .type = ARM_CP_NOP,
 
1775
      .accessfn = aa64_cacheop_access },
 
1776
    { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
 
1777
      .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
 
1778
      .access = PL1_W, .type = ARM_CP_NOP },
 
1779
    { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
 
1780
      .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
 
1781
      .access = PL0_W, .type = ARM_CP_NOP,
 
1782
      .accessfn = aa64_cacheop_access },
 
1783
    { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
 
1784
      .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
 
1785
      .access = PL0_W, .type = ARM_CP_NOP,
 
1786
      .accessfn = aa64_cacheop_access },
 
1787
    { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
 
1788
      .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
 
1789
      .access = PL1_W, .type = ARM_CP_NOP },
 
1790
    /* TLBI operations */
 
1791
    { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
 
1792
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 3, .opc2 = 0,
 
1793
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1794
      .writefn = tlbiall_write },
 
1795
    { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
 
1796
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 3, .opc2 = 1,
 
1797
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1798
      .writefn = tlbi_aa64_va_write },
 
1799
    { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
 
1800
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 3, .opc2 = 2,
 
1801
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1802
      .writefn = tlbi_aa64_asid_write },
 
1803
    { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
 
1804
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 3, .opc2 = 3,
 
1805
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1806
      .writefn = tlbi_aa64_vaa_write },
 
1807
    { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
 
1808
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 3, .opc2 = 5,
 
1809
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1810
      .writefn = tlbi_aa64_va_write },
 
1811
    { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
 
1812
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 3, .opc2 = 7,
 
1813
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1814
      .writefn = tlbi_aa64_vaa_write },
 
1815
    { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
 
1816
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 7, .opc2 = 0,
 
1817
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1818
      .writefn = tlbiall_write },
 
1819
    { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
 
1820
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 7, .opc2 = 1,
 
1821
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1822
      .writefn = tlbi_aa64_va_write },
 
1823
    { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
 
1824
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 7, .opc2 = 2,
 
1825
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1826
      .writefn = tlbi_aa64_asid_write },
 
1827
    { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
 
1828
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 7, .opc2 = 3,
 
1829
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1830
      .writefn = tlbi_aa64_vaa_write },
 
1831
    { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
 
1832
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 7, .opc2 = 5,
 
1833
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1834
      .writefn = tlbi_aa64_va_write },
 
1835
    { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
 
1836
      .opc0 = 1, .opc2 = 0, .crn = 8, .crm = 7, .opc2 = 7,
 
1837
      .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
 
1838
      .writefn = tlbi_aa64_vaa_write },
 
1839
    /* Dummy implementation of monitor debug system control register:
 
1840
     * we don't support debug.
 
1841
     */
 
1842
    { .name = "MDSCR_EL1", .state = ARM_CP_STATE_AA64,
 
1843
      .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
 
1844
      .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
 
1845
    /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
 
1846
    { .name = "OSLAR_EL1", .state = ARM_CP_STATE_AA64,
 
1847
      .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
 
1848
      .access = PL1_W, .type = ARM_CP_NOP },
1633
1849
    REGINFO_SENTINEL
1634
1850
};
1635
1851
 
1636
 
static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
 
1852
static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
1853
                        uint64_t value)
1637
1854
{
 
1855
    ARMCPU *cpu = arm_env_get_cpu(env);
 
1856
 
1638
1857
    env->cp15.c1_sys = value;
1639
1858
    /* ??? Lots of these bits are not implemented.  */
1640
1859
    /* This may enable/disable the MMU, so do a TLB flush.  */
1641
 
    tlb_flush(env, 1);
1642
 
    return 0;
 
1860
    tlb_flush(CPU(cpu), 1);
 
1861
}
 
1862
 
 
1863
static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
 
1864
{
 
1865
    /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
 
1866
     * but the AArch32 CTR has its own reginfo struct)
 
1867
     */
 
1868
    if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCT)) {
 
1869
        return CP_ACCESS_TRAP;
 
1870
    }
 
1871
    return CP_ACCESS_OK;
 
1872
}
 
1873
 
 
1874
static void define_aarch64_debug_regs(ARMCPU *cpu)
 
1875
{
 
1876
    /* Define breakpoint and watchpoint registers. These do nothing
 
1877
     * but read as written, for now.
 
1878
     */
 
1879
    int i;
 
1880
 
 
1881
    for (i = 0; i < 16; i++) {
 
1882
        ARMCPRegInfo dbgregs[] = {
 
1883
            { .name = "DBGBVR", .state = ARM_CP_STATE_AA64,
 
1884
              .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
 
1885
              .access = PL1_RW,
 
1886
              .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]) },
 
1887
            { .name = "DBGBCR", .state = ARM_CP_STATE_AA64,
 
1888
              .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
 
1889
              .access = PL1_RW,
 
1890
              .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]) },
 
1891
            { .name = "DBGWVR", .state = ARM_CP_STATE_AA64,
 
1892
              .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
 
1893
              .access = PL1_RW,
 
1894
              .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]) },
 
1895
            { .name = "DBGWCR", .state = ARM_CP_STATE_AA64,
 
1896
              .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
 
1897
              .access = PL1_RW,
 
1898
              .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]) },
 
1899
               REGINFO_SENTINEL
 
1900
        };
 
1901
        define_arm_cp_regs(cpu, dbgregs);
 
1902
    }
1643
1903
}
1644
1904
 
1645
1905
void register_cp_regs_for_features(ARMCPU *cpu)
1716
1976
    }
1717
1977
    if (arm_feature(env, ARM_FEATURE_V7)) {
1718
1978
        /* v7 performance monitor control register: same implementor
1719
 
         * field as main ID register, and we implement no event counters.
 
1979
         * field as main ID register, and we implement only the cycle
 
1980
         * count register.
1720
1981
         */
 
1982
#ifndef CONFIG_USER_ONLY
1721
1983
        ARMCPRegInfo pmcr = {
1722
1984
            .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
1723
1985
            .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
 
1986
            .type = ARM_CP_IO,
1724
1987
            .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
1725
 
            .readfn = pmreg_read, .writefn = pmcr_write,
1726
 
            .raw_readfn = raw_read, .raw_writefn = raw_write,
 
1988
            .accessfn = pmreg_access, .writefn = pmcr_write,
 
1989
            .raw_writefn = raw_write,
1727
1990
        };
 
1991
        define_one_arm_cp_reg(cpu, &pmcr);
 
1992
#endif
1728
1993
        ARMCPRegInfo clidr = {
1729
 
            .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
 
1994
            .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
 
1995
            .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
1730
1996
            .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
1731
1997
        };
1732
 
        define_one_arm_cp_reg(cpu, &pmcr);
1733
1998
        define_one_arm_cp_reg(cpu, &clidr);
1734
1999
        define_arm_cp_regs(cpu, v7_cp_reginfo);
1735
2000
    } else {
1736
2001
        define_arm_cp_regs(cpu, not_v7_cp_reginfo);
1737
2002
    }
1738
2003
    if (arm_feature(env, ARM_FEATURE_V8)) {
 
2004
        /* AArch64 ID registers, which all have impdef reset values */
 
2005
        ARMCPRegInfo v8_idregs[] = {
 
2006
            { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
 
2007
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
 
2008
              .access = PL1_R, .type = ARM_CP_CONST,
 
2009
              .resetvalue = cpu->id_aa64pfr0 },
 
2010
            { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
 
2011
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
 
2012
              .access = PL1_R, .type = ARM_CP_CONST,
 
2013
              .resetvalue = cpu->id_aa64pfr1},
 
2014
            { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
 
2015
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
 
2016
              .access = PL1_R, .type = ARM_CP_CONST,
 
2017
              .resetvalue = cpu->id_aa64dfr0 },
 
2018
            { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
 
2019
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
 
2020
              .access = PL1_R, .type = ARM_CP_CONST,
 
2021
              .resetvalue = cpu->id_aa64dfr1 },
 
2022
            { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
 
2023
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
 
2024
              .access = PL1_R, .type = ARM_CP_CONST,
 
2025
              .resetvalue = cpu->id_aa64afr0 },
 
2026
            { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
 
2027
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
 
2028
              .access = PL1_R, .type = ARM_CP_CONST,
 
2029
              .resetvalue = cpu->id_aa64afr1 },
 
2030
            { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
 
2031
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
 
2032
              .access = PL1_R, .type = ARM_CP_CONST,
 
2033
              .resetvalue = cpu->id_aa64isar0 },
 
2034
            { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
 
2035
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
 
2036
              .access = PL1_R, .type = ARM_CP_CONST,
 
2037
              .resetvalue = cpu->id_aa64isar1 },
 
2038
            { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
 
2039
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
 
2040
              .access = PL1_R, .type = ARM_CP_CONST,
 
2041
              .resetvalue = cpu->id_aa64mmfr0 },
 
2042
            { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
 
2043
              .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
 
2044
              .access = PL1_R, .type = ARM_CP_CONST,
 
2045
              .resetvalue = cpu->id_aa64mmfr1 },
 
2046
            REGINFO_SENTINEL
 
2047
        };
 
2048
        define_arm_cp_regs(cpu, v8_idregs);
1739
2049
        define_arm_cp_regs(cpu, v8_cp_reginfo);
 
2050
        define_aarch64_debug_regs(cpu);
1740
2051
    }
1741
2052
    if (arm_feature(env, ARM_FEATURE_MPU)) {
1742
2053
        /* These are the MPU registers prior to PMSAv6. Any new
1782
2093
    if (arm_feature(env, ARM_FEATURE_LPAE)) {
1783
2094
        define_arm_cp_regs(cpu, lpae_cp_reginfo);
1784
2095
    }
1785
 
    if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
1786
 
        define_arm_cp_regs(cpu, trustzone_cp_reginfo);
1787
 
    }
1788
2096
    /* Slightly awkwardly, the OMAP and StrongARM cores need all of
1789
2097
     * cp15 crn=0 to be writes-ignored, whereas for other cores they should
1790
2098
     * be read-only (ie write causes UNDEF exception).
1805
2113
              .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
1806
2114
              .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
1807
2115
              .type = ARM_CP_OVERRIDE },
 
2116
            { .name = "MIDR_EL1", .state = ARM_CP_STATE_AA64,
 
2117
              .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 0, .crm = 0,
 
2118
              .access = PL1_R, .resetvalue = cpu->midr, .type = ARM_CP_CONST },
1808
2119
            { .name = "CTR",
1809
2120
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
1810
2121
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
 
2122
            { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
 
2123
              .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
 
2124
              .access = PL0_R, .accessfn = ctr_el0_access,
 
2125
              .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
1811
2126
            { .name = "TCMTR",
1812
2127
              .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
1813
2128
              .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1866
2181
        define_one_arm_cp_reg(cpu, &auxcr);
1867
2182
    }
1868
2183
 
 
2184
    if (arm_feature(env, ARM_FEATURE_CBAR)) {
 
2185
        ARMCPRegInfo cbar = {
 
2186
            .name = "CBAR", .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
 
2187
            .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
 
2188
            .fieldoffset = offsetof(CPUARMState, cp15.c15_config_base_address)
 
2189
        };
 
2190
        define_one_arm_cp_reg(cpu, &cbar);
 
2191
    }
 
2192
 
1869
2193
    /* Generic registers whose values depend on the implementation */
1870
2194
    {
1871
2195
        ARMCPRegInfo sctlr = {
1872
 
            .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
 
2196
            .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
 
2197
            .opc0 = 3, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
1873
2198
            .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
1874
2199
            .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
1875
2200
            .raw_writefn = raw_write,
1887
2212
 
1888
2213
ARMCPU *cpu_arm_init(const char *cpu_model)
1889
2214
{
1890
 
    ARMCPU *cpu;
1891
 
    ObjectClass *oc;
1892
 
 
1893
 
    oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
1894
 
    if (!oc) {
1895
 
        return NULL;
1896
 
    }
1897
 
    cpu = ARM_CPU(object_new(object_class_get_name(oc)));
1898
 
 
1899
 
    /* TODO this should be set centrally, once possible */
1900
 
    object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
1901
 
 
1902
 
    return cpu;
 
2215
    return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
1903
2216
}
1904
2217
 
1905
2218
void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
2048
2361
    if (opaque) {
2049
2362
        r2->opaque = opaque;
2050
2363
    }
 
2364
    /* reginfo passed to helpers is correct for the actual access,
 
2365
     * and is never ARM_CP_STATE_BOTH:
 
2366
     */
 
2367
    r2->state = state;
2051
2368
    /* Make sure reginfo passed to helpers for wildcarded regs
2052
2369
     * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
2053
2370
     */
2211
2528
    return g_hash_table_lookup(cpregs, &encoded_cp);
2212
2529
}
2213
2530
 
2214
 
int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
2215
 
                        uint64_t value)
 
2531
void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
 
2532
                         uint64_t value)
2216
2533
{
2217
2534
    /* Helper coprocessor write function for write-ignore registers */
2218
 
    return 0;
2219
2535
}
2220
2536
 
2221
 
int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
 
2537
uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
2222
2538
{
2223
2539
    /* Helper coprocessor write function for read-as-zero registers */
2224
 
    *value = 0;
2225
2540
    return 0;
2226
2541
}
2227
2542
 
2258
2573
        (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
2259
2574
        | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
2260
2575
        | ((env->condexec_bits & 0xfc) << 8)
2261
 
        | (env->GE << 16);
 
2576
        | (env->GE << 16) | (env->daif & CPSR_AIF);
2262
2577
}
2263
2578
 
2264
2579
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
2285
2600
        env->GE = (val >> 16) & 0xf;
2286
2601
    }
2287
2602
 
 
2603
    env->daif &= ~(CPSR_AIF & mask);
 
2604
    env->daif |= val & CPSR_AIF & mask;
 
2605
 
2288
2606
    if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
2289
2607
        if (bad_mode_switch(env, val & CPSR_M)) {
2290
2608
            /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
2357
2675
 
2358
2676
void arm_cpu_do_interrupt(CPUState *cs)
2359
2677
{
 
2678
    cs->exception_index = -1;
 
2679
}
 
2680
 
 
2681
int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
 
2682
                             int mmu_idx)
 
2683
{
2360
2684
    ARMCPU *cpu = ARM_CPU(cs);
2361
2685
    CPUARMState *env = &cpu->env;
2362
2686
 
2363
 
    env->exception_index = -1;
2364
 
}
2365
 
 
2366
 
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
2367
 
                              int mmu_idx)
2368
 
{
2369
2687
    if (rw == 2) {
2370
 
        env->exception_index = EXCP_PREFETCH_ABORT;
 
2688
        cs->exception_index = EXCP_PREFETCH_ABORT;
2371
2689
        env->cp15.c6_insn = address;
2372
2690
    } else {
2373
 
        env->exception_index = EXCP_DATA_ABORT;
 
2691
        cs->exception_index = EXCP_DATA_ABORT;
2374
2692
        env->cp15.c6_data = address;
2375
2693
    }
2376
2694
    return 1;
2379
2697
/* These should probably raise undefined insn exceptions.  */
2380
2698
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2381
2699
{
2382
 
    cpu_abort(env, "v7m_mrs %d\n", reg);
 
2700
    ARMCPU *cpu = arm_env_get_cpu(env);
 
2701
 
 
2702
    cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
2383
2703
}
2384
2704
 
2385
2705
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2386
2706
{
2387
 
    cpu_abort(env, "v7m_mrs %d\n", reg);
 
2707
    ARMCPU *cpu = arm_env_get_cpu(env);
 
2708
 
 
2709
    cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
2388
2710
    return 0;
2389
2711
}
2390
2712
 
2391
2713
void switch_mode(CPUARMState *env, int mode)
2392
2714
{
2393
 
    if (mode != ARM_CPU_MODE_USR)
2394
 
        cpu_abort(env, "Tried to switch out of user mode\n");
 
2715
    ARMCPU *cpu = arm_env_get_cpu(env);
 
2716
 
 
2717
    if (mode != ARM_CPU_MODE_USR) {
 
2718
        cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
 
2719
    }
2395
2720
}
2396
2721
 
2397
2722
void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2398
2723
{
2399
 
    cpu_abort(env, "banked r13 write\n");
 
2724
    ARMCPU *cpu = arm_env_get_cpu(env);
 
2725
 
 
2726
    cpu_abort(CPU(cpu), "banked r13 write\n");
2400
2727
}
2401
2728
 
2402
2729
uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2403
2730
{
2404
 
    cpu_abort(env, "banked r13 read\n");
 
2731
    ARMCPU *cpu = arm_env_get_cpu(env);
 
2732
 
 
2733
    cpu_abort(CPU(cpu), "banked r13 read\n");
2405
2734
    return 0;
2406
2735
}
2407
2736
 
2424
2753
        return 4;
2425
2754
    case ARM_CPU_MODE_FIQ:
2426
2755
        return 5;
2427
 
    case ARM_CPU_MODE_SMC:
2428
 
        return 6;
2429
2756
    }
2430
2757
    hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
2431
2758
}
2460
2787
 
2461
2788
static void v7m_push(CPUARMState *env, uint32_t val)
2462
2789
{
 
2790
    CPUState *cs = CPU(arm_env_get_cpu(env));
 
2791
 
2463
2792
    env->regs[13] -= 4;
2464
 
    stl_phys(env->regs[13], val);
 
2793
    stl_phys(cs->as, env->regs[13], val);
2465
2794
}
2466
2795
 
2467
2796
static uint32_t v7m_pop(CPUARMState *env)
2468
2797
{
 
2798
    CPUState *cs = CPU(arm_env_get_cpu(env));
2469
2799
    uint32_t val;
2470
 
    val = ldl_phys(env->regs[13]);
 
2800
 
 
2801
    val = ldl_phys(cs->as, env->regs[13]);
2471
2802
    env->regs[13] += 4;
2472
2803
    return val;
2473
2804
}
2554
2885
    uint32_t lr;
2555
2886
    uint32_t addr;
2556
2887
 
2557
 
    arm_log_exception(env->exception_index);
 
2888
    arm_log_exception(cs->exception_index);
2558
2889
 
2559
2890
    lr = 0xfffffff1;
2560
2891
    if (env->v7m.current_sp)
2566
2897
       handle it.  */
2567
2898
    /* TODO: Need to escalate if the current priority is higher than the
2568
2899
       one we're raising.  */
2569
 
    switch (env->exception_index) {
 
2900
    switch (cs->exception_index) {
2570
2901
    case EXCP_UDEF:
2571
2902
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
2572
2903
        return;
2598
2929
        do_v7m_exception_exit(env);
2599
2930
        return;
2600
2931
    default:
2601
 
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
 
2932
        cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
2602
2933
        return; /* Never happens.  Keep compiler happy.  */
2603
2934
    }
2604
2935
 
2622
2953
    /* Clear IT bits */
2623
2954
    env->condexec_bits = 0;
2624
2955
    env->regs[14] = lr;
2625
 
    addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
 
2956
    addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
2626
2957
    env->regs[15] = addr & 0xfffffffe;
2627
2958
    env->thumb = addr & 1;
2628
2959
}
2639
2970
 
2640
2971
    assert(!IS_M(env));
2641
2972
 
2642
 
    arm_log_exception(env->exception_index);
 
2973
    arm_log_exception(cs->exception_index);
2643
2974
 
2644
2975
    /* TODO: Vectored interrupt controller.  */
2645
 
    switch (env->exception_index) {
 
2976
    switch (cs->exception_index) {
2646
2977
    case EXCP_UDEF:
2647
2978
        new_mode = ARM_CPU_MODE_UND;
2648
2979
        addr = 0x04;
2722
3053
        mask = CPSR_A | CPSR_I | CPSR_F;
2723
3054
        offset = 4;
2724
3055
        break;
2725
 
    case EXCP_SMC:
2726
 
        if (semihosting_enabled) {
2727
 
            cpu_abort(env, "SMC handling under semihosting not implemented\n");
2728
 
            return;
2729
 
        }
2730
 
        if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SMC) {
2731
 
            env->cp15.c1_scr &= ~1;
2732
 
        }
2733
 
        offset = env->thumb ? 2 : 0;
2734
 
        new_mode = ARM_CPU_MODE_SMC;
2735
 
        addr = 0x08;
2736
 
        mask = CPSR_A | CPSR_I | CPSR_F;
2737
 
        break;
2738
3056
    default:
2739
 
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
 
3057
        cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
2740
3058
        return; /* Never happens.  Keep compiler happy.  */
2741
3059
    }
2742
 
    if (arm_feature(env, ARM_FEATURE_TRUSTZONE)) {
2743
 
        if (new_mode == ARM_CPU_MODE_SMC ||
2744
 
            (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SMC) {
2745
 
            addr += env->cp15.c12_mvbar;
2746
 
        } else {
2747
 
            if (env->cp15.c1_sys & (1 << 13)) {
2748
 
                addr += 0xffff0000;
2749
 
            } else {
2750
 
                addr += env->cp15.c12_vbar;
2751
 
            }
2752
 
        }
 
3060
    /* High vectors.  */
 
3061
    if (env->cp15.c1_sys & SCTLR_V) {
 
3062
        /* when enabled, base address cannot be remapped.  */
 
3063
        addr += 0xffff0000;
2753
3064
    } else {
2754
 
        /* High vectors.  */
2755
 
        if (env->cp15.c1_sys & (1 << 13)) {
2756
 
            addr += 0xffff0000;
2757
 
        }
 
3065
        /* ARM v7 architectures provide a vector base address register to remap
 
3066
         * the interrupt vector table.
 
3067
         * This register is only followed in non-monitor mode, and has a secure
 
3068
         * and un-secure copy. Since the cpu is always in a un-secure operation
 
3069
         * and is never in monitor mode this feature is always active.
 
3070
         * Note: only bits 31:5 are valid.
 
3071
         */
 
3072
        addr += env->cp15.c12_vbar;
2758
3073
    }
2759
3074
    switch_mode (env, new_mode);
2760
3075
    env->spsr = cpsr_read(env);
2762
3077
    env->condexec_bits = 0;
2763
3078
    /* Switch to the new mode, and to the correct instruction set.  */
2764
3079
    env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
2765
 
    env->uncached_cpsr |= mask;
 
3080
    env->daif |= mask;
2766
3081
    /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
2767
3082
     * and we should just guard the thumb mode on V4 */
2768
3083
    if (arm_feature(env, ARM_FEATURE_V4T)) {
2769
 
        env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
 
3084
        env->thumb = (env->cp15.c1_sys & SCTLR_TE) != 0;
2770
3085
    }
2771
3086
    env->regs[14] = env->regs[15] + offset;
2772
3087
    env->regs[15] = addr;
2792
3107
 
2793
3108
  switch (ap) {
2794
3109
  case 0:
 
3110
      if (arm_feature(env, ARM_FEATURE_V7)) {
 
3111
          return 0;
 
3112
      }
2795
3113
      if (access_type == 1)
2796
3114
          return 0;
2797
 
      switch ((env->cp15.c1_sys >> 8) & 3) {
2798
 
      case 1:
 
3115
      switch (env->cp15.c1_sys & (SCTLR_S | SCTLR_R)) {
 
3116
      case SCTLR_S:
2799
3117
          return is_user ? 0 : PAGE_READ;
2800
 
      case 2:
 
3118
      case SCTLR_R:
2801
3119
          return PAGE_READ;
2802
3120
      default:
2803
3121
          return 0;
2831
3149
    uint32_t table;
2832
3150
 
2833
3151
    if (address & env->cp15.c2_mask)
2834
 
        table = env->cp15.c2_base1 & 0xffffc000;
 
3152
        table = env->cp15.ttbr1_el1 & 0xffffc000;
2835
3153
    else
2836
 
        table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
 
3154
        table = env->cp15.ttbr0_el1 & env->cp15.c2_base_mask;
2837
3155
 
2838
3156
    table |= (address >> 18) & 0x3ffc;
2839
3157
    return table;
2843
3161
                            int is_user, hwaddr *phys_ptr,
2844
3162
                            int *prot, target_ulong *page_size)
2845
3163
{
 
3164
    CPUState *cs = CPU(arm_env_get_cpu(env));
2846
3165
    int code;
2847
3166
    uint32_t table;
2848
3167
    uint32_t desc;
2855
3174
    /* Pagetable walk.  */
2856
3175
    /* Lookup l1 descriptor.  */
2857
3176
    table = get_level1_table_address(env, address);
2858
 
    desc = ldl_phys(table);
 
3177
    desc = ldl_phys(cs->as, table);
2859
3178
    type = (desc & 3);
2860
3179
    domain = (desc >> 5) & 0x0f;
2861
3180
    domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
2886
3205
            /* Fine pagetable.  */
2887
3206
            table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
2888
3207
        }
2889
 
        desc = ldl_phys(table);
 
3208
        desc = ldl_phys(cs->as, table);
2890
3209
        switch (desc & 3) {
2891
3210
        case 0: /* Page translation fault.  */
2892
3211
            code = 7;
2898
3217
            break;
2899
3218
        case 2: /* 4k page.  */
2900
3219
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
2901
 
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
 
3220
            ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
2902
3221
            *page_size = 0x1000;
2903
3222
            break;
2904
3223
        case 3: /* 1k page.  */
2938
3257
                            int is_user, hwaddr *phys_ptr,
2939
3258
                            int *prot, target_ulong *page_size)
2940
3259
{
 
3260
    CPUState *cs = CPU(arm_env_get_cpu(env));
2941
3261
    int code;
2942
3262
    uint32_t table;
2943
3263
    uint32_t desc;
2952
3272
    /* Pagetable walk.  */
2953
3273
    /* Lookup l1 descriptor.  */
2954
3274
    table = get_level1_table_address(env, address);
2955
 
    desc = ldl_phys(table);
 
3275
    desc = ldl_phys(cs->as, table);
2956
3276
    type = (desc & 3);
2957
3277
    if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
2958
3278
        /* Section translation fault, or attempt to use the encoding
2994
3314
        }
2995
3315
        /* Lookup l2 entry.  */
2996
3316
        table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
2997
 
        desc = ldl_phys(table);
 
3317
        desc = ldl_phys(cs->as, table);
2998
3318
        ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
2999
3319
        switch (desc & 3) {
3000
3320
        case 0: /* Page translation fault.  */
3026
3346
            goto do_fault;
3027
3347
 
3028
3348
        /* The simplified model uses AP[0] as an access control bit.  */
3029
 
        if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
 
3349
        if ((env->cp15.c1_sys & SCTLR_AFE) && (ap & 1) == 0) {
3030
3350
            /* Access flag fault.  */
3031
3351
            code = (code == 15) ? 6 : 3;
3032
3352
            goto do_fault;
3060
3380
                              hwaddr *phys_ptr, int *prot,
3061
3381
                              target_ulong *page_size_ptr)
3062
3382
{
 
3383
    CPUState *cs = CPU(arm_env_get_cpu(env));
3063
3384
    /* Read an LPAE long-descriptor translation table. */
3064
3385
    MMUFaultType fault_type = translation_fault;
3065
3386
    uint32_t level = 1;
3106
3427
     * we will always flush the TLB any time the ASID is changed).
3107
3428
     */
3108
3429
    if (ttbr_select == 0) {
3109
 
        ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0;
 
3430
        ttbr = env->cp15.ttbr0_el1;
3110
3431
        epd = extract32(env->cp15.c2_control, 7, 1);
3111
3432
        tsz = t0sz;
3112
3433
    } else {
3113
 
        ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1;
 
3434
        ttbr = env->cp15.ttbr1_el1;
3114
3435
        epd = extract32(env->cp15.c2_control, 23, 1);
3115
3436
        tsz = t1sz;
3116
3437
    }
3148
3469
        uint64_t descriptor;
3149
3470
 
3150
3471
        descaddr |= ((address >> (9 * (4 - level))) & 0xff8);
3151
 
        descriptor = ldq_phys(descaddr);
 
3472
        descriptor = ldq_phys(cs->as, descaddr);
3152
3473
        if (!(descriptor & 1) ||
3153
3474
            (!(descriptor & 2) && (level == 3))) {
3154
3475
            /* Invalid, or the Reserved level 3 encoding */
3317
3638
    if (address < 0x02000000)
3318
3639
        address += env->cp15.c13_fcse;
3319
3640
 
3320
 
    if ((env->cp15.c1_sys & 1) == 0) {
 
3641
    if ((env->cp15.c1_sys & SCTLR_M) == 0) {
3321
3642
        /* MMU/MPU disabled.  */
3322
3643
        *phys_ptr = address;
3323
3644
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
3330
3651
    } else if (extended_addresses_enabled(env)) {
3331
3652
        return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
3332
3653
                                  prot, page_size);
3333
 
    } else if (env->cp15.c1_sys & (1 << 23)) {
 
3654
    } else if (env->cp15.c1_sys & SCTLR_XP) {
3334
3655
        return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
3335
3656
                                prot, page_size);
3336
3657
    } else {
3339
3660
    }
3340
3661
}
3341
3662
 
3342
 
int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
3343
 
                              int access_type, int mmu_idx)
 
3663
int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
 
3664
                             int access_type, int mmu_idx)
3344
3665
{
 
3666
    ARMCPU *cpu = ARM_CPU(cs);
 
3667
    CPUARMState *env = &cpu->env;
3345
3668
    hwaddr phys_addr;
3346
3669
    target_ulong page_size;
3347
3670
    int prot;
3354
3677
        /* Map a single [sub]page.  */
3355
3678
        phys_addr &= ~(hwaddr)0x3ff;
3356
3679
        address &= ~(uint32_t)0x3ff;
3357
 
        tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
 
3680
        tlb_set_page(cs, address, phys_addr, prot, mmu_idx, page_size);
3358
3681
        return 0;
3359
3682
    }
3360
3683
 
3361
3684
    if (access_type == 2) {
3362
3685
        env->cp15.c5_insn = ret;
3363
3686
        env->cp15.c6_insn = address;
3364
 
        env->exception_index = EXCP_PREFETCH_ABORT;
 
3687
        cs->exception_index = EXCP_PREFETCH_ABORT;
3365
3688
    } else {
3366
3689
        env->cp15.c5_data = ret;
3367
3690
        if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
3368
3691
            env->cp15.c5_data |= (1 << 11);
3369
3692
        env->cp15.c6_data = address;
3370
 
        env->exception_index = EXCP_DATA_ABORT;
 
3693
        cs->exception_index = EXCP_DATA_ABORT;
3371
3694
    }
3372
3695
    return 1;
3373
3696
}
3409
3732
 
3410
3733
uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3411
3734
{
 
3735
    ARMCPU *cpu = arm_env_get_cpu(env);
 
3736
 
3412
3737
    switch (reg) {
3413
3738
    case 0: /* APSR */
3414
3739
        return xpsr_read(env) & 0xf8000000;
3429
3754
    case 9: /* PSP */
3430
3755
        return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
3431
3756
    case 16: /* PRIMASK */
3432
 
        return (env->uncached_cpsr & CPSR_I) != 0;
 
3757
        return (env->daif & PSTATE_I) != 0;
3433
3758
    case 17: /* BASEPRI */
3434
3759
    case 18: /* BASEPRI_MAX */
3435
3760
        return env->v7m.basepri;
3436
3761
    case 19: /* FAULTMASK */
3437
 
        return (env->uncached_cpsr & CPSR_F) != 0;
 
3762
        return (env->daif & PSTATE_F) != 0;
3438
3763
    case 20: /* CONTROL */
3439
3764
        return env->v7m.control;
3440
3765
    default:
3441
3766
        /* ??? For debugging only.  */
3442
 
        cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
 
3767
        cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
3443
3768
        return 0;
3444
3769
    }
3445
3770
}
3446
3771
 
3447
3772
void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3448
3773
{
 
3774
    ARMCPU *cpu = arm_env_get_cpu(env);
 
3775
 
3449
3776
    switch (reg) {
3450
3777
    case 0: /* APSR */
3451
3778
        xpsr_write(env, val, 0xf8000000);
3481
3808
            env->v7m.other_sp = val;
3482
3809
        break;
3483
3810
    case 16: /* PRIMASK */
3484
 
        if (val & 1)
3485
 
            env->uncached_cpsr |= CPSR_I;
3486
 
        else
3487
 
            env->uncached_cpsr &= ~CPSR_I;
 
3811
        if (val & 1) {
 
3812
            env->daif |= PSTATE_I;
 
3813
        } else {
 
3814
            env->daif &= ~PSTATE_I;
 
3815
        }
3488
3816
        break;
3489
3817
    case 17: /* BASEPRI */
3490
3818
        env->v7m.basepri = val & 0xff;
3495
3823
            env->v7m.basepri = val;
3496
3824
        break;
3497
3825
    case 19: /* FAULTMASK */
3498
 
        if (val & 1)
3499
 
            env->uncached_cpsr |= CPSR_F;
3500
 
        else
3501
 
            env->uncached_cpsr &= ~CPSR_F;
 
3826
        if (val & 1) {
 
3827
            env->daif |= PSTATE_F;
 
3828
        } else {
 
3829
            env->daif &= ~PSTATE_F;
 
3830
        }
3502
3831
        break;
3503
3832
    case 20: /* CONTROL */
3504
3833
        env->v7m.control = val & 3;
3506
3835
        break;
3507
3836
    default:
3508
3837
        /* ??? For debugging only.  */
3509
 
        cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
 
3838
        cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
3510
3839
        return;
3511
3840
    }
3512
3841
}
4191
4520
 * int->float conversions at run-time.  */
4192
4521
#define float64_256 make_float64(0x4070000000000000LL)
4193
4522
#define float64_512 make_float64(0x4080000000000000LL)
 
4523
#define float32_maxnorm make_float32(0x7f7fffff)
 
4524
#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
4194
4525
 
4195
 
/* The algorithm that must be used to calculate the estimate
4196
 
 * is specified by the ARM ARM.
 
4526
/* Reciprocal functions
 
4527
 *
 
4528
 * The algorithm that must be used to calculate the estimate
 
4529
 * is specified by the ARM ARM, see FPRecipEstimate()
4197
4530
 */
4198
 
static float64 recip_estimate(float64 a, CPUARMState *env)
 
4531
 
 
4532
static float64 recip_estimate(float64 a, float_status *real_fp_status)
4199
4533
{
4200
4534
    /* These calculations mustn't set any fp exception flags,
4201
4535
     * so we use a local copy of the fp_status.
4202
4536
     */
4203
 
    float_status dummy_status = env->vfp.standard_fp_status;
 
4537
    float_status dummy_status = *real_fp_status;
4204
4538
    float_status *s = &dummy_status;
4205
4539
    /* q = (int)(a * 512.0) */
4206
4540
    float64 q = float64_mul(float64_512, a, s);
4221
4555
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
4222
4556
}
4223
4557
 
4224
 
float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
4225
 
{
4226
 
    float_status *s = &env->vfp.standard_fp_status;
4227
 
    float64 f64;
4228
 
    uint32_t val32 = float32_val(a);
4229
 
 
4230
 
    int result_exp;
4231
 
    int a_exp = (val32  & 0x7f800000) >> 23;
4232
 
    int sign = val32 & 0x80000000;
4233
 
 
4234
 
    if (float32_is_any_nan(a)) {
4235
 
        if (float32_is_signaling_nan(a)) {
4236
 
            float_raise(float_flag_invalid, s);
4237
 
        }
4238
 
        return float32_default_nan;
4239
 
    } else if (float32_is_infinity(a)) {
4240
 
        return float32_set_sign(float32_zero, float32_is_neg(a));
4241
 
    } else if (float32_is_zero_or_denormal(a)) {
4242
 
        if (!float32_is_zero(a)) {
4243
 
            float_raise(float_flag_input_denormal, s);
4244
 
        }
4245
 
        float_raise(float_flag_divbyzero, s);
4246
 
        return float32_set_sign(float32_infinity, float32_is_neg(a));
4247
 
    } else if (a_exp >= 253) {
4248
 
        float_raise(float_flag_underflow, s);
4249
 
        return float32_set_sign(float32_zero, float32_is_neg(a));
4250
 
    }
4251
 
 
4252
 
    f64 = make_float64((0x3feULL << 52)
4253
 
                       | ((int64_t)(val32 & 0x7fffff) << 29));
4254
 
 
4255
 
    result_exp = 253 - a_exp;
4256
 
 
4257
 
    f64 = recip_estimate(f64, env);
4258
 
 
4259
 
    val32 = sign
4260
 
        | ((result_exp & 0xff) << 23)
4261
 
        | ((float64_val(f64) >> 29) & 0x7fffff);
4262
 
    return make_float32(val32);
 
4558
/* Common wrapper to call recip_estimate */
 
4559
static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
 
4560
{
 
4561
    uint64_t val64 = float64_val(num);
 
4562
    uint64_t frac = extract64(val64, 0, 52);
 
4563
    int64_t exp = extract64(val64, 52, 11);
 
4564
    uint64_t sbit;
 
4565
    float64 scaled, estimate;
 
4566
 
 
4567
    /* Generate the scaled number for the estimate function */
 
4568
    if (exp == 0) {
 
4569
        if (extract64(frac, 51, 1) == 0) {
 
4570
            exp = -1;
 
4571
            frac = extract64(frac, 0, 50) << 2;
 
4572
        } else {
 
4573
            frac = extract64(frac, 0, 51) << 1;
 
4574
        }
 
4575
    }
 
4576
 
 
4577
    /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
 
4578
    scaled = make_float64((0x3feULL << 52)
 
4579
                          | extract64(frac, 44, 8) << 44);
 
4580
 
 
4581
    estimate = recip_estimate(scaled, fpst);
 
4582
 
 
4583
    /* Build new result */
 
4584
    val64 = float64_val(estimate);
 
4585
    sbit = 0x8000000000000000ULL & val64;
 
4586
    exp = off - exp;
 
4587
    frac = extract64(val64, 0, 52);
 
4588
 
 
4589
    if (exp == 0) {
 
4590
        frac = 1ULL << 51 | extract64(frac, 1, 51);
 
4591
    } else if (exp == -1) {
 
4592
        frac = 1ULL << 50 | extract64(frac, 2, 50);
 
4593
        exp = 0;
 
4594
    }
 
4595
 
 
4596
    return make_float64(sbit | (exp << 52) | frac);
 
4597
}
 
4598
 
 
4599
static bool round_to_inf(float_status *fpst, bool sign_bit)
 
4600
{
 
4601
    switch (fpst->float_rounding_mode) {
 
4602
    case float_round_nearest_even: /* Round to Nearest */
 
4603
        return true;
 
4604
    case float_round_up: /* Round to +Inf */
 
4605
        return !sign_bit;
 
4606
    case float_round_down: /* Round to -Inf */
 
4607
        return sign_bit;
 
4608
    case float_round_to_zero: /* Round to Zero */
 
4609
        return false;
 
4610
    }
 
4611
 
 
4612
    g_assert_not_reached();
 
4613
}
 
4614
 
 
4615
float32 HELPER(recpe_f32)(float32 input, void *fpstp)
 
4616
{
 
4617
    float_status *fpst = fpstp;
 
4618
    float32 f32 = float32_squash_input_denormal(input, fpst);
 
4619
    uint32_t f32_val = float32_val(f32);
 
4620
    uint32_t f32_sbit = 0x80000000ULL & f32_val;
 
4621
    int32_t f32_exp = extract32(f32_val, 23, 8);
 
4622
    uint32_t f32_frac = extract32(f32_val, 0, 23);
 
4623
    float64 f64, r64;
 
4624
    uint64_t r64_val;
 
4625
    int64_t r64_exp;
 
4626
    uint64_t r64_frac;
 
4627
 
 
4628
    if (float32_is_any_nan(f32)) {
 
4629
        float32 nan = f32;
 
4630
        if (float32_is_signaling_nan(f32)) {
 
4631
            float_raise(float_flag_invalid, fpst);
 
4632
            nan = float32_maybe_silence_nan(f32);
 
4633
        }
 
4634
        if (fpst->default_nan_mode) {
 
4635
            nan =  float32_default_nan;
 
4636
        }
 
4637
        return nan;
 
4638
    } else if (float32_is_infinity(f32)) {
 
4639
        return float32_set_sign(float32_zero, float32_is_neg(f32));
 
4640
    } else if (float32_is_zero(f32)) {
 
4641
        float_raise(float_flag_divbyzero, fpst);
 
4642
        return float32_set_sign(float32_infinity, float32_is_neg(f32));
 
4643
    } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
 
4644
        /* Abs(value) < 2.0^-128 */
 
4645
        float_raise(float_flag_overflow | float_flag_inexact, fpst);
 
4646
        if (round_to_inf(fpst, f32_sbit)) {
 
4647
            return float32_set_sign(float32_infinity, float32_is_neg(f32));
 
4648
        } else {
 
4649
            return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
 
4650
        }
 
4651
    } else if (f32_exp >= 253 && fpst->flush_to_zero) {
 
4652
        float_raise(float_flag_underflow, fpst);
 
4653
        return float32_set_sign(float32_zero, float32_is_neg(f32));
 
4654
    }
 
4655
 
 
4656
 
 
4657
    f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
 
4658
    r64 = call_recip_estimate(f64, 253, fpst);
 
4659
    r64_val = float64_val(r64);
 
4660
    r64_exp = extract64(r64_val, 52, 11);
 
4661
    r64_frac = extract64(r64_val, 0, 52);
 
4662
 
 
4663
    /* result = sign : result_exp<7:0> : fraction<51:29>; */
 
4664
    return make_float32(f32_sbit |
 
4665
                        (r64_exp & 0xff) << 23 |
 
4666
                        extract64(r64_frac, 29, 24));
 
4667
}
 
4668
 
 
4669
float64 HELPER(recpe_f64)(float64 input, void *fpstp)
 
4670
{
 
4671
    float_status *fpst = fpstp;
 
4672
    float64 f64 = float64_squash_input_denormal(input, fpst);
 
4673
    uint64_t f64_val = float64_val(f64);
 
4674
    uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
 
4675
    int64_t f64_exp = extract64(f64_val, 52, 11);
 
4676
    float64 r64;
 
4677
    uint64_t r64_val;
 
4678
    int64_t r64_exp;
 
4679
    uint64_t r64_frac;
 
4680
 
 
4681
    /* Deal with any special cases */
 
4682
    if (float64_is_any_nan(f64)) {
 
4683
        float64 nan = f64;
 
4684
        if (float64_is_signaling_nan(f64)) {
 
4685
            float_raise(float_flag_invalid, fpst);
 
4686
            nan = float64_maybe_silence_nan(f64);
 
4687
        }
 
4688
        if (fpst->default_nan_mode) {
 
4689
            nan =  float64_default_nan;
 
4690
        }
 
4691
        return nan;
 
4692
    } else if (float64_is_infinity(f64)) {
 
4693
        return float64_set_sign(float64_zero, float64_is_neg(f64));
 
4694
    } else if (float64_is_zero(f64)) {
 
4695
        float_raise(float_flag_divbyzero, fpst);
 
4696
        return float64_set_sign(float64_infinity, float64_is_neg(f64));
 
4697
    } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
 
4698
        /* Abs(value) < 2.0^-1024 */
 
4699
        float_raise(float_flag_overflow | float_flag_inexact, fpst);
 
4700
        if (round_to_inf(fpst, f64_sbit)) {
 
4701
            return float64_set_sign(float64_infinity, float64_is_neg(f64));
 
4702
        } else {
 
4703
            return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
 
4704
        }
 
4705
    } else if (f64_exp >= 1023 && fpst->flush_to_zero) {
 
4706
        float_raise(float_flag_underflow, fpst);
 
4707
        return float64_set_sign(float64_zero, float64_is_neg(f64));
 
4708
    }
 
4709
 
 
4710
    r64 = call_recip_estimate(f64, 2045, fpst);
 
4711
    r64_val = float64_val(r64);
 
4712
    r64_exp = extract64(r64_val, 52, 11);
 
4713
    r64_frac = extract64(r64_val, 0, 52);
 
4714
 
 
4715
    /* result = sign : result_exp<10:0> : fraction<51:0> */
 
4716
    return make_float64(f64_sbit |
 
4717
                        ((r64_exp & 0x7ff) << 52) |
 
4718
                        r64_frac);
4263
4719
}
4264
4720
 
4265
4721
/* The algorithm that must be used to calculate the estimate
4266
4722
 * is specified by the ARM ARM.
4267
4723
 */
4268
 
static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
 
4724
static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
4269
4725
{
4270
4726
    /* These calculations mustn't set any fp exception flags,
4271
4727
     * so we use a local copy of the fp_status.
4272
4728
     */
4273
 
    float_status dummy_status = env->vfp.standard_fp_status;
 
4729
    float_status dummy_status = *real_fp_status;
4274
4730
    float_status *s = &dummy_status;
4275
4731
    float64 q;
4276
4732
    int64_t q_int;
4317
4773
    return float64_div(int64_to_float64(q_int, s), float64_256, s);
4318
4774
}
4319
4775
 
4320
 
float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
 
4776
float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
4321
4777
{
4322
 
    float_status *s = &env->vfp.standard_fp_status;
4323
 
    int result_exp;
4324
 
    float64 f64;
4325
 
    uint32_t val;
 
4778
    float_status *s = fpstp;
 
4779
    float32 f32 = float32_squash_input_denormal(input, s);
 
4780
    uint32_t val = float32_val(f32);
 
4781
    uint32_t f32_sbit = 0x80000000 & val;
 
4782
    int32_t f32_exp = extract32(val, 23, 8);
 
4783
    uint32_t f32_frac = extract32(val, 0, 23);
 
4784
    uint64_t f64_frac;
4326
4785
    uint64_t val64;
4327
 
 
4328
 
    val = float32_val(a);
4329
 
 
4330
 
    if (float32_is_any_nan(a)) {
4331
 
        if (float32_is_signaling_nan(a)) {
 
4786
    int result_exp;
 
4787
    float64 f64;
 
4788
 
 
4789
    if (float32_is_any_nan(f32)) {
 
4790
        float32 nan = f32;
 
4791
        if (float32_is_signaling_nan(f32)) {
4332
4792
            float_raise(float_flag_invalid, s);
4333
 
        }
4334
 
        return float32_default_nan;
4335
 
    } else if (float32_is_zero_or_denormal(a)) {
4336
 
        if (!float32_is_zero(a)) {
4337
 
            float_raise(float_flag_input_denormal, s);
4338
 
        }
 
4793
            nan = float32_maybe_silence_nan(f32);
 
4794
        }
 
4795
        if (s->default_nan_mode) {
 
4796
            nan =  float32_default_nan;
 
4797
        }
 
4798
        return nan;
 
4799
    } else if (float32_is_zero(f32)) {
4339
4800
        float_raise(float_flag_divbyzero, s);
4340
 
        return float32_set_sign(float32_infinity, float32_is_neg(a));
4341
 
    } else if (float32_is_neg(a)) {
 
4801
        return float32_set_sign(float32_infinity, float32_is_neg(f32));
 
4802
    } else if (float32_is_neg(f32)) {
4342
4803
        float_raise(float_flag_invalid, s);
4343
4804
        return float32_default_nan;
4344
 
    } else if (float32_is_infinity(a)) {
 
4805
    } else if (float32_is_infinity(f32)) {
4345
4806
        return float32_zero;
4346
4807
    }
4347
4808
 
4348
 
    /* Normalize to a double-precision value between 0.25 and 1.0,
 
4809
    /* Scale and normalize to a double-precision value between 0.25 and 1.0,
4349
4810
     * preserving the parity of the exponent.  */
4350
 
    if ((val & 0x800000) == 0) {
4351
 
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
 
4811
 
 
4812
    f64_frac = ((uint64_t) f32_frac) << 29;
 
4813
    if (f32_exp == 0) {
 
4814
        while (extract64(f64_frac, 51, 1) == 0) {
 
4815
            f64_frac = f64_frac << 1;
 
4816
            f32_exp = f32_exp-1;
 
4817
        }
 
4818
        f64_frac = extract64(f64_frac, 0, 51) << 1;
 
4819
    }
 
4820
 
 
4821
    if (extract64(f32_exp, 0, 1) == 0) {
 
4822
        f64 = make_float64(((uint64_t) f32_sbit) << 32
4352
4823
                           | (0x3feULL << 52)
4353
 
                           | ((uint64_t)(val & 0x7fffff) << 29));
 
4824
                           | f64_frac);
4354
4825
    } else {
4355
 
        f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
 
4826
        f64 = make_float64(((uint64_t) f32_sbit) << 32
4356
4827
                           | (0x3fdULL << 52)
4357
 
                           | ((uint64_t)(val & 0x7fffff) << 29));
 
4828
                           | f64_frac);
4358
4829
    }
4359
4830
 
4360
 
    result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
 
4831
    result_exp = (380 - f32_exp) / 2;
4361
4832
 
4362
 
    f64 = recip_sqrt_estimate(f64, env);
 
4833
    f64 = recip_sqrt_estimate(f64, s);
4363
4834
 
4364
4835
    val64 = float64_val(f64);
4365
4836
 
4368
4839
    return make_float32(val);
4369
4840
}
4370
4841
 
4371
 
uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
4372
 
{
 
4842
float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
 
4843
{
 
4844
    float_status *s = fpstp;
 
4845
    float64 f64 = float64_squash_input_denormal(input, s);
 
4846
    uint64_t val = float64_val(f64);
 
4847
    uint64_t f64_sbit = 0x8000000000000000ULL & val;
 
4848
    int64_t f64_exp = extract64(val, 52, 11);
 
4849
    uint64_t f64_frac = extract64(val, 0, 52);
 
4850
    int64_t result_exp;
 
4851
    uint64_t result_frac;
 
4852
 
 
4853
    if (float64_is_any_nan(f64)) {
 
4854
        float64 nan = f64;
 
4855
        if (float64_is_signaling_nan(f64)) {
 
4856
            float_raise(float_flag_invalid, s);
 
4857
            nan = float64_maybe_silence_nan(f64);
 
4858
        }
 
4859
        if (s->default_nan_mode) {
 
4860
            nan =  float64_default_nan;
 
4861
        }
 
4862
        return nan;
 
4863
    } else if (float64_is_zero(f64)) {
 
4864
        float_raise(float_flag_divbyzero, s);
 
4865
        return float64_set_sign(float64_infinity, float64_is_neg(f64));
 
4866
    } else if (float64_is_neg(f64)) {
 
4867
        float_raise(float_flag_invalid, s);
 
4868
        return float64_default_nan;
 
4869
    } else if (float64_is_infinity(f64)) {
 
4870
        return float64_zero;
 
4871
    }
 
4872
 
 
4873
    /* Scale and normalize to a double-precision value between 0.25 and 1.0,
 
4874
     * preserving the parity of the exponent.  */
 
4875
 
 
4876
    if (f64_exp == 0) {
 
4877
        while (extract64(f64_frac, 51, 1) == 0) {
 
4878
            f64_frac = f64_frac << 1;
 
4879
            f64_exp = f64_exp - 1;
 
4880
        }
 
4881
        f64_frac = extract64(f64_frac, 0, 51) << 1;
 
4882
    }
 
4883
 
 
4884
    if (extract64(f64_exp, 0, 1) == 0) {
 
4885
        f64 = make_float64(f64_sbit
 
4886
                           | (0x3feULL << 52)
 
4887
                           | f64_frac);
 
4888
    } else {
 
4889
        f64 = make_float64(f64_sbit
 
4890
                           | (0x3fdULL << 52)
 
4891
                           | f64_frac);
 
4892
    }
 
4893
 
 
4894
    result_exp = (3068 - f64_exp) / 2;
 
4895
 
 
4896
    f64 = recip_sqrt_estimate(f64, s);
 
4897
 
 
4898
    result_frac = extract64(float64_val(f64), 0, 52);
 
4899
 
 
4900
    return make_float64(f64_sbit |
 
4901
                        ((result_exp & 0x7ff) << 52) |
 
4902
                        result_frac);
 
4903
}
 
4904
 
 
4905
uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
 
4906
{
 
4907
    float_status *s = fpstp;
4373
4908
    float64 f64;
4374
4909
 
4375
4910
    if ((a & 0x80000000) == 0) {
4379
4914
    f64 = make_float64((0x3feULL << 52)
4380
4915
                       | ((int64_t)(a & 0x7fffffff) << 21));
4381
4916
 
4382
 
    f64 = recip_estimate (f64, env);
 
4917
    f64 = recip_estimate(f64, s);
4383
4918
 
4384
4919
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4385
4920
}
4386
4921
 
4387
 
uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
 
4922
uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
4388
4923
{
 
4924
    float_status *fpst = fpstp;
4389
4925
    float64 f64;
4390
4926
 
4391
4927
    if ((a & 0xc0000000) == 0) {
4400
4936
                           | ((uint64_t)(a & 0x3fffffff) << 22));
4401
4937
    }
4402
4938
 
4403
 
    f64 = recip_sqrt_estimate(f64, env);
 
4939
    f64 = recip_sqrt_estimate(f64, fpst);
4404
4940
 
4405
4941
    return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
4406
4942
}
4490
5026
    }
4491
5027
    return rmode;
4492
5028
}
 
5029
 
 
5030
static void crc_init_buffer(uint8_t *buf, uint32_t val, uint32_t bytes)
 
5031
{
 
5032
    memset(buf, 0, 4);
 
5033
 
 
5034
    if (bytes == 1) {
 
5035
        buf[0] = val & 0xff;
 
5036
    } else if (bytes == 2) {
 
5037
        buf[0] = val & 0xff;
 
5038
        buf[1] = (val >> 8) & 0xff;
 
5039
    } else {
 
5040
        buf[0] = val & 0xff;
 
5041
        buf[1] = (val >> 8) & 0xff;
 
5042
        buf[2] = (val >> 16) & 0xff;
 
5043
        buf[3] = (val >> 24) & 0xff;
 
5044
    }
 
5045
}
 
5046
 
 
5047
uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
 
5048
{
 
5049
    uint8_t buf[4];
 
5050
 
 
5051
    crc_init_buffer(buf, val, bytes);
 
5052
 
 
5053
    /* zlib crc32 converts the accumulator and output to one's complement.  */
 
5054
    return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
 
5055
}
 
5056
 
 
5057
uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
 
5058
{
 
5059
    uint8_t buf[4];
 
5060
 
 
5061
    crc_init_buffer(buf, val, bytes);
 
5062
 
 
5063
    /* Linux crc32c converts the output to one's complement.  */
 
5064
    return crc32c(acc, buf, bytes) ^ 0xffffffff;
 
5065
}