kvm.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. /* header to be included in non-KVM-specific code */
  14. #ifndef QEMU_KVM_H
  15. #define QEMU_KVM_H
  16. #include "exec/memattrs.h"
  17. #include "qemu/accel.h"
  18. #include "qom/object.h"
  19. #ifdef COMPILING_PER_TARGET
  20. # ifdef CONFIG_KVM
  21. # include <linux/kvm.h>
  22. # define CONFIG_KVM_IS_POSSIBLE
  23. # endif
  24. #else
  25. # define CONFIG_KVM_IS_POSSIBLE
  26. #endif
  27. #ifdef CONFIG_KVM_IS_POSSIBLE
  28. extern bool kvm_allowed;
  29. extern bool kvm_kernel_irqchip;
  30. extern bool kvm_split_irqchip;
  31. extern bool kvm_async_interrupts_allowed;
  32. extern bool kvm_halt_in_kernel_allowed;
  33. extern bool kvm_resamplefds_allowed;
  34. extern bool kvm_msi_via_irqfd_allowed;
  35. extern bool kvm_gsi_routing_allowed;
  36. extern bool kvm_gsi_direct_mapping;
  37. extern bool kvm_readonly_mem_allowed;
  38. extern bool kvm_msi_use_devid;
  39. #define kvm_enabled() (kvm_allowed)
  40. /**
  41. * kvm_irqchip_in_kernel:
  42. *
  43. * Returns: true if an in-kernel irqchip was created.
  44. * What this actually means is architecture and machine model
  45. * specific: on PC, for instance, it means that the LAPIC
  46. * is in kernel. This function should never be used from generic
  47. * target-independent code: use one of the following functions or
  48. * some other specific check instead.
  49. */
  50. #define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
  51. /**
  52. * kvm_irqchip_is_split:
  53. *
  54. * Returns: true if the irqchip implementation is split between
  55. * user and kernel space. The details are architecture and
  56. * machine specific. On PC, it means that the PIC, IOAPIC, and
  57. * PIT are in user space while the LAPIC is in the kernel.
  58. */
  59. #define kvm_irqchip_is_split() (kvm_split_irqchip)
  60. /**
  61. * kvm_async_interrupts_enabled:
  62. *
  63. * Returns: true if we can deliver interrupts to KVM
  64. * asynchronously (ie by ioctl from any thread at any time)
  65. * rather than having to do interrupt delivery synchronously
  66. * (where the vcpu must be stopped at a suitable point first).
  67. */
  68. #define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
  69. /**
  70. * kvm_halt_in_kernel
  71. *
  72. * Returns: true if halted cpus should still get a KVM_RUN ioctl to run
  73. * inside of kernel space. This only works if MP state is implemented.
  74. */
  75. #define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed)
  76. /**
  77. * kvm_irqfds_enabled:
  78. *
  79. * Returns: true if we can use irqfds to inject interrupts into
  80. * a KVM CPU (ie the kernel supports irqfds and we are running
  81. * with a configuration where it is meaningful to use them).
  82. *
  83. * Always available if running with in-kernel irqchip.
  84. */
  85. #define kvm_irqfds_enabled() kvm_irqchip_in_kernel()
  86. /**
  87. * kvm_resamplefds_enabled:
  88. *
  89. * Returns: true if we can use resamplefds to inject interrupts into
  90. * a KVM CPU (ie the kernel supports resamplefds and we are running
  91. * with a configuration where it is meaningful to use them).
  92. */
  93. #define kvm_resamplefds_enabled() (kvm_resamplefds_allowed)
  94. /**
  95. * kvm_msi_via_irqfd_enabled:
  96. *
  97. * Returns: true if we can route a PCI MSI (Message Signaled Interrupt)
  98. * to a KVM CPU via an irqfd. This requires that the kernel supports
  99. * this and that we're running in a configuration that permits it.
  100. */
  101. #define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
  102. /**
  103. * kvm_gsi_routing_enabled:
  104. *
  105. * Returns: true if GSI routing is enabled (ie the kernel supports
  106. * it and we're running in a configuration that permits it).
  107. */
  108. #define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
  109. /**
  110. * kvm_gsi_direct_mapping:
  111. *
  112. * Returns: true if GSI direct mapping is enabled.
  113. */
  114. #define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping)
  115. /**
  116. * kvm_readonly_mem_enabled:
  117. *
  118. * Returns: true if KVM readonly memory is enabled (ie the kernel
  119. * supports it and we're running in a configuration that permits it).
  120. */
  121. #define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed)
  122. /**
  123. * kvm_msi_devid_required:
  124. * Returns: true if KVM requires a device id to be provided while
  125. * defining an MSI routing entry.
  126. */
  127. #define kvm_msi_devid_required() (kvm_msi_use_devid)
  128. #else
  129. #define kvm_enabled() (0)
  130. #define kvm_irqchip_in_kernel() (false)
  131. #define kvm_irqchip_is_split() (false)
  132. #define kvm_async_interrupts_enabled() (false)
  133. #define kvm_halt_in_kernel() (false)
  134. #define kvm_irqfds_enabled() (false)
  135. #define kvm_resamplefds_enabled() (false)
  136. #define kvm_msi_via_irqfd_enabled() (false)
  137. #define kvm_gsi_routing_allowed() (false)
  138. #define kvm_gsi_direct_mapping() (false)
  139. #define kvm_readonly_mem_enabled() (false)
  140. #define kvm_msi_devid_required() (false)
  141. #endif /* CONFIG_KVM_IS_POSSIBLE */
  142. struct kvm_run;
  143. struct kvm_irq_routing_entry;
  144. typedef struct KVMCapabilityInfo {
  145. const char *name;
  146. int value;
  147. } KVMCapabilityInfo;
  148. #define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
  149. #define KVM_CAP_LAST_INFO { NULL, 0 }
  150. struct KVMState;
  151. #define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm")
  152. typedef struct KVMState KVMState;
  153. DECLARE_INSTANCE_CHECKER(KVMState, KVM_STATE,
  154. TYPE_KVM_ACCEL)
  155. extern KVMState *kvm_state;
  156. typedef struct Notifier Notifier;
  157. typedef struct KVMRouteChange {
  158. KVMState *s;
  159. int changes;
  160. } KVMRouteChange;
  161. /* external API */
  162. unsigned int kvm_get_max_memslots(void);
  163. unsigned int kvm_get_free_memslots(void);
  164. bool kvm_has_sync_mmu(void);
  165. int kvm_has_vcpu_events(void);
  166. int kvm_max_nested_state_length(void);
  167. int kvm_has_gsi_routing(void);
  168. /**
  169. * kvm_arm_supports_user_irq
  170. *
  171. * Not all KVM implementations support notifications for kernel generated
  172. * interrupt events to user space. This function indicates whether the current
  173. * KVM implementation does support them.
  174. *
  175. * Returns: true if KVM supports using kernel generated IRQs from user space
  176. */
  177. bool kvm_arm_supports_user_irq(void);
  178. int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
  179. int kvm_on_sigbus(int code, void *addr);
  180. #ifdef COMPILING_PER_TARGET
  181. #include "cpu.h"
  182. void kvm_flush_coalesced_mmio_buffer(void);
  183. /**
  184. * kvm_update_guest_debug(): ensure KVM debug structures updated
  185. * @cs: the CPUState for this cpu
  186. * @reinject_trap: KVM trap injection control
  187. *
  188. * There are usually per-arch specifics which will be handled by
  189. * calling down to kvm_arch_update_guest_debug after the generic
  190. * fields have been set.
  191. */
  192. #ifdef TARGET_KVM_HAVE_GUEST_DEBUG
  193. int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap);
  194. #else
  195. static inline int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
  196. {
  197. return -EINVAL;
  198. }
  199. #endif
  200. /* internal API */
  201. int kvm_ioctl(KVMState *s, unsigned long type, ...);
  202. int kvm_vm_ioctl(KVMState *s, unsigned long type, ...);
  203. int kvm_vcpu_ioctl(CPUState *cpu, unsigned long type, ...);
  204. /**
  205. * kvm_device_ioctl - call an ioctl on a kvm device
  206. * @fd: The KVM device file descriptor as returned from KVM_CREATE_DEVICE
  207. * @type: The device-ctrl ioctl number
  208. *
  209. * Returns: -errno on error, nonnegative on success
  210. */
  211. int kvm_device_ioctl(int fd, unsigned long type, ...);
  212. /**
  213. * kvm_vm_check_attr - check for existence of a specific vm attribute
  214. * @s: The KVMState pointer
  215. * @group: the group
  216. * @attr: the attribute of that group to query for
  217. *
  218. * Returns: 1 if the attribute exists
  219. * 0 if the attribute either does not exist or if the vm device
  220. * interface is unavailable
  221. */
  222. int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr);
  223. /**
  224. * kvm_device_check_attr - check for existence of a specific device attribute
  225. * @fd: The device file descriptor
  226. * @group: the group
  227. * @attr: the attribute of that group to query for
  228. *
  229. * Returns: 1 if the attribute exists
  230. * 0 if the attribute either does not exist or if the vm device
  231. * interface is unavailable
  232. */
  233. int kvm_device_check_attr(int fd, uint32_t group, uint64_t attr);
  234. /**
  235. * kvm_device_access - set or get value of a specific device attribute
  236. * @fd: The device file descriptor
  237. * @group: the group
  238. * @attr: the attribute of that group to set or get
  239. * @val: pointer to a storage area for the value
  240. * @write: true for set and false for get operation
  241. * @errp: error object handle
  242. *
  243. * Returns: 0 on success
  244. * < 0 on error
  245. * Use kvm_device_check_attr() in order to check for the availability
  246. * of optional attributes.
  247. */
  248. int kvm_device_access(int fd, int group, uint64_t attr,
  249. void *val, bool write, Error **errp);
  250. /**
  251. * kvm_create_device - create a KVM device for the device control API
  252. * @KVMState: The KVMState pointer
  253. * @type: The KVM device type (see Documentation/virtual/kvm/devices in the
  254. * kernel source)
  255. * @test: If true, only test if device can be created, but don't actually
  256. * create the device.
  257. *
  258. * Returns: -errno on error, nonnegative on success: @test ? 0 : device fd;
  259. */
  260. int kvm_create_device(KVMState *s, uint64_t type, bool test);
  261. /**
  262. * kvm_device_supported - probe whether KVM supports specific device
  263. *
  264. * @vmfd: The fd handler for VM
  265. * @type: type of device
  266. *
  267. * @return: true if supported, otherwise false.
  268. */
  269. bool kvm_device_supported(int vmfd, uint64_t type);
  270. /**
  271. * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
  272. * @cpu: QOM CPUState object for which KVM vCPU has to be fetched/created.
  273. *
  274. * @returns: 0 when success, errno (<0) when failed.
  275. */
  276. int kvm_create_vcpu(CPUState *cpu);
  277. /**
  278. * kvm_park_vcpu - Park QEMU KVM vCPU context
  279. * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be parked.
  280. *
  281. * @returns: none
  282. */
  283. void kvm_park_vcpu(CPUState *cpu);
  284. /**
  285. * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
  286. * @s: KVM State
  287. * @vcpu_id: Architecture vCPU ID of the parked vCPU
  288. *
  289. * @returns: KVM fd
  290. */
  291. int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
  292. /**
  293. * kvm_create_and_park_vcpu - Create and park a KVM vCPU
  294. * @cpu: QOM CPUState object for which KVM vCPU has to be created and parked.
  295. *
  296. * @returns: 0 when success, errno (<0) when failed.
  297. */
  298. int kvm_create_and_park_vcpu(CPUState *cpu);
  299. /* Arch specific hooks */
  300. extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
  301. void kvm_arch_accel_class_init(ObjectClass *oc);
  302. void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run);
  303. MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run);
  304. int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run);
  305. int kvm_arch_process_async_events(CPUState *cpu);
  306. int kvm_arch_get_registers(CPUState *cpu, Error **errp);
  307. /* state subset only touched by the VCPU itself during runtime */
  308. #define KVM_PUT_RUNTIME_STATE 1
  309. /* state subset modified during VCPU reset */
  310. #define KVM_PUT_RESET_STATE 2
  311. /* full state set, modified during initialization or on vmload */
  312. #define KVM_PUT_FULL_STATE 3
  313. int kvm_arch_put_registers(CPUState *cpu, int level, Error **errp);
  314. int kvm_arch_get_default_type(MachineState *ms);
  315. int kvm_arch_init(MachineState *ms, KVMState *s);
  316. int kvm_arch_init_vcpu(CPUState *cpu);
  317. int kvm_arch_destroy_vcpu(CPUState *cpu);
  318. bool kvm_vcpu_id_is_valid(int vcpu_id);
  319. /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
  320. unsigned long kvm_arch_vcpu_id(CPUState *cpu);
  321. #ifdef KVM_HAVE_MCE_INJECTION
  322. void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
  323. #endif
  324. void kvm_arch_init_irq_routing(KVMState *s);
  325. int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
  326. uint64_t address, uint32_t data, PCIDevice *dev);
  327. /* Notify arch about newly added MSI routes */
  328. int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
  329. int vector, PCIDevice *dev);
  330. /* Notify arch about released MSI routes */
  331. int kvm_arch_release_virq_post(int virq);
  332. int kvm_arch_msi_data_to_gsi(uint32_t data);
  333. int kvm_set_irq(KVMState *s, int irq, int level);
  334. int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
  335. void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
  336. void kvm_irqchip_add_change_notifier(Notifier *n);
  337. void kvm_irqchip_remove_change_notifier(Notifier *n);
  338. void kvm_irqchip_change_notify(void);
  339. struct kvm_guest_debug;
  340. struct kvm_debug_exit_arch;
  341. struct kvm_sw_breakpoint {
  342. vaddr pc;
  343. vaddr saved_insn;
  344. int use_count;
  345. QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
  346. };
  347. struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
  348. vaddr pc);
  349. int kvm_sw_breakpoints_active(CPUState *cpu);
  350. int kvm_arch_insert_sw_breakpoint(CPUState *cpu,
  351. struct kvm_sw_breakpoint *bp);
  352. int kvm_arch_remove_sw_breakpoint(CPUState *cpu,
  353. struct kvm_sw_breakpoint *bp);
  354. int kvm_arch_insert_hw_breakpoint(vaddr addr, vaddr len, int type);
  355. int kvm_arch_remove_hw_breakpoint(vaddr addr, vaddr len, int type);
  356. void kvm_arch_remove_all_hw_breakpoints(void);
  357. void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg);
  358. bool kvm_arch_stop_on_emulation_error(CPUState *cpu);
  359. int kvm_check_extension(KVMState *s, unsigned int extension);
  360. int kvm_vm_check_extension(KVMState *s, unsigned int extension);
  361. #define kvm_vm_enable_cap(s, capability, cap_flags, ...) \
  362. ({ \
  363. struct kvm_enable_cap cap = { \
  364. .cap = capability, \
  365. .flags = cap_flags, \
  366. }; \
  367. uint64_t args_tmp[] = { __VA_ARGS__ }; \
  368. size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \
  369. memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \
  370. kvm_vm_ioctl(s, KVM_ENABLE_CAP, &cap); \
  371. })
  372. #define kvm_vcpu_enable_cap(cpu, capability, cap_flags, ...) \
  373. ({ \
  374. struct kvm_enable_cap cap = { \
  375. .cap = capability, \
  376. .flags = cap_flags, \
  377. }; \
  378. uint64_t args_tmp[] = { __VA_ARGS__ }; \
  379. size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \
  380. memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \
  381. kvm_vcpu_ioctl(cpu, KVM_ENABLE_CAP, &cap); \
  382. })
  383. void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len);
  384. int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
  385. hwaddr *phys_addr);
  386. #endif /* COMPILING_PER_TARGET */
  387. void kvm_cpu_synchronize_state(CPUState *cpu);
  388. void kvm_init_cpu_signals(CPUState *cpu);
  389. /**
  390. * kvm_irqchip_add_msi_route - Add MSI route for specific vector
  391. * @c: KVMRouteChange instance.
  392. * @vector: which vector to add. This can be either MSI/MSIX
  393. * vector. The function will automatically detect whether
  394. * MSI/MSIX is enabled, and fetch corresponding MSI
  395. * message.
  396. * @dev: Owner PCI device to add the route. If @dev is specified
  397. * as @NULL, an empty MSI message will be inited.
  398. * @return: virq (>=0) when success, errno (<0) when failed.
  399. */
  400. int kvm_irqchip_add_msi_route(KVMRouteChange *c, int vector, PCIDevice *dev);
  401. int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg,
  402. PCIDevice *dev);
  403. void kvm_irqchip_commit_routes(KVMState *s);
  404. static inline KVMRouteChange kvm_irqchip_begin_route_changes(KVMState *s)
  405. {
  406. return (KVMRouteChange) { .s = s, .changes = 0 };
  407. }
  408. static inline void kvm_irqchip_commit_route_changes(KVMRouteChange *c)
  409. {
  410. if (c->changes) {
  411. kvm_irqchip_commit_routes(c->s);
  412. c->changes = 0;
  413. }
  414. }
  415. int kvm_irqchip_get_virq(KVMState *s);
  416. void kvm_irqchip_release_virq(KVMState *s, int virq);
  417. void kvm_add_routing_entry(KVMState *s,
  418. struct kvm_irq_routing_entry *entry);
  419. int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
  420. EventNotifier *rn, int virq);
  421. int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
  422. int virq);
  423. int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n,
  424. EventNotifier *rn, qemu_irq irq);
  425. int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n,
  426. qemu_irq irq);
  427. void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi);
  428. void kvm_init_irq_routing(KVMState *s);
  429. bool kvm_kernel_irqchip_allowed(void);
  430. bool kvm_kernel_irqchip_required(void);
  431. bool kvm_kernel_irqchip_split(void);
  432. /**
  433. * kvm_arch_irqchip_create:
  434. * @KVMState: The KVMState pointer
  435. *
  436. * Allow architectures to create an in-kernel irq chip themselves.
  437. *
  438. * Returns: < 0: error
  439. * 0: irq chip was not created
  440. * > 0: irq chip was created
  441. */
  442. int kvm_arch_irqchip_create(KVMState *s);
  443. /**
  444. * kvm_set_one_reg - set a register value in KVM via KVM_SET_ONE_REG ioctl
  445. * @id: The register ID
  446. * @source: The pointer to the value to be set. It must point to a variable
  447. * of the correct type/size for the register being accessed.
  448. *
  449. * Returns: 0 on success, or a negative errno on failure.
  450. */
  451. int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source);
  452. /**
  453. * kvm_get_one_reg - get a register value from KVM via KVM_GET_ONE_REG ioctl
  454. * @id: The register ID
  455. * @target: The pointer where the value is to be stored. It must point to a
  456. * variable of the correct type/size for the register being accessed.
  457. *
  458. * Returns: 0 on success, or a negative errno on failure.
  459. */
  460. int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target);
  461. /* Notify resamplefd for EOI of specific interrupts. */
  462. void kvm_resample_fd_notify(int gsi);
  463. bool kvm_dirty_ring_enabled(void);
  464. uint32_t kvm_dirty_ring_size(void);
  465. void kvm_mark_guest_state_protected(void);
  466. /**
  467. * kvm_hwpoisoned_mem - indicate if there is any hwpoisoned page
  468. * reported for the VM.
  469. */
  470. bool kvm_hwpoisoned_mem(void);
  471. int kvm_create_guest_memfd(uint64_t size, uint64_t flags, Error **errp);
  472. int kvm_set_memory_attributes_private(hwaddr start, uint64_t size);
  473. int kvm_set_memory_attributes_shared(hwaddr start, uint64_t size);
  474. int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private);
  475. #endif