blob: a783c7886811621c53cd8227fb4eec4a85d78e3c [file] [log] [blame]
aliguori05330442008-11-05 16:29:27 +00001/*
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#ifndef QEMU_KVM_H
15#define QEMU_KVM_H
16
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/queue.h"
Markus Armbruster2e5b09f2019-07-09 17:20:52 +020018#include "hw/core/cpu.h"
Paolo Bonzini4c663752015-04-08 13:30:58 +020019#include "exec/memattrs.h"
Claudio Fontana940e43a2021-02-04 17:39:24 +010020#include "qemu/accel.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040021#include "qom/object.h"
aliguori05330442008-11-05 16:29:27 +000022
Thomas Huthcbca3722017-06-26 07:22:54 +020023#ifdef NEED_CPU_H
24# ifdef CONFIG_KVM
25# include <linux/kvm.h>
Thomas Huthcbca3722017-06-26 07:22:54 +020026# define CONFIG_KVM_IS_POSSIBLE
27# endif
28#else
29# define CONFIG_KVM_IS_POSSIBLE
Michael S. Tsirkinca821802010-03-17 13:07:54 +020030#endif
aliguori05330442008-11-05 16:29:27 +000031
Thomas Huthcbca3722017-06-26 07:22:54 +020032#ifdef CONFIG_KVM_IS_POSSIBLE
33
liguangd5286af2013-01-24 13:03:27 +080034extern bool kvm_allowed;
Jan Kiszka3d4b2642012-01-31 19:17:52 +010035extern bool kvm_kernel_irqchip;
Matt Gingell32c18a22015-11-16 10:03:06 -080036extern bool kvm_split_irqchip;
Peter Maydell7ae26bd2012-07-26 15:35:11 +010037extern bool kvm_async_interrupts_allowed;
Alexander Graf215e79c2013-04-24 22:24:12 +020038extern bool kvm_halt_in_kernel_allowed;
Nikolay Nikolaev69e03ae2014-05-27 15:03:35 +030039extern bool kvm_eventfds_allowed;
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010040extern bool kvm_irqfds_allowed;
Eric Augerf41389a2014-10-31 13:38:18 +000041extern bool kvm_resamplefds_allowed;
Peter Maydell614e41b2012-07-26 15:35:15 +010042extern bool kvm_msi_via_irqfd_allowed;
Peter Maydellf3e1bed2012-07-26 15:35:16 +010043extern bool kvm_gsi_routing_allowed;
Alexey Kardashevskiy76fe21d2013-09-03 18:08:25 +100044extern bool kvm_gsi_direct_mapping;
Jordan Justendf9c8b72013-05-29 01:27:25 -070045extern bool kvm_readonly_mem_allowed;
Pavel Fedin50bf31b2015-10-15 16:44:50 +030046extern bool kvm_direct_msi_allowed;
Jason Wang35108222015-11-06 16:02:46 +080047extern bool kvm_ioeventfd_any_length_allowed;
Pavel Fedin767a5542016-10-04 13:28:09 +010048extern bool kvm_msi_use_devid;
Maxim Levitsky12bc5b42021-11-11 12:06:03 +010049extern bool kvm_has_guest_debug;
50extern int kvm_sstep_flags;
Paolo Bonzini98c85732010-04-19 18:59:30 +000051
Jan Kiszka3d4b2642012-01-31 19:17:52 +010052#define kvm_enabled() (kvm_allowed)
Peter Maydell96fda352012-07-26 15:35:17 +010053/**
54 * kvm_irqchip_in_kernel:
55 *
Eduardo Habkost31c707f2020-09-22 16:36:12 -040056 * Returns: true if an in-kernel irqchip was created.
Peter Maydell96fda352012-07-26 15:35:17 +010057 * What this actually means is architecture and machine model
Eduardo Habkost31c707f2020-09-22 16:36:12 -040058 * specific: on PC, for instance, it means that the LAPIC
59 * is in kernel. This function should never be used from generic
60 * target-independent code: use one of the following functions or
61 * some other specific check instead.
Peter Maydell96fda352012-07-26 15:35:17 +010062 */
Jan Kiszka3d4b2642012-01-31 19:17:52 +010063#define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
Peter Maydell7ae26bd2012-07-26 15:35:11 +010064
65/**
Matt Gingell32c18a22015-11-16 10:03:06 -080066 * kvm_irqchip_is_split:
67 *
Eduardo Habkost31c707f2020-09-22 16:36:12 -040068 * Returns: true if the irqchip implementation is split between
69 * user and kernel space. The details are architecture and
70 * machine specific. On PC, it means that the PIC, IOAPIC, and
71 * PIT are in user space while the LAPIC is in the kernel.
Matt Gingell32c18a22015-11-16 10:03:06 -080072 */
73#define kvm_irqchip_is_split() (kvm_split_irqchip)
74
75/**
Peter Maydell7ae26bd2012-07-26 15:35:11 +010076 * kvm_async_interrupts_enabled:
77 *
78 * Returns: true if we can deliver interrupts to KVM
79 * asynchronously (ie by ioctl from any thread at any time)
80 * rather than having to do interrupt delivery synchronously
81 * (where the vcpu must be stopped at a suitable point first).
82 */
83#define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
84
Peter Maydellcc7e0dd2012-07-26 15:35:14 +010085/**
Alexander Graf215e79c2013-04-24 22:24:12 +020086 * kvm_halt_in_kernel
87 *
88 * Returns: true if halted cpus should still get a KVM_RUN ioctl to run
89 * inside of kernel space. This only works if MP state is implemented.
90 */
91#define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed)
92
93/**
Nikolay Nikolaev69e03ae2014-05-27 15:03:35 +030094 * kvm_eventfds_enabled:
95 *
96 * Returns: true if we can use eventfds to receive notifications
97 * from a KVM CPU (ie the kernel supports eventds and we are running
98 * with a configuration where it is meaningful to use them).
99 */
100#define kvm_eventfds_enabled() (kvm_eventfds_allowed)
101
102/**
Peter Maydellcc7e0dd2012-07-26 15:35:14 +0100103 * kvm_irqfds_enabled:
104 *
105 * Returns: true if we can use irqfds to inject interrupts into
106 * a KVM CPU (ie the kernel supports irqfds and we are running
107 * with a configuration where it is meaningful to use them).
108 */
109#define kvm_irqfds_enabled() (kvm_irqfds_allowed)
110
Peter Maydell614e41b2012-07-26 15:35:15 +0100111/**
Eric Augerf41389a2014-10-31 13:38:18 +0000112 * kvm_resamplefds_enabled:
113 *
114 * Returns: true if we can use resamplefds to inject interrupts into
115 * a KVM CPU (ie the kernel supports resamplefds and we are running
116 * with a configuration where it is meaningful to use them).
117 */
118#define kvm_resamplefds_enabled() (kvm_resamplefds_allowed)
119
120/**
Peter Maydell614e41b2012-07-26 15:35:15 +0100121 * kvm_msi_via_irqfd_enabled:
122 *
123 * Returns: true if we can route a PCI MSI (Message Signaled Interrupt)
124 * to a KVM CPU via an irqfd. This requires that the kernel supports
125 * this and that we're running in a configuration that permits it.
126 */
127#define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
128
Peter Maydellf3e1bed2012-07-26 15:35:16 +0100129/**
130 * kvm_gsi_routing_enabled:
131 *
132 * Returns: true if GSI routing is enabled (ie the kernel supports
133 * it and we're running in a configuration that permits it).
134 */
135#define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
136
Jordan Justendf9c8b72013-05-29 01:27:25 -0700137/**
Alexey Kardashevskiy76fe21d2013-09-03 18:08:25 +1000138 * kvm_gsi_direct_mapping:
139 *
140 * Returns: true if GSI direct mapping is enabled.
141 */
142#define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping)
143
144/**
Jordan Justendf9c8b72013-05-29 01:27:25 -0700145 * kvm_readonly_mem_enabled:
146 *
147 * Returns: true if KVM readonly memory is enabled (ie the kernel
148 * supports it and we're running in a configuration that permits it).
149 */
150#define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed)
151
Pavel Fedin50bf31b2015-10-15 16:44:50 +0300152/**
153 * kvm_direct_msi_enabled:
154 *
155 * Returns: true if KVM allows direct MSI injection.
156 */
157#define kvm_direct_msi_enabled() (kvm_direct_msi_allowed)
158
Jason Wang35108222015-11-06 16:02:46 +0800159/**
160 * kvm_ioeventfd_any_length_enabled:
161 * Returns: true if KVM allows any length io eventfd.
162 */
163#define kvm_ioeventfd_any_length_enabled() (kvm_ioeventfd_any_length_allowed)
164
Pavel Fedin767a5542016-10-04 13:28:09 +0100165/**
166 * kvm_msi_devid_required:
167 * Returns: true if KVM requires a device id to be provided while
168 * defining an MSI routing entry.
169 */
170#define kvm_msi_devid_required() (kvm_msi_use_devid)
171
Maxim Levitsky12bc5b42021-11-11 12:06:03 +0100172/*
173 * Does KVM support guest debugging
174 */
175#define kvm_supports_guest_debug() (kvm_has_guest_debug)
176
177/*
178 * kvm_supported_sstep_flags
179 * Returns: SSTEP_* flags that KVM supports for guest debug
180 */
181#define kvm_get_supported_sstep_flags() (kvm_sstep_flags)
182
aliguori05330442008-11-05 16:29:27 +0000183#else
Thomas Huthcbca3722017-06-26 07:22:54 +0200184
Jan Kiszka3d4b2642012-01-31 19:17:52 +0100185#define kvm_enabled() (0)
186#define kvm_irqchip_in_kernel() (false)
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100187#define kvm_irqchip_is_split() (false)
Peter Maydell7ae26bd2012-07-26 15:35:11 +0100188#define kvm_async_interrupts_enabled() (false)
Alexander Graf215e79c2013-04-24 22:24:12 +0200189#define kvm_halt_in_kernel() (false)
Nikolay Nikolaev69e03ae2014-05-27 15:03:35 +0300190#define kvm_eventfds_enabled() (false)
Peter Maydellcc7e0dd2012-07-26 15:35:14 +0100191#define kvm_irqfds_enabled() (false)
Eric Auger879904e2015-07-06 12:15:14 -0600192#define kvm_resamplefds_enabled() (false)
Peter Maydell614e41b2012-07-26 15:35:15 +0100193#define kvm_msi_via_irqfd_enabled() (false)
Peter Maydellf3e1bed2012-07-26 15:35:16 +0100194#define kvm_gsi_routing_allowed() (false)
Alexey Kardashevskiy76fe21d2013-09-03 18:08:25 +1000195#define kvm_gsi_direct_mapping() (false)
Jordan Justendf9c8b72013-05-29 01:27:25 -0700196#define kvm_readonly_mem_enabled() (false)
Pavel Fedin50bf31b2015-10-15 16:44:50 +0300197#define kvm_direct_msi_enabled() (false)
Jason Wang35108222015-11-06 16:02:46 +0800198#define kvm_ioeventfd_any_length_enabled() (false)
Pavel Fedin767a5542016-10-04 13:28:09 +0100199#define kvm_msi_devid_required() (false)
Maxim Levitsky12bc5b42021-11-11 12:06:03 +0100200#define kvm_supports_guest_debug() (false)
201#define kvm_get_supported_sstep_flags() (0)
Thomas Huthcbca3722017-06-26 07:22:54 +0200202
203#endif /* CONFIG_KVM_IS_POSSIBLE */
aliguori05330442008-11-05 16:29:27 +0000204
205struct kvm_run;
Jan Kiszka680c1c62011-10-16 13:23:26 +0200206struct kvm_lapic_state;
Frank Blaschka9e03a042015-01-09 09:04:40 +0100207struct kvm_irq_routing_entry;
aliguori05330442008-11-05 16:29:27 +0000208
Jan Kiszka94a8d392011-01-21 21:48:17 +0100209typedef struct KVMCapabilityInfo {
210 const char *name;
211 int value;
212} KVMCapabilityInfo;
213
214#define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
215#define KVM_CAP_LAST_INFO { NULL, 0 }
216
Jan Kiszka92b4e482012-05-17 10:32:33 -0300217struct KVMState;
Eduardo Habkost97e622d2020-08-25 15:20:37 -0400218
219#define TYPE_KVM_ACCEL ACCEL_CLASS_NAME("kvm")
Jan Kiszka92b4e482012-05-17 10:32:33 -0300220typedef struct KVMState KVMState;
Eduardo Habkost8110fa12020-08-31 17:07:33 -0400221DECLARE_INSTANCE_CHECKER(KVMState, KVM_STATE,
222 TYPE_KVM_ACCEL)
Eduardo Habkost97e622d2020-08-25 15:20:37 -0400223
Jan Kiszka92b4e482012-05-17 10:32:33 -0300224extern KVMState *kvm_state;
David Gibson36077152019-10-17 12:12:35 +1100225typedef struct Notifier Notifier;
Jan Kiszka92b4e482012-05-17 10:32:33 -0300226
Longpeng(Mike)95686902022-02-22 22:11:15 +0800227typedef struct KVMRouteChange {
228 KVMState *s;
229 int changes;
230} KVMRouteChange;
231
aliguori05330442008-11-05 16:29:27 +0000232/* external API */
233
Igor Mammedovb8865592014-10-31 16:38:32 +0000234bool kvm_has_free_slot(MachineState *ms);
Greg Kurz62dd4ed2017-09-21 18:00:53 +0200235bool kvm_has_sync_mmu(void);
Paolo Bonzini00a15552010-04-01 19:57:11 +0200236int kvm_has_vcpu_events(void);
237int kvm_has_robust_singlestep(void);
Jan Kiszkaff44f1a2010-03-12 15:20:49 +0100238int kvm_has_debugregs(void);
Liran Alonebbfef22019-06-19 19:21:38 +0300239int kvm_max_nested_state_length(void);
Jan Kiszka8a7c7392012-03-02 20:28:48 +0100240int kvm_has_pit_state2(void);
Stefan Hajnoczid2f2b8a2011-01-10 13:50:05 +0200241int kvm_has_many_ioeventfds(void);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200242int kvm_has_gsi_routing(void);
Jan Kiszka3ab73842012-08-27 08:28:39 +0200243int kvm_has_intx_set_mask(void);
Paolo Bonzini00a15552010-04-01 19:57:11 +0200244
Alexander Graf5d721b72017-07-11 11:21:26 +0100245/**
246 * kvm_arm_supports_user_irq
247 *
248 * Not all KVM implementations support notifications for kernel generated
249 * interrupt events to user space. This function indicates whether the current
250 * KVM implementation does support them.
251 *
252 * Returns: true if KVM supports using kernel generated IRQs from user space
253 */
254bool kvm_arm_supports_user_irq(void);
255
Brijesh Singhb20e3782018-03-08 06:48:44 -0600256
Philippe Mathieu-Daudé82bd4ca2021-05-16 19:01:46 +0200257int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
258int kvm_on_sigbus(int code, void *addr);
259
Andreas Färber504134d2012-12-17 06:38:45 +0100260#ifdef NEED_CPU_H
Paolo Bonzini33c11872016-03-15 16:58:45 +0100261#include "cpu.h"
aliguori05330442008-11-05 16:29:27 +0000262
Igor Mammedovc4cfef52013-04-23 10:29:35 +0200263void kvm_flush_coalesced_mmio_buffer(void);
264
Andreas Färber62278812013-06-27 17:12:06 +0200265int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +0000266 target_ulong len, int type);
Andreas Färber62278812013-06-27 17:12:06 +0200267int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
aliguorie22a25c2009-03-12 20:12:48 +0000268 target_ulong len, int type);
Andreas Färber1d5791f2013-05-27 14:40:48 +0200269void kvm_remove_all_breakpoints(CPUState *cpu);
Stefan Weil38e478e2013-07-25 20:50:21 +0200270int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap);
aliguorie22a25c2009-03-12 20:12:48 +0000271
aliguori05330442008-11-05 16:29:27 +0000272/* internal API */
273
aliguori984b5182008-11-13 19:21:00 +0000274int kvm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000275
aliguori984b5182008-11-13 19:21:00 +0000276int kvm_vm_ioctl(KVMState *s, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000277
Andreas Färber1bc22652012-10-31 06:06:49 +0100278int kvm_vcpu_ioctl(CPUState *cpu, int type, ...);
aliguori05330442008-11-05 16:29:27 +0000279
Christoffer Dall0a6a7cc2014-02-26 17:20:00 +0000280/**
281 * kvm_device_ioctl - call an ioctl on a kvm device
282 * @fd: The KVM device file descriptor as returned from KVM_CREATE_DEVICE
283 * @type: The device-ctrl ioctl number
284 *
285 * Returns: -errno on error, nonnegative on success
286 */
287int kvm_device_ioctl(int fd, int type, ...);
288
289/**
Dominik Dingeld0a073a2015-03-12 13:53:49 +0100290 * kvm_vm_check_attr - check for existence of a specific vm attribute
291 * @s: The KVMState pointer
292 * @group: the group
293 * @attr: the attribute of that group to query for
294 *
295 * Returns: 1 if the attribute exists
296 * 0 if the attribute either does not exist or if the vm device
297 * interface is unavailable
298 */
299int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr);
300
301/**
Pavel Fedin4b3cfe72015-09-24 01:29:36 +0100302 * kvm_device_check_attr - check for existence of a specific device attribute
303 * @fd: The device file descriptor
304 * @group: the group
305 * @attr: the attribute of that group to query for
306 *
307 * Returns: 1 if the attribute exists
308 * 0 if the attribute either does not exist or if the vm device
309 * interface is unavailable
310 */
311int kvm_device_check_attr(int fd, uint32_t group, uint64_t attr);
312
313/**
Greg Kurz84b6ea02019-09-12 15:57:26 +0200314 * kvm_device_access - set or get value of a specific device attribute
Pavel Fedin4b3cfe72015-09-24 01:29:36 +0100315 * @fd: The device file descriptor
316 * @group: the group
317 * @attr: the attribute of that group to set or get
318 * @val: pointer to a storage area for the value
319 * @write: true for set and false for get operation
Eric Auger556969e2017-06-13 14:57:00 +0100320 * @errp: error object handle
Pavel Fedin4b3cfe72015-09-24 01:29:36 +0100321 *
Eric Auger556969e2017-06-13 14:57:00 +0100322 * Returns: 0 on success
323 * < 0 on error
324 * Use kvm_device_check_attr() in order to check for the availability
325 * of optional attributes.
Pavel Fedin4b3cfe72015-09-24 01:29:36 +0100326 */
Eric Auger556969e2017-06-13 14:57:00 +0100327int kvm_device_access(int fd, int group, uint64_t attr,
328 void *val, bool write, Error **errp);
Pavel Fedin4b3cfe72015-09-24 01:29:36 +0100329
330/**
Christoffer Dall0a6a7cc2014-02-26 17:20:00 +0000331 * kvm_create_device - create a KVM device for the device control API
332 * @KVMState: The KVMState pointer
333 * @type: The KVM device type (see Documentation/virtual/kvm/devices in the
334 * kernel source)
335 * @test: If true, only test if device can be created, but don't actually
336 * create the device.
337 *
338 * Returns: -errno on error, nonnegative on success: @test ? 0 : device fd;
339 */
340int kvm_create_device(KVMState *s, uint64_t type, bool test);
341
Peter Xu29039ac2016-03-30 17:27:24 +0100342/**
343 * kvm_device_supported - probe whether KVM supports specific device
344 *
345 * @vmfd: The fd handler for VM
346 * @type: type of device
347 *
348 * @return: true if supported, otherwise false.
349 */
350bool kvm_device_supported(int vmfd, uint64_t type);
Christoffer Dall0a6a7cc2014-02-26 17:20:00 +0000351
aliguori05330442008-11-05 16:29:27 +0000352/* Arch specific hooks */
353
Jan Kiszka94a8d392011-01-21 21:48:17 +0100354extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
355
Andreas Färber20d695a2012-10-31 06:57:49 +0100356void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run);
Paolo Bonzini4c663752015-04-08 13:30:58 +0200357MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000358
Andreas Färber20d695a2012-10-31 06:57:49 +0100359int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run);
aliguori05330442008-11-05 16:29:27 +0000360
Andreas Färber20d695a2012-10-31 06:57:49 +0100361int kvm_arch_process_async_events(CPUState *cpu);
Marcelo Tosatti0af691d2010-05-04 09:45:27 -0300362
Andreas Färber20d695a2012-10-31 06:57:49 +0100363int kvm_arch_get_registers(CPUState *cpu);
aliguori05330442008-11-05 16:29:27 +0000364
Jan Kiszkaea375f92010-03-01 19:10:30 +0100365/* state subset only touched by the VCPU itself during runtime */
366#define KVM_PUT_RUNTIME_STATE 1
367/* state subset modified during VCPU reset */
368#define KVM_PUT_RESET_STATE 2
369/* full state set, modified during initialization or on vmload */
370#define KVM_PUT_FULL_STATE 3
371
Andreas Färber20d695a2012-10-31 06:57:49 +0100372int kvm_arch_put_registers(CPUState *cpu, int level);
aliguori05330442008-11-05 16:29:27 +0000373
Marcel Apfelbaumb16565b2015-02-04 17:43:51 +0200374int kvm_arch_init(MachineState *ms, KVMState *s);
aliguori05330442008-11-05 16:29:27 +0000375
Andreas Färber20d695a2012-10-31 06:57:49 +0100376int kvm_arch_init_vcpu(CPUState *cpu);
Liran Alonb1115c92019-06-19 19:21:32 +0300377int kvm_arch_destroy_vcpu(CPUState *cpu);
aliguori05330442008-11-05 16:29:27 +0000378
Greg Kurz41264b32016-04-26 15:41:04 +0200379bool kvm_vcpu_id_is_valid(int vcpu_id);
380
Eduardo Habkostb164e482013-01-22 18:25:01 -0200381/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
382unsigned long kvm_arch_vcpu_id(CPUState *cpu);
383
Dongjiu Genge24fd072020-05-12 11:06:08 +0800384#ifdef KVM_HAVE_MCE_INJECTION
Paolo Bonzini2ae41db2017-02-08 12:48:54 +0100385void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
386#endif
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300387
Jan Kiszka84b058d2011-10-15 11:49:47 +0200388void kvm_arch_init_irq_routing(KVMState *s);
389
Frank Blaschka9e03a042015-01-09 09:04:40 +0100390int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
Pavel Fedindc9f06c2015-10-15 16:44:52 +0300391 uint64_t address, uint32_t data, PCIDevice *dev);
Frank Blaschka9e03a042015-01-09 09:04:40 +0100392
Peter Xu38d87492016-07-14 13:56:31 +0800393/* Notify arch about newly added MSI routes */
394int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
395 int vector, PCIDevice *dev);
396/* Notify arch about released MSI routes */
397int kvm_arch_release_virq_post(int virq);
398
Eric Auger1850b6b2015-06-02 14:56:23 +0100399int kvm_arch_msi_data_to_gsi(uint32_t data);
400
Peter Maydell3889c3f2012-07-26 15:35:12 +0100401int kvm_set_irq(KVMState *s, int irq, int level);
Jan Kiszka04fa27f2012-05-16 15:41:10 -0300402int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200403
Jan Kiszka1df186d2012-05-17 10:32:32 -0300404void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
Jan Kiszka84b058d2011-10-15 11:49:47 +0200405
David Gibson36077152019-10-17 12:12:35 +1100406void kvm_irqchip_add_change_notifier(Notifier *n);
407void kvm_irqchip_remove_change_notifier(Notifier *n);
408void kvm_irqchip_change_notify(void);
409
Jan Kiszka680c1c62011-10-16 13:23:26 +0200410void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
411
aliguorie22a25c2009-03-12 20:12:48 +0000412struct kvm_guest_debug;
413struct kvm_debug_exit_arch;
414
415struct kvm_sw_breakpoint {
416 target_ulong pc;
417 target_ulong saved_insn;
418 int use_count;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000419 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
aliguorie22a25c2009-03-12 20:12:48 +0000420};
421
Andreas Färbera60f24b2012-12-01 05:35:08 +0100422struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +0000423 target_ulong pc);
424
Andreas Färbera60f24b2012-12-01 05:35:08 +0100425int kvm_sw_breakpoints_active(CPUState *cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000426
Andreas Färber80b7cd72013-06-19 17:37:31 +0200427int kvm_arch_insert_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +0000428 struct kvm_sw_breakpoint *bp);
Andreas Färber80b7cd72013-06-19 17:37:31 +0200429int kvm_arch_remove_sw_breakpoint(CPUState *cpu,
aliguorie22a25c2009-03-12 20:12:48 +0000430 struct kvm_sw_breakpoint *bp);
431int kvm_arch_insert_hw_breakpoint(target_ulong addr,
432 target_ulong len, int type);
433int kvm_arch_remove_hw_breakpoint(target_ulong addr,
434 target_ulong len, int type);
435void kvm_arch_remove_all_hw_breakpoints(void);
436
Andreas Färber20d695a2012-10-31 06:57:49 +0100437void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg);
aliguorie22a25c2009-03-12 20:12:48 +0000438
Andreas Färber20d695a2012-10-31 06:57:49 +0100439bool kvm_arch_stop_on_emulation_error(CPUState *cpu);
Gleb Natapov4513d922010-05-10 11:21:34 +0300440
Anthony Liguoriad7b8b32009-05-08 15:33:24 -0500441int kvm_check_extension(KVMState *s, unsigned int extension);
442
Alexander Graf7d0a07f2014-07-14 19:15:15 +0200443int kvm_vm_check_extension(KVMState *s, unsigned int extension);
444
Cornelia Huck40f1ee22013-10-23 18:19:26 +0200445#define kvm_vm_enable_cap(s, capability, cap_flags, ...) \
446 ({ \
447 struct kvm_enable_cap cap = { \
448 .cap = capability, \
449 .flags = cap_flags, \
450 }; \
451 uint64_t args_tmp[] = { __VA_ARGS__ }; \
Greg Kurz1b7ac7c2017-08-07 13:36:44 +0200452 size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \
453 memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \
Cornelia Huck40f1ee22013-10-23 18:19:26 +0200454 kvm_vm_ioctl(s, KVM_ENABLE_CAP, &cap); \
455 })
456
457#define kvm_vcpu_enable_cap(cpu, capability, cap_flags, ...) \
458 ({ \
459 struct kvm_enable_cap cap = { \
460 .cap = capability, \
461 .flags = cap_flags, \
462 }; \
463 uint64_t args_tmp[] = { __VA_ARGS__ }; \
Greg Kurz1b7ac7c2017-08-07 13:36:44 +0200464 size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \
465 memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \
Cornelia Huck40f1ee22013-10-23 18:19:26 +0200466 kvm_vcpu_ioctl(cpu, KVM_ENABLE_CAP, &cap); \
467 })
468
Jan Kiszkaba9bc592011-06-08 16:11:05 +0200469uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
Sheng Yangc958a8b2010-06-17 15:18:13 +0800470 uint32_t index, int reg);
Paolo Bonziniede146c2019-07-01 17:38:54 +0200471uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index);
Robert Hoof57bceb2018-10-15 12:47:23 +0800472
James Hogan97577fd2013-08-27 12:19:10 +0100473
James Hoganaed6efb2014-06-17 23:10:31 +0100474void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len);
475
James Hogan97577fd2013-08-27 12:19:10 +0100476#if !defined(CONFIG_USER_ONLY)
477int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
478 hwaddr *phys_addr);
479#endif
480
481#endif /* NEED_CPU_H */
482
Andreas Färberdd1750d2013-05-01 13:45:44 +0200483void kvm_cpu_synchronize_state(CPUState *cpu);
Avi Kivityb827df52009-05-03 17:04:01 +0300484
Paolo Bonzini18268b62017-02-09 09:41:14 +0100485void kvm_init_cpu_signals(CPUState *cpu);
486
Peter Xud1f6af62016-07-14 13:56:30 +0800487/**
488 * kvm_irqchip_add_msi_route - Add MSI route for specific vector
Longpeng(Mike)def4c552022-02-22 22:11:16 +0800489 * @c: KVMRouteChange instance.
Peter Xud1f6af62016-07-14 13:56:30 +0800490 * @vector: which vector to add. This can be either MSI/MSIX
491 * vector. The function will automatically detect whether
492 * MSI/MSIX is enabled, and fetch corresponding MSI
493 * message.
494 * @dev: Owner PCI device to add the route. If @dev is specified
495 * as @NULL, an empty MSI message will be inited.
496 * @return: virq (>=0) when success, errno (<0) when failed.
497 */
Longpeng(Mike)def4c552022-02-22 22:11:16 +0800498int kvm_irqchip_add_msi_route(KVMRouteChange *c, int vector, PCIDevice *dev);
Pavel Fedindc9f06c2015-10-15 16:44:52 +0300499int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg,
500 PCIDevice *dev);
Peter Xu3f1fea02016-07-14 13:56:33 +0800501void kvm_irqchip_commit_routes(KVMState *s);
Longpeng(Mike)95686902022-02-22 22:11:15 +0800502
503static inline KVMRouteChange kvm_irqchip_begin_route_changes(KVMState *s)
504{
505 return (KVMRouteChange) { .s = s, .changes = 0 };
506}
507
508static inline void kvm_irqchip_commit_route_changes(KVMRouteChange *c)
509{
510 if (c->changes) {
511 kvm_irqchip_commit_routes(c->s);
512 c->changes = 0;
513 }
514}
515
Jan Kiszka1e2aa8b2012-05-17 10:32:34 -0300516void kvm_irqchip_release_virq(KVMState *s, int virq);
Jan Kiszka39853bb2012-05-17 10:32:36 -0300517
Cornelia Huckd426d9f2013-07-15 17:45:03 +0200518int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter);
Andrey Smetanin977a8d92015-11-10 15:52:42 +0300519int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint);
Cornelia Huckd426d9f2013-07-15 17:45:03 +0200520
Eric Auger1c9b71a2015-07-06 12:15:13 -0600521int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
522 EventNotifier *rn, int virq);
523int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
524 int virq);
Eric Auger197e3522015-07-06 12:15:13 -0600525int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n,
526 EventNotifier *rn, qemu_irq irq);
527int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n,
528 qemu_irq irq);
529void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi);
Jason Barond8ee0382012-11-14 15:54:02 -0500530void kvm_pc_setup_irq_routing(bool pci_enabled);
Alexander Graf7b774592013-04-16 15:58:13 +0200531void kvm_init_irq_routing(KVMState *s);
Christoffer Dalld6032e02014-02-26 17:20:00 +0000532
Paolo Bonzini4376c402019-11-13 11:17:12 +0100533bool kvm_kernel_irqchip_allowed(void);
534bool kvm_kernel_irqchip_required(void);
535bool kvm_kernel_irqchip_split(void);
536
Christoffer Dalld6032e02014-02-26 17:20:00 +0000537/**
538 * kvm_arch_irqchip_create:
539 * @KVMState: The KVMState pointer
540 *
541 * Allow architectures to create an in-kernel irq chip themselves.
542 *
543 * Returns: < 0: error
544 * 0: irq chip was not created
545 * > 0: irq chip was created
546 */
Paolo Bonzini4376c402019-11-13 11:17:12 +0100547int kvm_arch_irqchip_create(KVMState *s);
Cornelia Huckada41352014-05-09 10:06:46 +0200548
549/**
550 * kvm_set_one_reg - set a register value in KVM via KVM_SET_ONE_REG ioctl
551 * @id: The register ID
552 * @source: The pointer to the value to be set. It must point to a variable
553 * of the correct type/size for the register being accessed.
554 *
555 * Returns: 0 on success, or a negative errno on failure.
556 */
557int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source);
558
559/**
560 * kvm_get_one_reg - get a register value from KVM via KVM_GET_ONE_REG ioctl
561 * @id: The register ID
562 * @target: The pointer where the value is to be stored. It must point to a
563 * variable of the correct type/size for the register being accessed.
564 *
565 * Returns: 0 on success, or a negative errno on failure.
566 */
567int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target);
Sam Bobroffc64abd12017-03-20 10:46:43 +1100568struct ppc_radix_page_info *kvm_get_radix_page_info(void);
Bharata B Rao44f2e6c2016-06-01 15:21:24 +0530569int kvm_get_max_memslots(void);
Peter Xuc82d9d42020-03-18 10:52:03 -0400570
571/* Notify resamplefd for EOI of specific interrupts. */
572void kvm_resample_fd_notify(int gsi);
573
Tom Lendacky92a51992021-01-26 11:36:47 -0600574/**
575 * kvm_cpu_check_are_resettable - return whether CPUs can be reset
576 *
577 * Returns: true: CPUs are resettable
578 * false: CPUs are not resettable
579 */
580bool kvm_cpu_check_are_resettable(void);
581
582bool kvm_arch_cpu_check_are_resettable(void);
583
Hyman Huang(黄勇)7786ae42021-06-29 16:01:18 +0000584bool kvm_dirty_ring_enabled(void);
aliguori05330442008-11-05 16:29:27 +0000585#endif