test: add apic_id() accessor
[qemu/qemu-dev-zwu.git] / qemu-kvm.h
blob0091a16abefe0dd3d16f784bc2566a621a4161a2
1 /*
2 * qemu/kvm integration
4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
7 */
8 #ifndef THE_ORIGINAL_AND_TRUE_QEMU_KVM_H
9 #define THE_ORIGINAL_AND_TRUE_QEMU_KVM_H
11 #include "cpu.h"
13 #include <signal.h>
15 #ifdef CONFIG_KVM
17 #if defined(__s390__)
18 #include <asm/ptrace.h>
19 #endif
21 #include <stdint.h>
23 #ifndef __user
24 #define __user /* temporary, until installed via make headers_install */
25 #endif
27 #include <linux/kvm.h>
29 #include <signal.h>
31 /* FIXME: share this number with kvm */
32 /* FIXME: or dynamically alloc/realloc regions */
33 #ifdef __s390__
34 #define KVM_MAX_NUM_MEM_REGIONS 1u
35 #define MAX_VCPUS 64
36 #define LIBKVM_S390_ORIGIN (0UL)
37 #elif defined(__ia64__)
38 #define KVM_MAX_NUM_MEM_REGIONS 32u
39 #define MAX_VCPUS 256
40 #else
41 #define KVM_MAX_NUM_MEM_REGIONS 32u
42 #define MAX_VCPUS 16
43 #endif
45 /* kvm abi verison variable */
46 extern int kvm_abi;
48 /**
49 * \brief The KVM context
51 * The verbose KVM context
54 struct kvm_context {
55 void *opaque;
56 /// is dirty pages logging enabled for all regions or not
57 int dirty_pages_log_all;
58 /// do not create in-kernel irqchip if set
59 int no_irqchip_creation;
60 /// in-kernel irqchip status
61 int irqchip_in_kernel;
62 /// ioctl to use to inject interrupts
63 int irqchip_inject_ioctl;
64 /// do not create in-kernel pit if set
65 int no_pit_creation;
66 /// in-kernel pit status
67 int pit_in_kernel;
68 #ifdef KVM_CAP_IRQ_ROUTING
69 struct kvm_irq_routing *irq_routes;
70 int nr_allocated_irq_routes;
71 #endif
72 void *used_gsi_bitmap;
73 int max_gsi;
76 struct kvm_vcpu_context {
77 int fd;
78 struct kvm_run *run;
79 struct kvm_context *kvm;
80 uint32_t id;
83 typedef struct kvm_context *kvm_context_t;
84 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
86 #include "kvm.h"
87 int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
88 void **vm_mem);
89 int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
90 void **vm_mem);
92 int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
93 void **vm_mem);
94 int kvm_arch_run(kvm_vcpu_context_t vcpu);
97 void kvm_show_code(kvm_vcpu_context_t vcpu);
99 int handle_halt(kvm_vcpu_context_t vcpu);
100 int handle_shutdown(kvm_context_t kvm, CPUState *env);
101 void post_kvm_run(kvm_context_t kvm, CPUState *env);
102 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
103 int handle_io_window(kvm_context_t kvm);
104 int handle_debug(kvm_vcpu_context_t vcpu, void *env);
105 int try_push_interrupts(kvm_context_t kvm);
107 #if defined(__x86_64__) || defined(__i386__)
108 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
109 int kvm_get_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
110 int kvm_set_msrs(kvm_vcpu_context_t, struct kvm_msr_entry *msrs, int n);
111 int kvm_get_mce_cap_supported(kvm_context_t, uint64_t *mce_cap,
112 int *max_banks);
113 int kvm_setup_mce(kvm_vcpu_context_t vcpu, uint64_t *mcg_cap);
114 struct kvm_x86_mce;
115 int kvm_set_mce(kvm_vcpu_context_t vcpu, struct kvm_x86_mce *mce);
116 #endif
119 * \brief Create new KVM context
121 * This creates a new kvm_context. A KVM context is a small area of data that
122 * holds information about the KVM instance that gets created by this call.\n
123 * This should always be your first call to KVM.
125 * \param opaque Not used
126 * \return NULL on failure
128 int kvm_init(int smp_cpus);
131 * \brief Disable the in-kernel IRQCHIP creation
133 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
134 * this should be called prior to kvm_create().
136 * \param kvm Pointer to the kvm_context
138 void kvm_disable_irqchip_creation(kvm_context_t kvm);
141 * \brief Disable the in-kernel PIT creation
143 * In-kernel pit is enabled by default. If userspace pit is to be used,
144 * this should be called prior to kvm_create().
146 * \param kvm Pointer to the kvm_context
148 void kvm_disable_pit_creation(kvm_context_t kvm);
151 * \brief Create new virtual machine
153 * This creates a new virtual machine, maps physical RAM to it, and creates a
154 * virtual CPU for it.\n
155 * \n
156 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
158 * \param kvm Pointer to the current kvm_context
159 * \param phys_mem_bytes The amount of physical ram you want the VM to have
160 * \param phys_mem This pointer will be set to point to the memory that
161 * kvm_create allocates for physical RAM
162 * \return 0 on success
164 int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
165 void **phys_mem);
166 int kvm_create_vm(kvm_context_t kvm);
167 void kvm_create_irqchip(kvm_context_t kvm);
170 * \brief Create a new virtual cpu
172 * This creates a new virtual cpu (the first vcpu is created by kvm_create()).
173 * Should be called from a thread dedicated to the vcpu.
175 * \param kvm kvm context
176 * \param slot vcpu number (> 0)
177 * \return 0 on success, -errno on failure
179 kvm_vcpu_context_t kvm_create_vcpu(CPUState *env, int id);
182 * \brief Start the VCPU
184 * This starts the VCPU and virtualization is started.\n
185 * \n
186 * This function will not return until any of these conditions are met:
187 * - An IO/MMIO handler does not return "0"
188 * - An exception that neither the guest OS, nor KVM can handle occurs
190 * \note This function will call the callbacks registered in kvm_init()
191 * to emulate those functions
192 * \note If you at any point want to interrupt the VCPU, kvm_run() will
193 * listen to the EINTR signal. This allows you to simulate external interrupts
194 * and asyncronous IO.
196 * \param kvm Pointer to the current kvm_context
197 * \param vcpu Which virtual CPU should be started
198 * \return 0 on success, but you really shouldn't expect this function to
199 * return except for when an error has occured, or when you have sent it
200 * an EINTR signal.
202 int kvm_run(kvm_vcpu_context_t vcpu, void *env);
205 * \brief Get interrupt flag from on last exit to userspace
207 * This gets the CPU interrupt flag as it was on the last exit to userspace.
209 * \param kvm Pointer to the current kvm_context
210 * \param vcpu Which virtual CPU should get dumped
211 * \return interrupt flag value (0 or 1)
213 int kvm_get_interrupt_flag(kvm_vcpu_context_t vcpu);
216 * \brief Get the value of the APIC_BASE msr as of last exit to userspace
218 * This gets the APIC_BASE msr as it was on the last exit to userspace.
220 * \param kvm Pointer to the current kvm_context
221 * \param vcpu Which virtual CPU should get dumped
222 * \return APIC_BASE msr contents
224 uint64_t kvm_get_apic_base(kvm_vcpu_context_t vcpu);
227 * \brief Check if a vcpu is ready for interrupt injection
229 * This checks if vcpu interrupts are not masked by mov ss or sti.
231 * \param kvm Pointer to the current kvm_context
232 * \param vcpu Which virtual CPU should get dumped
233 * \return boolean indicating interrupt injection readiness
235 int kvm_is_ready_for_interrupt_injection(kvm_vcpu_context_t vcpu);
238 * \brief Read VCPU registers
240 * This gets the GP registers from the VCPU and outputs them
241 * into a kvm_regs structure
243 * \note This function returns a \b copy of the VCPUs registers.\n
244 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
246 * \param kvm Pointer to the current kvm_context
247 * \param vcpu Which virtual CPU should get dumped
248 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
249 * registers values
250 * \return 0 on success
252 int kvm_get_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
255 * \brief Write VCPU registers
257 * This sets the GP registers on the VCPU from a kvm_regs structure
259 * \note When this function returns, the regs pointer and the data it points to
260 * can be discarded
261 * \param kvm Pointer to the current kvm_context
262 * \param vcpu Which virtual CPU should get dumped
263 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
264 * registers values
265 * \return 0 on success
267 int kvm_set_regs(kvm_vcpu_context_t vcpu, struct kvm_regs *regs);
269 * \brief Read VCPU fpu registers
271 * This gets the FPU registers from the VCPU and outputs them
272 * into a kvm_fpu structure
274 * \note This function returns a \b copy of the VCPUs registers.\n
275 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
277 * \param kvm Pointer to the current kvm_context
278 * \param vcpu Which virtual CPU should get dumped
279 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
280 * fpu registers values
281 * \return 0 on success
283 int kvm_get_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
286 * \brief Write VCPU fpu registers
288 * This sets the FPU registers on the VCPU from a kvm_fpu structure
290 * \note When this function returns, the fpu pointer and the data it points to
291 * can be discarded
292 * \param kvm Pointer to the current kvm_context
293 * \param vcpu Which virtual CPU should get dumped
294 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
295 * \return 0 on success
297 int kvm_set_fpu(kvm_vcpu_context_t vcpu, struct kvm_fpu *fpu);
300 * \brief Read VCPU system registers
302 * This gets the non-GP registers from the VCPU and outputs them
303 * into a kvm_sregs structure
305 * \note This function returns a \b copy of the VCPUs registers.\n
306 * If you wish to modify the VCPUs non-GP registers, you should call
307 * kvm_set_sregs()
309 * \param kvm Pointer to the current kvm_context
310 * \param vcpu Which virtual CPU should get dumped
311 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
312 * registers values
313 * \return 0 on success
315 int kvm_get_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
318 * \brief Write VCPU system registers
320 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
322 * \note When this function returns, the regs pointer and the data it points to
323 * can be discarded
324 * \param kvm Pointer to the current kvm_context
325 * \param vcpu Which virtual CPU should get dumped
326 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
327 * registers values
328 * \return 0 on success
330 int kvm_set_sregs(kvm_vcpu_context_t vcpu, struct kvm_sregs *regs);
332 #ifdef KVM_CAP_MP_STATE
334 * * \brief Read VCPU MP state
337 int kvm_get_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
340 * * \brief Write VCPU MP state
343 int kvm_set_mpstate(kvm_vcpu_context_t vcpu, struct kvm_mp_state *mp_state);
345 * * \brief Reset VCPU MP state
348 static inline int kvm_reset_mpstate(kvm_vcpu_context_t vcpu)
350 struct kvm_mp_state mp_state = {.mp_state = KVM_MP_STATE_UNINITIALIZED
352 return kvm_set_mpstate(vcpu, &mp_state);
354 #endif
357 * \brief Simulate an external vectored interrupt
359 * This allows you to simulate an external vectored interrupt.
361 * \param kvm Pointer to the current kvm_context
362 * \param vcpu Which virtual CPU should get dumped
363 * \param irq Vector number
364 * \return 0 on success
366 int kvm_inject_irq(kvm_vcpu_context_t vcpu, unsigned irq);
368 #ifdef KVM_CAP_SET_GUEST_DEBUG
369 int kvm_set_guest_debug(kvm_vcpu_context_t, struct kvm_guest_debug *dbg);
370 #endif
372 #if defined(__i386__) || defined(__x86_64__)
374 * \brief Setup a vcpu's cpuid instruction emulation
376 * Set up a table of cpuid function to cpuid outputs.\n
378 * \param kvm Pointer to the current kvm_context
379 * \param vcpu Which virtual CPU should be initialized
380 * \param nent number of entries to be installed
381 * \param entries cpuid function entries table
382 * \return 0 on success, or -errno on error
384 int kvm_setup_cpuid(kvm_vcpu_context_t vcpu, int nent,
385 struct kvm_cpuid_entry *entries);
388 * \brief Setup a vcpu's cpuid instruction emulation
390 * Set up a table of cpuid function to cpuid outputs.
391 * This call replaces the older kvm_setup_cpuid interface by adding a few
392 * parameters to support cpuid functions that have sub-leaf values.
394 * \param kvm Pointer to the current kvm_context
395 * \param vcpu Which virtual CPU should be initialized
396 * \param nent number of entries to be installed
397 * \param entries cpuid function entries table
398 * \return 0 on success, or -errno on error
400 int kvm_setup_cpuid2(kvm_vcpu_context_t vcpu, int nent,
401 struct kvm_cpuid_entry2 *entries);
404 * \brief Setting the number of shadow pages to be allocated to the vm
406 * \param kvm pointer to kvm_context
407 * \param nrshadow_pages number of pages to be allocated
409 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages);
412 * \brief Getting the number of shadow pages that are allocated to the vm
414 * \param kvm pointer to kvm_context
415 * \param nrshadow_pages number of pages to be allocated
417 int kvm_get_shadow_pages(kvm_context_t kvm, unsigned int *nrshadow_pages);
420 * \brief Set up cr8 for next time the vcpu is executed
422 * This is a fast setter for cr8, which will be applied when the
423 * vcpu next enters guest mode.
425 * \param kvm Pointer to the current kvm_context
426 * \param vcpu Which virtual CPU should get dumped
427 * \param cr8 next cr8 value
429 void kvm_set_cr8(kvm_vcpu_context_t vcpu, uint64_t cr8);
432 * \brief Get cr8 for sync tpr in qemu apic emulation
434 * This is a getter for cr8, which used to sync with the tpr in qemu
435 * apic emualtion.
437 * \param kvm Pointer to the current kvm_context
438 * \param vcpu Which virtual CPU should get dumped
440 __u64 kvm_get_cr8(kvm_vcpu_context_t vcpu);
441 #endif
444 * \brief Set a vcpu's signal mask for guest mode
446 * A vcpu can have different signals blocked in guest mode and user mode.
447 * This allows guest execution to be interrupted on a signal, without requiring
448 * that the signal be delivered to a signal handler (the signal can be
449 * dequeued using sigwait(2).
451 * \param kvm Pointer to the current kvm_context
452 * \param vcpu Which virtual CPU should be initialized
453 * \param sigset signal mask for guest mode
454 * \return 0 on success, or -errno on error
456 int kvm_set_signal_mask(kvm_vcpu_context_t vcpu, const sigset_t *sigset);
459 * \brief Dump VCPU registers
461 * This dumps some of the information that KVM has about a virtual CPU, namely:
462 * - GP Registers
464 * A much more verbose version of this is available as kvm_dump_vcpu()
466 * \param kvm Pointer to the current kvm_context
467 * \param vcpu Which virtual CPU should get dumped
468 * \return 0 on success
470 void kvm_show_regs(kvm_vcpu_context_t vcpu);
473 void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
474 unsigned long len, int log, int writable);
475 void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,
476 unsigned long len);
477 void kvm_unregister_memory_area(kvm_context_t, uint64_t phys_start,
478 unsigned long len);
480 int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_start,
481 unsigned long size);
482 int kvm_register_phys_mem(kvm_context_t kvm, unsigned long phys_start,
483 void *userspace_addr, unsigned long len, int log);
484 int kvm_get_dirty_pages(kvm_context_t, unsigned long phys_addr, void *buf);
485 int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
486 unsigned long end_addr, void *opaque,
487 int (*cb)(unsigned long start,
488 unsigned long len, void *bitmap,
489 void *opaque));
490 int kvm_register_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
491 uint32_t size);
492 int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr,
493 uint32_t size);
496 * \brief Create a memory alias
498 * Aliases a portion of physical memory to another portion. If the guest
499 * accesses the alias region, it will behave exactly as if it accessed
500 * the target memory.
502 int kvm_create_memory_alias(kvm_context_t, uint64_t phys_start, uint64_t len,
503 uint64_t target_phys);
506 * \brief Destroy a memory alias
508 * Removes an alias created with kvm_create_memory_alias().
510 int kvm_destroy_memory_alias(kvm_context_t, uint64_t phys_start);
513 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
515 * \param kvm Pointer to the current kvm_context
516 * \param phys_addr Memory slot phys addr
517 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
519 int kvm_get_mem_map(kvm_context_t kvm, unsigned long phys_addr, void *bitmap);
520 int kvm_get_mem_map_range(kvm_context_t kvm, unsigned long phys_addr,
521 unsigned long len, void *buf, void *opaque,
522 int (*cb)(unsigned long start,
523 unsigned long len, void *bitmap,
524 void *opaque));
525 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
527 int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm, uint64_t phys_start,
528 uint64_t len);
529 int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm, uint64_t phys_start,
530 uint64_t len);
532 * \brief Enable dirty-pages-logging for all memory regions
534 * \param kvm Pointer to the current kvm_context
536 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm);
539 * \brief Disable dirty-page-logging for some memory regions
541 * Disable dirty-pages-logging for those memory regions that were
542 * created with dirty-page-logging disabled.
544 * \param kvm Pointer to the current kvm_context
546 int kvm_dirty_pages_log_reset(kvm_context_t kvm);
549 * \brief Query whether in kernel irqchip is used
551 * \param kvm Pointer to the current kvm_context
553 int kvm_irqchip_in_kernel(kvm_context_t kvm);
555 #ifdef KVM_CAP_IRQCHIP
557 * \brief Dump in kernel IRQCHIP contents
559 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
560 * and IOAPIC into a kvm_irqchip structure
562 * \param kvm Pointer to the current kvm_context
563 * \param chip The irq chip device to be dumped
565 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
568 * \brief Set in kernel IRQCHIP contents
570 * Write one of the in kernel irq chip devices, including PIC (master/slave)
571 * and IOAPIC
574 * \param kvm Pointer to the current kvm_context
575 * \param chip THe irq chip device to be written
577 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
579 #if defined(__i386__) || defined(__x86_64__)
581 * \brief Get in kernel local APIC for vcpu
583 * Save the local apic state including the timer of a virtual CPU
585 * \param kvm Pointer to the current kvm_context
586 * \param vcpu Which virtual CPU should be accessed
587 * \param s Local apic state of the specific virtual CPU
589 int kvm_get_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
592 * \brief Set in kernel local APIC for vcpu
594 * Restore the local apic state including the timer of a virtual CPU
596 * \param kvm Pointer to the current kvm_context
597 * \param vcpu Which virtual CPU should be accessed
598 * \param s Local apic state of the specific virtual CPU
600 int kvm_set_lapic(kvm_vcpu_context_t vcpu, struct kvm_lapic_state *s);
602 #endif
605 * \brief Simulate an NMI
607 * This allows you to simulate a non-maskable interrupt.
609 * \param kvm Pointer to the current kvm_context
610 * \param vcpu Which virtual CPU should get dumped
611 * \return 0 on success
613 int kvm_inject_nmi(kvm_vcpu_context_t vcpu);
615 #endif
618 * \brief Simulate an x86 MCE
620 * This allows you to simulate a x86 MCE.
622 * \param cenv Which virtual CPU should get MCE injected
623 * \param bank Bank number
624 * \param status MSR_MCI_STATUS
625 * \param mcg_status MSR_MCG_STATUS
626 * \param addr MSR_MCI_ADDR
627 * \param misc MSR_MCI_MISC
629 void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
630 uint64_t mcg_status, uint64_t addr, uint64_t misc);
633 * \brief Query wheather in kernel pit is used
635 * \param kvm Pointer to the current kvm_context
637 int kvm_pit_in_kernel(kvm_context_t kvm);
640 * \brief Initialize coalesced MMIO
642 * Check for coalesced MMIO capability and store in context
644 * \param kvm Pointer to the current kvm_context
646 int kvm_init_coalesced_mmio(kvm_context_t kvm);
648 #ifdef KVM_CAP_PIT
650 #if defined(__i386__) || defined(__x86_64__)
652 * \brief Get in kernel PIT of the virtual domain
654 * Save the PIT state.
656 * \param kvm Pointer to the current kvm_context
657 * \param s PIT state of the virtual domain
659 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
662 * \brief Set in kernel PIT of the virtual domain
664 * Restore the PIT state.
665 * Timer would be retriggerred after restored.
667 * \param kvm Pointer to the current kvm_context
668 * \param s PIT state of the virtual domain
670 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
672 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
674 #ifdef KVM_CAP_PIT_STATE2
676 * \brief Check for kvm support of kvm_pit_state2
678 * \param kvm Pointer to the current kvm_context
679 * \return 0 on success
681 int kvm_has_pit_state2(kvm_context_t kvm);
684 * \brief Set in kernel PIT state2 of the virtual domain
687 * \param kvm Pointer to the current kvm_context
688 * \param ps2 PIT state2 of the virtual domain
689 * \return 0 on success
691 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
694 * \brief Get in kernel PIT state2 of the virtual domain
697 * \param kvm Pointer to the current kvm_context
698 * \param ps2 PIT state2 of the virtual domain
699 * \return 0 on success
701 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
703 #endif
704 #endif
705 #endif
707 #ifdef KVM_CAP_VAPIC
710 * \brief Enable kernel tpr access reporting
712 * When tpr access reporting is enabled, the kernel will call the
713 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
715 * \param kvm Pointer to the current kvm_context
716 * \param vcpu vcpu to enable tpr access reporting on
718 int kvm_enable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
721 * \brief Disable kernel tpr access reporting
723 * Undoes the effect of kvm_enable_tpr_access_reporting().
725 * \param kvm Pointer to the current kvm_context
726 * \param vcpu vcpu to disable tpr access reporting on
728 int kvm_disable_tpr_access_reporting(kvm_vcpu_context_t vcpu);
730 int kvm_enable_vapic(kvm_vcpu_context_t vcpu, uint64_t vapic);
732 #endif
734 #if defined(__s390__)
735 int kvm_s390_initial_reset(kvm_context_t kvm, int slot);
736 int kvm_s390_interrupt(kvm_context_t kvm, int slot,
737 struct kvm_s390_interrupt *kvmint);
738 int kvm_s390_set_initial_psw(kvm_context_t kvm, int slot, psw_t psw);
739 int kvm_s390_store_status(kvm_context_t kvm, int slot, unsigned long addr);
740 #endif
742 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
744 * \brief Notifies host kernel about a PCI device to be assigned to a guest
746 * Used for PCI device assignment, this function notifies the host
747 * kernel about the assigning of the physical PCI device to a guest.
749 * \param kvm Pointer to the current kvm_context
750 * \param assigned_dev Parameters, like bus, devfn number, etc
752 int kvm_assign_pci_device(kvm_context_t kvm,
753 struct kvm_assigned_pci_dev *assigned_dev);
756 * \brief Assign IRQ for an assigned device
758 * Used for PCI device assignment, this function assigns IRQ numbers for
759 * an physical device and guest IRQ handling.
761 * \param kvm Pointer to the current kvm_context
762 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
764 int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
766 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
768 * \brief Deassign IRQ for an assigned device
770 * Used for PCI device assignment, this function deassigns IRQ numbers
771 * for an assigned device.
773 * \param kvm Pointer to the current kvm_context
774 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
776 int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
777 #endif
778 #endif
781 * \brief Determines whether destroying memory regions is allowed
783 * KVM before 2.6.29 had a bug when destroying memory regions.
785 * \param kvm Pointer to the current kvm_context
787 int kvm_destroy_memory_region_works(kvm_context_t kvm);
789 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
791 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
793 * Used for hot remove PCI device, this function notifies the host
794 * kernel about the deassigning of the physical PCI device from a guest.
796 * \param kvm Pointer to the current kvm_context
797 * \param assigned_dev Parameters, like bus, devfn number, etc
799 int kvm_deassign_pci_device(kvm_context_t kvm,
800 struct kvm_assigned_pci_dev *assigned_dev);
801 #endif
804 * \brief Checks whether the generic irq routing capability is present
806 * Checks whether kvm can reroute interrupts among the various interrupt
807 * controllers.
809 * \param kvm Pointer to the current kvm_context
811 int kvm_has_gsi_routing(kvm_context_t kvm);
814 * \brief Determines the number of gsis that can be routed
816 * Returns the number of distinct gsis that can be routed by kvm. This is
817 * also the number of distinct routes (if a gsi has two routes, than another
818 * gsi cannot be used...)
820 * \param kvm Pointer to the current kvm_context
822 int kvm_get_gsi_count(kvm_context_t kvm);
825 * \brief Clears the temporary irq routing table
827 * Clears the temporary irq routing table. Nothing is committed to the
828 * running VM.
830 * \param kvm Pointer to the current kvm_context
832 int kvm_clear_gsi_routes(kvm_context_t kvm);
835 * \brief Adds an irq route to the temporary irq routing table
837 * Adds an irq route to the temporary irq routing table. Nothing is
838 * committed to the running VM.
840 * \param kvm Pointer to the current kvm_context
842 int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
845 * \brief Removes an irq route from the temporary irq routing table
847 * Adds an irq route to the temporary irq routing table. Nothing is
848 * committed to the running VM.
850 * \param kvm Pointer to the current kvm_context
852 int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin);
854 struct kvm_irq_routing_entry;
856 * \brief Adds a routing entry to the temporary irq routing table
858 * Adds a filled routing entry to the temporary irq routing table. Nothing is
859 * committed to the running VM.
861 * \param kvm Pointer to the current kvm_context
863 int kvm_add_routing_entry(kvm_context_t kvm,
864 struct kvm_irq_routing_entry *entry);
867 * \brief Removes a routing from the temporary irq routing table
869 * Remove a routing to the temporary irq routing table. Nothing is
870 * committed to the running VM.
872 * \param kvm Pointer to the current kvm_context
874 int kvm_del_routing_entry(kvm_context_t kvm,
875 struct kvm_irq_routing_entry *entry);
878 * \brief Updates a routing in the temporary irq routing table
880 * Update a routing in the temporary irq routing table
881 * with a new value. entry type and GSI can not be changed.
882 * Nothing is committed to the running VM.
884 * \param kvm Pointer to the current kvm_context
886 int kvm_update_routing_entry(kvm_context_t kvm,
887 struct kvm_irq_routing_entry *entry,
888 struct kvm_irq_routing_entry *newentry);
891 * \brief Commit the temporary irq routing table
893 * Commit the temporary irq routing table to the running VM.
895 * \param kvm Pointer to the current kvm_context
897 int kvm_commit_irq_routes(kvm_context_t kvm);
900 * \brief Get unused GSI number for irq routing table
902 * Get unused GSI number for irq routing table
904 * \param kvm Pointer to the current kvm_context
906 int kvm_get_irq_route_gsi(kvm_context_t kvm);
909 * \brief Create a file descriptor for injecting interrupts
911 * Creates an eventfd based file-descriptor that maps to a specific GSI
912 * in the guest. eventfd compliant signaling (write() from userspace, or
913 * eventfd_signal() from kernelspace) will cause the GSI to inject
914 * itself into the guest at the next available window.
916 * \param kvm Pointer to the current kvm_context
917 * \param gsi GSI to assign to this fd
918 * \param flags reserved, must be zero
920 int kvm_irqfd(kvm_context_t kvm, int gsi, int flags);
922 #ifdef KVM_CAP_DEVICE_MSIX
923 int kvm_assign_set_msix_nr(kvm_context_t kvm,
924 struct kvm_assigned_msix_nr *msix_nr);
925 int kvm_assign_set_msix_entry(kvm_context_t kvm,
926 struct kvm_assigned_msix_entry *entry);
927 #endif
929 uint32_t kvm_get_supported_cpuid(kvm_context_t kvm, uint32_t function, int reg);
931 #else /* !CONFIG_KVM */
933 typedef struct kvm_context *kvm_context_t;
934 typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
936 struct kvm_pit_state {
939 static inline int kvm_init(int smp_cpus)
941 return 0;
944 static inline void kvm_inject_x86_mce(CPUState *cenv, int bank,
945 uint64_t status, uint64_t mcg_status,
946 uint64_t addr, uint64_t misc)
951 extern int kvm_allowed;
953 #endif /* !CONFIG_KVM */
956 int kvm_main_loop(void);
957 int kvm_init_ap(void);
958 int kvm_vcpu_inited(CPUState *env);
959 void kvm_load_registers(CPUState *env);
960 void kvm_save_registers(CPUState *env);
961 void kvm_load_mpstate(CPUState *env);
962 void kvm_save_mpstate(CPUState *env);
963 int kvm_cpu_exec(CPUState *env);
964 int kvm_insert_breakpoint(CPUState * current_env, target_ulong addr,
965 target_ulong len, int type);
966 int kvm_remove_breakpoint(CPUState * current_env, target_ulong addr,
967 target_ulong len, int type);
968 void kvm_remove_all_breakpoints(CPUState * current_env);
969 int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
970 void kvm_apic_init(CPUState *env);
971 /* called from vcpu initialization */
972 void qemu_kvm_load_lapic(CPUState *env);
974 void kvm_hpet_enable_kpit(void);
975 void kvm_hpet_disable_kpit(void);
976 int kvm_set_irq(int irq, int level, int *status);
978 int kvm_physical_memory_set_dirty_tracking(int enable);
979 int kvm_update_dirty_pages_log(void);
981 void qemu_kvm_call_with_env(void (*func)(void *), void *data, CPUState *env);
982 void qemu_kvm_cpuid_on_env(CPUState *env);
983 void kvm_inject_interrupt(CPUState *env, int mask);
984 void kvm_update_after_sipi(CPUState *env);
985 void kvm_update_interrupt_request(CPUState *env);
986 void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
987 ram_addr_t phys_offset);
988 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr, unsigned long size,
989 int log, int writable);
991 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
992 unsigned long size);
993 void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
994 int log);
995 int kvm_setup_guest_memory(void *area, unsigned long size);
996 int kvm_qemu_create_memory_alias(uint64_t phys_start, uint64_t len,
997 uint64_t target_phys);
998 int kvm_qemu_destroy_memory_alias(uint64_t phys_start);
1000 int kvm_arch_qemu_create_context(void);
1002 void kvm_arch_save_regs(CPUState *env);
1003 void kvm_arch_load_regs(CPUState *env);
1004 void kvm_arch_load_mpstate(CPUState *env);
1005 void kvm_arch_save_mpstate(CPUState *env);
1006 int kvm_arch_init_vcpu(CPUState *cenv);
1007 void kvm_arch_pre_kvm_run(void *opaque, CPUState *env);
1008 void kvm_arch_post_kvm_run(void *opaque, CPUState *env);
1009 int kvm_arch_has_work(CPUState *env);
1010 void kvm_arch_process_irqchip_events(CPUState *env);
1011 int kvm_arch_try_push_interrupts(void *opaque);
1012 void kvm_arch_push_nmi(void *opaque);
1013 void kvm_arch_update_regs_for_sipi(CPUState *env);
1014 void kvm_arch_cpu_reset(CPUState *env);
1015 int kvm_set_boot_cpu_id(uint32_t id);
1017 struct kvm_guest_debug;
1018 struct kvm_debug_exit_arch;
1020 struct kvm_sw_breakpoint {
1021 target_ulong pc;
1022 target_ulong saved_insn;
1023 int use_count;
1024 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
1027 QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
1029 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
1030 int kvm_sw_breakpoints_active(CPUState *env);
1031 struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *env,
1032 target_ulong pc);
1033 int kvm_arch_insert_sw_breakpoint(CPUState * current_env,
1034 struct kvm_sw_breakpoint *bp);
1035 int kvm_arch_remove_sw_breakpoint(CPUState * current_env,
1036 struct kvm_sw_breakpoint *bp);
1037 int kvm_arch_insert_hw_breakpoint(target_ulong addr, target_ulong len,
1038 int type);
1039 int kvm_arch_remove_hw_breakpoint(target_ulong addr, target_ulong len,
1040 int type);
1041 void kvm_arch_remove_all_hw_breakpoints(void);
1042 void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
1044 void qemu_kvm_aio_wait_start(void);
1045 void qemu_kvm_aio_wait(void);
1046 void qemu_kvm_aio_wait_end(void);
1048 void qemu_kvm_notify_work(void);
1050 void kvm_tpr_opt_setup(void);
1051 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
1052 void kvm_tpr_vcpu_start(CPUState *env);
1054 int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
1055 int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1056 int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
1058 int kvm_arch_init_irq_routing(void);
1060 int kvm_mmio_read(void *opaque, uint64_t addr, uint8_t * data, int len);
1061 int kvm_mmio_write(void *opaque, uint64_t addr, uint8_t * data, int len);
1063 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1064 struct ioperm_data;
1066 void kvm_ioperm(CPUState *env, void *data);
1067 void kvm_add_ioperm_data(struct ioperm_data *data);
1068 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
1069 void kvm_arch_do_ioperm(void *_data);
1070 #endif
1072 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
1073 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
1075 #ifdef CONFIG_KVM
1076 #include "qemu-queue.h"
1078 extern int kvm_allowed;
1079 extern int kvm_irqchip;
1080 extern int kvm_pit;
1081 extern int kvm_pit_reinject;
1082 extern int kvm_nested;
1083 extern kvm_context_t kvm_context;
1085 struct ioperm_data {
1086 unsigned long start_port;
1087 unsigned long num;
1088 int turn_on;
1089 QLIST_ENTRY(ioperm_data) entries;
1092 void qemu_kvm_cpu_stop(CPUState *env);
1093 int kvm_arch_halt(void *opaque, kvm_vcpu_context_t vcpu);
1094 int handle_tpr_access(void *opaque, kvm_vcpu_context_t vcpu, uint64_t rip,
1095 int is_write);
1096 int kvm_has_sync_mmu(void);
1098 #define kvm_enabled() (kvm_allowed)
1099 #define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
1100 #define qemu_kvm_pit_in_kernel() kvm_pit_in_kernel(kvm_context)
1101 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing(kvm_context)
1102 #ifdef TARGET_I386
1103 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
1104 #endif
1105 void kvm_init_vcpu(CPUState *env);
1106 void kvm_load_tsc(CPUState *env);
1107 #else
1108 #define kvm_has_sync_mmu() (0)
1109 #define kvm_enabled() (0)
1110 #define kvm_nested 0
1111 #define qemu_kvm_irqchip_in_kernel() (0)
1112 #define qemu_kvm_pit_in_kernel() (0)
1113 #define qemu_kvm_has_gsi_routing() (0)
1114 #ifdef TARGET_I386
1115 #define qemu_kvm_has_pit_state2() (0)
1116 #endif
1117 #define kvm_load_registers(env) do {} while(0)
1118 #define kvm_save_registers(env) do {} while(0)
1119 #define qemu_kvm_cpu_stop(env) do {} while(0)
1120 static inline void kvm_init_vcpu(CPUState *env)
1124 static inline void kvm_load_tsc(CPUState *env)
1127 #endif
1129 void kvm_mutex_unlock(void);
1130 void kvm_mutex_lock(void);
1132 static inline void qemu_mutex_unlock_iothread(void)
1134 if (kvm_enabled())
1135 kvm_mutex_unlock();
1138 static inline void qemu_mutex_lock_iothread(void)
1140 if (kvm_enabled())
1141 kvm_mutex_lock();
1144 int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
1145 target_phys_addr_t end_addr);
1147 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
1148 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
1151 static inline int kvm_sync_vcpus(void)
1153 return 0;
1156 static inline void kvm_arch_get_registers(CPUState *env)
1158 kvm_arch_save_regs(env);
1159 kvm_arch_save_mpstate(env);
1160 #ifdef KVM_CAP_MP_STATE
1161 env->halted = (env->mp_state == KVM_MP_STATE_HALTED);
1162 #endif
1165 static inline void kvm_arch_put_registers(CPUState *env)
1167 kvm_load_registers(env);
1168 kvm_load_mpstate(env);
1171 void kvm_cpu_synchronize_state(CPUState *env);
1173 static inline void cpu_synchronize_state(CPUState *env)
1175 if (kvm_enabled()) {
1176 kvm_cpu_synchronize_state(env);
1180 uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
1181 int reg);
1184 static inline int kvm_set_migration_log(int enable)
1186 return kvm_physical_memory_set_dirty_tracking(enable);
1189 #ifdef CONFIG_KVM
1191 typedef struct KVMSlot {
1192 target_phys_addr_t start_addr;
1193 ram_addr_t memory_size;
1194 ram_addr_t phys_offset;
1195 int slot;
1196 int flags;
1197 } KVMSlot;
1199 typedef struct kvm_dirty_log KVMDirtyLog;
1201 typedef struct KVMState {
1202 KVMSlot slots[32];
1203 int fd;
1204 int vmfd;
1205 int coalesced_mmio;
1206 int broken_set_mem_region;
1207 int migration_log;
1208 #ifdef KVM_CAP_SET_GUEST_DEBUG
1209 struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
1210 #endif
1211 struct kvm_context kvm_context;
1212 } KVMState;
1214 extern KVMState *kvm_state;
1216 int kvm_ioctl(KVMState *s, int type, ...);
1217 int kvm_vm_ioctl(KVMState *s, int type, ...);
1218 int kvm_check_extension(KVMState *s, unsigned int ext);
1220 #endif
1222 #endif