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