| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Virtual Machines. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __VM_H__ |
| 9 |
#define __VM_H__ |
| 10 |
|
| 11 |
#include <pthread.h> |
| 12 |
|
| 13 |
#include "dynamips.h" |
| 14 |
#include "memory.h" |
| 15 |
#include "cpu.h" |
| 16 |
#include "dev_vtty.h" |
| 17 |
#include "cisco_eeprom.h" |
| 18 |
#include "cisco_card.h" |
| 19 |
#include "rommon_var.h" |
| 20 |
|
| 21 |
#define VM_PAGE_SHIFT 12 |
| 22 |
#define VM_PAGE_SIZE (1 << VM_PAGE_SHIFT) |
| 23 |
#define VM_PAGE_IMASK (VM_PAGE_SIZE - 1) |
| 24 |
#define VM_PAGE_MASK (~(VM_PAGE_IMASK)) |
| 25 |
|
| 26 |
/* Number of pages in chunk area */ |
| 27 |
#define VM_CHUNK_AREA_SIZE 256 |
| 28 |
|
| 29 |
/* VM memory chunk */ |
| 30 |
typedef struct vm_chunk vm_chunk_t; |
| 31 |
struct vm_chunk { |
| 32 |
void *area; |
| 33 |
u_int page_alloc,page_total; |
| 34 |
vm_chunk_t *next; |
| 35 |
}; |
| 36 |
|
| 37 |
/* VM ghost pool entry */ |
| 38 |
typedef struct vm_ghost_image vm_ghost_image_t; |
| 39 |
struct vm_ghost_image { |
| 40 |
char *filename; |
| 41 |
u_int ref_count; |
| 42 |
int fd; |
| 43 |
off_t file_size; |
| 44 |
u_char *area_ptr; |
| 45 |
vm_ghost_image_t *next; |
| 46 |
}; |
| 47 |
|
| 48 |
/* Maximum number of devices per VM */ |
| 49 |
#define VM_DEVICE_MAX (1 << 6) |
| 50 |
|
| 51 |
/* Size of the PCI bus pool */ |
| 52 |
#define VM_PCI_POOL_SIZE 32 |
| 53 |
|
| 54 |
/* VM instance status */ |
| 55 |
enum { |
| 56 |
VM_STATUS_HALTED = 0, /* VM is halted and no HW resources are used */ |
| 57 |
VM_STATUS_SHUTDOWN, /* Shutdown procedure engaged */ |
| 58 |
VM_STATUS_RUNNING, /* VM is running */ |
| 59 |
VM_STATUS_SUSPENDED, /* VM is suspended */ |
| 60 |
}; |
| 61 |
|
| 62 |
/* Ghost RAM status */ |
| 63 |
enum { |
| 64 |
VM_GHOST_RAM_NONE = 0, |
| 65 |
VM_GHOST_RAM_GENERATE, |
| 66 |
VM_GHOST_RAM_USE, |
| 67 |
}; |
| 68 |
|
| 69 |
/* Timer IRQ check interval */ |
| 70 |
#define VM_TIMER_IRQ_CHECK_ITV 1000 |
| 71 |
|
| 72 |
/* Max slots per VM */ |
| 73 |
#define VM_MAX_SLOTS 16 |
| 74 |
|
| 75 |
/* forward declarations */ |
| 76 |
typedef struct vm_obj vm_obj_t; |
| 77 |
|
| 78 |
/* Shutdown function prototype for an object */ |
| 79 |
typedef void *(*vm_shutdown_t)(vm_instance_t *vm,void *data); |
| 80 |
|
| 81 |
/* VM object, used to keep track of devices and various things */ |
| 82 |
struct vm_obj { |
| 83 |
char *name; |
| 84 |
void *data; |
| 85 |
struct vm_obj *next,**pprev; |
| 86 |
vm_shutdown_t shutdown; |
| 87 |
}; |
| 88 |
|
| 89 |
/* VM instance */ |
| 90 |
struct vm_instance { |
| 91 |
char *name; |
| 92 |
vm_platform_t *platform; /* Platform specific helpers */ |
| 93 |
int status; /* Instance status */ |
| 94 |
int instance_id; /* Instance Identifier */ |
| 95 |
char *lock_file; /* Lock file */ |
| 96 |
char *log_file; /* Log filename */ |
| 97 |
int log_file_enabled; /* Logging enabled */ |
| 98 |
u_int ram_size,rom_size; /* RAM and ROM size in Mb */ |
| 99 |
u_int ram_res_size; /* RAM reserved space size */ |
| 100 |
u_int iomem_size; /* IOMEM size in Mb */ |
| 101 |
u_int nvram_size; /* NVRAM size in Kb */ |
| 102 |
u_int pcmcia_disk_size[2]; /* PCMCIA disk0 and disk1 sizes (in Mb) */ |
| 103 |
u_int conf_reg,conf_reg_setup; /* Config register */ |
| 104 |
u_int clock_divisor; /* Clock Divisor (see cp0.c) */ |
| 105 |
u_int ram_mmap; /* Memory-mapped RAM ? */ |
| 106 |
u_int restart_ios; /* Restart IOS on reload ? */ |
| 107 |
u_int elf_machine_id; /* ELF machine identifier */ |
| 108 |
u_int exec_area_size; /* Size of execution area for CPU */ |
| 109 |
m_uint32_t ios_entry_point; /* IOS entry point */ |
| 110 |
char *ios_image; /* IOS image filename */ |
| 111 |
char *ios_startup_config; /* IOS configuration file for startup-config */ |
| 112 |
char *ios_private_config; /* IOS configuration file for private-config */ |
| 113 |
char *rom_filename; /* ROM filename */ |
| 114 |
char *sym_filename; /* Symbol filename */ |
| 115 |
FILE *lock_fd,*log_fd; /* Lock/Log file descriptors */ |
| 116 |
int debug_level; /* Debugging Level */ |
| 117 |
int jit_use; /* CPUs use JIT */ |
| 118 |
int sparse_mem; /* Use sparse virtual memory */ |
| 119 |
u_int nm_iomem_size; /* IO mem size to be passed to Smart Init */ |
| 120 |
|
| 121 |
/* ROMMON variables */ |
| 122 |
struct rommon_var_list rommon_vars; |
| 123 |
|
| 124 |
/* Memory chunks */ |
| 125 |
vm_chunk_t *chunks; |
| 126 |
|
| 127 |
/* Basic hardware: system CPU, PCI busses and PCI I/O space */ |
| 128 |
cpu_group_t *cpu_group; |
| 129 |
cpu_gen_t *boot_cpu; |
| 130 |
struct pci_bus *pci_bus[2]; |
| 131 |
struct pci_bus *pci_bus_pool[VM_PCI_POOL_SIZE]; |
| 132 |
struct pci_io_data *pci_io_space; |
| 133 |
|
| 134 |
/* Memory mapped devices */ |
| 135 |
struct vdevice *dev_list; |
| 136 |
struct vdevice *dev_array[VM_DEVICE_MAX]; |
| 137 |
|
| 138 |
/* IRQ routing */ |
| 139 |
void (*set_irq)(vm_instance_t *vm,u_int irq); |
| 140 |
void (*clear_irq)(vm_instance_t *vm,u_int irq); |
| 141 |
|
| 142 |
/* Slots for PA/NM/... */ |
| 143 |
u_int nr_slots; |
| 144 |
u_int slots_type; |
| 145 |
struct cisco_card *slots[VM_MAX_SLOTS]; |
| 146 |
struct cisco_card_driver **slots_drivers; |
| 147 |
struct pci_bus *slots_pci_bus[VM_MAX_SLOTS]; |
| 148 |
|
| 149 |
/* Filename for ghosted RAM */ |
| 150 |
char *ghost_ram_filename; |
| 151 |
|
| 152 |
/* Ghost RAM image handling */ |
| 153 |
int ghost_status; |
| 154 |
|
| 155 |
/* Timer IRQ interval check */ |
| 156 |
u_int timer_irq_check_itv; |
| 157 |
|
| 158 |
/* Translation sharing group */ |
| 159 |
int tsg; |
| 160 |
|
| 161 |
/* "idling" pointer counter */ |
| 162 |
m_uint64_t idle_pc; |
| 163 |
|
| 164 |
/* JIT block direct jumps */ |
| 165 |
int exec_blk_direct_jump; |
| 166 |
|
| 167 |
/* IRQ idling preemption */ |
| 168 |
u_int irq_idle_preempt[256]; |
| 169 |
|
| 170 |
/* Console and AUX port VTTY type and parameters */ |
| 171 |
int vtty_con_type,vtty_aux_type; |
| 172 |
int vtty_con_tcp_port,vtty_aux_tcp_port; |
| 173 |
vtty_serial_option_t vtty_con_serial_option,vtty_aux_serial_option; |
| 174 |
|
| 175 |
/* Virtual TTY for Console and AUX ports */ |
| 176 |
vtty_t *vtty_con,*vtty_aux; |
| 177 |
|
| 178 |
/* Space reserved in NVRAM by ROM monitor */ |
| 179 |
u_int nvram_rom_space; |
| 180 |
|
| 181 |
/* Chassis cookie (for c2600 and maybe other routers) */ |
| 182 |
m_uint16_t chassis_cookie[64]; |
| 183 |
|
| 184 |
/* Specific hardware data */ |
| 185 |
void *hw_data; |
| 186 |
|
| 187 |
/* VM objects */ |
| 188 |
struct vm_obj *vm_object_list; |
| 189 |
}; |
| 190 |
|
| 191 |
/* VM Platform definition */ |
| 192 |
struct vm_platform { |
| 193 |
char *name; |
| 194 |
char *log_name; |
| 195 |
char *cli_name; |
| 196 |
int (*create_instance)(vm_instance_t *vm); |
| 197 |
int (*delete_instance)(vm_instance_t *vm); |
| 198 |
int (*init_instance)(vm_instance_t *vm); |
| 199 |
int (*stop_instance)(vm_instance_t *vm); |
| 200 |
int (*oir_start)(vm_instance_t *vm,u_int slot_id,u_int subslot_id); |
| 201 |
int (*oir_stop)(vm_instance_t *vm,u_int slot_id,u_int subslot_id); |
| 202 |
int (*nvram_extract_config)(vm_instance_t *vm,u_char **startup_config,size_t *startup_len,u_char **private_config,size_t *private_len); |
| 203 |
int (*nvram_push_config)(vm_instance_t *vm,u_char *startup_config,size_t startup_len,u_char *private_config,size_t private_len); |
| 204 |
u_int (*get_mac_addr_msb)(void); |
| 205 |
void (*save_config)(vm_instance_t *vm,FILE *fd); |
| 206 |
int (*cli_parse_options)(vm_instance_t *vm,int option); |
| 207 |
void (*cli_show_options)(vm_instance_t *vm); |
| 208 |
void (*show_spec_drivers)(void); |
| 209 |
}; |
| 210 |
|
| 211 |
/* VM platform list item */ |
| 212 |
struct vm_platform_list { |
| 213 |
struct vm_platform_list *next; |
| 214 |
struct vm_platform *platform; |
| 215 |
}; |
| 216 |
|
| 217 |
extern int vm_file_naming_type; |
| 218 |
|
| 219 |
/* Set an IRQ for a VM */ |
| 220 |
static inline void vm_set_irq(vm_instance_t *vm,u_int irq) |
| 221 |
{ |
| 222 |
if (vm->set_irq != NULL) |
| 223 |
vm->set_irq(vm,irq); |
| 224 |
} |
| 225 |
|
| 226 |
/* Clear an IRQ for a VM */ |
| 227 |
static inline void vm_clear_irq(vm_instance_t *vm,u_int irq) |
| 228 |
{ |
| 229 |
if (vm->clear_irq != NULL) |
| 230 |
vm->clear_irq(vm,irq); |
| 231 |
} |
| 232 |
|
| 233 |
/* Initialize a VM object */ |
| 234 |
void vm_object_init(vm_obj_t *obj); |
| 235 |
|
| 236 |
/* Add a VM object to an instance */ |
| 237 |
void vm_object_add(vm_instance_t *vm,vm_obj_t *obj); |
| 238 |
|
| 239 |
/* Remove a VM object from an instance */ |
| 240 |
void vm_object_remove(vm_instance_t *vm,vm_obj_t *obj); |
| 241 |
|
| 242 |
/* Find an object given its name */ |
| 243 |
vm_obj_t *vm_object_find(vm_instance_t *vm,char *name); |
| 244 |
|
| 245 |
/* Check that a mandatory object is present */ |
| 246 |
int vm_object_check(vm_instance_t *vm,char *name); |
| 247 |
|
| 248 |
/* Dump the object list of an instance */ |
| 249 |
void vm_object_dump(vm_instance_t *vm); |
| 250 |
|
| 251 |
/* Get VM type */ |
| 252 |
char *vm_get_type(vm_instance_t *vm); |
| 253 |
|
| 254 |
/* Get MAC address MSB */ |
| 255 |
u_int vm_get_mac_addr_msb(vm_instance_t *vm); |
| 256 |
|
| 257 |
/* Generate a filename for use by the instance */ |
| 258 |
char *vm_build_filename(vm_instance_t *vm,char *name); |
| 259 |
|
| 260 |
/* Get the amount of host virtual memory used by a VM */ |
| 261 |
size_t vm_get_vspace_size(vm_instance_t *vm); |
| 262 |
|
| 263 |
/* Check that an instance lock file doesn't already exist */ |
| 264 |
int vm_get_lock(vm_instance_t *vm); |
| 265 |
|
| 266 |
/* Erase lock file */ |
| 267 |
void vm_release_lock(vm_instance_t *vm,int erase); |
| 268 |
|
| 269 |
/* Log a message */ |
| 270 |
void vm_flog(vm_instance_t *vm,char *module,char *format,va_list ap); |
| 271 |
|
| 272 |
/* Log a message */ |
| 273 |
void vm_log(vm_instance_t *vm,char *module,char *format,...); |
| 274 |
|
| 275 |
/* Close the log file */ |
| 276 |
int vm_close_log(vm_instance_t *vm); |
| 277 |
|
| 278 |
/* Create the log file */ |
| 279 |
int vm_create_log(vm_instance_t *vm); |
| 280 |
|
| 281 |
/* Reopen the log file */ |
| 282 |
int vm_reopen_log(vm_instance_t *vm); |
| 283 |
|
| 284 |
/* Error message */ |
| 285 |
void vm_error(vm_instance_t *vm,char *format,...); |
| 286 |
|
| 287 |
/* Shutdown hardware resources used by a VM */ |
| 288 |
int vm_hardware_shutdown(vm_instance_t *vm); |
| 289 |
|
| 290 |
/* Free resources used by a VM */ |
| 291 |
void vm_free(vm_instance_t *vm); |
| 292 |
|
| 293 |
/* Get an instance given a name */ |
| 294 |
vm_instance_t *vm_acquire(char *name); |
| 295 |
|
| 296 |
/* Release a VM (decrement reference count) */ |
| 297 |
int vm_release(vm_instance_t *vm); |
| 298 |
|
| 299 |
/* Initialize RAM */ |
| 300 |
int vm_ram_init(vm_instance_t *vm,m_uint64_t paddr); |
| 301 |
|
| 302 |
/* Initialize VTTY */ |
| 303 |
int vm_init_vtty(vm_instance_t *vm); |
| 304 |
|
| 305 |
/* Delete VTTY */ |
| 306 |
void vm_delete_vtty(vm_instance_t *vm); |
| 307 |
|
| 308 |
/* Bind a device to a virtual machine */ |
| 309 |
int vm_bind_device(vm_instance_t *vm,struct vdevice *dev); |
| 310 |
|
| 311 |
/* Unbind a device from a virtual machine */ |
| 312 |
int vm_unbind_device(vm_instance_t *vm,struct vdevice *dev); |
| 313 |
|
| 314 |
/* Map a device at the specified physical address */ |
| 315 |
int vm_map_device(vm_instance_t *vm,struct vdevice *dev,m_uint64_t base_addr); |
| 316 |
|
| 317 |
/* Set an IRQ for a VM */ |
| 318 |
void vm_set_irq(vm_instance_t *vm,u_int irq); |
| 319 |
|
| 320 |
/* Clear an IRQ for a VM */ |
| 321 |
void vm_clear_irq(vm_instance_t *vm,u_int irq); |
| 322 |
|
| 323 |
/* Suspend a VM instance */ |
| 324 |
int vm_suspend(vm_instance_t *vm); |
| 325 |
|
| 326 |
/* Resume a VM instance */ |
| 327 |
int vm_resume(vm_instance_t *vm); |
| 328 |
|
| 329 |
/* Stop an instance */ |
| 330 |
int vm_stop(vm_instance_t *vm); |
| 331 |
|
| 332 |
/* Monitor an instance periodically */ |
| 333 |
void vm_monitor(vm_instance_t *vm); |
| 334 |
|
| 335 |
/* Allocate an host page */ |
| 336 |
void *vm_alloc_host_page(vm_instance_t *vm); |
| 337 |
|
| 338 |
/* Free an host page */ |
| 339 |
void vm_free_host_page(vm_instance_t *vm,void *ptr); |
| 340 |
|
| 341 |
/* Get a ghost image */ |
| 342 |
int vm_ghost_image_get(char *filename,u_char **ptr,int *fd); |
| 343 |
|
| 344 |
/* Release a ghost image */ |
| 345 |
int vm_ghost_image_release(int fd); |
| 346 |
|
| 347 |
/* Open a VM file and map it in memory */ |
| 348 |
int vm_mmap_open_file(vm_instance_t *vm,char *name, |
| 349 |
u_char **ptr,off_t *fsize); |
| 350 |
|
| 351 |
/* Open/Create a VM file and map it in memory */ |
| 352 |
int vm_mmap_create_file(vm_instance_t *vm,char *name,size_t len,u_char **ptr); |
| 353 |
|
| 354 |
/* Close a memory mapped file */ |
| 355 |
int vm_mmap_close_file(int fd,u_char *ptr,size_t len); |
| 356 |
|
| 357 |
/* Save the Cisco IOS configuration from NVRAM */ |
| 358 |
int vm_ios_save_config(vm_instance_t *vm); |
| 359 |
|
| 360 |
/* Set Cisco IOS image to use */ |
| 361 |
int vm_ios_set_image(vm_instance_t *vm,char *ios_image); |
| 362 |
|
| 363 |
/* Unset a Cisco IOS configuration file */ |
| 364 |
void vm_ios_unset_config(vm_instance_t *vm); |
| 365 |
|
| 366 |
/* Set Cisco IOS configuration files to use (NULL to keep existing data) */ |
| 367 |
int vm_ios_set_config(vm_instance_t *vm,const char *startup_filename,const char *private_filename); |
| 368 |
|
| 369 |
/* Extract IOS configuration from NVRAM and write it to a file */ |
| 370 |
int vm_nvram_extract_config(vm_instance_t *vm,char *filename); |
| 371 |
|
| 372 |
/* Read IOS configuraton from the files and push it to NVRAM (NULL to keep existing data) */ |
| 373 |
int vm_nvram_push_config(vm_instance_t *vm,const char *startup_filename,const char *private_filename); |
| 374 |
|
| 375 |
/* Save general VM configuration into the specified file */ |
| 376 |
void vm_save_config(vm_instance_t *vm,FILE *fd); |
| 377 |
|
| 378 |
/* Find a platform */ |
| 379 |
vm_platform_t *vm_platform_find(char *name); |
| 380 |
|
| 381 |
/* Find a platform given its CLI name */ |
| 382 |
vm_platform_t *vm_platform_find_cli_name(char *name); |
| 383 |
|
| 384 |
/* Register a platform */ |
| 385 |
int vm_platform_register(vm_platform_t *platform); |
| 386 |
|
| 387 |
/* Create an instance of the specified type */ |
| 388 |
vm_instance_t *vm_create_instance(char *name,int instance_id,char *type); |
| 389 |
|
| 390 |
/* Delete a VM instance */ |
| 391 |
int vm_delete_instance(char *name); |
| 392 |
|
| 393 |
/* Rename a VM instance */ |
| 394 |
int vm_rename_instance(vm_instance_t *vm, char *name); |
| 395 |
|
| 396 |
/* Initialize a VM instance */ |
| 397 |
int vm_init_instance(vm_instance_t *vm); |
| 398 |
|
| 399 |
/* Stop a VM instance */ |
| 400 |
int vm_stop_instance(vm_instance_t *vm); |
| 401 |
|
| 402 |
/* Delete all VM instances */ |
| 403 |
int vm_delete_all_instances(void); |
| 404 |
|
| 405 |
/* Save all VM configs */ |
| 406 |
int vm_save_config_all(FILE *fd); |
| 407 |
|
| 408 |
/* OIR to start a slot/subslot */ |
| 409 |
int vm_oir_start(vm_instance_t *vm,u_int slot,u_int subslot); |
| 410 |
|
| 411 |
/* OIR to stop a slot/subslot */ |
| 412 |
int vm_oir_stop(vm_instance_t *vm,u_int slot,u_int subslot); |
| 413 |
|
| 414 |
/* Set the JIT translation sharing group */ |
| 415 |
int vm_set_tsg(vm_instance_t *vm,int group); |
| 416 |
|
| 417 |
#endif |