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