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".
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_dev_mgr.h>
24 #include <palacios/vm_guest_mem.h>
28 #define DEBUG_PORT1 0xc0c0
29 #define HEARTBEAT_PORT 0x99
32 char debug_buf[BUF_SIZE];
38 static int handle_gen_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
39 struct debug_state * state = priv_data;
41 state->debug_buf[state->debug_offset++] = *(char*)src;
43 if ((*(char*)src == 0xa) || (state->debug_offset == (BUF_SIZE - 1))) {
44 PrintDebug("VM_CONSOLE>%s", state->debug_buf);
45 memset(state->debug_buf, 0, BUF_SIZE);
46 state->debug_offset = 0;
52 static int handle_hb_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
56 val = *(uint8_t *)src;
57 } else if (length == 2) {
58 val = *(uint16_t *)src;
60 val = *(uint32_t *)src;
63 V3_Print("HEARTBEAT> %x (%d)\n", val, val);
68 static int handle_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
69 struct debug_state * state = (struct debug_state *)priv_data;
71 int msg_len = info->vm_regs.rcx;
72 addr_t msg_gpa = info->vm_regs.rbx;
73 int buf_is_va = info->vm_regs.rdx;
75 if (msg_len >= BUF_SIZE) {
76 PrintError("Console message too large for buffer (len=%d)\n", msg_len);
81 if (v3_read_gva_memory(info, msg_gpa, msg_len, (uchar_t *)state->debug_buf) != msg_len) {
82 PrintError("Could not read debug message\n");
86 if (v3_read_gpa_memory(info, msg_gpa, msg_len, (uchar_t *)state->debug_buf) != msg_len) {
87 PrintError("Could not read debug message\n");
92 state->debug_buf[msg_len] = 0;
94 PrintDebug("VM_CONSOLE>%s\n", state->debug_buf);
101 static int debug_free(struct debug_state * state) {
103 // unregister hypercall
109 #ifdef V3_CONFIG_CHECKPOINT
110 static int debug_save(struct v3_chkpt_ctx * ctx, void * private_data) {
111 struct debug_state * dbg = (struct debug_state *)private_data;
113 V3_CHKPT_STD_SAVE(ctx, dbg->debug_buf);
114 V3_CHKPT_STD_SAVE(ctx, dbg->debug_offset);
120 static int debug_load(struct v3_chkpt_ctx * ctx, void * private_data) {
121 struct debug_state * dbg = (struct debug_state *)private_data;
123 V3_CHKPT_STD_LOAD(ctx, dbg->debug_buf);
124 V3_CHKPT_STD_LOAD(ctx, dbg->debug_offset);
133 static struct v3_device_ops dev_ops = {
134 .free = (int (*)(void *))debug_free,
135 #ifdef V3_CONFIG_CHECKPOINT
144 static int debug_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
145 struct debug_state * state = NULL;
146 char * dev_id = v3_cfg_val(cfg, "ID");
148 state = (struct debug_state *)V3_Malloc(sizeof(struct debug_state));
150 PrintDebug("Creating OS Debug Device\n");
152 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
155 PrintError("Could not attach device %s\n", dev_id);
160 if (v3_dev_hook_io(dev, DEBUG_PORT1, NULL, &handle_gen_write) == -1) {
161 PrintError("Error hooking OS debug IO port\n");
162 v3_remove_device(dev);
167 if (v3_dev_hook_io(dev, HEARTBEAT_PORT, NULL, &handle_hb_write) == -1) {
168 PrintError("error hooking OS heartbeat port\n");
169 v3_remove_device(dev);
173 v3_register_hypercall(vm, OS_DEBUG_HCALL, handle_hcall, state);
175 state->debug_offset = 0;
176 memset(state->debug_buf, 0, BUF_SIZE);
182 device_register("OS_DEBUG", debug_init)