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"
159 #include "qemu-objects.h"
163 #include "exec-all.h"
165 #include "qemu_socket.h"
167 #include "slirp/libslirp.h"
169 #include "qemu-queue.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 #define MAX_VIRTIO_CONSOLES 1
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_NONE
;
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;
276 static int default_virtcon
= 1;
277 static int default_monitor
= 1;
278 static int default_vga
= 1;
279 static int default_floppy
= 1;
280 static int default_cdrom
= 1;
281 static int default_sdcard
= 1;
287 { .driver
= "isa-serial", .flag
= &default_serial
},
288 { .driver
= "isa-parallel", .flag
= &default_parallel
},
289 { .driver
= "isa-fdc", .flag
= &default_floppy
},
290 { .driver
= "ide-drive", .flag
= &default_cdrom
},
291 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
293 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
294 { .driver
= "VGA", .flag
= &default_vga
},
295 { .driver
= "cirrus-vga", .flag
= &default_vga
},
296 { .driver
= "vmware-svga", .flag
= &default_vga
},
299 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
301 const char *driver
= qemu_opt_get(opts
, "driver");
306 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
307 if (strcmp(default_list
[i
].driver
, driver
) != 0)
309 *(default_list
[i
].flag
) = 0;
314 /***********************************************************/
315 /* x86 ISA bus support */
317 target_phys_addr_t isa_mem_base
= 0;
320 /***********************************************************/
321 void hw_error(const char *fmt
, ...)
327 fprintf(stderr
, "qemu: hardware error: ");
328 vfprintf(stderr
, fmt
, ap
);
329 fprintf(stderr
, "\n");
330 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
331 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
333 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
335 cpu_dump_state(env
, stderr
, fprintf
, 0);
342 static void set_proc_name(const char *s
)
344 #if defined(__linux__) && defined(PR_SET_NAME)
348 name
[sizeof(name
) - 1] = 0;
349 strncpy(name
, s
, sizeof(name
));
350 /* Could rewrite argv[0] too, but that's a bit more complicated.
351 This simple way is enough for `top'. */
352 prctl(PR_SET_NAME
, name
);
359 static QEMUBalloonEvent
*qemu_balloon_event
;
360 void *qemu_balloon_event_opaque
;
362 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
364 qemu_balloon_event
= func
;
365 qemu_balloon_event_opaque
= opaque
;
368 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
370 if (qemu_balloon_event
) {
371 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
378 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
380 if (qemu_balloon_event
) {
381 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
389 /***********************************************************/
390 /* real time host monotonic timer */
392 /* compute with 96 bit intermediate result: (a*b)/c */
393 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
398 #ifdef HOST_WORDS_BIGENDIAN
408 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
409 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
412 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
416 static int64_t get_clock_realtime(void)
420 gettimeofday(&tv
, NULL
);
421 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
426 static int64_t clock_freq
;
428 static void init_get_clock(void)
432 ret
= QueryPerformanceFrequency(&freq
);
434 fprintf(stderr
, "Could not calibrate ticks\n");
437 clock_freq
= freq
.QuadPart
;
440 static int64_t get_clock(void)
443 QueryPerformanceCounter(&ti
);
444 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
449 static int use_rt_clock
;
451 static void init_get_clock(void)
454 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
455 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
458 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
465 static int64_t get_clock(void)
467 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
468 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
471 clock_gettime(CLOCK_MONOTONIC
, &ts
);
472 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
476 /* XXX: using gettimeofday leads to problems if the date
477 changes, so it should be avoided. */
478 return get_clock_realtime();
483 /* Return the virtual CPU time, based on the instruction counter. */
484 static int64_t cpu_get_icount(void)
487 CPUState
*env
= cpu_single_env
;;
488 icount
= qemu_icount
;
491 fprintf(stderr
, "Bad clock read\n");
492 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
494 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
497 /***********************************************************/
498 /* guest cycle counter */
500 typedef struct TimersState
{
501 int64_t cpu_ticks_prev
;
502 int64_t cpu_ticks_offset
;
503 int64_t cpu_clock_offset
;
504 int32_t cpu_ticks_enabled
;
508 TimersState timers_state
;
510 /* return the host CPU cycle counter and handle stop/restart */
511 int64_t cpu_get_ticks(void)
514 return cpu_get_icount();
516 if (!timers_state
.cpu_ticks_enabled
) {
517 return timers_state
.cpu_ticks_offset
;
520 ticks
= cpu_get_real_ticks();
521 if (timers_state
.cpu_ticks_prev
> ticks
) {
522 /* Note: non increasing ticks may happen if the host uses
524 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
526 timers_state
.cpu_ticks_prev
= ticks
;
527 return ticks
+ timers_state
.cpu_ticks_offset
;
531 /* return the host CPU monotonic timer and handle stop/restart */
532 static int64_t cpu_get_clock(void)
535 if (!timers_state
.cpu_ticks_enabled
) {
536 return timers_state
.cpu_clock_offset
;
539 return ti
+ timers_state
.cpu_clock_offset
;
543 /* enable cpu_get_ticks() */
544 void cpu_enable_ticks(void)
546 if (!timers_state
.cpu_ticks_enabled
) {
547 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
548 timers_state
.cpu_clock_offset
-= get_clock();
549 timers_state
.cpu_ticks_enabled
= 1;
553 /* disable cpu_get_ticks() : the clock is stopped. You must not call
554 cpu_get_ticks() after that. */
555 void cpu_disable_ticks(void)
557 if (timers_state
.cpu_ticks_enabled
) {
558 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
559 timers_state
.cpu_clock_offset
= cpu_get_clock();
560 timers_state
.cpu_ticks_enabled
= 0;
564 /***********************************************************/
567 #define QEMU_CLOCK_REALTIME 0
568 #define QEMU_CLOCK_VIRTUAL 1
569 #define QEMU_CLOCK_HOST 2
573 /* XXX: add frequency */
581 struct QEMUTimer
*next
;
584 struct qemu_alarm_timer
{
588 int (*start
)(struct qemu_alarm_timer
*t
);
589 void (*stop
)(struct qemu_alarm_timer
*t
);
590 void (*rearm
)(struct qemu_alarm_timer
*t
);
594 #define ALARM_FLAG_DYNTICKS 0x1
595 #define ALARM_FLAG_EXPIRED 0x2
597 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
599 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
602 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
604 if (!alarm_has_dynticks(t
))
610 /* TODO: MIN_TIMER_REARM_US should be optimized */
611 #define MIN_TIMER_REARM_US 250
613 static struct qemu_alarm_timer
*alarm_timer
;
617 struct qemu_alarm_win32
{
620 } alarm_win32_data
= {0, -1};
622 static int win32_start_timer(struct qemu_alarm_timer
*t
);
623 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
624 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
628 static int unix_start_timer(struct qemu_alarm_timer
*t
);
629 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
633 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
634 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
635 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
637 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
638 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
640 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
641 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
643 #endif /* __linux__ */
647 /* Correlation between real and virtual time is always going to be
648 fairly approximate, so ignore small variation.
649 When the guest is idle real and virtual time will be aligned in
651 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
653 static void icount_adjust(void)
658 static int64_t last_delta
;
659 /* If the VM is not running, then do nothing. */
663 cur_time
= cpu_get_clock();
664 cur_icount
= qemu_get_clock(vm_clock
);
665 delta
= cur_icount
- cur_time
;
666 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
668 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
669 && icount_time_shift
> 0) {
670 /* The guest is getting too far ahead. Slow time down. */
674 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
675 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
676 /* The guest is getting too far behind. Speed time up. */
680 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
683 static void icount_adjust_rt(void * opaque
)
685 qemu_mod_timer(icount_rt_timer
,
686 qemu_get_clock(rt_clock
) + 1000);
690 static void icount_adjust_vm(void * opaque
)
692 qemu_mod_timer(icount_vm_timer
,
693 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
697 static void init_icount_adjust(void)
699 /* Have both realtime and virtual time triggers for speed adjustment.
700 The realtime trigger catches emulated time passing too slowly,
701 the virtual time trigger catches emulated time passing too fast.
702 Realtime triggers occur even when idle, so use them less frequently
704 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
705 qemu_mod_timer(icount_rt_timer
,
706 qemu_get_clock(rt_clock
) + 1000);
707 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
708 qemu_mod_timer(icount_vm_timer
,
709 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
712 static struct qemu_alarm_timer alarm_timers
[] = {
715 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
716 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
717 /* HPET - if available - is preferred */
718 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
719 /* ...otherwise try RTC */
720 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
722 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
724 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
725 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
726 {"win32", 0, win32_start_timer
,
727 win32_stop_timer
, NULL
, &alarm_win32_data
},
732 static void show_available_alarms(void)
736 printf("Available alarm timers, in order of precedence:\n");
737 for (i
= 0; alarm_timers
[i
].name
; i
++)
738 printf("%s\n", alarm_timers
[i
].name
);
741 static void configure_alarms(char const *opt
)
745 int count
= ARRAY_SIZE(alarm_timers
) - 1;
748 struct qemu_alarm_timer tmp
;
750 if (!strcmp(opt
, "?")) {
751 show_available_alarms();
755 arg
= qemu_strdup(opt
);
757 /* Reorder the array */
758 name
= strtok(arg
, ",");
760 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
761 if (!strcmp(alarm_timers
[i
].name
, name
))
766 fprintf(stderr
, "Unknown clock %s\n", name
);
775 tmp
= alarm_timers
[i
];
776 alarm_timers
[i
] = alarm_timers
[cur
];
777 alarm_timers
[cur
] = tmp
;
781 name
= strtok(NULL
, ",");
787 /* Disable remaining timers */
788 for (i
= cur
; i
< count
; i
++)
789 alarm_timers
[i
].name
= NULL
;
791 show_available_alarms();
796 #define QEMU_NUM_CLOCKS 3
800 QEMUClock
*host_clock
;
802 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
804 static QEMUClock
*qemu_new_clock(int type
)
807 clock
= qemu_mallocz(sizeof(QEMUClock
));
812 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
816 ts
= qemu_mallocz(sizeof(QEMUTimer
));
823 void qemu_free_timer(QEMUTimer
*ts
)
828 /* stop a timer, but do not dealloc it */
829 void qemu_del_timer(QEMUTimer
*ts
)
833 /* NOTE: this code must be signal safe because
834 qemu_timer_expired() can be called from a signal. */
835 pt
= &active_timers
[ts
->clock
->type
];
848 /* modify the current timer so that it will be fired when current_time
849 >= expire_time. The corresponding callback will be called. */
850 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
856 /* add the timer in the sorted list */
857 /* NOTE: this code must be signal safe because
858 qemu_timer_expired() can be called from a signal. */
859 pt
= &active_timers
[ts
->clock
->type
];
864 if (t
->expire_time
> expire_time
)
868 ts
->expire_time
= expire_time
;
872 /* Rearm if necessary */
873 if (pt
== &active_timers
[ts
->clock
->type
]) {
874 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
875 qemu_rearm_alarm_timer(alarm_timer
);
877 /* Interrupt execution to force deadline recalculation. */
883 int qemu_timer_pending(QEMUTimer
*ts
)
886 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
893 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
897 return (timer_head
->expire_time
<= current_time
);
900 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
906 if (!ts
|| ts
->expire_time
> current_time
)
908 /* remove timer from the list before calling the callback */
909 *ptimer_head
= ts
->next
;
912 /* run the callback (the timer list can be modified) */
917 int64_t qemu_get_clock(QEMUClock
*clock
)
919 switch(clock
->type
) {
920 case QEMU_CLOCK_REALTIME
:
921 return get_clock() / 1000000;
923 case QEMU_CLOCK_VIRTUAL
:
925 return cpu_get_icount();
927 return cpu_get_clock();
929 case QEMU_CLOCK_HOST
:
930 return get_clock_realtime();
934 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
936 switch(clock
->type
) {
937 case QEMU_CLOCK_REALTIME
:
940 case QEMU_CLOCK_VIRTUAL
:
942 return cpu_get_icount();
944 return cpu_get_clock();
946 case QEMU_CLOCK_HOST
:
947 return get_clock_realtime();
951 static void init_clocks(void)
954 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
955 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
956 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
958 rtc_clock
= host_clock
;
962 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
964 uint64_t expire_time
;
966 if (qemu_timer_pending(ts
)) {
967 expire_time
= ts
->expire_time
;
971 qemu_put_be64(f
, expire_time
);
974 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
976 uint64_t expire_time
;
978 expire_time
= qemu_get_be64(f
);
979 if (expire_time
!= -1) {
980 qemu_mod_timer(ts
, expire_time
);
986 static const VMStateDescription vmstate_timers
= {
989 .minimum_version_id
= 1,
990 .minimum_version_id_old
= 1,
991 .fields
= (VMStateField
[]) {
992 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
993 VMSTATE_INT64(dummy
, TimersState
),
994 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
995 VMSTATE_END_OF_LIST()
999 static void qemu_event_increment(void);
1002 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1003 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1006 static void host_alarm_handler(int host_signum
)
1010 #define DISP_FREQ 1000
1012 static int64_t delta_min
= INT64_MAX
;
1013 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1015 ti
= qemu_get_clock(vm_clock
);
1016 if (last_clock
!= 0) {
1017 delta
= ti
- last_clock
;
1018 if (delta
< delta_min
)
1020 if (delta
> delta_max
)
1023 if (++count
== DISP_FREQ
) {
1024 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1025 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1026 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1027 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1028 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1030 delta_min
= INT64_MAX
;
1038 if (alarm_has_dynticks(alarm_timer
) ||
1040 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1041 qemu_get_clock(vm_clock
))) ||
1042 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1043 qemu_get_clock(rt_clock
)) ||
1044 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1045 qemu_get_clock(host_clock
))) {
1046 qemu_event_increment();
1047 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1049 #ifndef CONFIG_IOTHREAD
1051 /* stop the currently executing cpu because a timer occured */
1055 timer_alarm_pending
= 1;
1056 qemu_notify_event();
1060 static int64_t qemu_next_deadline(void)
1062 /* To avoid problems with overflow limit this to 2^32. */
1063 int64_t delta
= INT32_MAX
;
1065 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1066 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1067 qemu_get_clock(vm_clock
);
1069 if (active_timers
[QEMU_CLOCK_HOST
]) {
1070 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1071 qemu_get_clock(host_clock
);
1082 #if defined(__linux__)
1083 static uint64_t qemu_next_deadline_dyntick(void)
1091 delta
= (qemu_next_deadline() + 999) / 1000;
1093 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1094 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1095 qemu_get_clock(rt_clock
))*1000;
1096 if (rtdelta
< delta
)
1100 if (delta
< MIN_TIMER_REARM_US
)
1101 delta
= MIN_TIMER_REARM_US
;
1109 /* Sets a specific flag */
1110 static int fcntl_setfl(int fd
, int flag
)
1114 flags
= fcntl(fd
, F_GETFL
);
1118 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1124 #if defined(__linux__)
1126 #define RTC_FREQ 1024
1128 static void enable_sigio_timer(int fd
)
1130 struct sigaction act
;
1133 sigfillset(&act
.sa_mask
);
1135 act
.sa_handler
= host_alarm_handler
;
1137 sigaction(SIGIO
, &act
, NULL
);
1138 fcntl_setfl(fd
, O_ASYNC
);
1139 fcntl(fd
, F_SETOWN
, getpid());
1142 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1144 struct hpet_info info
;
1147 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1152 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1154 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1155 "error, but for better emulation accuracy type:\n"
1156 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1160 /* Check capabilities */
1161 r
= ioctl(fd
, HPET_INFO
, &info
);
1165 /* Enable periodic mode */
1166 r
= ioctl(fd
, HPET_EPI
, 0);
1167 if (info
.hi_flags
&& (r
< 0))
1170 /* Enable interrupt */
1171 r
= ioctl(fd
, HPET_IE_ON
, 0);
1175 enable_sigio_timer(fd
);
1176 t
->priv
= (void *)(long)fd
;
1184 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1186 int fd
= (long)t
->priv
;
1191 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1194 unsigned long current_rtc_freq
= 0;
1196 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1199 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1200 if (current_rtc_freq
!= RTC_FREQ
&&
1201 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1202 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1203 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1204 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1207 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1213 enable_sigio_timer(rtc_fd
);
1215 t
->priv
= (void *)(long)rtc_fd
;
1220 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1222 int rtc_fd
= (long)t
->priv
;
1227 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1231 struct sigaction act
;
1233 sigfillset(&act
.sa_mask
);
1235 act
.sa_handler
= host_alarm_handler
;
1237 sigaction(SIGALRM
, &act
, NULL
);
1240 * Initialize ev struct to 0 to avoid valgrind complaining
1241 * about uninitialized data in timer_create call
1243 memset(&ev
, 0, sizeof(ev
));
1244 ev
.sigev_value
.sival_int
= 0;
1245 ev
.sigev_notify
= SIGEV_SIGNAL
;
1246 ev
.sigev_signo
= SIGALRM
;
1248 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1249 perror("timer_create");
1251 /* disable dynticks */
1252 fprintf(stderr
, "Dynamic Ticks disabled\n");
1257 t
->priv
= (void *)(long)host_timer
;
1262 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1264 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1266 timer_delete(host_timer
);
1269 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1271 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1272 struct itimerspec timeout
;
1273 int64_t nearest_delta_us
= INT64_MAX
;
1276 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1277 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1278 !active_timers
[QEMU_CLOCK_HOST
])
1281 nearest_delta_us
= qemu_next_deadline_dyntick();
1283 /* check whether a timer is already running */
1284 if (timer_gettime(host_timer
, &timeout
)) {
1286 fprintf(stderr
, "Internal timer error: aborting\n");
1289 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1290 if (current_us
&& current_us
<= nearest_delta_us
)
1293 timeout
.it_interval
.tv_sec
= 0;
1294 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1295 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1296 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1297 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1299 fprintf(stderr
, "Internal timer error: aborting\n");
1304 #endif /* defined(__linux__) */
1306 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1308 struct sigaction act
;
1309 struct itimerval itv
;
1313 sigfillset(&act
.sa_mask
);
1315 act
.sa_handler
= host_alarm_handler
;
1317 sigaction(SIGALRM
, &act
, NULL
);
1319 itv
.it_interval
.tv_sec
= 0;
1320 /* for i386 kernel 2.6 to get 1 ms */
1321 itv
.it_interval
.tv_usec
= 999;
1322 itv
.it_value
.tv_sec
= 0;
1323 itv
.it_value
.tv_usec
= 10 * 1000;
1325 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1332 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1334 struct itimerval itv
;
1336 memset(&itv
, 0, sizeof(itv
));
1337 setitimer(ITIMER_REAL
, &itv
, NULL
);
1340 #endif /* !defined(_WIN32) */
1345 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1348 struct qemu_alarm_win32
*data
= t
->priv
;
1351 memset(&tc
, 0, sizeof(tc
));
1352 timeGetDevCaps(&tc
, sizeof(tc
));
1354 if (data
->period
< tc
.wPeriodMin
)
1355 data
->period
= tc
.wPeriodMin
;
1357 timeBeginPeriod(data
->period
);
1359 flags
= TIME_CALLBACK_FUNCTION
;
1360 if (alarm_has_dynticks(t
))
1361 flags
|= TIME_ONESHOT
;
1363 flags
|= TIME_PERIODIC
;
1365 data
->timerId
= timeSetEvent(1, // interval (ms)
1366 data
->period
, // resolution
1367 host_alarm_handler
, // function
1368 (DWORD
)t
, // parameter
1371 if (!data
->timerId
) {
1372 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1374 timeEndPeriod(data
->period
);
1381 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1383 struct qemu_alarm_win32
*data
= t
->priv
;
1385 timeKillEvent(data
->timerId
);
1386 timeEndPeriod(data
->period
);
1389 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1391 struct qemu_alarm_win32
*data
= t
->priv
;
1393 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1394 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1395 !active_timers
[QEMU_CLOCK_HOST
])
1398 timeKillEvent(data
->timerId
);
1400 data
->timerId
= timeSetEvent(1,
1404 TIME_ONESHOT
| TIME_PERIODIC
);
1406 if (!data
->timerId
) {
1407 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1410 timeEndPeriod(data
->period
);
1417 static int init_timer_alarm(void)
1419 struct qemu_alarm_timer
*t
= NULL
;
1422 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1423 t
= &alarm_timers
[i
];
1443 static void quit_timers(void)
1445 alarm_timer
->stop(alarm_timer
);
1449 /***********************************************************/
1450 /* host time/date access */
1451 void qemu_get_timedate(struct tm
*tm
, int offset
)
1458 if (rtc_date_offset
== -1) {
1462 ret
= localtime(&ti
);
1464 ti
-= rtc_date_offset
;
1468 memcpy(tm
, ret
, sizeof(struct tm
));
1471 int qemu_timedate_diff(struct tm
*tm
)
1475 if (rtc_date_offset
== -1)
1477 seconds
= mktimegm(tm
);
1479 seconds
= mktime(tm
);
1481 seconds
= mktimegm(tm
) + rtc_date_offset
;
1483 return seconds
- time(NULL
);
1486 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1488 time_t rtc_start_date
;
1491 if (!strcmp(startdate
, "now") && legacy
) {
1492 rtc_date_offset
= -1;
1494 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1502 } else if (sscanf(startdate
, "%d-%d-%d",
1505 &tm
.tm_mday
) == 3) {
1514 rtc_start_date
= mktimegm(&tm
);
1515 if (rtc_start_date
== -1) {
1517 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1518 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1521 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1525 static void configure_rtc(QemuOpts
*opts
)
1529 value
= qemu_opt_get(opts
, "base");
1531 if (!strcmp(value
, "utc")) {
1533 } else if (!strcmp(value
, "localtime")) {
1536 configure_rtc_date_offset(value
, 0);
1539 value
= qemu_opt_get(opts
, "clock");
1541 if (!strcmp(value
, "host")) {
1542 rtc_clock
= host_clock
;
1543 } else if (!strcmp(value
, "vm")) {
1544 rtc_clock
= vm_clock
;
1546 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1550 #ifdef CONFIG_TARGET_I386
1551 value
= qemu_opt_get(opts
, "driftfix");
1553 if (!strcmp(buf
, "slew")) {
1555 } else if (!strcmp(buf
, "none")) {
1558 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1566 static void socket_cleanup(void)
1571 static int socket_init(void)
1576 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1578 err
= WSAGetLastError();
1579 fprintf(stderr
, "WSAStartup: %d\n", err
);
1582 atexit(socket_cleanup
);
1587 /***********************************************************/
1588 /* Bluetooth support */
1591 static struct HCIInfo
*hci_table
[MAX_NICS
];
1593 static struct bt_vlan_s
{
1594 struct bt_scatternet_s net
;
1596 struct bt_vlan_s
*next
;
1599 /* find or alloc a new bluetooth "VLAN" */
1600 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1602 struct bt_vlan_s
**pvlan
, *vlan
;
1603 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1607 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1609 pvlan
= &first_bt_vlan
;
1610 while (*pvlan
!= NULL
)
1611 pvlan
= &(*pvlan
)->next
;
1616 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1620 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1625 static struct HCIInfo null_hci
= {
1626 .cmd_send
= null_hci_send
,
1627 .sco_send
= null_hci_send
,
1628 .acl_send
= null_hci_send
,
1629 .bdaddr_set
= null_hci_addr_set
,
1632 struct HCIInfo
*qemu_next_hci(void)
1634 if (cur_hci
== nb_hcis
)
1637 return hci_table
[cur_hci
++];
1640 static struct HCIInfo
*hci_init(const char *str
)
1643 struct bt_scatternet_s
*vlan
= 0;
1645 if (!strcmp(str
, "null"))
1648 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1650 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1651 else if (!strncmp(str
, "hci", 3)) {
1654 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1655 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1660 vlan
= qemu_find_bt_vlan(0);
1662 return bt_new_hci(vlan
);
1665 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1670 static int bt_hci_parse(const char *str
)
1672 struct HCIInfo
*hci
;
1675 if (nb_hcis
>= MAX_NICS
) {
1676 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1680 hci
= hci_init(str
);
1689 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1690 hci
->bdaddr_set(hci
, bdaddr
.b
);
1692 hci_table
[nb_hcis
++] = hci
;
1697 static void bt_vhci_add(int vlan_id
)
1699 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1702 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1703 "an empty scatternet %i\n", vlan_id
);
1705 bt_vhci_init(bt_new_hci(vlan
));
1708 static struct bt_device_s
*bt_device_add(const char *opt
)
1710 struct bt_scatternet_s
*vlan
;
1712 char *endp
= strstr(opt
, ",vlan=");
1713 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1716 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1719 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1721 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1726 vlan
= qemu_find_bt_vlan(vlan_id
);
1729 fprintf(stderr
, "qemu: warning: adding a slave device to "
1730 "an empty scatternet %i\n", vlan_id
);
1732 if (!strcmp(devname
, "keyboard"))
1733 return bt_keyboard_init(vlan
);
1735 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1739 static int bt_parse(const char *opt
)
1741 const char *endp
, *p
;
1744 if (strstart(opt
, "hci", &endp
)) {
1745 if (!*endp
|| *endp
== ',') {
1747 if (!strstart(endp
, ",vlan=", 0))
1750 return bt_hci_parse(opt
);
1752 } else if (strstart(opt
, "vhci", &endp
)) {
1753 if (!*endp
|| *endp
== ',') {
1755 if (strstart(endp
, ",vlan=", &p
)) {
1756 vlan
= strtol(p
, (char **) &endp
, 0);
1758 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1762 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1771 } else if (strstart(opt
, "device:", &endp
))
1772 return !bt_device_add(endp
);
1774 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1778 /***********************************************************/
1779 /* QEMU Block devices */
1781 #define HD_ALIAS "index=%d,media=disk"
1782 #define CDROM_ALIAS "index=2,media=cdrom"
1783 #define FD_ALIAS "index=%d,if=floppy"
1784 #define PFLASH_ALIAS "if=pflash"
1785 #define MTD_ALIAS "if=mtd"
1786 #define SD_ALIAS "index=0,if=sd"
1788 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1795 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1798 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1800 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1801 __FUNCTION__
, optstr
);
1805 qemu_opt_set(opts
, "file", file
);
1809 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1813 /* seek interface, bus and unit */
1815 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1816 if (dinfo
->type
== type
&&
1817 dinfo
->bus
== bus
&&
1818 dinfo
->unit
== unit
)
1825 DriveInfo
*drive_get_by_id(const char *id
)
1829 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1830 if (strcmp(id
, dinfo
->id
))
1837 int drive_get_max_bus(BlockInterfaceType type
)
1843 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1844 if(dinfo
->type
== type
&&
1845 dinfo
->bus
> max_bus
)
1846 max_bus
= dinfo
->bus
;
1851 const char *drive_get_serial(BlockDriverState
*bdrv
)
1855 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1856 if (dinfo
->bdrv
== bdrv
)
1857 return dinfo
->serial
;
1863 BlockInterfaceErrorAction
drive_get_on_error(
1864 BlockDriverState
*bdrv
, int is_read
)
1868 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1869 if (dinfo
->bdrv
== bdrv
)
1870 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1873 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1876 static void bdrv_format_print(void *opaque
, const char *name
)
1878 fprintf(stderr
, " %s", name
);
1881 void drive_uninit(DriveInfo
*dinfo
)
1883 qemu_opts_del(dinfo
->opts
);
1884 bdrv_delete(dinfo
->bdrv
);
1885 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1889 static int parse_block_error_action(const char *buf
, int is_read
)
1891 if (!strcmp(buf
, "ignore")) {
1892 return BLOCK_ERR_IGNORE
;
1893 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1894 return BLOCK_ERR_STOP_ENOSPC
;
1895 } else if (!strcmp(buf
, "stop")) {
1896 return BLOCK_ERR_STOP_ANY
;
1897 } else if (!strcmp(buf
, "report")) {
1898 return BLOCK_ERR_REPORT
;
1900 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1901 buf
, is_read
? "read" : "write");
1906 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1910 const char *file
= NULL
;
1913 const char *mediastr
= "";
1914 BlockInterfaceType type
;
1915 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1916 int bus_id
, unit_id
;
1917 int cyls
, heads
, secs
, translation
;
1918 BlockDriver
*drv
= NULL
;
1919 QEMUMachine
*machine
= opaque
;
1926 int on_read_error
, on_write_error
;
1927 const char *devaddr
;
1933 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1936 if (machine
&& machine
->use_scsi
) {
1938 max_devs
= MAX_SCSI_DEVS
;
1939 pstrcpy(devname
, sizeof(devname
), "scsi");
1942 max_devs
= MAX_IDE_DEVS
;
1943 pstrcpy(devname
, sizeof(devname
), "ide");
1947 /* extract parameters */
1948 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1949 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1950 index
= qemu_opt_get_number(opts
, "index", -1);
1952 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1953 heads
= qemu_opt_get_number(opts
, "heads", 0);
1954 secs
= qemu_opt_get_number(opts
, "secs", 0);
1956 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1957 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1959 file
= qemu_opt_get(opts
, "file");
1960 serial
= qemu_opt_get(opts
, "serial");
1962 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1963 pstrcpy(devname
, sizeof(devname
), buf
);
1964 if (!strcmp(buf
, "ide")) {
1966 max_devs
= MAX_IDE_DEVS
;
1967 } else if (!strcmp(buf
, "scsi")) {
1969 max_devs
= MAX_SCSI_DEVS
;
1970 } else if (!strcmp(buf
, "floppy")) {
1973 } else if (!strcmp(buf
, "pflash")) {
1976 } else if (!strcmp(buf
, "mtd")) {
1979 } else if (!strcmp(buf
, "sd")) {
1982 } else if (!strcmp(buf
, "virtio")) {
1985 } else if (!strcmp(buf
, "xen")) {
1988 } else if (!strcmp(buf
, "none")) {
1992 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1997 if (cyls
|| heads
|| secs
) {
1998 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
1999 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2002 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2003 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2006 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2007 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2012 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2015 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2019 if (!strcmp(buf
, "none"))
2020 translation
= BIOS_ATA_TRANSLATION_NONE
;
2021 else if (!strcmp(buf
, "lba"))
2022 translation
= BIOS_ATA_TRANSLATION_LBA
;
2023 else if (!strcmp(buf
, "auto"))
2024 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2026 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2031 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2032 if (!strcmp(buf
, "disk")) {
2034 } else if (!strcmp(buf
, "cdrom")) {
2035 if (cyls
|| secs
|| heads
) {
2037 "qemu: '%s' invalid physical CHS format\n", buf
);
2040 media
= MEDIA_CDROM
;
2042 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2047 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2048 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2050 else if (!strcmp(buf
, "writethrough"))
2052 else if (!strcmp(buf
, "writeback"))
2055 fprintf(stderr
, "qemu: invalid cache option\n");
2060 #ifdef CONFIG_LINUX_AIO
2061 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2062 if (!strcmp(buf
, "threads"))
2064 else if (!strcmp(buf
, "native"))
2067 fprintf(stderr
, "qemu: invalid aio option\n");
2073 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2074 if (strcmp(buf
, "?") == 0) {
2075 fprintf(stderr
, "qemu: Supported formats:");
2076 bdrv_iterate_format(bdrv_format_print
, NULL
);
2077 fprintf(stderr
, "\n");
2080 drv
= bdrv_find_whitelisted_format(buf
);
2082 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2087 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2088 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2089 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2090 fprintf(stderr
, "werror is no supported by this format\n");
2094 on_write_error
= parse_block_error_action(buf
, 0);
2095 if (on_write_error
< 0) {
2100 on_read_error
= BLOCK_ERR_REPORT
;
2101 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2102 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2103 fprintf(stderr
, "rerror is no supported by this format\n");
2107 on_read_error
= parse_block_error_action(buf
, 1);
2108 if (on_read_error
< 0) {
2113 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2114 if (type
!= IF_VIRTIO
) {
2115 fprintf(stderr
, "addr is not supported\n");
2120 /* compute bus and unit according index */
2123 if (bus_id
!= 0 || unit_id
!= -1) {
2125 "qemu: index cannot be used with bus and unit\n");
2133 unit_id
= index
% max_devs
;
2134 bus_id
= index
/ max_devs
;
2138 /* if user doesn't specify a unit_id,
2139 * try to find the first free
2142 if (unit_id
== -1) {
2144 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2146 if (max_devs
&& unit_id
>= max_devs
) {
2147 unit_id
-= max_devs
;
2155 if (max_devs
&& unit_id
>= max_devs
) {
2156 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2157 unit_id
, max_devs
- 1);
2162 * ignore multiple definitions
2165 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2172 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2173 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2174 dinfo
->id
= qemu_strdup(buf
);
2176 /* no id supplied -> create one */
2177 dinfo
->id
= qemu_mallocz(32);
2178 if (type
== IF_IDE
|| type
== IF_SCSI
)
2179 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2181 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2182 devname
, bus_id
, mediastr
, unit_id
);
2184 snprintf(dinfo
->id
, 32, "%s%s%i",
2185 devname
, mediastr
, unit_id
);
2187 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2188 dinfo
->devaddr
= devaddr
;
2190 dinfo
->bus
= bus_id
;
2191 dinfo
->unit
= unit_id
;
2192 dinfo
->on_read_error
= on_read_error
;
2193 dinfo
->on_write_error
= on_write_error
;
2196 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2197 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2207 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2208 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2212 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2217 /* FIXME: This isn't really a floppy, but it's a reasonable
2220 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2226 /* add virtio block device */
2227 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2228 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2229 qemu_opt_set(opts
, "drive", dinfo
->id
);
2231 qemu_opt_set(opts
, "addr", devaddr
);
2242 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2243 cache
= 2; /* always use write-back with snapshot */
2245 if (cache
== 0) /* no caching */
2246 bdrv_flags
|= BDRV_O_NOCACHE
;
2247 else if (cache
== 2) /* write-back */
2248 bdrv_flags
|= BDRV_O_CACHE_WB
;
2251 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2253 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2257 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2258 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2263 * cdrom is read-only. Set it now, after above interface checking
2264 * since readonly attribute not explicitly required, so no error.
2266 if (media
== MEDIA_CDROM
) {
2269 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2271 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2272 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2273 file
, strerror(errno
));
2277 if (bdrv_key_required(dinfo
->bdrv
))
2283 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2285 QEMUMachine
*machine
= opaque
;
2286 int fatal_error
= 0;
2288 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2295 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2297 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2298 qemu_opt_set(opts
, "snapshot", "on");
2303 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2305 boot_set_handler
= func
;
2306 boot_set_opaque
= opaque
;
2309 int qemu_boot_set(const char *boot_devices
)
2311 if (!boot_set_handler
) {
2314 return boot_set_handler(boot_set_opaque
, boot_devices
);
2317 static int parse_bootdevices(char *devices
)
2319 /* We just do some generic consistency checks */
2323 for (p
= devices
; *p
!= '\0'; p
++) {
2324 /* Allowed boot devices are:
2325 * a-b: floppy disk drives
2326 * c-f: IDE disk drives
2327 * g-m: machine implementation dependant drives
2328 * n-p: network devices
2329 * It's up to each machine implementation to check if the given boot
2330 * devices match the actual hardware implementation and firmware
2333 if (*p
< 'a' || *p
> 'p') {
2334 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2337 if (bitmap
& (1 << (*p
- 'a'))) {
2338 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2341 bitmap
|= 1 << (*p
- 'a');
2346 static void restore_boot_devices(void *opaque
)
2348 char *standard_boot_devices
= opaque
;
2350 qemu_boot_set(standard_boot_devices
);
2352 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2353 qemu_free(standard_boot_devices
);
2356 static void numa_add(const char *optarg
)
2360 unsigned long long value
, endvalue
;
2363 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2364 if (!strcmp(option
, "node")) {
2365 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2366 nodenr
= nb_numa_nodes
;
2368 nodenr
= strtoull(option
, NULL
, 10);
2371 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2372 node_mem
[nodenr
] = 0;
2374 value
= strtoull(option
, &endptr
, 0);
2376 case 0: case 'M': case 'm':
2383 node_mem
[nodenr
] = value
;
2385 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2386 node_cpumask
[nodenr
] = 0;
2388 value
= strtoull(option
, &endptr
, 10);
2391 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2393 if (*endptr
== '-') {
2394 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2395 if (endvalue
>= 63) {
2398 "only 63 CPUs in NUMA mode supported.\n");
2400 value
= (2ULL << endvalue
) - (1ULL << value
);
2402 value
= 1ULL << value
;
2405 node_cpumask
[nodenr
] = value
;
2412 static void smp_parse(const char *optarg
)
2414 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2418 smp
= strtoul(optarg
, &endptr
, 10);
2419 if (endptr
!= optarg
) {
2420 if (*endptr
== ',') {
2424 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2425 sockets
= strtoull(option
, NULL
, 10);
2426 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2427 cores
= strtoull(option
, NULL
, 10);
2428 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2429 threads
= strtoull(option
, NULL
, 10);
2430 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2431 max_cpus
= strtoull(option
, NULL
, 10);
2433 /* compute missing values, prefer sockets over cores over threads */
2434 if (smp
== 0 || sockets
== 0) {
2435 sockets
= sockets
> 0 ? sockets
: 1;
2436 cores
= cores
> 0 ? cores
: 1;
2437 threads
= threads
> 0 ? threads
: 1;
2439 smp
= cores
* threads
* sockets
;
2443 threads
= threads
> 0 ? threads
: 1;
2444 cores
= smp
/ (sockets
* threads
);
2447 threads
= smp
/ (cores
* sockets
);
2452 smp_cores
= cores
> 0 ? cores
: 1;
2453 smp_threads
= threads
> 0 ? threads
: 1;
2455 max_cpus
= smp_cpus
;
2458 /***********************************************************/
2461 static int usb_device_add(const char *devname
, int is_hotplug
)
2464 USBDevice
*dev
= NULL
;
2469 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2470 dev
= usbdevice_create(devname
);
2474 /* the other ones */
2475 if (strstart(devname
, "host:", &p
)) {
2476 dev
= usb_host_device_open(p
);
2477 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2478 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2479 bt_new_hci(qemu_find_bt_vlan(0)));
2490 static int usb_device_del(const char *devname
)
2495 if (strstart(devname
, "host:", &p
))
2496 return usb_host_device_close(p
);
2501 p
= strchr(devname
, '.');
2504 bus_num
= strtoul(devname
, NULL
, 0);
2505 addr
= strtoul(p
+ 1, NULL
, 0);
2507 return usb_device_delete_addr(bus_num
, addr
);
2510 static int usb_parse(const char *cmdline
)
2513 r
= usb_device_add(cmdline
, 0);
2515 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2520 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2522 const char *devname
= qdict_get_str(qdict
, "devname");
2523 if (usb_device_add(devname
, 1) < 0) {
2524 qemu_error("could not add USB device '%s'\n", devname
);
2528 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2530 const char *devname
= qdict_get_str(qdict
, "devname");
2531 if (usb_device_del(devname
) < 0) {
2532 qemu_error("could not delete USB device '%s'\n", devname
);
2536 /***********************************************************/
2537 /* PCMCIA/Cardbus */
2539 static struct pcmcia_socket_entry_s
{
2540 PCMCIASocket
*socket
;
2541 struct pcmcia_socket_entry_s
*next
;
2542 } *pcmcia_sockets
= 0;
2544 void pcmcia_socket_register(PCMCIASocket
*socket
)
2546 struct pcmcia_socket_entry_s
*entry
;
2548 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2549 entry
->socket
= socket
;
2550 entry
->next
= pcmcia_sockets
;
2551 pcmcia_sockets
= entry
;
2554 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2556 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2558 ptr
= &pcmcia_sockets
;
2559 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2560 if (entry
->socket
== socket
) {
2566 void pcmcia_info(Monitor
*mon
)
2568 struct pcmcia_socket_entry_s
*iter
;
2570 if (!pcmcia_sockets
)
2571 monitor_printf(mon
, "No PCMCIA sockets\n");
2573 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2574 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2575 iter
->socket
->attached
? iter
->socket
->card_string
:
2579 /***********************************************************/
2580 /* register display */
2582 struct DisplayAllocator default_allocator
= {
2583 defaultallocator_create_displaysurface
,
2584 defaultallocator_resize_displaysurface
,
2585 defaultallocator_free_displaysurface
2590 static void dumb_display_init(void)
2592 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2593 ds
->allocator
= &default_allocator
;
2594 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2595 register_displaystate(ds
);
2598 void register_displaystate(DisplayState
*ds
)
2608 DisplayState
*get_displaystate(void)
2610 if (!display_state
) {
2611 dumb_display_init();
2613 return display_state
;
2616 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2618 if(ds
->allocator
== &default_allocator
) {
2619 DisplaySurface
*surf
;
2620 surf
= da
->create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
2621 defaultallocator_free_displaysurface(ds
->surface
);
2625 return ds
->allocator
;
2628 /***********************************************************/
2631 typedef struct IOHandlerRecord
{
2633 IOCanRWHandler
*fd_read_poll
;
2635 IOHandler
*fd_write
;
2638 /* temporary data */
2640 struct IOHandlerRecord
*next
;
2643 static IOHandlerRecord
*first_io_handler
;
2645 /* XXX: fd_read_poll should be suppressed, but an API change is
2646 necessary in the character devices to suppress fd_can_read(). */
2647 int qemu_set_fd_handler2(int fd
,
2648 IOCanRWHandler
*fd_read_poll
,
2650 IOHandler
*fd_write
,
2653 IOHandlerRecord
**pioh
, *ioh
;
2655 if (!fd_read
&& !fd_write
) {
2656 pioh
= &first_io_handler
;
2661 if (ioh
->fd
== fd
) {
2668 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2672 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2673 ioh
->next
= first_io_handler
;
2674 first_io_handler
= ioh
;
2677 ioh
->fd_read_poll
= fd_read_poll
;
2678 ioh
->fd_read
= fd_read
;
2679 ioh
->fd_write
= fd_write
;
2680 ioh
->opaque
= opaque
;
2686 int qemu_set_fd_handler(int fd
,
2688 IOHandler
*fd_write
,
2691 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2695 /***********************************************************/
2696 /* Polling handling */
2698 typedef struct PollingEntry
{
2701 struct PollingEntry
*next
;
2704 static PollingEntry
*first_polling_entry
;
2706 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2708 PollingEntry
**ppe
, *pe
;
2709 pe
= qemu_mallocz(sizeof(PollingEntry
));
2711 pe
->opaque
= opaque
;
2712 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2717 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2719 PollingEntry
**ppe
, *pe
;
2720 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2722 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2730 /***********************************************************/
2731 /* Wait objects support */
2732 typedef struct WaitObjects
{
2734 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2735 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2736 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2739 static WaitObjects wait_objects
= {0};
2741 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2743 WaitObjects
*w
= &wait_objects
;
2745 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2747 w
->events
[w
->num
] = handle
;
2748 w
->func
[w
->num
] = func
;
2749 w
->opaque
[w
->num
] = opaque
;
2754 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2757 WaitObjects
*w
= &wait_objects
;
2760 for (i
= 0; i
< w
->num
; i
++) {
2761 if (w
->events
[i
] == handle
)
2764 w
->events
[i
] = w
->events
[i
+ 1];
2765 w
->func
[i
] = w
->func
[i
+ 1];
2766 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2774 /***********************************************************/
2775 /* ram save/restore */
2777 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2778 #define RAM_SAVE_FLAG_COMPRESS 0x02
2779 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2780 #define RAM_SAVE_FLAG_PAGE 0x08
2781 #define RAM_SAVE_FLAG_EOS 0x10
2783 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2785 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2786 uint32_t *array
= (uint32_t *)page
;
2789 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2790 if (array
[i
] != val
)
2797 static int ram_save_block(QEMUFile
*f
)
2799 static ram_addr_t current_addr
= 0;
2800 ram_addr_t saved_addr
= current_addr
;
2801 ram_addr_t addr
= 0;
2804 while (addr
< last_ram_offset
) {
2805 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2808 cpu_physical_memory_reset_dirty(current_addr
,
2809 current_addr
+ TARGET_PAGE_SIZE
,
2810 MIGRATION_DIRTY_FLAG
);
2812 p
= qemu_get_ram_ptr(current_addr
);
2814 if (is_dup_page(p
, *p
)) {
2815 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2816 qemu_put_byte(f
, *p
);
2818 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2819 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2825 addr
+= TARGET_PAGE_SIZE
;
2826 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2832 static uint64_t bytes_transferred
;
2834 static ram_addr_t
ram_save_remaining(void)
2837 ram_addr_t count
= 0;
2839 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2840 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2847 uint64_t ram_bytes_remaining(void)
2849 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2852 uint64_t ram_bytes_transferred(void)
2854 return bytes_transferred
;
2857 uint64_t ram_bytes_total(void)
2859 return last_ram_offset
;
2862 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2865 uint64_t bytes_transferred_last
;
2867 uint64_t expected_time
= 0;
2870 cpu_physical_memory_set_dirty_tracking(0);
2874 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2875 qemu_file_set_error(f
);
2880 bytes_transferred
= 0;
2882 /* Make sure all dirty bits are set */
2883 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2884 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2885 cpu_physical_memory_set_dirty(addr
);
2888 /* Enable dirty memory tracking */
2889 cpu_physical_memory_set_dirty_tracking(1);
2891 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2894 bytes_transferred_last
= bytes_transferred
;
2895 bwidth
= qemu_get_clock_ns(rt_clock
);
2897 while (!qemu_file_rate_limit(f
)) {
2900 ret
= ram_save_block(f
);
2901 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2902 if (ret
== 0) /* no more blocks */
2906 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
2907 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2909 /* if we haven't transferred anything this round, force expected_time to a
2910 * a very high value, but without crashing */
2914 /* try transferring iterative blocks of memory */
2916 /* flush all remaining blocks regardless of rate limiting */
2917 while (ram_save_block(f
) != 0) {
2918 bytes_transferred
+= TARGET_PAGE_SIZE
;
2920 cpu_physical_memory_set_dirty_tracking(0);
2923 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2925 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2927 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2930 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2935 if (version_id
!= 3)
2939 addr
= qemu_get_be64(f
);
2941 flags
= addr
& ~TARGET_PAGE_MASK
;
2942 addr
&= TARGET_PAGE_MASK
;
2944 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2945 if (addr
!= last_ram_offset
)
2949 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2950 uint8_t ch
= qemu_get_byte(f
);
2951 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2954 (!kvm_enabled() || kvm_has_sync_mmu())) {
2955 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2958 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2959 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2961 if (qemu_file_has_error(f
)) {
2964 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2969 void qemu_service_io(void)
2971 qemu_notify_event();
2974 /***********************************************************/
2975 /* machine registration */
2977 static QEMUMachine
*first_machine
= NULL
;
2978 QEMUMachine
*current_machine
= NULL
;
2980 int qemu_register_machine(QEMUMachine
*m
)
2983 pm
= &first_machine
;
2991 static QEMUMachine
*find_machine(const char *name
)
2995 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2996 if (!strcmp(m
->name
, name
))
2998 if (m
->alias
&& !strcmp(m
->alias
, name
))
3004 static QEMUMachine
*find_default_machine(void)
3008 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3009 if (m
->is_default
) {
3016 /***********************************************************/
3017 /* main execution loop */
3019 static void gui_update(void *opaque
)
3021 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3022 DisplayState
*ds
= opaque
;
3023 DisplayChangeListener
*dcl
= ds
->listeners
;
3025 qemu_flush_coalesced_mmio_buffer();
3028 while (dcl
!= NULL
) {
3029 if (dcl
->gui_timer_interval
&&
3030 dcl
->gui_timer_interval
< interval
)
3031 interval
= dcl
->gui_timer_interval
;
3034 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3037 static void nographic_update(void *opaque
)
3039 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3041 qemu_flush_coalesced_mmio_buffer();
3042 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3045 struct vm_change_state_entry
{
3046 VMChangeStateHandler
*cb
;
3048 QLIST_ENTRY (vm_change_state_entry
) entries
;
3051 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3053 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3056 VMChangeStateEntry
*e
;
3058 e
= qemu_mallocz(sizeof (*e
));
3062 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3066 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3068 QLIST_REMOVE (e
, entries
);
3072 static void vm_state_notify(int running
, int reason
)
3074 VMChangeStateEntry
*e
;
3076 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3077 e
->cb(e
->opaque
, running
, reason
);
3081 static void resume_all_vcpus(void);
3082 static void pause_all_vcpus(void);
3089 vm_state_notify(1, 0);
3090 qemu_rearm_alarm_timer(alarm_timer
);
3095 /* reset/shutdown handler */
3097 typedef struct QEMUResetEntry
{
3098 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3099 QEMUResetHandler
*func
;
3103 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3104 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3105 static int reset_requested
;
3106 static int shutdown_requested
;
3107 static int powerdown_requested
;
3108 static int debug_requested
;
3109 static int vmstop_requested
;
3111 int qemu_shutdown_requested(void)
3113 int r
= shutdown_requested
;
3114 shutdown_requested
= 0;
3118 int qemu_reset_requested(void)
3120 int r
= reset_requested
;
3121 reset_requested
= 0;
3125 int qemu_powerdown_requested(void)
3127 int r
= powerdown_requested
;
3128 powerdown_requested
= 0;
3132 static int qemu_debug_requested(void)
3134 int r
= debug_requested
;
3135 debug_requested
= 0;
3139 static int qemu_vmstop_requested(void)
3141 int r
= vmstop_requested
;
3142 vmstop_requested
= 0;
3146 static void do_vm_stop(int reason
)
3149 cpu_disable_ticks();
3152 vm_state_notify(0, reason
);
3156 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3158 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3161 re
->opaque
= opaque
;
3162 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3165 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3169 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3170 if (re
->func
== func
&& re
->opaque
== opaque
) {
3171 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3178 void qemu_system_reset(void)
3180 QEMUResetEntry
*re
, *nre
;
3182 /* reset all devices */
3183 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3184 re
->func(re
->opaque
);
3188 void qemu_system_reset_request(void)
3191 shutdown_requested
= 1;
3193 reset_requested
= 1;
3195 qemu_notify_event();
3198 void qemu_system_shutdown_request(void)
3200 shutdown_requested
= 1;
3201 qemu_notify_event();
3204 void qemu_system_powerdown_request(void)
3206 powerdown_requested
= 1;
3207 qemu_notify_event();
3210 #ifdef CONFIG_IOTHREAD
3211 static void qemu_system_vmstop_request(int reason
)
3213 vmstop_requested
= reason
;
3214 qemu_notify_event();
3219 static int io_thread_fd
= -1;
3221 static void qemu_event_increment(void)
3223 static const char byte
= 0;
3226 if (io_thread_fd
== -1)
3230 ret
= write(io_thread_fd
, &byte
, sizeof(byte
));
3231 } while (ret
< 0 && errno
== EINTR
);
3233 /* EAGAIN is fine, a read must be pending. */
3234 if (ret
< 0 && errno
!= EAGAIN
) {
3235 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3241 static void qemu_event_read(void *opaque
)
3243 int fd
= (unsigned long)opaque
;
3247 /* Drain the notify pipe */
3249 len
= read(fd
, buffer
, sizeof(buffer
));
3250 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
3253 static int qemu_event_init(void)
3258 err
= qemu_pipe(fds
);
3262 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3266 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3270 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3271 (void *)(unsigned long)fds
[0]);
3273 io_thread_fd
= fds
[1];
3282 HANDLE qemu_event_handle
;
3284 static void dummy_event_handler(void *opaque
)
3288 static int qemu_event_init(void)
3290 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3291 if (!qemu_event_handle
) {
3292 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3295 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3299 static void qemu_event_increment(void)
3301 if (!SetEvent(qemu_event_handle
)) {
3302 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3309 static int cpu_can_run(CPUState
*env
)
3320 #ifndef CONFIG_IOTHREAD
3321 static int qemu_init_main_loop(void)
3323 return qemu_event_init();
3326 void qemu_init_vcpu(void *_env
)
3328 CPUState
*env
= _env
;
3330 env
->nr_cores
= smp_cores
;
3331 env
->nr_threads
= smp_threads
;
3337 int qemu_cpu_self(void *env
)
3342 static void resume_all_vcpus(void)
3346 static void pause_all_vcpus(void)
3350 void qemu_cpu_kick(void *env
)
3355 void qemu_notify_event(void)
3357 CPUState
*env
= cpu_single_env
;
3364 void qemu_mutex_lock_iothread(void) {}
3365 void qemu_mutex_unlock_iothread(void) {}
3367 void vm_stop(int reason
)
3372 #else /* CONFIG_IOTHREAD */
3374 #include "qemu-thread.h"
3376 QemuMutex qemu_global_mutex
;
3377 static QemuMutex qemu_fair_mutex
;
3379 static QemuThread io_thread
;
3381 static QemuThread
*tcg_cpu_thread
;
3382 static QemuCond
*tcg_halt_cond
;
3384 static int qemu_system_ready
;
3386 static QemuCond qemu_cpu_cond
;
3388 static QemuCond qemu_system_cond
;
3389 static QemuCond qemu_pause_cond
;
3391 static void block_io_signals(void);
3392 static void unblock_io_signals(void);
3393 static int tcg_has_work(void);
3395 static int qemu_init_main_loop(void)
3399 ret
= qemu_event_init();
3403 qemu_cond_init(&qemu_pause_cond
);
3404 qemu_mutex_init(&qemu_fair_mutex
);
3405 qemu_mutex_init(&qemu_global_mutex
);
3406 qemu_mutex_lock(&qemu_global_mutex
);
3408 unblock_io_signals();
3409 qemu_thread_self(&io_thread
);
3414 static void qemu_wait_io_event(CPUState
*env
)
3416 while (!tcg_has_work())
3417 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3419 qemu_mutex_unlock(&qemu_global_mutex
);
3422 * Users of qemu_global_mutex can be starved, having no chance
3423 * to acquire it since this path will get to it first.
3424 * So use another lock to provide fairness.
3426 qemu_mutex_lock(&qemu_fair_mutex
);
3427 qemu_mutex_unlock(&qemu_fair_mutex
);
3429 qemu_mutex_lock(&qemu_global_mutex
);
3433 qemu_cond_signal(&qemu_pause_cond
);
3437 static int qemu_cpu_exec(CPUState
*env
);
3439 static void *kvm_cpu_thread_fn(void *arg
)
3441 CPUState
*env
= arg
;
3444 qemu_thread_self(env
->thread
);
3448 /* signal CPU creation */
3449 qemu_mutex_lock(&qemu_global_mutex
);
3451 qemu_cond_signal(&qemu_cpu_cond
);
3453 /* and wait for machine initialization */
3454 while (!qemu_system_ready
)
3455 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3458 if (cpu_can_run(env
))
3460 qemu_wait_io_event(env
);
3466 static void tcg_cpu_exec(void);
3468 static void *tcg_cpu_thread_fn(void *arg
)
3470 CPUState
*env
= arg
;
3473 qemu_thread_self(env
->thread
);
3475 /* signal CPU creation */
3476 qemu_mutex_lock(&qemu_global_mutex
);
3477 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3479 qemu_cond_signal(&qemu_cpu_cond
);
3481 /* and wait for machine initialization */
3482 while (!qemu_system_ready
)
3483 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3487 qemu_wait_io_event(cur_cpu
);
3493 void qemu_cpu_kick(void *_env
)
3495 CPUState
*env
= _env
;
3496 qemu_cond_broadcast(env
->halt_cond
);
3498 qemu_thread_signal(env
->thread
, SIGUSR1
);
3501 int qemu_cpu_self(void *_env
)
3503 CPUState
*env
= _env
;
3506 qemu_thread_self(&this);
3508 return qemu_thread_equal(&this, env
->thread
);
3511 static void cpu_signal(int sig
)
3514 cpu_exit(cpu_single_env
);
3517 static void block_io_signals(void)
3520 struct sigaction sigact
;
3523 sigaddset(&set
, SIGUSR2
);
3524 sigaddset(&set
, SIGIO
);
3525 sigaddset(&set
, SIGALRM
);
3526 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3529 sigaddset(&set
, SIGUSR1
);
3530 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3532 memset(&sigact
, 0, sizeof(sigact
));
3533 sigact
.sa_handler
= cpu_signal
;
3534 sigaction(SIGUSR1
, &sigact
, NULL
);
3537 static void unblock_io_signals(void)
3542 sigaddset(&set
, SIGUSR2
);
3543 sigaddset(&set
, SIGIO
);
3544 sigaddset(&set
, SIGALRM
);
3545 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3548 sigaddset(&set
, SIGUSR1
);
3549 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3552 static void qemu_signal_lock(unsigned int msecs
)
3554 qemu_mutex_lock(&qemu_fair_mutex
);
3556 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3557 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3558 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3561 qemu_mutex_unlock(&qemu_fair_mutex
);
3564 void qemu_mutex_lock_iothread(void)
3566 if (kvm_enabled()) {
3567 qemu_mutex_lock(&qemu_fair_mutex
);
3568 qemu_mutex_lock(&qemu_global_mutex
);
3569 qemu_mutex_unlock(&qemu_fair_mutex
);
3571 qemu_signal_lock(100);
3574 void qemu_mutex_unlock_iothread(void)
3576 qemu_mutex_unlock(&qemu_global_mutex
);
3579 static int all_vcpus_paused(void)
3581 CPUState
*penv
= first_cpu
;
3586 penv
= (CPUState
*)penv
->next_cpu
;
3592 static void pause_all_vcpus(void)
3594 CPUState
*penv
= first_cpu
;
3598 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3599 qemu_cpu_kick(penv
);
3600 penv
= (CPUState
*)penv
->next_cpu
;
3603 while (!all_vcpus_paused()) {
3604 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3607 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3608 penv
= (CPUState
*)penv
->next_cpu
;
3613 static void resume_all_vcpus(void)
3615 CPUState
*penv
= first_cpu
;
3620 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3621 qemu_cpu_kick(penv
);
3622 penv
= (CPUState
*)penv
->next_cpu
;
3626 static void tcg_init_vcpu(void *_env
)
3628 CPUState
*env
= _env
;
3629 /* share a single thread for all cpus with TCG */
3630 if (!tcg_cpu_thread
) {
3631 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3632 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3633 qemu_cond_init(env
->halt_cond
);
3634 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3635 while (env
->created
== 0)
3636 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3637 tcg_cpu_thread
= env
->thread
;
3638 tcg_halt_cond
= env
->halt_cond
;
3640 env
->thread
= tcg_cpu_thread
;
3641 env
->halt_cond
= tcg_halt_cond
;
3645 static void kvm_start_vcpu(CPUState
*env
)
3647 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3648 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3649 qemu_cond_init(env
->halt_cond
);
3650 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3651 while (env
->created
== 0)
3652 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3655 void qemu_init_vcpu(void *_env
)
3657 CPUState
*env
= _env
;
3659 env
->nr_cores
= smp_cores
;
3660 env
->nr_threads
= smp_threads
;
3662 kvm_start_vcpu(env
);
3667 void qemu_notify_event(void)
3669 qemu_event_increment();
3672 void vm_stop(int reason
)
3675 qemu_thread_self(&me
);
3677 if (!qemu_thread_equal(&me
, &io_thread
)) {
3678 qemu_system_vmstop_request(reason
);
3680 * FIXME: should not return to device code in case
3681 * vm_stop() has been requested.
3683 if (cpu_single_env
) {
3684 cpu_exit(cpu_single_env
);
3685 cpu_single_env
->stop
= 1;
3696 static void host_main_loop_wait(int *timeout
)
3702 /* XXX: need to suppress polling by better using win32 events */
3704 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3705 ret
|= pe
->func(pe
->opaque
);
3709 WaitObjects
*w
= &wait_objects
;
3711 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3712 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3713 if (w
->func
[ret
- WAIT_OBJECT_0
])
3714 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3716 /* Check for additional signaled events */
3717 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3719 /* Check if event is signaled */
3720 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3721 if(ret2
== WAIT_OBJECT_0
) {
3723 w
->func
[i
](w
->opaque
[i
]);
3724 } else if (ret2
== WAIT_TIMEOUT
) {
3726 err
= GetLastError();
3727 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3730 } else if (ret
== WAIT_TIMEOUT
) {
3732 err
= GetLastError();
3733 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3740 static void host_main_loop_wait(int *timeout
)
3745 void main_loop_wait(int timeout
)
3747 IOHandlerRecord
*ioh
;
3748 fd_set rfds
, wfds
, xfds
;
3752 qemu_bh_update_timeout(&timeout
);
3754 host_main_loop_wait(&timeout
);
3756 /* poll any events */
3757 /* XXX: separate device handlers from system ones */
3762 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3766 (!ioh
->fd_read_poll
||
3767 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3768 FD_SET(ioh
->fd
, &rfds
);
3772 if (ioh
->fd_write
) {
3773 FD_SET(ioh
->fd
, &wfds
);
3779 tv
.tv_sec
= timeout
/ 1000;
3780 tv
.tv_usec
= (timeout
% 1000) * 1000;
3782 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3784 qemu_mutex_unlock_iothread();
3785 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3786 qemu_mutex_lock_iothread();
3788 IOHandlerRecord
**pioh
;
3790 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3791 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3792 ioh
->fd_read(ioh
->opaque
);
3794 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3795 ioh
->fd_write(ioh
->opaque
);
3799 /* remove deleted IO handlers */
3800 pioh
= &first_io_handler
;
3811 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3813 /* rearm timer, if not periodic */
3814 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3815 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3816 qemu_rearm_alarm_timer(alarm_timer
);
3819 /* vm time timers */
3821 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3822 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3823 qemu_get_clock(vm_clock
));
3826 /* real time timers */
3827 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3828 qemu_get_clock(rt_clock
));
3830 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3831 qemu_get_clock(host_clock
));
3833 /* Check bottom-halves last in case any of the earlier events triggered
3839 static int qemu_cpu_exec(CPUState
*env
)
3842 #ifdef CONFIG_PROFILER
3846 #ifdef CONFIG_PROFILER
3847 ti
= profile_getclock();
3852 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3853 env
->icount_decr
.u16
.low
= 0;
3854 env
->icount_extra
= 0;
3855 count
= qemu_next_deadline();
3856 count
= (count
+ (1 << icount_time_shift
) - 1)
3857 >> icount_time_shift
;
3858 qemu_icount
+= count
;
3859 decr
= (count
> 0xffff) ? 0xffff : count
;
3861 env
->icount_decr
.u16
.low
= decr
;
3862 env
->icount_extra
= count
;
3864 ret
= cpu_exec(env
);
3865 #ifdef CONFIG_PROFILER
3866 qemu_time
+= profile_getclock() - ti
;
3869 /* Fold pending instructions back into the
3870 instruction counter, and clear the interrupt flag. */
3871 qemu_icount
-= (env
->icount_decr
.u16
.low
3872 + env
->icount_extra
);
3873 env
->icount_decr
.u32
= 0;
3874 env
->icount_extra
= 0;
3879 static void tcg_cpu_exec(void)
3883 if (next_cpu
== NULL
)
3884 next_cpu
= first_cpu
;
3885 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3886 CPUState
*env
= cur_cpu
= next_cpu
;
3888 if (timer_alarm_pending
) {
3889 timer_alarm_pending
= 0;
3892 if (cpu_can_run(env
))
3893 ret
= qemu_cpu_exec(env
);
3897 if (ret
== EXCP_DEBUG
) {
3898 gdb_set_stop_cpu(env
);
3899 debug_requested
= 1;
3905 static int cpu_has_work(CPUState
*env
)
3913 if (qemu_cpu_has_work(env
))
3918 static int tcg_has_work(void)
3922 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3923 if (cpu_has_work(env
))
3928 static int qemu_calculate_timeout(void)
3930 #ifndef CONFIG_IOTHREAD
3935 else if (tcg_has_work())
3937 else if (!use_icount
)
3940 /* XXX: use timeout computed from timers */
3943 /* Advance virtual time to the next event. */
3944 if (use_icount
== 1) {
3945 /* When not using an adaptive execution frequency
3946 we tend to get badly out of sync with real time,
3947 so just delay for a reasonable amount of time. */
3950 delta
= cpu_get_icount() - cpu_get_clock();
3953 /* If virtual time is ahead of real time then just
3955 timeout
= (delta
/ 1000000) + 1;
3957 /* Wait for either IO to occur or the next
3959 add
= qemu_next_deadline();
3960 /* We advance the timer before checking for IO.
3961 Limit the amount we advance so that early IO
3962 activity won't get the guest too far ahead. */
3966 add
= (add
+ (1 << icount_time_shift
) - 1)
3967 >> icount_time_shift
;
3969 timeout
= delta
/ 1000000;
3976 #else /* CONFIG_IOTHREAD */
3981 static int vm_can_run(void)
3983 if (powerdown_requested
)
3985 if (reset_requested
)
3987 if (shutdown_requested
)
3989 if (debug_requested
)
3994 qemu_irq qemu_system_powerdown
;
3996 static void main_loop(void)
4000 #ifdef CONFIG_IOTHREAD
4001 qemu_system_ready
= 1;
4002 qemu_cond_broadcast(&qemu_system_cond
);
4007 #ifdef CONFIG_PROFILER
4010 #ifndef CONFIG_IOTHREAD
4013 #ifdef CONFIG_PROFILER
4014 ti
= profile_getclock();
4016 main_loop_wait(qemu_calculate_timeout());
4017 #ifdef CONFIG_PROFILER
4018 dev_time
+= profile_getclock() - ti
;
4020 } while (vm_can_run());
4022 if (qemu_debug_requested()) {
4023 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
4024 vm_stop(EXCP_DEBUG
);
4026 if (qemu_shutdown_requested()) {
4027 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4034 if (qemu_reset_requested()) {
4035 monitor_protocol_event(QEVENT_RESET
, NULL
);
4037 qemu_system_reset();
4040 if (qemu_powerdown_requested()) {
4041 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4042 qemu_irq_raise(qemu_system_powerdown
);
4044 if ((r
= qemu_vmstop_requested())) {
4045 monitor_protocol_event(QEVENT_STOP
, NULL
);
4052 static void version(void)
4054 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4057 static void help(int exitcode
)
4059 const char *options_help
=
4060 #define DEF(option, opt_arg, opt_enum, opt_help) \
4062 #define DEFHEADING(text) stringify(text) "\n"
4063 #include "qemu-options.h"
4069 printf("usage: %s [options] [disk_image]\n"
4071 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4074 "During emulation, the following keys are useful:\n"
4075 "ctrl-alt-f toggle full screen\n"
4076 "ctrl-alt-n switch to virtual console 'n'\n"
4077 "ctrl-alt toggle mouse and keyboard grab\n"
4079 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4085 #define HAS_ARG 0x0001
4088 #define DEF(option, opt_arg, opt_enum, opt_help) \
4090 #define DEFHEADING(text)
4091 #include "qemu-options.h"
4097 typedef struct QEMUOption
{
4103 static const QEMUOption qemu_options
[] = {
4104 { "h", 0, QEMU_OPTION_h
},
4105 #define DEF(option, opt_arg, opt_enum, opt_help) \
4106 { option, opt_arg, opt_enum },
4107 #define DEFHEADING(text)
4108 #include "qemu-options.h"
4116 struct soundhw soundhw
[] = {
4117 #ifdef HAS_AUDIO_CHOICE
4118 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4124 { .init_isa
= pcspk_audio_init
}
4131 "Creative Sound Blaster 16",
4134 { .init_isa
= SB16_init
}
4138 #ifdef CONFIG_CS4231A
4144 { .init_isa
= cs4231a_init
}
4152 "Yamaha YMF262 (OPL3)",
4154 "Yamaha YM3812 (OPL2)",
4158 { .init_isa
= Adlib_init
}
4165 "Gravis Ultrasound GF1",
4168 { .init_isa
= GUS_init
}
4175 "Intel 82801AA AC97 Audio",
4178 { .init_pci
= ac97_init
}
4182 #ifdef CONFIG_ES1370
4185 "ENSONIQ AudioPCI ES1370",
4188 { .init_pci
= es1370_init
}
4192 #endif /* HAS_AUDIO_CHOICE */
4194 { NULL
, NULL
, 0, 0, { NULL
} }
4197 static void select_soundhw (const char *optarg
)
4201 if (*optarg
== '?') {
4204 printf ("Valid sound card names (comma separated):\n");
4205 for (c
= soundhw
; c
->name
; ++c
) {
4206 printf ("%-11s %s\n", c
->name
, c
->descr
);
4208 printf ("\n-soundhw all will enable all of the above\n");
4209 exit (*optarg
!= '?');
4217 if (!strcmp (optarg
, "all")) {
4218 for (c
= soundhw
; c
->name
; ++c
) {
4226 e
= strchr (p
, ',');
4227 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4229 for (c
= soundhw
; c
->name
; ++c
) {
4230 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4239 "Unknown sound card name (too big to show)\n");
4242 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4247 p
+= l
+ (e
!= NULL
);
4251 goto show_valid_cards
;
4256 static void select_vgahw (const char *p
)
4261 vga_interface_type
= VGA_NONE
;
4262 if (strstart(p
, "std", &opts
)) {
4263 vga_interface_type
= VGA_STD
;
4264 } else if (strstart(p
, "cirrus", &opts
)) {
4265 vga_interface_type
= VGA_CIRRUS
;
4266 } else if (strstart(p
, "vmware", &opts
)) {
4267 vga_interface_type
= VGA_VMWARE
;
4268 } else if (strstart(p
, "xenfb", &opts
)) {
4269 vga_interface_type
= VGA_XENFB
;
4270 } else if (!strstart(p
, "none", &opts
)) {
4272 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4276 const char *nextopt
;
4278 if (strstart(opts
, ",retrace=", &nextopt
)) {
4280 if (strstart(opts
, "dumb", &nextopt
))
4281 vga_retrace_method
= VGA_RETRACE_DUMB
;
4282 else if (strstart(opts
, "precise", &nextopt
))
4283 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4284 else goto invalid_vga
;
4285 } else goto invalid_vga
;
4291 static int balloon_parse(const char *arg
)
4295 if (strcmp(arg
, "none") == 0) {
4299 if (!strncmp(arg
, "virtio", 6)) {
4300 if (arg
[6] == ',') {
4301 /* have params -> parse them */
4302 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4306 /* create empty opts */
4307 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4309 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4318 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4320 exit(STATUS_CONTROL_C_EXIT
);
4325 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4329 if(strlen(str
) != 36)
4332 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4333 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4334 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4340 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4348 static void termsig_handler(int signal
)
4350 qemu_system_shutdown_request();
4353 static void sigchld_handler(int signal
)
4355 waitpid(-1, NULL
, WNOHANG
);
4358 static void sighandler_setup(void)
4360 struct sigaction act
;
4362 memset(&act
, 0, sizeof(act
));
4363 act
.sa_handler
= termsig_handler
;
4364 sigaction(SIGINT
, &act
, NULL
);
4365 sigaction(SIGHUP
, &act
, NULL
);
4366 sigaction(SIGTERM
, &act
, NULL
);
4368 act
.sa_handler
= sigchld_handler
;
4369 act
.sa_flags
= SA_NOCLDSTOP
;
4370 sigaction(SIGCHLD
, &act
, NULL
);
4376 /* Look for support files in the same directory as the executable. */
4377 static char *find_datadir(const char *argv0
)
4383 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4390 while (p
!= buf
&& *p
!= '\\')
4393 if (access(buf
, R_OK
) == 0) {
4394 return qemu_strdup(buf
);
4400 /* Find a likely location for support files using the location of the binary.
4401 For installed binaries this will be "$bindir/../share/qemu". When
4402 running from the build tree this will be "$bindir/../pc-bios". */
4403 #define SHARE_SUFFIX "/share/qemu"
4404 #define BUILD_SUFFIX "/pc-bios"
4405 static char *find_datadir(const char *argv0
)
4413 #if defined(__linux__)
4416 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4422 #elif defined(__FreeBSD__)
4425 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4432 /* If we don't have any way of figuring out the actual executable
4433 location then try argv[0]. */
4435 p
= realpath(argv0
, buf
);
4443 max_len
= strlen(dir
) +
4444 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4445 res
= qemu_mallocz(max_len
);
4446 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4447 if (access(res
, R_OK
)) {
4448 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4449 if (access(res
, R_OK
)) {
4461 char *qemu_find_file(int type
, const char *name
)
4467 /* If name contains path separators then try it as a straight path. */
4468 if ((strchr(name
, '/') || strchr(name
, '\\'))
4469 && access(name
, R_OK
) == 0) {
4470 return qemu_strdup(name
);
4473 case QEMU_FILE_TYPE_BIOS
:
4476 case QEMU_FILE_TYPE_KEYMAP
:
4477 subdir
= "keymaps/";
4482 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4483 buf
= qemu_mallocz(len
);
4484 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4485 if (access(buf
, R_OK
)) {
4492 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4494 return qdev_device_help(opts
);
4497 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4501 dev
= qdev_device_add(opts
);
4507 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4509 CharDriverState
*chr
;
4511 chr
= qemu_chr_open_opts(opts
, NULL
);
4517 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4519 CharDriverState
*chr
;
4520 const char *chardev
;
4524 mode
= qemu_opt_get(opts
, "mode");
4528 if (strcmp(mode
, "readline") == 0) {
4529 flags
= MONITOR_USE_READLINE
;
4530 } else if (strcmp(mode
, "control") == 0) {
4531 flags
= MONITOR_USE_CONTROL
;
4533 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4537 if (qemu_opt_get_bool(opts
, "default", 0))
4538 flags
|= MONITOR_IS_DEFAULT
;
4540 chardev
= qemu_opt_get(opts
, "chardev");
4541 chr
= qemu_chr_find(chardev
);
4543 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4547 monitor_init(chr
, flags
);
4551 static void monitor_parse(const char *optarg
, const char *mode
)
4553 static int monitor_device_index
= 0;
4559 if (strstart(optarg
, "chardev:", &p
)) {
4560 snprintf(label
, sizeof(label
), "%s", p
);
4562 if (monitor_device_index
) {
4563 snprintf(label
, sizeof(label
), "monitor%d",
4564 monitor_device_index
);
4566 snprintf(label
, sizeof(label
), "monitor");
4569 opts
= qemu_chr_parse_compat(label
, optarg
);
4571 fprintf(stderr
, "parse error: %s\n", optarg
);
4576 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4578 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4581 qemu_opt_set(opts
, "mode", mode
);
4582 qemu_opt_set(opts
, "chardev", label
);
4584 qemu_opt_set(opts
, "default", "on");
4585 monitor_device_index
++;
4588 struct device_config
{
4590 DEV_USB
, /* -usbdevice */
4592 DEV_SERIAL
, /* -serial */
4593 DEV_PARALLEL
, /* -parallel */
4594 DEV_VIRTCON
, /* -virtioconsole */
4595 DEV_DEBUGCON
, /* -debugcon */
4597 const char *cmdline
;
4598 QTAILQ_ENTRY(device_config
) next
;
4600 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4602 static void add_device_config(int type
, const char *cmdline
)
4604 struct device_config
*conf
;
4606 conf
= qemu_mallocz(sizeof(*conf
));
4608 conf
->cmdline
= cmdline
;
4609 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4612 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4614 struct device_config
*conf
;
4617 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4618 if (conf
->type
!= type
)
4620 rc
= func(conf
->cmdline
);
4627 static int serial_parse(const char *devname
)
4629 static int index
= 0;
4632 if (strcmp(devname
, "none") == 0)
4634 if (index
== MAX_SERIAL_PORTS
) {
4635 fprintf(stderr
, "qemu: too many serial ports\n");
4638 snprintf(label
, sizeof(label
), "serial%d", index
);
4639 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4640 if (!serial_hds
[index
]) {
4641 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4642 devname
, strerror(errno
));
4649 static int parallel_parse(const char *devname
)
4651 static int index
= 0;
4654 if (strcmp(devname
, "none") == 0)
4656 if (index
== MAX_PARALLEL_PORTS
) {
4657 fprintf(stderr
, "qemu: too many parallel ports\n");
4660 snprintf(label
, sizeof(label
), "parallel%d", index
);
4661 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4662 if (!parallel_hds
[index
]) {
4663 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4664 devname
, strerror(errno
));
4671 static int virtcon_parse(const char *devname
)
4673 static int index
= 0;
4675 QemuOpts
*bus_opts
, *dev_opts
;
4677 if (strcmp(devname
, "none") == 0)
4679 if (index
== MAX_VIRTIO_CONSOLES
) {
4680 fprintf(stderr
, "qemu: too many virtio consoles\n");
4684 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4685 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4687 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4688 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4690 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4691 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4692 if (!virtcon_hds
[index
]) {
4693 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4694 devname
, strerror(errno
));
4697 qemu_opt_set(dev_opts
, "chardev", label
);
4703 static int debugcon_parse(const char *devname
)
4707 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4710 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4712 fprintf(stderr
, "qemu: already have a debugcon device\n");
4715 qemu_opt_set(opts
, "driver", "isa-debugcon");
4716 qemu_opt_set(opts
, "chardev", "debugcon");
4720 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4721 const char **poptarg
, int *poptind
)
4723 const QEMUOption
*popt
;
4724 int optind
= *poptind
;
4725 char *r
= argv
[optind
];
4729 /* Treat --foo the same as -foo. */
4732 popt
= qemu_options
;
4735 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4739 if (!strcmp(popt
->name
, r
+ 1))
4743 if (popt
->flags
& HAS_ARG
) {
4744 if (optind
>= argc
) {
4745 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4749 optarg
= argv
[optind
++];
4760 int main(int argc
, char **argv
, char **envp
)
4762 const char *gdbstub_dev
= NULL
;
4763 uint32_t boot_devices_bitmap
= 0;
4765 int snapshot
, linux_boot
, net_boot
;
4766 const char *initrd_filename
;
4767 const char *kernel_filename
, *kernel_cmdline
;
4768 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4770 DisplayChangeListener
*dcl
;
4771 int cyls
, heads
, secs
, translation
;
4772 QemuOpts
*hda_opts
= NULL
, *opts
;
4775 const char *loadvm
= NULL
;
4776 QEMUMachine
*machine
;
4777 const char *cpu_model
;
4782 const char *pid_file
= NULL
;
4783 const char *incoming
= NULL
;
4786 struct passwd
*pwd
= NULL
;
4787 const char *chroot_dir
= NULL
;
4788 const char *run_as
= NULL
;
4791 int show_vnc_port
= 0;
4796 qemu_errors_to_file(stderr
);
4797 qemu_cache_utils_init(envp
);
4799 QLIST_INIT (&vm_change_state_head
);
4802 struct sigaction act
;
4803 sigfillset(&act
.sa_mask
);
4805 act
.sa_handler
= SIG_IGN
;
4806 sigaction(SIGPIPE
, &act
, NULL
);
4809 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4810 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4811 QEMU to run on a single CPU */
4816 h
= GetCurrentProcess();
4817 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4818 for(i
= 0; i
< 32; i
++) {
4819 if (mask
& (1 << i
))
4824 SetProcessAffinityMask(h
, mask
);
4830 module_call_init(MODULE_INIT_MACHINE
);
4831 machine
= find_default_machine();
4833 initrd_filename
= NULL
;
4836 kernel_filename
= NULL
;
4837 kernel_cmdline
= "";
4838 cyls
= heads
= secs
= 0;
4839 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4841 for (i
= 0; i
< MAX_NODES
; i
++) {
4843 node_cpumask
[i
] = 0;
4852 /* first pass of option parsing */
4854 while (optind
< argc
) {
4855 if (argv
[optind
][0] != '-') {
4860 const QEMUOption
*popt
;
4862 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4863 switch (popt
->index
) {
4864 case QEMU_OPTION_nodefconfig
:
4873 fp
= fopen(CONFIG_QEMU_CONFDIR
"/qemu.conf", "r");
4875 if (qemu_config_parse(fp
) != 0) {
4881 fp
= fopen(CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", "r");
4883 if (qemu_config_parse(fp
) != 0) {
4890 /* second pass of option parsing */
4895 if (argv
[optind
][0] != '-') {
4896 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4898 const QEMUOption
*popt
;
4900 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4901 switch(popt
->index
) {
4903 machine
= find_machine(optarg
);
4906 printf("Supported machines are:\n");
4907 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4909 printf("%-10s %s (alias of %s)\n",
4910 m
->alias
, m
->desc
, m
->name
);
4911 printf("%-10s %s%s\n",
4913 m
->is_default
? " (default)" : "");
4915 exit(*optarg
!= '?');
4918 case QEMU_OPTION_cpu
:
4919 /* hw initialization will check this */
4920 if (*optarg
== '?') {
4921 /* XXX: implement xxx_cpu_list for targets that still miss it */
4922 #if defined(cpu_list)
4923 cpu_list(stdout
, &fprintf
);
4930 case QEMU_OPTION_initrd
:
4931 initrd_filename
= optarg
;
4933 case QEMU_OPTION_hda
:
4935 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4937 hda_opts
= drive_add(optarg
, HD_ALIAS
4938 ",cyls=%d,heads=%d,secs=%d%s",
4939 0, cyls
, heads
, secs
,
4940 translation
== BIOS_ATA_TRANSLATION_LBA
?
4942 translation
== BIOS_ATA_TRANSLATION_NONE
?
4943 ",trans=none" : "");
4945 case QEMU_OPTION_hdb
:
4946 case QEMU_OPTION_hdc
:
4947 case QEMU_OPTION_hdd
:
4948 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4950 case QEMU_OPTION_drive
:
4951 drive_add(NULL
, "%s", optarg
);
4953 case QEMU_OPTION_set
:
4954 if (qemu_set_option(optarg
) != 0)
4957 case QEMU_OPTION_global
:
4958 if (qemu_global_option(optarg
) != 0)
4961 case QEMU_OPTION_mtdblock
:
4962 drive_add(optarg
, MTD_ALIAS
);
4964 case QEMU_OPTION_sd
:
4965 drive_add(optarg
, SD_ALIAS
);
4967 case QEMU_OPTION_pflash
:
4968 drive_add(optarg
, PFLASH_ALIAS
);
4970 case QEMU_OPTION_snapshot
:
4973 case QEMU_OPTION_hdachs
:
4977 cyls
= strtol(p
, (char **)&p
, 0);
4978 if (cyls
< 1 || cyls
> 16383)
4983 heads
= strtol(p
, (char **)&p
, 0);
4984 if (heads
< 1 || heads
> 16)
4989 secs
= strtol(p
, (char **)&p
, 0);
4990 if (secs
< 1 || secs
> 63)
4994 if (!strcmp(p
, "none"))
4995 translation
= BIOS_ATA_TRANSLATION_NONE
;
4996 else if (!strcmp(p
, "lba"))
4997 translation
= BIOS_ATA_TRANSLATION_LBA
;
4998 else if (!strcmp(p
, "auto"))
4999 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5002 } else if (*p
!= '\0') {
5004 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5007 if (hda_opts
!= NULL
) {
5009 snprintf(num
, sizeof(num
), "%d", cyls
);
5010 qemu_opt_set(hda_opts
, "cyls", num
);
5011 snprintf(num
, sizeof(num
), "%d", heads
);
5012 qemu_opt_set(hda_opts
, "heads", num
);
5013 snprintf(num
, sizeof(num
), "%d", secs
);
5014 qemu_opt_set(hda_opts
, "secs", num
);
5015 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5016 qemu_opt_set(hda_opts
, "trans", "lba");
5017 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5018 qemu_opt_set(hda_opts
, "trans", "none");
5022 case QEMU_OPTION_numa
:
5023 if (nb_numa_nodes
>= MAX_NODES
) {
5024 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5029 case QEMU_OPTION_nographic
:
5030 display_type
= DT_NOGRAPHIC
;
5032 #ifdef CONFIG_CURSES
5033 case QEMU_OPTION_curses
:
5034 display_type
= DT_CURSES
;
5037 case QEMU_OPTION_portrait
:
5040 case QEMU_OPTION_kernel
:
5041 kernel_filename
= optarg
;
5043 case QEMU_OPTION_append
:
5044 kernel_cmdline
= optarg
;
5046 case QEMU_OPTION_cdrom
:
5047 drive_add(optarg
, CDROM_ALIAS
);
5049 case QEMU_OPTION_boot
:
5051 static const char * const params
[] = {
5052 "order", "once", "menu", NULL
5054 char buf
[sizeof(boot_devices
)];
5055 char *standard_boot_devices
;
5058 if (!strchr(optarg
, '=')) {
5060 pstrcpy(buf
, sizeof(buf
), optarg
);
5061 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5063 "qemu: unknown boot parameter '%s' in '%s'\n",
5069 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5070 boot_devices_bitmap
= parse_bootdevices(buf
);
5071 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5074 if (get_param_value(buf
, sizeof(buf
),
5076 boot_devices_bitmap
|= parse_bootdevices(buf
);
5077 standard_boot_devices
= qemu_strdup(boot_devices
);
5078 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5079 qemu_register_reset(restore_boot_devices
,
5080 standard_boot_devices
);
5082 if (get_param_value(buf
, sizeof(buf
),
5084 if (!strcmp(buf
, "on")) {
5086 } else if (!strcmp(buf
, "off")) {
5090 "qemu: invalid option value '%s'\n",
5098 case QEMU_OPTION_fda
:
5099 case QEMU_OPTION_fdb
:
5100 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5103 case QEMU_OPTION_no_fd_bootchk
:
5107 case QEMU_OPTION_netdev
:
5108 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5112 case QEMU_OPTION_net
:
5113 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5118 case QEMU_OPTION_tftp
:
5119 legacy_tftp_prefix
= optarg
;
5121 case QEMU_OPTION_bootp
:
5122 legacy_bootp_filename
= optarg
;
5125 case QEMU_OPTION_smb
:
5126 if (net_slirp_smb(optarg
) < 0)
5130 case QEMU_OPTION_redir
:
5131 if (net_slirp_redir(optarg
) < 0)
5135 case QEMU_OPTION_bt
:
5136 add_device_config(DEV_BT
, optarg
);
5139 case QEMU_OPTION_audio_help
:
5143 case QEMU_OPTION_soundhw
:
5144 select_soundhw (optarg
);
5150 case QEMU_OPTION_version
:
5154 case QEMU_OPTION_m
: {
5158 value
= strtoul(optarg
, &ptr
, 10);
5160 case 0: case 'M': case 'm':
5167 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5171 /* On 32-bit hosts, QEMU is limited by virtual address space */
5172 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5173 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5176 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5177 fprintf(stderr
, "qemu: ram size too large\n");
5186 const CPULogItem
*item
;
5188 mask
= cpu_str_to_log_mask(optarg
);
5190 printf("Log items (comma separated):\n");
5191 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5192 printf("%-10s %s\n", item
->name
, item
->help
);
5200 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5202 case QEMU_OPTION_gdb
:
5203 gdbstub_dev
= optarg
;
5208 case QEMU_OPTION_bios
:
5211 case QEMU_OPTION_singlestep
:
5218 keyboard_layout
= optarg
;
5220 case QEMU_OPTION_localtime
:
5223 case QEMU_OPTION_vga
:
5224 select_vgahw (optarg
);
5226 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5232 w
= strtol(p
, (char **)&p
, 10);
5235 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5241 h
= strtol(p
, (char **)&p
, 10);
5246 depth
= strtol(p
, (char **)&p
, 10);
5247 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5248 depth
!= 24 && depth
!= 32)
5250 } else if (*p
== '\0') {
5251 depth
= graphic_depth
;
5258 graphic_depth
= depth
;
5262 case QEMU_OPTION_echr
:
5265 term_escape_char
= strtol(optarg
, &r
, 0);
5267 printf("Bad argument to echr\n");
5270 case QEMU_OPTION_monitor
:
5271 monitor_parse(optarg
, "readline");
5272 default_monitor
= 0;
5274 case QEMU_OPTION_qmp
:
5275 monitor_parse(optarg
, "control");
5276 default_monitor
= 0;
5278 case QEMU_OPTION_mon
:
5279 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5281 fprintf(stderr
, "parse error: %s\n", optarg
);
5284 default_monitor
= 0;
5286 case QEMU_OPTION_chardev
:
5287 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5289 fprintf(stderr
, "parse error: %s\n", optarg
);
5293 case QEMU_OPTION_serial
:
5294 add_device_config(DEV_SERIAL
, optarg
);
5297 case QEMU_OPTION_watchdog
:
5300 "qemu: only one watchdog option may be given\n");
5305 case QEMU_OPTION_watchdog_action
:
5306 if (select_watchdog_action(optarg
) == -1) {
5307 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5311 case QEMU_OPTION_virtiocon
:
5312 add_device_config(DEV_VIRTCON
, optarg
);
5313 default_virtcon
= 0;
5315 case QEMU_OPTION_parallel
:
5316 add_device_config(DEV_PARALLEL
, optarg
);
5317 default_parallel
= 0;
5319 case QEMU_OPTION_debugcon
:
5320 add_device_config(DEV_DEBUGCON
, optarg
);
5322 case QEMU_OPTION_loadvm
:
5325 case QEMU_OPTION_full_screen
:
5329 case QEMU_OPTION_no_frame
:
5332 case QEMU_OPTION_alt_grab
:
5335 case QEMU_OPTION_ctrl_grab
:
5338 case QEMU_OPTION_no_quit
:
5341 case QEMU_OPTION_sdl
:
5342 display_type
= DT_SDL
;
5345 case QEMU_OPTION_pidfile
:
5349 case QEMU_OPTION_win2k_hack
:
5350 win2k_install_hack
= 1;
5352 case QEMU_OPTION_rtc_td_hack
:
5355 case QEMU_OPTION_acpitable
:
5356 if(acpi_table_add(optarg
) < 0) {
5357 fprintf(stderr
, "Wrong acpi table provided\n");
5361 case QEMU_OPTION_smbios
:
5362 if(smbios_entry_add(optarg
) < 0) {
5363 fprintf(stderr
, "Wrong smbios provided\n");
5369 case QEMU_OPTION_enable_kvm
:
5373 case QEMU_OPTION_usb
:
5376 case QEMU_OPTION_usbdevice
:
5378 add_device_config(DEV_USB
, optarg
);
5380 case QEMU_OPTION_device
:
5381 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5385 case QEMU_OPTION_smp
:
5388 fprintf(stderr
, "Invalid number of CPUs\n");
5391 if (max_cpus
< smp_cpus
) {
5392 fprintf(stderr
, "maxcpus must be equal to or greater than "
5396 if (max_cpus
> 255) {
5397 fprintf(stderr
, "Unsupported number of maxcpus\n");
5401 case QEMU_OPTION_vnc
:
5402 display_type
= DT_VNC
;
5403 vnc_display
= optarg
;
5406 case QEMU_OPTION_no_acpi
:
5409 case QEMU_OPTION_no_hpet
:
5412 case QEMU_OPTION_balloon
:
5413 if (balloon_parse(optarg
) < 0) {
5414 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5419 case QEMU_OPTION_no_reboot
:
5422 case QEMU_OPTION_no_shutdown
:
5425 case QEMU_OPTION_show_cursor
:
5428 case QEMU_OPTION_uuid
:
5429 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5430 fprintf(stderr
, "Fail to parse UUID string."
5431 " Wrong format.\n");
5436 case QEMU_OPTION_daemonize
:
5440 case QEMU_OPTION_option_rom
:
5441 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5442 fprintf(stderr
, "Too many option ROMs\n");
5445 option_rom
[nb_option_roms
] = optarg
;
5448 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5449 case QEMU_OPTION_semihosting
:
5450 semihosting_enabled
= 1;
5453 case QEMU_OPTION_name
:
5454 qemu_name
= qemu_strdup(optarg
);
5456 char *p
= strchr(qemu_name
, ',');
5459 if (strncmp(p
, "process=", 8)) {
5460 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5468 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5469 case QEMU_OPTION_prom_env
:
5470 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5471 fprintf(stderr
, "Too many prom variables\n");
5474 prom_envs
[nb_prom_envs
] = optarg
;
5479 case QEMU_OPTION_old_param
:
5483 case QEMU_OPTION_clock
:
5484 configure_alarms(optarg
);
5486 case QEMU_OPTION_startdate
:
5487 configure_rtc_date_offset(optarg
, 1);
5489 case QEMU_OPTION_rtc
:
5490 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5492 fprintf(stderr
, "parse error: %s\n", optarg
);
5495 configure_rtc(opts
);
5497 case QEMU_OPTION_tb_size
:
5498 tb_size
= strtol(optarg
, NULL
, 0);
5502 case QEMU_OPTION_icount
:
5504 if (strcmp(optarg
, "auto") == 0) {
5505 icount_time_shift
= -1;
5507 icount_time_shift
= strtol(optarg
, NULL
, 0);
5510 case QEMU_OPTION_incoming
:
5513 case QEMU_OPTION_nodefaults
:
5515 default_parallel
= 0;
5516 default_virtcon
= 0;
5517 default_monitor
= 0;
5525 case QEMU_OPTION_chroot
:
5526 chroot_dir
= optarg
;
5528 case QEMU_OPTION_runas
:
5533 case QEMU_OPTION_xen_domid
:
5534 xen_domid
= atoi(optarg
);
5536 case QEMU_OPTION_xen_create
:
5537 xen_mode
= XEN_CREATE
;
5539 case QEMU_OPTION_xen_attach
:
5540 xen_mode
= XEN_ATTACH
;
5543 case QEMU_OPTION_readconfig
:
5546 fp
= fopen(optarg
, "r");
5548 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5551 if (qemu_config_parse(fp
) != 0) {
5557 case QEMU_OPTION_writeconfig
:
5560 if (strcmp(optarg
, "-") == 0) {
5563 fp
= fopen(optarg
, "w");
5565 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5569 qemu_config_write(fp
);
5577 /* If no data_dir is specified then try to find it relative to the
5580 data_dir
= find_datadir(argv
[0]);
5582 /* If all else fails use the install patch specified when building. */
5584 data_dir
= CONFIG_QEMU_SHAREDIR
;
5588 * Default to max_cpus = smp_cpus, in case the user doesn't
5589 * specify a max_cpus value.
5592 max_cpus
= smp_cpus
;
5594 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5595 if (smp_cpus
> machine
->max_cpus
) {
5596 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5597 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5602 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5603 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5605 if (machine
->no_serial
) {
5608 if (machine
->no_parallel
) {
5609 default_parallel
= 0;
5611 if (!machine
->use_virtcon
) {
5612 default_virtcon
= 0;
5614 if (machine
->no_vga
) {
5617 if (machine
->no_floppy
) {
5620 if (machine
->no_cdrom
) {
5623 if (machine
->no_sdcard
) {
5627 if (display_type
== DT_NOGRAPHIC
) {
5628 if (default_parallel
)
5629 add_device_config(DEV_PARALLEL
, "null");
5630 if (default_serial
&& default_monitor
) {
5631 add_device_config(DEV_SERIAL
, "mon:stdio");
5632 } else if (default_virtcon
&& default_monitor
) {
5633 add_device_config(DEV_VIRTCON
, "mon:stdio");
5636 add_device_config(DEV_SERIAL
, "stdio");
5637 if (default_virtcon
)
5638 add_device_config(DEV_VIRTCON
, "stdio");
5639 if (default_monitor
)
5640 monitor_parse("stdio", "readline");
5644 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5645 if (default_parallel
)
5646 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5647 if (default_monitor
)
5648 monitor_parse("vc:80Cx24C", "readline");
5649 if (default_virtcon
)
5650 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5653 vga_interface_type
= VGA_CIRRUS
;
5655 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5662 if (pipe(fds
) == -1)
5673 len
= read(fds
[0], &status
, 1);
5674 if (len
== -1 && (errno
== EINTR
))
5679 else if (status
== 1) {
5680 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5688 qemu_set_cloexec(fds
[1]);
5700 signal(SIGTSTP
, SIG_IGN
);
5701 signal(SIGTTOU
, SIG_IGN
);
5702 signal(SIGTTIN
, SIG_IGN
);
5706 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5710 if (write(fds
[1], &status
, 1) != 1) {
5711 perror("daemonize. Writing to pipe\n");
5715 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5719 if (kvm_enabled()) {
5722 ret
= kvm_init(smp_cpus
);
5724 fprintf(stderr
, "failed to initialize KVM\n");
5729 if (qemu_init_main_loop()) {
5730 fprintf(stderr
, "qemu_init_main_loop failed\n");
5733 linux_boot
= (kernel_filename
!= NULL
);
5735 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5736 fprintf(stderr
, "-append only allowed with -kernel option\n");
5740 if (!linux_boot
&& initrd_filename
!= NULL
) {
5741 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5746 /* Win32 doesn't support line-buffering and requires size >= 2 */
5747 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5750 if (init_timer_alarm() < 0) {
5751 fprintf(stderr
, "could not initialize alarm timer\n");
5754 if (use_icount
&& icount_time_shift
< 0) {
5756 /* 125MIPS seems a reasonable initial guess at the guest speed.
5757 It will be corrected fairly quickly anyway. */
5758 icount_time_shift
= 3;
5759 init_icount_adjust();
5766 if (net_init_clients() < 0) {
5770 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5771 net_set_boot_mask(net_boot
);
5773 /* init the bluetooth world */
5774 if (foreach_device_config(DEV_BT
, bt_parse
))
5777 /* init the memory */
5779 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5781 /* init the dynamic translator */
5782 cpu_exec_init_all(tb_size
* 1024 * 1024);
5784 bdrv_init_with_whitelist();
5788 if (default_cdrom
) {
5789 /* we always create the cdrom drive, even if no disk is there */
5790 drive_add(NULL
, CDROM_ALIAS
);
5793 if (default_floppy
) {
5794 /* we always create at least one floppy */
5795 drive_add(NULL
, FD_ALIAS
, 0);
5798 if (default_sdcard
) {
5799 /* we always create one sd slot, even if no card is in it */
5800 drive_add(NULL
, SD_ALIAS
);
5803 /* open the virtual block devices */
5805 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5806 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5809 vmstate_register(0, &vmstate_timers
,&timers_state
);
5810 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5813 if (nb_numa_nodes
> 0) {
5816 if (nb_numa_nodes
> smp_cpus
) {
5817 nb_numa_nodes
= smp_cpus
;
5820 /* If no memory size if given for any node, assume the default case
5821 * and distribute the available memory equally across all nodes
5823 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5824 if (node_mem
[i
] != 0)
5827 if (i
== nb_numa_nodes
) {
5828 uint64_t usedmem
= 0;
5830 /* On Linux, the each node's border has to be 8MB aligned,
5831 * the final node gets the rest.
5833 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5834 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5835 usedmem
+= node_mem
[i
];
5837 node_mem
[i
] = ram_size
- usedmem
;
5840 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5841 if (node_cpumask
[i
] != 0)
5844 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5845 * must cope with this anyway, because there are BIOSes out there in
5846 * real machines which also use this scheme.
5848 if (i
== nb_numa_nodes
) {
5849 for (i
= 0; i
< smp_cpus
; i
++) {
5850 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5855 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5857 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5859 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5861 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5864 module_call_init(MODULE_INIT_DEVICE
);
5866 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5870 i
= select_watchdog(watchdog
);
5872 exit (i
== 1 ? 1 : 0);
5875 if (machine
->compat_props
) {
5876 qdev_prop_register_global_list(machine
->compat_props
);
5880 machine
->init(ram_size
, boot_devices
,
5881 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5885 /* must be after terminal init, SDL library changes signal handlers */
5889 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5890 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5891 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5897 current_machine
= machine
;
5899 /* init USB devices */
5901 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5905 /* init generic devices */
5906 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5909 net_check_clients();
5911 /* just use the first displaystate for the moment */
5912 ds
= get_displaystate();
5914 if (display_type
== DT_DEFAULT
) {
5915 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5916 display_type
= DT_SDL
;
5918 display_type
= DT_VNC
;
5919 vnc_display
= "localhost:0,to=99";
5925 switch (display_type
) {
5928 #if defined(CONFIG_CURSES)
5930 curses_display_init(ds
, full_screen
);
5933 #if defined(CONFIG_SDL)
5935 sdl_display_init(ds
, full_screen
, no_frame
);
5937 #elif defined(CONFIG_COCOA)
5939 cocoa_display_init(ds
, full_screen
);
5943 vnc_display_init(ds
);
5944 if (vnc_display_open(ds
, vnc_display
) < 0)
5947 if (show_vnc_port
) {
5948 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5956 dcl
= ds
->listeners
;
5957 while (dcl
!= NULL
) {
5958 if (dcl
->dpy_refresh
!= NULL
) {
5959 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5960 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5965 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5966 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5967 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5970 text_consoles_set_display(ds
);
5972 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5975 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5976 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5981 qdev_machine_creation_done();
5983 if (rom_load_all() != 0) {
5984 fprintf(stderr
, "rom loading failed\n");
5988 qemu_system_reset();
5990 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5996 qemu_start_incoming_migration(incoming
);
5997 } else if (autostart
) {
6007 len
= write(fds
[1], &status
, 1);
6008 if (len
== -1 && (errno
== EINTR
))
6015 perror("not able to chdir to /");
6018 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
6024 pwd
= getpwnam(run_as
);
6026 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6032 if (chroot(chroot_dir
) < 0) {
6033 fprintf(stderr
, "chroot failed\n");
6037 perror("not able to chdir to /");
6043 if (setgid(pwd
->pw_gid
) < 0) {
6044 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6047 if (setuid(pwd
->pw_uid
) < 0) {
6048 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6051 if (setuid(0) != -1) {
6052 fprintf(stderr
, "Dropping privileges failed\n");