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