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