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 DisplayType display_type
= DT_DEFAULT
;
186 const char* keyboard_layout
= NULL
;
188 const char *mem_path
= NULL
;
190 int mem_prealloc
= 0; /* force preallocation of physical target memory */
193 NICInfo nd_table
[MAX_NICS
];
196 static int rtc_utc
= 1;
197 static int rtc_date_offset
= -1; /* -1 means no change */
198 QEMUClock
*rtc_clock
;
199 int vga_interface_type
= VGA_NONE
;
201 int graphic_width
= 1024;
202 int graphic_height
= 768;
203 int graphic_depth
= 8;
205 int graphic_width
= 800;
206 int graphic_height
= 600;
207 int graphic_depth
= 15;
209 static int full_screen
= 0;
211 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
216 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
218 int win2k_install_hack
= 0;
227 const char *vnc_display
;
228 int acpi_enabled
= 1;
234 int graphic_rotate
= 0;
235 uint8_t irq0override
= 1;
239 const char *watchdog
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
249 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
250 unsigned int nb_prom_envs
= 0;
251 const char *prom_envs
[MAX_PROM_ENVS
];
256 uint64_t node_mem
[MAX_NODES
];
257 uint64_t node_cpumask
[MAX_NODES
];
259 static CPUState
*cur_cpu
;
260 static CPUState
*next_cpu
;
261 static int timer_alarm_pending
= 1;
262 /* Conversion factor from emulated instructions to virtual clock ticks. */
263 static int icount_time_shift
;
264 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
265 #define MAX_ICOUNT_SHIFT 10
266 /* Compensate for varying guest execution speed. */
267 static int64_t qemu_icount_bias
;
268 static QEMUTimer
*icount_rt_timer
;
269 static QEMUTimer
*icount_vm_timer
;
270 static QEMUTimer
*nographic_timer
;
272 uint8_t qemu_uuid
[16];
274 static QEMUBootSetHandler
*boot_set_handler
;
275 static void *boot_set_opaque
;
278 #define SIG_IPI (SIGRTMIN+4)
280 #define SIG_IPI SIGUSR1
283 static int default_serial
= 1;
284 static int default_parallel
= 1;
285 static int default_virtcon
= 1;
286 static int default_monitor
= 1;
287 static int default_vga
= 1;
288 static int default_floppy
= 1;
289 static int default_cdrom
= 1;
290 static int default_sdcard
= 1;
296 { .driver
= "isa-serial", .flag
= &default_serial
},
297 { .driver
= "isa-parallel", .flag
= &default_parallel
},
298 { .driver
= "isa-fdc", .flag
= &default_floppy
},
299 { .driver
= "ide-drive", .flag
= &default_cdrom
},
300 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
301 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
302 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
303 { .driver
= "VGA", .flag
= &default_vga
},
304 { .driver
= "cirrus-vga", .flag
= &default_vga
},
305 { .driver
= "vmware-svga", .flag
= &default_vga
},
308 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
310 const char *driver
= qemu_opt_get(opts
, "driver");
315 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
316 if (strcmp(default_list
[i
].driver
, driver
) != 0)
318 *(default_list
[i
].flag
) = 0;
323 /***********************************************************/
324 /* x86 ISA bus support */
326 target_phys_addr_t isa_mem_base
= 0;
329 /***********************************************************/
330 void hw_error(const char *fmt
, ...)
336 fprintf(stderr
, "qemu: hardware error: ");
337 vfprintf(stderr
, fmt
, ap
);
338 fprintf(stderr
, "\n");
339 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
340 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
342 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
344 cpu_dump_state(env
, stderr
, fprintf
, 0);
351 static void set_proc_name(const char *s
)
353 #if defined(__linux__) && defined(PR_SET_NAME)
357 name
[sizeof(name
) - 1] = 0;
358 strncpy(name
, s
, sizeof(name
));
359 /* Could rewrite argv[0] too, but that's a bit more complicated.
360 This simple way is enough for `top'. */
361 prctl(PR_SET_NAME
, name
);
368 static QEMUBalloonEvent
*qemu_balloon_event
;
369 void *qemu_balloon_event_opaque
;
371 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
373 qemu_balloon_event
= func
;
374 qemu_balloon_event_opaque
= opaque
;
377 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
379 if (qemu_balloon_event
) {
380 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
387 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
389 if (qemu_balloon_event
) {
390 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
398 /***********************************************************/
399 /* real time host monotonic timer */
401 /* compute with 96 bit intermediate result: (a*b)/c */
402 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
407 #ifdef HOST_WORDS_BIGENDIAN
417 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
418 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
421 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
425 static int64_t get_clock_realtime(void)
429 gettimeofday(&tv
, NULL
);
430 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
435 static int64_t clock_freq
;
437 static void init_get_clock(void)
441 ret
= QueryPerformanceFrequency(&freq
);
443 fprintf(stderr
, "Could not calibrate ticks\n");
446 clock_freq
= freq
.QuadPart
;
449 static int64_t get_clock(void)
452 QueryPerformanceCounter(&ti
);
453 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
458 static int use_rt_clock
;
460 static void init_get_clock(void)
463 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
464 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
467 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
474 static int64_t get_clock(void)
476 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
477 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
480 clock_gettime(CLOCK_MONOTONIC
, &ts
);
481 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
485 /* XXX: using gettimeofday leads to problems if the date
486 changes, so it should be avoided. */
487 return get_clock_realtime();
492 /* Return the virtual CPU time, based on the instruction counter. */
493 static int64_t cpu_get_icount(void)
496 CPUState
*env
= cpu_single_env
;;
497 icount
= qemu_icount
;
500 fprintf(stderr
, "Bad clock read\n");
501 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
503 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
506 /***********************************************************/
507 /* guest cycle counter */
509 typedef struct TimersState
{
510 int64_t cpu_ticks_prev
;
511 int64_t cpu_ticks_offset
;
512 int64_t cpu_clock_offset
;
513 int32_t cpu_ticks_enabled
;
517 TimersState timers_state
;
519 /* return the host CPU cycle counter and handle stop/restart */
520 int64_t cpu_get_ticks(void)
523 return cpu_get_icount();
525 if (!timers_state
.cpu_ticks_enabled
) {
526 return timers_state
.cpu_ticks_offset
;
529 ticks
= cpu_get_real_ticks();
530 if (timers_state
.cpu_ticks_prev
> ticks
) {
531 /* Note: non increasing ticks may happen if the host uses
533 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
535 timers_state
.cpu_ticks_prev
= ticks
;
536 return ticks
+ timers_state
.cpu_ticks_offset
;
540 /* return the host CPU monotonic timer and handle stop/restart */
541 static int64_t cpu_get_clock(void)
544 if (!timers_state
.cpu_ticks_enabled
) {
545 return timers_state
.cpu_clock_offset
;
548 return ti
+ timers_state
.cpu_clock_offset
;
552 /* enable cpu_get_ticks() */
553 void cpu_enable_ticks(void)
555 if (!timers_state
.cpu_ticks_enabled
) {
556 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
557 timers_state
.cpu_clock_offset
-= get_clock();
558 timers_state
.cpu_ticks_enabled
= 1;
562 /* disable cpu_get_ticks() : the clock is stopped. You must not call
563 cpu_get_ticks() after that. */
564 void cpu_disable_ticks(void)
566 if (timers_state
.cpu_ticks_enabled
) {
567 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
568 timers_state
.cpu_clock_offset
= cpu_get_clock();
569 timers_state
.cpu_ticks_enabled
= 0;
573 /***********************************************************/
576 #define QEMU_CLOCK_REALTIME 0
577 #define QEMU_CLOCK_VIRTUAL 1
578 #define QEMU_CLOCK_HOST 2
582 /* XXX: add frequency */
590 struct QEMUTimer
*next
;
593 struct qemu_alarm_timer
{
597 int (*start
)(struct qemu_alarm_timer
*t
);
598 void (*stop
)(struct qemu_alarm_timer
*t
);
599 void (*rearm
)(struct qemu_alarm_timer
*t
);
603 #define ALARM_FLAG_DYNTICKS 0x1
604 #define ALARM_FLAG_EXPIRED 0x2
606 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
608 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
611 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
613 if (!alarm_has_dynticks(t
))
619 /* TODO: MIN_TIMER_REARM_US should be optimized */
620 #define MIN_TIMER_REARM_US 250
622 static struct qemu_alarm_timer
*alarm_timer
;
626 struct qemu_alarm_win32
{
629 } alarm_win32_data
= {0, -1};
631 static int win32_start_timer(struct qemu_alarm_timer
*t
);
632 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
633 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
637 static int unix_start_timer(struct qemu_alarm_timer
*t
);
638 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
642 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
643 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
644 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
646 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
647 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
649 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
650 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
652 #endif /* __linux__ */
656 /* Correlation between real and virtual time is always going to be
657 fairly approximate, so ignore small variation.
658 When the guest is idle real and virtual time will be aligned in
660 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
662 static void icount_adjust(void)
667 static int64_t last_delta
;
668 /* If the VM is not running, then do nothing. */
672 cur_time
= cpu_get_clock();
673 cur_icount
= qemu_get_clock(vm_clock
);
674 delta
= cur_icount
- cur_time
;
675 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
677 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
678 && icount_time_shift
> 0) {
679 /* The guest is getting too far ahead. Slow time down. */
683 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
684 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
685 /* The guest is getting too far behind. Speed time up. */
689 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
692 static void icount_adjust_rt(void * opaque
)
694 qemu_mod_timer(icount_rt_timer
,
695 qemu_get_clock(rt_clock
) + 1000);
699 static void icount_adjust_vm(void * opaque
)
701 qemu_mod_timer(icount_vm_timer
,
702 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
706 static void init_icount_adjust(void)
708 /* Have both realtime and virtual time triggers for speed adjustment.
709 The realtime trigger catches emulated time passing too slowly,
710 the virtual time trigger catches emulated time passing too fast.
711 Realtime triggers occur even when idle, so use them less frequently
713 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
714 qemu_mod_timer(icount_rt_timer
,
715 qemu_get_clock(rt_clock
) + 1000);
716 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
717 qemu_mod_timer(icount_vm_timer
,
718 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
721 static struct qemu_alarm_timer alarm_timers
[] = {
724 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
725 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
726 /* HPET - if available - is preferred */
727 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
728 /* ...otherwise try RTC */
729 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
731 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
733 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
734 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
735 {"win32", 0, win32_start_timer
,
736 win32_stop_timer
, NULL
, &alarm_win32_data
},
741 static void show_available_alarms(void)
745 printf("Available alarm timers, in order of precedence:\n");
746 for (i
= 0; alarm_timers
[i
].name
; i
++)
747 printf("%s\n", alarm_timers
[i
].name
);
750 static void configure_alarms(char const *opt
)
754 int count
= ARRAY_SIZE(alarm_timers
) - 1;
757 struct qemu_alarm_timer tmp
;
759 if (!strcmp(opt
, "?")) {
760 show_available_alarms();
764 arg
= qemu_strdup(opt
);
766 /* Reorder the array */
767 name
= strtok(arg
, ",");
769 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
770 if (!strcmp(alarm_timers
[i
].name
, name
))
775 fprintf(stderr
, "Unknown clock %s\n", name
);
784 tmp
= alarm_timers
[i
];
785 alarm_timers
[i
] = alarm_timers
[cur
];
786 alarm_timers
[cur
] = tmp
;
790 name
= strtok(NULL
, ",");
796 /* Disable remaining timers */
797 for (i
= cur
; i
< count
; i
++)
798 alarm_timers
[i
].name
= NULL
;
800 show_available_alarms();
805 #define QEMU_NUM_CLOCKS 3
809 QEMUClock
*host_clock
;
811 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
813 static QEMUClock
*qemu_new_clock(int type
)
816 clock
= qemu_mallocz(sizeof(QEMUClock
));
821 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
825 ts
= qemu_mallocz(sizeof(QEMUTimer
));
832 void qemu_free_timer(QEMUTimer
*ts
)
837 /* stop a timer, but do not dealloc it */
838 void qemu_del_timer(QEMUTimer
*ts
)
842 /* NOTE: this code must be signal safe because
843 qemu_timer_expired() can be called from a signal. */
844 pt
= &active_timers
[ts
->clock
->type
];
857 /* modify the current timer so that it will be fired when current_time
858 >= expire_time. The corresponding callback will be called. */
859 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
865 /* add the timer in the sorted list */
866 /* NOTE: this code must be signal safe because
867 qemu_timer_expired() can be called from a signal. */
868 pt
= &active_timers
[ts
->clock
->type
];
873 if (t
->expire_time
> expire_time
)
877 ts
->expire_time
= expire_time
;
881 /* Rearm if necessary */
882 if (pt
== &active_timers
[ts
->clock
->type
]) {
883 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
884 qemu_rearm_alarm_timer(alarm_timer
);
886 /* Interrupt execution to force deadline recalculation. */
892 int qemu_timer_pending(QEMUTimer
*ts
)
895 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
902 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
906 return (timer_head
->expire_time
<= current_time
);
909 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
915 if (!ts
|| ts
->expire_time
> current_time
)
917 /* remove timer from the list before calling the callback */
918 *ptimer_head
= ts
->next
;
921 /* run the callback (the timer list can be modified) */
926 int64_t qemu_get_clock(QEMUClock
*clock
)
928 switch(clock
->type
) {
929 case QEMU_CLOCK_REALTIME
:
930 return get_clock() / 1000000;
932 case QEMU_CLOCK_VIRTUAL
:
934 return cpu_get_icount();
936 return cpu_get_clock();
938 case QEMU_CLOCK_HOST
:
939 return get_clock_realtime();
943 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
945 switch(clock
->type
) {
946 case QEMU_CLOCK_REALTIME
:
949 case QEMU_CLOCK_VIRTUAL
:
951 return cpu_get_icount();
953 return cpu_get_clock();
955 case QEMU_CLOCK_HOST
:
956 return get_clock_realtime();
960 static void init_clocks(void)
963 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
964 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
965 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
967 rtc_clock
= host_clock
;
971 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
973 uint64_t expire_time
;
975 if (qemu_timer_pending(ts
)) {
976 expire_time
= ts
->expire_time
;
980 qemu_put_be64(f
, expire_time
);
983 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
985 uint64_t expire_time
;
987 expire_time
= qemu_get_be64(f
);
988 if (expire_time
!= -1) {
989 qemu_mod_timer(ts
, expire_time
);
995 static const VMStateDescription vmstate_timers
= {
998 .minimum_version_id
= 1,
999 .minimum_version_id_old
= 1,
1000 .fields
= (VMStateField
[]) {
1001 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1002 VMSTATE_INT64(dummy
, TimersState
),
1003 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1004 VMSTATE_END_OF_LIST()
1008 static void qemu_event_increment(void);
1011 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1012 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1015 static void host_alarm_handler(int host_signum
)
1019 #define DISP_FREQ 1000
1021 static int64_t delta_min
= INT64_MAX
;
1022 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1024 ti
= qemu_get_clock(vm_clock
);
1025 if (last_clock
!= 0) {
1026 delta
= ti
- last_clock
;
1027 if (delta
< delta_min
)
1029 if (delta
> delta_max
)
1032 if (++count
== DISP_FREQ
) {
1033 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1034 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1035 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1036 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1037 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1039 delta_min
= INT64_MAX
;
1047 if (alarm_has_dynticks(alarm_timer
) ||
1049 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1050 qemu_get_clock(vm_clock
))) ||
1051 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1052 qemu_get_clock(rt_clock
)) ||
1053 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1054 qemu_get_clock(host_clock
))) {
1055 qemu_event_increment();
1056 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1058 #ifndef CONFIG_IOTHREAD
1060 /* stop the currently executing cpu because a timer occured */
1064 timer_alarm_pending
= 1;
1065 qemu_notify_event();
1069 static int64_t qemu_next_deadline(void)
1071 /* To avoid problems with overflow limit this to 2^32. */
1072 int64_t delta
= INT32_MAX
;
1074 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1075 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1076 qemu_get_clock(vm_clock
);
1078 if (active_timers
[QEMU_CLOCK_HOST
]) {
1079 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1080 qemu_get_clock(host_clock
);
1091 #if defined(__linux__)
1092 static uint64_t qemu_next_deadline_dyntick(void)
1100 delta
= (qemu_next_deadline() + 999) / 1000;
1102 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1103 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1104 qemu_get_clock(rt_clock
))*1000;
1105 if (rtdelta
< delta
)
1109 if (delta
< MIN_TIMER_REARM_US
)
1110 delta
= MIN_TIMER_REARM_US
;
1118 /* Sets a specific flag */
1119 static int fcntl_setfl(int fd
, int flag
)
1123 flags
= fcntl(fd
, F_GETFL
);
1127 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1133 #if defined(__linux__)
1135 #define RTC_FREQ 1024
1137 static void enable_sigio_timer(int fd
)
1139 struct sigaction act
;
1142 sigfillset(&act
.sa_mask
);
1144 act
.sa_handler
= host_alarm_handler
;
1146 sigaction(SIGIO
, &act
, NULL
);
1147 fcntl_setfl(fd
, O_ASYNC
);
1148 fcntl(fd
, F_SETOWN
, getpid());
1151 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1153 struct hpet_info info
;
1156 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1161 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1163 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1164 "error, but for better emulation accuracy type:\n"
1165 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1169 /* Check capabilities */
1170 r
= ioctl(fd
, HPET_INFO
, &info
);
1174 /* Enable periodic mode */
1175 r
= ioctl(fd
, HPET_EPI
, 0);
1176 if (info
.hi_flags
&& (r
< 0))
1179 /* Enable interrupt */
1180 r
= ioctl(fd
, HPET_IE_ON
, 0);
1184 enable_sigio_timer(fd
);
1185 t
->priv
= (void *)(long)fd
;
1193 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1195 int fd
= (long)t
->priv
;
1200 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1203 unsigned long current_rtc_freq
= 0;
1205 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1208 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1209 if (current_rtc_freq
!= RTC_FREQ
&&
1210 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1211 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1212 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1213 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1216 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1222 enable_sigio_timer(rtc_fd
);
1224 t
->priv
= (void *)(long)rtc_fd
;
1229 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1231 int rtc_fd
= (long)t
->priv
;
1236 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1240 struct sigaction act
;
1242 sigfillset(&act
.sa_mask
);
1244 act
.sa_handler
= host_alarm_handler
;
1246 sigaction(SIGALRM
, &act
, NULL
);
1249 * Initialize ev struct to 0 to avoid valgrind complaining
1250 * about uninitialized data in timer_create call
1252 memset(&ev
, 0, sizeof(ev
));
1253 ev
.sigev_value
.sival_int
= 0;
1254 ev
.sigev_notify
= SIGEV_SIGNAL
;
1255 ev
.sigev_signo
= SIGALRM
;
1257 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1258 perror("timer_create");
1260 /* disable dynticks */
1261 fprintf(stderr
, "Dynamic Ticks disabled\n");
1266 t
->priv
= (void *)(long)host_timer
;
1271 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1273 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1275 timer_delete(host_timer
);
1278 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1280 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1281 struct itimerspec timeout
;
1282 int64_t nearest_delta_us
= INT64_MAX
;
1285 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1286 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1287 !active_timers
[QEMU_CLOCK_HOST
])
1290 nearest_delta_us
= qemu_next_deadline_dyntick();
1292 /* check whether a timer is already running */
1293 if (timer_gettime(host_timer
, &timeout
)) {
1295 fprintf(stderr
, "Internal timer error: aborting\n");
1298 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1299 if (current_us
&& current_us
<= nearest_delta_us
)
1302 timeout
.it_interval
.tv_sec
= 0;
1303 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1304 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1305 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1306 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1308 fprintf(stderr
, "Internal timer error: aborting\n");
1313 #endif /* defined(__linux__) */
1315 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1317 struct sigaction act
;
1318 struct itimerval itv
;
1322 sigfillset(&act
.sa_mask
);
1324 act
.sa_handler
= host_alarm_handler
;
1326 sigaction(SIGALRM
, &act
, NULL
);
1328 itv
.it_interval
.tv_sec
= 0;
1329 /* for i386 kernel 2.6 to get 1 ms */
1330 itv
.it_interval
.tv_usec
= 999;
1331 itv
.it_value
.tv_sec
= 0;
1332 itv
.it_value
.tv_usec
= 10 * 1000;
1334 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1341 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1343 struct itimerval itv
;
1345 memset(&itv
, 0, sizeof(itv
));
1346 setitimer(ITIMER_REAL
, &itv
, NULL
);
1349 #endif /* !defined(_WIN32) */
1354 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1357 struct qemu_alarm_win32
*data
= t
->priv
;
1360 memset(&tc
, 0, sizeof(tc
));
1361 timeGetDevCaps(&tc
, sizeof(tc
));
1363 if (data
->period
< tc
.wPeriodMin
)
1364 data
->period
= tc
.wPeriodMin
;
1366 timeBeginPeriod(data
->period
);
1368 flags
= TIME_CALLBACK_FUNCTION
;
1369 if (alarm_has_dynticks(t
))
1370 flags
|= TIME_ONESHOT
;
1372 flags
|= TIME_PERIODIC
;
1374 data
->timerId
= timeSetEvent(1, // interval (ms)
1375 data
->period
, // resolution
1376 host_alarm_handler
, // function
1377 (DWORD
)t
, // parameter
1380 if (!data
->timerId
) {
1381 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1383 timeEndPeriod(data
->period
);
1390 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1392 struct qemu_alarm_win32
*data
= t
->priv
;
1394 timeKillEvent(data
->timerId
);
1395 timeEndPeriod(data
->period
);
1398 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1400 struct qemu_alarm_win32
*data
= t
->priv
;
1402 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1403 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1404 !active_timers
[QEMU_CLOCK_HOST
])
1407 timeKillEvent(data
->timerId
);
1409 data
->timerId
= timeSetEvent(1,
1413 TIME_ONESHOT
| TIME_PERIODIC
);
1415 if (!data
->timerId
) {
1416 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1419 timeEndPeriod(data
->period
);
1426 static int init_timer_alarm(void)
1428 struct qemu_alarm_timer
*t
= NULL
;
1431 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1432 t
= &alarm_timers
[i
];
1452 static void quit_timers(void)
1454 alarm_timer
->stop(alarm_timer
);
1458 /***********************************************************/
1459 /* host time/date access */
1460 void qemu_get_timedate(struct tm
*tm
, int offset
)
1467 if (rtc_date_offset
== -1) {
1471 ret
= localtime(&ti
);
1473 ti
-= rtc_date_offset
;
1477 memcpy(tm
, ret
, sizeof(struct tm
));
1480 int qemu_timedate_diff(struct tm
*tm
)
1484 if (rtc_date_offset
== -1)
1486 seconds
= mktimegm(tm
);
1488 seconds
= mktime(tm
);
1490 seconds
= mktimegm(tm
) + rtc_date_offset
;
1492 return seconds
- time(NULL
);
1495 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1497 time_t rtc_start_date
;
1500 if (!strcmp(startdate
, "now") && legacy
) {
1501 rtc_date_offset
= -1;
1503 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1511 } else if (sscanf(startdate
, "%d-%d-%d",
1514 &tm
.tm_mday
) == 3) {
1523 rtc_start_date
= mktimegm(&tm
);
1524 if (rtc_start_date
== -1) {
1526 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1527 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1530 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1534 static void configure_rtc(QemuOpts
*opts
)
1538 value
= qemu_opt_get(opts
, "base");
1540 if (!strcmp(value
, "utc")) {
1542 } else if (!strcmp(value
, "localtime")) {
1545 configure_rtc_date_offset(value
, 0);
1548 value
= qemu_opt_get(opts
, "clock");
1550 if (!strcmp(value
, "host")) {
1551 rtc_clock
= host_clock
;
1552 } else if (!strcmp(value
, "vm")) {
1553 rtc_clock
= vm_clock
;
1555 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1559 #ifdef CONFIG_TARGET_I386
1560 value
= qemu_opt_get(opts
, "driftfix");
1562 if (!strcmp(buf
, "slew")) {
1564 } else if (!strcmp(buf
, "none")) {
1567 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1575 static void socket_cleanup(void)
1580 static int socket_init(void)
1585 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1587 err
= WSAGetLastError();
1588 fprintf(stderr
, "WSAStartup: %d\n", err
);
1591 atexit(socket_cleanup
);
1596 /***********************************************************/
1597 /* Bluetooth support */
1600 static struct HCIInfo
*hci_table
[MAX_NICS
];
1602 static struct bt_vlan_s
{
1603 struct bt_scatternet_s net
;
1605 struct bt_vlan_s
*next
;
1608 /* find or alloc a new bluetooth "VLAN" */
1609 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1611 struct bt_vlan_s
**pvlan
, *vlan
;
1612 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1616 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1618 pvlan
= &first_bt_vlan
;
1619 while (*pvlan
!= NULL
)
1620 pvlan
= &(*pvlan
)->next
;
1625 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1629 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1634 static struct HCIInfo null_hci
= {
1635 .cmd_send
= null_hci_send
,
1636 .sco_send
= null_hci_send
,
1637 .acl_send
= null_hci_send
,
1638 .bdaddr_set
= null_hci_addr_set
,
1641 struct HCIInfo
*qemu_next_hci(void)
1643 if (cur_hci
== nb_hcis
)
1646 return hci_table
[cur_hci
++];
1649 static struct HCIInfo
*hci_init(const char *str
)
1652 struct bt_scatternet_s
*vlan
= 0;
1654 if (!strcmp(str
, "null"))
1657 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1659 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1660 else if (!strncmp(str
, "hci", 3)) {
1663 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1664 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1669 vlan
= qemu_find_bt_vlan(0);
1671 return bt_new_hci(vlan
);
1674 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1679 static int bt_hci_parse(const char *str
)
1681 struct HCIInfo
*hci
;
1684 if (nb_hcis
>= MAX_NICS
) {
1685 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1689 hci
= hci_init(str
);
1698 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1699 hci
->bdaddr_set(hci
, bdaddr
.b
);
1701 hci_table
[nb_hcis
++] = hci
;
1706 static void bt_vhci_add(int vlan_id
)
1708 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1711 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1712 "an empty scatternet %i\n", vlan_id
);
1714 bt_vhci_init(bt_new_hci(vlan
));
1717 static struct bt_device_s
*bt_device_add(const char *opt
)
1719 struct bt_scatternet_s
*vlan
;
1721 char *endp
= strstr(opt
, ",vlan=");
1722 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1725 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1728 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1730 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1735 vlan
= qemu_find_bt_vlan(vlan_id
);
1738 fprintf(stderr
, "qemu: warning: adding a slave device to "
1739 "an empty scatternet %i\n", vlan_id
);
1741 if (!strcmp(devname
, "keyboard"))
1742 return bt_keyboard_init(vlan
);
1744 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1748 static int bt_parse(const char *opt
)
1750 const char *endp
, *p
;
1753 if (strstart(opt
, "hci", &endp
)) {
1754 if (!*endp
|| *endp
== ',') {
1756 if (!strstart(endp
, ",vlan=", 0))
1759 return bt_hci_parse(opt
);
1761 } else if (strstart(opt
, "vhci", &endp
)) {
1762 if (!*endp
|| *endp
== ',') {
1764 if (strstart(endp
, ",vlan=", &p
)) {
1765 vlan
= strtol(p
, (char **) &endp
, 0);
1767 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1771 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1780 } else if (strstart(opt
, "device:", &endp
))
1781 return !bt_device_add(endp
);
1783 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1787 /***********************************************************/
1788 /* QEMU Block devices */
1790 #define HD_ALIAS "index=%d,media=disk"
1791 #define CDROM_ALIAS "index=2,media=cdrom"
1792 #define FD_ALIAS "index=%d,if=floppy"
1793 #define PFLASH_ALIAS "if=pflash"
1794 #define MTD_ALIAS "if=mtd"
1795 #define SD_ALIAS "index=0,if=sd"
1797 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1804 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1807 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1809 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1810 __FUNCTION__
, optstr
);
1814 qemu_opt_set(opts
, "file", file
);
1818 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1822 /* seek interface, bus and unit */
1824 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1825 if (dinfo
->type
== type
&&
1826 dinfo
->bus
== bus
&&
1827 dinfo
->unit
== unit
)
1834 DriveInfo
*drive_get_by_id(const char *id
)
1838 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1839 if (strcmp(id
, dinfo
->id
))
1846 int drive_get_max_bus(BlockInterfaceType type
)
1852 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1853 if(dinfo
->type
== type
&&
1854 dinfo
->bus
> max_bus
)
1855 max_bus
= dinfo
->bus
;
1860 const char *drive_get_serial(BlockDriverState
*bdrv
)
1864 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1865 if (dinfo
->bdrv
== bdrv
)
1866 return dinfo
->serial
;
1872 BlockInterfaceErrorAction
drive_get_on_error(
1873 BlockDriverState
*bdrv
, int is_read
)
1877 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1878 if (dinfo
->bdrv
== bdrv
)
1879 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
1882 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
1885 static void bdrv_format_print(void *opaque
, const char *name
)
1887 fprintf(stderr
, " %s", name
);
1890 void drive_uninit(DriveInfo
*dinfo
)
1892 qemu_opts_del(dinfo
->opts
);
1893 bdrv_delete(dinfo
->bdrv
);
1894 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1898 static int parse_block_error_action(const char *buf
, int is_read
)
1900 if (!strcmp(buf
, "ignore")) {
1901 return BLOCK_ERR_IGNORE
;
1902 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
1903 return BLOCK_ERR_STOP_ENOSPC
;
1904 } else if (!strcmp(buf
, "stop")) {
1905 return BLOCK_ERR_STOP_ANY
;
1906 } else if (!strcmp(buf
, "report")) {
1907 return BLOCK_ERR_REPORT
;
1909 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
1910 buf
, is_read
? "read" : "write");
1915 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1919 const char *file
= NULL
;
1922 const char *mediastr
= "";
1923 BlockInterfaceType type
;
1924 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1925 int bus_id
, unit_id
;
1926 int cyls
, heads
, secs
, translation
;
1927 BlockDriver
*drv
= NULL
;
1928 QEMUMachine
*machine
= opaque
;
1935 int on_read_error
, on_write_error
;
1936 const char *devaddr
;
1942 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1945 if (machine
&& machine
->use_scsi
) {
1947 max_devs
= MAX_SCSI_DEVS
;
1948 pstrcpy(devname
, sizeof(devname
), "scsi");
1951 max_devs
= MAX_IDE_DEVS
;
1952 pstrcpy(devname
, sizeof(devname
), "ide");
1956 /* extract parameters */
1957 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1958 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1959 index
= qemu_opt_get_number(opts
, "index", -1);
1961 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1962 heads
= qemu_opt_get_number(opts
, "heads", 0);
1963 secs
= qemu_opt_get_number(opts
, "secs", 0);
1965 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1966 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
1968 file
= qemu_opt_get(opts
, "file");
1969 serial
= qemu_opt_get(opts
, "serial");
1971 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1972 pstrcpy(devname
, sizeof(devname
), buf
);
1973 if (!strcmp(buf
, "ide")) {
1975 max_devs
= MAX_IDE_DEVS
;
1976 } else if (!strcmp(buf
, "scsi")) {
1978 max_devs
= MAX_SCSI_DEVS
;
1979 } else if (!strcmp(buf
, "floppy")) {
1982 } else if (!strcmp(buf
, "pflash")) {
1985 } else if (!strcmp(buf
, "mtd")) {
1988 } else if (!strcmp(buf
, "sd")) {
1991 } else if (!strcmp(buf
, "virtio")) {
1994 } else if (!strcmp(buf
, "xen")) {
1997 } else if (!strcmp(buf
, "none")) {
2001 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2006 if (cyls
|| heads
|| secs
) {
2007 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2008 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2011 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2012 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2015 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2016 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2021 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2024 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2028 if (!strcmp(buf
, "none"))
2029 translation
= BIOS_ATA_TRANSLATION_NONE
;
2030 else if (!strcmp(buf
, "lba"))
2031 translation
= BIOS_ATA_TRANSLATION_LBA
;
2032 else if (!strcmp(buf
, "auto"))
2033 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2035 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2040 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2041 if (!strcmp(buf
, "disk")) {
2043 } else if (!strcmp(buf
, "cdrom")) {
2044 if (cyls
|| secs
|| heads
) {
2046 "qemu: '%s' invalid physical CHS format\n", buf
);
2049 media
= MEDIA_CDROM
;
2051 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2056 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2057 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2059 else if (!strcmp(buf
, "writethrough"))
2061 else if (!strcmp(buf
, "writeback"))
2064 fprintf(stderr
, "qemu: invalid cache option\n");
2069 #ifdef CONFIG_LINUX_AIO
2070 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2071 if (!strcmp(buf
, "threads"))
2073 else if (!strcmp(buf
, "native"))
2076 fprintf(stderr
, "qemu: invalid aio option\n");
2082 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2083 if (strcmp(buf
, "?") == 0) {
2084 fprintf(stderr
, "qemu: Supported formats:");
2085 bdrv_iterate_format(bdrv_format_print
, NULL
);
2086 fprintf(stderr
, "\n");
2089 drv
= bdrv_find_whitelisted_format(buf
);
2091 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2096 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2097 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2098 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2099 fprintf(stderr
, "werror is no supported by this format\n");
2103 on_write_error
= parse_block_error_action(buf
, 0);
2104 if (on_write_error
< 0) {
2109 on_read_error
= BLOCK_ERR_REPORT
;
2110 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2111 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2112 fprintf(stderr
, "rerror is no supported by this format\n");
2116 on_read_error
= parse_block_error_action(buf
, 1);
2117 if (on_read_error
< 0) {
2122 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2123 if (type
!= IF_VIRTIO
) {
2124 fprintf(stderr
, "addr is not supported\n");
2129 /* compute bus and unit according index */
2132 if (bus_id
!= 0 || unit_id
!= -1) {
2134 "qemu: index cannot be used with bus and unit\n");
2142 unit_id
= index
% max_devs
;
2143 bus_id
= index
/ max_devs
;
2147 /* if user doesn't specify a unit_id,
2148 * try to find the first free
2151 if (unit_id
== -1) {
2153 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2155 if (max_devs
&& unit_id
>= max_devs
) {
2156 unit_id
-= max_devs
;
2164 if (max_devs
&& unit_id
>= max_devs
) {
2165 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2166 unit_id
, max_devs
- 1);
2171 * ignore multiple definitions
2174 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2181 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2182 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2183 dinfo
->id
= qemu_strdup(buf
);
2185 /* no id supplied -> create one */
2186 dinfo
->id
= qemu_mallocz(32);
2187 if (type
== IF_IDE
|| type
== IF_SCSI
)
2188 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2190 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2191 devname
, bus_id
, mediastr
, unit_id
);
2193 snprintf(dinfo
->id
, 32, "%s%s%i",
2194 devname
, mediastr
, unit_id
);
2196 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2197 dinfo
->devaddr
= devaddr
;
2199 dinfo
->bus
= bus_id
;
2200 dinfo
->unit
= unit_id
;
2201 dinfo
->on_read_error
= on_read_error
;
2202 dinfo
->on_write_error
= on_write_error
;
2205 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2206 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2216 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2217 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2221 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2226 /* FIXME: This isn't really a floppy, but it's a reasonable
2229 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2235 /* add virtio block device */
2236 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2237 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2238 qemu_opt_set(opts
, "drive", dinfo
->id
);
2240 qemu_opt_set(opts
, "addr", devaddr
);
2251 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2252 cache
= 2; /* always use write-back with snapshot */
2254 if (cache
== 0) /* no caching */
2255 bdrv_flags
|= BDRV_O_NOCACHE
;
2256 else if (cache
== 2) /* write-back */
2257 bdrv_flags
|= BDRV_O_CACHE_WB
;
2260 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2262 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2266 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
2267 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
2272 * cdrom is read-only. Set it now, after above interface checking
2273 * since readonly attribute not explicitly required, so no error.
2275 if (media
== MEDIA_CDROM
) {
2278 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
2280 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2281 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2282 file
, strerror(errno
));
2286 if (bdrv_key_required(dinfo
->bdrv
))
2292 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2294 QEMUMachine
*machine
= opaque
;
2295 int fatal_error
= 0;
2297 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2304 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2306 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2307 qemu_opt_set(opts
, "snapshot", "on");
2312 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2314 boot_set_handler
= func
;
2315 boot_set_opaque
= opaque
;
2318 int qemu_boot_set(const char *boot_devices
)
2320 if (!boot_set_handler
) {
2323 return boot_set_handler(boot_set_opaque
, boot_devices
);
2326 static int parse_bootdevices(char *devices
)
2328 /* We just do some generic consistency checks */
2332 for (p
= devices
; *p
!= '\0'; p
++) {
2333 /* Allowed boot devices are:
2334 * a-b: floppy disk drives
2335 * c-f: IDE disk drives
2336 * g-m: machine implementation dependant drives
2337 * n-p: network devices
2338 * It's up to each machine implementation to check if the given boot
2339 * devices match the actual hardware implementation and firmware
2342 if (*p
< 'a' || *p
> 'p') {
2343 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2346 if (bitmap
& (1 << (*p
- 'a'))) {
2347 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2350 bitmap
|= 1 << (*p
- 'a');
2355 static void restore_boot_devices(void *opaque
)
2357 char *standard_boot_devices
= opaque
;
2359 qemu_boot_set(standard_boot_devices
);
2361 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2362 qemu_free(standard_boot_devices
);
2365 static void numa_add(const char *optarg
)
2369 unsigned long long value
, endvalue
;
2372 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2373 if (!strcmp(option
, "node")) {
2374 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2375 nodenr
= nb_numa_nodes
;
2377 nodenr
= strtoull(option
, NULL
, 10);
2380 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2381 node_mem
[nodenr
] = 0;
2383 value
= strtoull(option
, &endptr
, 0);
2385 case 0: case 'M': case 'm':
2392 node_mem
[nodenr
] = value
;
2394 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2395 node_cpumask
[nodenr
] = 0;
2397 value
= strtoull(option
, &endptr
, 10);
2400 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2402 if (*endptr
== '-') {
2403 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2404 if (endvalue
>= 63) {
2407 "only 63 CPUs in NUMA mode supported.\n");
2409 value
= (2ULL << endvalue
) - (1ULL << value
);
2411 value
= 1ULL << value
;
2414 node_cpumask
[nodenr
] = value
;
2421 static void smp_parse(const char *optarg
)
2423 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2427 smp
= strtoul(optarg
, &endptr
, 10);
2428 if (endptr
!= optarg
) {
2429 if (*endptr
== ',') {
2433 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2434 sockets
= strtoull(option
, NULL
, 10);
2435 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2436 cores
= strtoull(option
, NULL
, 10);
2437 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2438 threads
= strtoull(option
, NULL
, 10);
2439 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2440 max_cpus
= strtoull(option
, NULL
, 10);
2442 /* compute missing values, prefer sockets over cores over threads */
2443 if (smp
== 0 || sockets
== 0) {
2444 sockets
= sockets
> 0 ? sockets
: 1;
2445 cores
= cores
> 0 ? cores
: 1;
2446 threads
= threads
> 0 ? threads
: 1;
2448 smp
= cores
* threads
* sockets
;
2452 threads
= threads
> 0 ? threads
: 1;
2453 cores
= smp
/ (sockets
* threads
);
2456 threads
= smp
/ (cores
* sockets
);
2461 smp_cores
= cores
> 0 ? cores
: 1;
2462 smp_threads
= threads
> 0 ? threads
: 1;
2464 max_cpus
= smp_cpus
;
2467 /***********************************************************/
2470 static int usb_device_add(const char *devname
, int is_hotplug
)
2473 USBDevice
*dev
= NULL
;
2478 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2479 dev
= usbdevice_create(devname
);
2483 /* the other ones */
2484 if (strstart(devname
, "host:", &p
)) {
2485 dev
= usb_host_device_open(p
);
2486 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2487 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2488 bt_new_hci(qemu_find_bt_vlan(0)));
2499 static int usb_device_del(const char *devname
)
2504 if (strstart(devname
, "host:", &p
))
2505 return usb_host_device_close(p
);
2510 p
= strchr(devname
, '.');
2513 bus_num
= strtoul(devname
, NULL
, 0);
2514 addr
= strtoul(p
+ 1, NULL
, 0);
2516 return usb_device_delete_addr(bus_num
, addr
);
2519 static int usb_parse(const char *cmdline
)
2522 r
= usb_device_add(cmdline
, 0);
2524 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
2529 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2531 const char *devname
= qdict_get_str(qdict
, "devname");
2532 if (usb_device_add(devname
, 1) < 0) {
2533 qemu_error("could not add USB device '%s'\n", devname
);
2537 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2539 const char *devname
= qdict_get_str(qdict
, "devname");
2540 if (usb_device_del(devname
) < 0) {
2541 qemu_error("could not delete USB device '%s'\n", devname
);
2545 /***********************************************************/
2546 /* PCMCIA/Cardbus */
2548 static struct pcmcia_socket_entry_s
{
2549 PCMCIASocket
*socket
;
2550 struct pcmcia_socket_entry_s
*next
;
2551 } *pcmcia_sockets
= 0;
2553 void pcmcia_socket_register(PCMCIASocket
*socket
)
2555 struct pcmcia_socket_entry_s
*entry
;
2557 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2558 entry
->socket
= socket
;
2559 entry
->next
= pcmcia_sockets
;
2560 pcmcia_sockets
= entry
;
2563 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2565 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2567 ptr
= &pcmcia_sockets
;
2568 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2569 if (entry
->socket
== socket
) {
2575 void pcmcia_info(Monitor
*mon
)
2577 struct pcmcia_socket_entry_s
*iter
;
2579 if (!pcmcia_sockets
)
2580 monitor_printf(mon
, "No PCMCIA sockets\n");
2582 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2583 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2584 iter
->socket
->attached
? iter
->socket
->card_string
:
2588 /***********************************************************/
2591 typedef struct IOHandlerRecord
{
2593 IOCanRWHandler
*fd_read_poll
;
2595 IOHandler
*fd_write
;
2598 /* temporary data */
2600 struct IOHandlerRecord
*next
;
2603 static IOHandlerRecord
*first_io_handler
;
2605 /* XXX: fd_read_poll should be suppressed, but an API change is
2606 necessary in the character devices to suppress fd_can_read(). */
2607 int qemu_set_fd_handler2(int fd
,
2608 IOCanRWHandler
*fd_read_poll
,
2610 IOHandler
*fd_write
,
2613 IOHandlerRecord
**pioh
, *ioh
;
2615 if (!fd_read
&& !fd_write
) {
2616 pioh
= &first_io_handler
;
2621 if (ioh
->fd
== fd
) {
2628 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2632 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2633 ioh
->next
= first_io_handler
;
2634 first_io_handler
= ioh
;
2637 ioh
->fd_read_poll
= fd_read_poll
;
2638 ioh
->fd_read
= fd_read
;
2639 ioh
->fd_write
= fd_write
;
2640 ioh
->opaque
= opaque
;
2646 int qemu_set_fd_handler(int fd
,
2648 IOHandler
*fd_write
,
2651 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2655 /***********************************************************/
2656 /* Polling handling */
2658 typedef struct PollingEntry
{
2661 struct PollingEntry
*next
;
2664 static PollingEntry
*first_polling_entry
;
2666 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2668 PollingEntry
**ppe
, *pe
;
2669 pe
= qemu_mallocz(sizeof(PollingEntry
));
2671 pe
->opaque
= opaque
;
2672 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2677 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2679 PollingEntry
**ppe
, *pe
;
2680 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2682 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2690 /***********************************************************/
2691 /* Wait objects support */
2692 typedef struct WaitObjects
{
2694 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2695 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2696 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2699 static WaitObjects wait_objects
= {0};
2701 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2703 WaitObjects
*w
= &wait_objects
;
2705 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2707 w
->events
[w
->num
] = handle
;
2708 w
->func
[w
->num
] = func
;
2709 w
->opaque
[w
->num
] = opaque
;
2714 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2717 WaitObjects
*w
= &wait_objects
;
2720 for (i
= 0; i
< w
->num
; i
++) {
2721 if (w
->events
[i
] == handle
)
2724 w
->events
[i
] = w
->events
[i
+ 1];
2725 w
->func
[i
] = w
->func
[i
+ 1];
2726 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2734 /***********************************************************/
2735 /* ram save/restore */
2737 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2738 #define RAM_SAVE_FLAG_COMPRESS 0x02
2739 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2740 #define RAM_SAVE_FLAG_PAGE 0x08
2741 #define RAM_SAVE_FLAG_EOS 0x10
2743 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2745 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2746 uint32_t *array
= (uint32_t *)page
;
2749 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2750 if (array
[i
] != val
)
2757 static int ram_save_block(QEMUFile
*f
)
2759 static ram_addr_t current_addr
= 0;
2760 ram_addr_t saved_addr
= current_addr
;
2761 ram_addr_t addr
= 0;
2764 while (addr
< last_ram_offset
) {
2765 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2768 cpu_physical_memory_reset_dirty(current_addr
,
2769 current_addr
+ TARGET_PAGE_SIZE
,
2770 MIGRATION_DIRTY_FLAG
);
2772 p
= qemu_get_ram_ptr(current_addr
);
2774 if (is_dup_page(p
, *p
)) {
2775 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2776 qemu_put_byte(f
, *p
);
2778 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2779 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2785 addr
+= TARGET_PAGE_SIZE
;
2786 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2792 static uint64_t bytes_transferred
;
2794 static ram_addr_t
ram_save_remaining(void)
2797 ram_addr_t count
= 0;
2799 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2800 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2807 uint64_t ram_bytes_remaining(void)
2809 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2812 uint64_t ram_bytes_transferred(void)
2814 return bytes_transferred
;
2817 uint64_t ram_bytes_total(void)
2819 return last_ram_offset
;
2822 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2825 uint64_t bytes_transferred_last
;
2827 uint64_t expected_time
= 0;
2830 cpu_physical_memory_set_dirty_tracking(0);
2834 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2835 qemu_file_set_error(f
);
2840 bytes_transferred
= 0;
2842 /* Make sure all dirty bits are set */
2843 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2844 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2845 cpu_physical_memory_set_dirty(addr
);
2848 /* Enable dirty memory tracking */
2849 cpu_physical_memory_set_dirty_tracking(1);
2851 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2854 bytes_transferred_last
= bytes_transferred
;
2855 bwidth
= qemu_get_clock_ns(rt_clock
);
2857 while (!qemu_file_rate_limit(f
)) {
2860 ret
= ram_save_block(f
);
2861 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2862 if (ret
== 0) /* no more blocks */
2866 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
2867 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2869 /* if we haven't transferred anything this round, force expected_time to a
2870 * a very high value, but without crashing */
2874 /* try transferring iterative blocks of memory */
2876 /* flush all remaining blocks regardless of rate limiting */
2877 while (ram_save_block(f
) != 0) {
2878 bytes_transferred
+= TARGET_PAGE_SIZE
;
2880 cpu_physical_memory_set_dirty_tracking(0);
2883 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2885 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2887 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2890 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2895 if (version_id
!= 3)
2899 addr
= qemu_get_be64(f
);
2901 flags
= addr
& ~TARGET_PAGE_MASK
;
2902 addr
&= TARGET_PAGE_MASK
;
2904 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2905 if (addr
!= last_ram_offset
)
2909 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2910 uint8_t ch
= qemu_get_byte(f
);
2911 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2914 (!kvm_enabled() || kvm_has_sync_mmu())) {
2915 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2918 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
2919 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2921 if (qemu_file_has_error(f
)) {
2924 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2929 void qemu_service_io(void)
2931 qemu_notify_event();
2934 /***********************************************************/
2935 /* machine registration */
2937 static QEMUMachine
*first_machine
= NULL
;
2938 QEMUMachine
*current_machine
= NULL
;
2940 int qemu_register_machine(QEMUMachine
*m
)
2943 pm
= &first_machine
;
2951 static QEMUMachine
*find_machine(const char *name
)
2955 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2956 if (!strcmp(m
->name
, name
))
2958 if (m
->alias
&& !strcmp(m
->alias
, name
))
2964 static QEMUMachine
*find_default_machine(void)
2968 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2969 if (m
->is_default
) {
2976 /***********************************************************/
2977 /* main execution loop */
2979 static void gui_update(void *opaque
)
2981 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2982 DisplayState
*ds
= opaque
;
2983 DisplayChangeListener
*dcl
= ds
->listeners
;
2985 qemu_flush_coalesced_mmio_buffer();
2988 while (dcl
!= NULL
) {
2989 if (dcl
->gui_timer_interval
&&
2990 dcl
->gui_timer_interval
< interval
)
2991 interval
= dcl
->gui_timer_interval
;
2994 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
2997 static void nographic_update(void *opaque
)
2999 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3001 qemu_flush_coalesced_mmio_buffer();
3002 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3005 struct vm_change_state_entry
{
3006 VMChangeStateHandler
*cb
;
3008 QLIST_ENTRY (vm_change_state_entry
) entries
;
3011 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3013 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3016 VMChangeStateEntry
*e
;
3018 e
= qemu_mallocz(sizeof (*e
));
3022 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3026 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3028 QLIST_REMOVE (e
, entries
);
3032 static void vm_state_notify(int running
, int reason
)
3034 VMChangeStateEntry
*e
;
3036 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3037 e
->cb(e
->opaque
, running
, reason
);
3041 static void resume_all_vcpus(void);
3042 static void pause_all_vcpus(void);
3049 vm_state_notify(1, 0);
3050 qemu_rearm_alarm_timer(alarm_timer
);
3055 /* reset/shutdown handler */
3057 typedef struct QEMUResetEntry
{
3058 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3059 QEMUResetHandler
*func
;
3063 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3064 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3065 static int reset_requested
;
3066 static int shutdown_requested
;
3067 static int powerdown_requested
;
3068 static int debug_requested
;
3069 static int vmstop_requested
;
3071 int qemu_shutdown_requested(void)
3073 int r
= shutdown_requested
;
3074 shutdown_requested
= 0;
3078 int qemu_reset_requested(void)
3080 int r
= reset_requested
;
3081 reset_requested
= 0;
3085 int qemu_powerdown_requested(void)
3087 int r
= powerdown_requested
;
3088 powerdown_requested
= 0;
3092 static int qemu_debug_requested(void)
3094 int r
= debug_requested
;
3095 debug_requested
= 0;
3099 static int qemu_vmstop_requested(void)
3101 int r
= vmstop_requested
;
3102 vmstop_requested
= 0;
3106 static void do_vm_stop(int reason
)
3109 cpu_disable_ticks();
3112 vm_state_notify(0, reason
);
3116 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3118 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3121 re
->opaque
= opaque
;
3122 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3125 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3129 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3130 if (re
->func
== func
&& re
->opaque
== opaque
) {
3131 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3138 void qemu_system_reset(void)
3140 QEMUResetEntry
*re
, *nre
;
3142 /* reset all devices */
3143 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3144 re
->func(re
->opaque
);
3148 void qemu_system_reset_request(void)
3151 shutdown_requested
= 1;
3153 reset_requested
= 1;
3155 qemu_notify_event();
3158 void qemu_system_shutdown_request(void)
3160 shutdown_requested
= 1;
3161 qemu_notify_event();
3164 void qemu_system_powerdown_request(void)
3166 powerdown_requested
= 1;
3167 qemu_notify_event();
3170 #ifdef CONFIG_IOTHREAD
3171 static void qemu_system_vmstop_request(int reason
)
3173 vmstop_requested
= reason
;
3174 qemu_notify_event();
3179 static int io_thread_fd
= -1;
3181 static void qemu_event_increment(void)
3183 /* Write 8 bytes to be compatible with eventfd. */
3184 static uint64_t val
= 1;
3187 if (io_thread_fd
== -1)
3191 ret
= write(io_thread_fd
, &val
, sizeof(val
));
3192 } while (ret
< 0 && errno
== EINTR
);
3194 /* EAGAIN is fine, a read must be pending. */
3195 if (ret
< 0 && errno
!= EAGAIN
) {
3196 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
3202 static void qemu_event_read(void *opaque
)
3204 int fd
= (unsigned long)opaque
;
3208 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
3210 len
= read(fd
, buffer
, sizeof(buffer
));
3211 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
3214 static int qemu_event_init(void)
3219 err
= qemu_eventfd(fds
);
3223 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3227 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3231 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3232 (void *)(unsigned long)fds
[0]);
3234 io_thread_fd
= fds
[1];
3243 HANDLE qemu_event_handle
;
3245 static void dummy_event_handler(void *opaque
)
3249 static int qemu_event_init(void)
3251 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3252 if (!qemu_event_handle
) {
3253 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3256 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3260 static void qemu_event_increment(void)
3262 if (!SetEvent(qemu_event_handle
)) {
3263 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3270 static int cpu_can_run(CPUState
*env
)
3281 #ifndef CONFIG_IOTHREAD
3282 static int qemu_init_main_loop(void)
3284 return qemu_event_init();
3287 void qemu_init_vcpu(void *_env
)
3289 CPUState
*env
= _env
;
3291 env
->nr_cores
= smp_cores
;
3292 env
->nr_threads
= smp_threads
;
3298 int qemu_cpu_self(void *env
)
3303 static void resume_all_vcpus(void)
3307 static void pause_all_vcpus(void)
3311 void qemu_cpu_kick(void *env
)
3316 void qemu_notify_event(void)
3318 CPUState
*env
= cpu_single_env
;
3325 void qemu_mutex_lock_iothread(void) {}
3326 void qemu_mutex_unlock_iothread(void) {}
3328 void vm_stop(int reason
)
3333 #else /* CONFIG_IOTHREAD */
3335 #include "qemu-thread.h"
3337 QemuMutex qemu_global_mutex
;
3338 static QemuMutex qemu_fair_mutex
;
3340 static QemuThread io_thread
;
3342 static QemuThread
*tcg_cpu_thread
;
3343 static QemuCond
*tcg_halt_cond
;
3345 static int qemu_system_ready
;
3347 static QemuCond qemu_cpu_cond
;
3349 static QemuCond qemu_system_cond
;
3350 static QemuCond qemu_pause_cond
;
3352 static void tcg_block_io_signals(void);
3353 static void kvm_block_io_signals(CPUState
*env
);
3354 static void unblock_io_signals(void);
3355 static int tcg_has_work(void);
3356 static int cpu_has_work(CPUState
*env
);
3358 static int qemu_init_main_loop(void)
3362 ret
= qemu_event_init();
3366 qemu_cond_init(&qemu_pause_cond
);
3367 qemu_mutex_init(&qemu_fair_mutex
);
3368 qemu_mutex_init(&qemu_global_mutex
);
3369 qemu_mutex_lock(&qemu_global_mutex
);
3371 unblock_io_signals();
3372 qemu_thread_self(&io_thread
);
3377 static void qemu_wait_io_event_common(CPUState
*env
)
3382 qemu_cond_signal(&qemu_pause_cond
);
3386 static void qemu_wait_io_event(CPUState
*env
)
3388 while (!tcg_has_work())
3389 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3391 qemu_mutex_unlock(&qemu_global_mutex
);
3394 * Users of qemu_global_mutex can be starved, having no chance
3395 * to acquire it since this path will get to it first.
3396 * So use another lock to provide fairness.
3398 qemu_mutex_lock(&qemu_fair_mutex
);
3399 qemu_mutex_unlock(&qemu_fair_mutex
);
3401 qemu_mutex_lock(&qemu_global_mutex
);
3402 qemu_wait_io_event_common(env
);
3405 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
3412 ts
.tv_sec
= timeout
/ 1000;
3413 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
3415 sigemptyset(&waitset
);
3416 sigaddset(&waitset
, SIG_IPI
);
3418 qemu_mutex_unlock(&qemu_global_mutex
);
3419 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
3421 qemu_mutex_lock(&qemu_global_mutex
);
3423 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
3424 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
3429 static void qemu_kvm_wait_io_event(CPUState
*env
)
3431 while (!cpu_has_work(env
))
3432 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3434 qemu_kvm_eat_signal(env
, 0);
3435 qemu_wait_io_event_common(env
);
3438 static int qemu_cpu_exec(CPUState
*env
);
3440 static void *kvm_cpu_thread_fn(void *arg
)
3442 CPUState
*env
= arg
;
3444 qemu_thread_self(env
->thread
);
3448 kvm_block_io_signals(env
);
3450 /* signal CPU creation */
3451 qemu_mutex_lock(&qemu_global_mutex
);
3453 qemu_cond_signal(&qemu_cpu_cond
);
3455 /* and wait for machine initialization */
3456 while (!qemu_system_ready
)
3457 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3460 if (cpu_can_run(env
))
3462 qemu_kvm_wait_io_event(env
);
3468 static void tcg_cpu_exec(void);
3470 static void *tcg_cpu_thread_fn(void *arg
)
3472 CPUState
*env
= arg
;
3474 tcg_block_io_signals();
3475 qemu_thread_self(env
->thread
);
3477 /* signal CPU creation */
3478 qemu_mutex_lock(&qemu_global_mutex
);
3479 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3481 qemu_cond_signal(&qemu_cpu_cond
);
3483 /* and wait for machine initialization */
3484 while (!qemu_system_ready
)
3485 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3489 qemu_wait_io_event(cur_cpu
);
3495 void qemu_cpu_kick(void *_env
)
3497 CPUState
*env
= _env
;
3498 qemu_cond_broadcast(env
->halt_cond
);
3500 qemu_thread_signal(env
->thread
, SIG_IPI
);
3503 int qemu_cpu_self(void *_env
)
3505 CPUState
*env
= _env
;
3508 qemu_thread_self(&this);
3510 return qemu_thread_equal(&this, env
->thread
);
3513 static void cpu_signal(int sig
)
3516 cpu_exit(cpu_single_env
);
3519 static void tcg_block_io_signals(void)
3522 struct sigaction sigact
;
3525 sigaddset(&set
, SIGUSR2
);
3526 sigaddset(&set
, SIGIO
);
3527 sigaddset(&set
, SIGALRM
);
3528 sigaddset(&set
, SIGCHLD
);
3529 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3532 sigaddset(&set
, SIG_IPI
);
3533 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3535 memset(&sigact
, 0, sizeof(sigact
));
3536 sigact
.sa_handler
= cpu_signal
;
3537 sigaction(SIG_IPI
, &sigact
, NULL
);
3540 static void dummy_signal(int sig
)
3544 static void kvm_block_io_signals(CPUState
*env
)
3548 struct sigaction sigact
;
3551 sigaddset(&set
, SIGUSR2
);
3552 sigaddset(&set
, SIGIO
);
3553 sigaddset(&set
, SIGALRM
);
3554 sigaddset(&set
, SIGCHLD
);
3555 sigaddset(&set
, SIG_IPI
);
3556 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3558 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
3559 sigdelset(&set
, SIG_IPI
);
3561 memset(&sigact
, 0, sizeof(sigact
));
3562 sigact
.sa_handler
= dummy_signal
;
3563 sigaction(SIG_IPI
, &sigact
, NULL
);
3565 r
= kvm_set_signal_mask(env
, &set
);
3567 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
3572 static void unblock_io_signals(void)
3577 sigaddset(&set
, SIGUSR2
);
3578 sigaddset(&set
, SIGIO
);
3579 sigaddset(&set
, SIGALRM
);
3580 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3583 sigaddset(&set
, SIG_IPI
);
3584 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3587 static void qemu_signal_lock(unsigned int msecs
)
3589 qemu_mutex_lock(&qemu_fair_mutex
);
3591 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3592 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
3593 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3596 qemu_mutex_unlock(&qemu_fair_mutex
);
3599 void qemu_mutex_lock_iothread(void)
3601 if (kvm_enabled()) {
3602 qemu_mutex_lock(&qemu_fair_mutex
);
3603 qemu_mutex_lock(&qemu_global_mutex
);
3604 qemu_mutex_unlock(&qemu_fair_mutex
);
3606 qemu_signal_lock(100);
3609 void qemu_mutex_unlock_iothread(void)
3611 qemu_mutex_unlock(&qemu_global_mutex
);
3614 static int all_vcpus_paused(void)
3616 CPUState
*penv
= first_cpu
;
3621 penv
= (CPUState
*)penv
->next_cpu
;
3627 static void pause_all_vcpus(void)
3629 CPUState
*penv
= first_cpu
;
3633 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3634 qemu_cpu_kick(penv
);
3635 penv
= (CPUState
*)penv
->next_cpu
;
3638 while (!all_vcpus_paused()) {
3639 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3642 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3643 penv
= (CPUState
*)penv
->next_cpu
;
3648 static void resume_all_vcpus(void)
3650 CPUState
*penv
= first_cpu
;
3655 qemu_thread_signal(penv
->thread
, SIG_IPI
);
3656 qemu_cpu_kick(penv
);
3657 penv
= (CPUState
*)penv
->next_cpu
;
3661 static void tcg_init_vcpu(void *_env
)
3663 CPUState
*env
= _env
;
3664 /* share a single thread for all cpus with TCG */
3665 if (!tcg_cpu_thread
) {
3666 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3667 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3668 qemu_cond_init(env
->halt_cond
);
3669 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3670 while (env
->created
== 0)
3671 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3672 tcg_cpu_thread
= env
->thread
;
3673 tcg_halt_cond
= env
->halt_cond
;
3675 env
->thread
= tcg_cpu_thread
;
3676 env
->halt_cond
= tcg_halt_cond
;
3680 static void kvm_start_vcpu(CPUState
*env
)
3682 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3683 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3684 qemu_cond_init(env
->halt_cond
);
3685 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3686 while (env
->created
== 0)
3687 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3690 void qemu_init_vcpu(void *_env
)
3692 CPUState
*env
= _env
;
3694 env
->nr_cores
= smp_cores
;
3695 env
->nr_threads
= smp_threads
;
3697 kvm_start_vcpu(env
);
3702 void qemu_notify_event(void)
3704 qemu_event_increment();
3707 void vm_stop(int reason
)
3710 qemu_thread_self(&me
);
3712 if (!qemu_thread_equal(&me
, &io_thread
)) {
3713 qemu_system_vmstop_request(reason
);
3715 * FIXME: should not return to device code in case
3716 * vm_stop() has been requested.
3718 if (cpu_single_env
) {
3719 cpu_exit(cpu_single_env
);
3720 cpu_single_env
->stop
= 1;
3731 static void host_main_loop_wait(int *timeout
)
3737 /* XXX: need to suppress polling by better using win32 events */
3739 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3740 ret
|= pe
->func(pe
->opaque
);
3744 WaitObjects
*w
= &wait_objects
;
3746 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3747 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3748 if (w
->func
[ret
- WAIT_OBJECT_0
])
3749 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3751 /* Check for additional signaled events */
3752 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3754 /* Check if event is signaled */
3755 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3756 if(ret2
== WAIT_OBJECT_0
) {
3758 w
->func
[i
](w
->opaque
[i
]);
3759 } else if (ret2
== WAIT_TIMEOUT
) {
3761 err
= GetLastError();
3762 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3765 } else if (ret
== WAIT_TIMEOUT
) {
3767 err
= GetLastError();
3768 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3775 static void host_main_loop_wait(int *timeout
)
3780 void main_loop_wait(int timeout
)
3782 IOHandlerRecord
*ioh
;
3783 fd_set rfds
, wfds
, xfds
;
3787 qemu_bh_update_timeout(&timeout
);
3789 host_main_loop_wait(&timeout
);
3791 /* poll any events */
3792 /* XXX: separate device handlers from system ones */
3797 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3801 (!ioh
->fd_read_poll
||
3802 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3803 FD_SET(ioh
->fd
, &rfds
);
3807 if (ioh
->fd_write
) {
3808 FD_SET(ioh
->fd
, &wfds
);
3814 tv
.tv_sec
= timeout
/ 1000;
3815 tv
.tv_usec
= (timeout
% 1000) * 1000;
3817 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3819 qemu_mutex_unlock_iothread();
3820 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3821 qemu_mutex_lock_iothread();
3823 IOHandlerRecord
**pioh
;
3825 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3826 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3827 ioh
->fd_read(ioh
->opaque
);
3829 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3830 ioh
->fd_write(ioh
->opaque
);
3834 /* remove deleted IO handlers */
3835 pioh
= &first_io_handler
;
3846 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3848 /* rearm timer, if not periodic */
3849 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3850 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3851 qemu_rearm_alarm_timer(alarm_timer
);
3854 /* vm time timers */
3856 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3857 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3858 qemu_get_clock(vm_clock
));
3861 /* real time timers */
3862 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3863 qemu_get_clock(rt_clock
));
3865 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3866 qemu_get_clock(host_clock
));
3868 /* Check bottom-halves last in case any of the earlier events triggered
3874 static int qemu_cpu_exec(CPUState
*env
)
3877 #ifdef CONFIG_PROFILER
3881 #ifdef CONFIG_PROFILER
3882 ti
= profile_getclock();
3887 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3888 env
->icount_decr
.u16
.low
= 0;
3889 env
->icount_extra
= 0;
3890 count
= qemu_next_deadline();
3891 count
= (count
+ (1 << icount_time_shift
) - 1)
3892 >> icount_time_shift
;
3893 qemu_icount
+= count
;
3894 decr
= (count
> 0xffff) ? 0xffff : count
;
3896 env
->icount_decr
.u16
.low
= decr
;
3897 env
->icount_extra
= count
;
3899 ret
= cpu_exec(env
);
3900 #ifdef CONFIG_PROFILER
3901 qemu_time
+= profile_getclock() - ti
;
3904 /* Fold pending instructions back into the
3905 instruction counter, and clear the interrupt flag. */
3906 qemu_icount
-= (env
->icount_decr
.u16
.low
3907 + env
->icount_extra
);
3908 env
->icount_decr
.u32
= 0;
3909 env
->icount_extra
= 0;
3914 static void tcg_cpu_exec(void)
3918 if (next_cpu
== NULL
)
3919 next_cpu
= first_cpu
;
3920 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3921 CPUState
*env
= cur_cpu
= next_cpu
;
3923 if (timer_alarm_pending
) {
3924 timer_alarm_pending
= 0;
3927 if (cpu_can_run(env
))
3928 ret
= qemu_cpu_exec(env
);
3932 if (ret
== EXCP_DEBUG
) {
3933 gdb_set_stop_cpu(env
);
3934 debug_requested
= 1;
3940 static int cpu_has_work(CPUState
*env
)
3948 if (qemu_cpu_has_work(env
))
3953 static int tcg_has_work(void)
3957 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3958 if (cpu_has_work(env
))
3963 static int qemu_calculate_timeout(void)
3965 #ifndef CONFIG_IOTHREAD
3970 else if (tcg_has_work())
3972 else if (!use_icount
)
3975 /* XXX: use timeout computed from timers */
3978 /* Advance virtual time to the next event. */
3979 if (use_icount
== 1) {
3980 /* When not using an adaptive execution frequency
3981 we tend to get badly out of sync with real time,
3982 so just delay for a reasonable amount of time. */
3985 delta
= cpu_get_icount() - cpu_get_clock();
3988 /* If virtual time is ahead of real time then just
3990 timeout
= (delta
/ 1000000) + 1;
3992 /* Wait for either IO to occur or the next
3994 add
= qemu_next_deadline();
3995 /* We advance the timer before checking for IO.
3996 Limit the amount we advance so that early IO
3997 activity won't get the guest too far ahead. */
4001 add
= (add
+ (1 << icount_time_shift
) - 1)
4002 >> icount_time_shift
;
4004 timeout
= delta
/ 1000000;
4011 #else /* CONFIG_IOTHREAD */
4016 static int vm_can_run(void)
4018 if (powerdown_requested
)
4020 if (reset_requested
)
4022 if (shutdown_requested
)
4024 if (debug_requested
)
4029 qemu_irq qemu_system_powerdown
;
4031 static void main_loop(void)
4035 #ifdef CONFIG_IOTHREAD
4036 qemu_system_ready
= 1;
4037 qemu_cond_broadcast(&qemu_system_cond
);
4042 #ifdef CONFIG_PROFILER
4045 #ifndef CONFIG_IOTHREAD
4048 #ifdef CONFIG_PROFILER
4049 ti
= profile_getclock();
4051 main_loop_wait(qemu_calculate_timeout());
4052 #ifdef CONFIG_PROFILER
4053 dev_time
+= profile_getclock() - ti
;
4055 } while (vm_can_run());
4057 if (qemu_debug_requested()) {
4058 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
4059 vm_stop(EXCP_DEBUG
);
4061 if (qemu_shutdown_requested()) {
4062 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4069 if (qemu_reset_requested()) {
4070 monitor_protocol_event(QEVENT_RESET
, NULL
);
4072 qemu_system_reset();
4075 if (qemu_powerdown_requested()) {
4076 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4077 qemu_irq_raise(qemu_system_powerdown
);
4079 if ((r
= qemu_vmstop_requested())) {
4080 monitor_protocol_event(QEVENT_STOP
, NULL
);
4087 static void version(void)
4089 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4092 static void help(int exitcode
)
4094 const char *options_help
=
4095 #define DEF(option, opt_arg, opt_enum, opt_help) \
4097 #define DEFHEADING(text) stringify(text) "\n"
4098 #include "qemu-options.h"
4104 printf("usage: %s [options] [disk_image]\n"
4106 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4109 "During emulation, the following keys are useful:\n"
4110 "ctrl-alt-f toggle full screen\n"
4111 "ctrl-alt-n switch to virtual console 'n'\n"
4112 "ctrl-alt toggle mouse and keyboard grab\n"
4114 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4120 #define HAS_ARG 0x0001
4123 #define DEF(option, opt_arg, opt_enum, opt_help) \
4125 #define DEFHEADING(text)
4126 #include "qemu-options.h"
4132 typedef struct QEMUOption
{
4138 static const QEMUOption qemu_options
[] = {
4139 { "h", 0, QEMU_OPTION_h
},
4140 #define DEF(option, opt_arg, opt_enum, opt_help) \
4141 { option, opt_arg, opt_enum },
4142 #define DEFHEADING(text)
4143 #include "qemu-options.h"
4151 struct soundhw soundhw
[] = {
4152 #ifdef HAS_AUDIO_CHOICE
4153 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4159 { .init_isa
= pcspk_audio_init
}
4166 "Creative Sound Blaster 16",
4169 { .init_isa
= SB16_init
}
4173 #ifdef CONFIG_CS4231A
4179 { .init_isa
= cs4231a_init
}
4187 "Yamaha YMF262 (OPL3)",
4189 "Yamaha YM3812 (OPL2)",
4193 { .init_isa
= Adlib_init
}
4200 "Gravis Ultrasound GF1",
4203 { .init_isa
= GUS_init
}
4210 "Intel 82801AA AC97 Audio",
4213 { .init_pci
= ac97_init
}
4217 #ifdef CONFIG_ES1370
4220 "ENSONIQ AudioPCI ES1370",
4223 { .init_pci
= es1370_init
}
4227 #endif /* HAS_AUDIO_CHOICE */
4229 { NULL
, NULL
, 0, 0, { NULL
} }
4232 static void select_soundhw (const char *optarg
)
4236 if (*optarg
== '?') {
4239 printf ("Valid sound card names (comma separated):\n");
4240 for (c
= soundhw
; c
->name
; ++c
) {
4241 printf ("%-11s %s\n", c
->name
, c
->descr
);
4243 printf ("\n-soundhw all will enable all of the above\n");
4244 exit (*optarg
!= '?');
4252 if (!strcmp (optarg
, "all")) {
4253 for (c
= soundhw
; c
->name
; ++c
) {
4261 e
= strchr (p
, ',');
4262 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4264 for (c
= soundhw
; c
->name
; ++c
) {
4265 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4274 "Unknown sound card name (too big to show)\n");
4277 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4282 p
+= l
+ (e
!= NULL
);
4286 goto show_valid_cards
;
4291 static void select_vgahw (const char *p
)
4296 vga_interface_type
= VGA_NONE
;
4297 if (strstart(p
, "std", &opts
)) {
4298 vga_interface_type
= VGA_STD
;
4299 } else if (strstart(p
, "cirrus", &opts
)) {
4300 vga_interface_type
= VGA_CIRRUS
;
4301 } else if (strstart(p
, "vmware", &opts
)) {
4302 vga_interface_type
= VGA_VMWARE
;
4303 } else if (strstart(p
, "xenfb", &opts
)) {
4304 vga_interface_type
= VGA_XENFB
;
4305 } else if (!strstart(p
, "none", &opts
)) {
4307 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4311 const char *nextopt
;
4313 if (strstart(opts
, ",retrace=", &nextopt
)) {
4315 if (strstart(opts
, "dumb", &nextopt
))
4316 vga_retrace_method
= VGA_RETRACE_DUMB
;
4317 else if (strstart(opts
, "precise", &nextopt
))
4318 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4319 else goto invalid_vga
;
4320 } else goto invalid_vga
;
4326 static int balloon_parse(const char *arg
)
4330 if (strcmp(arg
, "none") == 0) {
4334 if (!strncmp(arg
, "virtio", 6)) {
4335 if (arg
[6] == ',') {
4336 /* have params -> parse them */
4337 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4341 /* create empty opts */
4342 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4344 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4353 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4355 exit(STATUS_CONTROL_C_EXIT
);
4360 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4364 if(strlen(str
) != 36)
4367 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4368 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4369 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4375 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4383 static void termsig_handler(int signal
)
4385 qemu_system_shutdown_request();
4388 static void sigchld_handler(int signal
)
4390 waitpid(-1, NULL
, WNOHANG
);
4393 static void sighandler_setup(void)
4395 struct sigaction act
;
4397 memset(&act
, 0, sizeof(act
));
4398 act
.sa_handler
= termsig_handler
;
4399 sigaction(SIGINT
, &act
, NULL
);
4400 sigaction(SIGHUP
, &act
, NULL
);
4401 sigaction(SIGTERM
, &act
, NULL
);
4403 act
.sa_handler
= sigchld_handler
;
4404 act
.sa_flags
= SA_NOCLDSTOP
;
4405 sigaction(SIGCHLD
, &act
, NULL
);
4411 /* Look for support files in the same directory as the executable. */
4412 static char *find_datadir(const char *argv0
)
4418 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4425 while (p
!= buf
&& *p
!= '\\')
4428 if (access(buf
, R_OK
) == 0) {
4429 return qemu_strdup(buf
);
4435 /* Find a likely location for support files using the location of the binary.
4436 For installed binaries this will be "$bindir/../share/qemu". When
4437 running from the build tree this will be "$bindir/../pc-bios". */
4438 #define SHARE_SUFFIX "/share/qemu"
4439 #define BUILD_SUFFIX "/pc-bios"
4440 static char *find_datadir(const char *argv0
)
4448 #if defined(__linux__)
4451 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4457 #elif defined(__FreeBSD__)
4460 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4467 /* If we don't have any way of figuring out the actual executable
4468 location then try argv[0]. */
4470 p
= realpath(argv0
, buf
);
4478 max_len
= strlen(dir
) +
4479 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4480 res
= qemu_mallocz(max_len
);
4481 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4482 if (access(res
, R_OK
)) {
4483 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4484 if (access(res
, R_OK
)) {
4496 char *qemu_find_file(int type
, const char *name
)
4502 /* If name contains path separators then try it as a straight path. */
4503 if ((strchr(name
, '/') || strchr(name
, '\\'))
4504 && access(name
, R_OK
) == 0) {
4505 return qemu_strdup(name
);
4508 case QEMU_FILE_TYPE_BIOS
:
4511 case QEMU_FILE_TYPE_KEYMAP
:
4512 subdir
= "keymaps/";
4517 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4518 buf
= qemu_mallocz(len
);
4519 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4520 if (access(buf
, R_OK
)) {
4527 static int device_help_func(QemuOpts
*opts
, void *opaque
)
4529 return qdev_device_help(opts
);
4532 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4536 dev
= qdev_device_add(opts
);
4542 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4544 CharDriverState
*chr
;
4546 chr
= qemu_chr_open_opts(opts
, NULL
);
4552 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4554 CharDriverState
*chr
;
4555 const char *chardev
;
4559 mode
= qemu_opt_get(opts
, "mode");
4563 if (strcmp(mode
, "readline") == 0) {
4564 flags
= MONITOR_USE_READLINE
;
4565 } else if (strcmp(mode
, "control") == 0) {
4566 flags
= MONITOR_USE_CONTROL
;
4568 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4572 if (qemu_opt_get_bool(opts
, "default", 0))
4573 flags
|= MONITOR_IS_DEFAULT
;
4575 chardev
= qemu_opt_get(opts
, "chardev");
4576 chr
= qemu_chr_find(chardev
);
4578 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4582 monitor_init(chr
, flags
);
4586 static void monitor_parse(const char *optarg
, const char *mode
)
4588 static int monitor_device_index
= 0;
4594 if (strstart(optarg
, "chardev:", &p
)) {
4595 snprintf(label
, sizeof(label
), "%s", p
);
4597 if (monitor_device_index
) {
4598 snprintf(label
, sizeof(label
), "monitor%d",
4599 monitor_device_index
);
4601 snprintf(label
, sizeof(label
), "monitor");
4604 opts
= qemu_chr_parse_compat(label
, optarg
);
4606 fprintf(stderr
, "parse error: %s\n", optarg
);
4611 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4613 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4616 qemu_opt_set(opts
, "mode", mode
);
4617 qemu_opt_set(opts
, "chardev", label
);
4619 qemu_opt_set(opts
, "default", "on");
4620 monitor_device_index
++;
4623 struct device_config
{
4625 DEV_USB
, /* -usbdevice */
4627 DEV_SERIAL
, /* -serial */
4628 DEV_PARALLEL
, /* -parallel */
4629 DEV_VIRTCON
, /* -virtioconsole */
4630 DEV_DEBUGCON
, /* -debugcon */
4632 const char *cmdline
;
4633 QTAILQ_ENTRY(device_config
) next
;
4635 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4637 static void add_device_config(int type
, const char *cmdline
)
4639 struct device_config
*conf
;
4641 conf
= qemu_mallocz(sizeof(*conf
));
4643 conf
->cmdline
= cmdline
;
4644 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4647 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4649 struct device_config
*conf
;
4652 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4653 if (conf
->type
!= type
)
4655 rc
= func(conf
->cmdline
);
4662 static int serial_parse(const char *devname
)
4664 static int index
= 0;
4667 if (strcmp(devname
, "none") == 0)
4669 if (index
== MAX_SERIAL_PORTS
) {
4670 fprintf(stderr
, "qemu: too many serial ports\n");
4673 snprintf(label
, sizeof(label
), "serial%d", index
);
4674 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4675 if (!serial_hds
[index
]) {
4676 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4677 devname
, strerror(errno
));
4684 static int parallel_parse(const char *devname
)
4686 static int index
= 0;
4689 if (strcmp(devname
, "none") == 0)
4691 if (index
== MAX_PARALLEL_PORTS
) {
4692 fprintf(stderr
, "qemu: too many parallel ports\n");
4695 snprintf(label
, sizeof(label
), "parallel%d", index
);
4696 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4697 if (!parallel_hds
[index
]) {
4698 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4699 devname
, strerror(errno
));
4706 static int virtcon_parse(const char *devname
)
4708 static int index
= 0;
4710 QemuOpts
*bus_opts
, *dev_opts
;
4712 if (strcmp(devname
, "none") == 0)
4714 if (index
== MAX_VIRTIO_CONSOLES
) {
4715 fprintf(stderr
, "qemu: too many virtio consoles\n");
4719 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4720 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
4722 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4723 qemu_opt_set(dev_opts
, "driver", "virtconsole");
4725 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4726 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4727 if (!virtcon_hds
[index
]) {
4728 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4729 devname
, strerror(errno
));
4732 qemu_opt_set(dev_opts
, "chardev", label
);
4738 static int debugcon_parse(const char *devname
)
4742 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
4745 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
4747 fprintf(stderr
, "qemu: already have a debugcon device\n");
4750 qemu_opt_set(opts
, "driver", "isa-debugcon");
4751 qemu_opt_set(opts
, "chardev", "debugcon");
4755 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
4756 const char **poptarg
, int *poptind
)
4758 const QEMUOption
*popt
;
4759 int optind
= *poptind
;
4760 char *r
= argv
[optind
];
4764 /* Treat --foo the same as -foo. */
4767 popt
= qemu_options
;
4770 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4774 if (!strcmp(popt
->name
, r
+ 1))
4778 if (popt
->flags
& HAS_ARG
) {
4779 if (optind
>= argc
) {
4780 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4784 optarg
= argv
[optind
++];
4795 int main(int argc
, char **argv
, char **envp
)
4797 const char *gdbstub_dev
= NULL
;
4798 uint32_t boot_devices_bitmap
= 0;
4800 int snapshot
, linux_boot
, net_boot
;
4801 const char *initrd_filename
;
4802 const char *kernel_filename
, *kernel_cmdline
;
4803 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4805 DisplayChangeListener
*dcl
;
4806 int cyls
, heads
, secs
, translation
;
4807 QemuOpts
*hda_opts
= NULL
, *opts
;
4810 const char *loadvm
= NULL
;
4811 QEMUMachine
*machine
;
4812 const char *cpu_model
;
4817 const char *pid_file
= NULL
;
4818 const char *incoming
= NULL
;
4821 struct passwd
*pwd
= NULL
;
4822 const char *chroot_dir
= NULL
;
4823 const char *run_as
= NULL
;
4826 int show_vnc_port
= 0;
4831 qemu_errors_to_file(stderr
);
4832 qemu_cache_utils_init(envp
);
4834 QLIST_INIT (&vm_change_state_head
);
4837 struct sigaction act
;
4838 sigfillset(&act
.sa_mask
);
4840 act
.sa_handler
= SIG_IGN
;
4841 sigaction(SIGPIPE
, &act
, NULL
);
4844 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4845 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4846 QEMU to run on a single CPU */
4851 h
= GetCurrentProcess();
4852 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4853 for(i
= 0; i
< 32; i
++) {
4854 if (mask
& (1 << i
))
4859 SetProcessAffinityMask(h
, mask
);
4865 module_call_init(MODULE_INIT_MACHINE
);
4866 machine
= find_default_machine();
4868 initrd_filename
= NULL
;
4871 kernel_filename
= NULL
;
4872 kernel_cmdline
= "";
4873 cyls
= heads
= secs
= 0;
4874 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4876 for (i
= 0; i
< MAX_NODES
; i
++) {
4878 node_cpumask
[i
] = 0;
4887 /* first pass of option parsing */
4889 while (optind
< argc
) {
4890 if (argv
[optind
][0] != '-') {
4895 const QEMUOption
*popt
;
4897 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4898 switch (popt
->index
) {
4899 case QEMU_OPTION_nodefconfig
:
4908 fp
= fopen(CONFIG_QEMU_CONFDIR
"/qemu.conf", "r");
4910 if (qemu_config_parse(fp
) != 0) {
4916 fp
= fopen(CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf", "r");
4918 if (qemu_config_parse(fp
) != 0) {
4924 #if defined(cpudef_setup)
4925 cpudef_setup(); /* parse cpu definitions in target config file */
4928 /* second pass of option parsing */
4933 if (argv
[optind
][0] != '-') {
4934 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4936 const QEMUOption
*popt
;
4938 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
4939 switch(popt
->index
) {
4941 machine
= find_machine(optarg
);
4944 printf("Supported machines are:\n");
4945 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4947 printf("%-10s %s (alias of %s)\n",
4948 m
->alias
, m
->desc
, m
->name
);
4949 printf("%-10s %s%s\n",
4951 m
->is_default
? " (default)" : "");
4953 exit(*optarg
!= '?');
4956 case QEMU_OPTION_cpu
:
4957 /* hw initialization will check this */
4958 if (*optarg
== '?') {
4959 /* XXX: implement xxx_cpu_list for targets that still miss it */
4960 #if defined(cpu_list_id)
4961 cpu_list_id(stdout
, &fprintf
, optarg
);
4962 #elif defined(cpu_list)
4963 cpu_list(stdout
, &fprintf
); /* deprecated */
4970 case QEMU_OPTION_initrd
:
4971 initrd_filename
= optarg
;
4973 case QEMU_OPTION_hda
:
4975 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4977 hda_opts
= drive_add(optarg
, HD_ALIAS
4978 ",cyls=%d,heads=%d,secs=%d%s",
4979 0, cyls
, heads
, secs
,
4980 translation
== BIOS_ATA_TRANSLATION_LBA
?
4982 translation
== BIOS_ATA_TRANSLATION_NONE
?
4983 ",trans=none" : "");
4985 case QEMU_OPTION_hdb
:
4986 case QEMU_OPTION_hdc
:
4987 case QEMU_OPTION_hdd
:
4988 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4990 case QEMU_OPTION_drive
:
4991 drive_add(NULL
, "%s", optarg
);
4993 case QEMU_OPTION_set
:
4994 if (qemu_set_option(optarg
) != 0)
4997 case QEMU_OPTION_global
:
4998 if (qemu_global_option(optarg
) != 0)
5001 case QEMU_OPTION_mtdblock
:
5002 drive_add(optarg
, MTD_ALIAS
);
5004 case QEMU_OPTION_sd
:
5005 drive_add(optarg
, SD_ALIAS
);
5007 case QEMU_OPTION_pflash
:
5008 drive_add(optarg
, PFLASH_ALIAS
);
5010 case QEMU_OPTION_snapshot
:
5013 case QEMU_OPTION_hdachs
:
5017 cyls
= strtol(p
, (char **)&p
, 0);
5018 if (cyls
< 1 || cyls
> 16383)
5023 heads
= strtol(p
, (char **)&p
, 0);
5024 if (heads
< 1 || heads
> 16)
5029 secs
= strtol(p
, (char **)&p
, 0);
5030 if (secs
< 1 || secs
> 63)
5034 if (!strcmp(p
, "none"))
5035 translation
= BIOS_ATA_TRANSLATION_NONE
;
5036 else if (!strcmp(p
, "lba"))
5037 translation
= BIOS_ATA_TRANSLATION_LBA
;
5038 else if (!strcmp(p
, "auto"))
5039 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5042 } else if (*p
!= '\0') {
5044 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5047 if (hda_opts
!= NULL
) {
5049 snprintf(num
, sizeof(num
), "%d", cyls
);
5050 qemu_opt_set(hda_opts
, "cyls", num
);
5051 snprintf(num
, sizeof(num
), "%d", heads
);
5052 qemu_opt_set(hda_opts
, "heads", num
);
5053 snprintf(num
, sizeof(num
), "%d", secs
);
5054 qemu_opt_set(hda_opts
, "secs", num
);
5055 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5056 qemu_opt_set(hda_opts
, "trans", "lba");
5057 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5058 qemu_opt_set(hda_opts
, "trans", "none");
5062 case QEMU_OPTION_numa
:
5063 if (nb_numa_nodes
>= MAX_NODES
) {
5064 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5069 case QEMU_OPTION_nographic
:
5070 display_type
= DT_NOGRAPHIC
;
5072 #ifdef CONFIG_CURSES
5073 case QEMU_OPTION_curses
:
5074 display_type
= DT_CURSES
;
5077 case QEMU_OPTION_portrait
:
5080 case QEMU_OPTION_kernel
:
5081 kernel_filename
= optarg
;
5083 case QEMU_OPTION_append
:
5084 kernel_cmdline
= optarg
;
5086 case QEMU_OPTION_cdrom
:
5087 drive_add(optarg
, CDROM_ALIAS
);
5089 case QEMU_OPTION_boot
:
5091 static const char * const params
[] = {
5092 "order", "once", "menu", NULL
5094 char buf
[sizeof(boot_devices
)];
5095 char *standard_boot_devices
;
5098 if (!strchr(optarg
, '=')) {
5100 pstrcpy(buf
, sizeof(buf
), optarg
);
5101 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5103 "qemu: unknown boot parameter '%s' in '%s'\n",
5109 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5110 boot_devices_bitmap
= parse_bootdevices(buf
);
5111 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5114 if (get_param_value(buf
, sizeof(buf
),
5116 boot_devices_bitmap
|= parse_bootdevices(buf
);
5117 standard_boot_devices
= qemu_strdup(boot_devices
);
5118 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5119 qemu_register_reset(restore_boot_devices
,
5120 standard_boot_devices
);
5122 if (get_param_value(buf
, sizeof(buf
),
5124 if (!strcmp(buf
, "on")) {
5126 } else if (!strcmp(buf
, "off")) {
5130 "qemu: invalid option value '%s'\n",
5138 case QEMU_OPTION_fda
:
5139 case QEMU_OPTION_fdb
:
5140 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5143 case QEMU_OPTION_no_fd_bootchk
:
5147 case QEMU_OPTION_netdev
:
5148 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5152 case QEMU_OPTION_net
:
5153 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5158 case QEMU_OPTION_tftp
:
5159 legacy_tftp_prefix
= optarg
;
5161 case QEMU_OPTION_bootp
:
5162 legacy_bootp_filename
= optarg
;
5165 case QEMU_OPTION_smb
:
5166 if (net_slirp_smb(optarg
) < 0)
5170 case QEMU_OPTION_redir
:
5171 if (net_slirp_redir(optarg
) < 0)
5175 case QEMU_OPTION_bt
:
5176 add_device_config(DEV_BT
, optarg
);
5179 case QEMU_OPTION_audio_help
:
5183 case QEMU_OPTION_soundhw
:
5184 select_soundhw (optarg
);
5190 case QEMU_OPTION_version
:
5194 case QEMU_OPTION_m
: {
5198 value
= strtoul(optarg
, &ptr
, 10);
5200 case 0: case 'M': case 'm':
5207 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5211 /* On 32-bit hosts, QEMU is limited by virtual address space */
5212 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5213 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5216 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5217 fprintf(stderr
, "qemu: ram size too large\n");
5223 case QEMU_OPTION_mempath
:
5227 case QEMU_OPTION_mem_prealloc
:
5234 const CPULogItem
*item
;
5236 mask
= cpu_str_to_log_mask(optarg
);
5238 printf("Log items (comma separated):\n");
5239 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5240 printf("%-10s %s\n", item
->name
, item
->help
);
5248 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5250 case QEMU_OPTION_gdb
:
5251 gdbstub_dev
= optarg
;
5256 case QEMU_OPTION_bios
:
5259 case QEMU_OPTION_singlestep
:
5266 keyboard_layout
= optarg
;
5268 case QEMU_OPTION_localtime
:
5271 case QEMU_OPTION_vga
:
5272 select_vgahw (optarg
);
5274 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5280 w
= strtol(p
, (char **)&p
, 10);
5283 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5289 h
= strtol(p
, (char **)&p
, 10);
5294 depth
= strtol(p
, (char **)&p
, 10);
5295 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5296 depth
!= 24 && depth
!= 32)
5298 } else if (*p
== '\0') {
5299 depth
= graphic_depth
;
5306 graphic_depth
= depth
;
5310 case QEMU_OPTION_echr
:
5313 term_escape_char
= strtol(optarg
, &r
, 0);
5315 printf("Bad argument to echr\n");
5318 case QEMU_OPTION_monitor
:
5319 monitor_parse(optarg
, "readline");
5320 default_monitor
= 0;
5322 case QEMU_OPTION_qmp
:
5323 monitor_parse(optarg
, "control");
5324 default_monitor
= 0;
5326 case QEMU_OPTION_mon
:
5327 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5329 fprintf(stderr
, "parse error: %s\n", optarg
);
5332 default_monitor
= 0;
5334 case QEMU_OPTION_chardev
:
5335 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5337 fprintf(stderr
, "parse error: %s\n", optarg
);
5341 case QEMU_OPTION_serial
:
5342 add_device_config(DEV_SERIAL
, optarg
);
5345 case QEMU_OPTION_watchdog
:
5348 "qemu: only one watchdog option may be given\n");
5353 case QEMU_OPTION_watchdog_action
:
5354 if (select_watchdog_action(optarg
) == -1) {
5355 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5359 case QEMU_OPTION_virtiocon
:
5360 add_device_config(DEV_VIRTCON
, optarg
);
5361 default_virtcon
= 0;
5363 case QEMU_OPTION_parallel
:
5364 add_device_config(DEV_PARALLEL
, optarg
);
5365 default_parallel
= 0;
5367 case QEMU_OPTION_debugcon
:
5368 add_device_config(DEV_DEBUGCON
, optarg
);
5370 case QEMU_OPTION_loadvm
:
5373 case QEMU_OPTION_full_screen
:
5377 case QEMU_OPTION_no_frame
:
5380 case QEMU_OPTION_alt_grab
:
5383 case QEMU_OPTION_ctrl_grab
:
5386 case QEMU_OPTION_no_quit
:
5389 case QEMU_OPTION_sdl
:
5390 display_type
= DT_SDL
;
5393 case QEMU_OPTION_pidfile
:
5397 case QEMU_OPTION_win2k_hack
:
5398 win2k_install_hack
= 1;
5400 case QEMU_OPTION_rtc_td_hack
:
5403 case QEMU_OPTION_acpitable
:
5404 if(acpi_table_add(optarg
) < 0) {
5405 fprintf(stderr
, "Wrong acpi table provided\n");
5409 case QEMU_OPTION_smbios
:
5410 if(smbios_entry_add(optarg
) < 0) {
5411 fprintf(stderr
, "Wrong smbios provided\n");
5417 case QEMU_OPTION_enable_kvm
:
5421 case QEMU_OPTION_usb
:
5424 case QEMU_OPTION_usbdevice
:
5426 add_device_config(DEV_USB
, optarg
);
5428 case QEMU_OPTION_device
:
5429 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5433 case QEMU_OPTION_smp
:
5436 fprintf(stderr
, "Invalid number of CPUs\n");
5439 if (max_cpus
< smp_cpus
) {
5440 fprintf(stderr
, "maxcpus must be equal to or greater than "
5444 if (max_cpus
> 255) {
5445 fprintf(stderr
, "Unsupported number of maxcpus\n");
5449 case QEMU_OPTION_vnc
:
5450 display_type
= DT_VNC
;
5451 vnc_display
= optarg
;
5454 case QEMU_OPTION_no_acpi
:
5457 case QEMU_OPTION_no_hpet
:
5460 case QEMU_OPTION_balloon
:
5461 if (balloon_parse(optarg
) < 0) {
5462 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5467 case QEMU_OPTION_no_reboot
:
5470 case QEMU_OPTION_no_shutdown
:
5473 case QEMU_OPTION_show_cursor
:
5476 case QEMU_OPTION_uuid
:
5477 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5478 fprintf(stderr
, "Fail to parse UUID string."
5479 " Wrong format.\n");
5484 case QEMU_OPTION_daemonize
:
5488 case QEMU_OPTION_option_rom
:
5489 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5490 fprintf(stderr
, "Too many option ROMs\n");
5493 option_rom
[nb_option_roms
] = optarg
;
5496 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5497 case QEMU_OPTION_semihosting
:
5498 semihosting_enabled
= 1;
5501 case QEMU_OPTION_name
:
5502 qemu_name
= qemu_strdup(optarg
);
5504 char *p
= strchr(qemu_name
, ',');
5507 if (strncmp(p
, "process=", 8)) {
5508 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5516 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5517 case QEMU_OPTION_prom_env
:
5518 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5519 fprintf(stderr
, "Too many prom variables\n");
5522 prom_envs
[nb_prom_envs
] = optarg
;
5527 case QEMU_OPTION_old_param
:
5531 case QEMU_OPTION_clock
:
5532 configure_alarms(optarg
);
5534 case QEMU_OPTION_startdate
:
5535 configure_rtc_date_offset(optarg
, 1);
5537 case QEMU_OPTION_rtc
:
5538 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5540 fprintf(stderr
, "parse error: %s\n", optarg
);
5543 configure_rtc(opts
);
5545 case QEMU_OPTION_tb_size
:
5546 tb_size
= strtol(optarg
, NULL
, 0);
5550 case QEMU_OPTION_icount
:
5552 if (strcmp(optarg
, "auto") == 0) {
5553 icount_time_shift
= -1;
5555 icount_time_shift
= strtol(optarg
, NULL
, 0);
5558 case QEMU_OPTION_incoming
:
5561 case QEMU_OPTION_nodefaults
:
5563 default_parallel
= 0;
5564 default_virtcon
= 0;
5565 default_monitor
= 0;
5573 case QEMU_OPTION_chroot
:
5574 chroot_dir
= optarg
;
5576 case QEMU_OPTION_runas
:
5581 case QEMU_OPTION_xen_domid
:
5582 xen_domid
= atoi(optarg
);
5584 case QEMU_OPTION_xen_create
:
5585 xen_mode
= XEN_CREATE
;
5587 case QEMU_OPTION_xen_attach
:
5588 xen_mode
= XEN_ATTACH
;
5591 case QEMU_OPTION_readconfig
:
5594 fp
= fopen(optarg
, "r");
5596 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5599 if (qemu_config_parse(fp
) != 0) {
5605 case QEMU_OPTION_writeconfig
:
5608 if (strcmp(optarg
, "-") == 0) {
5611 fp
= fopen(optarg
, "w");
5613 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5617 qemu_config_write(fp
);
5625 /* If no data_dir is specified then try to find it relative to the
5628 data_dir
= find_datadir(argv
[0]);
5630 /* If all else fails use the install patch specified when building. */
5632 data_dir
= CONFIG_QEMU_SHAREDIR
;
5636 * Default to max_cpus = smp_cpus, in case the user doesn't
5637 * specify a max_cpus value.
5640 max_cpus
= smp_cpus
;
5642 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5643 if (smp_cpus
> machine
->max_cpus
) {
5644 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5645 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5650 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5651 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
5653 if (machine
->no_serial
) {
5656 if (machine
->no_parallel
) {
5657 default_parallel
= 0;
5659 if (!machine
->use_virtcon
) {
5660 default_virtcon
= 0;
5662 if (machine
->no_vga
) {
5665 if (machine
->no_floppy
) {
5668 if (machine
->no_cdrom
) {
5671 if (machine
->no_sdcard
) {
5675 if (display_type
== DT_NOGRAPHIC
) {
5676 if (default_parallel
)
5677 add_device_config(DEV_PARALLEL
, "null");
5678 if (default_serial
&& default_monitor
) {
5679 add_device_config(DEV_SERIAL
, "mon:stdio");
5680 } else if (default_virtcon
&& default_monitor
) {
5681 add_device_config(DEV_VIRTCON
, "mon:stdio");
5684 add_device_config(DEV_SERIAL
, "stdio");
5685 if (default_virtcon
)
5686 add_device_config(DEV_VIRTCON
, "stdio");
5687 if (default_monitor
)
5688 monitor_parse("stdio", "readline");
5692 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5693 if (default_parallel
)
5694 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5695 if (default_monitor
)
5696 monitor_parse("vc:80Cx24C", "readline");
5697 if (default_virtcon
)
5698 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
5701 vga_interface_type
= VGA_CIRRUS
;
5703 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5710 if (pipe(fds
) == -1)
5721 len
= read(fds
[0], &status
, 1);
5722 if (len
== -1 && (errno
== EINTR
))
5727 else if (status
== 1) {
5728 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5736 qemu_set_cloexec(fds
[1]);
5748 signal(SIGTSTP
, SIG_IGN
);
5749 signal(SIGTTOU
, SIG_IGN
);
5750 signal(SIGTTIN
, SIG_IGN
);
5754 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5758 if (write(fds
[1], &status
, 1) != 1) {
5759 perror("daemonize. Writing to pipe\n");
5763 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5767 if (kvm_enabled()) {
5770 ret
= kvm_init(smp_cpus
);
5772 fprintf(stderr
, "failed to initialize KVM\n");
5777 if (qemu_init_main_loop()) {
5778 fprintf(stderr
, "qemu_init_main_loop failed\n");
5781 linux_boot
= (kernel_filename
!= NULL
);
5783 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5784 fprintf(stderr
, "-append only allowed with -kernel option\n");
5788 if (!linux_boot
&& initrd_filename
!= NULL
) {
5789 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5794 /* Win32 doesn't support line-buffering and requires size >= 2 */
5795 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5798 if (init_timer_alarm() < 0) {
5799 fprintf(stderr
, "could not initialize alarm timer\n");
5802 if (use_icount
&& icount_time_shift
< 0) {
5804 /* 125MIPS seems a reasonable initial guess at the guest speed.
5805 It will be corrected fairly quickly anyway. */
5806 icount_time_shift
= 3;
5807 init_icount_adjust();
5814 if (net_init_clients() < 0) {
5818 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5819 net_set_boot_mask(net_boot
);
5821 /* init the bluetooth world */
5822 if (foreach_device_config(DEV_BT
, bt_parse
))
5825 /* init the memory */
5827 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5829 /* init the dynamic translator */
5830 cpu_exec_init_all(tb_size
* 1024 * 1024);
5832 bdrv_init_with_whitelist();
5836 if (default_cdrom
) {
5837 /* we always create the cdrom drive, even if no disk is there */
5838 drive_add(NULL
, CDROM_ALIAS
);
5841 if (default_floppy
) {
5842 /* we always create at least one floppy */
5843 drive_add(NULL
, FD_ALIAS
, 0);
5846 if (default_sdcard
) {
5847 /* we always create one sd slot, even if no card is in it */
5848 drive_add(NULL
, SD_ALIAS
);
5851 /* open the virtual block devices */
5853 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5854 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5857 vmstate_register(0, &vmstate_timers
,&timers_state
);
5858 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5861 if (nb_numa_nodes
> 0) {
5864 if (nb_numa_nodes
> smp_cpus
) {
5865 nb_numa_nodes
= smp_cpus
;
5868 /* If no memory size if given for any node, assume the default case
5869 * and distribute the available memory equally across all nodes
5871 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5872 if (node_mem
[i
] != 0)
5875 if (i
== nb_numa_nodes
) {
5876 uint64_t usedmem
= 0;
5878 /* On Linux, the each node's border has to be 8MB aligned,
5879 * the final node gets the rest.
5881 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5882 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5883 usedmem
+= node_mem
[i
];
5885 node_mem
[i
] = ram_size
- usedmem
;
5888 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5889 if (node_cpumask
[i
] != 0)
5892 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5893 * must cope with this anyway, because there are BIOSes out there in
5894 * real machines which also use this scheme.
5896 if (i
== nb_numa_nodes
) {
5897 for (i
= 0; i
< smp_cpus
; i
++) {
5898 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5903 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5905 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5907 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5909 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5912 module_call_init(MODULE_INIT_DEVICE
);
5914 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5918 i
= select_watchdog(watchdog
);
5920 exit (i
== 1 ? 1 : 0);
5923 if (machine
->compat_props
) {
5924 qdev_prop_register_global_list(machine
->compat_props
);
5928 machine
->init(ram_size
, boot_devices
,
5929 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5933 /* must be after terminal init, SDL library changes signal handlers */
5937 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5938 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5939 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5945 current_machine
= machine
;
5947 /* init USB devices */
5949 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5953 /* init generic devices */
5954 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5957 net_check_clients();
5959 /* just use the first displaystate for the moment */
5960 ds
= get_displaystate();
5962 if (display_type
== DT_DEFAULT
) {
5963 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5964 display_type
= DT_SDL
;
5966 display_type
= DT_VNC
;
5967 vnc_display
= "localhost:0,to=99";
5973 switch (display_type
) {
5976 #if defined(CONFIG_CURSES)
5978 curses_display_init(ds
, full_screen
);
5981 #if defined(CONFIG_SDL)
5983 sdl_display_init(ds
, full_screen
, no_frame
);
5985 #elif defined(CONFIG_COCOA)
5987 cocoa_display_init(ds
, full_screen
);
5991 vnc_display_init(ds
);
5992 if (vnc_display_open(ds
, vnc_display
) < 0)
5995 if (show_vnc_port
) {
5996 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6004 dcl
= ds
->listeners
;
6005 while (dcl
!= NULL
) {
6006 if (dcl
->dpy_refresh
!= NULL
) {
6007 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6008 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6013 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6014 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6015 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6018 text_consoles_set_display(ds
);
6020 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
6023 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6024 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6029 qdev_machine_creation_done();
6031 if (rom_load_all() != 0) {
6032 fprintf(stderr
, "rom loading failed\n");
6036 qemu_system_reset();
6038 if (load_vmstate(cur_mon
, loadvm
) < 0) {
6044 qemu_start_incoming_migration(incoming
);
6045 } else if (autostart
) {
6055 len
= write(fds
[1], &status
, 1);
6056 if (len
== -1 && (errno
== EINTR
))
6063 perror("not able to chdir to /");
6066 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
6072 pwd
= getpwnam(run_as
);
6074 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6080 if (chroot(chroot_dir
) < 0) {
6081 fprintf(stderr
, "chroot failed\n");
6085 perror("not able to chdir to /");
6091 if (setgid(pwd
->pw_gid
) < 0) {
6092 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6095 if (setuid(pwd
->pw_uid
) < 0) {
6096 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6099 if (setuid(0) != -1) {
6100 fprintf(stderr
, "Dropping privileges failed\n");