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