4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include <sys/types.h>
15 #include <sys/ioctl.h>
19 #include <linux/kvm.h>
21 #include "qemu-common.h"
28 #define dprintf(fmt, ...) \
29 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
31 #define dprintf(fmt, ...) \
35 typedef struct KVMSlot
37 target_phys_addr_t start_addr
;
38 ram_addr_t memory_size
;
39 ram_addr_t phys_offset
;
53 static KVMState
*kvm_state
;
55 static KVMSlot
*kvm_alloc_slot(KVMState
*s
)
59 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
60 /* KVM private memory slots */
63 if (s
->slots
[i
].memory_size
== 0)
70 static KVMSlot
*kvm_lookup_slot(KVMState
*s
, target_phys_addr_t start_addr
)
74 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++) {
75 KVMSlot
*mem
= &s
->slots
[i
];
77 if (start_addr
>= mem
->start_addr
&&
78 start_addr
< (mem
->start_addr
+ mem
->memory_size
))
85 int kvm_init_vcpu(CPUState
*env
)
87 KVMState
*s
= kvm_state
;
91 dprintf("kvm_init_vcpu\n");
93 ret
= kvm_vm_ioctl(s
, KVM_CREATE_VCPU
, env
->cpu_index
);
95 dprintf("kvm_create_vcpu failed\n");
102 mmap_size
= kvm_ioctl(s
, KVM_GET_VCPU_MMAP_SIZE
, 0);
104 dprintf("KVM_GET_VCPU_MMAP_SIZE failed\n");
108 env
->kvm_run
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
110 if (env
->kvm_run
== MAP_FAILED
) {
112 dprintf("mmap'ing vcpu state failed\n");
116 ret
= kvm_arch_init_vcpu(env
);
122 int kvm_init(int smp_cpus
)
131 s
= qemu_mallocz(sizeof(KVMState
));
135 for (i
= 0; i
< ARRAY_SIZE(s
->slots
); i
++)
136 s
->slots
[i
].slot
= i
;
139 s
->fd
= open("/dev/kvm", O_RDWR
);
141 fprintf(stderr
, "Could not access KVM kernel module: %m\n");
146 ret
= kvm_ioctl(s
, KVM_GET_API_VERSION
, 0);
147 if (ret
< KVM_API_VERSION
) {
150 fprintf(stderr
, "kvm version too old\n");
154 if (ret
> KVM_API_VERSION
) {
156 fprintf(stderr
, "kvm version not supported\n");
160 s
->vmfd
= kvm_ioctl(s
, KVM_CREATE_VM
, 0);
164 /* initially, KVM allocated its own memory and we had to jump through
165 * hooks to make phys_ram_base point to this. Modern versions of KVM
166 * just use a user allocated buffer so we can use phys_ram_base
167 * unmodified. Make sure we have a sufficiently modern version of KVM.
169 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_USER_MEMORY
);
173 fprintf(stderr
, "kvm does not support KVM_CAP_USER_MEMORY\n");
177 ret
= kvm_arch_init(s
, smp_cpus
);
197 static int kvm_handle_io(CPUState
*env
, uint16_t port
, void *data
,
198 int direction
, int size
, uint32_t count
)
203 for (i
= 0; i
< count
; i
++) {
204 if (direction
== KVM_EXIT_IO_IN
) {
207 stb_p(ptr
, cpu_inb(env
, port
));
210 stw_p(ptr
, cpu_inw(env
, port
));
213 stl_p(ptr
, cpu_inl(env
, port
));
219 cpu_outb(env
, port
, ldub_p(ptr
));
222 cpu_outw(env
, port
, lduw_p(ptr
));
225 cpu_outl(env
, port
, ldl_p(ptr
));
236 int kvm_cpu_exec(CPUState
*env
)
238 struct kvm_run
*run
= env
->kvm_run
;
241 dprintf("kvm_cpu_exec()\n");
244 kvm_arch_pre_run(env
, run
);
246 if ((env
->interrupt_request
& CPU_INTERRUPT_EXIT
)) {
247 dprintf("interrupt exit requested\n");
252 ret
= kvm_vcpu_ioctl(env
, KVM_RUN
, 0);
253 kvm_arch_post_run(env
, run
);
255 if (ret
== -EINTR
|| ret
== -EAGAIN
) {
256 dprintf("io window exit\n");
262 dprintf("kvm run failed %s\n", strerror(-ret
));
266 ret
= 0; /* exit loop */
267 switch (run
->exit_reason
) {
269 dprintf("handle_io\n");
270 ret
= kvm_handle_io(env
, run
->io
.port
,
271 (uint8_t *)run
+ run
->io
.data_offset
,
277 dprintf("handle_mmio\n");
278 cpu_physical_memory_rw(run
->mmio
.phys_addr
,
284 case KVM_EXIT_IRQ_WINDOW_OPEN
:
285 dprintf("irq_window_open\n");
287 case KVM_EXIT_SHUTDOWN
:
288 dprintf("shutdown\n");
289 qemu_system_reset_request();
292 case KVM_EXIT_UNKNOWN
:
293 dprintf("kvm_exit_unknown\n");
295 case KVM_EXIT_FAIL_ENTRY
:
296 dprintf("kvm_exit_fail_entry\n");
298 case KVM_EXIT_EXCEPTION
:
299 dprintf("kvm_exit_exception\n");
302 dprintf("kvm_exit_debug\n");
305 dprintf("kvm_arch_handle_exit\n");
306 ret
= kvm_arch_handle_exit(env
, run
);
311 if ((env
->interrupt_request
& CPU_INTERRUPT_EXIT
)) {
312 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
313 env
->exception_index
= EXCP_INTERRUPT
;
319 static int kvm_set_user_memory_region(KVMState
*s
, KVMSlot
*slot
)
321 struct kvm_userspace_memory_region mem
;
323 mem
.slot
= slot
->slot
;
324 mem
.guest_phys_addr
= slot
->start_addr
;
325 mem
.memory_size
= slot
->memory_size
;
326 mem
.userspace_addr
= (unsigned long)phys_ram_base
+ slot
->phys_offset
;
327 mem
.flags
= slot
->flags
;
329 return kvm_vm_ioctl(s
, KVM_SET_USER_MEMORY_REGION
, &mem
);
332 void kvm_set_phys_mem(target_phys_addr_t start_addr
,
334 ram_addr_t phys_offset
)
336 KVMState
*s
= kvm_state
;
337 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
340 /* KVM does not support read-only slots */
341 phys_offset
&= ~IO_MEM_ROM
;
343 mem
= kvm_lookup_slot(s
, start_addr
);
345 if ((flags
== IO_MEM_UNASSIGNED
) || (flags
>= TLB_MMIO
)) {
346 mem
->memory_size
= 0;
347 mem
->start_addr
= start_addr
;
348 mem
->phys_offset
= 0;
351 kvm_set_user_memory_region(s
, mem
);
352 } else if (start_addr
>= mem
->start_addr
&&
353 (start_addr
+ size
) <= (mem
->start_addr
+
356 target_phys_addr_t mem_start
;
357 ram_addr_t mem_size
, mem_offset
;
360 if ((phys_offset
- (start_addr
- mem
->start_addr
)) ==
364 /* unregister whole slot */
365 memcpy(&slot
, mem
, sizeof(slot
));
366 mem
->memory_size
= 0;
367 kvm_set_user_memory_region(s
, mem
);
369 /* register prefix slot */
370 mem_start
= slot
.start_addr
;
371 mem_size
= start_addr
- slot
.start_addr
;
372 mem_offset
= slot
.phys_offset
;
374 kvm_set_phys_mem(mem_start
, mem_size
, mem_offset
);
376 /* register new slot */
377 kvm_set_phys_mem(start_addr
, size
, phys_offset
);
379 /* register suffix slot */
380 mem_start
= start_addr
+ size
;
381 mem_offset
+= mem_size
+ size
;
382 mem_size
= slot
.memory_size
- mem_size
- size
;
384 kvm_set_phys_mem(mem_start
, mem_size
, mem_offset
);
388 printf("Registering overlapping slot\n");
392 /* KVM does not need to know about this memory */
393 if (flags
>= IO_MEM_UNASSIGNED
)
396 mem
= kvm_alloc_slot(s
);
397 mem
->memory_size
= size
;
398 mem
->start_addr
= start_addr
;
399 mem
->phys_offset
= phys_offset
;
402 kvm_set_user_memory_region(s
, mem
);
403 /* FIXME deal with errors */
406 int kvm_ioctl(KVMState
*s
, int type
, ...)
413 arg
= va_arg(ap
, void *);
416 ret
= ioctl(s
->fd
, type
, arg
);
423 int kvm_vm_ioctl(KVMState
*s
, int type
, ...)
430 arg
= va_arg(ap
, void *);
433 ret
= ioctl(s
->vmfd
, type
, arg
);
440 int kvm_vcpu_ioctl(CPUState
*env
, int type
, ...)
447 arg
= va_arg(ap
, void *);
450 ret
= ioctl(env
->kvm_fd
, type
, arg
);