Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/bhyvectl/bhyvectl.c
Show All 32 Lines | |||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
#include <sys/errno.h> | #include <sys/errno.h> | ||||
#include <sys/mman.h> | #include <sys/mman.h> | ||||
#include <sys/cpuset.h> | #include <sys/cpuset.h> | ||||
#include <dirent.h> | |||||
#include <err.h> | |||||
#include <errno.h> | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <libgen.h> | #include <libgen.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
Show All 24 Lines | |||||
usage(bool cpu_intel) | usage(bool cpu_intel) | ||||
{ | { | ||||
(void)fprintf(stderr, | (void)fprintf(stderr, | ||||
"Usage: %s --vm=<vmname>\n" | "Usage: %s --vm=<vmname>\n" | ||||
" [--cpu=<vcpu_number>]\n" | " [--cpu=<vcpu_number>]\n" | ||||
" [--create]\n" | " [--create]\n" | ||||
" [--destroy]\n" | " [--destroy]\n" | ||||
" [--list]\n" | |||||
" [--get-all]\n" | " [--get-all]\n" | ||||
" [--get-stats]\n" | " [--get-stats]\n" | ||||
" [--set-desc-ds]\n" | " [--set-desc-ds]\n" | ||||
" [--get-desc-ds]\n" | " [--get-desc-ds]\n" | ||||
" [--set-desc-es]\n" | " [--set-desc-es]\n" | ||||
" [--get-desc-es]\n" | " [--get-desc-es]\n" | ||||
" [--set-desc-gs]\n" | " [--set-desc-gs]\n" | ||||
" [--get-desc-gs]\n" | " [--get-desc-gs]\n" | ||||
▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | |||||
static int rtc_nvram_offset; | static int rtc_nvram_offset; | ||||
static uint8_t rtc_nvram_value; | static uint8_t rtc_nvram_value; | ||||
static time_t rtc_secs; | static time_t rtc_secs; | ||||
static int get_stats, getcap, setcap, capval, get_gpa_pmap; | static int get_stats, getcap, setcap, capval, get_gpa_pmap; | ||||
static int inject_nmi, assert_lapic_lvt; | static int inject_nmi, assert_lapic_lvt; | ||||
static int force_reset, force_poweroff; | static int force_reset, force_poweroff; | ||||
static const char *capname; | static const char *capname; | ||||
static int create, destroy, get_memmap, get_memseg; | static int create, destroy, list, get_memmap, get_memseg; | ||||
static int get_intinfo; | static int get_intinfo; | ||||
static int get_active_cpus, get_suspended_cpus; | static int get_active_cpus, get_suspended_cpus; | ||||
static uint64_t memsize; | static uint64_t memsize; | ||||
static int set_cr0, get_cr0, set_cr2, get_cr2, set_cr3, get_cr3; | static int set_cr0, get_cr0, set_cr2, get_cr2, set_cr3, get_cr3; | ||||
static int set_cr4, get_cr4; | static int set_cr4, get_cr4; | ||||
static int set_efer, get_efer; | static int set_efer, get_efer; | ||||
static int set_dr0, get_dr0; | static int set_dr0, get_dr0; | ||||
static int set_dr1, get_dr1; | static int set_dr1, get_dr1; | ||||
▲ Show 20 Lines • Show All 342 Lines • ▼ Show 20 Lines | for (i = 0; i < CPU_SETSIZE; i++) { | ||||
first = 0; | first = 0; | ||||
} | } | ||||
} | } | ||||
} else | } else | ||||
printf(" (none)"); | printf(" (none)"); | ||||
printf("\n"); | printf("\n"); | ||||
} | } | ||||
/* | |||||
* Parse the number of CPU specified to a VM and | |||||
* return the total number of it. | |||||
*/ | |||||
static int | |||||
count_vcpus(const cpuset_t *cpus) | |||||
{ | |||||
int i, count = 0; | |||||
if (!CPU_EMPTY(cpus)) { | |||||
for (i = 0; i < CPU_SETSIZE; i++) { | |||||
if (CPU_ISSET(count, cpus)) | |||||
count++; | |||||
} | |||||
} | |||||
return count; | |||||
} | |||||
static void | static void | ||||
print_intinfo(const char *banner, uint64_t info) | print_intinfo(const char *banner, uint64_t info) | ||||
{ | { | ||||
int type; | int type; | ||||
printf("%s:\t", banner); | printf("%s:\t", banner); | ||||
if (info & VM_INTINFO_VALID) { | if (info & VM_INTINFO_VALID) { | ||||
type = info & VM_INTINFO_TYPE; | type = info & VM_INTINFO_TYPE; | ||||
▲ Show 20 Lines • Show All 824 Lines • ▼ Show 20 Lines | const struct option common_opts[] = { | ||||
NO_ARG, &get_guest_sysenter, 1 }, | NO_ARG, &get_guest_sysenter, 1 }, | ||||
{ "get-exit-reason", | { "get-exit-reason", | ||||
NO_ARG, &get_exit_reason, 1 }, | NO_ARG, &get_exit_reason, 1 }, | ||||
{ "get-x2apic-state", NO_ARG, &get_x2apic_state, 1 }, | { "get-x2apic-state", NO_ARG, &get_x2apic_state, 1 }, | ||||
{ "get-all", NO_ARG, &get_all, 1 }, | { "get-all", NO_ARG, &get_all, 1 }, | ||||
{ "run", NO_ARG, &run, 1 }, | { "run", NO_ARG, &run, 1 }, | ||||
{ "create", NO_ARG, &create, 1 }, | { "create", NO_ARG, &create, 1 }, | ||||
{ "destroy", NO_ARG, &destroy, 1 }, | { "destroy", NO_ARG, &destroy, 1 }, | ||||
{ "list", NO_ARG, &list, 1}, | |||||
{ "inject-nmi", NO_ARG, &inject_nmi, 1 }, | { "inject-nmi", NO_ARG, &inject_nmi, 1 }, | ||||
{ "force-reset", NO_ARG, &force_reset, 1 }, | { "force-reset", NO_ARG, &force_reset, 1 }, | ||||
{ "force-poweroff", NO_ARG, &force_poweroff, 1 }, | { "force-poweroff", NO_ARG, &force_poweroff, 1 }, | ||||
{ "get-active-cpus", NO_ARG, &get_active_cpus, 1 }, | { "get-active-cpus", NO_ARG, &get_active_cpus, 1 }, | ||||
{ "get-suspended-cpus", NO_ARG, &get_suspended_cpus, 1 }, | { "get-suspended-cpus", NO_ARG, &get_suspended_cpus, 1 }, | ||||
{ "get-intinfo", NO_ARG, &get_intinfo, 1 }, | { "get-intinfo", NO_ARG, &get_intinfo, 1 }, | ||||
{ "get-cpu-topology", NO_ARG, &get_cpu_topology, 1 }, | { "get-cpu-topology", NO_ARG, &get_cpu_topology, 1 }, | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 132 Lines • ▼ Show 20 Lines | mon_str(int idx) | ||||
}; | }; | ||||
if (idx >= 0 && idx < 12) | if (idx >= 0 && idx < 12) | ||||
return (months[idx]); | return (months[idx]); | ||||
else | else | ||||
return ("UNK"); | return ("UNK"); | ||||
} | } | ||||
static void | |||||
list_vm(char *vmname) | |||||
{ | |||||
DIR *path_vmm; | |||||
struct dirent *dir; | |||||
struct vmctx *ctx; | |||||
int vcpus = 0; | |||||
cpuset_t cpus; | |||||
char numbuf[8]; | |||||
vm_paddr_t gpa; | |||||
vm_ooffset_t segoff; | |||||
int segid, flags, prot; | |||||
size_t maplen; | |||||
printf("NAME\t\t MEM\t\t\t VCPU\n"); | |||||
path_vmm = opendir("/dev/vmm"); | |||||
if (path_vmm) { | |||||
while ((dir = readdir(path_vmm)) != NULL) { | |||||
gpa = 0; | |||||
if (dir->d_type != DT_DIR) { | |||||
ctx = vm_open(dir->d_name); | |||||
rgrimes: You could collapse that to a simpler dir->d_type != DT_DIR and save yourself from errors if at… | |||||
if (ctx == NULL) | |||||
Not Done Inline ActionsI need to look at this, but this would not list a vm I named bootrom, I am not sure that there is any protection from creating such a vm name. rgrimes: I need to look at this, but this would not list a vm I named bootrom, I am not sure that there… | |||||
errx(EPERM, "You have no permission to list vms.\n"); | |||||
errno = vm_mmap_getnext(ctx, &gpa, &segid, &segoff, &maplen, | |||||
&prot, &flags); | |||||
if (errno) | |||||
errx(EPERM, "Error to get memory segment information.\n"); | |||||
humanize_number(numbuf, sizeof(numbuf), maplen, "B", | |||||
HN_AUTOSCALE, HN_NOSPACE); | |||||
errno = vm_active_cpus(ctx, &cpus); | |||||
if (!errno) | |||||
vcpus = count_vcpus(&cpus); | |||||
else | |||||
errx(EPERM, "Error to get the number of vcpus.\n"); | |||||
if (vmname && strcmp(vmname, dir->d_name) == 0) { | |||||
printf("%s\t\t %s\t\t\t %d\n", dir->d_name, | |||||
numbuf, vcpus); | |||||
break; | |||||
} else if (!vmname) | |||||
printf("%s\t\t %s\t\t\t %d\n", dir->d_name, | |||||
numbuf, vcpus); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
static int | static int | ||||
show_memmap(struct vmctx *ctx) | show_memmap(struct vmctx *ctx) | ||||
{ | { | ||||
char name[SPECNAMELEN + 1], numbuf[8]; | char name[SPECNAMELEN + 1], numbuf[8]; | ||||
vm_ooffset_t segoff; | vm_ooffset_t segoff; | ||||
vm_paddr_t gpa; | vm_paddr_t gpa; | ||||
size_t maplen, seglen; | size_t maplen, seglen; | ||||
int error, flags, prot, segid, delim; | int error, flags, prot, segid, delim; | ||||
▲ Show 20 Lines • Show All 248 Lines • ▼ Show 20 Lines | case ASSERT_LAPIC_LVT: | ||||
assert_lapic_lvt = atoi(optarg); | assert_lapic_lvt = atoi(optarg); | ||||
break; | break; | ||||
default: | default: | ||||
usage(cpu_intel); | usage(cpu_intel); | ||||
} | } | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
if (list) { | |||||
if (vmname) | |||||
list_vm(vmname); | |||||
else | |||||
list_vm(NULL); | |||||
exit (0); | |||||
} | |||||
if (vmname == NULL) | if (vmname == NULL) | ||||
usage(cpu_intel); | usage(cpu_intel); | ||||
error = 0; | error = 0; | ||||
if (!error && create) | if (!error && create) | ||||
error = vm_create(vmname); | error = vm_create(vmname); | ||||
if (!error) { | if (!error) { | ||||
ctx = vm_open(vmname); | ctx = vm_open(vmname); | ||||
Not Done Inline ActionsIt looks like this is not reached if (list) so !list would always be true, so I believe you can remove the && !list rgrimes: It looks like this is not reached if (list) so !list would always be true, so I believe you can… | |||||
if (ctx == NULL) { | if (ctx == NULL) { | ||||
printf("VM:%s is not created.\n", vmname); | printf("VM:%s is not created.\n", vmname); | ||||
exit (1); | exit (1); | ||||
} | } | ||||
} | } | ||||
if (!error && memsize) | if (!error && memsize) | ||||
error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); | error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); | ||||
▲ Show 20 Lines • Show All 463 Lines • Show Last 20 Lines |
You could collapse that to a simpler dir->d_type != DT_DIR and save yourself from errors if at some point a subdirectory is created by vmm.ko in this name space.