~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to .pc/ubuntu/arm64/0007-target-arm-Allow-secondary-KVM-CPUs-to-be-booted-via.patch/target-arm/kvm.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-04 12:13:08 UTC
  • mfrom: (10.1.45 sid)
  • Revision ID: package-import@ubuntu.com-20140204121308-1xq92lrfs75agw2g
Tags: 1.7.0+dfsg-3ubuntu1~ppa1
* Merge 1.7.0+dfsg-3 from debian.  Remaining changes:
  - debian/patches/ubuntu:
    * expose-vmx_qemu64cpu.patch
    * linaro (omap3) and arm64 patches
    * ubuntu/target-ppc-add-stubs-for-kvm-breakpoints: fix FTBFS
      on ppc
    * ubuntu/CVE-2013-4377.patch: fix denial of service via virtio
  - debian/qemu-system-x86.modprobe: set kvm_intel nested=1 options
  - debian/control:
    * add arm64 to Architectures
    * add qemu-common and qemu-system-aarch64 packages
  - debian/qemu-system-common.install: add debian/tmp/usr/lib
  - debian/qemu-system-common.preinst: add kvm group
  - debian/qemu-system-common.postinst: remove acl placed by udev,
    and add udevadm trigger.
  - qemu-system-x86.links: add eepro100.rom, remove pxe-virtio,
    pxe-e1000 and pxe-rtl8139.
  - add qemu-system-x86.qemu-kvm.upstart and .default
  - qemu-user-static.postinst-in: remove arm64 binfmt
  - debian/rules:
    * allow parallel build
    * add aarch64 to system_targets and sys_systems
    * add qemu-kvm-spice links
    * install qemu-system-x86.modprobe
  - add debian/qemu-system-common.links for OVMF.fd link
* Remove kvm-img, kvm-nbd, kvm-ifup and kvm-ifdown symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ARM implementation of KVM hooks
 
3
 *
 
4
 * Copyright Christoffer Dall 2009-2010
 
5
 *
 
6
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 
7
 * See the COPYING file in the top-level directory.
 
8
 *
 
9
 */
 
10
 
 
11
#include <stdio.h>
 
12
#include <sys/types.h>
 
13
#include <sys/ioctl.h>
 
14
#include <sys/mman.h>
 
15
 
 
16
#include <linux/kvm.h>
 
17
 
 
18
#include "qemu-common.h"
 
19
#include "qemu/timer.h"
 
20
#include "sysemu/sysemu.h"
 
21
#include "sysemu/kvm.h"
 
22
#include "kvm_arm.h"
 
23
#include "cpu.h"
 
24
#include "hw/arm/arm.h"
 
25
 
 
26
const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
 
27
    KVM_CAP_LAST_INFO
 
28
};
 
29
 
 
30
int kvm_arch_init(KVMState *s)
 
31
{
 
32
    /* For ARM interrupt delivery is always asynchronous,
 
33
     * whether we are using an in-kernel VGIC or not.
 
34
     */
 
35
    kvm_async_interrupts_allowed = true;
 
36
    return 0;
 
37
}
 
38
 
 
39
unsigned long kvm_arch_vcpu_id(CPUState *cpu)
 
40
{
 
41
    return cpu->cpu_index;
 
42
}
 
43
 
 
44
static bool reg_syncs_via_tuple_list(uint64_t regidx)
 
45
{
 
46
    /* Return true if the regidx is a register we should synchronize
 
47
     * via the cpreg_tuples array (ie is not a core reg we sync by
 
48
     * hand in kvm_arch_get/put_registers())
 
49
     */
 
50
    switch (regidx & KVM_REG_ARM_COPROC_MASK) {
 
51
    case KVM_REG_ARM_CORE:
 
52
    case KVM_REG_ARM_VFP:
 
53
        return false;
 
54
    default:
 
55
        return true;
 
56
    }
 
57
}
 
58
 
 
59
static int compare_u64(const void *a, const void *b)
 
60
{
 
61
    if (*(uint64_t *)a > *(uint64_t *)b) {
 
62
        return 1;
 
63
    }
 
64
    if (*(uint64_t *)a < *(uint64_t *)b) {
 
65
        return -1;
 
66
    }
 
67
    return 0;
 
68
}
 
69
 
 
70
int kvm_arch_init_vcpu(CPUState *cs)
 
71
{
 
72
    struct kvm_vcpu_init init;
 
73
    int i, ret, arraylen;
 
74
    uint64_t v;
 
75
    struct kvm_one_reg r;
 
76
    struct kvm_reg_list rl;
 
77
    struct kvm_reg_list *rlp;
 
78
    ARMCPU *cpu = ARM_CPU(cs);
 
79
 
 
80
    init.target = KVM_ARM_TARGET_CORTEX_A15;
 
81
    memset(init.features, 0, sizeof(init.features));
 
82
    ret = kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init);
 
83
    if (ret) {
 
84
        return ret;
 
85
    }
 
86
    /* Query the kernel to make sure it supports 32 VFP
 
87
     * registers: QEMU's "cortex-a15" CPU is always a
 
88
     * VFP-D32 core. The simplest way to do this is just
 
89
     * to attempt to read register d31.
 
90
     */
 
91
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP | 31;
 
92
    r.addr = (uintptr_t)(&v);
 
93
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
94
    if (ret == -ENOENT) {
 
95
        return -EINVAL;
 
96
    }
 
97
 
 
98
    /* Populate the cpreg list based on the kernel's idea
 
99
     * of what registers exist (and throw away the TCG-created list).
 
100
     */
 
101
    rl.n = 0;
 
102
    ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl);
 
103
    if (ret != -E2BIG) {
 
104
        return ret;
 
105
    }
 
106
    rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t));
 
107
    rlp->n = rl.n;
 
108
    ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp);
 
109
    if (ret) {
 
110
        goto out;
 
111
    }
 
112
    /* Sort the list we get back from the kernel, since cpreg_tuples
 
113
     * must be in strictly ascending order.
 
114
     */
 
115
    qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64);
 
116
 
 
117
    for (i = 0, arraylen = 0; i < rlp->n; i++) {
 
118
        if (!reg_syncs_via_tuple_list(rlp->reg[i])) {
 
119
            continue;
 
120
        }
 
121
        switch (rlp->reg[i] & KVM_REG_SIZE_MASK) {
 
122
        case KVM_REG_SIZE_U32:
 
123
        case KVM_REG_SIZE_U64:
 
124
            break;
 
125
        default:
 
126
            fprintf(stderr, "Can't handle size of register in kernel list\n");
 
127
            ret = -EINVAL;
 
128
            goto out;
 
129
        }
 
130
 
 
131
        arraylen++;
 
132
    }
 
133
 
 
134
    cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen);
 
135
    cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen);
 
136
    cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes,
 
137
                                         arraylen);
 
138
    cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values,
 
139
                                        arraylen);
 
140
    cpu->cpreg_array_len = arraylen;
 
141
    cpu->cpreg_vmstate_array_len = arraylen;
 
142
 
 
143
    for (i = 0, arraylen = 0; i < rlp->n; i++) {
 
144
        uint64_t regidx = rlp->reg[i];
 
145
        if (!reg_syncs_via_tuple_list(regidx)) {
 
146
            continue;
 
147
        }
 
148
        cpu->cpreg_indexes[arraylen] = regidx;
 
149
        arraylen++;
 
150
    }
 
151
    assert(cpu->cpreg_array_len == arraylen);
 
152
 
 
153
    if (!write_kvmstate_to_list(cpu)) {
 
154
        /* Shouldn't happen unless kernel is inconsistent about
 
155
         * what registers exist.
 
156
         */
 
157
        fprintf(stderr, "Initial read of kernel register state failed\n");
 
158
        ret = -EINVAL;
 
159
        goto out;
 
160
    }
 
161
 
 
162
    /* Save a copy of the initial register values so that we can
 
163
     * feed it back to the kernel on VCPU reset.
 
164
     */
 
165
    cpu->cpreg_reset_values = g_memdup(cpu->cpreg_values,
 
166
                                       cpu->cpreg_array_len *
 
167
                                       sizeof(cpu->cpreg_values[0]));
 
168
 
 
169
out:
 
170
    g_free(rlp);
 
171
    return ret;
 
172
}
 
173
 
 
174
/* We track all the KVM devices which need their memory addresses
 
175
 * passing to the kernel in a list of these structures.
 
176
 * When board init is complete we run through the list and
 
177
 * tell the kernel the base addresses of the memory regions.
 
178
 * We use a MemoryListener to track mapping and unmapping of
 
179
 * the regions during board creation, so the board models don't
 
180
 * need to do anything special for the KVM case.
 
181
 */
 
182
typedef struct KVMDevice {
 
183
    struct kvm_arm_device_addr kda;
 
184
    MemoryRegion *mr;
 
185
    QSLIST_ENTRY(KVMDevice) entries;
 
186
} KVMDevice;
 
187
 
 
188
static QSLIST_HEAD(kvm_devices_head, KVMDevice) kvm_devices_head;
 
189
 
 
190
static void kvm_arm_devlistener_add(MemoryListener *listener,
 
191
                                    MemoryRegionSection *section)
 
192
{
 
193
    KVMDevice *kd;
 
194
 
 
195
    QSLIST_FOREACH(kd, &kvm_devices_head, entries) {
 
196
        if (section->mr == kd->mr) {
 
197
            kd->kda.addr = section->offset_within_address_space;
 
198
        }
 
199
    }
 
200
}
 
201
 
 
202
static void kvm_arm_devlistener_del(MemoryListener *listener,
 
203
                                    MemoryRegionSection *section)
 
204
{
 
205
    KVMDevice *kd;
 
206
 
 
207
    QSLIST_FOREACH(kd, &kvm_devices_head, entries) {
 
208
        if (section->mr == kd->mr) {
 
209
            kd->kda.addr = -1;
 
210
        }
 
211
    }
 
212
}
 
213
 
 
214
static MemoryListener devlistener = {
 
215
    .region_add = kvm_arm_devlistener_add,
 
216
    .region_del = kvm_arm_devlistener_del,
 
217
};
 
218
 
 
219
static void kvm_arm_machine_init_done(Notifier *notifier, void *data)
 
220
{
 
221
    KVMDevice *kd, *tkd;
 
222
 
 
223
    memory_listener_unregister(&devlistener);
 
224
    QSLIST_FOREACH_SAFE(kd, &kvm_devices_head, entries, tkd) {
 
225
        if (kd->kda.addr != -1) {
 
226
            if (kvm_vm_ioctl(kvm_state, KVM_ARM_SET_DEVICE_ADDR,
 
227
                             &kd->kda) < 0) {
 
228
                fprintf(stderr, "KVM_ARM_SET_DEVICE_ADDRESS failed: %s\n",
 
229
                        strerror(errno));
 
230
                abort();
 
231
            }
 
232
        }
 
233
        memory_region_unref(kd->mr);
 
234
        g_free(kd);
 
235
    }
 
236
}
 
237
 
 
238
static Notifier notify = {
 
239
    .notify = kvm_arm_machine_init_done,
 
240
};
 
241
 
 
242
void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid)
 
243
{
 
244
    KVMDevice *kd;
 
245
 
 
246
    if (!kvm_irqchip_in_kernel()) {
 
247
        return;
 
248
    }
 
249
 
 
250
    if (QSLIST_EMPTY(&kvm_devices_head)) {
 
251
        memory_listener_register(&devlistener, NULL);
 
252
        qemu_add_machine_init_done_notifier(&notify);
 
253
    }
 
254
    kd = g_new0(KVMDevice, 1);
 
255
    kd->mr = mr;
 
256
    kd->kda.id = devid;
 
257
    kd->kda.addr = -1;
 
258
    QSLIST_INSERT_HEAD(&kvm_devices_head, kd, entries);
 
259
    memory_region_ref(kd->mr);
 
260
}
 
261
 
 
262
bool write_kvmstate_to_list(ARMCPU *cpu)
 
263
{
 
264
    CPUState *cs = CPU(cpu);
 
265
    int i;
 
266
    bool ok = true;
 
267
 
 
268
    for (i = 0; i < cpu->cpreg_array_len; i++) {
 
269
        struct kvm_one_reg r;
 
270
        uint64_t regidx = cpu->cpreg_indexes[i];
 
271
        uint32_t v32;
 
272
        int ret;
 
273
 
 
274
        r.id = regidx;
 
275
 
 
276
        switch (regidx & KVM_REG_SIZE_MASK) {
 
277
        case KVM_REG_SIZE_U32:
 
278
            r.addr = (uintptr_t)&v32;
 
279
            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
280
            if (!ret) {
 
281
                cpu->cpreg_values[i] = v32;
 
282
            }
 
283
            break;
 
284
        case KVM_REG_SIZE_U64:
 
285
            r.addr = (uintptr_t)(cpu->cpreg_values + i);
 
286
            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
287
            break;
 
288
        default:
 
289
            abort();
 
290
        }
 
291
        if (ret) {
 
292
            ok = false;
 
293
        }
 
294
    }
 
295
    return ok;
 
296
}
 
297
 
 
298
bool write_list_to_kvmstate(ARMCPU *cpu)
 
299
{
 
300
    CPUState *cs = CPU(cpu);
 
301
    int i;
 
302
    bool ok = true;
 
303
 
 
304
    for (i = 0; i < cpu->cpreg_array_len; i++) {
 
305
        struct kvm_one_reg r;
 
306
        uint64_t regidx = cpu->cpreg_indexes[i];
 
307
        uint32_t v32;
 
308
        int ret;
 
309
 
 
310
        r.id = regidx;
 
311
        switch (regidx & KVM_REG_SIZE_MASK) {
 
312
        case KVM_REG_SIZE_U32:
 
313
            v32 = cpu->cpreg_values[i];
 
314
            r.addr = (uintptr_t)&v32;
 
315
            break;
 
316
        case KVM_REG_SIZE_U64:
 
317
            r.addr = (uintptr_t)(cpu->cpreg_values + i);
 
318
            break;
 
319
        default:
 
320
            abort();
 
321
        }
 
322
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
323
        if (ret) {
 
324
            /* We might fail for "unknown register" and also for
 
325
             * "you tried to set a register which is constant with
 
326
             * a different value from what it actually contains".
 
327
             */
 
328
            ok = false;
 
329
        }
 
330
    }
 
331
    return ok;
 
332
}
 
333
 
 
334
typedef struct Reg {
 
335
    uint64_t id;
 
336
    int offset;
 
337
} Reg;
 
338
 
 
339
#define COREREG(KERNELNAME, QEMUFIELD)                       \
 
340
    {                                                        \
 
341
        KVM_REG_ARM | KVM_REG_SIZE_U32 |                     \
 
342
        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(KERNELNAME), \
 
343
        offsetof(CPUARMState, QEMUFIELD)                     \
 
344
    }
 
345
 
 
346
#define VFPSYSREG(R)                                       \
 
347
    {                                                      \
 
348
        KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | \
 
349
        KVM_REG_ARM_VFP_##R,                               \
 
350
        offsetof(CPUARMState, vfp.xregs[ARM_VFP_##R])      \
 
351
    }
 
352
 
 
353
static const Reg regs[] = {
 
354
    /* R0_usr .. R14_usr */
 
355
    COREREG(usr_regs.uregs[0], regs[0]),
 
356
    COREREG(usr_regs.uregs[1], regs[1]),
 
357
    COREREG(usr_regs.uregs[2], regs[2]),
 
358
    COREREG(usr_regs.uregs[3], regs[3]),
 
359
    COREREG(usr_regs.uregs[4], regs[4]),
 
360
    COREREG(usr_regs.uregs[5], regs[5]),
 
361
    COREREG(usr_regs.uregs[6], regs[6]),
 
362
    COREREG(usr_regs.uregs[7], regs[7]),
 
363
    COREREG(usr_regs.uregs[8], usr_regs[0]),
 
364
    COREREG(usr_regs.uregs[9], usr_regs[1]),
 
365
    COREREG(usr_regs.uregs[10], usr_regs[2]),
 
366
    COREREG(usr_regs.uregs[11], usr_regs[3]),
 
367
    COREREG(usr_regs.uregs[12], usr_regs[4]),
 
368
    COREREG(usr_regs.uregs[13], banked_r13[0]),
 
369
    COREREG(usr_regs.uregs[14], banked_r14[0]),
 
370
    /* R13, R14, SPSR for SVC, ABT, UND, IRQ banks */
 
371
    COREREG(svc_regs[0], banked_r13[1]),
 
372
    COREREG(svc_regs[1], banked_r14[1]),
 
373
    COREREG(svc_regs[2], banked_spsr[1]),
 
374
    COREREG(abt_regs[0], banked_r13[2]),
 
375
    COREREG(abt_regs[1], banked_r14[2]),
 
376
    COREREG(abt_regs[2], banked_spsr[2]),
 
377
    COREREG(und_regs[0], banked_r13[3]),
 
378
    COREREG(und_regs[1], banked_r14[3]),
 
379
    COREREG(und_regs[2], banked_spsr[3]),
 
380
    COREREG(irq_regs[0], banked_r13[4]),
 
381
    COREREG(irq_regs[1], banked_r14[4]),
 
382
    COREREG(irq_regs[2], banked_spsr[4]),
 
383
    /* R8_fiq .. R14_fiq and SPSR_fiq */
 
384
    COREREG(fiq_regs[0], fiq_regs[0]),
 
385
    COREREG(fiq_regs[1], fiq_regs[1]),
 
386
    COREREG(fiq_regs[2], fiq_regs[2]),
 
387
    COREREG(fiq_regs[3], fiq_regs[3]),
 
388
    COREREG(fiq_regs[4], fiq_regs[4]),
 
389
    COREREG(fiq_regs[5], banked_r13[5]),
 
390
    COREREG(fiq_regs[6], banked_r14[5]),
 
391
    COREREG(fiq_regs[7], banked_spsr[5]),
 
392
    /* R15 */
 
393
    COREREG(usr_regs.uregs[15], regs[15]),
 
394
    /* VFP system registers */
 
395
    VFPSYSREG(FPSID),
 
396
    VFPSYSREG(MVFR1),
 
397
    VFPSYSREG(MVFR0),
 
398
    VFPSYSREG(FPEXC),
 
399
    VFPSYSREG(FPINST),
 
400
    VFPSYSREG(FPINST2),
 
401
};
 
402
 
 
403
int kvm_arch_put_registers(CPUState *cs, int level)
 
404
{
 
405
    ARMCPU *cpu = ARM_CPU(cs);
 
406
    CPUARMState *env = &cpu->env;
 
407
    struct kvm_one_reg r;
 
408
    int mode, bn;
 
409
    int ret, i;
 
410
    uint32_t cpsr, fpscr;
 
411
 
 
412
    /* Make sure the banked regs are properly set */
 
413
    mode = env->uncached_cpsr & CPSR_M;
 
414
    bn = bank_number(mode);
 
415
    if (mode == ARM_CPU_MODE_FIQ) {
 
416
        memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
 
417
    } else {
 
418
        memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
 
419
    }
 
420
    env->banked_r13[bn] = env->regs[13];
 
421
    env->banked_r14[bn] = env->regs[14];
 
422
    env->banked_spsr[bn] = env->spsr;
 
423
 
 
424
    /* Now we can safely copy stuff down to the kernel */
 
425
    for (i = 0; i < ARRAY_SIZE(regs); i++) {
 
426
        r.id = regs[i].id;
 
427
        r.addr = (uintptr_t)(env) + regs[i].offset;
 
428
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
429
        if (ret) {
 
430
            return ret;
 
431
        }
 
432
    }
 
433
 
 
434
    /* Special cases which aren't a single CPUARMState field */
 
435
    cpsr = cpsr_read(env);
 
436
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
 
437
        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
 
438
    r.addr = (uintptr_t)(&cpsr);
 
439
    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
440
    if (ret) {
 
441
        return ret;
 
442
    }
 
443
 
 
444
    /* VFP registers */
 
445
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
 
446
    for (i = 0; i < 32; i++) {
 
447
        r.addr = (uintptr_t)(&env->vfp.regs[i]);
 
448
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
449
        if (ret) {
 
450
            return ret;
 
451
        }
 
452
        r.id++;
 
453
    }
 
454
 
 
455
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
 
456
        KVM_REG_ARM_VFP_FPSCR;
 
457
    fpscr = vfp_get_fpscr(env);
 
458
    r.addr = (uintptr_t)&fpscr;
 
459
    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
460
    if (ret) {
 
461
        return ret;
 
462
    }
 
463
 
 
464
    /* Note that we do not call write_cpustate_to_list()
 
465
     * here, so we are only writing the tuple list back to
 
466
     * KVM. This is safe because nothing can change the
 
467
     * CPUARMState cp15 fields (in particular gdb accesses cannot)
 
468
     * and so there are no changes to sync. In fact syncing would
 
469
     * be wrong at this point: for a constant register where TCG and
 
470
     * KVM disagree about its value, the preceding write_list_to_cpustate()
 
471
     * would not have had any effect on the CPUARMState value (since the
 
472
     * register is read-only), and a write_cpustate_to_list() here would
 
473
     * then try to write the TCG value back into KVM -- this would either
 
474
     * fail or incorrectly change the value the guest sees.
 
475
     *
 
476
     * If we ever want to allow the user to modify cp15 registers via
 
477
     * the gdb stub, we would need to be more clever here (for instance
 
478
     * tracking the set of registers kvm_arch_get_registers() successfully
 
479
     * managed to update the CPUARMState with, and only allowing those
 
480
     * to be written back up into the kernel).
 
481
     */
 
482
    if (!write_list_to_kvmstate(cpu)) {
 
483
        return EINVAL;
 
484
    }
 
485
 
 
486
    return ret;
 
487
}
 
488
 
 
489
int kvm_arch_get_registers(CPUState *cs)
 
490
{
 
491
    ARMCPU *cpu = ARM_CPU(cs);
 
492
    CPUARMState *env = &cpu->env;
 
493
    struct kvm_one_reg r;
 
494
    int mode, bn;
 
495
    int ret, i;
 
496
    uint32_t cpsr, fpscr;
 
497
 
 
498
    for (i = 0; i < ARRAY_SIZE(regs); i++) {
 
499
        r.id = regs[i].id;
 
500
        r.addr = (uintptr_t)(env) + regs[i].offset;
 
501
        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
502
        if (ret) {
 
503
            return ret;
 
504
        }
 
505
    }
 
506
 
 
507
    /* Special cases which aren't a single CPUARMState field */
 
508
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
 
509
        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
 
510
    r.addr = (uintptr_t)(&cpsr);
 
511
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
512
    if (ret) {
 
513
        return ret;
 
514
    }
 
515
    cpsr_write(env, cpsr, 0xffffffff);
 
516
 
 
517
    /* Make sure the current mode regs are properly set */
 
518
    mode = env->uncached_cpsr & CPSR_M;
 
519
    bn = bank_number(mode);
 
520
    if (mode == ARM_CPU_MODE_FIQ) {
 
521
        memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
 
522
    } else {
 
523
        memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
 
524
    }
 
525
    env->regs[13] = env->banked_r13[bn];
 
526
    env->regs[14] = env->banked_r14[bn];
 
527
    env->spsr = env->banked_spsr[bn];
 
528
 
 
529
    /* VFP registers */
 
530
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
 
531
    for (i = 0; i < 32; i++) {
 
532
        r.addr = (uintptr_t)(&env->vfp.regs[i]);
 
533
        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
534
        if (ret) {
 
535
            return ret;
 
536
        }
 
537
        r.id++;
 
538
    }
 
539
 
 
540
    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
 
541
        KVM_REG_ARM_VFP_FPSCR;
 
542
    r.addr = (uintptr_t)&fpscr;
 
543
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
544
    if (ret) {
 
545
        return ret;
 
546
    }
 
547
    vfp_set_fpscr(env, fpscr);
 
548
 
 
549
    if (!write_kvmstate_to_list(cpu)) {
 
550
        return EINVAL;
 
551
    }
 
552
    /* Note that it's OK to have registers which aren't in CPUState,
 
553
     * so we can ignore a failure return here.
 
554
     */
 
555
    write_list_to_cpustate(cpu);
 
556
 
 
557
    return 0;
 
558
}
 
559
 
 
560
void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
 
561
{
 
562
}
 
563
 
 
564
void kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
 
565
{
 
566
}
 
567
 
 
568
int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 
569
{
 
570
    return 0;
 
571
}
 
572
 
 
573
void kvm_arch_reset_vcpu(CPUState *cs)
 
574
{
 
575
    /* Feed the kernel back its initial register state */
 
576
    ARMCPU *cpu = ARM_CPU(cs);
 
577
 
 
578
    memmove(cpu->cpreg_values, cpu->cpreg_reset_values,
 
579
            cpu->cpreg_array_len * sizeof(cpu->cpreg_values[0]));
 
580
 
 
581
    if (!write_list_to_kvmstate(cpu)) {
 
582
        abort();
 
583
    }
 
584
}
 
585
 
 
586
bool kvm_arch_stop_on_emulation_error(CPUState *cs)
 
587
{
 
588
    return true;
 
589
}
 
590
 
 
591
int kvm_arch_process_async_events(CPUState *cs)
 
592
{
 
593
    return 0;
 
594
}
 
595
 
 
596
int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, void *addr)
 
597
{
 
598
    return 1;
 
599
}
 
600
 
 
601
int kvm_arch_on_sigbus(int code, void *addr)
 
602
{
 
603
    return 1;
 
604
}
 
605
 
 
606
void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
 
607
{
 
608
    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 
609
}
 
610
 
 
611
int kvm_arch_insert_sw_breakpoint(CPUState *cs,
 
612
                                  struct kvm_sw_breakpoint *bp)
 
613
{
 
614
    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 
615
    return -EINVAL;
 
616
}
 
617
 
 
618
int kvm_arch_insert_hw_breakpoint(target_ulong addr,
 
619
                                  target_ulong len, int type)
 
620
{
 
621
    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 
622
    return -EINVAL;
 
623
}
 
624
 
 
625
int kvm_arch_remove_hw_breakpoint(target_ulong addr,
 
626
                                  target_ulong len, int type)
 
627
{
 
628
    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 
629
    return -EINVAL;
 
630
}
 
631
 
 
632
int kvm_arch_remove_sw_breakpoint(CPUState *cs,
 
633
                                  struct kvm_sw_breakpoint *bp)
 
634
{
 
635
    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 
636
    return -EINVAL;
 
637
}
 
638
 
 
639
void kvm_arch_remove_all_hw_breakpoints(void)
 
640
{
 
641
    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 
642
}
 
643
 
 
644
void kvm_arch_init_irq_routing(KVMState *s)
 
645
{
 
646
}