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 <devices/os_debug.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vm_guest_mem.h>
27 #define DEBUG_PORT1 0xc0c0
28 #define DEBUG_HCALL 0xc0c0
31 char debug_buf[BUF_SIZE];
37 static int handle_gen_write(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
38 struct debug_state * state = (struct debug_state *)dev->private_data;
40 state->debug_buf[state->debug_offset++] = *(char*)src;
42 if ((*(char*)src == 0xa) || (state->debug_offset == (BUF_SIZE - 1))) {
43 PrintDebug("VM_CONSOLE>%s", state->debug_buf);
44 memset(state->debug_buf, 0, BUF_SIZE);
45 state->debug_offset = 0;
51 static int handle_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) {
52 struct vm_device * dev = (struct vm_device *)priv_data;
53 struct debug_state * state = (struct debug_state *)dev->private_data;
55 int msg_len = info->vm_regs.rcx;
56 addr_t msg_gpa = info->vm_regs.rbx;
58 if (msg_len >= BUF_SIZE) {
59 PrintError("Console message too large for buffer (len=%d)\n", msg_len);
63 if (read_guest_pa_memory(info, msg_gpa, msg_len, (uchar_t *)state->debug_buf) != msg_len) {
64 PrintError("Could not read debug message\n");
68 state->debug_buf[msg_len] = 0;
70 PrintDebug("VM_CONSOLE>%s\n", state->debug_buf);
76 static int debug_init(struct vm_device * dev) {
77 struct debug_state * state = (struct debug_state *)dev->private_data;
79 v3_dev_hook_io(dev, DEBUG_PORT1, NULL, &handle_gen_write);
80 v3_register_hypercall(dev->vm, DEBUG_HCALL, handle_hcall, dev);
82 state->debug_offset = 0;
83 memset(state->debug_buf, 0, BUF_SIZE);
88 static int debug_deinit(struct vm_device * dev) {
89 v3_dev_unhook_io(dev, DEBUG_PORT1);
98 static struct vm_device_ops dev_ops = {
100 .deinit = debug_deinit,
107 struct vm_device * v3_create_os_debug() {
108 struct debug_state * state = NULL;
110 state = (struct debug_state *)V3_Malloc(sizeof(struct debug_state));
112 PrintDebug("Creating OS Debug Device\n");
113 struct vm_device * device = v3_create_device("OS Debug", &dev_ops, state);