kvm_x86.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. #ifndef _ASM_X86_KVM_H
  3. #define _ASM_X86_KVM_H
  4. /*
  5. * KVM x86 specific structures and definitions
  6. *
  7. */
  8. #include <linux/types.h>
  9. #include <linux/ioctl.h>
  10. #include <linux/stddef.h>
  11. #define KVM_PIO_PAGE_OFFSET 1
  12. #define KVM_COALESCED_MMIO_PAGE_OFFSET 2
  13. #define KVM_DIRTY_LOG_PAGE_OFFSET 64
  14. #define DE_VECTOR 0
  15. #define DB_VECTOR 1
  16. #define BP_VECTOR 3
  17. #define OF_VECTOR 4
  18. #define BR_VECTOR 5
  19. #define UD_VECTOR 6
  20. #define NM_VECTOR 7
  21. #define DF_VECTOR 8
  22. #define TS_VECTOR 10
  23. #define NP_VECTOR 11
  24. #define SS_VECTOR 12
  25. #define GP_VECTOR 13
  26. #define PF_VECTOR 14
  27. #define MF_VECTOR 16
  28. #define AC_VECTOR 17
  29. #define MC_VECTOR 18
  30. #define XM_VECTOR 19
  31. #define VE_VECTOR 20
  32. /* Select x86 specific features in <linux/kvm.h> */
  33. #define __KVM_HAVE_PIT
  34. #define __KVM_HAVE_IOAPIC
  35. #define __KVM_HAVE_IRQ_LINE
  36. #define __KVM_HAVE_MSI
  37. #define __KVM_HAVE_USER_NMI
  38. #define __KVM_HAVE_GUEST_DEBUG
  39. #define __KVM_HAVE_MSIX
  40. #define __KVM_HAVE_MCE
  41. #define __KVM_HAVE_PIT_STATE2
  42. #define __KVM_HAVE_XEN_HVM
  43. #define __KVM_HAVE_VCPU_EVENTS
  44. #define __KVM_HAVE_DEBUGREGS
  45. #define __KVM_HAVE_XSAVE
  46. #define __KVM_HAVE_XCRS
  47. #define __KVM_HAVE_READONLY_MEM
  48. /* Architectural interrupt line count. */
  49. #define KVM_NR_INTERRUPTS 256
  50. /* for KVM_GET_IRQCHIP and KVM_SET_IRQCHIP */
  51. struct kvm_pic_state {
  52. __u8 last_irr; /* edge detection */
  53. __u8 irr; /* interrupt request register */
  54. __u8 imr; /* interrupt mask register */
  55. __u8 isr; /* interrupt service register */
  56. __u8 priority_add; /* highest irq priority */
  57. __u8 irq_base;
  58. __u8 read_reg_select;
  59. __u8 poll;
  60. __u8 special_mask;
  61. __u8 init_state;
  62. __u8 auto_eoi;
  63. __u8 rotate_on_auto_eoi;
  64. __u8 special_fully_nested_mode;
  65. __u8 init4; /* true if 4 byte init */
  66. __u8 elcr; /* PIIX edge/trigger selection */
  67. __u8 elcr_mask;
  68. };
  69. #define KVM_IOAPIC_NUM_PINS 24
  70. struct kvm_ioapic_state {
  71. __u64 base_address;
  72. __u32 ioregsel;
  73. __u32 id;
  74. __u32 irr;
  75. __u32 pad;
  76. union {
  77. __u64 bits;
  78. struct {
  79. __u8 vector;
  80. __u8 delivery_mode:3;
  81. __u8 dest_mode:1;
  82. __u8 delivery_status:1;
  83. __u8 polarity:1;
  84. __u8 remote_irr:1;
  85. __u8 trig_mode:1;
  86. __u8 mask:1;
  87. __u8 reserve:7;
  88. __u8 reserved[4];
  89. __u8 dest_id;
  90. } fields;
  91. } redirtbl[KVM_IOAPIC_NUM_PINS];
  92. };
  93. #define KVM_IRQCHIP_PIC_MASTER 0
  94. #define KVM_IRQCHIP_PIC_SLAVE 1
  95. #define KVM_IRQCHIP_IOAPIC 2
  96. #define KVM_NR_IRQCHIPS 3
  97. #define KVM_RUN_X86_SMM (1 << 0)
  98. #define KVM_RUN_X86_BUS_LOCK (1 << 1)
  99. /* for KVM_GET_REGS and KVM_SET_REGS */
  100. struct kvm_regs {
  101. /* out (KVM_GET_REGS) / in (KVM_SET_REGS) */
  102. __u64 rax, rbx, rcx, rdx;
  103. __u64 rsi, rdi, rsp, rbp;
  104. __u64 r8, r9, r10, r11;
  105. __u64 r12, r13, r14, r15;
  106. __u64 rip, rflags;
  107. };
  108. /* for KVM_GET_LAPIC and KVM_SET_LAPIC */
  109. #define KVM_APIC_REG_SIZE 0x400
  110. struct kvm_lapic_state {
  111. char regs[KVM_APIC_REG_SIZE];
  112. };
  113. struct kvm_segment {
  114. __u64 base;
  115. __u32 limit;
  116. __u16 selector;
  117. __u8 type;
  118. __u8 present, dpl, db, s, l, g, avl;
  119. __u8 unusable;
  120. __u8 padding;
  121. };
  122. struct kvm_dtable {
  123. __u64 base;
  124. __u16 limit;
  125. __u16 padding[3];
  126. };
  127. /* for KVM_GET_SREGS and KVM_SET_SREGS */
  128. struct kvm_sregs {
  129. /* out (KVM_GET_SREGS) / in (KVM_SET_SREGS) */
  130. struct kvm_segment cs, ds, es, fs, gs, ss;
  131. struct kvm_segment tr, ldt;
  132. struct kvm_dtable gdt, idt;
  133. __u64 cr0, cr2, cr3, cr4, cr8;
  134. __u64 efer;
  135. __u64 apic_base;
  136. __u64 interrupt_bitmap[(KVM_NR_INTERRUPTS + 63) / 64];
  137. };
  138. struct kvm_sregs2 {
  139. /* out (KVM_GET_SREGS2) / in (KVM_SET_SREGS2) */
  140. struct kvm_segment cs, ds, es, fs, gs, ss;
  141. struct kvm_segment tr, ldt;
  142. struct kvm_dtable gdt, idt;
  143. __u64 cr0, cr2, cr3, cr4, cr8;
  144. __u64 efer;
  145. __u64 apic_base;
  146. __u64 flags;
  147. __u64 pdptrs[4];
  148. };
  149. #define KVM_SREGS2_FLAGS_PDPTRS_VALID 1
  150. /* for KVM_GET_FPU and KVM_SET_FPU */
  151. struct kvm_fpu {
  152. __u8 fpr[8][16];
  153. __u16 fcw;
  154. __u16 fsw;
  155. __u8 ftwx; /* in fxsave format */
  156. __u8 pad1;
  157. __u16 last_opcode;
  158. __u64 last_ip;
  159. __u64 last_dp;
  160. __u8 xmm[16][16];
  161. __u32 mxcsr;
  162. __u32 pad2;
  163. };
  164. struct kvm_msr_entry {
  165. __u32 index;
  166. __u32 reserved;
  167. __u64 data;
  168. };
  169. /* for KVM_GET_MSRS and KVM_SET_MSRS */
  170. struct kvm_msrs {
  171. __u32 nmsrs; /* number of msrs in entries */
  172. __u32 pad;
  173. struct kvm_msr_entry entries[];
  174. };
  175. /* for KVM_GET_MSR_INDEX_LIST */
  176. struct kvm_msr_list {
  177. __u32 nmsrs; /* number of msrs in entries */
  178. __u32 indices[];
  179. };
  180. /* Maximum size of any access bitmap in bytes */
  181. #define KVM_MSR_FILTER_MAX_BITMAP_SIZE 0x600
  182. /* for KVM_X86_SET_MSR_FILTER */
  183. struct kvm_msr_filter_range {
  184. #define KVM_MSR_FILTER_READ (1 << 0)
  185. #define KVM_MSR_FILTER_WRITE (1 << 1)
  186. #define KVM_MSR_FILTER_RANGE_VALID_MASK (KVM_MSR_FILTER_READ | \
  187. KVM_MSR_FILTER_WRITE)
  188. __u32 flags;
  189. __u32 nmsrs; /* number of msrs in bitmap */
  190. __u32 base; /* MSR index the bitmap starts at */
  191. __u8 *bitmap; /* a 1 bit allows the operations in flags, 0 denies */
  192. };
  193. #define KVM_MSR_FILTER_MAX_RANGES 16
  194. struct kvm_msr_filter {
  195. #define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)
  196. #define KVM_MSR_FILTER_DEFAULT_DENY (1 << 0)
  197. #define KVM_MSR_FILTER_VALID_MASK (KVM_MSR_FILTER_DEFAULT_DENY)
  198. __u32 flags;
  199. struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES];
  200. };
  201. struct kvm_cpuid_entry {
  202. __u32 function;
  203. __u32 eax;
  204. __u32 ebx;
  205. __u32 ecx;
  206. __u32 edx;
  207. __u32 padding;
  208. };
  209. /* for KVM_SET_CPUID */
  210. struct kvm_cpuid {
  211. __u32 nent;
  212. __u32 padding;
  213. struct kvm_cpuid_entry entries[];
  214. };
  215. struct kvm_cpuid_entry2 {
  216. __u32 function;
  217. __u32 index;
  218. __u32 flags;
  219. __u32 eax;
  220. __u32 ebx;
  221. __u32 ecx;
  222. __u32 edx;
  223. __u32 padding[3];
  224. };
  225. #define KVM_CPUID_FLAG_SIGNIFCANT_INDEX (1 << 0)
  226. #define KVM_CPUID_FLAG_STATEFUL_FUNC (1 << 1)
  227. #define KVM_CPUID_FLAG_STATE_READ_NEXT (1 << 2)
  228. /* for KVM_SET_CPUID2 */
  229. struct kvm_cpuid2 {
  230. __u32 nent;
  231. __u32 padding;
  232. struct kvm_cpuid_entry2 entries[];
  233. };
  234. /* for KVM_GET_PIT and KVM_SET_PIT */
  235. struct 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. struct kvm_debug_exit_arch {
  251. __u32 exception;
  252. __u32 pad;
  253. __u64 pc;
  254. __u64 dr6;
  255. __u64 dr7;
  256. };
  257. #define KVM_GUESTDBG_USE_SW_BP 0x00010000
  258. #define KVM_GUESTDBG_USE_HW_BP 0x00020000
  259. #define KVM_GUESTDBG_INJECT_DB 0x00040000
  260. #define KVM_GUESTDBG_INJECT_BP 0x00080000
  261. #define KVM_GUESTDBG_BLOCKIRQ 0x00100000
  262. /* for KVM_SET_GUEST_DEBUG */
  263. struct kvm_guest_debug_arch {
  264. __u64 debugreg[8];
  265. };
  266. struct kvm_pit_state {
  267. struct kvm_pit_channel_state channels[3];
  268. };
  269. #define KVM_PIT_FLAGS_HPET_LEGACY 0x00000001
  270. #define KVM_PIT_FLAGS_SPEAKER_DATA_ON 0x00000002
  271. struct kvm_pit_state2 {
  272. struct kvm_pit_channel_state channels[3];
  273. __u32 flags;
  274. __u32 reserved[9];
  275. };
  276. struct kvm_reinject_control {
  277. __u8 pit_reinject;
  278. __u8 reserved[31];
  279. };
  280. /* When set in flags, include corresponding fields on KVM_SET_VCPU_EVENTS */
  281. #define KVM_VCPUEVENT_VALID_NMI_PENDING 0x00000001
  282. #define KVM_VCPUEVENT_VALID_SIPI_VECTOR 0x00000002
  283. #define KVM_VCPUEVENT_VALID_SHADOW 0x00000004
  284. #define KVM_VCPUEVENT_VALID_SMM 0x00000008
  285. #define KVM_VCPUEVENT_VALID_PAYLOAD 0x00000010
  286. #define KVM_VCPUEVENT_VALID_TRIPLE_FAULT 0x00000020
  287. /* Interrupt shadow states */
  288. #define KVM_X86_SHADOW_INT_MOV_SS 0x01
  289. #define KVM_X86_SHADOW_INT_STI 0x02
  290. /* for KVM_GET/SET_VCPU_EVENTS */
  291. struct kvm_vcpu_events {
  292. struct {
  293. __u8 injected;
  294. __u8 nr;
  295. __u8 has_error_code;
  296. __u8 pending;
  297. __u32 error_code;
  298. } exception;
  299. struct {
  300. __u8 injected;
  301. __u8 nr;
  302. __u8 soft;
  303. __u8 shadow;
  304. } interrupt;
  305. struct {
  306. __u8 injected;
  307. __u8 pending;
  308. __u8 masked;
  309. __u8 pad;
  310. } nmi;
  311. __u32 sipi_vector;
  312. __u32 flags;
  313. struct {
  314. __u8 smm;
  315. __u8 pending;
  316. __u8 smm_inside_nmi;
  317. __u8 latched_init;
  318. } smi;
  319. struct {
  320. __u8 pending;
  321. } triple_fault;
  322. __u8 reserved[26];
  323. __u8 exception_has_payload;
  324. __u64 exception_payload;
  325. };
  326. /* for KVM_GET/SET_DEBUGREGS */
  327. struct kvm_debugregs {
  328. __u64 db[4];
  329. __u64 dr6;
  330. __u64 dr7;
  331. __u64 flags;
  332. __u64 reserved[9];
  333. };
  334. /* for KVM_CAP_XSAVE and KVM_CAP_XSAVE2 */
  335. struct kvm_xsave {
  336. /*
  337. * KVM_GET_XSAVE2 and KVM_SET_XSAVE write and read as many bytes
  338. * as are returned by KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)
  339. * respectively, when invoked on the vm file descriptor.
  340. *
  341. * The size value returned by KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)
  342. * will always be at least 4096. Currently, it is only greater
  343. * than 4096 if a dynamic feature has been enabled with
  344. * ``arch_prctl()``, but this may change in the future.
  345. *
  346. * The offsets of the state save areas in struct kvm_xsave follow
  347. * the contents of CPUID leaf 0xD on the host.
  348. */
  349. __u32 region[1024];
  350. __u32 extra[];
  351. };
  352. #define KVM_MAX_XCRS 16
  353. struct kvm_xcr {
  354. __u32 xcr;
  355. __u32 reserved;
  356. __u64 value;
  357. };
  358. struct kvm_xcrs {
  359. __u32 nr_xcrs;
  360. __u32 flags;
  361. struct kvm_xcr xcrs[KVM_MAX_XCRS];
  362. __u64 padding[16];
  363. };
  364. #define KVM_SYNC_X86_REGS (1UL << 0)
  365. #define KVM_SYNC_X86_SREGS (1UL << 1)
  366. #define KVM_SYNC_X86_EVENTS (1UL << 2)
  367. #define KVM_SYNC_X86_VALID_FIELDS \
  368. (KVM_SYNC_X86_REGS| \
  369. KVM_SYNC_X86_SREGS| \
  370. KVM_SYNC_X86_EVENTS)
  371. /* kvm_sync_regs struct included by kvm_run struct */
  372. struct kvm_sync_regs {
  373. /* Members of this structure are potentially malicious.
  374. * Care must be taken by code reading, esp. interpreting,
  375. * data fields from them inside KVM to prevent TOCTOU and
  376. * double-fetch types of vulnerabilities.
  377. */
  378. struct kvm_regs regs;
  379. struct kvm_sregs sregs;
  380. struct kvm_vcpu_events events;
  381. };
  382. #define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0)
  383. #define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1)
  384. #define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2)
  385. #define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3)
  386. #define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT (1 << 4)
  387. #define KVM_X86_QUIRK_FIX_HYPERCALL_INSN (1 << 5)
  388. #define KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS (1 << 6)
  389. #define KVM_STATE_NESTED_FORMAT_VMX 0
  390. #define KVM_STATE_NESTED_FORMAT_SVM 1
  391. #define KVM_STATE_NESTED_GUEST_MODE 0x00000001
  392. #define KVM_STATE_NESTED_RUN_PENDING 0x00000002
  393. #define KVM_STATE_NESTED_EVMCS 0x00000004
  394. #define KVM_STATE_NESTED_MTF_PENDING 0x00000008
  395. #define KVM_STATE_NESTED_GIF_SET 0x00000100
  396. #define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
  397. #define KVM_STATE_NESTED_SMM_VMXON 0x00000002
  398. #define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000
  399. #define KVM_STATE_NESTED_SVM_VMCB_SIZE 0x1000
  400. #define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001
  401. /* attributes for system fd (group 0) */
  402. #define KVM_X86_XCOMP_GUEST_SUPP 0
  403. struct kvm_vmx_nested_state_data {
  404. __u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
  405. __u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];
  406. };
  407. struct kvm_vmx_nested_state_hdr {
  408. __u64 vmxon_pa;
  409. __u64 vmcs12_pa;
  410. struct {
  411. __u16 flags;
  412. } smm;
  413. __u16 pad;
  414. __u32 flags;
  415. __u64 preemption_timer_deadline;
  416. };
  417. struct kvm_svm_nested_state_data {
  418. /* Save area only used if KVM_STATE_NESTED_RUN_PENDING. */
  419. __u8 vmcb12[KVM_STATE_NESTED_SVM_VMCB_SIZE];
  420. };
  421. struct kvm_svm_nested_state_hdr {
  422. __u64 vmcb_pa;
  423. };
  424. /* for KVM_CAP_NESTED_STATE */
  425. struct kvm_nested_state {
  426. __u16 flags;
  427. __u16 format;
  428. __u32 size;
  429. union {
  430. struct kvm_vmx_nested_state_hdr vmx;
  431. struct kvm_svm_nested_state_hdr svm;
  432. /* Pad the header to 128 bytes. */
  433. __u8 pad[120];
  434. } hdr;
  435. /*
  436. * Define data region as 0 bytes to preserve backwards-compatability
  437. * to old definition of kvm_nested_state in order to avoid changing
  438. * KVM_{GET,PUT}_NESTED_STATE ioctl values.
  439. */
  440. union {
  441. __DECLARE_FLEX_ARRAY(struct kvm_vmx_nested_state_data, vmx);
  442. __DECLARE_FLEX_ARRAY(struct kvm_svm_nested_state_data, svm);
  443. } data;
  444. };
  445. /* for KVM_CAP_PMU_EVENT_FILTER */
  446. struct kvm_pmu_event_filter {
  447. __u32 action;
  448. __u32 nevents;
  449. __u32 fixed_counter_bitmap;
  450. __u32 flags;
  451. __u32 pad[4];
  452. __u64 events[];
  453. };
  454. #define KVM_PMU_EVENT_ALLOW 0
  455. #define KVM_PMU_EVENT_DENY 1
  456. #define KVM_PMU_EVENT_FLAG_MASKED_EVENTS BIT(0)
  457. #define KVM_PMU_EVENT_FLAGS_VALID_MASK (KVM_PMU_EVENT_FLAG_MASKED_EVENTS)
  458. /*
  459. * Masked event layout.
  460. * Bits Description
  461. * ---- -----------
  462. * 7:0 event select (low bits)
  463. * 15:8 umask match
  464. * 31:16 unused
  465. * 35:32 event select (high bits)
  466. * 36:54 unused
  467. * 55 exclude bit
  468. * 63:56 umask mask
  469. */
  470. #define KVM_PMU_ENCODE_MASKED_ENTRY(event_select, mask, match, exclude) \
  471. (((event_select) & 0xFFULL) | (((event_select) & 0XF00ULL) << 24) | \
  472. (((mask) & 0xFFULL) << 56) | \
  473. (((match) & 0xFFULL) << 8) | \
  474. ((__u64)(!!(exclude)) << 55))
  475. #define KVM_PMU_MASKED_ENTRY_EVENT_SELECT \
  476. (GENMASK_ULL(7, 0) | GENMASK_ULL(35, 32))
  477. #define KVM_PMU_MASKED_ENTRY_UMASK_MASK (GENMASK_ULL(63, 56))
  478. #define KVM_PMU_MASKED_ENTRY_UMASK_MATCH (GENMASK_ULL(15, 8))
  479. #define KVM_PMU_MASKED_ENTRY_EXCLUDE (BIT_ULL(55))
  480. #define KVM_PMU_MASKED_ENTRY_UMASK_MASK_SHIFT (56)
  481. /* for KVM_{GET,SET,HAS}_DEVICE_ATTR */
  482. #define KVM_VCPU_TSC_CTRL 0 /* control group for the timestamp counter (TSC) */
  483. #define KVM_VCPU_TSC_OFFSET 0 /* attribute for the TSC offset */
  484. /* x86-specific KVM_EXIT_HYPERCALL flags. */
  485. #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0)
  486. #endif /* _ASM_X86_KVM_H */