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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/*
* 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 2023 Oxide Computer Company
*/
/*
* VMM Time Data interface tests
*
* Note: requires `vmm_allow_state_writes` to be set
*/
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <errno.h>
#include <err.h>
#include <sys/vmm_dev.h>
#include <sys/vmm_data.h>
#include <vmmapi.h>
#include "common.h"
/*
* Constants from svm.c, redefined here for convenience
*/
#define AMD_TSC_MIN_FREQ 500000000
#define AMD_TSC_MAX_FREQ_RATIO 15
static void
should_eq_u32(const char *field_name, uint32_t a, uint32_t b)
{
if (a != b) {
errx(EXIT_FAILURE, "unexpected %s %u != %u",
field_name, a, b);
}
}
static void
should_eq_u64(const char *field_name, uint64_t a, uint64_t b)
{
if (a != b) {
errx(EXIT_FAILURE, "unexpected %s %lu != %lu",
field_name, a, b);
}
}
/* a should be >= b */
static void
should_geq_u64(const char *field_name, uint64_t a, uint64_t b)
{
if (a < b) {
errx(EXIT_FAILURE, "unexpected %s %lu < %lu",
field_name, a, b);
}
}
static void
should_geq_i64(const char *field_name, int64_t a, int64_t b)
{
if (a < b) {
errx(EXIT_FAILURE, "unexpected %s %ld < %ld",
field_name, a, b);
}
}
/*
* Test a valid VMM_DATA_READ of time data
*/
static void
test_valid_read_time_data(int vmfd, struct vdi_time_info_v1 *time_info)
{
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = time_info,
};
if (ioctl(vmfd, VM_DATA_READ, &xfer) != 0) {
errx(EXIT_FAILURE, "VMM_DATA_READ of time info failed");
}
}
/*
* Test valid VMM_DATA_WRITE of time data
*/
static void
test_valid_write_time_data(int vmfd, struct vdi_time_info_v1 *time_info)
{
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = time_info,
};
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
int error;
error = errno;
if (error == EPERM) {
warn("VMM_DATA_WRITE got EPERM: is "
"vmm_allow_state_writes set?");
}
errx(EXIT_FAILURE, "VMM_DATA_WRITE of time info failed");
}
}
/*
* Test malformed VMM_DATA_READ time data requests
*/
static void
test_invalid_read_time_data(int vmfd)
{
struct vdi_time_info_v1 res;
/* check error case: invalid vdr_len */
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = 0,
.vdx_data = &res,
};
int error;
if (ioctl(vmfd, VM_DATA_READ, &xfer) == 0) {
errx(EXIT_FAILURE,
"invalid VMM_DATA_READ of time info should fail");
}
error = errno;
if (error != ENOSPC) {
errx(EXIT_FAILURE, "test_invalid_read_time_data: "
"expected ENOSPC errno, got %d", error);
}
/* expected vdx_result_len should be communicated out */
should_eq_u32("vdx_result_len", xfer.vdx_result_len,
sizeof (struct vdi_time_info_v1));
}
/*
* Test malformed VMM_DATA_WRITE time data requests
*/
static void
test_invalid_write_time_data(int vmfd, struct vdi_time_info_v1 *src)
{
/* invalid vdx_len */
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = 0,
.vdx_data = src,
};
int error;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE,
"invalid VMM_DATA_WRITE of time info should fail");
}
error = errno;
if (error != ENOSPC) {
errx(EXIT_FAILURE, "test_invalid_write_time_data: "
"expected ENOSPC errno, got %d", error);
}
/* expected vdx_result_len should be communicated out */
should_eq_u32("vdx_result_len", xfer.vdx_result_len,
sizeof (struct vdi_time_info_v1));
}
/*
* Test platform-independent invalid frequency ratio requests
*/
static void
test_invalid_freq(int vmfd, struct vdi_time_info_v1 *src)
{
/* guest frequency of 0 always invalid */
struct vdi_time_info_v1 invalid = {
.vt_guest_freq = 0,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &invalid,
};
int error;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE,
"invalid VMM_DATA_WRITE of time info (vt_guest_freq = 0) "
"should fail");
}
error = errno;
if (error != EINVAL) {
errx(EXIT_FAILURE, "test_invalid_freq: \
expected EINVAL errno, got %d", error);
}
}
/*
* Test invalid AMD-specific frequency ratio requests
*/
static void
test_invalid_freq_amd(int vmfd, struct vdi_time_info_v1 *src)
{
struct vdi_time_info_v1 invalid = {
.vt_guest_freq = src->vt_guest_freq,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &invalid,
};
int error;
/* minimum guest frequency - 1 */
invalid.vt_guest_freq = AMD_TSC_MIN_FREQ - 1;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE, "invalid VMM_DATA_WRITE of time info "
"(min AMD guest freq) should fail");
}
error = errno;
if (error != EINVAL) {
errx(EXIT_FAILURE, "test_invalid_freq_amd (< min freq) "
"expected EINVAL errno, got %d", error);
}
/* ratio >= max ratio */
invalid.vt_guest_freq = src->vt_guest_freq * AMD_TSC_MAX_FREQ_RATIO;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE, "invalid VMM_DATA_WRITE of time info "
"(AMD guest freq ratio too large) should fail");
}
error = errno;
if (error != EINVAL) {
errx(EXIT_FAILURE, "test_invalid_freq_amd (> max freq) "
"expected EINVAL errno, got %d", error);
}
}
/*
* Test valid AMD-specific frequency ratio requests
*/
static void
test_valid_freq_amd(int vmfd, struct vdi_time_info_v1 *src)
{
struct vdi_time_info_v1 res;
int error;
/* minimum frequency */
struct vdi_time_info_v1 valid = {
.vt_guest_freq = AMD_TSC_MIN_FREQ,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &valid,
};
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
error = errno;
errx(EXIT_FAILURE, "valid VMM_DATA_WRITE of time info "
"(min AMD guest frequency) should succeed, errno=%d",
error);
}
/* verify the frequency was changed */
test_valid_read_time_data(vmfd, &res);
should_eq_u64("vt_guest_freq", res.vt_guest_freq, valid.vt_guest_freq);
/* maximum frequency */
valid.vt_guest_freq = src->vt_guest_freq * AMD_TSC_MAX_FREQ_RATIO - 1;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
error = errno;
errx(EXIT_FAILURE, "valid VMM_DATA_WRITE of time info "
"(max AMD guest frequency) should succeed, errno=%d",
error);
}
/* verify the frequency was changed */
test_valid_read_time_data(vmfd, &res);
should_eq_u64("vt_guest_freq", res.vt_guest_freq, valid.vt_guest_freq);
}
/*
* Test invalid Intel-specific frequency ratio requests
*/
static void
test_invalid_freq_intel(int vmfd, struct vdi_time_info_v1 *src)
{
/*
* As Intel is not currently supported, any frequency that differs from
* the host should be rejected.
*/
struct vdi_time_info_v1 invalid = {
.vt_guest_freq = src->vt_guest_freq + 1,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &invalid,
};
int error;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE, "invalid VMM_DATA_WRITE of time info "
"(intel scaling required) should fail");
}
error = errno;
if (error != EPERM) {
errx(EXIT_FAILURE, "test_invalid_freq_intel: "
"expected EPERM errno, got %d", error);
}
}
/*
* Test that an hrtime from the future is not accepted
*/
static void
test_invalid_host_times(int vmfd, struct vdi_time_info_v1 *src)
{
struct vdi_time_info_v1 invalid = {
.vt_guest_freq = src->vt_guest_freq,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &invalid,
};
int error;
/* hrtime + 500 seconds */
invalid.vt_hrtime += 500000000000;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE, "invalid VMM_DATA_WRITE of time info "
"(hrtime in the future) should fail");
}
error = errno;
if (error != EINVAL) {
errx(EXIT_FAILURE, "test_invalid_host_times: "
"expected EINVAL errno, got %d", error);
}
}
/*
* Test that a boot_hrtime from the future is not accepted
*/
static void
test_invalid_boot_hrtime(int vmfd, struct vdi_time_info_v1 *src)
{
struct vdi_time_info_v1 invalid = {
.vt_guest_freq = src->vt_guest_freq,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &invalid,
};
int error;
/* boot_hrtime = hrtime + 500 seconds */
invalid.vt_boot_hrtime += src->vt_hrtime + 500000000000;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) == 0) {
errx(EXIT_FAILURE, "invalid VMM_DATA_WRITE of time info "
"(boot_hrtime in the future) should fail");
}
error = errno;
if (error != EINVAL) {
errx(EXIT_FAILURE, "test_invalid_boot_hrtime: "
"expected EINVAL errno, got %d", error);
}
}
/*
* Test that a different guest TSC is accepted. There are no constraints on what
* this value can be.
*/
static void
test_valid_guest_tsc(int vmfd, struct vdi_time_info_v1 *src)
{
/* arbitrary guest TSC in the future */
struct vdi_time_info_v1 valid = {
.vt_guest_freq = src->vt_guest_freq,
.vt_guest_tsc = src->vt_guest_tsc + 500000000000,
.vt_boot_hrtime = src->vt_boot_hrtime,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
test_valid_write_time_data(vmfd, &valid);
/* read it back */
struct vdi_time_info_v1 res;
test_valid_read_time_data(vmfd, &res);
/*
* The guest TSC may have been adjusted by the kernel, but it should
* be at least what was supplied.
*/
should_geq_u64("vt_guest_tsc", res.vt_guest_tsc, valid.vt_guest_tsc);
}
/*
* Test that a different boot_hrtime is accepted.
*/
static void
test_valid_boot_hrtime(int vmfd, struct vdi_time_info_v1 *src)
{
struct vdi_time_info_v1 res;
/* boot_hrtime < 0 */
struct vdi_time_info_v1 valid = {
.vt_guest_freq = src->vt_guest_freq,
.vt_guest_tsc = src->vt_guest_tsc,
.vt_boot_hrtime = -100000000000,
.vt_hrtime = src->vt_hrtime,
.vt_hres_sec = src->vt_hres_sec,
.vt_hres_ns = src->vt_hres_ns,
};
test_valid_write_time_data(vmfd, &valid);
/* read it back */
test_valid_read_time_data(vmfd, &res);
/*
* The boot_hrtime may have been adjusted by the kernel, but it should
* be at least what was supplied.
*/
should_geq_i64("boot_hrtime", res.vt_boot_hrtime, valid.vt_boot_hrtime);
/* repeat for boot_hrtime = 0 */
valid.vt_boot_hrtime = 0;
test_valid_write_time_data(vmfd, &valid);
test_valid_read_time_data(vmfd, &res);
should_geq_i64("boot_hrtime", res.vt_boot_hrtime, valid.vt_boot_hrtime);
/* repeat for boot_hrtime > 0 */
valid.vt_boot_hrtime = src->vt_boot_hrtime + 1;
test_valid_write_time_data(vmfd, &valid);
test_valid_read_time_data(vmfd, &res);
should_geq_i64("boot_hrtime", res.vt_boot_hrtime, valid.vt_boot_hrtime);
}
/*
* Coarsely test that interface is making adjustments to the host times and
* guest time values.
*/
static void
test_adjust(int vmfd, struct vdi_time_info_v1 *src)
{
struct vdi_time_info_v1 res;
test_valid_write_time_data(vmfd, src);
/* read it back */
test_valid_read_time_data(vmfd, &res);
/*
* hrtime, hrestime, and guest TSC should all have moved forward
*/
should_geq_i64("vt_hrtime", res.vt_hrtime, src->vt_hrtime);
if (src->vt_hres_sec == res.vt_hres_sec) {
/* ns should be higher */
should_geq_u64("vt_hres_ns", res.vt_hres_ns, src->vt_hres_ns);
} else if (src->vt_hres_sec > res.vt_hres_sec) {
errx(EXIT_FAILURE, "test_adjust: hrestime went backwards");
}
should_geq_u64("vt_guest_tsc", res.vt_guest_tsc, src->vt_guest_tsc);
}
int
main(int argc, char *argv[])
{
const char *suite_name = basename(argv[0]);
struct vmctx *ctx;
struct vcpu *vcpu;
ctx = create_test_vm(suite_name);
if (ctx == NULL) {
errx(EXIT_FAILURE, "could not open test VM");
}
if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
err(EXIT_FAILURE, "Could not open vcpu0");
}
if (vm_activate_cpu(vcpu) != 0) {
err(EXIT_FAILURE, "could not activate vcpu0");
}
const int vmfd = vm_get_device_fd(ctx);
const bool is_svm = cpu_vendor_amd();
struct vdi_time_info_v1 time_info;
/*
* Reads
*/
/* do a valid read */
test_valid_read_time_data(vmfd, &time_info);
/* malformed read request */
test_invalid_read_time_data(vmfd);
/*
* Writes
*
* For the test writes, we reuse the data from the successful read,
* and change the request parameters as necessary to test specific
* behavior. This is sufficient for testing validation of individual
* parameters, as writing the exact data back from a read is allowed.
*
* The only platform-specific behavior is around changing the guest
* TSC frequency. If the guest frequency is the same as the host's,
* as it is for all VMs at boot, then no scaling is required, and thus
* the CPU vendor of the system, or its capability to scale a guest TSC,
* does not matter.
*/
/* try writing back the data from the read */
test_valid_write_time_data(vmfd, &time_info);
/* malformed write request */
test_invalid_write_time_data(vmfd, &time_info);
/* invalid host time requests */
test_invalid_host_times(vmfd, &time_info);
/* invalid guest frequency requests */
test_invalid_freq(vmfd, &time_info);
if (is_svm) {
test_invalid_freq_amd(vmfd, &time_info);
} else {
test_invalid_freq_intel(vmfd, &time_info);
}
/* invalid boot_hrtime request */
test_invalid_boot_hrtime(vmfd, &time_info);
/* valid frequency scaling requests */
if (is_svm) {
test_valid_freq_amd(vmfd, &time_info);
}
/* valid guest TSC values */
test_valid_guest_tsc(vmfd, &time_info);
/* valid boot_hrtime values */
test_valid_boot_hrtime(vmfd, &time_info);
/* observe that host times and guest data are updated after a write */
test_adjust(vmfd, &time_info);
vm_vcpu_close(vcpu);
vm_destroy(ctx);
(void) printf("%s\tPASS\n", suite_name);
return (EXIT_SUCCESS);
}
|