root / vl.c @ 9a743e5b
History | View | Annotate | Download (152.5 kB)
1 |
/*
|
---|---|
2 |
* QEMU System Emulator
|
3 |
*
|
4 |
* Copyright (c) 2003-2008 Fabrice Bellard
|
5 |
*
|
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:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
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
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include <unistd.h> |
25 |
#include <fcntl.h> |
26 |
#include <signal.h> |
27 |
#include <time.h> |
28 |
#include <errno.h> |
29 |
#include <sys/time.h> |
30 |
#include <zlib.h> |
31 |
|
32 |
/* Needed early for CONFIG_BSD etc. */
|
33 |
#include "config-host.h" |
34 |
|
35 |
#ifndef _WIN32
|
36 |
#include <libgen.h> |
37 |
#include <pwd.h> |
38 |
#include <sys/times.h> |
39 |
#include <sys/wait.h> |
40 |
#include <termios.h> |
41 |
#include <sys/mman.h> |
42 |
#include <sys/ioctl.h> |
43 |
#include <sys/resource.h> |
44 |
#include <sys/socket.h> |
45 |
#include <netinet/in.h> |
46 |
#include <net/if.h> |
47 |
#include <arpa/inet.h> |
48 |
#include <dirent.h> |
49 |
#include <netdb.h> |
50 |
#include <sys/select.h> |
51 |
#ifdef CONFIG_BSD
|
52 |
#include <sys/stat.h> |
53 |
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
54 |
#include <libutil.h> |
55 |
#else
|
56 |
#include <util.h> |
57 |
#endif
|
58 |
#else
|
59 |
#ifdef __linux__
|
60 |
#include <pty.h> |
61 |
#include <malloc.h> |
62 |
#include <linux/rtc.h> |
63 |
#include <sys/prctl.h> |
64 |
|
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> */
|
68 |
#include "hpet.h" |
69 |
|
70 |
#include <linux/ppdev.h> |
71 |
#include <linux/parport.h> |
72 |
#endif
|
73 |
#ifdef __sun__
|
74 |
#include <sys/stat.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> |
84 |
#include <net/if.h> |
85 |
#include <syslog.h> |
86 |
#include <stropts.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); |
90 |
#endif
|
91 |
#endif
|
92 |
#endif
|
93 |
|
94 |
#if defined(__OpenBSD__)
|
95 |
#include <util.h> |
96 |
#endif
|
97 |
|
98 |
#if defined(CONFIG_VDE)
|
99 |
#include <libvdeplug.h> |
100 |
#endif
|
101 |
|
102 |
#ifdef _WIN32
|
103 |
#include <windows.h> |
104 |
#include <mmsystem.h> |
105 |
#endif
|
106 |
|
107 |
#ifdef CONFIG_SDL
|
108 |
#if defined(__APPLE__) || defined(main)
|
109 |
#include <SDL.h> |
110 |
int qemu_main(int argc, char **argv, char **envp); |
111 |
int main(int argc, char **argv) |
112 |
{ |
113 |
return qemu_main(argc, argv, NULL); |
114 |
} |
115 |
#undef main
|
116 |
#define main qemu_main
|
117 |
#endif
|
118 |
#endif /* CONFIG_SDL */ |
119 |
|
120 |
#ifdef CONFIG_COCOA
|
121 |
#undef main
|
122 |
#define main qemu_main
|
123 |
#endif /* CONFIG_COCOA */ |
124 |
|
125 |
#include "hw/hw.h" |
126 |
#include "hw/boards.h" |
127 |
#include "hw/usb.h" |
128 |
#include "hw/pcmcia.h" |
129 |
#include "hw/pc.h" |
130 |
#include "hw/audiodev.h" |
131 |
#include "hw/isa.h" |
132 |
#include "hw/baum.h" |
133 |
#include "hw/bt.h" |
134 |
#include "hw/watchdog.h" |
135 |
#include "hw/smbios.h" |
136 |
#include "hw/xen.h" |
137 |
#include "hw/qdev.h" |
138 |
#include "hw/loader.h" |
139 |
#include "bt-host.h" |
140 |
#include "net.h" |
141 |
#include "net/slirp.h" |
142 |
#include "monitor.h" |
143 |
#include "console.h" |
144 |
#include "sysemu.h" |
145 |
#include "gdbstub.h" |
146 |
#include "qemu-timer.h" |
147 |
#include "qemu-char.h" |
148 |
#include "cache-utils.h" |
149 |
#include "block.h" |
150 |
#include "block_int.h" |
151 |
#include "block-migration.h" |
152 |
#include "dma.h" |
153 |
#include "audio/audio.h" |
154 |
#include "migration.h" |
155 |
#include "kvm.h" |
156 |
#include "balloon.h" |
157 |
#include "qemu-option.h" |
158 |
#include "qemu-config.h" |
159 |
|
160 |
#include "disas.h" |
161 |
|
162 |
#include "exec-all.h" |
163 |
|
164 |
#include "qemu_socket.h" |
165 |
|
166 |
#include "slirp/libslirp.h" |
167 |
|
168 |
#include "qemu-queue.h" |
169 |
|
170 |
//#define DEBUG_NET
|
171 |
//#define DEBUG_SLIRP
|
172 |
|
173 |
#define DEFAULT_RAM_SIZE 128 |
174 |
|
175 |
/* Maximum number of monitor devices */
|
176 |
#define MAX_MONITOR_DEVICES 10 |
177 |
|
178 |
static const char *data_dir; |
179 |
const char *bios_name = NULL; |
180 |
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
181 |
to store the VM snapshots */
|
182 |
struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
|
183 |
struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
|
184 |
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
|
185 |
static DisplayState *display_state;
|
186 |
DisplayType display_type = DT_DEFAULT; |
187 |
const char* keyboard_layout = NULL; |
188 |
ram_addr_t ram_size; |
189 |
int nb_nics;
|
190 |
NICInfo nd_table[MAX_NICS]; |
191 |
int vm_running;
|
192 |
int autostart;
|
193 |
static int rtc_utc = 1; |
194 |
static int rtc_date_offset = -1; /* -1 means no change */ |
195 |
QEMUClock *rtc_clock; |
196 |
int vga_interface_type = VGA_CIRRUS;
|
197 |
#ifdef TARGET_SPARC
|
198 |
int graphic_width = 1024; |
199 |
int graphic_height = 768; |
200 |
int graphic_depth = 8; |
201 |
#else
|
202 |
int graphic_width = 800; |
203 |
int graphic_height = 600; |
204 |
int graphic_depth = 15; |
205 |
#endif
|
206 |
static int full_screen = 0; |
207 |
#ifdef CONFIG_SDL
|
208 |
static int no_frame = 0; |
209 |
#endif
|
210 |
int no_quit = 0; |
211 |
CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
212 |
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
213 |
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
214 |
#ifdef TARGET_I386
|
215 |
int win2k_install_hack = 0; |
216 |
int rtc_td_hack = 0; |
217 |
#endif
|
218 |
int usb_enabled = 0; |
219 |
int singlestep = 0; |
220 |
int smp_cpus = 1; |
221 |
int max_cpus = 0; |
222 |
int smp_cores = 1; |
223 |
int smp_threads = 1; |
224 |
const char *vnc_display; |
225 |
int acpi_enabled = 1; |
226 |
int no_hpet = 0; |
227 |
int fd_bootchk = 1; |
228 |
int no_reboot = 0; |
229 |
int no_shutdown = 0; |
230 |
int cursor_hide = 1; |
231 |
int graphic_rotate = 0; |
232 |
uint8_t irq0override = 1;
|
233 |
#ifndef _WIN32
|
234 |
int daemonize = 0; |
235 |
#endif
|
236 |
const char *watchdog; |
237 |
const char *option_rom[MAX_OPTION_ROMS]; |
238 |
int nb_option_roms;
|
239 |
int semihosting_enabled = 0; |
240 |
#ifdef TARGET_ARM
|
241 |
int old_param = 0; |
242 |
#endif
|
243 |
const char *qemu_name; |
244 |
int alt_grab = 0; |
245 |
int ctrl_grab = 0; |
246 |
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
|
247 |
unsigned int nb_prom_envs = 0; |
248 |
const char *prom_envs[MAX_PROM_ENVS]; |
249 |
#endif
|
250 |
int boot_menu;
|
251 |
|
252 |
int nb_numa_nodes;
|
253 |
uint64_t node_mem[MAX_NODES]; |
254 |
uint64_t node_cpumask[MAX_NODES]; |
255 |
|
256 |
static CPUState *cur_cpu;
|
257 |
static CPUState *next_cpu;
|
258 |
static int timer_alarm_pending = 1; |
259 |
/* Conversion factor from emulated instructions to virtual clock ticks. */
|
260 |
static int icount_time_shift; |
261 |
/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
262 |
#define MAX_ICOUNT_SHIFT 10 |
263 |
/* Compensate for varying guest execution speed. */
|
264 |
static int64_t qemu_icount_bias;
|
265 |
static QEMUTimer *icount_rt_timer;
|
266 |
static QEMUTimer *icount_vm_timer;
|
267 |
static QEMUTimer *nographic_timer;
|
268 |
|
269 |
uint8_t qemu_uuid[16];
|
270 |
|
271 |
static QEMUBootSetHandler *boot_set_handler;
|
272 |
static void *boot_set_opaque; |
273 |
|
274 |
/***********************************************************/
|
275 |
/* x86 ISA bus support */
|
276 |
|
277 |
target_phys_addr_t isa_mem_base = 0;
|
278 |
PicState2 *isa_pic; |
279 |
|
280 |
/***********************************************************/
|
281 |
void hw_error(const char *fmt, ...) |
282 |
{ |
283 |
va_list ap; |
284 |
CPUState *env; |
285 |
|
286 |
va_start(ap, fmt); |
287 |
fprintf(stderr, "qemu: hardware error: ");
|
288 |
vfprintf(stderr, fmt, ap); |
289 |
fprintf(stderr, "\n");
|
290 |
for(env = first_cpu; env != NULL; env = env->next_cpu) { |
291 |
fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
292 |
#ifdef TARGET_I386
|
293 |
cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
294 |
#else
|
295 |
cpu_dump_state(env, stderr, fprintf, 0);
|
296 |
#endif
|
297 |
} |
298 |
va_end(ap); |
299 |
abort(); |
300 |
} |
301 |
|
302 |
static void set_proc_name(const char *s) |
303 |
{ |
304 |
#if defined(__linux__) && defined(PR_SET_NAME)
|
305 |
char name[16]; |
306 |
if (!s)
|
307 |
return;
|
308 |
name[sizeof(name) - 1] = 0; |
309 |
strncpy(name, s, sizeof(name));
|
310 |
/* Could rewrite argv[0] too, but that's a bit more complicated.
|
311 |
This simple way is enough for `top'. */
|
312 |
prctl(PR_SET_NAME, name); |
313 |
#endif
|
314 |
} |
315 |
|
316 |
/***************/
|
317 |
/* ballooning */
|
318 |
|
319 |
static QEMUBalloonEvent *qemu_balloon_event;
|
320 |
void *qemu_balloon_event_opaque;
|
321 |
|
322 |
void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
323 |
{ |
324 |
qemu_balloon_event = func; |
325 |
qemu_balloon_event_opaque = opaque; |
326 |
} |
327 |
|
328 |
void qemu_balloon(ram_addr_t target)
|
329 |
{ |
330 |
if (qemu_balloon_event)
|
331 |
qemu_balloon_event(qemu_balloon_event_opaque, target); |
332 |
} |
333 |
|
334 |
ram_addr_t qemu_balloon_status(void)
|
335 |
{ |
336 |
if (qemu_balloon_event)
|
337 |
return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
338 |
return 0; |
339 |
} |
340 |
|
341 |
/***********************************************************/
|
342 |
/* keyboard/mouse */
|
343 |
|
344 |
static QEMUPutKBDEvent *qemu_put_kbd_event;
|
345 |
static void *qemu_put_kbd_event_opaque; |
346 |
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
347 |
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
348 |
|
349 |
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
350 |
{ |
351 |
qemu_put_kbd_event_opaque = opaque; |
352 |
qemu_put_kbd_event = func; |
353 |
} |
354 |
|
355 |
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
356 |
void *opaque, int absolute, |
357 |
const char *name) |
358 |
{ |
359 |
QEMUPutMouseEntry *s, *cursor; |
360 |
|
361 |
s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
362 |
|
363 |
s->qemu_put_mouse_event = func; |
364 |
s->qemu_put_mouse_event_opaque = opaque; |
365 |
s->qemu_put_mouse_event_absolute = absolute; |
366 |
s->qemu_put_mouse_event_name = qemu_strdup(name); |
367 |
s->next = NULL;
|
368 |
|
369 |
if (!qemu_put_mouse_event_head) {
|
370 |
qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
371 |
return s;
|
372 |
} |
373 |
|
374 |
cursor = qemu_put_mouse_event_head; |
375 |
while (cursor->next != NULL) |
376 |
cursor = cursor->next; |
377 |
|
378 |
cursor->next = s; |
379 |
qemu_put_mouse_event_current = s; |
380 |
|
381 |
return s;
|
382 |
} |
383 |
|
384 |
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
385 |
{ |
386 |
QEMUPutMouseEntry *prev = NULL, *cursor;
|
387 |
|
388 |
if (!qemu_put_mouse_event_head || entry == NULL) |
389 |
return;
|
390 |
|
391 |
cursor = qemu_put_mouse_event_head; |
392 |
while (cursor != NULL && cursor != entry) { |
393 |
prev = cursor; |
394 |
cursor = cursor->next; |
395 |
} |
396 |
|
397 |
if (cursor == NULL) // does not exist or list empty |
398 |
return;
|
399 |
else if (prev == NULL) { // entry is head |
400 |
qemu_put_mouse_event_head = cursor->next; |
401 |
if (qemu_put_mouse_event_current == entry)
|
402 |
qemu_put_mouse_event_current = cursor->next; |
403 |
qemu_free(entry->qemu_put_mouse_event_name); |
404 |
qemu_free(entry); |
405 |
return;
|
406 |
} |
407 |
|
408 |
prev->next = entry->next; |
409 |
|
410 |
if (qemu_put_mouse_event_current == entry)
|
411 |
qemu_put_mouse_event_current = prev; |
412 |
|
413 |
qemu_free(entry->qemu_put_mouse_event_name); |
414 |
qemu_free(entry); |
415 |
} |
416 |
|
417 |
void kbd_put_keycode(int keycode) |
418 |
{ |
419 |
if (qemu_put_kbd_event) {
|
420 |
qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
421 |
} |
422 |
} |
423 |
|
424 |
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
425 |
{ |
426 |
QEMUPutMouseEvent *mouse_event; |
427 |
void *mouse_event_opaque;
|
428 |
int width;
|
429 |
|
430 |
if (!qemu_put_mouse_event_current) {
|
431 |
return;
|
432 |
} |
433 |
|
434 |
mouse_event = |
435 |
qemu_put_mouse_event_current->qemu_put_mouse_event; |
436 |
mouse_event_opaque = |
437 |
qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
438 |
|
439 |
if (mouse_event) {
|
440 |
if (graphic_rotate) {
|
441 |
if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
442 |
width = 0x7fff;
|
443 |
else
|
444 |
width = graphic_width - 1;
|
445 |
mouse_event(mouse_event_opaque, |
446 |
width - dy, dx, dz, buttons_state); |
447 |
} else
|
448 |
mouse_event(mouse_event_opaque, |
449 |
dx, dy, dz, buttons_state); |
450 |
} |
451 |
} |
452 |
|
453 |
int kbd_mouse_is_absolute(void) |
454 |
{ |
455 |
if (!qemu_put_mouse_event_current)
|
456 |
return 0; |
457 |
|
458 |
return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
459 |
} |
460 |
|
461 |
void do_info_mice(Monitor *mon)
|
462 |
{ |
463 |
QEMUPutMouseEntry *cursor; |
464 |
int index = 0; |
465 |
|
466 |
if (!qemu_put_mouse_event_head) {
|
467 |
monitor_printf(mon, "No mouse devices connected\n");
|
468 |
return;
|
469 |
} |
470 |
|
471 |
monitor_printf(mon, "Mouse devices available:\n");
|
472 |
cursor = qemu_put_mouse_event_head; |
473 |
while (cursor != NULL) { |
474 |
monitor_printf(mon, "%c Mouse #%d: %s\n",
|
475 |
(cursor == qemu_put_mouse_event_current ? '*' : ' '), |
476 |
index, cursor->qemu_put_mouse_event_name); |
477 |
index++; |
478 |
cursor = cursor->next; |
479 |
} |
480 |
} |
481 |
|
482 |
void do_mouse_set(Monitor *mon, const QDict *qdict) |
483 |
{ |
484 |
QEMUPutMouseEntry *cursor; |
485 |
int i = 0; |
486 |
int index = qdict_get_int(qdict, "index"); |
487 |
|
488 |
if (!qemu_put_mouse_event_head) {
|
489 |
monitor_printf(mon, "No mouse devices connected\n");
|
490 |
return;
|
491 |
} |
492 |
|
493 |
cursor = qemu_put_mouse_event_head; |
494 |
while (cursor != NULL && index != i) { |
495 |
i++; |
496 |
cursor = cursor->next; |
497 |
} |
498 |
|
499 |
if (cursor != NULL) |
500 |
qemu_put_mouse_event_current = cursor; |
501 |
else
|
502 |
monitor_printf(mon, "Mouse at given index not found\n");
|
503 |
} |
504 |
|
505 |
/* compute with 96 bit intermediate result: (a*b)/c */
|
506 |
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
507 |
{ |
508 |
union {
|
509 |
uint64_t ll; |
510 |
struct {
|
511 |
#ifdef HOST_WORDS_BIGENDIAN
|
512 |
uint32_t high, low; |
513 |
#else
|
514 |
uint32_t low, high; |
515 |
#endif
|
516 |
} l; |
517 |
} u, res; |
518 |
uint64_t rl, rh; |
519 |
|
520 |
u.ll = a; |
521 |
rl = (uint64_t)u.l.low * (uint64_t)b; |
522 |
rh = (uint64_t)u.l.high * (uint64_t)b; |
523 |
rh += (rl >> 32);
|
524 |
res.l.high = rh / c; |
525 |
res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
526 |
return res.ll;
|
527 |
} |
528 |
|
529 |
/***********************************************************/
|
530 |
/* real time host monotonic timer */
|
531 |
|
532 |
static int64_t get_clock_realtime(void) |
533 |
{ |
534 |
struct timeval tv;
|
535 |
|
536 |
gettimeofday(&tv, NULL);
|
537 |
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
538 |
} |
539 |
|
540 |
#ifdef WIN32
|
541 |
|
542 |
static int64_t clock_freq;
|
543 |
|
544 |
static void init_get_clock(void) |
545 |
{ |
546 |
LARGE_INTEGER freq; |
547 |
int ret;
|
548 |
ret = QueryPerformanceFrequency(&freq); |
549 |
if (ret == 0) { |
550 |
fprintf(stderr, "Could not calibrate ticks\n");
|
551 |
exit(1);
|
552 |
} |
553 |
clock_freq = freq.QuadPart; |
554 |
} |
555 |
|
556 |
static int64_t get_clock(void) |
557 |
{ |
558 |
LARGE_INTEGER ti; |
559 |
QueryPerformanceCounter(&ti); |
560 |
return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
|
561 |
} |
562 |
|
563 |
#else
|
564 |
|
565 |
static int use_rt_clock; |
566 |
|
567 |
static void init_get_clock(void) |
568 |
{ |
569 |
use_rt_clock = 0;
|
570 |
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
571 |
|| defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
572 |
{ |
573 |
struct timespec ts;
|
574 |
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
575 |
use_rt_clock = 1;
|
576 |
} |
577 |
} |
578 |
#endif
|
579 |
} |
580 |
|
581 |
static int64_t get_clock(void) |
582 |
{ |
583 |
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
584 |
|| defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
585 |
if (use_rt_clock) {
|
586 |
struct timespec ts;
|
587 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
588 |
return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
589 |
} else
|
590 |
#endif
|
591 |
{ |
592 |
/* XXX: using gettimeofday leads to problems if the date
|
593 |
changes, so it should be avoided. */
|
594 |
return get_clock_realtime();
|
595 |
} |
596 |
} |
597 |
#endif
|
598 |
|
599 |
/* Return the virtual CPU time, based on the instruction counter. */
|
600 |
static int64_t cpu_get_icount(void) |
601 |
{ |
602 |
int64_t icount; |
603 |
CPUState *env = cpu_single_env;; |
604 |
icount = qemu_icount; |
605 |
if (env) {
|
606 |
if (!can_do_io(env))
|
607 |
fprintf(stderr, "Bad clock read\n");
|
608 |
icount -= (env->icount_decr.u16.low + env->icount_extra); |
609 |
} |
610 |
return qemu_icount_bias + (icount << icount_time_shift);
|
611 |
} |
612 |
|
613 |
/***********************************************************/
|
614 |
/* guest cycle counter */
|
615 |
|
616 |
typedef struct TimersState { |
617 |
int64_t cpu_ticks_prev; |
618 |
int64_t cpu_ticks_offset; |
619 |
int64_t cpu_clock_offset; |
620 |
int32_t cpu_ticks_enabled; |
621 |
int64_t dummy; |
622 |
} TimersState; |
623 |
|
624 |
TimersState timers_state; |
625 |
|
626 |
/* return the host CPU cycle counter and handle stop/restart */
|
627 |
int64_t cpu_get_ticks(void)
|
628 |
{ |
629 |
if (use_icount) {
|
630 |
return cpu_get_icount();
|
631 |
} |
632 |
if (!timers_state.cpu_ticks_enabled) {
|
633 |
return timers_state.cpu_ticks_offset;
|
634 |
} else {
|
635 |
int64_t ticks; |
636 |
ticks = cpu_get_real_ticks(); |
637 |
if (timers_state.cpu_ticks_prev > ticks) {
|
638 |
/* Note: non increasing ticks may happen if the host uses
|
639 |
software suspend */
|
640 |
timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
641 |
} |
642 |
timers_state.cpu_ticks_prev = ticks; |
643 |
return ticks + timers_state.cpu_ticks_offset;
|
644 |
} |
645 |
} |
646 |
|
647 |
/* return the host CPU monotonic timer and handle stop/restart */
|
648 |
static int64_t cpu_get_clock(void) |
649 |
{ |
650 |
int64_t ti; |
651 |
if (!timers_state.cpu_ticks_enabled) {
|
652 |
return timers_state.cpu_clock_offset;
|
653 |
} else {
|
654 |
ti = get_clock(); |
655 |
return ti + timers_state.cpu_clock_offset;
|
656 |
} |
657 |
} |
658 |
|
659 |
/* enable cpu_get_ticks() */
|
660 |
void cpu_enable_ticks(void) |
661 |
{ |
662 |
if (!timers_state.cpu_ticks_enabled) {
|
663 |
timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
664 |
timers_state.cpu_clock_offset -= get_clock(); |
665 |
timers_state.cpu_ticks_enabled = 1;
|
666 |
} |
667 |
} |
668 |
|
669 |
/* disable cpu_get_ticks() : the clock is stopped. You must not call
|
670 |
cpu_get_ticks() after that. */
|
671 |
void cpu_disable_ticks(void) |
672 |
{ |
673 |
if (timers_state.cpu_ticks_enabled) {
|
674 |
timers_state.cpu_ticks_offset = cpu_get_ticks(); |
675 |
timers_state.cpu_clock_offset = cpu_get_clock(); |
676 |
timers_state.cpu_ticks_enabled = 0;
|
677 |
} |
678 |
} |
679 |
|
680 |
/***********************************************************/
|
681 |
/* timers */
|
682 |
|
683 |
#define QEMU_CLOCK_REALTIME 0 |
684 |
#define QEMU_CLOCK_VIRTUAL 1 |
685 |
#define QEMU_CLOCK_HOST 2 |
686 |
|
687 |
struct QEMUClock {
|
688 |
int type;
|
689 |
/* XXX: add frequency */
|
690 |
}; |
691 |
|
692 |
struct QEMUTimer {
|
693 |
QEMUClock *clock; |
694 |
int64_t expire_time; |
695 |
QEMUTimerCB *cb; |
696 |
void *opaque;
|
697 |
struct QEMUTimer *next;
|
698 |
}; |
699 |
|
700 |
struct qemu_alarm_timer {
|
701 |
char const *name; |
702 |
unsigned int flags; |
703 |
|
704 |
int (*start)(struct qemu_alarm_timer *t); |
705 |
void (*stop)(struct qemu_alarm_timer *t); |
706 |
void (*rearm)(struct qemu_alarm_timer *t); |
707 |
void *priv;
|
708 |
}; |
709 |
|
710 |
#define ALARM_FLAG_DYNTICKS 0x1 |
711 |
#define ALARM_FLAG_EXPIRED 0x2 |
712 |
|
713 |
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
714 |
{ |
715 |
return t && (t->flags & ALARM_FLAG_DYNTICKS);
|
716 |
} |
717 |
|
718 |
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
719 |
{ |
720 |
if (!alarm_has_dynticks(t))
|
721 |
return;
|
722 |
|
723 |
t->rearm(t); |
724 |
} |
725 |
|
726 |
/* TODO: MIN_TIMER_REARM_US should be optimized */
|
727 |
#define MIN_TIMER_REARM_US 250 |
728 |
|
729 |
static struct qemu_alarm_timer *alarm_timer; |
730 |
|
731 |
#ifdef _WIN32
|
732 |
|
733 |
struct qemu_alarm_win32 {
|
734 |
MMRESULT timerId; |
735 |
unsigned int period; |
736 |
} alarm_win32_data = {0, -1}; |
737 |
|
738 |
static int win32_start_timer(struct qemu_alarm_timer *t); |
739 |
static void win32_stop_timer(struct qemu_alarm_timer *t); |
740 |
static void win32_rearm_timer(struct qemu_alarm_timer *t); |
741 |
|
742 |
#else
|
743 |
|
744 |
static int unix_start_timer(struct qemu_alarm_timer *t); |
745 |
static void unix_stop_timer(struct qemu_alarm_timer *t); |
746 |
|
747 |
#ifdef __linux__
|
748 |
|
749 |
static int dynticks_start_timer(struct qemu_alarm_timer *t); |
750 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
751 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
752 |
|
753 |
static int hpet_start_timer(struct qemu_alarm_timer *t); |
754 |
static void hpet_stop_timer(struct qemu_alarm_timer *t); |
755 |
|
756 |
static int rtc_start_timer(struct qemu_alarm_timer *t); |
757 |
static void rtc_stop_timer(struct qemu_alarm_timer *t); |
758 |
|
759 |
#endif /* __linux__ */ |
760 |
|
761 |
#endif /* _WIN32 */ |
762 |
|
763 |
/* Correlation between real and virtual time is always going to be
|
764 |
fairly approximate, so ignore small variation.
|
765 |
When the guest is idle real and virtual time will be aligned in
|
766 |
the IO wait loop. */
|
767 |
#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
768 |
|
769 |
static void icount_adjust(void) |
770 |
{ |
771 |
int64_t cur_time; |
772 |
int64_t cur_icount; |
773 |
int64_t delta; |
774 |
static int64_t last_delta;
|
775 |
/* If the VM is not running, then do nothing. */
|
776 |
if (!vm_running)
|
777 |
return;
|
778 |
|
779 |
cur_time = cpu_get_clock(); |
780 |
cur_icount = qemu_get_clock(vm_clock); |
781 |
delta = cur_icount - cur_time; |
782 |
/* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
783 |
if (delta > 0 |
784 |
&& last_delta + ICOUNT_WOBBLE < delta * 2
|
785 |
&& icount_time_shift > 0) {
|
786 |
/* The guest is getting too far ahead. Slow time down. */
|
787 |
icount_time_shift--; |
788 |
} |
789 |
if (delta < 0 |
790 |
&& last_delta - ICOUNT_WOBBLE > delta * 2
|
791 |
&& icount_time_shift < MAX_ICOUNT_SHIFT) { |
792 |
/* The guest is getting too far behind. Speed time up. */
|
793 |
icount_time_shift++; |
794 |
} |
795 |
last_delta = delta; |
796 |
qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
797 |
} |
798 |
|
799 |
static void icount_adjust_rt(void * opaque) |
800 |
{ |
801 |
qemu_mod_timer(icount_rt_timer, |
802 |
qemu_get_clock(rt_clock) + 1000);
|
803 |
icount_adjust(); |
804 |
} |
805 |
|
806 |
static void icount_adjust_vm(void * opaque) |
807 |
{ |
808 |
qemu_mod_timer(icount_vm_timer, |
809 |
qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
|
810 |
icount_adjust(); |
811 |
} |
812 |
|
813 |
static void init_icount_adjust(void) |
814 |
{ |
815 |
/* Have both realtime and virtual time triggers for speed adjustment.
|
816 |
The realtime trigger catches emulated time passing too slowly,
|
817 |
the virtual time trigger catches emulated time passing too fast.
|
818 |
Realtime triggers occur even when idle, so use them less frequently
|
819 |
than VM triggers. */
|
820 |
icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
821 |
qemu_mod_timer(icount_rt_timer, |
822 |
qemu_get_clock(rt_clock) + 1000);
|
823 |
icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
824 |
qemu_mod_timer(icount_vm_timer, |
825 |
qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
|
826 |
} |
827 |
|
828 |
static struct qemu_alarm_timer alarm_timers[] = { |
829 |
#ifndef _WIN32
|
830 |
#ifdef __linux__
|
831 |
{"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
832 |
dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
833 |
/* HPET - if available - is preferred */
|
834 |
{"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
835 |
/* ...otherwise try RTC */
|
836 |
{"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
837 |
#endif
|
838 |
{"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
839 |
#else
|
840 |
{"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
841 |
win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
842 |
{"win32", 0, win32_start_timer, |
843 |
win32_stop_timer, NULL, &alarm_win32_data},
|
844 |
#endif
|
845 |
{NULL, }
|
846 |
}; |
847 |
|
848 |
static void show_available_alarms(void) |
849 |
{ |
850 |
int i;
|
851 |
|
852 |
printf("Available alarm timers, in order of precedence:\n");
|
853 |
for (i = 0; alarm_timers[i].name; i++) |
854 |
printf("%s\n", alarm_timers[i].name);
|
855 |
} |
856 |
|
857 |
static void configure_alarms(char const *opt) |
858 |
{ |
859 |
int i;
|
860 |
int cur = 0; |
861 |
int count = ARRAY_SIZE(alarm_timers) - 1; |
862 |
char *arg;
|
863 |
char *name;
|
864 |
struct qemu_alarm_timer tmp;
|
865 |
|
866 |
if (!strcmp(opt, "?")) { |
867 |
show_available_alarms(); |
868 |
exit(0);
|
869 |
} |
870 |
|
871 |
arg = qemu_strdup(opt); |
872 |
|
873 |
/* Reorder the array */
|
874 |
name = strtok(arg, ",");
|
875 |
while (name) {
|
876 |
for (i = 0; i < count && alarm_timers[i].name; i++) { |
877 |
if (!strcmp(alarm_timers[i].name, name))
|
878 |
break;
|
879 |
} |
880 |
|
881 |
if (i == count) {
|
882 |
fprintf(stderr, "Unknown clock %s\n", name);
|
883 |
goto next;
|
884 |
} |
885 |
|
886 |
if (i < cur)
|
887 |
/* Ignore */
|
888 |
goto next;
|
889 |
|
890 |
/* Swap */
|
891 |
tmp = alarm_timers[i]; |
892 |
alarm_timers[i] = alarm_timers[cur]; |
893 |
alarm_timers[cur] = tmp; |
894 |
|
895 |
cur++; |
896 |
next:
|
897 |
name = strtok(NULL, ","); |
898 |
} |
899 |
|
900 |
qemu_free(arg); |
901 |
|
902 |
if (cur) {
|
903 |
/* Disable remaining timers */
|
904 |
for (i = cur; i < count; i++)
|
905 |
alarm_timers[i].name = NULL;
|
906 |
} else {
|
907 |
show_available_alarms(); |
908 |
exit(1);
|
909 |
} |
910 |
} |
911 |
|
912 |
#define QEMU_NUM_CLOCKS 3 |
913 |
|
914 |
QEMUClock *rt_clock; |
915 |
QEMUClock *vm_clock; |
916 |
QEMUClock *host_clock; |
917 |
|
918 |
static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
|
919 |
|
920 |
static QEMUClock *qemu_new_clock(int type) |
921 |
{ |
922 |
QEMUClock *clock; |
923 |
clock = qemu_mallocz(sizeof(QEMUClock));
|
924 |
clock->type = type; |
925 |
return clock;
|
926 |
} |
927 |
|
928 |
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
929 |
{ |
930 |
QEMUTimer *ts; |
931 |
|
932 |
ts = qemu_mallocz(sizeof(QEMUTimer));
|
933 |
ts->clock = clock; |
934 |
ts->cb = cb; |
935 |
ts->opaque = opaque; |
936 |
return ts;
|
937 |
} |
938 |
|
939 |
void qemu_free_timer(QEMUTimer *ts)
|
940 |
{ |
941 |
qemu_free(ts); |
942 |
} |
943 |
|
944 |
/* stop a timer, but do not dealloc it */
|
945 |
void qemu_del_timer(QEMUTimer *ts)
|
946 |
{ |
947 |
QEMUTimer **pt, *t; |
948 |
|
949 |
/* NOTE: this code must be signal safe because
|
950 |
qemu_timer_expired() can be called from a signal. */
|
951 |
pt = &active_timers[ts->clock->type]; |
952 |
for(;;) {
|
953 |
t = *pt; |
954 |
if (!t)
|
955 |
break;
|
956 |
if (t == ts) {
|
957 |
*pt = t->next; |
958 |
break;
|
959 |
} |
960 |
pt = &t->next; |
961 |
} |
962 |
} |
963 |
|
964 |
/* modify the current timer so that it will be fired when current_time
|
965 |
>= expire_time. The corresponding callback will be called. */
|
966 |
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
967 |
{ |
968 |
QEMUTimer **pt, *t; |
969 |
|
970 |
qemu_del_timer(ts); |
971 |
|
972 |
/* add the timer in the sorted list */
|
973 |
/* NOTE: this code must be signal safe because
|
974 |
qemu_timer_expired() can be called from a signal. */
|
975 |
pt = &active_timers[ts->clock->type]; |
976 |
for(;;) {
|
977 |
t = *pt; |
978 |
if (!t)
|
979 |
break;
|
980 |
if (t->expire_time > expire_time)
|
981 |
break;
|
982 |
pt = &t->next; |
983 |
} |
984 |
ts->expire_time = expire_time; |
985 |
ts->next = *pt; |
986 |
*pt = ts; |
987 |
|
988 |
/* Rearm if necessary */
|
989 |
if (pt == &active_timers[ts->clock->type]) {
|
990 |
if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
991 |
qemu_rearm_alarm_timer(alarm_timer); |
992 |
} |
993 |
/* Interrupt execution to force deadline recalculation. */
|
994 |
if (use_icount)
|
995 |
qemu_notify_event(); |
996 |
} |
997 |
} |
998 |
|
999 |
int qemu_timer_pending(QEMUTimer *ts)
|
1000 |
{ |
1001 |
QEMUTimer *t; |
1002 |
for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1003 |
if (t == ts)
|
1004 |
return 1; |
1005 |
} |
1006 |
return 0; |
1007 |
} |
1008 |
|
1009 |
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
|
1010 |
{ |
1011 |
if (!timer_head)
|
1012 |
return 0; |
1013 |
return (timer_head->expire_time <= current_time);
|
1014 |
} |
1015 |
|
1016 |
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1017 |
{ |
1018 |
QEMUTimer *ts; |
1019 |
|
1020 |
for(;;) {
|
1021 |
ts = *ptimer_head; |
1022 |
if (!ts || ts->expire_time > current_time)
|
1023 |
break;
|
1024 |
/* remove timer from the list before calling the callback */
|
1025 |
*ptimer_head = ts->next; |
1026 |
ts->next = NULL;
|
1027 |
|
1028 |
/* run the callback (the timer list can be modified) */
|
1029 |
ts->cb(ts->opaque); |
1030 |
} |
1031 |
} |
1032 |
|
1033 |
int64_t qemu_get_clock(QEMUClock *clock) |
1034 |
{ |
1035 |
switch(clock->type) {
|
1036 |
case QEMU_CLOCK_REALTIME:
|
1037 |
return get_clock() / 1000000; |
1038 |
default:
|
1039 |
case QEMU_CLOCK_VIRTUAL:
|
1040 |
if (use_icount) {
|
1041 |
return cpu_get_icount();
|
1042 |
} else {
|
1043 |
return cpu_get_clock();
|
1044 |
} |
1045 |
case QEMU_CLOCK_HOST:
|
1046 |
return get_clock_realtime();
|
1047 |
} |
1048 |
} |
1049 |
|
1050 |
static void init_clocks(void) |
1051 |
{ |
1052 |
init_get_clock(); |
1053 |
rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
1054 |
vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
1055 |
host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
1056 |
|
1057 |
rtc_clock = host_clock; |
1058 |
} |
1059 |
|
1060 |
/* save a timer */
|
1061 |
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1062 |
{ |
1063 |
uint64_t expire_time; |
1064 |
|
1065 |
if (qemu_timer_pending(ts)) {
|
1066 |
expire_time = ts->expire_time; |
1067 |
} else {
|
1068 |
expire_time = -1;
|
1069 |
} |
1070 |
qemu_put_be64(f, expire_time); |
1071 |
} |
1072 |
|
1073 |
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1074 |
{ |
1075 |
uint64_t expire_time; |
1076 |
|
1077 |
expire_time = qemu_get_be64(f); |
1078 |
if (expire_time != -1) { |
1079 |
qemu_mod_timer(ts, expire_time); |
1080 |
} else {
|
1081 |
qemu_del_timer(ts); |
1082 |
} |
1083 |
} |
1084 |
|
1085 |
static const VMStateDescription vmstate_timers = { |
1086 |
.name = "timer",
|
1087 |
.version_id = 2,
|
1088 |
.minimum_version_id = 1,
|
1089 |
.minimum_version_id_old = 1,
|
1090 |
.fields = (VMStateField []) { |
1091 |
VMSTATE_INT64(cpu_ticks_offset, TimersState), |
1092 |
VMSTATE_INT64(dummy, TimersState), |
1093 |
VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
|
1094 |
VMSTATE_END_OF_LIST() |
1095 |
} |
1096 |
}; |
1097 |
|
1098 |
static void qemu_event_increment(void); |
1099 |
|
1100 |
#ifdef _WIN32
|
1101 |
static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
1102 |
DWORD_PTR dwUser, DWORD_PTR dw1, |
1103 |
DWORD_PTR dw2) |
1104 |
#else
|
1105 |
static void host_alarm_handler(int host_signum) |
1106 |
#endif
|
1107 |
{ |
1108 |
#if 0
|
1109 |
#define DISP_FREQ 1000
|
1110 |
{
|
1111 |
static int64_t delta_min = INT64_MAX;
|
1112 |
static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1113 |
static int count;
|
1114 |
ti = qemu_get_clock(vm_clock);
|
1115 |
if (last_clock != 0) {
|
1116 |
delta = ti - last_clock;
|
1117 |
if (delta < delta_min)
|
1118 |
delta_min = delta;
|
1119 |
if (delta > delta_max)
|
1120 |
delta_max = delta;
|
1121 |
delta_cum += delta;
|
1122 |
if (++count == DISP_FREQ) {
|
1123 |
printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1124 |
muldiv64(delta_min, 1000000, get_ticks_per_sec()),
|
1125 |
muldiv64(delta_max, 1000000, get_ticks_per_sec()),
|
1126 |
muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
|
1127 |
(double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
|
1128 |
count = 0;
|
1129 |
delta_min = INT64_MAX;
|
1130 |
delta_max = 0;
|
1131 |
delta_cum = 0;
|
1132 |
}
|
1133 |
}
|
1134 |
last_clock = ti;
|
1135 |
}
|
1136 |
#endif
|
1137 |
if (alarm_has_dynticks(alarm_timer) ||
|
1138 |
(!use_icount && |
1139 |
qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL], |
1140 |
qemu_get_clock(vm_clock))) || |
1141 |
qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME], |
1142 |
qemu_get_clock(rt_clock)) || |
1143 |
qemu_timer_expired(active_timers[QEMU_CLOCK_HOST], |
1144 |
qemu_get_clock(host_clock))) { |
1145 |
qemu_event_increment(); |
1146 |
if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
|
1147 |
|
1148 |
#ifndef CONFIG_IOTHREAD
|
1149 |
if (next_cpu) {
|
1150 |
/* stop the currently executing cpu because a timer occured */
|
1151 |
cpu_exit(next_cpu); |
1152 |
} |
1153 |
#endif
|
1154 |
timer_alarm_pending = 1;
|
1155 |
qemu_notify_event(); |
1156 |
} |
1157 |
} |
1158 |
|
1159 |
static int64_t qemu_next_deadline(void) |
1160 |
{ |
1161 |
/* To avoid problems with overflow limit this to 2^32. */
|
1162 |
int64_t delta = INT32_MAX; |
1163 |
|
1164 |
if (active_timers[QEMU_CLOCK_VIRTUAL]) {
|
1165 |
delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time - |
1166 |
qemu_get_clock(vm_clock); |
1167 |
} |
1168 |
if (active_timers[QEMU_CLOCK_HOST]) {
|
1169 |
int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time - |
1170 |
qemu_get_clock(host_clock); |
1171 |
if (hdelta < delta)
|
1172 |
delta = hdelta; |
1173 |
} |
1174 |
|
1175 |
if (delta < 0) |
1176 |
delta = 0;
|
1177 |
|
1178 |
return delta;
|
1179 |
} |
1180 |
|
1181 |
#if defined(__linux__)
|
1182 |
static uint64_t qemu_next_deadline_dyntick(void) |
1183 |
{ |
1184 |
int64_t delta; |
1185 |
int64_t rtdelta; |
1186 |
|
1187 |
if (use_icount)
|
1188 |
delta = INT32_MAX; |
1189 |
else
|
1190 |
delta = (qemu_next_deadline() + 999) / 1000; |
1191 |
|
1192 |
if (active_timers[QEMU_CLOCK_REALTIME]) {
|
1193 |
rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time - |
1194 |
qemu_get_clock(rt_clock))*1000;
|
1195 |
if (rtdelta < delta)
|
1196 |
delta = rtdelta; |
1197 |
} |
1198 |
|
1199 |
if (delta < MIN_TIMER_REARM_US)
|
1200 |
delta = MIN_TIMER_REARM_US; |
1201 |
|
1202 |
return delta;
|
1203 |
} |
1204 |
#endif
|
1205 |
|
1206 |
#ifndef _WIN32
|
1207 |
|
1208 |
/* Sets a specific flag */
|
1209 |
static int fcntl_setfl(int fd, int flag) |
1210 |
{ |
1211 |
int flags;
|
1212 |
|
1213 |
flags = fcntl(fd, F_GETFL); |
1214 |
if (flags == -1) |
1215 |
return -errno;
|
1216 |
|
1217 |
if (fcntl(fd, F_SETFL, flags | flag) == -1) |
1218 |
return -errno;
|
1219 |
|
1220 |
return 0; |
1221 |
} |
1222 |
|
1223 |
#if defined(__linux__)
|
1224 |
|
1225 |
#define RTC_FREQ 1024 |
1226 |
|
1227 |
static void enable_sigio_timer(int fd) |
1228 |
{ |
1229 |
struct sigaction act;
|
1230 |
|
1231 |
/* timer signal */
|
1232 |
sigfillset(&act.sa_mask); |
1233 |
act.sa_flags = 0;
|
1234 |
act.sa_handler = host_alarm_handler; |
1235 |
|
1236 |
sigaction(SIGIO, &act, NULL);
|
1237 |
fcntl_setfl(fd, O_ASYNC); |
1238 |
fcntl(fd, F_SETOWN, getpid()); |
1239 |
} |
1240 |
|
1241 |
static int hpet_start_timer(struct qemu_alarm_timer *t) |
1242 |
{ |
1243 |
struct hpet_info info;
|
1244 |
int r, fd;
|
1245 |
|
1246 |
fd = open("/dev/hpet", O_RDONLY);
|
1247 |
if (fd < 0) |
1248 |
return -1; |
1249 |
|
1250 |
/* Set frequency */
|
1251 |
r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1252 |
if (r < 0) { |
1253 |
fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1254 |
"error, but for better emulation accuracy type:\n"
|
1255 |
"'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1256 |
goto fail;
|
1257 |
} |
1258 |
|
1259 |
/* Check capabilities */
|
1260 |
r = ioctl(fd, HPET_INFO, &info); |
1261 |
if (r < 0) |
1262 |
goto fail;
|
1263 |
|
1264 |
/* Enable periodic mode */
|
1265 |
r = ioctl(fd, HPET_EPI, 0);
|
1266 |
if (info.hi_flags && (r < 0)) |
1267 |
goto fail;
|
1268 |
|
1269 |
/* Enable interrupt */
|
1270 |
r = ioctl(fd, HPET_IE_ON, 0);
|
1271 |
if (r < 0) |
1272 |
goto fail;
|
1273 |
|
1274 |
enable_sigio_timer(fd); |
1275 |
t->priv = (void *)(long)fd; |
1276 |
|
1277 |
return 0; |
1278 |
fail:
|
1279 |
close(fd); |
1280 |
return -1; |
1281 |
} |
1282 |
|
1283 |
static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1284 |
{ |
1285 |
int fd = (long)t->priv; |
1286 |
|
1287 |
close(fd); |
1288 |
} |
1289 |
|
1290 |
static int rtc_start_timer(struct qemu_alarm_timer *t) |
1291 |
{ |
1292 |
int rtc_fd;
|
1293 |
unsigned long current_rtc_freq = 0; |
1294 |
|
1295 |
TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1296 |
if (rtc_fd < 0) |
1297 |
return -1; |
1298 |
ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1299 |
if (current_rtc_freq != RTC_FREQ &&
|
1300 |
ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1301 |
fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1302 |
"error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1303 |
"type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1304 |
goto fail;
|
1305 |
} |
1306 |
if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1307 |
fail:
|
1308 |
close(rtc_fd); |
1309 |
return -1; |
1310 |
} |
1311 |
|
1312 |
enable_sigio_timer(rtc_fd); |
1313 |
|
1314 |
t->priv = (void *)(long)rtc_fd; |
1315 |
|
1316 |
return 0; |
1317 |
} |
1318 |
|
1319 |
static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1320 |
{ |
1321 |
int rtc_fd = (long)t->priv; |
1322 |
|
1323 |
close(rtc_fd); |
1324 |
} |
1325 |
|
1326 |
static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1327 |
{ |
1328 |
struct sigevent ev;
|
1329 |
timer_t host_timer; |
1330 |
struct sigaction act;
|
1331 |
|
1332 |
sigfillset(&act.sa_mask); |
1333 |
act.sa_flags = 0;
|
1334 |
act.sa_handler = host_alarm_handler; |
1335 |
|
1336 |
sigaction(SIGALRM, &act, NULL);
|
1337 |
|
1338 |
/*
|
1339 |
* Initialize ev struct to 0 to avoid valgrind complaining
|
1340 |
* about uninitialized data in timer_create call
|
1341 |
*/
|
1342 |
memset(&ev, 0, sizeof(ev)); |
1343 |
ev.sigev_value.sival_int = 0;
|
1344 |
ev.sigev_notify = SIGEV_SIGNAL; |
1345 |
ev.sigev_signo = SIGALRM; |
1346 |
|
1347 |
if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1348 |
perror("timer_create");
|
1349 |
|
1350 |
/* disable dynticks */
|
1351 |
fprintf(stderr, "Dynamic Ticks disabled\n");
|
1352 |
|
1353 |
return -1; |
1354 |
} |
1355 |
|
1356 |
t->priv = (void *)(long)host_timer; |
1357 |
|
1358 |
return 0; |
1359 |
} |
1360 |
|
1361 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1362 |
{ |
1363 |
timer_t host_timer = (timer_t)(long)t->priv;
|
1364 |
|
1365 |
timer_delete(host_timer); |
1366 |
} |
1367 |
|
1368 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1369 |
{ |
1370 |
timer_t host_timer = (timer_t)(long)t->priv;
|
1371 |
struct itimerspec timeout;
|
1372 |
int64_t nearest_delta_us = INT64_MAX; |
1373 |
int64_t current_us; |
1374 |
|
1375 |
if (!active_timers[QEMU_CLOCK_REALTIME] &&
|
1376 |
!active_timers[QEMU_CLOCK_VIRTUAL] && |
1377 |
!active_timers[QEMU_CLOCK_HOST]) |
1378 |
return;
|
1379 |
|
1380 |
nearest_delta_us = qemu_next_deadline_dyntick(); |
1381 |
|
1382 |
/* check whether a timer is already running */
|
1383 |
if (timer_gettime(host_timer, &timeout)) {
|
1384 |
perror("gettime");
|
1385 |
fprintf(stderr, "Internal timer error: aborting\n");
|
1386 |
exit(1);
|
1387 |
} |
1388 |
current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1389 |
if (current_us && current_us <= nearest_delta_us)
|
1390 |
return;
|
1391 |
|
1392 |
timeout.it_interval.tv_sec = 0;
|
1393 |
timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1394 |
timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1395 |
timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1396 |
if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1397 |
perror("settime");
|
1398 |
fprintf(stderr, "Internal timer error: aborting\n");
|
1399 |
exit(1);
|
1400 |
} |
1401 |
} |
1402 |
|
1403 |
#endif /* defined(__linux__) */ |
1404 |
|
1405 |
static int unix_start_timer(struct qemu_alarm_timer *t) |
1406 |
{ |
1407 |
struct sigaction act;
|
1408 |
struct itimerval itv;
|
1409 |
int err;
|
1410 |
|
1411 |
/* timer signal */
|
1412 |
sigfillset(&act.sa_mask); |
1413 |
act.sa_flags = 0;
|
1414 |
act.sa_handler = host_alarm_handler; |
1415 |
|
1416 |
sigaction(SIGALRM, &act, NULL);
|
1417 |
|
1418 |
itv.it_interval.tv_sec = 0;
|
1419 |
/* for i386 kernel 2.6 to get 1 ms */
|
1420 |
itv.it_interval.tv_usec = 999;
|
1421 |
itv.it_value.tv_sec = 0;
|
1422 |
itv.it_value.tv_usec = 10 * 1000; |
1423 |
|
1424 |
err = setitimer(ITIMER_REAL, &itv, NULL);
|
1425 |
if (err)
|
1426 |
return -1; |
1427 |
|
1428 |
return 0; |
1429 |
} |
1430 |
|
1431 |
static void unix_stop_timer(struct qemu_alarm_timer *t) |
1432 |
{ |
1433 |
struct itimerval itv;
|
1434 |
|
1435 |
memset(&itv, 0, sizeof(itv)); |
1436 |
setitimer(ITIMER_REAL, &itv, NULL);
|
1437 |
} |
1438 |
|
1439 |
#endif /* !defined(_WIN32) */ |
1440 |
|
1441 |
|
1442 |
#ifdef _WIN32
|
1443 |
|
1444 |
static int win32_start_timer(struct qemu_alarm_timer *t) |
1445 |
{ |
1446 |
TIMECAPS tc; |
1447 |
struct qemu_alarm_win32 *data = t->priv;
|
1448 |
UINT flags; |
1449 |
|
1450 |
memset(&tc, 0, sizeof(tc)); |
1451 |
timeGetDevCaps(&tc, sizeof(tc));
|
1452 |
|
1453 |
if (data->period < tc.wPeriodMin)
|
1454 |
data->period = tc.wPeriodMin; |
1455 |
|
1456 |
timeBeginPeriod(data->period); |
1457 |
|
1458 |
flags = TIME_CALLBACK_FUNCTION; |
1459 |
if (alarm_has_dynticks(t))
|
1460 |
flags |= TIME_ONESHOT; |
1461 |
else
|
1462 |
flags |= TIME_PERIODIC; |
1463 |
|
1464 |
data->timerId = timeSetEvent(1, // interval (ms) |
1465 |
data->period, // resolution
|
1466 |
host_alarm_handler, // function
|
1467 |
(DWORD)t, // parameter
|
1468 |
flags); |
1469 |
|
1470 |
if (!data->timerId) {
|
1471 |
fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
|
1472 |
GetLastError()); |
1473 |
timeEndPeriod(data->period); |
1474 |
return -1; |
1475 |
} |
1476 |
|
1477 |
return 0; |
1478 |
} |
1479 |
|
1480 |
static void win32_stop_timer(struct qemu_alarm_timer *t) |
1481 |
{ |
1482 |
struct qemu_alarm_win32 *data = t->priv;
|
1483 |
|
1484 |
timeKillEvent(data->timerId); |
1485 |
timeEndPeriod(data->period); |
1486 |
} |
1487 |
|
1488 |
static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1489 |
{ |
1490 |
struct qemu_alarm_win32 *data = t->priv;
|
1491 |
|
1492 |
if (!active_timers[QEMU_CLOCK_REALTIME] &&
|
1493 |
!active_timers[QEMU_CLOCK_VIRTUAL] && |
1494 |
!active_timers[QEMU_CLOCK_HOST]) |
1495 |
return;
|
1496 |
|
1497 |
timeKillEvent(data->timerId); |
1498 |
|
1499 |
data->timerId = timeSetEvent(1,
|
1500 |
data->period, |
1501 |
host_alarm_handler, |
1502 |
(DWORD)t, |
1503 |
TIME_ONESHOT | TIME_PERIODIC); |
1504 |
|
1505 |
if (!data->timerId) {
|
1506 |
fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
|
1507 |
GetLastError()); |
1508 |
|
1509 |
timeEndPeriod(data->period); |
1510 |
exit(1);
|
1511 |
} |
1512 |
} |
1513 |
|
1514 |
#endif /* _WIN32 */ |
1515 |
|
1516 |
static int init_timer_alarm(void) |
1517 |
{ |
1518 |
struct qemu_alarm_timer *t = NULL; |
1519 |
int i, err = -1; |
1520 |
|
1521 |
for (i = 0; alarm_timers[i].name; i++) { |
1522 |
t = &alarm_timers[i]; |
1523 |
|
1524 |
err = t->start(t); |
1525 |
if (!err)
|
1526 |
break;
|
1527 |
} |
1528 |
|
1529 |
if (err) {
|
1530 |
err = -ENOENT; |
1531 |
goto fail;
|
1532 |
} |
1533 |
|
1534 |
alarm_timer = t; |
1535 |
|
1536 |
return 0; |
1537 |
|
1538 |
fail:
|
1539 |
return err;
|
1540 |
} |
1541 |
|
1542 |
static void quit_timers(void) |
1543 |
{ |
1544 |
alarm_timer->stop(alarm_timer); |
1545 |
alarm_timer = NULL;
|
1546 |
} |
1547 |
|
1548 |
/***********************************************************/
|
1549 |
/* host time/date access */
|
1550 |
void qemu_get_timedate(struct tm *tm, int offset) |
1551 |
{ |
1552 |
time_t ti; |
1553 |
struct tm *ret;
|
1554 |
|
1555 |
time(&ti); |
1556 |
ti += offset; |
1557 |
if (rtc_date_offset == -1) { |
1558 |
if (rtc_utc)
|
1559 |
ret = gmtime(&ti); |
1560 |
else
|
1561 |
ret = localtime(&ti); |
1562 |
} else {
|
1563 |
ti -= rtc_date_offset; |
1564 |
ret = gmtime(&ti); |
1565 |
} |
1566 |
|
1567 |
memcpy(tm, ret, sizeof(struct tm)); |
1568 |
} |
1569 |
|
1570 |
int qemu_timedate_diff(struct tm *tm) |
1571 |
{ |
1572 |
time_t seconds; |
1573 |
|
1574 |
if (rtc_date_offset == -1) |
1575 |
if (rtc_utc)
|
1576 |
seconds = mktimegm(tm); |
1577 |
else
|
1578 |
seconds = mktime(tm); |
1579 |
else
|
1580 |
seconds = mktimegm(tm) + rtc_date_offset; |
1581 |
|
1582 |
return seconds - time(NULL); |
1583 |
} |
1584 |
|
1585 |
static void configure_rtc_date_offset(const char *startdate, int legacy) |
1586 |
{ |
1587 |
time_t rtc_start_date; |
1588 |
struct tm tm;
|
1589 |
|
1590 |
if (!strcmp(startdate, "now") && legacy) { |
1591 |
rtc_date_offset = -1;
|
1592 |
} else {
|
1593 |
if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", |
1594 |
&tm.tm_year, |
1595 |
&tm.tm_mon, |
1596 |
&tm.tm_mday, |
1597 |
&tm.tm_hour, |
1598 |
&tm.tm_min, |
1599 |
&tm.tm_sec) == 6) {
|
1600 |
/* OK */
|
1601 |
} else if (sscanf(startdate, "%d-%d-%d", |
1602 |
&tm.tm_year, |
1603 |
&tm.tm_mon, |
1604 |
&tm.tm_mday) == 3) {
|
1605 |
tm.tm_hour = 0;
|
1606 |
tm.tm_min = 0;
|
1607 |
tm.tm_sec = 0;
|
1608 |
} else {
|
1609 |
goto date_fail;
|
1610 |
} |
1611 |
tm.tm_year -= 1900;
|
1612 |
tm.tm_mon--; |
1613 |
rtc_start_date = mktimegm(&tm); |
1614 |
if (rtc_start_date == -1) { |
1615 |
date_fail:
|
1616 |
fprintf(stderr, "Invalid date format. Valid formats are:\n"
|
1617 |
"'2006-06-17T16:01:21' or '2006-06-17'\n");
|
1618 |
exit(1);
|
1619 |
} |
1620 |
rtc_date_offset = time(NULL) - rtc_start_date;
|
1621 |
} |
1622 |
} |
1623 |
|
1624 |
static void configure_rtc(QemuOpts *opts) |
1625 |
{ |
1626 |
const char *value; |
1627 |
|
1628 |
value = qemu_opt_get(opts, "base");
|
1629 |
if (value) {
|
1630 |
if (!strcmp(value, "utc")) { |
1631 |
rtc_utc = 1;
|
1632 |
} else if (!strcmp(value, "localtime")) { |
1633 |
rtc_utc = 0;
|
1634 |
} else {
|
1635 |
configure_rtc_date_offset(value, 0);
|
1636 |
} |
1637 |
} |
1638 |
value = qemu_opt_get(opts, "clock");
|
1639 |
if (value) {
|
1640 |
if (!strcmp(value, "host")) { |
1641 |
rtc_clock = host_clock; |
1642 |
} else if (!strcmp(value, "vm")) { |
1643 |
rtc_clock = vm_clock; |
1644 |
} else {
|
1645 |
fprintf(stderr, "qemu: invalid option value '%s'\n", value);
|
1646 |
exit(1);
|
1647 |
} |
1648 |
} |
1649 |
#ifdef CONFIG_TARGET_I386
|
1650 |
value = qemu_opt_get(opts, "driftfix");
|
1651 |
if (value) {
|
1652 |
if (!strcmp(buf, "slew")) { |
1653 |
rtc_td_hack = 1;
|
1654 |
} else if (!strcmp(buf, "none")) { |
1655 |
rtc_td_hack = 0;
|
1656 |
} else {
|
1657 |
fprintf(stderr, "qemu: invalid option value '%s'\n", value);
|
1658 |
exit(1);
|
1659 |
} |
1660 |
} |
1661 |
#endif
|
1662 |
} |
1663 |
|
1664 |
#ifdef _WIN32
|
1665 |
static void socket_cleanup(void) |
1666 |
{ |
1667 |
WSACleanup(); |
1668 |
} |
1669 |
|
1670 |
static int socket_init(void) |
1671 |
{ |
1672 |
WSADATA Data; |
1673 |
int ret, err;
|
1674 |
|
1675 |
ret = WSAStartup(MAKEWORD(2,2), &Data); |
1676 |
if (ret != 0) { |
1677 |
err = WSAGetLastError(); |
1678 |
fprintf(stderr, "WSAStartup: %d\n", err);
|
1679 |
return -1; |
1680 |
} |
1681 |
atexit(socket_cleanup); |
1682 |
return 0; |
1683 |
} |
1684 |
#endif
|
1685 |
|
1686 |
/***********************************************************/
|
1687 |
/* Bluetooth support */
|
1688 |
static int nb_hcis; |
1689 |
static int cur_hci; |
1690 |
static struct HCIInfo *hci_table[MAX_NICS]; |
1691 |
|
1692 |
static struct bt_vlan_s { |
1693 |
struct bt_scatternet_s net;
|
1694 |
int id;
|
1695 |
struct bt_vlan_s *next;
|
1696 |
} *first_bt_vlan; |
1697 |
|
1698 |
/* find or alloc a new bluetooth "VLAN" */
|
1699 |
static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
1700 |
{ |
1701 |
struct bt_vlan_s **pvlan, *vlan;
|
1702 |
for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
1703 |
if (vlan->id == id)
|
1704 |
return &vlan->net;
|
1705 |
} |
1706 |
vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
1707 |
vlan->id = id; |
1708 |
pvlan = &first_bt_vlan; |
1709 |
while (*pvlan != NULL) |
1710 |
pvlan = &(*pvlan)->next; |
1711 |
*pvlan = vlan; |
1712 |
return &vlan->net;
|
1713 |
} |
1714 |
|
1715 |
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
1716 |
{ |
1717 |
} |
1718 |
|
1719 |
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
1720 |
{ |
1721 |
return -ENOTSUP;
|
1722 |
} |
1723 |
|
1724 |
static struct HCIInfo null_hci = { |
1725 |
.cmd_send = null_hci_send, |
1726 |
.sco_send = null_hci_send, |
1727 |
.acl_send = null_hci_send, |
1728 |
.bdaddr_set = null_hci_addr_set, |
1729 |
}; |
1730 |
|
1731 |
struct HCIInfo *qemu_next_hci(void) |
1732 |
{ |
1733 |
if (cur_hci == nb_hcis)
|
1734 |
return &null_hci;
|
1735 |
|
1736 |
return hci_table[cur_hci++];
|
1737 |
} |
1738 |
|
1739 |
static struct HCIInfo *hci_init(const char *str) |
1740 |
{ |
1741 |
char *endp;
|
1742 |
struct bt_scatternet_s *vlan = 0; |
1743 |
|
1744 |
if (!strcmp(str, "null")) |
1745 |
/* null */
|
1746 |
return &null_hci;
|
1747 |
else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
1748 |
/* host[:hciN] */
|
1749 |
return bt_host_hci(str[4] ? str + 5 : "hci0"); |
1750 |
else if (!strncmp(str, "hci", 3)) { |
1751 |
/* hci[,vlan=n] */
|
1752 |
if (str[3]) { |
1753 |
if (!strncmp(str + 3, ",vlan=", 6)) { |
1754 |
vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
1755 |
if (*endp)
|
1756 |
vlan = 0;
|
1757 |
} |
1758 |
} else
|
1759 |
vlan = qemu_find_bt_vlan(0);
|
1760 |
if (vlan)
|
1761 |
return bt_new_hci(vlan);
|
1762 |
} |
1763 |
|
1764 |
fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
|
1765 |
|
1766 |
return 0; |
1767 |
} |
1768 |
|
1769 |
static int bt_hci_parse(const char *str) |
1770 |
{ |
1771 |
struct HCIInfo *hci;
|
1772 |
bdaddr_t bdaddr; |
1773 |
|
1774 |
if (nb_hcis >= MAX_NICS) {
|
1775 |
fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
|
1776 |
return -1; |
1777 |
} |
1778 |
|
1779 |
hci = hci_init(str); |
1780 |
if (!hci)
|
1781 |
return -1; |
1782 |
|
1783 |
bdaddr.b[0] = 0x52; |
1784 |
bdaddr.b[1] = 0x54; |
1785 |
bdaddr.b[2] = 0x00; |
1786 |
bdaddr.b[3] = 0x12; |
1787 |
bdaddr.b[4] = 0x34; |
1788 |
bdaddr.b[5] = 0x56 + nb_hcis; |
1789 |
hci->bdaddr_set(hci, bdaddr.b); |
1790 |
|
1791 |
hci_table[nb_hcis++] = hci; |
1792 |
|
1793 |
return 0; |
1794 |
} |
1795 |
|
1796 |
static void bt_vhci_add(int vlan_id) |
1797 |
{ |
1798 |
struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
|
1799 |
|
1800 |
if (!vlan->slave)
|
1801 |
fprintf(stderr, "qemu: warning: adding a VHCI to "
|
1802 |
"an empty scatternet %i\n", vlan_id);
|
1803 |
|
1804 |
bt_vhci_init(bt_new_hci(vlan)); |
1805 |
} |
1806 |
|
1807 |
static struct bt_device_s *bt_device_add(const char *opt) |
1808 |
{ |
1809 |
struct bt_scatternet_s *vlan;
|
1810 |
int vlan_id = 0; |
1811 |
char *endp = strstr(opt, ",vlan="); |
1812 |
int len = (endp ? endp - opt : strlen(opt)) + 1; |
1813 |
char devname[10]; |
1814 |
|
1815 |
pstrcpy(devname, MIN(sizeof(devname), len), opt);
|
1816 |
|
1817 |
if (endp) {
|
1818 |
vlan_id = strtol(endp + 6, &endp, 0); |
1819 |
if (*endp) {
|
1820 |
fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
|
1821 |
return 0; |
1822 |
} |
1823 |
} |
1824 |
|
1825 |
vlan = qemu_find_bt_vlan(vlan_id); |
1826 |
|
1827 |
if (!vlan->slave)
|
1828 |
fprintf(stderr, "qemu: warning: adding a slave device to "
|
1829 |
"an empty scatternet %i\n", vlan_id);
|
1830 |
|
1831 |
if (!strcmp(devname, "keyboard")) |
1832 |
return bt_keyboard_init(vlan);
|
1833 |
|
1834 |
fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
|
1835 |
return 0; |
1836 |
} |
1837 |
|
1838 |
static int bt_parse(const char *opt) |
1839 |
{ |
1840 |
const char *endp, *p; |
1841 |
int vlan;
|
1842 |
|
1843 |
if (strstart(opt, "hci", &endp)) { |
1844 |
if (!*endp || *endp == ',') { |
1845 |
if (*endp)
|
1846 |
if (!strstart(endp, ",vlan=", 0)) |
1847 |
opt = endp + 1;
|
1848 |
|
1849 |
return bt_hci_parse(opt);
|
1850 |
} |
1851 |
} else if (strstart(opt, "vhci", &endp)) { |
1852 |
if (!*endp || *endp == ',') { |
1853 |
if (*endp) {
|
1854 |
if (strstart(endp, ",vlan=", &p)) { |
1855 |
vlan = strtol(p, (char **) &endp, 0); |
1856 |
if (*endp) {
|
1857 |
fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
|
1858 |
return 1; |
1859 |
} |
1860 |
} else {
|
1861 |
fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
1862 |
return 1; |
1863 |
} |
1864 |
} else
|
1865 |
vlan = 0;
|
1866 |
|
1867 |
bt_vhci_add(vlan); |
1868 |
return 0; |
1869 |
} |
1870 |
} else if (strstart(opt, "device:", &endp)) |
1871 |
return !bt_device_add(endp);
|
1872 |
|
1873 |
fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
|
1874 |
return 1; |
1875 |
} |
1876 |
|
1877 |
/***********************************************************/
|
1878 |
/* QEMU Block devices */
|
1879 |
|
1880 |
#define HD_ALIAS "index=%d,media=disk" |
1881 |
#define CDROM_ALIAS "index=2,media=cdrom" |
1882 |
#define FD_ALIAS "index=%d,if=floppy" |
1883 |
#define PFLASH_ALIAS "if=pflash" |
1884 |
#define MTD_ALIAS "if=mtd" |
1885 |
#define SD_ALIAS "index=0,if=sd" |
1886 |
|
1887 |
QemuOpts *drive_add(const char *file, const char *fmt, ...) |
1888 |
{ |
1889 |
va_list ap; |
1890 |
char optstr[1024]; |
1891 |
QemuOpts *opts; |
1892 |
|
1893 |
va_start(ap, fmt); |
1894 |
vsnprintf(optstr, sizeof(optstr), fmt, ap);
|
1895 |
va_end(ap); |
1896 |
|
1897 |
opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
|
1898 |
if (!opts) {
|
1899 |
fprintf(stderr, "%s: huh? duplicate? (%s)\n",
|
1900 |
__FUNCTION__, optstr); |
1901 |
return NULL; |
1902 |
} |
1903 |
if (file)
|
1904 |
qemu_opt_set(opts, "file", file);
|
1905 |
return opts;
|
1906 |
} |
1907 |
|
1908 |
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
1909 |
{ |
1910 |
DriveInfo *dinfo; |
1911 |
|
1912 |
/* seek interface, bus and unit */
|
1913 |
|
1914 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
1915 |
if (dinfo->type == type &&
|
1916 |
dinfo->bus == bus && |
1917 |
dinfo->unit == unit) |
1918 |
return dinfo;
|
1919 |
} |
1920 |
|
1921 |
return NULL; |
1922 |
} |
1923 |
|
1924 |
DriveInfo *drive_get_by_id(const char *id) |
1925 |
{ |
1926 |
DriveInfo *dinfo; |
1927 |
|
1928 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
1929 |
if (strcmp(id, dinfo->id))
|
1930 |
continue;
|
1931 |
return dinfo;
|
1932 |
} |
1933 |
return NULL; |
1934 |
} |
1935 |
|
1936 |
int drive_get_max_bus(BlockInterfaceType type)
|
1937 |
{ |
1938 |
int max_bus;
|
1939 |
DriveInfo *dinfo; |
1940 |
|
1941 |
max_bus = -1;
|
1942 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
1943 |
if(dinfo->type == type &&
|
1944 |
dinfo->bus > max_bus) |
1945 |
max_bus = dinfo->bus; |
1946 |
} |
1947 |
return max_bus;
|
1948 |
} |
1949 |
|
1950 |
const char *drive_get_serial(BlockDriverState *bdrv) |
1951 |
{ |
1952 |
DriveInfo *dinfo; |
1953 |
|
1954 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
1955 |
if (dinfo->bdrv == bdrv)
|
1956 |
return dinfo->serial;
|
1957 |
} |
1958 |
|
1959 |
return "\0"; |
1960 |
} |
1961 |
|
1962 |
BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
1963 |
{ |
1964 |
DriveInfo *dinfo; |
1965 |
|
1966 |
QTAILQ_FOREACH(dinfo, &drives, next) { |
1967 |
if (dinfo->bdrv == bdrv)
|
1968 |
return dinfo->onerror;
|
1969 |
} |
1970 |
|
1971 |
return BLOCK_ERR_STOP_ENOSPC;
|
1972 |
} |
1973 |
|
1974 |
static void bdrv_format_print(void *opaque, const char *name) |
1975 |
{ |
1976 |
fprintf(stderr, " %s", name);
|
1977 |
} |
1978 |
|
1979 |
void drive_uninit(DriveInfo *dinfo)
|
1980 |
{ |
1981 |
qemu_opts_del(dinfo->opts); |
1982 |
bdrv_delete(dinfo->bdrv); |
1983 |
QTAILQ_REMOVE(&drives, dinfo, next); |
1984 |
qemu_free(dinfo); |
1985 |
} |
1986 |
|
1987 |
DriveInfo *drive_init(QemuOpts *opts, void *opaque,
|
1988 |
int *fatal_error)
|
1989 |
{ |
1990 |
const char *buf; |
1991 |
const char *file = NULL; |
1992 |
char devname[128]; |
1993 |
const char *serial; |
1994 |
const char *mediastr = ""; |
1995 |
BlockInterfaceType type; |
1996 |
enum { MEDIA_DISK, MEDIA_CDROM } media;
|
1997 |
int bus_id, unit_id;
|
1998 |
int cyls, heads, secs, translation;
|
1999 |
BlockDriver *drv = NULL;
|
2000 |
QEMUMachine *machine = opaque; |
2001 |
int max_devs;
|
2002 |
int index;
|
2003 |
int cache;
|
2004 |
int aio = 0; |
2005 |
int ro = 0; |
2006 |
int bdrv_flags, onerror;
|
2007 |
const char *devaddr; |
2008 |
DriveInfo *dinfo; |
2009 |
int snapshot = 0; |
2010 |
|
2011 |
*fatal_error = 1;
|
2012 |
|
2013 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
2014 |
cache = 1;
|
2015 |
|
2016 |
if (machine && machine->use_scsi) {
|
2017 |
type = IF_SCSI; |
2018 |
max_devs = MAX_SCSI_DEVS; |
2019 |
pstrcpy(devname, sizeof(devname), "scsi"); |
2020 |
} else {
|
2021 |
type = IF_IDE; |
2022 |
max_devs = MAX_IDE_DEVS; |
2023 |
pstrcpy(devname, sizeof(devname), "ide"); |
2024 |
} |
2025 |
media = MEDIA_DISK; |
2026 |
|
2027 |
/* extract parameters */
|
2028 |
bus_id = qemu_opt_get_number(opts, "bus", 0); |
2029 |
unit_id = qemu_opt_get_number(opts, "unit", -1); |
2030 |
index = qemu_opt_get_number(opts, "index", -1); |
2031 |
|
2032 |
cyls = qemu_opt_get_number(opts, "cyls", 0); |
2033 |
heads = qemu_opt_get_number(opts, "heads", 0); |
2034 |
secs = qemu_opt_get_number(opts, "secs", 0); |
2035 |
|
2036 |
snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
2037 |
ro = qemu_opt_get_bool(opts, "readonly", 0); |
2038 |
|
2039 |
file = qemu_opt_get(opts, "file");
|
2040 |
serial = qemu_opt_get(opts, "serial");
|
2041 |
|
2042 |
if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
2043 |
pstrcpy(devname, sizeof(devname), buf);
|
2044 |
if (!strcmp(buf, "ide")) { |
2045 |
type = IF_IDE; |
2046 |
max_devs = MAX_IDE_DEVS; |
2047 |
} else if (!strcmp(buf, "scsi")) { |
2048 |
type = IF_SCSI; |
2049 |
max_devs = MAX_SCSI_DEVS; |
2050 |
} else if (!strcmp(buf, "floppy")) { |
2051 |
type = IF_FLOPPY; |
2052 |
max_devs = 0;
|
2053 |
} else if (!strcmp(buf, "pflash")) { |
2054 |
type = IF_PFLASH; |
2055 |
max_devs = 0;
|
2056 |
} else if (!strcmp(buf, "mtd")) { |
2057 |
type = IF_MTD; |
2058 |
max_devs = 0;
|
2059 |
} else if (!strcmp(buf, "sd")) { |
2060 |
type = IF_SD; |
2061 |
max_devs = 0;
|
2062 |
} else if (!strcmp(buf, "virtio")) { |
2063 |
type = IF_VIRTIO; |
2064 |
max_devs = 0;
|
2065 |
} else if (!strcmp(buf, "xen")) { |
2066 |
type = IF_XEN; |
2067 |
max_devs = 0;
|
2068 |
} else if (!strcmp(buf, "none")) { |
2069 |
type = IF_NONE; |
2070 |
max_devs = 0;
|
2071 |
} else {
|
2072 |
fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
|
2073 |
return NULL; |
2074 |
} |
2075 |
} |
2076 |
|
2077 |
if (cyls || heads || secs) {
|
2078 |
if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
2079 |
fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
|
2080 |
return NULL; |
2081 |
} |
2082 |
if (heads < 1 || (type == IF_IDE && heads > 16)) { |
2083 |
fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
|
2084 |
return NULL; |
2085 |
} |
2086 |
if (secs < 1 || (type == IF_IDE && secs > 63)) { |
2087 |
fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
|
2088 |
return NULL; |
2089 |
} |
2090 |
} |
2091 |
|
2092 |
if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
2093 |
if (!cyls) {
|
2094 |
fprintf(stderr, |
2095 |
"qemu: '%s' trans must be used with cyls,heads and secs\n",
|
2096 |
buf); |
2097 |
return NULL; |
2098 |
} |
2099 |
if (!strcmp(buf, "none")) |
2100 |
translation = BIOS_ATA_TRANSLATION_NONE; |
2101 |
else if (!strcmp(buf, "lba")) |
2102 |
translation = BIOS_ATA_TRANSLATION_LBA; |
2103 |
else if (!strcmp(buf, "auto")) |
2104 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
2105 |
else {
|
2106 |
fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
|
2107 |
return NULL; |
2108 |
} |
2109 |
} |
2110 |
|
2111 |
if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
2112 |
if (!strcmp(buf, "disk")) { |
2113 |
media = MEDIA_DISK; |
2114 |
} else if (!strcmp(buf, "cdrom")) { |
2115 |
if (cyls || secs || heads) {
|
2116 |
fprintf(stderr, |
2117 |
"qemu: '%s' invalid physical CHS format\n", buf);
|
2118 |
return NULL; |
2119 |
} |
2120 |
media = MEDIA_CDROM; |
2121 |
} else {
|
2122 |
fprintf(stderr, "qemu: '%s' invalid media\n", buf);
|
2123 |
return NULL; |
2124 |
} |
2125 |
} |
2126 |
|
2127 |
if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
2128 |
if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
2129 |
cache = 0;
|
2130 |
else if (!strcmp(buf, "writethrough")) |
2131 |
cache = 1;
|
2132 |
else if (!strcmp(buf, "writeback")) |
2133 |
cache = 2;
|
2134 |
else {
|
2135 |
fprintf(stderr, "qemu: invalid cache option\n");
|
2136 |
return NULL; |
2137 |
} |
2138 |
} |
2139 |
|
2140 |
#ifdef CONFIG_LINUX_AIO
|
2141 |
if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
2142 |
if (!strcmp(buf, "threads")) |
2143 |
aio = 0;
|
2144 |
else if (!strcmp(buf, "native")) |
2145 |
aio = 1;
|
2146 |
else {
|
2147 |
fprintf(stderr, "qemu: invalid aio option\n");
|
2148 |
return NULL; |
2149 |
} |
2150 |
} |
2151 |
#endif
|
2152 |
|
2153 |
if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
2154 |
if (strcmp(buf, "?") == 0) { |
2155 |
fprintf(stderr, "qemu: Supported formats:");
|
2156 |
bdrv_iterate_format(bdrv_format_print, NULL);
|
2157 |
fprintf(stderr, "\n");
|
2158 |
return NULL; |
2159 |
} |
2160 |
drv = bdrv_find_whitelisted_format(buf); |
2161 |
if (!drv) {
|
2162 |
fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
2163 |
return NULL; |
2164 |
} |
2165 |
} |
2166 |
|
2167 |
onerror = BLOCK_ERR_STOP_ENOSPC; |
2168 |
if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
2169 |
if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
|
2170 |
fprintf(stderr, "werror is no supported by this format\n");
|
2171 |
return NULL; |
2172 |
} |
2173 |
if (!strcmp(buf, "ignore")) |
2174 |
onerror = BLOCK_ERR_IGNORE; |
2175 |
else if (!strcmp(buf, "enospc")) |
2176 |
onerror = BLOCK_ERR_STOP_ENOSPC; |
2177 |
else if (!strcmp(buf, "stop")) |
2178 |
onerror = BLOCK_ERR_STOP_ANY; |
2179 |
else if (!strcmp(buf, "report")) |
2180 |
onerror = BLOCK_ERR_REPORT; |
2181 |
else {
|
2182 |
fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
|
2183 |
return NULL; |
2184 |
} |
2185 |
} |
2186 |
|
2187 |
if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
2188 |
if (type != IF_VIRTIO) {
|
2189 |
fprintf(stderr, "addr is not supported\n");
|
2190 |
return NULL; |
2191 |
} |
2192 |
} |
2193 |
|
2194 |
/* compute bus and unit according index */
|
2195 |
|
2196 |
if (index != -1) { |
2197 |
if (bus_id != 0 || unit_id != -1) { |
2198 |
fprintf(stderr, |
2199 |
"qemu: index cannot be used with bus and unit\n");
|
2200 |
return NULL; |
2201 |
} |
2202 |
if (max_devs == 0) |
2203 |
{ |
2204 |
unit_id = index; |
2205 |
bus_id = 0;
|
2206 |
} else {
|
2207 |
unit_id = index % max_devs; |
2208 |
bus_id = index / max_devs; |
2209 |
} |
2210 |
} |
2211 |
|
2212 |
/* if user doesn't specify a unit_id,
|
2213 |
* try to find the first free
|
2214 |
*/
|
2215 |
|
2216 |
if (unit_id == -1) { |
2217 |
unit_id = 0;
|
2218 |
while (drive_get(type, bus_id, unit_id) != NULL) { |
2219 |
unit_id++; |
2220 |
if (max_devs && unit_id >= max_devs) {
|
2221 |
unit_id -= max_devs; |
2222 |
bus_id++; |
2223 |
} |
2224 |
} |
2225 |
} |
2226 |
|
2227 |
/* check unit id */
|
2228 |
|
2229 |
if (max_devs && unit_id >= max_devs) {
|
2230 |
fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
|
2231 |
unit_id, max_devs - 1);
|
2232 |
return NULL; |
2233 |
} |
2234 |
|
2235 |
/*
|
2236 |
* ignore multiple definitions
|
2237 |
*/
|
2238 |
|
2239 |
if (drive_get(type, bus_id, unit_id) != NULL) { |
2240 |
*fatal_error = 0;
|
2241 |
return NULL; |
2242 |
} |
2243 |
|
2244 |
/* init */
|
2245 |
|
2246 |
dinfo = qemu_mallocz(sizeof(*dinfo));
|
2247 |
if ((buf = qemu_opts_id(opts)) != NULL) { |
2248 |
dinfo->id = qemu_strdup(buf); |
2249 |
} else {
|
2250 |
/* no id supplied -> create one */
|
2251 |
dinfo->id = qemu_mallocz(32);
|
2252 |
if (type == IF_IDE || type == IF_SCSI)
|
2253 |
mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
2254 |
if (max_devs)
|
2255 |
snprintf(dinfo->id, 32, "%s%i%s%i", |
2256 |
devname, bus_id, mediastr, unit_id); |
2257 |
else
|
2258 |
snprintf(dinfo->id, 32, "%s%s%i", |
2259 |
devname, mediastr, unit_id); |
2260 |
} |
2261 |
dinfo->bdrv = bdrv_new(dinfo->id); |
2262 |
dinfo->devaddr = devaddr; |
2263 |
dinfo->type = type; |
2264 |
dinfo->bus = bus_id; |
2265 |
dinfo->unit = unit_id; |
2266 |
dinfo->onerror = onerror; |
2267 |
dinfo->opts = opts; |
2268 |
if (serial)
|
2269 |
strncpy(dinfo->serial, serial, sizeof(serial));
|
2270 |
QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
2271 |
|
2272 |
switch(type) {
|
2273 |
case IF_IDE:
|
2274 |
case IF_SCSI:
|
2275 |
case IF_XEN:
|
2276 |
case IF_NONE:
|
2277 |
switch(media) {
|
2278 |
case MEDIA_DISK:
|
2279 |
if (cyls != 0) { |
2280 |
bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
2281 |
bdrv_set_translation_hint(dinfo->bdrv, translation); |
2282 |
} |
2283 |
break;
|
2284 |
case MEDIA_CDROM:
|
2285 |
bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
2286 |
break;
|
2287 |
} |
2288 |
break;
|
2289 |
case IF_SD:
|
2290 |
/* FIXME: This isn't really a floppy, but it's a reasonable
|
2291 |
approximation. */
|
2292 |
case IF_FLOPPY:
|
2293 |
bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
2294 |
break;
|
2295 |
case IF_PFLASH:
|
2296 |
case IF_MTD:
|
2297 |
break;
|
2298 |
case IF_VIRTIO:
|
2299 |
/* add virtio block device */
|
2300 |
opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
2301 |
qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
2302 |
qemu_opt_set(opts, "drive", dinfo->id);
|
2303 |
if (devaddr)
|
2304 |
qemu_opt_set(opts, "addr", devaddr);
|
2305 |
break;
|
2306 |
case IF_COUNT:
|
2307 |
abort(); |
2308 |
} |
2309 |
if (!file) {
|
2310 |
*fatal_error = 0;
|
2311 |
return NULL; |
2312 |
} |
2313 |
bdrv_flags = 0;
|
2314 |
if (snapshot) {
|
2315 |
bdrv_flags |= BDRV_O_SNAPSHOT; |
2316 |
cache = 2; /* always use write-back with snapshot */ |
2317 |
} |
2318 |
if (cache == 0) /* no caching */ |
2319 |
bdrv_flags |= BDRV_O_NOCACHE; |
2320 |
else if (cache == 2) /* write-back */ |
2321 |
bdrv_flags |= BDRV_O_CACHE_WB; |
2322 |
|
2323 |
if (aio == 1) { |
2324 |
bdrv_flags |= BDRV_O_NATIVE_AIO; |
2325 |
} else {
|
2326 |
bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
2327 |
} |
2328 |
|
2329 |
if (ro == 1) { |
2330 |
if (type == IF_IDE) {
|
2331 |
fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
|
2332 |
return NULL; |
2333 |
} |
2334 |
(void)bdrv_set_read_only(dinfo->bdrv, 1); |
2335 |
} |
2336 |
|
2337 |
if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
2338 |
fprintf(stderr, "qemu: could not open disk image %s: %s\n",
|
2339 |
file, strerror(errno)); |
2340 |
return NULL; |
2341 |
} |
2342 |
|
2343 |
if (bdrv_key_required(dinfo->bdrv))
|
2344 |
autostart = 0;
|
2345 |
*fatal_error = 0;
|
2346 |
return dinfo;
|
2347 |
} |
2348 |
|
2349 |
static int drive_init_func(QemuOpts *opts, void *opaque) |
2350 |
{ |
2351 |
QEMUMachine *machine = opaque; |
2352 |
int fatal_error = 0; |
2353 |
|
2354 |
if (drive_init(opts, machine, &fatal_error) == NULL) { |
2355 |
if (fatal_error)
|
2356 |
return 1; |
2357 |
} |
2358 |
return 0; |
2359 |
} |
2360 |
|
2361 |
static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
2362 |
{ |
2363 |
if (NULL == qemu_opt_get(opts, "snapshot")) { |
2364 |
qemu_opt_set(opts, "snapshot", "on"); |
2365 |
} |
2366 |
return 0; |
2367 |
} |
2368 |
|
2369 |
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
2370 |
{ |
2371 |
boot_set_handler = func; |
2372 |
boot_set_opaque = opaque; |
2373 |
} |
2374 |
|
2375 |
int qemu_boot_set(const char *boot_devices) |
2376 |
{ |
2377 |
if (!boot_set_handler) {
|
2378 |
return -EINVAL;
|
2379 |
} |
2380 |
return boot_set_handler(boot_set_opaque, boot_devices);
|
2381 |
} |
2382 |
|
2383 |
static int parse_bootdevices(char *devices) |
2384 |
{ |
2385 |
/* We just do some generic consistency checks */
|
2386 |
const char *p; |
2387 |
int bitmap = 0; |
2388 |
|
2389 |
for (p = devices; *p != '\0'; p++) { |
2390 |
/* Allowed boot devices are:
|
2391 |
* a-b: floppy disk drives
|
2392 |
* c-f: IDE disk drives
|
2393 |
* g-m: machine implementation dependant drives
|
2394 |
* n-p: network devices
|
2395 |
* It's up to each machine implementation to check if the given boot
|
2396 |
* devices match the actual hardware implementation and firmware
|
2397 |
* features.
|
2398 |
*/
|
2399 |
if (*p < 'a' || *p > 'p') { |
2400 |
fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
2401 |
exit(1);
|
2402 |
} |
2403 |
if (bitmap & (1 << (*p - 'a'))) { |
2404 |
fprintf(stderr, "Boot device '%c' was given twice\n", *p);
|
2405 |
exit(1);
|
2406 |
} |
2407 |
bitmap |= 1 << (*p - 'a'); |
2408 |
} |
2409 |
return bitmap;
|
2410 |
} |
2411 |
|
2412 |
static void restore_boot_devices(void *opaque) |
2413 |
{ |
2414 |
char *standard_boot_devices = opaque;
|
2415 |
|
2416 |
qemu_boot_set(standard_boot_devices); |
2417 |
|
2418 |
qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
2419 |
qemu_free(standard_boot_devices); |
2420 |
} |
2421 |
|
2422 |
static void numa_add(const char *optarg) |
2423 |
{ |
2424 |
char option[128]; |
2425 |
char *endptr;
|
2426 |
unsigned long long value, endvalue; |
2427 |
int nodenr;
|
2428 |
|
2429 |
optarg = get_opt_name(option, 128, optarg, ',') + 1; |
2430 |
if (!strcmp(option, "node")) { |
2431 |
if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
2432 |
nodenr = nb_numa_nodes; |
2433 |
} else {
|
2434 |
nodenr = strtoull(option, NULL, 10); |
2435 |
} |
2436 |
|
2437 |
if (get_param_value(option, 128, "mem", optarg) == 0) { |
2438 |
node_mem[nodenr] = 0;
|
2439 |
} else {
|
2440 |
value = strtoull(option, &endptr, 0);
|
2441 |
switch (*endptr) {
|
2442 |
case 0: case 'M': case 'm': |
2443 |
value <<= 20;
|
2444 |
break;
|
2445 |
case 'G': case 'g': |
2446 |
value <<= 30;
|
2447 |
break;
|
2448 |
} |
2449 |
node_mem[nodenr] = value; |
2450 |
} |
2451 |
if (get_param_value(option, 128, "cpus", optarg) == 0) { |
2452 |
node_cpumask[nodenr] = 0;
|
2453 |
} else {
|
2454 |
value = strtoull(option, &endptr, 10);
|
2455 |
if (value >= 64) { |
2456 |
value = 63;
|
2457 |
fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
|
2458 |
} else {
|
2459 |
if (*endptr == '-') { |
2460 |
endvalue = strtoull(endptr+1, &endptr, 10); |
2461 |
if (endvalue >= 63) { |
2462 |
endvalue = 62;
|
2463 |
fprintf(stderr, |
2464 |
"only 63 CPUs in NUMA mode supported.\n");
|
2465 |
} |
2466 |
value = (1 << (endvalue + 1)) - (1 << value); |
2467 |
} else {
|
2468 |
value = 1 << value;
|
2469 |
} |
2470 |
} |
2471 |
node_cpumask[nodenr] = value; |
2472 |
} |
2473 |
nb_numa_nodes++; |
2474 |
} |
2475 |
return;
|
2476 |
} |
2477 |
|
2478 |
static void smp_parse(const char *optarg) |
2479 |
{ |
2480 |
int smp, sockets = 0, threads = 0, cores = 0; |
2481 |
char *endptr;
|
2482 |
char option[128]; |
2483 |
|
2484 |
smp = strtoul(optarg, &endptr, 10);
|
2485 |
if (endptr != optarg) {
|
2486 |
if (*endptr == ',') { |
2487 |
endptr++; |
2488 |
} |
2489 |
} |
2490 |
if (get_param_value(option, 128, "sockets", endptr) != 0) |
2491 |
sockets = strtoull(option, NULL, 10); |
2492 |
if (get_param_value(option, 128, "cores", endptr) != 0) |
2493 |
cores = strtoull(option, NULL, 10); |
2494 |
if (get_param_value(option, 128, "threads", endptr) != 0) |
2495 |
threads = strtoull(option, NULL, 10); |
2496 |
if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
2497 |
max_cpus = strtoull(option, NULL, 10); |
2498 |
|
2499 |
/* compute missing values, prefer sockets over cores over threads */
|
2500 |
if (smp == 0 || sockets == 0) { |
2501 |
sockets = sockets > 0 ? sockets : 1; |
2502 |
cores = cores > 0 ? cores : 1; |
2503 |
threads = threads > 0 ? threads : 1; |
2504 |
if (smp == 0) { |
2505 |
smp = cores * threads * sockets; |
2506 |
} else {
|
2507 |
sockets = smp / (cores * threads); |
2508 |
} |
2509 |
} else {
|
2510 |
if (cores == 0) { |
2511 |
threads = threads > 0 ? threads : 1; |
2512 |
cores = smp / (sockets * threads); |
2513 |
} else {
|
2514 |
if (sockets == 0) { |
2515 |
sockets = smp / (cores * threads); |
2516 |
} else {
|
2517 |
threads = smp / (cores * sockets); |
2518 |
} |
2519 |
} |
2520 |
} |
2521 |
smp_cpus = smp; |
2522 |
smp_cores = cores > 0 ? cores : 1; |
2523 |
smp_threads = threads > 0 ? threads : 1; |
2524 |
if (max_cpus == 0) |
2525 |
max_cpus = smp_cpus; |
2526 |
} |
2527 |
|
2528 |
/***********************************************************/
|
2529 |
/* USB devices */
|
2530 |
|
2531 |
static int usb_device_add(const char *devname, int is_hotplug) |
2532 |
{ |
2533 |
const char *p; |
2534 |
USBDevice *dev = NULL;
|
2535 |
|
2536 |
if (!usb_enabled)
|
2537 |
return -1; |
2538 |
|
2539 |
/* drivers with .usbdevice_name entry in USBDeviceInfo */
|
2540 |
dev = usbdevice_create(devname); |
2541 |
if (dev)
|
2542 |
goto done;
|
2543 |
|
2544 |
/* the other ones */
|
2545 |
if (strstart(devname, "host:", &p)) { |
2546 |
dev = usb_host_device_open(p); |
2547 |
} else if (strstart(devname, "net:", &p)) { |
2548 |
QemuOpts *opts; |
2549 |
int idx;
|
2550 |
|
2551 |
opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
|
2552 |
if (!opts) {
|
2553 |
return -1; |
2554 |
} |
2555 |
|
2556 |
qemu_opt_set(opts, "type", "nic"); |
2557 |
qemu_opt_set(opts, "model", "usb"); |
2558 |
|
2559 |
idx = net_client_init(NULL, opts, 0); |
2560 |
if (idx == -1) { |
2561 |
return -1; |
2562 |
} |
2563 |
|
2564 |
dev = usb_net_init(&nd_table[idx]); |
2565 |
} else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
2566 |
dev = usb_bt_init(devname[2] ? hci_init(p) :
|
2567 |
bt_new_hci(qemu_find_bt_vlan(0)));
|
2568 |
} else {
|
2569 |
return -1; |
2570 |
} |
2571 |
if (!dev)
|
2572 |
return -1; |
2573 |
|
2574 |
done:
|
2575 |
return 0; |
2576 |
} |
2577 |
|
2578 |
static int usb_device_del(const char *devname) |
2579 |
{ |
2580 |
int bus_num, addr;
|
2581 |
const char *p; |
2582 |
|
2583 |
if (strstart(devname, "host:", &p)) |
2584 |
return usb_host_device_close(p);
|
2585 |
|
2586 |
if (!usb_enabled)
|
2587 |
return -1; |
2588 |
|
2589 |
p = strchr(devname, '.');
|
2590 |
if (!p)
|
2591 |
return -1; |
2592 |
bus_num = strtoul(devname, NULL, 0); |
2593 |
addr = strtoul(p + 1, NULL, 0); |
2594 |
|
2595 |
return usb_device_delete_addr(bus_num, addr);
|
2596 |
} |
2597 |
|
2598 |
static int usb_parse(const char *cmdline) |
2599 |
{ |
2600 |
return usb_device_add(cmdline, 0); |
2601 |
} |
2602 |
|
2603 |
void do_usb_add(Monitor *mon, const QDict *qdict) |
2604 |
{ |
2605 |
usb_device_add(qdict_get_str(qdict, "devname"), 1); |
2606 |
} |
2607 |
|
2608 |
void do_usb_del(Monitor *mon, const QDict *qdict) |
2609 |
{ |
2610 |
usb_device_del(qdict_get_str(qdict, "devname"));
|
2611 |
} |
2612 |
|
2613 |
/***********************************************************/
|
2614 |
/* PCMCIA/Cardbus */
|
2615 |
|
2616 |
static struct pcmcia_socket_entry_s { |
2617 |
PCMCIASocket *socket; |
2618 |
struct pcmcia_socket_entry_s *next;
|
2619 |
} *pcmcia_sockets = 0;
|
2620 |
|
2621 |
void pcmcia_socket_register(PCMCIASocket *socket)
|
2622 |
{ |
2623 |
struct pcmcia_socket_entry_s *entry;
|
2624 |
|
2625 |
entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
2626 |
entry->socket = socket; |
2627 |
entry->next = pcmcia_sockets; |
2628 |
pcmcia_sockets = entry; |
2629 |
} |
2630 |
|
2631 |
void pcmcia_socket_unregister(PCMCIASocket *socket)
|
2632 |
{ |
2633 |
struct pcmcia_socket_entry_s *entry, **ptr;
|
2634 |
|
2635 |
ptr = &pcmcia_sockets; |
2636 |
for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
2637 |
if (entry->socket == socket) {
|
2638 |
*ptr = entry->next; |
2639 |
qemu_free(entry); |
2640 |
} |
2641 |
} |
2642 |
|
2643 |
void pcmcia_info(Monitor *mon)
|
2644 |
{ |
2645 |
struct pcmcia_socket_entry_s *iter;
|
2646 |
|
2647 |
if (!pcmcia_sockets)
|
2648 |
monitor_printf(mon, "No PCMCIA sockets\n");
|
2649 |
|
2650 |
for (iter = pcmcia_sockets; iter; iter = iter->next)
|
2651 |
monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
|
2652 |
iter->socket->attached ? iter->socket->card_string : |
2653 |
"Empty");
|
2654 |
} |
2655 |
|
2656 |
/***********************************************************/
|
2657 |
/* register display */
|
2658 |
|
2659 |
struct DisplayAllocator default_allocator = {
|
2660 |
defaultallocator_create_displaysurface, |
2661 |
defaultallocator_resize_displaysurface, |
2662 |
defaultallocator_free_displaysurface |
2663 |
}; |
2664 |
|
2665 |
void register_displaystate(DisplayState *ds)
|
2666 |
{ |
2667 |
DisplayState **s; |
2668 |
s = &display_state; |
2669 |
while (*s != NULL) |
2670 |
s = &(*s)->next; |
2671 |
ds->next = NULL;
|
2672 |
*s = ds; |
2673 |
} |
2674 |
|
2675 |
DisplayState *get_displaystate(void)
|
2676 |
{ |
2677 |
return display_state;
|
2678 |
} |
2679 |
|
2680 |
DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
2681 |
{ |
2682 |
if(ds->allocator == &default_allocator) ds->allocator = da;
|
2683 |
return ds->allocator;
|
2684 |
} |
2685 |
|
2686 |
/* dumb display */
|
2687 |
|
2688 |
static void dumb_display_init(void) |
2689 |
{ |
2690 |
DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
|
2691 |
ds->allocator = &default_allocator; |
2692 |
ds->surface = qemu_create_displaysurface(ds, 640, 480); |
2693 |
register_displaystate(ds); |
2694 |
} |
2695 |
|
2696 |
/***********************************************************/
|
2697 |
/* I/O handling */
|
2698 |
|
2699 |
typedef struct IOHandlerRecord { |
2700 |
int fd;
|
2701 |
IOCanRWHandler *fd_read_poll; |
2702 |
IOHandler *fd_read; |
2703 |
IOHandler *fd_write; |
2704 |
int deleted;
|
2705 |
void *opaque;
|
2706 |
/* temporary data */
|
2707 |
struct pollfd *ufd;
|
2708 |
struct IOHandlerRecord *next;
|
2709 |
} IOHandlerRecord; |
2710 |
|
2711 |
static IOHandlerRecord *first_io_handler;
|
2712 |
|
2713 |
/* XXX: fd_read_poll should be suppressed, but an API change is
|
2714 |
necessary in the character devices to suppress fd_can_read(). */
|
2715 |
int qemu_set_fd_handler2(int fd, |
2716 |
IOCanRWHandler *fd_read_poll, |
2717 |
IOHandler *fd_read, |
2718 |
IOHandler *fd_write, |
2719 |
void *opaque)
|
2720 |
{ |
2721 |
IOHandlerRecord **pioh, *ioh; |
2722 |
|
2723 |
if (!fd_read && !fd_write) {
|
2724 |
pioh = &first_io_handler; |
2725 |
for(;;) {
|
2726 |
ioh = *pioh; |
2727 |
if (ioh == NULL) |
2728 |
break;
|
2729 |
if (ioh->fd == fd) {
|
2730 |
ioh->deleted = 1;
|
2731 |
break;
|
2732 |
} |
2733 |
pioh = &ioh->next; |
2734 |
} |
2735 |
} else {
|
2736 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
2737 |
if (ioh->fd == fd)
|
2738 |
goto found;
|
2739 |
} |
2740 |
ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
2741 |
ioh->next = first_io_handler; |
2742 |
first_io_handler = ioh; |
2743 |
found:
|
2744 |
ioh->fd = fd; |
2745 |
ioh->fd_read_poll = fd_read_poll; |
2746 |
ioh->fd_read = fd_read; |
2747 |
ioh->fd_write = fd_write; |
2748 |
ioh->opaque = opaque; |
2749 |
ioh->deleted = 0;
|
2750 |
} |
2751 |
return 0; |
2752 |
} |
2753 |
|
2754 |
int qemu_set_fd_handler(int fd, |
2755 |
IOHandler *fd_read, |
2756 |
IOHandler *fd_write, |
2757 |
void *opaque)
|
2758 |
{ |
2759 |
return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
2760 |
} |
2761 |
|
2762 |
#ifdef _WIN32
|
2763 |
/***********************************************************/
|
2764 |
/* Polling handling */
|
2765 |
|
2766 |
typedef struct PollingEntry { |
2767 |
PollingFunc *func; |
2768 |
void *opaque;
|
2769 |
struct PollingEntry *next;
|
2770 |
} PollingEntry; |
2771 |
|
2772 |
static PollingEntry *first_polling_entry;
|
2773 |
|
2774 |
int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
2775 |
{ |
2776 |
PollingEntry **ppe, *pe; |
2777 |
pe = qemu_mallocz(sizeof(PollingEntry));
|
2778 |
pe->func = func; |
2779 |
pe->opaque = opaque; |
2780 |
for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
2781 |
*ppe = pe; |
2782 |
return 0; |
2783 |
} |
2784 |
|
2785 |
void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
2786 |
{ |
2787 |
PollingEntry **ppe, *pe; |
2788 |
for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
2789 |
pe = *ppe; |
2790 |
if (pe->func == func && pe->opaque == opaque) {
|
2791 |
*ppe = pe->next; |
2792 |
qemu_free(pe); |
2793 |
break;
|
2794 |
} |
2795 |
} |
2796 |
} |
2797 |
|
2798 |
/***********************************************************/
|
2799 |
/* Wait objects support */
|
2800 |
typedef struct WaitObjects { |
2801 |
int num;
|
2802 |
HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
2803 |
WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
2804 |
void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
2805 |
} WaitObjects; |
2806 |
|
2807 |
static WaitObjects wait_objects = {0}; |
2808 |
|
2809 |
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
2810 |
{ |
2811 |
WaitObjects *w = &wait_objects; |
2812 |
|
2813 |
if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
2814 |
return -1; |
2815 |
w->events[w->num] = handle; |
2816 |
w->func[w->num] = func; |
2817 |
w->opaque[w->num] = opaque; |
2818 |
w->num++; |
2819 |
return 0; |
2820 |
} |
2821 |
|
2822 |
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
2823 |
{ |
2824 |
int i, found;
|
2825 |
WaitObjects *w = &wait_objects; |
2826 |
|
2827 |
found = 0;
|
2828 |
for (i = 0; i < w->num; i++) { |
2829 |
if (w->events[i] == handle)
|
2830 |
found = 1;
|
2831 |
if (found) {
|
2832 |
w->events[i] = w->events[i + 1];
|
2833 |
w->func[i] = w->func[i + 1];
|
2834 |
w->opaque[i] = w->opaque[i + 1];
|
2835 |
} |
2836 |
} |
2837 |
if (found)
|
2838 |
w->num--; |
2839 |
} |
2840 |
#endif
|
2841 |
|
2842 |
/***********************************************************/
|
2843 |
/* ram save/restore */
|
2844 |
|
2845 |
#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
2846 |
#define RAM_SAVE_FLAG_COMPRESS 0x02 |
2847 |
#define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
2848 |
#define RAM_SAVE_FLAG_PAGE 0x08 |
2849 |
#define RAM_SAVE_FLAG_EOS 0x10 |
2850 |
|
2851 |
static int is_dup_page(uint8_t *page, uint8_t ch) |
2852 |
{ |
2853 |
uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
2854 |
uint32_t *array = (uint32_t *)page; |
2855 |
int i;
|
2856 |
|
2857 |
for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
2858 |
if (array[i] != val)
|
2859 |
return 0; |
2860 |
} |
2861 |
|
2862 |
return 1; |
2863 |
} |
2864 |
|
2865 |
static int ram_save_block(QEMUFile *f) |
2866 |
{ |
2867 |
static ram_addr_t current_addr = 0; |
2868 |
ram_addr_t saved_addr = current_addr; |
2869 |
ram_addr_t addr = 0;
|
2870 |
int found = 0; |
2871 |
|
2872 |
while (addr < last_ram_offset) {
|
2873 |
if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
|
2874 |
uint8_t *p; |
2875 |
|
2876 |
cpu_physical_memory_reset_dirty(current_addr, |
2877 |
current_addr + TARGET_PAGE_SIZE, |
2878 |
MIGRATION_DIRTY_FLAG); |
2879 |
|
2880 |
p = qemu_get_ram_ptr(current_addr); |
2881 |
|
2882 |
if (is_dup_page(p, *p)) {
|
2883 |
qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
2884 |
qemu_put_byte(f, *p); |
2885 |
} else {
|
2886 |
qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
2887 |
qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
2888 |
} |
2889 |
|
2890 |
found = 1;
|
2891 |
break;
|
2892 |
} |
2893 |
addr += TARGET_PAGE_SIZE; |
2894 |
current_addr = (saved_addr + addr) % last_ram_offset; |
2895 |
} |
2896 |
|
2897 |
return found;
|
2898 |
} |
2899 |
|
2900 |
static uint64_t bytes_transferred = 0; |
2901 |
|
2902 |
static ram_addr_t ram_save_remaining(void) |
2903 |
{ |
2904 |
ram_addr_t addr; |
2905 |
ram_addr_t count = 0;
|
2906 |
|
2907 |
for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
2908 |
if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
|
2909 |
count++; |
2910 |
} |
2911 |
|
2912 |
return count;
|
2913 |
} |
2914 |
|
2915 |
uint64_t ram_bytes_remaining(void)
|
2916 |
{ |
2917 |
return ram_save_remaining() * TARGET_PAGE_SIZE;
|
2918 |
} |
2919 |
|
2920 |
uint64_t ram_bytes_transferred(void)
|
2921 |
{ |
2922 |
return bytes_transferred;
|
2923 |
} |
2924 |
|
2925 |
uint64_t ram_bytes_total(void)
|
2926 |
{ |
2927 |
return last_ram_offset;
|
2928 |
} |
2929 |
|
2930 |
static int ram_save_live(QEMUFile *f, int stage, void *opaque) |
2931 |
{ |
2932 |
ram_addr_t addr; |
2933 |
uint64_t bytes_transferred_last; |
2934 |
double bwidth = 0; |
2935 |
uint64_t expected_time = 0;
|
2936 |
|
2937 |
if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
2938 |
qemu_file_set_error(f); |
2939 |
return 0; |
2940 |
} |
2941 |
|
2942 |
if (stage == 1) { |
2943 |
/* Make sure all dirty bits are set */
|
2944 |
for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
2945 |
if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
|
2946 |
cpu_physical_memory_set_dirty(addr); |
2947 |
} |
2948 |
|
2949 |
/* Enable dirty memory tracking */
|
2950 |
cpu_physical_memory_set_dirty_tracking(1);
|
2951 |
|
2952 |
qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
2953 |
} |
2954 |
|
2955 |
bytes_transferred_last = bytes_transferred; |
2956 |
bwidth = get_clock(); |
2957 |
|
2958 |
while (!qemu_file_rate_limit(f)) {
|
2959 |
int ret;
|
2960 |
|
2961 |
ret = ram_save_block(f); |
2962 |
bytes_transferred += ret * TARGET_PAGE_SIZE; |
2963 |
if (ret == 0) /* no more blocks */ |
2964 |
break;
|
2965 |
} |
2966 |
|
2967 |
bwidth = get_clock() - bwidth; |
2968 |
bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
2969 |
|
2970 |
/* if we haven't transferred anything this round, force expected_time to a
|
2971 |
* a very high value, but without crashing */
|
2972 |
if (bwidth == 0) |
2973 |
bwidth = 0.000001; |
2974 |
|
2975 |
/* try transferring iterative blocks of memory */
|
2976 |
if (stage == 3) { |
2977 |
/* flush all remaining blocks regardless of rate limiting */
|
2978 |
while (ram_save_block(f) != 0) { |
2979 |
bytes_transferred += TARGET_PAGE_SIZE; |
2980 |
} |
2981 |
cpu_physical_memory_set_dirty_tracking(0);
|
2982 |
} |
2983 |
|
2984 |
qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
2985 |
|
2986 |
expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
2987 |
|
2988 |
return (stage == 2) && (expected_time <= migrate_max_downtime()); |
2989 |
} |
2990 |
|
2991 |
static int ram_load(QEMUFile *f, void *opaque, int version_id) |
2992 |
{ |
2993 |
ram_addr_t addr; |
2994 |
int flags;
|
2995 |
|
2996 |
if (version_id != 3) |
2997 |
return -EINVAL;
|
2998 |
|
2999 |
do {
|
3000 |
addr = qemu_get_be64(f); |
3001 |
|
3002 |
flags = addr & ~TARGET_PAGE_MASK; |
3003 |
addr &= TARGET_PAGE_MASK; |
3004 |
|
3005 |
if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
|
3006 |
if (addr != last_ram_offset)
|
3007 |
return -EINVAL;
|
3008 |
} |
3009 |
|
3010 |
if (flags & RAM_SAVE_FLAG_COMPRESS) {
|
3011 |
uint8_t ch = qemu_get_byte(f); |
3012 |
memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
3013 |
#ifndef _WIN32
|
3014 |
if (ch == 0 && |
3015 |
(!kvm_enabled() || kvm_has_sync_mmu())) { |
3016 |
madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
3017 |
} |
3018 |
#endif
|
3019 |
} else if (flags & RAM_SAVE_FLAG_PAGE) { |
3020 |
qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
3021 |
} |
3022 |
if (qemu_file_has_error(f)) {
|
3023 |
return -EIO;
|
3024 |
} |
3025 |
} while (!(flags & RAM_SAVE_FLAG_EOS));
|
3026 |
|
3027 |
return 0; |
3028 |
} |
3029 |
|
3030 |
void qemu_service_io(void) |
3031 |
{ |
3032 |
qemu_notify_event(); |
3033 |
} |
3034 |
|
3035 |
/***********************************************************/
|
3036 |
/* machine registration */
|
3037 |
|
3038 |
static QEMUMachine *first_machine = NULL; |
3039 |
QEMUMachine *current_machine = NULL;
|
3040 |
|
3041 |
int qemu_register_machine(QEMUMachine *m)
|
3042 |
{ |
3043 |
QEMUMachine **pm; |
3044 |
pm = &first_machine; |
3045 |
while (*pm != NULL) |
3046 |
pm = &(*pm)->next; |
3047 |
m->next = NULL;
|
3048 |
*pm = m; |
3049 |
return 0; |
3050 |
} |
3051 |
|
3052 |
static QEMUMachine *find_machine(const char *name) |
3053 |
{ |
3054 |
QEMUMachine *m; |
3055 |
|
3056 |
for(m = first_machine; m != NULL; m = m->next) { |
3057 |
if (!strcmp(m->name, name))
|
3058 |
return m;
|
3059 |
if (m->alias && !strcmp(m->alias, name))
|
3060 |
return m;
|
3061 |
} |
3062 |
return NULL; |
3063 |
} |
3064 |
|
3065 |
static QEMUMachine *find_default_machine(void) |
3066 |
{ |
3067 |
QEMUMachine *m; |
3068 |
|
3069 |
for(m = first_machine; m != NULL; m = m->next) { |
3070 |
if (m->is_default) {
|
3071 |
return m;
|
3072 |
} |
3073 |
} |
3074 |
return NULL; |
3075 |
} |
3076 |
|
3077 |
/***********************************************************/
|
3078 |
/* main execution loop */
|
3079 |
|
3080 |
static void gui_update(void *opaque) |
3081 |
{ |
3082 |
uint64_t interval = GUI_REFRESH_INTERVAL; |
3083 |
DisplayState *ds = opaque; |
3084 |
DisplayChangeListener *dcl = ds->listeners; |
3085 |
|
3086 |
dpy_refresh(ds); |
3087 |
|
3088 |
while (dcl != NULL) { |
3089 |
if (dcl->gui_timer_interval &&
|
3090 |
dcl->gui_timer_interval < interval) |
3091 |
interval = dcl->gui_timer_interval; |
3092 |
dcl = dcl->next; |
3093 |
} |
3094 |
qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
3095 |
} |
3096 |
|
3097 |
static void nographic_update(void *opaque) |
3098 |
{ |
3099 |
uint64_t interval = GUI_REFRESH_INTERVAL; |
3100 |
|
3101 |
qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
3102 |
} |
3103 |
|
3104 |
struct vm_change_state_entry {
|
3105 |
VMChangeStateHandler *cb; |
3106 |
void *opaque;
|
3107 |
QLIST_ENTRY (vm_change_state_entry) entries; |
3108 |
}; |
3109 |
|
3110 |
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
3111 |
|
3112 |
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
3113 |
void *opaque)
|
3114 |
{ |
3115 |
VMChangeStateEntry *e; |
3116 |
|
3117 |
e = qemu_mallocz(sizeof (*e));
|
3118 |
|
3119 |
e->cb = cb; |
3120 |
e->opaque = opaque; |
3121 |
QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
3122 |
return e;
|
3123 |
} |
3124 |
|
3125 |
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
3126 |
{ |
3127 |
QLIST_REMOVE (e, entries); |
3128 |
qemu_free (e); |
3129 |
} |
3130 |
|
3131 |
static void vm_state_notify(int running, int reason) |
3132 |
{ |
3133 |
VMChangeStateEntry *e; |
3134 |
|
3135 |
for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
3136 |
e->cb(e->opaque, running, reason); |
3137 |
} |
3138 |
} |
3139 |
|
3140 |
static void resume_all_vcpus(void); |
3141 |
static void pause_all_vcpus(void); |
3142 |
|
3143 |
void vm_start(void) |
3144 |
{ |
3145 |
if (!vm_running) {
|
3146 |
cpu_enable_ticks(); |
3147 |
vm_running = 1;
|
3148 |
vm_state_notify(1, 0); |
3149 |
qemu_rearm_alarm_timer(alarm_timer); |
3150 |
resume_all_vcpus(); |
3151 |
} |
3152 |
} |
3153 |
|
3154 |
/* reset/shutdown handler */
|
3155 |
|
3156 |
typedef struct QEMUResetEntry { |
3157 |
QTAILQ_ENTRY(QEMUResetEntry) entry; |
3158 |
QEMUResetHandler *func; |
3159 |
void *opaque;
|
3160 |
} QEMUResetEntry; |
3161 |
|
3162 |
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
|
3163 |
QTAILQ_HEAD_INITIALIZER(reset_handlers); |
3164 |
static int reset_requested; |
3165 |
static int shutdown_requested; |
3166 |
static int powerdown_requested; |
3167 |
static int debug_requested; |
3168 |
static int vmstop_requested; |
3169 |
|
3170 |
int qemu_shutdown_requested(void) |
3171 |
{ |
3172 |
int r = shutdown_requested;
|
3173 |
shutdown_requested = 0;
|
3174 |
return r;
|
3175 |
} |
3176 |
|
3177 |
int qemu_reset_requested(void) |
3178 |
{ |
3179 |
int r = reset_requested;
|
3180 |
reset_requested = 0;
|
3181 |
return r;
|
3182 |
} |
3183 |
|
3184 |
int qemu_powerdown_requested(void) |
3185 |
{ |
3186 |
int r = powerdown_requested;
|
3187 |
powerdown_requested = 0;
|
3188 |
return r;
|
3189 |
} |
3190 |
|
3191 |
static int qemu_debug_requested(void) |
3192 |
{ |
3193 |
int r = debug_requested;
|
3194 |
debug_requested = 0;
|
3195 |
return r;
|
3196 |
} |
3197 |
|
3198 |
static int qemu_vmstop_requested(void) |
3199 |
{ |
3200 |
int r = vmstop_requested;
|
3201 |
vmstop_requested = 0;
|
3202 |
return r;
|
3203 |
} |
3204 |
|
3205 |
static void do_vm_stop(int reason) |
3206 |
{ |
3207 |
if (vm_running) {
|
3208 |
cpu_disable_ticks(); |
3209 |
vm_running = 0;
|
3210 |
pause_all_vcpus(); |
3211 |
vm_state_notify(0, reason);
|
3212 |
} |
3213 |
} |
3214 |
|
3215 |
void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
3216 |
{ |
3217 |
QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
|
3218 |
|
3219 |
re->func = func; |
3220 |
re->opaque = opaque; |
3221 |
QTAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
3222 |
} |
3223 |
|
3224 |
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
3225 |
{ |
3226 |
QEMUResetEntry *re; |
3227 |
|
3228 |
QTAILQ_FOREACH(re, &reset_handlers, entry) { |
3229 |
if (re->func == func && re->opaque == opaque) {
|
3230 |
QTAILQ_REMOVE(&reset_handlers, re, entry); |
3231 |
qemu_free(re); |
3232 |
return;
|
3233 |
} |
3234 |
} |
3235 |
} |
3236 |
|
3237 |
void qemu_system_reset(void) |
3238 |
{ |
3239 |
QEMUResetEntry *re, *nre; |
3240 |
|
3241 |
/* reset all devices */
|
3242 |
QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
3243 |
re->func(re->opaque); |
3244 |
} |
3245 |
} |
3246 |
|
3247 |
void qemu_system_reset_request(void) |
3248 |
{ |
3249 |
if (no_reboot) {
|
3250 |
shutdown_requested = 1;
|
3251 |
} else {
|
3252 |
reset_requested = 1;
|
3253 |
} |
3254 |
qemu_notify_event(); |
3255 |
} |
3256 |
|
3257 |
void qemu_system_shutdown_request(void) |
3258 |
{ |
3259 |
shutdown_requested = 1;
|
3260 |
qemu_notify_event(); |
3261 |
} |
3262 |
|
3263 |
void qemu_system_powerdown_request(void) |
3264 |
{ |
3265 |
powerdown_requested = 1;
|
3266 |
qemu_notify_event(); |
3267 |
} |
3268 |
|
3269 |
#ifdef CONFIG_IOTHREAD
|
3270 |
static void qemu_system_vmstop_request(int reason) |
3271 |
{ |
3272 |
vmstop_requested = reason; |
3273 |
qemu_notify_event(); |
3274 |
} |
3275 |
#endif
|
3276 |
|
3277 |
#ifndef _WIN32
|
3278 |
static int io_thread_fd = -1; |
3279 |
|
3280 |
static void qemu_event_increment(void) |
3281 |
{ |
3282 |
static const char byte = 0; |
3283 |
|
3284 |
if (io_thread_fd == -1) |
3285 |
return;
|
3286 |
|
3287 |
write(io_thread_fd, &byte, sizeof(byte));
|
3288 |
} |
3289 |
|
3290 |
static void qemu_event_read(void *opaque) |
3291 |
{ |
3292 |
int fd = (unsigned long)opaque; |
3293 |
ssize_t len; |
3294 |
|
3295 |
/* Drain the notify pipe */
|
3296 |
do {
|
3297 |
char buffer[512]; |
3298 |
len = read(fd, buffer, sizeof(buffer));
|
3299 |
} while ((len == -1 && errno == EINTR) || len > 0); |
3300 |
} |
3301 |
|
3302 |
static int qemu_event_init(void) |
3303 |
{ |
3304 |
int err;
|
3305 |
int fds[2]; |
3306 |
|
3307 |
err = pipe(fds); |
3308 |
if (err == -1) |
3309 |
return -errno;
|
3310 |
|
3311 |
err = fcntl_setfl(fds[0], O_NONBLOCK);
|
3312 |
if (err < 0) |
3313 |
goto fail;
|
3314 |
|
3315 |
err = fcntl_setfl(fds[1], O_NONBLOCK);
|
3316 |
if (err < 0) |
3317 |
goto fail;
|
3318 |
|
3319 |
qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
3320 |
(void *)(unsigned long)fds[0]); |
3321 |
|
3322 |
io_thread_fd = fds[1];
|
3323 |
return 0; |
3324 |
|
3325 |
fail:
|
3326 |
close(fds[0]);
|
3327 |
close(fds[1]);
|
3328 |
return err;
|
3329 |
} |
3330 |
#else
|
3331 |
HANDLE qemu_event_handle; |
3332 |
|
3333 |
static void dummy_event_handler(void *opaque) |
3334 |
{ |
3335 |
} |
3336 |
|
3337 |
static int qemu_event_init(void) |
3338 |
{ |
3339 |
qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
3340 |
if (!qemu_event_handle) {
|
3341 |
fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
|
3342 |
return -1; |
3343 |
} |
3344 |
qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
|
3345 |
return 0; |
3346 |
} |
3347 |
|
3348 |
static void qemu_event_increment(void) |
3349 |
{ |
3350 |
if (!SetEvent(qemu_event_handle)) {
|
3351 |
fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
|
3352 |
GetLastError()); |
3353 |
exit (1);
|
3354 |
} |
3355 |
} |
3356 |
#endif
|
3357 |
|
3358 |
static int cpu_can_run(CPUState *env) |
3359 |
{ |
3360 |
if (env->stop)
|
3361 |
return 0; |
3362 |
if (env->stopped)
|
3363 |
return 0; |
3364 |
return 1; |
3365 |
} |
3366 |
|
3367 |
#ifndef CONFIG_IOTHREAD
|
3368 |
static int qemu_init_main_loop(void) |
3369 |
{ |
3370 |
return qemu_event_init();
|
3371 |
} |
3372 |
|
3373 |
void qemu_init_vcpu(void *_env) |
3374 |
{ |
3375 |
CPUState *env = _env; |
3376 |
|
3377 |
if (kvm_enabled())
|
3378 |
kvm_init_vcpu(env); |
3379 |
env->nr_cores = smp_cores; |
3380 |
env->nr_threads = smp_threads; |
3381 |
return;
|
3382 |
} |
3383 |
|
3384 |
int qemu_cpu_self(void *env) |
3385 |
{ |
3386 |
return 1; |
3387 |
} |
3388 |
|
3389 |
static void resume_all_vcpus(void) |
3390 |
{ |
3391 |
} |
3392 |
|
3393 |
static void pause_all_vcpus(void) |
3394 |
{ |
3395 |
} |
3396 |
|
3397 |
void qemu_cpu_kick(void *env) |
3398 |
{ |
3399 |
return;
|
3400 |
} |
3401 |
|
3402 |
void qemu_notify_event(void) |
3403 |
{ |
3404 |
CPUState *env = cpu_single_env; |
3405 |
|
3406 |
if (env) {
|
3407 |
cpu_exit(env); |
3408 |
} |
3409 |
} |
3410 |
|
3411 |
void qemu_mutex_lock_iothread(void) {} |
3412 |
void qemu_mutex_unlock_iothread(void) {} |
3413 |
|
3414 |
void vm_stop(int reason) |
3415 |
{ |
3416 |
do_vm_stop(reason); |
3417 |
} |
3418 |
|
3419 |
#else /* CONFIG_IOTHREAD */ |
3420 |
|
3421 |
#include "qemu-thread.h" |
3422 |
|
3423 |
QemuMutex qemu_global_mutex; |
3424 |
static QemuMutex qemu_fair_mutex;
|
3425 |
|
3426 |
static QemuThread io_thread;
|
3427 |
|
3428 |
static QemuThread *tcg_cpu_thread;
|
3429 |
static QemuCond *tcg_halt_cond;
|
3430 |
|
3431 |
static int qemu_system_ready; |
3432 |
/* cpu creation */
|
3433 |
static QemuCond qemu_cpu_cond;
|
3434 |
/* system init */
|
3435 |
static QemuCond qemu_system_cond;
|
3436 |
static QemuCond qemu_pause_cond;
|
3437 |
|
3438 |
static void block_io_signals(void); |
3439 |
static void unblock_io_signals(void); |
3440 |
static int tcg_has_work(void); |
3441 |
|
3442 |
static int qemu_init_main_loop(void) |
3443 |
{ |
3444 |
int ret;
|
3445 |
|
3446 |
ret = qemu_event_init(); |
3447 |
if (ret)
|
3448 |
return ret;
|
3449 |
|
3450 |
qemu_cond_init(&qemu_pause_cond); |
3451 |
qemu_mutex_init(&qemu_fair_mutex); |
3452 |
qemu_mutex_init(&qemu_global_mutex); |
3453 |
qemu_mutex_lock(&qemu_global_mutex); |
3454 |
|
3455 |
unblock_io_signals(); |
3456 |
qemu_thread_self(&io_thread); |
3457 |
|
3458 |
return 0; |
3459 |
} |
3460 |
|
3461 |
static void qemu_wait_io_event(CPUState *env) |
3462 |
{ |
3463 |
while (!tcg_has_work())
|
3464 |
qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
|
3465 |
|
3466 |
qemu_mutex_unlock(&qemu_global_mutex); |
3467 |
|
3468 |
/*
|
3469 |
* Users of qemu_global_mutex can be starved, having no chance
|
3470 |
* to acquire it since this path will get to it first.
|
3471 |
* So use another lock to provide fairness.
|
3472 |
*/
|
3473 |
qemu_mutex_lock(&qemu_fair_mutex); |
3474 |
qemu_mutex_unlock(&qemu_fair_mutex); |
3475 |
|
3476 |
qemu_mutex_lock(&qemu_global_mutex); |
3477 |
if (env->stop) {
|
3478 |
env->stop = 0;
|
3479 |
env->stopped = 1;
|
3480 |
qemu_cond_signal(&qemu_pause_cond); |
3481 |
} |
3482 |
} |
3483 |
|
3484 |
static int qemu_cpu_exec(CPUState *env); |
3485 |
|
3486 |
static void *kvm_cpu_thread_fn(void *arg) |
3487 |
{ |
3488 |
CPUState *env = arg; |
3489 |
|
3490 |
block_io_signals(); |
3491 |
qemu_thread_self(env->thread); |
3492 |
if (kvm_enabled())
|
3493 |
kvm_init_vcpu(env); |
3494 |
|
3495 |
/* signal CPU creation */
|
3496 |
qemu_mutex_lock(&qemu_global_mutex); |
3497 |
env->created = 1;
|
3498 |
qemu_cond_signal(&qemu_cpu_cond); |
3499 |
|
3500 |
/* and wait for machine initialization */
|
3501 |
while (!qemu_system_ready)
|
3502 |
qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
|
3503 |
|
3504 |
while (1) { |
3505 |
if (cpu_can_run(env))
|
3506 |
qemu_cpu_exec(env); |
3507 |
qemu_wait_io_event(env); |
3508 |
} |
3509 |
|
3510 |
return NULL; |
3511 |
} |
3512 |
|
3513 |
static void tcg_cpu_exec(void); |
3514 |
|
3515 |
static void *tcg_cpu_thread_fn(void *arg) |
3516 |
{ |
3517 |
CPUState *env = arg; |
3518 |
|
3519 |
block_io_signals(); |
3520 |
qemu_thread_self(env->thread); |
3521 |
|
3522 |
/* signal CPU creation */
|
3523 |
qemu_mutex_lock(&qemu_global_mutex); |
3524 |
for (env = first_cpu; env != NULL; env = env->next_cpu) |
3525 |
env->created = 1;
|
3526 |
qemu_cond_signal(&qemu_cpu_cond); |
3527 |
|
3528 |
/* and wait for machine initialization */
|
3529 |
while (!qemu_system_ready)
|
3530 |
qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
|
3531 |
|
3532 |
while (1) { |
3533 |
tcg_cpu_exec(); |
3534 |
qemu_wait_io_event(cur_cpu); |
3535 |
} |
3536 |
|
3537 |
return NULL; |
3538 |
} |
3539 |
|
3540 |
void qemu_cpu_kick(void *_env) |
3541 |
{ |
3542 |
CPUState *env = _env; |
3543 |
qemu_cond_broadcast(env->halt_cond); |
3544 |
if (kvm_enabled())
|
3545 |
qemu_thread_signal(env->thread, SIGUSR1); |
3546 |
} |
3547 |
|
3548 |
int qemu_cpu_self(void *_env) |
3549 |
{ |
3550 |
CPUState *env = _env; |
3551 |
QemuThread this; |
3552 |
|
3553 |
qemu_thread_self(&this); |
3554 |
|
3555 |
return qemu_thread_equal(&this, env->thread);
|
3556 |
} |
3557 |
|
3558 |
static void cpu_signal(int sig) |
3559 |
{ |
3560 |
if (cpu_single_env)
|
3561 |
cpu_exit(cpu_single_env); |
3562 |
} |
3563 |
|
3564 |
static void block_io_signals(void) |
3565 |
{ |
3566 |
sigset_t set; |
3567 |
struct sigaction sigact;
|
3568 |
|
3569 |
sigemptyset(&set); |
3570 |
sigaddset(&set, SIGUSR2); |
3571 |
sigaddset(&set, SIGIO); |
3572 |
sigaddset(&set, SIGALRM); |
3573 |
pthread_sigmask(SIG_BLOCK, &set, NULL);
|
3574 |
|
3575 |
sigemptyset(&set); |
3576 |
sigaddset(&set, SIGUSR1); |
3577 |
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
|
3578 |
|
3579 |
memset(&sigact, 0, sizeof(sigact)); |
3580 |
sigact.sa_handler = cpu_signal; |
3581 |
sigaction(SIGUSR1, &sigact, NULL);
|
3582 |
} |
3583 |
|
3584 |
static void unblock_io_signals(void) |
3585 |
{ |
3586 |
sigset_t set; |
3587 |
|
3588 |
sigemptyset(&set); |
3589 |
sigaddset(&set, SIGUSR2); |
3590 |
sigaddset(&set, SIGIO); |
3591 |
sigaddset(&set, SIGALRM); |
3592 |
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
|
3593 |
|
3594 |
sigemptyset(&set); |
3595 |
sigaddset(&set, SIGUSR1); |
3596 |
pthread_sigmask(SIG_BLOCK, &set, NULL);
|
3597 |
} |
3598 |
|
3599 |
static void qemu_signal_lock(unsigned int msecs) |
3600 |
{ |
3601 |
qemu_mutex_lock(&qemu_fair_mutex); |
3602 |
|
3603 |
while (qemu_mutex_trylock(&qemu_global_mutex)) {
|
3604 |
qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
3605 |
if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
|
3606 |
break;
|
3607 |
} |
3608 |
qemu_mutex_unlock(&qemu_fair_mutex); |
3609 |
} |
3610 |
|
3611 |
void qemu_mutex_lock_iothread(void) |
3612 |
{ |
3613 |
if (kvm_enabled()) {
|
3614 |
qemu_mutex_lock(&qemu_fair_mutex); |
3615 |
qemu_mutex_lock(&qemu_global_mutex); |
3616 |
qemu_mutex_unlock(&qemu_fair_mutex); |
3617 |
} else
|
3618 |
qemu_signal_lock(100);
|
3619 |
} |
3620 |
|
3621 |
void qemu_mutex_unlock_iothread(void) |
3622 |
{ |
3623 |
qemu_mutex_unlock(&qemu_global_mutex); |
3624 |
} |
3625 |
|
3626 |
static int all_vcpus_paused(void) |
3627 |
{ |
3628 |
CPUState *penv = first_cpu; |
3629 |
|
3630 |
while (penv) {
|
3631 |
if (!penv->stopped)
|
3632 |
return 0; |
3633 |
penv = (CPUState *)penv->next_cpu; |
3634 |
} |
3635 |
|
3636 |
return 1; |
3637 |
} |
3638 |
|
3639 |
static void pause_all_vcpus(void) |
3640 |
{ |
3641 |
CPUState *penv = first_cpu; |
3642 |
|
3643 |
while (penv) {
|
3644 |
penv->stop = 1;
|
3645 |
qemu_thread_signal(penv->thread, SIGUSR1); |
3646 |
qemu_cpu_kick(penv); |
3647 |
penv = (CPUState *)penv->next_cpu; |
3648 |
} |
3649 |
|
3650 |
while (!all_vcpus_paused()) {
|
3651 |
qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
|
3652 |
penv = first_cpu; |
3653 |
while (penv) {
|
3654 |
qemu_thread_signal(penv->thread, SIGUSR1); |
3655 |
penv = (CPUState *)penv->next_cpu; |
3656 |
} |
3657 |
} |
3658 |
} |
3659 |
|
3660 |
static void resume_all_vcpus(void) |
3661 |
{ |
3662 |
CPUState *penv = first_cpu; |
3663 |
|
3664 |
while (penv) {
|
3665 |
penv->stop = 0;
|
3666 |
penv->stopped = 0;
|
3667 |
qemu_thread_signal(penv->thread, SIGUSR1); |
3668 |
qemu_cpu_kick(penv); |
3669 |
penv = (CPUState *)penv->next_cpu; |
3670 |
} |
3671 |
} |
3672 |
|
3673 |
static void tcg_init_vcpu(void *_env) |
3674 |
{ |
3675 |
CPUState *env = _env; |
3676 |
/* share a single thread for all cpus with TCG */
|
3677 |
if (!tcg_cpu_thread) {
|
3678 |
env->thread = qemu_mallocz(sizeof(QemuThread));
|
3679 |
env->halt_cond = qemu_mallocz(sizeof(QemuCond));
|
3680 |
qemu_cond_init(env->halt_cond); |
3681 |
qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
3682 |
while (env->created == 0) |
3683 |
qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
|
3684 |
tcg_cpu_thread = env->thread; |
3685 |
tcg_halt_cond = env->halt_cond; |
3686 |
} else {
|
3687 |
env->thread = tcg_cpu_thread; |
3688 |
env->halt_cond = tcg_halt_cond; |
3689 |
} |
3690 |
} |
3691 |
|
3692 |
static void kvm_start_vcpu(CPUState *env) |
3693 |
{ |
3694 |
env->thread = qemu_mallocz(sizeof(QemuThread));
|
3695 |
env->halt_cond = qemu_mallocz(sizeof(QemuCond));
|
3696 |
qemu_cond_init(env->halt_cond); |
3697 |
qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
3698 |
while (env->created == 0) |
3699 |
qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
|
3700 |
} |
3701 |
|
3702 |
void qemu_init_vcpu(void *_env) |
3703 |
{ |
3704 |
CPUState *env = _env; |
3705 |
|
3706 |
if (kvm_enabled())
|
3707 |
kvm_start_vcpu(env); |
3708 |
else
|
3709 |
tcg_init_vcpu(env); |
3710 |
env->nr_cores = smp_cores; |
3711 |
env->nr_threads = smp_threads; |
3712 |
} |
3713 |
|
3714 |
void qemu_notify_event(void) |
3715 |
{ |
3716 |
qemu_event_increment(); |
3717 |
} |
3718 |
|
3719 |
void vm_stop(int reason) |
3720 |
{ |
3721 |
QemuThread me; |
3722 |
qemu_thread_self(&me); |
3723 |
|
3724 |
if (!qemu_thread_equal(&me, &io_thread)) {
|
3725 |
qemu_system_vmstop_request(reason); |
3726 |
/*
|
3727 |
* FIXME: should not return to device code in case
|
3728 |
* vm_stop() has been requested.
|
3729 |
*/
|
3730 |
if (cpu_single_env) {
|
3731 |
cpu_exit(cpu_single_env); |
3732 |
cpu_single_env->stop = 1;
|
3733 |
} |
3734 |
return;
|
3735 |
} |
3736 |
do_vm_stop(reason); |
3737 |
} |
3738 |
|
3739 |
#endif
|
3740 |
|
3741 |
|
3742 |
#ifdef _WIN32
|
3743 |
static void host_main_loop_wait(int *timeout) |
3744 |
{ |
3745 |
int ret, ret2, i;
|
3746 |
PollingEntry *pe; |
3747 |
|
3748 |
|
3749 |
/* XXX: need to suppress polling by better using win32 events */
|
3750 |
ret = 0;
|
3751 |
for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
3752 |
ret |= pe->func(pe->opaque); |
3753 |
} |
3754 |
if (ret == 0) { |
3755 |
int err;
|
3756 |
WaitObjects *w = &wait_objects; |
3757 |
|
3758 |
ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
3759 |
if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
3760 |
if (w->func[ret - WAIT_OBJECT_0])
|
3761 |
w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
3762 |
|
3763 |
/* Check for additional signaled events */
|
3764 |
for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
3765 |
|
3766 |
/* Check if event is signaled */
|
3767 |
ret2 = WaitForSingleObject(w->events[i], 0);
|
3768 |
if(ret2 == WAIT_OBJECT_0) {
|
3769 |
if (w->func[i])
|
3770 |
w->func[i](w->opaque[i]); |
3771 |
} else if (ret2 == WAIT_TIMEOUT) { |
3772 |
} else {
|
3773 |
err = GetLastError(); |
3774 |
fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
3775 |
} |
3776 |
} |
3777 |
} else if (ret == WAIT_TIMEOUT) { |
3778 |
} else {
|
3779 |
err = GetLastError(); |
3780 |
fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
3781 |
} |
3782 |
} |
3783 |
|
3784 |
*timeout = 0;
|
3785 |
} |
3786 |
#else
|
3787 |
static void host_main_loop_wait(int *timeout) |
3788 |
{ |
3789 |
} |
3790 |
#endif
|
3791 |
|
3792 |
void main_loop_wait(int timeout) |
3793 |
{ |
3794 |
IOHandlerRecord *ioh; |
3795 |
fd_set rfds, wfds, xfds; |
3796 |
int ret, nfds;
|
3797 |
struct timeval tv;
|
3798 |
|
3799 |
qemu_bh_update_timeout(&timeout); |
3800 |
|
3801 |
host_main_loop_wait(&timeout); |
3802 |
|
3803 |
/* poll any events */
|
3804 |
/* XXX: separate device handlers from system ones */
|
3805 |
nfds = -1;
|
3806 |
FD_ZERO(&rfds); |
3807 |
FD_ZERO(&wfds); |
3808 |
FD_ZERO(&xfds); |
3809 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
3810 |
if (ioh->deleted)
|
3811 |
continue;
|
3812 |
if (ioh->fd_read &&
|
3813 |
(!ioh->fd_read_poll || |
3814 |
ioh->fd_read_poll(ioh->opaque) != 0)) {
|
3815 |
FD_SET(ioh->fd, &rfds); |
3816 |
if (ioh->fd > nfds)
|
3817 |
nfds = ioh->fd; |
3818 |
} |
3819 |
if (ioh->fd_write) {
|
3820 |
FD_SET(ioh->fd, &wfds); |
3821 |
if (ioh->fd > nfds)
|
3822 |
nfds = ioh->fd; |
3823 |
} |
3824 |
} |
3825 |
|
3826 |
tv.tv_sec = timeout / 1000;
|
3827 |
tv.tv_usec = (timeout % 1000) * 1000; |
3828 |
|
3829 |
slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
3830 |
|
3831 |
qemu_mutex_unlock_iothread(); |
3832 |
ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
3833 |
qemu_mutex_lock_iothread(); |
3834 |
if (ret > 0) { |
3835 |
IOHandlerRecord **pioh; |
3836 |
|
3837 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
3838 |
if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
3839 |
ioh->fd_read(ioh->opaque); |
3840 |
} |
3841 |
if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
3842 |
ioh->fd_write(ioh->opaque); |
3843 |
} |
3844 |
} |
3845 |
|
3846 |
/* remove deleted IO handlers */
|
3847 |
pioh = &first_io_handler; |
3848 |
while (*pioh) {
|
3849 |
ioh = *pioh; |
3850 |
if (ioh->deleted) {
|
3851 |
*pioh = ioh->next; |
3852 |
qemu_free(ioh); |
3853 |
} else
|
3854 |
pioh = &ioh->next; |
3855 |
} |
3856 |
} |
3857 |
|
3858 |
slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
|
3859 |
|
3860 |
/* rearm timer, if not periodic */
|
3861 |
if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
3862 |
alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
3863 |
qemu_rearm_alarm_timer(alarm_timer); |
3864 |
} |
3865 |
|
3866 |
/* vm time timers */
|
3867 |
if (vm_running) {
|
3868 |
if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
3869 |
qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL], |
3870 |
qemu_get_clock(vm_clock)); |
3871 |
} |
3872 |
|
3873 |
/* real time timers */
|
3874 |
qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME], |
3875 |
qemu_get_clock(rt_clock)); |
3876 |
|
3877 |
qemu_run_timers(&active_timers[QEMU_CLOCK_HOST], |
3878 |
qemu_get_clock(host_clock)); |
3879 |
|
3880 |
/* Check bottom-halves last in case any of the earlier events triggered
|
3881 |
them. */
|
3882 |
qemu_bh_poll(); |
3883 |
|
3884 |
} |
3885 |
|
3886 |
static int qemu_cpu_exec(CPUState *env) |
3887 |
{ |
3888 |
int ret;
|
3889 |
#ifdef CONFIG_PROFILER
|
3890 |
int64_t ti; |
3891 |
#endif
|
3892 |
|
3893 |
#ifdef CONFIG_PROFILER
|
3894 |
ti = profile_getclock(); |
3895 |
#endif
|
3896 |
if (use_icount) {
|
3897 |
int64_t count; |
3898 |
int decr;
|
3899 |
qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
3900 |
env->icount_decr.u16.low = 0;
|
3901 |
env->icount_extra = 0;
|
3902 |
count = qemu_next_deadline(); |
3903 |
count = (count + (1 << icount_time_shift) - 1) |
3904 |
>> icount_time_shift; |
3905 |
qemu_icount += count; |
3906 |
decr = (count > 0xffff) ? 0xffff : count; |
3907 |
count -= decr; |
3908 |
env->icount_decr.u16.low = decr; |
3909 |
env->icount_extra = count; |
3910 |
} |
3911 |
ret = cpu_exec(env); |
3912 |
#ifdef CONFIG_PROFILER
|
3913 |
qemu_time += profile_getclock() - ti; |
3914 |
#endif
|
3915 |
if (use_icount) {
|
3916 |
/* Fold pending instructions back into the
|
3917 |
instruction counter, and clear the interrupt flag. */
|
3918 |
qemu_icount -= (env->icount_decr.u16.low |
3919 |
+ env->icount_extra); |
3920 |
env->icount_decr.u32 = 0;
|
3921 |
env->icount_extra = 0;
|
3922 |
} |
3923 |
return ret;
|
3924 |
} |
3925 |
|
3926 |
static void tcg_cpu_exec(void) |
3927 |
{ |
3928 |
int ret = 0; |
3929 |
|
3930 |
if (next_cpu == NULL) |
3931 |
next_cpu = first_cpu; |
3932 |
for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
3933 |
CPUState *env = cur_cpu = next_cpu; |
3934 |
|
3935 |
if (!vm_running)
|
3936 |
break;
|
3937 |
if (timer_alarm_pending) {
|
3938 |
timer_alarm_pending = 0;
|
3939 |
break;
|
3940 |
} |
3941 |
if (cpu_can_run(env))
|
3942 |
ret = qemu_cpu_exec(env); |
3943 |
if (ret == EXCP_DEBUG) {
|
3944 |
gdb_set_stop_cpu(env); |
3945 |
debug_requested = 1;
|
3946 |
break;
|
3947 |
} |
3948 |
} |
3949 |
} |
3950 |
|
3951 |
static int cpu_has_work(CPUState *env) |
3952 |
{ |
3953 |
if (env->stop)
|
3954 |
return 1; |
3955 |
if (env->stopped)
|
3956 |
return 0; |
3957 |
if (!env->halted)
|
3958 |
return 1; |
3959 |
if (qemu_cpu_has_work(env))
|
3960 |
return 1; |
3961 |
return 0; |
3962 |
} |
3963 |
|
3964 |
static int tcg_has_work(void) |
3965 |
{ |
3966 |
CPUState *env; |
3967 |
|
3968 |
for (env = first_cpu; env != NULL; env = env->next_cpu) |
3969 |
if (cpu_has_work(env))
|
3970 |
return 1; |
3971 |
return 0; |
3972 |
} |
3973 |
|
3974 |
static int qemu_calculate_timeout(void) |
3975 |
{ |
3976 |
#ifndef CONFIG_IOTHREAD
|
3977 |
int timeout;
|
3978 |
|
3979 |
if (!vm_running)
|
3980 |
timeout = 5000;
|
3981 |
else if (tcg_has_work()) |
3982 |
timeout = 0;
|
3983 |
else if (!use_icount) |
3984 |
timeout = 5000;
|
3985 |
else {
|
3986 |
/* XXX: use timeout computed from timers */
|
3987 |
int64_t add; |
3988 |
int64_t delta; |
3989 |
/* Advance virtual time to the next event. */
|
3990 |
if (use_icount == 1) { |
3991 |
/* When not using an adaptive execution frequency
|
3992 |
we tend to get badly out of sync with real time,
|
3993 |
so just delay for a reasonable amount of time. */
|
3994 |
delta = 0;
|
3995 |
} else {
|
3996 |
delta = cpu_get_icount() - cpu_get_clock(); |
3997 |
} |
3998 |
if (delta > 0) { |
3999 |
/* If virtual time is ahead of real time then just
|
4000 |
wait for IO. */
|
4001 |
timeout = (delta / 1000000) + 1; |
4002 |
} else {
|
4003 |
/* Wait for either IO to occur or the next
|
4004 |
timer event. */
|
4005 |
add = qemu_next_deadline(); |
4006 |
/* We advance the timer before checking for IO.
|
4007 |
Limit the amount we advance so that early IO
|
4008 |
activity won't get the guest too far ahead. */
|
4009 |
if (add > 10000000) |
4010 |
add = 10000000;
|
4011 |
delta += add; |
4012 |
add = (add + (1 << icount_time_shift) - 1) |
4013 |
>> icount_time_shift; |
4014 |
qemu_icount += add; |
4015 |
timeout = delta / 1000000;
|
4016 |
if (timeout < 0) |
4017 |
timeout = 0;
|
4018 |
} |
4019 |
} |
4020 |
|
4021 |
return timeout;
|
4022 |
#else /* CONFIG_IOTHREAD */ |
4023 |
return 1000; |
4024 |
#endif
|
4025 |
} |
4026 |
|
4027 |
static int vm_can_run(void) |
4028 |
{ |
4029 |
if (powerdown_requested)
|
4030 |
return 0; |
4031 |
if (reset_requested)
|
4032 |
return 0; |
4033 |
if (shutdown_requested)
|
4034 |
return 0; |
4035 |
if (debug_requested)
|
4036 |
return 0; |
4037 |
return 1; |
4038 |
} |
4039 |
|
4040 |
qemu_irq qemu_system_powerdown; |
4041 |
|
4042 |
static void main_loop(void) |
4043 |
{ |
4044 |
int r;
|
4045 |
|
4046 |
#ifdef CONFIG_IOTHREAD
|
4047 |
qemu_system_ready = 1;
|
4048 |
qemu_cond_broadcast(&qemu_system_cond); |
4049 |
#endif
|
4050 |
|
4051 |
for (;;) {
|
4052 |
do {
|
4053 |
#ifdef CONFIG_PROFILER
|
4054 |
int64_t ti; |
4055 |
#endif
|
4056 |
#ifndef CONFIG_IOTHREAD
|
4057 |
tcg_cpu_exec(); |
4058 |
#endif
|
4059 |
#ifdef CONFIG_PROFILER
|
4060 |
ti = profile_getclock(); |
4061 |
#endif
|
4062 |
main_loop_wait(qemu_calculate_timeout()); |
4063 |
#ifdef CONFIG_PROFILER
|
4064 |
dev_time += profile_getclock() - ti; |
4065 |
#endif
|
4066 |
} while (vm_can_run());
|
4067 |
|
4068 |
if (qemu_debug_requested()) {
|
4069 |
monitor_protocol_event(EVENT_DEBUG, NULL);
|
4070 |
vm_stop(EXCP_DEBUG); |
4071 |
} |
4072 |
if (qemu_shutdown_requested()) {
|
4073 |
monitor_protocol_event(EVENT_SHUTDOWN, NULL);
|
4074 |
if (no_shutdown) {
|
4075 |
vm_stop(0);
|
4076 |
no_shutdown = 0;
|
4077 |
} else
|
4078 |
break;
|
4079 |
} |
4080 |
if (qemu_reset_requested()) {
|
4081 |
monitor_protocol_event(EVENT_RESET, NULL);
|
4082 |
pause_all_vcpus(); |
4083 |
qemu_system_reset(); |
4084 |
resume_all_vcpus(); |
4085 |
} |
4086 |
if (qemu_powerdown_requested()) {
|
4087 |
monitor_protocol_event(EVENT_POWERDOWN, NULL);
|
4088 |
qemu_irq_raise(qemu_system_powerdown); |
4089 |
} |
4090 |
if ((r = qemu_vmstop_requested())) {
|
4091 |
monitor_protocol_event(EVENT_STOP, NULL);
|
4092 |
vm_stop(r); |
4093 |
} |
4094 |
} |
4095 |
pause_all_vcpus(); |
4096 |
} |
4097 |
|
4098 |
static void version(void) |
4099 |
{ |
4100 |
printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
4101 |
} |
4102 |
|
4103 |
static void help(int exitcode) |
4104 |
{ |
4105 |
version(); |
4106 |
printf("usage: %s [options] [disk_image]\n"
|
4107 |
"\n"
|
4108 |
"'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
4109 |
"\n"
|
4110 |
#define DEF(option, opt_arg, opt_enum, opt_help) \
|
4111 |
opt_help |
4112 |
#define DEFHEADING(text) stringify(text) "\n" |
4113 |
#include "qemu-options.h" |
4114 |
#undef DEF
|
4115 |
#undef DEFHEADING
|
4116 |
#undef GEN_DOCS
|
4117 |
"\n"
|
4118 |
"During emulation, the following keys are useful:\n"
|
4119 |
"ctrl-alt-f toggle full screen\n"
|
4120 |
"ctrl-alt-n switch to virtual console 'n'\n"
|
4121 |
"ctrl-alt toggle mouse and keyboard grab\n"
|
4122 |
"\n"
|
4123 |
"When using -nographic, press 'ctrl-a h' to get some help.\n"
|
4124 |
, |
4125 |
"qemu",
|
4126 |
DEFAULT_RAM_SIZE, |
4127 |
#ifndef _WIN32
|
4128 |
DEFAULT_NETWORK_SCRIPT, |
4129 |
DEFAULT_NETWORK_DOWN_SCRIPT, |
4130 |
#endif
|
4131 |
DEFAULT_GDBSTUB_PORT, |
4132 |
"/tmp/qemu.log");
|
4133 |
exit(exitcode); |
4134 |
} |
4135 |
|
4136 |
#define HAS_ARG 0x0001 |
4137 |
|
4138 |
enum {
|
4139 |
#define DEF(option, opt_arg, opt_enum, opt_help) \
|
4140 |
opt_enum, |
4141 |
#define DEFHEADING(text)
|
4142 |
#include "qemu-options.h" |
4143 |
#undef DEF
|
4144 |
#undef DEFHEADING
|
4145 |
#undef GEN_DOCS
|
4146 |
}; |
4147 |
|
4148 |
typedef struct QEMUOption { |
4149 |
const char *name; |
4150 |
int flags;
|
4151 |
int index;
|
4152 |
} QEMUOption; |
4153 |
|
4154 |
static const QEMUOption qemu_options[] = { |
4155 |
{ "h", 0, QEMU_OPTION_h }, |
4156 |
#define DEF(option, opt_arg, opt_enum, opt_help) \
|
4157 |
{ option, opt_arg, opt_enum }, |
4158 |
#define DEFHEADING(text)
|
4159 |
#include "qemu-options.h" |
4160 |
#undef DEF
|
4161 |
#undef DEFHEADING
|
4162 |
#undef GEN_DOCS
|
4163 |
{ NULL },
|
4164 |
}; |
4165 |
|
4166 |
#ifdef HAS_AUDIO
|
4167 |
struct soundhw soundhw[] = {
|
4168 |
#ifdef HAS_AUDIO_CHOICE
|
4169 |
#if defined(TARGET_I386) || defined(TARGET_MIPS)
|
4170 |
{ |
4171 |
"pcspk",
|
4172 |
"PC speaker",
|
4173 |
0,
|
4174 |
1,
|
4175 |
{ .init_isa = pcspk_audio_init } |
4176 |
}, |
4177 |
#endif
|
4178 |
|
4179 |
#ifdef CONFIG_SB16
|
4180 |
{ |
4181 |
"sb16",
|
4182 |
"Creative Sound Blaster 16",
|
4183 |
0,
|
4184 |
1,
|
4185 |
{ .init_isa = SB16_init } |
4186 |
}, |
4187 |
#endif
|
4188 |
|
4189 |
#ifdef CONFIG_CS4231A
|
4190 |
{ |
4191 |
"cs4231a",
|
4192 |
"CS4231A",
|
4193 |
0,
|
4194 |
1,
|
4195 |
{ .init_isa = cs4231a_init } |
4196 |
}, |
4197 |
#endif
|
4198 |
|
4199 |
#ifdef CONFIG_ADLIB
|
4200 |
{ |
4201 |
"adlib",
|
4202 |
#ifdef HAS_YMF262
|
4203 |
"Yamaha YMF262 (OPL3)",
|
4204 |
#else
|
4205 |
"Yamaha YM3812 (OPL2)",
|
4206 |
#endif
|
4207 |
0,
|
4208 |
1,
|
4209 |
{ .init_isa = Adlib_init } |
4210 |
}, |
4211 |
#endif
|
4212 |
|
4213 |
#ifdef CONFIG_GUS
|
4214 |
{ |
4215 |
"gus",
|
4216 |
"Gravis Ultrasound GF1",
|
4217 |
0,
|
4218 |
1,
|
4219 |
{ .init_isa = GUS_init } |
4220 |
}, |
4221 |
#endif
|
4222 |
|
4223 |
#ifdef CONFIG_AC97
|
4224 |
{ |
4225 |
"ac97",
|
4226 |
"Intel 82801AA AC97 Audio",
|
4227 |
0,
|
4228 |
0,
|
4229 |
{ .init_pci = ac97_init } |
4230 |
}, |
4231 |
#endif
|
4232 |
|
4233 |
#ifdef CONFIG_ES1370
|
4234 |
{ |
4235 |
"es1370",
|
4236 |
"ENSONIQ AudioPCI ES1370",
|
4237 |
0,
|
4238 |
0,
|
4239 |
{ .init_pci = es1370_init } |
4240 |
}, |
4241 |
#endif
|
4242 |
|
4243 |
#endif /* HAS_AUDIO_CHOICE */ |
4244 |
|
4245 |
{ NULL, NULL, 0, 0, { NULL } } |
4246 |
}; |
4247 |
|
4248 |
static void select_soundhw (const char *optarg) |
4249 |
{ |
4250 |
struct soundhw *c;
|
4251 |
|
4252 |
if (*optarg == '?') { |
4253 |
show_valid_cards:
|
4254 |
|
4255 |
printf ("Valid sound card names (comma separated):\n");
|
4256 |
for (c = soundhw; c->name; ++c) {
|
4257 |
printf ("%-11s %s\n", c->name, c->descr);
|
4258 |
} |
4259 |
printf ("\n-soundhw all will enable all of the above\n");
|
4260 |
exit (*optarg != '?');
|
4261 |
} |
4262 |
else {
|
4263 |
size_t l; |
4264 |
const char *p; |
4265 |
char *e;
|
4266 |
int bad_card = 0; |
4267 |
|
4268 |
if (!strcmp (optarg, "all")) { |
4269 |
for (c = soundhw; c->name; ++c) {
|
4270 |
c->enabled = 1;
|
4271 |
} |
4272 |
return;
|
4273 |
} |
4274 |
|
4275 |
p = optarg; |
4276 |
while (*p) {
|
4277 |
e = strchr (p, ',');
|
4278 |
l = !e ? strlen (p) : (size_t) (e - p); |
4279 |
|
4280 |
for (c = soundhw; c->name; ++c) {
|
4281 |
if (!strncmp (c->name, p, l) && !c->name[l]) {
|
4282 |
c->enabled = 1;
|
4283 |
break;
|
4284 |
} |
4285 |
} |
4286 |
|
4287 |
if (!c->name) {
|
4288 |
if (l > 80) { |
4289 |
fprintf (stderr, |
4290 |
"Unknown sound card name (too big to show)\n");
|
4291 |
} |
4292 |
else {
|
4293 |
fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
4294 |
(int) l, p);
|
4295 |
} |
4296 |
bad_card = 1;
|
4297 |
} |
4298 |
p += l + (e != NULL);
|
4299 |
} |
4300 |
|
4301 |
if (bad_card)
|
4302 |
goto show_valid_cards;
|
4303 |
} |
4304 |
} |
4305 |
#endif
|
4306 |
|
4307 |
static void select_vgahw (const char *p) |
4308 |
{ |
4309 |
const char *opts; |
4310 |
|
4311 |
vga_interface_type = VGA_NONE; |
4312 |
if (strstart(p, "std", &opts)) { |
4313 |
vga_interface_type = VGA_STD; |
4314 |
} else if (strstart(p, "cirrus", &opts)) { |
4315 |
vga_interface_type = VGA_CIRRUS; |
4316 |
} else if (strstart(p, "vmware", &opts)) { |
4317 |
vga_interface_type = VGA_VMWARE; |
4318 |
} else if (strstart(p, "xenfb", &opts)) { |
4319 |
vga_interface_type = VGA_XENFB; |
4320 |
} else if (!strstart(p, "none", &opts)) { |
4321 |
invalid_vga:
|
4322 |
fprintf(stderr, "Unknown vga type: %s\n", p);
|
4323 |
exit(1);
|
4324 |
} |
4325 |
while (*opts) {
|
4326 |
const char *nextopt; |
4327 |
|
4328 |
if (strstart(opts, ",retrace=", &nextopt)) { |
4329 |
opts = nextopt; |
4330 |
if (strstart(opts, "dumb", &nextopt)) |
4331 |
vga_retrace_method = VGA_RETRACE_DUMB; |
4332 |
else if (strstart(opts, "precise", &nextopt)) |
4333 |
vga_retrace_method = VGA_RETRACE_PRECISE; |
4334 |
else goto invalid_vga; |
4335 |
} else goto invalid_vga; |
4336 |
opts = nextopt; |
4337 |
} |
4338 |
} |
4339 |
|
4340 |
#ifdef TARGET_I386
|
4341 |
static int balloon_parse(const char *arg) |
4342 |
{ |
4343 |
QemuOpts *opts; |
4344 |
|
4345 |
if (strcmp(arg, "none") == 0) { |
4346 |
return 0; |
4347 |
} |
4348 |
|
4349 |
if (!strncmp(arg, "virtio", 6)) { |
4350 |
if (arg[6] == ',') { |
4351 |
/* have params -> parse them */
|
4352 |
opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
4353 |
if (!opts)
|
4354 |
return -1; |
4355 |
} else {
|
4356 |
/* create empty opts */
|
4357 |
opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
4358 |
} |
4359 |
qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
4360 |
return 0; |
4361 |
} |
4362 |
|
4363 |
return -1; |
4364 |
} |
4365 |
#endif
|
4366 |
|
4367 |
#ifdef _WIN32
|
4368 |
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
4369 |
{ |
4370 |
exit(STATUS_CONTROL_C_EXIT); |
4371 |
return TRUE;
|
4372 |
} |
4373 |
#endif
|
4374 |
|
4375 |
int qemu_uuid_parse(const char *str, uint8_t *uuid) |
4376 |
{ |
4377 |
int ret;
|
4378 |
|
4379 |
if(strlen(str) != 36) |
4380 |
return -1; |
4381 |
|
4382 |
ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
4383 |
&uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
4384 |
&uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
4385 |
|
4386 |
if(ret != 16) |
4387 |
return -1; |
4388 |
|
4389 |
#ifdef TARGET_I386
|
4390 |
smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
4391 |
#endif
|
4392 |
|
4393 |
return 0; |
4394 |
} |
4395 |
|
4396 |
#ifndef _WIN32
|
4397 |
|
4398 |
static void termsig_handler(int signal) |
4399 |
{ |
4400 |
qemu_system_shutdown_request(); |
4401 |
} |
4402 |
|
4403 |
static void sigchld_handler(int signal) |
4404 |
{ |
4405 |
waitpid(-1, NULL, WNOHANG); |
4406 |
} |
4407 |
|
4408 |
static void sighandler_setup(void) |
4409 |
{ |
4410 |
struct sigaction act;
|
4411 |
|
4412 |
memset(&act, 0, sizeof(act)); |
4413 |
act.sa_handler = termsig_handler; |
4414 |
sigaction(SIGINT, &act, NULL);
|
4415 |
sigaction(SIGHUP, &act, NULL);
|
4416 |
sigaction(SIGTERM, &act, NULL);
|
4417 |
|
4418 |
act.sa_handler = sigchld_handler; |
4419 |
act.sa_flags = SA_NOCLDSTOP; |
4420 |
sigaction(SIGCHLD, &act, NULL);
|
4421 |
} |
4422 |
|
4423 |
#endif
|
4424 |
|
4425 |
#ifdef _WIN32
|
4426 |
/* Look for support files in the same directory as the executable. */
|
4427 |
static char *find_datadir(const char *argv0) |
4428 |
{ |
4429 |
char *p;
|
4430 |
char buf[MAX_PATH];
|
4431 |
DWORD len; |
4432 |
|
4433 |
len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
4434 |
if (len == 0) { |
4435 |
return NULL; |
4436 |
} |
4437 |
|
4438 |
buf[len] = 0;
|
4439 |
p = buf + len - 1;
|
4440 |
while (p != buf && *p != '\\') |
4441 |
p--; |
4442 |
*p = 0;
|
4443 |
if (access(buf, R_OK) == 0) { |
4444 |
return qemu_strdup(buf);
|
4445 |
} |
4446 |
return NULL; |
4447 |
} |
4448 |
#else /* !_WIN32 */ |
4449 |
|
4450 |
/* Find a likely location for support files using the location of the binary.
|
4451 |
For installed binaries this will be "$bindir/../share/qemu". When
|
4452 |
running from the build tree this will be "$bindir/../pc-bios". */
|
4453 |
#define SHARE_SUFFIX "/share/qemu" |
4454 |
#define BUILD_SUFFIX "/pc-bios" |
4455 |
static char *find_datadir(const char *argv0) |
4456 |
{ |
4457 |
char *dir;
|
4458 |
char *p = NULL; |
4459 |
char *res;
|
4460 |
char buf[PATH_MAX];
|
4461 |
size_t max_len; |
4462 |
|
4463 |
#if defined(__linux__)
|
4464 |
{ |
4465 |
int len;
|
4466 |
len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
4467 |
if (len > 0) { |
4468 |
buf[len] = 0;
|
4469 |
p = buf; |
4470 |
} |
4471 |
} |
4472 |
#elif defined(__FreeBSD__)
|
4473 |
{ |
4474 |
int len;
|
4475 |
len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
4476 |
if (len > 0) { |
4477 |
buf[len] = 0;
|
4478 |
p = buf; |
4479 |
} |
4480 |
} |
4481 |
#endif
|
4482 |
/* If we don't have any way of figuring out the actual executable
|
4483 |
location then try argv[0]. */
|
4484 |
if (!p) {
|
4485 |
p = realpath(argv0, buf); |
4486 |
if (!p) {
|
4487 |
return NULL; |
4488 |
} |
4489 |
} |
4490 |
dir = dirname(p); |
4491 |
dir = dirname(dir); |
4492 |
|
4493 |
max_len = strlen(dir) + |
4494 |
MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
|
4495 |
res = qemu_mallocz(max_len); |
4496 |
snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
|
4497 |
if (access(res, R_OK)) {
|
4498 |
snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
|
4499 |
if (access(res, R_OK)) {
|
4500 |
qemu_free(res); |
4501 |
res = NULL;
|
4502 |
} |
4503 |
} |
4504 |
|
4505 |
return res;
|
4506 |
} |
4507 |
#undef SHARE_SUFFIX
|
4508 |
#undef BUILD_SUFFIX
|
4509 |
#endif
|
4510 |
|
4511 |
char *qemu_find_file(int type, const char *name) |
4512 |
{ |
4513 |
int len;
|
4514 |
const char *subdir; |
4515 |
char *buf;
|
4516 |
|
4517 |
/* If name contains path separators then try it as a straight path. */
|
4518 |
if ((strchr(name, '/') || strchr(name, '\\')) |
4519 |
&& access(name, R_OK) == 0) {
|
4520 |
return qemu_strdup(name);
|
4521 |
} |
4522 |
switch (type) {
|
4523 |
case QEMU_FILE_TYPE_BIOS:
|
4524 |
subdir = "";
|
4525 |
break;
|
4526 |
case QEMU_FILE_TYPE_KEYMAP:
|
4527 |
subdir = "keymaps/";
|
4528 |
break;
|
4529 |
default:
|
4530 |
abort(); |
4531 |
} |
4532 |
len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
|
4533 |
buf = qemu_mallocz(len); |
4534 |
snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
|
4535 |
if (access(buf, R_OK)) {
|
4536 |
qemu_free(buf); |
4537 |
return NULL; |
4538 |
} |
4539 |
return buf;
|
4540 |
} |
4541 |
|
4542 |
static int device_init_func(QemuOpts *opts, void *opaque) |
4543 |
{ |
4544 |
DeviceState *dev; |
4545 |
|
4546 |
dev = qdev_device_add(opts); |
4547 |
if (!dev)
|
4548 |
return -1; |
4549 |
return 0; |
4550 |
} |
4551 |
|
4552 |
struct device_config {
|
4553 |
enum {
|
4554 |
DEV_USB, /* -usbdevice */
|
4555 |
DEV_BT, /* -bt */
|
4556 |
} type; |
4557 |
const char *cmdline; |
4558 |
QTAILQ_ENTRY(device_config) next; |
4559 |
}; |
4560 |
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs); |
4561 |
|
4562 |
static void add_device_config(int type, const char *cmdline) |
4563 |
{ |
4564 |
struct device_config *conf;
|
4565 |
|
4566 |
conf = qemu_mallocz(sizeof(*conf));
|
4567 |
conf->type = type; |
4568 |
conf->cmdline = cmdline; |
4569 |
QTAILQ_INSERT_TAIL(&device_configs, conf, next); |
4570 |
} |
4571 |
|
4572 |
static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
4573 |
{ |
4574 |
struct device_config *conf;
|
4575 |
int rc;
|
4576 |
|
4577 |
QTAILQ_FOREACH(conf, &device_configs, next) { |
4578 |
if (conf->type != type)
|
4579 |
continue;
|
4580 |
rc = func(conf->cmdline); |
4581 |
if (0 != rc) |
4582 |
return rc;
|
4583 |
} |
4584 |
return 0; |
4585 |
} |
4586 |
|
4587 |
int main(int argc, char **argv, char **envp) |
4588 |
{ |
4589 |
const char *gdbstub_dev = NULL; |
4590 |
uint32_t boot_devices_bitmap = 0;
|
4591 |
int i;
|
4592 |
int snapshot, linux_boot, net_boot;
|
4593 |
const char *initrd_filename; |
4594 |
const char *kernel_filename, *kernel_cmdline; |
4595 |
char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
4596 |
DisplayState *ds; |
4597 |
DisplayChangeListener *dcl; |
4598 |
int cyls, heads, secs, translation;
|
4599 |
QemuOpts *hda_opts = NULL, *opts;
|
4600 |
int optind;
|
4601 |
const char *r, *optarg; |
4602 |
CharDriverState *monitor_hds[MAX_MONITOR_DEVICES]; |
4603 |
const char *monitor_devices[MAX_MONITOR_DEVICES]; |
4604 |
int monitor_flags[MAX_MONITOR_DEVICES];
|
4605 |
int monitor_device_index;
|
4606 |
const char *serial_devices[MAX_SERIAL_PORTS]; |
4607 |
int serial_device_index;
|
4608 |
const char *parallel_devices[MAX_PARALLEL_PORTS]; |
4609 |
int parallel_device_index;
|
4610 |
const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
4611 |
int virtio_console_index;
|
4612 |
const char *loadvm = NULL; |
4613 |
QEMUMachine *machine; |
4614 |
const char *cpu_model; |
4615 |
#ifndef _WIN32
|
4616 |
int fds[2]; |
4617 |
#endif
|
4618 |
int tb_size;
|
4619 |
const char *pid_file = NULL; |
4620 |
const char *incoming = NULL; |
4621 |
#ifndef _WIN32
|
4622 |
int fd = 0; |
4623 |
struct passwd *pwd = NULL; |
4624 |
const char *chroot_dir = NULL; |
4625 |
const char *run_as = NULL; |
4626 |
#endif
|
4627 |
CPUState *env; |
4628 |
int show_vnc_port = 0; |
4629 |
|
4630 |
init_clocks(); |
4631 |
|
4632 |
qemu_errors_to_file(stderr); |
4633 |
qemu_cache_utils_init(envp); |
4634 |
|
4635 |
QLIST_INIT (&vm_change_state_head); |
4636 |
#ifndef _WIN32
|
4637 |
{ |
4638 |
struct sigaction act;
|
4639 |
sigfillset(&act.sa_mask); |
4640 |
act.sa_flags = 0;
|
4641 |
act.sa_handler = SIG_IGN; |
4642 |
sigaction(SIGPIPE, &act, NULL);
|
4643 |
} |
4644 |
#else
|
4645 |
SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
4646 |
/* Note: cpu_interrupt() is currently not SMP safe, so we force
|
4647 |
QEMU to run on a single CPU */
|
4648 |
{ |
4649 |
HANDLE h; |
4650 |
DWORD mask, smask; |
4651 |
int i;
|
4652 |
h = GetCurrentProcess(); |
4653 |
if (GetProcessAffinityMask(h, &mask, &smask)) {
|
4654 |
for(i = 0; i < 32; i++) { |
4655 |
if (mask & (1 << i)) |
4656 |
break;
|
4657 |
} |
4658 |
if (i != 32) { |
4659 |
mask = 1 << i;
|
4660 |
SetProcessAffinityMask(h, mask); |
4661 |
} |
4662 |
} |
4663 |
} |
4664 |
#endif
|
4665 |
|
4666 |
module_call_init(MODULE_INIT_MACHINE); |
4667 |
machine = find_default_machine(); |
4668 |
cpu_model = NULL;
|
4669 |
initrd_filename = NULL;
|
4670 |
ram_size = 0;
|
4671 |
snapshot = 0;
|
4672 |
kernel_filename = NULL;
|
4673 |
kernel_cmdline = "";
|
4674 |
cyls = heads = secs = 0;
|
4675 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
4676 |
|
4677 |
serial_devices[0] = "vc:80Cx24C"; |
4678 |
for(i = 1; i < MAX_SERIAL_PORTS; i++) |
4679 |
serial_devices[i] = NULL;
|
4680 |
serial_device_index = 0;
|
4681 |
|
4682 |
parallel_devices[0] = "vc:80Cx24C"; |
4683 |
for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
4684 |
parallel_devices[i] = NULL;
|
4685 |
parallel_device_index = 0;
|
4686 |
|
4687 |
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
4688 |
virtio_consoles[i] = NULL;
|
4689 |
virtio_console_index = 0;
|
4690 |
|
4691 |
monitor_devices[0] = "vc:80Cx24C"; |
4692 |
monitor_flags[0] = MONITOR_IS_DEFAULT | MONITOR_USE_READLINE;
|
4693 |
for (i = 1; i < MAX_MONITOR_DEVICES; i++) { |
4694 |
monitor_devices[i] = NULL;
|
4695 |
monitor_flags[i] = MONITOR_USE_READLINE; |
4696 |
} |
4697 |
monitor_device_index = 0;
|
4698 |
|
4699 |
for (i = 0; i < MAX_NODES; i++) { |
4700 |
node_mem[i] = 0;
|
4701 |
node_cpumask[i] = 0;
|
4702 |
} |
4703 |
|
4704 |
nb_numa_nodes = 0;
|
4705 |
nb_nics = 0;
|
4706 |
|
4707 |
tb_size = 0;
|
4708 |
autostart= 1;
|
4709 |
|
4710 |
optind = 1;
|
4711 |
for(;;) {
|
4712 |
if (optind >= argc)
|
4713 |
break;
|
4714 |
r = argv[optind]; |
4715 |
if (r[0] != '-') { |
4716 |
hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
|
4717 |
} else {
|
4718 |
const QEMUOption *popt;
|
4719 |
|
4720 |
optind++; |
4721 |
/* Treat --foo the same as -foo. */
|
4722 |
if (r[1] == '-') |
4723 |
r++; |
4724 |
popt = qemu_options; |
4725 |
for(;;) {
|
4726 |
if (!popt->name) {
|
4727 |
fprintf(stderr, "%s: invalid option -- '%s'\n",
|
4728 |
argv[0], r);
|
4729 |
exit(1);
|
4730 |
} |
4731 |
if (!strcmp(popt->name, r + 1)) |
4732 |
break;
|
4733 |
popt++; |
4734 |
} |
4735 |
if (popt->flags & HAS_ARG) {
|
4736 |
if (optind >= argc) {
|
4737 |
fprintf(stderr, "%s: option '%s' requires an argument\n",
|
4738 |
argv[0], r);
|
4739 |
exit(1);
|
4740 |
} |
4741 |
optarg = argv[optind++]; |
4742 |
} else {
|
4743 |
optarg = NULL;
|
4744 |
} |
4745 |
|
4746 |
switch(popt->index) {
|
4747 |
case QEMU_OPTION_M:
|
4748 |
machine = find_machine(optarg); |
4749 |
if (!machine) {
|
4750 |
QEMUMachine *m; |
4751 |
printf("Supported machines are:\n");
|
4752 |
for(m = first_machine; m != NULL; m = m->next) { |
4753 |
if (m->alias)
|
4754 |
printf("%-10s %s (alias of %s)\n",
|
4755 |
m->alias, m->desc, m->name); |
4756 |
printf("%-10s %s%s\n",
|
4757 |
m->name, m->desc, |
4758 |
m->is_default ? " (default)" : ""); |
4759 |
} |
4760 |
exit(*optarg != '?');
|
4761 |
} |
4762 |
break;
|
4763 |
case QEMU_OPTION_cpu:
|
4764 |
/* hw initialization will check this */
|
4765 |
if (*optarg == '?') { |
4766 |
/* XXX: implement xxx_cpu_list for targets that still miss it */
|
4767 |
#if defined(cpu_list)
|
4768 |
cpu_list(stdout, &fprintf); |
4769 |
#endif
|
4770 |
exit(0);
|
4771 |
} else {
|
4772 |
cpu_model = optarg; |
4773 |
} |
4774 |
break;
|
4775 |
case QEMU_OPTION_initrd:
|
4776 |
initrd_filename = optarg; |
4777 |
break;
|
4778 |
case QEMU_OPTION_hda:
|
4779 |
if (cyls == 0) |
4780 |
hda_opts = drive_add(optarg, HD_ALIAS, 0);
|
4781 |
else
|
4782 |
hda_opts = drive_add(optarg, HD_ALIAS |
4783 |
",cyls=%d,heads=%d,secs=%d%s",
|
4784 |
0, cyls, heads, secs,
|
4785 |
translation == BIOS_ATA_TRANSLATION_LBA ? |
4786 |
",trans=lba" :
|
4787 |
translation == BIOS_ATA_TRANSLATION_NONE ? |
4788 |
",trans=none" : ""); |
4789 |
break;
|
4790 |
case QEMU_OPTION_hdb:
|
4791 |
case QEMU_OPTION_hdc:
|
4792 |
case QEMU_OPTION_hdd:
|
4793 |
drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
4794 |
break;
|
4795 |
case QEMU_OPTION_drive:
|
4796 |
drive_add(NULL, "%s", optarg); |
4797 |
break;
|
4798 |
case QEMU_OPTION_set:
|
4799 |
if (qemu_set_option(optarg) != 0) |
4800 |
exit(1);
|
4801 |
break;
|
4802 |
case QEMU_OPTION_mtdblock:
|
4803 |
drive_add(optarg, MTD_ALIAS); |
4804 |
break;
|
4805 |
case QEMU_OPTION_sd:
|
4806 |
drive_add(optarg, SD_ALIAS); |
4807 |
break;
|
4808 |
case QEMU_OPTION_pflash:
|
4809 |
drive_add(optarg, PFLASH_ALIAS); |
4810 |
break;
|
4811 |
case QEMU_OPTION_snapshot:
|
4812 |
snapshot = 1;
|
4813 |
break;
|
4814 |
case QEMU_OPTION_hdachs:
|
4815 |
{ |
4816 |
const char *p; |
4817 |
p = optarg; |
4818 |
cyls = strtol(p, (char **)&p, 0); |
4819 |
if (cyls < 1 || cyls > 16383) |
4820 |
goto chs_fail;
|
4821 |
if (*p != ',') |
4822 |
goto chs_fail;
|
4823 |
p++; |
4824 |
heads = strtol(p, (char **)&p, 0); |
4825 |
if (heads < 1 || heads > 16) |
4826 |
goto chs_fail;
|
4827 |
if (*p != ',') |
4828 |
goto chs_fail;
|
4829 |
p++; |
4830 |
secs = strtol(p, (char **)&p, 0); |
4831 |
if (secs < 1 || secs > 63) |
4832 |
goto chs_fail;
|
4833 |
if (*p == ',') { |
4834 |
p++; |
4835 |
if (!strcmp(p, "none")) |
4836 |
translation = BIOS_ATA_TRANSLATION_NONE; |
4837 |
else if (!strcmp(p, "lba")) |
4838 |
translation = BIOS_ATA_TRANSLATION_LBA; |
4839 |
else if (!strcmp(p, "auto")) |
4840 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
4841 |
else
|
4842 |
goto chs_fail;
|
4843 |
} else if (*p != '\0') { |
4844 |
chs_fail:
|
4845 |
fprintf(stderr, "qemu: invalid physical CHS format\n");
|
4846 |
exit(1);
|
4847 |
} |
4848 |
if (hda_opts != NULL) { |
4849 |
char num[16]; |
4850 |
snprintf(num, sizeof(num), "%d", cyls); |
4851 |
qemu_opt_set(hda_opts, "cyls", num);
|
4852 |
snprintf(num, sizeof(num), "%d", heads); |
4853 |
qemu_opt_set(hda_opts, "heads", num);
|
4854 |
snprintf(num, sizeof(num), "%d", secs); |
4855 |
qemu_opt_set(hda_opts, "secs", num);
|
4856 |
if (translation == BIOS_ATA_TRANSLATION_LBA)
|
4857 |
qemu_opt_set(hda_opts, "trans", "lba"); |
4858 |
if (translation == BIOS_ATA_TRANSLATION_NONE)
|
4859 |
qemu_opt_set(hda_opts, "trans", "none"); |
4860 |
} |
4861 |
} |
4862 |
break;
|
4863 |
case QEMU_OPTION_numa:
|
4864 |
if (nb_numa_nodes >= MAX_NODES) {
|
4865 |
fprintf(stderr, "qemu: too many NUMA nodes\n");
|
4866 |
exit(1);
|
4867 |
} |
4868 |
numa_add(optarg); |
4869 |
break;
|
4870 |
case QEMU_OPTION_nographic:
|
4871 |
display_type = DT_NOGRAPHIC; |
4872 |
break;
|
4873 |
#ifdef CONFIG_CURSES
|
4874 |
case QEMU_OPTION_curses:
|
4875 |
display_type = DT_CURSES; |
4876 |
break;
|
4877 |
#endif
|
4878 |
case QEMU_OPTION_portrait:
|
4879 |
graphic_rotate = 1;
|
4880 |
break;
|
4881 |
case QEMU_OPTION_kernel:
|
4882 |
kernel_filename = optarg; |
4883 |
break;
|
4884 |
case QEMU_OPTION_append:
|
4885 |
kernel_cmdline = optarg; |
4886 |
break;
|
4887 |
case QEMU_OPTION_cdrom:
|
4888 |
drive_add(optarg, CDROM_ALIAS); |
4889 |
break;
|
4890 |
case QEMU_OPTION_boot:
|
4891 |
{ |
4892 |
static const char * const params[] = { |
4893 |
"order", "once", "menu", NULL |
4894 |
}; |
4895 |
char buf[sizeof(boot_devices)]; |
4896 |
char *standard_boot_devices;
|
4897 |
int legacy = 0; |
4898 |
|
4899 |
if (!strchr(optarg, '=')) { |
4900 |
legacy = 1;
|
4901 |
pstrcpy(buf, sizeof(buf), optarg);
|
4902 |
} else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
4903 |
fprintf(stderr, |
4904 |
"qemu: unknown boot parameter '%s' in '%s'\n",
|
4905 |
buf, optarg); |
4906 |
exit(1);
|
4907 |
} |
4908 |
|
4909 |
if (legacy ||
|
4910 |
get_param_value(buf, sizeof(buf), "order", optarg)) { |
4911 |
boot_devices_bitmap = parse_bootdevices(buf); |
4912 |
pstrcpy(boot_devices, sizeof(boot_devices), buf);
|
4913 |
} |
4914 |
if (!legacy) {
|
4915 |
if (get_param_value(buf, sizeof(buf), |
4916 |
"once", optarg)) {
|
4917 |
boot_devices_bitmap |= parse_bootdevices(buf); |
4918 |
standard_boot_devices = qemu_strdup(boot_devices); |
4919 |
pstrcpy(boot_devices, sizeof(boot_devices), buf);
|
4920 |
qemu_register_reset(restore_boot_devices, |
4921 |
standard_boot_devices); |
4922 |
} |
4923 |
if (get_param_value(buf, sizeof(buf), |
4924 |
"menu", optarg)) {
|
4925 |
if (!strcmp(buf, "on")) { |
4926 |
boot_menu = 1;
|
4927 |
} else if (!strcmp(buf, "off")) { |
4928 |
boot_menu = 0;
|
4929 |
} else {
|
4930 |
fprintf(stderr, |
4931 |
"qemu: invalid option value '%s'\n",
|
4932 |
buf); |
4933 |
exit(1);
|
4934 |
} |
4935 |
} |
4936 |
} |
4937 |
} |
4938 |
break;
|
4939 |
case QEMU_OPTION_fda:
|
4940 |
case QEMU_OPTION_fdb:
|
4941 |
drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
4942 |
break;
|
4943 |
#ifdef TARGET_I386
|
4944 |
case QEMU_OPTION_no_fd_bootchk:
|
4945 |
fd_bootchk = 0;
|
4946 |
break;
|
4947 |
#endif
|
4948 |
case QEMU_OPTION_netdev:
|
4949 |
if (net_client_parse(&qemu_netdev_opts, optarg) == -1) { |
4950 |
exit(1);
|
4951 |
} |
4952 |
break;
|
4953 |
case QEMU_OPTION_net:
|
4954 |
if (net_client_parse(&qemu_net_opts, optarg) == -1) { |
4955 |
exit(1);
|
4956 |
} |
4957 |
break;
|
4958 |
#ifdef CONFIG_SLIRP
|
4959 |
case QEMU_OPTION_tftp:
|
4960 |
legacy_tftp_prefix = optarg; |
4961 |
break;
|
4962 |
case QEMU_OPTION_bootp:
|
4963 |
legacy_bootp_filename = optarg; |
4964 |
break;
|
4965 |
#ifndef _WIN32
|
4966 |
case QEMU_OPTION_smb:
|
4967 |
if (net_slirp_smb(optarg) < 0) |
4968 |
exit(1);
|
4969 |
break;
|
4970 |
#endif
|
4971 |
case QEMU_OPTION_redir:
|
4972 |
if (net_slirp_redir(optarg) < 0) |
4973 |
exit(1);
|
4974 |
break;
|
4975 |
#endif
|
4976 |
case QEMU_OPTION_bt:
|
4977 |
add_device_config(DEV_BT, optarg); |
4978 |
break;
|
4979 |
#ifdef HAS_AUDIO
|
4980 |
case QEMU_OPTION_audio_help:
|
4981 |
AUD_help (); |
4982 |
exit (0);
|
4983 |
break;
|
4984 |
case QEMU_OPTION_soundhw:
|
4985 |
select_soundhw (optarg); |
4986 |
break;
|
4987 |
#endif
|
4988 |
case QEMU_OPTION_h:
|
4989 |
help(0);
|
4990 |
break;
|
4991 |
case QEMU_OPTION_version:
|
4992 |
version(); |
4993 |
exit(0);
|
4994 |
break;
|
4995 |
case QEMU_OPTION_m: {
|
4996 |
uint64_t value; |
4997 |
char *ptr;
|
4998 |
|
4999 |
value = strtoul(optarg, &ptr, 10);
|
5000 |
switch (*ptr) {
|
5001 |
case 0: case 'M': case 'm': |
5002 |
value <<= 20;
|
5003 |
break;
|
5004 |
case 'G': case 'g': |
5005 |
value <<= 30;
|
5006 |
break;
|
5007 |
default:
|
5008 |
fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
5009 |
exit(1);
|
5010 |
} |
5011 |
|
5012 |
/* On 32-bit hosts, QEMU is limited by virtual address space */
|
5013 |
if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
5014 |
fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
5015 |
exit(1);
|
5016 |
} |
5017 |
if (value != (uint64_t)(ram_addr_t)value) {
|
5018 |
fprintf(stderr, "qemu: ram size too large\n");
|
5019 |
exit(1);
|
5020 |
} |
5021 |
ram_size = value; |
5022 |
break;
|
5023 |
} |
5024 |
case QEMU_OPTION_d:
|
5025 |
{ |
5026 |
int mask;
|
5027 |
const CPULogItem *item;
|
5028 |
|
5029 |
mask = cpu_str_to_log_mask(optarg); |
5030 |
if (!mask) {
|
5031 |
printf("Log items (comma separated):\n");
|
5032 |
for(item = cpu_log_items; item->mask != 0; item++) { |
5033 |
printf("%-10s %s\n", item->name, item->help);
|
5034 |
} |
5035 |
exit(1);
|
5036 |
} |
5037 |
cpu_set_log(mask); |
5038 |
} |
5039 |
break;
|
5040 |
case QEMU_OPTION_s:
|
5041 |
gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
|
5042 |
break;
|
5043 |
case QEMU_OPTION_gdb:
|
5044 |
gdbstub_dev = optarg; |
5045 |
break;
|
5046 |
case QEMU_OPTION_L:
|
5047 |
data_dir = optarg; |
5048 |
break;
|
5049 |
case QEMU_OPTION_bios:
|
5050 |
bios_name = optarg; |
5051 |
break;
|
5052 |
case QEMU_OPTION_singlestep:
|
5053 |
singlestep = 1;
|
5054 |
break;
|
5055 |
case QEMU_OPTION_S:
|
5056 |
autostart = 0;
|
5057 |
break;
|
5058 |
case QEMU_OPTION_k:
|
5059 |
keyboard_layout = optarg; |
5060 |
break;
|
5061 |
case QEMU_OPTION_localtime:
|
5062 |
rtc_utc = 0;
|
5063 |
break;
|
5064 |
case QEMU_OPTION_vga:
|
5065 |
select_vgahw (optarg); |
5066 |
break;
|
5067 |
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
5068 |
case QEMU_OPTION_g:
|
5069 |
{ |
5070 |
const char *p; |
5071 |
int w, h, depth;
|
5072 |
p = optarg; |
5073 |
w = strtol(p, (char **)&p, 10); |
5074 |
if (w <= 0) { |
5075 |
graphic_error:
|
5076 |
fprintf(stderr, "qemu: invalid resolution or depth\n");
|
5077 |
exit(1);
|
5078 |
} |
5079 |
if (*p != 'x') |
5080 |
goto graphic_error;
|
5081 |
p++; |
5082 |
h = strtol(p, (char **)&p, 10); |
5083 |
if (h <= 0) |
5084 |
goto graphic_error;
|
5085 |
if (*p == 'x') { |
5086 |
p++; |
5087 |
depth = strtol(p, (char **)&p, 10); |
5088 |
if (depth != 8 && depth != 15 && depth != 16 && |
5089 |
depth != 24 && depth != 32) |
5090 |
goto graphic_error;
|
5091 |
} else if (*p == '\0') { |
5092 |
depth = graphic_depth; |
5093 |
} else {
|
5094 |
goto graphic_error;
|
5095 |
} |
5096 |
|
5097 |
graphic_width = w; |
5098 |
graphic_height = h; |
5099 |
graphic_depth = depth; |
5100 |
} |
5101 |
break;
|
5102 |
#endif
|
5103 |
case QEMU_OPTION_echr:
|
5104 |
{ |
5105 |
char *r;
|
5106 |
term_escape_char = strtol(optarg, &r, 0);
|
5107 |
if (r == optarg)
|
5108 |
printf("Bad argument to echr\n");
|
5109 |
break;
|
5110 |
} |
5111 |
case QEMU_OPTION_monitor:
|
5112 |
if (monitor_device_index >= MAX_MONITOR_DEVICES) {
|
5113 |
fprintf(stderr, "qemu: too many monitor devices\n");
|
5114 |
exit(1);
|
5115 |
} |
5116 |
monitor_devices[monitor_device_index] = |
5117 |
monitor_cmdline_parse(optarg, |
5118 |
&monitor_flags[monitor_device_index]); |
5119 |
monitor_device_index++; |
5120 |
break;
|
5121 |
case QEMU_OPTION_chardev:
|
5122 |
opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
|
5123 |
if (!opts) {
|
5124 |
fprintf(stderr, "parse error: %s\n", optarg);
|
5125 |
exit(1);
|
5126 |
} |
5127 |
if (qemu_chr_open_opts(opts, NULL) == NULL) { |
5128 |
exit(1);
|
5129 |
} |
5130 |
break;
|
5131 |
case QEMU_OPTION_serial:
|
5132 |
if (serial_device_index >= MAX_SERIAL_PORTS) {
|
5133 |
fprintf(stderr, "qemu: too many serial ports\n");
|
5134 |
exit(1);
|
5135 |
} |
5136 |
serial_devices[serial_device_index] = optarg; |
5137 |
serial_device_index++; |
5138 |
break;
|
5139 |
case QEMU_OPTION_watchdog:
|
5140 |
if (watchdog) {
|
5141 |
fprintf(stderr, |
5142 |
"qemu: only one watchdog option may be given\n");
|
5143 |
return 1; |
5144 |
} |
5145 |
watchdog = optarg; |
5146 |
break;
|
5147 |
case QEMU_OPTION_watchdog_action:
|
5148 |
if (select_watchdog_action(optarg) == -1) { |
5149 |
fprintf(stderr, "Unknown -watchdog-action parameter\n");
|
5150 |
exit(1);
|
5151 |
} |
5152 |
break;
|
5153 |
case QEMU_OPTION_virtiocon:
|
5154 |
if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
|
5155 |
fprintf(stderr, "qemu: too many virtio consoles\n");
|
5156 |
exit(1);
|
5157 |
} |
5158 |
virtio_consoles[virtio_console_index] = optarg; |
5159 |
virtio_console_index++; |
5160 |
break;
|
5161 |
case QEMU_OPTION_parallel:
|
5162 |
if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
5163 |
fprintf(stderr, "qemu: too many parallel ports\n");
|
5164 |
exit(1);
|
5165 |
} |
5166 |
parallel_devices[parallel_device_index] = optarg; |
5167 |
parallel_device_index++; |
5168 |
break;
|
5169 |
case QEMU_OPTION_loadvm:
|
5170 |
loadvm = optarg; |
5171 |
break;
|
5172 |
case QEMU_OPTION_full_screen:
|
5173 |
full_screen = 1;
|
5174 |
break;
|
5175 |
#ifdef CONFIG_SDL
|
5176 |
case QEMU_OPTION_no_frame:
|
5177 |
no_frame = 1;
|
5178 |
break;
|
5179 |
case QEMU_OPTION_alt_grab:
|
5180 |
alt_grab = 1;
|
5181 |
break;
|
5182 |
case QEMU_OPTION_ctrl_grab:
|
5183 |
ctrl_grab = 1;
|
5184 |
break;
|
5185 |
case QEMU_OPTION_no_quit:
|
5186 |
no_quit = 1;
|
5187 |
break;
|
5188 |
case QEMU_OPTION_sdl:
|
5189 |
display_type = DT_SDL; |
5190 |
break;
|
5191 |
#endif
|
5192 |
case QEMU_OPTION_pidfile:
|
5193 |
pid_file = optarg; |
5194 |
break;
|
5195 |
#ifdef TARGET_I386
|
5196 |
case QEMU_OPTION_win2k_hack:
|
5197 |
win2k_install_hack = 1;
|
5198 |
break;
|
5199 |
case QEMU_OPTION_rtc_td_hack:
|
5200 |
rtc_td_hack = 1;
|
5201 |
break;
|
5202 |
case QEMU_OPTION_acpitable:
|
5203 |
if(acpi_table_add(optarg) < 0) { |
5204 |
fprintf(stderr, "Wrong acpi table provided\n");
|
5205 |
exit(1);
|
5206 |
} |
5207 |
break;
|
5208 |
case QEMU_OPTION_smbios:
|
5209 |
if(smbios_entry_add(optarg) < 0) { |
5210 |
fprintf(stderr, "Wrong smbios provided\n");
|
5211 |
exit(1);
|
5212 |
} |
5213 |
break;
|
5214 |
#endif
|
5215 |
#ifdef CONFIG_KVM
|
5216 |
case QEMU_OPTION_enable_kvm:
|
5217 |
kvm_allowed = 1;
|
5218 |
break;
|
5219 |
#endif
|
5220 |
case QEMU_OPTION_usb:
|
5221 |
usb_enabled = 1;
|
5222 |
break;
|
5223 |
case QEMU_OPTION_usbdevice:
|
5224 |
usb_enabled = 1;
|
5225 |
add_device_config(DEV_USB, optarg); |
5226 |
break;
|
5227 |
case QEMU_OPTION_device:
|
5228 |
if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) { |
5229 |
exit(1);
|
5230 |
} |
5231 |
break;
|
5232 |
case QEMU_OPTION_smp:
|
5233 |
smp_parse(optarg); |
5234 |
if (smp_cpus < 1) { |
5235 |
fprintf(stderr, "Invalid number of CPUs\n");
|
5236 |
exit(1);
|
5237 |
} |
5238 |
if (max_cpus < smp_cpus) {
|
5239 |
fprintf(stderr, "maxcpus must be equal to or greater than "
|
5240 |
"smp\n");
|
5241 |
exit(1);
|
5242 |
} |
5243 |
if (max_cpus > 255) { |
5244 |
fprintf(stderr, "Unsupported number of maxcpus\n");
|
5245 |
exit(1);
|
5246 |
} |
5247 |
break;
|
5248 |
case QEMU_OPTION_vnc:
|
5249 |
display_type = DT_VNC; |
5250 |
vnc_display = optarg; |
5251 |
break;
|
5252 |
#ifdef TARGET_I386
|
5253 |
case QEMU_OPTION_no_acpi:
|
5254 |
acpi_enabled = 0;
|
5255 |
break;
|
5256 |
case QEMU_OPTION_no_hpet:
|
5257 |
no_hpet = 1;
|
5258 |
break;
|
5259 |
case QEMU_OPTION_balloon:
|
5260 |
if (balloon_parse(optarg) < 0) { |
5261 |
fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
|
5262 |
exit(1);
|
5263 |
} |
5264 |
break;
|
5265 |
#endif
|
5266 |
case QEMU_OPTION_no_reboot:
|
5267 |
no_reboot = 1;
|
5268 |
break;
|
5269 |
case QEMU_OPTION_no_shutdown:
|
5270 |
no_shutdown = 1;
|
5271 |
break;
|
5272 |
case QEMU_OPTION_show_cursor:
|
5273 |
cursor_hide = 0;
|
5274 |
break;
|
5275 |
case QEMU_OPTION_uuid:
|
5276 |
if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
5277 |
fprintf(stderr, "Fail to parse UUID string."
|
5278 |
" Wrong format.\n");
|
5279 |
exit(1);
|
5280 |
} |
5281 |
break;
|
5282 |
#ifndef _WIN32
|
5283 |
case QEMU_OPTION_daemonize:
|
5284 |
daemonize = 1;
|
5285 |
break;
|
5286 |
#endif
|
5287 |
case QEMU_OPTION_option_rom:
|
5288 |
if (nb_option_roms >= MAX_OPTION_ROMS) {
|
5289 |
fprintf(stderr, "Too many option ROMs\n");
|
5290 |
exit(1);
|
5291 |
} |
5292 |
option_rom[nb_option_roms] = optarg; |
5293 |
nb_option_roms++; |
5294 |
break;
|
5295 |
#if defined(TARGET_ARM) || defined(TARGET_M68K)
|
5296 |
case QEMU_OPTION_semihosting:
|
5297 |
semihosting_enabled = 1;
|
5298 |
break;
|
5299 |
#endif
|
5300 |
case QEMU_OPTION_name:
|
5301 |
qemu_name = qemu_strdup(optarg); |
5302 |
{ |
5303 |
char *p = strchr(qemu_name, ','); |
5304 |
if (p != NULL) { |
5305 |
*p++ = 0;
|
5306 |
if (strncmp(p, "process=", 8)) { |
5307 |
fprintf(stderr, "Unknown subargument %s to -name", p);
|
5308 |
exit(1);
|
5309 |
} |
5310 |
p += 8;
|
5311 |
set_proc_name(p); |
5312 |
} |
5313 |
} |
5314 |
break;
|
5315 |
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
|
5316 |
case QEMU_OPTION_prom_env:
|
5317 |
if (nb_prom_envs >= MAX_PROM_ENVS) {
|
5318 |
fprintf(stderr, "Too many prom variables\n");
|
5319 |
exit(1);
|
5320 |
} |
5321 |
prom_envs[nb_prom_envs] = optarg; |
5322 |
nb_prom_envs++; |
5323 |
break;
|
5324 |
#endif
|
5325 |
#ifdef TARGET_ARM
|
5326 |
case QEMU_OPTION_old_param:
|
5327 |
old_param = 1;
|
5328 |
break;
|
5329 |
#endif
|
5330 |
case QEMU_OPTION_clock:
|
5331 |
configure_alarms(optarg); |
5332 |
break;
|
5333 |
case QEMU_OPTION_startdate:
|
5334 |
configure_rtc_date_offset(optarg, 1);
|
5335 |
break;
|
5336 |
case QEMU_OPTION_rtc:
|
5337 |
opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
|
5338 |
if (!opts) {
|
5339 |
fprintf(stderr, "parse error: %s\n", optarg);
|
5340 |
exit(1);
|
5341 |
} |
5342 |
configure_rtc(opts); |
5343 |
break;
|
5344 |
case QEMU_OPTION_tb_size:
|
5345 |
tb_size = strtol(optarg, NULL, 0); |
5346 |
if (tb_size < 0) |
5347 |
tb_size = 0;
|
5348 |
break;
|
5349 |
case QEMU_OPTION_icount:
|
5350 |
use_icount = 1;
|
5351 |
if (strcmp(optarg, "auto") == 0) { |
5352 |
icount_time_shift = -1;
|
5353 |
} else {
|
5354 |
icount_time_shift = strtol(optarg, NULL, 0); |
5355 |
} |
5356 |
break;
|
5357 |
case QEMU_OPTION_incoming:
|
5358 |
incoming = optarg; |
5359 |
break;
|
5360 |
#ifndef _WIN32
|
5361 |
case QEMU_OPTION_chroot:
|
5362 |
chroot_dir = optarg; |
5363 |
break;
|
5364 |
case QEMU_OPTION_runas:
|
5365 |
run_as = optarg; |
5366 |
break;
|
5367 |
#endif
|
5368 |
#ifdef CONFIG_XEN
|
5369 |
case QEMU_OPTION_xen_domid:
|
5370 |
xen_domid = atoi(optarg); |
5371 |
break;
|
5372 |
case QEMU_OPTION_xen_create:
|
5373 |
xen_mode = XEN_CREATE; |
5374 |
break;
|
5375 |
case QEMU_OPTION_xen_attach:
|
5376 |
xen_mode = XEN_ATTACH; |
5377 |
break;
|
5378 |
#endif
|
5379 |
case QEMU_OPTION_readconfig:
|
5380 |
{ |
5381 |
FILE *fp; |
5382 |
fp = fopen(optarg, "r");
|
5383 |
if (fp == NULL) { |
5384 |
fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
|
5385 |
exit(1);
|
5386 |
} |
5387 |
if (qemu_config_parse(fp) != 0) { |
5388 |
exit(1);
|
5389 |
} |
5390 |
fclose(fp); |
5391 |
break;
|
5392 |
} |
5393 |
case QEMU_OPTION_writeconfig:
|
5394 |
{ |
5395 |
FILE *fp; |
5396 |
if (strcmp(optarg, "-") == 0) { |
5397 |
fp = stdout; |
5398 |
} else {
|
5399 |
fp = fopen(optarg, "w");
|
5400 |
if (fp == NULL) { |
5401 |
fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
|
5402 |
exit(1);
|
5403 |
} |
5404 |
} |
5405 |
qemu_config_write(fp); |
5406 |
fclose(fp); |
5407 |
break;
|
5408 |
} |
5409 |
} |
5410 |
} |
5411 |
} |
5412 |
|
5413 |
/* If no data_dir is specified then try to find it relative to the
|
5414 |
executable path. */
|
5415 |
if (!data_dir) {
|
5416 |
data_dir = find_datadir(argv[0]);
|
5417 |
} |
5418 |
/* If all else fails use the install patch specified when building. */
|
5419 |
if (!data_dir) {
|
5420 |
data_dir = CONFIG_QEMU_SHAREDIR; |
5421 |
} |
5422 |
|
5423 |
/*
|
5424 |
* Default to max_cpus = smp_cpus, in case the user doesn't
|
5425 |
* specify a max_cpus value.
|
5426 |
*/
|
5427 |
if (!max_cpus)
|
5428 |
max_cpus = smp_cpus; |
5429 |
|
5430 |
machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
5431 |
if (smp_cpus > machine->max_cpus) {
|
5432 |
fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
|
5433 |
"supported by machine `%s' (%d)\n", smp_cpus, machine->name,
|
5434 |
machine->max_cpus); |
5435 |
exit(1);
|
5436 |
} |
5437 |
|
5438 |
if (display_type == DT_NOGRAPHIC) {
|
5439 |
if (serial_device_index == 0) |
5440 |
serial_devices[0] = "stdio"; |
5441 |
if (parallel_device_index == 0) |
5442 |
parallel_devices[0] = "null"; |
5443 |
if (strncmp(monitor_devices[0], "vc", 2) == 0) { |
5444 |
monitor_devices[0] = "stdio"; |
5445 |
} |
5446 |
} |
5447 |
|
5448 |
#ifndef _WIN32
|
5449 |
if (daemonize) {
|
5450 |
pid_t pid; |
5451 |
|
5452 |
if (pipe(fds) == -1) |
5453 |
exit(1);
|
5454 |
|
5455 |
pid = fork(); |
5456 |
if (pid > 0) { |
5457 |
uint8_t status; |
5458 |
ssize_t len; |
5459 |
|
5460 |
close(fds[1]);
|
5461 |
|
5462 |
again:
|
5463 |
len = read(fds[0], &status, 1); |
5464 |
if (len == -1 && (errno == EINTR)) |
5465 |
goto again;
|
5466 |
|
5467 |
if (len != 1) |
5468 |
exit(1);
|
5469 |
else if (status == 1) { |
5470 |
fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
|
5471 |
exit(1);
|
5472 |
} else
|
5473 |
exit(0);
|
5474 |
} else if (pid < 0) |
5475 |
exit(1);
|
5476 |
|
5477 |
setsid(); |
5478 |
|
5479 |
pid = fork(); |
5480 |
if (pid > 0) |
5481 |
exit(0);
|
5482 |
else if (pid < 0) |
5483 |
exit(1);
|
5484 |
|
5485 |
umask(027);
|
5486 |
|
5487 |
signal(SIGTSTP, SIG_IGN); |
5488 |
signal(SIGTTOU, SIG_IGN); |
5489 |
signal(SIGTTIN, SIG_IGN); |
5490 |
} |
5491 |
|
5492 |
if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
5493 |
if (daemonize) {
|
5494 |
uint8_t status = 1;
|
5495 |
write(fds[1], &status, 1); |
5496 |
} else
|
5497 |
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
|
5498 |
exit(1);
|
5499 |
} |
5500 |
#endif
|
5501 |
|
5502 |
if (kvm_enabled()) {
|
5503 |
int ret;
|
5504 |
|
5505 |
ret = kvm_init(smp_cpus); |
5506 |
if (ret < 0) { |
5507 |
fprintf(stderr, "failed to initialize KVM\n");
|
5508 |
exit(1);
|
5509 |
} |
5510 |
} |
5511 |
|
5512 |
if (qemu_init_main_loop()) {
|
5513 |
fprintf(stderr, "qemu_init_main_loop failed\n");
|
5514 |
exit(1);
|
5515 |
} |
5516 |
linux_boot = (kernel_filename != NULL);
|
5517 |
|
5518 |
if (!linux_boot && *kernel_cmdline != '\0') { |
5519 |
fprintf(stderr, "-append only allowed with -kernel option\n");
|
5520 |
exit(1);
|
5521 |
} |
5522 |
|
5523 |
if (!linux_boot && initrd_filename != NULL) { |
5524 |
fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
5525 |
exit(1);
|
5526 |
} |
5527 |
|
5528 |
#ifndef _WIN32
|
5529 |
/* Win32 doesn't support line-buffering and requires size >= 2 */
|
5530 |
setvbuf(stdout, NULL, _IOLBF, 0); |
5531 |
#endif
|
5532 |
|
5533 |
if (init_timer_alarm() < 0) { |
5534 |
fprintf(stderr, "could not initialize alarm timer\n");
|
5535 |
exit(1);
|
5536 |
} |
5537 |
if (use_icount && icount_time_shift < 0) { |
5538 |
use_icount = 2;
|
5539 |
/* 125MIPS seems a reasonable initial guess at the guest speed.
|
5540 |
It will be corrected fairly quickly anyway. */
|
5541 |
icount_time_shift = 3;
|
5542 |
init_icount_adjust(); |
5543 |
} |
5544 |
|
5545 |
#ifdef _WIN32
|
5546 |
socket_init(); |
5547 |
#endif
|
5548 |
|
5549 |
if (net_init_clients() < 0) { |
5550 |
exit(1);
|
5551 |
} |
5552 |
|
5553 |
net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
5554 |
net_set_boot_mask(net_boot); |
5555 |
|
5556 |
/* init the bluetooth world */
|
5557 |
if (foreach_device_config(DEV_BT, bt_parse))
|
5558 |
exit(1);
|
5559 |
|
5560 |
/* init the memory */
|
5561 |
if (ram_size == 0) |
5562 |
ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
5563 |
|
5564 |
/* init the dynamic translator */
|
5565 |
cpu_exec_init_all(tb_size * 1024 * 1024); |
5566 |
|
5567 |
bdrv_init_with_whitelist(); |
5568 |
|
5569 |
blk_mig_init(); |
5570 |
|
5571 |
/* we always create the cdrom drive, even if no disk is there */
|
5572 |
drive_add(NULL, CDROM_ALIAS);
|
5573 |
|
5574 |
/* we always create at least one floppy */
|
5575 |
drive_add(NULL, FD_ALIAS, 0); |
5576 |
|
5577 |
/* we always create one sd slot, even if no card is in it */
|
5578 |
drive_add(NULL, SD_ALIAS);
|
5579 |
|
5580 |
/* open the virtual block devices */
|
5581 |
if (snapshot)
|
5582 |
qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
5583 |
if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0) |
5584 |
exit(1);
|
5585 |
|
5586 |
vmstate_register(0, &vmstate_timers ,&timers_state);
|
5587 |
register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, |
5588 |
ram_load, NULL);
|
5589 |
|
5590 |
/* Maintain compatibility with multiple stdio monitors */
|
5591 |
if (!strcmp(monitor_devices[0],"stdio")) { |
5592 |
for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
5593 |
const char *devname = serial_devices[i]; |
5594 |
if (devname && !strcmp(devname,"mon:stdio")) { |
5595 |
monitor_devices[0] = NULL; |
5596 |
break;
|
5597 |
} else if (devname && !strcmp(devname,"stdio")) { |
5598 |
monitor_devices[0] = NULL; |
5599 |
serial_devices[i] = "mon:stdio";
|
5600 |
break;
|
5601 |
} |
5602 |
} |
5603 |
} |
5604 |
|
5605 |
if (nb_numa_nodes > 0) { |
5606 |
int i;
|
5607 |
|
5608 |
if (nb_numa_nodes > smp_cpus) {
|
5609 |
nb_numa_nodes = smp_cpus; |
5610 |
} |
5611 |
|
5612 |
/* If no memory size if given for any node, assume the default case
|
5613 |
* and distribute the available memory equally across all nodes
|
5614 |
*/
|
5615 |
for (i = 0; i < nb_numa_nodes; i++) { |
5616 |
if (node_mem[i] != 0) |
5617 |
break;
|
5618 |
} |
5619 |
if (i == nb_numa_nodes) {
|
5620 |
uint64_t usedmem = 0;
|
5621 |
|
5622 |
/* On Linux, the each node's border has to be 8MB aligned,
|
5623 |
* the final node gets the rest.
|
5624 |
*/
|
5625 |
for (i = 0; i < nb_numa_nodes - 1; i++) { |
5626 |
node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
5627 |
usedmem += node_mem[i]; |
5628 |
} |
5629 |
node_mem[i] = ram_size - usedmem; |
5630 |
} |
5631 |
|
5632 |
for (i = 0; i < nb_numa_nodes; i++) { |
5633 |
if (node_cpumask[i] != 0) |
5634 |
break;
|
5635 |
} |
5636 |
/* assigning the VCPUs round-robin is easier to implement, guest OSes
|
5637 |
* must cope with this anyway, because there are BIOSes out there in
|
5638 |
* real machines which also use this scheme.
|
5639 |
*/
|
5640 |
if (i == nb_numa_nodes) {
|
5641 |
for (i = 0; i < smp_cpus; i++) { |
5642 |
node_cpumask[i % nb_numa_nodes] |= 1 << i;
|
5643 |
} |
5644 |
} |
5645 |
} |
5646 |
|
5647 |
for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
5648 |
const char *devname = monitor_devices[i]; |
5649 |
if (devname && strcmp(devname, "none")) { |
5650 |
char label[32]; |
5651 |
if (i == 0) { |
5652 |
snprintf(label, sizeof(label), "monitor"); |
5653 |
} else {
|
5654 |
snprintf(label, sizeof(label), "monitor%d", i); |
5655 |
} |
5656 |
monitor_hds[i] = qemu_chr_open(label, devname, NULL);
|
5657 |
if (!monitor_hds[i]) {
|
5658 |
fprintf(stderr, "qemu: could not open monitor device '%s'\n",
|
5659 |
devname); |
5660 |
exit(1);
|
5661 |
} |
5662 |
} |
5663 |
} |
5664 |
|
5665 |
for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
5666 |
const char *devname = serial_devices[i]; |
5667 |
if (devname && strcmp(devname, "none")) { |
5668 |
char label[32]; |
5669 |
snprintf(label, sizeof(label), "serial%d", i); |
5670 |
serial_hds[i] = qemu_chr_open(label, devname, NULL);
|
5671 |
if (!serial_hds[i]) {
|
5672 |
fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
|
5673 |
devname, strerror(errno)); |
5674 |
exit(1);
|
5675 |
} |
5676 |
} |
5677 |
} |
5678 |
|
5679 |
for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
5680 |
const char *devname = parallel_devices[i]; |
5681 |
if (devname && strcmp(devname, "none")) { |
5682 |
char label[32]; |
5683 |
snprintf(label, sizeof(label), "parallel%d", i); |
5684 |
parallel_hds[i] = qemu_chr_open(label, devname, NULL);
|
5685 |
if (!parallel_hds[i]) {
|
5686 |
fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
|
5687 |
devname, strerror(errno)); |
5688 |
exit(1);
|
5689 |
} |
5690 |
} |
5691 |
} |
5692 |
|
5693 |
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
5694 |
const char *devname = virtio_consoles[i]; |
5695 |
if (devname && strcmp(devname, "none")) { |
5696 |
char label[32]; |
5697 |
snprintf(label, sizeof(label), "virtcon%d", i); |
5698 |
virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
|
5699 |
if (!virtcon_hds[i]) {
|
5700 |
fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
|
5701 |
devname, strerror(errno)); |
5702 |
exit(1);
|
5703 |
} |
5704 |
} |
5705 |
} |
5706 |
|
5707 |
module_call_init(MODULE_INIT_DEVICE); |
5708 |
|
5709 |
if (watchdog) {
|
5710 |
i = select_watchdog(watchdog); |
5711 |
if (i > 0) |
5712 |
exit (i == 1 ? 1 : 0); |
5713 |
} |
5714 |
|
5715 |
if (machine->compat_props) {
|
5716 |
qdev_prop_register_compat(machine->compat_props); |
5717 |
} |
5718 |
machine->init(ram_size, boot_devices, |
5719 |
kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
5720 |
|
5721 |
|
5722 |
#ifndef _WIN32
|
5723 |
/* must be after terminal init, SDL library changes signal handlers */
|
5724 |
sighandler_setup(); |
5725 |
#endif
|
5726 |
|
5727 |
for (env = first_cpu; env != NULL; env = env->next_cpu) { |
5728 |
for (i = 0; i < nb_numa_nodes; i++) { |
5729 |
if (node_cpumask[i] & (1 << env->cpu_index)) { |
5730 |
env->numa_node = i; |
5731 |
} |
5732 |
} |
5733 |
} |
5734 |
|
5735 |
current_machine = machine; |
5736 |
|
5737 |
/* init USB devices */
|
5738 |
if (usb_enabled) {
|
5739 |
if (foreach_device_config(DEV_USB, usb_parse) < 0) |
5740 |
exit(1);
|
5741 |
} |
5742 |
|
5743 |
/* init generic devices */
|
5744 |
if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0) |
5745 |
exit(1);
|
5746 |
|
5747 |
if (!display_state)
|
5748 |
dumb_display_init(); |
5749 |
/* just use the first displaystate for the moment */
|
5750 |
ds = display_state; |
5751 |
|
5752 |
if (display_type == DT_DEFAULT) {
|
5753 |
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
|
5754 |
display_type = DT_SDL; |
5755 |
#else
|
5756 |
display_type = DT_VNC; |
5757 |
vnc_display = "localhost:0,to=99";
|
5758 |
show_vnc_port = 1;
|
5759 |
#endif
|
5760 |
} |
5761 |
|
5762 |
|
5763 |
switch (display_type) {
|
5764 |
case DT_NOGRAPHIC:
|
5765 |
break;
|
5766 |
#if defined(CONFIG_CURSES)
|
5767 |
case DT_CURSES:
|
5768 |
curses_display_init(ds, full_screen); |
5769 |
break;
|
5770 |
#endif
|
5771 |
#if defined(CONFIG_SDL)
|
5772 |
case DT_SDL:
|
5773 |
sdl_display_init(ds, full_screen, no_frame); |
5774 |
break;
|
5775 |
#elif defined(CONFIG_COCOA)
|
5776 |
case DT_SDL:
|
5777 |
cocoa_display_init(ds, full_screen); |
5778 |
break;
|
5779 |
#endif
|
5780 |
case DT_VNC:
|
5781 |
vnc_display_init(ds); |
5782 |
if (vnc_display_open(ds, vnc_display) < 0) |
5783 |
exit(1);
|
5784 |
|
5785 |
if (show_vnc_port) {
|
5786 |
printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
|
5787 |
} |
5788 |
break;
|
5789 |
default:
|
5790 |
break;
|
5791 |
} |
5792 |
dpy_resize(ds); |
5793 |
|
5794 |
dcl = ds->listeners; |
5795 |
while (dcl != NULL) { |
5796 |
if (dcl->dpy_refresh != NULL) { |
5797 |
ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
5798 |
qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
5799 |
} |
5800 |
dcl = dcl->next; |
5801 |
} |
5802 |
|
5803 |
if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
|
5804 |
nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
|
5805 |
qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
5806 |
} |
5807 |
|
5808 |
text_consoles_set_display(display_state); |
5809 |
|
5810 |
for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
5811 |
if (monitor_devices[i] && monitor_hds[i]) {
|
5812 |
monitor_init(monitor_hds[i], monitor_flags[i]); |
5813 |
} |
5814 |
} |
5815 |
|
5816 |
for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
5817 |
const char *devname = serial_devices[i]; |
5818 |
if (devname && strcmp(devname, "none")) { |
5819 |
if (strstart(devname, "vc", 0)) |
5820 |
qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
5821 |
} |
5822 |
} |
5823 |
|
5824 |
for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
5825 |
const char *devname = parallel_devices[i]; |
5826 |
if (devname && strcmp(devname, "none")) { |
5827 |
if (strstart(devname, "vc", 0)) |
5828 |
qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
5829 |
} |
5830 |
} |
5831 |
|
5832 |
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
5833 |
const char *devname = virtio_consoles[i]; |
5834 |
if (virtcon_hds[i] && devname) {
|
5835 |
if (strstart(devname, "vc", 0)) |
5836 |
qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
|
5837 |
} |
5838 |
} |
5839 |
|
5840 |
if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
5841 |
fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
|
5842 |
gdbstub_dev); |
5843 |
exit(1);
|
5844 |
} |
5845 |
|
5846 |
qdev_machine_creation_done(); |
5847 |
|
5848 |
rom_load_all(); |
5849 |
|
5850 |
qemu_system_reset(); |
5851 |
if (loadvm) {
|
5852 |
if (load_vmstate(cur_mon, loadvm) < 0) { |
5853 |
autostart = 0;
|
5854 |
} |
5855 |
} |
5856 |
|
5857 |
if (incoming) {
|
5858 |
qemu_start_incoming_migration(incoming); |
5859 |
} else if (autostart) { |
5860 |
vm_start(); |
5861 |
} |
5862 |
|
5863 |
#ifndef _WIN32
|
5864 |
if (daemonize) {
|
5865 |
uint8_t status = 0;
|
5866 |
ssize_t len; |
5867 |
|
5868 |
again1:
|
5869 |
len = write(fds[1], &status, 1); |
5870 |
if (len == -1 && (errno == EINTR)) |
5871 |
goto again1;
|
5872 |
|
5873 |
if (len != 1) |
5874 |
exit(1);
|
5875 |
|
5876 |
chdir("/");
|
5877 |
TFR(fd = open("/dev/null", O_RDWR));
|
5878 |
if (fd == -1) |
5879 |
exit(1);
|
5880 |
} |
5881 |
|
5882 |
if (run_as) {
|
5883 |
pwd = getpwnam(run_as); |
5884 |
if (!pwd) {
|
5885 |
fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
|
5886 |
exit(1);
|
5887 |
} |
5888 |
} |
5889 |
|
5890 |
if (chroot_dir) {
|
5891 |
if (chroot(chroot_dir) < 0) { |
5892 |
fprintf(stderr, "chroot failed\n");
|
5893 |
exit(1);
|
5894 |
} |
5895 |
chdir("/");
|
5896 |
} |
5897 |
|
5898 |
if (run_as) {
|
5899 |
if (setgid(pwd->pw_gid) < 0) { |
5900 |
fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
|
5901 |
exit(1);
|
5902 |
} |
5903 |
if (setuid(pwd->pw_uid) < 0) { |
5904 |
fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
|
5905 |
exit(1);
|
5906 |
} |
5907 |
if (setuid(0) != -1) { |
5908 |
fprintf(stderr, "Dropping privileges failed\n");
|
5909 |
exit(1);
|
5910 |
} |
5911 |
} |
5912 |
|
5913 |
if (daemonize) {
|
5914 |
dup2(fd, 0);
|
5915 |
dup2(fd, 1);
|
5916 |
dup2(fd, 2);
|
5917 |
|
5918 |
close(fd); |
5919 |
} |
5920 |
#endif
|
5921 |
|
5922 |
main_loop(); |
5923 |
quit_timers(); |
5924 |
net_cleanup(); |
5925 |
|
5926 |
return 0; |
5927 |
} |