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>
27 #define BOCHS_PORT1 0x400
28 #define BOCHS_PORT2 0x401
29 #define BOCHS_INFO_PORT 0x402
30 #define BOCHS_DEBUG_PORT 0x403
32 #define BOCHS_CONSOLE_PORT 0xe9
36 char debug_buf[BUF_SIZE];
39 char info_buf[BUF_SIZE];
42 char cons_buf[BUF_SIZE];
46 static int handle_info_write(struct guest_info * core, ushort_t port, void * src, uint_t length, struct vm_device * dev) {
47 struct debug_state * state = (struct debug_state *)dev->private_data;
49 state->info_buf[state->info_offset++] = *(char*)src;
51 if ((*(char*)src == 0xa) || (state->info_offset == (BUF_SIZE - 1))) {
52 PrintDebug("BOCHSINFO>%s", state->info_buf);
53 memset(state->info_buf, 0, BUF_SIZE);
54 state->info_offset = 0;
61 static int handle_debug_write(struct guest_info * core, ushort_t port, void * src, uint_t length, struct vm_device * dev) {
62 struct debug_state * state = (struct debug_state *)dev->private_data;
64 state->debug_buf[state->debug_offset++] = *(char*)src;
66 if ((*(char*)src == 0xa) || (state->debug_offset == (BUF_SIZE - 1))) {
67 PrintDebug("BOCHSDEBUG>%s", state->debug_buf);
68 memset(state->debug_buf, 0, BUF_SIZE);
69 state->debug_offset = 0;
76 static int handle_console_write(struct guest_info * core, ushort_t port, void * src, uint_t length, struct vm_device * dev) {
77 struct debug_state * state = (struct debug_state *)dev->private_data;
79 state->cons_buf[state->cons_offset++] = *(char*)src;
81 if ((*(char*)src == 0xa) || (state->cons_offset == (BUF_SIZE - 1))) {
82 PrintDebug("BOCHSCONSOLE>%s", state->cons_buf);
83 memset(state->cons_buf, 0, BUF_SIZE);
84 state->cons_offset = 0;
91 static int handle_gen_write(struct guest_info * core, ushort_t port, void * src, uint_t length, struct vm_device * dev) {
95 PrintDebug(">0x%.2x\n", *(uchar_t*)src);
98 PrintDebug(">0x%.4x\n", *(ushort_t*)src);
101 PrintDebug(">0x%.8x\n", *(uint_t*)src);
104 PrintError("Invalid length in handle_gen_write\n");
115 static int debug_free(struct vm_device * dev) {
116 v3_dev_unhook_io(dev, BOCHS_PORT1);
117 v3_dev_unhook_io(dev, BOCHS_PORT2);
118 v3_dev_unhook_io(dev, BOCHS_INFO_PORT);
119 v3_dev_unhook_io(dev, BOCHS_DEBUG_PORT);
127 static struct v3_device_ops dev_ops = {
137 static int debug_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
138 struct debug_state * state = NULL;
139 char * name = v3_cfg_val(cfg, "name");
141 state = (struct debug_state *)V3_Malloc(sizeof(struct debug_state));
143 V3_ASSERT(state != NULL);
145 PrintDebug("Creating Bochs Debug Device\n");
146 struct vm_device * dev = v3_allocate_device(name, &dev_ops, state);
148 if (v3_attach_device(vm, dev) == -1) {
149 PrintError("Could not attach device %s\n", name);
153 state->debug_offset = 0;
154 state->info_offset = 0;
155 memset(state->debug_buf, 0, BUF_SIZE);
156 memset(state->info_buf, 0, BUF_SIZE);
159 v3_dev_hook_io(dev, BOCHS_PORT1, NULL, &handle_gen_write);
160 v3_dev_hook_io(dev, BOCHS_PORT2, NULL, &handle_gen_write);
161 v3_dev_hook_io(dev, BOCHS_INFO_PORT, NULL, &handle_info_write);
162 v3_dev_hook_io(dev, BOCHS_DEBUG_PORT, NULL, &handle_debug_write);
163 v3_dev_hook_io(dev, BOCHS_CONSOLE_PORT, NULL, &handle_console_write);
170 device_register("BOCHS_DEBUG", debug_init);