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