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

« back to all changes in this revision

Viewing changes to debian/patches/ubuntu/arm64/0017-target-arm-kvm-Split-32-bit-only-code-into-its-own-f.patch

  • 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
From fb7196f6cae01cfcd522bf98c9bbf25596dccb4a Mon Sep 17 00:00:00 2001
 
2
From: Peter Maydell <peter.maydell@linaro.org>
 
3
Date: Tue, 17 Dec 2013 19:42:29 +0000
 
4
Subject: [PATCH 17/49] target-arm/kvm: Split 32 bit only code into its own
 
5
 file
 
6
 
 
7
Split ARM KVM support code which is 32 bit specific out into its
 
8
own file, which we only compile on 32 bit hosts. This will give
 
9
us a place to add the 64 bit support code without adding lots of
 
10
ifdefs to kvm.c.
 
11
 
 
12
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
 
13
Message-id: 1385645602-18662-2-git-send-email-peter.maydell@linaro.org
 
14
Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
 
15
---
 
16
 target-arm/Makefile.objs |   1 +
 
17
 target-arm/kvm.c         | 491 --------------------------------------------
 
18
 target-arm/kvm32.c       | 515 +++++++++++++++++++++++++++++++++++++++++++++++
 
19
 3 files changed, 516 insertions(+), 491 deletions(-)
 
20
 create mode 100644 target-arm/kvm32.c
 
21
 
 
22
diff --git a/target-arm/Makefile.objs b/target-arm/Makefile.objs
 
23
index 356fbfc..c9f7944 100644
 
24
--- a/target-arm/Makefile.objs
 
25
+++ b/target-arm/Makefile.objs
 
26
@@ -1,6 +1,7 @@
 
27
 obj-y += arm-semi.o
 
28
 obj-$(CONFIG_SOFTMMU) += machine.o
 
29
 obj-$(CONFIG_KVM) += kvm.o
 
30
+obj-$(call land,$(CONFIG_KVM),$(call lnot,$(TARGET_AARCH64))) += kvm32.o
 
31
 obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
 
32
 obj-y += translate.o op_helper.o helper.o cpu.o
 
33
 obj-y += neon_helper.o iwmmxt_helper.o
 
34
diff --git a/target-arm/kvm.c b/target-arm/kvm.c
 
35
index f865dac..5cdb3b9 100644
 
36
--- a/target-arm/kvm.c
 
37
+++ b/target-arm/kvm.c
 
38
@@ -100,120 +100,6 @@ void kvm_arm_destroy_scratch_host_vcpu(int *fdarray)
 
39
     }
 
40
 }
 
41
 
 
42
-static inline void set_feature(uint64_t *features, int feature)
 
43
-{
 
44
-    *features |= 1ULL << feature;
 
45
-}
 
46
-
 
47
-bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
 
48
-{
 
49
-    /* Identify the feature bits corresponding to the host CPU, and
 
50
-     * fill out the ARMHostCPUClass fields accordingly. To do this
 
51
-     * we have to create a scratch VM, create a single CPU inside it,
 
52
-     * and then query that CPU for the relevant ID registers.
 
53
-     */
 
54
-    int i, ret, fdarray[3];
 
55
-    uint32_t midr, id_pfr0, id_isar0, mvfr1;
 
56
-    uint64_t features = 0;
 
57
-    /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
 
58
-     * we know these will only support creating one kind of guest CPU,
 
59
-     * which is its preferred CPU type.
 
60
-     */
 
61
-    static const uint32_t cpus_to_try[] = {
 
62
-        QEMU_KVM_ARM_TARGET_CORTEX_A15,
 
63
-        QEMU_KVM_ARM_TARGET_NONE
 
64
-    };
 
65
-    struct kvm_vcpu_init init;
 
66
-    struct kvm_one_reg idregs[] = {
 
67
-        {
 
68
-            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
69
-            | ENCODE_CP_REG(15, 0, 0, 0, 0, 0),
 
70
-            .addr = (uintptr_t)&midr,
 
71
-        },
 
72
-        {
 
73
-            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
74
-            | ENCODE_CP_REG(15, 0, 0, 1, 0, 0),
 
75
-            .addr = (uintptr_t)&id_pfr0,
 
76
-        },
 
77
-        {
 
78
-            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
79
-            | ENCODE_CP_REG(15, 0, 0, 2, 0, 0),
 
80
-            .addr = (uintptr_t)&id_isar0,
 
81
-        },
 
82
-        {
 
83
-            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
84
-            | KVM_REG_ARM_VFP | KVM_REG_ARM_VFP_MVFR1,
 
85
-            .addr = (uintptr_t)&mvfr1,
 
86
-        },
 
87
-    };
 
88
-
 
89
-    if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
 
90
-        return false;
 
91
-    }
 
92
-
 
93
-    ahcc->target = init.target;
 
94
-
 
95
-    /* This is not strictly blessed by the device tree binding docs yet,
 
96
-     * but in practice the kernel does not care about this string so
 
97
-     * there is no point maintaining an KVM_ARM_TARGET_* -> string table.
 
98
-     */
 
99
-    ahcc->dtb_compatible = "arm,arm-v7";
 
100
-
 
101
-    for (i = 0; i < ARRAY_SIZE(idregs); i++) {
 
102
-        ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &idregs[i]);
 
103
-        if (ret) {
 
104
-            break;
 
105
-        }
 
106
-    }
 
107
-
 
108
-    kvm_arm_destroy_scratch_host_vcpu(fdarray);
 
109
-
 
110
-    if (ret) {
 
111
-        return false;
 
112
-    }
 
113
-
 
114
-    /* Now we've retrieved all the register information we can
 
115
-     * set the feature bits based on the ID register fields.
 
116
-     * We can assume any KVM supporting CPU is at least a v7
 
117
-     * with VFPv3, LPAE and the generic timers; this in turn implies
 
118
-     * most of the other feature bits, but a few must be tested.
 
119
-     */
 
120
-    set_feature(&features, ARM_FEATURE_V7);
 
121
-    set_feature(&features, ARM_FEATURE_VFP3);
 
122
-    set_feature(&features, ARM_FEATURE_LPAE);
 
123
-    set_feature(&features, ARM_FEATURE_GENERIC_TIMER);
 
124
-
 
125
-    switch (extract32(id_isar0, 24, 4)) {
 
126
-    case 1:
 
127
-        set_feature(&features, ARM_FEATURE_THUMB_DIV);
 
128
-        break;
 
129
-    case 2:
 
130
-        set_feature(&features, ARM_FEATURE_ARM_DIV);
 
131
-        set_feature(&features, ARM_FEATURE_THUMB_DIV);
 
132
-        break;
 
133
-    default:
 
134
-        break;
 
135
-    }
 
136
-
 
137
-    if (extract32(id_pfr0, 12, 4) == 1) {
 
138
-        set_feature(&features, ARM_FEATURE_THUMB2EE);
 
139
-    }
 
140
-    if (extract32(mvfr1, 20, 4) == 1) {
 
141
-        set_feature(&features, ARM_FEATURE_VFP_FP16);
 
142
-    }
 
143
-    if (extract32(mvfr1, 12, 4) == 1) {
 
144
-        set_feature(&features, ARM_FEATURE_NEON);
 
145
-    }
 
146
-    if (extract32(mvfr1, 28, 4) == 1) {
 
147
-        /* FMAC support implies VFPv4 */
 
148
-        set_feature(&features, ARM_FEATURE_VFP4);
 
149
-    }
 
150
-
 
151
-    ahcc->features = features;
 
152
-
 
153
-    return true;
 
154
-}
 
155
-
 
156
 static void kvm_arm_host_cpu_class_init(ObjectClass *oc, void *data)
 
157
 {
 
158
     ARMHostCPUClass *ahcc = ARM_HOST_CPU_CLASS(oc);
 
159
@@ -265,144 +151,6 @@ unsigned long kvm_arch_vcpu_id(CPUState *cpu)
 
160
     return cpu->cpu_index;
 
161
 }
 
162
 
 
163
-static bool reg_syncs_via_tuple_list(uint64_t regidx)
 
164
-{
 
165
-    /* Return true if the regidx is a register we should synchronize
 
166
-     * via the cpreg_tuples array (ie is not a core reg we sync by
 
167
-     * hand in kvm_arch_get/put_registers())
 
168
-     */
 
169
-    switch (regidx & KVM_REG_ARM_COPROC_MASK) {
 
170
-    case KVM_REG_ARM_CORE:
 
171
-    case KVM_REG_ARM_VFP:
 
172
-        return false;
 
173
-    default:
 
174
-        return true;
 
175
-    }
 
176
-}
 
177
-
 
178
-static int compare_u64(const void *a, const void *b)
 
179
-{
 
180
-    if (*(uint64_t *)a > *(uint64_t *)b) {
 
181
-        return 1;
 
182
-    }
 
183
-    if (*(uint64_t *)a < *(uint64_t *)b) {
 
184
-        return -1;
 
185
-    }
 
186
-    return 0;
 
187
-}
 
188
-
 
189
-int kvm_arch_init_vcpu(CPUState *cs)
 
190
-{
 
191
-    struct kvm_vcpu_init init;
 
192
-    int i, ret, arraylen;
 
193
-    uint64_t v;
 
194
-    struct kvm_one_reg r;
 
195
-    struct kvm_reg_list rl;
 
196
-    struct kvm_reg_list *rlp;
 
197
-    ARMCPU *cpu = ARM_CPU(cs);
 
198
-
 
199
-    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
 
200
-        fprintf(stderr, "KVM is not supported for this guest CPU type\n");
 
201
-        return -EINVAL;
 
202
-    }
 
203
-
 
204
-    init.target = cpu->kvm_target;
 
205
-    memset(init.features, 0, sizeof(init.features));
 
206
-    if (cpu->start_powered_off) {
 
207
-        init.features[0] = 1 << KVM_ARM_VCPU_POWER_OFF;
 
208
-    }
 
209
-    ret = kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init);
 
210
-    if (ret) {
 
211
-        return ret;
 
212
-    }
 
213
-    /* Query the kernel to make sure it supports 32 VFP
 
214
-     * registers: QEMU's "cortex-a15" CPU is always a
 
215
-     * VFP-D32 core. The simplest way to do this is just
 
216
-     * to attempt to read register d31.
 
217
-     */
 
218
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP | 31;
 
219
-    r.addr = (uintptr_t)(&v);
 
220
-    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
221
-    if (ret == -ENOENT) {
 
222
-        return -EINVAL;
 
223
-    }
 
224
-
 
225
-    /* Populate the cpreg list based on the kernel's idea
 
226
-     * of what registers exist (and throw away the TCG-created list).
 
227
-     */
 
228
-    rl.n = 0;
 
229
-    ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl);
 
230
-    if (ret != -E2BIG) {
 
231
-        return ret;
 
232
-    }
 
233
-    rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t));
 
234
-    rlp->n = rl.n;
 
235
-    ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp);
 
236
-    if (ret) {
 
237
-        goto out;
 
238
-    }
 
239
-    /* Sort the list we get back from the kernel, since cpreg_tuples
 
240
-     * must be in strictly ascending order.
 
241
-     */
 
242
-    qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64);
 
243
-
 
244
-    for (i = 0, arraylen = 0; i < rlp->n; i++) {
 
245
-        if (!reg_syncs_via_tuple_list(rlp->reg[i])) {
 
246
-            continue;
 
247
-        }
 
248
-        switch (rlp->reg[i] & KVM_REG_SIZE_MASK) {
 
249
-        case KVM_REG_SIZE_U32:
 
250
-        case KVM_REG_SIZE_U64:
 
251
-            break;
 
252
-        default:
 
253
-            fprintf(stderr, "Can't handle size of register in kernel list\n");
 
254
-            ret = -EINVAL;
 
255
-            goto out;
 
256
-        }
 
257
-
 
258
-        arraylen++;
 
259
-    }
 
260
-
 
261
-    cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen);
 
262
-    cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen);
 
263
-    cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes,
 
264
-                                         arraylen);
 
265
-    cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values,
 
266
-                                        arraylen);
 
267
-    cpu->cpreg_array_len = arraylen;
 
268
-    cpu->cpreg_vmstate_array_len = arraylen;
 
269
-
 
270
-    for (i = 0, arraylen = 0; i < rlp->n; i++) {
 
271
-        uint64_t regidx = rlp->reg[i];
 
272
-        if (!reg_syncs_via_tuple_list(regidx)) {
 
273
-            continue;
 
274
-        }
 
275
-        cpu->cpreg_indexes[arraylen] = regidx;
 
276
-        arraylen++;
 
277
-    }
 
278
-    assert(cpu->cpreg_array_len == arraylen);
 
279
-
 
280
-    if (!write_kvmstate_to_list(cpu)) {
 
281
-        /* Shouldn't happen unless kernel is inconsistent about
 
282
-         * what registers exist.
 
283
-         */
 
284
-        fprintf(stderr, "Initial read of kernel register state failed\n");
 
285
-        ret = -EINVAL;
 
286
-        goto out;
 
287
-    }
 
288
-
 
289
-    /* Save a copy of the initial register values so that we can
 
290
-     * feed it back to the kernel on VCPU reset.
 
291
-     */
 
292
-    cpu->cpreg_reset_values = g_memdup(cpu->cpreg_values,
 
293
-                                       cpu->cpreg_array_len *
 
294
-                                       sizeof(cpu->cpreg_values[0]));
 
295
-
 
296
-out:
 
297
-    g_free(rlp);
 
298
-    return ret;
 
299
-}
 
300
-
 
301
 /* We track all the KVM devices which need their memory addresses
 
302
  * passing to the kernel in a list of these structures.
 
303
  * When board init is complete we run through the list and
 
304
@@ -563,232 +311,6 @@ bool write_list_to_kvmstate(ARMCPU *cpu)
 
305
     return ok;
 
306
 }
 
307
 
 
308
-typedef struct Reg {
 
309
-    uint64_t id;
 
310
-    int offset;
 
311
-} Reg;
 
312
-
 
313
-#define COREREG(KERNELNAME, QEMUFIELD)                       \
 
314
-    {                                                        \
 
315
-        KVM_REG_ARM | KVM_REG_SIZE_U32 |                     \
 
316
-        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(KERNELNAME), \
 
317
-        offsetof(CPUARMState, QEMUFIELD)                     \
 
318
-    }
 
319
-
 
320
-#define VFPSYSREG(R)                                       \
 
321
-    {                                                      \
 
322
-        KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | \
 
323
-        KVM_REG_ARM_VFP_##R,                               \
 
324
-        offsetof(CPUARMState, vfp.xregs[ARM_VFP_##R])      \
 
325
-    }
 
326
-
 
327
-static const Reg regs[] = {
 
328
-    /* R0_usr .. R14_usr */
 
329
-    COREREG(usr_regs.uregs[0], regs[0]),
 
330
-    COREREG(usr_regs.uregs[1], regs[1]),
 
331
-    COREREG(usr_regs.uregs[2], regs[2]),
 
332
-    COREREG(usr_regs.uregs[3], regs[3]),
 
333
-    COREREG(usr_regs.uregs[4], regs[4]),
 
334
-    COREREG(usr_regs.uregs[5], regs[5]),
 
335
-    COREREG(usr_regs.uregs[6], regs[6]),
 
336
-    COREREG(usr_regs.uregs[7], regs[7]),
 
337
-    COREREG(usr_regs.uregs[8], usr_regs[0]),
 
338
-    COREREG(usr_regs.uregs[9], usr_regs[1]),
 
339
-    COREREG(usr_regs.uregs[10], usr_regs[2]),
 
340
-    COREREG(usr_regs.uregs[11], usr_regs[3]),
 
341
-    COREREG(usr_regs.uregs[12], usr_regs[4]),
 
342
-    COREREG(usr_regs.uregs[13], banked_r13[0]),
 
343
-    COREREG(usr_regs.uregs[14], banked_r14[0]),
 
344
-    /* R13, R14, SPSR for SVC, ABT, UND, IRQ banks */
 
345
-    COREREG(svc_regs[0], banked_r13[1]),
 
346
-    COREREG(svc_regs[1], banked_r14[1]),
 
347
-    COREREG(svc_regs[2], banked_spsr[1]),
 
348
-    COREREG(abt_regs[0], banked_r13[2]),
 
349
-    COREREG(abt_regs[1], banked_r14[2]),
 
350
-    COREREG(abt_regs[2], banked_spsr[2]),
 
351
-    COREREG(und_regs[0], banked_r13[3]),
 
352
-    COREREG(und_regs[1], banked_r14[3]),
 
353
-    COREREG(und_regs[2], banked_spsr[3]),
 
354
-    COREREG(irq_regs[0], banked_r13[4]),
 
355
-    COREREG(irq_regs[1], banked_r14[4]),
 
356
-    COREREG(irq_regs[2], banked_spsr[4]),
 
357
-    /* R8_fiq .. R14_fiq and SPSR_fiq */
 
358
-    COREREG(fiq_regs[0], fiq_regs[0]),
 
359
-    COREREG(fiq_regs[1], fiq_regs[1]),
 
360
-    COREREG(fiq_regs[2], fiq_regs[2]),
 
361
-    COREREG(fiq_regs[3], fiq_regs[3]),
 
362
-    COREREG(fiq_regs[4], fiq_regs[4]),
 
363
-    COREREG(fiq_regs[5], banked_r13[5]),
 
364
-    COREREG(fiq_regs[6], banked_r14[5]),
 
365
-    COREREG(fiq_regs[7], banked_spsr[5]),
 
366
-    /* R15 */
 
367
-    COREREG(usr_regs.uregs[15], regs[15]),
 
368
-    /* VFP system registers */
 
369
-    VFPSYSREG(FPSID),
 
370
-    VFPSYSREG(MVFR1),
 
371
-    VFPSYSREG(MVFR0),
 
372
-    VFPSYSREG(FPEXC),
 
373
-    VFPSYSREG(FPINST),
 
374
-    VFPSYSREG(FPINST2),
 
375
-};
 
376
-
 
377
-int kvm_arch_put_registers(CPUState *cs, int level)
 
378
-{
 
379
-    ARMCPU *cpu = ARM_CPU(cs);
 
380
-    CPUARMState *env = &cpu->env;
 
381
-    struct kvm_one_reg r;
 
382
-    int mode, bn;
 
383
-    int ret, i;
 
384
-    uint32_t cpsr, fpscr;
 
385
-
 
386
-    /* Make sure the banked regs are properly set */
 
387
-    mode = env->uncached_cpsr & CPSR_M;
 
388
-    bn = bank_number(mode);
 
389
-    if (mode == ARM_CPU_MODE_FIQ) {
 
390
-        memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
 
391
-    } else {
 
392
-        memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
 
393
-    }
 
394
-    env->banked_r13[bn] = env->regs[13];
 
395
-    env->banked_r14[bn] = env->regs[14];
 
396
-    env->banked_spsr[bn] = env->spsr;
 
397
-
 
398
-    /* Now we can safely copy stuff down to the kernel */
 
399
-    for (i = 0; i < ARRAY_SIZE(regs); i++) {
 
400
-        r.id = regs[i].id;
 
401
-        r.addr = (uintptr_t)(env) + regs[i].offset;
 
402
-        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
403
-        if (ret) {
 
404
-            return ret;
 
405
-        }
 
406
-    }
 
407
-
 
408
-    /* Special cases which aren't a single CPUARMState field */
 
409
-    cpsr = cpsr_read(env);
 
410
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
 
411
-        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
 
412
-    r.addr = (uintptr_t)(&cpsr);
 
413
-    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
414
-    if (ret) {
 
415
-        return ret;
 
416
-    }
 
417
-
 
418
-    /* VFP registers */
 
419
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
 
420
-    for (i = 0; i < 32; i++) {
 
421
-        r.addr = (uintptr_t)(&env->vfp.regs[i]);
 
422
-        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
423
-        if (ret) {
 
424
-            return ret;
 
425
-        }
 
426
-        r.id++;
 
427
-    }
 
428
-
 
429
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
 
430
-        KVM_REG_ARM_VFP_FPSCR;
 
431
-    fpscr = vfp_get_fpscr(env);
 
432
-    r.addr = (uintptr_t)&fpscr;
 
433
-    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
434
-    if (ret) {
 
435
-        return ret;
 
436
-    }
 
437
-
 
438
-    /* Note that we do not call write_cpustate_to_list()
 
439
-     * here, so we are only writing the tuple list back to
 
440
-     * KVM. This is safe because nothing can change the
 
441
-     * CPUARMState cp15 fields (in particular gdb accesses cannot)
 
442
-     * and so there are no changes to sync. In fact syncing would
 
443
-     * be wrong at this point: for a constant register where TCG and
 
444
-     * KVM disagree about its value, the preceding write_list_to_cpustate()
 
445
-     * would not have had any effect on the CPUARMState value (since the
 
446
-     * register is read-only), and a write_cpustate_to_list() here would
 
447
-     * then try to write the TCG value back into KVM -- this would either
 
448
-     * fail or incorrectly change the value the guest sees.
 
449
-     *
 
450
-     * If we ever want to allow the user to modify cp15 registers via
 
451
-     * the gdb stub, we would need to be more clever here (for instance
 
452
-     * tracking the set of registers kvm_arch_get_registers() successfully
 
453
-     * managed to update the CPUARMState with, and only allowing those
 
454
-     * to be written back up into the kernel).
 
455
-     */
 
456
-    if (!write_list_to_kvmstate(cpu)) {
 
457
-        return EINVAL;
 
458
-    }
 
459
-
 
460
-    return ret;
 
461
-}
 
462
-
 
463
-int kvm_arch_get_registers(CPUState *cs)
 
464
-{
 
465
-    ARMCPU *cpu = ARM_CPU(cs);
 
466
-    CPUARMState *env = &cpu->env;
 
467
-    struct kvm_one_reg r;
 
468
-    int mode, bn;
 
469
-    int ret, i;
 
470
-    uint32_t cpsr, fpscr;
 
471
-
 
472
-    for (i = 0; i < ARRAY_SIZE(regs); i++) {
 
473
-        r.id = regs[i].id;
 
474
-        r.addr = (uintptr_t)(env) + regs[i].offset;
 
475
-        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
476
-        if (ret) {
 
477
-            return ret;
 
478
-        }
 
479
-    }
 
480
-
 
481
-    /* Special cases which aren't a single CPUARMState field */
 
482
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
 
483
-        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
 
484
-    r.addr = (uintptr_t)(&cpsr);
 
485
-    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
486
-    if (ret) {
 
487
-        return ret;
 
488
-    }
 
489
-    cpsr_write(env, cpsr, 0xffffffff);
 
490
-
 
491
-    /* Make sure the current mode regs are properly set */
 
492
-    mode = env->uncached_cpsr & CPSR_M;
 
493
-    bn = bank_number(mode);
 
494
-    if (mode == ARM_CPU_MODE_FIQ) {
 
495
-        memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
 
496
-    } else {
 
497
-        memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
 
498
-    }
 
499
-    env->regs[13] = env->banked_r13[bn];
 
500
-    env->regs[14] = env->banked_r14[bn];
 
501
-    env->spsr = env->banked_spsr[bn];
 
502
-
 
503
-    /* VFP registers */
 
504
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
 
505
-    for (i = 0; i < 32; i++) {
 
506
-        r.addr = (uintptr_t)(&env->vfp.regs[i]);
 
507
-        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
508
-        if (ret) {
 
509
-            return ret;
 
510
-        }
 
511
-        r.id++;
 
512
-    }
 
513
-
 
514
-    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
 
515
-        KVM_REG_ARM_VFP_FPSCR;
 
516
-    r.addr = (uintptr_t)&fpscr;
 
517
-    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
518
-    if (ret) {
 
519
-        return ret;
 
520
-    }
 
521
-    vfp_set_fpscr(env, fpscr);
 
522
-
 
523
-    if (!write_kvmstate_to_list(cpu)) {
 
524
-        return EINVAL;
 
525
-    }
 
526
-    /* Note that it's OK to have registers which aren't in CPUState,
 
527
-     * so we can ignore a failure return here.
 
528
-     */
 
529
-    write_list_to_cpustate(cpu);
 
530
-
 
531
-    return 0;
 
532
-}
 
533
-
 
534
 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
 
535
 {
 
536
 }
 
537
@@ -802,19 +324,6 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 
538
     return 0;
 
539
 }
 
540
 
 
541
-void kvm_arch_reset_vcpu(CPUState *cs)
 
542
-{
 
543
-    /* Feed the kernel back its initial register state */
 
544
-    ARMCPU *cpu = ARM_CPU(cs);
 
545
-
 
546
-    memmove(cpu->cpreg_values, cpu->cpreg_reset_values,
 
547
-            cpu->cpreg_array_len * sizeof(cpu->cpreg_values[0]));
 
548
-
 
549
-    if (!write_list_to_kvmstate(cpu)) {
 
550
-        abort();
 
551
-    }
 
552
-}
 
553
-
 
554
 bool kvm_arch_stop_on_emulation_error(CPUState *cs)
 
555
 {
 
556
     return true;
 
557
diff --git a/target-arm/kvm32.c b/target-arm/kvm32.c
 
558
new file mode 100644
 
559
index 0000000..a4fde07
 
560
--- /dev/null
 
561
+++ b/target-arm/kvm32.c
 
562
@@ -0,0 +1,515 @@
 
563
+/*
 
564
+ * ARM implementation of KVM hooks, 32 bit specific code.
 
565
+ *
 
566
+ * Copyright Christoffer Dall 2009-2010
 
567
+ *
 
568
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
 
569
+ * See the COPYING file in the top-level directory.
 
570
+ *
 
571
+ */
 
572
+
 
573
+#include <stdio.h>
 
574
+#include <sys/types.h>
 
575
+#include <sys/ioctl.h>
 
576
+#include <sys/mman.h>
 
577
+
 
578
+#include <linux/kvm.h>
 
579
+
 
580
+#include "qemu-common.h"
 
581
+#include "qemu/timer.h"
 
582
+#include "sysemu/sysemu.h"
 
583
+#include "sysemu/kvm.h"
 
584
+#include "kvm_arm.h"
 
585
+#include "cpu.h"
 
586
+#include "hw/arm/arm.h"
 
587
+
 
588
+static inline void set_feature(uint64_t *features, int feature)
 
589
+{
 
590
+    *features |= 1ULL << feature;
 
591
+}
 
592
+
 
593
+bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
 
594
+{
 
595
+    /* Identify the feature bits corresponding to the host CPU, and
 
596
+     * fill out the ARMHostCPUClass fields accordingly. To do this
 
597
+     * we have to create a scratch VM, create a single CPU inside it,
 
598
+     * and then query that CPU for the relevant ID registers.
 
599
+     */
 
600
+    int i, ret, fdarray[3];
 
601
+    uint32_t midr, id_pfr0, id_isar0, mvfr1;
 
602
+    uint64_t features = 0;
 
603
+    /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
 
604
+     * we know these will only support creating one kind of guest CPU,
 
605
+     * which is its preferred CPU type.
 
606
+     */
 
607
+    static const uint32_t cpus_to_try[] = {
 
608
+        QEMU_KVM_ARM_TARGET_CORTEX_A15,
 
609
+        QEMU_KVM_ARM_TARGET_NONE
 
610
+    };
 
611
+    struct kvm_vcpu_init init;
 
612
+    struct kvm_one_reg idregs[] = {
 
613
+        {
 
614
+            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
615
+            | ENCODE_CP_REG(15, 0, 0, 0, 0, 0),
 
616
+            .addr = (uintptr_t)&midr,
 
617
+        },
 
618
+        {
 
619
+            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
620
+            | ENCODE_CP_REG(15, 0, 0, 1, 0, 0),
 
621
+            .addr = (uintptr_t)&id_pfr0,
 
622
+        },
 
623
+        {
 
624
+            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
625
+            | ENCODE_CP_REG(15, 0, 0, 2, 0, 0),
 
626
+            .addr = (uintptr_t)&id_isar0,
 
627
+        },
 
628
+        {
 
629
+            .id = KVM_REG_ARM | KVM_REG_SIZE_U32
 
630
+            | KVM_REG_ARM_VFP | KVM_REG_ARM_VFP_MVFR1,
 
631
+            .addr = (uintptr_t)&mvfr1,
 
632
+        },
 
633
+    };
 
634
+
 
635
+    if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
 
636
+        return false;
 
637
+    }
 
638
+
 
639
+    ahcc->target = init.target;
 
640
+
 
641
+    /* This is not strictly blessed by the device tree binding docs yet,
 
642
+     * but in practice the kernel does not care about this string so
 
643
+     * there is no point maintaining an KVM_ARM_TARGET_* -> string table.
 
644
+     */
 
645
+    ahcc->dtb_compatible = "arm,arm-v7";
 
646
+
 
647
+    for (i = 0; i < ARRAY_SIZE(idregs); i++) {
 
648
+        ret = ioctl(fdarray[2], KVM_GET_ONE_REG, &idregs[i]);
 
649
+        if (ret) {
 
650
+            break;
 
651
+        }
 
652
+    }
 
653
+
 
654
+    kvm_arm_destroy_scratch_host_vcpu(fdarray);
 
655
+
 
656
+    if (ret) {
 
657
+        return false;
 
658
+    }
 
659
+
 
660
+    /* Now we've retrieved all the register information we can
 
661
+     * set the feature bits based on the ID register fields.
 
662
+     * We can assume any KVM supporting CPU is at least a v7
 
663
+     * with VFPv3, LPAE and the generic timers; this in turn implies
 
664
+     * most of the other feature bits, but a few must be tested.
 
665
+     */
 
666
+    set_feature(&features, ARM_FEATURE_V7);
 
667
+    set_feature(&features, ARM_FEATURE_VFP3);
 
668
+    set_feature(&features, ARM_FEATURE_LPAE);
 
669
+    set_feature(&features, ARM_FEATURE_GENERIC_TIMER);
 
670
+
 
671
+    switch (extract32(id_isar0, 24, 4)) {
 
672
+    case 1:
 
673
+        set_feature(&features, ARM_FEATURE_THUMB_DIV);
 
674
+        break;
 
675
+    case 2:
 
676
+        set_feature(&features, ARM_FEATURE_ARM_DIV);
 
677
+        set_feature(&features, ARM_FEATURE_THUMB_DIV);
 
678
+        break;
 
679
+    default:
 
680
+        break;
 
681
+    }
 
682
+
 
683
+    if (extract32(id_pfr0, 12, 4) == 1) {
 
684
+        set_feature(&features, ARM_FEATURE_THUMB2EE);
 
685
+    }
 
686
+    if (extract32(mvfr1, 20, 4) == 1) {
 
687
+        set_feature(&features, ARM_FEATURE_VFP_FP16);
 
688
+    }
 
689
+    if (extract32(mvfr1, 12, 4) == 1) {
 
690
+        set_feature(&features, ARM_FEATURE_NEON);
 
691
+    }
 
692
+    if (extract32(mvfr1, 28, 4) == 1) {
 
693
+        /* FMAC support implies VFPv4 */
 
694
+        set_feature(&features, ARM_FEATURE_VFP4);
 
695
+    }
 
696
+
 
697
+    ahcc->features = features;
 
698
+
 
699
+    return true;
 
700
+}
 
701
+
 
702
+static bool reg_syncs_via_tuple_list(uint64_t regidx)
 
703
+{
 
704
+    /* Return true if the regidx is a register we should synchronize
 
705
+     * via the cpreg_tuples array (ie is not a core reg we sync by
 
706
+     * hand in kvm_arch_get/put_registers())
 
707
+     */
 
708
+    switch (regidx & KVM_REG_ARM_COPROC_MASK) {
 
709
+    case KVM_REG_ARM_CORE:
 
710
+    case KVM_REG_ARM_VFP:
 
711
+        return false;
 
712
+    default:
 
713
+        return true;
 
714
+    }
 
715
+}
 
716
+
 
717
+static int compare_u64(const void *a, const void *b)
 
718
+{
 
719
+    if (*(uint64_t *)a > *(uint64_t *)b) {
 
720
+        return 1;
 
721
+    }
 
722
+    if (*(uint64_t *)a < *(uint64_t *)b) {
 
723
+        return -1;
 
724
+    }
 
725
+    return 0;
 
726
+}
 
727
+
 
728
+int kvm_arch_init_vcpu(CPUState *cs)
 
729
+{
 
730
+    struct kvm_vcpu_init init;
 
731
+    int i, ret, arraylen;
 
732
+    uint64_t v;
 
733
+    struct kvm_one_reg r;
 
734
+    struct kvm_reg_list rl;
 
735
+    struct kvm_reg_list *rlp;
 
736
+    ARMCPU *cpu = ARM_CPU(cs);
 
737
+
 
738
+    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
 
739
+        fprintf(stderr, "KVM is not supported for this guest CPU type\n");
 
740
+        return -EINVAL;
 
741
+    }
 
742
+
 
743
+    init.target = cpu->kvm_target;
 
744
+    memset(init.features, 0, sizeof(init.features));
 
745
+    if (cpu->start_powered_off) {
 
746
+        init.features[0] = 1 << KVM_ARM_VCPU_POWER_OFF;
 
747
+    }
 
748
+    ret = kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init);
 
749
+    if (ret) {
 
750
+        return ret;
 
751
+    }
 
752
+    /* Query the kernel to make sure it supports 32 VFP
 
753
+     * registers: QEMU's "cortex-a15" CPU is always a
 
754
+     * VFP-D32 core. The simplest way to do this is just
 
755
+     * to attempt to read register d31.
 
756
+     */
 
757
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP | 31;
 
758
+    r.addr = (uintptr_t)(&v);
 
759
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
760
+    if (ret == -ENOENT) {
 
761
+        return -EINVAL;
 
762
+    }
 
763
+
 
764
+    /* Populate the cpreg list based on the kernel's idea
 
765
+     * of what registers exist (and throw away the TCG-created list).
 
766
+     */
 
767
+    rl.n = 0;
 
768
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, &rl);
 
769
+    if (ret != -E2BIG) {
 
770
+        return ret;
 
771
+    }
 
772
+    rlp = g_malloc(sizeof(struct kvm_reg_list) + rl.n * sizeof(uint64_t));
 
773
+    rlp->n = rl.n;
 
774
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_REG_LIST, rlp);
 
775
+    if (ret) {
 
776
+        goto out;
 
777
+    }
 
778
+    /* Sort the list we get back from the kernel, since cpreg_tuples
 
779
+     * must be in strictly ascending order.
 
780
+     */
 
781
+    qsort(&rlp->reg, rlp->n, sizeof(rlp->reg[0]), compare_u64);
 
782
+
 
783
+    for (i = 0, arraylen = 0; i < rlp->n; i++) {
 
784
+        if (!reg_syncs_via_tuple_list(rlp->reg[i])) {
 
785
+            continue;
 
786
+        }
 
787
+        switch (rlp->reg[i] & KVM_REG_SIZE_MASK) {
 
788
+        case KVM_REG_SIZE_U32:
 
789
+        case KVM_REG_SIZE_U64:
 
790
+            break;
 
791
+        default:
 
792
+            fprintf(stderr, "Can't handle size of register in kernel list\n");
 
793
+            ret = -EINVAL;
 
794
+            goto out;
 
795
+        }
 
796
+
 
797
+        arraylen++;
 
798
+    }
 
799
+
 
800
+    cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen);
 
801
+    cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen);
 
802
+    cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes,
 
803
+                                         arraylen);
 
804
+    cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values,
 
805
+                                        arraylen);
 
806
+    cpu->cpreg_array_len = arraylen;
 
807
+    cpu->cpreg_vmstate_array_len = arraylen;
 
808
+
 
809
+    for (i = 0, arraylen = 0; i < rlp->n; i++) {
 
810
+        uint64_t regidx = rlp->reg[i];
 
811
+        if (!reg_syncs_via_tuple_list(regidx)) {
 
812
+            continue;
 
813
+        }
 
814
+        cpu->cpreg_indexes[arraylen] = regidx;
 
815
+        arraylen++;
 
816
+    }
 
817
+    assert(cpu->cpreg_array_len == arraylen);
 
818
+
 
819
+    if (!write_kvmstate_to_list(cpu)) {
 
820
+        /* Shouldn't happen unless kernel is inconsistent about
 
821
+         * what registers exist.
 
822
+         */
 
823
+        fprintf(stderr, "Initial read of kernel register state failed\n");
 
824
+        ret = -EINVAL;
 
825
+        goto out;
 
826
+    }
 
827
+
 
828
+    /* Save a copy of the initial register values so that we can
 
829
+     * feed it back to the kernel on VCPU reset.
 
830
+     */
 
831
+    cpu->cpreg_reset_values = g_memdup(cpu->cpreg_values,
 
832
+                                       cpu->cpreg_array_len *
 
833
+                                       sizeof(cpu->cpreg_values[0]));
 
834
+
 
835
+out:
 
836
+    g_free(rlp);
 
837
+    return ret;
 
838
+}
 
839
+
 
840
+typedef struct Reg {
 
841
+    uint64_t id;
 
842
+    int offset;
 
843
+} Reg;
 
844
+
 
845
+#define COREREG(KERNELNAME, QEMUFIELD)                       \
 
846
+    {                                                        \
 
847
+        KVM_REG_ARM | KVM_REG_SIZE_U32 |                     \
 
848
+        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(KERNELNAME), \
 
849
+        offsetof(CPUARMState, QEMUFIELD)                     \
 
850
+    }
 
851
+
 
852
+#define VFPSYSREG(R)                                       \
 
853
+    {                                                      \
 
854
+        KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP | \
 
855
+        KVM_REG_ARM_VFP_##R,                               \
 
856
+        offsetof(CPUARMState, vfp.xregs[ARM_VFP_##R])      \
 
857
+    }
 
858
+
 
859
+static const Reg regs[] = {
 
860
+    /* R0_usr .. R14_usr */
 
861
+    COREREG(usr_regs.uregs[0], regs[0]),
 
862
+    COREREG(usr_regs.uregs[1], regs[1]),
 
863
+    COREREG(usr_regs.uregs[2], regs[2]),
 
864
+    COREREG(usr_regs.uregs[3], regs[3]),
 
865
+    COREREG(usr_regs.uregs[4], regs[4]),
 
866
+    COREREG(usr_regs.uregs[5], regs[5]),
 
867
+    COREREG(usr_regs.uregs[6], regs[6]),
 
868
+    COREREG(usr_regs.uregs[7], regs[7]),
 
869
+    COREREG(usr_regs.uregs[8], usr_regs[0]),
 
870
+    COREREG(usr_regs.uregs[9], usr_regs[1]),
 
871
+    COREREG(usr_regs.uregs[10], usr_regs[2]),
 
872
+    COREREG(usr_regs.uregs[11], usr_regs[3]),
 
873
+    COREREG(usr_regs.uregs[12], usr_regs[4]),
 
874
+    COREREG(usr_regs.uregs[13], banked_r13[0]),
 
875
+    COREREG(usr_regs.uregs[14], banked_r14[0]),
 
876
+    /* R13, R14, SPSR for SVC, ABT, UND, IRQ banks */
 
877
+    COREREG(svc_regs[0], banked_r13[1]),
 
878
+    COREREG(svc_regs[1], banked_r14[1]),
 
879
+    COREREG(svc_regs[2], banked_spsr[1]),
 
880
+    COREREG(abt_regs[0], banked_r13[2]),
 
881
+    COREREG(abt_regs[1], banked_r14[2]),
 
882
+    COREREG(abt_regs[2], banked_spsr[2]),
 
883
+    COREREG(und_regs[0], banked_r13[3]),
 
884
+    COREREG(und_regs[1], banked_r14[3]),
 
885
+    COREREG(und_regs[2], banked_spsr[3]),
 
886
+    COREREG(irq_regs[0], banked_r13[4]),
 
887
+    COREREG(irq_regs[1], banked_r14[4]),
 
888
+    COREREG(irq_regs[2], banked_spsr[4]),
 
889
+    /* R8_fiq .. R14_fiq and SPSR_fiq */
 
890
+    COREREG(fiq_regs[0], fiq_regs[0]),
 
891
+    COREREG(fiq_regs[1], fiq_regs[1]),
 
892
+    COREREG(fiq_regs[2], fiq_regs[2]),
 
893
+    COREREG(fiq_regs[3], fiq_regs[3]),
 
894
+    COREREG(fiq_regs[4], fiq_regs[4]),
 
895
+    COREREG(fiq_regs[5], banked_r13[5]),
 
896
+    COREREG(fiq_regs[6], banked_r14[5]),
 
897
+    COREREG(fiq_regs[7], banked_spsr[5]),
 
898
+    /* R15 */
 
899
+    COREREG(usr_regs.uregs[15], regs[15]),
 
900
+    /* VFP system registers */
 
901
+    VFPSYSREG(FPSID),
 
902
+    VFPSYSREG(MVFR1),
 
903
+    VFPSYSREG(MVFR0),
 
904
+    VFPSYSREG(FPEXC),
 
905
+    VFPSYSREG(FPINST),
 
906
+    VFPSYSREG(FPINST2),
 
907
+};
 
908
+
 
909
+int kvm_arch_put_registers(CPUState *cs, int level)
 
910
+{
 
911
+    ARMCPU *cpu = ARM_CPU(cs);
 
912
+    CPUARMState *env = &cpu->env;
 
913
+    struct kvm_one_reg r;
 
914
+    int mode, bn;
 
915
+    int ret, i;
 
916
+    uint32_t cpsr, fpscr;
 
917
+
 
918
+    /* Make sure the banked regs are properly set */
 
919
+    mode = env->uncached_cpsr & CPSR_M;
 
920
+    bn = bank_number(mode);
 
921
+    if (mode == ARM_CPU_MODE_FIQ) {
 
922
+        memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
 
923
+    } else {
 
924
+        memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
 
925
+    }
 
926
+    env->banked_r13[bn] = env->regs[13];
 
927
+    env->banked_r14[bn] = env->regs[14];
 
928
+    env->banked_spsr[bn] = env->spsr;
 
929
+
 
930
+    /* Now we can safely copy stuff down to the kernel */
 
931
+    for (i = 0; i < ARRAY_SIZE(regs); i++) {
 
932
+        r.id = regs[i].id;
 
933
+        r.addr = (uintptr_t)(env) + regs[i].offset;
 
934
+        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
935
+        if (ret) {
 
936
+            return ret;
 
937
+        }
 
938
+    }
 
939
+
 
940
+    /* Special cases which aren't a single CPUARMState field */
 
941
+    cpsr = cpsr_read(env);
 
942
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
 
943
+        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
 
944
+    r.addr = (uintptr_t)(&cpsr);
 
945
+    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
946
+    if (ret) {
 
947
+        return ret;
 
948
+    }
 
949
+
 
950
+    /* VFP registers */
 
951
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
 
952
+    for (i = 0; i < 32; i++) {
 
953
+        r.addr = (uintptr_t)(&env->vfp.regs[i]);
 
954
+        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
955
+        if (ret) {
 
956
+            return ret;
 
957
+        }
 
958
+        r.id++;
 
959
+    }
 
960
+
 
961
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
 
962
+        KVM_REG_ARM_VFP_FPSCR;
 
963
+    fpscr = vfp_get_fpscr(env);
 
964
+    r.addr = (uintptr_t)&fpscr;
 
965
+    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &r);
 
966
+    if (ret) {
 
967
+        return ret;
 
968
+    }
 
969
+
 
970
+    /* Note that we do not call write_cpustate_to_list()
 
971
+     * here, so we are only writing the tuple list back to
 
972
+     * KVM. This is safe because nothing can change the
 
973
+     * CPUARMState cp15 fields (in particular gdb accesses cannot)
 
974
+     * and so there are no changes to sync. In fact syncing would
 
975
+     * be wrong at this point: for a constant register where TCG and
 
976
+     * KVM disagree about its value, the preceding write_list_to_cpustate()
 
977
+     * would not have had any effect on the CPUARMState value (since the
 
978
+     * register is read-only), and a write_cpustate_to_list() here would
 
979
+     * then try to write the TCG value back into KVM -- this would either
 
980
+     * fail or incorrectly change the value the guest sees.
 
981
+     *
 
982
+     * If we ever want to allow the user to modify cp15 registers via
 
983
+     * the gdb stub, we would need to be more clever here (for instance
 
984
+     * tracking the set of registers kvm_arch_get_registers() successfully
 
985
+     * managed to update the CPUARMState with, and only allowing those
 
986
+     * to be written back up into the kernel).
 
987
+     */
 
988
+    if (!write_list_to_kvmstate(cpu)) {
 
989
+        return EINVAL;
 
990
+    }
 
991
+
 
992
+    return ret;
 
993
+}
 
994
+
 
995
+int kvm_arch_get_registers(CPUState *cs)
 
996
+{
 
997
+    ARMCPU *cpu = ARM_CPU(cs);
 
998
+    CPUARMState *env = &cpu->env;
 
999
+    struct kvm_one_reg r;
 
1000
+    int mode, bn;
 
1001
+    int ret, i;
 
1002
+    uint32_t cpsr, fpscr;
 
1003
+
 
1004
+    for (i = 0; i < ARRAY_SIZE(regs); i++) {
 
1005
+        r.id = regs[i].id;
 
1006
+        r.addr = (uintptr_t)(env) + regs[i].offset;
 
1007
+        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
1008
+        if (ret) {
 
1009
+            return ret;
 
1010
+        }
 
1011
+    }
 
1012
+
 
1013
+    /* Special cases which aren't a single CPUARMState field */
 
1014
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 |
 
1015
+        KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr);
 
1016
+    r.addr = (uintptr_t)(&cpsr);
 
1017
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
1018
+    if (ret) {
 
1019
+        return ret;
 
1020
+    }
 
1021
+    cpsr_write(env, cpsr, 0xffffffff);
 
1022
+
 
1023
+    /* Make sure the current mode regs are properly set */
 
1024
+    mode = env->uncached_cpsr & CPSR_M;
 
1025
+    bn = bank_number(mode);
 
1026
+    if (mode == ARM_CPU_MODE_FIQ) {
 
1027
+        memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
 
1028
+    } else {
 
1029
+        memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
 
1030
+    }
 
1031
+    env->regs[13] = env->banked_r13[bn];
 
1032
+    env->regs[14] = env->banked_r14[bn];
 
1033
+    env->spsr = env->banked_spsr[bn];
 
1034
+
 
1035
+    /* VFP registers */
 
1036
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U64 | KVM_REG_ARM_VFP;
 
1037
+    for (i = 0; i < 32; i++) {
 
1038
+        r.addr = (uintptr_t)(&env->vfp.regs[i]);
 
1039
+        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
1040
+        if (ret) {
 
1041
+            return ret;
 
1042
+        }
 
1043
+        r.id++;
 
1044
+    }
 
1045
+
 
1046
+    r.id = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_VFP |
 
1047
+        KVM_REG_ARM_VFP_FPSCR;
 
1048
+    r.addr = (uintptr_t)&fpscr;
 
1049
+    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &r);
 
1050
+    if (ret) {
 
1051
+        return ret;
 
1052
+    }
 
1053
+    vfp_set_fpscr(env, fpscr);
 
1054
+
 
1055
+    if (!write_kvmstate_to_list(cpu)) {
 
1056
+        return EINVAL;
 
1057
+    }
 
1058
+    /* Note that it's OK to have registers which aren't in CPUState,
 
1059
+     * so we can ignore a failure return here.
 
1060
+     */
 
1061
+    write_list_to_cpustate(cpu);
 
1062
+
 
1063
+    return 0;
 
1064
+}
 
1065
+
 
1066
+void kvm_arch_reset_vcpu(CPUState *cs)
 
1067
+{
 
1068
+    /* Feed the kernel back its initial register state */
 
1069
+    ARMCPU *cpu = ARM_CPU(cs);
 
1070
+
 
1071
+    memmove(cpu->cpreg_values, cpu->cpreg_reset_values,
 
1072
+            cpu->cpreg_array_len * sizeof(cpu->cpreg_values[0]));
 
1073
+
 
1074
+    if (!write_list_to_kvmstate(cpu)) {
 
1075
+        abort();
 
1076
+    }
 
1077
+}
 
1078
-- 
 
1079
1.8.5.2
 
1080