kvm.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * QEMU KVM support
  3. *
  4. * Copyright IBM, Corp. 2008
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  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.
  11. *
  12. */
  13. #ifndef QEMU_KVM_H
  14. #define QEMU_KVM_H
  15. #include <errno.h>
  16. #include "config-host.h"
  17. #include "qemu-queue.h"
  18. #ifdef CONFIG_KVM
  19. #include <linux/kvm.h>
  20. #endif
  21. extern int kvm_allowed;
  22. extern bool kvm_kernel_irqchip;
  23. #if defined CONFIG_KVM || !defined NEED_CPU_H
  24. #define kvm_enabled() (kvm_allowed)
  25. #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
  26. #else
  27. #define kvm_enabled() (0)
  28. #define kvm_irqchip_in_kernel() (false)
  29. #endif
  30. struct kvm_run;
  31. struct kvm_lapic_state;
  32. typedef struct KVMCapabilityInfo {
  33. const char *name;
  34. int value;
  35. } KVMCapabilityInfo;
  36. #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
  37. #define KVM_CAP_LAST_INFO { NULL, 0 }
  38. /* external API */
  39. int kvm_init(void);
  40. int kvm_has_sync_mmu(void);
  41. int kvm_has_vcpu_events(void);
  42. int kvm_has_robust_singlestep(void);
  43. int kvm_has_debugregs(void);
  44. int kvm_has_xsave(void);
  45. int kvm_has_xcrs(void);
  46. int kvm_has_pit_state2(void);
  47. int kvm_has_many_ioeventfds(void);
  48. int kvm_has_gsi_routing(void);
  49. int kvm_allows_irq0_override(void);
  50. #ifdef NEED_CPU_H
  51. int kvm_init_vcpu(CPUArchState *env);
  52. int kvm_cpu_exec(CPUArchState *env);
  53. #if !defined(CONFIG_USER_ONLY)
  54. void kvm_setup_guest_memory(void *start, size_t size);
  55. int kvm_coalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
  56. int kvm_uncoalesce_mmio_region(target_phys_addr_t start, ram_addr_t size);
  57. void kvm_flush_coalesced_mmio_buffer(void);
  58. #endif
  59. int kvm_insert_breakpoint(CPUArchState *current_env, target_ulong addr,
  60. target_ulong len, int type);
  61. int kvm_remove_breakpoint(CPUArchState *current_env, target_ulong addr,
  62. target_ulong len, int type);
  63. void kvm_remove_all_breakpoints(CPUArchState *current_env);
  64. int kvm_update_guest_debug(CPUArchState *env, unsigned long reinject_trap);
  65. #ifndef _WIN32
  66. int kvm_set_signal_mask(CPUArchState *env, const sigset_t *sigset);
  67. #endif
  68. int kvm_on_sigbus_vcpu(CPUArchState *env, int code, void *addr);
  69. int kvm_on_sigbus(int code, void *addr);
  70. /* internal API */
  71. struct KVMState;
  72. typedef struct KVMState KVMState;
  73. extern KVMState *kvm_state;
  74. int kvm_ioctl(KVMState *s, int type, ...);
  75. int kvm_vm_ioctl(KVMState *s, int type, ...);
  76. int kvm_vcpu_ioctl(CPUArchState *env, int type, ...);
  77. /* Arch specific hooks */
  78. extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
  79. void kvm_arch_pre_run(CPUArchState *env, struct kvm_run *run);
  80. void kvm_arch_post_run(CPUArchState *env, struct kvm_run *run);
  81. int kvm_arch_handle_exit(CPUArchState *env, struct kvm_run *run);
  82. int kvm_arch_process_async_events(CPUArchState *env);
  83. int kvm_arch_get_registers(CPUArchState *env);
  84. /* state subset only touched by the VCPU itself during runtime */
  85. #define KVM_PUT_RUNTIME_STATE 1
  86. /* state subset modified during VCPU reset */
  87. #define KVM_PUT_RESET_STATE 2
  88. /* full state set, modified during initialization or on vmload */
  89. #define KVM_PUT_FULL_STATE 3
  90. int kvm_arch_put_registers(CPUArchState *env, int level);
  91. int kvm_arch_init(KVMState *s);
  92. int kvm_arch_init_vcpu(CPUArchState *env);
  93. void kvm_arch_reset_vcpu(CPUArchState *env);
  94. int kvm_arch_on_sigbus_vcpu(CPUArchState *env, int code, void *addr);
  95. int kvm_arch_on_sigbus(int code, void *addr);
  96. void kvm_arch_init_irq_routing(KVMState *s);
  97. int kvm_irqchip_set_irq(KVMState *s, int irq, int level);
  98. void kvm_irqchip_add_route(KVMState *s, int gsi, int irqchip, int pin);
  99. int kvm_irqchip_commit_routes(KVMState *s);
  100. void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
  101. void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
  102. struct kvm_guest_debug;
  103. struct kvm_debug_exit_arch;
  104. struct kvm_sw_breakpoint {
  105. target_ulong pc;
  106. target_ulong saved_insn;
  107. int use_count;
  108. QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
  109. };
  110. QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
  111. struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUArchState *env,
  112. target_ulong pc);
  113. int kvm_sw_breakpoints_active(CPUArchState *env);
  114. int kvm_arch_insert_sw_breakpoint(CPUArchState *current_env,
  115. struct kvm_sw_breakpoint *bp);
  116. int kvm_arch_remove_sw_breakpoint(CPUArchState *current_env,
  117. struct kvm_sw_breakpoint *bp);
  118. int kvm_arch_insert_hw_breakpoint(target_ulong addr,
  119. target_ulong len, int type);
  120. int kvm_arch_remove_hw_breakpoint(target_ulong addr,
  121. target_ulong len, int type);
  122. void kvm_arch_remove_all_hw_breakpoints(void);
  123. void kvm_arch_update_guest_debug(CPUArchState *env, struct kvm_guest_debug *dbg);
  124. bool kvm_arch_stop_on_emulation_error(CPUArchState *env);
  125. int kvm_check_extension(KVMState *s, unsigned int extension);
  126. uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
  127. uint32_t index, int reg);
  128. void kvm_cpu_synchronize_state(CPUArchState *env);
  129. void kvm_cpu_synchronize_post_reset(CPUArchState *env);
  130. void kvm_cpu_synchronize_post_init(CPUArchState *env);
  131. /* generic hooks - to be moved/refactored once there are more users */
  132. static inline void cpu_synchronize_state(CPUArchState *env)
  133. {
  134. if (kvm_enabled()) {
  135. kvm_cpu_synchronize_state(env);
  136. }
  137. }
  138. static inline void cpu_synchronize_post_reset(CPUArchState *env)
  139. {
  140. if (kvm_enabled()) {
  141. kvm_cpu_synchronize_post_reset(env);
  142. }
  143. }
  144. static inline void cpu_synchronize_post_init(CPUArchState *env)
  145. {
  146. if (kvm_enabled()) {
  147. kvm_cpu_synchronize_post_init(env);
  148. }
  149. }
  150. #if !defined(CONFIG_USER_ONLY)
  151. int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
  152. target_phys_addr_t *phys_addr);
  153. #endif
  154. #endif
  155. int kvm_set_ioeventfd_mmio(int fd, uint32_t adr, uint32_t val, bool assign,
  156. uint32_t size);
  157. int kvm_set_ioeventfd_pio_word(int fd, uint16_t adr, uint16_t val, bool assign);
  158. #endif