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