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