nvmmctl.c revision 1.1.6.2 1 /* $NetBSD: nvmmctl.c,v 1.1.6.2 2020/04/13 08:05:56 martin Exp $ */
2
3 /*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Maxime Villard.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: nvmmctl.c,v 1.1.6.2 2020/04/13 08:05:56 martin Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdbool.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <time.h>
49 #include <util.h>
50 #include <nvmm.h>
51
52 #include <x86/specialreg.h>
53
54 __dead static void usage(void);
55 static void nvmm_identify(char **);
56 static void nvmm_list(char **);
57
58 static struct cmdtab {
59 const char *label;
60 bool takesargs;
61 bool argsoptional;
62 void (*func)(char **);
63 } const nvmm_cmdtab[] = {
64 { "identify", false, false, nvmm_identify },
65 { "list", false, false, nvmm_list },
66 { NULL, false, false, NULL },
67 };
68
69 static struct nvmm_capability cap;
70
71 int
72 main(int argc, char **argv)
73 {
74 const struct cmdtab *ct;
75
76 argc -= 1;
77 argv += 1;
78 if (argc < 1)
79 usage();
80
81 for (ct = nvmm_cmdtab; ct->label != NULL; ct++) {
82 if (strcmp(argv[0], ct->label) == 0) {
83 if (!ct->argsoptional &&
84 ((ct->takesargs == 0) ^ (argv[1] == NULL)))
85 {
86 usage();
87 }
88 (*ct->func)(argv + 1);
89 break;
90 }
91 }
92
93 if (ct->label == NULL)
94 errx(EXIT_FAILURE, "unknown command ``%s''", argv[0]);
95
96 exit(EXIT_SUCCESS);
97 /* NOTREACHED */
98 }
99
100 static void
101 usage(void)
102 {
103 const char *progname = getprogname();
104
105 fprintf(stderr, "usage: %s identify\n", progname);
106 fprintf(stderr, " %s list\n", progname);
107 exit(EXIT_FAILURE);
108 /* NOTREACHED */
109 }
110
111 #define MACH_CONF_FLAGS "\20"
112 #define VCPU_CONF_FLAGS "\20" "\1" "CPUID" "\2" "TPR"
113
114 static void
115 nvmm_identify(char **argv)
116 {
117 char buf[256], ram[4+1];
118
119 if (nvmm_init() == -1)
120 err(EXIT_FAILURE, "nvmm_init failed");
121 if (nvmm_capability(&cap) == -1)
122 err(EXIT_FAILURE, "nvmm_capability failed");
123
124 printf("nvmm: Kernel API version %u\n", cap.version);
125 printf("nvmm: State size %u\n", cap.state_size);
126 printf("nvmm: Max machines %u\n", cap.max_machines);
127 printf("nvmm: Max VCPUs per machine %u\n", cap.max_vcpus);
128
129 if (humanize_number(ram, sizeof(ram), cap.max_ram, NULL, HN_AUTOSCALE,
130 (HN_DECIMAL | HN_B | HN_NOSPACE)) == -1)
131 err(EXIT_FAILURE, "humanize_number");
132 printf("nvmm: Max RAM per machine %s\n", ram);
133
134 snprintb(buf, sizeof(buf), MACH_CONF_FLAGS, cap.arch.mach_conf_support);
135 printf("nvmm: Arch Mach conf %s\n", buf);
136
137 snprintb(buf, sizeof(buf), VCPU_CONF_FLAGS, cap.arch.vcpu_conf_support);
138 printf("nvmm: Arch VCPU conf %s\n", buf);
139
140 snprintb(buf, sizeof(buf), XCR0_FLAGS1, cap.arch.xcr0_mask);
141 printf("nvmm: Guest FPU states %s\n", buf);
142 }
143
144 static void
145 nvmm_list(char **argv)
146 {
147 struct nvmm_ctl_mach_info machinfo;
148 char ram[4+1], *ts;
149 size_t i;
150 int ret;
151
152 if (nvmm_root_init() == -1)
153 err(EXIT_FAILURE, "nvmm_root_init failed");
154 if (nvmm_capability(&cap) == -1)
155 err(EXIT_FAILURE, "nvmm_capability failed");
156
157 printf(
158 "Machine ID VCPUs RAM Owner PID Creation Time \n"
159 "---------- ----- ---- --------- ------------------------\n");
160
161 for (i = 0; i < cap.max_machines; i++) {
162 machinfo.machid = i;
163 ret = nvmm_ctl(NVMM_CTL_MACH_INFO, &machinfo, sizeof(machinfo));
164 if (ret == -1) {
165 if (errno == ENOENT)
166 continue;
167 err(EXIT_FAILURE, "nvmm_ctl failed");
168 }
169
170 ts = asctime(localtime(&machinfo.time));
171 ts[strlen(ts) - 1] = '\0';
172
173 if (humanize_number(ram, sizeof(ram), machinfo.nram, NULL,
174 HN_AUTOSCALE, (HN_DECIMAL | HN_B | HN_NOSPACE)) == -1)
175 err(EXIT_FAILURE, "humanize_number");
176
177 printf("%-10zu %-5u %-4s %-9d %s\n", i, machinfo.nvcpus, ram,
178 machinfo.pid, ts);
179 }
180 }
181