1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2022 Oxide Computer Company 14 */ 15 16 #include <stdio.h> 17 #include <unistd.h> 18 #include <stdlib.h> 19 #include <fcntl.h> 20 #include <libgen.h> 21 #include <sys/stat.h> 22 #include <errno.h> 23 #include <err.h> 24 #include <assert.h> 25 #include <sys/sysmacros.h> 26 #include <stdbool.h> 27 28 #include <sys/vmm.h> 29 #include <sys/vmm_dev.h> 30 #include <sys/vmm_data.h> 31 #include <vmmapi.h> 32 33 #include "common.h" 34 35 static void 36 should_eq_u32(const char *field_name, uint32_t a, uint32_t b) 37 { 38 if (a != b) { 39 errx(EXIT_FAILURE, "unexpected %s %u != %u", 40 field_name, a, b); 41 } 42 } 43 44 int 45 main(int argc, char *argv[]) 46 { 47 const char *suite_name = basename(argv[0]); 48 struct vmctx *ctx; 49 50 ctx = create_test_vm(suite_name); 51 if (ctx == NULL) { 52 errx(EXIT_FAILURE, "could not open test VM"); 53 } 54 55 /* 56 * Check that vmm_data import/export facility is robust in the face of 57 * potentially invalid inputs 58 */ 59 const int vmfd = vm_get_device_fd(ctx); 60 61 uint8_t buf[sizeof (struct vdi_atpic_v1) + sizeof (int)]; 62 struct vm_data_xfer vdx = { 63 .vdx_class = VDC_ATPIC, 64 .vdx_version = 1, 65 .vdx_len = sizeof (struct vdi_atpic_v1), 66 .vdx_data = buf, 67 }; 68 69 /* Attempt a valid-sized read first */ 70 if (ioctl(vmfd, VM_DATA_READ, &vdx) != 0) { 71 err(EXIT_FAILURE, "valid vmm_dat_read failed"); 72 } 73 should_eq_u32("vdx_result_len", vdx.vdx_result_len, 74 sizeof (struct vdi_atpic_v1)); 75 76 /* ... then too-small ... */ 77 vdx.vdx_len = sizeof (struct vdi_atpic_v1) - sizeof (int); 78 vdx.vdx_result_len = 0; 79 if (ioctl(vmfd, VM_DATA_READ, &vdx) == 0) { 80 errx(EXIT_FAILURE, "invalid vmm_dat_read should have failed"); 81 } 82 int error = errno; 83 if (error != ENOSPC) { 84 errx(EXIT_FAILURE, "expected ENOSPC errno, got %d", error); 85 } 86 /* the "correct" vdx_result_len should still be communicated out */ 87 should_eq_u32("vdx_result_len", vdx.vdx_result_len, 88 sizeof (struct vdi_atpic_v1)); 89 90 /* 91 * ... and too-big to round it out. 92 * 93 * This should pass, but still set vdx_result_len to the actual length 94 */ 95 vdx.vdx_len = sizeof (struct vdi_atpic_v1) + sizeof (int); 96 vdx.vdx_result_len = 0; 97 if (ioctl(vmfd, VM_DATA_READ, &vdx) != 0) { 98 err(EXIT_FAILURE, "too-large (but valid) vmm_dat_read failed"); 99 } 100 should_eq_u32("vdx_result_len", vdx.vdx_result_len, 101 sizeof (struct vdi_atpic_v1)); 102 103 /* 104 * The vmm_data_write paths should also be tested, but not until they 105 * are exposed to the general public without requring mdb -kw settings. 106 */ 107 108 vm_destroy(ctx); 109 (void) printf("%s\tPASS\n", suite_name); 110 return (EXIT_SUCCESS); 111 } 112