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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <linux/rtc.h>
63 #include <sys/prctl.h>
65 /* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67 /* #include <linux/hpet.h> */
70 #include <linux/ppdev.h>
71 #include <linux/parport.h>
75 #include <sys/ethernet.h>
76 #include <sys/sockio.h>
77 #include <netinet/arp.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> // must come after ip.h
82 #include <netinet/udp.h>
83 #include <netinet/tcp.h>
87 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89 extern int madvise(caddr_t
, size_t, int);
94 #if defined(__OpenBSD__)
98 #if defined(CONFIG_VDE)
99 #include <libvdeplug.h>
104 #include <mmsystem.h>
108 #if defined(__APPLE__) || defined(main)
110 int qemu_main(int argc
, char **argv
, char **envp
);
111 int main(int argc
, char **argv
)
113 return qemu_main(argc
, argv
, NULL
);
116 #define main qemu_main
118 #endif /* CONFIG_SDL */
122 #define main qemu_main
123 #endif /* CONFIG_COCOA */
126 #include "hw/boards.h"
128 #include "hw/pcmcia.h"
130 #include "hw/audiodev.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "block_int.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
157 #include "qemu-option.h"
158 #include "qemu-config.h"
162 #include "exec-all.h"
164 #include "qemu_socket.h"
166 #include "slirp/libslirp.h"
168 #include "qemu-queue.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 /* Maximum number of monitor devices */
176 #define MAX_MONITOR_DEVICES 10
178 static const char *data_dir
;
179 const char *bios_name
= NULL
;
180 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
181 to store the VM snapshots */
182 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
183 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
184 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
185 static DisplayState
*display_state
;
186 DisplayType display_type
= DT_DEFAULT
;
187 const char* keyboard_layout
= NULL
;
190 NICInfo nd_table
[MAX_NICS
];
193 static int rtc_utc
= 1;
194 static int rtc_date_offset
= -1; /* -1 means no change */
195 QEMUClock
*rtc_clock
;
196 int vga_interface_type
= VGA_CIRRUS
;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static int timer_alarm_pending
= 1;
259 /* Conversion factor from emulated instructions to virtual clock ticks. */
260 static int icount_time_shift
;
261 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
262 #define MAX_ICOUNT_SHIFT 10
263 /* Compensate for varying guest execution speed. */
264 static int64_t qemu_icount_bias
;
265 static QEMUTimer
*icount_rt_timer
;
266 static QEMUTimer
*icount_vm_timer
;
267 static QEMUTimer
*nographic_timer
;
269 uint8_t qemu_uuid
[16];
271 static QEMUBootSetHandler
*boot_set_handler
;
272 static void *boot_set_opaque
;
274 static int default_serial
= 1;
275 static int default_parallel
= 1;
281 { .driver
= "isa-serial", .flag
= &default_serial
},
282 { .driver
= "isa-parallel", .flag
= &default_parallel
},
285 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
287 const char *driver
= qemu_opt_get(opts
, "driver");
292 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
293 if (strcmp(default_list
[i
].driver
, driver
) != 0)
295 *(default_list
[i
].flag
) = 0;
300 /***********************************************************/
301 /* x86 ISA bus support */
303 target_phys_addr_t isa_mem_base
= 0;
306 /***********************************************************/
307 void hw_error(const char *fmt
, ...)
313 fprintf(stderr
, "qemu: hardware error: ");
314 vfprintf(stderr
, fmt
, ap
);
315 fprintf(stderr
, "\n");
316 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
317 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
319 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
321 cpu_dump_state(env
, stderr
, fprintf
, 0);
328 static void set_proc_name(const char *s
)
330 #if defined(__linux__) && defined(PR_SET_NAME)
334 name
[sizeof(name
) - 1] = 0;
335 strncpy(name
, s
, sizeof(name
));
336 /* Could rewrite argv[0] too, but that's a bit more complicated.
337 This simple way is enough for `top'. */
338 prctl(PR_SET_NAME
, name
);
345 static QEMUBalloonEvent
*qemu_balloon_event
;
346 void *qemu_balloon_event_opaque
;
348 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
350 qemu_balloon_event
= func
;
351 qemu_balloon_event_opaque
= opaque
;
354 void qemu_balloon(ram_addr_t target
)
356 if (qemu_balloon_event
)
357 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
360 ram_addr_t
qemu_balloon_status(void)
362 if (qemu_balloon_event
)
363 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
367 /***********************************************************/
370 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
371 static void *qemu_put_kbd_event_opaque
;
372 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
373 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
375 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
377 qemu_put_kbd_event_opaque
= opaque
;
378 qemu_put_kbd_event
= func
;
381 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
382 void *opaque
, int absolute
,
385 QEMUPutMouseEntry
*s
, *cursor
;
387 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
389 s
->qemu_put_mouse_event
= func
;
390 s
->qemu_put_mouse_event_opaque
= opaque
;
391 s
->qemu_put_mouse_event_absolute
= absolute
;
392 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
395 if (!qemu_put_mouse_event_head
) {
396 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
400 cursor
= qemu_put_mouse_event_head
;
401 while (cursor
->next
!= NULL
)
402 cursor
= cursor
->next
;
405 qemu_put_mouse_event_current
= s
;
410 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
412 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
414 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
417 cursor
= qemu_put_mouse_event_head
;
418 while (cursor
!= NULL
&& cursor
!= entry
) {
420 cursor
= cursor
->next
;
423 if (cursor
== NULL
) // does not exist or list empty
425 else if (prev
== NULL
) { // entry is head
426 qemu_put_mouse_event_head
= cursor
->next
;
427 if (qemu_put_mouse_event_current
== entry
)
428 qemu_put_mouse_event_current
= cursor
->next
;
429 qemu_free(entry
->qemu_put_mouse_event_name
);
434 prev
->next
= entry
->next
;
436 if (qemu_put_mouse_event_current
== entry
)
437 qemu_put_mouse_event_current
= prev
;
439 qemu_free(entry
->qemu_put_mouse_event_name
);
443 void kbd_put_keycode(int keycode
)
445 if (qemu_put_kbd_event
) {
446 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
450 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
452 QEMUPutMouseEvent
*mouse_event
;
453 void *mouse_event_opaque
;
456 if (!qemu_put_mouse_event_current
) {
461 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
463 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
466 if (graphic_rotate
) {
467 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
470 width
= graphic_width
- 1;
471 mouse_event(mouse_event_opaque
,
472 width
- dy
, dx
, dz
, buttons_state
);
474 mouse_event(mouse_event_opaque
,
475 dx
, dy
, dz
, buttons_state
);
479 int kbd_mouse_is_absolute(void)
481 if (!qemu_put_mouse_event_current
)
484 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
487 void do_info_mice(Monitor
*mon
)
489 QEMUPutMouseEntry
*cursor
;
492 if (!qemu_put_mouse_event_head
) {
493 monitor_printf(mon
, "No mouse devices connected\n");
497 monitor_printf(mon
, "Mouse devices available:\n");
498 cursor
= qemu_put_mouse_event_head
;
499 while (cursor
!= NULL
) {
500 monitor_printf(mon
, "%c Mouse #%d: %s\n",
501 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
502 index
, cursor
->qemu_put_mouse_event_name
);
504 cursor
= cursor
->next
;
508 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
510 QEMUPutMouseEntry
*cursor
;
512 int index
= qdict_get_int(qdict
, "index");
514 if (!qemu_put_mouse_event_head
) {
515 monitor_printf(mon
, "No mouse devices connected\n");
519 cursor
= qemu_put_mouse_event_head
;
520 while (cursor
!= NULL
&& index
!= i
) {
522 cursor
= cursor
->next
;
526 qemu_put_mouse_event_current
= cursor
;
528 monitor_printf(mon
, "Mouse at given index not found\n");
531 /* compute with 96 bit intermediate result: (a*b)/c */
532 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
537 #ifdef HOST_WORDS_BIGENDIAN
547 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
548 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
551 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
555 /***********************************************************/
556 /* real time host monotonic timer */
558 static int64_t get_clock_realtime(void)
562 gettimeofday(&tv
, NULL
);
563 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
568 static int64_t clock_freq
;
570 static void init_get_clock(void)
574 ret
= QueryPerformanceFrequency(&freq
);
576 fprintf(stderr
, "Could not calibrate ticks\n");
579 clock_freq
= freq
.QuadPart
;
582 static int64_t get_clock(void)
585 QueryPerformanceCounter(&ti
);
586 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
591 static int use_rt_clock
;
593 static void init_get_clock(void)
596 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
597 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
600 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
607 static int64_t get_clock(void)
609 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
610 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
613 clock_gettime(CLOCK_MONOTONIC
, &ts
);
614 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
618 /* XXX: using gettimeofday leads to problems if the date
619 changes, so it should be avoided. */
620 return get_clock_realtime();
625 /* Return the virtual CPU time, based on the instruction counter. */
626 static int64_t cpu_get_icount(void)
629 CPUState
*env
= cpu_single_env
;;
630 icount
= qemu_icount
;
633 fprintf(stderr
, "Bad clock read\n");
634 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
636 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
639 /***********************************************************/
640 /* guest cycle counter */
642 typedef struct TimersState
{
643 int64_t cpu_ticks_prev
;
644 int64_t cpu_ticks_offset
;
645 int64_t cpu_clock_offset
;
646 int32_t cpu_ticks_enabled
;
650 TimersState timers_state
;
652 /* return the host CPU cycle counter and handle stop/restart */
653 int64_t cpu_get_ticks(void)
656 return cpu_get_icount();
658 if (!timers_state
.cpu_ticks_enabled
) {
659 return timers_state
.cpu_ticks_offset
;
662 ticks
= cpu_get_real_ticks();
663 if (timers_state
.cpu_ticks_prev
> ticks
) {
664 /* Note: non increasing ticks may happen if the host uses
666 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
668 timers_state
.cpu_ticks_prev
= ticks
;
669 return ticks
+ timers_state
.cpu_ticks_offset
;
673 /* return the host CPU monotonic timer and handle stop/restart */
674 static int64_t cpu_get_clock(void)
677 if (!timers_state
.cpu_ticks_enabled
) {
678 return timers_state
.cpu_clock_offset
;
681 return ti
+ timers_state
.cpu_clock_offset
;
685 /* enable cpu_get_ticks() */
686 void cpu_enable_ticks(void)
688 if (!timers_state
.cpu_ticks_enabled
) {
689 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
690 timers_state
.cpu_clock_offset
-= get_clock();
691 timers_state
.cpu_ticks_enabled
= 1;
695 /* disable cpu_get_ticks() : the clock is stopped. You must not call
696 cpu_get_ticks() after that. */
697 void cpu_disable_ticks(void)
699 if (timers_state
.cpu_ticks_enabled
) {
700 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
701 timers_state
.cpu_clock_offset
= cpu_get_clock();
702 timers_state
.cpu_ticks_enabled
= 0;
706 /***********************************************************/
709 #define QEMU_CLOCK_REALTIME 0
710 #define QEMU_CLOCK_VIRTUAL 1
711 #define QEMU_CLOCK_HOST 2
715 /* XXX: add frequency */
723 struct QEMUTimer
*next
;
726 struct qemu_alarm_timer
{
730 int (*start
)(struct qemu_alarm_timer
*t
);
731 void (*stop
)(struct qemu_alarm_timer
*t
);
732 void (*rearm
)(struct qemu_alarm_timer
*t
);
736 #define ALARM_FLAG_DYNTICKS 0x1
737 #define ALARM_FLAG_EXPIRED 0x2
739 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
741 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
744 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
746 if (!alarm_has_dynticks(t
))
752 /* TODO: MIN_TIMER_REARM_US should be optimized */
753 #define MIN_TIMER_REARM_US 250
755 static struct qemu_alarm_timer
*alarm_timer
;
759 struct qemu_alarm_win32
{
762 } alarm_win32_data
= {0, -1};
764 static int win32_start_timer(struct qemu_alarm_timer
*t
);
765 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
766 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
770 static int unix_start_timer(struct qemu_alarm_timer
*t
);
771 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
775 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
776 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
777 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
779 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
780 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
782 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
783 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
785 #endif /* __linux__ */
789 /* Correlation between real and virtual time is always going to be
790 fairly approximate, so ignore small variation.
791 When the guest is idle real and virtual time will be aligned in
793 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
795 static void icount_adjust(void)
800 static int64_t last_delta
;
801 /* If the VM is not running, then do nothing. */
805 cur_time
= cpu_get_clock();
806 cur_icount
= qemu_get_clock(vm_clock
);
807 delta
= cur_icount
- cur_time
;
808 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
810 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
811 && icount_time_shift
> 0) {
812 /* The guest is getting too far ahead. Slow time down. */
816 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
817 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
818 /* The guest is getting too far behind. Speed time up. */
822 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
825 static void icount_adjust_rt(void * opaque
)
827 qemu_mod_timer(icount_rt_timer
,
828 qemu_get_clock(rt_clock
) + 1000);
832 static void icount_adjust_vm(void * opaque
)
834 qemu_mod_timer(icount_vm_timer
,
835 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
839 static void init_icount_adjust(void)
841 /* Have both realtime and virtual time triggers for speed adjustment.
842 The realtime trigger catches emulated time passing too slowly,
843 the virtual time trigger catches emulated time passing too fast.
844 Realtime triggers occur even when idle, so use them less frequently
846 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
847 qemu_mod_timer(icount_rt_timer
,
848 qemu_get_clock(rt_clock
) + 1000);
849 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
850 qemu_mod_timer(icount_vm_timer
,
851 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
854 static struct qemu_alarm_timer alarm_timers
[] = {
857 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
858 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
859 /* HPET - if available - is preferred */
860 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
861 /* ...otherwise try RTC */
862 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
864 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
866 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
867 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
868 {"win32", 0, win32_start_timer
,
869 win32_stop_timer
, NULL
, &alarm_win32_data
},
874 static void show_available_alarms(void)
878 printf("Available alarm timers, in order of precedence:\n");
879 for (i
= 0; alarm_timers
[i
].name
; i
++)
880 printf("%s\n", alarm_timers
[i
].name
);
883 static void configure_alarms(char const *opt
)
887 int count
= ARRAY_SIZE(alarm_timers
) - 1;
890 struct qemu_alarm_timer tmp
;
892 if (!strcmp(opt
, "?")) {
893 show_available_alarms();
897 arg
= qemu_strdup(opt
);
899 /* Reorder the array */
900 name
= strtok(arg
, ",");
902 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
903 if (!strcmp(alarm_timers
[i
].name
, name
))
908 fprintf(stderr
, "Unknown clock %s\n", name
);
917 tmp
= alarm_timers
[i
];
918 alarm_timers
[i
] = alarm_timers
[cur
];
919 alarm_timers
[cur
] = tmp
;
923 name
= strtok(NULL
, ",");
929 /* Disable remaining timers */
930 for (i
= cur
; i
< count
; i
++)
931 alarm_timers
[i
].name
= NULL
;
933 show_available_alarms();
938 #define QEMU_NUM_CLOCKS 3
942 QEMUClock
*host_clock
;
944 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
946 static QEMUClock
*qemu_new_clock(int type
)
949 clock
= qemu_mallocz(sizeof(QEMUClock
));
954 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
958 ts
= qemu_mallocz(sizeof(QEMUTimer
));
965 void qemu_free_timer(QEMUTimer
*ts
)
970 /* stop a timer, but do not dealloc it */
971 void qemu_del_timer(QEMUTimer
*ts
)
975 /* NOTE: this code must be signal safe because
976 qemu_timer_expired() can be called from a signal. */
977 pt
= &active_timers
[ts
->clock
->type
];
990 /* modify the current timer so that it will be fired when current_time
991 >= expire_time. The corresponding callback will be called. */
992 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
998 /* add the timer in the sorted list */
999 /* NOTE: this code must be signal safe because
1000 qemu_timer_expired() can be called from a signal. */
1001 pt
= &active_timers
[ts
->clock
->type
];
1006 if (t
->expire_time
> expire_time
)
1010 ts
->expire_time
= expire_time
;
1014 /* Rearm if necessary */
1015 if (pt
== &active_timers
[ts
->clock
->type
]) {
1016 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1017 qemu_rearm_alarm_timer(alarm_timer
);
1019 /* Interrupt execution to force deadline recalculation. */
1021 qemu_notify_event();
1025 int qemu_timer_pending(QEMUTimer
*ts
)
1028 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1035 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1039 return (timer_head
->expire_time
<= current_time
);
1042 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1048 if (!ts
|| ts
->expire_time
> current_time
)
1050 /* remove timer from the list before calling the callback */
1051 *ptimer_head
= ts
->next
;
1054 /* run the callback (the timer list can be modified) */
1059 int64_t qemu_get_clock(QEMUClock
*clock
)
1061 switch(clock
->type
) {
1062 case QEMU_CLOCK_REALTIME
:
1063 return get_clock() / 1000000;
1065 case QEMU_CLOCK_VIRTUAL
:
1067 return cpu_get_icount();
1069 return cpu_get_clock();
1071 case QEMU_CLOCK_HOST
:
1072 return get_clock_realtime();
1076 static void init_clocks(void)
1079 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1080 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1081 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1083 rtc_clock
= host_clock
;
1087 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1089 uint64_t expire_time
;
1091 if (qemu_timer_pending(ts
)) {
1092 expire_time
= ts
->expire_time
;
1096 qemu_put_be64(f
, expire_time
);
1099 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1101 uint64_t expire_time
;
1103 expire_time
= qemu_get_be64(f
);
1104 if (expire_time
!= -1) {
1105 qemu_mod_timer(ts
, expire_time
);
1111 static const VMStateDescription vmstate_timers
= {
1114 .minimum_version_id
= 1,
1115 .minimum_version_id_old
= 1,
1116 .fields
= (VMStateField
[]) {
1117 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1118 VMSTATE_INT64(dummy
, TimersState
),
1119 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1120 VMSTATE_END_OF_LIST()
1124 static void qemu_event_increment(void);
1127 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1128 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1131 static void host_alarm_handler(int host_signum
)
1135 #define DISP_FREQ 1000
1137 static int64_t delta_min
= INT64_MAX
;
1138 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1140 ti
= qemu_get_clock(vm_clock
);
1141 if (last_clock
!= 0) {
1142 delta
= ti
- last_clock
;
1143 if (delta
< delta_min
)
1145 if (delta
> delta_max
)
1148 if (++count
== DISP_FREQ
) {
1149 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1150 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1151 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1152 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1153 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1155 delta_min
= INT64_MAX
;
1163 if (alarm_has_dynticks(alarm_timer
) ||
1165 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1166 qemu_get_clock(vm_clock
))) ||
1167 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1168 qemu_get_clock(rt_clock
)) ||
1169 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1170 qemu_get_clock(host_clock
))) {
1171 qemu_event_increment();
1172 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1174 #ifndef CONFIG_IOTHREAD
1176 /* stop the currently executing cpu because a timer occured */
1180 timer_alarm_pending
= 1;
1181 qemu_notify_event();
1185 static int64_t qemu_next_deadline(void)
1187 /* To avoid problems with overflow limit this to 2^32. */
1188 int64_t delta
= INT32_MAX
;
1190 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1191 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1192 qemu_get_clock(vm_clock
);
1194 if (active_timers
[QEMU_CLOCK_HOST
]) {
1195 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1196 qemu_get_clock(host_clock
);
1207 #if defined(__linux__)
1208 static uint64_t qemu_next_deadline_dyntick(void)
1216 delta
= (qemu_next_deadline() + 999) / 1000;
1218 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1219 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1220 qemu_get_clock(rt_clock
))*1000;
1221 if (rtdelta
< delta
)
1225 if (delta
< MIN_TIMER_REARM_US
)
1226 delta
= MIN_TIMER_REARM_US
;
1234 /* Sets a specific flag */
1235 static int fcntl_setfl(int fd
, int flag
)
1239 flags
= fcntl(fd
, F_GETFL
);
1243 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1249 #if defined(__linux__)
1251 #define RTC_FREQ 1024
1253 static void enable_sigio_timer(int fd
)
1255 struct sigaction act
;
1258 sigfillset(&act
.sa_mask
);
1260 act
.sa_handler
= host_alarm_handler
;
1262 sigaction(SIGIO
, &act
, NULL
);
1263 fcntl_setfl(fd
, O_ASYNC
);
1264 fcntl(fd
, F_SETOWN
, getpid());
1267 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1269 struct hpet_info info
;
1272 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1277 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1279 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1280 "error, but for better emulation accuracy type:\n"
1281 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1285 /* Check capabilities */
1286 r
= ioctl(fd
, HPET_INFO
, &info
);
1290 /* Enable periodic mode */
1291 r
= ioctl(fd
, HPET_EPI
, 0);
1292 if (info
.hi_flags
&& (r
< 0))
1295 /* Enable interrupt */
1296 r
= ioctl(fd
, HPET_IE_ON
, 0);
1300 enable_sigio_timer(fd
);
1301 t
->priv
= (void *)(long)fd
;
1309 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1311 int fd
= (long)t
->priv
;
1316 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1319 unsigned long current_rtc_freq
= 0;
1321 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1324 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1325 if (current_rtc_freq
!= RTC_FREQ
&&
1326 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1327 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1328 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1329 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1332 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1338 enable_sigio_timer(rtc_fd
);
1340 t
->priv
= (void *)(long)rtc_fd
;
1345 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1347 int rtc_fd
= (long)t
->priv
;
1352 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1356 struct sigaction act
;
1358 sigfillset(&act
.sa_mask
);
1360 act
.sa_handler
= host_alarm_handler
;
1362 sigaction(SIGALRM
, &act
, NULL
);
1365 * Initialize ev struct to 0 to avoid valgrind complaining
1366 * about uninitialized data in timer_create call
1368 memset(&ev
, 0, sizeof(ev
));
1369 ev
.sigev_value
.sival_int
= 0;
1370 ev
.sigev_notify
= SIGEV_SIGNAL
;
1371 ev
.sigev_signo
= SIGALRM
;
1373 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1374 perror("timer_create");
1376 /* disable dynticks */
1377 fprintf(stderr
, "Dynamic Ticks disabled\n");
1382 t
->priv
= (void *)(long)host_timer
;
1387 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1389 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1391 timer_delete(host_timer
);
1394 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1396 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1397 struct itimerspec timeout
;
1398 int64_t nearest_delta_us
= INT64_MAX
;
1401 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1402 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1403 !active_timers
[QEMU_CLOCK_HOST
])
1406 nearest_delta_us
= qemu_next_deadline_dyntick();
1408 /* check whether a timer is already running */
1409 if (timer_gettime(host_timer
, &timeout
)) {
1411 fprintf(stderr
, "Internal timer error: aborting\n");
1414 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1415 if (current_us
&& current_us
<= nearest_delta_us
)
1418 timeout
.it_interval
.tv_sec
= 0;
1419 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1420 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1421 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1422 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1424 fprintf(stderr
, "Internal timer error: aborting\n");
1429 #endif /* defined(__linux__) */
1431 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1433 struct sigaction act
;
1434 struct itimerval itv
;
1438 sigfillset(&act
.sa_mask
);
1440 act
.sa_handler
= host_alarm_handler
;
1442 sigaction(SIGALRM
, &act
, NULL
);
1444 itv
.it_interval
.tv_sec
= 0;
1445 /* for i386 kernel 2.6 to get 1 ms */
1446 itv
.it_interval
.tv_usec
= 999;
1447 itv
.it_value
.tv_sec
= 0;
1448 itv
.it_value
.tv_usec
= 10 * 1000;
1450 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1457 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1459 struct itimerval itv
;
1461 memset(&itv
, 0, sizeof(itv
));
1462 setitimer(ITIMER_REAL
, &itv
, NULL
);
1465 #endif /* !defined(_WIN32) */
1470 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1473 struct qemu_alarm_win32
*data
= t
->priv
;
1476 memset(&tc
, 0, sizeof(tc
));
1477 timeGetDevCaps(&tc
, sizeof(tc
));
1479 if (data
->period
< tc
.wPeriodMin
)
1480 data
->period
= tc
.wPeriodMin
;
1482 timeBeginPeriod(data
->period
);
1484 flags
= TIME_CALLBACK_FUNCTION
;
1485 if (alarm_has_dynticks(t
))
1486 flags
|= TIME_ONESHOT
;
1488 flags
|= TIME_PERIODIC
;
1490 data
->timerId
= timeSetEvent(1, // interval (ms)
1491 data
->period
, // resolution
1492 host_alarm_handler
, // function
1493 (DWORD
)t
, // parameter
1496 if (!data
->timerId
) {
1497 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1499 timeEndPeriod(data
->period
);
1506 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1508 struct qemu_alarm_win32
*data
= t
->priv
;
1510 timeKillEvent(data
->timerId
);
1511 timeEndPeriod(data
->period
);
1514 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1516 struct qemu_alarm_win32
*data
= t
->priv
;
1518 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1519 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1520 !active_timers
[QEMU_CLOCK_HOST
])
1523 timeKillEvent(data
->timerId
);
1525 data
->timerId
= timeSetEvent(1,
1529 TIME_ONESHOT
| TIME_PERIODIC
);
1531 if (!data
->timerId
) {
1532 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1535 timeEndPeriod(data
->period
);
1542 static int init_timer_alarm(void)
1544 struct qemu_alarm_timer
*t
= NULL
;
1547 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1548 t
= &alarm_timers
[i
];
1568 static void quit_timers(void)
1570 alarm_timer
->stop(alarm_timer
);
1574 /***********************************************************/
1575 /* host time/date access */
1576 void qemu_get_timedate(struct tm
*tm
, int offset
)
1583 if (rtc_date_offset
== -1) {
1587 ret
= localtime(&ti
);
1589 ti
-= rtc_date_offset
;
1593 memcpy(tm
, ret
, sizeof(struct tm
));
1596 int qemu_timedate_diff(struct tm
*tm
)
1600 if (rtc_date_offset
== -1)
1602 seconds
= mktimegm(tm
);
1604 seconds
= mktime(tm
);
1606 seconds
= mktimegm(tm
) + rtc_date_offset
;
1608 return seconds
- time(NULL
);
1611 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1613 time_t rtc_start_date
;
1616 if (!strcmp(startdate
, "now") && legacy
) {
1617 rtc_date_offset
= -1;
1619 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1627 } else if (sscanf(startdate
, "%d-%d-%d",
1630 &tm
.tm_mday
) == 3) {
1639 rtc_start_date
= mktimegm(&tm
);
1640 if (rtc_start_date
== -1) {
1642 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1643 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1646 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1650 static void configure_rtc(QemuOpts
*opts
)
1654 value
= qemu_opt_get(opts
, "base");
1656 if (!strcmp(value
, "utc")) {
1658 } else if (!strcmp(value
, "localtime")) {
1661 configure_rtc_date_offset(value
, 0);
1664 value
= qemu_opt_get(opts
, "clock");
1666 if (!strcmp(value
, "host")) {
1667 rtc_clock
= host_clock
;
1668 } else if (!strcmp(value
, "vm")) {
1669 rtc_clock
= vm_clock
;
1671 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1675 #ifdef CONFIG_TARGET_I386
1676 value
= qemu_opt_get(opts
, "driftfix");
1678 if (!strcmp(buf
, "slew")) {
1680 } else if (!strcmp(buf
, "none")) {
1683 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1691 static void socket_cleanup(void)
1696 static int socket_init(void)
1701 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1703 err
= WSAGetLastError();
1704 fprintf(stderr
, "WSAStartup: %d\n", err
);
1707 atexit(socket_cleanup
);
1712 /***********************************************************/
1713 /* Bluetooth support */
1716 static struct HCIInfo
*hci_table
[MAX_NICS
];
1718 static struct bt_vlan_s
{
1719 struct bt_scatternet_s net
;
1721 struct bt_vlan_s
*next
;
1724 /* find or alloc a new bluetooth "VLAN" */
1725 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1727 struct bt_vlan_s
**pvlan
, *vlan
;
1728 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1732 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1734 pvlan
= &first_bt_vlan
;
1735 while (*pvlan
!= NULL
)
1736 pvlan
= &(*pvlan
)->next
;
1741 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1745 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1750 static struct HCIInfo null_hci
= {
1751 .cmd_send
= null_hci_send
,
1752 .sco_send
= null_hci_send
,
1753 .acl_send
= null_hci_send
,
1754 .bdaddr_set
= null_hci_addr_set
,
1757 struct HCIInfo
*qemu_next_hci(void)
1759 if (cur_hci
== nb_hcis
)
1762 return hci_table
[cur_hci
++];
1765 static struct HCIInfo
*hci_init(const char *str
)
1768 struct bt_scatternet_s
*vlan
= 0;
1770 if (!strcmp(str
, "null"))
1773 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1775 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1776 else if (!strncmp(str
, "hci", 3)) {
1779 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1780 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1785 vlan
= qemu_find_bt_vlan(0);
1787 return bt_new_hci(vlan
);
1790 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1795 static int bt_hci_parse(const char *str
)
1797 struct HCIInfo
*hci
;
1800 if (nb_hcis
>= MAX_NICS
) {
1801 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1805 hci
= hci_init(str
);
1814 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1815 hci
->bdaddr_set(hci
, bdaddr
.b
);
1817 hci_table
[nb_hcis
++] = hci
;
1822 static void bt_vhci_add(int vlan_id
)
1824 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1827 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1828 "an empty scatternet %i\n", vlan_id
);
1830 bt_vhci_init(bt_new_hci(vlan
));
1833 static struct bt_device_s
*bt_device_add(const char *opt
)
1835 struct bt_scatternet_s
*vlan
;
1837 char *endp
= strstr(opt
, ",vlan=");
1838 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1841 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1844 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1846 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1851 vlan
= qemu_find_bt_vlan(vlan_id
);
1854 fprintf(stderr
, "qemu: warning: adding a slave device to "
1855 "an empty scatternet %i\n", vlan_id
);
1857 if (!strcmp(devname
, "keyboard"))
1858 return bt_keyboard_init(vlan
);
1860 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1864 static int bt_parse(const char *opt
)
1866 const char *endp
, *p
;
1869 if (strstart(opt
, "hci", &endp
)) {
1870 if (!*endp
|| *endp
== ',') {
1872 if (!strstart(endp
, ",vlan=", 0))
1875 return bt_hci_parse(opt
);
1877 } else if (strstart(opt
, "vhci", &endp
)) {
1878 if (!*endp
|| *endp
== ',') {
1880 if (strstart(endp
, ",vlan=", &p
)) {
1881 vlan
= strtol(p
, (char **) &endp
, 0);
1883 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1887 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1896 } else if (strstart(opt
, "device:", &endp
))
1897 return !bt_device_add(endp
);
1899 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1903 /***********************************************************/
1904 /* QEMU Block devices */
1906 #define HD_ALIAS "index=%d,media=disk"
1907 #define CDROM_ALIAS "index=2,media=cdrom"
1908 #define FD_ALIAS "index=%d,if=floppy"
1909 #define PFLASH_ALIAS "if=pflash"
1910 #define MTD_ALIAS "if=mtd"
1911 #define SD_ALIAS "index=0,if=sd"
1913 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1920 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1923 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1925 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1926 __FUNCTION__
, optstr
);
1930 qemu_opt_set(opts
, "file", file
);
1934 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1938 /* seek interface, bus and unit */
1940 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1941 if (dinfo
->type
== type
&&
1942 dinfo
->bus
== bus
&&
1943 dinfo
->unit
== unit
)
1950 DriveInfo
*drive_get_by_id(const char *id
)
1954 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1955 if (strcmp(id
, dinfo
->id
))
1962 int drive_get_max_bus(BlockInterfaceType type
)
1968 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1969 if(dinfo
->type
== type
&&
1970 dinfo
->bus
> max_bus
)
1971 max_bus
= dinfo
->bus
;
1976 const char *drive_get_serial(BlockDriverState
*bdrv
)
1980 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1981 if (dinfo
->bdrv
== bdrv
)
1982 return dinfo
->serial
;
1988 BlockInterfaceErrorAction
drive_get_on_error(
1989 BlockDriverState
*bdrv
, int is_read
)
1994 return BLOCK_ERR_REPORT
;
1997 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1998 if (dinfo
->bdrv
== bdrv
)
1999 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
2002 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
2005 static void bdrv_format_print(void *opaque
, const char *name
)
2007 fprintf(stderr
, " %s", name
);
2010 void drive_uninit(DriveInfo
*dinfo
)
2012 qemu_opts_del(dinfo
->opts
);
2013 bdrv_delete(dinfo
->bdrv
);
2014 QTAILQ_REMOVE(&drives
, dinfo
, next
);
2018 static int parse_block_error_action(const char *buf
, int is_read
)
2020 if (!strcmp(buf
, "ignore")) {
2021 return BLOCK_ERR_IGNORE
;
2022 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
2023 return BLOCK_ERR_STOP_ENOSPC
;
2024 } else if (!strcmp(buf
, "stop")) {
2025 return BLOCK_ERR_STOP_ANY
;
2026 } else if (!strcmp(buf
, "report")) {
2027 return BLOCK_ERR_REPORT
;
2029 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
2030 buf
, is_read
? "read" : "write");
2035 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
2039 const char *file
= NULL
;
2042 const char *mediastr
= "";
2043 BlockInterfaceType type
;
2044 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2045 int bus_id
, unit_id
;
2046 int cyls
, heads
, secs
, translation
;
2047 BlockDriver
*drv
= NULL
;
2048 QEMUMachine
*machine
= opaque
;
2055 int on_read_error
, on_write_error
;
2056 const char *devaddr
;
2062 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2065 if (machine
&& machine
->use_scsi
) {
2067 max_devs
= MAX_SCSI_DEVS
;
2068 pstrcpy(devname
, sizeof(devname
), "scsi");
2071 max_devs
= MAX_IDE_DEVS
;
2072 pstrcpy(devname
, sizeof(devname
), "ide");
2076 /* extract parameters */
2077 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2078 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2079 index
= qemu_opt_get_number(opts
, "index", -1);
2081 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2082 heads
= qemu_opt_get_number(opts
, "heads", 0);
2083 secs
= qemu_opt_get_number(opts
, "secs", 0);
2085 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2086 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2088 file
= qemu_opt_get(opts
, "file");
2089 serial
= qemu_opt_get(opts
, "serial");
2091 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2092 pstrcpy(devname
, sizeof(devname
), buf
);
2093 if (!strcmp(buf
, "ide")) {
2095 max_devs
= MAX_IDE_DEVS
;
2096 } else if (!strcmp(buf
, "scsi")) {
2098 max_devs
= MAX_SCSI_DEVS
;
2099 } else if (!strcmp(buf
, "floppy")) {
2102 } else if (!strcmp(buf
, "pflash")) {
2105 } else if (!strcmp(buf
, "mtd")) {
2108 } else if (!strcmp(buf
, "sd")) {
2111 } else if (!strcmp(buf
, "virtio")) {
2114 } else if (!strcmp(buf
, "xen")) {
2117 } else if (!strcmp(buf
, "none")) {
2121 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2126 if (cyls
|| heads
|| secs
) {
2127 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2128 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2131 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2132 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2135 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2136 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2141 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2144 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2148 if (!strcmp(buf
, "none"))
2149 translation
= BIOS_ATA_TRANSLATION_NONE
;
2150 else if (!strcmp(buf
, "lba"))
2151 translation
= BIOS_ATA_TRANSLATION_LBA
;
2152 else if (!strcmp(buf
, "auto"))
2153 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2155 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2160 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2161 if (!strcmp(buf
, "disk")) {
2163 } else if (!strcmp(buf
, "cdrom")) {
2164 if (cyls
|| secs
|| heads
) {
2166 "qemu: '%s' invalid physical CHS format\n", buf
);
2169 media
= MEDIA_CDROM
;
2171 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2176 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2177 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2179 else if (!strcmp(buf
, "writethrough"))
2181 else if (!strcmp(buf
, "writeback"))
2184 fprintf(stderr
, "qemu: invalid cache option\n");
2189 #ifdef CONFIG_LINUX_AIO
2190 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2191 if (!strcmp(buf
, "threads"))
2193 else if (!strcmp(buf
, "native"))
2196 fprintf(stderr
, "qemu: invalid aio option\n");
2202 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2203 if (strcmp(buf
, "?") == 0) {
2204 fprintf(stderr
, "qemu: Supported formats:");
2205 bdrv_iterate_format(bdrv_format_print
, NULL
);
2206 fprintf(stderr
, "\n");
2209 drv
= bdrv_find_whitelisted_format(buf
);
2211 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2216 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2217 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2218 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2219 fprintf(stderr
, "werror is no supported by this format\n");
2223 on_write_error
= parse_block_error_action(buf
, 0);
2224 if (on_write_error
< 0) {
2229 on_read_error
= BLOCK_ERR_REPORT
;
2230 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2231 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2232 fprintf(stderr
, "rerror is no supported by this format\n");
2236 on_read_error
= parse_block_error_action(buf
, 1);
2237 if (on_read_error
< 0) {
2242 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2243 if (type
!= IF_VIRTIO
) {
2244 fprintf(stderr
, "addr is not supported\n");
2249 /* compute bus and unit according index */
2252 if (bus_id
!= 0 || unit_id
!= -1) {
2254 "qemu: index cannot be used with bus and unit\n");
2262 unit_id
= index
% max_devs
;
2263 bus_id
= index
/ max_devs
;
2267 /* if user doesn't specify a unit_id,
2268 * try to find the first free
2271 if (unit_id
== -1) {
2273 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2275 if (max_devs
&& unit_id
>= max_devs
) {
2276 unit_id
-= max_devs
;
2284 if (max_devs
&& unit_id
>= max_devs
) {
2285 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2286 unit_id
, max_devs
- 1);
2291 * ignore multiple definitions
2294 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2301 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2302 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2303 dinfo
->id
= qemu_strdup(buf
);
2305 /* no id supplied -> create one */
2306 dinfo
->id
= qemu_mallocz(32);
2307 if (type
== IF_IDE
|| type
== IF_SCSI
)
2308 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2310 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2311 devname
, bus_id
, mediastr
, unit_id
);
2313 snprintf(dinfo
->id
, 32, "%s%s%i",
2314 devname
, mediastr
, unit_id
);
2316 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2317 dinfo
->devaddr
= devaddr
;
2319 dinfo
->bus
= bus_id
;
2320 dinfo
->unit
= unit_id
;
2321 dinfo
->on_read_error
= on_read_error
;
2322 dinfo
->on_write_error
= on_write_error
;
2325 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2326 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2336 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2337 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2341 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2346 /* FIXME: This isn't really a floppy, but it's a reasonable
2349 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2355 /* add virtio block device */
2356 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2357 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2358 qemu_opt_set(opts
, "drive", dinfo
->id
);
2360 qemu_opt_set(opts
, "addr", devaddr
);
2371 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2372 cache
= 2; /* always use write-back with snapshot */
2374 if (cache
== 0) /* no caching */
2375 bdrv_flags
|= BDRV_O_NOCACHE
;
2376 else if (cache
== 2) /* write-back */
2377 bdrv_flags
|= BDRV_O_CACHE_WB
;
2380 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2382 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2386 if (type
== IF_IDE
) {
2387 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2390 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2393 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2394 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2395 file
, strerror(errno
));
2399 if (bdrv_key_required(dinfo
->bdrv
))
2405 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2407 QEMUMachine
*machine
= opaque
;
2408 int fatal_error
= 0;
2410 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2417 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2419 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2420 qemu_opt_set(opts
, "snapshot", "on");
2425 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2427 boot_set_handler
= func
;
2428 boot_set_opaque
= opaque
;
2431 int qemu_boot_set(const char *boot_devices
)
2433 if (!boot_set_handler
) {
2436 return boot_set_handler(boot_set_opaque
, boot_devices
);
2439 static int parse_bootdevices(char *devices
)
2441 /* We just do some generic consistency checks */
2445 for (p
= devices
; *p
!= '\0'; p
++) {
2446 /* Allowed boot devices are:
2447 * a-b: floppy disk drives
2448 * c-f: IDE disk drives
2449 * g-m: machine implementation dependant drives
2450 * n-p: network devices
2451 * It's up to each machine implementation to check if the given boot
2452 * devices match the actual hardware implementation and firmware
2455 if (*p
< 'a' || *p
> 'p') {
2456 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2459 if (bitmap
& (1 << (*p
- 'a'))) {
2460 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2463 bitmap
|= 1 << (*p
- 'a');
2468 static void restore_boot_devices(void *opaque
)
2470 char *standard_boot_devices
= opaque
;
2472 qemu_boot_set(standard_boot_devices
);
2474 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2475 qemu_free(standard_boot_devices
);
2478 static void numa_add(const char *optarg
)
2482 unsigned long long value
, endvalue
;
2485 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2486 if (!strcmp(option
, "node")) {
2487 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2488 nodenr
= nb_numa_nodes
;
2490 nodenr
= strtoull(option
, NULL
, 10);
2493 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2494 node_mem
[nodenr
] = 0;
2496 value
= strtoull(option
, &endptr
, 0);
2498 case 0: case 'M': case 'm':
2505 node_mem
[nodenr
] = value
;
2507 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2508 node_cpumask
[nodenr
] = 0;
2510 value
= strtoull(option
, &endptr
, 10);
2513 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2515 if (*endptr
== '-') {
2516 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2517 if (endvalue
>= 63) {
2520 "only 63 CPUs in NUMA mode supported.\n");
2522 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2527 node_cpumask
[nodenr
] = value
;
2534 static void smp_parse(const char *optarg
)
2536 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2540 smp
= strtoul(optarg
, &endptr
, 10);
2541 if (endptr
!= optarg
) {
2542 if (*endptr
== ',') {
2546 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2547 sockets
= strtoull(option
, NULL
, 10);
2548 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2549 cores
= strtoull(option
, NULL
, 10);
2550 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2551 threads
= strtoull(option
, NULL
, 10);
2552 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2553 max_cpus
= strtoull(option
, NULL
, 10);
2555 /* compute missing values, prefer sockets over cores over threads */
2556 if (smp
== 0 || sockets
== 0) {
2557 sockets
= sockets
> 0 ? sockets
: 1;
2558 cores
= cores
> 0 ? cores
: 1;
2559 threads
= threads
> 0 ? threads
: 1;
2561 smp
= cores
* threads
* sockets
;
2563 sockets
= smp
/ (cores
* threads
);
2567 threads
= threads
> 0 ? threads
: 1;
2568 cores
= smp
/ (sockets
* threads
);
2571 sockets
= smp
/ (cores
* threads
);
2573 threads
= smp
/ (cores
* sockets
);
2578 smp_cores
= cores
> 0 ? cores
: 1;
2579 smp_threads
= threads
> 0 ? threads
: 1;
2581 max_cpus
= smp_cpus
;
2584 /***********************************************************/
2587 static int usb_device_add(const char *devname
, int is_hotplug
)
2590 USBDevice
*dev
= NULL
;
2595 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2596 dev
= usbdevice_create(devname
);
2600 /* the other ones */
2601 if (strstart(devname
, "host:", &p
)) {
2602 dev
= usb_host_device_open(p
);
2603 } else if (strstart(devname
, "net:", &p
)) {
2607 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2612 qemu_opt_set(opts
, "type", "nic");
2613 qemu_opt_set(opts
, "model", "usb");
2615 idx
= net_client_init(NULL
, opts
, 0);
2620 dev
= usb_net_init(&nd_table
[idx
]);
2621 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2622 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2623 bt_new_hci(qemu_find_bt_vlan(0)));
2634 static int usb_device_del(const char *devname
)
2639 if (strstart(devname
, "host:", &p
))
2640 return usb_host_device_close(p
);
2645 p
= strchr(devname
, '.');
2648 bus_num
= strtoul(devname
, NULL
, 0);
2649 addr
= strtoul(p
+ 1, NULL
, 0);
2651 return usb_device_delete_addr(bus_num
, addr
);
2654 static int usb_parse(const char *cmdline
)
2656 return usb_device_add(cmdline
, 0);
2659 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2661 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2664 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2666 usb_device_del(qdict_get_str(qdict
, "devname"));
2669 /***********************************************************/
2670 /* PCMCIA/Cardbus */
2672 static struct pcmcia_socket_entry_s
{
2673 PCMCIASocket
*socket
;
2674 struct pcmcia_socket_entry_s
*next
;
2675 } *pcmcia_sockets
= 0;
2677 void pcmcia_socket_register(PCMCIASocket
*socket
)
2679 struct pcmcia_socket_entry_s
*entry
;
2681 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2682 entry
->socket
= socket
;
2683 entry
->next
= pcmcia_sockets
;
2684 pcmcia_sockets
= entry
;
2687 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2689 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2691 ptr
= &pcmcia_sockets
;
2692 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2693 if (entry
->socket
== socket
) {
2699 void pcmcia_info(Monitor
*mon
)
2701 struct pcmcia_socket_entry_s
*iter
;
2703 if (!pcmcia_sockets
)
2704 monitor_printf(mon
, "No PCMCIA sockets\n");
2706 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2707 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2708 iter
->socket
->attached
? iter
->socket
->card_string
:
2712 /***********************************************************/
2713 /* register display */
2715 struct DisplayAllocator default_allocator
= {
2716 defaultallocator_create_displaysurface
,
2717 defaultallocator_resize_displaysurface
,
2718 defaultallocator_free_displaysurface
2721 void register_displaystate(DisplayState
*ds
)
2731 DisplayState
*get_displaystate(void)
2733 return display_state
;
2736 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2738 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2739 return ds
->allocator
;
2744 static void dumb_display_init(void)
2746 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2747 ds
->allocator
= &default_allocator
;
2748 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2749 register_displaystate(ds
);
2752 /***********************************************************/
2755 typedef struct IOHandlerRecord
{
2757 IOCanRWHandler
*fd_read_poll
;
2759 IOHandler
*fd_write
;
2762 /* temporary data */
2764 struct IOHandlerRecord
*next
;
2767 static IOHandlerRecord
*first_io_handler
;
2769 /* XXX: fd_read_poll should be suppressed, but an API change is
2770 necessary in the character devices to suppress fd_can_read(). */
2771 int qemu_set_fd_handler2(int fd
,
2772 IOCanRWHandler
*fd_read_poll
,
2774 IOHandler
*fd_write
,
2777 IOHandlerRecord
**pioh
, *ioh
;
2779 if (!fd_read
&& !fd_write
) {
2780 pioh
= &first_io_handler
;
2785 if (ioh
->fd
== fd
) {
2792 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2796 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2797 ioh
->next
= first_io_handler
;
2798 first_io_handler
= ioh
;
2801 ioh
->fd_read_poll
= fd_read_poll
;
2802 ioh
->fd_read
= fd_read
;
2803 ioh
->fd_write
= fd_write
;
2804 ioh
->opaque
= opaque
;
2810 int qemu_set_fd_handler(int fd
,
2812 IOHandler
*fd_write
,
2815 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2819 /***********************************************************/
2820 /* Polling handling */
2822 typedef struct PollingEntry
{
2825 struct PollingEntry
*next
;
2828 static PollingEntry
*first_polling_entry
;
2830 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2832 PollingEntry
**ppe
, *pe
;
2833 pe
= qemu_mallocz(sizeof(PollingEntry
));
2835 pe
->opaque
= opaque
;
2836 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2841 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2843 PollingEntry
**ppe
, *pe
;
2844 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2846 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2854 /***********************************************************/
2855 /* Wait objects support */
2856 typedef struct WaitObjects
{
2858 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2859 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2860 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2863 static WaitObjects wait_objects
= {0};
2865 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2867 WaitObjects
*w
= &wait_objects
;
2869 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2871 w
->events
[w
->num
] = handle
;
2872 w
->func
[w
->num
] = func
;
2873 w
->opaque
[w
->num
] = opaque
;
2878 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2881 WaitObjects
*w
= &wait_objects
;
2884 for (i
= 0; i
< w
->num
; i
++) {
2885 if (w
->events
[i
] == handle
)
2888 w
->events
[i
] = w
->events
[i
+ 1];
2889 w
->func
[i
] = w
->func
[i
+ 1];
2890 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2898 /***********************************************************/
2899 /* ram save/restore */
2901 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2902 #define RAM_SAVE_FLAG_COMPRESS 0x02
2903 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2904 #define RAM_SAVE_FLAG_PAGE 0x08
2905 #define RAM_SAVE_FLAG_EOS 0x10
2907 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2909 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2910 uint32_t *array
= (uint32_t *)page
;
2913 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2914 if (array
[i
] != val
)
2921 static int ram_save_block(QEMUFile
*f
)
2923 static ram_addr_t current_addr
= 0;
2924 ram_addr_t saved_addr
= current_addr
;
2925 ram_addr_t addr
= 0;
2928 while (addr
< last_ram_offset
) {
2929 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2932 cpu_physical_memory_reset_dirty(current_addr
,
2933 current_addr
+ TARGET_PAGE_SIZE
,
2934 MIGRATION_DIRTY_FLAG
);
2936 p
= qemu_get_ram_ptr(current_addr
);
2938 if (is_dup_page(p
, *p
)) {
2939 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2940 qemu_put_byte(f
, *p
);
2942 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2943 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2949 addr
+= TARGET_PAGE_SIZE
;
2950 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2956 static uint64_t bytes_transferred
;
2958 static ram_addr_t
ram_save_remaining(void)
2961 ram_addr_t count
= 0;
2963 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2964 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2971 uint64_t ram_bytes_remaining(void)
2973 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2976 uint64_t ram_bytes_transferred(void)
2978 return bytes_transferred
;
2981 uint64_t ram_bytes_total(void)
2983 return last_ram_offset
;
2986 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2989 uint64_t bytes_transferred_last
;
2991 uint64_t expected_time
= 0;
2994 cpu_physical_memory_set_dirty_tracking(0);
2998 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2999 qemu_file_set_error(f
);
3004 bytes_transferred
= 0;
3006 /* Make sure all dirty bits are set */
3007 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3008 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3009 cpu_physical_memory_set_dirty(addr
);
3012 /* Enable dirty memory tracking */
3013 cpu_physical_memory_set_dirty_tracking(1);
3015 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3018 bytes_transferred_last
= bytes_transferred
;
3019 bwidth
= get_clock();
3021 while (!qemu_file_rate_limit(f
)) {
3024 ret
= ram_save_block(f
);
3025 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3026 if (ret
== 0) /* no more blocks */
3030 bwidth
= get_clock() - bwidth
;
3031 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3033 /* if we haven't transferred anything this round, force expected_time to a
3034 * a very high value, but without crashing */
3038 /* try transferring iterative blocks of memory */
3040 /* flush all remaining blocks regardless of rate limiting */
3041 while (ram_save_block(f
) != 0) {
3042 bytes_transferred
+= TARGET_PAGE_SIZE
;
3044 cpu_physical_memory_set_dirty_tracking(0);
3047 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3049 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3051 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3054 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3059 if (version_id
!= 3)
3063 addr
= qemu_get_be64(f
);
3065 flags
= addr
& ~TARGET_PAGE_MASK
;
3066 addr
&= TARGET_PAGE_MASK
;
3068 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3069 if (addr
!= last_ram_offset
)
3073 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3074 uint8_t ch
= qemu_get_byte(f
);
3075 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3078 (!kvm_enabled() || kvm_has_sync_mmu())) {
3079 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3082 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
3083 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3085 if (qemu_file_has_error(f
)) {
3088 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3093 void qemu_service_io(void)
3095 qemu_notify_event();
3098 /***********************************************************/
3099 /* machine registration */
3101 static QEMUMachine
*first_machine
= NULL
;
3102 QEMUMachine
*current_machine
= NULL
;
3104 int qemu_register_machine(QEMUMachine
*m
)
3107 pm
= &first_machine
;
3115 static QEMUMachine
*find_machine(const char *name
)
3119 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3120 if (!strcmp(m
->name
, name
))
3122 if (m
->alias
&& !strcmp(m
->alias
, name
))
3128 static QEMUMachine
*find_default_machine(void)
3132 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3133 if (m
->is_default
) {
3140 /***********************************************************/
3141 /* main execution loop */
3143 static void gui_update(void *opaque
)
3145 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3146 DisplayState
*ds
= opaque
;
3147 DisplayChangeListener
*dcl
= ds
->listeners
;
3151 while (dcl
!= NULL
) {
3152 if (dcl
->gui_timer_interval
&&
3153 dcl
->gui_timer_interval
< interval
)
3154 interval
= dcl
->gui_timer_interval
;
3157 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3160 static void nographic_update(void *opaque
)
3162 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3164 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3167 struct vm_change_state_entry
{
3168 VMChangeStateHandler
*cb
;
3170 QLIST_ENTRY (vm_change_state_entry
) entries
;
3173 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3175 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3178 VMChangeStateEntry
*e
;
3180 e
= qemu_mallocz(sizeof (*e
));
3184 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3188 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3190 QLIST_REMOVE (e
, entries
);
3194 static void vm_state_notify(int running
, int reason
)
3196 VMChangeStateEntry
*e
;
3198 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3199 e
->cb(e
->opaque
, running
, reason
);
3203 static void resume_all_vcpus(void);
3204 static void pause_all_vcpus(void);
3211 vm_state_notify(1, 0);
3212 qemu_rearm_alarm_timer(alarm_timer
);
3217 /* reset/shutdown handler */
3219 typedef struct QEMUResetEntry
{
3220 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3221 QEMUResetHandler
*func
;
3225 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3226 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3227 static int reset_requested
;
3228 static int shutdown_requested
;
3229 static int powerdown_requested
;
3230 static int debug_requested
;
3231 static int vmstop_requested
;
3233 int qemu_shutdown_requested(void)
3235 int r
= shutdown_requested
;
3236 shutdown_requested
= 0;
3240 int qemu_reset_requested(void)
3242 int r
= reset_requested
;
3243 reset_requested
= 0;
3247 int qemu_powerdown_requested(void)
3249 int r
= powerdown_requested
;
3250 powerdown_requested
= 0;
3254 static int qemu_debug_requested(void)
3256 int r
= debug_requested
;
3257 debug_requested
= 0;
3261 static int qemu_vmstop_requested(void)
3263 int r
= vmstop_requested
;
3264 vmstop_requested
= 0;
3268 static void do_vm_stop(int reason
)
3271 cpu_disable_ticks();
3274 vm_state_notify(0, reason
);
3278 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3280 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3283 re
->opaque
= opaque
;
3284 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3287 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3291 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3292 if (re
->func
== func
&& re
->opaque
== opaque
) {
3293 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3300 void qemu_system_reset(void)
3302 QEMUResetEntry
*re
, *nre
;
3304 /* reset all devices */
3305 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3306 re
->func(re
->opaque
);
3310 void qemu_system_reset_request(void)
3313 shutdown_requested
= 1;
3315 reset_requested
= 1;
3317 qemu_notify_event();
3320 void qemu_system_shutdown_request(void)
3322 shutdown_requested
= 1;
3323 qemu_notify_event();
3326 void qemu_system_powerdown_request(void)
3328 powerdown_requested
= 1;
3329 qemu_notify_event();
3332 #ifdef CONFIG_IOTHREAD
3333 static void qemu_system_vmstop_request(int reason
)
3335 vmstop_requested
= reason
;
3336 qemu_notify_event();
3341 static int io_thread_fd
= -1;
3343 static void qemu_event_increment(void)
3345 static const char byte
= 0;
3347 if (io_thread_fd
== -1)
3350 write(io_thread_fd
, &byte
, sizeof(byte
));
3353 static void qemu_event_read(void *opaque
)
3355 int fd
= (unsigned long)opaque
;
3358 /* Drain the notify pipe */
3361 len
= read(fd
, buffer
, sizeof(buffer
));
3362 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3365 static int qemu_event_init(void)
3370 err
= qemu_pipe(fds
);
3374 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3378 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3382 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3383 (void *)(unsigned long)fds
[0]);
3385 io_thread_fd
= fds
[1];
3394 HANDLE qemu_event_handle
;
3396 static void dummy_event_handler(void *opaque
)
3400 static int qemu_event_init(void)
3402 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3403 if (!qemu_event_handle
) {
3404 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3407 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3411 static void qemu_event_increment(void)
3413 if (!SetEvent(qemu_event_handle
)) {
3414 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3421 static int cpu_can_run(CPUState
*env
)
3430 #ifndef CONFIG_IOTHREAD
3431 static int qemu_init_main_loop(void)
3433 return qemu_event_init();
3436 void qemu_init_vcpu(void *_env
)
3438 CPUState
*env
= _env
;
3442 env
->nr_cores
= smp_cores
;
3443 env
->nr_threads
= smp_threads
;
3447 int qemu_cpu_self(void *env
)
3452 static void resume_all_vcpus(void)
3456 static void pause_all_vcpus(void)
3460 void qemu_cpu_kick(void *env
)
3465 void qemu_notify_event(void)
3467 CPUState
*env
= cpu_single_env
;
3474 void qemu_mutex_lock_iothread(void) {}
3475 void qemu_mutex_unlock_iothread(void) {}
3477 void vm_stop(int reason
)
3482 #else /* CONFIG_IOTHREAD */
3484 #include "qemu-thread.h"
3486 QemuMutex qemu_global_mutex
;
3487 static QemuMutex qemu_fair_mutex
;
3489 static QemuThread io_thread
;
3491 static QemuThread
*tcg_cpu_thread
;
3492 static QemuCond
*tcg_halt_cond
;
3494 static int qemu_system_ready
;
3496 static QemuCond qemu_cpu_cond
;
3498 static QemuCond qemu_system_cond
;
3499 static QemuCond qemu_pause_cond
;
3501 static void block_io_signals(void);
3502 static void unblock_io_signals(void);
3503 static int tcg_has_work(void);
3505 static int qemu_init_main_loop(void)
3509 ret
= qemu_event_init();
3513 qemu_cond_init(&qemu_pause_cond
);
3514 qemu_mutex_init(&qemu_fair_mutex
);
3515 qemu_mutex_init(&qemu_global_mutex
);
3516 qemu_mutex_lock(&qemu_global_mutex
);
3518 unblock_io_signals();
3519 qemu_thread_self(&io_thread
);
3524 static void qemu_wait_io_event(CPUState
*env
)
3526 while (!tcg_has_work())
3527 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3529 qemu_mutex_unlock(&qemu_global_mutex
);
3532 * Users of qemu_global_mutex can be starved, having no chance
3533 * to acquire it since this path will get to it first.
3534 * So use another lock to provide fairness.
3536 qemu_mutex_lock(&qemu_fair_mutex
);
3537 qemu_mutex_unlock(&qemu_fair_mutex
);
3539 qemu_mutex_lock(&qemu_global_mutex
);
3543 qemu_cond_signal(&qemu_pause_cond
);
3547 static int qemu_cpu_exec(CPUState
*env
);
3549 static void *kvm_cpu_thread_fn(void *arg
)
3551 CPUState
*env
= arg
;
3554 qemu_thread_self(env
->thread
);
3558 /* signal CPU creation */
3559 qemu_mutex_lock(&qemu_global_mutex
);
3561 qemu_cond_signal(&qemu_cpu_cond
);
3563 /* and wait for machine initialization */
3564 while (!qemu_system_ready
)
3565 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3568 if (cpu_can_run(env
))
3570 qemu_wait_io_event(env
);
3576 static void tcg_cpu_exec(void);
3578 static void *tcg_cpu_thread_fn(void *arg
)
3580 CPUState
*env
= arg
;
3583 qemu_thread_self(env
->thread
);
3585 /* signal CPU creation */
3586 qemu_mutex_lock(&qemu_global_mutex
);
3587 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3589 qemu_cond_signal(&qemu_cpu_cond
);
3591 /* and wait for machine initialization */
3592 while (!qemu_system_ready
)
3593 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3597 qemu_wait_io_event(cur_cpu
);
3603 void qemu_cpu_kick(void *_env
)
3605 CPUState
*env
= _env
;
3606 qemu_cond_broadcast(env
->halt_cond
);
3608 qemu_thread_signal(env
->thread
, SIGUSR1
);
3611 int qemu_cpu_self(void *_env
)
3613 CPUState
*env
= _env
;
3616 qemu_thread_self(&this);
3618 return qemu_thread_equal(&this, env
->thread
);
3621 static void cpu_signal(int sig
)
3624 cpu_exit(cpu_single_env
);
3627 static void block_io_signals(void)
3630 struct sigaction sigact
;
3633 sigaddset(&set
, SIGUSR2
);
3634 sigaddset(&set
, SIGIO
);
3635 sigaddset(&set
, SIGALRM
);
3636 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3639 sigaddset(&set
, SIGUSR1
);
3640 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3642 memset(&sigact
, 0, sizeof(sigact
));
3643 sigact
.sa_handler
= cpu_signal
;
3644 sigaction(SIGUSR1
, &sigact
, NULL
);
3647 static void unblock_io_signals(void)
3652 sigaddset(&set
, SIGUSR2
);
3653 sigaddset(&set
, SIGIO
);
3654 sigaddset(&set
, SIGALRM
);
3655 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3658 sigaddset(&set
, SIGUSR1
);
3659 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3662 static void qemu_signal_lock(unsigned int msecs
)
3664 qemu_mutex_lock(&qemu_fair_mutex
);
3666 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3667 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3668 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3671 qemu_mutex_unlock(&qemu_fair_mutex
);
3674 void qemu_mutex_lock_iothread(void)
3676 if (kvm_enabled()) {
3677 qemu_mutex_lock(&qemu_fair_mutex
);
3678 qemu_mutex_lock(&qemu_global_mutex
);
3679 qemu_mutex_unlock(&qemu_fair_mutex
);
3681 qemu_signal_lock(100);
3684 void qemu_mutex_unlock_iothread(void)
3686 qemu_mutex_unlock(&qemu_global_mutex
);
3689 static int all_vcpus_paused(void)
3691 CPUState
*penv
= first_cpu
;
3696 penv
= (CPUState
*)penv
->next_cpu
;
3702 static void pause_all_vcpus(void)
3704 CPUState
*penv
= first_cpu
;
3708 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3709 qemu_cpu_kick(penv
);
3710 penv
= (CPUState
*)penv
->next_cpu
;
3713 while (!all_vcpus_paused()) {
3714 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3717 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3718 penv
= (CPUState
*)penv
->next_cpu
;
3723 static void resume_all_vcpus(void)
3725 CPUState
*penv
= first_cpu
;
3730 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3731 qemu_cpu_kick(penv
);
3732 penv
= (CPUState
*)penv
->next_cpu
;
3736 static void tcg_init_vcpu(void *_env
)
3738 CPUState
*env
= _env
;
3739 /* share a single thread for all cpus with TCG */
3740 if (!tcg_cpu_thread
) {
3741 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3742 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3743 qemu_cond_init(env
->halt_cond
);
3744 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3745 while (env
->created
== 0)
3746 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3747 tcg_cpu_thread
= env
->thread
;
3748 tcg_halt_cond
= env
->halt_cond
;
3750 env
->thread
= tcg_cpu_thread
;
3751 env
->halt_cond
= tcg_halt_cond
;
3755 static void kvm_start_vcpu(CPUState
*env
)
3757 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3758 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3759 qemu_cond_init(env
->halt_cond
);
3760 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3761 while (env
->created
== 0)
3762 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3765 void qemu_init_vcpu(void *_env
)
3767 CPUState
*env
= _env
;
3770 kvm_start_vcpu(env
);
3773 env
->nr_cores
= smp_cores
;
3774 env
->nr_threads
= smp_threads
;
3777 void qemu_notify_event(void)
3779 qemu_event_increment();
3782 void vm_stop(int reason
)
3785 qemu_thread_self(&me
);
3787 if (!qemu_thread_equal(&me
, &io_thread
)) {
3788 qemu_system_vmstop_request(reason
);
3790 * FIXME: should not return to device code in case
3791 * vm_stop() has been requested.
3793 if (cpu_single_env
) {
3794 cpu_exit(cpu_single_env
);
3795 cpu_single_env
->stop
= 1;
3806 static void host_main_loop_wait(int *timeout
)
3812 /* XXX: need to suppress polling by better using win32 events */
3814 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3815 ret
|= pe
->func(pe
->opaque
);
3819 WaitObjects
*w
= &wait_objects
;
3821 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3822 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3823 if (w
->func
[ret
- WAIT_OBJECT_0
])
3824 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3826 /* Check for additional signaled events */
3827 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3829 /* Check if event is signaled */
3830 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3831 if(ret2
== WAIT_OBJECT_0
) {
3833 w
->func
[i
](w
->opaque
[i
]);
3834 } else if (ret2
== WAIT_TIMEOUT
) {
3836 err
= GetLastError();
3837 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3840 } else if (ret
== WAIT_TIMEOUT
) {
3842 err
= GetLastError();
3843 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3850 static void host_main_loop_wait(int *timeout
)
3855 void main_loop_wait(int timeout
)
3857 IOHandlerRecord
*ioh
;
3858 fd_set rfds
, wfds
, xfds
;
3862 qemu_bh_update_timeout(&timeout
);
3864 host_main_loop_wait(&timeout
);
3866 /* poll any events */
3867 /* XXX: separate device handlers from system ones */
3872 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3876 (!ioh
->fd_read_poll
||
3877 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3878 FD_SET(ioh
->fd
, &rfds
);
3882 if (ioh
->fd_write
) {
3883 FD_SET(ioh
->fd
, &wfds
);
3889 tv
.tv_sec
= timeout
/ 1000;
3890 tv
.tv_usec
= (timeout
% 1000) * 1000;
3892 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3894 qemu_mutex_unlock_iothread();
3895 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3896 qemu_mutex_lock_iothread();
3898 IOHandlerRecord
**pioh
;
3900 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3901 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3902 ioh
->fd_read(ioh
->opaque
);
3904 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3905 ioh
->fd_write(ioh
->opaque
);
3909 /* remove deleted IO handlers */
3910 pioh
= &first_io_handler
;
3921 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3923 /* rearm timer, if not periodic */
3924 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3925 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3926 qemu_rearm_alarm_timer(alarm_timer
);
3929 /* vm time timers */
3931 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3932 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3933 qemu_get_clock(vm_clock
));
3936 /* real time timers */
3937 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3938 qemu_get_clock(rt_clock
));
3940 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3941 qemu_get_clock(host_clock
));
3943 /* Check bottom-halves last in case any of the earlier events triggered
3949 static int qemu_cpu_exec(CPUState
*env
)
3952 #ifdef CONFIG_PROFILER
3956 #ifdef CONFIG_PROFILER
3957 ti
= profile_getclock();
3962 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3963 env
->icount_decr
.u16
.low
= 0;
3964 env
->icount_extra
= 0;
3965 count
= qemu_next_deadline();
3966 count
= (count
+ (1 << icount_time_shift
) - 1)
3967 >> icount_time_shift
;
3968 qemu_icount
+= count
;
3969 decr
= (count
> 0xffff) ? 0xffff : count
;
3971 env
->icount_decr
.u16
.low
= decr
;
3972 env
->icount_extra
= count
;
3974 ret
= cpu_exec(env
);
3975 #ifdef CONFIG_PROFILER
3976 qemu_time
+= profile_getclock() - ti
;
3979 /* Fold pending instructions back into the
3980 instruction counter, and clear the interrupt flag. */
3981 qemu_icount
-= (env
->icount_decr
.u16
.low
3982 + env
->icount_extra
);
3983 env
->icount_decr
.u32
= 0;
3984 env
->icount_extra
= 0;
3989 static void tcg_cpu_exec(void)
3993 if (next_cpu
== NULL
)
3994 next_cpu
= first_cpu
;
3995 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3996 CPUState
*env
= cur_cpu
= next_cpu
;
4000 if (timer_alarm_pending
) {
4001 timer_alarm_pending
= 0;
4004 if (cpu_can_run(env
))
4005 ret
= qemu_cpu_exec(env
);
4006 if (ret
== EXCP_DEBUG
) {
4007 gdb_set_stop_cpu(env
);
4008 debug_requested
= 1;
4014 static int cpu_has_work(CPUState
*env
)
4022 if (qemu_cpu_has_work(env
))
4027 static int tcg_has_work(void)
4031 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4032 if (cpu_has_work(env
))
4037 static int qemu_calculate_timeout(void)
4039 #ifndef CONFIG_IOTHREAD
4044 else if (tcg_has_work())
4046 else if (!use_icount
)
4049 /* XXX: use timeout computed from timers */
4052 /* Advance virtual time to the next event. */
4053 if (use_icount
== 1) {
4054 /* When not using an adaptive execution frequency
4055 we tend to get badly out of sync with real time,
4056 so just delay for a reasonable amount of time. */
4059 delta
= cpu_get_icount() - cpu_get_clock();
4062 /* If virtual time is ahead of real time then just
4064 timeout
= (delta
/ 1000000) + 1;
4066 /* Wait for either IO to occur or the next
4068 add
= qemu_next_deadline();
4069 /* We advance the timer before checking for IO.
4070 Limit the amount we advance so that early IO
4071 activity won't get the guest too far ahead. */
4075 add
= (add
+ (1 << icount_time_shift
) - 1)
4076 >> icount_time_shift
;
4078 timeout
= delta
/ 1000000;
4085 #else /* CONFIG_IOTHREAD */
4090 static int vm_can_run(void)
4092 if (powerdown_requested
)
4094 if (reset_requested
)
4096 if (shutdown_requested
)
4098 if (debug_requested
)
4103 qemu_irq qemu_system_powerdown
;
4105 static void main_loop(void)
4109 #ifdef CONFIG_IOTHREAD
4110 qemu_system_ready
= 1;
4111 qemu_cond_broadcast(&qemu_system_cond
);
4116 #ifdef CONFIG_PROFILER
4119 #ifndef CONFIG_IOTHREAD
4122 #ifdef CONFIG_PROFILER
4123 ti
= profile_getclock();
4125 main_loop_wait(qemu_calculate_timeout());
4126 #ifdef CONFIG_PROFILER
4127 dev_time
+= profile_getclock() - ti
;
4129 } while (vm_can_run());
4131 if (qemu_debug_requested()) {
4132 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
4133 vm_stop(EXCP_DEBUG
);
4135 if (qemu_shutdown_requested()) {
4136 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4143 if (qemu_reset_requested()) {
4144 monitor_protocol_event(QEVENT_RESET
, NULL
);
4146 qemu_system_reset();
4149 if (qemu_powerdown_requested()) {
4150 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4151 qemu_irq_raise(qemu_system_powerdown
);
4153 if ((r
= qemu_vmstop_requested())) {
4154 monitor_protocol_event(QEVENT_STOP
, NULL
);
4161 static void version(void)
4163 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4166 static void help(int exitcode
)
4169 printf("usage: %s [options] [disk_image]\n"
4171 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4173 #define DEF(option, opt_arg, opt_enum, opt_help) \
4175 #define DEFHEADING(text) stringify(text) "\n"
4176 #include "qemu-options.h"
4181 "During emulation, the following keys are useful:\n"
4182 "ctrl-alt-f toggle full screen\n"
4183 "ctrl-alt-n switch to virtual console 'n'\n"
4184 "ctrl-alt toggle mouse and keyboard grab\n"
4186 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4191 DEFAULT_NETWORK_SCRIPT
,
4192 DEFAULT_NETWORK_DOWN_SCRIPT
,
4194 DEFAULT_GDBSTUB_PORT
,
4199 #define HAS_ARG 0x0001
4202 #define DEF(option, opt_arg, opt_enum, opt_help) \
4204 #define DEFHEADING(text)
4205 #include "qemu-options.h"
4211 typedef struct QEMUOption
{
4217 static const QEMUOption qemu_options
[] = {
4218 { "h", 0, QEMU_OPTION_h
},
4219 #define DEF(option, opt_arg, opt_enum, opt_help) \
4220 { option, opt_arg, opt_enum },
4221 #define DEFHEADING(text)
4222 #include "qemu-options.h"
4230 struct soundhw soundhw
[] = {
4231 #ifdef HAS_AUDIO_CHOICE
4232 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4238 { .init_isa
= pcspk_audio_init
}
4245 "Creative Sound Blaster 16",
4248 { .init_isa
= SB16_init
}
4252 #ifdef CONFIG_CS4231A
4258 { .init_isa
= cs4231a_init
}
4266 "Yamaha YMF262 (OPL3)",
4268 "Yamaha YM3812 (OPL2)",
4272 { .init_isa
= Adlib_init
}
4279 "Gravis Ultrasound GF1",
4282 { .init_isa
= GUS_init
}
4289 "Intel 82801AA AC97 Audio",
4292 { .init_pci
= ac97_init
}
4296 #ifdef CONFIG_ES1370
4299 "ENSONIQ AudioPCI ES1370",
4302 { .init_pci
= es1370_init
}
4306 #endif /* HAS_AUDIO_CHOICE */
4308 { NULL
, NULL
, 0, 0, { NULL
} }
4311 static void select_soundhw (const char *optarg
)
4315 if (*optarg
== '?') {
4318 printf ("Valid sound card names (comma separated):\n");
4319 for (c
= soundhw
; c
->name
; ++c
) {
4320 printf ("%-11s %s\n", c
->name
, c
->descr
);
4322 printf ("\n-soundhw all will enable all of the above\n");
4323 exit (*optarg
!= '?');
4331 if (!strcmp (optarg
, "all")) {
4332 for (c
= soundhw
; c
->name
; ++c
) {
4340 e
= strchr (p
, ',');
4341 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4343 for (c
= soundhw
; c
->name
; ++c
) {
4344 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4353 "Unknown sound card name (too big to show)\n");
4356 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4361 p
+= l
+ (e
!= NULL
);
4365 goto show_valid_cards
;
4370 static void select_vgahw (const char *p
)
4374 vga_interface_type
= VGA_NONE
;
4375 if (strstart(p
, "std", &opts
)) {
4376 vga_interface_type
= VGA_STD
;
4377 } else if (strstart(p
, "cirrus", &opts
)) {
4378 vga_interface_type
= VGA_CIRRUS
;
4379 } else if (strstart(p
, "vmware", &opts
)) {
4380 vga_interface_type
= VGA_VMWARE
;
4381 } else if (strstart(p
, "xenfb", &opts
)) {
4382 vga_interface_type
= VGA_XENFB
;
4383 } else if (!strstart(p
, "none", &opts
)) {
4385 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4389 const char *nextopt
;
4391 if (strstart(opts
, ",retrace=", &nextopt
)) {
4393 if (strstart(opts
, "dumb", &nextopt
))
4394 vga_retrace_method
= VGA_RETRACE_DUMB
;
4395 else if (strstart(opts
, "precise", &nextopt
))
4396 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4397 else goto invalid_vga
;
4398 } else goto invalid_vga
;
4404 static int balloon_parse(const char *arg
)
4408 if (strcmp(arg
, "none") == 0) {
4412 if (!strncmp(arg
, "virtio", 6)) {
4413 if (arg
[6] == ',') {
4414 /* have params -> parse them */
4415 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4419 /* create empty opts */
4420 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4422 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4431 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4433 exit(STATUS_CONTROL_C_EXIT
);
4438 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4442 if(strlen(str
) != 36)
4445 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4446 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4447 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4453 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4461 static void termsig_handler(int signal
)
4463 qemu_system_shutdown_request();
4466 static void sigchld_handler(int signal
)
4468 waitpid(-1, NULL
, WNOHANG
);
4471 static void sighandler_setup(void)
4473 struct sigaction act
;
4475 memset(&act
, 0, sizeof(act
));
4476 act
.sa_handler
= termsig_handler
;
4477 sigaction(SIGINT
, &act
, NULL
);
4478 sigaction(SIGHUP
, &act
, NULL
);
4479 sigaction(SIGTERM
, &act
, NULL
);
4481 act
.sa_handler
= sigchld_handler
;
4482 act
.sa_flags
= SA_NOCLDSTOP
;
4483 sigaction(SIGCHLD
, &act
, NULL
);
4489 /* Look for support files in the same directory as the executable. */
4490 static char *find_datadir(const char *argv0
)
4496 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4503 while (p
!= buf
&& *p
!= '\\')
4506 if (access(buf
, R_OK
) == 0) {
4507 return qemu_strdup(buf
);
4513 /* Find a likely location for support files using the location of the binary.
4514 For installed binaries this will be "$bindir/../share/qemu". When
4515 running from the build tree this will be "$bindir/../pc-bios". */
4516 #define SHARE_SUFFIX "/share/qemu"
4517 #define BUILD_SUFFIX "/pc-bios"
4518 static char *find_datadir(const char *argv0
)
4526 #if defined(__linux__)
4529 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4535 #elif defined(__FreeBSD__)
4538 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4545 /* If we don't have any way of figuring out the actual executable
4546 location then try argv[0]. */
4548 p
= realpath(argv0
, buf
);
4556 max_len
= strlen(dir
) +
4557 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4558 res
= qemu_mallocz(max_len
);
4559 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4560 if (access(res
, R_OK
)) {
4561 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4562 if (access(res
, R_OK
)) {
4574 char *qemu_find_file(int type
, const char *name
)
4580 /* If name contains path separators then try it as a straight path. */
4581 if ((strchr(name
, '/') || strchr(name
, '\\'))
4582 && access(name
, R_OK
) == 0) {
4583 return qemu_strdup(name
);
4586 case QEMU_FILE_TYPE_BIOS
:
4589 case QEMU_FILE_TYPE_KEYMAP
:
4590 subdir
= "keymaps/";
4595 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4596 buf
= qemu_mallocz(len
);
4597 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4598 if (access(buf
, R_OK
)) {
4605 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4609 dev
= qdev_device_add(opts
);
4615 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4617 CharDriverState
*chr
;
4619 chr
= qemu_chr_open_opts(opts
, NULL
);
4625 struct device_config
{
4627 DEV_USB
, /* -usbdevice */
4629 DEV_SERIAL
, /* -serial */
4630 DEV_PARALLEL
, /* -parallel */
4632 const char *cmdline
;
4633 QTAILQ_ENTRY(device_config
) next
;
4635 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4637 static void add_device_config(int type
, const char *cmdline
)
4639 struct device_config
*conf
;
4641 conf
= qemu_mallocz(sizeof(*conf
));
4643 conf
->cmdline
= cmdline
;
4644 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4647 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4649 struct device_config
*conf
;
4652 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4653 if (conf
->type
!= type
)
4655 rc
= func(conf
->cmdline
);
4662 static void serial_monitor_mux(const char *monitor_devices
[])
4664 struct device_config
*serial
;
4665 const char *devname
;
4667 if (strcmp(monitor_devices
[0],"stdio") != 0)
4669 QTAILQ_FOREACH(serial
, &device_configs
, next
) {
4670 if (serial
->type
!= DEV_SERIAL
)
4672 devname
= serial
->cmdline
;
4673 if (devname
&& !strcmp(devname
,"mon:stdio")) {
4674 monitor_devices
[0] = NULL
;
4676 } else if (devname
&& !strcmp(devname
,"stdio")) {
4677 monitor_devices
[0] = NULL
;
4678 serial
->cmdline
= "mon:stdio";
4684 static int serial_parse(const char *devname
)
4686 static int index
= 0;
4689 if (strcmp(devname
, "none") == 0)
4691 if (index
== MAX_SERIAL_PORTS
) {
4692 fprintf(stderr
, "qemu: too many serial ports\n");
4695 snprintf(label
, sizeof(label
), "serial%d", index
);
4696 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4697 if (!serial_hds
[index
]) {
4698 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4699 devname
, strerror(errno
));
4706 static int parallel_parse(const char *devname
)
4708 static int index
= 0;
4711 if (strcmp(devname
, "none") == 0)
4713 if (index
== MAX_PARALLEL_PORTS
) {
4714 fprintf(stderr
, "qemu: too many parallel ports\n");
4717 snprintf(label
, sizeof(label
), "parallel%d", index
);
4718 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4719 if (!parallel_hds
[index
]) {
4720 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4721 devname
, strerror(errno
));
4728 int main(int argc
, char **argv
, char **envp
)
4730 const char *gdbstub_dev
= NULL
;
4731 uint32_t boot_devices_bitmap
= 0;
4733 int snapshot
, linux_boot
, net_boot
;
4734 const char *initrd_filename
;
4735 const char *kernel_filename
, *kernel_cmdline
;
4736 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4738 DisplayChangeListener
*dcl
;
4739 int cyls
, heads
, secs
, translation
;
4740 QemuOpts
*hda_opts
= NULL
, *opts
;
4742 const char *r
, *optarg
;
4743 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4744 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4745 int monitor_device_index
;
4746 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4747 int virtio_console_index
;
4748 const char *loadvm
= NULL
;
4749 QEMUMachine
*machine
;
4750 const char *cpu_model
;
4755 const char *pid_file
= NULL
;
4756 const char *incoming
= NULL
;
4759 struct passwd
*pwd
= NULL
;
4760 const char *chroot_dir
= NULL
;
4761 const char *run_as
= NULL
;
4764 int show_vnc_port
= 0;
4768 qemu_errors_to_file(stderr
);
4769 qemu_cache_utils_init(envp
);
4771 QLIST_INIT (&vm_change_state_head
);
4774 struct sigaction act
;
4775 sigfillset(&act
.sa_mask
);
4777 act
.sa_handler
= SIG_IGN
;
4778 sigaction(SIGPIPE
, &act
, NULL
);
4781 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4782 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4783 QEMU to run on a single CPU */
4788 h
= GetCurrentProcess();
4789 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4790 for(i
= 0; i
< 32; i
++) {
4791 if (mask
& (1 << i
))
4796 SetProcessAffinityMask(h
, mask
);
4802 module_call_init(MODULE_INIT_MACHINE
);
4803 machine
= find_default_machine();
4805 initrd_filename
= NULL
;
4808 kernel_filename
= NULL
;
4809 kernel_cmdline
= "";
4810 cyls
= heads
= secs
= 0;
4811 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4813 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4814 virtio_consoles
[i
] = NULL
;
4815 virtio_console_index
= 0;
4817 monitor_devices
[0] = "vc:80Cx24C";
4818 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4819 monitor_devices
[i
] = NULL
;
4821 monitor_device_index
= 0;
4823 for (i
= 0; i
< MAX_NODES
; i
++) {
4825 node_cpumask
[i
] = 0;
4840 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4842 const QEMUOption
*popt
;
4845 /* Treat --foo the same as -foo. */
4848 popt
= qemu_options
;
4851 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4855 if (!strcmp(popt
->name
, r
+ 1))
4859 if (popt
->flags
& HAS_ARG
) {
4860 if (optind
>= argc
) {
4861 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4865 optarg
= argv
[optind
++];
4870 switch(popt
->index
) {
4872 machine
= find_machine(optarg
);
4875 printf("Supported machines are:\n");
4876 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4878 printf("%-10s %s (alias of %s)\n",
4879 m
->alias
, m
->desc
, m
->name
);
4880 printf("%-10s %s%s\n",
4882 m
->is_default
? " (default)" : "");
4884 exit(*optarg
!= '?');
4887 case QEMU_OPTION_cpu
:
4888 /* hw initialization will check this */
4889 if (*optarg
== '?') {
4890 /* XXX: implement xxx_cpu_list for targets that still miss it */
4891 #if defined(cpu_list)
4892 cpu_list(stdout
, &fprintf
);
4899 case QEMU_OPTION_initrd
:
4900 initrd_filename
= optarg
;
4902 case QEMU_OPTION_hda
:
4904 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4906 hda_opts
= drive_add(optarg
, HD_ALIAS
4907 ",cyls=%d,heads=%d,secs=%d%s",
4908 0, cyls
, heads
, secs
,
4909 translation
== BIOS_ATA_TRANSLATION_LBA
?
4911 translation
== BIOS_ATA_TRANSLATION_NONE
?
4912 ",trans=none" : "");
4914 case QEMU_OPTION_hdb
:
4915 case QEMU_OPTION_hdc
:
4916 case QEMU_OPTION_hdd
:
4917 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4919 case QEMU_OPTION_drive
:
4920 drive_add(NULL
, "%s", optarg
);
4922 case QEMU_OPTION_set
:
4923 if (qemu_set_option(optarg
) != 0)
4926 case QEMU_OPTION_global
:
4927 if (qemu_global_option(optarg
) != 0)
4930 case QEMU_OPTION_mtdblock
:
4931 drive_add(optarg
, MTD_ALIAS
);
4933 case QEMU_OPTION_sd
:
4934 drive_add(optarg
, SD_ALIAS
);
4936 case QEMU_OPTION_pflash
:
4937 drive_add(optarg
, PFLASH_ALIAS
);
4939 case QEMU_OPTION_snapshot
:
4942 case QEMU_OPTION_hdachs
:
4946 cyls
= strtol(p
, (char **)&p
, 0);
4947 if (cyls
< 1 || cyls
> 16383)
4952 heads
= strtol(p
, (char **)&p
, 0);
4953 if (heads
< 1 || heads
> 16)
4958 secs
= strtol(p
, (char **)&p
, 0);
4959 if (secs
< 1 || secs
> 63)
4963 if (!strcmp(p
, "none"))
4964 translation
= BIOS_ATA_TRANSLATION_NONE
;
4965 else if (!strcmp(p
, "lba"))
4966 translation
= BIOS_ATA_TRANSLATION_LBA
;
4967 else if (!strcmp(p
, "auto"))
4968 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4971 } else if (*p
!= '\0') {
4973 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4976 if (hda_opts
!= NULL
) {
4978 snprintf(num
, sizeof(num
), "%d", cyls
);
4979 qemu_opt_set(hda_opts
, "cyls", num
);
4980 snprintf(num
, sizeof(num
), "%d", heads
);
4981 qemu_opt_set(hda_opts
, "heads", num
);
4982 snprintf(num
, sizeof(num
), "%d", secs
);
4983 qemu_opt_set(hda_opts
, "secs", num
);
4984 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4985 qemu_opt_set(hda_opts
, "trans", "lba");
4986 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4987 qemu_opt_set(hda_opts
, "trans", "none");
4991 case QEMU_OPTION_numa
:
4992 if (nb_numa_nodes
>= MAX_NODES
) {
4993 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4998 case QEMU_OPTION_nographic
:
4999 display_type
= DT_NOGRAPHIC
;
5001 #ifdef CONFIG_CURSES
5002 case QEMU_OPTION_curses
:
5003 display_type
= DT_CURSES
;
5006 case QEMU_OPTION_portrait
:
5009 case QEMU_OPTION_kernel
:
5010 kernel_filename
= optarg
;
5012 case QEMU_OPTION_append
:
5013 kernel_cmdline
= optarg
;
5015 case QEMU_OPTION_cdrom
:
5016 drive_add(optarg
, CDROM_ALIAS
);
5018 case QEMU_OPTION_boot
:
5020 static const char * const params
[] = {
5021 "order", "once", "menu", NULL
5023 char buf
[sizeof(boot_devices
)];
5024 char *standard_boot_devices
;
5027 if (!strchr(optarg
, '=')) {
5029 pstrcpy(buf
, sizeof(buf
), optarg
);
5030 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5032 "qemu: unknown boot parameter '%s' in '%s'\n",
5038 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5039 boot_devices_bitmap
= parse_bootdevices(buf
);
5040 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5043 if (get_param_value(buf
, sizeof(buf
),
5045 boot_devices_bitmap
|= parse_bootdevices(buf
);
5046 standard_boot_devices
= qemu_strdup(boot_devices
);
5047 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5048 qemu_register_reset(restore_boot_devices
,
5049 standard_boot_devices
);
5051 if (get_param_value(buf
, sizeof(buf
),
5053 if (!strcmp(buf
, "on")) {
5055 } else if (!strcmp(buf
, "off")) {
5059 "qemu: invalid option value '%s'\n",
5067 case QEMU_OPTION_fda
:
5068 case QEMU_OPTION_fdb
:
5069 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5072 case QEMU_OPTION_no_fd_bootchk
:
5076 case QEMU_OPTION_netdev
:
5077 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5081 case QEMU_OPTION_net
:
5082 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5087 case QEMU_OPTION_tftp
:
5088 legacy_tftp_prefix
= optarg
;
5090 case QEMU_OPTION_bootp
:
5091 legacy_bootp_filename
= optarg
;
5094 case QEMU_OPTION_smb
:
5095 if (net_slirp_smb(optarg
) < 0)
5099 case QEMU_OPTION_redir
:
5100 if (net_slirp_redir(optarg
) < 0)
5104 case QEMU_OPTION_bt
:
5105 add_device_config(DEV_BT
, optarg
);
5108 case QEMU_OPTION_audio_help
:
5112 case QEMU_OPTION_soundhw
:
5113 select_soundhw (optarg
);
5119 case QEMU_OPTION_version
:
5123 case QEMU_OPTION_m
: {
5127 value
= strtoul(optarg
, &ptr
, 10);
5129 case 0: case 'M': case 'm':
5136 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5140 /* On 32-bit hosts, QEMU is limited by virtual address space */
5141 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5142 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5145 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5146 fprintf(stderr
, "qemu: ram size too large\n");
5155 const CPULogItem
*item
;
5157 mask
= cpu_str_to_log_mask(optarg
);
5159 printf("Log items (comma separated):\n");
5160 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5161 printf("%-10s %s\n", item
->name
, item
->help
);
5169 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5171 case QEMU_OPTION_gdb
:
5172 gdbstub_dev
= optarg
;
5177 case QEMU_OPTION_bios
:
5180 case QEMU_OPTION_singlestep
:
5187 keyboard_layout
= optarg
;
5189 case QEMU_OPTION_localtime
:
5192 case QEMU_OPTION_vga
:
5193 select_vgahw (optarg
);
5195 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5201 w
= strtol(p
, (char **)&p
, 10);
5204 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5210 h
= strtol(p
, (char **)&p
, 10);
5215 depth
= strtol(p
, (char **)&p
, 10);
5216 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5217 depth
!= 24 && depth
!= 32)
5219 } else if (*p
== '\0') {
5220 depth
= graphic_depth
;
5227 graphic_depth
= depth
;
5231 case QEMU_OPTION_echr
:
5234 term_escape_char
= strtol(optarg
, &r
, 0);
5236 printf("Bad argument to echr\n");
5239 case QEMU_OPTION_monitor
:
5240 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5241 fprintf(stderr
, "qemu: too many monitor devices\n");
5244 monitor_devices
[monitor_device_index
] = optarg
;
5245 monitor_device_index
++;
5247 case QEMU_OPTION_chardev
:
5248 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5250 fprintf(stderr
, "parse error: %s\n", optarg
);
5254 case QEMU_OPTION_serial
:
5255 add_device_config(DEV_SERIAL
, optarg
);
5258 case QEMU_OPTION_watchdog
:
5261 "qemu: only one watchdog option may be given\n");
5266 case QEMU_OPTION_watchdog_action
:
5267 if (select_watchdog_action(optarg
) == -1) {
5268 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5272 case QEMU_OPTION_virtiocon
:
5273 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5274 fprintf(stderr
, "qemu: too many virtio consoles\n");
5277 virtio_consoles
[virtio_console_index
] = optarg
;
5278 virtio_console_index
++;
5280 case QEMU_OPTION_parallel
:
5281 add_device_config(DEV_PARALLEL
, optarg
);
5282 default_parallel
= 0;
5284 case QEMU_OPTION_loadvm
:
5287 case QEMU_OPTION_full_screen
:
5291 case QEMU_OPTION_no_frame
:
5294 case QEMU_OPTION_alt_grab
:
5297 case QEMU_OPTION_ctrl_grab
:
5300 case QEMU_OPTION_no_quit
:
5303 case QEMU_OPTION_sdl
:
5304 display_type
= DT_SDL
;
5307 case QEMU_OPTION_pidfile
:
5311 case QEMU_OPTION_win2k_hack
:
5312 win2k_install_hack
= 1;
5314 case QEMU_OPTION_rtc_td_hack
:
5317 case QEMU_OPTION_acpitable
:
5318 if(acpi_table_add(optarg
) < 0) {
5319 fprintf(stderr
, "Wrong acpi table provided\n");
5323 case QEMU_OPTION_smbios
:
5324 if(smbios_entry_add(optarg
) < 0) {
5325 fprintf(stderr
, "Wrong smbios provided\n");
5331 case QEMU_OPTION_enable_kvm
:
5335 case QEMU_OPTION_usb
:
5338 case QEMU_OPTION_usbdevice
:
5340 add_device_config(DEV_USB
, optarg
);
5342 case QEMU_OPTION_device
:
5343 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5347 case QEMU_OPTION_smp
:
5350 fprintf(stderr
, "Invalid number of CPUs\n");
5353 if (max_cpus
< smp_cpus
) {
5354 fprintf(stderr
, "maxcpus must be equal to or greater than "
5358 if (max_cpus
> 255) {
5359 fprintf(stderr
, "Unsupported number of maxcpus\n");
5363 case QEMU_OPTION_vnc
:
5364 display_type
= DT_VNC
;
5365 vnc_display
= optarg
;
5368 case QEMU_OPTION_no_acpi
:
5371 case QEMU_OPTION_no_hpet
:
5374 case QEMU_OPTION_balloon
:
5375 if (balloon_parse(optarg
) < 0) {
5376 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5381 case QEMU_OPTION_no_reboot
:
5384 case QEMU_OPTION_no_shutdown
:
5387 case QEMU_OPTION_show_cursor
:
5390 case QEMU_OPTION_uuid
:
5391 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5392 fprintf(stderr
, "Fail to parse UUID string."
5393 " Wrong format.\n");
5398 case QEMU_OPTION_daemonize
:
5402 case QEMU_OPTION_option_rom
:
5403 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5404 fprintf(stderr
, "Too many option ROMs\n");
5407 option_rom
[nb_option_roms
] = optarg
;
5410 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5411 case QEMU_OPTION_semihosting
:
5412 semihosting_enabled
= 1;
5415 case QEMU_OPTION_name
:
5416 qemu_name
= qemu_strdup(optarg
);
5418 char *p
= strchr(qemu_name
, ',');
5421 if (strncmp(p
, "process=", 8)) {
5422 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5430 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5431 case QEMU_OPTION_prom_env
:
5432 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5433 fprintf(stderr
, "Too many prom variables\n");
5436 prom_envs
[nb_prom_envs
] = optarg
;
5441 case QEMU_OPTION_old_param
:
5445 case QEMU_OPTION_clock
:
5446 configure_alarms(optarg
);
5448 case QEMU_OPTION_startdate
:
5449 configure_rtc_date_offset(optarg
, 1);
5451 case QEMU_OPTION_rtc
:
5452 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5454 fprintf(stderr
, "parse error: %s\n", optarg
);
5457 configure_rtc(opts
);
5459 case QEMU_OPTION_tb_size
:
5460 tb_size
= strtol(optarg
, NULL
, 0);
5464 case QEMU_OPTION_icount
:
5466 if (strcmp(optarg
, "auto") == 0) {
5467 icount_time_shift
= -1;
5469 icount_time_shift
= strtol(optarg
, NULL
, 0);
5472 case QEMU_OPTION_incoming
:
5476 case QEMU_OPTION_chroot
:
5477 chroot_dir
= optarg
;
5479 case QEMU_OPTION_runas
:
5484 case QEMU_OPTION_xen_domid
:
5485 xen_domid
= atoi(optarg
);
5487 case QEMU_OPTION_xen_create
:
5488 xen_mode
= XEN_CREATE
;
5490 case QEMU_OPTION_xen_attach
:
5491 xen_mode
= XEN_ATTACH
;
5494 case QEMU_OPTION_readconfig
:
5497 fp
= fopen(optarg
, "r");
5499 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5502 if (qemu_config_parse(fp
) != 0) {
5508 case QEMU_OPTION_writeconfig
:
5511 if (strcmp(optarg
, "-") == 0) {
5514 fp
= fopen(optarg
, "w");
5516 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5520 qemu_config_write(fp
);
5528 /* If no data_dir is specified then try to find it relative to the
5531 data_dir
= find_datadir(argv
[0]);
5533 /* If all else fails use the install patch specified when building. */
5535 data_dir
= CONFIG_QEMU_SHAREDIR
;
5539 * Default to max_cpus = smp_cpus, in case the user doesn't
5540 * specify a max_cpus value.
5543 max_cpus
= smp_cpus
;
5545 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5546 if (smp_cpus
> machine
->max_cpus
) {
5547 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5548 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5553 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5555 if (display_type
== DT_NOGRAPHIC
) {
5557 add_device_config(DEV_SERIAL
, "stdio");
5558 if (default_parallel
)
5559 add_device_config(DEV_PARALLEL
, "null");
5560 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5561 monitor_devices
[0] = "stdio";
5565 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5566 if (default_parallel
)
5567 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5570 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5577 if (pipe(fds
) == -1)
5588 len
= read(fds
[0], &status
, 1);
5589 if (len
== -1 && (errno
== EINTR
))
5594 else if (status
== 1) {
5595 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5603 qemu_set_cloexec(fds
[1]);
5615 signal(SIGTSTP
, SIG_IGN
);
5616 signal(SIGTTOU
, SIG_IGN
);
5617 signal(SIGTTIN
, SIG_IGN
);
5620 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5623 write(fds
[1], &status
, 1);
5625 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5630 if (kvm_enabled()) {
5633 ret
= kvm_init(smp_cpus
);
5635 fprintf(stderr
, "failed to initialize KVM\n");
5640 if (qemu_init_main_loop()) {
5641 fprintf(stderr
, "qemu_init_main_loop failed\n");
5644 linux_boot
= (kernel_filename
!= NULL
);
5646 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5647 fprintf(stderr
, "-append only allowed with -kernel option\n");
5651 if (!linux_boot
&& initrd_filename
!= NULL
) {
5652 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5657 /* Win32 doesn't support line-buffering and requires size >= 2 */
5658 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5661 if (init_timer_alarm() < 0) {
5662 fprintf(stderr
, "could not initialize alarm timer\n");
5665 if (use_icount
&& icount_time_shift
< 0) {
5667 /* 125MIPS seems a reasonable initial guess at the guest speed.
5668 It will be corrected fairly quickly anyway. */
5669 icount_time_shift
= 3;
5670 init_icount_adjust();
5677 if (net_init_clients() < 0) {
5681 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5682 net_set_boot_mask(net_boot
);
5684 /* init the bluetooth world */
5685 if (foreach_device_config(DEV_BT
, bt_parse
))
5688 /* init the memory */
5690 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5692 /* init the dynamic translator */
5693 cpu_exec_init_all(tb_size
* 1024 * 1024);
5695 bdrv_init_with_whitelist();
5699 /* we always create the cdrom drive, even if no disk is there */
5700 drive_add(NULL
, CDROM_ALIAS
);
5702 /* we always create at least one floppy */
5703 drive_add(NULL
, FD_ALIAS
, 0);
5705 /* we always create one sd slot, even if no card is in it */
5706 drive_add(NULL
, SD_ALIAS
);
5708 /* open the virtual block devices */
5710 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5711 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5714 vmstate_register(0, &vmstate_timers
,&timers_state
);
5715 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5718 /* Maintain compatibility with multiple stdio monitors */
5719 serial_monitor_mux(monitor_devices
);
5721 if (nb_numa_nodes
> 0) {
5724 if (nb_numa_nodes
> smp_cpus
) {
5725 nb_numa_nodes
= smp_cpus
;
5728 /* If no memory size if given for any node, assume the default case
5729 * and distribute the available memory equally across all nodes
5731 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5732 if (node_mem
[i
] != 0)
5735 if (i
== nb_numa_nodes
) {
5736 uint64_t usedmem
= 0;
5738 /* On Linux, the each node's border has to be 8MB aligned,
5739 * the final node gets the rest.
5741 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5742 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5743 usedmem
+= node_mem
[i
];
5745 node_mem
[i
] = ram_size
- usedmem
;
5748 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5749 if (node_cpumask
[i
] != 0)
5752 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5753 * must cope with this anyway, because there are BIOSes out there in
5754 * real machines which also use this scheme.
5756 if (i
== nb_numa_nodes
) {
5757 for (i
= 0; i
< smp_cpus
; i
++) {
5758 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5763 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5764 const char *devname
= monitor_devices
[i
];
5765 if (devname
&& strcmp(devname
, "none")) {
5768 snprintf(label
, sizeof(label
), "monitor");
5770 snprintf(label
, sizeof(label
), "monitor%d", i
);
5772 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5773 if (!monitor_hds
[i
]) {
5774 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5781 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5783 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5786 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5787 const char *devname
= virtio_consoles
[i
];
5788 if (devname
&& strcmp(devname
, "none")) {
5790 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5791 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5792 if (!virtcon_hds
[i
]) {
5793 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5794 devname
, strerror(errno
));
5800 module_call_init(MODULE_INIT_DEVICE
);
5803 i
= select_watchdog(watchdog
);
5805 exit (i
== 1 ? 1 : 0);
5808 if (machine
->compat_props
) {
5809 qdev_prop_register_global_list(machine
->compat_props
);
5813 machine
->init(ram_size
, boot_devices
,
5814 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5818 /* must be after terminal init, SDL library changes signal handlers */
5822 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5823 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5824 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5830 current_machine
= machine
;
5832 /* init USB devices */
5834 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5838 /* init generic devices */
5839 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5843 dumb_display_init();
5844 /* just use the first displaystate for the moment */
5847 if (display_type
== DT_DEFAULT
) {
5848 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5849 display_type
= DT_SDL
;
5851 display_type
= DT_VNC
;
5852 vnc_display
= "localhost:0,to=99";
5858 switch (display_type
) {
5861 #if defined(CONFIG_CURSES)
5863 curses_display_init(ds
, full_screen
);
5866 #if defined(CONFIG_SDL)
5868 sdl_display_init(ds
, full_screen
, no_frame
);
5870 #elif defined(CONFIG_COCOA)
5872 cocoa_display_init(ds
, full_screen
);
5876 vnc_display_init(ds
);
5877 if (vnc_display_open(ds
, vnc_display
) < 0)
5880 if (show_vnc_port
) {
5881 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5889 dcl
= ds
->listeners
;
5890 while (dcl
!= NULL
) {
5891 if (dcl
->dpy_refresh
!= NULL
) {
5892 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5893 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5898 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5899 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5900 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5903 text_consoles_set_display(display_state
);
5905 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5906 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5907 monitor_init(monitor_hds
[i
],
5908 MONITOR_USE_READLINE
|
5909 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5913 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5914 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5919 qdev_machine_creation_done();
5923 qemu_system_reset();
5925 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5931 qemu_start_incoming_migration(incoming
);
5932 } else if (autostart
) {
5942 len
= write(fds
[1], &status
, 1);
5943 if (len
== -1 && (errno
== EINTR
))
5950 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5956 pwd
= getpwnam(run_as
);
5958 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5964 if (chroot(chroot_dir
) < 0) {
5965 fprintf(stderr
, "chroot failed\n");
5972 if (setgid(pwd
->pw_gid
) < 0) {
5973 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5976 if (setuid(pwd
->pw_uid
) < 0) {
5977 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5980 if (setuid(0) != -1) {
5981 fprintf(stderr
, "Dropping privileges failed\n");