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