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