api.txt 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. The Definitive KVM (Kernel-based Virtual Machine) API Documentation
  2. ===================================================================
  3. 1. General description
  4. The kvm API is a set of ioctls that are issued to control various aspects
  5. of a virtual machine. The ioctls belong to three classes
  6. - System ioctls: These query and set global attributes which affect the
  7. whole kvm subsystem. In addition a system ioctl is used to create
  8. virtual machines
  9. - VM ioctls: These query and set attributes that affect an entire virtual
  10. machine, for example memory layout. In addition a VM ioctl is used to
  11. create virtual cpus (vcpus).
  12. Only run VM ioctls from the same process (address space) that was used
  13. to create the VM.
  14. - vcpu ioctls: These query and set attributes that control the operation
  15. of a single virtual cpu.
  16. Only run vcpu ioctls from the same thread that was used to create the
  17. vcpu.
  18. 2. File descriptors
  19. The kvm API is centered around file descriptors. An initial
  20. open("/dev/kvm") obtains a handle to the kvm subsystem; this handle
  21. can be used to issue system ioctls. A KVM_CREATE_VM ioctl on this
  22. handle will create a VM file descriptor which can be used to issue VM
  23. ioctls. A KVM_CREATE_VCPU ioctl on a VM fd will create a virtual cpu
  24. and return a file descriptor pointing to it. Finally, ioctls on a vcpu
  25. fd can be used to control the vcpu, including the important task of
  26. actually running guest code.
  27. In general file descriptors can be migrated among processes by means
  28. of fork() and the SCM_RIGHTS facility of unix domain socket. These
  29. kinds of tricks are explicitly not supported by kvm. While they will
  30. not cause harm to the host, their actual behavior is not guaranteed by
  31. the API. The only supported use is one virtual machine per process,
  32. and one vcpu per thread.
  33. 3. Extensions
  34. As of Linux 2.6.22, the KVM ABI has been stabilized: no backward
  35. incompatible change are allowed. However, there is an extension
  36. facility that allows backward-compatible extensions to the API to be
  37. queried and used.
  38. The extension mechanism is not based on on the Linux version number.
  39. Instead, kvm defines extension identifiers and a facility to query
  40. whether a particular extension identifier is available. If it is, a
  41. set of ioctls is available for application use.
  42. 4. API description
  43. This section describes ioctls that can be used to control kvm guests.
  44. For each ioctl, the following information is provided along with a
  45. description:
  46. Capability: which KVM extension provides this ioctl. Can be 'basic',
  47. which means that is will be provided by any kernel that supports
  48. API version 12 (see section 4.1), or a KVM_CAP_xyz constant, which
  49. means availability needs to be checked with KVM_CHECK_EXTENSION
  50. (see section 4.4).
  51. Architectures: which instruction set architectures provide this ioctl.
  52. x86 includes both i386 and x86_64.
  53. Type: system, vm, or vcpu.
  54. Parameters: what parameters are accepted by the ioctl.
  55. Returns: the return value. General error numbers (EBADF, ENOMEM, EINVAL)
  56. are not detailed, but errors with specific meanings are.
  57. 4.1 KVM_GET_API_VERSION
  58. Capability: basic
  59. Architectures: all
  60. Type: system ioctl
  61. Parameters: none
  62. Returns: the constant KVM_API_VERSION (=12)
  63. This identifies the API version as the stable kvm API. It is not
  64. expected that this number will change. However, Linux 2.6.20 and
  65. 2.6.21 report earlier versions; these are not documented and not
  66. supported. Applications should refuse to run if KVM_GET_API_VERSION
  67. returns a value other than 12. If this check passes, all ioctls
  68. described as 'basic' will be available.
  69. 4.2 KVM_CREATE_VM
  70. Capability: basic
  71. Architectures: all
  72. Type: system ioctl
  73. Parameters: none
  74. Returns: a VM fd that can be used to control the new virtual machine.
  75. The new VM has no virtual cpus and no memory. An mmap() of a VM fd
  76. will access the virtual machine's physical address space; offset zero
  77. corresponds to guest physical address zero. Use of mmap() on a VM fd
  78. is discouraged if userspace memory allocation (KVM_CAP_USER_MEMORY) is
  79. available.
  80. 4.3 KVM_GET_MSR_INDEX_LIST
  81. Capability: basic
  82. Architectures: x86
  83. Type: system
  84. Parameters: struct kvm_msr_list (in/out)
  85. Returns: 0 on success; -1 on error
  86. Errors:
  87. E2BIG: the msr index list is to be to fit in the array specified by
  88. the user.
  89. struct kvm_msr_list {
  90. __u32 nmsrs; /* number of msrs in entries */
  91. __u32 indices[0];
  92. };
  93. This ioctl returns the guest msrs that are supported. The list varies
  94. by kvm version and host processor, but does not change otherwise. The
  95. user fills in the size of the indices array in nmsrs, and in return
  96. kvm adjusts nmsrs to reflect the actual number of msrs and fills in
  97. the indices array with their numbers.
  98. Note: if kvm indicates supports MCE (KVM_CAP_MCE), then the MCE bank MSRs are
  99. not returned in the MSR list, as different vcpus can have a different number
  100. of banks, as set via the KVM_X86_SETUP_MCE ioctl.
  101. 4.4 KVM_CHECK_EXTENSION
  102. Capability: basic
  103. Architectures: all
  104. Type: system ioctl
  105. Parameters: extension identifier (KVM_CAP_*)
  106. Returns: 0 if unsupported; 1 (or some other positive integer) if supported
  107. The API allows the application to query about extensions to the core
  108. kvm API. Userspace passes an extension identifier (an integer) and
  109. receives an integer that describes the extension availability.
  110. Generally 0 means no and 1 means yes, but some extensions may report
  111. additional information in the integer return value.
  112. 4.5 KVM_GET_VCPU_MMAP_SIZE
  113. Capability: basic
  114. Architectures: all
  115. Type: system ioctl
  116. Parameters: none
  117. Returns: size of vcpu mmap area, in bytes
  118. The KVM_RUN ioctl (cf.) communicates with userspace via a shared
  119. memory region. This ioctl returns the size of that region. See the
  120. KVM_RUN documentation for details.
  121. 4.6 KVM_SET_MEMORY_REGION
  122. Capability: basic
  123. Architectures: all
  124. Type: vm ioctl
  125. Parameters: struct kvm_memory_region (in)
  126. Returns: 0 on success, -1 on error
  127. This ioctl is obsolete and has been removed.
  128. 4.6 KVM_CREATE_VCPU
  129. Capability: basic
  130. Architectures: all
  131. Type: vm ioctl
  132. Parameters: vcpu id (apic id on x86)
  133. Returns: vcpu fd on success, -1 on error
  134. This API adds a vcpu to a virtual machine. The vcpu id is a small integer
  135. in the range [0, max_vcpus).
  136. 4.7 KVM_GET_DIRTY_LOG (vm ioctl)
  137. Capability: basic
  138. Architectures: x86
  139. Type: vm ioctl
  140. Parameters: struct kvm_dirty_log (in/out)
  141. Returns: 0 on success, -1 on error
  142. /* for KVM_GET_DIRTY_LOG */
  143. struct kvm_dirty_log {
  144. __u32 slot;
  145. __u32 padding;
  146. union {
  147. void __user *dirty_bitmap; /* one bit per page */
  148. __u64 padding;
  149. };
  150. };
  151. Given a memory slot, return a bitmap containing any pages dirtied
  152. since the last call to this ioctl. Bit 0 is the first page in the
  153. memory slot. Ensure the entire structure is cleared to avoid padding
  154. issues.
  155. 4.8 KVM_SET_MEMORY_ALIAS
  156. Capability: basic
  157. Architectures: x86
  158. Type: vm ioctl
  159. Parameters: struct kvm_memory_alias (in)
  160. Returns: 0 (success), -1 (error)
  161. This ioctl is obsolete and has been removed.
  162. 4.9 KVM_RUN
  163. Capability: basic
  164. Architectures: all
  165. Type: vcpu ioctl
  166. Parameters: none
  167. Returns: 0 on success, -1 on error
  168. Errors:
  169. EINTR: an unmasked signal is pending
  170. This ioctl is used to run a guest virtual cpu. While there are no
  171. explicit parameters, there is an implicit parameter block that can be
  172. obtained by mmap()ing the vcpu fd at offset 0, with the size given by
  173. KVM_GET_VCPU_MMAP_SIZE. The parameter block is formatted as a 'struct
  174. kvm_run' (see below).
  175. 4.10 KVM_GET_REGS
  176. Capability: basic
  177. Architectures: all
  178. Type: vcpu ioctl
  179. Parameters: struct kvm_regs (out)
  180. Returns: 0 on success, -1 on error
  181. Reads the general purpose registers from the vcpu.
  182. /* x86 */
  183. struct kvm_regs {
  184. /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
  185. __u64 rax, rbx, rcx, rdx;
  186. __u64 rsi, rdi, rsp, rbp;
  187. __u64 r8, r9, r10, r11;
  188. __u64 r12, r13, r14, r15;
  189. __u64 rip, rflags;
  190. };
  191. 4.11 KVM_SET_REGS
  192. Capability: basic
  193. Architectures: all
  194. Type: vcpu ioctl
  195. Parameters: struct kvm_regs (in)
  196. Returns: 0 on success, -1 on error
  197. Writes the general purpose registers into the vcpu.
  198. See KVM_GET_REGS for the data structure.
  199. 4.12 KVM_GET_SREGS
  200. Capability: basic
  201. Architectures: x86
  202. Type: vcpu ioctl
  203. Parameters: struct kvm_sregs (out)
  204. Returns: 0 on success, -1 on error
  205. Reads special registers from the vcpu.
  206. /* x86 */
  207. struct kvm_sregs {
  208. struct kvm_segment cs, ds, es, fs, gs, ss;
  209. struct kvm_segment tr, ldt;
  210. struct kvm_dtable gdt, idt;
  211. __u64 cr0, cr2, cr3, cr4, cr8;
  212. __u64 efer;
  213. __u64 apic_base;
  214. __u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
  215. };
  216. interrupt_bitmap is a bitmap of pending external interrupts. At most
  217. one bit may be set. This interrupt has been acknowledged by the APIC
  218. but not yet injected into the cpu core.
  219. 4.13 KVM_SET_SREGS
  220. Capability: basic
  221. Architectures: x86
  222. Type: vcpu ioctl
  223. Parameters: struct kvm_sregs (in)
  224. Returns: 0 on success, -1 on error
  225. Writes special registers into the vcpu. See KVM_GET_SREGS for the
  226. data structures.
  227. 4.14 KVM_TRANSLATE
  228. Capability: basic
  229. Architectures: x86
  230. Type: vcpu ioctl
  231. Parameters: struct kvm_translation (in/out)
  232. Returns: 0 on success, -1 on error
  233. Translates a virtual address according to the vcpu's current address
  234. translation mode.
  235. struct kvm_translation {
  236. /* in */
  237. __u64 linear_address;
  238. /* out */
  239. __u64 physical_address;
  240. __u8 valid;
  241. __u8 writeable;
  242. __u8 usermode;
  243. __u8 pad[5];
  244. };
  245. 4.15 KVM_INTERRUPT
  246. Capability: basic
  247. Architectures: x86, ppc
  248. Type: vcpu ioctl
  249. Parameters: struct kvm_interrupt (in)
  250. Returns: 0 on success, -1 on error
  251. Queues a hardware interrupt vector to be injected. This is only
  252. useful if in-kernel local APIC or equivalent is not used.
  253. /* for KVM_INTERRUPT */
  254. struct kvm_interrupt {
  255. /* in */
  256. __u32 irq;
  257. };
  258. X86:
  259. Note 'irq' is an interrupt vector, not an interrupt pin or line.
  260. PPC:
  261. Queues an external interrupt to be injected. This ioctl is overleaded
  262. with 3 different irq values:
  263. a) KVM_INTERRUPT_SET
  264. This injects an edge type external interrupt into the guest once it's ready
  265. to receive interrupts. When injected, the interrupt is done.
  266. b) KVM_INTERRUPT_UNSET
  267. This unsets any pending interrupt.
  268. Only available with KVM_CAP_PPC_UNSET_IRQ.
  269. c) KVM_INTERRUPT_SET_LEVEL
  270. This injects a level type external interrupt into the guest context. The
  271. interrupt stays pending until a specific ioctl with KVM_INTERRUPT_UNSET
  272. is triggered.
  273. Only available with KVM_CAP_PPC_IRQ_LEVEL.
  274. Note that any value for 'irq' other than the ones stated above is invalid
  275. and incurs unexpected behavior.
  276. 4.16 KVM_DEBUG_GUEST
  277. Capability: basic
  278. Architectures: none
  279. Type: vcpu ioctl
  280. Parameters: none)
  281. Returns: -1 on error
  282. Support for this has been removed. Use KVM_SET_GUEST_DEBUG instead.
  283. 4.17 KVM_GET_MSRS
  284. Capability: basic
  285. Architectures: x86
  286. Type: vcpu ioctl
  287. Parameters: struct kvm_msrs (in/out)
  288. Returns: 0 on success, -1 on error
  289. Reads model-specific registers from the vcpu. Supported msr indices can
  290. be obtained using KVM_GET_MSR_INDEX_LIST.
  291. struct kvm_msrs {
  292. __u32 nmsrs; /* number of msrs in entries */
  293. __u32 pad;
  294. struct kvm_msr_entry entries[0];
  295. };
  296. struct kvm_msr_entry {
  297. __u32 index;
  298. __u32 reserved;
  299. __u64 data;
  300. };
  301. Application code should set the 'nmsrs' member (which indicates the
  302. size of the entries array) and the 'index' member of each array entry.
  303. kvm will fill in the 'data' member.
  304. 4.18 KVM_SET_MSRS
  305. Capability: basic
  306. Architectures: x86
  307. Type: vcpu ioctl
  308. Parameters: struct kvm_msrs (in)
  309. Returns: 0 on success, -1 on error
  310. Writes model-specific registers to the vcpu. See KVM_GET_MSRS for the
  311. data structures.
  312. Application code should set the 'nmsrs' member (which indicates the
  313. size of the entries array), and the 'index' and 'data' members of each
  314. array entry.
  315. 4.19 KVM_SET_CPUID
  316. Capability: basic
  317. Architectures: x86
  318. Type: vcpu ioctl
  319. Parameters: struct kvm_cpuid (in)
  320. Returns: 0 on success, -1 on error
  321. Defines the vcpu responses to the cpuid instruction. Applications
  322. should use the KVM_SET_CPUID2 ioctl if available.
  323. struct kvm_cpuid_entry {
  324. __u32 function;
  325. __u32 eax;
  326. __u32 ebx;
  327. __u32 ecx;
  328. __u32 edx;
  329. __u32 padding;
  330. };
  331. /* for KVM_SET_CPUID */
  332. struct kvm_cpuid {
  333. __u32 nent;
  334. __u32 padding;
  335. struct kvm_cpuid_entry entries[0];
  336. };
  337. 4.20 KVM_SET_SIGNAL_MASK
  338. Capability: basic
  339. Architectures: x86
  340. Type: vcpu ioctl
  341. Parameters: struct kvm_signal_mask (in)
  342. Returns: 0 on success, -1 on error
  343. Defines which signals are blocked during execution of KVM_RUN. This
  344. signal mask temporarily overrides the threads signal mask. Any
  345. unblocked signal received (except SIGKILL and SIGSTOP, which retain
  346. their traditional behaviour) will cause KVM_RUN to return with -EINTR.
  347. Note the signal will only be delivered if not blocked by the original
  348. signal mask.
  349. /* for KVM_SET_SIGNAL_MASK */
  350. struct kvm_signal_mask {
  351. __u32 len;
  352. __u8 sigset[0];
  353. };
  354. 4.21 KVM_GET_FPU
  355. Capability: basic
  356. Architectures: x86
  357. Type: vcpu ioctl
  358. Parameters: struct kvm_fpu (out)
  359. Returns: 0 on success, -1 on error
  360. Reads the floating point state from the vcpu.
  361. /* for KVM_GET_FPU and KVM_SET_FPU */
  362. struct kvm_fpu {
  363. __u8 fpr[8][16];
  364. __u16 fcw;
  365. __u16 fsw;
  366. __u8 ftwx; /* in fxsave format */
  367. __u8 pad1;
  368. __u16 last_opcode;
  369. __u64 last_ip;
  370. __u64 last_dp;
  371. __u8 xmm[16][16];
  372. __u32 mxcsr;
  373. __u32 pad2;
  374. };
  375. 4.22 KVM_SET_FPU
  376. Capability: basic
  377. Architectures: x86
  378. Type: vcpu ioctl
  379. Parameters: struct kvm_fpu (in)
  380. Returns: 0 on success, -1 on error
  381. Writes the floating point state to the vcpu.
  382. /* for KVM_GET_FPU and KVM_SET_FPU */
  383. struct kvm_fpu {
  384. __u8 fpr[8][16];
  385. __u16 fcw;
  386. __u16 fsw;
  387. __u8 ftwx; /* in fxsave format */
  388. __u8 pad1;
  389. __u16 last_opcode;
  390. __u64 last_ip;
  391. __u64 last_dp;
  392. __u8 xmm[16][16];
  393. __u32 mxcsr;
  394. __u32 pad2;
  395. };
  396. 4.23 KVM_CREATE_IRQCHIP
  397. Capability: KVM_CAP_IRQCHIP
  398. Architectures: x86, ia64
  399. Type: vm ioctl
  400. Parameters: none
  401. Returns: 0 on success, -1 on error
  402. Creates an interrupt controller model in the kernel. On x86, creates a virtual
  403. ioapic, a virtual PIC (two PICs, nested), and sets up future vcpus to have a
  404. local APIC. IRQ routing for GSIs 0-15 is set to both PIC and IOAPIC; GSI 16-23
  405. only go to the IOAPIC. On ia64, a IOSAPIC is created.
  406. 4.24 KVM_IRQ_LINE
  407. Capability: KVM_CAP_IRQCHIP
  408. Architectures: x86, ia64
  409. Type: vm ioctl
  410. Parameters: struct kvm_irq_level
  411. Returns: 0 on success, -1 on error
  412. Sets the level of a GSI input to the interrupt controller model in the kernel.
  413. Requires that an interrupt controller model has been previously created with
  414. KVM_CREATE_IRQCHIP. Note that edge-triggered interrupts require the level
  415. to be set to 1 and then back to 0.
  416. struct kvm_irq_level {
  417. union {
  418. __u32 irq; /* GSI */
  419. __s32 status; /* not used for KVM_IRQ_LEVEL */
  420. };
  421. __u32 level; /* 0 or 1 */
  422. };
  423. 4.25 KVM_GET_IRQCHIP
  424. Capability: KVM_CAP_IRQCHIP
  425. Architectures: x86, ia64
  426. Type: vm ioctl
  427. Parameters: struct kvm_irqchip (in/out)
  428. Returns: 0 on success, -1 on error
  429. Reads the state of a kernel interrupt controller created with
  430. KVM_CREATE_IRQCHIP into a buffer provided by the caller.
  431. struct kvm_irqchip {
  432. __u32 chip_id; /* 0 = PIC1, 1 = PIC2, 2 = IOAPIC */
  433. __u32 pad;
  434. union {
  435. char dummy[512]; /* reserving space */
  436. struct kvm_pic_state pic;
  437. struct kvm_ioapic_state ioapic;
  438. } chip;
  439. };
  440. 4.26 KVM_SET_IRQCHIP
  441. Capability: KVM_CAP_IRQCHIP
  442. Architectures: x86, ia64
  443. Type: vm ioctl
  444. Parameters: struct kvm_irqchip (in)
  445. Returns: 0 on success, -1 on error
  446. Sets the state of a kernel interrupt controller created with
  447. KVM_CREATE_IRQCHIP from a buffer provided by the caller.
  448. struct kvm_irqchip {
  449. __u32 chip_id; /* 0 = PIC1, 1 = PIC2, 2 = IOAPIC */
  450. __u32 pad;
  451. union {
  452. char dummy[512]; /* reserving space */
  453. struct kvm_pic_state pic;
  454. struct kvm_ioapic_state ioapic;
  455. } chip;
  456. };
  457. 4.27 KVM_XEN_HVM_CONFIG
  458. Capability: KVM_CAP_XEN_HVM
  459. Architectures: x86
  460. Type: vm ioctl
  461. Parameters: struct kvm_xen_hvm_config (in)
  462. Returns: 0 on success, -1 on error
  463. Sets the MSR that the Xen HVM guest uses to initialize its hypercall
  464. page, and provides the starting address and size of the hypercall
  465. blobs in userspace. When the guest writes the MSR, kvm copies one
  466. page of a blob (32- or 64-bit, depending on the vcpu mode) to guest
  467. memory.
  468. struct kvm_xen_hvm_config {
  469. __u32 flags;
  470. __u32 msr;
  471. __u64 blob_addr_32;
  472. __u64 blob_addr_64;
  473. __u8 blob_size_32;
  474. __u8 blob_size_64;
  475. __u8 pad2[30];
  476. };
  477. 4.27 KVM_GET_CLOCK
  478. Capability: KVM_CAP_ADJUST_CLOCK
  479. Architectures: x86
  480. Type: vm ioctl
  481. Parameters: struct kvm_clock_data (out)
  482. Returns: 0 on success, -1 on error
  483. Gets the current timestamp of kvmclock as seen by the current guest. In
  484. conjunction with KVM_SET_CLOCK, it is used to ensure monotonicity on scenarios
  485. such as migration.
  486. struct kvm_clock_data {
  487. __u64 clock; /* kvmclock current value */
  488. __u32 flags;
  489. __u32 pad[9];
  490. };
  491. 4.28 KVM_SET_CLOCK
  492. Capability: KVM_CAP_ADJUST_CLOCK
  493. Architectures: x86
  494. Type: vm ioctl
  495. Parameters: struct kvm_clock_data (in)
  496. Returns: 0 on success, -1 on error
  497. Sets the current timestamp of kvmclock to the value specified in its parameter.
  498. In conjunction with KVM_GET_CLOCK, it is used to ensure monotonicity on scenarios
  499. such as migration.
  500. struct kvm_clock_data {
  501. __u64 clock; /* kvmclock current value */
  502. __u32 flags;
  503. __u32 pad[9];
  504. };
  505. 4.29 KVM_GET_VCPU_EVENTS
  506. Capability: KVM_CAP_VCPU_EVENTS
  507. Extended by: KVM_CAP_INTR_SHADOW
  508. Architectures: x86
  509. Type: vm ioctl
  510. Parameters: struct kvm_vcpu_event (out)
  511. Returns: 0 on success, -1 on error
  512. Gets currently pending exceptions, interrupts, and NMIs as well as related
  513. states of the vcpu.
  514. struct kvm_vcpu_events {
  515. struct {
  516. __u8 injected;
  517. __u8 nr;
  518. __u8 has_error_code;
  519. __u8 pad;
  520. __u32 error_code;
  521. } exception;
  522. struct {
  523. __u8 injected;
  524. __u8 nr;
  525. __u8 soft;
  526. __u8 shadow;
  527. } interrupt;
  528. struct {
  529. __u8 injected;
  530. __u8 pending;
  531. __u8 masked;
  532. __u8 pad;
  533. } nmi;
  534. __u32 sipi_vector;
  535. __u32 flags;
  536. };
  537. KVM_VCPUEVENT_VALID_SHADOW may be set in the flags field to signal that
  538. interrupt.shadow contains a valid state. Otherwise, this field is undefined.
  539. 4.30 KVM_SET_VCPU_EVENTS
  540. Capability: KVM_CAP_VCPU_EVENTS
  541. Extended by: KVM_CAP_INTR_SHADOW
  542. Architectures: x86
  543. Type: vm ioctl
  544. Parameters: struct kvm_vcpu_event (in)
  545. Returns: 0 on success, -1 on error
  546. Set pending exceptions, interrupts, and NMIs as well as related states of the
  547. vcpu.
  548. See KVM_GET_VCPU_EVENTS for the data structure.
  549. Fields that may be modified asynchronously by running VCPUs can be excluded
  550. from the update. These fields are nmi.pending and sipi_vector. Keep the
  551. corresponding bits in the flags field cleared to suppress overwriting the
  552. current in-kernel state. The bits are:
  553. KVM_VCPUEVENT_VALID_NMI_PENDING - transfer nmi.pending to the kernel
  554. KVM_VCPUEVENT_VALID_SIPI_VECTOR - transfer sipi_vector
  555. If KVM_CAP_INTR_SHADOW is available, KVM_VCPUEVENT_VALID_SHADOW can be set in
  556. the flags field to signal that interrupt.shadow contains a valid state and
  557. shall be written into the VCPU.
  558. 4.32 KVM_GET_DEBUGREGS
  559. Capability: KVM_CAP_DEBUGREGS
  560. Architectures: x86
  561. Type: vm ioctl
  562. Parameters: struct kvm_debugregs (out)
  563. Returns: 0 on success, -1 on error
  564. Reads debug registers from the vcpu.
  565. struct kvm_debugregs {
  566. __u64 db[4];
  567. __u64 dr6;
  568. __u64 dr7;
  569. __u64 flags;
  570. __u64 reserved[9];
  571. };
  572. 4.33 KVM_SET_DEBUGREGS
  573. Capability: KVM_CAP_DEBUGREGS
  574. Architectures: x86
  575. Type: vm ioctl
  576. Parameters: struct kvm_debugregs (in)
  577. Returns: 0 on success, -1 on error
  578. Writes debug registers into the vcpu.
  579. See KVM_GET_DEBUGREGS for the data structure. The flags field is unused
  580. yet and must be cleared on entry.
  581. 4.34 KVM_SET_USER_MEMORY_REGION
  582. Capability: KVM_CAP_USER_MEM
  583. Architectures: all
  584. Type: vm ioctl
  585. Parameters: struct kvm_userspace_memory_region (in)
  586. Returns: 0 on success, -1 on error
  587. struct kvm_userspace_memory_region {
  588. __u32 slot;
  589. __u32 flags;
  590. __u64 guest_phys_addr;
  591. __u64 memory_size; /* bytes */
  592. __u64 userspace_addr; /* start of the userspace allocated memory */
  593. };
  594. /* for kvm_memory_region::flags */
  595. #define KVM_MEM_LOG_DIRTY_PAGES 1UL
  596. This ioctl allows the user to create or modify a guest physical memory
  597. slot. When changing an existing slot, it may be moved in the guest
  598. physical memory space, or its flags may be modified. It may not be
  599. resized. Slots may not overlap in guest physical address space.
  600. Memory for the region is taken starting at the address denoted by the
  601. field userspace_addr, which must point at user addressable memory for
  602. the entire memory slot size. Any object may back this memory, including
  603. anonymous memory, ordinary files, and hugetlbfs.
  604. It is recommended that the lower 21 bits of guest_phys_addr and userspace_addr
  605. be identical. This allows large pages in the guest to be backed by large
  606. pages in the host.
  607. The flags field supports just one flag, KVM_MEM_LOG_DIRTY_PAGES, which
  608. instructs kvm to keep track of writes to memory within the slot. See
  609. the KVM_GET_DIRTY_LOG ioctl.
  610. When the KVM_CAP_SYNC_MMU capability, changes in the backing of the memory
  611. region are automatically reflected into the guest. For example, an mmap()
  612. that affects the region will be made visible immediately. Another example
  613. is madvise(MADV_DROP).
  614. It is recommended to use this API instead of the KVM_SET_MEMORY_REGION ioctl.
  615. The KVM_SET_MEMORY_REGION does not allow fine grained control over memory
  616. allocation and is deprecated.
  617. 4.35 KVM_SET_TSS_ADDR
  618. Capability: KVM_CAP_SET_TSS_ADDR
  619. Architectures: x86
  620. Type: vm ioctl
  621. Parameters: unsigned long tss_address (in)
  622. Returns: 0 on success, -1 on error
  623. This ioctl defines the physical address of a three-page region in the guest
  624. physical address space. The region must be within the first 4GB of the
  625. guest physical address space and must not conflict with any memory slot
  626. or any mmio address. The guest may malfunction if it accesses this memory
  627. region.
  628. This ioctl is required on Intel-based hosts. This is needed on Intel hardware
  629. because of a quirk in the virtualization implementation (see the internals
  630. documentation when it pops into existence).
  631. 4.36 KVM_ENABLE_CAP
  632. Capability: KVM_CAP_ENABLE_CAP
  633. Architectures: ppc
  634. Type: vcpu ioctl
  635. Parameters: struct kvm_enable_cap (in)
  636. Returns: 0 on success; -1 on error
  637. +Not all extensions are enabled by default. Using this ioctl the application
  638. can enable an extension, making it available to the guest.
  639. On systems that do not support this ioctl, it always fails. On systems that
  640. do support it, it only works for extensions that are supported for enablement.
  641. To check if a capability can be enabled, the KVM_CHECK_EXTENSION ioctl should
  642. be used.
  643. struct kvm_enable_cap {
  644. /* in */
  645. __u32 cap;
  646. The capability that is supposed to get enabled.
  647. __u32 flags;
  648. A bitfield indicating future enhancements. Has to be 0 for now.
  649. __u64 args[4];
  650. Arguments for enabling a feature. If a feature needs initial values to
  651. function properly, this is the place to put them.
  652. __u8 pad[64];
  653. };
  654. 4.37 KVM_GET_MP_STATE
  655. Capability: KVM_CAP_MP_STATE
  656. Architectures: x86, ia64
  657. Type: vcpu ioctl
  658. Parameters: struct kvm_mp_state (out)
  659. Returns: 0 on success; -1 on error
  660. struct kvm_mp_state {
  661. __u32 mp_state;
  662. };
  663. Returns the vcpu's current "multiprocessing state" (though also valid on
  664. uniprocessor guests).
  665. Possible values are:
  666. - KVM_MP_STATE_RUNNABLE: the vcpu is currently running
  667. - KVM_MP_STATE_UNINITIALIZED: the vcpu is an application processor (AP)
  668. which has not yet received an INIT signal
  669. - KVM_MP_STATE_INIT_RECEIVED: the vcpu has received an INIT signal, and is
  670. now ready for a SIPI
  671. - KVM_MP_STATE_HALTED: the vcpu has executed a HLT instruction and
  672. is waiting for an interrupt
  673. - KVM_MP_STATE_SIPI_RECEIVED: the vcpu has just received a SIPI (vector
  674. accesible via KVM_GET_VCPU_EVENTS)
  675. This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
  676. irqchip, the multiprocessing state must be maintained by userspace.
  677. 4.38 KVM_SET_MP_STATE
  678. Capability: KVM_CAP_MP_STATE
  679. Architectures: x86, ia64
  680. Type: vcpu ioctl
  681. Parameters: struct kvm_mp_state (in)
  682. Returns: 0 on success; -1 on error
  683. Sets the vcpu's current "multiprocessing state"; see KVM_GET_MP_STATE for
  684. arguments.
  685. This ioctl is only useful after KVM_CREATE_IRQCHIP. Without an in-kernel
  686. irqchip, the multiprocessing state must be maintained by userspace.
  687. 4.39 KVM_SET_IDENTITY_MAP_ADDR
  688. Capability: KVM_CAP_SET_IDENTITY_MAP_ADDR
  689. Architectures: x86
  690. Type: vm ioctl
  691. Parameters: unsigned long identity (in)
  692. Returns: 0 on success, -1 on error
  693. This ioctl defines the physical address of a one-page region in the guest
  694. physical address space. The region must be within the first 4GB of the
  695. guest physical address space and must not conflict with any memory slot
  696. or any mmio address. The guest may malfunction if it accesses this memory
  697. region.
  698. This ioctl is required on Intel-based hosts. This is needed on Intel hardware
  699. because of a quirk in the virtualization implementation (see the internals
  700. documentation when it pops into existence).
  701. 4.40 KVM_SET_BOOT_CPU_ID
  702. Capability: KVM_CAP_SET_BOOT_CPU_ID
  703. Architectures: x86, ia64
  704. Type: vm ioctl
  705. Parameters: unsigned long vcpu_id
  706. Returns: 0 on success, -1 on error
  707. Define which vcpu is the Bootstrap Processor (BSP). Values are the same
  708. as the vcpu id in KVM_CREATE_VCPU. If this ioctl is not called, the default
  709. is vcpu 0.
  710. 4.41 KVM_GET_XSAVE
  711. Capability: KVM_CAP_XSAVE
  712. Architectures: x86
  713. Type: vcpu ioctl
  714. Parameters: struct kvm_xsave (out)
  715. Returns: 0 on success, -1 on error
  716. struct kvm_xsave {
  717. __u32 region[1024];
  718. };
  719. This ioctl would copy current vcpu's xsave struct to the userspace.
  720. 4.42 KVM_SET_XSAVE
  721. Capability: KVM_CAP_XSAVE
  722. Architectures: x86
  723. Type: vcpu ioctl
  724. Parameters: struct kvm_xsave (in)
  725. Returns: 0 on success, -1 on error
  726. struct kvm_xsave {
  727. __u32 region[1024];
  728. };
  729. This ioctl would copy userspace's xsave struct to the kernel.
  730. 4.43 KVM_GET_XCRS
  731. Capability: KVM_CAP_XCRS
  732. Architectures: x86
  733. Type: vcpu ioctl
  734. Parameters: struct kvm_xcrs (out)
  735. Returns: 0 on success, -1 on error
  736. struct kvm_xcr {
  737. __u32 xcr;
  738. __u32 reserved;
  739. __u64 value;
  740. };
  741. struct kvm_xcrs {
  742. __u32 nr_xcrs;
  743. __u32 flags;
  744. struct kvm_xcr xcrs[KVM_MAX_XCRS];
  745. __u64 padding[16];
  746. };
  747. This ioctl would copy current vcpu's xcrs to the userspace.
  748. 4.44 KVM_SET_XCRS
  749. Capability: KVM_CAP_XCRS
  750. Architectures: x86
  751. Type: vcpu ioctl
  752. Parameters: struct kvm_xcrs (in)
  753. Returns: 0 on success, -1 on error
  754. struct kvm_xcr {
  755. __u32 xcr;
  756. __u32 reserved;
  757. __u64 value;
  758. };
  759. struct kvm_xcrs {
  760. __u32 nr_xcrs;
  761. __u32 flags;
  762. struct kvm_xcr xcrs[KVM_MAX_XCRS];
  763. __u64 padding[16];
  764. };
  765. This ioctl would set vcpu's xcr to the value userspace specified.
  766. 4.45 KVM_GET_SUPPORTED_CPUID
  767. Capability: KVM_CAP_EXT_CPUID
  768. Architectures: x86
  769. Type: system ioctl
  770. Parameters: struct kvm_cpuid2 (in/out)
  771. Returns: 0 on success, -1 on error
  772. struct kvm_cpuid2 {
  773. __u32 nent;
  774. __u32 padding;
  775. struct kvm_cpuid_entry2 entries[0];
  776. };
  777. #define KVM_CPUID_FLAG_SIGNIFCANT_INDEX 1
  778. #define KVM_CPUID_FLAG_STATEFUL_FUNC 2
  779. #define KVM_CPUID_FLAG_STATE_READ_NEXT 4
  780. struct kvm_cpuid_entry2 {
  781. __u32 function;
  782. __u32 index;
  783. __u32 flags;
  784. __u32 eax;
  785. __u32 ebx;
  786. __u32 ecx;
  787. __u32 edx;
  788. __u32 padding[3];
  789. };
  790. This ioctl returns x86 cpuid features which are supported by both the hardware
  791. and kvm. Userspace can use the information returned by this ioctl to
  792. construct cpuid information (for KVM_SET_CPUID2) that is consistent with
  793. hardware, kernel, and userspace capabilities, and with user requirements (for
  794. example, the user may wish to constrain cpuid to emulate older hardware,
  795. or for feature consistency across a cluster).
  796. Userspace invokes KVM_GET_SUPPORTED_CPUID by passing a kvm_cpuid2 structure
  797. with the 'nent' field indicating the number of entries in the variable-size
  798. array 'entries'. If the number of entries is too low to describe the cpu
  799. capabilities, an error (E2BIG) is returned. If the number is too high,
  800. the 'nent' field is adjusted and an error (ENOMEM) is returned. If the
  801. number is just right, the 'nent' field is adjusted to the number of valid
  802. entries in the 'entries' array, which is then filled.
  803. The entries returned are the host cpuid as returned by the cpuid instruction,
  804. with unknown or unsupported features masked out. Some features (for example,
  805. x2apic), may not be present in the host cpu, but are exposed by kvm if it can
  806. emulate them efficiently. The fields in each entry are defined as follows:
  807. function: the eax value used to obtain the entry
  808. index: the ecx value used to obtain the entry (for entries that are
  809. affected by ecx)
  810. flags: an OR of zero or more of the following:
  811. KVM_CPUID_FLAG_SIGNIFCANT_INDEX:
  812. if the index field is valid
  813. KVM_CPUID_FLAG_STATEFUL_FUNC:
  814. if cpuid for this function returns different values for successive
  815. invocations; there will be several entries with the same function,
  816. all with this flag set
  817. KVM_CPUID_FLAG_STATE_READ_NEXT:
  818. for KVM_CPUID_FLAG_STATEFUL_FUNC entries, set if this entry is
  819. the first entry to be read by a cpu
  820. eax, ebx, ecx, edx: the values returned by the cpuid instruction for
  821. this function/index combination
  822. 4.46 KVM_PPC_GET_PVINFO
  823. Capability: KVM_CAP_PPC_GET_PVINFO
  824. Architectures: ppc
  825. Type: vm ioctl
  826. Parameters: struct kvm_ppc_pvinfo (out)
  827. Returns: 0 on success, !0 on error
  828. struct kvm_ppc_pvinfo {
  829. __u32 flags;
  830. __u32 hcall[4];
  831. __u8 pad[108];
  832. };
  833. This ioctl fetches PV specific information that need to be passed to the guest
  834. using the device tree or other means from vm context.
  835. For now the only implemented piece of information distributed here is an array
  836. of 4 instructions that make up a hypercall.
  837. If any additional field gets added to this structure later on, a bit for that
  838. additional piece of information will be set in the flags bitmap.
  839. 5. The kvm_run structure
  840. Application code obtains a pointer to the kvm_run structure by
  841. mmap()ing a vcpu fd. From that point, application code can control
  842. execution by changing fields in kvm_run prior to calling the KVM_RUN
  843. ioctl, and obtain information about the reason KVM_RUN returned by
  844. looking up structure members.
  845. struct kvm_run {
  846. /* in */
  847. __u8 request_interrupt_window;
  848. Request that KVM_RUN return when it becomes possible to inject external
  849. interrupts into the guest. Useful in conjunction with KVM_INTERRUPT.
  850. __u8 padding1[7];
  851. /* out */
  852. __u32 exit_reason;
  853. When KVM_RUN has returned successfully (return value 0), this informs
  854. application code why KVM_RUN has returned. Allowable values for this
  855. field are detailed below.
  856. __u8 ready_for_interrupt_injection;
  857. If request_interrupt_window has been specified, this field indicates
  858. an interrupt can be injected now with KVM_INTERRUPT.
  859. __u8 if_flag;
  860. The value of the current interrupt flag. Only valid if in-kernel
  861. local APIC is not used.
  862. __u8 padding2[2];
  863. /* in (pre_kvm_run), out (post_kvm_run) */
  864. __u64 cr8;
  865. The value of the cr8 register. Only valid if in-kernel local APIC is
  866. not used. Both input and output.
  867. __u64 apic_base;
  868. The value of the APIC BASE msr. Only valid if in-kernel local
  869. APIC is not used. Both input and output.
  870. union {
  871. /* KVM_EXIT_UNKNOWN */
  872. struct {
  873. __u64 hardware_exit_reason;
  874. } hw;
  875. If exit_reason is KVM_EXIT_UNKNOWN, the vcpu has exited due to unknown
  876. reasons. Further architecture-specific information is available in
  877. hardware_exit_reason.
  878. /* KVM_EXIT_FAIL_ENTRY */
  879. struct {
  880. __u64 hardware_entry_failure_reason;
  881. } fail_entry;
  882. If exit_reason is KVM_EXIT_FAIL_ENTRY, the vcpu could not be run due
  883. to unknown reasons. Further architecture-specific information is
  884. available in hardware_entry_failure_reason.
  885. /* KVM_EXIT_EXCEPTION */
  886. struct {
  887. __u32 exception;
  888. __u32 error_code;
  889. } ex;
  890. Unused.
  891. /* KVM_EXIT_IO */
  892. struct {
  893. #define KVM_EXIT_IO_IN 0
  894. #define KVM_EXIT_IO_OUT 1
  895. __u8 direction;
  896. __u8 size; /* bytes */
  897. __u16 port;
  898. __u32 count;
  899. __u64 data_offset; /* relative to kvm_run start */
  900. } io;
  901. If exit_reason is KVM_EXIT_IO, then the vcpu has
  902. executed a port I/O instruction which could not be satisfied by kvm.
  903. data_offset describes where the data is located (KVM_EXIT_IO_OUT) or
  904. where kvm expects application code to place the data for the next
  905. KVM_RUN invocation (KVM_EXIT_IO_IN). Data format is a packed array.
  906. struct {
  907. struct kvm_debug_exit_arch arch;
  908. } debug;
  909. Unused.
  910. /* KVM_EXIT_MMIO */
  911. struct {
  912. __u64 phys_addr;
  913. __u8 data[8];
  914. __u32 len;
  915. __u8 is_write;
  916. } mmio;
  917. If exit_reason is KVM_EXIT_MMIO, then the vcpu has
  918. executed a memory-mapped I/O instruction which could not be satisfied
  919. by kvm. The 'data' member contains the written data if 'is_write' is
  920. true, and should be filled by application code otherwise.
  921. NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO and KVM_EXIT_OSI, the corresponding
  922. operations are complete (and guest state is consistent) only after userspace
  923. has re-entered the kernel with KVM_RUN. The kernel side will first finish
  924. incomplete operations and then check for pending signals. Userspace
  925. can re-enter the guest with an unmasked signal pending to complete
  926. pending operations.
  927. /* KVM_EXIT_HYPERCALL */
  928. struct {
  929. __u64 nr;
  930. __u64 args[6];
  931. __u64 ret;
  932. __u32 longmode;
  933. __u32 pad;
  934. } hypercall;
  935. Unused. This was once used for 'hypercall to userspace'. To implement
  936. such functionality, use KVM_EXIT_IO (x86) or KVM_EXIT_MMIO (all except s390).
  937. Note KVM_EXIT_IO is significantly faster than KVM_EXIT_MMIO.
  938. /* KVM_EXIT_TPR_ACCESS */
  939. struct {
  940. __u64 rip;
  941. __u32 is_write;
  942. __u32 pad;
  943. } tpr_access;
  944. To be documented (KVM_TPR_ACCESS_REPORTING).
  945. /* KVM_EXIT_S390_SIEIC */
  946. struct {
  947. __u8 icptcode;
  948. __u64 mask; /* psw upper half */
  949. __u64 addr; /* psw lower half */
  950. __u16 ipa;
  951. __u32 ipb;
  952. } s390_sieic;
  953. s390 specific.
  954. /* KVM_EXIT_S390_RESET */
  955. #define KVM_S390_RESET_POR 1
  956. #define KVM_S390_RESET_CLEAR 2
  957. #define KVM_S390_RESET_SUBSYSTEM 4
  958. #define KVM_S390_RESET_CPU_INIT 8
  959. #define KVM_S390_RESET_IPL 16
  960. __u64 s390_reset_flags;
  961. s390 specific.
  962. /* KVM_EXIT_DCR */
  963. struct {
  964. __u32 dcrn;
  965. __u32 data;
  966. __u8 is_write;
  967. } dcr;
  968. powerpc specific.
  969. /* KVM_EXIT_OSI */
  970. struct {
  971. __u64 gprs[32];
  972. } osi;
  973. MOL uses a special hypercall interface it calls 'OSI'. To enable it, we catch
  974. hypercalls and exit with this exit struct that contains all the guest gprs.
  975. If exit_reason is KVM_EXIT_OSI, then the vcpu has triggered such a hypercall.
  976. Userspace can now handle the hypercall and when it's done modify the gprs as
  977. necessary. Upon guest entry all guest GPRs will then be replaced by the values
  978. in this struct.
  979. /* Fix the size of the union. */
  980. char padding[256];
  981. };
  982. };