Merge commit '64e07be544ee9c5fb5b741175262fd34726ec431' into upstream-merge
[qemu/qemu-dev-zwu.git] / qemu-kvm.h
blob8b3ce9c20998691a605e73c6a2ca8a29229d7753
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>
14 #include <stdlib.h>
16 #ifdef CONFIG_KVM
18 #if defined(__s390__)
19 #include <asm/ptrace.h>
20 #endif
22 #include <stdint.h>
24 #ifndef __user
25 #define __user /* temporary, until installed via make headers_install */
26 #endif
28 #include <linux/kvm.h>
30 #include <signal.h>
32 /* FIXME: share this number with kvm */
33 /* FIXME: or dynamically alloc/realloc regions */
34 #ifdef __s390__
35 #define KVM_MAX_NUM_MEM_REGIONS 1u
36 #define MAX_VCPUS 64
37 #define LIBKVM_S390_ORIGIN (0UL)
38 #elif defined(__ia64__)
39 #define KVM_MAX_NUM_MEM_REGIONS 32u
40 #define MAX_VCPUS 256
41 #else
42 #define KVM_MAX_NUM_MEM_REGIONS 32u
43 #define MAX_VCPUS 16
44 #endif
46 /**
47 * \brief The KVM context
49 * The verbose KVM context
52 struct kvm_context {
53 /// ioctl to use to inject interrupts
54 int irqchip_inject_ioctl;
55 #ifdef KVM_CAP_IRQ_ROUTING
56 struct kvm_irq_routing *irq_routes;
57 int nr_allocated_irq_routes;
58 #endif
59 void *used_gsi_bitmap;
60 int max_gsi;
63 typedef struct kvm_context *kvm_context_t;
65 #include "kvm.h"
67 int kvm_arch_run(CPUState *env);
69 int handle_halt(CPUState *env);
71 int handle_shutdown(kvm_context_t kvm, CPUState *env);
72 void post_kvm_run(kvm_context_t kvm, CPUState *env);
73 int pre_kvm_run(kvm_context_t kvm, CPUState *env);
74 int handle_io_window(kvm_context_t kvm);
75 int try_push_interrupts(kvm_context_t kvm);
77 void kvm_create_irqchip(kvm_context_t kvm);
79 /*!
80 * \brief Start the VCPU
82 * This starts the VCPU and virtualization is started.\n
83 * \n
84 * This function will not return until any of these conditions are met:
85 * - An IO/MMIO handler does not return "0"
86 * - An exception that neither the guest OS, nor KVM can handle occurs
88 * \note This function will call the callbacks registered in kvm_init()
89 * to emulate those functions
90 * \note If you at any point want to interrupt the VCPU, kvm_run() will
91 * listen to the EINTR signal. This allows you to simulate external interrupts
92 * and asyncronous IO.
94 * \param kvm Pointer to the current kvm_context
95 * \param vcpu Which virtual CPU should be started
96 * \return 0 on success, but you really shouldn't expect this function to
97 * return except for when an error has occured, or when you have sent it
98 * an EINTR signal.
100 int kvm_run(CPUState *env);
103 * \brief Check if a vcpu is ready for interrupt injection
105 * This checks if vcpu interrupts are not masked by mov ss or sti.
107 * \param kvm Pointer to the current kvm_context
108 * \param vcpu Which virtual CPU should get dumped
109 * \return boolean indicating interrupt injection readiness
111 int kvm_is_ready_for_interrupt_injection(CPUState *env);
113 #if defined(__i386__) || defined(__x86_64__)
115 * \brief Simulate an external vectored interrupt
117 * This allows you to simulate an external vectored interrupt.
119 * \param kvm Pointer to the current kvm_context
120 * \param vcpu Which virtual CPU should get dumped
121 * \param irq Vector number
122 * \return 0 on success
124 int kvm_inject_irq(CPUState *env, unsigned irq);
125 #endif
127 int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status);
129 #ifdef KVM_CAP_IRQCHIP
131 * \brief Dump in kernel IRQCHIP contents
133 * Dump one of the in kernel irq chip devices, including PIC (master/slave)
134 * and IOAPIC into a kvm_irqchip structure
136 * \param kvm Pointer to the current kvm_context
137 * \param chip The irq chip device to be dumped
139 int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
142 * \brief Set in kernel IRQCHIP contents
144 * Write one of the in kernel irq chip devices, including PIC (master/slave)
145 * and IOAPIC
148 * \param kvm Pointer to the current kvm_context
149 * \param chip THe irq chip device to be written
151 int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
153 #if defined(__i386__) || defined(__x86_64__)
155 * \brief Get in kernel local APIC for vcpu
157 * Save the local apic state including the timer of a virtual CPU
159 * \param kvm Pointer to the current kvm_context
160 * \param vcpu Which virtual CPU should be accessed
161 * \param s Local apic state of the specific virtual CPU
163 int kvm_get_lapic(CPUState *env, struct kvm_lapic_state *s);
166 * \brief Set in kernel local APIC for vcpu
168 * Restore the local apic state including the timer of a virtual CPU
170 * \param kvm Pointer to the current kvm_context
171 * \param vcpu Which virtual CPU should be accessed
172 * \param s Local apic state of the specific virtual CPU
174 int kvm_set_lapic(CPUState *env, struct kvm_lapic_state *s);
176 #endif
179 * \brief Simulate an NMI
181 * This allows you to simulate a non-maskable interrupt.
183 * \param kvm Pointer to the current kvm_context
184 * \param vcpu Which virtual CPU should get dumped
185 * \return 0 on success
187 int kvm_inject_nmi(CPUState *env);
189 #endif
191 #ifdef KVM_CAP_PIT
193 #if defined(__i386__) || defined(__x86_64__)
195 * \brief Get in kernel PIT of the virtual domain
197 * Save the PIT state.
199 * \param kvm Pointer to the current kvm_context
200 * \param s PIT state of the virtual domain
202 int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
205 * \brief Set in kernel PIT of the virtual domain
207 * Restore the PIT state.
208 * Timer would be retriggerred after restored.
210 * \param kvm Pointer to the current kvm_context
211 * \param s PIT state of the virtual domain
213 int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
215 int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
217 #ifdef KVM_CAP_PIT_STATE2
219 * \brief Check for kvm support of kvm_pit_state2
221 * \param kvm Pointer to the current kvm_context
222 * \return 0 on success
224 int kvm_has_pit_state2(kvm_context_t kvm);
227 * \brief Set in kernel PIT state2 of the virtual domain
230 * \param kvm Pointer to the current kvm_context
231 * \param ps2 PIT state2 of the virtual domain
232 * \return 0 on success
234 int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
237 * \brief Get in kernel PIT state2 of the virtual domain
240 * \param kvm Pointer to the current kvm_context
241 * \param ps2 PIT state2 of the virtual domain
242 * \return 0 on success
244 int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
246 #endif
247 #endif
248 #endif
250 #ifdef KVM_CAP_VAPIC
252 int kvm_enable_vapic(CPUState *env, uint64_t vapic);
254 #endif
256 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
258 * \brief Notifies host kernel about a PCI device to be assigned to a guest
260 * Used for PCI device assignment, this function notifies the host
261 * kernel about the assigning of the physical PCI device to a guest.
263 * \param kvm Pointer to the current kvm_context
264 * \param assigned_dev Parameters, like bus, devfn number, etc
266 int kvm_assign_pci_device(kvm_context_t kvm,
267 struct kvm_assigned_pci_dev *assigned_dev);
270 * \brief Assign IRQ for an assigned device
272 * Used for PCI device assignment, this function assigns IRQ numbers for
273 * an physical device and guest IRQ handling.
275 * \param kvm Pointer to the current kvm_context
276 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
278 int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
280 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
282 * \brief Deassign IRQ for an assigned device
284 * Used for PCI device assignment, this function deassigns IRQ numbers
285 * for an assigned device.
287 * \param kvm Pointer to the current kvm_context
288 * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
290 int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
291 #endif
292 #endif
294 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
296 * \brief Notifies host kernel about a PCI device to be deassigned from a guest
298 * Used for hot remove PCI device, this function notifies the host
299 * kernel about the deassigning of the physical PCI device from a guest.
301 * \param kvm Pointer to the current kvm_context
302 * \param assigned_dev Parameters, like bus, devfn number, etc
304 int kvm_deassign_pci_device(kvm_context_t kvm,
305 struct kvm_assigned_pci_dev *assigned_dev);
306 #endif
309 * \brief Determines the number of gsis that can be routed
311 * Returns the number of distinct gsis that can be routed by kvm. This is
312 * also the number of distinct routes (if a gsi has two routes, than another
313 * gsi cannot be used...)
315 * \param kvm Pointer to the current kvm_context
317 int kvm_get_gsi_count(kvm_context_t kvm);
320 * \brief Clears the temporary irq routing table
322 * Clears the temporary irq routing table. Nothing is committed to the
323 * running VM.
326 int kvm_clear_gsi_routes(void);
329 * \brief Adds an irq route to the temporary irq routing table
331 * Adds an irq route to the temporary irq routing table. Nothing is
332 * committed to the running VM.
334 int kvm_add_irq_route(int gsi, int irqchip, int pin);
337 * \brief Removes an irq route from the temporary irq routing table
339 * Adds an irq route to the temporary irq routing table. Nothing is
340 * committed to the running VM.
342 int kvm_del_irq_route(int gsi, int irqchip, int pin);
344 struct kvm_irq_routing_entry;
346 * \brief Adds a routing entry to the temporary irq routing table
348 * Adds a filled routing entry to the temporary irq routing table. Nothing is
349 * committed to the running VM.
351 int kvm_add_routing_entry(struct kvm_irq_routing_entry *entry);
354 * \brief Removes a routing from the temporary irq routing table
356 * Remove a routing to the temporary irq routing table. Nothing is
357 * committed to the running VM.
359 int kvm_del_routing_entry(struct kvm_irq_routing_entry *entry);
362 * \brief Updates a routing in the temporary irq routing table
364 * Update a routing in the temporary irq routing table
365 * with a new value. entry type and GSI can not be changed.
366 * Nothing is committed to the running VM.
368 int kvm_update_routing_entry(struct kvm_irq_routing_entry *entry,
369 struct kvm_irq_routing_entry *newentry);
372 #ifdef KVM_CAP_DEVICE_MSIX
373 int kvm_assign_set_msix_nr(kvm_context_t kvm,
374 struct kvm_assigned_msix_nr *msix_nr);
375 int kvm_assign_set_msix_entry(kvm_context_t kvm,
376 struct kvm_assigned_msix_entry *entry);
377 #endif
379 #else /* !CONFIG_KVM */
381 typedef struct kvm_context *kvm_context_t;
383 struct kvm_pit_state {
386 #endif /* !CONFIG_KVM */
390 * \brief Create new KVM context
392 * This creates a new kvm_context. A KVM context is a small area of data that
393 * holds information about the KVM instance that gets created by this call.\n
394 * This should always be your first call to KVM.
396 * \param opaque Not used
397 * \return NULL on failure
399 int kvm_init(void);
401 int kvm_main_loop(void);
402 int kvm_init_ap(void);
403 void kvm_save_lapic(CPUState *env);
404 void kvm_load_lapic(CPUState *env);
406 void kvm_hpet_enable_kpit(void);
407 void kvm_hpet_disable_kpit(void);
409 void on_vcpu(CPUState *env, void (*func)(void *data), void *data);
410 void kvm_inject_interrupt(CPUState *env, int mask);
411 void kvm_update_interrupt_request(CPUState *env);
413 int kvm_arch_has_work(CPUState *env);
414 void kvm_arch_process_irqchip_events(CPUState *env);
415 int kvm_arch_try_push_interrupts(void *opaque);
416 void kvm_arch_push_nmi(void);
417 int kvm_set_boot_cpu_id(uint32_t id);
419 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
421 int kvm_arch_init_irq_routing(void);
423 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
424 struct ioperm_data;
426 void kvm_ioperm(CPUState *env, void *data);
427 void kvm_add_ioperm_data(struct ioperm_data *data);
428 void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num);
429 void kvm_arch_do_ioperm(void *_data);
430 #endif
432 #ifdef CONFIG_KVM
433 #include "qemu-queue.h"
435 extern int kvm_irqchip;
436 extern int kvm_pit;
437 extern int kvm_pit_reinject;
438 extern int kvm_nested;
439 extern kvm_context_t kvm_context;
440 extern unsigned int kvm_shadow_memory;
442 struct ioperm_data {
443 unsigned long start_port;
444 unsigned long num;
445 int turn_on;
446 QLIST_ENTRY(ioperm_data) entries;
449 int kvm_arch_halt(CPUState *env);
450 int handle_tpr_access(void *opaque, CPUState *env, uint64_t rip,
451 int is_write);
453 #define qemu_kvm_has_gsi_routing() kvm_has_gsi_routing()
454 #ifdef TARGET_I386
455 #define qemu_kvm_has_pit_state2() kvm_has_pit_state2(kvm_context)
456 #endif
457 #else
458 #define kvm_nested 0
459 #define qemu_kvm_has_gsi_routing() (0)
460 #ifdef TARGET_I386
461 #define qemu_kvm_has_pit_state2() (0)
462 #endif
463 #endif
465 #ifdef CONFIG_KVM
467 typedef struct KVMSlot {
468 target_phys_addr_t start_addr;
469 ram_addr_t memory_size;
470 ram_addr_t phys_offset;
471 int slot;
472 int flags;
473 } KVMSlot;
475 typedef struct kvm_dirty_log KVMDirtyLog;
477 struct KVMState {
478 KVMSlot slots[32];
479 int fd;
480 int vmfd;
481 int coalesced_mmio;
482 #ifdef KVM_CAP_COALESCED_MMIO
483 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
484 #endif
485 int broken_set_mem_region;
486 int migration_log;
487 int vcpu_events;
488 int robust_singlestep;
489 int debugregs;
490 #ifdef KVM_CAP_SET_GUEST_DEBUG
491 QTAILQ_HEAD(, kvm_sw_breakpoint) kvm_sw_breakpoints;
492 #endif
493 int irqchip_in_kernel;
494 int pit_in_kernel;
495 int xsave, xcrs;
496 int many_ioeventfds;
498 struct kvm_context kvm_context;
501 int kvm_tpr_enable_vapic(CPUState *env);
503 unsigned long kvm_get_thread_id(void);
504 int kvm_cpu_is_stopped(CPUState *env);
506 #endif
508 #endif