1 |
/* |
2 |
* Cisco 7200 (Predator) simulation platform. |
3 |
* Copyright (c) 2006 Christophe Fillot (cf@utc.fr) |
4 |
* |
5 |
* Hypervisor routines for VM debugging. |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <stdlib.h> |
10 |
#include <unistd.h> |
11 |
#include <string.h> |
12 |
#include <sys/types.h> |
13 |
#include <sys/stat.h> |
14 |
#include <sys/mman.h> |
15 |
#include <signal.h> |
16 |
#include <fcntl.h> |
17 |
#include <errno.h> |
18 |
#include <assert.h> |
19 |
#include <stdarg.h> |
20 |
#include <sys/ioctl.h> |
21 |
#include <sys/types.h> |
22 |
#include <sys/socket.h> |
23 |
#include <arpa/inet.h> |
24 |
#include <pthread.h> |
25 |
|
26 |
#include "mips64.h" |
27 |
#include "cp0.h" |
28 |
#include "dynamips.h" |
29 |
#include "device.h" |
30 |
#include "dev_c7200.h" |
31 |
#include "dev_vtty.h" |
32 |
#include "utils.h" |
33 |
#include "registry.h" |
34 |
#include "hypervisor.h" |
35 |
|
36 |
/* Show CPU registers */ |
37 |
static int cmd_show_cpu_regs(hypervisor_conn_t *conn,int argc,char *argv[]) |
38 |
{ |
39 |
vm_instance_t *vm; |
40 |
cpu_mips_t *cpu; |
41 |
|
42 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
43 |
return(-1); |
44 |
|
45 |
if ((cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]))) != NULL) |
46 |
mips64_dump_regs(cpu); |
47 |
|
48 |
vm_release(vm); |
49 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
50 |
return(0); |
51 |
} |
52 |
|
53 |
/* Show CPU TLB */ |
54 |
static int cmd_show_cpu_tlb(hypervisor_conn_t *conn,int argc,char *argv[]) |
55 |
{ |
56 |
vm_instance_t *vm; |
57 |
cpu_mips_t *cpu; |
58 |
|
59 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
60 |
return(-1); |
61 |
|
62 |
if ((cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]))) != NULL) |
63 |
tlb_dump(cpu); |
64 |
|
65 |
vm_release(vm); |
66 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
67 |
return(0); |
68 |
} |
69 |
|
70 |
/* Set a CPU register*/ |
71 |
static int cmd_set_cpu_reg(hypervisor_conn_t *conn,int argc,char *argv[]) |
72 |
{ |
73 |
vm_instance_t *vm; |
74 |
cpu_mips_t *cpu; |
75 |
int reg_index; |
76 |
|
77 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
78 |
return(-1); |
79 |
|
80 |
cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])); |
81 |
reg_index = cp0_get_reg_index(argv[2]); |
82 |
|
83 |
if (!cpu || (reg_index < 1)) { |
84 |
vm_release(vm); |
85 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU or register"); |
86 |
return(-1); |
87 |
} |
88 |
|
89 |
|
90 |
/* Set register value */ |
91 |
cpu->gpr[reg_index] = strtoull(argv[3],NULL,0); |
92 |
|
93 |
vm_release(vm); |
94 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
95 |
return(0); |
96 |
} |
97 |
|
98 |
/* Add a breakpoint */ |
99 |
static int cmd_add_cpu_breakpoint(hypervisor_conn_t *conn, |
100 |
int argc,char *argv[]) |
101 |
{ |
102 |
vm_instance_t *vm; |
103 |
cpu_mips_t *cpu; |
104 |
m_uint64_t addr; |
105 |
|
106 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
107 |
return(-1); |
108 |
|
109 |
if (!(cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])))) { |
110 |
vm_release(vm); |
111 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU"); |
112 |
return(-1); |
113 |
} |
114 |
|
115 |
addr = strtoull(argv[2],NULL,0); |
116 |
mips64_add_breakpoint(cpu,addr); |
117 |
|
118 |
vm_release(vm); |
119 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
120 |
return(0); |
121 |
} |
122 |
|
123 |
/* Remove a breakpoint */ |
124 |
static int cmd_remove_cpu_breakpoint(hypervisor_conn_t *conn, |
125 |
int argc,char *argv[]) |
126 |
{ |
127 |
vm_instance_t *vm; |
128 |
cpu_mips_t *cpu; |
129 |
m_uint64_t addr; |
130 |
|
131 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
132 |
return(-1); |
133 |
|
134 |
if (!(cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])))) { |
135 |
vm_release(vm); |
136 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU"); |
137 |
return(-1); |
138 |
} |
139 |
|
140 |
addr = strtoull(argv[2],NULL,0); |
141 |
mips64_remove_breakpoint(cpu,addr); |
142 |
|
143 |
vm_release(vm); |
144 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
145 |
return(0); |
146 |
} |
147 |
|
148 |
/* Write a 32-bit memory word in physical memory */ |
149 |
static int cmd_pmem_w32(hypervisor_conn_t *conn,int argc,char *argv[]) |
150 |
{ |
151 |
vm_instance_t *vm; |
152 |
cpu_mips_t *cpu; |
153 |
m_uint64_t addr; |
154 |
m_uint32_t value; |
155 |
|
156 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
157 |
return(-1); |
158 |
|
159 |
cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])); |
160 |
|
161 |
if (!cpu) { |
162 |
vm_release(vm); |
163 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"No CPU found."); |
164 |
return(-1); |
165 |
} |
166 |
|
167 |
/* Write word */ |
168 |
addr = strtoull(argv[2],NULL,0); |
169 |
value = strtoul(argv[3],NULL,0); |
170 |
physmem_copy_u32_to_vm(vm,addr,value); |
171 |
|
172 |
vm_release(vm); |
173 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
174 |
return(0); |
175 |
} |
176 |
|
177 |
/* Read a 32-bit memory word */ |
178 |
static int cmd_pmem_r32(hypervisor_conn_t *conn,int argc,char *argv[]) |
179 |
{ |
180 |
vm_instance_t *vm; |
181 |
cpu_mips_t *cpu; |
182 |
m_uint64_t addr; |
183 |
m_uint32_t value; |
184 |
|
185 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
186 |
return(-1); |
187 |
|
188 |
cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])); |
189 |
|
190 |
if (!cpu) { |
191 |
vm_release(vm); |
192 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"No CPU found."); |
193 |
return(-1); |
194 |
} |
195 |
|
196 |
/* Write word */ |
197 |
addr = strtoull(argv[2],NULL,0); |
198 |
value = physmem_copy_u32_from_vm(vm,addr); |
199 |
|
200 |
vm_release(vm); |
201 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"0x%8.8x",value); |
202 |
return(0); |
203 |
} |
204 |
|
205 |
/* Write a 16-bit memory word */ |
206 |
static int cmd_pmem_w16(hypervisor_conn_t *conn,int argc,char *argv[]) |
207 |
{ |
208 |
vm_instance_t *vm; |
209 |
cpu_mips_t *cpu; |
210 |
m_uint64_t addr; |
211 |
m_uint16_t value; |
212 |
|
213 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
214 |
return(-1); |
215 |
|
216 |
cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])); |
217 |
|
218 |
if (!cpu) { |
219 |
vm_release(vm); |
220 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"No CPU found."); |
221 |
return(-1); |
222 |
} |
223 |
|
224 |
/* Write word */ |
225 |
addr = strtoull(argv[2],NULL,0); |
226 |
value = strtoul(argv[3],NULL,0); |
227 |
physmem_copy_u16_to_vm(vm,addr,value); |
228 |
|
229 |
vm_release(vm); |
230 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
231 |
return(0); |
232 |
} |
233 |
|
234 |
/* Read a 16-bit memory word */ |
235 |
static int cmd_pmem_r16(hypervisor_conn_t *conn,int argc,char *argv[]) |
236 |
{ |
237 |
vm_instance_t *vm; |
238 |
cpu_mips_t *cpu; |
239 |
m_uint64_t addr; |
240 |
m_uint16_t value; |
241 |
|
242 |
if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) |
243 |
return(-1); |
244 |
|
245 |
cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])); |
246 |
|
247 |
if (!cpu) { |
248 |
vm_release(vm); |
249 |
hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"No CPU found."); |
250 |
return(-1); |
251 |
} |
252 |
|
253 |
/* Write word */ |
254 |
addr = strtoull(argv[2],NULL,0); |
255 |
value = physmem_copy_u16_from_vm(vm,addr); |
256 |
|
257 |
vm_release(vm); |
258 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"0x%4.4x",value); |
259 |
return(0); |
260 |
} |
261 |
|
262 |
/* VM debug commands */ |
263 |
static hypervisor_cmd_t vm_cmd_array[] = { |
264 |
{ "show_cpu_regs", 2, 2, cmd_show_cpu_regs, NULL }, |
265 |
{ "show_cpu_tlb", 2, 2, cmd_show_cpu_tlb, NULL }, |
266 |
{ "set_cpu_reg", 4, 4, cmd_set_cpu_reg, NULL }, |
267 |
{ "add_cpu_breakpoint", 3, 3, cmd_add_cpu_breakpoint, NULL }, |
268 |
{ "remove_cpu_breakpoint", 3, 3, cmd_remove_cpu_breakpoint, NULL }, |
269 |
{ "pmem_w32", 4, 4, cmd_pmem_w32, NULL }, |
270 |
{ "pmem_r32", 3, 3, cmd_pmem_r32, NULL }, |
271 |
{ "pmem_w16", 4, 4, cmd_pmem_w16, NULL }, |
272 |
{ "pmem_r16", 3, 3, cmd_pmem_r16, NULL }, |
273 |
{ NULL, -1, -1, NULL, NULL }, |
274 |
}; |
275 |
|
276 |
/* Hypervisor VM debugging initialization */ |
277 |
int hypervisor_vm_debug_init(void) |
278 |
{ |
279 |
hypervisor_module_t *module; |
280 |
|
281 |
module = hypervisor_register_module("vm_debug"); |
282 |
assert(module != NULL); |
283 |
|
284 |
hypervisor_register_cmd_array(module,vm_cmd_array); |
285 |
return(0); |
286 |
} |