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