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