1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef QEMU_KVM_H
15#define QEMU_KVM_H
16
17#include "qemu/queue.h"
18#include "hw/core/cpu.h"
19#include "exec/memattrs.h"
20
21#ifdef NEED_CPU_H
22# ifdef CONFIG_KVM
23# include <linux/kvm.h>
24# define CONFIG_KVM_IS_POSSIBLE
25# endif
26#else
27# define CONFIG_KVM_IS_POSSIBLE
28#endif
29
30#ifdef CONFIG_KVM_IS_POSSIBLE
31
32extern bool kvm_allowed;
33extern bool kvm_kernel_irqchip;
34extern bool kvm_split_irqchip;
35extern bool kvm_async_interrupts_allowed;
36extern bool kvm_halt_in_kernel_allowed;
37extern bool kvm_eventfds_allowed;
38extern bool kvm_irqfds_allowed;
39extern bool kvm_resamplefds_allowed;
40extern bool kvm_msi_via_irqfd_allowed;
41extern bool kvm_gsi_routing_allowed;
42extern bool kvm_gsi_direct_mapping;
43extern bool kvm_readonly_mem_allowed;
44extern bool kvm_direct_msi_allowed;
45extern bool kvm_ioeventfd_any_length_allowed;
46extern bool kvm_msi_use_devid;
47
48#define kvm_enabled() (kvm_allowed)
49
50
51
52
53
54
55
56
57
58
59
60#define kvm_irqchip_in_kernel() (kvm_kernel_irqchip)
61
62
63
64
65
66
67
68
69
70#define kvm_irqchip_is_split() (kvm_split_irqchip)
71
72
73
74
75
76
77
78
79
80#define kvm_async_interrupts_enabled() (kvm_async_interrupts_allowed)
81
82
83
84
85
86
87
88#define kvm_halt_in_kernel() (kvm_halt_in_kernel_allowed)
89
90
91
92
93
94
95
96
97#define kvm_eventfds_enabled() (kvm_eventfds_allowed)
98
99
100
101
102
103
104
105
106#define kvm_irqfds_enabled() (kvm_irqfds_allowed)
107
108
109
110
111
112
113
114
115#define kvm_resamplefds_enabled() (kvm_resamplefds_allowed)
116
117
118
119
120
121
122
123
124#define kvm_msi_via_irqfd_enabled() (kvm_msi_via_irqfd_allowed)
125
126
127
128
129
130
131
132#define kvm_gsi_routing_enabled() (kvm_gsi_routing_allowed)
133
134
135
136
137
138
139#define kvm_gsi_direct_mapping() (kvm_gsi_direct_mapping)
140
141
142
143
144
145
146
147#define kvm_readonly_mem_enabled() (kvm_readonly_mem_allowed)
148
149
150
151
152
153
154#define kvm_direct_msi_enabled() (kvm_direct_msi_allowed)
155
156
157
158
159
160#define kvm_ioeventfd_any_length_enabled() (kvm_ioeventfd_any_length_allowed)
161
162
163
164
165
166
167#define kvm_msi_devid_required() (kvm_msi_use_devid)
168
169#else
170
171#define kvm_enabled() (0)
172#define kvm_irqchip_in_kernel() (false)
173#define kvm_irqchip_is_split() (false)
174#define kvm_async_interrupts_enabled() (false)
175#define kvm_halt_in_kernel() (false)
176#define kvm_eventfds_enabled() (false)
177#define kvm_irqfds_enabled() (false)
178#define kvm_resamplefds_enabled() (false)
179#define kvm_msi_via_irqfd_enabled() (false)
180#define kvm_gsi_routing_allowed() (false)
181#define kvm_gsi_direct_mapping() (false)
182#define kvm_readonly_mem_enabled() (false)
183#define kvm_direct_msi_enabled() (false)
184#define kvm_ioeventfd_any_length_enabled() (false)
185#define kvm_msi_devid_required() (false)
186
187#endif
188
189struct kvm_run;
190struct kvm_lapic_state;
191struct kvm_irq_routing_entry;
192
193typedef struct KVMCapabilityInfo {
194 const char *name;
195 int value;
196} KVMCapabilityInfo;
197
198#define KVM_CAP_INFO(CAP) { "KVM_CAP_" stringify(CAP), KVM_CAP_##CAP }
199#define KVM_CAP_LAST_INFO { NULL, 0 }
200
201struct KVMState;
202typedef struct KVMState KVMState;
203extern KVMState *kvm_state;
204typedef struct Notifier Notifier;
205
206
207
208bool kvm_has_free_slot(MachineState *ms);
209bool kvm_has_sync_mmu(void);
210int kvm_has_vcpu_events(void);
211int kvm_has_robust_singlestep(void);
212int kvm_has_debugregs(void);
213int kvm_max_nested_state_length(void);
214int kvm_has_pit_state2(void);
215int kvm_has_many_ioeventfds(void);
216int kvm_has_gsi_routing(void);
217int kvm_has_intx_set_mask(void);
218
219int kvm_init_vcpu(CPUState *cpu);
220int kvm_cpu_exec(CPUState *cpu);
221int kvm_destroy_vcpu(CPUState *cpu);
222
223
224
225
226
227
228
229
230
231
232bool kvm_arm_supports_user_irq(void);
233
234
235
236
237
238
239
240bool kvm_memcrypt_enabled(void);
241
242
243
244
245
246
247
248int kvm_memcrypt_encrypt_data(uint8_t *ptr, uint64_t len);
249
250
251#ifdef NEED_CPU_H
252#include "cpu.h"
253
254void kvm_flush_coalesced_mmio_buffer(void);
255
256int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
257 target_ulong len, int type);
258int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
259 target_ulong len, int type);
260void kvm_remove_all_breakpoints(CPUState *cpu);
261int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap);
262
263int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
264int kvm_on_sigbus(int code, void *addr);
265
266
267
268void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align, bool shared));
269
270
271
272int kvm_ioctl(KVMState *s, int type, ...);
273
274int kvm_vm_ioctl(KVMState *s, int type, ...);
275
276int kvm_vcpu_ioctl(CPUState *cpu, int type, ...);
277
278
279
280
281
282
283
284
285int kvm_device_ioctl(int fd, int type, ...);
286
287
288
289
290
291
292
293
294
295
296
297int kvm_vm_check_attr(KVMState *s, uint32_t group, uint64_t attr);
298
299
300
301
302
303
304
305
306
307
308
309int kvm_device_check_attr(int fd, uint32_t group, uint64_t attr);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325int kvm_device_access(int fd, int group, uint64_t attr,
326 void *val, bool write, Error **errp);
327
328
329
330
331
332
333
334
335
336
337
338int kvm_create_device(KVMState *s, uint64_t type, bool test);
339
340
341
342
343
344
345
346
347
348bool kvm_device_supported(int vmfd, uint64_t type);
349
350
351
352extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
353
354void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run);
355MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run);
356
357int kvm_arch_handle_exit(CPUState *cpu, struct kvm_run *run);
358
359int kvm_arch_process_async_events(CPUState *cpu);
360
361int kvm_arch_get_registers(CPUState *cpu);
362
363
364#define KVM_PUT_RUNTIME_STATE 1
365
366#define KVM_PUT_RESET_STATE 2
367
368#define KVM_PUT_FULL_STATE 3
369
370int kvm_arch_put_registers(CPUState *cpu, int level);
371
372int kvm_arch_init(MachineState *ms, KVMState *s);
373
374int kvm_arch_init_vcpu(CPUState *cpu);
375int kvm_arch_destroy_vcpu(CPUState *cpu);
376
377bool kvm_vcpu_id_is_valid(int vcpu_id);
378
379
380unsigned long kvm_arch_vcpu_id(CPUState *cpu);
381
382#ifdef TARGET_I386
383#define KVM_HAVE_MCE_INJECTION 1
384void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
385#endif
386
387void kvm_arch_init_irq_routing(KVMState *s);
388
389int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
390 uint64_t address, uint32_t data, PCIDevice *dev);
391
392
393int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
394 int vector, PCIDevice *dev);
395
396int kvm_arch_release_virq_post(int virq);
397
398int kvm_arch_msi_data_to_gsi(uint32_t data);
399
400int kvm_set_irq(KVMState *s, int irq, int level);
401int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg);
402
403void kvm_irqchip_add_irq_route(KVMState *s, int gsi, int irqchip, int pin);
404
405void kvm_irqchip_add_change_notifier(Notifier *n);
406void kvm_irqchip_remove_change_notifier(Notifier *n);
407void kvm_irqchip_change_notify(void);
408
409void kvm_get_apic_state(DeviceState *d, struct kvm_lapic_state *kapic);
410
411struct kvm_guest_debug;
412struct kvm_debug_exit_arch;
413
414struct kvm_sw_breakpoint {
415 target_ulong pc;
416 target_ulong saved_insn;
417 int use_count;
418 QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
419};
420
421struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(CPUState *cpu,
422 target_ulong pc);
423
424int kvm_sw_breakpoints_active(CPUState *cpu);
425
426int kvm_arch_insert_sw_breakpoint(CPUState *cpu,
427 struct kvm_sw_breakpoint *bp);
428int kvm_arch_remove_sw_breakpoint(CPUState *cpu,
429 struct kvm_sw_breakpoint *bp);
430int kvm_arch_insert_hw_breakpoint(target_ulong addr,
431 target_ulong len, int type);
432int kvm_arch_remove_hw_breakpoint(target_ulong addr,
433 target_ulong len, int type);
434void kvm_arch_remove_all_hw_breakpoints(void);
435
436void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg);
437
438bool kvm_arch_stop_on_emulation_error(CPUState *cpu);
439
440int kvm_check_extension(KVMState *s, unsigned int extension);
441
442int kvm_vm_check_extension(KVMState *s, unsigned int extension);
443
444#define kvm_vm_enable_cap(s, capability, cap_flags, ...) \
445 ({ \
446 struct kvm_enable_cap cap = { \
447 .cap = capability, \
448 .flags = cap_flags, \
449 }; \
450 uint64_t args_tmp[] = { __VA_ARGS__ }; \
451 size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \
452 memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \
453 kvm_vm_ioctl(s, KVM_ENABLE_CAP, &cap); \
454 })
455
456#define kvm_vcpu_enable_cap(cpu, capability, cap_flags, ...) \
457 ({ \
458 struct kvm_enable_cap cap = { \
459 .cap = capability, \
460 .flags = cap_flags, \
461 }; \
462 uint64_t args_tmp[] = { __VA_ARGS__ }; \
463 size_t n = MIN(ARRAY_SIZE(args_tmp), ARRAY_SIZE(cap.args)); \
464 memcpy(cap.args, args_tmp, n * sizeof(cap.args[0])); \
465 kvm_vcpu_ioctl(cpu, KVM_ENABLE_CAP, &cap); \
466 })
467
468uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
469 uint32_t index, int reg);
470uint64_t kvm_arch_get_supported_msr_feature(KVMState *s, uint32_t index);
471
472
473void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len);
474
475#if !defined(CONFIG_USER_ONLY)
476int kvm_physical_memory_addr_from_host(KVMState *s, void *ram_addr,
477 hwaddr *phys_addr);
478#endif
479
480#endif
481
482void kvm_cpu_synchronize_state(CPUState *cpu);
483void kvm_cpu_synchronize_post_reset(CPUState *cpu);
484void kvm_cpu_synchronize_post_init(CPUState *cpu);
485void kvm_cpu_synchronize_pre_loadvm(CPUState *cpu);
486
487void kvm_init_cpu_signals(CPUState *cpu);
488
489
490
491
492
493
494
495
496
497
498
499
500int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev);
501int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg,
502 PCIDevice *dev);
503void kvm_irqchip_commit_routes(KVMState *s);
504void kvm_irqchip_release_virq(KVMState *s, int virq);
505
506int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter);
507int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint);
508
509int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
510 EventNotifier *rn, int virq);
511int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
512 int virq);
513int kvm_irqchip_add_irqfd_notifier(KVMState *s, EventNotifier *n,
514 EventNotifier *rn, qemu_irq irq);
515int kvm_irqchip_remove_irqfd_notifier(KVMState *s, EventNotifier *n,
516 qemu_irq irq);
517void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi);
518void kvm_pc_gsi_handler(void *opaque, int n, int level);
519void kvm_pc_setup_irq_routing(bool pci_enabled);
520void kvm_init_irq_routing(KVMState *s);
521
522
523
524
525
526
527
528
529
530
531
532
533int kvm_arch_irqchip_create(MachineState *ms, KVMState *s);
534
535
536
537
538
539
540
541
542
543int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source);
544
545
546
547
548
549
550
551
552
553int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target);
554struct ppc_radix_page_info *kvm_get_radix_page_info(void);
555int kvm_get_max_memslots(void);
556#endif
557