]>
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 | 33 | #include "config-host.h" |
96555a96 BS |
34 | /* Needed early to override system queue definitions on BSD */ |
35 | #include "sys-queue.h" | |
d40cdb10 | 36 | |
67b915a5 | 37 | #ifndef _WIN32 |
5cea8590 | 38 | #include <libgen.h> |
0858532e | 39 | #include <pwd.h> |
67b915a5 | 40 | #include <sys/times.h> |
f1510b2c | 41 | #include <sys/wait.h> |
67b915a5 | 42 | #include <termios.h> |
67b915a5 | 43 | #include <sys/mman.h> |
f1510b2c | 44 | #include <sys/ioctl.h> |
24646c7e | 45 | #include <sys/resource.h> |
f1510b2c | 46 | #include <sys/socket.h> |
c94c8d64 | 47 | #include <netinet/in.h> |
24646c7e BS |
48 | #include <net/if.h> |
49 | #if defined(__NetBSD__) | |
50 | #include <net/if_tap.h> | |
51 | #endif | |
52 | #ifdef __linux__ | |
53 | #include <linux/if_tun.h> | |
54 | #endif | |
55 | #include <arpa/inet.h> | |
9d728e8c | 56 | #include <dirent.h> |
7c9d8e07 | 57 | #include <netdb.h> |
cb4b976b | 58 | #include <sys/select.h> |
71e72a19 | 59 | #ifdef CONFIG_BSD |
7d3505c5 | 60 | #include <sys/stat.h> |
c5e97233 | 61 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
7d3505c5 | 62 | #include <libutil.h> |
24646c7e BS |
63 | #else |
64 | #include <util.h> | |
128ab2ff | 65 | #endif |
5c40d2bd TS |
66 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
67 | #include <freebsd/stdlib.h> | |
7d3505c5 | 68 | #else |
223f0d72 | 69 | #ifdef __linux__ |
7d3505c5 FB |
70 | #include <pty.h> |
71 | #include <malloc.h> | |
fd872598 | 72 | #include <linux/rtc.h> |
1889465a | 73 | #include <sys/prctl.h> |
bd494f4c TS |
74 | |
75 | /* For the benefit of older linux systems which don't supply it, | |
76 | we use a local copy of hpet.h. */ | |
77 | /* #include <linux/hpet.h> */ | |
78 | #include "hpet.h" | |
79 | ||
e57a8c0e | 80 | #include <linux/ppdev.h> |
5867c88a | 81 | #include <linux/parport.h> |
223f0d72 BS |
82 | #endif |
83 | #ifdef __sun__ | |
d5d10bc3 TS |
84 | #include <sys/stat.h> |
85 | #include <sys/ethernet.h> | |
86 | #include <sys/sockio.h> | |
d5d10bc3 TS |
87 | #include <netinet/arp.h> |
88 | #include <netinet/in.h> | |
89 | #include <netinet/in_systm.h> | |
90 | #include <netinet/ip.h> | |
91 | #include <netinet/ip_icmp.h> // must come after ip.h | |
92 | #include <netinet/udp.h> | |
93 | #include <netinet/tcp.h> | |
94 | #include <net/if.h> | |
95 | #include <syslog.h> | |
96 | #include <stropts.h> | |
67b915a5 | 97 | #endif |
7d3505c5 | 98 | #endif |
ec530c81 | 99 | #endif |
67b915a5 | 100 | |
9892fbfb BS |
101 | #if defined(__OpenBSD__) |
102 | #include <util.h> | |
103 | #endif | |
104 | ||
8a16d273 TS |
105 | #if defined(CONFIG_VDE) |
106 | #include <libvdeplug.h> | |
107 | #endif | |
108 | ||
67b915a5 | 109 | #ifdef _WIN32 |
49dc768d | 110 | #include <windows.h> |
4fddf62a | 111 | #include <mmsystem.h> |
67b915a5 FB |
112 | #endif |
113 | ||
73332e5c | 114 | #ifdef CONFIG_SDL |
59a36a2f | 115 | #if defined(__APPLE__) || defined(main) |
6693665a | 116 | #include <SDL.h> |
880fec5d | 117 | int qemu_main(int argc, char **argv, char **envp); |
118 | int main(int argc, char **argv) | |
119 | { | |
59a36a2f | 120 | return qemu_main(argc, argv, NULL); |
880fec5d | 121 | } |
122 | #undef main | |
123 | #define main qemu_main | |
96bcd4f8 | 124 | #endif |
73332e5c | 125 | #endif /* CONFIG_SDL */ |
0824d6fc | 126 | |
5b0753e0 FB |
127 | #ifdef CONFIG_COCOA |
128 | #undef main | |
129 | #define main qemu_main | |
130 | #endif /* CONFIG_COCOA */ | |
131 | ||
511d2b14 BS |
132 | #include "hw/hw.h" |
133 | #include "hw/boards.h" | |
134 | #include "hw/usb.h" | |
135 | #include "hw/pcmcia.h" | |
136 | #include "hw/pc.h" | |
137 | #include "hw/audiodev.h" | |
138 | #include "hw/isa.h" | |
139 | #include "hw/baum.h" | |
140 | #include "hw/bt.h" | |
9dd986cc | 141 | #include "hw/watchdog.h" |
b6f6e3d3 | 142 | #include "hw/smbios.h" |
e37630ca | 143 | #include "hw/xen.h" |
bd3c948d | 144 | #include "hw/qdev.h" |
5ef4efa4 | 145 | #include "bt-host.h" |
511d2b14 BS |
146 | #include "net.h" |
147 | #include "monitor.h" | |
148 | #include "console.h" | |
149 | #include "sysemu.h" | |
150 | #include "gdbstub.h" | |
151 | #include "qemu-timer.h" | |
152 | #include "qemu-char.h" | |
153 | #include "cache-utils.h" | |
154 | #include "block.h" | |
a718acec | 155 | #include "dma.h" |
511d2b14 BS |
156 | #include "audio/audio.h" |
157 | #include "migration.h" | |
158 | #include "kvm.h" | |
159 | #include "balloon.h" | |
d3f24367 | 160 | #include "qemu-option.h" |
7282a033 | 161 | #include "qemu-config.h" |
511d2b14 | 162 | |
0824d6fc | 163 | #include "disas.h" |
fc01f7e7 | 164 | |
8a7ddc38 | 165 | #include "exec-all.h" |
0824d6fc | 166 | |
511d2b14 BS |
167 | #include "qemu_socket.h" |
168 | ||
d918f23e | 169 | #include "slirp/libslirp.h" |
511d2b14 | 170 | |
9dc63a1e BS |
171 | //#define DEBUG_NET |
172 | //#define DEBUG_SLIRP | |
330d0414 | 173 | |
1bfe856e | 174 | #define DEFAULT_RAM_SIZE 128 |
313aa567 | 175 | |
ddd9bbd9 JK |
176 | /* Maximum number of monitor devices */ |
177 | #define MAX_MONITOR_DEVICES 10 | |
178 | ||
5cea8590 | 179 | static const char *data_dir; |
1192dad8 | 180 | const char *bios_name = NULL; |
e4bcb14c | 181 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
faea38e7 | 182 | to store the VM snapshots */ |
751c6a17 | 183 | struct drivelist drives = TAILQ_HEAD_INITIALIZER(drives); |
3b0ba927 | 184 | struct driveoptlist driveopts = TAILQ_HEAD_INITIALIZER(driveopts); |
cb5a7aa8 | 185 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
3023f332 | 186 | static DisplayState *display_state; |
993fbfdb | 187 | DisplayType display_type = DT_DEFAULT; |
3d11d0eb | 188 | const char* keyboard_layout = NULL; |
6ee093c9 | 189 | static int64_t ticks_per_sec; |
00f82b8a | 190 | ram_addr_t ram_size; |
c4b1fcc0 | 191 | int nb_nics; |
7c9d8e07 | 192 | NICInfo nd_table[MAX_NICS]; |
8a7ddc38 | 193 | int vm_running; |
d399f677 | 194 | int autostart; |
f6503059 AZ |
195 | static int rtc_utc = 1; |
196 | static int rtc_date_offset = -1; /* -1 means no change */ | |
86176759 | 197 | int vga_interface_type = VGA_CIRRUS; |
d827220b FB |
198 | #ifdef TARGET_SPARC |
199 | int graphic_width = 1024; | |
200 | int graphic_height = 768; | |
eee0b836 | 201 | int graphic_depth = 8; |
d827220b | 202 | #else |
1bfe856e FB |
203 | int graphic_width = 800; |
204 | int graphic_height = 600; | |
e9b137c2 | 205 | int graphic_depth = 15; |
eee0b836 | 206 | #endif |
dbed7e40 | 207 | static int full_screen = 0; |
634a21f6 | 208 | #ifdef CONFIG_SDL |
dbed7e40 | 209 | static int no_frame = 0; |
634a21f6 | 210 | #endif |
667accab | 211 | int no_quit = 0; |
8d11df9e | 212 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
6508fe59 | 213 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
9ede2fde | 214 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
a09db21f FB |
215 | #ifdef TARGET_I386 |
216 | int win2k_install_hack = 0; | |
73822ec8 | 217 | int rtc_td_hack = 0; |
a09db21f | 218 | #endif |
bb36d470 | 219 | int usb_enabled = 0; |
1b530a6d | 220 | int singlestep = 0; |
6a00d601 | 221 | int smp_cpus = 1; |
6be68d7e | 222 | int max_cpus = 0; |
dc6b1c09 AP |
223 | int smp_cores = 1; |
224 | int smp_threads = 1; | |
73fc9742 | 225 | const char *vnc_display; |
6515b203 | 226 | int acpi_enabled = 1; |
16b29ae1 | 227 | int no_hpet = 0; |
52ca8d6a | 228 | int fd_bootchk = 1; |
d1beab82 | 229 | int no_reboot = 0; |
b2f76161 | 230 | int no_shutdown = 0; |
9467cd46 | 231 | int cursor_hide = 1; |
a171fe39 | 232 | int graphic_rotate = 0; |
6b35e7bf | 233 | uint8_t irq0override = 1; |
b9e82a59 | 234 | #ifndef _WIN32 |
71e3ceb8 | 235 | int daemonize = 0; |
b9e82a59 | 236 | #endif |
09aaa160 | 237 | const char *watchdog; |
9ae02555 TS |
238 | const char *option_rom[MAX_OPTION_ROMS]; |
239 | int nb_option_roms; | |
8e71621f | 240 | int semihosting_enabled = 0; |
2b8f2d41 AZ |
241 | #ifdef TARGET_ARM |
242 | int old_param = 0; | |
243 | #endif | |
c35734b2 | 244 | const char *qemu_name; |
3780e197 | 245 | int alt_grab = 0; |
95efd11c | 246 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
66508601 BS |
247 | unsigned int nb_prom_envs = 0; |
248 | const char *prom_envs[MAX_PROM_ENVS]; | |
249 | #endif | |
95387491 | 250 | int boot_menu; |
0824d6fc | 251 | |
268a362c AL |
252 | int nb_numa_nodes; |
253 | uint64_t node_mem[MAX_NODES]; | |
254 | uint64_t node_cpumask[MAX_NODES]; | |
255 | ||
ee5605e5 AZ |
256 | static CPUState *cur_cpu; |
257 | static CPUState *next_cpu; | |
43b96858 | 258 | static int timer_alarm_pending = 1; |
bf20dc07 | 259 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
2e70f6ef | 260 | static int icount_time_shift; |
bf20dc07 | 261 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
2e70f6ef PB |
262 | #define MAX_ICOUNT_SHIFT 10 |
263 | /* Compensate for varying guest execution speed. */ | |
264 | static int64_t qemu_icount_bias; | |
dbed7e40 BS |
265 | static QEMUTimer *icount_rt_timer; |
266 | static QEMUTimer *icount_vm_timer; | |
9043b62d | 267 | static QEMUTimer *nographic_timer; |
ee5605e5 | 268 | |
8fcb1b90 BS |
269 | uint8_t qemu_uuid[16]; |
270 | ||
76e30d0f JK |
271 | static QEMUBootSetHandler *boot_set_handler; |
272 | static void *boot_set_opaque; | |
273 | ||
0824d6fc | 274 | /***********************************************************/ |
26aa7d72 FB |
275 | /* x86 ISA bus support */ |
276 | ||
277 | target_phys_addr_t isa_mem_base = 0; | |
3de388f6 | 278 | PicState2 *isa_pic; |
0824d6fc | 279 | |
0824d6fc | 280 | /***********************************************************/ |
0824d6fc FB |
281 | void hw_error(const char *fmt, ...) |
282 | { | |
283 | va_list ap; | |
6a00d601 | 284 | CPUState *env; |
0824d6fc FB |
285 | |
286 | va_start(ap, fmt); | |
287 | fprintf(stderr, "qemu: hardware error: "); | |
288 | vfprintf(stderr, fmt, ap); | |
289 | fprintf(stderr, "\n"); | |
6a00d601 FB |
290 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
291 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); | |
0824d6fc | 292 | #ifdef TARGET_I386 |
6a00d601 | 293 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
c45886db | 294 | #else |
6a00d601 | 295 | cpu_dump_state(env, stderr, fprintf, 0); |
0824d6fc | 296 | #endif |
6a00d601 | 297 | } |
0824d6fc FB |
298 | va_end(ap); |
299 | abort(); | |
300 | } | |
1889465a AK |
301 | |
302 | static void set_proc_name(const char *s) | |
303 | { | |
6ca8d0fd | 304 | #if defined(__linux__) && defined(PR_SET_NAME) |
1889465a AK |
305 | char name[16]; |
306 | if (!s) | |
307 | return; | |
308 | name[sizeof(name) - 1] = 0; | |
309 | strncpy(name, s, sizeof(name)); | |
310 | /* Could rewrite argv[0] too, but that's a bit more complicated. | |
311 | This simple way is enough for `top'. */ | |
312 | prctl(PR_SET_NAME, name); | |
313 | #endif | |
314 | } | |
df751fa8 AL |
315 | |
316 | /***************/ | |
317 | /* ballooning */ | |
318 | ||
319 | static QEMUBalloonEvent *qemu_balloon_event; | |
320 | void *qemu_balloon_event_opaque; | |
321 | ||
322 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) | |
323 | { | |
324 | qemu_balloon_event = func; | |
325 | qemu_balloon_event_opaque = opaque; | |
326 | } | |
327 | ||
328 | void qemu_balloon(ram_addr_t target) | |
329 | { | |
330 | if (qemu_balloon_event) | |
331 | qemu_balloon_event(qemu_balloon_event_opaque, target); | |
332 | } | |
333 | ||
334 | ram_addr_t qemu_balloon_status(void) | |
335 | { | |
336 | if (qemu_balloon_event) | |
337 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); | |
338 | return 0; | |
339 | } | |
0824d6fc | 340 | |
63066f4f FB |
341 | /***********************************************************/ |
342 | /* keyboard/mouse */ | |
343 | ||
344 | static QEMUPutKBDEvent *qemu_put_kbd_event; | |
345 | static void *qemu_put_kbd_event_opaque; | |
455204eb TS |
346 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
347 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; | |
63066f4f FB |
348 | |
349 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) | |
350 | { | |
351 | qemu_put_kbd_event_opaque = opaque; | |
352 | qemu_put_kbd_event = func; | |
353 | } | |
354 | ||
455204eb TS |
355 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
356 | void *opaque, int absolute, | |
357 | const char *name) | |
63066f4f | 358 | { |
455204eb TS |
359 | QEMUPutMouseEntry *s, *cursor; |
360 | ||
361 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); | |
455204eb TS |
362 | |
363 | s->qemu_put_mouse_event = func; | |
364 | s->qemu_put_mouse_event_opaque = opaque; | |
365 | s->qemu_put_mouse_event_absolute = absolute; | |
366 | s->qemu_put_mouse_event_name = qemu_strdup(name); | |
367 | s->next = NULL; | |
368 | ||
369 | if (!qemu_put_mouse_event_head) { | |
370 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; | |
371 | return s; | |
372 | } | |
373 | ||
374 | cursor = qemu_put_mouse_event_head; | |
375 | while (cursor->next != NULL) | |
376 | cursor = cursor->next; | |
377 | ||
378 | cursor->next = s; | |
379 | qemu_put_mouse_event_current = s; | |
380 | ||
381 | return s; | |
382 | } | |
383 | ||
384 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) | |
385 | { | |
386 | QEMUPutMouseEntry *prev = NULL, *cursor; | |
387 | ||
388 | if (!qemu_put_mouse_event_head || entry == NULL) | |
389 | return; | |
390 | ||
391 | cursor = qemu_put_mouse_event_head; | |
392 | while (cursor != NULL && cursor != entry) { | |
393 | prev = cursor; | |
394 | cursor = cursor->next; | |
395 | } | |
396 | ||
397 | if (cursor == NULL) // does not exist or list empty | |
398 | return; | |
399 | else if (prev == NULL) { // entry is head | |
400 | qemu_put_mouse_event_head = cursor->next; | |
401 | if (qemu_put_mouse_event_current == entry) | |
402 | qemu_put_mouse_event_current = cursor->next; | |
403 | qemu_free(entry->qemu_put_mouse_event_name); | |
404 | qemu_free(entry); | |
405 | return; | |
406 | } | |
407 | ||
408 | prev->next = entry->next; | |
409 | ||
410 | if (qemu_put_mouse_event_current == entry) | |
411 | qemu_put_mouse_event_current = prev; | |
412 | ||
413 | qemu_free(entry->qemu_put_mouse_event_name); | |
414 | qemu_free(entry); | |
63066f4f FB |
415 | } |
416 | ||
417 | void kbd_put_keycode(int keycode) | |
418 | { | |
419 | if (qemu_put_kbd_event) { | |
420 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); | |
421 | } | |
422 | } | |
423 | ||
424 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) | |
425 | { | |
455204eb TS |
426 | QEMUPutMouseEvent *mouse_event; |
427 | void *mouse_event_opaque; | |
a171fe39 | 428 | int width; |
455204eb TS |
429 | |
430 | if (!qemu_put_mouse_event_current) { | |
431 | return; | |
432 | } | |
433 | ||
434 | mouse_event = | |
435 | qemu_put_mouse_event_current->qemu_put_mouse_event; | |
436 | mouse_event_opaque = | |
437 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; | |
438 | ||
439 | if (mouse_event) { | |
a171fe39 AZ |
440 | if (graphic_rotate) { |
441 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) | |
442 | width = 0x7fff; | |
443 | else | |
b94ed577 | 444 | width = graphic_width - 1; |
a171fe39 AZ |
445 | mouse_event(mouse_event_opaque, |
446 | width - dy, dx, dz, buttons_state); | |
447 | } else | |
448 | mouse_event(mouse_event_opaque, | |
449 | dx, dy, dz, buttons_state); | |
63066f4f FB |
450 | } |
451 | } | |
452 | ||
09b26c5e FB |
453 | int kbd_mouse_is_absolute(void) |
454 | { | |
455204eb TS |
455 | if (!qemu_put_mouse_event_current) |
456 | return 0; | |
457 | ||
458 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; | |
459 | } | |
460 | ||
376253ec | 461 | void do_info_mice(Monitor *mon) |
455204eb TS |
462 | { |
463 | QEMUPutMouseEntry *cursor; | |
464 | int index = 0; | |
465 | ||
466 | if (!qemu_put_mouse_event_head) { | |
376253ec | 467 | monitor_printf(mon, "No mouse devices connected\n"); |
455204eb TS |
468 | return; |
469 | } | |
470 | ||
376253ec | 471 | monitor_printf(mon, "Mouse devices available:\n"); |
455204eb TS |
472 | cursor = qemu_put_mouse_event_head; |
473 | while (cursor != NULL) { | |
376253ec AL |
474 | monitor_printf(mon, "%c Mouse #%d: %s\n", |
475 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), | |
476 | index, cursor->qemu_put_mouse_event_name); | |
455204eb TS |
477 | index++; |
478 | cursor = cursor->next; | |
479 | } | |
480 | } | |
481 | ||
d54908a5 | 482 | void do_mouse_set(Monitor *mon, const QDict *qdict) |
455204eb TS |
483 | { |
484 | QEMUPutMouseEntry *cursor; | |
485 | int i = 0; | |
d54908a5 | 486 | int index = qdict_get_int(qdict, "index"); |
455204eb TS |
487 | |
488 | if (!qemu_put_mouse_event_head) { | |
376253ec | 489 | monitor_printf(mon, "No mouse devices connected\n"); |
455204eb TS |
490 | return; |
491 | } | |
492 | ||
493 | cursor = qemu_put_mouse_event_head; | |
494 | while (cursor != NULL && index != i) { | |
495 | i++; | |
496 | cursor = cursor->next; | |
497 | } | |
498 | ||
499 | if (cursor != NULL) | |
500 | qemu_put_mouse_event_current = cursor; | |
501 | else | |
376253ec | 502 | monitor_printf(mon, "Mouse at given index not found\n"); |
09b26c5e FB |
503 | } |
504 | ||
1dce7c3c FB |
505 | /* compute with 96 bit intermediate result: (a*b)/c */ |
506 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) | |
0824d6fc | 507 | { |
1dce7c3c FB |
508 | union { |
509 | uint64_t ll; | |
510 | struct { | |
e2542fe2 | 511 | #ifdef HOST_WORDS_BIGENDIAN |
1dce7c3c FB |
512 | uint32_t high, low; |
513 | #else | |
514 | uint32_t low, high; | |
3b46e624 | 515 | #endif |
1dce7c3c FB |
516 | } l; |
517 | } u, res; | |
518 | uint64_t rl, rh; | |
0824d6fc | 519 | |
1dce7c3c FB |
520 | u.ll = a; |
521 | rl = (uint64_t)u.l.low * (uint64_t)b; | |
522 | rh = (uint64_t)u.l.high * (uint64_t)b; | |
523 | rh += (rl >> 32); | |
524 | res.l.high = rh / c; | |
525 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; | |
526 | return res.ll; | |
34865134 FB |
527 | } |
528 | ||
1dce7c3c FB |
529 | /***********************************************************/ |
530 | /* real time host monotonic timer */ | |
34865134 | 531 | |
1dce7c3c | 532 | #define QEMU_TIMER_BASE 1000000000LL |
34865134 | 533 | |
1dce7c3c | 534 | #ifdef WIN32 |
0824d6fc | 535 | |
1dce7c3c | 536 | static int64_t clock_freq; |
1115dde7 | 537 | |
1dce7c3c | 538 | static void init_get_clock(void) |
1115dde7 | 539 | { |
a8e5ac33 FB |
540 | LARGE_INTEGER freq; |
541 | int ret; | |
1dce7c3c FB |
542 | ret = QueryPerformanceFrequency(&freq); |
543 | if (ret == 0) { | |
544 | fprintf(stderr, "Could not calibrate ticks\n"); | |
545 | exit(1); | |
546 | } | |
547 | clock_freq = freq.QuadPart; | |
1115dde7 FB |
548 | } |
549 | ||
1dce7c3c | 550 | static int64_t get_clock(void) |
b8076a74 | 551 | { |
1dce7c3c FB |
552 | LARGE_INTEGER ti; |
553 | QueryPerformanceCounter(&ti); | |
554 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); | |
b8076a74 FB |
555 | } |
556 | ||
1dce7c3c | 557 | #else |
90cb9493 | 558 | |
1dce7c3c FB |
559 | static int use_rt_clock; |
560 | ||
561 | static void init_get_clock(void) | |
90cb9493 | 562 | { |
1dce7c3c | 563 | use_rt_clock = 0; |
c5e97233 BS |
564 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
565 | || defined(__DragonFly__) | |
1dce7c3c FB |
566 | { |
567 | struct timespec ts; | |
568 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { | |
569 | use_rt_clock = 1; | |
570 | } | |
571 | } | |
572 | #endif | |
90cb9493 FB |
573 | } |
574 | ||
1dce7c3c | 575 | static int64_t get_clock(void) |
fdbb4691 | 576 | { |
c5e97233 BS |
577 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
578 | || defined(__DragonFly__) | |
1dce7c3c FB |
579 | if (use_rt_clock) { |
580 | struct timespec ts; | |
581 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
582 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; | |
5fafdf24 | 583 | } else |
fdbb4691 | 584 | #endif |
1dce7c3c FB |
585 | { |
586 | /* XXX: using gettimeofday leads to problems if the date | |
587 | changes, so it should be avoided. */ | |
588 | struct timeval tv; | |
589 | gettimeofday(&tv, NULL); | |
590 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); | |
591 | } | |
fdbb4691 | 592 | } |
34865134 FB |
593 | #endif |
594 | ||
2e70f6ef PB |
595 | /* Return the virtual CPU time, based on the instruction counter. */ |
596 | static int64_t cpu_get_icount(void) | |
597 | { | |
598 | int64_t icount; | |
599 | CPUState *env = cpu_single_env;; | |
600 | icount = qemu_icount; | |
601 | if (env) { | |
602 | if (!can_do_io(env)) | |
603 | fprintf(stderr, "Bad clock read\n"); | |
604 | icount -= (env->icount_decr.u16.low + env->icount_extra); | |
605 | } | |
606 | return qemu_icount_bias + (icount << icount_time_shift); | |
607 | } | |
608 | ||
1dce7c3c FB |
609 | /***********************************************************/ |
610 | /* guest cycle counter */ | |
611 | ||
eade0f19 | 612 | static int64_t cpu_ticks_prev; |
34865134 | 613 | static int64_t cpu_ticks_offset; |
1dce7c3c | 614 | static int64_t cpu_clock_offset; |
8a7ddc38 | 615 | static int cpu_ticks_enabled; |
34865134 | 616 | |
1dce7c3c FB |
617 | /* return the host CPU cycle counter and handle stop/restart */ |
618 | int64_t cpu_get_ticks(void) | |
34865134 | 619 | { |
2e70f6ef PB |
620 | if (use_icount) { |
621 | return cpu_get_icount(); | |
622 | } | |
8a7ddc38 FB |
623 | if (!cpu_ticks_enabled) { |
624 | return cpu_ticks_offset; | |
625 | } else { | |
eade0f19 FB |
626 | int64_t ticks; |
627 | ticks = cpu_get_real_ticks(); | |
628 | if (cpu_ticks_prev > ticks) { | |
629 | /* Note: non increasing ticks may happen if the host uses | |
630 | software suspend */ | |
631 | cpu_ticks_offset += cpu_ticks_prev - ticks; | |
632 | } | |
633 | cpu_ticks_prev = ticks; | |
634 | return ticks + cpu_ticks_offset; | |
8a7ddc38 | 635 | } |
34865134 FB |
636 | } |
637 | ||
1dce7c3c FB |
638 | /* return the host CPU monotonic timer and handle stop/restart */ |
639 | static int64_t cpu_get_clock(void) | |
640 | { | |
641 | int64_t ti; | |
642 | if (!cpu_ticks_enabled) { | |
643 | return cpu_clock_offset; | |
644 | } else { | |
645 | ti = get_clock(); | |
646 | return ti + cpu_clock_offset; | |
647 | } | |
648 | } | |
649 | ||
34865134 FB |
650 | /* enable cpu_get_ticks() */ |
651 | void cpu_enable_ticks(void) | |
652 | { | |
8a7ddc38 FB |
653 | if (!cpu_ticks_enabled) { |
654 | cpu_ticks_offset -= cpu_get_real_ticks(); | |
1dce7c3c | 655 | cpu_clock_offset -= get_clock(); |
8a7ddc38 FB |
656 | cpu_ticks_enabled = 1; |
657 | } | |
34865134 FB |
658 | } |
659 | ||
660 | /* disable cpu_get_ticks() : the clock is stopped. You must not call | |
661 | cpu_get_ticks() after that. */ | |
662 | void cpu_disable_ticks(void) | |
663 | { | |
8a7ddc38 FB |
664 | if (cpu_ticks_enabled) { |
665 | cpu_ticks_offset = cpu_get_ticks(); | |
1dce7c3c | 666 | cpu_clock_offset = cpu_get_clock(); |
8a7ddc38 FB |
667 | cpu_ticks_enabled = 0; |
668 | } | |
34865134 FB |
669 | } |
670 | ||
1dce7c3c FB |
671 | /***********************************************************/ |
672 | /* timers */ | |
5fafdf24 | 673 | |
8a7ddc38 FB |
674 | #define QEMU_TIMER_REALTIME 0 |
675 | #define QEMU_TIMER_VIRTUAL 1 | |
676 | ||
677 | struct QEMUClock { | |
678 | int type; | |
679 | /* XXX: add frequency */ | |
680 | }; | |
681 | ||
682 | struct QEMUTimer { | |
683 | QEMUClock *clock; | |
684 | int64_t expire_time; | |
685 | QEMUTimerCB *cb; | |
686 | void *opaque; | |
687 | struct QEMUTimer *next; | |
688 | }; | |
689 | ||
c8994013 TS |
690 | struct qemu_alarm_timer { |
691 | char const *name; | |
efe75411 | 692 | unsigned int flags; |
c8994013 TS |
693 | |
694 | int (*start)(struct qemu_alarm_timer *t); | |
695 | void (*stop)(struct qemu_alarm_timer *t); | |
efe75411 | 696 | void (*rearm)(struct qemu_alarm_timer *t); |
c8994013 TS |
697 | void *priv; |
698 | }; | |
699 | ||
efe75411 | 700 | #define ALARM_FLAG_DYNTICKS 0x1 |
d5d08334 | 701 | #define ALARM_FLAG_EXPIRED 0x2 |
efe75411 TS |
702 | |
703 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) | |
704 | { | |
e332340a | 705 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
efe75411 TS |
706 | } |
707 | ||
708 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) | |
709 | { | |
710 | if (!alarm_has_dynticks(t)) | |
711 | return; | |
712 | ||
713 | t->rearm(t); | |
714 | } | |
715 | ||
716 | /* TODO: MIN_TIMER_REARM_US should be optimized */ | |
717 | #define MIN_TIMER_REARM_US 250 | |
718 | ||
c8994013 | 719 | static struct qemu_alarm_timer *alarm_timer; |
8a7ddc38 | 720 | |
40c3bac3 | 721 | #ifdef _WIN32 |
c8994013 TS |
722 | |
723 | struct qemu_alarm_win32 { | |
724 | MMRESULT timerId; | |
c8994013 | 725 | unsigned int period; |
ef28c4b0 | 726 | } alarm_win32_data = {0, -1}; |
c8994013 TS |
727 | |
728 | static int win32_start_timer(struct qemu_alarm_timer *t); | |
729 | static void win32_stop_timer(struct qemu_alarm_timer *t); | |
efe75411 | 730 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
c8994013 | 731 | |
40c3bac3 | 732 | #else |
c8994013 TS |
733 | |
734 | static int unix_start_timer(struct qemu_alarm_timer *t); | |
735 | static void unix_stop_timer(struct qemu_alarm_timer *t); | |
736 | ||
231c6586 TS |
737 | #ifdef __linux__ |
738 | ||
efe75411 TS |
739 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
740 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); | |
741 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); | |
742 | ||
c40ec5a9 TS |
743 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
744 | static void hpet_stop_timer(struct qemu_alarm_timer *t); | |
745 | ||
c8994013 TS |
746 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
747 | static void rtc_stop_timer(struct qemu_alarm_timer *t); | |
748 | ||
efe75411 | 749 | #endif /* __linux__ */ |
8a7ddc38 | 750 | |
c8994013 TS |
751 | #endif /* _WIN32 */ |
752 | ||
2e70f6ef | 753 | /* Correlation between real and virtual time is always going to be |
bf20dc07 | 754 | fairly approximate, so ignore small variation. |
2e70f6ef PB |
755 | When the guest is idle real and virtual time will be aligned in |
756 | the IO wait loop. */ | |
757 | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) | |
758 | ||
759 | static void icount_adjust(void) | |
760 | { | |
761 | int64_t cur_time; | |
762 | int64_t cur_icount; | |
763 | int64_t delta; | |
764 | static int64_t last_delta; | |
765 | /* If the VM is not running, then do nothing. */ | |
766 | if (!vm_running) | |
767 | return; | |
768 | ||
769 | cur_time = cpu_get_clock(); | |
770 | cur_icount = qemu_get_clock(vm_clock); | |
771 | delta = cur_icount - cur_time; | |
772 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ | |
773 | if (delta > 0 | |
774 | && last_delta + ICOUNT_WOBBLE < delta * 2 | |
775 | && icount_time_shift > 0) { | |
776 | /* The guest is getting too far ahead. Slow time down. */ | |
777 | icount_time_shift--; | |
778 | } | |
779 | if (delta < 0 | |
780 | && last_delta - ICOUNT_WOBBLE > delta * 2 | |
781 | && icount_time_shift < MAX_ICOUNT_SHIFT) { | |
782 | /* The guest is getting too far behind. Speed time up. */ | |
783 | icount_time_shift++; | |
784 | } | |
785 | last_delta = delta; | |
786 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); | |
787 | } | |
788 | ||
789 | static void icount_adjust_rt(void * opaque) | |
790 | { | |
791 | qemu_mod_timer(icount_rt_timer, | |
792 | qemu_get_clock(rt_clock) + 1000); | |
793 | icount_adjust(); | |
794 | } | |
795 | ||
796 | static void icount_adjust_vm(void * opaque) | |
797 | { | |
798 | qemu_mod_timer(icount_vm_timer, | |
799 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); | |
800 | icount_adjust(); | |
801 | } | |
802 | ||
803 | static void init_icount_adjust(void) | |
804 | { | |
805 | /* Have both realtime and virtual time triggers for speed adjustment. | |
806 | The realtime trigger catches emulated time passing too slowly, | |
807 | the virtual time trigger catches emulated time passing too fast. | |
808 | Realtime triggers occur even when idle, so use them less frequently | |
809 | than VM triggers. */ | |
810 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); | |
811 | qemu_mod_timer(icount_rt_timer, | |
812 | qemu_get_clock(rt_clock) + 1000); | |
813 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); | |
814 | qemu_mod_timer(icount_vm_timer, | |
815 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); | |
816 | } | |
817 | ||
c8994013 | 818 | static struct qemu_alarm_timer alarm_timers[] = { |
efe75411 | 819 | #ifndef _WIN32 |
231c6586 | 820 | #ifdef __linux__ |
efe75411 TS |
821 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
822 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, | |
c40ec5a9 | 823 | /* HPET - if available - is preferred */ |
efe75411 | 824 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
c40ec5a9 | 825 | /* ...otherwise try RTC */ |
efe75411 | 826 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
c8994013 | 827 | #endif |
efe75411 | 828 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
c8994013 | 829 | #else |
efe75411 TS |
830 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
831 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, | |
832 | {"win32", 0, win32_start_timer, | |
833 | win32_stop_timer, NULL, &alarm_win32_data}, | |
c8994013 TS |
834 | #endif |
835 | {NULL, } | |
836 | }; | |
837 | ||
3f47aa8c | 838 | static void show_available_alarms(void) |
f3dcfada TS |
839 | { |
840 | int i; | |
841 | ||
842 | printf("Available alarm timers, in order of precedence:\n"); | |
843 | for (i = 0; alarm_timers[i].name; i++) | |
844 | printf("%s\n", alarm_timers[i].name); | |
845 | } | |
846 | ||
847 | static void configure_alarms(char const *opt) | |
848 | { | |
849 | int i; | |
850 | int cur = 0; | |
b1503cda | 851 | int count = ARRAY_SIZE(alarm_timers) - 1; |
f3dcfada TS |
852 | char *arg; |
853 | char *name; | |
2e70f6ef | 854 | struct qemu_alarm_timer tmp; |
f3dcfada | 855 | |
3adda04c | 856 | if (!strcmp(opt, "?")) { |
f3dcfada TS |
857 | show_available_alarms(); |
858 | exit(0); | |
859 | } | |
860 | ||
73ffc805 | 861 | arg = qemu_strdup(opt); |
f3dcfada TS |
862 | |
863 | /* Reorder the array */ | |
864 | name = strtok(arg, ","); | |
865 | while (name) { | |
e2b577e5 | 866 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
f3dcfada TS |
867 | if (!strcmp(alarm_timers[i].name, name)) |
868 | break; | |
869 | } | |
870 | ||
871 | if (i == count) { | |
872 | fprintf(stderr, "Unknown clock %s\n", name); | |
873 | goto next; | |
874 | } | |
875 | ||
876 | if (i < cur) | |
877 | /* Ignore */ | |
878 | goto next; | |
879 | ||
880 | /* Swap */ | |
881 | tmp = alarm_timers[i]; | |
882 | alarm_timers[i] = alarm_timers[cur]; | |
883 | alarm_timers[cur] = tmp; | |
884 | ||
885 | cur++; | |
886 | next: | |
887 | name = strtok(NULL, ","); | |
888 | } | |
889 | ||
73ffc805 | 890 | qemu_free(arg); |
f3dcfada TS |
891 | |
892 | if (cur) { | |
2e70f6ef | 893 | /* Disable remaining timers */ |
f3dcfada TS |
894 | for (i = cur; i < count; i++) |
895 | alarm_timers[i].name = NULL; | |
3adda04c AJ |
896 | } else { |
897 | show_available_alarms(); | |
898 | exit(1); | |
f3dcfada | 899 | } |
f3dcfada TS |
900 | } |
901 | ||
c8994013 TS |
902 | QEMUClock *rt_clock; |
903 | QEMUClock *vm_clock; | |
904 | ||
905 | static QEMUTimer *active_timers[2]; | |
906 | ||
9596ebb7 | 907 | static QEMUClock *qemu_new_clock(int type) |
8a7ddc38 FB |
908 | { |
909 | QEMUClock *clock; | |
910 | clock = qemu_mallocz(sizeof(QEMUClock)); | |
8a7ddc38 FB |
911 | clock->type = type; |
912 | return clock; | |
913 | } | |
914 | ||
915 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) | |
916 | { | |
917 | QEMUTimer *ts; | |
918 | ||
919 | ts = qemu_mallocz(sizeof(QEMUTimer)); | |
920 | ts->clock = clock; | |
921 | ts->cb = cb; | |
922 | ts->opaque = opaque; | |
923 | return ts; | |
924 | } | |
925 | ||
926 | void qemu_free_timer(QEMUTimer *ts) | |
927 | { | |
928 | qemu_free(ts); | |
929 | } | |
930 | ||
931 | /* stop a timer, but do not dealloc it */ | |
932 | void qemu_del_timer(QEMUTimer *ts) | |
933 | { | |
934 | QEMUTimer **pt, *t; | |
935 | ||
936 | /* NOTE: this code must be signal safe because | |
937 | qemu_timer_expired() can be called from a signal. */ | |
938 | pt = &active_timers[ts->clock->type]; | |
939 | for(;;) { | |
940 | t = *pt; | |
941 | if (!t) | |
942 | break; | |
943 | if (t == ts) { | |
944 | *pt = t->next; | |
945 | break; | |
946 | } | |
947 | pt = &t->next; | |
948 | } | |
949 | } | |
950 | ||
951 | /* modify the current timer so that it will be fired when current_time | |
952 | >= expire_time. The corresponding callback will be called. */ | |
953 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) | |
954 | { | |
955 | QEMUTimer **pt, *t; | |
956 | ||
957 | qemu_del_timer(ts); | |
958 | ||
959 | /* add the timer in the sorted list */ | |
960 | /* NOTE: this code must be signal safe because | |
961 | qemu_timer_expired() can be called from a signal. */ | |
962 | pt = &active_timers[ts->clock->type]; | |
963 | for(;;) { | |
964 | t = *pt; | |
965 | if (!t) | |
966 | break; | |
5fafdf24 | 967 | if (t->expire_time > expire_time) |
8a7ddc38 FB |
968 | break; |
969 | pt = &t->next; | |
970 | } | |
971 | ts->expire_time = expire_time; | |
972 | ts->next = *pt; | |
973 | *pt = ts; | |
d5d08334 AZ |
974 | |
975 | /* Rearm if necessary */ | |
2e70f6ef PB |
976 | if (pt == &active_timers[ts->clock->type]) { |
977 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { | |
978 | qemu_rearm_alarm_timer(alarm_timer); | |
979 | } | |
980 | /* Interrupt execution to force deadline recalculation. */ | |
d9f75a4e AL |
981 | if (use_icount) |
982 | qemu_notify_event(); | |
2e70f6ef | 983 | } |
8a7ddc38 FB |
984 | } |
985 | ||
986 | int qemu_timer_pending(QEMUTimer *ts) | |
987 | { | |
988 | QEMUTimer *t; | |
989 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { | |
990 | if (t == ts) | |
991 | return 1; | |
992 | } | |
993 | return 0; | |
994 | } | |
995 | ||
2430ffe4 | 996 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
8a7ddc38 FB |
997 | { |
998 | if (!timer_head) | |
999 | return 0; | |
1000 | return (timer_head->expire_time <= current_time); | |
1001 | } | |
1002 | ||
1003 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) | |
1004 | { | |
1005 | QEMUTimer *ts; | |
3b46e624 | 1006 | |
8a7ddc38 FB |
1007 | for(;;) { |
1008 | ts = *ptimer_head; | |
e95c8d51 | 1009 | if (!ts || ts->expire_time > current_time) |
8a7ddc38 FB |
1010 | break; |
1011 | /* remove timer from the list before calling the callback */ | |
1012 | *ptimer_head = ts->next; | |
1013 | ts->next = NULL; | |
3b46e624 | 1014 | |
8a7ddc38 FB |
1015 | /* run the callback (the timer list can be modified) */ |
1016 | ts->cb(ts->opaque); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | int64_t qemu_get_clock(QEMUClock *clock) | |
1021 | { | |
1022 | switch(clock->type) { | |
1023 | case QEMU_TIMER_REALTIME: | |
1dce7c3c | 1024 | return get_clock() / 1000000; |
8a7ddc38 FB |
1025 | default: |
1026 | case QEMU_TIMER_VIRTUAL: | |
2e70f6ef PB |
1027 | if (use_icount) { |
1028 | return cpu_get_icount(); | |
1029 | } else { | |
1030 | return cpu_get_clock(); | |
1031 | } | |
8a7ddc38 FB |
1032 | } |
1033 | } | |
1034 | ||
6ee093c9 JQ |
1035 | int64_t get_ticks_per_sec(void) |
1036 | { | |
1037 | return ticks_per_sec; | |
1038 | } | |
1039 | ||
1dce7c3c FB |
1040 | static void init_timers(void) |
1041 | { | |
1042 | init_get_clock(); | |
1043 | ticks_per_sec = QEMU_TIMER_BASE; | |
1044 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); | |
1045 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); | |
1046 | } | |
1047 | ||
8a7ddc38 FB |
1048 | /* save a timer */ |
1049 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) | |
1050 | { | |
1051 | uint64_t expire_time; | |
1052 | ||
1053 | if (qemu_timer_pending(ts)) { | |
1054 | expire_time = ts->expire_time; | |
1055 | } else { | |
1056 | expire_time = -1; | |
1057 | } | |
1058 | qemu_put_be64(f, expire_time); | |
1059 | } | |
1060 | ||
1061 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) | |
1062 | { | |
1063 | uint64_t expire_time; | |
1064 | ||
1065 | expire_time = qemu_get_be64(f); | |
1066 | if (expire_time != -1) { | |
1067 | qemu_mod_timer(ts, expire_time); | |
1068 | } else { | |
1069 | qemu_del_timer(ts); | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | static void timer_save(QEMUFile *f, void *opaque) | |
1074 | { | |
bee8d684 TS |
1075 | qemu_put_be64(f, cpu_ticks_offset); |
1076 | qemu_put_be64(f, ticks_per_sec); | |
1077 | qemu_put_be64(f, cpu_clock_offset); | |
8a7ddc38 FB |
1078 | } |
1079 | ||
1080 | static int timer_load(QEMUFile *f, void *opaque, int version_id) | |
1081 | { | |
c88676f8 | 1082 | if (version_id != 1 && version_id != 2) |
8a7ddc38 | 1083 | return -EINVAL; |
bee8d684 TS |
1084 | cpu_ticks_offset=qemu_get_be64(f); |
1085 | ticks_per_sec=qemu_get_be64(f); | |
c88676f8 | 1086 | if (version_id == 2) { |
bee8d684 | 1087 | cpu_clock_offset=qemu_get_be64(f); |
c88676f8 | 1088 | } |
8a7ddc38 FB |
1089 | return 0; |
1090 | } | |
1091 | ||
50317c7f AL |
1092 | static void qemu_event_increment(void); |
1093 | ||
67b915a5 | 1094 | #ifdef _WIN32 |
b9e82a59 BS |
1095 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
1096 | DWORD_PTR dwUser, DWORD_PTR dw1, | |
1097 | DWORD_PTR dw2) | |
67b915a5 | 1098 | #else |
8a7ddc38 | 1099 | static void host_alarm_handler(int host_signum) |
67b915a5 | 1100 | #endif |
8a7ddc38 | 1101 | { |
02ba45c5 FB |
1102 | #if 0 |
1103 | #define DISP_FREQ 1000 | |
1104 | { | |
1105 | static int64_t delta_min = INT64_MAX; | |
1106 | static int64_t delta_max, delta_cum, last_clock, delta, ti; | |
1107 | static int count; | |
1108 | ti = qemu_get_clock(vm_clock); | |
1109 | if (last_clock != 0) { | |
1110 | delta = ti - last_clock; | |
1111 | if (delta < delta_min) | |
1112 | delta_min = delta; | |
1113 | if (delta > delta_max) | |
1114 | delta_max = delta; | |
1115 | delta_cum += delta; | |
1116 | if (++count == DISP_FREQ) { | |
26a76461 | 1117 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
6ee093c9 JQ |
1118 | muldiv64(delta_min, 1000000, get_ticks_per_sec()), |
1119 | muldiv64(delta_max, 1000000, get_ticks_per_sec()), | |
1120 | muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()), | |
1121 | (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ)); | |
02ba45c5 FB |
1122 | count = 0; |
1123 | delta_min = INT64_MAX; | |
1124 | delta_max = 0; | |
1125 | delta_cum = 0; | |
1126 | } | |
1127 | } | |
1128 | last_clock = ti; | |
1129 | } | |
1130 | #endif | |
efe75411 | 1131 | if (alarm_has_dynticks(alarm_timer) || |
2e70f6ef PB |
1132 | (!use_icount && |
1133 | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], | |
1134 | qemu_get_clock(vm_clock))) || | |
8a7ddc38 FB |
1135 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1136 | qemu_get_clock(rt_clock))) { | |
50317c7f | 1137 | qemu_event_increment(); |
e332340a | 1138 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
d5d08334 | 1139 | |
d6dc3d42 AL |
1140 | #ifndef CONFIG_IOTHREAD |
1141 | if (next_cpu) { | |
4f8eb8da | 1142 | /* stop the currently executing cpu because a timer occured */ |
d6dc3d42 | 1143 | cpu_exit(next_cpu); |
4f8eb8da | 1144 | } |
d6dc3d42 | 1145 | #endif |
43b96858 | 1146 | timer_alarm_pending = 1; |
d9f75a4e | 1147 | qemu_notify_event(); |
8a7ddc38 FB |
1148 | } |
1149 | } | |
1150 | ||
2e70f6ef | 1151 | static int64_t qemu_next_deadline(void) |
efe75411 | 1152 | { |
2e70f6ef | 1153 | int64_t delta; |
efe75411 TS |
1154 | |
1155 | if (active_timers[QEMU_TIMER_VIRTUAL]) { | |
2e70f6ef PB |
1156 | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1157 | qemu_get_clock(vm_clock); | |
1158 | } else { | |
1159 | /* To avoid problems with overflow limit this to 2^32. */ | |
1160 | delta = INT32_MAX; | |
efe75411 TS |
1161 | } |
1162 | ||
2e70f6ef PB |
1163 | if (delta < 0) |
1164 | delta = 0; | |
efe75411 | 1165 | |
2e70f6ef PB |
1166 | return delta; |
1167 | } | |
1168 | ||
8632fb9a | 1169 | #if defined(__linux__) || defined(_WIN32) |
2e70f6ef PB |
1170 | static uint64_t qemu_next_deadline_dyntick(void) |
1171 | { | |
1172 | int64_t delta; | |
1173 | int64_t rtdelta; | |
1174 | ||
1175 | if (use_icount) | |
1176 | delta = INT32_MAX; | |
1177 | else | |
1178 | delta = (qemu_next_deadline() + 999) / 1000; | |
1179 | ||
1180 | if (active_timers[QEMU_TIMER_REALTIME]) { | |
1181 | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - | |
1182 | qemu_get_clock(rt_clock))*1000; | |
1183 | if (rtdelta < delta) | |
1184 | delta = rtdelta; | |
1185 | } | |
1186 | ||
1187 | if (delta < MIN_TIMER_REARM_US) | |
1188 | delta = MIN_TIMER_REARM_US; | |
1189 | ||
1190 | return delta; | |
efe75411 | 1191 | } |
8632fb9a | 1192 | #endif |
efe75411 | 1193 | |
fd872598 FB |
1194 | #ifndef _WIN32 |
1195 | ||
7183b4b4 AL |
1196 | /* Sets a specific flag */ |
1197 | static int fcntl_setfl(int fd, int flag) | |
1198 | { | |
1199 | int flags; | |
1200 | ||
1201 | flags = fcntl(fd, F_GETFL); | |
1202 | if (flags == -1) | |
1203 | return -errno; | |
1204 | ||
1205 | if (fcntl(fd, F_SETFL, flags | flag) == -1) | |
1206 | return -errno; | |
1207 | ||
1208 | return 0; | |
1209 | } | |
1210 | ||
829309c7 FB |
1211 | #if defined(__linux__) |
1212 | ||
fd872598 FB |
1213 | #define RTC_FREQ 1024 |
1214 | ||
de9a95f0 | 1215 | static void enable_sigio_timer(int fd) |
c8994013 TS |
1216 | { |
1217 | struct sigaction act; | |
1218 | ||
1219 | /* timer signal */ | |
1220 | sigfillset(&act.sa_mask); | |
1221 | act.sa_flags = 0; | |
c8994013 TS |
1222 | act.sa_handler = host_alarm_handler; |
1223 | ||
1224 | sigaction(SIGIO, &act, NULL); | |
7183b4b4 | 1225 | fcntl_setfl(fd, O_ASYNC); |
c8994013 TS |
1226 | fcntl(fd, F_SETOWN, getpid()); |
1227 | } | |
829309c7 | 1228 | |
c40ec5a9 TS |
1229 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1230 | { | |
1231 | struct hpet_info info; | |
1232 | int r, fd; | |
1233 | ||
1234 | fd = open("/dev/hpet", O_RDONLY); | |
1235 | if (fd < 0) | |
1236 | return -1; | |
1237 | ||
1238 | /* Set frequency */ | |
1239 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); | |
1240 | if (r < 0) { | |
1241 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" | |
1242 | "error, but for better emulation accuracy type:\n" | |
1243 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); | |
1244 | goto fail; | |
1245 | } | |
1246 | ||
1247 | /* Check capabilities */ | |
1248 | r = ioctl(fd, HPET_INFO, &info); | |
1249 | if (r < 0) | |
1250 | goto fail; | |
1251 | ||
1252 | /* Enable periodic mode */ | |
1253 | r = ioctl(fd, HPET_EPI, 0); | |
1254 | if (info.hi_flags && (r < 0)) | |
1255 | goto fail; | |
1256 | ||
1257 | /* Enable interrupt */ | |
1258 | r = ioctl(fd, HPET_IE_ON, 0); | |
1259 | if (r < 0) | |
1260 | goto fail; | |
1261 | ||
1262 | enable_sigio_timer(fd); | |
fcdc2129 | 1263 | t->priv = (void *)(long)fd; |
c40ec5a9 TS |
1264 | |
1265 | return 0; | |
1266 | fail: | |
1267 | close(fd); | |
1268 | return -1; | |
1269 | } | |
1270 | ||
1271 | static void hpet_stop_timer(struct qemu_alarm_timer *t) | |
1272 | { | |
fcdc2129 | 1273 | int fd = (long)t->priv; |
c40ec5a9 TS |
1274 | |
1275 | close(fd); | |
1276 | } | |
1277 | ||
c8994013 | 1278 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
fd872598 | 1279 | { |
c8994013 | 1280 | int rtc_fd; |
b5a23ad4 | 1281 | unsigned long current_rtc_freq = 0; |
c8994013 | 1282 | |
aeb30be6 | 1283 | TFR(rtc_fd = open("/dev/rtc", O_RDONLY)); |
fd872598 FB |
1284 | if (rtc_fd < 0) |
1285 | return -1; | |
b5a23ad4 AZ |
1286 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1287 | if (current_rtc_freq != RTC_FREQ && | |
1288 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { | |
fd872598 FB |
1289 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
1290 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" | |
1291 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); | |
1292 | goto fail; | |
1293 | } | |
1294 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { | |
1295 | fail: | |
1296 | close(rtc_fd); | |
1297 | return -1; | |
1298 | } | |
c8994013 TS |
1299 | |
1300 | enable_sigio_timer(rtc_fd); | |
1301 | ||
fcdc2129 | 1302 | t->priv = (void *)(long)rtc_fd; |
c8994013 | 1303 | |
fd872598 FB |
1304 | return 0; |
1305 | } | |
1306 | ||
c8994013 | 1307 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
829309c7 | 1308 | { |
fcdc2129 | 1309 | int rtc_fd = (long)t->priv; |
c8994013 TS |
1310 | |
1311 | close(rtc_fd); | |
829309c7 FB |
1312 | } |
1313 | ||
efe75411 TS |
1314 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1315 | { | |
1316 | struct sigevent ev; | |
1317 | timer_t host_timer; | |
1318 | struct sigaction act; | |
1319 | ||
1320 | sigfillset(&act.sa_mask); | |
1321 | act.sa_flags = 0; | |
efe75411 TS |
1322 | act.sa_handler = host_alarm_handler; |
1323 | ||
1324 | sigaction(SIGALRM, &act, NULL); | |
1325 | ||
9ed415b2 JCD |
1326 | /* |
1327 | * Initialize ev struct to 0 to avoid valgrind complaining | |
1328 | * about uninitialized data in timer_create call | |
1329 | */ | |
1330 | memset(&ev, 0, sizeof(ev)); | |
efe75411 TS |
1331 | ev.sigev_value.sival_int = 0; |
1332 | ev.sigev_notify = SIGEV_SIGNAL; | |
1333 | ev.sigev_signo = SIGALRM; | |
1334 | ||
1335 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { | |
1336 | perror("timer_create"); | |
1337 | ||
1338 | /* disable dynticks */ | |
1339 | fprintf(stderr, "Dynamic Ticks disabled\n"); | |
1340 | ||
1341 | return -1; | |
1342 | } | |
1343 | ||
0399bfe0 | 1344 | t->priv = (void *)(long)host_timer; |
efe75411 TS |
1345 | |
1346 | return 0; | |
1347 | } | |
1348 | ||
1349 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) | |
1350 | { | |
0399bfe0 | 1351 | timer_t host_timer = (timer_t)(long)t->priv; |
efe75411 TS |
1352 | |
1353 | timer_delete(host_timer); | |
1354 | } | |
1355 | ||
1356 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) | |
1357 | { | |
0399bfe0 | 1358 | timer_t host_timer = (timer_t)(long)t->priv; |
efe75411 TS |
1359 | struct itimerspec timeout; |
1360 | int64_t nearest_delta_us = INT64_MAX; | |
1361 | int64_t current_us; | |
1362 | ||
1363 | if (!active_timers[QEMU_TIMER_REALTIME] && | |
1364 | !active_timers[QEMU_TIMER_VIRTUAL]) | |
d5d08334 | 1365 | return; |
efe75411 | 1366 | |
2e70f6ef | 1367 | nearest_delta_us = qemu_next_deadline_dyntick(); |
efe75411 TS |
1368 | |
1369 | /* check whether a timer is already running */ | |
1370 | if (timer_gettime(host_timer, &timeout)) { | |
1371 | perror("gettime"); | |
1372 | fprintf(stderr, "Internal timer error: aborting\n"); | |
1373 | exit(1); | |
1374 | } | |
1375 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; | |
1376 | if (current_us && current_us <= nearest_delta_us) | |
1377 | return; | |
1378 | ||
1379 | timeout.it_interval.tv_sec = 0; | |
1380 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ | |
1381 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; | |
1382 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; | |
1383 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { | |
1384 | perror("settime"); | |
1385 | fprintf(stderr, "Internal timer error: aborting\n"); | |
1386 | exit(1); | |
1387 | } | |
1388 | } | |
1389 | ||
70744b3a | 1390 | #endif /* defined(__linux__) */ |
231c6586 | 1391 | |
c8994013 TS |
1392 | static int unix_start_timer(struct qemu_alarm_timer *t) |
1393 | { | |
1394 | struct sigaction act; | |
1395 | struct itimerval itv; | |
1396 | int err; | |
1397 | ||
1398 | /* timer signal */ | |
1399 | sigfillset(&act.sa_mask); | |
1400 | act.sa_flags = 0; | |
c8994013 TS |
1401 | act.sa_handler = host_alarm_handler; |
1402 | ||
1403 | sigaction(SIGALRM, &act, NULL); | |
1404 | ||
1405 | itv.it_interval.tv_sec = 0; | |
1406 | /* for i386 kernel 2.6 to get 1 ms */ | |
1407 | itv.it_interval.tv_usec = 999; | |
1408 | itv.it_value.tv_sec = 0; | |
1409 | itv.it_value.tv_usec = 10 * 1000; | |
1410 | ||
1411 | err = setitimer(ITIMER_REAL, &itv, NULL); | |
1412 | if (err) | |
1413 | return -1; | |
1414 | ||
1415 | return 0; | |
1416 | } | |
1417 | ||
1418 | static void unix_stop_timer(struct qemu_alarm_timer *t) | |
1419 | { | |
1420 | struct itimerval itv; | |
1421 | ||
1422 | memset(&itv, 0, sizeof(itv)); | |
1423 | setitimer(ITIMER_REAL, &itv, NULL); | |
1424 | } | |
1425 | ||
829309c7 | 1426 | #endif /* !defined(_WIN32) */ |
fd872598 | 1427 | |
f49e58dc | 1428 | |
c8994013 TS |
1429 | #ifdef _WIN32 |
1430 | ||
1431 | static int win32_start_timer(struct qemu_alarm_timer *t) | |
1432 | { | |
1433 | TIMECAPS tc; | |
1434 | struct qemu_alarm_win32 *data = t->priv; | |
efe75411 | 1435 | UINT flags; |
c8994013 | 1436 | |
c8994013 TS |
1437 | memset(&tc, 0, sizeof(tc)); |
1438 | timeGetDevCaps(&tc, sizeof(tc)); | |
1439 | ||
1440 | if (data->period < tc.wPeriodMin) | |
1441 | data->period = tc.wPeriodMin; | |
1442 | ||
1443 | timeBeginPeriod(data->period); | |
1444 | ||
efe75411 TS |
1445 | flags = TIME_CALLBACK_FUNCTION; |
1446 | if (alarm_has_dynticks(t)) | |
1447 | flags |= TIME_ONESHOT; | |
1448 | else | |
1449 | flags |= TIME_PERIODIC; | |
1450 | ||
c8994013 TS |
1451 | data->timerId = timeSetEvent(1, // interval (ms) |
1452 | data->period, // resolution | |
1453 | host_alarm_handler, // function | |
1454 | (DWORD)t, // parameter | |
efe75411 | 1455 | flags); |
c8994013 TS |
1456 | |
1457 | if (!data->timerId) { | |
1458 | perror("Failed to initialize win32 alarm timer"); | |
c8994013 | 1459 | timeEndPeriod(data->period); |
c8994013 TS |
1460 | return -1; |
1461 | } | |
1462 | ||
c8994013 TS |
1463 | return 0; |
1464 | } | |
1465 | ||
1466 | static void win32_stop_timer(struct qemu_alarm_timer *t) | |
1467 | { | |
1468 | struct qemu_alarm_win32 *data = t->priv; | |
1469 | ||
1470 | timeKillEvent(data->timerId); | |
1471 | timeEndPeriod(data->period); | |
c8994013 TS |
1472 | } |
1473 | ||
efe75411 TS |
1474 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1475 | { | |
1476 | struct qemu_alarm_win32 *data = t->priv; | |
1477 | uint64_t nearest_delta_us; | |
1478 | ||
1479 | if (!active_timers[QEMU_TIMER_REALTIME] && | |
1480 | !active_timers[QEMU_TIMER_VIRTUAL]) | |
d5d08334 | 1481 | return; |
efe75411 | 1482 | |
2e70f6ef | 1483 | nearest_delta_us = qemu_next_deadline_dyntick(); |
efe75411 TS |
1484 | nearest_delta_us /= 1000; |
1485 | ||
1486 | timeKillEvent(data->timerId); | |
1487 | ||
1488 | data->timerId = timeSetEvent(1, | |
1489 | data->period, | |
1490 | host_alarm_handler, | |
1491 | (DWORD)t, | |
1492 | TIME_ONESHOT | TIME_PERIODIC); | |
1493 | ||
1494 | if (!data->timerId) { | |
1495 | perror("Failed to re-arm win32 alarm timer"); | |
1496 | ||
1497 | timeEndPeriod(data->period); | |
efe75411 TS |
1498 | exit(1); |
1499 | } | |
1500 | } | |
1501 | ||
c8994013 TS |
1502 | #endif /* _WIN32 */ |
1503 | ||
7183b4b4 | 1504 | static int init_timer_alarm(void) |
8a7ddc38 | 1505 | { |
223f0d72 | 1506 | struct qemu_alarm_timer *t = NULL; |
c8994013 | 1507 | int i, err = -1; |
f49e58dc | 1508 | |
c8994013 TS |
1509 | for (i = 0; alarm_timers[i].name; i++) { |
1510 | t = &alarm_timers[i]; | |
1511 | ||
c8994013 TS |
1512 | err = t->start(t); |
1513 | if (!err) | |
1514 | break; | |
67b915a5 | 1515 | } |
fd872598 | 1516 | |
c8994013 | 1517 | if (err) { |
7183b4b4 AL |
1518 | err = -ENOENT; |
1519 | goto fail; | |
67b915a5 | 1520 | } |
c8994013 TS |
1521 | |
1522 | alarm_timer = t; | |
7183b4b4 | 1523 | |
6abfbd79 | 1524 | return 0; |
7183b4b4 AL |
1525 | |
1526 | fail: | |
7183b4b4 | 1527 | return err; |
8a7ddc38 FB |
1528 | } |
1529 | ||
9596ebb7 | 1530 | static void quit_timers(void) |
40c3bac3 | 1531 | { |
c8994013 TS |
1532 | alarm_timer->stop(alarm_timer); |
1533 | alarm_timer = NULL; | |
40c3bac3 FB |
1534 | } |
1535 | ||
f6503059 AZ |
1536 | /***********************************************************/ |
1537 | /* host time/date access */ | |
1538 | void qemu_get_timedate(struct tm *tm, int offset) | |
1539 | { | |
1540 | time_t ti; | |
1541 | struct tm *ret; | |
1542 | ||
1543 | time(&ti); | |
1544 | ti += offset; | |
1545 | if (rtc_date_offset == -1) { | |
1546 | if (rtc_utc) | |
1547 | ret = gmtime(&ti); | |
1548 | else | |
1549 | ret = localtime(&ti); | |
1550 | } else { | |
1551 | ti -= rtc_date_offset; | |
1552 | ret = gmtime(&ti); | |
1553 | } | |
1554 | ||
1555 | memcpy(tm, ret, sizeof(struct tm)); | |
1556 | } | |
1557 | ||
1558 | int qemu_timedate_diff(struct tm *tm) | |
1559 | { | |
1560 | time_t seconds; | |
1561 | ||
1562 | if (rtc_date_offset == -1) | |
1563 | if (rtc_utc) | |
1564 | seconds = mktimegm(tm); | |
1565 | else | |
1566 | seconds = mktime(tm); | |
1567 | else | |
1568 | seconds = mktimegm(tm) + rtc_date_offset; | |
1569 | ||
1570 | return seconds - time(NULL); | |
1571 | } | |
1572 | ||
fd1dff4b | 1573 | #ifdef _WIN32 |
fd1dff4b FB |
1574 | static void socket_cleanup(void) |
1575 | { | |
1576 | WSACleanup(); | |
1577 | } | |
82c643ff | 1578 | |
fd1dff4b FB |
1579 | static int socket_init(void) |
1580 | { | |
1581 | WSADATA Data; | |
1582 | int ret, err; | |
1583 | ||
1584 | ret = WSAStartup(MAKEWORD(2,2), &Data); | |
1585 | if (ret != 0) { | |
1586 | err = WSAGetLastError(); | |
1587 | fprintf(stderr, "WSAStartup: %d\n", err); | |
1588 | return -1; | |
1589 | } | |
1590 | atexit(socket_cleanup); | |
1591 | return 0; | |
1592 | } | |
64b7b733 AJ |
1593 | #endif |
1594 | ||
1ae26a18 AZ |
1595 | /***********************************************************/ |
1596 | /* Bluetooth support */ | |
1597 | static int nb_hcis; | |
1598 | static int cur_hci; | |
1599 | static struct HCIInfo *hci_table[MAX_NICS]; | |
dc72ac14 | 1600 | |
1ae26a18 AZ |
1601 | static struct bt_vlan_s { |
1602 | struct bt_scatternet_s net; | |
1603 | int id; | |
1604 | struct bt_vlan_s *next; | |
1605 | } *first_bt_vlan; | |
1606 | ||
1607 | /* find or alloc a new bluetooth "VLAN" */ | |
674bb261 | 1608 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
1ae26a18 AZ |
1609 | { |
1610 | struct bt_vlan_s **pvlan, *vlan; | |
1611 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { | |
1612 | if (vlan->id == id) | |
1613 | return &vlan->net; | |
1614 | } | |
1615 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); | |
1616 | vlan->id = id; | |
1617 | pvlan = &first_bt_vlan; | |
1618 | while (*pvlan != NULL) | |
1619 | pvlan = &(*pvlan)->next; | |
1620 | *pvlan = vlan; | |
1621 | return &vlan->net; | |
1622 | } | |
1623 | ||
1624 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) | |
1625 | { | |
1626 | } | |
1627 | ||
1628 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) | |
1629 | { | |
1630 | return -ENOTSUP; | |
1631 | } | |
1632 | ||
1633 | static struct HCIInfo null_hci = { | |
1634 | .cmd_send = null_hci_send, | |
1635 | .sco_send = null_hci_send, | |
1636 | .acl_send = null_hci_send, | |
1637 | .bdaddr_set = null_hci_addr_set, | |
1638 | }; | |
1639 | ||
1640 | struct HCIInfo *qemu_next_hci(void) | |
1641 | { | |
1642 | if (cur_hci == nb_hcis) | |
1643 | return &null_hci; | |
1644 | ||
1645 | return hci_table[cur_hci++]; | |
1646 | } | |
1647 | ||
dc72ac14 AZ |
1648 | static struct HCIInfo *hci_init(const char *str) |
1649 | { | |
1650 | char *endp; | |
1651 | struct bt_scatternet_s *vlan = 0; | |
1652 | ||
1653 | if (!strcmp(str, "null")) | |
1654 | /* null */ | |
1655 | return &null_hci; | |
1656 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) | |
1657 | /* host[:hciN] */ | |
1658 | return bt_host_hci(str[4] ? str + 5 : "hci0"); | |
1659 | else if (!strncmp(str, "hci", 3)) { | |
1660 | /* hci[,vlan=n] */ | |
1661 | if (str[3]) { | |
1662 | if (!strncmp(str + 3, ",vlan=", 6)) { | |
1663 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); | |
1664 | if (*endp) | |
1665 | vlan = 0; | |
1666 | } | |
1667 | } else | |
1668 | vlan = qemu_find_bt_vlan(0); | |
1669 | if (vlan) | |
1670 | return bt_new_hci(vlan); | |
1671 | } | |
1672 | ||
1673 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); | |
1674 | ||
1675 | return 0; | |
1676 | } | |
1677 | ||
1678 | static int bt_hci_parse(const char *str) | |
1679 | { | |
1680 | struct HCIInfo *hci; | |
1681 | bdaddr_t bdaddr; | |
1682 | ||
1683 | if (nb_hcis >= MAX_NICS) { | |
1684 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); | |
1685 | return -1; | |
1686 | } | |
1687 | ||
1688 | hci = hci_init(str); | |
1689 | if (!hci) | |
1690 | return -1; | |
1691 | ||
1692 | bdaddr.b[0] = 0x52; | |
1693 | bdaddr.b[1] = 0x54; | |
1694 | bdaddr.b[2] = 0x00; | |
1695 | bdaddr.b[3] = 0x12; | |
1696 | bdaddr.b[4] = 0x34; | |
1697 | bdaddr.b[5] = 0x56 + nb_hcis; | |
1698 | hci->bdaddr_set(hci, bdaddr.b); | |
1699 | ||
1700 | hci_table[nb_hcis++] = hci; | |
1701 | ||
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | static void bt_vhci_add(int vlan_id) | |
1706 | { | |
1707 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); | |
1708 | ||
1709 | if (!vlan->slave) | |
1710 | fprintf(stderr, "qemu: warning: adding a VHCI to " | |
1711 | "an empty scatternet %i\n", vlan_id); | |
1712 | ||
1713 | bt_vhci_init(bt_new_hci(vlan)); | |
1714 | } | |
1715 | ||
1716 | static struct bt_device_s *bt_device_add(const char *opt) | |
1717 | { | |
1718 | struct bt_scatternet_s *vlan; | |
1719 | int vlan_id = 0; | |
1720 | char *endp = strstr(opt, ",vlan="); | |
1721 | int len = (endp ? endp - opt : strlen(opt)) + 1; | |
1722 | char devname[10]; | |
1723 | ||
1724 | pstrcpy(devname, MIN(sizeof(devname), len), opt); | |
1725 | ||
1726 | if (endp) { | |
1727 | vlan_id = strtol(endp + 6, &endp, 0); | |
1728 | if (*endp) { | |
1729 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); | |
1730 | return 0; | |
1731 | } | |
1732 | } | |
1733 | ||
1734 | vlan = qemu_find_bt_vlan(vlan_id); | |
1735 | ||
1736 | if (!vlan->slave) | |
1737 | fprintf(stderr, "qemu: warning: adding a slave device to " | |
1738 | "an empty scatternet %i\n", vlan_id); | |
1739 | ||
1740 | if (!strcmp(devname, "keyboard")) | |
1741 | return bt_keyboard_init(vlan); | |
1742 | ||
1743 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); | |
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | static int bt_parse(const char *opt) | |
1748 | { | |
1749 | const char *endp, *p; | |
1750 | int vlan; | |
1751 | ||
1752 | if (strstart(opt, "hci", &endp)) { | |
1753 | if (!*endp || *endp == ',') { | |
1754 | if (*endp) | |
1755 | if (!strstart(endp, ",vlan=", 0)) | |
1756 | opt = endp + 1; | |
1757 | ||
1758 | return bt_hci_parse(opt); | |
1759 | } | |
1760 | } else if (strstart(opt, "vhci", &endp)) { | |
1761 | if (!*endp || *endp == ',') { | |
1762 | if (*endp) { | |
1763 | if (strstart(endp, ",vlan=", &p)) { | |
1764 | vlan = strtol(p, (char **) &endp, 0); | |
1765 | if (*endp) { | |
1766 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); | |
1767 | return 1; | |
1768 | } | |
1769 | } else { | |
1770 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); | |
1771 | return 1; | |
1772 | } | |
1773 | } else | |
1774 | vlan = 0; | |
1775 | ||
1776 | bt_vhci_add(vlan); | |
1777 | return 0; | |
1778 | } | |
1779 | } else if (strstart(opt, "device:", &endp)) | |
1780 | return !bt_device_add(endp); | |
1781 | ||
1782 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); | |
1783 | return 1; | |
1784 | } | |
1785 | ||
1ae26a18 AZ |
1786 | /***********************************************************/ |
1787 | /* QEMU Block devices */ | |
1788 | ||
609497ab | 1789 | #define HD_ALIAS "index=%d,media=disk" |
e4bcb14c | 1790 | #define CDROM_ALIAS "index=2,media=cdrom" |
e4bcb14c | 1791 | #define FD_ALIAS "index=%d,if=floppy" |
609497ab AZ |
1792 | #define PFLASH_ALIAS "if=pflash" |
1793 | #define MTD_ALIAS "if=mtd" | |
9d413d1d | 1794 | #define SD_ALIAS "index=0,if=sd" |
e4bcb14c | 1795 | |
9dfd7c7a | 1796 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
e4bcb14c TS |
1797 | { |
1798 | va_list ap; | |
9dfd7c7a GH |
1799 | char optstr[1024]; |
1800 | QemuOpts *opts; | |
e4bcb14c | 1801 | |
e4bcb14c | 1802 | va_start(ap, fmt); |
9dfd7c7a | 1803 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
e4bcb14c TS |
1804 | va_end(ap); |
1805 | ||
7282a033 | 1806 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
9dfd7c7a GH |
1807 | if (!opts) { |
1808 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", | |
1809 | __FUNCTION__, optstr); | |
1810 | return NULL; | |
1811 | } | |
1812 | if (file) | |
1813 | qemu_opt_set(opts, "file", file); | |
1814 | return opts; | |
b01b1111 AL |
1815 | } |
1816 | ||
751c6a17 | 1817 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
e4bcb14c | 1818 | { |
751c6a17 | 1819 | DriveInfo *dinfo; |
e4bcb14c TS |
1820 | |
1821 | /* seek interface, bus and unit */ | |
1822 | ||
751c6a17 GH |
1823 | TAILQ_FOREACH(dinfo, &drives, next) { |
1824 | if (dinfo->type == type && | |
1825 | dinfo->bus == bus && | |
1826 | dinfo->unit == unit) | |
1827 | return dinfo; | |
1828 | } | |
e4bcb14c | 1829 | |
751c6a17 | 1830 | return NULL; |
e4bcb14c TS |
1831 | } |
1832 | ||
2e810b36 | 1833 | DriveInfo *drive_get_by_id(const char *id) |
1dae12e6 GH |
1834 | { |
1835 | DriveInfo *dinfo; | |
1836 | ||
1837 | TAILQ_FOREACH(dinfo, &drives, next) { | |
1838 | if (strcmp(id, dinfo->id)) | |
1839 | continue; | |
1840 | return dinfo; | |
1841 | } | |
1842 | return NULL; | |
1843 | } | |
1844 | ||
f60d39bc | 1845 | int drive_get_max_bus(BlockInterfaceType type) |
e4bcb14c TS |
1846 | { |
1847 | int max_bus; | |
751c6a17 | 1848 | DriveInfo *dinfo; |
e4bcb14c TS |
1849 | |
1850 | max_bus = -1; | |
751c6a17 GH |
1851 | TAILQ_FOREACH(dinfo, &drives, next) { |
1852 | if(dinfo->type == type && | |
1853 | dinfo->bus > max_bus) | |
1854 | max_bus = dinfo->bus; | |
e4bcb14c TS |
1855 | } |
1856 | return max_bus; | |
1857 | } | |
1858 | ||
fa879c64 AL |
1859 | const char *drive_get_serial(BlockDriverState *bdrv) |
1860 | { | |
751c6a17 | 1861 | DriveInfo *dinfo; |
fa879c64 | 1862 | |
751c6a17 GH |
1863 | TAILQ_FOREACH(dinfo, &drives, next) { |
1864 | if (dinfo->bdrv == bdrv) | |
1865 | return dinfo->serial; | |
1866 | } | |
fa879c64 AL |
1867 | |
1868 | return "\0"; | |
1869 | } | |
1870 | ||
428c5705 AL |
1871 | BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
1872 | { | |
751c6a17 | 1873 | DriveInfo *dinfo; |
428c5705 | 1874 | |
751c6a17 GH |
1875 | TAILQ_FOREACH(dinfo, &drives, next) { |
1876 | if (dinfo->bdrv == bdrv) | |
1877 | return dinfo->onerror; | |
1878 | } | |
428c5705 | 1879 | |
cdad4bd8 | 1880 | return BLOCK_ERR_STOP_ENOSPC; |
428c5705 AL |
1881 | } |
1882 | ||
a1620fac AJ |
1883 | static void bdrv_format_print(void *opaque, const char *name) |
1884 | { | |
1885 | fprintf(stderr, " %s", name); | |
1886 | } | |
1887 | ||
b01b1111 AL |
1888 | void drive_uninit(BlockDriverState *bdrv) |
1889 | { | |
751c6a17 | 1890 | DriveInfo *dinfo; |
b01b1111 | 1891 | |
751c6a17 GH |
1892 | TAILQ_FOREACH(dinfo, &drives, next) { |
1893 | if (dinfo->bdrv != bdrv) | |
1894 | continue; | |
9dfd7c7a | 1895 | qemu_opts_del(dinfo->opts); |
751c6a17 GH |
1896 | TAILQ_REMOVE(&drives, dinfo, next); |
1897 | qemu_free(dinfo); | |
1898 | break; | |
1899 | } | |
b01b1111 AL |
1900 | } |
1901 | ||
9dfd7c7a | 1902 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
751c6a17 | 1903 | int *fatal_error) |
e4bcb14c | 1904 | { |
9dfd7c7a GH |
1905 | const char *buf; |
1906 | const char *file = NULL; | |
c8522bdf | 1907 | char devname[128]; |
9dfd7c7a | 1908 | const char *serial; |
c8522bdf | 1909 | const char *mediastr = ""; |
f60d39bc | 1910 | BlockInterfaceType type; |
e4bcb14c TS |
1911 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
1912 | int bus_id, unit_id; | |
1913 | int cyls, heads, secs, translation; | |
1e72d3b7 | 1914 | BlockDriver *drv = NULL; |
4d73cd3b | 1915 | QEMUMachine *machine = opaque; |
e4bcb14c TS |
1916 | int max_devs; |
1917 | int index; | |
33f00271 | 1918 | int cache; |
5c6c3a6c | 1919 | int aio = 0; |
428c5705 | 1920 | int bdrv_flags, onerror; |
c2cc47a4 | 1921 | const char *devaddr; |
751c6a17 | 1922 | DriveInfo *dinfo; |
9dfd7c7a | 1923 | int snapshot = 0; |
e4bcb14c | 1924 | |
9dfd7c7a | 1925 | *fatal_error = 1; |
e4bcb14c | 1926 | |
e4bcb14c | 1927 | translation = BIOS_ATA_TRANSLATION_AUTO; |
0aa217e4 | 1928 | cache = 1; |
e4bcb14c | 1929 | |
4d007814 | 1930 | if (machine && machine->use_scsi) { |
f60d39bc | 1931 | type = IF_SCSI; |
e4bcb14c | 1932 | max_devs = MAX_SCSI_DEVS; |
363a37d5 | 1933 | pstrcpy(devname, sizeof(devname), "scsi"); |
e4bcb14c | 1934 | } else { |
f60d39bc | 1935 | type = IF_IDE; |
e4bcb14c | 1936 | max_devs = MAX_IDE_DEVS; |
363a37d5 | 1937 | pstrcpy(devname, sizeof(devname), "ide"); |
e4bcb14c TS |
1938 | } |
1939 | media = MEDIA_DISK; | |
1940 | ||
1941 | /* extract parameters */ | |
9dfd7c7a GH |
1942 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
1943 | unit_id = qemu_opt_get_number(opts, "unit", -1); | |
1944 | index = qemu_opt_get_number(opts, "index", -1); | |
e4bcb14c | 1945 | |
9dfd7c7a GH |
1946 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
1947 | heads = qemu_opt_get_number(opts, "heads", 0); | |
1948 | secs = qemu_opt_get_number(opts, "secs", 0); | |
e4bcb14c | 1949 | |
9dfd7c7a | 1950 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
e4bcb14c | 1951 | |
9dfd7c7a GH |
1952 | file = qemu_opt_get(opts, "file"); |
1953 | serial = qemu_opt_get(opts, "serial"); | |
1954 | ||
1955 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { | |
ae45d369 | 1956 | pstrcpy(devname, sizeof(devname), buf); |
e4bcb14c | 1957 | if (!strcmp(buf, "ide")) { |
f60d39bc | 1958 | type = IF_IDE; |
e4bcb14c TS |
1959 | max_devs = MAX_IDE_DEVS; |
1960 | } else if (!strcmp(buf, "scsi")) { | |
f60d39bc | 1961 | type = IF_SCSI; |
e4bcb14c TS |
1962 | max_devs = MAX_SCSI_DEVS; |
1963 | } else if (!strcmp(buf, "floppy")) { | |
f60d39bc | 1964 | type = IF_FLOPPY; |
e4bcb14c TS |
1965 | max_devs = 0; |
1966 | } else if (!strcmp(buf, "pflash")) { | |
f60d39bc | 1967 | type = IF_PFLASH; |
e4bcb14c TS |
1968 | max_devs = 0; |
1969 | } else if (!strcmp(buf, "mtd")) { | |
f60d39bc | 1970 | type = IF_MTD; |
e4bcb14c TS |
1971 | max_devs = 0; |
1972 | } else if (!strcmp(buf, "sd")) { | |
f60d39bc | 1973 | type = IF_SD; |
e4bcb14c | 1974 | max_devs = 0; |
6e02c38d AL |
1975 | } else if (!strcmp(buf, "virtio")) { |
1976 | type = IF_VIRTIO; | |
1977 | max_devs = 0; | |
62d23efa AL |
1978 | } else if (!strcmp(buf, "xen")) { |
1979 | type = IF_XEN; | |
1980 | max_devs = 0; | |
a8659e90 GH |
1981 | } else if (!strcmp(buf, "none")) { |
1982 | type = IF_NONE; | |
1983 | max_devs = 0; | |
62d23efa | 1984 | } else { |
9dfd7c7a | 1985 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
751c6a17 | 1986 | return NULL; |
e4bcb14c TS |
1987 | } |
1988 | } | |
1989 | ||
e4bcb14c TS |
1990 | if (cyls || heads || secs) { |
1991 | if (cyls < 1 || cyls > 16383) { | |
9dfd7c7a | 1992 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
751c6a17 | 1993 | return NULL; |
e4bcb14c TS |
1994 | } |
1995 | if (heads < 1 || heads > 16) { | |
9dfd7c7a | 1996 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
751c6a17 | 1997 | return NULL; |
e4bcb14c TS |
1998 | } |
1999 | if (secs < 1 || secs > 63) { | |
9dfd7c7a | 2000 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
751c6a17 | 2001 | return NULL; |
e4bcb14c TS |
2002 | } |
2003 | } | |
2004 | ||
9dfd7c7a | 2005 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
e4bcb14c TS |
2006 | if (!cyls) { |
2007 | fprintf(stderr, | |
2008 | "qemu: '%s' trans must be used with cyls,heads and secs\n", | |
9dfd7c7a | 2009 | buf); |
751c6a17 | 2010 | return NULL; |
e4bcb14c TS |
2011 | } |
2012 | if (!strcmp(buf, "none")) | |
2013 | translation = BIOS_ATA_TRANSLATION_NONE; | |
2014 | else if (!strcmp(buf, "lba")) | |
2015 | translation = BIOS_ATA_TRANSLATION_LBA; | |
2016 | else if (!strcmp(buf, "auto")) | |
2017 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
2018 | else { | |
9dfd7c7a | 2019 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
751c6a17 | 2020 | return NULL; |
e4bcb14c TS |
2021 | } |
2022 | } | |
2023 | ||
9dfd7c7a | 2024 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
e4bcb14c TS |
2025 | if (!strcmp(buf, "disk")) { |
2026 | media = MEDIA_DISK; | |
2027 | } else if (!strcmp(buf, "cdrom")) { | |
2028 | if (cyls || secs || heads) { | |
2029 | fprintf(stderr, | |
9dfd7c7a | 2030 | "qemu: '%s' invalid physical CHS format\n", buf); |
751c6a17 | 2031 | return NULL; |
e4bcb14c TS |
2032 | } |
2033 | media = MEDIA_CDROM; | |
2034 | } else { | |
9dfd7c7a | 2035 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
751c6a17 | 2036 | return NULL; |
e4bcb14c TS |
2037 | } |
2038 | } | |
2039 | ||
9dfd7c7a | 2040 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
9f7965c7 | 2041 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
33f00271 | 2042 | cache = 0; |
9f7965c7 | 2043 | else if (!strcmp(buf, "writethrough")) |
33f00271 | 2044 | cache = 1; |
9f7965c7 AL |
2045 | else if (!strcmp(buf, "writeback")) |
2046 | cache = 2; | |
33f00271 AZ |
2047 | else { |
2048 | fprintf(stderr, "qemu: invalid cache option\n"); | |
751c6a17 | 2049 | return NULL; |
33f00271 AZ |
2050 | } |
2051 | } | |
2052 | ||
5c6c3a6c CH |
2053 | #ifdef CONFIG_LINUX_AIO |
2054 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { | |
2055 | if (!strcmp(buf, "threads")) | |
2056 | aio = 0; | |
2057 | else if (!strcmp(buf, "native")) | |
2058 | aio = 1; | |
2059 | else { | |
2060 | fprintf(stderr, "qemu: invalid aio option\n"); | |
2061 | return NULL; | |
2062 | } | |
2063 | } | |
2064 | #endif | |
2065 | ||
9dfd7c7a | 2066 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
a1620fac AJ |
2067 | if (strcmp(buf, "?") == 0) { |
2068 | fprintf(stderr, "qemu: Supported formats:"); | |
2069 | bdrv_iterate_format(bdrv_format_print, NULL); | |
2070 | fprintf(stderr, "\n"); | |
751c6a17 | 2071 | return NULL; |
a1620fac | 2072 | } |
1e72d3b7 AJ |
2073 | drv = bdrv_find_format(buf); |
2074 | if (!drv) { | |
2075 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); | |
751c6a17 | 2076 | return NULL; |
1e72d3b7 AJ |
2077 | } |
2078 | } | |
2079 | ||
cdad4bd8 | 2080 | onerror = BLOCK_ERR_STOP_ENOSPC; |
9dfd7c7a | 2081 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
869a5c6d | 2082 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
ea8a5d7f | 2083 | fprintf(stderr, "werror is no supported by this format\n"); |
751c6a17 | 2084 | return NULL; |
428c5705 AL |
2085 | } |
2086 | if (!strcmp(buf, "ignore")) | |
2087 | onerror = BLOCK_ERR_IGNORE; | |
2088 | else if (!strcmp(buf, "enospc")) | |
2089 | onerror = BLOCK_ERR_STOP_ENOSPC; | |
2090 | else if (!strcmp(buf, "stop")) | |
2091 | onerror = BLOCK_ERR_STOP_ANY; | |
2092 | else if (!strcmp(buf, "report")) | |
2093 | onerror = BLOCK_ERR_REPORT; | |
2094 | else { | |
2095 | fprintf(stderr, "qemu: '%s' invalid write error action\n", buf); | |
751c6a17 | 2096 | return NULL; |
428c5705 AL |
2097 | } |
2098 | } | |
2099 | ||
9dfd7c7a | 2100 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
c2cc47a4 | 2101 | if (type != IF_VIRTIO) { |
9dfd7c7a | 2102 | fprintf(stderr, "addr is not supported\n"); |
751c6a17 | 2103 | return NULL; |
c2cc47a4 | 2104 | } |
c2cc47a4 MA |
2105 | } |
2106 | ||
e4bcb14c TS |
2107 | /* compute bus and unit according index */ |
2108 | ||
2109 | if (index != -1) { | |
2110 | if (bus_id != 0 || unit_id != -1) { | |
2111 | fprintf(stderr, | |
9dfd7c7a | 2112 | "qemu: index cannot be used with bus and unit\n"); |
751c6a17 | 2113 | return NULL; |
e4bcb14c TS |
2114 | } |
2115 | if (max_devs == 0) | |
2116 | { | |
2117 | unit_id = index; | |
2118 | bus_id = 0; | |
2119 | } else { | |
2120 | unit_id = index % max_devs; | |
2121 | bus_id = index / max_devs; | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | /* if user doesn't specify a unit_id, | |
2126 | * try to find the first free | |
2127 | */ | |
2128 | ||
2129 | if (unit_id == -1) { | |
2130 | unit_id = 0; | |
751c6a17 | 2131 | while (drive_get(type, bus_id, unit_id) != NULL) { |
e4bcb14c TS |
2132 | unit_id++; |
2133 | if (max_devs && unit_id >= max_devs) { | |
2134 | unit_id -= max_devs; | |
2135 | bus_id++; | |
2136 | } | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | /* check unit id */ | |
2141 | ||
2142 | if (max_devs && unit_id >= max_devs) { | |
9dfd7c7a GH |
2143 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
2144 | unit_id, max_devs - 1); | |
751c6a17 | 2145 | return NULL; |
e4bcb14c TS |
2146 | } |
2147 | ||
2148 | /* | |
2149 | * ignore multiple definitions | |
2150 | */ | |
2151 | ||
751c6a17 GH |
2152 | if (drive_get(type, bus_id, unit_id) != NULL) { |
2153 | *fatal_error = 0; | |
2154 | return NULL; | |
2155 | } | |
e4bcb14c TS |
2156 | |
2157 | /* init */ | |
2158 | ||
751c6a17 | 2159 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
e23d9c4d | 2160 | if ((buf = qemu_opts_id(opts)) != NULL) { |
9dfd7c7a GH |
2161 | dinfo->id = qemu_strdup(buf); |
2162 | } else { | |
1dae12e6 | 2163 | /* no id supplied -> create one */ |
9dfd7c7a | 2164 | dinfo->id = qemu_mallocz(32); |
1dae12e6 GH |
2165 | if (type == IF_IDE || type == IF_SCSI) |
2166 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
2167 | if (max_devs) | |
9dfd7c7a | 2168 | snprintf(dinfo->id, 32, "%s%i%s%i", |
1dae12e6 GH |
2169 | devname, bus_id, mediastr, unit_id); |
2170 | else | |
9dfd7c7a | 2171 | snprintf(dinfo->id, 32, "%s%s%i", |
1dae12e6 GH |
2172 | devname, mediastr, unit_id); |
2173 | } | |
1dae12e6 | 2174 | dinfo->bdrv = bdrv_new(dinfo->id); |
751c6a17 GH |
2175 | dinfo->devaddr = devaddr; |
2176 | dinfo->type = type; | |
2177 | dinfo->bus = bus_id; | |
2178 | dinfo->unit = unit_id; | |
2179 | dinfo->onerror = onerror; | |
9dfd7c7a GH |
2180 | dinfo->opts = opts; |
2181 | if (serial) | |
2182 | strncpy(dinfo->serial, serial, sizeof(serial)); | |
751c6a17 | 2183 | TAILQ_INSERT_TAIL(&drives, dinfo, next); |
e4bcb14c | 2184 | |
f60d39bc | 2185 | switch(type) { |
e4bcb14c TS |
2186 | case IF_IDE: |
2187 | case IF_SCSI: | |
62d23efa | 2188 | case IF_XEN: |
e4bcb14c TS |
2189 | switch(media) { |
2190 | case MEDIA_DISK: | |
2191 | if (cyls != 0) { | |
1dae12e6 GH |
2192 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
2193 | bdrv_set_translation_hint(dinfo->bdrv, translation); | |
e4bcb14c TS |
2194 | } |
2195 | break; | |
2196 | case MEDIA_CDROM: | |
1dae12e6 | 2197 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
e4bcb14c TS |
2198 | break; |
2199 | } | |
2200 | break; | |
2201 | case IF_SD: | |
2202 | /* FIXME: This isn't really a floppy, but it's a reasonable | |
2203 | approximation. */ | |
2204 | case IF_FLOPPY: | |
1dae12e6 | 2205 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
e4bcb14c TS |
2206 | break; |
2207 | case IF_PFLASH: | |
2208 | case IF_MTD: | |
a8659e90 | 2209 | case IF_NONE: |
e4bcb14c | 2210 | break; |
d176c495 GH |
2211 | case IF_VIRTIO: |
2212 | /* add virtio block device */ | |
2213 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); | |
2214 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); | |
2215 | qemu_opt_set(opts, "drive", dinfo->id); | |
2216 | if (devaddr) | |
2217 | qemu_opt_set(opts, "addr", devaddr); | |
2218 | break; | |
aae9460e PB |
2219 | case IF_COUNT: |
2220 | abort(); | |
e4bcb14c | 2221 | } |
9dfd7c7a | 2222 | if (!file) { |
751c6a17 GH |
2223 | *fatal_error = 0; |
2224 | return NULL; | |
2225 | } | |
33f00271 | 2226 | bdrv_flags = 0; |
9f7965c7 | 2227 | if (snapshot) { |
33f00271 | 2228 | bdrv_flags |= BDRV_O_SNAPSHOT; |
9f7965c7 AL |
2229 | cache = 2; /* always use write-back with snapshot */ |
2230 | } | |
2231 | if (cache == 0) /* no caching */ | |
2232 | bdrv_flags |= BDRV_O_NOCACHE; | |
2233 | else if (cache == 2) /* write-back */ | |
2234 | bdrv_flags |= BDRV_O_CACHE_WB; | |
5c6c3a6c CH |
2235 | |
2236 | if (aio == 1) { | |
2237 | bdrv_flags |= BDRV_O_NATIVE_AIO; | |
2238 | } else { | |
2239 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; | |
2240 | } | |
2241 | ||
1dae12e6 | 2242 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
e4bcb14c TS |
2243 | fprintf(stderr, "qemu: could not open disk image %s\n", |
2244 | file); | |
751c6a17 | 2245 | return NULL; |
e4bcb14c | 2246 | } |
5c6c3a6c | 2247 | |
1dae12e6 | 2248 | if (bdrv_key_required(dinfo->bdrv)) |
c0f4ce77 | 2249 | autostart = 0; |
751c6a17 GH |
2250 | *fatal_error = 0; |
2251 | return dinfo; | |
e4bcb14c TS |
2252 | } |
2253 | ||
9dfd7c7a GH |
2254 | static int drive_init_func(QemuOpts *opts, void *opaque) |
2255 | { | |
2256 | QEMUMachine *machine = opaque; | |
2257 | int fatal_error = 0; | |
2258 | ||
2259 | if (drive_init(opts, machine, &fatal_error) == NULL) { | |
2260 | if (fatal_error) | |
2261 | return 1; | |
2262 | } | |
2263 | return 0; | |
2264 | } | |
2265 | ||
2266 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) | |
2267 | { | |
2268 | if (NULL == qemu_opt_get(opts, "snapshot")) { | |
2269 | qemu_opt_set(opts, "snapshot", "on"); | |
2270 | } | |
2271 | return 0; | |
2272 | } | |
2273 | ||
76e30d0f JK |
2274 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
2275 | { | |
2276 | boot_set_handler = func; | |
2277 | boot_set_opaque = opaque; | |
2278 | } | |
2279 | ||
2280 | int qemu_boot_set(const char *boot_devices) | |
2281 | { | |
2282 | if (!boot_set_handler) { | |
2283 | return -EINVAL; | |
2284 | } | |
2285 | return boot_set_handler(boot_set_opaque, boot_devices); | |
2286 | } | |
2287 | ||
ef3adf68 JK |
2288 | static int parse_bootdevices(char *devices) |
2289 | { | |
2290 | /* We just do some generic consistency checks */ | |
2291 | const char *p; | |
2292 | int bitmap = 0; | |
2293 | ||
2294 | for (p = devices; *p != '\0'; p++) { | |
2295 | /* Allowed boot devices are: | |
2296 | * a-b: floppy disk drives | |
2297 | * c-f: IDE disk drives | |
2298 | * g-m: machine implementation dependant drives | |
2299 | * n-p: network devices | |
2300 | * It's up to each machine implementation to check if the given boot | |
2301 | * devices match the actual hardware implementation and firmware | |
2302 | * features. | |
2303 | */ | |
2304 | if (*p < 'a' || *p > 'p') { | |
2305 | fprintf(stderr, "Invalid boot device '%c'\n", *p); | |
2306 | exit(1); | |
2307 | } | |
2308 | if (bitmap & (1 << (*p - 'a'))) { | |
2309 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); | |
2310 | exit(1); | |
2311 | } | |
2312 | bitmap |= 1 << (*p - 'a'); | |
2313 | } | |
2314 | return bitmap; | |
2315 | } | |
2316 | ||
e0f084bf JK |
2317 | static void restore_boot_devices(void *opaque) |
2318 | { | |
2319 | char *standard_boot_devices = opaque; | |
2320 | ||
2321 | qemu_boot_set(standard_boot_devices); | |
2322 | ||
2323 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); | |
2324 | qemu_free(standard_boot_devices); | |
2325 | } | |
2326 | ||
268a362c AL |
2327 | static void numa_add(const char *optarg) |
2328 | { | |
2329 | char option[128]; | |
2330 | char *endptr; | |
2331 | unsigned long long value, endvalue; | |
2332 | int nodenr; | |
2333 | ||
2334 | optarg = get_opt_name(option, 128, optarg, ',') + 1; | |
2335 | if (!strcmp(option, "node")) { | |
2336 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { | |
2337 | nodenr = nb_numa_nodes; | |
2338 | } else { | |
2339 | nodenr = strtoull(option, NULL, 10); | |
2340 | } | |
2341 | ||
2342 | if (get_param_value(option, 128, "mem", optarg) == 0) { | |
2343 | node_mem[nodenr] = 0; | |
2344 | } else { | |
2345 | value = strtoull(option, &endptr, 0); | |
2346 | switch (*endptr) { | |
2347 | case 0: case 'M': case 'm': | |
2348 | value <<= 20; | |
2349 | break; | |
2350 | case 'G': case 'g': | |
2351 | value <<= 30; | |
2352 | break; | |
2353 | } | |
2354 | node_mem[nodenr] = value; | |
2355 | } | |
2356 | if (get_param_value(option, 128, "cpus", optarg) == 0) { | |
2357 | node_cpumask[nodenr] = 0; | |
2358 | } else { | |
2359 | value = strtoull(option, &endptr, 10); | |
2360 | if (value >= 64) { | |
2361 | value = 63; | |
2362 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); | |
2363 | } else { | |
2364 | if (*endptr == '-') { | |
2365 | endvalue = strtoull(endptr+1, &endptr, 10); | |
2366 | if (endvalue >= 63) { | |
2367 | endvalue = 62; | |
2368 | fprintf(stderr, | |
2369 | "only 63 CPUs in NUMA mode supported.\n"); | |
2370 | } | |
2371 | value = (1 << (endvalue + 1)) - (1 << value); | |
2372 | } else { | |
2373 | value = 1 << value; | |
2374 | } | |
2375 | } | |
2376 | node_cpumask[nodenr] = value; | |
2377 | } | |
2378 | nb_numa_nodes++; | |
2379 | } | |
2380 | return; | |
2381 | } | |
2382 | ||
dc6b1c09 AP |
2383 | static void smp_parse(const char *optarg) |
2384 | { | |
2385 | int smp, sockets = 0, threads = 0, cores = 0; | |
2386 | char *endptr; | |
2387 | char option[128]; | |
2388 | ||
2389 | smp = strtoul(optarg, &endptr, 10); | |
2390 | if (endptr != optarg) { | |
2391 | if (*endptr == ',') { | |
2392 | endptr++; | |
2393 | } | |
2394 | } | |
2395 | if (get_param_value(option, 128, "sockets", endptr) != 0) | |
2396 | sockets = strtoull(option, NULL, 10); | |
2397 | if (get_param_value(option, 128, "cores", endptr) != 0) | |
2398 | cores = strtoull(option, NULL, 10); | |
2399 | if (get_param_value(option, 128, "threads", endptr) != 0) | |
2400 | threads = strtoull(option, NULL, 10); | |
2401 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) | |
2402 | max_cpus = strtoull(option, NULL, 10); | |
2403 | ||
2404 | /* compute missing values, prefer sockets over cores over threads */ | |
2405 | if (smp == 0 || sockets == 0) { | |
2406 | sockets = sockets > 0 ? sockets : 1; | |
2407 | cores = cores > 0 ? cores : 1; | |
2408 | threads = threads > 0 ? threads : 1; | |
2409 | if (smp == 0) { | |
2410 | smp = cores * threads * sockets; | |
2411 | } else { | |
2412 | sockets = smp / (cores * threads); | |
2413 | } | |
2414 | } else { | |
2415 | if (cores == 0) { | |
2416 | threads = threads > 0 ? threads : 1; | |
2417 | cores = smp / (sockets * threads); | |
2418 | } else { | |
2419 | if (sockets == 0) { | |
2420 | sockets = smp / (cores * threads); | |
2421 | } else { | |
2422 | threads = smp / (cores * sockets); | |
2423 | } | |
2424 | } | |
2425 | } | |
2426 | smp_cpus = smp; | |
2427 | smp_cores = cores > 0 ? cores : 1; | |
2428 | smp_threads = threads > 0 ? threads : 1; | |
2429 | if (max_cpus == 0) | |
2430 | max_cpus = smp_cpus; | |
2431 | } | |
2432 | ||
a594cfbf FB |
2433 | /***********************************************************/ |
2434 | /* USB devices */ | |
2435 | ||
bb5fc20f AL |
2436 | static void usb_msd_password_cb(void *opaque, int err) |
2437 | { | |
2438 | USBDevice *dev = opaque; | |
2439 | ||
2440 | if (!err) | |
a5d2f727 | 2441 | usb_device_attach(dev); |
bb5fc20f | 2442 | else |
806b6024 | 2443 | dev->info->handle_destroy(dev); |
bb5fc20f AL |
2444 | } |
2445 | ||
a5d2f727 GH |
2446 | static struct { |
2447 | const char *name; | |
2448 | const char *qdev; | |
2449 | } usbdevs[] = { | |
2450 | { | |
2451 | .name = "mouse", | |
2452 | .qdev = "QEMU USB Mouse", | |
2453 | },{ | |
2454 | .name = "tablet", | |
2455 | .qdev = "QEMU USB Tablet", | |
2456 | },{ | |
2457 | .name = "keyboard", | |
2458 | .qdev = "QEMU USB Keyboard", | |
2459 | },{ | |
2460 | .name = "wacom-tablet", | |
2461 | .qdev = "QEMU PenPartner Tablet", | |
2462 | } | |
2463 | }; | |
2464 | ||
c0f4ce77 | 2465 | static int usb_device_add(const char *devname, int is_hotplug) |
a594cfbf FB |
2466 | { |
2467 | const char *p; | |
a5d2f727 GH |
2468 | USBBus *bus = usb_bus_find(-1 /* any */); |
2469 | USBDevice *dev = NULL; | |
2470 | int i; | |
a594cfbf | 2471 | |
a5d2f727 | 2472 | if (!usb_enabled) |
a594cfbf FB |
2473 | return -1; |
2474 | ||
a5d2f727 GH |
2475 | /* simple devices which don't need extra care */ |
2476 | for (i = 0; i < ARRAY_SIZE(usbdevs); i++) { | |
2477 | if (strcmp(devname, usbdevs[i].name) != 0) | |
2478 | continue; | |
2479 | dev = usb_create_simple(bus, usbdevs[i].qdev); | |
2480 | goto done; | |
2481 | } | |
2482 | ||
2483 | /* the other ones */ | |
a594cfbf FB |
2484 | if (strstart(devname, "host:", &p)) { |
2485 | dev = usb_host_device_open(p); | |
2e5d83bb | 2486 | } else if (strstart(devname, "disk:", &p)) { |
c0f4ce77 AL |
2487 | BlockDriverState *bs; |
2488 | ||
bb5fc20f | 2489 | dev = usb_msd_init(p); |
c0f4ce77 AL |
2490 | if (!dev) |
2491 | return -1; | |
bb5fc20f | 2492 | bs = usb_msd_get_bdrv(dev); |
c0f4ce77 AL |
2493 | if (bdrv_key_required(bs)) { |
2494 | autostart = 0; | |
bb5fc20f | 2495 | if (is_hotplug) { |
376253ec AL |
2496 | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, |
2497 | dev); | |
bb5fc20f | 2498 | return 0; |
c0f4ce77 AL |
2499 | } |
2500 | } | |
a7954218 AZ |
2501 | } else if (strstart(devname, "serial:", &p)) { |
2502 | dev = usb_serial_init(p); | |
2e4d9fb1 AJ |
2503 | #ifdef CONFIG_BRLAPI |
2504 | } else if (!strcmp(devname, "braille")) { | |
2505 | dev = usb_baum_init(); | |
2506 | #endif | |
6c9f886c | 2507 | } else if (strstart(devname, "net:", &p)) { |
9ad97e65 | 2508 | int nic = nb_nics; |
6c9f886c | 2509 | |
10ae5a7a | 2510 | if (net_client_init(NULL, "nic", p) < 0) |
6c9f886c | 2511 | return -1; |
9ad97e65 AZ |
2512 | nd_table[nic].model = "usb"; |
2513 | dev = usb_net_init(&nd_table[nic]); | |
dc72ac14 AZ |
2514 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
2515 | dev = usb_bt_init(devname[2] ? hci_init(p) : | |
2516 | bt_new_hci(qemu_find_bt_vlan(0))); | |
a594cfbf FB |
2517 | } else { |
2518 | return -1; | |
2519 | } | |
0d92ed30 PB |
2520 | if (!dev) |
2521 | return -1; | |
2522 | ||
a5d2f727 | 2523 | done: |
a594cfbf FB |
2524 | return 0; |
2525 | } | |
2526 | ||
1f3870ab AL |
2527 | static int usb_device_del(const char *devname) |
2528 | { | |
2529 | int bus_num, addr; | |
2530 | const char *p; | |
2531 | ||
5d0c5750 AL |
2532 | if (strstart(devname, "host:", &p)) |
2533 | return usb_host_device_close(p); | |
2534 | ||
a5d2f727 | 2535 | if (!usb_enabled) |
1f3870ab AL |
2536 | return -1; |
2537 | ||
2538 | p = strchr(devname, '.'); | |
2539 | if (!p) | |
2540 | return -1; | |
2541 | bus_num = strtoul(devname, NULL, 0); | |
2542 | addr = strtoul(p + 1, NULL, 0); | |
2543 | ||
a5d2f727 | 2544 | return usb_device_delete_addr(bus_num, addr); |
1f3870ab AL |
2545 | } |
2546 | ||
bd3c948d GH |
2547 | static int usb_parse(const char *cmdline) |
2548 | { | |
2549 | return usb_device_add(cmdline, 0); | |
2550 | } | |
2551 | ||
d54908a5 | 2552 | void do_usb_add(Monitor *mon, const QDict *qdict) |
a594cfbf | 2553 | { |
d54908a5 | 2554 | usb_device_add(qdict_get_str(qdict, "devname"), 1); |
a594cfbf FB |
2555 | } |
2556 | ||
d54908a5 | 2557 | void do_usb_del(Monitor *mon, const QDict *qdict) |
a594cfbf | 2558 | { |
d54908a5 | 2559 | usb_device_del(qdict_get_str(qdict, "devname")); |
a594cfbf FB |
2560 | } |
2561 | ||
201a51fc AZ |
2562 | /***********************************************************/ |
2563 | /* PCMCIA/Cardbus */ | |
2564 | ||
2565 | static struct pcmcia_socket_entry_s { | |
bc24a225 | 2566 | PCMCIASocket *socket; |
201a51fc AZ |
2567 | struct pcmcia_socket_entry_s *next; |
2568 | } *pcmcia_sockets = 0; | |
2569 | ||
bc24a225 | 2570 | void pcmcia_socket_register(PCMCIASocket *socket) |
201a51fc AZ |
2571 | { |
2572 | struct pcmcia_socket_entry_s *entry; | |
2573 | ||
2574 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); | |
2575 | entry->socket = socket; | |
2576 | entry->next = pcmcia_sockets; | |
2577 | pcmcia_sockets = entry; | |
2578 | } | |
2579 | ||
bc24a225 | 2580 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
201a51fc AZ |
2581 | { |
2582 | struct pcmcia_socket_entry_s *entry, **ptr; | |
2583 | ||
2584 | ptr = &pcmcia_sockets; | |
2585 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) | |
2586 | if (entry->socket == socket) { | |
2587 | *ptr = entry->next; | |
2588 | qemu_free(entry); | |
2589 | } | |
2590 | } | |
2591 | ||
376253ec | 2592 | void pcmcia_info(Monitor *mon) |
201a51fc AZ |
2593 | { |
2594 | struct pcmcia_socket_entry_s *iter; | |
376253ec | 2595 | |
201a51fc | 2596 | if (!pcmcia_sockets) |
376253ec | 2597 | monitor_printf(mon, "No PCMCIA sockets\n"); |
201a51fc AZ |
2598 | |
2599 | for (iter = pcmcia_sockets; iter; iter = iter->next) | |
376253ec AL |
2600 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
2601 | iter->socket->attached ? iter->socket->card_string : | |
2602 | "Empty"); | |
201a51fc AZ |
2603 | } |
2604 | ||
2ff89790 | 2605 | /***********************************************************/ |
3023f332 AL |
2606 | /* register display */ |
2607 | ||
7b5d76da AL |
2608 | struct DisplayAllocator default_allocator = { |
2609 | defaultallocator_create_displaysurface, | |
2610 | defaultallocator_resize_displaysurface, | |
2611 | defaultallocator_free_displaysurface | |
2612 | }; | |
2613 | ||
3023f332 AL |
2614 | void register_displaystate(DisplayState *ds) |
2615 | { | |
2616 | DisplayState **s; | |
2617 | s = &display_state; | |
2618 | while (*s != NULL) | |
2619 | s = &(*s)->next; | |
2620 | ds->next = NULL; | |
2621 | *s = ds; | |
2622 | } | |
2623 | ||
2624 | DisplayState *get_displaystate(void) | |
2625 | { | |
2626 | return display_state; | |
2627 | } | |
2628 | ||
7b5d76da AL |
2629 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
2630 | { | |
2631 | if(ds->allocator == &default_allocator) ds->allocator = da; | |
2632 | return ds->allocator; | |
2633 | } | |
2634 | ||
2ff89790 TS |
2635 | /* dumb display */ |
2636 | ||
8f391ab4 | 2637 | static void dumb_display_init(void) |
2ff89790 | 2638 | { |
8f391ab4 | 2639 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
7b5d76da AL |
2640 | ds->allocator = &default_allocator; |
2641 | ds->surface = qemu_create_displaysurface(ds, 640, 480); | |
8f391ab4 | 2642 | register_displaystate(ds); |
2ff89790 TS |
2643 | } |
2644 | ||
8a7ddc38 FB |
2645 | /***********************************************************/ |
2646 | /* I/O handling */ | |
0824d6fc | 2647 | |
c4b1fcc0 FB |
2648 | typedef struct IOHandlerRecord { |
2649 | int fd; | |
7c9d8e07 FB |
2650 | IOCanRWHandler *fd_read_poll; |
2651 | IOHandler *fd_read; | |
2652 | IOHandler *fd_write; | |
cafffd40 | 2653 | int deleted; |
c4b1fcc0 FB |
2654 | void *opaque; |
2655 | /* temporary data */ | |
2656 | struct pollfd *ufd; | |
8a7ddc38 | 2657 | struct IOHandlerRecord *next; |
c4b1fcc0 FB |
2658 | } IOHandlerRecord; |
2659 | ||
8a7ddc38 | 2660 | static IOHandlerRecord *first_io_handler; |
c4b1fcc0 | 2661 | |
7c9d8e07 FB |
2662 | /* XXX: fd_read_poll should be suppressed, but an API change is |
2663 | necessary in the character devices to suppress fd_can_read(). */ | |
5fafdf24 TS |
2664 | int qemu_set_fd_handler2(int fd, |
2665 | IOCanRWHandler *fd_read_poll, | |
2666 | IOHandler *fd_read, | |
2667 | IOHandler *fd_write, | |
7c9d8e07 | 2668 | void *opaque) |
c4b1fcc0 | 2669 | { |
7c9d8e07 | 2670 | IOHandlerRecord **pioh, *ioh; |
c4b1fcc0 | 2671 | |
7c9d8e07 FB |
2672 | if (!fd_read && !fd_write) { |
2673 | pioh = &first_io_handler; | |
2674 | for(;;) { | |
2675 | ioh = *pioh; | |
2676 | if (ioh == NULL) | |
2677 | break; | |
2678 | if (ioh->fd == fd) { | |
cafffd40 | 2679 | ioh->deleted = 1; |
7c9d8e07 FB |
2680 | break; |
2681 | } | |
2682 | pioh = &ioh->next; | |
2683 | } | |
2684 | } else { | |
2685 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { | |
2686 | if (ioh->fd == fd) | |
2687 | goto found; | |
2688 | } | |
2689 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); | |
7c9d8e07 FB |
2690 | ioh->next = first_io_handler; |
2691 | first_io_handler = ioh; | |
2692 | found: | |
2693 | ioh->fd = fd; | |
2694 | ioh->fd_read_poll = fd_read_poll; | |
2695 | ioh->fd_read = fd_read; | |
2696 | ioh->fd_write = fd_write; | |
2697 | ioh->opaque = opaque; | |
cafffd40 | 2698 | ioh->deleted = 0; |
7c9d8e07 | 2699 | } |
c4b1fcc0 FB |
2700 | return 0; |
2701 | } | |
2702 | ||
5fafdf24 TS |
2703 | int qemu_set_fd_handler(int fd, |
2704 | IOHandler *fd_read, | |
2705 | IOHandler *fd_write, | |
7c9d8e07 | 2706 | void *opaque) |
8a7ddc38 | 2707 | { |
7c9d8e07 | 2708 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
8a7ddc38 FB |
2709 | } |
2710 | ||
56f3a5d0 | 2711 | #ifdef _WIN32 |
f331110f FB |
2712 | /***********************************************************/ |
2713 | /* Polling handling */ | |
2714 | ||
2715 | typedef struct PollingEntry { | |
2716 | PollingFunc *func; | |
2717 | void *opaque; | |
2718 | struct PollingEntry *next; | |
2719 | } PollingEntry; | |
2720 | ||
2721 | static PollingEntry *first_polling_entry; | |
2722 | ||
2723 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) | |
2724 | { | |
2725 | PollingEntry **ppe, *pe; | |
2726 | pe = qemu_mallocz(sizeof(PollingEntry)); | |
f331110f FB |
2727 | pe->func = func; |
2728 | pe->opaque = opaque; | |
2729 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); | |
2730 | *ppe = pe; | |
2731 | return 0; | |
2732 | } | |
2733 | ||
2734 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) | |
2735 | { | |
2736 | PollingEntry **ppe, *pe; | |
2737 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { | |
2738 | pe = *ppe; | |
2739 | if (pe->func == func && pe->opaque == opaque) { | |
2740 | *ppe = pe->next; | |
2741 | qemu_free(pe); | |
2742 | break; | |
2743 | } | |
2744 | } | |
2745 | } | |
2746 | ||
a18e524a FB |
2747 | /***********************************************************/ |
2748 | /* Wait objects support */ | |
2749 | typedef struct WaitObjects { | |
2750 | int num; | |
2751 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; | |
2752 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; | |
2753 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; | |
2754 | } WaitObjects; | |
2755 | ||
2756 | static WaitObjects wait_objects = {0}; | |
3b46e624 | 2757 | |
a18e524a FB |
2758 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
2759 | { | |
2760 | WaitObjects *w = &wait_objects; | |
2761 | ||
2762 | if (w->num >= MAXIMUM_WAIT_OBJECTS) | |
2763 | return -1; | |
2764 | w->events[w->num] = handle; | |
2765 | w->func[w->num] = func; | |
2766 | w->opaque[w->num] = opaque; | |
2767 | w->num++; | |
2768 | return 0; | |
2769 | } | |
2770 | ||
2771 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) | |
2772 | { | |
2773 | int i, found; | |
2774 | WaitObjects *w = &wait_objects; | |
2775 | ||
2776 | found = 0; | |
2777 | for (i = 0; i < w->num; i++) { | |
2778 | if (w->events[i] == handle) | |
2779 | found = 1; | |
2780 | if (found) { | |
2781 | w->events[i] = w->events[i + 1]; | |
2782 | w->func[i] = w->func[i + 1]; | |
2783 | w->opaque[i] = w->opaque[i + 1]; | |
3b46e624 | 2784 | } |
a18e524a FB |
2785 | } |
2786 | if (found) | |
2787 | w->num--; | |
2788 | } | |
2789 | #endif | |
2790 | ||
8a7ddc38 FB |
2791 | /***********************************************************/ |
2792 | /* ram save/restore */ | |
2793 | ||
94fb0909 | 2794 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
475e4277 AL |
2795 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
2796 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
2797 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
2798 | #define RAM_SAVE_FLAG_EOS 0x10 | |
2799 | ||
2800 | static int is_dup_page(uint8_t *page, uint8_t ch) | |
8a7ddc38 | 2801 | { |
475e4277 AL |
2802 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
2803 | uint32_t *array = (uint32_t *)page; | |
2804 | int i; | |
3b46e624 | 2805 | |
475e4277 AL |
2806 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
2807 | if (array[i] != val) | |
2808 | return 0; | |
2809 | } | |
2810 | ||
2811 | return 1; | |
2812 | } | |
2813 | ||
2814 | static int ram_save_block(QEMUFile *f) | |
2815 | { | |
2816 | static ram_addr_t current_addr = 0; | |
2817 | ram_addr_t saved_addr = current_addr; | |
2818 | ram_addr_t addr = 0; | |
2819 | int found = 0; | |
2820 | ||
94a6b54f | 2821 | while (addr < last_ram_offset) { |
475e4277 | 2822 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
5579c7f3 | 2823 | uint8_t *p; |
475e4277 AL |
2824 | |
2825 | cpu_physical_memory_reset_dirty(current_addr, | |
2826 | current_addr + TARGET_PAGE_SIZE, | |
2827 | MIGRATION_DIRTY_FLAG); | |
2828 | ||
5579c7f3 | 2829 | p = qemu_get_ram_ptr(current_addr); |
475e4277 | 2830 | |
5579c7f3 | 2831 | if (is_dup_page(p, *p)) { |
475e4277 | 2832 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
5579c7f3 | 2833 | qemu_put_byte(f, *p); |
475e4277 AL |
2834 | } else { |
2835 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); | |
5579c7f3 | 2836 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
c88676f8 | 2837 | } |
475e4277 AL |
2838 | |
2839 | found = 1; | |
2840 | break; | |
c88676f8 | 2841 | } |
475e4277 | 2842 | addr += TARGET_PAGE_SIZE; |
94a6b54f | 2843 | current_addr = (saved_addr + addr) % last_ram_offset; |
8a7ddc38 | 2844 | } |
475e4277 AL |
2845 | |
2846 | return found; | |
8a7ddc38 FB |
2847 | } |
2848 | ||
9f9e28cd | 2849 | static uint64_t bytes_transferred = 0; |
475e4277 AL |
2850 | |
2851 | static ram_addr_t ram_save_remaining(void) | |
2852 | { | |
2853 | ram_addr_t addr; | |
2854 | ram_addr_t count = 0; | |
2855 | ||
94a6b54f | 2856 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
475e4277 AL |
2857 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
2858 | count++; | |
2859 | } | |
2860 | ||
2861 | return count; | |
2862 | } | |
2863 | ||
9f9e28cd GC |
2864 | uint64_t ram_bytes_remaining(void) |
2865 | { | |
2866 | return ram_save_remaining() * TARGET_PAGE_SIZE; | |
2867 | } | |
2868 | ||
2869 | uint64_t ram_bytes_transferred(void) | |
2870 | { | |
2871 | return bytes_transferred; | |
2872 | } | |
2873 | ||
2874 | uint64_t ram_bytes_total(void) | |
2875 | { | |
2876 | return last_ram_offset; | |
2877 | } | |
2878 | ||
475e4277 AL |
2879 | static int ram_save_live(QEMUFile *f, int stage, void *opaque) |
2880 | { | |
2881 | ram_addr_t addr; | |
a0a3fd60 GC |
2882 | uint64_t bytes_transferred_last; |
2883 | double bwidth = 0; | |
2884 | uint64_t expected_time = 0; | |
475e4277 | 2885 | |
9fa06385 | 2886 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
b0a46a33 JK |
2887 | qemu_file_set_error(f); |
2888 | return 0; | |
2889 | } | |
2890 | ||
475e4277 AL |
2891 | if (stage == 1) { |
2892 | /* Make sure all dirty bits are set */ | |
94a6b54f | 2893 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
475e4277 AL |
2894 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
2895 | cpu_physical_memory_set_dirty(addr); | |
2896 | } | |
b0a46a33 | 2897 | |
475e4277 AL |
2898 | /* Enable dirty memory tracking */ |
2899 | cpu_physical_memory_set_dirty_tracking(1); | |
2900 | ||
94a6b54f | 2901 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
475e4277 AL |
2902 | } |
2903 | ||
a0a3fd60 GC |
2904 | bytes_transferred_last = bytes_transferred; |
2905 | bwidth = get_clock(); | |
2906 | ||
475e4277 AL |
2907 | while (!qemu_file_rate_limit(f)) { |
2908 | int ret; | |
2909 | ||
2910 | ret = ram_save_block(f); | |
9f9e28cd | 2911 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
475e4277 AL |
2912 | if (ret == 0) /* no more blocks */ |
2913 | break; | |
2914 | } | |
2915 | ||
a0a3fd60 GC |
2916 | bwidth = get_clock() - bwidth; |
2917 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; | |
2918 | ||
2919 | /* if we haven't transferred anything this round, force expected_time to a | |
2920 | * a very high value, but without crashing */ | |
2921 | if (bwidth == 0) | |
2922 | bwidth = 0.000001; | |
2923 | ||
475e4277 AL |
2924 | /* try transferring iterative blocks of memory */ |
2925 | ||
2926 | if (stage == 3) { | |
475e4277 AL |
2927 | |
2928 | /* flush all remaining blocks regardless of rate limiting */ | |
9f9e28cd GC |
2929 | while (ram_save_block(f) != 0) { |
2930 | bytes_transferred += TARGET_PAGE_SIZE; | |
2931 | } | |
8215e914 | 2932 | cpu_physical_memory_set_dirty_tracking(0); |
475e4277 AL |
2933 | } |
2934 | ||
2935 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
2936 | ||
a0a3fd60 GC |
2937 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
2938 | ||
2939 | return (stage == 2) && (expected_time <= migrate_max_downtime()); | |
475e4277 AL |
2940 | } |
2941 | ||
475e4277 AL |
2942 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
2943 | { | |
2944 | ram_addr_t addr; | |
2945 | int flags; | |
2946 | ||
475e4277 AL |
2947 | if (version_id != 3) |
2948 | return -EINVAL; | |
2949 | ||
2950 | do { | |
2951 | addr = qemu_get_be64(f); | |
2952 | ||
2953 | flags = addr & ~TARGET_PAGE_MASK; | |
2954 | addr &= TARGET_PAGE_MASK; | |
2955 | ||
2956 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { | |
94a6b54f | 2957 | if (addr != last_ram_offset) |
475e4277 AL |
2958 | return -EINVAL; |
2959 | } | |
2960 | ||
475e4277 AL |
2961 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
2962 | uint8_t ch = qemu_get_byte(f); | |
779c6bef AL |
2963 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
2964 | #ifndef _WIN32 | |
30868442 AL |
2965 | if (ch == 0 && |
2966 | (!kvm_enabled() || kvm_has_sync_mmu())) { | |
2967 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); | |
779c6bef | 2968 | } |
30868442 | 2969 | #endif |
475e4277 | 2970 | } else if (flags & RAM_SAVE_FLAG_PAGE) |
5579c7f3 | 2971 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
475e4277 AL |
2972 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
2973 | ||
8a7ddc38 FB |
2974 | return 0; |
2975 | } | |
2976 | ||
9e472e10 AL |
2977 | void qemu_service_io(void) |
2978 | { | |
d9f75a4e | 2979 | qemu_notify_event(); |
9e472e10 AL |
2980 | } |
2981 | ||
83f64091 FB |
2982 | /***********************************************************/ |
2983 | /* bottom halves (can be seen as timers which expire ASAP) */ | |
2984 | ||
2985 | struct QEMUBH { | |
2986 | QEMUBHFunc *cb; | |
2987 | void *opaque; | |
2988 | int scheduled; | |
1b435b10 AL |
2989 | int idle; |
2990 | int deleted; | |
83f64091 FB |
2991 | QEMUBH *next; |
2992 | }; | |
2993 | ||
2994 | static QEMUBH *first_bh = NULL; | |
2995 | ||
2996 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) | |
2997 | { | |
2998 | QEMUBH *bh; | |
2999 | bh = qemu_mallocz(sizeof(QEMUBH)); | |
83f64091 FB |
3000 | bh->cb = cb; |
3001 | bh->opaque = opaque; | |
1b435b10 AL |
3002 | bh->next = first_bh; |
3003 | first_bh = bh; | |
83f64091 FB |
3004 | return bh; |
3005 | } | |
3006 | ||
6eb5733a | 3007 | int qemu_bh_poll(void) |
83f64091 | 3008 | { |
1b435b10 | 3009 | QEMUBH *bh, **bhp; |
6eb5733a | 3010 | int ret; |
83f64091 | 3011 | |
6eb5733a | 3012 | ret = 0; |
1b435b10 AL |
3013 | for (bh = first_bh; bh; bh = bh->next) { |
3014 | if (!bh->deleted && bh->scheduled) { | |
3015 | bh->scheduled = 0; | |
3016 | if (!bh->idle) | |
3017 | ret = 1; | |
3018 | bh->idle = 0; | |
3019 | bh->cb(bh->opaque); | |
3020 | } | |
83f64091 | 3021 | } |
1b435b10 AL |
3022 | |
3023 | /* remove deleted bhs */ | |
3024 | bhp = &first_bh; | |
3025 | while (*bhp) { | |
3026 | bh = *bhp; | |
3027 | if (bh->deleted) { | |
3028 | *bhp = bh->next; | |
3029 | qemu_free(bh); | |
3030 | } else | |
3031 | bhp = &bh->next; | |
3032 | } | |
3033 | ||
6eb5733a | 3034 | return ret; |
83f64091 FB |
3035 | } |
3036 | ||
1b435b10 AL |
3037 | void qemu_bh_schedule_idle(QEMUBH *bh) |
3038 | { | |
3039 | if (bh->scheduled) | |
3040 | return; | |
3041 | bh->scheduled = 1; | |
3042 | bh->idle = 1; | |
3043 | } | |
3044 | ||
83f64091 FB |
3045 | void qemu_bh_schedule(QEMUBH *bh) |
3046 | { | |
83f64091 FB |
3047 | if (bh->scheduled) |
3048 | return; | |
3049 | bh->scheduled = 1; | |
1b435b10 | 3050 | bh->idle = 0; |
83f64091 | 3051 | /* stop the currently executing CPU to execute the BH ASAP */ |
d9f75a4e | 3052 | qemu_notify_event(); |
83f64091 FB |
3053 | } |
3054 | ||
3055 | void qemu_bh_cancel(QEMUBH *bh) | |
3056 | { | |
1b435b10 | 3057 | bh->scheduled = 0; |
83f64091 FB |
3058 | } |
3059 | ||
3060 | void qemu_bh_delete(QEMUBH *bh) | |
3061 | { | |
1b435b10 AL |
3062 | bh->scheduled = 0; |
3063 | bh->deleted = 1; | |
83f64091 FB |
3064 | } |
3065 | ||
56f3a5d0 AL |
3066 | static void qemu_bh_update_timeout(int *timeout) |
3067 | { | |
3068 | QEMUBH *bh; | |
3069 | ||
3070 | for (bh = first_bh; bh; bh = bh->next) { | |
3071 | if (!bh->deleted && bh->scheduled) { | |
3072 | if (bh->idle) { | |
3073 | /* idle bottom halves will be polled at least | |
3074 | * every 10ms */ | |
3075 | *timeout = MIN(10, *timeout); | |
3076 | } else { | |
3077 | /* non-idle bottom halves will be executed | |
3078 | * immediately */ | |
3079 | *timeout = 0; | |
3080 | break; | |
3081 | } | |
3082 | } | |
3083 | } | |
3084 | } | |
3085 | ||
cc1daa40 FB |
3086 | /***********************************************************/ |
3087 | /* machine registration */ | |
3088 | ||
bdaf78e0 | 3089 | static QEMUMachine *first_machine = NULL; |
6f338c34 | 3090 | QEMUMachine *current_machine = NULL; |
cc1daa40 FB |
3091 | |
3092 | int qemu_register_machine(QEMUMachine *m) | |
3093 | { | |
3094 | QEMUMachine **pm; | |
3095 | pm = &first_machine; | |
3096 | while (*pm != NULL) | |
3097 | pm = &(*pm)->next; | |
3098 | m->next = NULL; | |
3099 | *pm = m; | |
3100 | return 0; | |
3101 | } | |
3102 | ||
9596ebb7 | 3103 | static QEMUMachine *find_machine(const char *name) |
cc1daa40 FB |
3104 | { |
3105 | QEMUMachine *m; | |
3106 | ||
3107 | for(m = first_machine; m != NULL; m = m->next) { | |
3108 | if (!strcmp(m->name, name)) | |
3109 | return m; | |
3f6599e6 MM |
3110 | if (m->alias && !strcmp(m->alias, name)) |
3111 | return m; | |
cc1daa40 FB |
3112 | } |
3113 | return NULL; | |
3114 | } | |
3115 | ||
0c257437 AL |
3116 | static QEMUMachine *find_default_machine(void) |
3117 | { | |
3118 | QEMUMachine *m; | |
3119 | ||
3120 | for(m = first_machine; m != NULL; m = m->next) { | |
3121 | if (m->is_default) { | |
3122 | return m; | |
3123 | } | |
3124 | } | |
3125 | return NULL; | |
3126 | } | |
3127 | ||
8a7ddc38 FB |
3128 | /***********************************************************/ |
3129 | /* main execution loop */ | |
3130 | ||
9596ebb7 | 3131 | static void gui_update(void *opaque) |
8a7ddc38 | 3132 | { |
7d957bd8 | 3133 | uint64_t interval = GUI_REFRESH_INTERVAL; |
740733bb | 3134 | DisplayState *ds = opaque; |
7d957bd8 AL |
3135 | DisplayChangeListener *dcl = ds->listeners; |
3136 | ||
3137 | dpy_refresh(ds); | |
3138 | ||
3139 | while (dcl != NULL) { | |
3140 | if (dcl->gui_timer_interval && | |
3141 | dcl->gui_timer_interval < interval) | |
3142 | interval = dcl->gui_timer_interval; | |
3143 | dcl = dcl->next; | |
3144 | } | |
3145 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); | |
8a7ddc38 FB |
3146 | } |
3147 | ||
9043b62d BS |
3148 | static void nographic_update(void *opaque) |
3149 | { | |
3150 | uint64_t interval = GUI_REFRESH_INTERVAL; | |
3151 | ||
3152 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); | |
3153 | } | |
3154 | ||
0bd48850 FB |
3155 | struct vm_change_state_entry { |
3156 | VMChangeStateHandler *cb; | |
3157 | void *opaque; | |
3158 | LIST_ENTRY (vm_change_state_entry) entries; | |
3159 | }; | |
3160 | ||
3161 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; | |
3162 | ||
3163 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, | |
3164 | void *opaque) | |
3165 | { | |
3166 | VMChangeStateEntry *e; | |
3167 | ||
3168 | e = qemu_mallocz(sizeof (*e)); | |
0bd48850 FB |
3169 | |
3170 | e->cb = cb; | |
3171 | e->opaque = opaque; | |
3172 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); | |
3173 | return e; | |
3174 | } | |
3175 | ||
3176 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) | |
3177 | { | |
3178 | LIST_REMOVE (e, entries); | |
3179 | qemu_free (e); | |
3180 | } | |
3181 | ||
9781e040 | 3182 | static void vm_state_notify(int running, int reason) |
0bd48850 FB |
3183 | { |
3184 | VMChangeStateEntry *e; | |
3185 | ||
3186 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { | |
9781e040 | 3187 | e->cb(e->opaque, running, reason); |
0bd48850 FB |
3188 | } |
3189 | } | |
3190 | ||
d6dc3d42 AL |
3191 | static void resume_all_vcpus(void); |
3192 | static void pause_all_vcpus(void); | |
3193 | ||
8a7ddc38 FB |
3194 | void vm_start(void) |
3195 | { | |
3196 | if (!vm_running) { | |
3197 | cpu_enable_ticks(); | |
3198 | vm_running = 1; | |
9781e040 | 3199 | vm_state_notify(1, 0); |
efe75411 | 3200 | qemu_rearm_alarm_timer(alarm_timer); |
d6dc3d42 | 3201 | resume_all_vcpus(); |
8a7ddc38 FB |
3202 | } |
3203 | } | |
3204 | ||
bb0c6722 FB |
3205 | /* reset/shutdown handler */ |
3206 | ||
3207 | typedef struct QEMUResetEntry { | |
55ddfe8e | 3208 | TAILQ_ENTRY(QEMUResetEntry) entry; |
bb0c6722 FB |
3209 | QEMUResetHandler *func; |
3210 | void *opaque; | |
bb0c6722 FB |
3211 | } QEMUResetEntry; |
3212 | ||
55ddfe8e JK |
3213 | static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
3214 | TAILQ_HEAD_INITIALIZER(reset_handlers); | |
bb0c6722 FB |
3215 | static int reset_requested; |
3216 | static int shutdown_requested; | |
3475187d | 3217 | static int powerdown_requested; |
e568902a | 3218 | static int debug_requested; |
6e29f5da | 3219 | static int vmstop_requested; |
bb0c6722 | 3220 | |
cf7a2fe2 AJ |
3221 | int qemu_shutdown_requested(void) |
3222 | { | |
3223 | int r = shutdown_requested; | |
3224 | shutdown_requested = 0; | |
3225 | return r; | |
3226 | } | |
3227 | ||
3228 | int qemu_reset_requested(void) | |
3229 | { | |
3230 | int r = reset_requested; | |
3231 | reset_requested = 0; | |
3232 | return r; | |
3233 | } | |
3234 | ||
3235 | int qemu_powerdown_requested(void) | |
3236 | { | |
3237 | int r = powerdown_requested; | |
3238 | powerdown_requested = 0; | |
3239 | return r; | |
3240 | } | |
3241 | ||
e568902a AL |
3242 | static int qemu_debug_requested(void) |
3243 | { | |
3244 | int r = debug_requested; | |
3245 | debug_requested = 0; | |
3246 | return r; | |
3247 | } | |
3248 | ||
6e29f5da AL |
3249 | static int qemu_vmstop_requested(void) |
3250 | { | |
3251 | int r = vmstop_requested; | |
3252 | vmstop_requested = 0; | |
3253 | return r; | |
3254 | } | |
3255 | ||
3256 | static void do_vm_stop(int reason) | |
3257 | { | |
3258 | if (vm_running) { | |
3259 | cpu_disable_ticks(); | |
3260 | vm_running = 0; | |
d6dc3d42 | 3261 | pause_all_vcpus(); |
6e29f5da AL |
3262 | vm_state_notify(0, reason); |
3263 | } | |
3264 | } | |
3265 | ||
a08d4367 | 3266 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bb0c6722 | 3267 | { |
55ddfe8e | 3268 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bb0c6722 | 3269 | |
bb0c6722 FB |
3270 | re->func = func; |
3271 | re->opaque = opaque; | |
55ddfe8e | 3272 | TAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bb0c6722 FB |
3273 | } |
3274 | ||
dda9b29f | 3275 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bb0c6722 FB |
3276 | { |
3277 | QEMUResetEntry *re; | |
3278 | ||
55ddfe8e | 3279 | TAILQ_FOREACH(re, &reset_handlers, entry) { |
dda9b29f JK |
3280 | if (re->func == func && re->opaque == opaque) { |
3281 | TAILQ_REMOVE(&reset_handlers, re, entry); | |
3282 | qemu_free(re); | |
3283 | return; | |
3284 | } | |
3285 | } | |
3286 | } | |
3287 | ||
3288 | void qemu_system_reset(void) | |
3289 | { | |
3290 | QEMUResetEntry *re, *nre; | |
3291 | ||
3292 | /* reset all devices */ | |
3293 | TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { | |
bb0c6722 FB |
3294 | re->func(re->opaque); |
3295 | } | |
3296 | } | |
3297 | ||
3298 | void qemu_system_reset_request(void) | |
3299 | { | |
d1beab82 FB |
3300 | if (no_reboot) { |
3301 | shutdown_requested = 1; | |
3302 | } else { | |
3303 | reset_requested = 1; | |
3304 | } | |
d9f75a4e | 3305 | qemu_notify_event(); |
bb0c6722 FB |
3306 | } |
3307 | ||
3308 | void qemu_system_shutdown_request(void) | |
3309 | { | |
3310 | shutdown_requested = 1; | |
d9f75a4e | 3311 | qemu_notify_event(); |
bb0c6722 FB |
3312 | } |
3313 | ||
3475187d FB |
3314 | void qemu_system_powerdown_request(void) |
3315 | { | |
3316 | powerdown_requested = 1; | |
d9f75a4e AL |
3317 | qemu_notify_event(); |
3318 | } | |
3319 | ||
d6dc3d42 AL |
3320 | #ifdef CONFIG_IOTHREAD |
3321 | static void qemu_system_vmstop_request(int reason) | |
d9f75a4e | 3322 | { |
d6dc3d42 AL |
3323 | vmstop_requested = reason; |
3324 | qemu_notify_event(); | |
bb0c6722 | 3325 | } |
d6dc3d42 | 3326 | #endif |
bb0c6722 | 3327 | |
50317c7f AL |
3328 | #ifndef _WIN32 |
3329 | static int io_thread_fd = -1; | |
3330 | ||
3331 | static void qemu_event_increment(void) | |
3fcf7b6b | 3332 | { |
50317c7f AL |
3333 | static const char byte = 0; |
3334 | ||
3335 | if (io_thread_fd == -1) | |
3336 | return; | |
3337 | ||
3338 | write(io_thread_fd, &byte, sizeof(byte)); | |
3339 | } | |
3340 | ||
3341 | static void qemu_event_read(void *opaque) | |
3342 | { | |
3343 | int fd = (unsigned long)opaque; | |
3344 | ssize_t len; | |
3345 | ||
3346 | /* Drain the notify pipe */ | |
3347 | do { | |
3348 | char buffer[512]; | |
3349 | len = read(fd, buffer, sizeof(buffer)); | |
3350 | } while ((len == -1 && errno == EINTR) || len > 0); | |
3351 | } | |
3352 | ||
3353 | static int qemu_event_init(void) | |
3354 | { | |
3355 | int err; | |
3356 | int fds[2]; | |
3357 | ||
3358 | err = pipe(fds); | |
3359 | if (err == -1) | |
3360 | return -errno; | |
3361 | ||
3362 | err = fcntl_setfl(fds[0], O_NONBLOCK); | |
3363 | if (err < 0) | |
3364 | goto fail; | |
3365 | ||
3366 | err = fcntl_setfl(fds[1], O_NONBLOCK); | |
3367 | if (err < 0) | |
3368 | goto fail; | |
3369 | ||
3370 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, | |
3371 | (void *)(unsigned long)fds[0]); | |
3372 | ||
3373 | io_thread_fd = fds[1]; | |
a7e21219 JK |
3374 | return 0; |
3375 | ||
50317c7f AL |
3376 | fail: |
3377 | close(fds[0]); | |
3378 | close(fds[1]); | |
3379 | return err; | |
3380 | } | |
3381 | #else | |
3382 | HANDLE qemu_event_handle; | |
3383 | ||
3384 | static void dummy_event_handler(void *opaque) | |
3385 | { | |
3386 | } | |
3387 | ||
3388 | static int qemu_event_init(void) | |
3389 | { | |
3390 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); | |
3391 | if (!qemu_event_handle) { | |
3392 | perror("Failed CreateEvent"); | |
3393 | return -1; | |
3394 | } | |
3395 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); | |
3fcf7b6b AL |
3396 | return 0; |
3397 | } | |
3398 | ||
50317c7f AL |
3399 | static void qemu_event_increment(void) |
3400 | { | |
3401 | SetEvent(qemu_event_handle); | |
3402 | } | |
3403 | #endif | |
3404 | ||
d6dc3d42 AL |
3405 | static int cpu_can_run(CPUState *env) |
3406 | { | |
3407 | if (env->stop) | |
3408 | return 0; | |
3409 | if (env->stopped) | |
3410 | return 0; | |
3411 | return 1; | |
3412 | } | |
3413 | ||
3414 | #ifndef CONFIG_IOTHREAD | |
50317c7f AL |
3415 | static int qemu_init_main_loop(void) |
3416 | { | |
3417 | return qemu_event_init(); | |
3418 | } | |
3419 | ||
0bf46a40 AL |
3420 | void qemu_init_vcpu(void *_env) |
3421 | { | |
3422 | CPUState *env = _env; | |
3423 | ||
3424 | if (kvm_enabled()) | |
3425 | kvm_init_vcpu(env); | |
dc6b1c09 AP |
3426 | env->nr_cores = smp_cores; |
3427 | env->nr_threads = smp_threads; | |
0bf46a40 AL |
3428 | return; |
3429 | } | |
3430 | ||
8edac960 AL |
3431 | int qemu_cpu_self(void *env) |
3432 | { | |
3433 | return 1; | |
3434 | } | |
3435 | ||
d6dc3d42 AL |
3436 | static void resume_all_vcpus(void) |
3437 | { | |
3438 | } | |
3439 | ||
3440 | static void pause_all_vcpus(void) | |
3441 | { | |
3442 | } | |
3443 | ||
8edac960 AL |
3444 | void qemu_cpu_kick(void *env) |
3445 | { | |
3446 | return; | |
3447 | } | |
3448 | ||
d6dc3d42 AL |
3449 | void qemu_notify_event(void) |
3450 | { | |
3451 | CPUState *env = cpu_single_env; | |
3452 | ||
3453 | if (env) { | |
3454 | cpu_exit(env); | |
4a1418e0 | 3455 | } |
d6dc3d42 AL |
3456 | } |
3457 | ||
4870852c AL |
3458 | #define qemu_mutex_lock_iothread() do { } while (0) |
3459 | #define qemu_mutex_unlock_iothread() do { } while (0) | |
3460 | ||
6e29f5da AL |
3461 | void vm_stop(int reason) |
3462 | { | |
3463 | do_vm_stop(reason); | |
3464 | } | |
3465 | ||
d6dc3d42 AL |
3466 | #else /* CONFIG_IOTHREAD */ |
3467 | ||
3468 | #include "qemu-thread.h" | |
3469 | ||
3470 | QemuMutex qemu_global_mutex; | |
3471 | static QemuMutex qemu_fair_mutex; | |
3472 | ||
3473 | static QemuThread io_thread; | |
3474 | ||
3475 | static QemuThread *tcg_cpu_thread; | |
3476 | static QemuCond *tcg_halt_cond; | |
3477 | ||
3478 | static int qemu_system_ready; | |
3479 | /* cpu creation */ | |
3480 | static QemuCond qemu_cpu_cond; | |
3481 | /* system init */ | |
3482 | static QemuCond qemu_system_cond; | |
3483 | static QemuCond qemu_pause_cond; | |
3484 | ||
3485 | static void block_io_signals(void); | |
3486 | static void unblock_io_signals(void); | |
3487 | static int tcg_has_work(void); | |
3488 | ||
3489 | static int qemu_init_main_loop(void) | |
3490 | { | |
3491 | int ret; | |
3492 | ||
3493 | ret = qemu_event_init(); | |
3494 | if (ret) | |
3495 | return ret; | |
3496 | ||
3497 | qemu_cond_init(&qemu_pause_cond); | |
3498 | qemu_mutex_init(&qemu_fair_mutex); | |
3499 | qemu_mutex_init(&qemu_global_mutex); | |
3500 | qemu_mutex_lock(&qemu_global_mutex); | |
3501 | ||
3502 | unblock_io_signals(); | |
3503 | qemu_thread_self(&io_thread); | |
3504 | ||
3505 | return 0; | |
3506 | } | |
3507 | ||
3508 | static void qemu_wait_io_event(CPUState *env) | |
3509 | { | |
3510 | while (!tcg_has_work()) | |
3511 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); | |
3512 | ||
3513 | qemu_mutex_unlock(&qemu_global_mutex); | |
3514 | ||
3515 | /* | |
3516 | * Users of qemu_global_mutex can be starved, having no chance | |
3517 | * to acquire it since this path will get to it first. | |
3518 | * So use another lock to provide fairness. | |
3519 | */ | |
3520 | qemu_mutex_lock(&qemu_fair_mutex); | |
3521 | qemu_mutex_unlock(&qemu_fair_mutex); | |
3522 | ||
3523 | qemu_mutex_lock(&qemu_global_mutex); | |
3524 | if (env->stop) { | |
3525 | env->stop = 0; | |
3526 | env->stopped = 1; | |
3527 | qemu_cond_signal(&qemu_pause_cond); | |
3528 | } | |
3529 | } | |
3530 | ||
3531 | static int qemu_cpu_exec(CPUState *env); | |
3532 | ||
3533 | static void *kvm_cpu_thread_fn(void *arg) | |
3534 | { | |
3535 | CPUState *env = arg; | |
3536 | ||
3537 | block_io_signals(); | |
3538 | qemu_thread_self(env->thread); | |
321c1cb1 JCD |
3539 | if (kvm_enabled()) |
3540 | kvm_init_vcpu(env); | |
d6dc3d42 AL |
3541 | |
3542 | /* signal CPU creation */ | |
3543 | qemu_mutex_lock(&qemu_global_mutex); | |
3544 | env->created = 1; | |
3545 | qemu_cond_signal(&qemu_cpu_cond); | |
3546 | ||
3547 | /* and wait for machine initialization */ | |
3548 | while (!qemu_system_ready) | |
3549 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
3550 | ||
3551 | while (1) { | |
3552 | if (cpu_can_run(env)) | |
3553 | qemu_cpu_exec(env); | |
1c3173b9 | 3554 | qemu_wait_io_event(env); |
d6dc3d42 AL |
3555 | } |
3556 | ||
3557 | return NULL; | |
3558 | } | |
3559 | ||
3560 | static void tcg_cpu_exec(void); | |
3561 | ||
3562 | static void *tcg_cpu_thread_fn(void *arg) | |
3563 | { | |
3564 | CPUState *env = arg; | |
3565 | ||
3566 | block_io_signals(); | |
3567 | qemu_thread_self(env->thread); | |
3568 | ||
3569 | /* signal CPU creation */ | |
3570 | qemu_mutex_lock(&qemu_global_mutex); | |
3571 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
3572 | env->created = 1; | |
3573 | qemu_cond_signal(&qemu_cpu_cond); | |
3574 | ||
3575 | /* and wait for machine initialization */ | |
3576 | while (!qemu_system_ready) | |
3577 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); | |
3578 | ||
3579 | while (1) { | |
3580 | tcg_cpu_exec(); | |
3581 | qemu_wait_io_event(cur_cpu); | |
3582 | } | |
3583 | ||
3584 | return NULL; | |
3585 | } | |
3586 | ||
3587 | void qemu_cpu_kick(void *_env) | |
3588 | { | |
3589 | CPUState *env = _env; | |
3590 | qemu_cond_broadcast(env->halt_cond); | |
3591 | if (kvm_enabled()) | |
3592 | qemu_thread_signal(env->thread, SIGUSR1); | |
3593 | } | |
3594 | ||
3595 | int qemu_cpu_self(void *env) | |
3596 | { | |
3597 | return (cpu_single_env != NULL); | |
3598 | } | |
3599 | ||
3600 | static void cpu_signal(int sig) | |
3601 | { | |
3602 | if (cpu_single_env) | |
3603 | cpu_exit(cpu_single_env); | |
3604 | } | |
3605 | ||
3606 | static void block_io_signals(void) | |
3607 | { | |
3608 | sigset_t set; | |
3609 | struct sigaction sigact; | |
3610 | ||
3611 | sigemptyset(&set); | |
3612 | sigaddset(&set, SIGUSR2); | |
3613 | sigaddset(&set, SIGIO); | |
3614 | sigaddset(&set, SIGALRM); | |
3615 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
3616 | ||
3617 | sigemptyset(&set); | |
3618 | sigaddset(&set, SIGUSR1); | |
3619 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
3620 | ||
3621 | memset(&sigact, 0, sizeof(sigact)); | |
3622 | sigact.sa_handler = cpu_signal; | |
3623 | sigaction(SIGUSR1, &sigact, NULL); | |
3624 | } | |
3625 | ||
3626 | static void unblock_io_signals(void) | |
3627 | { | |
3628 | sigset_t set; | |
3629 | ||
3630 | sigemptyset(&set); | |
3631 | sigaddset(&set, SIGUSR2); | |
3632 | sigaddset(&set, SIGIO); | |
3633 | sigaddset(&set, SIGALRM); | |
3634 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
3635 | ||
3636 | sigemptyset(&set); | |
3637 | sigaddset(&set, SIGUSR1); | |
3638 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
3639 | } | |
3640 | ||
3641 | static void qemu_signal_lock(unsigned int msecs) | |
3642 | { | |
3643 | qemu_mutex_lock(&qemu_fair_mutex); | |
3644 | ||
3645 | while (qemu_mutex_trylock(&qemu_global_mutex)) { | |
3646 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); | |
3647 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) | |
3648 | break; | |
3649 | } | |
3650 | qemu_mutex_unlock(&qemu_fair_mutex); | |
3651 | } | |
3652 | ||
3653 | static void qemu_mutex_lock_iothread(void) | |
3654 | { | |
3655 | if (kvm_enabled()) { | |
3656 | qemu_mutex_lock(&qemu_fair_mutex); | |
3657 | qemu_mutex_lock(&qemu_global_mutex); | |
3658 | qemu_mutex_unlock(&qemu_fair_mutex); | |
3659 | } else | |
3660 | qemu_signal_lock(100); | |
3661 | } | |
3662 | ||
3663 | static void qemu_mutex_unlock_iothread(void) | |
3664 | { | |
3665 | qemu_mutex_unlock(&qemu_global_mutex); | |
3666 | } | |
3667 | ||
3668 | static int all_vcpus_paused(void) | |
3669 | { | |
3670 | CPUState *penv = first_cpu; | |
3671 | ||
3672 | while (penv) { | |
3673 | if (!penv->stopped) | |
3674 | return 0; | |
3675 | penv = (CPUState *)penv->next_cpu; | |
3676 | } | |
3677 | ||
3678 | return 1; | |
3679 | } | |
3680 | ||
3681 | static void pause_all_vcpus(void) | |
3682 | { | |
3683 | CPUState *penv = first_cpu; | |
3684 | ||
3685 | while (penv) { | |
3686 | penv->stop = 1; | |
3687 | qemu_thread_signal(penv->thread, SIGUSR1); | |
3688 | qemu_cpu_kick(penv); | |
3689 | penv = (CPUState *)penv->next_cpu; | |
3690 | } | |
3691 | ||
3692 | while (!all_vcpus_paused()) { | |
3693 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); | |
3694 | penv = first_cpu; | |
3695 | while (penv) { | |
3696 | qemu_thread_signal(penv->thread, SIGUSR1); | |
3697 | penv = (CPUState *)penv->next_cpu; | |
3698 | } | |
3699 | } | |
3700 | } | |
3701 | ||
3702 | static void resume_all_vcpus(void) | |
3703 | { | |
3704 | CPUState *penv = first_cpu; | |
3705 | ||
3706 | while (penv) { | |
3707 | penv->stop = 0; | |
3708 | penv->stopped = 0; | |
3709 | qemu_thread_signal(penv->thread, SIGUSR1); | |
3710 | qemu_cpu_kick(penv); | |
3711 | penv = (CPUState *)penv->next_cpu; | |
3712 | } | |
3713 | } | |
3714 | ||
3715 | static void tcg_init_vcpu(void *_env) | |
3716 | { | |
3717 | CPUState *env = _env; | |
3718 | /* share a single thread for all cpus with TCG */ | |
3719 | if (!tcg_cpu_thread) { | |
3720 | env->thread = qemu_mallocz(sizeof(QemuThread)); | |
3721 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
3722 | qemu_cond_init(env->halt_cond); | |
3723 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); | |
3724 | while (env->created == 0) | |
3725 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
3726 | tcg_cpu_thread = env->thread; | |
3727 | tcg_halt_cond = env->halt_cond; | |
3728 | } else { | |
3729 | env->thread = tcg_cpu_thread; | |
3730 | env->halt_cond = tcg_halt_cond; | |
3731 | } | |
3732 | } | |
3733 | ||
3734 | static void kvm_start_vcpu(CPUState *env) | |
3735 | { | |
d6dc3d42 AL |
3736 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
3737 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); | |
3738 | qemu_cond_init(env->halt_cond); | |
3739 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); | |
3740 | while (env->created == 0) | |
3741 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); | |
3742 | } | |
3743 | ||
3744 | void qemu_init_vcpu(void *_env) | |
3745 | { | |
3746 | CPUState *env = _env; | |
3747 | ||
3748 | if (kvm_enabled()) | |
3749 | kvm_start_vcpu(env); | |
3750 | else | |
3751 | tcg_init_vcpu(env); | |
dc6b1c09 AP |
3752 | env->nr_cores = smp_cores; |
3753 | env->nr_threads = smp_threads; | |
d6dc3d42 AL |
3754 | } |
3755 | ||
3756 | void qemu_notify_event(void) | |
3757 | { | |
3758 | qemu_event_increment(); | |
3759 | } | |
3760 | ||
3761 | void vm_stop(int reason) | |
3762 | { | |
3763 | QemuThread me; | |
3764 | qemu_thread_self(&me); | |
3765 | ||
3766 | if (!qemu_thread_equal(&me, &io_thread)) { | |
3767 | qemu_system_vmstop_request(reason); | |
3768 | /* | |
3769 | * FIXME: should not return to device code in case | |
3770 | * vm_stop() has been requested. | |
3771 | */ | |
3772 | if (cpu_single_env) { | |
3773 | cpu_exit(cpu_single_env); | |
3774 | cpu_single_env->stop = 1; | |
3775 | } | |
3776 | return; | |
3777 | } | |
3778 | do_vm_stop(reason); | |
3779 | } | |
3780 | ||
3781 | #endif | |
3782 | ||
3783 | ||
877cf882 | 3784 | #ifdef _WIN32 |
69d6451c | 3785 | static void host_main_loop_wait(int *timeout) |
56f3a5d0 AL |
3786 | { |
3787 | int ret, ret2, i; | |
f331110f FB |
3788 | PollingEntry *pe; |
3789 | ||
c4b1fcc0 | 3790 | |
f331110f FB |
3791 | /* XXX: need to suppress polling by better using win32 events */ |
3792 | ret = 0; | |
3793 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { | |
3794 | ret |= pe->func(pe->opaque); | |
3795 | } | |
e6b1e558 | 3796 | if (ret == 0) { |
a18e524a FB |
3797 | int err; |
3798 | WaitObjects *w = &wait_objects; | |
3b46e624 | 3799 | |
56f3a5d0 | 3800 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
a18e524a FB |
3801 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
3802 | if (w->func[ret - WAIT_OBJECT_0]) | |
3803 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); | |
3b46e624 | 3804 | |
5fafdf24 | 3805 | /* Check for additional signaled events */ |
e6b1e558 | 3806 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
3b46e624 | 3807 | |
e6b1e558 TS |
3808 | /* Check if event is signaled */ |
3809 | ret2 = WaitForSingleObject(w->events[i], 0); | |
3810 | if(ret2 == WAIT_OBJECT_0) { | |
3811 | if (w->func[i]) | |
3812 | w->func[i](w->opaque[i]); | |
3813 | } else if (ret2 == WAIT_TIMEOUT) { | |
3814 | } else { | |
3815 | err = GetLastError(); | |
3816 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); | |
3b46e624 TS |
3817 | } |
3818 | } | |
a18e524a FB |
3819 | } else if (ret == WAIT_TIMEOUT) { |
3820 | } else { | |
3821 | err = GetLastError(); | |
e6b1e558 | 3822 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
a18e524a | 3823 | } |
f331110f | 3824 | } |
56f3a5d0 AL |
3825 | |
3826 | *timeout = 0; | |
3827 | } | |
3828 | #else | |
69d6451c | 3829 | static void host_main_loop_wait(int *timeout) |
56f3a5d0 AL |
3830 | { |
3831 | } | |
fd1dff4b | 3832 | #endif |
56f3a5d0 AL |
3833 | |
3834 | void main_loop_wait(int timeout) | |
3835 | { | |
3836 | IOHandlerRecord *ioh; | |
3837 | fd_set rfds, wfds, xfds; | |
3838 | int ret, nfds; | |
3839 | struct timeval tv; | |
3840 | ||
3841 | qemu_bh_update_timeout(&timeout); | |
3842 | ||
3843 | host_main_loop_wait(&timeout); | |
3844 | ||
fd1dff4b FB |
3845 | /* poll any events */ |
3846 | /* XXX: separate device handlers from system ones */ | |
6abfbd79 | 3847 | nfds = -1; |
fd1dff4b FB |
3848 | FD_ZERO(&rfds); |
3849 | FD_ZERO(&wfds); | |
e035649e | 3850 | FD_ZERO(&xfds); |
fd1dff4b | 3851 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
cafffd40 TS |
3852 | if (ioh->deleted) |
3853 | continue; | |
fd1dff4b FB |
3854 | if (ioh->fd_read && |
3855 | (!ioh->fd_read_poll || | |
3856 | ioh->fd_read_poll(ioh->opaque) != 0)) { | |
3857 | FD_SET(ioh->fd, &rfds); | |
3858 | if (ioh->fd > nfds) | |
3859 | nfds = ioh->fd; | |
3860 | } | |
3861 | if (ioh->fd_write) { | |
3862 | FD_SET(ioh->fd, &wfds); | |
3863 | if (ioh->fd > nfds) | |
3864 | nfds = ioh->fd; | |
3865 | } | |
3866 | } | |
3b46e624 | 3867 | |
56f3a5d0 AL |
3868 | tv.tv_sec = timeout / 1000; |
3869 | tv.tv_usec = (timeout % 1000) * 1000; | |
3870 | ||
d918f23e JK |
3871 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
3872 | ||
4870852c | 3873 | qemu_mutex_unlock_iothread(); |
e035649e | 3874 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
4870852c | 3875 | qemu_mutex_lock_iothread(); |
fd1dff4b | 3876 | if (ret > 0) { |
cafffd40 TS |
3877 | IOHandlerRecord **pioh; |
3878 | ||
3879 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { | |
6ab43fdc | 3880 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
fd1dff4b | 3881 | ioh->fd_read(ioh->opaque); |
7c9d8e07 | 3882 | } |
6ab43fdc | 3883 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
fd1dff4b | 3884 | ioh->fd_write(ioh->opaque); |
c4b1fcc0 | 3885 | } |
b4608c04 | 3886 | } |
cafffd40 TS |
3887 | |
3888 | /* remove deleted IO handlers */ | |
3889 | pioh = &first_io_handler; | |
3890 | while (*pioh) { | |
3891 | ioh = *pioh; | |
3892 | if (ioh->deleted) { | |
3893 | *pioh = ioh->next; | |
3894 | qemu_free(ioh); | |
5fafdf24 | 3895 | } else |
cafffd40 TS |
3896 | pioh = &ioh->next; |
3897 | } | |
fd1dff4b | 3898 | } |
d918f23e JK |
3899 | |
3900 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); | |
b4608c04 | 3901 | |
50317c7f AL |
3902 | /* rearm timer, if not periodic */ |
3903 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { | |
3904 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; | |
3905 | qemu_rearm_alarm_timer(alarm_timer); | |
3906 | } | |
3907 | ||
357c692c | 3908 | /* vm time timers */ |
d6dc3d42 AL |
3909 | if (vm_running) { |
3910 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) | |
3911 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], | |
3912 | qemu_get_clock(vm_clock)); | |
3913 | } | |
357c692c AL |
3914 | |
3915 | /* real time timers */ | |
3916 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], | |
3917 | qemu_get_clock(rt_clock)); | |
3918 | ||
423f0742 PB |
3919 | /* Check bottom-halves last in case any of the earlier events triggered |
3920 | them. */ | |
3921 | qemu_bh_poll(); | |
3b46e624 | 3922 | |
5905b2e5 FB |
3923 | } |
3924 | ||
43b96858 | 3925 | static int qemu_cpu_exec(CPUState *env) |
5905b2e5 | 3926 | { |
43b96858 | 3927 | int ret; |
89bfc105 FB |
3928 | #ifdef CONFIG_PROFILER |
3929 | int64_t ti; | |
3930 | #endif | |
5905b2e5 | 3931 | |
89bfc105 | 3932 | #ifdef CONFIG_PROFILER |
43b96858 | 3933 | ti = profile_getclock(); |
89bfc105 | 3934 | #endif |
43b96858 AL |
3935 | if (use_icount) { |
3936 | int64_t count; | |
3937 | int decr; | |
3938 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); | |
3939 | env->icount_decr.u16.low = 0; | |
3940 | env->icount_extra = 0; | |
3941 | count = qemu_next_deadline(); | |
3942 | count = (count + (1 << icount_time_shift) - 1) | |
3943 | >> icount_time_shift; | |
3944 | qemu_icount += count; | |
3945 | decr = (count > 0xffff) ? 0xffff : count; | |
3946 | count -= decr; | |
3947 | env->icount_decr.u16.low = decr; | |
3948 | env->icount_extra = count; | |
3949 | } | |
3950 | ret = cpu_exec(env); | |
89bfc105 | 3951 | #ifdef CONFIG_PROFILER |
43b96858 | 3952 | qemu_time += profile_getclock() - ti; |
89bfc105 | 3953 | #endif |
43b96858 AL |
3954 | if (use_icount) { |
3955 | /* Fold pending instructions back into the | |
3956 | instruction counter, and clear the interrupt flag. */ | |
3957 | qemu_icount -= (env->icount_decr.u16.low | |
3958 | + env->icount_extra); | |
3959 | env->icount_decr.u32 = 0; | |
3960 | env->icount_extra = 0; | |
3961 | } | |
3962 | return ret; | |
3963 | } | |
3964 | ||
e6e35b1e AL |
3965 | static void tcg_cpu_exec(void) |
3966 | { | |
d6dc3d42 | 3967 | int ret = 0; |
e6e35b1e AL |
3968 | |
3969 | if (next_cpu == NULL) | |
3970 | next_cpu = first_cpu; | |
3971 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { | |
3972 | CPUState *env = cur_cpu = next_cpu; | |
3973 | ||
3974 | if (!vm_running) | |
3975 | break; | |
3976 | if (timer_alarm_pending) { | |
3977 | timer_alarm_pending = 0; | |
3978 | break; | |
3979 | } | |
d6dc3d42 AL |
3980 | if (cpu_can_run(env)) |
3981 | ret = qemu_cpu_exec(env); | |
e6e35b1e AL |
3982 | if (ret == EXCP_DEBUG) { |
3983 | gdb_set_stop_cpu(env); | |
3984 | debug_requested = 1; | |
3985 | break; | |
3986 | } | |
3987 | } | |
3988 | } | |
3989 | ||
43b96858 AL |
3990 | static int cpu_has_work(CPUState *env) |
3991 | { | |
d6dc3d42 AL |
3992 | if (env->stop) |
3993 | return 1; | |
3994 | if (env->stopped) | |
3995 | return 0; | |
43b96858 AL |
3996 | if (!env->halted) |
3997 | return 1; | |
3998 | if (qemu_cpu_has_work(env)) | |
3999 | return 1; | |
4000 | return 0; | |
4001 | } | |
4002 | ||
4003 | static int tcg_has_work(void) | |
4004 | { | |
4005 | CPUState *env; | |
4006 | ||
4007 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
4008 | if (cpu_has_work(env)) | |
4009 | return 1; | |
4010 | return 0; | |
4011 | } | |
4012 | ||
4013 | static int qemu_calculate_timeout(void) | |
4014 | { | |
b319820d | 4015 | #ifndef CONFIG_IOTHREAD |
43b96858 AL |
4016 | int timeout; |
4017 | ||
4018 | if (!vm_running) | |
4019 | timeout = 5000; | |
4020 | else if (tcg_has_work()) | |
4021 | timeout = 0; | |
4022 | else if (!use_icount) | |
4023 | timeout = 5000; | |
4024 | else { | |
4025 | /* XXX: use timeout computed from timers */ | |
4026 | int64_t add; | |
4027 | int64_t delta; | |
4028 | /* Advance virtual time to the next event. */ | |
4029 | if (use_icount == 1) { | |
4030 | /* When not using an adaptive execution frequency | |
4031 | we tend to get badly out of sync with real time, | |
4032 | so just delay for a reasonable amount of time. */ | |
4033 | delta = 0; | |
4034 | } else { | |
4035 | delta = cpu_get_icount() - cpu_get_clock(); | |
4036 | } | |
4037 | if (delta > 0) { | |
4038 | /* If virtual time is ahead of real time then just | |
4039 | wait for IO. */ | |
4040 | timeout = (delta / 1000000) + 1; | |
4041 | } else { | |
4042 | /* Wait for either IO to occur or the next | |
4043 | timer event. */ | |
4044 | add = qemu_next_deadline(); | |
4045 | /* We advance the timer before checking for IO. | |
4046 | Limit the amount we advance so that early IO | |
4047 | activity won't get the guest too far ahead. */ | |
4048 | if (add > 10000000) | |
4049 | add = 10000000; | |
4050 | delta += add; | |
4051 | add = (add + (1 << icount_time_shift) - 1) | |
4052 | >> icount_time_shift; | |
4053 | qemu_icount += add; | |
4054 | timeout = delta / 1000000; | |
4055 | if (timeout < 0) | |
4056 | timeout = 0; | |
4057 | } | |
4058 | } | |
4059 | ||
4060 | return timeout; | |
b319820d LC |
4061 | #else /* CONFIG_IOTHREAD */ |
4062 | return 1000; | |
4063 | #endif | |
43b96858 AL |
4064 | } |
4065 | ||
4066 | static int vm_can_run(void) | |
4067 | { | |
4068 | if (powerdown_requested) | |
4069 | return 0; | |
4070 | if (reset_requested) | |
4071 | return 0; | |
4072 | if (shutdown_requested) | |
4073 | return 0; | |
e568902a AL |
4074 | if (debug_requested) |
4075 | return 0; | |
43b96858 AL |
4076 | return 1; |
4077 | } | |
4078 | ||
d9c32310 BS |
4079 | qemu_irq qemu_system_powerdown; |
4080 | ||
43b96858 AL |
4081 | static void main_loop(void) |
4082 | { | |
6e29f5da | 4083 | int r; |
e6e35b1e | 4084 | |
d6dc3d42 AL |
4085 | #ifdef CONFIG_IOTHREAD |
4086 | qemu_system_ready = 1; | |
4087 | qemu_cond_broadcast(&qemu_system_cond); | |
4088 | #endif | |
4089 | ||
6e29f5da | 4090 | for (;;) { |
43b96858 | 4091 | do { |
e6e35b1e AL |
4092 | #ifdef CONFIG_PROFILER |
4093 | int64_t ti; | |
4094 | #endif | |
d6dc3d42 | 4095 | #ifndef CONFIG_IOTHREAD |
e6e35b1e | 4096 | tcg_cpu_exec(); |
d6dc3d42 | 4097 | #endif |
89bfc105 | 4098 | #ifdef CONFIG_PROFILER |
43b96858 | 4099 | ti = profile_getclock(); |
89bfc105 | 4100 | #endif |
43b96858 | 4101 | main_loop_wait(qemu_calculate_timeout()); |
89bfc105 | 4102 | #ifdef CONFIG_PROFILER |
43b96858 | 4103 | dev_time += profile_getclock() - ti; |
89bfc105 | 4104 | #endif |
e568902a | 4105 | } while (vm_can_run()); |
43b96858 | 4106 | |
e568902a AL |
4107 | if (qemu_debug_requested()) |
4108 | vm_stop(EXCP_DEBUG); | |
43b96858 AL |
4109 | if (qemu_shutdown_requested()) { |
4110 | if (no_shutdown) { | |
4111 | vm_stop(0); | |
4112 | no_shutdown = 0; | |
4113 | } else | |
4114 | break; | |
4115 | } | |
d6dc3d42 AL |
4116 | if (qemu_reset_requested()) { |
4117 | pause_all_vcpus(); | |
43b96858 | 4118 | qemu_system_reset(); |
d6dc3d42 AL |
4119 | resume_all_vcpus(); |
4120 | } | |
d9c32310 BS |
4121 | if (qemu_powerdown_requested()) { |
4122 | qemu_irq_raise(qemu_system_powerdown); | |
4123 | } | |
6e29f5da AL |
4124 | if ((r = qemu_vmstop_requested())) |
4125 | vm_stop(r); | |
b4608c04 | 4126 | } |
d6dc3d42 | 4127 | pause_all_vcpus(); |
b4608c04 FB |
4128 | } |
4129 | ||
9bd7e6d9 PB |
4130 | static void version(void) |
4131 | { | |
4a19f1ec | 4132 | printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
9bd7e6d9 PB |
4133 | } |
4134 | ||
15f82208 | 4135 | static void help(int exitcode) |
0824d6fc | 4136 | { |
9bd7e6d9 PB |
4137 | version(); |
4138 | printf("usage: %s [options] [disk_image]\n" | |
0824d6fc | 4139 | "\n" |
a20dd508 | 4140 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" |
fc01f7e7 | 4141 | "\n" |
5824d651 BS |
4142 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
4143 | opt_help | |
4144 | #define DEFHEADING(text) stringify(text) "\n" | |
4145 | #include "qemu-options.h" | |
4146 | #undef DEF | |
4147 | #undef DEFHEADING | |
4148 | #undef GEN_DOCS | |
0824d6fc | 4149 | "\n" |
82c643ff | 4150 | "During emulation, the following keys are useful:\n" |
032a8c9e FB |
4151 | "ctrl-alt-f toggle full screen\n" |
4152 | "ctrl-alt-n switch to virtual console 'n'\n" | |
4153 | "ctrl-alt toggle mouse and keyboard grab\n" | |
82c643ff FB |
4154 | "\n" |
4155 | "When using -nographic, press 'ctrl-a h' to get some help.\n" | |
4156 | , | |
0db63474 | 4157 | "qemu", |
a00bad7e | 4158 | DEFAULT_RAM_SIZE, |
7c9d8e07 | 4159 | #ifndef _WIN32 |
a00bad7e | 4160 | DEFAULT_NETWORK_SCRIPT, |
b46a8906 | 4161 | DEFAULT_NETWORK_DOWN_SCRIPT, |
7c9d8e07 | 4162 | #endif |
6e44ba7f | 4163 | DEFAULT_GDBSTUB_PORT, |
bce61846 | 4164 | "/tmp/qemu.log"); |
15f82208 | 4165 | exit(exitcode); |
0824d6fc FB |
4166 | } |
4167 | ||
cd6f1169 FB |
4168 | #define HAS_ARG 0x0001 |
4169 | ||
4170 | enum { | |
5824d651 BS |
4171 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
4172 | opt_enum, | |
4173 | #define DEFHEADING(text) | |
4174 | #include "qemu-options.h" | |
4175 | #undef DEF | |
4176 | #undef DEFHEADING | |
4177 | #undef GEN_DOCS | |
cd6f1169 FB |
4178 | }; |
4179 | ||
4180 | typedef struct QEMUOption { | |
4181 | const char *name; | |
4182 | int flags; | |
4183 | int index; | |
4184 | } QEMUOption; | |
4185 | ||
dbed7e40 | 4186 | static const QEMUOption qemu_options[] = { |
cd6f1169 | 4187 | { "h", 0, QEMU_OPTION_h }, |
5824d651 BS |
4188 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
4189 | { option, opt_arg, opt_enum }, | |
4190 | #define DEFHEADING(text) | |
4191 | #include "qemu-options.h" | |
4192 | #undef DEF | |
4193 | #undef DEFHEADING | |
4194 | #undef GEN_DOCS | |
cd6f1169 | 4195 | { NULL }, |
fc01f7e7 FB |
4196 | }; |
4197 | ||
1d14ffa9 | 4198 | #ifdef HAS_AUDIO |
6a36d84e | 4199 | struct soundhw soundhw[] = { |
b00052e4 | 4200 | #ifdef HAS_AUDIO_CHOICE |
4ce7ff6e | 4201 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
fd06c375 FB |
4202 | { |
4203 | "pcspk", | |
4204 | "PC speaker", | |
4205 | 0, | |
4206 | 1, | |
4207 | { .init_isa = pcspk_audio_init } | |
4208 | }, | |
4209 | #endif | |
4c9b53e3 | 4210 | |
4211 | #ifdef CONFIG_SB16 | |
6a36d84e FB |
4212 | { |
4213 | "sb16", | |
4214 | "Creative Sound Blaster 16", | |
4215 | 0, | |
4216 | 1, | |
4217 | { .init_isa = SB16_init } | |
4218 | }, | |
4c9b53e3 | 4219 | #endif |
6a36d84e | 4220 | |
cc53d26d | 4221 | #ifdef CONFIG_CS4231A |
4222 | { | |
4223 | "cs4231a", | |
4224 | "CS4231A", | |
4225 | 0, | |
4226 | 1, | |
4227 | { .init_isa = cs4231a_init } | |
4228 | }, | |
4229 | #endif | |
4230 | ||
1d14ffa9 | 4231 | #ifdef CONFIG_ADLIB |
6a36d84e FB |
4232 | { |
4233 | "adlib", | |
1d14ffa9 | 4234 | #ifdef HAS_YMF262 |
6a36d84e | 4235 | "Yamaha YMF262 (OPL3)", |
1d14ffa9 | 4236 | #else |
6a36d84e | 4237 | "Yamaha YM3812 (OPL2)", |
1d14ffa9 | 4238 | #endif |
6a36d84e FB |
4239 | 0, |
4240 | 1, | |
4241 | { .init_isa = Adlib_init } | |
4242 | }, | |
1d14ffa9 | 4243 | #endif |
6a36d84e | 4244 | |
1d14ffa9 | 4245 | #ifdef CONFIG_GUS |
6a36d84e FB |
4246 | { |
4247 | "gus", | |
4248 | "Gravis Ultrasound GF1", | |
4249 | 0, | |
4250 | 1, | |
4251 | { .init_isa = GUS_init } | |
4252 | }, | |
1d14ffa9 | 4253 | #endif |
6a36d84e | 4254 | |
4c9b53e3 | 4255 | #ifdef CONFIG_AC97 |
e5c9a13e AZ |
4256 | { |
4257 | "ac97", | |
4258 | "Intel 82801AA AC97 Audio", | |
4259 | 0, | |
4260 | 0, | |
4261 | { .init_pci = ac97_init } | |
4262 | }, | |
4c9b53e3 | 4263 | #endif |
e5c9a13e | 4264 | |
4c9b53e3 | 4265 | #ifdef CONFIG_ES1370 |
6a36d84e FB |
4266 | { |
4267 | "es1370", | |
4268 | "ENSONIQ AudioPCI ES1370", | |
4269 | 0, | |
4270 | 0, | |
4271 | { .init_pci = es1370_init } | |
4272 | }, | |
b00052e4 | 4273 | #endif |
6a36d84e | 4274 | |
4c9b53e3 | 4275 | #endif /* HAS_AUDIO_CHOICE */ |
4276 | ||
6a36d84e FB |
4277 | { NULL, NULL, 0, 0, { NULL } } |
4278 | }; | |
4279 | ||
4280 | static void select_soundhw (const char *optarg) | |
4281 | { | |
4282 | struct soundhw *c; | |
4283 | ||
4284 | if (*optarg == '?') { | |
4285 | show_valid_cards: | |
4286 | ||
4287 | printf ("Valid sound card names (comma separated):\n"); | |
4288 | for (c = soundhw; c->name; ++c) { | |
4289 | printf ("%-11s %s\n", c->name, c->descr); | |
4290 | } | |
4291 | printf ("\n-soundhw all will enable all of the above\n"); | |
1d14ffa9 FB |
4292 | exit (*optarg != '?'); |
4293 | } | |
4294 | else { | |
6a36d84e | 4295 | size_t l; |
1d14ffa9 FB |
4296 | const char *p; |
4297 | char *e; | |
4298 | int bad_card = 0; | |
4299 | ||
6a36d84e FB |
4300 | if (!strcmp (optarg, "all")) { |
4301 | for (c = soundhw; c->name; ++c) { | |
4302 | c->enabled = 1; | |
4303 | } | |
4304 | return; | |
4305 | } | |
1d14ffa9 | 4306 | |
6a36d84e | 4307 | p = optarg; |
1d14ffa9 FB |
4308 | while (*p) { |
4309 | e = strchr (p, ','); | |
4310 | l = !e ? strlen (p) : (size_t) (e - p); | |
6a36d84e FB |
4311 | |
4312 | for (c = soundhw; c->name; ++c) { | |
b3d6fb4a | 4313 | if (!strncmp (c->name, p, l) && !c->name[l]) { |
6a36d84e | 4314 | c->enabled = 1; |
1d14ffa9 FB |
4315 | break; |
4316 | } | |
4317 | } | |
6a36d84e FB |
4318 | |
4319 | if (!c->name) { | |
1d14ffa9 FB |
4320 | if (l > 80) { |
4321 | fprintf (stderr, | |
4322 | "Unknown sound card name (too big to show)\n"); | |
4323 | } | |
4324 | else { | |
4325 | fprintf (stderr, "Unknown sound card name `%.*s'\n", | |
4326 | (int) l, p); | |
4327 | } | |
4328 | bad_card = 1; | |
4329 | } | |
4330 | p += l + (e != NULL); | |
4331 | } | |
4332 | ||
4333 | if (bad_card) | |
4334 | goto show_valid_cards; | |
4335 | } | |
4336 | } | |
4337 | #endif | |
4338 | ||
3893c124 | 4339 | static void select_vgahw (const char *p) |
4340 | { | |
4341 | const char *opts; | |
4342 | ||
86176759 | 4343 | vga_interface_type = VGA_NONE; |
3893c124 | 4344 | if (strstart(p, "std", &opts)) { |
86176759 | 4345 | vga_interface_type = VGA_STD; |
3893c124 | 4346 | } else if (strstart(p, "cirrus", &opts)) { |
86176759 | 4347 | vga_interface_type = VGA_CIRRUS; |
3893c124 | 4348 | } else if (strstart(p, "vmware", &opts)) { |
86176759 | 4349 | vga_interface_type = VGA_VMWARE; |
94909d9f | 4350 | } else if (strstart(p, "xenfb", &opts)) { |
86176759 | 4351 | vga_interface_type = VGA_XENFB; |
28b85ed8 | 4352 | } else if (!strstart(p, "none", &opts)) { |
3893c124 | 4353 | invalid_vga: |
4354 | fprintf(stderr, "Unknown vga type: %s\n", p); | |
4355 | exit(1); | |
4356 | } | |
cb5a7aa8 | 4357 | while (*opts) { |
4358 | const char *nextopt; | |
4359 | ||
4360 | if (strstart(opts, ",retrace=", &nextopt)) { | |
4361 | opts = nextopt; | |
4362 | if (strstart(opts, "dumb", &nextopt)) | |
4363 | vga_retrace_method = VGA_RETRACE_DUMB; | |
4364 | else if (strstart(opts, "precise", &nextopt)) | |
4365 | vga_retrace_method = VGA_RETRACE_PRECISE; | |
4366 | else goto invalid_vga; | |
4367 | } else goto invalid_vga; | |
4368 | opts = nextopt; | |
4369 | } | |
3893c124 | 4370 | } |
4371 | ||
7d4c3d53 MA |
4372 | #ifdef TARGET_I386 |
4373 | static int balloon_parse(const char *arg) | |
4374 | { | |
382f0743 | 4375 | QemuOpts *opts; |
7d4c3d53 | 4376 | |
382f0743 GH |
4377 | if (strcmp(arg, "none") == 0) { |
4378 | return 0; | |
4379 | } | |
4380 | ||
4381 | if (!strncmp(arg, "virtio", 6)) { | |
4382 | if (arg[6] == ',') { | |
4383 | /* have params -> parse them */ | |
4384 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); | |
4385 | if (!opts) | |
4386 | return -1; | |
4387 | } else { | |
4388 | /* create empty opts */ | |
4389 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); | |
7d4c3d53 | 4390 | } |
382f0743 GH |
4391 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
4392 | return 0; | |
7d4c3d53 | 4393 | } |
382f0743 GH |
4394 | |
4395 | return -1; | |
7d4c3d53 MA |
4396 | } |
4397 | #endif | |
4398 | ||
3587d7e6 FB |
4399 | #ifdef _WIN32 |
4400 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) | |
4401 | { | |
4402 | exit(STATUS_CONTROL_C_EXIT); | |
4403 | return TRUE; | |
4404 | } | |
4405 | #endif | |
4406 | ||
c4be29ff | 4407 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
8fcb1b90 BS |
4408 | { |
4409 | int ret; | |
4410 | ||
4411 | if(strlen(str) != 36) | |
4412 | return -1; | |
4413 | ||
4414 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
4415 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
4416 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); | |
4417 | ||
4418 | if(ret != 16) | |
4419 | return -1; | |
4420 | ||
b6f6e3d3 AL |
4421 | #ifdef TARGET_I386 |
4422 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); | |
4423 | #endif | |
4424 | ||
8fcb1b90 BS |
4425 | return 0; |
4426 | } | |
4427 | ||
7c9d8e07 | 4428 | #define MAX_NET_CLIENTS 32 |
c20709aa | 4429 | |
5b08fc10 AL |
4430 | #ifndef _WIN32 |
4431 | ||
4432 | static void termsig_handler(int signal) | |
4433 | { | |
4434 | qemu_system_shutdown_request(); | |
4435 | } | |
4436 | ||
7c3370d4 JK |
4437 | static void sigchld_handler(int signal) |
4438 | { | |
4439 | waitpid(-1, NULL, WNOHANG); | |
4440 | } | |
4441 | ||
4442 | static void sighandler_setup(void) | |
5b08fc10 AL |
4443 | { |
4444 | struct sigaction act; | |
4445 | ||
4446 | memset(&act, 0, sizeof(act)); | |
4447 | act.sa_handler = termsig_handler; | |
4448 | sigaction(SIGINT, &act, NULL); | |
4449 | sigaction(SIGHUP, &act, NULL); | |
4450 | sigaction(SIGTERM, &act, NULL); | |
7c3370d4 JK |
4451 | |
4452 | act.sa_handler = sigchld_handler; | |
4453 | act.sa_flags = SA_NOCLDSTOP; | |
4454 | sigaction(SIGCHLD, &act, NULL); | |
5b08fc10 AL |
4455 | } |
4456 | ||
4457 | #endif | |
4458 | ||
5cea8590 PB |
4459 | #ifdef _WIN32 |
4460 | /* Look for support files in the same directory as the executable. */ | |
4461 | static char *find_datadir(const char *argv0) | |
4462 | { | |
4463 | char *p; | |
4464 | char buf[MAX_PATH]; | |
4465 | DWORD len; | |
4466 | ||
4467 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); | |
4468 | if (len == 0) { | |
c5947808 | 4469 | return NULL; |
5cea8590 PB |
4470 | } |
4471 | ||
4472 | buf[len] = 0; | |
4473 | p = buf + len - 1; | |
4474 | while (p != buf && *p != '\\') | |
4475 | p--; | |
4476 | *p = 0; | |
4477 | if (access(buf, R_OK) == 0) { | |
4478 | return qemu_strdup(buf); | |
4479 | } | |
4480 | return NULL; | |
4481 | } | |
4482 | #else /* !_WIN32 */ | |
4483 | ||
4484 | /* Find a likely location for support files using the location of the binary. | |
4485 | For installed binaries this will be "$bindir/../share/qemu". When | |
4486 | running from the build tree this will be "$bindir/../pc-bios". */ | |
4487 | #define SHARE_SUFFIX "/share/qemu" | |
4488 | #define BUILD_SUFFIX "/pc-bios" | |
4489 | static char *find_datadir(const char *argv0) | |
4490 | { | |
4491 | char *dir; | |
4492 | char *p = NULL; | |
4493 | char *res; | |
5cea8590 | 4494 | char buf[PATH_MAX]; |
3a41759d | 4495 | size_t max_len; |
5cea8590 PB |
4496 | |
4497 | #if defined(__linux__) | |
4498 | { | |
4499 | int len; | |
4500 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); | |
4501 | if (len > 0) { | |
4502 | buf[len] = 0; | |
4503 | p = buf; | |
4504 | } | |
4505 | } | |
4506 | #elif defined(__FreeBSD__) | |
4507 | { | |
4508 | int len; | |
4509 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); | |
4510 | if (len > 0) { | |
4511 | buf[len] = 0; | |
4512 | p = buf; | |
4513 | } | |
4514 | } | |
4515 | #endif | |
4516 | /* If we don't have any way of figuring out the actual executable | |
4517 | location then try argv[0]. */ | |
4518 | if (!p) { | |
4d224196 | 4519 | p = realpath(argv0, buf); |
5cea8590 PB |
4520 | if (!p) { |
4521 | return NULL; | |
4522 | } | |
4523 | } | |
4524 | dir = dirname(p); | |
4525 | dir = dirname(dir); | |
4526 | ||
3a41759d BS |
4527 | max_len = strlen(dir) + |
4528 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; | |
4529 | res = qemu_mallocz(max_len); | |
4530 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); | |
5cea8590 | 4531 | if (access(res, R_OK)) { |
3a41759d | 4532 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
5cea8590 PB |
4533 | if (access(res, R_OK)) { |
4534 | qemu_free(res); | |
4535 | res = NULL; | |
4536 | } | |
4537 | } | |
4d224196 | 4538 | |
5cea8590 PB |
4539 | return res; |
4540 | } | |
4541 | #undef SHARE_SUFFIX | |
4542 | #undef BUILD_SUFFIX | |
4543 | #endif | |
4544 | ||
4545 | char *qemu_find_file(int type, const char *name) | |
4546 | { | |
4547 | int len; | |
4548 | const char *subdir; | |
4549 | char *buf; | |
4550 | ||
4551 | /* If name contains path separators then try it as a straight path. */ | |
4552 | if ((strchr(name, '/') || strchr(name, '\\')) | |
4553 | && access(name, R_OK) == 0) { | |
73ffc805 | 4554 | return qemu_strdup(name); |
5cea8590 PB |
4555 | } |
4556 | switch (type) { | |
4557 | case QEMU_FILE_TYPE_BIOS: | |
4558 | subdir = ""; | |
4559 | break; | |
4560 | case QEMU_FILE_TYPE_KEYMAP: | |
4561 | subdir = "keymaps/"; | |
4562 | break; | |
4563 | default: | |
4564 | abort(); | |
4565 | } | |
4566 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; | |
4567 | buf = qemu_mallocz(len); | |
3a41759d | 4568 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
5cea8590 PB |
4569 | if (access(buf, R_OK)) { |
4570 | qemu_free(buf); | |
4571 | return NULL; | |
4572 | } | |
4573 | return buf; | |
4574 | } | |
4575 | ||
f31d07d1 GH |
4576 | static int device_init_func(QemuOpts *opts, void *opaque) |
4577 | { | |
4578 | DeviceState *dev; | |
4579 | ||
4580 | dev = qdev_device_add(opts); | |
4581 | if (!dev) | |
4582 | return -1; | |
4583 | return 0; | |
4584 | } | |
4585 | ||
bd3c948d GH |
4586 | struct device_config { |
4587 | enum { | |
bd3c948d GH |
4588 | DEV_USB, /* -usbdevice */ |
4589 | DEV_BT, /* -bt */ | |
4590 | } type; | |
4591 | const char *cmdline; | |
4592 | TAILQ_ENTRY(device_config) next; | |
4593 | }; | |
4594 | TAILQ_HEAD(, device_config) device_configs = TAILQ_HEAD_INITIALIZER(device_configs); | |
4595 | ||
4596 | static void add_device_config(int type, const char *cmdline) | |
4597 | { | |
4598 | struct device_config *conf; | |
4599 | ||
4600 | conf = qemu_mallocz(sizeof(*conf)); | |
4601 | conf->type = type; | |
4602 | conf->cmdline = cmdline; | |
4603 | TAILQ_INSERT_TAIL(&device_configs, conf, next); | |
4604 | } | |
4605 | ||
4606 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) | |
4607 | { | |
4608 | struct device_config *conf; | |
4609 | int rc; | |
4610 | ||
4611 | TAILQ_FOREACH(conf, &device_configs, next) { | |
4612 | if (conf->type != type) | |
4613 | continue; | |
4614 | rc = func(conf->cmdline); | |
4615 | if (0 != rc) | |
4616 | return rc; | |
4617 | } | |
4618 | return 0; | |
4619 | } | |
4620 | ||
902b3d5c | 4621 | int main(int argc, char **argv, char **envp) |
0824d6fc | 4622 | { |
59030a8c | 4623 | const char *gdbstub_dev = NULL; |
28c5af54 | 4624 | uint32_t boot_devices_bitmap = 0; |
e4bcb14c | 4625 | int i; |
28c5af54 | 4626 | int snapshot, linux_boot, net_boot; |
7f7f9873 | 4627 | const char *initrd_filename; |
a20dd508 | 4628 | const char *kernel_filename, *kernel_cmdline; |
ef3adf68 | 4629 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
3023f332 | 4630 | DisplayState *ds; |
7d957bd8 | 4631 | DisplayChangeListener *dcl; |
46d4767d | 4632 | int cyls, heads, secs, translation; |
fd5f393a | 4633 | const char *net_clients[MAX_NET_CLIENTS]; |
7c9d8e07 | 4634 | int nb_net_clients; |
f31d07d1 | 4635 | QemuOpts *hda_opts = NULL, *opts; |
cd6f1169 FB |
4636 | int optind; |
4637 | const char *r, *optarg; | |
ddd9bbd9 JK |
4638 | CharDriverState *monitor_hds[MAX_MONITOR_DEVICES]; |
4639 | const char *monitor_devices[MAX_MONITOR_DEVICES]; | |
4640 | int monitor_device_index; | |
fd5f393a | 4641 | const char *serial_devices[MAX_SERIAL_PORTS]; |
8d11df9e | 4642 | int serial_device_index; |
fd5f393a | 4643 | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
6508fe59 | 4644 | int parallel_device_index; |
9ede2fde AL |
4645 | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
4646 | int virtio_console_index; | |
d63d307f | 4647 | const char *loadvm = NULL; |
cc1daa40 | 4648 | QEMUMachine *machine; |
94fc95cd | 4649 | const char *cpu_model; |
b9e82a59 | 4650 | #ifndef _WIN32 |
71e3ceb8 | 4651 | int fds[2]; |
b9e82a59 | 4652 | #endif |
26a5f13b | 4653 | int tb_size; |
93815bc2 | 4654 | const char *pid_file = NULL; |
5bb7910a | 4655 | const char *incoming = NULL; |
b9e82a59 | 4656 | #ifndef _WIN32 |
54042bcf AL |
4657 | int fd = 0; |
4658 | struct passwd *pwd = NULL; | |
0858532e AL |
4659 | const char *chroot_dir = NULL; |
4660 | const char *run_as = NULL; | |
b9e82a59 | 4661 | #endif |
268a362c | 4662 | CPUState *env; |
993fbfdb | 4663 | int show_vnc_port = 0; |
0bd48850 | 4664 | |
ac7531ec | 4665 | qemu_errors_to_file(stderr); |
902b3d5c | 4666 | qemu_cache_utils_init(envp); |
4667 | ||
0bd48850 | 4668 | LIST_INIT (&vm_change_state_head); |
be995c27 FB |
4669 | #ifndef _WIN32 |
4670 | { | |
4671 | struct sigaction act; | |
4672 | sigfillset(&act.sa_mask); | |
4673 | act.sa_flags = 0; | |
4674 | act.sa_handler = SIG_IGN; | |
4675 | sigaction(SIGPIPE, &act, NULL); | |
4676 | } | |
3587d7e6 FB |
4677 | #else |
4678 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); | |
a8e5ac33 FB |
4679 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
4680 | QEMU to run on a single CPU */ | |
4681 | { | |
4682 | HANDLE h; | |
4683 | DWORD mask, smask; | |
4684 | int i; | |
4685 | h = GetCurrentProcess(); | |
4686 | if (GetProcessAffinityMask(h, &mask, &smask)) { | |
4687 | for(i = 0; i < 32; i++) { | |
4688 | if (mask & (1 << i)) | |
4689 | break; | |
4690 | } | |
4691 | if (i != 32) { | |
4692 | mask = 1 << i; | |
4693 | SetProcessAffinityMask(h, mask); | |
4694 | } | |
4695 | } | |
4696 | } | |
67b915a5 | 4697 | #endif |
be995c27 | 4698 | |
f80f9ec9 | 4699 | module_call_init(MODULE_INIT_MACHINE); |
0c257437 | 4700 | machine = find_default_machine(); |
94fc95cd | 4701 | cpu_model = NULL; |
fc01f7e7 | 4702 | initrd_filename = NULL; |
4fc5d071 | 4703 | ram_size = 0; |
33e3963e | 4704 | snapshot = 0; |
a20dd508 FB |
4705 | kernel_filename = NULL; |
4706 | kernel_cmdline = ""; | |
c4b1fcc0 | 4707 | cyls = heads = secs = 0; |
46d4767d | 4708 | translation = BIOS_ATA_TRANSLATION_AUTO; |
c4b1fcc0 | 4709 | |
c75a823c | 4710 | serial_devices[0] = "vc:80Cx24C"; |
8d11df9e | 4711 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
fd5f393a | 4712 | serial_devices[i] = NULL; |
8d11df9e | 4713 | serial_device_index = 0; |
3b46e624 | 4714 | |
8290edda | 4715 | parallel_devices[0] = "vc:80Cx24C"; |
6508fe59 | 4716 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
fd5f393a | 4717 | parallel_devices[i] = NULL; |
6508fe59 | 4718 | parallel_device_index = 0; |
3b46e624 | 4719 | |
1b8fc811 | 4720 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
9ede2fde AL |
4721 | virtio_consoles[i] = NULL; |
4722 | virtio_console_index = 0; | |
4723 | ||
ddd9bbd9 JK |
4724 | monitor_devices[0] = "vc:80Cx24C"; |
4725 | for (i = 1; i < MAX_MONITOR_DEVICES; i++) { | |
4726 | monitor_devices[i] = NULL; | |
4727 | } | |
4728 | monitor_device_index = 0; | |
4729 | ||
268a362c AL |
4730 | for (i = 0; i < MAX_NODES; i++) { |
4731 | node_mem[i] = 0; | |
4732 | node_cpumask[i] = 0; | |
4733 | } | |
4734 | ||
7c9d8e07 | 4735 | nb_net_clients = 0; |
268a362c | 4736 | nb_numa_nodes = 0; |
7c9d8e07 | 4737 | nb_nics = 0; |
3b46e624 | 4738 | |
26a5f13b | 4739 | tb_size = 0; |
41bd639b BS |
4740 | autostart= 1; |
4741 | ||
cd6f1169 | 4742 | optind = 1; |
0824d6fc | 4743 | for(;;) { |
cd6f1169 | 4744 | if (optind >= argc) |
0824d6fc | 4745 | break; |
cd6f1169 FB |
4746 | r = argv[optind]; |
4747 | if (r[0] != '-') { | |
9dfd7c7a | 4748 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
cd6f1169 FB |
4749 | } else { |
4750 | const QEMUOption *popt; | |
4751 | ||
4752 | optind++; | |
dff5efc8 PB |
4753 | /* Treat --foo the same as -foo. */ |
4754 | if (r[1] == '-') | |
4755 | r++; | |
cd6f1169 FB |
4756 | popt = qemu_options; |
4757 | for(;;) { | |
4758 | if (!popt->name) { | |
5fafdf24 | 4759 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
cd6f1169 FB |
4760 | argv[0], r); |
4761 | exit(1); | |
4762 | } | |
4763 | if (!strcmp(popt->name, r + 1)) | |
4764 | break; | |
4765 | popt++; | |
4766 | } | |
4767 | if (popt->flags & HAS_ARG) { | |
4768 | if (optind >= argc) { | |
4769 | fprintf(stderr, "%s: option '%s' requires an argument\n", | |
4770 | argv[0], r); | |
4771 | exit(1); | |
4772 | } | |
4773 | optarg = argv[optind++]; | |
4774 | } else { | |
4775 | optarg = NULL; | |
4776 | } | |
4777 | ||
4778 | switch(popt->index) { | |
cc1daa40 FB |
4779 | case QEMU_OPTION_M: |
4780 | machine = find_machine(optarg); | |
4781 | if (!machine) { | |
4782 | QEMUMachine *m; | |
4783 | printf("Supported machines are:\n"); | |
4784 | for(m = first_machine; m != NULL; m = m->next) { | |
3f6599e6 MM |
4785 | if (m->alias) |
4786 | printf("%-10s %s (alias of %s)\n", | |
4787 | m->alias, m->desc, m->name); | |
cc1daa40 | 4788 | printf("%-10s %s%s\n", |
5fafdf24 | 4789 | m->name, m->desc, |
0c257437 | 4790 | m->is_default ? " (default)" : ""); |
cc1daa40 | 4791 | } |
15f82208 | 4792 | exit(*optarg != '?'); |
cc1daa40 FB |
4793 | } |
4794 | break; | |
94fc95cd JM |
4795 | case QEMU_OPTION_cpu: |
4796 | /* hw initialization will check this */ | |
15f82208 | 4797 | if (*optarg == '?') { |
c732abe2 JM |
4798 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
4799 | #if defined(cpu_list) | |
4800 | cpu_list(stdout, &fprintf); | |
94fc95cd | 4801 | #endif |
15f82208 | 4802 | exit(0); |
94fc95cd JM |
4803 | } else { |
4804 | cpu_model = optarg; | |
4805 | } | |
4806 | break; | |
cd6f1169 | 4807 | case QEMU_OPTION_initrd: |
fc01f7e7 FB |
4808 | initrd_filename = optarg; |
4809 | break; | |
cd6f1169 | 4810 | case QEMU_OPTION_hda: |
e4bcb14c | 4811 | if (cyls == 0) |
9dfd7c7a | 4812 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
e4bcb14c | 4813 | else |
9dfd7c7a | 4814 | hda_opts = drive_add(optarg, HD_ALIAS |
e4bcb14c | 4815 | ",cyls=%d,heads=%d,secs=%d%s", |
609497ab | 4816 | 0, cyls, heads, secs, |
e4bcb14c TS |
4817 | translation == BIOS_ATA_TRANSLATION_LBA ? |
4818 | ",trans=lba" : | |
4819 | translation == BIOS_ATA_TRANSLATION_NONE ? | |
4820 | ",trans=none" : ""); | |
4821 | break; | |
cd6f1169 | 4822 | case QEMU_OPTION_hdb: |
cc1daa40 FB |
4823 | case QEMU_OPTION_hdc: |
4824 | case QEMU_OPTION_hdd: | |
609497ab | 4825 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
fc01f7e7 | 4826 | break; |
e4bcb14c | 4827 | case QEMU_OPTION_drive: |
609497ab | 4828 | drive_add(NULL, "%s", optarg); |
e4bcb14c | 4829 | break; |
d058fe03 GH |
4830 | case QEMU_OPTION_set: |
4831 | if (qemu_set_option(optarg) != 0) | |
4832 | exit(1); | |
4833 | break; | |
3e3d5815 | 4834 | case QEMU_OPTION_mtdblock: |
609497ab | 4835 | drive_add(optarg, MTD_ALIAS); |
3e3d5815 | 4836 | break; |
a1bb27b1 | 4837 | case QEMU_OPTION_sd: |
609497ab | 4838 | drive_add(optarg, SD_ALIAS); |
a1bb27b1 | 4839 | break; |
86f55663 | 4840 | case QEMU_OPTION_pflash: |
609497ab | 4841 | drive_add(optarg, PFLASH_ALIAS); |
86f55663 | 4842 | break; |
cd6f1169 | 4843 | case QEMU_OPTION_snapshot: |
33e3963e FB |
4844 | snapshot = 1; |
4845 | break; | |
cd6f1169 | 4846 | case QEMU_OPTION_hdachs: |
330d0414 | 4847 | { |
330d0414 FB |
4848 | const char *p; |
4849 | p = optarg; | |
4850 | cyls = strtol(p, (char **)&p, 0); | |
46d4767d FB |
4851 | if (cyls < 1 || cyls > 16383) |
4852 | goto chs_fail; | |
330d0414 FB |
4853 | if (*p != ',') |
4854 | goto chs_fail; | |
4855 | p++; | |
4856 | heads = strtol(p, (char **)&p, 0); | |
46d4767d FB |
4857 | if (heads < 1 || heads > 16) |
4858 | goto chs_fail; | |
330d0414 FB |
4859 | if (*p != ',') |
4860 | goto chs_fail; | |
4861 | p++; | |
4862 | secs = strtol(p, (char **)&p, 0); | |
46d4767d FB |
4863 | if (secs < 1 || secs > 63) |
4864 | goto chs_fail; | |
4865 | if (*p == ',') { | |
4866 | p++; | |
4867 | if (!strcmp(p, "none")) | |
4868 | translation = BIOS_ATA_TRANSLATION_NONE; | |
4869 | else if (!strcmp(p, "lba")) | |
4870 | translation = BIOS_ATA_TRANSLATION_LBA; | |
4871 | else if (!strcmp(p, "auto")) | |
4872 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
4873 | else | |
4874 | goto chs_fail; | |
4875 | } else if (*p != '\0') { | |
c4b1fcc0 | 4876 | chs_fail: |
46d4767d FB |
4877 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
4878 | exit(1); | |
c4b1fcc0 | 4879 | } |
9dfd7c7a GH |
4880 | if (hda_opts != NULL) { |
4881 | char num[16]; | |
4882 | snprintf(num, sizeof(num), "%d", cyls); | |
4883 | qemu_opt_set(hda_opts, "cyls", num); | |
4884 | snprintf(num, sizeof(num), "%d", heads); | |
4885 | qemu_opt_set(hda_opts, "heads", num); | |
4886 | snprintf(num, sizeof(num), "%d", secs); | |
4887 | qemu_opt_set(hda_opts, "secs", num); | |
4888 | if (translation == BIOS_ATA_TRANSLATION_LBA) | |
4889 | qemu_opt_set(hda_opts, "trans", "lba"); | |
4890 | if (translation == BIOS_ATA_TRANSLATION_NONE) | |
4891 | qemu_opt_set(hda_opts, "trans", "none"); | |
4892 | } | |
330d0414 FB |
4893 | } |
4894 | break; | |
268a362c AL |
4895 | case QEMU_OPTION_numa: |
4896 | if (nb_numa_nodes >= MAX_NODES) { | |
4897 | fprintf(stderr, "qemu: too many NUMA nodes\n"); | |
4898 | exit(1); | |
4899 | } | |
4900 | numa_add(optarg); | |
4901 | break; | |
cd6f1169 | 4902 | case QEMU_OPTION_nographic: |
993fbfdb | 4903 | display_type = DT_NOGRAPHIC; |
a20dd508 | 4904 | break; |
4d3b6f6e AZ |
4905 | #ifdef CONFIG_CURSES |
4906 | case QEMU_OPTION_curses: | |
993fbfdb | 4907 | display_type = DT_CURSES; |
4d3b6f6e AZ |
4908 | break; |
4909 | #endif | |
a171fe39 AZ |
4910 | case QEMU_OPTION_portrait: |
4911 | graphic_rotate = 1; | |
4912 | break; | |
cd6f1169 | 4913 | case QEMU_OPTION_kernel: |
a20dd508 FB |
4914 | kernel_filename = optarg; |
4915 | break; | |
cd6f1169 | 4916 | case QEMU_OPTION_append: |
a20dd508 | 4917 | kernel_cmdline = optarg; |
313aa567 | 4918 | break; |
cd6f1169 | 4919 | case QEMU_OPTION_cdrom: |
609497ab | 4920 | drive_add(optarg, CDROM_ALIAS); |
36b486bb | 4921 | break; |
cd6f1169 | 4922 | case QEMU_OPTION_boot: |
28c5af54 | 4923 | { |
ef3adf68 | 4924 | static const char * const params[] = { |
95387491 | 4925 | "order", "once", "menu", NULL |
ef3adf68 JK |
4926 | }; |
4927 | char buf[sizeof(boot_devices)]; | |
e0f084bf | 4928 | char *standard_boot_devices; |
ef3adf68 JK |
4929 | int legacy = 0; |
4930 | ||
4931 | if (!strchr(optarg, '=')) { | |
4932 | legacy = 1; | |
4933 | pstrcpy(buf, sizeof(buf), optarg); | |
4934 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { | |
4935 | fprintf(stderr, | |
4936 | "qemu: unknown boot parameter '%s' in '%s'\n", | |
4937 | buf, optarg); | |
4938 | exit(1); | |
4939 | } | |
4940 | ||
4941 | if (legacy || | |
4942 | get_param_value(buf, sizeof(buf), "order", optarg)) { | |
4943 | boot_devices_bitmap = parse_bootdevices(buf); | |
4944 | pstrcpy(boot_devices, sizeof(boot_devices), buf); | |
28c5af54 | 4945 | } |
e0f084bf JK |
4946 | if (!legacy) { |
4947 | if (get_param_value(buf, sizeof(buf), | |
4948 | "once", optarg)) { | |
4949 | boot_devices_bitmap |= parse_bootdevices(buf); | |
4950 | standard_boot_devices = qemu_strdup(boot_devices); | |
4951 | pstrcpy(boot_devices, sizeof(boot_devices), buf); | |
4952 | qemu_register_reset(restore_boot_devices, | |
4953 | standard_boot_devices); | |
4954 | } | |
95387491 JK |
4955 | if (get_param_value(buf, sizeof(buf), |
4956 | "menu", optarg)) { | |
4957 | if (!strcmp(buf, "on")) { | |
4958 | boot_menu = 1; | |
4959 | } else if (!strcmp(buf, "off")) { | |
4960 | boot_menu = 0; | |
4961 | } else { | |
4962 | fprintf(stderr, | |
4963 | "qemu: invalid option value '%s'\n", | |
4964 | buf); | |
4965 | exit(1); | |
4966 | } | |
4967 | } | |
e0f084bf | 4968 | } |
36b486bb FB |
4969 | } |
4970 | break; | |
cd6f1169 | 4971 | case QEMU_OPTION_fda: |
cd6f1169 | 4972 | case QEMU_OPTION_fdb: |
609497ab | 4973 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
c45886db | 4974 | break; |
52ca8d6a FB |
4975 | #ifdef TARGET_I386 |
4976 | case QEMU_OPTION_no_fd_bootchk: | |
4977 | fd_bootchk = 0; | |
4978 | break; | |
4979 | #endif | |
7c9d8e07 FB |
4980 | case QEMU_OPTION_net: |
4981 | if (nb_net_clients >= MAX_NET_CLIENTS) { | |
4982 | fprintf(stderr, "qemu: too many network clients\n"); | |
c4b1fcc0 FB |
4983 | exit(1); |
4984 | } | |
fd5f393a | 4985 | net_clients[nb_net_clients] = optarg; |
7c9d8e07 | 4986 | nb_net_clients++; |
702c651c | 4987 | break; |
c7f74643 FB |
4988 | #ifdef CONFIG_SLIRP |
4989 | case QEMU_OPTION_tftp: | |
ad196a9d | 4990 | legacy_tftp_prefix = optarg; |
9bf05444 | 4991 | break; |
47d5d01a | 4992 | case QEMU_OPTION_bootp: |
ad196a9d | 4993 | legacy_bootp_filename = optarg; |
47d5d01a | 4994 | break; |
c94c8d64 | 4995 | #ifndef _WIN32 |
9d728e8c | 4996 | case QEMU_OPTION_smb: |
ad196a9d | 4997 | net_slirp_smb(optarg); |
9d728e8c | 4998 | break; |
c94c8d64 | 4999 | #endif |
9bf05444 | 5000 | case QEMU_OPTION_redir: |
f3546deb | 5001 | net_slirp_redir(optarg); |
9bf05444 | 5002 | break; |
c7f74643 | 5003 | #endif |
dc72ac14 | 5004 | case QEMU_OPTION_bt: |
bd3c948d | 5005 | add_device_config(DEV_BT, optarg); |
dc72ac14 | 5006 | break; |
1d14ffa9 | 5007 | #ifdef HAS_AUDIO |
1d14ffa9 FB |
5008 | case QEMU_OPTION_audio_help: |
5009 | AUD_help (); | |
5010 | exit (0); | |
5011 | break; | |
5012 | case QEMU_OPTION_soundhw: | |
5013 | select_soundhw (optarg); | |
5014 | break; | |
5015 | #endif | |
cd6f1169 | 5016 | case QEMU_OPTION_h: |
15f82208 | 5017 | help(0); |
cd6f1169 | 5018 | break; |
9bd7e6d9 PB |
5019 | case QEMU_OPTION_version: |
5020 | version(); | |
5021 | exit(0); | |
5022 | break; | |
00f82b8a AJ |
5023 | case QEMU_OPTION_m: { |
5024 | uint64_t value; | |
5025 | char *ptr; | |
5026 | ||
5027 | value = strtoul(optarg, &ptr, 10); | |
5028 | switch (*ptr) { | |
5029 | case 0: case 'M': case 'm': | |
5030 | value <<= 20; | |
5031 | break; | |
5032 | case 'G': case 'g': | |
5033 | value <<= 30; | |
5034 | break; | |
5035 | default: | |
5036 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); | |
cd6f1169 FB |
5037 | exit(1); |
5038 | } | |
00f82b8a AJ |
5039 | |
5040 | /* On 32-bit hosts, QEMU is limited by virtual address space */ | |
4a1418e0 | 5041 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
00f82b8a AJ |
5042 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
5043 | exit(1); | |
5044 | } | |
5045 | if (value != (uint64_t)(ram_addr_t)value) { | |
5046 | fprintf(stderr, "qemu: ram size too large\n"); | |
5047 | exit(1); | |
5048 | } | |
5049 | ram_size = value; | |
cd6f1169 | 5050 | break; |
00f82b8a | 5051 | } |
cd6f1169 FB |
5052 | case QEMU_OPTION_d: |
5053 | { | |
5054 | int mask; | |
c7cd6a37 | 5055 | const CPULogItem *item; |
3b46e624 | 5056 | |
cd6f1169 FB |
5057 | mask = cpu_str_to_log_mask(optarg); |
5058 | if (!mask) { | |
5059 | printf("Log items (comma separated):\n"); | |
f193c797 FB |
5060 | for(item = cpu_log_items; item->mask != 0; item++) { |
5061 | printf("%-10s %s\n", item->name, item->help); | |
5062 | } | |
5063 | exit(1); | |
cd6f1169 FB |
5064 | } |
5065 | cpu_set_log(mask); | |
f193c797 | 5066 | } |
cd6f1169 | 5067 | break; |
cd6f1169 | 5068 | case QEMU_OPTION_s: |
59030a8c | 5069 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
cd6f1169 | 5070 | break; |
59030a8c AL |
5071 | case QEMU_OPTION_gdb: |
5072 | gdbstub_dev = optarg; | |
cd6f1169 | 5073 | break; |
cd6f1169 | 5074 | case QEMU_OPTION_L: |
5cea8590 | 5075 | data_dir = optarg; |
cd6f1169 | 5076 | break; |
1192dad8 JM |
5077 | case QEMU_OPTION_bios: |
5078 | bios_name = optarg; | |
5079 | break; | |
1b530a6d AJ |
5080 | case QEMU_OPTION_singlestep: |
5081 | singlestep = 1; | |
5082 | break; | |
cd6f1169 | 5083 | case QEMU_OPTION_S: |
3c07f8e8 | 5084 | autostart = 0; |
cd6f1169 | 5085 | break; |
5824d651 | 5086 | #ifndef _WIN32 |
3d11d0eb FB |
5087 | case QEMU_OPTION_k: |
5088 | keyboard_layout = optarg; | |
5089 | break; | |
5824d651 | 5090 | #endif |
ee22c2f7 FB |
5091 | case QEMU_OPTION_localtime: |
5092 | rtc_utc = 0; | |
5093 | break; | |
3893c124 | 5094 | case QEMU_OPTION_vga: |
5095 | select_vgahw (optarg); | |
1bfe856e | 5096 | break; |
5824d651 | 5097 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
e9b137c2 FB |
5098 | case QEMU_OPTION_g: |
5099 | { | |
5100 | const char *p; | |
5101 | int w, h, depth; | |
5102 | p = optarg; | |
5103 | w = strtol(p, (char **)&p, 10); | |
5104 | if (w <= 0) { | |
5105 | graphic_error: | |
5106 | fprintf(stderr, "qemu: invalid resolution or depth\n"); | |
5107 | exit(1); | |
5108 | } | |
5109 | if (*p != 'x') | |
5110 | goto graphic_error; | |
5111 | p++; | |
5112 | h = strtol(p, (char **)&p, 10); | |
5113 | if (h <= 0) | |
5114 | goto graphic_error; | |
5115 | if (*p == 'x') { | |
5116 | p++; | |
5117 | depth = strtol(p, (char **)&p, 10); | |
5fafdf24 | 5118 | if (depth != 8 && depth != 15 && depth != 16 && |
e9b137c2 FB |
5119 | depth != 24 && depth != 32) |
5120 | goto graphic_error; | |
5121 | } else if (*p == '\0') { | |
5122 | depth = graphic_depth; | |
5123 | } else { | |
5124 | goto graphic_error; | |
5125 | } | |
3b46e624 | 5126 | |
e9b137c2 FB |
5127 | graphic_width = w; |
5128 | graphic_height = h; | |
5129 | graphic_depth = depth; | |
5130 | } | |
5131 | break; | |
5824d651 | 5132 | #endif |
20d8a3ed TS |
5133 | case QEMU_OPTION_echr: |
5134 | { | |
5135 | char *r; | |
5136 | term_escape_char = strtol(optarg, &r, 0); | |
5137 | if (r == optarg) | |
5138 | printf("Bad argument to echr\n"); | |
5139 | break; | |
5140 | } | |
82c643ff | 5141 | case QEMU_OPTION_monitor: |
ddd9bbd9 JK |
5142 | if (monitor_device_index >= MAX_MONITOR_DEVICES) { |
5143 | fprintf(stderr, "qemu: too many monitor devices\n"); | |
5144 | exit(1); | |
5145 | } | |
5146 | monitor_devices[monitor_device_index] = optarg; | |
5147 | monitor_device_index++; | |
82c643ff | 5148 | break; |
191bc01b GH |
5149 | case QEMU_OPTION_chardev: |
5150 | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend"); | |
5151 | if (!opts) { | |
5152 | fprintf(stderr, "parse error: %s\n", optarg); | |
5153 | exit(1); | |
5154 | } | |
5155 | if (NULL == qemu_chr_open_opts(opts, NULL)) { | |
5156 | exit(1); | |
5157 | } | |
5158 | break; | |
82c643ff | 5159 | case QEMU_OPTION_serial: |
8d11df9e FB |
5160 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
5161 | fprintf(stderr, "qemu: too many serial ports\n"); | |
5162 | exit(1); | |
5163 | } | |
fd5f393a | 5164 | serial_devices[serial_device_index] = optarg; |
8d11df9e | 5165 | serial_device_index++; |
82c643ff | 5166 | break; |
9dd986cc | 5167 | case QEMU_OPTION_watchdog: |
09aaa160 MA |
5168 | if (watchdog) { |
5169 | fprintf(stderr, | |
5170 | "qemu: only one watchdog option may be given\n"); | |
5171 | return 1; | |
5172 | } | |
5173 | watchdog = optarg; | |
9dd986cc RJ |
5174 | break; |
5175 | case QEMU_OPTION_watchdog_action: | |
5176 | if (select_watchdog_action(optarg) == -1) { | |
5177 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); | |
5178 | exit(1); | |
5179 | } | |
5180 | break; | |
51ecf136 AL |
5181 | case QEMU_OPTION_virtiocon: |
5182 | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) { | |
5183 | fprintf(stderr, "qemu: too many virtio consoles\n"); | |
5184 | exit(1); | |
5185 | } | |
5186 | virtio_consoles[virtio_console_index] = optarg; | |
5187 | virtio_console_index++; | |
5188 | break; | |
6508fe59 FB |
5189 | case QEMU_OPTION_parallel: |
5190 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { | |
5191 | fprintf(stderr, "qemu: too many parallel ports\n"); | |
5192 | exit(1); | |
5193 | } | |
fd5f393a | 5194 | parallel_devices[parallel_device_index] = optarg; |
6508fe59 FB |
5195 | parallel_device_index++; |
5196 | break; | |
d63d307f FB |
5197 | case QEMU_OPTION_loadvm: |
5198 | loadvm = optarg; | |
5199 | break; | |
5200 | case QEMU_OPTION_full_screen: | |
5201 | full_screen = 1; | |
5202 | break; | |
667accab | 5203 | #ifdef CONFIG_SDL |
43523e93 TS |
5204 | case QEMU_OPTION_no_frame: |
5205 | no_frame = 1; | |
5206 | break; | |
3780e197 TS |
5207 | case QEMU_OPTION_alt_grab: |
5208 | alt_grab = 1; | |
5209 | break; | |
667accab TS |
5210 | case QEMU_OPTION_no_quit: |
5211 | no_quit = 1; | |
5212 | break; | |
7d957bd8 | 5213 | case QEMU_OPTION_sdl: |
993fbfdb | 5214 | display_type = DT_SDL; |
7d957bd8 | 5215 | break; |
667accab | 5216 | #endif |
f7cce898 | 5217 | case QEMU_OPTION_pidfile: |
93815bc2 | 5218 | pid_file = optarg; |
f7cce898 | 5219 | break; |
a09db21f FB |
5220 | #ifdef TARGET_I386 |
5221 | case QEMU_OPTION_win2k_hack: | |
5222 | win2k_install_hack = 1; | |
5223 | break; | |
73822ec8 AL |
5224 | case QEMU_OPTION_rtc_td_hack: |
5225 | rtc_td_hack = 1; | |
5226 | break; | |
8a92ea2f AL |
5227 | case QEMU_OPTION_acpitable: |
5228 | if(acpi_table_add(optarg) < 0) { | |
5229 | fprintf(stderr, "Wrong acpi table provided\n"); | |
5230 | exit(1); | |
5231 | } | |
5232 | break; | |
b6f6e3d3 AL |
5233 | case QEMU_OPTION_smbios: |
5234 | if(smbios_entry_add(optarg) < 0) { | |
5235 | fprintf(stderr, "Wrong smbios provided\n"); | |
5236 | exit(1); | |
5237 | } | |
5238 | break; | |
a09db21f | 5239 | #endif |
7ba1e619 AL |
5240 | #ifdef CONFIG_KVM |
5241 | case QEMU_OPTION_enable_kvm: | |
5242 | kvm_allowed = 1; | |
7ba1e619 | 5243 | break; |
d993e026 | 5244 | #endif |
bb36d470 FB |
5245 | case QEMU_OPTION_usb: |
5246 | usb_enabled = 1; | |
5247 | break; | |
a594cfbf FB |
5248 | case QEMU_OPTION_usbdevice: |
5249 | usb_enabled = 1; | |
bd3c948d GH |
5250 | add_device_config(DEV_USB, optarg); |
5251 | break; | |
5252 | case QEMU_OPTION_device: | |
f31d07d1 GH |
5253 | opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver"); |
5254 | if (!opts) { | |
5255 | fprintf(stderr, "parse error: %s\n", optarg); | |
5256 | exit(1); | |
5257 | } | |
a594cfbf | 5258 | break; |
6a00d601 | 5259 | case QEMU_OPTION_smp: |
dc6b1c09 | 5260 | smp_parse(optarg); |
b2097003 | 5261 | if (smp_cpus < 1) { |
6a00d601 FB |
5262 | fprintf(stderr, "Invalid number of CPUs\n"); |
5263 | exit(1); | |
5264 | } | |
6be68d7e JS |
5265 | if (max_cpus < smp_cpus) { |
5266 | fprintf(stderr, "maxcpus must be equal to or greater than " | |
5267 | "smp\n"); | |
5268 | exit(1); | |
5269 | } | |
5270 | if (max_cpus > 255) { | |
5271 | fprintf(stderr, "Unsupported number of maxcpus\n"); | |
5272 | exit(1); | |
5273 | } | |
6a00d601 | 5274 | break; |
24236869 | 5275 | case QEMU_OPTION_vnc: |
993fbfdb | 5276 | display_type = DT_VNC; |
73fc9742 | 5277 | vnc_display = optarg; |
24236869 | 5278 | break; |
5824d651 | 5279 | #ifdef TARGET_I386 |
6515b203 FB |
5280 | case QEMU_OPTION_no_acpi: |
5281 | acpi_enabled = 0; | |
5282 | break; | |
16b29ae1 AL |
5283 | case QEMU_OPTION_no_hpet: |
5284 | no_hpet = 1; | |
5285 | break; | |
7d4c3d53 MA |
5286 | case QEMU_OPTION_balloon: |
5287 | if (balloon_parse(optarg) < 0) { | |
5288 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); | |
5289 | exit(1); | |
5290 | } | |
df97b920 | 5291 | break; |
5824d651 | 5292 | #endif |
d1beab82 FB |
5293 | case QEMU_OPTION_no_reboot: |
5294 | no_reboot = 1; | |
5295 | break; | |
b2f76161 AJ |
5296 | case QEMU_OPTION_no_shutdown: |
5297 | no_shutdown = 1; | |
5298 | break; | |
9467cd46 AZ |
5299 | case QEMU_OPTION_show_cursor: |
5300 | cursor_hide = 0; | |
5301 | break; | |
8fcb1b90 BS |
5302 | case QEMU_OPTION_uuid: |
5303 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { | |
5304 | fprintf(stderr, "Fail to parse UUID string." | |
5305 | " Wrong format.\n"); | |
5306 | exit(1); | |
5307 | } | |
5308 | break; | |
5824d651 | 5309 | #ifndef _WIN32 |
71e3ceb8 TS |
5310 | case QEMU_OPTION_daemonize: |
5311 | daemonize = 1; | |
5312 | break; | |
5824d651 | 5313 | #endif |
9ae02555 TS |
5314 | case QEMU_OPTION_option_rom: |
5315 | if (nb_option_roms >= MAX_OPTION_ROMS) { | |
5316 | fprintf(stderr, "Too many option ROMs\n"); | |
5317 | exit(1); | |
5318 | } | |
5319 | option_rom[nb_option_roms] = optarg; | |
5320 | nb_option_roms++; | |
5321 | break; | |
5824d651 | 5322 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
8e71621f PB |
5323 | case QEMU_OPTION_semihosting: |
5324 | semihosting_enabled = 1; | |
5325 | break; | |
5824d651 | 5326 | #endif |
c35734b2 | 5327 | case QEMU_OPTION_name: |
1889465a AK |
5328 | qemu_name = qemu_strdup(optarg); |
5329 | { | |
5330 | char *p = strchr(qemu_name, ','); | |
5331 | if (p != NULL) { | |
5332 | *p++ = 0; | |
5333 | if (strncmp(p, "process=", 8)) { | |
5334 | fprintf(stderr, "Unknown subargument %s to -name", p); | |
5335 | exit(1); | |
5336 | } | |
5337 | p += 8; | |
5338 | set_proc_name(p); | |
5339 | } | |
5340 | } | |
c35734b2 | 5341 | break; |
95efd11c | 5342 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
66508601 BS |
5343 | case QEMU_OPTION_prom_env: |
5344 | if (nb_prom_envs >= MAX_PROM_ENVS) { | |
5345 | fprintf(stderr, "Too many prom variables\n"); | |
5346 | exit(1); | |
5347 | } | |
5348 | prom_envs[nb_prom_envs] = optarg; | |
5349 | nb_prom_envs++; | |
5350 | break; | |
2b8f2d41 AZ |
5351 | #endif |
5352 | #ifdef TARGET_ARM | |
5353 | case QEMU_OPTION_old_param: | |
5354 | old_param = 1; | |
05ebd537 | 5355 | break; |
66508601 | 5356 | #endif |
f3dcfada TS |
5357 | case QEMU_OPTION_clock: |
5358 | configure_alarms(optarg); | |
5359 | break; | |
7e0af5d0 FB |
5360 | case QEMU_OPTION_startdate: |
5361 | { | |
5362 | struct tm tm; | |
f6503059 | 5363 | time_t rtc_start_date; |
7e0af5d0 | 5364 | if (!strcmp(optarg, "now")) { |
f6503059 | 5365 | rtc_date_offset = -1; |
7e0af5d0 FB |
5366 | } else { |
5367 | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", | |
5368 | &tm.tm_year, | |
5369 | &tm.tm_mon, | |
5370 | &tm.tm_mday, | |
5371 | &tm.tm_hour, | |
5372 | &tm.tm_min, | |
5373 | &tm.tm_sec) == 6) { | |
5374 | /* OK */ | |
5375 | } else if (sscanf(optarg, "%d-%d-%d", | |
5376 | &tm.tm_year, | |
5377 | &tm.tm_mon, | |
5378 | &tm.tm_mday) == 3) { | |
5379 | tm.tm_hour = 0; | |
5380 | tm.tm_min = 0; | |
5381 | tm.tm_sec = 0; | |
5382 | } else { | |
5383 | goto date_fail; | |
5384 | } | |
5385 | tm.tm_year -= 1900; | |
5386 | tm.tm_mon--; | |
3c6b2088 | 5387 | rtc_start_date = mktimegm(&tm); |
7e0af5d0 FB |
5388 | if (rtc_start_date == -1) { |
5389 | date_fail: | |
5390 | fprintf(stderr, "Invalid date format. Valid format are:\n" | |
5391 | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n"); | |
5392 | exit(1); | |
5393 | } | |
f6503059 | 5394 | rtc_date_offset = time(NULL) - rtc_start_date; |
7e0af5d0 FB |
5395 | } |
5396 | } | |
5397 | break; | |
26a5f13b FB |
5398 | case QEMU_OPTION_tb_size: |
5399 | tb_size = strtol(optarg, NULL, 0); | |
5400 | if (tb_size < 0) | |
5401 | tb_size = 0; | |
5402 | break; | |
2e70f6ef PB |
5403 | case QEMU_OPTION_icount: |
5404 | use_icount = 1; | |
5405 | if (strcmp(optarg, "auto") == 0) { | |
5406 | icount_time_shift = -1; | |
5407 | } else { | |
5408 | icount_time_shift = strtol(optarg, NULL, 0); | |
5409 | } | |
5410 | break; | |
5bb7910a AL |
5411 | case QEMU_OPTION_incoming: |
5412 | incoming = optarg; | |
5413 | break; | |
5824d651 | 5414 | #ifndef _WIN32 |
0858532e AL |
5415 | case QEMU_OPTION_chroot: |
5416 | chroot_dir = optarg; | |
5417 | break; | |
5418 | case QEMU_OPTION_runas: | |
5419 | run_as = optarg; | |
5420 | break; | |
e37630ca AL |
5421 | #endif |
5422 | #ifdef CONFIG_XEN | |
5423 | case QEMU_OPTION_xen_domid: | |
5424 | xen_domid = atoi(optarg); | |
5425 | break; | |
5426 | case QEMU_OPTION_xen_create: | |
5427 | xen_mode = XEN_CREATE; | |
5428 | break; | |
5429 | case QEMU_OPTION_xen_attach: | |
5430 | xen_mode = XEN_ATTACH; | |
5431 | break; | |
5824d651 | 5432 | #endif |
cd6f1169 | 5433 | } |
0824d6fc FB |
5434 | } |
5435 | } | |
330d0414 | 5436 | |
e09a5267 DK |
5437 | if (kvm_enabled()) { |
5438 | int ret; | |
5439 | ||
5440 | ret = kvm_init(smp_cpus); | |
5441 | if (ret < 0) { | |
5442 | fprintf(stderr, "failed to initialize KVM\n"); | |
5443 | exit(1); | |
5444 | } | |
5445 | } | |
5446 | ||
5cea8590 PB |
5447 | /* If no data_dir is specified then try to find it relative to the |
5448 | executable path. */ | |
5449 | if (!data_dir) { | |
5450 | data_dir = find_datadir(argv[0]); | |
5451 | } | |
5452 | /* If all else fails use the install patch specified when building. */ | |
5453 | if (!data_dir) { | |
5454 | data_dir = CONFIG_QEMU_SHAREDIR; | |
5455 | } | |
5456 | ||
6be68d7e JS |
5457 | /* |
5458 | * Default to max_cpus = smp_cpus, in case the user doesn't | |
5459 | * specify a max_cpus value. | |
5460 | */ | |
5461 | if (!max_cpus) | |
5462 | max_cpus = smp_cpus; | |
5463 | ||
3d878caa | 5464 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
b2097003 AL |
5465 | if (smp_cpus > machine->max_cpus) { |
5466 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " | |
5467 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, | |
5468 | machine->max_cpus); | |
5469 | exit(1); | |
5470 | } | |
5471 | ||
993fbfdb | 5472 | if (display_type == DT_NOGRAPHIC) { |
bc0129d9 AL |
5473 | if (serial_device_index == 0) |
5474 | serial_devices[0] = "stdio"; | |
5475 | if (parallel_device_index == 0) | |
5476 | parallel_devices[0] = "null"; | |
ddd9bbd9 JK |
5477 | if (strncmp(monitor_devices[0], "vc", 2) == 0) { |
5478 | monitor_devices[0] = "stdio"; | |
5479 | } | |
bc0129d9 AL |
5480 | } |
5481 | ||
71e3ceb8 | 5482 | #ifndef _WIN32 |
71e3ceb8 TS |
5483 | if (daemonize) { |
5484 | pid_t pid; | |
5485 | ||
5486 | if (pipe(fds) == -1) | |
5487 | exit(1); | |
5488 | ||
5489 | pid = fork(); | |
5490 | if (pid > 0) { | |
5491 | uint8_t status; | |
5492 | ssize_t len; | |
5493 | ||
5494 | close(fds[1]); | |
5495 | ||
5496 | again: | |
93815bc2 TS |
5497 | len = read(fds[0], &status, 1); |
5498 | if (len == -1 && (errno == EINTR)) | |
5499 | goto again; | |
5500 | ||
5501 | if (len != 1) | |
5502 | exit(1); | |
5503 | else if (status == 1) { | |
5504 | fprintf(stderr, "Could not acquire pidfile\n"); | |
5505 | exit(1); | |
5506 | } else | |
5507 | exit(0); | |
71e3ceb8 | 5508 | } else if (pid < 0) |
93815bc2 | 5509 | exit(1); |
71e3ceb8 TS |
5510 | |
5511 | setsid(); | |
5512 | ||
5513 | pid = fork(); | |
5514 | if (pid > 0) | |
5515 | exit(0); | |
5516 | else if (pid < 0) | |
5517 | exit(1); | |
5518 | ||
5519 | umask(027); | |
71e3ceb8 TS |
5520 | |
5521 | signal(SIGTSTP, SIG_IGN); | |
5522 | signal(SIGTTOU, SIG_IGN); | |
5523 | signal(SIGTTIN, SIG_IGN); | |
5524 | } | |
71e3ceb8 | 5525 | |
aa26bb2d | 5526 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
93815bc2 TS |
5527 | if (daemonize) { |
5528 | uint8_t status = 1; | |
5529 | write(fds[1], &status, 1); | |
5530 | } else | |
5531 | fprintf(stderr, "Could not acquire pid file\n"); | |
5532 | exit(1); | |
5533 | } | |
b9e82a59 | 5534 | #endif |
93815bc2 | 5535 | |
3fcf7b6b AL |
5536 | if (qemu_init_main_loop()) { |
5537 | fprintf(stderr, "qemu_init_main_loop failed\n"); | |
5538 | exit(1); | |
5539 | } | |
a20dd508 | 5540 | linux_boot = (kernel_filename != NULL); |
6c41b272 | 5541 | |
f8d39c01 TS |
5542 | if (!linux_boot && *kernel_cmdline != '\0') { |
5543 | fprintf(stderr, "-append only allowed with -kernel option\n"); | |
5544 | exit(1); | |
5545 | } | |
5546 | ||
5547 | if (!linux_boot && initrd_filename != NULL) { | |
5548 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); | |
5549 | exit(1); | |
5550 | } | |
5551 | ||
bf65f53f FN |
5552 | #ifndef _WIN32 |
5553 | /* Win32 doesn't support line-buffering and requires size >= 2 */ | |
b118d61e | 5554 | setvbuf(stdout, NULL, _IOLBF, 0); |
bf65f53f | 5555 | #endif |
3b46e624 | 5556 | |
634fce96 | 5557 | init_timers(); |
7183b4b4 AL |
5558 | if (init_timer_alarm() < 0) { |
5559 | fprintf(stderr, "could not initialize alarm timer\n"); | |
5560 | exit(1); | |
5561 | } | |
2e70f6ef PB |
5562 | if (use_icount && icount_time_shift < 0) { |
5563 | use_icount = 2; | |
5564 | /* 125MIPS seems a reasonable initial guess at the guest speed. | |
5565 | It will be corrected fairly quickly anyway. */ | |
5566 | icount_time_shift = 3; | |
5567 | init_icount_adjust(); | |
5568 | } | |
634fce96 | 5569 | |
fd1dff4b FB |
5570 | #ifdef _WIN32 |
5571 | socket_init(); | |
5572 | #endif | |
5573 | ||
7c9d8e07 FB |
5574 | /* init network clients */ |
5575 | if (nb_net_clients == 0) { | |
5576 | /* if no clients, we use a default config */ | |
f441b28b AL |
5577 | net_clients[nb_net_clients++] = "nic"; |
5578 | #ifdef CONFIG_SLIRP | |
5579 | net_clients[nb_net_clients++] = "user"; | |
5580 | #endif | |
c20709aa FB |
5581 | } |
5582 | ||
7c9d8e07 | 5583 | for(i = 0;i < nb_net_clients; i++) { |
9ad97e65 | 5584 | if (net_client_parse(net_clients[i]) < 0) |
7c9d8e07 | 5585 | exit(1); |
702c651c | 5586 | } |
f1510b2c | 5587 | |
406c8df3 GC |
5588 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
5589 | net_set_boot_mask(net_boot); | |
5590 | ||
5591 | net_client_check(); | |
eec85c2a | 5592 | |
dc72ac14 | 5593 | /* init the bluetooth world */ |
bd3c948d GH |
5594 | if (foreach_device_config(DEV_BT, bt_parse)) |
5595 | exit(1); | |
dc72ac14 | 5596 | |
0824d6fc | 5597 | /* init the memory */ |
94a6b54f PB |
5598 | if (ram_size == 0) |
5599 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; | |
9ae02555 | 5600 | |
26a5f13b FB |
5601 | /* init the dynamic translator */ |
5602 | cpu_exec_init_all(tb_size * 1024 * 1024); | |
5603 | ||
5905b2e5 | 5604 | bdrv_init(); |
c4b1fcc0 | 5605 | |
e4bcb14c | 5606 | /* we always create the cdrom drive, even if no disk is there */ |
3b0ba927 | 5607 | drive_add(NULL, CDROM_ALIAS); |
c4b1fcc0 | 5608 | |
9d413d1d | 5609 | /* we always create at least one floppy */ |
3b0ba927 | 5610 | drive_add(NULL, FD_ALIAS, 0); |
86f55663 | 5611 | |
9d413d1d | 5612 | /* we always create one sd slot, even if no card is in it */ |
3b0ba927 | 5613 | drive_add(NULL, SD_ALIAS); |
9d413d1d | 5614 | |
e4bcb14c | 5615 | /* open the virtual block devices */ |
9dfd7c7a | 5616 | if (snapshot) |
7282a033 GH |
5617 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
5618 | if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0) | |
9dfd7c7a | 5619 | exit(1); |
3e3d5815 | 5620 | |
c88676f8 | 5621 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
475e4277 | 5622 | register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL); |
8a7ddc38 | 5623 | |
3023f332 | 5624 | /* Maintain compatibility with multiple stdio monitors */ |
ddd9bbd9 | 5625 | if (!strcmp(monitor_devices[0],"stdio")) { |
3023f332 AL |
5626 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
5627 | const char *devname = serial_devices[i]; | |
5628 | if (devname && !strcmp(devname,"mon:stdio")) { | |
ddd9bbd9 | 5629 | monitor_devices[0] = NULL; |
3023f332 AL |
5630 | break; |
5631 | } else if (devname && !strcmp(devname,"stdio")) { | |
ddd9bbd9 | 5632 | monitor_devices[0] = NULL; |
3023f332 AL |
5633 | serial_devices[i] = "mon:stdio"; |
5634 | break; | |
5635 | } | |
5636 | } | |
5637 | } | |
5638 | ||
268a362c AL |
5639 | if (nb_numa_nodes > 0) { |
5640 | int i; | |
5641 | ||
5642 | if (nb_numa_nodes > smp_cpus) { | |
5643 | nb_numa_nodes = smp_cpus; | |
5644 | } | |
5645 | ||
5646 | /* If no memory size if given for any node, assume the default case | |
5647 | * and distribute the available memory equally across all nodes | |
5648 | */ | |
5649 | for (i = 0; i < nb_numa_nodes; i++) { | |
5650 | if (node_mem[i] != 0) | |
5651 | break; | |
5652 | } | |
5653 | if (i == nb_numa_nodes) { | |
5654 | uint64_t usedmem = 0; | |
5655 | ||
5656 | /* On Linux, the each node's border has to be 8MB aligned, | |
5657 | * the final node gets the rest. | |
5658 | */ | |
5659 | for (i = 0; i < nb_numa_nodes - 1; i++) { | |
5660 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); | |
5661 | usedmem += node_mem[i]; | |
5662 | } | |
5663 | node_mem[i] = ram_size - usedmem; | |
5664 | } | |
5665 | ||
5666 | for (i = 0; i < nb_numa_nodes; i++) { | |
5667 | if (node_cpumask[i] != 0) | |
5668 | break; | |
5669 | } | |
5670 | /* assigning the VCPUs round-robin is easier to implement, guest OSes | |
5671 | * must cope with this anyway, because there are BIOSes out there in | |
5672 | * real machines which also use this scheme. | |
5673 | */ | |
5674 | if (i == nb_numa_nodes) { | |
5675 | for (i = 0; i < smp_cpus; i++) { | |
5676 | node_cpumask[i % nb_numa_nodes] |= 1 << i; | |
5677 | } | |
5678 | } | |
5679 | } | |
5680 | ||
ddd9bbd9 JK |
5681 | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
5682 | const char *devname = monitor_devices[i]; | |
5683 | if (devname && strcmp(devname, "none")) { | |
5684 | char label[32]; | |
5685 | if (i == 0) { | |
5686 | snprintf(label, sizeof(label), "monitor"); | |
5687 | } else { | |
5688 | snprintf(label, sizeof(label), "monitor%d", i); | |
5689 | } | |
5690 | monitor_hds[i] = qemu_chr_open(label, devname, NULL); | |
5691 | if (!monitor_hds[i]) { | |
5692 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", | |
5693 | devname); | |
5694 | exit(1); | |
5695 | } | |
4c621805 AL |
5696 | } |
5697 | } | |
5698 | ||
2796dae0 AL |
5699 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
5700 | const char *devname = serial_devices[i]; | |
5701 | if (devname && strcmp(devname, "none")) { | |
5702 | char label[32]; | |
5703 | snprintf(label, sizeof(label), "serial%d", i); | |
ceecf1d1 | 5704 | serial_hds[i] = qemu_chr_open(label, devname, NULL); |
2796dae0 AL |
5705 | if (!serial_hds[i]) { |
5706 | fprintf(stderr, "qemu: could not open serial device '%s'\n", | |
5707 | devname); | |
5708 | exit(1); | |
5709 | } | |
5710 | } | |
5711 | } | |
5712 | ||
5713 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { | |
5714 | const char *devname = parallel_devices[i]; | |
5715 | if (devname && strcmp(devname, "none")) { | |
5716 | char label[32]; | |
5717 | snprintf(label, sizeof(label), "parallel%d", i); | |
ceecf1d1 | 5718 | parallel_hds[i] = qemu_chr_open(label, devname, NULL); |
2796dae0 AL |
5719 | if (!parallel_hds[i]) { |
5720 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", | |
5721 | devname); | |
5722 | exit(1); | |
5723 | } | |
5724 | } | |
5725 | } | |
5726 | ||
5727 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { | |
5728 | const char *devname = virtio_consoles[i]; | |
5729 | if (devname && strcmp(devname, "none")) { | |
5730 | char label[32]; | |
5731 | snprintf(label, sizeof(label), "virtcon%d", i); | |
ceecf1d1 | 5732 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL); |
2796dae0 AL |
5733 | if (!virtcon_hds[i]) { |
5734 | fprintf(stderr, "qemu: could not open virtio console '%s'\n", | |
5735 | devname); | |
5736 | exit(1); | |
5737 | } | |
5738 | } | |
5739 | } | |
5740 | ||
aae9460e PB |
5741 | module_call_init(MODULE_INIT_DEVICE); |
5742 | ||
09aaa160 MA |
5743 | if (watchdog) { |
5744 | i = select_watchdog(watchdog); | |
5745 | if (i > 0) | |
5746 | exit (i == 1 ? 1 : 0); | |
5747 | } | |
5748 | ||
b6b61144 GH |
5749 | if (machine->compat_props) { |
5750 | qdev_prop_register_compat(machine->compat_props); | |
5751 | } | |
fbe1b595 | 5752 | machine->init(ram_size, boot_devices, |
3023f332 AL |
5753 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
5754 | ||
268a362c | 5755 | |
67b3b71d JQ |
5756 | #ifndef _WIN32 |
5757 | /* must be after terminal init, SDL library changes signal handlers */ | |
5758 | sighandler_setup(); | |
5759 | #endif | |
5760 | ||
268a362c AL |
5761 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
5762 | for (i = 0; i < nb_numa_nodes; i++) { | |
5763 | if (node_cpumask[i] & (1 << env->cpu_index)) { | |
5764 | env->numa_node = i; | |
5765 | } | |
5766 | } | |
5767 | } | |
5768 | ||
6f338c34 AL |
5769 | current_machine = machine; |
5770 | ||
3023f332 AL |
5771 | /* init USB devices */ |
5772 | if (usb_enabled) { | |
bd3c948d | 5773 | foreach_device_config(DEV_USB, usb_parse); |
3023f332 AL |
5774 | } |
5775 | ||
bd3c948d | 5776 | /* init generic devices */ |
f31d07d1 | 5777 | if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0) |
bd3c948d GH |
5778 | exit(1); |
5779 | ||
8f391ab4 AL |
5780 | if (!display_state) |
5781 | dumb_display_init(); | |
3023f332 AL |
5782 | /* just use the first displaystate for the moment */ |
5783 | ds = display_state; | |
993fbfdb AL |
5784 | |
5785 | if (display_type == DT_DEFAULT) { | |
5786 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) | |
5787 | display_type = DT_SDL; | |
5788 | #else | |
5789 | display_type = DT_VNC; | |
5790 | vnc_display = "localhost:0,to=99"; | |
5791 | show_vnc_port = 1; | |
5792 | #endif | |
5793 | } | |
5794 | ||
5795 | ||
5796 | switch (display_type) { | |
5797 | case DT_NOGRAPHIC: | |
5798 | break; | |
4d3b6f6e | 5799 | #if defined(CONFIG_CURSES) |
993fbfdb AL |
5800 | case DT_CURSES: |
5801 | curses_display_init(ds, full_screen); | |
5802 | break; | |
4d3b6f6e | 5803 | #endif |
5b0753e0 | 5804 | #if defined(CONFIG_SDL) |
993fbfdb AL |
5805 | case DT_SDL: |
5806 | sdl_display_init(ds, full_screen, no_frame); | |
5807 | break; | |
5b0753e0 | 5808 | #elif defined(CONFIG_COCOA) |
993fbfdb AL |
5809 | case DT_SDL: |
5810 | cocoa_display_init(ds, full_screen); | |
5811 | break; | |
313aa567 | 5812 | #endif |
993fbfdb AL |
5813 | case DT_VNC: |
5814 | vnc_display_init(ds); | |
5815 | if (vnc_display_open(ds, vnc_display) < 0) | |
5816 | exit(1); | |
f92f8afe | 5817 | |
993fbfdb AL |
5818 | if (show_vnc_port) { |
5819 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); | |
f92f8afe | 5820 | } |
993fbfdb AL |
5821 | break; |
5822 | default: | |
5823 | break; | |
313aa567 | 5824 | } |
7d957bd8 | 5825 | dpy_resize(ds); |
5b08fc10 | 5826 | |
3023f332 AL |
5827 | dcl = ds->listeners; |
5828 | while (dcl != NULL) { | |
5829 | if (dcl->dpy_refresh != NULL) { | |
5830 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); | |
5831 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); | |
20d8a3ed | 5832 | } |
3023f332 | 5833 | dcl = dcl->next; |
20d8a3ed | 5834 | } |
3023f332 | 5835 | |
993fbfdb | 5836 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
9043b62d BS |
5837 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
5838 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); | |
5839 | } | |
5840 | ||
2796dae0 | 5841 | text_consoles_set_display(display_state); |
2970a6c9 | 5842 | qemu_chr_initial_reset(); |
2796dae0 | 5843 | |
ddd9bbd9 JK |
5844 | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
5845 | if (monitor_devices[i] && monitor_hds[i]) { | |
5846 | monitor_init(monitor_hds[i], | |
5847 | MONITOR_USE_READLINE | | |
5848 | ((i == 0) ? MONITOR_IS_DEFAULT : 0)); | |
5849 | } | |
5850 | } | |
82c643ff | 5851 | |
8d11df9e | 5852 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
c03b0f0f | 5853 | const char *devname = serial_devices[i]; |
fd5f393a | 5854 | if (devname && strcmp(devname, "none")) { |
af3a9031 | 5855 | if (strstart(devname, "vc", 0)) |
7ba1260a | 5856 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
8d11df9e | 5857 | } |
82c643ff | 5858 | } |
82c643ff | 5859 | |
6508fe59 | 5860 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
c03b0f0f | 5861 | const char *devname = parallel_devices[i]; |
fd5f393a | 5862 | if (devname && strcmp(devname, "none")) { |
af3a9031 | 5863 | if (strstart(devname, "vc", 0)) |
7ba1260a | 5864 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
6508fe59 FB |
5865 | } |
5866 | } | |
5867 | ||
9ede2fde AL |
5868 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
5869 | const char *devname = virtio_consoles[i]; | |
2796dae0 | 5870 | if (virtcon_hds[i] && devname) { |
9ede2fde AL |
5871 | if (strstart(devname, "vc", 0)) |
5872 | qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i); | |
5873 | } | |
5874 | } | |
5875 | ||
59030a8c AL |
5876 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
5877 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", | |
5878 | gdbstub_dev); | |
5879 | exit(1); | |
45669e00 | 5880 | } |
45669e00 | 5881 | |
05f2401e JQ |
5882 | if (loadvm) { |
5883 | if (load_vmstate(cur_mon, loadvm) < 0) { | |
5884 | autostart = 0; | |
5885 | } | |
5886 | } | |
d63d307f | 5887 | |
2bb8c10c | 5888 | if (incoming) { |
5bb7910a | 5889 | qemu_start_incoming_migration(incoming); |
6b99dadc | 5890 | } else if (autostart) { |
c0f4ce77 | 5891 | vm_start(); |
6b99dadc | 5892 | } |
ffd843bc | 5893 | |
b9e82a59 | 5894 | #ifndef _WIN32 |
71e3ceb8 TS |
5895 | if (daemonize) { |
5896 | uint8_t status = 0; | |
5897 | ssize_t len; | |
71e3ceb8 TS |
5898 | |
5899 | again1: | |
5900 | len = write(fds[1], &status, 1); | |
5901 | if (len == -1 && (errno == EINTR)) | |
5902 | goto again1; | |
5903 | ||
5904 | if (len != 1) | |
5905 | exit(1); | |
5906 | ||
bd54b863 | 5907 | chdir("/"); |
aeb30be6 | 5908 | TFR(fd = open("/dev/null", O_RDWR)); |
71e3ceb8 TS |
5909 | if (fd == -1) |
5910 | exit(1); | |
0858532e | 5911 | } |
71e3ceb8 | 5912 | |
0858532e AL |
5913 | if (run_as) { |
5914 | pwd = getpwnam(run_as); | |
5915 | if (!pwd) { | |
5916 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); | |
5917 | exit(1); | |
5918 | } | |
5919 | } | |
5920 | ||
5921 | if (chroot_dir) { | |
5922 | if (chroot(chroot_dir) < 0) { | |
5923 | fprintf(stderr, "chroot failed\n"); | |
5924 | exit(1); | |
5925 | } | |
5926 | chdir("/"); | |
5927 | } | |
5928 | ||
5929 | if (run_as) { | |
5930 | if (setgid(pwd->pw_gid) < 0) { | |
5931 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); | |
5932 | exit(1); | |
5933 | } | |
5934 | if (setuid(pwd->pw_uid) < 0) { | |
5935 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); | |
5936 | exit(1); | |
5937 | } | |
5938 | if (setuid(0) != -1) { | |
5939 | fprintf(stderr, "Dropping privileges failed\n"); | |
5940 | exit(1); | |
5941 | } | |
5942 | } | |
0858532e AL |
5943 | |
5944 | if (daemonize) { | |
5945 | dup2(fd, 0); | |
5946 | dup2(fd, 1); | |
5947 | dup2(fd, 2); | |
71e3ceb8 | 5948 | |
0858532e | 5949 | close(fd); |
71e3ceb8 | 5950 | } |
b9e82a59 | 5951 | #endif |
71e3ceb8 | 5952 | |
8a7ddc38 | 5953 | main_loop(); |
40c3bac3 | 5954 | quit_timers(); |
63a01ef8 | 5955 | net_cleanup(); |
b46a8906 | 5956 | |
0824d6fc FB |
5957 | return 0; |
5958 | } |