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 17 #include <stdio.h> 18 #include <unistd.h> 19 #include <stropts.h> 20 #include <strings.h> 21 #include <signal.h> 22 #include <setjmp.h> 23 #include <libgen.h> 24 25 #include <sys/vmm.h> 26 #include <sys/vmm_dev.h> 27 #include <sys/mman.h> 28 #include <vmmapi.h> 29 30 #include "common.h" 31 32 #define TEST_SEGID 0 33 #define PAGE_CNT 1024 34 #define PAGE_SZ 4096 35 #define SEG_SZ (PAGE_CNT * PAGE_SZ) 36 37 int 38 main(int argc, char *argv[]) 39 { 40 struct vmctx *ctx; 41 int res, fd; 42 void *seg_obj, *guest_mem; 43 44 ctx = create_test_vm(); 45 if (ctx == NULL) { 46 perror("could open test VM"); 47 return (1); 48 } 49 fd = vm_get_device_fd(ctx); 50 51 res = alloc_memseg(ctx, TEST_SEGID, SEG_SZ, "test_seg"); 52 if (res != 0) { 53 perror("could not alloc memseg"); 54 goto bail; 55 } 56 off_t seg_obj_off; 57 res = vm_get_devmem_offset(ctx, TEST_SEGID, &seg_obj_off); 58 if (res != 0) { 59 perror("could not find mapping offset for seg object"); 60 goto bail; 61 } 62 63 seg_obj = mmap(NULL, SEG_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, 64 fd, seg_obj_off); 65 if (seg_obj == MAP_FAILED) { 66 perror("could not mmap seg object"); 67 goto bail; 68 } 69 70 /* populate with initial data */ 71 for (uint_t i = 0; i < PAGE_CNT; i++) { 72 uint64_t *p = (uint64_t *)((uintptr_t)seg_obj + i * PAGE_SZ); 73 74 *p = i; 75 } 76 77 res = vm_mmap_memseg(ctx, 0, TEST_SEGID, 0, SEG_SZ, PROT_ALL); 78 if (res != 0) { 79 perror("could not map memseg into vmspace"); 80 goto bail; 81 } 82 guest_mem = mmap(NULL, SEG_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, 83 fd, 0); 84 if (seg_obj == MAP_FAILED) { 85 perror("could not mmap vmspace"); 86 goto bail; 87 } 88 89 /* check data and access though vmspace */ 90 for (uint_t i = 0; i < PAGE_CNT; i++) { 91 const uint64_t off = i * PAGE_SZ; 92 uint64_t *p = (uint64_t *)((uintptr_t)guest_mem + off); 93 94 const uint64_t val = *p; 95 if (val != i) { 96 (void) printf("%lu != %u at gpa:%lx\n", val, i, off); 97 goto bail; 98 } 99 100 /* leave a change behind */ 101 *p = val * 2; 102 } 103 104 /* check changes made through vmspace */ 105 for (uint_t i = 0; i < PAGE_CNT; i++) { 106 const uint64_t off = i * PAGE_SZ; 107 uint64_t *p = (uint64_t *)((uintptr_t)seg_obj + off); 108 109 const uint_t expected = i * 2; 110 const uint64_t val = *p; 111 if (val != expected) { 112 (void) printf("%lu != %u at gpa:%lx\n", val, expected, 113 off); 114 goto bail; 115 } 116 } 117 118 /* unmap access mappings */ 119 res = munmap(guest_mem, SEG_SZ); 120 if (res != 0) { 121 perror("could not munmap vmspace"); 122 goto bail; 123 } 124 res = munmap(seg_obj, SEG_SZ); 125 if (res != 0) { 126 perror("could not munmap seg object"); 127 goto bail; 128 } 129 130 /* mission accomplished */ 131 vm_destroy(ctx); 132 (void) printf("%s\tPASS\n", basename(argv[0])); 133 return (0); 134 135 bail: 136 vm_destroy(ctx); 137 return (1); 138 } 139