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