示例#1
0
文件: vm.c 项目: johanhenriksson/vm
int main(int argc, char* argv[]) 
{
	char* image = "image";
	if (argc > 1)
		image = argv[1];

	vm_init();

	mop_add(0, 1, 0, 0, 10); // r1 = 10
	mop_add(8, 2, 0, 0, 20); // r2 = 20
	mop_push(16, 1); // push r1
	mop_push(24, 2); // push r2
	mop_add(32, 2, 0, 0, 0); // r2 = 0
	mop_sub(40, 1, 1, 0, 1); // r1--
	mop_prti(48, 1); // print r1
	mop_jnz(64, 1, 32);
	mop_pop(72, 2); // pop r2
	mop_pop(80, 1); // pop r1
	mop_prti(88, 1); // print r1, should be 10
	mop_prti(96, 2); // print r2
	mop_exit(104);

	vm_run(0);

	vm_exit();

/*
	vm_init();
	vm_load(image);
	vm_run(ENTRY_POINT);
*/	
	return exitval;
}
static char * test_arrinit()
{
    int err = 0;
    int code_size;
    struct vm_state state;
    char input[] = "3\n";

    /* Write input values to stdin */
    uu_open_stdin_writer();
    uu_write_stdin(input);
    uu_close_stdin_writer();

    b91_loader_read_file(mem, memsize, &code_size, "asm/arrinit.b91");
    pu_assert("Error while loading a b91 binary file.", err == 0);

    vm_init_state(&state, code_size, memsize);
    vm_run(&state, mem);

    pu_assert_equal("Correct r1 value", state.regs[1], 3);
    pu_assert_equal("Correct r2 value", state.regs[2], 2716);
    pu_assert_equal("Correct r3 value", state.regs[3], 2716);
    pu_assert_equal("Correct mem value at", mem[20], -875);
    pu_assert_equal("Correct mem value at", mem[25], 860);
    pu_assert_equal("Correct mem value at", mem[27], 1554);
    return 0;
}
示例#3
0
int	launch_vm(t_param *param)
{
  t_vm	*vm;
  int	i;

  if ((vm = my_malloc(sizeof(t_vm), "Error: fail to malloc vm\n")) == 0)
    return (1);
  vm->screen = SDL_SetVideoMode(MEM_SIZE / NB_COL * WIDTH + DECAL, NB_COL * WIDTH, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  SDL_WM_SetCaption("Corewar", NULL);
  if (vm->screen == NULL)
    err_SDL();
  vm->police = TTF_OpenFont("arial.ttf", 20);
  vm->param = param;
  vm->cycle = 0;
  vm->cycle_to_die = CYCLE_TO_DIE;
  vm->nb_live = 0;
  owner_set(vm->mem_owner, -1, 0, MEM_SIZE);
  my_memset(vm->register_player, 0, (MAX_PLAYER + 1) * sizeof(char));
  i = 0;
  while (i < MAX_PROG_NUMBER)
    vm->progs_live[i++] = -1;
  if (load_all_programs(vm) != 0)
    return (1);
  my_printf("%d program(s) loaded\n", vm->prog_list->nb_elm);
  if (param->debug == 1)
    dump_memory(vm->memory);
  pause_vm(vm, 1);
  vm_run(vm);
  pause_vm(vm, 0);
  if (param->debug == 1)
    dump_memory(vm->memory);
  TTF_CloseFont(vm->police);
  return (0);
}
示例#4
0
int main(void)
{
	vm_program* program = update_vm_program((vm_program*)vm_data_raw);
	vm_run(program, 0, vm_stack, vm_variables);

	return 0;
}
示例#5
0
static void
vm_suspend_resume(struct vmctx *ctx)
{
	/*
	 * If we get warm reboot request, we don't want to exit the
	 * vcpu_loop/vm_loop/mevent_loop. So we do:
	 *   1. pause VM
	 *   2. flush and clear ioreqs
	 *   3. stop vm watchdog
	 *   4. wait for resume signal
	 *   5. reset vm watchdog
	 *   6. hypercall restart vm
	 */
	vm_pause(ctx);

	vm_clear_ioreq(ctx);
	vm_stop_watchdog(ctx);
	wait_for_resume(ctx);

	pm_backto_wakeup(ctx);
	vm_reset_watchdog(ctx);
	vm_reset(ctx);

	/* set the BSP init state */
	vm_set_vcpu_regs(ctx, &ctx->bsp_regs);
	vm_run(ctx);
}
int _vm_run(struct vmctx *ctx, int vcpu, uint64_t rip, struct vmexit *vmexit)
{
	int error = vm_run(ctx, vcpu, rip, vmexit);
	if (error)
		rb_raise(rb_eException, "vm_get_register failed error=%d", error);
	return error;
}
示例#7
0
void readline_main(void)
{
	token_t *tokenized_buf;
	cons_t *tree_head;
	func_t *func;
	char *input;
	int reg[256];
	
	while(1) {
		input = (char *)readline(">>>");
		add_history(input);
		if(input == NULL){
			break;
		}
		printf("%s\n", input);
		tokenized_buf = tokenize(input);
		treePointer = tokenized_buf;
		tree_head = parse(tokenized_buf);
		func = (func_t *)malloc(sizeof(func_t));
		func->index = 0;
		func->cons = tree_head;
		generatecoder(tree_head, func, 0);
		func->code[func->index].op = RET;
		func->code[func->index].reg0 = 0;
		fprintf(stdout, "%d\n", vm_run(func->code, reg));
	}
}
示例#8
0
文件: VM.c 项目: tehologist/compc
int main()
{
    FILE * f = fopen("eforth.img","rb");
    vm_init(&vm, f, VM_SIZE);
    fclose(f);
    vm_run(&vm,0);
    return 1;
}
示例#9
0
static void
vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
{
	cpuset_t mask;
	int error, rc, prevcpu;
	enum vm_exitcode exitcode;

	if (pincpu >= 0) {
		CPU_ZERO(&mask);
		CPU_SET(pincpu + vcpu, &mask);
		error = pthread_setaffinity_np(pthread_self(),
					       sizeof(mask), &mask);
		assert(error == 0);
	}

	while (1) {
		error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
		if (error != 0) {
			/*
			 * It is possible that 'vmmctl' or some other process
			 * has transitioned the vcpu to CANNOT_RUN state right
			 * before we tried to transition it to RUNNING.
			 *
			 * This is expected to be temporary so just retry.
			 */
			if (errno == EBUSY)
				continue;
			else
				break;
		}

		prevcpu = vcpu;

		exitcode = vmexit[vcpu].exitcode;
		if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
			fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
			    exitcode);
			exit(1);
		}

                rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);

		switch (rc) {
		case VMEXIT_CONTINUE:
                        rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
			break;
		case VMEXIT_RESTART:
                        rip = vmexit[vcpu].rip;
			break;
		case VMEXIT_RESET:
			exit(0);
		default:
			exit(1);
		}
	}
	fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
}
int
xh_vm_run(int vcpu, struct vm_exit *ret_vmexit)
{
	int error;

	vcpu_freeze(vcpu, true);
	error = vm_run(vm, vcpu, ret_vmexit);
	vcpu_freeze(vcpu, false);

	return (error);
}
示例#11
0
int
main(int argc, char* argv[]) {
  if (argc < 2) return -1;
  char *buf = read_file(argv[1]);
  struct VM* vm = vm_new(4000);
  value res = vm_run(vm, buf);
  print_value(res);
  vm_close(vm);
  free(buf);
  return 0;
}
示例#12
0
文件: vm_test.c 项目: hornos/ibniz
int runtest(char*code,uint32_t correct_stacktop)
{
  vm_init();
  vm_compile(code);
  while(!vm.stopped) vm_run();
  if(vm.stack[vm.sp]==correct_stacktop) return 0;
  fprintf(stderr,"unit test failed with code \"%s\"\n",code);
  fprintf(stderr,"stacktop=%x (should have been %x)\n",
    vm.stack[vm.sp],correct_stacktop);
  exit(1);
}
示例#13
0
文件: labels.c 项目: voidlizard/vmcmp
void vm_run( vm_state_t *vm, opcode_t *opcodes )
{
    opcode_t *op = opcodes;
    word *regs = vm->regs;
    word r1 = 0, r2 = 0, r3 = 0;

    static const void * code[256] = {
        &&l_NOP, &&l_LOAD, &&l_MOV, &&l_ADD, &&l_SUB, &&l_NOP, 
        &&l_NOP, &&l_NOP,

        NOP128, NOP64, NOP32, NOP16, 
        &&l_NOP, &&l_NOP, &&l_NOP, &&l_NOP, &&l_NOP, &&l_NOP, &&l_NOP, 
        &&l_DOWN
    };

    goto *code[OPCODE(*op)];

    l_NOP:
        NEXT();
    l_LOAD:
        r1 = R1(*op);
        regs[r1] = *(++op);
        NEXT();
    l_MOV:
        regs[R2(*op)] = regs[R1(*op)];
        NEXT();
    l_ADD:
        regs[R3(*op)] = regs[R1(*op)] + regs[R2(*op)] ;
        NEXT();
    l_SUB:
        regs[R3(*op)] = regs[R1(*op)] - regs[R2(*op)] ;
        NEXT();
    l_DOWN:
        return;

}

int main(int a, char **b)
{
    unsigned int i = 0;
    vm_state_t vm = { {0} };
    opcode_t opcodes[] = {
        #include "bytecode"
    };
    for(i=0;i<500000;i++)
    {
        vm_run(&vm, opcodes);
    }
    printf("R3: %08X\n", vm.regs[R3]);
    return 0;
}
示例#14
0
static void
vm_loop(struct vmctx *ctx, int vcpu, uint64_t startrip)
{
	int error, rc, prevcpu;
	enum vm_exitcode exitcode;
	cpuset_t active_cpus;

	if (vcpumap[vcpu] != NULL) {
		error = pthread_setaffinity_np(pthread_self(),
		    sizeof(cpuset_t), vcpumap[vcpu]);
		assert(error == 0);
	}

	error = vm_active_cpus(ctx, &active_cpus);
	assert(CPU_ISSET(vcpu, &active_cpus));

	error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, startrip);
	assert(error == 0);

	while (1) {
		error = vm_run(ctx, vcpu, &vmexit[vcpu]);
		if (error != 0)
			break;

		prevcpu = vcpu;

		exitcode = vmexit[vcpu].exitcode;
		if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
			fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
			    exitcode);
			exit(1);
		}

                rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);

		switch (rc) {
		case VMEXIT_CONTINUE:
			break;
		case VMEXIT_ABORT:
			abort();
		default:
			exit(1);
		}
	}
	fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
}
示例#15
0
static void
vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
{
	cpuset_t mask;
	int error, rc, prevcpu;
	enum vm_exitcode exitcode;

	if (pincpu >= 0) {
		CPU_ZERO(&mask);
		CPU_SET(pincpu + vcpu, &mask);
		error = pthread_setaffinity_np(pthread_self(),
					       sizeof(mask), &mask);
		assert(error == 0);
	}

	while (1) {
		error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
		if (error != 0)
			break;

		prevcpu = vcpu;

		exitcode = vmexit[vcpu].exitcode;
		if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
			fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
			    exitcode);
			exit(1);
		}

                rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);

		switch (rc) {
		case VMEXIT_CONTINUE:
                        rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
			break;
		case VMEXIT_RESTART:
                        rip = vmexit[vcpu].rip;
			break;
		case VMEXIT_RESET:
			exit(0);
		default:
			exit(1);
		}
	}
	fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
}
示例#16
0
文件: process.c 项目: catseye/Bhuna
void
process_scheduler(void)
{
	struct process *next;

	if ((current_process = run_head) == NULL)
		return;

	for (;;) {
		next = current_process->next;
#ifdef DEBUG
		if (trace_scheduling)
			printf("context switched to process #%d\n", current_process->number);
#endif
		switch (vm_run(current_process->vm, TIMESLICE)) {
		case VM_TERMINATED:
#ifdef DEBUG
			if (trace_scheduling)
				printf("process #%d terminated\n", current_process->number);
#endif
			process_free(current_process);
			break;
		case VM_RETURNED:
#ifdef DEBUG
			if (trace_scheduling)
				printf("process #%d returned\n", current_process->number);
#endif
			process_free(current_process);
			break;
		case VM_WAITING:
#ifdef DEBUG
			if (trace_scheduling)
				printf("process #%d falling asleep\n", current_process->number);
#endif
			process_sleep(current_process);
			break;
		case VM_TIME_EXPIRED:
		default:
			break;
		}
		if ((current_process = next) == NULL)
			if ((current_process = run_head) == NULL)
				return;
	}
}
示例#17
0
文件: main.c 项目: matianfu/redbank
int vm_main() {

  int ret;

  ret = start_server();
  if (ret < 0)
    return 0;

  for (;;) {
    int socket_fd = accept_new_client();
    if (socket_fd < 0)
      return -1;
    vm_run(socket_fd);
  }

  stop_server();
  return 0;
}
示例#18
0
文件: vm.c 项目: ukutaht/owlang
void vm_run_function(vm_t *vm, const char *function_name) {
  uint8_t function_id = strings_lookup(vm->function_names, function_name);

  if (function_id != 0) {
    uint8_t call_code[6] = {
      OP_CALL, 0, function_id, 0,
      OP_EXIT, 0
    };

    memcpy(&vm->code[vm->code_size], &call_code, 6);
    vm->ip = vm->code_size;
    vm->code_size += 6;
    vm_run(vm);
  } else {
    printf("Function %s not found\n", function_name);
    exit(1);
  }
}
示例#19
0
static void
vm_system_reset(struct vmctx *ctx)
{
	/*
	 * If we get system reset request, we don't want to exit the
	 * vcpu_loop/vm_loop/mevent_loop. So we do:
	 *   1. pause VM
	 *   2. flush and clear ioreqs
	 *   3. reset virtual devices
	 *   4. load software for UOS
	 *   5. hypercall reset vm
	 *   6. reset suspend mode to VM_SUSPEND_NONE
	 */

	vm_pause(ctx);

	/*
	 * After vm_pause, there should be no new coming ioreq.
	 *
	 * Unless under emergency mode, the vcpu writing to the ACPI PM
	 * CR should be the only vcpu of that VM that is still
	 * running. In this case there should be only one completed
	 * request which is the APIC PM CR write. VM reset will reset it
	 *
	 * When handling emergency mode triggered by one vcpu without
	 * offlining any other vcpus, there can be multiple VHM requests
	 * with various states. We should be careful on potential races
	 * when resetting especially in SMP SOS. vm_clear_ioreq can be used
	 * to clear all ioreq status in VHM after VM pause, then let VM
	 * reset in hypervisor reset all ioreqs.
	 */
	vm_clear_ioreq(ctx);

	vm_reset_vdevs(ctx);
	vm_reset(ctx);
	vm_set_suspend_mode(VM_SUSPEND_NONE);

	/* set the BSP init state */
	acrn_sw_load(ctx);
	vm_set_vcpu_regs(ctx, &ctx->bsp_regs);
	vm_run(ctx);
}
示例#20
0
static void
vm_loop(struct vmctx *ctx)
{
	int error;

	ctx->ioreq_client = vm_create_ioreq_client(ctx);
	assert(ctx->ioreq_client > 0);

	error = vm_run(ctx);
	assert(error == 0);

	while (1) {
		int vcpu_id;
		struct vhm_request *vhm_req;

		error = vm_attach_ioreq_client(ctx);
		if (error)
			break;

		for (vcpu_id = 0; vcpu_id < 4; vcpu_id++) {
			vhm_req = &vhm_req_buf[vcpu_id];
			if ((atomic_load(&vhm_req->processed) == REQ_STATE_PROCESSING)
				&& (vhm_req->client == ctx->ioreq_client))
				handle_vmexit(ctx, vhm_req, vcpu_id);
		}

		if (VM_SUSPEND_FULL_RESET == vm_get_suspend_mode() ||
		    VM_SUSPEND_POWEROFF == vm_get_suspend_mode()) {
			break;
		}

		if (VM_SUSPEND_SYSTEM_RESET == vm_get_suspend_mode()) {
			vm_system_reset(ctx);
		}

		if (VM_SUSPEND_SUSPEND == vm_get_suspend_mode()) {
			vm_suspend_resume(ctx);
		}
	}
	printf("VM loop exit\n");
}
static char * test_pow()
{
    int err = 0;
    int code_size;
    struct vm_state state;
    char * input[] = {"2\n", "4\n"};

    /* Write input values to stdin */
    uu_open_stdin_writer();
    uu_write_stdin(input[0]);
    uu_write_stdin(input[1]);
    uu_close_stdin_writer();

    b91_loader_read_file(mem, memsize, &code_size, "asm/pow.b91");
    pu_assert("Error while loading a b91 binary file.", err == 0);

    vm_init_state(&state, code_size, memsize);
    vm_run(&state, mem);

    pu_assert_equal("Result of 2^4 == 16", state.regs[1], 16);
    return 0;
}
示例#22
0
void file_main(char **argv)
{
	token_t *tokenized_buf;
	cons_t *tree_head;
	func_t *func;
	char input[1024];
	FILE *fp = NULL;

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

	if(fp == NULL) {
		printf("file not open.");
	}else{
		int reg[256];
		//func_t *funclist[256];
		//size_t funcnum = 0;
		while(fgets(input, 1024, fp) != NULL) {
			printf("%s\n", input);
			tokenized_buf = tokenize(input);
			treePointer = tokenized_buf;
			tree_head = parse(tokenized_buf);
			func = (func_t *)malloc(sizeof(func_t));
			func->index = 0;
			func->cons = tree_head;
			generatecoder(tree_head, func, 0);
			func->code[func->index].op = RET;
			func->code[func->index].reg0 = 0;
			fprintf(stderr, "%d\n", vm_run(func->code, reg));
		}
		//size_t i;
		//for (i = 0; i < funcnum; i++) {
		//	free(funclist[i]);
		//}
	}
	free(treePointer);

	fclose(fp);
}
示例#23
0
int vm_exec(const char * path, int argc, char * argv[])
{
	struct runtime_t * r;
	struct vm_t * vm;
	int ret = -1;

	if(!runtime_alloc_save(&r))
		return ret;

	vm = vm_alloc(path, argc, argv);
	if(!vm)
	{
		runtime_free_restore(r);
		return ret;
	}

	ret = vm_run(vm);

	vm_free(vm);
	runtime_free_restore(r);

	return ret;
}
示例#24
0
文件: vm.c 项目: terrayi/vm
int vm_quick_run(VM *vm, byte *code, size_t code_length)
{
	int ret = VM_RET_ERROR;

	vm_init(vm);

	ret = vm_load(vm, code, code_length);

	if (ret != VM_RET_OK)
	{
		return ret;
	}

	ret = vm_run(vm);

	if (ret != VM_RET_OK)
	{
		return ret;
	}

	vm_uninit(vm);

	return VM_RET_OK;
}
示例#25
0
文件: vm.c 项目: endeav0r/ravm
int main (int argc, char * argv[]) {
    char * filename = NULL;
    char * output_filename = NULL;
    int opt_god_mode = 0;
    int c;
    int memory_view_offset = VM_MEMORY_SIZE - 32;
    int memory_view_bytes  = 32;
    int print_info = 0;
    int step = 0;
    int error;
    struct _vm * vm;
    
    while ((c = getopt(argc, argv, "b:gi:m:o:sp")) != -1) {
        switch (c) {
            case 'b' :
                memory_view_bytes = strtoul(optarg, NULL, 16);
                break;
            case 'g' :
                opt_god_mode = 1;
                break;
            case 'i' :
                filename = optarg;
                break;
            case 'm' :
                memory_view_offset = strtoul(optarg, NULL, 16);
                break;
            case 'o' :
                output_filename = optarg;
                break;
            case 'p' :
                print_info = 1;
                break;
            case 's' :
                step = 1;
                print_info = 1;
                break;
            case '?' :
                if ((optopt == 'f') || (optopt == 'o')) {
                    fprintf(stderr, "option %c requires argument\n", optopt);
                    exit(0);
                }
                else {
                    fprintf(stderr, "Unknown option: %c\n", optopt);
                    exit(0);
                }
        }
    }
    
    if (filename == NULL) {
        fprintf(stderr, "Usage: %s [-ps] [-o output] -i image\n", argv[0]);
        fprintf(stderr, "Runs an assembled image for the rnp_vm\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "  -b [hex]   BYTES of memory to view in debug output\n");
        fprintf(stderr, "  -g         god mode allows visualization of memory\n");
        fprintf(stderr, "  -i [path]  path to IMAGE\n");
        fprintf(stderr, "  -m [hex]   OFFSET in memory to view in debug output\n");
        fprintf(stderr, "  -o [path]  path to OUTPUT memory dump at HLT\n");
        fprintf(stderr, "  -p         PRINT info at each step\n");
        fprintf(stderr, "  -s         STEP through instruction (implies PRINT)\n");
        exit(0);
    }
    
    vm = (struct _vm *) malloc(sizeof(struct _vm));
    vm_initialize(vm);
    
    if ((error = image_load(vm, filename)) != 0) {
        fprintf(stderr, "error %d\n", error);
        exit(error);
    }
    
    if (opt_god_mode) {
        god_mode(vm);
    }
    else {    
        if (step | print_info)
            vm->step = 1;

        if (print_info) {
            debug_view_memory(vm,
                              memory_view_offset,
                              memory_view_bytes);
            debug_view_registers(vm);
            fflush(stdout);
            printf("%s\n", debug_instruction_description(&(vm->memory[vm->IP])));
            printf("\n");
        }

        while (vm_run(vm)) {
            if (print_info) {
                debug_view_memory(vm,
                                  memory_view_offset,
                                  memory_view_bytes);
                debug_view_registers(vm);
                fflush(stdout);
                printf("%s\n", debug_instruction_description(&(vm->memory[vm->IP])));
                printf("\n");
            }
            if (step) getc(stdin);
        }
    }
    
    if (output_filename != NULL) {
        if ((error = image_dump(vm, output_filename)) != 0) {
            fprintf(stderr, "error dumping memory to file: %d\n", error);
            exit(error);
        }
    }
    
    free(vm);
    
    return 0;
}
示例#26
0
文件: cacao.c 项目: jiangxilong/yari
int main(int argc, char **argv)
{
#if defined(ENABLE_LIBJVM) && !defined(WITH_STATIC_CLASSPATH)
	char           *path;
#endif

#if defined(ENABLE_LIBJVM)	
	/* Variables for JNI_CreateJavaVM dlopen call. */
	lt_dlhandle     libjvm_handle;
	lt_ptr          libjvm_vm_createjvm;
	lt_ptr          libjvm_vm_run;
	const char     *lterror;

	bool (*vm_createjvm)(JavaVM **, void **, void *);
	void (*vm_run)(JavaVM *, JavaVMInitArgs *);
#endif

	JavaVM         *vm;                 /* denotes a Java VM                  */
	JNIEnv         *env;
	JavaVMInitArgs *vm_args;

	/* prepare the options */

	/* vm_args = cacao_options_prepare(argc, argv); */
/*	char *tmp[] = {"cacao", "hello"};
	vm_args = cacao_options_prepare(2, &tmp); 
	char *tmp[] = {"cacao", "jbe.DoMicro"};
	char *tmp[] = {"cacao", "jbe.BenchKfl"};
	char *tmp[] = {"cacao", "jbe.BenchLift"};
	char *tmp[] = {"cacao", "jbe.BenchUdpIp"};
        char *tmp[] = {"cacao", "jbe.BenchUnpredictableKfl"};
        char *tmp[] = {"cacao", "jbe.BenchPredictableKfl"};
*/
	/*char *tmp[] = {"cacao", "hello"};*/
	char *tmp[] = {"cacao", "jbe.DoKernel"};
	/*char *tmp[] = {"cacao", "jbe.BenchKfl"};*/
	/*char *tmp[] = {"cacao", "jbe.BenchLift"};*/
	/*char *tmp[] = {"cacao", "jbe.DoMicro"};*/
	/*char *tmp[] = {"cacao", "jbe.BenchUdpIp"};*/

	vm_args = cacao_options_prepare(2, &tmp);

	/* load and initialize a Java VM, return a JNI interface pointer in env */

#if defined(ENABLE_LIBJVM) && !defined(WITH_STATIC_CLASSPATH)
# if defined(WITH_JRE_LAYOUT)
	/* SUN also uses a buffer of 4096-bytes (strace is your friend). */

	path = malloc(sizeof(char) * 4096);

	if (readlink("/proc/self/exe", path, 4095) == -1) {
		fprintf(stderr, "main: readlink failed: %s\n", strerror(errno));
		abort();
	}

	/* get the path of the current executable */

	path = dirname(path);

	if ((strlen(path) + strlen("/../lib/libjvm") + strlen("0")) > 4096) {
		fprintf(stderr, "main: libjvm name to long for buffer\n");
		abort();
	}

	/* concatinate the library name */

	strcat(path, "/../lib/libjvm");
# else
	path = CACAO_LIBDIR"/libjvm";
# endif

	if (lt_dlinit()) {
		fprintf(stderr, "main: lt_dlinit failed: %s\n", lt_dlerror());
		abort();
	}

	/* First try to open where dlopen searches, e.g. LD_LIBRARY_PATH.
	   If not found, try the absolute path. */

	if (!(libjvm_handle = lt_dlopenext("libjvm"))) {
		/* save the error message */

		lterror = strdup(lt_dlerror());

		if (!(libjvm_handle = lt_dlopenext(path))) {
			/* print the first error message too */

			fprintf(stderr, "main: lt_dlopenext failed: %s\n", lterror);

			/* and now the current one */

			fprintf(stderr, "main: lt_dlopenext failed: %s\n", lt_dlerror());
			abort();
		}

		/* free the error string */

		free((void *) lterror);
	}

	if (!(libjvm_vm_createjvm = lt_dlsym(libjvm_handle, "vm_createjvm"))) {
		fprintf(stderr, "main: lt_dlsym failed: %s\n", lt_dlerror());
		abort();
	}

	vm_createjvm =
		(bool (*)(JavaVM **, void **, void *)) (ptrint) libjvm_vm_createjvm;
#endif

	/* create the Java VM */

	(void) vm_createjvm(&vm, (void *) &env, vm_args);

#if defined(ENABLE_JVMTI)
	pthread_mutex_init(&dbgcomlock,NULL);
	if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
#endif

#if !defined(WITH_STATIC_CLASSPATH) && defined(ENABLE_LIBJVM)
	if (!(libjvm_vm_run = lt_dlsym(libjvm_handle, "vm_run"))) {
		fprintf(stderr, "lt_dlsym failed: %s\n", lt_dlerror());
		abort();
	}

	vm_run = (void (*)(JavaVM *, JavaVMInitArgs *)) (ptrint) libjvm_vm_run;
#endif

	/* run the VM */

	vm_run(vm, vm_args);

	/* keep compiler happy */

	return 0;
}
示例#27
0
/**
 * [[Call]] implementation for Function objects,
 * created through 13.2 (ECMA_OBJECT_TYPE_FUNCTION)
 * or 15.3.4.5 (ECMA_OBJECT_TYPE_BOUND_FUNCTION),
 * and for built-in Function objects
 * from section 15 (ECMA_OBJECT_TYPE_FUNCTION).
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value
 */
ecma_value_t
ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
                       ecma_value_t this_arg_value, /**< 'this' argument's value */
                       const ecma_value_t *arguments_list_p, /**< arguments list */
                       ecma_length_t arguments_list_len) /**< length of arguments list */
{
  JERRY_ASSERT (func_obj_p != NULL
                && !ecma_is_lexical_environment (func_obj_p));
  JERRY_ASSERT (ecma_op_is_callable (ecma_make_object_value (func_obj_p)));

  ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);

  if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION)
  {
    if (unlikely (ecma_get_object_is_builtin (func_obj_p)))
    {
      ret_value = ecma_builtin_dispatch_call (func_obj_p,
                                              this_arg_value,
                                              arguments_list_p,
                                              arguments_list_len);
    }
    else
    {
      /* Entering Function Code (ECMA-262 v5, 10.4.3) */
      ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) func_obj_p;

      ecma_object_t *scope_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
                                                                ext_func_p->u.function.scope_cp);

      // 8.
      ecma_value_t this_binding;
      bool is_strict;
      bool is_no_lex_env;

      const ecma_compiled_code_t *bytecode_data_p;
      bytecode_data_p = ECMA_GET_INTERNAL_VALUE_POINTER (const ecma_compiled_code_t,
                                                         ext_func_p->u.function.bytecode_cp);

      is_strict = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE) ? true : false;
      is_no_lex_env = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED) ? true : false;

      // 1.
      if (is_strict)
      {
        this_binding = ecma_copy_value (this_arg_value);
      }
      else if (ecma_is_value_undefined (this_arg_value)
               || ecma_is_value_null (this_arg_value))
      {
        // 2.
        this_binding = ecma_make_object_value (ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL));
      }
      else
      {
        // 3., 4.
        this_binding = ecma_op_to_object (this_arg_value);

        JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (this_binding));
      }

      // 5.
      ecma_object_t *local_env_p;
      if (is_no_lex_env)
      {
        local_env_p = scope_p;
      }
      else
      {
        local_env_p = ecma_create_decl_lex_env (scope_p);
        if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_ARGUMENTS_NEEDED)
        {
          ecma_op_create_arguments_object (func_obj_p,
                                           local_env_p,
                                           arguments_list_p,
                                           arguments_list_len,
                                           bytecode_data_p);
        }
      }

      ret_value = vm_run (bytecode_data_p,
                          this_binding,
                          local_env_p,
                          false,
                          arguments_list_p,
                          arguments_list_len);

      if (!is_no_lex_env)
      {
        ecma_deref_object (local_env_p);
      }

      ecma_free_value (this_binding);
    }
  }
  else if (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION)
示例#28
0
int
main(int argc, char *argv[])
{
	char *vmname;
	int error, ch, vcpu, ptenum;
	vm_paddr_t gpa, gpa_pmap;
	size_t len;
	struct vm_exit vmexit;
	uint64_t ctl, eptp, bm, addr, u64, pteval[4], *pte;
	struct vmctx *ctx;
	int wired;
	cpuset_t cpus;

	uint64_t cr0, cr3, cr4, dr7, rsp, rip, rflags, efer, pat;
	uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp;
	uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;

	struct option opts[] = {
		{ "vm",		REQ_ARG,	0,	VMNAME },
		{ "cpu",	REQ_ARG,	0,	VCPU },
		{ "set-mem",	REQ_ARG,	0,	SET_MEM },
		{ "set-efer",	REQ_ARG,	0,	SET_EFER },
		{ "set-cr0",	REQ_ARG,	0,	SET_CR0 },
		{ "set-cr3",	REQ_ARG,	0,	SET_CR3 },
		{ "set-cr4",	REQ_ARG,	0,	SET_CR4 },
		{ "set-dr7",	REQ_ARG,	0,	SET_DR7 },
		{ "set-rsp",	REQ_ARG,	0,	SET_RSP },
		{ "set-rip",	REQ_ARG,	0,	SET_RIP },
		{ "set-rax",	REQ_ARG,	0,	SET_RAX },
		{ "set-rflags",	REQ_ARG,	0,	SET_RFLAGS },
		{ "desc-base",	REQ_ARG,	0,	DESC_BASE },
		{ "desc-limit",	REQ_ARG,	0,	DESC_LIMIT },
		{ "desc-access",REQ_ARG,	0,	DESC_ACCESS },
		{ "set-cs",	REQ_ARG,	0,	SET_CS },
		{ "set-ds",	REQ_ARG,	0,	SET_DS },
		{ "set-es",	REQ_ARG,	0,	SET_ES },
		{ "set-fs",	REQ_ARG,	0,	SET_FS },
		{ "set-gs",	REQ_ARG,	0,	SET_GS },
		{ "set-ss",	REQ_ARG,	0,	SET_SS },
		{ "set-tr",	REQ_ARG,	0,	SET_TR },
		{ "set-ldtr",	REQ_ARG,	0,	SET_LDTR },
		{ "set-x2apic-state",REQ_ARG,	0,	SET_X2APIC_STATE },
		{ "set-vmcs-exception-bitmap",
				REQ_ARG,	0, SET_VMCS_EXCEPTION_BITMAP },
		{ "set-vmcs-entry-interruption-info",
				REQ_ARG, 0, SET_VMCS_ENTRY_INTERRUPTION_INFO },
		{ "capname",	REQ_ARG,	0,	CAPNAME },
		{ "unassign-pptdev", REQ_ARG,	0,	UNASSIGN_PPTDEV },
		{ "setcap",	REQ_ARG,	0,	SET_CAP },
		{ "get-gpa-pmap", REQ_ARG,	0,	GET_GPA_PMAP },
		{ "assert-lapic-lvt", REQ_ARG,	0,	ASSERT_LAPIC_LVT },
		{ "getcap",	NO_ARG,		&getcap,	1 },
		{ "get-stats",	NO_ARG,		&get_stats,	1 },
		{ "get-desc-ds",NO_ARG,		&get_desc_ds,	1 },
		{ "set-desc-ds",NO_ARG,		&set_desc_ds,	1 },
		{ "get-desc-es",NO_ARG,		&get_desc_es,	1 },
		{ "set-desc-es",NO_ARG,		&set_desc_es,	1 },
		{ "get-desc-ss",NO_ARG,		&get_desc_ss,	1 },
		{ "set-desc-ss",NO_ARG,		&set_desc_ss,	1 },
		{ "get-desc-cs",NO_ARG,		&get_desc_cs,	1 },
		{ "set-desc-cs",NO_ARG,		&set_desc_cs,	1 },
		{ "get-desc-fs",NO_ARG,		&get_desc_fs,	1 },
		{ "set-desc-fs",NO_ARG,		&set_desc_fs,	1 },
		{ "get-desc-gs",NO_ARG,		&get_desc_gs,	1 },
		{ "set-desc-gs",NO_ARG,		&set_desc_gs,	1 },
		{ "get-desc-tr",NO_ARG,		&get_desc_tr,	1 },
		{ "set-desc-tr",NO_ARG,		&set_desc_tr,	1 },
		{ "set-desc-ldtr", NO_ARG,	&set_desc_ldtr,	1 },
		{ "get-desc-ldtr", NO_ARG,	&get_desc_ldtr,	1 },
		{ "set-desc-gdtr", NO_ARG,	&set_desc_gdtr, 1 },
		{ "get-desc-gdtr", NO_ARG,	&get_desc_gdtr, 1 },
		{ "set-desc-idtr", NO_ARG,	&set_desc_idtr, 1 },
		{ "get-desc-idtr", NO_ARG,	&get_desc_idtr, 1 },
		{ "get-lowmem", NO_ARG,		&get_lowmem,	1 },
		{ "get-highmem",NO_ARG,		&get_highmem,	1 },
		{ "get-efer",	NO_ARG,		&get_efer,	1 },
		{ "get-cr0",	NO_ARG,		&get_cr0,	1 },
		{ "get-cr3",	NO_ARG,		&get_cr3,	1 },
		{ "get-cr4",	NO_ARG,		&get_cr4,	1 },
		{ "get-dr7",	NO_ARG,		&get_dr7,	1 },
		{ "get-rsp",	NO_ARG,		&get_rsp,	1 },
		{ "get-rip",	NO_ARG,		&get_rip,	1 },
		{ "get-rax",	NO_ARG,		&get_rax,	1 },
		{ "get-rbx",	NO_ARG,		&get_rbx,	1 },
		{ "get-rcx",	NO_ARG,		&get_rcx,	1 },
		{ "get-rdx",	NO_ARG,		&get_rdx,	1 },
		{ "get-rsi",	NO_ARG,		&get_rsi,	1 },
		{ "get-rdi",	NO_ARG,		&get_rdi,	1 },
		{ "get-rbp",	NO_ARG,		&get_rbp,	1 },
		{ "get-r8",	NO_ARG,		&get_r8,	1 },
		{ "get-r9",	NO_ARG,		&get_r9,	1 },
		{ "get-r10",	NO_ARG,		&get_r10,	1 },
		{ "get-r11",	NO_ARG,		&get_r11,	1 },
		{ "get-r12",	NO_ARG,		&get_r12,	1 },
		{ "get-r13",	NO_ARG,		&get_r13,	1 },
		{ "get-r14",	NO_ARG,		&get_r14,	1 },
		{ "get-r15",	NO_ARG,		&get_r15,	1 },
		{ "get-rflags",	NO_ARG,		&get_rflags,	1 },
		{ "get-cs",	NO_ARG,		&get_cs,	1 },
		{ "get-ds",	NO_ARG,		&get_ds,	1 },
		{ "get-es",	NO_ARG,		&get_es,	1 },
		{ "get-fs",	NO_ARG,		&get_fs,	1 },
		{ "get-gs",	NO_ARG,		&get_gs,	1 },
		{ "get-ss",	NO_ARG,		&get_ss,	1 },
		{ "get-tr",	NO_ARG,		&get_tr,	1 },
		{ "get-ldtr",	NO_ARG,		&get_ldtr,	1 },
		{ "get-vmcs-pinbased-ctls",
				NO_ARG,		&get_pinbased_ctls, 1 },
		{ "get-vmcs-procbased-ctls",
				NO_ARG,		&get_procbased_ctls, 1 },
		{ "get-vmcs-procbased-ctls2",
				NO_ARG,		&get_procbased_ctls2, 1 },
		{ "get-vmcs-guest-linear-address",
				NO_ARG,		&get_vmcs_gla,	1 },
		{ "get-vmcs-guest-physical-address",
				NO_ARG,		&get_vmcs_gpa,	1 },
		{ "get-vmcs-entry-interruption-info",
				NO_ARG, &get_vmcs_entry_interruption_info, 1},
		{ "get-vmcs-eptp", NO_ARG,	&get_eptp,	1 },
		{ "get-vmcs-exception-bitmap",
				NO_ARG,		&get_exception_bitmap, 1 },
		{ "get-vmcs-io-bitmap-address",
				NO_ARG,		&get_io_bitmap,	1 },
		{ "get-vmcs-tsc-offset", NO_ARG,&get_tsc_offset, 1 },
		{ "get-vmcs-cr0-mask", NO_ARG,	&get_cr0_mask,	1 },
		{ "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 },
		{ "get-vmcs-cr4-mask", NO_ARG,	&get_cr4_mask,	1 },
		{ "get-vmcs-cr4-shadow", NO_ARG,&get_cr4_shadow, 1 },
		{ "get-vmcs-cr3-targets", NO_ARG, &get_cr3_targets, 1},
		{ "get-vmcs-apic-access-address",
				NO_ARG,		&get_apic_access_addr, 1},
		{ "get-vmcs-virtual-apic-address",
				NO_ARG,		&get_virtual_apic_addr, 1},
		{ "get-vmcs-tpr-threshold",
				NO_ARG,		&get_tpr_threshold, 1 },
		{ "get-vmcs-msr-bitmap",
				NO_ARG,		&get_msr_bitmap, 1 },
		{ "get-vmcs-msr-bitmap-address",
				NO_ARG,		&get_msr_bitmap_address, 1 },
		{ "get-vmcs-vpid", NO_ARG,	&get_vpid,	1 },
		{ "get-vmcs-ple-gap", NO_ARG,	&get_ple_gap,	1 },
		{ "get-vmcs-ple-window", NO_ARG,&get_ple_window,1 },
		{ "get-vmcs-instruction-error",
				NO_ARG,		&get_inst_err,	1 },
		{ "get-vmcs-exit-ctls", NO_ARG,	&get_exit_ctls,	1 },
		{ "get-vmcs-entry-ctls",
					NO_ARG,	&get_entry_ctls, 1 },
		{ "get-vmcs-guest-pat",	NO_ARG,	&get_guest_pat,	1 },
		{ "get-vmcs-host-pat",	NO_ARG,	&get_host_pat,	1 },
		{ "get-vmcs-host-cr0",
				NO_ARG,		&get_host_cr0,	1 },
		{ "get-vmcs-host-cr3",
				NO_ARG,		&get_host_cr3,	1 },
		{ "get-vmcs-host-cr4",
				NO_ARG,		&get_host_cr4,	1 },
		{ "get-vmcs-host-rip",
				NO_ARG,		&get_host_rip,	1 },
		{ "get-vmcs-host-rsp",
				NO_ARG,		&get_host_rsp,	1 },
		{ "get-vmcs-guest-sysenter",
				NO_ARG,		&get_guest_sysenter, 1 },
		{ "get-vmcs-link", NO_ARG,	&get_vmcs_link, 1 },
		{ "get-vmcs-exit-reason",
				NO_ARG,		&get_vmcs_exit_reason, 1 },
		{ "get-vmcs-exit-qualification",
			NO_ARG,		&get_vmcs_exit_qualification, 1 },
		{ "get-vmcs-exit-interruption-info",
				NO_ARG,	&get_vmcs_exit_interruption_info, 1},
		{ "get-vmcs-exit-interruption-error",
				NO_ARG,	&get_vmcs_exit_interruption_error, 1},
		{ "get-vmcs-interruptibility",
				NO_ARG, &get_vmcs_interruptibility, 1 },
		{ "get-x2apic-state",NO_ARG,	&get_x2apic_state, 1 },
		{ "get-all",	NO_ARG,		&get_all,	1 },
		{ "run",	NO_ARG,		&run,		1 },
		{ "create",	NO_ARG,		&create,	1 },
		{ "destroy",	NO_ARG,		&destroy,	1 },
		{ "inject-nmi",	NO_ARG,		&inject_nmi,	1 },
		{ "force-reset",	NO_ARG,	&force_reset,	1 },
		{ "force-poweroff", NO_ARG,	&force_poweroff, 1 },
		{ "get-active-cpus", NO_ARG,	&get_active_cpus, 1 },
		{ "get-suspended-cpus", NO_ARG,	&get_suspended_cpus, 1 },
		{ NULL,		0,		NULL,		0 }
	};

	vcpu = 0;
	vmname = NULL;
	assert_lapic_lvt = -1;
	progname = basename(argv[0]);

	while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) {
		switch (ch) {
		case 0:
			break;
		case VMNAME:
			vmname = optarg;
			break;
		case VCPU:
			vcpu = atoi(optarg);
			break;
		case SET_MEM:
			memsize = atoi(optarg) * MB;
			memsize = roundup(memsize, 2 * MB);
			break;
		case SET_EFER:
			efer = strtoul(optarg, NULL, 0);
			set_efer = 1;
			break;
		case SET_CR0:
			cr0 = strtoul(optarg, NULL, 0);
			set_cr0 = 1;
			break;
		case SET_CR3:
			cr3 = strtoul(optarg, NULL, 0);
			set_cr3 = 1;
			break;
		case SET_CR4:
			cr4 = strtoul(optarg, NULL, 0);
			set_cr4 = 1;
			break;
		case SET_DR7:
			dr7 = strtoul(optarg, NULL, 0);
			set_dr7 = 1;
			break;
		case SET_RSP:
			rsp = strtoul(optarg, NULL, 0);
			set_rsp = 1;
			break;
		case SET_RIP:
			rip = strtoul(optarg, NULL, 0);
			set_rip = 1;
			break;
		case SET_RAX:
			rax = strtoul(optarg, NULL, 0);
			set_rax = 1;
			break;
		case SET_RFLAGS:
			rflags = strtoul(optarg, NULL, 0);
			set_rflags = 1;
			break;
		case DESC_BASE:
			desc_base = strtoul(optarg, NULL, 0);
			break;
		case DESC_LIMIT:
			desc_limit = strtoul(optarg, NULL, 0);
			break;
		case DESC_ACCESS:
			desc_access = strtoul(optarg, NULL, 0);
			break;
		case SET_CS:
			cs = strtoul(optarg, NULL, 0);
			set_cs = 1;
			break;
		case SET_DS:
			ds = strtoul(optarg, NULL, 0);
			set_ds = 1;
			break;
		case SET_ES:
			es = strtoul(optarg, NULL, 0);
			set_es = 1;
			break;
		case SET_FS:
			fs = strtoul(optarg, NULL, 0);
			set_fs = 1;
			break;
		case SET_GS:
			gs = strtoul(optarg, NULL, 0);
			set_gs = 1;
			break;
		case SET_SS:
			ss = strtoul(optarg, NULL, 0);
			set_ss = 1;
			break;
		case SET_TR:
			tr = strtoul(optarg, NULL, 0);
			set_tr = 1;
			break;
		case SET_LDTR:
			ldtr = strtoul(optarg, NULL, 0);
			set_ldtr = 1;
			break;
		case SET_X2APIC_STATE:
			x2apic_state = strtol(optarg, NULL, 0);
			set_x2apic_state = 1;
			break;
		case SET_VMCS_EXCEPTION_BITMAP:
			exception_bitmap = strtoul(optarg, NULL, 0);
			set_exception_bitmap = 1;
			break;
		case SET_VMCS_ENTRY_INTERRUPTION_INFO:
			vmcs_entry_interruption_info = strtoul(optarg, NULL, 0);
			set_vmcs_entry_interruption_info = 1;
			break;
		case SET_CAP:
			capval = strtoul(optarg, NULL, 0);
			setcap = 1;
			break;
		case GET_GPA_PMAP:
			gpa_pmap = strtoul(optarg, NULL, 0);
			get_gpa_pmap = 1;
			break;
		case CAPNAME:
			capname = optarg;
			break;
		case UNASSIGN_PPTDEV:
			unassign_pptdev = 1;
			if (sscanf(optarg, "%d/%d/%d", &bus, &slot, &func) != 3)
				usage();
			break;
		case ASSERT_LAPIC_LVT:
			assert_lapic_lvt = atoi(optarg);
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (vmname == NULL)
		usage();

	error = 0;

	if (!error && create)
		error = vm_create(vmname);

	if (!error) {
		ctx = vm_open(vmname);
		if (ctx == NULL)
			error = -1;
	}

	if (!error && memsize)
		error = vm_setup_memory(ctx, memsize, VM_MMAP_NONE);

	if (!error && set_efer)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer);

	if (!error && set_cr0)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR0, cr0);

	if (!error && set_cr3)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR3, cr3);

	if (!error && set_cr4)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR4, cr4);

	if (!error && set_dr7)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR7, dr7);

	if (!error && set_rsp)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RSP, rsp);

	if (!error && set_rip)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, rip);

	if (!error && set_rax)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, rax);

	if (!error && set_rflags) {
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
					rflags);
	}

	if (!error && set_desc_ds) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_DS,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_es) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_ES,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_ss) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_SS,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_cs) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_CS,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_fs) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_FS,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_gs) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GS,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_tr) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_TR,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_ldtr) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
				    desc_base, desc_limit, desc_access);
	}

	if (!error && set_desc_gdtr) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
				    desc_base, desc_limit, 0);
	}

	if (!error && set_desc_idtr) {
		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
				    desc_base, desc_limit, 0);
	}

	if (!error && set_cs)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CS, cs);

	if (!error && set_ds)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DS, ds);

	if (!error && set_es)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_ES, es);

	if (!error && set_fs)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_FS, fs);

	if (!error && set_gs)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_GS, gs);

	if (!error && set_ss)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_SS, ss);

	if (!error && set_tr)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_TR, tr);

	if (!error && set_ldtr)
		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_LDTR, ldtr);

	if (!error && set_x2apic_state)
		error = vm_set_x2apic_state(ctx, vcpu, x2apic_state);

	if (!error && unassign_pptdev)
		error = vm_unassign_pptdev(ctx, bus, slot, func);

	if (!error && set_exception_bitmap) {
		error = vm_set_vmcs_field(ctx, vcpu, VMCS_EXCEPTION_BITMAP,
					  exception_bitmap);
	}

	if (!error && set_vmcs_entry_interruption_info) {
		error = vm_set_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,
					  vmcs_entry_interruption_info);
	}

	if (!error && inject_nmi) {
		error = vm_inject_nmi(ctx, vcpu);
	}

	if (!error && assert_lapic_lvt != -1) {
		error = vm_lapic_local_irq(ctx, vcpu, assert_lapic_lvt);
	}

	if (!error && (get_lowmem || get_all)) {
		gpa = 0;
		error = vm_get_memory_seg(ctx, gpa, &len, &wired);
		if (error == 0)
			printf("lowmem\t\t0x%016lx/%ld%s\n", gpa, len,
			    wired ? " wired" : "");
	}

	if (!error && (get_highmem || get_all)) {
		gpa = 4 * GB;
		error = vm_get_memory_seg(ctx, gpa, &len, &wired);
		if (error == 0)
			printf("highmem\t\t0x%016lx/%ld%s\n", gpa, len,
			    wired ? " wired" : "");
	}

	if (!error && (get_efer || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_EFER, &efer);
		if (error == 0)
			printf("efer[%d]\t\t0x%016lx\n", vcpu, efer);
	}

	if (!error && (get_cr0 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR0, &cr0);
		if (error == 0)
			printf("cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
	}

	if (!error && (get_cr3 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR3, &cr3);
		if (error == 0)
			printf("cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
	}

	if (!error && (get_cr4 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR4, &cr4);
		if (error == 0)
			printf("cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
	}

	if (!error && (get_dr7 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR7, &dr7);
		if (error == 0)
			printf("dr7[%d]\t\t0x%016lx\n", vcpu, dr7);
	}

	if (!error && (get_rsp || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSP, &rsp);
		if (error == 0)
			printf("rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
	}

	if (!error && (get_rip || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
		if (error == 0)
			printf("rip[%d]\t\t0x%016lx\n", vcpu, rip);
	}

	if (!error && (get_rax || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RAX, &rax);
		if (error == 0)
			printf("rax[%d]\t\t0x%016lx\n", vcpu, rax);
	}

	if (!error && (get_rbx || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBX, &rbx);
		if (error == 0)
			printf("rbx[%d]\t\t0x%016lx\n", vcpu, rbx);
	}

	if (!error && (get_rcx || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RCX, &rcx);
		if (error == 0)
			printf("rcx[%d]\t\t0x%016lx\n", vcpu, rcx);
	}

	if (!error && (get_rdx || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDX, &rdx);
		if (error == 0)
			printf("rdx[%d]\t\t0x%016lx\n", vcpu, rdx);
	}

	if (!error && (get_rsi || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSI, &rsi);
		if (error == 0)
			printf("rsi[%d]\t\t0x%016lx\n", vcpu, rsi);
	}

	if (!error && (get_rdi || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDI, &rdi);
		if (error == 0)
			printf("rdi[%d]\t\t0x%016lx\n", vcpu, rdi);
	}

	if (!error && (get_rbp || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBP, &rbp);
		if (error == 0)
			printf("rbp[%d]\t\t0x%016lx\n", vcpu, rbp);
	}

	if (!error && (get_r8 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R8, &r8);
		if (error == 0)
			printf("r8[%d]\t\t0x%016lx\n", vcpu, r8);
	}

	if (!error && (get_r9 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R9, &r9);
		if (error == 0)
			printf("r9[%d]\t\t0x%016lx\n", vcpu, r9);
	}

	if (!error && (get_r10 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R10, &r10);
		if (error == 0)
			printf("r10[%d]\t\t0x%016lx\n", vcpu, r10);
	}

	if (!error && (get_r11 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R11, &r11);
		if (error == 0)
			printf("r11[%d]\t\t0x%016lx\n", vcpu, r11);
	}

	if (!error && (get_r12 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R12, &r12);
		if (error == 0)
			printf("r12[%d]\t\t0x%016lx\n", vcpu, r12);
	}

	if (!error && (get_r13 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R13, &r13);
		if (error == 0)
			printf("r13[%d]\t\t0x%016lx\n", vcpu, r13);
	}

	if (!error && (get_r14 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R14, &r14);
		if (error == 0)
			printf("r14[%d]\t\t0x%016lx\n", vcpu, r14);
	}

	if (!error && (get_r15 || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R15, &r15);
		if (error == 0)
			printf("r15[%d]\t\t0x%016lx\n", vcpu, r15);
	}

	if (!error && (get_rflags || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
					&rflags);
		if (error == 0)
			printf("rflags[%d]\t0x%016lx\n", vcpu, rflags);
	}

	if (!error && (get_stats || get_all)) {
		int i, num_stats;
		uint64_t *stats;
		struct timeval tv;
		const char *desc;

		stats = vm_get_stats(ctx, vcpu, &tv, &num_stats);
		if (stats != NULL) {
			printf("vcpu%d\n", vcpu);
			for (i = 0; i < num_stats; i++) {
				desc = vm_get_stat_desc(ctx, i);
				printf("%-40s\t%ld\n", desc, stats[i]);
			}
		}
	}

	if (!error && (get_desc_ds || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_DS,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("ds desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_es || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_ES,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("es desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_fs || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_FS,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("fs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_gs || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GS,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("gs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_ss || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("ss desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_cs || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_CS,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("cs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_tr || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_ldtr || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
			       vcpu, desc_base, desc_limit, desc_access);	
		}
	}

	if (!error && (get_desc_gdtr || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("gdtr[%d]\t\t0x%016lx/0x%08x\n",
			       vcpu, desc_base, desc_limit);	
		}
	}

	if (!error && (get_desc_idtr || get_all)) {
		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
				    &desc_base, &desc_limit, &desc_access);
		if (error == 0) {
			printf("idtr[%d]\t\t0x%016lx/0x%08x\n",
			       vcpu, desc_base, desc_limit);	
		}
	}

	if (!error && (get_cs || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CS, &cs);
		if (error == 0)
			printf("cs[%d]\t\t0x%04lx\n", vcpu, cs);
	}

	if (!error && (get_ds || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DS, &ds);
		if (error == 0)
			printf("ds[%d]\t\t0x%04lx\n", vcpu, ds);
	}

	if (!error && (get_es || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_ES, &es);
		if (error == 0)
			printf("es[%d]\t\t0x%04lx\n", vcpu, es);
	}

	if (!error && (get_fs || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_FS, &fs);
		if (error == 0)
			printf("fs[%d]\t\t0x%04lx\n", vcpu, fs);
	}

	if (!error && (get_gs || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_GS, &gs);
		if (error == 0)
			printf("gs[%d]\t\t0x%04lx\n", vcpu, gs);
	}

	if (!error && (get_ss || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_SS, &ss);
		if (error == 0)
			printf("ss[%d]\t\t0x%04lx\n", vcpu, ss);
	}

	if (!error && (get_tr || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_TR, &tr);
		if (error == 0)
			printf("tr[%d]\t\t0x%04lx\n", vcpu, tr);
	}

	if (!error && (get_ldtr || get_all)) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_LDTR, &ldtr);
		if (error == 0)
			printf("ldtr[%d]\t\t0x%04lx\n", vcpu, ldtr);
	}

	if (!error && (get_x2apic_state || get_all)) {
		error = vm_get_x2apic_state(ctx, vcpu, &x2apic_state);
		if (error == 0)
			printf("x2apic_state[%d]\t%d\n", vcpu, x2apic_state);
	}

	if (!error && (get_pinbased_ctls || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_PIN_BASED_CTLS, &ctl);
		if (error == 0)
			printf("pinbased_ctls[%d]\t0x%08lx\n", vcpu, ctl);
	}

	if (!error && (get_procbased_ctls || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_PRI_PROC_BASED_CTLS, &ctl);
		if (error == 0)
			printf("procbased_ctls[%d]\t0x%08lx\n", vcpu, ctl);
	}

	if (!error && (get_procbased_ctls2 || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_SEC_PROC_BASED_CTLS, &ctl);
		if (error == 0)
			printf("procbased_ctls2[%d]\t0x%08lx\n", vcpu, ctl);
	}

	if (!error && (get_vmcs_gla || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_GUEST_LINEAR_ADDRESS, &u64);
		if (error == 0)
			printf("gla[%d]\t\t0x%016lx\n", vcpu, u64);
	}

	if (!error && (get_vmcs_gpa || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_GUEST_PHYSICAL_ADDRESS, &u64);
		if (error == 0)
			printf("gpa[%d]\t\t0x%016lx\n", vcpu, u64);
	}

	if (!error && (get_vmcs_entry_interruption_info || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,&u64);
		if (error == 0) {
			printf("entry_interruption_info[%d]\t0x%08lx\n",
				vcpu, u64);
		}
	}

	if (!error && (get_eptp || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EPTP, &eptp);
		if (error == 0)
			printf("eptp[%d]\t\t0x%016lx\n", vcpu, eptp);
	}

	if (!error && (get_exception_bitmap || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXCEPTION_BITMAP,
					  &bm);
		if (error == 0)
			printf("exception_bitmap[%d]\t0x%08lx\n", vcpu, bm);
	}

	if (!error && (get_io_bitmap || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_A, &bm);
		if (error == 0)
			printf("io_bitmap_a[%d]\t0x%08lx\n", vcpu, bm);
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_B, &bm);
		if (error == 0)
			printf("io_bitmap_b[%d]\t0x%08lx\n", vcpu, bm);
	}

	if (!error && (get_tsc_offset || get_all)) {
		uint64_t tscoff;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_TSC_OFFSET, &tscoff);
		if (error == 0)
			printf("tsc_offset[%d]\t0x%016lx\n", vcpu, tscoff);
	}

	if (!error && (get_cr0_mask || get_all)) {
		uint64_t cr0mask;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_MASK, &cr0mask);
		if (error == 0)
			printf("cr0_mask[%d]\t\t0x%016lx\n", vcpu, cr0mask);
	}

	if (!error && (get_cr0_shadow || get_all)) {
		uint64_t cr0shadow;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_SHADOW,
					  &cr0shadow);
		if (error == 0)
			printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpu, cr0shadow);
	}

	if (!error && (get_cr4_mask || get_all)) {
		uint64_t cr4mask;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_MASK, &cr4mask);
		if (error == 0)
			printf("cr4_mask[%d]\t\t0x%016lx\n", vcpu, cr4mask);
	}

	if (!error && (get_cr4_shadow || get_all)) {
		uint64_t cr4shadow;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_SHADOW,
					  &cr4shadow);
		if (error == 0)
			printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpu, cr4shadow);
	}
	
	if (!error && (get_cr3_targets || get_all)) {
		uint64_t target_count, target_addr;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET_COUNT,
					  &target_count);
		if (error == 0) {
			printf("cr3_target_count[%d]\t0x%08lx\n",
				vcpu, target_count);
		}

		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET0,
					  &target_addr);
		if (error == 0) {
			printf("cr3_target0[%d]\t\t0x%016lx\n",
				vcpu, target_addr);
		}

		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET1,
					  &target_addr);
		if (error == 0) {
			printf("cr3_target1[%d]\t\t0x%016lx\n",
				vcpu, target_addr);
		}

		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET2,
					  &target_addr);
		if (error == 0) {
			printf("cr3_target2[%d]\t\t0x%016lx\n",
				vcpu, target_addr);
		}

		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET3,
					  &target_addr);
		if (error == 0) {
			printf("cr3_target3[%d]\t\t0x%016lx\n",
				vcpu, target_addr);
		}
	}

	if (!error && (get_apic_access_addr || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_APIC_ACCESS, &addr);
		if (error == 0)
			printf("apic_access_addr[%d]\t0x%016lx\n", vcpu, addr);
	}

	if (!error && (get_virtual_apic_addr || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_VIRTUAL_APIC, &addr);
		if (error == 0)
			printf("virtual_apic_addr[%d]\t0x%016lx\n", vcpu, addr);
	}

	if (!error && (get_tpr_threshold || get_all)) {
		uint64_t threshold;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_TPR_THRESHOLD,
					  &threshold);
		if (error == 0)
			printf("tpr_threshold[%d]\t0x%08lx\n", vcpu, threshold);
	}

	if (!error && (get_msr_bitmap_address || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP, &addr);
		if (error == 0)
			printf("msr_bitmap[%d]\t\t0x%016lx\n", vcpu, addr);
	}

	if (!error && (get_msr_bitmap || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP, &addr);
		if (error == 0)
			error = dump_vmcs_msr_bitmap(vcpu, addr);
	}

	if (!error && (get_vpid || get_all)) {
		uint64_t vpid;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_VPID, &vpid);
		if (error == 0)
			printf("vpid[%d]\t\t0x%04lx\n", vcpu, vpid);
	}
	
	if (!error && (get_ple_window || get_all)) {
		uint64_t window;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_PLE_WINDOW, &window);
		if (error == 0)
			printf("ple_window[%d]\t\t0x%08lx\n", vcpu, window);
	}

	if (!error && (get_ple_gap || get_all)) {
		uint64_t gap;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_PLE_GAP, &gap);
		if (error == 0)
			printf("ple_gap[%d]\t\t0x%08lx\n", vcpu, gap);
	}

	if (!error && (get_inst_err || get_all)) {
		uint64_t insterr;
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_INSTRUCTION_ERROR,
					  &insterr);
		if (error == 0) {
			printf("instruction_error[%d]\t0x%08lx\n",
				vcpu, insterr);
		}
	}

	if (!error && (get_exit_ctls || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_CTLS, &ctl);
		if (error == 0)
			printf("exit_ctls[%d]\t\t0x%08lx\n", vcpu, ctl);
	}

	if (!error && (get_entry_ctls || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_CTLS, &ctl);
		if (error == 0)
			printf("entry_ctls[%d]\t\t0x%08lx\n", vcpu, ctl);
	}

	if (!error && (get_host_pat || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_IA32_PAT, &pat);
		if (error == 0)
			printf("host_pat[%d]\t\t0x%016lx\n", vcpu, pat);
	}

	if (!error && (get_guest_pat || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_GUEST_IA32_PAT, &pat);
		if (error == 0)
			printf("guest_pat[%d]\t\t0x%016lx\n", vcpu, pat);
	}

	if (!error && (get_host_cr0 || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR0, &cr0);
		if (error == 0)
			printf("host_cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
	}

	if (!error && (get_host_cr3 || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR3, &cr3);
		if (error == 0)
			printf("host_cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
	}

	if (!error && (get_host_cr4 || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR4, &cr4);
		if (error == 0)
			printf("host_cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
	}

	if (!error && (get_host_rip || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RIP, &rip);
		if (error == 0)
			printf("host_rip[%d]\t\t0x%016lx\n", vcpu, rip);
	}

	if (!error && (get_host_rsp || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RSP, &rsp);
		if (error == 0)
			printf("host_rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
	}

	if (!error && (get_guest_sysenter || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_GUEST_IA32_SYSENTER_CS, &cs);
		if (error == 0)
			printf("guest_sysenter_cs[%d]\t0x%08lx\n", vcpu, cs);

		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_GUEST_IA32_SYSENTER_ESP, &rsp);
		if (error == 0)
			printf("guest_sysenter_sp[%d]\t0x%016lx\n", vcpu, rsp);
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_GUEST_IA32_SYSENTER_EIP, &rip);
		if (error == 0)
			printf("guest_sysenter_ip[%d]\t0x%016lx\n", vcpu, rip);
	}

	if (!error && (get_vmcs_link || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_LINK_POINTER, &addr);
		if (error == 0)
			printf("vmcs_pointer[%d]\t0x%016lx\n", vcpu, addr);
	}

	if (!error && (get_vmcs_exit_reason || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_REASON, &u64);
		if (error == 0)
			printf("vmcs_exit_reason[%d]\t0x%016lx\n", vcpu, u64);
	}

	if (!error && (get_vmcs_exit_qualification || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_QUALIFICATION,
					  &u64);
		if (error == 0)
			printf("vmcs_exit_qualification[%d]\t0x%016lx\n",
				vcpu, u64);
	}

	if (!error && (get_vmcs_exit_interruption_info || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_INFO, &u64);
		if (error == 0) {
			printf("vmcs_exit_interruption_info[%d]\t0x%08lx\n",
				vcpu, u64);
		}
	}

	if (!error && (get_vmcs_exit_interruption_error || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_ERRCODE,
		    &u64);
		if (error == 0) {
			printf("vmcs_exit_interruption_error[%d]\t0x%08lx\n",
				vcpu, u64);
		}
	}

	if (!error && (get_vmcs_interruptibility || get_all)) {
		error = vm_get_vmcs_field(ctx, vcpu,
					  VMCS_GUEST_INTERRUPTIBILITY, &u64);
		if (error == 0) {
			printf("vmcs_guest_interruptibility[%d]\t0x%08lx\n",
				vcpu, u64);
		}
	}

	if (!error && setcap) {
		int captype;
		captype = vm_capability_name2type(capname);
		error = vm_set_capability(ctx, vcpu, captype, capval);
		if (error != 0 && errno == ENOENT)
			printf("Capability \"%s\" is not available\n", capname);
	}

	if (!error && get_gpa_pmap) {
		error = vm_get_gpa_pmap(ctx, gpa_pmap, pteval, &ptenum);
		if (error == 0) {
			printf("gpa %#lx:", gpa_pmap);
			pte = &pteval[0];
			while (ptenum-- > 0)
				printf(" %#lx", *pte++);
			printf("\n");
		}
	}

	if (!error && (getcap || get_all)) {
		int captype, val, getcaptype;

		if (getcap && capname)
			getcaptype = vm_capability_name2type(capname);
		else
			getcaptype = -1;

		for (captype = 0; captype < VM_CAP_MAX; captype++) {
			if (getcaptype >= 0 && captype != getcaptype)
				continue;
			error = vm_get_capability(ctx, vcpu, captype, &val);
			if (error == 0) {
				printf("Capability \"%s\" is %s on vcpu %d\n",
					vm_capability_type2name(captype),
					val ? "set" : "not set", vcpu);
			} else if (errno == ENOENT) {
				error = 0;
				printf("Capability \"%s\" is not available\n",
					vm_capability_type2name(captype));
			} else {
				break;
			}
		}
	}

	if (!error && (get_active_cpus || get_all)) {
		error = vm_active_cpus(ctx, &cpus);
		if (!error)
			print_cpus("active cpus", &cpus);
	}

	if (!error && (get_suspended_cpus || get_all)) {
		error = vm_suspended_cpus(ctx, &cpus);
		if (!error)
			print_cpus("suspended cpus", &cpus);
	}

	if (!error && run) {
		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
		assert(error == 0);

		error = vm_run(ctx, vcpu, rip, &vmexit);
		if (error == 0)
			dump_vm_run_exitcode(&vmexit, vcpu);
		else
			printf("vm_run error %d\n", error);
	}

	if (!error && force_reset)
		error = vm_suspend(ctx, VM_SUSPEND_RESET);

	if (!error && force_poweroff)
		error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);

	if (error)
		printf("errno = %d\n", errno);

	if (!error && destroy)
		vm_destroy(ctx);

	exit(error);
}
示例#29
0
文件: kramvm.c 项目: kisom/kram
static int
run(const char *filespec)
{
	struct stat	 st;
	int		 i;
	int		 res;
	int		 status = EXIT_FAILURE;
	VM		 vm = NULL;
	FILE		*bin = NULL;
	uint8_t		*prog = NULL;

	if (-1 == stat(filespec, &st))
		goto exit;

	printf("Loading %lu byte program.\n", st.st_size);
	if (st.st_size > memory) {
		fprintf(stderr, "Out of memory (prog=%uB, vm=%uB).\n",
		    (unsigned)st.st_size, memory);
		return EXIT_FAILURE;
	}

	prog = malloc(st.st_size);
	if (NULL == prog)
		goto exit;

	if (NULL == (bin = fopen(filespec, "rb")))
		goto exit;

	if (st.st_size != (off_t)fread(prog, sizeof(uint8_t), st.st_size, bin))
		goto exit;

	vm = vm_new_with(memory, sp, entry);
	if (NULL == vm)
		goto exit;

	printf("Starting VM.\n");
	vm_load(vm, prog, st.st_size);
	for (i = 0; i < 72; i++) printf("-");
	printf("\n");
	res = vm_run(vm);
	for (i = 0; i < 72; i++) printf("-");
	printf("\n");
	if (VM_ERR == res)
		goto exit;

	printf("OK\n");
	status = EXIT_SUCCESS;

exit:
	if (prog)
		free(prog);

	if (vm) {
		if (debug) {
			vm_dump_registers(vm);
		}
		vm_destroy(vm);
	}

	if (bin)
		fclose(bin);
	return status;
}