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