void arm_dyncom_init(arm_core_t* core){ cpu_t* cpu = cpu_new(0, 0, arm_arch_func); /* init the reg structure */ cpu->rf.pc = &core->Reg[15]; cpu->rf.phys_pc = &core->Reg[15]; cpu->rf.grf = core->Reg; cpu->rf.srf = core->Spsr; cpu->cpu_data = (conf_object_t*)core; core->dyncom_cpu = get_conf_obj_by_cast(cpu, "cpu_t"); cpu->debug_func = arm_debug_func; sky_pref_t *pref = get_skyeye_pref(); if(pref->user_mode_sim){ cpu->syscall_func = arm_dyncom_syscall; } else cpu->syscall_func = NULL; cpu->dyncom_engine->code_start = 0x8000; cpu->dyncom_engine->code_end = 0x100000; cpu->dyncom_engine->code_entry = 0x80d0; return; }
VM* vm_new(int num_cpus) { if (num_cpus <= 0) { LOG_ERROR("vm_new: num_cpus <= 0"); return NULL; } VM* vm = (VM*)IOMalloc(sizeof(VM)); if (!vm) { LOG_ERROR("vm_new: can't allocate VM"); return NULL; } memset(vm, 0, sizeof(VM)); vm->num_cpus = num_cpus; vm->cpus = (CPU**)IOMalloc(sizeof(CPU*) * num_cpus); if (!vm->cpus) { LOG_ERROR("vm_new: can't allocate cpus[]"); vm_delete(vm); return NULL; } memset(vm->cpus, 0, sizeof(sizeof(CPU*) * num_cpus)); for (int i = 0; i < vm->num_cpus; i++) { CPU* cpu = cpu_new(); if (!cpu) { vm_delete(vm); return NULL; } vm->cpus[i] = cpu; } LOG("created VM"); return vm; }
int main(int argc, char **argv) { CPU *cpu; if(argc < 3) { fprintf(stderr, "Linha de comando inválida!\n"); return EXIT_FAILURE; } if ((cpuError_new(CPUERROR_FAILUREDESCLENGTH)) == NULL) { fprintf(stderr, "CPU: %s\n", CPUERROR_EALLOC_MSG); return EXIT_FAILURE; } if((cpu = cpu_new(QTD_REG, 1)) == CPU_EALLOC) { fprintf(stderr, "CPU: %s\n", cpuError_getDesc()); return EXIT_FAILURE; } if(cpu_start(cpu, argv[1], argv[2]) == CPU_ERROR) { fprintf(stderr, "CPU: %s\n", cpuError_getDesc()); return EXIT_FAILURE; } else { printf("Programa executado com sucesso! =D\n"); } cpu_free(cpu); cpuError_free(); return EXIT_SUCCESS; }
void mips_dyncom_init(mips_core_t *core) { cpu_t* cpu = cpu_new(0, 0, arch_func_mips); /* init the reg structure */ cpu->rf.pc = &core->pc; cpu->rf.phys_pc = &core->pc; cpu->rf.grf = core->gpr; cpu->rf.frf = core->fpr; cpu->rf.srf = core->cp0; cpu_set_flags_debug(cpu, 0 | CPU_DEBUG_PRINT_IR | CPU_DEBUG_LOG ); cpu_set_flags_codegen(cpu, CPU_CODEGEN_TAG_LIMIT); cpu->cpu_data = (conf_object_t*)core; core->dyncom_cpu = get_conf_obj_by_cast(cpu, "cpu_t"); cpu->debug_func = mips_debug_func; sky_pref_t *pref = get_skyeye_pref(); set_memory_operator(arch_mips_read_memory, arch_mips_write_memory); if(pref->user_mode_sim){ //cpu->syscall_func = mips_dyncom_syscall; cpu->syscall_func = NULL; } else cpu->syscall_func = NULL; cpu->dyncom_engine->code_start = 0x0; cpu->dyncom_engine->code_end = 0x100000; cpu->dyncom_engine->code_entry = 0x0; return; }
int main(int argc, char **argv) { char *executable; cpu_arch_t arch; cpu_t *cpu; uint8_t *RAM; FILE *f; int ramsize; int singlestep = SINGLESTEP_NONE; int log = 1; int print_ir = 0; /* parameter parsing */ if (argc < 2) { printf("Usage: %s [--print-ir] <binary>\n", argv[0]); return 0; } if (!strcmp(argv[1], "--print-ir")) { print_ir = 1; argv++; } executable = argv[1]; arch = CPU_ARCH_I386; ramsize = 1*1024*1024; RAM = (uint8_t*)malloc(ramsize); cpu = cpu_new(arch, 0, 0); cpu_set_flags_codegen(cpu, CPU_CODEGEN_OPTIMIZE); cpu_set_flags_debug(cpu, 0 | (print_ir? CPU_DEBUG_PRINT_IR : 0) | (print_ir? CPU_DEBUG_PRINT_IR_OPTIMIZED : 0) | (log? CPU_DEBUG_LOG :0) | (singlestep == SINGLESTEP_STEP? CPU_DEBUG_SINGLESTEP : 0) | (singlestep == SINGLESTEP_BB? CPU_DEBUG_SINGLESTEP_BB : 0) ); cpu_set_ram(cpu, RAM); /* load code */ if (!(f = fopen(executable, "rb"))) { printf("Could not open %s!\n", executable); return 2; } cpu->code_start = START; cpu->code_end = cpu->code_start + fread(&RAM[cpu->code_start], 1, ramsize-cpu->code_start, f); fclose(f); cpu->code_entry = cpu->code_start + ENTRY; cpu_tag(cpu, cpu->code_entry); cpu_translate(cpu); /* force translation now */ printf("\n*** Executing...\n"); printf("GUEST run..."); fflush(stdout); cpu_run(cpu, debug_function); printf("done!\n"); cpu_free(cpu); return 0; }
Host* host_new(GQuark id, gchar* hostname, gchar* ipHint, gchar* geocodeHint, gchar* typeHint, guint64 requestedBWDownKiBps, guint64 requestedBWUpKiBps, guint cpuFrequency, gint cpuThreshold, gint cpuPrecision, guint nodeSeed, SimulationTime heartbeatInterval, GLogLevelFlags heartbeatLogLevel, gchar* heartbeatLogInfo, GLogLevelFlags logLevel, gboolean logPcap, gchar* pcapDir, gchar* qdisc, guint64 receiveBufferSize, gboolean autotuneReceiveBuffer, guint64 sendBufferSize, gboolean autotuneSendBuffer, guint64 interfaceReceiveLength) { Host* host = g_new0(Host, 1); MAGIC_INIT(host); host->id = id; host->name = g_strdup(hostname); host->random = random_new(nodeSeed); /* get unique virtual address identifiers for each network interface */ Address* loopbackAddress = dns_register(worker_getDNS(), host->id, host->name, "127.0.0.1"); Address* ethernetAddress = dns_register(worker_getDNS(), host->id, host->name, ipHint); /* connect to topology and get the default bandwidth */ guint64 bwDownKiBps = 0, bwUpKiBps = 0; topology_attach(worker_getTopology(), ethernetAddress, host->random, ipHint, geocodeHint, typeHint, &bwDownKiBps, &bwUpKiBps); /* prefer assigned bandwidth if available */ if(requestedBWDownKiBps) { bwDownKiBps = requestedBWDownKiBps; } if(requestedBWUpKiBps) { bwUpKiBps = requestedBWUpKiBps; } /* virtual addresses and interfaces for managing network I/O */ NetworkInterface* loopback = networkinterface_new(loopbackAddress, G_MAXUINT32, G_MAXUINT32, logPcap, pcapDir, qdisc, interfaceReceiveLength); NetworkInterface* ethernet = networkinterface_new(ethernetAddress, bwDownKiBps, bwUpKiBps, logPcap, pcapDir, qdisc, interfaceReceiveLength); host->interfaces = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) networkinterface_free); g_hash_table_replace(host->interfaces, GUINT_TO_POINTER((guint)networkinterface_getIPAddress(ethernet)), ethernet); g_hash_table_replace(host->interfaces, GUINT_TO_POINTER((guint)htonl(INADDR_LOOPBACK)), loopback); host->defaultInterface = ethernet; /* thread-level event communication with other nodes */ g_mutex_init(&(host->lock)); host->events = eventqueue_new(); host->availableDescriptors = g_queue_new(); host->descriptorHandleCounter = MIN_DESCRIPTOR; /* virtual descriptor management */ host->descriptors = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, descriptor_unref); host->receiveBufferSize = receiveBufferSize; host->sendBufferSize = sendBufferSize; host->autotuneReceiveBuffer = autotuneReceiveBuffer; host->autotuneSendBuffer = autotuneSendBuffer; host->shadowToOSHandleMap = g_hash_table_new(g_direct_hash, g_direct_equal); host->osToShadowHandleMap = g_hash_table_new(g_direct_hash, g_direct_equal); host->unixPathToPortMap = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); /* applications this node will run */ host->applications = g_queue_new(); host->cpu = cpu_new(cpuFrequency, cpuThreshold, cpuPrecision); host->tracker = tracker_new(heartbeatInterval, heartbeatLogLevel, heartbeatLogInfo); host->logLevel = logLevel; host->logPcap = logPcap; host->pcapDir = pcapDir; message("Created Host '%s', ip %s, " "%"G_GUINT64_FORMAT" bwUpKiBps, %"G_GUINT64_FORMAT" bwDownKiBps, %"G_GUINT64_FORMAT" initSockSendBufSize, %"G_GUINT64_FORMAT" initSockRecvBufSize, " "%u cpuFrequency, %i cpuThreshold, %i cpuPrecision, %u seed", g_quark_to_string(host->id), networkinterface_getIPName(host->defaultInterface), bwUpKiBps, bwDownKiBps, sendBufferSize, receiveBufferSize, cpuFrequency, cpuThreshold, cpuPrecision, nodeSeed); return host; }
int main(int ac, char **av, char **ep) { int rc; cpu_t *cpu; xec_guest_info_t guest_info; xec_mem_if_t *mem_if; xec_monitor_t *monitor; xec_us_syscall_if_t *us_syscall; m88k_uintptr_t stack_top; nix_env_t *env; bool debugging = false; aspace_lock(); if (ac < 2) { fprintf(stderr, "usage: %s <executable> [args...]\n", *av); exit(EXIT_FAILURE); } /* Initialize xec, nix and loader. */ xec_init(); obsd41_init(); loader_init(); /* Create CPU */ cpu = cpu_new(CPU_ARCH_M88K, CPU_FLAG_ENDIAN_BIG, 0); if (cpu == NULL) { fprintf(stderr, "error: failed initializing M88K architecture.\n"); exit(EXIT_FAILURE); } /* Create XEC bridge mem-if */ mem_if = run88_new_mem_if(); /* Create the XEC US Syscall */ us_syscall = obsd41_us_syscall_create(mem_if); if (us_syscall == NULL) { fprintf(stderr, "error: failed creating xec userspace syscall.\n"); exit(EXIT_FAILURE); } /* Create NIX env */ env = nix_env_create(mem_if); if (env == NULL) { fprintf(stderr, "error: failed creating nix environment.\n"); exit(EXIT_FAILURE); } /* Load the executable */ rc = loader_load(mem_if, av[1]); if (rc != LOADER_SUCCESS) { fprintf(stderr, "error: cannot load executable '%s', error=%d.\n", av[1], rc); exit(EXIT_FAILURE); } /* Setup arguments */ g_uframe_log = xec_log_register("uframe"); #ifndef DEBUGGER xec_log_disable("nix"); xec_log_disable("openbsd41"); xec_log_disable("uframe"); xec_log_disable(NULL); #endif openbsd_m88k_setup_uframe(cpu, mem_if, ac - 1, av + 1, ep, &stack_top); /* Setup the CPU */ cpu_set_flags_codegen(cpu, CPU_CODEGEN_OPTIMIZE|CPU_CODEGEN_TAG_LIMIT); cpu_set_flags_debug(cpu, CPU_DEBUG_NONE); //cpu_set_flags_debug(cpu, CPU_DEBUG_SINGLESTEP_BB); cpu_set_flags_hint(cpu, CPU_HINT_TRAP_RETURNS_TWICE); cpu_set_ram(cpu, RAM); /* Create XEC bridge monitor */ guest_info.name = cpu->info.name; guest_info.endian = (cpu->info.common_flags & CPU_FLAG_ENDIAN_MASK) == CPU_FLAG_ENDIAN_BIG ? XEC_ENDIAN_BIG : XEC_ENDIAN_LITTLE; guest_info.byte_size = cpu->info.byte_size; guest_info.word_size = cpu->info.word_size; guest_info.page_size = cpu->info.default_page_size; monitor = xec_monitor_create(&guest_info, mem_if, cpu->rf.grf, NULL); if (monitor == NULL) { fprintf(stderr, "error: failed createc xec monitor.\n"); exit(EXIT_FAILURE); } /* Setup registers for execution */ PC = g_ahdr.entry; R[31] = stack_top; // Stack Pointer R[1] = -1; // Return Address cpu->code_start = g_ahdr.tstart; cpu->code_end = g_ahdr.tstart + g_ahdr.tsize; cpu->code_entry = g_ahdr.entry; cpu_tag(cpu, cpu->code_entry); dump_state(RAM, (m88k_grf_t*)cpu->rf.grf); #ifdef DEBUGGER debugging = true; #else fprintf(stderr, "Translating..."); fflush(stderr); cpu_translate(cpu); fprintf(stderr, "done.\n"); #endif aspace_unlock(); for (;;) { if (debugging) { rc = cpu_debugger(cpu, debug_function); if (rc < 0) { debugging = false; continue; } } else { rc = cpu_run(cpu, debug_function); } switch (rc) { case JIT_RETURN_NOERR: /* JIT code wants us to end execution */ break; case JIT_RETURN_FUNCNOTFOUND: #ifndef DEBUGGER fprintf(stderr, "%s: error: 0x%llX not found!\n", __func__, (unsigned long long)PC); fprintf(stderr, "Translating..."); fflush(stderr); cpu_tag(cpu, PC); cpu_flush(cpu); cpu_translate(cpu); fprintf(stderr, "done.\n"); #else dump_state(RAM, (m88k_grf_t*)cpu->rf.grf); if (PC == (uint32_t)(-1U)) goto exit_loop; // bad :( fprintf(stderr, "%s: warning: 0x%llX not found!\n", __func__, (unsigned long long)PC); fprintf(stderr, "PC: "); for (size_t i = 0; i < 16; i++) fprintf(stderr, "%02X ", RAM[PC+i]); fprintf(stderr, "\n"); exit(EXIT_FAILURE); #endif break; case JIT_RETURN_TRAP: // printf("TRAP %u / %u!\n", TRAPNO, R[13]); if (TRAPNO == 0x80 && R[13] == 1) // exit goto exit_loop; // printf("BEFORE:\n"); // dump_state(RAM, (m88k_grf_t*)cpu->rf.grf); xec_us_syscall_dispatch(us_syscall, monitor); // printf("AFTER:\n"); // dump_state(RAM, (m88k_grf_t*)cpu->rf.grf); break; case JIT_RETURN_SINGLESTEP: break; default: fprintf(stderr, "unknown return code: %d\n", rc); goto exit_loop; } if (cpu->flags_debug & (CPU_DEBUG_SINGLESTEP | CPU_DEBUG_SINGLESTEP_BB)) cpu_flush(cpu); } exit_loop: cpu_free(cpu); fprintf(stderr, ">> run88 success\n"); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { char *s_arch; char *executable; cpu_arch_t arch; cpu_t *cpu; uint8_t *RAM; FILE *f; int ramsize; int r1, r2; uint64_t t1, t2, t3, t4; unsigned start_no = START_NO; int singlestep = SINGLESTEP_NONE; int log = 1; int print_ir = 1; /* parameter parsing */ if (argc < 3) { printf("Usage: %s executable [arch] [itercount] [entries]\n", argv[0]); return 0; } s_arch = argv[1]; executable = argv[2]; if (argc >= 4) start_no = atoi(argv[3]); if (!strcmp("mips", s_arch)) arch = CPU_ARCH_MIPS; else if (!strcmp("m88k", s_arch)) arch = CPU_ARCH_M88K; else if (!strcmp("arm", s_arch)) arch = CPU_ARCH_ARM; else if (!strcmp("fapra", s_arch)) arch = CPU_ARCH_FAPRA; else { printf("unknown architecture '%s'!\n", s_arch); return 0; } ramsize = 5*1024*1024; RAM = (uint8_t*)malloc(ramsize); cpu = cpu_new(arch, 0, 0); cpu_set_flags_codegen(cpu, CPU_CODEGEN_OPTIMIZE); cpu_set_flags_debug(cpu, 0 | (print_ir? CPU_DEBUG_PRINT_IR : 0) | (print_ir? CPU_DEBUG_PRINT_IR_OPTIMIZED : 0) | (log? CPU_DEBUG_LOG :0) | (singlestep == SINGLESTEP_STEP? CPU_DEBUG_SINGLESTEP : 0) | (singlestep == SINGLESTEP_BB? CPU_DEBUG_SINGLESTEP_BB : 0) ); cpu_set_ram(cpu, RAM); /* load code */ if (!(f = fopen(executable, "rb"))) { printf("Could not open %s!\n", executable); return 2; } cpu->code_start = START; cpu->code_end = cpu->code_start + fread(&RAM[cpu->code_start], 1, ramsize-cpu->code_start, f); fclose(f); cpu->code_entry = cpu->code_start + ENTRY; cpu_tag(cpu, cpu->code_entry); cpu_translate(cpu); /* force translation now */ printf("\n*** Executing...\n"); printf("number of iterations: %u\n", start_no); uint32_t *reg_pc, *reg_lr, *reg_param, *reg_result; switch (arch) { case CPU_ARCH_M88K: reg_pc = &((m88k_grf_t*)cpu->rf.grf)->sxip; reg_lr = &((m88k_grf_t*)cpu->rf.grf)->r[1]; reg_param = &((m88k_grf_t*)cpu->rf.grf)->r[2]; reg_result = &((m88k_grf_t*)cpu->rf.grf)->r[2]; break; case CPU_ARCH_MIPS: reg_pc = &((reg_mips32_t*)cpu->rf.grf)->pc; reg_lr = &((reg_mips32_t*)cpu->rf.grf)->r[31]; reg_param = &((reg_mips32_t*)cpu->rf.grf)->r[4]; reg_result = &((reg_mips32_t*)cpu->rf.grf)->r[4]; break; case CPU_ARCH_ARM: reg_pc = &((reg_arm_t*)cpu->rf.grf)->pc; reg_lr = &((reg_arm_t*)cpu->rf.grf)->r[14]; reg_param = &((reg_arm_t*)cpu->rf.grf)->r[0]; reg_result = &((reg_arm_t*)cpu->rf.grf)->r[0]; break; case CPU_ARCH_FAPRA: reg_pc = &((reg_fapra32_t*)cpu->rf.grf)->pc; reg_lr = &((reg_fapra32_t*)cpu->rf.grf)->r[0]; reg_param = &((reg_fapra32_t*)cpu->rf.grf)->r[3]; reg_result = &((reg_fapra32_t*)cpu->rf.grf)->r[3]; break; default: fprintf(stderr, "architecture %u not handled.\n", arch); exit(EXIT_FAILURE); } *reg_pc = cpu->code_entry; *reg_lr = RET_MAGIC; *reg_param = start_no; printf("GUEST run..."); fflush(stdout); t1 = abs_time(); cpu_run(cpu, debug_function); t2 = abs_time(); r1 = *reg_result; printf("done!\n"); printf("HOST run..."); fflush(stdout); t3 = abs_time(); r2 = fib(start_no); t4 = abs_time(); printf("done!\n"); cpu_free(cpu); printf("Time GUEST: %lld\n", t2-t1); printf("Time HOST: %lld\n", t4-t3); printf("Result HOST: %d\n", r2); printf("Result GUEST: %d\n", r1); printf("GUEST required \033[1m%.2f%%\033[22m of HOST time.\n", (float)(t2-t1)/(float)(t4-t3)*100); if (r1 == r2) printf("\033[1mSUCCESS!\033[22m\n\n"); else printf("\033[1mFAILED!\033[22m\n\n"); return 0; }