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