blob: 7e415943a11e54cd19c42f8d9d2f8ec69bdbeec3 [file] [log] [blame]
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001The Definitive KVM (Kernel-based Virtual Machine) API Documentation
2===================================================================
3
41. General description
5
6The kvm API is a set of ioctls that are issued to control various aspects
7of a virtual machine. The ioctls belong to three classes
8
9 - System ioctls: These query and set global attributes which affect the
10 whole kvm subsystem. In addition a system ioctl is used to create
11 virtual machines
12
13 - VM ioctls: These query and set attributes that affect an entire virtual
14 machine, for example memory layout. In addition a VM ioctl is used to
15 create virtual cpus (vcpus).
16
17 Only run VM ioctls from the same process (address space) that was used
18 to create the VM.
19
20 - vcpu ioctls: These query and set attributes that control the operation
21 of a single virtual cpu.
22
23 Only run vcpu ioctls from the same thread that was used to create the
24 vcpu.
25
Wu Fengguang20448922009-12-24 09:04:16 +0800262. File descriptors
Avi Kivity9c1b96e2009-06-09 12:37:58 +030027
28The kvm API is centered around file descriptors. An initial
29open("/dev/kvm") obtains a handle to the kvm subsystem; this handle
30can be used to issue system ioctls. A KVM_CREATE_VM ioctl on this
Wu Fengguang20448922009-12-24 09:04:16 +080031handle will create a VM file descriptor which can be used to issue VM
Avi Kivity9c1b96e2009-06-09 12:37:58 +030032ioctls. A KVM_CREATE_VCPU ioctl on a VM fd will create a virtual cpu
33and return a file descriptor pointing to it. Finally, ioctls on a vcpu
34fd can be used to control the vcpu, including the important task of
35actually running guest code.
36
37In general file descriptors can be migrated among processes by means
38of fork() and the SCM_RIGHTS facility of unix domain socket. These
39kinds of tricks are explicitly not supported by kvm. While they will
40not cause harm to the host, their actual behavior is not guaranteed by
41the API. The only supported use is one virtual machine per process,
42and one vcpu per thread.
43
443. Extensions
45
46As of Linux 2.6.22, the KVM ABI has been stabilized: no backward
47incompatible change are allowed. However, there is an extension
48facility that allows backward-compatible extensions to the API to be
49queried and used.
50
51The extension mechanism is not based on on the Linux version number.
52Instead, kvm defines extension identifiers and a facility to query
53whether a particular extension identifier is available. If it is, a
54set of ioctls is available for application use.
55
564. API description
57
58This section describes ioctls that can be used to control kvm guests.
59For each ioctl, the following information is provided along with a
60description:
61
62 Capability: which KVM extension provides this ioctl. Can be 'basic',
63 which means that is will be provided by any kernel that supports
64 API version 12 (see section 4.1), or a KVM_CAP_xyz constant, which
65 means availability needs to be checked with KVM_CHECK_EXTENSION
66 (see section 4.4).
67
68 Architectures: which instruction set architectures provide this ioctl.
69 x86 includes both i386 and x86_64.
70
71 Type: system, vm, or vcpu.
72
73 Parameters: what parameters are accepted by the ioctl.
74
75 Returns: the return value. General error numbers (EBADF, ENOMEM, EINVAL)
76 are not detailed, but errors with specific meanings are.
77
784.1 KVM_GET_API_VERSION
79
80Capability: basic
81Architectures: all
82Type: system ioctl
83Parameters: none
84Returns: the constant KVM_API_VERSION (=12)
85
86This identifies the API version as the stable kvm API. It is not
87expected that this number will change. However, Linux 2.6.20 and
882.6.21 report earlier versions; these are not documented and not
89supported. Applications should refuse to run if KVM_GET_API_VERSION
90returns a value other than 12. If this check passes, all ioctls
91described as 'basic' will be available.
92
934.2 KVM_CREATE_VM
94
95Capability: basic
96Architectures: all
97Type: system ioctl
98Parameters: none
99Returns: a VM fd that can be used to control the new virtual machine.
100
101The new VM has no virtual cpus and no memory. An mmap() of a VM fd
102will access the virtual machine's physical address space; offset zero
103corresponds to guest physical address zero. Use of mmap() on a VM fd
104is discouraged if userspace memory allocation (KVM_CAP_USER_MEMORY) is
105available.
106
1074.3 KVM_GET_MSR_INDEX_LIST
108
109Capability: basic
110Architectures: x86
111Type: system
112Parameters: struct kvm_msr_list (in/out)
113Returns: 0 on success; -1 on error
114Errors:
115 E2BIG: the msr index list is to be to fit in the array specified by
116 the user.
117
118struct kvm_msr_list {
119 __u32 nmsrs; /* number of msrs in entries */
120 __u32 indices[0];
121};
122
123This ioctl returns the guest msrs that are supported. The list varies
124by kvm version and host processor, but does not change otherwise. The
125user fills in the size of the indices array in nmsrs, and in return
126kvm adjusts nmsrs to reflect the actual number of msrs and fills in
127the indices array with their numbers.
128
1294.4 KVM_CHECK_EXTENSION
130
131Capability: basic
132Architectures: all
133Type: system ioctl
134Parameters: extension identifier (KVM_CAP_*)
135Returns: 0 if unsupported; 1 (or some other positive integer) if supported
136
137The API allows the application to query about extensions to the core
138kvm API. Userspace passes an extension identifier (an integer) and
139receives an integer that describes the extension availability.
140Generally 0 means no and 1 means yes, but some extensions may report
141additional information in the integer return value.
142
1434.5 KVM_GET_VCPU_MMAP_SIZE
144
145Capability: basic
146Architectures: all
147Type: system ioctl
148Parameters: none
149Returns: size of vcpu mmap area, in bytes
150
151The KVM_RUN ioctl (cf.) communicates with userspace via a shared
152memory region. This ioctl returns the size of that region. See the
153KVM_RUN documentation for details.
154
1554.6 KVM_SET_MEMORY_REGION
156
157Capability: basic
158Architectures: all
159Type: vm ioctl
160Parameters: struct kvm_memory_region (in)
161Returns: 0 on success, -1 on error
162
163struct kvm_memory_region {
164 __u32 slot;
165 __u32 flags;
166 __u64 guest_phys_addr;
167 __u64 memory_size; /* bytes */
168};
169
170/* for kvm_memory_region::flags */
171#define KVM_MEM_LOG_DIRTY_PAGES 1UL
172
173This ioctl allows the user to create or modify a guest physical memory
174slot. When changing an existing slot, it may be moved in the guest
175physical memory space, or its flags may be modified. It may not be
176resized. Slots may not overlap.
177
178The flags field supports just one flag, KVM_MEM_LOG_DIRTY_PAGES, which
179instructs kvm to keep track of writes to memory within the slot. See
180the KVM_GET_DIRTY_LOG ioctl.
181
182It is recommended to use the KVM_SET_USER_MEMORY_REGION ioctl instead
183of this API, if available. This newer API allows placing guest memory
184at specified locations in the host address space, yielding better
185control and easy access.
186
1874.6 KVM_CREATE_VCPU
188
189Capability: basic
190Architectures: all
191Type: vm ioctl
192Parameters: vcpu id (apic id on x86)
193Returns: vcpu fd on success, -1 on error
194
195This API adds a vcpu to a virtual machine. The vcpu id is a small integer
196in the range [0, max_vcpus).
197
1984.7 KVM_GET_DIRTY_LOG (vm ioctl)
199
200Capability: basic
201Architectures: x86
202Type: vm ioctl
203Parameters: struct kvm_dirty_log (in/out)
204Returns: 0 on success, -1 on error
205
206/* for KVM_GET_DIRTY_LOG */
207struct kvm_dirty_log {
208 __u32 slot;
209 __u32 padding;
210 union {
211 void __user *dirty_bitmap; /* one bit per page */
212 __u64 padding;
213 };
214};
215
216Given a memory slot, return a bitmap containing any pages dirtied
217since the last call to this ioctl. Bit 0 is the first page in the
218memory slot. Ensure the entire structure is cleared to avoid padding
219issues.
220
2214.8 KVM_SET_MEMORY_ALIAS
222
223Capability: basic
224Architectures: x86
225Type: vm ioctl
226Parameters: struct kvm_memory_alias (in)
227Returns: 0 (success), -1 (error)
228
Avi Kivitya1f4d392010-06-21 11:44:20 +0300229This ioctl is obsolete and has been removed.
Avi Kivity9c1b96e2009-06-09 12:37:58 +0300230
2314.9 KVM_RUN
232
233Capability: basic
234Architectures: all
235Type: vcpu ioctl
236Parameters: none
237Returns: 0 on success, -1 on error
238Errors:
239 EINTR: an unmasked signal is pending
240
241This ioctl is used to run a guest virtual cpu. While there are no
242explicit parameters, there is an implicit parameter block that can be
243obtained by mmap()ing the vcpu fd at offset 0, with the size given by
244KVM_GET_VCPU_MMAP_SIZE. The parameter block is formatted as a 'struct
245kvm_run' (see below).
246
2474.10 KVM_GET_REGS
248
249Capability: basic
250Architectures: all
251Type: vcpu ioctl
252Parameters: struct kvm_regs (out)
253Returns: 0 on success, -1 on error
254
255Reads the general purpose registers from the vcpu.
256
257/* x86 */
258struct kvm_regs {
259 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
260 __u64 rax, rbx, rcx, rdx;
261 __u64 rsi, rdi, rsp, rbp;
262 __u64 r8, r9, r10, r11;
263 __u64 r12, r13, r14, r15;
264 __u64 rip, rflags;
265};
266
2674.11 KVM_SET_REGS
268
269Capability: basic
270Architectures: all
271Type: vcpu ioctl
272Parameters: struct kvm_regs (in)
273Returns: 0 on success, -1 on error
274
275Writes the general purpose registers into the vcpu.
276
277See KVM_GET_REGS for the data structure.
278
2794.12 KVM_GET_SREGS
280
281Capability: basic
282Architectures: x86
283Type: vcpu ioctl
284Parameters: struct kvm_sregs (out)
285Returns: 0 on success, -1 on error
286
287Reads special registers from the vcpu.
288
289/* x86 */
290struct kvm_sregs {
291 struct kvm_segment cs, ds, es, fs, gs, ss;
292 struct kvm_segment tr, ldt;
293 struct kvm_dtable gdt, idt;
294 __u64 cr0, cr2, cr3, cr4, cr8;
295 __u64 efer;
296 __u64 apic_base;
297 __u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
298};
299
300interrupt_bitmap is a bitmap of pending external interrupts. At most
301one bit may be set. This interrupt has been acknowledged by the APIC
302but not yet injected into the cpu core.
303
3044.13 KVM_SET_SREGS
305
306Capability: basic
307Architectures: x86
308Type: vcpu ioctl
309Parameters: struct kvm_sregs (in)
310Returns: 0 on success, -1 on error
311
312Writes special registers into the vcpu. See KVM_GET_SREGS for the
313data structures.
314
3154.14 KVM_TRANSLATE
316
317Capability: basic
318Architectures: x86
319Type: vcpu ioctl
320Parameters: struct kvm_translation (in/out)
321Returns: 0 on success, -1 on error
322
323Translates a virtual address according to the vcpu's current address
324translation mode.
325
326struct kvm_translation {
327 /* in */
328 __u64 linear_address;
329
330 /* out */
331 __u64 physical_address;
332 __u8 valid;
333 __u8 writeable;
334 __u8 usermode;
335 __u8 pad[5];
336};
337
3384.15 KVM_INTERRUPT
339
340Capability: basic
341Architectures: x86
342Type: vcpu ioctl
343Parameters: struct kvm_interrupt (in)
344Returns: 0 on success, -1 on error
345
346Queues a hardware interrupt vector to be injected. This is only
347useful if in-kernel local APIC is not used.
348
349/* for KVM_INTERRUPT */
350struct kvm_interrupt {
351 /* in */
352 __u32 irq;
353};
354
355Note 'irq' is an interrupt vector, not an interrupt pin or line.
356
3574.16 KVM_DEBUG_GUEST
358
359Capability: basic
360Architectures: none
361Type: vcpu ioctl
362Parameters: none)
363Returns: -1 on error
364
365Support for this has been removed. Use KVM_SET_GUEST_DEBUG instead.
366
3674.17 KVM_GET_MSRS
368
369Capability: basic
370Architectures: x86
371Type: vcpu ioctl
372Parameters: struct kvm_msrs (in/out)
373Returns: 0 on success, -1 on error
374
375Reads model-specific registers from the vcpu. Supported msr indices can
376be obtained using KVM_GET_MSR_INDEX_LIST.
377
378struct kvm_msrs {
379 __u32 nmsrs; /* number of msrs in entries */
380 __u32 pad;
381
382 struct kvm_msr_entry entries[0];
383};
384
385struct kvm_msr_entry {
386 __u32 index;
387 __u32 reserved;
388 __u64 data;
389};
390
391Application code should set the 'nmsrs' member (which indicates the
392size of the entries array) and the 'index' member of each array entry.
393kvm will fill in the 'data' member.
394
3954.18 KVM_SET_MSRS
396
397Capability: basic
398Architectures: x86
399Type: vcpu ioctl
400Parameters: struct kvm_msrs (in)
401Returns: 0 on success, -1 on error
402
403Writes model-specific registers to the vcpu. See KVM_GET_MSRS for the
404data structures.
405
406Application code should set the 'nmsrs' member (which indicates the
407size of the entries array), and the 'index' and 'data' members of each
408array entry.
409
4104.19 KVM_SET_CPUID
411
412Capability: basic
413Architectures: x86
414Type: vcpu ioctl
415Parameters: struct kvm_cpuid (in)
416Returns: 0 on success, -1 on error
417
418Defines the vcpu responses to the cpuid instruction. Applications
419should use the KVM_SET_CPUID2 ioctl if available.
420
421
422struct kvm_cpuid_entry {
423 __u32 function;
424 __u32 eax;
425 __u32 ebx;
426 __u32 ecx;
427 __u32 edx;
428 __u32 padding;
429};
430
431/* for KVM_SET_CPUID */
432struct kvm_cpuid {
433 __u32 nent;
434 __u32 padding;
435 struct kvm_cpuid_entry entries[0];
436};
437
4384.20 KVM_SET_SIGNAL_MASK
439
440Capability: basic
441Architectures: x86
442Type: vcpu ioctl
443Parameters: struct kvm_signal_mask (in)
444Returns: 0 on success, -1 on error
445
446Defines which signals are blocked during execution of KVM_RUN. This
447signal mask temporarily overrides the threads signal mask. Any
448unblocked signal received (except SIGKILL and SIGSTOP, which retain
449their traditional behaviour) will cause KVM_RUN to return with -EINTR.
450
451Note the signal will only be delivered if not blocked by the original
452signal mask.
453
454/* for KVM_SET_SIGNAL_MASK */
455struct kvm_signal_mask {
456 __u32 len;
457 __u8 sigset[0];
458};
459
4604.21 KVM_GET_FPU
461
462Capability: basic
463Architectures: x86
464Type: vcpu ioctl
465Parameters: struct kvm_fpu (out)
466Returns: 0 on success, -1 on error
467
468Reads the floating point state from the vcpu.
469
470/* for KVM_GET_FPU and KVM_SET_FPU */
471struct kvm_fpu {
472 __u8 fpr[8][16];
473 __u16 fcw;
474 __u16 fsw;
475 __u8 ftwx; /* in fxsave format */
476 __u8 pad1;
477 __u16 last_opcode;
478 __u64 last_ip;
479 __u64 last_dp;
480 __u8 xmm[16][16];
481 __u32 mxcsr;
482 __u32 pad2;
483};
484
4854.22 KVM_SET_FPU
486
487Capability: basic
488Architectures: x86
489Type: vcpu ioctl
490Parameters: struct kvm_fpu (in)
491Returns: 0 on success, -1 on error
492
493Writes the floating point state to the vcpu.
494
495/* for KVM_GET_FPU and KVM_SET_FPU */
496struct kvm_fpu {
497 __u8 fpr[8][16];
498 __u16 fcw;
499 __u16 fsw;
500 __u8 ftwx; /* in fxsave format */
501 __u8 pad1;
502 __u16 last_opcode;
503 __u64 last_ip;
504 __u64 last_dp;
505 __u8 xmm[16][16];
506 __u32 mxcsr;
507 __u32 pad2;
508};
509
Avi Kivity5dadbfd2009-08-23 17:08:04 +03005104.23 KVM_CREATE_IRQCHIP
511
512Capability: KVM_CAP_IRQCHIP
513Architectures: x86, ia64
514Type: vm ioctl
515Parameters: none
516Returns: 0 on success, -1 on error
517
518Creates an interrupt controller model in the kernel. On x86, creates a virtual
519ioapic, a virtual PIC (two PICs, nested), and sets up future vcpus to have a
520local APIC. IRQ routing for GSIs 0-15 is set to both PIC and IOAPIC; GSI 16-23
521only go to the IOAPIC. On ia64, a IOSAPIC is created.
522
5234.24 KVM_IRQ_LINE
524
525Capability: KVM_CAP_IRQCHIP
526Architectures: x86, ia64
527Type: vm ioctl
528Parameters: struct kvm_irq_level
529Returns: 0 on success, -1 on error
530
531Sets the level of a GSI input to the interrupt controller model in the kernel.
532Requires that an interrupt controller model has been previously created with
533KVM_CREATE_IRQCHIP. Note that edge-triggered interrupts require the level
534to be set to 1 and then back to 0.
535
536struct kvm_irq_level {
537 union {
538 __u32 irq; /* GSI */
539 __s32 status; /* not used for KVM_IRQ_LEVEL */
540 };
541 __u32 level; /* 0 or 1 */
542};
543
5444.25 KVM_GET_IRQCHIP
545
546Capability: KVM_CAP_IRQCHIP
547Architectures: x86, ia64
548Type: vm ioctl
549Parameters: struct kvm_irqchip (in/out)
550Returns: 0 on success, -1 on error
551
552Reads the state of a kernel interrupt controller created with
553KVM_CREATE_IRQCHIP into a buffer provided by the caller.
554
555struct kvm_irqchip {
556 __u32 chip_id; /* 0 = PIC1, 1 = PIC2, 2 = IOAPIC */
557 __u32 pad;
558 union {
559 char dummy[512]; /* reserving space */
560 struct kvm_pic_state pic;
561 struct kvm_ioapic_state ioapic;
562 } chip;
563};
564
5654.26 KVM_SET_IRQCHIP
566
567Capability: KVM_CAP_IRQCHIP
568Architectures: x86, ia64
569Type: vm ioctl
570Parameters: struct kvm_irqchip (in)
571Returns: 0 on success, -1 on error
572
573Sets the state of a kernel interrupt controller created with
574KVM_CREATE_IRQCHIP from a buffer provided by the caller.
575
576struct kvm_irqchip {
577 __u32 chip_id; /* 0 = PIC1, 1 = PIC2, 2 = IOAPIC */
578 __u32 pad;
579 union {
580 char dummy[512]; /* reserving space */
581 struct kvm_pic_state pic;
582 struct kvm_ioapic_state ioapic;
583 } chip;
584};
585
Ed Swierkffde22a2009-10-15 15:21:43 -07005864.27 KVM_XEN_HVM_CONFIG
587
588Capability: KVM_CAP_XEN_HVM
589Architectures: x86
590Type: vm ioctl
591Parameters: struct kvm_xen_hvm_config (in)
592Returns: 0 on success, -1 on error
593
594Sets the MSR that the Xen HVM guest uses to initialize its hypercall
595page, and provides the starting address and size of the hypercall
596blobs in userspace. When the guest writes the MSR, kvm copies one
597page of a blob (32- or 64-bit, depending on the vcpu mode) to guest
598memory.
599
600struct kvm_xen_hvm_config {
601 __u32 flags;
602 __u32 msr;
603 __u64 blob_addr_32;
604 __u64 blob_addr_64;
605 __u8 blob_size_32;
606 __u8 blob_size_64;
607 __u8 pad2[30];
608};
609
Glauber Costaafbcf7a2009-10-16 15:28:36 -04006104.27 KVM_GET_CLOCK
611
612Capability: KVM_CAP_ADJUST_CLOCK
613Architectures: x86
614Type: vm ioctl
615Parameters: struct kvm_clock_data (out)
616Returns: 0 on success, -1 on error
617
618Gets the current timestamp of kvmclock as seen by the current guest. In
619conjunction with KVM_SET_CLOCK, it is used to ensure monotonicity on scenarios
620such as migration.
621
622struct kvm_clock_data {
623 __u64 clock; /* kvmclock current value */
624 __u32 flags;
625 __u32 pad[9];
626};
627
6284.28 KVM_SET_CLOCK
629
630Capability: KVM_CAP_ADJUST_CLOCK
631Architectures: x86
632Type: vm ioctl
633Parameters: struct kvm_clock_data (in)
634Returns: 0 on success, -1 on error
635
Wu Fengguang20448922009-12-24 09:04:16 +0800636Sets the current timestamp of kvmclock to the value specified in its parameter.
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400637In conjunction with KVM_GET_CLOCK, it is used to ensure monotonicity on scenarios
638such as migration.
639
640struct kvm_clock_data {
641 __u64 clock; /* kvmclock current value */
642 __u32 flags;
643 __u32 pad[9];
644};
645
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006464.29 KVM_GET_VCPU_EVENTS
647
648Capability: KVM_CAP_VCPU_EVENTS
Jan Kiszka48005f62010-02-19 19:38:07 +0100649Extended by: KVM_CAP_INTR_SHADOW
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100650Architectures: x86
651Type: vm ioctl
652Parameters: struct kvm_vcpu_event (out)
653Returns: 0 on success, -1 on error
654
655Gets currently pending exceptions, interrupts, and NMIs as well as related
656states of the vcpu.
657
658struct kvm_vcpu_events {
659 struct {
660 __u8 injected;
661 __u8 nr;
662 __u8 has_error_code;
663 __u8 pad;
664 __u32 error_code;
665 } exception;
666 struct {
667 __u8 injected;
668 __u8 nr;
669 __u8 soft;
Jan Kiszka48005f62010-02-19 19:38:07 +0100670 __u8 shadow;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100671 } interrupt;
672 struct {
673 __u8 injected;
674 __u8 pending;
675 __u8 masked;
676 __u8 pad;
677 } nmi;
678 __u32 sipi_vector;
Jan Kiszkadab4b912009-12-06 18:24:15 +0100679 __u32 flags;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100680};
681
Jan Kiszka48005f62010-02-19 19:38:07 +0100682KVM_VCPUEVENT_VALID_SHADOW may be set in the flags field to signal that
683interrupt.shadow contains a valid state. Otherwise, this field is undefined.
684
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006854.30 KVM_SET_VCPU_EVENTS
686
687Capability: KVM_CAP_VCPU_EVENTS
Jan Kiszka48005f62010-02-19 19:38:07 +0100688Extended by: KVM_CAP_INTR_SHADOW
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100689Architectures: x86
690Type: vm ioctl
691Parameters: struct kvm_vcpu_event (in)
692Returns: 0 on success, -1 on error
693
694Set pending exceptions, interrupts, and NMIs as well as related states of the
695vcpu.
696
697See KVM_GET_VCPU_EVENTS for the data structure.
698
Jan Kiszkadab4b912009-12-06 18:24:15 +0100699Fields that may be modified asynchronously by running VCPUs can be excluded
700from the update. These fields are nmi.pending and sipi_vector. Keep the
701corresponding bits in the flags field cleared to suppress overwriting the
702current in-kernel state. The bits are:
703
704KVM_VCPUEVENT_VALID_NMI_PENDING - transfer nmi.pending to the kernel
705KVM_VCPUEVENT_VALID_SIPI_VECTOR - transfer sipi_vector
706
Jan Kiszka48005f62010-02-19 19:38:07 +0100707If KVM_CAP_INTR_SHADOW is available, KVM_VCPUEVENT_VALID_SHADOW can be set in
708the flags field to signal that interrupt.shadow contains a valid state and
709shall be written into the VCPU.
710
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01007114.32 KVM_GET_DEBUGREGS
712
713Capability: KVM_CAP_DEBUGREGS
714Architectures: x86
715Type: vm ioctl
716Parameters: struct kvm_debugregs (out)
717Returns: 0 on success, -1 on error
718
719Reads debug registers from the vcpu.
720
721struct kvm_debugregs {
722 __u64 db[4];
723 __u64 dr6;
724 __u64 dr7;
725 __u64 flags;
726 __u64 reserved[9];
727};
728
7294.33 KVM_SET_DEBUGREGS
730
731Capability: KVM_CAP_DEBUGREGS
732Architectures: x86
733Type: vm ioctl
734Parameters: struct kvm_debugregs (in)
735Returns: 0 on success, -1 on error
736
737Writes debug registers into the vcpu.
738
739See KVM_GET_DEBUGREGS for the data structure. The flags field is unused
740yet and must be cleared on entry.
741
Avi Kivity0f2d8f42010-03-25 12:16:48 +02007424.34 KVM_SET_USER_MEMORY_REGION
743
744Capability: KVM_CAP_USER_MEM
745Architectures: all
746Type: vm ioctl
747Parameters: struct kvm_userspace_memory_region (in)
748Returns: 0 on success, -1 on error
749
750struct kvm_userspace_memory_region {
751 __u32 slot;
752 __u32 flags;
753 __u64 guest_phys_addr;
754 __u64 memory_size; /* bytes */
755 __u64 userspace_addr; /* start of the userspace allocated memory */
756};
757
758/* for kvm_memory_region::flags */
759#define KVM_MEM_LOG_DIRTY_PAGES 1UL
760
761This ioctl allows the user to create or modify a guest physical memory
762slot. When changing an existing slot, it may be moved in the guest
763physical memory space, or its flags may be modified. It may not be
764resized. Slots may not overlap in guest physical address space.
765
766Memory for the region is taken starting at the address denoted by the
767field userspace_addr, which must point at user addressable memory for
768the entire memory slot size. Any object may back this memory, including
769anonymous memory, ordinary files, and hugetlbfs.
770
771It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr
772be identical. This allows large pages in the guest to be backed by large
773pages in the host.
774
775The flags field supports just one flag, KVM_MEM_LOG_DIRTY_PAGES, which
776instructs kvm to keep track of writes to memory within the slot. See
777the KVM_GET_DIRTY_LOG ioctl.
778
779When the KVM_CAP_SYNC_MMU capability, changes in the backing of the memory
780region are automatically reflected into the guest. For example, an mmap()
781that affects the region will be made visible immediately. Another example
782is madvise(MADV_DROP).
783
784It is recommended to use this API instead of the KVM_SET_MEMORY_REGION ioctl.
785The KVM_SET_MEMORY_REGION does not allow fine grained control over memory
786allocation and is deprecated.
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100787
Avi Kivity8a5416d2010-03-25 12:27:30 +02007884.35 KVM_SET_TSS_ADDR
789
790Capability: KVM_CAP_SET_TSS_ADDR
791Architectures: x86
792Type: vm ioctl
793Parameters: unsigned long tss_address (in)
794Returns: 0 on success, -1 on error
795
796This ioctl defines the physical address of a three-page region in the guest
797physical address space. The region must be within the first 4GB of the
798guest physical address space and must not conflict with any memory slot
799or any mmio address. The guest may malfunction if it accesses this memory
800region.
801
802This ioctl is required on Intel-based hosts. This is needed on Intel hardware
803because of a quirk in the virtualization implementation (see the internals
804documentation when it pops into existence).
805
Alexander Graf71fbfd52010-03-24 21:48:29 +01008064.36 KVM_ENABLE_CAP
807
808Capability: KVM_CAP_ENABLE_CAP
809Architectures: ppc
810Type: vcpu ioctl
811Parameters: struct kvm_enable_cap (in)
812Returns: 0 on success; -1 on error
813
814+Not all extensions are enabled by default. Using this ioctl the application
815can enable an extension, making it available to the guest.
816
817On systems that do not support this ioctl, it always fails. On systems that
818do support it, it only works for extensions that are supported for enablement.
819
820To check if a capability can be enabled, the KVM_CHECK_EXTENSION ioctl should
821be used.
822
823struct kvm_enable_cap {
824 /* in */
825 __u32 cap;
826
827The capability that is supposed to get enabled.
828
829 __u32 flags;
830
831A bitfield indicating future enhancements. Has to be 0 for now.
832
833 __u64 args[4];
834
835Arguments for enabling a feature. If a feature needs initial values to
836function properly, this is the place to put them.
837
838 __u8 pad[64];
839};
840
Avi Kivityb843f062010-04-25 15:51:46 +03008414.37 KVM_GET_MP_STATE
842
843Capability: KVM_CAP_MP_STATE
844Architectures: x86, ia64
845Type: vcpu ioctl
846Parameters: struct kvm_mp_state (out)
847Returns: 0 on success; -1 on error
848
849struct kvm_mp_state {
850 __u32 mp_state;
851};
852
853Returns the vcpu's current "multiprocessing state" (though also valid on
854uniprocessor guests).
855
856Possible values are:
857
858 - KVM_MP_STATE_RUNNABLE: the vcpu is currently running
859 - KVM_MP_STATE_UNINITIALIZED: the vcpu is an application processor (AP)
860 which has not yet received an INIT signal
861 - KVM_MP_STATE_INIT_RECEIVED: the vcpu has received an INIT signal, and is
862 now ready for a SIPI
863 - KVM_MP_STATE_HALTED: the vcpu has executed a HLT instruction and
864 is waiting for an interrupt
865 - KVM_MP_STATE_SIPI_RECEIVED: the vcpu has just received a SIPI (vector
866 accesible via KVM_GET_VCPU_EVENTS)
867
868This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
869irqchip, the multiprocessing state must be maintained by userspace.
870
8714.38 KVM_SET_MP_STATE
872
873Capability: KVM_CAP_MP_STATE
874Architectures: x86, ia64
875Type: vcpu ioctl
876Parameters: struct kvm_mp_state (in)
877Returns: 0 on success; -1 on error
878
879Sets the vcpu's current "multiprocessing state"; see KVM_GET_MP_STATE for
880arguments.
881
882This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
883irqchip, the multiprocessing state must be maintained by userspace.
884
Avi Kivity47dbb842010-04-29 12:08:56 +03008854.39 KVM_SET_IDENTITY_MAP_ADDR
886
887Capability: KVM_CAP_SET_IDENTITY_MAP_ADDR
888Architectures: x86
889Type: vm ioctl
890Parameters: unsigned long identity (in)
891Returns: 0 on success, -1 on error
892
893This ioctl defines the physical address of a one-page region in the guest
894physical address space. The region must be within the first 4GB of the
895guest physical address space and must not conflict with any memory slot
896or any mmio address. The guest may malfunction if it accesses this memory
897region.
898
899This ioctl is required on Intel-based hosts. This is needed on Intel hardware
900because of a quirk in the virtualization implementation (see the internals
901documentation when it pops into existence).
902
Avi Kivity57bc24c2010-04-29 12:12:57 +03009034.40 KVM_SET_BOOT_CPU_ID
904
905Capability: KVM_CAP_SET_BOOT_CPU_ID
906Architectures: x86, ia64
907Type: vm ioctl
908Parameters: unsigned long vcpu_id
909Returns: 0 on success, -1 on error
910
911Define which vcpu is the Bootstrap Processor (BSP). Values are the same
912as the vcpu id in KVM_CREATE_VCPU. If this ioctl is not called, the default
913is vcpu 0.
914
Sheng Yang2d5b5a62010-06-13 17:29:39 +08009154.41 KVM_GET_XSAVE
916
917Capability: KVM_CAP_XSAVE
918Architectures: x86
919Type: vcpu ioctl
920Parameters: struct kvm_xsave (out)
921Returns: 0 on success, -1 on error
922
923struct kvm_xsave {
924 __u32 region[1024];
925};
926
927This ioctl would copy current vcpu's xsave struct to the userspace.
928
9294.42 KVM_SET_XSAVE
930
931Capability: KVM_CAP_XSAVE
932Architectures: x86
933Type: vcpu ioctl
934Parameters: struct kvm_xsave (in)
935Returns: 0 on success, -1 on error
936
937struct kvm_xsave {
938 __u32 region[1024];
939};
940
941This ioctl would copy userspace's xsave struct to the kernel.
942
9434.43 KVM_GET_XCRS
944
945Capability: KVM_CAP_XCRS
946Architectures: x86
947Type: vcpu ioctl
948Parameters: struct kvm_xcrs (out)
949Returns: 0 on success, -1 on error
950
951struct kvm_xcr {
952 __u32 xcr;
953 __u32 reserved;
954 __u64 value;
955};
956
957struct kvm_xcrs {
958 __u32 nr_xcrs;
959 __u32 flags;
960 struct kvm_xcr xcrs[KVM_MAX_XCRS];
961 __u64 padding[16];
962};
963
964This ioctl would copy current vcpu's xcrs to the userspace.
965
9664.44 KVM_SET_XCRS
967
968Capability: KVM_CAP_XCRS
969Architectures: x86
970Type: vcpu ioctl
971Parameters: struct kvm_xcrs (in)
972Returns: 0 on success, -1 on error
973
974struct kvm_xcr {
975 __u32 xcr;
976 __u32 reserved;
977 __u64 value;
978};
979
980struct kvm_xcrs {
981 __u32 nr_xcrs;
982 __u32 flags;
983 struct kvm_xcr xcrs[KVM_MAX_XCRS];
984 __u64 padding[16];
985};
986
987This ioctl would set vcpu's xcr to the value userspace specified.
988
Avi Kivity9c1b96e2009-06-09 12:37:58 +03009895. The kvm_run structure
990
991Application code obtains a pointer to the kvm_run structure by
992mmap()ing a vcpu fd. From that point, application code can control
993execution by changing fields in kvm_run prior to calling the KVM_RUN
994ioctl, and obtain information about the reason KVM_RUN returned by
995looking up structure members.
996
997struct kvm_run {
998 /* in */
999 __u8 request_interrupt_window;
1000
1001Request that KVM_RUN return when it becomes possible to inject external
1002interrupts into the guest. Useful in conjunction with KVM_INTERRUPT.
1003
1004 __u8 padding1[7];
1005
1006 /* out */
1007 __u32 exit_reason;
1008
1009When KVM_RUN has returned successfully (return value 0), this informs
1010application code why KVM_RUN has returned. Allowable values for this
1011field are detailed below.
1012
1013 __u8 ready_for_interrupt_injection;
1014
1015If request_interrupt_window has been specified, this field indicates
1016an interrupt can be injected now with KVM_INTERRUPT.
1017
1018 __u8 if_flag;
1019
1020The value of the current interrupt flag. Only valid if in-kernel
1021local APIC is not used.
1022
1023 __u8 padding2[2];
1024
1025 /* in (pre_kvm_run), out (post_kvm_run) */
1026 __u64 cr8;
1027
1028The value of the cr8 register. Only valid if in-kernel local APIC is
1029not used. Both input and output.
1030
1031 __u64 apic_base;
1032
1033The value of the APIC BASE msr. Only valid if in-kernel local
1034APIC is not used. Both input and output.
1035
1036 union {
1037 /* KVM_EXIT_UNKNOWN */
1038 struct {
1039 __u64 hardware_exit_reason;
1040 } hw;
1041
1042If exit_reason is KVM_EXIT_UNKNOWN, the vcpu has exited due to unknown
1043reasons. Further architecture-specific information is available in
1044hardware_exit_reason.
1045
1046 /* KVM_EXIT_FAIL_ENTRY */
1047 struct {
1048 __u64 hardware_entry_failure_reason;
1049 } fail_entry;
1050
1051If exit_reason is KVM_EXIT_FAIL_ENTRY, the vcpu could not be run due
1052to unknown reasons. Further architecture-specific information is
1053available in hardware_entry_failure_reason.
1054
1055 /* KVM_EXIT_EXCEPTION */
1056 struct {
1057 __u32 exception;
1058 __u32 error_code;
1059 } ex;
1060
1061Unused.
1062
1063 /* KVM_EXIT_IO */
1064 struct {
1065#define KVM_EXIT_IO_IN 0
1066#define KVM_EXIT_IO_OUT 1
1067 __u8 direction;
1068 __u8 size; /* bytes */
1069 __u16 port;
1070 __u32 count;
1071 __u64 data_offset; /* relative to kvm_run start */
1072 } io;
1073
Wu Fengguang20448922009-12-24 09:04:16 +08001074If exit_reason is KVM_EXIT_IO, then the vcpu has
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001075executed a port I/O instruction which could not be satisfied by kvm.
1076data_offset describes where the data is located (KVM_EXIT_IO_OUT) or
1077where kvm expects application code to place the data for the next
Wu Fengguang20448922009-12-24 09:04:16 +08001078KVM_RUN invocation (KVM_EXIT_IO_IN). Data format is a packed array.
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001079
1080 struct {
1081 struct kvm_debug_exit_arch arch;
1082 } debug;
1083
1084Unused.
1085
1086 /* KVM_EXIT_MMIO */
1087 struct {
1088 __u64 phys_addr;
1089 __u8 data[8];
1090 __u32 len;
1091 __u8 is_write;
1092 } mmio;
1093
Wu Fengguang20448922009-12-24 09:04:16 +08001094If exit_reason is KVM_EXIT_MMIO, then the vcpu has
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001095executed a memory-mapped I/O instruction which could not be satisfied
1096by kvm. The 'data' member contains the written data if 'is_write' is
1097true, and should be filled by application code otherwise.
1098
Alexander Grafad0a0482010-03-24 21:48:30 +01001099NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO and KVM_EXIT_OSI, the corresponding
1100operations are complete (and guest state is consistent) only after userspace
1101has re-entered the kernel with KVM_RUN. The kernel side will first finish
Marcelo Tosatti67961342010-02-13 16:10:26 -02001102incomplete operations and then check for pending signals. Userspace
1103can re-enter the guest with an unmasked signal pending to complete
1104pending operations.
1105
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001106 /* KVM_EXIT_HYPERCALL */
1107 struct {
1108 __u64 nr;
1109 __u64 args[6];
1110 __u64 ret;
1111 __u32 longmode;
1112 __u32 pad;
1113 } hypercall;
1114
Avi Kivity647dc492010-04-01 14:39:21 +03001115Unused. This was once used for 'hypercall to userspace'. To implement
1116such functionality, use KVM_EXIT_IO (x86) or KVM_EXIT_MMIO (all except s390).
1117Note KVM_EXIT_IO is significantly faster than KVM_EXIT_MMIO.
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001118
1119 /* KVM_EXIT_TPR_ACCESS */
1120 struct {
1121 __u64 rip;
1122 __u32 is_write;
1123 __u32 pad;
1124 } tpr_access;
1125
1126To be documented (KVM_TPR_ACCESS_REPORTING).
1127
1128 /* KVM_EXIT_S390_SIEIC */
1129 struct {
1130 __u8 icptcode;
1131 __u64 mask; /* psw upper half */
1132 __u64 addr; /* psw lower half */
1133 __u16 ipa;
1134 __u32 ipb;
1135 } s390_sieic;
1136
1137s390 specific.
1138
1139 /* KVM_EXIT_S390_RESET */
1140#define KVM_S390_RESET_POR 1
1141#define KVM_S390_RESET_CLEAR 2
1142#define KVM_S390_RESET_SUBSYSTEM 4
1143#define KVM_S390_RESET_CPU_INIT 8
1144#define KVM_S390_RESET_IPL 16
1145 __u64 s390_reset_flags;
1146
1147s390 specific.
1148
1149 /* KVM_EXIT_DCR */
1150 struct {
1151 __u32 dcrn;
1152 __u32 data;
1153 __u8 is_write;
1154 } dcr;
1155
1156powerpc specific.
1157
Alexander Grafad0a0482010-03-24 21:48:30 +01001158 /* KVM_EXIT_OSI */
1159 struct {
1160 __u64 gprs[32];
1161 } osi;
1162
1163MOL uses a special hypercall interface it calls 'OSI'. To enable it, we catch
1164hypercalls and exit with this exit struct that contains all the guest gprs.
1165
1166If exit_reason is KVM_EXIT_OSI, then the vcpu has triggered such a hypercall.
1167Userspace can now handle the hypercall and when it's done modify the gprs as
1168necessary. Upon guest entry all guest GPRs will then be replaced by the values
1169in this struct.
1170
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001171 /* Fix the size of the union. */
1172 char padding[256];
1173 };
1174};