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

📄 monitor.c

📁 qemu虚拟机代码
💻 C
📖 第 1 页 / 共 4 页
字号:
                                    uint32_t addrh, uint32_t addrl){    target_long addr = GET_TLONG(addrh, addrl);    memory_dump(count, format, size, addr, 1);}static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall){    target_long val = GET_TLONG(valh, vall);#if TARGET_LONG_BITS == 32    switch(format) {    case 'o':        term_printf("%#o", val);        break;    case 'x':        term_printf("%#x", val);        break;    case 'u':        term_printf("%u", val);        break;    default:    case 'd':        term_printf("%d", val);        break;    case 'c':        term_printc(val);        break;    }#else    switch(format) {    case 'o':        term_printf("%#llo", val);        break;    case 'x':        term_printf("%#llx", val);        break;    case 'u':        term_printf("%llu", val);        break;    default:    case 'd':        term_printf("%lld", val);        break;    case 'c':        term_printc(val);        break;    }#endif    term_printf("\n");}static void do_sum(uint32_t start, uint32_t size){    uint32_t addr;    uint8_t buf[1];    uint16_t sum;    sum = 0;    for(addr = start; addr < (start + size); addr++) {        cpu_physical_memory_rw(addr, buf, 1, 0);        /* BSD sum algorithm ('sum' Unix command) */        sum = (sum >> 1) | (sum << 15);        sum += buf[0];    }    term_printf("%05d\n", sum);}typedef struct {    int keycode;    const char *name;} KeyDef;static const KeyDef key_defs[] = {    { 0x2a, "shift" },    { 0x36, "shift_r" },        { 0x38, "alt" },    { 0xb8, "alt_r" },    { 0x1d, "ctrl" },    { 0x9d, "ctrl_r" },    { 0xdd, "menu" },    { 0x01, "esc" },    { 0x02, "1" },    { 0x03, "2" },    { 0x04, "3" },    { 0x05, "4" },    { 0x06, "5" },    { 0x07, "6" },    { 0x08, "7" },    { 0x09, "8" },    { 0x0a, "9" },    { 0x0b, "0" },    { 0x0e, "backspace" },    { 0x0f, "tab" },    { 0x10, "q" },    { 0x11, "w" },    { 0x12, "e" },    { 0x13, "r" },    { 0x14, "t" },    { 0x15, "y" },    { 0x16, "u" },    { 0x17, "i" },    { 0x18, "o" },    { 0x19, "p" },    { 0x1c, "ret" },    { 0x1e, "a" },    { 0x1f, "s" },    { 0x20, "d" },    { 0x21, "f" },    { 0x22, "g" },    { 0x23, "h" },    { 0x24, "j" },    { 0x25, "k" },    { 0x26, "l" },    { 0x2c, "z" },    { 0x2d, "x" },    { 0x2e, "c" },    { 0x2f, "v" },    { 0x30, "b" },    { 0x31, "n" },    { 0x32, "m" },        { 0x39, "spc" },    { 0x3a, "caps_lock" },    { 0x3b, "f1" },    { 0x3c, "f2" },    { 0x3d, "f3" },    { 0x3e, "f4" },    { 0x3f, "f5" },    { 0x40, "f6" },    { 0x41, "f7" },    { 0x42, "f8" },    { 0x43, "f9" },    { 0x44, "f10" },    { 0x45, "num_lock" },    { 0x46, "scroll_lock" },    { 0x56, "<" },    { 0x57, "f11" },    { 0x58, "f12" },    { 0xb7, "print" },    { 0xc7, "home" },    { 0xc9, "pgup" },    { 0xd1, "pgdn" },    { 0xcf, "end" },    { 0xcb, "left" },    { 0xc8, "up" },    { 0xd0, "down" },    { 0xcd, "right" },    { 0xd2, "insert" },    { 0xd3, "delete" },    { 0, NULL },};static int get_keycode(const char *key){    const KeyDef *p;    for(p = key_defs; p->name != NULL; p++) {        if (!strcmp(key, p->name))            return p->keycode;    }    return -1;}static void do_send_key(const char *string){    char keybuf[16], *q;    uint8_t keycodes[16];    const char *p;    int nb_keycodes, keycode, i;        nb_keycodes = 0;    p = string;    while (*p != '\0') {        q = keybuf;        while (*p != '\0' && *p != '-') {            if ((q - keybuf) < sizeof(keybuf) - 1) {                *q++ = *p;            }            p++;        }        *q = '\0';        keycode = get_keycode(keybuf);        if (keycode < 0) {            term_printf("unknown key: '%s'\n", keybuf);            return;        }        keycodes[nb_keycodes++] = keycode;        if (*p == '\0')            break;        p++;    }    /* key down events */    for(i = 0; i < nb_keycodes; i++) {        keycode = keycodes[i];        if (keycode & 0x80)            kbd_put_keycode(0xe0);        kbd_put_keycode(keycode & 0x7f);    }    /* key up events */    for(i = nb_keycodes - 1; i >= 0; i--) {        keycode = keycodes[i];        if (keycode & 0x80)            kbd_put_keycode(0xe0);        kbd_put_keycode(keycode | 0x80);    }}static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index){    uint32_t val;    int suffix;    if (has_index) {        cpu_outb(NULL, addr & 0xffff, index & 0xff);        addr++;    }    addr &= 0xffff;    switch(size) {    default:    case 1:        val = cpu_inb(NULL, addr);        suffix = 'b';        break;    case 2:        val = cpu_inw(NULL, addr);        suffix = 'w';        break;    case 4:        val = cpu_inl(NULL, addr);        suffix = 'l';        break;    }    term_printf("port%c[0x%04x] = %#0*x\n",                suffix, addr, size * 2, val);}static void do_system_reset(void){    qemu_system_reset_request();}static void do_system_powerdown(void){    qemu_system_powerdown_request();}#if defined(TARGET_I386)static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask){    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",                 addr,                pte & mask,                pte & PG_GLOBAL_MASK ? 'G' : '-',                pte & PG_PSE_MASK ? 'P' : '-',                pte & PG_DIRTY_MASK ? 'D' : '-',                pte & PG_ACCESSED_MASK ? 'A' : '-',                pte & PG_PCD_MASK ? 'C' : '-',                pte & PG_PWT_MASK ? 'T' : '-',                pte & PG_USER_MASK ? 'U' : '-',                pte & PG_RW_MASK ? 'W' : '-');}static void tlb_info(void){    CPUState *env;    int l1, l2;    uint32_t pgd, pde, pte;    env = mon_get_cpu();    if (!env)        return;    if (!(env->cr[0] & CR0_PG_MASK)) {        term_printf("PG disabled\n");        return;    }    pgd = env->cr[3] & ~0xfff;    for(l1 = 0; l1 < 1024; l1++) {        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);        pde = le32_to_cpu(pde);        if (pde & PG_PRESENT_MASK) {            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {                print_pte((l1 << 22), pde, ~((1 << 20) - 1));            } else {                for(l2 = 0; l2 < 1024; l2++) {                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,                                              (uint8_t *)&pte, 4);                    pte = le32_to_cpu(pte);                    if (pte & PG_PRESENT_MASK) {                        print_pte((l1 << 22) + (l2 << 12),                                   pte & ~PG_PSE_MASK,                                   ~0xfff);                    }                }            }        }    }}static void mem_print(uint32_t *pstart, int *plast_prot,                       uint32_t end, int prot){    int prot1;    prot1 = *plast_prot;    if (prot != prot1) {        if (*pstart != -1) {            term_printf("%08x-%08x %08x %c%c%c\n",                        *pstart, end, end - *pstart,                         prot1 & PG_USER_MASK ? 'u' : '-',                        'r',                        prot1 & PG_RW_MASK ? 'w' : '-');        }        if (prot != 0)            *pstart = end;        else            *pstart = -1;        *plast_prot = prot;    }}static void mem_info(void){    CPUState *env;    int l1, l2, prot, last_prot;    uint32_t pgd, pde, pte, start, end;    env = mon_get_cpu();    if (!env)        return;    if (!(env->cr[0] & CR0_PG_MASK)) {        term_printf("PG disabled\n");        return;    }    pgd = env->cr[3] & ~0xfff;    last_prot = 0;    start = -1;    for(l1 = 0; l1 < 1024; l1++) {        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);        pde = le32_to_cpu(pde);        end = l1 << 22;        if (pde & PG_PRESENT_MASK) {            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);                mem_print(&start, &last_prot, end, prot);            } else {                for(l2 = 0; l2 < 1024; l2++) {                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,                                              (uint8_t *)&pte, 4);                    pte = le32_to_cpu(pte);                    end = (l1 << 22) + (l2 << 12);                    if (pte & PG_PRESENT_MASK) {                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);                    } else {                        prot = 0;                    }                    mem_print(&start, &last_prot, end, prot);                }            }        } else {            prot = 0;            mem_print(&start, &last_prot, end, prot);        }    }}#endifstatic void do_info_kqemu(void){#ifdef USE_KQEMU    CPUState *env;    int val;    val = 0;    env = mon_get_cpu();    if (!env) {        term_printf("No cpu initialized yet");        return;    }    val = env->kqemu_enabled;    term_printf("kqemu support: ");    switch(val) {    default:    case 0:        term_printf("disabled\n");        break;    case 1:        term_printf("enabled for user code\n");        break;    case 2:        term_printf("enabled for user and kernel code\n");        break;    }#else    term_printf("kqemu support: not compiled\n");#endif} #ifdef CONFIG_PROFILERint64_t kqemu_time;int64_t qemu_time;int64_t kqemu_exec_count;int64_t dev_time;int64_t kqemu_ret_int_count;int64_t kqemu_ret_excp_count;int64_t kqemu_ret_intr_count;static void do_info_profile(void){    int64_t total;    total = qemu_time;    if (total == 0)        total = 1;    term_printf("async time  %lld (%0.3f)\n",                dev_time, dev_time / (double)ticks_per_sec);    term_printf("qemu time   %lld (%0.3f)\n",                qemu_time, qemu_time / (double)ticks_per_sec);    term_printf("kqemu time  %lld (%0.3f %0.1f%%) count=%lld int=%lld excp=%lld intr=%lld\n",                kqemu_time, kqemu_time / (double)ticks_per_sec,                kqemu_time / (double)total * 100.0,                kqemu_exec_count,                kqemu_ret_int_count,                kqemu_ret_excp_count,                kqemu_ret_intr_count);    qemu_time = 0;    kqemu_time = 0;    kqemu_exec_count = 0;    dev_time = 0;    kqemu_ret_int_count = 0;    kqemu_ret_excp_count = 0;    kqemu_ret_intr_count = 0;#ifdef USE_KQEMU    kqemu_record_dump();#endif}#elsestatic void do_info_profile(void){    term_printf("Internal profiler not compiled\n");}#endifstatic term_cmd_t term_cmds[] = {    { "help|?", "s?", do_help,       "[cmd]", "show the help" },    { "commit", "", do_commit,       "", "commit changes to the disk images (if -snapshot is used)" },    { "info", "s?", do_info,      "subcommand", "show various information about the system state" },    { "q|quit", "", do_quit,      "", "quit the emulator" },    { "eject", "-fB", do_eject,      "[-f] device", "eject a removable media (use -f to force it)" },    { "change", "BF", do_change,      "device filename", "change a removable media" },    { "screendump", "F", do_screen_dump,       "filename", "save screen into PPM image 'filename'" },    { "log", "s", do_log,      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },     { "savevm", "F", do_savevm,      "filename", "save the whole virtual machine state to 'filename'" },     { "loadvm", "F", do_loadvm,      "filename", "restore the whole virtual machine state from 'filename'" },     { "stop", "", do_stop,       "", "stop emulation", },    { "c|cont", "", do_cont,       "", "resume emulation", },#ifdef CONFIG_GDBSTUB    { "gdbserver", "i?", do_gdbserver,       "[port]", "start gdbserver session (default port=1234)", },#endif    { "x", "/l", do_memory_dump,       "/fmt addr", "virtual memory dump starting at 'addr'", },    { "xp", "/l", do_physical_memory_dump,       "/fmt addr", "physical memory dump starting at 'addr'", },    { "p|print", "/l", do_print,       "/fmt expr", "print expression value (use $reg for CPU register access)", },    { "i", "/ii.", do_ioport_read,       "/fmt addr", "I/O port read" },    { "sendkey", "s", do_send_key,       "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },    { "system_reset", "", do_system_reset,       "", "reset the system" },    { "system_powerdown", "", do_system_powerdown,       "", "send system power down event" },    { "sum", "ii", do_sum,       "addr size", "compute the checksum of a memory region" },    { "usb_add", "s", do_usb_add,      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },    { "usb_del", "s", do_usb_del,      "device", "remove USB device 'bus.addr'" },    { "cpu", "i", do_cpu_set,       "index", "set the default CPU" },    { NULL, NULL, }, };static term_cmd_t info_cmds[] = {    { "version", "", do_info_version,      "", "show the version of qemu" },    { "network", "", do_info_network,      "", "show the network state" },    { "block", "", do_info_block,      "", "show the block devices" },    { "registers", "", do_info_registers,      "", "show the cpu registers" },    { "cpus", "", do_info_cpus,      "", "show infos for each CPU" },    { "history", "", do_info_history,      "", "show the command line history", },    { "irq", "", irq_info,      "", "show the interrupts statistics (if available)", },    { "pic", "", pic_info,      "", "show i8259 (PIC) state", },    { "pci", "", pci_info,      "", "show PCI info", },#if defined(TARGET_I386)    { "tlb", "", tlb_info,      "", "show virtual to physical memory mappings", },    { "mem", "", mem_info,      "", "show the active virtual memory mappings", },#endif    { "jit", "", do_info_jit,      "", "show dynamic compiler info", },    { "kqemu", "", do_info_kqemu,      "", "show kqemu information", },    { "usb", "", usb_info,      "", "show guest USB devices", },    { "usbhost", "", usb_host_info,      "", "show host USB devices", },    { "profile", "", do_info_profile,      "", "show profiling information", },    { NULL, NULL, },};/*******************************************************************/static const char *pch;static jmp_buf expr_env;#define MD_TLONG 0#define MD_I32   1typedef struct MonitorDef {    const char *name;    int offset;    target_long (*get_value)(struct MonitorDef *md, int val);    int type;} MonitorDef;#if defined(TARGET_I386)static target_long monitor_get_pc (struct MonitorDef *md, int val){    CPUState *env = mon_get_cpu();    if (!env)        return 0;

⌨️ 快捷键说明

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