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