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