Concentrate most table entries to top
[qemu-kvm/fedora.git] / vl.c
blob3242c230eae1b20e4d685b629a14b35045bb32ae
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>
72 /* For the benefit of older linux systems which don't supply it,
73 we use a local copy of hpet.h. */
74 /* #include <linux/hpet.h> */
75 #include "hpet.h"
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
79 #endif
80 #ifdef __sun__
81 #include <sys/stat.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
91 #include <net/if.h>
92 #include <syslog.h>
93 #include <stropts.h>
94 #endif
95 #endif
96 #endif
98 #if defined(__OpenBSD__)
99 #include <util.h>
100 #endif
102 #if defined(CONFIG_VDE)
103 #include <libvdeplug.h>
104 #endif
106 #ifdef _WIN32
107 #include <windows.h>
108 #include <malloc.h>
109 #include <sys/timeb.h>
110 #include <mmsystem.h>
111 #define getopt_long_only getopt_long
112 #define memalign(align, size) malloc(size)
113 #endif
115 #ifdef CONFIG_SDL
116 #ifdef __APPLE__
117 #include <SDL.h>
118 int qemu_main(int argc, char **argv, char **envp);
119 int main(int argc, char **argv)
121 qemu_main(argc, argv, NULL);
123 #undef main
124 #define main qemu_main
125 #endif
126 #endif /* CONFIG_SDL */
128 #ifdef CONFIG_COCOA
129 #undef main
130 #define main qemu_main
131 #endif /* CONFIG_COCOA */
133 #include "hw/hw.h"
134 #include "hw/boards.h"
135 #include "hw/usb.h"
136 #include "hw/pcmcia.h"
137 #include "hw/pc.h"
138 #include "hw/audiodev.h"
139 #include "hw/isa.h"
140 #include "hw/baum.h"
141 #include "hw/bt.h"
142 #include "hw/watchdog.h"
143 #include "hw/smbios.h"
144 #include "hw/xen.h"
145 #include "bt-host.h"
146 #include "net.h"
147 #include "monitor.h"
148 #include "console.h"
149 #include "sysemu.h"
150 #include "gdbstub.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
154 #include "block.h"
155 #include "dma.h"
156 #include "audio/audio.h"
157 #include "migration.h"
158 #include "kvm.h"
159 #include "balloon.h"
160 #include "qemu-option.h"
162 #include "disas.h"
164 #include "exec-all.h"
166 #include "qemu_socket.h"
168 #if defined(CONFIG_SLIRP)
169 #include "libslirp.h"
170 #endif
172 //#define DEBUG_UNUSED_IOPORT
173 //#define DEBUG_IOPORT
174 //#define DEBUG_NET
175 //#define DEBUG_SLIRP
178 #ifdef DEBUG_IOPORT
179 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
180 #else
181 # define LOG_IOPORT(...) do { } while (0)
182 #endif
184 #define DEFAULT_RAM_SIZE 128
186 /* Max number of USB devices that can be specified on the commandline. */
187 #define MAX_USB_CMDLINE 8
189 /* Max number of bluetooth switches on the commandline. */
190 #define MAX_BT_CMDLINE 10
192 /* XXX: use a two level table to limit memory usage */
193 #define MAX_IOPORTS 65536
195 static const char *data_dir;
196 const char *bios_name = NULL;
197 static void *ioport_opaque[MAX_IOPORTS];
198 static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
199 static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
200 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
201 to store the VM snapshots */
202 DriveInfo drives_table[MAX_DRIVES+1];
203 int nb_drives;
204 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
205 static DisplayState *display_state;
206 DisplayType display_type = DT_DEFAULT;
207 const char* keyboard_layout = NULL;
208 int64_t ticks_per_sec;
209 ram_addr_t ram_size;
210 int nb_nics;
211 NICInfo nd_table[MAX_NICS];
212 int vm_running;
213 static int autostart;
214 static int rtc_utc = 1;
215 static int rtc_date_offset = -1; /* -1 means no change */
216 int cirrus_vga_enabled = 1;
217 int std_vga_enabled = 0;
218 int vmsvga_enabled = 0;
219 int xenfb_enabled = 0;
220 #ifdef TARGET_SPARC
221 int graphic_width = 1024;
222 int graphic_height = 768;
223 int graphic_depth = 8;
224 #else
225 int graphic_width = 800;
226 int graphic_height = 600;
227 int graphic_depth = 15;
228 #endif
229 static int full_screen = 0;
230 #ifdef CONFIG_SDL
231 static int no_frame = 0;
232 #endif
233 int no_quit = 0;
234 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
235 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
236 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
237 #ifdef TARGET_I386
238 int win2k_install_hack = 0;
239 int rtc_td_hack = 0;
240 #endif
241 int usb_enabled = 0;
242 int singlestep = 0;
243 int smp_cpus = 1;
244 const char *vnc_display;
245 int acpi_enabled = 1;
246 int no_hpet = 0;
247 int no_virtio_balloon = 0;
248 int fd_bootchk = 1;
249 int no_reboot = 0;
250 int no_shutdown = 0;
251 int cursor_hide = 1;
252 int graphic_rotate = 0;
253 #ifndef _WIN32
254 int daemonize = 0;
255 #endif
256 WatchdogTimerModel *watchdog = NULL;
257 int watchdog_action = WDT_RESET;
258 const char *option_rom[MAX_OPTION_ROMS];
259 int nb_option_roms;
260 int semihosting_enabled = 0;
261 #ifdef TARGET_ARM
262 int old_param = 0;
263 #endif
264 const char *qemu_name;
265 int alt_grab = 0;
266 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
267 unsigned int nb_prom_envs = 0;
268 const char *prom_envs[MAX_PROM_ENVS];
269 #endif
270 int nb_drives_opt;
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 static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
299 static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
301 static uint32_t ioport_read(int index, uint32_t address)
303 static IOPortReadFunc *default_func[3] = {
304 default_ioport_readb,
305 default_ioport_readw,
306 default_ioport_readl
308 IOPortReadFunc *func = ioport_read_table[index][address];
309 if (!func)
310 func = default_func[index];
311 return func(ioport_opaque[address], address);
314 static void ioport_write(int index, uint32_t address, uint32_t data)
316 static IOPortWriteFunc *default_func[3] = {
317 default_ioport_writeb,
318 default_ioport_writew,
319 default_ioport_writel
321 IOPortWriteFunc *func = ioport_write_table[index][address];
322 if (!func)
323 func = default_func[index];
324 func(ioport_opaque[address], address, data);
327 static uint32_t default_ioport_readb(void *opaque, uint32_t address)
329 #ifdef DEBUG_UNUSED_IOPORT
330 fprintf(stderr, "unused inb: port=0x%04x\n", address);
331 #endif
332 return 0xff;
335 static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
337 #ifdef DEBUG_UNUSED_IOPORT
338 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
339 #endif
342 /* default is to make two byte accesses */
343 static uint32_t default_ioport_readw(void *opaque, uint32_t address)
345 uint32_t data;
346 data = ioport_read(0, address);
347 address = (address + 1) & (MAX_IOPORTS - 1);
348 data |= ioport_read(0, address) << 8;
349 return data;
352 static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
354 ioport_write(0, address, data & 0xff);
355 address = (address + 1) & (MAX_IOPORTS - 1);
356 ioport_write(0, address, (data >> 8) & 0xff);
359 static uint32_t default_ioport_readl(void *opaque, uint32_t address)
361 #ifdef DEBUG_UNUSED_IOPORT
362 fprintf(stderr, "unused inl: port=0x%04x\n", address);
363 #endif
364 return 0xffffffff;
367 static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
369 #ifdef DEBUG_UNUSED_IOPORT
370 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
371 #endif
374 /* size is the word size in byte */
375 int register_ioport_read(int start, int length, int size,
376 IOPortReadFunc *func, void *opaque)
378 int i, bsize;
380 if (size == 1) {
381 bsize = 0;
382 } else if (size == 2) {
383 bsize = 1;
384 } else if (size == 4) {
385 bsize = 2;
386 } else {
387 hw_error("register_ioport_read: invalid size");
388 return -1;
390 for(i = start; i < start + length; i += size) {
391 ioport_read_table[bsize][i] = func;
392 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
393 hw_error("register_ioport_read: invalid opaque");
394 ioport_opaque[i] = opaque;
396 return 0;
399 /* size is the word size in byte */
400 int register_ioport_write(int start, int length, int size,
401 IOPortWriteFunc *func, void *opaque)
403 int i, bsize;
405 if (size == 1) {
406 bsize = 0;
407 } else if (size == 2) {
408 bsize = 1;
409 } else if (size == 4) {
410 bsize = 2;
411 } else {
412 hw_error("register_ioport_write: invalid size");
413 return -1;
415 for(i = start; i < start + length; i += size) {
416 ioport_write_table[bsize][i] = func;
417 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
418 hw_error("register_ioport_write: invalid opaque");
419 ioport_opaque[i] = opaque;
421 return 0;
424 void isa_unassign_ioport(int start, int length)
426 int i;
428 for(i = start; i < start + length; i++) {
429 ioport_read_table[0][i] = default_ioport_readb;
430 ioport_read_table[1][i] = default_ioport_readw;
431 ioport_read_table[2][i] = default_ioport_readl;
433 ioport_write_table[0][i] = default_ioport_writeb;
434 ioport_write_table[1][i] = default_ioport_writew;
435 ioport_write_table[2][i] = default_ioport_writel;
437 ioport_opaque[i] = NULL;
441 /***********************************************************/
443 void cpu_outb(CPUState *env, int addr, int val)
445 LOG_IOPORT("outb: %04x %02x\n", addr, val);
446 ioport_write(0, addr, val);
447 #ifdef CONFIG_KQEMU
448 if (env)
449 env->last_io_time = cpu_get_time_fast();
450 #endif
453 void cpu_outw(CPUState *env, int addr, int val)
455 LOG_IOPORT("outw: %04x %04x\n", addr, val);
456 ioport_write(1, addr, val);
457 #ifdef CONFIG_KQEMU
458 if (env)
459 env->last_io_time = cpu_get_time_fast();
460 #endif
463 void cpu_outl(CPUState *env, int addr, int val)
465 LOG_IOPORT("outl: %04x %08x\n", addr, val);
466 ioport_write(2, addr, val);
467 #ifdef CONFIG_KQEMU
468 if (env)
469 env->last_io_time = cpu_get_time_fast();
470 #endif
473 int cpu_inb(CPUState *env, int addr)
475 int val;
476 val = ioport_read(0, addr);
477 LOG_IOPORT("inb : %04x %02x\n", addr, val);
478 #ifdef CONFIG_KQEMU
479 if (env)
480 env->last_io_time = cpu_get_time_fast();
481 #endif
482 return val;
485 int cpu_inw(CPUState *env, int addr)
487 int val;
488 val = ioport_read(1, addr);
489 LOG_IOPORT("inw : %04x %04x\n", addr, val);
490 #ifdef CONFIG_KQEMU
491 if (env)
492 env->last_io_time = cpu_get_time_fast();
493 #endif
494 return val;
497 int cpu_inl(CPUState *env, int addr)
499 int val;
500 val = ioport_read(2, addr);
501 LOG_IOPORT("inl : %04x %08x\n", addr, val);
502 #ifdef CONFIG_KQEMU
503 if (env)
504 env->last_io_time = cpu_get_time_fast();
505 #endif
506 return val;
509 /***********************************************************/
510 void hw_error(const char *fmt, ...)
512 va_list ap;
513 CPUState *env;
515 va_start(ap, fmt);
516 fprintf(stderr, "qemu: hardware error: ");
517 vfprintf(stderr, fmt, ap);
518 fprintf(stderr, "\n");
519 for(env = first_cpu; env != NULL; env = env->next_cpu) {
520 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
521 #ifdef TARGET_I386
522 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
523 #else
524 cpu_dump_state(env, stderr, fprintf, 0);
525 #endif
527 va_end(ap);
528 abort();
531 /***************/
532 /* ballooning */
534 static QEMUBalloonEvent *qemu_balloon_event;
535 void *qemu_balloon_event_opaque;
537 void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
539 qemu_balloon_event = func;
540 qemu_balloon_event_opaque = opaque;
543 void qemu_balloon(ram_addr_t target)
545 if (qemu_balloon_event)
546 qemu_balloon_event(qemu_balloon_event_opaque, target);
549 ram_addr_t qemu_balloon_status(void)
551 if (qemu_balloon_event)
552 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
553 return 0;
556 /***********************************************************/
557 /* keyboard/mouse */
559 static QEMUPutKBDEvent *qemu_put_kbd_event;
560 static void *qemu_put_kbd_event_opaque;
561 static QEMUPutMouseEntry *qemu_put_mouse_event_head;
562 static QEMUPutMouseEntry *qemu_put_mouse_event_current;
564 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
566 qemu_put_kbd_event_opaque = opaque;
567 qemu_put_kbd_event = func;
570 QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
571 void *opaque, int absolute,
572 const char *name)
574 QEMUPutMouseEntry *s, *cursor;
576 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
578 s->qemu_put_mouse_event = func;
579 s->qemu_put_mouse_event_opaque = opaque;
580 s->qemu_put_mouse_event_absolute = absolute;
581 s->qemu_put_mouse_event_name = qemu_strdup(name);
582 s->next = NULL;
584 if (!qemu_put_mouse_event_head) {
585 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
586 return s;
589 cursor = qemu_put_mouse_event_head;
590 while (cursor->next != NULL)
591 cursor = cursor->next;
593 cursor->next = s;
594 qemu_put_mouse_event_current = s;
596 return s;
599 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
601 QEMUPutMouseEntry *prev = NULL, *cursor;
603 if (!qemu_put_mouse_event_head || entry == NULL)
604 return;
606 cursor = qemu_put_mouse_event_head;
607 while (cursor != NULL && cursor != entry) {
608 prev = cursor;
609 cursor = cursor->next;
612 if (cursor == NULL) // does not exist or list empty
613 return;
614 else if (prev == NULL) { // entry is head
615 qemu_put_mouse_event_head = cursor->next;
616 if (qemu_put_mouse_event_current == entry)
617 qemu_put_mouse_event_current = cursor->next;
618 qemu_free(entry->qemu_put_mouse_event_name);
619 qemu_free(entry);
620 return;
623 prev->next = entry->next;
625 if (qemu_put_mouse_event_current == entry)
626 qemu_put_mouse_event_current = prev;
628 qemu_free(entry->qemu_put_mouse_event_name);
629 qemu_free(entry);
632 void kbd_put_keycode(int keycode)
634 if (qemu_put_kbd_event) {
635 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
639 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
641 QEMUPutMouseEvent *mouse_event;
642 void *mouse_event_opaque;
643 int width;
645 if (!qemu_put_mouse_event_current) {
646 return;
649 mouse_event =
650 qemu_put_mouse_event_current->qemu_put_mouse_event;
651 mouse_event_opaque =
652 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
654 if (mouse_event) {
655 if (graphic_rotate) {
656 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
657 width = 0x7fff;
658 else
659 width = graphic_width - 1;
660 mouse_event(mouse_event_opaque,
661 width - dy, dx, dz, buttons_state);
662 } else
663 mouse_event(mouse_event_opaque,
664 dx, dy, dz, buttons_state);
668 int kbd_mouse_is_absolute(void)
670 if (!qemu_put_mouse_event_current)
671 return 0;
673 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
676 void do_info_mice(Monitor *mon)
678 QEMUPutMouseEntry *cursor;
679 int index = 0;
681 if (!qemu_put_mouse_event_head) {
682 monitor_printf(mon, "No mouse devices connected\n");
683 return;
686 monitor_printf(mon, "Mouse devices available:\n");
687 cursor = qemu_put_mouse_event_head;
688 while (cursor != NULL) {
689 monitor_printf(mon, "%c Mouse #%d: %s\n",
690 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
691 index, cursor->qemu_put_mouse_event_name);
692 index++;
693 cursor = cursor->next;
697 void do_mouse_set(Monitor *mon, int index)
699 QEMUPutMouseEntry *cursor;
700 int i = 0;
702 if (!qemu_put_mouse_event_head) {
703 monitor_printf(mon, "No mouse devices connected\n");
704 return;
707 cursor = qemu_put_mouse_event_head;
708 while (cursor != NULL && index != i) {
709 i++;
710 cursor = cursor->next;
713 if (cursor != NULL)
714 qemu_put_mouse_event_current = cursor;
715 else
716 monitor_printf(mon, "Mouse at given index not found\n");
719 /* compute with 96 bit intermediate result: (a*b)/c */
720 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
722 union {
723 uint64_t ll;
724 struct {
725 #ifdef WORDS_BIGENDIAN
726 uint32_t high, low;
727 #else
728 uint32_t low, high;
729 #endif
730 } l;
731 } u, res;
732 uint64_t rl, rh;
734 u.ll = a;
735 rl = (uint64_t)u.l.low * (uint64_t)b;
736 rh = (uint64_t)u.l.high * (uint64_t)b;
737 rh += (rl >> 32);
738 res.l.high = rh / c;
739 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
740 return res.ll;
743 /***********************************************************/
744 /* real time host monotonic timer */
746 #define QEMU_TIMER_BASE 1000000000LL
748 #ifdef WIN32
750 static int64_t clock_freq;
752 static void init_get_clock(void)
754 LARGE_INTEGER freq;
755 int ret;
756 ret = QueryPerformanceFrequency(&freq);
757 if (ret == 0) {
758 fprintf(stderr, "Could not calibrate ticks\n");
759 exit(1);
761 clock_freq = freq.QuadPart;
764 static int64_t get_clock(void)
766 LARGE_INTEGER ti;
767 QueryPerformanceCounter(&ti);
768 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
771 #else
773 static int use_rt_clock;
775 static void init_get_clock(void)
777 use_rt_clock = 0;
778 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
779 || defined(__DragonFly__)
781 struct timespec ts;
782 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
783 use_rt_clock = 1;
786 #endif
789 static int64_t get_clock(void)
791 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
792 || defined(__DragonFly__)
793 if (use_rt_clock) {
794 struct timespec ts;
795 clock_gettime(CLOCK_MONOTONIC, &ts);
796 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
797 } else
798 #endif
800 /* XXX: using gettimeofday leads to problems if the date
801 changes, so it should be avoided. */
802 struct timeval tv;
803 gettimeofday(&tv, NULL);
804 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
807 #endif
809 /* Return the virtual CPU time, based on the instruction counter. */
810 static int64_t cpu_get_icount(void)
812 int64_t icount;
813 CPUState *env = cpu_single_env;;
814 icount = qemu_icount;
815 if (env) {
816 if (!can_do_io(env))
817 fprintf(stderr, "Bad clock read\n");
818 icount -= (env->icount_decr.u16.low + env->icount_extra);
820 return qemu_icount_bias + (icount << icount_time_shift);
823 /***********************************************************/
824 /* guest cycle counter */
826 static int64_t cpu_ticks_prev;
827 static int64_t cpu_ticks_offset;
828 static int64_t cpu_clock_offset;
829 static int cpu_ticks_enabled;
831 /* return the host CPU cycle counter and handle stop/restart */
832 int64_t cpu_get_ticks(void)
834 if (use_icount) {
835 return cpu_get_icount();
837 if (!cpu_ticks_enabled) {
838 return cpu_ticks_offset;
839 } else {
840 int64_t ticks;
841 ticks = cpu_get_real_ticks();
842 if (cpu_ticks_prev > ticks) {
843 /* Note: non increasing ticks may happen if the host uses
844 software suspend */
845 cpu_ticks_offset += cpu_ticks_prev - ticks;
847 cpu_ticks_prev = ticks;
848 return ticks + cpu_ticks_offset;
852 /* return the host CPU monotonic timer and handle stop/restart */
853 static int64_t cpu_get_clock(void)
855 int64_t ti;
856 if (!cpu_ticks_enabled) {
857 return cpu_clock_offset;
858 } else {
859 ti = get_clock();
860 return ti + cpu_clock_offset;
864 /* enable cpu_get_ticks() */
865 void cpu_enable_ticks(void)
867 if (!cpu_ticks_enabled) {
868 cpu_ticks_offset -= cpu_get_real_ticks();
869 cpu_clock_offset -= get_clock();
870 cpu_ticks_enabled = 1;
874 /* disable cpu_get_ticks() : the clock is stopped. You must not call
875 cpu_get_ticks() after that. */
876 void cpu_disable_ticks(void)
878 if (cpu_ticks_enabled) {
879 cpu_ticks_offset = cpu_get_ticks();
880 cpu_clock_offset = cpu_get_clock();
881 cpu_ticks_enabled = 0;
885 /***********************************************************/
886 /* timers */
888 #define QEMU_TIMER_REALTIME 0
889 #define QEMU_TIMER_VIRTUAL 1
891 struct QEMUClock {
892 int type;
893 /* XXX: add frequency */
896 struct QEMUTimer {
897 QEMUClock *clock;
898 int64_t expire_time;
899 QEMUTimerCB *cb;
900 void *opaque;
901 struct QEMUTimer *next;
904 struct qemu_alarm_timer {
905 char const *name;
906 unsigned int flags;
908 int (*start)(struct qemu_alarm_timer *t);
909 void (*stop)(struct qemu_alarm_timer *t);
910 void (*rearm)(struct qemu_alarm_timer *t);
911 void *priv;
914 #define ALARM_FLAG_DYNTICKS 0x1
915 #define ALARM_FLAG_EXPIRED 0x2
917 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
919 return t && (t->flags & ALARM_FLAG_DYNTICKS);
922 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
924 if (!alarm_has_dynticks(t))
925 return;
927 t->rearm(t);
930 /* TODO: MIN_TIMER_REARM_US should be optimized */
931 #define MIN_TIMER_REARM_US 250
933 static struct qemu_alarm_timer *alarm_timer;
935 #ifdef _WIN32
937 struct qemu_alarm_win32 {
938 MMRESULT timerId;
939 unsigned int period;
940 } alarm_win32_data = {0, -1};
942 static int win32_start_timer(struct qemu_alarm_timer *t);
943 static void win32_stop_timer(struct qemu_alarm_timer *t);
944 static void win32_rearm_timer(struct qemu_alarm_timer *t);
946 #else
948 static int unix_start_timer(struct qemu_alarm_timer *t);
949 static void unix_stop_timer(struct qemu_alarm_timer *t);
951 #ifdef __linux__
953 static int dynticks_start_timer(struct qemu_alarm_timer *t);
954 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
955 static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
957 static int hpet_start_timer(struct qemu_alarm_timer *t);
958 static void hpet_stop_timer(struct qemu_alarm_timer *t);
960 static int rtc_start_timer(struct qemu_alarm_timer *t);
961 static void rtc_stop_timer(struct qemu_alarm_timer *t);
963 #endif /* __linux__ */
965 #endif /* _WIN32 */
967 /* Correlation between real and virtual time is always going to be
968 fairly approximate, so ignore small variation.
969 When the guest is idle real and virtual time will be aligned in
970 the IO wait loop. */
971 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
973 static void icount_adjust(void)
975 int64_t cur_time;
976 int64_t cur_icount;
977 int64_t delta;
978 static int64_t last_delta;
979 /* If the VM is not running, then do nothing. */
980 if (!vm_running)
981 return;
983 cur_time = cpu_get_clock();
984 cur_icount = qemu_get_clock(vm_clock);
985 delta = cur_icount - cur_time;
986 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
987 if (delta > 0
988 && last_delta + ICOUNT_WOBBLE < delta * 2
989 && icount_time_shift > 0) {
990 /* The guest is getting too far ahead. Slow time down. */
991 icount_time_shift--;
993 if (delta < 0
994 && last_delta - ICOUNT_WOBBLE > delta * 2
995 && icount_time_shift < MAX_ICOUNT_SHIFT) {
996 /* The guest is getting too far behind. Speed time up. */
997 icount_time_shift++;
999 last_delta = delta;
1000 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
1003 static void icount_adjust_rt(void * opaque)
1005 qemu_mod_timer(icount_rt_timer,
1006 qemu_get_clock(rt_clock) + 1000);
1007 icount_adjust();
1010 static void icount_adjust_vm(void * opaque)
1012 qemu_mod_timer(icount_vm_timer,
1013 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1014 icount_adjust();
1017 static void init_icount_adjust(void)
1019 /* Have both realtime and virtual time triggers for speed adjustment.
1020 The realtime trigger catches emulated time passing too slowly,
1021 the virtual time trigger catches emulated time passing too fast.
1022 Realtime triggers occur even when idle, so use them less frequently
1023 than VM triggers. */
1024 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1025 qemu_mod_timer(icount_rt_timer,
1026 qemu_get_clock(rt_clock) + 1000);
1027 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1028 qemu_mod_timer(icount_vm_timer,
1029 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1032 static struct qemu_alarm_timer alarm_timers[] = {
1033 #ifndef _WIN32
1034 #ifdef __linux__
1035 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1036 dynticks_stop_timer, dynticks_rearm_timer, NULL},
1037 /* HPET - if available - is preferred */
1038 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
1039 /* ...otherwise try RTC */
1040 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
1041 #endif
1042 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
1043 #else
1044 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1045 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1046 {"win32", 0, win32_start_timer,
1047 win32_stop_timer, NULL, &alarm_win32_data},
1048 #endif
1049 {NULL, }
1052 static void show_available_alarms(void)
1054 int i;
1056 printf("Available alarm timers, in order of precedence:\n");
1057 for (i = 0; alarm_timers[i].name; i++)
1058 printf("%s\n", alarm_timers[i].name);
1061 static void configure_alarms(char const *opt)
1063 int i;
1064 int cur = 0;
1065 int count = ARRAY_SIZE(alarm_timers) - 1;
1066 char *arg;
1067 char *name;
1068 struct qemu_alarm_timer tmp;
1070 if (!strcmp(opt, "?")) {
1071 show_available_alarms();
1072 exit(0);
1075 arg = strdup(opt);
1077 /* Reorder the array */
1078 name = strtok(arg, ",");
1079 while (name) {
1080 for (i = 0; i < count && alarm_timers[i].name; i++) {
1081 if (!strcmp(alarm_timers[i].name, name))
1082 break;
1085 if (i == count) {
1086 fprintf(stderr, "Unknown clock %s\n", name);
1087 goto next;
1090 if (i < cur)
1091 /* Ignore */
1092 goto next;
1094 /* Swap */
1095 tmp = alarm_timers[i];
1096 alarm_timers[i] = alarm_timers[cur];
1097 alarm_timers[cur] = tmp;
1099 cur++;
1100 next:
1101 name = strtok(NULL, ",");
1104 free(arg);
1106 if (cur) {
1107 /* Disable remaining timers */
1108 for (i = cur; i < count; i++)
1109 alarm_timers[i].name = NULL;
1110 } else {
1111 show_available_alarms();
1112 exit(1);
1116 QEMUClock *rt_clock;
1117 QEMUClock *vm_clock;
1119 static QEMUTimer *active_timers[2];
1121 static QEMUClock *qemu_new_clock(int type)
1123 QEMUClock *clock;
1124 clock = qemu_mallocz(sizeof(QEMUClock));
1125 clock->type = type;
1126 return clock;
1129 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1131 QEMUTimer *ts;
1133 ts = qemu_mallocz(sizeof(QEMUTimer));
1134 ts->clock = clock;
1135 ts->cb = cb;
1136 ts->opaque = opaque;
1137 return ts;
1140 void qemu_free_timer(QEMUTimer *ts)
1142 qemu_free(ts);
1145 /* stop a timer, but do not dealloc it */
1146 void qemu_del_timer(QEMUTimer *ts)
1148 QEMUTimer **pt, *t;
1150 /* NOTE: this code must be signal safe because
1151 qemu_timer_expired() can be called from a signal. */
1152 pt = &active_timers[ts->clock->type];
1153 for(;;) {
1154 t = *pt;
1155 if (!t)
1156 break;
1157 if (t == ts) {
1158 *pt = t->next;
1159 break;
1161 pt = &t->next;
1165 /* modify the current timer so that it will be fired when current_time
1166 >= expire_time. The corresponding callback will be called. */
1167 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1169 QEMUTimer **pt, *t;
1171 qemu_del_timer(ts);
1173 /* add the timer in the sorted list */
1174 /* NOTE: this code must be signal safe because
1175 qemu_timer_expired() can be called from a signal. */
1176 pt = &active_timers[ts->clock->type];
1177 for(;;) {
1178 t = *pt;
1179 if (!t)
1180 break;
1181 if (t->expire_time > expire_time)
1182 break;
1183 pt = &t->next;
1185 ts->expire_time = expire_time;
1186 ts->next = *pt;
1187 *pt = ts;
1189 /* Rearm if necessary */
1190 if (pt == &active_timers[ts->clock->type]) {
1191 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1192 qemu_rearm_alarm_timer(alarm_timer);
1194 /* Interrupt execution to force deadline recalculation. */
1195 if (use_icount)
1196 qemu_notify_event();
1200 int qemu_timer_pending(QEMUTimer *ts)
1202 QEMUTimer *t;
1203 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1204 if (t == ts)
1205 return 1;
1207 return 0;
1210 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1212 if (!timer_head)
1213 return 0;
1214 return (timer_head->expire_time <= current_time);
1217 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1219 QEMUTimer *ts;
1221 for(;;) {
1222 ts = *ptimer_head;
1223 if (!ts || ts->expire_time > current_time)
1224 break;
1225 /* remove timer from the list before calling the callback */
1226 *ptimer_head = ts->next;
1227 ts->next = NULL;
1229 /* run the callback (the timer list can be modified) */
1230 ts->cb(ts->opaque);
1234 int64_t qemu_get_clock(QEMUClock *clock)
1236 switch(clock->type) {
1237 case QEMU_TIMER_REALTIME:
1238 return get_clock() / 1000000;
1239 default:
1240 case QEMU_TIMER_VIRTUAL:
1241 if (use_icount) {
1242 return cpu_get_icount();
1243 } else {
1244 return cpu_get_clock();
1249 static void init_timers(void)
1251 init_get_clock();
1252 ticks_per_sec = QEMU_TIMER_BASE;
1253 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1254 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1257 /* save a timer */
1258 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1260 uint64_t expire_time;
1262 if (qemu_timer_pending(ts)) {
1263 expire_time = ts->expire_time;
1264 } else {
1265 expire_time = -1;
1267 qemu_put_be64(f, expire_time);
1270 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1272 uint64_t expire_time;
1274 expire_time = qemu_get_be64(f);
1275 if (expire_time != -1) {
1276 qemu_mod_timer(ts, expire_time);
1277 } else {
1278 qemu_del_timer(ts);
1282 static void timer_save(QEMUFile *f, void *opaque)
1284 if (cpu_ticks_enabled) {
1285 hw_error("cannot save state if virtual timers are running");
1287 qemu_put_be64(f, cpu_ticks_offset);
1288 qemu_put_be64(f, ticks_per_sec);
1289 qemu_put_be64(f, cpu_clock_offset);
1292 static int timer_load(QEMUFile *f, void *opaque, int version_id)
1294 if (version_id != 1 && version_id != 2)
1295 return -EINVAL;
1296 if (cpu_ticks_enabled) {
1297 return -EINVAL;
1299 cpu_ticks_offset=qemu_get_be64(f);
1300 ticks_per_sec=qemu_get_be64(f);
1301 if (version_id == 2) {
1302 cpu_clock_offset=qemu_get_be64(f);
1304 return 0;
1307 static void qemu_event_increment(void);
1309 #ifdef _WIN32
1310 static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1311 DWORD_PTR dwUser, DWORD_PTR dw1,
1312 DWORD_PTR dw2)
1313 #else
1314 static void host_alarm_handler(int host_signum)
1315 #endif
1317 #if 0
1318 #define DISP_FREQ 1000
1320 static int64_t delta_min = INT64_MAX;
1321 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1322 static int count;
1323 ti = qemu_get_clock(vm_clock);
1324 if (last_clock != 0) {
1325 delta = ti - last_clock;
1326 if (delta < delta_min)
1327 delta_min = delta;
1328 if (delta > delta_max)
1329 delta_max = delta;
1330 delta_cum += delta;
1331 if (++count == DISP_FREQ) {
1332 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1333 muldiv64(delta_min, 1000000, ticks_per_sec),
1334 muldiv64(delta_max, 1000000, ticks_per_sec),
1335 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1336 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1337 count = 0;
1338 delta_min = INT64_MAX;
1339 delta_max = 0;
1340 delta_cum = 0;
1343 last_clock = ti;
1345 #endif
1346 if (alarm_has_dynticks(alarm_timer) ||
1347 (!use_icount &&
1348 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1349 qemu_get_clock(vm_clock))) ||
1350 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1351 qemu_get_clock(rt_clock))) {
1352 qemu_event_increment();
1353 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1355 #ifndef CONFIG_IOTHREAD
1356 if (next_cpu) {
1357 /* stop the currently executing cpu because a timer occured */
1358 cpu_exit(next_cpu);
1359 #ifdef CONFIG_KQEMU
1360 if (next_cpu->kqemu_enabled) {
1361 kqemu_cpu_interrupt(next_cpu);
1363 #endif
1365 #endif
1366 timer_alarm_pending = 1;
1367 qemu_notify_event();
1371 static int64_t qemu_next_deadline(void)
1373 int64_t delta;
1375 if (active_timers[QEMU_TIMER_VIRTUAL]) {
1376 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1377 qemu_get_clock(vm_clock);
1378 } else {
1379 /* To avoid problems with overflow limit this to 2^32. */
1380 delta = INT32_MAX;
1383 if (delta < 0)
1384 delta = 0;
1386 return delta;
1389 #if defined(__linux__) || defined(_WIN32)
1390 static uint64_t qemu_next_deadline_dyntick(void)
1392 int64_t delta;
1393 int64_t rtdelta;
1395 if (use_icount)
1396 delta = INT32_MAX;
1397 else
1398 delta = (qemu_next_deadline() + 999) / 1000;
1400 if (active_timers[QEMU_TIMER_REALTIME]) {
1401 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1402 qemu_get_clock(rt_clock))*1000;
1403 if (rtdelta < delta)
1404 delta = rtdelta;
1407 if (delta < MIN_TIMER_REARM_US)
1408 delta = MIN_TIMER_REARM_US;
1410 return delta;
1412 #endif
1414 #ifndef _WIN32
1416 /* Sets a specific flag */
1417 static int fcntl_setfl(int fd, int flag)
1419 int flags;
1421 flags = fcntl(fd, F_GETFL);
1422 if (flags == -1)
1423 return -errno;
1425 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1426 return -errno;
1428 return 0;
1431 #if defined(__linux__)
1433 #define RTC_FREQ 1024
1435 static void enable_sigio_timer(int fd)
1437 struct sigaction act;
1439 /* timer signal */
1440 sigfillset(&act.sa_mask);
1441 act.sa_flags = 0;
1442 act.sa_handler = host_alarm_handler;
1444 sigaction(SIGIO, &act, NULL);
1445 fcntl_setfl(fd, O_ASYNC);
1446 fcntl(fd, F_SETOWN, getpid());
1449 static int hpet_start_timer(struct qemu_alarm_timer *t)
1451 struct hpet_info info;
1452 int r, fd;
1454 fd = open("/dev/hpet", O_RDONLY);
1455 if (fd < 0)
1456 return -1;
1458 /* Set frequency */
1459 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1460 if (r < 0) {
1461 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1462 "error, but for better emulation accuracy type:\n"
1463 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1464 goto fail;
1467 /* Check capabilities */
1468 r = ioctl(fd, HPET_INFO, &info);
1469 if (r < 0)
1470 goto fail;
1472 /* Enable periodic mode */
1473 r = ioctl(fd, HPET_EPI, 0);
1474 if (info.hi_flags && (r < 0))
1475 goto fail;
1477 /* Enable interrupt */
1478 r = ioctl(fd, HPET_IE_ON, 0);
1479 if (r < 0)
1480 goto fail;
1482 enable_sigio_timer(fd);
1483 t->priv = (void *)(long)fd;
1485 return 0;
1486 fail:
1487 close(fd);
1488 return -1;
1491 static void hpet_stop_timer(struct qemu_alarm_timer *t)
1493 int fd = (long)t->priv;
1495 close(fd);
1498 static int rtc_start_timer(struct qemu_alarm_timer *t)
1500 int rtc_fd;
1501 unsigned long current_rtc_freq = 0;
1503 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1504 if (rtc_fd < 0)
1505 return -1;
1506 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1507 if (current_rtc_freq != RTC_FREQ &&
1508 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1509 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1510 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1511 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1512 goto fail;
1514 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1515 fail:
1516 close(rtc_fd);
1517 return -1;
1520 enable_sigio_timer(rtc_fd);
1522 t->priv = (void *)(long)rtc_fd;
1524 return 0;
1527 static void rtc_stop_timer(struct qemu_alarm_timer *t)
1529 int rtc_fd = (long)t->priv;
1531 close(rtc_fd);
1534 static int dynticks_start_timer(struct qemu_alarm_timer *t)
1536 struct sigevent ev;
1537 timer_t host_timer;
1538 struct sigaction act;
1540 sigfillset(&act.sa_mask);
1541 act.sa_flags = 0;
1542 act.sa_handler = host_alarm_handler;
1544 sigaction(SIGALRM, &act, NULL);
1547 * Initialize ev struct to 0 to avoid valgrind complaining
1548 * about uninitialized data in timer_create call
1550 memset(&ev, 0, sizeof(ev));
1551 ev.sigev_value.sival_int = 0;
1552 ev.sigev_notify = SIGEV_SIGNAL;
1553 ev.sigev_signo = SIGALRM;
1555 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1556 perror("timer_create");
1558 /* disable dynticks */
1559 fprintf(stderr, "Dynamic Ticks disabled\n");
1561 return -1;
1564 t->priv = (void *)(long)host_timer;
1566 return 0;
1569 static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1571 timer_t host_timer = (timer_t)(long)t->priv;
1573 timer_delete(host_timer);
1576 static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1578 timer_t host_timer = (timer_t)(long)t->priv;
1579 struct itimerspec timeout;
1580 int64_t nearest_delta_us = INT64_MAX;
1581 int64_t current_us;
1583 if (!active_timers[QEMU_TIMER_REALTIME] &&
1584 !active_timers[QEMU_TIMER_VIRTUAL])
1585 return;
1587 nearest_delta_us = qemu_next_deadline_dyntick();
1589 /* check whether a timer is already running */
1590 if (timer_gettime(host_timer, &timeout)) {
1591 perror("gettime");
1592 fprintf(stderr, "Internal timer error: aborting\n");
1593 exit(1);
1595 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1596 if (current_us && current_us <= nearest_delta_us)
1597 return;
1599 timeout.it_interval.tv_sec = 0;
1600 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1601 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1602 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1603 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1604 perror("settime");
1605 fprintf(stderr, "Internal timer error: aborting\n");
1606 exit(1);
1610 #endif /* defined(__linux__) */
1612 static int unix_start_timer(struct qemu_alarm_timer *t)
1614 struct sigaction act;
1615 struct itimerval itv;
1616 int err;
1618 /* timer signal */
1619 sigfillset(&act.sa_mask);
1620 act.sa_flags = 0;
1621 act.sa_handler = host_alarm_handler;
1623 sigaction(SIGALRM, &act, NULL);
1625 itv.it_interval.tv_sec = 0;
1626 /* for i386 kernel 2.6 to get 1 ms */
1627 itv.it_interval.tv_usec = 999;
1628 itv.it_value.tv_sec = 0;
1629 itv.it_value.tv_usec = 10 * 1000;
1631 err = setitimer(ITIMER_REAL, &itv, NULL);
1632 if (err)
1633 return -1;
1635 return 0;
1638 static void unix_stop_timer(struct qemu_alarm_timer *t)
1640 struct itimerval itv;
1642 memset(&itv, 0, sizeof(itv));
1643 setitimer(ITIMER_REAL, &itv, NULL);
1646 #endif /* !defined(_WIN32) */
1649 #ifdef _WIN32
1651 static int win32_start_timer(struct qemu_alarm_timer *t)
1653 TIMECAPS tc;
1654 struct qemu_alarm_win32 *data = t->priv;
1655 UINT flags;
1657 memset(&tc, 0, sizeof(tc));
1658 timeGetDevCaps(&tc, sizeof(tc));
1660 if (data->period < tc.wPeriodMin)
1661 data->period = tc.wPeriodMin;
1663 timeBeginPeriod(data->period);
1665 flags = TIME_CALLBACK_FUNCTION;
1666 if (alarm_has_dynticks(t))
1667 flags |= TIME_ONESHOT;
1668 else
1669 flags |= TIME_PERIODIC;
1671 data->timerId = timeSetEvent(1, // interval (ms)
1672 data->period, // resolution
1673 host_alarm_handler, // function
1674 (DWORD)t, // parameter
1675 flags);
1677 if (!data->timerId) {
1678 perror("Failed to initialize win32 alarm timer");
1679 timeEndPeriod(data->period);
1680 return -1;
1683 return 0;
1686 static void win32_stop_timer(struct qemu_alarm_timer *t)
1688 struct qemu_alarm_win32 *data = t->priv;
1690 timeKillEvent(data->timerId);
1691 timeEndPeriod(data->period);
1694 static void win32_rearm_timer(struct qemu_alarm_timer *t)
1696 struct qemu_alarm_win32 *data = t->priv;
1697 uint64_t nearest_delta_us;
1699 if (!active_timers[QEMU_TIMER_REALTIME] &&
1700 !active_timers[QEMU_TIMER_VIRTUAL])
1701 return;
1703 nearest_delta_us = qemu_next_deadline_dyntick();
1704 nearest_delta_us /= 1000;
1706 timeKillEvent(data->timerId);
1708 data->timerId = timeSetEvent(1,
1709 data->period,
1710 host_alarm_handler,
1711 (DWORD)t,
1712 TIME_ONESHOT | TIME_PERIODIC);
1714 if (!data->timerId) {
1715 perror("Failed to re-arm win32 alarm timer");
1717 timeEndPeriod(data->period);
1718 exit(1);
1722 #endif /* _WIN32 */
1724 static int init_timer_alarm(void)
1726 struct qemu_alarm_timer *t = NULL;
1727 int i, err = -1;
1729 for (i = 0; alarm_timers[i].name; i++) {
1730 t = &alarm_timers[i];
1732 err = t->start(t);
1733 if (!err)
1734 break;
1737 if (err) {
1738 err = -ENOENT;
1739 goto fail;
1742 alarm_timer = t;
1744 return 0;
1746 fail:
1747 return err;
1750 static void quit_timers(void)
1752 alarm_timer->stop(alarm_timer);
1753 alarm_timer = NULL;
1756 /***********************************************************/
1757 /* host time/date access */
1758 void qemu_get_timedate(struct tm *tm, int offset)
1760 time_t ti;
1761 struct tm *ret;
1763 time(&ti);
1764 ti += offset;
1765 if (rtc_date_offset == -1) {
1766 if (rtc_utc)
1767 ret = gmtime(&ti);
1768 else
1769 ret = localtime(&ti);
1770 } else {
1771 ti -= rtc_date_offset;
1772 ret = gmtime(&ti);
1775 memcpy(tm, ret, sizeof(struct tm));
1778 int qemu_timedate_diff(struct tm *tm)
1780 time_t seconds;
1782 if (rtc_date_offset == -1)
1783 if (rtc_utc)
1784 seconds = mktimegm(tm);
1785 else
1786 seconds = mktime(tm);
1787 else
1788 seconds = mktimegm(tm) + rtc_date_offset;
1790 return seconds - time(NULL);
1793 #ifdef _WIN32
1794 static void socket_cleanup(void)
1796 WSACleanup();
1799 static int socket_init(void)
1801 WSADATA Data;
1802 int ret, err;
1804 ret = WSAStartup(MAKEWORD(2,2), &Data);
1805 if (ret != 0) {
1806 err = WSAGetLastError();
1807 fprintf(stderr, "WSAStartup: %d\n", err);
1808 return -1;
1810 atexit(socket_cleanup);
1811 return 0;
1813 #endif
1815 int get_param_value(char *buf, int buf_size,
1816 const char *tag, const char *str)
1818 const char *p;
1819 char option[128];
1821 p = str;
1822 for(;;) {
1823 p = get_opt_name(option, sizeof(option), p, '=');
1824 if (*p != '=')
1825 break;
1826 p++;
1827 if (!strcmp(tag, option)) {
1828 (void)get_opt_value(buf, buf_size, p);
1829 return strlen(buf);
1830 } else {
1831 p = get_opt_value(NULL, 0, p);
1833 if (*p != ',')
1834 break;
1835 p++;
1837 return 0;
1840 int check_params(char *buf, int buf_size,
1841 const char * const *params, const char *str)
1843 const char *p;
1844 int i;
1846 p = str;
1847 while (*p != '\0') {
1848 p = get_opt_name(buf, buf_size, p, '=');
1849 if (*p != '=') {
1850 return -1;
1852 p++;
1853 for (i = 0; params[i] != NULL; i++) {
1854 if (!strcmp(params[i], buf)) {
1855 break;
1858 if (params[i] == NULL) {
1859 return -1;
1861 p = get_opt_value(NULL, 0, p);
1862 if (*p != ',') {
1863 break;
1865 p++;
1867 return 0;
1870 /***********************************************************/
1871 /* Bluetooth support */
1872 static int nb_hcis;
1873 static int cur_hci;
1874 static struct HCIInfo *hci_table[MAX_NICS];
1876 static struct bt_vlan_s {
1877 struct bt_scatternet_s net;
1878 int id;
1879 struct bt_vlan_s *next;
1880 } *first_bt_vlan;
1882 /* find or alloc a new bluetooth "VLAN" */
1883 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1885 struct bt_vlan_s **pvlan, *vlan;
1886 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1887 if (vlan->id == id)
1888 return &vlan->net;
1890 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1891 vlan->id = id;
1892 pvlan = &first_bt_vlan;
1893 while (*pvlan != NULL)
1894 pvlan = &(*pvlan)->next;
1895 *pvlan = vlan;
1896 return &vlan->net;
1899 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1903 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1905 return -ENOTSUP;
1908 static struct HCIInfo null_hci = {
1909 .cmd_send = null_hci_send,
1910 .sco_send = null_hci_send,
1911 .acl_send = null_hci_send,
1912 .bdaddr_set = null_hci_addr_set,
1915 struct HCIInfo *qemu_next_hci(void)
1917 if (cur_hci == nb_hcis)
1918 return &null_hci;
1920 return hci_table[cur_hci++];
1923 static struct HCIInfo *hci_init(const char *str)
1925 char *endp;
1926 struct bt_scatternet_s *vlan = 0;
1928 if (!strcmp(str, "null"))
1929 /* null */
1930 return &null_hci;
1931 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1932 /* host[:hciN] */
1933 return bt_host_hci(str[4] ? str + 5 : "hci0");
1934 else if (!strncmp(str, "hci", 3)) {
1935 /* hci[,vlan=n] */
1936 if (str[3]) {
1937 if (!strncmp(str + 3, ",vlan=", 6)) {
1938 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1939 if (*endp)
1940 vlan = 0;
1942 } else
1943 vlan = qemu_find_bt_vlan(0);
1944 if (vlan)
1945 return bt_new_hci(vlan);
1948 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1950 return 0;
1953 static int bt_hci_parse(const char *str)
1955 struct HCIInfo *hci;
1956 bdaddr_t bdaddr;
1958 if (nb_hcis >= MAX_NICS) {
1959 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1960 return -1;
1963 hci = hci_init(str);
1964 if (!hci)
1965 return -1;
1967 bdaddr.b[0] = 0x52;
1968 bdaddr.b[1] = 0x54;
1969 bdaddr.b[2] = 0x00;
1970 bdaddr.b[3] = 0x12;
1971 bdaddr.b[4] = 0x34;
1972 bdaddr.b[5] = 0x56 + nb_hcis;
1973 hci->bdaddr_set(hci, bdaddr.b);
1975 hci_table[nb_hcis++] = hci;
1977 return 0;
1980 static void bt_vhci_add(int vlan_id)
1982 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1984 if (!vlan->slave)
1985 fprintf(stderr, "qemu: warning: adding a VHCI to "
1986 "an empty scatternet %i\n", vlan_id);
1988 bt_vhci_init(bt_new_hci(vlan));
1991 static struct bt_device_s *bt_device_add(const char *opt)
1993 struct bt_scatternet_s *vlan;
1994 int vlan_id = 0;
1995 char *endp = strstr(opt, ",vlan=");
1996 int len = (endp ? endp - opt : strlen(opt)) + 1;
1997 char devname[10];
1999 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2001 if (endp) {
2002 vlan_id = strtol(endp + 6, &endp, 0);
2003 if (*endp) {
2004 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2005 return 0;
2009 vlan = qemu_find_bt_vlan(vlan_id);
2011 if (!vlan->slave)
2012 fprintf(stderr, "qemu: warning: adding a slave device to "
2013 "an empty scatternet %i\n", vlan_id);
2015 if (!strcmp(devname, "keyboard"))
2016 return bt_keyboard_init(vlan);
2018 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2019 return 0;
2022 static int bt_parse(const char *opt)
2024 const char *endp, *p;
2025 int vlan;
2027 if (strstart(opt, "hci", &endp)) {
2028 if (!*endp || *endp == ',') {
2029 if (*endp)
2030 if (!strstart(endp, ",vlan=", 0))
2031 opt = endp + 1;
2033 return bt_hci_parse(opt);
2035 } else if (strstart(opt, "vhci", &endp)) {
2036 if (!*endp || *endp == ',') {
2037 if (*endp) {
2038 if (strstart(endp, ",vlan=", &p)) {
2039 vlan = strtol(p, (char **) &endp, 0);
2040 if (*endp) {
2041 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2042 return 1;
2044 } else {
2045 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2046 return 1;
2048 } else
2049 vlan = 0;
2051 bt_vhci_add(vlan);
2052 return 0;
2054 } else if (strstart(opt, "device:", &endp))
2055 return !bt_device_add(endp);
2057 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2058 return 1;
2061 /***********************************************************/
2062 /* QEMU Block devices */
2064 #define HD_ALIAS "index=%d,media=disk"
2065 #define CDROM_ALIAS "index=2,media=cdrom"
2066 #define FD_ALIAS "index=%d,if=floppy"
2067 #define PFLASH_ALIAS "if=pflash"
2068 #define MTD_ALIAS "if=mtd"
2069 #define SD_ALIAS "index=0,if=sd"
2071 static int drive_opt_get_free_idx(void)
2073 int index;
2075 for (index = 0; index < MAX_DRIVES; index++)
2076 if (!drives_opt[index].used) {
2077 drives_opt[index].used = 1;
2078 return index;
2081 return -1;
2084 static int drive_get_free_idx(void)
2086 int index;
2088 for (index = 0; index < MAX_DRIVES; index++)
2089 if (!drives_table[index].used) {
2090 drives_table[index].used = 1;
2091 return index;
2094 return -1;
2097 int drive_add(const char *file, const char *fmt, ...)
2099 va_list ap;
2100 int index = drive_opt_get_free_idx();
2102 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
2103 fprintf(stderr, "qemu: too many drives\n");
2104 return -1;
2107 drives_opt[index].file = file;
2108 va_start(ap, fmt);
2109 vsnprintf(drives_opt[index].opt,
2110 sizeof(drives_opt[0].opt), fmt, ap);
2111 va_end(ap);
2113 nb_drives_opt++;
2114 return index;
2117 void drive_remove(int index)
2119 drives_opt[index].used = 0;
2120 nb_drives_opt--;
2123 int drive_get_index(BlockInterfaceType type, int bus, int unit)
2125 int index;
2127 /* seek interface, bus and unit */
2129 for (index = 0; index < MAX_DRIVES; index++)
2130 if (drives_table[index].type == type &&
2131 drives_table[index].bus == bus &&
2132 drives_table[index].unit == unit &&
2133 drives_table[index].used)
2134 return index;
2136 return -1;
2139 int drive_get_max_bus(BlockInterfaceType type)
2141 int max_bus;
2142 int index;
2144 max_bus = -1;
2145 for (index = 0; index < nb_drives; index++) {
2146 if(drives_table[index].type == type &&
2147 drives_table[index].bus > max_bus)
2148 max_bus = drives_table[index].bus;
2150 return max_bus;
2153 const char *drive_get_serial(BlockDriverState *bdrv)
2155 int index;
2157 for (index = 0; index < nb_drives; index++)
2158 if (drives_table[index].bdrv == bdrv)
2159 return drives_table[index].serial;
2161 return "\0";
2164 BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2166 int index;
2168 for (index = 0; index < nb_drives; index++)
2169 if (drives_table[index].bdrv == bdrv)
2170 return drives_table[index].onerror;
2172 return BLOCK_ERR_STOP_ENOSPC;
2175 static void bdrv_format_print(void *opaque, const char *name)
2177 fprintf(stderr, " %s", name);
2180 void drive_uninit(BlockDriverState *bdrv)
2182 int i;
2184 for (i = 0; i < MAX_DRIVES; i++)
2185 if (drives_table[i].bdrv == bdrv) {
2186 drives_table[i].bdrv = NULL;
2187 drives_table[i].used = 0;
2188 drive_remove(drives_table[i].drive_opt_idx);
2189 nb_drives--;
2190 break;
2194 int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
2196 char buf[128];
2197 char file[1024];
2198 char devname[128];
2199 char serial[21];
2200 const char *mediastr = "";
2201 BlockInterfaceType type;
2202 enum { MEDIA_DISK, MEDIA_CDROM } media;
2203 int bus_id, unit_id;
2204 int cyls, heads, secs, translation;
2205 BlockDriverState *bdrv;
2206 BlockDriver *drv = NULL;
2207 QEMUMachine *machine = opaque;
2208 int max_devs;
2209 int index;
2210 int cache;
2211 int bdrv_flags, onerror;
2212 int drives_table_idx;
2213 char *str = arg->opt;
2214 static const char * const params[] = { "bus", "unit", "if", "index",
2215 "cyls", "heads", "secs", "trans",
2216 "media", "snapshot", "file",
2217 "cache", "format", "serial", "werror",
2218 NULL };
2220 if (check_params(buf, sizeof(buf), params, str) < 0) {
2221 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2222 buf, str);
2223 return -1;
2226 file[0] = 0;
2227 cyls = heads = secs = 0;
2228 bus_id = 0;
2229 unit_id = -1;
2230 translation = BIOS_ATA_TRANSLATION_AUTO;
2231 index = -1;
2232 cache = 3;
2234 if (machine->use_scsi) {
2235 type = IF_SCSI;
2236 max_devs = MAX_SCSI_DEVS;
2237 pstrcpy(devname, sizeof(devname), "scsi");
2238 } else {
2239 type = IF_IDE;
2240 max_devs = MAX_IDE_DEVS;
2241 pstrcpy(devname, sizeof(devname), "ide");
2243 media = MEDIA_DISK;
2245 /* extract parameters */
2247 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2248 bus_id = strtol(buf, NULL, 0);
2249 if (bus_id < 0) {
2250 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2251 return -1;
2255 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2256 unit_id = strtol(buf, NULL, 0);
2257 if (unit_id < 0) {
2258 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2259 return -1;
2263 if (get_param_value(buf, sizeof(buf), "if", str)) {
2264 pstrcpy(devname, sizeof(devname), buf);
2265 if (!strcmp(buf, "ide")) {
2266 type = IF_IDE;
2267 max_devs = MAX_IDE_DEVS;
2268 } else if (!strcmp(buf, "scsi")) {
2269 type = IF_SCSI;
2270 max_devs = MAX_SCSI_DEVS;
2271 } else if (!strcmp(buf, "floppy")) {
2272 type = IF_FLOPPY;
2273 max_devs = 0;
2274 } else if (!strcmp(buf, "pflash")) {
2275 type = IF_PFLASH;
2276 max_devs = 0;
2277 } else if (!strcmp(buf, "mtd")) {
2278 type = IF_MTD;
2279 max_devs = 0;
2280 } else if (!strcmp(buf, "sd")) {
2281 type = IF_SD;
2282 max_devs = 0;
2283 } else if (!strcmp(buf, "virtio")) {
2284 type = IF_VIRTIO;
2285 max_devs = 0;
2286 } else if (!strcmp(buf, "xen")) {
2287 type = IF_XEN;
2288 max_devs = 0;
2289 } else {
2290 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2291 return -1;
2295 if (get_param_value(buf, sizeof(buf), "index", str)) {
2296 index = strtol(buf, NULL, 0);
2297 if (index < 0) {
2298 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2299 return -1;
2303 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2304 cyls = strtol(buf, NULL, 0);
2307 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2308 heads = strtol(buf, NULL, 0);
2311 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2312 secs = strtol(buf, NULL, 0);
2315 if (cyls || heads || secs) {
2316 if (cyls < 1 || cyls > 16383) {
2317 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2318 return -1;
2320 if (heads < 1 || heads > 16) {
2321 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2322 return -1;
2324 if (secs < 1 || secs > 63) {
2325 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2326 return -1;
2330 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2331 if (!cyls) {
2332 fprintf(stderr,
2333 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2334 str);
2335 return -1;
2337 if (!strcmp(buf, "none"))
2338 translation = BIOS_ATA_TRANSLATION_NONE;
2339 else if (!strcmp(buf, "lba"))
2340 translation = BIOS_ATA_TRANSLATION_LBA;
2341 else if (!strcmp(buf, "auto"))
2342 translation = BIOS_ATA_TRANSLATION_AUTO;
2343 else {
2344 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2345 return -1;
2349 if (get_param_value(buf, sizeof(buf), "media", str)) {
2350 if (!strcmp(buf, "disk")) {
2351 media = MEDIA_DISK;
2352 } else if (!strcmp(buf, "cdrom")) {
2353 if (cyls || secs || heads) {
2354 fprintf(stderr,
2355 "qemu: '%s' invalid physical CHS format\n", str);
2356 return -1;
2358 media = MEDIA_CDROM;
2359 } else {
2360 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2361 return -1;
2365 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2366 if (!strcmp(buf, "on"))
2367 snapshot = 1;
2368 else if (!strcmp(buf, "off"))
2369 snapshot = 0;
2370 else {
2371 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2372 return -1;
2376 if (get_param_value(buf, sizeof(buf), "cache", str)) {
2377 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2378 cache = 0;
2379 else if (!strcmp(buf, "writethrough"))
2380 cache = 1;
2381 else if (!strcmp(buf, "writeback"))
2382 cache = 2;
2383 else {
2384 fprintf(stderr, "qemu: invalid cache option\n");
2385 return -1;
2389 if (get_param_value(buf, sizeof(buf), "format", str)) {
2390 if (strcmp(buf, "?") == 0) {
2391 fprintf(stderr, "qemu: Supported formats:");
2392 bdrv_iterate_format(bdrv_format_print, NULL);
2393 fprintf(stderr, "\n");
2394 return -1;
2396 drv = bdrv_find_format(buf);
2397 if (!drv) {
2398 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2399 return -1;
2403 if (arg->file == NULL)
2404 get_param_value(file, sizeof(file), "file", str);
2405 else
2406 pstrcpy(file, sizeof(file), arg->file);
2408 if (!get_param_value(serial, sizeof(serial), "serial", str))
2409 memset(serial, 0, sizeof(serial));
2411 onerror = BLOCK_ERR_STOP_ENOSPC;
2412 if (get_param_value(buf, sizeof(serial), "werror", str)) {
2413 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
2414 fprintf(stderr, "werror is no supported by this format\n");
2415 return -1;
2417 if (!strcmp(buf, "ignore"))
2418 onerror = BLOCK_ERR_IGNORE;
2419 else if (!strcmp(buf, "enospc"))
2420 onerror = BLOCK_ERR_STOP_ENOSPC;
2421 else if (!strcmp(buf, "stop"))
2422 onerror = BLOCK_ERR_STOP_ANY;
2423 else if (!strcmp(buf, "report"))
2424 onerror = BLOCK_ERR_REPORT;
2425 else {
2426 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2427 return -1;
2431 /* compute bus and unit according index */
2433 if (index != -1) {
2434 if (bus_id != 0 || unit_id != -1) {
2435 fprintf(stderr,
2436 "qemu: '%s' index cannot be used with bus and unit\n", str);
2437 return -1;
2439 if (max_devs == 0)
2441 unit_id = index;
2442 bus_id = 0;
2443 } else {
2444 unit_id = index % max_devs;
2445 bus_id = index / max_devs;
2449 /* if user doesn't specify a unit_id,
2450 * try to find the first free
2453 if (unit_id == -1) {
2454 unit_id = 0;
2455 while (drive_get_index(type, bus_id, unit_id) != -1) {
2456 unit_id++;
2457 if (max_devs && unit_id >= max_devs) {
2458 unit_id -= max_devs;
2459 bus_id++;
2464 /* check unit id */
2466 if (max_devs && unit_id >= max_devs) {
2467 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2468 str, unit_id, max_devs - 1);
2469 return -1;
2473 * ignore multiple definitions
2476 if (drive_get_index(type, bus_id, unit_id) != -1)
2477 return -2;
2479 /* init */
2481 if (type == IF_IDE || type == IF_SCSI)
2482 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2483 if (max_devs)
2484 snprintf(buf, sizeof(buf), "%s%i%s%i",
2485 devname, bus_id, mediastr, unit_id);
2486 else
2487 snprintf(buf, sizeof(buf), "%s%s%i",
2488 devname, mediastr, unit_id);
2489 bdrv = bdrv_new(buf);
2490 drives_table_idx = drive_get_free_idx();
2491 drives_table[drives_table_idx].bdrv = bdrv;
2492 drives_table[drives_table_idx].type = type;
2493 drives_table[drives_table_idx].bus = bus_id;
2494 drives_table[drives_table_idx].unit = unit_id;
2495 drives_table[drives_table_idx].onerror = onerror;
2496 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
2497 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
2498 nb_drives++;
2500 switch(type) {
2501 case IF_IDE:
2502 case IF_SCSI:
2503 case IF_XEN:
2504 switch(media) {
2505 case MEDIA_DISK:
2506 if (cyls != 0) {
2507 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2508 bdrv_set_translation_hint(bdrv, translation);
2510 break;
2511 case MEDIA_CDROM:
2512 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2513 break;
2515 break;
2516 case IF_SD:
2517 /* FIXME: This isn't really a floppy, but it's a reasonable
2518 approximation. */
2519 case IF_FLOPPY:
2520 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2521 break;
2522 case IF_PFLASH:
2523 case IF_MTD:
2524 case IF_VIRTIO:
2525 break;
2526 case IF_COUNT:
2527 abort();
2529 if (!file[0])
2530 return -2;
2531 bdrv_flags = 0;
2532 if (snapshot) {
2533 bdrv_flags |= BDRV_O_SNAPSHOT;
2534 cache = 2; /* always use write-back with snapshot */
2536 if (cache == 0) /* no caching */
2537 bdrv_flags |= BDRV_O_NOCACHE;
2538 else if (cache == 2) /* write-back */
2539 bdrv_flags |= BDRV_O_CACHE_WB;
2540 else if (cache == 3) /* not specified */
2541 bdrv_flags |= BDRV_O_CACHE_DEF;
2542 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
2543 fprintf(stderr, "qemu: could not open disk image %s\n",
2544 file);
2545 return -1;
2547 if (bdrv_key_required(bdrv))
2548 autostart = 0;
2549 return drives_table_idx;
2552 static void numa_add(const char *optarg)
2554 char option[128];
2555 char *endptr;
2556 unsigned long long value, endvalue;
2557 int nodenr;
2559 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2560 if (!strcmp(option, "node")) {
2561 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2562 nodenr = nb_numa_nodes;
2563 } else {
2564 nodenr = strtoull(option, NULL, 10);
2567 if (get_param_value(option, 128, "mem", optarg) == 0) {
2568 node_mem[nodenr] = 0;
2569 } else {
2570 value = strtoull(option, &endptr, 0);
2571 switch (*endptr) {
2572 case 0: case 'M': case 'm':
2573 value <<= 20;
2574 break;
2575 case 'G': case 'g':
2576 value <<= 30;
2577 break;
2579 node_mem[nodenr] = value;
2581 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2582 node_cpumask[nodenr] = 0;
2583 } else {
2584 value = strtoull(option, &endptr, 10);
2585 if (value >= 64) {
2586 value = 63;
2587 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2588 } else {
2589 if (*endptr == '-') {
2590 endvalue = strtoull(endptr+1, &endptr, 10);
2591 if (endvalue >= 63) {
2592 endvalue = 62;
2593 fprintf(stderr,
2594 "only 63 CPUs in NUMA mode supported.\n");
2596 value = (1 << (endvalue + 1)) - (1 << value);
2597 } else {
2598 value = 1 << value;
2601 node_cpumask[nodenr] = value;
2603 nb_numa_nodes++;
2605 return;
2608 /***********************************************************/
2609 /* USB devices */
2611 static USBPort *used_usb_ports;
2612 static USBPort *free_usb_ports;
2614 /* ??? Maybe change this to register a hub to keep track of the topology. */
2615 void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2616 usb_attachfn attach)
2618 port->opaque = opaque;
2619 port->index = index;
2620 port->attach = attach;
2621 port->next = free_usb_ports;
2622 free_usb_ports = port;
2625 int usb_device_add_dev(USBDevice *dev)
2627 USBPort *port;
2629 /* Find a USB port to add the device to. */
2630 port = free_usb_ports;
2631 if (!port->next) {
2632 USBDevice *hub;
2634 /* Create a new hub and chain it on. */
2635 free_usb_ports = NULL;
2636 port->next = used_usb_ports;
2637 used_usb_ports = port;
2639 hub = usb_hub_init(VM_USB_HUB_SIZE);
2640 usb_attach(port, hub);
2641 port = free_usb_ports;
2644 free_usb_ports = port->next;
2645 port->next = used_usb_ports;
2646 used_usb_ports = port;
2647 usb_attach(port, dev);
2648 return 0;
2651 static void usb_msd_password_cb(void *opaque, int err)
2653 USBDevice *dev = opaque;
2655 if (!err)
2656 usb_device_add_dev(dev);
2657 else
2658 dev->handle_destroy(dev);
2661 static int usb_device_add(const char *devname, int is_hotplug)
2663 const char *p;
2664 USBDevice *dev;
2666 if (!free_usb_ports)
2667 return -1;
2669 if (strstart(devname, "host:", &p)) {
2670 dev = usb_host_device_open(p);
2671 } else if (!strcmp(devname, "mouse")) {
2672 dev = usb_mouse_init();
2673 } else if (!strcmp(devname, "tablet")) {
2674 dev = usb_tablet_init();
2675 } else if (!strcmp(devname, "keyboard")) {
2676 dev = usb_keyboard_init();
2677 } else if (strstart(devname, "disk:", &p)) {
2678 BlockDriverState *bs;
2680 dev = usb_msd_init(p);
2681 if (!dev)
2682 return -1;
2683 bs = usb_msd_get_bdrv(dev);
2684 if (bdrv_key_required(bs)) {
2685 autostart = 0;
2686 if (is_hotplug) {
2687 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2688 dev);
2689 return 0;
2692 } else if (!strcmp(devname, "wacom-tablet")) {
2693 dev = usb_wacom_init();
2694 } else if (strstart(devname, "serial:", &p)) {
2695 dev = usb_serial_init(p);
2696 #ifdef CONFIG_BRLAPI
2697 } else if (!strcmp(devname, "braille")) {
2698 dev = usb_baum_init();
2699 #endif
2700 } else if (strstart(devname, "net:", &p)) {
2701 int nic = nb_nics;
2703 if (net_client_init(NULL, "nic", p) < 0)
2704 return -1;
2705 nd_table[nic].model = "usb";
2706 dev = usb_net_init(&nd_table[nic]);
2707 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2708 dev = usb_bt_init(devname[2] ? hci_init(p) :
2709 bt_new_hci(qemu_find_bt_vlan(0)));
2710 } else {
2711 return -1;
2713 if (!dev)
2714 return -1;
2716 return usb_device_add_dev(dev);
2719 int usb_device_del_addr(int bus_num, int addr)
2721 USBPort *port;
2722 USBPort **lastp;
2723 USBDevice *dev;
2725 if (!used_usb_ports)
2726 return -1;
2728 if (bus_num != 0)
2729 return -1;
2731 lastp = &used_usb_ports;
2732 port = used_usb_ports;
2733 while (port && port->dev->addr != addr) {
2734 lastp = &port->next;
2735 port = port->next;
2738 if (!port)
2739 return -1;
2741 dev = port->dev;
2742 *lastp = port->next;
2743 usb_attach(port, NULL);
2744 dev->handle_destroy(dev);
2745 port->next = free_usb_ports;
2746 free_usb_ports = port;
2747 return 0;
2750 static int usb_device_del(const char *devname)
2752 int bus_num, addr;
2753 const char *p;
2755 if (strstart(devname, "host:", &p))
2756 return usb_host_device_close(p);
2758 if (!used_usb_ports)
2759 return -1;
2761 p = strchr(devname, '.');
2762 if (!p)
2763 return -1;
2764 bus_num = strtoul(devname, NULL, 0);
2765 addr = strtoul(p + 1, NULL, 0);
2767 return usb_device_del_addr(bus_num, addr);
2770 void do_usb_add(Monitor *mon, const char *devname)
2772 usb_device_add(devname, 1);
2775 void do_usb_del(Monitor *mon, const char *devname)
2777 usb_device_del(devname);
2780 void usb_info(Monitor *mon)
2782 USBDevice *dev;
2783 USBPort *port;
2784 const char *speed_str;
2786 if (!usb_enabled) {
2787 monitor_printf(mon, "USB support not enabled\n");
2788 return;
2791 for (port = used_usb_ports; port; port = port->next) {
2792 dev = port->dev;
2793 if (!dev)
2794 continue;
2795 switch(dev->speed) {
2796 case USB_SPEED_LOW:
2797 speed_str = "1.5";
2798 break;
2799 case USB_SPEED_FULL:
2800 speed_str = "12";
2801 break;
2802 case USB_SPEED_HIGH:
2803 speed_str = "480";
2804 break;
2805 default:
2806 speed_str = "?";
2807 break;
2809 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2810 0, dev->addr, speed_str, dev->devname);
2814 /***********************************************************/
2815 /* PCMCIA/Cardbus */
2817 static struct pcmcia_socket_entry_s {
2818 PCMCIASocket *socket;
2819 struct pcmcia_socket_entry_s *next;
2820 } *pcmcia_sockets = 0;
2822 void pcmcia_socket_register(PCMCIASocket *socket)
2824 struct pcmcia_socket_entry_s *entry;
2826 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2827 entry->socket = socket;
2828 entry->next = pcmcia_sockets;
2829 pcmcia_sockets = entry;
2832 void pcmcia_socket_unregister(PCMCIASocket *socket)
2834 struct pcmcia_socket_entry_s *entry, **ptr;
2836 ptr = &pcmcia_sockets;
2837 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2838 if (entry->socket == socket) {
2839 *ptr = entry->next;
2840 qemu_free(entry);
2844 void pcmcia_info(Monitor *mon)
2846 struct pcmcia_socket_entry_s *iter;
2848 if (!pcmcia_sockets)
2849 monitor_printf(mon, "No PCMCIA sockets\n");
2851 for (iter = pcmcia_sockets; iter; iter = iter->next)
2852 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2853 iter->socket->attached ? iter->socket->card_string :
2854 "Empty");
2857 /***********************************************************/
2858 /* register display */
2860 struct DisplayAllocator default_allocator = {
2861 defaultallocator_create_displaysurface,
2862 defaultallocator_resize_displaysurface,
2863 defaultallocator_free_displaysurface
2866 void register_displaystate(DisplayState *ds)
2868 DisplayState **s;
2869 s = &display_state;
2870 while (*s != NULL)
2871 s = &(*s)->next;
2872 ds->next = NULL;
2873 *s = ds;
2876 DisplayState *get_displaystate(void)
2878 return display_state;
2881 DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2883 if(ds->allocator == &default_allocator) ds->allocator = da;
2884 return ds->allocator;
2887 /* dumb display */
2889 static void dumb_display_init(void)
2891 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
2892 ds->allocator = &default_allocator;
2893 ds->surface = qemu_create_displaysurface(ds, 640, 480);
2894 register_displaystate(ds);
2897 /***********************************************************/
2898 /* I/O handling */
2900 typedef struct IOHandlerRecord {
2901 int fd;
2902 IOCanRWHandler *fd_read_poll;
2903 IOHandler *fd_read;
2904 IOHandler *fd_write;
2905 int deleted;
2906 void *opaque;
2907 /* temporary data */
2908 struct pollfd *ufd;
2909 struct IOHandlerRecord *next;
2910 } IOHandlerRecord;
2912 static IOHandlerRecord *first_io_handler;
2914 /* XXX: fd_read_poll should be suppressed, but an API change is
2915 necessary in the character devices to suppress fd_can_read(). */
2916 int qemu_set_fd_handler2(int fd,
2917 IOCanRWHandler *fd_read_poll,
2918 IOHandler *fd_read,
2919 IOHandler *fd_write,
2920 void *opaque)
2922 IOHandlerRecord **pioh, *ioh;
2924 if (!fd_read && !fd_write) {
2925 pioh = &first_io_handler;
2926 for(;;) {
2927 ioh = *pioh;
2928 if (ioh == NULL)
2929 break;
2930 if (ioh->fd == fd) {
2931 ioh->deleted = 1;
2932 break;
2934 pioh = &ioh->next;
2936 } else {
2937 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2938 if (ioh->fd == fd)
2939 goto found;
2941 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2942 ioh->next = first_io_handler;
2943 first_io_handler = ioh;
2944 found:
2945 ioh->fd = fd;
2946 ioh->fd_read_poll = fd_read_poll;
2947 ioh->fd_read = fd_read;
2948 ioh->fd_write = fd_write;
2949 ioh->opaque = opaque;
2950 ioh->deleted = 0;
2952 return 0;
2955 int qemu_set_fd_handler(int fd,
2956 IOHandler *fd_read,
2957 IOHandler *fd_write,
2958 void *opaque)
2960 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2963 #ifdef _WIN32
2964 /***********************************************************/
2965 /* Polling handling */
2967 typedef struct PollingEntry {
2968 PollingFunc *func;
2969 void *opaque;
2970 struct PollingEntry *next;
2971 } PollingEntry;
2973 static PollingEntry *first_polling_entry;
2975 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2977 PollingEntry **ppe, *pe;
2978 pe = qemu_mallocz(sizeof(PollingEntry));
2979 pe->func = func;
2980 pe->opaque = opaque;
2981 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2982 *ppe = pe;
2983 return 0;
2986 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2988 PollingEntry **ppe, *pe;
2989 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2990 pe = *ppe;
2991 if (pe->func == func && pe->opaque == opaque) {
2992 *ppe = pe->next;
2993 qemu_free(pe);
2994 break;
2999 /***********************************************************/
3000 /* Wait objects support */
3001 typedef struct WaitObjects {
3002 int num;
3003 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3004 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3005 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3006 } WaitObjects;
3008 static WaitObjects wait_objects = {0};
3010 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3012 WaitObjects *w = &wait_objects;
3014 if (w->num >= MAXIMUM_WAIT_OBJECTS)
3015 return -1;
3016 w->events[w->num] = handle;
3017 w->func[w->num] = func;
3018 w->opaque[w->num] = opaque;
3019 w->num++;
3020 return 0;
3023 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3025 int i, found;
3026 WaitObjects *w = &wait_objects;
3028 found = 0;
3029 for (i = 0; i < w->num; i++) {
3030 if (w->events[i] == handle)
3031 found = 1;
3032 if (found) {
3033 w->events[i] = w->events[i + 1];
3034 w->func[i] = w->func[i + 1];
3035 w->opaque[i] = w->opaque[i + 1];
3038 if (found)
3039 w->num--;
3041 #endif
3043 /***********************************************************/
3044 /* ram save/restore */
3046 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3048 int v;
3050 v = qemu_get_byte(f);
3051 switch(v) {
3052 case 0:
3053 if (qemu_get_buffer(f, buf, len) != len)
3054 return -EIO;
3055 break;
3056 case 1:
3057 v = qemu_get_byte(f);
3058 memset(buf, v, len);
3059 break;
3060 default:
3061 return -EINVAL;
3064 if (qemu_file_has_error(f))
3065 return -EIO;
3067 return 0;
3070 static int ram_load_v1(QEMUFile *f, void *opaque)
3072 int ret;
3073 ram_addr_t i;
3075 if (qemu_get_be32(f) != last_ram_offset)
3076 return -EINVAL;
3077 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
3078 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
3079 if (ret)
3080 return ret;
3082 return 0;
3085 #define BDRV_HASH_BLOCK_SIZE 1024
3086 #define IOBUF_SIZE 4096
3087 #define RAM_CBLOCK_MAGIC 0xfabe
3089 typedef struct RamDecompressState {
3090 z_stream zstream;
3091 QEMUFile *f;
3092 uint8_t buf[IOBUF_SIZE];
3093 } RamDecompressState;
3095 static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3097 int ret;
3098 memset(s, 0, sizeof(*s));
3099 s->f = f;
3100 ret = inflateInit(&s->zstream);
3101 if (ret != Z_OK)
3102 return -1;
3103 return 0;
3106 static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3108 int ret, clen;
3110 s->zstream.avail_out = len;
3111 s->zstream.next_out = buf;
3112 while (s->zstream.avail_out > 0) {
3113 if (s->zstream.avail_in == 0) {
3114 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3115 return -1;
3116 clen = qemu_get_be16(s->f);
3117 if (clen > IOBUF_SIZE)
3118 return -1;
3119 qemu_get_buffer(s->f, s->buf, clen);
3120 s->zstream.avail_in = clen;
3121 s->zstream.next_in = s->buf;
3123 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3124 if (ret != Z_OK && ret != Z_STREAM_END) {
3125 return -1;
3128 return 0;
3131 static void ram_decompress_close(RamDecompressState *s)
3133 inflateEnd(&s->zstream);
3136 #define RAM_SAVE_FLAG_FULL 0x01
3137 #define RAM_SAVE_FLAG_COMPRESS 0x02
3138 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3139 #define RAM_SAVE_FLAG_PAGE 0x08
3140 #define RAM_SAVE_FLAG_EOS 0x10
3142 static int is_dup_page(uint8_t *page, uint8_t ch)
3144 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3145 uint32_t *array = (uint32_t *)page;
3146 int i;
3148 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3149 if (array[i] != val)
3150 return 0;
3153 return 1;
3156 static int ram_save_block(QEMUFile *f)
3158 static ram_addr_t current_addr = 0;
3159 ram_addr_t saved_addr = current_addr;
3160 ram_addr_t addr = 0;
3161 int found = 0;
3163 while (addr < last_ram_offset) {
3164 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3165 uint8_t *p;
3167 cpu_physical_memory_reset_dirty(current_addr,
3168 current_addr + TARGET_PAGE_SIZE,
3169 MIGRATION_DIRTY_FLAG);
3171 p = qemu_get_ram_ptr(current_addr);
3173 if (is_dup_page(p, *p)) {
3174 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3175 qemu_put_byte(f, *p);
3176 } else {
3177 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3178 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
3181 found = 1;
3182 break;
3184 addr += TARGET_PAGE_SIZE;
3185 current_addr = (saved_addr + addr) % last_ram_offset;
3188 return found;
3191 static uint64_t bytes_transferred = 0;
3193 static ram_addr_t ram_save_remaining(void)
3195 ram_addr_t addr;
3196 ram_addr_t count = 0;
3198 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3199 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3200 count++;
3203 return count;
3206 uint64_t ram_bytes_remaining(void)
3208 return ram_save_remaining() * TARGET_PAGE_SIZE;
3211 uint64_t ram_bytes_transferred(void)
3213 return bytes_transferred;
3216 uint64_t ram_bytes_total(void)
3218 return last_ram_offset;
3221 static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3223 ram_addr_t addr;
3224 uint64_t bytes_transferred_last;
3225 double bwidth = 0;
3226 uint64_t expected_time = 0;
3228 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
3229 qemu_file_set_error(f);
3230 return 0;
3233 if (stage == 1) {
3234 /* Make sure all dirty bits are set */
3235 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3236 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3237 cpu_physical_memory_set_dirty(addr);
3240 /* Enable dirty memory tracking */
3241 cpu_physical_memory_set_dirty_tracking(1);
3243 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
3246 bytes_transferred_last = bytes_transferred;
3247 bwidth = get_clock();
3249 while (!qemu_file_rate_limit(f)) {
3250 int ret;
3252 ret = ram_save_block(f);
3253 bytes_transferred += ret * TARGET_PAGE_SIZE;
3254 if (ret == 0) /* no more blocks */
3255 break;
3258 bwidth = get_clock() - bwidth;
3259 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3261 /* if we haven't transferred anything this round, force expected_time to a
3262 * a very high value, but without crashing */
3263 if (bwidth == 0)
3264 bwidth = 0.000001;
3266 /* try transferring iterative blocks of memory */
3268 if (stage == 3) {
3270 /* flush all remaining blocks regardless of rate limiting */
3271 while (ram_save_block(f) != 0) {
3272 bytes_transferred += TARGET_PAGE_SIZE;
3274 cpu_physical_memory_set_dirty_tracking(0);
3277 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3279 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3281 return (stage == 2) && (expected_time <= migrate_max_downtime());
3284 static int ram_load_dead(QEMUFile *f, void *opaque)
3286 RamDecompressState s1, *s = &s1;
3287 uint8_t buf[10];
3288 ram_addr_t i;
3290 if (ram_decompress_open(s, f) < 0)
3291 return -EINVAL;
3292 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
3293 if (ram_decompress_buf(s, buf, 1) < 0) {
3294 fprintf(stderr, "Error while reading ram block header\n");
3295 goto error;
3297 if (buf[0] == 0) {
3298 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3299 BDRV_HASH_BLOCK_SIZE) < 0) {
3300 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
3301 goto error;
3303 } else {
3304 error:
3305 printf("Error block header\n");
3306 return -EINVAL;
3309 ram_decompress_close(s);
3311 return 0;
3314 static int ram_load(QEMUFile *f, void *opaque, int version_id)
3316 ram_addr_t addr;
3317 int flags;
3319 if (version_id == 1)
3320 return ram_load_v1(f, opaque);
3322 if (version_id == 2) {
3323 if (qemu_get_be32(f) != last_ram_offset)
3324 return -EINVAL;
3325 return ram_load_dead(f, opaque);
3328 if (version_id != 3)
3329 return -EINVAL;
3331 do {
3332 addr = qemu_get_be64(f);
3334 flags = addr & ~TARGET_PAGE_MASK;
3335 addr &= TARGET_PAGE_MASK;
3337 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3338 if (addr != last_ram_offset)
3339 return -EINVAL;
3342 if (flags & RAM_SAVE_FLAG_FULL) {
3343 if (ram_load_dead(f, opaque) < 0)
3344 return -EINVAL;
3347 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3348 uint8_t ch = qemu_get_byte(f);
3349 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3350 } else if (flags & RAM_SAVE_FLAG_PAGE)
3351 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
3352 } while (!(flags & RAM_SAVE_FLAG_EOS));
3354 return 0;
3357 void qemu_service_io(void)
3359 qemu_notify_event();
3362 /***********************************************************/
3363 /* bottom halves (can be seen as timers which expire ASAP) */
3365 struct QEMUBH {
3366 QEMUBHFunc *cb;
3367 void *opaque;
3368 int scheduled;
3369 int idle;
3370 int deleted;
3371 QEMUBH *next;
3374 static QEMUBH *first_bh = NULL;
3376 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3378 QEMUBH *bh;
3379 bh = qemu_mallocz(sizeof(QEMUBH));
3380 bh->cb = cb;
3381 bh->opaque = opaque;
3382 bh->next = first_bh;
3383 first_bh = bh;
3384 return bh;
3387 int qemu_bh_poll(void)
3389 QEMUBH *bh, **bhp;
3390 int ret;
3392 ret = 0;
3393 for (bh = first_bh; bh; bh = bh->next) {
3394 if (!bh->deleted && bh->scheduled) {
3395 bh->scheduled = 0;
3396 if (!bh->idle)
3397 ret = 1;
3398 bh->idle = 0;
3399 bh->cb(bh->opaque);
3403 /* remove deleted bhs */
3404 bhp = &first_bh;
3405 while (*bhp) {
3406 bh = *bhp;
3407 if (bh->deleted) {
3408 *bhp = bh->next;
3409 qemu_free(bh);
3410 } else
3411 bhp = &bh->next;
3414 return ret;
3417 void qemu_bh_schedule_idle(QEMUBH *bh)
3419 if (bh->scheduled)
3420 return;
3421 bh->scheduled = 1;
3422 bh->idle = 1;
3425 void qemu_bh_schedule(QEMUBH *bh)
3427 if (bh->scheduled)
3428 return;
3429 bh->scheduled = 1;
3430 bh->idle = 0;
3431 /* stop the currently executing CPU to execute the BH ASAP */
3432 qemu_notify_event();
3435 void qemu_bh_cancel(QEMUBH *bh)
3437 bh->scheduled = 0;
3440 void qemu_bh_delete(QEMUBH *bh)
3442 bh->scheduled = 0;
3443 bh->deleted = 1;
3446 static void qemu_bh_update_timeout(int *timeout)
3448 QEMUBH *bh;
3450 for (bh = first_bh; bh; bh = bh->next) {
3451 if (!bh->deleted && bh->scheduled) {
3452 if (bh->idle) {
3453 /* idle bottom halves will be polled at least
3454 * every 10ms */
3455 *timeout = MIN(10, *timeout);
3456 } else {
3457 /* non-idle bottom halves will be executed
3458 * immediately */
3459 *timeout = 0;
3460 break;
3466 /***********************************************************/
3467 /* machine registration */
3469 static QEMUMachine *first_machine = NULL;
3470 QEMUMachine *current_machine = NULL;
3472 int qemu_register_machine(QEMUMachine *m)
3474 QEMUMachine **pm;
3475 pm = &first_machine;
3476 while (*pm != NULL)
3477 pm = &(*pm)->next;
3478 m->next = NULL;
3479 *pm = m;
3480 return 0;
3483 static QEMUMachine *find_machine(const char *name)
3485 QEMUMachine *m;
3487 for(m = first_machine; m != NULL; m = m->next) {
3488 if (!strcmp(m->name, name))
3489 return m;
3491 return NULL;
3494 static QEMUMachine *find_default_machine(void)
3496 QEMUMachine *m;
3498 for(m = first_machine; m != NULL; m = m->next) {
3499 if (m->is_default) {
3500 return m;
3503 return NULL;
3506 /***********************************************************/
3507 /* main execution loop */
3509 static void gui_update(void *opaque)
3511 uint64_t interval = GUI_REFRESH_INTERVAL;
3512 DisplayState *ds = opaque;
3513 DisplayChangeListener *dcl = ds->listeners;
3515 dpy_refresh(ds);
3517 while (dcl != NULL) {
3518 if (dcl->gui_timer_interval &&
3519 dcl->gui_timer_interval < interval)
3520 interval = dcl->gui_timer_interval;
3521 dcl = dcl->next;
3523 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
3526 static void nographic_update(void *opaque)
3528 uint64_t interval = GUI_REFRESH_INTERVAL;
3530 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3533 struct vm_change_state_entry {
3534 VMChangeStateHandler *cb;
3535 void *opaque;
3536 LIST_ENTRY (vm_change_state_entry) entries;
3539 static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3541 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3542 void *opaque)
3544 VMChangeStateEntry *e;
3546 e = qemu_mallocz(sizeof (*e));
3548 e->cb = cb;
3549 e->opaque = opaque;
3550 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3551 return e;
3554 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3556 LIST_REMOVE (e, entries);
3557 qemu_free (e);
3560 static void vm_state_notify(int running, int reason)
3562 VMChangeStateEntry *e;
3564 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3565 e->cb(e->opaque, running, reason);
3569 static void resume_all_vcpus(void);
3570 static void pause_all_vcpus(void);
3572 void vm_start(void)
3574 if (!vm_running) {
3575 cpu_enable_ticks();
3576 vm_running = 1;
3577 vm_state_notify(1, 0);
3578 qemu_rearm_alarm_timer(alarm_timer);
3579 resume_all_vcpus();
3583 /* reset/shutdown handler */
3585 typedef struct QEMUResetEntry {
3586 QEMUResetHandler *func;
3587 void *opaque;
3588 int order;
3589 struct QEMUResetEntry *next;
3590 } QEMUResetEntry;
3592 static QEMUResetEntry *first_reset_entry;
3593 static int reset_requested;
3594 static int shutdown_requested;
3595 static int powerdown_requested;
3596 static int debug_requested;
3597 static int vmstop_requested;
3599 int qemu_shutdown_requested(void)
3601 int r = shutdown_requested;
3602 shutdown_requested = 0;
3603 return r;
3606 int qemu_reset_requested(void)
3608 int r = reset_requested;
3609 reset_requested = 0;
3610 return r;
3613 int qemu_powerdown_requested(void)
3615 int r = powerdown_requested;
3616 powerdown_requested = 0;
3617 return r;
3620 static int qemu_debug_requested(void)
3622 int r = debug_requested;
3623 debug_requested = 0;
3624 return r;
3627 static int qemu_vmstop_requested(void)
3629 int r = vmstop_requested;
3630 vmstop_requested = 0;
3631 return r;
3634 static void do_vm_stop(int reason)
3636 if (vm_running) {
3637 cpu_disable_ticks();
3638 vm_running = 0;
3639 pause_all_vcpus();
3640 vm_state_notify(0, reason);
3644 void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
3646 QEMUResetEntry **pre, *re;
3648 pre = &first_reset_entry;
3649 while (*pre != NULL && (*pre)->order >= order) {
3650 pre = &(*pre)->next;
3652 re = qemu_mallocz(sizeof(QEMUResetEntry));
3653 re->func = func;
3654 re->opaque = opaque;
3655 re->order = order;
3656 re->next = NULL;
3657 *pre = re;
3660 void qemu_system_reset(void)
3662 QEMUResetEntry *re;
3664 /* reset all devices */
3665 for(re = first_reset_entry; re != NULL; re = re->next) {
3666 re->func(re->opaque);
3670 void qemu_system_reset_request(void)
3672 if (no_reboot) {
3673 shutdown_requested = 1;
3674 } else {
3675 reset_requested = 1;
3677 qemu_notify_event();
3680 void qemu_system_shutdown_request(void)
3682 shutdown_requested = 1;
3683 qemu_notify_event();
3686 void qemu_system_powerdown_request(void)
3688 powerdown_requested = 1;
3689 qemu_notify_event();
3692 #ifdef CONFIG_IOTHREAD
3693 static void qemu_system_vmstop_request(int reason)
3695 vmstop_requested = reason;
3696 qemu_notify_event();
3698 #endif
3700 #ifndef _WIN32
3701 static int io_thread_fd = -1;
3703 static void qemu_event_increment(void)
3705 static const char byte = 0;
3707 if (io_thread_fd == -1)
3708 return;
3710 write(io_thread_fd, &byte, sizeof(byte));
3713 static void qemu_event_read(void *opaque)
3715 int fd = (unsigned long)opaque;
3716 ssize_t len;
3718 /* Drain the notify pipe */
3719 do {
3720 char buffer[512];
3721 len = read(fd, buffer, sizeof(buffer));
3722 } while ((len == -1 && errno == EINTR) || len > 0);
3725 static int qemu_event_init(void)
3727 int err;
3728 int fds[2];
3730 err = pipe(fds);
3731 if (err == -1)
3732 return -errno;
3734 err = fcntl_setfl(fds[0], O_NONBLOCK);
3735 if (err < 0)
3736 goto fail;
3738 err = fcntl_setfl(fds[1], O_NONBLOCK);
3739 if (err < 0)
3740 goto fail;
3742 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3743 (void *)(unsigned long)fds[0]);
3745 io_thread_fd = fds[1];
3746 return 0;
3748 fail:
3749 close(fds[0]);
3750 close(fds[1]);
3751 return err;
3753 #else
3754 HANDLE qemu_event_handle;
3756 static void dummy_event_handler(void *opaque)
3760 static int qemu_event_init(void)
3762 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3763 if (!qemu_event_handle) {
3764 perror("Failed CreateEvent");
3765 return -1;
3767 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3768 return 0;
3771 static void qemu_event_increment(void)
3773 SetEvent(qemu_event_handle);
3775 #endif
3777 static int cpu_can_run(CPUState *env)
3779 if (env->stop)
3780 return 0;
3781 if (env->stopped)
3782 return 0;
3783 return 1;
3786 #ifndef CONFIG_IOTHREAD
3787 static int qemu_init_main_loop(void)
3789 return qemu_event_init();
3792 void qemu_init_vcpu(void *_env)
3794 CPUState *env = _env;
3796 if (kvm_enabled())
3797 kvm_init_vcpu(env);
3798 return;
3801 int qemu_cpu_self(void *env)
3803 return 1;
3806 static void resume_all_vcpus(void)
3810 static void pause_all_vcpus(void)
3814 void qemu_cpu_kick(void *env)
3816 return;
3819 void qemu_notify_event(void)
3821 CPUState *env = cpu_single_env;
3823 if (env) {
3824 cpu_exit(env);
3825 #ifdef USE_KQEMU
3826 if (env->kqemu_enabled)
3827 kqemu_cpu_interrupt(env);
3828 #endif
3832 #define qemu_mutex_lock_iothread() do { } while (0)
3833 #define qemu_mutex_unlock_iothread() do { } while (0)
3835 void vm_stop(int reason)
3837 do_vm_stop(reason);
3840 #else /* CONFIG_IOTHREAD */
3842 #include "qemu-thread.h"
3844 QemuMutex qemu_global_mutex;
3845 static QemuMutex qemu_fair_mutex;
3847 static QemuThread io_thread;
3849 static QemuThread *tcg_cpu_thread;
3850 static QemuCond *tcg_halt_cond;
3852 static int qemu_system_ready;
3853 /* cpu creation */
3854 static QemuCond qemu_cpu_cond;
3855 /* system init */
3856 static QemuCond qemu_system_cond;
3857 static QemuCond qemu_pause_cond;
3859 static void block_io_signals(void);
3860 static void unblock_io_signals(void);
3861 static int tcg_has_work(void);
3863 static int qemu_init_main_loop(void)
3865 int ret;
3867 ret = qemu_event_init();
3868 if (ret)
3869 return ret;
3871 qemu_cond_init(&qemu_pause_cond);
3872 qemu_mutex_init(&qemu_fair_mutex);
3873 qemu_mutex_init(&qemu_global_mutex);
3874 qemu_mutex_lock(&qemu_global_mutex);
3876 unblock_io_signals();
3877 qemu_thread_self(&io_thread);
3879 return 0;
3882 static void qemu_wait_io_event(CPUState *env)
3884 while (!tcg_has_work())
3885 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3887 qemu_mutex_unlock(&qemu_global_mutex);
3890 * Users of qemu_global_mutex can be starved, having no chance
3891 * to acquire it since this path will get to it first.
3892 * So use another lock to provide fairness.
3894 qemu_mutex_lock(&qemu_fair_mutex);
3895 qemu_mutex_unlock(&qemu_fair_mutex);
3897 qemu_mutex_lock(&qemu_global_mutex);
3898 if (env->stop) {
3899 env->stop = 0;
3900 env->stopped = 1;
3901 qemu_cond_signal(&qemu_pause_cond);
3905 static int qemu_cpu_exec(CPUState *env);
3907 static void *kvm_cpu_thread_fn(void *arg)
3909 CPUState *env = arg;
3911 block_io_signals();
3912 qemu_thread_self(env->thread);
3914 /* signal CPU creation */
3915 qemu_mutex_lock(&qemu_global_mutex);
3916 env->created = 1;
3917 qemu_cond_signal(&qemu_cpu_cond);
3919 /* and wait for machine initialization */
3920 while (!qemu_system_ready)
3921 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3923 while (1) {
3924 if (cpu_can_run(env))
3925 qemu_cpu_exec(env);
3926 qemu_wait_io_event(env);
3929 return NULL;
3932 static void tcg_cpu_exec(void);
3934 static void *tcg_cpu_thread_fn(void *arg)
3936 CPUState *env = arg;
3938 block_io_signals();
3939 qemu_thread_self(env->thread);
3941 /* signal CPU creation */
3942 qemu_mutex_lock(&qemu_global_mutex);
3943 for (env = first_cpu; env != NULL; env = env->next_cpu)
3944 env->created = 1;
3945 qemu_cond_signal(&qemu_cpu_cond);
3947 /* and wait for machine initialization */
3948 while (!qemu_system_ready)
3949 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3951 while (1) {
3952 tcg_cpu_exec();
3953 qemu_wait_io_event(cur_cpu);
3956 return NULL;
3959 void qemu_cpu_kick(void *_env)
3961 CPUState *env = _env;
3962 qemu_cond_broadcast(env->halt_cond);
3963 if (kvm_enabled())
3964 qemu_thread_signal(env->thread, SIGUSR1);
3967 int qemu_cpu_self(void *env)
3969 return (cpu_single_env != NULL);
3972 static void cpu_signal(int sig)
3974 if (cpu_single_env)
3975 cpu_exit(cpu_single_env);
3978 static void block_io_signals(void)
3980 sigset_t set;
3981 struct sigaction sigact;
3983 sigemptyset(&set);
3984 sigaddset(&set, SIGUSR2);
3985 sigaddset(&set, SIGIO);
3986 sigaddset(&set, SIGALRM);
3987 pthread_sigmask(SIG_BLOCK, &set, NULL);
3989 sigemptyset(&set);
3990 sigaddset(&set, SIGUSR1);
3991 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3993 memset(&sigact, 0, sizeof(sigact));
3994 sigact.sa_handler = cpu_signal;
3995 sigaction(SIGUSR1, &sigact, NULL);
3998 static void unblock_io_signals(void)
4000 sigset_t set;
4002 sigemptyset(&set);
4003 sigaddset(&set, SIGUSR2);
4004 sigaddset(&set, SIGIO);
4005 sigaddset(&set, SIGALRM);
4006 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
4008 sigemptyset(&set);
4009 sigaddset(&set, SIGUSR1);
4010 pthread_sigmask(SIG_BLOCK, &set, NULL);
4013 static void qemu_signal_lock(unsigned int msecs)
4015 qemu_mutex_lock(&qemu_fair_mutex);
4017 while (qemu_mutex_trylock(&qemu_global_mutex)) {
4018 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
4019 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
4020 break;
4022 qemu_mutex_unlock(&qemu_fair_mutex);
4025 static void qemu_mutex_lock_iothread(void)
4027 if (kvm_enabled()) {
4028 qemu_mutex_lock(&qemu_fair_mutex);
4029 qemu_mutex_lock(&qemu_global_mutex);
4030 qemu_mutex_unlock(&qemu_fair_mutex);
4031 } else
4032 qemu_signal_lock(100);
4035 static void qemu_mutex_unlock_iothread(void)
4037 qemu_mutex_unlock(&qemu_global_mutex);
4040 static int all_vcpus_paused(void)
4042 CPUState *penv = first_cpu;
4044 while (penv) {
4045 if (!penv->stopped)
4046 return 0;
4047 penv = (CPUState *)penv->next_cpu;
4050 return 1;
4053 static void pause_all_vcpus(void)
4055 CPUState *penv = first_cpu;
4057 while (penv) {
4058 penv->stop = 1;
4059 qemu_thread_signal(penv->thread, SIGUSR1);
4060 qemu_cpu_kick(penv);
4061 penv = (CPUState *)penv->next_cpu;
4064 while (!all_vcpus_paused()) {
4065 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
4066 penv = first_cpu;
4067 while (penv) {
4068 qemu_thread_signal(penv->thread, SIGUSR1);
4069 penv = (CPUState *)penv->next_cpu;
4074 static void resume_all_vcpus(void)
4076 CPUState *penv = first_cpu;
4078 while (penv) {
4079 penv->stop = 0;
4080 penv->stopped = 0;
4081 qemu_thread_signal(penv->thread, SIGUSR1);
4082 qemu_cpu_kick(penv);
4083 penv = (CPUState *)penv->next_cpu;
4087 static void tcg_init_vcpu(void *_env)
4089 CPUState *env = _env;
4090 /* share a single thread for all cpus with TCG */
4091 if (!tcg_cpu_thread) {
4092 env->thread = qemu_mallocz(sizeof(QemuThread));
4093 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
4094 qemu_cond_init(env->halt_cond);
4095 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
4096 while (env->created == 0)
4097 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
4098 tcg_cpu_thread = env->thread;
4099 tcg_halt_cond = env->halt_cond;
4100 } else {
4101 env->thread = tcg_cpu_thread;
4102 env->halt_cond = tcg_halt_cond;
4106 static void kvm_start_vcpu(CPUState *env)
4108 kvm_init_vcpu(env);
4109 env->thread = qemu_mallocz(sizeof(QemuThread));
4110 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
4111 qemu_cond_init(env->halt_cond);
4112 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
4113 while (env->created == 0)
4114 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
4117 void qemu_init_vcpu(void *_env)
4119 CPUState *env = _env;
4121 if (kvm_enabled())
4122 kvm_start_vcpu(env);
4123 else
4124 tcg_init_vcpu(env);
4127 void qemu_notify_event(void)
4129 qemu_event_increment();
4132 void vm_stop(int reason)
4134 QemuThread me;
4135 qemu_thread_self(&me);
4137 if (!qemu_thread_equal(&me, &io_thread)) {
4138 qemu_system_vmstop_request(reason);
4140 * FIXME: should not return to device code in case
4141 * vm_stop() has been requested.
4143 if (cpu_single_env) {
4144 cpu_exit(cpu_single_env);
4145 cpu_single_env->stop = 1;
4147 return;
4149 do_vm_stop(reason);
4152 #endif
4155 #ifdef _WIN32
4156 static void host_main_loop_wait(int *timeout)
4158 int ret, ret2, i;
4159 PollingEntry *pe;
4162 /* XXX: need to suppress polling by better using win32 events */
4163 ret = 0;
4164 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4165 ret |= pe->func(pe->opaque);
4167 if (ret == 0) {
4168 int err;
4169 WaitObjects *w = &wait_objects;
4171 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
4172 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4173 if (w->func[ret - WAIT_OBJECT_0])
4174 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
4176 /* Check for additional signaled events */
4177 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
4179 /* Check if event is signaled */
4180 ret2 = WaitForSingleObject(w->events[i], 0);
4181 if(ret2 == WAIT_OBJECT_0) {
4182 if (w->func[i])
4183 w->func[i](w->opaque[i]);
4184 } else if (ret2 == WAIT_TIMEOUT) {
4185 } else {
4186 err = GetLastError();
4187 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
4190 } else if (ret == WAIT_TIMEOUT) {
4191 } else {
4192 err = GetLastError();
4193 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
4197 *timeout = 0;
4199 #else
4200 static void host_main_loop_wait(int *timeout)
4203 #endif
4205 void main_loop_wait(int timeout)
4207 IOHandlerRecord *ioh;
4208 fd_set rfds, wfds, xfds;
4209 int ret, nfds;
4210 struct timeval tv;
4212 qemu_bh_update_timeout(&timeout);
4214 host_main_loop_wait(&timeout);
4216 /* poll any events */
4217 /* XXX: separate device handlers from system ones */
4218 nfds = -1;
4219 FD_ZERO(&rfds);
4220 FD_ZERO(&wfds);
4221 FD_ZERO(&xfds);
4222 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4223 if (ioh->deleted)
4224 continue;
4225 if (ioh->fd_read &&
4226 (!ioh->fd_read_poll ||
4227 ioh->fd_read_poll(ioh->opaque) != 0)) {
4228 FD_SET(ioh->fd, &rfds);
4229 if (ioh->fd > nfds)
4230 nfds = ioh->fd;
4232 if (ioh->fd_write) {
4233 FD_SET(ioh->fd, &wfds);
4234 if (ioh->fd > nfds)
4235 nfds = ioh->fd;
4239 tv.tv_sec = timeout / 1000;
4240 tv.tv_usec = (timeout % 1000) * 1000;
4242 #if defined(CONFIG_SLIRP)
4243 if (slirp_is_inited()) {
4244 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4246 #endif
4247 qemu_mutex_unlock_iothread();
4248 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4249 qemu_mutex_lock_iothread();
4250 if (ret > 0) {
4251 IOHandlerRecord **pioh;
4253 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4254 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
4255 ioh->fd_read(ioh->opaque);
4257 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
4258 ioh->fd_write(ioh->opaque);
4262 /* remove deleted IO handlers */
4263 pioh = &first_io_handler;
4264 while (*pioh) {
4265 ioh = *pioh;
4266 if (ioh->deleted) {
4267 *pioh = ioh->next;
4268 qemu_free(ioh);
4269 } else
4270 pioh = &ioh->next;
4273 #if defined(CONFIG_SLIRP)
4274 if (slirp_is_inited()) {
4275 if (ret < 0) {
4276 FD_ZERO(&rfds);
4277 FD_ZERO(&wfds);
4278 FD_ZERO(&xfds);
4280 slirp_select_poll(&rfds, &wfds, &xfds);
4282 #endif
4284 /* rearm timer, if not periodic */
4285 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4286 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4287 qemu_rearm_alarm_timer(alarm_timer);
4290 /* vm time timers */
4291 if (vm_running) {
4292 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4293 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4294 qemu_get_clock(vm_clock));
4297 /* real time timers */
4298 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4299 qemu_get_clock(rt_clock));
4301 /* Check bottom-halves last in case any of the earlier events triggered
4302 them. */
4303 qemu_bh_poll();
4307 static int qemu_cpu_exec(CPUState *env)
4309 int ret;
4310 #ifdef CONFIG_PROFILER
4311 int64_t ti;
4312 #endif
4314 #ifdef CONFIG_PROFILER
4315 ti = profile_getclock();
4316 #endif
4317 if (use_icount) {
4318 int64_t count;
4319 int decr;
4320 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4321 env->icount_decr.u16.low = 0;
4322 env->icount_extra = 0;
4323 count = qemu_next_deadline();
4324 count = (count + (1 << icount_time_shift) - 1)
4325 >> icount_time_shift;
4326 qemu_icount += count;
4327 decr = (count > 0xffff) ? 0xffff : count;
4328 count -= decr;
4329 env->icount_decr.u16.low = decr;
4330 env->icount_extra = count;
4332 ret = cpu_exec(env);
4333 #ifdef CONFIG_PROFILER
4334 qemu_time += profile_getclock() - ti;
4335 #endif
4336 if (use_icount) {
4337 /* Fold pending instructions back into the
4338 instruction counter, and clear the interrupt flag. */
4339 qemu_icount -= (env->icount_decr.u16.low
4340 + env->icount_extra);
4341 env->icount_decr.u32 = 0;
4342 env->icount_extra = 0;
4344 return ret;
4347 static void tcg_cpu_exec(void)
4349 int ret = 0;
4351 if (next_cpu == NULL)
4352 next_cpu = first_cpu;
4353 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4354 CPUState *env = cur_cpu = next_cpu;
4356 if (!vm_running)
4357 break;
4358 if (timer_alarm_pending) {
4359 timer_alarm_pending = 0;
4360 break;
4362 if (cpu_can_run(env))
4363 ret = qemu_cpu_exec(env);
4364 if (ret == EXCP_DEBUG) {
4365 gdb_set_stop_cpu(env);
4366 debug_requested = 1;
4367 break;
4372 static int cpu_has_work(CPUState *env)
4374 if (env->stop)
4375 return 1;
4376 if (env->stopped)
4377 return 0;
4378 if (!env->halted)
4379 return 1;
4380 if (qemu_cpu_has_work(env))
4381 return 1;
4382 return 0;
4385 static int tcg_has_work(void)
4387 CPUState *env;
4389 for (env = first_cpu; env != NULL; env = env->next_cpu)
4390 if (cpu_has_work(env))
4391 return 1;
4392 return 0;
4395 static int qemu_calculate_timeout(void)
4397 #ifndef CONFIG_IOTHREAD
4398 int timeout;
4400 if (!vm_running)
4401 timeout = 5000;
4402 else if (tcg_has_work())
4403 timeout = 0;
4404 else if (!use_icount)
4405 timeout = 5000;
4406 else {
4407 /* XXX: use timeout computed from timers */
4408 int64_t add;
4409 int64_t delta;
4410 /* Advance virtual time to the next event. */
4411 if (use_icount == 1) {
4412 /* When not using an adaptive execution frequency
4413 we tend to get badly out of sync with real time,
4414 so just delay for a reasonable amount of time. */
4415 delta = 0;
4416 } else {
4417 delta = cpu_get_icount() - cpu_get_clock();
4419 if (delta > 0) {
4420 /* If virtual time is ahead of real time then just
4421 wait for IO. */
4422 timeout = (delta / 1000000) + 1;
4423 } else {
4424 /* Wait for either IO to occur or the next
4425 timer event. */
4426 add = qemu_next_deadline();
4427 /* We advance the timer before checking for IO.
4428 Limit the amount we advance so that early IO
4429 activity won't get the guest too far ahead. */
4430 if (add > 10000000)
4431 add = 10000000;
4432 delta += add;
4433 add = (add + (1 << icount_time_shift) - 1)
4434 >> icount_time_shift;
4435 qemu_icount += add;
4436 timeout = delta / 1000000;
4437 if (timeout < 0)
4438 timeout = 0;
4442 return timeout;
4443 #else /* CONFIG_IOTHREAD */
4444 return 1000;
4445 #endif
4448 static int vm_can_run(void)
4450 if (powerdown_requested)
4451 return 0;
4452 if (reset_requested)
4453 return 0;
4454 if (shutdown_requested)
4455 return 0;
4456 if (debug_requested)
4457 return 0;
4458 return 1;
4461 static void main_loop(void)
4463 int r;
4465 #ifdef CONFIG_IOTHREAD
4466 qemu_system_ready = 1;
4467 qemu_cond_broadcast(&qemu_system_cond);
4468 #endif
4470 for (;;) {
4471 do {
4472 #ifdef CONFIG_PROFILER
4473 int64_t ti;
4474 #endif
4475 #ifndef CONFIG_IOTHREAD
4476 tcg_cpu_exec();
4477 #endif
4478 #ifdef CONFIG_PROFILER
4479 ti = profile_getclock();
4480 #endif
4481 main_loop_wait(qemu_calculate_timeout());
4482 #ifdef CONFIG_PROFILER
4483 dev_time += profile_getclock() - ti;
4484 #endif
4485 } while (vm_can_run());
4487 if (qemu_debug_requested())
4488 vm_stop(EXCP_DEBUG);
4489 if (qemu_shutdown_requested()) {
4490 if (no_shutdown) {
4491 vm_stop(0);
4492 no_shutdown = 0;
4493 } else
4494 break;
4496 if (qemu_reset_requested()) {
4497 pause_all_vcpus();
4498 qemu_system_reset();
4499 resume_all_vcpus();
4501 if (qemu_powerdown_requested())
4502 qemu_system_powerdown();
4503 if ((r = qemu_vmstop_requested()))
4504 vm_stop(r);
4506 pause_all_vcpus();
4509 static void version(void)
4511 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
4514 static void help(int exitcode)
4516 version();
4517 printf("usage: %s [options] [disk_image]\n"
4518 "\n"
4519 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4520 "\n"
4521 #define DEF(option, opt_arg, opt_enum, opt_help) \
4522 opt_help
4523 #define DEFHEADING(text) stringify(text) "\n"
4524 #include "qemu-options.h"
4525 #undef DEF
4526 #undef DEFHEADING
4527 #undef GEN_DOCS
4528 "\n"
4529 "During emulation, the following keys are useful:\n"
4530 "ctrl-alt-f toggle full screen\n"
4531 "ctrl-alt-n switch to virtual console 'n'\n"
4532 "ctrl-alt toggle mouse and keyboard grab\n"
4533 "\n"
4534 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4536 "qemu",
4537 DEFAULT_RAM_SIZE,
4538 #ifndef _WIN32
4539 DEFAULT_NETWORK_SCRIPT,
4540 DEFAULT_NETWORK_DOWN_SCRIPT,
4541 #endif
4542 DEFAULT_GDBSTUB_PORT,
4543 "/tmp/qemu.log");
4544 exit(exitcode);
4547 #define HAS_ARG 0x0001
4549 enum {
4550 #define DEF(option, opt_arg, opt_enum, opt_help) \
4551 opt_enum,
4552 #define DEFHEADING(text)
4553 #include "qemu-options.h"
4554 #undef DEF
4555 #undef DEFHEADING
4556 #undef GEN_DOCS
4559 typedef struct QEMUOption {
4560 const char *name;
4561 int flags;
4562 int index;
4563 } QEMUOption;
4565 static const QEMUOption qemu_options[] = {
4566 { "h", 0, QEMU_OPTION_h },
4567 #define DEF(option, opt_arg, opt_enum, opt_help) \
4568 { option, opt_arg, opt_enum },
4569 #define DEFHEADING(text)
4570 #include "qemu-options.h"
4571 #undef DEF
4572 #undef DEFHEADING
4573 #undef GEN_DOCS
4574 { NULL },
4577 #ifdef HAS_AUDIO
4578 struct soundhw soundhw[] = {
4579 #ifdef HAS_AUDIO_CHOICE
4580 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4582 "pcspk",
4583 "PC speaker",
4586 { .init_isa = pcspk_audio_init }
4588 #endif
4590 #ifdef CONFIG_SB16
4592 "sb16",
4593 "Creative Sound Blaster 16",
4596 { .init_isa = SB16_init }
4598 #endif
4600 #ifdef CONFIG_CS4231A
4602 "cs4231a",
4603 "CS4231A",
4606 { .init_isa = cs4231a_init }
4608 #endif
4610 #ifdef CONFIG_ADLIB
4612 "adlib",
4613 #ifdef HAS_YMF262
4614 "Yamaha YMF262 (OPL3)",
4615 #else
4616 "Yamaha YM3812 (OPL2)",
4617 #endif
4620 { .init_isa = Adlib_init }
4622 #endif
4624 #ifdef CONFIG_GUS
4626 "gus",
4627 "Gravis Ultrasound GF1",
4630 { .init_isa = GUS_init }
4632 #endif
4634 #ifdef CONFIG_AC97
4636 "ac97",
4637 "Intel 82801AA AC97 Audio",
4640 { .init_pci = ac97_init }
4642 #endif
4644 #ifdef CONFIG_ES1370
4646 "es1370",
4647 "ENSONIQ AudioPCI ES1370",
4650 { .init_pci = es1370_init }
4652 #endif
4654 #endif /* HAS_AUDIO_CHOICE */
4656 { NULL, NULL, 0, 0, { NULL } }
4659 static void select_soundhw (const char *optarg)
4661 struct soundhw *c;
4663 if (*optarg == '?') {
4664 show_valid_cards:
4666 printf ("Valid sound card names (comma separated):\n");
4667 for (c = soundhw; c->name; ++c) {
4668 printf ("%-11s %s\n", c->name, c->descr);
4670 printf ("\n-soundhw all will enable all of the above\n");
4671 exit (*optarg != '?');
4673 else {
4674 size_t l;
4675 const char *p;
4676 char *e;
4677 int bad_card = 0;
4679 if (!strcmp (optarg, "all")) {
4680 for (c = soundhw; c->name; ++c) {
4681 c->enabled = 1;
4683 return;
4686 p = optarg;
4687 while (*p) {
4688 e = strchr (p, ',');
4689 l = !e ? strlen (p) : (size_t) (e - p);
4691 for (c = soundhw; c->name; ++c) {
4692 if (!strncmp (c->name, p, l)) {
4693 c->enabled = 1;
4694 break;
4698 if (!c->name) {
4699 if (l > 80) {
4700 fprintf (stderr,
4701 "Unknown sound card name (too big to show)\n");
4703 else {
4704 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4705 (int) l, p);
4707 bad_card = 1;
4709 p += l + (e != NULL);
4712 if (bad_card)
4713 goto show_valid_cards;
4716 #endif
4718 static void select_vgahw (const char *p)
4720 const char *opts;
4722 cirrus_vga_enabled = 0;
4723 std_vga_enabled = 0;
4724 vmsvga_enabled = 0;
4725 xenfb_enabled = 0;
4726 if (strstart(p, "std", &opts)) {
4727 std_vga_enabled = 1;
4728 } else if (strstart(p, "cirrus", &opts)) {
4729 cirrus_vga_enabled = 1;
4730 } else if (strstart(p, "vmware", &opts)) {
4731 vmsvga_enabled = 1;
4732 } else if (strstart(p, "xenfb", &opts)) {
4733 xenfb_enabled = 1;
4734 } else if (!strstart(p, "none", &opts)) {
4735 invalid_vga:
4736 fprintf(stderr, "Unknown vga type: %s\n", p);
4737 exit(1);
4739 while (*opts) {
4740 const char *nextopt;
4742 if (strstart(opts, ",retrace=", &nextopt)) {
4743 opts = nextopt;
4744 if (strstart(opts, "dumb", &nextopt))
4745 vga_retrace_method = VGA_RETRACE_DUMB;
4746 else if (strstart(opts, "precise", &nextopt))
4747 vga_retrace_method = VGA_RETRACE_PRECISE;
4748 else goto invalid_vga;
4749 } else goto invalid_vga;
4750 opts = nextopt;
4754 #ifdef _WIN32
4755 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4757 exit(STATUS_CONTROL_C_EXIT);
4758 return TRUE;
4760 #endif
4762 int qemu_uuid_parse(const char *str, uint8_t *uuid)
4764 int ret;
4766 if(strlen(str) != 36)
4767 return -1;
4769 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4770 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4771 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4773 if(ret != 16)
4774 return -1;
4776 #ifdef TARGET_I386
4777 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4778 #endif
4780 return 0;
4783 #define MAX_NET_CLIENTS 32
4785 #ifndef _WIN32
4787 static void termsig_handler(int signal)
4789 qemu_system_shutdown_request();
4792 static void sigchld_handler(int signal)
4794 waitpid(-1, NULL, WNOHANG);
4797 static void sighandler_setup(void)
4799 struct sigaction act;
4801 memset(&act, 0, sizeof(act));
4802 act.sa_handler = termsig_handler;
4803 sigaction(SIGINT, &act, NULL);
4804 sigaction(SIGHUP, &act, NULL);
4805 sigaction(SIGTERM, &act, NULL);
4807 act.sa_handler = sigchld_handler;
4808 act.sa_flags = SA_NOCLDSTOP;
4809 sigaction(SIGCHLD, &act, NULL);
4812 #endif
4814 #ifdef _WIN32
4815 /* Look for support files in the same directory as the executable. */
4816 static char *find_datadir(const char *argv0)
4818 char *p;
4819 char buf[MAX_PATH];
4820 DWORD len;
4822 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4823 if (len == 0) {
4824 return NULL;
4827 buf[len] = 0;
4828 p = buf + len - 1;
4829 while (p != buf && *p != '\\')
4830 p--;
4831 *p = 0;
4832 if (access(buf, R_OK) == 0) {
4833 return qemu_strdup(buf);
4835 return NULL;
4837 #else /* !_WIN32 */
4839 /* Find a likely location for support files using the location of the binary.
4840 For installed binaries this will be "$bindir/../share/qemu". When
4841 running from the build tree this will be "$bindir/../pc-bios". */
4842 #define SHARE_SUFFIX "/share/qemu"
4843 #define BUILD_SUFFIX "/pc-bios"
4844 static char *find_datadir(const char *argv0)
4846 char *dir;
4847 char *p = NULL;
4848 char *res;
4849 #ifdef PATH_MAX
4850 char buf[PATH_MAX];
4851 #endif
4852 size_t max_len;
4854 #if defined(__linux__)
4856 int len;
4857 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4858 if (len > 0) {
4859 buf[len] = 0;
4860 p = buf;
4863 #elif defined(__FreeBSD__)
4865 int len;
4866 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4867 if (len > 0) {
4868 buf[len] = 0;
4869 p = buf;
4872 #endif
4873 /* If we don't have any way of figuring out the actual executable
4874 location then try argv[0]. */
4875 if (!p) {
4876 #ifdef PATH_MAX
4877 p = buf;
4878 #endif
4879 p = realpath(argv0, p);
4880 if (!p) {
4881 return NULL;
4884 dir = dirname(p);
4885 dir = dirname(dir);
4887 max_len = strlen(dir) +
4888 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4889 res = qemu_mallocz(max_len);
4890 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
4891 if (access(res, R_OK)) {
4892 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
4893 if (access(res, R_OK)) {
4894 qemu_free(res);
4895 res = NULL;
4898 #ifndef PATH_MAX
4899 free(p);
4900 #endif
4901 return res;
4903 #undef SHARE_SUFFIX
4904 #undef BUILD_SUFFIX
4905 #endif
4907 char *qemu_find_file(int type, const char *name)
4909 int len;
4910 const char *subdir;
4911 char *buf;
4913 /* If name contains path separators then try it as a straight path. */
4914 if ((strchr(name, '/') || strchr(name, '\\'))
4915 && access(name, R_OK) == 0) {
4916 return strdup(name);
4918 switch (type) {
4919 case QEMU_FILE_TYPE_BIOS:
4920 subdir = "";
4921 break;
4922 case QEMU_FILE_TYPE_KEYMAP:
4923 subdir = "keymaps/";
4924 break;
4925 default:
4926 abort();
4928 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4929 buf = qemu_mallocz(len);
4930 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
4931 if (access(buf, R_OK)) {
4932 qemu_free(buf);
4933 return NULL;
4935 return buf;
4938 int main(int argc, char **argv, char **envp)
4940 const char *gdbstub_dev = NULL;
4941 uint32_t boot_devices_bitmap = 0;
4942 int i;
4943 int snapshot, linux_boot, net_boot;
4944 const char *initrd_filename;
4945 const char *kernel_filename, *kernel_cmdline;
4946 const char *boot_devices = "";
4947 DisplayState *ds;
4948 DisplayChangeListener *dcl;
4949 int cyls, heads, secs, translation;
4950 const char *net_clients[MAX_NET_CLIENTS];
4951 int nb_net_clients;
4952 const char *bt_opts[MAX_BT_CMDLINE];
4953 int nb_bt_opts;
4954 int hda_index;
4955 int optind;
4956 const char *r, *optarg;
4957 CharDriverState *monitor_hd = NULL;
4958 const char *monitor_device;
4959 const char *serial_devices[MAX_SERIAL_PORTS];
4960 int serial_device_index;
4961 const char *parallel_devices[MAX_PARALLEL_PORTS];
4962 int parallel_device_index;
4963 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4964 int virtio_console_index;
4965 const char *loadvm = NULL;
4966 QEMUMachine *machine;
4967 const char *cpu_model;
4968 const char *usb_devices[MAX_USB_CMDLINE];
4969 int usb_devices_index;
4970 #ifndef _WIN32
4971 int fds[2];
4972 #endif
4973 int tb_size;
4974 const char *pid_file = NULL;
4975 const char *incoming = NULL;
4976 #ifndef _WIN32
4977 int fd = 0;
4978 struct passwd *pwd = NULL;
4979 const char *chroot_dir = NULL;
4980 const char *run_as = NULL;
4981 #endif
4982 CPUState *env;
4983 int show_vnc_port = 0;
4985 qemu_cache_utils_init(envp);
4987 LIST_INIT (&vm_change_state_head);
4988 #ifndef _WIN32
4990 struct sigaction act;
4991 sigfillset(&act.sa_mask);
4992 act.sa_flags = 0;
4993 act.sa_handler = SIG_IGN;
4994 sigaction(SIGPIPE, &act, NULL);
4996 #else
4997 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
4998 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4999 QEMU to run on a single CPU */
5001 HANDLE h;
5002 DWORD mask, smask;
5003 int i;
5004 h = GetCurrentProcess();
5005 if (GetProcessAffinityMask(h, &mask, &smask)) {
5006 for(i = 0; i < 32; i++) {
5007 if (mask & (1 << i))
5008 break;
5010 if (i != 32) {
5011 mask = 1 << i;
5012 SetProcessAffinityMask(h, mask);
5016 #endif
5018 module_call_init(MODULE_INIT_MACHINE);
5019 machine = find_default_machine();
5020 cpu_model = NULL;
5021 initrd_filename = NULL;
5022 ram_size = 0;
5023 snapshot = 0;
5024 kernel_filename = NULL;
5025 kernel_cmdline = "";
5026 cyls = heads = secs = 0;
5027 translation = BIOS_ATA_TRANSLATION_AUTO;
5028 monitor_device = "vc:80Cx24C";
5030 serial_devices[0] = "vc:80Cx24C";
5031 for(i = 1; i < MAX_SERIAL_PORTS; i++)
5032 serial_devices[i] = NULL;
5033 serial_device_index = 0;
5035 parallel_devices[0] = "vc:80Cx24C";
5036 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
5037 parallel_devices[i] = NULL;
5038 parallel_device_index = 0;
5040 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
5041 virtio_consoles[i] = NULL;
5042 virtio_console_index = 0;
5044 for (i = 0; i < MAX_NODES; i++) {
5045 node_mem[i] = 0;
5046 node_cpumask[i] = 0;
5049 usb_devices_index = 0;
5051 nb_net_clients = 0;
5052 nb_bt_opts = 0;
5053 nb_drives = 0;
5054 nb_drives_opt = 0;
5055 nb_numa_nodes = 0;
5056 hda_index = -1;
5058 nb_nics = 0;
5060 tb_size = 0;
5061 autostart= 1;
5063 register_watchdogs();
5065 optind = 1;
5066 for(;;) {
5067 if (optind >= argc)
5068 break;
5069 r = argv[optind];
5070 if (r[0] != '-') {
5071 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
5072 } else {
5073 const QEMUOption *popt;
5075 optind++;
5076 /* Treat --foo the same as -foo. */
5077 if (r[1] == '-')
5078 r++;
5079 popt = qemu_options;
5080 for(;;) {
5081 if (!popt->name) {
5082 fprintf(stderr, "%s: invalid option -- '%s'\n",
5083 argv[0], r);
5084 exit(1);
5086 if (!strcmp(popt->name, r + 1))
5087 break;
5088 popt++;
5090 if (popt->flags & HAS_ARG) {
5091 if (optind >= argc) {
5092 fprintf(stderr, "%s: option '%s' requires an argument\n",
5093 argv[0], r);
5094 exit(1);
5096 optarg = argv[optind++];
5097 } else {
5098 optarg = NULL;
5101 switch(popt->index) {
5102 case QEMU_OPTION_M:
5103 machine = find_machine(optarg);
5104 if (!machine) {
5105 QEMUMachine *m;
5106 printf("Supported machines are:\n");
5107 for(m = first_machine; m != NULL; m = m->next) {
5108 printf("%-10s %s%s\n",
5109 m->name, m->desc,
5110 m->is_default ? " (default)" : "");
5112 exit(*optarg != '?');
5114 break;
5115 case QEMU_OPTION_cpu:
5116 /* hw initialization will check this */
5117 if (*optarg == '?') {
5118 /* XXX: implement xxx_cpu_list for targets that still miss it */
5119 #if defined(cpu_list)
5120 cpu_list(stdout, &fprintf);
5121 #endif
5122 exit(0);
5123 } else {
5124 cpu_model = optarg;
5126 break;
5127 case QEMU_OPTION_initrd:
5128 initrd_filename = optarg;
5129 break;
5130 case QEMU_OPTION_hda:
5131 if (cyls == 0)
5132 hda_index = drive_add(optarg, HD_ALIAS, 0);
5133 else
5134 hda_index = drive_add(optarg, HD_ALIAS
5135 ",cyls=%d,heads=%d,secs=%d%s",
5136 0, cyls, heads, secs,
5137 translation == BIOS_ATA_TRANSLATION_LBA ?
5138 ",trans=lba" :
5139 translation == BIOS_ATA_TRANSLATION_NONE ?
5140 ",trans=none" : "");
5141 break;
5142 case QEMU_OPTION_hdb:
5143 case QEMU_OPTION_hdc:
5144 case QEMU_OPTION_hdd:
5145 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
5146 break;
5147 case QEMU_OPTION_drive:
5148 drive_add(NULL, "%s", optarg);
5149 break;
5150 case QEMU_OPTION_mtdblock:
5151 drive_add(optarg, MTD_ALIAS);
5152 break;
5153 case QEMU_OPTION_sd:
5154 drive_add(optarg, SD_ALIAS);
5155 break;
5156 case QEMU_OPTION_pflash:
5157 drive_add(optarg, PFLASH_ALIAS);
5158 break;
5159 case QEMU_OPTION_snapshot:
5160 snapshot = 1;
5161 break;
5162 case QEMU_OPTION_hdachs:
5164 const char *p;
5165 p = optarg;
5166 cyls = strtol(p, (char **)&p, 0);
5167 if (cyls < 1 || cyls > 16383)
5168 goto chs_fail;
5169 if (*p != ',')
5170 goto chs_fail;
5171 p++;
5172 heads = strtol(p, (char **)&p, 0);
5173 if (heads < 1 || heads > 16)
5174 goto chs_fail;
5175 if (*p != ',')
5176 goto chs_fail;
5177 p++;
5178 secs = strtol(p, (char **)&p, 0);
5179 if (secs < 1 || secs > 63)
5180 goto chs_fail;
5181 if (*p == ',') {
5182 p++;
5183 if (!strcmp(p, "none"))
5184 translation = BIOS_ATA_TRANSLATION_NONE;
5185 else if (!strcmp(p, "lba"))
5186 translation = BIOS_ATA_TRANSLATION_LBA;
5187 else if (!strcmp(p, "auto"))
5188 translation = BIOS_ATA_TRANSLATION_AUTO;
5189 else
5190 goto chs_fail;
5191 } else if (*p != '\0') {
5192 chs_fail:
5193 fprintf(stderr, "qemu: invalid physical CHS format\n");
5194 exit(1);
5196 if (hda_index != -1)
5197 snprintf(drives_opt[hda_index].opt,
5198 sizeof(drives_opt[hda_index].opt),
5199 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5200 0, cyls, heads, secs,
5201 translation == BIOS_ATA_TRANSLATION_LBA ?
5202 ",trans=lba" :
5203 translation == BIOS_ATA_TRANSLATION_NONE ?
5204 ",trans=none" : "");
5206 break;
5207 case QEMU_OPTION_numa:
5208 if (nb_numa_nodes >= MAX_NODES) {
5209 fprintf(stderr, "qemu: too many NUMA nodes\n");
5210 exit(1);
5212 numa_add(optarg);
5213 break;
5214 case QEMU_OPTION_nographic:
5215 display_type = DT_NOGRAPHIC;
5216 break;
5217 #ifdef CONFIG_CURSES
5218 case QEMU_OPTION_curses:
5219 display_type = DT_CURSES;
5220 break;
5221 #endif
5222 case QEMU_OPTION_portrait:
5223 graphic_rotate = 1;
5224 break;
5225 case QEMU_OPTION_kernel:
5226 kernel_filename = optarg;
5227 break;
5228 case QEMU_OPTION_append:
5229 kernel_cmdline = optarg;
5230 break;
5231 case QEMU_OPTION_cdrom:
5232 drive_add(optarg, CDROM_ALIAS);
5233 break;
5234 case QEMU_OPTION_boot:
5235 boot_devices = optarg;
5236 /* We just do some generic consistency checks */
5238 /* Could easily be extended to 64 devices if needed */
5239 const char *p;
5241 boot_devices_bitmap = 0;
5242 for (p = boot_devices; *p != '\0'; p++) {
5243 /* Allowed boot devices are:
5244 * a b : floppy disk drives
5245 * c ... f : IDE disk drives
5246 * g ... m : machine implementation dependant drives
5247 * n ... p : network devices
5248 * It's up to each machine implementation to check
5249 * if the given boot devices match the actual hardware
5250 * implementation and firmware features.
5252 if (*p < 'a' || *p > 'q') {
5253 fprintf(stderr, "Invalid boot device '%c'\n", *p);
5254 exit(1);
5256 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5257 fprintf(stderr,
5258 "Boot device '%c' was given twice\n",*p);
5259 exit(1);
5261 boot_devices_bitmap |= 1 << (*p - 'a');
5264 break;
5265 case QEMU_OPTION_fda:
5266 case QEMU_OPTION_fdb:
5267 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
5268 break;
5269 #ifdef TARGET_I386
5270 case QEMU_OPTION_no_fd_bootchk:
5271 fd_bootchk = 0;
5272 break;
5273 #endif
5274 case QEMU_OPTION_net:
5275 if (nb_net_clients >= MAX_NET_CLIENTS) {
5276 fprintf(stderr, "qemu: too many network clients\n");
5277 exit(1);
5279 net_clients[nb_net_clients] = optarg;
5280 nb_net_clients++;
5281 break;
5282 #ifdef CONFIG_SLIRP
5283 case QEMU_OPTION_tftp:
5284 tftp_prefix = optarg;
5285 break;
5286 case QEMU_OPTION_bootp:
5287 bootp_filename = optarg;
5288 break;
5289 #ifndef _WIN32
5290 case QEMU_OPTION_smb:
5291 net_slirp_smb(optarg);
5292 break;
5293 #endif
5294 case QEMU_OPTION_redir:
5295 net_slirp_redir(NULL, optarg, NULL);
5296 break;
5297 #endif
5298 case QEMU_OPTION_bt:
5299 if (nb_bt_opts >= MAX_BT_CMDLINE) {
5300 fprintf(stderr, "qemu: too many bluetooth options\n");
5301 exit(1);
5303 bt_opts[nb_bt_opts++] = optarg;
5304 break;
5305 #ifdef HAS_AUDIO
5306 case QEMU_OPTION_audio_help:
5307 AUD_help ();
5308 exit (0);
5309 break;
5310 case QEMU_OPTION_soundhw:
5311 select_soundhw (optarg);
5312 break;
5313 #endif
5314 case QEMU_OPTION_h:
5315 help(0);
5316 break;
5317 case QEMU_OPTION_version:
5318 version();
5319 exit(0);
5320 break;
5321 case QEMU_OPTION_m: {
5322 uint64_t value;
5323 char *ptr;
5325 value = strtoul(optarg, &ptr, 10);
5326 switch (*ptr) {
5327 case 0: case 'M': case 'm':
5328 value <<= 20;
5329 break;
5330 case 'G': case 'g':
5331 value <<= 30;
5332 break;
5333 default:
5334 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
5335 exit(1);
5338 /* On 32-bit hosts, QEMU is limited by virtual address space */
5339 if (value > (2047 << 20)
5340 #ifndef CONFIG_KQEMU
5341 && HOST_LONG_BITS == 32
5342 #endif
5344 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5345 exit(1);
5347 if (value != (uint64_t)(ram_addr_t)value) {
5348 fprintf(stderr, "qemu: ram size too large\n");
5349 exit(1);
5351 ram_size = value;
5352 break;
5354 case QEMU_OPTION_d:
5356 int mask;
5357 const CPULogItem *item;
5359 mask = cpu_str_to_log_mask(optarg);
5360 if (!mask) {
5361 printf("Log items (comma separated):\n");
5362 for(item = cpu_log_items; item->mask != 0; item++) {
5363 printf("%-10s %s\n", item->name, item->help);
5365 exit(1);
5367 cpu_set_log(mask);
5369 break;
5370 case QEMU_OPTION_s:
5371 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
5372 break;
5373 case QEMU_OPTION_gdb:
5374 gdbstub_dev = optarg;
5375 break;
5376 case QEMU_OPTION_L:
5377 data_dir = optarg;
5378 break;
5379 case QEMU_OPTION_bios:
5380 bios_name = optarg;
5381 break;
5382 case QEMU_OPTION_singlestep:
5383 singlestep = 1;
5384 break;
5385 case QEMU_OPTION_S:
5386 autostart = 0;
5387 break;
5388 #ifndef _WIN32
5389 case QEMU_OPTION_k:
5390 keyboard_layout = optarg;
5391 break;
5392 #endif
5393 case QEMU_OPTION_localtime:
5394 rtc_utc = 0;
5395 break;
5396 case QEMU_OPTION_vga:
5397 select_vgahw (optarg);
5398 break;
5399 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5400 case QEMU_OPTION_g:
5402 const char *p;
5403 int w, h, depth;
5404 p = optarg;
5405 w = strtol(p, (char **)&p, 10);
5406 if (w <= 0) {
5407 graphic_error:
5408 fprintf(stderr, "qemu: invalid resolution or depth\n");
5409 exit(1);
5411 if (*p != 'x')
5412 goto graphic_error;
5413 p++;
5414 h = strtol(p, (char **)&p, 10);
5415 if (h <= 0)
5416 goto graphic_error;
5417 if (*p == 'x') {
5418 p++;
5419 depth = strtol(p, (char **)&p, 10);
5420 if (depth != 8 && depth != 15 && depth != 16 &&
5421 depth != 24 && depth != 32)
5422 goto graphic_error;
5423 } else if (*p == '\0') {
5424 depth = graphic_depth;
5425 } else {
5426 goto graphic_error;
5429 graphic_width = w;
5430 graphic_height = h;
5431 graphic_depth = depth;
5433 break;
5434 #endif
5435 case QEMU_OPTION_echr:
5437 char *r;
5438 term_escape_char = strtol(optarg, &r, 0);
5439 if (r == optarg)
5440 printf("Bad argument to echr\n");
5441 break;
5443 case QEMU_OPTION_monitor:
5444 monitor_device = optarg;
5445 break;
5446 case QEMU_OPTION_serial:
5447 if (serial_device_index >= MAX_SERIAL_PORTS) {
5448 fprintf(stderr, "qemu: too many serial ports\n");
5449 exit(1);
5451 serial_devices[serial_device_index] = optarg;
5452 serial_device_index++;
5453 break;
5454 case QEMU_OPTION_watchdog:
5455 i = select_watchdog(optarg);
5456 if (i > 0)
5457 exit (i == 1 ? 1 : 0);
5458 break;
5459 case QEMU_OPTION_watchdog_action:
5460 if (select_watchdog_action(optarg) == -1) {
5461 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5462 exit(1);
5464 break;
5465 case QEMU_OPTION_virtiocon:
5466 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5467 fprintf(stderr, "qemu: too many virtio consoles\n");
5468 exit(1);
5470 virtio_consoles[virtio_console_index] = optarg;
5471 virtio_console_index++;
5472 break;
5473 case QEMU_OPTION_parallel:
5474 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5475 fprintf(stderr, "qemu: too many parallel ports\n");
5476 exit(1);
5478 parallel_devices[parallel_device_index] = optarg;
5479 parallel_device_index++;
5480 break;
5481 case QEMU_OPTION_loadvm:
5482 loadvm = optarg;
5483 break;
5484 case QEMU_OPTION_full_screen:
5485 full_screen = 1;
5486 break;
5487 #ifdef CONFIG_SDL
5488 case QEMU_OPTION_no_frame:
5489 no_frame = 1;
5490 break;
5491 case QEMU_OPTION_alt_grab:
5492 alt_grab = 1;
5493 break;
5494 case QEMU_OPTION_no_quit:
5495 no_quit = 1;
5496 break;
5497 case QEMU_OPTION_sdl:
5498 display_type = DT_SDL;
5499 break;
5500 #endif
5501 case QEMU_OPTION_pidfile:
5502 pid_file = optarg;
5503 break;
5504 #ifdef TARGET_I386
5505 case QEMU_OPTION_win2k_hack:
5506 win2k_install_hack = 1;
5507 break;
5508 case QEMU_OPTION_rtc_td_hack:
5509 rtc_td_hack = 1;
5510 break;
5511 case QEMU_OPTION_acpitable:
5512 if(acpi_table_add(optarg) < 0) {
5513 fprintf(stderr, "Wrong acpi table provided\n");
5514 exit(1);
5516 break;
5517 case QEMU_OPTION_smbios:
5518 if(smbios_entry_add(optarg) < 0) {
5519 fprintf(stderr, "Wrong smbios provided\n");
5520 exit(1);
5522 break;
5523 #endif
5524 #ifdef CONFIG_KQEMU
5525 case QEMU_OPTION_no_kqemu:
5526 kqemu_allowed = 0;
5527 break;
5528 case QEMU_OPTION_kernel_kqemu:
5529 kqemu_allowed = 2;
5530 break;
5531 #endif
5532 #ifdef CONFIG_KVM
5533 case QEMU_OPTION_enable_kvm:
5534 kvm_allowed = 1;
5535 #ifdef CONFIG_KQEMU
5536 kqemu_allowed = 0;
5537 #endif
5538 break;
5539 #endif
5540 case QEMU_OPTION_usb:
5541 usb_enabled = 1;
5542 break;
5543 case QEMU_OPTION_usbdevice:
5544 usb_enabled = 1;
5545 if (usb_devices_index >= MAX_USB_CMDLINE) {
5546 fprintf(stderr, "Too many USB devices\n");
5547 exit(1);
5549 usb_devices[usb_devices_index] = optarg;
5550 usb_devices_index++;
5551 break;
5552 case QEMU_OPTION_smp:
5553 smp_cpus = atoi(optarg);
5554 if (smp_cpus < 1) {
5555 fprintf(stderr, "Invalid number of CPUs\n");
5556 exit(1);
5558 break;
5559 case QEMU_OPTION_vnc:
5560 display_type = DT_VNC;
5561 vnc_display = optarg;
5562 break;
5563 #ifdef TARGET_I386
5564 case QEMU_OPTION_no_acpi:
5565 acpi_enabled = 0;
5566 break;
5567 case QEMU_OPTION_no_hpet:
5568 no_hpet = 1;
5569 break;
5570 case QEMU_OPTION_no_virtio_balloon:
5571 no_virtio_balloon = 1;
5572 break;
5573 #endif
5574 case QEMU_OPTION_no_reboot:
5575 no_reboot = 1;
5576 break;
5577 case QEMU_OPTION_no_shutdown:
5578 no_shutdown = 1;
5579 break;
5580 case QEMU_OPTION_show_cursor:
5581 cursor_hide = 0;
5582 break;
5583 case QEMU_OPTION_uuid:
5584 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5585 fprintf(stderr, "Fail to parse UUID string."
5586 " Wrong format.\n");
5587 exit(1);
5589 break;
5590 #ifndef _WIN32
5591 case QEMU_OPTION_daemonize:
5592 daemonize = 1;
5593 break;
5594 #endif
5595 case QEMU_OPTION_option_rom:
5596 if (nb_option_roms >= MAX_OPTION_ROMS) {
5597 fprintf(stderr, "Too many option ROMs\n");
5598 exit(1);
5600 option_rom[nb_option_roms] = optarg;
5601 nb_option_roms++;
5602 break;
5603 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5604 case QEMU_OPTION_semihosting:
5605 semihosting_enabled = 1;
5606 break;
5607 #endif
5608 case QEMU_OPTION_name:
5609 qemu_name = optarg;
5610 break;
5611 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5612 case QEMU_OPTION_prom_env:
5613 if (nb_prom_envs >= MAX_PROM_ENVS) {
5614 fprintf(stderr, "Too many prom variables\n");
5615 exit(1);
5617 prom_envs[nb_prom_envs] = optarg;
5618 nb_prom_envs++;
5619 break;
5620 #endif
5621 #ifdef TARGET_ARM
5622 case QEMU_OPTION_old_param:
5623 old_param = 1;
5624 break;
5625 #endif
5626 case QEMU_OPTION_clock:
5627 configure_alarms(optarg);
5628 break;
5629 case QEMU_OPTION_startdate:
5631 struct tm tm;
5632 time_t rtc_start_date;
5633 if (!strcmp(optarg, "now")) {
5634 rtc_date_offset = -1;
5635 } else {
5636 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5637 &tm.tm_year,
5638 &tm.tm_mon,
5639 &tm.tm_mday,
5640 &tm.tm_hour,
5641 &tm.tm_min,
5642 &tm.tm_sec) == 6) {
5643 /* OK */
5644 } else if (sscanf(optarg, "%d-%d-%d",
5645 &tm.tm_year,
5646 &tm.tm_mon,
5647 &tm.tm_mday) == 3) {
5648 tm.tm_hour = 0;
5649 tm.tm_min = 0;
5650 tm.tm_sec = 0;
5651 } else {
5652 goto date_fail;
5654 tm.tm_year -= 1900;
5655 tm.tm_mon--;
5656 rtc_start_date = mktimegm(&tm);
5657 if (rtc_start_date == -1) {
5658 date_fail:
5659 fprintf(stderr, "Invalid date format. Valid format are:\n"
5660 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5661 exit(1);
5663 rtc_date_offset = time(NULL) - rtc_start_date;
5666 break;
5667 case QEMU_OPTION_tb_size:
5668 tb_size = strtol(optarg, NULL, 0);
5669 if (tb_size < 0)
5670 tb_size = 0;
5671 break;
5672 case QEMU_OPTION_icount:
5673 use_icount = 1;
5674 if (strcmp(optarg, "auto") == 0) {
5675 icount_time_shift = -1;
5676 } else {
5677 icount_time_shift = strtol(optarg, NULL, 0);
5679 break;
5680 case QEMU_OPTION_incoming:
5681 incoming = optarg;
5682 break;
5683 #ifndef _WIN32
5684 case QEMU_OPTION_chroot:
5685 chroot_dir = optarg;
5686 break;
5687 case QEMU_OPTION_runas:
5688 run_as = optarg;
5689 break;
5690 #endif
5691 #ifdef CONFIG_XEN
5692 case QEMU_OPTION_xen_domid:
5693 xen_domid = atoi(optarg);
5694 break;
5695 case QEMU_OPTION_xen_create:
5696 xen_mode = XEN_CREATE;
5697 break;
5698 case QEMU_OPTION_xen_attach:
5699 xen_mode = XEN_ATTACH;
5700 break;
5701 #endif
5706 /* If no data_dir is specified then try to find it relative to the
5707 executable path. */
5708 if (!data_dir) {
5709 data_dir = find_datadir(argv[0]);
5711 /* If all else fails use the install patch specified when building. */
5712 if (!data_dir) {
5713 data_dir = CONFIG_QEMU_SHAREDIR;
5716 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5717 if (kvm_allowed && kqemu_allowed) {
5718 fprintf(stderr,
5719 "You can not enable both KVM and kqemu at the same time\n");
5720 exit(1);
5722 #endif
5724 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5725 if (smp_cpus > machine->max_cpus) {
5726 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5727 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5728 machine->max_cpus);
5729 exit(1);
5732 if (display_type == DT_NOGRAPHIC) {
5733 if (serial_device_index == 0)
5734 serial_devices[0] = "stdio";
5735 if (parallel_device_index == 0)
5736 parallel_devices[0] = "null";
5737 if (strncmp(monitor_device, "vc", 2) == 0)
5738 monitor_device = "stdio";
5741 #ifndef _WIN32
5742 if (daemonize) {
5743 pid_t pid;
5745 if (pipe(fds) == -1)
5746 exit(1);
5748 pid = fork();
5749 if (pid > 0) {
5750 uint8_t status;
5751 ssize_t len;
5753 close(fds[1]);
5755 again:
5756 len = read(fds[0], &status, 1);
5757 if (len == -1 && (errno == EINTR))
5758 goto again;
5760 if (len != 1)
5761 exit(1);
5762 else if (status == 1) {
5763 fprintf(stderr, "Could not acquire pidfile\n");
5764 exit(1);
5765 } else
5766 exit(0);
5767 } else if (pid < 0)
5768 exit(1);
5770 setsid();
5772 pid = fork();
5773 if (pid > 0)
5774 exit(0);
5775 else if (pid < 0)
5776 exit(1);
5778 umask(027);
5780 signal(SIGTSTP, SIG_IGN);
5781 signal(SIGTTOU, SIG_IGN);
5782 signal(SIGTTIN, SIG_IGN);
5785 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5786 if (daemonize) {
5787 uint8_t status = 1;
5788 write(fds[1], &status, 1);
5789 } else
5790 fprintf(stderr, "Could not acquire pid file\n");
5791 exit(1);
5793 #endif
5795 #ifdef CONFIG_KQEMU
5796 if (smp_cpus > 1)
5797 kqemu_allowed = 0;
5798 #endif
5799 if (qemu_init_main_loop()) {
5800 fprintf(stderr, "qemu_init_main_loop failed\n");
5801 exit(1);
5803 linux_boot = (kernel_filename != NULL);
5804 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5806 if (!linux_boot && *kernel_cmdline != '\0') {
5807 fprintf(stderr, "-append only allowed with -kernel option\n");
5808 exit(1);
5811 if (!linux_boot && initrd_filename != NULL) {
5812 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5813 exit(1);
5816 /* boot to floppy or the default cd if no hard disk defined yet */
5817 if (!boot_devices[0]) {
5818 boot_devices = "cad";
5820 setvbuf(stdout, NULL, _IOLBF, 0);
5822 init_timers();
5823 if (init_timer_alarm() < 0) {
5824 fprintf(stderr, "could not initialize alarm timer\n");
5825 exit(1);
5827 if (use_icount && icount_time_shift < 0) {
5828 use_icount = 2;
5829 /* 125MIPS seems a reasonable initial guess at the guest speed.
5830 It will be corrected fairly quickly anyway. */
5831 icount_time_shift = 3;
5832 init_icount_adjust();
5835 #ifdef _WIN32
5836 socket_init();
5837 #endif
5839 /* init network clients */
5840 if (nb_net_clients == 0) {
5841 /* if no clients, we use a default config */
5842 net_clients[nb_net_clients++] = "nic";
5843 #ifdef CONFIG_SLIRP
5844 net_clients[nb_net_clients++] = "user";
5845 #endif
5848 for(i = 0;i < nb_net_clients; i++) {
5849 if (net_client_parse(net_clients[i]) < 0)
5850 exit(1);
5852 net_client_check();
5854 #ifdef TARGET_I386
5855 /* XXX: this should be moved in the PC machine instantiation code */
5856 if (net_boot != 0) {
5857 int netroms = 0;
5858 for (i = 0; i < nb_nics && i < 4; i++) {
5859 const char *model = nd_table[i].model;
5860 char buf[1024];
5861 char *filename;
5862 if (net_boot & (1 << i)) {
5863 if (model == NULL)
5864 model = "ne2k_pci";
5865 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5866 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5867 if (filename && get_image_size(filename) > 0) {
5868 if (nb_option_roms >= MAX_OPTION_ROMS) {
5869 fprintf(stderr, "Too many option ROMs\n");
5870 exit(1);
5872 option_rom[nb_option_roms] = qemu_strdup(buf);
5873 nb_option_roms++;
5874 netroms++;
5876 if (filename) {
5877 qemu_free(filename);
5881 if (netroms == 0) {
5882 fprintf(stderr, "No valid PXE rom found for network device\n");
5883 exit(1);
5886 #endif
5888 /* init the bluetooth world */
5889 for (i = 0; i < nb_bt_opts; i++)
5890 if (bt_parse(bt_opts[i]))
5891 exit(1);
5893 /* init the memory */
5894 if (ram_size == 0)
5895 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5897 #ifdef CONFIG_KQEMU
5898 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5899 guest ram allocation. It needs to go away. */
5900 if (kqemu_allowed) {
5901 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5902 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5903 if (!kqemu_phys_ram_base) {
5904 fprintf(stderr, "Could not allocate physical memory\n");
5905 exit(1);
5908 #endif
5910 /* init the dynamic translator */
5911 cpu_exec_init_all(tb_size * 1024 * 1024);
5913 bdrv_init();
5915 /* we always create the cdrom drive, even if no disk is there */
5917 if (nb_drives_opt < MAX_DRIVES)
5918 drive_add(NULL, CDROM_ALIAS);
5920 /* we always create at least one floppy */
5922 if (nb_drives_opt < MAX_DRIVES)
5923 drive_add(NULL, FD_ALIAS, 0);
5925 /* we always create one sd slot, even if no card is in it */
5927 if (nb_drives_opt < MAX_DRIVES)
5928 drive_add(NULL, SD_ALIAS);
5930 /* open the virtual block devices */
5932 for(i = 0; i < nb_drives_opt; i++)
5933 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
5934 exit(1);
5936 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
5937 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5939 #ifndef _WIN32
5940 /* must be after terminal init, SDL library changes signal handlers */
5941 sighandler_setup();
5942 #endif
5944 /* Maintain compatibility with multiple stdio monitors */
5945 if (!strcmp(monitor_device,"stdio")) {
5946 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5947 const char *devname = serial_devices[i];
5948 if (devname && !strcmp(devname,"mon:stdio")) {
5949 monitor_device = NULL;
5950 break;
5951 } else if (devname && !strcmp(devname,"stdio")) {
5952 monitor_device = NULL;
5953 serial_devices[i] = "mon:stdio";
5954 break;
5959 if (nb_numa_nodes > 0) {
5960 int i;
5962 if (nb_numa_nodes > smp_cpus) {
5963 nb_numa_nodes = smp_cpus;
5966 /* If no memory size if given for any node, assume the default case
5967 * and distribute the available memory equally across all nodes
5969 for (i = 0; i < nb_numa_nodes; i++) {
5970 if (node_mem[i] != 0)
5971 break;
5973 if (i == nb_numa_nodes) {
5974 uint64_t usedmem = 0;
5976 /* On Linux, the each node's border has to be 8MB aligned,
5977 * the final node gets the rest.
5979 for (i = 0; i < nb_numa_nodes - 1; i++) {
5980 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5981 usedmem += node_mem[i];
5983 node_mem[i] = ram_size - usedmem;
5986 for (i = 0; i < nb_numa_nodes; i++) {
5987 if (node_cpumask[i] != 0)
5988 break;
5990 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5991 * must cope with this anyway, because there are BIOSes out there in
5992 * real machines which also use this scheme.
5994 if (i == nb_numa_nodes) {
5995 for (i = 0; i < smp_cpus; i++) {
5996 node_cpumask[i % nb_numa_nodes] |= 1 << i;
6001 if (kvm_enabled()) {
6002 int ret;
6004 ret = kvm_init(smp_cpus);
6005 if (ret < 0) {
6006 fprintf(stderr, "failed to initialize KVM\n");
6007 exit(1);
6011 if (monitor_device) {
6012 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
6013 if (!monitor_hd) {
6014 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
6015 exit(1);
6019 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6020 const char *devname = serial_devices[i];
6021 if (devname && strcmp(devname, "none")) {
6022 char label[32];
6023 snprintf(label, sizeof(label), "serial%d", i);
6024 serial_hds[i] = qemu_chr_open(label, devname, NULL);
6025 if (!serial_hds[i]) {
6026 fprintf(stderr, "qemu: could not open serial device '%s'\n",
6027 devname);
6028 exit(1);
6033 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6034 const char *devname = parallel_devices[i];
6035 if (devname && strcmp(devname, "none")) {
6036 char label[32];
6037 snprintf(label, sizeof(label), "parallel%d", i);
6038 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
6039 if (!parallel_hds[i]) {
6040 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
6041 devname);
6042 exit(1);
6047 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6048 const char *devname = virtio_consoles[i];
6049 if (devname && strcmp(devname, "none")) {
6050 char label[32];
6051 snprintf(label, sizeof(label), "virtcon%d", i);
6052 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
6053 if (!virtcon_hds[i]) {
6054 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
6055 devname);
6056 exit(1);
6061 module_call_init(MODULE_INIT_DEVICE);
6063 machine->init(ram_size, boot_devices,
6064 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6067 for (env = first_cpu; env != NULL; env = env->next_cpu) {
6068 for (i = 0; i < nb_numa_nodes; i++) {
6069 if (node_cpumask[i] & (1 << env->cpu_index)) {
6070 env->numa_node = i;
6075 current_machine = machine;
6077 /* Set KVM's vcpu state to qemu's initial CPUState. */
6078 if (kvm_enabled()) {
6079 int ret;
6081 ret = kvm_sync_vcpus();
6082 if (ret < 0) {
6083 fprintf(stderr, "failed to initialize vcpus\n");
6084 exit(1);
6088 /* init USB devices */
6089 if (usb_enabled) {
6090 for(i = 0; i < usb_devices_index; i++) {
6091 if (usb_device_add(usb_devices[i], 0) < 0) {
6092 fprintf(stderr, "Warning: could not add USB device %s\n",
6093 usb_devices[i]);
6098 if (!display_state)
6099 dumb_display_init();
6100 /* just use the first displaystate for the moment */
6101 ds = display_state;
6103 if (display_type == DT_DEFAULT) {
6104 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6105 display_type = DT_SDL;
6106 #else
6107 display_type = DT_VNC;
6108 vnc_display = "localhost:0,to=99";
6109 show_vnc_port = 1;
6110 #endif
6114 switch (display_type) {
6115 case DT_NOGRAPHIC:
6116 break;
6117 #if defined(CONFIG_CURSES)
6118 case DT_CURSES:
6119 curses_display_init(ds, full_screen);
6120 break;
6121 #endif
6122 #if defined(CONFIG_SDL)
6123 case DT_SDL:
6124 sdl_display_init(ds, full_screen, no_frame);
6125 break;
6126 #elif defined(CONFIG_COCOA)
6127 case DT_SDL:
6128 cocoa_display_init(ds, full_screen);
6129 break;
6130 #endif
6131 case DT_VNC:
6132 vnc_display_init(ds);
6133 if (vnc_display_open(ds, vnc_display) < 0)
6134 exit(1);
6136 if (show_vnc_port) {
6137 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
6139 break;
6140 default:
6141 break;
6143 dpy_resize(ds);
6145 dcl = ds->listeners;
6146 while (dcl != NULL) {
6147 if (dcl->dpy_refresh != NULL) {
6148 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6149 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
6151 dcl = dcl->next;
6154 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
6155 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6156 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6159 text_consoles_set_display(display_state);
6160 qemu_chr_initial_reset();
6162 if (monitor_device && monitor_hd)
6163 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
6165 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6166 const char *devname = serial_devices[i];
6167 if (devname && strcmp(devname, "none")) {
6168 if (strstart(devname, "vc", 0))
6169 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
6173 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6174 const char *devname = parallel_devices[i];
6175 if (devname && strcmp(devname, "none")) {
6176 if (strstart(devname, "vc", 0))
6177 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
6181 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6182 const char *devname = virtio_consoles[i];
6183 if (virtcon_hds[i] && devname) {
6184 if (strstart(devname, "vc", 0))
6185 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6189 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6190 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6191 gdbstub_dev);
6192 exit(1);
6195 if (loadvm)
6196 do_loadvm(cur_mon, loadvm);
6198 if (incoming) {
6199 autostart = 0; /* fixme how to deal with -daemonize */
6200 qemu_start_incoming_migration(incoming);
6203 if (autostart)
6204 vm_start();
6206 #ifndef _WIN32
6207 if (daemonize) {
6208 uint8_t status = 0;
6209 ssize_t len;
6211 again1:
6212 len = write(fds[1], &status, 1);
6213 if (len == -1 && (errno == EINTR))
6214 goto again1;
6216 if (len != 1)
6217 exit(1);
6219 chdir("/");
6220 TFR(fd = open("/dev/null", O_RDWR));
6221 if (fd == -1)
6222 exit(1);
6225 if (run_as) {
6226 pwd = getpwnam(run_as);
6227 if (!pwd) {
6228 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6229 exit(1);
6233 if (chroot_dir) {
6234 if (chroot(chroot_dir) < 0) {
6235 fprintf(stderr, "chroot failed\n");
6236 exit(1);
6238 chdir("/");
6241 if (run_as) {
6242 if (setgid(pwd->pw_gid) < 0) {
6243 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6244 exit(1);
6246 if (setuid(pwd->pw_uid) < 0) {
6247 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6248 exit(1);
6250 if (setuid(0) != -1) {
6251 fprintf(stderr, "Dropping privileges failed\n");
6252 exit(1);
6256 if (daemonize) {
6257 dup2(fd, 0);
6258 dup2(fd, 1);
6259 dup2(fd, 2);
6261 close(fd);
6263 #endif
6265 main_loop();
6266 quit_timers();
6267 net_cleanup();
6269 return 0;