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