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