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-console-pci", .flag
= &default_virtcon
},
292 { .driver
= "virtio-console-s390", .flag
= &default_virtcon
},
293 { .driver
= "VGA", .flag
= &default_vga
},
294 { .driver
= "cirrus-vga", .flag
= &default_vga
},
295 { .driver
= "vmware-svga", .flag
= &default_vga
},
298 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
300 const char *driver
= qemu_opt_get(opts
, "driver");
305 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
306 if (strcmp(default_list
[i
].driver
, driver
) != 0)
308 *(default_list
[i
].flag
) = 0;
313 /***********************************************************/
314 /* x86 ISA bus support */
316 target_phys_addr_t isa_mem_base
= 0;
319 /***********************************************************/
320 void hw_error(const char *fmt
, ...)
326 fprintf(stderr
, "qemu: hardware error: ");
327 vfprintf(stderr
, fmt
, ap
);
328 fprintf(stderr
, "\n");
329 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
330 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
332 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
334 cpu_dump_state(env
, stderr
, fprintf
, 0);
341 static void set_proc_name(const char *s
)
343 #if defined(__linux__) && defined(PR_SET_NAME)
347 name
[sizeof(name
) - 1] = 0;
348 strncpy(name
, s
, sizeof(name
));
349 /* Could rewrite argv[0] too, but that's a bit more complicated.
350 This simple way is enough for `top'. */
351 prctl(PR_SET_NAME
, name
);
358 static QEMUBalloonEvent
*qemu_balloon_event
;
359 void *qemu_balloon_event_opaque
;
361 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
363 qemu_balloon_event
= func
;
364 qemu_balloon_event_opaque
= opaque
;
367 void qemu_balloon(ram_addr_t target
)
369 if (qemu_balloon_event
)
370 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
373 ram_addr_t
qemu_balloon_status(void)
375 if (qemu_balloon_event
)
376 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
381 /***********************************************************/
382 /* real time host monotonic timer */
384 /* compute with 96 bit intermediate result: (a*b)/c */
385 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
390 #ifdef HOST_WORDS_BIGENDIAN
400 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
401 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
404 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
408 static int64_t get_clock_realtime(void)
412 gettimeofday(&tv
, NULL
);
413 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
418 static int64_t clock_freq
;
420 static void init_get_clock(void)
424 ret
= QueryPerformanceFrequency(&freq
);
426 fprintf(stderr
, "Could not calibrate ticks\n");
429 clock_freq
= freq
.QuadPart
;
432 static int64_t get_clock(void)
435 QueryPerformanceCounter(&ti
);
436 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
441 static int use_rt_clock
;
443 static void init_get_clock(void)
446 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
447 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
450 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
457 static int64_t get_clock(void)
459 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
460 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
463 clock_gettime(CLOCK_MONOTONIC
, &ts
);
464 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
468 /* XXX: using gettimeofday leads to problems if the date
469 changes, so it should be avoided. */
470 return get_clock_realtime();
475 /* Return the virtual CPU time, based on the instruction counter. */
476 static int64_t cpu_get_icount(void)
479 CPUState
*env
= cpu_single_env
;;
480 icount
= qemu_icount
;
483 fprintf(stderr
, "Bad clock read\n");
484 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
486 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
489 /***********************************************************/
490 /* guest cycle counter */
492 typedef struct TimersState
{
493 int64_t cpu_ticks_prev
;
494 int64_t cpu_ticks_offset
;
495 int64_t cpu_clock_offset
;
496 int32_t cpu_ticks_enabled
;
500 TimersState timers_state
;
502 /* return the host CPU cycle counter and handle stop/restart */
503 int64_t cpu_get_ticks(void)
506 return cpu_get_icount();
508 if (!timers_state
.cpu_ticks_enabled
) {
509 return timers_state
.cpu_ticks_offset
;
512 ticks
= cpu_get_real_ticks();
513 if (timers_state
.cpu_ticks_prev
> ticks
) {
514 /* Note: non increasing ticks may happen if the host uses
516 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
518 timers_state
.cpu_ticks_prev
= ticks
;
519 return ticks
+ timers_state
.cpu_ticks_offset
;
523 /* return the host CPU monotonic timer and handle stop/restart */
524 static int64_t cpu_get_clock(void)
527 if (!timers_state
.cpu_ticks_enabled
) {
528 return timers_state
.cpu_clock_offset
;
531 return ti
+ timers_state
.cpu_clock_offset
;
535 /* enable cpu_get_ticks() */
536 void cpu_enable_ticks(void)
538 if (!timers_state
.cpu_ticks_enabled
) {
539 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
540 timers_state
.cpu_clock_offset
-= get_clock();
541 timers_state
.cpu_ticks_enabled
= 1;
545 /* disable cpu_get_ticks() : the clock is stopped. You must not call
546 cpu_get_ticks() after that. */
547 void cpu_disable_ticks(void)
549 if (timers_state
.cpu_ticks_enabled
) {
550 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
551 timers_state
.cpu_clock_offset
= cpu_get_clock();
552 timers_state
.cpu_ticks_enabled
= 0;
556 /***********************************************************/
559 #define QEMU_CLOCK_REALTIME 0
560 #define QEMU_CLOCK_VIRTUAL 1
561 #define QEMU_CLOCK_HOST 2
565 /* XXX: add frequency */
573 struct QEMUTimer
*next
;
576 struct qemu_alarm_timer
{
580 int (*start
)(struct qemu_alarm_timer
*t
);
581 void (*stop
)(struct qemu_alarm_timer
*t
);
582 void (*rearm
)(struct qemu_alarm_timer
*t
);
586 #define ALARM_FLAG_DYNTICKS 0x1
587 #define ALARM_FLAG_EXPIRED 0x2
589 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
591 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
594 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
596 if (!alarm_has_dynticks(t
))
602 /* TODO: MIN_TIMER_REARM_US should be optimized */
603 #define MIN_TIMER_REARM_US 250
605 static struct qemu_alarm_timer
*alarm_timer
;
609 struct qemu_alarm_win32
{
612 } alarm_win32_data
= {0, -1};
614 static int win32_start_timer(struct qemu_alarm_timer
*t
);
615 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
616 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
620 static int unix_start_timer(struct qemu_alarm_timer
*t
);
621 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
625 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
626 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
627 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
629 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
630 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
632 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
633 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
635 #endif /* __linux__ */
639 /* Correlation between real and virtual time is always going to be
640 fairly approximate, so ignore small variation.
641 When the guest is idle real and virtual time will be aligned in
643 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
645 static void icount_adjust(void)
650 static int64_t last_delta
;
651 /* If the VM is not running, then do nothing. */
655 cur_time
= cpu_get_clock();
656 cur_icount
= qemu_get_clock(vm_clock
);
657 delta
= cur_icount
- cur_time
;
658 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
660 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
661 && icount_time_shift
> 0) {
662 /* The guest is getting too far ahead. Slow time down. */
666 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
667 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
668 /* The guest is getting too far behind. Speed time up. */
672 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
675 static void icount_adjust_rt(void * opaque
)
677 qemu_mod_timer(icount_rt_timer
,
678 qemu_get_clock(rt_clock
) + 1000);
682 static void icount_adjust_vm(void * opaque
)
684 qemu_mod_timer(icount_vm_timer
,
685 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
689 static void init_icount_adjust(void)
691 /* Have both realtime and virtual time triggers for speed adjustment.
692 The realtime trigger catches emulated time passing too slowly,
693 the virtual time trigger catches emulated time passing too fast.
694 Realtime triggers occur even when idle, so use them less frequently
696 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
697 qemu_mod_timer(icount_rt_timer
,
698 qemu_get_clock(rt_clock
) + 1000);
699 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
700 qemu_mod_timer(icount_vm_timer
,
701 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
704 static struct qemu_alarm_timer alarm_timers
[] = {
707 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
708 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
709 /* HPET - if available - is preferred */
710 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
711 /* ...otherwise try RTC */
712 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
714 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
716 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
717 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
718 {"win32", 0, win32_start_timer
,
719 win32_stop_timer
, NULL
, &alarm_win32_data
},
724 static void show_available_alarms(void)
728 printf("Available alarm timers, in order of precedence:\n");
729 for (i
= 0; alarm_timers
[i
].name
; i
++)
730 printf("%s\n", alarm_timers
[i
].name
);
733 static void configure_alarms(char const *opt
)
737 int count
= ARRAY_SIZE(alarm_timers
) - 1;
740 struct qemu_alarm_timer tmp
;
742 if (!strcmp(opt
, "?")) {
743 show_available_alarms();
747 arg
= qemu_strdup(opt
);
749 /* Reorder the array */
750 name
= strtok(arg
, ",");
752 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
753 if (!strcmp(alarm_timers
[i
].name
, name
))
758 fprintf(stderr
, "Unknown clock %s\n", name
);
767 tmp
= alarm_timers
[i
];
768 alarm_timers
[i
] = alarm_timers
[cur
];
769 alarm_timers
[cur
] = tmp
;
773 name
= strtok(NULL
, ",");
779 /* Disable remaining timers */
780 for (i
= cur
; i
< count
; i
++)
781 alarm_timers
[i
].name
= NULL
;
783 show_available_alarms();
788 #define QEMU_NUM_CLOCKS 3
792 QEMUClock
*host_clock
;
794 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
796 static QEMUClock
*qemu_new_clock(int type
)
799 clock
= qemu_mallocz(sizeof(QEMUClock
));
804 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
808 ts
= qemu_mallocz(sizeof(QEMUTimer
));
815 void qemu_free_timer(QEMUTimer
*ts
)
820 /* stop a timer, but do not dealloc it */
821 void qemu_del_timer(QEMUTimer
*ts
)
825 /* NOTE: this code must be signal safe because
826 qemu_timer_expired() can be called from a signal. */
827 pt
= &active_timers
[ts
->clock
->type
];
840 /* modify the current timer so that it will be fired when current_time
841 >= expire_time. The corresponding callback will be called. */
842 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
848 /* add the timer in the sorted list */
849 /* NOTE: this code must be signal safe because
850 qemu_timer_expired() can be called from a signal. */
851 pt
= &active_timers
[ts
->clock
->type
];
856 if (t
->expire_time
> expire_time
)
860 ts
->expire_time
= expire_time
;
864 /* Rearm if necessary */
865 if (pt
== &active_timers
[ts
->clock
->type
]) {
866 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
867 qemu_rearm_alarm_timer(alarm_timer
);
869 /* Interrupt execution to force deadline recalculation. */
875 int qemu_timer_pending(QEMUTimer
*ts
)
878 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
885 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
889 return (timer_head
->expire_time
<= current_time
);
892 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
898 if (!ts
|| ts
->expire_time
> current_time
)
900 /* remove timer from the list before calling the callback */
901 *ptimer_head
= ts
->next
;
904 /* run the callback (the timer list can be modified) */
909 int64_t qemu_get_clock(QEMUClock
*clock
)
911 switch(clock
->type
) {
912 case QEMU_CLOCK_REALTIME
:
913 return get_clock() / 1000000;
915 case QEMU_CLOCK_VIRTUAL
:
917 return cpu_get_icount();
919 return cpu_get_clock();
921 case QEMU_CLOCK_HOST
:
922 return get_clock_realtime();
926 static void init_clocks(void)
929 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
930 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
931 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
933 rtc_clock
= host_clock
;
937 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
939 uint64_t expire_time
;
941 if (qemu_timer_pending(ts
)) {
942 expire_time
= ts
->expire_time
;
946 qemu_put_be64(f
, expire_time
);
949 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
951 uint64_t expire_time
;
953 expire_time
= qemu_get_be64(f
);
954 if (expire_time
!= -1) {
955 qemu_mod_timer(ts
, expire_time
);
961 static const VMStateDescription vmstate_timers
= {
964 .minimum_version_id
= 1,
965 .minimum_version_id_old
= 1,
966 .fields
= (VMStateField
[]) {
967 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
968 VMSTATE_INT64(dummy
, TimersState
),
969 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
970 VMSTATE_END_OF_LIST()
974 static void qemu_event_increment(void);
977 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
978 DWORD_PTR dwUser
, DWORD_PTR dw1
,
981 static void host_alarm_handler(int host_signum
)
985 #define DISP_FREQ 1000
987 static int64_t delta_min
= INT64_MAX
;
988 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
990 ti
= qemu_get_clock(vm_clock
);
991 if (last_clock
!= 0) {
992 delta
= ti
- last_clock
;
993 if (delta
< delta_min
)
995 if (delta
> delta_max
)
998 if (++count
== DISP_FREQ
) {
999 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1000 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1001 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1002 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1003 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1005 delta_min
= INT64_MAX
;
1013 if (alarm_has_dynticks(alarm_timer
) ||
1015 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1016 qemu_get_clock(vm_clock
))) ||
1017 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1018 qemu_get_clock(rt_clock
)) ||
1019 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1020 qemu_get_clock(host_clock
))) {
1021 qemu_event_increment();
1022 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1024 #ifndef CONFIG_IOTHREAD
1026 /* stop the currently executing cpu because a timer occured */
1030 timer_alarm_pending
= 1;
1031 qemu_notify_event();
1035 static int64_t qemu_next_deadline(void)
1037 /* To avoid problems with overflow limit this to 2^32. */
1038 int64_t delta
= INT32_MAX
;
1040 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1041 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1042 qemu_get_clock(vm_clock
);
1044 if (active_timers
[QEMU_CLOCK_HOST
]) {
1045 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1046 qemu_get_clock(host_clock
);
1057 #if defined(__linux__)
1058 static uint64_t qemu_next_deadline_dyntick(void)
1066 delta
= (qemu_next_deadline() + 999) / 1000;
1068 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1069 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1070 qemu_get_clock(rt_clock
))*1000;
1071 if (rtdelta
< delta
)
1075 if (delta
< MIN_TIMER_REARM_US
)
1076 delta
= MIN_TIMER_REARM_US
;
1084 /* Sets a specific flag */
1085 static int fcntl_setfl(int fd
, int flag
)
1089 flags
= fcntl(fd
, F_GETFL
);
1093 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1099 #if defined(__linux__)
1101 #define RTC_FREQ 1024
1103 static void enable_sigio_timer(int fd
)
1105 struct sigaction act
;
1108 sigfillset(&act
.sa_mask
);
1110 act
.sa_handler
= host_alarm_handler
;
1112 sigaction(SIGIO
, &act
, NULL
);
1113 fcntl_setfl(fd
, O_ASYNC
);
1114 fcntl(fd
, F_SETOWN
, getpid());
1117 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1119 struct hpet_info info
;
1122 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1127 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1129 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1130 "error, but for better emulation accuracy type:\n"
1131 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1135 /* Check capabilities */
1136 r
= ioctl(fd
, HPET_INFO
, &info
);
1140 /* Enable periodic mode */
1141 r
= ioctl(fd
, HPET_EPI
, 0);
1142 if (info
.hi_flags
&& (r
< 0))
1145 /* Enable interrupt */
1146 r
= ioctl(fd
, HPET_IE_ON
, 0);
1150 enable_sigio_timer(fd
);
1151 t
->priv
= (void *)(long)fd
;
1159 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1161 int fd
= (long)t
->priv
;
1166 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1169 unsigned long current_rtc_freq
= 0;
1171 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1174 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1175 if (current_rtc_freq
!= RTC_FREQ
&&
1176 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1177 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1178 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1179 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1182 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1188 enable_sigio_timer(rtc_fd
);
1190 t
->priv
= (void *)(long)rtc_fd
;
1195 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1197 int rtc_fd
= (long)t
->priv
;
1202 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1206 struct sigaction act
;
1208 sigfillset(&act
.sa_mask
);
1210 act
.sa_handler
= host_alarm_handler
;
1212 sigaction(SIGALRM
, &act
, NULL
);
1215 * Initialize ev struct to 0 to avoid valgrind complaining
1216 * about uninitialized data in timer_create call
1218 memset(&ev
, 0, sizeof(ev
));
1219 ev
.sigev_value
.sival_int
= 0;
1220 ev
.sigev_notify
= SIGEV_SIGNAL
;
1221 ev
.sigev_signo
= SIGALRM
;
1223 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1224 perror("timer_create");
1226 /* disable dynticks */
1227 fprintf(stderr
, "Dynamic Ticks disabled\n");
1232 t
->priv
= (void *)(long)host_timer
;
1237 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1239 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1241 timer_delete(host_timer
);
1244 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1246 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1247 struct itimerspec timeout
;
1248 int64_t nearest_delta_us
= INT64_MAX
;
1251 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1252 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1253 !active_timers
[QEMU_CLOCK_HOST
])
1256 nearest_delta_us
= qemu_next_deadline_dyntick();
1258 /* check whether a timer is already running */
1259 if (timer_gettime(host_timer
, &timeout
)) {
1261 fprintf(stderr
, "Internal timer error: aborting\n");
1264 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1265 if (current_us
&& current_us
<= nearest_delta_us
)
1268 timeout
.it_interval
.tv_sec
= 0;
1269 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1270 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1271 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1272 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1274 fprintf(stderr
, "Internal timer error: aborting\n");
1279 #endif /* defined(__linux__) */
1281 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1283 struct sigaction act
;
1284 struct itimerval itv
;
1288 sigfillset(&act
.sa_mask
);
1290 act
.sa_handler
= host_alarm_handler
;
1292 sigaction(SIGALRM
, &act
, NULL
);
1294 itv
.it_interval
.tv_sec
= 0;
1295 /* for i386 kernel 2.6 to get 1 ms */
1296 itv
.it_interval
.tv_usec
= 999;
1297 itv
.it_value
.tv_sec
= 0;
1298 itv
.it_value
.tv_usec
= 10 * 1000;
1300 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1307 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1309 struct itimerval itv
;
1311 memset(&itv
, 0, sizeof(itv
));
1312 setitimer(ITIMER_REAL
, &itv
, NULL
);
1315 #endif /* !defined(_WIN32) */
1320 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1323 struct qemu_alarm_win32
*data
= t
->priv
;
1326 memset(&tc
, 0, sizeof(tc
));
1327 timeGetDevCaps(&tc
, sizeof(tc
));
1329 if (data
->period
< tc
.wPeriodMin
)
1330 data
->period
= tc
.wPeriodMin
;
1332 timeBeginPeriod(data
->period
);
1334 flags
= TIME_CALLBACK_FUNCTION
;
1335 if (alarm_has_dynticks(t
))
1336 flags
|= TIME_ONESHOT
;
1338 flags
|= TIME_PERIODIC
;
1340 data
->timerId
= timeSetEvent(1, // interval (ms)
1341 data
->period
, // resolution
1342 host_alarm_handler
, // function
1343 (DWORD
)t
, // parameter
1346 if (!data
->timerId
) {
1347 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1349 timeEndPeriod(data
->period
);
1356 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1358 struct qemu_alarm_win32
*data
= t
->priv
;
1360 timeKillEvent(data
->timerId
);
1361 timeEndPeriod(data
->period
);
1364 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1366 struct qemu_alarm_win32
*data
= t
->priv
;
1368 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1369 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1370 !active_timers
[QEMU_CLOCK_HOST
])
1373 timeKillEvent(data
->timerId
);
1375 data
->timerId
= timeSetEvent(1,
1379 TIME_ONESHOT
| TIME_PERIODIC
);
1381 if (!data
->timerId
) {
1382 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1385 timeEndPeriod(data
->period
);
1392 static int init_timer_alarm(void)
1394 struct qemu_alarm_timer
*t
= NULL
;
1397 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1398 t
= &alarm_timers
[i
];
1418 static void quit_timers(void)
1420 alarm_timer
->stop(alarm_timer
);
1424 /***********************************************************/
1425 /* host time/date access */
1426 void qemu_get_timedate(struct tm
*tm
, int offset
)
1433 if (rtc_date_offset
== -1) {
1437 ret
= localtime(&ti
);
1439 ti
-= rtc_date_offset
;
1443 memcpy(tm
, ret
, sizeof(struct tm
));
1446 int qemu_timedate_diff(struct tm
*tm
)
1450 if (rtc_date_offset
== -1)
1452 seconds
= mktimegm(tm
);
1454 seconds
= mktime(tm
);
1456 seconds
= mktimegm(tm
) + rtc_date_offset
;
1458 return seconds
- time(NULL
);
1461 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1463 time_t rtc_start_date
;
1466 if (!strcmp(startdate
, "now") && legacy
) {
1467 rtc_date_offset
= -1;
1469 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1477 } else if (sscanf(startdate
, "%d-%d-%d",
1480 &tm
.tm_mday
) == 3) {
1489 rtc_start_date
= mktimegm(&tm
);
1490 if (rtc_start_date
== -1) {
1492 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1493 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1496 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1500 static void configure_rtc(QemuOpts
*opts
)
1504 value
= qemu_opt_get(opts
, "base");
1506 if (!strcmp(value
, "utc")) {
1508 } else if (!strcmp(value
, "localtime")) {
1511 configure_rtc_date_offset(value
, 0);
1514 value
= qemu_opt_get(opts
, "clock");
1516 if (!strcmp(value
, "host")) {
1517 rtc_clock
= host_clock
;
1518 } else if (!strcmp(value
, "vm")) {
1519 rtc_clock
= vm_clock
;
1521 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1525 #ifdef CONFIG_TARGET_I386
1526 value
= qemu_opt_get(opts
, "driftfix");
1528 if (!strcmp(buf
, "slew")) {
1530 } else if (!strcmp(buf
, "none")) {
1533 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1541 static void socket_cleanup(void)
1546 static int socket_init(void)
1551 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1553 err
= WSAGetLastError();
1554 fprintf(stderr
, "WSAStartup: %d\n", err
);
1557 atexit(socket_cleanup
);
1562 /***********************************************************/
1563 /* Bluetooth support */
1566 static struct HCIInfo
*hci_table
[MAX_NICS
];
1568 static struct bt_vlan_s
{
1569 struct bt_scatternet_s net
;
1571 struct bt_vlan_s
*next
;
1574 /* find or alloc a new bluetooth "VLAN" */
1575 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1577 struct bt_vlan_s
**pvlan
, *vlan
;
1578 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1582 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1584 pvlan
= &first_bt_vlan
;
1585 while (*pvlan
!= NULL
)
1586 pvlan
= &(*pvlan
)->next
;
1591 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1595 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1600 static struct HCIInfo null_hci
= {
1601 .cmd_send
= null_hci_send
,
1602 .sco_send
= null_hci_send
,
1603 .acl_send
= null_hci_send
,
1604 .bdaddr_set
= null_hci_addr_set
,
1607 struct HCIInfo
*qemu_next_hci(void)
1609 if (cur_hci
== nb_hcis
)
1612 return hci_table
[cur_hci
++];
1615 static struct HCIInfo
*hci_init(const char *str
)
1618 struct bt_scatternet_s
*vlan
= 0;
1620 if (!strcmp(str
, "null"))
1623 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1625 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1626 else if (!strncmp(str
, "hci", 3)) {
1629 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1630 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1635 vlan
= qemu_find_bt_vlan(0);
1637 return bt_new_hci(vlan
);
1640 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1645 static int bt_hci_parse(const char *str
)
1647 struct HCIInfo
*hci
;
1650 if (nb_hcis
>= MAX_NICS
) {
1651 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1655 hci
= hci_init(str
);
1664 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1665 hci
->bdaddr_set(hci
, bdaddr
.b
);
1667 hci_table
[nb_hcis
++] = hci
;
1672 static void bt_vhci_add(int vlan_id
)
1674 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1677 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1678 "an empty scatternet %i\n", vlan_id
);
1680 bt_vhci_init(bt_new_hci(vlan
));
1683 static struct bt_device_s
*bt_device_add(const char *opt
)
1685 struct bt_scatternet_s
*vlan
;
1687 char *endp
= strstr(opt
, ",vlan=");
1688 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1691 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1694 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1696 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1701 vlan
= qemu_find_bt_vlan(vlan_id
);
1704 fprintf(stderr
, "qemu: warning: adding a slave device to "
1705 "an empty scatternet %i\n", vlan_id
);
1707 if (!strcmp(devname
, "keyboard"))
1708 return bt_keyboard_init(vlan
);
1710 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1714 static int bt_parse(const char *opt
)
1716 const char *endp
, *p
;
1719 if (strstart(opt
, "hci", &endp
)) {
1720 if (!*endp
|| *endp
== ',') {
1722 if (!strstart(endp
, ",vlan=", 0))
1725 return bt_hci_parse(opt
);
1727 } else if (strstart(opt
, "vhci", &endp
)) {
1728 if (!*endp
|| *endp
== ',') {
1730 if (strstart(endp
, ",vlan=", &p
)) {
1731 vlan
= strtol(p
, (char **) &endp
, 0);
1733 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1737 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1746 } else if (strstart(opt
, "device:", &endp
))
1747 return !bt_device_add(endp
);
1749 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1753 /***********************************************************/
1754 /* QEMU Block devices */
1756 #define HD_ALIAS "index=%d,media=disk"
1757 #define CDROM_ALIAS "index=2,media=cdrom"
1758 #define FD_ALIAS "index=%d,if=floppy"
1759 #define PFLASH_ALIAS "if=pflash"
1760 #define MTD_ALIAS "if=mtd"
1761 #define SD_ALIAS "index=0,if=sd"
1763 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1770 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1773 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1775 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1776 __FUNCTION__
, optstr
);
1780 qemu_opt_set(opts
, "file", file
);
1784 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1788 /* seek interface, bus and unit */
1790 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1791 if (dinfo
->type
== type
&&
1792 dinfo
->bus
== bus
&&
1793 dinfo
->unit
== unit
)
1800 DriveInfo
*drive_get_by_id(const char *id
)
1804 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1805 if (strcmp(id
, dinfo
->id
))
1812 int drive_get_max_bus(BlockInterfaceType type
)
1818 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1819 if(dinfo
->type
== type
&&
1820 dinfo
->bus
> max_bus
)
1821 max_bus
= dinfo
->bus
;
1826 const char *drive_get_serial(BlockDriverState
*bdrv
)
1830 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1831 if (dinfo
->bdrv
== bdrv
)
1832 return dinfo
->serial
;
1838 BlockInterfaceErrorAction
drive_get_on_error(
1839 BlockDriverState
*bdrv
, int is_read
)
1843 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1844 if (dinfo
->bdrv
== bdrv
)
1845 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1848 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1851 static void bdrv_format_print(void *opaque
, const char *name
)
1853 fprintf(stderr
, " %s", name
);
1856 void drive_uninit(DriveInfo
*dinfo
)
1858 qemu_opts_del(dinfo
->opts
);
1859 bdrv_delete(dinfo
->bdrv
);
1860 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1864 static int parse_block_error_action(const char *buf
, int is_read
)
1866 if (!strcmp(buf
, "ignore")) {
1867 return BLOCK_ERR_IGNORE
;
1868 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1869 return BLOCK_ERR_STOP_ENOSPC
;
1870 } else if (!strcmp(buf
, "stop")) {
1871 return BLOCK_ERR_STOP_ANY
;
1872 } else if (!strcmp(buf
, "report")) {
1873 return BLOCK_ERR_REPORT
;
1875 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1876 buf
, is_read
? "read" : "write");
1881 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1885 const char *file
= NULL
;
1888 const char *mediastr
= "";
1889 BlockInterfaceType type
;
1890 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1891 int bus_id
, unit_id
;
1892 int cyls
, heads
, secs
, translation
;
1893 BlockDriver
*drv
= NULL
;
1894 QEMUMachine
*machine
= opaque
;
1901 int on_read_error
, on_write_error
;
1902 const char *devaddr
;
1908 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1911 if (machine
&& machine
->use_scsi
) {
1913 max_devs
= MAX_SCSI_DEVS
;
1914 pstrcpy(devname
, sizeof(devname
), "scsi");
1917 max_devs
= MAX_IDE_DEVS
;
1918 pstrcpy(devname
, sizeof(devname
), "ide");
1922 /* extract parameters */
1923 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1924 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1925 index
= qemu_opt_get_number(opts
, "index", -1);
1927 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1928 heads
= qemu_opt_get_number(opts
, "heads", 0);
1929 secs
= qemu_opt_get_number(opts
, "secs", 0);
1931 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1932 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1934 file
= qemu_opt_get(opts
, "file");
1935 serial
= qemu_opt_get(opts
, "serial");
1937 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1938 pstrcpy(devname
, sizeof(devname
), buf
);
1939 if (!strcmp(buf
, "ide")) {
1941 max_devs
= MAX_IDE_DEVS
;
1942 } else if (!strcmp(buf
, "scsi")) {
1944 max_devs
= MAX_SCSI_DEVS
;
1945 } else if (!strcmp(buf
, "floppy")) {
1948 } else if (!strcmp(buf
, "pflash")) {
1951 } else if (!strcmp(buf
, "mtd")) {
1954 } else if (!strcmp(buf
, "sd")) {
1957 } else if (!strcmp(buf
, "virtio")) {
1960 } else if (!strcmp(buf
, "xen")) {
1963 } else if (!strcmp(buf
, "none")) {
1967 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1972 if (cyls
|| heads
|| secs
) {
1973 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
1974 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1977 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
1978 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1981 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1982 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1987 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1990 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1994 if (!strcmp(buf
, "none"))
1995 translation
= BIOS_ATA_TRANSLATION_NONE
;
1996 else if (!strcmp(buf
, "lba"))
1997 translation
= BIOS_ATA_TRANSLATION_LBA
;
1998 else if (!strcmp(buf
, "auto"))
1999 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2001 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2006 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2007 if (!strcmp(buf
, "disk")) {
2009 } else if (!strcmp(buf
, "cdrom")) {
2010 if (cyls
|| secs
|| heads
) {
2012 "qemu: '%s' invalid physical CHS format\n", buf
);
2015 media
= MEDIA_CDROM
;
2017 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2022 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2023 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2025 else if (!strcmp(buf
, "writethrough"))
2027 else if (!strcmp(buf
, "writeback"))
2030 fprintf(stderr
, "qemu: invalid cache option\n");
2035 #ifdef CONFIG_LINUX_AIO
2036 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2037 if (!strcmp(buf
, "threads"))
2039 else if (!strcmp(buf
, "native"))
2042 fprintf(stderr
, "qemu: invalid aio option\n");
2048 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2049 if (strcmp(buf
, "?") == 0) {
2050 fprintf(stderr
, "qemu: Supported formats:");
2051 bdrv_iterate_format(bdrv_format_print
, NULL
);
2052 fprintf(stderr
, "\n");
2055 drv
= bdrv_find_whitelisted_format(buf
);
2057 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2062 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2063 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2064 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2065 fprintf(stderr
, "werror is no supported by this format\n");
2069 on_write_error
= parse_block_error_action(buf
, 0);
2070 if (on_write_error
< 0) {
2075 on_read_error
= BLOCK_ERR_REPORT
;
2076 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2077 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2078 fprintf(stderr
, "rerror is no supported by this format\n");
2082 on_read_error
= parse_block_error_action(buf
, 1);
2083 if (on_read_error
< 0) {
2088 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2089 if (type
!= IF_VIRTIO
) {
2090 fprintf(stderr
, "addr is not supported\n");
2095 /* compute bus and unit according index */
2098 if (bus_id
!= 0 || unit_id
!= -1) {
2100 "qemu: index cannot be used with bus and unit\n");
2108 unit_id
= index
% max_devs
;
2109 bus_id
= index
/ max_devs
;
2113 /* if user doesn't specify a unit_id,
2114 * try to find the first free
2117 if (unit_id
== -1) {
2119 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2121 if (max_devs
&& unit_id
>= max_devs
) {
2122 unit_id
-= max_devs
;
2130 if (max_devs
&& unit_id
>= max_devs
) {
2131 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2132 unit_id
, max_devs
- 1);
2137 * ignore multiple definitions
2140 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2147 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2148 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2149 dinfo
->id
= qemu_strdup(buf
);
2151 /* no id supplied -> create one */
2152 dinfo
->id
= qemu_mallocz(32);
2153 if (type
== IF_IDE
|| type
== IF_SCSI
)
2154 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2156 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2157 devname
, bus_id
, mediastr
, unit_id
);
2159 snprintf(dinfo
->id
, 32, "%s%s%i",
2160 devname
, mediastr
, unit_id
);
2162 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2163 dinfo
->devaddr
= devaddr
;
2165 dinfo
->bus
= bus_id
;
2166 dinfo
->unit
= unit_id
;
2167 dinfo
->on_read_error
= on_read_error
;
2168 dinfo
->on_write_error
= on_write_error
;
2171 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2172 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2182 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2183 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2187 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2192 /* FIXME: This isn't really a floppy, but it's a reasonable
2195 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2201 /* add virtio block device */
2202 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2203 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2204 qemu_opt_set(opts
, "drive", dinfo
->id
);
2206 qemu_opt_set(opts
, "addr", devaddr
);
2217 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2218 cache
= 2; /* always use write-back with snapshot */
2220 if (cache
== 0) /* no caching */
2221 bdrv_flags
|= BDRV_O_NOCACHE
;
2222 else if (cache
== 2) /* write-back */
2223 bdrv_flags
|= BDRV_O_CACHE_WB
;
2226 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2228 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2232 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2233 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2238 * cdrom is read-only. Set it now, after above interface checking
2239 * since readonly attribute not explicitly required, so no error.
2241 if (media
== MEDIA_CDROM
) {
2244 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2246 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2247 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2248 file
, strerror(errno
));
2252 if (bdrv_key_required(dinfo
->bdrv
))
2258 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2260 QEMUMachine
*machine
= opaque
;
2261 int fatal_error
= 0;
2263 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2270 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2272 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2273 qemu_opt_set(opts
, "snapshot", "on");
2278 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2280 boot_set_handler
= func
;
2281 boot_set_opaque
= opaque
;
2284 int qemu_boot_set(const char *boot_devices
)
2286 if (!boot_set_handler
) {
2289 return boot_set_handler(boot_set_opaque
, boot_devices
);
2292 static int parse_bootdevices(char *devices
)
2294 /* We just do some generic consistency checks */
2298 for (p
= devices
; *p
!= '\0'; p
++) {
2299 /* Allowed boot devices are:
2300 * a-b: floppy disk drives
2301 * c-f: IDE disk drives
2302 * g-m: machine implementation dependant drives
2303 * n-p: network devices
2304 * It's up to each machine implementation to check if the given boot
2305 * devices match the actual hardware implementation and firmware
2308 if (*p
< 'a' || *p
> 'p') {
2309 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2312 if (bitmap
& (1 << (*p
- 'a'))) {
2313 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2316 bitmap
|= 1 << (*p
- 'a');
2321 static void restore_boot_devices(void *opaque
)
2323 char *standard_boot_devices
= opaque
;
2325 qemu_boot_set(standard_boot_devices
);
2327 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2328 qemu_free(standard_boot_devices
);
2331 static void numa_add(const char *optarg
)
2335 unsigned long long value
, endvalue
;
2338 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2339 if (!strcmp(option
, "node")) {
2340 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2341 nodenr
= nb_numa_nodes
;
2343 nodenr
= strtoull(option
, NULL
, 10);
2346 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2347 node_mem
[nodenr
] = 0;
2349 value
= strtoull(option
, &endptr
, 0);
2351 case 0: case 'M': case 'm':
2358 node_mem
[nodenr
] = value
;
2360 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2361 node_cpumask
[nodenr
] = 0;
2363 value
= strtoull(option
, &endptr
, 10);
2366 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2368 if (*endptr
== '-') {
2369 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2370 if (endvalue
>= 63) {
2373 "only 63 CPUs in NUMA mode supported.\n");
2375 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2380 node_cpumask
[nodenr
] = value
;
2387 static void smp_parse(const char *optarg
)
2389 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2393 smp
= strtoul(optarg
, &endptr
, 10);
2394 if (endptr
!= optarg
) {
2395 if (*endptr
== ',') {
2399 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2400 sockets
= strtoull(option
, NULL
, 10);
2401 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2402 cores
= strtoull(option
, NULL
, 10);
2403 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2404 threads
= strtoull(option
, NULL
, 10);
2405 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2406 max_cpus
= strtoull(option
, NULL
, 10);
2408 /* compute missing values, prefer sockets over cores over threads */
2409 if (smp
== 0 || sockets
== 0) {
2410 sockets
= sockets
> 0 ? sockets
: 1;
2411 cores
= cores
> 0 ? cores
: 1;
2412 threads
= threads
> 0 ? threads
: 1;
2414 smp
= cores
* threads
* sockets
;
2418 threads
= threads
> 0 ? threads
: 1;
2419 cores
= smp
/ (sockets
* threads
);
2422 threads
= smp
/ (cores
* sockets
);
2427 smp_cores
= cores
> 0 ? cores
: 1;
2428 smp_threads
= threads
> 0 ? threads
: 1;
2430 max_cpus
= smp_cpus
;
2433 /***********************************************************/
2436 static int usb_device_add(const char *devname
, int is_hotplug
)
2439 USBDevice
*dev
= NULL
;
2444 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2445 dev
= usbdevice_create(devname
);
2449 /* the other ones */
2450 if (strstart(devname
, "host:", &p
)) {
2451 dev
= usb_host_device_open(p
);
2452 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2453 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2454 bt_new_hci(qemu_find_bt_vlan(0)));
2465 static int usb_device_del(const char *devname
)
2470 if (strstart(devname
, "host:", &p
))
2471 return usb_host_device_close(p
);
2476 p
= strchr(devname
, '.');
2479 bus_num
= strtoul(devname
, NULL
, 0);
2480 addr
= strtoul(p
+ 1, NULL
, 0);
2482 return usb_device_delete_addr(bus_num
, addr
);
2485 static int usb_parse(const char *cmdline
)
2488 r
= usb_device_add(cmdline
, 0);
2490 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2495 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2497 const char *devname
= qdict_get_str(qdict
, "devname");
2498 if (usb_device_add(devname
, 1) < 0) {
2499 qemu_error("could not add USB device '%s'\n", devname
);
2503 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2505 const char *devname
= qdict_get_str(qdict
, "devname");
2506 if (usb_device_del(devname
) < 0) {
2507 qemu_error("could not delete USB device '%s'\n", devname
);
2511 /***********************************************************/
2512 /* PCMCIA/Cardbus */
2514 static struct pcmcia_socket_entry_s
{
2515 PCMCIASocket
*socket
;
2516 struct pcmcia_socket_entry_s
*next
;
2517 } *pcmcia_sockets
= 0;
2519 void pcmcia_socket_register(PCMCIASocket
*socket
)
2521 struct pcmcia_socket_entry_s
*entry
;
2523 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2524 entry
->socket
= socket
;
2525 entry
->next
= pcmcia_sockets
;
2526 pcmcia_sockets
= entry
;
2529 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2531 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2533 ptr
= &pcmcia_sockets
;
2534 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2535 if (entry
->socket
== socket
) {
2541 void pcmcia_info(Monitor
*mon
)
2543 struct pcmcia_socket_entry_s
*iter
;
2545 if (!pcmcia_sockets
)
2546 monitor_printf(mon
, "No PCMCIA sockets\n");
2548 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2549 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2550 iter
->socket
->attached
? iter
->socket
->card_string
:
2554 /***********************************************************/
2555 /* register display */
2557 struct DisplayAllocator default_allocator
= {
2558 defaultallocator_create_displaysurface
,
2559 defaultallocator_resize_displaysurface
,
2560 defaultallocator_free_displaysurface
2563 void register_displaystate(DisplayState
*ds
)
2573 DisplayState
*get_displaystate(void)
2575 return display_state
;
2578 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2580 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2581 return ds
->allocator
;
2586 static void dumb_display_init(void)
2588 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2589 ds
->allocator
= &default_allocator
;
2590 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2591 register_displaystate(ds
);
2594 /***********************************************************/
2597 typedef struct IOHandlerRecord
{
2599 IOCanRWHandler
*fd_read_poll
;
2601 IOHandler
*fd_write
;
2604 /* temporary data */
2606 struct IOHandlerRecord
*next
;
2609 static IOHandlerRecord
*first_io_handler
;
2611 /* XXX: fd_read_poll should be suppressed, but an API change is
2612 necessary in the character devices to suppress fd_can_read(). */
2613 int qemu_set_fd_handler2(int fd
,
2614 IOCanRWHandler
*fd_read_poll
,
2616 IOHandler
*fd_write
,
2619 IOHandlerRecord
**pioh
, *ioh
;
2621 if (!fd_read
&& !fd_write
) {
2622 pioh
= &first_io_handler
;
2627 if (ioh
->fd
== fd
) {
2634 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2638 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2639 ioh
->next
= first_io_handler
;
2640 first_io_handler
= ioh
;
2643 ioh
->fd_read_poll
= fd_read_poll
;
2644 ioh
->fd_read
= fd_read
;
2645 ioh
->fd_write
= fd_write
;
2646 ioh
->opaque
= opaque
;
2652 int qemu_set_fd_handler(int fd
,
2654 IOHandler
*fd_write
,
2657 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2661 /***********************************************************/
2662 /* Polling handling */
2664 typedef struct PollingEntry
{
2667 struct PollingEntry
*next
;
2670 static PollingEntry
*first_polling_entry
;
2672 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2674 PollingEntry
**ppe
, *pe
;
2675 pe
= qemu_mallocz(sizeof(PollingEntry
));
2677 pe
->opaque
= opaque
;
2678 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2683 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2685 PollingEntry
**ppe
, *pe
;
2686 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2688 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2696 /***********************************************************/
2697 /* Wait objects support */
2698 typedef struct WaitObjects
{
2700 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2701 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2702 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2705 static WaitObjects wait_objects
= {0};
2707 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2709 WaitObjects
*w
= &wait_objects
;
2711 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2713 w
->events
[w
->num
] = handle
;
2714 w
->func
[w
->num
] = func
;
2715 w
->opaque
[w
->num
] = opaque
;
2720 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2723 WaitObjects
*w
= &wait_objects
;
2726 for (i
= 0; i
< w
->num
; i
++) {
2727 if (w
->events
[i
] == handle
)
2730 w
->events
[i
] = w
->events
[i
+ 1];
2731 w
->func
[i
] = w
->func
[i
+ 1];
2732 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2740 /***********************************************************/
2741 /* ram save/restore */
2743 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2744 #define RAM_SAVE_FLAG_COMPRESS 0x02
2745 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2746 #define RAM_SAVE_FLAG_PAGE 0x08
2747 #define RAM_SAVE_FLAG_EOS 0x10
2749 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2751 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2752 uint32_t *array
= (uint32_t *)page
;
2755 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2756 if (array
[i
] != val
)
2763 static int ram_save_block(QEMUFile
*f
)
2765 static ram_addr_t current_addr
= 0;
2766 ram_addr_t saved_addr
= current_addr
;
2767 ram_addr_t addr
= 0;
2770 while (addr
< last_ram_offset
) {
2771 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2774 cpu_physical_memory_reset_dirty(current_addr
,
2775 current_addr
+ TARGET_PAGE_SIZE
,
2776 MIGRATION_DIRTY_FLAG
);
2778 p
= qemu_get_ram_ptr(current_addr
);
2780 if (is_dup_page(p
, *p
)) {
2781 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2782 qemu_put_byte(f
, *p
);
2784 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2785 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2791 addr
+= TARGET_PAGE_SIZE
;
2792 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2798 static uint64_t bytes_transferred
;
2800 static ram_addr_t
ram_save_remaining(void)
2803 ram_addr_t count
= 0;
2805 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2806 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2813 uint64_t ram_bytes_remaining(void)
2815 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2818 uint64_t ram_bytes_transferred(void)
2820 return bytes_transferred
;
2823 uint64_t ram_bytes_total(void)
2825 return last_ram_offset
;
2828 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2831 uint64_t bytes_transferred_last
;
2833 uint64_t expected_time
= 0;
2836 cpu_physical_memory_set_dirty_tracking(0);
2840 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2841 qemu_file_set_error(f
);
2846 bytes_transferred
= 0;
2848 /* Make sure all dirty bits are set */
2849 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2850 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2851 cpu_physical_memory_set_dirty(addr
);
2854 /* Enable dirty memory tracking */
2855 cpu_physical_memory_set_dirty_tracking(1);
2857 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2860 bytes_transferred_last
= bytes_transferred
;
2861 bwidth
= get_clock();
2863 while (!qemu_file_rate_limit(f
)) {
2866 ret
= ram_save_block(f
);
2867 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2868 if (ret
== 0) /* no more blocks */
2872 bwidth
= get_clock() - bwidth
;
2873 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2875 /* if we haven't transferred anything this round, force expected_time to a
2876 * a very high value, but without crashing */
2880 /* try transferring iterative blocks of memory */
2882 /* flush all remaining blocks regardless of rate limiting */
2883 while (ram_save_block(f
) != 0) {
2884 bytes_transferred
+= TARGET_PAGE_SIZE
;
2886 cpu_physical_memory_set_dirty_tracking(0);
2889 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2891 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2893 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2896 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2901 if (version_id
!= 3)
2905 addr
= qemu_get_be64(f
);
2907 flags
= addr
& ~TARGET_PAGE_MASK
;
2908 addr
&= TARGET_PAGE_MASK
;
2910 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2911 if (addr
!= last_ram_offset
)
2915 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2916 uint8_t ch
= qemu_get_byte(f
);
2917 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2920 (!kvm_enabled() || kvm_has_sync_mmu())) {
2921 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2924 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2925 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2927 if (qemu_file_has_error(f
)) {
2930 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2935 void qemu_service_io(void)
2937 qemu_notify_event();
2940 /***********************************************************/
2941 /* machine registration */
2943 static QEMUMachine
*first_machine
= NULL
;
2944 QEMUMachine
*current_machine
= NULL
;
2946 int qemu_register_machine(QEMUMachine
*m
)
2949 pm
= &first_machine
;
2957 static QEMUMachine
*find_machine(const char *name
)
2961 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2962 if (!strcmp(m
->name
, name
))
2964 if (m
->alias
&& !strcmp(m
->alias
, name
))
2970 static QEMUMachine
*find_default_machine(void)
2974 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2975 if (m
->is_default
) {
2982 /***********************************************************/
2983 /* main execution loop */
2985 static void gui_update(void *opaque
)
2987 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2988 DisplayState
*ds
= opaque
;
2989 DisplayChangeListener
*dcl
= ds
->listeners
;
2993 while (dcl
!= NULL
) {
2994 if (dcl
->gui_timer_interval
&&
2995 dcl
->gui_timer_interval
< interval
)
2996 interval
= dcl
->gui_timer_interval
;
2999 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3002 static void nographic_update(void *opaque
)
3004 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3006 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3009 struct vm_change_state_entry
{
3010 VMChangeStateHandler
*cb
;
3012 QLIST_ENTRY (vm_change_state_entry
) entries
;
3015 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3017 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3020 VMChangeStateEntry
*e
;
3022 e
= qemu_mallocz(sizeof (*e
));
3026 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3030 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3032 QLIST_REMOVE (e
, entries
);
3036 static void vm_state_notify(int running
, int reason
)
3038 VMChangeStateEntry
*e
;
3040 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3041 e
->cb(e
->opaque
, running
, reason
);
3045 static void resume_all_vcpus(void);
3046 static void pause_all_vcpus(void);
3053 vm_state_notify(1, 0);
3054 qemu_rearm_alarm_timer(alarm_timer
);
3059 /* reset/shutdown handler */
3061 typedef struct QEMUResetEntry
{
3062 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3063 QEMUResetHandler
*func
;
3067 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3068 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3069 static int reset_requested
;
3070 static int shutdown_requested
;
3071 static int powerdown_requested
;
3072 static int debug_requested
;
3073 static int vmstop_requested
;
3075 int qemu_shutdown_requested(void)
3077 int r
= shutdown_requested
;
3078 shutdown_requested
= 0;
3082 int qemu_reset_requested(void)
3084 int r
= reset_requested
;
3085 reset_requested
= 0;
3089 int qemu_powerdown_requested(void)
3091 int r
= powerdown_requested
;
3092 powerdown_requested
= 0;
3096 static int qemu_debug_requested(void)
3098 int r
= debug_requested
;
3099 debug_requested
= 0;
3103 static int qemu_vmstop_requested(void)
3105 int r
= vmstop_requested
;
3106 vmstop_requested
= 0;
3110 static void do_vm_stop(int reason
)
3113 cpu_disable_ticks();
3116 vm_state_notify(0, reason
);
3120 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3122 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3125 re
->opaque
= opaque
;
3126 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3129 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3133 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3134 if (re
->func
== func
&& re
->opaque
== opaque
) {
3135 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3142 void qemu_system_reset(void)
3144 QEMUResetEntry
*re
, *nre
;
3146 /* reset all devices */
3147 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3148 re
->func(re
->opaque
);
3152 void qemu_system_reset_request(void)
3155 shutdown_requested
= 1;
3157 reset_requested
= 1;
3159 qemu_notify_event();
3162 void qemu_system_shutdown_request(void)
3164 shutdown_requested
= 1;
3165 qemu_notify_event();
3168 void qemu_system_powerdown_request(void)
3170 powerdown_requested
= 1;
3171 qemu_notify_event();
3174 #ifdef CONFIG_IOTHREAD
3175 static void qemu_system_vmstop_request(int reason
)
3177 vmstop_requested
= reason
;
3178 qemu_notify_event();
3183 static int io_thread_fd
= -1;
3185 static void qemu_event_increment(void)
3187 static const char byte
= 0;
3190 if (io_thread_fd
== -1)
3193 ret
= write(io_thread_fd
, &byte
, sizeof(byte
));
3194 if (ret
< 0 && (errno
!= EINTR
&& errno
!= EAGAIN
)) {
3195 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3201 static void qemu_event_read(void *opaque
)
3203 int fd
= (unsigned long)opaque
;
3206 /* Drain the notify pipe */
3209 len
= read(fd
, buffer
, sizeof(buffer
));
3210 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3213 static int qemu_event_init(void)
3218 err
= qemu_pipe(fds
);
3222 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3226 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3230 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3231 (void *)(unsigned long)fds
[0]);
3233 io_thread_fd
= fds
[1];
3242 HANDLE qemu_event_handle
;
3244 static void dummy_event_handler(void *opaque
)
3248 static int qemu_event_init(void)
3250 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3251 if (!qemu_event_handle
) {
3252 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3255 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3259 static void qemu_event_increment(void)
3261 if (!SetEvent(qemu_event_handle
)) {
3262 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3269 static int cpu_can_run(CPUState
*env
)
3278 #ifndef CONFIG_IOTHREAD
3279 static int qemu_init_main_loop(void)
3281 return qemu_event_init();
3284 void qemu_init_vcpu(void *_env
)
3286 CPUState
*env
= _env
;
3288 env
->nr_cores
= smp_cores
;
3289 env
->nr_threads
= smp_threads
;
3295 int qemu_cpu_self(void *env
)
3300 static void resume_all_vcpus(void)
3304 static void pause_all_vcpus(void)
3308 void qemu_cpu_kick(void *env
)
3313 void qemu_notify_event(void)
3315 CPUState
*env
= cpu_single_env
;
3322 void qemu_mutex_lock_iothread(void) {}
3323 void qemu_mutex_unlock_iothread(void) {}
3325 void vm_stop(int reason
)
3330 #else /* CONFIG_IOTHREAD */
3332 #include "qemu-thread.h"
3334 QemuMutex qemu_global_mutex
;
3335 static QemuMutex qemu_fair_mutex
;
3337 static QemuThread io_thread
;
3339 static QemuThread
*tcg_cpu_thread
;
3340 static QemuCond
*tcg_halt_cond
;
3342 static int qemu_system_ready
;
3344 static QemuCond qemu_cpu_cond
;
3346 static QemuCond qemu_system_cond
;
3347 static QemuCond qemu_pause_cond
;
3349 static void block_io_signals(void);
3350 static void unblock_io_signals(void);
3351 static int tcg_has_work(void);
3353 static int qemu_init_main_loop(void)
3357 ret
= qemu_event_init();
3361 qemu_cond_init(&qemu_pause_cond
);
3362 qemu_mutex_init(&qemu_fair_mutex
);
3363 qemu_mutex_init(&qemu_global_mutex
);
3364 qemu_mutex_lock(&qemu_global_mutex
);
3366 unblock_io_signals();
3367 qemu_thread_self(&io_thread
);
3372 static void qemu_wait_io_event(CPUState
*env
)
3374 while (!tcg_has_work())
3375 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3377 qemu_mutex_unlock(&qemu_global_mutex
);
3380 * Users of qemu_global_mutex can be starved, having no chance
3381 * to acquire it since this path will get to it first.
3382 * So use another lock to provide fairness.
3384 qemu_mutex_lock(&qemu_fair_mutex
);
3385 qemu_mutex_unlock(&qemu_fair_mutex
);
3387 qemu_mutex_lock(&qemu_global_mutex
);
3391 qemu_cond_signal(&qemu_pause_cond
);
3395 static int qemu_cpu_exec(CPUState
*env
);
3397 static void *kvm_cpu_thread_fn(void *arg
)
3399 CPUState
*env
= arg
;
3402 qemu_thread_self(env
->thread
);
3406 /* signal CPU creation */
3407 qemu_mutex_lock(&qemu_global_mutex
);
3409 qemu_cond_signal(&qemu_cpu_cond
);
3411 /* and wait for machine initialization */
3412 while (!qemu_system_ready
)
3413 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3416 if (cpu_can_run(env
))
3418 qemu_wait_io_event(env
);
3424 static void tcg_cpu_exec(void);
3426 static void *tcg_cpu_thread_fn(void *arg
)
3428 CPUState
*env
= arg
;
3431 qemu_thread_self(env
->thread
);
3433 /* signal CPU creation */
3434 qemu_mutex_lock(&qemu_global_mutex
);
3435 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3437 qemu_cond_signal(&qemu_cpu_cond
);
3439 /* and wait for machine initialization */
3440 while (!qemu_system_ready
)
3441 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3445 qemu_wait_io_event(cur_cpu
);
3451 void qemu_cpu_kick(void *_env
)
3453 CPUState
*env
= _env
;
3454 qemu_cond_broadcast(env
->halt_cond
);
3456 qemu_thread_signal(env
->thread
, SIGUSR1
);
3459 int qemu_cpu_self(void *_env
)
3461 CPUState
*env
= _env
;
3464 qemu_thread_self(&this);
3466 return qemu_thread_equal(&this, env
->thread
);
3469 static void cpu_signal(int sig
)
3472 cpu_exit(cpu_single_env
);
3475 static void block_io_signals(void)
3478 struct sigaction sigact
;
3481 sigaddset(&set
, SIGUSR2
);
3482 sigaddset(&set
, SIGIO
);
3483 sigaddset(&set
, SIGALRM
);
3484 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3487 sigaddset(&set
, SIGUSR1
);
3488 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3490 memset(&sigact
, 0, sizeof(sigact
));
3491 sigact
.sa_handler
= cpu_signal
;
3492 sigaction(SIGUSR1
, &sigact
, NULL
);
3495 static void unblock_io_signals(void)
3500 sigaddset(&set
, SIGUSR2
);
3501 sigaddset(&set
, SIGIO
);
3502 sigaddset(&set
, SIGALRM
);
3503 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3506 sigaddset(&set
, SIGUSR1
);
3507 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3510 static void qemu_signal_lock(unsigned int msecs
)
3512 qemu_mutex_lock(&qemu_fair_mutex
);
3514 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3515 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3516 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3519 qemu_mutex_unlock(&qemu_fair_mutex
);
3522 void qemu_mutex_lock_iothread(void)
3524 if (kvm_enabled()) {
3525 qemu_mutex_lock(&qemu_fair_mutex
);
3526 qemu_mutex_lock(&qemu_global_mutex
);
3527 qemu_mutex_unlock(&qemu_fair_mutex
);
3529 qemu_signal_lock(100);
3532 void qemu_mutex_unlock_iothread(void)
3534 qemu_mutex_unlock(&qemu_global_mutex
);
3537 static int all_vcpus_paused(void)
3539 CPUState
*penv
= first_cpu
;
3544 penv
= (CPUState
*)penv
->next_cpu
;
3550 static void pause_all_vcpus(void)
3552 CPUState
*penv
= first_cpu
;
3556 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3557 qemu_cpu_kick(penv
);
3558 penv
= (CPUState
*)penv
->next_cpu
;
3561 while (!all_vcpus_paused()) {
3562 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3565 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3566 penv
= (CPUState
*)penv
->next_cpu
;
3571 static void resume_all_vcpus(void)
3573 CPUState
*penv
= first_cpu
;
3578 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3579 qemu_cpu_kick(penv
);
3580 penv
= (CPUState
*)penv
->next_cpu
;
3584 static void tcg_init_vcpu(void *_env
)
3586 CPUState
*env
= _env
;
3587 /* share a single thread for all cpus with TCG */
3588 if (!tcg_cpu_thread
) {
3589 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3590 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3591 qemu_cond_init(env
->halt_cond
);
3592 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3593 while (env
->created
== 0)
3594 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3595 tcg_cpu_thread
= env
->thread
;
3596 tcg_halt_cond
= env
->halt_cond
;
3598 env
->thread
= tcg_cpu_thread
;
3599 env
->halt_cond
= tcg_halt_cond
;
3603 static void kvm_start_vcpu(CPUState
*env
)
3605 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3606 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3607 qemu_cond_init(env
->halt_cond
);
3608 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3609 while (env
->created
== 0)
3610 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3613 void qemu_init_vcpu(void *_env
)
3615 CPUState
*env
= _env
;
3617 env
->nr_cores
= smp_cores
;
3618 env
->nr_threads
= smp_threads
;
3620 kvm_start_vcpu(env
);
3625 void qemu_notify_event(void)
3627 qemu_event_increment();
3630 void vm_stop(int reason
)
3633 qemu_thread_self(&me
);
3635 if (!qemu_thread_equal(&me
, &io_thread
)) {
3636 qemu_system_vmstop_request(reason
);
3638 * FIXME: should not return to device code in case
3639 * vm_stop() has been requested.
3641 if (cpu_single_env
) {
3642 cpu_exit(cpu_single_env
);
3643 cpu_single_env
->stop
= 1;
3654 static void host_main_loop_wait(int *timeout
)
3660 /* XXX: need to suppress polling by better using win32 events */
3662 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3663 ret
|= pe
->func(pe
->opaque
);
3667 WaitObjects
*w
= &wait_objects
;
3669 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3670 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3671 if (w
->func
[ret
- WAIT_OBJECT_0
])
3672 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3674 /* Check for additional signaled events */
3675 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3677 /* Check if event is signaled */
3678 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3679 if(ret2
== WAIT_OBJECT_0
) {
3681 w
->func
[i
](w
->opaque
[i
]);
3682 } else if (ret2
== WAIT_TIMEOUT
) {
3684 err
= GetLastError();
3685 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3688 } else if (ret
== WAIT_TIMEOUT
) {
3690 err
= GetLastError();
3691 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3698 static void host_main_loop_wait(int *timeout
)
3703 void main_loop_wait(int timeout
)
3705 IOHandlerRecord
*ioh
;
3706 fd_set rfds
, wfds
, xfds
;
3710 qemu_bh_update_timeout(&timeout
);
3712 host_main_loop_wait(&timeout
);
3714 /* poll any events */
3715 /* XXX: separate device handlers from system ones */
3720 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3724 (!ioh
->fd_read_poll
||
3725 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3726 FD_SET(ioh
->fd
, &rfds
);
3730 if (ioh
->fd_write
) {
3731 FD_SET(ioh
->fd
, &wfds
);
3737 tv
.tv_sec
= timeout
/ 1000;
3738 tv
.tv_usec
= (timeout
% 1000) * 1000;
3740 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3742 qemu_mutex_unlock_iothread();
3743 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3744 qemu_mutex_lock_iothread();
3746 IOHandlerRecord
**pioh
;
3748 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3749 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3750 ioh
->fd_read(ioh
->opaque
);
3752 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3753 ioh
->fd_write(ioh
->opaque
);
3757 /* remove deleted IO handlers */
3758 pioh
= &first_io_handler
;
3769 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3771 /* rearm timer, if not periodic */
3772 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3773 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3774 qemu_rearm_alarm_timer(alarm_timer
);
3777 /* vm time timers */
3779 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3780 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3781 qemu_get_clock(vm_clock
));
3784 /* real time timers */
3785 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3786 qemu_get_clock(rt_clock
));
3788 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3789 qemu_get_clock(host_clock
));
3791 /* Check bottom-halves last in case any of the earlier events triggered
3797 static int qemu_cpu_exec(CPUState
*env
)
3800 #ifdef CONFIG_PROFILER
3804 #ifdef CONFIG_PROFILER
3805 ti
= profile_getclock();
3810 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3811 env
->icount_decr
.u16
.low
= 0;
3812 env
->icount_extra
= 0;
3813 count
= qemu_next_deadline();
3814 count
= (count
+ (1 << icount_time_shift
) - 1)
3815 >> icount_time_shift
;
3816 qemu_icount
+= count
;
3817 decr
= (count
> 0xffff) ? 0xffff : count
;
3819 env
->icount_decr
.u16
.low
= decr
;
3820 env
->icount_extra
= count
;
3822 ret
= cpu_exec(env
);
3823 #ifdef CONFIG_PROFILER
3824 qemu_time
+= profile_getclock() - ti
;
3827 /* Fold pending instructions back into the
3828 instruction counter, and clear the interrupt flag. */
3829 qemu_icount
-= (env
->icount_decr
.u16
.low
3830 + env
->icount_extra
);
3831 env
->icount_decr
.u32
= 0;
3832 env
->icount_extra
= 0;
3837 static void tcg_cpu_exec(void)
3841 if (next_cpu
== NULL
)
3842 next_cpu
= first_cpu
;
3843 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3844 CPUState
*env
= cur_cpu
= next_cpu
;
3848 if (timer_alarm_pending
) {
3849 timer_alarm_pending
= 0;
3852 if (cpu_can_run(env
))
3853 ret
= qemu_cpu_exec(env
);
3854 if (ret
== EXCP_DEBUG
) {
3855 gdb_set_stop_cpu(env
);
3856 debug_requested
= 1;
3862 static int cpu_has_work(CPUState
*env
)
3870 if (qemu_cpu_has_work(env
))
3875 static int tcg_has_work(void)
3879 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3880 if (cpu_has_work(env
))
3885 static int qemu_calculate_timeout(void)
3887 #ifndef CONFIG_IOTHREAD
3892 else if (tcg_has_work())
3894 else if (!use_icount
)
3897 /* XXX: use timeout computed from timers */
3900 /* Advance virtual time to the next event. */
3901 if (use_icount
== 1) {
3902 /* When not using an adaptive execution frequency
3903 we tend to get badly out of sync with real time,
3904 so just delay for a reasonable amount of time. */
3907 delta
= cpu_get_icount() - cpu_get_clock();
3910 /* If virtual time is ahead of real time then just
3912 timeout
= (delta
/ 1000000) + 1;
3914 /* Wait for either IO to occur or the next
3916 add
= qemu_next_deadline();
3917 /* We advance the timer before checking for IO.
3918 Limit the amount we advance so that early IO
3919 activity won't get the guest too far ahead. */
3923 add
= (add
+ (1 << icount_time_shift
) - 1)
3924 >> icount_time_shift
;
3926 timeout
= delta
/ 1000000;
3933 #else /* CONFIG_IOTHREAD */
3938 static int vm_can_run(void)
3940 if (powerdown_requested
)
3942 if (reset_requested
)
3944 if (shutdown_requested
)
3946 if (debug_requested
)
3951 qemu_irq qemu_system_powerdown
;
3953 static void main_loop(void)
3957 #ifdef CONFIG_IOTHREAD
3958 qemu_system_ready
= 1;
3959 qemu_cond_broadcast(&qemu_system_cond
);
3964 #ifdef CONFIG_PROFILER
3967 #ifndef CONFIG_IOTHREAD
3970 #ifdef CONFIG_PROFILER
3971 ti
= profile_getclock();
3973 main_loop_wait(qemu_calculate_timeout());
3974 #ifdef CONFIG_PROFILER
3975 dev_time
+= profile_getclock() - ti
;
3977 } while (vm_can_run());
3979 if (qemu_debug_requested()) {
3980 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
3981 vm_stop(EXCP_DEBUG
);
3983 if (qemu_shutdown_requested()) {
3984 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3991 if (qemu_reset_requested()) {
3992 monitor_protocol_event(QEVENT_RESET
, NULL
);
3994 qemu_system_reset();
3997 if (qemu_powerdown_requested()) {
3998 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
3999 qemu_irq_raise(qemu_system_powerdown
);
4001 if ((r
= qemu_vmstop_requested())) {
4002 monitor_protocol_event(QEVENT_STOP
, NULL
);
4009 static void version(void)
4011 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4014 static void help(int exitcode
)
4017 printf("usage: %s [options] [disk_image]\n"
4019 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4021 #define DEF(option, opt_arg, opt_enum, opt_help) \
4023 #define DEFHEADING(text) stringify(text) "\n"
4024 #include "qemu-options.h"
4029 "During emulation, the following keys are useful:\n"
4030 "ctrl-alt-f toggle full screen\n"
4031 "ctrl-alt-n switch to virtual console 'n'\n"
4032 "ctrl-alt toggle mouse and keyboard grab\n"
4034 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4039 DEFAULT_NETWORK_SCRIPT
,
4040 DEFAULT_NETWORK_DOWN_SCRIPT
,
4042 DEFAULT_GDBSTUB_PORT
,
4047 #define HAS_ARG 0x0001
4050 #define DEF(option, opt_arg, opt_enum, opt_help) \
4052 #define DEFHEADING(text)
4053 #include "qemu-options.h"
4059 typedef struct QEMUOption
{
4065 static const QEMUOption qemu_options
[] = {
4066 { "h", 0, QEMU_OPTION_h
},
4067 #define DEF(option, opt_arg, opt_enum, opt_help) \
4068 { option, opt_arg, opt_enum },
4069 #define DEFHEADING(text)
4070 #include "qemu-options.h"
4078 struct soundhw soundhw
[] = {
4079 #ifdef HAS_AUDIO_CHOICE
4080 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4086 { .init_isa
= pcspk_audio_init
}
4093 "Creative Sound Blaster 16",
4096 { .init_isa
= SB16_init
}
4100 #ifdef CONFIG_CS4231A
4106 { .init_isa
= cs4231a_init
}
4114 "Yamaha YMF262 (OPL3)",
4116 "Yamaha YM3812 (OPL2)",
4120 { .init_isa
= Adlib_init
}
4127 "Gravis Ultrasound GF1",
4130 { .init_isa
= GUS_init
}
4137 "Intel 82801AA AC97 Audio",
4140 { .init_pci
= ac97_init
}
4144 #ifdef CONFIG_ES1370
4147 "ENSONIQ AudioPCI ES1370",
4150 { .init_pci
= es1370_init
}
4154 #endif /* HAS_AUDIO_CHOICE */
4156 { NULL
, NULL
, 0, 0, { NULL
} }
4159 static void select_soundhw (const char *optarg
)
4163 if (*optarg
== '?') {
4166 printf ("Valid sound card names (comma separated):\n");
4167 for (c
= soundhw
; c
->name
; ++c
) {
4168 printf ("%-11s %s\n", c
->name
, c
->descr
);
4170 printf ("\n-soundhw all will enable all of the above\n");
4171 exit (*optarg
!= '?');
4179 if (!strcmp (optarg
, "all")) {
4180 for (c
= soundhw
; c
->name
; ++c
) {
4188 e
= strchr (p
, ',');
4189 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4191 for (c
= soundhw
; c
->name
; ++c
) {
4192 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4201 "Unknown sound card name (too big to show)\n");
4204 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4209 p
+= l
+ (e
!= NULL
);
4213 goto show_valid_cards
;
4218 static void select_vgahw (const char *p
)
4223 vga_interface_type
= VGA_NONE
;
4224 if (strstart(p
, "std", &opts
)) {
4225 vga_interface_type
= VGA_STD
;
4226 } else if (strstart(p
, "cirrus", &opts
)) {
4227 vga_interface_type
= VGA_CIRRUS
;
4228 } else if (strstart(p
, "vmware", &opts
)) {
4229 vga_interface_type
= VGA_VMWARE
;
4230 } else if (strstart(p
, "xenfb", &opts
)) {
4231 vga_interface_type
= VGA_XENFB
;
4232 } else if (!strstart(p
, "none", &opts
)) {
4234 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4238 const char *nextopt
;
4240 if (strstart(opts
, ",retrace=", &nextopt
)) {
4242 if (strstart(opts
, "dumb", &nextopt
))
4243 vga_retrace_method
= VGA_RETRACE_DUMB
;
4244 else if (strstart(opts
, "precise", &nextopt
))
4245 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4246 else goto invalid_vga
;
4247 } else goto invalid_vga
;
4253 static int balloon_parse(const char *arg
)
4257 if (strcmp(arg
, "none") == 0) {
4261 if (!strncmp(arg
, "virtio", 6)) {
4262 if (arg
[6] == ',') {
4263 /* have params -> parse them */
4264 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4268 /* create empty opts */
4269 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4271 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4280 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4282 exit(STATUS_CONTROL_C_EXIT
);
4287 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4291 if(strlen(str
) != 36)
4294 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4295 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4296 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4302 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4310 static void termsig_handler(int signal
)
4312 qemu_system_shutdown_request();
4315 static void sigchld_handler(int signal
)
4317 waitpid(-1, NULL
, WNOHANG
);
4320 static void sighandler_setup(void)
4322 struct sigaction act
;
4324 memset(&act
, 0, sizeof(act
));
4325 act
.sa_handler
= termsig_handler
;
4326 sigaction(SIGINT
, &act
, NULL
);
4327 sigaction(SIGHUP
, &act
, NULL
);
4328 sigaction(SIGTERM
, &act
, NULL
);
4330 act
.sa_handler
= sigchld_handler
;
4331 act
.sa_flags
= SA_NOCLDSTOP
;
4332 sigaction(SIGCHLD
, &act
, NULL
);
4338 /* Look for support files in the same directory as the executable. */
4339 static char *find_datadir(const char *argv0
)
4345 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4352 while (p
!= buf
&& *p
!= '\\')
4355 if (access(buf
, R_OK
) == 0) {
4356 return qemu_strdup(buf
);
4362 /* Find a likely location for support files using the location of the binary.
4363 For installed binaries this will be "$bindir/../share/qemu". When
4364 running from the build tree this will be "$bindir/../pc-bios". */
4365 #define SHARE_SUFFIX "/share/qemu"
4366 #define BUILD_SUFFIX "/pc-bios"
4367 static char *find_datadir(const char *argv0
)
4375 #if defined(__linux__)
4378 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4384 #elif defined(__FreeBSD__)
4387 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4394 /* If we don't have any way of figuring out the actual executable
4395 location then try argv[0]. */
4397 p
= realpath(argv0
, buf
);
4405 max_len
= strlen(dir
) +
4406 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4407 res
= qemu_mallocz(max_len
);
4408 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4409 if (access(res
, R_OK
)) {
4410 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4411 if (access(res
, R_OK
)) {
4423 char *qemu_find_file(int type
, const char *name
)
4429 /* If name contains path separators then try it as a straight path. */
4430 if ((strchr(name
, '/') || strchr(name
, '\\'))
4431 && access(name
, R_OK
) == 0) {
4432 return qemu_strdup(name
);
4435 case QEMU_FILE_TYPE_BIOS
:
4438 case QEMU_FILE_TYPE_KEYMAP
:
4439 subdir
= "keymaps/";
4444 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4445 buf
= qemu_mallocz(len
);
4446 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4447 if (access(buf
, R_OK
)) {
4454 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4458 dev
= qdev_device_add(opts
);
4464 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4466 CharDriverState
*chr
;
4468 chr
= qemu_chr_open_opts(opts
, NULL
);
4474 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4476 CharDriverState
*chr
;
4477 const char *chardev
;
4481 mode
= qemu_opt_get(opts
, "mode");
4485 if (strcmp(mode
, "readline") == 0) {
4486 flags
= MONITOR_USE_READLINE
;
4487 } else if (strcmp(mode
, "control") == 0) {
4488 flags
= MONITOR_USE_CONTROL
;
4490 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4494 if (qemu_opt_get_bool(opts
, "default", 0))
4495 flags
|= MONITOR_IS_DEFAULT
;
4497 chardev
= qemu_opt_get(opts
, "chardev");
4498 chr
= qemu_chr_find(chardev
);
4500 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4504 monitor_init(chr
, flags
);
4508 static void monitor_parse(const char *optarg
, const char *mode
)
4510 static int monitor_device_index
= 0;
4516 if (strstart(optarg
, "chardev:", &p
)) {
4517 snprintf(label
, sizeof(label
), "%s", p
);
4519 if (monitor_device_index
) {
4520 snprintf(label
, sizeof(label
), "monitor%d",
4521 monitor_device_index
);
4523 snprintf(label
, sizeof(label
), "monitor");
4526 opts
= qemu_chr_parse_compat(label
, optarg
);
4528 fprintf(stderr
, "parse error: %s\n", optarg
);
4533 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4535 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4538 qemu_opt_set(opts
, "mode", mode
);
4539 qemu_opt_set(opts
, "chardev", label
);
4541 qemu_opt_set(opts
, "default", "on");
4542 monitor_device_index
++;
4545 struct device_config
{
4547 DEV_USB
, /* -usbdevice */
4549 DEV_SERIAL
, /* -serial */
4550 DEV_PARALLEL
, /* -parallel */
4551 DEV_VIRTCON
, /* -virtioconsole */
4552 DEV_DEBUGCON
, /* -debugcon */
4554 const char *cmdline
;
4555 QTAILQ_ENTRY(device_config
) next
;
4557 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4559 static void add_device_config(int type
, const char *cmdline
)
4561 struct device_config
*conf
;
4563 conf
= qemu_mallocz(sizeof(*conf
));
4565 conf
->cmdline
= cmdline
;
4566 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4569 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4571 struct device_config
*conf
;
4574 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4575 if (conf
->type
!= type
)
4577 rc
= func(conf
->cmdline
);
4584 static int serial_parse(const char *devname
)
4586 static int index
= 0;
4589 if (strcmp(devname
, "none") == 0)
4591 if (index
== MAX_SERIAL_PORTS
) {
4592 fprintf(stderr
, "qemu: too many serial ports\n");
4595 snprintf(label
, sizeof(label
), "serial%d", index
);
4596 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4597 if (!serial_hds
[index
]) {
4598 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4599 devname
, strerror(errno
));
4606 static int parallel_parse(const char *devname
)
4608 static int index
= 0;
4611 if (strcmp(devname
, "none") == 0)
4613 if (index
== MAX_PARALLEL_PORTS
) {
4614 fprintf(stderr
, "qemu: too many parallel ports\n");
4617 snprintf(label
, sizeof(label
), "parallel%d", index
);
4618 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4619 if (!parallel_hds
[index
]) {
4620 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4621 devname
, strerror(errno
));
4628 static int virtcon_parse(const char *devname
)
4630 static int index
= 0;
4633 if (strcmp(devname
, "none") == 0)
4635 if (index
== MAX_VIRTIO_CONSOLES
) {
4636 fprintf(stderr
, "qemu: too many virtio consoles\n");
4639 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4640 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4641 if (!virtcon_hds
[index
]) {
4642 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4643 devname
, strerror(errno
));
4650 static int debugcon_parse(const char *devname
)
4654 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4657 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4659 fprintf(stderr
, "qemu: already have a debugcon device\n");
4662 qemu_opt_set(opts
, "driver", "isa-debugcon");
4663 qemu_opt_set(opts
, "chardev", "debugcon");
4667 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4668 const char **poptarg
, int *poptind
)
4670 const QEMUOption
*popt
;
4671 int optind
= *poptind
;
4672 char *r
= argv
[optind
];
4676 /* Treat --foo the same as -foo. */
4679 popt
= qemu_options
;
4682 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4686 if (!strcmp(popt
->name
, r
+ 1))
4690 if (popt
->flags
& HAS_ARG
) {
4691 if (optind
>= argc
) {
4692 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4696 optarg
= argv
[optind
++];
4707 int main(int argc
, char **argv
, char **envp
)
4709 const char *gdbstub_dev
= NULL
;
4710 uint32_t boot_devices_bitmap
= 0;
4712 int snapshot
, linux_boot
, net_boot
;
4713 const char *initrd_filename
;
4714 const char *kernel_filename
, *kernel_cmdline
;
4715 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4717 DisplayChangeListener
*dcl
;
4718 int cyls
, heads
, secs
, translation
;
4719 QemuOpts
*hda_opts
= NULL
, *opts
;
4722 const char *loadvm
= NULL
;
4723 QEMUMachine
*machine
;
4724 const char *cpu_model
;
4729 const char *pid_file
= NULL
;
4730 const char *incoming
= NULL
;
4733 struct passwd
*pwd
= NULL
;
4734 const char *chroot_dir
= NULL
;
4735 const char *run_as
= NULL
;
4738 int show_vnc_port
= 0;
4743 qemu_errors_to_file(stderr
);
4744 qemu_cache_utils_init(envp
);
4746 QLIST_INIT (&vm_change_state_head
);
4749 struct sigaction act
;
4750 sigfillset(&act
.sa_mask
);
4752 act
.sa_handler
= SIG_IGN
;
4753 sigaction(SIGPIPE
, &act
, NULL
);
4756 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4757 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4758 QEMU to run on a single CPU */
4763 h
= GetCurrentProcess();
4764 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4765 for(i
= 0; i
< 32; i
++) {
4766 if (mask
& (1 << i
))
4771 SetProcessAffinityMask(h
, mask
);
4777 module_call_init(MODULE_INIT_MACHINE
);
4778 machine
= find_default_machine();
4780 initrd_filename
= NULL
;
4783 kernel_filename
= NULL
;
4784 kernel_cmdline
= "";
4785 cyls
= heads
= secs
= 0;
4786 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4788 for (i
= 0; i
< MAX_NODES
; i
++) {
4790 node_cpumask
[i
] = 0;
4799 /* first pass of option parsing */
4801 while (optind
< argc
) {
4802 if (argv
[optind
][0] != '-') {
4806 const QEMUOption
*popt
;
4808 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4809 switch (popt
->index
) {
4810 case QEMU_OPTION_nodefconfig
:
4819 fp
= fopen(CONFIG_QEMU_CONFDIR
"/qemu.conf", "r");
4821 if (qemu_config_parse(fp
) != 0) {
4827 fp
= fopen(CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", "r");
4829 if (qemu_config_parse(fp
) != 0) {
4836 /* second pass of option parsing */
4841 if (argv
[optind
][0] != '-') {
4842 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4844 const QEMUOption
*popt
;
4846 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4847 switch(popt
->index
) {
4849 machine
= find_machine(optarg
);
4852 printf("Supported machines are:\n");
4853 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4855 printf("%-10s %s (alias of %s)\n",
4856 m
->alias
, m
->desc
, m
->name
);
4857 printf("%-10s %s%s\n",
4859 m
->is_default
? " (default)" : "");
4861 exit(*optarg
!= '?');
4864 case QEMU_OPTION_cpu
:
4865 /* hw initialization will check this */
4866 if (*optarg
== '?') {
4867 /* XXX: implement xxx_cpu_list for targets that still miss it */
4868 #if defined(cpu_list)
4869 cpu_list(stdout
, &fprintf
);
4876 case QEMU_OPTION_initrd
:
4877 initrd_filename
= optarg
;
4879 case QEMU_OPTION_hda
:
4881 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4883 hda_opts
= drive_add(optarg
, HD_ALIAS
4884 ",cyls=%d,heads=%d,secs=%d%s",
4885 0, cyls
, heads
, secs
,
4886 translation
== BIOS_ATA_TRANSLATION_LBA
?
4888 translation
== BIOS_ATA_TRANSLATION_NONE
?
4889 ",trans=none" : "");
4891 case QEMU_OPTION_hdb
:
4892 case QEMU_OPTION_hdc
:
4893 case QEMU_OPTION_hdd
:
4894 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4896 case QEMU_OPTION_drive
:
4897 drive_add(NULL
, "%s", optarg
);
4899 case QEMU_OPTION_set
:
4900 if (qemu_set_option(optarg
) != 0)
4903 case QEMU_OPTION_global
:
4904 if (qemu_global_option(optarg
) != 0)
4907 case QEMU_OPTION_mtdblock
:
4908 drive_add(optarg
, MTD_ALIAS
);
4910 case QEMU_OPTION_sd
:
4911 drive_add(optarg
, SD_ALIAS
);
4913 case QEMU_OPTION_pflash
:
4914 drive_add(optarg
, PFLASH_ALIAS
);
4916 case QEMU_OPTION_snapshot
:
4919 case QEMU_OPTION_hdachs
:
4923 cyls
= strtol(p
, (char **)&p
, 0);
4924 if (cyls
< 1 || cyls
> 16383)
4929 heads
= strtol(p
, (char **)&p
, 0);
4930 if (heads
< 1 || heads
> 16)
4935 secs
= strtol(p
, (char **)&p
, 0);
4936 if (secs
< 1 || secs
> 63)
4940 if (!strcmp(p
, "none"))
4941 translation
= BIOS_ATA_TRANSLATION_NONE
;
4942 else if (!strcmp(p
, "lba"))
4943 translation
= BIOS_ATA_TRANSLATION_LBA
;
4944 else if (!strcmp(p
, "auto"))
4945 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4948 } else if (*p
!= '\0') {
4950 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4953 if (hda_opts
!= NULL
) {
4955 snprintf(num
, sizeof(num
), "%d", cyls
);
4956 qemu_opt_set(hda_opts
, "cyls", num
);
4957 snprintf(num
, sizeof(num
), "%d", heads
);
4958 qemu_opt_set(hda_opts
, "heads", num
);
4959 snprintf(num
, sizeof(num
), "%d", secs
);
4960 qemu_opt_set(hda_opts
, "secs", num
);
4961 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4962 qemu_opt_set(hda_opts
, "trans", "lba");
4963 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4964 qemu_opt_set(hda_opts
, "trans", "none");
4968 case QEMU_OPTION_numa
:
4969 if (nb_numa_nodes
>= MAX_NODES
) {
4970 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4975 case QEMU_OPTION_nographic
:
4976 display_type
= DT_NOGRAPHIC
;
4978 #ifdef CONFIG_CURSES
4979 case QEMU_OPTION_curses
:
4980 display_type
= DT_CURSES
;
4983 case QEMU_OPTION_portrait
:
4986 case QEMU_OPTION_kernel
:
4987 kernel_filename
= optarg
;
4989 case QEMU_OPTION_append
:
4990 kernel_cmdline
= optarg
;
4992 case QEMU_OPTION_cdrom
:
4993 drive_add(optarg
, CDROM_ALIAS
);
4995 case QEMU_OPTION_boot
:
4997 static const char * const params
[] = {
4998 "order", "once", "menu", NULL
5000 char buf
[sizeof(boot_devices
)];
5001 char *standard_boot_devices
;
5004 if (!strchr(optarg
, '=')) {
5006 pstrcpy(buf
, sizeof(buf
), optarg
);
5007 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5009 "qemu: unknown boot parameter '%s' in '%s'\n",
5015 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5016 boot_devices_bitmap
= parse_bootdevices(buf
);
5017 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5020 if (get_param_value(buf
, sizeof(buf
),
5022 boot_devices_bitmap
|= parse_bootdevices(buf
);
5023 standard_boot_devices
= qemu_strdup(boot_devices
);
5024 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5025 qemu_register_reset(restore_boot_devices
,
5026 standard_boot_devices
);
5028 if (get_param_value(buf
, sizeof(buf
),
5030 if (!strcmp(buf
, "on")) {
5032 } else if (!strcmp(buf
, "off")) {
5036 "qemu: invalid option value '%s'\n",
5044 case QEMU_OPTION_fda
:
5045 case QEMU_OPTION_fdb
:
5046 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5049 case QEMU_OPTION_no_fd_bootchk
:
5053 case QEMU_OPTION_netdev
:
5054 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5058 case QEMU_OPTION_net
:
5059 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5064 case QEMU_OPTION_tftp
:
5065 legacy_tftp_prefix
= optarg
;
5067 case QEMU_OPTION_bootp
:
5068 legacy_bootp_filename
= optarg
;
5071 case QEMU_OPTION_smb
:
5072 if (net_slirp_smb(optarg
) < 0)
5076 case QEMU_OPTION_redir
:
5077 if (net_slirp_redir(optarg
) < 0)
5081 case QEMU_OPTION_bt
:
5082 add_device_config(DEV_BT
, optarg
);
5085 case QEMU_OPTION_audio_help
:
5089 case QEMU_OPTION_soundhw
:
5090 select_soundhw (optarg
);
5096 case QEMU_OPTION_version
:
5100 case QEMU_OPTION_m
: {
5104 value
= strtoul(optarg
, &ptr
, 10);
5106 case 0: case 'M': case 'm':
5113 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5117 /* On 32-bit hosts, QEMU is limited by virtual address space */
5118 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5119 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5122 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5123 fprintf(stderr
, "qemu: ram size too large\n");
5132 const CPULogItem
*item
;
5134 mask
= cpu_str_to_log_mask(optarg
);
5136 printf("Log items (comma separated):\n");
5137 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5138 printf("%-10s %s\n", item
->name
, item
->help
);
5146 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5148 case QEMU_OPTION_gdb
:
5149 gdbstub_dev
= optarg
;
5154 case QEMU_OPTION_bios
:
5157 case QEMU_OPTION_singlestep
:
5164 keyboard_layout
= optarg
;
5166 case QEMU_OPTION_localtime
:
5169 case QEMU_OPTION_vga
:
5170 select_vgahw (optarg
);
5172 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5178 w
= strtol(p
, (char **)&p
, 10);
5181 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5187 h
= strtol(p
, (char **)&p
, 10);
5192 depth
= strtol(p
, (char **)&p
, 10);
5193 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5194 depth
!= 24 && depth
!= 32)
5196 } else if (*p
== '\0') {
5197 depth
= graphic_depth
;
5204 graphic_depth
= depth
;
5208 case QEMU_OPTION_echr
:
5211 term_escape_char
= strtol(optarg
, &r
, 0);
5213 printf("Bad argument to echr\n");
5216 case QEMU_OPTION_monitor
:
5217 monitor_parse(optarg
, "readline");
5218 default_monitor
= 0;
5220 case QEMU_OPTION_qmp
:
5221 monitor_parse(optarg
, "control");
5222 default_monitor
= 0;
5224 case QEMU_OPTION_mon
:
5225 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5227 fprintf(stderr
, "parse error: %s\n", optarg
);
5230 default_monitor
= 0;
5232 case QEMU_OPTION_chardev
:
5233 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5235 fprintf(stderr
, "parse error: %s\n", optarg
);
5239 case QEMU_OPTION_serial
:
5240 add_device_config(DEV_SERIAL
, optarg
);
5243 case QEMU_OPTION_watchdog
:
5246 "qemu: only one watchdog option may be given\n");
5251 case QEMU_OPTION_watchdog_action
:
5252 if (select_watchdog_action(optarg
) == -1) {
5253 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5257 case QEMU_OPTION_virtiocon
:
5258 add_device_config(DEV_VIRTCON
, optarg
);
5259 default_virtcon
= 0;
5261 case QEMU_OPTION_parallel
:
5262 add_device_config(DEV_PARALLEL
, optarg
);
5263 default_parallel
= 0;
5265 case QEMU_OPTION_debugcon
:
5266 add_device_config(DEV_DEBUGCON
, optarg
);
5268 case QEMU_OPTION_loadvm
:
5271 case QEMU_OPTION_full_screen
:
5275 case QEMU_OPTION_no_frame
:
5278 case QEMU_OPTION_alt_grab
:
5281 case QEMU_OPTION_ctrl_grab
:
5284 case QEMU_OPTION_no_quit
:
5287 case QEMU_OPTION_sdl
:
5288 display_type
= DT_SDL
;
5291 case QEMU_OPTION_pidfile
:
5295 case QEMU_OPTION_win2k_hack
:
5296 win2k_install_hack
= 1;
5298 case QEMU_OPTION_rtc_td_hack
:
5301 case QEMU_OPTION_acpitable
:
5302 if(acpi_table_add(optarg
) < 0) {
5303 fprintf(stderr
, "Wrong acpi table provided\n");
5307 case QEMU_OPTION_smbios
:
5308 if(smbios_entry_add(optarg
) < 0) {
5309 fprintf(stderr
, "Wrong smbios provided\n");
5315 case QEMU_OPTION_enable_kvm
:
5319 case QEMU_OPTION_usb
:
5322 case QEMU_OPTION_usbdevice
:
5324 add_device_config(DEV_USB
, optarg
);
5326 case QEMU_OPTION_device
:
5327 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5331 case QEMU_OPTION_smp
:
5334 fprintf(stderr
, "Invalid number of CPUs\n");
5337 if (max_cpus
< smp_cpus
) {
5338 fprintf(stderr
, "maxcpus must be equal to or greater than "
5342 if (max_cpus
> 255) {
5343 fprintf(stderr
, "Unsupported number of maxcpus\n");
5347 case QEMU_OPTION_vnc
:
5348 display_type
= DT_VNC
;
5349 vnc_display
= optarg
;
5352 case QEMU_OPTION_no_acpi
:
5355 case QEMU_OPTION_no_hpet
:
5358 case QEMU_OPTION_balloon
:
5359 if (balloon_parse(optarg
) < 0) {
5360 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5365 case QEMU_OPTION_no_reboot
:
5368 case QEMU_OPTION_no_shutdown
:
5371 case QEMU_OPTION_show_cursor
:
5374 case QEMU_OPTION_uuid
:
5375 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5376 fprintf(stderr
, "Fail to parse UUID string."
5377 " Wrong format.\n");
5382 case QEMU_OPTION_daemonize
:
5386 case QEMU_OPTION_option_rom
:
5387 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5388 fprintf(stderr
, "Too many option ROMs\n");
5391 option_rom
[nb_option_roms
] = optarg
;
5394 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5395 case QEMU_OPTION_semihosting
:
5396 semihosting_enabled
= 1;
5399 case QEMU_OPTION_name
:
5400 qemu_name
= qemu_strdup(optarg
);
5402 char *p
= strchr(qemu_name
, ',');
5405 if (strncmp(p
, "process=", 8)) {
5406 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5414 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5415 case QEMU_OPTION_prom_env
:
5416 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5417 fprintf(stderr
, "Too many prom variables\n");
5420 prom_envs
[nb_prom_envs
] = optarg
;
5425 case QEMU_OPTION_old_param
:
5429 case QEMU_OPTION_clock
:
5430 configure_alarms(optarg
);
5432 case QEMU_OPTION_startdate
:
5433 configure_rtc_date_offset(optarg
, 1);
5435 case QEMU_OPTION_rtc
:
5436 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5438 fprintf(stderr
, "parse error: %s\n", optarg
);
5441 configure_rtc(opts
);
5443 case QEMU_OPTION_tb_size
:
5444 tb_size
= strtol(optarg
, NULL
, 0);
5448 case QEMU_OPTION_icount
:
5450 if (strcmp(optarg
, "auto") == 0) {
5451 icount_time_shift
= -1;
5453 icount_time_shift
= strtol(optarg
, NULL
, 0);
5456 case QEMU_OPTION_incoming
:
5459 case QEMU_OPTION_nodefaults
:
5461 default_parallel
= 0;
5462 default_virtcon
= 0;
5463 default_monitor
= 0;
5471 case QEMU_OPTION_chroot
:
5472 chroot_dir
= optarg
;
5474 case QEMU_OPTION_runas
:
5479 case QEMU_OPTION_xen_domid
:
5480 xen_domid
= atoi(optarg
);
5482 case QEMU_OPTION_xen_create
:
5483 xen_mode
= XEN_CREATE
;
5485 case QEMU_OPTION_xen_attach
:
5486 xen_mode
= XEN_ATTACH
;
5489 case QEMU_OPTION_readconfig
:
5492 fp
= fopen(optarg
, "r");
5494 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5497 if (qemu_config_parse(fp
) != 0) {
5503 case QEMU_OPTION_writeconfig
:
5506 if (strcmp(optarg
, "-") == 0) {
5509 fp
= fopen(optarg
, "w");
5511 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5515 qemu_config_write(fp
);
5523 /* If no data_dir is specified then try to find it relative to the
5526 data_dir
= find_datadir(argv
[0]);
5528 /* If all else fails use the install patch specified when building. */
5530 data_dir
= CONFIG_QEMU_SHAREDIR
;
5534 * Default to max_cpus = smp_cpus, in case the user doesn't
5535 * specify a max_cpus value.
5538 max_cpus
= smp_cpus
;
5540 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5541 if (smp_cpus
> machine
->max_cpus
) {
5542 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5543 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5548 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5549 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5551 if (machine
->no_serial
) {
5554 if (machine
->no_parallel
) {
5555 default_parallel
= 0;
5557 if (!machine
->use_virtcon
) {
5558 default_virtcon
= 0;
5560 if (machine
->no_vga
) {
5563 if (machine
->no_floppy
) {
5566 if (machine
->no_cdrom
) {
5569 if (machine
->no_sdcard
) {
5573 if (display_type
== DT_NOGRAPHIC
) {
5574 if (default_parallel
)
5575 add_device_config(DEV_PARALLEL
, "null");
5576 if (default_serial
&& default_monitor
) {
5577 add_device_config(DEV_SERIAL
, "mon:stdio");
5578 } else if (default_virtcon
&& default_monitor
) {
5579 add_device_config(DEV_VIRTCON
, "mon:stdio");
5582 add_device_config(DEV_SERIAL
, "stdio");
5583 if (default_virtcon
)
5584 add_device_config(DEV_VIRTCON
, "stdio");
5585 if (default_monitor
)
5586 monitor_parse("stdio", "readline");
5590 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5591 if (default_parallel
)
5592 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5593 if (default_monitor
)
5594 monitor_parse("vc:80Cx24C", "readline");
5595 if (default_virtcon
)
5596 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5599 vga_interface_type
= VGA_CIRRUS
;
5601 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5608 if (pipe(fds
) == -1)
5619 len
= read(fds
[0], &status
, 1);
5620 if (len
== -1 && (errno
== EINTR
))
5625 else if (status
== 1) {
5626 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5634 qemu_set_cloexec(fds
[1]);
5646 signal(SIGTSTP
, SIG_IGN
);
5647 signal(SIGTTOU
, SIG_IGN
);
5648 signal(SIGTTIN
, SIG_IGN
);
5652 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5656 if (write(fds
[1], &status
, 1) != 1) {
5657 perror("daemonize. Writing to pipe\n");
5661 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5665 if (kvm_enabled()) {
5668 ret
= kvm_init(smp_cpus
);
5670 fprintf(stderr
, "failed to initialize KVM\n");
5675 if (qemu_init_main_loop()) {
5676 fprintf(stderr
, "qemu_init_main_loop failed\n");
5679 linux_boot
= (kernel_filename
!= NULL
);
5681 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5682 fprintf(stderr
, "-append only allowed with -kernel option\n");
5686 if (!linux_boot
&& initrd_filename
!= NULL
) {
5687 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5692 /* Win32 doesn't support line-buffering and requires size >= 2 */
5693 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5696 if (init_timer_alarm() < 0) {
5697 fprintf(stderr
, "could not initialize alarm timer\n");
5700 if (use_icount
&& icount_time_shift
< 0) {
5702 /* 125MIPS seems a reasonable initial guess at the guest speed.
5703 It will be corrected fairly quickly anyway. */
5704 icount_time_shift
= 3;
5705 init_icount_adjust();
5712 if (net_init_clients() < 0) {
5716 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5717 net_set_boot_mask(net_boot
);
5719 /* init the bluetooth world */
5720 if (foreach_device_config(DEV_BT
, bt_parse
))
5723 /* init the memory */
5725 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5727 /* init the dynamic translator */
5728 cpu_exec_init_all(tb_size
* 1024 * 1024);
5730 bdrv_init_with_whitelist();
5734 if (default_cdrom
) {
5735 /* we always create the cdrom drive, even if no disk is there */
5736 drive_add(NULL
, CDROM_ALIAS
);
5739 if (default_floppy
) {
5740 /* we always create at least one floppy */
5741 drive_add(NULL
, FD_ALIAS
, 0);
5744 if (default_sdcard
) {
5745 /* we always create one sd slot, even if no card is in it */
5746 drive_add(NULL
, SD_ALIAS
);
5749 /* open the virtual block devices */
5751 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5752 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5755 vmstate_register(0, &vmstate_timers
,&timers_state
);
5756 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5759 if (nb_numa_nodes
> 0) {
5762 if (nb_numa_nodes
> smp_cpus
) {
5763 nb_numa_nodes
= smp_cpus
;
5766 /* If no memory size if given for any node, assume the default case
5767 * and distribute the available memory equally across all nodes
5769 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5770 if (node_mem
[i
] != 0)
5773 if (i
== nb_numa_nodes
) {
5774 uint64_t usedmem
= 0;
5776 /* On Linux, the each node's border has to be 8MB aligned,
5777 * the final node gets the rest.
5779 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5780 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5781 usedmem
+= node_mem
[i
];
5783 node_mem
[i
] = ram_size
- usedmem
;
5786 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5787 if (node_cpumask
[i
] != 0)
5790 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5791 * must cope with this anyway, because there are BIOSes out there in
5792 * real machines which also use this scheme.
5794 if (i
== nb_numa_nodes
) {
5795 for (i
= 0; i
< smp_cpus
; i
++) {
5796 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5801 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5803 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5805 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5807 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5810 module_call_init(MODULE_INIT_DEVICE
);
5813 i
= select_watchdog(watchdog
);
5815 exit (i
== 1 ? 1 : 0);
5818 if (machine
->compat_props
) {
5819 qdev_prop_register_global_list(machine
->compat_props
);
5823 machine
->init(ram_size
, boot_devices
,
5824 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5828 /* must be after terminal init, SDL library changes signal handlers */
5832 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5833 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5834 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5840 current_machine
= machine
;
5842 /* init USB devices */
5844 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5848 /* init generic devices */
5849 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5853 dumb_display_init();
5854 /* just use the first displaystate for the moment */
5857 if (display_type
== DT_DEFAULT
) {
5858 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5859 display_type
= DT_SDL
;
5861 display_type
= DT_VNC
;
5862 vnc_display
= "localhost:0,to=99";
5868 switch (display_type
) {
5871 #if defined(CONFIG_CURSES)
5873 curses_display_init(ds
, full_screen
);
5876 #if defined(CONFIG_SDL)
5878 sdl_display_init(ds
, full_screen
, no_frame
);
5880 #elif defined(CONFIG_COCOA)
5882 cocoa_display_init(ds
, full_screen
);
5886 vnc_display_init(ds
);
5887 if (vnc_display_open(ds
, vnc_display
) < 0)
5890 if (show_vnc_port
) {
5891 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5899 dcl
= ds
->listeners
;
5900 while (dcl
!= NULL
) {
5901 if (dcl
->dpy_refresh
!= NULL
) {
5902 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5903 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5908 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5909 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5910 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5913 text_consoles_set_display(display_state
);
5915 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5918 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5919 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5924 qdev_machine_creation_done();
5926 if (rom_load_all() != 0) {
5927 fprintf(stderr
, "rom loading failed\n");
5931 qemu_system_reset();
5933 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5939 qemu_start_incoming_migration(incoming
);
5940 } else if (autostart
) {
5950 len
= write(fds
[1], &status
, 1);
5951 if (len
== -1 && (errno
== EINTR
))
5958 perror("not able to chdir to /");
5961 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5967 pwd
= getpwnam(run_as
);
5969 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5975 if (chroot(chroot_dir
) < 0) {
5976 fprintf(stderr
, "chroot failed\n");
5980 perror("not able to chdir to /");
5986 if (setgid(pwd
->pw_gid
) < 0) {
5987 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5990 if (setuid(pwd
->pw_uid
) < 0) {
5991 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5994 if (setuid(0) != -1) {
5995 fprintf(stderr
, "Dropping privileges failed\n");