Statistics
| Branch: | Revision:

root / target-arm / helper.c @ 10055562

History | View | Annotate | Download (73.4 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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
197
    id = env->cp15.c0_cpuid;
198
    memset(env, 0, offsetof(CPUARMState, breakpoints));
199
    if (id)
200
        cpu_reset_model_id(env, id);
201
#if defined (CONFIG_USER_ONLY)
202
    env->uncached_cpsr = ARM_CPU_MODE_USR;
203
    env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
204
#else
205
    /* SVC mode with interrupts disabled.  */
206
    env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
207
    /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
208
       clear at reset.  */
209
    if (IS_M(env))
210
        env->uncached_cpsr &= ~CPSR_I;
211
    env->vfp.xregs[ARM_VFP_FPEXC] = 0;
212
    env->cp15.c2_base_mask = 0xffffc000u;
213
#endif
214
    env->regs[15] = 0;
215
    tlb_flush(env, 1);
216
}
217

    
218
static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
219
{
220
    int nregs;
221

    
222
    /* VFP data registers are always little-endian.  */
223
    nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
224
    if (reg < nregs) {
225
        stfq_le_p(buf, env->vfp.regs[reg]);
226
        return 8;
227
    }
228
    if (arm_feature(env, ARM_FEATURE_NEON)) {
229
        /* Aliases for Q regs.  */
230
        nregs += 16;
231
        if (reg < nregs) {
232
            stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
233
            stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
234
            return 16;
235
        }
236
    }
237
    switch (reg - nregs) {
238
    case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
239
    case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
240
    case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
241
    }
242
    return 0;
243
}
244

    
245
static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
246
{
247
    int nregs;
248

    
249
    nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
250
    if (reg < nregs) {
251
        env->vfp.regs[reg] = ldfq_le_p(buf);
252
        return 8;
253
    }
254
    if (arm_feature(env, ARM_FEATURE_NEON)) {
255
        nregs += 16;
256
        if (reg < nregs) {
257
            env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
258
            env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
259
            return 16;
260
        }
261
    }
262
    switch (reg - nregs) {
263
    case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
264
    case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
265
    case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
266
    }
267
    return 0;
268
}
269

    
270
CPUARMState *cpu_arm_init(const char *cpu_model)
271
{
272
    CPUARMState *env;
273
    uint32_t id;
274
    static int inited = 0;
275

    
276
    id = cpu_arm_find_by_name(cpu_model);
277
    if (id == 0)
278
        return NULL;
279
    env = qemu_mallocz(sizeof(CPUARMState));
280
    cpu_exec_init(env);
281
    if (!inited) {
282
        inited = 1;
283
        arm_translate_init();
284
    }
285

    
286
    env->cpu_model_str = cpu_model;
287
    env->cp15.c0_cpuid = id;
288
    cpu_reset(env);
289
    if (arm_feature(env, ARM_FEATURE_NEON)) {
290
        gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
291
                                 51, "arm-neon.xml", 0);
292
    } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
293
        gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
294
                                 35, "arm-vfp3.xml", 0);
295
    } else if (arm_feature(env, ARM_FEATURE_VFP)) {
296
        gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
297
                                 19, "arm-vfp.xml", 0);
298
    }
299
    qemu_init_vcpu(env);
300
    return env;
301
}
302

    
303
struct arm_cpu_t {
304
    uint32_t id;
305
    const char *name;
306
};
307

    
308
static const struct arm_cpu_t arm_cpu_names[] = {
309
    { ARM_CPUID_ARM926, "arm926"},
310
    { ARM_CPUID_ARM946, "arm946"},
311
    { ARM_CPUID_ARM1026, "arm1026"},
312
    { ARM_CPUID_ARM1136, "arm1136"},
313
    { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
314
    { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
315
    { ARM_CPUID_CORTEXM3, "cortex-m3"},
316
    { ARM_CPUID_CORTEXA8, "cortex-a8"},
317
    { ARM_CPUID_CORTEXA9, "cortex-a9"},
318
    { ARM_CPUID_TI925T, "ti925t" },
319
    { ARM_CPUID_PXA250, "pxa250" },
320
    { ARM_CPUID_PXA255, "pxa255" },
321
    { ARM_CPUID_PXA260, "pxa260" },
322
    { ARM_CPUID_PXA261, "pxa261" },
323
    { ARM_CPUID_PXA262, "pxa262" },
324
    { ARM_CPUID_PXA270, "pxa270" },
325
    { ARM_CPUID_PXA270_A0, "pxa270-a0" },
326
    { ARM_CPUID_PXA270_A1, "pxa270-a1" },
327
    { ARM_CPUID_PXA270_B0, "pxa270-b0" },
328
    { ARM_CPUID_PXA270_B1, "pxa270-b1" },
329
    { ARM_CPUID_PXA270_C0, "pxa270-c0" },
330
    { ARM_CPUID_PXA270_C5, "pxa270-c5" },
331
    { ARM_CPUID_ANY, "any"},
332
    { 0, NULL}
333
};
334

    
335
void arm_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
336
{
337
    int i;
338

    
339
    (*cpu_fprintf)(f, "Available CPUs:\n");
340
    for (i = 0; arm_cpu_names[i].name; i++) {
341
        (*cpu_fprintf)(f, "  %s\n", arm_cpu_names[i].name);
342
    }
343
}
344

    
345
/* return 0 if not found */
346
static uint32_t cpu_arm_find_by_name(const char *name)
347
{
348
    int i;
349
    uint32_t id;
350

    
351
    id = 0;
352
    for (i = 0; arm_cpu_names[i].name; i++) {
353
        if (strcmp(name, arm_cpu_names[i].name) == 0) {
354
            id = arm_cpu_names[i].id;
355
            break;
356
        }
357
    }
358
    return id;
359
}
360

    
361
void cpu_arm_close(CPUARMState *env)
362
{
363
    free(env);
364
}
365

    
366
uint32_t cpsr_read(CPUARMState *env)
367
{
368
    int ZF;
369
    ZF = (env->ZF == 0);
370
    return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
371
        (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
372
        | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
373
        | ((env->condexec_bits & 0xfc) << 8)
374
        | (env->GE << 16);
375
}
376

    
377
void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
378
{
379
    if (mask & CPSR_NZCV) {
380
        env->ZF = (~val) & CPSR_Z;
381
        env->NF = val;
382
        env->CF = (val >> 29) & 1;
383
        env->VF = (val << 3) & 0x80000000;
384
    }
385
    if (mask & CPSR_Q)
386
        env->QF = ((val & CPSR_Q) != 0);
387
    if (mask & CPSR_T)
388
        env->thumb = ((val & CPSR_T) != 0);
389
    if (mask & CPSR_IT_0_1) {
390
        env->condexec_bits &= ~3;
391
        env->condexec_bits |= (val >> 25) & 3;
392
    }
393
    if (mask & CPSR_IT_2_7) {
394
        env->condexec_bits &= 3;
395
        env->condexec_bits |= (val >> 8) & 0xfc;
396
    }
397
    if (mask & CPSR_GE) {
398
        env->GE = (val >> 16) & 0xf;
399
    }
400

    
401
    if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
402
        switch_mode(env, val & CPSR_M);
403
    }
404
    mask &= ~CACHED_CPSR_BITS;
405
    env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
406
}
407

    
408
/* Sign/zero extend */
409
uint32_t HELPER(sxtb16)(uint32_t x)
410
{
411
    uint32_t res;
412
    res = (uint16_t)(int8_t)x;
413
    res |= (uint32_t)(int8_t)(x >> 16) << 16;
414
    return res;
415
}
416

    
417
uint32_t HELPER(uxtb16)(uint32_t x)
418
{
419
    uint32_t res;
420
    res = (uint16_t)(uint8_t)x;
421
    res |= (uint32_t)(uint8_t)(x >> 16) << 16;
422
    return res;
423
}
424

    
425
uint32_t HELPER(clz)(uint32_t x)
426
{
427
    return clz32(x);
428
}
429

    
430
int32_t HELPER(sdiv)(int32_t num, int32_t den)
431
{
432
    if (den == 0)
433
      return 0;
434
    if (num == INT_MIN && den == -1)
435
      return INT_MIN;
436
    return num / den;
437
}
438

    
439
uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
440
{
441
    if (den == 0)
442
      return 0;
443
    return num / den;
444
}
445

    
446
uint32_t HELPER(rbit)(uint32_t x)
447
{
448
    x =  ((x & 0xff000000) >> 24)
449
       | ((x & 0x00ff0000) >> 8)
450
       | ((x & 0x0000ff00) << 8)
451
       | ((x & 0x000000ff) << 24);
452
    x =  ((x & 0xf0f0f0f0) >> 4)
453
       | ((x & 0x0f0f0f0f) << 4);
454
    x =  ((x & 0x88888888) >> 3)
455
       | ((x & 0x44444444) >> 1)
456
       | ((x & 0x22222222) << 1)
457
       | ((x & 0x11111111) << 3);
458
    return x;
459
}
460

    
461
uint32_t HELPER(abs)(uint32_t x)
462
{
463
    return ((int32_t)x < 0) ? -x : x;
464
}
465

    
466
#if defined(CONFIG_USER_ONLY)
467

    
468
void do_interrupt (CPUState *env)
469
{
470
    env->exception_index = -1;
471
}
472

    
473
/* Structure used to record exclusive memory locations.  */
474
typedef struct mmon_state {
475
    struct mmon_state *next;
476
    CPUARMState *cpu_env;
477
    uint32_t addr;
478
} mmon_state;
479

    
480
/* Chain of current locks.  */
481
static mmon_state* mmon_head = NULL;
482

    
483
int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
484
                              int mmu_idx, int is_softmmu)
485
{
486
    if (rw == 2) {
487
        env->exception_index = EXCP_PREFETCH_ABORT;
488
        env->cp15.c6_insn = address;
489
    } else {
490
        env->exception_index = EXCP_DATA_ABORT;
491
        env->cp15.c6_data = address;
492
    }
493
    return 1;
494
}
495

    
496
static void allocate_mmon_state(CPUState *env)
497
{
498
    env->mmon_entry = malloc(sizeof (mmon_state));
499
    memset (env->mmon_entry, 0, sizeof (mmon_state));
500
    env->mmon_entry->cpu_env = env;
501
    mmon_head = env->mmon_entry;
502
}
503

    
504
/* Flush any monitor locks for the specified address.  */
505
static void flush_mmon(uint32_t addr)
506
{
507
    mmon_state *mon;
508

    
509
    for (mon = mmon_head; mon; mon = mon->next)
510
      {
511
        if (mon->addr != addr)
512
          continue;
513

    
514
        mon->addr = 0;
515
        break;
516
      }
517
}
518

    
519
/* Mark an address for exclusive access.  */
520
void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
521
{
522
    if (!env->mmon_entry)
523
        allocate_mmon_state(env);
524
    /* Clear any previous locks.  */
525
    flush_mmon(addr);
526
    env->mmon_entry->addr = addr;
527
}
528

    
529
/* Test if an exclusive address is still exclusive.  Returns zero
530
   if the address is still exclusive.   */
531
uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
532
{
533
    int res;
534

    
535
    if (!env->mmon_entry)
536
        return 1;
537
    if (env->mmon_entry->addr == addr)
538
        res = 0;
539
    else
540
        res = 1;
541
    flush_mmon(addr);
542
    return res;
543
}
544

    
545
void HELPER(clrex)(CPUState *env)
546
{
547
    if (!(env->mmon_entry && env->mmon_entry->addr))
548
        return;
549
    flush_mmon(env->mmon_entry->addr);
550
}
551

    
552
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
553
{
554
    return addr;
555
}
556

    
557
/* These should probably raise undefined insn exceptions.  */
558
void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
559
{
560
    int op1 = (insn >> 8) & 0xf;
561
    cpu_abort(env, "cp%i insn %08x\n", op1, insn);
562
    return;
563
}
564

    
565
uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
566
{
567
    int op1 = (insn >> 8) & 0xf;
568
    cpu_abort(env, "cp%i insn %08x\n", op1, insn);
569
    return 0;
570
}
571

    
572
void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
573
{
574
    cpu_abort(env, "cp15 insn %08x\n", insn);
575
}
576

    
577
uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
578
{
579
    cpu_abort(env, "cp15 insn %08x\n", insn);
580
    return 0;
581
}
582

    
583
/* These should probably raise undefined insn exceptions.  */
584
void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
585
{
586
    cpu_abort(env, "v7m_mrs %d\n", reg);
587
}
588

    
589
uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
590
{
591
    cpu_abort(env, "v7m_mrs %d\n", reg);
592
    return 0;
593
}
594

    
595
void switch_mode(CPUState *env, int mode)
596
{
597
    if (mode != ARM_CPU_MODE_USR)
598
        cpu_abort(env, "Tried to switch out of user mode\n");
599
}
600

    
601
void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
602
{
603
    cpu_abort(env, "banked r13 write\n");
604
}
605

    
606
uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
607
{
608
    cpu_abort(env, "banked r13 read\n");
609
    return 0;
610
}
611

    
612
#else
613

    
614
extern int semihosting_enabled;
615

    
616
/* Map CPU modes onto saved register banks.  */
617
static inline int bank_number (int mode)
618
{
619
    switch (mode) {
620
    case ARM_CPU_MODE_USR:
621
    case ARM_CPU_MODE_SYS:
622
        return 0;
623
    case ARM_CPU_MODE_SVC:
624
        return 1;
625
    case ARM_CPU_MODE_ABT:
626
        return 2;
627
    case ARM_CPU_MODE_UND:
628
        return 3;
629
    case ARM_CPU_MODE_IRQ:
630
        return 4;
631
    case ARM_CPU_MODE_FIQ:
632
        return 5;
633
    }
634
    cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
635
    return -1;
636
}
637

    
638
void switch_mode(CPUState *env, int mode)
639
{
640
    int old_mode;
641
    int i;
642

    
643
    old_mode = env->uncached_cpsr & CPSR_M;
644
    if (mode == old_mode)
645
        return;
646

    
647
    if (old_mode == ARM_CPU_MODE_FIQ) {
648
        memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
649
        memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
650
    } else if (mode == ARM_CPU_MODE_FIQ) {
651
        memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
652
        memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
653
    }
654

    
655
    i = bank_number(old_mode);
656
    env->banked_r13[i] = env->regs[13];
657
    env->banked_r14[i] = env->regs[14];
658
    env->banked_spsr[i] = env->spsr;
659

    
660
    i = bank_number(mode);
661
    env->regs[13] = env->banked_r13[i];
662
    env->regs[14] = env->banked_r14[i];
663
    env->spsr = env->banked_spsr[i];
664
}
665

    
666
static void v7m_push(CPUARMState *env, uint32_t val)
667
{
668
    env->regs[13] -= 4;
669
    stl_phys(env->regs[13], val);
670
}
671

    
672
static uint32_t v7m_pop(CPUARMState *env)
673
{
674
    uint32_t val;
675
    val = ldl_phys(env->regs[13]);
676
    env->regs[13] += 4;
677
    return val;
678
}
679

    
680
/* Switch to V7M main or process stack pointer.  */
681
static void switch_v7m_sp(CPUARMState *env, int process)
682
{
683
    uint32_t tmp;
684
    if (env->v7m.current_sp != process) {
685
        tmp = env->v7m.other_sp;
686
        env->v7m.other_sp = env->regs[13];
687
        env->regs[13] = tmp;
688
        env->v7m.current_sp = process;
689
    }
690
}
691

    
692
static void do_v7m_exception_exit(CPUARMState *env)
693
{
694
    uint32_t type;
695
    uint32_t xpsr;
696

    
697
    type = env->regs[15];
698
    if (env->v7m.exception != 0)
699
        armv7m_nvic_complete_irq(env->v7m.nvic, env->v7m.exception);
700

    
701
    /* Switch to the target stack.  */
702
    switch_v7m_sp(env, (type & 4) != 0);
703
    /* Pop registers.  */
704
    env->regs[0] = v7m_pop(env);
705
    env->regs[1] = v7m_pop(env);
706
    env->regs[2] = v7m_pop(env);
707
    env->regs[3] = v7m_pop(env);
708
    env->regs[12] = v7m_pop(env);
709
    env->regs[14] = v7m_pop(env);
710
    env->regs[15] = v7m_pop(env);
711
    xpsr = v7m_pop(env);
712
    xpsr_write(env, xpsr, 0xfffffdff);
713
    /* Undo stack alignment.  */
714
    if (xpsr & 0x200)
715
        env->regs[13] |= 4;
716
    /* ??? The exception return type specifies Thread/Handler mode.  However
717
       this is also implied by the xPSR value. Not sure what to do
718
       if there is a mismatch.  */
719
    /* ??? Likewise for mismatches between the CONTROL register and the stack
720
       pointer.  */
721
}
722

    
723
static void do_interrupt_v7m(CPUARMState *env)
724
{
725
    uint32_t xpsr = xpsr_read(env);
726
    uint32_t lr;
727
    uint32_t addr;
728

    
729
    lr = 0xfffffff1;
730
    if (env->v7m.current_sp)
731
        lr |= 4;
732
    if (env->v7m.exception == 0)
733
        lr |= 8;
734

    
735
    /* For exceptions we just mark as pending on the NVIC, and let that
736
       handle it.  */
737
    /* TODO: Need to escalate if the current priority is higher than the
738
       one we're raising.  */
739
    switch (env->exception_index) {
740
    case EXCP_UDEF:
741
        armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_USAGE);
742
        return;
743
    case EXCP_SWI:
744
        env->regs[15] += 2;
745
        armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_SVC);
746
        return;
747
    case EXCP_PREFETCH_ABORT:
748
    case EXCP_DATA_ABORT:
749
        armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_MEM);
750
        return;
751
    case EXCP_BKPT:
752
        if (semihosting_enabled) {
753
            int nr;
754
            nr = lduw_code(env->regs[15]) & 0xff;
755
            if (nr == 0xab) {
756
                env->regs[15] += 2;
757
                env->regs[0] = do_arm_semihosting(env);
758
                return;
759
            }
760
        }
761
        armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_DEBUG);
762
        return;
763
    case EXCP_IRQ:
764
        env->v7m.exception = armv7m_nvic_acknowledge_irq(env->v7m.nvic);
765
        break;
766
    case EXCP_EXCEPTION_EXIT:
767
        do_v7m_exception_exit(env);
768
        return;
769
    default:
770
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
771
        return; /* Never happens.  Keep compiler happy.  */
772
    }
773

    
774
    /* Align stack pointer.  */
775
    /* ??? Should only do this if Configuration Control Register
776
       STACKALIGN bit is set.  */
777
    if (env->regs[13] & 4) {
778
        env->regs[13] -= 4;
779
        xpsr |= 0x200;
780
    }
781
    /* Switch to the handler mode.  */
782
    v7m_push(env, xpsr);
783
    v7m_push(env, env->regs[15]);
784
    v7m_push(env, env->regs[14]);
785
    v7m_push(env, env->regs[12]);
786
    v7m_push(env, env->regs[3]);
787
    v7m_push(env, env->regs[2]);
788
    v7m_push(env, env->regs[1]);
789
    v7m_push(env, env->regs[0]);
790
    switch_v7m_sp(env, 0);
791
    env->uncached_cpsr &= ~CPSR_IT;
792
    env->regs[14] = lr;
793
    addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
794
    env->regs[15] = addr & 0xfffffffe;
795
    env->thumb = addr & 1;
796
}
797

    
798
/* Handle a CPU exception.  */
799
void do_interrupt(CPUARMState *env)
800
{
801
    uint32_t addr;
802
    uint32_t mask;
803
    int new_mode;
804
    uint32_t offset;
805

    
806
    if (IS_M(env)) {
807
        do_interrupt_v7m(env);
808
        return;
809
    }
810
    /* TODO: Vectored interrupt controller.  */
811
    switch (env->exception_index) {
812
    case EXCP_UDEF:
813
        new_mode = ARM_CPU_MODE_UND;
814
        addr = 0x04;
815
        mask = CPSR_I;
816
        if (env->thumb)
817
            offset = 2;
818
        else
819
            offset = 4;
820
        break;
821
    case EXCP_SWI:
822
        if (semihosting_enabled) {
823
            /* Check for semihosting interrupt.  */
824
            if (env->thumb) {
825
                mask = lduw_code(env->regs[15] - 2) & 0xff;
826
            } else {
827
                mask = ldl_code(env->regs[15] - 4) & 0xffffff;
828
            }
829
            /* Only intercept calls from privileged modes, to provide some
830
               semblance of security.  */
831
            if (((mask == 0x123456 && !env->thumb)
832
                    || (mask == 0xab && env->thumb))
833
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
834
                env->regs[0] = do_arm_semihosting(env);
835
                return;
836
            }
837
        }
838
        new_mode = ARM_CPU_MODE_SVC;
839
        addr = 0x08;
840
        mask = CPSR_I;
841
        /* The PC already points to the next instruction.  */
842
        offset = 0;
843
        break;
844
    case EXCP_BKPT:
845
        /* See if this is a semihosting syscall.  */
846
        if (env->thumb && semihosting_enabled) {
847
            mask = lduw_code(env->regs[15]) & 0xff;
848
            if (mask == 0xab
849
                  && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
850
                env->regs[15] += 2;
851
                env->regs[0] = do_arm_semihosting(env);
852
                return;
853
            }
854
        }
855
        /* Fall through to prefetch abort.  */
856
    case EXCP_PREFETCH_ABORT:
857
        new_mode = ARM_CPU_MODE_ABT;
858
        addr = 0x0c;
859
        mask = CPSR_A | CPSR_I;
860
        offset = 4;
861
        break;
862
    case EXCP_DATA_ABORT:
863
        new_mode = ARM_CPU_MODE_ABT;
864
        addr = 0x10;
865
        mask = CPSR_A | CPSR_I;
866
        offset = 8;
867
        break;
868
    case EXCP_IRQ:
869
        new_mode = ARM_CPU_MODE_IRQ;
870
        addr = 0x18;
871
        /* Disable IRQ and imprecise data aborts.  */
872
        mask = CPSR_A | CPSR_I;
873
        offset = 4;
874
        break;
875
    case EXCP_FIQ:
876
        new_mode = ARM_CPU_MODE_FIQ;
877
        addr = 0x1c;
878
        /* Disable FIQ, IRQ and imprecise data aborts.  */
879
        mask = CPSR_A | CPSR_I | CPSR_F;
880
        offset = 4;
881
        break;
882
    default:
883
        cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
884
        return; /* Never happens.  Keep compiler happy.  */
885
    }
886
    /* High vectors.  */
887
    if (env->cp15.c1_sys & (1 << 13)) {
888
        addr += 0xffff0000;
889
    }
890
    switch_mode (env, new_mode);
891
    env->spsr = cpsr_read(env);
892
    /* Clear IT bits.  */
893
    env->condexec_bits = 0;
894
    /* Switch to the new mode, and switch to Arm mode.  */
895
    /* ??? Thumb interrupt handlers not implemented.  */
896
    env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
897
    env->uncached_cpsr |= mask;
898
    env->thumb = 0;
899
    env->regs[14] = env->regs[15] + offset;
900
    env->regs[15] = addr;
901
    env->interrupt_request |= CPU_INTERRUPT_EXITTB;
902
}
903

    
904
/* Check section/page access permissions.
905
   Returns the page protection flags, or zero if the access is not
906
   permitted.  */
907
static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
908
                           int is_user)
909
{
910
  int prot_ro;
911

    
912
  if (domain == 3)
913
    return PAGE_READ | PAGE_WRITE;
914

    
915
  if (access_type == 1)
916
      prot_ro = 0;
917
  else
918
      prot_ro = PAGE_READ;
919

    
920
  switch (ap) {
921
  case 0:
922
      if (access_type == 1)
923
          return 0;
924
      switch ((env->cp15.c1_sys >> 8) & 3) {
925
      case 1:
926
          return is_user ? 0 : PAGE_READ;
927
      case 2:
928
          return PAGE_READ;
929
      default:
930
          return 0;
931
      }
932
  case 1:
933
      return is_user ? 0 : PAGE_READ | PAGE_WRITE;
934
  case 2:
935
      if (is_user)
936
          return prot_ro;
937
      else
938
          return PAGE_READ | PAGE_WRITE;
939
  case 3:
940
      return PAGE_READ | PAGE_WRITE;
941
  case 4: /* Reserved.  */
942
      return 0;
943
  case 5:
944
      return is_user ? 0 : prot_ro;
945
  case 6:
946
      return prot_ro;
947
  case 7:
948
      if (!arm_feature (env, ARM_FEATURE_V7))
949
          return 0;
950
      return prot_ro;
951
  default:
952
      abort();
953
  }
954
}
955

    
956
static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
957
{
958
    uint32_t table;
959

    
960
    if (address & env->cp15.c2_mask)
961
        table = env->cp15.c2_base1 & 0xffffc000;
962
    else
963
        table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
964

    
965
    table |= (address >> 18) & 0x3ffc;
966
    return table;
967
}
968

    
969
static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
970
                            int is_user, uint32_t *phys_ptr, int *prot)
971
{
972
    int code;
973
    uint32_t table;
974
    uint32_t desc;
975
    int type;
976
    int ap;
977
    int domain;
978
    uint32_t phys_addr;
979

    
980
    /* Pagetable walk.  */
981
    /* Lookup l1 descriptor.  */
982
    table = get_level1_table_address(env, address);
983
    desc = ldl_phys(table);
984
    type = (desc & 3);
985
    domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
986
    if (type == 0) {
987
        /* Section translation fault.  */
988
        code = 5;
989
        goto do_fault;
990
    }
991
    if (domain == 0 || domain == 2) {
992
        if (type == 2)
993
            code = 9; /* Section domain fault.  */
994
        else
995
            code = 11; /* Page domain fault.  */
996
        goto do_fault;
997
    }
998
    if (type == 2) {
999
        /* 1Mb section.  */
1000
        phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1001
        ap = (desc >> 10) & 3;
1002
        code = 13;
1003
    } else {
1004
        /* Lookup l2 entry.  */
1005
        if (type == 1) {
1006
            /* Coarse pagetable.  */
1007
            table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1008
        } else {
1009
            /* Fine pagetable.  */
1010
            table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1011
        }
1012
        desc = ldl_phys(table);
1013
        switch (desc & 3) {
1014
        case 0: /* Page translation fault.  */
1015
            code = 7;
1016
            goto do_fault;
1017
        case 1: /* 64k page.  */
1018
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1019
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1020
            break;
1021
        case 2: /* 4k page.  */
1022
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1023
            ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1024
            break;
1025
        case 3: /* 1k page.  */
1026
            if (type == 1) {
1027
                if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1028
                    phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1029
                } else {
1030
                    /* Page translation fault.  */
1031
                    code = 7;
1032
                    goto do_fault;
1033
                }
1034
            } else {
1035
                phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1036
            }
1037
            ap = (desc >> 4) & 3;
1038
            break;
1039
        default:
1040
            /* Never happens, but compiler isn't smart enough to tell.  */
1041
            abort();
1042
        }
1043
        code = 15;
1044
    }
1045
    *prot = check_ap(env, ap, domain, access_type, is_user);
1046
    if (!*prot) {
1047
        /* Access permission fault.  */
1048
        goto do_fault;
1049
    }
1050
    *phys_ptr = phys_addr;
1051
    return 0;
1052
do_fault:
1053
    return code | (domain << 4);
1054
}
1055

    
1056
static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
1057
                            int is_user, uint32_t *phys_ptr, int *prot)
1058
{
1059
    int code;
1060
    uint32_t table;
1061
    uint32_t desc;
1062
    uint32_t xn;
1063
    int type;
1064
    int ap;
1065
    int domain;
1066
    uint32_t phys_addr;
1067

    
1068
    /* Pagetable walk.  */
1069
    /* Lookup l1 descriptor.  */
1070
    table = get_level1_table_address(env, address);
1071
    desc = ldl_phys(table);
1072
    type = (desc & 3);
1073
    if (type == 0) {
1074
        /* Section translation fault.  */
1075
        code = 5;
1076
        domain = 0;
1077
        goto do_fault;
1078
    } else if (type == 2 && (desc & (1 << 18))) {
1079
        /* Supersection.  */
1080
        domain = 0;
1081
    } else {
1082
        /* Section or page.  */
1083
        domain = (desc >> 4) & 0x1e;
1084
    }
1085
    domain = (env->cp15.c3 >> domain) & 3;
1086
    if (domain == 0 || domain == 2) {
1087
        if (type == 2)
1088
            code = 9; /* Section domain fault.  */
1089
        else
1090
            code = 11; /* Page domain fault.  */
1091
        goto do_fault;
1092
    }
1093
    if (type == 2) {
1094
        if (desc & (1 << 18)) {
1095
            /* Supersection.  */
1096
            phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1097
        } else {
1098
            /* Section.  */
1099
            phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1100
        }
1101
        ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1102
        xn = desc & (1 << 4);
1103
        code = 13;
1104
    } else {
1105
        /* Lookup l2 entry.  */
1106
        table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1107
        desc = ldl_phys(table);
1108
        ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1109
        switch (desc & 3) {
1110
        case 0: /* Page translation fault.  */
1111
            code = 7;
1112
            goto do_fault;
1113
        case 1: /* 64k page.  */
1114
            phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1115
            xn = desc & (1 << 15);
1116
            break;
1117
        case 2: case 3: /* 4k page.  */
1118
            phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1119
            xn = desc & 1;
1120
            break;
1121
        default:
1122
            /* Never happens, but compiler isn't smart enough to tell.  */
1123
            abort();
1124
        }
1125
        code = 15;
1126
    }
1127
    if (xn && access_type == 2)
1128
        goto do_fault;
1129

    
1130
    /* The simplified model uses AP[0] as an access control bit.  */
1131
    if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1132
        /* Access flag fault.  */
1133
        code = (code == 15) ? 6 : 3;
1134
        goto do_fault;
1135
    }
1136
    *prot = check_ap(env, ap, domain, access_type, is_user);
1137
    if (!*prot) {
1138
        /* Access permission fault.  */
1139
        goto do_fault;
1140
    }
1141
    *phys_ptr = phys_addr;
1142
    return 0;
1143
do_fault:
1144
    return code | (domain << 4);
1145
}
1146

    
1147
static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1148
                             int is_user, uint32_t *phys_ptr, int *prot)
1149
{
1150
    int n;
1151
    uint32_t mask;
1152
    uint32_t base;
1153

    
1154
    *phys_ptr = address;
1155
    for (n = 7; n >= 0; n--) {
1156
        base = env->cp15.c6_region[n];
1157
        if ((base & 1) == 0)
1158
            continue;
1159
        mask = 1 << ((base >> 1) & 0x1f);
1160
        /* Keep this shift separate from the above to avoid an
1161
           (undefined) << 32.  */
1162
        mask = (mask << 1) - 1;
1163
        if (((base ^ address) & ~mask) == 0)
1164
            break;
1165
    }
1166
    if (n < 0)
1167
        return 2;
1168

    
1169
    if (access_type == 2) {
1170
        mask = env->cp15.c5_insn;
1171
    } else {
1172
        mask = env->cp15.c5_data;
1173
    }
1174
    mask = (mask >> (n * 4)) & 0xf;
1175
    switch (mask) {
1176
    case 0:
1177
        return 1;
1178
    case 1:
1179
        if (is_user)
1180
          return 1;
1181
        *prot = PAGE_READ | PAGE_WRITE;
1182
        break;
1183
    case 2:
1184
        *prot = PAGE_READ;
1185
        if (!is_user)
1186
            *prot |= PAGE_WRITE;
1187
        break;
1188
    case 3:
1189
        *prot = PAGE_READ | PAGE_WRITE;
1190
        break;
1191
    case 5:
1192
        if (is_user)
1193
            return 1;
1194
        *prot = PAGE_READ;
1195
        break;
1196
    case 6:
1197
        *prot = PAGE_READ;
1198
        break;
1199
    default:
1200
        /* Bad permission.  */
1201
        return 1;
1202
    }
1203
    return 0;
1204
}
1205

    
1206
static inline int get_phys_addr(CPUState *env, uint32_t address,
1207
                                int access_type, int is_user,
1208
                                uint32_t *phys_ptr, int *prot)
1209
{
1210
    /* Fast Context Switch Extension.  */
1211
    if (address < 0x02000000)
1212
        address += env->cp15.c13_fcse;
1213

    
1214
    if ((env->cp15.c1_sys & 1) == 0) {
1215
        /* MMU/MPU disabled.  */
1216
        *phys_ptr = address;
1217
        *prot = PAGE_READ | PAGE_WRITE;
1218
        return 0;
1219
    } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1220
        return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1221
                                 prot);
1222
    } else if (env->cp15.c1_sys & (1 << 23)) {
1223
        return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1224
                                prot);
1225
    } else {
1226
        return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1227
                                prot);
1228
    }
1229
}
1230

    
1231
int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
1232
                              int access_type, int mmu_idx, int is_softmmu)
1233
{
1234
    uint32_t phys_addr;
1235
    int prot;
1236
    int ret, is_user;
1237

    
1238
    is_user = mmu_idx == MMU_USER_IDX;
1239
    ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot);
1240
    if (ret == 0) {
1241
        /* Map a single [sub]page.  */
1242
        phys_addr &= ~(uint32_t)0x3ff;
1243
        address &= ~(uint32_t)0x3ff;
1244
        return tlb_set_page (env, address, phys_addr, prot, mmu_idx,
1245
                             is_softmmu);
1246
    }
1247

    
1248
    if (access_type == 2) {
1249
        env->cp15.c5_insn = ret;
1250
        env->cp15.c6_insn = address;
1251
        env->exception_index = EXCP_PREFETCH_ABORT;
1252
    } else {
1253
        env->cp15.c5_data = ret;
1254
        if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1255
            env->cp15.c5_data |= (1 << 11);
1256
        env->cp15.c6_data = address;
1257
        env->exception_index = EXCP_DATA_ABORT;
1258
    }
1259
    return 1;
1260
}
1261

    
1262
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
1263
{
1264
    uint32_t phys_addr;
1265
    int prot;
1266
    int ret;
1267

    
1268
    ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot);
1269

    
1270
    if (ret != 0)
1271
        return -1;
1272

    
1273
    return phys_addr;
1274
}
1275

    
1276
/* Not really implemented.  Need to figure out a sane way of doing this.
1277
   Maybe add generic watchpoint support and use that.  */
1278

    
1279
void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
1280
{
1281
    env->mmon_addr = addr;
1282
}
1283

    
1284
uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
1285
{
1286
    return (env->mmon_addr != addr);
1287
}
1288

    
1289
void HELPER(clrex)(CPUState *env)
1290
{
1291
    env->mmon_addr = -1;
1292
}
1293

    
1294
void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
1295
{
1296
    int cp_num = (insn >> 8) & 0xf;
1297
    int cp_info = (insn >> 5) & 7;
1298
    int src = (insn >> 16) & 0xf;
1299
    int operand = insn & 0xf;
1300

    
1301
    if (env->cp[cp_num].cp_write)
1302
        env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1303
                                 cp_info, src, operand, val);
1304
}
1305

    
1306
uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
1307
{
1308
    int cp_num = (insn >> 8) & 0xf;
1309
    int cp_info = (insn >> 5) & 7;
1310
    int dest = (insn >> 16) & 0xf;
1311
    int operand = insn & 0xf;
1312

    
1313
    if (env->cp[cp_num].cp_read)
1314
        return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1315
                                       cp_info, dest, operand);
1316
    return 0;
1317
}
1318

    
1319
/* Return basic MPU access permission bits.  */
1320
static uint32_t simple_mpu_ap_bits(uint32_t val)
1321
{
1322
    uint32_t ret;
1323
    uint32_t mask;
1324
    int i;
1325
    ret = 0;
1326
    mask = 3;
1327
    for (i = 0; i < 16; i += 2) {
1328
        ret |= (val >> i) & mask;
1329
        mask <<= 2;
1330
    }
1331
    return ret;
1332
}
1333

    
1334
/* Pad basic MPU access permission bits to extended format.  */
1335
static uint32_t extended_mpu_ap_bits(uint32_t val)
1336
{
1337
    uint32_t ret;
1338
    uint32_t mask;
1339
    int i;
1340
    ret = 0;
1341
    mask = 3;
1342
    for (i = 0; i < 16; i += 2) {
1343
        ret |= (val & mask) << i;
1344
        mask <<= 2;
1345
    }
1346
    return ret;
1347
}
1348

    
1349
void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
1350
{
1351
    int op1;
1352
    int op2;
1353
    int crm;
1354

    
1355
    op1 = (insn >> 21) & 7;
1356
    op2 = (insn >> 5) & 7;
1357
    crm = insn & 0xf;
1358
    switch ((insn >> 16) & 0xf) {
1359
    case 0:
1360
        /* ID codes.  */
1361
        if (arm_feature(env, ARM_FEATURE_XSCALE))
1362
            break;
1363
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1364
            break;
1365
        if (arm_feature(env, ARM_FEATURE_V7)
1366
                && op1 == 2 && crm == 0 && op2 == 0) {
1367
            env->cp15.c0_cssel = val & 0xf;
1368
            break;
1369
        }
1370
        goto bad_reg;
1371
    case 1: /* System configuration.  */
1372
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1373
            op2 = 0;
1374
        switch (op2) {
1375
        case 0:
1376
            if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1377
                env->cp15.c1_sys = val;
1378
            /* ??? Lots of these bits are not implemented.  */
1379
            /* This may enable/disable the MMU, so do a TLB flush.  */
1380
            tlb_flush(env, 1);
1381
            break;
1382
        case 1: /* Auxiliary cotrol register.  */
1383
            if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1384
                env->cp15.c1_xscaleauxcr = val;
1385
                break;
1386
            }
1387
            /* Not implemented.  */
1388
            break;
1389
        case 2:
1390
            if (arm_feature(env, ARM_FEATURE_XSCALE))
1391
                goto bad_reg;
1392
            if (env->cp15.c1_coproc != val) {
1393
                env->cp15.c1_coproc = val;
1394
                /* ??? Is this safe when called from within a TB?  */
1395
                tb_flush(env);
1396
            }
1397
            break;
1398
        default:
1399
            goto bad_reg;
1400
        }
1401
        break;
1402
    case 2: /* MMU Page table control / MPU cache control.  */
1403
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1404
            switch (op2) {
1405
            case 0:
1406
                env->cp15.c2_data = val;
1407
                break;
1408
            case 1:
1409
                env->cp15.c2_insn = val;
1410
                break;
1411
            default:
1412
                goto bad_reg;
1413
            }
1414
        } else {
1415
            switch (op2) {
1416
            case 0:
1417
                env->cp15.c2_base0 = val;
1418
                break;
1419
            case 1:
1420
                env->cp15.c2_base1 = val;
1421
                break;
1422
            case 2:
1423
                val &= 7;
1424
                env->cp15.c2_control = val;
1425
                env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1426
                env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1427
                break;
1428
            default:
1429
                goto bad_reg;
1430
            }
1431
        }
1432
        break;
1433
    case 3: /* MMU Domain access control / MPU write buffer control.  */
1434
        env->cp15.c3 = val;
1435
        tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1436
        break;
1437
    case 4: /* Reserved.  */
1438
        goto bad_reg;
1439
    case 5: /* MMU Fault status / MPU access permission.  */
1440
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1441
            op2 = 0;
1442
        switch (op2) {
1443
        case 0:
1444
            if (arm_feature(env, ARM_FEATURE_MPU))
1445
                val = extended_mpu_ap_bits(val);
1446
            env->cp15.c5_data = val;
1447
            break;
1448
        case 1:
1449
            if (arm_feature(env, ARM_FEATURE_MPU))
1450
                val = extended_mpu_ap_bits(val);
1451
            env->cp15.c5_insn = val;
1452
            break;
1453
        case 2:
1454
            if (!arm_feature(env, ARM_FEATURE_MPU))
1455
                goto bad_reg;
1456
            env->cp15.c5_data = val;
1457
            break;
1458
        case 3:
1459
            if (!arm_feature(env, ARM_FEATURE_MPU))
1460
                goto bad_reg;
1461
            env->cp15.c5_insn = val;
1462
            break;
1463
        default:
1464
            goto bad_reg;
1465
        }
1466
        break;
1467
    case 6: /* MMU Fault address / MPU base/size.  */
1468
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1469
            if (crm >= 8)
1470
                goto bad_reg;
1471
            env->cp15.c6_region[crm] = val;
1472
        } else {
1473
            if (arm_feature(env, ARM_FEATURE_OMAPCP))
1474
                op2 = 0;
1475
            switch (op2) {
1476
            case 0:
1477
                env->cp15.c6_data = val;
1478
                break;
1479
            case 1: /* ??? This is WFAR on armv6 */
1480
            case 2:
1481
                env->cp15.c6_insn = val;
1482
                break;
1483
            default:
1484
                goto bad_reg;
1485
            }
1486
        }
1487
        break;
1488
    case 7: /* Cache control.  */
1489
        env->cp15.c15_i_max = 0x000;
1490
        env->cp15.c15_i_min = 0xff0;
1491
        /* No cache, so nothing to do.  */
1492
        /* ??? MPCore has VA to PA translation functions.  */
1493
        break;
1494
    case 8: /* MMU TLB control.  */
1495
        switch (op2) {
1496
        case 0: /* Invalidate all.  */
1497
            tlb_flush(env, 0);
1498
            break;
1499
        case 1: /* Invalidate single TLB entry.  */
1500
#if 0
1501
            /* ??? This is wrong for large pages and sections.  */
1502
            /* As an ugly hack to make linux work we always flush a 4K
1503
               pages.  */
1504
            val &= 0xfffff000;
1505
            tlb_flush_page(env, val);
1506
            tlb_flush_page(env, val + 0x400);
1507
            tlb_flush_page(env, val + 0x800);
1508
            tlb_flush_page(env, val + 0xc00);
1509
#else
1510
            tlb_flush(env, 1);
1511
#endif
1512
            break;
1513
        case 2: /* Invalidate on ASID.  */
1514
            tlb_flush(env, val == 0);
1515
            break;
1516
        case 3: /* Invalidate single entry on MVA.  */
1517
            /* ??? This is like case 1, but ignores ASID.  */
1518
            tlb_flush(env, 1);
1519
            break;
1520
        default:
1521
            goto bad_reg;
1522
        }
1523
        break;
1524
    case 9:
1525
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1526
            break;
1527
        switch (crm) {
1528
        case 0: /* Cache lockdown.  */
1529
            switch (op1) {
1530
            case 0: /* L1 cache.  */
1531
                switch (op2) {
1532
                case 0:
1533
                    env->cp15.c9_data = val;
1534
                    break;
1535
                case 1:
1536
                    env->cp15.c9_insn = val;
1537
                    break;
1538
                default:
1539
                    goto bad_reg;
1540
                }
1541
                break;
1542
            case 1: /* L2 cache.  */
1543
                /* Ignore writes to L2 lockdown/auxiliary registers.  */
1544
                break;
1545
            default:
1546
                goto bad_reg;
1547
            }
1548
            break;
1549
        case 1: /* TCM memory region registers.  */
1550
            /* Not implemented.  */
1551
            goto bad_reg;
1552
        default:
1553
            goto bad_reg;
1554
        }
1555
        break;
1556
    case 10: /* MMU TLB lockdown.  */
1557
        /* ??? TLB lockdown not implemented.  */
1558
        break;
1559
    case 12: /* Reserved.  */
1560
        goto bad_reg;
1561
    case 13: /* Process ID.  */
1562
        switch (op2) {
1563
        case 0:
1564
            /* Unlike real hardware the qemu TLB uses virtual addresses,
1565
               not modified virtual addresses, so this causes a TLB flush.
1566
             */
1567
            if (env->cp15.c13_fcse != val)
1568
              tlb_flush(env, 1);
1569
            env->cp15.c13_fcse = val;
1570
            break;
1571
        case 1:
1572
            /* This changes the ASID, so do a TLB flush.  */
1573
            if (env->cp15.c13_context != val
1574
                && !arm_feature(env, ARM_FEATURE_MPU))
1575
              tlb_flush(env, 0);
1576
            env->cp15.c13_context = val;
1577
            break;
1578
        case 2:
1579
            env->cp15.c13_tls1 = val;
1580
            break;
1581
        case 3:
1582
            env->cp15.c13_tls2 = val;
1583
            break;
1584
        case 4:
1585
            env->cp15.c13_tls3 = val;
1586
            break;
1587
        default:
1588
            goto bad_reg;
1589
        }
1590
        break;
1591
    case 14: /* Reserved.  */
1592
        goto bad_reg;
1593
    case 15: /* Implementation specific.  */
1594
        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1595
            if (op2 == 0 && crm == 1) {
1596
                if (env->cp15.c15_cpar != (val & 0x3fff)) {
1597
                    /* Changes cp0 to cp13 behavior, so needs a TB flush.  */
1598
                    tb_flush(env);
1599
                    env->cp15.c15_cpar = val & 0x3fff;
1600
                }
1601
                break;
1602
            }
1603
            goto bad_reg;
1604
        }
1605
        if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1606
            switch (crm) {
1607
            case 0:
1608
                break;
1609
            case 1: /* Set TI925T configuration.  */
1610
                env->cp15.c15_ticonfig = val & 0xe7;
1611
                env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1612
                        ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1613
                break;
1614
            case 2: /* Set I_max.  */
1615
                env->cp15.c15_i_max = val;
1616
                break;
1617
            case 3: /* Set I_min.  */
1618
                env->cp15.c15_i_min = val;
1619
                break;
1620
            case 4: /* Set thread-ID.  */
1621
                env->cp15.c15_threadid = val & 0xffff;
1622
                break;
1623
            case 8: /* Wait-for-interrupt (deprecated).  */
1624
                cpu_interrupt(env, CPU_INTERRUPT_HALT);
1625
                break;
1626
            default:
1627
                goto bad_reg;
1628
            }
1629
        }
1630
        break;
1631
    }
1632
    return;
1633
bad_reg:
1634
    /* ??? For debugging only.  Should raise illegal instruction exception.  */
1635
    cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1636
              (insn >> 16) & 0xf, crm, op1, op2);
1637
}
1638

    
1639
uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
1640
{
1641
    int op1;
1642
    int op2;
1643
    int crm;
1644

    
1645
    op1 = (insn >> 21) & 7;
1646
    op2 = (insn >> 5) & 7;
1647
    crm = insn & 0xf;
1648
    switch ((insn >> 16) & 0xf) {
1649
    case 0: /* ID codes.  */
1650
        switch (op1) {
1651
        case 0:
1652
            switch (crm) {
1653
            case 0:
1654
                switch (op2) {
1655
                case 0: /* Device ID.  */
1656
                    return env->cp15.c0_cpuid;
1657
                case 1: /* Cache Type.  */
1658
                    return env->cp15.c0_cachetype;
1659
                case 2: /* TCM status.  */
1660
                    return 0;
1661
                case 3: /* TLB type register.  */
1662
                    return 0; /* No lockable TLB entries.  */
1663
                case 5: /* CPU ID */
1664
                    if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1665
                        return env->cpu_index | 0x80000900;
1666
                    } else {
1667
                        return env->cpu_index;
1668
                    }
1669
                default:
1670
                    goto bad_reg;
1671
                }
1672
            case 1:
1673
                if (!arm_feature(env, ARM_FEATURE_V6))
1674
                    goto bad_reg;
1675
                return env->cp15.c0_c1[op2];
1676
            case 2:
1677
                if (!arm_feature(env, ARM_FEATURE_V6))
1678
                    goto bad_reg;
1679
                return env->cp15.c0_c2[op2];
1680
            case 3: case 4: case 5: case 6: case 7:
1681
                return 0;
1682
            default:
1683
                goto bad_reg;
1684
            }
1685
        case 1:
1686
            /* These registers aren't documented on arm11 cores.  However
1687
               Linux looks at them anyway.  */
1688
            if (!arm_feature(env, ARM_FEATURE_V6))
1689
                goto bad_reg;
1690
            if (crm != 0)
1691
                goto bad_reg;
1692
            if (!arm_feature(env, ARM_FEATURE_V7))
1693
                return 0;
1694

    
1695
            switch (op2) {
1696
            case 0:
1697
                return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1698
            case 1:
1699
                return env->cp15.c0_clid;
1700
            case 7:
1701
                return 0;
1702
            }
1703
            goto bad_reg;
1704
        case 2:
1705
            if (op2 != 0 || crm != 0)
1706
                goto bad_reg;
1707
            return env->cp15.c0_cssel;
1708
        default:
1709
            goto bad_reg;
1710
        }
1711
    case 1: /* System configuration.  */
1712
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1713
            op2 = 0;
1714
        switch (op2) {
1715
        case 0: /* Control register.  */
1716
            return env->cp15.c1_sys;
1717
        case 1: /* Auxiliary control register.  */
1718
            if (arm_feature(env, ARM_FEATURE_XSCALE))
1719
                return env->cp15.c1_xscaleauxcr;
1720
            if (!arm_feature(env, ARM_FEATURE_AUXCR))
1721
                goto bad_reg;
1722
            switch (ARM_CPUID(env)) {
1723
            case ARM_CPUID_ARM1026:
1724
                return 1;
1725
            case ARM_CPUID_ARM1136:
1726
            case ARM_CPUID_ARM1136_R2:
1727
                return 7;
1728
            case ARM_CPUID_ARM11MPCORE:
1729
                return 1;
1730
            case ARM_CPUID_CORTEXA8:
1731
                return 2;
1732
            case ARM_CPUID_CORTEXA9:
1733
                return 0;
1734
            default:
1735
                goto bad_reg;
1736
            }
1737
        case 2: /* Coprocessor access register.  */
1738
            if (arm_feature(env, ARM_FEATURE_XSCALE))
1739
                goto bad_reg;
1740
            return env->cp15.c1_coproc;
1741
        default:
1742
            goto bad_reg;
1743
        }
1744
    case 2: /* MMU Page table control / MPU cache control.  */
1745
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1746
            switch (op2) {
1747
            case 0:
1748
                return env->cp15.c2_data;
1749
                break;
1750
            case 1:
1751
                return env->cp15.c2_insn;
1752
                break;
1753
            default:
1754
                goto bad_reg;
1755
            }
1756
        } else {
1757
            switch (op2) {
1758
            case 0:
1759
                return env->cp15.c2_base0;
1760
            case 1:
1761
                return env->cp15.c2_base1;
1762
            case 2:
1763
                return env->cp15.c2_control;
1764
            default:
1765
                goto bad_reg;
1766
            }
1767
        }
1768
    case 3: /* MMU Domain access control / MPU write buffer control.  */
1769
        return env->cp15.c3;
1770
    case 4: /* Reserved.  */
1771
        goto bad_reg;
1772
    case 5: /* MMU Fault status / MPU access permission.  */
1773
        if (arm_feature(env, ARM_FEATURE_OMAPCP))
1774
            op2 = 0;
1775
        switch (op2) {
1776
        case 0:
1777
            if (arm_feature(env, ARM_FEATURE_MPU))
1778
                return simple_mpu_ap_bits(env->cp15.c5_data);
1779
            return env->cp15.c5_data;
1780
        case 1:
1781
            if (arm_feature(env, ARM_FEATURE_MPU))
1782
                return simple_mpu_ap_bits(env->cp15.c5_data);
1783
            return env->cp15.c5_insn;
1784
        case 2:
1785
            if (!arm_feature(env, ARM_FEATURE_MPU))
1786
                goto bad_reg;
1787
            return env->cp15.c5_data;
1788
        case 3:
1789
            if (!arm_feature(env, ARM_FEATURE_MPU))
1790
                goto bad_reg;
1791
            return env->cp15.c5_insn;
1792
        default:
1793
            goto bad_reg;
1794
        }
1795
    case 6: /* MMU Fault address.  */
1796
        if (arm_feature(env, ARM_FEATURE_MPU)) {
1797
            if (crm >= 8)
1798
                goto bad_reg;
1799
            return env->cp15.c6_region[crm];
1800
        } else {
1801
            if (arm_feature(env, ARM_FEATURE_OMAPCP))
1802
                op2 = 0;
1803
            switch (op2) {
1804
            case 0:
1805
                return env->cp15.c6_data;
1806
            case 1:
1807
                if (arm_feature(env, ARM_FEATURE_V6)) {
1808
                    /* Watchpoint Fault Adrress.  */
1809
                    return 0; /* Not implemented.  */
1810
                } else {
1811
                    /* Instruction Fault Adrress.  */
1812
                    /* Arm9 doesn't have an IFAR, but implementing it anyway
1813
                       shouldn't do any harm.  */
1814
                    return env->cp15.c6_insn;
1815
                }
1816
            case 2:
1817
                if (arm_feature(env, ARM_FEATURE_V6)) {
1818
                    /* Instruction Fault Adrress.  */
1819
                    return env->cp15.c6_insn;
1820
                } else {
1821
                    goto bad_reg;
1822
                }
1823
            default:
1824
                goto bad_reg;
1825
            }
1826
        }
1827
    case 7: /* Cache control.  */
1828
        /* FIXME: Should only clear Z flag if destination is r15.  */
1829
        env->ZF = 0;
1830
        return 0;
1831
    case 8: /* MMU TLB control.  */
1832
        goto bad_reg;
1833
    case 9: /* Cache lockdown.  */
1834
        switch (op1) {
1835
        case 0: /* L1 cache.  */
1836
            if (arm_feature(env, ARM_FEATURE_OMAPCP))
1837
                return 0;
1838
            switch (op2) {
1839
            case 0:
1840
                return env->cp15.c9_data;
1841
            case 1:
1842
                return env->cp15.c9_insn;
1843
            default:
1844
                goto bad_reg;
1845
            }
1846
        case 1: /* L2 cache */
1847
            if (crm != 0)
1848
                goto bad_reg;
1849
            /* L2 Lockdown and Auxiliary control.  */
1850
            return 0;
1851
        default:
1852
            goto bad_reg;
1853
        }
1854
    case 10: /* MMU TLB lockdown.  */
1855
        /* ??? TLB lockdown not implemented.  */
1856
        return 0;
1857
    case 11: /* TCM DMA control.  */
1858
    case 12: /* Reserved.  */
1859
        goto bad_reg;
1860
    case 13: /* Process ID.  */
1861
        switch (op2) {
1862
        case 0:
1863
            return env->cp15.c13_fcse;
1864
        case 1:
1865
            return env->cp15.c13_context;
1866
        case 2:
1867
            return env->cp15.c13_tls1;
1868
        case 3:
1869
            return env->cp15.c13_tls2;
1870
        case 4:
1871
            return env->cp15.c13_tls3;
1872
        default:
1873
            goto bad_reg;
1874
        }
1875
    case 14: /* Reserved.  */
1876
        goto bad_reg;
1877
    case 15: /* Implementation specific.  */
1878
        if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1879
            if (op2 == 0 && crm == 1)
1880
                return env->cp15.c15_cpar;
1881

    
1882
            goto bad_reg;
1883
        }
1884
        if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1885
            switch (crm) {
1886
            case 0:
1887
                return 0;
1888
            case 1: /* Read TI925T configuration.  */
1889
                return env->cp15.c15_ticonfig;
1890
            case 2: /* Read I_max.  */
1891
                return env->cp15.c15_i_max;
1892
            case 3: /* Read I_min.  */
1893
                return env->cp15.c15_i_min;
1894
            case 4: /* Read thread-ID.  */
1895
                return env->cp15.c15_threadid;
1896
            case 8: /* TI925T_status */
1897
                return 0;
1898
            }
1899
            /* TODO: Peripheral port remap register:
1900
             * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1901
             * controller base address at $rn & ~0xfff and map size of
1902
             * 0x200 << ($rn & 0xfff), when MMU is off.  */
1903
            goto bad_reg;
1904
        }
1905
        return 0;
1906
    }
1907
bad_reg:
1908
    /* ??? For debugging only.  Should raise illegal instruction exception.  */
1909
    cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1910
              (insn >> 16) & 0xf, crm, op1, op2);
1911
    return 0;
1912
}
1913

    
1914
void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
1915
{
1916
    env->banked_r13[bank_number(mode)] = val;
1917
}
1918

    
1919
uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
1920
{
1921
    return env->banked_r13[bank_number(mode)];
1922
}
1923

    
1924
uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
1925
{
1926
    switch (reg) {
1927
    case 0: /* APSR */
1928
        return xpsr_read(env) & 0xf8000000;
1929
    case 1: /* IAPSR */
1930
        return xpsr_read(env) & 0xf80001ff;
1931
    case 2: /* EAPSR */
1932
        return xpsr_read(env) & 0xff00fc00;
1933
    case 3: /* xPSR */
1934
        return xpsr_read(env) & 0xff00fdff;
1935
    case 5: /* IPSR */
1936
        return xpsr_read(env) & 0x000001ff;
1937
    case 6: /* EPSR */
1938
        return xpsr_read(env) & 0x0700fc00;
1939
    case 7: /* IEPSR */
1940
        return xpsr_read(env) & 0x0700edff;
1941
    case 8: /* MSP */
1942
        return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
1943
    case 9: /* PSP */
1944
        return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
1945
    case 16: /* PRIMASK */
1946
        return (env->uncached_cpsr & CPSR_I) != 0;
1947
    case 17: /* FAULTMASK */
1948
        return (env->uncached_cpsr & CPSR_F) != 0;
1949
    case 18: /* BASEPRI */
1950
    case 19: /* BASEPRI_MAX */
1951
        return env->v7m.basepri;
1952
    case 20: /* CONTROL */
1953
        return env->v7m.control;
1954
    default:
1955
        /* ??? For debugging only.  */
1956
        cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
1957
        return 0;
1958
    }
1959
}
1960

    
1961
void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
1962
{
1963
    switch (reg) {
1964
    case 0: /* APSR */
1965
        xpsr_write(env, val, 0xf8000000);
1966
        break;
1967
    case 1: /* IAPSR */
1968
        xpsr_write(env, val, 0xf8000000);
1969
        break;
1970
    case 2: /* EAPSR */
1971
        xpsr_write(env, val, 0xfe00fc00);
1972
        break;
1973
    case 3: /* xPSR */
1974
        xpsr_write(env, val, 0xfe00fc00);
1975
        break;
1976
    case 5: /* IPSR */
1977
        /* IPSR bits are readonly.  */
1978
        break;
1979
    case 6: /* EPSR */
1980
        xpsr_write(env, val, 0x0600fc00);
1981
        break;
1982
    case 7: /* IEPSR */
1983
        xpsr_write(env, val, 0x0600fc00);
1984
        break;
1985
    case 8: /* MSP */
1986
        if (env->v7m.current_sp)
1987
            env->v7m.other_sp = val;
1988
        else
1989
            env->regs[13] = val;
1990
        break;
1991
    case 9: /* PSP */
1992
        if (env->v7m.current_sp)
1993
            env->regs[13] = val;
1994
        else
1995
            env->v7m.other_sp = val;
1996
        break;
1997
    case 16: /* PRIMASK */
1998
        if (val & 1)
1999
            env->uncached_cpsr |= CPSR_I;
2000
        else
2001
            env->uncached_cpsr &= ~CPSR_I;
2002
        break;
2003
    case 17: /* FAULTMASK */
2004
        if (val & 1)
2005
            env->uncached_cpsr |= CPSR_F;
2006
        else
2007
            env->uncached_cpsr &= ~CPSR_F;
2008
        break;
2009
    case 18: /* BASEPRI */
2010
        env->v7m.basepri = val & 0xff;
2011
        break;
2012
    case 19: /* BASEPRI_MAX */
2013
        val &= 0xff;
2014
        if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2015
            env->v7m.basepri = val;
2016
        break;
2017
    case 20: /* CONTROL */
2018
        env->v7m.control = val & 3;
2019
        switch_v7m_sp(env, (val & 2) != 0);
2020
        break;
2021
    default:
2022
        /* ??? For debugging only.  */
2023
        cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2024
        return;
2025
    }
2026
}
2027

    
2028
void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2029
                ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2030
                void *opaque)
2031
{
2032
    if (cpnum < 0 || cpnum > 14) {
2033
        cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2034
        return;
2035
    }
2036

    
2037
    env->cp[cpnum].cp_read = cp_read;
2038
    env->cp[cpnum].cp_write = cp_write;
2039
    env->cp[cpnum].opaque = opaque;
2040
}
2041

    
2042
#endif
2043

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

    
2048
/* Signed saturating arithmetic.  */
2049

    
2050
/* Perform 16-bit signed saturating addition.  */
2051
static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2052
{
2053
    uint16_t res;
2054

    
2055
    res = a + b;
2056
    if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2057
        if (a & 0x8000)
2058
            res = 0x8000;
2059
        else
2060
            res = 0x7fff;
2061
    }
2062
    return res;
2063
}
2064

    
2065
/* Perform 8-bit signed saturating addition.  */
2066
static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2067
{
2068
    uint8_t res;
2069

    
2070
    res = a + b;
2071
    if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2072
        if (a & 0x80)
2073
            res = 0x80;
2074
        else
2075
            res = 0x7f;
2076
    }
2077
    return res;
2078
}
2079

    
2080
/* Perform 16-bit signed saturating subtraction.  */
2081
static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2082
{
2083
    uint16_t res;
2084

    
2085
    res = a - b;
2086
    if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2087
        if (a & 0x8000)
2088
            res = 0x8000;
2089
        else
2090
            res = 0x7fff;
2091
    }
2092
    return res;
2093
}
2094

    
2095
/* Perform 8-bit signed saturating subtraction.  */
2096
static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2097
{
2098
    uint8_t res;
2099

    
2100
    res = a - b;
2101
    if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2102
        if (a & 0x80)
2103
            res = 0x80;
2104
        else
2105
            res = 0x7f;
2106
    }
2107
    return res;
2108
}
2109

    
2110
#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2111
#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2112
#define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
2113
#define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
2114
#define PFX q
2115

    
2116
#include "op_addsub.h"
2117

    
2118
/* Unsigned saturating arithmetic.  */
2119
static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2120
{
2121
    uint16_t res;
2122
    res = a + b;
2123
    if (res < a)
2124
        res = 0xffff;
2125
    return res;
2126
}
2127

    
2128
static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2129
{
2130
    if (a < b)
2131
        return a - b;
2132
    else
2133
        return 0;
2134
}
2135

    
2136
static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2137
{
2138
    uint8_t res;
2139
    res = a + b;
2140
    if (res < a)
2141
        res = 0xff;
2142
    return res;
2143
}
2144

    
2145
static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2146
{
2147
    if (a < b)
2148
        return a - b;
2149
    else
2150
        return 0;
2151
}
2152

    
2153
#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2154
#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2155
#define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
2156
#define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
2157
#define PFX uq
2158

    
2159
#include "op_addsub.h"
2160

    
2161
/* Signed modulo arithmetic.  */
2162
#define SARITH16(a, b, n, op) do { \
2163
    int32_t sum; \
2164
    sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2165
    RESULT(sum, n, 16); \
2166
    if (sum >= 0) \
2167
        ge |= 3 << (n * 2); \
2168
    } while(0)
2169

    
2170
#define SARITH8(a, b, n, op) do { \
2171
    int32_t sum; \
2172
    sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2173
    RESULT(sum, n, 8); \
2174
    if (sum >= 0) \
2175
        ge |= 1 << n; \
2176
    } while(0)
2177

    
2178

    
2179
#define ADD16(a, b, n) SARITH16(a, b, n, +)
2180
#define SUB16(a, b, n) SARITH16(a, b, n, -)
2181
#define ADD8(a, b, n)  SARITH8(a, b, n, +)
2182
#define SUB8(a, b, n)  SARITH8(a, b, n, -)
2183
#define PFX s
2184
#define ARITH_GE
2185

    
2186
#include "op_addsub.h"
2187

    
2188
/* Unsigned modulo arithmetic.  */
2189
#define ADD16(a, b, n) do { \
2190
    uint32_t sum; \
2191
    sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2192
    RESULT(sum, n, 16); \
2193
    if ((sum >> 16) == 1) \
2194
        ge |= 3 << (n * 2); \
2195
    } while(0)
2196

    
2197
#define ADD8(a, b, n) do { \
2198
    uint32_t sum; \
2199
    sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2200
    RESULT(sum, n, 8); \
2201
    if ((sum >> 8) == 1) \
2202
        ge |= 1 << n; \
2203
    } while(0)
2204

    
2205
#define SUB16(a, b, n) do { \
2206
    uint32_t sum; \
2207
    sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2208
    RESULT(sum, n, 16); \
2209
    if ((sum >> 16) == 0) \
2210
        ge |= 3 << (n * 2); \
2211
    } while(0)
2212

    
2213
#define SUB8(a, b, n) do { \
2214
    uint32_t sum; \
2215
    sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2216
    RESULT(sum, n, 8); \
2217
    if ((sum >> 8) == 0) \
2218
        ge |= 1 << n; \
2219
    } while(0)
2220

    
2221
#define PFX u
2222
#define ARITH_GE
2223

    
2224
#include "op_addsub.h"
2225

    
2226
/* Halved signed arithmetic.  */
2227
#define ADD16(a, b, n) \
2228
  RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2229
#define SUB16(a, b, n) \
2230
  RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2231
#define ADD8(a, b, n) \
2232
  RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2233
#define SUB8(a, b, n) \
2234
  RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2235
#define PFX sh
2236

    
2237
#include "op_addsub.h"
2238

    
2239
/* Halved unsigned arithmetic.  */
2240
#define ADD16(a, b, n) \
2241
  RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2242
#define SUB16(a, b, n) \
2243
  RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2244
#define ADD8(a, b, n) \
2245
  RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2246
#define SUB8(a, b, n) \
2247
  RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2248
#define PFX uh
2249

    
2250
#include "op_addsub.h"
2251

    
2252
static inline uint8_t do_usad(uint8_t a, uint8_t b)
2253
{
2254
    if (a > b)
2255
        return a - b;
2256
    else
2257
        return b - a;
2258
}
2259

    
2260
/* Unsigned sum of absolute byte differences.  */
2261
uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2262
{
2263
    uint32_t sum;
2264
    sum = do_usad(a, b);
2265
    sum += do_usad(a >> 8, b >> 8);
2266
    sum += do_usad(a >> 16, b >>16);
2267
    sum += do_usad(a >> 24, b >> 24);
2268
    return sum;
2269
}
2270

    
2271
/* For ARMv6 SEL instruction.  */
2272
uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2273
{
2274
    uint32_t mask;
2275

    
2276
    mask = 0;
2277
    if (flags & 1)
2278
        mask |= 0xff;
2279
    if (flags & 2)
2280
        mask |= 0xff00;
2281
    if (flags & 4)
2282
        mask |= 0xff0000;
2283
    if (flags & 8)
2284
        mask |= 0xff000000;
2285
    return (a & mask) | (b & ~mask);
2286
}
2287

    
2288
uint32_t HELPER(logicq_cc)(uint64_t val)
2289
{
2290
    return (val >> 32) | (val != 0);
2291
}
2292

    
2293
/* VFP support.  We follow the convention used for VFP instrunctions:
2294
   Single precition routines have a "s" suffix, double precision a
2295
   "d" suffix.  */
2296

    
2297
/* Convert host exception flags to vfp form.  */
2298
static inline int vfp_exceptbits_from_host(int host_bits)
2299
{
2300
    int target_bits = 0;
2301

    
2302
    if (host_bits & float_flag_invalid)
2303
        target_bits |= 1;
2304
    if (host_bits & float_flag_divbyzero)
2305
        target_bits |= 2;
2306
    if (host_bits & float_flag_overflow)
2307
        target_bits |= 4;
2308
    if (host_bits & float_flag_underflow)
2309
        target_bits |= 8;
2310
    if (host_bits & float_flag_inexact)
2311
        target_bits |= 0x10;
2312
    return target_bits;
2313
}
2314

    
2315
uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2316
{
2317
    int i;
2318
    uint32_t fpscr;
2319

    
2320
    fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2321
            | (env->vfp.vec_len << 16)
2322
            | (env->vfp.vec_stride << 20);
2323
    i = get_float_exception_flags(&env->vfp.fp_status);
2324
    fpscr |= vfp_exceptbits_from_host(i);
2325
    return fpscr;
2326
}
2327

    
2328
/* Convert vfp exception flags to target form.  */
2329
static inline int vfp_exceptbits_to_host(int target_bits)
2330
{
2331
    int host_bits = 0;
2332

    
2333
    if (target_bits & 1)
2334
        host_bits |= float_flag_invalid;
2335
    if (target_bits & 2)
2336
        host_bits |= float_flag_divbyzero;
2337
    if (target_bits & 4)
2338
        host_bits |= float_flag_overflow;
2339
    if (target_bits & 8)
2340
        host_bits |= float_flag_underflow;
2341
    if (target_bits & 0x10)
2342
        host_bits |= float_flag_inexact;
2343
    return host_bits;
2344
}
2345

    
2346
void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2347
{
2348
    int i;
2349
    uint32_t changed;
2350

    
2351
    changed = env->vfp.xregs[ARM_VFP_FPSCR];
2352
    env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2353
    env->vfp.vec_len = (val >> 16) & 7;
2354
    env->vfp.vec_stride = (val >> 20) & 3;
2355

    
2356
    changed ^= val;
2357
    if (changed & (3 << 22)) {
2358
        i = (val >> 22) & 3;
2359
        switch (i) {
2360
        case 0:
2361
            i = float_round_nearest_even;
2362
            break;
2363
        case 1:
2364
            i = float_round_up;
2365
            break;
2366
        case 2:
2367
            i = float_round_down;
2368
            break;
2369
        case 3:
2370
            i = float_round_to_zero;
2371
            break;
2372
        }
2373
        set_float_rounding_mode(i, &env->vfp.fp_status);
2374
    }
2375
    if (changed & (1 << 24))
2376
        set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2377
    if (changed & (1 << 25))
2378
        set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2379

    
2380
    i = vfp_exceptbits_to_host((val >> 8) & 0x1f);
2381
    set_float_exception_flags(i, &env->vfp.fp_status);
2382
}
2383

    
2384
#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2385

    
2386
#define VFP_BINOP(name) \
2387
float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2388
{ \
2389
    return float32_ ## name (a, b, &env->vfp.fp_status); \
2390
} \
2391
float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2392
{ \
2393
    return float64_ ## name (a, b, &env->vfp.fp_status); \
2394
}
2395
VFP_BINOP(add)
2396
VFP_BINOP(sub)
2397
VFP_BINOP(mul)
2398
VFP_BINOP(div)
2399
#undef VFP_BINOP
2400

    
2401
float32 VFP_HELPER(neg, s)(float32 a)
2402
{
2403
    return float32_chs(a);
2404
}
2405

    
2406
float64 VFP_HELPER(neg, d)(float64 a)
2407
{
2408
    return float64_chs(a);
2409
}
2410

    
2411
float32 VFP_HELPER(abs, s)(float32 a)
2412
{
2413
    return float32_abs(a);
2414
}
2415

    
2416
float64 VFP_HELPER(abs, d)(float64 a)
2417
{
2418
    return float64_abs(a);
2419
}
2420

    
2421
float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2422
{
2423
    return float32_sqrt(a, &env->vfp.fp_status);
2424
}
2425

    
2426
float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2427
{
2428
    return float64_sqrt(a, &env->vfp.fp_status);
2429
}
2430

    
2431
/* XXX: check quiet/signaling case */
2432
#define DO_VFP_cmp(p, type) \
2433
void VFP_HELPER(cmp, p)(type a, type b, CPUState *env)  \
2434
{ \
2435
    uint32_t flags; \
2436
    switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2437
    case 0: flags = 0x6; break; \
2438
    case -1: flags = 0x8; break; \
2439
    case 1: flags = 0x2; break; \
2440
    default: case 2: flags = 0x3; break; \
2441
    } \
2442
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2443
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2444
} \
2445
void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2446
{ \
2447
    uint32_t flags; \
2448
    switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2449
    case 0: flags = 0x6; break; \
2450
    case -1: flags = 0x8; break; \
2451
    case 1: flags = 0x2; break; \
2452
    default: case 2: flags = 0x3; break; \
2453
    } \
2454
    env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2455
        | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2456
}
2457
DO_VFP_cmp(s, float32)
2458
DO_VFP_cmp(d, float64)
2459
#undef DO_VFP_cmp
2460

    
2461
/* Helper routines to perform bitwise copies between float and int.  */
2462
static inline float32 vfp_itos(uint32_t i)
2463
{
2464
    union {
2465
        uint32_t i;
2466
        float32 s;
2467
    } v;
2468

    
2469
    v.i = i;
2470
    return v.s;
2471
}
2472

    
2473
static inline uint32_t vfp_stoi(float32 s)
2474
{
2475
    union {
2476
        uint32_t i;
2477
        float32 s;
2478
    } v;
2479

    
2480
    v.s = s;
2481
    return v.i;
2482
}
2483

    
2484
static inline float64 vfp_itod(uint64_t i)
2485
{
2486
    union {
2487
        uint64_t i;
2488
        float64 d;
2489
    } v;
2490

    
2491
    v.i = i;
2492
    return v.d;
2493
}
2494

    
2495
static inline uint64_t vfp_dtoi(float64 d)
2496
{
2497
    union {
2498
        uint64_t i;
2499
        float64 d;
2500
    } v;
2501

    
2502
    v.d = d;
2503
    return v.i;
2504
}
2505

    
2506
/* Integer to float conversion.  */
2507
float32 VFP_HELPER(uito, s)(float32 x, CPUState *env)
2508
{
2509
    return uint32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2510
}
2511

    
2512
float64 VFP_HELPER(uito, d)(float32 x, CPUState *env)
2513
{
2514
    return uint32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2515
}
2516

    
2517
float32 VFP_HELPER(sito, s)(float32 x, CPUState *env)
2518
{
2519
    return int32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2520
}
2521

    
2522
float64 VFP_HELPER(sito, d)(float32 x, CPUState *env)
2523
{
2524
    return int32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2525
}
2526

    
2527
/* Float to integer conversion.  */
2528
float32 VFP_HELPER(toui, s)(float32 x, CPUState *env)
2529
{
2530
    return vfp_itos(float32_to_uint32(x, &env->vfp.fp_status));
2531
}
2532

    
2533
float32 VFP_HELPER(toui, d)(float64 x, CPUState *env)
2534
{
2535
    return vfp_itos(float64_to_uint32(x, &env->vfp.fp_status));
2536
}
2537

    
2538
float32 VFP_HELPER(tosi, s)(float32 x, CPUState *env)
2539
{
2540
    return vfp_itos(float32_to_int32(x, &env->vfp.fp_status));
2541
}
2542

    
2543
float32 VFP_HELPER(tosi, d)(float64 x, CPUState *env)
2544
{
2545
    return vfp_itos(float64_to_int32(x, &env->vfp.fp_status));
2546
}
2547

    
2548
float32 VFP_HELPER(touiz, s)(float32 x, CPUState *env)
2549
{
2550
    return vfp_itos(float32_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2551
}
2552

    
2553
float32 VFP_HELPER(touiz, d)(float64 x, CPUState *env)
2554
{
2555
    return vfp_itos(float64_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2556
}
2557

    
2558
float32 VFP_HELPER(tosiz, s)(float32 x, CPUState *env)
2559
{
2560
    return vfp_itos(float32_to_int32_round_to_zero(x, &env->vfp.fp_status));
2561
}
2562

    
2563
float32 VFP_HELPER(tosiz, d)(float64 x, CPUState *env)
2564
{
2565
    return vfp_itos(float64_to_int32_round_to_zero(x, &env->vfp.fp_status));
2566
}
2567

    
2568
/* floating point conversion */
2569
float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2570
{
2571
    return float32_to_float64(x, &env->vfp.fp_status);
2572
}
2573

    
2574
float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2575
{
2576
    return float64_to_float32(x, &env->vfp.fp_status);
2577
}
2578

    
2579
/* VFP3 fixed point conversion.  */
2580
#define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2581
ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2582
{ \
2583
    ftype tmp; \
2584
    tmp = sign##int32_to_##ftype ((itype)vfp_##p##toi(x), \
2585
                                  &env->vfp.fp_status); \
2586
    return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
2587
} \
2588
ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2589
{ \
2590
    ftype tmp; \
2591
    tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2592
    return vfp_ito##p((itype)ftype##_to_##sign##int32_round_to_zero(tmp, \
2593
        &env->vfp.fp_status)); \
2594
}
2595

    
2596
VFP_CONV_FIX(sh, d, float64, int16, )
2597
VFP_CONV_FIX(sl, d, float64, int32, )
2598
VFP_CONV_FIX(uh, d, float64, uint16, u)
2599
VFP_CONV_FIX(ul, d, float64, uint32, u)
2600
VFP_CONV_FIX(sh, s, float32, int16, )
2601
VFP_CONV_FIX(sl, s, float32, int32, )
2602
VFP_CONV_FIX(uh, s, float32, uint16, u)
2603
VFP_CONV_FIX(ul, s, float32, uint32, u)
2604
#undef VFP_CONV_FIX
2605

    
2606
/* Half precision conversions.  */
2607
float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2608
{
2609
    float_status *s = &env->vfp.fp_status;
2610
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2611
    return float16_to_float32(a, ieee, s);
2612
}
2613

    
2614
uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2615
{
2616
    float_status *s = &env->vfp.fp_status;
2617
    int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2618
    return float32_to_float16(a, ieee, s);
2619
}
2620

    
2621
float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2622
{
2623
    float_status *s = &env->vfp.fp_status;
2624
    float32 two = int32_to_float32(2, s);
2625
    return float32_sub(two, float32_mul(a, b, s), s);
2626
}
2627

    
2628
float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2629
{
2630
    float_status *s = &env->vfp.fp_status;
2631
    float32 three = int32_to_float32(3, s);
2632
    return float32_sub(three, float32_mul(a, b, s), s);
2633
}
2634

    
2635
/* NEON helpers.  */
2636

    
2637
/* TODO: The architecture specifies the value that the estimate functions
2638
   should return.  We return the exact reciprocal/root instead.  */
2639
float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2640
{
2641
    float_status *s = &env->vfp.fp_status;
2642
    float32 one = int32_to_float32(1, s);
2643
    return float32_div(one, a, s);
2644
}
2645

    
2646
float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2647
{
2648
    float_status *s = &env->vfp.fp_status;
2649
    float32 one = int32_to_float32(1, s);
2650
    return float32_div(one, float32_sqrt(a, s), s);
2651
}
2652

    
2653
uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
2654
{
2655
    float_status *s = &env->vfp.fp_status;
2656
    float32 tmp;
2657
    tmp = int32_to_float32(a, s);
2658
    tmp = float32_scalbn(tmp, -32, s);
2659
    tmp = helper_recpe_f32(tmp, env);
2660
    tmp = float32_scalbn(tmp, 31, s);
2661
    return float32_to_int32(tmp, s);
2662
}
2663

    
2664
uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
2665
{
2666
    float_status *s = &env->vfp.fp_status;
2667
    float32 tmp;
2668
    tmp = int32_to_float32(a, s);
2669
    tmp = float32_scalbn(tmp, -32, s);
2670
    tmp = helper_rsqrte_f32(tmp, env);
2671
    tmp = float32_scalbn(tmp, 31, s);
2672
    return float32_to_int32(tmp, s);
2673
}
2674

    
2675
void HELPER(set_teecr)(CPUState *env, uint32_t val)
2676
{
2677
    val &= 1;
2678
    if (env->teecr != val) {
2679
        env->teecr = val;
2680
        tb_flush(env);
2681
    }
2682
}