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