Statistics
| Branch: | Revision:

root / target-i386 / kvm.c @ 6fb6d245

History | View | Annotate | Download (28.6 kB)

1 05330448 aliguori
/*
2 05330448 aliguori
 * QEMU KVM support
3 05330448 aliguori
 *
4 05330448 aliguori
 * Copyright (C) 2006-2008 Qumranet Technologies
5 05330448 aliguori
 * Copyright IBM, Corp. 2008
6 05330448 aliguori
 *
7 05330448 aliguori
 * Authors:
8 05330448 aliguori
 *  Anthony Liguori   <aliguori@us.ibm.com>
9 05330448 aliguori
 *
10 05330448 aliguori
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 05330448 aliguori
 * See the COPYING file in the top-level directory.
12 05330448 aliguori
 *
13 05330448 aliguori
 */
14 05330448 aliguori
15 05330448 aliguori
#include <sys/types.h>
16 05330448 aliguori
#include <sys/ioctl.h>
17 05330448 aliguori
#include <sys/mman.h>
18 05330448 aliguori
19 05330448 aliguori
#include <linux/kvm.h>
20 05330448 aliguori
21 05330448 aliguori
#include "qemu-common.h"
22 05330448 aliguori
#include "sysemu.h"
23 05330448 aliguori
#include "kvm.h"
24 05330448 aliguori
#include "cpu.h"
25 e22a25c9 aliguori
#include "gdbstub.h"
26 0e607a80 Jan Kiszka
#include "host-utils.h"
27 05330448 aliguori
28 05330448 aliguori
//#define DEBUG_KVM
29 05330448 aliguori
30 05330448 aliguori
#ifdef DEBUG_KVM
31 05330448 aliguori
#define dprintf(fmt, ...) \
32 05330448 aliguori
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
33 05330448 aliguori
#else
34 05330448 aliguori
#define dprintf(fmt, ...) \
35 05330448 aliguori
    do { } while (0)
36 05330448 aliguori
#endif
37 05330448 aliguori
38 b827df58 Avi Kivity
#ifdef KVM_CAP_EXT_CPUID
39 b827df58 Avi Kivity
40 b827df58 Avi Kivity
static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
41 b827df58 Avi Kivity
{
42 b827df58 Avi Kivity
    struct kvm_cpuid2 *cpuid;
43 b827df58 Avi Kivity
    int r, size;
44 b827df58 Avi Kivity
45 b827df58 Avi Kivity
    size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
46 b827df58 Avi Kivity
    cpuid = (struct kvm_cpuid2 *)qemu_mallocz(size);
47 b827df58 Avi Kivity
    cpuid->nent = max;
48 b827df58 Avi Kivity
    r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid);
49 76ae317f Mark McLoughlin
    if (r == 0 && cpuid->nent >= max) {
50 76ae317f Mark McLoughlin
        r = -E2BIG;
51 76ae317f Mark McLoughlin
    }
52 b827df58 Avi Kivity
    if (r < 0) {
53 b827df58 Avi Kivity
        if (r == -E2BIG) {
54 b827df58 Avi Kivity
            qemu_free(cpuid);
55 b827df58 Avi Kivity
            return NULL;
56 b827df58 Avi Kivity
        } else {
57 b827df58 Avi Kivity
            fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
58 b827df58 Avi Kivity
                    strerror(-r));
59 b827df58 Avi Kivity
            exit(1);
60 b827df58 Avi Kivity
        }
61 b827df58 Avi Kivity
    }
62 b827df58 Avi Kivity
    return cpuid;
63 b827df58 Avi Kivity
}
64 b827df58 Avi Kivity
65 b827df58 Avi Kivity
uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function, int reg)
66 b827df58 Avi Kivity
{
67 b827df58 Avi Kivity
    struct kvm_cpuid2 *cpuid;
68 b827df58 Avi Kivity
    int i, max;
69 b827df58 Avi Kivity
    uint32_t ret = 0;
70 b827df58 Avi Kivity
    uint32_t cpuid_1_edx;
71 b827df58 Avi Kivity
72 b827df58 Avi Kivity
    if (!kvm_check_extension(env->kvm_state, KVM_CAP_EXT_CPUID)) {
73 b827df58 Avi Kivity
        return -1U;
74 b827df58 Avi Kivity
    }
75 b827df58 Avi Kivity
76 b827df58 Avi Kivity
    max = 1;
77 b827df58 Avi Kivity
    while ((cpuid = try_get_cpuid(env->kvm_state, max)) == NULL) {
78 b827df58 Avi Kivity
        max *= 2;
79 b827df58 Avi Kivity
    }
80 b827df58 Avi Kivity
81 b827df58 Avi Kivity
    for (i = 0; i < cpuid->nent; ++i) {
82 b827df58 Avi Kivity
        if (cpuid->entries[i].function == function) {
83 b827df58 Avi Kivity
            switch (reg) {
84 b827df58 Avi Kivity
            case R_EAX:
85 b827df58 Avi Kivity
                ret = cpuid->entries[i].eax;
86 b827df58 Avi Kivity
                break;
87 b827df58 Avi Kivity
            case R_EBX:
88 b827df58 Avi Kivity
                ret = cpuid->entries[i].ebx;
89 b827df58 Avi Kivity
                break;
90 b827df58 Avi Kivity
            case R_ECX:
91 b827df58 Avi Kivity
                ret = cpuid->entries[i].ecx;
92 b827df58 Avi Kivity
                break;
93 b827df58 Avi Kivity
            case R_EDX:
94 b827df58 Avi Kivity
                ret = cpuid->entries[i].edx;
95 b827df58 Avi Kivity
                if (function == 0x80000001) {
96 b827df58 Avi Kivity
                    /* On Intel, kvm returns cpuid according to the Intel spec,
97 b827df58 Avi Kivity
                     * so add missing bits according to the AMD spec:
98 b827df58 Avi Kivity
                     */
99 b827df58 Avi Kivity
                    cpuid_1_edx = kvm_arch_get_supported_cpuid(env, 1, R_EDX);
100 b827df58 Avi Kivity
                    ret |= cpuid_1_edx & 0xdfeff7ff;
101 b827df58 Avi Kivity
                }
102 b827df58 Avi Kivity
                break;
103 b827df58 Avi Kivity
            }
104 b827df58 Avi Kivity
        }
105 b827df58 Avi Kivity
    }
106 b827df58 Avi Kivity
107 b827df58 Avi Kivity
    qemu_free(cpuid);
108 b827df58 Avi Kivity
109 b827df58 Avi Kivity
    return ret;
110 b827df58 Avi Kivity
}
111 b827df58 Avi Kivity
112 b827df58 Avi Kivity
#else
113 b827df58 Avi Kivity
114 b827df58 Avi Kivity
uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function, int reg)
115 b827df58 Avi Kivity
{
116 b827df58 Avi Kivity
    return -1U;
117 b827df58 Avi Kivity
}
118 b827df58 Avi Kivity
119 b827df58 Avi Kivity
#endif
120 b827df58 Avi Kivity
121 6c1f42fe Andre Przywara
static void kvm_trim_features(uint32_t *features, uint32_t supported)
122 6c1f42fe Andre Przywara
{
123 6c1f42fe Andre Przywara
    int i;
124 6c1f42fe Andre Przywara
    uint32_t mask;
125 6c1f42fe Andre Przywara
126 6c1f42fe Andre Przywara
    for (i = 0; i < 32; ++i) {
127 6c1f42fe Andre Przywara
        mask = 1U << i;
128 6c1f42fe Andre Przywara
        if ((*features & mask) && !(supported & mask)) {
129 6c1f42fe Andre Przywara
            *features &= ~mask;
130 6c1f42fe Andre Przywara
        }
131 6c1f42fe Andre Przywara
    }
132 6c1f42fe Andre Przywara
}
133 6c1f42fe Andre Przywara
134 05330448 aliguori
int kvm_arch_init_vcpu(CPUState *env)
135 05330448 aliguori
{
136 05330448 aliguori
    struct {
137 486bd5a2 aliguori
        struct kvm_cpuid2 cpuid;
138 486bd5a2 aliguori
        struct kvm_cpuid_entry2 entries[100];
139 05330448 aliguori
    } __attribute__((packed)) cpuid_data;
140 486bd5a2 aliguori
    uint32_t limit, i, j, cpuid_i;
141 a33609ca aliguori
    uint32_t unused;
142 05330448 aliguori
143 f8d926e9 Jan Kiszka
    env->mp_state = KVM_MP_STATE_RUNNABLE;
144 f8d926e9 Jan Kiszka
145 6c1f42fe Andre Przywara
    kvm_trim_features(&env->cpuid_features,
146 6c1f42fe Andre Przywara
        kvm_arch_get_supported_cpuid(env, 1, R_EDX));
147 6c0d7ee8 Andre Przywara
148 6c0d7ee8 Andre Przywara
    i = env->cpuid_ext_features & CPUID_EXT_HYPERVISOR;
149 6c1f42fe Andre Przywara
    kvm_trim_features(&env->cpuid_ext_features,
150 6c1f42fe Andre Przywara
        kvm_arch_get_supported_cpuid(env, 1, R_ECX));
151 6c0d7ee8 Andre Przywara
    env->cpuid_ext_features |= i;
152 6c0d7ee8 Andre Przywara
153 6c1f42fe Andre Przywara
    kvm_trim_features(&env->cpuid_ext2_features,
154 6c1f42fe Andre Przywara
        kvm_arch_get_supported_cpuid(env, 0x80000001, R_EDX));
155 6c1f42fe Andre Przywara
    kvm_trim_features(&env->cpuid_ext3_features,
156 6c1f42fe Andre Przywara
        kvm_arch_get_supported_cpuid(env, 0x80000001, R_ECX));
157 6c1f42fe Andre Przywara
158 05330448 aliguori
    cpuid_i = 0;
159 05330448 aliguori
160 a33609ca aliguori
    cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
161 05330448 aliguori
162 05330448 aliguori
    for (i = 0; i <= limit; i++) {
163 486bd5a2 aliguori
        struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
164 486bd5a2 aliguori
165 486bd5a2 aliguori
        switch (i) {
166 a36b1029 aliguori
        case 2: {
167 a36b1029 aliguori
            /* Keep reading function 2 till all the input is received */
168 a36b1029 aliguori
            int times;
169 a36b1029 aliguori
170 a36b1029 aliguori
            c->function = i;
171 a33609ca aliguori
            c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC |
172 a33609ca aliguori
                       KVM_CPUID_FLAG_STATE_READ_NEXT;
173 a33609ca aliguori
            cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
174 a33609ca aliguori
            times = c->eax & 0xff;
175 a36b1029 aliguori
176 a36b1029 aliguori
            for (j = 1; j < times; ++j) {
177 a33609ca aliguori
                c = &cpuid_data.entries[cpuid_i++];
178 a36b1029 aliguori
                c->function = i;
179 a33609ca aliguori
                c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC;
180 a33609ca aliguori
                cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
181 a36b1029 aliguori
            }
182 a36b1029 aliguori
            break;
183 a36b1029 aliguori
        }
184 486bd5a2 aliguori
        case 4:
185 486bd5a2 aliguori
        case 0xb:
186 486bd5a2 aliguori
        case 0xd:
187 486bd5a2 aliguori
            for (j = 0; ; j++) {
188 486bd5a2 aliguori
                c->function = i;
189 486bd5a2 aliguori
                c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
190 486bd5a2 aliguori
                c->index = j;
191 a33609ca aliguori
                cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx);
192 486bd5a2 aliguori
193 a33609ca aliguori
                if (i == 4 && c->eax == 0)
194 486bd5a2 aliguori
                    break;
195 a33609ca aliguori
                if (i == 0xb && !(c->ecx & 0xff00))
196 486bd5a2 aliguori
                    break;
197 a33609ca aliguori
                if (i == 0xd && c->eax == 0)
198 486bd5a2 aliguori
                    break;
199 a33609ca aliguori
200 a33609ca aliguori
                c = &cpuid_data.entries[cpuid_i++];
201 486bd5a2 aliguori
            }
202 486bd5a2 aliguori
            break;
203 486bd5a2 aliguori
        default:
204 486bd5a2 aliguori
            c->function = i;
205 a33609ca aliguori
            c->flags = 0;
206 a33609ca aliguori
            cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
207 486bd5a2 aliguori
            break;
208 486bd5a2 aliguori
        }
209 05330448 aliguori
    }
210 a33609ca aliguori
    cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused);
211 05330448 aliguori
212 05330448 aliguori
    for (i = 0x80000000; i <= limit; i++) {
213 486bd5a2 aliguori
        struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
214 05330448 aliguori
215 05330448 aliguori
        c->function = i;
216 a33609ca aliguori
        c->flags = 0;
217 a33609ca aliguori
        cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
218 05330448 aliguori
    }
219 05330448 aliguori
220 05330448 aliguori
    cpuid_data.cpuid.nent = cpuid_i;
221 05330448 aliguori
222 486bd5a2 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
223 05330448 aliguori
}
224 05330448 aliguori
225 caa5af0f Jan Kiszka
void kvm_arch_reset_vcpu(CPUState *env)
226 caa5af0f Jan Kiszka
{
227 0e607a80 Jan Kiszka
    env->interrupt_injected = -1;
228 a0fb002c Jan Kiszka
    env->nmi_injected = 0;
229 a0fb002c Jan Kiszka
    env->nmi_pending = 0;
230 caa5af0f Jan Kiszka
}
231 caa5af0f Jan Kiszka
232 05330448 aliguori
static int kvm_has_msr_star(CPUState *env)
233 05330448 aliguori
{
234 05330448 aliguori
    static int has_msr_star;
235 05330448 aliguori
    int ret;
236 05330448 aliguori
237 05330448 aliguori
    /* first time */
238 05330448 aliguori
    if (has_msr_star == 0) {        
239 05330448 aliguori
        struct kvm_msr_list msr_list, *kvm_msr_list;
240 05330448 aliguori
241 05330448 aliguori
        has_msr_star = -1;
242 05330448 aliguori
243 05330448 aliguori
        /* Obtain MSR list from KVM.  These are the MSRs that we must
244 05330448 aliguori
         * save/restore */
245 4c9f7372 aliguori
        msr_list.nmsrs = 0;
246 05330448 aliguori
        ret = kvm_ioctl(env->kvm_state, KVM_GET_MSR_INDEX_LIST, &msr_list);
247 6fb6d245 Jan Kiszka
        if (ret < 0 && ret != -E2BIG) {
248 05330448 aliguori
            return 0;
249 6fb6d245 Jan Kiszka
        }
250 d9db889f Jan Kiszka
        /* Old kernel modules had a bug and could write beyond the provided
251 d9db889f Jan Kiszka
           memory. Allocate at least a safe amount of 1K. */
252 d9db889f Jan Kiszka
        kvm_msr_list = qemu_mallocz(MAX(1024, sizeof(msr_list) +
253 d9db889f Jan Kiszka
                                              msr_list.nmsrs *
254 d9db889f Jan Kiszka
                                              sizeof(msr_list.indices[0])));
255 05330448 aliguori
256 55308450 aliguori
        kvm_msr_list->nmsrs = msr_list.nmsrs;
257 05330448 aliguori
        ret = kvm_ioctl(env->kvm_state, KVM_GET_MSR_INDEX_LIST, kvm_msr_list);
258 05330448 aliguori
        if (ret >= 0) {
259 05330448 aliguori
            int i;
260 05330448 aliguori
261 05330448 aliguori
            for (i = 0; i < kvm_msr_list->nmsrs; i++) {
262 05330448 aliguori
                if (kvm_msr_list->indices[i] == MSR_STAR) {
263 05330448 aliguori
                    has_msr_star = 1;
264 05330448 aliguori
                    break;
265 05330448 aliguori
                }
266 05330448 aliguori
            }
267 05330448 aliguori
        }
268 05330448 aliguori
269 05330448 aliguori
        free(kvm_msr_list);
270 05330448 aliguori
    }
271 05330448 aliguori
272 05330448 aliguori
    if (has_msr_star == 1)
273 05330448 aliguori
        return 1;
274 05330448 aliguori
    return 0;
275 05330448 aliguori
}
276 05330448 aliguori
277 05330448 aliguori
int kvm_arch_init(KVMState *s, int smp_cpus)
278 05330448 aliguori
{
279 05330448 aliguori
    int ret;
280 05330448 aliguori
281 05330448 aliguori
    /* create vm86 tss.  KVM uses vm86 mode to emulate 16-bit code
282 05330448 aliguori
     * directly.  In order to use vm86 mode, a TSS is needed.  Since this
283 05330448 aliguori
     * must be part of guest physical memory, we need to allocate it.  Older
284 05330448 aliguori
     * versions of KVM just assumed that it would be at the end of physical
285 05330448 aliguori
     * memory but that doesn't work with more than 4GB of memory.  We simply
286 05330448 aliguori
     * refuse to work with those older versions of KVM. */
287 984b5181 aliguori
    ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
288 05330448 aliguori
    if (ret <= 0) {
289 05330448 aliguori
        fprintf(stderr, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
290 05330448 aliguori
        return ret;
291 05330448 aliguori
    }
292 05330448 aliguori
293 05330448 aliguori
    /* this address is 3 pages before the bios, and the bios should present
294 05330448 aliguori
     * as unavaible memory.  FIXME, need to ensure the e820 map deals with
295 05330448 aliguori
     * this?
296 05330448 aliguori
     */
297 984b5181 aliguori
    return kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, 0xfffbd000);
298 05330448 aliguori
}
299 05330448 aliguori
                    
300 05330448 aliguori
static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
301 05330448 aliguori
{
302 05330448 aliguori
    lhs->selector = rhs->selector;
303 05330448 aliguori
    lhs->base = rhs->base;
304 05330448 aliguori
    lhs->limit = rhs->limit;
305 05330448 aliguori
    lhs->type = 3;
306 05330448 aliguori
    lhs->present = 1;
307 05330448 aliguori
    lhs->dpl = 3;
308 05330448 aliguori
    lhs->db = 0;
309 05330448 aliguori
    lhs->s = 1;
310 05330448 aliguori
    lhs->l = 0;
311 05330448 aliguori
    lhs->g = 0;
312 05330448 aliguori
    lhs->avl = 0;
313 05330448 aliguori
    lhs->unusable = 0;
314 05330448 aliguori
}
315 05330448 aliguori
316 05330448 aliguori
static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
317 05330448 aliguori
{
318 05330448 aliguori
    unsigned flags = rhs->flags;
319 05330448 aliguori
    lhs->selector = rhs->selector;
320 05330448 aliguori
    lhs->base = rhs->base;
321 05330448 aliguori
    lhs->limit = rhs->limit;
322 05330448 aliguori
    lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
323 05330448 aliguori
    lhs->present = (flags & DESC_P_MASK) != 0;
324 05330448 aliguori
    lhs->dpl = rhs->selector & 3;
325 05330448 aliguori
    lhs->db = (flags >> DESC_B_SHIFT) & 1;
326 05330448 aliguori
    lhs->s = (flags & DESC_S_MASK) != 0;
327 05330448 aliguori
    lhs->l = (flags >> DESC_L_SHIFT) & 1;
328 05330448 aliguori
    lhs->g = (flags & DESC_G_MASK) != 0;
329 05330448 aliguori
    lhs->avl = (flags & DESC_AVL_MASK) != 0;
330 05330448 aliguori
    lhs->unusable = 0;
331 05330448 aliguori
}
332 05330448 aliguori
333 05330448 aliguori
static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
334 05330448 aliguori
{
335 05330448 aliguori
    lhs->selector = rhs->selector;
336 05330448 aliguori
    lhs->base = rhs->base;
337 05330448 aliguori
    lhs->limit = rhs->limit;
338 05330448 aliguori
    lhs->flags =
339 05330448 aliguori
        (rhs->type << DESC_TYPE_SHIFT)
340 05330448 aliguori
        | (rhs->present * DESC_P_MASK)
341 05330448 aliguori
        | (rhs->dpl << DESC_DPL_SHIFT)
342 05330448 aliguori
        | (rhs->db << DESC_B_SHIFT)
343 05330448 aliguori
        | (rhs->s * DESC_S_MASK)
344 05330448 aliguori
        | (rhs->l << DESC_L_SHIFT)
345 05330448 aliguori
        | (rhs->g * DESC_G_MASK)
346 05330448 aliguori
        | (rhs->avl * DESC_AVL_MASK);
347 05330448 aliguori
}
348 05330448 aliguori
349 05330448 aliguori
static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
350 05330448 aliguori
{
351 05330448 aliguori
    if (set)
352 05330448 aliguori
        *kvm_reg = *qemu_reg;
353 05330448 aliguori
    else
354 05330448 aliguori
        *qemu_reg = *kvm_reg;
355 05330448 aliguori
}
356 05330448 aliguori
357 05330448 aliguori
static int kvm_getput_regs(CPUState *env, int set)
358 05330448 aliguori
{
359 05330448 aliguori
    struct kvm_regs regs;
360 05330448 aliguori
    int ret = 0;
361 05330448 aliguori
362 05330448 aliguori
    if (!set) {
363 05330448 aliguori
        ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
364 05330448 aliguori
        if (ret < 0)
365 05330448 aliguori
            return ret;
366 05330448 aliguori
    }
367 05330448 aliguori
368 05330448 aliguori
    kvm_getput_reg(&regs.rax, &env->regs[R_EAX], set);
369 05330448 aliguori
    kvm_getput_reg(&regs.rbx, &env->regs[R_EBX], set);
370 05330448 aliguori
    kvm_getput_reg(&regs.rcx, &env->regs[R_ECX], set);
371 05330448 aliguori
    kvm_getput_reg(&regs.rdx, &env->regs[R_EDX], set);
372 05330448 aliguori
    kvm_getput_reg(&regs.rsi, &env->regs[R_ESI], set);
373 05330448 aliguori
    kvm_getput_reg(&regs.rdi, &env->regs[R_EDI], set);
374 05330448 aliguori
    kvm_getput_reg(&regs.rsp, &env->regs[R_ESP], set);
375 05330448 aliguori
    kvm_getput_reg(&regs.rbp, &env->regs[R_EBP], set);
376 05330448 aliguori
#ifdef TARGET_X86_64
377 05330448 aliguori
    kvm_getput_reg(&regs.r8, &env->regs[8], set);
378 05330448 aliguori
    kvm_getput_reg(&regs.r9, &env->regs[9], set);
379 05330448 aliguori
    kvm_getput_reg(&regs.r10, &env->regs[10], set);
380 05330448 aliguori
    kvm_getput_reg(&regs.r11, &env->regs[11], set);
381 05330448 aliguori
    kvm_getput_reg(&regs.r12, &env->regs[12], set);
382 05330448 aliguori
    kvm_getput_reg(&regs.r13, &env->regs[13], set);
383 05330448 aliguori
    kvm_getput_reg(&regs.r14, &env->regs[14], set);
384 05330448 aliguori
    kvm_getput_reg(&regs.r15, &env->regs[15], set);
385 05330448 aliguori
#endif
386 05330448 aliguori
387 05330448 aliguori
    kvm_getput_reg(&regs.rflags, &env->eflags, set);
388 05330448 aliguori
    kvm_getput_reg(&regs.rip, &env->eip, set);
389 05330448 aliguori
390 05330448 aliguori
    if (set)
391 05330448 aliguori
        ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, &regs);
392 05330448 aliguori
393 05330448 aliguori
    return ret;
394 05330448 aliguori
}
395 05330448 aliguori
396 05330448 aliguori
static int kvm_put_fpu(CPUState *env)
397 05330448 aliguori
{
398 05330448 aliguori
    struct kvm_fpu fpu;
399 05330448 aliguori
    int i;
400 05330448 aliguori
401 05330448 aliguori
    memset(&fpu, 0, sizeof fpu);
402 05330448 aliguori
    fpu.fsw = env->fpus & ~(7 << 11);
403 05330448 aliguori
    fpu.fsw |= (env->fpstt & 7) << 11;
404 05330448 aliguori
    fpu.fcw = env->fpuc;
405 05330448 aliguori
    for (i = 0; i < 8; ++i)
406 05330448 aliguori
        fpu.ftwx |= (!env->fptags[i]) << i;
407 05330448 aliguori
    memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
408 05330448 aliguori
    memcpy(fpu.xmm, env->xmm_regs, sizeof env->xmm_regs);
409 05330448 aliguori
    fpu.mxcsr = env->mxcsr;
410 05330448 aliguori
411 05330448 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_FPU, &fpu);
412 05330448 aliguori
}
413 05330448 aliguori
414 05330448 aliguori
static int kvm_put_sregs(CPUState *env)
415 05330448 aliguori
{
416 05330448 aliguori
    struct kvm_sregs sregs;
417 05330448 aliguori
418 0e607a80 Jan Kiszka
    memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));
419 0e607a80 Jan Kiszka
    if (env->interrupt_injected >= 0) {
420 0e607a80 Jan Kiszka
        sregs.interrupt_bitmap[env->interrupt_injected / 64] |=
421 0e607a80 Jan Kiszka
                (uint64_t)1 << (env->interrupt_injected % 64);
422 0e607a80 Jan Kiszka
    }
423 05330448 aliguori
424 05330448 aliguori
    if ((env->eflags & VM_MASK)) {
425 05330448 aliguori
            set_v8086_seg(&sregs.cs, &env->segs[R_CS]);
426 05330448 aliguori
            set_v8086_seg(&sregs.ds, &env->segs[R_DS]);
427 05330448 aliguori
            set_v8086_seg(&sregs.es, &env->segs[R_ES]);
428 05330448 aliguori
            set_v8086_seg(&sregs.fs, &env->segs[R_FS]);
429 05330448 aliguori
            set_v8086_seg(&sregs.gs, &env->segs[R_GS]);
430 05330448 aliguori
            set_v8086_seg(&sregs.ss, &env->segs[R_SS]);
431 05330448 aliguori
    } else {
432 05330448 aliguori
            set_seg(&sregs.cs, &env->segs[R_CS]);
433 05330448 aliguori
            set_seg(&sregs.ds, &env->segs[R_DS]);
434 05330448 aliguori
            set_seg(&sregs.es, &env->segs[R_ES]);
435 05330448 aliguori
            set_seg(&sregs.fs, &env->segs[R_FS]);
436 05330448 aliguori
            set_seg(&sregs.gs, &env->segs[R_GS]);
437 05330448 aliguori
            set_seg(&sregs.ss, &env->segs[R_SS]);
438 05330448 aliguori
439 05330448 aliguori
            if (env->cr[0] & CR0_PE_MASK) {
440 05330448 aliguori
                /* force ss cpl to cs cpl */
441 05330448 aliguori
                sregs.ss.selector = (sregs.ss.selector & ~3) |
442 05330448 aliguori
                        (sregs.cs.selector & 3);
443 05330448 aliguori
                sregs.ss.dpl = sregs.ss.selector & 3;
444 05330448 aliguori
            }
445 05330448 aliguori
    }
446 05330448 aliguori
447 05330448 aliguori
    set_seg(&sregs.tr, &env->tr);
448 05330448 aliguori
    set_seg(&sregs.ldt, &env->ldt);
449 05330448 aliguori
450 05330448 aliguori
    sregs.idt.limit = env->idt.limit;
451 05330448 aliguori
    sregs.idt.base = env->idt.base;
452 05330448 aliguori
    sregs.gdt.limit = env->gdt.limit;
453 05330448 aliguori
    sregs.gdt.base = env->gdt.base;
454 05330448 aliguori
455 05330448 aliguori
    sregs.cr0 = env->cr[0];
456 05330448 aliguori
    sregs.cr2 = env->cr[2];
457 05330448 aliguori
    sregs.cr3 = env->cr[3];
458 05330448 aliguori
    sregs.cr4 = env->cr[4];
459 05330448 aliguori
460 05330448 aliguori
    sregs.cr8 = cpu_get_apic_tpr(env);
461 05330448 aliguori
    sregs.apic_base = cpu_get_apic_base(env);
462 05330448 aliguori
463 05330448 aliguori
    sregs.efer = env->efer;
464 05330448 aliguori
465 05330448 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_SREGS, &sregs);
466 05330448 aliguori
}
467 05330448 aliguori
468 05330448 aliguori
static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
469 05330448 aliguori
                              uint32_t index, uint64_t value)
470 05330448 aliguori
{
471 05330448 aliguori
    entry->index = index;
472 05330448 aliguori
    entry->data = value;
473 05330448 aliguori
}
474 05330448 aliguori
475 05330448 aliguori
static int kvm_put_msrs(CPUState *env)
476 05330448 aliguori
{
477 05330448 aliguori
    struct {
478 05330448 aliguori
        struct kvm_msrs info;
479 05330448 aliguori
        struct kvm_msr_entry entries[100];
480 05330448 aliguori
    } msr_data;
481 05330448 aliguori
    struct kvm_msr_entry *msrs = msr_data.entries;
482 05330448 aliguori
    int n = 0;
483 05330448 aliguori
484 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
485 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
486 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
487 05330448 aliguori
    if (kvm_has_msr_star(env))
488 05330448 aliguori
        kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
489 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
490 05330448 aliguori
#ifdef TARGET_X86_64
491 05330448 aliguori
    /* FIXME if lm capable */
492 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
493 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
494 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
495 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
496 05330448 aliguori
#endif
497 05330448 aliguori
    msr_data.info.nmsrs = n;
498 05330448 aliguori
499 05330448 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_MSRS, &msr_data);
500 05330448 aliguori
501 05330448 aliguori
}
502 05330448 aliguori
503 05330448 aliguori
504 05330448 aliguori
static int kvm_get_fpu(CPUState *env)
505 05330448 aliguori
{
506 05330448 aliguori
    struct kvm_fpu fpu;
507 05330448 aliguori
    int i, ret;
508 05330448 aliguori
509 05330448 aliguori
    ret = kvm_vcpu_ioctl(env, KVM_GET_FPU, &fpu);
510 05330448 aliguori
    if (ret < 0)
511 05330448 aliguori
        return ret;
512 05330448 aliguori
513 05330448 aliguori
    env->fpstt = (fpu.fsw >> 11) & 7;
514 05330448 aliguori
    env->fpus = fpu.fsw;
515 05330448 aliguori
    env->fpuc = fpu.fcw;
516 05330448 aliguori
    for (i = 0; i < 8; ++i)
517 05330448 aliguori
        env->fptags[i] = !((fpu.ftwx >> i) & 1);
518 05330448 aliguori
    memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
519 05330448 aliguori
    memcpy(env->xmm_regs, fpu.xmm, sizeof env->xmm_regs);
520 05330448 aliguori
    env->mxcsr = fpu.mxcsr;
521 05330448 aliguori
522 05330448 aliguori
    return 0;
523 05330448 aliguori
}
524 05330448 aliguori
525 05330448 aliguori
static int kvm_get_sregs(CPUState *env)
526 05330448 aliguori
{
527 05330448 aliguori
    struct kvm_sregs sregs;
528 05330448 aliguori
    uint32_t hflags;
529 0e607a80 Jan Kiszka
    int bit, i, ret;
530 05330448 aliguori
531 05330448 aliguori
    ret = kvm_vcpu_ioctl(env, KVM_GET_SREGS, &sregs);
532 05330448 aliguori
    if (ret < 0)
533 05330448 aliguori
        return ret;
534 05330448 aliguori
535 0e607a80 Jan Kiszka
    /* There can only be one pending IRQ set in the bitmap at a time, so try
536 0e607a80 Jan Kiszka
       to find it and save its number instead (-1 for none). */
537 0e607a80 Jan Kiszka
    env->interrupt_injected = -1;
538 0e607a80 Jan Kiszka
    for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) {
539 0e607a80 Jan Kiszka
        if (sregs.interrupt_bitmap[i]) {
540 0e607a80 Jan Kiszka
            bit = ctz64(sregs.interrupt_bitmap[i]);
541 0e607a80 Jan Kiszka
            env->interrupt_injected = i * 64 + bit;
542 0e607a80 Jan Kiszka
            break;
543 0e607a80 Jan Kiszka
        }
544 0e607a80 Jan Kiszka
    }
545 05330448 aliguori
546 05330448 aliguori
    get_seg(&env->segs[R_CS], &sregs.cs);
547 05330448 aliguori
    get_seg(&env->segs[R_DS], &sregs.ds);
548 05330448 aliguori
    get_seg(&env->segs[R_ES], &sregs.es);
549 05330448 aliguori
    get_seg(&env->segs[R_FS], &sregs.fs);
550 05330448 aliguori
    get_seg(&env->segs[R_GS], &sregs.gs);
551 05330448 aliguori
    get_seg(&env->segs[R_SS], &sregs.ss);
552 05330448 aliguori
553 05330448 aliguori
    get_seg(&env->tr, &sregs.tr);
554 05330448 aliguori
    get_seg(&env->ldt, &sregs.ldt);
555 05330448 aliguori
556 05330448 aliguori
    env->idt.limit = sregs.idt.limit;
557 05330448 aliguori
    env->idt.base = sregs.idt.base;
558 05330448 aliguori
    env->gdt.limit = sregs.gdt.limit;
559 05330448 aliguori
    env->gdt.base = sregs.gdt.base;
560 05330448 aliguori
561 05330448 aliguori
    env->cr[0] = sregs.cr0;
562 05330448 aliguori
    env->cr[2] = sregs.cr2;
563 05330448 aliguori
    env->cr[3] = sregs.cr3;
564 05330448 aliguori
    env->cr[4] = sregs.cr4;
565 05330448 aliguori
566 05330448 aliguori
    cpu_set_apic_base(env, sregs.apic_base);
567 05330448 aliguori
568 05330448 aliguori
    env->efer = sregs.efer;
569 05330448 aliguori
    //cpu_set_apic_tpr(env, sregs.cr8);
570 05330448 aliguori
571 05330448 aliguori
#define HFLAG_COPY_MASK ~( \
572 05330448 aliguori
                        HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
573 05330448 aliguori
                        HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
574 05330448 aliguori
                        HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
575 05330448 aliguori
                        HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
576 05330448 aliguori
577 05330448 aliguori
578 05330448 aliguori
579 05330448 aliguori
    hflags = (env->segs[R_CS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;
580 05330448 aliguori
    hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
581 05330448 aliguori
    hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
582 05330448 aliguori
            (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK);
583 05330448 aliguori
    hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK));
584 05330448 aliguori
    hflags |= (env->cr[4] & CR4_OSFXSR_MASK) <<
585 05330448 aliguori
            (HF_OSFXSR_SHIFT - CR4_OSFXSR_SHIFT);
586 05330448 aliguori
587 05330448 aliguori
    if (env->efer & MSR_EFER_LMA) {
588 05330448 aliguori
        hflags |= HF_LMA_MASK;
589 05330448 aliguori
    }
590 05330448 aliguori
591 05330448 aliguori
    if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
592 05330448 aliguori
        hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
593 05330448 aliguori
    } else {
594 05330448 aliguori
        hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >>
595 05330448 aliguori
                (DESC_B_SHIFT - HF_CS32_SHIFT);
596 05330448 aliguori
        hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >>
597 05330448 aliguori
                (DESC_B_SHIFT - HF_SS32_SHIFT);
598 05330448 aliguori
        if (!(env->cr[0] & CR0_PE_MASK) ||
599 05330448 aliguori
                   (env->eflags & VM_MASK) ||
600 05330448 aliguori
                   !(hflags & HF_CS32_MASK)) {
601 05330448 aliguori
                hflags |= HF_ADDSEG_MASK;
602 05330448 aliguori
            } else {
603 05330448 aliguori
                hflags |= ((env->segs[R_DS].base |
604 05330448 aliguori
                                env->segs[R_ES].base |
605 05330448 aliguori
                                env->segs[R_SS].base) != 0) <<
606 05330448 aliguori
                    HF_ADDSEG_SHIFT;
607 05330448 aliguori
            }
608 05330448 aliguori
    }
609 05330448 aliguori
    env->hflags = (env->hflags & HFLAG_COPY_MASK) | hflags;
610 05330448 aliguori
611 05330448 aliguori
    return 0;
612 05330448 aliguori
}
613 05330448 aliguori
614 05330448 aliguori
static int kvm_get_msrs(CPUState *env)
615 05330448 aliguori
{
616 05330448 aliguori
    struct {
617 05330448 aliguori
        struct kvm_msrs info;
618 05330448 aliguori
        struct kvm_msr_entry entries[100];
619 05330448 aliguori
    } msr_data;
620 05330448 aliguori
    struct kvm_msr_entry *msrs = msr_data.entries;
621 05330448 aliguori
    int ret, i, n;
622 05330448 aliguori
623 05330448 aliguori
    n = 0;
624 05330448 aliguori
    msrs[n++].index = MSR_IA32_SYSENTER_CS;
625 05330448 aliguori
    msrs[n++].index = MSR_IA32_SYSENTER_ESP;
626 05330448 aliguori
    msrs[n++].index = MSR_IA32_SYSENTER_EIP;
627 05330448 aliguori
    if (kvm_has_msr_star(env))
628 05330448 aliguori
        msrs[n++].index = MSR_STAR;
629 05330448 aliguori
    msrs[n++].index = MSR_IA32_TSC;
630 05330448 aliguori
#ifdef TARGET_X86_64
631 05330448 aliguori
    /* FIXME lm_capable_kernel */
632 05330448 aliguori
    msrs[n++].index = MSR_CSTAR;
633 05330448 aliguori
    msrs[n++].index = MSR_KERNELGSBASE;
634 05330448 aliguori
    msrs[n++].index = MSR_FMASK;
635 05330448 aliguori
    msrs[n++].index = MSR_LSTAR;
636 05330448 aliguori
#endif
637 05330448 aliguori
    msr_data.info.nmsrs = n;
638 05330448 aliguori
    ret = kvm_vcpu_ioctl(env, KVM_GET_MSRS, &msr_data);
639 05330448 aliguori
    if (ret < 0)
640 05330448 aliguori
        return ret;
641 05330448 aliguori
642 05330448 aliguori
    for (i = 0; i < ret; i++) {
643 05330448 aliguori
        switch (msrs[i].index) {
644 05330448 aliguori
        case MSR_IA32_SYSENTER_CS:
645 05330448 aliguori
            env->sysenter_cs = msrs[i].data;
646 05330448 aliguori
            break;
647 05330448 aliguori
        case MSR_IA32_SYSENTER_ESP:
648 05330448 aliguori
            env->sysenter_esp = msrs[i].data;
649 05330448 aliguori
            break;
650 05330448 aliguori
        case MSR_IA32_SYSENTER_EIP:
651 05330448 aliguori
            env->sysenter_eip = msrs[i].data;
652 05330448 aliguori
            break;
653 05330448 aliguori
        case MSR_STAR:
654 05330448 aliguori
            env->star = msrs[i].data;
655 05330448 aliguori
            break;
656 05330448 aliguori
#ifdef TARGET_X86_64
657 05330448 aliguori
        case MSR_CSTAR:
658 05330448 aliguori
            env->cstar = msrs[i].data;
659 05330448 aliguori
            break;
660 05330448 aliguori
        case MSR_KERNELGSBASE:
661 05330448 aliguori
            env->kernelgsbase = msrs[i].data;
662 05330448 aliguori
            break;
663 05330448 aliguori
        case MSR_FMASK:
664 05330448 aliguori
            env->fmask = msrs[i].data;
665 05330448 aliguori
            break;
666 05330448 aliguori
        case MSR_LSTAR:
667 05330448 aliguori
            env->lstar = msrs[i].data;
668 05330448 aliguori
            break;
669 05330448 aliguori
#endif
670 05330448 aliguori
        case MSR_IA32_TSC:
671 05330448 aliguori
            env->tsc = msrs[i].data;
672 05330448 aliguori
            break;
673 05330448 aliguori
        }
674 05330448 aliguori
    }
675 05330448 aliguori
676 05330448 aliguori
    return 0;
677 05330448 aliguori
}
678 05330448 aliguori
679 9bdbe550 Hollis Blanchard
static int kvm_put_mp_state(CPUState *env)
680 9bdbe550 Hollis Blanchard
{
681 9bdbe550 Hollis Blanchard
    struct kvm_mp_state mp_state = { .mp_state = env->mp_state };
682 9bdbe550 Hollis Blanchard
683 9bdbe550 Hollis Blanchard
    return kvm_vcpu_ioctl(env, KVM_SET_MP_STATE, &mp_state);
684 9bdbe550 Hollis Blanchard
}
685 9bdbe550 Hollis Blanchard
686 9bdbe550 Hollis Blanchard
static int kvm_get_mp_state(CPUState *env)
687 9bdbe550 Hollis Blanchard
{
688 9bdbe550 Hollis Blanchard
    struct kvm_mp_state mp_state;
689 9bdbe550 Hollis Blanchard
    int ret;
690 9bdbe550 Hollis Blanchard
691 9bdbe550 Hollis Blanchard
    ret = kvm_vcpu_ioctl(env, KVM_GET_MP_STATE, &mp_state);
692 9bdbe550 Hollis Blanchard
    if (ret < 0) {
693 9bdbe550 Hollis Blanchard
        return ret;
694 9bdbe550 Hollis Blanchard
    }
695 9bdbe550 Hollis Blanchard
    env->mp_state = mp_state.mp_state;
696 9bdbe550 Hollis Blanchard
    return 0;
697 9bdbe550 Hollis Blanchard
}
698 9bdbe550 Hollis Blanchard
699 a0fb002c Jan Kiszka
static int kvm_put_vcpu_events(CPUState *env)
700 a0fb002c Jan Kiszka
{
701 a0fb002c Jan Kiszka
#ifdef KVM_CAP_VCPU_EVENTS
702 a0fb002c Jan Kiszka
    struct kvm_vcpu_events events;
703 a0fb002c Jan Kiszka
704 a0fb002c Jan Kiszka
    if (!kvm_has_vcpu_events()) {
705 a0fb002c Jan Kiszka
        return 0;
706 a0fb002c Jan Kiszka
    }
707 a0fb002c Jan Kiszka
708 a0fb002c Jan Kiszka
    events.exception.injected = (env->exception_index >= 0);
709 a0fb002c Jan Kiszka
    events.exception.nr = env->exception_index;
710 a0fb002c Jan Kiszka
    events.exception.has_error_code = env->has_error_code;
711 a0fb002c Jan Kiszka
    events.exception.error_code = env->error_code;
712 a0fb002c Jan Kiszka
713 a0fb002c Jan Kiszka
    events.interrupt.injected = (env->interrupt_injected >= 0);
714 a0fb002c Jan Kiszka
    events.interrupt.nr = env->interrupt_injected;
715 a0fb002c Jan Kiszka
    events.interrupt.soft = env->soft_interrupt;
716 a0fb002c Jan Kiszka
717 a0fb002c Jan Kiszka
    events.nmi.injected = env->nmi_injected;
718 a0fb002c Jan Kiszka
    events.nmi.pending = env->nmi_pending;
719 a0fb002c Jan Kiszka
    events.nmi.masked = !!(env->hflags2 & HF2_NMI_MASK);
720 a0fb002c Jan Kiszka
721 a0fb002c Jan Kiszka
    events.sipi_vector = env->sipi_vector;
722 a0fb002c Jan Kiszka
723 a0fb002c Jan Kiszka
    return kvm_vcpu_ioctl(env, KVM_SET_VCPU_EVENTS, &events);
724 a0fb002c Jan Kiszka
#else
725 a0fb002c Jan Kiszka
    return 0;
726 a0fb002c Jan Kiszka
#endif
727 a0fb002c Jan Kiszka
}
728 a0fb002c Jan Kiszka
729 a0fb002c Jan Kiszka
static int kvm_get_vcpu_events(CPUState *env)
730 a0fb002c Jan Kiszka
{
731 a0fb002c Jan Kiszka
#ifdef KVM_CAP_VCPU_EVENTS
732 a0fb002c Jan Kiszka
    struct kvm_vcpu_events events;
733 a0fb002c Jan Kiszka
    int ret;
734 a0fb002c Jan Kiszka
735 a0fb002c Jan Kiszka
    if (!kvm_has_vcpu_events()) {
736 a0fb002c Jan Kiszka
        return 0;
737 a0fb002c Jan Kiszka
    }
738 a0fb002c Jan Kiszka
739 a0fb002c Jan Kiszka
    ret = kvm_vcpu_ioctl(env, KVM_GET_VCPU_EVENTS, &events);
740 a0fb002c Jan Kiszka
    if (ret < 0) {
741 a0fb002c Jan Kiszka
       return ret;
742 a0fb002c Jan Kiszka
    }
743 a0fb002c Jan Kiszka
    env->exception_index =
744 a0fb002c Jan Kiszka
       events.exception.injected ? events.exception.nr : -1;
745 a0fb002c Jan Kiszka
    env->has_error_code = events.exception.has_error_code;
746 a0fb002c Jan Kiszka
    env->error_code = events.exception.error_code;
747 a0fb002c Jan Kiszka
748 a0fb002c Jan Kiszka
    env->interrupt_injected =
749 a0fb002c Jan Kiszka
        events.interrupt.injected ? events.interrupt.nr : -1;
750 a0fb002c Jan Kiszka
    env->soft_interrupt = events.interrupt.soft;
751 a0fb002c Jan Kiszka
752 a0fb002c Jan Kiszka
    env->nmi_injected = events.nmi.injected;
753 a0fb002c Jan Kiszka
    env->nmi_pending = events.nmi.pending;
754 a0fb002c Jan Kiszka
    if (events.nmi.masked) {
755 a0fb002c Jan Kiszka
        env->hflags2 |= HF2_NMI_MASK;
756 a0fb002c Jan Kiszka
    } else {
757 a0fb002c Jan Kiszka
        env->hflags2 &= ~HF2_NMI_MASK;
758 a0fb002c Jan Kiszka
    }
759 a0fb002c Jan Kiszka
760 a0fb002c Jan Kiszka
    env->sipi_vector = events.sipi_vector;
761 a0fb002c Jan Kiszka
#endif
762 a0fb002c Jan Kiszka
763 a0fb002c Jan Kiszka
    return 0;
764 a0fb002c Jan Kiszka
}
765 a0fb002c Jan Kiszka
766 05330448 aliguori
int kvm_arch_put_registers(CPUState *env)
767 05330448 aliguori
{
768 05330448 aliguori
    int ret;
769 05330448 aliguori
770 05330448 aliguori
    ret = kvm_getput_regs(env, 1);
771 05330448 aliguori
    if (ret < 0)
772 05330448 aliguori
        return ret;
773 05330448 aliguori
774 05330448 aliguori
    ret = kvm_put_fpu(env);
775 05330448 aliguori
    if (ret < 0)
776 05330448 aliguori
        return ret;
777 05330448 aliguori
778 05330448 aliguori
    ret = kvm_put_sregs(env);
779 05330448 aliguori
    if (ret < 0)
780 05330448 aliguori
        return ret;
781 05330448 aliguori
782 05330448 aliguori
    ret = kvm_put_msrs(env);
783 05330448 aliguori
    if (ret < 0)
784 05330448 aliguori
        return ret;
785 05330448 aliguori
786 f8d926e9 Jan Kiszka
    ret = kvm_put_mp_state(env);
787 f8d926e9 Jan Kiszka
    if (ret < 0)
788 f8d926e9 Jan Kiszka
        return ret;
789 f8d926e9 Jan Kiszka
790 a0fb002c Jan Kiszka
    ret = kvm_put_vcpu_events(env);
791 a0fb002c Jan Kiszka
    if (ret < 0)
792 a0fb002c Jan Kiszka
        return ret;
793 a0fb002c Jan Kiszka
794 05330448 aliguori
    return 0;
795 05330448 aliguori
}
796 05330448 aliguori
797 05330448 aliguori
int kvm_arch_get_registers(CPUState *env)
798 05330448 aliguori
{
799 05330448 aliguori
    int ret;
800 05330448 aliguori
801 05330448 aliguori
    ret = kvm_getput_regs(env, 0);
802 05330448 aliguori
    if (ret < 0)
803 05330448 aliguori
        return ret;
804 05330448 aliguori
805 05330448 aliguori
    ret = kvm_get_fpu(env);
806 05330448 aliguori
    if (ret < 0)
807 05330448 aliguori
        return ret;
808 05330448 aliguori
809 05330448 aliguori
    ret = kvm_get_sregs(env);
810 05330448 aliguori
    if (ret < 0)
811 05330448 aliguori
        return ret;
812 05330448 aliguori
813 05330448 aliguori
    ret = kvm_get_msrs(env);
814 05330448 aliguori
    if (ret < 0)
815 05330448 aliguori
        return ret;
816 05330448 aliguori
817 5a2e3c2e Jan Kiszka
    ret = kvm_get_mp_state(env);
818 5a2e3c2e Jan Kiszka
    if (ret < 0)
819 5a2e3c2e Jan Kiszka
        return ret;
820 5a2e3c2e Jan Kiszka
821 a0fb002c Jan Kiszka
    ret = kvm_get_vcpu_events(env);
822 a0fb002c Jan Kiszka
    if (ret < 0)
823 a0fb002c Jan Kiszka
        return ret;
824 a0fb002c Jan Kiszka
825 05330448 aliguori
    return 0;
826 05330448 aliguori
}
827 05330448 aliguori
828 05330448 aliguori
int kvm_arch_pre_run(CPUState *env, struct kvm_run *run)
829 05330448 aliguori
{
830 05330448 aliguori
    /* Try to inject an interrupt if the guest can accept it */
831 05330448 aliguori
    if (run->ready_for_interrupt_injection &&
832 05330448 aliguori
        (env->interrupt_request & CPU_INTERRUPT_HARD) &&
833 05330448 aliguori
        (env->eflags & IF_MASK)) {
834 05330448 aliguori
        int irq;
835 05330448 aliguori
836 05330448 aliguori
        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
837 05330448 aliguori
        irq = cpu_get_pic_interrupt(env);
838 05330448 aliguori
        if (irq >= 0) {
839 05330448 aliguori
            struct kvm_interrupt intr;
840 05330448 aliguori
            intr.irq = irq;
841 05330448 aliguori
            /* FIXME: errors */
842 05330448 aliguori
            dprintf("injected interrupt %d\n", irq);
843 05330448 aliguori
            kvm_vcpu_ioctl(env, KVM_INTERRUPT, &intr);
844 05330448 aliguori
        }
845 05330448 aliguori
    }
846 05330448 aliguori
847 05330448 aliguori
    /* If we have an interrupt but the guest is not ready to receive an
848 05330448 aliguori
     * interrupt, request an interrupt window exit.  This will
849 05330448 aliguori
     * cause a return to userspace as soon as the guest is ready to
850 05330448 aliguori
     * receive interrupts. */
851 05330448 aliguori
    if ((env->interrupt_request & CPU_INTERRUPT_HARD))
852 05330448 aliguori
        run->request_interrupt_window = 1;
853 05330448 aliguori
    else
854 05330448 aliguori
        run->request_interrupt_window = 0;
855 05330448 aliguori
856 05330448 aliguori
    dprintf("setting tpr\n");
857 05330448 aliguori
    run->cr8 = cpu_get_apic_tpr(env);
858 05330448 aliguori
859 05330448 aliguori
    return 0;
860 05330448 aliguori
}
861 05330448 aliguori
862 05330448 aliguori
int kvm_arch_post_run(CPUState *env, struct kvm_run *run)
863 05330448 aliguori
{
864 05330448 aliguori
    if (run->if_flag)
865 05330448 aliguori
        env->eflags |= IF_MASK;
866 05330448 aliguori
    else
867 05330448 aliguori
        env->eflags &= ~IF_MASK;
868 05330448 aliguori
    
869 05330448 aliguori
    cpu_set_apic_tpr(env, run->cr8);
870 05330448 aliguori
    cpu_set_apic_base(env, run->apic_base);
871 05330448 aliguori
872 05330448 aliguori
    return 0;
873 05330448 aliguori
}
874 05330448 aliguori
875 05330448 aliguori
static int kvm_handle_halt(CPUState *env)
876 05330448 aliguori
{
877 05330448 aliguori
    if (!((env->interrupt_request & CPU_INTERRUPT_HARD) &&
878 05330448 aliguori
          (env->eflags & IF_MASK)) &&
879 05330448 aliguori
        !(env->interrupt_request & CPU_INTERRUPT_NMI)) {
880 05330448 aliguori
        env->halted = 1;
881 05330448 aliguori
        env->exception_index = EXCP_HLT;
882 05330448 aliguori
        return 0;
883 05330448 aliguori
    }
884 05330448 aliguori
885 05330448 aliguori
    return 1;
886 05330448 aliguori
}
887 05330448 aliguori
888 05330448 aliguori
int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
889 05330448 aliguori
{
890 05330448 aliguori
    int ret = 0;
891 05330448 aliguori
892 05330448 aliguori
    switch (run->exit_reason) {
893 05330448 aliguori
    case KVM_EXIT_HLT:
894 05330448 aliguori
        dprintf("handle_hlt\n");
895 05330448 aliguori
        ret = kvm_handle_halt(env);
896 05330448 aliguori
        break;
897 05330448 aliguori
    }
898 05330448 aliguori
899 05330448 aliguori
    return ret;
900 05330448 aliguori
}
901 e22a25c9 aliguori
902 e22a25c9 aliguori
#ifdef KVM_CAP_SET_GUEST_DEBUG
903 e22a25c9 aliguori
int kvm_arch_insert_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
904 e22a25c9 aliguori
{
905 38972938 Juan Quintela
    static const uint8_t int3 = 0xcc;
906 64bf3f4e aliguori
907 e22a25c9 aliguori
    if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) ||
908 64bf3f4e aliguori
        cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&int3, 1, 1))
909 e22a25c9 aliguori
        return -EINVAL;
910 e22a25c9 aliguori
    return 0;
911 e22a25c9 aliguori
}
912 e22a25c9 aliguori
913 e22a25c9 aliguori
int kvm_arch_remove_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
914 e22a25c9 aliguori
{
915 e22a25c9 aliguori
    uint8_t int3;
916 e22a25c9 aliguori
917 e22a25c9 aliguori
    if (cpu_memory_rw_debug(env, bp->pc, &int3, 1, 0) || int3 != 0xcc ||
918 64bf3f4e aliguori
        cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1))
919 e22a25c9 aliguori
        return -EINVAL;
920 e22a25c9 aliguori
    return 0;
921 e22a25c9 aliguori
}
922 e22a25c9 aliguori
923 e22a25c9 aliguori
static struct {
924 e22a25c9 aliguori
    target_ulong addr;
925 e22a25c9 aliguori
    int len;
926 e22a25c9 aliguori
    int type;
927 e22a25c9 aliguori
} hw_breakpoint[4];
928 e22a25c9 aliguori
929 e22a25c9 aliguori
static int nb_hw_breakpoint;
930 e22a25c9 aliguori
931 e22a25c9 aliguori
static int find_hw_breakpoint(target_ulong addr, int len, int type)
932 e22a25c9 aliguori
{
933 e22a25c9 aliguori
    int n;
934 e22a25c9 aliguori
935 e22a25c9 aliguori
    for (n = 0; n < nb_hw_breakpoint; n++)
936 e22a25c9 aliguori
        if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
937 e22a25c9 aliguori
            (hw_breakpoint[n].len == len || len == -1))
938 e22a25c9 aliguori
            return n;
939 e22a25c9 aliguori
    return -1;
940 e22a25c9 aliguori
}
941 e22a25c9 aliguori
942 e22a25c9 aliguori
int kvm_arch_insert_hw_breakpoint(target_ulong addr,
943 e22a25c9 aliguori
                                  target_ulong len, int type)
944 e22a25c9 aliguori
{
945 e22a25c9 aliguori
    switch (type) {
946 e22a25c9 aliguori
    case GDB_BREAKPOINT_HW:
947 e22a25c9 aliguori
        len = 1;
948 e22a25c9 aliguori
        break;
949 e22a25c9 aliguori
    case GDB_WATCHPOINT_WRITE:
950 e22a25c9 aliguori
    case GDB_WATCHPOINT_ACCESS:
951 e22a25c9 aliguori
        switch (len) {
952 e22a25c9 aliguori
        case 1:
953 e22a25c9 aliguori
            break;
954 e22a25c9 aliguori
        case 2:
955 e22a25c9 aliguori
        case 4:
956 e22a25c9 aliguori
        case 8:
957 e22a25c9 aliguori
            if (addr & (len - 1))
958 e22a25c9 aliguori
                return -EINVAL;
959 e22a25c9 aliguori
            break;
960 e22a25c9 aliguori
        default:
961 e22a25c9 aliguori
            return -EINVAL;
962 e22a25c9 aliguori
        }
963 e22a25c9 aliguori
        break;
964 e22a25c9 aliguori
    default:
965 e22a25c9 aliguori
        return -ENOSYS;
966 e22a25c9 aliguori
    }
967 e22a25c9 aliguori
968 e22a25c9 aliguori
    if (nb_hw_breakpoint == 4)
969 e22a25c9 aliguori
        return -ENOBUFS;
970 e22a25c9 aliguori
971 e22a25c9 aliguori
    if (find_hw_breakpoint(addr, len, type) >= 0)
972 e22a25c9 aliguori
        return -EEXIST;
973 e22a25c9 aliguori
974 e22a25c9 aliguori
    hw_breakpoint[nb_hw_breakpoint].addr = addr;
975 e22a25c9 aliguori
    hw_breakpoint[nb_hw_breakpoint].len = len;
976 e22a25c9 aliguori
    hw_breakpoint[nb_hw_breakpoint].type = type;
977 e22a25c9 aliguori
    nb_hw_breakpoint++;
978 e22a25c9 aliguori
979 e22a25c9 aliguori
    return 0;
980 e22a25c9 aliguori
}
981 e22a25c9 aliguori
982 e22a25c9 aliguori
int kvm_arch_remove_hw_breakpoint(target_ulong addr,
983 e22a25c9 aliguori
                                  target_ulong len, int type)
984 e22a25c9 aliguori
{
985 e22a25c9 aliguori
    int n;
986 e22a25c9 aliguori
987 e22a25c9 aliguori
    n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
988 e22a25c9 aliguori
    if (n < 0)
989 e22a25c9 aliguori
        return -ENOENT;
990 e22a25c9 aliguori
991 e22a25c9 aliguori
    nb_hw_breakpoint--;
992 e22a25c9 aliguori
    hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint];
993 e22a25c9 aliguori
994 e22a25c9 aliguori
    return 0;
995 e22a25c9 aliguori
}
996 e22a25c9 aliguori
997 e22a25c9 aliguori
void kvm_arch_remove_all_hw_breakpoints(void)
998 e22a25c9 aliguori
{
999 e22a25c9 aliguori
    nb_hw_breakpoint = 0;
1000 e22a25c9 aliguori
}
1001 e22a25c9 aliguori
1002 e22a25c9 aliguori
static CPUWatchpoint hw_watchpoint;
1003 e22a25c9 aliguori
1004 e22a25c9 aliguori
int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
1005 e22a25c9 aliguori
{
1006 e22a25c9 aliguori
    int handle = 0;
1007 e22a25c9 aliguori
    int n;
1008 e22a25c9 aliguori
1009 e22a25c9 aliguori
    if (arch_info->exception == 1) {
1010 e22a25c9 aliguori
        if (arch_info->dr6 & (1 << 14)) {
1011 e22a25c9 aliguori
            if (cpu_single_env->singlestep_enabled)
1012 e22a25c9 aliguori
                handle = 1;
1013 e22a25c9 aliguori
        } else {
1014 e22a25c9 aliguori
            for (n = 0; n < 4; n++)
1015 e22a25c9 aliguori
                if (arch_info->dr6 & (1 << n))
1016 e22a25c9 aliguori
                    switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
1017 e22a25c9 aliguori
                    case 0x0:
1018 e22a25c9 aliguori
                        handle = 1;
1019 e22a25c9 aliguori
                        break;
1020 e22a25c9 aliguori
                    case 0x1:
1021 e22a25c9 aliguori
                        handle = 1;
1022 e22a25c9 aliguori
                        cpu_single_env->watchpoint_hit = &hw_watchpoint;
1023 e22a25c9 aliguori
                        hw_watchpoint.vaddr = hw_breakpoint[n].addr;
1024 e22a25c9 aliguori
                        hw_watchpoint.flags = BP_MEM_WRITE;
1025 e22a25c9 aliguori
                        break;
1026 e22a25c9 aliguori
                    case 0x3:
1027 e22a25c9 aliguori
                        handle = 1;
1028 e22a25c9 aliguori
                        cpu_single_env->watchpoint_hit = &hw_watchpoint;
1029 e22a25c9 aliguori
                        hw_watchpoint.vaddr = hw_breakpoint[n].addr;
1030 e22a25c9 aliguori
                        hw_watchpoint.flags = BP_MEM_ACCESS;
1031 e22a25c9 aliguori
                        break;
1032 e22a25c9 aliguori
                    }
1033 e22a25c9 aliguori
        }
1034 e22a25c9 aliguori
    } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc))
1035 e22a25c9 aliguori
        handle = 1;
1036 e22a25c9 aliguori
1037 e22a25c9 aliguori
    if (!handle)
1038 e22a25c9 aliguori
        kvm_update_guest_debug(cpu_single_env,
1039 e22a25c9 aliguori
                        (arch_info->exception == 1) ?
1040 e22a25c9 aliguori
                        KVM_GUESTDBG_INJECT_DB : KVM_GUESTDBG_INJECT_BP);
1041 e22a25c9 aliguori
1042 e22a25c9 aliguori
    return handle;
1043 e22a25c9 aliguori
}
1044 e22a25c9 aliguori
1045 e22a25c9 aliguori
void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
1046 e22a25c9 aliguori
{
1047 e22a25c9 aliguori
    const uint8_t type_code[] = {
1048 e22a25c9 aliguori
        [GDB_BREAKPOINT_HW] = 0x0,
1049 e22a25c9 aliguori
        [GDB_WATCHPOINT_WRITE] = 0x1,
1050 e22a25c9 aliguori
        [GDB_WATCHPOINT_ACCESS] = 0x3
1051 e22a25c9 aliguori
    };
1052 e22a25c9 aliguori
    const uint8_t len_code[] = {
1053 e22a25c9 aliguori
        [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1054 e22a25c9 aliguori
    };
1055 e22a25c9 aliguori
    int n;
1056 e22a25c9 aliguori
1057 e22a25c9 aliguori
    if (kvm_sw_breakpoints_active(env))
1058 e22a25c9 aliguori
        dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1059 e22a25c9 aliguori
1060 e22a25c9 aliguori
    if (nb_hw_breakpoint > 0) {
1061 e22a25c9 aliguori
        dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1062 e22a25c9 aliguori
        dbg->arch.debugreg[7] = 0x0600;
1063 e22a25c9 aliguori
        for (n = 0; n < nb_hw_breakpoint; n++) {
1064 e22a25c9 aliguori
            dbg->arch.debugreg[n] = hw_breakpoint[n].addr;
1065 e22a25c9 aliguori
            dbg->arch.debugreg[7] |= (2 << (n * 2)) |
1066 e22a25c9 aliguori
                (type_code[hw_breakpoint[n].type] << (16 + n*4)) |
1067 e22a25c9 aliguori
                (len_code[hw_breakpoint[n].len] << (18 + n*4));
1068 e22a25c9 aliguori
        }
1069 e22a25c9 aliguori
    }
1070 e22a25c9 aliguori
}
1071 e22a25c9 aliguori
#endif /* KVM_CAP_SET_GUEST_DEBUG */