4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <linux/rtc.h>
63 #include <sys/prctl.h>
65 /* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67 /* #include <linux/hpet.h> */
70 #include <linux/ppdev.h>
71 #include <linux/parport.h>
75 #include <sys/ethernet.h>
76 #include <sys/sockio.h>
77 #include <netinet/arp.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_icmp.h> // must come after ip.h
82 #include <netinet/udp.h>
83 #include <netinet/tcp.h>
87 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89 extern int madvise(caddr_t
, size_t, int);
94 #if defined(__OpenBSD__)
98 #if defined(CONFIG_VDE)
99 #include <libvdeplug.h>
104 #include <mmsystem.h>
108 #if defined(__APPLE__) || defined(main)
110 int qemu_main(int argc
, char **argv
, char **envp
);
111 int main(int argc
, char **argv
)
113 return qemu_main(argc
, argv
, NULL
);
116 #define main qemu_main
118 #endif /* CONFIG_SDL */
122 #define main qemu_main
123 #endif /* CONFIG_COCOA */
126 #include "hw/boards.h"
128 #include "hw/pcmcia.h"
130 #include "hw/audiodev.h"
134 #include "hw/watchdog.h"
135 #include "hw/smbios.h"
138 #include "hw/loader.h"
141 #include "net/slirp.h"
146 #include "qemu-timer.h"
147 #include "qemu-char.h"
148 #include "cache-utils.h"
150 #include "block_int.h"
151 #include "block-migration.h"
153 #include "audio/audio.h"
154 #include "migration.h"
157 #include "qemu-option.h"
158 #include "qemu-config.h"
162 #include "exec-all.h"
164 #include "qemu_socket.h"
166 #include "slirp/libslirp.h"
168 #include "qemu-queue.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 static const char *data_dir
;
176 const char *bios_name
= NULL
;
177 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
178 to store the VM snapshots */
179 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
180 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
181 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
182 static DisplayState
*display_state
;
183 DisplayType display_type
= DT_DEFAULT
;
184 const char* keyboard_layout
= NULL
;
187 NICInfo nd_table
[MAX_NICS
];
190 static int rtc_utc
= 1;
191 static int rtc_date_offset
= -1; /* -1 means no change */
192 QEMUClock
*rtc_clock
;
193 int vga_interface_type
= VGA_NONE
;
195 int graphic_width
= 1024;
196 int graphic_height
= 768;
197 int graphic_depth
= 8;
199 int graphic_width
= 800;
200 int graphic_height
= 600;
201 int graphic_depth
= 15;
203 static int full_screen
= 0;
205 static int no_frame
= 0;
208 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
209 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
210 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
212 int win2k_install_hack
= 0;
221 const char *vnc_display
;
222 int acpi_enabled
= 1;
228 int graphic_rotate
= 0;
229 uint8_t irq0override
= 1;
233 const char *watchdog
;
234 const char *option_rom
[MAX_OPTION_ROMS
];
236 int semihosting_enabled
= 0;
240 const char *qemu_name
;
243 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
244 unsigned int nb_prom_envs
= 0;
245 const char *prom_envs
[MAX_PROM_ENVS
];
250 uint64_t node_mem
[MAX_NODES
];
251 uint64_t node_cpumask
[MAX_NODES
];
253 static CPUState
*cur_cpu
;
254 static CPUState
*next_cpu
;
255 static int timer_alarm_pending
= 1;
256 /* Conversion factor from emulated instructions to virtual clock ticks. */
257 static int icount_time_shift
;
258 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
259 #define MAX_ICOUNT_SHIFT 10
260 /* Compensate for varying guest execution speed. */
261 static int64_t qemu_icount_bias
;
262 static QEMUTimer
*icount_rt_timer
;
263 static QEMUTimer
*icount_vm_timer
;
264 static QEMUTimer
*nographic_timer
;
266 uint8_t qemu_uuid
[16];
268 static QEMUBootSetHandler
*boot_set_handler
;
269 static void *boot_set_opaque
;
271 static int default_serial
= 1;
272 static int default_parallel
= 1;
273 static int default_virtcon
= 1;
274 static int default_monitor
= 1;
275 static int default_vga
= 1;
276 static int default_drive
= 1;
282 { .driver
= "isa-serial", .flag
= &default_serial
},
283 { .driver
= "isa-parallel", .flag
= &default_parallel
},
284 { .driver
= "virtio-console-pci", .flag
= &default_virtcon
},
285 { .driver
= "virtio-console-s390", .flag
= &default_virtcon
},
286 { .driver
= "VGA", .flag
= &default_vga
},
287 { .driver
= "Cirrus VGA", .flag
= &default_vga
},
288 { .driver
= "QEMUware SVGA", .flag
= &default_vga
},
291 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
293 const char *driver
= qemu_opt_get(opts
, "driver");
298 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
299 if (strcmp(default_list
[i
].driver
, driver
) != 0)
301 *(default_list
[i
].flag
) = 0;
306 /***********************************************************/
307 /* x86 ISA bus support */
309 target_phys_addr_t isa_mem_base
= 0;
312 /***********************************************************/
313 void hw_error(const char *fmt
, ...)
319 fprintf(stderr
, "qemu: hardware error: ");
320 vfprintf(stderr
, fmt
, ap
);
321 fprintf(stderr
, "\n");
322 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
323 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
325 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
327 cpu_dump_state(env
, stderr
, fprintf
, 0);
334 static void set_proc_name(const char *s
)
336 #if defined(__linux__) && defined(PR_SET_NAME)
340 name
[sizeof(name
) - 1] = 0;
341 strncpy(name
, s
, sizeof(name
));
342 /* Could rewrite argv[0] too, but that's a bit more complicated.
343 This simple way is enough for `top'. */
344 prctl(PR_SET_NAME
, name
);
351 static QEMUBalloonEvent
*qemu_balloon_event
;
352 void *qemu_balloon_event_opaque
;
354 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
356 qemu_balloon_event
= func
;
357 qemu_balloon_event_opaque
= opaque
;
360 void qemu_balloon(ram_addr_t target
)
362 if (qemu_balloon_event
)
363 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
366 ram_addr_t
qemu_balloon_status(void)
368 if (qemu_balloon_event
)
369 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
373 /***********************************************************/
376 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
377 static void *qemu_put_kbd_event_opaque
;
378 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
379 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
381 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
383 qemu_put_kbd_event_opaque
= opaque
;
384 qemu_put_kbd_event
= func
;
387 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
388 void *opaque
, int absolute
,
391 QEMUPutMouseEntry
*s
, *cursor
;
393 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
395 s
->qemu_put_mouse_event
= func
;
396 s
->qemu_put_mouse_event_opaque
= opaque
;
397 s
->qemu_put_mouse_event_absolute
= absolute
;
398 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
401 if (!qemu_put_mouse_event_head
) {
402 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
406 cursor
= qemu_put_mouse_event_head
;
407 while (cursor
->next
!= NULL
)
408 cursor
= cursor
->next
;
411 qemu_put_mouse_event_current
= s
;
416 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
418 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
420 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
423 cursor
= qemu_put_mouse_event_head
;
424 while (cursor
!= NULL
&& cursor
!= entry
) {
426 cursor
= cursor
->next
;
429 if (cursor
== NULL
) // does not exist or list empty
431 else if (prev
== NULL
) { // entry is head
432 qemu_put_mouse_event_head
= cursor
->next
;
433 if (qemu_put_mouse_event_current
== entry
)
434 qemu_put_mouse_event_current
= cursor
->next
;
435 qemu_free(entry
->qemu_put_mouse_event_name
);
440 prev
->next
= entry
->next
;
442 if (qemu_put_mouse_event_current
== entry
)
443 qemu_put_mouse_event_current
= prev
;
445 qemu_free(entry
->qemu_put_mouse_event_name
);
449 void kbd_put_keycode(int keycode
)
451 if (qemu_put_kbd_event
) {
452 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
456 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
458 QEMUPutMouseEvent
*mouse_event
;
459 void *mouse_event_opaque
;
462 if (!qemu_put_mouse_event_current
) {
467 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
469 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
472 if (graphic_rotate
) {
473 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
476 width
= graphic_width
- 1;
477 mouse_event(mouse_event_opaque
,
478 width
- dy
, dx
, dz
, buttons_state
);
480 mouse_event(mouse_event_opaque
,
481 dx
, dy
, dz
, buttons_state
);
485 int kbd_mouse_is_absolute(void)
487 if (!qemu_put_mouse_event_current
)
490 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
493 void do_info_mice(Monitor
*mon
)
495 QEMUPutMouseEntry
*cursor
;
498 if (!qemu_put_mouse_event_head
) {
499 monitor_printf(mon
, "No mouse devices connected\n");
503 monitor_printf(mon
, "Mouse devices available:\n");
504 cursor
= qemu_put_mouse_event_head
;
505 while (cursor
!= NULL
) {
506 monitor_printf(mon
, "%c Mouse #%d: %s\n",
507 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
508 index
, cursor
->qemu_put_mouse_event_name
);
510 cursor
= cursor
->next
;
514 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
516 QEMUPutMouseEntry
*cursor
;
518 int index
= qdict_get_int(qdict
, "index");
520 if (!qemu_put_mouse_event_head
) {
521 monitor_printf(mon
, "No mouse devices connected\n");
525 cursor
= qemu_put_mouse_event_head
;
526 while (cursor
!= NULL
&& index
!= i
) {
528 cursor
= cursor
->next
;
532 qemu_put_mouse_event_current
= cursor
;
534 monitor_printf(mon
, "Mouse at given index not found\n");
537 /* compute with 96 bit intermediate result: (a*b)/c */
538 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
543 #ifdef HOST_WORDS_BIGENDIAN
553 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
554 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
557 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
561 /***********************************************************/
562 /* real time host monotonic timer */
564 static int64_t get_clock_realtime(void)
568 gettimeofday(&tv
, NULL
);
569 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
574 static int64_t clock_freq
;
576 static void init_get_clock(void)
580 ret
= QueryPerformanceFrequency(&freq
);
582 fprintf(stderr
, "Could not calibrate ticks\n");
585 clock_freq
= freq
.QuadPart
;
588 static int64_t get_clock(void)
591 QueryPerformanceCounter(&ti
);
592 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
597 static int use_rt_clock
;
599 static void init_get_clock(void)
602 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
603 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
606 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
613 static int64_t get_clock(void)
615 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
616 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
619 clock_gettime(CLOCK_MONOTONIC
, &ts
);
620 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
624 /* XXX: using gettimeofday leads to problems if the date
625 changes, so it should be avoided. */
626 return get_clock_realtime();
631 /* Return the virtual CPU time, based on the instruction counter. */
632 static int64_t cpu_get_icount(void)
635 CPUState
*env
= cpu_single_env
;;
636 icount
= qemu_icount
;
639 fprintf(stderr
, "Bad clock read\n");
640 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
642 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
645 /***********************************************************/
646 /* guest cycle counter */
648 typedef struct TimersState
{
649 int64_t cpu_ticks_prev
;
650 int64_t cpu_ticks_offset
;
651 int64_t cpu_clock_offset
;
652 int32_t cpu_ticks_enabled
;
656 TimersState timers_state
;
658 /* return the host CPU cycle counter and handle stop/restart */
659 int64_t cpu_get_ticks(void)
662 return cpu_get_icount();
664 if (!timers_state
.cpu_ticks_enabled
) {
665 return timers_state
.cpu_ticks_offset
;
668 ticks
= cpu_get_real_ticks();
669 if (timers_state
.cpu_ticks_prev
> ticks
) {
670 /* Note: non increasing ticks may happen if the host uses
672 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
674 timers_state
.cpu_ticks_prev
= ticks
;
675 return ticks
+ timers_state
.cpu_ticks_offset
;
679 /* return the host CPU monotonic timer and handle stop/restart */
680 static int64_t cpu_get_clock(void)
683 if (!timers_state
.cpu_ticks_enabled
) {
684 return timers_state
.cpu_clock_offset
;
687 return ti
+ timers_state
.cpu_clock_offset
;
691 /* enable cpu_get_ticks() */
692 void cpu_enable_ticks(void)
694 if (!timers_state
.cpu_ticks_enabled
) {
695 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
696 timers_state
.cpu_clock_offset
-= get_clock();
697 timers_state
.cpu_ticks_enabled
= 1;
701 /* disable cpu_get_ticks() : the clock is stopped. You must not call
702 cpu_get_ticks() after that. */
703 void cpu_disable_ticks(void)
705 if (timers_state
.cpu_ticks_enabled
) {
706 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
707 timers_state
.cpu_clock_offset
= cpu_get_clock();
708 timers_state
.cpu_ticks_enabled
= 0;
712 /***********************************************************/
715 #define QEMU_CLOCK_REALTIME 0
716 #define QEMU_CLOCK_VIRTUAL 1
717 #define QEMU_CLOCK_HOST 2
721 /* XXX: add frequency */
729 struct QEMUTimer
*next
;
732 struct qemu_alarm_timer
{
736 int (*start
)(struct qemu_alarm_timer
*t
);
737 void (*stop
)(struct qemu_alarm_timer
*t
);
738 void (*rearm
)(struct qemu_alarm_timer
*t
);
742 #define ALARM_FLAG_DYNTICKS 0x1
743 #define ALARM_FLAG_EXPIRED 0x2
745 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
747 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
750 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
752 if (!alarm_has_dynticks(t
))
758 /* TODO: MIN_TIMER_REARM_US should be optimized */
759 #define MIN_TIMER_REARM_US 250
761 static struct qemu_alarm_timer
*alarm_timer
;
765 struct qemu_alarm_win32
{
768 } alarm_win32_data
= {0, -1};
770 static int win32_start_timer(struct qemu_alarm_timer
*t
);
771 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
772 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
776 static int unix_start_timer(struct qemu_alarm_timer
*t
);
777 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
781 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
782 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
783 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
785 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
786 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
788 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
789 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
791 #endif /* __linux__ */
795 /* Correlation between real and virtual time is always going to be
796 fairly approximate, so ignore small variation.
797 When the guest is idle real and virtual time will be aligned in
799 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
801 static void icount_adjust(void)
806 static int64_t last_delta
;
807 /* If the VM is not running, then do nothing. */
811 cur_time
= cpu_get_clock();
812 cur_icount
= qemu_get_clock(vm_clock
);
813 delta
= cur_icount
- cur_time
;
814 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
816 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
817 && icount_time_shift
> 0) {
818 /* The guest is getting too far ahead. Slow time down. */
822 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
823 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
824 /* The guest is getting too far behind. Speed time up. */
828 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
831 static void icount_adjust_rt(void * opaque
)
833 qemu_mod_timer(icount_rt_timer
,
834 qemu_get_clock(rt_clock
) + 1000);
838 static void icount_adjust_vm(void * opaque
)
840 qemu_mod_timer(icount_vm_timer
,
841 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
845 static void init_icount_adjust(void)
847 /* Have both realtime and virtual time triggers for speed adjustment.
848 The realtime trigger catches emulated time passing too slowly,
849 the virtual time trigger catches emulated time passing too fast.
850 Realtime triggers occur even when idle, so use them less frequently
852 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
853 qemu_mod_timer(icount_rt_timer
,
854 qemu_get_clock(rt_clock
) + 1000);
855 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
856 qemu_mod_timer(icount_vm_timer
,
857 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
860 static struct qemu_alarm_timer alarm_timers
[] = {
863 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
864 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
865 /* HPET - if available - is preferred */
866 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
867 /* ...otherwise try RTC */
868 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
870 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
872 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
873 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
874 {"win32", 0, win32_start_timer
,
875 win32_stop_timer
, NULL
, &alarm_win32_data
},
880 static void show_available_alarms(void)
884 printf("Available alarm timers, in order of precedence:\n");
885 for (i
= 0; alarm_timers
[i
].name
; i
++)
886 printf("%s\n", alarm_timers
[i
].name
);
889 static void configure_alarms(char const *opt
)
893 int count
= ARRAY_SIZE(alarm_timers
) - 1;
896 struct qemu_alarm_timer tmp
;
898 if (!strcmp(opt
, "?")) {
899 show_available_alarms();
903 arg
= qemu_strdup(opt
);
905 /* Reorder the array */
906 name
= strtok(arg
, ",");
908 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
909 if (!strcmp(alarm_timers
[i
].name
, name
))
914 fprintf(stderr
, "Unknown clock %s\n", name
);
923 tmp
= alarm_timers
[i
];
924 alarm_timers
[i
] = alarm_timers
[cur
];
925 alarm_timers
[cur
] = tmp
;
929 name
= strtok(NULL
, ",");
935 /* Disable remaining timers */
936 for (i
= cur
; i
< count
; i
++)
937 alarm_timers
[i
].name
= NULL
;
939 show_available_alarms();
944 #define QEMU_NUM_CLOCKS 3
948 QEMUClock
*host_clock
;
950 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
952 static QEMUClock
*qemu_new_clock(int type
)
955 clock
= qemu_mallocz(sizeof(QEMUClock
));
960 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
964 ts
= qemu_mallocz(sizeof(QEMUTimer
));
971 void qemu_free_timer(QEMUTimer
*ts
)
976 /* stop a timer, but do not dealloc it */
977 void qemu_del_timer(QEMUTimer
*ts
)
981 /* NOTE: this code must be signal safe because
982 qemu_timer_expired() can be called from a signal. */
983 pt
= &active_timers
[ts
->clock
->type
];
996 /* modify the current timer so that it will be fired when current_time
997 >= expire_time. The corresponding callback will be called. */
998 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1004 /* add the timer in the sorted list */
1005 /* NOTE: this code must be signal safe because
1006 qemu_timer_expired() can be called from a signal. */
1007 pt
= &active_timers
[ts
->clock
->type
];
1012 if (t
->expire_time
> expire_time
)
1016 ts
->expire_time
= expire_time
;
1020 /* Rearm if necessary */
1021 if (pt
== &active_timers
[ts
->clock
->type
]) {
1022 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1023 qemu_rearm_alarm_timer(alarm_timer
);
1025 /* Interrupt execution to force deadline recalculation. */
1027 qemu_notify_event();
1031 int qemu_timer_pending(QEMUTimer
*ts
)
1034 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1041 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1045 return (timer_head
->expire_time
<= current_time
);
1048 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1054 if (!ts
|| ts
->expire_time
> current_time
)
1056 /* remove timer from the list before calling the callback */
1057 *ptimer_head
= ts
->next
;
1060 /* run the callback (the timer list can be modified) */
1065 int64_t qemu_get_clock(QEMUClock
*clock
)
1067 switch(clock
->type
) {
1068 case QEMU_CLOCK_REALTIME
:
1069 return get_clock() / 1000000;
1071 case QEMU_CLOCK_VIRTUAL
:
1073 return cpu_get_icount();
1075 return cpu_get_clock();
1077 case QEMU_CLOCK_HOST
:
1078 return get_clock_realtime();
1082 static void init_clocks(void)
1085 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1086 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1087 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1089 rtc_clock
= host_clock
;
1093 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1095 uint64_t expire_time
;
1097 if (qemu_timer_pending(ts
)) {
1098 expire_time
= ts
->expire_time
;
1102 qemu_put_be64(f
, expire_time
);
1105 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1107 uint64_t expire_time
;
1109 expire_time
= qemu_get_be64(f
);
1110 if (expire_time
!= -1) {
1111 qemu_mod_timer(ts
, expire_time
);
1117 static const VMStateDescription vmstate_timers
= {
1120 .minimum_version_id
= 1,
1121 .minimum_version_id_old
= 1,
1122 .fields
= (VMStateField
[]) {
1123 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1124 VMSTATE_INT64(dummy
, TimersState
),
1125 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1126 VMSTATE_END_OF_LIST()
1130 static void qemu_event_increment(void);
1133 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1134 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1137 static void host_alarm_handler(int host_signum
)
1141 #define DISP_FREQ 1000
1143 static int64_t delta_min
= INT64_MAX
;
1144 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1146 ti
= qemu_get_clock(vm_clock
);
1147 if (last_clock
!= 0) {
1148 delta
= ti
- last_clock
;
1149 if (delta
< delta_min
)
1151 if (delta
> delta_max
)
1154 if (++count
== DISP_FREQ
) {
1155 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1156 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1157 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1158 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1159 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1161 delta_min
= INT64_MAX
;
1169 if (alarm_has_dynticks(alarm_timer
) ||
1171 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1172 qemu_get_clock(vm_clock
))) ||
1173 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1174 qemu_get_clock(rt_clock
)) ||
1175 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1176 qemu_get_clock(host_clock
))) {
1177 qemu_event_increment();
1178 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1180 #ifndef CONFIG_IOTHREAD
1182 /* stop the currently executing cpu because a timer occured */
1186 timer_alarm_pending
= 1;
1187 qemu_notify_event();
1191 static int64_t qemu_next_deadline(void)
1193 /* To avoid problems with overflow limit this to 2^32. */
1194 int64_t delta
= INT32_MAX
;
1196 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1197 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1198 qemu_get_clock(vm_clock
);
1200 if (active_timers
[QEMU_CLOCK_HOST
]) {
1201 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1202 qemu_get_clock(host_clock
);
1213 #if defined(__linux__)
1214 static uint64_t qemu_next_deadline_dyntick(void)
1222 delta
= (qemu_next_deadline() + 999) / 1000;
1224 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1225 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1226 qemu_get_clock(rt_clock
))*1000;
1227 if (rtdelta
< delta
)
1231 if (delta
< MIN_TIMER_REARM_US
)
1232 delta
= MIN_TIMER_REARM_US
;
1240 /* Sets a specific flag */
1241 static int fcntl_setfl(int fd
, int flag
)
1245 flags
= fcntl(fd
, F_GETFL
);
1249 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1255 #if defined(__linux__)
1257 #define RTC_FREQ 1024
1259 static void enable_sigio_timer(int fd
)
1261 struct sigaction act
;
1264 sigfillset(&act
.sa_mask
);
1266 act
.sa_handler
= host_alarm_handler
;
1268 sigaction(SIGIO
, &act
, NULL
);
1269 fcntl_setfl(fd
, O_ASYNC
);
1270 fcntl(fd
, F_SETOWN
, getpid());
1273 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1275 struct hpet_info info
;
1278 fd
= qemu_open("/dev/hpet", O_RDONLY
);
1283 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1285 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1286 "error, but for better emulation accuracy type:\n"
1287 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1291 /* Check capabilities */
1292 r
= ioctl(fd
, HPET_INFO
, &info
);
1296 /* Enable periodic mode */
1297 r
= ioctl(fd
, HPET_EPI
, 0);
1298 if (info
.hi_flags
&& (r
< 0))
1301 /* Enable interrupt */
1302 r
= ioctl(fd
, HPET_IE_ON
, 0);
1306 enable_sigio_timer(fd
);
1307 t
->priv
= (void *)(long)fd
;
1315 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1317 int fd
= (long)t
->priv
;
1322 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1325 unsigned long current_rtc_freq
= 0;
1327 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
1330 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1331 if (current_rtc_freq
!= RTC_FREQ
&&
1332 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1333 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1334 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1335 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1338 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1344 enable_sigio_timer(rtc_fd
);
1346 t
->priv
= (void *)(long)rtc_fd
;
1351 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1353 int rtc_fd
= (long)t
->priv
;
1358 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1362 struct sigaction act
;
1364 sigfillset(&act
.sa_mask
);
1366 act
.sa_handler
= host_alarm_handler
;
1368 sigaction(SIGALRM
, &act
, NULL
);
1371 * Initialize ev struct to 0 to avoid valgrind complaining
1372 * about uninitialized data in timer_create call
1374 memset(&ev
, 0, sizeof(ev
));
1375 ev
.sigev_value
.sival_int
= 0;
1376 ev
.sigev_notify
= SIGEV_SIGNAL
;
1377 ev
.sigev_signo
= SIGALRM
;
1379 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1380 perror("timer_create");
1382 /* disable dynticks */
1383 fprintf(stderr
, "Dynamic Ticks disabled\n");
1388 t
->priv
= (void *)(long)host_timer
;
1393 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1395 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1397 timer_delete(host_timer
);
1400 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1402 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1403 struct itimerspec timeout
;
1404 int64_t nearest_delta_us
= INT64_MAX
;
1407 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1408 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1409 !active_timers
[QEMU_CLOCK_HOST
])
1412 nearest_delta_us
= qemu_next_deadline_dyntick();
1414 /* check whether a timer is already running */
1415 if (timer_gettime(host_timer
, &timeout
)) {
1417 fprintf(stderr
, "Internal timer error: aborting\n");
1420 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1421 if (current_us
&& current_us
<= nearest_delta_us
)
1424 timeout
.it_interval
.tv_sec
= 0;
1425 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1426 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1427 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1428 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1430 fprintf(stderr
, "Internal timer error: aborting\n");
1435 #endif /* defined(__linux__) */
1437 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1439 struct sigaction act
;
1440 struct itimerval itv
;
1444 sigfillset(&act
.sa_mask
);
1446 act
.sa_handler
= host_alarm_handler
;
1448 sigaction(SIGALRM
, &act
, NULL
);
1450 itv
.it_interval
.tv_sec
= 0;
1451 /* for i386 kernel 2.6 to get 1 ms */
1452 itv
.it_interval
.tv_usec
= 999;
1453 itv
.it_value
.tv_sec
= 0;
1454 itv
.it_value
.tv_usec
= 10 * 1000;
1456 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1463 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1465 struct itimerval itv
;
1467 memset(&itv
, 0, sizeof(itv
));
1468 setitimer(ITIMER_REAL
, &itv
, NULL
);
1471 #endif /* !defined(_WIN32) */
1476 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1479 struct qemu_alarm_win32
*data
= t
->priv
;
1482 memset(&tc
, 0, sizeof(tc
));
1483 timeGetDevCaps(&tc
, sizeof(tc
));
1485 if (data
->period
< tc
.wPeriodMin
)
1486 data
->period
= tc
.wPeriodMin
;
1488 timeBeginPeriod(data
->period
);
1490 flags
= TIME_CALLBACK_FUNCTION
;
1491 if (alarm_has_dynticks(t
))
1492 flags
|= TIME_ONESHOT
;
1494 flags
|= TIME_PERIODIC
;
1496 data
->timerId
= timeSetEvent(1, // interval (ms)
1497 data
->period
, // resolution
1498 host_alarm_handler
, // function
1499 (DWORD
)t
, // parameter
1502 if (!data
->timerId
) {
1503 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1505 timeEndPeriod(data
->period
);
1512 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1514 struct qemu_alarm_win32
*data
= t
->priv
;
1516 timeKillEvent(data
->timerId
);
1517 timeEndPeriod(data
->period
);
1520 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1522 struct qemu_alarm_win32
*data
= t
->priv
;
1524 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1525 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1526 !active_timers
[QEMU_CLOCK_HOST
])
1529 timeKillEvent(data
->timerId
);
1531 data
->timerId
= timeSetEvent(1,
1535 TIME_ONESHOT
| TIME_PERIODIC
);
1537 if (!data
->timerId
) {
1538 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1541 timeEndPeriod(data
->period
);
1548 static int init_timer_alarm(void)
1550 struct qemu_alarm_timer
*t
= NULL
;
1553 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1554 t
= &alarm_timers
[i
];
1574 static void quit_timers(void)
1576 alarm_timer
->stop(alarm_timer
);
1580 /***********************************************************/
1581 /* host time/date access */
1582 void qemu_get_timedate(struct tm
*tm
, int offset
)
1589 if (rtc_date_offset
== -1) {
1593 ret
= localtime(&ti
);
1595 ti
-= rtc_date_offset
;
1599 memcpy(tm
, ret
, sizeof(struct tm
));
1602 int qemu_timedate_diff(struct tm
*tm
)
1606 if (rtc_date_offset
== -1)
1608 seconds
= mktimegm(tm
);
1610 seconds
= mktime(tm
);
1612 seconds
= mktimegm(tm
) + rtc_date_offset
;
1614 return seconds
- time(NULL
);
1617 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1619 time_t rtc_start_date
;
1622 if (!strcmp(startdate
, "now") && legacy
) {
1623 rtc_date_offset
= -1;
1625 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1633 } else if (sscanf(startdate
, "%d-%d-%d",
1636 &tm
.tm_mday
) == 3) {
1645 rtc_start_date
= mktimegm(&tm
);
1646 if (rtc_start_date
== -1) {
1648 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1649 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1652 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1656 static void configure_rtc(QemuOpts
*opts
)
1660 value
= qemu_opt_get(opts
, "base");
1662 if (!strcmp(value
, "utc")) {
1664 } else if (!strcmp(value
, "localtime")) {
1667 configure_rtc_date_offset(value
, 0);
1670 value
= qemu_opt_get(opts
, "clock");
1672 if (!strcmp(value
, "host")) {
1673 rtc_clock
= host_clock
;
1674 } else if (!strcmp(value
, "vm")) {
1675 rtc_clock
= vm_clock
;
1677 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1681 #ifdef CONFIG_TARGET_I386
1682 value
= qemu_opt_get(opts
, "driftfix");
1684 if (!strcmp(buf
, "slew")) {
1686 } else if (!strcmp(buf
, "none")) {
1689 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1697 static void socket_cleanup(void)
1702 static int socket_init(void)
1707 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1709 err
= WSAGetLastError();
1710 fprintf(stderr
, "WSAStartup: %d\n", err
);
1713 atexit(socket_cleanup
);
1718 /***********************************************************/
1719 /* Bluetooth support */
1722 static struct HCIInfo
*hci_table
[MAX_NICS
];
1724 static struct bt_vlan_s
{
1725 struct bt_scatternet_s net
;
1727 struct bt_vlan_s
*next
;
1730 /* find or alloc a new bluetooth "VLAN" */
1731 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1733 struct bt_vlan_s
**pvlan
, *vlan
;
1734 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1738 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1740 pvlan
= &first_bt_vlan
;
1741 while (*pvlan
!= NULL
)
1742 pvlan
= &(*pvlan
)->next
;
1747 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1751 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1756 static struct HCIInfo null_hci
= {
1757 .cmd_send
= null_hci_send
,
1758 .sco_send
= null_hci_send
,
1759 .acl_send
= null_hci_send
,
1760 .bdaddr_set
= null_hci_addr_set
,
1763 struct HCIInfo
*qemu_next_hci(void)
1765 if (cur_hci
== nb_hcis
)
1768 return hci_table
[cur_hci
++];
1771 static struct HCIInfo
*hci_init(const char *str
)
1774 struct bt_scatternet_s
*vlan
= 0;
1776 if (!strcmp(str
, "null"))
1779 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1781 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1782 else if (!strncmp(str
, "hci", 3)) {
1785 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1786 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1791 vlan
= qemu_find_bt_vlan(0);
1793 return bt_new_hci(vlan
);
1796 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1801 static int bt_hci_parse(const char *str
)
1803 struct HCIInfo
*hci
;
1806 if (nb_hcis
>= MAX_NICS
) {
1807 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1811 hci
= hci_init(str
);
1820 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1821 hci
->bdaddr_set(hci
, bdaddr
.b
);
1823 hci_table
[nb_hcis
++] = hci
;
1828 static void bt_vhci_add(int vlan_id
)
1830 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1833 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1834 "an empty scatternet %i\n", vlan_id
);
1836 bt_vhci_init(bt_new_hci(vlan
));
1839 static struct bt_device_s
*bt_device_add(const char *opt
)
1841 struct bt_scatternet_s
*vlan
;
1843 char *endp
= strstr(opt
, ",vlan=");
1844 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1847 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1850 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1852 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1857 vlan
= qemu_find_bt_vlan(vlan_id
);
1860 fprintf(stderr
, "qemu: warning: adding a slave device to "
1861 "an empty scatternet %i\n", vlan_id
);
1863 if (!strcmp(devname
, "keyboard"))
1864 return bt_keyboard_init(vlan
);
1866 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1870 static int bt_parse(const char *opt
)
1872 const char *endp
, *p
;
1875 if (strstart(opt
, "hci", &endp
)) {
1876 if (!*endp
|| *endp
== ',') {
1878 if (!strstart(endp
, ",vlan=", 0))
1881 return bt_hci_parse(opt
);
1883 } else if (strstart(opt
, "vhci", &endp
)) {
1884 if (!*endp
|| *endp
== ',') {
1886 if (strstart(endp
, ",vlan=", &p
)) {
1887 vlan
= strtol(p
, (char **) &endp
, 0);
1889 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1893 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1902 } else if (strstart(opt
, "device:", &endp
))
1903 return !bt_device_add(endp
);
1905 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1909 /***********************************************************/
1910 /* QEMU Block devices */
1912 #define HD_ALIAS "index=%d,media=disk"
1913 #define CDROM_ALIAS "index=2,media=cdrom"
1914 #define FD_ALIAS "index=%d,if=floppy"
1915 #define PFLASH_ALIAS "if=pflash"
1916 #define MTD_ALIAS "if=mtd"
1917 #define SD_ALIAS "index=0,if=sd"
1919 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1926 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1929 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1931 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1932 __FUNCTION__
, optstr
);
1936 qemu_opt_set(opts
, "file", file
);
1940 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1944 /* seek interface, bus and unit */
1946 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1947 if (dinfo
->type
== type
&&
1948 dinfo
->bus
== bus
&&
1949 dinfo
->unit
== unit
)
1956 DriveInfo
*drive_get_by_id(const char *id
)
1960 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1961 if (strcmp(id
, dinfo
->id
))
1968 int drive_get_max_bus(BlockInterfaceType type
)
1974 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1975 if(dinfo
->type
== type
&&
1976 dinfo
->bus
> max_bus
)
1977 max_bus
= dinfo
->bus
;
1982 const char *drive_get_serial(BlockDriverState
*bdrv
)
1986 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1987 if (dinfo
->bdrv
== bdrv
)
1988 return dinfo
->serial
;
1994 BlockInterfaceErrorAction
drive_get_on_error(
1995 BlockDriverState
*bdrv
, int is_read
)
2000 return BLOCK_ERR_REPORT
;
2003 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
2004 if (dinfo
->bdrv
== bdrv
)
2005 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
2008 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
2011 static void bdrv_format_print(void *opaque
, const char *name
)
2013 fprintf(stderr
, " %s", name
);
2016 void drive_uninit(DriveInfo
*dinfo
)
2018 qemu_opts_del(dinfo
->opts
);
2019 bdrv_delete(dinfo
->bdrv
);
2020 QTAILQ_REMOVE(&drives
, dinfo
, next
);
2024 static int parse_block_error_action(const char *buf
, int is_read
)
2026 if (!strcmp(buf
, "ignore")) {
2027 return BLOCK_ERR_IGNORE
;
2028 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
2029 return BLOCK_ERR_STOP_ENOSPC
;
2030 } else if (!strcmp(buf
, "stop")) {
2031 return BLOCK_ERR_STOP_ANY
;
2032 } else if (!strcmp(buf
, "report")) {
2033 return BLOCK_ERR_REPORT
;
2035 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
2036 buf
, is_read
? "read" : "write");
2041 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
2045 const char *file
= NULL
;
2048 const char *mediastr
= "";
2049 BlockInterfaceType type
;
2050 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2051 int bus_id
, unit_id
;
2052 int cyls
, heads
, secs
, translation
;
2053 BlockDriver
*drv
= NULL
;
2054 QEMUMachine
*machine
= opaque
;
2061 int on_read_error
, on_write_error
;
2062 const char *devaddr
;
2068 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2071 if (machine
&& machine
->use_scsi
) {
2073 max_devs
= MAX_SCSI_DEVS
;
2074 pstrcpy(devname
, sizeof(devname
), "scsi");
2077 max_devs
= MAX_IDE_DEVS
;
2078 pstrcpy(devname
, sizeof(devname
), "ide");
2082 /* extract parameters */
2083 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2084 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2085 index
= qemu_opt_get_number(opts
, "index", -1);
2087 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2088 heads
= qemu_opt_get_number(opts
, "heads", 0);
2089 secs
= qemu_opt_get_number(opts
, "secs", 0);
2091 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2092 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2094 file
= qemu_opt_get(opts
, "file");
2095 serial
= qemu_opt_get(opts
, "serial");
2097 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2098 pstrcpy(devname
, sizeof(devname
), buf
);
2099 if (!strcmp(buf
, "ide")) {
2101 max_devs
= MAX_IDE_DEVS
;
2102 } else if (!strcmp(buf
, "scsi")) {
2104 max_devs
= MAX_SCSI_DEVS
;
2105 } else if (!strcmp(buf
, "floppy")) {
2108 } else if (!strcmp(buf
, "pflash")) {
2111 } else if (!strcmp(buf
, "mtd")) {
2114 } else if (!strcmp(buf
, "sd")) {
2117 } else if (!strcmp(buf
, "virtio")) {
2120 } else if (!strcmp(buf
, "xen")) {
2123 } else if (!strcmp(buf
, "none")) {
2127 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2132 if (cyls
|| heads
|| secs
) {
2133 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2134 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2137 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2138 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2141 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2142 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2147 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2150 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2154 if (!strcmp(buf
, "none"))
2155 translation
= BIOS_ATA_TRANSLATION_NONE
;
2156 else if (!strcmp(buf
, "lba"))
2157 translation
= BIOS_ATA_TRANSLATION_LBA
;
2158 else if (!strcmp(buf
, "auto"))
2159 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2161 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2166 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2167 if (!strcmp(buf
, "disk")) {
2169 } else if (!strcmp(buf
, "cdrom")) {
2170 if (cyls
|| secs
|| heads
) {
2172 "qemu: '%s' invalid physical CHS format\n", buf
);
2175 media
= MEDIA_CDROM
;
2177 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2182 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2183 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2185 else if (!strcmp(buf
, "writethrough"))
2187 else if (!strcmp(buf
, "writeback"))
2190 fprintf(stderr
, "qemu: invalid cache option\n");
2195 #ifdef CONFIG_LINUX_AIO
2196 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2197 if (!strcmp(buf
, "threads"))
2199 else if (!strcmp(buf
, "native"))
2202 fprintf(stderr
, "qemu: invalid aio option\n");
2208 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2209 if (strcmp(buf
, "?") == 0) {
2210 fprintf(stderr
, "qemu: Supported formats:");
2211 bdrv_iterate_format(bdrv_format_print
, NULL
);
2212 fprintf(stderr
, "\n");
2215 drv
= bdrv_find_whitelisted_format(buf
);
2217 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2222 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
2223 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2224 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2225 fprintf(stderr
, "werror is no supported by this format\n");
2229 on_write_error
= parse_block_error_action(buf
, 0);
2230 if (on_write_error
< 0) {
2235 on_read_error
= BLOCK_ERR_REPORT
;
2236 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
2237 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
2238 fprintf(stderr
, "rerror is no supported by this format\n");
2242 on_read_error
= parse_block_error_action(buf
, 1);
2243 if (on_read_error
< 0) {
2248 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2249 if (type
!= IF_VIRTIO
) {
2250 fprintf(stderr
, "addr is not supported\n");
2255 /* compute bus and unit according index */
2258 if (bus_id
!= 0 || unit_id
!= -1) {
2260 "qemu: index cannot be used with bus and unit\n");
2268 unit_id
= index
% max_devs
;
2269 bus_id
= index
/ max_devs
;
2273 /* if user doesn't specify a unit_id,
2274 * try to find the first free
2277 if (unit_id
== -1) {
2279 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2281 if (max_devs
&& unit_id
>= max_devs
) {
2282 unit_id
-= max_devs
;
2290 if (max_devs
&& unit_id
>= max_devs
) {
2291 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2292 unit_id
, max_devs
- 1);
2297 * ignore multiple definitions
2300 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2307 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2308 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2309 dinfo
->id
= qemu_strdup(buf
);
2311 /* no id supplied -> create one */
2312 dinfo
->id
= qemu_mallocz(32);
2313 if (type
== IF_IDE
|| type
== IF_SCSI
)
2314 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2316 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2317 devname
, bus_id
, mediastr
, unit_id
);
2319 snprintf(dinfo
->id
, 32, "%s%s%i",
2320 devname
, mediastr
, unit_id
);
2322 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2323 dinfo
->devaddr
= devaddr
;
2325 dinfo
->bus
= bus_id
;
2326 dinfo
->unit
= unit_id
;
2327 dinfo
->on_read_error
= on_read_error
;
2328 dinfo
->on_write_error
= on_write_error
;
2331 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2332 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2342 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2343 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2347 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2352 /* FIXME: This isn't really a floppy, but it's a reasonable
2355 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2361 /* add virtio block device */
2362 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2363 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2364 qemu_opt_set(opts
, "drive", dinfo
->id
);
2366 qemu_opt_set(opts
, "addr", devaddr
);
2377 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2378 cache
= 2; /* always use write-back with snapshot */
2380 if (cache
== 0) /* no caching */
2381 bdrv_flags
|= BDRV_O_NOCACHE
;
2382 else if (cache
== 2) /* write-back */
2383 bdrv_flags
|= BDRV_O_CACHE_WB
;
2386 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2388 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2392 if (type
== IF_IDE
) {
2393 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2396 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2399 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2400 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2401 file
, strerror(errno
));
2405 if (bdrv_key_required(dinfo
->bdrv
))
2411 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2413 QEMUMachine
*machine
= opaque
;
2414 int fatal_error
= 0;
2416 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2423 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2425 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2426 qemu_opt_set(opts
, "snapshot", "on");
2431 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2433 boot_set_handler
= func
;
2434 boot_set_opaque
= opaque
;
2437 int qemu_boot_set(const char *boot_devices
)
2439 if (!boot_set_handler
) {
2442 return boot_set_handler(boot_set_opaque
, boot_devices
);
2445 static int parse_bootdevices(char *devices
)
2447 /* We just do some generic consistency checks */
2451 for (p
= devices
; *p
!= '\0'; p
++) {
2452 /* Allowed boot devices are:
2453 * a-b: floppy disk drives
2454 * c-f: IDE disk drives
2455 * g-m: machine implementation dependant drives
2456 * n-p: network devices
2457 * It's up to each machine implementation to check if the given boot
2458 * devices match the actual hardware implementation and firmware
2461 if (*p
< 'a' || *p
> 'p') {
2462 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2465 if (bitmap
& (1 << (*p
- 'a'))) {
2466 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2469 bitmap
|= 1 << (*p
- 'a');
2474 static void restore_boot_devices(void *opaque
)
2476 char *standard_boot_devices
= opaque
;
2478 qemu_boot_set(standard_boot_devices
);
2480 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2481 qemu_free(standard_boot_devices
);
2484 static void numa_add(const char *optarg
)
2488 unsigned long long value
, endvalue
;
2491 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2492 if (!strcmp(option
, "node")) {
2493 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2494 nodenr
= nb_numa_nodes
;
2496 nodenr
= strtoull(option
, NULL
, 10);
2499 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2500 node_mem
[nodenr
] = 0;
2502 value
= strtoull(option
, &endptr
, 0);
2504 case 0: case 'M': case 'm':
2511 node_mem
[nodenr
] = value
;
2513 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2514 node_cpumask
[nodenr
] = 0;
2516 value
= strtoull(option
, &endptr
, 10);
2519 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2521 if (*endptr
== '-') {
2522 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2523 if (endvalue
>= 63) {
2526 "only 63 CPUs in NUMA mode supported.\n");
2528 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2533 node_cpumask
[nodenr
] = value
;
2540 static void smp_parse(const char *optarg
)
2542 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2546 smp
= strtoul(optarg
, &endptr
, 10);
2547 if (endptr
!= optarg
) {
2548 if (*endptr
== ',') {
2552 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2553 sockets
= strtoull(option
, NULL
, 10);
2554 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2555 cores
= strtoull(option
, NULL
, 10);
2556 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2557 threads
= strtoull(option
, NULL
, 10);
2558 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2559 max_cpus
= strtoull(option
, NULL
, 10);
2561 /* compute missing values, prefer sockets over cores over threads */
2562 if (smp
== 0 || sockets
== 0) {
2563 sockets
= sockets
> 0 ? sockets
: 1;
2564 cores
= cores
> 0 ? cores
: 1;
2565 threads
= threads
> 0 ? threads
: 1;
2567 smp
= cores
* threads
* sockets
;
2569 sockets
= smp
/ (cores
* threads
);
2573 threads
= threads
> 0 ? threads
: 1;
2574 cores
= smp
/ (sockets
* threads
);
2577 sockets
= smp
/ (cores
* threads
);
2579 threads
= smp
/ (cores
* sockets
);
2584 smp_cores
= cores
> 0 ? cores
: 1;
2585 smp_threads
= threads
> 0 ? threads
: 1;
2587 max_cpus
= smp_cpus
;
2590 /***********************************************************/
2593 static int usb_device_add(const char *devname
, int is_hotplug
)
2596 USBDevice
*dev
= NULL
;
2601 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2602 dev
= usbdevice_create(devname
);
2606 /* the other ones */
2607 if (strstart(devname
, "host:", &p
)) {
2608 dev
= usb_host_device_open(p
);
2609 } else if (strstart(devname
, "net:", &p
)) {
2613 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2618 qemu_opt_set(opts
, "type", "nic");
2619 qemu_opt_set(opts
, "model", "usb");
2621 idx
= net_client_init(NULL
, opts
, 0);
2626 dev
= usb_net_init(&nd_table
[idx
]);
2627 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2628 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2629 bt_new_hci(qemu_find_bt_vlan(0)));
2640 static int usb_device_del(const char *devname
)
2645 if (strstart(devname
, "host:", &p
))
2646 return usb_host_device_close(p
);
2651 p
= strchr(devname
, '.');
2654 bus_num
= strtoul(devname
, NULL
, 0);
2655 addr
= strtoul(p
+ 1, NULL
, 0);
2657 return usb_device_delete_addr(bus_num
, addr
);
2660 static int usb_parse(const char *cmdline
)
2662 return usb_device_add(cmdline
, 0);
2665 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2667 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2670 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2672 usb_device_del(qdict_get_str(qdict
, "devname"));
2675 /***********************************************************/
2676 /* PCMCIA/Cardbus */
2678 static struct pcmcia_socket_entry_s
{
2679 PCMCIASocket
*socket
;
2680 struct pcmcia_socket_entry_s
*next
;
2681 } *pcmcia_sockets
= 0;
2683 void pcmcia_socket_register(PCMCIASocket
*socket
)
2685 struct pcmcia_socket_entry_s
*entry
;
2687 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2688 entry
->socket
= socket
;
2689 entry
->next
= pcmcia_sockets
;
2690 pcmcia_sockets
= entry
;
2693 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2695 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2697 ptr
= &pcmcia_sockets
;
2698 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2699 if (entry
->socket
== socket
) {
2705 void pcmcia_info(Monitor
*mon
)
2707 struct pcmcia_socket_entry_s
*iter
;
2709 if (!pcmcia_sockets
)
2710 monitor_printf(mon
, "No PCMCIA sockets\n");
2712 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2713 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2714 iter
->socket
->attached
? iter
->socket
->card_string
:
2718 /***********************************************************/
2719 /* register display */
2721 struct DisplayAllocator default_allocator
= {
2722 defaultallocator_create_displaysurface
,
2723 defaultallocator_resize_displaysurface
,
2724 defaultallocator_free_displaysurface
2727 void register_displaystate(DisplayState
*ds
)
2737 DisplayState
*get_displaystate(void)
2739 return display_state
;
2742 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2744 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2745 return ds
->allocator
;
2750 static void dumb_display_init(void)
2752 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2753 ds
->allocator
= &default_allocator
;
2754 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2755 register_displaystate(ds
);
2758 /***********************************************************/
2761 typedef struct IOHandlerRecord
{
2763 IOCanRWHandler
*fd_read_poll
;
2765 IOHandler
*fd_write
;
2768 /* temporary data */
2770 struct IOHandlerRecord
*next
;
2773 static IOHandlerRecord
*first_io_handler
;
2775 /* XXX: fd_read_poll should be suppressed, but an API change is
2776 necessary in the character devices to suppress fd_can_read(). */
2777 int qemu_set_fd_handler2(int fd
,
2778 IOCanRWHandler
*fd_read_poll
,
2780 IOHandler
*fd_write
,
2783 IOHandlerRecord
**pioh
, *ioh
;
2785 if (!fd_read
&& !fd_write
) {
2786 pioh
= &first_io_handler
;
2791 if (ioh
->fd
== fd
) {
2798 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2802 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2803 ioh
->next
= first_io_handler
;
2804 first_io_handler
= ioh
;
2807 ioh
->fd_read_poll
= fd_read_poll
;
2808 ioh
->fd_read
= fd_read
;
2809 ioh
->fd_write
= fd_write
;
2810 ioh
->opaque
= opaque
;
2816 int qemu_set_fd_handler(int fd
,
2818 IOHandler
*fd_write
,
2821 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2825 /***********************************************************/
2826 /* Polling handling */
2828 typedef struct PollingEntry
{
2831 struct PollingEntry
*next
;
2834 static PollingEntry
*first_polling_entry
;
2836 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2838 PollingEntry
**ppe
, *pe
;
2839 pe
= qemu_mallocz(sizeof(PollingEntry
));
2841 pe
->opaque
= opaque
;
2842 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2847 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2849 PollingEntry
**ppe
, *pe
;
2850 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2852 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2860 /***********************************************************/
2861 /* Wait objects support */
2862 typedef struct WaitObjects
{
2864 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2865 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2866 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2869 static WaitObjects wait_objects
= {0};
2871 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2873 WaitObjects
*w
= &wait_objects
;
2875 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2877 w
->events
[w
->num
] = handle
;
2878 w
->func
[w
->num
] = func
;
2879 w
->opaque
[w
->num
] = opaque
;
2884 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2887 WaitObjects
*w
= &wait_objects
;
2890 for (i
= 0; i
< w
->num
; i
++) {
2891 if (w
->events
[i
] == handle
)
2894 w
->events
[i
] = w
->events
[i
+ 1];
2895 w
->func
[i
] = w
->func
[i
+ 1];
2896 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2904 /***********************************************************/
2905 /* ram save/restore */
2907 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2908 #define RAM_SAVE_FLAG_COMPRESS 0x02
2909 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2910 #define RAM_SAVE_FLAG_PAGE 0x08
2911 #define RAM_SAVE_FLAG_EOS 0x10
2913 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2915 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2916 uint32_t *array
= (uint32_t *)page
;
2919 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2920 if (array
[i
] != val
)
2927 static int ram_save_block(QEMUFile
*f
)
2929 static ram_addr_t current_addr
= 0;
2930 ram_addr_t saved_addr
= current_addr
;
2931 ram_addr_t addr
= 0;
2934 while (addr
< last_ram_offset
) {
2935 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2938 cpu_physical_memory_reset_dirty(current_addr
,
2939 current_addr
+ TARGET_PAGE_SIZE
,
2940 MIGRATION_DIRTY_FLAG
);
2942 p
= qemu_get_ram_ptr(current_addr
);
2944 if (is_dup_page(p
, *p
)) {
2945 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2946 qemu_put_byte(f
, *p
);
2948 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2949 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2955 addr
+= TARGET_PAGE_SIZE
;
2956 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2962 static uint64_t bytes_transferred
;
2964 static ram_addr_t
ram_save_remaining(void)
2967 ram_addr_t count
= 0;
2969 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2970 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2977 uint64_t ram_bytes_remaining(void)
2979 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2982 uint64_t ram_bytes_transferred(void)
2984 return bytes_transferred
;
2987 uint64_t ram_bytes_total(void)
2989 return last_ram_offset
;
2992 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
2995 uint64_t bytes_transferred_last
;
2997 uint64_t expected_time
= 0;
3000 cpu_physical_memory_set_dirty_tracking(0);
3004 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3005 qemu_file_set_error(f
);
3010 bytes_transferred
= 0;
3012 /* Make sure all dirty bits are set */
3013 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3014 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3015 cpu_physical_memory_set_dirty(addr
);
3018 /* Enable dirty memory tracking */
3019 cpu_physical_memory_set_dirty_tracking(1);
3021 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3024 bytes_transferred_last
= bytes_transferred
;
3025 bwidth
= get_clock();
3027 while (!qemu_file_rate_limit(f
)) {
3030 ret
= ram_save_block(f
);
3031 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3032 if (ret
== 0) /* no more blocks */
3036 bwidth
= get_clock() - bwidth
;
3037 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3039 /* if we haven't transferred anything this round, force expected_time to a
3040 * a very high value, but without crashing */
3044 /* try transferring iterative blocks of memory */
3046 /* flush all remaining blocks regardless of rate limiting */
3047 while (ram_save_block(f
) != 0) {
3048 bytes_transferred
+= TARGET_PAGE_SIZE
;
3050 cpu_physical_memory_set_dirty_tracking(0);
3053 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3055 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3057 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3060 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3065 if (version_id
!= 3)
3069 addr
= qemu_get_be64(f
);
3071 flags
= addr
& ~TARGET_PAGE_MASK
;
3072 addr
&= TARGET_PAGE_MASK
;
3074 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3075 if (addr
!= last_ram_offset
)
3079 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3080 uint8_t ch
= qemu_get_byte(f
);
3081 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3084 (!kvm_enabled() || kvm_has_sync_mmu())) {
3085 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3088 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
3089 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3091 if (qemu_file_has_error(f
)) {
3094 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3099 void qemu_service_io(void)
3101 qemu_notify_event();
3104 /***********************************************************/
3105 /* machine registration */
3107 static QEMUMachine
*first_machine
= NULL
;
3108 QEMUMachine
*current_machine
= NULL
;
3110 int qemu_register_machine(QEMUMachine
*m
)
3113 pm
= &first_machine
;
3121 static QEMUMachine
*find_machine(const char *name
)
3125 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3126 if (!strcmp(m
->name
, name
))
3128 if (m
->alias
&& !strcmp(m
->alias
, name
))
3134 static QEMUMachine
*find_default_machine(void)
3138 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3139 if (m
->is_default
) {
3146 /***********************************************************/
3147 /* main execution loop */
3149 static void gui_update(void *opaque
)
3151 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3152 DisplayState
*ds
= opaque
;
3153 DisplayChangeListener
*dcl
= ds
->listeners
;
3157 while (dcl
!= NULL
) {
3158 if (dcl
->gui_timer_interval
&&
3159 dcl
->gui_timer_interval
< interval
)
3160 interval
= dcl
->gui_timer_interval
;
3163 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3166 static void nographic_update(void *opaque
)
3168 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3170 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3173 struct vm_change_state_entry
{
3174 VMChangeStateHandler
*cb
;
3176 QLIST_ENTRY (vm_change_state_entry
) entries
;
3179 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3181 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3184 VMChangeStateEntry
*e
;
3186 e
= qemu_mallocz(sizeof (*e
));
3190 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3194 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3196 QLIST_REMOVE (e
, entries
);
3200 static void vm_state_notify(int running
, int reason
)
3202 VMChangeStateEntry
*e
;
3204 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3205 e
->cb(e
->opaque
, running
, reason
);
3209 static void resume_all_vcpus(void);
3210 static void pause_all_vcpus(void);
3217 vm_state_notify(1, 0);
3218 qemu_rearm_alarm_timer(alarm_timer
);
3223 /* reset/shutdown handler */
3225 typedef struct QEMUResetEntry
{
3226 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3227 QEMUResetHandler
*func
;
3231 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3232 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3233 static int reset_requested
;
3234 static int shutdown_requested
;
3235 static int powerdown_requested
;
3236 static int debug_requested
;
3237 static int vmstop_requested
;
3239 int qemu_shutdown_requested(void)
3241 int r
= shutdown_requested
;
3242 shutdown_requested
= 0;
3246 int qemu_reset_requested(void)
3248 int r
= reset_requested
;
3249 reset_requested
= 0;
3253 int qemu_powerdown_requested(void)
3255 int r
= powerdown_requested
;
3256 powerdown_requested
= 0;
3260 static int qemu_debug_requested(void)
3262 int r
= debug_requested
;
3263 debug_requested
= 0;
3267 static int qemu_vmstop_requested(void)
3269 int r
= vmstop_requested
;
3270 vmstop_requested
= 0;
3274 static void do_vm_stop(int reason
)
3277 cpu_disable_ticks();
3280 vm_state_notify(0, reason
);
3284 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3286 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3289 re
->opaque
= opaque
;
3290 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3293 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3297 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3298 if (re
->func
== func
&& re
->opaque
== opaque
) {
3299 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3306 void qemu_system_reset(void)
3308 QEMUResetEntry
*re
, *nre
;
3310 /* reset all devices */
3311 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3312 re
->func(re
->opaque
);
3316 void qemu_system_reset_request(void)
3319 shutdown_requested
= 1;
3321 reset_requested
= 1;
3323 qemu_notify_event();
3326 void qemu_system_shutdown_request(void)
3328 shutdown_requested
= 1;
3329 qemu_notify_event();
3332 void qemu_system_powerdown_request(void)
3334 powerdown_requested
= 1;
3335 qemu_notify_event();
3338 #ifdef CONFIG_IOTHREAD
3339 static void qemu_system_vmstop_request(int reason
)
3341 vmstop_requested
= reason
;
3342 qemu_notify_event();
3347 static int io_thread_fd
= -1;
3349 static void qemu_event_increment(void)
3351 static const char byte
= 0;
3353 if (io_thread_fd
== -1)
3356 write(io_thread_fd
, &byte
, sizeof(byte
));
3359 static void qemu_event_read(void *opaque
)
3361 int fd
= (unsigned long)opaque
;
3364 /* Drain the notify pipe */
3367 len
= read(fd
, buffer
, sizeof(buffer
));
3368 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3371 static int qemu_event_init(void)
3376 err
= qemu_pipe(fds
);
3380 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3384 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3388 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3389 (void *)(unsigned long)fds
[0]);
3391 io_thread_fd
= fds
[1];
3400 HANDLE qemu_event_handle
;
3402 static void dummy_event_handler(void *opaque
)
3406 static int qemu_event_init(void)
3408 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3409 if (!qemu_event_handle
) {
3410 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3413 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3417 static void qemu_event_increment(void)
3419 if (!SetEvent(qemu_event_handle
)) {
3420 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3427 static int cpu_can_run(CPUState
*env
)
3436 #ifndef CONFIG_IOTHREAD
3437 static int qemu_init_main_loop(void)
3439 return qemu_event_init();
3442 void qemu_init_vcpu(void *_env
)
3444 CPUState
*env
= _env
;
3448 env
->nr_cores
= smp_cores
;
3449 env
->nr_threads
= smp_threads
;
3453 int qemu_cpu_self(void *env
)
3458 static void resume_all_vcpus(void)
3462 static void pause_all_vcpus(void)
3466 void qemu_cpu_kick(void *env
)
3471 void qemu_notify_event(void)
3473 CPUState
*env
= cpu_single_env
;
3480 void qemu_mutex_lock_iothread(void) {}
3481 void qemu_mutex_unlock_iothread(void) {}
3483 void vm_stop(int reason
)
3488 #else /* CONFIG_IOTHREAD */
3490 #include "qemu-thread.h"
3492 QemuMutex qemu_global_mutex
;
3493 static QemuMutex qemu_fair_mutex
;
3495 static QemuThread io_thread
;
3497 static QemuThread
*tcg_cpu_thread
;
3498 static QemuCond
*tcg_halt_cond
;
3500 static int qemu_system_ready
;
3502 static QemuCond qemu_cpu_cond
;
3504 static QemuCond qemu_system_cond
;
3505 static QemuCond qemu_pause_cond
;
3507 static void block_io_signals(void);
3508 static void unblock_io_signals(void);
3509 static int tcg_has_work(void);
3511 static int qemu_init_main_loop(void)
3515 ret
= qemu_event_init();
3519 qemu_cond_init(&qemu_pause_cond
);
3520 qemu_mutex_init(&qemu_fair_mutex
);
3521 qemu_mutex_init(&qemu_global_mutex
);
3522 qemu_mutex_lock(&qemu_global_mutex
);
3524 unblock_io_signals();
3525 qemu_thread_self(&io_thread
);
3530 static void qemu_wait_io_event(CPUState
*env
)
3532 while (!tcg_has_work())
3533 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3535 qemu_mutex_unlock(&qemu_global_mutex
);
3538 * Users of qemu_global_mutex can be starved, having no chance
3539 * to acquire it since this path will get to it first.
3540 * So use another lock to provide fairness.
3542 qemu_mutex_lock(&qemu_fair_mutex
);
3543 qemu_mutex_unlock(&qemu_fair_mutex
);
3545 qemu_mutex_lock(&qemu_global_mutex
);
3549 qemu_cond_signal(&qemu_pause_cond
);
3553 static int qemu_cpu_exec(CPUState
*env
);
3555 static void *kvm_cpu_thread_fn(void *arg
)
3557 CPUState
*env
= arg
;
3560 qemu_thread_self(env
->thread
);
3564 /* signal CPU creation */
3565 qemu_mutex_lock(&qemu_global_mutex
);
3567 qemu_cond_signal(&qemu_cpu_cond
);
3569 /* and wait for machine initialization */
3570 while (!qemu_system_ready
)
3571 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3574 if (cpu_can_run(env
))
3576 qemu_wait_io_event(env
);
3582 static void tcg_cpu_exec(void);
3584 static void *tcg_cpu_thread_fn(void *arg
)
3586 CPUState
*env
= arg
;
3589 qemu_thread_self(env
->thread
);
3591 /* signal CPU creation */
3592 qemu_mutex_lock(&qemu_global_mutex
);
3593 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3595 qemu_cond_signal(&qemu_cpu_cond
);
3597 /* and wait for machine initialization */
3598 while (!qemu_system_ready
)
3599 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3603 qemu_wait_io_event(cur_cpu
);
3609 void qemu_cpu_kick(void *_env
)
3611 CPUState
*env
= _env
;
3612 qemu_cond_broadcast(env
->halt_cond
);
3614 qemu_thread_signal(env
->thread
, SIGUSR1
);
3617 int qemu_cpu_self(void *_env
)
3619 CPUState
*env
= _env
;
3622 qemu_thread_self(&this);
3624 return qemu_thread_equal(&this, env
->thread
);
3627 static void cpu_signal(int sig
)
3630 cpu_exit(cpu_single_env
);
3633 static void block_io_signals(void)
3636 struct sigaction sigact
;
3639 sigaddset(&set
, SIGUSR2
);
3640 sigaddset(&set
, SIGIO
);
3641 sigaddset(&set
, SIGALRM
);
3642 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3645 sigaddset(&set
, SIGUSR1
);
3646 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3648 memset(&sigact
, 0, sizeof(sigact
));
3649 sigact
.sa_handler
= cpu_signal
;
3650 sigaction(SIGUSR1
, &sigact
, NULL
);
3653 static void unblock_io_signals(void)
3658 sigaddset(&set
, SIGUSR2
);
3659 sigaddset(&set
, SIGIO
);
3660 sigaddset(&set
, SIGALRM
);
3661 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3664 sigaddset(&set
, SIGUSR1
);
3665 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3668 static void qemu_signal_lock(unsigned int msecs
)
3670 qemu_mutex_lock(&qemu_fair_mutex
);
3672 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3673 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3674 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3677 qemu_mutex_unlock(&qemu_fair_mutex
);
3680 void qemu_mutex_lock_iothread(void)
3682 if (kvm_enabled()) {
3683 qemu_mutex_lock(&qemu_fair_mutex
);
3684 qemu_mutex_lock(&qemu_global_mutex
);
3685 qemu_mutex_unlock(&qemu_fair_mutex
);
3687 qemu_signal_lock(100);
3690 void qemu_mutex_unlock_iothread(void)
3692 qemu_mutex_unlock(&qemu_global_mutex
);
3695 static int all_vcpus_paused(void)
3697 CPUState
*penv
= first_cpu
;
3702 penv
= (CPUState
*)penv
->next_cpu
;
3708 static void pause_all_vcpus(void)
3710 CPUState
*penv
= first_cpu
;
3714 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3715 qemu_cpu_kick(penv
);
3716 penv
= (CPUState
*)penv
->next_cpu
;
3719 while (!all_vcpus_paused()) {
3720 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3723 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3724 penv
= (CPUState
*)penv
->next_cpu
;
3729 static void resume_all_vcpus(void)
3731 CPUState
*penv
= first_cpu
;
3736 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3737 qemu_cpu_kick(penv
);
3738 penv
= (CPUState
*)penv
->next_cpu
;
3742 static void tcg_init_vcpu(void *_env
)
3744 CPUState
*env
= _env
;
3745 /* share a single thread for all cpus with TCG */
3746 if (!tcg_cpu_thread
) {
3747 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3748 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3749 qemu_cond_init(env
->halt_cond
);
3750 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3751 while (env
->created
== 0)
3752 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3753 tcg_cpu_thread
= env
->thread
;
3754 tcg_halt_cond
= env
->halt_cond
;
3756 env
->thread
= tcg_cpu_thread
;
3757 env
->halt_cond
= tcg_halt_cond
;
3761 static void kvm_start_vcpu(CPUState
*env
)
3763 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3764 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3765 qemu_cond_init(env
->halt_cond
);
3766 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3767 while (env
->created
== 0)
3768 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3771 void qemu_init_vcpu(void *_env
)
3773 CPUState
*env
= _env
;
3776 kvm_start_vcpu(env
);
3779 env
->nr_cores
= smp_cores
;
3780 env
->nr_threads
= smp_threads
;
3783 void qemu_notify_event(void)
3785 qemu_event_increment();
3788 void vm_stop(int reason
)
3791 qemu_thread_self(&me
);
3793 if (!qemu_thread_equal(&me
, &io_thread
)) {
3794 qemu_system_vmstop_request(reason
);
3796 * FIXME: should not return to device code in case
3797 * vm_stop() has been requested.
3799 if (cpu_single_env
) {
3800 cpu_exit(cpu_single_env
);
3801 cpu_single_env
->stop
= 1;
3812 static void host_main_loop_wait(int *timeout
)
3818 /* XXX: need to suppress polling by better using win32 events */
3820 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3821 ret
|= pe
->func(pe
->opaque
);
3825 WaitObjects
*w
= &wait_objects
;
3827 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3828 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3829 if (w
->func
[ret
- WAIT_OBJECT_0
])
3830 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3832 /* Check for additional signaled events */
3833 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3835 /* Check if event is signaled */
3836 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3837 if(ret2
== WAIT_OBJECT_0
) {
3839 w
->func
[i
](w
->opaque
[i
]);
3840 } else if (ret2
== WAIT_TIMEOUT
) {
3842 err
= GetLastError();
3843 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3846 } else if (ret
== WAIT_TIMEOUT
) {
3848 err
= GetLastError();
3849 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3856 static void host_main_loop_wait(int *timeout
)
3861 void main_loop_wait(int timeout
)
3863 IOHandlerRecord
*ioh
;
3864 fd_set rfds
, wfds
, xfds
;
3868 qemu_bh_update_timeout(&timeout
);
3870 host_main_loop_wait(&timeout
);
3872 /* poll any events */
3873 /* XXX: separate device handlers from system ones */
3878 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3882 (!ioh
->fd_read_poll
||
3883 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3884 FD_SET(ioh
->fd
, &rfds
);
3888 if (ioh
->fd_write
) {
3889 FD_SET(ioh
->fd
, &wfds
);
3895 tv
.tv_sec
= timeout
/ 1000;
3896 tv
.tv_usec
= (timeout
% 1000) * 1000;
3898 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3900 qemu_mutex_unlock_iothread();
3901 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3902 qemu_mutex_lock_iothread();
3904 IOHandlerRecord
**pioh
;
3906 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3907 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3908 ioh
->fd_read(ioh
->opaque
);
3910 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3911 ioh
->fd_write(ioh
->opaque
);
3915 /* remove deleted IO handlers */
3916 pioh
= &first_io_handler
;
3927 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3929 /* rearm timer, if not periodic */
3930 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3931 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3932 qemu_rearm_alarm_timer(alarm_timer
);
3935 /* vm time timers */
3937 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3938 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3939 qemu_get_clock(vm_clock
));
3942 /* real time timers */
3943 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3944 qemu_get_clock(rt_clock
));
3946 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3947 qemu_get_clock(host_clock
));
3949 /* Check bottom-halves last in case any of the earlier events triggered
3955 static int qemu_cpu_exec(CPUState
*env
)
3958 #ifdef CONFIG_PROFILER
3962 #ifdef CONFIG_PROFILER
3963 ti
= profile_getclock();
3968 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3969 env
->icount_decr
.u16
.low
= 0;
3970 env
->icount_extra
= 0;
3971 count
= qemu_next_deadline();
3972 count
= (count
+ (1 << icount_time_shift
) - 1)
3973 >> icount_time_shift
;
3974 qemu_icount
+= count
;
3975 decr
= (count
> 0xffff) ? 0xffff : count
;
3977 env
->icount_decr
.u16
.low
= decr
;
3978 env
->icount_extra
= count
;
3980 ret
= cpu_exec(env
);
3981 #ifdef CONFIG_PROFILER
3982 qemu_time
+= profile_getclock() - ti
;
3985 /* Fold pending instructions back into the
3986 instruction counter, and clear the interrupt flag. */
3987 qemu_icount
-= (env
->icount_decr
.u16
.low
3988 + env
->icount_extra
);
3989 env
->icount_decr
.u32
= 0;
3990 env
->icount_extra
= 0;
3995 static void tcg_cpu_exec(void)
3999 if (next_cpu
== NULL
)
4000 next_cpu
= first_cpu
;
4001 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4002 CPUState
*env
= cur_cpu
= next_cpu
;
4006 if (timer_alarm_pending
) {
4007 timer_alarm_pending
= 0;
4010 if (cpu_can_run(env
))
4011 ret
= qemu_cpu_exec(env
);
4012 if (ret
== EXCP_DEBUG
) {
4013 gdb_set_stop_cpu(env
);
4014 debug_requested
= 1;
4020 static int cpu_has_work(CPUState
*env
)
4028 if (qemu_cpu_has_work(env
))
4033 static int tcg_has_work(void)
4037 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4038 if (cpu_has_work(env
))
4043 static int qemu_calculate_timeout(void)
4045 #ifndef CONFIG_IOTHREAD
4050 else if (tcg_has_work())
4052 else if (!use_icount
)
4055 /* XXX: use timeout computed from timers */
4058 /* Advance virtual time to the next event. */
4059 if (use_icount
== 1) {
4060 /* When not using an adaptive execution frequency
4061 we tend to get badly out of sync with real time,
4062 so just delay for a reasonable amount of time. */
4065 delta
= cpu_get_icount() - cpu_get_clock();
4068 /* If virtual time is ahead of real time then just
4070 timeout
= (delta
/ 1000000) + 1;
4072 /* Wait for either IO to occur or the next
4074 add
= qemu_next_deadline();
4075 /* We advance the timer before checking for IO.
4076 Limit the amount we advance so that early IO
4077 activity won't get the guest too far ahead. */
4081 add
= (add
+ (1 << icount_time_shift
) - 1)
4082 >> icount_time_shift
;
4084 timeout
= delta
/ 1000000;
4091 #else /* CONFIG_IOTHREAD */
4096 static int vm_can_run(void)
4098 if (powerdown_requested
)
4100 if (reset_requested
)
4102 if (shutdown_requested
)
4104 if (debug_requested
)
4109 qemu_irq qemu_system_powerdown
;
4111 static void main_loop(void)
4115 #ifdef CONFIG_IOTHREAD
4116 qemu_system_ready
= 1;
4117 qemu_cond_broadcast(&qemu_system_cond
);
4122 #ifdef CONFIG_PROFILER
4125 #ifndef CONFIG_IOTHREAD
4128 #ifdef CONFIG_PROFILER
4129 ti
= profile_getclock();
4131 main_loop_wait(qemu_calculate_timeout());
4132 #ifdef CONFIG_PROFILER
4133 dev_time
+= profile_getclock() - ti
;
4135 } while (vm_can_run());
4137 if (qemu_debug_requested()) {
4138 monitor_protocol_event(QEVENT_DEBUG
, NULL
);
4139 vm_stop(EXCP_DEBUG
);
4141 if (qemu_shutdown_requested()) {
4142 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
4149 if (qemu_reset_requested()) {
4150 monitor_protocol_event(QEVENT_RESET
, NULL
);
4152 qemu_system_reset();
4155 if (qemu_powerdown_requested()) {
4156 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
4157 qemu_irq_raise(qemu_system_powerdown
);
4159 if ((r
= qemu_vmstop_requested())) {
4160 monitor_protocol_event(QEVENT_STOP
, NULL
);
4167 static void version(void)
4169 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4172 static void help(int exitcode
)
4175 printf("usage: %s [options] [disk_image]\n"
4177 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4179 #define DEF(option, opt_arg, opt_enum, opt_help) \
4181 #define DEFHEADING(text) stringify(text) "\n"
4182 #include "qemu-options.h"
4187 "During emulation, the following keys are useful:\n"
4188 "ctrl-alt-f toggle full screen\n"
4189 "ctrl-alt-n switch to virtual console 'n'\n"
4190 "ctrl-alt toggle mouse and keyboard grab\n"
4192 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4197 DEFAULT_NETWORK_SCRIPT
,
4198 DEFAULT_NETWORK_DOWN_SCRIPT
,
4200 DEFAULT_GDBSTUB_PORT
,
4205 #define HAS_ARG 0x0001
4208 #define DEF(option, opt_arg, opt_enum, opt_help) \
4210 #define DEFHEADING(text)
4211 #include "qemu-options.h"
4217 typedef struct QEMUOption
{
4223 static const QEMUOption qemu_options
[] = {
4224 { "h", 0, QEMU_OPTION_h
},
4225 #define DEF(option, opt_arg, opt_enum, opt_help) \
4226 { option, opt_arg, opt_enum },
4227 #define DEFHEADING(text)
4228 #include "qemu-options.h"
4236 struct soundhw soundhw
[] = {
4237 #ifdef HAS_AUDIO_CHOICE
4238 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4244 { .init_isa
= pcspk_audio_init
}
4251 "Creative Sound Blaster 16",
4254 { .init_isa
= SB16_init
}
4258 #ifdef CONFIG_CS4231A
4264 { .init_isa
= cs4231a_init
}
4272 "Yamaha YMF262 (OPL3)",
4274 "Yamaha YM3812 (OPL2)",
4278 { .init_isa
= Adlib_init
}
4285 "Gravis Ultrasound GF1",
4288 { .init_isa
= GUS_init
}
4295 "Intel 82801AA AC97 Audio",
4298 { .init_pci
= ac97_init
}
4302 #ifdef CONFIG_ES1370
4305 "ENSONIQ AudioPCI ES1370",
4308 { .init_pci
= es1370_init
}
4312 #endif /* HAS_AUDIO_CHOICE */
4314 { NULL
, NULL
, 0, 0, { NULL
} }
4317 static void select_soundhw (const char *optarg
)
4321 if (*optarg
== '?') {
4324 printf ("Valid sound card names (comma separated):\n");
4325 for (c
= soundhw
; c
->name
; ++c
) {
4326 printf ("%-11s %s\n", c
->name
, c
->descr
);
4328 printf ("\n-soundhw all will enable all of the above\n");
4329 exit (*optarg
!= '?');
4337 if (!strcmp (optarg
, "all")) {
4338 for (c
= soundhw
; c
->name
; ++c
) {
4346 e
= strchr (p
, ',');
4347 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4349 for (c
= soundhw
; c
->name
; ++c
) {
4350 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4359 "Unknown sound card name (too big to show)\n");
4362 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4367 p
+= l
+ (e
!= NULL
);
4371 goto show_valid_cards
;
4376 static void select_vgahw (const char *p
)
4381 vga_interface_type
= VGA_NONE
;
4382 if (strstart(p
, "std", &opts
)) {
4383 vga_interface_type
= VGA_STD
;
4384 } else if (strstart(p
, "cirrus", &opts
)) {
4385 vga_interface_type
= VGA_CIRRUS
;
4386 } else if (strstart(p
, "vmware", &opts
)) {
4387 vga_interface_type
= VGA_VMWARE
;
4388 } else if (strstart(p
, "xenfb", &opts
)) {
4389 vga_interface_type
= VGA_XENFB
;
4390 } else if (!strstart(p
, "none", &opts
)) {
4392 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4396 const char *nextopt
;
4398 if (strstart(opts
, ",retrace=", &nextopt
)) {
4400 if (strstart(opts
, "dumb", &nextopt
))
4401 vga_retrace_method
= VGA_RETRACE_DUMB
;
4402 else if (strstart(opts
, "precise", &nextopt
))
4403 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4404 else goto invalid_vga
;
4405 } else goto invalid_vga
;
4411 static int balloon_parse(const char *arg
)
4415 if (strcmp(arg
, "none") == 0) {
4419 if (!strncmp(arg
, "virtio", 6)) {
4420 if (arg
[6] == ',') {
4421 /* have params -> parse them */
4422 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4426 /* create empty opts */
4427 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4429 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4438 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4440 exit(STATUS_CONTROL_C_EXIT
);
4445 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4449 if(strlen(str
) != 36)
4452 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4453 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4454 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4460 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4468 static void termsig_handler(int signal
)
4470 qemu_system_shutdown_request();
4473 static void sigchld_handler(int signal
)
4475 waitpid(-1, NULL
, WNOHANG
);
4478 static void sighandler_setup(void)
4480 struct sigaction act
;
4482 memset(&act
, 0, sizeof(act
));
4483 act
.sa_handler
= termsig_handler
;
4484 sigaction(SIGINT
, &act
, NULL
);
4485 sigaction(SIGHUP
, &act
, NULL
);
4486 sigaction(SIGTERM
, &act
, NULL
);
4488 act
.sa_handler
= sigchld_handler
;
4489 act
.sa_flags
= SA_NOCLDSTOP
;
4490 sigaction(SIGCHLD
, &act
, NULL
);
4496 /* Look for support files in the same directory as the executable. */
4497 static char *find_datadir(const char *argv0
)
4503 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4510 while (p
!= buf
&& *p
!= '\\')
4513 if (access(buf
, R_OK
) == 0) {
4514 return qemu_strdup(buf
);
4520 /* Find a likely location for support files using the location of the binary.
4521 For installed binaries this will be "$bindir/../share/qemu". When
4522 running from the build tree this will be "$bindir/../pc-bios". */
4523 #define SHARE_SUFFIX "/share/qemu"
4524 #define BUILD_SUFFIX "/pc-bios"
4525 static char *find_datadir(const char *argv0
)
4533 #if defined(__linux__)
4536 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4542 #elif defined(__FreeBSD__)
4545 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4552 /* If we don't have any way of figuring out the actual executable
4553 location then try argv[0]. */
4555 p
= realpath(argv0
, buf
);
4563 max_len
= strlen(dir
) +
4564 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4565 res
= qemu_mallocz(max_len
);
4566 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4567 if (access(res
, R_OK
)) {
4568 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4569 if (access(res
, R_OK
)) {
4581 char *qemu_find_file(int type
, const char *name
)
4587 /* If name contains path separators then try it as a straight path. */
4588 if ((strchr(name
, '/') || strchr(name
, '\\'))
4589 && access(name
, R_OK
) == 0) {
4590 return qemu_strdup(name
);
4593 case QEMU_FILE_TYPE_BIOS
:
4596 case QEMU_FILE_TYPE_KEYMAP
:
4597 subdir
= "keymaps/";
4602 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4603 buf
= qemu_mallocz(len
);
4604 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4605 if (access(buf
, R_OK
)) {
4612 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4616 dev
= qdev_device_add(opts
);
4622 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
4624 CharDriverState
*chr
;
4626 chr
= qemu_chr_open_opts(opts
, NULL
);
4632 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
4634 CharDriverState
*chr
;
4635 const char *chardev
;
4639 mode
= qemu_opt_get(opts
, "mode");
4643 if (strcmp(mode
, "readline") == 0) {
4644 flags
= MONITOR_USE_READLINE
;
4645 } else if (strcmp(mode
, "control") == 0) {
4646 flags
= MONITOR_USE_CONTROL
;
4648 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
4652 if (qemu_opt_get_bool(opts
, "default", 0))
4653 flags
|= MONITOR_IS_DEFAULT
;
4655 chardev
= qemu_opt_get(opts
, "chardev");
4656 chr
= qemu_chr_find(chardev
);
4658 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
4662 monitor_init(chr
, flags
);
4666 static void monitor_parse(const char *optarg
, const char *mode
)
4668 static int monitor_device_index
= 0;
4674 if (strstart(optarg
, "chardev:", &p
)) {
4675 snprintf(label
, sizeof(label
), "%s", p
);
4677 if (monitor_device_index
) {
4678 snprintf(label
, sizeof(label
), "monitor%d",
4679 monitor_device_index
);
4681 snprintf(label
, sizeof(label
), "monitor");
4684 opts
= qemu_chr_parse_compat(label
, optarg
);
4686 fprintf(stderr
, "parse error: %s\n", optarg
);
4691 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
4693 fprintf(stderr
, "duplicate chardev: %s\n", label
);
4696 qemu_opt_set(opts
, "mode", mode
);
4697 qemu_opt_set(opts
, "chardev", label
);
4699 qemu_opt_set(opts
, "default", "on");
4700 monitor_device_index
++;
4703 struct device_config
{
4705 DEV_USB
, /* -usbdevice */
4707 DEV_SERIAL
, /* -serial */
4708 DEV_PARALLEL
, /* -parallel */
4709 DEV_VIRTCON
, /* -virtioconsole */
4711 const char *cmdline
;
4712 QTAILQ_ENTRY(device_config
) next
;
4714 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4716 static void add_device_config(int type
, const char *cmdline
)
4718 struct device_config
*conf
;
4720 conf
= qemu_mallocz(sizeof(*conf
));
4722 conf
->cmdline
= cmdline
;
4723 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4726 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4728 struct device_config
*conf
;
4731 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4732 if (conf
->type
!= type
)
4734 rc
= func(conf
->cmdline
);
4741 static int serial_parse(const char *devname
)
4743 static int index
= 0;
4746 if (strcmp(devname
, "none") == 0)
4748 if (index
== MAX_SERIAL_PORTS
) {
4749 fprintf(stderr
, "qemu: too many serial ports\n");
4752 snprintf(label
, sizeof(label
), "serial%d", index
);
4753 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4754 if (!serial_hds
[index
]) {
4755 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
4756 devname
, strerror(errno
));
4763 static int parallel_parse(const char *devname
)
4765 static int index
= 0;
4768 if (strcmp(devname
, "none") == 0)
4770 if (index
== MAX_PARALLEL_PORTS
) {
4771 fprintf(stderr
, "qemu: too many parallel ports\n");
4774 snprintf(label
, sizeof(label
), "parallel%d", index
);
4775 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4776 if (!parallel_hds
[index
]) {
4777 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
4778 devname
, strerror(errno
));
4785 static int virtcon_parse(const char *devname
)
4787 static int index
= 0;
4790 if (strcmp(devname
, "none") == 0)
4792 if (index
== MAX_VIRTIO_CONSOLES
) {
4793 fprintf(stderr
, "qemu: too many virtio consoles\n");
4796 snprintf(label
, sizeof(label
), "virtcon%d", index
);
4797 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
4798 if (!virtcon_hds
[index
]) {
4799 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
4800 devname
, strerror(errno
));
4807 int main(int argc
, char **argv
, char **envp
)
4809 const char *gdbstub_dev
= NULL
;
4810 uint32_t boot_devices_bitmap
= 0;
4812 int snapshot
, linux_boot
, net_boot
;
4813 const char *initrd_filename
;
4814 const char *kernel_filename
, *kernel_cmdline
;
4815 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4817 DisplayChangeListener
*dcl
;
4818 int cyls
, heads
, secs
, translation
;
4819 QemuOpts
*hda_opts
= NULL
, *opts
;
4821 const char *r
, *optarg
;
4822 const char *loadvm
= NULL
;
4823 QEMUMachine
*machine
;
4824 const char *cpu_model
;
4829 const char *pid_file
= NULL
;
4830 const char *incoming
= NULL
;
4833 struct passwd
*pwd
= NULL
;
4834 const char *chroot_dir
= NULL
;
4835 const char *run_as
= NULL
;
4838 int show_vnc_port
= 0;
4842 qemu_errors_to_file(stderr
);
4843 qemu_cache_utils_init(envp
);
4845 QLIST_INIT (&vm_change_state_head
);
4848 struct sigaction act
;
4849 sigfillset(&act
.sa_mask
);
4851 act
.sa_handler
= SIG_IGN
;
4852 sigaction(SIGPIPE
, &act
, NULL
);
4855 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4856 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4857 QEMU to run on a single CPU */
4862 h
= GetCurrentProcess();
4863 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4864 for(i
= 0; i
< 32; i
++) {
4865 if (mask
& (1 << i
))
4870 SetProcessAffinityMask(h
, mask
);
4876 module_call_init(MODULE_INIT_MACHINE
);
4877 machine
= find_default_machine();
4879 initrd_filename
= NULL
;
4882 kernel_filename
= NULL
;
4883 kernel_cmdline
= "";
4884 cyls
= heads
= secs
= 0;
4885 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4887 for (i
= 0; i
< MAX_NODES
; i
++) {
4889 node_cpumask
[i
] = 0;
4904 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4906 const QEMUOption
*popt
;
4909 /* Treat --foo the same as -foo. */
4912 popt
= qemu_options
;
4915 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4919 if (!strcmp(popt
->name
, r
+ 1))
4923 if (popt
->flags
& HAS_ARG
) {
4924 if (optind
>= argc
) {
4925 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4929 optarg
= argv
[optind
++];
4934 switch(popt
->index
) {
4936 machine
= find_machine(optarg
);
4939 printf("Supported machines are:\n");
4940 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4942 printf("%-10s %s (alias of %s)\n",
4943 m
->alias
, m
->desc
, m
->name
);
4944 printf("%-10s %s%s\n",
4946 m
->is_default
? " (default)" : "");
4948 exit(*optarg
!= '?');
4951 case QEMU_OPTION_cpu
:
4952 /* hw initialization will check this */
4953 if (*optarg
== '?') {
4954 /* XXX: implement xxx_cpu_list for targets that still miss it */
4955 #if defined(cpu_list)
4956 cpu_list(stdout
, &fprintf
);
4963 case QEMU_OPTION_initrd
:
4964 initrd_filename
= optarg
;
4966 case QEMU_OPTION_hda
:
4968 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4970 hda_opts
= drive_add(optarg
, HD_ALIAS
4971 ",cyls=%d,heads=%d,secs=%d%s",
4972 0, cyls
, heads
, secs
,
4973 translation
== BIOS_ATA_TRANSLATION_LBA
?
4975 translation
== BIOS_ATA_TRANSLATION_NONE
?
4976 ",trans=none" : "");
4978 case QEMU_OPTION_hdb
:
4979 case QEMU_OPTION_hdc
:
4980 case QEMU_OPTION_hdd
:
4981 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4983 case QEMU_OPTION_drive
:
4984 drive_add(NULL
, "%s", optarg
);
4986 case QEMU_OPTION_set
:
4987 if (qemu_set_option(optarg
) != 0)
4990 case QEMU_OPTION_global
:
4991 if (qemu_global_option(optarg
) != 0)
4994 case QEMU_OPTION_mtdblock
:
4995 drive_add(optarg
, MTD_ALIAS
);
4997 case QEMU_OPTION_sd
:
4998 drive_add(optarg
, SD_ALIAS
);
5000 case QEMU_OPTION_pflash
:
5001 drive_add(optarg
, PFLASH_ALIAS
);
5003 case QEMU_OPTION_snapshot
:
5006 case QEMU_OPTION_hdachs
:
5010 cyls
= strtol(p
, (char **)&p
, 0);
5011 if (cyls
< 1 || cyls
> 16383)
5016 heads
= strtol(p
, (char **)&p
, 0);
5017 if (heads
< 1 || heads
> 16)
5022 secs
= strtol(p
, (char **)&p
, 0);
5023 if (secs
< 1 || secs
> 63)
5027 if (!strcmp(p
, "none"))
5028 translation
= BIOS_ATA_TRANSLATION_NONE
;
5029 else if (!strcmp(p
, "lba"))
5030 translation
= BIOS_ATA_TRANSLATION_LBA
;
5031 else if (!strcmp(p
, "auto"))
5032 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5035 } else if (*p
!= '\0') {
5037 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5040 if (hda_opts
!= NULL
) {
5042 snprintf(num
, sizeof(num
), "%d", cyls
);
5043 qemu_opt_set(hda_opts
, "cyls", num
);
5044 snprintf(num
, sizeof(num
), "%d", heads
);
5045 qemu_opt_set(hda_opts
, "heads", num
);
5046 snprintf(num
, sizeof(num
), "%d", secs
);
5047 qemu_opt_set(hda_opts
, "secs", num
);
5048 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5049 qemu_opt_set(hda_opts
, "trans", "lba");
5050 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5051 qemu_opt_set(hda_opts
, "trans", "none");
5055 case QEMU_OPTION_numa
:
5056 if (nb_numa_nodes
>= MAX_NODES
) {
5057 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5062 case QEMU_OPTION_nographic
:
5063 display_type
= DT_NOGRAPHIC
;
5065 #ifdef CONFIG_CURSES
5066 case QEMU_OPTION_curses
:
5067 display_type
= DT_CURSES
;
5070 case QEMU_OPTION_portrait
:
5073 case QEMU_OPTION_kernel
:
5074 kernel_filename
= optarg
;
5076 case QEMU_OPTION_append
:
5077 kernel_cmdline
= optarg
;
5079 case QEMU_OPTION_cdrom
:
5080 drive_add(optarg
, CDROM_ALIAS
);
5082 case QEMU_OPTION_boot
:
5084 static const char * const params
[] = {
5085 "order", "once", "menu", NULL
5087 char buf
[sizeof(boot_devices
)];
5088 char *standard_boot_devices
;
5091 if (!strchr(optarg
, '=')) {
5093 pstrcpy(buf
, sizeof(buf
), optarg
);
5094 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5096 "qemu: unknown boot parameter '%s' in '%s'\n",
5102 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5103 boot_devices_bitmap
= parse_bootdevices(buf
);
5104 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5107 if (get_param_value(buf
, sizeof(buf
),
5109 boot_devices_bitmap
|= parse_bootdevices(buf
);
5110 standard_boot_devices
= qemu_strdup(boot_devices
);
5111 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5112 qemu_register_reset(restore_boot_devices
,
5113 standard_boot_devices
);
5115 if (get_param_value(buf
, sizeof(buf
),
5117 if (!strcmp(buf
, "on")) {
5119 } else if (!strcmp(buf
, "off")) {
5123 "qemu: invalid option value '%s'\n",
5131 case QEMU_OPTION_fda
:
5132 case QEMU_OPTION_fdb
:
5133 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5136 case QEMU_OPTION_no_fd_bootchk
:
5140 case QEMU_OPTION_netdev
:
5141 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
5145 case QEMU_OPTION_net
:
5146 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
5151 case QEMU_OPTION_tftp
:
5152 legacy_tftp_prefix
= optarg
;
5154 case QEMU_OPTION_bootp
:
5155 legacy_bootp_filename
= optarg
;
5158 case QEMU_OPTION_smb
:
5159 if (net_slirp_smb(optarg
) < 0)
5163 case QEMU_OPTION_redir
:
5164 if (net_slirp_redir(optarg
) < 0)
5168 case QEMU_OPTION_bt
:
5169 add_device_config(DEV_BT
, optarg
);
5172 case QEMU_OPTION_audio_help
:
5176 case QEMU_OPTION_soundhw
:
5177 select_soundhw (optarg
);
5183 case QEMU_OPTION_version
:
5187 case QEMU_OPTION_m
: {
5191 value
= strtoul(optarg
, &ptr
, 10);
5193 case 0: case 'M': case 'm':
5200 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5204 /* On 32-bit hosts, QEMU is limited by virtual address space */
5205 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5206 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5209 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5210 fprintf(stderr
, "qemu: ram size too large\n");
5219 const CPULogItem
*item
;
5221 mask
= cpu_str_to_log_mask(optarg
);
5223 printf("Log items (comma separated):\n");
5224 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5225 printf("%-10s %s\n", item
->name
, item
->help
);
5233 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5235 case QEMU_OPTION_gdb
:
5236 gdbstub_dev
= optarg
;
5241 case QEMU_OPTION_bios
:
5244 case QEMU_OPTION_singlestep
:
5251 keyboard_layout
= optarg
;
5253 case QEMU_OPTION_localtime
:
5256 case QEMU_OPTION_vga
:
5257 select_vgahw (optarg
);
5259 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5265 w
= strtol(p
, (char **)&p
, 10);
5268 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5274 h
= strtol(p
, (char **)&p
, 10);
5279 depth
= strtol(p
, (char **)&p
, 10);
5280 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5281 depth
!= 24 && depth
!= 32)
5283 } else if (*p
== '\0') {
5284 depth
= graphic_depth
;
5291 graphic_depth
= depth
;
5295 case QEMU_OPTION_echr
:
5298 term_escape_char
= strtol(optarg
, &r
, 0);
5300 printf("Bad argument to echr\n");
5303 case QEMU_OPTION_monitor
:
5304 monitor_parse(optarg
, "readline");
5305 default_monitor
= 0;
5307 case QEMU_OPTION_qmp
:
5308 monitor_parse(optarg
, "control");
5309 default_monitor
= 0;
5311 case QEMU_OPTION_mon
:
5312 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, "chardev");
5314 fprintf(stderr
, "parse error: %s\n", optarg
);
5317 default_monitor
= 0;
5319 case QEMU_OPTION_chardev
:
5320 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5322 fprintf(stderr
, "parse error: %s\n", optarg
);
5326 case QEMU_OPTION_serial
:
5327 add_device_config(DEV_SERIAL
, optarg
);
5330 case QEMU_OPTION_watchdog
:
5333 "qemu: only one watchdog option may be given\n");
5338 case QEMU_OPTION_watchdog_action
:
5339 if (select_watchdog_action(optarg
) == -1) {
5340 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5344 case QEMU_OPTION_virtiocon
:
5345 add_device_config(DEV_VIRTCON
, optarg
);
5346 default_virtcon
= 0;
5348 case QEMU_OPTION_parallel
:
5349 add_device_config(DEV_PARALLEL
, optarg
);
5350 default_parallel
= 0;
5352 case QEMU_OPTION_loadvm
:
5355 case QEMU_OPTION_full_screen
:
5359 case QEMU_OPTION_no_frame
:
5362 case QEMU_OPTION_alt_grab
:
5365 case QEMU_OPTION_ctrl_grab
:
5368 case QEMU_OPTION_no_quit
:
5371 case QEMU_OPTION_sdl
:
5372 display_type
= DT_SDL
;
5375 case QEMU_OPTION_pidfile
:
5379 case QEMU_OPTION_win2k_hack
:
5380 win2k_install_hack
= 1;
5382 case QEMU_OPTION_rtc_td_hack
:
5385 case QEMU_OPTION_acpitable
:
5386 if(acpi_table_add(optarg
) < 0) {
5387 fprintf(stderr
, "Wrong acpi table provided\n");
5391 case QEMU_OPTION_smbios
:
5392 if(smbios_entry_add(optarg
) < 0) {
5393 fprintf(stderr
, "Wrong smbios provided\n");
5399 case QEMU_OPTION_enable_kvm
:
5403 case QEMU_OPTION_usb
:
5406 case QEMU_OPTION_usbdevice
:
5408 add_device_config(DEV_USB
, optarg
);
5410 case QEMU_OPTION_device
:
5411 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5415 case QEMU_OPTION_smp
:
5418 fprintf(stderr
, "Invalid number of CPUs\n");
5421 if (max_cpus
< smp_cpus
) {
5422 fprintf(stderr
, "maxcpus must be equal to or greater than "
5426 if (max_cpus
> 255) {
5427 fprintf(stderr
, "Unsupported number of maxcpus\n");
5431 case QEMU_OPTION_vnc
:
5432 display_type
= DT_VNC
;
5433 vnc_display
= optarg
;
5436 case QEMU_OPTION_no_acpi
:
5439 case QEMU_OPTION_no_hpet
:
5442 case QEMU_OPTION_balloon
:
5443 if (balloon_parse(optarg
) < 0) {
5444 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5449 case QEMU_OPTION_no_reboot
:
5452 case QEMU_OPTION_no_shutdown
:
5455 case QEMU_OPTION_show_cursor
:
5458 case QEMU_OPTION_uuid
:
5459 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5460 fprintf(stderr
, "Fail to parse UUID string."
5461 " Wrong format.\n");
5466 case QEMU_OPTION_daemonize
:
5470 case QEMU_OPTION_option_rom
:
5471 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5472 fprintf(stderr
, "Too many option ROMs\n");
5475 option_rom
[nb_option_roms
] = optarg
;
5478 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5479 case QEMU_OPTION_semihosting
:
5480 semihosting_enabled
= 1;
5483 case QEMU_OPTION_name
:
5484 qemu_name
= qemu_strdup(optarg
);
5486 char *p
= strchr(qemu_name
, ',');
5489 if (strncmp(p
, "process=", 8)) {
5490 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5498 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5499 case QEMU_OPTION_prom_env
:
5500 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5501 fprintf(stderr
, "Too many prom variables\n");
5504 prom_envs
[nb_prom_envs
] = optarg
;
5509 case QEMU_OPTION_old_param
:
5513 case QEMU_OPTION_clock
:
5514 configure_alarms(optarg
);
5516 case QEMU_OPTION_startdate
:
5517 configure_rtc_date_offset(optarg
, 1);
5519 case QEMU_OPTION_rtc
:
5520 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5522 fprintf(stderr
, "parse error: %s\n", optarg
);
5525 configure_rtc(opts
);
5527 case QEMU_OPTION_tb_size
:
5528 tb_size
= strtol(optarg
, NULL
, 0);
5532 case QEMU_OPTION_icount
:
5534 if (strcmp(optarg
, "auto") == 0) {
5535 icount_time_shift
= -1;
5537 icount_time_shift
= strtol(optarg
, NULL
, 0);
5540 case QEMU_OPTION_incoming
:
5543 case QEMU_OPTION_nodefaults
:
5545 default_parallel
= 0;
5546 default_virtcon
= 0;
5547 default_monitor
= 0;
5553 case QEMU_OPTION_chroot
:
5554 chroot_dir
= optarg
;
5556 case QEMU_OPTION_runas
:
5561 case QEMU_OPTION_xen_domid
:
5562 xen_domid
= atoi(optarg
);
5564 case QEMU_OPTION_xen_create
:
5565 xen_mode
= XEN_CREATE
;
5567 case QEMU_OPTION_xen_attach
:
5568 xen_mode
= XEN_ATTACH
;
5571 case QEMU_OPTION_readconfig
:
5574 fp
= fopen(optarg
, "r");
5576 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5579 if (qemu_config_parse(fp
) != 0) {
5585 case QEMU_OPTION_writeconfig
:
5588 if (strcmp(optarg
, "-") == 0) {
5591 fp
= fopen(optarg
, "w");
5593 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5597 qemu_config_write(fp
);
5605 /* If no data_dir is specified then try to find it relative to the
5608 data_dir
= find_datadir(argv
[0]);
5610 /* If all else fails use the install patch specified when building. */
5612 data_dir
= CONFIG_QEMU_SHAREDIR
;
5616 * Default to max_cpus = smp_cpus, in case the user doesn't
5617 * specify a max_cpus value.
5620 max_cpus
= smp_cpus
;
5622 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5623 if (smp_cpus
> machine
->max_cpus
) {
5624 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5625 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5630 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
5632 if (machine
->no_serial
) {
5635 if (machine
->no_parallel
) {
5636 default_parallel
= 0;
5638 if (!machine
->use_virtcon
) {
5639 default_virtcon
= 0;
5641 if (machine
->no_vga
) {
5645 if (display_type
== DT_NOGRAPHIC
) {
5646 if (default_parallel
)
5647 add_device_config(DEV_PARALLEL
, "null");
5648 if (default_serial
&& default_monitor
) {
5649 add_device_config(DEV_SERIAL
, "mon:stdio");
5650 } else if (default_virtcon
&& default_monitor
) {
5651 add_device_config(DEV_VIRTCON
, "mon:stdio");
5654 add_device_config(DEV_SERIAL
, "stdio");
5655 if (default_virtcon
)
5656 add_device_config(DEV_VIRTCON
, "stdio");
5657 if (default_monitor
)
5658 monitor_parse("stdio", "readline");
5662 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
5663 if (default_parallel
)
5664 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
5665 if (default_monitor
)
5666 monitor_parse("vc:80Cx24C", "readline");
5669 vga_interface_type
= VGA_CIRRUS
;
5671 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
5678 if (pipe(fds
) == -1)
5689 len
= read(fds
[0], &status
, 1);
5690 if (len
== -1 && (errno
== EINTR
))
5695 else if (status
== 1) {
5696 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5704 qemu_set_cloexec(fds
[1]);
5716 signal(SIGTSTP
, SIG_IGN
);
5717 signal(SIGTTOU
, SIG_IGN
);
5718 signal(SIGTTIN
, SIG_IGN
);
5721 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5724 write(fds
[1], &status
, 1);
5726 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5731 if (kvm_enabled()) {
5734 ret
= kvm_init(smp_cpus
);
5736 fprintf(stderr
, "failed to initialize KVM\n");
5741 if (qemu_init_main_loop()) {
5742 fprintf(stderr
, "qemu_init_main_loop failed\n");
5745 linux_boot
= (kernel_filename
!= NULL
);
5747 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5748 fprintf(stderr
, "-append only allowed with -kernel option\n");
5752 if (!linux_boot
&& initrd_filename
!= NULL
) {
5753 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5758 /* Win32 doesn't support line-buffering and requires size >= 2 */
5759 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5762 if (init_timer_alarm() < 0) {
5763 fprintf(stderr
, "could not initialize alarm timer\n");
5766 if (use_icount
&& icount_time_shift
< 0) {
5768 /* 125MIPS seems a reasonable initial guess at the guest speed.
5769 It will be corrected fairly quickly anyway. */
5770 icount_time_shift
= 3;
5771 init_icount_adjust();
5778 if (net_init_clients() < 0) {
5782 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5783 net_set_boot_mask(net_boot
);
5785 /* init the bluetooth world */
5786 if (foreach_device_config(DEV_BT
, bt_parse
))
5789 /* init the memory */
5791 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5793 /* init the dynamic translator */
5794 cpu_exec_init_all(tb_size
* 1024 * 1024);
5796 bdrv_init_with_whitelist();
5800 if (default_drive
) {
5801 /* we always create the cdrom drive, even if no disk is there */
5802 drive_add(NULL
, CDROM_ALIAS
);
5804 /* we always create at least one floppy */
5805 drive_add(NULL
, FD_ALIAS
, 0);
5807 /* we always create one sd slot, even if no card is in it */
5808 drive_add(NULL
, SD_ALIAS
);
5811 /* open the virtual block devices */
5813 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5814 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5817 vmstate_register(0, &vmstate_timers
,&timers_state
);
5818 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5821 if (nb_numa_nodes
> 0) {
5824 if (nb_numa_nodes
> smp_cpus
) {
5825 nb_numa_nodes
= smp_cpus
;
5828 /* If no memory size if given for any node, assume the default case
5829 * and distribute the available memory equally across all nodes
5831 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5832 if (node_mem
[i
] != 0)
5835 if (i
== nb_numa_nodes
) {
5836 uint64_t usedmem
= 0;
5838 /* On Linux, the each node's border has to be 8MB aligned,
5839 * the final node gets the rest.
5841 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5842 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5843 usedmem
+= node_mem
[i
];
5845 node_mem
[i
] = ram_size
- usedmem
;
5848 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5849 if (node_cpumask
[i
] != 0)
5852 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5853 * must cope with this anyway, because there are BIOSes out there in
5854 * real machines which also use this scheme.
5856 if (i
== nb_numa_nodes
) {
5857 for (i
= 0; i
< smp_cpus
; i
++) {
5858 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5863 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5865 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5867 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5870 module_call_init(MODULE_INIT_DEVICE
);
5873 i
= select_watchdog(watchdog
);
5875 exit (i
== 1 ? 1 : 0);
5878 if (machine
->compat_props
) {
5879 qdev_prop_register_global_list(machine
->compat_props
);
5883 machine
->init(ram_size
, boot_devices
,
5884 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5888 /* must be after terminal init, SDL library changes signal handlers */
5892 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5893 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5894 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5900 current_machine
= machine
;
5902 /* init USB devices */
5904 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5908 /* init generic devices */
5909 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5913 dumb_display_init();
5914 /* just use the first displaystate for the moment */
5917 if (display_type
== DT_DEFAULT
) {
5918 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5919 display_type
= DT_SDL
;
5921 display_type
= DT_VNC
;
5922 vnc_display
= "localhost:0,to=99";
5928 switch (display_type
) {
5931 #if defined(CONFIG_CURSES)
5933 curses_display_init(ds
, full_screen
);
5936 #if defined(CONFIG_SDL)
5938 sdl_display_init(ds
, full_screen
, no_frame
);
5940 #elif defined(CONFIG_COCOA)
5942 cocoa_display_init(ds
, full_screen
);
5946 vnc_display_init(ds
);
5947 if (vnc_display_open(ds
, vnc_display
) < 0)
5950 if (show_vnc_port
) {
5951 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5959 dcl
= ds
->listeners
;
5960 while (dcl
!= NULL
) {
5961 if (dcl
->dpy_refresh
!= NULL
) {
5962 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5963 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5968 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5969 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5970 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5973 text_consoles_set_display(display_state
);
5975 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5978 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5979 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5984 qdev_machine_creation_done();
5988 qemu_system_reset();
5990 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5996 qemu_start_incoming_migration(incoming
);
5997 } else if (autostart
) {
6007 len
= write(fds
[1], &status
, 1);
6008 if (len
== -1 && (errno
== EINTR
))
6015 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
6021 pwd
= getpwnam(run_as
);
6023 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6029 if (chroot(chroot_dir
) < 0) {
6030 fprintf(stderr
, "chroot failed\n");
6037 if (setgid(pwd
->pw_gid
) < 0) {
6038 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6041 if (setuid(pwd
->pw_uid
) < 0) {
6042 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6045 if (setuid(0) != -1) {
6046 fprintf(stderr
, "Dropping privileges failed\n");