• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Bindings for the Linux KVM (Kernel Virtual Machine) API.
6 
7 #![cfg(unix)]
8 #![allow(non_upper_case_globals)]
9 #![allow(non_camel_case_types)]
10 #![allow(non_snake_case)]
11 
12 use base::ioctl_io_nr;
13 use base::ioctl_ior_nr;
14 use base::ioctl_iow_nr;
15 use base::ioctl_iowr_nr;
16 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
17 use data_model::flexible_array_impl;
18 // Each of the below modules defines ioctls specific to their platform.
19 
20 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
21 pub const KVM_MSR_FILTER_RANGE_MAX_BITS: usize = 0x2000;
22 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
23 pub const KVM_MSR_FILTER_RANGE_MAX_BYTES: usize = KVM_MSR_FILTER_RANGE_MAX_BITS / 8;
24 
25 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
26 pub mod x86 {
27     // generated with bindgen /usr/include/linux/kvm.h --no-unstable-rust --constified-enum '*' --with-derive-default
28     #[allow(clippy::all)]
29     pub mod bindings;
30     use base::ioctl_ior_nr;
31     use base::ioctl_iow_nr;
32     use base::ioctl_iowr_nr;
33 
34     pub use crate::bindings::*;
35 
36     ioctl_iowr_nr!(KVM_GET_MSR_INDEX_LIST, KVMIO, 0x02, kvm_msr_list);
37     ioctl_iowr_nr!(KVM_GET_SUPPORTED_CPUID, KVMIO, 0x05, kvm_cpuid2);
38     ioctl_iowr_nr!(KVM_GET_EMULATED_CPUID, KVMIO, 0x09, kvm_cpuid2);
39     ioctl_iow_nr!(KVM_SET_MEMORY_ALIAS, KVMIO, 0x43, kvm_memory_alias);
40     ioctl_iow_nr!(KVM_XEN_HVM_CONFIG, KVMIO, 0x7a, kvm_xen_hvm_config);
41     ioctl_ior_nr!(KVM_GET_PIT2, KVMIO, 0x9f, kvm_pit_state2);
42     ioctl_iow_nr!(KVM_SET_PIT2, KVMIO, 0xa0, kvm_pit_state2);
43     ioctl_iowr_nr!(KVM_GET_MSRS, KVMIO, 0x88, kvm_msrs);
44     ioctl_iow_nr!(KVM_SET_MSRS, KVMIO, 0x89, kvm_msrs);
45     ioctl_iow_nr!(KVM_SET_CPUID, KVMIO, 0x8a, kvm_cpuid);
46     ioctl_ior_nr!(KVM_GET_LAPIC, KVMIO, 0x8e, kvm_lapic_state);
47     ioctl_iow_nr!(KVM_SET_LAPIC, KVMIO, 0x8f, kvm_lapic_state);
48     ioctl_iow_nr!(KVM_SET_CPUID2, KVMIO, 0x90, kvm_cpuid2);
49     ioctl_iowr_nr!(KVM_GET_CPUID2, KVMIO, 0x91, kvm_cpuid2);
50     ioctl_iow_nr!(KVM_X86_SETUP_MCE, KVMIO, 0x9c, u64);
51     ioctl_ior_nr!(KVM_X86_GET_MCE_CAP_SUPPORTED, KVMIO, 0x9d, u64);
52     ioctl_iow_nr!(KVM_X86_SET_MCE, KVMIO, 0x9e, kvm_x86_mce);
53     ioctl_ior_nr!(KVM_GET_VCPU_EVENTS, KVMIO, 0x9f, kvm_vcpu_events);
54     ioctl_iow_nr!(KVM_SET_VCPU_EVENTS, KVMIO, 0xa0, kvm_vcpu_events);
55     ioctl_ior_nr!(KVM_GET_DEBUGREGS, KVMIO, 0xa1, kvm_debugregs);
56     ioctl_iow_nr!(KVM_SET_DEBUGREGS, KVMIO, 0xa2, kvm_debugregs);
57     ioctl_ior_nr!(KVM_GET_XSAVE, KVMIO, 0xa4, kvm_xsave);
58     ioctl_iow_nr!(KVM_SET_XSAVE, KVMIO, 0xa5, kvm_xsave);
59     ioctl_ior_nr!(KVM_GET_XCRS, KVMIO, 0xa6, kvm_xcrs);
60     ioctl_iow_nr!(KVM_SET_XCRS, KVMIO, 0xa7, kvm_xcrs);
61     ioctl_iowr_nr!(KVM_GET_SUPPORTED_HV_CPUID, KVMIO, 0xc1, kvm_cpuid2);
62     ioctl_ior_nr!(KVM_GET_XSAVE2, KVMIO, 0xcf, kvm_xsave);
63 }
64 
65 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
66 pub mod aarch64 {
67     // generated with bindgen <arm sysroot>/usr/include/linux/kvm.h --no-unstable-rust --constified-enum '*' --with-derive-default -- -I<arm sysroot>/usr/include
68     pub mod bindings;
69     use base::ioctl_ior_nr;
70     use base::ioctl_iow_nr;
71     pub use bindings::*;
72 
73     ioctl_iow_nr!(KVM_ARM_SET_DEVICE_ADDR, KVMIO, 0xab, kvm_arm_device_addr);
74     ioctl_iow_nr!(KVM_ARM_VCPU_INIT, KVMIO, 0xae, kvm_vcpu_init);
75     ioctl_ior_nr!(KVM_ARM_PREFERRED_TARGET, KVMIO, 0xaf, kvm_vcpu_init);
76 }
77 
78 // These ioctls are commonly defined on all/multiple platforms.
79 ioctl_io_nr!(KVM_GET_API_VERSION, KVMIO, 0x00);
80 ioctl_io_nr!(KVM_CREATE_VM, KVMIO, 0x01);
81 ioctl_io_nr!(KVM_CHECK_EXTENSION, KVMIO, 0x03);
82 ioctl_io_nr!(KVM_GET_VCPU_MMAP_SIZE, KVMIO, 0x04);
83 ioctl_iow_nr!(KVM_SET_MEMORY_REGION, KVMIO, 0x40, kvm_memory_region);
84 ioctl_io_nr!(KVM_CREATE_VCPU, KVMIO, 0x41);
85 ioctl_iow_nr!(KVM_GET_DIRTY_LOG, KVMIO, 0x42, kvm_dirty_log);
86 ioctl_io_nr!(KVM_SET_NR_MMU_PAGES, KVMIO, 0x44);
87 ioctl_io_nr!(KVM_GET_NR_MMU_PAGES, KVMIO, 0x45);
88 ioctl_iow_nr!(
89     KVM_SET_USER_MEMORY_REGION,
90     KVMIO,
91     0x46,
92     kvm_userspace_memory_region
93 );
94 ioctl_io_nr!(KVM_SET_TSS_ADDR, KVMIO, 0x47);
95 ioctl_iow_nr!(KVM_SET_IDENTITY_MAP_ADDR, KVMIO, 0x48, u64);
96 ioctl_io_nr!(KVM_CREATE_IRQCHIP, KVMIO, 0x60);
97 ioctl_iow_nr!(KVM_IRQ_LINE, KVMIO, 0x61, kvm_irq_level);
98 ioctl_iowr_nr!(KVM_GET_IRQCHIP, KVMIO, 0x62, kvm_irqchip);
99 ioctl_ior_nr!(KVM_SET_IRQCHIP, KVMIO, 0x63, kvm_irqchip);
100 ioctl_io_nr!(KVM_CREATE_PIT, KVMIO, 0x64);
101 ioctl_iowr_nr!(KVM_IRQ_LINE_STATUS, KVMIO, 0x67, kvm_irq_level);
102 ioctl_iow_nr!(
103     KVM_REGISTER_COALESCED_MMIO,
104     KVMIO,
105     0x67,
106     kvm_coalesced_mmio_zone
107 );
108 ioctl_iow_nr!(
109     KVM_UNREGISTER_COALESCED_MMIO,
110     KVMIO,
111     0x68,
112     kvm_coalesced_mmio_zone
113 );
114 ioctl_ior_nr!(KVM_ASSIGN_PCI_DEVICE, KVMIO, 0x69, kvm_assigned_pci_dev);
115 ioctl_iow_nr!(KVM_SET_GSI_ROUTING, KVMIO, 0x6a, kvm_irq_routing);
116 ioctl_iow_nr!(KVM_ASSIGN_DEV_IRQ, KVMIO, 0x70, kvm_assigned_irq);
117 ioctl_io_nr!(KVM_REINJECT_CONTROL, KVMIO, 0x71);
118 ioctl_iow_nr!(KVM_DEASSIGN_PCI_DEVICE, KVMIO, 0x72, kvm_assigned_pci_dev);
119 ioctl_iow_nr!(KVM_ASSIGN_SET_MSIX_NR, KVMIO, 0x73, kvm_assigned_msix_nr);
120 ioctl_iow_nr!(
121     KVM_ASSIGN_SET_MSIX_ENTRY,
122     KVMIO,
123     0x74,
124     kvm_assigned_msix_entry
125 );
126 ioctl_iow_nr!(KVM_DEASSIGN_DEV_IRQ, KVMIO, 0x75, kvm_assigned_irq);
127 ioctl_iow_nr!(KVM_IRQFD, KVMIO, 0x76, kvm_irqfd);
128 ioctl_iow_nr!(KVM_CREATE_PIT2, KVMIO, 0x77, kvm_pit_config);
129 ioctl_io_nr!(KVM_SET_BOOT_CPU_ID, KVMIO, 0x78);
130 ioctl_iow_nr!(KVM_IOEVENTFD, KVMIO, 0x79, kvm_ioeventfd);
131 ioctl_iow_nr!(KVM_SET_CLOCK, KVMIO, 0x7b, kvm_clock_data);
132 ioctl_ior_nr!(KVM_GET_CLOCK, KVMIO, 0x7c, kvm_clock_data);
133 ioctl_io_nr!(KVM_SET_TSC_KHZ, KVMIO, 0xa2);
134 ioctl_io_nr!(KVM_GET_TSC_KHZ, KVMIO, 0xa3);
135 ioctl_iow_nr!(KVM_ASSIGN_SET_INTX_MASK, KVMIO, 0xa4, kvm_assigned_pci_dev);
136 ioctl_iow_nr!(KVM_SIGNAL_MSI, KVMIO, 0xa5, kvm_msi);
137 ioctl_iowr_nr!(KVM_CREATE_DEVICE, KVMIO, 0xe0, kvm_create_device);
138 ioctl_iow_nr!(KVM_SET_DEVICE_ATTR, KVMIO, 0xe1, kvm_device_attr);
139 ioctl_iow_nr!(KVM_GET_DEVICE_ATTR, KVMIO, 0xe2, kvm_device_attr);
140 ioctl_iow_nr!(KVM_HAS_DEVICE_ATTR, KVMIO, 0xe3, kvm_device_attr);
141 ioctl_io_nr!(KVM_RUN, KVMIO, 0x80);
142 // The following two ioctls are commonly defined but specifically excluded
143 // from arm platforms.
144 #[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
145 ioctl_ior_nr!(KVM_GET_REGS, KVMIO, 0x81, kvm_regs);
146 #[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
147 ioctl_iow_nr!(KVM_SET_REGS, KVMIO, 0x82, kvm_regs);
148 ioctl_ior_nr!(KVM_GET_SREGS, KVMIO, 0x83, kvm_sregs);
149 ioctl_iow_nr!(KVM_SET_SREGS, KVMIO, 0x84, kvm_sregs);
150 ioctl_iowr_nr!(KVM_TRANSLATE, KVMIO, 0x85, kvm_translation);
151 ioctl_iow_nr!(KVM_INTERRUPT, KVMIO, 0x86, kvm_interrupt);
152 ioctl_iow_nr!(KVM_SET_SIGNAL_MASK, KVMIO, 0x8b, kvm_signal_mask);
153 ioctl_ior_nr!(KVM_GET_FPU, KVMIO, 0x8c, kvm_fpu);
154 ioctl_iow_nr!(KVM_SET_FPU, KVMIO, 0x8d, kvm_fpu);
155 ioctl_iowr_nr!(KVM_TPR_ACCESS_REPORTING, KVMIO, 0x92, kvm_tpr_access_ctl);
156 ioctl_iow_nr!(KVM_SET_VAPIC_ADDR, KVMIO, 0x93, kvm_vapic_addr);
157 ioctl_ior_nr!(KVM_GET_MP_STATE, KVMIO, 0x98, kvm_mp_state);
158 ioctl_iow_nr!(KVM_SET_MP_STATE, KVMIO, 0x99, kvm_mp_state);
159 ioctl_io_nr!(KVM_NMI, KVMIO, 0x9a);
160 ioctl_iow_nr!(KVM_SET_GUEST_DEBUG, KVMIO, 0x9b, kvm_guest_debug);
161 ioctl_iow_nr!(KVM_ENABLE_CAP, KVMIO, 0xa3, kvm_enable_cap);
162 ioctl_iow_nr!(KVM_DIRTY_TLB, KVMIO, 0xaa, kvm_dirty_tlb);
163 ioctl_iow_nr!(KVM_GET_ONE_REG, KVMIO, 0xab, kvm_one_reg);
164 ioctl_iow_nr!(KVM_SET_ONE_REG, KVMIO, 0xac, kvm_one_reg);
165 ioctl_io_nr!(KVM_KVMCLOCK_CTRL, KVMIO, 0xad);
166 ioctl_iowr_nr!(KVM_GET_REG_LIST, KVMIO, 0xb0, kvm_reg_list);
167 ioctl_io_nr!(KVM_SMI, KVMIO, 0xb7);
168 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
169 ioctl_iow_nr!(KVM_X86_SET_MSR_FILTER, KVMIO, 0xc6, kvm_msr_filter);
170 
171 // Along with the common ioctls, we reexport the ioctls of the current
172 // platform.
173 
174 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
175 pub use crate::x86::*;
176 
177 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
178 flexible_array_impl!(kvm_cpuid2, kvm_cpuid_entry2, nent, entries);
179 
180 #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
181 pub use aarch64::*;
182