Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Support HVM partitioning for APICs
[palacios.git] / palacios / src / devices / os_debug.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_dev_mgr.h>
24 #include <palacios/vm_guest_mem.h>
25
26 #define BUF_SIZE 1024
27
28 #define DEBUG_PORT1 0xc0c0
29 #define HEARTBEAT_PORT 0x99
30
31 struct debug_state {
32     char debug_buf[BUF_SIZE];
33     uint_t debug_offset;
34
35 };
36
37
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;
40
41     state->debug_buf[state->debug_offset++] = *(char*)src;
42
43     if ((*(char*)src == 0xa) ||  (state->debug_offset == (BUF_SIZE - 1))) {
44         PrintDebug(core->vm_info, core, "VM_CONSOLE>%s", state->debug_buf);
45         memset(state->debug_buf, 0, BUF_SIZE);
46         state->debug_offset = 0;
47     }
48
49     return length;
50 }
51
52 static int handle_hb_write(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
53     uint32_t val = 0;
54
55     if (length == 1) {
56         val = *(uint8_t *)src;
57     } else if (length == 2) {
58         val = *(uint16_t *)src;
59     } else {
60         val = *(uint32_t *)src;
61     }
62
63     V3_Print(core->vm_info, core, "HEARTBEAT> %x (%d)\n", val, val);
64
65     return length;
66 }
67
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;
70
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;
74
75     if (msg_len >= BUF_SIZE) {
76         PrintError(info->vm_info, info, "Console message too large for buffer (len=%d)\n", msg_len);
77         return -1;
78     }
79
80     if (buf_is_va == 1) {
81         if (v3_read_gva_memory(info, msg_gpa, msg_len, (uchar_t *)state->debug_buf) != msg_len) {
82             PrintError(info->vm_info, info, "Could not read debug message\n");
83             return -1;
84         }
85     } else {
86         if (v3_read_gpa_memory(info, msg_gpa, msg_len, (uchar_t *)state->debug_buf) != msg_len) {
87             PrintError(info->vm_info, info, "Could not read debug message\n");
88             return -1;
89         }
90     }   
91
92     state->debug_buf[msg_len] = 0;
93
94     PrintDebug(info->vm_info, info, "VM_CONSOLE>%s\n", state->debug_buf);
95
96     return 0;
97 }
98
99
100
101 static int debug_free(struct debug_state * state) {
102
103     // unregister hypercall
104
105     V3_Free(state);
106     return 0;
107 };
108
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;
112     
113     V3_CHKPT_SAVE_AUTOTAG(ctx, dbg->debug_buf, savefailout);
114     V3_CHKPT_SAVE_AUTOTAG(ctx, dbg->debug_offset, savefailout);
115     
116     return 0;
117
118  savefailout:
119     PrintError(VM_NONE,VCORE_NONE, "Failed to save debug\n");
120     return -1;
121
122 }
123
124
125 static int debug_load(struct v3_chkpt_ctx * ctx, void * private_data) {
126     struct debug_state * dbg = (struct debug_state *)private_data;
127     
128     V3_CHKPT_LOAD_AUTOTAG(ctx, dbg->debug_buf,loadfailout);
129     V3_CHKPT_LOAD_AUTOTAG(ctx, dbg->debug_offset,loadfailout);
130     
131     return 0;
132
133  loadfailout:
134     PrintError(VM_NONE, VCORE_NONE, "Failed to load debug\n");
135     return -1;
136 }
137
138 #endif
139
140
141
142 static struct v3_device_ops dev_ops = {
143     .free = (int (*)(void *))debug_free,
144 #ifdef V3_CONFIG_CHECKPOINT
145     .save = debug_save,
146     .load = debug_load
147 #endif 
148 };
149
150
151
152
153 static int debug_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
154     struct debug_state * state = NULL;
155     char * dev_id = v3_cfg_val(cfg, "ID");
156
157     state = (struct debug_state *)V3_Malloc(sizeof(struct debug_state));
158
159     if (!state) {
160         PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
161         return -1;
162     }
163
164     PrintDebug(vm, VCORE_NONE, "Creating OS Debug Device\n");
165
166     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
167
168     if (dev == NULL) {
169         PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
170         V3_Free(state);
171         return -1;
172     }
173
174     if (v3_dev_hook_io(dev, DEBUG_PORT1,  NULL, &handle_gen_write) == -1) {
175         PrintError(vm, VCORE_NONE, "Error hooking OS debug IO port\n");
176         v3_remove_device(dev);
177         return -1;
178     }
179
180
181     if (v3_dev_hook_io(dev, HEARTBEAT_PORT, NULL, &handle_hb_write) == -1) {
182         PrintError(vm, VCORE_NONE, "error hooking OS heartbeat port\n");
183         v3_remove_device(dev);
184         return -1;
185     }
186
187     v3_register_hypercall(vm, OS_DEBUG_HCALL, handle_hcall, state);
188
189     state->debug_offset = 0;
190     memset(state->debug_buf, 0, BUF_SIZE);
191   
192     return 0;
193 }
194
195
196 device_register("OS_DEBUG", debug_init)