1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/cpuset.h>
33 #include <sys/errno.h>
34 #include <sys/mman.h>
35 #include <sys/nv.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/un.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stdbool.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <libgen.h>
46 #include <libutil.h>
47 #include <fcntl.h>
48 #include <getopt.h>
49 #include <time.h>
50 #include <assert.h>
51 #include <libutil.h>
52
53 #include <machine/cpufunc.h>
54 #include <machine/specialreg.h>
55 #include <machine/vmm.h>
56 #include <machine/vmm_dev.h>
57 #include <vmmapi.h>
58
59 #include "amd/vmcb.h"
60 #include "intel/vmcs.h"
61
62 #ifdef BHYVE_SNAPSHOT
63 #include "snapshot.h"
64 #endif
65
66 #define MB (1UL << 20)
67 #define GB (1UL << 30)
68
69 #define REQ_ARG required_argument
70 #define NO_ARG no_argument
71 #define OPT_ARG optional_argument
72
73 static const char *progname;
74
75 static int get_rtc_time, set_rtc_time;
76 static int get_rtc_nvram, set_rtc_nvram;
77 static int rtc_nvram_offset;
78 static uint8_t rtc_nvram_value;
79 static time_t rtc_secs;
80
81 static int get_stats, getcap, setcap, capval, get_gpa_pmap;
82 static int inject_nmi, assert_lapic_lvt;
83 static int force_reset, force_poweroff;
84 static const char *capname;
85 static int create, destroy, get_memmap, get_memseg;
86 static int get_intinfo;
87 static int get_active_cpus, get_debug_cpus, get_suspended_cpus;
88 static uint64_t memsize;
89 static int set_cr0, get_cr0, set_cr2, get_cr2, set_cr3, get_cr3;
90 static int set_cr4, get_cr4;
91 static int set_efer, get_efer;
92 static int set_dr0, get_dr0;
93 static int set_dr1, get_dr1;
94 static int set_dr2, get_dr2;
95 static int set_dr3, get_dr3;
96 static int set_dr6, get_dr6;
97 static int set_dr7, get_dr7;
98 static int set_rsp, get_rsp, set_rip, get_rip, set_rflags, get_rflags;
99 static int set_rax, get_rax;
100 static int get_rbx, get_rcx, get_rdx, get_rsi, get_rdi, get_rbp;
101 static int get_r8, get_r9, get_r10, get_r11, get_r12, get_r13, get_r14, get_r15;
102 static int set_desc_ds, get_desc_ds;
103 static int set_desc_es, get_desc_es;
104 static int set_desc_fs, get_desc_fs;
105 static int set_desc_gs, get_desc_gs;
106 static int set_desc_cs, get_desc_cs;
107 static int set_desc_ss, get_desc_ss;
108 static int set_desc_gdtr, get_desc_gdtr;
109 static int set_desc_idtr, get_desc_idtr;
110 static int set_desc_tr, get_desc_tr;
111 static int set_desc_ldtr, get_desc_ldtr;
112 static int set_cs, set_ds, set_es, set_fs, set_gs, set_ss, set_tr, set_ldtr;
113 static int get_cs, get_ds, get_es, get_fs, get_gs, get_ss, get_tr, get_ldtr;
114 static int set_x2apic_state, get_x2apic_state;
115 static enum x2apic_state x2apic_state;
116 static int unassign_pptdev, bus, slot, func;
117 static int run;
118 static int get_cpu_topology;
119 #ifdef BHYVE_SNAPSHOT
120 static int vm_suspend_opt;
121 #endif
122
123 /*
124 * VMCB specific.
125 */
126 static int get_vmcb_intercept, get_vmcb_exit_details, get_vmcb_tlb_ctrl;
127 static int get_vmcb_virq, get_avic_table;
128
129 /*
130 * VMCS-specific fields
131 */
132 static int get_pinbased_ctls, get_procbased_ctls, get_procbased_ctls2;
133 static int get_eptp, get_io_bitmap, get_tsc_offset;
134 static int get_vmcs_entry_interruption_info;
135 static int get_vmcs_interruptibility;
136 static int get_vmcs_gpa, get_vmcs_gla;
137 static int get_exception_bitmap;
138 static int get_cr0_mask, get_cr0_shadow;
139 static int get_cr4_mask, get_cr4_shadow;
140 static int get_cr3_targets;
141 static int get_apic_access_addr, get_virtual_apic_addr, get_tpr_threshold;
142 static int get_msr_bitmap, get_msr_bitmap_address;
143 static int get_vpid_asid;
144 static int get_inst_err, get_exit_ctls, get_entry_ctls;
145 static int get_host_cr0, get_host_cr3, get_host_cr4;
146 static int get_host_rip, get_host_rsp;
147 static int get_guest_pat, get_host_pat;
148 static int get_guest_sysenter, get_vmcs_link;
149 static int get_exit_reason, get_vmcs_exit_qualification;
150 static int get_vmcs_exit_interruption_info, get_vmcs_exit_interruption_error;
151 static int get_vmcs_exit_inst_length;
152
153 static uint64_t desc_base;
154 static uint32_t desc_limit, desc_access;
155
156 static int get_all;
157
158 static void
dump_vm_run_exitcode(struct vm_exit * vmexit,int vcpu)159 dump_vm_run_exitcode(struct vm_exit *vmexit, int vcpu)
160 {
161 printf("vm exit[%d]\n", vcpu);
162 printf("\trip\t\t0x%016lx\n", vmexit->rip);
163 printf("\tinst_length\t%d\n", vmexit->inst_length);
164 switch (vmexit->exitcode) {
165 case VM_EXITCODE_INOUT:
166 printf("\treason\t\tINOUT\n");
167 printf("\tdirection\t%s\n", vmexit->u.inout.in ? "IN" : "OUT");
168 printf("\tbytes\t\t%d\n", vmexit->u.inout.bytes);
169 printf("\tflags\t\t%s%s\n",
170 vmexit->u.inout.string ? "STRING " : "",
171 vmexit->u.inout.rep ? "REP " : "");
172 printf("\tport\t\t0x%04x\n", vmexit->u.inout.port);
173 printf("\teax\t\t0x%08x\n", vmexit->u.inout.eax);
174 break;
175 case VM_EXITCODE_VMX:
176 printf("\treason\t\tVMX\n");
177 printf("\tstatus\t\t%d\n", vmexit->u.vmx.status);
178 printf("\texit_reason\t0x%08x (%u)\n",
179 vmexit->u.vmx.exit_reason, vmexit->u.vmx.exit_reason);
180 printf("\tqualification\t0x%016lx\n",
181 vmexit->u.vmx.exit_qualification);
182 printf("\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
183 printf("\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
184 break;
185 case VM_EXITCODE_SVM:
186 printf("\treason\t\tSVM\n");
187 printf("\texit_reason\t\t%#lx\n", vmexit->u.svm.exitcode);
188 printf("\texitinfo1\t\t%#lx\n", vmexit->u.svm.exitinfo1);
189 printf("\texitinfo2\t\t%#lx\n", vmexit->u.svm.exitinfo2);
190 break;
191 default:
192 printf("*** unknown vm run exitcode %d\n", vmexit->exitcode);
193 break;
194 }
195 }
196
197 /* AMD 6th generation and Intel compatible MSRs */
198 #define MSR_AMD6TH_START 0xC0000000
199 #define MSR_AMD6TH_END 0xC0001FFF
200 /* AMD 7th and 8th generation compatible MSRs */
201 #define MSR_AMD7TH_START 0xC0010000
202 #define MSR_AMD7TH_END 0xC0011FFF
203
204 static const char *
msr_name(uint32_t msr)205 msr_name(uint32_t msr)
206 {
207 static char buf[32];
208
209 switch(msr) {
210 case MSR_TSC:
211 return ("MSR_TSC");
212 case MSR_EFER:
213 return ("MSR_EFER");
214 case MSR_STAR:
215 return ("MSR_STAR");
216 case MSR_LSTAR:
217 return ("MSR_LSTAR");
218 case MSR_CSTAR:
219 return ("MSR_CSTAR");
220 case MSR_SF_MASK:
221 return ("MSR_SF_MASK");
222 case MSR_FSBASE:
223 return ("MSR_FSBASE");
224 case MSR_GSBASE:
225 return ("MSR_GSBASE");
226 case MSR_KGSBASE:
227 return ("MSR_KGSBASE");
228 case MSR_SYSENTER_CS_MSR:
229 return ("MSR_SYSENTER_CS_MSR");
230 case MSR_SYSENTER_ESP_MSR:
231 return ("MSR_SYSENTER_ESP_MSR");
232 case MSR_SYSENTER_EIP_MSR:
233 return ("MSR_SYSENTER_EIP_MSR");
234 case MSR_PAT:
235 return ("MSR_PAT");
236 }
237 snprintf(buf, sizeof(buf), "MSR %#08x", msr);
238
239 return (buf);
240 }
241
242 static inline void
print_msr_pm(uint64_t msr,int vcpu,int readable,int writeable)243 print_msr_pm(uint64_t msr, int vcpu, int readable, int writeable)
244 {
245
246 if (readable || writeable) {
247 printf("%-20s[%d]\t\t%c%c\n", msr_name(msr), vcpu,
248 readable ? 'R' : '-', writeable ? 'W' : '-');
249 }
250 }
251
252 /*
253 * Reference APM vol2, section 15.11 MSR Intercepts.
254 */
255 static void
dump_amd_msr_pm(const char * bitmap,int vcpu)256 dump_amd_msr_pm(const char *bitmap, int vcpu)
257 {
258 int byte, bit, readable, writeable;
259 uint32_t msr;
260
261 for (msr = 0; msr < 0x2000; msr++) {
262 byte = msr / 4;
263 bit = (msr % 4) * 2;
264
265 /* Look at MSRs in the range 0x00000000 to 0x00001FFF */
266 readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
267 writeable = (bitmap[byte] & (2 << bit)) ? 0 : 1;
268 print_msr_pm(msr, vcpu, readable, writeable);
269
270 /* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */
271 byte += 2048;
272 readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
273 writeable = (bitmap[byte] & (2 << bit)) ? 0 : 1;
274 print_msr_pm(msr + MSR_AMD6TH_START, vcpu, readable,
275 writeable);
276
277 /* MSR 0xC0010000 to 0xC0011FF is only for AMD */
278 byte += 4096;
279 readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
280 writeable = (bitmap[byte] & (2 << bit)) ? 0 : 1;
281 print_msr_pm(msr + MSR_AMD7TH_START, vcpu, readable,
282 writeable);
283 }
284 }
285
286 /*
287 * Reference Intel SDM Vol3 Section 24.6.9 MSR-Bitmap Address
288 */
289 static void
dump_intel_msr_pm(const char * bitmap,int vcpu)290 dump_intel_msr_pm(const char *bitmap, int vcpu)
291 {
292 int byte, bit, readable, writeable;
293 uint32_t msr;
294
295 for (msr = 0; msr < 0x2000; msr++) {
296 byte = msr / 8;
297 bit = msr & 0x7;
298
299 /* Look at MSRs in the range 0x00000000 to 0x00001FFF */
300 readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
301 writeable = (bitmap[2048 + byte] & (1 << bit)) ? 0 : 1;
302 print_msr_pm(msr, vcpu, readable, writeable);
303
304 /* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */
305 byte += 1024;
306 readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
307 writeable = (bitmap[2048 + byte] & (1 << bit)) ? 0 : 1;
308 print_msr_pm(msr + MSR_AMD6TH_START, vcpu, readable,
309 writeable);
310 }
311 }
312
313 static int
dump_msr_bitmap(int vcpu,uint64_t addr,bool cpu_intel)314 dump_msr_bitmap(int vcpu, uint64_t addr, bool cpu_intel)
315 {
316 char *bitmap;
317 int error, fd, map_size;
318
319 error = -1;
320 bitmap = MAP_FAILED;
321
322 fd = open("/dev/mem", O_RDONLY, 0);
323 if (fd < 0) {
324 perror("Couldn't open /dev/mem");
325 goto done;
326 }
327
328 if (cpu_intel)
329 map_size = PAGE_SIZE;
330 else
331 map_size = 2 * PAGE_SIZE;
332
333 bitmap = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, addr);
334 if (bitmap == MAP_FAILED) {
335 perror("mmap failed");
336 goto done;
337 }
338
339 if (cpu_intel)
340 dump_intel_msr_pm(bitmap, vcpu);
341 else
342 dump_amd_msr_pm(bitmap, vcpu);
343
344 error = 0;
345 done:
346 if (bitmap != MAP_FAILED)
347 munmap((void *)bitmap, map_size);
348 if (fd >= 0)
349 close(fd);
350
351 return (error);
352 }
353
354 static int
vm_get_vmcs_field(struct vcpu * vcpu,int field,uint64_t * ret_val)355 vm_get_vmcs_field(struct vcpu *vcpu, int field, uint64_t *ret_val)
356 {
357
358 return (vm_get_register(vcpu, VMCS_IDENT(field), ret_val));
359 }
360
361 static int
vm_get_vmcb_field(struct vcpu * vcpu,int off,int bytes,uint64_t * ret_val)362 vm_get_vmcb_field(struct vcpu *vcpu, int off, int bytes,
363 uint64_t *ret_val)
364 {
365
366 return (vm_get_register(vcpu, VMCB_ACCESS(off, bytes), ret_val));
367 }
368
369 enum {
370 VMNAME = 1000, /* avoid collision with return values from getopt */
371 VCPU,
372 SET_MEM,
373 SET_EFER,
374 SET_CR0,
375 SET_CR2,
376 SET_CR3,
377 SET_CR4,
378 SET_DR0,
379 SET_DR1,
380 SET_DR2,
381 SET_DR3,
382 SET_DR6,
383 SET_DR7,
384 SET_RSP,
385 SET_RIP,
386 SET_RAX,
387 SET_RFLAGS,
388 DESC_BASE,
389 DESC_LIMIT,
390 DESC_ACCESS,
391 SET_CS,
392 SET_DS,
393 SET_ES,
394 SET_FS,
395 SET_GS,
396 SET_SS,
397 SET_TR,
398 SET_LDTR,
399 SET_X2APIC_STATE,
400 SET_CAP,
401 CAPNAME,
402 UNASSIGN_PPTDEV,
403 GET_GPA_PMAP,
404 ASSERT_LAPIC_LVT,
405 SET_RTC_TIME,
406 SET_RTC_NVRAM,
407 RTC_NVRAM_OFFSET,
408 #ifdef BHYVE_SNAPSHOT
409 SET_CHECKPOINT_FILE,
410 SET_SUSPEND_FILE,
411 #endif
412 };
413
414 static void
print_cpus(const char * banner,const cpuset_t * cpus)415 print_cpus(const char *banner, const cpuset_t *cpus)
416 {
417 int i, first;
418
419 first = 1;
420 printf("%s:\t", banner);
421 if (!CPU_EMPTY(cpus)) {
422 for (i = 0; i < CPU_SETSIZE; i++) {
423 if (CPU_ISSET(i, cpus)) {
424 printf("%s%d", first ? " " : ", ", i);
425 first = 0;
426 }
427 }
428 } else
429 printf(" (none)");
430 printf("\n");
431 }
432
433 static void
print_intinfo(const char * banner,uint64_t info)434 print_intinfo(const char *banner, uint64_t info)
435 {
436 int type;
437
438 printf("%s:\t", banner);
439 if (info & VM_INTINFO_VALID) {
440 type = info & VM_INTINFO_TYPE;
441 switch (type) {
442 case VM_INTINFO_HWINTR:
443 printf("extint");
444 break;
445 case VM_INTINFO_NMI:
446 printf("nmi");
447 break;
448 case VM_INTINFO_SWINTR:
449 printf("swint");
450 break;
451 default:
452 printf("exception");
453 break;
454 }
455 printf(" vector %d", (int)VM_INTINFO_VECTOR(info));
456 if (info & VM_INTINFO_DEL_ERRCODE)
457 printf(" errcode %#x", (u_int)(info >> 32));
458 } else {
459 printf("n/a");
460 }
461 printf("\n");
462 }
463
464 static bool
cpu_vendor_intel(void)465 cpu_vendor_intel(void)
466 {
467 u_int regs[4], v[3];
468
469 do_cpuid(0, regs);
470 v[0] = regs[1];
471 v[1] = regs[3];
472 v[2] = regs[2];
473
474 if (memcmp(v, "GenuineIntel", sizeof(v)) == 0)
475 return (true);
476 if (memcmp(v, "AuthenticAMD", sizeof(v)) == 0 ||
477 memcmp(v, "HygonGenuine", sizeof(v)) == 0)
478 return (false);
479 fprintf(stderr, "Unknown cpu vendor \"%s\"\n", (const char *)v);
480 exit(1);
481 }
482
483 static int
get_all_registers(struct vcpu * vcpu,int vcpuid)484 get_all_registers(struct vcpu *vcpu, int vcpuid)
485 {
486 uint64_t cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
487 uint64_t rsp, rip, rflags, efer;
488 uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp;
489 uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
490 int error = 0;
491
492 if (!error && (get_efer || get_all)) {
493 error = vm_get_register(vcpu, VM_REG_GUEST_EFER, &efer);
494 if (error == 0)
495 printf("efer[%d]\t\t0x%016lx\n", vcpuid, efer);
496 }
497
498 if (!error && (get_cr0 || get_all)) {
499 error = vm_get_register(vcpu, VM_REG_GUEST_CR0, &cr0);
500 if (error == 0)
501 printf("cr0[%d]\t\t0x%016lx\n", vcpuid, cr0);
502 }
503
504 if (!error && (get_cr2 || get_all)) {
505 error = vm_get_register(vcpu, VM_REG_GUEST_CR2, &cr2);
506 if (error == 0)
507 printf("cr2[%d]\t\t0x%016lx\n", vcpuid, cr2);
508 }
509
510 if (!error && (get_cr3 || get_all)) {
511 error = vm_get_register(vcpu, VM_REG_GUEST_CR3, &cr3);
512 if (error == 0)
513 printf("cr3[%d]\t\t0x%016lx\n", vcpuid, cr3);
514 }
515
516 if (!error && (get_cr4 || get_all)) {
517 error = vm_get_register(vcpu, VM_REG_GUEST_CR4, &cr4);
518 if (error == 0)
519 printf("cr4[%d]\t\t0x%016lx\n", vcpuid, cr4);
520 }
521
522 if (!error && (get_dr0 || get_all)) {
523 error = vm_get_register(vcpu, VM_REG_GUEST_DR0, &dr0);
524 if (error == 0)
525 printf("dr0[%d]\t\t0x%016lx\n", vcpuid, dr0);
526 }
527
528 if (!error && (get_dr1 || get_all)) {
529 error = vm_get_register(vcpu, VM_REG_GUEST_DR1, &dr1);
530 if (error == 0)
531 printf("dr1[%d]\t\t0x%016lx\n", vcpuid, dr1);
532 }
533
534 if (!error && (get_dr2 || get_all)) {
535 error = vm_get_register(vcpu, VM_REG_GUEST_DR2, &dr2);
536 if (error == 0)
537 printf("dr2[%d]\t\t0x%016lx\n", vcpuid, dr2);
538 }
539
540 if (!error && (get_dr3 || get_all)) {
541 error = vm_get_register(vcpu, VM_REG_GUEST_DR3, &dr3);
542 if (error == 0)
543 printf("dr3[%d]\t\t0x%016lx\n", vcpuid, dr3);
544 }
545
546 if (!error && (get_dr6 || get_all)) {
547 error = vm_get_register(vcpu, VM_REG_GUEST_DR6, &dr6);
548 if (error == 0)
549 printf("dr6[%d]\t\t0x%016lx\n", vcpuid, dr6);
550 }
551
552 if (!error && (get_dr7 || get_all)) {
553 error = vm_get_register(vcpu, VM_REG_GUEST_DR7, &dr7);
554 if (error == 0)
555 printf("dr7[%d]\t\t0x%016lx\n", vcpuid, dr7);
556 }
557
558 if (!error && (get_rsp || get_all)) {
559 error = vm_get_register(vcpu, VM_REG_GUEST_RSP, &rsp);
560 if (error == 0)
561 printf("rsp[%d]\t\t0x%016lx\n", vcpuid, rsp);
562 }
563
564 if (!error && (get_rip || get_all)) {
565 error = vm_get_register(vcpu, VM_REG_GUEST_RIP, &rip);
566 if (error == 0)
567 printf("rip[%d]\t\t0x%016lx\n", vcpuid, rip);
568 }
569
570 if (!error && (get_rax || get_all)) {
571 error = vm_get_register(vcpu, VM_REG_GUEST_RAX, &rax);
572 if (error == 0)
573 printf("rax[%d]\t\t0x%016lx\n", vcpuid, rax);
574 }
575
576 if (!error && (get_rbx || get_all)) {
577 error = vm_get_register(vcpu, VM_REG_GUEST_RBX, &rbx);
578 if (error == 0)
579 printf("rbx[%d]\t\t0x%016lx\n", vcpuid, rbx);
580 }
581
582 if (!error && (get_rcx || get_all)) {
583 error = vm_get_register(vcpu, VM_REG_GUEST_RCX, &rcx);
584 if (error == 0)
585 printf("rcx[%d]\t\t0x%016lx\n", vcpuid, rcx);
586 }
587
588 if (!error && (get_rdx || get_all)) {
589 error = vm_get_register(vcpu, VM_REG_GUEST_RDX, &rdx);
590 if (error == 0)
591 printf("rdx[%d]\t\t0x%016lx\n", vcpuid, rdx);
592 }
593
594 if (!error && (get_rsi || get_all)) {
595 error = vm_get_register(vcpu, VM_REG_GUEST_RSI, &rsi);
596 if (error == 0)
597 printf("rsi[%d]\t\t0x%016lx\n", vcpuid, rsi);
598 }
599
600 if (!error && (get_rdi || get_all)) {
601 error = vm_get_register(vcpu, VM_REG_GUEST_RDI, &rdi);
602 if (error == 0)
603 printf("rdi[%d]\t\t0x%016lx\n", vcpuid, rdi);
604 }
605
606 if (!error && (get_rbp || get_all)) {
607 error = vm_get_register(vcpu, VM_REG_GUEST_RBP, &rbp);
608 if (error == 0)
609 printf("rbp[%d]\t\t0x%016lx\n", vcpuid, rbp);
610 }
611
612 if (!error && (get_r8 || get_all)) {
613 error = vm_get_register(vcpu, VM_REG_GUEST_R8, &r8);
614 if (error == 0)
615 printf("r8[%d]\t\t0x%016lx\n", vcpuid, r8);
616 }
617
618 if (!error && (get_r9 || get_all)) {
619 error = vm_get_register(vcpu, VM_REG_GUEST_R9, &r9);
620 if (error == 0)
621 printf("r9[%d]\t\t0x%016lx\n", vcpuid, r9);
622 }
623
624 if (!error && (get_r10 || get_all)) {
625 error = vm_get_register(vcpu, VM_REG_GUEST_R10, &r10);
626 if (error == 0)
627 printf("r10[%d]\t\t0x%016lx\n", vcpuid, r10);
628 }
629
630 if (!error && (get_r11 || get_all)) {
631 error = vm_get_register(vcpu, VM_REG_GUEST_R11, &r11);
632 if (error == 0)
633 printf("r11[%d]\t\t0x%016lx\n", vcpuid, r11);
634 }
635
636 if (!error && (get_r12 || get_all)) {
637 error = vm_get_register(vcpu, VM_REG_GUEST_R12, &r12);
638 if (error == 0)
639 printf("r12[%d]\t\t0x%016lx\n", vcpuid, r12);
640 }
641
642 if (!error && (get_r13 || get_all)) {
643 error = vm_get_register(vcpu, VM_REG_GUEST_R13, &r13);
644 if (error == 0)
645 printf("r13[%d]\t\t0x%016lx\n", vcpuid, r13);
646 }
647
648 if (!error && (get_r14 || get_all)) {
649 error = vm_get_register(vcpu, VM_REG_GUEST_R14, &r14);
650 if (error == 0)
651 printf("r14[%d]\t\t0x%016lx\n", vcpuid, r14);
652 }
653
654 if (!error && (get_r15 || get_all)) {
655 error = vm_get_register(vcpu, VM_REG_GUEST_R15, &r15);
656 if (error == 0)
657 printf("r15[%d]\t\t0x%016lx\n", vcpuid, r15);
658 }
659
660 if (!error && (get_rflags || get_all)) {
661 error = vm_get_register(vcpu, VM_REG_GUEST_RFLAGS,
662 &rflags);
663 if (error == 0)
664 printf("rflags[%d]\t0x%016lx\n", vcpuid, rflags);
665 }
666
667 return (error);
668 }
669
670 static int
get_all_segments(struct vcpu * vcpu,int vcpuid)671 get_all_segments(struct vcpu *vcpu, int vcpuid)
672 {
673 uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
674 int error = 0;
675
676 if (!error && (get_desc_ds || get_all)) {
677 error = vm_get_desc(vcpu, VM_REG_GUEST_DS,
678 &desc_base, &desc_limit, &desc_access);
679 if (error == 0) {
680 printf("ds desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
681 vcpuid, desc_base, desc_limit, desc_access);
682 }
683 }
684
685 if (!error && (get_desc_es || get_all)) {
686 error = vm_get_desc(vcpu, VM_REG_GUEST_ES,
687 &desc_base, &desc_limit, &desc_access);
688 if (error == 0) {
689 printf("es desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
690 vcpuid, desc_base, desc_limit, desc_access);
691 }
692 }
693
694 if (!error && (get_desc_fs || get_all)) {
695 error = vm_get_desc(vcpu, VM_REG_GUEST_FS,
696 &desc_base, &desc_limit, &desc_access);
697 if (error == 0) {
698 printf("fs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
699 vcpuid, desc_base, desc_limit, desc_access);
700 }
701 }
702
703 if (!error && (get_desc_gs || get_all)) {
704 error = vm_get_desc(vcpu, VM_REG_GUEST_GS,
705 &desc_base, &desc_limit, &desc_access);
706 if (error == 0) {
707 printf("gs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
708 vcpuid, desc_base, desc_limit, desc_access);
709 }
710 }
711
712 if (!error && (get_desc_ss || get_all)) {
713 error = vm_get_desc(vcpu, VM_REG_GUEST_SS,
714 &desc_base, &desc_limit, &desc_access);
715 if (error == 0) {
716 printf("ss desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
717 vcpuid, desc_base, desc_limit, desc_access);
718 }
719 }
720
721 if (!error && (get_desc_cs || get_all)) {
722 error = vm_get_desc(vcpu, VM_REG_GUEST_CS,
723 &desc_base, &desc_limit, &desc_access);
724 if (error == 0) {
725 printf("cs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
726 vcpuid, desc_base, desc_limit, desc_access);
727 }
728 }
729
730 if (!error && (get_desc_tr || get_all)) {
731 error = vm_get_desc(vcpu, VM_REG_GUEST_TR,
732 &desc_base, &desc_limit, &desc_access);
733 if (error == 0) {
734 printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
735 vcpuid, desc_base, desc_limit, desc_access);
736 }
737 }
738
739 if (!error && (get_desc_ldtr || get_all)) {
740 error = vm_get_desc(vcpu, VM_REG_GUEST_LDTR,
741 &desc_base, &desc_limit, &desc_access);
742 if (error == 0) {
743 printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
744 vcpuid, desc_base, desc_limit, desc_access);
745 }
746 }
747
748 if (!error && (get_desc_gdtr || get_all)) {
749 error = vm_get_desc(vcpu, VM_REG_GUEST_GDTR,
750 &desc_base, &desc_limit, &desc_access);
751 if (error == 0) {
752 printf("gdtr[%d]\t\t0x%016lx/0x%08x\n",
753 vcpuid, desc_base, desc_limit);
754 }
755 }
756
757 if (!error && (get_desc_idtr || get_all)) {
758 error = vm_get_desc(vcpu, VM_REG_GUEST_IDTR,
759 &desc_base, &desc_limit, &desc_access);
760 if (error == 0) {
761 printf("idtr[%d]\t\t0x%016lx/0x%08x\n",
762 vcpuid, desc_base, desc_limit);
763 }
764 }
765
766 if (!error && (get_cs || get_all)) {
767 error = vm_get_register(vcpu, VM_REG_GUEST_CS, &cs);
768 if (error == 0)
769 printf("cs[%d]\t\t0x%04lx\n", vcpuid, cs);
770 }
771
772 if (!error && (get_ds || get_all)) {
773 error = vm_get_register(vcpu, VM_REG_GUEST_DS, &ds);
774 if (error == 0)
775 printf("ds[%d]\t\t0x%04lx\n", vcpuid, ds);
776 }
777
778 if (!error && (get_es || get_all)) {
779 error = vm_get_register(vcpu, VM_REG_GUEST_ES, &es);
780 if (error == 0)
781 printf("es[%d]\t\t0x%04lx\n", vcpuid, es);
782 }
783
784 if (!error && (get_fs || get_all)) {
785 error = vm_get_register(vcpu, VM_REG_GUEST_FS, &fs);
786 if (error == 0)
787 printf("fs[%d]\t\t0x%04lx\n", vcpuid, fs);
788 }
789
790 if (!error && (get_gs || get_all)) {
791 error = vm_get_register(vcpu, VM_REG_GUEST_GS, &gs);
792 if (error == 0)
793 printf("gs[%d]\t\t0x%04lx\n", vcpuid, gs);
794 }
795
796 if (!error && (get_ss || get_all)) {
797 error = vm_get_register(vcpu, VM_REG_GUEST_SS, &ss);
798 if (error == 0)
799 printf("ss[%d]\t\t0x%04lx\n", vcpuid, ss);
800 }
801
802 if (!error && (get_tr || get_all)) {
803 error = vm_get_register(vcpu, VM_REG_GUEST_TR, &tr);
804 if (error == 0)
805 printf("tr[%d]\t\t0x%04lx\n", vcpuid, tr);
806 }
807
808 if (!error && (get_ldtr || get_all)) {
809 error = vm_get_register(vcpu, VM_REG_GUEST_LDTR, &ldtr);
810 if (error == 0)
811 printf("ldtr[%d]\t\t0x%04lx\n", vcpuid, ldtr);
812 }
813
814 return (error);
815 }
816
817 static int
get_misc_vmcs(struct vcpu * vcpu,int vcpuid)818 get_misc_vmcs(struct vcpu *vcpu, int vcpuid)
819 {
820 uint64_t ctl, cr0, cr3, cr4, rsp, rip, pat, addr, u64;
821 int error = 0;
822
823 if (!error && (get_cr0_mask || get_all)) {
824 uint64_t cr0mask;
825 error = vm_get_vmcs_field(vcpu, VMCS_CR0_MASK, &cr0mask);
826 if (error == 0)
827 printf("cr0_mask[%d]\t\t0x%016lx\n", vcpuid, cr0mask);
828 }
829
830 if (!error && (get_cr0_shadow || get_all)) {
831 uint64_t cr0shadow;
832 error = vm_get_vmcs_field(vcpu, VMCS_CR0_SHADOW,
833 &cr0shadow);
834 if (error == 0)
835 printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpuid, cr0shadow);
836 }
837
838 if (!error && (get_cr4_mask || get_all)) {
839 uint64_t cr4mask;
840 error = vm_get_vmcs_field(vcpu, VMCS_CR4_MASK, &cr4mask);
841 if (error == 0)
842 printf("cr4_mask[%d]\t\t0x%016lx\n", vcpuid, cr4mask);
843 }
844
845 if (!error && (get_cr4_shadow || get_all)) {
846 uint64_t cr4shadow;
847 error = vm_get_vmcs_field(vcpu, VMCS_CR4_SHADOW,
848 &cr4shadow);
849 if (error == 0)
850 printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpuid, cr4shadow);
851 }
852
853 if (!error && (get_cr3_targets || get_all)) {
854 uint64_t target_count, target_addr;
855 error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET_COUNT,
856 &target_count);
857 if (error == 0) {
858 printf("cr3_target_count[%d]\t0x%016lx\n",
859 vcpuid, target_count);
860 }
861
862 error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET0,
863 &target_addr);
864 if (error == 0) {
865 printf("cr3_target0[%d]\t\t0x%016lx\n",
866 vcpuid, target_addr);
867 }
868
869 error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET1,
870 &target_addr);
871 if (error == 0) {
872 printf("cr3_target1[%d]\t\t0x%016lx\n",
873 vcpuid, target_addr);
874 }
875
876 error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET2,
877 &target_addr);
878 if (error == 0) {
879 printf("cr3_target2[%d]\t\t0x%016lx\n",
880 vcpuid, target_addr);
881 }
882
883 error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET3,
884 &target_addr);
885 if (error == 0) {
886 printf("cr3_target3[%d]\t\t0x%016lx\n",
887 vcpuid, target_addr);
888 }
889 }
890
891 if (!error && (get_pinbased_ctls || get_all)) {
892 error = vm_get_vmcs_field(vcpu, VMCS_PIN_BASED_CTLS, &ctl);
893 if (error == 0)
894 printf("pinbased_ctls[%d]\t0x%016lx\n", vcpuid, ctl);
895 }
896
897 if (!error && (get_procbased_ctls || get_all)) {
898 error = vm_get_vmcs_field(vcpu,
899 VMCS_PRI_PROC_BASED_CTLS, &ctl);
900 if (error == 0)
901 printf("procbased_ctls[%d]\t0x%016lx\n", vcpuid, ctl);
902 }
903
904 if (!error && (get_procbased_ctls2 || get_all)) {
905 error = vm_get_vmcs_field(vcpu,
906 VMCS_SEC_PROC_BASED_CTLS, &ctl);
907 if (error == 0)
908 printf("procbased_ctls2[%d]\t0x%016lx\n", vcpuid, ctl);
909 }
910
911 if (!error && (get_vmcs_gla || get_all)) {
912 error = vm_get_vmcs_field(vcpu,
913 VMCS_GUEST_LINEAR_ADDRESS, &u64);
914 if (error == 0)
915 printf("gla[%d]\t\t0x%016lx\n", vcpuid, u64);
916 }
917
918 if (!error && (get_vmcs_gpa || get_all)) {
919 error = vm_get_vmcs_field(vcpu,
920 VMCS_GUEST_PHYSICAL_ADDRESS, &u64);
921 if (error == 0)
922 printf("gpa[%d]\t\t0x%016lx\n", vcpuid, u64);
923 }
924
925 if (!error && (get_vmcs_entry_interruption_info ||
926 get_all)) {
927 error = vm_get_vmcs_field(vcpu, VMCS_ENTRY_INTR_INFO,&u64);
928 if (error == 0) {
929 printf("entry_interruption_info[%d]\t0x%016lx\n",
930 vcpuid, u64);
931 }
932 }
933
934 if (!error && (get_tpr_threshold || get_all)) {
935 uint64_t threshold;
936 error = vm_get_vmcs_field(vcpu, VMCS_TPR_THRESHOLD,
937 &threshold);
938 if (error == 0)
939 printf("tpr_threshold[%d]\t0x%016lx\n", vcpuid, threshold);
940 }
941
942 if (!error && (get_inst_err || get_all)) {
943 uint64_t insterr;
944 error = vm_get_vmcs_field(vcpu, VMCS_INSTRUCTION_ERROR,
945 &insterr);
946 if (error == 0) {
947 printf("instruction_error[%d]\t0x%016lx\n",
948 vcpuid, insterr);
949 }
950 }
951
952 if (!error && (get_exit_ctls || get_all)) {
953 error = vm_get_vmcs_field(vcpu, VMCS_EXIT_CTLS, &ctl);
954 if (error == 0)
955 printf("exit_ctls[%d]\t\t0x%016lx\n", vcpuid, ctl);
956 }
957
958 if (!error && (get_entry_ctls || get_all)) {
959 error = vm_get_vmcs_field(vcpu, VMCS_ENTRY_CTLS, &ctl);
960 if (error == 0)
961 printf("entry_ctls[%d]\t\t0x%016lx\n", vcpuid, ctl);
962 }
963
964 if (!error && (get_host_pat || get_all)) {
965 error = vm_get_vmcs_field(vcpu, VMCS_HOST_IA32_PAT, &pat);
966 if (error == 0)
967 printf("host_pat[%d]\t\t0x%016lx\n", vcpuid, pat);
968 }
969
970 if (!error && (get_host_cr0 || get_all)) {
971 error = vm_get_vmcs_field(vcpu, VMCS_HOST_CR0, &cr0);
972 if (error == 0)
973 printf("host_cr0[%d]\t\t0x%016lx\n", vcpuid, cr0);
974 }
975
976 if (!error && (get_host_cr3 || get_all)) {
977 error = vm_get_vmcs_field(vcpu, VMCS_HOST_CR3, &cr3);
978 if (error == 0)
979 printf("host_cr3[%d]\t\t0x%016lx\n", vcpuid, cr3);
980 }
981
982 if (!error && (get_host_cr4 || get_all)) {
983 error = vm_get_vmcs_field(vcpu, VMCS_HOST_CR4, &cr4);
984 if (error == 0)
985 printf("host_cr4[%d]\t\t0x%016lx\n", vcpuid, cr4);
986 }
987
988 if (!error && (get_host_rip || get_all)) {
989 error = vm_get_vmcs_field(vcpu, VMCS_HOST_RIP, &rip);
990 if (error == 0)
991 printf("host_rip[%d]\t\t0x%016lx\n", vcpuid, rip);
992 }
993
994 if (!error && (get_host_rsp || get_all)) {
995 error = vm_get_vmcs_field(vcpu, VMCS_HOST_RSP, &rsp);
996 if (error == 0)
997 printf("host_rsp[%d]\t\t0x%016lx\n", vcpuid, rsp);
998 }
999
1000 if (!error && (get_vmcs_link || get_all)) {
1001 error = vm_get_vmcs_field(vcpu, VMCS_LINK_POINTER, &addr);
1002 if (error == 0)
1003 printf("vmcs_pointer[%d]\t0x%016lx\n", vcpuid, addr);
1004 }
1005
1006 if (!error && (get_vmcs_exit_interruption_info || get_all)) {
1007 error = vm_get_vmcs_field(vcpu, VMCS_EXIT_INTR_INFO, &u64);
1008 if (error == 0) {
1009 printf("vmcs_exit_interruption_info[%d]\t0x%016lx\n",
1010 vcpuid, u64);
1011 }
1012 }
1013
1014 if (!error && (get_vmcs_exit_interruption_error || get_all)) {
1015 error = vm_get_vmcs_field(vcpu, VMCS_EXIT_INTR_ERRCODE,
1016 &u64);
1017 if (error == 0) {
1018 printf("vmcs_exit_interruption_error[%d]\t0x%016lx\n",
1019 vcpuid, u64);
1020 }
1021 }
1022
1023 if (!error && (get_vmcs_interruptibility || get_all)) {
1024 error = vm_get_vmcs_field(vcpu,
1025 VMCS_GUEST_INTERRUPTIBILITY, &u64);
1026 if (error == 0) {
1027 printf("vmcs_guest_interruptibility[%d]\t0x%016lx\n",
1028 vcpuid, u64);
1029 }
1030 }
1031
1032 if (!error && (get_vmcs_exit_inst_length || get_all)) {
1033 error = vm_get_vmcs_field(vcpu,
1034 VMCS_EXIT_INSTRUCTION_LENGTH, &u64);
1035 if (error == 0)
1036 printf("vmcs_exit_inst_length[%d]\t0x%08x\n", vcpuid,
1037 (uint32_t)u64);
1038 }
1039
1040 if (!error && (get_vmcs_exit_qualification || get_all)) {
1041 error = vm_get_vmcs_field(vcpu, VMCS_EXIT_QUALIFICATION,
1042 &u64);
1043 if (error == 0)
1044 printf("vmcs_exit_qualification[%d]\t0x%016lx\n",
1045 vcpuid, u64);
1046 }
1047
1048 return (error);
1049 }
1050
1051 static int
get_misc_vmcb(struct vcpu * vcpu,int vcpuid)1052 get_misc_vmcb(struct vcpu *vcpu, int vcpuid)
1053 {
1054 uint64_t ctl, addr;
1055 int error = 0;
1056
1057 if (!error && (get_vmcb_intercept || get_all)) {
1058 error = vm_get_vmcb_field(vcpu, VMCB_OFF_CR_INTERCEPT, 4,
1059 &ctl);
1060 if (error == 0)
1061 printf("cr_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1062
1063 error = vm_get_vmcb_field(vcpu, VMCB_OFF_DR_INTERCEPT, 4,
1064 &ctl);
1065 if (error == 0)
1066 printf("dr_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1067
1068 error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXC_INTERCEPT, 4,
1069 &ctl);
1070 if (error == 0)
1071 printf("exc_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1072
1073 error = vm_get_vmcb_field(vcpu, VMCB_OFF_INST1_INTERCEPT,
1074 4, &ctl);
1075 if (error == 0)
1076 printf("inst1_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1077
1078 error = vm_get_vmcb_field(vcpu, VMCB_OFF_INST2_INTERCEPT,
1079 4, &ctl);
1080 if (error == 0)
1081 printf("inst2_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1082 }
1083
1084 if (!error && (get_vmcb_tlb_ctrl || get_all)) {
1085 error = vm_get_vmcb_field(vcpu, VMCB_OFF_TLB_CTRL,
1086 4, &ctl);
1087 if (error == 0)
1088 printf("TLB ctrl[%d]\t0x%016lx\n", vcpuid, ctl);
1089 }
1090
1091 if (!error && (get_vmcb_exit_details || get_all)) {
1092 error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXITINFO1,
1093 8, &ctl);
1094 if (error == 0)
1095 printf("exitinfo1[%d]\t0x%016lx\n", vcpuid, ctl);
1096 error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXITINFO2,
1097 8, &ctl);
1098 if (error == 0)
1099 printf("exitinfo2[%d]\t0x%016lx\n", vcpuid, ctl);
1100 error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXITINTINFO,
1101 8, &ctl);
1102 if (error == 0)
1103 printf("exitintinfo[%d]\t0x%016lx\n", vcpuid, ctl);
1104 }
1105
1106 if (!error && (get_vmcb_virq || get_all)) {
1107 error = vm_get_vmcb_field(vcpu, VMCB_OFF_VIRQ,
1108 8, &ctl);
1109 if (error == 0)
1110 printf("v_irq/tpr[%d]\t0x%016lx\n", vcpuid, ctl);
1111 }
1112
1113 if (!error && (get_apic_access_addr || get_all)) {
1114 error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_BAR, 8,
1115 &addr);
1116 if (error == 0)
1117 printf("AVIC apic_bar[%d]\t0x%016lx\n", vcpuid, addr);
1118 }
1119
1120 if (!error && (get_virtual_apic_addr || get_all)) {
1121 error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_PAGE, 8,
1122 &addr);
1123 if (error == 0)
1124 printf("AVIC backing page[%d]\t0x%016lx\n", vcpuid, addr);
1125 }
1126
1127 if (!error && (get_avic_table || get_all)) {
1128 error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_LT, 8,
1129 &addr);
1130 if (error == 0)
1131 printf("AVIC logical table[%d]\t0x%016lx\n",
1132 vcpuid, addr);
1133 error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_PT, 8,
1134 &addr);
1135 if (error == 0)
1136 printf("AVIC physical table[%d]\t0x%016lx\n",
1137 vcpuid, addr);
1138 }
1139
1140 return (error);
1141 }
1142
1143 static struct option *
setup_options(bool cpu_intel)1144 setup_options(bool cpu_intel)
1145 {
1146 const struct option common_opts[] = {
1147 { "vm", REQ_ARG, 0, VMNAME },
1148 { "cpu", REQ_ARG, 0, VCPU },
1149 { "set-mem", REQ_ARG, 0, SET_MEM },
1150 { "set-efer", REQ_ARG, 0, SET_EFER },
1151 { "set-cr0", REQ_ARG, 0, SET_CR0 },
1152 { "set-cr2", REQ_ARG, 0, SET_CR2 },
1153 { "set-cr3", REQ_ARG, 0, SET_CR3 },
1154 { "set-cr4", REQ_ARG, 0, SET_CR4 },
1155 { "set-dr0", REQ_ARG, 0, SET_DR0 },
1156 { "set-dr1", REQ_ARG, 0, SET_DR1 },
1157 { "set-dr2", REQ_ARG, 0, SET_DR2 },
1158 { "set-dr3", REQ_ARG, 0, SET_DR3 },
1159 { "set-dr6", REQ_ARG, 0, SET_DR6 },
1160 { "set-dr7", REQ_ARG, 0, SET_DR7 },
1161 { "set-rsp", REQ_ARG, 0, SET_RSP },
1162 { "set-rip", REQ_ARG, 0, SET_RIP },
1163 { "set-rax", REQ_ARG, 0, SET_RAX },
1164 { "set-rflags", REQ_ARG, 0, SET_RFLAGS },
1165 { "desc-base", REQ_ARG, 0, DESC_BASE },
1166 { "desc-limit", REQ_ARG, 0, DESC_LIMIT },
1167 { "desc-access",REQ_ARG, 0, DESC_ACCESS },
1168 { "set-cs", REQ_ARG, 0, SET_CS },
1169 { "set-ds", REQ_ARG, 0, SET_DS },
1170 { "set-es", REQ_ARG, 0, SET_ES },
1171 { "set-fs", REQ_ARG, 0, SET_FS },
1172 { "set-gs", REQ_ARG, 0, SET_GS },
1173 { "set-ss", REQ_ARG, 0, SET_SS },
1174 { "set-tr", REQ_ARG, 0, SET_TR },
1175 { "set-ldtr", REQ_ARG, 0, SET_LDTR },
1176 { "set-x2apic-state",REQ_ARG, 0, SET_X2APIC_STATE },
1177 { "capname", REQ_ARG, 0, CAPNAME },
1178 { "unassign-pptdev", REQ_ARG, 0, UNASSIGN_PPTDEV },
1179 { "setcap", REQ_ARG, 0, SET_CAP },
1180 { "get-gpa-pmap", REQ_ARG, 0, GET_GPA_PMAP },
1181 { "assert-lapic-lvt", REQ_ARG, 0, ASSERT_LAPIC_LVT },
1182 { "get-rtc-time", NO_ARG, &get_rtc_time, 1 },
1183 { "set-rtc-time", REQ_ARG, 0, SET_RTC_TIME },
1184 { "rtc-nvram-offset", REQ_ARG, 0, RTC_NVRAM_OFFSET },
1185 { "get-rtc-nvram", NO_ARG, &get_rtc_nvram, 1 },
1186 { "set-rtc-nvram", REQ_ARG, 0, SET_RTC_NVRAM },
1187 { "getcap", NO_ARG, &getcap, 1 },
1188 { "get-stats", NO_ARG, &get_stats, 1 },
1189 { "get-desc-ds",NO_ARG, &get_desc_ds, 1 },
1190 { "set-desc-ds",NO_ARG, &set_desc_ds, 1 },
1191 { "get-desc-es",NO_ARG, &get_desc_es, 1 },
1192 { "set-desc-es",NO_ARG, &set_desc_es, 1 },
1193 { "get-desc-ss",NO_ARG, &get_desc_ss, 1 },
1194 { "set-desc-ss",NO_ARG, &set_desc_ss, 1 },
1195 { "get-desc-cs",NO_ARG, &get_desc_cs, 1 },
1196 { "set-desc-cs",NO_ARG, &set_desc_cs, 1 },
1197 { "get-desc-fs",NO_ARG, &get_desc_fs, 1 },
1198 { "set-desc-fs",NO_ARG, &set_desc_fs, 1 },
1199 { "get-desc-gs",NO_ARG, &get_desc_gs, 1 },
1200 { "set-desc-gs",NO_ARG, &set_desc_gs, 1 },
1201 { "get-desc-tr",NO_ARG, &get_desc_tr, 1 },
1202 { "set-desc-tr",NO_ARG, &set_desc_tr, 1 },
1203 { "set-desc-ldtr", NO_ARG, &set_desc_ldtr, 1 },
1204 { "get-desc-ldtr", NO_ARG, &get_desc_ldtr, 1 },
1205 { "set-desc-gdtr", NO_ARG, &set_desc_gdtr, 1 },
1206 { "get-desc-gdtr", NO_ARG, &get_desc_gdtr, 1 },
1207 { "set-desc-idtr", NO_ARG, &set_desc_idtr, 1 },
1208 { "get-desc-idtr", NO_ARG, &get_desc_idtr, 1 },
1209 { "get-memmap", NO_ARG, &get_memmap, 1 },
1210 { "get-memseg", NO_ARG, &get_memseg, 1 },
1211 { "get-efer", NO_ARG, &get_efer, 1 },
1212 { "get-cr0", NO_ARG, &get_cr0, 1 },
1213 { "get-cr2", NO_ARG, &get_cr2, 1 },
1214 { "get-cr3", NO_ARG, &get_cr3, 1 },
1215 { "get-cr4", NO_ARG, &get_cr4, 1 },
1216 { "get-dr0", NO_ARG, &get_dr0, 1 },
1217 { "get-dr1", NO_ARG, &get_dr1, 1 },
1218 { "get-dr2", NO_ARG, &get_dr2, 1 },
1219 { "get-dr3", NO_ARG, &get_dr3, 1 },
1220 { "get-dr6", NO_ARG, &get_dr6, 1 },
1221 { "get-dr7", NO_ARG, &get_dr7, 1 },
1222 { "get-rsp", NO_ARG, &get_rsp, 1 },
1223 { "get-rip", NO_ARG, &get_rip, 1 },
1224 { "get-rax", NO_ARG, &get_rax, 1 },
1225 { "get-rbx", NO_ARG, &get_rbx, 1 },
1226 { "get-rcx", NO_ARG, &get_rcx, 1 },
1227 { "get-rdx", NO_ARG, &get_rdx, 1 },
1228 { "get-rsi", NO_ARG, &get_rsi, 1 },
1229 { "get-rdi", NO_ARG, &get_rdi, 1 },
1230 { "get-rbp", NO_ARG, &get_rbp, 1 },
1231 { "get-r8", NO_ARG, &get_r8, 1 },
1232 { "get-r9", NO_ARG, &get_r9, 1 },
1233 { "get-r10", NO_ARG, &get_r10, 1 },
1234 { "get-r11", NO_ARG, &get_r11, 1 },
1235 { "get-r12", NO_ARG, &get_r12, 1 },
1236 { "get-r13", NO_ARG, &get_r13, 1 },
1237 { "get-r14", NO_ARG, &get_r14, 1 },
1238 { "get-r15", NO_ARG, &get_r15, 1 },
1239 { "get-rflags", NO_ARG, &get_rflags, 1 },
1240 { "get-cs", NO_ARG, &get_cs, 1 },
1241 { "get-ds", NO_ARG, &get_ds, 1 },
1242 { "get-es", NO_ARG, &get_es, 1 },
1243 { "get-fs", NO_ARG, &get_fs, 1 },
1244 { "get-gs", NO_ARG, &get_gs, 1 },
1245 { "get-ss", NO_ARG, &get_ss, 1 },
1246 { "get-tr", NO_ARG, &get_tr, 1 },
1247 { "get-ldtr", NO_ARG, &get_ldtr, 1 },
1248 { "get-eptp", NO_ARG, &get_eptp, 1 },
1249 { "get-exception-bitmap",
1250 NO_ARG, &get_exception_bitmap, 1 },
1251 { "get-io-bitmap-address",
1252 NO_ARG, &get_io_bitmap, 1 },
1253 { "get-tsc-offset", NO_ARG, &get_tsc_offset, 1 },
1254 { "get-msr-bitmap",
1255 NO_ARG, &get_msr_bitmap, 1 },
1256 { "get-msr-bitmap-address",
1257 NO_ARG, &get_msr_bitmap_address, 1 },
1258 { "get-guest-pat", NO_ARG, &get_guest_pat, 1 },
1259 { "get-guest-sysenter",
1260 NO_ARG, &get_guest_sysenter, 1 },
1261 { "get-exit-reason",
1262 NO_ARG, &get_exit_reason, 1 },
1263 { "get-x2apic-state", NO_ARG, &get_x2apic_state, 1 },
1264 { "get-all", NO_ARG, &get_all, 1 },
1265 { "run", NO_ARG, &run, 1 },
1266 { "create", NO_ARG, &create, 1 },
1267 { "destroy", NO_ARG, &destroy, 1 },
1268 { "inject-nmi", NO_ARG, &inject_nmi, 1 },
1269 { "force-reset", NO_ARG, &force_reset, 1 },
1270 { "force-poweroff", NO_ARG, &force_poweroff, 1 },
1271 { "get-active-cpus", NO_ARG, &get_active_cpus, 1 },
1272 { "get-debug-cpus", NO_ARG, &get_debug_cpus, 1 },
1273 { "get-suspended-cpus", NO_ARG, &get_suspended_cpus, 1 },
1274 { "get-intinfo", NO_ARG, &get_intinfo, 1 },
1275 { "get-cpu-topology", NO_ARG, &get_cpu_topology, 1 },
1276 #ifdef BHYVE_SNAPSHOT
1277 { "checkpoint", REQ_ARG, 0, SET_CHECKPOINT_FILE},
1278 { "suspend", REQ_ARG, 0, SET_SUSPEND_FILE},
1279 #endif
1280 };
1281
1282 const struct option intel_opts[] = {
1283 { "get-vmcs-pinbased-ctls",
1284 NO_ARG, &get_pinbased_ctls, 1 },
1285 { "get-vmcs-procbased-ctls",
1286 NO_ARG, &get_procbased_ctls, 1 },
1287 { "get-vmcs-procbased-ctls2",
1288 NO_ARG, &get_procbased_ctls2, 1 },
1289 { "get-vmcs-guest-linear-address",
1290 NO_ARG, &get_vmcs_gla, 1 },
1291 { "get-vmcs-guest-physical-address",
1292 NO_ARG, &get_vmcs_gpa, 1 },
1293 { "get-vmcs-entry-interruption-info",
1294 NO_ARG, &get_vmcs_entry_interruption_info, 1},
1295 { "get-vmcs-cr0-mask", NO_ARG, &get_cr0_mask, 1 },
1296 { "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 },
1297 { "get-vmcs-cr4-mask", NO_ARG, &get_cr4_mask, 1 },
1298 { "get-vmcs-cr4-shadow", NO_ARG, &get_cr4_shadow, 1 },
1299 { "get-vmcs-cr3-targets", NO_ARG, &get_cr3_targets, 1 },
1300 { "get-vmcs-tpr-threshold",
1301 NO_ARG, &get_tpr_threshold, 1 },
1302 { "get-vmcs-vpid", NO_ARG, &get_vpid_asid, 1 },
1303 { "get-vmcs-exit-ctls", NO_ARG, &get_exit_ctls, 1 },
1304 { "get-vmcs-entry-ctls",
1305 NO_ARG, &get_entry_ctls, 1 },
1306 { "get-vmcs-instruction-error",
1307 NO_ARG, &get_inst_err, 1 },
1308 { "get-vmcs-host-pat", NO_ARG, &get_host_pat, 1 },
1309 { "get-vmcs-host-cr0",
1310 NO_ARG, &get_host_cr0, 1 },
1311 { "get-vmcs-exit-qualification",
1312 NO_ARG, &get_vmcs_exit_qualification, 1 },
1313 { "get-vmcs-exit-inst-length",
1314 NO_ARG, &get_vmcs_exit_inst_length, 1 },
1315 { "get-vmcs-interruptibility",
1316 NO_ARG, &get_vmcs_interruptibility, 1 },
1317 { "get-vmcs-exit-interruption-error",
1318 NO_ARG, &get_vmcs_exit_interruption_error, 1 },
1319 { "get-vmcs-exit-interruption-info",
1320 NO_ARG, &get_vmcs_exit_interruption_info, 1 },
1321 { "get-vmcs-link", NO_ARG, &get_vmcs_link, 1 },
1322 { "get-vmcs-host-cr3",
1323 NO_ARG, &get_host_cr3, 1 },
1324 { "get-vmcs-host-cr4",
1325 NO_ARG, &get_host_cr4, 1 },
1326 { "get-vmcs-host-rip",
1327 NO_ARG, &get_host_rip, 1 },
1328 { "get-vmcs-host-rsp",
1329 NO_ARG, &get_host_rsp, 1 },
1330 { "get-apic-access-address",
1331 NO_ARG, &get_apic_access_addr, 1},
1332 { "get-virtual-apic-address",
1333 NO_ARG, &get_virtual_apic_addr, 1}
1334 };
1335
1336 const struct option amd_opts[] = {
1337 { "get-vmcb-intercepts",
1338 NO_ARG, &get_vmcb_intercept, 1 },
1339 { "get-vmcb-asid",
1340 NO_ARG, &get_vpid_asid, 1 },
1341 { "get-vmcb-exit-details",
1342 NO_ARG, &get_vmcb_exit_details, 1 },
1343 { "get-vmcb-tlb-ctrl",
1344 NO_ARG, &get_vmcb_tlb_ctrl, 1 },
1345 { "get-vmcb-virq",
1346 NO_ARG, &get_vmcb_virq, 1 },
1347 { "get-avic-apic-bar",
1348 NO_ARG, &get_apic_access_addr, 1 },
1349 { "get-avic-backing-page",
1350 NO_ARG, &get_virtual_apic_addr, 1 },
1351 { "get-avic-table",
1352 NO_ARG, &get_avic_table, 1 }
1353 };
1354
1355 const struct option null_opt = {
1356 NULL, 0, NULL, 0
1357 };
1358
1359 struct option *all_opts;
1360 char *cp;
1361 int optlen;
1362
1363 optlen = sizeof(common_opts);
1364
1365 if (cpu_intel)
1366 optlen += sizeof(intel_opts);
1367 else
1368 optlen += sizeof(amd_opts);
1369
1370 optlen += sizeof(null_opt);
1371
1372 all_opts = malloc(optlen);
1373
1374 cp = (char *)all_opts;
1375 memcpy(cp, common_opts, sizeof(common_opts));
1376 cp += sizeof(common_opts);
1377
1378 if (cpu_intel) {
1379 memcpy(cp, intel_opts, sizeof(intel_opts));
1380 cp += sizeof(intel_opts);
1381 } else {
1382 memcpy(cp, amd_opts, sizeof(amd_opts));
1383 cp += sizeof(amd_opts);
1384 }
1385
1386 memcpy(cp, &null_opt, sizeof(null_opt));
1387 cp += sizeof(null_opt);
1388
1389 return (all_opts);
1390 }
1391
1392 static void
usage(const struct option * opts)1393 usage(const struct option *opts)
1394 {
1395 static const char *set_desc[] = {
1396 [VCPU] = "vcpu_number",
1397 [SET_MEM] = "memory in units of MB",
1398 [SET_EFER] = "EFER",
1399 [SET_CR0] = "CR0",
1400 [SET_CR2] = "CR2",
1401 [SET_CR3] = "CR3",
1402 [SET_CR4] = "CR4",
1403 [SET_DR0] = "DR0",
1404 [SET_DR1] = "DR1",
1405 [SET_DR2] = "DR2",
1406 [SET_DR3] = "DR3",
1407 [SET_DR6] = "DR6",
1408 [SET_DR7] = "DR7",
1409 [SET_RSP] = "RSP",
1410 [SET_RIP] = "RIP",
1411 [SET_RAX] = "RAX",
1412 [SET_RFLAGS] = "RFLAGS",
1413 [DESC_BASE] = "BASE",
1414 [DESC_LIMIT] = "LIMIT",
1415 [DESC_ACCESS] = "ACCESS",
1416 [SET_CS] = "CS",
1417 [SET_DS] = "DS",
1418 [SET_ES] = "ES",
1419 [SET_FS] = "FS",
1420 [SET_GS] = "GS",
1421 [SET_SS] = "SS",
1422 [SET_TR] = "TR",
1423 [SET_LDTR] = "LDTR",
1424 [SET_X2APIC_STATE] = "state",
1425 [SET_CAP] = "0|1",
1426 [CAPNAME] = "capname",
1427 [UNASSIGN_PPTDEV] = "bus/slot.func",
1428 [GET_GPA_PMAP] = "gpa",
1429 [ASSERT_LAPIC_LVT] = "pin",
1430 [SET_RTC_TIME] = "secs",
1431 [SET_RTC_NVRAM] = "val",
1432 [RTC_NVRAM_OFFSET] = "offset",
1433 #ifdef BHYVE_SNAPSHOT
1434 [SET_CHECKPOINT_FILE] = "filename",
1435 [SET_SUSPEND_FILE] = "filename",
1436 #endif
1437 };
1438 (void)fprintf(stderr, "Usage: %s --vm=<vmname>\n", progname);
1439 for (const struct option *o = opts; o->name; o++) {
1440 if (strcmp(o->name, "vm") == 0)
1441 continue;
1442 if (o->has_arg == REQ_ARG)
1443 (void)fprintf(stderr, " [--%s=<%s>]\n",
1444 o->name, set_desc[o->val]);
1445 else
1446 (void)fprintf(stderr, " [--%s]\n", o->name);
1447 }
1448 exit(1);
1449 }
1450
1451 static const char *
wday_str(int idx)1452 wday_str(int idx)
1453 {
1454 static const char *weekdays[] = {
1455 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1456 };
1457
1458 if (idx >= 0 && idx < 7)
1459 return (weekdays[idx]);
1460 else
1461 return ("UNK");
1462 }
1463
1464 static const char *
mon_str(int idx)1465 mon_str(int idx)
1466 {
1467 static const char *months[] = {
1468 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1469 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1470 };
1471
1472 if (idx >= 0 && idx < 12)
1473 return (months[idx]);
1474 else
1475 return ("UNK");
1476 }
1477
1478 static int
show_memmap(struct vmctx * ctx)1479 show_memmap(struct vmctx *ctx)
1480 {
1481 char name[SPECNAMELEN + 1], numbuf[8];
1482 vm_ooffset_t segoff;
1483 vm_paddr_t gpa;
1484 size_t maplen, seglen;
1485 int error, flags, prot, segid, delim;
1486
1487 printf("Address Length Segment Offset ");
1488 printf("Prot Flags\n");
1489
1490 gpa = 0;
1491 while (1) {
1492 error = vm_mmap_getnext(ctx, &gpa, &segid, &segoff, &maplen,
1493 &prot, &flags);
1494 if (error)
1495 return (errno == ENOENT ? 0 : error);
1496
1497 error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1498 if (error)
1499 return (error);
1500
1501 printf("%-12lX", gpa);
1502 humanize_number(numbuf, sizeof(numbuf), maplen, "B",
1503 HN_AUTOSCALE, HN_NOSPACE);
1504 printf("%-12s", numbuf);
1505
1506 printf("%-12s", name[0] ? name : "sysmem");
1507 printf("%-12lX", segoff);
1508 printf("%c%c%c ", prot & PROT_READ ? 'R' : '-',
1509 prot & PROT_WRITE ? 'W' : '-',
1510 prot & PROT_EXEC ? 'X' : '-');
1511
1512 delim = '\0';
1513 if (flags & VM_MEMMAP_F_WIRED) {
1514 printf("%cwired", delim);
1515 delim = '/';
1516 }
1517 if (flags & VM_MEMMAP_F_IOMMU) {
1518 printf("%ciommu", delim);
1519 delim = '/';
1520 }
1521 printf("\n");
1522
1523 gpa += maplen;
1524 }
1525 }
1526
1527 static int
show_memseg(struct vmctx * ctx)1528 show_memseg(struct vmctx *ctx)
1529 {
1530 char name[SPECNAMELEN + 1], numbuf[8];
1531 size_t seglen;
1532 int error, segid;
1533
1534 printf("ID Length Name\n");
1535
1536 segid = 0;
1537 while (1) {
1538 error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1539 if (error)
1540 return (errno == EINVAL ? 0 : error);
1541
1542 if (seglen) {
1543 printf("%-4d", segid);
1544 humanize_number(numbuf, sizeof(numbuf), seglen, "B",
1545 HN_AUTOSCALE, HN_NOSPACE);
1546 printf("%-12s", numbuf);
1547 printf("%s", name[0] ? name : "sysmem");
1548 printf("\n");
1549 }
1550 segid++;
1551 }
1552 }
1553
1554 #ifdef BHYVE_SNAPSHOT
1555 static int
send_message(const char * vmname,nvlist_t * nvl)1556 send_message(const char *vmname, nvlist_t *nvl)
1557 {
1558 struct sockaddr_un addr;
1559 int err = 0, socket_fd;
1560
1561 socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
1562 if (socket_fd < 0) {
1563 perror("Error creating bhyvectl socket");
1564 err = errno;
1565 goto done;
1566 }
1567
1568 memset(&addr, 0, sizeof(struct sockaddr_un));
1569 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
1570 BHYVE_RUN_DIR, vmname);
1571 addr.sun_family = AF_UNIX;
1572 addr.sun_len = SUN_LEN(&addr);
1573
1574 if (connect(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) {
1575 perror("connect() failed");
1576 err = errno;
1577 goto done;
1578 }
1579
1580 if (nvlist_send(socket_fd, nvl) < 0) {
1581 perror("nvlist_send() failed");
1582 err = errno;
1583 }
1584 done:
1585 nvlist_destroy(nvl);
1586
1587 if (socket_fd >= 0)
1588 close(socket_fd);
1589 return (err);
1590 }
1591
1592 static int
open_directory(const char * file)1593 open_directory(const char *file)
1594 {
1595 char *path;
1596 int fd;
1597
1598 if ((path = strdup(file)) == NULL)
1599 return (-1);
1600
1601 dirname(path);
1602 fd = open(path, O_DIRECTORY);
1603 free(path);
1604
1605 return (fd);
1606 }
1607
1608 static int
snapshot_request(const char * vmname,char * file,bool suspend)1609 snapshot_request(const char *vmname, char *file, bool suspend)
1610 {
1611 nvlist_t *nvl;
1612 int fd;
1613
1614 if ((fd = open_directory(file)) < 0)
1615 return (errno);
1616
1617 nvl = nvlist_create(0);
1618 nvlist_add_string(nvl, "cmd", "checkpoint");
1619 nvlist_add_string(nvl, "filename", basename(file));
1620 nvlist_add_bool(nvl, "suspend", suspend);
1621 nvlist_move_descriptor(nvl, "fddir", fd);
1622
1623 return (send_message(vmname, nvl));
1624 }
1625 #endif
1626
1627 int
main(int argc,char * argv[])1628 main(int argc, char *argv[])
1629 {
1630 char *vmname;
1631 int error, ch, vcpuid, ptenum;
1632 vm_paddr_t gpa_pmap;
1633 struct vm_run vmrun;
1634 uint64_t rax, cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
1635 uint64_t rsp, rip, rflags, efer, pat;
1636 uint64_t eptp, bm, addr, u64, pteval[4], *pte, info[2];
1637 struct vmctx *ctx;
1638 struct vcpu *vcpu;
1639 cpuset_t cpus;
1640 bool cpu_intel;
1641 uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
1642 struct tm tm;
1643 struct option *opts;
1644 #ifdef BHYVE_SNAPSHOT
1645 char *checkpoint_file = NULL;
1646 #endif
1647
1648 cpu_intel = cpu_vendor_intel();
1649 opts = setup_options(cpu_intel);
1650
1651 vcpuid = 0;
1652 vmname = NULL;
1653 assert_lapic_lvt = -1;
1654 progname = basename(argv[0]);
1655
1656 while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) {
1657 switch (ch) {
1658 case 0:
1659 break;
1660 case VMNAME:
1661 vmname = optarg;
1662 break;
1663 case VCPU:
1664 vcpuid = atoi(optarg);
1665 break;
1666 case SET_MEM:
1667 memsize = atoi(optarg) * MB;
1668 memsize = roundup(memsize, 2 * MB);
1669 break;
1670 case SET_EFER:
1671 efer = strtoul(optarg, NULL, 0);
1672 set_efer = 1;
1673 break;
1674 case SET_CR0:
1675 cr0 = strtoul(optarg, NULL, 0);
1676 set_cr0 = 1;
1677 break;
1678 case SET_CR2:
1679 cr2 = strtoul(optarg, NULL, 0);
1680 set_cr2 = 1;
1681 break;
1682 case SET_CR3:
1683 cr3 = strtoul(optarg, NULL, 0);
1684 set_cr3 = 1;
1685 break;
1686 case SET_CR4:
1687 cr4 = strtoul(optarg, NULL, 0);
1688 set_cr4 = 1;
1689 break;
1690 case SET_DR0:
1691 dr0 = strtoul(optarg, NULL, 0);
1692 set_dr0 = 1;
1693 break;
1694 case SET_DR1:
1695 dr1 = strtoul(optarg, NULL, 0);
1696 set_dr1 = 1;
1697 break;
1698 case SET_DR2:
1699 dr2 = strtoul(optarg, NULL, 0);
1700 set_dr2 = 1;
1701 break;
1702 case SET_DR3:
1703 dr3 = strtoul(optarg, NULL, 0);
1704 set_dr3 = 1;
1705 break;
1706 case SET_DR6:
1707 dr6 = strtoul(optarg, NULL, 0);
1708 set_dr6 = 1;
1709 break;
1710 case SET_DR7:
1711 dr7 = strtoul(optarg, NULL, 0);
1712 set_dr7 = 1;
1713 break;
1714 case SET_RSP:
1715 rsp = strtoul(optarg, NULL, 0);
1716 set_rsp = 1;
1717 break;
1718 case SET_RIP:
1719 rip = strtoul(optarg, NULL, 0);
1720 set_rip = 1;
1721 break;
1722 case SET_RAX:
1723 rax = strtoul(optarg, NULL, 0);
1724 set_rax = 1;
1725 break;
1726 case SET_RFLAGS:
1727 rflags = strtoul(optarg, NULL, 0);
1728 set_rflags = 1;
1729 break;
1730 case DESC_BASE:
1731 desc_base = strtoul(optarg, NULL, 0);
1732 break;
1733 case DESC_LIMIT:
1734 desc_limit = strtoul(optarg, NULL, 0);
1735 break;
1736 case DESC_ACCESS:
1737 desc_access = strtoul(optarg, NULL, 0);
1738 break;
1739 case SET_CS:
1740 cs = strtoul(optarg, NULL, 0);
1741 set_cs = 1;
1742 break;
1743 case SET_DS:
1744 ds = strtoul(optarg, NULL, 0);
1745 set_ds = 1;
1746 break;
1747 case SET_ES:
1748 es = strtoul(optarg, NULL, 0);
1749 set_es = 1;
1750 break;
1751 case SET_FS:
1752 fs = strtoul(optarg, NULL, 0);
1753 set_fs = 1;
1754 break;
1755 case SET_GS:
1756 gs = strtoul(optarg, NULL, 0);
1757 set_gs = 1;
1758 break;
1759 case SET_SS:
1760 ss = strtoul(optarg, NULL, 0);
1761 set_ss = 1;
1762 break;
1763 case SET_TR:
1764 tr = strtoul(optarg, NULL, 0);
1765 set_tr = 1;
1766 break;
1767 case SET_LDTR:
1768 ldtr = strtoul(optarg, NULL, 0);
1769 set_ldtr = 1;
1770 break;
1771 case SET_X2APIC_STATE:
1772 x2apic_state = strtol(optarg, NULL, 0);
1773 set_x2apic_state = 1;
1774 break;
1775 case SET_CAP:
1776 capval = strtoul(optarg, NULL, 0);
1777 setcap = 1;
1778 break;
1779 case SET_RTC_TIME:
1780 rtc_secs = strtoul(optarg, NULL, 0);
1781 set_rtc_time = 1;
1782 break;
1783 case SET_RTC_NVRAM:
1784 rtc_nvram_value = (uint8_t)strtoul(optarg, NULL, 0);
1785 set_rtc_nvram = 1;
1786 break;
1787 case RTC_NVRAM_OFFSET:
1788 rtc_nvram_offset = strtoul(optarg, NULL, 0);
1789 break;
1790 case GET_GPA_PMAP:
1791 gpa_pmap = strtoul(optarg, NULL, 0);
1792 get_gpa_pmap = 1;
1793 break;
1794 case CAPNAME:
1795 capname = optarg;
1796 break;
1797 case UNASSIGN_PPTDEV:
1798 unassign_pptdev = 1;
1799 if (sscanf(optarg, "%d/%d/%d", &bus, &slot, &func) != 3)
1800 usage(opts);
1801 break;
1802 case ASSERT_LAPIC_LVT:
1803 assert_lapic_lvt = atoi(optarg);
1804 break;
1805 #ifdef BHYVE_SNAPSHOT
1806 case SET_CHECKPOINT_FILE:
1807 case SET_SUSPEND_FILE:
1808 if (checkpoint_file != NULL)
1809 usage(opts);
1810
1811 checkpoint_file = optarg;
1812 vm_suspend_opt = (ch == SET_SUSPEND_FILE);
1813 break;
1814 #endif
1815 default:
1816 usage(opts);
1817 }
1818 }
1819 argc -= optind;
1820 argv += optind;
1821
1822 if (vmname == NULL)
1823 usage(opts);
1824
1825 error = 0;
1826
1827 if (!error && create)
1828 error = vm_create(vmname);
1829
1830 if (!error) {
1831 ctx = vm_open(vmname);
1832 if (ctx == NULL) {
1833 fprintf(stderr,
1834 "vm_open: %s could not be opened: %s\n",
1835 vmname, strerror(errno));
1836 exit (1);
1837 }
1838 vcpu = vm_vcpu_open(ctx, vcpuid);
1839 }
1840
1841 if (!error && memsize)
1842 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
1843
1844 if (!error && set_efer)
1845 error = vm_set_register(vcpu, VM_REG_GUEST_EFER, efer);
1846
1847 if (!error && set_cr0)
1848 error = vm_set_register(vcpu, VM_REG_GUEST_CR0, cr0);
1849
1850 if (!error && set_cr2)
1851 error = vm_set_register(vcpu, VM_REG_GUEST_CR2, cr2);
1852
1853 if (!error && set_cr3)
1854 error = vm_set_register(vcpu, VM_REG_GUEST_CR3, cr3);
1855
1856 if (!error && set_cr4)
1857 error = vm_set_register(vcpu, VM_REG_GUEST_CR4, cr4);
1858
1859 if (!error && set_dr0)
1860 error = vm_set_register(vcpu, VM_REG_GUEST_DR0, dr0);
1861
1862 if (!error && set_dr1)
1863 error = vm_set_register(vcpu, VM_REG_GUEST_DR1, dr1);
1864
1865 if (!error && set_dr2)
1866 error = vm_set_register(vcpu, VM_REG_GUEST_DR2, dr2);
1867
1868 if (!error && set_dr3)
1869 error = vm_set_register(vcpu, VM_REG_GUEST_DR3, dr3);
1870
1871 if (!error && set_dr6)
1872 error = vm_set_register(vcpu, VM_REG_GUEST_DR6, dr6);
1873
1874 if (!error && set_dr7)
1875 error = vm_set_register(vcpu, VM_REG_GUEST_DR7, dr7);
1876
1877 if (!error && set_rsp)
1878 error = vm_set_register(vcpu, VM_REG_GUEST_RSP, rsp);
1879
1880 if (!error && set_rip)
1881 error = vm_set_register(vcpu, VM_REG_GUEST_RIP, rip);
1882
1883 if (!error && set_rax)
1884 error = vm_set_register(vcpu, VM_REG_GUEST_RAX, rax);
1885
1886 if (!error && set_rflags) {
1887 error = vm_set_register(vcpu, VM_REG_GUEST_RFLAGS,
1888 rflags);
1889 }
1890
1891 if (!error && set_desc_ds) {
1892 error = vm_set_desc(vcpu, VM_REG_GUEST_DS,
1893 desc_base, desc_limit, desc_access);
1894 }
1895
1896 if (!error && set_desc_es) {
1897 error = vm_set_desc(vcpu, VM_REG_GUEST_ES,
1898 desc_base, desc_limit, desc_access);
1899 }
1900
1901 if (!error && set_desc_ss) {
1902 error = vm_set_desc(vcpu, VM_REG_GUEST_SS,
1903 desc_base, desc_limit, desc_access);
1904 }
1905
1906 if (!error && set_desc_cs) {
1907 error = vm_set_desc(vcpu, VM_REG_GUEST_CS,
1908 desc_base, desc_limit, desc_access);
1909 }
1910
1911 if (!error && set_desc_fs) {
1912 error = vm_set_desc(vcpu, VM_REG_GUEST_FS,
1913 desc_base, desc_limit, desc_access);
1914 }
1915
1916 if (!error && set_desc_gs) {
1917 error = vm_set_desc(vcpu, VM_REG_GUEST_GS,
1918 desc_base, desc_limit, desc_access);
1919 }
1920
1921 if (!error && set_desc_tr) {
1922 error = vm_set_desc(vcpu, VM_REG_GUEST_TR,
1923 desc_base, desc_limit, desc_access);
1924 }
1925
1926 if (!error && set_desc_ldtr) {
1927 error = vm_set_desc(vcpu, VM_REG_GUEST_LDTR,
1928 desc_base, desc_limit, desc_access);
1929 }
1930
1931 if (!error && set_desc_gdtr) {
1932 error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR,
1933 desc_base, desc_limit, 0);
1934 }
1935
1936 if (!error && set_desc_idtr) {
1937 error = vm_set_desc(vcpu, VM_REG_GUEST_IDTR,
1938 desc_base, desc_limit, 0);
1939 }
1940
1941 if (!error && set_cs)
1942 error = vm_set_register(vcpu, VM_REG_GUEST_CS, cs);
1943
1944 if (!error && set_ds)
1945 error = vm_set_register(vcpu, VM_REG_GUEST_DS, ds);
1946
1947 if (!error && set_es)
1948 error = vm_set_register(vcpu, VM_REG_GUEST_ES, es);
1949
1950 if (!error && set_fs)
1951 error = vm_set_register(vcpu, VM_REG_GUEST_FS, fs);
1952
1953 if (!error && set_gs)
1954 error = vm_set_register(vcpu, VM_REG_GUEST_GS, gs);
1955
1956 if (!error && set_ss)
1957 error = vm_set_register(vcpu, VM_REG_GUEST_SS, ss);
1958
1959 if (!error && set_tr)
1960 error = vm_set_register(vcpu, VM_REG_GUEST_TR, tr);
1961
1962 if (!error && set_ldtr)
1963 error = vm_set_register(vcpu, VM_REG_GUEST_LDTR, ldtr);
1964
1965 if (!error && set_x2apic_state)
1966 error = vm_set_x2apic_state(vcpu, x2apic_state);
1967
1968 if (!error && unassign_pptdev)
1969 error = vm_unassign_pptdev(ctx, bus, slot, func);
1970
1971 if (!error && inject_nmi) {
1972 error = vm_inject_nmi(vcpu);
1973 }
1974
1975 if (!error && assert_lapic_lvt != -1) {
1976 error = vm_lapic_local_irq(vcpu, assert_lapic_lvt);
1977 }
1978
1979 if (!error && (get_memseg || get_all))
1980 error = show_memseg(ctx);
1981
1982 if (!error && (get_memmap || get_all))
1983 error = show_memmap(ctx);
1984
1985 if (!error)
1986 error = get_all_registers(vcpu, vcpuid);
1987
1988 if (!error)
1989 error = get_all_segments(vcpu, vcpuid);
1990
1991 if (!error) {
1992 if (cpu_intel)
1993 error = get_misc_vmcs(vcpu, vcpuid);
1994 else
1995 error = get_misc_vmcb(vcpu, vcpuid);
1996 }
1997
1998 if (!error && (get_x2apic_state || get_all)) {
1999 error = vm_get_x2apic_state(vcpu, &x2apic_state);
2000 if (error == 0)
2001 printf("x2apic_state[%d]\t%d\n", vcpuid, x2apic_state);
2002 }
2003
2004 if (!error && (get_eptp || get_all)) {
2005 if (cpu_intel)
2006 error = vm_get_vmcs_field(vcpu, VMCS_EPTP, &eptp);
2007 else
2008 error = vm_get_vmcb_field(vcpu, VMCB_OFF_NPT_BASE,
2009 8, &eptp);
2010 if (error == 0)
2011 printf("%s[%d]\t\t0x%016lx\n",
2012 cpu_intel ? "eptp" : "rvi/npt", vcpuid, eptp);
2013 }
2014
2015 if (!error && (get_exception_bitmap || get_all)) {
2016 if(cpu_intel)
2017 error = vm_get_vmcs_field(vcpu,
2018 VMCS_EXCEPTION_BITMAP, &bm);
2019 else
2020 error = vm_get_vmcb_field(vcpu,
2021 VMCB_OFF_EXC_INTERCEPT,
2022 4, &bm);
2023 if (error == 0)
2024 printf("exception_bitmap[%d]\t%#lx\n", vcpuid, bm);
2025 }
2026
2027 if (!error && (get_io_bitmap || get_all)) {
2028 if (cpu_intel) {
2029 error = vm_get_vmcs_field(vcpu, VMCS_IO_BITMAP_A,
2030 &bm);
2031 if (error == 0)
2032 printf("io_bitmap_a[%d]\t%#lx\n", vcpuid, bm);
2033 error = vm_get_vmcs_field(vcpu, VMCS_IO_BITMAP_B,
2034 &bm);
2035 if (error == 0)
2036 printf("io_bitmap_b[%d]\t%#lx\n", vcpuid, bm);
2037 } else {
2038 error = vm_get_vmcb_field(vcpu,
2039 VMCB_OFF_IO_PERM, 8, &bm);
2040 if (error == 0)
2041 printf("io_bitmap[%d]\t%#lx\n", vcpuid, bm);
2042 }
2043 }
2044
2045 if (!error && (get_tsc_offset || get_all)) {
2046 uint64_t tscoff;
2047 if (cpu_intel)
2048 error = vm_get_vmcs_field(vcpu, VMCS_TSC_OFFSET,
2049 &tscoff);
2050 else
2051 error = vm_get_vmcb_field(vcpu,
2052 VMCB_OFF_TSC_OFFSET,
2053 8, &tscoff);
2054 if (error == 0)
2055 printf("tsc_offset[%d]\t0x%016lx\n", vcpuid, tscoff);
2056 }
2057
2058 if (!error && (get_msr_bitmap_address || get_all)) {
2059 if (cpu_intel)
2060 error = vm_get_vmcs_field(vcpu, VMCS_MSR_BITMAP,
2061 &addr);
2062 else
2063 error = vm_get_vmcb_field(vcpu,
2064 VMCB_OFF_MSR_PERM, 8, &addr);
2065 if (error == 0)
2066 printf("msr_bitmap[%d]\t\t%#lx\n", vcpuid, addr);
2067 }
2068
2069 if (!error && (get_msr_bitmap || get_all)) {
2070 if (cpu_intel) {
2071 error = vm_get_vmcs_field(vcpu,
2072 VMCS_MSR_BITMAP, &addr);
2073 } else {
2074 error = vm_get_vmcb_field(vcpu,
2075 VMCB_OFF_MSR_PERM, 8,
2076 &addr);
2077 }
2078
2079 if (error == 0)
2080 error = dump_msr_bitmap(vcpuid, addr, cpu_intel);
2081 }
2082
2083 if (!error && (get_vpid_asid || get_all)) {
2084 uint64_t vpid;
2085 if (cpu_intel)
2086 error = vm_get_vmcs_field(vcpu, VMCS_VPID, &vpid);
2087 else
2088 error = vm_get_vmcb_field(vcpu, VMCB_OFF_ASID,
2089 4, &vpid);
2090 if (error == 0)
2091 printf("%s[%d]\t\t0x%04lx\n",
2092 cpu_intel ? "vpid" : "asid", vcpuid, vpid);
2093 }
2094
2095 if (!error && (get_guest_pat || get_all)) {
2096 if (cpu_intel)
2097 error = vm_get_vmcs_field(vcpu,
2098 VMCS_GUEST_IA32_PAT, &pat);
2099 else
2100 error = vm_get_vmcb_field(vcpu,
2101 VMCB_OFF_GUEST_PAT, 8, &pat);
2102 if (error == 0)
2103 printf("guest_pat[%d]\t\t0x%016lx\n", vcpuid, pat);
2104 }
2105
2106 if (!error && (get_guest_sysenter || get_all)) {
2107 if (cpu_intel)
2108 error = vm_get_vmcs_field(vcpu,
2109 VMCS_GUEST_IA32_SYSENTER_CS,
2110 &cs);
2111 else
2112 error = vm_get_vmcb_field(vcpu,
2113 VMCB_OFF_SYSENTER_CS, 8,
2114 &cs);
2115
2116 if (error == 0)
2117 printf("guest_sysenter_cs[%d]\t%#lx\n", vcpuid, cs);
2118 if (cpu_intel)
2119 error = vm_get_vmcs_field(vcpu,
2120 VMCS_GUEST_IA32_SYSENTER_ESP,
2121 &rsp);
2122 else
2123 error = vm_get_vmcb_field(vcpu,
2124 VMCB_OFF_SYSENTER_ESP, 8,
2125 &rsp);
2126
2127 if (error == 0)
2128 printf("guest_sysenter_sp[%d]\t%#lx\n", vcpuid, rsp);
2129 if (cpu_intel)
2130 error = vm_get_vmcs_field(vcpu,
2131 VMCS_GUEST_IA32_SYSENTER_EIP,
2132 &rip);
2133 else
2134 error = vm_get_vmcb_field(vcpu,
2135 VMCB_OFF_SYSENTER_EIP, 8,
2136 &rip);
2137 if (error == 0)
2138 printf("guest_sysenter_ip[%d]\t%#lx\n", vcpuid, rip);
2139 }
2140
2141 if (!error && (get_exit_reason || get_all)) {
2142 if (cpu_intel)
2143 error = vm_get_vmcs_field(vcpu, VMCS_EXIT_REASON,
2144 &u64);
2145 else
2146 error = vm_get_vmcb_field(vcpu,
2147 VMCB_OFF_EXIT_REASON, 8,
2148 &u64);
2149 if (error == 0)
2150 printf("exit_reason[%d]\t%#lx\n", vcpuid, u64);
2151 }
2152
2153 if (!error && setcap) {
2154 int captype;
2155 captype = vm_capability_name2type(capname);
2156 error = vm_set_capability(vcpu, captype, capval);
2157 if (error != 0 && errno == ENOENT)
2158 printf("Capability \"%s\" is not available\n", capname);
2159 }
2160
2161 if (!error && get_gpa_pmap) {
2162 error = vm_get_gpa_pmap(ctx, gpa_pmap, pteval, &ptenum);
2163 if (error == 0) {
2164 printf("gpa %#lx:", gpa_pmap);
2165 pte = &pteval[0];
2166 while (ptenum-- > 0)
2167 printf(" %#lx", *pte++);
2168 printf("\n");
2169 }
2170 }
2171
2172 if (!error && set_rtc_nvram)
2173 error = vm_rtc_write(ctx, rtc_nvram_offset, rtc_nvram_value);
2174
2175 if (!error && (get_rtc_nvram || get_all)) {
2176 error = vm_rtc_read(ctx, rtc_nvram_offset, &rtc_nvram_value);
2177 if (error == 0) {
2178 printf("rtc nvram[%03d]: 0x%02x\n", rtc_nvram_offset,
2179 rtc_nvram_value);
2180 }
2181 }
2182
2183 if (!error && set_rtc_time)
2184 error = vm_rtc_settime(ctx, rtc_secs);
2185
2186 if (!error && (get_rtc_time || get_all)) {
2187 error = vm_rtc_gettime(ctx, &rtc_secs);
2188 if (error == 0) {
2189 gmtime_r(&rtc_secs, &tm);
2190 printf("rtc time %#lx: %s %s %02d %02d:%02d:%02d %d\n",
2191 rtc_secs, wday_str(tm.tm_wday), mon_str(tm.tm_mon),
2192 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
2193 1900 + tm.tm_year);
2194 }
2195 }
2196
2197 if (!error && (getcap || get_all)) {
2198 int captype, val, getcaptype;
2199
2200 if (getcap && capname)
2201 getcaptype = vm_capability_name2type(capname);
2202 else
2203 getcaptype = -1;
2204
2205 for (captype = 0; captype < VM_CAP_MAX; captype++) {
2206 if (getcaptype >= 0 && captype != getcaptype)
2207 continue;
2208 error = vm_get_capability(vcpu, captype, &val);
2209 if (error == 0) {
2210 printf("Capability \"%s\" is %s on vcpu %d\n",
2211 vm_capability_type2name(captype),
2212 val ? "set" : "not set", vcpuid);
2213 } else if (errno == ENOENT) {
2214 error = 0;
2215 printf("Capability \"%s\" is not available\n",
2216 vm_capability_type2name(captype));
2217 } else {
2218 break;
2219 }
2220 }
2221 }
2222
2223 if (!error && (get_active_cpus || get_all)) {
2224 error = vm_active_cpus(ctx, &cpus);
2225 if (!error)
2226 print_cpus("active cpus", &cpus);
2227 }
2228
2229 if (!error && (get_debug_cpus || get_all)) {
2230 error = vm_debug_cpus(ctx, &cpus);
2231 if (!error)
2232 print_cpus("debug cpus", &cpus);
2233 }
2234
2235 if (!error && (get_suspended_cpus || get_all)) {
2236 error = vm_suspended_cpus(ctx, &cpus);
2237 if (!error)
2238 print_cpus("suspended cpus", &cpus);
2239 }
2240
2241 if (!error && (get_intinfo || get_all)) {
2242 error = vm_get_intinfo(vcpu, &info[0], &info[1]);
2243 if (!error) {
2244 print_intinfo("pending", info[0]);
2245 print_intinfo("current", info[1]);
2246 }
2247 }
2248
2249 if (!error && (get_stats || get_all)) {
2250 int i, num_stats;
2251 uint64_t *stats;
2252 struct timeval tv;
2253 const char *desc;
2254
2255 stats = vm_get_stats(vcpu, &tv, &num_stats);
2256 if (stats != NULL) {
2257 printf("vcpu%d stats:\n", vcpuid);
2258 for (i = 0; i < num_stats; i++) {
2259 desc = vm_get_stat_desc(ctx, i);
2260 printf("%-40s\t%ld\n", desc, stats[i]);
2261 }
2262 }
2263 }
2264
2265 if (!error && (get_cpu_topology || get_all)) {
2266 uint16_t sockets, cores, threads, maxcpus;
2267
2268 vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
2269 printf("cpu_topology:\tsockets=%hu, cores=%hu, threads=%hu, "
2270 "maxcpus=%hu\n", sockets, cores, threads, maxcpus);
2271 }
2272
2273 if (!error && run) {
2274 struct vm_exit vmexit;
2275 cpuset_t cpuset;
2276
2277 vmrun.vm_exit = &vmexit;
2278 vmrun.cpuset = &cpuset;
2279 vmrun.cpusetsize = sizeof(cpuset);
2280 error = vm_run(vcpu, &vmrun);
2281 if (error == 0)
2282 dump_vm_run_exitcode(&vmexit, vcpuid);
2283 else
2284 printf("vm_run error %d\n", error);
2285 }
2286
2287 if (!error && force_reset)
2288 error = vm_suspend(ctx, VM_SUSPEND_RESET);
2289
2290 if (!error && force_poweroff)
2291 error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
2292
2293 if (error)
2294 printf("errno = %d\n", errno);
2295
2296 if (!error && destroy)
2297 vm_destroy(ctx);
2298
2299 #ifdef BHYVE_SNAPSHOT
2300 if (!error && checkpoint_file)
2301 error = snapshot_request(vmname, checkpoint_file, vm_suspend_opt);
2302 #endif
2303
2304 free (opts);
2305 exit(error);
2306 }
2307