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