11 #define __user /* temporary, until installed via make headers_install */
14 #include <linux/kvm.h>
20 typedef struct kvm_context
*kvm_context_t
;
22 #if defined(__x86_64__) || defined(__i386__)
23 struct kvm_msr_list
*kvm_get_msr_list(kvm_context_t
);
24 int kvm_get_msrs(kvm_context_t
, int vcpu
, struct kvm_msr_entry
*msrs
, int n
);
25 int kvm_set_msrs(kvm_context_t
, int vcpu
, struct kvm_msr_entry
*msrs
, int n
);
29 * \brief KVM callbacks structure
31 * This structure holds pointers to various functions that KVM will call
32 * when it encounters something that cannot be virtualized, such as
33 * accessing hardware devices via MMIO or regular IO.
35 struct kvm_callbacks
{
36 /// For 8bit IO reads from the guest (Usually when executing 'inb')
37 int (*inb
)(void *opaque
, uint16_t addr
, uint8_t *data
);
38 /// For 16bit IO reads from the guest (Usually when executing 'inw')
39 int (*inw
)(void *opaque
, uint16_t addr
, uint16_t *data
);
40 /// For 32bit IO reads from the guest (Usually when executing 'inl')
41 int (*inl
)(void *opaque
, uint16_t addr
, uint32_t *data
);
42 /// For 8bit IO writes from the guest (Usually when executing 'outb')
43 int (*outb
)(void *opaque
, uint16_t addr
, uint8_t data
);
44 /// For 16bit IO writes from the guest (Usually when executing 'outw')
45 int (*outw
)(void *opaque
, uint16_t addr
, uint16_t data
);
46 /// For 32bit IO writes from the guest (Usually when executing 'outl')
47 int (*outl
)(void *opaque
, uint16_t addr
, uint32_t data
);
48 /// generic memory reads to unmapped memory (For MMIO devices)
49 int (*mmio_read
)(void *opaque
, uint64_t addr
, uint8_t *data
,
51 /// generic memory writes to unmapped memory (For MMIO devices)
52 int (*mmio_write
)(void *opaque
, uint64_t addr
, uint8_t *data
,
54 int (*debug
)(void *opaque
, int vcpu
);
56 * \brief Called when the VCPU issues an 'hlt' instruction.
58 * Typically, you should yeild here to prevent 100% CPU utilization
61 int (*halt
)(void *opaque
, int vcpu
);
62 int (*shutdown
)(void *opaque
, int vcpu
);
63 int (*io_window
)(void *opaque
);
64 int (*try_push_interrupts
)(void *opaque
);
65 void (*post_kvm_run
)(void *opaque
, int vcpu
);
66 int (*pre_kvm_run
)(void *opaque
, int vcpu
);
67 int (*tpr_access
)(void *opaque
, int vcpu
, uint64_t rip
, int is_write
);
68 #if defined(__powerpc__)
69 int (*powerpc_dcr_read
)(uint32_t dcrn
, uint32_t *data
);
70 int (*powerpc_dcr_write
)(uint32_t dcrn
, uint32_t data
);
75 * \brief Create new KVM context
77 * This creates a new kvm_context. A KVM context is a small area of data that
78 * holds information about the KVM instance that gets created by this call.\n
79 * This should always be your first call to KVM.
81 * \param callbacks Pointer to a valid kvm_callbacks structure
82 * \param opaque Not used
83 * \return NULL on failure
85 kvm_context_t
kvm_init(struct kvm_callbacks
*callbacks
,
89 * \brief Cleanup the KVM context
91 * Should always be called when closing down KVM.\n
92 * Exception: If kvm_init() fails, this function should not be called, as the
93 * context would be invalid
95 * \param kvm Pointer to the kvm_context that is to be freed
97 void kvm_finalize(kvm_context_t kvm
);
100 * \brief Disable the in-kernel IRQCHIP creation
102 * In-kernel irqchip is enabled by default. If userspace irqchip is to be used,
103 * this should be called prior to kvm_create().
105 * \param kvm Pointer to the kvm_context
107 void kvm_disable_irqchip_creation(kvm_context_t kvm
);
110 * \brief Create new virtual machine
112 * This creates a new virtual machine, maps physical RAM to it, and creates a
113 * virtual CPU for it.\n
115 * Memory gets mapped for addresses 0->0xA0000, 0xC0000->phys_mem_bytes
117 * \param kvm Pointer to the current kvm_context
118 * \param phys_mem_bytes The amount of physical ram you want the VM to have
119 * \param phys_mem This pointer will be set to point to the memory that
120 * kvm_create allocates for physical RAM
121 * \return 0 on success
123 int kvm_create(kvm_context_t kvm
,
124 unsigned long phys_mem_bytes
,
126 int kvm_create_vm(kvm_context_t kvm
);
127 int kvm_check_extension(kvm_context_t kvm
, int ext
);
128 void kvm_create_irqchip(kvm_context_t kvm
);
131 * \brief Create a new virtual cpu
133 * This creates a new virtual cpu (the first vcpu is created by kvm_create()).
134 * Should be called from a thread dedicated to the vcpu.
136 * \param kvm kvm context
137 * \param slot vcpu number (> 0)
138 * \return 0 on success, -errno on failure
140 int kvm_create_vcpu(kvm_context_t kvm
, int slot
);
143 * \brief Start the VCPU
145 * This starts the VCPU and virtualization is started.\n
147 * This function will not return until any of these conditions are met:
148 * - An IO/MMIO handler does not return "0"
149 * - An exception that neither the guest OS, nor KVM can handle occurs
151 * \note This function will call the callbacks registered in kvm_init()
152 * to emulate those functions
153 * \note If you at any point want to interrupt the VCPU, kvm_run() will
154 * listen to the EINTR signal. This allows you to simulate external interrupts
155 * and asyncronous IO.
157 * \param kvm Pointer to the current kvm_context
158 * \param vcpu Which virtual CPU should be started
159 * \return 0 on success, but you really shouldn't expect this function to
160 * return except for when an error has occured, or when you have sent it
163 int kvm_run(kvm_context_t kvm
, int vcpu
);
166 * \brief Get interrupt flag from on last exit to userspace
168 * This gets the CPU interrupt flag as it was on the last exit to userspace.
170 * \param kvm Pointer to the current kvm_context
171 * \param vcpu Which virtual CPU should get dumped
172 * \return interrupt flag value (0 or 1)
174 int kvm_get_interrupt_flag(kvm_context_t kvm
, int vcpu
);
177 * \brief Get the value of the APIC_BASE msr as of last exit to userspace
179 * This gets the APIC_BASE msr as it was on the last exit to userspace.
181 * \param kvm Pointer to the current kvm_context
182 * \param vcpu Which virtual CPU should get dumped
183 * \return APIC_BASE msr contents
185 uint64_t kvm_get_apic_base(kvm_context_t kvm
, int vcpu
);
188 * \brief Check if a vcpu is ready for interrupt injection
190 * This checks if vcpu interrupts are not masked by mov ss or sti.
192 * \param kvm Pointer to the current kvm_context
193 * \param vcpu Which virtual CPU should get dumped
194 * \return boolean indicating interrupt injection readiness
196 int kvm_is_ready_for_interrupt_injection(kvm_context_t kvm
, int vcpu
);
199 * \brief Read VCPU registers
201 * This gets the GP registers from the VCPU and outputs them
202 * into a kvm_regs structure
204 * \note This function returns a \b copy of the VCPUs registers.\n
205 * If you wish to modify the VCPUs GP registers, you should call kvm_set_regs()
207 * \param kvm Pointer to the current kvm_context
208 * \param vcpu Which virtual CPU should get dumped
209 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
211 * \return 0 on success
213 int kvm_get_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
);
216 * \brief Write VCPU registers
218 * This sets the GP registers on the VCPU from a kvm_regs structure
220 * \note When this function returns, the regs pointer and the data it points to
222 * \param kvm Pointer to the current kvm_context
223 * \param vcpu Which virtual CPU should get dumped
224 * \param regs Pointer to a kvm_regs which will be populated with the VCPUs
226 * \return 0 on success
228 int kvm_set_regs(kvm_context_t kvm
, int vcpu
, struct kvm_regs
*regs
);
230 * \brief Read VCPU fpu registers
232 * This gets the FPU registers from the VCPU and outputs them
233 * into a kvm_fpu structure
235 * \note This function returns a \b copy of the VCPUs registers.\n
236 * If you wish to modify the VCPU FPU registers, you should call kvm_set_fpu()
238 * \param kvm Pointer to the current kvm_context
239 * \param vcpu Which virtual CPU should get dumped
240 * \param fpu Pointer to a kvm_fpu which will be populated with the VCPUs
241 * fpu registers values
242 * \return 0 on success
244 int kvm_get_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
);
247 * \brief Write VCPU fpu registers
249 * This sets the FPU registers on the VCPU from a kvm_fpu structure
251 * \note When this function returns, the fpu pointer and the data it points to
253 * \param kvm Pointer to the current kvm_context
254 * \param vcpu Which virtual CPU should get dumped
255 * \param fpu Pointer to a kvm_fpu which holds the new vcpu fpu state
256 * \return 0 on success
258 int kvm_set_fpu(kvm_context_t kvm
, int vcpu
, struct kvm_fpu
*fpu
);
261 * \brief Read VCPU system registers
263 * This gets the non-GP registers from the VCPU and outputs them
264 * into a kvm_sregs structure
266 * \note This function returns a \b copy of the VCPUs registers.\n
267 * If you wish to modify the VCPUs non-GP registers, you should call
270 * \param kvm Pointer to the current kvm_context
271 * \param vcpu Which virtual CPU should get dumped
272 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
274 * \return 0 on success
276 int kvm_get_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*regs
);
279 * \brief Write VCPU system registers
281 * This sets the non-GP registers on the VCPU from a kvm_sregs structure
283 * \note When this function returns, the regs pointer and the data it points to
285 * \param kvm Pointer to the current kvm_context
286 * \param vcpu Which virtual CPU should get dumped
287 * \param regs Pointer to a kvm_sregs which will be populated with the VCPUs
289 * \return 0 on success
291 int kvm_set_sregs(kvm_context_t kvm
, int vcpu
, struct kvm_sregs
*regs
);
294 * \brief Simulate an external vectored interrupt
296 * This allows you to simulate an external vectored interrupt.
298 * \param kvm Pointer to the current kvm_context
299 * \param vcpu Which virtual CPU should get dumped
300 * \param irq Vector number
301 * \return 0 on success
303 int kvm_inject_irq(kvm_context_t kvm
, int vcpu
, unsigned irq
);
305 int kvm_guest_debug(kvm_context_t
, int vcpu
, struct kvm_debug_guest
*dbg
);
307 #if defined(__i386__) || defined(__x86_64__)
309 * \brief Setup a vcpu's cpuid instruction emulation
311 * Set up a table of cpuid function to cpuid outputs.\n
313 * \param kvm Pointer to the current kvm_context
314 * \param vcpu Which virtual CPU should be initialized
315 * \param nent number of entries to be installed
316 * \param entries cpuid function entries table
317 * \return 0 on success, or -errno on error
319 int kvm_setup_cpuid(kvm_context_t kvm
, int vcpu
, int nent
,
320 struct kvm_cpuid_entry
*entries
);
323 * \brief Setting the number of shadow pages to be allocated to the vm
325 * \param kvm pointer to kvm_context
326 * \param nrshadow_pages number of pages to be allocated
328 int kvm_set_shadow_pages(kvm_context_t kvm
, unsigned int nrshadow_pages
);
331 * \breif Getting the number of shadow pages that are allocated to the vm
333 * \param kvm pointer to kvm_context
334 * \param nrshadow_pages number of pages to be allocated
336 int kvm_get_shadow_pages(kvm_context_t kvm
, unsigned int *nrshadow_pages
);
339 * \brief Set up cr8 for next time the vcpu is executed
341 * This is a fast setter for cr8, which will be applied when the
342 * vcpu next enters guest mode.
344 * \param kvm Pointer to the current kvm_context
345 * \param vcpu Which virtual CPU should get dumped
346 * \param cr8 next cr8 value
348 void kvm_set_cr8(kvm_context_t kvm
, int vcpu
, uint64_t cr8
);
351 * \brief Get cr8 for sync tpr in qemu apic emulation
353 * This is a getter for cr8, which used to sync with the tpr in qemu
356 * \param kvm Pointer to the current kvm_context
357 * \param vcpu Which virtual CPU should get dumped
359 __u64
kvm_get_cr8(kvm_context_t kvm
, int vcpu
);
363 * \brief Set a vcpu's signal mask for guest mode
365 * A vcpu can have different signals blocked in guest mode and user mode.
366 * This allows guest execution to be interrupted on a signal, without requiring
367 * that the signal be delivered to a signal handler (the signal can be
368 * dequeued using sigwait(2).
370 * \param kvm Pointer to the current kvm_context
371 * \param vcpu Which virtual CPU should be initialized
372 * \param sigset signal mask for guest mode
373 * \return 0 on success, or -errno on error
375 int kvm_set_signal_mask(kvm_context_t kvm
, int vcpu
, const sigset_t
*sigset
);
378 * \brief Dump all VCPU information
380 * This dumps \b all the information that KVM has about a virtual CPU, namely:
382 * - System registers (selectors, descriptors, etc)
385 * - Pending interrupts
387 * \param kvm Pointer to the current kvm_context
388 * \param vcpu Which virtual CPU should get dumped
389 * \return 0 on success
391 int kvm_dump_vcpu(kvm_context_t kvm
, int vcpu
);
394 * \brief Dump VCPU registers
396 * This dumps some of the information that KVM has about a virtual CPU, namely:
399 * A much more verbose version of this is available as kvm_dump_vcpu()
401 * \param kvm Pointer to the current kvm_context
402 * \param vcpu Which virtual CPU should get dumped
403 * \return 0 on success
405 void kvm_show_regs(kvm_context_t kvm
, int vcpu
);
408 void *kvm_create_phys_mem(kvm_context_t
, unsigned long phys_start
,
409 unsigned long len
, int log
, int writable
);
410 void kvm_destroy_phys_mem(kvm_context_t
, unsigned long phys_start
,
412 int kvm_is_intersecting_mem(kvm_context_t kvm
, unsigned long phys_start
);
413 int kvm_is_allocated_mem(kvm_context_t kvm
, unsigned long phys_start
,
415 int kvm_create_mem_hole(kvm_context_t kvm
, unsigned long phys_start
,
417 int kvm_register_userspace_phys_mem(kvm_context_t kvm
,
418 unsigned long phys_start
, void *userspace_addr
,
419 unsigned long len
, int log
);
420 int kvm_get_dirty_pages(kvm_context_t
, unsigned long phys_addr
, void *buf
);
421 int kvm_get_dirty_pages_range(kvm_context_t kvm
, unsigned long phys_addr
,
422 unsigned long end_addr
, void *buf
, void*opaque
,
423 int (*cb
)(unsigned long start
, unsigned long len
,
424 void*bitmap
, void *opaque
));
427 * \brief Create a memory alias
429 * Aliases a portion of physical memory to another portion. If the guest
430 * accesses the alias region, it will behave exactly as if it accessed
433 int kvm_create_memory_alias(kvm_context_t
, uint64_t phys_addr
,
434 uint64_t phys_start
, uint64_t len
,
435 uint64_t target_phys
);
438 * \brief Destroy a memory alias
440 * Removes an alias created with kvm_create_memory_alias().
442 int kvm_destroy_memory_alias(kvm_context_t
, uint64_t phys_addr
);
445 * \brief Get a bitmap of guest ram pages which are allocated to the guest.
447 * \param kvm Pointer to the current kvm_context
448 * \param phys_addr Memory slot phys addr
449 * \param bitmap Long aligned address of a big enough bitmap (one bit per page)
451 int kvm_get_mem_map(kvm_context_t kvm
, unsigned long phys_addr
, void *bitmap
);
452 int kvm_get_mem_map_range(kvm_context_t kvm
, unsigned long phys_addr
,
453 unsigned long len
, void *buf
, void *opaque
,
454 int (*cb
)(unsigned long start
,unsigned long len
,
455 void* bitmap
, void* opaque
));
456 int kvm_set_irq_level(kvm_context_t kvm
, int irq
, int level
);
459 * \brief Enable dirty-pages-logging for all memory regions
461 * \param kvm Pointer to the current kvm_context
463 int kvm_dirty_pages_log_enable_all(kvm_context_t kvm
);
466 * \brief Disable dirty-page-logging for some memory regions
468 * Disable dirty-pages-logging for those memory regions that were
469 * created with dirty-page-logging disabled.
471 * \param kvm Pointer to the current kvm_context
473 int kvm_dirty_pages_log_reset(kvm_context_t kvm
);
476 * \brief Query whether in kernel irqchip is used
478 * \param kvm Pointer to the current kvm_context
480 int kvm_irqchip_in_kernel(kvm_context_t kvm
);
482 #ifdef KVM_CAP_IRQCHIP
484 * \brief Dump in kernel IRQCHIP contents
486 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
487 * and IOAPIC into a kvm_irqchip structure
489 * \param kvm Pointer to the current kvm_context
490 * \param chip The irq chip device to be dumped
492 int kvm_get_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
);
495 * \brief Set in kernel IRQCHIP contents
497 * Write one of the in kernel irq chip devices, including PIC (master/slave)
501 * \param kvm Pointer to the current kvm_context
502 * \param chip THe irq chip device to be written
504 int kvm_set_irqchip(kvm_context_t kvm
, struct kvm_irqchip
*chip
);
506 #if defined(__i386__) || defined(__x86_64__)
508 * \brief Get in kernel local APIC for vcpu
510 * Save the local apic state including the timer of a virtual CPU
512 * \param kvm Pointer to the current kvm_context
513 * \param vcpu Which virtual CPU should be accessed
514 * \param s Local apic state of the specific virtual CPU
516 int kvm_get_lapic(kvm_context_t kvm
, int vcpu
, struct kvm_lapic_state
*s
);
519 * \brief Set in kernel local APIC for vcpu
521 * Restore the local apic state including the timer of a virtual CPU
523 * \param kvm Pointer to the current kvm_context
524 * \param vcpu Which virtual CPU should be accessed
525 * \param s Local apic state of the specific virtual CPU
527 int kvm_set_lapic(kvm_context_t kvm
, int vcpu
, struct kvm_lapic_state
*s
);
535 * \brief Enable kernel tpr access reporting
537 * When tpr access reporting is enabled, the kernel will call the
538 * ->tpr_access() callback every time the guest vcpu accesses the tpr.
540 * \param kvm Pointer to the current kvm_context
541 * \param vcpu vcpu to enable tpr access reporting on
543 int kvm_enable_tpr_access_reporting(kvm_context_t kvm
, int vcpu
);
546 * \brief Disable kernel tpr access reporting
548 * Undoes the effect of kvm_enable_tpr_access_reporting().
550 * \param kvm Pointer to the current kvm_context
551 * \param vcpu vcpu to disable tpr access reporting on
553 int kvm_disable_tpr_access_reporting(kvm_context_t kvm
, int vcpu
);
555 int kvm_enable_vapic(kvm_context_t kvm
, int vcpu
, uint64_t vapic
);