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 static void init_clocks(void)
937 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
938 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
939 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
941 rtc_clock
= host_clock
;
945 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
947 uint64_t expire_time
;
949 if (qemu_timer_pending(ts
)) {
950 expire_time
= ts
->expire_time
;
954 qemu_put_be64(f
, expire_time
);
957 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
959 uint64_t expire_time
;
961 expire_time
= qemu_get_be64(f
);
962 if (expire_time
!= -1) {
963 qemu_mod_timer(ts
, expire_time
);
969 static const VMStateDescription vmstate_timers
= {
972 .minimum_version_id
= 1,
973 .minimum_version_id_old
= 1,
974 .fields
= (VMStateField
[]) {
975 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
976 VMSTATE_INT64(dummy
, TimersState
),
977 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
978 VMSTATE_END_OF_LIST()
982 static void qemu_event_increment(void);
985 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
986 DWORD_PTR dwUser
, DWORD_PTR dw1
,
989 static void host_alarm_handler(int host_signum
)
993 #define DISP_FREQ 1000
995 static int64_t delta_min
= INT64_MAX
;
996 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
998 ti
= qemu_get_clock(vm_clock
);
999 if (last_clock
!= 0) {
1000 delta
= ti
- last_clock
;
1001 if (delta
< delta_min
)
1003 if (delta
> delta_max
)
1006 if (++count
== DISP_FREQ
) {
1007 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1008 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1009 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1010 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1011 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1013 delta_min
= INT64_MAX
;
1021 if (alarm_has_dynticks(alarm_timer
) ||
1023 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1024 qemu_get_clock(vm_clock
))) ||
1025 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1026 qemu_get_clock(rt_clock
)) ||
1027 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1028 qemu_get_clock(host_clock
))) {
1029 qemu_event_increment();
1030 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1032 #ifndef CONFIG_IOTHREAD
1034 /* stop the currently executing cpu because a timer occured */
1038 timer_alarm_pending
= 1;
1039 qemu_notify_event();
1043 static int64_t qemu_next_deadline(void)
1045 /* To avoid problems with overflow limit this to 2^32. */
1046 int64_t delta
= INT32_MAX
;
1048 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1049 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1050 qemu_get_clock(vm_clock
);
1052 if (active_timers
[QEMU_CLOCK_HOST
]) {
1053 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1054 qemu_get_clock(host_clock
);
1065 #if defined(__linux__)
1066 static uint64_t qemu_next_deadline_dyntick(void)
1074 delta
= (qemu_next_deadline() + 999) / 1000;
1076 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1077 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1078 qemu_get_clock(rt_clock
))*1000;
1079 if (rtdelta
< delta
)
1083 if (delta
< MIN_TIMER_REARM_US
)
1084 delta
= MIN_TIMER_REARM_US
;
1092 /* Sets a specific flag */
1093 static int fcntl_setfl(int fd
, int flag
)
1097 flags
= fcntl(fd
, F_GETFL
);
1101 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1107 #if defined(__linux__)
1109 #define RTC_FREQ 1024
1111 static void enable_sigio_timer(int fd
)
1113 struct sigaction act
;
1116 sigfillset(&act
.sa_mask
);
1118 act
.sa_handler
= host_alarm_handler
;
1120 sigaction(SIGIO
, &act
, NULL
);
1121 fcntl_setfl(fd
, O_ASYNC
);
1122 fcntl(fd
, F_SETOWN
, getpid());
1125 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1127 struct hpet_info info
;
1130 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1135 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1137 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1138 "error, but for better emulation accuracy type:\n"
1139 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1143 /* Check capabilities */
1144 r
= ioctl(fd
, HPET_INFO
, &info
);
1148 /* Enable periodic mode */
1149 r
= ioctl(fd
, HPET_EPI
, 0);
1150 if (info
.hi_flags
&& (r
< 0))
1153 /* Enable interrupt */
1154 r
= ioctl(fd
, HPET_IE_ON
, 0);
1158 enable_sigio_timer(fd
);
1159 t
->priv
= (void *)(long)fd
;
1167 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1169 int fd
= (long)t
->priv
;
1174 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1177 unsigned long current_rtc_freq
= 0;
1179 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1182 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1183 if (current_rtc_freq
!= RTC_FREQ
&&
1184 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1185 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1186 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1187 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1190 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1196 enable_sigio_timer(rtc_fd
);
1198 t
->priv
= (void *)(long)rtc_fd
;
1203 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1205 int rtc_fd
= (long)t
->priv
;
1210 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1214 struct sigaction act
;
1216 sigfillset(&act
.sa_mask
);
1218 act
.sa_handler
= host_alarm_handler
;
1220 sigaction(SIGALRM
, &act
, NULL
);
1223 * Initialize ev struct to 0 to avoid valgrind complaining
1224 * about uninitialized data in timer_create call
1226 memset(&ev
, 0, sizeof(ev
));
1227 ev
.sigev_value
.sival_int
= 0;
1228 ev
.sigev_notify
= SIGEV_SIGNAL
;
1229 ev
.sigev_signo
= SIGALRM
;
1231 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1232 perror("timer_create");
1234 /* disable dynticks */
1235 fprintf(stderr
, "Dynamic Ticks disabled\n");
1240 t
->priv
= (void *)(long)host_timer
;
1245 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1247 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1249 timer_delete(host_timer
);
1252 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1254 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1255 struct itimerspec timeout
;
1256 int64_t nearest_delta_us
= INT64_MAX
;
1259 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1260 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1261 !active_timers
[QEMU_CLOCK_HOST
])
1264 nearest_delta_us
= qemu_next_deadline_dyntick();
1266 /* check whether a timer is already running */
1267 if (timer_gettime(host_timer
, &timeout
)) {
1269 fprintf(stderr
, "Internal timer error: aborting\n");
1272 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1273 if (current_us
&& current_us
<= nearest_delta_us
)
1276 timeout
.it_interval
.tv_sec
= 0;
1277 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1278 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1279 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1280 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1282 fprintf(stderr
, "Internal timer error: aborting\n");
1287 #endif /* defined(__linux__) */
1289 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1291 struct sigaction act
;
1292 struct itimerval itv
;
1296 sigfillset(&act
.sa_mask
);
1298 act
.sa_handler
= host_alarm_handler
;
1300 sigaction(SIGALRM
, &act
, NULL
);
1302 itv
.it_interval
.tv_sec
= 0;
1303 /* for i386 kernel 2.6 to get 1 ms */
1304 itv
.it_interval
.tv_usec
= 999;
1305 itv
.it_value
.tv_sec
= 0;
1306 itv
.it_value
.tv_usec
= 10 * 1000;
1308 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1315 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1317 struct itimerval itv
;
1319 memset(&itv
, 0, sizeof(itv
));
1320 setitimer(ITIMER_REAL
, &itv
, NULL
);
1323 #endif /* !defined(_WIN32) */
1328 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1331 struct qemu_alarm_win32
*data
= t
->priv
;
1334 memset(&tc
, 0, sizeof(tc
));
1335 timeGetDevCaps(&tc
, sizeof(tc
));
1337 if (data
->period
< tc
.wPeriodMin
)
1338 data
->period
= tc
.wPeriodMin
;
1340 timeBeginPeriod(data
->period
);
1342 flags
= TIME_CALLBACK_FUNCTION
;
1343 if (alarm_has_dynticks(t
))
1344 flags
|= TIME_ONESHOT
;
1346 flags
|= TIME_PERIODIC
;
1348 data
->timerId
= timeSetEvent(1, // interval (ms)
1349 data
->period
, // resolution
1350 host_alarm_handler
, // function
1351 (DWORD
)t
, // parameter
1354 if (!data
->timerId
) {
1355 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1357 timeEndPeriod(data
->period
);
1364 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1366 struct qemu_alarm_win32
*data
= t
->priv
;
1368 timeKillEvent(data
->timerId
);
1369 timeEndPeriod(data
->period
);
1372 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1374 struct qemu_alarm_win32
*data
= t
->priv
;
1376 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1377 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1378 !active_timers
[QEMU_CLOCK_HOST
])
1381 timeKillEvent(data
->timerId
);
1383 data
->timerId
= timeSetEvent(1,
1387 TIME_ONESHOT
| TIME_PERIODIC
);
1389 if (!data
->timerId
) {
1390 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1393 timeEndPeriod(data
->period
);
1400 static int init_timer_alarm(void)
1402 struct qemu_alarm_timer
*t
= NULL
;
1405 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1406 t
= &alarm_timers
[i
];
1426 static void quit_timers(void)
1428 alarm_timer
->stop(alarm_timer
);
1432 /***********************************************************/
1433 /* host time/date access */
1434 void qemu_get_timedate(struct tm
*tm
, int offset
)
1441 if (rtc_date_offset
== -1) {
1445 ret
= localtime(&ti
);
1447 ti
-= rtc_date_offset
;
1451 memcpy(tm
, ret
, sizeof(struct tm
));
1454 int qemu_timedate_diff(struct tm
*tm
)
1458 if (rtc_date_offset
== -1)
1460 seconds
= mktimegm(tm
);
1462 seconds
= mktime(tm
);
1464 seconds
= mktimegm(tm
) + rtc_date_offset
;
1466 return seconds
- time(NULL
);
1469 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1471 time_t rtc_start_date
;
1474 if (!strcmp(startdate
, "now") && legacy
) {
1475 rtc_date_offset
= -1;
1477 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1485 } else if (sscanf(startdate
, "%d-%d-%d",
1488 &tm
.tm_mday
) == 3) {
1497 rtc_start_date
= mktimegm(&tm
);
1498 if (rtc_start_date
== -1) {
1500 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1501 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1504 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1508 static void configure_rtc(QemuOpts
*opts
)
1512 value
= qemu_opt_get(opts
, "base");
1514 if (!strcmp(value
, "utc")) {
1516 } else if (!strcmp(value
, "localtime")) {
1519 configure_rtc_date_offset(value
, 0);
1522 value
= qemu_opt_get(opts
, "clock");
1524 if (!strcmp(value
, "host")) {
1525 rtc_clock
= host_clock
;
1526 } else if (!strcmp(value
, "vm")) {
1527 rtc_clock
= vm_clock
;
1529 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1533 #ifdef CONFIG_TARGET_I386
1534 value
= qemu_opt_get(opts
, "driftfix");
1536 if (!strcmp(buf
, "slew")) {
1538 } else if (!strcmp(buf
, "none")) {
1541 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1549 static void socket_cleanup(void)
1554 static int socket_init(void)
1559 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1561 err
= WSAGetLastError();
1562 fprintf(stderr
, "WSAStartup: %d\n", err
);
1565 atexit(socket_cleanup
);
1570 /***********************************************************/
1571 /* Bluetooth support */
1574 static struct HCIInfo
*hci_table
[MAX_NICS
];
1576 static struct bt_vlan_s
{
1577 struct bt_scatternet_s net
;
1579 struct bt_vlan_s
*next
;
1582 /* find or alloc a new bluetooth "VLAN" */
1583 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1585 struct bt_vlan_s
**pvlan
, *vlan
;
1586 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1590 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1592 pvlan
= &first_bt_vlan
;
1593 while (*pvlan
!= NULL
)
1594 pvlan
= &(*pvlan
)->next
;
1599 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1603 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1608 static struct HCIInfo null_hci
= {
1609 .cmd_send
= null_hci_send
,
1610 .sco_send
= null_hci_send
,
1611 .acl_send
= null_hci_send
,
1612 .bdaddr_set
= null_hci_addr_set
,
1615 struct HCIInfo
*qemu_next_hci(void)
1617 if (cur_hci
== nb_hcis
)
1620 return hci_table
[cur_hci
++];
1623 static struct HCIInfo
*hci_init(const char *str
)
1626 struct bt_scatternet_s
*vlan
= 0;
1628 if (!strcmp(str
, "null"))
1631 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1633 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1634 else if (!strncmp(str
, "hci", 3)) {
1637 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1638 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1643 vlan
= qemu_find_bt_vlan(0);
1645 return bt_new_hci(vlan
);
1648 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1653 static int bt_hci_parse(const char *str
)
1655 struct HCIInfo
*hci
;
1658 if (nb_hcis
>= MAX_NICS
) {
1659 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1663 hci
= hci_init(str
);
1672 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1673 hci
->bdaddr_set(hci
, bdaddr
.b
);
1675 hci_table
[nb_hcis
++] = hci
;
1680 static void bt_vhci_add(int vlan_id
)
1682 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1685 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1686 "an empty scatternet %i\n", vlan_id
);
1688 bt_vhci_init(bt_new_hci(vlan
));
1691 static struct bt_device_s
*bt_device_add(const char *opt
)
1693 struct bt_scatternet_s
*vlan
;
1695 char *endp
= strstr(opt
, ",vlan=");
1696 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1699 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1702 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1704 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1709 vlan
= qemu_find_bt_vlan(vlan_id
);
1712 fprintf(stderr
, "qemu: warning: adding a slave device to "
1713 "an empty scatternet %i\n", vlan_id
);
1715 if (!strcmp(devname
, "keyboard"))
1716 return bt_keyboard_init(vlan
);
1718 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1722 static int bt_parse(const char *opt
)
1724 const char *endp
, *p
;
1727 if (strstart(opt
, "hci", &endp
)) {
1728 if (!*endp
|| *endp
== ',') {
1730 if (!strstart(endp
, ",vlan=", 0))
1733 return bt_hci_parse(opt
);
1735 } else if (strstart(opt
, "vhci", &endp
)) {
1736 if (!*endp
|| *endp
== ',') {
1738 if (strstart(endp
, ",vlan=", &p
)) {
1739 vlan
= strtol(p
, (char **) &endp
, 0);
1741 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1745 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1754 } else if (strstart(opt
, "device:", &endp
))
1755 return !bt_device_add(endp
);
1757 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1761 /***********************************************************/
1762 /* QEMU Block devices */
1764 #define HD_ALIAS "index=%d,media=disk"
1765 #define CDROM_ALIAS "index=2,media=cdrom"
1766 #define FD_ALIAS "index=%d,if=floppy"
1767 #define PFLASH_ALIAS "if=pflash"
1768 #define MTD_ALIAS "if=mtd"
1769 #define SD_ALIAS "index=0,if=sd"
1771 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1778 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1781 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1783 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1784 __FUNCTION__
, optstr
);
1788 qemu_opt_set(opts
, "file", file
);
1792 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1796 /* seek interface, bus and unit */
1798 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1799 if (dinfo
->type
== type
&&
1800 dinfo
->bus
== bus
&&
1801 dinfo
->unit
== unit
)
1808 DriveInfo
*drive_get_by_id(const char *id
)
1812 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1813 if (strcmp(id
, dinfo
->id
))
1820 int drive_get_max_bus(BlockInterfaceType type
)
1826 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1827 if(dinfo
->type
== type
&&
1828 dinfo
->bus
> max_bus
)
1829 max_bus
= dinfo
->bus
;
1834 const char *drive_get_serial(BlockDriverState
*bdrv
)
1838 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1839 if (dinfo
->bdrv
== bdrv
)
1840 return dinfo
->serial
;
1846 BlockInterfaceErrorAction
drive_get_on_error(
1847 BlockDriverState
*bdrv
, int is_read
)
1851 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1852 if (dinfo
->bdrv
== bdrv
)
1853 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1856 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1859 static void bdrv_format_print(void *opaque
, const char *name
)
1861 fprintf(stderr
, " %s", name
);
1864 void drive_uninit(DriveInfo
*dinfo
)
1866 qemu_opts_del(dinfo
->opts
);
1867 bdrv_delete(dinfo
->bdrv
);
1868 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1872 static int parse_block_error_action(const char *buf
, int is_read
)
1874 if (!strcmp(buf
, "ignore")) {
1875 return BLOCK_ERR_IGNORE
;
1876 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1877 return BLOCK_ERR_STOP_ENOSPC
;
1878 } else if (!strcmp(buf
, "stop")) {
1879 return BLOCK_ERR_STOP_ANY
;
1880 } else if (!strcmp(buf
, "report")) {
1881 return BLOCK_ERR_REPORT
;
1883 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1884 buf
, is_read
? "read" : "write");
1889 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1893 const char *file
= NULL
;
1896 const char *mediastr
= "";
1897 BlockInterfaceType type
;
1898 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1899 int bus_id
, unit_id
;
1900 int cyls
, heads
, secs
, translation
;
1901 BlockDriver
*drv
= NULL
;
1902 QEMUMachine
*machine
= opaque
;
1909 int on_read_error
, on_write_error
;
1910 const char *devaddr
;
1916 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1919 if (machine
&& machine
->use_scsi
) {
1921 max_devs
= MAX_SCSI_DEVS
;
1922 pstrcpy(devname
, sizeof(devname
), "scsi");
1925 max_devs
= MAX_IDE_DEVS
;
1926 pstrcpy(devname
, sizeof(devname
), "ide");
1930 /* extract parameters */
1931 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1932 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1933 index
= qemu_opt_get_number(opts
, "index", -1);
1935 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1936 heads
= qemu_opt_get_number(opts
, "heads", 0);
1937 secs
= qemu_opt_get_number(opts
, "secs", 0);
1939 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1940 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1942 file
= qemu_opt_get(opts
, "file");
1943 serial
= qemu_opt_get(opts
, "serial");
1945 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1946 pstrcpy(devname
, sizeof(devname
), buf
);
1947 if (!strcmp(buf
, "ide")) {
1949 max_devs
= MAX_IDE_DEVS
;
1950 } else if (!strcmp(buf
, "scsi")) {
1952 max_devs
= MAX_SCSI_DEVS
;
1953 } else if (!strcmp(buf
, "floppy")) {
1956 } else if (!strcmp(buf
, "pflash")) {
1959 } else if (!strcmp(buf
, "mtd")) {
1962 } else if (!strcmp(buf
, "sd")) {
1965 } else if (!strcmp(buf
, "virtio")) {
1968 } else if (!strcmp(buf
, "xen")) {
1971 } else if (!strcmp(buf
, "none")) {
1975 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1980 if (cyls
|| heads
|| secs
) {
1981 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
1982 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1985 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
1986 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1989 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1990 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1995 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1998 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2002 if (!strcmp(buf
, "none"))
2003 translation
= BIOS_ATA_TRANSLATION_NONE
;
2004 else if (!strcmp(buf
, "lba"))
2005 translation
= BIOS_ATA_TRANSLATION_LBA
;
2006 else if (!strcmp(buf
, "auto"))
2007 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2009 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2014 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2015 if (!strcmp(buf
, "disk")) {
2017 } else if (!strcmp(buf
, "cdrom")) {
2018 if (cyls
|| secs
|| heads
) {
2020 "qemu: '%s' invalid physical CHS format\n", buf
);
2023 media
= MEDIA_CDROM
;
2025 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2030 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2031 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2033 else if (!strcmp(buf
, "writethrough"))
2035 else if (!strcmp(buf
, "writeback"))
2038 fprintf(stderr
, "qemu: invalid cache option\n");
2043 #ifdef CONFIG_LINUX_AIO
2044 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2045 if (!strcmp(buf
, "threads"))
2047 else if (!strcmp(buf
, "native"))
2050 fprintf(stderr
, "qemu: invalid aio option\n");
2056 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2057 if (strcmp(buf
, "?") == 0) {
2058 fprintf(stderr
, "qemu: Supported formats:");
2059 bdrv_iterate_format(bdrv_format_print
, NULL
);
2060 fprintf(stderr
, "\n");
2063 drv
= bdrv_find_whitelisted_format(buf
);
2065 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2070 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2071 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2072 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2073 fprintf(stderr
, "werror is no supported by this format\n");
2077 on_write_error
= parse_block_error_action(buf
, 0);
2078 if (on_write_error
< 0) {
2083 on_read_error
= BLOCK_ERR_REPORT
;
2084 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2085 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2086 fprintf(stderr
, "rerror is no supported by this format\n");
2090 on_read_error
= parse_block_error_action(buf
, 1);
2091 if (on_read_error
< 0) {
2096 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2097 if (type
!= IF_VIRTIO
) {
2098 fprintf(stderr
, "addr is not supported\n");
2103 /* compute bus and unit according index */
2106 if (bus_id
!= 0 || unit_id
!= -1) {
2108 "qemu: index cannot be used with bus and unit\n");
2116 unit_id
= index
% max_devs
;
2117 bus_id
= index
/ max_devs
;
2121 /* if user doesn't specify a unit_id,
2122 * try to find the first free
2125 if (unit_id
== -1) {
2127 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2129 if (max_devs
&& unit_id
>= max_devs
) {
2130 unit_id
-= max_devs
;
2138 if (max_devs
&& unit_id
>= max_devs
) {
2139 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2140 unit_id
, max_devs
- 1);
2145 * ignore multiple definitions
2148 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2155 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2156 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2157 dinfo
->id
= qemu_strdup(buf
);
2159 /* no id supplied -> create one */
2160 dinfo
->id
= qemu_mallocz(32);
2161 if (type
== IF_IDE
|| type
== IF_SCSI
)
2162 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2164 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2165 devname
, bus_id
, mediastr
, unit_id
);
2167 snprintf(dinfo
->id
, 32, "%s%s%i",
2168 devname
, mediastr
, unit_id
);
2170 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2171 dinfo
->devaddr
= devaddr
;
2173 dinfo
->bus
= bus_id
;
2174 dinfo
->unit
= unit_id
;
2175 dinfo
->on_read_error
= on_read_error
;
2176 dinfo
->on_write_error
= on_write_error
;
2179 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2180 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2190 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2191 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2195 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2200 /* FIXME: This isn't really a floppy, but it's a reasonable
2203 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2209 /* add virtio block device */
2210 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2211 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2212 qemu_opt_set(opts
, "drive", dinfo
->id
);
2214 qemu_opt_set(opts
, "addr", devaddr
);
2225 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2226 cache
= 2; /* always use write-back with snapshot */
2228 if (cache
== 0) /* no caching */
2229 bdrv_flags
|= BDRV_O_NOCACHE
;
2230 else if (cache
== 2) /* write-back */
2231 bdrv_flags
|= BDRV_O_CACHE_WB
;
2234 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2236 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2240 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2241 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2246 * cdrom is read-only. Set it now, after above interface checking
2247 * since readonly attribute not explicitly required, so no error.
2249 if (media
== MEDIA_CDROM
) {
2252 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2254 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2255 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2256 file
, strerror(errno
));
2260 if (bdrv_key_required(dinfo
->bdrv
))
2266 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2268 QEMUMachine
*machine
= opaque
;
2269 int fatal_error
= 0;
2271 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2278 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2280 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2281 qemu_opt_set(opts
, "snapshot", "on");
2286 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2288 boot_set_handler
= func
;
2289 boot_set_opaque
= opaque
;
2292 int qemu_boot_set(const char *boot_devices
)
2294 if (!boot_set_handler
) {
2297 return boot_set_handler(boot_set_opaque
, boot_devices
);
2300 static int parse_bootdevices(char *devices
)
2302 /* We just do some generic consistency checks */
2306 for (p
= devices
; *p
!= '\0'; p
++) {
2307 /* Allowed boot devices are:
2308 * a-b: floppy disk drives
2309 * c-f: IDE disk drives
2310 * g-m: machine implementation dependant drives
2311 * n-p: network devices
2312 * It's up to each machine implementation to check if the given boot
2313 * devices match the actual hardware implementation and firmware
2316 if (*p
< 'a' || *p
> 'p') {
2317 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2320 if (bitmap
& (1 << (*p
- 'a'))) {
2321 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2324 bitmap
|= 1 << (*p
- 'a');
2329 static void restore_boot_devices(void *opaque
)
2331 char *standard_boot_devices
= opaque
;
2333 qemu_boot_set(standard_boot_devices
);
2335 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2336 qemu_free(standard_boot_devices
);
2339 static void numa_add(const char *optarg
)
2343 unsigned long long value
, endvalue
;
2346 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2347 if (!strcmp(option
, "node")) {
2348 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2349 nodenr
= nb_numa_nodes
;
2351 nodenr
= strtoull(option
, NULL
, 10);
2354 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2355 node_mem
[nodenr
] = 0;
2357 value
= strtoull(option
, &endptr
, 0);
2359 case 0: case 'M': case 'm':
2366 node_mem
[nodenr
] = value
;
2368 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2369 node_cpumask
[nodenr
] = 0;
2371 value
= strtoull(option
, &endptr
, 10);
2374 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2376 if (*endptr
== '-') {
2377 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2378 if (endvalue
>= 63) {
2381 "only 63 CPUs in NUMA mode supported.\n");
2383 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2388 node_cpumask
[nodenr
] = value
;
2395 static void smp_parse(const char *optarg
)
2397 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2401 smp
= strtoul(optarg
, &endptr
, 10);
2402 if (endptr
!= optarg
) {
2403 if (*endptr
== ',') {
2407 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2408 sockets
= strtoull(option
, NULL
, 10);
2409 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2410 cores
= strtoull(option
, NULL
, 10);
2411 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2412 threads
= strtoull(option
, NULL
, 10);
2413 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2414 max_cpus
= strtoull(option
, NULL
, 10);
2416 /* compute missing values, prefer sockets over cores over threads */
2417 if (smp
== 0 || sockets
== 0) {
2418 sockets
= sockets
> 0 ? sockets
: 1;
2419 cores
= cores
> 0 ? cores
: 1;
2420 threads
= threads
> 0 ? threads
: 1;
2422 smp
= cores
* threads
* sockets
;
2426 threads
= threads
> 0 ? threads
: 1;
2427 cores
= smp
/ (sockets
* threads
);
2430 threads
= smp
/ (cores
* sockets
);
2435 smp_cores
= cores
> 0 ? cores
: 1;
2436 smp_threads
= threads
> 0 ? threads
: 1;
2438 max_cpus
= smp_cpus
;
2441 /***********************************************************/
2444 static int usb_device_add(const char *devname
, int is_hotplug
)
2447 USBDevice
*dev
= NULL
;
2452 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2453 dev
= usbdevice_create(devname
);
2457 /* the other ones */
2458 if (strstart(devname
, "host:", &p
)) {
2459 dev
= usb_host_device_open(p
);
2460 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2461 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2462 bt_new_hci(qemu_find_bt_vlan(0)));
2473 static int usb_device_del(const char *devname
)
2478 if (strstart(devname
, "host:", &p
))
2479 return usb_host_device_close(p
);
2484 p
= strchr(devname
, '.');
2487 bus_num
= strtoul(devname
, NULL
, 0);
2488 addr
= strtoul(p
+ 1, NULL
, 0);
2490 return usb_device_delete_addr(bus_num
, addr
);
2493 static int usb_parse(const char *cmdline
)
2496 r
= usb_device_add(cmdline
, 0);
2498 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2503 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2505 const char *devname
= qdict_get_str(qdict
, "devname");
2506 if (usb_device_add(devname
, 1) < 0) {
2507 qemu_error("could not add USB device '%s'\n", devname
);
2511 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2513 const char *devname
= qdict_get_str(qdict
, "devname");
2514 if (usb_device_del(devname
) < 0) {
2515 qemu_error("could not delete USB device '%s'\n", devname
);
2519 /***********************************************************/
2520 /* PCMCIA/Cardbus */
2522 static struct pcmcia_socket_entry_s
{
2523 PCMCIASocket
*socket
;
2524 struct pcmcia_socket_entry_s
*next
;
2525 } *pcmcia_sockets
= 0;
2527 void pcmcia_socket_register(PCMCIASocket
*socket
)
2529 struct pcmcia_socket_entry_s
*entry
;
2531 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2532 entry
->socket
= socket
;
2533 entry
->next
= pcmcia_sockets
;
2534 pcmcia_sockets
= entry
;
2537 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2539 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2541 ptr
= &pcmcia_sockets
;
2542 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2543 if (entry
->socket
== socket
) {
2549 void pcmcia_info(Monitor
*mon
)
2551 struct pcmcia_socket_entry_s
*iter
;
2553 if (!pcmcia_sockets
)
2554 monitor_printf(mon
, "No PCMCIA sockets\n");
2556 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2557 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2558 iter
->socket
->attached
? iter
->socket
->card_string
:
2562 /***********************************************************/
2563 /* register display */
2565 struct DisplayAllocator default_allocator
= {
2566 defaultallocator_create_displaysurface
,
2567 defaultallocator_resize_displaysurface
,
2568 defaultallocator_free_displaysurface
2571 void register_displaystate(DisplayState
*ds
)
2581 DisplayState
*get_displaystate(void)
2583 return display_state
;
2586 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2588 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2589 return ds
->allocator
;
2594 static void dumb_display_init(void)
2596 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2597 ds
->allocator
= &default_allocator
;
2598 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2599 register_displaystate(ds
);
2602 /***********************************************************/
2605 typedef struct IOHandlerRecord
{
2607 IOCanRWHandler
*fd_read_poll
;
2609 IOHandler
*fd_write
;
2612 /* temporary data */
2614 struct IOHandlerRecord
*next
;
2617 static IOHandlerRecord
*first_io_handler
;
2619 /* XXX: fd_read_poll should be suppressed, but an API change is
2620 necessary in the character devices to suppress fd_can_read(). */
2621 int qemu_set_fd_handler2(int fd
,
2622 IOCanRWHandler
*fd_read_poll
,
2624 IOHandler
*fd_write
,
2627 IOHandlerRecord
**pioh
, *ioh
;
2629 if (!fd_read
&& !fd_write
) {
2630 pioh
= &first_io_handler
;
2635 if (ioh
->fd
== fd
) {
2642 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2646 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2647 ioh
->next
= first_io_handler
;
2648 first_io_handler
= ioh
;
2651 ioh
->fd_read_poll
= fd_read_poll
;
2652 ioh
->fd_read
= fd_read
;
2653 ioh
->fd_write
= fd_write
;
2654 ioh
->opaque
= opaque
;
2660 int qemu_set_fd_handler(int fd
,
2662 IOHandler
*fd_write
,
2665 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2669 /***********************************************************/
2670 /* Polling handling */
2672 typedef struct PollingEntry
{
2675 struct PollingEntry
*next
;
2678 static PollingEntry
*first_polling_entry
;
2680 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2682 PollingEntry
**ppe
, *pe
;
2683 pe
= qemu_mallocz(sizeof(PollingEntry
));
2685 pe
->opaque
= opaque
;
2686 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2691 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2693 PollingEntry
**ppe
, *pe
;
2694 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2696 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2704 /***********************************************************/
2705 /* Wait objects support */
2706 typedef struct WaitObjects
{
2708 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2709 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2710 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2713 static WaitObjects wait_objects
= {0};
2715 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2717 WaitObjects
*w
= &wait_objects
;
2719 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2721 w
->events
[w
->num
] = handle
;
2722 w
->func
[w
->num
] = func
;
2723 w
->opaque
[w
->num
] = opaque
;
2728 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2731 WaitObjects
*w
= &wait_objects
;
2734 for (i
= 0; i
< w
->num
; i
++) {
2735 if (w
->events
[i
] == handle
)
2738 w
->events
[i
] = w
->events
[i
+ 1];
2739 w
->func
[i
] = w
->func
[i
+ 1];
2740 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2748 /***********************************************************/
2749 /* ram save/restore */
2751 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2752 #define RAM_SAVE_FLAG_COMPRESS 0x02
2753 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2754 #define RAM_SAVE_FLAG_PAGE 0x08
2755 #define RAM_SAVE_FLAG_EOS 0x10
2757 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2759 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2760 uint32_t *array
= (uint32_t *)page
;
2763 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2764 if (array
[i
] != val
)
2771 static int ram_save_block(QEMUFile
*f
)
2773 static ram_addr_t current_addr
= 0;
2774 ram_addr_t saved_addr
= current_addr
;
2775 ram_addr_t addr
= 0;
2778 while (addr
< last_ram_offset
) {
2779 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2782 cpu_physical_memory_reset_dirty(current_addr
,
2783 current_addr
+ TARGET_PAGE_SIZE
,
2784 MIGRATION_DIRTY_FLAG
);
2786 p
= qemu_get_ram_ptr(current_addr
);
2788 if (is_dup_page(p
, *p
)) {
2789 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2790 qemu_put_byte(f
, *p
);
2792 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2793 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2799 addr
+= TARGET_PAGE_SIZE
;
2800 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2806 static uint64_t bytes_transferred
;
2808 static ram_addr_t
ram_save_remaining(void)
2811 ram_addr_t count
= 0;
2813 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2814 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2821 uint64_t ram_bytes_remaining(void)
2823 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2826 uint64_t ram_bytes_transferred(void)
2828 return bytes_transferred
;
2831 uint64_t ram_bytes_total(void)
2833 return last_ram_offset
;
2836 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2839 uint64_t bytes_transferred_last
;
2841 uint64_t expected_time
= 0;
2844 cpu_physical_memory_set_dirty_tracking(0);
2848 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2849 qemu_file_set_error(f
);
2854 bytes_transferred
= 0;
2856 /* Make sure all dirty bits are set */
2857 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2858 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2859 cpu_physical_memory_set_dirty(addr
);
2862 /* Enable dirty memory tracking */
2863 cpu_physical_memory_set_dirty_tracking(1);
2865 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2868 bytes_transferred_last
= bytes_transferred
;
2869 bwidth
= get_clock();
2871 while (!qemu_file_rate_limit(f
)) {
2874 ret
= ram_save_block(f
);
2875 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2876 if (ret
== 0) /* no more blocks */
2880 bwidth
= get_clock() - bwidth
;
2881 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2883 /* if we haven't transferred anything this round, force expected_time to a
2884 * a very high value, but without crashing */
2888 /* try transferring iterative blocks of memory */
2890 /* flush all remaining blocks regardless of rate limiting */
2891 while (ram_save_block(f
) != 0) {
2892 bytes_transferred
+= TARGET_PAGE_SIZE
;
2894 cpu_physical_memory_set_dirty_tracking(0);
2897 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2899 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2901 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2904 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2909 if (version_id
!= 3)
2913 addr
= qemu_get_be64(f
);
2915 flags
= addr
& ~TARGET_PAGE_MASK
;
2916 addr
&= TARGET_PAGE_MASK
;
2918 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2919 if (addr
!= last_ram_offset
)
2923 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2924 uint8_t ch
= qemu_get_byte(f
);
2925 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2928 (!kvm_enabled() || kvm_has_sync_mmu())) {
2929 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2932 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2933 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2935 if (qemu_file_has_error(f
)) {
2938 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2943 void qemu_service_io(void)
2945 qemu_notify_event();
2948 /***********************************************************/
2949 /* machine registration */
2951 static QEMUMachine
*first_machine
= NULL
;
2952 QEMUMachine
*current_machine
= NULL
;
2954 int qemu_register_machine(QEMUMachine
*m
)
2957 pm
= &first_machine
;
2965 static QEMUMachine
*find_machine(const char *name
)
2969 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2970 if (!strcmp(m
->name
, name
))
2972 if (m
->alias
&& !strcmp(m
->alias
, name
))
2978 static QEMUMachine
*find_default_machine(void)
2982 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2983 if (m
->is_default
) {
2990 /***********************************************************/
2991 /* main execution loop */
2993 static void gui_update(void *opaque
)
2995 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2996 DisplayState
*ds
= opaque
;
2997 DisplayChangeListener
*dcl
= ds
->listeners
;
2999 qemu_flush_coalesced_mmio_buffer();
3002 while (dcl
!= NULL
) {
3003 if (dcl
->gui_timer_interval
&&
3004 dcl
->gui_timer_interval
< interval
)
3005 interval
= dcl
->gui_timer_interval
;
3008 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3011 static void nographic_update(void *opaque
)
3013 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3015 qemu_flush_coalesced_mmio_buffer();
3016 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3019 struct vm_change_state_entry
{
3020 VMChangeStateHandler
*cb
;
3022 QLIST_ENTRY (vm_change_state_entry
) entries
;
3025 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3027 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3030 VMChangeStateEntry
*e
;
3032 e
= qemu_mallocz(sizeof (*e
));
3036 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3040 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3042 QLIST_REMOVE (e
, entries
);
3046 static void vm_state_notify(int running
, int reason
)
3048 VMChangeStateEntry
*e
;
3050 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3051 e
->cb(e
->opaque
, running
, reason
);
3055 static void resume_all_vcpus(void);
3056 static void pause_all_vcpus(void);
3063 vm_state_notify(1, 0);
3064 qemu_rearm_alarm_timer(alarm_timer
);
3069 /* reset/shutdown handler */
3071 typedef struct QEMUResetEntry
{
3072 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3073 QEMUResetHandler
*func
;
3077 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3078 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3079 static int reset_requested
;
3080 static int shutdown_requested
;
3081 static int powerdown_requested
;
3082 static int debug_requested
;
3083 static int vmstop_requested
;
3085 int qemu_shutdown_requested(void)
3087 int r
= shutdown_requested
;
3088 shutdown_requested
= 0;
3092 int qemu_reset_requested(void)
3094 int r
= reset_requested
;
3095 reset_requested
= 0;
3099 int qemu_powerdown_requested(void)
3101 int r
= powerdown_requested
;
3102 powerdown_requested
= 0;
3106 static int qemu_debug_requested(void)
3108 int r
= debug_requested
;
3109 debug_requested
= 0;
3113 static int qemu_vmstop_requested(void)
3115 int r
= vmstop_requested
;
3116 vmstop_requested
= 0;
3120 static void do_vm_stop(int reason
)
3123 cpu_disable_ticks();
3126 vm_state_notify(0, reason
);
3130 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3132 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3135 re
->opaque
= opaque
;
3136 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3139 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3143 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3144 if (re
->func
== func
&& re
->opaque
== opaque
) {
3145 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3152 void qemu_system_reset(void)
3154 QEMUResetEntry
*re
, *nre
;
3156 /* reset all devices */
3157 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3158 re
->func(re
->opaque
);
3162 void qemu_system_reset_request(void)
3165 shutdown_requested
= 1;
3167 reset_requested
= 1;
3169 qemu_notify_event();
3172 void qemu_system_shutdown_request(void)
3174 shutdown_requested
= 1;
3175 qemu_notify_event();
3178 void qemu_system_powerdown_request(void)
3180 powerdown_requested
= 1;
3181 qemu_notify_event();
3184 #ifdef CONFIG_IOTHREAD
3185 static void qemu_system_vmstop_request(int reason
)
3187 vmstop_requested
= reason
;
3188 qemu_notify_event();
3193 static int io_thread_fd
= -1;
3195 static void qemu_event_increment(void)
3197 static const char byte
= 0;
3200 if (io_thread_fd
== -1)
3203 ret
= write(io_thread_fd
, &byte
, sizeof(byte
));
3204 if (ret
< 0 && (errno
!= EINTR
&& errno
!= EAGAIN
)) {
3205 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3211 static void qemu_event_read(void *opaque
)
3213 int fd
= (unsigned long)opaque
;
3216 /* Drain the notify pipe */
3219 len
= read(fd
, buffer
, sizeof(buffer
));
3220 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3223 static int qemu_event_init(void)
3228 err
= qemu_pipe(fds
);
3232 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3236 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3240 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3241 (void *)(unsigned long)fds
[0]);
3243 io_thread_fd
= fds
[1];
3252 HANDLE qemu_event_handle
;
3254 static void dummy_event_handler(void *opaque
)
3258 static int qemu_event_init(void)
3260 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3261 if (!qemu_event_handle
) {
3262 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3265 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3269 static void qemu_event_increment(void)
3271 if (!SetEvent(qemu_event_handle
)) {
3272 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3279 static int cpu_can_run(CPUState
*env
)
3288 #ifndef CONFIG_IOTHREAD
3289 static int qemu_init_main_loop(void)
3291 return qemu_event_init();
3294 void qemu_init_vcpu(void *_env
)
3296 CPUState
*env
= _env
;
3298 env
->nr_cores
= smp_cores
;
3299 env
->nr_threads
= smp_threads
;
3305 int qemu_cpu_self(void *env
)
3310 static void resume_all_vcpus(void)
3314 static void pause_all_vcpus(void)
3318 void qemu_cpu_kick(void *env
)
3323 void qemu_notify_event(void)
3325 CPUState
*env
= cpu_single_env
;
3332 void qemu_mutex_lock_iothread(void) {}
3333 void qemu_mutex_unlock_iothread(void) {}
3335 void vm_stop(int reason
)
3340 #else /* CONFIG_IOTHREAD */
3342 #include "qemu-thread.h"
3344 QemuMutex qemu_global_mutex
;
3345 static QemuMutex qemu_fair_mutex
;
3347 static QemuThread io_thread
;
3349 static QemuThread
*tcg_cpu_thread
;
3350 static QemuCond
*tcg_halt_cond
;
3352 static int qemu_system_ready
;
3354 static QemuCond qemu_cpu_cond
;
3356 static QemuCond qemu_system_cond
;
3357 static QemuCond qemu_pause_cond
;
3359 static void block_io_signals(void);
3360 static void unblock_io_signals(void);
3361 static int tcg_has_work(void);
3363 static int qemu_init_main_loop(void)
3367 ret
= qemu_event_init();
3371 qemu_cond_init(&qemu_pause_cond
);
3372 qemu_mutex_init(&qemu_fair_mutex
);
3373 qemu_mutex_init(&qemu_global_mutex
);
3374 qemu_mutex_lock(&qemu_global_mutex
);
3376 unblock_io_signals();
3377 qemu_thread_self(&io_thread
);
3382 static void qemu_wait_io_event(CPUState
*env
)
3384 while (!tcg_has_work())
3385 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3387 qemu_mutex_unlock(&qemu_global_mutex
);
3390 * Users of qemu_global_mutex can be starved, having no chance
3391 * to acquire it since this path will get to it first.
3392 * So use another lock to provide fairness.
3394 qemu_mutex_lock(&qemu_fair_mutex
);
3395 qemu_mutex_unlock(&qemu_fair_mutex
);
3397 qemu_mutex_lock(&qemu_global_mutex
);
3401 qemu_cond_signal(&qemu_pause_cond
);
3405 static int qemu_cpu_exec(CPUState
*env
);
3407 static void *kvm_cpu_thread_fn(void *arg
)
3409 CPUState
*env
= arg
;
3412 qemu_thread_self(env
->thread
);
3416 /* signal CPU creation */
3417 qemu_mutex_lock(&qemu_global_mutex
);
3419 qemu_cond_signal(&qemu_cpu_cond
);
3421 /* and wait for machine initialization */
3422 while (!qemu_system_ready
)
3423 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3426 if (cpu_can_run(env
))
3428 qemu_wait_io_event(env
);
3434 static void tcg_cpu_exec(void);
3436 static void *tcg_cpu_thread_fn(void *arg
)
3438 CPUState
*env
= arg
;
3441 qemu_thread_self(env
->thread
);
3443 /* signal CPU creation */
3444 qemu_mutex_lock(&qemu_global_mutex
);
3445 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3447 qemu_cond_signal(&qemu_cpu_cond
);
3449 /* and wait for machine initialization */
3450 while (!qemu_system_ready
)
3451 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3455 qemu_wait_io_event(cur_cpu
);
3461 void qemu_cpu_kick(void *_env
)
3463 CPUState
*env
= _env
;
3464 qemu_cond_broadcast(env
->halt_cond
);
3466 qemu_thread_signal(env
->thread
, SIGUSR1
);
3469 int qemu_cpu_self(void *_env
)
3471 CPUState
*env
= _env
;
3474 qemu_thread_self(&this);
3476 return qemu_thread_equal(&this, env
->thread
);
3479 static void cpu_signal(int sig
)
3482 cpu_exit(cpu_single_env
);
3485 static void block_io_signals(void)
3488 struct sigaction sigact
;
3491 sigaddset(&set
, SIGUSR2
);
3492 sigaddset(&set
, SIGIO
);
3493 sigaddset(&set
, SIGALRM
);
3494 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3497 sigaddset(&set
, SIGUSR1
);
3498 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3500 memset(&sigact
, 0, sizeof(sigact
));
3501 sigact
.sa_handler
= cpu_signal
;
3502 sigaction(SIGUSR1
, &sigact
, NULL
);
3505 static void unblock_io_signals(void)
3510 sigaddset(&set
, SIGUSR2
);
3511 sigaddset(&set
, SIGIO
);
3512 sigaddset(&set
, SIGALRM
);
3513 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3516 sigaddset(&set
, SIGUSR1
);
3517 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3520 static void qemu_signal_lock(unsigned int msecs
)
3522 qemu_mutex_lock(&qemu_fair_mutex
);
3524 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3525 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3526 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3529 qemu_mutex_unlock(&qemu_fair_mutex
);
3532 void qemu_mutex_lock_iothread(void)
3534 if (kvm_enabled()) {
3535 qemu_mutex_lock(&qemu_fair_mutex
);
3536 qemu_mutex_lock(&qemu_global_mutex
);
3537 qemu_mutex_unlock(&qemu_fair_mutex
);
3539 qemu_signal_lock(100);
3542 void qemu_mutex_unlock_iothread(void)
3544 qemu_mutex_unlock(&qemu_global_mutex
);
3547 static int all_vcpus_paused(void)
3549 CPUState
*penv
= first_cpu
;
3554 penv
= (CPUState
*)penv
->next_cpu
;
3560 static void pause_all_vcpus(void)
3562 CPUState
*penv
= first_cpu
;
3566 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3567 qemu_cpu_kick(penv
);
3568 penv
= (CPUState
*)penv
->next_cpu
;
3571 while (!all_vcpus_paused()) {
3572 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3575 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3576 penv
= (CPUState
*)penv
->next_cpu
;
3581 static void resume_all_vcpus(void)
3583 CPUState
*penv
= first_cpu
;
3588 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3589 qemu_cpu_kick(penv
);
3590 penv
= (CPUState
*)penv
->next_cpu
;
3594 static void tcg_init_vcpu(void *_env
)
3596 CPUState
*env
= _env
;
3597 /* share a single thread for all cpus with TCG */
3598 if (!tcg_cpu_thread
) {
3599 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3600 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3601 qemu_cond_init(env
->halt_cond
);
3602 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3603 while (env
->created
== 0)
3604 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3605 tcg_cpu_thread
= env
->thread
;
3606 tcg_halt_cond
= env
->halt_cond
;
3608 env
->thread
= tcg_cpu_thread
;
3609 env
->halt_cond
= tcg_halt_cond
;
3613 static void kvm_start_vcpu(CPUState
*env
)
3615 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3616 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3617 qemu_cond_init(env
->halt_cond
);
3618 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3619 while (env
->created
== 0)
3620 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3623 void qemu_init_vcpu(void *_env
)
3625 CPUState
*env
= _env
;
3627 env
->nr_cores
= smp_cores
;
3628 env
->nr_threads
= smp_threads
;
3630 kvm_start_vcpu(env
);
3635 void qemu_notify_event(void)
3637 qemu_event_increment();
3640 void vm_stop(int reason
)
3643 qemu_thread_self(&me
);
3645 if (!qemu_thread_equal(&me
, &io_thread
)) {
3646 qemu_system_vmstop_request(reason
);
3648 * FIXME: should not return to device code in case
3649 * vm_stop() has been requested.
3651 if (cpu_single_env
) {
3652 cpu_exit(cpu_single_env
);
3653 cpu_single_env
->stop
= 1;
3664 static void host_main_loop_wait(int *timeout
)
3670 /* XXX: need to suppress polling by better using win32 events */
3672 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3673 ret
|= pe
->func(pe
->opaque
);
3677 WaitObjects
*w
= &wait_objects
;
3679 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3680 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3681 if (w
->func
[ret
- WAIT_OBJECT_0
])
3682 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3684 /* Check for additional signaled events */
3685 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3687 /* Check if event is signaled */
3688 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3689 if(ret2
== WAIT_OBJECT_0
) {
3691 w
->func
[i
](w
->opaque
[i
]);
3692 } else if (ret2
== WAIT_TIMEOUT
) {
3694 err
= GetLastError();
3695 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3698 } else if (ret
== WAIT_TIMEOUT
) {
3700 err
= GetLastError();
3701 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3708 static void host_main_loop_wait(int *timeout
)
3713 void main_loop_wait(int timeout
)
3715 IOHandlerRecord
*ioh
;
3716 fd_set rfds
, wfds
, xfds
;
3720 qemu_bh_update_timeout(&timeout
);
3722 host_main_loop_wait(&timeout
);
3724 /* poll any events */
3725 /* XXX: separate device handlers from system ones */
3730 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3734 (!ioh
->fd_read_poll
||
3735 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3736 FD_SET(ioh
->fd
, &rfds
);
3740 if (ioh
->fd_write
) {
3741 FD_SET(ioh
->fd
, &wfds
);
3747 tv
.tv_sec
= timeout
/ 1000;
3748 tv
.tv_usec
= (timeout
% 1000) * 1000;
3750 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3752 qemu_mutex_unlock_iothread();
3753 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3754 qemu_mutex_lock_iothread();
3756 IOHandlerRecord
**pioh
;
3758 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3759 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3760 ioh
->fd_read(ioh
->opaque
);
3762 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3763 ioh
->fd_write(ioh
->opaque
);
3767 /* remove deleted IO handlers */
3768 pioh
= &first_io_handler
;
3779 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3781 /* rearm timer, if not periodic */
3782 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3783 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3784 qemu_rearm_alarm_timer(alarm_timer
);
3787 /* vm time timers */
3789 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3790 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3791 qemu_get_clock(vm_clock
));
3794 /* real time timers */
3795 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3796 qemu_get_clock(rt_clock
));
3798 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3799 qemu_get_clock(host_clock
));
3801 /* Check bottom-halves last in case any of the earlier events triggered
3807 static int qemu_cpu_exec(CPUState
*env
)
3810 #ifdef CONFIG_PROFILER
3814 #ifdef CONFIG_PROFILER
3815 ti
= profile_getclock();
3820 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3821 env
->icount_decr
.u16
.low
= 0;
3822 env
->icount_extra
= 0;
3823 count
= qemu_next_deadline();
3824 count
= (count
+ (1 << icount_time_shift
) - 1)
3825 >> icount_time_shift
;
3826 qemu_icount
+= count
;
3827 decr
= (count
> 0xffff) ? 0xffff : count
;
3829 env
->icount_decr
.u16
.low
= decr
;
3830 env
->icount_extra
= count
;
3832 ret
= cpu_exec(env
);
3833 #ifdef CONFIG_PROFILER
3834 qemu_time
+= profile_getclock() - ti
;
3837 /* Fold pending instructions back into the
3838 instruction counter, and clear the interrupt flag. */
3839 qemu_icount
-= (env
->icount_decr
.u16
.low
3840 + env
->icount_extra
);
3841 env
->icount_decr
.u32
= 0;
3842 env
->icount_extra
= 0;
3847 static void tcg_cpu_exec(void)
3851 if (next_cpu
== NULL
)
3852 next_cpu
= first_cpu
;
3853 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3854 CPUState
*env
= cur_cpu
= next_cpu
;
3858 if (timer_alarm_pending
) {
3859 timer_alarm_pending
= 0;
3862 if (cpu_can_run(env
))
3863 ret
= qemu_cpu_exec(env
);
3864 if (ret
== EXCP_DEBUG
) {
3865 gdb_set_stop_cpu(env
);
3866 debug_requested
= 1;
3872 static int cpu_has_work(CPUState
*env
)
3880 if (qemu_cpu_has_work(env
))
3885 static int tcg_has_work(void)
3889 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3890 if (cpu_has_work(env
))
3895 static int qemu_calculate_timeout(void)
3897 #ifndef CONFIG_IOTHREAD
3902 else if (tcg_has_work())
3904 else if (!use_icount
)
3907 /* XXX: use timeout computed from timers */
3910 /* Advance virtual time to the next event. */
3911 if (use_icount
== 1) {
3912 /* When not using an adaptive execution frequency
3913 we tend to get badly out of sync with real time,
3914 so just delay for a reasonable amount of time. */
3917 delta
= cpu_get_icount() - cpu_get_clock();
3920 /* If virtual time is ahead of real time then just
3922 timeout
= (delta
/ 1000000) + 1;
3924 /* Wait for either IO to occur or the next
3926 add
= qemu_next_deadline();
3927 /* We advance the timer before checking for IO.
3928 Limit the amount we advance so that early IO
3929 activity won't get the guest too far ahead. */
3933 add
= (add
+ (1 << icount_time_shift
) - 1)
3934 >> icount_time_shift
;
3936 timeout
= delta
/ 1000000;
3943 #else /* CONFIG_IOTHREAD */
3948 static int vm_can_run(void)
3950 if (powerdown_requested
)
3952 if (reset_requested
)
3954 if (shutdown_requested
)
3956 if (debug_requested
)
3961 qemu_irq qemu_system_powerdown
;
3963 static void main_loop(void)
3967 #ifdef CONFIG_IOTHREAD
3968 qemu_system_ready
= 1;
3969 qemu_cond_broadcast(&qemu_system_cond
);
3974 #ifdef CONFIG_PROFILER
3977 #ifndef CONFIG_IOTHREAD
3980 #ifdef CONFIG_PROFILER
3981 ti
= profile_getclock();
3983 main_loop_wait(qemu_calculate_timeout());
3984 #ifdef CONFIG_PROFILER
3985 dev_time
+= profile_getclock() - ti
;
3987 } while (vm_can_run());
3989 if (qemu_debug_requested()) {
3990 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
3991 vm_stop(EXCP_DEBUG
);
3993 if (qemu_shutdown_requested()) {
3994 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4001 if (qemu_reset_requested()) {
4002 monitor_protocol_event(QEVENT_RESET
, NULL
);
4004 qemu_system_reset();
4007 if (qemu_powerdown_requested()) {
4008 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4009 qemu_irq_raise(qemu_system_powerdown
);
4011 if ((r
= qemu_vmstop_requested())) {
4012 monitor_protocol_event(QEVENT_STOP
, NULL
);
4019 static void version(void)
4021 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4024 static void help(int exitcode
)
4027 printf("usage: %s [options] [disk_image]\n"
4029 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4031 #define DEF(option, opt_arg, opt_enum, opt_help) \
4033 #define DEFHEADING(text) stringify(text) "\n"
4034 #include "qemu-options.h"
4039 "During emulation, the following keys are useful:\n"
4040 "ctrl-alt-f toggle full screen\n"
4041 "ctrl-alt-n switch to virtual console 'n'\n"
4042 "ctrl-alt toggle mouse and keyboard grab\n"
4044 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4049 DEFAULT_NETWORK_SCRIPT
,
4050 DEFAULT_NETWORK_DOWN_SCRIPT
,
4052 DEFAULT_GDBSTUB_PORT
,
4057 #define HAS_ARG 0x0001
4060 #define DEF(option, opt_arg, opt_enum, opt_help) \
4062 #define DEFHEADING(text)
4063 #include "qemu-options.h"
4069 typedef struct QEMUOption
{
4075 static const QEMUOption qemu_options
[] = {
4076 { "h", 0, QEMU_OPTION_h
},
4077 #define DEF(option, opt_arg, opt_enum, opt_help) \
4078 { option, opt_arg, opt_enum },
4079 #define DEFHEADING(text)
4080 #include "qemu-options.h"
4088 struct soundhw soundhw
[] = {
4089 #ifdef HAS_AUDIO_CHOICE
4090 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4096 { .init_isa
= pcspk_audio_init
}
4103 "Creative Sound Blaster 16",
4106 { .init_isa
= SB16_init
}
4110 #ifdef CONFIG_CS4231A
4116 { .init_isa
= cs4231a_init
}
4124 "Yamaha YMF262 (OPL3)",
4126 "Yamaha YM3812 (OPL2)",
4130 { .init_isa
= Adlib_init
}
4137 "Gravis Ultrasound GF1",
4140 { .init_isa
= GUS_init
}
4147 "Intel 82801AA AC97 Audio",
4150 { .init_pci
= ac97_init
}
4154 #ifdef CONFIG_ES1370
4157 "ENSONIQ AudioPCI ES1370",
4160 { .init_pci
= es1370_init
}
4164 #endif /* HAS_AUDIO_CHOICE */
4166 { NULL
, NULL
, 0, 0, { NULL
} }
4169 static void select_soundhw (const char *optarg
)
4173 if (*optarg
== '?') {
4176 printf ("Valid sound card names (comma separated):\n");
4177 for (c
= soundhw
; c
->name
; ++c
) {
4178 printf ("%-11s %s\n", c
->name
, c
->descr
);
4180 printf ("\n-soundhw all will enable all of the above\n");
4181 exit (*optarg
!= '?');
4189 if (!strcmp (optarg
, "all")) {
4190 for (c
= soundhw
; c
->name
; ++c
) {
4198 e
= strchr (p
, ',');
4199 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4201 for (c
= soundhw
; c
->name
; ++c
) {
4202 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4211 "Unknown sound card name (too big to show)\n");
4214 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4219 p
+= l
+ (e
!= NULL
);
4223 goto show_valid_cards
;
4228 static void select_vgahw (const char *p
)
4233 vga_interface_type
= VGA_NONE
;
4234 if (strstart(p
, "std", &opts
)) {
4235 vga_interface_type
= VGA_STD
;
4236 } else if (strstart(p
, "cirrus", &opts
)) {
4237 vga_interface_type
= VGA_CIRRUS
;
4238 } else if (strstart(p
, "vmware", &opts
)) {
4239 vga_interface_type
= VGA_VMWARE
;
4240 } else if (strstart(p
, "xenfb", &opts
)) {
4241 vga_interface_type
= VGA_XENFB
;
4242 } else if (!strstart(p
, "none", &opts
)) {
4244 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4248 const char *nextopt
;
4250 if (strstart(opts
, ",retrace=", &nextopt
)) {
4252 if (strstart(opts
, "dumb", &nextopt
))
4253 vga_retrace_method
= VGA_RETRACE_DUMB
;
4254 else if (strstart(opts
, "precise", &nextopt
))
4255 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4256 else goto invalid_vga
;
4257 } else goto invalid_vga
;
4263 static int balloon_parse(const char *arg
)
4267 if (strcmp(arg
, "none") == 0) {
4271 if (!strncmp(arg
, "virtio", 6)) {
4272 if (arg
[6] == ',') {
4273 /* have params -> parse them */
4274 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4278 /* create empty opts */
4279 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4281 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4290 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4292 exit(STATUS_CONTROL_C_EXIT
);
4297 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4301 if(strlen(str
) != 36)
4304 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4305 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4306 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4312 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4320 static void termsig_handler(int signal
)
4322 qemu_system_shutdown_request();
4325 static void sigchld_handler(int signal
)
4327 waitpid(-1, NULL
, WNOHANG
);
4330 static void sighandler_setup(void)
4332 struct sigaction act
;
4334 memset(&act
, 0, sizeof(act
));
4335 act
.sa_handler
= termsig_handler
;
4336 sigaction(SIGINT
, &act
, NULL
);
4337 sigaction(SIGHUP
, &act
, NULL
);
4338 sigaction(SIGTERM
, &act
, NULL
);
4340 act
.sa_handler
= sigchld_handler
;
4341 act
.sa_flags
= SA_NOCLDSTOP
;
4342 sigaction(SIGCHLD
, &act
, NULL
);
4348 /* Look for support files in the same directory as the executable. */
4349 static char *find_datadir(const char *argv0
)
4355 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4362 while (p
!= buf
&& *p
!= '\\')
4365 if (access(buf
, R_OK
) == 0) {
4366 return qemu_strdup(buf
);
4372 /* Find a likely location for support files using the location of the binary.
4373 For installed binaries this will be "$bindir/../share/qemu". When
4374 running from the build tree this will be "$bindir/../pc-bios". */
4375 #define SHARE_SUFFIX "/share/qemu"
4376 #define BUILD_SUFFIX "/pc-bios"
4377 static char *find_datadir(const char *argv0
)
4385 #if defined(__linux__)
4388 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4394 #elif defined(__FreeBSD__)
4397 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4404 /* If we don't have any way of figuring out the actual executable
4405 location then try argv[0]. */
4407 p
= realpath(argv0
, buf
);
4415 max_len
= strlen(dir
) +
4416 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4417 res
= qemu_mallocz(max_len
);
4418 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4419 if (access(res
, R_OK
)) {
4420 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4421 if (access(res
, R_OK
)) {
4433 char *qemu_find_file(int type
, const char *name
)
4439 /* If name contains path separators then try it as a straight path. */
4440 if ((strchr(name
, '/') || strchr(name
, '\\'))
4441 && access(name
, R_OK
) == 0) {
4442 return qemu_strdup(name
);
4445 case QEMU_FILE_TYPE_BIOS
:
4448 case QEMU_FILE_TYPE_KEYMAP
:
4449 subdir
= "keymaps/";
4454 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4455 buf
= qemu_mallocz(len
);
4456 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4457 if (access(buf
, R_OK
)) {
4464 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4466 return qdev_device_help(opts
);
4469 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4473 dev
= qdev_device_add(opts
);
4479 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4481 CharDriverState
*chr
;
4483 chr
= qemu_chr_open_opts(opts
, NULL
);
4489 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4491 CharDriverState
*chr
;
4492 const char *chardev
;
4496 mode
= qemu_opt_get(opts
, "mode");
4500 if (strcmp(mode
, "readline") == 0) {
4501 flags
= MONITOR_USE_READLINE
;
4502 } else if (strcmp(mode
, "control") == 0) {
4503 flags
= MONITOR_USE_CONTROL
;
4505 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4509 if (qemu_opt_get_bool(opts
, "default", 0))
4510 flags
|= MONITOR_IS_DEFAULT
;
4512 chardev
= qemu_opt_get(opts
, "chardev");
4513 chr
= qemu_chr_find(chardev
);
4515 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4519 monitor_init(chr
, flags
);
4523 static void monitor_parse(const char *optarg
, const char *mode
)
4525 static int monitor_device_index
= 0;
4531 if (strstart(optarg
, "chardev:", &p
)) {
4532 snprintf(label
, sizeof(label
), "%s", p
);
4534 if (monitor_device_index
) {
4535 snprintf(label
, sizeof(label
), "monitor%d",
4536 monitor_device_index
);
4538 snprintf(label
, sizeof(label
), "monitor");
4541 opts
= qemu_chr_parse_compat(label
, optarg
);
4543 fprintf(stderr
, "parse error: %s\n", optarg
);
4548 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4550 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4553 qemu_opt_set(opts
, "mode", mode
);
4554 qemu_opt_set(opts
, "chardev", label
);
4556 qemu_opt_set(opts
, "default", "on");
4557 monitor_device_index
++;
4560 struct device_config
{
4562 DEV_USB
, /* -usbdevice */
4564 DEV_SERIAL
, /* -serial */
4565 DEV_PARALLEL
, /* -parallel */
4566 DEV_VIRTCON
, /* -virtioconsole */
4567 DEV_DEBUGCON
, /* -debugcon */
4569 const char *cmdline
;
4570 QTAILQ_ENTRY(device_config
) next
;
4572 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4574 static void add_device_config(int type
, const char *cmdline
)
4576 struct device_config
*conf
;
4578 conf
= qemu_mallocz(sizeof(*conf
));
4580 conf
->cmdline
= cmdline
;
4581 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4584 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4586 struct device_config
*conf
;
4589 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4590 if (conf
->type
!= type
)
4592 rc
= func(conf
->cmdline
);
4599 static int serial_parse(const char *devname
)
4601 static int index
= 0;
4604 if (strcmp(devname
, "none") == 0)
4606 if (index
== MAX_SERIAL_PORTS
) {
4607 fprintf(stderr
, "qemu: too many serial ports\n");
4610 snprintf(label
, sizeof(label
), "serial%d", index
);
4611 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4612 if (!serial_hds
[index
]) {
4613 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4614 devname
, strerror(errno
));
4621 static int parallel_parse(const char *devname
)
4623 static int index
= 0;
4626 if (strcmp(devname
, "none") == 0)
4628 if (index
== MAX_PARALLEL_PORTS
) {
4629 fprintf(stderr
, "qemu: too many parallel ports\n");
4632 snprintf(label
, sizeof(label
), "parallel%d", index
);
4633 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4634 if (!parallel_hds
[index
]) {
4635 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4636 devname
, strerror(errno
));
4643 static int virtcon_parse(const char *devname
)
4645 static int index
= 0;
4647 QemuOpts
*bus_opts
, *dev_opts
;
4649 if (strcmp(devname
, "none") == 0)
4651 if (index
== MAX_VIRTIO_CONSOLES
) {
4652 fprintf(stderr
, "qemu: too many virtio consoles\n");
4656 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4657 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4659 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4660 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4662 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4663 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4664 if (!virtcon_hds
[index
]) {
4665 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4666 devname
, strerror(errno
));
4669 qemu_opt_set(dev_opts
, "chardev", label
);
4675 static int debugcon_parse(const char *devname
)
4679 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4682 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4684 fprintf(stderr
, "qemu: already have a debugcon device\n");
4687 qemu_opt_set(opts
, "driver", "isa-debugcon");
4688 qemu_opt_set(opts
, "chardev", "debugcon");
4692 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4693 const char **poptarg
, int *poptind
)
4695 const QEMUOption
*popt
;
4696 int optind
= *poptind
;
4697 char *r
= argv
[optind
];
4701 /* Treat --foo the same as -foo. */
4704 popt
= qemu_options
;
4707 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4711 if (!strcmp(popt
->name
, r
+ 1))
4715 if (popt
->flags
& HAS_ARG
) {
4716 if (optind
>= argc
) {
4717 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4721 optarg
= argv
[optind
++];
4732 int main(int argc
, char **argv
, char **envp
)
4734 const char *gdbstub_dev
= NULL
;
4735 uint32_t boot_devices_bitmap
= 0;
4737 int snapshot
, linux_boot
, net_boot
;
4738 const char *initrd_filename
;
4739 const char *kernel_filename
, *kernel_cmdline
;
4740 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4742 DisplayChangeListener
*dcl
;
4743 int cyls
, heads
, secs
, translation
;
4744 QemuOpts
*hda_opts
= NULL
, *opts
;
4747 const char *loadvm
= NULL
;
4748 QEMUMachine
*machine
;
4749 const char *cpu_model
;
4754 const char *pid_file
= NULL
;
4755 const char *incoming
= NULL
;
4758 struct passwd
*pwd
= NULL
;
4759 const char *chroot_dir
= NULL
;
4760 const char *run_as
= NULL
;
4763 int show_vnc_port
= 0;
4768 qemu_errors_to_file(stderr
);
4769 qemu_cache_utils_init(envp
);
4771 QLIST_INIT (&vm_change_state_head
);
4774 struct sigaction act
;
4775 sigfillset(&act
.sa_mask
);
4777 act
.sa_handler
= SIG_IGN
;
4778 sigaction(SIGPIPE
, &act
, NULL
);
4781 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4782 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4783 QEMU to run on a single CPU */
4788 h
= GetCurrentProcess();
4789 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4790 for(i
= 0; i
< 32; i
++) {
4791 if (mask
& (1 << i
))
4796 SetProcessAffinityMask(h
, mask
);
4802 module_call_init(MODULE_INIT_MACHINE
);
4803 machine
= find_default_machine();
4805 initrd_filename
= NULL
;
4808 kernel_filename
= NULL
;
4809 kernel_cmdline
= "";
4810 cyls
= heads
= secs
= 0;
4811 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4813 for (i
= 0; i
< MAX_NODES
; i
++) {
4815 node_cpumask
[i
] = 0;
4824 /* first pass of option parsing */
4826 while (optind
< argc
) {
4827 if (argv
[optind
][0] != '-') {
4832 const QEMUOption
*popt
;
4834 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4835 switch (popt
->index
) {
4836 case QEMU_OPTION_nodefconfig
:
4845 fp
= fopen(CONFIG_QEMU_CONFDIR
"/qemu.conf", "r");
4847 if (qemu_config_parse(fp
) != 0) {
4853 fp
= fopen(CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", "r");
4855 if (qemu_config_parse(fp
) != 0) {
4862 /* second pass of option parsing */
4867 if (argv
[optind
][0] != '-') {
4868 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4870 const QEMUOption
*popt
;
4872 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4873 switch(popt
->index
) {
4875 machine
= find_machine(optarg
);
4878 printf("Supported machines are:\n");
4879 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4881 printf("%-10s %s (alias of %s)\n",
4882 m
->alias
, m
->desc
, m
->name
);
4883 printf("%-10s %s%s\n",
4885 m
->is_default
? " (default)" : "");
4887 exit(*optarg
!= '?');
4890 case QEMU_OPTION_cpu
:
4891 /* hw initialization will check this */
4892 if (*optarg
== '?') {
4893 /* XXX: implement xxx_cpu_list for targets that still miss it */
4894 #if defined(cpu_list)
4895 cpu_list(stdout
, &fprintf
);
4902 case QEMU_OPTION_initrd
:
4903 initrd_filename
= optarg
;
4905 case QEMU_OPTION_hda
:
4907 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4909 hda_opts
= drive_add(optarg
, HD_ALIAS
4910 ",cyls=%d,heads=%d,secs=%d%s",
4911 0, cyls
, heads
, secs
,
4912 translation
== BIOS_ATA_TRANSLATION_LBA
?
4914 translation
== BIOS_ATA_TRANSLATION_NONE
?
4915 ",trans=none" : "");
4917 case QEMU_OPTION_hdb
:
4918 case QEMU_OPTION_hdc
:
4919 case QEMU_OPTION_hdd
:
4920 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4922 case QEMU_OPTION_drive
:
4923 drive_add(NULL
, "%s", optarg
);
4925 case QEMU_OPTION_set
:
4926 if (qemu_set_option(optarg
) != 0)
4929 case QEMU_OPTION_global
:
4930 if (qemu_global_option(optarg
) != 0)
4933 case QEMU_OPTION_mtdblock
:
4934 drive_add(optarg
, MTD_ALIAS
);
4936 case QEMU_OPTION_sd
:
4937 drive_add(optarg
, SD_ALIAS
);
4939 case QEMU_OPTION_pflash
:
4940 drive_add(optarg
, PFLASH_ALIAS
);
4942 case QEMU_OPTION_snapshot
:
4945 case QEMU_OPTION_hdachs
:
4949 cyls
= strtol(p
, (char **)&p
, 0);
4950 if (cyls
< 1 || cyls
> 16383)
4955 heads
= strtol(p
, (char **)&p
, 0);
4956 if (heads
< 1 || heads
> 16)
4961 secs
= strtol(p
, (char **)&p
, 0);
4962 if (secs
< 1 || secs
> 63)
4966 if (!strcmp(p
, "none"))
4967 translation
= BIOS_ATA_TRANSLATION_NONE
;
4968 else if (!strcmp(p
, "lba"))
4969 translation
= BIOS_ATA_TRANSLATION_LBA
;
4970 else if (!strcmp(p
, "auto"))
4971 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4974 } else if (*p
!= '\0') {
4976 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4979 if (hda_opts
!= NULL
) {
4981 snprintf(num
, sizeof(num
), "%d", cyls
);
4982 qemu_opt_set(hda_opts
, "cyls", num
);
4983 snprintf(num
, sizeof(num
), "%d", heads
);
4984 qemu_opt_set(hda_opts
, "heads", num
);
4985 snprintf(num
, sizeof(num
), "%d", secs
);
4986 qemu_opt_set(hda_opts
, "secs", num
);
4987 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4988 qemu_opt_set(hda_opts
, "trans", "lba");
4989 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4990 qemu_opt_set(hda_opts
, "trans", "none");
4994 case QEMU_OPTION_numa
:
4995 if (nb_numa_nodes
>= MAX_NODES
) {
4996 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5001 case QEMU_OPTION_nographic
:
5002 display_type
= DT_NOGRAPHIC
;
5004 #ifdef CONFIG_CURSES
5005 case QEMU_OPTION_curses
:
5006 display_type
= DT_CURSES
;
5009 case QEMU_OPTION_portrait
:
5012 case QEMU_OPTION_kernel
:
5013 kernel_filename
= optarg
;
5015 case QEMU_OPTION_append
:
5016 kernel_cmdline
= optarg
;
5018 case QEMU_OPTION_cdrom
:
5019 drive_add(optarg
, CDROM_ALIAS
);
5021 case QEMU_OPTION_boot
:
5023 static const char * const params
[] = {
5024 "order", "once", "menu", NULL
5026 char buf
[sizeof(boot_devices
)];
5027 char *standard_boot_devices
;
5030 if (!strchr(optarg
, '=')) {
5032 pstrcpy(buf
, sizeof(buf
), optarg
);
5033 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5035 "qemu: unknown boot parameter '%s' in '%s'\n",
5041 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5042 boot_devices_bitmap
= parse_bootdevices(buf
);
5043 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5046 if (get_param_value(buf
, sizeof(buf
),
5048 boot_devices_bitmap
|= parse_bootdevices(buf
);
5049 standard_boot_devices
= qemu_strdup(boot_devices
);
5050 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5051 qemu_register_reset(restore_boot_devices
,
5052 standard_boot_devices
);
5054 if (get_param_value(buf
, sizeof(buf
),
5056 if (!strcmp(buf
, "on")) {
5058 } else if (!strcmp(buf
, "off")) {
5062 "qemu: invalid option value '%s'\n",
5070 case QEMU_OPTION_fda
:
5071 case QEMU_OPTION_fdb
:
5072 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5075 case QEMU_OPTION_no_fd_bootchk
:
5079 case QEMU_OPTION_netdev
:
5080 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5084 case QEMU_OPTION_net
:
5085 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5090 case QEMU_OPTION_tftp
:
5091 legacy_tftp_prefix
= optarg
;
5093 case QEMU_OPTION_bootp
:
5094 legacy_bootp_filename
= optarg
;
5097 case QEMU_OPTION_smb
:
5098 if (net_slirp_smb(optarg
) < 0)
5102 case QEMU_OPTION_redir
:
5103 if (net_slirp_redir(optarg
) < 0)
5107 case QEMU_OPTION_bt
:
5108 add_device_config(DEV_BT
, optarg
);
5111 case QEMU_OPTION_audio_help
:
5115 case QEMU_OPTION_soundhw
:
5116 select_soundhw (optarg
);
5122 case QEMU_OPTION_version
:
5126 case QEMU_OPTION_m
: {
5130 value
= strtoul(optarg
, &ptr
, 10);
5132 case 0: case 'M': case 'm':
5139 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5143 /* On 32-bit hosts, QEMU is limited by virtual address space */
5144 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5145 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5148 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5149 fprintf(stderr
, "qemu: ram size too large\n");
5158 const CPULogItem
*item
;
5160 mask
= cpu_str_to_log_mask(optarg
);
5162 printf("Log items (comma separated):\n");
5163 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5164 printf("%-10s %s\n", item
->name
, item
->help
);
5172 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5174 case QEMU_OPTION_gdb
:
5175 gdbstub_dev
= optarg
;
5180 case QEMU_OPTION_bios
:
5183 case QEMU_OPTION_singlestep
:
5190 keyboard_layout
= optarg
;
5192 case QEMU_OPTION_localtime
:
5195 case QEMU_OPTION_vga
:
5196 select_vgahw (optarg
);
5198 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5204 w
= strtol(p
, (char **)&p
, 10);
5207 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5213 h
= strtol(p
, (char **)&p
, 10);
5218 depth
= strtol(p
, (char **)&p
, 10);
5219 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5220 depth
!= 24 && depth
!= 32)
5222 } else if (*p
== '\0') {
5223 depth
= graphic_depth
;
5230 graphic_depth
= depth
;
5234 case QEMU_OPTION_echr
:
5237 term_escape_char
= strtol(optarg
, &r
, 0);
5239 printf("Bad argument to echr\n");
5242 case QEMU_OPTION_monitor
:
5243 monitor_parse(optarg
, "readline");
5244 default_monitor
= 0;
5246 case QEMU_OPTION_qmp
:
5247 monitor_parse(optarg
, "control");
5248 default_monitor
= 0;
5250 case QEMU_OPTION_mon
:
5251 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5253 fprintf(stderr
, "parse error: %s\n", optarg
);
5256 default_monitor
= 0;
5258 case QEMU_OPTION_chardev
:
5259 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5261 fprintf(stderr
, "parse error: %s\n", optarg
);
5265 case QEMU_OPTION_serial
:
5266 add_device_config(DEV_SERIAL
, optarg
);
5269 case QEMU_OPTION_watchdog
:
5272 "qemu: only one watchdog option may be given\n");
5277 case QEMU_OPTION_watchdog_action
:
5278 if (select_watchdog_action(optarg
) == -1) {
5279 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5283 case QEMU_OPTION_virtiocon
:
5284 add_device_config(DEV_VIRTCON
, optarg
);
5285 default_virtcon
= 0;
5287 case QEMU_OPTION_parallel
:
5288 add_device_config(DEV_PARALLEL
, optarg
);
5289 default_parallel
= 0;
5291 case QEMU_OPTION_debugcon
:
5292 add_device_config(DEV_DEBUGCON
, optarg
);
5294 case QEMU_OPTION_loadvm
:
5297 case QEMU_OPTION_full_screen
:
5301 case QEMU_OPTION_no_frame
:
5304 case QEMU_OPTION_alt_grab
:
5307 case QEMU_OPTION_ctrl_grab
:
5310 case QEMU_OPTION_no_quit
:
5313 case QEMU_OPTION_sdl
:
5314 display_type
= DT_SDL
;
5317 case QEMU_OPTION_pidfile
:
5321 case QEMU_OPTION_win2k_hack
:
5322 win2k_install_hack
= 1;
5324 case QEMU_OPTION_rtc_td_hack
:
5327 case QEMU_OPTION_acpitable
:
5328 if(acpi_table_add(optarg
) < 0) {
5329 fprintf(stderr
, "Wrong acpi table provided\n");
5333 case QEMU_OPTION_smbios
:
5334 if(smbios_entry_add(optarg
) < 0) {
5335 fprintf(stderr
, "Wrong smbios provided\n");
5341 case QEMU_OPTION_enable_kvm
:
5345 case QEMU_OPTION_usb
:
5348 case QEMU_OPTION_usbdevice
:
5350 add_device_config(DEV_USB
, optarg
);
5352 case QEMU_OPTION_device
:
5353 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5357 case QEMU_OPTION_smp
:
5360 fprintf(stderr
, "Invalid number of CPUs\n");
5363 if (max_cpus
< smp_cpus
) {
5364 fprintf(stderr
, "maxcpus must be equal to or greater than "
5368 if (max_cpus
> 255) {
5369 fprintf(stderr
, "Unsupported number of maxcpus\n");
5373 case QEMU_OPTION_vnc
:
5374 display_type
= DT_VNC
;
5375 vnc_display
= optarg
;
5378 case QEMU_OPTION_no_acpi
:
5381 case QEMU_OPTION_no_hpet
:
5384 case QEMU_OPTION_balloon
:
5385 if (balloon_parse(optarg
) < 0) {
5386 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5391 case QEMU_OPTION_no_reboot
:
5394 case QEMU_OPTION_no_shutdown
:
5397 case QEMU_OPTION_show_cursor
:
5400 case QEMU_OPTION_uuid
:
5401 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5402 fprintf(stderr
, "Fail to parse UUID string."
5403 " Wrong format.\n");
5408 case QEMU_OPTION_daemonize
:
5412 case QEMU_OPTION_option_rom
:
5413 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5414 fprintf(stderr
, "Too many option ROMs\n");
5417 option_rom
[nb_option_roms
] = optarg
;
5420 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5421 case QEMU_OPTION_semihosting
:
5422 semihosting_enabled
= 1;
5425 case QEMU_OPTION_name
:
5426 qemu_name
= qemu_strdup(optarg
);
5428 char *p
= strchr(qemu_name
, ',');
5431 if (strncmp(p
, "process=", 8)) {
5432 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5440 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5441 case QEMU_OPTION_prom_env
:
5442 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5443 fprintf(stderr
, "Too many prom variables\n");
5446 prom_envs
[nb_prom_envs
] = optarg
;
5451 case QEMU_OPTION_old_param
:
5455 case QEMU_OPTION_clock
:
5456 configure_alarms(optarg
);
5458 case QEMU_OPTION_startdate
:
5459 configure_rtc_date_offset(optarg
, 1);
5461 case QEMU_OPTION_rtc
:
5462 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5464 fprintf(stderr
, "parse error: %s\n", optarg
);
5467 configure_rtc(opts
);
5469 case QEMU_OPTION_tb_size
:
5470 tb_size
= strtol(optarg
, NULL
, 0);
5474 case QEMU_OPTION_icount
:
5476 if (strcmp(optarg
, "auto") == 0) {
5477 icount_time_shift
= -1;
5479 icount_time_shift
= strtol(optarg
, NULL
, 0);
5482 case QEMU_OPTION_incoming
:
5485 case QEMU_OPTION_nodefaults
:
5487 default_parallel
= 0;
5488 default_virtcon
= 0;
5489 default_monitor
= 0;
5497 case QEMU_OPTION_chroot
:
5498 chroot_dir
= optarg
;
5500 case QEMU_OPTION_runas
:
5505 case QEMU_OPTION_xen_domid
:
5506 xen_domid
= atoi(optarg
);
5508 case QEMU_OPTION_xen_create
:
5509 xen_mode
= XEN_CREATE
;
5511 case QEMU_OPTION_xen_attach
:
5512 xen_mode
= XEN_ATTACH
;
5515 case QEMU_OPTION_readconfig
:
5518 fp
= fopen(optarg
, "r");
5520 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5523 if (qemu_config_parse(fp
) != 0) {
5529 case QEMU_OPTION_writeconfig
:
5532 if (strcmp(optarg
, "-") == 0) {
5535 fp
= fopen(optarg
, "w");
5537 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5541 qemu_config_write(fp
);
5549 /* If no data_dir is specified then try to find it relative to the
5552 data_dir
= find_datadir(argv
[0]);
5554 /* If all else fails use the install patch specified when building. */
5556 data_dir
= CONFIG_QEMU_SHAREDIR
;
5560 * Default to max_cpus = smp_cpus, in case the user doesn't
5561 * specify a max_cpus value.
5564 max_cpus
= smp_cpus
;
5566 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5567 if (smp_cpus
> machine
->max_cpus
) {
5568 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5569 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5574 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5575 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5577 if (machine
->no_serial
) {
5580 if (machine
->no_parallel
) {
5581 default_parallel
= 0;
5583 if (!machine
->use_virtcon
) {
5584 default_virtcon
= 0;
5586 if (machine
->no_vga
) {
5589 if (machine
->no_floppy
) {
5592 if (machine
->no_cdrom
) {
5595 if (machine
->no_sdcard
) {
5599 if (display_type
== DT_NOGRAPHIC
) {
5600 if (default_parallel
)
5601 add_device_config(DEV_PARALLEL
, "null");
5602 if (default_serial
&& default_monitor
) {
5603 add_device_config(DEV_SERIAL
, "mon:stdio");
5604 } else if (default_virtcon
&& default_monitor
) {
5605 add_device_config(DEV_VIRTCON
, "mon:stdio");
5608 add_device_config(DEV_SERIAL
, "stdio");
5609 if (default_virtcon
)
5610 add_device_config(DEV_VIRTCON
, "stdio");
5611 if (default_monitor
)
5612 monitor_parse("stdio", "readline");
5616 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5617 if (default_parallel
)
5618 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5619 if (default_monitor
)
5620 monitor_parse("vc:80Cx24C", "readline");
5621 if (default_virtcon
)
5622 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5625 vga_interface_type
= VGA_CIRRUS
;
5627 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5634 if (pipe(fds
) == -1)
5645 len
= read(fds
[0], &status
, 1);
5646 if (len
== -1 && (errno
== EINTR
))
5651 else if (status
== 1) {
5652 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5660 qemu_set_cloexec(fds
[1]);
5672 signal(SIGTSTP
, SIG_IGN
);
5673 signal(SIGTTOU
, SIG_IGN
);
5674 signal(SIGTTIN
, SIG_IGN
);
5678 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5682 if (write(fds
[1], &status
, 1) != 1) {
5683 perror("daemonize. Writing to pipe\n");
5687 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5691 if (kvm_enabled()) {
5694 ret
= kvm_init(smp_cpus
);
5696 fprintf(stderr
, "failed to initialize KVM\n");
5701 if (qemu_init_main_loop()) {
5702 fprintf(stderr
, "qemu_init_main_loop failed\n");
5705 linux_boot
= (kernel_filename
!= NULL
);
5707 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5708 fprintf(stderr
, "-append only allowed with -kernel option\n");
5712 if (!linux_boot
&& initrd_filename
!= NULL
) {
5713 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5718 /* Win32 doesn't support line-buffering and requires size >= 2 */
5719 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5722 if (init_timer_alarm() < 0) {
5723 fprintf(stderr
, "could not initialize alarm timer\n");
5726 if (use_icount
&& icount_time_shift
< 0) {
5728 /* 125MIPS seems a reasonable initial guess at the guest speed.
5729 It will be corrected fairly quickly anyway. */
5730 icount_time_shift
= 3;
5731 init_icount_adjust();
5738 if (net_init_clients() < 0) {
5742 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5743 net_set_boot_mask(net_boot
);
5745 /* init the bluetooth world */
5746 if (foreach_device_config(DEV_BT
, bt_parse
))
5749 /* init the memory */
5751 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5753 /* init the dynamic translator */
5754 cpu_exec_init_all(tb_size
* 1024 * 1024);
5756 bdrv_init_with_whitelist();
5760 if (default_cdrom
) {
5761 /* we always create the cdrom drive, even if no disk is there */
5762 drive_add(NULL
, CDROM_ALIAS
);
5765 if (default_floppy
) {
5766 /* we always create at least one floppy */
5767 drive_add(NULL
, FD_ALIAS
, 0);
5770 if (default_sdcard
) {
5771 /* we always create one sd slot, even if no card is in it */
5772 drive_add(NULL
, SD_ALIAS
);
5775 /* open the virtual block devices */
5777 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5778 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5781 vmstate_register(0, &vmstate_timers
,&timers_state
);
5782 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5785 if (nb_numa_nodes
> 0) {
5788 if (nb_numa_nodes
> smp_cpus
) {
5789 nb_numa_nodes
= smp_cpus
;
5792 /* If no memory size if given for any node, assume the default case
5793 * and distribute the available memory equally across all nodes
5795 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5796 if (node_mem
[i
] != 0)
5799 if (i
== nb_numa_nodes
) {
5800 uint64_t usedmem
= 0;
5802 /* On Linux, the each node's border has to be 8MB aligned,
5803 * the final node gets the rest.
5805 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5806 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5807 usedmem
+= node_mem
[i
];
5809 node_mem
[i
] = ram_size
- usedmem
;
5812 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5813 if (node_cpumask
[i
] != 0)
5816 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5817 * must cope with this anyway, because there are BIOSes out there in
5818 * real machines which also use this scheme.
5820 if (i
== nb_numa_nodes
) {
5821 for (i
= 0; i
< smp_cpus
; i
++) {
5822 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5827 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5829 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5831 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5833 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5836 module_call_init(MODULE_INIT_DEVICE
);
5838 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5842 i
= select_watchdog(watchdog
);
5844 exit (i
== 1 ? 1 : 0);
5847 if (machine
->compat_props
) {
5848 qdev_prop_register_global_list(machine
->compat_props
);
5852 machine
->init(ram_size
, boot_devices
,
5853 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5857 /* must be after terminal init, SDL library changes signal handlers */
5861 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5862 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5863 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5869 current_machine
= machine
;
5871 /* init USB devices */
5873 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5877 /* init generic devices */
5878 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5882 dumb_display_init();
5883 /* just use the first displaystate for the moment */
5886 if (display_type
== DT_DEFAULT
) {
5887 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5888 display_type
= DT_SDL
;
5890 display_type
= DT_VNC
;
5891 vnc_display
= "localhost:0,to=99";
5897 switch (display_type
) {
5900 #if defined(CONFIG_CURSES)
5902 curses_display_init(ds
, full_screen
);
5905 #if defined(CONFIG_SDL)
5907 sdl_display_init(ds
, full_screen
, no_frame
);
5909 #elif defined(CONFIG_COCOA)
5911 cocoa_display_init(ds
, full_screen
);
5915 vnc_display_init(ds
);
5916 if (vnc_display_open(ds
, vnc_display
) < 0)
5919 if (show_vnc_port
) {
5920 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5928 dcl
= ds
->listeners
;
5929 while (dcl
!= NULL
) {
5930 if (dcl
->dpy_refresh
!= NULL
) {
5931 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5932 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5937 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5938 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5939 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5942 text_consoles_set_display(display_state
);
5944 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5947 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5948 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5953 qdev_machine_creation_done();
5955 if (rom_load_all() != 0) {
5956 fprintf(stderr
, "rom loading failed\n");
5960 qemu_system_reset();
5962 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5968 qemu_start_incoming_migration(incoming
);
5969 } else if (autostart
) {
5979 len
= write(fds
[1], &status
, 1);
5980 if (len
== -1 && (errno
== EINTR
))
5987 perror("not able to chdir to /");
5990 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5996 pwd
= getpwnam(run_as
);
5998 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6004 if (chroot(chroot_dir
) < 0) {
6005 fprintf(stderr
, "chroot failed\n");
6009 perror("not able to chdir to /");
6015 if (setgid(pwd
->pw_gid
) < 0) {
6016 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6019 if (setuid(pwd
->pw_uid
) < 0) {
6020 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6023 if (setuid(0) != -1) {
6024 fprintf(stderr
, "Dropping privileges failed\n");