machine.c 16.4 KB
Newer Older
P
Peter Maydell 已提交
1
#include "qemu/osdep.h"
2 3
#include "qemu-common.h"
#include "cpu.h"
A
aurel32 已提交
4 5
#include "hw/hw.h"
#include "hw/boards.h"
6
#include "qemu/error-report.h"
7 8
#include "sysemu/kvm.h"
#include "kvm_arm.h"
9
#include "internals.h"
10
#include "migration/cpu.h"
A
aurel32 已提交
11

12
static bool vfp_needed(void *opaque)
A
aurel32 已提交
13
{
14 15
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
A
aurel32 已提交
16

17 18
    return arm_feature(env, ARM_FEATURE_VFP);
}
A
aurel32 已提交
19

J
Jianjun Duan 已提交
20 21
static int get_fpscr(QEMUFile *f, void *opaque, size_t size,
                     VMStateField *field)
22 23 24 25 26 27 28 29 30
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
    uint32_t val = qemu_get_be32(f);

    vfp_set_fpscr(env, val);
    return 0;
}

J
Jianjun Duan 已提交
31 32
static int put_fpscr(QEMUFile *f, void *opaque, size_t size,
                     VMStateField *field, QJSON *vmdesc)
33 34 35 36 37
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    qemu_put_be32(f, vfp_get_fpscr(env));
J
Jianjun Duan 已提交
38
    return 0;
39 40 41 42 43 44 45 46
}

static const VMStateInfo vmstate_fpscr = {
    .name = "fpscr",
    .get = get_fpscr,
    .put = put_fpscr,
};

47 48
static const VMStateDescription vmstate_vfp = {
    .name = "cpu/vfp",
49 50
    .version_id = 3,
    .minimum_version_id = 3,
51
    .needed = vfp_needed,
52
    .fields = (VMStateField[]) {
53
        VMSTATE_FLOAT64_ARRAY(env.vfp.regs, ARMCPU, 64),
54 55 56 57 58 59 60 61 62 63 64 65 66 67
        /* The xregs array is a little awkward because element 1 (FPSCR)
         * requires a specific accessor, so we have to split it up in
         * the vmstate:
         */
        VMSTATE_UINT32(env.vfp.xregs[0], ARMCPU),
        VMSTATE_UINT32_SUB_ARRAY(env.vfp.xregs, ARMCPU, 2, 14),
        {
            .name = "fpscr",
            .version_id = 0,
            .size = sizeof(uint32_t),
            .info = &vmstate_fpscr,
            .flags = VMS_SINGLE,
            .offset = 0,
        },
68
        VMSTATE_END_OF_LIST()
A
aurel32 已提交
69
    }
70
};
A
aurel32 已提交
71

72 73 74 75
static bool iwmmxt_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
A
aurel32 已提交
76

77 78
    return arm_feature(env, ARM_FEATURE_IWMMXT);
}
P
Paul Brook 已提交
79

80 81 82 83
static const VMStateDescription vmstate_iwmmxt = {
    .name = "cpu/iwmmxt",
    .version_id = 1,
    .minimum_version_id = 1,
84
    .needed = iwmmxt_needed,
85 86 87 88
    .fields = (VMStateField[]) {
        VMSTATE_UINT64_ARRAY(env.iwmmxt.regs, ARMCPU, 16),
        VMSTATE_UINT32_ARRAY(env.iwmmxt.cregs, ARMCPU, 16),
        VMSTATE_END_OF_LIST()
P
Paul Brook 已提交
89
    }
90 91 92 93 94 95 96 97
};

static bool m_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    return arm_feature(env, ARM_FEATURE_M);
A
aurel32 已提交
98 99
}

100 101 102 103 104
static const VMStateDescription vmstate_m_faultmask_primask = {
    .name = "cpu/m/faultmask-primask",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
105
        VMSTATE_UINT32(env.v7m.faultmask[M_REG_NS], ARMCPU),
106
        VMSTATE_UINT32(env.v7m.primask[M_REG_NS], ARMCPU),
107 108 109 110
        VMSTATE_END_OF_LIST()
    }
};

111
static const VMStateDescription vmstate_m = {
112
    .name = "cpu/m",
113 114
    .version_id = 4,
    .minimum_version_id = 4,
115
    .needed = m_needed,
116
    .fields = (VMStateField[]) {
117
        VMSTATE_UINT32(env.v7m.vecbase[M_REG_NS], ARMCPU),
118
        VMSTATE_UINT32(env.v7m.basepri[M_REG_NS], ARMCPU),
119
        VMSTATE_UINT32(env.v7m.control[M_REG_NS], ARMCPU),
120
        VMSTATE_UINT32(env.v7m.ccr[M_REG_NS], ARMCPU),
121 122 123 124 125
        VMSTATE_UINT32(env.v7m.cfsr, ARMCPU),
        VMSTATE_UINT32(env.v7m.hfsr, ARMCPU),
        VMSTATE_UINT32(env.v7m.dfsr, ARMCPU),
        VMSTATE_UINT32(env.v7m.mmfar, ARMCPU),
        VMSTATE_UINT32(env.v7m.bfar, ARMCPU),
126
        VMSTATE_UINT32(env.v7m.mpu_ctrl[M_REG_NS], ARMCPU),
127 128
        VMSTATE_INT32(env.v7m.exception, ARMCPU),
        VMSTATE_END_OF_LIST()
129 130 131 132
    },
    .subsections = (const VMStateDescription*[]) {
        &vmstate_m_faultmask_primask,
        NULL
133 134 135 136
    }
};

static bool thumb2ee_needed(void *opaque)
A
aurel32 已提交
137
{
138 139
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
A
aurel32 已提交
140

141 142
    return arm_feature(env, ARM_FEATURE_THUMB2EE);
}
A
aurel32 已提交
143

144 145 146 147
static const VMStateDescription vmstate_thumb2ee = {
    .name = "cpu/thumb2ee",
    .version_id = 1,
    .minimum_version_id = 1,
148
    .needed = thumb2ee_needed,
149 150 151 152
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(env.teecr, ARMCPU),
        VMSTATE_UINT32(env.teehbr, ARMCPU),
        VMSTATE_END_OF_LIST()
A
aurel32 已提交
153
    }
154 155
};

156 157 158 159 160
static bool pmsav7_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

161
    return arm_feature(env, ARM_FEATURE_PMSA) &&
162 163
           arm_feature(env, ARM_FEATURE_V7) &&
           !arm_feature(env, ARM_FEATURE_V8);
164 165 166 167 168 169
}

static bool pmsav7_rgnr_vmstate_validate(void *opaque, int version_id)
{
    ARMCPU *cpu = opaque;

170
    return cpu->env.pmsav7.rnr[M_REG_NS] < cpu->pmsav7_dregion;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

static const VMStateDescription vmstate_pmsav7 = {
    .name = "cpu/pmsav7",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = pmsav7_needed,
    .fields = (VMStateField[]) {
        VMSTATE_VARRAY_UINT32(env.pmsav7.drbar, ARMCPU, pmsav7_dregion, 0,
                              vmstate_info_uint32, uint32_t),
        VMSTATE_VARRAY_UINT32(env.pmsav7.drsr, ARMCPU, pmsav7_dregion, 0,
                              vmstate_info_uint32, uint32_t),
        VMSTATE_VARRAY_UINT32(env.pmsav7.dracr, ARMCPU, pmsav7_dregion, 0,
                              vmstate_info_uint32, uint32_t),
        VMSTATE_VALIDATE("rgnr is valid", pmsav7_rgnr_vmstate_validate),
        VMSTATE_END_OF_LIST()
    }
};

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
static bool pmsav7_rnr_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    /* For R profile cores pmsav7.rnr is migrated via the cpreg
     * "RGNR" definition in helper.h. For M profile we have to
     * migrate it separately.
     */
    return arm_feature(env, ARM_FEATURE_M);
}

static const VMStateDescription vmstate_pmsav7_rnr = {
    .name = "cpu/pmsav7-rnr",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = pmsav7_rnr_needed,
    .fields = (VMStateField[]) {
208
        VMSTATE_UINT32(env.pmsav7.rnr[M_REG_NS], ARMCPU),
209 210 211 212
        VMSTATE_END_OF_LIST()
    }
};

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
static bool pmsav8_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    return arm_feature(env, ARM_FEATURE_PMSA) &&
        arm_feature(env, ARM_FEATURE_V8);
}

static const VMStateDescription vmstate_pmsav8 = {
    .name = "cpu/pmsav8",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = pmsav8_needed,
    .fields = (VMStateField[]) {
228 229 230 231
        VMSTATE_VARRAY_UINT32(env.pmsav8.rbar[M_REG_NS], ARMCPU, pmsav7_dregion,
                              0, vmstate_info_uint32, uint32_t),
        VMSTATE_VARRAY_UINT32(env.pmsav8.rlar[M_REG_NS], ARMCPU, pmsav7_dregion,
                              0, vmstate_info_uint32, uint32_t),
232 233
        VMSTATE_UINT32(env.pmsav8.mair0[M_REG_NS], ARMCPU),
        VMSTATE_UINT32(env.pmsav8.mair1[M_REG_NS], ARMCPU),
234 235 236 237
        VMSTATE_END_OF_LIST()
    }
};

238 239 240 241 242 243 244
static bool s_rnr_vmstate_validate(void *opaque, int version_id)
{
    ARMCPU *cpu = opaque;

    return cpu->env.pmsav7.rnr[M_REG_S] < cpu->pmsav7_dregion;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
static bool m_security_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    return arm_feature(env, ARM_FEATURE_M_SECURITY);
}

static const VMStateDescription vmstate_m_security = {
    .name = "cpu/m-security",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = m_security_needed,
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(env.v7m.secure, ARMCPU),
260
        VMSTATE_UINT32(env.v7m.basepri[M_REG_S], ARMCPU),
261
        VMSTATE_UINT32(env.v7m.primask[M_REG_S], ARMCPU),
262
        VMSTATE_UINT32(env.v7m.faultmask[M_REG_S], ARMCPU),
263
        VMSTATE_UINT32(env.v7m.control[M_REG_S], ARMCPU),
264
        VMSTATE_UINT32(env.v7m.vecbase[M_REG_S], ARMCPU),
265 266
        VMSTATE_UINT32(env.pmsav8.mair0[M_REG_S], ARMCPU),
        VMSTATE_UINT32(env.pmsav8.mair1[M_REG_S], ARMCPU),
267 268 269 270
        VMSTATE_VARRAY_UINT32(env.pmsav8.rbar[M_REG_S], ARMCPU, pmsav7_dregion,
                              0, vmstate_info_uint32, uint32_t),
        VMSTATE_VARRAY_UINT32(env.pmsav8.rlar[M_REG_S], ARMCPU, pmsav7_dregion,
                              0, vmstate_info_uint32, uint32_t),
271 272
        VMSTATE_UINT32(env.pmsav7.rnr[M_REG_S], ARMCPU),
        VMSTATE_VALIDATE("secure MPU_RNR is valid", s_rnr_vmstate_validate),
273
        VMSTATE_UINT32(env.v7m.mpu_ctrl[M_REG_S], ARMCPU),
274
        VMSTATE_UINT32(env.v7m.ccr[M_REG_S], ARMCPU),
275 276 277 278
        VMSTATE_END_OF_LIST()
    }
};

J
Jianjun Duan 已提交
279 280
static int get_cpsr(QEMUFile *f, void *opaque, size_t size,
                    VMStateField *field)
281 282 283 284 285
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
    uint32_t val = qemu_get_be32(f);

286
    if (arm_feature(env, ARM_FEATURE_M)) {
287 288 289 290 291 292 293 294
        if (val & XPSR_EXCP) {
            /* This is a CPSR format value from an older QEMU. (We can tell
             * because values transferred in XPSR format always have zero
             * for the EXCP field, and CPSR format will always have bit 4
             * set in CPSR_M.) Rearrange it into XPSR format. The significant
             * differences are that the T bit is not in the same place, the
             * primask/faultmask info may be in the CPSR I and F bits, and
             * we do not want the mode bits.
295 296
             * We know that this cleanup happened before v8M, so there
             * is no complication with banked primask/faultmask.
297 298 299
             */
            uint32_t newval = val;

300 301
            assert(!arm_feature(env, ARM_FEATURE_M_SECURITY));

302 303 304 305 306 307 308 309 310 311
            newval &= (CPSR_NZCV | CPSR_Q | CPSR_IT | CPSR_GE);
            if (val & CPSR_T) {
                newval |= XPSR_T;
            }
            /* If the I or F bits are set then this is a migration from
             * an old QEMU which still stored the M profile FAULTMASK
             * and PRIMASK in env->daif. For a new QEMU, the data is
             * transferred using the vmstate_m_faultmask_primask subsection.
             */
            if (val & CPSR_F) {
312
                env->v7m.faultmask[M_REG_NS] = 1;
313 314
            }
            if (val & CPSR_I) {
315
                env->v7m.primask[M_REG_NS] = 1;
316 317
            }
            val = newval;
318
        }
319 320 321
        /* Ignore the low bits, they are handled by vmstate_m. */
        xpsr_write(env, val, ~XPSR_EXCP);
        return 0;
322 323
    }

324 325 326 327 328 329 330
    env->aarch64 = ((val & PSTATE_nRW) == 0);

    if (is_a64(env)) {
        pstate_write(env, val);
        return 0;
    }

331
    cpsr_write(env, val, 0xffffffff, CPSRWriteRaw);
332 333
    return 0;
}
A
aurel32 已提交
334

J
Jianjun Duan 已提交
335 336
static int put_cpsr(QEMUFile *f, void *opaque, size_t size,
                    VMStateField *field, QJSON *vmdesc)
337 338 339
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
340 341
    uint32_t val;

342 343 344 345
    if (arm_feature(env, ARM_FEATURE_M)) {
        /* The low 9 bits are v7m.exception, which is handled by vmstate_m. */
        val = xpsr_read(env) & ~XPSR_EXCP;
    } else if (is_a64(env)) {
346 347 348 349
        val = pstate_read(env);
    } else {
        val = cpsr_read(env);
    }
A
aurel32 已提交
350

351
    qemu_put_be32(f, val);
J
Jianjun Duan 已提交
352
    return 0;
353
}
A
aurel32 已提交
354

355 356 357 358 359 360
static const VMStateInfo vmstate_cpsr = {
    .name = "cpsr",
    .get = get_cpsr,
    .put = put_cpsr,
};

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static int get_power(QEMUFile *f, void *opaque, size_t size,
                    VMStateField *field)
{
    ARMCPU *cpu = opaque;
    bool powered_off = qemu_get_byte(f);
    cpu->power_state = powered_off ? PSCI_OFF : PSCI_ON;
    return 0;
}

static int put_power(QEMUFile *f, void *opaque, size_t size,
                    VMStateField *field, QJSON *vmdesc)
{
    ARMCPU *cpu = opaque;

    /* Migration should never happen while we transition power states */

    if (cpu->power_state == PSCI_ON ||
        cpu->power_state == PSCI_OFF) {
        bool powered_off = (cpu->power_state == PSCI_OFF) ? true : false;
        qemu_put_byte(f, powered_off);
        return 0;
    } else {
        return 1;
    }
}

static const VMStateInfo vmstate_powered_off = {
    .name = "powered_off",
    .get = get_power,
    .put = put_power,
};

393 394 395 396
static void cpu_pre_save(void *opaque)
{
    ARMCPU *cpu = opaque;

397 398 399 400 401 402 403 404 405 406
    if (kvm_enabled()) {
        if (!write_kvmstate_to_list(cpu)) {
            /* This should never fail */
            abort();
        }
    } else {
        if (!write_cpustate_to_list(cpu)) {
            /* This should never fail. */
            abort();
        }
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    }

    cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
    memcpy(cpu->cpreg_vmstate_indexes, cpu->cpreg_indexes,
           cpu->cpreg_array_len * sizeof(uint64_t));
    memcpy(cpu->cpreg_vmstate_values, cpu->cpreg_values,
           cpu->cpreg_array_len * sizeof(uint64_t));
}

static int cpu_post_load(void *opaque, int version_id)
{
    ARMCPU *cpu = opaque;
    int i, v;

    /* Update the values list from the incoming migration data.
     * Anything in the incoming data which we don't know about is
     * a migration failure; anything we know about but the incoming
     * data doesn't specify retains its current (reset) value.
     * The indexes list remains untouched -- we only inspect the
     * incoming migration index list so we can match the values array
     * entries with the right slots in our own values array.
     */

    for (i = 0, v = 0; i < cpu->cpreg_array_len
             && v < cpu->cpreg_vmstate_array_len; i++) {
        if (cpu->cpreg_vmstate_indexes[v] > cpu->cpreg_indexes[i]) {
            /* register in our list but not incoming : skip it */
            continue;
        }
        if (cpu->cpreg_vmstate_indexes[v] < cpu->cpreg_indexes[i]) {
            /* register in their list but not ours: fail migration */
            return -1;
        }
        /* matching register, copy the value over */
        cpu->cpreg_values[i] = cpu->cpreg_vmstate_values[v];
        v++;
    }

445
    if (kvm_enabled()) {
446
        if (!write_list_to_kvmstate(cpu, KVM_PUT_FULL_STATE)) {
447 448 449 450 451 452 453 454 455 456 457
            return -1;
        }
        /* Note that it's OK for the TCG side not to know about
         * every register in the list; KVM is authoritative if
         * we're using it.
         */
        write_list_to_cpustate(cpu);
    } else {
        if (!write_list_to_cpustate(cpu)) {
            return -1;
        }
458 459
    }

460
    hw_breakpoint_update_all(cpu);
461 462
    hw_watchpoint_update_all(cpu);

463 464 465
    return 0;
}

466 467
const VMStateDescription vmstate_arm_cpu = {
    .name = "cpu",
468 469
    .version_id = 22,
    .minimum_version_id = 22,
470 471
    .pre_save = cpu_pre_save,
    .post_load = cpu_post_load,
472 473
    .fields = (VMStateField[]) {
        VMSTATE_UINT32_ARRAY(env.regs, ARMCPU, 16),
474 475
        VMSTATE_UINT64_ARRAY(env.xregs, ARMCPU, 32),
        VMSTATE_UINT64(env.pc, ARMCPU),
476 477 478 479 480 481 482 483 484
        {
            .name = "cpsr",
            .version_id = 0,
            .size = sizeof(uint32_t),
            .info = &vmstate_cpsr,
            .flags = VMS_SINGLE,
            .offset = 0,
        },
        VMSTATE_UINT32(env.spsr, ARMCPU),
485
        VMSTATE_UINT64_ARRAY(env.banked_spsr, ARMCPU, 8),
486 487
        VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 8),
        VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 8),
488 489
        VMSTATE_UINT32_ARRAY(env.usr_regs, ARMCPU, 5),
        VMSTATE_UINT32_ARRAY(env.fiq_regs, ARMCPU, 5),
490
        VMSTATE_UINT64_ARRAY(env.elr_el, ARMCPU, 4),
491
        VMSTATE_UINT64_ARRAY(env.sp_el, ARMCPU, 4),
492 493 494
        /* The length-check must come before the arrays to avoid
         * incoming data possibly overflowing the array.
         */
495
        VMSTATE_INT32_POSITIVE_LE(cpreg_vmstate_array_len, ARMCPU),
496 497 498 499 500 501
        VMSTATE_VARRAY_INT32(cpreg_vmstate_indexes, ARMCPU,
                             cpreg_vmstate_array_len,
                             0, vmstate_info_uint64, uint64_t),
        VMSTATE_VARRAY_INT32(cpreg_vmstate_values, ARMCPU,
                             cpreg_vmstate_array_len,
                             0, vmstate_info_uint64, uint64_t),
502 503 504
        VMSTATE_UINT64(env.exclusive_addr, ARMCPU),
        VMSTATE_UINT64(env.exclusive_val, ARMCPU),
        VMSTATE_UINT64(env.exclusive_high, ARMCPU),
505
        VMSTATE_UINT64(env.features, ARMCPU),
506 507 508
        VMSTATE_UINT32(env.exception.syndrome, ARMCPU),
        VMSTATE_UINT32(env.exception.fsr, ARMCPU),
        VMSTATE_UINT64(env.exception.vaddress, ARMCPU),
509 510
        VMSTATE_TIMER_PTR(gt_timer[GTIMER_PHYS], ARMCPU),
        VMSTATE_TIMER_PTR(gt_timer[GTIMER_VIRT], ARMCPU),
511 512 513 514 515 516 517 518
        {
            .name = "power_state",
            .version_id = 0,
            .size = sizeof(bool),
            .info = &vmstate_powered_off,
            .flags = VMS_SINGLE,
            .offset = 0,
        },
519 520
        VMSTATE_END_OF_LIST()
    },
521 522 523 524 525
    .subsections = (const VMStateDescription*[]) {
        &vmstate_vfp,
        &vmstate_iwmmxt,
        &vmstate_m,
        &vmstate_thumb2ee,
526 527 528 529 530
        /* pmsav7_rnr must come before pmsav7 so that we have the
         * region number before we test it in the VMSTATE_VALIDATE
         * in vmstate_pmsav7.
         */
        &vmstate_pmsav7_rnr,
531
        &vmstate_pmsav7,
532
        &vmstate_pmsav8,
533
        &vmstate_m_security,
534
        NULL
P
Paul Brook 已提交
535
    }
536
};