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

📄 vl.c

📁 qemu虚拟机代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * QEMU System Emulator *  * Copyright (c) 2003-2005 Fabrice Bellard *  * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */#include "vl.h"#include <unistd.h>#include <fcntl.h>#include <signal.h>#include <time.h>#include <errno.h>#include <sys/time.h>#ifndef _WIN32#include <sys/times.h>#include <sys/wait.h>#include <termios.h>#include <sys/poll.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <sys/socket.h>#include <netinet/in.h>#include <dirent.h>#include <netdb.h>#ifdef _BSD#include <sys/stat.h>#ifndef __APPLE__#include <libutil.h>#endif#else#ifndef __sun__#include <linux/if.h>#include <linux/if_tun.h>#include <pty.h>#include <malloc.h>#include <linux/rtc.h>#include <linux/ppdev.h>#endif#endif#endif#if defined(CONFIG_SLIRP)#include "libslirp.h"#endif#ifdef _WIN32#include <malloc.h>#include <sys/timeb.h>#include <windows.h>#define getopt_long_only getopt_long#define memalign(align, size) malloc(size)#endif#include "qemu_socket.h"#ifdef CONFIG_SDL#ifdef __APPLE__#include <SDL/SDL.h>#endif#endif /* CONFIG_SDL */#ifdef CONFIG_COCOA#undef main#define main qemu_main#endif /* CONFIG_COCOA */#include "disas.h"#include "exec-all.h"#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"//#define DEBUG_UNUSED_IOPORT//#define DEBUG_IOPORT#if !defined(CONFIG_SOFTMMU)#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)#else#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)#endif#ifdef TARGET_PPC#define DEFAULT_RAM_SIZE 144#else#define DEFAULT_RAM_SIZE 128#endif/* in ms */#define GUI_REFRESH_INTERVAL 30/* XXX: use a two level table to limit memory usage */#define MAX_IOPORTS 65536const char *bios_dir = CONFIG_QEMU_SHAREDIR;char phys_ram_file[1024];void *ioport_opaque[MAX_IOPORTS];IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];int vga_ram_size;int bios_size;static DisplayState display_state;int nographic;const char* keyboard_layout = NULL;int64_t ticks_per_sec;int boot_device = 'c';int ram_size;int pit_min_timer_count = 0;int nb_nics;NICInfo nd_table[MAX_NICS];QEMUTimer *gui_timer;int vm_running;int rtc_utc = 1;int cirrus_vga_enabled = 1;#ifdef TARGET_SPARCint graphic_width = 1024;int graphic_height = 768;#elseint graphic_width = 800;int graphic_height = 600;#endifint graphic_depth = 15;int full_screen = 0;CharDriverState *serial_hds[MAX_SERIAL_PORTS];CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];#ifdef TARGET_I386int win2k_install_hack = 0;#endifint usb_enabled = 0;USBPort *vm_usb_ports[MAX_VM_USB_PORTS];USBDevice *vm_usb_hub;static VLANState *first_vlan;int smp_cpus = 1;int vnc_display = -1;#if defined(TARGET_SPARC)#define MAX_CPUS 16#elif defined(TARGET_I386)#define MAX_CPUS 255#else#define MAX_CPUS 1#endif/***********************************************************//* x86 ISA bus support */target_phys_addr_t isa_mem_base = 0;PicState2 *isa_pic;uint32_t default_ioport_readb(void *opaque, uint32_t address){#ifdef DEBUG_UNUSED_IOPORT    fprintf(stderr, "inb: port=0x%04x\n", address);#endif    return 0xff;}void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data){#ifdef DEBUG_UNUSED_IOPORT    fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);#endif}/* default is to make two byte accesses */uint32_t default_ioport_readw(void *opaque, uint32_t address){    uint32_t data;    data = ioport_read_table[0][address](ioport_opaque[address], address);    address = (address + 1) & (MAX_IOPORTS - 1);    data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;    return data;}void default_ioport_writew(void *opaque, uint32_t address, uint32_t data){    ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);    address = (address + 1) & (MAX_IOPORTS - 1);    ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);}uint32_t default_ioport_readl(void *opaque, uint32_t address){#ifdef DEBUG_UNUSED_IOPORT    fprintf(stderr, "inl: port=0x%04x\n", address);#endif    return 0xffffffff;}void default_ioport_writel(void *opaque, uint32_t address, uint32_t data){#ifdef DEBUG_UNUSED_IOPORT    fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);#endif}void init_ioports(void){    int i;    for(i = 0; i < MAX_IOPORTS; i++) {        ioport_read_table[0][i] = default_ioport_readb;        ioport_write_table[0][i] = default_ioport_writeb;        ioport_read_table[1][i] = default_ioport_readw;        ioport_write_table[1][i] = default_ioport_writew;        ioport_read_table[2][i] = default_ioport_readl;        ioport_write_table[2][i] = default_ioport_writel;    }}/* size is the word size in byte */int register_ioport_read(int start, int length, int size,                          IOPortReadFunc *func, void *opaque){    int i, bsize;    if (size == 1) {        bsize = 0;    } else if (size == 2) {        bsize = 1;    } else if (size == 4) {        bsize = 2;    } else {        hw_error("register_ioport_read: invalid size");        return -1;    }    for(i = start; i < start + length; i += size) {        ioport_read_table[bsize][i] = func;        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)            hw_error("register_ioport_read: invalid opaque");        ioport_opaque[i] = opaque;    }    return 0;}/* size is the word size in byte */int register_ioport_write(int start, int length, int size,                           IOPortWriteFunc *func, void *opaque){    int i, bsize;    if (size == 1) {        bsize = 0;    } else if (size == 2) {        bsize = 1;    } else if (size == 4) {        bsize = 2;    } else {        hw_error("register_ioport_write: invalid size");        return -1;    }    for(i = start; i < start + length; i += size) {        ioport_write_table[bsize][i] = func;        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)            hw_error("register_ioport_read: invalid opaque");        ioport_opaque[i] = opaque;    }    return 0;}void isa_unassign_ioport(int start, int length){    int i;    for(i = start; i < start + length; i++) {        ioport_read_table[0][i] = default_ioport_readb;        ioport_read_table[1][i] = default_ioport_readw;        ioport_read_table[2][i] = default_ioport_readl;        ioport_write_table[0][i] = default_ioport_writeb;        ioport_write_table[1][i] = default_ioport_writew;        ioport_write_table[2][i] = default_ioport_writel;    }}/***********************************************************/void pstrcpy(char *buf, int buf_size, const char *str){    int c;    char *q = buf;    if (buf_size <= 0)        return;    for(;;) {        c = *str++;        if (c == 0 || q >= buf + buf_size - 1)            break;        *q++ = c;    }    *q = '\0';}/* strcat and truncate. */char *pstrcat(char *buf, int buf_size, const char *s){    int len;    len = strlen(buf);    if (len < buf_size)         pstrcpy(buf + len, buf_size - len, s);    return buf;}int strstart(const char *str, const char *val, const char **ptr){    const char *p, *q;    p = str;    q = val;    while (*q != '\0') {        if (*p != *q)            return 0;        p++;        q++;    }    if (ptr)        *ptr = p;    return 1;}void cpu_outb(CPUState *env, int addr, int val){#ifdef DEBUG_IOPORT    if (loglevel & CPU_LOG_IOPORT)        fprintf(logfile, "outb: %04x %02x\n", addr, val);#endif        ioport_write_table[0][addr](ioport_opaque[addr], addr, val);#ifdef USE_KQEMU    if (env)        env->last_io_time = cpu_get_time_fast();#endif}void cpu_outw(CPUState *env, int addr, int val){#ifdef DEBUG_IOPORT    if (loglevel & CPU_LOG_IOPORT)        fprintf(logfile, "outw: %04x %04x\n", addr, val);#endif        ioport_write_table[1][addr](ioport_opaque[addr], addr, val);#ifdef USE_KQEMU    if (env)        env->last_io_time = cpu_get_time_fast();#endif}void cpu_outl(CPUState *env, int addr, int val){#ifdef DEBUG_IOPORT    if (loglevel & CPU_LOG_IOPORT)        fprintf(logfile, "outl: %04x %08x\n", addr, val);#endif    ioport_write_table[2][addr](ioport_opaque[addr], addr, val);#ifdef USE_KQEMU    if (env)        env->last_io_time = cpu_get_time_fast();#endif}int cpu_inb(CPUState *env, int addr){    int val;    val = ioport_read_table[0][addr](ioport_opaque[addr], addr);#ifdef DEBUG_IOPORT    if (loglevel & CPU_LOG_IOPORT)        fprintf(logfile, "inb : %04x %02x\n", addr, val);#endif#ifdef USE_KQEMU    if (env)        env->last_io_time = cpu_get_time_fast();#endif    return val;}int cpu_inw(CPUState *env, int addr){    int val;    val = ioport_read_table[1][addr](ioport_opaque[addr], addr);#ifdef DEBUG_IOPORT    if (loglevel & CPU_LOG_IOPORT)        fprintf(logfile, "inw : %04x %04x\n", addr, val);#endif#ifdef USE_KQEMU    if (env)        env->last_io_time = cpu_get_time_fast();#endif    return val;}int cpu_inl(CPUState *env, int addr){    int val;    val = ioport_read_table[2][addr](ioport_opaque[addr], addr);#ifdef DEBUG_IOPORT    if (loglevel & CPU_LOG_IOPORT)        fprintf(logfile, "inl : %04x %08x\n", addr, val);#endif#ifdef USE_KQEMU    if (env)        env->last_io_time = cpu_get_time_fast();#endif    return val;}/***********************************************************/void hw_error(const char *fmt, ...){    va_list ap;    CPUState *env;    va_start(ap, fmt);    fprintf(stderr, "qemu: hardware error: ");    vfprintf(stderr, fmt, ap);    fprintf(stderr, "\n");    for(env = first_cpu; env != NULL; env = env->next_cpu) {        fprintf(stderr, "CPU #%d:\n", env->cpu_index);#ifdef TARGET_I386        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);#else        cpu_dump_state(env, stderr, fprintf, 0);#endif    }    va_end(ap);    abort();}/***********************************************************//* keyboard/mouse */static QEMUPutKBDEvent *qemu_put_kbd_event;static void *qemu_put_kbd_event_opaque;static QEMUPutMouseEvent *qemu_put_mouse_event;static void *qemu_put_mouse_event_opaque;static int qemu_put_mouse_event_absolute;void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque){    qemu_put_kbd_event_opaque = opaque;    qemu_put_kbd_event = func;}void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque, int absolute){    qemu_put_mouse_event_opaque = opaque;    qemu_put_mouse_event = func;    qemu_put_mouse_event_absolute = absolute;}void kbd_put_keycode(int keycode){    if (qemu_put_kbd_event) {        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);    }}void kbd_mouse_event(int dx, int dy, int dz, int buttons_state){    if (qemu_put_mouse_event) {        qemu_put_mouse_event(qemu_put_mouse_event_opaque,                              dx, dy, dz, buttons_state);    }}int kbd_mouse_is_absolute(void){    return qemu_put_mouse_event_absolute;}/***********************************************************//* timers */#if defined(__powerpc__)static inline uint32_t get_tbl(void) {    uint32_t tbl;    asm volatile("mftb %0" : "=r" (tbl));    return tbl;}static inline uint32_t get_tbu(void) {	uint32_t tbl;	asm volatile("mftbu %0" : "=r" (tbl));	return tbl;}int64_t cpu_get_real_ticks(void){    uint32_t l, h, h1;    /* NOTE: we test if wrapping has occurred */    do {        h = get_tbu();        l = get_tbl();        h1 = get_tbu();    } while (h != h1);    return ((int64_t)h << 32) | l;}#elif defined(__i386__)int64_t cpu_get_real_ticks(void){#ifdef _WIN32    LARGE_INTEGER ti;    QueryPerformanceCounter(&ti);    return ti.QuadPart;#else    int64_t val;    asm volatile ("rdtsc" : "=A" (val));    return val;#endif}#elif defined(__x86_64__)int64_t cpu_get_real_ticks(void){    uint32_t low,high;    int64_t val;    asm volatile("rdtsc" : "=a" (low), "=d" (high));    val = high;    val <<= 32;    val |= low;    return val;}

⌨️ 快捷键说明

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