1 |
/* |
2 |
* Cisco router simulation platform. |
3 |
* Copyright (c) 2006 Christophe Fillot (cf@utc.fr) |
4 |
* |
5 |
* PowerPC VM experimentations. |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <stdlib.h> |
10 |
#include <string.h> |
11 |
#include <unistd.h> |
12 |
#include <sys/types.h> |
13 |
#include <assert.h> |
14 |
|
15 |
#include "cpu.h" |
16 |
#include "vm.h" |
17 |
#include "dynamips.h" |
18 |
#include "memory.h" |
19 |
#include "device.h" |
20 |
#include "dev_rom.h" |
21 |
#include "pci_io.h" |
22 |
#include "dev_vtty.h" |
23 |
#include "registry.h" |
24 |
#include "net.h" |
25 |
#include "ppc32_mem.h" |
26 |
#include "ppc32_vmtest.h" |
27 |
|
28 |
static struct ppc32_bat_prog bat_array[] = { |
29 |
{ PPC32_IBAT_IDX, 0, 0xfff0001e, 0xfff00001 }, |
30 |
{ PPC32_IBAT_IDX, 1, 0x00001ffe, 0x00000001 }, |
31 |
{ PPC32_IBAT_IDX, 2, 0x00000000, 0xee3e0072 }, |
32 |
{ PPC32_IBAT_IDX, 3, 0x80001ffe, 0x80000001 }, |
33 |
|
34 |
{ PPC32_DBAT_IDX, 0, 0x80001ffe, 0x80000042 }, |
35 |
{ PPC32_DBAT_IDX, 1, 0x00001ffe, 0x0000002a }, |
36 |
{ PPC32_DBAT_IDX, 2, 0x60001ffe, 0x6000002a }, |
37 |
{ PPC32_DBAT_IDX, 3, 0xfc0007fe, 0xfc00002a }, |
38 |
{ -1, -1, 0, 0 }, |
39 |
}; |
40 |
|
41 |
/* Create a new test instance */ |
42 |
vm_instance_t *ppc32_vmtest_create_instance(char *name,int instance_id) |
43 |
{ |
44 |
vm_instance_t *vm; |
45 |
|
46 |
if (!(vm = vm_create(name,instance_id,VM_TYPE_PPC32_TEST))) { |
47 |
fprintf(stderr,"PPC32_VMTEST '%s': unable to create VM instance!\n", |
48 |
name); |
49 |
return NULL; |
50 |
} |
51 |
|
52 |
vm->ram_size = PPC32_VMTEST_DEFAULT_RAM_SIZE; |
53 |
return vm; |
54 |
} |
55 |
|
56 |
/* Free resources used by a test instance */ |
57 |
static int ppc32_vmtest_free_instance(void *data,void *arg) |
58 |
{ |
59 |
vm_instance_t *vm = data; |
60 |
|
61 |
if (vm->type == VM_TYPE_PPC32_TEST) { |
62 |
/* Stop all CPUs */ |
63 |
if (vm->cpu_group != NULL) { |
64 |
vm_stop(vm); |
65 |
|
66 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
67 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
68 |
return(FALSE); |
69 |
} |
70 |
} |
71 |
|
72 |
/* Free all resources used by VM */ |
73 |
vm_free(vm); |
74 |
return(TRUE); |
75 |
} |
76 |
|
77 |
return(FALSE); |
78 |
} |
79 |
|
80 |
/* Delete a router instance */ |
81 |
int ppc32_vmtest_delete_instance(char *name) |
82 |
{ |
83 |
return(registry_delete_if_unused(name,OBJ_TYPE_VM, |
84 |
ppc32_vmtest_free_instance,NULL)); |
85 |
} |
86 |
|
87 |
/* Delete all router instances */ |
88 |
int ppc32_vmtest_delete_all_instances(void) |
89 |
{ |
90 |
return(registry_delete_type(OBJ_TYPE_VM,ppc32_vmtest_free_instance,NULL)); |
91 |
} |
92 |
|
93 |
/* Set IRQ line */ |
94 |
static void ppc32_vmtest_set_irq(vm_instance_t *vm,u_int irq) |
95 |
{ |
96 |
cpu_ppc_t *cpu = CPU_PPC32(vm->boot_cpu); |
97 |
|
98 |
cpu->irq_check = cpu->irq_pending = TRUE; |
99 |
} |
100 |
|
101 |
/* Clear IRQ line */ |
102 |
static void ppc32_vmtest_clear_irq(vm_instance_t *vm,u_int irq) |
103 |
{ |
104 |
cpu_ppc_t *cpu = CPU_PPC32(vm->boot_cpu); |
105 |
|
106 |
cpu->irq_check = cpu->irq_pending = FALSE; |
107 |
} |
108 |
|
109 |
/* Initialize the PPC32 VM test Platform */ |
110 |
int ppc32_vmtest_init_platform(vm_instance_t *vm) |
111 |
{ |
112 |
cpu_ppc_t *cpu0; |
113 |
cpu_gen_t *gen0; |
114 |
|
115 |
/* Create Console and AUX ports */ |
116 |
vm_init_vtty(vm); |
117 |
|
118 |
/* Create a CPU group */ |
119 |
vm->cpu_group = cpu_group_create("System CPU"); |
120 |
|
121 |
/* Initialize the virtual PowerPC processor */ |
122 |
if (!(gen0 = cpu_create(vm,CPU_TYPE_PPC32,0))) { |
123 |
vm_error(vm,"unable to create CPU0!\n"); |
124 |
return(-1); |
125 |
} |
126 |
|
127 |
cpu0 = CPU_PPC32(gen0); |
128 |
|
129 |
/* Enable as PowerPC 405 */ |
130 |
//ppc32_set_pvr(cpu0,PPC32_PVR_405 | 0x0102); |
131 |
|
132 |
/* Add this CPU to the system CPU group */ |
133 |
cpu_group_add(vm->cpu_group,gen0); |
134 |
vm->boot_cpu = gen0; |
135 |
|
136 |
/* Set IRQ vectors */ |
137 |
vm->set_irq = ppc32_vmtest_set_irq; |
138 |
vm->clear_irq = ppc32_vmtest_clear_irq; |
139 |
|
140 |
#if 0 |
141 |
{ |
142 |
vm_obj_t *obj; |
143 |
/* Initialize ROM (as a Flash) */ |
144 |
if (!(obj = dev_flash_init(vm,"rom",0xFF000000,16*1048576))) |
145 |
return(-1); |
146 |
|
147 |
dev_flash_copy_data(obj,0x0F00000,ppc32_microcode,ppc32_microcode_len); |
148 |
} |
149 |
#endif |
150 |
|
151 |
//dev_bootflash_init(vm,"bootflash",0xFF000000,8*1048576); |
152 |
|
153 |
#if 1 |
154 |
/* Initialize ROM */ |
155 |
if (!vm->rom_filename) { |
156 |
/* use embedded ROM */ |
157 |
dev_rom_init(vm,"rom",0xFFF00000,512*1024, |
158 |
ppc32_microcode,ppc32_microcode_len); |
159 |
} else { |
160 |
/* use alternate ROM */ |
161 |
dev_ram_init(vm,"rom",TRUE,TRUE,NULL,FALSE, |
162 |
0xFFF00000,512*1024); |
163 |
} |
164 |
#endif |
165 |
|
166 |
dev_ram_init(vm,"nvram",TRUE,FALSE,NULL,FALSE, |
167 |
0x67c00000,vm->nvram_size*4096); |
168 |
|
169 |
dev_ns16552_init(vm,0xffe00000,0x1000,0,0,vm->vtty_con,vm->vtty_aux); |
170 |
|
171 |
/* Remote emulator control */ |
172 |
dev_remote_control_init(vm,0xf6000000,0x1000); |
173 |
|
174 |
/* Initialize RAM */ |
175 |
vm_ram_init(vm,0x00000000); |
176 |
|
177 |
/* RAM aliasing */ |
178 |
dev_create_ram_alias(vm,"ram_alias","ram",0x80000000,vm->ram_size*1048576); |
179 |
|
180 |
/* Display the device list */ |
181 |
dev_show_list(vm); |
182 |
return(0); |
183 |
} |
184 |
|
185 |
/* Boot the RAW image */ |
186 |
int ppc32_vmtest_boot_raw(vm_instance_t *vm) |
187 |
{ |
188 |
cpu_ppc_t *cpu; |
189 |
|
190 |
if (!vm->boot_cpu) |
191 |
return(-1); |
192 |
|
193 |
/* Suspend CPU activity since we will restart directly from ROM */ |
194 |
vm_suspend(vm); |
195 |
|
196 |
/* Check that CPU activity is really suspended */ |
197 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
198 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
199 |
return(-1); |
200 |
} |
201 |
|
202 |
/* Reset the boot CPU */ |
203 |
cpu = CPU_PPC32(vm->boot_cpu); |
204 |
ppc32_reset(cpu); |
205 |
|
206 |
/* Load RAW image */ |
207 |
if (ppc32_load_raw_image(cpu,vm->ios_image,0xFFF00000) < 0) { |
208 |
vm_error(vm,"failed to load RAW image '%s'.\n",vm->ios_image); |
209 |
return(-1); |
210 |
} |
211 |
|
212 |
cpu->ia = 0xFFF00100; |
213 |
cpu->gpr[1] = 0x2000; |
214 |
|
215 |
/* Launch the simulation */ |
216 |
printf("\nPPC32_VMTEST '%s': starting simulation (CPU0 IA=0x%8.8x), " |
217 |
"JIT %sabled.\n", |
218 |
vm->name,cpu->ia,vm->jit_use ? "en":"dis"); |
219 |
|
220 |
vm_log(vm,"PPC32_VMTEST_BOOT", |
221 |
"starting instance (CPU0 IA=0x%8.8x,JIT %s)\n", |
222 |
cpu->ia,vm->jit_use ? "on":"off"); |
223 |
|
224 |
/* Start main CPU */ |
225 |
if (vm->ghost_status != VM_GHOST_RAM_GENERATE) { |
226 |
vm->status = VM_STATUS_RUNNING; |
227 |
cpu_start(vm->boot_cpu); |
228 |
} else { |
229 |
vm->status = VM_STATUS_SHUTDOWN; |
230 |
} |
231 |
return(0); |
232 |
} |
233 |
|
234 |
/* Boot the ELF image */ |
235 |
int ppc32_vmtest_boot_elf(vm_instance_t *vm) |
236 |
{ |
237 |
m_uint32_t rom_entry_point; |
238 |
cpu_ppc_t *cpu; |
239 |
|
240 |
if (!vm->boot_cpu) |
241 |
return(-1); |
242 |
|
243 |
/* Suspend CPU activity since we will restart directly from ROM */ |
244 |
vm_suspend(vm); |
245 |
|
246 |
/* Check that CPU activity is really suspended */ |
247 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
248 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
249 |
return(-1); |
250 |
} |
251 |
|
252 |
/* Reset the boot CPU */ |
253 |
cpu = CPU_PPC32(vm->boot_cpu); |
254 |
ppc32_reset(cpu); |
255 |
|
256 |
/* Load ROM (ELF image or embedded) */ |
257 |
cpu = CPU_PPC32(vm->boot_cpu); |
258 |
rom_entry_point = (m_uint32_t)PPC32_ROM_START; |
259 |
|
260 |
if ((vm->rom_filename != NULL) && |
261 |
(ppc32_load_elf_image(cpu,vm->rom_filename,0,&rom_entry_point) < 0)) |
262 |
{ |
263 |
vm_error(vm,"unable to load alternate ROM '%s', " |
264 |
"fallback to embedded ROM.\n\n",vm->rom_filename); |
265 |
vm->rom_filename = NULL; |
266 |
} |
267 |
|
268 |
/* Load ELF image */ |
269 |
if (ppc32_load_elf_image(cpu,vm->ios_image, |
270 |
(vm->ghost_status == VM_GHOST_RAM_USE), |
271 |
&vm->ios_entry_point) < 0) |
272 |
{ |
273 |
vm_error(vm,"failed to load ELF image '%s'.\n",vm->ios_image); |
274 |
return(-1); |
275 |
} |
276 |
|
277 |
/* Launch the simulation */ |
278 |
printf("\nPPC32_VMTEST '%s': starting simulation (CPU0 IA=0x%8.8x), " |
279 |
"JIT %sabled.\n", |
280 |
vm->name,cpu->ia,vm->jit_use ? "en":"dis"); |
281 |
|
282 |
vm_log(vm,"PPC32_VMTEST_BOOT", |
283 |
"starting instance (CPU0 IA=0x%8.8x,JIT %s)\n", |
284 |
cpu->ia,vm->jit_use ? "on":"off"); |
285 |
|
286 |
/* Start main CPU */ |
287 |
if (vm->ghost_status != VM_GHOST_RAM_GENERATE) { |
288 |
vm->status = VM_STATUS_RUNNING; |
289 |
cpu_start(vm->boot_cpu); |
290 |
} else { |
291 |
vm->status = VM_STATUS_SHUTDOWN; |
292 |
} |
293 |
return(0); |
294 |
} |
295 |
|
296 |
/* Initialize a test instance */ |
297 |
int ppc32_vmtest_init_instance(vm_instance_t *vm) |
298 |
{ |
299 |
/* Initialize the test platform */ |
300 |
if (ppc32_vmtest_init_platform(vm) == -1) { |
301 |
vm_error(vm,"unable to initialize the platform hardware.\n"); |
302 |
return(-1); |
303 |
} |
304 |
|
305 |
/* Load BAT registers */ |
306 |
ppc32_load_bat_array(CPU_PPC32(vm->boot_cpu),bat_array); |
307 |
|
308 |
return(ppc32_vmtest_boot_elf(vm)); |
309 |
} |
310 |
|
311 |
/* Stop a test instance */ |
312 |
int ppc32_vmtest_stop_instance(vm_instance_t *vm) |
313 |
{ |
314 |
printf("\nPPC32_VMTEST '%s': stopping simulation.\n",vm->name); |
315 |
vm_log(vm,"PPC32_VMTEST_STOP","stopping simulation.\n"); |
316 |
|
317 |
/* Stop all CPUs */ |
318 |
if (vm->cpu_group != NULL) { |
319 |
vm_stop(vm); |
320 |
|
321 |
if (cpu_group_sync_state(vm->cpu_group) == -1) { |
322 |
vm_error(vm,"unable to sync with system CPUs.\n"); |
323 |
return(-1); |
324 |
} |
325 |
} |
326 |
|
327 |
/* Free resources that were used during execution to emulate hardware */ |
328 |
vm_hardware_shutdown(vm); |
329 |
return(0); |
330 |
} |