1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 /* 3 * Userspace interfaces for /dev/mshv* devices and derived fds 4 * 5 * This file is divided into sections containing data structures and IOCTLs for 6 * a particular set of related devices or derived file descriptors. 7 * 8 * The IOCTL definitions are at the end of each section. They are grouped by 9 * device/fd, so that new IOCTLs can easily be added with a monotonically 10 * increasing number. 11 */ 12 #ifndef _UAPI_LINUX_MSHV_H 13 #define _UAPI_LINUX_MSHV_H 14 15 #include <linux/types.h> 16 17 #define MSHV_IOCTL 0xB8 18 19 /* 20 ******************************************* 21 * Entry point to main VMM APIs: /dev/mshv * 22 ******************************************* 23 */ 24 25 enum { 26 MSHV_PT_BIT_LAPIC, 27 MSHV_PT_BIT_X2APIC, 28 MSHV_PT_BIT_GPA_SUPER_PAGES, 29 MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES, 30 MSHV_PT_BIT_NESTED_VIRTUALIZATION, 31 MSHV_PT_BIT_SMT_ENABLED_GUEST, 32 MSHV_PT_BIT_COUNT, 33 }; 34 35 #define MSHV_PT_FLAGS_MASK ((1 << MSHV_PT_BIT_COUNT) - 1) 36 37 enum { 38 MSHV_PT_ISOLATION_NONE, 39 MSHV_PT_ISOLATION_COUNT, 40 }; 41 42 /** 43 * struct mshv_create_partition - arguments for MSHV_CREATE_PARTITION 44 * @pt_flags: Bitmask of 1 << MSHV_PT_BIT_* 45 * @pt_isolation: MSHV_PT_ISOLATION_* 46 * 47 * This is the initial/v1 version for backward compatibility. 48 * 49 * Returns a file descriptor to act as a handle to a guest partition. 50 * At this point the partition is not yet initialized in the hypervisor. 51 * Some operations must be done with the partition in this state, e.g. setting 52 * so-called "early" partition properties. The partition can then be 53 * initialized with MSHV_INITIALIZE_PARTITION. 54 */ 55 struct mshv_create_partition { 56 __u64 pt_flags; 57 __u64 pt_isolation; 58 }; 59 60 #define MSHV_NUM_CPU_FEATURES_BANKS 2 61 62 /** 63 * struct mshv_create_partition_v2 64 * 65 * This is extended version of the above initial MSHV_CREATE_PARTITION 66 * ioctl and allows for following additional parameters: 67 * 68 * @pt_num_cpu_fbanks: Must be set to MSHV_NUM_CPU_FEATURES_BANKS. 69 * @pt_cpu_fbanks: Disabled processor feature banks array. 70 * @pt_disabled_xsave: Disabled xsave feature bits. 71 * 72 * pt_cpu_fbanks and pt_disabled_xsave are passed through as-is to the create 73 * partition hypercall. 74 * 75 * Returns : same as above original mshv_create_partition 76 */ 77 struct mshv_create_partition_v2 { 78 __u64 pt_flags; 79 __u64 pt_isolation; 80 __u16 pt_num_cpu_fbanks; 81 __u8 pt_rsvd[6]; /* MBZ */ 82 __u64 pt_cpu_fbanks[MSHV_NUM_CPU_FEATURES_BANKS]; 83 __u64 pt_rsvd1[2]; /* MBZ */ 84 #if defined(__x86_64__) 85 __u64 pt_disabled_xsave; 86 #else 87 __u64 pt_rsvd2; /* MBZ */ 88 #endif 89 } __packed; 90 91 /* /dev/mshv */ 92 #define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_partition) 93 94 /* 95 ************************ 96 * Child partition APIs * 97 ************************ 98 */ 99 100 struct mshv_create_vp { 101 __u32 vp_index; 102 }; 103 104 enum { 105 MSHV_SET_MEM_BIT_WRITABLE, 106 MSHV_SET_MEM_BIT_EXECUTABLE, 107 MSHV_SET_MEM_BIT_UNMAP, 108 MSHV_SET_MEM_BIT_COUNT 109 }; 110 111 #define MSHV_SET_MEM_FLAGS_MASK ((1 << MSHV_SET_MEM_BIT_COUNT) - 1) 112 113 /* The hypervisor's "native" page size */ 114 #define MSHV_HV_PAGE_SIZE 0x1000 115 116 /** 117 * struct mshv_user_mem_region - arguments for MSHV_SET_GUEST_MEMORY 118 * @size: Size of the memory region (bytes). Must be aligned to 119 * MSHV_HV_PAGE_SIZE 120 * @guest_pfn: Base guest page number to map 121 * @userspace_addr: Base address of userspace memory. Must be aligned to 122 * MSHV_HV_PAGE_SIZE 123 * @flags: Bitmask of 1 << MSHV_SET_MEM_BIT_*. If (1 << MSHV_SET_MEM_BIT_UNMAP) 124 * is set, ignore other bits. 125 * @rsvd: MBZ 126 * 127 * Map or unmap a region of userspace memory to Guest Physical Addresses (GPA). 128 * Mappings can't overlap in GPA space. 129 * To unmap, these fields must match an existing mapping. 130 */ 131 struct mshv_user_mem_region { 132 __u64 size; 133 __u64 guest_pfn; 134 __u64 userspace_addr; 135 __u8 flags; 136 __u8 rsvd[7]; 137 }; 138 139 enum { 140 MSHV_IRQFD_BIT_DEASSIGN, 141 MSHV_IRQFD_BIT_RESAMPLE, 142 MSHV_IRQFD_BIT_COUNT, 143 }; 144 145 #define MSHV_IRQFD_FLAGS_MASK ((1 << MSHV_IRQFD_BIT_COUNT) - 1) 146 147 struct mshv_user_irqfd { 148 __s32 fd; 149 __s32 resamplefd; 150 __u32 gsi; 151 __u32 flags; 152 }; 153 154 enum { 155 MSHV_IOEVENTFD_BIT_DATAMATCH, 156 MSHV_IOEVENTFD_BIT_PIO, 157 MSHV_IOEVENTFD_BIT_DEASSIGN, 158 MSHV_IOEVENTFD_BIT_COUNT, 159 }; 160 161 #define MSHV_IOEVENTFD_FLAGS_MASK ((1 << MSHV_IOEVENTFD_BIT_COUNT) - 1) 162 163 struct mshv_user_ioeventfd { 164 __u64 datamatch; 165 __u64 addr; /* legal pio/mmio address */ 166 __u32 len; /* 1, 2, 4, or 8 bytes */ 167 __s32 fd; 168 __u32 flags; 169 __u8 rsvd[4]; 170 }; 171 172 struct mshv_user_irq_entry { 173 __u32 gsi; 174 __u32 address_lo; 175 __u32 address_hi; 176 __u32 data; 177 }; 178 179 struct mshv_user_irq_table { 180 __u32 nr; 181 __u32 rsvd; /* MBZ */ 182 struct mshv_user_irq_entry entries[]; 183 }; 184 185 enum { 186 MSHV_GPAP_ACCESS_TYPE_ACCESSED, 187 MSHV_GPAP_ACCESS_TYPE_DIRTY, 188 MSHV_GPAP_ACCESS_TYPE_COUNT /* Count of enum members */ 189 }; 190 191 enum { 192 MSHV_GPAP_ACCESS_OP_NOOP, 193 MSHV_GPAP_ACCESS_OP_CLEAR, 194 MSHV_GPAP_ACCESS_OP_SET, 195 MSHV_GPAP_ACCESS_OP_COUNT /* Count of enum members */ 196 }; 197 198 /** 199 * struct mshv_gpap_access_bitmap - arguments for MSHV_GET_GPAP_ACCESS_BITMAP 200 * @access_type: MSHV_GPAP_ACCESS_TYPE_* - The type of access to record in the 201 * bitmap 202 * @access_op: MSHV_GPAP_ACCESS_OP_* - Allows an optional clear or set of all 203 * the access states in the range, after retrieving the current 204 * states. 205 * @rsvd: MBZ 206 * @page_count: Number of pages 207 * @gpap_base: Base gpa page number 208 * @bitmap_ptr: Output buffer for bitmap, at least (page_count + 7) / 8 bytes 209 * 210 * Retrieve a bitmap of either ACCESSED or DIRTY bits for a given range of guest 211 * memory, and optionally clear or set the bits. 212 */ 213 struct mshv_gpap_access_bitmap { 214 __u8 access_type; 215 __u8 access_op; 216 __u8 rsvd[6]; 217 __u64 page_count; 218 __u64 gpap_base; 219 __u64 bitmap_ptr; 220 }; 221 222 /** 223 * struct mshv_root_hvcall - arguments for MSHV_ROOT_HVCALL 224 * @code: Hypercall code (HVCALL_*) 225 * @reps: in: Rep count ('repcount') 226 * out: Reps completed ('repcomp'). MBZ unless rep hvcall 227 * @in_sz: Size of input incl rep data. <= MSHV_HV_PAGE_SIZE 228 * @out_sz: Size of output buffer. <= MSHV_HV_PAGE_SIZE. MBZ if out_ptr is 0 229 * @status: in: MBZ 230 * out: HV_STATUS_* from hypercall 231 * @rsvd: MBZ 232 * @in_ptr: Input data buffer (struct hv_input_*). If used with partition or 233 * vp fd, partition id field is populated by kernel. 234 * @out_ptr: Output data buffer (optional) 235 */ 236 struct mshv_root_hvcall { 237 __u16 code; 238 __u16 reps; 239 __u16 in_sz; 240 __u16 out_sz; 241 __u16 status; 242 __u8 rsvd[6]; 243 __u64 in_ptr; 244 __u64 out_ptr; 245 }; 246 247 /* Partition fds created with MSHV_CREATE_PARTITION */ 248 #define MSHV_INITIALIZE_PARTITION _IO(MSHV_IOCTL, 0x00) 249 #define MSHV_CREATE_VP _IOW(MSHV_IOCTL, 0x01, struct mshv_create_vp) 250 #define MSHV_SET_GUEST_MEMORY _IOW(MSHV_IOCTL, 0x02, struct mshv_user_mem_region) 251 #define MSHV_IRQFD _IOW(MSHV_IOCTL, 0x03, struct mshv_user_irqfd) 252 #define MSHV_IOEVENTFD _IOW(MSHV_IOCTL, 0x04, struct mshv_user_ioeventfd) 253 #define MSHV_SET_MSI_ROUTING _IOW(MSHV_IOCTL, 0x05, struct mshv_user_irq_table) 254 #define MSHV_GET_GPAP_ACCESS_BITMAP _IOWR(MSHV_IOCTL, 0x06, struct mshv_gpap_access_bitmap) 255 /* Generic hypercall */ 256 #define MSHV_ROOT_HVCALL _IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall) 257 258 /* 259 ******************************** 260 * VP APIs for child partitions * 261 ******************************** 262 */ 263 264 #define MSHV_RUN_VP_BUF_SZ 256 265 266 /* 267 * VP state pages may be mapped to userspace via mmap(). 268 * To specify which state page, use MSHV_VP_MMAP_OFFSET_ values multiplied by 269 * the system page size. 270 * e.g. 271 * long page_size = sysconf(_SC_PAGE_SIZE); 272 * void *reg_page = mmap(NULL, MSHV_HV_PAGE_SIZE, PROT_READ|PROT_WRITE, 273 * MAP_SHARED, vp_fd, 274 * MSHV_VP_MMAP_OFFSET_REGISTERS * page_size); 275 */ 276 enum { 277 MSHV_VP_MMAP_OFFSET_REGISTERS, 278 MSHV_VP_MMAP_OFFSET_INTERCEPT_MESSAGE, 279 MSHV_VP_MMAP_OFFSET_GHCB, 280 MSHV_VP_MMAP_OFFSET_COUNT 281 }; 282 283 /** 284 * struct mshv_run_vp - argument for MSHV_RUN_VP 285 * @msg_buf: On success, the intercept message is copied here. It can be 286 * interpreted using the relevant hypervisor definitions. 287 */ 288 struct mshv_run_vp { 289 __u8 msg_buf[MSHV_RUN_VP_BUF_SZ]; 290 }; 291 292 enum { 293 MSHV_VP_STATE_LAPIC, /* Local interrupt controller state (either arch) */ 294 MSHV_VP_STATE_XSAVE, /* XSAVE data in compacted form (x86_64) */ 295 MSHV_VP_STATE_SIMP, 296 MSHV_VP_STATE_SIEFP, 297 MSHV_VP_STATE_SYNTHETIC_TIMERS, 298 MSHV_VP_STATE_COUNT, 299 }; 300 301 /** 302 * struct mshv_get_set_vp_state - arguments for MSHV_[GET,SET]_VP_STATE 303 * @type: MSHV_VP_STATE_* 304 * @rsvd: MBZ 305 * @buf_sz: in: 4k page-aligned size of buffer 306 * out: Actual size of data (on EINVAL, check this to see if buffer 307 * was too small) 308 * @buf_ptr: 4k page-aligned data buffer 309 */ 310 struct mshv_get_set_vp_state { 311 __u8 type; 312 __u8 rsvd[3]; 313 __u32 buf_sz; 314 __u64 buf_ptr; 315 }; 316 317 /* VP fds created with MSHV_CREATE_VP */ 318 #define MSHV_RUN_VP _IOR(MSHV_IOCTL, 0x00, struct mshv_run_vp) 319 #define MSHV_GET_VP_STATE _IOWR(MSHV_IOCTL, 0x01, struct mshv_get_set_vp_state) 320 #define MSHV_SET_VP_STATE _IOWR(MSHV_IOCTL, 0x02, struct mshv_get_set_vp_state) 321 /* 322 * Generic hypercall 323 * Defined above in partition IOCTLs, avoid redefining it here 324 * #define MSHV_ROOT_HVCALL _IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall) 325 */ 326 327 /* Structure definitions, macros and IOCTLs for mshv_vtl */ 328 329 #define MSHV_CAP_CORE_API_STABLE 0x0 330 #define MSHV_CAP_REGISTER_PAGE 0x1 331 #define MSHV_CAP_VTL_RETURN_ACTION 0x2 332 #define MSHV_CAP_DR6_SHARED 0x3 333 #define MSHV_MAX_RUN_MSG_SIZE 256 334 335 struct mshv_vp_registers { 336 __u32 count; /* supports only 1 register at a time */ 337 __u32 reserved; /* Reserved for alignment or future use */ 338 __u64 regs_ptr; /* pointer to struct hv_register_assoc */ 339 }; 340 341 struct mshv_vtl_set_eventfd { 342 __s32 fd; 343 __u32 flag; 344 }; 345 346 struct mshv_vtl_signal_event { 347 __u32 connection_id; 348 __u32 flag; 349 }; 350 351 struct mshv_vtl_sint_post_msg { 352 __u64 message_type; 353 __u32 connection_id; 354 __u32 payload_size; /* Must not exceed HV_MESSAGE_PAYLOAD_BYTE_COUNT */ 355 __u64 payload_ptr; /* pointer to message payload (bytes) */ 356 }; 357 358 struct mshv_vtl_ram_disposition { 359 __u64 start_pfn; 360 __u64 last_pfn; 361 }; 362 363 struct mshv_vtl_set_poll_file { 364 __u32 cpu; 365 __u32 fd; 366 }; 367 368 struct mshv_vtl_hvcall_setup { 369 __u64 bitmap_array_size; /* stores number of bytes */ 370 __u64 allow_bitmap_ptr; 371 }; 372 373 struct mshv_vtl_hvcall { 374 __u64 control; /* Hypercall control code */ 375 __u64 input_size; /* Size of the input data */ 376 __u64 input_ptr; /* Pointer to the input struct */ 377 __u64 status; /* Status of the hypercall (output) */ 378 __u64 output_size; /* Size of the output data */ 379 __u64 output_ptr; /* Pointer to the output struct */ 380 }; 381 382 struct mshv_sint_mask { 383 __u8 mask; 384 __u8 reserved[7]; 385 }; 386 387 /* /dev/mshv device IOCTL */ 388 #define MSHV_CHECK_EXTENSION _IOW(MSHV_IOCTL, 0x00, __u32) 389 390 /* vtl device */ 391 #define MSHV_CREATE_VTL _IOR(MSHV_IOCTL, 0x1D, char) 392 #define MSHV_ADD_VTL0_MEMORY _IOW(MSHV_IOCTL, 0x21, struct mshv_vtl_ram_disposition) 393 #define MSHV_SET_POLL_FILE _IOW(MSHV_IOCTL, 0x25, struct mshv_vtl_set_poll_file) 394 #define MSHV_RETURN_TO_LOWER_VTL _IO(MSHV_IOCTL, 0x27) 395 #define MSHV_GET_VP_REGISTERS _IOWR(MSHV_IOCTL, 0x05, struct mshv_vp_registers) 396 #define MSHV_SET_VP_REGISTERS _IOW(MSHV_IOCTL, 0x06, struct mshv_vp_registers) 397 398 /* VMBus device IOCTLs */ 399 #define MSHV_SINT_SIGNAL_EVENT _IOW(MSHV_IOCTL, 0x22, struct mshv_vtl_signal_event) 400 #define MSHV_SINT_POST_MESSAGE _IOW(MSHV_IOCTL, 0x23, struct mshv_vtl_sint_post_msg) 401 #define MSHV_SINT_SET_EVENTFD _IOW(MSHV_IOCTL, 0x24, struct mshv_vtl_set_eventfd) 402 #define MSHV_SINT_PAUSE_MESSAGE_STREAM _IOW(MSHV_IOCTL, 0x25, struct mshv_sint_mask) 403 404 /* hv_hvcall device */ 405 #define MSHV_HVCALL_SETUP _IOW(MSHV_IOCTL, 0x1E, struct mshv_vtl_hvcall_setup) 406 #define MSHV_HVCALL _IOWR(MSHV_IOCTL, 0x1F, struct mshv_vtl_hvcall) 407 #endif 408