qemu-kvm: routing table update thinko fix
[qemu-kvm/fedora.git] / vl.c
blobc09d801c035fb24cd3358b11ac058f44d7d64e5e
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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>
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
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 #if defined(__NetBSD__)
48 #include <net/if_tap.h>
49 #endif
50 #ifdef __linux__
51 #include <linux/if_tun.h>
52 #endif
53 #include <arpa/inet.h>
54 #include <dirent.h>
55 #include <netdb.h>
56 #include <sys/select.h>
57 #ifdef HOST_BSD
58 #include <sys/stat.h>
59 #if defined(__FreeBSD__) || defined(__DragonFly__)
60 #include <libutil.h>
61 #else
62 #include <util.h>
63 #endif
64 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65 #include <freebsd/stdlib.h>
66 #else
67 #ifdef __linux__
68 #include <pty.h>
69 #include <malloc.h>
70 #include <linux/rtc.h>
71 #include <sys/prctl.h>
73 /* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75 /* #include <linux/hpet.h> */
76 #include "hpet.h"
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
80 #endif
81 #ifdef __sun__
82 #include <sys/stat.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
92 #include <net/if.h>
93 #include <syslog.h>
94 #include <stropts.h>
95 #endif
96 #endif
97 #endif
99 #if defined(__OpenBSD__)
100 #include <util.h>
101 #endif
103 #if defined(CONFIG_VDE)
104 #include <libvdeplug.h>
105 #endif
107 #ifdef _WIN32
108 #include <windows.h>
109 #include <malloc.h>
110 #include <sys/timeb.h>
111 #include <mmsystem.h>
112 #define getopt_long_only getopt_long
113 #define memalign(align, size) malloc(size)
114 #endif
116 #ifdef CONFIG_SDL
117 #if defined(__APPLE__) || defined(main)
118 #include <SDL.h>
119 int qemu_main(int argc, char **argv, char **envp);
120 int main(int argc, char **argv)
122 return qemu_main(argc, argv, NULL);
124 #undef main
125 #define main qemu_main
126 #endif
127 #endif /* CONFIG_SDL */
129 #ifdef CONFIG_COCOA
130 #undef main
131 #define main qemu_main
132 #endif /* CONFIG_COCOA */
134 #include "hw/hw.h"
135 #include "hw/boards.h"
136 #include "hw/usb.h"
137 #include "hw/pcmcia.h"
138 #include "hw/pc.h"
139 #include "hw/audiodev.h"
140 #include "hw/isa.h"
141 #include "hw/baum.h"
142 #include "hw/bt.h"
143 #include "hw/watchdog.h"
144 #include "hw/smbios.h"
145 #include "hw/xen.h"
146 #include "bt-host.h"
147 #include "net.h"
148 #include "monitor.h"
149 #include "console.h"
150 #include "sysemu.h"
151 #include "gdbstub.h"
152 #include "qemu-timer.h"
153 #include "qemu-char.h"
154 #include "cache-utils.h"
155 #include "block.h"
156 #include "dma.h"
157 #include "audio/audio.h"
158 #include "migration.h"
159 #include "kvm.h"
160 #include "balloon.h"
161 #include "qemu-option.h"
162 #include "qemu-kvm.h"
163 #include "hw/device-assignment.h"
165 #include "disas.h"
167 #include "exec-all.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
173 //#define DEBUG_NET
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 /* Max number of USB devices that can be specified on the commandline. */
179 #define MAX_USB_CMDLINE 8
181 /* Max number of bluetooth switches on the commandline. */
182 #define MAX_BT_CMDLINE 10
184 static const char *data_dir;
185 const char *bios_name = NULL;
186 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
187 to store the VM snapshots */
188 DriveInfo drives_table[MAX_DRIVES+1];
189 int nb_drives;
190 int extboot_drive = -1;
191 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
192 static DisplayState *display_state;
193 DisplayType display_type = DT_DEFAULT;
194 const char* keyboard_layout = NULL;
195 int64_t ticks_per_sec;
196 ram_addr_t ram_size;
197 int nb_nics;
198 NICInfo nd_table[MAX_NICS];
199 int vm_running;
200 static int autostart;
201 static int rtc_utc = 1;
202 static int rtc_date_offset = -1; /* -1 means no change */
203 int cirrus_vga_enabled = 1;
204 int std_vga_enabled = 0;
205 int vmsvga_enabled = 0;
206 int xenfb_enabled = 0;
207 #ifdef TARGET_SPARC
208 int graphic_width = 1024;
209 int graphic_height = 768;
210 int graphic_depth = 8;
211 #else
212 int graphic_width = 800;
213 int graphic_height = 600;
214 int graphic_depth = 15;
215 #endif
216 static int full_screen = 0;
217 #ifdef CONFIG_SDL
218 static int no_frame = 0;
219 #endif
220 int no_quit = 0;
221 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
222 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
223 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
224 #ifdef TARGET_I386
225 int win2k_install_hack = 0;
226 int rtc_td_hack = 0;
227 #endif
228 int usb_enabled = 0;
229 int singlestep = 0;
230 const char *assigned_devices[MAX_DEV_ASSIGN_CMDLINE];
231 int assigned_devices_index;
232 int smp_cpus = 1;
233 const char *vnc_display;
234 int acpi_enabled = 1;
235 #ifdef TARGET_I386
236 int no_hpet = 0;
237 #endif
238 int virtio_balloon = 1;
239 const char *virtio_balloon_devaddr;
240 int fd_bootchk = 1;
241 int no_reboot = 0;
242 int no_shutdown = 0;
243 int cursor_hide = 1;
244 int graphic_rotate = 0;
245 uint8_t irq0override = 1;
246 #ifndef _WIN32
247 int daemonize = 0;
248 #endif
249 WatchdogTimerModel *watchdog = NULL;
250 int watchdog_action = WDT_RESET;
251 const char *option_rom[MAX_OPTION_ROMS];
252 int nb_option_roms;
253 int semihosting_enabled = 0;
254 int time_drift_fix = 0;
255 unsigned int kvm_shadow_memory = 0;
256 const char *mem_path = NULL;
257 #ifdef MAP_POPULATE
258 int mem_prealloc = 1; /* force preallocation of physical target memory */
259 #endif
260 #ifdef TARGET_ARM
261 int old_param = 0;
262 #endif
263 const char *qemu_name;
264 int alt_grab = 0;
265 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
266 unsigned int nb_prom_envs = 0;
267 const char *prom_envs[MAX_PROM_ENVS];
268 #endif
269 int nb_drives_opt;
270 const char *nvram = NULL;
271 struct drive_opt drives_opt[MAX_DRIVES];
273 int nb_numa_nodes;
274 uint64_t node_mem[MAX_NODES];
275 uint64_t node_cpumask[MAX_NODES];
277 static CPUState *cur_cpu;
278 static CPUState *next_cpu;
279 static int timer_alarm_pending = 1;
280 /* Conversion factor from emulated instructions to virtual clock ticks. */
281 static int icount_time_shift;
282 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
283 #define MAX_ICOUNT_SHIFT 10
284 /* Compensate for varying guest execution speed. */
285 static int64_t qemu_icount_bias;
286 static QEMUTimer *icount_rt_timer;
287 static QEMUTimer *icount_vm_timer;
288 static QEMUTimer *nographic_timer;
290 uint8_t qemu_uuid[16];
292 /***********************************************************/
293 /* x86 ISA bus support */
295 target_phys_addr_t isa_mem_base = 0;
296 PicState2 *isa_pic;
298 /***********************************************************/
299 void hw_error(const char *fmt, ...)
301 va_list ap;
302 CPUState *env;
304 va_start(ap, fmt);
305 fprintf(stderr, "qemu: hardware error: ");
306 vfprintf(stderr, fmt, ap);
307 fprintf(stderr, "\n");
308 for(env = first_cpu; env != NULL; env = env->next_cpu) {
309 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
310 #ifdef TARGET_I386
311 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
312 #else
313 cpu_dump_state(env, stderr, fprintf, 0);
314 #endif
316 va_end(ap);
317 abort();
320 static void set_proc_name(const char *s)
322 #ifdef __linux__
323 char name[16];
324 if (!s)
325 return;
326 name[sizeof(name) - 1] = 0;
327 strncpy(name, s, sizeof(name));
328 /* Could rewrite argv[0] too, but that's a bit more complicated.
329 This simple way is enough for `top'. */
330 prctl(PR_SET_NAME, name);
331 #endif
334 /***************/
335 /* ballooning */
337 static QEMUBalloonEvent *qemu_balloon_event;
338 void *qemu_balloon_event_opaque;
340 void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
342 qemu_balloon_event = func;
343 qemu_balloon_event_opaque = opaque;
346 void qemu_balloon(ram_addr_t target)
348 if (qemu_balloon_event)
349 qemu_balloon_event(qemu_balloon_event_opaque, target);
352 ram_addr_t qemu_balloon_status(void)
354 if (qemu_balloon_event)
355 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
356 return 0;
359 /***********************************************************/
360 /* keyboard/mouse */
362 static QEMUPutKBDEvent *qemu_put_kbd_event;
363 static void *qemu_put_kbd_event_opaque;
364 static QEMUPutMouseEntry *qemu_put_mouse_event_head;
365 static QEMUPutMouseEntry *qemu_put_mouse_event_current;
367 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
369 qemu_put_kbd_event_opaque = opaque;
370 qemu_put_kbd_event = func;
373 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
374 void *opaque, int absolute,
375 const char *name)
377 QEMUPutMouseEntry *s, *cursor;
379 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
381 s->qemu_put_mouse_event = func;
382 s->qemu_put_mouse_event_opaque = opaque;
383 s->qemu_put_mouse_event_absolute = absolute;
384 s->qemu_put_mouse_event_name = qemu_strdup(name);
385 s->next = NULL;
387 if (!qemu_put_mouse_event_head) {
388 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
389 return s;
392 cursor = qemu_put_mouse_event_head;
393 while (cursor->next != NULL)
394 cursor = cursor->next;
396 cursor->next = s;
397 qemu_put_mouse_event_current = s;
399 return s;
402 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
404 QEMUPutMouseEntry *prev = NULL, *cursor;
406 if (!qemu_put_mouse_event_head || entry == NULL)
407 return;
409 cursor = qemu_put_mouse_event_head;
410 while (cursor != NULL && cursor != entry) {
411 prev = cursor;
412 cursor = cursor->next;
415 if (cursor == NULL) // does not exist or list empty
416 return;
417 else if (prev == NULL) { // entry is head
418 qemu_put_mouse_event_head = cursor->next;
419 if (qemu_put_mouse_event_current == entry)
420 qemu_put_mouse_event_current = cursor->next;
421 qemu_free(entry->qemu_put_mouse_event_name);
422 qemu_free(entry);
423 return;
426 prev->next = entry->next;
428 if (qemu_put_mouse_event_current == entry)
429 qemu_put_mouse_event_current = prev;
431 qemu_free(entry->qemu_put_mouse_event_name);
432 qemu_free(entry);
435 void kbd_put_keycode(int keycode)
437 if (qemu_put_kbd_event) {
438 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
442 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
444 QEMUPutMouseEvent *mouse_event;
445 void *mouse_event_opaque;
446 int width;
448 if (!qemu_put_mouse_event_current) {
449 return;
452 mouse_event =
453 qemu_put_mouse_event_current->qemu_put_mouse_event;
454 mouse_event_opaque =
455 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
457 if (mouse_event) {
458 if (graphic_rotate) {
459 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
460 width = 0x7fff;
461 else
462 width = graphic_width - 1;
463 mouse_event(mouse_event_opaque,
464 width - dy, dx, dz, buttons_state);
465 } else
466 mouse_event(mouse_event_opaque,
467 dx, dy, dz, buttons_state);
471 int kbd_mouse_is_absolute(void)
473 if (!qemu_put_mouse_event_current)
474 return 0;
476 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
479 void do_info_mice(Monitor *mon)
481 QEMUPutMouseEntry *cursor;
482 int index = 0;
484 if (!qemu_put_mouse_event_head) {
485 monitor_printf(mon, "No mouse devices connected\n");
486 return;
489 monitor_printf(mon, "Mouse devices available:\n");
490 cursor = qemu_put_mouse_event_head;
491 while (cursor != NULL) {
492 monitor_printf(mon, "%c Mouse #%d: %s\n",
493 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
494 index, cursor->qemu_put_mouse_event_name);
495 index++;
496 cursor = cursor->next;
500 void do_mouse_set(Monitor *mon, int index)
502 QEMUPutMouseEntry *cursor;
503 int i = 0;
505 if (!qemu_put_mouse_event_head) {
506 monitor_printf(mon, "No mouse devices connected\n");
507 return;
510 cursor = qemu_put_mouse_event_head;
511 while (cursor != NULL && index != i) {
512 i++;
513 cursor = cursor->next;
516 if (cursor != NULL)
517 qemu_put_mouse_event_current = cursor;
518 else
519 monitor_printf(mon, "Mouse at given index not found\n");
522 /* compute with 96 bit intermediate result: (a*b)/c */
523 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
525 union {
526 uint64_t ll;
527 struct {
528 #ifdef WORDS_BIGENDIAN
529 uint32_t high, low;
530 #else
531 uint32_t low, high;
532 #endif
533 } l;
534 } u, res;
535 uint64_t rl, rh;
537 u.ll = a;
538 rl = (uint64_t)u.l.low * (uint64_t)b;
539 rh = (uint64_t)u.l.high * (uint64_t)b;
540 rh += (rl >> 32);
541 res.l.high = rh / c;
542 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
543 return res.ll;
546 /***********************************************************/
547 /* real time host monotonic timer */
549 #define QEMU_TIMER_BASE 1000000000LL
551 #ifdef WIN32
553 static int64_t clock_freq;
555 static void init_get_clock(void)
557 LARGE_INTEGER freq;
558 int ret;
559 ret = QueryPerformanceFrequency(&freq);
560 if (ret == 0) {
561 fprintf(stderr, "Could not calibrate ticks\n");
562 exit(1);
564 clock_freq = freq.QuadPart;
567 static int64_t get_clock(void)
569 LARGE_INTEGER ti;
570 QueryPerformanceCounter(&ti);
571 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
574 #else
576 static int use_rt_clock;
578 static void init_get_clock(void)
580 use_rt_clock = 0;
581 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
582 || defined(__DragonFly__)
584 struct timespec ts;
585 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
586 use_rt_clock = 1;
589 #endif
592 static int64_t get_clock(void)
594 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
595 || defined(__DragonFly__)
596 if (use_rt_clock) {
597 struct timespec ts;
598 clock_gettime(CLOCK_MONOTONIC, &ts);
599 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
600 } else
601 #endif
603 /* XXX: using gettimeofday leads to problems if the date
604 changes, so it should be avoided. */
605 struct timeval tv;
606 gettimeofday(&tv, NULL);
607 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
610 #endif
612 /* Return the virtual CPU time, based on the instruction counter. */
613 static int64_t cpu_get_icount(void)
615 int64_t icount;
616 CPUState *env = cpu_single_env;;
617 icount = qemu_icount;
618 if (env) {
619 if (!can_do_io(env))
620 fprintf(stderr, "Bad clock read\n");
621 icount -= (env->icount_decr.u16.low + env->icount_extra);
623 return qemu_icount_bias + (icount << icount_time_shift);
626 /***********************************************************/
627 /* guest cycle counter */
629 static int64_t cpu_ticks_prev;
630 static int64_t cpu_ticks_offset;
631 static int64_t cpu_clock_offset;
632 static int cpu_ticks_enabled;
634 /* return the host CPU cycle counter and handle stop/restart */
635 int64_t cpu_get_ticks(void)
637 if (use_icount) {
638 return cpu_get_icount();
640 if (!cpu_ticks_enabled) {
641 return cpu_ticks_offset;
642 } else {
643 int64_t ticks;
644 ticks = cpu_get_real_ticks();
645 if (cpu_ticks_prev > ticks) {
646 /* Note: non increasing ticks may happen if the host uses
647 software suspend */
648 cpu_ticks_offset += cpu_ticks_prev - ticks;
650 cpu_ticks_prev = ticks;
651 return ticks + cpu_ticks_offset;
655 /* return the host CPU monotonic timer and handle stop/restart */
656 static int64_t cpu_get_clock(void)
658 int64_t ti;
659 if (!cpu_ticks_enabled) {
660 return cpu_clock_offset;
661 } else {
662 ti = get_clock();
663 return ti + cpu_clock_offset;
667 /* enable cpu_get_ticks() */
668 void cpu_enable_ticks(void)
670 if (!cpu_ticks_enabled) {
671 cpu_ticks_offset -= cpu_get_real_ticks();
672 cpu_clock_offset -= get_clock();
673 cpu_ticks_enabled = 1;
677 /* disable cpu_get_ticks() : the clock is stopped. You must not call
678 cpu_get_ticks() after that. */
679 void cpu_disable_ticks(void)
681 if (cpu_ticks_enabled) {
682 cpu_ticks_offset = cpu_get_ticks();
683 cpu_clock_offset = cpu_get_clock();
684 cpu_ticks_enabled = 0;
688 /***********************************************************/
689 /* timers */
691 #define QEMU_TIMER_REALTIME 0
692 #define QEMU_TIMER_VIRTUAL 1
694 struct QEMUClock {
695 int type;
696 /* XXX: add frequency */
699 struct QEMUTimer {
700 QEMUClock *clock;
701 int64_t expire_time;
702 QEMUTimerCB *cb;
703 void *opaque;
704 struct QEMUTimer *next;
707 struct qemu_alarm_timer {
708 char const *name;
709 unsigned int flags;
711 int (*start)(struct qemu_alarm_timer *t);
712 void (*stop)(struct qemu_alarm_timer *t);
713 void (*rearm)(struct qemu_alarm_timer *t);
714 void *priv;
717 #define ALARM_FLAG_DYNTICKS 0x1
718 #define ALARM_FLAG_EXPIRED 0x2
720 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
722 return t && (t->flags & ALARM_FLAG_DYNTICKS);
725 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
727 if (!alarm_has_dynticks(t))
728 return;
730 t->rearm(t);
733 /* TODO: MIN_TIMER_REARM_US should be optimized */
734 #define MIN_TIMER_REARM_US 250
736 static struct qemu_alarm_timer *alarm_timer;
738 #ifdef _WIN32
740 struct qemu_alarm_win32 {
741 MMRESULT timerId;
742 unsigned int period;
743 } alarm_win32_data = {0, -1};
745 static int win32_start_timer(struct qemu_alarm_timer *t);
746 static void win32_stop_timer(struct qemu_alarm_timer *t);
747 static void win32_rearm_timer(struct qemu_alarm_timer *t);
749 #else
751 static int unix_start_timer(struct qemu_alarm_timer *t);
752 static void unix_stop_timer(struct qemu_alarm_timer *t);
754 #ifdef __linux__
756 static int dynticks_start_timer(struct qemu_alarm_timer *t);
757 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
758 static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
760 static int hpet_start_timer(struct qemu_alarm_timer *t);
761 static void hpet_stop_timer(struct qemu_alarm_timer *t);
763 static int rtc_start_timer(struct qemu_alarm_timer *t);
764 static void rtc_stop_timer(struct qemu_alarm_timer *t);
766 #endif /* __linux__ */
768 #endif /* _WIN32 */
770 /* Correlation between real and virtual time is always going to be
771 fairly approximate, so ignore small variation.
772 When the guest is idle real and virtual time will be aligned in
773 the IO wait loop. */
774 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
776 static void icount_adjust(void)
778 int64_t cur_time;
779 int64_t cur_icount;
780 int64_t delta;
781 static int64_t last_delta;
782 /* If the VM is not running, then do nothing. */
783 if (!vm_running)
784 return;
786 cur_time = cpu_get_clock();
787 cur_icount = qemu_get_clock(vm_clock);
788 delta = cur_icount - cur_time;
789 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
790 if (delta > 0
791 && last_delta + ICOUNT_WOBBLE < delta * 2
792 && icount_time_shift > 0) {
793 /* The guest is getting too far ahead. Slow time down. */
794 icount_time_shift--;
796 if (delta < 0
797 && last_delta - ICOUNT_WOBBLE > delta * 2
798 && icount_time_shift < MAX_ICOUNT_SHIFT) {
799 /* The guest is getting too far behind. Speed time up. */
800 icount_time_shift++;
802 last_delta = delta;
803 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
806 static void icount_adjust_rt(void * opaque)
808 qemu_mod_timer(icount_rt_timer,
809 qemu_get_clock(rt_clock) + 1000);
810 icount_adjust();
813 static void icount_adjust_vm(void * opaque)
815 qemu_mod_timer(icount_vm_timer,
816 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
817 icount_adjust();
820 static void init_icount_adjust(void)
822 /* Have both realtime and virtual time triggers for speed adjustment.
823 The realtime trigger catches emulated time passing too slowly,
824 the virtual time trigger catches emulated time passing too fast.
825 Realtime triggers occur even when idle, so use them less frequently
826 than VM triggers. */
827 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
828 qemu_mod_timer(icount_rt_timer,
829 qemu_get_clock(rt_clock) + 1000);
830 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
831 qemu_mod_timer(icount_vm_timer,
832 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
835 static struct qemu_alarm_timer alarm_timers[] = {
836 #ifndef _WIN32
837 #ifdef __linux__
838 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
839 dynticks_stop_timer, dynticks_rearm_timer, NULL},
840 /* HPET - if available - is preferred */
841 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
842 /* ...otherwise try RTC */
843 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
844 #endif
845 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
846 #else
847 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
848 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
849 {"win32", 0, win32_start_timer,
850 win32_stop_timer, NULL, &alarm_win32_data},
851 #endif
852 {NULL, }
855 static void show_available_alarms(void)
857 int i;
859 printf("Available alarm timers, in order of precedence:\n");
860 for (i = 0; alarm_timers[i].name; i++)
861 printf("%s\n", alarm_timers[i].name);
864 static void configure_alarms(char const *opt)
866 int i;
867 int cur = 0;
868 int count = ARRAY_SIZE(alarm_timers) - 1;
869 char *arg;
870 char *name;
871 struct qemu_alarm_timer tmp;
873 if (!strcmp(opt, "?")) {
874 show_available_alarms();
875 exit(0);
878 arg = strdup(opt);
880 /* Reorder the array */
881 name = strtok(arg, ",");
882 while (name) {
883 for (i = 0; i < count && alarm_timers[i].name; i++) {
884 if (!strcmp(alarm_timers[i].name, name))
885 break;
888 if (i == count) {
889 fprintf(stderr, "Unknown clock %s\n", name);
890 goto next;
893 if (i < cur)
894 /* Ignore */
895 goto next;
897 /* Swap */
898 tmp = alarm_timers[i];
899 alarm_timers[i] = alarm_timers[cur];
900 alarm_timers[cur] = tmp;
902 cur++;
903 next:
904 name = strtok(NULL, ",");
907 free(arg);
909 if (cur) {
910 /* Disable remaining timers */
911 for (i = cur; i < count; i++)
912 alarm_timers[i].name = NULL;
913 } else {
914 show_available_alarms();
915 exit(1);
919 QEMUClock *rt_clock;
920 QEMUClock *vm_clock;
922 static QEMUTimer *active_timers[2];
924 static QEMUClock *qemu_new_clock(int type)
926 QEMUClock *clock;
927 clock = qemu_mallocz(sizeof(QEMUClock));
928 clock->type = type;
929 return clock;
932 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
934 QEMUTimer *ts;
936 ts = qemu_mallocz(sizeof(QEMUTimer));
937 ts->clock = clock;
938 ts->cb = cb;
939 ts->opaque = opaque;
940 return ts;
943 void qemu_free_timer(QEMUTimer *ts)
945 qemu_free(ts);
948 /* stop a timer, but do not dealloc it */
949 void qemu_del_timer(QEMUTimer *ts)
951 QEMUTimer **pt, *t;
953 /* NOTE: this code must be signal safe because
954 qemu_timer_expired() can be called from a signal. */
955 pt = &active_timers[ts->clock->type];
956 for(;;) {
957 t = *pt;
958 if (!t)
959 break;
960 if (t == ts) {
961 *pt = t->next;
962 break;
964 pt = &t->next;
968 /* modify the current timer so that it will be fired when current_time
969 >= expire_time. The corresponding callback will be called. */
970 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
972 QEMUTimer **pt, *t;
974 qemu_del_timer(ts);
976 /* add the timer in the sorted list */
977 /* NOTE: this code must be signal safe because
978 qemu_timer_expired() can be called from a signal. */
979 pt = &active_timers[ts->clock->type];
980 for(;;) {
981 t = *pt;
982 if (!t)
983 break;
984 if (t->expire_time > expire_time)
985 break;
986 pt = &t->next;
988 ts->expire_time = expire_time;
989 ts->next = *pt;
990 *pt = ts;
992 /* Rearm if necessary */
993 if (pt == &active_timers[ts->clock->type]) {
994 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
995 qemu_rearm_alarm_timer(alarm_timer);
997 /* Interrupt execution to force deadline recalculation. */
998 if (use_icount)
999 qemu_notify_event();
1003 int qemu_timer_pending(QEMUTimer *ts)
1005 QEMUTimer *t;
1006 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1007 if (t == ts)
1008 return 1;
1010 return 0;
1013 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1015 if (!timer_head)
1016 return 0;
1017 return (timer_head->expire_time <= current_time);
1020 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1022 QEMUTimer *ts;
1024 for(;;) {
1025 ts = *ptimer_head;
1026 if (!ts || ts->expire_time > current_time)
1027 break;
1028 /* remove timer from the list before calling the callback */
1029 *ptimer_head = ts->next;
1030 ts->next = NULL;
1032 /* run the callback (the timer list can be modified) */
1033 ts->cb(ts->opaque);
1037 int64_t qemu_get_clock(QEMUClock *clock)
1039 switch(clock->type) {
1040 case QEMU_TIMER_REALTIME:
1041 return get_clock() / 1000000;
1042 default:
1043 case QEMU_TIMER_VIRTUAL:
1044 if (use_icount) {
1045 return cpu_get_icount();
1046 } else {
1047 return cpu_get_clock();
1052 static void init_timers(void)
1054 init_get_clock();
1055 ticks_per_sec = QEMU_TIMER_BASE;
1056 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1057 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1060 /* save a timer */
1061 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1063 uint64_t expire_time;
1065 if (qemu_timer_pending(ts)) {
1066 expire_time = ts->expire_time;
1067 } else {
1068 expire_time = -1;
1070 qemu_put_be64(f, expire_time);
1073 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1075 uint64_t expire_time;
1077 expire_time = qemu_get_be64(f);
1078 if (expire_time != -1) {
1079 qemu_mod_timer(ts, expire_time);
1080 } else {
1081 qemu_del_timer(ts);
1085 static void timer_save(QEMUFile *f, void *opaque)
1087 if (cpu_ticks_enabled) {
1088 hw_error("cannot save state if virtual timers are running");
1090 qemu_put_be64(f, cpu_ticks_offset);
1091 qemu_put_be64(f, ticks_per_sec);
1092 qemu_put_be64(f, cpu_clock_offset);
1095 static int timer_load(QEMUFile *f, void *opaque, int version_id)
1097 if (version_id != 1 && version_id != 2)
1098 return -EINVAL;
1099 if (cpu_ticks_enabled) {
1100 return -EINVAL;
1102 cpu_ticks_offset=qemu_get_be64(f);
1103 ticks_per_sec=qemu_get_be64(f);
1104 if (version_id == 2) {
1105 cpu_clock_offset=qemu_get_be64(f);
1107 return 0;
1110 static void qemu_event_increment(void);
1112 #ifdef _WIN32
1113 static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1114 DWORD_PTR dwUser, DWORD_PTR dw1,
1115 DWORD_PTR dw2)
1116 #else
1117 static void host_alarm_handler(int host_signum)
1118 #endif
1120 #if 0
1121 #define DISP_FREQ 1000
1123 static int64_t delta_min = INT64_MAX;
1124 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1125 static int count;
1126 ti = qemu_get_clock(vm_clock);
1127 if (last_clock != 0) {
1128 delta = ti - last_clock;
1129 if (delta < delta_min)
1130 delta_min = delta;
1131 if (delta > delta_max)
1132 delta_max = delta;
1133 delta_cum += delta;
1134 if (++count == DISP_FREQ) {
1135 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1136 muldiv64(delta_min, 1000000, ticks_per_sec),
1137 muldiv64(delta_max, 1000000, ticks_per_sec),
1138 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1139 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1140 count = 0;
1141 delta_min = INT64_MAX;
1142 delta_max = 0;
1143 delta_cum = 0;
1146 last_clock = ti;
1148 #endif
1149 if (alarm_has_dynticks(alarm_timer) ||
1150 (!use_icount &&
1151 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1152 qemu_get_clock(vm_clock))) ||
1153 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1154 qemu_get_clock(rt_clock))) {
1155 qemu_event_increment();
1156 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1158 #ifndef CONFIG_IOTHREAD
1159 if (next_cpu) {
1160 /* stop the currently executing cpu because a timer occured */
1161 cpu_exit(next_cpu);
1162 #ifdef CONFIG_KQEMU
1163 if (next_cpu->kqemu_enabled) {
1164 kqemu_cpu_interrupt(next_cpu);
1166 #endif
1168 #endif
1169 timer_alarm_pending = 1;
1170 qemu_notify_event();
1174 static int64_t qemu_next_deadline(void)
1176 int64_t delta;
1178 if (active_timers[QEMU_TIMER_VIRTUAL]) {
1179 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1180 qemu_get_clock(vm_clock);
1181 } else {
1182 /* To avoid problems with overflow limit this to 2^32. */
1183 delta = INT32_MAX;
1186 if (delta < 0)
1187 delta = 0;
1189 return delta;
1192 #if defined(__linux__) || defined(_WIN32)
1193 static uint64_t qemu_next_deadline_dyntick(void)
1195 int64_t delta;
1196 int64_t rtdelta;
1198 if (use_icount)
1199 delta = INT32_MAX;
1200 else
1201 delta = (qemu_next_deadline() + 999) / 1000;
1203 if (active_timers[QEMU_TIMER_REALTIME]) {
1204 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1205 qemu_get_clock(rt_clock))*1000;
1206 if (rtdelta < delta)
1207 delta = rtdelta;
1210 if (delta < MIN_TIMER_REARM_US)
1211 delta = MIN_TIMER_REARM_US;
1213 return delta;
1215 #endif
1217 #ifndef _WIN32
1219 /* Sets a specific flag */
1220 static int fcntl_setfl(int fd, int flag)
1222 int flags;
1224 flags = fcntl(fd, F_GETFL);
1225 if (flags == -1)
1226 return -errno;
1228 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1229 return -errno;
1231 return 0;
1234 #if defined(__linux__)
1236 #define RTC_FREQ 1024
1238 static void enable_sigio_timer(int fd)
1240 struct sigaction act;
1242 /* timer signal */
1243 sigfillset(&act.sa_mask);
1244 act.sa_flags = 0;
1245 act.sa_handler = host_alarm_handler;
1247 sigaction(SIGIO, &act, NULL);
1248 fcntl_setfl(fd, O_ASYNC);
1249 fcntl(fd, F_SETOWN, getpid());
1252 static int hpet_start_timer(struct qemu_alarm_timer *t)
1254 struct hpet_info info;
1255 int r, fd;
1257 fd = open("/dev/hpet", O_RDONLY);
1258 if (fd < 0)
1259 return -1;
1261 /* Set frequency */
1262 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1263 if (r < 0) {
1264 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1265 "error, but for better emulation accuracy type:\n"
1266 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1267 goto fail;
1270 /* Check capabilities */
1271 r = ioctl(fd, HPET_INFO, &info);
1272 if (r < 0)
1273 goto fail;
1275 /* Enable periodic mode */
1276 r = ioctl(fd, HPET_EPI, 0);
1277 if (info.hi_flags && (r < 0))
1278 goto fail;
1280 /* Enable interrupt */
1281 r = ioctl(fd, HPET_IE_ON, 0);
1282 if (r < 0)
1283 goto fail;
1285 enable_sigio_timer(fd);
1286 t->priv = (void *)(long)fd;
1288 return 0;
1289 fail:
1290 close(fd);
1291 return -1;
1294 static void hpet_stop_timer(struct qemu_alarm_timer *t)
1296 int fd = (long)t->priv;
1298 close(fd);
1301 static int rtc_start_timer(struct qemu_alarm_timer *t)
1303 int rtc_fd;
1304 unsigned long current_rtc_freq = 0;
1306 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1307 if (rtc_fd < 0)
1308 return -1;
1309 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1310 if (current_rtc_freq != RTC_FREQ &&
1311 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1312 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1313 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1314 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1315 goto fail;
1317 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1318 fail:
1319 close(rtc_fd);
1320 return -1;
1323 enable_sigio_timer(rtc_fd);
1325 t->priv = (void *)(long)rtc_fd;
1327 return 0;
1330 static void rtc_stop_timer(struct qemu_alarm_timer *t)
1332 int rtc_fd = (long)t->priv;
1334 close(rtc_fd);
1337 static int dynticks_start_timer(struct qemu_alarm_timer *t)
1339 struct sigevent ev;
1340 timer_t host_timer;
1341 struct sigaction act;
1343 sigfillset(&act.sa_mask);
1344 act.sa_flags = 0;
1345 act.sa_handler = host_alarm_handler;
1347 sigaction(SIGALRM, &act, NULL);
1350 * Initialize ev struct to 0 to avoid valgrind complaining
1351 * about uninitialized data in timer_create call
1353 memset(&ev, 0, sizeof(ev));
1354 ev.sigev_value.sival_int = 0;
1355 ev.sigev_notify = SIGEV_SIGNAL;
1356 ev.sigev_signo = SIGALRM;
1358 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1359 perror("timer_create");
1361 /* disable dynticks */
1362 fprintf(stderr, "Dynamic Ticks disabled\n");
1364 return -1;
1367 t->priv = (void *)(long)host_timer;
1369 return 0;
1372 static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1374 timer_t host_timer = (timer_t)(long)t->priv;
1376 timer_delete(host_timer);
1379 static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1381 timer_t host_timer = (timer_t)(long)t->priv;
1382 struct itimerspec timeout;
1383 int64_t nearest_delta_us = INT64_MAX;
1384 int64_t current_us;
1386 if (!active_timers[QEMU_TIMER_REALTIME] &&
1387 !active_timers[QEMU_TIMER_VIRTUAL])
1388 return;
1390 nearest_delta_us = qemu_next_deadline_dyntick();
1392 /* check whether a timer is already running */
1393 if (timer_gettime(host_timer, &timeout)) {
1394 perror("gettime");
1395 fprintf(stderr, "Internal timer error: aborting\n");
1396 exit(1);
1398 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1399 if (current_us && current_us <= nearest_delta_us)
1400 return;
1402 timeout.it_interval.tv_sec = 0;
1403 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1404 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1405 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1406 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1407 perror("settime");
1408 fprintf(stderr, "Internal timer error: aborting\n");
1409 exit(1);
1413 #endif /* defined(__linux__) */
1415 static int unix_start_timer(struct qemu_alarm_timer *t)
1417 struct sigaction act;
1418 struct itimerval itv;
1419 int err;
1421 /* timer signal */
1422 sigfillset(&act.sa_mask);
1423 act.sa_flags = 0;
1424 act.sa_handler = host_alarm_handler;
1426 sigaction(SIGALRM, &act, NULL);
1428 itv.it_interval.tv_sec = 0;
1429 /* for i386 kernel 2.6 to get 1 ms */
1430 itv.it_interval.tv_usec = 999;
1431 itv.it_value.tv_sec = 0;
1432 itv.it_value.tv_usec = 10 * 1000;
1434 err = setitimer(ITIMER_REAL, &itv, NULL);
1435 if (err)
1436 return -1;
1438 return 0;
1441 static void unix_stop_timer(struct qemu_alarm_timer *t)
1443 struct itimerval itv;
1445 memset(&itv, 0, sizeof(itv));
1446 setitimer(ITIMER_REAL, &itv, NULL);
1449 #endif /* !defined(_WIN32) */
1452 #ifdef _WIN32
1454 static int win32_start_timer(struct qemu_alarm_timer *t)
1456 TIMECAPS tc;
1457 struct qemu_alarm_win32 *data = t->priv;
1458 UINT flags;
1460 memset(&tc, 0, sizeof(tc));
1461 timeGetDevCaps(&tc, sizeof(tc));
1463 if (data->period < tc.wPeriodMin)
1464 data->period = tc.wPeriodMin;
1466 timeBeginPeriod(data->period);
1468 flags = TIME_CALLBACK_FUNCTION;
1469 if (alarm_has_dynticks(t))
1470 flags |= TIME_ONESHOT;
1471 else
1472 flags |= TIME_PERIODIC;
1474 data->timerId = timeSetEvent(1, // interval (ms)
1475 data->period, // resolution
1476 host_alarm_handler, // function
1477 (DWORD)t, // parameter
1478 flags);
1480 if (!data->timerId) {
1481 perror("Failed to initialize win32 alarm timer");
1482 timeEndPeriod(data->period);
1483 return -1;
1486 return 0;
1489 static void win32_stop_timer(struct qemu_alarm_timer *t)
1491 struct qemu_alarm_win32 *data = t->priv;
1493 timeKillEvent(data->timerId);
1494 timeEndPeriod(data->period);
1497 static void win32_rearm_timer(struct qemu_alarm_timer *t)
1499 struct qemu_alarm_win32 *data = t->priv;
1500 uint64_t nearest_delta_us;
1502 if (!active_timers[QEMU_TIMER_REALTIME] &&
1503 !active_timers[QEMU_TIMER_VIRTUAL])
1504 return;
1506 nearest_delta_us = qemu_next_deadline_dyntick();
1507 nearest_delta_us /= 1000;
1509 timeKillEvent(data->timerId);
1511 data->timerId = timeSetEvent(1,
1512 data->period,
1513 host_alarm_handler,
1514 (DWORD)t,
1515 TIME_ONESHOT | TIME_PERIODIC);
1517 if (!data->timerId) {
1518 perror("Failed to re-arm win32 alarm timer");
1520 timeEndPeriod(data->period);
1521 exit(1);
1525 #endif /* _WIN32 */
1527 static int init_timer_alarm(void)
1529 struct qemu_alarm_timer *t = NULL;
1530 int i, err = -1;
1532 for (i = 0; alarm_timers[i].name; i++) {
1533 t = &alarm_timers[i];
1535 err = t->start(t);
1536 if (!err)
1537 break;
1540 if (err) {
1541 err = -ENOENT;
1542 goto fail;
1545 alarm_timer = t;
1547 return 0;
1549 fail:
1550 return err;
1553 static void quit_timers(void)
1555 alarm_timer->stop(alarm_timer);
1556 alarm_timer = NULL;
1559 /***********************************************************/
1560 /* host time/date access */
1561 void qemu_get_timedate(struct tm *tm, int offset)
1563 time_t ti;
1564 struct tm *ret;
1566 time(&ti);
1567 ti += offset;
1568 if (rtc_date_offset == -1) {
1569 if (rtc_utc)
1570 ret = gmtime(&ti);
1571 else
1572 ret = localtime(&ti);
1573 } else {
1574 ti -= rtc_date_offset;
1575 ret = gmtime(&ti);
1578 memcpy(tm, ret, sizeof(struct tm));
1581 int qemu_timedate_diff(struct tm *tm)
1583 time_t seconds;
1585 if (rtc_date_offset == -1)
1586 if (rtc_utc)
1587 seconds = mktimegm(tm);
1588 else
1589 seconds = mktime(tm);
1590 else
1591 seconds = mktimegm(tm) + rtc_date_offset;
1593 return seconds - time(NULL);
1596 #ifdef _WIN32
1597 static void socket_cleanup(void)
1599 WSACleanup();
1602 static int socket_init(void)
1604 WSADATA Data;
1605 int ret, err;
1607 ret = WSAStartup(MAKEWORD(2,2), &Data);
1608 if (ret != 0) {
1609 err = WSAGetLastError();
1610 fprintf(stderr, "WSAStartup: %d\n", err);
1611 return -1;
1613 atexit(socket_cleanup);
1614 return 0;
1616 #endif
1618 int get_next_param_value(char *buf, int buf_size,
1619 const char *tag, const char **pstr)
1621 const char *p;
1622 char option[128];
1624 p = *pstr;
1625 for(;;) {
1626 p = get_opt_name(option, sizeof(option), p, '=');
1627 if (*p != '=')
1628 break;
1629 p++;
1630 if (!strcmp(tag, option)) {
1631 *pstr = get_opt_value(buf, buf_size, p);
1632 if (**pstr == ',') {
1633 (*pstr)++;
1635 return strlen(buf);
1636 } else {
1637 p = get_opt_value(NULL, 0, p);
1639 if (*p != ',')
1640 break;
1641 p++;
1643 return 0;
1646 int get_param_value(char *buf, int buf_size,
1647 const char *tag, const char *str)
1649 return get_next_param_value(buf, buf_size, tag, &str);
1652 int check_params(char *buf, int buf_size,
1653 const char * const *params, const char *str)
1655 const char *p;
1656 int i;
1658 p = str;
1659 while (*p != '\0') {
1660 p = get_opt_name(buf, buf_size, p, '=');
1661 if (*p != '=') {
1662 return -1;
1664 p++;
1665 for (i = 0; params[i] != NULL; i++) {
1666 if (!strcmp(params[i], buf)) {
1667 break;
1670 if (params[i] == NULL) {
1671 return -1;
1673 p = get_opt_value(NULL, 0, p);
1674 if (*p != ',') {
1675 break;
1677 p++;
1679 return 0;
1682 /***********************************************************/
1683 /* Bluetooth support */
1684 static int nb_hcis;
1685 static int cur_hci;
1686 static struct HCIInfo *hci_table[MAX_NICS];
1688 static struct bt_vlan_s {
1689 struct bt_scatternet_s net;
1690 int id;
1691 struct bt_vlan_s *next;
1692 } *first_bt_vlan;
1694 /* find or alloc a new bluetooth "VLAN" */
1695 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1697 struct bt_vlan_s **pvlan, *vlan;
1698 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1699 if (vlan->id == id)
1700 return &vlan->net;
1702 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1703 vlan->id = id;
1704 pvlan = &first_bt_vlan;
1705 while (*pvlan != NULL)
1706 pvlan = &(*pvlan)->next;
1707 *pvlan = vlan;
1708 return &vlan->net;
1711 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1715 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1717 return -ENOTSUP;
1720 static struct HCIInfo null_hci = {
1721 .cmd_send = null_hci_send,
1722 .sco_send = null_hci_send,
1723 .acl_send = null_hci_send,
1724 .bdaddr_set = null_hci_addr_set,
1727 struct HCIInfo *qemu_next_hci(void)
1729 if (cur_hci == nb_hcis)
1730 return &null_hci;
1732 return hci_table[cur_hci++];
1735 static struct HCIInfo *hci_init(const char *str)
1737 char *endp;
1738 struct bt_scatternet_s *vlan = 0;
1740 if (!strcmp(str, "null"))
1741 /* null */
1742 return &null_hci;
1743 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1744 /* host[:hciN] */
1745 return bt_host_hci(str[4] ? str + 5 : "hci0");
1746 else if (!strncmp(str, "hci", 3)) {
1747 /* hci[,vlan=n] */
1748 if (str[3]) {
1749 if (!strncmp(str + 3, ",vlan=", 6)) {
1750 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1751 if (*endp)
1752 vlan = 0;
1754 } else
1755 vlan = qemu_find_bt_vlan(0);
1756 if (vlan)
1757 return bt_new_hci(vlan);
1760 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1762 return 0;
1765 static int bt_hci_parse(const char *str)
1767 struct HCIInfo *hci;
1768 bdaddr_t bdaddr;
1770 if (nb_hcis >= MAX_NICS) {
1771 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1772 return -1;
1775 hci = hci_init(str);
1776 if (!hci)
1777 return -1;
1779 bdaddr.b[0] = 0x52;
1780 bdaddr.b[1] = 0x54;
1781 bdaddr.b[2] = 0x00;
1782 bdaddr.b[3] = 0x12;
1783 bdaddr.b[4] = 0x34;
1784 bdaddr.b[5] = 0x56 + nb_hcis;
1785 hci->bdaddr_set(hci, bdaddr.b);
1787 hci_table[nb_hcis++] = hci;
1789 return 0;
1792 static void bt_vhci_add(int vlan_id)
1794 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1796 if (!vlan->slave)
1797 fprintf(stderr, "qemu: warning: adding a VHCI to "
1798 "an empty scatternet %i\n", vlan_id);
1800 bt_vhci_init(bt_new_hci(vlan));
1803 static struct bt_device_s *bt_device_add(const char *opt)
1805 struct bt_scatternet_s *vlan;
1806 int vlan_id = 0;
1807 char *endp = strstr(opt, ",vlan=");
1808 int len = (endp ? endp - opt : strlen(opt)) + 1;
1809 char devname[10];
1811 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1813 if (endp) {
1814 vlan_id = strtol(endp + 6, &endp, 0);
1815 if (*endp) {
1816 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1817 return 0;
1821 vlan = qemu_find_bt_vlan(vlan_id);
1823 if (!vlan->slave)
1824 fprintf(stderr, "qemu: warning: adding a slave device to "
1825 "an empty scatternet %i\n", vlan_id);
1827 if (!strcmp(devname, "keyboard"))
1828 return bt_keyboard_init(vlan);
1830 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1831 return 0;
1834 static int bt_parse(const char *opt)
1836 const char *endp, *p;
1837 int vlan;
1839 if (strstart(opt, "hci", &endp)) {
1840 if (!*endp || *endp == ',') {
1841 if (*endp)
1842 if (!strstart(endp, ",vlan=", 0))
1843 opt = endp + 1;
1845 return bt_hci_parse(opt);
1847 } else if (strstart(opt, "vhci", &endp)) {
1848 if (!*endp || *endp == ',') {
1849 if (*endp) {
1850 if (strstart(endp, ",vlan=", &p)) {
1851 vlan = strtol(p, (char **) &endp, 0);
1852 if (*endp) {
1853 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1854 return 1;
1856 } else {
1857 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1858 return 1;
1860 } else
1861 vlan = 0;
1863 bt_vhci_add(vlan);
1864 return 0;
1866 } else if (strstart(opt, "device:", &endp))
1867 return !bt_device_add(endp);
1869 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1870 return 1;
1873 /***********************************************************/
1874 /* QEMU Block devices */
1876 #define HD_ALIAS "index=%d,media=disk"
1877 #define CDROM_ALIAS "index=2,media=cdrom"
1878 #define FD_ALIAS "index=%d,if=floppy"
1879 #define PFLASH_ALIAS "if=pflash"
1880 #define MTD_ALIAS "if=mtd"
1881 #define SD_ALIAS "index=0,if=sd"
1883 static int drive_opt_get_free_idx(void)
1885 int index;
1887 for (index = 0; index < MAX_DRIVES; index++)
1888 if (!drives_opt[index].used) {
1889 drives_opt[index].used = 1;
1890 return index;
1893 return -1;
1896 static int drive_get_free_idx(void)
1898 int index;
1900 for (index = 0; index < MAX_DRIVES; index++)
1901 if (!drives_table[index].used) {
1902 drives_table[index].used = 1;
1903 return index;
1906 return -1;
1909 int drive_add(const char *file, const char *fmt, ...)
1911 va_list ap;
1912 int index = drive_opt_get_free_idx();
1914 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
1915 fprintf(stderr, "qemu: too many drives\n");
1916 return -1;
1919 drives_opt[index].file = file;
1920 va_start(ap, fmt);
1921 vsnprintf(drives_opt[index].opt,
1922 sizeof(drives_opt[0].opt), fmt, ap);
1923 va_end(ap);
1925 nb_drives_opt++;
1926 return index;
1929 void drive_remove(int index)
1931 drives_opt[index].used = 0;
1932 nb_drives_opt--;
1935 int drive_get_index(BlockInterfaceType type, int bus, int unit)
1937 int index;
1939 /* seek interface, bus and unit */
1941 for (index = 0; index < MAX_DRIVES; index++)
1942 if (drives_table[index].type == type &&
1943 drives_table[index].bus == bus &&
1944 drives_table[index].unit == unit &&
1945 drives_table[index].used)
1946 return index;
1948 return -1;
1951 int drive_get_max_bus(BlockInterfaceType type)
1953 int max_bus;
1954 int index;
1956 max_bus = -1;
1957 for (index = 0; index < nb_drives; index++) {
1958 if(drives_table[index].type == type &&
1959 drives_table[index].bus > max_bus)
1960 max_bus = drives_table[index].bus;
1962 return max_bus;
1965 const char *drive_get_serial(BlockDriverState *bdrv)
1967 int index;
1969 for (index = 0; index < nb_drives; index++)
1970 if (drives_table[index].bdrv == bdrv)
1971 return drives_table[index].serial;
1973 return "\0";
1976 BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1978 int index;
1980 for (index = 0; index < nb_drives; index++)
1981 if (drives_table[index].bdrv == bdrv)
1982 return drives_table[index].onerror;
1984 return BLOCK_ERR_STOP_ENOSPC;
1987 static void bdrv_format_print(void *opaque, const char *name)
1989 fprintf(stderr, " %s", name);
1992 void drive_uninit(BlockDriverState *bdrv)
1994 int i;
1996 for (i = 0; i < MAX_DRIVES; i++)
1997 if (drives_table[i].bdrv == bdrv) {
1998 drives_table[i].bdrv = NULL;
1999 drives_table[i].used = 0;
2000 drive_remove(drives_table[i].drive_opt_idx);
2001 nb_drives--;
2002 break;
2006 int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
2008 char buf[128];
2009 char file[1024];
2010 char devname[128];
2011 char serial[21];
2012 const char *mediastr = "";
2013 BlockInterfaceType type;
2014 enum { MEDIA_DISK, MEDIA_CDROM } media;
2015 int bus_id, unit_id;
2016 int cyls, heads, secs, translation;
2017 BlockDriverState *bdrv;
2018 BlockDriver *drv = NULL;
2019 QEMUMachine *machine = opaque;
2020 int max_devs;
2021 int index;
2022 int cache;
2023 int bdrv_flags, onerror;
2024 const char *devaddr;
2025 int drives_table_idx;
2026 char *str = arg->opt;
2027 static const char * const params[] = { "bus", "unit", "if", "index",
2028 "cyls", "heads", "secs", "trans",
2029 "media", "snapshot", "file",
2030 "cache", "format", "serial",
2031 "werror", "addr",
2032 "boot",
2033 NULL };
2035 if (check_params(buf, sizeof(buf), params, str) < 0) {
2036 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2037 buf, str);
2038 return -1;
2041 file[0] = 0;
2042 cyls = heads = secs = 0;
2043 bus_id = 0;
2044 unit_id = -1;
2045 translation = BIOS_ATA_TRANSLATION_AUTO;
2046 index = -1;
2047 cache = 1;
2049 if (machine->use_scsi) {
2050 type = IF_SCSI;
2051 max_devs = MAX_SCSI_DEVS;
2052 pstrcpy(devname, sizeof(devname), "scsi");
2053 } else {
2054 type = IF_IDE;
2055 max_devs = MAX_IDE_DEVS;
2056 pstrcpy(devname, sizeof(devname), "ide");
2058 media = MEDIA_DISK;
2060 /* extract parameters */
2062 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2063 bus_id = strtol(buf, NULL, 0);
2064 if (bus_id < 0) {
2065 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2066 return -1;
2070 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2071 unit_id = strtol(buf, NULL, 0);
2072 if (unit_id < 0) {
2073 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2074 return -1;
2078 if (get_param_value(buf, sizeof(buf), "if", str)) {
2079 pstrcpy(devname, sizeof(devname), buf);
2080 if (!strcmp(buf, "ide")) {
2081 type = IF_IDE;
2082 max_devs = MAX_IDE_DEVS;
2083 } else if (!strcmp(buf, "scsi")) {
2084 type = IF_SCSI;
2085 max_devs = MAX_SCSI_DEVS;
2086 } else if (!strcmp(buf, "floppy")) {
2087 type = IF_FLOPPY;
2088 max_devs = 0;
2089 } else if (!strcmp(buf, "pflash")) {
2090 type = IF_PFLASH;
2091 max_devs = 0;
2092 } else if (!strcmp(buf, "mtd")) {
2093 type = IF_MTD;
2094 max_devs = 0;
2095 } else if (!strcmp(buf, "sd")) {
2096 type = IF_SD;
2097 max_devs = 0;
2098 } else if (!strcmp(buf, "virtio")) {
2099 type = IF_VIRTIO;
2100 max_devs = 0;
2101 } else if (!strcmp(buf, "xen")) {
2102 type = IF_XEN;
2103 max_devs = 0;
2104 } else {
2105 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2106 return -1;
2110 if (get_param_value(buf, sizeof(buf), "index", str)) {
2111 index = strtol(buf, NULL, 0);
2112 if (index < 0) {
2113 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2114 return -1;
2118 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2119 cyls = strtol(buf, NULL, 0);
2122 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2123 heads = strtol(buf, NULL, 0);
2126 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2127 secs = strtol(buf, NULL, 0);
2130 if (cyls || heads || secs) {
2131 if (cyls < 1 || cyls > 16383) {
2132 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2133 return -1;
2135 if (heads < 1 || heads > 16) {
2136 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2137 return -1;
2139 if (secs < 1 || secs > 63) {
2140 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2141 return -1;
2145 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2146 if (!cyls) {
2147 fprintf(stderr,
2148 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2149 str);
2150 return -1;
2152 if (!strcmp(buf, "none"))
2153 translation = BIOS_ATA_TRANSLATION_NONE;
2154 else if (!strcmp(buf, "lba"))
2155 translation = BIOS_ATA_TRANSLATION_LBA;
2156 else if (!strcmp(buf, "auto"))
2157 translation = BIOS_ATA_TRANSLATION_AUTO;
2158 else {
2159 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2160 return -1;
2164 if (get_param_value(buf, sizeof(buf), "media", str)) {
2165 if (!strcmp(buf, "disk")) {
2166 media = MEDIA_DISK;
2167 } else if (!strcmp(buf, "cdrom")) {
2168 if (cyls || secs || heads) {
2169 fprintf(stderr,
2170 "qemu: '%s' invalid physical CHS format\n", str);
2171 return -1;
2173 media = MEDIA_CDROM;
2174 } else {
2175 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2176 return -1;
2180 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2181 if (!strcmp(buf, "on"))
2182 snapshot = 1;
2183 else if (!strcmp(buf, "off"))
2184 snapshot = 0;
2185 else {
2186 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2187 return -1;
2191 if (get_param_value(buf, sizeof(buf), "cache", str)) {
2192 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2193 cache = 0;
2194 else if (!strcmp(buf, "writethrough"))
2195 cache = 1;
2196 else if (!strcmp(buf, "writeback"))
2197 cache = 2;
2198 else {
2199 fprintf(stderr, "qemu: invalid cache option\n");
2200 return -1;
2204 if (get_param_value(buf, sizeof(buf), "format", str)) {
2205 if (strcmp(buf, "?") == 0) {
2206 fprintf(stderr, "qemu: Supported formats:");
2207 bdrv_iterate_format(bdrv_format_print, NULL);
2208 fprintf(stderr, "\n");
2209 return -1;
2211 drv = bdrv_find_format(buf);
2212 if (!drv) {
2213 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2214 return -1;
2218 if (get_param_value(buf, sizeof(buf), "boot", str)) {
2219 if (!strcmp(buf, "on")) {
2220 if (extboot_drive != -1) {
2221 fprintf(stderr, "qemu: two bootable drives specified\n");
2222 return -1;
2224 extboot_drive = nb_drives;
2225 } else if (strcmp(buf, "off")) {
2226 fprintf(stderr, "qemu: '%s' invalid boot option\n", str);
2227 return -1;
2231 if (arg->file == NULL)
2232 get_param_value(file, sizeof(file), "file", str);
2233 else
2234 pstrcpy(file, sizeof(file), arg->file);
2236 if (!get_param_value(serial, sizeof(serial), "serial", str))
2237 memset(serial, 0, sizeof(serial));
2239 onerror = BLOCK_ERR_STOP_ENOSPC;
2240 if (get_param_value(buf, sizeof(serial), "werror", str)) {
2241 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
2242 fprintf(stderr, "werror is no supported by this format\n");
2243 return -1;
2245 if (!strcmp(buf, "ignore"))
2246 onerror = BLOCK_ERR_IGNORE;
2247 else if (!strcmp(buf, "enospc"))
2248 onerror = BLOCK_ERR_STOP_ENOSPC;
2249 else if (!strcmp(buf, "stop"))
2250 onerror = BLOCK_ERR_STOP_ANY;
2251 else if (!strcmp(buf, "report"))
2252 onerror = BLOCK_ERR_REPORT;
2253 else {
2254 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2255 return -1;
2259 devaddr = NULL;
2260 if (get_param_value(buf, sizeof(buf), "addr", str)) {
2261 if (type != IF_VIRTIO) {
2262 fprintf(stderr, "addr is not supported by in '%s'\n", str);
2263 return -1;
2265 devaddr = strdup(buf);
2268 /* compute bus and unit according index */
2270 if (index != -1) {
2271 if (bus_id != 0 || unit_id != -1) {
2272 fprintf(stderr,
2273 "qemu: '%s' index cannot be used with bus and unit\n", str);
2274 return -1;
2276 if (max_devs == 0)
2278 unit_id = index;
2279 bus_id = 0;
2280 } else {
2281 unit_id = index % max_devs;
2282 bus_id = index / max_devs;
2286 /* if user doesn't specify a unit_id,
2287 * try to find the first free
2290 if (unit_id == -1) {
2291 unit_id = 0;
2292 while (drive_get_index(type, bus_id, unit_id) != -1) {
2293 unit_id++;
2294 if (max_devs && unit_id >= max_devs) {
2295 unit_id -= max_devs;
2296 bus_id++;
2301 /* check unit id */
2303 if (max_devs && unit_id >= max_devs) {
2304 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2305 str, unit_id, max_devs - 1);
2306 return -1;
2310 * ignore multiple definitions
2313 if (drive_get_index(type, bus_id, unit_id) != -1)
2314 return -2;
2316 /* init */
2318 if (type == IF_IDE || type == IF_SCSI)
2319 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2320 if (max_devs)
2321 snprintf(buf, sizeof(buf), "%s%i%s%i",
2322 devname, bus_id, mediastr, unit_id);
2323 else
2324 snprintf(buf, sizeof(buf), "%s%s%i",
2325 devname, mediastr, unit_id);
2326 bdrv = bdrv_new(buf);
2327 drives_table_idx = drive_get_free_idx();
2328 drives_table[drives_table_idx].bdrv = bdrv;
2329 drives_table[drives_table_idx].devaddr = devaddr;
2330 drives_table[drives_table_idx].type = type;
2331 drives_table[drives_table_idx].bus = bus_id;
2332 drives_table[drives_table_idx].unit = unit_id;
2333 drives_table[drives_table_idx].onerror = onerror;
2334 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
2335 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
2336 nb_drives++;
2338 switch(type) {
2339 case IF_IDE:
2340 case IF_SCSI:
2341 case IF_XEN:
2342 switch(media) {
2343 case MEDIA_DISK:
2344 if (cyls != 0) {
2345 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2346 bdrv_set_translation_hint(bdrv, translation);
2348 break;
2349 case MEDIA_CDROM:
2350 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2351 break;
2353 break;
2354 case IF_SD:
2355 /* FIXME: This isn't really a floppy, but it's a reasonable
2356 approximation. */
2357 case IF_FLOPPY:
2358 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2359 break;
2360 case IF_PFLASH:
2361 case IF_MTD:
2362 case IF_VIRTIO:
2363 break;
2364 case IF_COUNT:
2365 abort();
2367 if (!file[0])
2368 return -2;
2369 bdrv_flags = 0;
2370 if (snapshot) {
2371 bdrv_flags |= BDRV_O_SNAPSHOT;
2372 cache = 2; /* always use write-back with snapshot */
2374 if (cache == 0) /* no caching */
2375 bdrv_flags |= BDRV_O_NOCACHE;
2376 else if (cache == 2) /* write-back */
2377 bdrv_flags |= BDRV_O_CACHE_WB;
2378 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
2379 fprintf(stderr, "qemu: could not open disk image %s\n",
2380 file);
2381 return -1;
2383 if (bdrv_key_required(bdrv))
2384 autostart = 0;
2385 return drives_table_idx;
2388 static void numa_add(const char *optarg)
2390 char option[128];
2391 char *endptr;
2392 unsigned long long value, endvalue;
2393 int nodenr;
2395 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2396 if (!strcmp(option, "node")) {
2397 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2398 nodenr = nb_numa_nodes;
2399 } else {
2400 nodenr = strtoull(option, NULL, 10);
2403 if (get_param_value(option, 128, "mem", optarg) == 0) {
2404 node_mem[nodenr] = 0;
2405 } else {
2406 value = strtoull(option, &endptr, 0);
2407 switch (*endptr) {
2408 case 0: case 'M': case 'm':
2409 value <<= 20;
2410 break;
2411 case 'G': case 'g':
2412 value <<= 30;
2413 break;
2415 node_mem[nodenr] = value;
2417 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2418 node_cpumask[nodenr] = 0;
2419 } else {
2420 value = strtoull(option, &endptr, 10);
2421 if (value >= 64) {
2422 value = 63;
2423 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2424 } else {
2425 if (*endptr == '-') {
2426 endvalue = strtoull(endptr+1, &endptr, 10);
2427 if (endvalue >= 63) {
2428 endvalue = 62;
2429 fprintf(stderr,
2430 "only 63 CPUs in NUMA mode supported.\n");
2432 value = (1 << (endvalue + 1)) - (1 << value);
2433 } else {
2434 value = 1 << value;
2437 node_cpumask[nodenr] = value;
2439 nb_numa_nodes++;
2441 return;
2444 /***********************************************************/
2445 /* USB devices */
2447 static USBPort *used_usb_ports;
2448 static USBPort *free_usb_ports;
2450 /* ??? Maybe change this to register a hub to keep track of the topology. */
2451 void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2452 usb_attachfn attach)
2454 port->opaque = opaque;
2455 port->index = index;
2456 port->attach = attach;
2457 port->next = free_usb_ports;
2458 free_usb_ports = port;
2461 int usb_device_add_dev(USBDevice *dev)
2463 USBPort *port;
2465 /* Find a USB port to add the device to. */
2466 port = free_usb_ports;
2467 if (!port->next) {
2468 USBDevice *hub;
2470 /* Create a new hub and chain it on. */
2471 free_usb_ports = NULL;
2472 port->next = used_usb_ports;
2473 used_usb_ports = port;
2475 hub = usb_hub_init(VM_USB_HUB_SIZE);
2476 usb_attach(port, hub);
2477 port = free_usb_ports;
2480 free_usb_ports = port->next;
2481 port->next = used_usb_ports;
2482 used_usb_ports = port;
2483 usb_attach(port, dev);
2484 return 0;
2487 static void usb_msd_password_cb(void *opaque, int err)
2489 USBDevice *dev = opaque;
2491 if (!err)
2492 usb_device_add_dev(dev);
2493 else
2494 dev->handle_destroy(dev);
2497 static int usb_device_add(const char *devname, int is_hotplug)
2499 const char *p;
2500 USBDevice *dev;
2502 if (!free_usb_ports)
2503 return -1;
2505 if (strstart(devname, "host:", &p)) {
2506 dev = usb_host_device_open(p);
2507 } else if (!strcmp(devname, "mouse")) {
2508 dev = usb_mouse_init();
2509 } else if (!strcmp(devname, "tablet")) {
2510 dev = usb_tablet_init();
2511 } else if (!strcmp(devname, "keyboard")) {
2512 dev = usb_keyboard_init();
2513 } else if (strstart(devname, "disk:", &p)) {
2514 BlockDriverState *bs;
2516 dev = usb_msd_init(p);
2517 if (!dev)
2518 return -1;
2519 bs = usb_msd_get_bdrv(dev);
2520 if (bdrv_key_required(bs)) {
2521 autostart = 0;
2522 if (is_hotplug) {
2523 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2524 dev);
2525 return 0;
2528 } else if (!strcmp(devname, "wacom-tablet")) {
2529 dev = usb_wacom_init();
2530 } else if (strstart(devname, "serial:", &p)) {
2531 dev = usb_serial_init(p);
2532 #ifdef CONFIG_BRLAPI
2533 } else if (!strcmp(devname, "braille")) {
2534 dev = usb_baum_init();
2535 #endif
2536 } else if (strstart(devname, "net:", &p)) {
2537 int nic = nb_nics;
2539 if (net_client_init(NULL, "nic", p) < 0)
2540 return -1;
2541 nd_table[nic].model = "usb";
2542 dev = usb_net_init(&nd_table[nic]);
2543 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2544 dev = usb_bt_init(devname[2] ? hci_init(p) :
2545 bt_new_hci(qemu_find_bt_vlan(0)));
2546 } else {
2547 return -1;
2549 if (!dev)
2550 return -1;
2552 return usb_device_add_dev(dev);
2555 int usb_device_del_addr(int bus_num, int addr)
2557 USBPort *port;
2558 USBPort **lastp;
2559 USBDevice *dev;
2561 if (!used_usb_ports)
2562 return -1;
2564 if (bus_num != 0)
2565 return -1;
2567 lastp = &used_usb_ports;
2568 port = used_usb_ports;
2569 while (port && port->dev->addr != addr) {
2570 lastp = &port->next;
2571 port = port->next;
2574 if (!port)
2575 return -1;
2577 dev = port->dev;
2578 *lastp = port->next;
2579 usb_attach(port, NULL);
2580 dev->handle_destroy(dev);
2581 port->next = free_usb_ports;
2582 free_usb_ports = port;
2583 return 0;
2586 static int usb_device_del(const char *devname)
2588 int bus_num, addr;
2589 const char *p;
2591 if (strstart(devname, "host:", &p))
2592 return usb_host_device_close(p);
2594 if (!used_usb_ports)
2595 return -1;
2597 p = strchr(devname, '.');
2598 if (!p)
2599 return -1;
2600 bus_num = strtoul(devname, NULL, 0);
2601 addr = strtoul(p + 1, NULL, 0);
2603 return usb_device_del_addr(bus_num, addr);
2606 void do_usb_add(Monitor *mon, const char *devname)
2608 usb_device_add(devname, 1);
2611 void do_usb_del(Monitor *mon, const char *devname)
2613 usb_device_del(devname);
2616 void usb_info(Monitor *mon)
2618 USBDevice *dev;
2619 USBPort *port;
2620 const char *speed_str;
2622 if (!usb_enabled) {
2623 monitor_printf(mon, "USB support not enabled\n");
2624 return;
2627 for (port = used_usb_ports; port; port = port->next) {
2628 dev = port->dev;
2629 if (!dev)
2630 continue;
2631 switch(dev->speed) {
2632 case USB_SPEED_LOW:
2633 speed_str = "1.5";
2634 break;
2635 case USB_SPEED_FULL:
2636 speed_str = "12";
2637 break;
2638 case USB_SPEED_HIGH:
2639 speed_str = "480";
2640 break;
2641 default:
2642 speed_str = "?";
2643 break;
2645 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2646 0, dev->addr, speed_str, dev->devname);
2650 /***********************************************************/
2651 /* PCMCIA/Cardbus */
2653 static struct pcmcia_socket_entry_s {
2654 PCMCIASocket *socket;
2655 struct pcmcia_socket_entry_s *next;
2656 } *pcmcia_sockets = 0;
2658 void pcmcia_socket_register(PCMCIASocket *socket)
2660 struct pcmcia_socket_entry_s *entry;
2662 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2663 entry->socket = socket;
2664 entry->next = pcmcia_sockets;
2665 pcmcia_sockets = entry;
2668 void pcmcia_socket_unregister(PCMCIASocket *socket)
2670 struct pcmcia_socket_entry_s *entry, **ptr;
2672 ptr = &pcmcia_sockets;
2673 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2674 if (entry->socket == socket) {
2675 *ptr = entry->next;
2676 qemu_free(entry);
2680 void pcmcia_info(Monitor *mon)
2682 struct pcmcia_socket_entry_s *iter;
2684 if (!pcmcia_sockets)
2685 monitor_printf(mon, "No PCMCIA sockets\n");
2687 for (iter = pcmcia_sockets; iter; iter = iter->next)
2688 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2689 iter->socket->attached ? iter->socket->card_string :
2690 "Empty");
2693 /***********************************************************/
2694 /* register display */
2696 struct DisplayAllocator default_allocator = {
2697 defaultallocator_create_displaysurface,
2698 defaultallocator_resize_displaysurface,
2699 defaultallocator_free_displaysurface
2702 void register_displaystate(DisplayState *ds)
2704 DisplayState **s;
2705 s = &display_state;
2706 while (*s != NULL)
2707 s = &(*s)->next;
2708 ds->next = NULL;
2709 *s = ds;
2712 DisplayState *get_displaystate(void)
2714 return display_state;
2717 DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2719 if(ds->allocator == &default_allocator) ds->allocator = da;
2720 return ds->allocator;
2723 /* dumb display */
2725 static void dumb_display_init(void)
2727 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
2728 ds->allocator = &default_allocator;
2729 ds->surface = qemu_create_displaysurface(ds, 640, 480);
2730 register_displaystate(ds);
2733 /***********************************************************/
2734 /* I/O handling */
2736 typedef struct IOHandlerRecord {
2737 int fd;
2738 IOCanRWHandler *fd_read_poll;
2739 IOHandler *fd_read;
2740 IOHandler *fd_write;
2741 int deleted;
2742 void *opaque;
2743 /* temporary data */
2744 struct pollfd *ufd;
2745 struct IOHandlerRecord *next;
2746 } IOHandlerRecord;
2748 static IOHandlerRecord *first_io_handler;
2750 /* XXX: fd_read_poll should be suppressed, but an API change is
2751 necessary in the character devices to suppress fd_can_read(). */
2752 int qemu_set_fd_handler2(int fd,
2753 IOCanRWHandler *fd_read_poll,
2754 IOHandler *fd_read,
2755 IOHandler *fd_write,
2756 void *opaque)
2758 IOHandlerRecord **pioh, *ioh;
2760 if (!fd_read && !fd_write) {
2761 pioh = &first_io_handler;
2762 for(;;) {
2763 ioh = *pioh;
2764 if (ioh == NULL)
2765 break;
2766 if (ioh->fd == fd) {
2767 ioh->deleted = 1;
2768 break;
2770 pioh = &ioh->next;
2772 } else {
2773 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2774 if (ioh->fd == fd)
2775 goto found;
2777 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2778 ioh->next = first_io_handler;
2779 first_io_handler = ioh;
2780 found:
2781 ioh->fd = fd;
2782 ioh->fd_read_poll = fd_read_poll;
2783 ioh->fd_read = fd_read;
2784 ioh->fd_write = fd_write;
2785 ioh->opaque = opaque;
2786 ioh->deleted = 0;
2788 qemu_notify_event();
2789 return 0;
2792 int qemu_set_fd_handler(int fd,
2793 IOHandler *fd_read,
2794 IOHandler *fd_write,
2795 void *opaque)
2797 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2800 #ifdef _WIN32
2801 /***********************************************************/
2802 /* Polling handling */
2804 typedef struct PollingEntry {
2805 PollingFunc *func;
2806 void *opaque;
2807 struct PollingEntry *next;
2808 } PollingEntry;
2810 static PollingEntry *first_polling_entry;
2812 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2814 PollingEntry **ppe, *pe;
2815 pe = qemu_mallocz(sizeof(PollingEntry));
2816 pe->func = func;
2817 pe->opaque = opaque;
2818 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2819 *ppe = pe;
2820 return 0;
2823 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2825 PollingEntry **ppe, *pe;
2826 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2827 pe = *ppe;
2828 if (pe->func == func && pe->opaque == opaque) {
2829 *ppe = pe->next;
2830 qemu_free(pe);
2831 break;
2836 /***********************************************************/
2837 /* Wait objects support */
2838 typedef struct WaitObjects {
2839 int num;
2840 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2841 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2842 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2843 } WaitObjects;
2845 static WaitObjects wait_objects = {0};
2847 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2849 WaitObjects *w = &wait_objects;
2851 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2852 return -1;
2853 w->events[w->num] = handle;
2854 w->func[w->num] = func;
2855 w->opaque[w->num] = opaque;
2856 w->num++;
2857 return 0;
2860 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2862 int i, found;
2863 WaitObjects *w = &wait_objects;
2865 found = 0;
2866 for (i = 0; i < w->num; i++) {
2867 if (w->events[i] == handle)
2868 found = 1;
2869 if (found) {
2870 w->events[i] = w->events[i + 1];
2871 w->func[i] = w->func[i + 1];
2872 w->opaque[i] = w->opaque[i + 1];
2875 if (found)
2876 w->num--;
2878 #endif
2880 /***********************************************************/
2881 /* ram save/restore */
2883 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2885 int v;
2887 v = qemu_get_byte(f);
2888 switch(v) {
2889 case 0:
2890 if (qemu_get_buffer(f, buf, len) != len)
2891 return -EIO;
2892 break;
2893 case 1:
2894 v = qemu_get_byte(f);
2895 memset(buf, v, len);
2896 break;
2897 default:
2898 return -EINVAL;
2901 if (qemu_file_has_error(f))
2902 return -EIO;
2904 return 0;
2907 static int ram_load_v1(QEMUFile *f, void *opaque)
2909 int ret;
2910 ram_addr_t i;
2912 if (qemu_get_be32(f) != last_ram_offset)
2913 return -EINVAL;
2914 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
2915 if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
2916 continue;
2917 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
2918 if (ret)
2919 return ret;
2921 return 0;
2924 #define BDRV_HASH_BLOCK_SIZE 1024
2925 #define IOBUF_SIZE 4096
2926 #define RAM_CBLOCK_MAGIC 0xfabe
2928 typedef struct RamDecompressState {
2929 z_stream zstream;
2930 QEMUFile *f;
2931 uint8_t buf[IOBUF_SIZE];
2932 } RamDecompressState;
2934 static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2936 int ret;
2937 memset(s, 0, sizeof(*s));
2938 s->f = f;
2939 ret = inflateInit(&s->zstream);
2940 if (ret != Z_OK)
2941 return -1;
2942 return 0;
2945 static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2947 int ret, clen;
2949 s->zstream.avail_out = len;
2950 s->zstream.next_out = buf;
2951 while (s->zstream.avail_out > 0) {
2952 if (s->zstream.avail_in == 0) {
2953 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
2954 return -1;
2955 clen = qemu_get_be16(s->f);
2956 if (clen > IOBUF_SIZE)
2957 return -1;
2958 qemu_get_buffer(s->f, s->buf, clen);
2959 s->zstream.avail_in = clen;
2960 s->zstream.next_in = s->buf;
2962 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
2963 if (ret != Z_OK && ret != Z_STREAM_END) {
2964 return -1;
2967 return 0;
2970 static void ram_decompress_close(RamDecompressState *s)
2972 inflateEnd(&s->zstream);
2975 #define RAM_SAVE_FLAG_FULL 0x01
2976 #define RAM_SAVE_FLAG_COMPRESS 0x02
2977 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2978 #define RAM_SAVE_FLAG_PAGE 0x08
2979 #define RAM_SAVE_FLAG_EOS 0x10
2981 static int is_dup_page(uint8_t *page, uint8_t ch)
2983 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2984 uint32_t *array = (uint32_t *)page;
2985 int i;
2987 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2988 if (array[i] != val)
2989 return 0;
2992 return 1;
2995 static int ram_save_block(QEMUFile *f)
2997 static ram_addr_t current_addr = 0;
2998 ram_addr_t saved_addr = current_addr;
2999 ram_addr_t addr = 0;
3000 int found = 0;
3002 while (addr < last_ram_offset) {
3003 if (kvm_enabled() && current_addr == 0) {
3004 int r;
3005 r = kvm_update_dirty_pages_log();
3006 if (r) {
3007 fprintf(stderr, "%s: update dirty pages log failed %d\n", __FUNCTION__, r);
3008 qemu_file_set_error(f);
3009 return 0;
3012 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3013 uint8_t *p;
3015 cpu_physical_memory_reset_dirty(current_addr,
3016 current_addr + TARGET_PAGE_SIZE,
3017 MIGRATION_DIRTY_FLAG);
3019 p = qemu_get_ram_ptr(current_addr);
3021 if (is_dup_page(p, *p)) {
3022 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3023 qemu_put_byte(f, *p);
3024 } else {
3025 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3026 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
3029 found = 1;
3030 break;
3032 addr += TARGET_PAGE_SIZE;
3033 current_addr = (saved_addr + addr) % last_ram_offset;
3036 return found;
3039 static uint64_t bytes_transferred = 0;
3041 static ram_addr_t ram_save_remaining(void)
3043 ram_addr_t addr;
3044 ram_addr_t count = 0;
3046 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3047 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3048 count++;
3051 return count;
3054 uint64_t ram_bytes_remaining(void)
3056 return ram_save_remaining() * TARGET_PAGE_SIZE;
3059 uint64_t ram_bytes_transferred(void)
3061 return bytes_transferred;
3064 uint64_t ram_bytes_total(void)
3066 return last_ram_offset;
3069 static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3071 ram_addr_t addr;
3072 uint64_t bytes_transferred_last;
3073 double bwidth = 0;
3074 uint64_t expected_time = 0;
3076 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
3077 qemu_file_set_error(f);
3078 return 0;
3081 if (stage == 1) {
3082 /* Make sure all dirty bits are set */
3083 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3084 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3085 cpu_physical_memory_set_dirty(addr);
3088 /* Enable dirty memory tracking */
3089 cpu_physical_memory_set_dirty_tracking(1);
3091 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
3094 bytes_transferred_last = bytes_transferred;
3095 bwidth = get_clock();
3097 while (!qemu_file_rate_limit(f)) {
3098 int ret;
3100 ret = ram_save_block(f);
3101 bytes_transferred += ret * TARGET_PAGE_SIZE;
3102 if (ret == 0) /* no more blocks */
3103 break;
3106 bwidth = get_clock() - bwidth;
3107 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3109 /* if we haven't transferred anything this round, force expected_time to a
3110 * a very high value, but without crashing */
3111 if (bwidth == 0)
3112 bwidth = 0.000001;
3114 /* try transferring iterative blocks of memory */
3116 if (stage == 3) {
3118 /* flush all remaining blocks regardless of rate limiting */
3119 while (ram_save_block(f) != 0) {
3120 bytes_transferred += TARGET_PAGE_SIZE;
3122 cpu_physical_memory_set_dirty_tracking(0);
3125 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3127 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3129 return (stage == 2) && (expected_time <= migrate_max_downtime());
3132 static int ram_load_dead(QEMUFile *f, void *opaque)
3134 RamDecompressState s1, *s = &s1;
3135 uint8_t buf[10];
3136 ram_addr_t i;
3138 if (ram_decompress_open(s, f) < 0)
3139 return -EINVAL;
3140 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
3141 if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
3142 continue;
3143 if (ram_decompress_buf(s, buf, 1) < 0) {
3144 fprintf(stderr, "Error while reading ram block header\n");
3145 goto error;
3147 if (buf[0] == 0) {
3148 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3149 BDRV_HASH_BLOCK_SIZE) < 0) {
3150 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
3151 goto error;
3153 } else {
3154 error:
3155 printf("Error block header\n");
3156 return -EINVAL;
3159 ram_decompress_close(s);
3161 return 0;
3164 static int ram_load(QEMUFile *f, void *opaque, int version_id)
3166 ram_addr_t addr;
3167 int flags;
3169 if (version_id == 1)
3170 return ram_load_v1(f, opaque);
3172 if (version_id == 2) {
3173 if (qemu_get_be32(f) != last_ram_offset)
3174 return -EINVAL;
3175 return ram_load_dead(f, opaque);
3178 if (version_id != 3)
3179 return -EINVAL;
3181 do {
3182 addr = qemu_get_be64(f);
3184 flags = addr & ~TARGET_PAGE_MASK;
3185 addr &= TARGET_PAGE_MASK;
3187 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3188 if (addr != last_ram_offset)
3189 return -EINVAL;
3192 if (flags & RAM_SAVE_FLAG_FULL) {
3193 if (ram_load_dead(f, opaque) < 0)
3194 return -EINVAL;
3197 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3198 uint8_t ch = qemu_get_byte(f);
3199 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3200 #ifndef _WIN32
3201 if (ch == 0 &&
3202 (!kvm_enabled() || kvm_has_sync_mmu())) {
3203 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
3205 #endif
3206 } else if (flags & RAM_SAVE_FLAG_PAGE)
3207 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
3208 } while (!(flags & RAM_SAVE_FLAG_EOS));
3210 return 0;
3213 void qemu_service_io(void)
3215 qemu_notify_event();
3218 /***********************************************************/
3219 /* bottom halves (can be seen as timers which expire ASAP) */
3221 struct QEMUBH {
3222 QEMUBHFunc *cb;
3223 void *opaque;
3224 int scheduled;
3225 int idle;
3226 int deleted;
3227 QEMUBH *next;
3230 static QEMUBH *first_bh = NULL;
3232 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3234 QEMUBH *bh;
3235 bh = qemu_mallocz(sizeof(QEMUBH));
3236 bh->cb = cb;
3237 bh->opaque = opaque;
3238 bh->next = first_bh;
3239 first_bh = bh;
3240 return bh;
3243 int qemu_bh_poll(void)
3245 QEMUBH *bh, **bhp;
3246 int ret;
3248 ret = 0;
3249 for (bh = first_bh; bh; bh = bh->next) {
3250 if (!bh->deleted && bh->scheduled) {
3251 bh->scheduled = 0;
3252 if (!bh->idle)
3253 ret = 1;
3254 bh->idle = 0;
3255 bh->cb(bh->opaque);
3259 /* remove deleted bhs */
3260 bhp = &first_bh;
3261 while (*bhp) {
3262 bh = *bhp;
3263 if (bh->deleted) {
3264 *bhp = bh->next;
3265 qemu_free(bh);
3266 } else
3267 bhp = &bh->next;
3270 return ret;
3273 void qemu_bh_schedule_idle(QEMUBH *bh)
3275 if (bh->scheduled)
3276 return;
3277 bh->scheduled = 1;
3278 bh->idle = 1;
3281 void qemu_bh_schedule(QEMUBH *bh)
3283 if (bh->scheduled)
3284 return;
3285 bh->scheduled = 1;
3286 bh->idle = 0;
3287 /* stop the currently executing CPU to execute the BH ASAP */
3288 qemu_notify_event();
3291 void qemu_bh_cancel(QEMUBH *bh)
3293 bh->scheduled = 0;
3296 void qemu_bh_delete(QEMUBH *bh)
3298 bh->scheduled = 0;
3299 bh->deleted = 1;
3302 static void qemu_bh_update_timeout(int *timeout)
3304 QEMUBH *bh;
3306 for (bh = first_bh; bh; bh = bh->next) {
3307 if (!bh->deleted && bh->scheduled) {
3308 if (bh->idle) {
3309 /* idle bottom halves will be polled at least
3310 * every 10ms */
3311 *timeout = MIN(10, *timeout);
3312 } else {
3313 /* non-idle bottom halves will be executed
3314 * immediately */
3315 *timeout = 0;
3316 break;
3322 /***********************************************************/
3323 /* machine registration */
3325 static QEMUMachine *first_machine = NULL;
3326 QEMUMachine *current_machine = NULL;
3328 int qemu_register_machine(QEMUMachine *m)
3330 QEMUMachine **pm;
3331 pm = &first_machine;
3332 while (*pm != NULL)
3333 pm = &(*pm)->next;
3334 m->next = NULL;
3335 *pm = m;
3336 return 0;
3339 static QEMUMachine *find_machine(const char *name)
3341 QEMUMachine *m;
3343 for(m = first_machine; m != NULL; m = m->next) {
3344 if (!strcmp(m->name, name))
3345 return m;
3347 return NULL;
3350 static QEMUMachine *find_default_machine(void)
3352 QEMUMachine *m;
3354 for(m = first_machine; m != NULL; m = m->next) {
3355 if (m->is_default) {
3356 return m;
3359 return NULL;
3362 /***********************************************************/
3363 /* main execution loop */
3365 static void gui_update(void *opaque)
3367 uint64_t interval = GUI_REFRESH_INTERVAL;
3368 DisplayState *ds = opaque;
3369 DisplayChangeListener *dcl = ds->listeners;
3371 dpy_refresh(ds);
3373 while (dcl != NULL) {
3374 if (dcl->gui_timer_interval &&
3375 dcl->gui_timer_interval < interval)
3376 interval = dcl->gui_timer_interval;
3377 dcl = dcl->next;
3379 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
3382 static void nographic_update(void *opaque)
3384 uint64_t interval = GUI_REFRESH_INTERVAL;
3386 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3389 struct vm_change_state_entry {
3390 VMChangeStateHandler *cb;
3391 void *opaque;
3392 LIST_ENTRY (vm_change_state_entry) entries;
3395 static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3397 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3398 void *opaque)
3400 VMChangeStateEntry *e;
3402 e = qemu_mallocz(sizeof (*e));
3404 e->cb = cb;
3405 e->opaque = opaque;
3406 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3407 return e;
3410 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3412 LIST_REMOVE (e, entries);
3413 qemu_free (e);
3416 static void vm_state_notify(int running, int reason)
3418 VMChangeStateEntry *e;
3420 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3421 e->cb(e->opaque, running, reason);
3425 static void resume_all_vcpus(void);
3426 static void pause_all_vcpus(void);
3428 void vm_start(void)
3430 if (!vm_running) {
3431 cpu_enable_ticks();
3432 vm_running = 1;
3433 vm_state_notify(1, 0);
3434 qemu_rearm_alarm_timer(alarm_timer);
3435 resume_all_vcpus();
3439 /* reset/shutdown handler */
3441 typedef struct QEMUResetEntry {
3442 QEMUResetHandler *func;
3443 void *opaque;
3444 struct QEMUResetEntry *next;
3445 } QEMUResetEntry;
3447 static QEMUResetEntry *first_reset_entry;
3448 static int reset_requested;
3449 static int shutdown_requested;
3450 static int powerdown_requested;
3451 static int debug_requested;
3452 static int vmstop_requested;
3454 int qemu_no_shutdown(void)
3456 int r = no_shutdown;
3457 no_shutdown = 0;
3458 return r;
3461 int qemu_shutdown_requested(void)
3463 int r = shutdown_requested;
3464 shutdown_requested = 0;
3465 return r;
3468 int qemu_reset_requested(void)
3470 int r = reset_requested;
3471 reset_requested = 0;
3472 return r;
3475 int qemu_powerdown_requested(void)
3477 int r = powerdown_requested;
3478 powerdown_requested = 0;
3479 return r;
3482 static int qemu_debug_requested(void)
3484 int r = debug_requested;
3485 debug_requested = 0;
3486 return r;
3489 static int qemu_vmstop_requested(void)
3491 int r = vmstop_requested;
3492 vmstop_requested = 0;
3493 return r;
3496 static void do_vm_stop(int reason)
3498 if (vm_running) {
3499 cpu_disable_ticks();
3500 vm_running = 0;
3501 pause_all_vcpus();
3502 vm_state_notify(0, reason);
3506 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3508 QEMUResetEntry **pre, *re;
3510 pre = &first_reset_entry;
3511 while (*pre != NULL)
3512 pre = &(*pre)->next;
3513 re = qemu_mallocz(sizeof(QEMUResetEntry));
3514 re->func = func;
3515 re->opaque = opaque;
3516 re->next = NULL;
3517 *pre = re;
3520 void qemu_system_reset(void)
3522 QEMUResetEntry *re;
3524 /* reset all devices */
3525 for(re = first_reset_entry; re != NULL; re = re->next) {
3526 re->func(re->opaque);
3530 void qemu_system_reset_request(void)
3532 if (no_reboot) {
3533 shutdown_requested = 1;
3534 } else {
3535 reset_requested = 1;
3537 if (cpu_single_env) {
3538 cpu_single_env->stopped = 1;
3540 qemu_notify_event();
3543 void qemu_system_shutdown_request(void)
3545 shutdown_requested = 1;
3546 qemu_notify_event();
3549 void qemu_system_powerdown_request(void)
3551 powerdown_requested = 1;
3552 qemu_notify_event();
3555 #ifdef CONFIG_IOTHREAD
3556 static void qemu_system_vmstop_request(int reason)
3558 vmstop_requested = reason;
3559 qemu_notify_event();
3561 #endif
3563 #ifndef _WIN32
3564 static int io_thread_fd = -1;
3566 static void qemu_event_increment(void)
3568 static const char byte = 0;
3570 if (io_thread_fd == -1)
3571 return;
3573 write(io_thread_fd, &byte, sizeof(byte));
3576 static void qemu_event_read(void *opaque)
3578 int fd = (unsigned long)opaque;
3579 ssize_t len;
3581 /* Drain the notify pipe */
3582 do {
3583 char buffer[512];
3584 len = read(fd, buffer, sizeof(buffer));
3585 } while ((len == -1 && errno == EINTR) || len > 0);
3588 static int qemu_event_init(void)
3590 int err;
3591 int fds[2];
3593 err = pipe(fds);
3594 if (err == -1)
3595 return -errno;
3597 err = fcntl_setfl(fds[0], O_NONBLOCK);
3598 if (err < 0)
3599 goto fail;
3601 err = fcntl_setfl(fds[1], O_NONBLOCK);
3602 if (err < 0)
3603 goto fail;
3605 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3606 (void *)(unsigned long)fds[0]);
3608 io_thread_fd = fds[1];
3609 return 0;
3611 fail:
3612 close(fds[0]);
3613 close(fds[1]);
3614 return err;
3616 #else
3617 HANDLE qemu_event_handle;
3619 static void dummy_event_handler(void *opaque)
3623 static int qemu_event_init(void)
3625 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3626 if (!qemu_event_handle) {
3627 perror("Failed CreateEvent");
3628 return -1;
3630 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3631 return 0;
3634 static void qemu_event_increment(void)
3636 SetEvent(qemu_event_handle);
3638 #endif
3640 static int cpu_can_run(CPUState *env)
3642 if (env->stop)
3643 return 0;
3644 if (env->stopped)
3645 return 0;
3646 return 1;
3649 #ifndef CONFIG_IOTHREAD
3650 static int qemu_init_main_loop(void)
3652 return qemu_event_init();
3655 void qemu_init_vcpu(void *_env)
3657 CPUState *env = _env;
3659 if (kvm_enabled())
3660 kvm_init_vcpu(env);
3661 return;
3664 int qemu_cpu_self(void *env)
3666 return 1;
3669 static void resume_all_vcpus(void)
3673 static void pause_all_vcpus(void)
3677 void qemu_cpu_kick(void *env)
3679 return;
3682 void qemu_notify_event(void)
3684 CPUState *env = cpu_single_env;
3686 if (kvm_enabled()) {
3687 qemu_kvm_notify_work();
3688 return;
3690 if (env) {
3691 cpu_exit(env);
3692 #ifdef USE_KQEMU
3693 if (env->kqemu_enabled)
3694 kqemu_cpu_interrupt(env);
3695 #endif
3699 #ifdef KVM_UPSTREAM
3700 #define qemu_mutex_lock_iothread() do { } while (0)
3701 #define qemu_mutex_unlock_iothread() do { } while (0)
3702 #endif
3704 void vm_stop(int reason)
3706 do_vm_stop(reason);
3709 #else /* CONFIG_IOTHREAD */
3711 #include "qemu-thread.h"
3713 QemuMutex qemu_global_mutex;
3714 static QemuMutex qemu_fair_mutex;
3716 static QemuThread io_thread;
3718 static QemuThread *tcg_cpu_thread;
3719 static QemuCond *tcg_halt_cond;
3721 static int qemu_system_ready;
3722 /* cpu creation */
3723 static QemuCond qemu_cpu_cond;
3724 /* system init */
3725 static QemuCond qemu_system_cond;
3726 static QemuCond qemu_pause_cond;
3728 static void block_io_signals(void);
3729 static void unblock_io_signals(void);
3730 static int tcg_has_work(void);
3732 static int qemu_init_main_loop(void)
3734 int ret;
3736 ret = qemu_event_init();
3737 if (ret)
3738 return ret;
3740 qemu_cond_init(&qemu_pause_cond);
3741 qemu_mutex_init(&qemu_fair_mutex);
3742 qemu_mutex_init(&qemu_global_mutex);
3743 qemu_mutex_lock(&qemu_global_mutex);
3745 unblock_io_signals();
3746 qemu_thread_self(&io_thread);
3748 return 0;
3751 static void qemu_wait_io_event(CPUState *env)
3753 while (!tcg_has_work())
3754 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3756 qemu_mutex_unlock(&qemu_global_mutex);
3759 * Users of qemu_global_mutex can be starved, having no chance
3760 * to acquire it since this path will get to it first.
3761 * So use another lock to provide fairness.
3763 qemu_mutex_lock(&qemu_fair_mutex);
3764 qemu_mutex_unlock(&qemu_fair_mutex);
3766 qemu_mutex_lock(&qemu_global_mutex);
3767 if (env->stop) {
3768 env->stop = 0;
3769 env->stopped = 1;
3770 qemu_cond_signal(&qemu_pause_cond);
3774 static int qemu_cpu_exec(CPUState *env);
3776 static void *kvm_cpu_thread_fn(void *arg)
3778 CPUState *env = arg;
3780 block_io_signals();
3781 qemu_thread_self(env->thread);
3783 /* signal CPU creation */
3784 qemu_mutex_lock(&qemu_global_mutex);
3785 env->created = 1;
3786 qemu_cond_signal(&qemu_cpu_cond);
3788 /* and wait for machine initialization */
3789 while (!qemu_system_ready)
3790 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3792 while (1) {
3793 if (cpu_can_run(env))
3794 qemu_cpu_exec(env);
3795 qemu_wait_io_event(env);
3798 return NULL;
3801 static void tcg_cpu_exec(void);
3803 static void *tcg_cpu_thread_fn(void *arg)
3805 CPUState *env = arg;
3807 block_io_signals();
3808 qemu_thread_self(env->thread);
3810 /* signal CPU creation */
3811 qemu_mutex_lock(&qemu_global_mutex);
3812 for (env = first_cpu; env != NULL; env = env->next_cpu)
3813 env->created = 1;
3814 qemu_cond_signal(&qemu_cpu_cond);
3816 /* and wait for machine initialization */
3817 while (!qemu_system_ready)
3818 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3820 while (1) {
3821 tcg_cpu_exec();
3822 qemu_wait_io_event(cur_cpu);
3825 return NULL;
3828 void qemu_cpu_kick(void *_env)
3830 CPUState *env = _env;
3831 qemu_cond_broadcast(env->halt_cond);
3832 if (kvm_enabled())
3833 qemu_thread_signal(env->thread, SIGUSR1);
3836 int qemu_cpu_self(void *env)
3838 return (cpu_single_env != NULL);
3841 static void cpu_signal(int sig)
3843 if (cpu_single_env)
3844 cpu_exit(cpu_single_env);
3847 static void block_io_signals(void)
3849 sigset_t set;
3850 struct sigaction sigact;
3852 sigemptyset(&set);
3853 sigaddset(&set, SIGUSR2);
3854 sigaddset(&set, SIGIO);
3855 sigaddset(&set, SIGALRM);
3856 pthread_sigmask(SIG_BLOCK, &set, NULL);
3858 sigemptyset(&set);
3859 sigaddset(&set, SIGUSR1);
3860 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3862 memset(&sigact, 0, sizeof(sigact));
3863 sigact.sa_handler = cpu_signal;
3864 sigaction(SIGUSR1, &sigact, NULL);
3867 static void unblock_io_signals(void)
3869 sigset_t set;
3871 sigemptyset(&set);
3872 sigaddset(&set, SIGUSR2);
3873 sigaddset(&set, SIGIO);
3874 sigaddset(&set, SIGALRM);
3875 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3877 sigemptyset(&set);
3878 sigaddset(&set, SIGUSR1);
3879 pthread_sigmask(SIG_BLOCK, &set, NULL);
3882 static void qemu_signal_lock(unsigned int msecs)
3884 qemu_mutex_lock(&qemu_fair_mutex);
3886 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3887 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3888 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3889 break;
3891 qemu_mutex_unlock(&qemu_fair_mutex);
3894 static void qemu_mutex_lock_iothread(void)
3896 if (kvm_enabled()) {
3897 qemu_mutex_lock(&qemu_fair_mutex);
3898 qemu_mutex_lock(&qemu_global_mutex);
3899 qemu_mutex_unlock(&qemu_fair_mutex);
3900 } else
3901 qemu_signal_lock(100);
3904 static void qemu_mutex_unlock_iothread(void)
3906 qemu_mutex_unlock(&qemu_global_mutex);
3909 static int all_vcpus_paused(void)
3911 CPUState *penv = first_cpu;
3913 while (penv) {
3914 if (!penv->stopped)
3915 return 0;
3916 penv = (CPUState *)penv->next_cpu;
3919 return 1;
3922 static void pause_all_vcpus(void)
3924 CPUState *penv = first_cpu;
3926 while (penv) {
3927 penv->stop = 1;
3928 qemu_thread_signal(penv->thread, SIGUSR1);
3929 qemu_cpu_kick(penv);
3930 penv = (CPUState *)penv->next_cpu;
3933 while (!all_vcpus_paused()) {
3934 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3935 penv = first_cpu;
3936 while (penv) {
3937 qemu_thread_signal(penv->thread, SIGUSR1);
3938 penv = (CPUState *)penv->next_cpu;
3943 static void resume_all_vcpus(void)
3945 CPUState *penv = first_cpu;
3947 while (penv) {
3948 penv->stop = 0;
3949 penv->stopped = 0;
3950 qemu_thread_signal(penv->thread, SIGUSR1);
3951 qemu_cpu_kick(penv);
3952 penv = (CPUState *)penv->next_cpu;
3956 static void tcg_init_vcpu(void *_env)
3958 CPUState *env = _env;
3959 /* share a single thread for all cpus with TCG */
3960 if (!tcg_cpu_thread) {
3961 env->thread = qemu_mallocz(sizeof(QemuThread));
3962 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3963 qemu_cond_init(env->halt_cond);
3964 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3965 while (env->created == 0)
3966 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3967 tcg_cpu_thread = env->thread;
3968 tcg_halt_cond = env->halt_cond;
3969 } else {
3970 env->thread = tcg_cpu_thread;
3971 env->halt_cond = tcg_halt_cond;
3975 static void kvm_start_vcpu(CPUState *env)
3977 kvm_init_vcpu(env);
3978 env->thread = qemu_mallocz(sizeof(QemuThread));
3979 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3980 qemu_cond_init(env->halt_cond);
3981 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3982 while (env->created == 0)
3983 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3986 void qemu_init_vcpu(void *_env)
3988 CPUState *env = _env;
3990 if (kvm_enabled())
3991 kvm_start_vcpu(env);
3992 else
3993 tcg_init_vcpu(env);
3996 void qemu_notify_event(void)
3998 qemu_event_increment();
4001 void vm_stop(int reason)
4003 QemuThread me;
4004 qemu_thread_self(&me);
4006 if (!qemu_thread_equal(&me, &io_thread)) {
4007 qemu_system_vmstop_request(reason);
4009 * FIXME: should not return to device code in case
4010 * vm_stop() has been requested.
4012 if (cpu_single_env) {
4013 cpu_exit(cpu_single_env);
4014 cpu_single_env->stop = 1;
4016 return;
4018 do_vm_stop(reason);
4021 #endif
4024 #ifdef _WIN32
4025 static void host_main_loop_wait(int *timeout)
4027 int ret, ret2, i;
4028 PollingEntry *pe;
4031 /* XXX: need to suppress polling by better using win32 events */
4032 ret = 0;
4033 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4034 ret |= pe->func(pe->opaque);
4036 if (ret == 0) {
4037 int err;
4038 WaitObjects *w = &wait_objects;
4040 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
4041 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4042 if (w->func[ret - WAIT_OBJECT_0])
4043 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
4045 /* Check for additional signaled events */
4046 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
4048 /* Check if event is signaled */
4049 ret2 = WaitForSingleObject(w->events[i], 0);
4050 if(ret2 == WAIT_OBJECT_0) {
4051 if (w->func[i])
4052 w->func[i](w->opaque[i]);
4053 } else if (ret2 == WAIT_TIMEOUT) {
4054 } else {
4055 err = GetLastError();
4056 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
4059 } else if (ret == WAIT_TIMEOUT) {
4060 } else {
4061 err = GetLastError();
4062 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
4066 *timeout = 0;
4068 #else
4069 static void host_main_loop_wait(int *timeout)
4072 #endif
4074 void main_loop_wait(int timeout)
4076 IOHandlerRecord *ioh;
4077 fd_set rfds, wfds, xfds;
4078 int ret, nfds;
4079 struct timeval tv;
4081 qemu_bh_update_timeout(&timeout);
4083 host_main_loop_wait(&timeout);
4085 /* poll any events */
4086 /* XXX: separate device handlers from system ones */
4087 nfds = -1;
4088 FD_ZERO(&rfds);
4089 FD_ZERO(&wfds);
4090 FD_ZERO(&xfds);
4091 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4092 if (ioh->deleted)
4093 continue;
4094 if (ioh->fd_read &&
4095 (!ioh->fd_read_poll ||
4096 ioh->fd_read_poll(ioh->opaque) != 0)) {
4097 FD_SET(ioh->fd, &rfds);
4098 if (ioh->fd > nfds)
4099 nfds = ioh->fd;
4101 if (ioh->fd_write) {
4102 FD_SET(ioh->fd, &wfds);
4103 if (ioh->fd > nfds)
4104 nfds = ioh->fd;
4108 tv.tv_sec = timeout / 1000;
4109 tv.tv_usec = (timeout % 1000) * 1000;
4111 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4113 qemu_mutex_unlock_iothread();
4114 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4115 qemu_mutex_lock_iothread();
4116 if (ret > 0) {
4117 IOHandlerRecord **pioh;
4119 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4120 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
4121 ioh->fd_read(ioh->opaque);
4122 if (!(ioh->fd_read_poll && ioh->fd_read_poll(ioh->opaque)))
4123 FD_CLR(ioh->fd, &rfds);
4125 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
4126 ioh->fd_write(ioh->opaque);
4130 /* remove deleted IO handlers */
4131 pioh = &first_io_handler;
4132 while (*pioh) {
4133 ioh = *pioh;
4134 if (ioh->deleted) {
4135 *pioh = ioh->next;
4136 qemu_free(ioh);
4137 } else
4138 pioh = &ioh->next;
4142 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
4144 /* rearm timer, if not periodic */
4145 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4146 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4147 qemu_rearm_alarm_timer(alarm_timer);
4150 /* vm time timers */
4151 if (vm_running) {
4152 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4153 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4154 qemu_get_clock(vm_clock));
4157 /* real time timers */
4158 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4159 qemu_get_clock(rt_clock));
4161 /* Check bottom-halves last in case any of the earlier events triggered
4162 them. */
4163 qemu_bh_poll();
4167 static int qemu_cpu_exec(CPUState *env)
4169 int ret;
4170 #ifdef CONFIG_PROFILER
4171 int64_t ti;
4172 #endif
4174 #ifdef CONFIG_PROFILER
4175 ti = profile_getclock();
4176 #endif
4177 if (use_icount) {
4178 int64_t count;
4179 int decr;
4180 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4181 env->icount_decr.u16.low = 0;
4182 env->icount_extra = 0;
4183 count = qemu_next_deadline();
4184 count = (count + (1 << icount_time_shift) - 1)
4185 >> icount_time_shift;
4186 qemu_icount += count;
4187 decr = (count > 0xffff) ? 0xffff : count;
4188 count -= decr;
4189 env->icount_decr.u16.low = decr;
4190 env->icount_extra = count;
4192 ret = cpu_exec(env);
4193 #ifdef CONFIG_PROFILER
4194 qemu_time += profile_getclock() - ti;
4195 #endif
4196 if (use_icount) {
4197 /* Fold pending instructions back into the
4198 instruction counter, and clear the interrupt flag. */
4199 qemu_icount -= (env->icount_decr.u16.low
4200 + env->icount_extra);
4201 env->icount_decr.u32 = 0;
4202 env->icount_extra = 0;
4204 return ret;
4207 static void tcg_cpu_exec(void)
4209 int ret = 0;
4211 if (next_cpu == NULL)
4212 next_cpu = first_cpu;
4213 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4214 CPUState *env = cur_cpu = next_cpu;
4216 if (!vm_running)
4217 break;
4218 if (timer_alarm_pending) {
4219 timer_alarm_pending = 0;
4220 break;
4222 if (cpu_can_run(env))
4223 ret = qemu_cpu_exec(env);
4224 if (ret == EXCP_DEBUG) {
4225 gdb_set_stop_cpu(env);
4226 debug_requested = 1;
4227 break;
4232 static int cpu_has_work(CPUState *env)
4234 if (env->stop)
4235 return 1;
4236 if (env->stopped)
4237 return 0;
4238 if (!env->halted)
4239 return 1;
4240 if (qemu_cpu_has_work(env))
4241 return 1;
4242 return 0;
4245 static int tcg_has_work(void)
4247 CPUState *env;
4249 for (env = first_cpu; env != NULL; env = env->next_cpu)
4250 if (cpu_has_work(env))
4251 return 1;
4252 return 0;
4255 static int qemu_calculate_timeout(void)
4257 #ifndef CONFIG_IOTHREAD
4258 int timeout;
4260 if (!vm_running)
4261 timeout = 5000;
4262 else if (tcg_has_work())
4263 timeout = 0;
4264 else if (!use_icount)
4265 timeout = 5000;
4266 else {
4267 /* XXX: use timeout computed from timers */
4268 int64_t add;
4269 int64_t delta;
4270 /* Advance virtual time to the next event. */
4271 if (use_icount == 1) {
4272 /* When not using an adaptive execution frequency
4273 we tend to get badly out of sync with real time,
4274 so just delay for a reasonable amount of time. */
4275 delta = 0;
4276 } else {
4277 delta = cpu_get_icount() - cpu_get_clock();
4279 if (delta > 0) {
4280 /* If virtual time is ahead of real time then just
4281 wait for IO. */
4282 timeout = (delta / 1000000) + 1;
4283 } else {
4284 /* Wait for either IO to occur or the next
4285 timer event. */
4286 add = qemu_next_deadline();
4287 /* We advance the timer before checking for IO.
4288 Limit the amount we advance so that early IO
4289 activity won't get the guest too far ahead. */
4290 if (add > 10000000)
4291 add = 10000000;
4292 delta += add;
4293 add = (add + (1 << icount_time_shift) - 1)
4294 >> icount_time_shift;
4295 qemu_icount += add;
4296 timeout = delta / 1000000;
4297 if (timeout < 0)
4298 timeout = 0;
4302 return timeout;
4303 #else /* CONFIG_IOTHREAD */
4304 return 1000;
4305 #endif
4308 static int vm_can_run(void)
4310 if (powerdown_requested)
4311 return 0;
4312 if (reset_requested)
4313 return 0;
4314 if (shutdown_requested)
4315 return 0;
4316 if (debug_requested)
4317 return 0;
4318 return 1;
4321 static void main_loop(void)
4323 int r;
4325 if (kvm_enabled()) {
4326 kvm_main_loop();
4327 cpu_disable_ticks();
4328 return;
4331 #ifdef CONFIG_IOTHREAD
4332 qemu_system_ready = 1;
4333 qemu_cond_broadcast(&qemu_system_cond);
4334 #endif
4336 for (;;) {
4337 do {
4338 #ifdef CONFIG_PROFILER
4339 int64_t ti;
4340 #endif
4341 #ifndef CONFIG_IOTHREAD
4342 tcg_cpu_exec();
4343 #endif
4344 #ifdef CONFIG_PROFILER
4345 ti = profile_getclock();
4346 #endif
4347 main_loop_wait(qemu_calculate_timeout());
4348 #ifdef CONFIG_PROFILER
4349 dev_time += profile_getclock() - ti;
4350 #endif
4351 } while (vm_can_run());
4353 if (qemu_debug_requested())
4354 vm_stop(EXCP_DEBUG);
4355 if (qemu_shutdown_requested()) {
4356 if (no_shutdown) {
4357 vm_stop(0);
4358 no_shutdown = 0;
4359 } else
4360 break;
4362 if (qemu_reset_requested()) {
4363 pause_all_vcpus();
4364 qemu_system_reset();
4365 resume_all_vcpus();
4367 if (qemu_powerdown_requested())
4368 qemu_system_powerdown();
4369 if ((r = qemu_vmstop_requested()))
4370 vm_stop(r);
4372 pause_all_vcpus();
4375 static void version(void)
4377 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
4380 static void help(int exitcode)
4382 version();
4383 printf("usage: %s [options] [disk_image]\n"
4384 "\n"
4385 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4386 "\n"
4387 #define DEF(option, opt_arg, opt_enum, opt_help) \
4388 opt_help
4389 #define DEFHEADING(text) stringify(text) "\n"
4390 #include "qemu-options.h"
4391 #undef DEF
4392 #undef DEFHEADING
4393 #undef GEN_DOCS
4394 "\n"
4395 "During emulation, the following keys are useful:\n"
4396 "ctrl-alt-f toggle full screen\n"
4397 "ctrl-alt-n switch to virtual console 'n'\n"
4398 "ctrl-alt toggle mouse and keyboard grab\n"
4399 "\n"
4400 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4402 "qemu",
4403 DEFAULT_RAM_SIZE,
4404 #ifndef _WIN32
4405 DEFAULT_NETWORK_SCRIPT,
4406 DEFAULT_NETWORK_DOWN_SCRIPT,
4407 #endif
4408 DEFAULT_GDBSTUB_PORT,
4409 "/tmp/qemu.log");
4410 exit(exitcode);
4413 #define HAS_ARG 0x0001
4415 enum {
4416 #define DEF(option, opt_arg, opt_enum, opt_help) \
4417 opt_enum,
4418 #define DEFHEADING(text)
4419 #include "qemu-options.h"
4420 #undef DEF
4421 #undef DEFHEADING
4422 #undef GEN_DOCS
4425 typedef struct QEMUOption {
4426 const char *name;
4427 int flags;
4428 int index;
4429 } QEMUOption;
4431 static const QEMUOption qemu_options[] = {
4432 { "h", 0, QEMU_OPTION_h },
4433 #define DEF(option, opt_arg, opt_enum, opt_help) \
4434 { option, opt_arg, opt_enum },
4435 #define DEFHEADING(text)
4436 #include "qemu-options.h"
4437 #undef DEF
4438 #undef DEFHEADING
4439 #undef GEN_DOCS
4440 { NULL },
4443 #ifdef HAS_AUDIO
4444 struct soundhw soundhw[] = {
4445 #ifdef HAS_AUDIO_CHOICE
4446 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4448 "pcspk",
4449 "PC speaker",
4452 { .init_isa = pcspk_audio_init }
4454 #endif
4456 #ifdef CONFIG_SB16
4458 "sb16",
4459 "Creative Sound Blaster 16",
4462 { .init_isa = SB16_init }
4464 #endif
4466 #ifdef CONFIG_CS4231A
4468 "cs4231a",
4469 "CS4231A",
4472 { .init_isa = cs4231a_init }
4474 #endif
4476 #ifdef CONFIG_ADLIB
4478 "adlib",
4479 #ifdef HAS_YMF262
4480 "Yamaha YMF262 (OPL3)",
4481 #else
4482 "Yamaha YM3812 (OPL2)",
4483 #endif
4486 { .init_isa = Adlib_init }
4488 #endif
4490 #ifdef CONFIG_GUS
4492 "gus",
4493 "Gravis Ultrasound GF1",
4496 { .init_isa = GUS_init }
4498 #endif
4500 #ifdef CONFIG_AC97
4502 "ac97",
4503 "Intel 82801AA AC97 Audio",
4506 { .init_pci = ac97_init }
4508 #endif
4510 #ifdef CONFIG_ES1370
4512 "es1370",
4513 "ENSONIQ AudioPCI ES1370",
4516 { .init_pci = es1370_init }
4518 #endif
4520 #endif /* HAS_AUDIO_CHOICE */
4522 { NULL, NULL, 0, 0, { NULL } }
4525 static void select_soundhw (const char *optarg)
4527 struct soundhw *c;
4529 if (*optarg == '?') {
4530 show_valid_cards:
4532 printf ("Valid sound card names (comma separated):\n");
4533 for (c = soundhw; c->name; ++c) {
4534 printf ("%-11s %s\n", c->name, c->descr);
4536 printf ("\n-soundhw all will enable all of the above\n");
4537 exit (*optarg != '?');
4539 else {
4540 size_t l;
4541 const char *p;
4542 char *e;
4543 int bad_card = 0;
4545 if (!strcmp (optarg, "all")) {
4546 for (c = soundhw; c->name; ++c) {
4547 c->enabled = 1;
4549 return;
4552 p = optarg;
4553 while (*p) {
4554 e = strchr (p, ',');
4555 l = !e ? strlen (p) : (size_t) (e - p);
4557 for (c = soundhw; c->name; ++c) {
4558 if (!strncmp (c->name, p, l)) {
4559 c->enabled = 1;
4560 break;
4564 if (!c->name) {
4565 if (l > 80) {
4566 fprintf (stderr,
4567 "Unknown sound card name (too big to show)\n");
4569 else {
4570 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4571 (int) l, p);
4573 bad_card = 1;
4575 p += l + (e != NULL);
4578 if (bad_card)
4579 goto show_valid_cards;
4582 #endif
4584 static void select_vgahw (const char *p)
4586 const char *opts;
4588 cirrus_vga_enabled = 0;
4589 std_vga_enabled = 0;
4590 vmsvga_enabled = 0;
4591 xenfb_enabled = 0;
4592 if (strstart(p, "std", &opts)) {
4593 std_vga_enabled = 1;
4594 } else if (strstart(p, "cirrus", &opts)) {
4595 cirrus_vga_enabled = 1;
4596 } else if (strstart(p, "vmware", &opts)) {
4597 vmsvga_enabled = 1;
4598 } else if (strstart(p, "xenfb", &opts)) {
4599 xenfb_enabled = 1;
4600 } else if (!strstart(p, "none", &opts)) {
4601 invalid_vga:
4602 fprintf(stderr, "Unknown vga type: %s\n", p);
4603 exit(1);
4605 while (*opts) {
4606 const char *nextopt;
4608 if (strstart(opts, ",retrace=", &nextopt)) {
4609 opts = nextopt;
4610 if (strstart(opts, "dumb", &nextopt))
4611 vga_retrace_method = VGA_RETRACE_DUMB;
4612 else if (strstart(opts, "precise", &nextopt))
4613 vga_retrace_method = VGA_RETRACE_PRECISE;
4614 else goto invalid_vga;
4615 } else goto invalid_vga;
4616 opts = nextopt;
4620 #ifdef TARGET_I386
4621 static int balloon_parse(const char *arg)
4623 char buf[128];
4624 const char *p;
4626 if (!strcmp(arg, "none")) {
4627 virtio_balloon = 0;
4628 } else if (!strncmp(arg, "virtio", 6)) {
4629 virtio_balloon = 1;
4630 if (arg[6] == ',') {
4631 p = arg + 7;
4632 if (get_param_value(buf, sizeof(buf), "addr", p)) {
4633 virtio_balloon_devaddr = strdup(buf);
4636 } else {
4637 return -1;
4639 return 0;
4641 #endif
4643 #ifdef _WIN32
4644 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4646 exit(STATUS_CONTROL_C_EXIT);
4647 return TRUE;
4649 #endif
4651 int qemu_uuid_parse(const char *str, uint8_t *uuid)
4653 int ret;
4655 if(strlen(str) != 36)
4656 return -1;
4658 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4659 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4660 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4662 if(ret != 16)
4663 return -1;
4665 #ifdef TARGET_I386
4666 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4667 #endif
4669 return 0;
4672 #define MAX_NET_CLIENTS 32
4674 #ifndef _WIN32
4676 static void termsig_handler(int signal)
4678 qemu_system_shutdown_request();
4681 static void sigchld_handler(int signal)
4683 waitpid(-1, NULL, WNOHANG);
4686 static void sighandler_setup(void)
4688 struct sigaction act;
4690 memset(&act, 0, sizeof(act));
4691 act.sa_handler = termsig_handler;
4692 sigaction(SIGINT, &act, NULL);
4693 sigaction(SIGHUP, &act, NULL);
4694 sigaction(SIGTERM, &act, NULL);
4696 act.sa_handler = sigchld_handler;
4697 act.sa_flags = SA_NOCLDSTOP;
4698 sigaction(SIGCHLD, &act, NULL);
4701 #endif
4703 #ifdef _WIN32
4704 /* Look for support files in the same directory as the executable. */
4705 static char *find_datadir(const char *argv0)
4707 char *p;
4708 char buf[MAX_PATH];
4709 DWORD len;
4711 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4712 if (len == 0) {
4713 return NULL;
4716 buf[len] = 0;
4717 p = buf + len - 1;
4718 while (p != buf && *p != '\\')
4719 p--;
4720 *p = 0;
4721 if (access(buf, R_OK) == 0) {
4722 return qemu_strdup(buf);
4724 return NULL;
4726 #else /* !_WIN32 */
4728 /* Find a likely location for support files using the location of the binary.
4729 For installed binaries this will be "$bindir/../share/qemu". When
4730 running from the build tree this will be "$bindir/../pc-bios". */
4731 #define SHARE_SUFFIX "/share/qemu"
4732 #define BUILD_SUFFIX "/pc-bios"
4733 static char *find_datadir(const char *argv0)
4735 char *dir;
4736 char *p = NULL;
4737 char *res;
4738 #ifdef PATH_MAX
4739 char buf[PATH_MAX];
4740 #endif
4741 size_t max_len;
4743 #if defined(__linux__)
4745 int len;
4746 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4747 if (len > 0) {
4748 buf[len] = 0;
4749 p = buf;
4752 #elif defined(__FreeBSD__)
4754 int len;
4755 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4756 if (len > 0) {
4757 buf[len] = 0;
4758 p = buf;
4761 #endif
4762 /* If we don't have any way of figuring out the actual executable
4763 location then try argv[0]. */
4764 if (!p) {
4765 #ifdef PATH_MAX
4766 p = buf;
4767 #endif
4768 p = realpath(argv0, p);
4769 if (!p) {
4770 return NULL;
4773 dir = dirname(p);
4774 dir = dirname(dir);
4776 max_len = strlen(dir) +
4777 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4778 res = qemu_mallocz(max_len);
4779 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
4780 if (access(res, R_OK)) {
4781 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
4782 if (access(res, R_OK)) {
4783 qemu_free(res);
4784 res = NULL;
4787 #ifndef PATH_MAX
4788 free(p);
4789 #endif
4790 return res;
4792 #undef SHARE_SUFFIX
4793 #undef BUILD_SUFFIX
4794 #endif
4796 char *qemu_find_file(int type, const char *name)
4798 int len;
4799 const char *subdir;
4800 char *buf;
4802 /* If name contains path separators then try it as a straight path. */
4803 if ((strchr(name, '/') || strchr(name, '\\'))
4804 && access(name, R_OK) == 0) {
4805 return strdup(name);
4807 switch (type) {
4808 case QEMU_FILE_TYPE_BIOS:
4809 subdir = "";
4810 break;
4811 case QEMU_FILE_TYPE_KEYMAP:
4812 subdir = "keymaps/";
4813 break;
4814 default:
4815 abort();
4817 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4818 buf = qemu_mallocz(len);
4819 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
4820 if (access(buf, R_OK)) {
4821 qemu_free(buf);
4822 return NULL;
4824 return buf;
4827 int main(int argc, char **argv, char **envp)
4829 const char *gdbstub_dev = NULL;
4830 uint32_t boot_devices_bitmap = 0;
4831 int i;
4832 int snapshot, linux_boot, net_boot;
4833 const char *initrd_filename;
4834 const char *kernel_filename, *kernel_cmdline;
4835 const char *boot_devices = "";
4836 DisplayState *ds;
4837 DisplayChangeListener *dcl;
4838 int cyls, heads, secs, translation;
4839 const char *net_clients[MAX_NET_CLIENTS];
4840 int nb_net_clients;
4841 const char *bt_opts[MAX_BT_CMDLINE];
4842 int nb_bt_opts;
4843 int hda_index;
4844 int optind;
4845 const char *r, *optarg;
4846 CharDriverState *monitor_hd = NULL;
4847 const char *monitor_device;
4848 const char *serial_devices[MAX_SERIAL_PORTS];
4849 int serial_device_index;
4850 const char *parallel_devices[MAX_PARALLEL_PORTS];
4851 int parallel_device_index;
4852 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4853 int virtio_console_index;
4854 const char *loadvm = NULL;
4855 QEMUMachine *machine;
4856 const char *cpu_model;
4857 const char *usb_devices[MAX_USB_CMDLINE];
4858 int usb_devices_index;
4859 #ifndef _WIN32
4860 int fds[2];
4861 #endif
4862 int tb_size;
4863 const char *pid_file = NULL;
4864 const char *incoming = NULL;
4865 #ifndef _WIN32
4866 int fd = 0;
4867 struct passwd *pwd = NULL;
4868 const char *chroot_dir = NULL;
4869 const char *run_as = NULL;
4870 #endif
4871 CPUState *env;
4872 int show_vnc_port = 0;
4874 qemu_cache_utils_init(envp);
4876 LIST_INIT (&vm_change_state_head);
4877 #ifndef _WIN32
4879 struct sigaction act;
4880 sigfillset(&act.sa_mask);
4881 act.sa_flags = 0;
4882 act.sa_handler = SIG_IGN;
4883 sigaction(SIGPIPE, &act, NULL);
4885 #else
4886 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
4887 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4888 QEMU to run on a single CPU */
4890 HANDLE h;
4891 DWORD mask, smask;
4892 int i;
4893 h = GetCurrentProcess();
4894 if (GetProcessAffinityMask(h, &mask, &smask)) {
4895 for(i = 0; i < 32; i++) {
4896 if (mask & (1 << i))
4897 break;
4899 if (i != 32) {
4900 mask = 1 << i;
4901 SetProcessAffinityMask(h, mask);
4905 #endif
4907 module_call_init(MODULE_INIT_MACHINE);
4908 machine = find_default_machine();
4909 cpu_model = NULL;
4910 initrd_filename = NULL;
4911 ram_size = 0;
4912 snapshot = 0;
4913 kernel_filename = NULL;
4914 kernel_cmdline = "";
4915 cyls = heads = secs = 0;
4916 translation = BIOS_ATA_TRANSLATION_AUTO;
4917 monitor_device = "vc:80Cx24C";
4919 serial_devices[0] = "vc:80Cx24C";
4920 for(i = 1; i < MAX_SERIAL_PORTS; i++)
4921 serial_devices[i] = NULL;
4922 serial_device_index = 0;
4924 parallel_devices[0] = "vc:80Cx24C";
4925 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
4926 parallel_devices[i] = NULL;
4927 parallel_device_index = 0;
4929 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
4930 virtio_consoles[i] = NULL;
4931 virtio_console_index = 0;
4933 for (i = 0; i < MAX_NODES; i++) {
4934 node_mem[i] = 0;
4935 node_cpumask[i] = 0;
4938 usb_devices_index = 0;
4939 assigned_devices_index = 0;
4941 nb_net_clients = 0;
4942 nb_bt_opts = 0;
4943 nb_drives = 0;
4944 nb_drives_opt = 0;
4945 nb_numa_nodes = 0;
4946 hda_index = -1;
4948 nb_nics = 0;
4950 tb_size = 0;
4951 autostart= 1;
4953 register_watchdogs();
4955 optind = 1;
4956 for(;;) {
4957 if (optind >= argc)
4958 break;
4959 r = argv[optind];
4960 if (r[0] != '-') {
4961 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
4962 } else {
4963 const QEMUOption *popt;
4965 optind++;
4966 /* Treat --foo the same as -foo. */
4967 if (r[1] == '-')
4968 r++;
4969 popt = qemu_options;
4970 for(;;) {
4971 if (!popt->name) {
4972 fprintf(stderr, "%s: invalid option -- '%s'\n",
4973 argv[0], r);
4974 exit(1);
4976 if (!strcmp(popt->name, r + 1))
4977 break;
4978 popt++;
4980 if (popt->flags & HAS_ARG) {
4981 if (optind >= argc) {
4982 fprintf(stderr, "%s: option '%s' requires an argument\n",
4983 argv[0], r);
4984 exit(1);
4986 optarg = argv[optind++];
4987 } else {
4988 optarg = NULL;
4991 switch(popt->index) {
4992 case QEMU_OPTION_M:
4993 machine = find_machine(optarg);
4994 if (!machine) {
4995 QEMUMachine *m;
4996 printf("Supported machines are:\n");
4997 for(m = first_machine; m != NULL; m = m->next) {
4998 printf("%-10s %s%s\n",
4999 m->name, m->desc,
5000 m->is_default ? " (default)" : "");
5002 exit(*optarg != '?');
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)
5010 cpu_list(stdout, &fprintf);
5011 #endif
5012 exit(0);
5013 } else {
5014 cpu_model = optarg;
5016 break;
5017 case QEMU_OPTION_initrd:
5018 initrd_filename = optarg;
5019 break;
5020 case QEMU_OPTION_hda:
5021 if (cyls == 0)
5022 hda_index = drive_add(optarg, HD_ALIAS, 0);
5023 else
5024 hda_index = drive_add(optarg, HD_ALIAS
5025 ",cyls=%d,heads=%d,secs=%d%s",
5026 0, cyls, heads, secs,
5027 translation == BIOS_ATA_TRANSLATION_LBA ?
5028 ",trans=lba" :
5029 translation == BIOS_ATA_TRANSLATION_NONE ?
5030 ",trans=none" : "");
5031 break;
5032 case QEMU_OPTION_hdb:
5033 case QEMU_OPTION_hdc:
5034 case QEMU_OPTION_hdd:
5035 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
5036 break;
5037 case QEMU_OPTION_drive:
5038 drive_add(NULL, "%s", optarg);
5039 break;
5040 case QEMU_OPTION_mtdblock:
5041 drive_add(optarg, MTD_ALIAS);
5042 break;
5043 case QEMU_OPTION_sd:
5044 drive_add(optarg, SD_ALIAS);
5045 break;
5046 case QEMU_OPTION_pflash:
5047 drive_add(optarg, PFLASH_ALIAS);
5048 break;
5049 case QEMU_OPTION_snapshot:
5050 snapshot = 1;
5051 break;
5052 case QEMU_OPTION_hdachs:
5054 const char *p;
5055 p = optarg;
5056 cyls = strtol(p, (char **)&p, 0);
5057 if (cyls < 1 || cyls > 16383)
5058 goto chs_fail;
5059 if (*p != ',')
5060 goto chs_fail;
5061 p++;
5062 heads = strtol(p, (char **)&p, 0);
5063 if (heads < 1 || heads > 16)
5064 goto chs_fail;
5065 if (*p != ',')
5066 goto chs_fail;
5067 p++;
5068 secs = strtol(p, (char **)&p, 0);
5069 if (secs < 1 || secs > 63)
5070 goto chs_fail;
5071 if (*p == ',') {
5072 p++;
5073 if (!strcmp(p, "none"))
5074 translation = BIOS_ATA_TRANSLATION_NONE;
5075 else if (!strcmp(p, "lba"))
5076 translation = BIOS_ATA_TRANSLATION_LBA;
5077 else if (!strcmp(p, "auto"))
5078 translation = BIOS_ATA_TRANSLATION_AUTO;
5079 else
5080 goto chs_fail;
5081 } else if (*p != '\0') {
5082 chs_fail:
5083 fprintf(stderr, "qemu: invalid physical CHS format\n");
5084 exit(1);
5086 if (hda_index != -1)
5087 snprintf(drives_opt[hda_index].opt,
5088 sizeof(drives_opt[hda_index].opt),
5089 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5090 0, cyls, heads, secs,
5091 translation == BIOS_ATA_TRANSLATION_LBA ?
5092 ",trans=lba" :
5093 translation == BIOS_ATA_TRANSLATION_NONE ?
5094 ",trans=none" : "");
5096 break;
5097 case QEMU_OPTION_numa:
5098 if (nb_numa_nodes >= MAX_NODES) {
5099 fprintf(stderr, "qemu: too many NUMA nodes\n");
5100 exit(1);
5102 numa_add(optarg);
5103 break;
5104 case QEMU_OPTION_nographic:
5105 display_type = DT_NOGRAPHIC;
5106 break;
5107 #ifdef CONFIG_CURSES
5108 case QEMU_OPTION_curses:
5109 display_type = DT_CURSES;
5110 break;
5111 #endif
5112 case QEMU_OPTION_portrait:
5113 graphic_rotate = 1;
5114 break;
5115 case QEMU_OPTION_kernel:
5116 kernel_filename = optarg;
5117 break;
5118 case QEMU_OPTION_append:
5119 kernel_cmdline = optarg;
5120 break;
5121 case QEMU_OPTION_cdrom:
5122 drive_add(optarg, CDROM_ALIAS);
5123 break;
5124 case QEMU_OPTION_boot:
5125 boot_devices = optarg;
5126 /* We just do some generic consistency checks */
5128 /* Could easily be extended to 64 devices if needed */
5129 const char *p;
5131 boot_devices_bitmap = 0;
5132 for (p = boot_devices; *p != '\0'; p++) {
5133 /* Allowed boot devices are:
5134 * a b : floppy disk drives
5135 * c ... f : IDE disk drives
5136 * g ... m : machine implementation dependant drives
5137 * n ... p : network devices
5138 * It's up to each machine implementation to check
5139 * if the given boot devices match the actual hardware
5140 * implementation and firmware features.
5142 if (*p < 'a' || *p > 'q') {
5143 fprintf(stderr, "Invalid boot device '%c'\n", *p);
5144 exit(1);
5146 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5147 fprintf(stderr,
5148 "Boot device '%c' was given twice\n",*p);
5149 exit(1);
5151 boot_devices_bitmap |= 1 << (*p - 'a');
5154 break;
5155 case QEMU_OPTION_fda:
5156 case QEMU_OPTION_fdb:
5157 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
5158 break;
5159 #ifdef TARGET_I386
5160 case QEMU_OPTION_no_fd_bootchk:
5161 fd_bootchk = 0;
5162 break;
5163 #endif
5164 case QEMU_OPTION_net:
5165 if (nb_net_clients >= MAX_NET_CLIENTS) {
5166 fprintf(stderr, "qemu: too many network clients\n");
5167 exit(1);
5169 net_clients[nb_net_clients] = optarg;
5170 nb_net_clients++;
5171 break;
5172 #ifdef CONFIG_SLIRP
5173 case QEMU_OPTION_tftp:
5174 legacy_tftp_prefix = optarg;
5175 break;
5176 case QEMU_OPTION_bootp:
5177 legacy_bootp_filename = optarg;
5178 break;
5179 #ifndef _WIN32
5180 case QEMU_OPTION_smb:
5181 net_slirp_smb(optarg);
5182 break;
5183 #endif
5184 case QEMU_OPTION_redir:
5185 net_slirp_redir(optarg);
5186 break;
5187 #endif
5188 case QEMU_OPTION_bt:
5189 if (nb_bt_opts >= MAX_BT_CMDLINE) {
5190 fprintf(stderr, "qemu: too many bluetooth options\n");
5191 exit(1);
5193 bt_opts[nb_bt_opts++] = optarg;
5194 break;
5195 #ifdef HAS_AUDIO
5196 case QEMU_OPTION_audio_help:
5197 AUD_help ();
5198 exit (0);
5199 break;
5200 case QEMU_OPTION_soundhw:
5201 select_soundhw (optarg);
5202 break;
5203 #endif
5204 case QEMU_OPTION_h:
5205 help(0);
5206 break;
5207 case QEMU_OPTION_version:
5208 version();
5209 exit(0);
5210 break;
5211 case QEMU_OPTION_m: {
5212 uint64_t value;
5213 char *ptr;
5215 value = strtoul(optarg, &ptr, 10);
5216 switch (*ptr) {
5217 case 0: case 'M': case 'm':
5218 value <<= 20;
5219 break;
5220 case 'G': case 'g':
5221 value <<= 30;
5222 break;
5223 default:
5224 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
5225 exit(1);
5228 /* On 32-bit hosts, QEMU is limited by virtual address space */
5229 if (value > (2047 << 20)
5230 #ifndef CONFIG_KQEMU
5231 && HOST_LONG_BITS == 32
5232 #endif
5234 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5235 exit(1);
5237 if (value != (uint64_t)(ram_addr_t)value) {
5238 fprintf(stderr, "qemu: ram size too large\n");
5239 exit(1);
5241 ram_size = value;
5242 break;
5244 case QEMU_OPTION_d:
5246 int mask;
5247 const CPULogItem *item;
5249 mask = cpu_str_to_log_mask(optarg);
5250 if (!mask) {
5251 printf("Log items (comma separated):\n");
5252 for(item = cpu_log_items; item->mask != 0; item++) {
5253 printf("%-10s %s\n", item->name, item->help);
5255 exit(1);
5257 cpu_set_log(mask);
5259 break;
5260 case QEMU_OPTION_s:
5261 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
5262 break;
5263 case QEMU_OPTION_gdb:
5264 gdbstub_dev = optarg;
5265 break;
5266 case QEMU_OPTION_L:
5267 data_dir = optarg;
5268 break;
5269 case QEMU_OPTION_bios:
5270 bios_name = optarg;
5271 break;
5272 case QEMU_OPTION_singlestep:
5273 singlestep = 1;
5274 break;
5275 case QEMU_OPTION_S:
5276 autostart = 0;
5277 break;
5278 #ifndef _WIN32
5279 case QEMU_OPTION_k:
5280 keyboard_layout = optarg;
5281 break;
5282 #endif
5283 case QEMU_OPTION_localtime:
5284 rtc_utc = 0;
5285 break;
5286 case QEMU_OPTION_vga:
5287 select_vgahw (optarg);
5288 break;
5289 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5290 case QEMU_OPTION_g:
5292 const char *p;
5293 int w, h, depth;
5294 p = optarg;
5295 w = strtol(p, (char **)&p, 10);
5296 if (w <= 0) {
5297 graphic_error:
5298 fprintf(stderr, "qemu: invalid resolution or depth\n");
5299 exit(1);
5301 if (*p != 'x')
5302 goto graphic_error;
5303 p++;
5304 h = strtol(p, (char **)&p, 10);
5305 if (h <= 0)
5306 goto graphic_error;
5307 if (*p == 'x') {
5308 p++;
5309 depth = strtol(p, (char **)&p, 10);
5310 if (depth != 8 && depth != 15 && depth != 16 &&
5311 depth != 24 && depth != 32)
5312 goto graphic_error;
5313 } else if (*p == '\0') {
5314 depth = graphic_depth;
5315 } else {
5316 goto graphic_error;
5319 graphic_width = w;
5320 graphic_height = h;
5321 graphic_depth = depth;
5323 break;
5324 #endif
5325 case QEMU_OPTION_echr:
5327 char *r;
5328 term_escape_char = strtol(optarg, &r, 0);
5329 if (r == optarg)
5330 printf("Bad argument to echr\n");
5331 break;
5333 case QEMU_OPTION_monitor:
5334 monitor_device = optarg;
5335 break;
5336 case QEMU_OPTION_serial:
5337 if (serial_device_index >= MAX_SERIAL_PORTS) {
5338 fprintf(stderr, "qemu: too many serial ports\n");
5339 exit(1);
5341 serial_devices[serial_device_index] = optarg;
5342 serial_device_index++;
5343 break;
5344 case QEMU_OPTION_watchdog:
5345 i = select_watchdog(optarg);
5346 if (i > 0)
5347 exit (i == 1 ? 1 : 0);
5348 break;
5349 case QEMU_OPTION_watchdog_action:
5350 if (select_watchdog_action(optarg) == -1) {
5351 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5352 exit(1);
5354 break;
5355 case QEMU_OPTION_virtiocon:
5356 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5357 fprintf(stderr, "qemu: too many virtio consoles\n");
5358 exit(1);
5360 virtio_consoles[virtio_console_index] = optarg;
5361 virtio_console_index++;
5362 break;
5363 case QEMU_OPTION_parallel:
5364 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5365 fprintf(stderr, "qemu: too many parallel ports\n");
5366 exit(1);
5368 parallel_devices[parallel_device_index] = optarg;
5369 parallel_device_index++;
5370 break;
5371 case QEMU_OPTION_loadvm:
5372 loadvm = optarg;
5373 break;
5374 case QEMU_OPTION_full_screen:
5375 full_screen = 1;
5376 break;
5377 #ifdef CONFIG_SDL
5378 case QEMU_OPTION_no_frame:
5379 no_frame = 1;
5380 break;
5381 case QEMU_OPTION_alt_grab:
5382 alt_grab = 1;
5383 break;
5384 case QEMU_OPTION_no_quit:
5385 no_quit = 1;
5386 break;
5387 case QEMU_OPTION_sdl:
5388 display_type = DT_SDL;
5389 break;
5390 #endif
5391 case QEMU_OPTION_pidfile:
5392 pid_file = optarg;
5393 break;
5394 #ifdef TARGET_I386
5395 case QEMU_OPTION_win2k_hack:
5396 win2k_install_hack = 1;
5397 break;
5398 case QEMU_OPTION_rtc_td_hack:
5399 rtc_td_hack = 1;
5400 break;
5401 case QEMU_OPTION_acpitable:
5402 if(acpi_table_add(optarg) < 0) {
5403 fprintf(stderr, "Wrong acpi table provided\n");
5404 exit(1);
5406 break;
5407 case QEMU_OPTION_smbios:
5408 if(smbios_entry_add(optarg) < 0) {
5409 fprintf(stderr, "Wrong smbios provided\n");
5410 exit(1);
5412 break;
5413 #endif
5414 #ifdef CONFIG_KQEMU
5415 case QEMU_OPTION_enable_kqemu:
5416 kqemu_allowed = 1;
5417 break;
5418 case QEMU_OPTION_kernel_kqemu:
5419 kqemu_allowed = 2;
5420 break;
5421 #endif
5422 #ifdef CONFIG_KVM
5423 #ifdef KVM_UPSTREAM
5424 case QEMU_OPTION_enable_kvm:
5425 kvm_allowed = 1;
5426 #ifdef CONFIG_KQEMU
5427 kqemu_allowed = 0;
5428 #endif
5429 #endif
5430 break;
5431 case QEMU_OPTION_no_kvm:
5432 kvm_allowed = 0;
5433 break;
5434 case QEMU_OPTION_no_kvm_irqchip: {
5435 kvm_irqchip = 0;
5436 kvm_pit = 0;
5437 break;
5439 case QEMU_OPTION_no_kvm_pit: {
5440 kvm_pit = 0;
5441 break;
5443 case QEMU_OPTION_no_kvm_pit_reinjection: {
5444 kvm_pit_reinject = 0;
5445 break;
5447 case QEMU_OPTION_enable_nesting: {
5448 kvm_nested = 1;
5449 break;
5451 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5452 case QEMU_OPTION_pcidevice:
5453 if (assigned_devices_index >= MAX_DEV_ASSIGN_CMDLINE) {
5454 fprintf(stderr, "Too many assigned devices\n");
5455 exit(1);
5457 assigned_devices[assigned_devices_index] = optarg;
5458 assigned_devices_index++;
5459 break;
5460 #endif
5461 #endif
5462 case QEMU_OPTION_usb:
5463 usb_enabled = 1;
5464 break;
5465 case QEMU_OPTION_usbdevice:
5466 usb_enabled = 1;
5467 if (usb_devices_index >= MAX_USB_CMDLINE) {
5468 fprintf(stderr, "Too many USB devices\n");
5469 exit(1);
5471 usb_devices[usb_devices_index] = optarg;
5472 usb_devices_index++;
5473 break;
5474 case QEMU_OPTION_smp:
5475 smp_cpus = atoi(optarg);
5476 if (smp_cpus < 1) {
5477 fprintf(stderr, "Invalid number of CPUs\n");
5478 exit(1);
5480 break;
5481 case QEMU_OPTION_vnc:
5482 display_type = DT_VNC;
5483 vnc_display = optarg;
5484 break;
5485 #ifdef TARGET_I386
5486 case QEMU_OPTION_no_acpi:
5487 acpi_enabled = 0;
5488 break;
5489 case QEMU_OPTION_no_hpet:
5490 no_hpet = 1;
5491 break;
5492 case QEMU_OPTION_balloon:
5493 if (balloon_parse(optarg) < 0) {
5494 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5495 exit(1);
5497 break;
5498 #endif
5499 case QEMU_OPTION_no_reboot:
5500 no_reboot = 1;
5501 break;
5502 case QEMU_OPTION_no_shutdown:
5503 no_shutdown = 1;
5504 break;
5505 case QEMU_OPTION_show_cursor:
5506 cursor_hide = 0;
5507 break;
5508 case QEMU_OPTION_uuid:
5509 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5510 fprintf(stderr, "Fail to parse UUID string."
5511 " Wrong format.\n");
5512 exit(1);
5514 break;
5515 #ifndef _WIN32
5516 case QEMU_OPTION_daemonize:
5517 daemonize = 1;
5518 break;
5519 #endif
5520 case QEMU_OPTION_option_rom:
5521 if (nb_option_roms >= MAX_OPTION_ROMS) {
5522 fprintf(stderr, "Too many option ROMs\n");
5523 exit(1);
5525 option_rom[nb_option_roms] = optarg;
5526 nb_option_roms++;
5527 break;
5528 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5529 case QEMU_OPTION_semihosting:
5530 semihosting_enabled = 1;
5531 break;
5532 #endif
5533 case QEMU_OPTION_tdf:
5534 time_drift_fix = 1;
5535 break;
5536 case QEMU_OPTION_kvm_shadow_memory:
5537 kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
5538 break;
5539 case QEMU_OPTION_mempath:
5540 mem_path = optarg;
5541 break;
5542 #ifdef MAP_POPULATE
5543 case QEMU_OPTION_mem_prealloc:
5544 mem_prealloc = !mem_prealloc;
5545 break;
5546 #endif
5547 case QEMU_OPTION_name:
5548 qemu_name = qemu_strdup(optarg);
5550 char *p = strchr(qemu_name, ',');
5551 if (p != NULL) {
5552 *p++ = 0;
5553 if (strncmp(p, "process=", 8)) {
5554 fprintf(stderr, "Unknown subargument %s to -name", p);
5555 exit(1);
5557 p += 8;
5558 set_proc_name(p);
5561 break;
5562 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5563 case QEMU_OPTION_prom_env:
5564 if (nb_prom_envs >= MAX_PROM_ENVS) {
5565 fprintf(stderr, "Too many prom variables\n");
5566 exit(1);
5568 prom_envs[nb_prom_envs] = optarg;
5569 nb_prom_envs++;
5570 break;
5571 #endif
5572 #ifdef TARGET_ARM
5573 case QEMU_OPTION_old_param:
5574 old_param = 1;
5575 break;
5576 #endif
5577 case QEMU_OPTION_clock:
5578 configure_alarms(optarg);
5579 break;
5580 case QEMU_OPTION_startdate:
5582 struct tm tm;
5583 time_t rtc_start_date;
5584 if (!strcmp(optarg, "now")) {
5585 rtc_date_offset = -1;
5586 } else {
5587 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5588 &tm.tm_year,
5589 &tm.tm_mon,
5590 &tm.tm_mday,
5591 &tm.tm_hour,
5592 &tm.tm_min,
5593 &tm.tm_sec) == 6) {
5594 /* OK */
5595 } else if (sscanf(optarg, "%d-%d-%d",
5596 &tm.tm_year,
5597 &tm.tm_mon,
5598 &tm.tm_mday) == 3) {
5599 tm.tm_hour = 0;
5600 tm.tm_min = 0;
5601 tm.tm_sec = 0;
5602 } else {
5603 goto date_fail;
5605 tm.tm_year -= 1900;
5606 tm.tm_mon--;
5607 rtc_start_date = mktimegm(&tm);
5608 if (rtc_start_date == -1) {
5609 date_fail:
5610 fprintf(stderr, "Invalid date format. Valid format are:\n"
5611 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5612 exit(1);
5614 rtc_date_offset = time(NULL) - rtc_start_date;
5617 break;
5618 case QEMU_OPTION_tb_size:
5619 tb_size = strtol(optarg, NULL, 0);
5620 if (tb_size < 0)
5621 tb_size = 0;
5622 break;
5623 case QEMU_OPTION_icount:
5624 use_icount = 1;
5625 if (strcmp(optarg, "auto") == 0) {
5626 icount_time_shift = -1;
5627 } else {
5628 icount_time_shift = strtol(optarg, NULL, 0);
5630 break;
5631 case QEMU_OPTION_incoming:
5632 incoming = optarg;
5633 break;
5634 #ifndef _WIN32
5635 case QEMU_OPTION_chroot:
5636 chroot_dir = optarg;
5637 break;
5638 case QEMU_OPTION_runas:
5639 run_as = optarg;
5640 break;
5641 case QEMU_OPTION_nvram:
5642 nvram = optarg;
5643 break;
5644 #endif
5645 #ifdef CONFIG_XEN
5646 case QEMU_OPTION_xen_domid:
5647 xen_domid = atoi(optarg);
5648 break;
5649 case QEMU_OPTION_xen_create:
5650 xen_mode = XEN_CREATE;
5651 break;
5652 case QEMU_OPTION_xen_attach:
5653 xen_mode = XEN_ATTACH;
5654 break;
5655 #endif
5660 /* If no data_dir is specified then try to find it relative to the
5661 executable path. */
5662 if (!data_dir) {
5663 data_dir = find_datadir(argv[0]);
5665 /* If all else fails use the install patch specified when building. */
5666 if (!data_dir) {
5667 data_dir = CONFIG_QEMU_SHAREDIR;
5670 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5671 if (kvm_allowed && kqemu_allowed) {
5672 fprintf(stderr,
5673 "You can not enable both KVM and kqemu at the same time\n");
5674 exit(1);
5676 #endif
5678 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5679 if (smp_cpus > machine->max_cpus) {
5680 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5681 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5682 machine->max_cpus);
5683 exit(1);
5686 if (display_type == DT_NOGRAPHIC) {
5687 if (serial_device_index == 0)
5688 serial_devices[0] = "stdio";
5689 if (parallel_device_index == 0)
5690 parallel_devices[0] = "null";
5691 if (strncmp(monitor_device, "vc", 2) == 0)
5692 monitor_device = "stdio";
5695 #ifndef _WIN32
5696 if (daemonize) {
5697 pid_t pid;
5699 if (pipe(fds) == -1)
5700 exit(1);
5702 pid = fork();
5703 if (pid > 0) {
5704 uint8_t status;
5705 ssize_t len;
5707 close(fds[1]);
5709 again:
5710 len = read(fds[0], &status, 1);
5711 if (len == -1 && (errno == EINTR))
5712 goto again;
5714 if (len != 1)
5715 exit(1);
5716 else if (status == 1) {
5717 fprintf(stderr, "Could not acquire pidfile\n");
5718 exit(1);
5719 } else
5720 exit(0);
5721 } else if (pid < 0)
5722 exit(1);
5724 setsid();
5726 pid = fork();
5727 if (pid > 0)
5728 exit(0);
5729 else if (pid < 0)
5730 exit(1);
5732 umask(027);
5734 signal(SIGTSTP, SIG_IGN);
5735 signal(SIGTTOU, SIG_IGN);
5736 signal(SIGTTIN, SIG_IGN);
5739 #ifdef CONFIG_KVM
5740 if (kvm_enabled()) {
5741 if (kvm_init(smp_cpus) < 0) {
5742 fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
5743 #ifdef NO_CPU_EMULATION
5744 fprintf(stderr, "Compiled with --disable-cpu-emulation, exiting.\n");
5745 exit(1);
5746 #endif
5747 kvm_allowed = 0;
5750 #endif
5752 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5753 if (daemonize) {
5754 uint8_t status = 1;
5755 write(fds[1], &status, 1);
5756 } else
5757 fprintf(stderr, "Could not acquire pid file\n");
5758 exit(1);
5760 #endif
5762 #ifdef CONFIG_KQEMU
5763 if (smp_cpus > 1)
5764 kqemu_allowed = 0;
5765 #endif
5766 if (qemu_init_main_loop()) {
5767 fprintf(stderr, "qemu_init_main_loop failed\n");
5768 exit(1);
5770 linux_boot = (kernel_filename != NULL);
5772 if (!linux_boot && *kernel_cmdline != '\0') {
5773 fprintf(stderr, "-append only allowed with -kernel option\n");
5774 exit(1);
5777 if (!linux_boot && initrd_filename != NULL) {
5778 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5779 exit(1);
5782 /* boot to floppy or the default cd if no hard disk defined yet */
5783 if (!boot_devices[0]) {
5784 boot_devices = "cad";
5786 setvbuf(stdout, NULL, _IOLBF, 0);
5788 init_timers();
5789 if (init_timer_alarm() < 0) {
5790 fprintf(stderr, "could not initialize alarm timer\n");
5791 exit(1);
5793 if (use_icount && icount_time_shift < 0) {
5794 use_icount = 2;
5795 /* 125MIPS seems a reasonable initial guess at the guest speed.
5796 It will be corrected fairly quickly anyway. */
5797 icount_time_shift = 3;
5798 init_icount_adjust();
5801 #ifdef _WIN32
5802 socket_init();
5803 #endif
5805 /* init network clients */
5806 if (nb_net_clients == 0) {
5807 /* if no clients, we use a default config */
5808 net_clients[nb_net_clients++] = "nic";
5809 #ifdef CONFIG_SLIRP
5810 net_clients[nb_net_clients++] = "user";
5811 #endif
5814 for(i = 0;i < nb_net_clients; i++) {
5815 if (net_client_parse(net_clients[i]) < 0)
5816 exit(1);
5819 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5820 net_set_boot_mask(net_boot);
5822 net_client_check();
5824 /* init the bluetooth world */
5825 for (i = 0; i < nb_bt_opts; i++)
5826 if (bt_parse(bt_opts[i]))
5827 exit(1);
5829 /* init the memory */
5830 if (ram_size == 0)
5831 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5833 #ifdef CONFIG_KQEMU
5834 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5835 guest ram allocation. It needs to go away. */
5836 if (kqemu_allowed) {
5837 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5838 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5839 if (!kqemu_phys_ram_base) {
5840 fprintf(stderr, "Could not allocate physical memory\n");
5841 exit(1);
5844 #endif
5846 /* init the dynamic translator */
5847 cpu_exec_init_all(tb_size * 1024 * 1024);
5849 bdrv_init();
5851 /* we always create the cdrom drive, even if no disk is there */
5853 if (nb_drives_opt < MAX_DRIVES)
5854 drive_add(NULL, CDROM_ALIAS);
5856 /* we always create at least one floppy */
5858 if (nb_drives_opt < MAX_DRIVES)
5859 drive_add(NULL, FD_ALIAS, 0);
5861 /* we always create one sd slot, even if no card is in it */
5863 if (nb_drives_opt < MAX_DRIVES)
5864 drive_add(NULL, SD_ALIAS);
5866 /* open the virtual block devices */
5868 for(i = 0; i < nb_drives_opt; i++)
5869 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
5870 exit(1);
5872 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
5873 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5875 #ifndef _WIN32
5876 /* must be after terminal init, SDL library changes signal handlers */
5877 sighandler_setup();
5878 #endif
5880 /* Maintain compatibility with multiple stdio monitors */
5881 if (!strcmp(monitor_device,"stdio")) {
5882 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5883 const char *devname = serial_devices[i];
5884 if (devname && !strcmp(devname,"mon:stdio")) {
5885 monitor_device = NULL;
5886 break;
5887 } else if (devname && !strcmp(devname,"stdio")) {
5888 monitor_device = NULL;
5889 serial_devices[i] = "mon:stdio";
5890 break;
5895 if (nb_numa_nodes > 0) {
5896 int i;
5898 if (nb_numa_nodes > smp_cpus) {
5899 nb_numa_nodes = smp_cpus;
5902 /* If no memory size if given for any node, assume the default case
5903 * and distribute the available memory equally across all nodes
5905 for (i = 0; i < nb_numa_nodes; i++) {
5906 if (node_mem[i] != 0)
5907 break;
5909 if (i == nb_numa_nodes) {
5910 uint64_t usedmem = 0;
5912 /* On Linux, the each node's border has to be 8MB aligned,
5913 * the final node gets the rest.
5915 for (i = 0; i < nb_numa_nodes - 1; i++) {
5916 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5917 usedmem += node_mem[i];
5919 node_mem[i] = ram_size - usedmem;
5922 for (i = 0; i < nb_numa_nodes; i++) {
5923 if (node_cpumask[i] != 0)
5924 break;
5926 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5927 * must cope with this anyway, because there are BIOSes out there in
5928 * real machines which also use this scheme.
5930 if (i == nb_numa_nodes) {
5931 for (i = 0; i < smp_cpus; i++) {
5932 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5937 #ifdef KVM_UPSTREAM
5938 if (kvm_enabled()) {
5939 int ret;
5941 ret = kvm_init(smp_cpus);
5942 if (ret < 0) {
5943 fprintf(stderr, "failed to initialize KVM\n");
5944 exit(1);
5947 #endif
5949 if (monitor_device) {
5950 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5951 if (!monitor_hd) {
5952 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5953 exit(1);
5957 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5958 const char *devname = serial_devices[i];
5959 if (devname && strcmp(devname, "none")) {
5960 char label[32];
5961 snprintf(label, sizeof(label), "serial%d", i);
5962 serial_hds[i] = qemu_chr_open(label, devname, NULL);
5963 if (!serial_hds[i]) {
5964 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5965 devname);
5966 exit(1);
5971 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5972 const char *devname = parallel_devices[i];
5973 if (devname && strcmp(devname, "none")) {
5974 char label[32];
5975 snprintf(label, sizeof(label), "parallel%d", i);
5976 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5977 if (!parallel_hds[i]) {
5978 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5979 devname);
5980 exit(1);
5985 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5986 const char *devname = virtio_consoles[i];
5987 if (devname && strcmp(devname, "none")) {
5988 char label[32];
5989 snprintf(label, sizeof(label), "virtcon%d", i);
5990 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5991 if (!virtcon_hds[i]) {
5992 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5993 devname);
5994 exit(1);
5999 module_call_init(MODULE_INIT_DEVICE);
6001 if (kvm_enabled()) {
6002 kvm_init_ap();
6003 #ifdef CONFIG_KVM
6004 if (kvm_irqchip) {
6005 if (!qemu_kvm_has_gsi_routing()) {
6006 irq0override = 0;
6007 #ifdef TARGET_I386
6008 /* if kernel can't do irq routing, interrupt source
6009 * override 0->2 can not be set up as required by hpet,
6010 * so disable hpet.
6012 no_hpet=1;
6013 } else if (!qemu_kvm_has_pit_state2()) {
6014 no_hpet=1;
6016 #else
6018 #endif
6020 #endif
6023 machine->init(ram_size, boot_devices,
6024 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6027 for (env = first_cpu; env != NULL; env = env->next_cpu) {
6028 for (i = 0; i < nb_numa_nodes; i++) {
6029 if (node_cpumask[i] & (1 << env->cpu_index)) {
6030 env->numa_node = i;
6035 current_machine = machine;
6037 /* init USB devices */
6038 if (usb_enabled) {
6039 for(i = 0; i < usb_devices_index; i++) {
6040 if (usb_device_add(usb_devices[i], 0) < 0) {
6041 fprintf(stderr, "Warning: could not add USB device %s\n",
6042 usb_devices[i]);
6047 if (!display_state)
6048 dumb_display_init();
6049 /* just use the first displaystate for the moment */
6050 ds = display_state;
6052 if (display_type == DT_DEFAULT) {
6053 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6054 display_type = DT_SDL;
6055 #else
6056 display_type = DT_VNC;
6057 vnc_display = "localhost:0,to=99";
6058 show_vnc_port = 1;
6059 #endif
6063 switch (display_type) {
6064 case DT_NOGRAPHIC:
6065 break;
6066 #if defined(CONFIG_CURSES)
6067 case DT_CURSES:
6068 curses_display_init(ds, full_screen);
6069 break;
6070 #endif
6071 #if defined(CONFIG_SDL)
6072 case DT_SDL:
6073 sdl_display_init(ds, full_screen, no_frame);
6074 break;
6075 #elif defined(CONFIG_COCOA)
6076 case DT_SDL:
6077 cocoa_display_init(ds, full_screen);
6078 break;
6079 #endif
6080 case DT_VNC:
6081 vnc_display_init(ds);
6082 if (vnc_display_open(ds, vnc_display) < 0)
6083 exit(1);
6085 if (show_vnc_port) {
6086 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
6088 break;
6089 default:
6090 break;
6092 dpy_resize(ds);
6094 dcl = ds->listeners;
6095 while (dcl != NULL) {
6096 if (dcl->dpy_refresh != NULL) {
6097 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6098 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
6100 dcl = dcl->next;
6103 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
6104 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6105 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6108 text_consoles_set_display(display_state);
6109 qemu_chr_initial_reset();
6111 if (monitor_device && monitor_hd)
6112 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
6114 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6115 const char *devname = serial_devices[i];
6116 if (devname && strcmp(devname, "none")) {
6117 if (strstart(devname, "vc", 0))
6118 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
6122 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6123 const char *devname = parallel_devices[i];
6124 if (devname && strcmp(devname, "none")) {
6125 if (strstart(devname, "vc", 0))
6126 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
6130 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6131 const char *devname = virtio_consoles[i];
6132 if (virtcon_hds[i] && devname) {
6133 if (strstart(devname, "vc", 0))
6134 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6138 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6139 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6140 gdbstub_dev);
6141 exit(1);
6144 if (loadvm)
6145 do_loadvm(cur_mon, loadvm);
6147 if (incoming) {
6148 autostart = 0; /* fixme how to deal with -daemonize */
6149 qemu_start_incoming_migration(incoming);
6152 if (autostart)
6153 vm_start();
6155 #ifndef _WIN32
6156 if (daemonize) {
6157 uint8_t status = 0;
6158 ssize_t len;
6160 again1:
6161 len = write(fds[1], &status, 1);
6162 if (len == -1 && (errno == EINTR))
6163 goto again1;
6165 if (len != 1)
6166 exit(1);
6168 chdir("/");
6169 TFR(fd = open("/dev/null", O_RDWR));
6170 if (fd == -1)
6171 exit(1);
6174 if (run_as) {
6175 pwd = getpwnam(run_as);
6176 if (!pwd) {
6177 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6178 exit(1);
6182 if (chroot_dir) {
6183 if (chroot(chroot_dir) < 0) {
6184 fprintf(stderr, "chroot failed\n");
6185 exit(1);
6187 chdir("/");
6190 if (run_as) {
6191 if (setgid(pwd->pw_gid) < 0) {
6192 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6193 exit(1);
6195 if (setuid(pwd->pw_uid) < 0) {
6196 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6197 exit(1);
6199 if (setuid(0) != -1) {
6200 fprintf(stderr, "Dropping privileges failed\n");
6201 exit(1);
6205 if (daemonize) {
6206 dup2(fd, 0);
6207 dup2(fd, 1);
6208 dup2(fd, 2);
6210 close(fd);
6212 #endif
6214 main_loop();
6215 quit_timers();
6216 net_cleanup();
6218 return 0;