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