blob: 17c5a038f42d3978d1b06d7cec5f8c8afd92eaaf [file] [log] [blame]
Greg Kroah-Hartman6f52b162017-11-01 15:08:43 +01001/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_KVM_H
3#define _ASM_X86_KVM_H
Jerone Youngf6a40e32007-11-19 17:06:31 -06004
5/*
6 * KVM x86 specific structures and definitions
7 *
8 */
9
Jaswinder Singh Rajputcef37672009-01-30 22:46:08 +053010#include <linux/types.h>
Jerone Youngf6a40e32007-11-19 17:06:31 -060011#include <linux/ioctl.h>
12
Paolo Bonzini4b4357e2017-03-31 13:53:23 +020013#define KVM_PIO_PAGE_OFFSET 1
14#define KVM_COALESCED_MMIO_PAGE_OFFSET 2
15
Xiao Guangrong26bf2642012-09-17 16:31:13 +080016#define DE_VECTOR 0
17#define DB_VECTOR 1
18#define BP_VECTOR 3
19#define OF_VECTOR 4
20#define BR_VECTOR 5
21#define UD_VECTOR 6
22#define NM_VECTOR 7
23#define DF_VECTOR 8
24#define TS_VECTOR 10
25#define NP_VECTOR 11
26#define SS_VECTOR 12
27#define GP_VECTOR 13
28#define PF_VECTOR 14
29#define MF_VECTOR 16
Nadav Amitc9cdd082014-07-21 14:37:24 +030030#define AC_VECTOR 17
Xiao Guangrong26bf2642012-09-17 16:31:13 +080031#define MC_VECTOR 18
Nadav Amitc9cdd082014-07-21 14:37:24 +030032#define XM_VECTOR 19
33#define VE_VECTOR 20
Xiao Guangrong26bf2642012-09-17 16:31:13 +080034
Avi Kivity7a0eb192009-01-19 14:57:52 +020035/* Select x86 specific features in <linux/kvm.h> */
36#define __KVM_HAVE_PIT
37#define __KVM_HAVE_IOAPIC
Christoffer Dalla1e4ccb2012-06-15 15:07:13 -040038#define __KVM_HAVE_IRQ_LINE
Avi Kivity7a0eb192009-01-19 14:57:52 +020039#define __KVM_HAVE_MSI
40#define __KVM_HAVE_USER_NMI
Avi Kivity91b2ae72009-01-19 14:57:52 +020041#define __KVM_HAVE_GUEST_DEBUG
Sheng Yangd510d6c2009-02-25 17:22:28 +080042#define __KVM_HAVE_MSIX
Huang Ying890ca9a2009-05-11 16:48:15 +080043#define __KVM_HAVE_MCE
Beth Kone9f42752009-07-07 11:50:38 -040044#define __KVM_HAVE_PIT_STATE2
Ed Swierkffde22a2009-10-15 15:21:43 -070045#define __KVM_HAVE_XEN_HVM
Jan Kiszka3cfc3092009-11-12 01:04:25 +010046#define __KVM_HAVE_VCPU_EVENTS
Jan Kiszkaa1efbe72010-02-15 10:45:43 +010047#define __KVM_HAVE_DEBUGREGS
Sheng Yang2d5b5a62010-06-13 17:29:39 +080048#define __KVM_HAVE_XSAVE
49#define __KVM_HAVE_XCRS
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +080050#define __KVM_HAVE_READONLY_MEM
Avi Kivity7a0eb192009-01-19 14:57:52 +020051
Jerone Young244d57e2007-11-19 17:06:36 -060052/* Architectural interrupt line count. */
53#define KVM_NR_INTERRUPTS 256
54
Jerone Youngf6a40e32007-11-19 17:06:31 -060055struct kvm_memory_alias {
56 __u32 slot; /* this has a different namespace than memory slots */
57 __u32 flags;
58 __u64 guest_phys_addr;
59 __u64 memory_size;
60 __u64 target_phys_addr;
61};
62
Jerone Youngda1386a2007-11-19 17:06:32 -060063/* for KVM_GET_IRQCHIP and KVM_SET_IRQCHIP */
64struct kvm_pic_state {
65 __u8 last_irr; /* edge detection */
66 __u8 irr; /* interrupt request register */
67 __u8 imr; /* interrupt mask register */
68 __u8 isr; /* interrupt service register */
69 __u8 priority_add; /* highest irq priority */
70 __u8 irq_base;
71 __u8 read_reg_select;
72 __u8 poll;
73 __u8 special_mask;
74 __u8 init_state;
75 __u8 auto_eoi;
76 __u8 rotate_on_auto_eoi;
77 __u8 special_fully_nested_mode;
78 __u8 init4; /* true if 4 byte init */
79 __u8 elcr; /* PIIX edge/trigger selection */
80 __u8 elcr_mask;
81};
82
83#define KVM_IOAPIC_NUM_PINS 24
84struct kvm_ioapic_state {
85 __u64 base_address;
86 __u32 ioregsel;
87 __u32 id;
88 __u32 irr;
89 __u32 pad;
90 union {
91 __u64 bits;
92 struct {
93 __u8 vector;
94 __u8 delivery_mode:3;
95 __u8 dest_mode:1;
96 __u8 delivery_status:1;
97 __u8 polarity:1;
98 __u8 remote_irr:1;
99 __u8 trig_mode:1;
100 __u8 mask:1;
101 __u8 reserve:7;
102 __u8 reserved[4];
103 __u8 dest_id;
104 } fields;
105 } redirtbl[KVM_IOAPIC_NUM_PINS];
106};
107
108#define KVM_IRQCHIP_PIC_MASTER 0
109#define KVM_IRQCHIP_PIC_SLAVE 1
110#define KVM_IRQCHIP_IOAPIC 2
Gleb Natapov3e71f882009-08-24 11:54:21 +0300111#define KVM_NR_IRQCHIPS 3
Jerone Youngda1386a2007-11-19 17:06:32 -0600112
Paolo Bonzinif0778252015-04-01 15:06:40 +0200113#define KVM_RUN_X86_SMM (1 << 0)
114
Jerone Young19d30b12007-11-19 17:06:33 -0600115/* for KVM_GET_REGS and KVM_SET_REGS */
116struct kvm_regs {
117 /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
118 __u64 rax, rbx, rcx, rdx;
119 __u64 rsi, rdi, rsp, rbp;
120 __u64 r8, r9, r10, r11;
121 __u64 r12, r13, r14, r15;
122 __u64 rip, rflags;
123};
124
Jerone Youngd9ecf922007-11-19 17:06:34 -0600125/* for KVM_GET_LAPIC and KVM_SET_LAPIC */
126#define KVM_APIC_REG_SIZE 0x400
127struct kvm_lapic_state {
128 char regs[KVM_APIC_REG_SIZE];
129};
130
Jerone Young3a56b202007-11-19 17:06:35 -0600131struct kvm_segment {
132 __u64 base;
133 __u32 limit;
134 __u16 selector;
135 __u8 type;
136 __u8 present, dpl, db, s, l, g, avl;
137 __u8 unusable;
138 __u8 padding;
139};
140
141struct kvm_dtable {
142 __u64 base;
143 __u16 limit;
144 __u16 padding[3];
145};
146
147
Jerone Young244d57e2007-11-19 17:06:36 -0600148/* for KVM_GET_SREGS and KVM_SET_SREGS */
149struct kvm_sregs {
150 /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */
151 struct kvm_segment cs, ds, es, fs, gs, ss;
152 struct kvm_segment tr, ldt;
153 struct kvm_dtable gdt, idt;
154 __u64 cr0, cr2, cr3, cr4, cr8;
155 __u64 efer;
156 __u64 apic_base;
157 __u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
158};
159
Christian Ehrhardt6f723c72008-01-08 08:04:50 +0100160/* for KVM_GET_FPU and KVM_SET_FPU */
161struct kvm_fpu {
162 __u8 fpr[8][16];
163 __u16 fcw;
164 __u16 fsw;
165 __u8 ftwx; /* in fxsave format */
166 __u8 pad1;
167 __u16 last_opcode;
168 __u64 last_ip;
169 __u64 last_dp;
170 __u8 xmm[16][16];
171 __u32 mxcsr;
172 __u32 pad2;
173};
174
Jerone Young244d57e2007-11-19 17:06:36 -0600175struct kvm_msr_entry {
176 __u32 index;
177 __u32 reserved;
178 __u64 data;
179};
180
181/* for KVM_GET_MSRS and KVM_SET_MSRS */
182struct kvm_msrs {
183 __u32 nmsrs; /* number of msrs in entries */
184 __u32 pad;
185
186 struct kvm_msr_entry entries[0];
187};
188
189/* for KVM_GET_MSR_INDEX_LIST */
190struct kvm_msr_list {
191 __u32 nmsrs; /* number of msrs in entries */
192 __u32 indices[0];
193};
194
195
Jerone Younga162dd52007-11-19 17:06:37 -0600196struct kvm_cpuid_entry {
197 __u32 function;
198 __u32 eax;
199 __u32 ebx;
200 __u32 ecx;
201 __u32 edx;
202 __u32 padding;
203};
204
205/* for KVM_SET_CPUID */
206struct kvm_cpuid {
207 __u32 nent;
208 __u32 padding;
209 struct kvm_cpuid_entry entries[0];
210};
211
Dan Kenigsberg07716712007-11-21 17:10:04 +0200212struct kvm_cpuid_entry2 {
213 __u32 function;
214 __u32 index;
215 __u32 flags;
216 __u32 eax;
217 __u32 ebx;
218 __u32 ecx;
219 __u32 edx;
220 __u32 padding[3];
221};
222
Borislav Petkov3dbe3452016-05-05 10:18:23 +0200223#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX (1 << 0)
224#define KVM_CPUID_FLAG_STATEFUL_FUNC (1 << 1)
225#define KVM_CPUID_FLAG_STATE_READ_NEXT (1 << 2)
Dan Kenigsberg07716712007-11-21 17:10:04 +0200226
227/* for KVM_SET_CPUID2 */
228struct kvm_cpuid2 {
229 __u32 nent;
230 __u32 padding;
231 struct kvm_cpuid_entry2 entries[0];
232};
Jerone Younga162dd52007-11-19 17:06:37 -0600233
Sheng Yange0f63cb2008-03-04 00:50:59 +0800234/* for KVM_GET_PIT and KVM_SET_PIT */
235struct kvm_pit_channel_state {
236 __u32 count; /* can be 65536 */
237 __u16 latched_count;
238 __u8 count_latched;
239 __u8 status_latched;
240 __u8 status;
241 __u8 read_state;
242 __u8 write_state;
243 __u8 write_latch;
244 __u8 rw_mode;
245 __u8 mode;
246 __u8 bcd;
247 __u8 gate;
248 __s64 count_load_time;
249};
250
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100251struct kvm_debug_exit_arch {
252 __u32 exception;
253 __u32 pad;
254 __u64 pc;
255 __u64 dr6;
256 __u64 dr7;
257};
258
259#define KVM_GUESTDBG_USE_SW_BP 0x00010000
260#define KVM_GUESTDBG_USE_HW_BP 0x00020000
261#define KVM_GUESTDBG_INJECT_DB 0x00040000
262#define KVM_GUESTDBG_INJECT_BP 0x00080000
263
264/* for KVM_SET_GUEST_DEBUG */
265struct kvm_guest_debug_arch {
266 __u64 debugreg[8];
267};
268
Sheng Yange0f63cb2008-03-04 00:50:59 +0800269struct kvm_pit_state {
270 struct kvm_pit_channel_state channels[3];
271};
Marcelo Tosatti52d939a2008-12-30 15:55:06 -0200272
Beth Kone9f42752009-07-07 11:50:38 -0400273#define KVM_PIT_FLAGS_HPET_LEGACY 0x00000001
274
275struct kvm_pit_state2 {
276 struct kvm_pit_channel_state channels[3];
277 __u32 flags;
278 __u32 reserved[9];
279};
280
Marcelo Tosatti52d939a2008-12-30 15:55:06 -0200281struct kvm_reinject_control {
282 __u8 pit_reinject;
283 __u8 reserved[31];
284};
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100285
Jan Kiszkadab4b912009-12-06 18:24:15 +0100286/* When set in flags, include corresponding fields on KVM_SET_VCPU_EVENTS */
287#define KVM_VCPUEVENT_VALID_NMI_PENDING 0x00000001
288#define KVM_VCPUEVENT_VALID_SIPI_VECTOR 0x00000002
Jan Kiszka48005f62010-02-19 19:38:07 +0100289#define KVM_VCPUEVENT_VALID_SHADOW 0x00000004
Paolo Bonzinif0778252015-04-01 15:06:40 +0200290#define KVM_VCPUEVENT_VALID_SMM 0x00000008
Jim Mattson59073aa2018-10-16 14:29:20 -0700291#define KVM_VCPUEVENT_VALID_PAYLOAD 0x00000010
Jan Kiszka48005f62010-02-19 19:38:07 +0100292
293/* Interrupt shadow states */
294#define KVM_X86_SHADOW_INT_MOV_SS 0x01
295#define KVM_X86_SHADOW_INT_STI 0x02
Jan Kiszkadab4b912009-12-06 18:24:15 +0100296
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100297/* for KVM_GET/SET_VCPU_EVENTS */
298struct kvm_vcpu_events {
299 struct {
300 __u8 injected;
301 __u8 nr;
302 __u8 has_error_code;
Jim Mattson59073aa2018-10-16 14:29:20 -0700303 __u8 pending;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100304 __u32 error_code;
305 } exception;
306 struct {
307 __u8 injected;
308 __u8 nr;
309 __u8 soft;
Jan Kiszka48005f62010-02-19 19:38:07 +0100310 __u8 shadow;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100311 } interrupt;
312 struct {
313 __u8 injected;
314 __u8 pending;
315 __u8 masked;
316 __u8 pad;
317 } nmi;
318 __u32 sipi_vector;
319 __u32 flags;
Paolo Bonzinif0778252015-04-01 15:06:40 +0200320 struct {
321 __u8 smm;
322 __u8 pending;
323 __u8 smm_inside_nmi;
324 __u8 latched_init;
325 } smi;
Jim Mattson59073aa2018-10-16 14:29:20 -0700326 __u8 reserved[27];
327 __u8 exception_has_payload;
328 __u64 exception_payload;
Jan Kiszka3cfc3092009-11-12 01:04:25 +0100329};
330
Jan Kiszkaa1efbe72010-02-15 10:45:43 +0100331/* for KVM_GET/SET_DEBUGREGS */
332struct kvm_debugregs {
333 __u64 db[4];
334 __u64 dr6;
335 __u64 dr7;
336 __u64 flags;
337 __u64 reserved[9];
338};
339
Sheng Yang2d5b5a62010-06-13 17:29:39 +0800340/* for KVM_CAP_XSAVE */
341struct kvm_xsave {
342 __u32 region[1024];
343};
344
345#define KVM_MAX_XCRS 16
346
347struct kvm_xcr {
348 __u32 xcr;
349 __u32 reserved;
350 __u64 value;
351};
352
353struct kvm_xcrs {
354 __u32 nr_xcrs;
355 __u32 flags;
356 struct kvm_xcr xcrs[KVM_MAX_XCRS];
357 __u64 padding[16];
358};
359
Ken Hofsass01643c52018-01-31 16:03:36 -0800360#define KVM_SYNC_X86_REGS (1UL << 0)
361#define KVM_SYNC_X86_SREGS (1UL << 1)
362#define KVM_SYNC_X86_EVENTS (1UL << 2)
363
364#define KVM_SYNC_X86_VALID_FIELDS \
365 (KVM_SYNC_X86_REGS| \
366 KVM_SYNC_X86_SREGS| \
367 KVM_SYNC_X86_EVENTS)
368
369/* kvm_sync_regs struct included by kvm_run struct */
Christian Borntraegerb9e5dc82012-01-11 11:20:30 +0100370struct kvm_sync_regs {
Ken Hofsass01643c52018-01-31 16:03:36 -0800371 /* Members of this structure are potentially malicious.
372 * Care must be taken by code reading, esp. interpreting,
373 * data fields from them inside KVM to prevent TOCTOU and
374 * double-fetch types of vulnerabilities.
375 */
376 struct kvm_regs regs;
377 struct kvm_sregs sregs;
378 struct kvm_vcpu_events events;
Christian Borntraegerb9e5dc82012-01-11 11:20:30 +0100379};
380
Wanpeng Li511a85562019-05-21 14:06:54 +0800381#define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0)
382#define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1)
383#define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2)
384#define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3)
385#define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4)
Nadav Amit90de4a12015-04-13 01:53:41 +0300386
Liran Alon6ca00df2019-06-16 15:03:10 +0300387#define KVM_STATE_NESTED_FORMAT_VMX 0
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400388#define KVM_STATE_NESTED_FORMAT_SVM 1
Liran Alon6ca00df2019-06-16 15:03:10 +0300389
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200390#define KVM_STATE_NESTED_GUEST_MODE 0x00000001
391#define KVM_STATE_NESTED_RUN_PENDING 0x00000002
Vitaly Kuznetsov8cab6502018-10-16 18:50:09 +0200392#define KVM_STATE_NESTED_EVMCS 0x00000004
Oliver Upton5ef8acb2020-02-07 02:36:07 -0800393#define KVM_STATE_NESTED_MTF_PENDING 0x00000008
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400394#define KVM_STATE_NESTED_GIF_SET 0x00000100
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200395
396#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
397#define KVM_STATE_NESTED_SMM_VMXON 0x00000002
398
Liran Alon6ca00df2019-06-16 15:03:10 +0300399#define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000
400
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400401#define KVM_STATE_NESTED_SVM_VMCB_SIZE 0x1000
402
Peter Shier850448f2020-05-26 14:51:06 -0700403#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400404
Liran Alon6ca00df2019-06-16 15:03:10 +0300405struct kvm_vmx_nested_state_data {
406 __u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
407 __u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
408};
409
410struct kvm_vmx_nested_state_hdr {
Peter Shier850448f2020-05-26 14:51:06 -0700411 __u32 flags;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200412 __u64 vmxon_pa;
Liran Alon6ca00df2019-06-16 15:03:10 +0300413 __u64 vmcs12_pa;
Peter Shier850448f2020-05-26 14:51:06 -0700414 __u64 preemption_timer_deadline;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200415
416 struct {
417 __u16 flags;
418 } smm;
419};
420
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400421struct kvm_svm_nested_state_data {
422 /* Save area only used if KVM_STATE_NESTED_RUN_PENDING. */
423 __u8 vmcb12[KVM_STATE_NESTED_SVM_VMCB_SIZE];
424};
425
426struct kvm_svm_nested_state_hdr {
427 __u64 vmcb_pa;
428};
429
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200430/* for KVM_CAP_NESTED_STATE */
431struct kvm_nested_state {
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200432 __u16 flags;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200433 __u16 format;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200434 __u32 size;
435
436 union {
Liran Alon6ca00df2019-06-16 15:03:10 +0300437 struct kvm_vmx_nested_state_hdr vmx;
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400438 struct kvm_svm_nested_state_hdr svm;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200439
440 /* Pad the header to 128 bytes. */
441 __u8 pad[120];
Liran Alon6ca00df2019-06-16 15:03:10 +0300442 } hdr;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200443
Liran Alon6ca00df2019-06-16 15:03:10 +0300444 /*
445 * Define data region as 0 bytes to preserve backwards-compatability
446 * to old definition of kvm_nested_state in order to avoid changing
447 * KVM_{GET,PUT}_NESTED_STATE ioctl values.
448 */
449 union {
450 struct kvm_vmx_nested_state_data vmx[0];
Paolo Bonzinicc440cd2020-05-13 13:36:32 -0400451 struct kvm_svm_nested_state_data svm[0];
Liran Alon6ca00df2019-06-16 15:03:10 +0300452 } data;
Jim Mattson8fcc4b52018-07-10 11:27:20 +0200453};
454
Eric Hankland66bb8a02019-07-10 18:25:15 -0700455/* for KVM_CAP_PMU_EVENT_FILTER */
456struct kvm_pmu_event_filter {
Eric Hankland30cd8602019-07-18 11:38:18 -0700457 __u32 action;
458 __u32 nevents;
459 __u32 fixed_counter_bitmap;
460 __u32 flags;
461 __u32 pad[4];
462 __u64 events[0];
Eric Hankland66bb8a02019-07-10 18:25:15 -0700463};
464
465#define KVM_PMU_EVENT_ALLOW 0
466#define KVM_PMU_EVENT_DENY 1
467
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700468#endif /* _ASM_X86_KVM_H */