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