2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm_debug.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_host_events.h>
24 #include <palacios/vm_guest.h>
25 #include <palacios/vmm_decoder.h>
26 #include <palacios/vm_guest_mem.h>
27 #include <palacios/vmm_config.h>
29 #define PRINT_TELEMETRY 1
30 #define PRINT_CORE_STATE 2
31 #define PRINT_ARCH_STATE 3
33 #define PRINT_BACKTRACE 5
36 #define PRINT_ALL 100 // Absolutely everything
37 #define PRINT_STATE 101 // telemetry, core state, arch state
42 static int core_handler(struct guest_info * core, uint32_t cmd) {
47 v3_print_core_telemetry(core);
50 case PRINT_CORE_STATE:
51 v3_raise_barrier(core->vm_info, NULL);
53 v3_print_guest_state(core);
55 v3_lower_barrier(core->vm_info);
57 case PRINT_ARCH_STATE:
58 v3_raise_barrier(core->vm_info, NULL);
60 v3_print_arch_state(core);
62 v3_lower_barrier(core->vm_info);
65 v3_raise_barrier(core->vm_info, NULL);
69 v3_lower_barrier(core->vm_info);
72 v3_raise_barrier(core->vm_info, NULL);
74 v3_print_backtrace(core);
76 v3_lower_barrier(core->vm_info);
80 v3_raise_barrier(core->vm_info, NULL);
82 v3_print_core_telemetry(core);
83 v3_print_guest_state(core);
84 v3_print_arch_state(core);
86 v3_lower_barrier(core->vm_info);
95 static int evt_handler(struct v3_vm_info * vm, struct v3_debug_event * evt, void * priv_data) {
97 V3_Print("Debug Event Handler for core %d\n", evt->core_id);
99 if (evt->core_id == -1) {
101 for (i = 0; i < vm->num_cores; i++) {
102 core_handler(&(vm->cores[i]), evt->cmd);
105 return core_handler(&vm->cores[evt->core_id], evt->cmd);
113 int v3_init_vm_debugging(struct v3_vm_info * vm) {
114 v3_hook_host_event(vm, HOST_DEBUG_EVT,
115 V3_HOST_EVENT_HANDLER(evt_handler),
126 void v3_print_segments(struct v3_segments * segs) {
128 struct v3_segment * seg_ptr;
130 seg_ptr=(struct v3_segment *)segs;
132 char *seg_names[] = {"CS", "DS" , "ES", "FS", "GS", "SS" , "LDTR", "GDTR", "IDTR", "TR", NULL};
133 V3_Print("Segments\n");
135 for (i = 0; seg_names[i] != NULL; i++) {
137 V3_Print("\t%s: Sel=%x, base=%p, limit=%x (long_mode=%d, db=%d)\n", seg_names[i], seg_ptr[i].selector,
138 (void *)(addr_t)seg_ptr[i].base, seg_ptr[i].limit,
139 seg_ptr[i].long_mode, seg_ptr[i].db);
146 void v3_print_ctrl_regs(struct guest_info * core) {
147 struct v3_ctrl_regs * regs = &(core->ctrl_regs);
150 char * reg_names[] = {"CR0", "CR2", "CR3", "CR4", "CR8", "FLAGS", "EFER", NULL};
153 reg_ptr = (v3_reg_t *)regs;
155 V3_Print("Ctrl Regs:\n");
157 for (i = 0; reg_names[i] != NULL; i++) {
158 V3_Print("\t%s=0x%p (at %p)\n", reg_names[i], (void *)(addr_t)reg_ptr[i], &(reg_ptr[i]));
165 static int safe_gva_to_hva(struct guest_info * core, addr_t linear_addr, addr_t * host_addr) {
166 /* select the proper translation based on guest mode */
167 if (core->mem_mode == PHYSICAL_MEM) {
168 if (v3_gpa_to_hva(core, linear_addr, host_addr) == -1) return -1;
169 } else if (core->mem_mode == VIRTUAL_MEM) {
170 if (v3_gva_to_hva(core, linear_addr, host_addr) == -1) return -1;
175 static int v3_print_disassembly(struct guest_info * core) {
177 addr_t rip, rip_linear, rip_host;
179 /* we don't know where the instructions preceding RIP start, so we just take
180 * a guess and hope the instruction stream synced up with our disassembly
181 * some time before RIP; if it has not we correct RIP at that point
184 /* start disassembly 64 bytes before current RIP, continue 32 bytes after */
185 rip = (addr_t) core->rip - 64;
186 while ((int) (rip - core->rip) < 32) {
187 V3_Print("disassembly step\n");
189 /* always print RIP, even if the instructions before were bad */
190 if (!passed_rip && rip >= core->rip) {
191 if (rip != core->rip) {
192 V3_Print("***** bad disassembly up to this point *****\n");
198 /* look up host virtual address for this instruction */
199 rip_linear = get_addr_linear(core, rip, &(core->segments.cs));
200 if (safe_gva_to_hva(core, rip_linear, &rip_host) < 0) {
205 /* print disassembled instrcution (updates rip) */
206 if (v3_disasm(core, (void *) rip_host, &rip, rip == core->rip) < 0) {
218 void v3_print_guest_state(struct guest_info * core) {
219 addr_t linear_addr = 0;
221 V3_Print("RIP: %p\n", (void *)(addr_t)(core->rip));
222 linear_addr = get_addr_linear(core, core->rip, &(core->segments.cs));
223 V3_Print("RIP Linear: %p\n", (void *)linear_addr);
225 V3_Print("NumExits: %u\n", (uint32_t)core->num_exits);
227 V3_Print("IRQ STATE: started=%d, pending=%d\n",
228 core->intr_core_state.irq_started,
229 core->intr_core_state.irq_pending);
230 V3_Print("EXCP STATE: err_code_valid=%d, err_code=%x\n",
231 core->excp_state.excp_error_code_valid,
232 core->excp_state.excp_error_code);
235 v3_print_segments(&(core->segments));
236 v3_print_ctrl_regs(core);
238 if (core->shdw_pg_mode == SHADOW_PAGING) {
239 V3_Print("Shadow Paging Guest Registers:\n");
240 V3_Print("\tGuest CR0=%p\n", (void *)(addr_t)(core->shdw_pg_state.guest_cr0));
241 V3_Print("\tGuest CR3=%p\n", (void *)(addr_t)(core->shdw_pg_state.guest_cr3));
242 V3_Print("\tGuest EFER=%p\n", (void *)(addr_t)(core->shdw_pg_state.guest_efer.value));
247 v3_print_mem_map(core->vm_info);
249 v3_print_stack(core);
251 // v3_print_disassembly(core);
255 void v3_print_arch_state(struct guest_info * core) {
261 void v3_print_guest_state_all(struct v3_vm_info * vm) {
264 V3_Print("VM Core states for %s\n", vm->name);
266 for (i = 0; i < 80; i++) {
270 for (i = 0; i < vm->num_cores; i++) {
271 v3_print_guest_state(&vm->cores[i]);
274 for (i = 0; i < 80; i++) {
283 void v3_print_stack(struct guest_info * core) {
284 addr_t linear_addr = 0;
285 addr_t host_addr = 0;
287 v3_cpu_mode_t cpu_mode = v3_get_vm_cpu_mode(core);
289 linear_addr = get_addr_linear(core, core->vm_regs.rsp, &(core->segments.ss));
291 V3_Print("Stack at %p:\n", (void *)linear_addr);
293 if (core->mem_mode == PHYSICAL_MEM) {
294 if (v3_gpa_to_hva(core, linear_addr, &host_addr) == -1) {
295 PrintError("Could not translate Stack address\n");
298 } else if (core->mem_mode == VIRTUAL_MEM) {
299 if (v3_gva_to_hva(core, linear_addr, &host_addr) == -1) {
300 PrintError("Could not translate Virtual Stack address\n");
305 V3_Print("Host Address of rsp = 0x%p\n", (void *)host_addr);
307 // We start i at one because the current stack pointer points to an unused stack element
308 for (i = 0; i <= 24; i++) {
310 if (cpu_mode == REAL) {
311 V3_Print("\t0x%.4x\n", *((uint16_t *)host_addr + (i * 2)));
312 } else if (cpu_mode == LONG) {
313 V3_Print("\t%p\n", (void *)*(addr_t *)(host_addr + (i * 8)));
316 V3_Print("\t0x%.8x\n", *(uint32_t *)(host_addr + (i * 4)));
323 void v3_print_backtrace(struct guest_info * core) {
326 v3_cpu_mode_t cpu_mode = v3_get_vm_cpu_mode(core);
327 struct v3_cfg_file * system_map = v3_cfg_get_file(core->vm_info, "System.map");
329 V3_Print("Performing Backtrace for Core %d\n", core->vcpu_id);
330 V3_Print("\tRSP=%p, RBP=%p\n", (void *)core->vm_regs.rsp, (void *)core->vm_regs.rbp);
332 gla_rbp = get_addr_linear(core, core->vm_regs.rbp, &(core->segments.ss));
335 for (i = 0; i < 30; i++) {
338 char * sym_name = NULL;
341 if (core->mem_mode == PHYSICAL_MEM) {
342 if (v3_gpa_to_hva(core, gla_rbp, &hva_rbp) == -1) {
343 PrintError("Could not translate Stack address\n");
346 } else if (core->mem_mode == VIRTUAL_MEM) {
347 if (v3_gva_to_hva(core, gla_rbp, &hva_rbp) == -1) {
348 PrintError("Could not translate Virtual Stack address\n");
354 hva_rip = hva_rbp + v3_get_addr_width(core);
356 if (cpu_mode == REAL) {
357 rip_val = (addr_t)*(uint16_t *)hva_rip;
358 } else if (cpu_mode == LONG) {
359 rip_val = (addr_t)*(uint64_t *)hva_rip;
361 rip_val = (addr_t)*(uint32_t *)hva_rip;
365 char * tmp_ptr = system_map->data;
366 char * sym_ptr = NULL;
367 uint64_t file_offset = 0;
368 uint64_t sym_offset = 0;
370 while (file_offset < system_map->size) {
371 sym_offset = strtox(tmp_ptr, &tmp_ptr);
373 tmp_ptr += 3; // pass over symbol type
375 if (sym_offset > rip_val) {
376 char * end_ptr = strchr(sym_ptr, '\n');
379 *end_ptr = 0; // null terminate symbol...
388 char * end_ptr2 = strchr(tmp_ptr, '\n');
391 tmp_ptr += strlen(tmp_ptr) + 1;
393 tmp_ptr = end_ptr2 + 1;
403 if (cpu_mode == REAL) {
404 V3_Print("Next RBP=0x%.4x, RIP=0x%.4x (%s)\n",
405 *(uint16_t *)hva_rbp,*(uint16_t *)hva_rip,
408 gla_rbp = *(uint16_t *)hva_rbp;
409 } else if (cpu_mode == LONG) {
410 V3_Print("Next RBP=%p, RIP=%p (%s)\n",
411 (void *)*(uint64_t *)hva_rbp, (void *)*(uint64_t *)hva_rip,
413 gla_rbp = *(uint64_t *)hva_rbp;
415 V3_Print("Next RBP=0x%.8x, RIP=0x%.8x (%s)\n",
416 *(uint32_t *)hva_rbp, *(uint32_t *)hva_rip,
418 gla_rbp = *(uint32_t *)hva_rbp;
427 void v3_print_GPRs(struct guest_info * core) {
428 struct v3_gprs * regs = &(core->vm_regs);
431 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", NULL};
433 reg_ptr = (v3_reg_t *)regs;
435 V3_Print("32 bit GPRs:\n");
437 for (i = 0; reg_names[i] != NULL; i++) {
438 V3_Print("\t%s=0x%p (at %p)\n", reg_names[i], (void *)(addr_t)reg_ptr[i], &(reg_ptr[i]));
444 void v3_print_GPRs(struct guest_info * core) {
445 struct v3_gprs * regs = &(core->vm_regs);
448 char * reg_names[] = { "RDI", "RSI", "RBP", "RSP", "RBX", "RDX", "RCX", "RAX", \
449 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", NULL};
451 reg_ptr = (v3_reg_t *)regs;
453 V3_Print("64 bit GPRs:\n");
455 for (i = 0; reg_names[i] != NULL; i++) {
456 V3_Print("\t%s=0x%p (at %p)\n", reg_names[i], (void *)(addr_t)reg_ptr[i], &(reg_ptr[i]));