File:  [Qemu by Fabrice Bellard] / qemu / vl.c
Revision 1.1.1.20 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 18:55:32 2018 UTC (3 years, 1 month ago) by root
Branches: qemu, MAIN
CVS tags: qemu1000, qemu0151, HEAD
qemu 0.15.1

    1: /*
    2:  * QEMU System Emulator
    3:  *
    4:  * Copyright (c) 2003-2008 Fabrice Bellard
    5:  *
    6:  * Permission is hereby granted, free of charge, to any person obtaining a copy
    7:  * of this software and associated documentation files (the "Software"), to deal
    8:  * in the Software without restriction, including without limitation the rights
    9:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   10:  * copies of the Software, and to permit persons to whom the Software is
   11:  * furnished to do so, subject to the following conditions:
   12:  *
   13:  * The above copyright notice and this permission notice shall be included in
   14:  * all copies or substantial portions of the Software.
   15:  *
   16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   17:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   21:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   22:  * THE SOFTWARE.
   23:  */
   24: #include <unistd.h>
   25: #include <fcntl.h>
   26: #include <signal.h>
   27: #include <time.h>
   28: #include <errno.h>
   29: #include <sys/time.h>
   30: #include <zlib.h>
   31: 
   32: /* Needed early for CONFIG_BSD etc. */
   33: #include "config-host.h"
   34: 
   35: #ifndef _WIN32
   36: #include <libgen.h>
   37: #include <sys/times.h>
   38: #include <sys/wait.h>
   39: #include <termios.h>
   40: #include <sys/mman.h>
   41: #include <sys/ioctl.h>
   42: #include <sys/resource.h>
   43: #include <sys/socket.h>
   44: #include <netinet/in.h>
   45: #include <net/if.h>
   46: #include <arpa/inet.h>
   47: #include <dirent.h>
   48: #include <netdb.h>
   49: #include <sys/select.h>
   50: 
   51: #ifdef CONFIG_BSD
   52: #include <sys/stat.h>
   53: #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
   54: #include <libutil.h>
   55: #include <sys/sysctl.h>
   56: #else
   57: #include <util.h>
   58: #endif
   59: #else
   60: #ifdef __linux__
   61: #include <pty.h>
   62: #include <malloc.h>
   63: 
   64: #include <linux/ppdev.h>
   65: #include <linux/parport.h>
   66: #endif
   67: #ifdef __sun__
   68: #include <sys/stat.h>
   69: #include <sys/ethernet.h>
   70: #include <sys/sockio.h>
   71: #include <netinet/arp.h>
   72: #include <netinet/in_systm.h>
   73: #include <netinet/ip.h>
   74: #include <netinet/ip_icmp.h> // must come after ip.h
   75: #include <netinet/udp.h>
   76: #include <netinet/tcp.h>
   77: #include <net/if.h>
   78: #include <syslog.h>
   79: #include <stropts.h>
   80: #endif
   81: #endif
   82: #endif
   83: 
   84: #if defined(__OpenBSD__)
   85: #include <util.h>
   86: #endif
   87: 
   88: #if defined(CONFIG_VDE)
   89: #include <libvdeplug.h>
   90: #endif
   91: 
   92: #ifdef _WIN32
   93: #include <windows.h>
   94: #endif
   95: 
   96: #ifdef CONFIG_SDL
   97: #if defined(__APPLE__) || defined(main)
   98: #include <SDL.h>
   99: int qemu_main(int argc, char **argv, char **envp);
  100: int main(int argc, char **argv)
  101: {
  102:     return qemu_main(argc, argv, NULL);
  103: }
  104: #undef main
  105: #define main qemu_main
  106: #endif
  107: #endif /* CONFIG_SDL */
  108: 
  109: #ifdef CONFIG_COCOA
  110: #undef main
  111: #define main qemu_main
  112: #endif /* CONFIG_COCOA */
  113: 
  114: #include "hw/hw.h"
  115: #include "hw/boards.h"
  116: #include "hw/usb.h"
  117: #include "hw/pcmcia.h"
  118: #include "hw/pc.h"
  119: #include "hw/isa.h"
  120: #include "hw/baum.h"
  121: #include "hw/bt.h"
  122: #include "hw/watchdog.h"
  123: #include "hw/smbios.h"
  124: #include "hw/xen.h"
  125: #include "hw/qdev.h"
  126: #include "hw/loader.h"
  127: #include "bt-host.h"
  128: #include "net.h"
  129: #include "net/slirp.h"
  130: #include "monitor.h"
  131: #include "console.h"
  132: #include "sysemu.h"
  133: #include "gdbstub.h"
  134: #include "qemu-timer.h"
  135: #include "qemu-char.h"
  136: #include "cache-utils.h"
  137: #include "block.h"
  138: #include "blockdev.h"
  139: #include "block-migration.h"
  140: #include "dma.h"
  141: #include "audio/audio.h"
  142: #include "migration.h"
  143: #include "kvm.h"
  144: #include "qemu-option.h"
  145: #include "qemu-config.h"
  146: #include "qemu-objects.h"
  147: #include "qemu-options.h"
  148: #ifdef CONFIG_VIRTFS
  149: #include "fsdev/qemu-fsdev.h"
  150: #endif
  151: 
  152: #include "disas.h"
  153: 
  154: #include "qemu_socket.h"
  155: 
  156: #include "slirp/libslirp.h"
  157: 
  158: #include "trace.h"
  159: #include "simpletrace.h"
  160: #include "qemu-queue.h"
  161: #include "cpus.h"
  162: #include "arch_init.h"
  163: 
  164: #include "ui/qemu-spice.h"
  165: 
  166: //#define DEBUG_NET
  167: //#define DEBUG_SLIRP
  168: 
  169: #define DEFAULT_RAM_SIZE 128
  170: 
  171: #define MAX_VIRTIO_CONSOLES 1
  172: 
  173: static const char *data_dir;
  174: const char *bios_name = NULL;
  175: enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
  176: DisplayType display_type = DT_DEFAULT;
  177: int display_remote = 0;
  178: const char* keyboard_layout = NULL;
  179: ram_addr_t ram_size;
  180: const char *mem_path = NULL;
  181: #ifdef MAP_POPULATE
  182: int mem_prealloc = 0; /* force preallocation of physical target memory */
  183: #endif
  184: int nb_nics;
  185: NICInfo nd_table[MAX_NICS];
  186: int vm_running;
  187: int autostart;
  188: int incoming_expected; /* Started with -incoming and waiting for incoming */
  189: static int rtc_utc = 1;
  190: static int rtc_date_offset = -1; /* -1 means no change */
  191: QEMUClock *rtc_clock;
  192: int vga_interface_type = VGA_NONE;
  193: static int full_screen = 0;
  194: #ifdef CONFIG_SDL
  195: static int no_frame = 0;
  196: #endif
  197: int no_quit = 0;
  198: CharDriverState *serial_hds[MAX_SERIAL_PORTS];
  199: CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
  200: CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
  201: int win2k_install_hack = 0;
  202: int rtc_td_hack = 0;
  203: int usb_enabled = 0;
  204: int singlestep = 0;
  205: int smp_cpus = 1;
  206: int max_cpus = 0;
  207: int smp_cores = 1;
  208: int smp_threads = 1;
  209: #ifdef CONFIG_VNC
  210: const char *vnc_display;
  211: #endif
  212: int acpi_enabled = 1;
  213: int no_hpet = 0;
  214: int fd_bootchk = 1;
  215: int no_reboot = 0;
  216: int no_shutdown = 0;
  217: int cursor_hide = 1;
  218: int graphic_rotate = 0;
  219: uint8_t irq0override = 1;
  220: const char *watchdog;
  221: QEMUOptionRom option_rom[MAX_OPTION_ROMS];
  222: int nb_option_roms;
  223: int semihosting_enabled = 0;
  224: int old_param = 0;
  225: const char *qemu_name;
  226: int alt_grab = 0;
  227: int ctrl_grab = 0;
  228: unsigned int nb_prom_envs = 0;
  229: const char *prom_envs[MAX_PROM_ENVS];
  230: int boot_menu;
  231: 
  232: typedef struct FWBootEntry FWBootEntry;
  233: 
  234: struct FWBootEntry {
  235:     QTAILQ_ENTRY(FWBootEntry) link;
  236:     int32_t bootindex;
  237:     DeviceState *dev;
  238:     char *suffix;
  239: };
  240: 
  241: QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
  242: 
  243: int nb_numa_nodes;
  244: uint64_t node_mem[MAX_NODES];
  245: uint64_t node_cpumask[MAX_NODES];
  246: 
  247: static QEMUTimer *nographic_timer;
  248: 
  249: uint8_t qemu_uuid[16];
  250: 
  251: static QEMUBootSetHandler *boot_set_handler;
  252: static void *boot_set_opaque;
  253: 
  254: static NotifierList exit_notifiers =
  255:     NOTIFIER_LIST_INITIALIZER(exit_notifiers);
  256: 
  257: static NotifierList machine_init_done_notifiers =
  258:     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
  259: 
  260: static int tcg_allowed = 1;
  261: int kvm_allowed = 0;
  262: int xen_allowed = 0;
  263: uint32_t xen_domid;
  264: enum xen_mode xen_mode = XEN_EMULATE;
  265: 
  266: static int default_serial = 1;
  267: static int default_parallel = 1;
  268: static int default_virtcon = 1;
  269: static int default_monitor = 1;
  270: static int default_vga = 1;
  271: static int default_floppy = 1;
  272: static int default_cdrom = 1;
  273: static int default_sdcard = 1;
  274: 
  275: static struct {
  276:     const char *driver;
  277:     int *flag;
  278: } default_list[] = {
  279:     { .driver = "isa-serial",           .flag = &default_serial    },
  280:     { .driver = "isa-parallel",         .flag = &default_parallel  },
  281:     { .driver = "isa-fdc",              .flag = &default_floppy    },
  282:     { .driver = "ide-cd",               .flag = &default_cdrom     },
  283:     { .driver = "ide-hd",               .flag = &default_cdrom     },
  284:     { .driver = "ide-drive",            .flag = &default_cdrom     },
  285:     { .driver = "scsi-cd",              .flag = &default_cdrom     },
  286:     { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
  287:     { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
  288:     { .driver = "virtio-serial",        .flag = &default_virtcon   },
  289:     { .driver = "VGA",                  .flag = &default_vga       },
  290:     { .driver = "cirrus-vga",           .flag = &default_vga       },
  291:     { .driver = "vmware-svga",          .flag = &default_vga       },
  292:     { .driver = "isa-vga",              .flag = &default_vga       },
  293:     { .driver = "qxl-vga",              .flag = &default_vga       },
  294: };
  295: 
  296: static int default_driver_check(QemuOpts *opts, void *opaque)
  297: {
  298:     const char *driver = qemu_opt_get(opts, "driver");
  299:     int i;
  300: 
  301:     if (!driver)
  302:         return 0;
  303:     for (i = 0; i < ARRAY_SIZE(default_list); i++) {
  304:         if (strcmp(default_list[i].driver, driver) != 0)
  305:             continue;
  306:         *(default_list[i].flag) = 0;
  307:     }
  308:     return 0;
  309: }
  310: 
  311: /***********************************************************/
  312: /* real time host monotonic timer */
  313: 
  314: /***********************************************************/
  315: /* host time/date access */
  316: void qemu_get_timedate(struct tm *tm, int offset)
  317: {
  318:     time_t ti;
  319:     struct tm *ret;
  320: 
  321:     time(&ti);
  322:     ti += offset;
  323:     if (rtc_date_offset == -1) {
  324:         if (rtc_utc)
  325:             ret = gmtime(&ti);
  326:         else
  327:             ret = localtime(&ti);
  328:     } else {
  329:         ti -= rtc_date_offset;
  330:         ret = gmtime(&ti);
  331:     }
  332: 
  333:     memcpy(tm, ret, sizeof(struct tm));
  334: }
  335: 
  336: int qemu_timedate_diff(struct tm *tm)
  337: {
  338:     time_t seconds;
  339: 
  340:     if (rtc_date_offset == -1)
  341:         if (rtc_utc)
  342:             seconds = mktimegm(tm);
  343:         else
  344:             seconds = mktime(tm);
  345:     else
  346:         seconds = mktimegm(tm) + rtc_date_offset;
  347: 
  348:     return seconds - time(NULL);
  349: }
  350: 
  351: void rtc_change_mon_event(struct tm *tm)
  352: {
  353:     QObject *data;
  354: 
  355:     data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
  356:     monitor_protocol_event(QEVENT_RTC_CHANGE, data);
  357:     qobject_decref(data);
  358: }
  359: 
  360: static void configure_rtc_date_offset(const char *startdate, int legacy)
  361: {
  362:     time_t rtc_start_date;
  363:     struct tm tm;
  364: 
  365:     if (!strcmp(startdate, "now") && legacy) {
  366:         rtc_date_offset = -1;
  367:     } else {
  368:         if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
  369:                    &tm.tm_year,
  370:                    &tm.tm_mon,
  371:                    &tm.tm_mday,
  372:                    &tm.tm_hour,
  373:                    &tm.tm_min,
  374:                    &tm.tm_sec) == 6) {
  375:             /* OK */
  376:         } else if (sscanf(startdate, "%d-%d-%d",
  377:                           &tm.tm_year,
  378:                           &tm.tm_mon,
  379:                           &tm.tm_mday) == 3) {
  380:             tm.tm_hour = 0;
  381:             tm.tm_min = 0;
  382:             tm.tm_sec = 0;
  383:         } else {
  384:             goto date_fail;
  385:         }
  386:         tm.tm_year -= 1900;
  387:         tm.tm_mon--;
  388:         rtc_start_date = mktimegm(&tm);
  389:         if (rtc_start_date == -1) {
  390:         date_fail:
  391:             fprintf(stderr, "Invalid date format. Valid formats are:\n"
  392:                             "'2006-06-17T16:01:21' or '2006-06-17'\n");
  393:             exit(1);
  394:         }
  395:         rtc_date_offset = time(NULL) - rtc_start_date;
  396:     }
  397: }
  398: 
  399: static void configure_rtc(QemuOpts *opts)
  400: {
  401:     const char *value;
  402: 
  403:     value = qemu_opt_get(opts, "base");
  404:     if (value) {
  405:         if (!strcmp(value, "utc")) {
  406:             rtc_utc = 1;
  407:         } else if (!strcmp(value, "localtime")) {
  408:             rtc_utc = 0;
  409:         } else {
  410:             configure_rtc_date_offset(value, 0);
  411:         }
  412:     }
  413:     value = qemu_opt_get(opts, "clock");
  414:     if (value) {
  415:         if (!strcmp(value, "host")) {
  416:             rtc_clock = host_clock;
  417:         } else if (!strcmp(value, "vm")) {
  418:             rtc_clock = vm_clock;
  419:         } else {
  420:             fprintf(stderr, "qemu: invalid option value '%s'\n", value);
  421:             exit(1);
  422:         }
  423:     }
  424:     value = qemu_opt_get(opts, "driftfix");
  425:     if (value) {
  426:         if (!strcmp(value, "slew")) {
  427:             rtc_td_hack = 1;
  428:         } else if (!strcmp(value, "none")) {
  429:             rtc_td_hack = 0;
  430:         } else {
  431:             fprintf(stderr, "qemu: invalid option value '%s'\n", value);
  432:             exit(1);
  433:         }
  434:     }
  435: }
  436: 
  437: /***********************************************************/
  438: /* Bluetooth support */
  439: static int nb_hcis;
  440: static int cur_hci;
  441: static struct HCIInfo *hci_table[MAX_NICS];
  442: 
  443: static struct bt_vlan_s {
  444:     struct bt_scatternet_s net;
  445:     int id;
  446:     struct bt_vlan_s *next;
  447: } *first_bt_vlan;
  448: 
  449: /* find or alloc a new bluetooth "VLAN" */
  450: static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
  451: {
  452:     struct bt_vlan_s **pvlan, *vlan;
  453:     for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
  454:         if (vlan->id == id)
  455:             return &vlan->net;
  456:     }
  457:     vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
  458:     vlan->id = id;
  459:     pvlan = &first_bt_vlan;
  460:     while (*pvlan != NULL)
  461:         pvlan = &(*pvlan)->next;
  462:     *pvlan = vlan;
  463:     return &vlan->net;
  464: }
  465: 
  466: static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
  467: {
  468: }
  469: 
  470: static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
  471: {
  472:     return -ENOTSUP;
  473: }
  474: 
  475: static struct HCIInfo null_hci = {
  476:     .cmd_send = null_hci_send,
  477:     .sco_send = null_hci_send,
  478:     .acl_send = null_hci_send,
  479:     .bdaddr_set = null_hci_addr_set,
  480: };
  481: 
  482: struct HCIInfo *qemu_next_hci(void)
  483: {
  484:     if (cur_hci == nb_hcis)
  485:         return &null_hci;
  486: 
  487:     return hci_table[cur_hci++];
  488: }
  489: 
  490: static struct HCIInfo *hci_init(const char *str)
  491: {
  492:     char *endp;
  493:     struct bt_scatternet_s *vlan = 0;
  494: 
  495:     if (!strcmp(str, "null"))
  496:         /* null */
  497:         return &null_hci;
  498:     else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
  499:         /* host[:hciN] */
  500:         return bt_host_hci(str[4] ? str + 5 : "hci0");
  501:     else if (!strncmp(str, "hci", 3)) {
  502:         /* hci[,vlan=n] */
  503:         if (str[3]) {
  504:             if (!strncmp(str + 3, ",vlan=", 6)) {
  505:                 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
  506:                 if (*endp)
  507:                     vlan = 0;
  508:             }
  509:         } else
  510:             vlan = qemu_find_bt_vlan(0);
  511:         if (vlan)
  512:            return bt_new_hci(vlan);
  513:     }
  514: 
  515:     fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
  516: 
  517:     return 0;
  518: }
  519: 
  520: static int bt_hci_parse(const char *str)
  521: {
  522:     struct HCIInfo *hci;
  523:     bdaddr_t bdaddr;
  524: 
  525:     if (nb_hcis >= MAX_NICS) {
  526:         fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
  527:         return -1;
  528:     }
  529: 
  530:     hci = hci_init(str);
  531:     if (!hci)
  532:         return -1;
  533: 
  534:     bdaddr.b[0] = 0x52;
  535:     bdaddr.b[1] = 0x54;
  536:     bdaddr.b[2] = 0x00;
  537:     bdaddr.b[3] = 0x12;
  538:     bdaddr.b[4] = 0x34;
  539:     bdaddr.b[5] = 0x56 + nb_hcis;
  540:     hci->bdaddr_set(hci, bdaddr.b);
  541: 
  542:     hci_table[nb_hcis++] = hci;
  543: 
  544:     return 0;
  545: }
  546: 
  547: static void bt_vhci_add(int vlan_id)
  548: {
  549:     struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
  550: 
  551:     if (!vlan->slave)
  552:         fprintf(stderr, "qemu: warning: adding a VHCI to "
  553:                         "an empty scatternet %i\n", vlan_id);
  554: 
  555:     bt_vhci_init(bt_new_hci(vlan));
  556: }
  557: 
  558: static struct bt_device_s *bt_device_add(const char *opt)
  559: {
  560:     struct bt_scatternet_s *vlan;
  561:     int vlan_id = 0;
  562:     char *endp = strstr(opt, ",vlan=");
  563:     int len = (endp ? endp - opt : strlen(opt)) + 1;
  564:     char devname[10];
  565: 
  566:     pstrcpy(devname, MIN(sizeof(devname), len), opt);
  567: 
  568:     if (endp) {
  569:         vlan_id = strtol(endp + 6, &endp, 0);
  570:         if (*endp) {
  571:             fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
  572:             return 0;
  573:         }
  574:     }
  575: 
  576:     vlan = qemu_find_bt_vlan(vlan_id);
  577: 
  578:     if (!vlan->slave)
  579:         fprintf(stderr, "qemu: warning: adding a slave device to "
  580:                         "an empty scatternet %i\n", vlan_id);
  581: 
  582:     if (!strcmp(devname, "keyboard"))
  583:         return bt_keyboard_init(vlan);
  584: 
  585:     fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
  586:     return 0;
  587: }
  588: 
  589: static int bt_parse(const char *opt)
  590: {
  591:     const char *endp, *p;
  592:     int vlan;
  593: 
  594:     if (strstart(opt, "hci", &endp)) {
  595:         if (!*endp || *endp == ',') {
  596:             if (*endp)
  597:                 if (!strstart(endp, ",vlan=", 0))
  598:                     opt = endp + 1;
  599: 
  600:             return bt_hci_parse(opt);
  601:        }
  602:     } else if (strstart(opt, "vhci", &endp)) {
  603:         if (!*endp || *endp == ',') {
  604:             if (*endp) {
  605:                 if (strstart(endp, ",vlan=", &p)) {
  606:                     vlan = strtol(p, (char **) &endp, 0);
  607:                     if (*endp) {
  608:                         fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
  609:                         return 1;
  610:                     }
  611:                 } else {
  612:                     fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
  613:                     return 1;
  614:                 }
  615:             } else
  616:                 vlan = 0;
  617: 
  618:             bt_vhci_add(vlan);
  619:             return 0;
  620:         }
  621:     } else if (strstart(opt, "device:", &endp))
  622:         return !bt_device_add(endp);
  623: 
  624:     fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
  625:     return 1;
  626: }
  627: 
  628: /***********************************************************/
  629: /* QEMU Block devices */
  630: 
  631: #define HD_OPTS "media=disk"
  632: #define CDROM_OPTS "media=cdrom"
  633: #define FD_OPTS ""
  634: #define PFLASH_OPTS ""
  635: #define MTD_OPTS ""
  636: #define SD_OPTS ""
  637: 
  638: static int drive_init_func(QemuOpts *opts, void *opaque)
  639: {
  640:     int *use_scsi = opaque;
  641: 
  642:     return drive_init(opts, *use_scsi) == NULL;
  643: }
  644: 
  645: static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
  646: {
  647:     if (NULL == qemu_opt_get(opts, "snapshot")) {
  648:         qemu_opt_set(opts, "snapshot", "on");
  649:     }
  650:     return 0;
  651: }
  652: 
  653: static void default_drive(int enable, int snapshot, int use_scsi,
  654:                           BlockInterfaceType type, int index,
  655:                           const char *optstr)
  656: {
  657:     QemuOpts *opts;
  658: 
  659:     if (type == IF_DEFAULT) {
  660:         type = use_scsi ? IF_SCSI : IF_IDE;
  661:     }
  662: 
  663:     if (!enable || drive_get_by_index(type, index)) {
  664:         return;
  665:     }
  666: 
  667:     opts = drive_add(type, index, NULL, optstr);
  668:     if (snapshot) {
  669:         drive_enable_snapshot(opts, NULL);
  670:     }
  671:     if (!drive_init(opts, use_scsi)) {
  672:         exit(1);
  673:     }
  674: }
  675: 
  676: void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
  677: {
  678:     boot_set_handler = func;
  679:     boot_set_opaque = opaque;
  680: }
  681: 
  682: int qemu_boot_set(const char *boot_devices)
  683: {
  684:     if (!boot_set_handler) {
  685:         return -EINVAL;
  686:     }
  687:     return boot_set_handler(boot_set_opaque, boot_devices);
  688: }
  689: 
  690: static void validate_bootdevices(char *devices)
  691: {
  692:     /* We just do some generic consistency checks */
  693:     const char *p;
  694:     int bitmap = 0;
  695: 
  696:     for (p = devices; *p != '\0'; p++) {
  697:         /* Allowed boot devices are:
  698:          * a-b: floppy disk drives
  699:          * c-f: IDE disk drives
  700:          * g-m: machine implementation dependant drives
  701:          * n-p: network devices
  702:          * It's up to each machine implementation to check if the given boot
  703:          * devices match the actual hardware implementation and firmware
  704:          * features.
  705:          */
  706:         if (*p < 'a' || *p > 'p') {
  707:             fprintf(stderr, "Invalid boot device '%c'\n", *p);
  708:             exit(1);
  709:         }
  710:         if (bitmap & (1 << (*p - 'a'))) {
  711:             fprintf(stderr, "Boot device '%c' was given twice\n", *p);
  712:             exit(1);
  713:         }
  714:         bitmap |= 1 << (*p - 'a');
  715:     }
  716: }
  717: 
  718: static void restore_boot_devices(void *opaque)
  719: {
  720:     char *standard_boot_devices = opaque;
  721:     static int first = 1;
  722: 
  723:     /* Restore boot order and remove ourselves after the first boot */
  724:     if (first) {
  725:         first = 0;
  726:         return;
  727:     }
  728: 
  729:     qemu_boot_set(standard_boot_devices);
  730: 
  731:     qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
  732:     qemu_free(standard_boot_devices);
  733: }
  734: 
  735: void add_boot_device_path(int32_t bootindex, DeviceState *dev,
  736:                           const char *suffix)
  737: {
  738:     FWBootEntry *node, *i;
  739: 
  740:     if (bootindex < 0) {
  741:         return;
  742:     }
  743: 
  744:     assert(dev != NULL || suffix != NULL);
  745: 
  746:     node = qemu_mallocz(sizeof(FWBootEntry));
  747:     node->bootindex = bootindex;
  748:     node->suffix = suffix ? qemu_strdup(suffix) : NULL;
  749:     node->dev = dev;
  750: 
  751:     QTAILQ_FOREACH(i, &fw_boot_order, link) {
  752:         if (i->bootindex == bootindex) {
  753:             fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
  754:             exit(1);
  755:         } else if (i->bootindex < bootindex) {
  756:             continue;
  757:         }
  758:         QTAILQ_INSERT_BEFORE(i, node, link);
  759:         return;
  760:     }
  761:     QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
  762: }
  763: 
  764: /*
  765:  * This function returns null terminated string that consist of new line
  766:  * separated device paths.
  767:  *
  768:  * memory pointed by "size" is assigned total length of the array in bytes
  769:  *
  770:  */
  771: char *get_boot_devices_list(uint32_t *size)
  772: {
  773:     FWBootEntry *i;
  774:     uint32_t total = 0;
  775:     char *list = NULL;
  776: 
  777:     QTAILQ_FOREACH(i, &fw_boot_order, link) {
  778:         char *devpath = NULL, *bootpath;
  779:         int len;
  780: 
  781:         if (i->dev) {
  782:             devpath = qdev_get_fw_dev_path(i->dev);
  783:             assert(devpath);
  784:         }
  785: 
  786:         if (i->suffix && devpath) {
  787:             size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
  788: 
  789:             bootpath = qemu_malloc(bootpathlen);
  790:             snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
  791:             qemu_free(devpath);
  792:         } else if (devpath) {
  793:             bootpath = devpath;
  794:         } else {
  795:             bootpath = qemu_strdup(i->suffix);
  796:             assert(bootpath);
  797:         }
  798: 
  799:         if (total) {
  800:             list[total-1] = '\n';
  801:         }
  802:         len = strlen(bootpath) + 1;
  803:         list = qemu_realloc(list, total + len);
  804:         memcpy(&list[total], bootpath, len);
  805:         total += len;
  806:         qemu_free(bootpath);
  807:     }
  808: 
  809:     *size = total;
  810: 
  811:     return list;
  812: }
  813: 
  814: static void numa_add(const char *optarg)
  815: {
  816:     char option[128];
  817:     char *endptr;
  818:     unsigned long long value, endvalue;
  819:     int nodenr;
  820: 
  821:     optarg = get_opt_name(option, 128, optarg, ',') + 1;
  822:     if (!strcmp(option, "node")) {
  823:         if (get_param_value(option, 128, "nodeid", optarg) == 0) {
  824:             nodenr = nb_numa_nodes;
  825:         } else {
  826:             nodenr = strtoull(option, NULL, 10);
  827:         }
  828: 
  829:         if (get_param_value(option, 128, "mem", optarg) == 0) {
  830:             node_mem[nodenr] = 0;
  831:         } else {
  832:             int64_t sval;
  833:             sval = strtosz(option, NULL);
  834:             if (sval < 0) {
  835:                 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
  836:                 exit(1);
  837:             }
  838:             node_mem[nodenr] = sval;
  839:         }
  840:         if (get_param_value(option, 128, "cpus", optarg) == 0) {
  841:             node_cpumask[nodenr] = 0;
  842:         } else {
  843:             value = strtoull(option, &endptr, 10);
  844:             if (value >= 64) {
  845:                 value = 63;
  846:                 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
  847:             } else {
  848:                 if (*endptr == '-') {
  849:                     endvalue = strtoull(endptr+1, &endptr, 10);
  850:                     if (endvalue >= 63) {
  851:                         endvalue = 62;
  852:                         fprintf(stderr,
  853:                             "only 63 CPUs in NUMA mode supported.\n");
  854:                     }
  855:                     value = (2ULL << endvalue) - (1ULL << value);
  856:                 } else {
  857:                     value = 1ULL << value;
  858:                 }
  859:             }
  860:             node_cpumask[nodenr] = value;
  861:         }
  862:         nb_numa_nodes++;
  863:     }
  864:     return;
  865: }
  866: 
  867: static void smp_parse(const char *optarg)
  868: {
  869:     int smp, sockets = 0, threads = 0, cores = 0;
  870:     char *endptr;
  871:     char option[128];
  872: 
  873:     smp = strtoul(optarg, &endptr, 10);
  874:     if (endptr != optarg) {
  875:         if (*endptr == ',') {
  876:             endptr++;
  877:         }
  878:     }
  879:     if (get_param_value(option, 128, "sockets", endptr) != 0)
  880:         sockets = strtoull(option, NULL, 10);
  881:     if (get_param_value(option, 128, "cores", endptr) != 0)
  882:         cores = strtoull(option, NULL, 10);
  883:     if (get_param_value(option, 128, "threads", endptr) != 0)
  884:         threads = strtoull(option, NULL, 10);
  885:     if (get_param_value(option, 128, "maxcpus", endptr) != 0)
  886:         max_cpus = strtoull(option, NULL, 10);
  887: 
  888:     /* compute missing values, prefer sockets over cores over threads */
  889:     if (smp == 0 || sockets == 0) {
  890:         sockets = sockets > 0 ? sockets : 1;
  891:         cores = cores > 0 ? cores : 1;
  892:         threads = threads > 0 ? threads : 1;
  893:         if (smp == 0) {
  894:             smp = cores * threads * sockets;
  895:         }
  896:     } else {
  897:         if (cores == 0) {
  898:             threads = threads > 0 ? threads : 1;
  899:             cores = smp / (sockets * threads);
  900:         } else {
  901:             threads = smp / (cores * sockets);
  902:         }
  903:     }
  904:     smp_cpus = smp;
  905:     smp_cores = cores > 0 ? cores : 1;
  906:     smp_threads = threads > 0 ? threads : 1;
  907:     if (max_cpus == 0)
  908:         max_cpus = smp_cpus;
  909: }
  910: 
  911: /***********************************************************/
  912: /* USB devices */
  913: 
  914: static int usb_device_add(const char *devname)
  915: {
  916:     const char *p;
  917:     USBDevice *dev = NULL;
  918: 
  919:     if (!usb_enabled)
  920:         return -1;
  921: 
  922:     /* drivers with .usbdevice_name entry in USBDeviceInfo */
  923:     dev = usbdevice_create(devname);
  924:     if (dev)
  925:         goto done;
  926: 
  927:     /* the other ones */
  928: #ifndef CONFIG_LINUX
  929:     /* only the linux version is qdev-ified, usb-bsd still needs this */
  930:     if (strstart(devname, "host:", &p)) {
  931:         dev = usb_host_device_open(p);
  932:     } else
  933: #endif
  934:     if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
  935:         dev = usb_bt_init(devname[2] ? hci_init(p) :
  936:                         bt_new_hci(qemu_find_bt_vlan(0)));
  937:     } else {
  938:         return -1;
  939:     }
  940:     if (!dev)
  941:         return -1;
  942: 
  943: done:
  944:     return 0;
  945: }
  946: 
  947: static int usb_device_del(const char *devname)
  948: {
  949:     int bus_num, addr;
  950:     const char *p;
  951: 
  952:     if (strstart(devname, "host:", &p))
  953:         return usb_host_device_close(p);
  954: 
  955:     if (!usb_enabled)
  956:         return -1;
  957: 
  958:     p = strchr(devname, '.');
  959:     if (!p)
  960:         return -1;
  961:     bus_num = strtoul(devname, NULL, 0);
  962:     addr = strtoul(p + 1, NULL, 0);
  963: 
  964:     return usb_device_delete_addr(bus_num, addr);
  965: }
  966: 
  967: static int usb_parse(const char *cmdline)
  968: {
  969:     int r;
  970:     r = usb_device_add(cmdline);
  971:     if (r < 0) {
  972:         fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
  973:     }
  974:     return r;
  975: }
  976: 
  977: void do_usb_add(Monitor *mon, const QDict *qdict)
  978: {
  979:     const char *devname = qdict_get_str(qdict, "devname");
  980:     if (usb_device_add(devname) < 0) {
  981:         error_report("could not add USB device '%s'", devname);
  982:     }
  983: }
  984: 
  985: void do_usb_del(Monitor *mon, const QDict *qdict)
  986: {
  987:     const char *devname = qdict_get_str(qdict, "devname");
  988:     if (usb_device_del(devname) < 0) {
  989:         error_report("could not delete USB device '%s'", devname);
  990:     }
  991: }
  992: 
  993: /***********************************************************/
  994: /* PCMCIA/Cardbus */
  995: 
  996: static struct pcmcia_socket_entry_s {
  997:     PCMCIASocket *socket;
  998:     struct pcmcia_socket_entry_s *next;
  999: } *pcmcia_sockets = 0;
 1000: 
 1001: void pcmcia_socket_register(PCMCIASocket *socket)
 1002: {
 1003:     struct pcmcia_socket_entry_s *entry;
 1004: 
 1005:     entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
 1006:     entry->socket = socket;
 1007:     entry->next = pcmcia_sockets;
 1008:     pcmcia_sockets = entry;
 1009: }
 1010: 
 1011: void pcmcia_socket_unregister(PCMCIASocket *socket)
 1012: {
 1013:     struct pcmcia_socket_entry_s *entry, **ptr;
 1014: 
 1015:     ptr = &pcmcia_sockets;
 1016:     for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
 1017:         if (entry->socket == socket) {
 1018:             *ptr = entry->next;
 1019:             qemu_free(entry);
 1020:         }
 1021: }
 1022: 
 1023: void pcmcia_info(Monitor *mon)
 1024: {
 1025:     struct pcmcia_socket_entry_s *iter;
 1026: 
 1027:     if (!pcmcia_sockets)
 1028:         monitor_printf(mon, "No PCMCIA sockets\n");
 1029: 
 1030:     for (iter = pcmcia_sockets; iter; iter = iter->next)
 1031:         monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
 1032:                        iter->socket->attached ? iter->socket->card_string :
 1033:                        "Empty");
 1034: }
 1035: 
 1036: /***********************************************************/
 1037: /* machine registration */
 1038: 
 1039: static QEMUMachine *first_machine = NULL;
 1040: QEMUMachine *current_machine = NULL;
 1041: 
 1042: int qemu_register_machine(QEMUMachine *m)
 1043: {
 1044:     QEMUMachine **pm;
 1045:     pm = &first_machine;
 1046:     while (*pm != NULL)
 1047:         pm = &(*pm)->next;
 1048:     m->next = NULL;
 1049:     *pm = m;
 1050:     return 0;
 1051: }
 1052: 
 1053: static QEMUMachine *find_machine(const char *name)
 1054: {
 1055:     QEMUMachine *m;
 1056: 
 1057:     for(m = first_machine; m != NULL; m = m->next) {
 1058:         if (!strcmp(m->name, name))
 1059:             return m;
 1060:         if (m->alias && !strcmp(m->alias, name))
 1061:             return m;
 1062:     }
 1063:     return NULL;
 1064: }
 1065: 
 1066: static QEMUMachine *find_default_machine(void)
 1067: {
 1068:     QEMUMachine *m;
 1069: 
 1070:     for(m = first_machine; m != NULL; m = m->next) {
 1071:         if (m->is_default) {
 1072:             return m;
 1073:         }
 1074:     }
 1075:     return NULL;
 1076: }
 1077: 
 1078: /***********************************************************/
 1079: /* main execution loop */
 1080: 
 1081: static void gui_update(void *opaque)
 1082: {
 1083:     uint64_t interval = GUI_REFRESH_INTERVAL;
 1084:     DisplayState *ds = opaque;
 1085:     DisplayChangeListener *dcl = ds->listeners;
 1086: 
 1087:     qemu_flush_coalesced_mmio_buffer();
 1088:     dpy_refresh(ds);
 1089: 
 1090:     while (dcl != NULL) {
 1091:         if (dcl->gui_timer_interval &&
 1092:             dcl->gui_timer_interval < interval)
 1093:             interval = dcl->gui_timer_interval;
 1094:         dcl = dcl->next;
 1095:     }
 1096:     qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
 1097: }
 1098: 
 1099: static void nographic_update(void *opaque)
 1100: {
 1101:     uint64_t interval = GUI_REFRESH_INTERVAL;
 1102: 
 1103:     qemu_flush_coalesced_mmio_buffer();
 1104:     qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
 1105: }
 1106: 
 1107: struct vm_change_state_entry {
 1108:     VMChangeStateHandler *cb;
 1109:     void *opaque;
 1110:     QLIST_ENTRY (vm_change_state_entry) entries;
 1111: };
 1112: 
 1113: static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
 1114: 
 1115: VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
 1116:                                                      void *opaque)
 1117: {
 1118:     VMChangeStateEntry *e;
 1119: 
 1120:     e = qemu_mallocz(sizeof (*e));
 1121: 
 1122:     e->cb = cb;
 1123:     e->opaque = opaque;
 1124:     QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
 1125:     return e;
 1126: }
 1127: 
 1128: void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
 1129: {
 1130:     QLIST_REMOVE (e, entries);
 1131:     qemu_free (e);
 1132: }
 1133: 
 1134: void vm_state_notify(int running, int reason)
 1135: {
 1136:     VMChangeStateEntry *e;
 1137: 
 1138:     trace_vm_state_notify(running, reason);
 1139: 
 1140:     for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
 1141:         e->cb(e->opaque, running, reason);
 1142:     }
 1143: }
 1144: 
 1145: void vm_start(void)
 1146: {
 1147:     if (!vm_running) {
 1148:         cpu_enable_ticks();
 1149:         vm_running = 1;
 1150:         vm_state_notify(1, 0);
 1151:         resume_all_vcpus();
 1152:         monitor_protocol_event(QEVENT_RESUME, NULL);
 1153:     }
 1154: }
 1155: 
 1156: /* reset/shutdown handler */
 1157: 
 1158: typedef struct QEMUResetEntry {
 1159:     QTAILQ_ENTRY(QEMUResetEntry) entry;
 1160:     QEMUResetHandler *func;
 1161:     void *opaque;
 1162: } QEMUResetEntry;
 1163: 
 1164: static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
 1165:     QTAILQ_HEAD_INITIALIZER(reset_handlers);
 1166: static int reset_requested;
 1167: static int shutdown_requested, shutdown_signal = -1;
 1168: static pid_t shutdown_pid;
 1169: static int powerdown_requested;
 1170: static int debug_requested;
 1171: static int vmstop_requested;
 1172: 
 1173: int qemu_shutdown_requested_get(void)
 1174: {
 1175:     return shutdown_requested;
 1176: }
 1177: 
 1178: int qemu_reset_requested_get(void)
 1179: {
 1180:     return reset_requested;
 1181: }
 1182: 
 1183: int qemu_shutdown_requested(void)
 1184: {
 1185:     int r = shutdown_requested;
 1186:     shutdown_requested = 0;
 1187:     return r;
 1188: }
 1189: 
 1190: void qemu_kill_report(void)
 1191: {
 1192:     if (shutdown_signal != -1) {
 1193:         fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
 1194:         if (shutdown_pid == 0) {
 1195:             /* This happens for eg ^C at the terminal, so it's worth
 1196:              * avoiding printing an odd message in that case.
 1197:              */
 1198:             fputc('\n', stderr);
 1199:         } else {
 1200:             fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
 1201:         }
 1202:         shutdown_signal = -1;
 1203:     }
 1204: }
 1205: 
 1206: int qemu_reset_requested(void)
 1207: {
 1208:     int r = reset_requested;
 1209:     reset_requested = 0;
 1210:     return r;
 1211: }
 1212: 
 1213: int qemu_powerdown_requested(void)
 1214: {
 1215:     int r = powerdown_requested;
 1216:     powerdown_requested = 0;
 1217:     return r;
 1218: }
 1219: 
 1220: static int qemu_debug_requested(void)
 1221: {
 1222:     int r = debug_requested;
 1223:     debug_requested = 0;
 1224:     return r;
 1225: }
 1226: 
 1227: static int qemu_vmstop_requested(void)
 1228: {
 1229:     int r = vmstop_requested;
 1230:     vmstop_requested = 0;
 1231:     return r;
 1232: }
 1233: 
 1234: void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 1235: {
 1236:     QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
 1237: 
 1238:     re->func = func;
 1239:     re->opaque = opaque;
 1240:     QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
 1241: }
 1242: 
 1243: void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
 1244: {
 1245:     QEMUResetEntry *re;
 1246: 
 1247:     QTAILQ_FOREACH(re, &reset_handlers, entry) {
 1248:         if (re->func == func && re->opaque == opaque) {
 1249:             QTAILQ_REMOVE(&reset_handlers, re, entry);
 1250:             qemu_free(re);
 1251:             return;
 1252:         }
 1253:     }
 1254: }
 1255: 
 1256: void qemu_system_reset(bool report)
 1257: {
 1258:     QEMUResetEntry *re, *nre;
 1259: 
 1260:     /* reset all devices */
 1261:     QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
 1262:         re->func(re->opaque);
 1263:     }
 1264:     if (report) {
 1265:         monitor_protocol_event(QEVENT_RESET, NULL);
 1266:     }
 1267:     cpu_synchronize_all_post_reset();
 1268: }
 1269: 
 1270: void qemu_system_reset_request(void)
 1271: {
 1272:     if (no_reboot) {
 1273:         shutdown_requested = 1;
 1274:     } else {
 1275:         reset_requested = 1;
 1276:     }
 1277:     cpu_stop_current();
 1278:     qemu_notify_event();
 1279: }
 1280: 
 1281: void qemu_system_killed(int signal, pid_t pid)
 1282: {
 1283:     shutdown_signal = signal;
 1284:     shutdown_pid = pid;
 1285:     no_shutdown = 0;
 1286:     qemu_system_shutdown_request();
 1287: }
 1288: 
 1289: void qemu_system_shutdown_request(void)
 1290: {
 1291:     shutdown_requested = 1;
 1292:     qemu_notify_event();
 1293: }
 1294: 
 1295: void qemu_system_powerdown_request(void)
 1296: {
 1297:     powerdown_requested = 1;
 1298:     qemu_notify_event();
 1299: }
 1300: 
 1301: void qemu_system_debug_request(void)
 1302: {
 1303:     debug_requested = 1;
 1304:     qemu_notify_event();
 1305: }
 1306: 
 1307: void qemu_system_vmstop_request(int reason)
 1308: {
 1309:     vmstop_requested = reason;
 1310:     qemu_notify_event();
 1311: }
 1312: 
 1313: void main_loop_wait(int nonblocking)
 1314: {
 1315:     fd_set rfds, wfds, xfds;
 1316:     int ret, nfds;
 1317:     struct timeval tv;
 1318:     int timeout;
 1319: 
 1320:     if (nonblocking)
 1321:         timeout = 0;
 1322:     else {
 1323:         timeout = qemu_calculate_timeout();
 1324:         qemu_bh_update_timeout(&timeout);
 1325:     }
 1326: 
 1327:     os_host_main_loop_wait(&timeout);
 1328: 
 1329:     tv.tv_sec = timeout / 1000;
 1330:     tv.tv_usec = (timeout % 1000) * 1000;
 1331: 
 1332:     /* poll any events */
 1333:     /* XXX: separate device handlers from system ones */
 1334:     nfds = -1;
 1335:     FD_ZERO(&rfds);
 1336:     FD_ZERO(&wfds);
 1337:     FD_ZERO(&xfds);
 1338:     qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
 1339:     slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
 1340: 
 1341:     qemu_mutex_unlock_iothread();
 1342:     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
 1343:     qemu_mutex_lock_iothread();
 1344: 
 1345:     qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
 1346:     slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
 1347: 
 1348:     qemu_run_all_timers();
 1349: 
 1350:     /* Check bottom-halves last in case any of the earlier events triggered
 1351:        them.  */
 1352:     qemu_bh_poll();
 1353: 
 1354: }
 1355: 
 1356: #ifndef CONFIG_IOTHREAD
 1357: static int vm_request_pending(void)
 1358: {
 1359:     return powerdown_requested ||
 1360:            reset_requested ||
 1361:            shutdown_requested ||
 1362:            debug_requested ||
 1363:            vmstop_requested;
 1364: }
 1365: #endif
 1366: 
 1367: qemu_irq qemu_system_powerdown;
 1368: 
 1369: static void main_loop(void)
 1370: {
 1371:     bool nonblocking = false;
 1372: #ifdef CONFIG_PROFILER
 1373:     int64_t ti;
 1374: #endif
 1375:     int r;
 1376: 
 1377:     qemu_main_loop_start();
 1378: 
 1379:     for (;;) {
 1380: #ifndef CONFIG_IOTHREAD
 1381:         nonblocking = cpu_exec_all();
 1382:         if (vm_request_pending()) {
 1383:             nonblocking = true;
 1384:         }
 1385: #endif
 1386: #ifdef CONFIG_PROFILER
 1387:         ti = profile_getclock();
 1388: #endif
 1389:         main_loop_wait(nonblocking);
 1390: #ifdef CONFIG_PROFILER
 1391:         dev_time += profile_getclock() - ti;
 1392: #endif
 1393: 
 1394:         if (qemu_debug_requested()) {
 1395:             vm_stop(VMSTOP_DEBUG);
 1396:         }
 1397:         if (qemu_shutdown_requested()) {
 1398:             qemu_kill_report();
 1399:             monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
 1400:             if (no_shutdown) {
 1401:                 vm_stop(VMSTOP_SHUTDOWN);
 1402:             } else
 1403:                 break;
 1404:         }
 1405:         if (qemu_reset_requested()) {
 1406:             pause_all_vcpus();
 1407:             cpu_synchronize_all_states();
 1408:             qemu_system_reset(VMRESET_REPORT);
 1409:             resume_all_vcpus();
 1410:         }
 1411:         if (qemu_powerdown_requested()) {
 1412:             monitor_protocol_event(QEVENT_POWERDOWN, NULL);
 1413:             qemu_irq_raise(qemu_system_powerdown);
 1414:         }
 1415:         if ((r = qemu_vmstop_requested())) {
 1416:             vm_stop(r);
 1417:         }
 1418:     }
 1419:     bdrv_close_all();
 1420:     pause_all_vcpus();
 1421: }
 1422: 
 1423: static void version(void)
 1424: {
 1425:     printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
 1426: }
 1427: 
 1428: static void help(int exitcode)
 1429: {
 1430:     const char *options_help =
 1431: #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
 1432:         opt_help
 1433: #define DEFHEADING(text) stringify(text) "\n"
 1434: #include "qemu-options.def"
 1435: #undef DEF
 1436: #undef DEFHEADING
 1437: #undef GEN_DOCS
 1438:         ;
 1439:     version();
 1440:     printf("usage: %s [options] [disk_image]\n"
 1441:            "\n"
 1442:            "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
 1443:            "\n"
 1444:            "%s\n"
 1445:            "During emulation, the following keys are useful:\n"
 1446:            "ctrl-alt-f      toggle full screen\n"
 1447:            "ctrl-alt-n      switch to virtual console 'n'\n"
 1448:            "ctrl-alt        toggle mouse and keyboard grab\n"
 1449:            "\n"
 1450:            "When using -nographic, press 'ctrl-a h' to get some help.\n",
 1451:            "qemu",
 1452:            options_help);
 1453:     exit(exitcode);
 1454: }
 1455: 
 1456: #define HAS_ARG 0x0001
 1457: 
 1458: typedef struct QEMUOption {
 1459:     const char *name;
 1460:     int flags;
 1461:     int index;
 1462:     uint32_t arch_mask;
 1463: } QEMUOption;
 1464: 
 1465: static const QEMUOption qemu_options[] = {
 1466:     { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
 1467: #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
 1468:     { option, opt_arg, opt_enum, arch_mask },
 1469: #define DEFHEADING(text)
 1470: #include "qemu-options.def"
 1471: #undef DEF
 1472: #undef DEFHEADING
 1473: #undef GEN_DOCS
 1474:     { NULL },
 1475: };
 1476: static void select_vgahw (const char *p)
 1477: {
 1478:     const char *opts;
 1479: 
 1480:     default_vga = 0;
 1481:     vga_interface_type = VGA_NONE;
 1482:     if (strstart(p, "std", &opts)) {
 1483:         vga_interface_type = VGA_STD;
 1484:     } else if (strstart(p, "cirrus", &opts)) {
 1485:         vga_interface_type = VGA_CIRRUS;
 1486:     } else if (strstart(p, "vmware", &opts)) {
 1487:         vga_interface_type = VGA_VMWARE;
 1488:     } else if (strstart(p, "xenfb", &opts)) {
 1489:         vga_interface_type = VGA_XENFB;
 1490:     } else if (strstart(p, "qxl", &opts)) {
 1491:         vga_interface_type = VGA_QXL;
 1492:     } else if (!strstart(p, "none", &opts)) {
 1493:     invalid_vga:
 1494:         fprintf(stderr, "Unknown vga type: %s\n", p);
 1495:         exit(1);
 1496:     }
 1497:     while (*opts) {
 1498:         const char *nextopt;
 1499: 
 1500:         if (strstart(opts, ",retrace=", &nextopt)) {
 1501:             opts = nextopt;
 1502:             if (strstart(opts, "dumb", &nextopt))
 1503:                 vga_retrace_method = VGA_RETRACE_DUMB;
 1504:             else if (strstart(opts, "precise", &nextopt))
 1505:                 vga_retrace_method = VGA_RETRACE_PRECISE;
 1506:             else goto invalid_vga;
 1507:         } else goto invalid_vga;
 1508:         opts = nextopt;
 1509:     }
 1510: }
 1511: 
 1512: static DisplayType select_display(const char *p)
 1513: {
 1514:     const char *opts;
 1515:     DisplayType display = DT_DEFAULT;
 1516: 
 1517:     if (strstart(p, "sdl", &opts)) {
 1518: #ifdef CONFIG_SDL
 1519:         display = DT_SDL;
 1520:         while (*opts) {
 1521:             const char *nextopt;
 1522: 
 1523:             if (strstart(opts, ",frame=", &nextopt)) {
 1524:                 opts = nextopt;
 1525:                 if (strstart(opts, "on", &nextopt)) {
 1526:                     no_frame = 0;
 1527:                 } else if (strstart(opts, "off", &nextopt)) {
 1528:                     no_frame = 1;
 1529:                 } else {
 1530:                     goto invalid_sdl_args;
 1531:                 }
 1532:             } else if (strstart(opts, ",alt_grab=", &nextopt)) {
 1533:                 opts = nextopt;
 1534:                 if (strstart(opts, "on", &nextopt)) {
 1535:                     alt_grab = 1;
 1536:                 } else if (strstart(opts, "off", &nextopt)) {
 1537:                     alt_grab = 0;
 1538:                 } else {
 1539:                     goto invalid_sdl_args;
 1540:                 }
 1541:             } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
 1542:                 opts = nextopt;
 1543:                 if (strstart(opts, "on", &nextopt)) {
 1544:                     ctrl_grab = 1;
 1545:                 } else if (strstart(opts, "off", &nextopt)) {
 1546:                     ctrl_grab = 0;
 1547:                 } else {
 1548:                     goto invalid_sdl_args;
 1549:                 }
 1550:             } else if (strstart(opts, ",window_close=", &nextopt)) {
 1551:                 opts = nextopt;
 1552:                 if (strstart(opts, "on", &nextopt)) {
 1553:                     no_quit = 0;
 1554:                 } else if (strstart(opts, "off", &nextopt)) {
 1555:                     no_quit = 1;
 1556:                 } else {
 1557:                     goto invalid_sdl_args;
 1558:                 }
 1559:             } else {
 1560:             invalid_sdl_args:
 1561:                 fprintf(stderr, "Invalid SDL option string: %s\n", p);
 1562:                 exit(1);
 1563:             }
 1564:             opts = nextopt;
 1565:         }
 1566: #else
 1567:         fprintf(stderr, "SDL support is disabled\n");
 1568:         exit(1);
 1569: #endif
 1570:     } else if (strstart(p, "vnc", &opts)) {
 1571: #ifdef CONFIG_VNC
 1572:         display_remote++;
 1573: 
 1574:         if (*opts) {
 1575:             const char *nextopt;
 1576: 
 1577:             if (strstart(opts, "=", &nextopt)) {
 1578:                 vnc_display = nextopt;
 1579:             }
 1580:         }
 1581:         if (!vnc_display) {
 1582:             fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
 1583:             exit(1);
 1584:         }
 1585: #else
 1586:         fprintf(stderr, "VNC support is disabled\n");
 1587:         exit(1);
 1588: #endif
 1589:     } else if (strstart(p, "curses", &opts)) {
 1590: #ifdef CONFIG_CURSES
 1591:         display = DT_CURSES;
 1592: #else
 1593:         fprintf(stderr, "Curses support is disabled\n");
 1594:         exit(1);
 1595: #endif
 1596:     } else if (strstart(p, "none", &opts)) {
 1597:         display = DT_NONE;
 1598:     } else {
 1599:         fprintf(stderr, "Unknown display type: %s\n", p);
 1600:         exit(1);
 1601:     }
 1602: 
 1603:     return display;
 1604: }
 1605: 
 1606: static int balloon_parse(const char *arg)
 1607: {
 1608:     QemuOpts *opts;
 1609: 
 1610:     if (strcmp(arg, "none") == 0) {
 1611:         return 0;
 1612:     }
 1613: 
 1614:     if (!strncmp(arg, "virtio", 6)) {
 1615:         if (arg[6] == ',') {
 1616:             /* have params -> parse them */
 1617:             opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
 1618:             if (!opts)
 1619:                 return  -1;
 1620:         } else {
 1621:             /* create empty opts */
 1622:             opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
 1623:         }
 1624:         qemu_opt_set(opts, "driver", "virtio-balloon");
 1625:         return 0;
 1626:     }
 1627: 
 1628:     return -1;
 1629: }
 1630: 
 1631: char *qemu_find_file(int type, const char *name)
 1632: {
 1633:     int len;
 1634:     const char *subdir;
 1635:     char *buf;
 1636: 
 1637:     /* If name contains path separators then try it as a straight path.  */
 1638:     if ((strchr(name, '/') || strchr(name, '\\'))
 1639:         && access(name, R_OK) == 0) {
 1640:         return qemu_strdup(name);
 1641:     }
 1642:     switch (type) {
 1643:     case QEMU_FILE_TYPE_BIOS:
 1644:         subdir = "";
 1645:         break;
 1646:     case QEMU_FILE_TYPE_KEYMAP:
 1647:         subdir = "keymaps/";
 1648:         break;
 1649:     default:
 1650:         abort();
 1651:     }
 1652:     len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
 1653:     buf = qemu_mallocz(len);
 1654:     snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
 1655:     if (access(buf, R_OK)) {
 1656:         qemu_free(buf);
 1657:         return NULL;
 1658:     }
 1659:     return buf;
 1660: }
 1661: 
 1662: static int device_help_func(QemuOpts *opts, void *opaque)
 1663: {
 1664:     return qdev_device_help(opts);
 1665: }
 1666: 
 1667: static int device_init_func(QemuOpts *opts, void *opaque)
 1668: {
 1669:     DeviceState *dev;
 1670: 
 1671:     dev = qdev_device_add(opts);
 1672:     if (!dev)
 1673:         return -1;
 1674:     return 0;
 1675: }
 1676: 
 1677: static int chardev_init_func(QemuOpts *opts, void *opaque)
 1678: {
 1679:     CharDriverState *chr;
 1680: 
 1681:     chr = qemu_chr_open_opts(opts, NULL);
 1682:     if (!chr)
 1683:         return -1;
 1684:     return 0;
 1685: }
 1686: 
 1687: #ifdef CONFIG_VIRTFS
 1688: static int fsdev_init_func(QemuOpts *opts, void *opaque)
 1689: {
 1690:     int ret;
 1691:     ret = qemu_fsdev_add(opts);
 1692: 
 1693:     return ret;
 1694: }
 1695: #endif
 1696: 
 1697: static int mon_init_func(QemuOpts *opts, void *opaque)
 1698: {
 1699:     CharDriverState *chr;
 1700:     const char *chardev;
 1701:     const char *mode;
 1702:     int flags;
 1703: 
 1704:     mode = qemu_opt_get(opts, "mode");
 1705:     if (mode == NULL) {
 1706:         mode = "readline";
 1707:     }
 1708:     if (strcmp(mode, "readline") == 0) {
 1709:         flags = MONITOR_USE_READLINE;
 1710:     } else if (strcmp(mode, "control") == 0) {
 1711:         flags = MONITOR_USE_CONTROL;
 1712:     } else {
 1713:         fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
 1714:         exit(1);
 1715:     }
 1716: 
 1717:     if (qemu_opt_get_bool(opts, "pretty", 0))
 1718:         flags |= MONITOR_USE_PRETTY;
 1719: 
 1720:     if (qemu_opt_get_bool(opts, "default", 0))
 1721:         flags |= MONITOR_IS_DEFAULT;
 1722: 
 1723:     chardev = qemu_opt_get(opts, "chardev");
 1724:     chr = qemu_chr_find(chardev);
 1725:     if (chr == NULL) {
 1726:         fprintf(stderr, "chardev \"%s\" not found\n", chardev);
 1727:         exit(1);
 1728:     }
 1729: 
 1730:     monitor_init(chr, flags);
 1731:     return 0;
 1732: }
 1733: 
 1734: static void monitor_parse(const char *optarg, const char *mode)
 1735: {
 1736:     static int monitor_device_index = 0;
 1737:     QemuOpts *opts;
 1738:     const char *p;
 1739:     char label[32];
 1740:     int def = 0;
 1741: 
 1742:     if (strstart(optarg, "chardev:", &p)) {
 1743:         snprintf(label, sizeof(label), "%s", p);
 1744:     } else {
 1745:         snprintf(label, sizeof(label), "compat_monitor%d",
 1746:                  monitor_device_index);
 1747:         if (monitor_device_index == 0) {
 1748:             def = 1;
 1749:         }
 1750:         opts = qemu_chr_parse_compat(label, optarg);
 1751:         if (!opts) {
 1752:             fprintf(stderr, "parse error: %s\n", optarg);
 1753:             exit(1);
 1754:         }
 1755:     }
 1756: 
 1757:     opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
 1758:     if (!opts) {
 1759:         fprintf(stderr, "duplicate chardev: %s\n", label);
 1760:         exit(1);
 1761:     }
 1762:     qemu_opt_set(opts, "mode", mode);
 1763:     qemu_opt_set(opts, "chardev", label);
 1764:     if (def)
 1765:         qemu_opt_set(opts, "default", "on");
 1766:     monitor_device_index++;
 1767: }
 1768: 
 1769: struct device_config {
 1770:     enum {
 1771:         DEV_USB,       /* -usbdevice     */
 1772:         DEV_BT,        /* -bt            */
 1773:         DEV_SERIAL,    /* -serial        */
 1774:         DEV_PARALLEL,  /* -parallel      */
 1775:         DEV_VIRTCON,   /* -virtioconsole */
 1776:         DEV_DEBUGCON,  /* -debugcon */
 1777:     } type;
 1778:     const char *cmdline;
 1779:     QTAILQ_ENTRY(device_config) next;
 1780: };
 1781: QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
 1782: 
 1783: static void add_device_config(int type, const char *cmdline)
 1784: {
 1785:     struct device_config *conf;
 1786: 
 1787:     conf = qemu_mallocz(sizeof(*conf));
 1788:     conf->type = type;
 1789:     conf->cmdline = cmdline;
 1790:     QTAILQ_INSERT_TAIL(&device_configs, conf, next);
 1791: }
 1792: 
 1793: static int foreach_device_config(int type, int (*func)(const char *cmdline))
 1794: {
 1795:     struct device_config *conf;
 1796:     int rc;
 1797: 
 1798:     QTAILQ_FOREACH(conf, &device_configs, next) {
 1799:         if (conf->type != type)
 1800:             continue;
 1801:         rc = func(conf->cmdline);
 1802:         if (0 != rc)
 1803:             return rc;
 1804:     }
 1805:     return 0;
 1806: }
 1807: 
 1808: static int serial_parse(const char *devname)
 1809: {
 1810:     static int index = 0;
 1811:     char label[32];
 1812: 
 1813:     if (strcmp(devname, "none") == 0)
 1814:         return 0;
 1815:     if (index == MAX_SERIAL_PORTS) {
 1816:         fprintf(stderr, "qemu: too many serial ports\n");
 1817:         exit(1);
 1818:     }
 1819:     snprintf(label, sizeof(label), "serial%d", index);
 1820:     serial_hds[index] = qemu_chr_open(label, devname, NULL);
 1821:     if (!serial_hds[index]) {
 1822:         fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
 1823:                 devname, strerror(errno));
 1824:         return -1;
 1825:     }
 1826:     index++;
 1827:     return 0;
 1828: }
 1829: 
 1830: static int parallel_parse(const char *devname)
 1831: {
 1832:     static int index = 0;
 1833:     char label[32];
 1834: 
 1835:     if (strcmp(devname, "none") == 0)
 1836:         return 0;
 1837:     if (index == MAX_PARALLEL_PORTS) {
 1838:         fprintf(stderr, "qemu: too many parallel ports\n");
 1839:         exit(1);
 1840:     }
 1841:     snprintf(label, sizeof(label), "parallel%d", index);
 1842:     parallel_hds[index] = qemu_chr_open(label, devname, NULL);
 1843:     if (!parallel_hds[index]) {
 1844:         fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
 1845:                 devname, strerror(errno));
 1846:         return -1;
 1847:     }
 1848:     index++;
 1849:     return 0;
 1850: }
 1851: 
 1852: static int virtcon_parse(const char *devname)
 1853: {
 1854:     QemuOptsList *device = qemu_find_opts("device");
 1855:     static int index = 0;
 1856:     char label[32];
 1857:     QemuOpts *bus_opts, *dev_opts;
 1858: 
 1859:     if (strcmp(devname, "none") == 0)
 1860:         return 0;
 1861:     if (index == MAX_VIRTIO_CONSOLES) {
 1862:         fprintf(stderr, "qemu: too many virtio consoles\n");
 1863:         exit(1);
 1864:     }
 1865: 
 1866:     bus_opts = qemu_opts_create(device, NULL, 0);
 1867:     qemu_opt_set(bus_opts, "driver", "virtio-serial");
 1868: 
 1869:     dev_opts = qemu_opts_create(device, NULL, 0);
 1870:     qemu_opt_set(dev_opts, "driver", "virtconsole");
 1871: 
 1872:     snprintf(label, sizeof(label), "virtcon%d", index);
 1873:     virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
 1874:     if (!virtcon_hds[index]) {
 1875:         fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
 1876:                 devname, strerror(errno));
 1877:         return -1;
 1878:     }
 1879:     qemu_opt_set(dev_opts, "chardev", label);
 1880: 
 1881:     index++;
 1882:     return 0;
 1883: }
 1884: 
 1885: static int debugcon_parse(const char *devname)
 1886: {   
 1887:     QemuOpts *opts;
 1888: 
 1889:     if (!qemu_chr_open("debugcon", devname, NULL)) {
 1890:         exit(1);
 1891:     }
 1892:     opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
 1893:     if (!opts) {
 1894:         fprintf(stderr, "qemu: already have a debugcon device\n");
 1895:         exit(1);
 1896:     }
 1897:     qemu_opt_set(opts, "driver", "isa-debugcon");
 1898:     qemu_opt_set(opts, "chardev", "debugcon");
 1899:     return 0;
 1900: }
 1901: 
 1902: static QEMUMachine *machine_parse(const char *name)
 1903: {
 1904:     QEMUMachine *m, *machine = NULL;
 1905: 
 1906:     if (name) {
 1907:         machine = find_machine(name);
 1908:     }
 1909:     if (machine) {
 1910:         return machine;
 1911:     }
 1912:     printf("Supported machines are:\n");
 1913:     for (m = first_machine; m != NULL; m = m->next) {
 1914:         if (m->alias) {
 1915:             printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
 1916:         }
 1917:         printf("%-10s %s%s\n", m->name, m->desc,
 1918:                m->is_default ? " (default)" : "");
 1919:     }
 1920:     exit(!name || *name != '?');
 1921: }
 1922: 
 1923: static int tcg_init(void)
 1924: {
 1925:     return 0;
 1926: }
 1927: 
 1928: static struct {
 1929:     const char *opt_name;
 1930:     const char *name;
 1931:     int (*available)(void);
 1932:     int (*init)(void);
 1933:     int *allowed;
 1934: } accel_list[] = {
 1935:     { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
 1936:     { "xen", "Xen", xen_available, xen_init, &xen_allowed },
 1937:     { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
 1938: };
 1939: 
 1940: static int configure_accelerator(void)
 1941: {
 1942:     const char *p = NULL;
 1943:     char buf[10];
 1944:     int i, ret;
 1945:     bool accel_initalised = 0;
 1946:     bool init_failed = 0;
 1947: 
 1948:     QemuOptsList *list = qemu_find_opts("machine");
 1949:     if (!QTAILQ_EMPTY(&list->head)) {
 1950:         p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
 1951:     }
 1952: 
 1953:     if (p == NULL) {
 1954:         /* Use the default "accelerator", tcg */
 1955:         p = "tcg";
 1956:     }
 1957: 
 1958:     while (!accel_initalised && *p != '\0') {
 1959:         if (*p == ':') {
 1960:             p++;
 1961:         }
 1962:         p = get_opt_name(buf, sizeof (buf), p, ':');
 1963:         for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
 1964:             if (strcmp(accel_list[i].opt_name, buf) == 0) {
 1965:                 *(accel_list[i].allowed) = 1;
 1966:                 ret = accel_list[i].init();
 1967:                 if (ret < 0) {
 1968:                     init_failed = 1;
 1969:                     if (!accel_list[i].available()) {
 1970:                         printf("%s not supported for this target\n",
 1971:                                accel_list[i].name);
 1972:                     } else {
 1973:                         fprintf(stderr, "failed to initialize %s: %s\n",
 1974:                                 accel_list[i].name,
 1975:                                 strerror(-ret));
 1976:                     }
 1977:                     *(accel_list[i].allowed) = 0;
 1978:                 } else {
 1979:                     accel_initalised = 1;
 1980:                 }
 1981:                 break;
 1982:             }
 1983:         }
 1984:         if (i == ARRAY_SIZE(accel_list)) {
 1985:             fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
 1986:         }
 1987:     }
 1988: 
 1989:     if (!accel_initalised) {
 1990:         fprintf(stderr, "No accelerator found!\n");
 1991:         exit(1);
 1992:     }
 1993: 
 1994:     if (init_failed) {
 1995:         fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
 1996:     }
 1997: 
 1998:     return !accel_initalised;
 1999: }
 2000: 
 2001: void qemu_add_exit_notifier(Notifier *notify)
 2002: {
 2003:     notifier_list_add(&exit_notifiers, notify);
 2004: }
 2005: 
 2006: void qemu_remove_exit_notifier(Notifier *notify)
 2007: {
 2008:     notifier_list_remove(&exit_notifiers, notify);
 2009: }
 2010: 
 2011: static void qemu_run_exit_notifiers(void)
 2012: {
 2013:     notifier_list_notify(&exit_notifiers, NULL);
 2014: }
 2015: 
 2016: void qemu_add_machine_init_done_notifier(Notifier *notify)
 2017: {
 2018:     notifier_list_add(&machine_init_done_notifiers, notify);
 2019: }
 2020: 
 2021: static void qemu_run_machine_init_done_notifiers(void)
 2022: {
 2023:     notifier_list_notify(&machine_init_done_notifiers, NULL);
 2024: }
 2025: 
 2026: static const QEMUOption *lookup_opt(int argc, char **argv,
 2027:                                     const char **poptarg, int *poptind)
 2028: {
 2029:     const QEMUOption *popt;
 2030:     int optind = *poptind;
 2031:     char *r = argv[optind];
 2032:     const char *optarg;
 2033: 
 2034:     loc_set_cmdline(argv, optind, 1);
 2035:     optind++;
 2036:     /* Treat --foo the same as -foo.  */
 2037:     if (r[1] == '-')
 2038:         r++;
 2039:     popt = qemu_options;
 2040:     for(;;) {
 2041:         if (!popt->name) {
 2042:             error_report("invalid option");
 2043:             exit(1);
 2044:         }
 2045:         if (!strcmp(popt->name, r + 1))
 2046:             break;
 2047:         popt++;
 2048:     }
 2049:     if (popt->flags & HAS_ARG) {
 2050:         if (optind >= argc) {
 2051:             error_report("requires an argument");
 2052:             exit(1);
 2053:         }
 2054:         optarg = argv[optind++];
 2055:         loc_set_cmdline(argv, optind - 2, 2);
 2056:     } else {
 2057:         optarg = NULL;
 2058:     }
 2059: 
 2060:     *poptarg = optarg;
 2061:     *poptind = optind;
 2062: 
 2063:     return popt;
 2064: }
 2065: 
 2066: int main(int argc, char **argv, char **envp)
 2067: {
 2068:     const char *gdbstub_dev = NULL;
 2069:     int i;
 2070:     int snapshot, linux_boot;
 2071:     const char *icount_option = NULL;
 2072:     const char *initrd_filename;
 2073:     const char *kernel_filename, *kernel_cmdline;
 2074:     char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
 2075:     DisplayState *ds;
 2076:     DisplayChangeListener *dcl;
 2077:     int cyls, heads, secs, translation;
 2078:     QemuOpts *hda_opts = NULL, *opts;
 2079:     QemuOptsList *olist;
 2080:     int optind;
 2081:     const char *optarg;
 2082:     const char *loadvm = NULL;
 2083:     QEMUMachine *machine;
 2084:     const char *cpu_model;
 2085:     int tb_size;
 2086:     const char *pid_file = NULL;
 2087:     const char *incoming = NULL;
 2088: #ifdef CONFIG_VNC
 2089:     int show_vnc_port = 0;
 2090: #endif
 2091:     int defconfig = 1;
 2092:     const char *trace_file = NULL;
 2093:     const char *log_mask = NULL;
 2094:     const char *log_file = NULL;
 2095: 
 2096:     atexit(qemu_run_exit_notifiers);
 2097:     error_set_progname(argv[0]);
 2098: 
 2099:     init_clocks();
 2100: 
 2101:     qemu_cache_utils_init(envp);
 2102: 
 2103:     QLIST_INIT (&vm_change_state_head);
 2104:     os_setup_early_signal_handling();
 2105: 
 2106:     module_call_init(MODULE_INIT_MACHINE);
 2107:     machine = find_default_machine();
 2108:     cpu_model = NULL;
 2109:     initrd_filename = NULL;
 2110:     ram_size = 0;
 2111:     snapshot = 0;
 2112:     kernel_filename = NULL;
 2113:     kernel_cmdline = "";
 2114:     cyls = heads = secs = 0;
 2115:     translation = BIOS_ATA_TRANSLATION_AUTO;
 2116: 
 2117:     for (i = 0; i < MAX_NODES; i++) {
 2118:         node_mem[i] = 0;
 2119:         node_cpumask[i] = 0;
 2120:     }
 2121: 
 2122:     nb_numa_nodes = 0;
 2123:     nb_nics = 0;
 2124: 
 2125:     tb_size = 0;
 2126:     autostart= 1;
 2127: 
 2128:     /* first pass of option parsing */
 2129:     optind = 1;
 2130:     while (optind < argc) {
 2131:         if (argv[optind][0] != '-') {
 2132:             /* disk image */
 2133:             optind++;
 2134:             continue;
 2135:         } else {
 2136:             const QEMUOption *popt;
 2137: 
 2138:             popt = lookup_opt(argc, argv, &optarg, &optind);
 2139:             switch (popt->index) {
 2140:             case QEMU_OPTION_nodefconfig:
 2141:                 defconfig=0;
 2142:                 break;
 2143:             }
 2144:         }
 2145:     }
 2146: 
 2147:     if (defconfig) {
 2148:         int ret;
 2149: 
 2150:         ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
 2151:         if (ret < 0 && ret != -ENOENT) {
 2152:             exit(1);
 2153:         }
 2154: 
 2155:         ret = qemu_read_config_file(arch_config_name);
 2156:         if (ret < 0 && ret != -ENOENT) {
 2157:             exit(1);
 2158:         }
 2159:     }
 2160:     cpudef_init();
 2161: 
 2162:     /* second pass of option parsing */
 2163:     optind = 1;
 2164:     for(;;) {
 2165:         if (optind >= argc)
 2166:             break;
 2167:         if (argv[optind][0] != '-') {
 2168: 	    hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
 2169:         } else {
 2170:             const QEMUOption *popt;
 2171: 
 2172:             popt = lookup_opt(argc, argv, &optarg, &optind);
 2173:             if (!(popt->arch_mask & arch_type)) {
 2174:                 printf("Option %s not supported for this target\n", popt->name);
 2175:                 exit(1);
 2176:             }
 2177:             switch(popt->index) {
 2178:             case QEMU_OPTION_M:
 2179:                 machine = machine_parse(optarg);
 2180:                 break;
 2181:             case QEMU_OPTION_cpu:
 2182:                 /* hw initialization will check this */
 2183:                 if (*optarg == '?') {
 2184:                     list_cpus(stdout, &fprintf, optarg);
 2185:                     exit(0);
 2186:                 } else {
 2187:                     cpu_model = optarg;
 2188:                 }
 2189:                 break;
 2190:             case QEMU_OPTION_initrd:
 2191:                 initrd_filename = optarg;
 2192:                 break;
 2193:             case QEMU_OPTION_hda:
 2194:                 {
 2195:                     char buf[256];
 2196:                     if (cyls == 0)
 2197:                         snprintf(buf, sizeof(buf), "%s", HD_OPTS);
 2198:                     else
 2199:                         snprintf(buf, sizeof(buf),
 2200:                                  "%s,cyls=%d,heads=%d,secs=%d%s",
 2201:                                  HD_OPTS , cyls, heads, secs,
 2202:                                  translation == BIOS_ATA_TRANSLATION_LBA ?
 2203:                                  ",trans=lba" :
 2204:                                  translation == BIOS_ATA_TRANSLATION_NONE ?
 2205:                                  ",trans=none" : "");
 2206:                     drive_add(IF_DEFAULT, 0, optarg, buf);
 2207:                     break;
 2208:                 }
 2209:             case QEMU_OPTION_hdb:
 2210:             case QEMU_OPTION_hdc:
 2211:             case QEMU_OPTION_hdd:
 2212:                 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
 2213:                           HD_OPTS);
 2214:                 break;
 2215:             case QEMU_OPTION_drive:
 2216:                 if (drive_def(optarg) == NULL) {
 2217:                     exit(1);
 2218:                 }
 2219: 	        break;
 2220:             case QEMU_OPTION_set:
 2221:                 if (qemu_set_option(optarg) != 0)
 2222:                     exit(1);
 2223: 	        break;
 2224:             case QEMU_OPTION_global:
 2225:                 if (qemu_global_option(optarg) != 0)
 2226:                     exit(1);
 2227: 	        break;
 2228:             case QEMU_OPTION_mtdblock:
 2229:                 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
 2230:                 break;
 2231:             case QEMU_OPTION_sd:
 2232:                 drive_add(IF_SD, 0, optarg, SD_OPTS);
 2233:                 break;
 2234:             case QEMU_OPTION_pflash:
 2235:                 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
 2236:                 break;
 2237:             case QEMU_OPTION_snapshot:
 2238:                 snapshot = 1;
 2239:                 break;
 2240:             case QEMU_OPTION_hdachs:
 2241:                 {
 2242:                     const char *p;
 2243:                     p = optarg;
 2244:                     cyls = strtol(p, (char **)&p, 0);
 2245:                     if (cyls < 1 || cyls > 16383)
 2246:                         goto chs_fail;
 2247:                     if (*p != ',')
 2248:                         goto chs_fail;
 2249:                     p++;
 2250:                     heads = strtol(p, (char **)&p, 0);
 2251:                     if (heads < 1 || heads > 16)
 2252:                         goto chs_fail;
 2253:                     if (*p != ',')
 2254:                         goto chs_fail;
 2255:                     p++;
 2256:                     secs = strtol(p, (char **)&p, 0);
 2257:                     if (secs < 1 || secs > 63)
 2258:                         goto chs_fail;
 2259:                     if (*p == ',') {
 2260:                         p++;
 2261:                         if (!strcmp(p, "none"))
 2262:                             translation = BIOS_ATA_TRANSLATION_NONE;
 2263:                         else if (!strcmp(p, "lba"))
 2264:                             translation = BIOS_ATA_TRANSLATION_LBA;
 2265:                         else if (!strcmp(p, "auto"))
 2266:                             translation = BIOS_ATA_TRANSLATION_AUTO;
 2267:                         else
 2268:                             goto chs_fail;
 2269:                     } else if (*p != '\0') {
 2270:                     chs_fail:
 2271:                         fprintf(stderr, "qemu: invalid physical CHS format\n");
 2272:                         exit(1);
 2273:                     }
 2274: 		    if (hda_opts != NULL) {
 2275:                         char num[16];
 2276:                         snprintf(num, sizeof(num), "%d", cyls);
 2277:                         qemu_opt_set(hda_opts, "cyls", num);
 2278:                         snprintf(num, sizeof(num), "%d", heads);
 2279:                         qemu_opt_set(hda_opts, "heads", num);
 2280:                         snprintf(num, sizeof(num), "%d", secs);
 2281:                         qemu_opt_set(hda_opts, "secs", num);
 2282:                         if (translation == BIOS_ATA_TRANSLATION_LBA)
 2283:                             qemu_opt_set(hda_opts, "trans", "lba");
 2284:                         if (translation == BIOS_ATA_TRANSLATION_NONE)
 2285:                             qemu_opt_set(hda_opts, "trans", "none");
 2286:                     }
 2287:                 }
 2288:                 break;
 2289:             case QEMU_OPTION_numa:
 2290:                 if (nb_numa_nodes >= MAX_NODES) {
 2291:                     fprintf(stderr, "qemu: too many NUMA nodes\n");
 2292:                     exit(1);
 2293:                 }
 2294:                 numa_add(optarg);
 2295:                 break;
 2296:             case QEMU_OPTION_display:
 2297:                 display_type = select_display(optarg);
 2298:                 break;
 2299:             case QEMU_OPTION_nographic:
 2300:                 display_type = DT_NOGRAPHIC;
 2301:                 break;
 2302:             case QEMU_OPTION_curses:
 2303: #ifdef CONFIG_CURSES
 2304:                 display_type = DT_CURSES;
 2305: #else
 2306:                 fprintf(stderr, "Curses support is disabled\n");
 2307:                 exit(1);
 2308: #endif
 2309:                 break;
 2310:             case QEMU_OPTION_portrait:
 2311:                 graphic_rotate = 90;
 2312:                 break;
 2313:             case QEMU_OPTION_rotate:
 2314:                 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
 2315:                 if (graphic_rotate != 0 && graphic_rotate != 90 &&
 2316:                     graphic_rotate != 180 && graphic_rotate != 270) {
 2317:                     fprintf(stderr,
 2318:                         "qemu: only 90, 180, 270 deg rotation is available\n");
 2319:                     exit(1);
 2320:                 }
 2321:                 break;
 2322:             case QEMU_OPTION_kernel:
 2323:                 kernel_filename = optarg;
 2324:                 break;
 2325:             case QEMU_OPTION_append:
 2326:                 kernel_cmdline = optarg;
 2327:                 break;
 2328:             case QEMU_OPTION_cdrom:
 2329:                 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
 2330:                 break;
 2331:             case QEMU_OPTION_boot:
 2332:                 {
 2333:                     static const char * const params[] = {
 2334:                         "order", "once", "menu", NULL
 2335:                     };
 2336:                     char buf[sizeof(boot_devices)];
 2337:                     char *standard_boot_devices;
 2338:                     int legacy = 0;
 2339: 
 2340:                     if (!strchr(optarg, '=')) {
 2341:                         legacy = 1;
 2342:                         pstrcpy(buf, sizeof(buf), optarg);
 2343:                     } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
 2344:                         fprintf(stderr,
 2345:                                 "qemu: unknown boot parameter '%s' in '%s'\n",
 2346:                                 buf, optarg);
 2347:                         exit(1);
 2348:                     }
 2349: 
 2350:                     if (legacy ||
 2351:                         get_param_value(buf, sizeof(buf), "order", optarg)) {
 2352:                         validate_bootdevices(buf);
 2353:                         pstrcpy(boot_devices, sizeof(boot_devices), buf);
 2354:                     }
 2355:                     if (!legacy) {
 2356:                         if (get_param_value(buf, sizeof(buf),
 2357:                                             "once", optarg)) {
 2358:                             validate_bootdevices(buf);
 2359:                             standard_boot_devices = qemu_strdup(boot_devices);
 2360:                             pstrcpy(boot_devices, sizeof(boot_devices), buf);
 2361:                             qemu_register_reset(restore_boot_devices,
 2362:                                                 standard_boot_devices);
 2363:                         }
 2364:                         if (get_param_value(buf, sizeof(buf),
 2365:                                             "menu", optarg)) {
 2366:                             if (!strcmp(buf, "on")) {
 2367:                                 boot_menu = 1;
 2368:                             } else if (!strcmp(buf, "off")) {
 2369:                                 boot_menu = 0;
 2370:                             } else {
 2371:                                 fprintf(stderr,
 2372:                                         "qemu: invalid option value '%s'\n",
 2373:                                         buf);
 2374:                                 exit(1);
 2375:                             }
 2376:                         }
 2377:                     }
 2378:                 }
 2379:                 break;
 2380:             case QEMU_OPTION_fda:
 2381:             case QEMU_OPTION_fdb:
 2382:                 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
 2383:                           optarg, FD_OPTS);
 2384:                 break;
 2385:             case QEMU_OPTION_no_fd_bootchk:
 2386:                 fd_bootchk = 0;
 2387:                 break;
 2388:             case QEMU_OPTION_netdev:
 2389:                 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
 2390:                     exit(1);
 2391:                 }
 2392:                 break;
 2393:             case QEMU_OPTION_net:
 2394:                 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
 2395:                     exit(1);
 2396:                 }
 2397:                 break;
 2398: #ifdef CONFIG_SLIRP
 2399:             case QEMU_OPTION_tftp:
 2400:                 legacy_tftp_prefix = optarg;
 2401:                 break;
 2402:             case QEMU_OPTION_bootp:
 2403:                 legacy_bootp_filename = optarg;
 2404:                 break;
 2405:             case QEMU_OPTION_redir:
 2406:                 if (net_slirp_redir(optarg) < 0)
 2407:                     exit(1);
 2408:                 break;
 2409: #endif
 2410:             case QEMU_OPTION_bt:
 2411:                 add_device_config(DEV_BT, optarg);
 2412:                 break;
 2413:             case QEMU_OPTION_audio_help:
 2414:                 if (!(audio_available())) {
 2415:                     printf("Option %s not supported for this target\n", popt->name);
 2416:                     exit(1);
 2417:                 }
 2418:                 AUD_help ();
 2419:                 exit (0);
 2420:                 break;
 2421:             case QEMU_OPTION_soundhw:
 2422:                 if (!(audio_available())) {
 2423:                     printf("Option %s not supported for this target\n", popt->name);
 2424:                     exit(1);
 2425:                 }
 2426:                 select_soundhw (optarg);
 2427:                 break;
 2428:             case QEMU_OPTION_h:
 2429:                 help(0);
 2430:                 break;
 2431:             case QEMU_OPTION_version:
 2432:                 version();
 2433:                 exit(0);
 2434:                 break;
 2435:             case QEMU_OPTION_m: {
 2436:                 int64_t value;
 2437: 
 2438:                 value = strtosz(optarg, NULL);
 2439:                 if (value < 0) {
 2440:                     fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
 2441:                     exit(1);
 2442:                 }
 2443: 
 2444:                 /* On 32-bit hosts, QEMU is limited by virtual address space */
 2445:                 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
 2446:                     fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
 2447:                     exit(1);
 2448:                 }
 2449:                 if (value != (uint64_t)(ram_addr_t)value) {
 2450:                     fprintf(stderr, "qemu: ram size too large\n");
 2451:                     exit(1);
 2452:                 }
 2453:                 ram_size = value;
 2454:                 break;
 2455:             }
 2456:             case QEMU_OPTION_mempath:
 2457:                 mem_path = optarg;
 2458:                 break;
 2459: #ifdef MAP_POPULATE
 2460:             case QEMU_OPTION_mem_prealloc:
 2461:                 mem_prealloc = 1;
 2462:                 break;
 2463: #endif
 2464:             case QEMU_OPTION_d:
 2465:                 log_mask = optarg;
 2466:                 break;
 2467:             case QEMU_OPTION_D:
 2468:                 log_file = optarg;
 2469:                 break;
 2470:             case QEMU_OPTION_s:
 2471:                 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
 2472:                 break;
 2473:             case QEMU_OPTION_gdb:
 2474:                 gdbstub_dev = optarg;
 2475:                 break;
 2476:             case QEMU_OPTION_L:
 2477:                 data_dir = optarg;
 2478:                 break;
 2479:             case QEMU_OPTION_bios:
 2480:                 bios_name = optarg;
 2481:                 break;
 2482:             case QEMU_OPTION_singlestep:
 2483:                 singlestep = 1;
 2484:                 break;
 2485:             case QEMU_OPTION_S:
 2486:                 autostart = 0;
 2487:                 break;
 2488: 	    case QEMU_OPTION_k:
 2489: 		keyboard_layout = optarg;
 2490: 		break;
 2491:             case QEMU_OPTION_localtime:
 2492:                 rtc_utc = 0;
 2493:                 break;
 2494:             case QEMU_OPTION_vga:
 2495:                 select_vgahw (optarg);
 2496:                 break;
 2497:             case QEMU_OPTION_g:
 2498:                 {
 2499:                     const char *p;
 2500:                     int w, h, depth;
 2501:                     p = optarg;
 2502:                     w = strtol(p, (char **)&p, 10);
 2503:                     if (w <= 0) {
 2504:                     graphic_error:
 2505:                         fprintf(stderr, "qemu: invalid resolution or depth\n");
 2506:                         exit(1);
 2507:                     }
 2508:                     if (*p != 'x')
 2509:                         goto graphic_error;
 2510:                     p++;
 2511:                     h = strtol(p, (char **)&p, 10);
 2512:                     if (h <= 0)
 2513:                         goto graphic_error;
 2514:                     if (*p == 'x') {
 2515:                         p++;
 2516:                         depth = strtol(p, (char **)&p, 10);
 2517:                         if (depth != 8 && depth != 15 && depth != 16 &&
 2518:                             depth != 24 && depth != 32)
 2519:                             goto graphic_error;
 2520:                     } else if (*p == '\0') {
 2521:                         depth = graphic_depth;
 2522:                     } else {
 2523:                         goto graphic_error;
 2524:                     }
 2525: 
 2526:                     graphic_width = w;
 2527:                     graphic_height = h;
 2528:                     graphic_depth = depth;
 2529:                 }
 2530:                 break;
 2531:             case QEMU_OPTION_echr:
 2532:                 {
 2533:                     char *r;
 2534:                     term_escape_char = strtol(optarg, &r, 0);
 2535:                     if (r == optarg)
 2536:                         printf("Bad argument to echr\n");
 2537:                     break;
 2538:                 }
 2539:             case QEMU_OPTION_monitor:
 2540:                 monitor_parse(optarg, "readline");
 2541:                 default_monitor = 0;
 2542:                 break;
 2543:             case QEMU_OPTION_qmp:
 2544:                 monitor_parse(optarg, "control");
 2545:                 default_monitor = 0;
 2546:                 break;
 2547:             case QEMU_OPTION_mon:
 2548:                 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
 2549:                 if (!opts) {
 2550:                     exit(1);
 2551:                 }
 2552:                 default_monitor = 0;
 2553:                 break;
 2554:             case QEMU_OPTION_chardev:
 2555:                 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
 2556:                 if (!opts) {
 2557:                     exit(1);
 2558:                 }
 2559:                 break;
 2560:             case QEMU_OPTION_fsdev:
 2561:                 olist = qemu_find_opts("fsdev");
 2562:                 if (!olist) {
 2563:                     fprintf(stderr, "fsdev is not supported by this qemu build.\n");
 2564:                     exit(1);
 2565:                 }
 2566:                 opts = qemu_opts_parse(olist, optarg, 1);
 2567:                 if (!opts) {
 2568:                     fprintf(stderr, "parse error: %s\n", optarg);
 2569:                     exit(1);
 2570:                 }
 2571:                 break;
 2572:             case QEMU_OPTION_virtfs: {
 2573:                 QemuOpts *fsdev;
 2574:                 QemuOpts *device;
 2575: 
 2576:                 olist = qemu_find_opts("virtfs");
 2577:                 if (!olist) {
 2578:                     fprintf(stderr, "virtfs is not supported by this qemu build.\n");
 2579:                     exit(1);
 2580:                 }
 2581:                 opts = qemu_opts_parse(olist, optarg, 1);
 2582:                 if (!opts) {
 2583:                     fprintf(stderr, "parse error: %s\n", optarg);
 2584:                     exit(1);
 2585:                 }
 2586: 
 2587:                 if (qemu_opt_get(opts, "fstype") == NULL ||
 2588:                         qemu_opt_get(opts, "mount_tag") == NULL ||
 2589:                         qemu_opt_get(opts, "path") == NULL ||
 2590:                         qemu_opt_get(opts, "security_model") == NULL) {
 2591:                     fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
 2592:                             "security_model=[mapped|passthrough|none],"
 2593:                             "mount_tag=tag.\n");
 2594:                     exit(1);
 2595:                 }
 2596: 
 2597:                 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
 2598:                                          qemu_opt_get(opts, "mount_tag"), 1);
 2599:                 if (!fsdev) {
 2600:                     fprintf(stderr, "duplicate fsdev id: %s\n",
 2601:                             qemu_opt_get(opts, "mount_tag"));
 2602:                     exit(1);
 2603:                 }
 2604:                 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
 2605:                 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
 2606:                 qemu_opt_set(fsdev, "security_model",
 2607:                              qemu_opt_get(opts, "security_model"));
 2608: 
 2609:                 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
 2610:                 qemu_opt_set(device, "driver", "virtio-9p-pci");
 2611:                 qemu_opt_set(device, "fsdev",
 2612:                              qemu_opt_get(opts, "mount_tag"));
 2613:                 qemu_opt_set(device, "mount_tag",
 2614:                              qemu_opt_get(opts, "mount_tag"));
 2615:                 break;
 2616:             }
 2617:             case QEMU_OPTION_serial:
 2618:                 add_device_config(DEV_SERIAL, optarg);
 2619:                 default_serial = 0;
 2620:                 if (strncmp(optarg, "mon:", 4) == 0) {
 2621:                     default_monitor = 0;
 2622:                 }
 2623:                 break;
 2624:             case QEMU_OPTION_watchdog:
 2625:                 if (watchdog) {
 2626:                     fprintf(stderr,
 2627:                             "qemu: only one watchdog option may be given\n");
 2628:                     return 1;
 2629:                 }
 2630:                 watchdog = optarg;
 2631:                 break;
 2632:             case QEMU_OPTION_watchdog_action:
 2633:                 if (select_watchdog_action(optarg) == -1) {
 2634:                     fprintf(stderr, "Unknown -watchdog-action parameter\n");
 2635:                     exit(1);
 2636:                 }
 2637:                 break;
 2638:             case QEMU_OPTION_virtiocon:
 2639:                 add_device_config(DEV_VIRTCON, optarg);
 2640:                 default_virtcon = 0;
 2641:                 if (strncmp(optarg, "mon:", 4) == 0) {
 2642:                     default_monitor = 0;
 2643:                 }
 2644:                 break;
 2645:             case QEMU_OPTION_parallel:
 2646:                 add_device_config(DEV_PARALLEL, optarg);
 2647:                 default_parallel = 0;
 2648:                 if (strncmp(optarg, "mon:", 4) == 0) {
 2649:                     default_monitor = 0;
 2650:                 }
 2651:                 break;
 2652:             case QEMU_OPTION_debugcon:
 2653:                 add_device_config(DEV_DEBUGCON, optarg);
 2654:                 break;
 2655: 	    case QEMU_OPTION_loadvm:
 2656: 		loadvm = optarg;
 2657: 		break;
 2658:             case QEMU_OPTION_full_screen:
 2659:                 full_screen = 1;
 2660:                 break;
 2661: #ifdef CONFIG_SDL
 2662:             case QEMU_OPTION_no_frame:
 2663:                 no_frame = 1;
 2664:                 break;
 2665:             case QEMU_OPTION_alt_grab:
 2666:                 alt_grab = 1;
 2667:                 break;
 2668:             case QEMU_OPTION_ctrl_grab:
 2669:                 ctrl_grab = 1;
 2670:                 break;
 2671:             case QEMU_OPTION_no_quit:
 2672:                 no_quit = 1;
 2673:                 break;
 2674:             case QEMU_OPTION_sdl:
 2675:                 display_type = DT_SDL;
 2676:                 break;
 2677: #else
 2678:             case QEMU_OPTION_no_frame:
 2679:             case QEMU_OPTION_alt_grab:
 2680:             case QEMU_OPTION_ctrl_grab:
 2681:             case QEMU_OPTION_no_quit:
 2682:             case QEMU_OPTION_sdl:
 2683:                 fprintf(stderr, "SDL support is disabled\n");
 2684:                 exit(1);
 2685: #endif
 2686:             case QEMU_OPTION_pidfile:
 2687:                 pid_file = optarg;
 2688:                 break;
 2689:             case QEMU_OPTION_win2k_hack:
 2690:                 win2k_install_hack = 1;
 2691:                 break;
 2692:             case QEMU_OPTION_rtc_td_hack:
 2693:                 rtc_td_hack = 1;
 2694:                 break;
 2695:             case QEMU_OPTION_acpitable:
 2696:                 do_acpitable_option(optarg);
 2697:                 break;
 2698:             case QEMU_OPTION_smbios:
 2699:                 do_smbios_option(optarg);
 2700:                 break;
 2701:             case QEMU_OPTION_enable_kvm:
 2702:                 olist = qemu_find_opts("machine");
 2703:                 qemu_opts_reset(olist);
 2704:                 qemu_opts_parse(olist, "accel=kvm", 0);
 2705:                 break;
 2706:             case QEMU_OPTION_machine:
 2707:                 olist = qemu_find_opts("machine");
 2708:                 qemu_opts_reset(olist);
 2709:                 opts = qemu_opts_parse(olist, optarg, 1);
 2710:                 if (!opts) {
 2711:                     fprintf(stderr, "parse error: %s\n", optarg);
 2712:                     exit(1);
 2713:                 }
 2714:                 machine = machine_parse(qemu_opt_get(opts, "type"));
 2715:                 break;
 2716:             case QEMU_OPTION_usb:
 2717:                 usb_enabled = 1;
 2718:                 break;
 2719:             case QEMU_OPTION_usbdevice:
 2720:                 usb_enabled = 1;
 2721:                 add_device_config(DEV_USB, optarg);
 2722:                 break;
 2723:             case QEMU_OPTION_device:
 2724:                 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
 2725:                     exit(1);
 2726:                 }
 2727:                 break;
 2728:             case QEMU_OPTION_smp:
 2729:                 smp_parse(optarg);
 2730:                 if (smp_cpus < 1) {
 2731:                     fprintf(stderr, "Invalid number of CPUs\n");
 2732:                     exit(1);
 2733:                 }
 2734:                 if (max_cpus < smp_cpus) {
 2735:                     fprintf(stderr, "maxcpus must be equal to or greater than "
 2736:                             "smp\n");
 2737:                     exit(1);
 2738:                 }
 2739:                 if (max_cpus > 255) {
 2740:                     fprintf(stderr, "Unsupported number of maxcpus\n");
 2741:                     exit(1);
 2742:                 }
 2743:                 break;
 2744: 	    case QEMU_OPTION_vnc:
 2745: #ifdef CONFIG_VNC
 2746:                 display_remote++;
 2747:                 vnc_display = optarg;
 2748: #else
 2749:                 fprintf(stderr, "VNC support is disabled\n");
 2750:                 exit(1);
 2751: #endif
 2752:                 break;
 2753:             case QEMU_OPTION_no_acpi:
 2754:                 acpi_enabled = 0;
 2755:                 break;
 2756:             case QEMU_OPTION_no_hpet:
 2757:                 no_hpet = 1;
 2758:                 break;
 2759:             case QEMU_OPTION_balloon:
 2760:                 if (balloon_parse(optarg) < 0) {
 2761:                     fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
 2762:                     exit(1);
 2763:                 }
 2764:                 break;
 2765:             case QEMU_OPTION_no_reboot:
 2766:                 no_reboot = 1;
 2767:                 break;
 2768:             case QEMU_OPTION_no_shutdown:
 2769:                 no_shutdown = 1;
 2770:                 break;
 2771:             case QEMU_OPTION_show_cursor:
 2772:                 cursor_hide = 0;
 2773:                 break;
 2774:             case QEMU_OPTION_uuid:
 2775:                 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
 2776:                     fprintf(stderr, "Fail to parse UUID string."
 2777:                             " Wrong format.\n");
 2778:                     exit(1);
 2779:                 }
 2780:                 break;
 2781: 	    case QEMU_OPTION_option_rom:
 2782: 		if (nb_option_roms >= MAX_OPTION_ROMS) {
 2783: 		    fprintf(stderr, "Too many option ROMs\n");
 2784: 		    exit(1);
 2785: 		}
 2786:                 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
 2787:                 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
 2788:                 option_rom[nb_option_roms].bootindex =
 2789:                     qemu_opt_get_number(opts, "bootindex", -1);
 2790:                 if (!option_rom[nb_option_roms].name) {
 2791:                     fprintf(stderr, "Option ROM file is not specified\n");
 2792:                     exit(1);
 2793:                 }
 2794: 		nb_option_roms++;
 2795: 		break;
 2796:             case QEMU_OPTION_semihosting:
 2797:                 semihosting_enabled = 1;
 2798:                 break;
 2799:             case QEMU_OPTION_name:
 2800:                 qemu_name = qemu_strdup(optarg);
 2801: 		 {
 2802: 		     char *p = strchr(qemu_name, ',');
 2803: 		     if (p != NULL) {
 2804: 		        *p++ = 0;
 2805: 			if (strncmp(p, "process=", 8)) {
 2806: 			    fprintf(stderr, "Unknown subargument %s to -name\n", p);
 2807: 			    exit(1);
 2808: 			}
 2809: 			p += 8;
 2810: 			os_set_proc_name(p);
 2811: 		     }	
 2812: 		 }	
 2813:                 break;
 2814:             case QEMU_OPTION_prom_env:
 2815:                 if (nb_prom_envs >= MAX_PROM_ENVS) {
 2816:                     fprintf(stderr, "Too many prom variables\n");
 2817:                     exit(1);
 2818:                 }
 2819:                 prom_envs[nb_prom_envs] = optarg;
 2820:                 nb_prom_envs++;
 2821:                 break;
 2822:             case QEMU_OPTION_old_param:
 2823:                 old_param = 1;
 2824:                 break;
 2825:             case QEMU_OPTION_clock:
 2826:                 configure_alarms(optarg);
 2827:                 break;
 2828:             case QEMU_OPTION_startdate:
 2829:                 configure_rtc_date_offset(optarg, 1);
 2830:                 break;
 2831:             case QEMU_OPTION_rtc:
 2832:                 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
 2833:                 if (!opts) {
 2834:                     exit(1);
 2835:                 }
 2836:                 configure_rtc(opts);
 2837:                 break;
 2838:             case QEMU_OPTION_tb_size:
 2839:                 tb_size = strtol(optarg, NULL, 0);
 2840:                 if (tb_size < 0)
 2841:                     tb_size = 0;
 2842:                 break;
 2843:             case QEMU_OPTION_icount:
 2844:                 icount_option = optarg;
 2845:                 break;
 2846:             case QEMU_OPTION_incoming:
 2847:                 incoming = optarg;
 2848:                 incoming_expected = true;
 2849:                 break;
 2850:             case QEMU_OPTION_nodefaults:
 2851:                 default_serial = 0;
 2852:                 default_parallel = 0;
 2853:                 default_virtcon = 0;
 2854:                 default_monitor = 0;
 2855:                 default_vga = 0;
 2856:                 default_net = 0;
 2857:                 default_floppy = 0;
 2858:                 default_cdrom = 0;
 2859:                 default_sdcard = 0;
 2860:                 break;
 2861:             case QEMU_OPTION_xen_domid:
 2862:                 if (!(xen_available())) {
 2863:                     printf("Option %s not supported for this target\n", popt->name);
 2864:                     exit(1);
 2865:                 }
 2866:                 xen_domid = atoi(optarg);
 2867:                 break;
 2868:             case QEMU_OPTION_xen_create:
 2869:                 if (!(xen_available())) {
 2870:                     printf("Option %s not supported for this target\n", popt->name);
 2871:                     exit(1);
 2872:                 }
 2873:                 xen_mode = XEN_CREATE;
 2874:                 break;
 2875:             case QEMU_OPTION_xen_attach:
 2876:                 if (!(xen_available())) {
 2877:                     printf("Option %s not supported for this target\n", popt->name);
 2878:                     exit(1);
 2879:                 }
 2880:                 xen_mode = XEN_ATTACH;
 2881:                 break;
 2882: #ifdef CONFIG_SIMPLE_TRACE
 2883:             case QEMU_OPTION_trace:
 2884:                 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
 2885:                 if (opts) {
 2886:                     trace_file = qemu_opt_get(opts, "file");
 2887:                 }
 2888:                 break;
 2889: #endif
 2890:             case QEMU_OPTION_readconfig:
 2891:                 {
 2892:                     int ret = qemu_read_config_file(optarg);
 2893:                     if (ret < 0) {
 2894:                         fprintf(stderr, "read config %s: %s\n", optarg,
 2895:                             strerror(-ret));
 2896:                         exit(1);
 2897:                     }
 2898:                     break;
 2899:                 }
 2900:             case QEMU_OPTION_spice:
 2901:                 olist = qemu_find_opts("spice");
 2902:                 if (!olist) {
 2903:                     fprintf(stderr, "spice is not supported by this qemu build.\n");
 2904:                     exit(1);
 2905:                 }
 2906:                 opts = qemu_opts_parse(olist, optarg, 0);
 2907:                 if (!opts) {
 2908:                     fprintf(stderr, "parse error: %s\n", optarg);
 2909:                     exit(1);
 2910:                 }
 2911:                 break;
 2912:             case QEMU_OPTION_writeconfig:
 2913:                 {
 2914:                     FILE *fp;
 2915:                     if (strcmp(optarg, "-") == 0) {
 2916:                         fp = stdout;
 2917:                     } else {
 2918:                         fp = fopen(optarg, "w");
 2919:                         if (fp == NULL) {
 2920:                             fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
 2921:                             exit(1);
 2922:                         }
 2923:                     }
 2924:                     qemu_config_write(fp);
 2925:                     fclose(fp);
 2926:                     break;
 2927:                 }
 2928:             default:
 2929:                 os_parse_cmd_args(popt->index, optarg);
 2930:             }
 2931:         }
 2932:     }
 2933:     loc_set_none();
 2934: 
 2935:     /* Open the logfile at this point, if necessary. We can't open the logfile
 2936:      * when encountering either of the logging options (-d or -D) because the
 2937:      * other one may be encountered later on the command line, changing the
 2938:      * location or level of logging.
 2939:      */
 2940:     if (log_mask) {
 2941:         if (log_file) {
 2942:             set_cpu_log_filename(log_file);
 2943:         }
 2944:         set_cpu_log(log_mask);
 2945:     }
 2946: 
 2947:     if (!st_init(trace_file)) {
 2948:         fprintf(stderr, "warning: unable to initialize simple trace backend\n");
 2949:     }
 2950: 
 2951:     /* If no data_dir is specified then try to find it relative to the
 2952:        executable path.  */
 2953:     if (!data_dir) {
 2954:         data_dir = os_find_datadir(argv[0]);
 2955:     }
 2956:     /* If all else fails use the install patch specified when building.  */
 2957:     if (!data_dir) {
 2958:         data_dir = CONFIG_QEMU_DATADIR;
 2959:     }
 2960: 
 2961:     /*
 2962:      * Default to max_cpus = smp_cpus, in case the user doesn't
 2963:      * specify a max_cpus value.
 2964:      */
 2965:     if (!max_cpus)
 2966:         max_cpus = smp_cpus;
 2967: 
 2968:     machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
 2969:     if (smp_cpus > machine->max_cpus) {
 2970:         fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
 2971:                 "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
 2972:                 machine->max_cpus);
 2973:         exit(1);
 2974:     }
 2975: 
 2976:     /*
 2977:      * Get the default machine options from the machine if it is not already
 2978:      * specified either by the configuration file or by the command line.
 2979:      */
 2980:     if (machine->default_machine_opts) {
 2981:         QemuOptsList *list = qemu_find_opts("machine");
 2982:         const char *p = NULL;
 2983: 
 2984:         if (!QTAILQ_EMPTY(&list->head)) {
 2985:             p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
 2986:         }
 2987:         if (p == NULL) {
 2988:             qemu_opts_reset(list);
 2989:             opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
 2990:             if (!opts) {
 2991:                 fprintf(stderr, "parse error for machine %s: %s\n",
 2992:                         machine->name, machine->default_machine_opts);
 2993:                 exit(1);
 2994:             }
 2995:         }
 2996:     }
 2997: 
 2998:     qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
 2999:     qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
 3000: 
 3001:     if (machine->no_serial) {
 3002:         default_serial = 0;
 3003:     }
 3004:     if (machine->no_parallel) {
 3005:         default_parallel = 0;
 3006:     }
 3007:     if (!machine->use_virtcon) {
 3008:         default_virtcon = 0;
 3009:     }
 3010:     if (machine->no_vga) {
 3011:         default_vga = 0;
 3012:     }
 3013:     if (machine->no_floppy) {
 3014:         default_floppy = 0;
 3015:     }
 3016:     if (machine->no_cdrom) {
 3017:         default_cdrom = 0;
 3018:     }
 3019:     if (machine->no_sdcard) {
 3020:         default_sdcard = 0;
 3021:     }
 3022: 
 3023:     if (display_type == DT_NOGRAPHIC) {
 3024:         if (default_parallel)
 3025:             add_device_config(DEV_PARALLEL, "null");
 3026:         if (default_serial && default_monitor) {
 3027:             add_device_config(DEV_SERIAL, "mon:stdio");
 3028:         } else if (default_virtcon && default_monitor) {
 3029:             add_device_config(DEV_VIRTCON, "mon:stdio");
 3030:         } else {
 3031:             if (default_serial)
 3032:                 add_device_config(DEV_SERIAL, "stdio");
 3033:             if (default_virtcon)
 3034:                 add_device_config(DEV_VIRTCON, "stdio");
 3035:             if (default_monitor)
 3036:                 monitor_parse("stdio", "readline");
 3037:         }
 3038:     } else {
 3039:         if (default_serial)
 3040:             add_device_config(DEV_SERIAL, "vc:80Cx24C");
 3041:         if (default_parallel)
 3042:             add_device_config(DEV_PARALLEL, "vc:80Cx24C");
 3043:         if (default_monitor)
 3044:             monitor_parse("vc:80Cx24C", "readline");
 3045:         if (default_virtcon)
 3046:             add_device_config(DEV_VIRTCON, "vc:80Cx24C");
 3047:     }
 3048:     if (default_vga)
 3049:         vga_interface_type = VGA_CIRRUS;
 3050: 
 3051:     socket_init();
 3052: 
 3053:     if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
 3054:         exit(1);
 3055: #ifdef CONFIG_VIRTFS
 3056:     if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
 3057:         exit(1);
 3058:     }
 3059: #endif
 3060: 
 3061:     os_daemonize();
 3062: 
 3063:     if (pid_file && qemu_create_pidfile(pid_file) != 0) {
 3064:         os_pidfile_error();
 3065:         exit(1);
 3066:     }
 3067: 
 3068:     configure_accelerator();
 3069: 
 3070:     if (qemu_init_main_loop()) {
 3071:         fprintf(stderr, "qemu_init_main_loop failed\n");
 3072:         exit(1);
 3073:     }
 3074:     linux_boot = (kernel_filename != NULL);
 3075: 
 3076:     if (!linux_boot && *kernel_cmdline != '\0') {
 3077:         fprintf(stderr, "-append only allowed with -kernel option\n");
 3078:         exit(1);
 3079:     }
 3080: 
 3081:     if (!linux_boot && initrd_filename != NULL) {
 3082:         fprintf(stderr, "-initrd only allowed with -kernel option\n");
 3083:         exit(1);
 3084:     }
 3085: 
 3086:     os_set_line_buffering();
 3087: 
 3088:     if (init_timer_alarm() < 0) {
 3089:         fprintf(stderr, "could not initialize alarm timer\n");
 3090:         exit(1);
 3091:     }
 3092:     configure_icount(icount_option);
 3093: 
 3094:     if (net_init_clients() < 0) {
 3095:         exit(1);
 3096:     }
 3097: 
 3098:     /* init the bluetooth world */
 3099:     if (foreach_device_config(DEV_BT, bt_parse))
 3100:         exit(1);
 3101: 
 3102:     /* init the memory */
 3103:     if (ram_size == 0)
 3104:         ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
 3105: 
 3106:     /* init the dynamic translator */
 3107:     cpu_exec_init_all(tb_size * 1024 * 1024);
 3108: 
 3109:     bdrv_init_with_whitelist();
 3110: 
 3111:     blk_mig_init();
 3112: 
 3113:     /* open the virtual block devices */
 3114:     if (snapshot)
 3115:         qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
 3116:     if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
 3117:         exit(1);
 3118: 
 3119:     default_drive(default_cdrom, snapshot, machine->use_scsi,
 3120:                   IF_DEFAULT, 2, CDROM_OPTS);
 3121:     default_drive(default_floppy, snapshot, machine->use_scsi,
 3122:                   IF_FLOPPY, 0, FD_OPTS);
 3123:     default_drive(default_sdcard, snapshot, machine->use_scsi,
 3124:                   IF_SD, 0, SD_OPTS);
 3125: 
 3126:     register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
 3127:                          ram_load, NULL);
 3128: 
 3129:     if (nb_numa_nodes > 0) {
 3130:         int i;
 3131: 
 3132:         if (nb_numa_nodes > MAX_NODES) {
 3133:             nb_numa_nodes = MAX_NODES;
 3134:         }
 3135: 
 3136:         /* If no memory size if given for any node, assume the default case
 3137:          * and distribute the available memory equally across all nodes
 3138:          */
 3139:         for (i = 0; i < nb_numa_nodes; i++) {
 3140:             if (node_mem[i] != 0)
 3141:                 break;
 3142:         }
 3143:         if (i == nb_numa_nodes) {
 3144:             uint64_t usedmem = 0;
 3145: 
 3146:             /* On Linux, the each node's border has to be 8MB aligned,
 3147:              * the final node gets the rest.
 3148:              */
 3149:             for (i = 0; i < nb_numa_nodes - 1; i++) {
 3150:                 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
 3151:                 usedmem += node_mem[i];
 3152:             }
 3153:             node_mem[i] = ram_size - usedmem;
 3154:         }
 3155: 
 3156:         for (i = 0; i < nb_numa_nodes; i++) {
 3157:             if (node_cpumask[i] != 0)
 3158:                 break;
 3159:         }
 3160:         /* assigning the VCPUs round-robin is easier to implement, guest OSes
 3161:          * must cope with this anyway, because there are BIOSes out there in
 3162:          * real machines which also use this scheme.
 3163:          */
 3164:         if (i == nb_numa_nodes) {
 3165:             for (i = 0; i < smp_cpus; i++) {
 3166:                 node_cpumask[i % nb_numa_nodes] |= 1 << i;
 3167:             }
 3168:         }
 3169:     }
 3170: 
 3171:     if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
 3172:         exit(1);
 3173:     }
 3174: 
 3175:     if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
 3176:         exit(1);
 3177:     if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
 3178:         exit(1);
 3179:     if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
 3180:         exit(1);
 3181:     if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
 3182:         exit(1);
 3183: 
 3184:     module_call_init(MODULE_INIT_DEVICE);
 3185: 
 3186:     if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
 3187:         exit(0);
 3188: 
 3189:     if (watchdog) {
 3190:         i = select_watchdog(watchdog);
 3191:         if (i > 0)
 3192:             exit (i == 1 ? 1 : 0);
 3193:     }
 3194: 
 3195:     if (machine->compat_props) {
 3196:         qdev_prop_register_global_list(machine->compat_props);
 3197:     }
 3198:     qemu_add_globals();
 3199: 
 3200:     machine->init(ram_size, boot_devices,
 3201:                   kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
 3202: 
 3203:     cpu_synchronize_all_post_init();
 3204: 
 3205:     set_numa_modes();
 3206: 
 3207:     current_machine = machine;
 3208: 
 3209:     /* init USB devices */
 3210:     if (usb_enabled) {
 3211:         if (foreach_device_config(DEV_USB, usb_parse) < 0)
 3212:             exit(1);
 3213:     }
 3214: 
 3215:     /* init generic devices */
 3216:     if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
 3217:         exit(1);
 3218: 
 3219:     net_check_clients();
 3220: 
 3221:     /* just use the first displaystate for the moment */
 3222:     ds = get_displaystate();
 3223: 
 3224:     if (using_spice)
 3225:         display_remote++;
 3226:     if (display_type == DT_DEFAULT && !display_remote) {
 3227: #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
 3228:         display_type = DT_SDL;
 3229: #elif defined(CONFIG_VNC)
 3230:         vnc_display = "localhost:0,to=99";
 3231:         show_vnc_port = 1;
 3232: #else
 3233:         display_type = DT_NONE;
 3234: #endif
 3235:     }
 3236: 
 3237: 
 3238:     /* init local displays */
 3239:     switch (display_type) {
 3240:     case DT_NOGRAPHIC:
 3241:         break;
 3242: #if defined(CONFIG_CURSES)
 3243:     case DT_CURSES:
 3244:         curses_display_init(ds, full_screen);
 3245:         break;
 3246: #endif
 3247: #if defined(CONFIG_SDL)
 3248:     case DT_SDL:
 3249:         sdl_display_init(ds, full_screen, no_frame);
 3250:         break;
 3251: #elif defined(CONFIG_COCOA)
 3252:     case DT_SDL:
 3253:         cocoa_display_init(ds, full_screen);
 3254:         break;
 3255: #endif
 3256:     default:
 3257:         break;
 3258:     }
 3259: 
 3260:     /* must be after terminal init, SDL library changes signal handlers */
 3261:     os_setup_signal_handling();
 3262: 
 3263: #ifdef CONFIG_VNC
 3264:     /* init remote displays */
 3265:     if (vnc_display) {
 3266:         vnc_display_init(ds);
 3267:         if (vnc_display_open(ds, vnc_display) < 0)
 3268:             exit(1);
 3269: 
 3270:         if (show_vnc_port) {
 3271:             printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
 3272:         }
 3273:     }
 3274: #endif
 3275: #ifdef CONFIG_SPICE
 3276:     if (using_spice && !qxl_enabled) {
 3277:         qemu_spice_display_init(ds);
 3278:     }
 3279: #endif
 3280: 
 3281:     /* display setup */
 3282:     dpy_resize(ds);
 3283:     dcl = ds->listeners;
 3284:     while (dcl != NULL) {
 3285:         if (dcl->dpy_refresh != NULL) {
 3286:             ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
 3287:             qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
 3288:             break;
 3289:         }
 3290:         dcl = dcl->next;
 3291:     }
 3292:     if (ds->gui_timer == NULL) {
 3293:         nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
 3294:         qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
 3295:     }
 3296:     text_consoles_set_display(ds);
 3297: 
 3298:     if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
 3299:         fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
 3300:                 gdbstub_dev);
 3301:         exit(1);
 3302:     }
 3303: 
 3304:     qdev_machine_creation_done();
 3305: 
 3306:     if (rom_load_all() != 0) {
 3307:         fprintf(stderr, "rom loading failed\n");
 3308:         exit(1);
 3309:     }
 3310: 
 3311:     /* TODO: once all bus devices are qdevified, this should be done
 3312:      * when bus is created by qdev.c */
 3313:     qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
 3314:     qemu_run_machine_init_done_notifiers();
 3315: 
 3316:     qemu_system_reset(VMRESET_SILENT);
 3317:     if (loadvm) {
 3318:         if (load_vmstate(loadvm) < 0) {
 3319:             autostart = 0;
 3320:         }
 3321:     }
 3322: 
 3323:     if (incoming) {
 3324:         int ret = qemu_start_incoming_migration(incoming);
 3325:         if (ret < 0) {
 3326:             fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
 3327:                     incoming, ret);
 3328:             exit(ret);
 3329:         }
 3330:     } else if (autostart) {
 3331:         vm_start();
 3332:     }
 3333: 
 3334:     os_setup_post();
 3335: 
 3336:     main_loop();
 3337:     quit_timers();
 3338:     net_cleanup();
 3339: 
 3340:     return 0;
 3341: }

unix.superglobalmegacorp.com