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