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