Esempio n. 1
0
/*
 * Extract from the PRI the processor, strand and their fru identity
 */
int
cpu_mdesc_init(topo_mod_t *mod, md_info_t *chip)
{
	int rc = -1;
	md_t *mdp;
	ssize_t bufsiz = 0;
	uint64_t *bufp;
	ldom_hdl_t *lhp;
	uint32_t type = 0;

	/* get the PRI/MD */
	if ((lhp = ldom_init(cpu_alloc, cpu_free)) == NULL) {
		topo_mod_dprintf(mod, "ldom_init() failed\n");
		return (topo_mod_seterrno(mod, EMOD_NOMEM));
	}

	(void) ldom_get_type(lhp, &type);
	if ((type & LDOM_TYPE_CONTROL) != 0) {
		bufsiz = ldom_get_core_md(lhp, &bufp);
	} else {
		bufsiz = ldom_get_local_md(lhp, &bufp);
	}
	if (bufsiz <= 0) {
		topo_mod_dprintf(mod, "failed to get the PRI/MD\n");
		ldom_fini(lhp);
		return (-1);
	}

	if ((mdp = md_init_intern(bufp, cpu_alloc, cpu_free)) == NULL ||
	    md_node_count(mdp) <= 0) {
		cpu_free(bufp, (size_t)bufsiz);
		ldom_fini(lhp);
		return (-1);
	}

	/*
	 * N1 MD contains cpu nodes while N2 MD contains component nodes.
	 */
	if (md_find_name(mdp, MD_STR_COMPONENT) != MDE_INVAL_STR_COOKIE) {
		rc = cpu_n2_mdesc_init(mod, mdp, chip);
	} else if (md_find_name(mdp, MD_STR_CPU) != MDE_INVAL_STR_COOKIE) {
		rc =  cpu_n1_mdesc_init(mod, mdp, chip);
	} else {
		topo_mod_dprintf(mod, "Unsupported PRI/MD\n");
		rc = -1;
	}

	cpu_free(bufp, (size_t)bufsiz);
	(void) md_fini(mdp);
	ldom_fini(lhp);

	return (rc);
}
Esempio n. 2
0
void nes_free(NES *nes)
{
    // disable ndb debugging will make cpu keep running
    ndb_set_debug(&(nes->ndb), NDB_DEBUG_MODE_DISABLE);

    // destroy nes thread
    nes->thread_exit = TRUE;
    pthread_join(nes->thread_id, NULL);

    // free replay
    replay_free(&(nes->replay));

    // free joypad
    joypad_setkey(&(nes->pad), 0, NES_PAD_CONNECT, 0);
    joypad_setkey(&(nes->pad), 1, NES_PAD_CONNECT, 0);
    joypad_free  (&(nes->pad ));

    // free cpu & ppu & apu & mmc
    cpu_free(&(nes->cpu));
    ppu_free(&(nes->ppu));
    apu_free(&(nes->apu));
    mmc_free(&(nes->mmc));
    ndb_free(&(nes->ndb));

    // free cartridge
    cartridge_free(&(nes->cart));

    log_done(); // log done
}
Esempio n. 3
0
File: os.c Progetto: jiamacs/rhype
void
os_free(struct os *os)
{
	int i;

#ifdef HAS_HTAB
	if (!os->cached_partition_info[1].sfw_tlb) {
		htab_free(os);
	}
#endif /* HAS_HTAB */

	if (os->po_psm)
		os_psm_destroy(os->po_psm);

	for (i = 0; i < os->installed_cpus; i++) {
		cpu_free(os->cpu[i]);
		os->cpu[i] = NULL;
	}

	ht_remove(&os_hash, os->po_lpid);

	os->po_lpid = -1;

	hfree(os, sizeof(*os));
}
Esempio n. 4
0
void host_free(Host* host, gpointer userData) {
	MAGIC_ASSERT(host);

	g_queue_free(host->applications);

	topology_detach(worker_getTopology(), networkinterface_getAddress(host->defaultInterface));

	g_hash_table_destroy(host->interfaces);
	g_hash_table_destroy(host->descriptors);
	g_hash_table_destroy(host->shadowToOSHandleMap);
	g_hash_table_destroy(host->osToShadowHandleMap);
	g_hash_table_destroy(host->unixPathToPortMap);

	g_free(host->name);

	eventqueue_free(host->events);
	cpu_free(host->cpu);
	tracker_free(host->tracker);

	g_queue_free(host->availableDescriptors);

	g_mutex_clear(&(host->lock));

	MAGIC_CLEAR(host);
	g_free(host);
}
Esempio n. 5
0
void nes_free(NES *nes)
{
    // destroy nes event & thread
    nes->bExitThread = TRUE;
    SetEvent(nes->hNesEvent);
    WaitForSingleObject(nes->hNesThread, -1);
    CloseHandle(nes->hNesEvent );
    CloseHandle(nes->hNesThread);

    cpu_free      (&(nes->cpu));
    ppu_free      (&(nes->ppu));
    apu_free      (&(nes->apu));
    mmc_free      (&(nes->mmc));
    joypad_setkey (&(nes->pad), 0, NES_PAD_CONNECT, 0);
    joypad_setkey (&(nes->pad), 1, NES_PAD_CONNECT, 0);
    joypad_free   (&(nes->pad )); // free joypad
    cartridge_free(&(nes->cart)); // free cartridge

    log_done();
}
Esempio n. 6
0
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;
}
Esempio n. 7
0
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;
}
Esempio n. 8
0
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);
}
Esempio n. 9
0
int main(int argc, char *argv[])
{
    cpu_t *cpu = cpu_create();
    uint8_t opcode;

    FILE *file = NULL;
    char *dump_ram = NULL;
    char *dump_flash = NULL;
    char **buffer = NULL;
    int i;

    if(argc != 2 && argc != 4 && argc != 6)
    {
        puts("usage: fsim <in> [--dumpram|-r <ram filename>] [--dumpflash|-f <flash filename>]");
        return EXIT_SUCCESS;
    }
    for (i = 2; i < argc; i++)
    {
        if (memcmp("--dumpram", argv[i], 9) == 0 || memcmp("-r", argv[i], 2) == 0)
        {
            buffer = &dump_ram;
        }
        else if (memcmp("--dumpflash", argv[i], 11) == 0 || memcmp("-f", argv[i], 2) == 0)
        {
            buffer = &dump_flash;
        }
        else if (buffer)
        {
            *buffer = argv[i];
            buffer = NULL;
        }
        else
        {
            printf("unknown option \"%s\"\n", argv[i]);
            return EXIT_FAILURE;
        }
    }
    if (buffer)
    {
         puts("Not all options set");
         return EXIT_FAILURE;
    }

    file = fopen(argv[1], "r");

    if(!file)
    {
        printf("could not open file \"%s\"",argv[1]);
        return EXIT_FAILURE;
    }

    fread(cpu->flash, sizeof(cpu->flash), 1, file);

    fclose(file);
    file = NULL;

    printf("First 160 bytes of flash:");
    for(i = 0;i < 160; i++) {
        if (i % 16 == 0)
        {
            printf("\n%08x:", i + 0x01000000);
        }
        if (i % 2 == 0)
        {
            printf(" ");
        }
        printf("%02x", cpu->flash[i]);
    }
    printf("\n\n");

    while(!cpu->status)
    {
        opcode = cpu_step(cpu);
    }
    switch (cpu->status)
    {
        case 2:
            printf("Illegal opcode \"%02x\"\n", opcode);
            break;
        case 1:
            puts("");
            puts("####################");
            puts("#        ##        #");
            puts("#### Halted CPU ####");
            puts("#        ##        #");
            puts("####################");
            puts("");
            break;
        default:
            printf("Unknown exit status %d", cpu->status);
    }

    puts("Register Dump:");
    printf("A  = %08x\n", cpu->a);
    printf("X  = %08x\n", cpu->x);
    printf("PC = %08x\n", cpu->pc);
    printf("SP = %08x\n", cpu->sp);
    printf("z  = %01d\n", cpu->z);
    printf("n  = %01d\n", cpu->n);
    printf("i  = %01d\n", cpu->i);
    printf("if = %02x\n", cpu->interrupt_flags);
    printf("iv = %08x\n", cpu->interrupt_vector);
    puts("");
    dump_stack(cpu, 10);

    if (dump_flash)
    {
        file = fopen(dump_flash, "w");
        if(!file)
        {
            printf("could not open flash file \"%s\"", dump_flash);
        } else {
            fwrite(cpu->flash, sizeof(cpu->flash), 1, file);
            fclose(file);
            printf("Dumped flash to \"%s\"\n", dump_flash);
        }
    }

    if (dump_ram)
    {
        file = fopen(dump_ram, "w");
        if(!file)
        {
            printf("could not open ram file \"%s\"", dump_ram);
        } else {
            fwrite(cpu->ram, sizeof(cpu->ram), 1, file);
            fclose(file);
            printf("Dumped ram to \"%s\"\n", dump_ram);
        }
    }
    cpu = cpu_free(cpu);

    return 0;
}
Esempio n. 10
0
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;
}