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