root / kvm-all.c @ 79f85c3a
History | View | Annotate | Download (16.4 kB)
1 | 05330448 | aliguori | /*
|
---|---|---|---|
2 | 05330448 | aliguori | * QEMU KVM support
|
3 | 05330448 | aliguori | *
|
4 | 05330448 | aliguori | * Copyright IBM, Corp. 2008
|
5 | 5832d1f2 | aliguori | * Red Hat, Inc. 2008
|
6 | 05330448 | aliguori | *
|
7 | 05330448 | aliguori | * Authors:
|
8 | 05330448 | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
9 | 5832d1f2 | aliguori | * Glauber Costa <gcosta@redhat.com>
|
10 | 05330448 | aliguori | *
|
11 | 05330448 | aliguori | * This work is licensed under the terms of the GNU GPL, version 2 or later.
|
12 | 05330448 | aliguori | * See the COPYING file in the top-level directory.
|
13 | 05330448 | aliguori | *
|
14 | 05330448 | aliguori | */
|
15 | 05330448 | aliguori | |
16 | 05330448 | aliguori | #include <sys/types.h> |
17 | 05330448 | aliguori | #include <sys/ioctl.h> |
18 | 05330448 | aliguori | #include <sys/mman.h> |
19 | 984b5181 | aliguori | #include <stdarg.h> |
20 | 05330448 | aliguori | |
21 | 05330448 | aliguori | #include <linux/kvm.h> |
22 | 05330448 | aliguori | |
23 | 05330448 | aliguori | #include "qemu-common.h" |
24 | 05330448 | aliguori | #include "sysemu.h" |
25 | 05330448 | aliguori | #include "kvm.h" |
26 | 05330448 | aliguori | |
27 | f65ed4c1 | aliguori | /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
|
28 | f65ed4c1 | aliguori | #define PAGE_SIZE TARGET_PAGE_SIZE
|
29 | f65ed4c1 | aliguori | |
30 | 05330448 | aliguori | //#define DEBUG_KVM
|
31 | 05330448 | aliguori | |
32 | 05330448 | aliguori | #ifdef DEBUG_KVM
|
33 | 05330448 | aliguori | #define dprintf(fmt, ...) \
|
34 | 05330448 | aliguori | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
35 | 05330448 | aliguori | #else
|
36 | 05330448 | aliguori | #define dprintf(fmt, ...) \
|
37 | 05330448 | aliguori | do { } while (0) |
38 | 05330448 | aliguori | #endif
|
39 | 05330448 | aliguori | |
40 | 34fc643f | aliguori | typedef struct KVMSlot |
41 | 34fc643f | aliguori | { |
42 | 34fc643f | aliguori | target_phys_addr_t start_addr; |
43 | 34fc643f | aliguori | ram_addr_t memory_size; |
44 | 34fc643f | aliguori | ram_addr_t phys_offset; |
45 | 34fc643f | aliguori | int slot;
|
46 | 34fc643f | aliguori | int flags;
|
47 | 34fc643f | aliguori | } KVMSlot; |
48 | 05330448 | aliguori | |
49 | 5832d1f2 | aliguori | typedef struct kvm_dirty_log KVMDirtyLog; |
50 | 5832d1f2 | aliguori | |
51 | 05330448 | aliguori | int kvm_allowed = 0; |
52 | 05330448 | aliguori | |
53 | 05330448 | aliguori | struct KVMState
|
54 | 05330448 | aliguori | { |
55 | 05330448 | aliguori | KVMSlot slots[32];
|
56 | 05330448 | aliguori | int fd;
|
57 | 05330448 | aliguori | int vmfd;
|
58 | f65ed4c1 | aliguori | int coalesced_mmio;
|
59 | 05330448 | aliguori | }; |
60 | 05330448 | aliguori | |
61 | 05330448 | aliguori | static KVMState *kvm_state;
|
62 | 05330448 | aliguori | |
63 | 05330448 | aliguori | static KVMSlot *kvm_alloc_slot(KVMState *s)
|
64 | 05330448 | aliguori | { |
65 | 05330448 | aliguori | int i;
|
66 | 05330448 | aliguori | |
67 | 05330448 | aliguori | for (i = 0; i < ARRAY_SIZE(s->slots); i++) { |
68 | 62d60e8c | aliguori | /* KVM private memory slots */
|
69 | 62d60e8c | aliguori | if (i >= 8 && i < 12) |
70 | 62d60e8c | aliguori | continue;
|
71 | 05330448 | aliguori | if (s->slots[i].memory_size == 0) |
72 | 05330448 | aliguori | return &s->slots[i];
|
73 | 05330448 | aliguori | } |
74 | 05330448 | aliguori | |
75 | 05330448 | aliguori | return NULL; |
76 | 05330448 | aliguori | } |
77 | 05330448 | aliguori | |
78 | 05330448 | aliguori | static KVMSlot *kvm_lookup_slot(KVMState *s, target_phys_addr_t start_addr)
|
79 | 05330448 | aliguori | { |
80 | 05330448 | aliguori | int i;
|
81 | 05330448 | aliguori | |
82 | 05330448 | aliguori | for (i = 0; i < ARRAY_SIZE(s->slots); i++) { |
83 | 05330448 | aliguori | KVMSlot *mem = &s->slots[i]; |
84 | 05330448 | aliguori | |
85 | 34fc643f | aliguori | if (start_addr >= mem->start_addr &&
|
86 | 34fc643f | aliguori | start_addr < (mem->start_addr + mem->memory_size)) |
87 | 05330448 | aliguori | return mem;
|
88 | 05330448 | aliguori | } |
89 | 05330448 | aliguori | |
90 | 05330448 | aliguori | return NULL; |
91 | 05330448 | aliguori | } |
92 | 05330448 | aliguori | |
93 | 5832d1f2 | aliguori | static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot) |
94 | 5832d1f2 | aliguori | { |
95 | 5832d1f2 | aliguori | struct kvm_userspace_memory_region mem;
|
96 | 5832d1f2 | aliguori | |
97 | 5832d1f2 | aliguori | mem.slot = slot->slot; |
98 | 5832d1f2 | aliguori | mem.guest_phys_addr = slot->start_addr; |
99 | 5832d1f2 | aliguori | mem.memory_size = slot->memory_size; |
100 | 5832d1f2 | aliguori | mem.userspace_addr = (unsigned long)phys_ram_base + slot->phys_offset; |
101 | 5832d1f2 | aliguori | mem.flags = slot->flags; |
102 | 5832d1f2 | aliguori | |
103 | 5832d1f2 | aliguori | return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
|
104 | 5832d1f2 | aliguori | } |
105 | 5832d1f2 | aliguori | |
106 | 5832d1f2 | aliguori | |
107 | 05330448 | aliguori | int kvm_init_vcpu(CPUState *env)
|
108 | 05330448 | aliguori | { |
109 | 05330448 | aliguori | KVMState *s = kvm_state; |
110 | 05330448 | aliguori | long mmap_size;
|
111 | 05330448 | aliguori | int ret;
|
112 | 05330448 | aliguori | |
113 | 05330448 | aliguori | dprintf("kvm_init_vcpu\n");
|
114 | 05330448 | aliguori | |
115 | 984b5181 | aliguori | ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, env->cpu_index); |
116 | 05330448 | aliguori | if (ret < 0) { |
117 | 05330448 | aliguori | dprintf("kvm_create_vcpu failed\n");
|
118 | 05330448 | aliguori | goto err;
|
119 | 05330448 | aliguori | } |
120 | 05330448 | aliguori | |
121 | 05330448 | aliguori | env->kvm_fd = ret; |
122 | 05330448 | aliguori | env->kvm_state = s; |
123 | 05330448 | aliguori | |
124 | 05330448 | aliguori | mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
|
125 | 05330448 | aliguori | if (mmap_size < 0) { |
126 | 05330448 | aliguori | dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
|
127 | 05330448 | aliguori | goto err;
|
128 | 05330448 | aliguori | } |
129 | 05330448 | aliguori | |
130 | 05330448 | aliguori | env->kvm_run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
131 | 05330448 | aliguori | env->kvm_fd, 0);
|
132 | 05330448 | aliguori | if (env->kvm_run == MAP_FAILED) {
|
133 | 05330448 | aliguori | ret = -errno; |
134 | 05330448 | aliguori | dprintf("mmap'ing vcpu state failed\n");
|
135 | 05330448 | aliguori | goto err;
|
136 | 05330448 | aliguori | } |
137 | 05330448 | aliguori | |
138 | 05330448 | aliguori | ret = kvm_arch_init_vcpu(env); |
139 | 05330448 | aliguori | |
140 | 05330448 | aliguori | err:
|
141 | 05330448 | aliguori | return ret;
|
142 | 05330448 | aliguori | } |
143 | 05330448 | aliguori | |
144 | f5d6f51b | aliguori | int kvm_sync_vcpus(void) |
145 | f5d6f51b | aliguori | { |
146 | f5d6f51b | aliguori | CPUState *env; |
147 | f5d6f51b | aliguori | |
148 | f5d6f51b | aliguori | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
149 | f5d6f51b | aliguori | int ret;
|
150 | f5d6f51b | aliguori | |
151 | f5d6f51b | aliguori | ret = kvm_arch_put_registers(env); |
152 | f5d6f51b | aliguori | if (ret)
|
153 | f5d6f51b | aliguori | return ret;
|
154 | f5d6f51b | aliguori | } |
155 | f5d6f51b | aliguori | |
156 | f5d6f51b | aliguori | return 0; |
157 | f5d6f51b | aliguori | } |
158 | f5d6f51b | aliguori | |
159 | 5832d1f2 | aliguori | /*
|
160 | 5832d1f2 | aliguori | * dirty pages logging control
|
161 | 5832d1f2 | aliguori | */
|
162 | 5832d1f2 | aliguori | static int kvm_dirty_pages_log_change(target_phys_addr_t phys_addr, target_phys_addr_t end_addr, |
163 | 5832d1f2 | aliguori | unsigned flags,
|
164 | 5832d1f2 | aliguori | unsigned mask)
|
165 | 5832d1f2 | aliguori | { |
166 | 5832d1f2 | aliguori | KVMState *s = kvm_state; |
167 | 5832d1f2 | aliguori | KVMSlot *mem = kvm_lookup_slot(s, phys_addr); |
168 | 5832d1f2 | aliguori | if (mem == NULL) { |
169 | 5832d1f2 | aliguori | dprintf("invalid parameters %llx-%llx\n", phys_addr, end_addr);
|
170 | 5832d1f2 | aliguori | return -EINVAL;
|
171 | 5832d1f2 | aliguori | } |
172 | 5832d1f2 | aliguori | |
173 | 5832d1f2 | aliguori | flags = (mem->flags & ~mask) | flags; |
174 | 5832d1f2 | aliguori | /* Nothing changed, no need to issue ioctl */
|
175 | 5832d1f2 | aliguori | if (flags == mem->flags)
|
176 | 5832d1f2 | aliguori | return 0; |
177 | 5832d1f2 | aliguori | |
178 | 5832d1f2 | aliguori | mem->flags = flags; |
179 | 5832d1f2 | aliguori | |
180 | 5832d1f2 | aliguori | return kvm_set_user_memory_region(s, mem);
|
181 | 5832d1f2 | aliguori | } |
182 | 5832d1f2 | aliguori | |
183 | 5832d1f2 | aliguori | int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t end_addr)
|
184 | 5832d1f2 | aliguori | { |
185 | 5832d1f2 | aliguori | return kvm_dirty_pages_log_change(phys_addr, end_addr,
|
186 | 5832d1f2 | aliguori | KVM_MEM_LOG_DIRTY_PAGES, |
187 | 5832d1f2 | aliguori | KVM_MEM_LOG_DIRTY_PAGES); |
188 | 5832d1f2 | aliguori | } |
189 | 5832d1f2 | aliguori | |
190 | 5832d1f2 | aliguori | int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t end_addr)
|
191 | 5832d1f2 | aliguori | { |
192 | 5832d1f2 | aliguori | return kvm_dirty_pages_log_change(phys_addr, end_addr,
|
193 | 5832d1f2 | aliguori | 0,
|
194 | 5832d1f2 | aliguori | KVM_MEM_LOG_DIRTY_PAGES); |
195 | 5832d1f2 | aliguori | } |
196 | 5832d1f2 | aliguori | |
197 | 5832d1f2 | aliguori | /**
|
198 | 5832d1f2 | aliguori | * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
|
199 | 5832d1f2 | aliguori | * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty().
|
200 | 5832d1f2 | aliguori | * This means all bits are set to dirty.
|
201 | 5832d1f2 | aliguori | *
|
202 | 5832d1f2 | aliguori | * @start_add: start of logged region. This is what we use to search the memslot
|
203 | 5832d1f2 | aliguori | * @end_addr: end of logged region.
|
204 | 5832d1f2 | aliguori | */
|
205 | 5832d1f2 | aliguori | void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_addr_t end_addr)
|
206 | 5832d1f2 | aliguori | { |
207 | 5832d1f2 | aliguori | KVMState *s = kvm_state; |
208 | 5832d1f2 | aliguori | KVMDirtyLog d; |
209 | 5832d1f2 | aliguori | KVMSlot *mem = kvm_lookup_slot(s, start_addr); |
210 | 5832d1f2 | aliguori | unsigned long alloc_size; |
211 | 5832d1f2 | aliguori | ram_addr_t addr; |
212 | 5832d1f2 | aliguori | target_phys_addr_t phys_addr = start_addr; |
213 | 5832d1f2 | aliguori | |
214 | 5832d1f2 | aliguori | dprintf("sync addr: %llx into %lx\n", start_addr, mem->phys_offset);
|
215 | 5832d1f2 | aliguori | if (mem == NULL) { |
216 | 5832d1f2 | aliguori | fprintf(stderr, "BUG: %s: invalid parameters\n", __func__);
|
217 | 5832d1f2 | aliguori | return;
|
218 | 5832d1f2 | aliguori | } |
219 | 5832d1f2 | aliguori | |
220 | 5832d1f2 | aliguori | alloc_size = mem->memory_size >> TARGET_PAGE_BITS / sizeof(d.dirty_bitmap);
|
221 | 5832d1f2 | aliguori | d.dirty_bitmap = qemu_mallocz(alloc_size); |
222 | 5832d1f2 | aliguori | |
223 | 5832d1f2 | aliguori | if (d.dirty_bitmap == NULL) { |
224 | 5832d1f2 | aliguori | dprintf("Could not allocate dirty bitmap\n");
|
225 | 5832d1f2 | aliguori | return;
|
226 | 5832d1f2 | aliguori | } |
227 | 5832d1f2 | aliguori | |
228 | 5832d1f2 | aliguori | d.slot = mem->slot; |
229 | 5832d1f2 | aliguori | dprintf("slot %d, phys_addr %llx, uaddr: %llx\n",
|
230 | 5832d1f2 | aliguori | d.slot, mem->start_addr, mem->phys_offset); |
231 | 5832d1f2 | aliguori | |
232 | 5832d1f2 | aliguori | if (kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d) == -1) { |
233 | 5832d1f2 | aliguori | dprintf("ioctl failed %d\n", errno);
|
234 | 5832d1f2 | aliguori | goto out;
|
235 | 5832d1f2 | aliguori | } |
236 | 5832d1f2 | aliguori | |
237 | 5832d1f2 | aliguori | phys_addr = start_addr; |
238 | 5832d1f2 | aliguori | for (addr = mem->phys_offset; phys_addr < end_addr; phys_addr+= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) {
|
239 | 5832d1f2 | aliguori | unsigned long *bitmap = (unsigned long *)d.dirty_bitmap; |
240 | 5832d1f2 | aliguori | unsigned nr = (phys_addr - start_addr) >> TARGET_PAGE_BITS;
|
241 | 5832d1f2 | aliguori | unsigned word = nr / (sizeof(*bitmap) * 8); |
242 | 5832d1f2 | aliguori | unsigned bit = nr % (sizeof(*bitmap) * 8); |
243 | 5832d1f2 | aliguori | if ((bitmap[word] >> bit) & 1) |
244 | 5832d1f2 | aliguori | cpu_physical_memory_set_dirty(addr); |
245 | 5832d1f2 | aliguori | } |
246 | 5832d1f2 | aliguori | out:
|
247 | 5832d1f2 | aliguori | qemu_free(d.dirty_bitmap); |
248 | 5832d1f2 | aliguori | } |
249 | 5832d1f2 | aliguori | |
250 | f65ed4c1 | aliguori | int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
|
251 | f65ed4c1 | aliguori | { |
252 | f65ed4c1 | aliguori | int ret = -ENOSYS;
|
253 | f65ed4c1 | aliguori | #ifdef KVM_CAP_COALESCED_MMIO
|
254 | f65ed4c1 | aliguori | KVMState *s = kvm_state; |
255 | f65ed4c1 | aliguori | |
256 | f65ed4c1 | aliguori | if (s->coalesced_mmio) {
|
257 | f65ed4c1 | aliguori | struct kvm_coalesced_mmio_zone zone;
|
258 | f65ed4c1 | aliguori | |
259 | f65ed4c1 | aliguori | zone.addr = start; |
260 | f65ed4c1 | aliguori | zone.size = size; |
261 | f65ed4c1 | aliguori | |
262 | f65ed4c1 | aliguori | ret = kvm_vm_ioctl(s, KVM_REGISTER_COALESCED_MMIO, &zone); |
263 | f65ed4c1 | aliguori | } |
264 | f65ed4c1 | aliguori | #endif
|
265 | f65ed4c1 | aliguori | |
266 | f65ed4c1 | aliguori | return ret;
|
267 | f65ed4c1 | aliguori | } |
268 | f65ed4c1 | aliguori | |
269 | f65ed4c1 | aliguori | int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size)
|
270 | f65ed4c1 | aliguori | { |
271 | f65ed4c1 | aliguori | int ret = -ENOSYS;
|
272 | f65ed4c1 | aliguori | #ifdef KVM_CAP_COALESCED_MMIO
|
273 | f65ed4c1 | aliguori | KVMState *s = kvm_state; |
274 | f65ed4c1 | aliguori | |
275 | f65ed4c1 | aliguori | if (s->coalesced_mmio) {
|
276 | f65ed4c1 | aliguori | struct kvm_coalesced_mmio_zone zone;
|
277 | f65ed4c1 | aliguori | |
278 | f65ed4c1 | aliguori | zone.addr = start; |
279 | f65ed4c1 | aliguori | zone.size = size; |
280 | f65ed4c1 | aliguori | |
281 | f65ed4c1 | aliguori | ret = kvm_vm_ioctl(s, KVM_UNREGISTER_COALESCED_MMIO, &zone); |
282 | f65ed4c1 | aliguori | } |
283 | f65ed4c1 | aliguori | #endif
|
284 | f65ed4c1 | aliguori | |
285 | f65ed4c1 | aliguori | return ret;
|
286 | f65ed4c1 | aliguori | } |
287 | f65ed4c1 | aliguori | |
288 | 05330448 | aliguori | int kvm_init(int smp_cpus) |
289 | 05330448 | aliguori | { |
290 | 05330448 | aliguori | KVMState *s; |
291 | 05330448 | aliguori | int ret;
|
292 | 05330448 | aliguori | int i;
|
293 | 05330448 | aliguori | |
294 | 05330448 | aliguori | if (smp_cpus > 1) |
295 | 05330448 | aliguori | return -EINVAL;
|
296 | 05330448 | aliguori | |
297 | 05330448 | aliguori | s = qemu_mallocz(sizeof(KVMState));
|
298 | 05330448 | aliguori | if (s == NULL) |
299 | 05330448 | aliguori | return -ENOMEM;
|
300 | 05330448 | aliguori | |
301 | 05330448 | aliguori | for (i = 0; i < ARRAY_SIZE(s->slots); i++) |
302 | 05330448 | aliguori | s->slots[i].slot = i; |
303 | 05330448 | aliguori | |
304 | 05330448 | aliguori | s->vmfd = -1;
|
305 | 05330448 | aliguori | s->fd = open("/dev/kvm", O_RDWR);
|
306 | 05330448 | aliguori | if (s->fd == -1) { |
307 | 05330448 | aliguori | fprintf(stderr, "Could not access KVM kernel module: %m\n");
|
308 | 05330448 | aliguori | ret = -errno; |
309 | 05330448 | aliguori | goto err;
|
310 | 05330448 | aliguori | } |
311 | 05330448 | aliguori | |
312 | 05330448 | aliguori | ret = kvm_ioctl(s, KVM_GET_API_VERSION, 0);
|
313 | 05330448 | aliguori | if (ret < KVM_API_VERSION) {
|
314 | 05330448 | aliguori | if (ret > 0) |
315 | 05330448 | aliguori | ret = -EINVAL; |
316 | 05330448 | aliguori | fprintf(stderr, "kvm version too old\n");
|
317 | 05330448 | aliguori | goto err;
|
318 | 05330448 | aliguori | } |
319 | 05330448 | aliguori | |
320 | 05330448 | aliguori | if (ret > KVM_API_VERSION) {
|
321 | 05330448 | aliguori | ret = -EINVAL; |
322 | 05330448 | aliguori | fprintf(stderr, "kvm version not supported\n");
|
323 | 05330448 | aliguori | goto err;
|
324 | 05330448 | aliguori | } |
325 | 05330448 | aliguori | |
326 | 05330448 | aliguori | s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0);
|
327 | 05330448 | aliguori | if (s->vmfd < 0) |
328 | 05330448 | aliguori | goto err;
|
329 | 05330448 | aliguori | |
330 | 05330448 | aliguori | /* initially, KVM allocated its own memory and we had to jump through
|
331 | 05330448 | aliguori | * hooks to make phys_ram_base point to this. Modern versions of KVM
|
332 | 05330448 | aliguori | * just use a user allocated buffer so we can use phys_ram_base
|
333 | 05330448 | aliguori | * unmodified. Make sure we have a sufficiently modern version of KVM.
|
334 | 05330448 | aliguori | */
|
335 | 984b5181 | aliguori | ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY); |
336 | 05330448 | aliguori | if (ret <= 0) { |
337 | 05330448 | aliguori | if (ret == 0) |
338 | 05330448 | aliguori | ret = -EINVAL; |
339 | 05330448 | aliguori | fprintf(stderr, "kvm does not support KVM_CAP_USER_MEMORY\n");
|
340 | 05330448 | aliguori | goto err;
|
341 | 05330448 | aliguori | } |
342 | 05330448 | aliguori | |
343 | d85dc283 | aliguori | /* There was a nasty bug in < kvm-80 that prevents memory slots from being
|
344 | d85dc283 | aliguori | * destroyed properly. Since we rely on this capability, refuse to work
|
345 | d85dc283 | aliguori | * with any kernel without this capability. */
|
346 | d85dc283 | aliguori | ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, |
347 | d85dc283 | aliguori | KVM_CAP_DESTROY_MEMORY_REGION_WORKS); |
348 | d85dc283 | aliguori | if (ret <= 0) { |
349 | d85dc283 | aliguori | if (ret == 0) |
350 | d85dc283 | aliguori | ret = -EINVAL; |
351 | d85dc283 | aliguori | |
352 | d85dc283 | aliguori | fprintf(stderr, |
353 | d85dc283 | aliguori | "KVM kernel module broken (DESTROY_MEMORY_REGION)\n"
|
354 | d85dc283 | aliguori | "Please upgrade to at least kvm-81.\n");
|
355 | d85dc283 | aliguori | goto err;
|
356 | d85dc283 | aliguori | } |
357 | d85dc283 | aliguori | |
358 | f65ed4c1 | aliguori | s->coalesced_mmio = 0;
|
359 | f65ed4c1 | aliguori | #ifdef KVM_CAP_COALESCED_MMIO
|
360 | f65ed4c1 | aliguori | ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_COALESCED_MMIO); |
361 | f65ed4c1 | aliguori | if (ret > 0) |
362 | f65ed4c1 | aliguori | s->coalesced_mmio = ret; |
363 | f65ed4c1 | aliguori | #endif
|
364 | f65ed4c1 | aliguori | |
365 | 05330448 | aliguori | ret = kvm_arch_init(s, smp_cpus); |
366 | 05330448 | aliguori | if (ret < 0) |
367 | 05330448 | aliguori | goto err;
|
368 | 05330448 | aliguori | |
369 | 05330448 | aliguori | kvm_state = s; |
370 | 05330448 | aliguori | |
371 | 05330448 | aliguori | return 0; |
372 | 05330448 | aliguori | |
373 | 05330448 | aliguori | err:
|
374 | 05330448 | aliguori | if (s) {
|
375 | 05330448 | aliguori | if (s->vmfd != -1) |
376 | 05330448 | aliguori | close(s->vmfd); |
377 | 05330448 | aliguori | if (s->fd != -1) |
378 | 05330448 | aliguori | close(s->fd); |
379 | 05330448 | aliguori | } |
380 | 05330448 | aliguori | qemu_free(s); |
381 | 05330448 | aliguori | |
382 | 05330448 | aliguori | return ret;
|
383 | 05330448 | aliguori | } |
384 | 05330448 | aliguori | |
385 | 05330448 | aliguori | static int kvm_handle_io(CPUState *env, uint16_t port, void *data, |
386 | 05330448 | aliguori | int direction, int size, uint32_t count) |
387 | 05330448 | aliguori | { |
388 | 05330448 | aliguori | int i;
|
389 | 05330448 | aliguori | uint8_t *ptr = data; |
390 | 05330448 | aliguori | |
391 | 05330448 | aliguori | for (i = 0; i < count; i++) { |
392 | 05330448 | aliguori | if (direction == KVM_EXIT_IO_IN) {
|
393 | 05330448 | aliguori | switch (size) {
|
394 | 05330448 | aliguori | case 1: |
395 | 05330448 | aliguori | stb_p(ptr, cpu_inb(env, port)); |
396 | 05330448 | aliguori | break;
|
397 | 05330448 | aliguori | case 2: |
398 | 05330448 | aliguori | stw_p(ptr, cpu_inw(env, port)); |
399 | 05330448 | aliguori | break;
|
400 | 05330448 | aliguori | case 4: |
401 | 05330448 | aliguori | stl_p(ptr, cpu_inl(env, port)); |
402 | 05330448 | aliguori | break;
|
403 | 05330448 | aliguori | } |
404 | 05330448 | aliguori | } else {
|
405 | 05330448 | aliguori | switch (size) {
|
406 | 05330448 | aliguori | case 1: |
407 | 05330448 | aliguori | cpu_outb(env, port, ldub_p(ptr)); |
408 | 05330448 | aliguori | break;
|
409 | 05330448 | aliguori | case 2: |
410 | 05330448 | aliguori | cpu_outw(env, port, lduw_p(ptr)); |
411 | 05330448 | aliguori | break;
|
412 | 05330448 | aliguori | case 4: |
413 | 05330448 | aliguori | cpu_outl(env, port, ldl_p(ptr)); |
414 | 05330448 | aliguori | break;
|
415 | 05330448 | aliguori | } |
416 | 05330448 | aliguori | } |
417 | 05330448 | aliguori | |
418 | 05330448 | aliguori | ptr += size; |
419 | 05330448 | aliguori | } |
420 | 05330448 | aliguori | |
421 | 05330448 | aliguori | return 1; |
422 | 05330448 | aliguori | } |
423 | 05330448 | aliguori | |
424 | f65ed4c1 | aliguori | static void kvm_run_coalesced_mmio(CPUState *env, struct kvm_run *run) |
425 | f65ed4c1 | aliguori | { |
426 | f65ed4c1 | aliguori | #ifdef KVM_CAP_COALESCED_MMIO
|
427 | f65ed4c1 | aliguori | KVMState *s = kvm_state; |
428 | f65ed4c1 | aliguori | if (s->coalesced_mmio) {
|
429 | f65ed4c1 | aliguori | struct kvm_coalesced_mmio_ring *ring;
|
430 | f65ed4c1 | aliguori | |
431 | f65ed4c1 | aliguori | ring = (void *)run + (s->coalesced_mmio * TARGET_PAGE_SIZE);
|
432 | f65ed4c1 | aliguori | while (ring->first != ring->last) {
|
433 | f65ed4c1 | aliguori | struct kvm_coalesced_mmio *ent;
|
434 | f65ed4c1 | aliguori | |
435 | f65ed4c1 | aliguori | ent = &ring->coalesced_mmio[ring->first]; |
436 | f65ed4c1 | aliguori | |
437 | f65ed4c1 | aliguori | cpu_physical_memory_write(ent->phys_addr, ent->data, ent->len); |
438 | f65ed4c1 | aliguori | /* FIXME smp_wmb() */
|
439 | f65ed4c1 | aliguori | ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
|
440 | f65ed4c1 | aliguori | } |
441 | f65ed4c1 | aliguori | } |
442 | f65ed4c1 | aliguori | #endif
|
443 | f65ed4c1 | aliguori | } |
444 | f65ed4c1 | aliguori | |
445 | 05330448 | aliguori | int kvm_cpu_exec(CPUState *env)
|
446 | 05330448 | aliguori | { |
447 | 05330448 | aliguori | struct kvm_run *run = env->kvm_run;
|
448 | 05330448 | aliguori | int ret;
|
449 | 05330448 | aliguori | |
450 | 05330448 | aliguori | dprintf("kvm_cpu_exec()\n");
|
451 | 05330448 | aliguori | |
452 | 05330448 | aliguori | do {
|
453 | 05330448 | aliguori | kvm_arch_pre_run(env, run); |
454 | 05330448 | aliguori | |
455 | 05330448 | aliguori | if ((env->interrupt_request & CPU_INTERRUPT_EXIT)) {
|
456 | 05330448 | aliguori | dprintf("interrupt exit requested\n");
|
457 | 05330448 | aliguori | ret = 0;
|
458 | 05330448 | aliguori | break;
|
459 | 05330448 | aliguori | } |
460 | 05330448 | aliguori | |
461 | 05330448 | aliguori | ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
|
462 | 05330448 | aliguori | kvm_arch_post_run(env, run); |
463 | 05330448 | aliguori | |
464 | 05330448 | aliguori | if (ret == -EINTR || ret == -EAGAIN) {
|
465 | 05330448 | aliguori | dprintf("io window exit\n");
|
466 | 05330448 | aliguori | ret = 0;
|
467 | 05330448 | aliguori | break;
|
468 | 05330448 | aliguori | } |
469 | 05330448 | aliguori | |
470 | 05330448 | aliguori | if (ret < 0) { |
471 | 05330448 | aliguori | dprintf("kvm run failed %s\n", strerror(-ret));
|
472 | 05330448 | aliguori | abort(); |
473 | 05330448 | aliguori | } |
474 | 05330448 | aliguori | |
475 | f65ed4c1 | aliguori | kvm_run_coalesced_mmio(env, run); |
476 | f65ed4c1 | aliguori | |
477 | 05330448 | aliguori | ret = 0; /* exit loop */ |
478 | 05330448 | aliguori | switch (run->exit_reason) {
|
479 | 05330448 | aliguori | case KVM_EXIT_IO:
|
480 | 05330448 | aliguori | dprintf("handle_io\n");
|
481 | 05330448 | aliguori | ret = kvm_handle_io(env, run->io.port, |
482 | 05330448 | aliguori | (uint8_t *)run + run->io.data_offset, |
483 | 05330448 | aliguori | run->io.direction, |
484 | 05330448 | aliguori | run->io.size, |
485 | 05330448 | aliguori | run->io.count); |
486 | 05330448 | aliguori | break;
|
487 | 05330448 | aliguori | case KVM_EXIT_MMIO:
|
488 | 05330448 | aliguori | dprintf("handle_mmio\n");
|
489 | 05330448 | aliguori | cpu_physical_memory_rw(run->mmio.phys_addr, |
490 | 05330448 | aliguori | run->mmio.data, |
491 | 05330448 | aliguori | run->mmio.len, |
492 | 05330448 | aliguori | run->mmio.is_write); |
493 | 05330448 | aliguori | ret = 1;
|
494 | 05330448 | aliguori | break;
|
495 | 05330448 | aliguori | case KVM_EXIT_IRQ_WINDOW_OPEN:
|
496 | 05330448 | aliguori | dprintf("irq_window_open\n");
|
497 | 05330448 | aliguori | break;
|
498 | 05330448 | aliguori | case KVM_EXIT_SHUTDOWN:
|
499 | 05330448 | aliguori | dprintf("shutdown\n");
|
500 | 05330448 | aliguori | qemu_system_reset_request(); |
501 | 05330448 | aliguori | ret = 1;
|
502 | 05330448 | aliguori | break;
|
503 | 05330448 | aliguori | case KVM_EXIT_UNKNOWN:
|
504 | 05330448 | aliguori | dprintf("kvm_exit_unknown\n");
|
505 | 05330448 | aliguori | break;
|
506 | 05330448 | aliguori | case KVM_EXIT_FAIL_ENTRY:
|
507 | 05330448 | aliguori | dprintf("kvm_exit_fail_entry\n");
|
508 | 05330448 | aliguori | break;
|
509 | 05330448 | aliguori | case KVM_EXIT_EXCEPTION:
|
510 | 05330448 | aliguori | dprintf("kvm_exit_exception\n");
|
511 | 05330448 | aliguori | break;
|
512 | 05330448 | aliguori | case KVM_EXIT_DEBUG:
|
513 | 05330448 | aliguori | dprintf("kvm_exit_debug\n");
|
514 | 05330448 | aliguori | break;
|
515 | 05330448 | aliguori | default:
|
516 | 05330448 | aliguori | dprintf("kvm_arch_handle_exit\n");
|
517 | 05330448 | aliguori | ret = kvm_arch_handle_exit(env, run); |
518 | 05330448 | aliguori | break;
|
519 | 05330448 | aliguori | } |
520 | 05330448 | aliguori | } while (ret > 0); |
521 | 05330448 | aliguori | |
522 | becfc390 | aliguori | if ((env->interrupt_request & CPU_INTERRUPT_EXIT)) {
|
523 | becfc390 | aliguori | env->interrupt_request &= ~CPU_INTERRUPT_EXIT; |
524 | becfc390 | aliguori | env->exception_index = EXCP_INTERRUPT; |
525 | becfc390 | aliguori | } |
526 | becfc390 | aliguori | |
527 | 05330448 | aliguori | return ret;
|
528 | 05330448 | aliguori | } |
529 | 05330448 | aliguori | |
530 | 05330448 | aliguori | void kvm_set_phys_mem(target_phys_addr_t start_addr,
|
531 | 05330448 | aliguori | ram_addr_t size, |
532 | 05330448 | aliguori | ram_addr_t phys_offset) |
533 | 05330448 | aliguori | { |
534 | 05330448 | aliguori | KVMState *s = kvm_state; |
535 | 05330448 | aliguori | ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK; |
536 | 05330448 | aliguori | KVMSlot *mem; |
537 | 05330448 | aliguori | |
538 | 05330448 | aliguori | /* KVM does not support read-only slots */
|
539 | 05330448 | aliguori | phys_offset &= ~IO_MEM_ROM; |
540 | 05330448 | aliguori | |
541 | 05330448 | aliguori | mem = kvm_lookup_slot(s, start_addr); |
542 | 05330448 | aliguori | if (mem) {
|
543 | a3d6841f | aliguori | if ((flags == IO_MEM_UNASSIGNED) || (flags >= TLB_MMIO)) {
|
544 | 05330448 | aliguori | mem->memory_size = 0;
|
545 | 34fc643f | aliguori | mem->start_addr = start_addr; |
546 | 34fc643f | aliguori | mem->phys_offset = 0;
|
547 | 05330448 | aliguori | mem->flags = 0;
|
548 | 05330448 | aliguori | |
549 | 34fc643f | aliguori | kvm_set_user_memory_region(s, mem); |
550 | 34fc643f | aliguori | } else if (start_addr >= mem->start_addr && |
551 | 34fc643f | aliguori | (start_addr + size) <= (mem->start_addr + |
552 | 62d60e8c | aliguori | mem->memory_size)) { |
553 | 62d60e8c | aliguori | KVMSlot slot; |
554 | 62d60e8c | aliguori | target_phys_addr_t mem_start; |
555 | 62d60e8c | aliguori | ram_addr_t mem_size, mem_offset; |
556 | 62d60e8c | aliguori | |
557 | 62d60e8c | aliguori | /* Not splitting */
|
558 | 34fc643f | aliguori | if ((phys_offset - (start_addr - mem->start_addr)) ==
|
559 | 34fc643f | aliguori | mem->phys_offset) |
560 | 62d60e8c | aliguori | return;
|
561 | 62d60e8c | aliguori | |
562 | 62d60e8c | aliguori | /* unregister whole slot */
|
563 | 62d60e8c | aliguori | memcpy(&slot, mem, sizeof(slot));
|
564 | 62d60e8c | aliguori | mem->memory_size = 0;
|
565 | 34fc643f | aliguori | kvm_set_user_memory_region(s, mem); |
566 | 62d60e8c | aliguori | |
567 | 62d60e8c | aliguori | /* register prefix slot */
|
568 | 34fc643f | aliguori | mem_start = slot.start_addr; |
569 | 34fc643f | aliguori | mem_size = start_addr - slot.start_addr; |
570 | 34fc643f | aliguori | mem_offset = slot.phys_offset; |
571 | 62d60e8c | aliguori | if (mem_size)
|
572 | 62d60e8c | aliguori | kvm_set_phys_mem(mem_start, mem_size, mem_offset); |
573 | 62d60e8c | aliguori | |
574 | 62d60e8c | aliguori | /* register new slot */
|
575 | 62d60e8c | aliguori | kvm_set_phys_mem(start_addr, size, phys_offset); |
576 | 62d60e8c | aliguori | |
577 | 62d60e8c | aliguori | /* register suffix slot */
|
578 | 62d60e8c | aliguori | mem_start = start_addr + size; |
579 | 62d60e8c | aliguori | mem_offset += mem_size + size; |
580 | 62d60e8c | aliguori | mem_size = slot.memory_size - mem_size - size; |
581 | 62d60e8c | aliguori | if (mem_size)
|
582 | 62d60e8c | aliguori | kvm_set_phys_mem(mem_start, mem_size, mem_offset); |
583 | 62d60e8c | aliguori | |
584 | 05330448 | aliguori | return;
|
585 | 62d60e8c | aliguori | } else {
|
586 | 62d60e8c | aliguori | printf("Registering overlapping slot\n");
|
587 | 62d60e8c | aliguori | abort(); |
588 | 62d60e8c | aliguori | } |
589 | 05330448 | aliguori | } |
590 | 05330448 | aliguori | /* KVM does not need to know about this memory */
|
591 | 05330448 | aliguori | if (flags >= IO_MEM_UNASSIGNED)
|
592 | 05330448 | aliguori | return;
|
593 | 05330448 | aliguori | |
594 | 05330448 | aliguori | mem = kvm_alloc_slot(s); |
595 | 05330448 | aliguori | mem->memory_size = size; |
596 | 34fc643f | aliguori | mem->start_addr = start_addr; |
597 | 34fc643f | aliguori | mem->phys_offset = phys_offset; |
598 | 05330448 | aliguori | mem->flags = 0;
|
599 | 05330448 | aliguori | |
600 | 34fc643f | aliguori | kvm_set_user_memory_region(s, mem); |
601 | 05330448 | aliguori | /* FIXME deal with errors */
|
602 | 05330448 | aliguori | } |
603 | 05330448 | aliguori | |
604 | 984b5181 | aliguori | int kvm_ioctl(KVMState *s, int type, ...) |
605 | 05330448 | aliguori | { |
606 | 05330448 | aliguori | int ret;
|
607 | 984b5181 | aliguori | void *arg;
|
608 | 984b5181 | aliguori | va_list ap; |
609 | 05330448 | aliguori | |
610 | 984b5181 | aliguori | va_start(ap, type); |
611 | 984b5181 | aliguori | arg = va_arg(ap, void *);
|
612 | 984b5181 | aliguori | va_end(ap); |
613 | 984b5181 | aliguori | |
614 | 984b5181 | aliguori | ret = ioctl(s->fd, type, arg); |
615 | 05330448 | aliguori | if (ret == -1) |
616 | 05330448 | aliguori | ret = -errno; |
617 | 05330448 | aliguori | |
618 | 05330448 | aliguori | return ret;
|
619 | 05330448 | aliguori | } |
620 | 05330448 | aliguori | |
621 | 984b5181 | aliguori | int kvm_vm_ioctl(KVMState *s, int type, ...) |
622 | 05330448 | aliguori | { |
623 | 05330448 | aliguori | int ret;
|
624 | 984b5181 | aliguori | void *arg;
|
625 | 984b5181 | aliguori | va_list ap; |
626 | 984b5181 | aliguori | |
627 | 984b5181 | aliguori | va_start(ap, type); |
628 | 984b5181 | aliguori | arg = va_arg(ap, void *);
|
629 | 984b5181 | aliguori | va_end(ap); |
630 | 05330448 | aliguori | |
631 | 984b5181 | aliguori | ret = ioctl(s->vmfd, type, arg); |
632 | 05330448 | aliguori | if (ret == -1) |
633 | 05330448 | aliguori | ret = -errno; |
634 | 05330448 | aliguori | |
635 | 05330448 | aliguori | return ret;
|
636 | 05330448 | aliguori | } |
637 | 05330448 | aliguori | |
638 | 984b5181 | aliguori | int kvm_vcpu_ioctl(CPUState *env, int type, ...) |
639 | 05330448 | aliguori | { |
640 | 05330448 | aliguori | int ret;
|
641 | 984b5181 | aliguori | void *arg;
|
642 | 984b5181 | aliguori | va_list ap; |
643 | 984b5181 | aliguori | |
644 | 984b5181 | aliguori | va_start(ap, type); |
645 | 984b5181 | aliguori | arg = va_arg(ap, void *);
|
646 | 984b5181 | aliguori | va_end(ap); |
647 | 05330448 | aliguori | |
648 | 984b5181 | aliguori | ret = ioctl(env->kvm_fd, type, arg); |
649 | 05330448 | aliguori | if (ret == -1) |
650 | 05330448 | aliguori | ret = -errno; |
651 | 05330448 | aliguori | |
652 | 05330448 | aliguori | return ret;
|
653 | 05330448 | aliguori | } |
654 | bd322087 | aliguori | |
655 | bd322087 | aliguori | int kvm_has_sync_mmu(void) |
656 | bd322087 | aliguori | { |
657 | a9c11522 | aurel32 | #ifdef KVM_CAP_SYNC_MMU
|
658 | bd322087 | aliguori | KVMState *s = kvm_state; |
659 | bd322087 | aliguori | |
660 | bd322087 | aliguori | if (kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SYNC_MMU) > 0) |
661 | bd322087 | aliguori | return 1; |
662 | bd322087 | aliguori | #endif
|
663 | bd322087 | aliguori | |
664 | bd322087 | aliguori | return 0; |
665 | bd322087 | aliguori | } |