⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vl.c

📁 qemu性能直逼VMware的仿真器QEMU 的模擬速度約為實機的 25%;約為 Bochs 的 60 倍。Plex86、User-Mode-Linux、VMware 和 Virtual PC 則比
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (!qemu_put_mouse_event_head) {        term_printf("No mouse devices connected\n");        return;    }    term_printf("Mouse devices available:\n");    cursor = qemu_put_mouse_event_head;    while (cursor != NULL) {        term_printf("%c Mouse #%d: %s\n",                    (cursor == qemu_put_mouse_event_current ? '*' : ' '),                    index, cursor->qemu_put_mouse_event_name);        index++;        cursor = cursor->next;    }}void do_mouse_set(int index){    QEMUPutMouseEntry *cursor;    int i = 0;    if (!qemu_put_mouse_event_head) {        term_printf("No mouse devices connected\n");        return;    }    cursor = qemu_put_mouse_event_head;    while (cursor != NULL && index != i) {        i++;        cursor = cursor->next;    }    if (cursor != NULL)        qemu_put_mouse_event_current = cursor;    else        term_printf("Mouse at given index not found\n");}/* compute with 96 bit intermediate result: (a*b)/c */uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c){    union {        uint64_t ll;        struct {#ifdef WORDS_BIGENDIAN            uint32_t high, low;#else            uint32_t low, high;#endif                    } l;    } u, res;    uint64_t rl, rh;    u.ll = a;    rl = (uint64_t)u.l.low * (uint64_t)b;    rh = (uint64_t)u.l.high * (uint64_t)b;    rh += (rl >> 32);    res.l.high = rh / c;    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;    return res.ll;}/***********************************************************//* real time host monotonic timer */#define QEMU_TIMER_BASE 1000000000LL#ifdef WIN32static int64_t clock_freq;static void init_get_clock(void){    LARGE_INTEGER freq;    int ret;    ret = QueryPerformanceFrequency(&freq);    if (ret == 0) {        fprintf(stderr, "Could not calibrate ticks\n");        exit(1);    }    clock_freq = freq.QuadPart;}static int64_t get_clock(void){    LARGE_INTEGER ti;    QueryPerformanceCounter(&ti);    return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);}#elsestatic int use_rt_clock;static void init_get_clock(void){    use_rt_clock = 0;#if defined(__linux__)    {        struct timespec ts;        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {            use_rt_clock = 1;        }    }#endif}static int64_t get_clock(void){#if defined(__linux__)    if (use_rt_clock) {        struct timespec ts;        clock_gettime(CLOCK_MONOTONIC, &ts);        return ts.tv_sec * 1000000000LL + ts.tv_nsec;    } else #endif    {        /* XXX: using gettimeofday leads to problems if the date           changes, so it should be avoided. */        struct timeval tv;        gettimeofday(&tv, NULL);        return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);    }}#endif/***********************************************************//* guest cycle counter */static int64_t cpu_ticks_prev;static int64_t cpu_ticks_offset;static int64_t cpu_clock_offset;static int cpu_ticks_enabled;/* return the host CPU cycle counter and handle stop/restart */int64_t cpu_get_ticks(void){    if (!cpu_ticks_enabled) {        return cpu_ticks_offset;    } else {        int64_t ticks;        ticks = cpu_get_real_ticks();        if (cpu_ticks_prev > ticks) {            /* Note: non increasing ticks may happen if the host uses               software suspend */            cpu_ticks_offset += cpu_ticks_prev - ticks;        }        cpu_ticks_prev = ticks;        return ticks + cpu_ticks_offset;    }}/* return the host CPU monotonic timer and handle stop/restart */static int64_t cpu_get_clock(void){    int64_t ti;    if (!cpu_ticks_enabled) {        return cpu_clock_offset;    } else {        ti = get_clock();        return ti + cpu_clock_offset;    }}/* enable cpu_get_ticks() */void cpu_enable_ticks(void){    if (!cpu_ticks_enabled) {        cpu_ticks_offset -= cpu_get_real_ticks();        cpu_clock_offset -= get_clock();        cpu_ticks_enabled = 1;    }}/* disable cpu_get_ticks() : the clock is stopped. You must not call   cpu_get_ticks() after that.  */void cpu_disable_ticks(void){    if (cpu_ticks_enabled) {        cpu_ticks_offset = cpu_get_ticks();        cpu_clock_offset = cpu_get_clock();        cpu_ticks_enabled = 0;    }}/***********************************************************//* timers */ #define QEMU_TIMER_REALTIME 0#define QEMU_TIMER_VIRTUAL  1struct QEMUClock {    int type;    /* XXX: add frequency */};struct QEMUTimer {    QEMUClock *clock;    int64_t expire_time;    QEMUTimerCB *cb;    void *opaque;    struct QEMUTimer *next;};QEMUClock *rt_clock;QEMUClock *vm_clock;static QEMUTimer *active_timers[2];#ifdef _WIN32static MMRESULT timerID;static HANDLE host_alarm = NULL;static unsigned int period = 1;#else/* frequency of the times() clock tick */static int timer_freq;#endifQEMUClock *qemu_new_clock(int type){    QEMUClock *clock;    clock = qemu_mallocz(sizeof(QEMUClock));    if (!clock)        return NULL;    clock->type = type;    return clock;}QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque){    QEMUTimer *ts;    ts = qemu_mallocz(sizeof(QEMUTimer));    ts->clock = clock;    ts->cb = cb;    ts->opaque = opaque;    return ts;}void qemu_free_timer(QEMUTimer *ts){    qemu_free(ts);}/* stop a timer, but do not dealloc it */void qemu_del_timer(QEMUTimer *ts){    QEMUTimer **pt, *t;    /* NOTE: this code must be signal safe because       qemu_timer_expired() can be called from a signal. */    pt = &active_timers[ts->clock->type];    for(;;) {        t = *pt;        if (!t)            break;        if (t == ts) {            *pt = t->next;            break;        }        pt = &t->next;    }}/* modify the current timer so that it will be fired when current_time   >= expire_time. The corresponding callback will be called. */void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time){    QEMUTimer **pt, *t;    qemu_del_timer(ts);    /* add the timer in the sorted list */    /* NOTE: this code must be signal safe because       qemu_timer_expired() can be called from a signal. */    pt = &active_timers[ts->clock->type];    for(;;) {        t = *pt;        if (!t)            break;        if (t->expire_time > expire_time)             break;        pt = &t->next;    }    ts->expire_time = expire_time;    ts->next = *pt;    *pt = ts;}int qemu_timer_pending(QEMUTimer *ts){    QEMUTimer *t;    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {        if (t == ts)            return 1;    }    return 0;}static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time){    if (!timer_head)        return 0;    return (timer_head->expire_time <= current_time);}static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time){    QEMUTimer *ts;        for(;;) {        ts = *ptimer_head;        if (!ts || ts->expire_time > current_time)            break;        /* remove timer from the list before calling the callback */        *ptimer_head = ts->next;        ts->next = NULL;                /* run the callback (the timer list can be modified) */        ts->cb(ts->opaque);    }}int64_t qemu_get_clock(QEMUClock *clock){    switch(clock->type) {    case QEMU_TIMER_REALTIME:        return get_clock() / 1000000;    default:    case QEMU_TIMER_VIRTUAL:        return cpu_get_clock();    }}static void init_timers(void){    init_get_clock();    ticks_per_sec = QEMU_TIMER_BASE;    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);}/* save a timer */void qemu_put_timer(QEMUFile *f, QEMUTimer *ts){    uint64_t expire_time;    if (qemu_timer_pending(ts)) {        expire_time = ts->expire_time;    } else {        expire_time = -1;    }    qemu_put_be64(f, expire_time);}void qemu_get_timer(QEMUFile *f, QEMUTimer *ts){    uint64_t expire_time;    expire_time = qemu_get_be64(f);    if (expire_time != -1) {        qemu_mod_timer(ts, expire_time);    } else {        qemu_del_timer(ts);    }}static void timer_save(QEMUFile *f, void *opaque){    if (cpu_ticks_enabled) {        hw_error("cannot save state if virtual timers are running");    }    qemu_put_be64s(f, &cpu_ticks_offset);    qemu_put_be64s(f, &ticks_per_sec);    qemu_put_be64s(f, &cpu_clock_offset);}static int timer_load(QEMUFile *f, void *opaque, int version_id){    if (version_id != 1 && version_id != 2)        return -EINVAL;    if (cpu_ticks_enabled) {        return -EINVAL;    }    qemu_get_be64s(f, &cpu_ticks_offset);    qemu_get_be64s(f, &ticks_per_sec);    if (version_id == 2) {        qemu_get_be64s(f, &cpu_clock_offset);    }    return 0;}#ifdef _WIN32void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,                                  DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)#elsestatic void host_alarm_handler(int host_signum)#endif{#if 0#define DISP_FREQ 1000    {        static int64_t delta_min = INT64_MAX;        static int64_t delta_max, delta_cum, last_clock, delta, ti;        static int count;        ti = qemu_get_clock(vm_clock);        if (last_clock != 0) {            delta = ti - last_clock;            if (delta < delta_min)                delta_min = delta;            if (delta > delta_max)                delta_max = delta;            delta_cum += delta;            if (++count == DISP_FREQ) {                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",                       muldiv64(delta_min, 1000000, ticks_per_sec),                       muldiv64(delta_max, 1000000, ticks_per_sec),                       muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),                       (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));                count = 0;                delta_min = INT64_MAX;                delta_max = 0;                delta_cum = 0;            }        }        last_clock = ti;    }#endif    if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],                           qemu_get_clock(vm_clock)) ||        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],                           qemu_get_clock(rt_clock))) {#ifdef _WIN32        SetEvent(host_alarm);#endif        CPUState *env = cpu_single_env;        if (env) {            /* stop the currently executing cpu because a timer occured */            cpu_interrupt(env, CPU_INTERRUPT_EXIT);#ifdef USE_KQEMU            if (env->kqemu_enabled) {                kqemu_cpu_interrupt(env);            }#endif        }    }}#ifndef _WIN32#if defined(__linux__)#define RTC_FREQ 1024static int rtc_fd;static int start_rtc_timer(void){    rtc_fd = open("/dev/rtc", O_RDONLY);    if (rtc_fd < 0)        return -1;    if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");        goto fail;    }    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {    fail:        close(rtc_fd);        return -1;    }    pit_min_timer_count = PIT_FREQ / RTC_FREQ;    return 0;}#elsestatic int start_rtc_timer(void){    return -1;}#endif /* !defined(__linux__) */#endif /* !defined(_WIN32) */static void init_timer_alarm(void){#ifdef _WIN32    {        int count=0;        TIMECAPS tc;        ZeroMemory(&tc, sizeof(TIMECAPS));        timeGetDevCaps(&tc, sizeof(TIMECAPS));        if (period < tc.wPeriodMin)            period = tc.wPeriodMin;        timeBeginPeriod(period);        timerID = timeSetEvent(1,     // interval (ms)                               period,     // resolution                               host_alarm_handler, // function                               (DWORD)&count,  // user parameter                               TIME_PERIODIC | TIME_CALLBACK_FUNCTION); 	if( !timerID ) {            perror("failed timer alarm");            exit(1); 	}        host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);        if (!host_alarm) {            perror("failed CreateEvent");            exit(1);        }        qemu_add_wait_object(host_alarm, NULL, NULL);    }    pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;#else    {        struct sigaction act;        struct itimerval itv;                /* get times() syscall frequency */        timer_freq = sysconf(_SC_CLK_TCK);                /* timer signal */        sigfillset(&act.sa_mask);       act.sa_flags = 0;#if defined (TARGET_I386) && defined(USE_CODE_COPY)        act.sa_flags |= SA_ONSTACK;#endif        act.sa_handler = host_alarm_handler;        sigaction(SIGALRM, &act, NULL);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -