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