xref: /illumos-gate/usr/src/test/bhyve-tests/tests/vmm/pause_resume.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 int
36 main(int argc, char *argv[])
37 {
38 	const char *suite_name = basename(argv[0]);
39 	struct vmctx *ctx;
40 
41 	ctx = create_test_vm(suite_name);
42 	if (ctx == NULL) {
43 		errx(EXIT_FAILURE, "could not open test VM");
44 	}
45 
46 	if (vm_activate_cpu(ctx, 0) != 0) {
47 		err(EXIT_FAILURE, "could not activate vcpu0");
48 	}
49 
50 	const int vmfd = vm_get_device_fd(ctx);
51 	int error;
52 
53 	if (ioctl(vmfd, VM_PAUSE, 0) != 0) {
54 		err(EXIT_FAILURE, "VM_PAUSE failed");
55 	}
56 
57 	/* Pausing an already-paused instanced should result in EALREADY */
58 	if (ioctl(vmfd, VM_PAUSE, 0) == 0) {
59 		errx(EXIT_FAILURE, "VM_PAUSE should have failed");
60 	}
61 	error = errno;
62 	if (error != EALREADY) {
63 		errx(EXIT_FAILURE, "VM_PAUSE unexpected errno: %d != %d",
64 		    EALREADY, error);
65 	}
66 
67 	if (ioctl(vmfd, VM_RESUME, 0) != 0) {
68 		err(EXIT_FAILURE, "VM_RESUME failed");
69 	}
70 
71 	/* Resuming an already-running instanced should result in EALREADY */
72 	if (ioctl(vmfd, VM_RESUME, 0) == 0) {
73 		errx(EXIT_FAILURE, "VM_RESUME should have failed");
74 	}
75 	error = errno;
76 	if (error != EALREADY) {
77 		errx(EXIT_FAILURE, "VM_RESUME unexpected errno: %d != %d",
78 		    EALREADY, error);
79 	}
80 
81 	vm_destroy(ctx);
82 	(void) printf("%s\tPASS\n", suite_name);
83 	return (EXIT_SUCCESS);
84 }
85