int main(int argc,char *argv[]) { vm_instance_t *vm; #ifdef PROFILE atexit(profiler_savestat); #endif #ifdef USE_UNSTABLE printf("Cisco Router Simulation Platform (version %s/%s unstable)\n", sw_version,os_name); #else printf("Cisco Router Simulation Platform (version %s/%s stable)\n", sw_version,os_name); #endif printf("Copyright (c) 2005-2011 Christophe Fillot.\n"); printf("Build date: %s %s\n\n",__DATE__,__TIME__); gen_uuid_init(); /* Register platforms */ register_default_platforms(); /* Initialize timers */ timer_init(); /* Initialize object registry */ registry_init(); /* Initialize ATM module (for HEC checksums) */ atm_init(); /* Initialize CRC functions */ crc_init(); /* Initialize NetIO code */ netio_rxl_init(); /* Initialize NetIO packet filters */ netio_filter_load_all(); /* Initialize VTTY code */ vtty_init(); /* Parse standard command line */ atexit(destroy_cmd_line_vars); if (!run_hypervisor(argc,argv)) parse_std_cmd_line(argc,argv); /* Create general log file */ create_log_file(); /* Periodic tasks initialization */ if (ptask_init(0) == -1) exit(EXIT_FAILURE); /* Create instruction lookup tables */ mips64_jit_create_ilt(); mips64_exec_create_ilt(); ppc32_jit_create_ilt(); ppc32_exec_create_ilt(); setup_signals(); if (!hypervisor_mode) { /* Initialize the default instance */ vm = vm_acquire("default"); assert(vm != NULL); if (vm_init_instance(vm) == -1) { fprintf(stderr,"Unable to initialize router instance.\n"); exit(EXIT_FAILURE); } #if (DEBUG_INSN_PERF_CNT > 0) || (DEBUG_BLOCK_PERF_CNT > 0) { m_uint32_t counter,prev = 0,delta; while(vm->status == VM_STATUS_RUNNING) { counter = cpu_get_perf_counter(vm->boot_cpu); delta = counter - prev; prev = counter; printf("delta = %u\n",delta); sleep(1); } } #else /* Start instance monitoring */ vm_monitor(vm); #endif /* Free resources used by instance */ vm_release(vm); } else { hypervisor_tcp_server(hypervisor_ip_address,hypervisor_tcp_port); } dynamips_reset(); close_log_file(); return(0); }
/* Parse the command line */ static int parse_std_cmd_line(int argc,char *argv[]) { char *options_list = "r:o:n:c:m:l:C:i:jt:p:s:k:T:U:A:B:a:f:E:b:S:R:M:eXP:N:G:g:L:I:"; vm_platform_t *platform; vm_instance_t *vm = NULL; int instance_id; int option; char *str; FILE *pid_file = NULL; // For saving the pid if requested /* Get the instance ID */ instance_id = 0; /* Use the old VM file naming type */ vm_file_naming_type = 1; cli_load_plugins(argc,argv); if ((str = cli_find_option(argc,argv,"-i"))) { instance_id = atoi(str); printf("Instance ID set to %d.\n",instance_id); } if ((str = cli_find_option(argc,argv,"-N"))) vm_file_naming_type = atoi(str); /* Get the platform type */ if (!(platform = cli_get_platform_type(argc,argv))) goto exit_failure; /* Create the default instance */ if (!(vm = cli_create_instance("default",platform->name,instance_id))) goto exit_failure; opterr = 0; vtty_set_ctrlhandler(1); /* By default allow ctrl ] */ vtty_set_telnetmsg(1); /* By default allow telnet message */ while((option = getopt_long(argc,argv,options_list, cmd_line_lopts,NULL)) != -1) { switch(option) { /* Instance ID (already managed) */ case 'i': break; /* Platform (already managed) */ case 'P': break; /* RAM size */ case 'r': vm->ram_size = strtol(optarg, NULL, 10); printf("Virtual RAM size set to %d MB.\n",vm->ram_size); break; /* ROM size */ case 'o': vm->rom_size = strtol(optarg, NULL, 10); printf("Virtual ROM size set to %d MB.\n",vm->rom_size); break; /* NVRAM size */ case 'n': vm->nvram_size = strtol(optarg, NULL, 10); printf("NVRAM size set to %d KB.\n",vm->nvram_size); break; /* PCMCIA disk0 size */ case OPT_DISK0_SIZE: vm->pcmcia_disk_size[0] = atoi(optarg); printf("PCMCIA ATA disk0 size set to %u MB.\n", vm->pcmcia_disk_size[0]); break; /* PCMCIA disk1 size */ case OPT_DISK1_SIZE: vm->pcmcia_disk_size[1] = atoi(optarg); printf("PCMCIA ATA disk1 size set to %u MB.\n", vm->pcmcia_disk_size[1]); break; case OPT_NOCTRL: vtty_set_ctrlhandler(0); /* Ignore ctrl ] */ printf("Block ctrl+] access to monitor console.\n"); break; /* Config Register */ case 'c': vm->conf_reg_setup = strtol(optarg, NULL, 0); printf("Config. Register set to 0x%x.\n",vm->conf_reg_setup); break; /* IOS startup configuration file */ case 'C': case OPT_STARTUP_CONFIG_FILE: vm_ios_set_config(vm,optarg,vm->ios_private_config); break; /* IOS private configuration file */ case OPT_PRIVATE_CONFIG_FILE: vm_ios_set_config(vm,vm->ios_startup_config,optarg); break; /* Use physical memory to emulate RAM (no-mapped file) */ case 'X': vm->ram_mmap = 0; break; /* Use a ghost file to simulate RAM */ case 'G': free(vm->ghost_ram_filename); vm->ghost_ram_filename = strdup(optarg); vm->ghost_status = VM_GHOST_RAM_USE; break; /* Generate a ghost RAM image */ case 'g': free(vm->ghost_ram_filename); vm->ghost_ram_filename = strdup(optarg); vm->ghost_status = VM_GHOST_RAM_GENERATE; break; /* Use sparse memory */ case OPT_SPARSE_MEM: vm->sparse_mem = TRUE; break; /* Alternate ROM */ case 'R': vm->rom_filename = optarg; break; case OPT_NOTELMSG: vtty_set_telnetmsg(0); /* disable telnet greeting */ printf("Prevent telnet message on AUX/CONSOLE connecte.\n"); break; case OPT_FILEPID: if ((pid_file = fopen(optarg,"w"))) { fprintf(pid_file,"%d",getpid()); fclose(pid_file); } else { printf("Unable to save to %s.\n",optarg); } break; /* Idle PC */ case OPT_IDLE_PC: vm->idle_pc = strtoull(optarg,NULL,0); printf("Idle PC set to 0x%llx.\n",vm->idle_pc); break; /* Timer IRQ check interval */ case OPT_TIMER_ITV: vm->timer_irq_check_itv = atoi(optarg); break; /* Clock divisor */ case 'k': vm->clock_divisor = atoi(optarg); if (!vm->clock_divisor) { fprintf(stderr,"Invalid Clock Divisor specified!\n"); goto exit_failure; } printf("Using a clock divisor of %d.\n",vm->clock_divisor); break; /* Disable JIT */ case 'j': vm->jit_use = FALSE; break; /* VM debug level */ case OPT_VM_DEBUG: vm->debug_level = atoi(optarg); break; /* Log file */ case 'l': if (!(log_file_name = realloc(log_file_name, strlen(optarg)+1))) { fprintf(stderr,"Unable to set log file name.\n"); goto exit_failure; } strcpy(log_file_name, optarg); printf("Log file: writing to %s\n",log_file_name); break; #if DEBUG_SYM_TREE /* Symbol file */ case 'S': vm->sym_filename = strdup(optarg); break; #endif /* TCP server for Console Port */ case 'T': vm->vtty_con_type = VTTY_TYPE_TCP; vm->vtty_con_tcp_port = atoi(optarg); break; /* Serial interface for Console port */ case 'U': vm->vtty_con_type = VTTY_TYPE_SERIAL; if (vtty_parse_serial_option(&vm->vtty_con_serial_option,optarg)) { fprintf(stderr, "Invalid Console serial interface descriptor!\n"); goto exit_failure; } break; /* TCP server for AUX Port */ case 'A': vm->vtty_aux_type = VTTY_TYPE_TCP; vm->vtty_aux_tcp_port = atoi(optarg); break; /* Serial interface for AUX port */ case 'B': vm->vtty_aux_type = VTTY_TYPE_SERIAL; if (vtty_parse_serial_option(&vm->vtty_aux_serial_option,optarg)) { fprintf(stderr,"Invalid AUX serial interface descriptor!\n"); goto exit_failure; } break; /* Port settings */ case 'p': vm_slot_cmd_create(vm,optarg); break; /* NIO settings */ case 's': vm_slot_cmd_add_nio(vm,optarg); break; /* Virtual ATM switch */ case 'a': if (atmsw_start(optarg) == -1) goto exit_failure; break; /* Virtual ATM bridge */ case 'M': if (atm_bridge_start(optarg) == -1) goto exit_failure; break; /* Virtual Frame-Relay switch */ case 'f': if (frsw_start(optarg) == -1) goto exit_failure; break; /* Virtual Ethernet switch */ case 'E': if (ethsw_start(optarg) == -1) goto exit_failure; break; /* Virtual bridge */ case 'b': if (netio_bridge_start(optarg) == -1) goto exit_failure; break; #ifdef GEN_ETH /* Ethernet device list */ case 'e': gen_eth_show_dev_list(); goto exit_success; #endif /* Load plugin (already handled) */ case 'L': break; /* Oops ! */ case '?': show_usage(vm,argc,argv); goto exit_failure; /* Parse options specific to the platform */ default: if (vm->platform->cli_parse_options != NULL) /* If you get an option wrong, say which option is was */ /* Wont be pretty for a long option, but it will at least help */ if (vm->platform->cli_parse_options(vm,option) == -1) { printf("Flag not recognised: -%c\n",(char)option); goto exit_failure; } } } /* Last argument, this is the IOS filename */ if (optind == (argc - 1)) { /* setting IOS image file */ vm_ios_set_image(vm,argv[optind]); printf("IOS image file: %s\n\n",vm->ios_image); } else { /* IOS missing */ fprintf(stderr,"Please specify an IOS image filename\n"); show_usage(vm,argc,argv); goto exit_failure; } vm_release(vm); return(0); exit_success: if (vm) { vm_release(vm); vm_delete_instance("default"); } exit(EXIT_SUCCESS); return(-1); exit_failure: if (vm) { vm_release(vm); vm_delete_instance("default"); } exit(EXIT_FAILURE); return(-1); }
/* Push IOS configuration to a given router */ static int cmd_push_config(hypervisor_conn_t *conn,int argc,char *argv[]) { vm_instance_t *vm; u_char *startup_config = NULL; u_char *private_config = NULL; int startup_len = 0; int private_len = 0; if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM))) return(-1); if (!vm->platform->nvram_push_config) goto err_no_push_method; /* * Convert base64 input to standard text. base64 uses 4 bytes for each group of 3 bytes. */ if (strcmp(argv[1],"(keep)") != 0) { startup_len = (strlen(argv[1]) + 3) / 4 * 3; if (!(startup_config = malloc(1 + startup_len))) goto err_alloc_base64; if ((startup_len = base64_decode(startup_config,(u_char *)argv[1],startup_len)) < 0) goto err_decode_base64; startup_config[startup_len] = '\0'; } if (argc > 2 && strcmp(argv[2],"(keep)") != 0) { private_len = (strlen(argv[2]) + 3) / 4 * 3; if (!(private_config = malloc(1 + private_len))) goto err_alloc_base64; if ((private_len = base64_decode(private_config,(u_char *)argv[2],private_len)) < 0) goto err_decode_base64; private_config[private_len] = '\0'; } /* Push configuration */ if (vm->platform->nvram_push_config(vm,startup_config,(size_t)startup_len,private_config,(size_t)private_len) < 0) goto err_nvram_push; free(private_config); free(startup_config); vm_release(vm); hypervisor_send_reply(conn,HSC_INFO_OK,1, "IOS config file pushed tm VM '%s'", argv[0]); return(0); err_nvram_push: err_decode_base64: err_alloc_base64: free(private_config); free(startup_config); err_no_push_method: vm_release(vm); hypervisor_send_reply(conn,HSC_ERR_CREATE,1, "unable to push IOS config for VM '%s'", argv[0]); return(-1); }
/* Tests covered here: - TEST_VM_PROT_* program slices actually succeeds when a crash occurs - TEST_VM_MAP_ANON* program slices succeeds when it could be compiled */ int main(void) { vm_init(); signal(SIGSEGV, fault_handler); #ifdef SIGBUS signal(SIGBUS, fault_handler); #endif #define page_align(address) ((char *)((vm_uintptr_t)(address) & -page_size)) vm_uintptr_t page_size = vm_get_page_size(); const int area_size = 6 * page_size; volatile char * area = (volatile char *) vm_acquire(area_size); volatile char * fault_address = area + (page_size * 7) / 2; #if defined(TEST_VM_MMAP_ANON) || defined(TEST_VM_MMAP_ANONYMOUS) if (area == VM_MAP_FAILED) return 1; if (vm_release((char *)area, area_size) < 0) return 1; return 0; #endif #if defined(TEST_VM_PROT_NONE_READ) || defined(TEST_VM_PROT_NONE_WRITE) if (area == VM_MAP_FAILED) return 0; if (vm_protect(page_align(fault_address), page_size, VM_PAGE_NOACCESS) < 0) return 0; #endif #if defined(TEST_VM_PROT_RDWR_WRITE) if (area == VM_MAP_FAILED) return 1; if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0) return 1; if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0) return 1; #endif #if defined(TEST_VM_PROT_READ_WRITE) if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0) return 0; #endif #if defined(TEST_VM_PROT_NONE_READ) // this should cause a core dump char foo = *fault_address; return 0; #endif #if defined(TEST_VM_PROT_NONE_WRITE) || defined(TEST_VM_PROT_READ_WRITE) // this should cause a core dump *fault_address = 'z'; return 0; #endif #if defined(TEST_VM_PROT_RDWR_WRITE) // this should not cause a core dump *fault_address = 'z'; return 0; #endif }
static inline int vm_mac_release(uint32 addr, uint32 size) { return vm_release(Mac2HostAddr(addr), size); }