]>
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> |
24646c7e BS |
55 | #else |
56 | #include <util.h> | |
128ab2ff | 57 | #endif |
bbe813a2 | 58 | #else |
223f0d72 | 59 | #ifdef __linux__ |
7d3505c5 FB |
60 | #include <pty.h> |
61 | #include <malloc.h> | |
1889465a | 62 | #include <sys/prctl.h> |
bd494f4c | 63 | |
e57a8c0e | 64 | #include <linux/ppdev.h> |
5867c88a | 65 | #include <linux/parport.h> |
223f0d72 BS |
66 | #endif |
67 | #ifdef __sun__ | |
d5d10bc3 TS |
68 | #include <sys/stat.h> |
69 | #include <sys/ethernet.h> | |
70 | #include <sys/sockio.h> | |
d5d10bc3 TS |
71 | #include <netinet/arp.h> |
72 | #include <netinet/in.h> | |
73 | #include <netinet/in_systm.h> | |
74 | #include <netinet/ip.h> | |
75 | #include <netinet/ip_icmp.h> // must come after ip.h | |
76 | #include <netinet/udp.h> | |
77 | #include <netinet/tcp.h> | |
78 | #include <net/if.h> | |
79 | #include <syslog.h> | |
80 | #include <stropts.h> | |
8d32cf0e BS |
81 | /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for |
82 | discussion about Solaris header problems */ | |
83 | extern int madvise(caddr_t, size_t, int); | |
67b915a5 | 84 | #endif |
7d3505c5 | 85 | #endif |
ec530c81 | 86 | #endif |
67b915a5 | 87 | |
9892fbfb BS |
88 | #if defined(__OpenBSD__) |
89 | #include <util.h> | |
90 | #endif | |
91 | ||
8a16d273 TS |
92 | #if defined(CONFIG_VDE) |
93 | #include <libvdeplug.h> | |
94 | #endif | |
95 | ||
67b915a5 | 96 | #ifdef _WIN32 |
49dc768d | 97 | #include <windows.h> |
67b915a5 FB |
98 | #endif |
99 | ||
73332e5c | 100 | #ifdef CONFIG_SDL |
59a36a2f | 101 | #if defined(__APPLE__) || defined(main) |
6693665a | 102 | #include <SDL.h> |
880fec5d | 103 | int qemu_main(int argc, char **argv, char **envp); |
104 | int main(int argc, char **argv) | |
105 | { | |
59a36a2f | 106 | return qemu_main(argc, argv, NULL); |
880fec5d | 107 | } |
108 | #undef main | |
109 | #define main qemu_main | |
96bcd4f8 | 110 | #endif |
73332e5c | 111 | #endif /* CONFIG_SDL */ |
0824d6fc | 112 | |
5b0753e0 FB |
113 | #ifdef CONFIG_COCOA |
114 | #undef main | |
115 | #define main qemu_main | |
116 | #endif /* CONFIG_COCOA */ | |
117 | ||
511d2b14 BS |
118 | #include "hw/hw.h" |
119 | #include "hw/boards.h" | |
120 | #include "hw/usb.h" | |
121 | #include "hw/pcmcia.h" | |
122 | #include "hw/pc.h" | |
123 | #include "hw/audiodev.h" | |
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 | |
8a7ddc38 | 156 | #include "exec-all.h" |
0824d6fc | 157 | |
511d2b14 BS |
158 | #include "qemu_socket.h" |
159 | ||
d918f23e | 160 | #include "slirp/libslirp.h" |
511d2b14 | 161 | |
72cf2d4f BS |
162 | #include "qemu-queue.h" |
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; |
d827220b FB |
193 | #ifdef TARGET_SPARC |
194 | int graphic_width = 1024; | |
195 | int graphic_height = 768; | |
eee0b836 | 196 | int graphic_depth = 8; |
d827220b | 197 | #else |
1bfe856e FB |
198 | int graphic_width = 800; |
199 | int graphic_height = 600; | |
e9b137c2 | 200 | int graphic_depth = 15; |
eee0b836 | 201 | #endif |
dbed7e40 | 202 | static int full_screen = 0; |
634a21f6 | 203 | #ifdef CONFIG_SDL |
dbed7e40 | 204 | static int no_frame = 0; |
634a21f6 | 205 | #endif |
667accab | 206 | int no_quit = 0; |
8d11df9e | 207 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
6508fe59 | 208 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
9ede2fde | 209 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
a09db21f | 210 | int win2k_install_hack = 0; |
f7736b91 | 211 | #ifdef TARGET_I386 |
73822ec8 | 212 | int rtc_td_hack = 0; |
a09db21f | 213 | #endif |
bb36d470 | 214 | int usb_enabled = 0; |
1b530a6d | 215 | int singlestep = 0; |
6a00d601 | 216 | int smp_cpus = 1; |
6be68d7e | 217 | int max_cpus = 0; |
dc6b1c09 AP |
218 | int smp_cores = 1; |
219 | int smp_threads = 1; | |
73fc9742 | 220 | const char *vnc_display; |
6515b203 | 221 | int acpi_enabled = 1; |
16b29ae1 | 222 | int no_hpet = 0; |
52ca8d6a | 223 | int fd_bootchk = 1; |
d1beab82 | 224 | int no_reboot = 0; |
b2f76161 | 225 | int no_shutdown = 0; |
9467cd46 | 226 | int cursor_hide = 1; |
a171fe39 | 227 | int graphic_rotate = 0; |
6b35e7bf | 228 | uint8_t irq0override = 1; |
b9e82a59 | 229 | #ifndef _WIN32 |
71e3ceb8 | 230 | int daemonize = 0; |
b9e82a59 | 231 | #endif |
09aaa160 | 232 | const char *watchdog; |
9ae02555 TS |
233 | const char *option_rom[MAX_OPTION_ROMS]; |
234 | int nb_option_roms; | |
8e71621f | 235 | int semihosting_enabled = 0; |
2b8f2d41 AZ |
236 | #ifdef TARGET_ARM |
237 | int old_param = 0; | |
238 | #endif | |
c35734b2 | 239 | const char *qemu_name; |
3780e197 | 240 | int alt_grab = 0; |
0ca9f8a4 | 241 | int ctrl_grab = 0; |
95efd11c | 242 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
66508601 BS |
243 | unsigned int nb_prom_envs = 0; |
244 | const char *prom_envs[MAX_PROM_ENVS]; | |
245 | #endif | |
95387491 | 246 | int boot_menu; |
0824d6fc | 247 | |
268a362c AL |
248 | int nb_numa_nodes; |
249 | uint64_t node_mem[MAX_NODES]; | |
250 | uint64_t node_cpumask[MAX_NODES]; | |
251 | ||
ee5605e5 AZ |
252 | static CPUState *cur_cpu; |
253 | static CPUState *next_cpu; | |
9043b62d | 254 | static QEMUTimer *nographic_timer; |
ee5605e5 | 255 | |
8fcb1b90 BS |
256 | uint8_t qemu_uuid[16]; |
257 | ||
76e30d0f JK |
258 | static QEMUBootSetHandler *boot_set_handler; |
259 | static void *boot_set_opaque; | |
260 | ||
cc84de95 MT |
261 | #ifdef SIGRTMIN |
262 | #define SIG_IPI (SIGRTMIN+4) | |
263 | #else | |
264 | #define SIG_IPI SIGUSR1 | |
265 | #endif | |
266 | ||
998bbd74 | 267 | static int default_serial = 1; |
6a5e8b0e | 268 | static int default_parallel = 1; |
986c5f78 | 269 | static int default_virtcon = 1; |
abdeed06 | 270 | static int default_monitor = 1; |
64465297 | 271 | static int default_vga = 1; |
ac33f8fa GH |
272 | static int default_floppy = 1; |
273 | static int default_cdrom = 1; | |
274 | static int default_sdcard = 1; | |
998bbd74 GH |
275 | |
276 | static struct { | |
277 | const char *driver; | |
278 | int *flag; | |
279 | } default_list[] = { | |
6a5e8b0e GH |
280 | { .driver = "isa-serial", .flag = &default_serial }, |
281 | { .driver = "isa-parallel", .flag = &default_parallel }, | |
d8bcbabf GH |
282 | { .driver = "isa-fdc", .flag = &default_floppy }, |
283 | { .driver = "ide-drive", .flag = &default_cdrom }, | |
392ecf54 AS |
284 | { .driver = "virtio-serial-pci", .flag = &default_virtcon }, |
285 | { .driver = "virtio-serial-s390", .flag = &default_virtcon }, | |
286 | { .driver = "virtio-serial", .flag = &default_virtcon }, | |
64465297 | 287 | { .driver = "VGA", .flag = &default_vga }, |
69fd02ee GH |
288 | { .driver = "cirrus-vga", .flag = &default_vga }, |
289 | { .driver = "vmware-svga", .flag = &default_vga }, | |
998bbd74 GH |
290 | }; |
291 | ||
292 | static int default_driver_check(QemuOpts *opts, void *opaque) | |
293 | { | |
294 | const char *driver = qemu_opt_get(opts, "driver"); | |
295 | int i; | |
296 | ||
297 | if (!driver) | |
298 | return 0; | |
299 | for (i = 0; i < ARRAY_SIZE(default_list); i++) { | |
300 | if (strcmp(default_list[i].driver, driver) != 0) | |
301 | continue; | |
302 | *(default_list[i].flag) = 0; | |
303 | } | |
304 | return 0; | |
305 | } | |
306 | ||
0824d6fc | 307 | /***********************************************************/ |
26aa7d72 FB |
308 | /* x86 ISA bus support */ |
309 | ||
c227f099 | 310 | target_phys_addr_t isa_mem_base = 0; |
3de388f6 | 311 | PicState2 *isa_pic; |
0824d6fc | 312 | |
0824d6fc | 313 | /***********************************************************/ |
0824d6fc FB |
314 | void hw_error(const char *fmt, ...) |
315 | { | |
316 | va_list ap; | |
6a00d601 | 317 | CPUState *env; |
0824d6fc FB |
318 | |
319 | va_start(ap, fmt); | |
320 | fprintf(stderr, "qemu: hardware error: "); | |
321 | vfprintf(stderr, fmt, ap); | |
322 | fprintf(stderr, "\n"); | |
6a00d601 FB |
323 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
324 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); | |
0824d6fc | 325 | #ifdef TARGET_I386 |
6a00d601 | 326 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
c45886db | 327 | #else |
6a00d601 | 328 | cpu_dump_state(env, stderr, fprintf, 0); |
0824d6fc | 329 | #endif |
6a00d601 | 330 | } |
0824d6fc FB |
331 | va_end(ap); |
332 | abort(); | |
333 | } | |
1889465a AK |
334 | |
335 | static void set_proc_name(const char *s) | |
336 | { | |
6ca8d0fd | 337 | #if defined(__linux__) && defined(PR_SET_NAME) |
1889465a AK |
338 | char name[16]; |
339 | if (!s) | |
340 | return; | |
341 | name[sizeof(name) - 1] = 0; | |
342 | strncpy(name, s, sizeof(name)); | |
343 | /* Could rewrite argv[0] too, but that's a bit more complicated. | |
344 | This simple way is enough for `top'. */ | |
345 | prctl(PR_SET_NAME, name); | |
346 | #endif | |
347 | } | |
df751fa8 AL |
348 | |
349 | /***************/ | |
350 | /* ballooning */ | |
351 | ||
352 | static QEMUBalloonEvent *qemu_balloon_event; | |
353 | void *qemu_balloon_event_opaque; | |
354 | ||
355 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) | |
356 | { | |
357 | qemu_balloon_event = func; | |
358 | qemu_balloon_event_opaque = opaque; | |
359 | } | |
360 | ||
625a5bef | 361 | int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque) |
df751fa8 | 362 | { |
625a5bef AL |
363 | if (qemu_balloon_event) { |
364 | qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque); | |
365 | return 1; | |
366 | } else { | |
367 | return 0; | |
368 | } | |
df751fa8 AL |
369 | } |
370 | ||
625a5bef | 371 | int qemu_balloon_status(MonitorCompletion cb, void *opaque) |
df751fa8 | 372 | { |
625a5bef AL |
373 | if (qemu_balloon_event) { |
374 | qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque); | |
375 | return 1; | |
376 | } else { | |
377 | return 0; | |
378 | } | |
df751fa8 | 379 | } |
0824d6fc | 380 | |
455204eb | 381 | |
8f0056b7 PB |
382 | /***********************************************************/ |
383 | /* real time host monotonic timer */ | |
09b26c5e | 384 | |
1dce7c3c FB |
385 | /* compute with 96 bit intermediate result: (a*b)/c */ |
386 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) | |
0824d6fc | 387 | { |
1dce7c3c FB |
388 | union { |
389 | uint64_t ll; | |
390 | struct { | |
e2542fe2 | 391 | #ifdef HOST_WORDS_BIGENDIAN |
1dce7c3c FB |
392 | uint32_t high, low; |
393 | #else | |
394 | uint32_t low, high; | |
3b46e624 | 395 | #endif |
1dce7c3c FB |
396 | } l; |
397 | } u, res; | |
398 | uint64_t rl, rh; | |
0824d6fc | 399 | |
1dce7c3c FB |
400 | u.ll = a; |
401 | rl = (uint64_t)u.l.low * (uint64_t)b; | |
402 | rh = (uint64_t)u.l.high * (uint64_t)b; | |
403 | rh += (rl >> 32); | |
404 | res.l.high = rh / c; | |
405 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; | |
406 | return res.ll; | |
34865134 FB |
407 | } |
408 | ||
f6503059 AZ |
409 | /***********************************************************/ |
410 | /* host time/date access */ | |
411 | void qemu_get_timedate(struct tm *tm, int offset) | |
412 | { | |
413 | time_t ti; | |
414 | struct tm *ret; | |
415 | ||
416 | time(&ti); | |
417 | ti += offset; | |
418 | if (rtc_date_offset == -1) { | |
419 | if (rtc_utc) | |
420 | ret = gmtime(&ti); | |
421 | else | |
422 | ret = localtime(&ti); | |
423 | } else { | |
424 | ti -= rtc_date_offset; | |
425 | ret = gmtime(&ti); | |
426 | } | |
427 | ||
428 | memcpy(tm, ret, sizeof(struct tm)); | |
429 | } | |
430 | ||
431 | int qemu_timedate_diff(struct tm *tm) | |
432 | { | |
433 | time_t seconds; | |
434 | ||
435 | if (rtc_date_offset == -1) | |
436 | if (rtc_utc) | |
437 | seconds = mktimegm(tm); | |
438 | else | |
439 | seconds = mktime(tm); | |
440 | else | |
441 | seconds = mktimegm(tm) + rtc_date_offset; | |
442 | ||
443 | return seconds - time(NULL); | |
444 | } | |
445 | ||
80cd3478 LC |
446 | void rtc_change_mon_event(struct tm *tm) |
447 | { | |
448 | QObject *data; | |
449 | ||
450 | data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm)); | |
451 | monitor_protocol_event(QEVENT_RTC_CHANGE, data); | |
452 | qobject_decref(data); | |
453 | } | |
454 | ||
1ed2fc1f JK |
455 | static void configure_rtc_date_offset(const char *startdate, int legacy) |
456 | { | |
457 | time_t rtc_start_date; | |
458 | struct tm tm; | |
459 | ||
460 | if (!strcmp(startdate, "now") && legacy) { | |
461 | rtc_date_offset = -1; | |
462 | } else { | |
463 | if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", | |
464 | &tm.tm_year, | |
465 | &tm.tm_mon, | |
466 | &tm.tm_mday, | |
467 | &tm.tm_hour, | |
468 | &tm.tm_min, | |
469 | &tm.tm_sec) == 6) { | |
470 | /* OK */ | |
471 | } else if (sscanf(startdate, "%d-%d-%d", | |
472 | &tm.tm_year, | |
473 | &tm.tm_mon, | |
474 | &tm.tm_mday) == 3) { | |
475 | tm.tm_hour = 0; | |
476 | tm.tm_min = 0; | |
477 | tm.tm_sec = 0; | |
478 | } else { | |
479 | goto date_fail; | |
480 | } | |
481 | tm.tm_year -= 1900; | |
482 | tm.tm_mon--; | |
483 | rtc_start_date = mktimegm(&tm); | |
484 | if (rtc_start_date == -1) { | |
485 | date_fail: | |
486 | fprintf(stderr, "Invalid date format. Valid formats are:\n" | |
487 | "'2006-06-17T16:01:21' or '2006-06-17'\n"); | |
488 | exit(1); | |
489 | } | |
490 | rtc_date_offset = time(NULL) - rtc_start_date; | |
491 | } | |
492 | } | |
493 | ||
494 | static void configure_rtc(QemuOpts *opts) | |
495 | { | |
496 | const char *value; | |
497 | ||
498 | value = qemu_opt_get(opts, "base"); | |
499 | if (value) { | |
500 | if (!strcmp(value, "utc")) { | |
501 | rtc_utc = 1; | |
502 | } else if (!strcmp(value, "localtime")) { | |
503 | rtc_utc = 0; | |
504 | } else { | |
505 | configure_rtc_date_offset(value, 0); | |
506 | } | |
507 | } | |
6875204c JK |
508 | value = qemu_opt_get(opts, "clock"); |
509 | if (value) { | |
510 | if (!strcmp(value, "host")) { | |
511 | rtc_clock = host_clock; | |
512 | } else if (!strcmp(value, "vm")) { | |
513 | rtc_clock = vm_clock; | |
514 | } else { | |
515 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); | |
516 | exit(1); | |
517 | } | |
518 | } | |
1ed2fc1f JK |
519 | #ifdef CONFIG_TARGET_I386 |
520 | value = qemu_opt_get(opts, "driftfix"); | |
521 | if (value) { | |
522 | if (!strcmp(buf, "slew")) { | |
523 | rtc_td_hack = 1; | |
524 | } else if (!strcmp(buf, "none")) { | |
525 | rtc_td_hack = 0; | |
526 | } else { | |
527 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); | |
528 | exit(1); | |
529 | } | |
530 | } | |
531 | #endif | |
532 | } | |
533 | ||
fd1dff4b | 534 | #ifdef _WIN32 |
28695489 | 535 | static void socket_cleanup(void) |
fd1dff4b FB |
536 | { |
537 | WSACleanup(); | |
538 | } | |
82c643ff | 539 | |
fd1dff4b FB |
540 | static int socket_init(void) |
541 | { | |
542 | WSADATA Data; | |
543 | int ret, err; | |
544 | ||
545 | ret = WSAStartup(MAKEWORD(2,2), &Data); | |
546 | if (ret != 0) { | |
547 | err = WSAGetLastError(); | |
548 | fprintf(stderr, "WSAStartup: %d\n", err); | |
549 | return -1; | |
550 | } | |
28695489 | 551 | atexit(socket_cleanup); |
fd1dff4b FB |
552 | return 0; |
553 | } | |
64b7b733 AJ |
554 | #endif |
555 | ||
1ae26a18 AZ |
556 | /***********************************************************/ |
557 | /* Bluetooth support */ | |
558 | static int nb_hcis; | |
559 | static int cur_hci; | |
560 | static struct HCIInfo *hci_table[MAX_NICS]; | |
dc72ac14 | 561 | |
1ae26a18 AZ |
562 | static struct bt_vlan_s { |
563 | struct bt_scatternet_s net; | |
564 | int id; | |
565 | struct bt_vlan_s *next; | |
566 | } *first_bt_vlan; | |
567 | ||
568 | /* find or alloc a new bluetooth "VLAN" */ | |
674bb261 | 569 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
1ae26a18 AZ |
570 | { |
571 | struct bt_vlan_s **pvlan, *vlan; | |
572 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { | |
573 | if (vlan->id == id) | |
574 | return &vlan->net; | |
575 | } | |
576 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); | |
577 | vlan->id = id; | |
578 | pvlan = &first_bt_vlan; | |
579 | while (*pvlan != NULL) | |
580 | pvlan = &(*pvlan)->next; | |
581 | *pvlan = vlan; | |
582 | return &vlan->net; | |
583 | } | |
584 | ||
585 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) | |
586 | { | |
587 | } | |
588 | ||
589 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) | |
590 | { | |
591 | return -ENOTSUP; | |
592 | } | |
593 | ||
594 | static struct HCIInfo null_hci = { | |
595 | .cmd_send = null_hci_send, | |
596 | .sco_send = null_hci_send, | |
597 | .acl_send = null_hci_send, | |
598 | .bdaddr_set = null_hci_addr_set, | |
599 | }; | |
600 | ||
601 | struct HCIInfo *qemu_next_hci(void) | |
602 | { | |
603 | if (cur_hci == nb_hcis) | |
604 | return &null_hci; | |
605 | ||
606 | return hci_table[cur_hci++]; | |
607 | } | |
608 | ||
dc72ac14 AZ |
609 | static struct HCIInfo *hci_init(const char *str) |
610 | { | |
611 | char *endp; | |
612 | struct bt_scatternet_s *vlan = 0; | |
613 | ||
614 | if (!strcmp(str, "null")) | |
615 | /* null */ | |
616 | return &null_hci; | |
617 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) | |
618 | /* host[:hciN] */ | |
619 | return bt_host_hci(str[4] ? str + 5 : "hci0"); | |
620 | else if (!strncmp(str, "hci", 3)) { | |
621 | /* hci[,vlan=n] */ | |
622 | if (str[3]) { | |
623 | if (!strncmp(str + 3, ",vlan=", 6)) { | |
624 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); | |
625 | if (*endp) | |
626 | vlan = 0; | |
627 | } | |
628 | } else | |
629 | vlan = qemu_find_bt_vlan(0); | |
630 | if (vlan) | |
631 | return bt_new_hci(vlan); | |
632 | } | |
633 | ||
634 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
639 | static int bt_hci_parse(const char *str) | |
640 | { | |
641 | struct HCIInfo *hci; | |
c227f099 | 642 | bdaddr_t bdaddr; |
dc72ac14 AZ |
643 | |
644 | if (nb_hcis >= MAX_NICS) { | |
645 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); | |
646 | return -1; | |
647 | } | |
648 | ||
649 | hci = hci_init(str); | |
650 | if (!hci) | |
651 | return -1; | |
652 | ||
653 | bdaddr.b[0] = 0x52; | |
654 | bdaddr.b[1] = 0x54; | |
655 | bdaddr.b[2] = 0x00; | |
656 | bdaddr.b[3] = 0x12; | |
657 | bdaddr.b[4] = 0x34; | |
658 | bdaddr.b[5] = 0x56 + nb_hcis; | |
659 | hci->bdaddr_set(hci, bdaddr.b); | |
660 | ||
661 | hci_table[nb_hcis++] = hci; | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
666 | static void bt_vhci_add(int vlan_id) | |
667 | { | |
668 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); | |
669 | ||
670 | if (!vlan->slave) | |
671 | fprintf(stderr, "qemu: warning: adding a VHCI to " | |
672 | "an empty scatternet %i\n", vlan_id); | |
673 | ||
674 | bt_vhci_init(bt_new_hci(vlan)); | |
675 | } | |
676 | ||
677 | static struct bt_device_s *bt_device_add(const char *opt) | |
678 | { | |
679 | struct bt_scatternet_s *vlan; | |
680 | int vlan_id = 0; | |
681 | char *endp = strstr(opt, ",vlan="); | |
682 | int len = (endp ? endp - opt : strlen(opt)) + 1; | |
683 | char devname[10]; | |
684 | ||
685 | pstrcpy(devname, MIN(sizeof(devname), len), opt); | |
686 | ||
687 | if (endp) { | |
688 | vlan_id = strtol(endp + 6, &endp, 0); | |
689 | if (*endp) { | |
690 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); | |
691 | return 0; | |
692 | } | |
693 | } | |
694 | ||
695 | vlan = qemu_find_bt_vlan(vlan_id); | |
696 | ||
697 | if (!vlan->slave) | |
698 | fprintf(stderr, "qemu: warning: adding a slave device to " | |
699 | "an empty scatternet %i\n", vlan_id); | |
700 | ||
701 | if (!strcmp(devname, "keyboard")) | |
702 | return bt_keyboard_init(vlan); | |
703 | ||
704 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); | |
705 | return 0; | |
706 | } | |
707 | ||
708 | static int bt_parse(const char *opt) | |
709 | { | |
710 | const char *endp, *p; | |
711 | int vlan; | |
712 | ||
713 | if (strstart(opt, "hci", &endp)) { | |
714 | if (!*endp || *endp == ',') { | |
715 | if (*endp) | |
716 | if (!strstart(endp, ",vlan=", 0)) | |
717 | opt = endp + 1; | |
718 | ||
719 | return bt_hci_parse(opt); | |
720 | } | |
721 | } else if (strstart(opt, "vhci", &endp)) { | |
722 | if (!*endp || *endp == ',') { | |
723 | if (*endp) { | |
724 | if (strstart(endp, ",vlan=", &p)) { | |
725 | vlan = strtol(p, (char **) &endp, 0); | |
726 | if (*endp) { | |
727 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); | |
728 | return 1; | |
729 | } | |
730 | } else { | |
731 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); | |
732 | return 1; | |
733 | } | |
734 | } else | |
735 | vlan = 0; | |
736 | ||
737 | bt_vhci_add(vlan); | |
738 | return 0; | |
739 | } | |
740 | } else if (strstart(opt, "device:", &endp)) | |
741 | return !bt_device_add(endp); | |
742 | ||
743 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); | |
744 | return 1; | |
745 | } | |
746 | ||
1ae26a18 AZ |
747 | /***********************************************************/ |
748 | /* QEMU Block devices */ | |
749 | ||
609497ab | 750 | #define HD_ALIAS "index=%d,media=disk" |
e4bcb14c | 751 | #define CDROM_ALIAS "index=2,media=cdrom" |
e4bcb14c | 752 | #define FD_ALIAS "index=%d,if=floppy" |
609497ab AZ |
753 | #define PFLASH_ALIAS "if=pflash" |
754 | #define MTD_ALIAS "if=mtd" | |
9d413d1d | 755 | #define SD_ALIAS "index=0,if=sd" |
e4bcb14c | 756 | |
9dfd7c7a | 757 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
e4bcb14c TS |
758 | { |
759 | va_list ap; | |
9dfd7c7a GH |
760 | char optstr[1024]; |
761 | QemuOpts *opts; | |
e4bcb14c | 762 | |
e4bcb14c | 763 | va_start(ap, fmt); |
9dfd7c7a | 764 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
e4bcb14c TS |
765 | va_end(ap); |
766 | ||
8212c64f | 767 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0); |
9dfd7c7a GH |
768 | if (!opts) { |
769 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", | |
770 | __FUNCTION__, optstr); | |
771 | return NULL; | |
772 | } | |
773 | if (file) | |
774 | qemu_opt_set(opts, "file", file); | |
775 | return opts; | |
b01b1111 AL |
776 | } |
777 | ||
751c6a17 | 778 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
e4bcb14c | 779 | { |
751c6a17 | 780 | DriveInfo *dinfo; |
e4bcb14c TS |
781 | |
782 | /* seek interface, bus and unit */ | |
783 | ||
72cf2d4f | 784 | QTAILQ_FOREACH(dinfo, &drives, next) { |
751c6a17 GH |
785 | if (dinfo->type == type && |
786 | dinfo->bus == bus && | |
787 | dinfo->unit == unit) | |
788 | return dinfo; | |
789 | } | |
e4bcb14c | 790 | |
751c6a17 | 791 | return NULL; |
e4bcb14c TS |
792 | } |
793 | ||
2e810b36 | 794 | DriveInfo *drive_get_by_id(const char *id) |
1dae12e6 GH |
795 | { |
796 | DriveInfo *dinfo; | |
797 | ||
72cf2d4f | 798 | QTAILQ_FOREACH(dinfo, &drives, next) { |
1dae12e6 GH |
799 | if (strcmp(id, dinfo->id)) |
800 | continue; | |
801 | return dinfo; | |
802 | } | |
803 | return NULL; | |
804 | } | |
805 | ||
f60d39bc | 806 | int drive_get_max_bus(BlockInterfaceType type) |
e4bcb14c TS |
807 | { |
808 | int max_bus; | |
751c6a17 | 809 | DriveInfo *dinfo; |
e4bcb14c TS |
810 | |
811 | max_bus = -1; | |
72cf2d4f | 812 | QTAILQ_FOREACH(dinfo, &drives, next) { |
751c6a17 GH |
813 | if(dinfo->type == type && |
814 | dinfo->bus > max_bus) | |
815 | max_bus = dinfo->bus; | |
e4bcb14c TS |
816 | } |
817 | return max_bus; | |
818 | } | |
819 | ||
fa879c64 AL |
820 | const char *drive_get_serial(BlockDriverState *bdrv) |
821 | { | |
751c6a17 | 822 | DriveInfo *dinfo; |
fa879c64 | 823 | |
72cf2d4f | 824 | QTAILQ_FOREACH(dinfo, &drives, next) { |
751c6a17 GH |
825 | if (dinfo->bdrv == bdrv) |
826 | return dinfo->serial; | |
827 | } | |
fa879c64 AL |
828 | |
829 | return "\0"; | |
830 | } | |
831 | ||
f7850099 KW |
832 | BlockInterfaceErrorAction drive_get_on_error( |
833 | BlockDriverState *bdrv, int is_read) | |
428c5705 | 834 | { |
751c6a17 | 835 | DriveInfo *dinfo; |
428c5705 | 836 | |
72cf2d4f | 837 | QTAILQ_FOREACH(dinfo, &drives, next) { |
751c6a17 | 838 | if (dinfo->bdrv == bdrv) |
e9b2e818 | 839 | return is_read ? dinfo->on_read_error : dinfo->on_write_error; |
751c6a17 | 840 | } |
428c5705 | 841 | |
e9b2e818 | 842 | return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC; |
428c5705 AL |
843 | } |
844 | ||
a1620fac AJ |
845 | static void bdrv_format_print(void *opaque, const char *name) |
846 | { | |
847 | fprintf(stderr, " %s", name); | |
848 | } | |
849 | ||
56a14938 | 850 | void drive_uninit(DriveInfo *dinfo) |
b01b1111 | 851 | { |
56a14938 GH |
852 | qemu_opts_del(dinfo->opts); |
853 | bdrv_delete(dinfo->bdrv); | |
854 | QTAILQ_REMOVE(&drives, dinfo, next); | |
855 | qemu_free(dinfo); | |
b01b1111 AL |
856 | } |
857 | ||
e9b2e818 KW |
858 | static int parse_block_error_action(const char *buf, int is_read) |
859 | { | |
860 | if (!strcmp(buf, "ignore")) { | |
861 | return BLOCK_ERR_IGNORE; | |
862 | } else if (!is_read && !strcmp(buf, "enospc")) { | |
863 | return BLOCK_ERR_STOP_ENOSPC; | |
864 | } else if (!strcmp(buf, "stop")) { | |
865 | return BLOCK_ERR_STOP_ANY; | |
866 | } else if (!strcmp(buf, "report")) { | |
867 | return BLOCK_ERR_REPORT; | |
868 | } else { | |
869 | fprintf(stderr, "qemu: '%s' invalid %s error action\n", | |
870 | buf, is_read ? "read" : "write"); | |
871 | return -1; | |
872 | } | |
873 | } | |
874 | ||
9dfd7c7a | 875 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
751c6a17 | 876 | int *fatal_error) |
e4bcb14c | 877 | { |
9dfd7c7a GH |
878 | const char *buf; |
879 | const char *file = NULL; | |
c8522bdf | 880 | char devname[128]; |
9dfd7c7a | 881 | const char *serial; |
c8522bdf | 882 | const char *mediastr = ""; |
f60d39bc | 883 | BlockInterfaceType type; |
e4bcb14c TS |
884 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
885 | int bus_id, unit_id; | |
886 | int cyls, heads, secs, translation; | |
1e72d3b7 | 887 | BlockDriver *drv = NULL; |
4d73cd3b | 888 | QEMUMachine *machine = opaque; |
e4bcb14c TS |
889 | int max_devs; |
890 | int index; | |
33f00271 | 891 | int cache; |
5c6c3a6c | 892 | int aio = 0; |
59f2689d | 893 | int ro = 0; |
e9b2e818 KW |
894 | int bdrv_flags; |
895 | int on_read_error, on_write_error; | |
c2cc47a4 | 896 | const char *devaddr; |
751c6a17 | 897 | DriveInfo *dinfo; |
9dfd7c7a | 898 | int snapshot = 0; |
e4bcb14c | 899 | |
9dfd7c7a | 900 | *fatal_error = 1; |
e4bcb14c | 901 | |
e4bcb14c | 902 | translation = BIOS_ATA_TRANSLATION_AUTO; |
0aa217e4 | 903 | cache = 1; |
e4bcb14c | 904 | |
4d007814 | 905 | if (machine && machine->use_scsi) { |
f60d39bc | 906 | type = IF_SCSI; |
e4bcb14c | 907 | max_devs = MAX_SCSI_DEVS; |
363a37d5 | 908 | pstrcpy(devname, sizeof(devname), "scsi"); |
e4bcb14c | 909 | } else { |
f60d39bc | 910 | type = IF_IDE; |
e4bcb14c | 911 | max_devs = MAX_IDE_DEVS; |
363a37d5 | 912 | pstrcpy(devname, sizeof(devname), "ide"); |
e4bcb14c TS |
913 | } |
914 | media = MEDIA_DISK; | |
915 | ||
916 | /* extract parameters */ | |
9dfd7c7a GH |
917 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
918 | unit_id = qemu_opt_get_number(opts, "unit", -1); | |
919 | index = qemu_opt_get_number(opts, "index", -1); | |
e4bcb14c | 920 | |
9dfd7c7a GH |
921 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
922 | heads = qemu_opt_get_number(opts, "heads", 0); | |
923 | secs = qemu_opt_get_number(opts, "secs", 0); | |
e4bcb14c | 924 | |
9dfd7c7a | 925 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
59f2689d | 926 | ro = qemu_opt_get_bool(opts, "readonly", 0); |
e4bcb14c | 927 | |
9dfd7c7a GH |
928 | file = qemu_opt_get(opts, "file"); |
929 | serial = qemu_opt_get(opts, "serial"); | |
930 | ||
931 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { | |
ae45d369 | 932 | pstrcpy(devname, sizeof(devname), buf); |
e4bcb14c | 933 | if (!strcmp(buf, "ide")) { |
f60d39bc | 934 | type = IF_IDE; |
e4bcb14c TS |
935 | max_devs = MAX_IDE_DEVS; |
936 | } else if (!strcmp(buf, "scsi")) { | |
f60d39bc | 937 | type = IF_SCSI; |
e4bcb14c TS |
938 | max_devs = MAX_SCSI_DEVS; |
939 | } else if (!strcmp(buf, "floppy")) { | |
f60d39bc | 940 | type = IF_FLOPPY; |
e4bcb14c TS |
941 | max_devs = 0; |
942 | } else if (!strcmp(buf, "pflash")) { | |
f60d39bc | 943 | type = IF_PFLASH; |
e4bcb14c TS |
944 | max_devs = 0; |
945 | } else if (!strcmp(buf, "mtd")) { | |
f60d39bc | 946 | type = IF_MTD; |
e4bcb14c TS |
947 | max_devs = 0; |
948 | } else if (!strcmp(buf, "sd")) { | |
f60d39bc | 949 | type = IF_SD; |
e4bcb14c | 950 | max_devs = 0; |
6e02c38d AL |
951 | } else if (!strcmp(buf, "virtio")) { |
952 | type = IF_VIRTIO; | |
953 | max_devs = 0; | |
62d23efa AL |
954 | } else if (!strcmp(buf, "xen")) { |
955 | type = IF_XEN; | |
956 | max_devs = 0; | |
a8659e90 GH |
957 | } else if (!strcmp(buf, "none")) { |
958 | type = IF_NONE; | |
959 | max_devs = 0; | |
62d23efa | 960 | } else { |
9dfd7c7a | 961 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
751c6a17 | 962 | return NULL; |
e4bcb14c TS |
963 | } |
964 | } | |
965 | ||
e4bcb14c | 966 | if (cyls || heads || secs) { |
5afe3f04 | 967 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
9dfd7c7a | 968 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
751c6a17 | 969 | return NULL; |
e4bcb14c | 970 | } |
5afe3f04 | 971 | if (heads < 1 || (type == IF_IDE && heads > 16)) { |
9dfd7c7a | 972 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
751c6a17 | 973 | return NULL; |
e4bcb14c | 974 | } |
5afe3f04 | 975 | if (secs < 1 || (type == IF_IDE && secs > 63)) { |
9dfd7c7a | 976 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
751c6a17 | 977 | return NULL; |
e4bcb14c TS |
978 | } |
979 | } | |
980 | ||
9dfd7c7a | 981 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
e4bcb14c TS |
982 | if (!cyls) { |
983 | fprintf(stderr, | |
984 | "qemu: '%s' trans must be used with cyls,heads and secs\n", | |
9dfd7c7a | 985 | buf); |
751c6a17 | 986 | return NULL; |
e4bcb14c TS |
987 | } |
988 | if (!strcmp(buf, "none")) | |
989 | translation = BIOS_ATA_TRANSLATION_NONE; | |
990 | else if (!strcmp(buf, "lba")) | |
991 | translation = BIOS_ATA_TRANSLATION_LBA; | |
992 | else if (!strcmp(buf, "auto")) | |
993 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
994 | else { | |
9dfd7c7a | 995 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
751c6a17 | 996 | return NULL; |
e4bcb14c TS |
997 | } |
998 | } | |
999 | ||
9dfd7c7a | 1000 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
e4bcb14c TS |
1001 | if (!strcmp(buf, "disk")) { |
1002 | media = MEDIA_DISK; | |
1003 | } else if (!strcmp(buf, "cdrom")) { | |
1004 | if (cyls || secs || heads) { | |
1005 | fprintf(stderr, | |
9dfd7c7a | 1006 | "qemu: '%s' invalid physical CHS format\n", buf); |
751c6a17 | 1007 | return NULL; |
e4bcb14c TS |
1008 | } |
1009 | media = MEDIA_CDROM; | |
1010 | } else { | |
9dfd7c7a | 1011 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
751c6a17 | 1012 | return NULL; |
e4bcb14c TS |
1013 | } |
1014 | } | |
1015 | ||
9dfd7c7a | 1016 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
9f7965c7 | 1017 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
33f00271 | 1018 | cache = 0; |
9f7965c7 | 1019 | else if (!strcmp(buf, "writethrough")) |
33f00271 | 1020 | cache = 1; |
9f7965c7 AL |
1021 | else if (!strcmp(buf, "writeback")) |
1022 | cache = 2; | |
33f00271 AZ |
1023 | else { |
1024 | fprintf(stderr, "qemu: invalid cache option\n"); | |
751c6a17 | 1025 | return NULL; |
33f00271 AZ |
1026 | } |
1027 | } | |
1028 | ||
5c6c3a6c CH |
1029 | #ifdef CONFIG_LINUX_AIO |
1030 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { | |
1031 | if (!strcmp(buf, "threads")) | |
1032 | aio = 0; | |
1033 | else if (!strcmp(buf, "native")) | |
1034 | aio = 1; | |
1035 | else { | |
1036 | fprintf(stderr, "qemu: invalid aio option\n"); | |
1037 | return NULL; | |
1038 | } | |
1039 | } | |
1040 | #endif | |
1041 | ||
9dfd7c7a | 1042 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
a1620fac AJ |
1043 | if (strcmp(buf, "?") == 0) { |
1044 | fprintf(stderr, "qemu: Supported formats:"); | |
1045 | bdrv_iterate_format(bdrv_format_print, NULL); | |
1046 | fprintf(stderr, "\n"); | |
751c6a17 | 1047 | return NULL; |
a1620fac | 1048 | } |
eb852011 | 1049 | drv = bdrv_find_whitelisted_format(buf); |
1e72d3b7 AJ |
1050 | if (!drv) { |
1051 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); | |
751c6a17 | 1052 | return NULL; |
1e72d3b7 AJ |
1053 | } |
1054 | } | |
1055 | ||
e9b2e818 | 1056 | on_write_error = BLOCK_ERR_STOP_ENOSPC; |
9dfd7c7a | 1057 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
869a5c6d | 1058 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
ea8a5d7f | 1059 | fprintf(stderr, "werror is no supported by this format\n"); |
751c6a17 | 1060 | return NULL; |
428c5705 | 1061 | } |
e9b2e818 KW |
1062 | |
1063 | on_write_error = parse_block_error_action(buf, 0); | |
1064 | if (on_write_error < 0) { | |
1065 | return NULL; | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | on_read_error = BLOCK_ERR_REPORT; | |
1070 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { | |
f35d68f0 | 1071 | if (type != IF_IDE && type != IF_VIRTIO) { |
e9b2e818 KW |
1072 | fprintf(stderr, "rerror is no supported by this format\n"); |
1073 | return NULL; | |
1074 | } | |
1075 | ||
1076 | on_read_error = parse_block_error_action(buf, 1); | |
1077 | if (on_read_error < 0) { | |
751c6a17 | 1078 | return NULL; |
428c5705 AL |
1079 | } |
1080 | } | |
1081 | ||
9dfd7c7a | 1082 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
c2cc47a4 | 1083 | if (type != IF_VIRTIO) { |
9dfd7c7a | 1084 | fprintf(stderr, "addr is not supported\n"); |
751c6a17 | 1085 | return NULL; |
c2cc47a4 | 1086 | } |
c2cc47a4 MA |
1087 | } |
1088 | ||
e4bcb14c TS |
1089 | /* compute bus and unit according index */ |
1090 | ||
1091 | if (index != -1) { | |
1092 | if (bus_id != 0 || unit_id != -1) { | |
1093 | fprintf(stderr, | |
9dfd7c7a | 1094 | "qemu: index cannot be used with bus and unit\n"); |
751c6a17 | 1095 | return NULL; |
e4bcb14c TS |
1096 | } |
1097 | if (max_devs == 0) | |
1098 | { | |
1099 | unit_id = index; | |
1100 | bus_id = 0; | |
1101 | } else { | |
1102 | unit_id = index % max_devs; | |
1103 | bus_id = index / max_devs; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | /* if user doesn't specify a unit_id, | |
1108 | * try to find the first free | |
1109 | */ | |
1110 | ||
1111 | if (unit_id == -1) { | |
1112 | unit_id = 0; | |
751c6a17 | 1113 | while (drive_get(type, bus_id, unit_id) != NULL) { |
e4bcb14c TS |
1114 | unit_id++; |
1115 | if (max_devs && unit_id >= max_devs) { | |
1116 | unit_id -= max_devs; | |
1117 | bus_id++; | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | /* check unit id */ | |
1123 | ||
1124 | if (max_devs && unit_id >= max_devs) { | |
9dfd7c7a GH |
1125 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
1126 | unit_id, max_devs - 1); | |
751c6a17 | 1127 | return NULL; |
e4bcb14c TS |
1128 | } |
1129 | ||
1130 | /* | |
1131 | * ignore multiple definitions | |
1132 | */ | |
1133 | ||
751c6a17 GH |
1134 | if (drive_get(type, bus_id, unit_id) != NULL) { |
1135 | *fatal_error = 0; | |
1136 | return NULL; | |
1137 | } | |
e4bcb14c TS |
1138 | |
1139 | /* init */ | |
1140 | ||
751c6a17 | 1141 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
e23d9c4d | 1142 | if ((buf = qemu_opts_id(opts)) != NULL) { |
9dfd7c7a GH |
1143 | dinfo->id = qemu_strdup(buf); |
1144 | } else { | |
1dae12e6 | 1145 | /* no id supplied -> create one */ |
9dfd7c7a | 1146 | dinfo->id = qemu_mallocz(32); |
1dae12e6 GH |
1147 | if (type == IF_IDE || type == IF_SCSI) |
1148 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
1149 | if (max_devs) | |
9dfd7c7a | 1150 | snprintf(dinfo->id, 32, "%s%i%s%i", |
1dae12e6 GH |
1151 | devname, bus_id, mediastr, unit_id); |
1152 | else | |
9dfd7c7a | 1153 | snprintf(dinfo->id, 32, "%s%s%i", |
1dae12e6 GH |
1154 | devname, mediastr, unit_id); |
1155 | } | |
1dae12e6 | 1156 | dinfo->bdrv = bdrv_new(dinfo->id); |
751c6a17 GH |
1157 | dinfo->devaddr = devaddr; |
1158 | dinfo->type = type; | |
1159 | dinfo->bus = bus_id; | |
1160 | dinfo->unit = unit_id; | |
e9b2e818 KW |
1161 | dinfo->on_read_error = on_read_error; |
1162 | dinfo->on_write_error = on_write_error; | |
9dfd7c7a GH |
1163 | dinfo->opts = opts; |
1164 | if (serial) | |
1165 | strncpy(dinfo->serial, serial, sizeof(serial)); | |
72cf2d4f | 1166 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
e4bcb14c | 1167 | |
f60d39bc | 1168 | switch(type) { |
e4bcb14c TS |
1169 | case IF_IDE: |
1170 | case IF_SCSI: | |
62d23efa | 1171 | case IF_XEN: |
c219331e | 1172 | case IF_NONE: |
e4bcb14c TS |
1173 | switch(media) { |
1174 | case MEDIA_DISK: | |
1175 | if (cyls != 0) { | |
1dae12e6 GH |
1176 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
1177 | bdrv_set_translation_hint(dinfo->bdrv, translation); | |
e4bcb14c TS |
1178 | } |
1179 | break; | |
1180 | case MEDIA_CDROM: | |
1dae12e6 | 1181 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
e4bcb14c TS |
1182 | break; |
1183 | } | |
1184 | break; | |
1185 | case IF_SD: | |
1186 | /* FIXME: This isn't really a floppy, but it's a reasonable | |
1187 | approximation. */ | |
1188 | case IF_FLOPPY: | |
1dae12e6 | 1189 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
e4bcb14c TS |
1190 | break; |
1191 | case IF_PFLASH: | |
1192 | case IF_MTD: | |
1193 | break; | |
d176c495 GH |
1194 | case IF_VIRTIO: |
1195 | /* add virtio block device */ | |
1196 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); | |
1197 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); | |
1198 | qemu_opt_set(opts, "drive", dinfo->id); | |
1199 | if (devaddr) | |
1200 | qemu_opt_set(opts, "addr", devaddr); | |
1201 | break; | |
aae9460e PB |
1202 | case IF_COUNT: |
1203 | abort(); | |
e4bcb14c | 1204 | } |
9dfd7c7a | 1205 | if (!file) { |
751c6a17 GH |
1206 | *fatal_error = 0; |
1207 | return NULL; | |
1208 | } | |
33f00271 | 1209 | bdrv_flags = 0; |
9f7965c7 | 1210 | if (snapshot) { |
33f00271 | 1211 | bdrv_flags |= BDRV_O_SNAPSHOT; |
9f7965c7 AL |
1212 | cache = 2; /* always use write-back with snapshot */ |
1213 | } | |
1214 | if (cache == 0) /* no caching */ | |
1215 | bdrv_flags |= BDRV_O_NOCACHE; | |
1216 | else if (cache == 2) /* write-back */ | |
1217 | bdrv_flags |= BDRV_O_CACHE_WB; | |
5c6c3a6c CH |
1218 | |
1219 | if (aio == 1) { | |
1220 | bdrv_flags |= BDRV_O_NATIVE_AIO; | |
1221 | } else { | |
1222 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; | |
1223 | } | |
1224 | ||
59f2689d | 1225 | if (ro == 1) { |
f5edb014 NS |
1226 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) { |
1227 | fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n"); | |
59f2689d NS |
1228 | return NULL; |
1229 | } | |
b196b153 NS |
1230 | } |
1231 | /* | |
1232 | * cdrom is read-only. Set it now, after above interface checking | |
1233 | * since readonly attribute not explicitly required, so no error. | |
1234 | */ | |
1235 | if (media == MEDIA_CDROM) { | |
f5edb014 | 1236 | ro = 1; |
59f2689d | 1237 | } |
f5edb014 | 1238 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; |
59f2689d | 1239 | |
1dae12e6 | 1240 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
850810d0 JF |
1241 | fprintf(stderr, "qemu: could not open disk image %s: %s\n", |
1242 | file, strerror(errno)); | |
751c6a17 | 1243 | return NULL; |
e4bcb14c | 1244 | } |
5c6c3a6c | 1245 | |
1dae12e6 | 1246 | if (bdrv_key_required(dinfo->bdrv)) |
c0f4ce77 | 1247 | autostart = 0; |
751c6a17 GH |
1248 | *fatal_error = 0; |
1249 | return dinfo; | |
e4bcb14c TS |
1250 | } |
1251 | ||
9dfd7c7a GH |
1252 | static int drive_init_func(QemuOpts *opts, void *opaque) |
1253 | { | |
1254 | QEMUMachine *machine = opaque; | |
1255 | int fatal_error = 0; | |
1256 | ||
1257 | if (drive_init(opts, machine, &fatal_error) == NULL) { | |
1258 | if (fatal_error) | |
1259 | return 1; | |
1260 | } | |
1261 | return 0; | |
1262 | } | |
1263 | ||
1264 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) | |
1265 | { | |
1266 | if (NULL == qemu_opt_get(opts, "snapshot")) { | |
1267 | qemu_opt_set(opts, "snapshot", "on"); | |
1268 | } | |
1269 | return 0; | |
1270 | } | |
1271 | ||
76e30d0f JK |
1272 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
1273 | { | |
1274 | boot_set_handler = func; | |
1275 | boot_set_opaque = opaque; | |
1276 | } | |
1277 | ||
1278 | int qemu_boot_set(const char *boot_devices) | |
1279 | { | |
1280 | if (!boot_set_handler) { | |
1281 | return -EINVAL; | |
1282 | } | |
1283 | return boot_set_handler(boot_set_opaque, boot_devices); | |
1284 | } | |
1285 | ||
ef3adf68 JK |
1286 | static int parse_bootdevices(char *devices) |
1287 | { | |
1288 | /* We just do some generic consistency checks */ | |
1289 | const char *p; | |
1290 | int bitmap = 0; | |
1291 | ||
1292 | for (p = devices; *p != '\0'; p++) { | |
1293 | /* Allowed boot devices are: | |
1294 | * a-b: floppy disk drives | |
1295 | * c-f: IDE disk drives | |
1296 | * g-m: machine implementation dependant drives | |
1297 | * n-p: network devices | |
1298 | * It's up to each machine implementation to check if the given boot | |
1299 | * devices match the actual hardware implementation and firmware | |
1300 | * features. | |
1301 | */ | |
1302 | if (*p < 'a' || *p > 'p') { | |
1303 | fprintf(stderr, "Invalid boot device '%c'\n", *p); | |
1304 | exit(1); | |
1305 | } | |
1306 | if (bitmap & (1 << (*p - 'a'))) { | |
1307 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); | |
1308 | exit(1); | |
1309 | } | |
1310 | bitmap |= 1 << (*p - 'a'); | |
1311 | } | |
1312 | return bitmap; | |
1313 | } | |
1314 | ||
e0f084bf JK |
1315 | static void restore_boot_devices(void *opaque) |
1316 | { | |
1317 | char *standard_boot_devices = opaque; | |
1318 | ||
1319 | qemu_boot_set(standard_boot_devices); | |
1320 | ||
1321 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); | |
1322 | qemu_free(standard_boot_devices); | |
1323 | } | |
1324 | ||
268a362c AL |
1325 | static void numa_add(const char *optarg) |
1326 | { | |
1327 | char option[128]; | |
1328 | char *endptr; | |
1329 | unsigned long long value, endvalue; | |
1330 | int nodenr; | |
1331 | ||
1332 | optarg = get_opt_name(option, 128, optarg, ',') + 1; | |
1333 | if (!strcmp(option, "node")) { | |
1334 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { | |
1335 | nodenr = nb_numa_nodes; | |
1336 | } else { | |
1337 | nodenr = strtoull(option, NULL, 10); | |
1338 | } | |
1339 | ||
1340 | if (get_param_value(option, 128, "mem", optarg) == 0) { | |
1341 | node_mem[nodenr] = 0; | |
1342 | } else { | |
1343 | value = strtoull(option, &endptr, 0); | |
1344 | switch (*endptr) { | |
1345 | case 0: case 'M': case 'm': | |
1346 | value <<= 20; | |
1347 | break; | |
1348 | case 'G': case 'g': | |
1349 | value <<= 30; | |
1350 | break; | |
1351 | } | |
1352 | node_mem[nodenr] = value; | |
1353 | } | |
1354 | if (get_param_value(option, 128, "cpus", optarg) == 0) { | |
1355 | node_cpumask[nodenr] = 0; | |
1356 | } else { | |
1357 | value = strtoull(option, &endptr, 10); | |
1358 | if (value >= 64) { | |
1359 | value = 63; | |
1360 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); | |
1361 | } else { | |
1362 | if (*endptr == '-') { | |
1363 | endvalue = strtoull(endptr+1, &endptr, 10); | |
1364 | if (endvalue >= 63) { | |
1365 | endvalue = 62; | |
1366 | fprintf(stderr, | |
1367 | "only 63 CPUs in NUMA mode supported.\n"); | |
1368 | } | |
0dfbd514 | 1369 | value = (2ULL << endvalue) - (1ULL << value); |
268a362c | 1370 | } else { |
0dfbd514 | 1371 | value = 1ULL << value; |
268a362c AL |
1372 | } |
1373 | } | |
1374 | node_cpumask[nodenr] = value; | |
1375 | } | |
1376 | nb_numa_nodes++; | |
1377 | } | |
1378 | return; | |
1379 | } | |
1380 | ||
dc6b1c09 AP |
1381 | static void smp_parse(const char *optarg) |
1382 | { | |
1383 | int smp, sockets = 0, threads = 0, cores = 0; | |
1384 | char *endptr; | |
1385 | char option[128]; | |
1386 | ||
1387 | smp = strtoul(optarg, &endptr, 10); | |
1388 | if (endptr != optarg) { | |
1389 | if (*endptr == ',') { | |
1390 | endptr++; | |
1391 | } | |
1392 | } | |
1393 | if (get_param_value(option, 128, "sockets", endptr) != 0) | |
1394 | sockets = strtoull(option, NULL, 10); | |
1395 | if (get_param_value(option, 128, "cores", endptr) != 0) | |
1396 | cores = strtoull(option, NULL, 10); | |
1397 | if (get_param_value(option, 128, "threads", endptr) != 0) | |
1398 | threads = strtoull(option, NULL, 10); | |
1399 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) | |
1400 | max_cpus = strtoull(option, NULL, 10); | |
1401 | ||
1402 | /* compute missing values, prefer sockets over cores over threads */ | |
1403 | if (smp == 0 || sockets == 0) { | |
1404 | sockets = sockets > 0 ? sockets : 1; | |
1405 | cores = cores > 0 ? cores : 1; | |
1406 | threads = threads > 0 ? threads : 1; | |
1407 | if (smp == 0) { | |
1408 | smp = cores * threads * sockets; | |
dc6b1c09 AP |
1409 | } |
1410 | } else { | |
1411 | if (cores == 0) { | |
1412 | threads = threads > 0 ? threads : 1; | |
1413 | cores = smp / (sockets * threads); | |
1414 | } else { | |
5cdc9b76 | 1415 | if (sockets) { |
dc6b1c09 AP |
1416 | threads = smp / (cores * sockets); |
1417 | } | |
1418 | } | |
1419 | } | |
1420 | smp_cpus = smp; | |
1421 | smp_cores = cores > 0 ? cores : 1; | |
1422 | smp_threads = threads > 0 ? threads : 1; | |
1423 | if (max_cpus == 0) | |
1424 | max_cpus = smp_cpus; | |
1425 | } | |
1426 | ||
a594cfbf FB |
1427 | /***********************************************************/ |
1428 | /* USB devices */ | |
1429 | ||
c0f4ce77 | 1430 | static int usb_device_add(const char *devname, int is_hotplug) |
a594cfbf FB |
1431 | { |
1432 | const char *p; | |
a5d2f727 | 1433 | USBDevice *dev = NULL; |
a594cfbf | 1434 | |
a5d2f727 | 1435 | if (!usb_enabled) |
a594cfbf FB |
1436 | return -1; |
1437 | ||
0958b4cc GH |
1438 | /* drivers with .usbdevice_name entry in USBDeviceInfo */ |
1439 | dev = usbdevice_create(devname); | |
1440 | if (dev) | |
1441 | goto done; | |
1442 | ||
a5d2f727 | 1443 | /* the other ones */ |
a594cfbf FB |
1444 | if (strstart(devname, "host:", &p)) { |
1445 | dev = usb_host_device_open(p); | |
dc72ac14 AZ |
1446 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
1447 | dev = usb_bt_init(devname[2] ? hci_init(p) : | |
1448 | bt_new_hci(qemu_find_bt_vlan(0))); | |
a594cfbf FB |
1449 | } else { |
1450 | return -1; | |
1451 | } | |
0d92ed30 PB |
1452 | if (!dev) |
1453 | return -1; | |
1454 | ||
a5d2f727 | 1455 | done: |
a594cfbf FB |
1456 | return 0; |
1457 | } | |
1458 | ||
1f3870ab AL |
1459 | static int usb_device_del(const char *devname) |
1460 | { | |
1461 | int bus_num, addr; | |
1462 | const char *p; | |
1463 | ||
5d0c5750 AL |
1464 | if (strstart(devname, "host:", &p)) |
1465 | return usb_host_device_close(p); | |
1466 | ||
a5d2f727 | 1467 | if (!usb_enabled) |
1f3870ab AL |
1468 | return -1; |
1469 | ||
1470 | p = strchr(devname, '.'); | |
1471 | if (!p) | |
1472 | return -1; | |
1473 | bus_num = strtoul(devname, NULL, 0); | |
1474 | addr = strtoul(p + 1, NULL, 0); | |
1475 | ||
a5d2f727 | 1476 | return usb_device_delete_addr(bus_num, addr); |
1f3870ab AL |
1477 | } |
1478 | ||
bd3c948d GH |
1479 | static int usb_parse(const char *cmdline) |
1480 | { | |
59d1c1c2 ST |
1481 | int r; |
1482 | r = usb_device_add(cmdline, 0); | |
1483 | if (r < 0) { | |
1484 | fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline); | |
1485 | } | |
1486 | return r; | |
bd3c948d GH |
1487 | } |
1488 | ||
d54908a5 | 1489 | void do_usb_add(Monitor *mon, const QDict *qdict) |
a594cfbf | 1490 | { |
59d1c1c2 ST |
1491 | const char *devname = qdict_get_str(qdict, "devname"); |
1492 | if (usb_device_add(devname, 1) < 0) { | |
1ecda02b | 1493 | error_report("could not add USB device '%s'", devname); |
59d1c1c2 | 1494 | } |
a594cfbf FB |
1495 | } |
1496 | ||
d54908a5 | 1497 | void do_usb_del(Monitor *mon, const QDict *qdict) |
a594cfbf | 1498 | { |
59d1c1c2 ST |
1499 | const char *devname = qdict_get_str(qdict, "devname"); |
1500 | if (usb_device_del(devname) < 0) { | |
1ecda02b | 1501 | error_report("could not delete USB device '%s'", devname); |
59d1c1c2 | 1502 | } |
a594cfbf FB |
1503 | } |
1504 | ||
201a51fc AZ |
1505 | /***********************************************************/ |
1506 | /* PCMCIA/Cardbus */ | |
1507 | ||
1508 | static struct pcmcia_socket_entry_s { | |
bc24a225 | 1509 | PCMCIASocket *socket; |
201a51fc AZ |
1510 | struct pcmcia_socket_entry_s *next; |
1511 | } *pcmcia_sockets = 0; | |
1512 | ||
bc24a225 | 1513 | void pcmcia_socket_register(PCMCIASocket *socket) |
201a51fc AZ |
1514 | { |
1515 | struct pcmcia_socket_entry_s *entry; | |
1516 | ||
1517 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); | |
1518 | entry->socket = socket; | |
1519 | entry->next = pcmcia_sockets; | |
1520 | pcmcia_sockets = entry; | |
1521 | } | |
1522 | ||
bc24a225 | 1523 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
201a51fc AZ |
1524 | { |
1525 | struct pcmcia_socket_entry_s *entry, **ptr; | |
1526 | ||
1527 | ptr = &pcmcia_sockets; | |
1528 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) | |
1529 | if (entry->socket == socket) { | |
1530 | *ptr = entry->next; | |
1531 | qemu_free(entry); | |
1532 | } | |
1533 | } | |
1534 | ||
376253ec | 1535 | void pcmcia_info(Monitor *mon) |
201a51fc AZ |
1536 | { |
1537 | struct pcmcia_socket_entry_s *iter; | |
376253ec | 1538 | |
201a51fc | 1539 | if (!pcmcia_sockets) |
376253ec | 1540 | monitor_printf(mon, "No PCMCIA sockets\n"); |
201a51fc AZ |
1541 | |
1542 | for (iter = pcmcia_sockets; iter; iter = iter->next) | |
376253ec AL |
1543 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
1544 | iter->socket->attached ? iter->socket->card_string : | |
1545 | "Empty"); | |
201a51fc AZ |
1546 | } |
1547 | ||
2ff89790 | 1548 | /***********************************************************/ |
8a7ddc38 | 1549 | /* I/O handling */ |
0824d6fc | 1550 | |
c4b1fcc0 FB |
1551 | typedef struct IOHandlerRecord { |
1552 | int fd; | |
7b27a769 | 1553 | IOCanReadHandler *fd_read_poll; |
7c9d8e07 FB |
1554 | IOHandler *fd_read; |
1555 | IOHandler *fd_write; | |
cafffd40 | 1556 | int deleted; |
c4b1fcc0 FB |
1557 | void *opaque; |
1558 | /* temporary data */ | |
1559 | struct pollfd *ufd; | |
31d4ee6c | 1560 | QLIST_ENTRY(IOHandlerRecord) next; |
c4b1fcc0 FB |
1561 | } IOHandlerRecord; |
1562 | ||
31d4ee6c JQ |
1563 | static QLIST_HEAD(, IOHandlerRecord) io_handlers = |
1564 | QLIST_HEAD_INITIALIZER(io_handlers); | |
1565 | ||
c4b1fcc0 | 1566 | |
7c9d8e07 FB |
1567 | /* XXX: fd_read_poll should be suppressed, but an API change is |
1568 | necessary in the character devices to suppress fd_can_read(). */ | |
5fafdf24 | 1569 | int qemu_set_fd_handler2(int fd, |
7b27a769 | 1570 | IOCanReadHandler *fd_read_poll, |
5fafdf24 TS |
1571 | IOHandler *fd_read, |
1572 | IOHandler *fd_write, | |
7c9d8e07 | 1573 | void *opaque) |
c4b1fcc0 | 1574 | { |
31d4ee6c | 1575 | IOHandlerRecord *ioh; |
c4b1fcc0 | 1576 | |
7c9d8e07 | 1577 | if (!fd_read && !fd_write) { |
31d4ee6c | 1578 | QLIST_FOREACH(ioh, &io_handlers, next) { |
7c9d8e07 | 1579 | if (ioh->fd == fd) { |
cafffd40 | 1580 | ioh->deleted = 1; |
7c9d8e07 FB |
1581 | break; |
1582 | } | |
7c9d8e07 FB |
1583 | } |
1584 | } else { | |
31d4ee6c | 1585 | QLIST_FOREACH(ioh, &io_handlers, next) { |
7c9d8e07 FB |
1586 | if (ioh->fd == fd) |
1587 | goto found; | |
1588 | } | |
1589 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); | |
31d4ee6c | 1590 | QLIST_INSERT_HEAD(&io_handlers, ioh, next); |
7c9d8e07 FB |
1591 | found: |
1592 | ioh->fd = fd; | |
1593 | ioh->fd_read_poll = fd_read_poll; | |
1594 | ioh->fd_read = fd_read; | |
1595 | ioh->fd_write = fd_write; | |
1596 | ioh->opaque = opaque; | |
cafffd40 | 1597 | ioh->deleted = 0; |
7c9d8e07 | 1598 | } |
c4b1fcc0 FB |
1599 | return 0; |
1600 | } | |
1601 | ||
5fafdf24 TS |
1602 | int qemu_set_fd_handler(int fd, |
1603 | IOHandler *fd_read, | |
1604 | IOHandler *fd_write, | |
7c9d8e07 | 1605 | void *opaque) |
8a7ddc38 | 1606 | { |
7c9d8e07 | 1607 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
8a7ddc38 FB |
1608 | } |
1609 | ||
56f3a5d0 | 1610 | #ifdef _WIN32 |
f331110f FB |
1611 | /***********************************************************/ |
1612 | /* Polling handling */ | |
1613 | ||
1614 | typedef struct PollingEntry { | |
1615 | PollingFunc *func; | |
1616 | void *opaque; | |
1617 | struct PollingEntry *next; | |
1618 | } PollingEntry; | |
1619 | ||
1620 | static PollingEntry *first_polling_entry; | |
1621 | ||
1622 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) | |
1623 | { | |
1624 | PollingEntry **ppe, *pe; | |
1625 | pe = qemu_mallocz(sizeof(PollingEntry)); | |
f331110f FB |
1626 | pe->func = func; |
1627 | pe->opaque = opaque; | |
1628 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); | |
1629 | *ppe = pe; | |
1630 | return 0; | |
1631 | } | |
1632 | ||
1633 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) | |
1634 | { | |
1635 | PollingEntry **ppe, *pe; | |
1636 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { | |
1637 | pe = *ppe; | |
1638 | if (pe->func == func && pe->opaque == opaque) { | |
1639 | *ppe = pe->next; | |
1640 | qemu_free(pe); | |
1641 | break; | |
1642 | } | |
1643 | } | |
1644 | } | |
1645 | ||
a18e524a FB |
1646 | /***********************************************************/ |
1647 | /* Wait objects support */ | |
1648 | typedef struct WaitObjects { | |
1649 | int num; | |
1650 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; | |
1651 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; | |
1652 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; | |
1653 | } WaitObjects; | |
1654 | ||
1655 | static WaitObjects wait_objects = {0}; | |
3b46e624 | 1656 | |
a18e524a FB |
1657 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
1658 | { | |
1659 | WaitObjects *w = &wait_objects; | |
1660 | ||
1661 | if (w->num >= MAXIMUM_WAIT_OBJECTS) | |
1662 | return -1; | |
1663 | w->events[w->num] = handle; | |
1664 | w->func[w->num] = func; | |
1665 | w->opaque[w->num] = opaque; | |
1666 | w->num++; | |
1667 | return 0; | |
1668 | } | |
1669 | ||
1670 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) | |
1671 | { | |
1672 | int i, found; | |
1673 | WaitObjects *w = &wait_objects; | |
1674 | ||
1675 | found = 0; | |
1676 | for (i = 0; i < w->num; i++) { | |
1677 | if (w->events[i] == handle) | |
1678 | found = 1; | |
1679 | if (found) { | |
1680 | w->events[i] = w->events[i + 1]; | |
1681 | w->func[i] = w->func[i + 1]; | |
1682 | w->opaque[i] = w->opaque[i + 1]; | |
3b46e624 | 1683 | } |
a18e524a FB |
1684 | } |
1685 | if (found) | |
1686 | w->num--; | |
1687 | } | |
1688 | #endif | |
1689 | ||
8a7ddc38 FB |
1690 | /***********************************************************/ |
1691 | /* ram save/restore */ | |
1692 | ||
94fb0909 | 1693 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
475e4277 AL |
1694 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
1695 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
1696 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
1697 | #define RAM_SAVE_FLAG_EOS 0x10 | |
1698 | ||
1699 | static int is_dup_page(uint8_t *page, uint8_t ch) | |
8a7ddc38 | 1700 | { |
475e4277 AL |
1701 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
1702 | uint32_t *array = (uint32_t *)page; | |
1703 | int i; | |
3b46e624 | 1704 | |
475e4277 AL |
1705 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
1706 | if (array[i] != val) | |
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | return 1; | |
1711 | } | |
1712 | ||
1713 | static int ram_save_block(QEMUFile *f) | |
1714 | { | |
c227f099 AL |
1715 | static ram_addr_t current_addr = 0; |
1716 | ram_addr_t saved_addr = current_addr; | |
1717 | ram_addr_t addr = 0; | |
475e4277 AL |
1718 | int found = 0; |
1719 | ||
94a6b54f | 1720 | while (addr < last_ram_offset) { |
475e4277 | 1721 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
5579c7f3 | 1722 | uint8_t *p; |
475e4277 AL |
1723 | |
1724 | cpu_physical_memory_reset_dirty(current_addr, | |
1725 | current_addr + TARGET_PAGE_SIZE, | |
1726 | MIGRATION_DIRTY_FLAG); | |
1727 | ||
5579c7f3 | 1728 | p = qemu_get_ram_ptr(current_addr); |
475e4277 | 1729 | |
5579c7f3 | 1730 | if (is_dup_page(p, *p)) { |
475e4277 | 1731 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
5579c7f3 | 1732 | qemu_put_byte(f, *p); |
475e4277 AL |
1733 | } else { |
1734 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); | |
5579c7f3 | 1735 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
c88676f8 | 1736 | } |
475e4277 AL |
1737 | |
1738 | found = 1; | |
1739 | break; | |
c88676f8 | 1740 | } |
475e4277 | 1741 | addr += TARGET_PAGE_SIZE; |
94a6b54f | 1742 | current_addr = (saved_addr + addr) % last_ram_offset; |
8a7ddc38 | 1743 | } |
475e4277 AL |
1744 | |
1745 | return found; | |
8a7ddc38 FB |
1746 | } |
1747 | ||
84307938 | 1748 | static uint64_t bytes_transferred; |
475e4277 | 1749 | |
c227f099 | 1750 | static ram_addr_t ram_save_remaining(void) |
475e4277 | 1751 | { |
c227f099 AL |
1752 | ram_addr_t addr; |
1753 | ram_addr_t count = 0; | |
475e4277 | 1754 | |
94a6b54f | 1755 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
475e4277 AL |
1756 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
1757 | count++; | |
1758 | } | |
1759 | ||
1760 | return count; | |
1761 | } | |
1762 | ||
9f9e28cd GC |
1763 | uint64_t ram_bytes_remaining(void) |
1764 | { | |
1765 | return ram_save_remaining() * TARGET_PAGE_SIZE; | |
1766 | } | |
1767 | ||
1768 | uint64_t ram_bytes_transferred(void) | |
1769 | { | |
1770 | return bytes_transferred; | |
1771 | } | |
1772 | ||
1773 | uint64_t ram_bytes_total(void) | |
1774 | { | |
1775 | return last_ram_offset; | |
1776 | } | |
1777 | ||
f327aa0c | 1778 | static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) |
475e4277 | 1779 | { |
c227f099 | 1780 | ram_addr_t addr; |
a0a3fd60 GC |
1781 | uint64_t bytes_transferred_last; |
1782 | double bwidth = 0; | |
1783 | uint64_t expected_time = 0; | |
475e4277 | 1784 | |
4ec7fcc7 JK |
1785 | if (stage < 0) { |
1786 | cpu_physical_memory_set_dirty_tracking(0); | |
1787 | return 0; | |
1788 | } | |
1789 | ||
9fa06385 | 1790 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
b0a46a33 JK |
1791 | qemu_file_set_error(f); |
1792 | return 0; | |
1793 | } | |
1794 | ||
475e4277 | 1795 | if (stage == 1) { |
84307938 JK |
1796 | bytes_transferred = 0; |
1797 | ||
475e4277 | 1798 | /* Make sure all dirty bits are set */ |
94a6b54f | 1799 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
475e4277 AL |
1800 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
1801 | cpu_physical_memory_set_dirty(addr); | |
1802 | } | |
b0a46a33 | 1803 | |
475e4277 AL |
1804 | /* Enable dirty memory tracking */ |
1805 | cpu_physical_memory_set_dirty_tracking(1); | |
1806 | ||
94a6b54f | 1807 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
475e4277 AL |
1808 | } |
1809 | ||
a0a3fd60 | 1810 | bytes_transferred_last = bytes_transferred; |
41c872b6 | 1811 | bwidth = qemu_get_clock_ns(rt_clock); |
a0a3fd60 | 1812 | |
475e4277 AL |
1813 | while (!qemu_file_rate_limit(f)) { |
1814 | int ret; | |
1815 | ||
1816 | ret = ram_save_block(f); | |
9f9e28cd | 1817 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
475e4277 AL |
1818 | if (ret == 0) /* no more blocks */ |
1819 | break; | |
1820 | } | |
1821 | ||
41c872b6 | 1822 | bwidth = qemu_get_clock_ns(rt_clock) - bwidth; |
a0a3fd60 GC |
1823 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
1824 | ||
1825 | /* if we haven't transferred anything this round, force expected_time to a | |
1826 | * a very high value, but without crashing */ | |
1827 | if (bwidth == 0) | |
1828 | bwidth = 0.000001; | |
1829 | ||
475e4277 | 1830 | /* try transferring iterative blocks of memory */ |
475e4277 | 1831 | if (stage == 3) { |
475e4277 | 1832 | /* flush all remaining blocks regardless of rate limiting */ |
9f9e28cd GC |
1833 | while (ram_save_block(f) != 0) { |
1834 | bytes_transferred += TARGET_PAGE_SIZE; | |
1835 | } | |
8215e914 | 1836 | cpu_physical_memory_set_dirty_tracking(0); |
475e4277 AL |
1837 | } |
1838 | ||
1839 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
1840 | ||
a0a3fd60 GC |
1841 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
1842 | ||
1843 | return (stage == 2) && (expected_time <= migrate_max_downtime()); | |
475e4277 AL |
1844 | } |
1845 | ||
475e4277 AL |
1846 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
1847 | { | |
c227f099 | 1848 | ram_addr_t addr; |
475e4277 AL |
1849 | int flags; |
1850 | ||
475e4277 AL |
1851 | if (version_id != 3) |
1852 | return -EINVAL; | |
1853 | ||
1854 | do { | |
1855 | addr = qemu_get_be64(f); | |
1856 | ||
1857 | flags = addr & ~TARGET_PAGE_MASK; | |
1858 | addr &= TARGET_PAGE_MASK; | |
1859 | ||
1860 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { | |
94a6b54f | 1861 | if (addr != last_ram_offset) |
475e4277 AL |
1862 | return -EINVAL; |
1863 | } | |
1864 | ||
475e4277 AL |
1865 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
1866 | uint8_t ch = qemu_get_byte(f); | |
779c6bef AL |
1867 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
1868 | #ifndef _WIN32 | |
30868442 AL |
1869 | if (ch == 0 && |
1870 | (!kvm_enabled() || kvm_has_sync_mmu())) { | |
1871 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); | |
779c6bef | 1872 | } |
30868442 | 1873 | #endif |
9a743e5b | 1874 | } else if (flags & RAM_SAVE_FLAG_PAGE) { |
5579c7f3 | 1875 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
9a743e5b JK |
1876 | } |
1877 | if (qemu_file_has_error(f)) { | |
1878 | return -EIO; | |
1879 | } | |
475e4277 AL |
1880 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
1881 | ||
8a7ddc38 FB |
1882 | return 0; |
1883 | } | |
1884 | ||
9e472e10 AL |
1885 | void qemu_service_io(void) |
1886 | { | |
d9f75a4e | 1887 | qemu_notify_event(); |
9e472e10 AL |
1888 | } |
1889 | ||
cc1daa40 FB |
1890 | /***********************************************************/ |
1891 | /* machine registration */ | |
1892 | ||
bdaf78e0 | 1893 | static QEMUMachine *first_machine = NULL; |
6f338c34 | 1894 | QEMUMachine *current_machine = NULL; |
cc1daa40 FB |
1895 | |
1896 | int qemu_register_machine(QEMUMachine *m) | |
1897 | { | |
1898 | QEMUMachine **pm; | |
1899 | pm = &first_machine; | |
1900 | while (*pm != NULL) | |
1901 | pm = &(*pm)->next; | |
1902 | m->next = NULL; | |
1903 | *pm = m; | |
1904 | return 0; | |
1905 | } | |
1906 | ||
9596ebb7 | 1907 | static QEMUMachine *find_machine(const char *name) |
cc1daa40 FB |
1908 | { |
1909 | QEMUMachine *m; | |
1910 | ||
1911 | for(m = first_machine; m != NULL; m = m->next) { | |
1912 | if (!strcmp(m->name, name)) | |
1913 | return m; | |
3f6599e6 MM |
1914 | if (m->alias && !strcmp(m->alias, name)) |
1915 | return m; | |
cc1daa40 FB |
1916 | } |
1917 | return NULL; | |
1918 | } | |
1919 | ||
0c257437 AL |
1920 | static QEMUMachine *find_default_machine(void) |
1921 | { | |
1922 | QEMUMachine *m; | |
1923 | ||
1924 | for(m = first_machine; m != NULL; m = m->next) { | |
1925 | if (m->is_default) { | |
1926 | return m; | |
1927 | } | |
1928 | } | |
1929 | return NULL; | |
1930 | } | |
1931 | ||
8a7ddc38 FB |
1932 | /***********************************************************/ |
1933 | /* main execution loop */ | |
1934 | ||
9596ebb7 | 1935 | static void gui_update(void *opaque) |
8a7ddc38 | 1936 | { |
7d957bd8 | 1937 | uint64_t interval = GUI_REFRESH_INTERVAL; |
740733bb | 1938 | DisplayState *ds = opaque; |
7d957bd8 AL |
1939 | DisplayChangeListener *dcl = ds->listeners; |
1940 | ||
62a2744c | 1941 | qemu_flush_coalesced_mmio_buffer(); |
7d957bd8 AL |
1942 | dpy_refresh(ds); |
1943 | ||
1944 | while (dcl != NULL) { | |
1945 | if (dcl->gui_timer_interval && | |
1946 | dcl->gui_timer_interval < interval) | |
1947 | interval = dcl->gui_timer_interval; | |
1948 | dcl = dcl->next; | |
1949 | } | |
1950 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); | |
8a7ddc38 FB |
1951 | } |
1952 | ||
9043b62d BS |
1953 | static void nographic_update(void *opaque) |
1954 | { | |
1955 | uint64_t interval = GUI_REFRESH_INTERVAL; | |
1956 | ||
62a2744c | 1957 | qemu_flush_coalesced_mmio_buffer(); |
9043b62d BS |
1958 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
1959 | } | |
1960 | ||
ea375f9a JK |
1961 | void cpu_synchronize_all_states(void) |
1962 | { | |
1963 | CPUState *cpu; | |
1964 | ||
1965 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { | |
1966 | cpu_synchronize_state(cpu); | |
1967 | } | |
1968 | } | |
1969 | ||
1970 | void cpu_synchronize_all_post_reset(void) | |
1971 | { | |
1972 | CPUState *cpu; | |
1973 | ||
1974 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { | |
1975 | cpu_synchronize_post_reset(cpu); | |
1976 | } | |
1977 | } | |
1978 | ||
1979 | void cpu_synchronize_all_post_init(void) | |
1980 | { | |
1981 | CPUState *cpu; | |
1982 | ||
1983 | for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) { | |
1984 | cpu_synchronize_post_init(cpu); | |
1985 | } | |
1986 | } | |
1987 | ||
0bd48850 FB |
1988 | struct vm_change_state_entry { |
1989 | VMChangeStateHandler *cb; | |
1990 | void *opaque; | |
72cf2d4f | 1991 | QLIST_ENTRY (vm_change_state_entry) entries; |
0bd48850 FB |
1992 | }; |
1993 | ||
72cf2d4f | 1994 | static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
0bd48850 FB |
1995 | |
1996 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, | |
1997 | void *opaque) | |
1998 | { | |
1999 | VMChangeStateEntry *e; | |
2000 | ||
2001 | e = qemu_mallocz(sizeof (*e)); | |
0bd48850 FB |
2002 | |
2003 | e->cb = cb; | |
2004 | e->opaque = opaque; | |
72cf2d4f | 2005 | QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
0bd48850 FB |
2006 | return e; |
2007 | } | |
2008 | ||
2009 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) | |
2010 | { | |
72cf2d4f | 2011 | QLIST_REMOVE (e, entries); |
0bd48850 FB |
2012 | qemu_free (e); |
2013 | } | |
2014 | ||
9781e040 | 2015 | static void vm_state_notify(int running, int reason) |
0bd48850 FB |
2016 | { |
2017 | VMChangeStateEntry *e; | |
2018 | ||
2019 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { | |
9781e040 | 2020 | e->cb(e->opaque, running, reason); |
0bd48850 FB |
2021 | } |
2022 | } | |
2023 | ||
d6dc3d42 AL |
2024 | static void resume_all_vcpus(void); |
2025 | static void pause_all_vcpus(void); | |
2026 | ||
8a7ddc38 FB |
2027 | void vm_start(void) |
2028 | { | |
2029 | if (!vm_running) { | |
2030 | cpu_enable_ticks(); | |
2031 | vm_running = 1; | |
9781e040 | 2032 | vm_state_notify(1, 0); |
d6dc3d42 | 2033 | resume_all_vcpus(); |
8a7ddc38 FB |
2034 | } |
2035 | } | |
2036 | ||
bb0c6722 FB |
2037 | /* reset/shutdown handler */ |
2038 | ||
2039 | typedef struct QEMUResetEntry { | |
72cf2d4f | 2040 | QTAILQ_ENTRY(QEMUResetEntry) entry; |
bb0c6722 FB |
2041 | QEMUResetHandler *func; |
2042 | void *opaque; | |
bb0c6722 FB |
2043 | } QEMUResetEntry; |
2044 | ||
72cf2d4f BS |
2045 | static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
2046 | QTAILQ_HEAD_INITIALIZER(reset_handlers); | |
bb0c6722 FB |
2047 | static int reset_requested; |
2048 | static int shutdown_requested; | |
3475187d | 2049 | static int powerdown_requested; |
e568902a | 2050 | static int debug_requested; |
6e29f5da | 2051 | static int vmstop_requested; |
bb0c6722 | 2052 | |
cf7a2fe2 AJ |
2053 | int qemu_shutdown_requested(void) |
2054 | { | |
2055 | int r = shutdown_requested; | |
2056 | shutdown_requested = 0; | |
2057 | return r; | |
2058 | } | |
2059 | ||
2060 | int qemu_reset_requested(void) | |
2061 | { | |
2062 | int r = reset_requested; | |
2063 | reset_requested = 0; | |
2064 | return r; | |
2065 | } | |
2066 | ||
2067 | int qemu_powerdown_requested(void) | |
2068 | { | |
2069 | int r = powerdown_requested; | |
2070 | powerdown_requested = 0; | |
2071 | return r; | |
2072 | } | |
2073 | ||
e568902a AL |
2074 | static int qemu_debug_requested(void) |
2075 | { | |
2076 | int r = debug_requested; | |
2077 | debug_requested = 0; | |
2078 | return r; | |
2079 | } | |
2080 | ||
6e29f5da AL |
2081 | static int qemu_vmstop_requested(void) |
2082 | { | |
2083 | int r = vmstop_requested; | |
2084 | vmstop_requested = 0; | |
2085 | return r; | |
2086 | } | |
2087 | ||
2088 | static void do_vm_stop(int reason) | |
2089 | { | |
2090 | if (vm_running) { | |
2091 | cpu_disable_ticks(); | |
2092 | vm_running = 0; | |
d6dc3d42 | 2093 | pause_all_vcpus(); |
6e29f5da | 2094 | vm_state_notify(0, reason); |
6b8f8fff | 2095 | monitor_protocol_event(QEVENT_STOP, NULL); |
6e29f5da AL |
2096 | } |
2097 | } | |
2098 | ||
a08d4367 | 2099 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bb0c6722 | 2100 | { |
55ddfe8e | 2101 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bb0c6722 | 2102 | |
bb0c6722 FB |
2103 | re->func = func; |
2104 | re->opaque = opaque; | |
72cf2d4f | 2105 | QTAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bb0c6722 FB |
2106 | } |
2107 | ||
dda9b29f | 2108 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bb0c6722 FB |
2109 | { |
2110 | QEMUResetEntry *re; | |
2111 | ||
72cf2d4f | 2112 | QTAILQ_FOREACH(re, &reset_handlers, entry) { |
dda9b29f | 2113 | if (re->func == func && re->opaque == opaque) { |
72cf2d4f | 2114 | QTAILQ_REMOVE(&reset_handlers, re, entry); |
dda9b29f JK |
2115 | qemu_free(re); |
2116 | return; | |
2117 | } | |
2118 | } | |
2119 | } | |
2120 | ||
2121 | void qemu_system_reset(void) | |
2122 | { | |
2123 | QEMUResetEntry *re, *nre; | |
2124 | ||
2125 | /* reset all devices */ | |
72cf2d4f | 2126 | QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bb0c6722 FB |
2127 | re->func(re->opaque); |
2128 | } | |
81d9b784 | 2129 | monitor_protocol_event(QEVENT_RESET, NULL); |
ea375f9a | 2130 | cpu_synchronize_all_post_reset(); |
bb0c6722 FB |
2131 | } |
2132 | ||
2133 | void qemu_system_reset_request(void) | |
2134 | { | |
d1beab82 FB |
2135 | if (no_reboot) { |
2136 | shutdown_requested = 1; | |
2137 | } else { | |
2138 | reset_requested = 1; | |
2139 | } | |
d9f75a4e | 2140 | qemu_notify_event(); |
bb0c6722 FB |
2141 | } |
2142 | ||
2143 | void qemu_system_shutdown_request(void) | |
2144 | { | |
2145 | shutdown_requested = 1; | |
d9f75a4e | 2146 | qemu_notify_event(); |
bb0c6722 FB |
2147 | } |
2148 | ||
3475187d FB |
2149 | void qemu_system_powerdown_request(void) |
2150 | { | |
2151 | powerdown_requested = 1; | |
d9f75a4e AL |
2152 | qemu_notify_event(); |
2153 | } | |
2154 | ||
16b151c3 | 2155 | static int cpu_can_run(CPUState *env) |
d9f75a4e | 2156 | { |
16b151c3 PB |
2157 | if (env->stop) |
2158 | return 0; | |
2159 | if (env->stopped) | |
2160 | return 0; | |
2161 | if (!vm_running) | |
2162 | return 0; | |
2163 | return 1; | |
2164 | } | |
2165 | ||
2166 | static int cpu_has_work(CPUState *env) | |
2167 | { | |
2168 | if (env->stop) | |
2169 | return 1; | |
2170 | if (env->stopped) | |
2171 | return 0; | |
2172 | if (!env->halted) | |
2173 | return 1; | |
2174 | if (qemu_cpu_has_work(env)) | |
2175 | return 1; | |
2176 | return 0; | |
2177 | } | |
2178 | ||
2179 | static int tcg_has_work(void) | |
2180 | { | |
2181 | CPUState *env; | |
2182 | ||
2183 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
2184 | if (cpu_has_work(env)) | |
2185 | return 1; | |
2186 | return 0; | |
bb0c6722 FB |
2187 | } |
2188 | ||
50317c7f AL |
2189 | #ifndef _WIN32 |
2190 | static int io_thread_fd = -1; | |
2191 | ||
2192 | static void qemu_event_increment(void) | |
3fcf7b6b | 2193 | { |
f3dfda61 PB |
2194 | /* Write 8 bytes to be compatible with eventfd. */ |
2195 | static uint64_t val = 1; | |
dc330e28 | 2196 | ssize_t ret; |
50317c7f AL |
2197 | |
2198 | if (io_thread_fd == -1) | |
2199 | return; | |
2200 | ||
652ce2d4 | 2201 | do { |
f3dfda61 | 2202 | ret = write(io_thread_fd, &val, sizeof(val)); |
652ce2d4 PB |
2203 | } while (ret < 0 && errno == EINTR); |
2204 | ||
2205 | /* EAGAIN is fine, a read must be pending. */ | |
2206 | if (ret < 0 && errno != EAGAIN) { | |
dc330e28 KS |
2207 | fprintf(stderr, "qemu_event_increment: write() filed: %s\n", |
2208 | strerror(errno)); | |
2209 | exit (1); | |
2210 | } | |
50317c7f AL |
2211 | } |
2212 | ||
2213 | static void qemu_event_read(void *opaque) | |
2214 | { | |
2215 | int fd = (unsigned long)opaque; | |
2216 | ssize_t len; | |
1d0f0d91 | 2217 | char buffer[512]; |
50317c7f | 2218 | |
f3dfda61 | 2219 | /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
50317c7f | 2220 | do { |
50317c7f | 2221 | len = read(fd, buffer, sizeof(buffer)); |
1d0f0d91 | 2222 | } while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
50317c7f AL |
2223 | } |
2224 | ||
2225 | static int qemu_event_init(void) | |
2226 | { | |
2227 | int err; | |
2228 | int fds[2]; | |
2229 | ||
f3dfda61 | 2230 | err = qemu_eventfd(fds); |
50317c7f AL |
2231 | if (err == -1) |
2232 | return -errno; | |
2233 | ||
2234 | err = fcntl_setfl(fds[0], O_NONBLOCK); | |
2235 | if (err < 0) | |
2236 | goto fail; | |
2237 | ||
2238 | err = fcntl_setfl(fds[1], O_NONBLOCK); | |
2239 | if (err < 0) | |
2240 | goto fail; | |
2241 | ||
2242 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, | |
2243 | (void *)(unsigned long)fds[0]); | |
2244 | ||
2245 | io_thread_fd = fds[1]; | |
a7e21219 JK |
2246 | return 0; |
2247 | ||
50317c7f AL |
2248 | fail: |
2249 | close(fds[0]); | |
2250 | close(fds[1]); | |
2251 | return err; | |
2252 | } | |
2253 | #else | |
2254 | HANDLE qemu_event_handle; | |
2255 | ||
2256 | static void dummy_event_handler(void *opaque) | |
2257 | { | |
2258 | } | |
2259 | ||
2260 | static int qemu_event_init(void) | |
2261 | { | |
2262 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2263 | if (!qemu_event_handle) { | |
20889d4e | 2264 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
50317c7f AL |
2265 | return -1; |
2266 | } | |
2267 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); | |
3fcf7b6b AL |
2268 | return 0; |
2269 | } | |
2270 | ||
50317c7f AL |
2271 | static void qemu_event_increment(void) |
2272 | { | |
de1c90cf | 2273 | if (!SetEvent(qemu_event_handle)) { |
20889d4e | 2274 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
de1c90cf | 2275 | GetLastError()); |
2276 | exit (1); | |
2277 | } | |
50317c7f AL |
2278 | } |
2279 | #endif | |
2280 | ||
d6dc3d42 | 2281 | #ifndef CONFIG_IOTHREAD |
50317c7f AL |
2282 | static int qemu_init_main_loop(void) |
2283 | { | |
2284 | return qemu_event_init(); | |
2285 | } | |
2286 | ||
0bf46a40 AL |
2287 | void qemu_init_vcpu(void *_env) |
2288 | { | |
2289 | CPUState *env = _env; | |
2290 | ||
dc6b1c09 AP |
2291 | env->nr_cores = smp_cores; |
2292 | env->nr_threads = smp_threads; | |
3f7638ec JD |
2293 | if (kvm_enabled()) |
2294 | kvm_init_vcpu(env); | |
0bf46a40 AL |
2295 | return; |
2296 | } | |
2297 | ||
8edac960 AL |
2298 | int qemu_cpu_self(void *env) |
2299 | { | |
2300 | return 1; | |
2301 | } | |
2302 | ||
d6dc3d42 AL |
2303 | static void resume_all_vcpus(void) |
2304 | { | |
2305 | } | |
2306 | ||
2307 | static void pause_all_vcpus(void) | |
2308 | { | |
2309 | } | |
2310 | ||
8edac960 AL |
2311 | void qemu_cpu_kick(void *env) |
2312 | { | |
2313 | return; | |
2314 | } | |
2315 | ||
d6dc3d42 AL |
2316 | void qemu_notify_event(void) |
2317 | { | |
2318 | CPUState *env = cpu_single_env; | |
2319 | ||
7a5e5838 | 2320 | qemu_event_increment (); |
d6dc3d42 AL |
2321 | if (env) { |
2322 | cpu_exit(env); | |
4a1418e0 | 2323 | } |
1db89e91 PB |
2324 | if (next_cpu && env != next_cpu) { |
2325 | cpu_exit(next_cpu); | |
2326 | } | |
d6dc3d42 AL |
2327 | } |
2328 | ||
d549db5a GC |
2329 | void qemu_mutex_lock_iothread(void) {} |
2330 | void qemu_mutex_unlock_iothread(void) {} | |
4870852c | 2331 | |
6e29f5da AL |
2332 | void vm_stop(int reason) |
2333 | { | |
2334 | do_vm_stop(reason); | |
2335 | } | |
2336 | ||
d6dc3d42 AL |
2337 | #else /* CONFIG_IOTHREAD */ |
2338 | ||
2339 | #include "qemu-thread.h" | |
2340 | ||
2341 | QemuMutex qemu_global_mutex; | |
2342 | static QemuMutex qemu_fair_mutex; | |
2343 | ||
2344 | static QemuThread io_thread; | |
2345 | ||
2346 | static QemuThread *tcg_cpu_thread; | |
2347 | static QemuCond *tcg_halt_cond; | |
2348 | ||
2349 | static int qemu_system_ready; | |
2350 | /* cpu creation */ | |
2351 | static QemuCond qemu_cpu_cond; | |
2352 | /* system init */ | |
2353 | static QemuCond qemu_system_cond; | |
2354 | static QemuCond qemu_pause_cond; | |
2355 | ||
cc84de95 MT |
2356 | static void tcg_block_io_signals(void); |
2357 | static void kvm_block_io_signals(CPUState *env); | |
d6dc3d42 | 2358 | static void unblock_io_signals(void); |
d6dc3d42 AL |
2359 | |
2360 | static int qemu_init_main_loop(void) | |
2361 | { | |
2362 | int ret; | |
2363 | ||
2364 | ret = qemu_event_init(); | |
2365 | if (ret) | |
2366 | return ret; | |
2367 | ||
2368 | qemu_cond_init(&qemu_pause_cond); | |
2369 | qemu_mutex_init(&qemu_fair_mutex); | |
2370 | qemu_mutex_init(&qemu_global_mutex); | |
2371 | qemu_mutex_lock(&qemu_global_mutex); | |
2372 | ||
2373 | unblock_io_signals(); | |
2374 | qemu_thread_self(&io_thread); | |
2375 | ||
2376 | return 0; | |
2377 | } | |
2378 | ||
fed6c344 MT |
2379 | static void qemu_wait_io_event_common(CPUState *env) |
2380 | { | |
2381 | if (env->stop) { | |
2382 | env->stop = 0; | |
2383 | env->stopped = 1; | |
2384 | qemu_cond_signal(&qemu_pause_cond); | |
2385 | } | |
2386 | } | |
2387 | ||
d6dc3d42 AL |
2388 | static void qemu_wait_io_event(CPUState *env) |
2389 | { | |
2390 | while (!tcg_has_work()) | |
2391 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); | |
2392 | ||
2393 | qemu_mutex_unlock(&qemu_global_mutex); | |
2394 | ||
2395 | /* | |
2396 | * Users of qemu_global_mutex can be starved, having no chance | |
2397 | * to acquire it since this path will get to it first. | |
2398 | * So use another lock to provide fairness. | |
2399 | */ | |
2400 | qemu_mutex_lock(&qemu_fair_mutex); | |
2401 | qemu_mutex_unlock(&qemu_fair_mutex); | |
2402 | ||
2403 | qemu_mutex_lock(&qemu_global_mutex); | |
fed6c344 MT |
2404 | qemu_wait_io_event_common(env); |
2405 | } | |
2406 | ||
cc84de95 MT |
2407 | static void qemu_kvm_eat_signal(CPUState *env, int timeout) |
2408 | { | |
2409 | struct timespec ts; | |
2410 | int r, e; | |
2411 | siginfo_t siginfo; | |
2412 | sigset_t waitset; | |
2413 | ||
2414 | ts.tv_sec = timeout / 1000; | |
2415 | ts.tv_nsec = (timeout % 1000) * 1000000; | |
2416 | ||
2417 | sigemptyset(&waitset); | |
2418 | sigaddset(&waitset, SIG_IPI); | |
2419 | ||
2420 | qemu_mutex_unlock(&qemu_global_mutex); | |
2421 | r = sigtimedwait(&waitset, &siginfo, &ts); | |
2422 | e = errno; | |
2423 | qemu_mutex_lock(&qemu_global_mutex); | |
2424 | ||
2425 | if (r == -1 && !(e == EAGAIN || e == EINTR)) { | |
2426 | fprintf(stderr, "sigtimedwait: %s\n", strerror(e)); | |
2427 | exit(1); | |
d6dc3d42 AL |
2428 | } |
2429 | } | |
2430 | ||
fed6c344 MT |
2431 | static void qemu_kvm_wait_io_event(CPUState *env) |
2432 | { | |
2433 | while (!cpu_has_work(env)) | |
2434 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); | |
2435 | ||
cc84de95 | 2436 | qemu_kvm_eat_signal(env, 0); |
fed6c344 | 2437 | qemu_wait_io_event_common(env); |
d6dc3d42 AL |
2438 | } |
2439 | ||
2440 | static int qemu_cpu_exec(CPUState *env); | |
2441 | ||
2442 | static void *kvm_cpu_thread_fn(void *arg) | |
2443 | { | |
2444 | CPUState *env = arg; | |
2445 | ||
d6dc3d42 | 2446 | qemu_thread_self(env->thread); |
321c1cb1 JCD |
2447 | if (kvm_enabled()) |
2448 | kvm_init_vcpu(env); | |
d6dc3d42 | 2449 | |
cc84de95 MT |
2450 | kvm_block_io_signals(env); |
2451 | ||
d6dc3d42 AL |
2452 | /* signal CPU creation */ |
2453 | qemu_mutex_lock(&qemu_global_mutex); | |
2454 | env->created = 1; | |
2455 | qemu_cond_signal(&qemu_cpu_cond); | |
2456 | ||
2457 | /* and wait for machine initialization */ | |
2458 | while (!qemu_system_ready) | |
2459 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
2460 | ||
2461 | while (1) { | |
2462 | if (cpu_can_run(env)) | |
2463 | qemu_cpu_exec(env); | |
fed6c344 | 2464 | qemu_kvm_wait_io_event(env); |
d6dc3d42 AL |
2465 | } |
2466 | ||
2467 | return NULL; | |
2468 | } | |
2469 | ||
d6f4ade2 | 2470 | static bool tcg_cpu_exec(void); |
d6dc3d42 AL |
2471 | |
2472 | static void *tcg_cpu_thread_fn(void *arg) | |
2473 | { | |
2474 | CPUState *env = arg; | |
2475 | ||
cc84de95 | 2476 | tcg_block_io_signals(); |
d6dc3d42 AL |
2477 | qemu_thread_self(env->thread); |
2478 | ||
2479 | /* signal CPU creation */ | |
2480 | qemu_mutex_lock(&qemu_global_mutex); | |
2481 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
2482 | env->created = 1; | |
2483 | qemu_cond_signal(&qemu_cpu_cond); | |
2484 | ||
2485 | /* and wait for machine initialization */ | |
2486 | while (!qemu_system_ready) | |
2487 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
2488 | ||
2489 | while (1) { | |
2490 | tcg_cpu_exec(); | |
2491 | qemu_wait_io_event(cur_cpu); | |
2492 | } | |
2493 | ||
2494 | return NULL; | |
2495 | } | |
2496 | ||
2497 | void qemu_cpu_kick(void *_env) | |
2498 | { | |
2499 | CPUState *env = _env; | |
2500 | qemu_cond_broadcast(env->halt_cond); | |
2501 | if (kvm_enabled()) | |
cc84de95 | 2502 | qemu_thread_signal(env->thread, SIG_IPI); |
d6dc3d42 AL |
2503 | } |
2504 | ||
e5bc201d | 2505 | int qemu_cpu_self(void *_env) |
d6dc3d42 | 2506 | { |
e5bc201d GC |
2507 | CPUState *env = _env; |
2508 | QemuThread this; | |
2509 | ||
2510 | qemu_thread_self(&this); | |
2511 | ||
2512 | return qemu_thread_equal(&this, env->thread); | |
d6dc3d42 AL |
2513 | } |
2514 | ||
2515 | static void cpu_signal(int sig) | |
2516 | { | |
2517 | if (cpu_single_env) | |
2518 | cpu_exit(cpu_single_env); | |
2519 | } | |
2520 | ||
cc84de95 | 2521 | static void tcg_block_io_signals(void) |
d6dc3d42 AL |
2522 | { |
2523 | sigset_t set; | |
2524 | struct sigaction sigact; | |
2525 | ||
2526 | sigemptyset(&set); | |
2527 | sigaddset(&set, SIGUSR2); | |
2528 | sigaddset(&set, SIGIO); | |
2529 | sigaddset(&set, SIGALRM); | |
adc8c965 | 2530 | sigaddset(&set, SIGCHLD); |
d6dc3d42 AL |
2531 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
2532 | ||
2533 | sigemptyset(&set); | |
cc84de95 | 2534 | sigaddset(&set, SIG_IPI); |
d6dc3d42 AL |
2535 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
2536 | ||
2537 | memset(&sigact, 0, sizeof(sigact)); | |
2538 | sigact.sa_handler = cpu_signal; | |
cc84de95 MT |
2539 | sigaction(SIG_IPI, &sigact, NULL); |
2540 | } | |
2541 | ||
2542 | static void dummy_signal(int sig) | |
2543 | { | |
2544 | } | |
2545 | ||
2546 | static void kvm_block_io_signals(CPUState *env) | |
2547 | { | |
2548 | int r; | |
2549 | sigset_t set; | |
2550 | struct sigaction sigact; | |
2551 | ||
2552 | sigemptyset(&set); | |
2553 | sigaddset(&set, SIGUSR2); | |
2554 | sigaddset(&set, SIGIO); | |
2555 | sigaddset(&set, SIGALRM); | |
2556 | sigaddset(&set, SIGCHLD); | |
2557 | sigaddset(&set, SIG_IPI); | |
2558 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
2559 | ||
2560 | pthread_sigmask(SIG_BLOCK, NULL, &set); | |
2561 | sigdelset(&set, SIG_IPI); | |
2562 | ||
2563 | memset(&sigact, 0, sizeof(sigact)); | |
2564 | sigact.sa_handler = dummy_signal; | |
2565 | sigaction(SIG_IPI, &sigact, NULL); | |
2566 | ||
2567 | r = kvm_set_signal_mask(env, &set); | |
2568 | if (r) { | |
2569 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r)); | |
2570 | exit(1); | |
2571 | } | |
d6dc3d42 AL |
2572 | } |
2573 | ||
2574 | static void unblock_io_signals(void) | |
2575 | { | |
2576 | sigset_t set; | |
2577 | ||
2578 | sigemptyset(&set); | |
2579 | sigaddset(&set, SIGUSR2); | |
2580 | sigaddset(&set, SIGIO); | |
2581 | sigaddset(&set, SIGALRM); | |
2582 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
2583 | ||
2584 | sigemptyset(&set); | |
cc84de95 | 2585 | sigaddset(&set, SIG_IPI); |
d6dc3d42 AL |
2586 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
2587 | } | |
2588 | ||
2589 | static void qemu_signal_lock(unsigned int msecs) | |
2590 | { | |
2591 | qemu_mutex_lock(&qemu_fair_mutex); | |
2592 | ||
2593 | while (qemu_mutex_trylock(&qemu_global_mutex)) { | |
cc84de95 | 2594 | qemu_thread_signal(tcg_cpu_thread, SIG_IPI); |
d6dc3d42 AL |
2595 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
2596 | break; | |
2597 | } | |
2598 | qemu_mutex_unlock(&qemu_fair_mutex); | |
2599 | } | |
2600 | ||
d549db5a | 2601 | void qemu_mutex_lock_iothread(void) |
d6dc3d42 AL |
2602 | { |
2603 | if (kvm_enabled()) { | |
2604 | qemu_mutex_lock(&qemu_fair_mutex); | |
2605 | qemu_mutex_lock(&qemu_global_mutex); | |
2606 | qemu_mutex_unlock(&qemu_fair_mutex); | |
2607 | } else | |
2608 | qemu_signal_lock(100); | |
2609 | } | |
2610 | ||
d549db5a | 2611 | void qemu_mutex_unlock_iothread(void) |
d6dc3d42 AL |
2612 | { |
2613 | qemu_mutex_unlock(&qemu_global_mutex); | |
2614 | } | |
2615 | ||
2616 | static int all_vcpus_paused(void) | |
2617 | { | |
2618 | CPUState *penv = first_cpu; | |
2619 | ||
2620 | while (penv) { | |
2621 | if (!penv->stopped) | |
2622 | return 0; | |
2623 | penv = (CPUState *)penv->next_cpu; | |
2624 | } | |
2625 | ||
2626 | return 1; | |
2627 | } | |
2628 | ||
2629 | static void pause_all_vcpus(void) | |
2630 | { | |
2631 | CPUState *penv = first_cpu; | |
2632 | ||
2633 | while (penv) { | |
2634 | penv->stop = 1; | |
cc84de95 | 2635 | qemu_thread_signal(penv->thread, SIG_IPI); |
d6dc3d42 AL |
2636 | qemu_cpu_kick(penv); |
2637 | penv = (CPUState *)penv->next_cpu; | |
2638 | } | |
2639 | ||
2640 | while (!all_vcpus_paused()) { | |
2641 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); | |
2642 | penv = first_cpu; | |
2643 | while (penv) { | |
cc84de95 | 2644 | qemu_thread_signal(penv->thread, SIG_IPI); |
d6dc3d42 AL |
2645 | penv = (CPUState *)penv->next_cpu; |
2646 | } | |
2647 | } | |
2648 | } | |
2649 | ||
2650 | static void resume_all_vcpus(void) | |
2651 | { | |
2652 | CPUState *penv = first_cpu; | |
2653 | ||
2654 | while (penv) { | |
2655 | penv->stop = 0; | |
2656 | penv->stopped = 0; | |
cc84de95 | 2657 | qemu_thread_signal(penv->thread, SIG_IPI); |
d6dc3d42 AL |
2658 | qemu_cpu_kick(penv); |
2659 | penv = (CPUState *)penv->next_cpu; | |
2660 | } | |
2661 | } | |
2662 | ||
2663 | static void tcg_init_vcpu(void *_env) | |
2664 | { | |
2665 | CPUState *env = _env; | |
2666 | /* share a single thread for all cpus with TCG */ | |
2667 | if (!tcg_cpu_thread) { | |
2668 | env->thread = qemu_mallocz(sizeof(QemuThread)); | |
2669 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
2670 | qemu_cond_init(env->halt_cond); | |
2671 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); | |
2672 | while (env->created == 0) | |
2673 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
2674 | tcg_cpu_thread = env->thread; | |
2675 | tcg_halt_cond = env->halt_cond; | |
2676 | } else { | |
2677 | env->thread = tcg_cpu_thread; | |
2678 | env->halt_cond = tcg_halt_cond; | |
2679 | } | |
2680 | } | |
2681 | ||
2682 | static void kvm_start_vcpu(CPUState *env) | |
2683 | { | |
d6dc3d42 AL |
2684 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
2685 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
2686 | qemu_cond_init(env->halt_cond); | |
2687 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); | |
2688 | while (env->created == 0) | |
2689 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
2690 | } | |
2691 | ||
2692 | void qemu_init_vcpu(void *_env) | |
2693 | { | |
2694 | CPUState *env = _env; | |
2695 | ||
3f7638ec JD |
2696 | env->nr_cores = smp_cores; |
2697 | env->nr_threads = smp_threads; | |
d6dc3d42 AL |
2698 | if (kvm_enabled()) |
2699 | kvm_start_vcpu(env); | |
2700 | else | |
2701 | tcg_init_vcpu(env); | |
2702 | } | |
2703 | ||
2704 | void qemu_notify_event(void) | |
2705 | { | |
2706 | qemu_event_increment(); | |
2707 | } | |
2708 | ||
16b151c3 PB |
2709 | static void qemu_system_vmstop_request(int reason) |
2710 | { | |
2711 | vmstop_requested = reason; | |
2712 | qemu_notify_event(); | |
2713 | } | |
2714 | ||
d6dc3d42 AL |
2715 | void vm_stop(int reason) |
2716 | { | |
2717 | QemuThread me; | |
2718 | qemu_thread_self(&me); | |
2719 | ||
2720 | if (!qemu_thread_equal(&me, &io_thread)) { | |
2721 | qemu_system_vmstop_request(reason); | |
2722 | /* | |
2723 | * FIXME: should not return to device code in case | |
2724 | * vm_stop() has been requested. | |
2725 | */ | |
2726 | if (cpu_single_env) { | |
2727 | cpu_exit(cpu_single_env); | |
2728 | cpu_single_env->stop = 1; | |
2729 | } | |
2730 | return; | |
2731 | } | |
2732 | do_vm_stop(reason); | |
2733 | } | |
2734 | ||
2735 | #endif | |
2736 | ||
2737 | ||
877cf882 | 2738 | #ifdef _WIN32 |
69d6451c | 2739 | static void host_main_loop_wait(int *timeout) |
56f3a5d0 AL |
2740 | { |
2741 | int ret, ret2, i; | |
f331110f FB |
2742 | PollingEntry *pe; |
2743 | ||
c4b1fcc0 | 2744 | |
f331110f FB |
2745 | /* XXX: need to suppress polling by better using win32 events */ |
2746 | ret = 0; | |
2747 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { | |
2748 | ret |= pe->func(pe->opaque); | |
2749 | } | |
e6b1e558 | 2750 | if (ret == 0) { |
a18e524a FB |
2751 | int err; |
2752 | WaitObjects *w = &wait_objects; | |
3b46e624 | 2753 | |
56f3a5d0 | 2754 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
a18e524a FB |
2755 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
2756 | if (w->func[ret - WAIT_OBJECT_0]) | |
2757 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); | |
3b46e624 | 2758 | |
5fafdf24 | 2759 | /* Check for additional signaled events */ |
e6b1e558 | 2760 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
3b46e624 | 2761 | |
e6b1e558 TS |
2762 | /* Check if event is signaled */ |
2763 | ret2 = WaitForSingleObject(w->events[i], 0); | |
2764 | if(ret2 == WAIT_OBJECT_0) { | |
2765 | if (w->func[i]) | |
2766 | w->func[i](w->opaque[i]); | |
2767 | } else if (ret2 == WAIT_TIMEOUT) { | |
2768 | } else { | |
2769 | err = GetLastError(); | |
2770 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); | |
3b46e624 TS |
2771 | } |
2772 | } | |
a18e524a FB |
2773 | } else if (ret == WAIT_TIMEOUT) { |
2774 | } else { | |
2775 | err = GetLastError(); | |
e6b1e558 | 2776 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
a18e524a | 2777 | } |
f331110f | 2778 | } |
56f3a5d0 AL |
2779 | |
2780 | *timeout = 0; | |
2781 | } | |
2782 | #else | |
69d6451c | 2783 | static void host_main_loop_wait(int *timeout) |
56f3a5d0 AL |
2784 | { |
2785 | } | |
fd1dff4b | 2786 | #endif |
56f3a5d0 | 2787 | |
d6f4ade2 | 2788 | void main_loop_wait(int nonblocking) |
56f3a5d0 AL |
2789 | { |
2790 | IOHandlerRecord *ioh; | |
2791 | fd_set rfds, wfds, xfds; | |
2792 | int ret, nfds; | |
2793 | struct timeval tv; | |
d6f4ade2 | 2794 | int timeout; |
56f3a5d0 | 2795 | |
d6f4ade2 PB |
2796 | if (nonblocking) |
2797 | timeout = 0; | |
2798 | else { | |
2799 | timeout = qemu_calculate_timeout(); | |
2800 | qemu_bh_update_timeout(&timeout); | |
2801 | } | |
56f3a5d0 AL |
2802 | |
2803 | host_main_loop_wait(&timeout); | |
2804 | ||
fd1dff4b FB |
2805 | /* poll any events */ |
2806 | /* XXX: separate device handlers from system ones */ | |
6abfbd79 | 2807 | nfds = -1; |
fd1dff4b FB |
2808 | FD_ZERO(&rfds); |
2809 | FD_ZERO(&wfds); | |
e035649e | 2810 | FD_ZERO(&xfds); |
31d4ee6c | 2811 | QLIST_FOREACH(ioh, &io_handlers, next) { |
cafffd40 TS |
2812 | if (ioh->deleted) |
2813 | continue; | |
fd1dff4b FB |
2814 | if (ioh->fd_read && |
2815 | (!ioh->fd_read_poll || | |
2816 | ioh->fd_read_poll(ioh->opaque) != 0)) { | |
2817 | FD_SET(ioh->fd, &rfds); | |
2818 | if (ioh->fd > nfds) | |
2819 | nfds = ioh->fd; | |
2820 | } | |
2821 | if (ioh->fd_write) { | |
2822 | FD_SET(ioh->fd, &wfds); | |
2823 | if (ioh->fd > nfds) | |
2824 | nfds = ioh->fd; | |
2825 | } | |
2826 | } | |
3b46e624 | 2827 | |
56f3a5d0 AL |
2828 | tv.tv_sec = timeout / 1000; |
2829 | tv.tv_usec = (timeout % 1000) * 1000; | |
2830 | ||
d918f23e JK |
2831 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
2832 | ||
4870852c | 2833 | qemu_mutex_unlock_iothread(); |
e035649e | 2834 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
4870852c | 2835 | qemu_mutex_lock_iothread(); |
fd1dff4b | 2836 | if (ret > 0) { |
31d4ee6c | 2837 | IOHandlerRecord *pioh; |
cafffd40 | 2838 | |
31d4ee6c | 2839 | QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) { |
cafffd40 | 2840 | if (ioh->deleted) { |
31d4ee6c | 2841 | QLIST_REMOVE(ioh, next); |
cafffd40 | 2842 | qemu_free(ioh); |
4bed9837 JQ |
2843 | continue; |
2844 | } | |
2845 | if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { | |
2846 | ioh->fd_read(ioh->opaque); | |
2847 | } | |
2848 | if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { | |
2849 | ioh->fd_write(ioh->opaque); | |
31d4ee6c | 2850 | } |
cafffd40 | 2851 | } |
fd1dff4b | 2852 | } |
d918f23e JK |
2853 | |
2854 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); | |
b4608c04 | 2855 | |
b6964827 | 2856 | qemu_run_all_timers(); |
21d5d12b | 2857 | |
423f0742 PB |
2858 | /* Check bottom-halves last in case any of the earlier events triggered |
2859 | them. */ | |
2860 | qemu_bh_poll(); | |
3b46e624 | 2861 | |
5905b2e5 FB |
2862 | } |
2863 | ||
43b96858 | 2864 | static int qemu_cpu_exec(CPUState *env) |
5905b2e5 | 2865 | { |
43b96858 | 2866 | int ret; |
89bfc105 FB |
2867 | #ifdef CONFIG_PROFILER |
2868 | int64_t ti; | |
2869 | #endif | |
5905b2e5 | 2870 | |
89bfc105 | 2871 | #ifdef CONFIG_PROFILER |
43b96858 | 2872 | ti = profile_getclock(); |
89bfc105 | 2873 | #endif |
43b96858 AL |
2874 | if (use_icount) { |
2875 | int64_t count; | |
2876 | int decr; | |
2877 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); | |
2878 | env->icount_decr.u16.low = 0; | |
2879 | env->icount_extra = 0; | |
e2a7bb4b | 2880 | count = qemu_icount_round (qemu_next_deadline()); |
43b96858 AL |
2881 | qemu_icount += count; |
2882 | decr = (count > 0xffff) ? 0xffff : count; | |
2883 | count -= decr; | |
2884 | env->icount_decr.u16.low = decr; | |
2885 | env->icount_extra = count; | |
2886 | } | |
2887 | ret = cpu_exec(env); | |
89bfc105 | 2888 | #ifdef CONFIG_PROFILER |
43b96858 | 2889 | qemu_time += profile_getclock() - ti; |
89bfc105 | 2890 | #endif |
43b96858 AL |
2891 | if (use_icount) { |
2892 | /* Fold pending instructions back into the | |
2893 | instruction counter, and clear the interrupt flag. */ | |
2894 | qemu_icount -= (env->icount_decr.u16.low | |
2895 | + env->icount_extra); | |
2896 | env->icount_decr.u32 = 0; | |
2897 | env->icount_extra = 0; | |
2898 | } | |
2899 | return ret; | |
2900 | } | |
2901 | ||
d6f4ade2 | 2902 | static bool tcg_cpu_exec(void) |
e6e35b1e | 2903 | { |
d6dc3d42 | 2904 | int ret = 0; |
e6e35b1e AL |
2905 | |
2906 | if (next_cpu == NULL) | |
2907 | next_cpu = first_cpu; | |
2908 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { | |
2909 | CPUState *env = cur_cpu = next_cpu; | |
2910 | ||
972abbe0 PB |
2911 | qemu_clock_enable(vm_clock, |
2912 | (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); | |
2913 | ||
63255043 | 2914 | if (qemu_alarm_pending()) |
e6e35b1e | 2915 | break; |
d6dc3d42 AL |
2916 | if (cpu_can_run(env)) |
2917 | ret = qemu_cpu_exec(env); | |
c37cc7b0 MT |
2918 | else if (env->stop) |
2919 | break; | |
2920 | ||
e6e35b1e AL |
2921 | if (ret == EXCP_DEBUG) { |
2922 | gdb_set_stop_cpu(env); | |
2923 | debug_requested = 1; | |
2924 | break; | |
2925 | } | |
2926 | } | |
d6f4ade2 | 2927 | return tcg_has_work(); |
e6e35b1e AL |
2928 | } |
2929 | ||
43b96858 AL |
2930 | static int vm_can_run(void) |
2931 | { | |
2932 | if (powerdown_requested) | |
2933 | return 0; | |
2934 | if (reset_requested) | |
2935 | return 0; | |
2936 | if (shutdown_requested) | |
2937 | return 0; | |
e568902a AL |
2938 | if (debug_requested) |
2939 | return 0; | |
43b96858 AL |
2940 | return 1; |
2941 | } | |
2942 | ||
d9c32310 BS |
2943 | qemu_irq qemu_system_powerdown; |
2944 | ||
43b96858 AL |
2945 | static void main_loop(void) |
2946 | { | |
6e29f5da | 2947 | int r; |
e6e35b1e | 2948 | |
d6dc3d42 AL |
2949 | #ifdef CONFIG_IOTHREAD |
2950 | qemu_system_ready = 1; | |
2951 | qemu_cond_broadcast(&qemu_system_cond); | |
2952 | #endif | |
2953 | ||
6e29f5da | 2954 | for (;;) { |
43b96858 | 2955 | do { |
d6f4ade2 | 2956 | bool nonblocking = false; |
e6e35b1e AL |
2957 | #ifdef CONFIG_PROFILER |
2958 | int64_t ti; | |
2959 | #endif | |
d6dc3d42 | 2960 | #ifndef CONFIG_IOTHREAD |
d6f4ade2 | 2961 | nonblocking = tcg_cpu_exec(); |
d6dc3d42 | 2962 | #endif |
89bfc105 | 2963 | #ifdef CONFIG_PROFILER |
43b96858 | 2964 | ti = profile_getclock(); |
89bfc105 | 2965 | #endif |
d6f4ade2 | 2966 | main_loop_wait(nonblocking); |
89bfc105 | 2967 | #ifdef CONFIG_PROFILER |
43b96858 | 2968 | dev_time += profile_getclock() - ti; |
89bfc105 | 2969 | #endif |
e568902a | 2970 | } while (vm_can_run()); |
43b96858 | 2971 | |
b1a15e7e | 2972 | if (qemu_debug_requested()) { |
e568902a | 2973 | vm_stop(EXCP_DEBUG); |
b1a15e7e | 2974 | } |
43b96858 | 2975 | if (qemu_shutdown_requested()) { |
242cd003 | 2976 | monitor_protocol_event(QEVENT_SHUTDOWN, NULL); |
43b96858 AL |
2977 | if (no_shutdown) { |
2978 | vm_stop(0); | |
2979 | no_shutdown = 0; | |
2980 | } else | |
2981 | break; | |
2982 | } | |
d6dc3d42 AL |
2983 | if (qemu_reset_requested()) { |
2984 | pause_all_vcpus(); | |
43b96858 | 2985 | qemu_system_reset(); |
d6dc3d42 AL |
2986 | resume_all_vcpus(); |
2987 | } | |
d9c32310 | 2988 | if (qemu_powerdown_requested()) { |
242cd003 | 2989 | monitor_protocol_event(QEVENT_POWERDOWN, NULL); |
d9c32310 BS |
2990 | qemu_irq_raise(qemu_system_powerdown); |
2991 | } | |
b1a15e7e | 2992 | if ((r = qemu_vmstop_requested())) { |
6e29f5da | 2993 | vm_stop(r); |
b1a15e7e | 2994 | } |
b4608c04 | 2995 | } |
d6dc3d42 | 2996 | pause_all_vcpus(); |
b4608c04 FB |
2997 | } |
2998 | ||
9bd7e6d9 PB |
2999 | static void version(void) |
3000 | { | |
4a19f1ec | 3001 | printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
9bd7e6d9 PB |
3002 | } |
3003 | ||
15f82208 | 3004 | static void help(int exitcode) |
0824d6fc | 3005 | { |
e8105ebb | 3006 | const char *options_help = |
5824d651 BS |
3007 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
3008 | opt_help | |
3009 | #define DEFHEADING(text) stringify(text) "\n" | |
3010 | #include "qemu-options.h" | |
3011 | #undef DEF | |
3012 | #undef DEFHEADING | |
3013 | #undef GEN_DOCS | |
e8105ebb PB |
3014 | ; |
3015 | version(); | |
3016 | printf("usage: %s [options] [disk_image]\n" | |
3017 | "\n" | |
3018 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" | |
3f020d70 | 3019 | "\n" |
e8105ebb | 3020 | "%s\n" |
3f020d70 | 3021 | "During emulation, the following keys are useful:\n" |
3022 | "ctrl-alt-f toggle full screen\n" | |
3023 | "ctrl-alt-n switch to virtual console 'n'\n" | |
3024 | "ctrl-alt toggle mouse and keyboard grab\n" | |
3025 | "\n" | |
e8105ebb PB |
3026 | "When using -nographic, press 'ctrl-a h' to get some help.\n", |
3027 | "qemu", | |
3028 | options_help); | |
15f82208 | 3029 | exit(exitcode); |
0824d6fc FB |
3030 | } |
3031 | ||
cd6f1169 FB |
3032 | #define HAS_ARG 0x0001 |
3033 | ||
3034 | enum { | |
5824d651 BS |
3035 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
3036 | opt_enum, | |
3037 | #define DEFHEADING(text) | |
3038 | #include "qemu-options.h" | |
3039 | #undef DEF | |
3040 | #undef DEFHEADING | |
3041 | #undef GEN_DOCS | |
cd6f1169 FB |
3042 | }; |
3043 | ||
3044 | typedef struct QEMUOption { | |
3045 | const char *name; | |
3046 | int flags; | |
3047 | int index; | |
3048 | } QEMUOption; | |
3049 | ||
dbed7e40 | 3050 | static const QEMUOption qemu_options[] = { |
cd6f1169 | 3051 | { "h", 0, QEMU_OPTION_h }, |
5824d651 BS |
3052 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
3053 | { option, opt_arg, opt_enum }, | |
3054 | #define DEFHEADING(text) | |
3055 | #include "qemu-options.h" | |
3056 | #undef DEF | |
3057 | #undef DEFHEADING | |
3058 | #undef GEN_DOCS | |
cd6f1169 | 3059 | { NULL }, |
fc01f7e7 FB |
3060 | }; |
3061 | ||
1d14ffa9 | 3062 | #ifdef HAS_AUDIO |
6a36d84e | 3063 | struct soundhw soundhw[] = { |
b00052e4 | 3064 | #ifdef HAS_AUDIO_CHOICE |
4ce7ff6e | 3065 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
fd06c375 FB |
3066 | { |
3067 | "pcspk", | |
3068 | "PC speaker", | |
3069 | 0, | |
3070 | 1, | |
3071 | { .init_isa = pcspk_audio_init } | |
3072 | }, | |
3073 | #endif | |
4c9b53e3 | 3074 | |
3075 | #ifdef CONFIG_SB16 | |
6a36d84e FB |
3076 | { |
3077 | "sb16", | |
3078 | "Creative Sound Blaster 16", | |
3079 | 0, | |
3080 | 1, | |
3081 | { .init_isa = SB16_init } | |
3082 | }, | |
4c9b53e3 | 3083 | #endif |
6a36d84e | 3084 | |
cc53d26d | 3085 | #ifdef CONFIG_CS4231A |
3086 | { | |
3087 | "cs4231a", | |
3088 | "CS4231A", | |
3089 | 0, | |
3090 | 1, | |
3091 | { .init_isa = cs4231a_init } | |
3092 | }, | |
3093 | #endif | |
3094 | ||
1d14ffa9 | 3095 | #ifdef CONFIG_ADLIB |
6a36d84e FB |
3096 | { |
3097 | "adlib", | |
1d14ffa9 | 3098 | #ifdef HAS_YMF262 |
6a36d84e | 3099 | "Yamaha YMF262 (OPL3)", |
1d14ffa9 | 3100 | #else |
6a36d84e | 3101 | "Yamaha YM3812 (OPL2)", |
1d14ffa9 | 3102 | #endif |
6a36d84e FB |
3103 | 0, |
3104 | 1, | |
3105 | { .init_isa = Adlib_init } | |
3106 | }, | |
1d14ffa9 | 3107 | #endif |
6a36d84e | 3108 | |
1d14ffa9 | 3109 | #ifdef CONFIG_GUS |
6a36d84e FB |
3110 | { |
3111 | "gus", | |
3112 | "Gravis Ultrasound GF1", | |
3113 | 0, | |
3114 | 1, | |
3115 | { .init_isa = GUS_init } | |
3116 | }, | |
1d14ffa9 | 3117 | #endif |
6a36d84e | 3118 | |
4c9b53e3 | 3119 | #ifdef CONFIG_AC97 |
e5c9a13e AZ |
3120 | { |
3121 | "ac97", | |
3122 | "Intel 82801AA AC97 Audio", | |
3123 | 0, | |
3124 | 0, | |
3125 | { .init_pci = ac97_init } | |
3126 | }, | |
4c9b53e3 | 3127 | #endif |
e5c9a13e | 3128 | |
4c9b53e3 | 3129 | #ifdef CONFIG_ES1370 |
6a36d84e FB |
3130 | { |
3131 | "es1370", | |
3132 | "ENSONIQ AudioPCI ES1370", | |
3133 | 0, | |
3134 | 0, | |
3135 | { .init_pci = es1370_init } | |
3136 | }, | |
b00052e4 | 3137 | #endif |
6a36d84e | 3138 | |
4c9b53e3 | 3139 | #endif /* HAS_AUDIO_CHOICE */ |
3140 | ||
6a36d84e FB |
3141 | { NULL, NULL, 0, 0, { NULL } } |
3142 | }; | |
3143 | ||
3144 | static void select_soundhw (const char *optarg) | |
3145 | { | |
3146 | struct soundhw *c; | |
3147 | ||
3148 | if (*optarg == '?') { | |
3149 | show_valid_cards: | |
3150 | ||
3151 | printf ("Valid sound card names (comma separated):\n"); | |
3152 | for (c = soundhw; c->name; ++c) { | |
3153 | printf ("%-11s %s\n", c->name, c->descr); | |
3154 | } | |
3155 | printf ("\n-soundhw all will enable all of the above\n"); | |
1d14ffa9 FB |
3156 | exit (*optarg != '?'); |
3157 | } | |
3158 | else { | |
6a36d84e | 3159 | size_t l; |
1d14ffa9 FB |
3160 | const char *p; |
3161 | char *e; | |
3162 | int bad_card = 0; | |
3163 | ||
6a36d84e FB |
3164 | if (!strcmp (optarg, "all")) { |
3165 | for (c = soundhw; c->name; ++c) { | |
3166 | c->enabled = 1; | |
3167 | } | |
3168 | return; | |
3169 | } | |
1d14ffa9 | 3170 | |
6a36d84e | 3171 | p = optarg; |
1d14ffa9 FB |
3172 | while (*p) { |
3173 | e = strchr (p, ','); | |
3174 | l = !e ? strlen (p) : (size_t) (e - p); | |
6a36d84e FB |
3175 | |
3176 | for (c = soundhw; c->name; ++c) { | |
b3d6fb4a | 3177 | if (!strncmp (c->name, p, l) && !c->name[l]) { |
6a36d84e | 3178 | c->enabled = 1; |
1d14ffa9 FB |
3179 | break; |
3180 | } | |
3181 | } | |
6a36d84e FB |
3182 | |
3183 | if (!c->name) { | |
1d14ffa9 FB |
3184 | if (l > 80) { |
3185 | fprintf (stderr, | |
3186 | "Unknown sound card name (too big to show)\n"); | |
3187 | } | |
3188 | else { | |
3189 | fprintf (stderr, "Unknown sound card name `%.*s'\n", | |
3190 | (int) l, p); | |
3191 | } | |
3192 | bad_card = 1; | |
3193 | } | |
3194 | p += l + (e != NULL); | |
3195 | } | |
3196 | ||
3197 | if (bad_card) | |
3198 | goto show_valid_cards; | |
3199 | } | |
3200 | } | |
3201 | #endif | |
3202 | ||
3893c124 | 3203 | static void select_vgahw (const char *p) |
3204 | { | |
3205 | const char *opts; | |
3206 | ||
64465297 | 3207 | default_vga = 0; |
86176759 | 3208 | vga_interface_type = VGA_NONE; |
3893c124 | 3209 | if (strstart(p, "std", &opts)) { |
86176759 | 3210 | vga_interface_type = VGA_STD; |
3893c124 | 3211 | } else if (strstart(p, "cirrus", &opts)) { |
86176759 | 3212 | vga_interface_type = VGA_CIRRUS; |
3893c124 | 3213 | } else if (strstart(p, "vmware", &opts)) { |
86176759 | 3214 | vga_interface_type = VGA_VMWARE; |
94909d9f | 3215 | } else if (strstart(p, "xenfb", &opts)) { |
86176759 | 3216 | vga_interface_type = VGA_XENFB; |
28b85ed8 | 3217 | } else if (!strstart(p, "none", &opts)) { |
3893c124 | 3218 | invalid_vga: |
3219 | fprintf(stderr, "Unknown vga type: %s\n", p); | |
3220 | exit(1); | |
3221 | } | |
cb5a7aa8 | 3222 | while (*opts) { |
3223 | const char *nextopt; | |
3224 | ||
3225 | if (strstart(opts, ",retrace=", &nextopt)) { | |
3226 | opts = nextopt; | |
3227 | if (strstart(opts, "dumb", &nextopt)) | |
3228 | vga_retrace_method = VGA_RETRACE_DUMB; | |
3229 | else if (strstart(opts, "precise", &nextopt)) | |
3230 | vga_retrace_method = VGA_RETRACE_PRECISE; | |
3231 | else goto invalid_vga; | |
3232 | } else goto invalid_vga; | |
3233 | opts = nextopt; | |
3234 | } | |
3893c124 | 3235 | } |
3236 | ||
7d4c3d53 MA |
3237 | #ifdef TARGET_I386 |
3238 | static int balloon_parse(const char *arg) | |
3239 | { | |
382f0743 | 3240 | QemuOpts *opts; |
7d4c3d53 | 3241 | |
382f0743 GH |
3242 | if (strcmp(arg, "none") == 0) { |
3243 | return 0; | |
3244 | } | |
3245 | ||
3246 | if (!strncmp(arg, "virtio", 6)) { | |
3247 | if (arg[6] == ',') { | |
3248 | /* have params -> parse them */ | |
8212c64f | 3249 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0); |
382f0743 GH |
3250 | if (!opts) |
3251 | return -1; | |
3252 | } else { | |
3253 | /* create empty opts */ | |
3254 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); | |
7d4c3d53 | 3255 | } |
382f0743 GH |
3256 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
3257 | return 0; | |
7d4c3d53 | 3258 | } |
382f0743 GH |
3259 | |
3260 | return -1; | |
7d4c3d53 MA |
3261 | } |
3262 | #endif | |
3263 | ||
3587d7e6 FB |
3264 | #ifdef _WIN32 |
3265 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) | |
3266 | { | |
3267 | exit(STATUS_CONTROL_C_EXIT); | |
3268 | return TRUE; | |
3269 | } | |
3270 | #endif | |
3271 | ||
c4be29ff | 3272 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
8fcb1b90 BS |
3273 | { |
3274 | int ret; | |
3275 | ||
3276 | if(strlen(str) != 36) | |
3277 | return -1; | |
3278 | ||
3279 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
3280 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
3281 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); | |
3282 | ||
3283 | if(ret != 16) | |
3284 | return -1; | |
3285 | ||
b6f6e3d3 AL |
3286 | #ifdef TARGET_I386 |
3287 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); | |
3288 | #endif | |
3289 | ||
8fcb1b90 BS |
3290 | return 0; |
3291 | } | |
3292 | ||
5b08fc10 AL |
3293 | #ifndef _WIN32 |
3294 | ||
3295 | static void termsig_handler(int signal) | |
3296 | { | |
3297 | qemu_system_shutdown_request(); | |
3298 | } | |
3299 | ||
7c3370d4 JK |
3300 | static void sigchld_handler(int signal) |
3301 | { | |
3302 | waitpid(-1, NULL, WNOHANG); | |
3303 | } | |
3304 | ||
3305 | static void sighandler_setup(void) | |
5b08fc10 AL |
3306 | { |
3307 | struct sigaction act; | |
3308 | ||
3309 | memset(&act, 0, sizeof(act)); | |
3310 | act.sa_handler = termsig_handler; | |
3311 | sigaction(SIGINT, &act, NULL); | |
3312 | sigaction(SIGHUP, &act, NULL); | |
3313 | sigaction(SIGTERM, &act, NULL); | |
7c3370d4 JK |
3314 | |
3315 | act.sa_handler = sigchld_handler; | |
3316 | act.sa_flags = SA_NOCLDSTOP; | |
3317 | sigaction(SIGCHLD, &act, NULL); | |
5b08fc10 AL |
3318 | } |
3319 | ||
3320 | #endif | |
3321 | ||
5cea8590 PB |
3322 | #ifdef _WIN32 |
3323 | /* Look for support files in the same directory as the executable. */ | |
3324 | static char *find_datadir(const char *argv0) | |
3325 | { | |
3326 | char *p; | |
3327 | char buf[MAX_PATH]; | |
3328 | DWORD len; | |
3329 | ||
3330 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); | |
3331 | if (len == 0) { | |
c5947808 | 3332 | return NULL; |
5cea8590 PB |
3333 | } |
3334 | ||
3335 | buf[len] = 0; | |
3336 | p = buf + len - 1; | |
3337 | while (p != buf && *p != '\\') | |
3338 | p--; | |
3339 | *p = 0; | |
3340 | if (access(buf, R_OK) == 0) { | |
3341 | return qemu_strdup(buf); | |
3342 | } | |
3343 | return NULL; | |
3344 | } | |
3345 | #else /* !_WIN32 */ | |
3346 | ||
3347 | /* Find a likely location for support files using the location of the binary. | |
3348 | For installed binaries this will be "$bindir/../share/qemu". When | |
3349 | running from the build tree this will be "$bindir/../pc-bios". */ | |
3350 | #define SHARE_SUFFIX "/share/qemu" | |
3351 | #define BUILD_SUFFIX "/pc-bios" | |
3352 | static char *find_datadir(const char *argv0) | |
3353 | { | |
3354 | char *dir; | |
3355 | char *p = NULL; | |
3356 | char *res; | |
5cea8590 | 3357 | char buf[PATH_MAX]; |
3a41759d | 3358 | size_t max_len; |
5cea8590 PB |
3359 | |
3360 | #if defined(__linux__) | |
3361 | { | |
3362 | int len; | |
3363 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); | |
3364 | if (len > 0) { | |
3365 | buf[len] = 0; | |
3366 | p = buf; | |
3367 | } | |
3368 | } | |
3369 | #elif defined(__FreeBSD__) | |
3370 | { | |
3371 | int len; | |
3372 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); | |
3373 | if (len > 0) { | |
3374 | buf[len] = 0; | |
3375 | p = buf; | |
3376 | } | |
3377 | } | |
3378 | #endif | |
3379 | /* If we don't have any way of figuring out the actual executable | |
3380 | location then try argv[0]. */ | |
3381 | if (!p) { | |
4d224196 | 3382 | p = realpath(argv0, buf); |
5cea8590 PB |
3383 | if (!p) { |
3384 | return NULL; | |
3385 | } | |
3386 | } | |
3387 | dir = dirname(p); | |
3388 | dir = dirname(dir); | |
3389 | ||
3a41759d BS |
3390 | max_len = strlen(dir) + |
3391 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; | |
3392 | res = qemu_mallocz(max_len); | |
3393 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); | |
5cea8590 | 3394 | if (access(res, R_OK)) { |
3a41759d | 3395 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
5cea8590 PB |
3396 | if (access(res, R_OK)) { |
3397 | qemu_free(res); | |
3398 | res = NULL; | |
3399 | } | |
3400 | } | |
4d224196 | 3401 | |
5cea8590 PB |
3402 | return res; |
3403 | } | |
3404 | #undef SHARE_SUFFIX | |
3405 | #undef BUILD_SUFFIX | |
3406 | #endif | |
3407 | ||
3408 | char *qemu_find_file(int type, const char *name) | |
3409 | { | |
3410 | int len; | |
3411 | const char *subdir; | |
3412 | char *buf; | |
3413 | ||
3414 | /* If name contains path separators then try it as a straight path. */ | |
3415 | if ((strchr(name, '/') || strchr(name, '\\')) | |
3416 | && access(name, R_OK) == 0) { | |
73ffc805 | 3417 | return qemu_strdup(name); |
5cea8590 PB |
3418 | } |
3419 | switch (type) { | |
3420 | case QEMU_FILE_TYPE_BIOS: | |
3421 | subdir = ""; | |
3422 | break; | |
3423 | case QEMU_FILE_TYPE_KEYMAP: | |
3424 | subdir = "keymaps/"; | |
3425 | break; | |
3426 | default: | |
3427 | abort(); | |
3428 | } | |
3429 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; | |
3430 | buf = qemu_mallocz(len); | |
3a41759d | 3431 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
5cea8590 PB |
3432 | if (access(buf, R_OK)) { |
3433 | qemu_free(buf); | |
3434 | return NULL; | |
3435 | } | |
3436 | return buf; | |
3437 | } | |
3438 | ||
ff952ba2 MA |
3439 | static int device_help_func(QemuOpts *opts, void *opaque) |
3440 | { | |
3441 | return qdev_device_help(opts); | |
3442 | } | |
3443 | ||
f31d07d1 GH |
3444 | static int device_init_func(QemuOpts *opts, void *opaque) |
3445 | { | |
3446 | DeviceState *dev; | |
3447 | ||
3448 | dev = qdev_device_add(opts); | |
3449 | if (!dev) | |
3450 | return -1; | |
3451 | return 0; | |
3452 | } | |
3453 | ||
1a688d3b GH |
3454 | static int chardev_init_func(QemuOpts *opts, void *opaque) |
3455 | { | |
3456 | CharDriverState *chr; | |
3457 | ||
3458 | chr = qemu_chr_open_opts(opts, NULL); | |
3459 | if (!chr) | |
3460 | return -1; | |
3461 | return 0; | |
3462 | } | |
3463 | ||
88589343 GH |
3464 | static int mon_init_func(QemuOpts *opts, void *opaque) |
3465 | { | |
3466 | CharDriverState *chr; | |
3467 | const char *chardev; | |
3468 | const char *mode; | |
3469 | int flags; | |
3470 | ||
3471 | mode = qemu_opt_get(opts, "mode"); | |
3472 | if (mode == NULL) { | |
3473 | mode = "readline"; | |
3474 | } | |
3475 | if (strcmp(mode, "readline") == 0) { | |
3476 | flags = MONITOR_USE_READLINE; | |
3477 | } else if (strcmp(mode, "control") == 0) { | |
3478 | flags = MONITOR_USE_CONTROL; | |
3479 | } else { | |
3480 | fprintf(stderr, "unknown monitor mode \"%s\"\n", mode); | |
3481 | exit(1); | |
3482 | } | |
3483 | ||
3484 | if (qemu_opt_get_bool(opts, "default", 0)) | |
3485 | flags |= MONITOR_IS_DEFAULT; | |
3486 | ||
3487 | chardev = qemu_opt_get(opts, "chardev"); | |
3488 | chr = qemu_chr_find(chardev); | |
3489 | if (chr == NULL) { | |
3490 | fprintf(stderr, "chardev \"%s\" not found\n", chardev); | |
3491 | exit(1); | |
3492 | } | |
3493 | ||
3494 | monitor_init(chr, flags); | |
3495 | return 0; | |
3496 | } | |
3497 | ||
6ca5582d | 3498 | static void monitor_parse(const char *optarg, const char *mode) |
88589343 GH |
3499 | { |
3500 | static int monitor_device_index = 0; | |
3501 | QemuOpts *opts; | |
3502 | const char *p; | |
3503 | char label[32]; | |
3504 | int def = 0; | |
3505 | ||
3506 | if (strstart(optarg, "chardev:", &p)) { | |
3507 | snprintf(label, sizeof(label), "%s", p); | |
3508 | } else { | |
3509 | if (monitor_device_index) { | |
3510 | snprintf(label, sizeof(label), "monitor%d", | |
3511 | monitor_device_index); | |
3512 | } else { | |
3513 | snprintf(label, sizeof(label), "monitor"); | |
3514 | def = 1; | |
3515 | } | |
3516 | opts = qemu_chr_parse_compat(label, optarg); | |
3517 | if (!opts) { | |
3518 | fprintf(stderr, "parse error: %s\n", optarg); | |
3519 | exit(1); | |
3520 | } | |
3521 | } | |
3522 | ||
3523 | opts = qemu_opts_create(&qemu_mon_opts, label, 1); | |
3524 | if (!opts) { | |
3525 | fprintf(stderr, "duplicate chardev: %s\n", label); | |
3526 | exit(1); | |
3527 | } | |
6ca5582d | 3528 | qemu_opt_set(opts, "mode", mode); |
88589343 GH |
3529 | qemu_opt_set(opts, "chardev", label); |
3530 | if (def) | |
3531 | qemu_opt_set(opts, "default", "on"); | |
3532 | monitor_device_index++; | |
3533 | } | |
3534 | ||
bd3c948d GH |
3535 | struct device_config { |
3536 | enum { | |
aee1b935 GH |
3537 | DEV_USB, /* -usbdevice */ |
3538 | DEV_BT, /* -bt */ | |
3539 | DEV_SERIAL, /* -serial */ | |
3540 | DEV_PARALLEL, /* -parallel */ | |
3541 | DEV_VIRTCON, /* -virtioconsole */ | |
c9f398e5 | 3542 | DEV_DEBUGCON, /* -debugcon */ |
bd3c948d GH |
3543 | } type; |
3544 | const char *cmdline; | |
72cf2d4f | 3545 | QTAILQ_ENTRY(device_config) next; |
bd3c948d | 3546 | }; |
72cf2d4f | 3547 | QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs); |
bd3c948d GH |
3548 | |
3549 | static void add_device_config(int type, const char *cmdline) | |
3550 | { | |
3551 | struct device_config *conf; | |
3552 | ||
3553 | conf = qemu_mallocz(sizeof(*conf)); | |
3554 | conf->type = type; | |
3555 | conf->cmdline = cmdline; | |
72cf2d4f | 3556 | QTAILQ_INSERT_TAIL(&device_configs, conf, next); |
bd3c948d GH |
3557 | } |
3558 | ||
3559 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) | |
3560 | { | |
3561 | struct device_config *conf; | |
3562 | int rc; | |
3563 | ||
72cf2d4f | 3564 | QTAILQ_FOREACH(conf, &device_configs, next) { |
bd3c948d GH |
3565 | if (conf->type != type) |
3566 | continue; | |
3567 | rc = func(conf->cmdline); | |
3568 | if (0 != rc) | |
3569 | return rc; | |
3570 | } | |
3571 | return 0; | |
3572 | } | |
3573 | ||
998bbd74 GH |
3574 | static int serial_parse(const char *devname) |
3575 | { | |
3576 | static int index = 0; | |
3577 | char label[32]; | |
3578 | ||
3579 | if (strcmp(devname, "none") == 0) | |
3580 | return 0; | |
3581 | if (index == MAX_SERIAL_PORTS) { | |
3582 | fprintf(stderr, "qemu: too many serial ports\n"); | |
3583 | exit(1); | |
3584 | } | |
3585 | snprintf(label, sizeof(label), "serial%d", index); | |
3586 | serial_hds[index] = qemu_chr_open(label, devname, NULL); | |
3587 | if (!serial_hds[index]) { | |
3588 | fprintf(stderr, "qemu: could not open serial device '%s': %s\n", | |
3589 | devname, strerror(errno)); | |
3590 | return -1; | |
3591 | } | |
3592 | index++; | |
3593 | return 0; | |
3594 | } | |
3595 | ||
6a5e8b0e GH |
3596 | static int parallel_parse(const char *devname) |
3597 | { | |
3598 | static int index = 0; | |
3599 | char label[32]; | |
3600 | ||
3601 | if (strcmp(devname, "none") == 0) | |
3602 | return 0; | |
3603 | if (index == MAX_PARALLEL_PORTS) { | |
3604 | fprintf(stderr, "qemu: too many parallel ports\n"); | |
3605 | exit(1); | |
3606 | } | |
3607 | snprintf(label, sizeof(label), "parallel%d", index); | |
3608 | parallel_hds[index] = qemu_chr_open(label, devname, NULL); | |
3609 | if (!parallel_hds[index]) { | |
3610 | fprintf(stderr, "qemu: could not open parallel device '%s': %s\n", | |
3611 | devname, strerror(errno)); | |
3612 | return -1; | |
3613 | } | |
3614 | index++; | |
3615 | return 0; | |
3616 | } | |
3617 | ||
aee1b935 GH |
3618 | static int virtcon_parse(const char *devname) |
3619 | { | |
3620 | static int index = 0; | |
3621 | char label[32]; | |
392ecf54 | 3622 | QemuOpts *bus_opts, *dev_opts; |
aee1b935 GH |
3623 | |
3624 | if (strcmp(devname, "none") == 0) | |
3625 | return 0; | |
3626 | if (index == MAX_VIRTIO_CONSOLES) { | |
3627 | fprintf(stderr, "qemu: too many virtio consoles\n"); | |
3628 | exit(1); | |
3629 | } | |
392ecf54 AS |
3630 | |
3631 | bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0); | |
3632 | qemu_opt_set(bus_opts, "driver", "virtio-serial"); | |
3633 | ||
3634 | dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0); | |
3635 | qemu_opt_set(dev_opts, "driver", "virtconsole"); | |
3636 | ||
aee1b935 GH |
3637 | snprintf(label, sizeof(label), "virtcon%d", index); |
3638 | virtcon_hds[index] = qemu_chr_open(label, devname, NULL); | |
3639 | if (!virtcon_hds[index]) { | |
3640 | fprintf(stderr, "qemu: could not open virtio console '%s': %s\n", | |
3641 | devname, strerror(errno)); | |
3642 | return -1; | |
3643 | } | |
392ecf54 AS |
3644 | qemu_opt_set(dev_opts, "chardev", label); |
3645 | ||
aee1b935 GH |
3646 | index++; |
3647 | return 0; | |
3648 | } | |
3649 | ||
c9f398e5 PA |
3650 | static int debugcon_parse(const char *devname) |
3651 | { | |
3652 | QemuOpts *opts; | |
3653 | ||
3654 | if (!qemu_chr_open("debugcon", devname, NULL)) { | |
3655 | exit(1); | |
3656 | } | |
3657 | opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1); | |
3658 | if (!opts) { | |
3659 | fprintf(stderr, "qemu: already have a debugcon device\n"); | |
3660 | exit(1); | |
3661 | } | |
3662 | qemu_opt_set(opts, "driver", "isa-debugcon"); | |
3663 | qemu_opt_set(opts, "chardev", "debugcon"); | |
3664 | return 0; | |
3665 | } | |
3666 | ||
6530a97b AL |
3667 | static const QEMUOption *lookup_opt(int argc, char **argv, |
3668 | const char **poptarg, int *poptind) | |
3669 | { | |
3670 | const QEMUOption *popt; | |
3671 | int optind = *poptind; | |
3672 | char *r = argv[optind]; | |
3673 | const char *optarg; | |
3674 | ||
0f0bc3f1 | 3675 | loc_set_cmdline(argv, optind, 1); |
6530a97b AL |
3676 | optind++; |
3677 | /* Treat --foo the same as -foo. */ | |
3678 | if (r[1] == '-') | |
3679 | r++; | |
3680 | popt = qemu_options; | |
3681 | for(;;) { | |
3682 | if (!popt->name) { | |
0f0bc3f1 | 3683 | error_report("invalid option"); |
6530a97b AL |
3684 | exit(1); |
3685 | } | |
3686 | if (!strcmp(popt->name, r + 1)) | |
3687 | break; | |
3688 | popt++; | |
3689 | } | |
3690 | if (popt->flags & HAS_ARG) { | |
3691 | if (optind >= argc) { | |
0f0bc3f1 | 3692 | error_report("requires an argument"); |
6530a97b AL |
3693 | exit(1); |
3694 | } | |
3695 | optarg = argv[optind++]; | |
0f0bc3f1 | 3696 | loc_set_cmdline(argv, optind - 2, 2); |
6530a97b AL |
3697 | } else { |
3698 | optarg = NULL; | |
3699 | } | |
3700 | ||
3701 | *poptarg = optarg; | |
3702 | *poptind = optind; | |
3703 | ||
3704 | return popt; | |
3705 | } | |
3706 | ||
902b3d5c | 3707 | int main(int argc, char **argv, char **envp) |
0824d6fc | 3708 | { |
59030a8c | 3709 | const char *gdbstub_dev = NULL; |
28c5af54 | 3710 | uint32_t boot_devices_bitmap = 0; |
e4bcb14c | 3711 | int i; |
28c5af54 | 3712 | int snapshot, linux_boot, net_boot; |
4e3de9e9 | 3713 | const char *icount_option = NULL; |
7f7f9873 | 3714 | const char *initrd_filename; |
a20dd508 | 3715 | const char *kernel_filename, *kernel_cmdline; |
195325a4 | 3716 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
3023f332 | 3717 | DisplayState *ds; |
7d957bd8 | 3718 | DisplayChangeListener *dcl; |
46d4767d | 3719 | int cyls, heads, secs, translation; |
f31d07d1 | 3720 | QemuOpts *hda_opts = NULL, *opts; |
cd6f1169 | 3721 | int optind; |
6530a97b | 3722 | const char *optarg; |
d63d307f | 3723 | const char *loadvm = NULL; |
cc1daa40 | 3724 | QEMUMachine *machine; |
94fc95cd | 3725 | const char *cpu_model; |
b9e82a59 | 3726 | #ifndef _WIN32 |
71e3ceb8 | 3727 | int fds[2]; |
b9e82a59 | 3728 | #endif |
26a5f13b | 3729 | int tb_size; |
93815bc2 | 3730 | const char *pid_file = NULL; |
5bb7910a | 3731 | const char *incoming = NULL; |
b9e82a59 | 3732 | #ifndef _WIN32 |
54042bcf AL |
3733 | int fd = 0; |
3734 | struct passwd *pwd = NULL; | |
0858532e AL |
3735 | const char *chroot_dir = NULL; |
3736 | const char *run_as = NULL; | |
b9e82a59 | 3737 | #endif |
268a362c | 3738 | CPUState *env; |
993fbfdb | 3739 | int show_vnc_port = 0; |
292444cb | 3740 | int defconfig = 1; |
0bd48850 | 3741 | |
65abca0a MA |
3742 | error_set_progname(argv[0]); |
3743 | ||
6875204c JK |
3744 | init_clocks(); |
3745 | ||
902b3d5c | 3746 | qemu_cache_utils_init(envp); |
3747 | ||
72cf2d4f | 3748 | QLIST_INIT (&vm_change_state_head); |
be995c27 FB |
3749 | #ifndef _WIN32 |
3750 | { | |
3751 | struct sigaction act; | |
3752 | sigfillset(&act.sa_mask); | |
3753 | act.sa_flags = 0; | |
3754 | act.sa_handler = SIG_IGN; | |
3755 | sigaction(SIGPIPE, &act, NULL); | |
3756 | } | |
3587d7e6 FB |
3757 | #else |
3758 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); | |
a8e5ac33 FB |
3759 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
3760 | QEMU to run on a single CPU */ | |
3761 | { | |
3762 | HANDLE h; | |
3763 | DWORD mask, smask; | |
3764 | int i; | |
3765 | h = GetCurrentProcess(); | |
3766 | if (GetProcessAffinityMask(h, &mask, &smask)) { | |
3767 | for(i = 0; i < 32; i++) { | |
3768 | if (mask & (1 << i)) | |
3769 | break; | |
3770 | } | |
3771 | if (i != 32) { | |
3772 | mask = 1 << i; | |
3773 | SetProcessAffinityMask(h, mask); | |
3774 | } | |
3775 | } | |
3776 | } | |
67b915a5 | 3777 | #endif |
be995c27 | 3778 | |
f80f9ec9 | 3779 | module_call_init(MODULE_INIT_MACHINE); |
0c257437 | 3780 | machine = find_default_machine(); |
94fc95cd | 3781 | cpu_model = NULL; |
fc01f7e7 | 3782 | initrd_filename = NULL; |
4fc5d071 | 3783 | ram_size = 0; |
33e3963e | 3784 | snapshot = 0; |
a20dd508 FB |
3785 | kernel_filename = NULL; |
3786 | kernel_cmdline = ""; | |
c4b1fcc0 | 3787 | cyls = heads = secs = 0; |
46d4767d | 3788 | translation = BIOS_ATA_TRANSLATION_AUTO; |
c4b1fcc0 | 3789 | |
268a362c AL |
3790 | for (i = 0; i < MAX_NODES; i++) { |
3791 | node_mem[i] = 0; | |
3792 | node_cpumask[i] = 0; | |
3793 | } | |
3794 | ||
268a362c | 3795 | nb_numa_nodes = 0; |
7c9d8e07 | 3796 | nb_nics = 0; |
3b46e624 | 3797 | |
26a5f13b | 3798 | tb_size = 0; |
41bd639b BS |
3799 | autostart= 1; |
3800 | ||
292444cb AL |
3801 | /* first pass of option parsing */ |
3802 | optind = 1; | |
3803 | while (optind < argc) { | |
3804 | if (argv[optind][0] != '-') { | |
3805 | /* disk image */ | |
28e68d68 | 3806 | optind++; |
292444cb AL |
3807 | continue; |
3808 | } else { | |
3809 | const QEMUOption *popt; | |
3810 | ||
3811 | popt = lookup_opt(argc, argv, &optarg, &optind); | |
3812 | switch (popt->index) { | |
3813 | case QEMU_OPTION_nodefconfig: | |
3814 | defconfig=0; | |
3815 | break; | |
3816 | } | |
3817 | } | |
3818 | } | |
3819 | ||
3820 | if (defconfig) { | |
cf5a65aa | 3821 | const char *fname; |
292444cb | 3822 | FILE *fp; |
cf5a65aa MA |
3823 | |
3824 | fname = CONFIG_QEMU_CONFDIR "/qemu.conf"; | |
3825 | fp = fopen(fname, "r"); | |
292444cb | 3826 | if (fp) { |
cf5a65aa | 3827 | if (qemu_config_parse(fp, fname) != 0) { |
292444cb AL |
3828 | exit(1); |
3829 | } | |
3830 | fclose(fp); | |
3831 | } | |
3832 | ||
cf5a65aa MA |
3833 | fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf"; |
3834 | fp = fopen(fname, "r"); | |
292444cb | 3835 | if (fp) { |
cf5a65aa | 3836 | if (qemu_config_parse(fp, fname) != 0) { |
292444cb AL |
3837 | exit(1); |
3838 | } | |
3839 | fclose(fp); | |
3840 | } | |
3841 | } | |
b5ec5ce0 | 3842 | #if defined(cpudef_setup) |
3843 | cpudef_setup(); /* parse cpu definitions in target config file */ | |
3844 | #endif | |
292444cb AL |
3845 | |
3846 | /* second pass of option parsing */ | |
cd6f1169 | 3847 | optind = 1; |
0824d6fc | 3848 | for(;;) { |
cd6f1169 | 3849 | if (optind >= argc) |
0824d6fc | 3850 | break; |
6530a97b | 3851 | if (argv[optind][0] != '-') { |
9dfd7c7a | 3852 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
cd6f1169 FB |
3853 | } else { |
3854 | const QEMUOption *popt; | |
3855 | ||
6530a97b | 3856 | popt = lookup_opt(argc, argv, &optarg, &optind); |
cd6f1169 | 3857 | switch(popt->index) { |
cc1daa40 FB |
3858 | case QEMU_OPTION_M: |
3859 | machine = find_machine(optarg); | |
3860 | if (!machine) { | |
3861 | QEMUMachine *m; | |
3862 | printf("Supported machines are:\n"); | |
3863 | for(m = first_machine; m != NULL; m = m->next) { | |
3f6599e6 MM |
3864 | if (m->alias) |
3865 | printf("%-10s %s (alias of %s)\n", | |
3866 | m->alias, m->desc, m->name); | |
cc1daa40 | 3867 | printf("%-10s %s%s\n", |
5fafdf24 | 3868 | m->name, m->desc, |
0c257437 | 3869 | m->is_default ? " (default)" : ""); |
cc1daa40 | 3870 | } |
15f82208 | 3871 | exit(*optarg != '?'); |
cc1daa40 FB |
3872 | } |
3873 | break; | |
94fc95cd JM |
3874 | case QEMU_OPTION_cpu: |
3875 | /* hw initialization will check this */ | |
15f82208 | 3876 | if (*optarg == '?') { |
c732abe2 | 3877 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
b5ec5ce0 | 3878 | #if defined(cpu_list_id) |
3879 | cpu_list_id(stdout, &fprintf, optarg); | |
3880 | #elif defined(cpu_list) | |
3881 | cpu_list(stdout, &fprintf); /* deprecated */ | |
94fc95cd | 3882 | #endif |
15f82208 | 3883 | exit(0); |
94fc95cd JM |
3884 | } else { |
3885 | cpu_model = optarg; | |
3886 | } | |
3887 | break; | |
cd6f1169 | 3888 | case QEMU_OPTION_initrd: |
fc01f7e7 FB |
3889 | initrd_filename = optarg; |
3890 | break; | |
cd6f1169 | 3891 | case QEMU_OPTION_hda: |
e4bcb14c | 3892 | if (cyls == 0) |
9dfd7c7a | 3893 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
e4bcb14c | 3894 | else |
9dfd7c7a | 3895 | hda_opts = drive_add(optarg, HD_ALIAS |
e4bcb14c | 3896 | ",cyls=%d,heads=%d,secs=%d%s", |
609497ab | 3897 | 0, cyls, heads, secs, |
e4bcb14c TS |
3898 | translation == BIOS_ATA_TRANSLATION_LBA ? |
3899 | ",trans=lba" : | |
3900 | translation == BIOS_ATA_TRANSLATION_NONE ? | |
3901 | ",trans=none" : ""); | |
3902 | break; | |
cd6f1169 | 3903 | case QEMU_OPTION_hdb: |
cc1daa40 FB |
3904 | case QEMU_OPTION_hdc: |
3905 | case QEMU_OPTION_hdd: | |
609497ab | 3906 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
fc01f7e7 | 3907 | break; |
e4bcb14c | 3908 | case QEMU_OPTION_drive: |
609497ab | 3909 | drive_add(NULL, "%s", optarg); |
e4bcb14c | 3910 | break; |
d058fe03 GH |
3911 | case QEMU_OPTION_set: |
3912 | if (qemu_set_option(optarg) != 0) | |
3913 | exit(1); | |
3914 | break; | |
d0fef6fb GH |
3915 | case QEMU_OPTION_global: |
3916 | if (qemu_global_option(optarg) != 0) | |
3917 | exit(1); | |
3918 | break; | |
3e3d5815 | 3919 | case QEMU_OPTION_mtdblock: |
609497ab | 3920 | drive_add(optarg, MTD_ALIAS); |
3e3d5815 | 3921 | break; |
a1bb27b1 | 3922 | case QEMU_OPTION_sd: |
609497ab | 3923 | drive_add(optarg, SD_ALIAS); |
a1bb27b1 | 3924 | break; |
86f55663 | 3925 | case QEMU_OPTION_pflash: |
609497ab | 3926 | drive_add(optarg, PFLASH_ALIAS); |
86f55663 | 3927 | break; |
cd6f1169 | 3928 | case QEMU_OPTION_snapshot: |
33e3963e FB |
3929 | snapshot = 1; |
3930 | break; | |
cd6f1169 | 3931 | case QEMU_OPTION_hdachs: |
330d0414 | 3932 | { |
330d0414 FB |
3933 | const char *p; |
3934 | p = optarg; | |
3935 | cyls = strtol(p, (char **)&p, 0); | |
46d4767d FB |
3936 | if (cyls < 1 || cyls > 16383) |
3937 | goto chs_fail; | |
330d0414 FB |
3938 | if (*p != ',') |
3939 | goto chs_fail; | |
3940 | p++; | |
3941 | heads = strtol(p, (char **)&p, 0); | |
46d4767d FB |
3942 | if (heads < 1 || heads > 16) |
3943 | goto chs_fail; | |
330d0414 FB |
3944 | if (*p != ',') |
3945 | goto chs_fail; | |
3946 | p++; | |
3947 | secs = strtol(p, (char **)&p, 0); | |
46d4767d FB |
3948 | if (secs < 1 || secs > 63) |
3949 | goto chs_fail; | |
3950 | if (*p == ',') { | |
3951 | p++; | |
3952 | if (!strcmp(p, "none")) | |
3953 | translation = BIOS_ATA_TRANSLATION_NONE; | |
3954 | else if (!strcmp(p, "lba")) | |
3955 | translation = BIOS_ATA_TRANSLATION_LBA; | |
3956 | else if (!strcmp(p, "auto")) | |
3957 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
3958 | else | |
3959 | goto chs_fail; | |
3960 | } else if (*p != '\0') { | |
c4b1fcc0 | 3961 | chs_fail: |
46d4767d FB |
3962 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
3963 | exit(1); | |
c4b1fcc0 | 3964 | } |
9dfd7c7a GH |
3965 | if (hda_opts != NULL) { |
3966 | char num[16]; | |
3967 | snprintf(num, sizeof(num), "%d", cyls); | |
3968 | qemu_opt_set(hda_opts, "cyls", num); | |
3969 | snprintf(num, sizeof(num), "%d", heads); | |
3970 | qemu_opt_set(hda_opts, "heads", num); | |
3971 | snprintf(num, sizeof(num), "%d", secs); | |
3972 | qemu_opt_set(hda_opts, "secs", num); | |
3973 | if (translation == BIOS_ATA_TRANSLATION_LBA) | |
3974 | qemu_opt_set(hda_opts, "trans", "lba"); | |
3975 | if (translation == BIOS_ATA_TRANSLATION_NONE) | |
3976 | qemu_opt_set(hda_opts, "trans", "none"); | |
3977 | } | |
330d0414 FB |
3978 | } |
3979 | break; | |
268a362c AL |
3980 | case QEMU_OPTION_numa: |
3981 | if (nb_numa_nodes >= MAX_NODES) { | |
3982 | fprintf(stderr, "qemu: too many NUMA nodes\n"); | |
3983 | exit(1); | |
3984 | } | |
3985 | numa_add(optarg); | |
3986 | break; | |
cd6f1169 | 3987 | case QEMU_OPTION_nographic: |
993fbfdb | 3988 | display_type = DT_NOGRAPHIC; |
a20dd508 | 3989 | break; |
4d3b6f6e AZ |
3990 | #ifdef CONFIG_CURSES |
3991 | case QEMU_OPTION_curses: | |
993fbfdb | 3992 | display_type = DT_CURSES; |
4d3b6f6e AZ |
3993 | break; |
3994 | #endif | |
a171fe39 AZ |
3995 | case QEMU_OPTION_portrait: |
3996 | graphic_rotate = 1; | |
3997 | break; | |
cd6f1169 | 3998 | case QEMU_OPTION_kernel: |
a20dd508 FB |
3999 | kernel_filename = optarg; |
4000 | break; | |
cd6f1169 | 4001 | case QEMU_OPTION_append: |
a20dd508 | 4002 | kernel_cmdline = optarg; |
313aa567 | 4003 | break; |
cd6f1169 | 4004 | case QEMU_OPTION_cdrom: |
609497ab | 4005 | drive_add(optarg, CDROM_ALIAS); |
36b486bb | 4006 | break; |
cd6f1169 | 4007 | case QEMU_OPTION_boot: |
28c5af54 | 4008 | { |
ef3adf68 | 4009 | static const char * const params[] = { |
95387491 | 4010 | "order", "once", "menu", NULL |
ef3adf68 JK |
4011 | }; |
4012 | char buf[sizeof(boot_devices)]; | |
e0f084bf | 4013 | char *standard_boot_devices; |
ef3adf68 JK |
4014 | int legacy = 0; |
4015 | ||
4016 | if (!strchr(optarg, '=')) { | |
4017 | legacy = 1; | |
4018 | pstrcpy(buf, sizeof(buf), optarg); | |
4019 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { | |
4020 | fprintf(stderr, | |
4021 | "qemu: unknown boot parameter '%s' in '%s'\n", | |
4022 | buf, optarg); | |
4023 | exit(1); | |
4024 | } | |
4025 | ||
4026 | if (legacy || | |
4027 | get_param_value(buf, sizeof(buf), "order", optarg)) { | |
4028 | boot_devices_bitmap = parse_bootdevices(buf); | |
4029 | pstrcpy(boot_devices, sizeof(boot_devices), buf); | |
28c5af54 | 4030 | } |
e0f084bf JK |
4031 | if (!legacy) { |
4032 | if (get_param_value(buf, sizeof(buf), | |
4033 | "once", optarg)) { | |
4034 | boot_devices_bitmap |= parse_bootdevices(buf); | |
4035 | standard_boot_devices = qemu_strdup(boot_devices); | |
4036 | pstrcpy(boot_devices, sizeof(boot_devices), buf); | |
4037 | qemu_register_reset(restore_boot_devices, | |
4038 | standard_boot_devices); | |
4039 | } | |
95387491 JK |
4040 | if (get_param_value(buf, sizeof(buf), |
4041 | "menu", optarg)) { | |
4042 | if (!strcmp(buf, "on")) { | |
4043 | boot_menu = 1; | |
4044 | } else if (!strcmp(buf, "off")) { | |
4045 | boot_menu = 0; | |
4046 | } else { | |
4047 | fprintf(stderr, | |
4048 | "qemu: invalid option value '%s'\n", | |
4049 | buf); | |
4050 | exit(1); | |
4051 | } | |
4052 | } | |
e0f084bf | 4053 | } |
36b486bb FB |
4054 | } |
4055 | break; | |
cd6f1169 | 4056 | case QEMU_OPTION_fda: |
cd6f1169 | 4057 | case QEMU_OPTION_fdb: |
609497ab | 4058 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
c45886db | 4059 | break; |
52ca8d6a FB |
4060 | #ifdef TARGET_I386 |
4061 | case QEMU_OPTION_no_fd_bootchk: | |
4062 | fd_bootchk = 0; | |
4063 | break; | |
4064 | #endif | |
a1ea458f MM |
4065 | case QEMU_OPTION_netdev: |
4066 | if (net_client_parse(&qemu_netdev_opts, optarg) == -1) { | |
4067 | exit(1); | |
4068 | } | |
4069 | break; | |
7c9d8e07 | 4070 | case QEMU_OPTION_net: |
7f161aae | 4071 | if (net_client_parse(&qemu_net_opts, optarg) == -1) { |
c4b1fcc0 FB |
4072 | exit(1); |
4073 | } | |
702c651c | 4074 | break; |
c7f74643 FB |
4075 | #ifdef CONFIG_SLIRP |
4076 | case QEMU_OPTION_tftp: | |
ad196a9d | 4077 | legacy_tftp_prefix = optarg; |
9bf05444 | 4078 | break; |
47d5d01a | 4079 | case QEMU_OPTION_bootp: |
ad196a9d | 4080 | legacy_bootp_filename = optarg; |
47d5d01a | 4081 | break; |
c94c8d64 | 4082 | #ifndef _WIN32 |
9d728e8c | 4083 | case QEMU_OPTION_smb: |
0752706d MA |
4084 | if (net_slirp_smb(optarg) < 0) |
4085 | exit(1); | |
9d728e8c | 4086 | break; |
c94c8d64 | 4087 | #endif |
9bf05444 | 4088 | case QEMU_OPTION_redir: |
0752706d MA |
4089 | if (net_slirp_redir(optarg) < 0) |
4090 | exit(1); | |
9bf05444 | 4091 | break; |
c7f74643 | 4092 | #endif |
dc72ac14 | 4093 | case QEMU_OPTION_bt: |
bd3c948d | 4094 | add_device_config(DEV_BT, optarg); |
dc72ac14 | 4095 | break; |
1d14ffa9 | 4096 | #ifdef HAS_AUDIO |
1d14ffa9 FB |
4097 | case QEMU_OPTION_audio_help: |
4098 | AUD_help (); | |
4099 | exit (0); | |
4100 | break; | |
4101 | case QEMU_OPTION_soundhw: | |
4102 | select_soundhw (optarg); | |
4103 | break; | |
4104 | #endif | |
cd6f1169 | 4105 | case QEMU_OPTION_h: |
15f82208 | 4106 | help(0); |
cd6f1169 | 4107 | break; |
9bd7e6d9 PB |
4108 | case QEMU_OPTION_version: |
4109 | version(); | |
4110 | exit(0); | |
4111 | break; | |
00f82b8a AJ |
4112 | case QEMU_OPTION_m: { |
4113 | uint64_t value; | |
4114 | char *ptr; | |
4115 | ||
4116 | value = strtoul(optarg, &ptr, 10); | |
4117 | switch (*ptr) { | |
4118 | case 0: case 'M': case 'm': | |
4119 | value <<= 20; | |
4120 | break; | |
4121 | case 'G': case 'g': | |
4122 | value <<= 30; | |
4123 | break; | |
4124 | default: | |
4125 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); | |
cd6f1169 FB |
4126 | exit(1); |
4127 | } | |
00f82b8a AJ |
4128 | |
4129 | /* On 32-bit hosts, QEMU is limited by virtual address space */ | |
4a1418e0 | 4130 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
00f82b8a AJ |
4131 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
4132 | exit(1); | |
4133 | } | |
c227f099 | 4134 | if (value != (uint64_t)(ram_addr_t)value) { |
00f82b8a AJ |
4135 | fprintf(stderr, "qemu: ram size too large\n"); |
4136 | exit(1); | |
4137 | } | |
4138 | ram_size = value; | |
cd6f1169 | 4139 | break; |
00f82b8a | 4140 | } |
c902760f MT |
4141 | case QEMU_OPTION_mempath: |
4142 | mem_path = optarg; | |
4143 | break; | |
4144 | #ifdef MAP_POPULATE | |
4145 | case QEMU_OPTION_mem_prealloc: | |
4146 | mem_prealloc = 1; | |
4147 | break; | |
4148 | #endif | |
cd6f1169 FB |
4149 | case QEMU_OPTION_d: |
4150 | { | |
4151 | int mask; | |
c7cd6a37 | 4152 | const CPULogItem *item; |
3b46e624 | 4153 | |
cd6f1169 FB |
4154 | mask = cpu_str_to_log_mask(optarg); |
4155 | if (!mask) { | |
4156 | printf("Log items (comma separated):\n"); | |
f193c797 FB |
4157 | for(item = cpu_log_items; item->mask != 0; item++) { |
4158 | printf("%-10s %s\n", item->name, item->help); | |
4159 | } | |
4160 | exit(1); | |
cd6f1169 FB |
4161 | } |
4162 | cpu_set_log(mask); | |
f193c797 | 4163 | } |
cd6f1169 | 4164 | break; |
cd6f1169 | 4165 | case QEMU_OPTION_s: |
59030a8c | 4166 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
cd6f1169 | 4167 | break; |
59030a8c AL |
4168 | case QEMU_OPTION_gdb: |
4169 | gdbstub_dev = optarg; | |
cd6f1169 | 4170 | break; |
cd6f1169 | 4171 | case QEMU_OPTION_L: |
5cea8590 | 4172 | data_dir = optarg; |
cd6f1169 | 4173 | break; |
1192dad8 JM |
4174 | case QEMU_OPTION_bios: |
4175 | bios_name = optarg; | |
4176 | break; | |
1b530a6d AJ |
4177 | case QEMU_OPTION_singlestep: |
4178 | singlestep = 1; | |
4179 | break; | |
cd6f1169 | 4180 | case QEMU_OPTION_S: |
3c07f8e8 | 4181 | autostart = 0; |
cd6f1169 | 4182 | break; |
3d11d0eb FB |
4183 | case QEMU_OPTION_k: |
4184 | keyboard_layout = optarg; | |
4185 | break; | |
ee22c2f7 FB |
4186 | case QEMU_OPTION_localtime: |
4187 | rtc_utc = 0; | |
4188 | break; | |
3893c124 | 4189 | case QEMU_OPTION_vga: |
4190 | select_vgahw (optarg); | |
1bfe856e | 4191 | break; |
5824d651 | 4192 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
e9b137c2 FB |
4193 | case QEMU_OPTION_g: |
4194 | { | |
4195 | const char *p; | |
4196 | int w, h, depth; | |
4197 | p = optarg; | |
4198 | w = strtol(p, (char **)&p, 10); | |
4199 | if (w <= 0) { | |
4200 | graphic_error: | |
4201 | fprintf(stderr, "qemu: invalid resolution or depth\n"); | |
4202 | exit(1); | |
4203 | } | |
4204 | if (*p != 'x') | |
4205 | goto graphic_error; | |
4206 | p++; | |
4207 | h = strtol(p, (char **)&p, 10); | |
4208 | if (h <= 0) | |
4209 | goto graphic_error; | |
4210 | if (*p == 'x') { | |
4211 | p++; | |
4212 | depth = strtol(p, (char **)&p, 10); | |
5fafdf24 | 4213 | if (depth != 8 && depth != 15 && depth != 16 && |
e9b137c2 FB |
4214 | depth != 24 && depth != 32) |
4215 | goto graphic_error; | |
4216 | } else if (*p == '\0') { | |
4217 | depth = graphic_depth; | |
4218 | } else { | |
4219 | goto graphic_error; | |
4220 | } | |
3b46e624 | 4221 | |
e9b137c2 FB |
4222 | graphic_width = w; |
4223 | graphic_height = h; | |
4224 | graphic_depth = depth; | |
4225 | } | |
4226 | break; | |
5824d651 | 4227 | #endif |
20d8a3ed TS |
4228 | case QEMU_OPTION_echr: |
4229 | { | |
4230 | char *r; | |
4231 | term_escape_char = strtol(optarg, &r, 0); | |
4232 | if (r == optarg) | |
4233 | printf("Bad argument to echr\n"); | |
4234 | break; | |
4235 | } | |
82c643ff | 4236 | case QEMU_OPTION_monitor: |
6ca5582d GH |
4237 | monitor_parse(optarg, "readline"); |
4238 | default_monitor = 0; | |
4239 | break; | |
4240 | case QEMU_OPTION_qmp: | |
4241 | monitor_parse(optarg, "control"); | |
2d114dc1 | 4242 | default_monitor = 0; |
82c643ff | 4243 | break; |
22a0e04b | 4244 | case QEMU_OPTION_mon: |
8212c64f | 4245 | opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1); |
22a0e04b GH |
4246 | if (!opts) { |
4247 | fprintf(stderr, "parse error: %s\n", optarg); | |
4248 | exit(1); | |
4249 | } | |
2d114dc1 | 4250 | default_monitor = 0; |
22a0e04b | 4251 | break; |
191bc01b | 4252 | case QEMU_OPTION_chardev: |
8212c64f | 4253 | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1); |
191bc01b GH |
4254 | if (!opts) { |
4255 | fprintf(stderr, "parse error: %s\n", optarg); | |
4256 | exit(1); | |
4257 | } | |
191bc01b | 4258 | break; |
82c643ff | 4259 | case QEMU_OPTION_serial: |
998bbd74 GH |
4260 | add_device_config(DEV_SERIAL, optarg); |
4261 | default_serial = 0; | |
18141ed6 JK |
4262 | if (strncmp(optarg, "mon:", 4) == 0) { |
4263 | default_monitor = 0; | |
4264 | } | |
82c643ff | 4265 | break; |
9dd986cc | 4266 | case QEMU_OPTION_watchdog: |
09aaa160 MA |
4267 | if (watchdog) { |
4268 | fprintf(stderr, | |
4269 | "qemu: only one watchdog option may be given\n"); | |
4270 | return 1; | |
4271 | } | |
4272 | watchdog = optarg; | |
9dd986cc RJ |
4273 | break; |
4274 | case QEMU_OPTION_watchdog_action: | |
4275 | if (select_watchdog_action(optarg) == -1) { | |
4276 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); | |
4277 | exit(1); | |
4278 | } | |
4279 | break; | |
51ecf136 | 4280 | case QEMU_OPTION_virtiocon: |
aee1b935 GH |
4281 | add_device_config(DEV_VIRTCON, optarg); |
4282 | default_virtcon = 0; | |
18141ed6 JK |
4283 | if (strncmp(optarg, "mon:", 4) == 0) { |
4284 | default_monitor = 0; | |
4285 | } | |
51ecf136 | 4286 | break; |
6508fe59 | 4287 | case QEMU_OPTION_parallel: |
6a5e8b0e GH |
4288 | add_device_config(DEV_PARALLEL, optarg); |
4289 | default_parallel = 0; | |
18141ed6 JK |
4290 | if (strncmp(optarg, "mon:", 4) == 0) { |
4291 | default_monitor = 0; | |
4292 | } | |
6508fe59 | 4293 | break; |
c9f398e5 PA |
4294 | case QEMU_OPTION_debugcon: |
4295 | add_device_config(DEV_DEBUGCON, optarg); | |
4296 | break; | |
d63d307f FB |
4297 | case QEMU_OPTION_loadvm: |
4298 | loadvm = optarg; | |
4299 | break; | |
4300 | case QEMU_OPTION_full_screen: | |
4301 | full_screen = 1; | |
4302 | break; | |
667accab | 4303 | #ifdef CONFIG_SDL |
43523e93 TS |
4304 | case QEMU_OPTION_no_frame: |
4305 | no_frame = 1; | |
4306 | break; | |
3780e197 TS |
4307 | case QEMU_OPTION_alt_grab: |
4308 | alt_grab = 1; | |
4309 | break; | |
0ca9f8a4 DK |
4310 | case QEMU_OPTION_ctrl_grab: |
4311 | ctrl_grab = 1; | |
4312 | break; | |
667accab TS |
4313 | case QEMU_OPTION_no_quit: |
4314 | no_quit = 1; | |
4315 | break; | |
7d957bd8 | 4316 | case QEMU_OPTION_sdl: |
993fbfdb | 4317 | display_type = DT_SDL; |
7d957bd8 | 4318 | break; |
667accab | 4319 | #endif |
f7cce898 | 4320 | case QEMU_OPTION_pidfile: |
93815bc2 | 4321 | pid_file = optarg; |
f7cce898 | 4322 | break; |
a09db21f FB |
4323 | #ifdef TARGET_I386 |
4324 | case QEMU_OPTION_win2k_hack: | |
4325 | win2k_install_hack = 1; | |
4326 | break; | |
73822ec8 AL |
4327 | case QEMU_OPTION_rtc_td_hack: |
4328 | rtc_td_hack = 1; | |
4329 | break; | |
8a92ea2f AL |
4330 | case QEMU_OPTION_acpitable: |
4331 | if(acpi_table_add(optarg) < 0) { | |
4332 | fprintf(stderr, "Wrong acpi table provided\n"); | |
4333 | exit(1); | |
4334 | } | |
4335 | break; | |
b6f6e3d3 AL |
4336 | case QEMU_OPTION_smbios: |
4337 | if(smbios_entry_add(optarg) < 0) { | |
4338 | fprintf(stderr, "Wrong smbios provided\n"); | |
4339 | exit(1); | |
4340 | } | |
4341 | break; | |
a09db21f | 4342 | #endif |
7ba1e619 AL |
4343 | #ifdef CONFIG_KVM |
4344 | case QEMU_OPTION_enable_kvm: | |
4345 | kvm_allowed = 1; | |
7ba1e619 | 4346 | break; |
d993e026 | 4347 | #endif |
bb36d470 FB |
4348 | case QEMU_OPTION_usb: |
4349 | usb_enabled = 1; | |
4350 | break; | |
a594cfbf FB |
4351 | case QEMU_OPTION_usbdevice: |
4352 | usb_enabled = 1; | |
bd3c948d GH |
4353 | add_device_config(DEV_USB, optarg); |
4354 | break; | |
4355 | case QEMU_OPTION_device: | |
8212c64f | 4356 | if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) { |
f31d07d1 GH |
4357 | exit(1); |
4358 | } | |
a594cfbf | 4359 | break; |
6a00d601 | 4360 | case QEMU_OPTION_smp: |
dc6b1c09 | 4361 | smp_parse(optarg); |
b2097003 | 4362 | if (smp_cpus < 1) { |
6a00d601 FB |
4363 | fprintf(stderr, "Invalid number of CPUs\n"); |
4364 | exit(1); | |
4365 | } | |
6be68d7e JS |
4366 | if (max_cpus < smp_cpus) { |
4367 | fprintf(stderr, "maxcpus must be equal to or greater than " | |
4368 | "smp\n"); | |
4369 | exit(1); | |
4370 | } | |
4371 | if (max_cpus > 255) { | |
4372 | fprintf(stderr, "Unsupported number of maxcpus\n"); | |
4373 | exit(1); | |
4374 | } | |
6a00d601 | 4375 | break; |
24236869 | 4376 | case QEMU_OPTION_vnc: |
993fbfdb | 4377 | display_type = DT_VNC; |
73fc9742 | 4378 | vnc_display = optarg; |
24236869 | 4379 | break; |
5824d651 | 4380 | #ifdef TARGET_I386 |
6515b203 FB |
4381 | case QEMU_OPTION_no_acpi: |
4382 | acpi_enabled = 0; | |
4383 | break; | |
16b29ae1 AL |
4384 | case QEMU_OPTION_no_hpet: |
4385 | no_hpet = 1; | |
4386 | break; | |
7d4c3d53 MA |
4387 | case QEMU_OPTION_balloon: |
4388 | if (balloon_parse(optarg) < 0) { | |
4389 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); | |
4390 | exit(1); | |
4391 | } | |
df97b920 | 4392 | break; |
5824d651 | 4393 | #endif |
d1beab82 FB |
4394 | case QEMU_OPTION_no_reboot: |
4395 | no_reboot = 1; | |
4396 | break; | |
b2f76161 AJ |
4397 | case QEMU_OPTION_no_shutdown: |
4398 | no_shutdown = 1; | |
4399 | break; | |
9467cd46 AZ |
4400 | case QEMU_OPTION_show_cursor: |
4401 | cursor_hide = 0; | |
4402 | break; | |
8fcb1b90 BS |
4403 | case QEMU_OPTION_uuid: |
4404 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { | |
4405 | fprintf(stderr, "Fail to parse UUID string." | |
4406 | " Wrong format.\n"); | |
4407 | exit(1); | |
4408 | } | |
4409 | break; | |
5824d651 | 4410 | #ifndef _WIN32 |
71e3ceb8 TS |
4411 | case QEMU_OPTION_daemonize: |
4412 | daemonize = 1; | |
4413 | break; | |
5824d651 | 4414 | #endif |
9ae02555 TS |
4415 | case QEMU_OPTION_option_rom: |
4416 | if (nb_option_roms >= MAX_OPTION_ROMS) { | |
4417 | fprintf(stderr, "Too many option ROMs\n"); | |
4418 | exit(1); | |
4419 | } | |
4420 | option_rom[nb_option_roms] = optarg; | |
4421 | nb_option_roms++; | |
4422 | break; | |
5824d651 | 4423 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
8e71621f PB |
4424 | case QEMU_OPTION_semihosting: |
4425 | semihosting_enabled = 1; | |
4426 | break; | |
5824d651 | 4427 | #endif |
c35734b2 | 4428 | case QEMU_OPTION_name: |
1889465a AK |
4429 | qemu_name = qemu_strdup(optarg); |
4430 | { | |
4431 | char *p = strchr(qemu_name, ','); | |
4432 | if (p != NULL) { | |
4433 | *p++ = 0; | |
4434 | if (strncmp(p, "process=", 8)) { | |
4435 | fprintf(stderr, "Unknown subargument %s to -name", p); | |
4436 | exit(1); | |
4437 | } | |
4438 | p += 8; | |
4439 | set_proc_name(p); | |
4440 | } | |
4441 | } | |
c35734b2 | 4442 | break; |
95efd11c | 4443 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
66508601 BS |
4444 | case QEMU_OPTION_prom_env: |
4445 | if (nb_prom_envs >= MAX_PROM_ENVS) { | |
4446 | fprintf(stderr, "Too many prom variables\n"); | |
4447 | exit(1); | |
4448 | } | |
4449 | prom_envs[nb_prom_envs] = optarg; | |
4450 | nb_prom_envs++; | |
4451 | break; | |
2b8f2d41 AZ |
4452 | #endif |
4453 | #ifdef TARGET_ARM | |
4454 | case QEMU_OPTION_old_param: | |
4455 | old_param = 1; | |
05ebd537 | 4456 | break; |
66508601 | 4457 | #endif |
f3dcfada TS |
4458 | case QEMU_OPTION_clock: |
4459 | configure_alarms(optarg); | |
4460 | break; | |
7e0af5d0 | 4461 | case QEMU_OPTION_startdate: |
1ed2fc1f JK |
4462 | configure_rtc_date_offset(optarg, 1); |
4463 | break; | |
4464 | case QEMU_OPTION_rtc: | |
8212c64f | 4465 | opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0); |
1ed2fc1f JK |
4466 | if (!opts) { |
4467 | fprintf(stderr, "parse error: %s\n", optarg); | |
4468 | exit(1); | |
7e0af5d0 | 4469 | } |
1ed2fc1f | 4470 | configure_rtc(opts); |
7e0af5d0 | 4471 | break; |
26a5f13b FB |
4472 | case QEMU_OPTION_tb_size: |
4473 | tb_size = strtol(optarg, NULL, 0); | |
4474 | if (tb_size < 0) | |
4475 | tb_size = 0; | |
4476 | break; | |
2e70f6ef | 4477 | case QEMU_OPTION_icount: |
4e3de9e9 | 4478 | icount_option = optarg; |
2e70f6ef | 4479 | break; |
5bb7910a AL |
4480 | case QEMU_OPTION_incoming: |
4481 | incoming = optarg; | |
4482 | break; | |
d8c208dd GH |
4483 | case QEMU_OPTION_nodefaults: |
4484 | default_serial = 0; | |
4485 | default_parallel = 0; | |
aee1b935 | 4486 | default_virtcon = 0; |
d8c208dd GH |
4487 | default_monitor = 0; |
4488 | default_vga = 0; | |
cb4522cc | 4489 | default_net = 0; |
ac33f8fa GH |
4490 | default_floppy = 0; |
4491 | default_cdrom = 0; | |
4492 | default_sdcard = 0; | |
d8c208dd | 4493 | break; |
5824d651 | 4494 | #ifndef _WIN32 |
0858532e AL |
4495 | case QEMU_OPTION_chroot: |
4496 | chroot_dir = optarg; | |
4497 | break; | |
4498 | case QEMU_OPTION_runas: | |
4499 | run_as = optarg; | |
4500 | break; | |
e37630ca AL |
4501 | #endif |
4502 | #ifdef CONFIG_XEN | |
4503 | case QEMU_OPTION_xen_domid: | |
4504 | xen_domid = atoi(optarg); | |
4505 | break; | |
4506 | case QEMU_OPTION_xen_create: | |
4507 | xen_mode = XEN_CREATE; | |
4508 | break; | |
4509 | case QEMU_OPTION_xen_attach: | |
4510 | xen_mode = XEN_ATTACH; | |
4511 | break; | |
5824d651 | 4512 | #endif |
715a664a GH |
4513 | case QEMU_OPTION_readconfig: |
4514 | { | |
4515 | FILE *fp; | |
4516 | fp = fopen(optarg, "r"); | |
4517 | if (fp == NULL) { | |
4518 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); | |
4519 | exit(1); | |
4520 | } | |
cf5a65aa | 4521 | if (qemu_config_parse(fp, optarg) != 0) { |
715a664a GH |
4522 | exit(1); |
4523 | } | |
4524 | fclose(fp); | |
4525 | break; | |
4526 | } | |
4527 | case QEMU_OPTION_writeconfig: | |
4528 | { | |
4529 | FILE *fp; | |
4530 | if (strcmp(optarg, "-") == 0) { | |
4531 | fp = stdout; | |
4532 | } else { | |
4533 | fp = fopen(optarg, "w"); | |
4534 | if (fp == NULL) { | |
4535 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); | |
4536 | exit(1); | |
4537 | } | |
4538 | } | |
4539 | qemu_config_write(fp); | |
4540 | fclose(fp); | |
4541 | break; | |
4542 | } | |
cd6f1169 | 4543 | } |
0824d6fc FB |
4544 | } |
4545 | } | |
0f0bc3f1 | 4546 | loc_set_none(); |
330d0414 | 4547 | |
5cea8590 PB |
4548 | /* If no data_dir is specified then try to find it relative to the |
4549 | executable path. */ | |
4550 | if (!data_dir) { | |
4551 | data_dir = find_datadir(argv[0]); | |
4552 | } | |
4553 | /* If all else fails use the install patch specified when building. */ | |
4554 | if (!data_dir) { | |
4555 | data_dir = CONFIG_QEMU_SHAREDIR; | |
4556 | } | |
4557 | ||
6be68d7e JS |
4558 | /* |
4559 | * Default to max_cpus = smp_cpus, in case the user doesn't | |
4560 | * specify a max_cpus value. | |
4561 | */ | |
4562 | if (!max_cpus) | |
4563 | max_cpus = smp_cpus; | |
4564 | ||
3d878caa | 4565 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
b2097003 AL |
4566 | if (smp_cpus > machine->max_cpus) { |
4567 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " | |
4568 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, | |
4569 | machine->max_cpus); | |
4570 | exit(1); | |
4571 | } | |
4572 | ||
998bbd74 | 4573 | qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0); |
d8bcbabf | 4574 | qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0); |
998bbd74 | 4575 | |
986c5f78 GH |
4576 | if (machine->no_serial) { |
4577 | default_serial = 0; | |
4578 | } | |
4579 | if (machine->no_parallel) { | |
4580 | default_parallel = 0; | |
4581 | } | |
4582 | if (!machine->use_virtcon) { | |
4583 | default_virtcon = 0; | |
4584 | } | |
4585 | if (machine->no_vga) { | |
4586 | default_vga = 0; | |
4587 | } | |
ac33f8fa GH |
4588 | if (machine->no_floppy) { |
4589 | default_floppy = 0; | |
4590 | } | |
4591 | if (machine->no_cdrom) { | |
4592 | default_cdrom = 0; | |
4593 | } | |
4594 | if (machine->no_sdcard) { | |
4595 | default_sdcard = 0; | |
4596 | } | |
986c5f78 | 4597 | |
993fbfdb | 4598 | if (display_type == DT_NOGRAPHIC) { |
6a5e8b0e GH |
4599 | if (default_parallel) |
4600 | add_device_config(DEV_PARALLEL, "null"); | |
e1c09175 GH |
4601 | if (default_serial && default_monitor) { |
4602 | add_device_config(DEV_SERIAL, "mon:stdio"); | |
986c5f78 GH |
4603 | } else if (default_virtcon && default_monitor) { |
4604 | add_device_config(DEV_VIRTCON, "mon:stdio"); | |
e1c09175 GH |
4605 | } else { |
4606 | if (default_serial) | |
4607 | add_device_config(DEV_SERIAL, "stdio"); | |
986c5f78 GH |
4608 | if (default_virtcon) |
4609 | add_device_config(DEV_VIRTCON, "stdio"); | |
e1c09175 | 4610 | if (default_monitor) |
6ca5582d | 4611 | monitor_parse("stdio", "readline"); |
e1c09175 | 4612 | } |
998bbd74 GH |
4613 | } else { |
4614 | if (default_serial) | |
4615 | add_device_config(DEV_SERIAL, "vc:80Cx24C"); | |
6a5e8b0e GH |
4616 | if (default_parallel) |
4617 | add_device_config(DEV_PARALLEL, "vc:80Cx24C"); | |
abdeed06 | 4618 | if (default_monitor) |
6ca5582d | 4619 | monitor_parse("vc:80Cx24C", "readline"); |
38536da1 AG |
4620 | if (default_virtcon) |
4621 | add_device_config(DEV_VIRTCON, "vc:80Cx24C"); | |
bc0129d9 | 4622 | } |
64465297 GH |
4623 | if (default_vga) |
4624 | vga_interface_type = VGA_CIRRUS; | |
bc0129d9 | 4625 | |
1a688d3b GH |
4626 | if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0) |
4627 | exit(1); | |
4628 | ||
71e3ceb8 | 4629 | #ifndef _WIN32 |
71e3ceb8 TS |
4630 | if (daemonize) { |
4631 | pid_t pid; | |
4632 | ||
4633 | if (pipe(fds) == -1) | |
4634 | exit(1); | |
4635 | ||
4636 | pid = fork(); | |
4637 | if (pid > 0) { | |
4638 | uint8_t status; | |
4639 | ssize_t len; | |
4640 | ||
4641 | close(fds[1]); | |
4642 | ||
4643 | again: | |
93815bc2 TS |
4644 | len = read(fds[0], &status, 1); |
4645 | if (len == -1 && (errno == EINTR)) | |
4646 | goto again; | |
4647 | ||
4648 | if (len != 1) | |
4649 | exit(1); | |
4650 | else if (status == 1) { | |
850810d0 | 4651 | fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno)); |
93815bc2 TS |
4652 | exit(1); |
4653 | } else | |
4654 | exit(0); | |
71e3ceb8 | 4655 | } else if (pid < 0) |
93815bc2 | 4656 | exit(1); |
71e3ceb8 | 4657 | |
40ff6d7e KW |
4658 | close(fds[0]); |
4659 | qemu_set_cloexec(fds[1]); | |
4660 | ||
71e3ceb8 TS |
4661 | setsid(); |
4662 | ||
4663 | pid = fork(); | |
4664 | if (pid > 0) | |
4665 | exit(0); | |
4666 | else if (pid < 0) | |
4667 | exit(1); | |
4668 | ||
4669 | umask(027); | |
71e3ceb8 TS |
4670 | |
4671 | signal(SIGTSTP, SIG_IGN); | |
4672 | signal(SIGTTOU, SIG_IGN); | |
4673 | signal(SIGTTIN, SIG_IGN); | |
4674 | } | |
099fe236 | 4675 | #endif |
71e3ceb8 | 4676 | |
aa26bb2d | 4677 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
099fe236 | 4678 | #ifndef _WIN32 |
93815bc2 TS |
4679 | if (daemonize) { |
4680 | uint8_t status = 1; | |
dc330e28 KS |
4681 | if (write(fds[1], &status, 1) != 1) { |
4682 | perror("daemonize. Writing to pipe\n"); | |
4683 | } | |
93815bc2 | 4684 | } else |
099fe236 | 4685 | #endif |
850810d0 | 4686 | fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); |
93815bc2 TS |
4687 | exit(1); |
4688 | } | |
4689 | ||
214910a7 MT |
4690 | if (kvm_enabled()) { |
4691 | int ret; | |
4692 | ||
4693 | ret = kvm_init(smp_cpus); | |
4694 | if (ret < 0) { | |
4695 | fprintf(stderr, "failed to initialize KVM\n"); | |
4696 | exit(1); | |
4697 | } | |
4698 | } | |
4699 | ||
3fcf7b6b AL |
4700 | if (qemu_init_main_loop()) { |
4701 | fprintf(stderr, "qemu_init_main_loop failed\n"); | |
4702 | exit(1); | |
4703 | } | |
a20dd508 | 4704 | linux_boot = (kernel_filename != NULL); |
6c41b272 | 4705 | |
f8d39c01 TS |
4706 | if (!linux_boot && *kernel_cmdline != '\0') { |
4707 | fprintf(stderr, "-append only allowed with -kernel option\n"); | |
4708 | exit(1); | |
4709 | } | |
4710 | ||
4711 | if (!linux_boot && initrd_filename != NULL) { | |
4712 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); | |
4713 | exit(1); | |
4714 | } | |
4715 | ||
bf65f53f FN |
4716 | #ifndef _WIN32 |
4717 | /* Win32 doesn't support line-buffering and requires size >= 2 */ | |
b118d61e | 4718 | setvbuf(stdout, NULL, _IOLBF, 0); |
bf65f53f | 4719 | #endif |
3b46e624 | 4720 | |
7183b4b4 AL |
4721 | if (init_timer_alarm() < 0) { |
4722 | fprintf(stderr, "could not initialize alarm timer\n"); | |
4723 | exit(1); | |
4724 | } | |
4e3de9e9 | 4725 | configure_icount(icount_option); |
634fce96 | 4726 | |
fd1dff4b FB |
4727 | #ifdef _WIN32 |
4728 | socket_init(); | |
4729 | #endif | |
4730 | ||
dc1c9fe8 MM |
4731 | if (net_init_clients() < 0) { |
4732 | exit(1); | |
702c651c | 4733 | } |
f1510b2c | 4734 | |
406c8df3 GC |
4735 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
4736 | net_set_boot_mask(net_boot); | |
4737 | ||
dc72ac14 | 4738 | /* init the bluetooth world */ |
bd3c948d GH |
4739 | if (foreach_device_config(DEV_BT, bt_parse)) |
4740 | exit(1); | |
dc72ac14 | 4741 | |
0824d6fc | 4742 | /* init the memory */ |
94a6b54f PB |
4743 | if (ram_size == 0) |
4744 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; | |
9ae02555 | 4745 | |
26a5f13b FB |
4746 | /* init the dynamic translator */ |
4747 | cpu_exec_init_all(tb_size * 1024 * 1024); | |
4748 | ||
eb852011 | 4749 | bdrv_init_with_whitelist(); |
c4b1fcc0 | 4750 | |
c163b5ca LS |
4751 | blk_mig_init(); |
4752 | ||
ac33f8fa | 4753 | if (default_cdrom) { |
aa40fc9c GH |
4754 | /* we always create the cdrom drive, even if no disk is there */ |
4755 | drive_add(NULL, CDROM_ALIAS); | |
ac33f8fa | 4756 | } |
c4b1fcc0 | 4757 | |
ac33f8fa | 4758 | if (default_floppy) { |
aa40fc9c GH |
4759 | /* we always create at least one floppy */ |
4760 | drive_add(NULL, FD_ALIAS, 0); | |
ac33f8fa | 4761 | } |
86f55663 | 4762 | |
ac33f8fa | 4763 | if (default_sdcard) { |
aa40fc9c GH |
4764 | /* we always create one sd slot, even if no card is in it */ |
4765 | drive_add(NULL, SD_ALIAS); | |
4766 | } | |
9d413d1d | 4767 | |
e4bcb14c | 4768 | /* open the virtual block devices */ |
9dfd7c7a | 4769 | if (snapshot) |
7282a033 GH |
4770 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
4771 | if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0) | |
9dfd7c7a | 4772 | exit(1); |
3e3d5815 | 4773 | |
c163b5ca LS |
4774 | register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, |
4775 | ram_load, NULL); | |
8a7ddc38 | 4776 | |
268a362c AL |
4777 | if (nb_numa_nodes > 0) { |
4778 | int i; | |
4779 | ||
4780 | if (nb_numa_nodes > smp_cpus) { | |
4781 | nb_numa_nodes = smp_cpus; | |
4782 | } | |
4783 | ||
4784 | /* If no memory size if given for any node, assume the default case | |
4785 | * and distribute the available memory equally across all nodes | |
4786 | */ | |
4787 | for (i = 0; i < nb_numa_nodes; i++) { | |
4788 | if (node_mem[i] != 0) | |
4789 | break; | |
4790 | } | |
4791 | if (i == nb_numa_nodes) { | |
4792 | uint64_t usedmem = 0; | |
4793 | ||
4794 | /* On Linux, the each node's border has to be 8MB aligned, | |
4795 | * the final node gets the rest. | |
4796 | */ | |
4797 | for (i = 0; i < nb_numa_nodes - 1; i++) { | |
4798 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); | |
4799 | usedmem += node_mem[i]; | |
4800 | } | |
4801 | node_mem[i] = ram_size - usedmem; | |
4802 | } | |
4803 | ||
4804 | for (i = 0; i < nb_numa_nodes; i++) { | |
4805 | if (node_cpumask[i] != 0) | |
4806 | break; | |
4807 | } | |
4808 | /* assigning the VCPUs round-robin is easier to implement, guest OSes | |
4809 | * must cope with this anyway, because there are BIOSes out there in | |
4810 | * real machines which also use this scheme. | |
4811 | */ | |
4812 | if (i == nb_numa_nodes) { | |
4813 | for (i = 0; i < smp_cpus; i++) { | |
4814 | node_cpumask[i % nb_numa_nodes] |= 1 << i; | |
4815 | } | |
4816 | } | |
4817 | } | |
4818 | ||
998bbd74 GH |
4819 | if (foreach_device_config(DEV_SERIAL, serial_parse) < 0) |
4820 | exit(1); | |
6a5e8b0e GH |
4821 | if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0) |
4822 | exit(1); | |
aee1b935 GH |
4823 | if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0) |
4824 | exit(1); | |
c9f398e5 PA |
4825 | if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0) |
4826 | exit(1); | |
2796dae0 | 4827 | |
aae9460e PB |
4828 | module_call_init(MODULE_INIT_DEVICE); |
4829 | ||
ff952ba2 MA |
4830 | if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0) |
4831 | exit(0); | |
4832 | ||
09aaa160 MA |
4833 | if (watchdog) { |
4834 | i = select_watchdog(watchdog); | |
4835 | if (i > 0) | |
4836 | exit (i == 1 ? 1 : 0); | |
4837 | } | |
4838 | ||
b6b61144 | 4839 | if (machine->compat_props) { |
458fb679 | 4840 | qdev_prop_register_global_list(machine->compat_props); |
b6b61144 | 4841 | } |
d0fef6fb GH |
4842 | qemu_add_globals(); |
4843 | ||
fbe1b595 | 4844 | machine->init(ram_size, boot_devices, |
3023f332 AL |
4845 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
4846 | ||
ea375f9a | 4847 | cpu_synchronize_all_post_init(); |
268a362c | 4848 | |
67b3b71d JQ |
4849 | #ifndef _WIN32 |
4850 | /* must be after terminal init, SDL library changes signal handlers */ | |
4851 | sighandler_setup(); | |
4852 | #endif | |
4853 | ||
268a362c AL |
4854 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
4855 | for (i = 0; i < nb_numa_nodes; i++) { | |
4856 | if (node_cpumask[i] & (1 << env->cpu_index)) { | |
4857 | env->numa_node = i; | |
4858 | } | |
4859 | } | |
4860 | } | |
4861 | ||
6f338c34 AL |
4862 | current_machine = machine; |
4863 | ||
3023f332 AL |
4864 | /* init USB devices */ |
4865 | if (usb_enabled) { | |
0752706d MA |
4866 | if (foreach_device_config(DEV_USB, usb_parse) < 0) |
4867 | exit(1); | |
3023f332 AL |
4868 | } |
4869 | ||
bd3c948d | 4870 | /* init generic devices */ |
f31d07d1 | 4871 | if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0) |
bd3c948d GH |
4872 | exit(1); |
4873 | ||
668680f7 MA |
4874 | net_check_clients(); |
4875 | ||
3023f332 | 4876 | /* just use the first displaystate for the moment */ |
b473df6e | 4877 | ds = get_displaystate(); |
993fbfdb AL |
4878 | |
4879 | if (display_type == DT_DEFAULT) { | |
4880 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) | |
4881 | display_type = DT_SDL; | |
4882 | #else | |
4883 | display_type = DT_VNC; | |
4884 | vnc_display = "localhost:0,to=99"; | |
4885 | show_vnc_port = 1; | |
4886 | #endif | |
4887 | } | |
4888 | ||
4889 | ||
4890 | switch (display_type) { | |
4891 | case DT_NOGRAPHIC: | |
4892 | break; | |
4d3b6f6e | 4893 | #if defined(CONFIG_CURSES) |
993fbfdb AL |
4894 | case DT_CURSES: |
4895 | curses_display_init(ds, full_screen); | |
4896 | break; | |
4d3b6f6e | 4897 | #endif |
5b0753e0 | 4898 | #if defined(CONFIG_SDL) |
993fbfdb AL |
4899 | case DT_SDL: |
4900 | sdl_display_init(ds, full_screen, no_frame); | |
4901 | break; | |
5b0753e0 | 4902 | #elif defined(CONFIG_COCOA) |
993fbfdb AL |
4903 | case DT_SDL: |
4904 | cocoa_display_init(ds, full_screen); | |
4905 | break; | |
313aa567 | 4906 | #endif |
993fbfdb AL |
4907 | case DT_VNC: |
4908 | vnc_display_init(ds); | |
4909 | if (vnc_display_open(ds, vnc_display) < 0) | |
4910 | exit(1); | |
f92f8afe | 4911 | |
993fbfdb AL |
4912 | if (show_vnc_port) { |
4913 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); | |
f92f8afe | 4914 | } |
993fbfdb AL |
4915 | break; |
4916 | default: | |
4917 | break; | |
313aa567 | 4918 | } |
7d957bd8 | 4919 | dpy_resize(ds); |
5b08fc10 | 4920 | |
3023f332 AL |
4921 | dcl = ds->listeners; |
4922 | while (dcl != NULL) { | |
4923 | if (dcl->dpy_refresh != NULL) { | |
4924 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); | |
4925 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); | |
20d8a3ed | 4926 | } |
3023f332 | 4927 | dcl = dcl->next; |
20d8a3ed | 4928 | } |
3023f332 | 4929 | |
993fbfdb | 4930 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
9043b62d BS |
4931 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
4932 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); | |
4933 | } | |
4934 | ||
b473df6e | 4935 | text_consoles_set_display(ds); |
2796dae0 | 4936 | |
88589343 GH |
4937 | if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) |
4938 | exit(1); | |
82c643ff | 4939 | |
59030a8c AL |
4940 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
4941 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", | |
4942 | gdbstub_dev); | |
4943 | exit(1); | |
45669e00 | 4944 | } |
45669e00 | 4945 | |
3418bd25 GH |
4946 | qdev_machine_creation_done(); |
4947 | ||
15ff7705 GH |
4948 | if (rom_load_all() != 0) { |
4949 | fprintf(stderr, "rom loading failed\n"); | |
4950 | exit(1); | |
4951 | } | |
45a50b16 | 4952 | |
504c2948 | 4953 | qemu_system_reset(); |
05f2401e | 4954 | if (loadvm) { |
03cd4655 | 4955 | if (load_vmstate(loadvm) < 0) { |
05f2401e JQ |
4956 | autostart = 0; |
4957 | } | |
4958 | } | |
d63d307f | 4959 | |
2bb8c10c | 4960 | if (incoming) { |
5bb7910a | 4961 | qemu_start_incoming_migration(incoming); |
6b99dadc | 4962 | } else if (autostart) { |
c0f4ce77 | 4963 | vm_start(); |
6b99dadc | 4964 | } |
ffd843bc | 4965 | |
b9e82a59 | 4966 | #ifndef _WIN32 |
71e3ceb8 TS |
4967 | if (daemonize) { |
4968 | uint8_t status = 0; | |
4969 | ssize_t len; | |
71e3ceb8 TS |
4970 | |
4971 | again1: | |
4972 | len = write(fds[1], &status, 1); | |
4973 | if (len == -1 && (errno == EINTR)) | |
4974 | goto again1; | |
4975 | ||
4976 | if (len != 1) | |
4977 | exit(1); | |
4978 | ||
dc330e28 KS |
4979 | if (chdir("/")) { |
4980 | perror("not able to chdir to /"); | |
4981 | exit(1); | |
4982 | } | |
40ff6d7e | 4983 | TFR(fd = qemu_open("/dev/null", O_RDWR)); |
71e3ceb8 TS |
4984 | if (fd == -1) |
4985 | exit(1); | |
0858532e | 4986 | } |
71e3ceb8 | 4987 | |
0858532e AL |
4988 | if (run_as) { |
4989 | pwd = getpwnam(run_as); | |
4990 | if (!pwd) { | |
4991 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); | |
4992 | exit(1); | |
4993 | } | |
4994 | } | |
4995 | ||
4996 | if (chroot_dir) { | |
4997 | if (chroot(chroot_dir) < 0) { | |
4998 | fprintf(stderr, "chroot failed\n"); | |
4999 | exit(1); | |
5000 | } | |
dc330e28 KS |
5001 | if (chdir("/")) { |
5002 | perror("not able to chdir to /"); | |
5003 | exit(1); | |
5004 | } | |
0858532e AL |
5005 | } |
5006 | ||
5007 | if (run_as) { | |
5008 | if (setgid(pwd->pw_gid) < 0) { | |
5009 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); | |
5010 | exit(1); | |
5011 | } | |
5012 | if (setuid(pwd->pw_uid) < 0) { | |
5013 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); | |
5014 | exit(1); | |
5015 | } | |
5016 | if (setuid(0) != -1) { | |
5017 | fprintf(stderr, "Dropping privileges failed\n"); | |
5018 | exit(1); | |
5019 | } | |
5020 | } | |
0858532e AL |
5021 | |
5022 | if (daemonize) { | |
5023 | dup2(fd, 0); | |
5024 | dup2(fd, 1); | |
5025 | dup2(fd, 2); | |
71e3ceb8 | 5026 | |
0858532e | 5027 | close(fd); |
71e3ceb8 | 5028 | } |
b9e82a59 | 5029 | #endif |
71e3ceb8 | 5030 | |
8a7ddc38 | 5031 | main_loop(); |
40c3bac3 | 5032 | quit_timers(); |
63a01ef8 | 5033 | net_cleanup(); |
b46a8906 | 5034 | |
0824d6fc FB |
5035 | return 0; |
5036 | } |