示例#1
0
bool
basic_jit_cache::init_translation_cache(uint32 size)
{
	size *= 1024;

	// Round up translation cache size to 16 KB boundaries
	const uint32 roundup = 16 * 1024;
	cache_size = (size + JIT_CACHE_SIZE_GUARD + roundup - 1) & -roundup;
	assert(cache_size > 0);

	tcode_start = (uint8 *)vm_acquire(cache_size, VM_MAP_PRIVATE | VM_MAP_32BIT);
	if (tcode_start == VM_MAP_FAILED) {
		tcode_start = NULL;
		return false;
	}

	if (vm_protect(tcode_start, cache_size,
				   VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) {
		vm_release(tcode_start, cache_size);
		tcode_start = NULL;
		return false;
	}

  done:
	D(bug("basic_jit_cache: Translation cache: %d KB at %p\n", cache_size / 1024, tcode_start));
	code_start = tcode_start;
	code_p = code_start;
	code_end = code_p + size;
	return true;
}
示例#2
0
uint8 *
basic_jit_cache::copy_data(const uint8 *block, uint32 size)
{
	const int ALIGN = 16;
	uint8 *ptr;

	if (data && (data->offs + size) < data->size)
		ptr = (uint8 *)data + data->offs;
	else {
		// No free space left, allocate a new chunk
		uint32 to_alloc = sizeof(*data) + size + ALIGN;
		uint32 page_size = vm_get_page_size();
		to_alloc = (to_alloc + page_size - 1) & -page_size;

		D(bug("basic_jit_cache: Allocate data pool (%d KB)\n", to_alloc / 1024));
		ptr = (uint8 *)vm_acquire(to_alloc, VM_MAP_PRIVATE | VM_MAP_32BIT);
		if (ptr == VM_MAP_FAILED) {
			fprintf(stderr, "FATAL: Could not allocate data pool!\n");
			abort();
		}

		data_chunk_t *dcp = (data_chunk_t *)ptr;
		dcp->size = to_alloc;
		dcp->offs = (sizeof(*data) + ALIGN - 1) & -ALIGN;
		dcp->next = data;
		data = dcp;

		ptr += dcp->offs;
	}

	memcpy(ptr, block, size);
	data->offs += (size + ALIGN - 1) & -ALIGN;
	D(bug("basic_jit_cache: DATA %p, %d bytes [data=%p, offs=%u]\n", ptr, size, data, data->offs));
	return ptr;
}
示例#3
0
文件: main.c 项目: kidaa/luvi
int main(int argc, char* argv[] ) {

  lua_State* L;
  int index;
  int res;

  // Hooks in libuv that need to be done in main.
  argv = uv_setup_args(argc, argv);

  // Create the lua state.
  L = vm_acquire(); 
  if (L == NULL) {
    fprintf(stderr, "luaL_newstate has failed\n");
    return 1;
  }

  luv_set_thread_cb(vm_acquire, vm_release);

#ifdef WITH_WINSVC
  // Store luvi module definition at preload.openssl
  lua_pushcfunction(L, luaopen_winsvc);
  lua_setfield(L, -2, "winsvc");
  lua_pushcfunction(L, luaopen_winsvcaux);
  lua_setfield(L, -2, "winsvcaux");
#endif

  // Load the init.lua script
  if (luaL_loadstring(L, "return require('init')(...)")) {
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
    return -1;
  }

  // Pass the command-line arguments to init as a zero-indexed table
  lua_createtable (L, argc, 0);
  for (index = 0; index < argc; index++) {
    lua_pushstring(L, argv[index]);
    lua_rawseti(L, -2, index);
  }

  // Start the main script.
  if (lua_pcall(L, 1, 1, 0)) {
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
    return -1;
  }

  // Use the return value from the script as process exit code.
  res = 0;
  if (lua_type(L, -1) == LUA_TNUMBER) {
    res = lua_tointeger(L, -1);
  }
  vm_release(L);
  return res;
}
示例#4
0
int
main(void)
{
	D(bug("%s\n", __func__));

	int i, j;

	vm_init();

	vm_uintptr_t page_size = vm_get_page_size();
	
	char *area;
	const int n_pages = 7;
	const int area_size = n_pages * page_size;
	const int map_options = VM_MAP_DEFAULT | VM_MAP_WRITE_WATCH;
	if ((area = (char *)vm_acquire(area_size, map_options)) == VM_MAP_FAILED)
		return 1;

	unsigned int n_modified_pages_expected = 0;
	static const int touch_page[n_pages] = { 0, 1, 1, 0, 1, 0, 1 };
	for (i = 0; i < n_pages; i++) {
		if (touch_page[i]) {
			area[i * page_size] = 1;
			++n_modified_pages_expected;
		}
	}

	char *modified_pages[n_pages];
	unsigned int n_modified_pages = n_pages;
	if (vm_get_write_watch(area, area_size, (void **)modified_pages, &n_modified_pages) < 0)
		return 2;
	if (n_modified_pages != n_modified_pages_expected)
		return 3;
	for (i = 0, j = 0; i < n_pages; i++) {
		char v = area[i * page_size];
		if ((touch_page[i] && !v) || (!touch_page[i] && v))
			return 4;
		if (!touch_page[i])
			continue;
		if (modified_pages[j] != (area + i * page_size))
			return 5;
		++j;
	}

	vm_release(area, area_size);
	return 0;
}
示例#5
0
/* Generic signal handler */
void signal_gen_handler(int sig)
{
    switch(sig) {
    case SIGHUP:
        /* For future use */
        break;

    case SIGQUIT:
        /* save VM context */
        vm_save_state = TRUE;
        break;

    /* Handle SIGPIPE by ignoring it */
    case SIGPIPE:
        fprintf(stderr,"Error: unwanted SIGPIPE.\n");
        break;

    case SIGINT:
        /* CTRL+C has been pressed */
        if (hypervisor_mode)
            hypervisor_stopsig();
        else {
            /* In theory, this shouldn't happen thanks to VTTY settings */
            vm_instance_t *vm;

            if ((vm = vm_acquire("default")) != NULL) {
                /* Only forward ctrl-c if user has requested local terminal */
                if (vm->vtty_con_type == VTTY_TYPE_TERM) {
                    vtty_store_ctrlc(vm->vtty_con);
                } else {
                    vm_stop(vm);
                }
                vm_release(vm);
            } else {
                fprintf(stderr,"Error: Cannot acquire instance handle.\n");
            }
        }
        break;

    default:
        fprintf(stderr,"Unhandled signal %d\n",sig);
    }
}
示例#6
0
/* Find a VM in the registry */
void *hypervisor_find_vm(hypervisor_conn_t *conn,char *name)
{
   vm_platform_t *platform = conn->cur_module->opt;
   vm_instance_t *vm;

   if (!(vm = vm_acquire(name))) {
      hypervisor_send_reply(conn,HSC_ERR_UNK_OBJ,1,
                            "unable to find VM '%s'",name);
      return NULL;
   }

   if (vm->platform != platform) {
      vm_release(vm);
      hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,
                            "VM '%s' is not a VM type %s",
                            name,platform->name);
      return NULL;
   }

   return vm;
}
int main(int argc,char *argv[])
{
   vm_instance_t *vm;

#ifdef PROFILE
   atexit(profiler_savestat);
#endif

   printf("Cisco Router Simulation Platform (version %s)\n",sw_version);
   printf("Copyright (c) 2005-2007 Christophe Fillot.\n");
   printf("Build date: %s %s\n\n",__DATE__,__TIME__);

   /* Register platforms */
   register_default_platforms();

   /* Initialize timers */
   timer_init();

   /* Initialize object registry */
   registry_init();
   
   /* Initialize ATM module (for HEC checksums) */
   atm_init();

   /* Initialize CRC functions */
   crc_init();

   /* Initialize NetIO code */
   netio_rxl_init();

   /* Initialize NetIO packet filters */
   netio_filter_load_all();

   /* Initialize VTTY code */
   vtty_init();
   
   /* Parse standard command line */
   if (!run_hypervisor(argc,argv))
      parse_std_cmd_line(argc,argv);

   /* Create general log file */
   create_log_file();

   /* Periodic tasks initialization */
   if (ptask_init(0) == -1)
      exit(EXIT_FAILURE);

   /* Create instruction lookup tables */
   mips64_jit_create_ilt();
   mips64_exec_create_ilt();
   ppc32_jit_create_ilt();
   ppc32_exec_create_ilt();
   
   setup_signals();

   if (!hypervisor_mode) {
      /* Initialize the default instance */
      vm = vm_acquire("default");
      assert(vm != NULL);

      if (vm->platform->init_instance(vm) == -1) {
         fprintf(stderr,"Unable to initialize router instance.\n");
         exit(EXIT_FAILURE);
      }

      /* Start GDB server before the image to allow debugging from
         the begining of it's execution */ 
      if (vm->gdb_server_running)
      {
        /* Stop main CPU */
        vm_suspend(vm);
//         cpu_stop(vm->boot_cpu);
      
        if (gdb_server_start_listener(vm) < 0) {
            fprintf(stderr,"GDB server unable to create TCP sockets.\n");
            exit(EXIT_FAILURE);
        }
      }

#if (DEBUG_INSN_PERF_CNT > 0) || (DEBUG_BLOCK_PERF_CNT > 0)
      {
         m_uint32_t counter,prev = 0,delta;
         while(vm->status == VM_STATUS_RUNNING) {
            counter = cpu_get_perf_counter(vm->boot_cpu);
            delta = counter - prev;
            prev = counter;
            printf("delta = %u\n",delta);
            sleep(1);
         }
      }
#else
      /* Start instance monitoring */
      vm_monitor(vm);
#endif

      // FIXME: remove this kludge
      if (vm->gdb_server_running)
      {
         //while (vm->gdb_conn->active)
         //   usleep(1000000);
         gdb_server_close_control_sockets();
      }

      /* Free resources used by instance */
      vm_release(vm);

   } else {
      hypervisor_tcp_server(hypervisor_ip_address,hypervisor_tcp_port);
   }

   dynamips_reset();
   close_log_file();
   return(0);
}
示例#8
0
int main(int argc,char *argv[])
{
    vm_instance_t *vm;

#ifdef PROFILE
    atexit(profiler_savestat);
#endif

#ifdef USE_UNSTABLE
    printf("Cisco Router Simulation Platform (version %s/%s unstable)\n",
           sw_version,os_name);
#else
    printf("Cisco Router Simulation Platform (version %s/%s stable)\n",
           sw_version,os_name);
#endif

    printf("Copyright (c) 2005-2011 Christophe Fillot.\n");
    printf("Build date: %s %s\n\n",__DATE__,__TIME__);

    gen_uuid_init();

    /* Register platforms */
    register_default_platforms();

    /* Initialize timers */
    timer_init();

    /* Initialize object registry */
    registry_init();

    /* Initialize ATM module (for HEC checksums) */
    atm_init();

    /* Initialize CRC functions */
    crc_init();

    /* Initialize NetIO code */
    netio_rxl_init();

    /* Initialize NetIO packet filters */
    netio_filter_load_all();

    /* Initialize VTTY code */
    vtty_init();

    /* Parse standard command line */
    atexit(destroy_cmd_line_vars);
    if (!run_hypervisor(argc,argv))
        parse_std_cmd_line(argc,argv);

    /* Create general log file */
    create_log_file();

    /* Periodic tasks initialization */
    if (ptask_init(0) == -1)
        exit(EXIT_FAILURE);

    /* Create instruction lookup tables */
    mips64_jit_create_ilt();
    mips64_exec_create_ilt();
    ppc32_jit_create_ilt();
    ppc32_exec_create_ilt();

    setup_signals();

    if (!hypervisor_mode) {
        /* Initialize the default instance */
        vm = vm_acquire("default");
        assert(vm != NULL);

        if (vm_init_instance(vm) == -1) {
            fprintf(stderr,"Unable to initialize router instance.\n");
            exit(EXIT_FAILURE);
        }

#if (DEBUG_INSN_PERF_CNT > 0) || (DEBUG_BLOCK_PERF_CNT > 0)
        {
            m_uint32_t counter,prev = 0,delta;
            while(vm->status == VM_STATUS_RUNNING) {
                counter = cpu_get_perf_counter(vm->boot_cpu);
                delta = counter - prev;
                prev = counter;
                printf("delta = %u\n",delta);
                sleep(1);
            }
        }
#else
        /* Start instance monitoring */
        vm_monitor(vm);
#endif

        /* Free resources used by instance */
        vm_release(vm);
    } else {
        hypervisor_tcp_server(hypervisor_ip_address,hypervisor_tcp_port);
    }

    dynamips_reset();
    close_log_file();
    return(0);
}
示例#9
0
/* Tests covered here:
   - TEST_VM_PROT_* program slices actually succeeds when a crash occurs
   - TEST_VM_MAP_ANON* program slices succeeds when it could be compiled
*/
int main(void)
{
	vm_init();

	signal(SIGSEGV, fault_handler);
#ifdef SIGBUS
	signal(SIGBUS,  fault_handler);
#endif

#define page_align(address) ((char *)((vm_uintptr_t)(address) & -page_size))
	vm_uintptr_t page_size = vm_get_page_size();

	const int area_size = 6 * page_size;
	volatile char * area = (volatile char *) vm_acquire(area_size);
	volatile char * fault_address = area + (page_size * 7) / 2;

#if defined(TEST_VM_MMAP_ANON) || defined(TEST_VM_MMAP_ANONYMOUS)
	if (area == VM_MAP_FAILED)
		return 1;

	if (vm_release((char *)area, area_size) < 0)
		return 1;

	return 0;
#endif

#if defined(TEST_VM_PROT_NONE_READ) || defined(TEST_VM_PROT_NONE_WRITE)
	if (area == VM_MAP_FAILED)
		return 0;

	if (vm_protect(page_align(fault_address), page_size, VM_PAGE_NOACCESS) < 0)
		return 0;
#endif

#if defined(TEST_VM_PROT_RDWR_WRITE)
	if (area == VM_MAP_FAILED)
		return 1;

	if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0)
		return 1;

	if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ | VM_PAGE_WRITE) < 0)
		return 1;
#endif

#if defined(TEST_VM_PROT_READ_WRITE)
	if (vm_protect(page_align(fault_address), page_size, VM_PAGE_READ) < 0)
		return 0;
#endif

#if defined(TEST_VM_PROT_NONE_READ)
	// this should cause a core dump
	char foo = *fault_address;
	return 0;
#endif

#if defined(TEST_VM_PROT_NONE_WRITE) || defined(TEST_VM_PROT_READ_WRITE)
	// this should cause a core dump
	*fault_address = 'z';
	return 0;
#endif

#if defined(TEST_VM_PROT_RDWR_WRITE)
	// this should not cause a core dump
	*fault_address = 'z';
	return 0;
#endif
}