Statistics
| Branch: | Revision:

root / target-arm / helper.c @ 983fe826

History | View | Annotate | Download (72 kB)

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4

    
5
#include "cpu.h"
6
#include "exec-all.h"
7
#include "gdbstub.h"
8
#include "helpers.h"
9
#include "qemu-common.h"
10
#include "host-utils.h"
11
#include "hw/loader.h"
12

    
13
static uint32_t cortexa9_cp15_c0_c1[8] =
14
{ 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
15

    
16
static uint32_t cortexa9_cp15_c0_c2[8] =
17
{ 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
18

    
19
static uint32_t cortexa8_cp15_c0_c1[8] =
20
{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
21

    
22
static uint32_t cortexa8_cp15_c0_c2[8] =
23
{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
24

    
25
static uint32_t mpcore_cp15_c0_c1[8] =
26
{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
27

    
28
static uint32_t mpcore_cp15_c0_c2[8] =
29
{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
30

    
31
static uint32_t arm1136_cp15_c0_c1[8] =
32
{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
33

    
34
static uint32_t arm1136_cp15_c0_c2[8] =
35
{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
36

    
37
static uint32_t cpu_arm_find_by_name(const char *name);
38

    
39
static inline void set_feature(CPUARMState *env, int feature)
40
{
41
    env->features |= 1u << feature;
42
}
43

    
44
static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
45
{
46
    env->cp15.c0_cpuid = id;
47
    switch (id) {
48
    case ARM_CPUID_ARM926:
49
        set_feature(env, ARM_FEATURE_VFP);
50
        env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
51
        env->cp15.c0_cachetype = 0x1dd20d2;
52
        env->cp15.c1_sys = 0x00090078;
53
        break;
54
    case ARM_CPUID_ARM946:
55
        set_feature(env, ARM_FEATURE_MPU);
56
        env->cp15.c0_cachetype = 0x0f004006;
57
        env->cp15.c1_sys = 0x00000078;
58
        break;
59
    case ARM_CPUID_ARM1026:
60
        set_feature(env, ARM_FEATURE_VFP);
61
        set_feature(env, ARM_FEATURE_AUXCR);
62
        env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
63
        env->cp15.c0_cachetype = 0x1dd20d2;
64
        env->cp15.c1_sys = 0x00090078;
65
        break;
66
    case ARM_CPUID_ARM1136_R2:
67
    case ARM_CPUID_ARM1136:
68
        set_feature(env, ARM_FEATURE_V6);
69
        set_feature(env, ARM_FEATURE_VFP);
70
        set_feature(env, ARM_FEATURE_AUXCR);
71
        env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
72
        env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
73
        env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
74
        memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
75
        memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
76
        env->cp15.c0_cachetype = 0x1dd20d2;
77
        break;
78
    case ARM_CPUID_ARM11MPCORE:
79
        set_feature(env, ARM_FEATURE_V6);
80
        set_feature(env, ARM_FEATURE_V6K);
81
        set_feature(env, ARM_FEATURE_VFP);
82
        set_feature(env, ARM_FEATURE_AUXCR);
83
        env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
84
        env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
85
        env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
86
        memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
87
        memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
88
        env->cp15.c0_cachetype = 0x1dd20d2;
89
        break;
90
    case ARM_CPUID_CORTEXA8:
91
        set_feature(env, ARM_FEATURE_V6);
92
        set_feature(env, ARM_FEATURE_V6K);
93
        set_feature(env, ARM_FEATURE_V7);
94
        set_feature(env, ARM_FEATURE_AUXCR);
95
        set_feature(env, ARM_FEATURE_THUMB2);
96
        set_feature(env, ARM_FEATURE_VFP);
97
        set_feature(env, ARM_FEATURE_VFP3);
98
        set_feature(env, ARM_FEATURE_NEON);
99
        set_feature(env, ARM_FEATURE_THUMB2EE);
100
        env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
101
        env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
102
        env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
103
        memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
104
        memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
105
        env->cp15.c0_cachetype = 0x82048004;
106
        env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
107
        env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
108
        env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
109
        env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
110
        break;
111
    case ARM_CPUID_CORTEXA9:
112
        set_feature(env, ARM_FEATURE_V6);
113
        set_feature(env, ARM_FEATURE_V6K);
114
        set_feature(env, ARM_FEATURE_V7);
115
        set_feature(env, ARM_FEATURE_AUXCR);
116
        set_feature(env, ARM_FEATURE_THUMB2);
117
        set_feature(env, ARM_FEATURE_VFP);
118
        set_feature(env, ARM_FEATURE_VFP3);
119
        set_feature(env, ARM_FEATURE_VFP_FP16);
120
        set_feature(env, ARM_FEATURE_NEON);
121
        set_feature(env, ARM_FEATURE_THUMB2EE);
122
        env->vfp.xregs[ARM_VFP_FPSID] = 0x41034000; /* Guess */
123
        env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
124
        env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
125
        memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
126
        memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
127
        env->cp15.c0_cachetype = 0x80038003;
128
        env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
129
        env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
130
        env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
131
        break;
132
    case ARM_CPUID_CORTEXM3:
133
        set_feature(env, ARM_FEATURE_V6);
134
        set_feature(env, ARM_FEATURE_THUMB2);
135
        set_feature(env, ARM_FEATURE_V7);
136
        set_feature(env, ARM_FEATURE_M);
137
        set_feature(env, ARM_FEATURE_DIV);
138
        break;
139
    case ARM_CPUID_ANY: /* For userspace emulation.  */
140
        set_feature(env, ARM_FEATURE_V6);
141
        set_feature(env, ARM_FEATURE_V6K);
142
        set_feature(env, ARM_FEATURE_V7);
143
        set_feature(env, ARM_FEATURE_THUMB2);
144
        set_feature(env, ARM_FEATURE_VFP);
145
        set_feature(env, ARM_FEATURE_VFP3);
146
        set_feature(env, ARM_FEATURE_VFP_FP16);
147
        set_feature(env, ARM_FEATURE_NEON);
148
        set_feature(env, ARM_FEATURE_THUMB2EE);
149
        set_feature(env, ARM_FEATURE_DIV);
150
        break;
151
    case ARM_CPUID_TI915T:
152
    case ARM_CPUID_TI925T:
153
        set_feature(env, ARM_FEATURE_OMAPCP);
154
        env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring.  */
155
        env->cp15.c0_cachetype = 0x5109149;
156
        env->cp15.c1_sys = 0x00000070;
157
        env->cp15.c15_i_max = 0x000;
158
        env->cp15.c15_i_min = 0xff0;
159
        break;
160
    case ARM_CPUID_PXA250:
161
    case ARM_CPUID_PXA255:
162
    case ARM_CPUID_PXA260:
163
    case ARM_CPUID_PXA261:
164
    case ARM_CPUID_PXA262:
165
        set_feature(env, ARM_FEATURE_XSCALE);
166
        /* JTAG_ID is ((id << 28) | 0x09265013) */
167
        env->cp15.c0_cachetype = 0xd172172;
168
        env->cp15.c1_sys = 0x00000078;
169
        break;
170
    case ARM_CPUID_PXA270_A0:
171
    case ARM_CPUID_PXA270_A1:
172
    case ARM_CPUID_PXA270_B0:
173
    case ARM_CPUID_PXA270_B1:
174
    case ARM_CPUID_PXA270_C0:
175
    case ARM_CPUID_PXA270_C5:
176
        set_feature(env, ARM_FEATURE_XSCALE);
177
        /* JTAG_ID is ((id << 28) | 0x09265013) */
178
        set_feature(env, ARM_FEATURE_IWMMXT);
179
        env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
180
        env->cp15.c0_cachetype = 0xd172172;
181
        env->cp15.c1_sys = 0x00000078;
182
        break;
183
    default:
184
        cpu_abort(env, "Bad CPU ID: %x\n", id);
185
        break;
186
    }
187
}
188

    
189
void cpu_reset(CPUARMState *env)
190
{
191
    uint32_t id;
192

    
193
    if (qemu_loglevel_mask(CPU_LOG_RESET)) {
194
        qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
195
        log_cpu_state(env, 0);
196
    }
197

    
198
    id = env->cp15.c0_cpuid;
199
    memset(env, 0, offsetof(CPUARMState, breakpoints));
200
    if (id)
201
        cpu_reset_model_id(env, id);
202
#if defined (CONFIG_USER_ONLY)
203
    env->uncached_cpsr = ARM_CPU_MODE_USR;
204
    env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
205
#else
206
    /* SVC mode with interrupts disabled.  */
207
    env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
208
    env->regs[15] = 0;
209
    /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
210
       clear at reset.  Initial SP and PC are loaded from ROM.  */
211
    if (IS_M(env)) {
212
        uint32_t pc;
213
        uint8_t *rom;
214
        env->uncached_cpsr &= ~CPSR_I;
215
        rom = rom_ptr(0);
216
        if (rom) {
217
            /* We should really use ldl_phys here, in case the guest
218
               modified flash and reset itself.  However images
219
               loaded via -kenrel have not been copied yet, so load the
220
               values directly from there.  */
221
            env->regs[13] = ldl_p(rom);
222
            pc = ldl_p(rom + 4);
223
            env->thumb = pc & 1;
224
            env->regs[15] = pc & ~1;
225
        }
226
    }
227
    env->vfp.xregs[ARM_VFP_FPEXC] = 0;
228
    env->cp15.c2_base_mask = 0xffffc000u;
229
#endif
230
    tlb_flush(env, 1);
231
}
232

    
233
static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
234
{
235
    int nregs;
236

    
237
    /* VFP data registers are always little-endian.  */
238
    nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
239
    if (reg < nregs) {
240
        stfq_le_p(buf, env->vfp.regs[reg]);
241
        return 8;
242
    }
243
    if (arm_feature(env, ARM_FEATURE_NEON)) {
244
        /* Aliases for Q regs.  */
245
        nregs += 16;
246
        if (reg < nregs) {
247
            stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
248
            stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
249
            return 16;
250
        }
251
    }
252
    switch (reg - nregs) {
253
    case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
254
    case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
255
    case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
256
    }
257
    return 0;
258
}
259

    
260
static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
261
{
262
    int nregs;
263

    
264
    nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
265
    if (reg < nregs) {
266
        env->vfp.regs[reg] = ldfq_le_p(buf);
267
        return 8;
268
    }
269
    if (arm_feature(env, ARM_FEATURE_NEON)) {
270
        nregs += 16;
271
        if (reg < nregs) {
272
            env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
273
            env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
274
            return 16;
275
        }
276
    }
277
    switch (reg - nregs) {
278
    case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
279
    case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
280
    case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
281
    }
282
    return 0;
283
}
284

    
285
CPUARMState *cpu_arm_init(const char *cpu_model)
286
{
287
    CPUARMState *env;
288
    uint32_t id;
289
    static int inited = 0;
290

    
291
    id = cpu_arm_find_by_name(cpu_model);
292
    if (id == 0)
293
        return NULL;
294
    env = qemu_mallocz(sizeof(CPUARMState));
295
    cpu_exec_init(env);
296
    if (!inited) {
297
        inited = 1;
298
        arm_translate_init();
299
    }
300

    
301
    env->cpu_model_str = cpu_model;
302
    env->cp15.c0_cpuid = id;
303
    cpu_reset(env);
304
    if (arm_feature(env, ARM_FEATURE_NEON)) {
305
        gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
306
                                 51, "arm-neon.xml", 0);
307
    } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
308
        gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
309
                                 35, "arm-vfp3.xml", 0);
310
    } else if (arm_feature(env, ARM_FEATURE_VFP)) {
311
        gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
312
                                 19, "arm-vfp.xml", 0);
313
    }
314
    qemu_init_vcpu(env);
315
    return env;
316
}
317

    
318
struct arm_cpu_t {
319
    uint32_t id;
320
    const char *name;
321
};
322

    
323
static const struct arm_cpu_t arm_cpu_names[] = {
324
    { ARM_CPUID_ARM926, "arm926"},
325
    { ARM_CPUID_ARM946, "arm946"},
326
    { ARM_CPUID_ARM1026, "arm1026"},
327
    { ARM_CPUID_ARM1136, "arm1136"},
328
    { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
329
    { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
330
    { ARM_CPUID_CORTEXM3, "cortex-m3"},
331
    { ARM_CPUID_CORTEXA8, "cortex-a8"},
332
    { ARM_CPUID_CORTEXA9, "cortex-a9"},
333
    { ARM_CPUID_TI925T, "ti925t" },
334
    { ARM_CPUID_PXA250, "pxa250" },
335
    { ARM_CPUID_PXA255, "pxa255" },
336
    { ARM_CPUID_PXA260, "pxa260" },
337
    { ARM_CPUID_PXA261, "pxa261" },
338
    { ARM_CPUID_PXA262, "pxa262" },
339
    { ARM_CPUID_PXA270, "pxa270" },
340
    { ARM_CPUID_PXA270_A0, "pxa270-a0" },
341
    { ARM_CPUID_PXA270_A1, "pxa270-a1" },
342
    { ARM_CPUID_PXA270_B0, "pxa270-b0" },
343
    { ARM_CPUID_PXA270_B1, "pxa270-b1" },
344
    { ARM_CPUID_PXA270_C0, "pxa270-c0" },
345
    { ARM_CPUID_PXA270_C5, "pxa270-c5" },
346
    { ARM_CPUID_ANY, "any"},
347
    { 0, NULL}
348
};
349

    
350
void arm_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
351
{
352
    int i;
353

    
354
    (*cpu_fprintf)(f, "Available CPUs:\n");
355
    for (i = 0; arm_cpu_names[i].name; i++) {
356
        (*cpu_fprintf)(f, "  %s\n", arm_cpu_names[i].name);
357
    }
358
}
359

    
360
/* return 0 if not found */
361
static uint32_t cpu_arm_find_by_name(const char *name)
362
{
363
    int i;
364
    uint32_t id;
365

    
366
    id = 0;
367
    for (i = 0; arm_cpu_names[i].name; i++) {
368
        if (strcmp(name, arm_cpu_names[i].name) == 0) {
369
            id = arm_cpu_names[i].id;
370
            break;
371
        }
372
    }
373
    return id;
374
}
375

    
376
void cpu_arm_close(CPUARMState *env)
377
{
378
    free(env);
379
}
380

    
381
uint32_t cpsr_read(CPUARMState *env)
382
{
383
    int ZF;
384
    ZF = (env->ZF == 0);
385
    return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
386
        (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
387
        | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
388
        | ((env->condexec_bits & 0xfc) << 8)
389
        | (env->GE << 16);
390
}
391

    
392
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
393
{
394
    if (mask & CPSR_NZCV) {
395
        env->ZF = (~val) & CPSR_Z;
396
        env->NF = val;
397
        env->CF = (val >> 29) & 1;
398
        env->VF = (val << 3) & 0x80000000;
399
    }
400
    if (mask & CPSR_Q)
401
        env->QF = ((val & CPSR_Q) != 0);
402
    if (mask & CPSR_T)
403
        env->thumb = ((val & CPSR_T) != 0);
404
    if (mask & CPSR_IT_0_1) {
405
        env->condexec_bits &= ~3;
406
        env->condexec_bits |= (val >> 25) & 3;
407
    }
408
    if (mask & CPSR_IT_2_7) {
409
        env->condexec_bits &= 3;
410
        env->condexec_bits |= (val >> 8) & 0xfc;
411
    }
412
    if (mask & CPSR_GE) {
413
        env->GE = (val >> 16) & 0xf;
414
    }
415

    
416
    if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
417
        switch_mode(env, val & CPSR_M);
418
    }
419
    mask &= ~CACHED_CPSR_BITS;
420
    env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
421
}
422

    
423
/* Sign/zero extend */
424
uint32_t HELPER(sxtb16)(uint32_t x)
425
{
426
    uint32_t res;
427
    res = (uint16_t)(int8_t)x;
428
    res |= (uint32_t)(int8_t)(x >> 16) << 16;
429
    return res;
430
}
431

    
432
uint32_t HELPER(uxtb16)(uint32_t x)
433
{
434
    uint32_t res;
435
    res = (uint16_t)(uint8_t)x;
436
    res |= (uint32_t)(uint8_t)(x >> 16) << 16;
437
    return res;
438
}
439

    
440
uint32_t HELPER(clz)(uint32_t x)
441
{
442
    return clz32(x);
443
}
444

    
445
int32_t HELPER(sdiv)(int32_t num, int32_t den)
446
{
447
    if (den == 0)
448
      return 0;
449
    if (num == INT_MIN && den == -1)
450
      return INT_MIN;
451
    return num / den;
452
}
453

    
454
uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
455
{
456
    if (den == 0)
457
      return 0;
458
    return num / den;
459
}
460

    
461
uint32_t HELPER(rbit)(uint32_t x)
462
{
463
    x =  ((x & 0xff000000) >> 24)
464
       | ((x & 0x00ff0000) >> 8)
465
       | ((x & 0x0000ff00) << 8)
466
       | ((x & 0x000000ff) << 24);
467
    x =  ((x & 0xf0f0f0f0) >> 4)
468
       | ((x & 0x0f0f0f0f) << 4);
469
    x =  ((x & 0x88888888) >> 3)
470
       | ((x & 0x44444444) >> 1)
471
       | ((x & 0x22222222) << 1)
472
       | ((x & 0x11111111) << 3);
473
    return x;
474
}
475

    
476
uint32_t HELPER(abs)(uint32_t x)
477
{
478
    return ((int32_t)x < 0) ? -x : x;
479
}
480

    
481
#if defined(CONFIG_USER_ONLY)
482

    
483
void do_interrupt (CPUState *env)
484
{
485
    env->exception_index = -1;
486
}
487

    
488
int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
489
                              int mmu_idx, int is_softmmu)
490
{
491
    if (rw == 2) {
492
        env->exception_index = EXCP_PREFETCH_ABORT;
493
        env->cp15.c6_insn = address;
494
    } else {
495
        env->exception_index = EXCP_DATA_ABORT;
496
        env->cp15.c6_data = address;
497
    }
498
    return 1;
499
}
500

    
501
/* These should probably raise undefined insn exceptions.  */
502
void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
503
{
504
    int op1 = (insn >> 8) & 0xf;
505
    cpu_abort(env, "cp%i insn %08x\n", op1, insn);
506
    return;
507
}
508

    
509
uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
510
{
511
    int op1 = (insn >> 8) & 0xf;
512
    cpu_abort(env, "cp%i insn %08x\n", op1, insn);
513
    return 0;
514
}
515

    
516
void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
517
{
518
    cpu_abort(env, "cp15 insn %08x\n", insn);
519
}
520

    
521
uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
522
{
523
    cpu_abort(env, "cp15 insn %08x\n", insn);
524
}
525

    
526
/* These should probably raise undefined insn exceptions.  */
527
void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
528
{
529
    cpu_abort(env, "v7m_mrs %d\n", reg);
530
}
531

    
532
uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
533
{
534
    cpu_abort(env, "v7m_mrs %d\n", reg);
535
    return 0;
536
}
537

    
538
void switch_mode(CPUState *env, int mode)
539
{
540
    if (mode != ARM_CPU_MODE_USR)
541
        cpu_abort(env, "Tried to switch out of user mode\n");
542
}
543

    
544
void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
545
{
546
    cpu_abort(env, "banked r13 write\n");
547
}
548

    
549
uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
550
{
551
    cpu_abort(env, "banked r13 read\n");
552
    return 0;
553
}
554

    
555
#else
556

    
557
extern int semihosting_enabled;
558

    
559
/* Map CPU modes onto saved register banks.  */
560
static inline int bank_number (int mode)
561
{
562
    switch (mode) {
563
    case ARM_CPU_MODE_USR:
564
    case ARM_CPU_MODE_SYS:
565
        return 0;
566
    case ARM_CPU_MODE_SVC:
567
        return 1;
568
    case ARM_CPU_MODE_ABT:
569
        return 2;
570
    case ARM_CPU_MODE_UND:
571
        return 3;
572
    case ARM_CPU_MODE_IRQ:
573
        return 4;
574
    case ARM_CPU_MODE_FIQ:
575
        return 5;
576
    }
577
    cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
578
    return -1;
579
}
580

    
581
void switch_mode(CPUState *env, int mode)
582
{
583
    int old_mode;
584
    int i;
585

    
586
    old_mode = env->uncached_cpsr & CPSR_M;
587
    if (mode == old_mode)
588
        return;
589

    
590
    if (old_mode == ARM_CPU_MODE_FIQ) {
591
        memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
592
        memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
593
    } else if (mode == ARM_CPU_MODE_FIQ) {
594
        memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
595
        memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
596
    }
597

    
598
    i = bank_number(old_mode);
599
    env->banked_r13[i] = env->regs[13];
600
    env->banked_r14[i] = env->regs[14];
601
    env->banked_spsr[i] = env->spsr;
602

    
603
    i = bank_number(mode);
604
    env->regs[13] = env->banked_r13[i];
605
    env->regs[14] = env->banked_r14[i];
606
    env->spsr = env->banked_spsr[i];
607
}
608

    
609
static void v7m_push(CPUARMState *env, uint32_t val)
610
{
611
    env->regs[13] -= 4;
612
    stl_phys(env->regs[13], val);
613
}
614

    
615
static uint32_t v7m_pop(CPUARMState *env)
616
{
617
    uint32_t val;
618
    val = ldl_phys(env->regs[13]);
619
    env->regs[13] += 4;
620
    return val;
621
}
622

    
623
/* Switch to V7M main or process stack pointer.  */
624
static void switch_v7m_sp(CPUARMState *env, int process)
625
{
626
    uint32_t tmp;
627
    if (env->v7m.current_sp != process) {
628
        tmp = env->v7m.other_sp;
629
        env->v7m.other_sp = env->regs[13];
630
        env->regs[13] = tmp;
631
        env->v7m.current_sp = process;
632
    }
633
}
634

    
635
static void do_v7m_exception_exit(CPUARMState *env)
636
{
637
    uint32_t type;
638
    uint32_t xpsr;
639

    
640
    type = env->regs[15];
641
    if (env->v7m.exception != 0)
642
        armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
643

    
644
    /* Switch to the target stack.  */
645
    switch_v7m_sp(env, (type & 4) != 0);
646
    /* Pop registers.  */
647
    env->regs[0] = v7m_pop(env);
648
    env->regs[1] = v7m_pop(env);
649
    env->regs[2] = v7m_pop(env);
650
    env->regs[3] = v7m_pop(env);
651
    env->regs[12] = v7m_pop(env);
652
    env->regs[14] = v7m_pop(env);
653
    env->regs[15] = v7m_pop(env);
654
    xpsr = v7m_pop(env);
655
    xpsr_write(env, xpsr, 0xfffffdff);
656
    /* Undo stack alignment.  */
657
    if (xpsr & 0x200)
658
        env->regs[13] |= 4;
659
    /* ??? The exception return type specifies Thread/Handler mode.  However
660
       this is also implied by the xPSR value. Not sure what to do
661
       if there is a mismatch.  */
662
    /* ??? Likewise for mismatches between the CONTROL register and the stack
663
       pointer.  */
664
}
665

    
666
static void do_interrupt_v7m(CPUARMState *env)
667
{
668
    uint32_t xpsr = xpsr_read(env);
669
    uint32_t lr;
670
    uint32_t addr;
671

    
672
    lr = 0xfffffff1;
673
    if (env->v7m.current_sp)
674
        lr |= 4;
675
    if (env->v7m.exception == 0)
676
        lr |= 8;
677

    
678
    /* For exceptions we just mark as pending on the NVIC, and let that
679
       handle it.  */
680
    /* TODO: Need to escalate if the current priority is higher than the
681
       one we're raising.  */
682
    switch (env->exception_index) {
683
    case EXCP_UDEF:
684
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
685
        return;
686
    case EXCP_SWI:
687
        env->regs[15] += 2;
688
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
689
        return;
690
    case EXCP_PREFETCH_ABORT:
691
    case EXCP_DATA_ABORT:
692
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
693
        return;
694
    case EXCP_BKPT:
695
        if (semihosting_enabled) {
696
            int nr;
697
            nr = lduw_code(env->regs[15]) & 0xff;
698
            if (nr == 0xab) {
699
                env->regs[15] += 2;
700
                env->regs[0] = do_arm_semihosting(env);
701
                return;
702
            }
703
        }
704
        armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
705
        return;
706
    case EXCP_IRQ:
707
        env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
708
        break;
709
    case EXCP_EXCEPTION_EXIT:
710
        do_v7m_exception_exit(env);
711
        return;
712
    default:
713
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
714
        return; /* Never happens.  Keep compiler happy.  */
715
    }
716

    
717
    /* Align stack pointer.  */
718
    /* ??? Should only do this if Configuration Control Register
719
       STACKALIGN bit is set.  */
720
    if (env->regs[13] & 4) {
721
        env->regs[13] -= 4;
722
        xpsr |= 0x200;
723
    }
724
    /* Switch to the handler mode.  */
725
    v7m_push(env, xpsr);
726
    v7m_push(env, env->regs[15]);
727
    v7m_push(env, env->regs[14]);
728
    v7m_push(env, env->regs[12]);
729
    v7m_push(env, env->regs[3]);
730
    v7m_push(env, env->regs[2]);
731
    v7m_push(env, env->regs[1]);
732
    v7m_push(env, env->regs[0]);
733
    switch_v7m_sp(env, 0);
734
    env->uncached_cpsr &= ~CPSR_IT;
735
    env->regs[14] = lr;
736
    addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
737
    env->regs[15] = addr & 0xfffffffe;
738
    env->thumb = addr & 1;
739
}
740

    
741
/* Handle a CPU exception.  */
742
void do_interrupt(CPUARMState *env)
743
{
744
    uint32_t addr;
745
    uint32_t mask;
746
    int new_mode;
747
    uint32_t offset;
748

    
749
    if (IS_M(env)) {
750
        do_interrupt_v7m(env);
751
        return;
752
    }
753
    /* TODO: Vectored interrupt controller.  */
754
    switch (env->exception_index) {
755
    case EXCP_UDEF:
756
        new_mode = ARM_CPU_MODE_UND;
757
        addr = 0x04;
758
        mask = CPSR_I;
759
        if (env->thumb)
760
            offset = 2;
761
        else
762
            offset = 4;
763
        break;
764
    case EXCP_SWI:
765
        if (semihosting_enabled) {
766
            /* Check for semihosting interrupt.  */
767
            if (env->thumb) {
768
                mask = lduw_code(env->regs[15] - 2) & 0xff;
769
            } else {
770
                mask = ldl_code(env->regs[15] - 4) & 0xffffff;
771
            }
772
            /* Only intercept calls from privileged modes, to provide some
773
               semblance of security.  */
774
            if (((mask == 0x123456 && !env->thumb)
775
                    || (mask == 0xab && env->thumb))
776
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
777
                env->regs[0] = do_arm_semihosting(env);
778
                return;
779
            }
780
        }
781
        new_mode = ARM_CPU_MODE_SVC;
782
        addr = 0x08;
783
        mask = CPSR_I;
784
        /* The PC already points to the next instruction.  */
785
        offset = 0;
786
        break;
787
    case EXCP_BKPT:
788
        /* See if this is a semihosting syscall.  */
789
        if (env->thumb && semihosting_enabled) {
790
            mask = lduw_code(env->regs[15]) & 0xff;
791
            if (mask == 0xab
792
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
793
                env->regs[15] += 2;
794
                env->regs[0] = do_arm_semihosting(env);
795
                return;
796
            }
797
        }
798
        /* Fall through to prefetch abort.  */
799
    case EXCP_PREFETCH_ABORT:
800
        new_mode = ARM_CPU_MODE_ABT;
801
        addr = 0x0c;
802
        mask = CPSR_A | CPSR_I;
803
        offset = 4;
804
        break;
805
    case EXCP_DATA_ABORT:
806
        new_mode = ARM_CPU_MODE_ABT;
807
        addr = 0x10;
808
        mask = CPSR_A | CPSR_I;
809
        offset = 8;
810
        break;
811
    case EXCP_IRQ:
812
        new_mode = ARM_CPU_MODE_IRQ;
813
        addr = 0x18;
814
        /* Disable IRQ and imprecise data aborts.  */
815
        mask = CPSR_A | CPSR_I;
816
        offset = 4;
817
        break;
818
    case EXCP_FIQ:
819
        new_mode = ARM_CPU_MODE_FIQ;
820
        addr = 0x1c;
821
        /* Disable FIQ, IRQ and imprecise data aborts.  */
822
        mask = CPSR_A | CPSR_I | CPSR_F;
823
        offset = 4;
824
        break;
825
    default:
826
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
827
        return; /* Never happens.  Keep compiler happy.  */
828
    }
829
    /* High vectors.  */
830
    if (env->cp15.c1_sys & (1 << 13)) {
831
        addr += 0xffff0000;
832
    }
833
    switch_mode (env, new_mode);
834
    env->spsr = cpsr_read(env);
835
    /* Clear IT bits.  */
836
    env->condexec_bits = 0;
837
    /* Switch to the new mode, and to the correct instruction set.  */
838
    env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
839
    env->uncached_cpsr |= mask;
840
    env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
841
    env->regs[14] = env->regs[15] + offset;
842
    env->regs[15] = addr;
843
    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
844
}
845

    
846
/* Check section/page access permissions.
847
   Returns the page protection flags, or zero if the access is not
848
   permitted.  */
849
static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
850
                           int is_user)
851
{
852
  int prot_ro;
853

    
854
  if (domain == 3)
855
    return PAGE_READ | PAGE_WRITE;
856

    
857
  if (access_type == 1)
858
      prot_ro = 0;
859
  else
860
      prot_ro = PAGE_READ;
861

    
862
  switch (ap) {
863
  case 0:
864
      if (access_type == 1)
865
          return 0;
866
      switch ((env->cp15.c1_sys >> 8) & 3) {
867
      case 1:
868
          return is_user ? 0 : PAGE_READ;
869
      case 2:
870
          return PAGE_READ;
871
      default:
872
          return 0;
873
      }
874
  case 1:
875
      return is_user ? 0 : PAGE_READ | PAGE_WRITE;
876
  case 2:
877
      if (is_user)
878
          return prot_ro;
879
      else
880
          return PAGE_READ | PAGE_WRITE;
881
  case 3:
882
      return PAGE_READ | PAGE_WRITE;
883
  case 4: /* Reserved.  */
884
      return 0;
885
  case 5:
886
      return is_user ? 0 : prot_ro;
887
  case 6:
888
      return prot_ro;
889
  case 7:
890
      if (!arm_feature (env, ARM_FEATURE_V7))
891
          return 0;
892
      return prot_ro;
893
  default:
894
      abort();
895
  }
896
}
897

    
898
static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
899
{
900
    uint32_t table;
901

    
902
    if (address & env->cp15.c2_mask)
903
        table = env->cp15.c2_base1 & 0xffffc000;
904
    else
905
        table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
906

    
907
    table |= (address >> 18) & 0x3ffc;
908
    return table;
909
}
910

    
911
static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
912
                            int is_user, uint32_t *phys_ptr, int *prot,
913
                            target_ulong *page_size)
914
{
915
    int code;
916
    uint32_t table;
917
    uint32_t desc;
918
    int type;
919
    int ap;
920
    int domain;
921
    uint32_t phys_addr;
922

    
923
    /* Pagetable walk.  */
924
    /* Lookup l1 descriptor.  */
925
    table = get_level1_table_address(env, address);
926
    desc = ldl_phys(table);
927
    type = (desc & 3);
928
    domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
929
    if (type == 0) {
930
        /* Section translation fault.  */
931
        code = 5;
932
        goto do_fault;
933
    }
934
    if (domain == 0 || domain == 2) {
935
        if (type == 2)
936
            code = 9; /* Section domain fault.  */
937
        else
938
            code = 11; /* Page domain fault.  */
939
        goto do_fault;
940
    }
941
    if (type == 2) {
942
        /* 1Mb section.  */
943
        phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
944
        ap = (desc >> 10) & 3;
945
        code = 13;
946
        *page_size = 1024 * 1024;
947
    } else {
948
        /* Lookup l2 entry.  */
949
        if (type == 1) {
950
            /* Coarse pagetable.  */
951
            table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
952
        } else {
953
            /* Fine pagetable.  */
954
            table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
955
        }
956
        desc = ldl_phys(table);
957
        switch (desc & 3) {
958
        case 0: /* Page translation fault.  */
959
            code = 7;
960
            goto do_fault;
961
        case 1: /* 64k page.  */
962
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
963
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
964
            *page_size = 0x10000;
965
            break;
966
        case 2: /* 4k page.  */
967
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
968
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
969
            *page_size = 0x1000;
970
            break;
971
        case 3: /* 1k page.  */
972
            if (type == 1) {
973
                if (arm_feature(env, ARM_FEATURE_XSCALE)) {
974
                    phys_addr = (desc & 0xfffff000) | (address & 0xfff);
975
                } else {
976
                    /* Page translation fault.  */
977
                    code = 7;
978
                    goto do_fault;
979
                }
980
            } else {
981
                phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
982
            }
983
            ap = (desc >> 4) & 3;
984
            *page_size = 0x400;
985
            break;
986
        default:
987
            /* Never happens, but compiler isn't smart enough to tell.  */
988
            abort();
989
        }
990
        code = 15;
991
    }
992
    *prot = check_ap(env, ap, domain, access_type, is_user);
993
    if (!*prot) {
994
        /* Access permission fault.  */
995
        goto do_fault;
996
    }
997
    *prot |= PAGE_EXEC;
998
    *phys_ptr = phys_addr;
999
    return 0;
1000
do_fault:
1001
    return code | (domain << 4);
1002
}
1003

    
1004
static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
1005
                            int is_user, uint32_t *phys_ptr, int *prot,
1006
                            target_ulong *page_size)
1007
{
1008
    int code;
1009
    uint32_t table;
1010
    uint32_t desc;
1011
    uint32_t xn;
1012
    int type;
1013
    int ap;
1014
    int domain;
1015
    uint32_t phys_addr;
1016

    
1017
    /* Pagetable walk.  */
1018
    /* Lookup l1 descriptor.  */
1019
    table = get_level1_table_address(env, address);
1020
    desc = ldl_phys(table);
1021
    type = (desc & 3);
1022
    if (type == 0) {
1023
        /* Section translation fault.  */
1024
        code = 5;
1025
        domain = 0;
1026
        goto do_fault;
1027
    } else if (type == 2 && (desc & (1 << 18))) {
1028
        /* Supersection.  */
1029
        domain = 0;
1030
    } else {
1031
        /* Section or page.  */
1032
        domain = (desc >> 4) & 0x1e;
1033
    }
1034
    domain = (env->cp15.c3 >> domain) & 3;
1035
    if (domain == 0 || domain == 2) {
1036
        if (type == 2)
1037
            code = 9; /* Section domain fault.  */
1038
        else
1039
            code = 11; /* Page domain fault.  */
1040
        goto do_fault;
1041
    }
1042
    if (type == 2) {
1043
        if (desc & (1 << 18)) {
1044
            /* Supersection.  */
1045
            phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1046
            *page_size = 0x1000000;
1047
        } else {
1048
            /* Section.  */
1049
            phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1050
            *page_size = 0x100000;
1051
        }
1052
        ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1053
        xn = desc & (1 << 4);
1054
        code = 13;
1055
    } else {
1056
        /* Lookup l2 entry.  */
1057
        table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1058
        desc = ldl_phys(table);
1059
        ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1060
        switch (desc & 3) {
1061
        case 0: /* Page translation fault.  */
1062
            code = 7;
1063
            goto do_fault;
1064
        case 1: /* 64k page.  */
1065
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1066
            xn = desc & (1 << 15);
1067
            *page_size = 0x10000;
1068
            break;
1069
        case 2: case 3: /* 4k page.  */
1070
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1071
            xn = desc & 1;
1072
            *page_size = 0x1000;
1073
            break;
1074
        default:
1075
            /* Never happens, but compiler isn't smart enough to tell.  */
1076
            abort();
1077
        }
1078
        code = 15;
1079
    }
1080
    if (xn && access_type == 2)
1081
        goto do_fault;
1082

    
1083
    /* The simplified model uses AP[0] as an access control bit.  */
1084
    if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1085
        /* Access flag fault.  */
1086
        code = (code == 15) ? 6 : 3;
1087
        goto do_fault;
1088
    }
1089
    *prot = check_ap(env, ap, domain, access_type, is_user);
1090
    if (!*prot) {
1091
        /* Access permission fault.  */
1092
        goto do_fault;
1093
    }
1094
    if (!xn) {
1095
        *prot |= PAGE_EXEC;
1096
    }
1097
    *phys_ptr = phys_addr;
1098
    return 0;
1099
do_fault:
1100
    return code | (domain << 4);
1101
}
1102

    
1103
static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1104
                             int is_user, uint32_t *phys_ptr, int *prot)
1105
{
1106
    int n;
1107
    uint32_t mask;
1108
    uint32_t base;
1109

    
1110
    *phys_ptr = address;
1111
    for (n = 7; n >= 0; n--) {
1112
        base = env->cp15.c6_region[n];
1113
        if ((base & 1) == 0)
1114
            continue;
1115
        mask = 1 << ((base >> 1) & 0x1f);
1116
        /* Keep this shift separate from the above to avoid an
1117
           (undefined) << 32.  */
1118
        mask = (mask << 1) - 1;
1119
        if (((base ^ address) & ~mask) == 0)
1120
            break;
1121
    }
1122
    if (n < 0)
1123
        return 2;
1124

    
1125
    if (access_type == 2) {
1126
        mask = env->cp15.c5_insn;
1127
    } else {
1128
        mask = env->cp15.c5_data;
1129
    }
1130
    mask = (mask >> (n * 4)) & 0xf;
1131
    switch (mask) {
1132
    case 0:
1133
        return 1;
1134
    case 1:
1135
        if (is_user)
1136
          return 1;
1137
        *prot = PAGE_READ | PAGE_WRITE;
1138
        break;
1139
    case 2:
1140
        *prot = PAGE_READ;
1141
        if (!is_user)
1142
            *prot |= PAGE_WRITE;
1143
        break;
1144
    case 3:
1145
        *prot = PAGE_READ | PAGE_WRITE;
1146
        break;
1147
    case 5:
1148
        if (is_user)
1149
            return 1;
1150
        *prot = PAGE_READ;
1151
        break;
1152
    case 6:
1153
        *prot = PAGE_READ;
1154
        break;
1155
    default:
1156
        /* Bad permission.  */
1157
        return 1;
1158
    }
1159
    *prot |= PAGE_EXEC;
1160
    return 0;
1161
}
1162

    
1163
static inline int get_phys_addr(CPUState *env, uint32_t address,
1164
                                int access_type, int is_user,
1165
                                uint32_t *phys_ptr, int *prot,
1166
                                target_ulong *page_size)
1167
{
1168
    /* Fast Context Switch Extension.  */
1169
    if (address < 0x02000000)
1170
        address += env->cp15.c13_fcse;
1171

    
1172
    if ((env->cp15.c1_sys & 1) == 0) {
1173
        /* MMU/MPU disabled.  */
1174
        *phys_ptr = address;
1175
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1176
        *page_size = TARGET_PAGE_SIZE;
1177
        return 0;
1178
    } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1179
        *page_size = TARGET_PAGE_SIZE;
1180
        return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1181
                                 prot);
1182
    } else if (env->cp15.c1_sys & (1 << 23)) {
1183
        return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1184
                                prot, page_size);
1185
    } else {
1186
        return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1187
                                prot, page_size);
1188
    }
1189
}
1190

    
1191
int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
1192
                              int access_type, int mmu_idx, int is_softmmu)
1193
{
1194
    uint32_t phys_addr;
1195
    target_ulong page_size;
1196
    int prot;
1197
    int ret, is_user;
1198

    
1199
    is_user = mmu_idx == MMU_USER_IDX;
1200
    ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1201
                        &page_size);
1202
    if (ret == 0) {
1203
        /* Map a single [sub]page.  */
1204
        phys_addr &= ~(uint32_t)0x3ff;
1205
        address &= ~(uint32_t)0x3ff;
1206
        tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1207
        return 0;
1208
    }
1209

    
1210
    if (access_type == 2) {
1211
        env->cp15.c5_insn = ret;
1212
        env->cp15.c6_insn = address;
1213
        env->exception_index = EXCP_PREFETCH_ABORT;
1214
    } else {
1215
        env->cp15.c5_data = ret;
1216
        if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1217
            env->cp15.c5_data |= (1 << 11);
1218
        env->cp15.c6_data = address;
1219
        env->exception_index = EXCP_DATA_ABORT;
1220
    }
1221
    return 1;
1222
}
1223

    
1224
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1225
{
1226
    uint32_t phys_addr;
1227
    target_ulong page_size;
1228
    int prot;
1229
    int ret;
1230

    
1231
    ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1232

    
1233
    if (ret != 0)
1234
        return -1;
1235

    
1236
    return phys_addr;
1237
}
1238

    
1239
void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
1240
{
1241
    int cp_num = (insn >> 8) & 0xf;
1242
    int cp_info = (insn >> 5) & 7;
1243
    int src = (insn >> 16) & 0xf;
1244
    int operand = insn & 0xf;
1245

    
1246
    if (env->cp[cp_num].cp_write)
1247
        env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1248
                                 cp_info, src, operand, val);
1249
}
1250

    
1251
uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
1252
{
1253
    int cp_num = (insn >> 8) & 0xf;
1254
    int cp_info = (insn >> 5) & 7;
1255
    int dest = (insn >> 16) & 0xf;
1256
    int operand = insn & 0xf;
1257

    
1258
    if (env->cp[cp_num].cp_read)
1259
        return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1260
                                       cp_info, dest, operand);
1261
    return 0;
1262
}
1263

    
1264
/* Return basic MPU access permission bits.  */
1265
static uint32_t simple_mpu_ap_bits(uint32_t val)
1266
{
1267
    uint32_t ret;
1268
    uint32_t mask;
1269
    int i;
1270
    ret = 0;
1271
    mask = 3;
1272
    for (i = 0; i < 16; i += 2) {
1273
        ret |= (val >> i) & mask;
1274
        mask <<= 2;
1275
    }
1276
    return ret;
1277
}
1278

    
1279
/* Pad basic MPU access permission bits to extended format.  */
1280
static uint32_t extended_mpu_ap_bits(uint32_t val)
1281
{
1282
    uint32_t ret;
1283
    uint32_t mask;
1284
    int i;
1285
    ret = 0;
1286
    mask = 3;
1287
    for (i = 0; i < 16; i += 2) {
1288
        ret |= (val & mask) << i;
1289
        mask <<= 2;
1290
    }
1291
    return ret;
1292
}
1293

    
1294
void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
1295
{
1296
    int op1;
1297
    int op2;
1298
    int crm;
1299

    
1300
    op1 = (insn >> 21) & 7;
1301
    op2 = (insn >> 5) & 7;
1302
    crm = insn & 0xf;
1303
    switch ((insn >> 16) & 0xf) {
1304
    case 0:
1305
        /* ID codes.  */
1306
        if (arm_feature(env, ARM_FEATURE_XSCALE))
1307
            break;
1308
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1309
            break;
1310
        if (arm_feature(env, ARM_FEATURE_V7)
1311
                && op1 == 2 && crm == 0 && op2 == 0) {
1312
            env->cp15.c0_cssel = val & 0xf;
1313
            break;
1314
        }
1315
        goto bad_reg;
1316
    case 1: /* System configuration.  */
1317
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1318
            op2 = 0;
1319
        switch (op2) {
1320
        case 0:
1321
            if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1322
                env->cp15.c1_sys = val;
1323
            /* ??? Lots of these bits are not implemented.  */
1324
            /* This may enable/disable the MMU, so do a TLB flush.  */
1325
            tlb_flush(env, 1);
1326
            break;
1327
        case 1: /* Auxiliary cotrol register.  */
1328
            if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1329
                env->cp15.c1_xscaleauxcr = val;
1330
                break;
1331
            }
1332
            /* Not implemented.  */
1333
            break;
1334
        case 2:
1335
            if (arm_feature(env, ARM_FEATURE_XSCALE))
1336
                goto bad_reg;
1337
            if (env->cp15.c1_coproc != val) {
1338
                env->cp15.c1_coproc = val;
1339
                /* ??? Is this safe when called from within a TB?  */
1340
                tb_flush(env);
1341
            }
1342
            break;
1343
        default:
1344
            goto bad_reg;
1345
        }
1346
        break;
1347
    case 2: /* MMU Page table control / MPU cache control.  */
1348
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1349
            switch (op2) {
1350
            case 0:
1351
                env->cp15.c2_data = val;
1352
                break;
1353
            case 1:
1354
                env->cp15.c2_insn = val;
1355
                break;
1356
            default:
1357
                goto bad_reg;
1358
            }
1359
        } else {
1360
            switch (op2) {
1361
            case 0:
1362
                env->cp15.c2_base0 = val;
1363
                break;
1364
            case 1:
1365
                env->cp15.c2_base1 = val;
1366
                break;
1367
            case 2:
1368
                val &= 7;
1369
                env->cp15.c2_control = val;
1370
                env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1371
                env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1372
                break;
1373
            default:
1374
                goto bad_reg;
1375
            }
1376
        }
1377
        break;
1378
    case 3: /* MMU Domain access control / MPU write buffer control.  */
1379
        env->cp15.c3 = val;
1380
        tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1381
        break;
1382
    case 4: /* Reserved.  */
1383
        goto bad_reg;
1384
    case 5: /* MMU Fault status / MPU access permission.  */
1385
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1386
            op2 = 0;
1387
        switch (op2) {
1388
        case 0:
1389
            if (arm_feature(env, ARM_FEATURE_MPU))
1390
                val = extended_mpu_ap_bits(val);
1391
            env->cp15.c5_data = val;
1392
            break;
1393
        case 1:
1394
            if (arm_feature(env, ARM_FEATURE_MPU))
1395
                val = extended_mpu_ap_bits(val);
1396
            env->cp15.c5_insn = val;
1397
            break;
1398
        case 2:
1399
            if (!arm_feature(env, ARM_FEATURE_MPU))
1400
                goto bad_reg;
1401
            env->cp15.c5_data = val;
1402
            break;
1403
        case 3:
1404
            if (!arm_feature(env, ARM_FEATURE_MPU))
1405
                goto bad_reg;
1406
            env->cp15.c5_insn = val;
1407
            break;
1408
        default:
1409
            goto bad_reg;
1410
        }
1411
        break;
1412
    case 6: /* MMU Fault address / MPU base/size.  */
1413
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1414
            if (crm >= 8)
1415
                goto bad_reg;
1416
            env->cp15.c6_region[crm] = val;
1417
        } else {
1418
            if (arm_feature(env, ARM_FEATURE_OMAPCP))
1419
                op2 = 0;
1420
            switch (op2) {
1421
            case 0:
1422
                env->cp15.c6_data = val;
1423
                break;
1424
            case 1: /* ??? This is WFAR on armv6 */
1425
            case 2:
1426
                env->cp15.c6_insn = val;
1427
                break;
1428
            default:
1429
                goto bad_reg;
1430
            }
1431
        }
1432
        break;
1433
    case 7: /* Cache control.  */
1434
        env->cp15.c15_i_max = 0x000;
1435
        env->cp15.c15_i_min = 0xff0;
1436
        /* No cache, so nothing to do.  */
1437
        /* ??? MPCore has VA to PA translation functions.  */
1438
        break;
1439
    case 8: /* MMU TLB control.  */
1440
        switch (op2) {
1441
        case 0: /* Invalidate all.  */
1442
            tlb_flush(env, 0);
1443
            break;
1444
        case 1: /* Invalidate single TLB entry.  */
1445
            tlb_flush_page(env, val & TARGET_PAGE_MASK);
1446
            break;
1447
        case 2: /* Invalidate on ASID.  */
1448
            tlb_flush(env, val == 0);
1449
            break;
1450
        case 3: /* Invalidate single entry on MVA.  */
1451
            /* ??? This is like case 1, but ignores ASID.  */
1452
            tlb_flush(env, 1);
1453
            break;
1454
        default:
1455
            goto bad_reg;
1456
        }
1457
        break;
1458
    case 9:
1459
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1460
            break;
1461
        switch (crm) {
1462
        case 0: /* Cache lockdown.  */
1463
            switch (op1) {
1464
            case 0: /* L1 cache.  */
1465
                switch (op2) {
1466
                case 0:
1467
                    env->cp15.c9_data = val;
1468
                    break;
1469
                case 1:
1470
                    env->cp15.c9_insn = val;
1471
                    break;
1472
                default:
1473
                    goto bad_reg;
1474
                }
1475
                break;
1476
            case 1: /* L2 cache.  */
1477
                /* Ignore writes to L2 lockdown/auxiliary registers.  */
1478
                break;
1479
            default:
1480
                goto bad_reg;
1481
            }
1482
            break;
1483
        case 1: /* TCM memory region registers.  */
1484
            /* Not implemented.  */
1485
            goto bad_reg;
1486
        default:
1487
            goto bad_reg;
1488
        }
1489
        break;
1490
    case 10: /* MMU TLB lockdown.  */
1491
        /* ??? TLB lockdown not implemented.  */
1492
        break;
1493
    case 12: /* Reserved.  */
1494
        goto bad_reg;
1495
    case 13: /* Process ID.  */
1496
        switch (op2) {
1497
        case 0:
1498
            /* Unlike real hardware the qemu TLB uses virtual addresses,
1499
               not modified virtual addresses, so this causes a TLB flush.
1500
             */
1501
            if (env->cp15.c13_fcse != val)
1502
              tlb_flush(env, 1);
1503
            env->cp15.c13_fcse = val;
1504
            break;
1505
        case 1:
1506
            /* This changes the ASID, so do a TLB flush.  */
1507
            if (env->cp15.c13_context != val
1508
                && !arm_feature(env, ARM_FEATURE_MPU))
1509
              tlb_flush(env, 0);
1510
            env->cp15.c13_context = val;
1511
            break;
1512
        default:
1513
            goto bad_reg;
1514
        }
1515
        break;
1516
    case 14: /* Reserved.  */
1517
        goto bad_reg;
1518
    case 15: /* Implementation specific.  */
1519
        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1520
            if (op2 == 0 && crm == 1) {
1521
                if (env->cp15.c15_cpar != (val & 0x3fff)) {
1522
                    /* Changes cp0 to cp13 behavior, so needs a TB flush.  */
1523
                    tb_flush(env);
1524
                    env->cp15.c15_cpar = val & 0x3fff;
1525
                }
1526
                break;
1527
            }
1528
            goto bad_reg;
1529
        }
1530
        if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1531
            switch (crm) {
1532
            case 0:
1533
                break;
1534
            case 1: /* Set TI925T configuration.  */
1535
                env->cp15.c15_ticonfig = val & 0xe7;
1536
                env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1537
                        ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1538
                break;
1539
            case 2: /* Set I_max.  */
1540
                env->cp15.c15_i_max = val;
1541
                break;
1542
            case 3: /* Set I_min.  */
1543
                env->cp15.c15_i_min = val;
1544
                break;
1545
            case 4: /* Set thread-ID.  */
1546
                env->cp15.c15_threadid = val & 0xffff;
1547
                break;
1548
            case 8: /* Wait-for-interrupt (deprecated).  */
1549
                cpu_interrupt(env, CPU_INTERRUPT_HALT);
1550
                break;
1551
            default:
1552
                goto bad_reg;
1553
            }
1554
        }
1555
        break;
1556
    }
1557
    return;
1558
bad_reg:
1559
    /* ??? For debugging only.  Should raise illegal instruction exception.  */
1560
    cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1561
              (insn >> 16) & 0xf, crm, op1, op2);
1562
}
1563

    
1564
uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
1565
{
1566
    int op1;
1567
    int op2;
1568
    int crm;
1569

    
1570
    op1 = (insn >> 21) & 7;
1571
    op2 = (insn >> 5) & 7;
1572
    crm = insn & 0xf;
1573
    switch ((insn >> 16) & 0xf) {
1574
    case 0: /* ID codes.  */
1575
        switch (op1) {
1576
        case 0:
1577
            switch (crm) {
1578
            case 0:
1579
                switch (op2) {
1580
                case 0: /* Device ID.  */
1581
                    return env->cp15.c0_cpuid;
1582
                case 1: /* Cache Type.  */
1583
                    return env->cp15.c0_cachetype;
1584
                case 2: /* TCM status.  */
1585
                    return 0;
1586
                case 3: /* TLB type register.  */
1587
                    return 0; /* No lockable TLB entries.  */
1588
                case 5: /* CPU ID */
1589
                    if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1590
                        return env->cpu_index | 0x80000900;
1591
                    } else {
1592
                        return env->cpu_index;
1593
                    }
1594
                default:
1595
                    goto bad_reg;
1596
                }
1597
            case 1:
1598
                if (!arm_feature(env, ARM_FEATURE_V6))
1599
                    goto bad_reg;
1600
                return env->cp15.c0_c1[op2];
1601
            case 2:
1602
                if (!arm_feature(env, ARM_FEATURE_V6))
1603
                    goto bad_reg;
1604
                return env->cp15.c0_c2[op2];
1605
            case 3: case 4: case 5: case 6: case 7:
1606
                return 0;
1607
            default:
1608
                goto bad_reg;
1609
            }
1610
        case 1:
1611
            /* These registers aren't documented on arm11 cores.  However
1612
               Linux looks at them anyway.  */
1613
            if (!arm_feature(env, ARM_FEATURE_V6))
1614
                goto bad_reg;
1615
            if (crm != 0)
1616
                goto bad_reg;
1617
            if (!arm_feature(env, ARM_FEATURE_V7))
1618
                return 0;
1619

    
1620
            switch (op2) {
1621
            case 0:
1622
                return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1623
            case 1:
1624
                return env->cp15.c0_clid;
1625
            case 7:
1626
                return 0;
1627
            }
1628
            goto bad_reg;
1629
        case 2:
1630
            if (op2 != 0 || crm != 0)
1631
                goto bad_reg;
1632
            return env->cp15.c0_cssel;
1633
        default:
1634
            goto bad_reg;
1635
        }
1636
    case 1: /* System configuration.  */
1637
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1638
            op2 = 0;
1639
        switch (op2) {
1640
        case 0: /* Control register.  */
1641
            return env->cp15.c1_sys;
1642
        case 1: /* Auxiliary control register.  */
1643
            if (arm_feature(env, ARM_FEATURE_XSCALE))
1644
                return env->cp15.c1_xscaleauxcr;
1645
            if (!arm_feature(env, ARM_FEATURE_AUXCR))
1646
                goto bad_reg;
1647
            switch (ARM_CPUID(env)) {
1648
            case ARM_CPUID_ARM1026:
1649
                return 1;
1650
            case ARM_CPUID_ARM1136:
1651
            case ARM_CPUID_ARM1136_R2:
1652
                return 7;
1653
            case ARM_CPUID_ARM11MPCORE:
1654
                return 1;
1655
            case ARM_CPUID_CORTEXA8:
1656
                return 2;
1657
            case ARM_CPUID_CORTEXA9:
1658
                return 0;
1659
            default:
1660
                goto bad_reg;
1661
            }
1662
        case 2: /* Coprocessor access register.  */
1663
            if (arm_feature(env, ARM_FEATURE_XSCALE))
1664
                goto bad_reg;
1665
            return env->cp15.c1_coproc;
1666
        default:
1667
            goto bad_reg;
1668
        }
1669
    case 2: /* MMU Page table control / MPU cache control.  */
1670
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1671
            switch (op2) {
1672
            case 0:
1673
                return env->cp15.c2_data;
1674
                break;
1675
            case 1:
1676
                return env->cp15.c2_insn;
1677
                break;
1678
            default:
1679
                goto bad_reg;
1680
            }
1681
        } else {
1682
            switch (op2) {
1683
            case 0:
1684
                return env->cp15.c2_base0;
1685
            case 1:
1686
                return env->cp15.c2_base1;
1687
            case 2:
1688
                return env->cp15.c2_control;
1689
            default:
1690
                goto bad_reg;
1691
            }
1692
        }
1693
    case 3: /* MMU Domain access control / MPU write buffer control.  */
1694
        return env->cp15.c3;
1695
    case 4: /* Reserved.  */
1696
        goto bad_reg;
1697
    case 5: /* MMU Fault status / MPU access permission.  */
1698
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1699
            op2 = 0;
1700
        switch (op2) {
1701
        case 0:
1702
            if (arm_feature(env, ARM_FEATURE_MPU))
1703
                return simple_mpu_ap_bits(env->cp15.c5_data);
1704
            return env->cp15.c5_data;
1705
        case 1:
1706
            if (arm_feature(env, ARM_FEATURE_MPU))
1707
                return simple_mpu_ap_bits(env->cp15.c5_data);
1708
            return env->cp15.c5_insn;
1709
        case 2:
1710
            if (!arm_feature(env, ARM_FEATURE_MPU))
1711
                goto bad_reg;
1712
            return env->cp15.c5_data;
1713
        case 3:
1714
            if (!arm_feature(env, ARM_FEATURE_MPU))
1715
                goto bad_reg;
1716
            return env->cp15.c5_insn;
1717
        default:
1718
            goto bad_reg;
1719
        }
1720
    case 6: /* MMU Fault address.  */
1721
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1722
            if (crm >= 8)
1723
                goto bad_reg;
1724
            return env->cp15.c6_region[crm];
1725
        } else {
1726
            if (arm_feature(env, ARM_FEATURE_OMAPCP))
1727
                op2 = 0;
1728
            switch (op2) {
1729
            case 0:
1730
                return env->cp15.c6_data;
1731
            case 1:
1732
                if (arm_feature(env, ARM_FEATURE_V6)) {
1733
                    /* Watchpoint Fault Adrress.  */
1734
                    return 0; /* Not implemented.  */
1735
                } else {
1736
                    /* Instruction Fault Adrress.  */
1737
                    /* Arm9 doesn't have an IFAR, but implementing it anyway
1738
                       shouldn't do any harm.  */
1739
                    return env->cp15.c6_insn;
1740
                }
1741
            case 2:
1742
                if (arm_feature(env, ARM_FEATURE_V6)) {
1743
                    /* Instruction Fault Adrress.  */
1744
                    return env->cp15.c6_insn;
1745
                } else {
1746
                    goto bad_reg;
1747
                }
1748
            default:
1749
                goto bad_reg;
1750
            }
1751
        }
1752
    case 7: /* Cache control.  */
1753
        /* FIXME: Should only clear Z flag if destination is r15.  */
1754
        env->ZF = 0;
1755
        return 0;
1756
    case 8: /* MMU TLB control.  */
1757
        goto bad_reg;
1758
    case 9: /* Cache lockdown.  */
1759
        switch (op1) {
1760
        case 0: /* L1 cache.  */
1761
            if (arm_feature(env, ARM_FEATURE_OMAPCP))
1762
                return 0;
1763
            switch (op2) {
1764
            case 0:
1765
                return env->cp15.c9_data;
1766
            case 1:
1767
                return env->cp15.c9_insn;
1768
            default:
1769
                goto bad_reg;
1770
            }
1771
        case 1: /* L2 cache */
1772
            if (crm != 0)
1773
                goto bad_reg;
1774
            /* L2 Lockdown and Auxiliary control.  */
1775
            return 0;
1776
        default:
1777
            goto bad_reg;
1778
        }
1779
    case 10: /* MMU TLB lockdown.  */
1780
        /* ??? TLB lockdown not implemented.  */
1781
        return 0;
1782
    case 11: /* TCM DMA control.  */
1783
    case 12: /* Reserved.  */
1784
        goto bad_reg;
1785
    case 13: /* Process ID.  */
1786
        switch (op2) {
1787
        case 0:
1788
            return env->cp15.c13_fcse;
1789
        case 1:
1790
            return env->cp15.c13_context;
1791
        default:
1792
            goto bad_reg;
1793
        }
1794
    case 14: /* Reserved.  */
1795
        goto bad_reg;
1796
    case 15: /* Implementation specific.  */
1797
        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1798
            if (op2 == 0 && crm == 1)
1799
                return env->cp15.c15_cpar;
1800

    
1801
            goto bad_reg;
1802
        }
1803
        if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1804
            switch (crm) {
1805
            case 0:
1806
                return 0;
1807
            case 1: /* Read TI925T configuration.  */
1808
                return env->cp15.c15_ticonfig;
1809
            case 2: /* Read I_max.  */
1810
                return env->cp15.c15_i_max;
1811
            case 3: /* Read I_min.  */
1812
                return env->cp15.c15_i_min;
1813
            case 4: /* Read thread-ID.  */
1814
                return env->cp15.c15_threadid;
1815
            case 8: /* TI925T_status */
1816
                return 0;
1817
            }
1818
            /* TODO: Peripheral port remap register:
1819
             * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1820
             * controller base address at $rn & ~0xfff and map size of
1821
             * 0x200 << ($rn & 0xfff), when MMU is off.  */
1822
            goto bad_reg;
1823
        }
1824
        return 0;
1825
    }
1826
bad_reg:
1827
    /* ??? For debugging only.  Should raise illegal instruction exception.  */
1828
    cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1829
              (insn >> 16) & 0xf, crm, op1, op2);
1830
    return 0;
1831
}
1832

    
1833
void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
1834
{
1835
    env->banked_r13[bank_number(mode)] = val;
1836
}
1837

    
1838
uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
1839
{
1840
    return env->banked_r13[bank_number(mode)];
1841
}
1842

    
1843
uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
1844
{
1845
    switch (reg) {
1846
    case 0: /* APSR */
1847
        return xpsr_read(env) & 0xf8000000;
1848
    case 1: /* IAPSR */
1849
        return xpsr_read(env) & 0xf80001ff;
1850
    case 2: /* EAPSR */
1851
        return xpsr_read(env) & 0xff00fc00;
1852
    case 3: /* xPSR */
1853
        return xpsr_read(env) & 0xff00fdff;
1854
    case 5: /* IPSR */
1855
        return xpsr_read(env) & 0x000001ff;
1856
    case 6: /* EPSR */
1857
        return xpsr_read(env) & 0x0700fc00;
1858
    case 7: /* IEPSR */
1859
        return xpsr_read(env) & 0x0700edff;
1860
    case 8: /* MSP */
1861
        return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
1862
    case 9: /* PSP */
1863
        return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
1864
    case 16: /* PRIMASK */
1865
        return (env->uncached_cpsr & CPSR_I) != 0;
1866
    case 17: /* FAULTMASK */
1867
        return (env->uncached_cpsr & CPSR_F) != 0;
1868
    case 18: /* BASEPRI */
1869
    case 19: /* BASEPRI_MAX */
1870
        return env->v7m.basepri;
1871
    case 20: /* CONTROL */
1872
        return env->v7m.control;
1873
    default:
1874
        /* ??? For debugging only.  */
1875
        cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
1876
        return 0;
1877
    }
1878
}
1879

    
1880
void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
1881
{
1882
    switch (reg) {
1883
    case 0: /* APSR */
1884
        xpsr_write(env, val, 0xf8000000);
1885
        break;
1886
    case 1: /* IAPSR */
1887
        xpsr_write(env, val, 0xf8000000);
1888
        break;
1889
    case 2: /* EAPSR */
1890
        xpsr_write(env, val, 0xfe00fc00);
1891
        break;
1892
    case 3: /* xPSR */
1893
        xpsr_write(env, val, 0xfe00fc00);
1894
        break;
1895
    case 5: /* IPSR */
1896
        /* IPSR bits are readonly.  */
1897
        break;
1898
    case 6: /* EPSR */
1899
        xpsr_write(env, val, 0x0600fc00);
1900
        break;
1901
    case 7: /* IEPSR */
1902
        xpsr_write(env, val, 0x0600fc00);
1903
        break;
1904
    case 8: /* MSP */
1905
        if (env->v7m.current_sp)
1906
            env->v7m.other_sp = val;
1907
        else
1908
            env->regs[13] = val;
1909
        break;
1910
    case 9: /* PSP */
1911
        if (env->v7m.current_sp)
1912
            env->regs[13] = val;
1913
        else
1914
            env->v7m.other_sp = val;
1915
        break;
1916
    case 16: /* PRIMASK */
1917
        if (val & 1)
1918
            env->uncached_cpsr |= CPSR_I;
1919
        else
1920
            env->uncached_cpsr &= ~CPSR_I;
1921
        break;
1922
    case 17: /* FAULTMASK */
1923
        if (val & 1)
1924
            env->uncached_cpsr |= CPSR_F;
1925
        else
1926
            env->uncached_cpsr &= ~CPSR_F;
1927
        break;
1928
    case 18: /* BASEPRI */
1929
        env->v7m.basepri = val & 0xff;
1930
        break;
1931
    case 19: /* BASEPRI_MAX */
1932
        val &= 0xff;
1933
        if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
1934
            env->v7m.basepri = val;
1935
        break;
1936
    case 20: /* CONTROL */
1937
        env->v7m.control = val & 3;
1938
        switch_v7m_sp(env, (val & 2) != 0);
1939
        break;
1940
    default:
1941
        /* ??? For debugging only.  */
1942
        cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
1943
        return;
1944
    }
1945
}
1946

    
1947
void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
1948
                ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
1949
                void *opaque)
1950
{
1951
    if (cpnum < 0 || cpnum > 14) {
1952
        cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
1953
        return;
1954
    }
1955

    
1956
    env->cp[cpnum].cp_read = cp_read;
1957
    env->cp[cpnum].cp_write = cp_write;
1958
    env->cp[cpnum].opaque = opaque;
1959
}
1960

    
1961
#endif
1962

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

    
1967
/* Signed saturating arithmetic.  */
1968

    
1969
/* Perform 16-bit signed saturating addition.  */
1970
static inline uint16_t add16_sat(uint16_t a, uint16_t b)
1971
{
1972
    uint16_t res;
1973

    
1974
    res = a + b;
1975
    if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
1976
        if (a & 0x8000)
1977
            res = 0x8000;
1978
        else
1979
            res = 0x7fff;
1980
    }
1981
    return res;
1982
}
1983

    
1984
/* Perform 8-bit signed saturating addition.  */
1985
static inline uint8_t add8_sat(uint8_t a, uint8_t b)
1986
{
1987
    uint8_t res;
1988

    
1989
    res = a + b;
1990
    if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
1991
        if (a & 0x80)
1992
            res = 0x80;
1993
        else
1994
            res = 0x7f;
1995
    }
1996
    return res;
1997
}
1998

    
1999
/* Perform 16-bit signed saturating subtraction.  */
2000
static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2001
{
2002
    uint16_t res;
2003

    
2004
    res = a - b;
2005
    if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2006
        if (a & 0x8000)
2007
            res = 0x8000;
2008
        else
2009
            res = 0x7fff;
2010
    }
2011
    return res;
2012
}
2013

    
2014
/* Perform 8-bit signed saturating subtraction.  */
2015
static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2016
{
2017
    uint8_t res;
2018

    
2019
    res = a - b;
2020
    if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2021
        if (a & 0x80)
2022
            res = 0x80;
2023
        else
2024
            res = 0x7f;
2025
    }
2026
    return res;
2027
}
2028

    
2029
#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2030
#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2031
#define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
2032
#define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
2033
#define PFX q
2034

    
2035
#include "op_addsub.h"
2036

    
2037
/* Unsigned saturating arithmetic.  */
2038
static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2039
{
2040
    uint16_t res;
2041
    res = a + b;
2042
    if (res < a)
2043
        res = 0xffff;
2044
    return res;
2045
}
2046

    
2047
static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2048
{
2049
    if (a < b)
2050
        return a - b;
2051
    else
2052
        return 0;
2053
}
2054

    
2055
static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2056
{
2057
    uint8_t res;
2058
    res = a + b;
2059
    if (res < a)
2060
        res = 0xff;
2061
    return res;
2062
}
2063

    
2064
static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2065
{
2066
    if (a < b)
2067
        return a - b;
2068
    else
2069
        return 0;
2070
}
2071

    
2072
#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2073
#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2074
#define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
2075
#define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
2076
#define PFX uq
2077

    
2078
#include "op_addsub.h"
2079

    
2080
/* Signed modulo arithmetic.  */
2081
#define SARITH16(a, b, n, op) do { \
2082
    int32_t sum; \
2083
    sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2084
    RESULT(sum, n, 16); \
2085
    if (sum >= 0) \
2086
        ge |= 3 << (n * 2); \
2087
    } while(0)
2088

    
2089
#define SARITH8(a, b, n, op) do { \
2090
    int32_t sum; \
2091
    sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2092
    RESULT(sum, n, 8); \
2093
    if (sum >= 0) \
2094
        ge |= 1 << n; \
2095
    } while(0)
2096

    
2097

    
2098
#define ADD16(a, b, n) SARITH16(a, b, n, +)
2099
#define SUB16(a, b, n) SARITH16(a, b, n, -)
2100
#define ADD8(a, b, n)  SARITH8(a, b, n, +)
2101
#define SUB8(a, b, n)  SARITH8(a, b, n, -)
2102
#define PFX s
2103
#define ARITH_GE
2104

    
2105
#include "op_addsub.h"
2106

    
2107
/* Unsigned modulo arithmetic.  */
2108
#define ADD16(a, b, n) do { \
2109
    uint32_t sum; \
2110
    sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2111
    RESULT(sum, n, 16); \
2112
    if ((sum >> 16) == 1) \
2113
        ge |= 3 << (n * 2); \
2114
    } while(0)
2115

    
2116
#define ADD8(a, b, n) do { \
2117
    uint32_t sum; \
2118
    sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2119
    RESULT(sum, n, 8); \
2120
    if ((sum >> 8) == 1) \
2121
        ge |= 1 << n; \
2122
    } while(0)
2123

    
2124
#define SUB16(a, b, n) do { \
2125
    uint32_t sum; \
2126
    sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2127
    RESULT(sum, n, 16); \
2128
    if ((sum >> 16) == 0) \
2129
        ge |= 3 << (n * 2); \
2130
    } while(0)
2131

    
2132
#define SUB8(a, b, n) do { \
2133
    uint32_t sum; \
2134
    sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2135
    RESULT(sum, n, 8); \
2136
    if ((sum >> 8) == 0) \
2137
        ge |= 1 << n; \
2138
    } while(0)
2139

    
2140
#define PFX u
2141
#define ARITH_GE
2142

    
2143
#include "op_addsub.h"
2144

    
2145
/* Halved signed arithmetic.  */
2146
#define ADD16(a, b, n) \
2147
  RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2148
#define SUB16(a, b, n) \
2149
  RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2150
#define ADD8(a, b, n) \
2151
  RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2152
#define SUB8(a, b, n) \
2153
  RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2154
#define PFX sh
2155

    
2156
#include "op_addsub.h"
2157

    
2158
/* Halved unsigned arithmetic.  */
2159
#define ADD16(a, b, n) \
2160
  RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2161
#define SUB16(a, b, n) \
2162
  RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2163
#define ADD8(a, b, n) \
2164
  RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2165
#define SUB8(a, b, n) \
2166
  RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2167
#define PFX uh
2168

    
2169
#include "op_addsub.h"
2170

    
2171
static inline uint8_t do_usad(uint8_t a, uint8_t b)
2172
{
2173
    if (a > b)
2174
        return a - b;
2175
    else
2176
        return b - a;
2177
}
2178

    
2179
/* Unsigned sum of absolute byte differences.  */
2180
uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2181
{
2182
    uint32_t sum;
2183
    sum = do_usad(a, b);
2184
    sum += do_usad(a >> 8, b >> 8);
2185
    sum += do_usad(a >> 16, b >>16);
2186
    sum += do_usad(a >> 24, b >> 24);
2187
    return sum;
2188
}
2189

    
2190
/* For ARMv6 SEL instruction.  */
2191
uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2192
{
2193
    uint32_t mask;
2194

    
2195
    mask = 0;
2196
    if (flags & 1)
2197
        mask |= 0xff;
2198
    if (flags & 2)
2199
        mask |= 0xff00;
2200
    if (flags & 4)
2201
        mask |= 0xff0000;
2202
    if (flags & 8)
2203
        mask |= 0xff000000;
2204
    return (a & mask) | (b & ~mask);
2205
}
2206

    
2207
uint32_t HELPER(logicq_cc)(uint64_t val)
2208
{
2209
    return (val >> 32) | (val != 0);
2210
}
2211

    
2212
/* VFP support.  We follow the convention used for VFP instrunctions:
2213
   Single precition routines have a "s" suffix, double precision a
2214
   "d" suffix.  */
2215

    
2216
/* Convert host exception flags to vfp form.  */
2217
static inline int vfp_exceptbits_from_host(int host_bits)
2218
{
2219
    int target_bits = 0;
2220

    
2221
    if (host_bits & float_flag_invalid)
2222
        target_bits |= 1;
2223
    if (host_bits & float_flag_divbyzero)
2224
        target_bits |= 2;
2225
    if (host_bits & float_flag_overflow)
2226
        target_bits |= 4;
2227
    if (host_bits & float_flag_underflow)
2228
        target_bits |= 8;
2229
    if (host_bits & float_flag_inexact)
2230
        target_bits |= 0x10;
2231
    return target_bits;
2232
}
2233

    
2234
uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2235
{
2236
    int i;
2237
    uint32_t fpscr;
2238

    
2239
    fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2240
            | (env->vfp.vec_len << 16)
2241
            | (env->vfp.vec_stride << 20);
2242
    i = get_float_exception_flags(&env->vfp.fp_status);
2243
    fpscr |= vfp_exceptbits_from_host(i);
2244
    return fpscr;
2245
}
2246

    
2247
/* Convert vfp exception flags to target form.  */
2248
static inline int vfp_exceptbits_to_host(int target_bits)
2249
{
2250
    int host_bits = 0;
2251

    
2252
    if (target_bits & 1)
2253
        host_bits |= float_flag_invalid;
2254
    if (target_bits & 2)
2255
        host_bits |= float_flag_divbyzero;
2256
    if (target_bits & 4)
2257
        host_bits |= float_flag_overflow;
2258
    if (target_bits & 8)
2259
        host_bits |= float_flag_underflow;
2260
    if (target_bits & 0x10)
2261
        host_bits |= float_flag_inexact;
2262
    return host_bits;
2263
}
2264

    
2265
void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2266
{
2267
    int i;
2268
    uint32_t changed;
2269

    
2270
    changed = env->vfp.xregs[ARM_VFP_FPSCR];
2271
    env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2272
    env->vfp.vec_len = (val >> 16) & 7;
2273
    env->vfp.vec_stride = (val >> 20) & 3;
2274

    
2275
    changed ^= val;
2276
    if (changed & (3 << 22)) {
2277
        i = (val >> 22) & 3;
2278
        switch (i) {
2279
        case 0:
2280
            i = float_round_nearest_even;
2281
            break;
2282
        case 1:
2283
            i = float_round_up;
2284
            break;
2285
        case 2:
2286
            i = float_round_down;
2287
            break;
2288
        case 3:
2289
            i = float_round_to_zero;
2290
            break;
2291
        }
2292
        set_float_rounding_mode(i, &env->vfp.fp_status);
2293
    }
2294
    if (changed & (1 << 24))
2295
        set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2296
    if (changed & (1 << 25))
2297
        set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2298

    
2299
    i = vfp_exceptbits_to_host((val >> 8) & 0x1f);
2300
    set_float_exception_flags(i, &env->vfp.fp_status);
2301
}
2302

    
2303
#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2304

    
2305
#define VFP_BINOP(name) \
2306
float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2307
{ \
2308
    return float32_ ## name (a, b, &env->vfp.fp_status); \
2309
} \
2310
float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2311
{ \
2312
    return float64_ ## name (a, b, &env->vfp.fp_status); \
2313
}
2314
VFP_BINOP(add)
2315
VFP_BINOP(sub)
2316
VFP_BINOP(mul)
2317
VFP_BINOP(div)
2318
#undef VFP_BINOP
2319

    
2320
float32 VFP_HELPER(neg, s)(float32 a)
2321
{
2322
    return float32_chs(a);
2323
}
2324

    
2325
float64 VFP_HELPER(neg, d)(float64 a)
2326
{
2327
    return float64_chs(a);
2328
}
2329

    
2330
float32 VFP_HELPER(abs, s)(float32 a)
2331
{
2332
    return float32_abs(a);
2333
}
2334

    
2335
float64 VFP_HELPER(abs, d)(float64 a)
2336
{
2337
    return float64_abs(a);
2338
}
2339

    
2340
float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2341
{
2342
    return float32_sqrt(a, &env->vfp.fp_status);
2343
}
2344

    
2345
float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2346
{
2347
    return float64_sqrt(a, &env->vfp.fp_status);
2348
}
2349

    
2350
/* XXX: check quiet/signaling case */
2351
#define DO_VFP_cmp(p, type) \
2352
void VFP_HELPER(cmp, p)(type a, type b, CPUState *env)  \
2353
{ \
2354
    uint32_t flags; \
2355
    switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2356
    case 0: flags = 0x6; break; \
2357
    case -1: flags = 0x8; break; \
2358
    case 1: flags = 0x2; break; \
2359
    default: case 2: flags = 0x3; break; \
2360
    } \
2361
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2362
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2363
} \
2364
void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2365
{ \
2366
    uint32_t flags; \
2367
    switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2368
    case 0: flags = 0x6; break; \
2369
    case -1: flags = 0x8; break; \
2370
    case 1: flags = 0x2; break; \
2371
    default: case 2: flags = 0x3; break; \
2372
    } \
2373
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2374
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2375
}
2376
DO_VFP_cmp(s, float32)
2377
DO_VFP_cmp(d, float64)
2378
#undef DO_VFP_cmp
2379

    
2380
/* Helper routines to perform bitwise copies between float and int.  */
2381
static inline float32 vfp_itos(uint32_t i)
2382
{
2383
    union {
2384
        uint32_t i;
2385
        float32 s;
2386
    } v;
2387

    
2388
    v.i = i;
2389
    return v.s;
2390
}
2391

    
2392
static inline uint32_t vfp_stoi(float32 s)
2393
{
2394
    union {
2395
        uint32_t i;
2396
        float32 s;
2397
    } v;
2398

    
2399
    v.s = s;
2400
    return v.i;
2401
}
2402

    
2403
static inline float64 vfp_itod(uint64_t i)
2404
{
2405
    union {
2406
        uint64_t i;
2407
        float64 d;
2408
    } v;
2409

    
2410
    v.i = i;
2411
    return v.d;
2412
}
2413

    
2414
static inline uint64_t vfp_dtoi(float64 d)
2415
{
2416
    union {
2417
        uint64_t i;
2418
        float64 d;
2419
    } v;
2420

    
2421
    v.d = d;
2422
    return v.i;
2423
}
2424

    
2425
/* Integer to float conversion.  */
2426
float32 VFP_HELPER(uito, s)(float32 x, CPUState *env)
2427
{
2428
    return uint32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2429
}
2430

    
2431
float64 VFP_HELPER(uito, d)(float32 x, CPUState *env)
2432
{
2433
    return uint32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2434
}
2435

    
2436
float32 VFP_HELPER(sito, s)(float32 x, CPUState *env)
2437
{
2438
    return int32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2439
}
2440

    
2441
float64 VFP_HELPER(sito, d)(float32 x, CPUState *env)
2442
{
2443
    return int32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2444
}
2445

    
2446
/* Float to integer conversion.  */
2447
float32 VFP_HELPER(toui, s)(float32 x, CPUState *env)
2448
{
2449
    return vfp_itos(float32_to_uint32(x, &env->vfp.fp_status));
2450
}
2451

    
2452
float32 VFP_HELPER(toui, d)(float64 x, CPUState *env)
2453
{
2454
    return vfp_itos(float64_to_uint32(x, &env->vfp.fp_status));
2455
}
2456

    
2457
float32 VFP_HELPER(tosi, s)(float32 x, CPUState *env)
2458
{
2459
    return vfp_itos(float32_to_int32(x, &env->vfp.fp_status));
2460
}
2461

    
2462
float32 VFP_HELPER(tosi, d)(float64 x, CPUState *env)
2463
{
2464
    return vfp_itos(float64_to_int32(x, &env->vfp.fp_status));
2465
}
2466

    
2467
float32 VFP_HELPER(touiz, s)(float32 x, CPUState *env)
2468
{
2469
    return vfp_itos(float32_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2470
}
2471

    
2472
float32 VFP_HELPER(touiz, d)(float64 x, CPUState *env)
2473
{
2474
    return vfp_itos(float64_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2475
}
2476

    
2477
float32 VFP_HELPER(tosiz, s)(float32 x, CPUState *env)
2478
{
2479
    return vfp_itos(float32_to_int32_round_to_zero(x, &env->vfp.fp_status));
2480
}
2481

    
2482
float32 VFP_HELPER(tosiz, d)(float64 x, CPUState *env)
2483
{
2484
    return vfp_itos(float64_to_int32_round_to_zero(x, &env->vfp.fp_status));
2485
}
2486

    
2487
/* floating point conversion */
2488
float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2489
{
2490
    return float32_to_float64(x, &env->vfp.fp_status);
2491
}
2492

    
2493
float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2494
{
2495
    return float64_to_float32(x, &env->vfp.fp_status);
2496
}
2497

    
2498
/* VFP3 fixed point conversion.  */
2499
#define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2500
ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2501
{ \
2502
    ftype tmp; \
2503
    tmp = sign##int32_to_##ftype ((itype)vfp_##p##toi(x), \
2504
                                  &env->vfp.fp_status); \
2505
    return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
2506
} \
2507
ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2508
{ \
2509
    ftype tmp; \
2510
    tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2511
    return vfp_ito##p((itype)ftype##_to_##sign##int32_round_to_zero(tmp, \
2512
        &env->vfp.fp_status)); \
2513
}
2514

    
2515
VFP_CONV_FIX(sh, d, float64, int16, )
2516
VFP_CONV_FIX(sl, d, float64, int32, )
2517
VFP_CONV_FIX(uh, d, float64, uint16, u)
2518
VFP_CONV_FIX(ul, d, float64, uint32, u)
2519
VFP_CONV_FIX(sh, s, float32, int16, )
2520
VFP_CONV_FIX(sl, s, float32, int32, )
2521
VFP_CONV_FIX(uh, s, float32, uint16, u)
2522
VFP_CONV_FIX(ul, s, float32, uint32, u)
2523
#undef VFP_CONV_FIX
2524

    
2525
/* Half precision conversions.  */
2526
float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2527
{
2528
    float_status *s = &env->vfp.fp_status;
2529
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2530
    return float16_to_float32(a, ieee, s);
2531
}
2532

    
2533
uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2534
{
2535
    float_status *s = &env->vfp.fp_status;
2536
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2537
    return float32_to_float16(a, ieee, s);
2538
}
2539

    
2540
float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2541
{
2542
    float_status *s = &env->vfp.fp_status;
2543
    float32 two = int32_to_float32(2, s);
2544
    return float32_sub(two, float32_mul(a, b, s), s);
2545
}
2546

    
2547
float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2548
{
2549
    float_status *s = &env->vfp.fp_status;
2550
    float32 three = int32_to_float32(3, s);
2551
    return float32_sub(three, float32_mul(a, b, s), s);
2552
}
2553

    
2554
/* NEON helpers.  */
2555

    
2556
/* TODO: The architecture specifies the value that the estimate functions
2557
   should return.  We return the exact reciprocal/root instead.  */
2558
float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2559
{
2560
    float_status *s = &env->vfp.fp_status;
2561
    float32 one = int32_to_float32(1, s);
2562
    return float32_div(one, a, s);
2563
}
2564

    
2565
float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2566
{
2567
    float_status *s = &env->vfp.fp_status;
2568
    float32 one = int32_to_float32(1, s);
2569
    return float32_div(one, float32_sqrt(a, s), s);
2570
}
2571

    
2572
uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
2573
{
2574
    float_status *s = &env->vfp.fp_status;
2575
    float32 tmp;
2576
    tmp = int32_to_float32(a, s);
2577
    tmp = float32_scalbn(tmp, -32, s);
2578
    tmp = helper_recpe_f32(tmp, env);
2579
    tmp = float32_scalbn(tmp, 31, s);
2580
    return float32_to_int32(tmp, s);
2581
}
2582

    
2583
uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
2584
{
2585
    float_status *s = &env->vfp.fp_status;
2586
    float32 tmp;
2587
    tmp = int32_to_float32(a, s);
2588
    tmp = float32_scalbn(tmp, -32, s);
2589
    tmp = helper_rsqrte_f32(tmp, env);
2590
    tmp = float32_scalbn(tmp, 31, s);
2591
    return float32_to_int32(tmp, s);
2592
}
2593

    
2594
void HELPER(set_teecr)(CPUState *env, uint32_t val)
2595
{
2596
    val &= 1;
2597
    if (env->teecr != val) {
2598
        env->teecr = val;
2599
        tb_flush(env);
2600
    }
2601
}