Statistics
| Branch: | Revision:

root / target-i386 / kvm.c @ 408392b3

History | View | Annotate | Download (38.2 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 4c5b10b7 Jes Sorensen
#include "hw/pc.h"
28 408392b3 Sheng Yang
#include "hw/apic.h"
29 35bed8ee Paul Brook
#include "ioport.h"
30 05330448 aliguori
31 bb0300dc Gleb Natapov
#ifdef CONFIG_KVM_PARA
32 bb0300dc Gleb Natapov
#include <linux/kvm_para.h>
33 bb0300dc Gleb Natapov
#endif
34 bb0300dc Gleb Natapov
//
35 05330448 aliguori
//#define DEBUG_KVM
36 05330448 aliguori
37 05330448 aliguori
#ifdef DEBUG_KVM
38 8c0d577e Blue Swirl
#define DPRINTF(fmt, ...) \
39 05330448 aliguori
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40 05330448 aliguori
#else
41 8c0d577e Blue Swirl
#define DPRINTF(fmt, ...) \
42 05330448 aliguori
    do { } while (0)
43 05330448 aliguori
#endif
44 05330448 aliguori
45 1a03675d Glauber Costa
#define MSR_KVM_WALL_CLOCK  0x11
46 1a03675d Glauber Costa
#define MSR_KVM_SYSTEM_TIME 0x12
47 1a03675d Glauber Costa
48 b827df58 Avi Kivity
#ifdef KVM_CAP_EXT_CPUID
49 b827df58 Avi Kivity
50 b827df58 Avi Kivity
static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
51 b827df58 Avi Kivity
{
52 b827df58 Avi Kivity
    struct kvm_cpuid2 *cpuid;
53 b827df58 Avi Kivity
    int r, size;
54 b827df58 Avi Kivity
55 b827df58 Avi Kivity
    size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
56 b827df58 Avi Kivity
    cpuid = (struct kvm_cpuid2 *)qemu_mallocz(size);
57 b827df58 Avi Kivity
    cpuid->nent = max;
58 b827df58 Avi Kivity
    r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid);
59 76ae317f Mark McLoughlin
    if (r == 0 && cpuid->nent >= max) {
60 76ae317f Mark McLoughlin
        r = -E2BIG;
61 76ae317f Mark McLoughlin
    }
62 b827df58 Avi Kivity
    if (r < 0) {
63 b827df58 Avi Kivity
        if (r == -E2BIG) {
64 b827df58 Avi Kivity
            qemu_free(cpuid);
65 b827df58 Avi Kivity
            return NULL;
66 b827df58 Avi Kivity
        } else {
67 b827df58 Avi Kivity
            fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
68 b827df58 Avi Kivity
                    strerror(-r));
69 b827df58 Avi Kivity
            exit(1);
70 b827df58 Avi Kivity
        }
71 b827df58 Avi Kivity
    }
72 b827df58 Avi Kivity
    return cpuid;
73 b827df58 Avi Kivity
}
74 b827df58 Avi Kivity
75 c958a8bd Sheng Yang
uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
76 c958a8bd Sheng Yang
                                      uint32_t index, int reg)
77 b827df58 Avi Kivity
{
78 b827df58 Avi Kivity
    struct kvm_cpuid2 *cpuid;
79 b827df58 Avi Kivity
    int i, max;
80 b827df58 Avi Kivity
    uint32_t ret = 0;
81 b827df58 Avi Kivity
    uint32_t cpuid_1_edx;
82 b827df58 Avi Kivity
83 b827df58 Avi Kivity
    if (!kvm_check_extension(env->kvm_state, KVM_CAP_EXT_CPUID)) {
84 b827df58 Avi Kivity
        return -1U;
85 b827df58 Avi Kivity
    }
86 b827df58 Avi Kivity
87 b827df58 Avi Kivity
    max = 1;
88 b827df58 Avi Kivity
    while ((cpuid = try_get_cpuid(env->kvm_state, max)) == NULL) {
89 b827df58 Avi Kivity
        max *= 2;
90 b827df58 Avi Kivity
    }
91 b827df58 Avi Kivity
92 b827df58 Avi Kivity
    for (i = 0; i < cpuid->nent; ++i) {
93 c958a8bd Sheng Yang
        if (cpuid->entries[i].function == function &&
94 c958a8bd Sheng Yang
            cpuid->entries[i].index == index) {
95 b827df58 Avi Kivity
            switch (reg) {
96 b827df58 Avi Kivity
            case R_EAX:
97 b827df58 Avi Kivity
                ret = cpuid->entries[i].eax;
98 b827df58 Avi Kivity
                break;
99 b827df58 Avi Kivity
            case R_EBX:
100 b827df58 Avi Kivity
                ret = cpuid->entries[i].ebx;
101 b827df58 Avi Kivity
                break;
102 b827df58 Avi Kivity
            case R_ECX:
103 b827df58 Avi Kivity
                ret = cpuid->entries[i].ecx;
104 b827df58 Avi Kivity
                break;
105 b827df58 Avi Kivity
            case R_EDX:
106 b827df58 Avi Kivity
                ret = cpuid->entries[i].edx;
107 19ccb8ea Jan Kiszka
                switch (function) {
108 19ccb8ea Jan Kiszka
                case 1:
109 19ccb8ea Jan Kiszka
                    /* KVM before 2.6.30 misreports the following features */
110 19ccb8ea Jan Kiszka
                    ret |= CPUID_MTRR | CPUID_PAT | CPUID_MCE | CPUID_MCA;
111 19ccb8ea Jan Kiszka
                    break;
112 19ccb8ea Jan Kiszka
                case 0x80000001:
113 b827df58 Avi Kivity
                    /* On Intel, kvm returns cpuid according to the Intel spec,
114 b827df58 Avi Kivity
                     * so add missing bits according to the AMD spec:
115 b827df58 Avi Kivity
                     */
116 c958a8bd Sheng Yang
                    cpuid_1_edx = kvm_arch_get_supported_cpuid(env, 1, 0, R_EDX);
117 c1667e40 Gleb Natapov
                    ret |= cpuid_1_edx & 0x183f7ff;
118 19ccb8ea Jan Kiszka
                    break;
119 b827df58 Avi Kivity
                }
120 b827df58 Avi Kivity
                break;
121 b827df58 Avi Kivity
            }
122 b827df58 Avi Kivity
        }
123 b827df58 Avi Kivity
    }
124 b827df58 Avi Kivity
125 b827df58 Avi Kivity
    qemu_free(cpuid);
126 b827df58 Avi Kivity
127 b827df58 Avi Kivity
    return ret;
128 b827df58 Avi Kivity
}
129 b827df58 Avi Kivity
130 b827df58 Avi Kivity
#else
131 b827df58 Avi Kivity
132 c958a8bd Sheng Yang
uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
133 c958a8bd Sheng Yang
                                      uint32_t index, int reg)
134 b827df58 Avi Kivity
{
135 b827df58 Avi Kivity
    return -1U;
136 b827df58 Avi Kivity
}
137 b827df58 Avi Kivity
138 b827df58 Avi Kivity
#endif
139 b827df58 Avi Kivity
140 bb0300dc Gleb Natapov
#ifdef CONFIG_KVM_PARA
141 bb0300dc Gleb Natapov
struct kvm_para_features {
142 bb0300dc Gleb Natapov
        int cap;
143 bb0300dc Gleb Natapov
        int feature;
144 bb0300dc Gleb Natapov
} para_features[] = {
145 bb0300dc Gleb Natapov
#ifdef KVM_CAP_CLOCKSOURCE
146 bb0300dc Gleb Natapov
        { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
147 bb0300dc Gleb Natapov
#endif
148 bb0300dc Gleb Natapov
#ifdef KVM_CAP_NOP_IO_DELAY
149 bb0300dc Gleb Natapov
        { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
150 bb0300dc Gleb Natapov
#endif
151 bb0300dc Gleb Natapov
#ifdef KVM_CAP_PV_MMU
152 bb0300dc Gleb Natapov
        { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
153 bb0300dc Gleb Natapov
#endif
154 bb0300dc Gleb Natapov
        { -1, -1 }
155 bb0300dc Gleb Natapov
};
156 bb0300dc Gleb Natapov
157 bb0300dc Gleb Natapov
static int get_para_features(CPUState *env)
158 bb0300dc Gleb Natapov
{
159 bb0300dc Gleb Natapov
        int i, features = 0;
160 bb0300dc Gleb Natapov
161 bb0300dc Gleb Natapov
        for (i = 0; i < ARRAY_SIZE(para_features) - 1; i++) {
162 bb0300dc Gleb Natapov
                if (kvm_check_extension(env->kvm_state, para_features[i].cap))
163 bb0300dc Gleb Natapov
                        features |= (1 << para_features[i].feature);
164 bb0300dc Gleb Natapov
        }
165 bb0300dc Gleb Natapov
166 bb0300dc Gleb Natapov
        return features;
167 bb0300dc Gleb Natapov
}
168 bb0300dc Gleb Natapov
#endif
169 bb0300dc Gleb Natapov
170 05330448 aliguori
int kvm_arch_init_vcpu(CPUState *env)
171 05330448 aliguori
{
172 05330448 aliguori
    struct {
173 486bd5a2 aliguori
        struct kvm_cpuid2 cpuid;
174 486bd5a2 aliguori
        struct kvm_cpuid_entry2 entries[100];
175 05330448 aliguori
    } __attribute__((packed)) cpuid_data;
176 486bd5a2 aliguori
    uint32_t limit, i, j, cpuid_i;
177 a33609ca aliguori
    uint32_t unused;
178 bb0300dc Gleb Natapov
    struct kvm_cpuid_entry2 *c;
179 bb0300dc Gleb Natapov
#ifdef KVM_CPUID_SIGNATURE
180 bb0300dc Gleb Natapov
    uint32_t signature[3];
181 bb0300dc Gleb Natapov
#endif
182 05330448 aliguori
183 f8d926e9 Jan Kiszka
    env->mp_state = KVM_MP_STATE_RUNNABLE;
184 f8d926e9 Jan Kiszka
185 c958a8bd Sheng Yang
    env->cpuid_features &= kvm_arch_get_supported_cpuid(env, 1, 0, R_EDX);
186 6c0d7ee8 Andre Przywara
187 6c0d7ee8 Andre Przywara
    i = env->cpuid_ext_features & CPUID_EXT_HYPERVISOR;
188 c958a8bd Sheng Yang
    env->cpuid_ext_features &= kvm_arch_get_supported_cpuid(env, 1, 0, R_ECX);
189 6c0d7ee8 Andre Przywara
    env->cpuid_ext_features |= i;
190 6c0d7ee8 Andre Przywara
191 457dfed6 Andre Przywara
    env->cpuid_ext2_features &= kvm_arch_get_supported_cpuid(env, 0x80000001,
192 c958a8bd Sheng Yang
                                                             0, R_EDX);
193 457dfed6 Andre Przywara
    env->cpuid_ext3_features &= kvm_arch_get_supported_cpuid(env, 0x80000001,
194 c958a8bd Sheng Yang
                                                             0, R_ECX);
195 6c1f42fe Andre Przywara
196 05330448 aliguori
    cpuid_i = 0;
197 05330448 aliguori
198 bb0300dc Gleb Natapov
#ifdef CONFIG_KVM_PARA
199 bb0300dc Gleb Natapov
    /* Paravirtualization CPUIDs */
200 bb0300dc Gleb Natapov
    memcpy(signature, "KVMKVMKVM\0\0\0", 12);
201 bb0300dc Gleb Natapov
    c = &cpuid_data.entries[cpuid_i++];
202 bb0300dc Gleb Natapov
    memset(c, 0, sizeof(*c));
203 bb0300dc Gleb Natapov
    c->function = KVM_CPUID_SIGNATURE;
204 bb0300dc Gleb Natapov
    c->eax = 0;
205 bb0300dc Gleb Natapov
    c->ebx = signature[0];
206 bb0300dc Gleb Natapov
    c->ecx = signature[1];
207 bb0300dc Gleb Natapov
    c->edx = signature[2];
208 bb0300dc Gleb Natapov
209 bb0300dc Gleb Natapov
    c = &cpuid_data.entries[cpuid_i++];
210 bb0300dc Gleb Natapov
    memset(c, 0, sizeof(*c));
211 bb0300dc Gleb Natapov
    c->function = KVM_CPUID_FEATURES;
212 bb0300dc Gleb Natapov
    c->eax = env->cpuid_kvm_features & get_para_features(env);
213 bb0300dc Gleb Natapov
#endif
214 bb0300dc Gleb Natapov
215 a33609ca aliguori
    cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
216 05330448 aliguori
217 05330448 aliguori
    for (i = 0; i <= limit; i++) {
218 bb0300dc Gleb Natapov
        c = &cpuid_data.entries[cpuid_i++];
219 486bd5a2 aliguori
220 486bd5a2 aliguori
        switch (i) {
221 a36b1029 aliguori
        case 2: {
222 a36b1029 aliguori
            /* Keep reading function 2 till all the input is received */
223 a36b1029 aliguori
            int times;
224 a36b1029 aliguori
225 a36b1029 aliguori
            c->function = i;
226 a33609ca aliguori
            c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC |
227 a33609ca aliguori
                       KVM_CPUID_FLAG_STATE_READ_NEXT;
228 a33609ca aliguori
            cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
229 a33609ca aliguori
            times = c->eax & 0xff;
230 a36b1029 aliguori
231 a36b1029 aliguori
            for (j = 1; j < times; ++j) {
232 a33609ca aliguori
                c = &cpuid_data.entries[cpuid_i++];
233 a36b1029 aliguori
                c->function = i;
234 a33609ca aliguori
                c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC;
235 a33609ca aliguori
                cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
236 a36b1029 aliguori
            }
237 a36b1029 aliguori
            break;
238 a36b1029 aliguori
        }
239 486bd5a2 aliguori
        case 4:
240 486bd5a2 aliguori
        case 0xb:
241 486bd5a2 aliguori
        case 0xd:
242 486bd5a2 aliguori
            for (j = 0; ; j++) {
243 486bd5a2 aliguori
                c->function = i;
244 486bd5a2 aliguori
                c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
245 486bd5a2 aliguori
                c->index = j;
246 a33609ca aliguori
                cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx);
247 486bd5a2 aliguori
248 a33609ca aliguori
                if (i == 4 && c->eax == 0)
249 486bd5a2 aliguori
                    break;
250 a33609ca aliguori
                if (i == 0xb && !(c->ecx & 0xff00))
251 486bd5a2 aliguori
                    break;
252 a33609ca aliguori
                if (i == 0xd && c->eax == 0)
253 486bd5a2 aliguori
                    break;
254 a33609ca aliguori
255 a33609ca aliguori
                c = &cpuid_data.entries[cpuid_i++];
256 486bd5a2 aliguori
            }
257 486bd5a2 aliguori
            break;
258 486bd5a2 aliguori
        default:
259 486bd5a2 aliguori
            c->function = i;
260 a33609ca aliguori
            c->flags = 0;
261 a33609ca aliguori
            cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
262 486bd5a2 aliguori
            break;
263 486bd5a2 aliguori
        }
264 05330448 aliguori
    }
265 a33609ca aliguori
    cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused);
266 05330448 aliguori
267 05330448 aliguori
    for (i = 0x80000000; i <= limit; i++) {
268 bb0300dc Gleb Natapov
        c = &cpuid_data.entries[cpuid_i++];
269 05330448 aliguori
270 05330448 aliguori
        c->function = i;
271 a33609ca aliguori
        c->flags = 0;
272 a33609ca aliguori
        cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
273 05330448 aliguori
    }
274 05330448 aliguori
275 05330448 aliguori
    cpuid_data.cpuid.nent = cpuid_i;
276 05330448 aliguori
277 486bd5a2 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
278 05330448 aliguori
}
279 05330448 aliguori
280 caa5af0f Jan Kiszka
void kvm_arch_reset_vcpu(CPUState *env)
281 caa5af0f Jan Kiszka
{
282 e73223a5 Gleb Natapov
    env->exception_injected = -1;
283 0e607a80 Jan Kiszka
    env->interrupt_injected = -1;
284 a0fb002c Jan Kiszka
    env->nmi_injected = 0;
285 a0fb002c Jan Kiszka
    env->nmi_pending = 0;
286 ddced198 Marcelo Tosatti
    if (kvm_irqchip_in_kernel()) {
287 ddced198 Marcelo Tosatti
        env->mp_state = cpu_is_bsp(env) ? KVM_MP_STATE_RUNNABLE :
288 ddced198 Marcelo Tosatti
                                          KVM_MP_STATE_UNINITIALIZED;
289 ddced198 Marcelo Tosatti
    } else {
290 ddced198 Marcelo Tosatti
        env->mp_state = KVM_MP_STATE_RUNNABLE;
291 ddced198 Marcelo Tosatti
    }
292 caa5af0f Jan Kiszka
}
293 caa5af0f Jan Kiszka
294 05330448 aliguori
static int kvm_has_msr_star(CPUState *env)
295 05330448 aliguori
{
296 05330448 aliguori
    static int has_msr_star;
297 05330448 aliguori
    int ret;
298 05330448 aliguori
299 05330448 aliguori
    /* first time */
300 05330448 aliguori
    if (has_msr_star == 0) {        
301 05330448 aliguori
        struct kvm_msr_list msr_list, *kvm_msr_list;
302 05330448 aliguori
303 05330448 aliguori
        has_msr_star = -1;
304 05330448 aliguori
305 05330448 aliguori
        /* Obtain MSR list from KVM.  These are the MSRs that we must
306 05330448 aliguori
         * save/restore */
307 4c9f7372 aliguori
        msr_list.nmsrs = 0;
308 05330448 aliguori
        ret = kvm_ioctl(env->kvm_state, KVM_GET_MSR_INDEX_LIST, &msr_list);
309 6fb6d245 Jan Kiszka
        if (ret < 0 && ret != -E2BIG) {
310 05330448 aliguori
            return 0;
311 6fb6d245 Jan Kiszka
        }
312 d9db889f Jan Kiszka
        /* Old kernel modules had a bug and could write beyond the provided
313 d9db889f Jan Kiszka
           memory. Allocate at least a safe amount of 1K. */
314 d9db889f Jan Kiszka
        kvm_msr_list = qemu_mallocz(MAX(1024, sizeof(msr_list) +
315 d9db889f Jan Kiszka
                                              msr_list.nmsrs *
316 d9db889f Jan Kiszka
                                              sizeof(msr_list.indices[0])));
317 05330448 aliguori
318 55308450 aliguori
        kvm_msr_list->nmsrs = msr_list.nmsrs;
319 05330448 aliguori
        ret = kvm_ioctl(env->kvm_state, KVM_GET_MSR_INDEX_LIST, kvm_msr_list);
320 05330448 aliguori
        if (ret >= 0) {
321 05330448 aliguori
            int i;
322 05330448 aliguori
323 05330448 aliguori
            for (i = 0; i < kvm_msr_list->nmsrs; i++) {
324 05330448 aliguori
                if (kvm_msr_list->indices[i] == MSR_STAR) {
325 05330448 aliguori
                    has_msr_star = 1;
326 05330448 aliguori
                    break;
327 05330448 aliguori
                }
328 05330448 aliguori
            }
329 05330448 aliguori
        }
330 05330448 aliguori
331 05330448 aliguori
        free(kvm_msr_list);
332 05330448 aliguori
    }
333 05330448 aliguori
334 05330448 aliguori
    if (has_msr_star == 1)
335 05330448 aliguori
        return 1;
336 05330448 aliguori
    return 0;
337 05330448 aliguori
}
338 05330448 aliguori
339 20420430 Sheng Yang
static int kvm_init_identity_map_page(KVMState *s)
340 20420430 Sheng Yang
{
341 20420430 Sheng Yang
#ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
342 20420430 Sheng Yang
    int ret;
343 20420430 Sheng Yang
    uint64_t addr = 0xfffbc000;
344 20420430 Sheng Yang
345 20420430 Sheng Yang
    if (!kvm_check_extension(s, KVM_CAP_SET_IDENTITY_MAP_ADDR)) {
346 20420430 Sheng Yang
        return 0;
347 20420430 Sheng Yang
    }
348 20420430 Sheng Yang
349 20420430 Sheng Yang
    ret = kvm_vm_ioctl(s, KVM_SET_IDENTITY_MAP_ADDR, &addr);
350 20420430 Sheng Yang
    if (ret < 0) {
351 20420430 Sheng Yang
        fprintf(stderr, "kvm_set_identity_map_addr: %s\n", strerror(ret));
352 20420430 Sheng Yang
        return ret;
353 20420430 Sheng Yang
    }
354 20420430 Sheng Yang
#endif
355 20420430 Sheng Yang
    return 0;
356 20420430 Sheng Yang
}
357 20420430 Sheng Yang
358 05330448 aliguori
int kvm_arch_init(KVMState *s, int smp_cpus)
359 05330448 aliguori
{
360 05330448 aliguori
    int ret;
361 05330448 aliguori
362 05330448 aliguori
    /* create vm86 tss.  KVM uses vm86 mode to emulate 16-bit code
363 05330448 aliguori
     * directly.  In order to use vm86 mode, a TSS is needed.  Since this
364 05330448 aliguori
     * must be part of guest physical memory, we need to allocate it.  Older
365 05330448 aliguori
     * versions of KVM just assumed that it would be at the end of physical
366 05330448 aliguori
     * memory but that doesn't work with more than 4GB of memory.  We simply
367 05330448 aliguori
     * refuse to work with those older versions of KVM. */
368 984b5181 aliguori
    ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
369 05330448 aliguori
    if (ret <= 0) {
370 05330448 aliguori
        fprintf(stderr, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
371 05330448 aliguori
        return ret;
372 05330448 aliguori
    }
373 05330448 aliguori
374 05330448 aliguori
    /* this address is 3 pages before the bios, and the bios should present
375 05330448 aliguori
     * as unavaible memory.  FIXME, need to ensure the e820 map deals with
376 05330448 aliguori
     * this?
377 05330448 aliguori
     */
378 4c5b10b7 Jes Sorensen
    /*
379 4c5b10b7 Jes Sorensen
     * Tell fw_cfg to notify the BIOS to reserve the range.
380 4c5b10b7 Jes Sorensen
     */
381 4c5b10b7 Jes Sorensen
    if (e820_add_entry(0xfffbc000, 0x4000, E820_RESERVED) < 0) {
382 4c5b10b7 Jes Sorensen
        perror("e820_add_entry() table is full");
383 4c5b10b7 Jes Sorensen
        exit(1);
384 4c5b10b7 Jes Sorensen
    }
385 20420430 Sheng Yang
    ret = kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, 0xfffbd000);
386 20420430 Sheng Yang
    if (ret < 0) {
387 20420430 Sheng Yang
        return ret;
388 20420430 Sheng Yang
    }
389 20420430 Sheng Yang
390 20420430 Sheng Yang
    return kvm_init_identity_map_page(s);
391 05330448 aliguori
}
392 05330448 aliguori
                    
393 05330448 aliguori
static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
394 05330448 aliguori
{
395 05330448 aliguori
    lhs->selector = rhs->selector;
396 05330448 aliguori
    lhs->base = rhs->base;
397 05330448 aliguori
    lhs->limit = rhs->limit;
398 05330448 aliguori
    lhs->type = 3;
399 05330448 aliguori
    lhs->present = 1;
400 05330448 aliguori
    lhs->dpl = 3;
401 05330448 aliguori
    lhs->db = 0;
402 05330448 aliguori
    lhs->s = 1;
403 05330448 aliguori
    lhs->l = 0;
404 05330448 aliguori
    lhs->g = 0;
405 05330448 aliguori
    lhs->avl = 0;
406 05330448 aliguori
    lhs->unusable = 0;
407 05330448 aliguori
}
408 05330448 aliguori
409 05330448 aliguori
static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
410 05330448 aliguori
{
411 05330448 aliguori
    unsigned flags = rhs->flags;
412 05330448 aliguori
    lhs->selector = rhs->selector;
413 05330448 aliguori
    lhs->base = rhs->base;
414 05330448 aliguori
    lhs->limit = rhs->limit;
415 05330448 aliguori
    lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
416 05330448 aliguori
    lhs->present = (flags & DESC_P_MASK) != 0;
417 05330448 aliguori
    lhs->dpl = rhs->selector & 3;
418 05330448 aliguori
    lhs->db = (flags >> DESC_B_SHIFT) & 1;
419 05330448 aliguori
    lhs->s = (flags & DESC_S_MASK) != 0;
420 05330448 aliguori
    lhs->l = (flags >> DESC_L_SHIFT) & 1;
421 05330448 aliguori
    lhs->g = (flags & DESC_G_MASK) != 0;
422 05330448 aliguori
    lhs->avl = (flags & DESC_AVL_MASK) != 0;
423 05330448 aliguori
    lhs->unusable = 0;
424 05330448 aliguori
}
425 05330448 aliguori
426 05330448 aliguori
static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
427 05330448 aliguori
{
428 05330448 aliguori
    lhs->selector = rhs->selector;
429 05330448 aliguori
    lhs->base = rhs->base;
430 05330448 aliguori
    lhs->limit = rhs->limit;
431 05330448 aliguori
    lhs->flags =
432 05330448 aliguori
        (rhs->type << DESC_TYPE_SHIFT)
433 05330448 aliguori
        | (rhs->present * DESC_P_MASK)
434 05330448 aliguori
        | (rhs->dpl << DESC_DPL_SHIFT)
435 05330448 aliguori
        | (rhs->db << DESC_B_SHIFT)
436 05330448 aliguori
        | (rhs->s * DESC_S_MASK)
437 05330448 aliguori
        | (rhs->l << DESC_L_SHIFT)
438 05330448 aliguori
        | (rhs->g * DESC_G_MASK)
439 05330448 aliguori
        | (rhs->avl * DESC_AVL_MASK);
440 05330448 aliguori
}
441 05330448 aliguori
442 05330448 aliguori
static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
443 05330448 aliguori
{
444 05330448 aliguori
    if (set)
445 05330448 aliguori
        *kvm_reg = *qemu_reg;
446 05330448 aliguori
    else
447 05330448 aliguori
        *qemu_reg = *kvm_reg;
448 05330448 aliguori
}
449 05330448 aliguori
450 05330448 aliguori
static int kvm_getput_regs(CPUState *env, int set)
451 05330448 aliguori
{
452 05330448 aliguori
    struct kvm_regs regs;
453 05330448 aliguori
    int ret = 0;
454 05330448 aliguori
455 05330448 aliguori
    if (!set) {
456 05330448 aliguori
        ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, &regs);
457 05330448 aliguori
        if (ret < 0)
458 05330448 aliguori
            return ret;
459 05330448 aliguori
    }
460 05330448 aliguori
461 05330448 aliguori
    kvm_getput_reg(&regs.rax, &env->regs[R_EAX], set);
462 05330448 aliguori
    kvm_getput_reg(&regs.rbx, &env->regs[R_EBX], set);
463 05330448 aliguori
    kvm_getput_reg(&regs.rcx, &env->regs[R_ECX], set);
464 05330448 aliguori
    kvm_getput_reg(&regs.rdx, &env->regs[R_EDX], set);
465 05330448 aliguori
    kvm_getput_reg(&regs.rsi, &env->regs[R_ESI], set);
466 05330448 aliguori
    kvm_getput_reg(&regs.rdi, &env->regs[R_EDI], set);
467 05330448 aliguori
    kvm_getput_reg(&regs.rsp, &env->regs[R_ESP], set);
468 05330448 aliguori
    kvm_getput_reg(&regs.rbp, &env->regs[R_EBP], set);
469 05330448 aliguori
#ifdef TARGET_X86_64
470 05330448 aliguori
    kvm_getput_reg(&regs.r8, &env->regs[8], set);
471 05330448 aliguori
    kvm_getput_reg(&regs.r9, &env->regs[9], set);
472 05330448 aliguori
    kvm_getput_reg(&regs.r10, &env->regs[10], set);
473 05330448 aliguori
    kvm_getput_reg(&regs.r11, &env->regs[11], set);
474 05330448 aliguori
    kvm_getput_reg(&regs.r12, &env->regs[12], set);
475 05330448 aliguori
    kvm_getput_reg(&regs.r13, &env->regs[13], set);
476 05330448 aliguori
    kvm_getput_reg(&regs.r14, &env->regs[14], set);
477 05330448 aliguori
    kvm_getput_reg(&regs.r15, &env->regs[15], set);
478 05330448 aliguori
#endif
479 05330448 aliguori
480 05330448 aliguori
    kvm_getput_reg(&regs.rflags, &env->eflags, set);
481 05330448 aliguori
    kvm_getput_reg(&regs.rip, &env->eip, set);
482 05330448 aliguori
483 05330448 aliguori
    if (set)
484 05330448 aliguori
        ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, &regs);
485 05330448 aliguori
486 05330448 aliguori
    return ret;
487 05330448 aliguori
}
488 05330448 aliguori
489 05330448 aliguori
static int kvm_put_fpu(CPUState *env)
490 05330448 aliguori
{
491 05330448 aliguori
    struct kvm_fpu fpu;
492 05330448 aliguori
    int i;
493 05330448 aliguori
494 05330448 aliguori
    memset(&fpu, 0, sizeof fpu);
495 05330448 aliguori
    fpu.fsw = env->fpus & ~(7 << 11);
496 05330448 aliguori
    fpu.fsw |= (env->fpstt & 7) << 11;
497 05330448 aliguori
    fpu.fcw = env->fpuc;
498 05330448 aliguori
    for (i = 0; i < 8; ++i)
499 05330448 aliguori
        fpu.ftwx |= (!env->fptags[i]) << i;
500 05330448 aliguori
    memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
501 05330448 aliguori
    memcpy(fpu.xmm, env->xmm_regs, sizeof env->xmm_regs);
502 05330448 aliguori
    fpu.mxcsr = env->mxcsr;
503 05330448 aliguori
504 05330448 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_FPU, &fpu);
505 05330448 aliguori
}
506 05330448 aliguori
507 f1665b21 Sheng Yang
#ifdef KVM_CAP_XSAVE
508 f1665b21 Sheng Yang
#define XSAVE_CWD_RIP     2
509 f1665b21 Sheng Yang
#define XSAVE_CWD_RDP     4
510 f1665b21 Sheng Yang
#define XSAVE_MXCSR       6
511 f1665b21 Sheng Yang
#define XSAVE_ST_SPACE    8
512 f1665b21 Sheng Yang
#define XSAVE_XMM_SPACE   40
513 f1665b21 Sheng Yang
#define XSAVE_XSTATE_BV   128
514 f1665b21 Sheng Yang
#define XSAVE_YMMH_SPACE  144
515 f1665b21 Sheng Yang
#endif
516 f1665b21 Sheng Yang
517 f1665b21 Sheng Yang
static int kvm_put_xsave(CPUState *env)
518 f1665b21 Sheng Yang
{
519 f1665b21 Sheng Yang
#ifdef KVM_CAP_XSAVE
520 f1665b21 Sheng Yang
    int i;
521 f1665b21 Sheng Yang
    struct kvm_xsave* xsave;
522 f1665b21 Sheng Yang
    uint16_t cwd, swd, twd, fop;
523 f1665b21 Sheng Yang
524 f1665b21 Sheng Yang
    if (!kvm_has_xsave())
525 f1665b21 Sheng Yang
        return kvm_put_fpu(env);
526 f1665b21 Sheng Yang
527 f1665b21 Sheng Yang
    xsave = qemu_memalign(4096, sizeof(struct kvm_xsave));
528 f1665b21 Sheng Yang
    memset(xsave, 0, sizeof(struct kvm_xsave));
529 f1665b21 Sheng Yang
    cwd = swd = twd = fop = 0;
530 f1665b21 Sheng Yang
    swd = env->fpus & ~(7 << 11);
531 f1665b21 Sheng Yang
    swd |= (env->fpstt & 7) << 11;
532 f1665b21 Sheng Yang
    cwd = env->fpuc;
533 f1665b21 Sheng Yang
    for (i = 0; i < 8; ++i)
534 f1665b21 Sheng Yang
        twd |= (!env->fptags[i]) << i;
535 f1665b21 Sheng Yang
    xsave->region[0] = (uint32_t)(swd << 16) + cwd;
536 f1665b21 Sheng Yang
    xsave->region[1] = (uint32_t)(fop << 16) + twd;
537 f1665b21 Sheng Yang
    memcpy(&xsave->region[XSAVE_ST_SPACE], env->fpregs,
538 f1665b21 Sheng Yang
            sizeof env->fpregs);
539 f1665b21 Sheng Yang
    memcpy(&xsave->region[XSAVE_XMM_SPACE], env->xmm_regs,
540 f1665b21 Sheng Yang
            sizeof env->xmm_regs);
541 f1665b21 Sheng Yang
    xsave->region[XSAVE_MXCSR] = env->mxcsr;
542 f1665b21 Sheng Yang
    *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv;
543 f1665b21 Sheng Yang
    memcpy(&xsave->region[XSAVE_YMMH_SPACE], env->ymmh_regs,
544 f1665b21 Sheng Yang
            sizeof env->ymmh_regs);
545 f1665b21 Sheng Yang
    return kvm_vcpu_ioctl(env, KVM_SET_XSAVE, xsave);
546 f1665b21 Sheng Yang
#else
547 f1665b21 Sheng Yang
    return kvm_put_fpu(env);
548 f1665b21 Sheng Yang
#endif
549 f1665b21 Sheng Yang
}
550 f1665b21 Sheng Yang
551 f1665b21 Sheng Yang
static int kvm_put_xcrs(CPUState *env)
552 f1665b21 Sheng Yang
{
553 f1665b21 Sheng Yang
#ifdef KVM_CAP_XCRS
554 f1665b21 Sheng Yang
    struct kvm_xcrs xcrs;
555 f1665b21 Sheng Yang
556 f1665b21 Sheng Yang
    if (!kvm_has_xcrs())
557 f1665b21 Sheng Yang
        return 0;
558 f1665b21 Sheng Yang
559 f1665b21 Sheng Yang
    xcrs.nr_xcrs = 1;
560 f1665b21 Sheng Yang
    xcrs.flags = 0;
561 f1665b21 Sheng Yang
    xcrs.xcrs[0].xcr = 0;
562 f1665b21 Sheng Yang
    xcrs.xcrs[0].value = env->xcr0;
563 f1665b21 Sheng Yang
    return kvm_vcpu_ioctl(env, KVM_SET_XCRS, &xcrs);
564 f1665b21 Sheng Yang
#else
565 f1665b21 Sheng Yang
    return 0;
566 f1665b21 Sheng Yang
#endif
567 f1665b21 Sheng Yang
}
568 f1665b21 Sheng Yang
569 05330448 aliguori
static int kvm_put_sregs(CPUState *env)
570 05330448 aliguori
{
571 05330448 aliguori
    struct kvm_sregs sregs;
572 05330448 aliguori
573 0e607a80 Jan Kiszka
    memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));
574 0e607a80 Jan Kiszka
    if (env->interrupt_injected >= 0) {
575 0e607a80 Jan Kiszka
        sregs.interrupt_bitmap[env->interrupt_injected / 64] |=
576 0e607a80 Jan Kiszka
                (uint64_t)1 << (env->interrupt_injected % 64);
577 0e607a80 Jan Kiszka
    }
578 05330448 aliguori
579 05330448 aliguori
    if ((env->eflags & VM_MASK)) {
580 05330448 aliguori
            set_v8086_seg(&sregs.cs, &env->segs[R_CS]);
581 05330448 aliguori
            set_v8086_seg(&sregs.ds, &env->segs[R_DS]);
582 05330448 aliguori
            set_v8086_seg(&sregs.es, &env->segs[R_ES]);
583 05330448 aliguori
            set_v8086_seg(&sregs.fs, &env->segs[R_FS]);
584 05330448 aliguori
            set_v8086_seg(&sregs.gs, &env->segs[R_GS]);
585 05330448 aliguori
            set_v8086_seg(&sregs.ss, &env->segs[R_SS]);
586 05330448 aliguori
    } else {
587 05330448 aliguori
            set_seg(&sregs.cs, &env->segs[R_CS]);
588 05330448 aliguori
            set_seg(&sregs.ds, &env->segs[R_DS]);
589 05330448 aliguori
            set_seg(&sregs.es, &env->segs[R_ES]);
590 05330448 aliguori
            set_seg(&sregs.fs, &env->segs[R_FS]);
591 05330448 aliguori
            set_seg(&sregs.gs, &env->segs[R_GS]);
592 05330448 aliguori
            set_seg(&sregs.ss, &env->segs[R_SS]);
593 05330448 aliguori
594 05330448 aliguori
            if (env->cr[0] & CR0_PE_MASK) {
595 05330448 aliguori
                /* force ss cpl to cs cpl */
596 05330448 aliguori
                sregs.ss.selector = (sregs.ss.selector & ~3) |
597 05330448 aliguori
                        (sregs.cs.selector & 3);
598 05330448 aliguori
                sregs.ss.dpl = sregs.ss.selector & 3;
599 05330448 aliguori
            }
600 05330448 aliguori
    }
601 05330448 aliguori
602 05330448 aliguori
    set_seg(&sregs.tr, &env->tr);
603 05330448 aliguori
    set_seg(&sregs.ldt, &env->ldt);
604 05330448 aliguori
605 05330448 aliguori
    sregs.idt.limit = env->idt.limit;
606 05330448 aliguori
    sregs.idt.base = env->idt.base;
607 05330448 aliguori
    sregs.gdt.limit = env->gdt.limit;
608 05330448 aliguori
    sregs.gdt.base = env->gdt.base;
609 05330448 aliguori
610 05330448 aliguori
    sregs.cr0 = env->cr[0];
611 05330448 aliguori
    sregs.cr2 = env->cr[2];
612 05330448 aliguori
    sregs.cr3 = env->cr[3];
613 05330448 aliguori
    sregs.cr4 = env->cr[4];
614 05330448 aliguori
615 4a942cea Blue Swirl
    sregs.cr8 = cpu_get_apic_tpr(env->apic_state);
616 4a942cea Blue Swirl
    sregs.apic_base = cpu_get_apic_base(env->apic_state);
617 05330448 aliguori
618 05330448 aliguori
    sregs.efer = env->efer;
619 05330448 aliguori
620 05330448 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_SREGS, &sregs);
621 05330448 aliguori
}
622 05330448 aliguori
623 05330448 aliguori
static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
624 05330448 aliguori
                              uint32_t index, uint64_t value)
625 05330448 aliguori
{
626 05330448 aliguori
    entry->index = index;
627 05330448 aliguori
    entry->data = value;
628 05330448 aliguori
}
629 05330448 aliguori
630 ea643051 Jan Kiszka
static int kvm_put_msrs(CPUState *env, int level)
631 05330448 aliguori
{
632 05330448 aliguori
    struct {
633 05330448 aliguori
        struct kvm_msrs info;
634 05330448 aliguori
        struct kvm_msr_entry entries[100];
635 05330448 aliguori
    } msr_data;
636 05330448 aliguori
    struct kvm_msr_entry *msrs = msr_data.entries;
637 05330448 aliguori
    int n = 0;
638 05330448 aliguori
639 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
640 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
641 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
642 05330448 aliguori
    if (kvm_has_msr_star(env))
643 05330448 aliguori
        kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
644 05330448 aliguori
#ifdef TARGET_X86_64
645 05330448 aliguori
    /* FIXME if lm capable */
646 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
647 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
648 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
649 05330448 aliguori
    kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
650 05330448 aliguori
#endif
651 ea643051 Jan Kiszka
    if (level == KVM_PUT_FULL_STATE) {
652 ea643051 Jan Kiszka
        kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
653 ea643051 Jan Kiszka
        kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME,
654 ea643051 Jan Kiszka
                          env->system_time_msr);
655 ea643051 Jan Kiszka
        kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
656 ea643051 Jan Kiszka
    }
657 1a03675d Glauber Costa
658 05330448 aliguori
    msr_data.info.nmsrs = n;
659 05330448 aliguori
660 05330448 aliguori
    return kvm_vcpu_ioctl(env, KVM_SET_MSRS, &msr_data);
661 05330448 aliguori
662 05330448 aliguori
}
663 05330448 aliguori
664 05330448 aliguori
665 05330448 aliguori
static int kvm_get_fpu(CPUState *env)
666 05330448 aliguori
{
667 05330448 aliguori
    struct kvm_fpu fpu;
668 05330448 aliguori
    int i, ret;
669 05330448 aliguori
670 05330448 aliguori
    ret = kvm_vcpu_ioctl(env, KVM_GET_FPU, &fpu);
671 05330448 aliguori
    if (ret < 0)
672 05330448 aliguori
        return ret;
673 05330448 aliguori
674 05330448 aliguori
    env->fpstt = (fpu.fsw >> 11) & 7;
675 05330448 aliguori
    env->fpus = fpu.fsw;
676 05330448 aliguori
    env->fpuc = fpu.fcw;
677 05330448 aliguori
    for (i = 0; i < 8; ++i)
678 05330448 aliguori
        env->fptags[i] = !((fpu.ftwx >> i) & 1);
679 05330448 aliguori
    memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
680 05330448 aliguori
    memcpy(env->xmm_regs, fpu.xmm, sizeof env->xmm_regs);
681 05330448 aliguori
    env->mxcsr = fpu.mxcsr;
682 05330448 aliguori
683 05330448 aliguori
    return 0;
684 05330448 aliguori
}
685 05330448 aliguori
686 f1665b21 Sheng Yang
static int kvm_get_xsave(CPUState *env)
687 f1665b21 Sheng Yang
{
688 f1665b21 Sheng Yang
#ifdef KVM_CAP_XSAVE
689 f1665b21 Sheng Yang
    struct kvm_xsave* xsave;
690 f1665b21 Sheng Yang
    int ret, i;
691 f1665b21 Sheng Yang
    uint16_t cwd, swd, twd, fop;
692 f1665b21 Sheng Yang
693 f1665b21 Sheng Yang
    if (!kvm_has_xsave())
694 f1665b21 Sheng Yang
        return kvm_get_fpu(env);
695 f1665b21 Sheng Yang
696 f1665b21 Sheng Yang
    xsave = qemu_memalign(4096, sizeof(struct kvm_xsave));
697 f1665b21 Sheng Yang
    ret = kvm_vcpu_ioctl(env, KVM_GET_XSAVE, xsave);
698 f1665b21 Sheng Yang
    if (ret < 0)
699 f1665b21 Sheng Yang
        return ret;
700 f1665b21 Sheng Yang
701 f1665b21 Sheng Yang
    cwd = (uint16_t)xsave->region[0];
702 f1665b21 Sheng Yang
    swd = (uint16_t)(xsave->region[0] >> 16);
703 f1665b21 Sheng Yang
    twd = (uint16_t)xsave->region[1];
704 f1665b21 Sheng Yang
    fop = (uint16_t)(xsave->region[1] >> 16);
705 f1665b21 Sheng Yang
    env->fpstt = (swd >> 11) & 7;
706 f1665b21 Sheng Yang
    env->fpus = swd;
707 f1665b21 Sheng Yang
    env->fpuc = cwd;
708 f1665b21 Sheng Yang
    for (i = 0; i < 8; ++i)
709 f1665b21 Sheng Yang
        env->fptags[i] = !((twd >> i) & 1);
710 f1665b21 Sheng Yang
    env->mxcsr = xsave->region[XSAVE_MXCSR];
711 f1665b21 Sheng Yang
    memcpy(env->fpregs, &xsave->region[XSAVE_ST_SPACE],
712 f1665b21 Sheng Yang
            sizeof env->fpregs);
713 f1665b21 Sheng Yang
    memcpy(env->xmm_regs, &xsave->region[XSAVE_XMM_SPACE],
714 f1665b21 Sheng Yang
            sizeof env->xmm_regs);
715 f1665b21 Sheng Yang
    env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV];
716 f1665b21 Sheng Yang
    memcpy(env->ymmh_regs, &xsave->region[XSAVE_YMMH_SPACE],
717 f1665b21 Sheng Yang
            sizeof env->ymmh_regs);
718 f1665b21 Sheng Yang
    return 0;
719 f1665b21 Sheng Yang
#else
720 f1665b21 Sheng Yang
    return kvm_get_fpu(env);
721 f1665b21 Sheng Yang
#endif
722 f1665b21 Sheng Yang
}
723 f1665b21 Sheng Yang
724 f1665b21 Sheng Yang
static int kvm_get_xcrs(CPUState *env)
725 f1665b21 Sheng Yang
{
726 f1665b21 Sheng Yang
#ifdef KVM_CAP_XCRS
727 f1665b21 Sheng Yang
    int i, ret;
728 f1665b21 Sheng Yang
    struct kvm_xcrs xcrs;
729 f1665b21 Sheng Yang
730 f1665b21 Sheng Yang
    if (!kvm_has_xcrs())
731 f1665b21 Sheng Yang
        return 0;
732 f1665b21 Sheng Yang
733 f1665b21 Sheng Yang
    ret = kvm_vcpu_ioctl(env, KVM_GET_XCRS, &xcrs);
734 f1665b21 Sheng Yang
    if (ret < 0)
735 f1665b21 Sheng Yang
        return ret;
736 f1665b21 Sheng Yang
737 f1665b21 Sheng Yang
    for (i = 0; i < xcrs.nr_xcrs; i++)
738 f1665b21 Sheng Yang
        /* Only support xcr0 now */
739 f1665b21 Sheng Yang
        if (xcrs.xcrs[0].xcr == 0) {
740 f1665b21 Sheng Yang
            env->xcr0 = xcrs.xcrs[0].value;
741 f1665b21 Sheng Yang
            break;
742 f1665b21 Sheng Yang
        }
743 f1665b21 Sheng Yang
    return 0;
744 f1665b21 Sheng Yang
#else
745 f1665b21 Sheng Yang
    return 0;
746 f1665b21 Sheng Yang
#endif
747 f1665b21 Sheng Yang
}
748 f1665b21 Sheng Yang
749 05330448 aliguori
static int kvm_get_sregs(CPUState *env)
750 05330448 aliguori
{
751 05330448 aliguori
    struct kvm_sregs sregs;
752 05330448 aliguori
    uint32_t hflags;
753 0e607a80 Jan Kiszka
    int bit, i, ret;
754 05330448 aliguori
755 05330448 aliguori
    ret = kvm_vcpu_ioctl(env, KVM_GET_SREGS, &sregs);
756 05330448 aliguori
    if (ret < 0)
757 05330448 aliguori
        return ret;
758 05330448 aliguori
759 0e607a80 Jan Kiszka
    /* There can only be one pending IRQ set in the bitmap at a time, so try
760 0e607a80 Jan Kiszka
       to find it and save its number instead (-1 for none). */
761 0e607a80 Jan Kiszka
    env->interrupt_injected = -1;
762 0e607a80 Jan Kiszka
    for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) {
763 0e607a80 Jan Kiszka
        if (sregs.interrupt_bitmap[i]) {
764 0e607a80 Jan Kiszka
            bit = ctz64(sregs.interrupt_bitmap[i]);
765 0e607a80 Jan Kiszka
            env->interrupt_injected = i * 64 + bit;
766 0e607a80 Jan Kiszka
            break;
767 0e607a80 Jan Kiszka
        }
768 0e607a80 Jan Kiszka
    }
769 05330448 aliguori
770 05330448 aliguori
    get_seg(&env->segs[R_CS], &sregs.cs);
771 05330448 aliguori
    get_seg(&env->segs[R_DS], &sregs.ds);
772 05330448 aliguori
    get_seg(&env->segs[R_ES], &sregs.es);
773 05330448 aliguori
    get_seg(&env->segs[R_FS], &sregs.fs);
774 05330448 aliguori
    get_seg(&env->segs[R_GS], &sregs.gs);
775 05330448 aliguori
    get_seg(&env->segs[R_SS], &sregs.ss);
776 05330448 aliguori
777 05330448 aliguori
    get_seg(&env->tr, &sregs.tr);
778 05330448 aliguori
    get_seg(&env->ldt, &sregs.ldt);
779 05330448 aliguori
780 05330448 aliguori
    env->idt.limit = sregs.idt.limit;
781 05330448 aliguori
    env->idt.base = sregs.idt.base;
782 05330448 aliguori
    env->gdt.limit = sregs.gdt.limit;
783 05330448 aliguori
    env->gdt.base = sregs.gdt.base;
784 05330448 aliguori
785 05330448 aliguori
    env->cr[0] = sregs.cr0;
786 05330448 aliguori
    env->cr[2] = sregs.cr2;
787 05330448 aliguori
    env->cr[3] = sregs.cr3;
788 05330448 aliguori
    env->cr[4] = sregs.cr4;
789 05330448 aliguori
790 4a942cea Blue Swirl
    cpu_set_apic_base(env->apic_state, sregs.apic_base);
791 05330448 aliguori
792 05330448 aliguori
    env->efer = sregs.efer;
793 4a942cea Blue Swirl
    //cpu_set_apic_tpr(env->apic_state, sregs.cr8);
794 05330448 aliguori
795 05330448 aliguori
#define HFLAG_COPY_MASK ~( \
796 05330448 aliguori
                        HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
797 05330448 aliguori
                        HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
798 05330448 aliguori
                        HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
799 05330448 aliguori
                        HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
800 05330448 aliguori
801 05330448 aliguori
802 05330448 aliguori
803 05330448 aliguori
    hflags = (env->segs[R_CS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;
804 05330448 aliguori
    hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
805 05330448 aliguori
    hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
806 05330448 aliguori
            (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK);
807 05330448 aliguori
    hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK));
808 05330448 aliguori
    hflags |= (env->cr[4] & CR4_OSFXSR_MASK) <<
809 05330448 aliguori
            (HF_OSFXSR_SHIFT - CR4_OSFXSR_SHIFT);
810 05330448 aliguori
811 05330448 aliguori
    if (env->efer & MSR_EFER_LMA) {
812 05330448 aliguori
        hflags |= HF_LMA_MASK;
813 05330448 aliguori
    }
814 05330448 aliguori
815 05330448 aliguori
    if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
816 05330448 aliguori
        hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
817 05330448 aliguori
    } else {
818 05330448 aliguori
        hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >>
819 05330448 aliguori
                (DESC_B_SHIFT - HF_CS32_SHIFT);
820 05330448 aliguori
        hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >>
821 05330448 aliguori
                (DESC_B_SHIFT - HF_SS32_SHIFT);
822 05330448 aliguori
        if (!(env->cr[0] & CR0_PE_MASK) ||
823 05330448 aliguori
                   (env->eflags & VM_MASK) ||
824 05330448 aliguori
                   !(hflags & HF_CS32_MASK)) {
825 05330448 aliguori
                hflags |= HF_ADDSEG_MASK;
826 05330448 aliguori
            } else {
827 05330448 aliguori
                hflags |= ((env->segs[R_DS].base |
828 05330448 aliguori
                                env->segs[R_ES].base |
829 05330448 aliguori
                                env->segs[R_SS].base) != 0) <<
830 05330448 aliguori
                    HF_ADDSEG_SHIFT;
831 05330448 aliguori
            }
832 05330448 aliguori
    }
833 05330448 aliguori
    env->hflags = (env->hflags & HFLAG_COPY_MASK) | hflags;
834 05330448 aliguori
835 05330448 aliguori
    return 0;
836 05330448 aliguori
}
837 05330448 aliguori
838 05330448 aliguori
static int kvm_get_msrs(CPUState *env)
839 05330448 aliguori
{
840 05330448 aliguori
    struct {
841 05330448 aliguori
        struct kvm_msrs info;
842 05330448 aliguori
        struct kvm_msr_entry entries[100];
843 05330448 aliguori
    } msr_data;
844 05330448 aliguori
    struct kvm_msr_entry *msrs = msr_data.entries;
845 05330448 aliguori
    int ret, i, n;
846 05330448 aliguori
847 05330448 aliguori
    n = 0;
848 05330448 aliguori
    msrs[n++].index = MSR_IA32_SYSENTER_CS;
849 05330448 aliguori
    msrs[n++].index = MSR_IA32_SYSENTER_ESP;
850 05330448 aliguori
    msrs[n++].index = MSR_IA32_SYSENTER_EIP;
851 05330448 aliguori
    if (kvm_has_msr_star(env))
852 05330448 aliguori
        msrs[n++].index = MSR_STAR;
853 05330448 aliguori
    msrs[n++].index = MSR_IA32_TSC;
854 05330448 aliguori
#ifdef TARGET_X86_64
855 05330448 aliguori
    /* FIXME lm_capable_kernel */
856 05330448 aliguori
    msrs[n++].index = MSR_CSTAR;
857 05330448 aliguori
    msrs[n++].index = MSR_KERNELGSBASE;
858 05330448 aliguori
    msrs[n++].index = MSR_FMASK;
859 05330448 aliguori
    msrs[n++].index = MSR_LSTAR;
860 05330448 aliguori
#endif
861 1a03675d Glauber Costa
    msrs[n++].index = MSR_KVM_SYSTEM_TIME;
862 1a03675d Glauber Costa
    msrs[n++].index = MSR_KVM_WALL_CLOCK;
863 1a03675d Glauber Costa
864 05330448 aliguori
    msr_data.info.nmsrs = n;
865 05330448 aliguori
    ret = kvm_vcpu_ioctl(env, KVM_GET_MSRS, &msr_data);
866 05330448 aliguori
    if (ret < 0)
867 05330448 aliguori
        return ret;
868 05330448 aliguori
869 05330448 aliguori
    for (i = 0; i < ret; i++) {
870 05330448 aliguori
        switch (msrs[i].index) {
871 05330448 aliguori
        case MSR_IA32_SYSENTER_CS:
872 05330448 aliguori
            env->sysenter_cs = msrs[i].data;
873 05330448 aliguori
            break;
874 05330448 aliguori
        case MSR_IA32_SYSENTER_ESP:
875 05330448 aliguori
            env->sysenter_esp = msrs[i].data;
876 05330448 aliguori
            break;
877 05330448 aliguori
        case MSR_IA32_SYSENTER_EIP:
878 05330448 aliguori
            env->sysenter_eip = msrs[i].data;
879 05330448 aliguori
            break;
880 05330448 aliguori
        case MSR_STAR:
881 05330448 aliguori
            env->star = msrs[i].data;
882 05330448 aliguori
            break;
883 05330448 aliguori
#ifdef TARGET_X86_64
884 05330448 aliguori
        case MSR_CSTAR:
885 05330448 aliguori
            env->cstar = msrs[i].data;
886 05330448 aliguori
            break;
887 05330448 aliguori
        case MSR_KERNELGSBASE:
888 05330448 aliguori
            env->kernelgsbase = msrs[i].data;
889 05330448 aliguori
            break;
890 05330448 aliguori
        case MSR_FMASK:
891 05330448 aliguori
            env->fmask = msrs[i].data;
892 05330448 aliguori
            break;
893 05330448 aliguori
        case MSR_LSTAR:
894 05330448 aliguori
            env->lstar = msrs[i].data;
895 05330448 aliguori
            break;
896 05330448 aliguori
#endif
897 05330448 aliguori
        case MSR_IA32_TSC:
898 05330448 aliguori
            env->tsc = msrs[i].data;
899 05330448 aliguori
            break;
900 1a03675d Glauber Costa
        case MSR_KVM_SYSTEM_TIME:
901 1a03675d Glauber Costa
            env->system_time_msr = msrs[i].data;
902 1a03675d Glauber Costa
            break;
903 1a03675d Glauber Costa
        case MSR_KVM_WALL_CLOCK:
904 1a03675d Glauber Costa
            env->wall_clock_msr = msrs[i].data;
905 1a03675d Glauber Costa
            break;
906 05330448 aliguori
        }
907 05330448 aliguori
    }
908 05330448 aliguori
909 05330448 aliguori
    return 0;
910 05330448 aliguori
}
911 05330448 aliguori
912 9bdbe550 Hollis Blanchard
static int kvm_put_mp_state(CPUState *env)
913 9bdbe550 Hollis Blanchard
{
914 9bdbe550 Hollis Blanchard
    struct kvm_mp_state mp_state = { .mp_state = env->mp_state };
915 9bdbe550 Hollis Blanchard
916 9bdbe550 Hollis Blanchard
    return kvm_vcpu_ioctl(env, KVM_SET_MP_STATE, &mp_state);
917 9bdbe550 Hollis Blanchard
}
918 9bdbe550 Hollis Blanchard
919 9bdbe550 Hollis Blanchard
static int kvm_get_mp_state(CPUState *env)
920 9bdbe550 Hollis Blanchard
{
921 9bdbe550 Hollis Blanchard
    struct kvm_mp_state mp_state;
922 9bdbe550 Hollis Blanchard
    int ret;
923 9bdbe550 Hollis Blanchard
924 9bdbe550 Hollis Blanchard
    ret = kvm_vcpu_ioctl(env, KVM_GET_MP_STATE, &mp_state);
925 9bdbe550 Hollis Blanchard
    if (ret < 0) {
926 9bdbe550 Hollis Blanchard
        return ret;
927 9bdbe550 Hollis Blanchard
    }
928 9bdbe550 Hollis Blanchard
    env->mp_state = mp_state.mp_state;
929 9bdbe550 Hollis Blanchard
    return 0;
930 9bdbe550 Hollis Blanchard
}
931 9bdbe550 Hollis Blanchard
932 ea643051 Jan Kiszka
static int kvm_put_vcpu_events(CPUState *env, int level)
933 a0fb002c Jan Kiszka
{
934 a0fb002c Jan Kiszka
#ifdef KVM_CAP_VCPU_EVENTS
935 a0fb002c Jan Kiszka
    struct kvm_vcpu_events events;
936 a0fb002c Jan Kiszka
937 a0fb002c Jan Kiszka
    if (!kvm_has_vcpu_events()) {
938 a0fb002c Jan Kiszka
        return 0;
939 a0fb002c Jan Kiszka
    }
940 a0fb002c Jan Kiszka
941 31827373 Jan Kiszka
    events.exception.injected = (env->exception_injected >= 0);
942 31827373 Jan Kiszka
    events.exception.nr = env->exception_injected;
943 a0fb002c Jan Kiszka
    events.exception.has_error_code = env->has_error_code;
944 a0fb002c Jan Kiszka
    events.exception.error_code = env->error_code;
945 a0fb002c Jan Kiszka
946 a0fb002c Jan Kiszka
    events.interrupt.injected = (env->interrupt_injected >= 0);
947 a0fb002c Jan Kiszka
    events.interrupt.nr = env->interrupt_injected;
948 a0fb002c Jan Kiszka
    events.interrupt.soft = env->soft_interrupt;
949 a0fb002c Jan Kiszka
950 a0fb002c Jan Kiszka
    events.nmi.injected = env->nmi_injected;
951 a0fb002c Jan Kiszka
    events.nmi.pending = env->nmi_pending;
952 a0fb002c Jan Kiszka
    events.nmi.masked = !!(env->hflags2 & HF2_NMI_MASK);
953 a0fb002c Jan Kiszka
954 a0fb002c Jan Kiszka
    events.sipi_vector = env->sipi_vector;
955 a0fb002c Jan Kiszka
956 ea643051 Jan Kiszka
    events.flags = 0;
957 ea643051 Jan Kiszka
    if (level >= KVM_PUT_RESET_STATE) {
958 ea643051 Jan Kiszka
        events.flags |=
959 ea643051 Jan Kiszka
            KVM_VCPUEVENT_VALID_NMI_PENDING | KVM_VCPUEVENT_VALID_SIPI_VECTOR;
960 ea643051 Jan Kiszka
    }
961 aee028b9 Jan Kiszka
962 a0fb002c Jan Kiszka
    return kvm_vcpu_ioctl(env, KVM_SET_VCPU_EVENTS, &events);
963 a0fb002c Jan Kiszka
#else
964 a0fb002c Jan Kiszka
    return 0;
965 a0fb002c Jan Kiszka
#endif
966 a0fb002c Jan Kiszka
}
967 a0fb002c Jan Kiszka
968 a0fb002c Jan Kiszka
static int kvm_get_vcpu_events(CPUState *env)
969 a0fb002c Jan Kiszka
{
970 a0fb002c Jan Kiszka
#ifdef KVM_CAP_VCPU_EVENTS
971 a0fb002c Jan Kiszka
    struct kvm_vcpu_events events;
972 a0fb002c Jan Kiszka
    int ret;
973 a0fb002c Jan Kiszka
974 a0fb002c Jan Kiszka
    if (!kvm_has_vcpu_events()) {
975 a0fb002c Jan Kiszka
        return 0;
976 a0fb002c Jan Kiszka
    }
977 a0fb002c Jan Kiszka
978 a0fb002c Jan Kiszka
    ret = kvm_vcpu_ioctl(env, KVM_GET_VCPU_EVENTS, &events);
979 a0fb002c Jan Kiszka
    if (ret < 0) {
980 a0fb002c Jan Kiszka
       return ret;
981 a0fb002c Jan Kiszka
    }
982 31827373 Jan Kiszka
    env->exception_injected =
983 a0fb002c Jan Kiszka
       events.exception.injected ? events.exception.nr : -1;
984 a0fb002c Jan Kiszka
    env->has_error_code = events.exception.has_error_code;
985 a0fb002c Jan Kiszka
    env->error_code = events.exception.error_code;
986 a0fb002c Jan Kiszka
987 a0fb002c Jan Kiszka
    env->interrupt_injected =
988 a0fb002c Jan Kiszka
        events.interrupt.injected ? events.interrupt.nr : -1;
989 a0fb002c Jan Kiszka
    env->soft_interrupt = events.interrupt.soft;
990 a0fb002c Jan Kiszka
991 a0fb002c Jan Kiszka
    env->nmi_injected = events.nmi.injected;
992 a0fb002c Jan Kiszka
    env->nmi_pending = events.nmi.pending;
993 a0fb002c Jan Kiszka
    if (events.nmi.masked) {
994 a0fb002c Jan Kiszka
        env->hflags2 |= HF2_NMI_MASK;
995 a0fb002c Jan Kiszka
    } else {
996 a0fb002c Jan Kiszka
        env->hflags2 &= ~HF2_NMI_MASK;
997 a0fb002c Jan Kiszka
    }
998 a0fb002c Jan Kiszka
999 a0fb002c Jan Kiszka
    env->sipi_vector = events.sipi_vector;
1000 a0fb002c Jan Kiszka
#endif
1001 a0fb002c Jan Kiszka
1002 a0fb002c Jan Kiszka
    return 0;
1003 a0fb002c Jan Kiszka
}
1004 a0fb002c Jan Kiszka
1005 b0b1d690 Jan Kiszka
static int kvm_guest_debug_workarounds(CPUState *env)
1006 b0b1d690 Jan Kiszka
{
1007 b0b1d690 Jan Kiszka
    int ret = 0;
1008 b0b1d690 Jan Kiszka
#ifdef KVM_CAP_SET_GUEST_DEBUG
1009 b0b1d690 Jan Kiszka
    unsigned long reinject_trap = 0;
1010 b0b1d690 Jan Kiszka
1011 b0b1d690 Jan Kiszka
    if (!kvm_has_vcpu_events()) {
1012 b0b1d690 Jan Kiszka
        if (env->exception_injected == 1) {
1013 b0b1d690 Jan Kiszka
            reinject_trap = KVM_GUESTDBG_INJECT_DB;
1014 b0b1d690 Jan Kiszka
        } else if (env->exception_injected == 3) {
1015 b0b1d690 Jan Kiszka
            reinject_trap = KVM_GUESTDBG_INJECT_BP;
1016 b0b1d690 Jan Kiszka
        }
1017 b0b1d690 Jan Kiszka
        env->exception_injected = -1;
1018 b0b1d690 Jan Kiszka
    }
1019 b0b1d690 Jan Kiszka
1020 b0b1d690 Jan Kiszka
    /*
1021 b0b1d690 Jan Kiszka
     * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1022 b0b1d690 Jan Kiszka
     * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1023 b0b1d690 Jan Kiszka
     * by updating the debug state once again if single-stepping is on.
1024 b0b1d690 Jan Kiszka
     * Another reason to call kvm_update_guest_debug here is a pending debug
1025 b0b1d690 Jan Kiszka
     * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1026 b0b1d690 Jan Kiszka
     * reinject them via SET_GUEST_DEBUG.
1027 b0b1d690 Jan Kiszka
     */
1028 b0b1d690 Jan Kiszka
    if (reinject_trap ||
1029 b0b1d690 Jan Kiszka
        (!kvm_has_robust_singlestep() && env->singlestep_enabled)) {
1030 b0b1d690 Jan Kiszka
        ret = kvm_update_guest_debug(env, reinject_trap);
1031 b0b1d690 Jan Kiszka
    }
1032 b0b1d690 Jan Kiszka
#endif /* KVM_CAP_SET_GUEST_DEBUG */
1033 b0b1d690 Jan Kiszka
    return ret;
1034 b0b1d690 Jan Kiszka
}
1035 b0b1d690 Jan Kiszka
1036 ff44f1a3 Jan Kiszka
static int kvm_put_debugregs(CPUState *env)
1037 ff44f1a3 Jan Kiszka
{
1038 ff44f1a3 Jan Kiszka
#ifdef KVM_CAP_DEBUGREGS
1039 ff44f1a3 Jan Kiszka
    struct kvm_debugregs dbgregs;
1040 ff44f1a3 Jan Kiszka
    int i;
1041 ff44f1a3 Jan Kiszka
1042 ff44f1a3 Jan Kiszka
    if (!kvm_has_debugregs()) {
1043 ff44f1a3 Jan Kiszka
        return 0;
1044 ff44f1a3 Jan Kiszka
    }
1045 ff44f1a3 Jan Kiszka
1046 ff44f1a3 Jan Kiszka
    for (i = 0; i < 4; i++) {
1047 ff44f1a3 Jan Kiszka
        dbgregs.db[i] = env->dr[i];
1048 ff44f1a3 Jan Kiszka
    }
1049 ff44f1a3 Jan Kiszka
    dbgregs.dr6 = env->dr[6];
1050 ff44f1a3 Jan Kiszka
    dbgregs.dr7 = env->dr[7];
1051 ff44f1a3 Jan Kiszka
    dbgregs.flags = 0;
1052 ff44f1a3 Jan Kiszka
1053 ff44f1a3 Jan Kiszka
    return kvm_vcpu_ioctl(env, KVM_SET_DEBUGREGS, &dbgregs);
1054 ff44f1a3 Jan Kiszka
#else
1055 ff44f1a3 Jan Kiszka
    return 0;
1056 ff44f1a3 Jan Kiszka
#endif
1057 ff44f1a3 Jan Kiszka
}
1058 ff44f1a3 Jan Kiszka
1059 ff44f1a3 Jan Kiszka
static int kvm_get_debugregs(CPUState *env)
1060 ff44f1a3 Jan Kiszka
{
1061 ff44f1a3 Jan Kiszka
#ifdef KVM_CAP_DEBUGREGS
1062 ff44f1a3 Jan Kiszka
    struct kvm_debugregs dbgregs;
1063 ff44f1a3 Jan Kiszka
    int i, ret;
1064 ff44f1a3 Jan Kiszka
1065 ff44f1a3 Jan Kiszka
    if (!kvm_has_debugregs()) {
1066 ff44f1a3 Jan Kiszka
        return 0;
1067 ff44f1a3 Jan Kiszka
    }
1068 ff44f1a3 Jan Kiszka
1069 ff44f1a3 Jan Kiszka
    ret = kvm_vcpu_ioctl(env, KVM_GET_DEBUGREGS, &dbgregs);
1070 ff44f1a3 Jan Kiszka
    if (ret < 0) {
1071 ff44f1a3 Jan Kiszka
       return ret;
1072 ff44f1a3 Jan Kiszka
    }
1073 ff44f1a3 Jan Kiszka
    for (i = 0; i < 4; i++) {
1074 ff44f1a3 Jan Kiszka
        env->dr[i] = dbgregs.db[i];
1075 ff44f1a3 Jan Kiszka
    }
1076 ff44f1a3 Jan Kiszka
    env->dr[4] = env->dr[6] = dbgregs.dr6;
1077 ff44f1a3 Jan Kiszka
    env->dr[5] = env->dr[7] = dbgregs.dr7;
1078 ff44f1a3 Jan Kiszka
#endif
1079 ff44f1a3 Jan Kiszka
1080 ff44f1a3 Jan Kiszka
    return 0;
1081 ff44f1a3 Jan Kiszka
}
1082 ff44f1a3 Jan Kiszka
1083 ea375f9a Jan Kiszka
int kvm_arch_put_registers(CPUState *env, int level)
1084 05330448 aliguori
{
1085 05330448 aliguori
    int ret;
1086 05330448 aliguori
1087 dbaa07c4 Jan Kiszka
    assert(cpu_is_stopped(env) || qemu_cpu_self(env));
1088 dbaa07c4 Jan Kiszka
1089 05330448 aliguori
    ret = kvm_getput_regs(env, 1);
1090 05330448 aliguori
    if (ret < 0)
1091 05330448 aliguori
        return ret;
1092 05330448 aliguori
1093 f1665b21 Sheng Yang
    ret = kvm_put_xsave(env);
1094 f1665b21 Sheng Yang
    if (ret < 0)
1095 f1665b21 Sheng Yang
        return ret;
1096 f1665b21 Sheng Yang
1097 f1665b21 Sheng Yang
    ret = kvm_put_xcrs(env);
1098 05330448 aliguori
    if (ret < 0)
1099 05330448 aliguori
        return ret;
1100 05330448 aliguori
1101 05330448 aliguori
    ret = kvm_put_sregs(env);
1102 05330448 aliguori
    if (ret < 0)
1103 05330448 aliguori
        return ret;
1104 05330448 aliguori
1105 ea643051 Jan Kiszka
    ret = kvm_put_msrs(env, level);
1106 05330448 aliguori
    if (ret < 0)
1107 05330448 aliguori
        return ret;
1108 05330448 aliguori
1109 ea643051 Jan Kiszka
    if (level >= KVM_PUT_RESET_STATE) {
1110 ea643051 Jan Kiszka
        ret = kvm_put_mp_state(env);
1111 ea643051 Jan Kiszka
        if (ret < 0)
1112 ea643051 Jan Kiszka
            return ret;
1113 ea643051 Jan Kiszka
    }
1114 f8d926e9 Jan Kiszka
1115 ea643051 Jan Kiszka
    ret = kvm_put_vcpu_events(env, level);
1116 a0fb002c Jan Kiszka
    if (ret < 0)
1117 a0fb002c Jan Kiszka
        return ret;
1118 a0fb002c Jan Kiszka
1119 b0b1d690 Jan Kiszka
    /* must be last */
1120 b0b1d690 Jan Kiszka
    ret = kvm_guest_debug_workarounds(env);
1121 b0b1d690 Jan Kiszka
    if (ret < 0)
1122 b0b1d690 Jan Kiszka
        return ret;
1123 b0b1d690 Jan Kiszka
1124 ff44f1a3 Jan Kiszka
    ret = kvm_put_debugregs(env);
1125 ff44f1a3 Jan Kiszka
    if (ret < 0)
1126 ff44f1a3 Jan Kiszka
        return ret;
1127 ff44f1a3 Jan Kiszka
1128 05330448 aliguori
    return 0;
1129 05330448 aliguori
}
1130 05330448 aliguori
1131 05330448 aliguori
int kvm_arch_get_registers(CPUState *env)
1132 05330448 aliguori
{
1133 05330448 aliguori
    int ret;
1134 05330448 aliguori
1135 dbaa07c4 Jan Kiszka
    assert(cpu_is_stopped(env) || qemu_cpu_self(env));
1136 dbaa07c4 Jan Kiszka
1137 05330448 aliguori
    ret = kvm_getput_regs(env, 0);
1138 05330448 aliguori
    if (ret < 0)
1139 05330448 aliguori
        return ret;
1140 05330448 aliguori
1141 f1665b21 Sheng Yang
    ret = kvm_get_xsave(env);
1142 f1665b21 Sheng Yang
    if (ret < 0)
1143 f1665b21 Sheng Yang
        return ret;
1144 f1665b21 Sheng Yang
1145 f1665b21 Sheng Yang
    ret = kvm_get_xcrs(env);
1146 05330448 aliguori
    if (ret < 0)
1147 05330448 aliguori
        return ret;
1148 05330448 aliguori
1149 05330448 aliguori
    ret = kvm_get_sregs(env);
1150 05330448 aliguori
    if (ret < 0)
1151 05330448 aliguori
        return ret;
1152 05330448 aliguori
1153 05330448 aliguori
    ret = kvm_get_msrs(env);
1154 05330448 aliguori
    if (ret < 0)
1155 05330448 aliguori
        return ret;
1156 05330448 aliguori
1157 5a2e3c2e Jan Kiszka
    ret = kvm_get_mp_state(env);
1158 5a2e3c2e Jan Kiszka
    if (ret < 0)
1159 5a2e3c2e Jan Kiszka
        return ret;
1160 5a2e3c2e Jan Kiszka
1161 a0fb002c Jan Kiszka
    ret = kvm_get_vcpu_events(env);
1162 a0fb002c Jan Kiszka
    if (ret < 0)
1163 a0fb002c Jan Kiszka
        return ret;
1164 a0fb002c Jan Kiszka
1165 ff44f1a3 Jan Kiszka
    ret = kvm_get_debugregs(env);
1166 ff44f1a3 Jan Kiszka
    if (ret < 0)
1167 ff44f1a3 Jan Kiszka
        return ret;
1168 ff44f1a3 Jan Kiszka
1169 05330448 aliguori
    return 0;
1170 05330448 aliguori
}
1171 05330448 aliguori
1172 05330448 aliguori
int kvm_arch_pre_run(CPUState *env, struct kvm_run *run)
1173 05330448 aliguori
{
1174 05330448 aliguori
    /* Try to inject an interrupt if the guest can accept it */
1175 05330448 aliguori
    if (run->ready_for_interrupt_injection &&
1176 05330448 aliguori
        (env->interrupt_request & CPU_INTERRUPT_HARD) &&
1177 05330448 aliguori
        (env->eflags & IF_MASK)) {
1178 05330448 aliguori
        int irq;
1179 05330448 aliguori
1180 05330448 aliguori
        env->interrupt_request &= ~CPU_INTERRUPT_HARD;
1181 05330448 aliguori
        irq = cpu_get_pic_interrupt(env);
1182 05330448 aliguori
        if (irq >= 0) {
1183 05330448 aliguori
            struct kvm_interrupt intr;
1184 05330448 aliguori
            intr.irq = irq;
1185 05330448 aliguori
            /* FIXME: errors */
1186 8c0d577e Blue Swirl
            DPRINTF("injected interrupt %d\n", irq);
1187 05330448 aliguori
            kvm_vcpu_ioctl(env, KVM_INTERRUPT, &intr);
1188 05330448 aliguori
        }
1189 05330448 aliguori
    }
1190 05330448 aliguori
1191 05330448 aliguori
    /* If we have an interrupt but the guest is not ready to receive an
1192 05330448 aliguori
     * interrupt, request an interrupt window exit.  This will
1193 05330448 aliguori
     * cause a return to userspace as soon as the guest is ready to
1194 05330448 aliguori
     * receive interrupts. */
1195 05330448 aliguori
    if ((env->interrupt_request & CPU_INTERRUPT_HARD))
1196 05330448 aliguori
        run->request_interrupt_window = 1;
1197 05330448 aliguori
    else
1198 05330448 aliguori
        run->request_interrupt_window = 0;
1199 05330448 aliguori
1200 8c0d577e Blue Swirl
    DPRINTF("setting tpr\n");
1201 4a942cea Blue Swirl
    run->cr8 = cpu_get_apic_tpr(env->apic_state);
1202 05330448 aliguori
1203 05330448 aliguori
    return 0;
1204 05330448 aliguori
}
1205 05330448 aliguori
1206 05330448 aliguori
int kvm_arch_post_run(CPUState *env, struct kvm_run *run)
1207 05330448 aliguori
{
1208 05330448 aliguori
    if (run->if_flag)
1209 05330448 aliguori
        env->eflags |= IF_MASK;
1210 05330448 aliguori
    else
1211 05330448 aliguori
        env->eflags &= ~IF_MASK;
1212 05330448 aliguori
    
1213 4a942cea Blue Swirl
    cpu_set_apic_tpr(env->apic_state, run->cr8);
1214 4a942cea Blue Swirl
    cpu_set_apic_base(env->apic_state, run->apic_base);
1215 05330448 aliguori
1216 05330448 aliguori
    return 0;
1217 05330448 aliguori
}
1218 05330448 aliguori
1219 0af691d7 Marcelo Tosatti
int kvm_arch_process_irqchip_events(CPUState *env)
1220 0af691d7 Marcelo Tosatti
{
1221 0af691d7 Marcelo Tosatti
    if (env->interrupt_request & CPU_INTERRUPT_INIT) {
1222 0af691d7 Marcelo Tosatti
        kvm_cpu_synchronize_state(env);
1223 0af691d7 Marcelo Tosatti
        do_cpu_init(env);
1224 0af691d7 Marcelo Tosatti
        env->exception_index = EXCP_HALTED;
1225 0af691d7 Marcelo Tosatti
    }
1226 0af691d7 Marcelo Tosatti
1227 0af691d7 Marcelo Tosatti
    if (env->interrupt_request & CPU_INTERRUPT_SIPI) {
1228 0af691d7 Marcelo Tosatti
        kvm_cpu_synchronize_state(env);
1229 0af691d7 Marcelo Tosatti
        do_cpu_sipi(env);
1230 0af691d7 Marcelo Tosatti
    }
1231 0af691d7 Marcelo Tosatti
1232 0af691d7 Marcelo Tosatti
    return env->halted;
1233 0af691d7 Marcelo Tosatti
}
1234 0af691d7 Marcelo Tosatti
1235 05330448 aliguori
static int kvm_handle_halt(CPUState *env)
1236 05330448 aliguori
{
1237 05330448 aliguori
    if (!((env->interrupt_request & CPU_INTERRUPT_HARD) &&
1238 05330448 aliguori
          (env->eflags & IF_MASK)) &&
1239 05330448 aliguori
        !(env->interrupt_request & CPU_INTERRUPT_NMI)) {
1240 05330448 aliguori
        env->halted = 1;
1241 05330448 aliguori
        env->exception_index = EXCP_HLT;
1242 05330448 aliguori
        return 0;
1243 05330448 aliguori
    }
1244 05330448 aliguori
1245 05330448 aliguori
    return 1;
1246 05330448 aliguori
}
1247 05330448 aliguori
1248 05330448 aliguori
int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run)
1249 05330448 aliguori
{
1250 05330448 aliguori
    int ret = 0;
1251 05330448 aliguori
1252 05330448 aliguori
    switch (run->exit_reason) {
1253 05330448 aliguori
    case KVM_EXIT_HLT:
1254 8c0d577e Blue Swirl
        DPRINTF("handle_hlt\n");
1255 05330448 aliguori
        ret = kvm_handle_halt(env);
1256 05330448 aliguori
        break;
1257 05330448 aliguori
    }
1258 05330448 aliguori
1259 05330448 aliguori
    return ret;
1260 05330448 aliguori
}
1261 e22a25c9 aliguori
1262 e22a25c9 aliguori
#ifdef KVM_CAP_SET_GUEST_DEBUG
1263 e22a25c9 aliguori
int kvm_arch_insert_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
1264 e22a25c9 aliguori
{
1265 38972938 Juan Quintela
    static const uint8_t int3 = 0xcc;
1266 64bf3f4e aliguori
1267 e22a25c9 aliguori
    if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) ||
1268 64bf3f4e aliguori
        cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&int3, 1, 1))
1269 e22a25c9 aliguori
        return -EINVAL;
1270 e22a25c9 aliguori
    return 0;
1271 e22a25c9 aliguori
}
1272 e22a25c9 aliguori
1273 e22a25c9 aliguori
int kvm_arch_remove_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp)
1274 e22a25c9 aliguori
{
1275 e22a25c9 aliguori
    uint8_t int3;
1276 e22a25c9 aliguori
1277 e22a25c9 aliguori
    if (cpu_memory_rw_debug(env, bp->pc, &int3, 1, 0) || int3 != 0xcc ||
1278 64bf3f4e aliguori
        cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1))
1279 e22a25c9 aliguori
        return -EINVAL;
1280 e22a25c9 aliguori
    return 0;
1281 e22a25c9 aliguori
}
1282 e22a25c9 aliguori
1283 e22a25c9 aliguori
static struct {
1284 e22a25c9 aliguori
    target_ulong addr;
1285 e22a25c9 aliguori
    int len;
1286 e22a25c9 aliguori
    int type;
1287 e22a25c9 aliguori
} hw_breakpoint[4];
1288 e22a25c9 aliguori
1289 e22a25c9 aliguori
static int nb_hw_breakpoint;
1290 e22a25c9 aliguori
1291 e22a25c9 aliguori
static int find_hw_breakpoint(target_ulong addr, int len, int type)
1292 e22a25c9 aliguori
{
1293 e22a25c9 aliguori
    int n;
1294 e22a25c9 aliguori
1295 e22a25c9 aliguori
    for (n = 0; n < nb_hw_breakpoint; n++)
1296 e22a25c9 aliguori
        if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
1297 e22a25c9 aliguori
            (hw_breakpoint[n].len == len || len == -1))
1298 e22a25c9 aliguori
            return n;
1299 e22a25c9 aliguori
    return -1;
1300 e22a25c9 aliguori
}
1301 e22a25c9 aliguori
1302 e22a25c9 aliguori
int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1303 e22a25c9 aliguori
                                  target_ulong len, int type)
1304 e22a25c9 aliguori
{
1305 e22a25c9 aliguori
    switch (type) {
1306 e22a25c9 aliguori
    case GDB_BREAKPOINT_HW:
1307 e22a25c9 aliguori
        len = 1;
1308 e22a25c9 aliguori
        break;
1309 e22a25c9 aliguori
    case GDB_WATCHPOINT_WRITE:
1310 e22a25c9 aliguori
    case GDB_WATCHPOINT_ACCESS:
1311 e22a25c9 aliguori
        switch (len) {
1312 e22a25c9 aliguori
        case 1:
1313 e22a25c9 aliguori
            break;
1314 e22a25c9 aliguori
        case 2:
1315 e22a25c9 aliguori
        case 4:
1316 e22a25c9 aliguori
        case 8:
1317 e22a25c9 aliguori
            if (addr & (len - 1))
1318 e22a25c9 aliguori
                return -EINVAL;
1319 e22a25c9 aliguori
            break;
1320 e22a25c9 aliguori
        default:
1321 e22a25c9 aliguori
            return -EINVAL;
1322 e22a25c9 aliguori
        }
1323 e22a25c9 aliguori
        break;
1324 e22a25c9 aliguori
    default:
1325 e22a25c9 aliguori
        return -ENOSYS;
1326 e22a25c9 aliguori
    }
1327 e22a25c9 aliguori
1328 e22a25c9 aliguori
    if (nb_hw_breakpoint == 4)
1329 e22a25c9 aliguori
        return -ENOBUFS;
1330 e22a25c9 aliguori
1331 e22a25c9 aliguori
    if (find_hw_breakpoint(addr, len, type) >= 0)
1332 e22a25c9 aliguori
        return -EEXIST;
1333 e22a25c9 aliguori
1334 e22a25c9 aliguori
    hw_breakpoint[nb_hw_breakpoint].addr = addr;
1335 e22a25c9 aliguori
    hw_breakpoint[nb_hw_breakpoint].len = len;
1336 e22a25c9 aliguori
    hw_breakpoint[nb_hw_breakpoint].type = type;
1337 e22a25c9 aliguori
    nb_hw_breakpoint++;
1338 e22a25c9 aliguori
1339 e22a25c9 aliguori
    return 0;
1340 e22a25c9 aliguori
}
1341 e22a25c9 aliguori
1342 e22a25c9 aliguori
int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1343 e22a25c9 aliguori
                                  target_ulong len, int type)
1344 e22a25c9 aliguori
{
1345 e22a25c9 aliguori
    int n;
1346 e22a25c9 aliguori
1347 e22a25c9 aliguori
    n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
1348 e22a25c9 aliguori
    if (n < 0)
1349 e22a25c9 aliguori
        return -ENOENT;
1350 e22a25c9 aliguori
1351 e22a25c9 aliguori
    nb_hw_breakpoint--;
1352 e22a25c9 aliguori
    hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint];
1353 e22a25c9 aliguori
1354 e22a25c9 aliguori
    return 0;
1355 e22a25c9 aliguori
}
1356 e22a25c9 aliguori
1357 e22a25c9 aliguori
void kvm_arch_remove_all_hw_breakpoints(void)
1358 e22a25c9 aliguori
{
1359 e22a25c9 aliguori
    nb_hw_breakpoint = 0;
1360 e22a25c9 aliguori
}
1361 e22a25c9 aliguori
1362 e22a25c9 aliguori
static CPUWatchpoint hw_watchpoint;
1363 e22a25c9 aliguori
1364 e22a25c9 aliguori
int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
1365 e22a25c9 aliguori
{
1366 e22a25c9 aliguori
    int handle = 0;
1367 e22a25c9 aliguori
    int n;
1368 e22a25c9 aliguori
1369 e22a25c9 aliguori
    if (arch_info->exception == 1) {
1370 e22a25c9 aliguori
        if (arch_info->dr6 & (1 << 14)) {
1371 e22a25c9 aliguori
            if (cpu_single_env->singlestep_enabled)
1372 e22a25c9 aliguori
                handle = 1;
1373 e22a25c9 aliguori
        } else {
1374 e22a25c9 aliguori
            for (n = 0; n < 4; n++)
1375 e22a25c9 aliguori
                if (arch_info->dr6 & (1 << n))
1376 e22a25c9 aliguori
                    switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
1377 e22a25c9 aliguori
                    case 0x0:
1378 e22a25c9 aliguori
                        handle = 1;
1379 e22a25c9 aliguori
                        break;
1380 e22a25c9 aliguori
                    case 0x1:
1381 e22a25c9 aliguori
                        handle = 1;
1382 e22a25c9 aliguori
                        cpu_single_env->watchpoint_hit = &hw_watchpoint;
1383 e22a25c9 aliguori
                        hw_watchpoint.vaddr = hw_breakpoint[n].addr;
1384 e22a25c9 aliguori
                        hw_watchpoint.flags = BP_MEM_WRITE;
1385 e22a25c9 aliguori
                        break;
1386 e22a25c9 aliguori
                    case 0x3:
1387 e22a25c9 aliguori
                        handle = 1;
1388 e22a25c9 aliguori
                        cpu_single_env->watchpoint_hit = &hw_watchpoint;
1389 e22a25c9 aliguori
                        hw_watchpoint.vaddr = hw_breakpoint[n].addr;
1390 e22a25c9 aliguori
                        hw_watchpoint.flags = BP_MEM_ACCESS;
1391 e22a25c9 aliguori
                        break;
1392 e22a25c9 aliguori
                    }
1393 e22a25c9 aliguori
        }
1394 e22a25c9 aliguori
    } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc))
1395 e22a25c9 aliguori
        handle = 1;
1396 e22a25c9 aliguori
1397 b0b1d690 Jan Kiszka
    if (!handle) {
1398 b0b1d690 Jan Kiszka
        cpu_synchronize_state(cpu_single_env);
1399 b0b1d690 Jan Kiszka
        assert(cpu_single_env->exception_injected == -1);
1400 b0b1d690 Jan Kiszka
1401 b0b1d690 Jan Kiszka
        cpu_single_env->exception_injected = arch_info->exception;
1402 b0b1d690 Jan Kiszka
        cpu_single_env->has_error_code = 0;
1403 b0b1d690 Jan Kiszka
    }
1404 e22a25c9 aliguori
1405 e22a25c9 aliguori
    return handle;
1406 e22a25c9 aliguori
}
1407 e22a25c9 aliguori
1408 e22a25c9 aliguori
void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
1409 e22a25c9 aliguori
{
1410 e22a25c9 aliguori
    const uint8_t type_code[] = {
1411 e22a25c9 aliguori
        [GDB_BREAKPOINT_HW] = 0x0,
1412 e22a25c9 aliguori
        [GDB_WATCHPOINT_WRITE] = 0x1,
1413 e22a25c9 aliguori
        [GDB_WATCHPOINT_ACCESS] = 0x3
1414 e22a25c9 aliguori
    };
1415 e22a25c9 aliguori
    const uint8_t len_code[] = {
1416 e22a25c9 aliguori
        [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1417 e22a25c9 aliguori
    };
1418 e22a25c9 aliguori
    int n;
1419 e22a25c9 aliguori
1420 e22a25c9 aliguori
    if (kvm_sw_breakpoints_active(env))
1421 e22a25c9 aliguori
        dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1422 e22a25c9 aliguori
1423 e22a25c9 aliguori
    if (nb_hw_breakpoint > 0) {
1424 e22a25c9 aliguori
        dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1425 e22a25c9 aliguori
        dbg->arch.debugreg[7] = 0x0600;
1426 e22a25c9 aliguori
        for (n = 0; n < nb_hw_breakpoint; n++) {
1427 e22a25c9 aliguori
            dbg->arch.debugreg[n] = hw_breakpoint[n].addr;
1428 e22a25c9 aliguori
            dbg->arch.debugreg[7] |= (2 << (n * 2)) |
1429 e22a25c9 aliguori
                (type_code[hw_breakpoint[n].type] << (16 + n*4)) |
1430 e22a25c9 aliguori
                (len_code[hw_breakpoint[n].len] << (18 + n*4));
1431 e22a25c9 aliguori
        }
1432 e22a25c9 aliguori
    }
1433 f1665b21 Sheng Yang
    /* Legal xcr0 for loading */
1434 f1665b21 Sheng Yang
    env->xcr0 = 1;
1435 e22a25c9 aliguori
}
1436 e22a25c9 aliguori
#endif /* KVM_CAP_SET_GUEST_DEBUG */
1437 4513d923 Gleb Natapov
1438 4513d923 Gleb Natapov
bool kvm_arch_stop_on_emulation_error(CPUState *env)
1439 4513d923 Gleb Natapov
{
1440 4513d923 Gleb Natapov
      return !(env->cr[0] & CR0_PE_MASK) ||
1441 4513d923 Gleb Natapov
              ((env->segs[R_CS].selector  & 3) != 3);
1442 4513d923 Gleb Natapov
}