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