blob: d9b00f15fbe660b096bad185d88433c47376e4a7 [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
Avi Kivityb74a07b2010-06-21 11:48:05 +0300163This ioctl is obsolete and has been removed.
Avi Kivity9c1b96e2009-06-09 12:37:58 +0300164
1654.6 KVM_CREATE_VCPU
166
167Capability: basic
168Architectures: all
169Type: vm ioctl
170Parameters: vcpu id (apic id on x86)
171Returns: vcpu fd on success, -1 on error
172
173This API adds a vcpu to a virtual machine. The vcpu id is a small integer
174in the range [0, max_vcpus).
175
1764.7 KVM_GET_DIRTY_LOG (vm ioctl)
177
178Capability: basic
179Architectures: x86
180Type: vm ioctl
181Parameters: struct kvm_dirty_log (in/out)
182Returns: 0 on success, -1 on error
183
184/* for KVM_GET_DIRTY_LOG */
185struct kvm_dirty_log {
186 __u32 slot;
187 __u32 padding;
188 union {
189 void __user *dirty_bitmap; /* one bit per page */
190 __u64 padding;
191 };
192};
193
194Given a memory slot, return a bitmap containing any pages dirtied
195since the last call to this ioctl. Bit 0 is the first page in the
196memory slot. Ensure the entire structure is cleared to avoid padding
197issues.
198
1994.8 KVM_SET_MEMORY_ALIAS
200
201Capability: basic
202Architectures: x86
203Type: vm ioctl
204Parameters: struct kvm_memory_alias (in)
205Returns: 0 (success), -1 (error)
206
Avi Kivitya1f4d392010-06-21 11:44:20 +0300207This ioctl is obsolete and has been removed.
Avi Kivity9c1b96e2009-06-09 12:37:58 +0300208
2094.9 KVM_RUN
210
211Capability: basic
212Architectures: all
213Type: vcpu ioctl
214Parameters: none
215Returns: 0 on success, -1 on error
216Errors:
217 EINTR: an unmasked signal is pending
218
219This ioctl is used to run a guest virtual cpu. While there are no
220explicit parameters, there is an implicit parameter block that can be
221obtained by mmap()ing the vcpu fd at offset 0, with the size given by
222KVM_GET_VCPU_MMAP_SIZE. The parameter block is formatted as a 'struct
223kvm_run' (see below).
224
2254.10 KVM_GET_REGS
226
227Capability: basic
228Architectures: all
229Type: vcpu ioctl
230Parameters: struct kvm_regs (out)
231Returns: 0 on success, -1 on error
232
233Reads the general purpose registers from the vcpu.
234
235/* x86 */
236struct kvm_regs {
237 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
238 __u64 rax, rbx, rcx, rdx;
239 __u64 rsi, rdi, rsp, rbp;
240 __u64 r8, r9, r10, r11;
241 __u64 r12, r13, r14, r15;
242 __u64 rip, rflags;
243};
244
2454.11 KVM_SET_REGS
246
247Capability: basic
248Architectures: all
249Type: vcpu ioctl
250Parameters: struct kvm_regs (in)
251Returns: 0 on success, -1 on error
252
253Writes the general purpose registers into the vcpu.
254
255See KVM_GET_REGS for the data structure.
256
2574.12 KVM_GET_SREGS
258
259Capability: basic
260Architectures: x86
261Type: vcpu ioctl
262Parameters: struct kvm_sregs (out)
263Returns: 0 on success, -1 on error
264
265Reads special registers from the vcpu.
266
267/* x86 */
268struct kvm_sregs {
269 struct kvm_segment cs, ds, es, fs, gs, ss;
270 struct kvm_segment tr, ldt;
271 struct kvm_dtable gdt, idt;
272 __u64 cr0, cr2, cr3, cr4, cr8;
273 __u64 efer;
274 __u64 apic_base;
275 __u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
276};
277
278interrupt_bitmap is a bitmap of pending external interrupts. At most
279one bit may be set. This interrupt has been acknowledged by the APIC
280but not yet injected into the cpu core.
281
2824.13 KVM_SET_SREGS
283
284Capability: basic
285Architectures: x86
286Type: vcpu ioctl
287Parameters: struct kvm_sregs (in)
288Returns: 0 on success, -1 on error
289
290Writes special registers into the vcpu. See KVM_GET_SREGS for the
291data structures.
292
2934.14 KVM_TRANSLATE
294
295Capability: basic
296Architectures: x86
297Type: vcpu ioctl
298Parameters: struct kvm_translation (in/out)
299Returns: 0 on success, -1 on error
300
301Translates a virtual address according to the vcpu's current address
302translation mode.
303
304struct kvm_translation {
305 /* in */
306 __u64 linear_address;
307
308 /* out */
309 __u64 physical_address;
310 __u8 valid;
311 __u8 writeable;
312 __u8 usermode;
313 __u8 pad[5];
314};
315
3164.15 KVM_INTERRUPT
317
318Capability: basic
319Architectures: x86
320Type: vcpu ioctl
321Parameters: struct kvm_interrupt (in)
322Returns: 0 on success, -1 on error
323
324Queues a hardware interrupt vector to be injected. This is only
325useful if in-kernel local APIC is not used.
326
327/* for KVM_INTERRUPT */
328struct kvm_interrupt {
329 /* in */
330 __u32 irq;
331};
332
333Note 'irq' is an interrupt vector, not an interrupt pin or line.
334
3354.16 KVM_DEBUG_GUEST
336
337Capability: basic
338Architectures: none
339Type: vcpu ioctl
340Parameters: none)
341Returns: -1 on error
342
343Support for this has been removed. Use KVM_SET_GUEST_DEBUG instead.
344
3454.17 KVM_GET_MSRS
346
347Capability: basic
348Architectures: x86
349Type: vcpu ioctl
350Parameters: struct kvm_msrs (in/out)
351Returns: 0 on success, -1 on error
352
353Reads model-specific registers from the vcpu. Supported msr indices can
354be obtained using KVM_GET_MSR_INDEX_LIST.
355
356struct kvm_msrs {
357 __u32 nmsrs; /* number of msrs in entries */
358 __u32 pad;
359
360 struct kvm_msr_entry entries[0];
361};
362
363struct kvm_msr_entry {
364 __u32 index;
365 __u32 reserved;
366 __u64 data;
367};
368
369Application code should set the 'nmsrs' member (which indicates the
370size of the entries array) and the 'index' member of each array entry.
371kvm will fill in the 'data' member.
372
3734.18 KVM_SET_MSRS
374
375Capability: basic
376Architectures: x86
377Type: vcpu ioctl
378Parameters: struct kvm_msrs (in)
379Returns: 0 on success, -1 on error
380
381Writes model-specific registers to the vcpu. See KVM_GET_MSRS for the
382data structures.
383
384Application code should set the 'nmsrs' member (which indicates the
385size of the entries array), and the 'index' and 'data' members of each
386array entry.
387
3884.19 KVM_SET_CPUID
389
390Capability: basic
391Architectures: x86
392Type: vcpu ioctl
393Parameters: struct kvm_cpuid (in)
394Returns: 0 on success, -1 on error
395
396Defines the vcpu responses to the cpuid instruction. Applications
397should use the KVM_SET_CPUID2 ioctl if available.
398
399
400struct kvm_cpuid_entry {
401 __u32 function;
402 __u32 eax;
403 __u32 ebx;
404 __u32 ecx;
405 __u32 edx;
406 __u32 padding;
407};
408
409/* for KVM_SET_CPUID */
410struct kvm_cpuid {
411 __u32 nent;
412 __u32 padding;
413 struct kvm_cpuid_entry entries[0];
414};
415
4164.20 KVM_SET_SIGNAL_MASK
417
418Capability: basic
419Architectures: x86
420Type: vcpu ioctl
421Parameters: struct kvm_signal_mask (in)
422Returns: 0 on success, -1 on error
423
424Defines which signals are blocked during execution of KVM_RUN. This
425signal mask temporarily overrides the threads signal mask. Any
426unblocked signal received (except SIGKILL and SIGSTOP, which retain
427their traditional behaviour) will cause KVM_RUN to return with -EINTR.
428
429Note the signal will only be delivered if not blocked by the original
430signal mask.
431
432/* for KVM_SET_SIGNAL_MASK */
433struct kvm_signal_mask {
434 __u32 len;
435 __u8 sigset[0];
436};
437
4384.21 KVM_GET_FPU
439
440Capability: basic
441Architectures: x86
442Type: vcpu ioctl
443Parameters: struct kvm_fpu (out)
444Returns: 0 on success, -1 on error
445
446Reads the floating point state from the vcpu.
447
448/* for KVM_GET_FPU and KVM_SET_FPU */
449struct kvm_fpu {
450 __u8 fpr[8][16];
451 __u16 fcw;
452 __u16 fsw;
453 __u8 ftwx; /* in fxsave format */
454 __u8 pad1;
455 __u16 last_opcode;
456 __u64 last_ip;
457 __u64 last_dp;
458 __u8 xmm[16][16];
459 __u32 mxcsr;
460 __u32 pad2;
461};
462
4634.22 KVM_SET_FPU
464
465Capability: basic
466Architectures: x86
467Type: vcpu ioctl
468Parameters: struct kvm_fpu (in)
469Returns: 0 on success, -1 on error
470
471Writes the floating point state to the vcpu.
472
473/* for KVM_GET_FPU and KVM_SET_FPU */
474struct kvm_fpu {
475 __u8 fpr[8][16];
476 __u16 fcw;
477 __u16 fsw;
478 __u8 ftwx; /* in fxsave format */
479 __u8 pad1;
480 __u16 last_opcode;
481 __u64 last_ip;
482 __u64 last_dp;
483 __u8 xmm[16][16];
484 __u32 mxcsr;
485 __u32 pad2;
486};
487
Avi Kivity5dadbfd2009-08-23 17:08:04 +03004884.23 KVM_CREATE_IRQCHIP
489
490Capability: KVM_CAP_IRQCHIP
491Architectures: x86, ia64
492Type: vm ioctl
493Parameters: none
494Returns: 0 on success, -1 on error
495
496Creates an interrupt controller model in the kernel. On x86, creates a virtual
497ioapic, a virtual PIC (two PICs, nested), and sets up future vcpus to have a
498local APIC. IRQ routing for GSIs 0-15 is set to both PIC and IOAPIC; GSI 16-23
499only go to the IOAPIC. On ia64, a IOSAPIC is created.
500
5014.24 KVM_IRQ_LINE
502
503Capability: KVM_CAP_IRQCHIP
504Architectures: x86, ia64
505Type: vm ioctl
506Parameters: struct kvm_irq_level
507Returns: 0 on success, -1 on error
508
509Sets the level of a GSI input to the interrupt controller model in the kernel.
510Requires that an interrupt controller model has been previously created with
511KVM_CREATE_IRQCHIP. Note that edge-triggered interrupts require the level
512to be set to 1 and then back to 0.
513
514struct kvm_irq_level {
515 union {
516 __u32 irq; /* GSI */
517 __s32 status; /* not used for KVM_IRQ_LEVEL */
518 };
519 __u32 level; /* 0 or 1 */
520};
521
5224.25 KVM_GET_IRQCHIP
523
524Capability: KVM_CAP_IRQCHIP
525Architectures: x86, ia64
526Type: vm ioctl
527Parameters: struct kvm_irqchip (in/out)
528Returns: 0 on success, -1 on error
529
530Reads the state of a kernel interrupt controller created with
531KVM_CREATE_IRQCHIP into a buffer provided by the caller.
532
533struct kvm_irqchip {
534 __u32 chip_id; /* 0 = PIC1, 1 = PIC2, 2 = IOAPIC */
535 __u32 pad;
536 union {
537 char dummy[512]; /* reserving space */
538 struct kvm_pic_state pic;
539 struct kvm_ioapic_state ioapic;
540 } chip;
541};
542
5434.26 KVM_SET_IRQCHIP
544
545Capability: KVM_CAP_IRQCHIP
546Architectures: x86, ia64
547Type: vm ioctl
548Parameters: struct kvm_irqchip (in)
549Returns: 0 on success, -1 on error
550
551Sets the state of a kernel interrupt controller created with
552KVM_CREATE_IRQCHIP from a buffer provided by the caller.
553
554struct kvm_irqchip {
555 __u32 chip_id; /* 0 = PIC1, 1 = PIC2, 2 = IOAPIC */
556 __u32 pad;
557 union {
558 char dummy[512]; /* reserving space */
559 struct kvm_pic_state pic;
560 struct kvm_ioapic_state ioapic;
561 } chip;
562};
563
Ed Swierkffde22a2009-10-15 15:21:43 -07005644.27 KVM_XEN_HVM_CONFIG
565
566Capability: KVM_CAP_XEN_HVM
567Architectures: x86
568Type: vm ioctl
569Parameters: struct kvm_xen_hvm_config (in)
570Returns: 0 on success, -1 on error
571
572Sets the MSR that the Xen HVM guest uses to initialize its hypercall
573page, and provides the starting address and size of the hypercall
574blobs in userspace. When the guest writes the MSR, kvm copies one
575page of a blob (32- or 64-bit, depending on the vcpu mode) to guest
576memory.
577
578struct kvm_xen_hvm_config {
579 __u32 flags;
580 __u32 msr;
581 __u64 blob_addr_32;
582 __u64 blob_addr_64;
583 __u8 blob_size_32;
584 __u8 blob_size_64;
585 __u8 pad2[30];
586};
587
Glauber Costaafbcf7a2009-10-16 15:28:36 -04005884.27 KVM_GET_CLOCK
589
590Capability: KVM_CAP_ADJUST_CLOCK
591Architectures: x86
592Type: vm ioctl
593Parameters: struct kvm_clock_data (out)
594Returns: 0 on success, -1 on error
595
596Gets the current timestamp of kvmclock as seen by the current guest. In
597conjunction with KVM_SET_CLOCK, it is used to ensure monotonicity on scenarios
598such as migration.
599
600struct kvm_clock_data {
601 __u64 clock; /* kvmclock current value */
602 __u32 flags;
603 __u32 pad[9];
604};
605
6064.28 KVM_SET_CLOCK
607
608Capability: KVM_CAP_ADJUST_CLOCK
609Architectures: x86
610Type: vm ioctl
611Parameters: struct kvm_clock_data (in)
612Returns: 0 on success, -1 on error
613
Wu Fengguang20448922009-12-24 09:04:16 +0800614Sets the current timestamp of kvmclock to the value specified in its parameter.
Glauber Costaafbcf7a2009-10-16 15:28:36 -0400615In conjunction with KVM_GET_CLOCK, it is used to ensure monotonicity on scenarios
616such as migration.
617
618struct kvm_clock_data {
619 __u64 clock; /* kvmclock current value */
620 __u32 flags;
621 __u32 pad[9];
622};
623
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006244.29 KVM_GET_VCPU_EVENTS
625
626Capability: KVM_CAP_VCPU_EVENTS
Jan Kiszka48005f62010-02-19 19:38:07 +0100627Extended by: KVM_CAP_INTR_SHADOW
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100628Architectures: x86
629Type: vm ioctl
630Parameters: struct kvm_vcpu_event (out)
631Returns: 0 on success, -1 on error
632
633Gets currently pending exceptions, interrupts, and NMIs as well as related
634states of the vcpu.
635
636struct kvm_vcpu_events {
637 struct {
638 __u8 injected;
639 __u8 nr;
640 __u8 has_error_code;
641 __u8 pad;
642 __u32 error_code;
643 } exception;
644 struct {
645 __u8 injected;
646 __u8 nr;
647 __u8 soft;
Jan Kiszka48005f62010-02-19 19:38:07 +0100648 __u8 shadow;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100649 } interrupt;
650 struct {
651 __u8 injected;
652 __u8 pending;
653 __u8 masked;
654 __u8 pad;
655 } nmi;
656 __u32 sipi_vector;
Jan Kiszkadab4b912009-12-06 18:24:15 +0100657 __u32 flags;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100658};
659
Jan Kiszka48005f62010-02-19 19:38:07 +0100660KVM_VCPUEVENT_VALID_SHADOW may be set in the flags field to signal that
661interrupt.shadow contains a valid state. Otherwise, this field is undefined.
662
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006634.30 KVM_SET_VCPU_EVENTS
664
665Capability: KVM_CAP_VCPU_EVENTS
Jan Kiszka48005f62010-02-19 19:38:07 +0100666Extended by: KVM_CAP_INTR_SHADOW
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100667Architectures: x86
668Type: vm ioctl
669Parameters: struct kvm_vcpu_event (in)
670Returns: 0 on success, -1 on error
671
672Set pending exceptions, interrupts, and NMIs as well as related states of the
673vcpu.
674
675See KVM_GET_VCPU_EVENTS for the data structure.
676
Jan Kiszkadab4b912009-12-06 18:24:15 +0100677Fields that may be modified asynchronously by running VCPUs can be excluded
678from the update. These fields are nmi.pending and sipi_vector. Keep the
679corresponding bits in the flags field cleared to suppress overwriting the
680current in-kernel state. The bits are:
681
682KVM_VCPUEVENT_VALID_NMI_PENDING - transfer nmi.pending to the kernel
683KVM_VCPUEVENT_VALID_SIPI_VECTOR - transfer sipi_vector
684
Jan Kiszka48005f62010-02-19 19:38:07 +0100685If KVM_CAP_INTR_SHADOW is available, KVM_VCPUEVENT_VALID_SHADOW can be set in
686the flags field to signal that interrupt.shadow contains a valid state and
687shall be written into the VCPU.
688
Jan Kiszkaa1efbe72010-02-15 10:45:43 +01006894.32 KVM_GET_DEBUGREGS
690
691Capability: KVM_CAP_DEBUGREGS
692Architectures: x86
693Type: vm ioctl
694Parameters: struct kvm_debugregs (out)
695Returns: 0 on success, -1 on error
696
697Reads debug registers from the vcpu.
698
699struct kvm_debugregs {
700 __u64 db[4];
701 __u64 dr6;
702 __u64 dr7;
703 __u64 flags;
704 __u64 reserved[9];
705};
706
7074.33 KVM_SET_DEBUGREGS
708
709Capability: KVM_CAP_DEBUGREGS
710Architectures: x86
711Type: vm ioctl
712Parameters: struct kvm_debugregs (in)
713Returns: 0 on success, -1 on error
714
715Writes debug registers into the vcpu.
716
717See KVM_GET_DEBUGREGS for the data structure. The flags field is unused
718yet and must be cleared on entry.
719
Avi Kivity0f2d8f42010-03-25 12:16:48 +02007204.34 KVM_SET_USER_MEMORY_REGION
721
722Capability: KVM_CAP_USER_MEM
723Architectures: all
724Type: vm ioctl
725Parameters: struct kvm_userspace_memory_region (in)
726Returns: 0 on success, -1 on error
727
728struct kvm_userspace_memory_region {
729 __u32 slot;
730 __u32 flags;
731 __u64 guest_phys_addr;
732 __u64 memory_size; /* bytes */
733 __u64 userspace_addr; /* start of the userspace allocated memory */
734};
735
736/* for kvm_memory_region::flags */
737#define KVM_MEM_LOG_DIRTY_PAGES 1UL
738
739This ioctl allows the user to create or modify a guest physical memory
740slot. When changing an existing slot, it may be moved in the guest
741physical memory space, or its flags may be modified. It may not be
742resized. Slots may not overlap in guest physical address space.
743
744Memory for the region is taken starting at the address denoted by the
745field userspace_addr, which must point at user addressable memory for
746the entire memory slot size. Any object may back this memory, including
747anonymous memory, ordinary files, and hugetlbfs.
748
749It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr
750be identical. This allows large pages in the guest to be backed by large
751pages in the host.
752
753The flags field supports just one flag, KVM_MEM_LOG_DIRTY_PAGES, which
754instructs kvm to keep track of writes to memory within the slot. See
755the KVM_GET_DIRTY_LOG ioctl.
756
757When the KVM_CAP_SYNC_MMU capability, changes in the backing of the memory
758region are automatically reflected into the guest. For example, an mmap()
759that affects the region will be made visible immediately. Another example
760is madvise(MADV_DROP).
761
762It is recommended to use this API instead of the KVM_SET_MEMORY_REGION ioctl.
763The KVM_SET_MEMORY_REGION does not allow fine grained control over memory
764allocation and is deprecated.
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100765
Avi Kivity8a5416d2010-03-25 12:27:30 +02007664.35 KVM_SET_TSS_ADDR
767
768Capability: KVM_CAP_SET_TSS_ADDR
769Architectures: x86
770Type: vm ioctl
771Parameters: unsigned long tss_address (in)
772Returns: 0 on success, -1 on error
773
774This ioctl defines the physical address of a three-page region in the guest
775physical address space. The region must be within the first 4GB of the
776guest physical address space and must not conflict with any memory slot
777or any mmio address. The guest may malfunction if it accesses this memory
778region.
779
780This ioctl is required on Intel-based hosts. This is needed on Intel hardware
781because of a quirk in the virtualization implementation (see the internals
782documentation when it pops into existence).
783
Alexander Graf71fbfd52010-03-24 21:48:29 +01007844.36 KVM_ENABLE_CAP
785
786Capability: KVM_CAP_ENABLE_CAP
787Architectures: ppc
788Type: vcpu ioctl
789Parameters: struct kvm_enable_cap (in)
790Returns: 0 on success; -1 on error
791
792+Not all extensions are enabled by default. Using this ioctl the application
793can enable an extension, making it available to the guest.
794
795On systems that do not support this ioctl, it always fails. On systems that
796do support it, it only works for extensions that are supported for enablement.
797
798To check if a capability can be enabled, the KVM_CHECK_EXTENSION ioctl should
799be used.
800
801struct kvm_enable_cap {
802 /* in */
803 __u32 cap;
804
805The capability that is supposed to get enabled.
806
807 __u32 flags;
808
809A bitfield indicating future enhancements. Has to be 0 for now.
810
811 __u64 args[4];
812
813Arguments for enabling a feature. If a feature needs initial values to
814function properly, this is the place to put them.
815
816 __u8 pad[64];
817};
818
Avi Kivityb843f062010-04-25 15:51:46 +03008194.37 KVM_GET_MP_STATE
820
821Capability: KVM_CAP_MP_STATE
822Architectures: x86, ia64
823Type: vcpu ioctl
824Parameters: struct kvm_mp_state (out)
825Returns: 0 on success; -1 on error
826
827struct kvm_mp_state {
828 __u32 mp_state;
829};
830
831Returns the vcpu's current "multiprocessing state" (though also valid on
832uniprocessor guests).
833
834Possible values are:
835
836 - KVM_MP_STATE_RUNNABLE: the vcpu is currently running
837 - KVM_MP_STATE_UNINITIALIZED: the vcpu is an application processor (AP)
838 which has not yet received an INIT signal
839 - KVM_MP_STATE_INIT_RECEIVED: the vcpu has received an INIT signal, and is
840 now ready for a SIPI
841 - KVM_MP_STATE_HALTED: the vcpu has executed a HLT instruction and
842 is waiting for an interrupt
843 - KVM_MP_STATE_SIPI_RECEIVED: the vcpu has just received a SIPI (vector
844 accesible via KVM_GET_VCPU_EVENTS)
845
846This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
847irqchip, the multiprocessing state must be maintained by userspace.
848
8494.38 KVM_SET_MP_STATE
850
851Capability: KVM_CAP_MP_STATE
852Architectures: x86, ia64
853Type: vcpu ioctl
854Parameters: struct kvm_mp_state (in)
855Returns: 0 on success; -1 on error
856
857Sets the vcpu's current "multiprocessing state"; see KVM_GET_MP_STATE for
858arguments.
859
860This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
861irqchip, the multiprocessing state must be maintained by userspace.
862
Avi Kivity47dbb842010-04-29 12:08:56 +03008634.39 KVM_SET_IDENTITY_MAP_ADDR
864
865Capability: KVM_CAP_SET_IDENTITY_MAP_ADDR
866Architectures: x86
867Type: vm ioctl
868Parameters: unsigned long identity (in)
869Returns: 0 on success, -1 on error
870
871This ioctl defines the physical address of a one-page region in the guest
872physical address space. The region must be within the first 4GB of the
873guest physical address space and must not conflict with any memory slot
874or any mmio address. The guest may malfunction if it accesses this memory
875region.
876
877This ioctl is required on Intel-based hosts. This is needed on Intel hardware
878because of a quirk in the virtualization implementation (see the internals
879documentation when it pops into existence).
880
Avi Kivity57bc24c2010-04-29 12:12:57 +03008814.40 KVM_SET_BOOT_CPU_ID
882
883Capability: KVM_CAP_SET_BOOT_CPU_ID
884Architectures: x86, ia64
885Type: vm ioctl
886Parameters: unsigned long vcpu_id
887Returns: 0 on success, -1 on error
888
889Define which vcpu is the Bootstrap Processor (BSP). Values are the same
890as the vcpu id in KVM_CREATE_VCPU. If this ioctl is not called, the default
891is vcpu 0.
892
Sheng Yang2d5b5a62010-06-13 17:29:39 +08008934.41 KVM_GET_XSAVE
894
895Capability: KVM_CAP_XSAVE
896Architectures: x86
897Type: vcpu ioctl
898Parameters: struct kvm_xsave (out)
899Returns: 0 on success, -1 on error
900
901struct kvm_xsave {
902 __u32 region[1024];
903};
904
905This ioctl would copy current vcpu's xsave struct to the userspace.
906
9074.42 KVM_SET_XSAVE
908
909Capability: KVM_CAP_XSAVE
910Architectures: x86
911Type: vcpu ioctl
912Parameters: struct kvm_xsave (in)
913Returns: 0 on success, -1 on error
914
915struct kvm_xsave {
916 __u32 region[1024];
917};
918
919This ioctl would copy userspace's xsave struct to the kernel.
920
9214.43 KVM_GET_XCRS
922
923Capability: KVM_CAP_XCRS
924Architectures: x86
925Type: vcpu ioctl
926Parameters: struct kvm_xcrs (out)
927Returns: 0 on success, -1 on error
928
929struct kvm_xcr {
930 __u32 xcr;
931 __u32 reserved;
932 __u64 value;
933};
934
935struct kvm_xcrs {
936 __u32 nr_xcrs;
937 __u32 flags;
938 struct kvm_xcr xcrs[KVM_MAX_XCRS];
939 __u64 padding[16];
940};
941
942This ioctl would copy current vcpu's xcrs to the userspace.
943
9444.44 KVM_SET_XCRS
945
946Capability: KVM_CAP_XCRS
947Architectures: x86
948Type: vcpu ioctl
949Parameters: struct kvm_xcrs (in)
950Returns: 0 on success, -1 on error
951
952struct kvm_xcr {
953 __u32 xcr;
954 __u32 reserved;
955 __u64 value;
956};
957
958struct kvm_xcrs {
959 __u32 nr_xcrs;
960 __u32 flags;
961 struct kvm_xcr xcrs[KVM_MAX_XCRS];
962 __u64 padding[16];
963};
964
965This ioctl would set vcpu's xcr to the value userspace specified.
966
Avi Kivity9c1b96e2009-06-09 12:37:58 +03009675. The kvm_run structure
968
969Application code obtains a pointer to the kvm_run structure by
970mmap()ing a vcpu fd. From that point, application code can control
971execution by changing fields in kvm_run prior to calling the KVM_RUN
972ioctl, and obtain information about the reason KVM_RUN returned by
973looking up structure members.
974
975struct kvm_run {
976 /* in */
977 __u8 request_interrupt_window;
978
979Request that KVM_RUN return when it becomes possible to inject external
980interrupts into the guest. Useful in conjunction with KVM_INTERRUPT.
981
982 __u8 padding1[7];
983
984 /* out */
985 __u32 exit_reason;
986
987When KVM_RUN has returned successfully (return value 0), this informs
988application code why KVM_RUN has returned. Allowable values for this
989field are detailed below.
990
991 __u8 ready_for_interrupt_injection;
992
993If request_interrupt_window has been specified, this field indicates
994an interrupt can be injected now with KVM_INTERRUPT.
995
996 __u8 if_flag;
997
998The value of the current interrupt flag. Only valid if in-kernel
999local APIC is not used.
1000
1001 __u8 padding2[2];
1002
1003 /* in (pre_kvm_run), out (post_kvm_run) */
1004 __u64 cr8;
1005
1006The value of the cr8 register. Only valid if in-kernel local APIC is
1007not used. Both input and output.
1008
1009 __u64 apic_base;
1010
1011The value of the APIC BASE msr. Only valid if in-kernel local
1012APIC is not used. Both input and output.
1013
1014 union {
1015 /* KVM_EXIT_UNKNOWN */
1016 struct {
1017 __u64 hardware_exit_reason;
1018 } hw;
1019
1020If exit_reason is KVM_EXIT_UNKNOWN, the vcpu has exited due to unknown
1021reasons. Further architecture-specific information is available in
1022hardware_exit_reason.
1023
1024 /* KVM_EXIT_FAIL_ENTRY */
1025 struct {
1026 __u64 hardware_entry_failure_reason;
1027 } fail_entry;
1028
1029If exit_reason is KVM_EXIT_FAIL_ENTRY, the vcpu could not be run due
1030to unknown reasons. Further architecture-specific information is
1031available in hardware_entry_failure_reason.
1032
1033 /* KVM_EXIT_EXCEPTION */
1034 struct {
1035 __u32 exception;
1036 __u32 error_code;
1037 } ex;
1038
1039Unused.
1040
1041 /* KVM_EXIT_IO */
1042 struct {
1043#define KVM_EXIT_IO_IN 0
1044#define KVM_EXIT_IO_OUT 1
1045 __u8 direction;
1046 __u8 size; /* bytes */
1047 __u16 port;
1048 __u32 count;
1049 __u64 data_offset; /* relative to kvm_run start */
1050 } io;
1051
Wu Fengguang20448922009-12-24 09:04:16 +08001052If exit_reason is KVM_EXIT_IO, then the vcpu has
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001053executed a port I/O instruction which could not be satisfied by kvm.
1054data_offset describes where the data is located (KVM_EXIT_IO_OUT) or
1055where kvm expects application code to place the data for the next
Wu Fengguang20448922009-12-24 09:04:16 +08001056KVM_RUN invocation (KVM_EXIT_IO_IN). Data format is a packed array.
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001057
1058 struct {
1059 struct kvm_debug_exit_arch arch;
1060 } debug;
1061
1062Unused.
1063
1064 /* KVM_EXIT_MMIO */
1065 struct {
1066 __u64 phys_addr;
1067 __u8 data[8];
1068 __u32 len;
1069 __u8 is_write;
1070 } mmio;
1071
Wu Fengguang20448922009-12-24 09:04:16 +08001072If exit_reason is KVM_EXIT_MMIO, then the vcpu has
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001073executed a memory-mapped I/O instruction which could not be satisfied
1074by kvm. The 'data' member contains the written data if 'is_write' is
1075true, and should be filled by application code otherwise.
1076
Alexander Grafad0a0482010-03-24 21:48:30 +01001077NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO and KVM_EXIT_OSI, the corresponding
1078operations are complete (and guest state is consistent) only after userspace
1079has re-entered the kernel with KVM_RUN. The kernel side will first finish
Marcelo Tosatti67961342010-02-13 16:10:26 -02001080incomplete operations and then check for pending signals. Userspace
1081can re-enter the guest with an unmasked signal pending to complete
1082pending operations.
1083
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001084 /* KVM_EXIT_HYPERCALL */
1085 struct {
1086 __u64 nr;
1087 __u64 args[6];
1088 __u64 ret;
1089 __u32 longmode;
1090 __u32 pad;
1091 } hypercall;
1092
Avi Kivity647dc492010-04-01 14:39:21 +03001093Unused. This was once used for 'hypercall to userspace'. To implement
1094such functionality, use KVM_EXIT_IO (x86) or KVM_EXIT_MMIO (all except s390).
1095Note KVM_EXIT_IO is significantly faster than KVM_EXIT_MMIO.
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001096
1097 /* KVM_EXIT_TPR_ACCESS */
1098 struct {
1099 __u64 rip;
1100 __u32 is_write;
1101 __u32 pad;
1102 } tpr_access;
1103
1104To be documented (KVM_TPR_ACCESS_REPORTING).
1105
1106 /* KVM_EXIT_S390_SIEIC */
1107 struct {
1108 __u8 icptcode;
1109 __u64 mask; /* psw upper half */
1110 __u64 addr; /* psw lower half */
1111 __u16 ipa;
1112 __u32 ipb;
1113 } s390_sieic;
1114
1115s390 specific.
1116
1117 /* KVM_EXIT_S390_RESET */
1118#define KVM_S390_RESET_POR 1
1119#define KVM_S390_RESET_CLEAR 2
1120#define KVM_S390_RESET_SUBSYSTEM 4
1121#define KVM_S390_RESET_CPU_INIT 8
1122#define KVM_S390_RESET_IPL 16
1123 __u64 s390_reset_flags;
1124
1125s390 specific.
1126
1127 /* KVM_EXIT_DCR */
1128 struct {
1129 __u32 dcrn;
1130 __u32 data;
1131 __u8 is_write;
1132 } dcr;
1133
1134powerpc specific.
1135
Alexander Grafad0a0482010-03-24 21:48:30 +01001136 /* KVM_EXIT_OSI */
1137 struct {
1138 __u64 gprs[32];
1139 } osi;
1140
1141MOL uses a special hypercall interface it calls 'OSI'. To enable it, we catch
1142hypercalls and exit with this exit struct that contains all the guest gprs.
1143
1144If exit_reason is KVM_EXIT_OSI, then the vcpu has triggered such a hypercall.
1145Userspace can now handle the hypercall and when it's done modify the gprs as
1146necessary. Upon guest entry all guest GPRs will then be replaced by the values
1147in this struct.
1148
Avi Kivity9c1b96e2009-06-09 12:37:58 +03001149 /* Fix the size of the union. */
1150 char padding[256];
1151 };
1152};