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

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

15 16
    return arm_feature(env, ARM_FEATURE_VFP);
}
A
aurel32 已提交
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
static int get_fpscr(QEMUFile *f, void *opaque, size_t size)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
    uint32_t val = qemu_get_be32(f);

    vfp_set_fpscr(env, val);
    return 0;
}

static void put_fpscr(QEMUFile *f, void *opaque, size_t size)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    qemu_put_be32(f, vfp_get_fpscr(env));
}

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

42 43
static const VMStateDescription vmstate_vfp = {
    .name = "cpu/vfp",
44 45
    .version_id = 3,
    .minimum_version_id = 3,
46
    .needed = vfp_needed,
47
    .fields = (VMStateField[]) {
48
        VMSTATE_FLOAT64_ARRAY(env.vfp.regs, ARMCPU, 64),
49 50 51 52 53 54 55 56 57 58 59 60 61 62
        /* 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,
        },
63
        VMSTATE_END_OF_LIST()
A
aurel32 已提交
64
    }
65
};
A
aurel32 已提交
66

67 68 69 70
static bool iwmmxt_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
A
aurel32 已提交
71

72 73
    return arm_feature(env, ARM_FEATURE_IWMMXT);
}
P
Paul Brook 已提交
74

75 76 77 78
static const VMStateDescription vmstate_iwmmxt = {
    .name = "cpu/iwmmxt",
    .version_id = 1,
    .minimum_version_id = 1,
79
    .needed = iwmmxt_needed,
80 81 82 83
    .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 已提交
84
    }
85 86 87 88 89 90 91 92
};

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

    return arm_feature(env, ARM_FEATURE_M);
A
aurel32 已提交
93 94
}

95
static const VMStateDescription vmstate_m = {
96 97 98
    .name = "cpu/m",
    .version_id = 1,
    .minimum_version_id = 1,
99
    .needed = m_needed,
100 101 102 103 104 105 106 107 108 109 110 111
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(env.v7m.other_sp, ARMCPU),
        VMSTATE_UINT32(env.v7m.vecbase, ARMCPU),
        VMSTATE_UINT32(env.v7m.basepri, ARMCPU),
        VMSTATE_UINT32(env.v7m.control, ARMCPU),
        VMSTATE_INT32(env.v7m.current_sp, ARMCPU),
        VMSTATE_INT32(env.v7m.exception, ARMCPU),
        VMSTATE_END_OF_LIST()
    }
};

static bool thumb2ee_needed(void *opaque)
A
aurel32 已提交
112
{
113 114
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
A
aurel32 已提交
115

116 117
    return arm_feature(env, ARM_FEATURE_THUMB2EE);
}
A
aurel32 已提交
118

119 120 121 122
static const VMStateDescription vmstate_thumb2ee = {
    .name = "cpu/thumb2ee",
    .version_id = 1,
    .minimum_version_id = 1,
123
    .needed = thumb2ee_needed,
124 125 126 127
    .fields = (VMStateField[]) {
        VMSTATE_UINT32(env.teecr, ARMCPU),
        VMSTATE_UINT32(env.teehbr, ARMCPU),
        VMSTATE_END_OF_LIST()
A
aurel32 已提交
128
    }
129 130
};

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static bool pmsav7_needed(void *opaque)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;

    return arm_feature(env, ARM_FEATURE_MPU) &&
           arm_feature(env, ARM_FEATURE_V7);
}

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

    return cpu->env.cp15.c6_rgnr < cpu->pmsav7_dregion;
}

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()
    }
};

164 165 166 167 168 169
static int get_cpsr(QEMUFile *f, void *opaque, size_t size)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
    uint32_t val = qemu_get_be32(f);

170 171 172 173 174 175 176
    env->aarch64 = ((val & PSTATE_nRW) == 0);

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

177
    cpsr_write(env, val, 0xffffffff, CPSRWriteRaw);
178 179
    return 0;
}
A
aurel32 已提交
180

181 182 183 184
static void put_cpsr(QEMUFile *f, void *opaque, size_t size)
{
    ARMCPU *cpu = opaque;
    CPUARMState *env = &cpu->env;
185 186 187 188 189 190 191
    uint32_t val;

    if (is_a64(env)) {
        val = pstate_read(env);
    } else {
        val = cpsr_read(env);
    }
A
aurel32 已提交
192

193
    qemu_put_be32(f, val);
194
}
A
aurel32 已提交
195

196 197 198 199 200 201
static const VMStateInfo vmstate_cpsr = {
    .name = "cpsr",
    .get = get_cpsr,
    .put = put_cpsr,
};

202 203 204 205
static void cpu_pre_save(void *opaque)
{
    ARMCPU *cpu = opaque;

206 207 208 209 210 211 212 213 214 215
    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();
        }
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }

    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++;
    }

254
    if (kvm_enabled()) {
255
        if (!write_list_to_kvmstate(cpu, KVM_PUT_FULL_STATE)) {
256 257 258 259 260 261 262 263 264 265 266
            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;
        }
267 268
    }

269
    hw_breakpoint_update_all(cpu);
270 271
    hw_watchpoint_update_all(cpu);

272 273 274
    return 0;
}

275 276
const VMStateDescription vmstate_arm_cpu = {
    .name = "cpu",
277 278
    .version_id = 22,
    .minimum_version_id = 22,
279 280
    .pre_save = cpu_pre_save,
    .post_load = cpu_post_load,
281 282
    .fields = (VMStateField[]) {
        VMSTATE_UINT32_ARRAY(env.regs, ARMCPU, 16),
283 284
        VMSTATE_UINT64_ARRAY(env.xregs, ARMCPU, 32),
        VMSTATE_UINT64(env.pc, ARMCPU),
285 286 287 288 289 290 291 292 293
        {
            .name = "cpsr",
            .version_id = 0,
            .size = sizeof(uint32_t),
            .info = &vmstate_cpsr,
            .flags = VMS_SINGLE,
            .offset = 0,
        },
        VMSTATE_UINT32(env.spsr, ARMCPU),
294
        VMSTATE_UINT64_ARRAY(env.banked_spsr, ARMCPU, 8),
295 296
        VMSTATE_UINT32_ARRAY(env.banked_r13, ARMCPU, 8),
        VMSTATE_UINT32_ARRAY(env.banked_r14, ARMCPU, 8),
297 298
        VMSTATE_UINT32_ARRAY(env.usr_regs, ARMCPU, 5),
        VMSTATE_UINT32_ARRAY(env.fiq_regs, ARMCPU, 5),
299
        VMSTATE_UINT64_ARRAY(env.elr_el, ARMCPU, 4),
300
        VMSTATE_UINT64_ARRAY(env.sp_el, ARMCPU, 4),
301 302 303
        /* The length-check must come before the arrays to avoid
         * incoming data possibly overflowing the array.
         */
304
        VMSTATE_INT32_POSITIVE_LE(cpreg_vmstate_array_len, ARMCPU),
305 306 307 308 309 310
        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),
311 312 313
        VMSTATE_UINT64(env.exclusive_addr, ARMCPU),
        VMSTATE_UINT64(env.exclusive_val, ARMCPU),
        VMSTATE_UINT64(env.exclusive_high, ARMCPU),
314
        VMSTATE_UINT64(env.features, ARMCPU),
315 316 317
        VMSTATE_UINT32(env.exception.syndrome, ARMCPU),
        VMSTATE_UINT32(env.exception.fsr, ARMCPU),
        VMSTATE_UINT64(env.exception.vaddress, ARMCPU),
318 319
        VMSTATE_TIMER_PTR(gt_timer[GTIMER_PHYS], ARMCPU),
        VMSTATE_TIMER_PTR(gt_timer[GTIMER_VIRT], ARMCPU),
320
        VMSTATE_BOOL(powered_off, ARMCPU),
321 322
        VMSTATE_END_OF_LIST()
    },
323 324 325 326 327
    .subsections = (const VMStateDescription*[]) {
        &vmstate_vfp,
        &vmstate_iwmmxt,
        &vmstate_m,
        &vmstate_thumb2ee,
328
        &vmstate_pmsav7,
329
        NULL
P
Paul Brook 已提交
330
    }
331
};
332 333 334 335 336 337 338 339

const char *gicv3_class_name(void)
{
    if (kvm_irqchip_in_kernel()) {
#ifdef TARGET_AARCH64
        return "kvm-arm-gicv3";
#else
        error_report("KVM GICv3 acceleration is not supported on this "
340
                     "platform");
341 342 343
#endif
    } else {
        /* TODO: Software emulation is not implemented yet */
344
        error_report("KVM is currently required for GICv3 emulation");
345 346 347 348
    }

    exit(1);
}