Statistics
| Branch: | Revision:

root / vl.c @ 1b48824f

History | View | Annotate | Download (157.6 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
#include "qemu-objects.h"
160

    
161
#include "disas.h"
162

    
163
#include "exec-all.h"
164

    
165
#include "qemu_socket.h"
166

    
167
#include "slirp/libslirp.h"
168

    
169
#include "qemu-queue.h"
170

    
171
//#define DEBUG_NET
172
//#define DEBUG_SLIRP
173

    
174
#define DEFAULT_RAM_SIZE 128
175

    
176
#define MAX_VIRTIO_CONSOLES 1
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
DisplayType display_type = DT_DEFAULT;
186
const char* keyboard_layout = NULL;
187
ram_addr_t ram_size;
188
const char *mem_path = NULL;
189
#ifdef MAP_POPULATE
190
int mem_prealloc = 0; /* force preallocation of physical target memory */
191
#endif
192
int nb_nics;
193
NICInfo nd_table[MAX_NICS];
194
int vm_running;
195
int autostart;
196
static int rtc_utc = 1;
197
static int rtc_date_offset = -1; /* -1 means no change */
198
QEMUClock *rtc_clock;
199
int vga_interface_type = VGA_NONE;
200
#ifdef TARGET_SPARC
201
int graphic_width = 1024;
202
int graphic_height = 768;
203
int graphic_depth = 8;
204
#else
205
int graphic_width = 800;
206
int graphic_height = 600;
207
int graphic_depth = 15;
208
#endif
209
static int full_screen = 0;
210
#ifdef CONFIG_SDL
211
static int no_frame = 0;
212
#endif
213
int no_quit = 0;
214
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
215
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
216
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
217
#ifdef TARGET_I386
218
int win2k_install_hack = 0;
219
int rtc_td_hack = 0;
220
#endif
221
int usb_enabled = 0;
222
int singlestep = 0;
223
int smp_cpus = 1;
224
int max_cpus = 0;
225
int smp_cores = 1;
226
int smp_threads = 1;
227
const char *vnc_display;
228
int acpi_enabled = 1;
229
int no_hpet = 0;
230
int fd_bootchk = 1;
231
int no_reboot = 0;
232
int no_shutdown = 0;
233
int cursor_hide = 1;
234
int graphic_rotate = 0;
235
uint8_t irq0override = 1;
236
#ifndef _WIN32
237
int daemonize = 0;
238
#endif
239
const char *watchdog;
240
const char *option_rom[MAX_OPTION_ROMS];
241
int nb_option_roms;
242
int semihosting_enabled = 0;
243
#ifdef TARGET_ARM
244
int old_param = 0;
245
#endif
246
const char *qemu_name;
247
int alt_grab = 0;
248
int ctrl_grab = 0;
249
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
250
unsigned int nb_prom_envs = 0;
251
const char *prom_envs[MAX_PROM_ENVS];
252
#endif
253
int boot_menu;
254

    
255
int nb_numa_nodes;
256
uint64_t node_mem[MAX_NODES];
257
uint64_t node_cpumask[MAX_NODES];
258

    
259
static CPUState *cur_cpu;
260
static CPUState *next_cpu;
261
/* Conversion factor from emulated instructions to virtual clock ticks.  */
262
static int icount_time_shift;
263
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
264
#define MAX_ICOUNT_SHIFT 10
265
/* Compensate for varying guest execution speed.  */
266
static int64_t qemu_icount_bias;
267
static QEMUTimer *icount_rt_timer;
268
static QEMUTimer *icount_vm_timer;
269
static QEMUTimer *nographic_timer;
270

    
271
uint8_t qemu_uuid[16];
272

    
273
static QEMUBootSetHandler *boot_set_handler;
274
static void *boot_set_opaque;
275

    
276
#ifdef SIGRTMIN
277
#define SIG_IPI (SIGRTMIN+4)
278
#else
279
#define SIG_IPI SIGUSR1
280
#endif
281

    
282
static int default_serial = 1;
283
static int default_parallel = 1;
284
static int default_virtcon = 1;
285
static int default_monitor = 1;
286
static int default_vga = 1;
287
static int default_floppy = 1;
288
static int default_cdrom = 1;
289
static int default_sdcard = 1;
290

    
291
static struct {
292
    const char *driver;
293
    int *flag;
294
} default_list[] = {
295
    { .driver = "isa-serial",           .flag = &default_serial    },
296
    { .driver = "isa-parallel",         .flag = &default_parallel  },
297
    { .driver = "isa-fdc",              .flag = &default_floppy    },
298
    { .driver = "ide-drive",            .flag = &default_cdrom     },
299
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
300
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
301
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
302
    { .driver = "VGA",                  .flag = &default_vga       },
303
    { .driver = "cirrus-vga",           .flag = &default_vga       },
304
    { .driver = "vmware-svga",          .flag = &default_vga       },
305
};
306

    
307
static int default_driver_check(QemuOpts *opts, void *opaque)
308
{
309
    const char *driver = qemu_opt_get(opts, "driver");
310
    int i;
311

    
312
    if (!driver)
313
        return 0;
314
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
315
        if (strcmp(default_list[i].driver, driver) != 0)
316
            continue;
317
        *(default_list[i].flag) = 0;
318
    }
319
    return 0;
320
}
321

    
322
/***********************************************************/
323
/* x86 ISA bus support */
324

    
325
target_phys_addr_t isa_mem_base = 0;
326
PicState2 *isa_pic;
327

    
328
/***********************************************************/
329
void hw_error(const char *fmt, ...)
330
{
331
    va_list ap;
332
    CPUState *env;
333

    
334
    va_start(ap, fmt);
335
    fprintf(stderr, "qemu: hardware error: ");
336
    vfprintf(stderr, fmt, ap);
337
    fprintf(stderr, "\n");
338
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
339
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
340
#ifdef TARGET_I386
341
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
342
#else
343
        cpu_dump_state(env, stderr, fprintf, 0);
344
#endif
345
    }
346
    va_end(ap);
347
    abort();
348
}
349

    
350
static void set_proc_name(const char *s)
351
{
352
#if defined(__linux__) && defined(PR_SET_NAME)
353
    char name[16];
354
    if (!s)
355
        return;
356
    name[sizeof(name) - 1] = 0;
357
    strncpy(name, s, sizeof(name));
358
    /* Could rewrite argv[0] too, but that's a bit more complicated.
359
       This simple way is enough for `top'. */
360
    prctl(PR_SET_NAME, name);
361
#endif            
362
}
363
 
364
/***************/
365
/* ballooning */
366

    
367
static QEMUBalloonEvent *qemu_balloon_event;
368
void *qemu_balloon_event_opaque;
369

    
370
void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
371
{
372
    qemu_balloon_event = func;
373
    qemu_balloon_event_opaque = opaque;
374
}
375

    
376
int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
377
{
378
    if (qemu_balloon_event) {
379
        qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
380
        return 1;
381
    } else {
382
        return 0;
383
    }
384
}
385

    
386
int qemu_balloon_status(MonitorCompletion cb, void *opaque)
387
{
388
    if (qemu_balloon_event) {
389
        qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
390
        return 1;
391
    } else {
392
        return 0;
393
    }
394
}
395

    
396

    
397
/***********************************************************/
398
/* real time host monotonic timer */
399

    
400
/* compute with 96 bit intermediate result: (a*b)/c */
401
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
402
{
403
    union {
404
        uint64_t ll;
405
        struct {
406
#ifdef HOST_WORDS_BIGENDIAN
407
            uint32_t high, low;
408
#else
409
            uint32_t low, high;
410
#endif
411
        } l;
412
    } u, res;
413
    uint64_t rl, rh;
414

    
415
    u.ll = a;
416
    rl = (uint64_t)u.l.low * (uint64_t)b;
417
    rh = (uint64_t)u.l.high * (uint64_t)b;
418
    rh += (rl >> 32);
419
    res.l.high = rh / c;
420
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
421
    return res.ll;
422
}
423

    
424
static int64_t get_clock_realtime(void)
425
{
426
    struct timeval tv;
427

    
428
    gettimeofday(&tv, NULL);
429
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
430
}
431

    
432
#ifdef WIN32
433

    
434
static int64_t clock_freq;
435

    
436
static void init_get_clock(void)
437
{
438
    LARGE_INTEGER freq;
439
    int ret;
440
    ret = QueryPerformanceFrequency(&freq);
441
    if (ret == 0) {
442
        fprintf(stderr, "Could not calibrate ticks\n");
443
        exit(1);
444
    }
445
    clock_freq = freq.QuadPart;
446
}
447

    
448
static int64_t get_clock(void)
449
{
450
    LARGE_INTEGER ti;
451
    QueryPerformanceCounter(&ti);
452
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
453
}
454

    
455
#else
456

    
457
static int use_rt_clock;
458

    
459
static void init_get_clock(void)
460
{
461
    use_rt_clock = 0;
462
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
463
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
464
    {
465
        struct timespec ts;
466
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
467
            use_rt_clock = 1;
468
        }
469
    }
470
#endif
471
}
472

    
473
static int64_t get_clock(void)
474
{
475
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
476
        || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
477
    if (use_rt_clock) {
478
        struct timespec ts;
479
        clock_gettime(CLOCK_MONOTONIC, &ts);
480
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
481
    } else
482
#endif
483
    {
484
        /* XXX: using gettimeofday leads to problems if the date
485
           changes, so it should be avoided. */
486
        return get_clock_realtime();
487
    }
488
}
489
#endif
490

    
491
/* Return the virtual CPU time, based on the instruction counter.  */
492
static int64_t cpu_get_icount(void)
493
{
494
    int64_t icount;
495
    CPUState *env = cpu_single_env;;
496
    icount = qemu_icount;
497
    if (env) {
498
        if (!can_do_io(env))
499
            fprintf(stderr, "Bad clock read\n");
500
        icount -= (env->icount_decr.u16.low + env->icount_extra);
501
    }
502
    return qemu_icount_bias + (icount << icount_time_shift);
503
}
504

    
505
/***********************************************************/
506
/* guest cycle counter */
507

    
508
typedef struct TimersState {
509
    int64_t cpu_ticks_prev;
510
    int64_t cpu_ticks_offset;
511
    int64_t cpu_clock_offset;
512
    int32_t cpu_ticks_enabled;
513
    int64_t dummy;
514
} TimersState;
515

    
516
TimersState timers_state;
517

    
518
/* return the host CPU cycle counter and handle stop/restart */
519
int64_t cpu_get_ticks(void)
520
{
521
    if (use_icount) {
522
        return cpu_get_icount();
523
    }
524
    if (!timers_state.cpu_ticks_enabled) {
525
        return timers_state.cpu_ticks_offset;
526
    } else {
527
        int64_t ticks;
528
        ticks = cpu_get_real_ticks();
529
        if (timers_state.cpu_ticks_prev > ticks) {
530
            /* Note: non increasing ticks may happen if the host uses
531
               software suspend */
532
            timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
533
        }
534
        timers_state.cpu_ticks_prev = ticks;
535
        return ticks + timers_state.cpu_ticks_offset;
536
    }
537
}
538

    
539
/* return the host CPU monotonic timer and handle stop/restart */
540
static int64_t cpu_get_clock(void)
541
{
542
    int64_t ti;
543
    if (!timers_state.cpu_ticks_enabled) {
544
        return timers_state.cpu_clock_offset;
545
    } else {
546
        ti = get_clock();
547
        return ti + timers_state.cpu_clock_offset;
548
    }
549
}
550

    
551
#ifndef CONFIG_IOTHREAD
552
static int64_t qemu_icount_delta(void)
553
{
554
    if (!use_icount) {
555
        return 5000 * (int64_t) 1000000;
556
    } else if (use_icount == 1) {
557
        /* When not using an adaptive execution frequency
558
           we tend to get badly out of sync with real time,
559
           so just delay for a reasonable amount of time.  */
560
        return 0;
561
    } else {
562
        return cpu_get_icount() - cpu_get_clock();
563
    }
564
}
565
#endif
566

    
567
/* enable cpu_get_ticks() */
568
void cpu_enable_ticks(void)
569
{
570
    if (!timers_state.cpu_ticks_enabled) {
571
        timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
572
        timers_state.cpu_clock_offset -= get_clock();
573
        timers_state.cpu_ticks_enabled = 1;
574
    }
575
}
576

    
577
/* disable cpu_get_ticks() : the clock is stopped. You must not call
578
   cpu_get_ticks() after that.  */
579
void cpu_disable_ticks(void)
580
{
581
    if (timers_state.cpu_ticks_enabled) {
582
        timers_state.cpu_ticks_offset = cpu_get_ticks();
583
        timers_state.cpu_clock_offset = cpu_get_clock();
584
        timers_state.cpu_ticks_enabled = 0;
585
    }
586
}
587

    
588
/***********************************************************/
589
/* timers */
590

    
591
#define QEMU_CLOCK_REALTIME 0
592
#define QEMU_CLOCK_VIRTUAL  1
593
#define QEMU_CLOCK_HOST     2
594

    
595
struct QEMUClock {
596
    int type;
597
    int enabled;
598
    /* XXX: add frequency */
599
};
600

    
601
struct QEMUTimer {
602
    QEMUClock *clock;
603
    int64_t expire_time;
604
    QEMUTimerCB *cb;
605
    void *opaque;
606
    struct QEMUTimer *next;
607
};
608

    
609
struct qemu_alarm_timer {
610
    char const *name;
611
    int (*start)(struct qemu_alarm_timer *t);
612
    void (*stop)(struct qemu_alarm_timer *t);
613
    void (*rearm)(struct qemu_alarm_timer *t);
614
    void *priv;
615

    
616
    char expired;
617
    char pending;
618
};
619

    
620
static struct qemu_alarm_timer *alarm_timer;
621

    
622
static inline int qemu_alarm_pending(void)
623
{
624
    return alarm_timer->pending;
625
}
626

    
627
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
628
{
629
    return !!t->rearm;
630
}
631

    
632
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
633
{
634
    if (!alarm_has_dynticks(t))
635
        return;
636

    
637
    t->rearm(t);
638
}
639

    
640
/* TODO: MIN_TIMER_REARM_US should be optimized */
641
#define MIN_TIMER_REARM_US 250
642

    
643
#ifdef _WIN32
644

    
645
struct qemu_alarm_win32 {
646
    MMRESULT timerId;
647
    unsigned int period;
648
} alarm_win32_data = {0, 0};
649

    
650
static int win32_start_timer(struct qemu_alarm_timer *t);
651
static void win32_stop_timer(struct qemu_alarm_timer *t);
652
static void win32_rearm_timer(struct qemu_alarm_timer *t);
653

    
654
#else
655

    
656
static int unix_start_timer(struct qemu_alarm_timer *t);
657
static void unix_stop_timer(struct qemu_alarm_timer *t);
658

    
659
#ifdef __linux__
660

    
661
static int dynticks_start_timer(struct qemu_alarm_timer *t);
662
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
663
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
664

    
665
static int hpet_start_timer(struct qemu_alarm_timer *t);
666
static void hpet_stop_timer(struct qemu_alarm_timer *t);
667

    
668
static int rtc_start_timer(struct qemu_alarm_timer *t);
669
static void rtc_stop_timer(struct qemu_alarm_timer *t);
670

    
671
#endif /* __linux__ */
672

    
673
#endif /* _WIN32 */
674

    
675
/* Correlation between real and virtual time is always going to be
676
   fairly approximate, so ignore small variation.
677
   When the guest is idle real and virtual time will be aligned in
678
   the IO wait loop.  */
679
#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
680

    
681
static void icount_adjust(void)
682
{
683
    int64_t cur_time;
684
    int64_t cur_icount;
685
    int64_t delta;
686
    static int64_t last_delta;
687
    /* If the VM is not running, then do nothing.  */
688
    if (!vm_running)
689
        return;
690

    
691
    cur_time = cpu_get_clock();
692
    cur_icount = qemu_get_clock(vm_clock);
693
    delta = cur_icount - cur_time;
694
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
695
    if (delta > 0
696
        && last_delta + ICOUNT_WOBBLE < delta * 2
697
        && icount_time_shift > 0) {
698
        /* The guest is getting too far ahead.  Slow time down.  */
699
        icount_time_shift--;
700
    }
701
    if (delta < 0
702
        && last_delta - ICOUNT_WOBBLE > delta * 2
703
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
704
        /* The guest is getting too far behind.  Speed time up.  */
705
        icount_time_shift++;
706
    }
707
    last_delta = delta;
708
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
709
}
710

    
711
static void icount_adjust_rt(void * opaque)
712
{
713
    qemu_mod_timer(icount_rt_timer,
714
                   qemu_get_clock(rt_clock) + 1000);
715
    icount_adjust();
716
}
717

    
718
static void icount_adjust_vm(void * opaque)
719
{
720
    qemu_mod_timer(icount_vm_timer,
721
                   qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
722
    icount_adjust();
723
}
724

    
725
static int64_t qemu_icount_round(int64_t count)
726
{
727
    return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
728
}
729

    
730
static struct qemu_alarm_timer alarm_timers[] = {
731
#ifndef _WIN32
732
#ifdef __linux__
733
    {"dynticks", dynticks_start_timer,
734
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
735
    /* HPET - if available - is preferred */
736
    {"hpet", hpet_start_timer, hpet_stop_timer, NULL, NULL},
737
    /* ...otherwise try RTC */
738
    {"rtc", rtc_start_timer, rtc_stop_timer, NULL, NULL},
739
#endif
740
    {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
741
#else
742
    {"dynticks", win32_start_timer,
743
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
744
    {"win32", win32_start_timer,
745
     win32_stop_timer, NULL, &alarm_win32_data},
746
#endif
747
    {NULL, }
748
};
749

    
750
static void show_available_alarms(void)
751
{
752
    int i;
753

    
754
    printf("Available alarm timers, in order of precedence:\n");
755
    for (i = 0; alarm_timers[i].name; i++)
756
        printf("%s\n", alarm_timers[i].name);
757
}
758

    
759
static void configure_alarms(char const *opt)
760
{
761
    int i;
762
    int cur = 0;
763
    int count = ARRAY_SIZE(alarm_timers) - 1;
764
    char *arg;
765
    char *name;
766
    struct qemu_alarm_timer tmp;
767

    
768
    if (!strcmp(opt, "?")) {
769
        show_available_alarms();
770
        exit(0);
771
    }
772

    
773
    arg = qemu_strdup(opt);
774

    
775
    /* Reorder the array */
776
    name = strtok(arg, ",");
777
    while (name) {
778
        for (i = 0; i < count && alarm_timers[i].name; i++) {
779
            if (!strcmp(alarm_timers[i].name, name))
780
                break;
781
        }
782

    
783
        if (i == count) {
784
            fprintf(stderr, "Unknown clock %s\n", name);
785
            goto next;
786
        }
787

    
788
        if (i < cur)
789
            /* Ignore */
790
            goto next;
791

    
792
        /* Swap */
793
        tmp = alarm_timers[i];
794
        alarm_timers[i] = alarm_timers[cur];
795
        alarm_timers[cur] = tmp;
796

    
797
        cur++;
798
next:
799
        name = strtok(NULL, ",");
800
    }
801

    
802
    qemu_free(arg);
803

    
804
    if (cur) {
805
        /* Disable remaining timers */
806
        for (i = cur; i < count; i++)
807
            alarm_timers[i].name = NULL;
808
    } else {
809
        show_available_alarms();
810
        exit(1);
811
    }
812
}
813

    
814
#define QEMU_NUM_CLOCKS 3
815

    
816
QEMUClock *rt_clock;
817
QEMUClock *vm_clock;
818
QEMUClock *host_clock;
819

    
820
static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
821

    
822
static QEMUClock *qemu_new_clock(int type)
823
{
824
    QEMUClock *clock;
825
    clock = qemu_mallocz(sizeof(QEMUClock));
826
    clock->type = type;
827
    clock->enabled = 1;
828
    return clock;
829
}
830

    
831
static void qemu_clock_enable(QEMUClock *clock, int enabled)
832
{
833
    clock->enabled = enabled;
834
}
835

    
836
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
837
{
838
    QEMUTimer *ts;
839

    
840
    ts = qemu_mallocz(sizeof(QEMUTimer));
841
    ts->clock = clock;
842
    ts->cb = cb;
843
    ts->opaque = opaque;
844
    return ts;
845
}
846

    
847
void qemu_free_timer(QEMUTimer *ts)
848
{
849
    qemu_free(ts);
850
}
851

    
852
/* stop a timer, but do not dealloc it */
853
void qemu_del_timer(QEMUTimer *ts)
854
{
855
    QEMUTimer **pt, *t;
856

    
857
    /* NOTE: this code must be signal safe because
858
       qemu_timer_expired() can be called from a signal. */
859
    pt = &active_timers[ts->clock->type];
860
    for(;;) {
861
        t = *pt;
862
        if (!t)
863
            break;
864
        if (t == ts) {
865
            *pt = t->next;
866
            break;
867
        }
868
        pt = &t->next;
869
    }
870
}
871

    
872
/* modify the current timer so that it will be fired when current_time
873
   >= expire_time. The corresponding callback will be called. */
874
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
875
{
876
    QEMUTimer **pt, *t;
877

    
878
    qemu_del_timer(ts);
879

    
880
    /* add the timer in the sorted list */
881
    /* NOTE: this code must be signal safe because
882
       qemu_timer_expired() can be called from a signal. */
883
    pt = &active_timers[ts->clock->type];
884
    for(;;) {
885
        t = *pt;
886
        if (!t)
887
            break;
888
        if (t->expire_time > expire_time)
889
            break;
890
        pt = &t->next;
891
    }
892
    ts->expire_time = expire_time;
893
    ts->next = *pt;
894
    *pt = ts;
895

    
896
    /* Rearm if necessary  */
897
    if (pt == &active_timers[ts->clock->type]) {
898
        if (!alarm_timer->pending) {
899
            qemu_rearm_alarm_timer(alarm_timer);
900
        }
901
        /* Interrupt execution to force deadline recalculation.  */
902
        if (use_icount)
903
            qemu_notify_event();
904
    }
905
}
906

    
907
int qemu_timer_pending(QEMUTimer *ts)
908
{
909
    QEMUTimer *t;
910
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
911
        if (t == ts)
912
            return 1;
913
    }
914
    return 0;
915
}
916

    
917
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
918
{
919
    if (!timer_head)
920
        return 0;
921
    return (timer_head->expire_time <= current_time);
922
}
923

    
924
static void qemu_run_timers(QEMUClock *clock)
925
{
926
    QEMUTimer **ptimer_head, *ts;
927
    int64_t current_time;
928
   
929
    if (!clock->enabled)
930
        return;
931

    
932
    current_time = qemu_get_clock (clock);
933
    ptimer_head = &active_timers[clock->type];
934
    for(;;) {
935
        ts = *ptimer_head;
936
        if (!ts || ts->expire_time > current_time)
937
            break;
938
        /* remove timer from the list before calling the callback */
939
        *ptimer_head = ts->next;
940
        ts->next = NULL;
941

    
942
        /* run the callback (the timer list can be modified) */
943
        ts->cb(ts->opaque);
944
    }
945
}
946

    
947
int64_t qemu_get_clock(QEMUClock *clock)
948
{
949
    switch(clock->type) {
950
    case QEMU_CLOCK_REALTIME:
951
        return get_clock() / 1000000;
952
    default:
953
    case QEMU_CLOCK_VIRTUAL:
954
        if (use_icount) {
955
            return cpu_get_icount();
956
        } else {
957
            return cpu_get_clock();
958
        }
959
    case QEMU_CLOCK_HOST:
960
        return get_clock_realtime();
961
    }
962
}
963

    
964
int64_t qemu_get_clock_ns(QEMUClock *clock)
965
{
966
    switch(clock->type) {
967
    case QEMU_CLOCK_REALTIME:
968
        return get_clock();
969
    default:
970
    case QEMU_CLOCK_VIRTUAL:
971
        if (use_icount) {
972
            return cpu_get_icount();
973
        } else {
974
            return cpu_get_clock();
975
        }
976
    case QEMU_CLOCK_HOST:
977
        return get_clock_realtime();
978
    }
979
}
980

    
981
static void init_clocks(void)
982
{
983
    init_get_clock();
984
    rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
985
    vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
986
    host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
987

    
988
    rtc_clock = host_clock;
989
}
990

    
991
/* save a timer */
992
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
993
{
994
    uint64_t expire_time;
995

    
996
    if (qemu_timer_pending(ts)) {
997
        expire_time = ts->expire_time;
998
    } else {
999
        expire_time = -1;
1000
    }
1001
    qemu_put_be64(f, expire_time);
1002
}
1003

    
1004
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1005
{
1006
    uint64_t expire_time;
1007

    
1008
    expire_time = qemu_get_be64(f);
1009
    if (expire_time != -1) {
1010
        qemu_mod_timer(ts, expire_time);
1011
    } else {
1012
        qemu_del_timer(ts);
1013
    }
1014
}
1015

    
1016
static const VMStateDescription vmstate_timers = {
1017
    .name = "timer",
1018
    .version_id = 2,
1019
    .minimum_version_id = 1,
1020
    .minimum_version_id_old = 1,
1021
    .fields      = (VMStateField []) {
1022
        VMSTATE_INT64(cpu_ticks_offset, TimersState),
1023
        VMSTATE_INT64(dummy, TimersState),
1024
        VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1025
        VMSTATE_END_OF_LIST()
1026
    }
1027
};
1028

    
1029
static void configure_icount(const char *option)
1030
{
1031
    vmstate_register(0, &vmstate_timers, &timers_state);
1032
    if (!option)
1033
        return;
1034

    
1035
    if (strcmp(option, "auto") != 0) {
1036
        icount_time_shift = strtol(option, NULL, 0);
1037
        use_icount = 1;
1038
        return;
1039
    }
1040

    
1041
    use_icount = 2;
1042

    
1043
    /* 125MIPS seems a reasonable initial guess at the guest speed.
1044
       It will be corrected fairly quickly anyway.  */
1045
    icount_time_shift = 3;
1046

    
1047
    /* Have both realtime and virtual time triggers for speed adjustment.
1048
       The realtime trigger catches emulated time passing too slowly,
1049
       the virtual time trigger catches emulated time passing too fast.
1050
       Realtime triggers occur even when idle, so use them less frequently
1051
       than VM triggers.  */
1052
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1053
    qemu_mod_timer(icount_rt_timer,
1054
                   qemu_get_clock(rt_clock) + 1000);
1055
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1056
    qemu_mod_timer(icount_vm_timer,
1057
                   qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
1058
}
1059

    
1060
static void qemu_run_all_timers(void)
1061
{
1062
    /* rearm timer, if not periodic */
1063
    if (alarm_timer->expired) {
1064
        alarm_timer->expired = 0;
1065
        qemu_rearm_alarm_timer(alarm_timer);
1066
    }
1067

    
1068
    alarm_timer->pending = 0;
1069

    
1070
    /* vm time timers */
1071
    if (vm_running) {
1072
        qemu_run_timers(vm_clock);
1073
    }
1074

    
1075
    qemu_run_timers(rt_clock);
1076
    qemu_run_timers(host_clock);
1077
}
1078

    
1079
#ifdef _WIN32
1080
static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1081
                                        DWORD_PTR dwUser, DWORD_PTR dw1,
1082
                                        DWORD_PTR dw2)
1083
#else
1084
static void host_alarm_handler(int host_signum)
1085
#endif
1086
{
1087
    struct qemu_alarm_timer *t = alarm_timer;
1088
    if (!t)
1089
        return;
1090

    
1091
#if 0
1092
#define DISP_FREQ 1000
1093
    {
1094
        static int64_t delta_min = INT64_MAX;
1095
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
1096
        static int count;
1097
        ti = qemu_get_clock(vm_clock);
1098
        if (last_clock != 0) {
1099
            delta = ti - last_clock;
1100
            if (delta < delta_min)
1101
                delta_min = delta;
1102
            if (delta > delta_max)
1103
                delta_max = delta;
1104
            delta_cum += delta;
1105
            if (++count == DISP_FREQ) {
1106
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1107
                       muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1108
                       muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1109
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1110
                       (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
1111
                count = 0;
1112
                delta_min = INT64_MAX;
1113
                delta_max = 0;
1114
                delta_cum = 0;
1115
            }
1116
        }
1117
        last_clock = ti;
1118
    }
1119
#endif
1120
    if (alarm_has_dynticks(t) ||
1121
        (!use_icount &&
1122
            qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
1123
                               qemu_get_clock(vm_clock))) ||
1124
        qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
1125
                           qemu_get_clock(rt_clock)) ||
1126
        qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1127
                           qemu_get_clock(host_clock))) {
1128

    
1129
        t->expired = alarm_has_dynticks(t);
1130
        t->pending = 1;
1131
        qemu_notify_event();
1132
    }
1133
}
1134

    
1135
static int64_t qemu_next_deadline(void)
1136
{
1137
    /* To avoid problems with overflow limit this to 2^32.  */
1138
    int64_t delta = INT32_MAX;
1139

    
1140
    if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1141
        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
1142
                     qemu_get_clock(vm_clock);
1143
    }
1144
    if (active_timers[QEMU_CLOCK_HOST]) {
1145
        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1146
                 qemu_get_clock(host_clock);
1147
        if (hdelta < delta)
1148
            delta = hdelta;
1149
    }
1150

    
1151
    if (delta < 0)
1152
        delta = 0;
1153

    
1154
    return delta;
1155
}
1156

    
1157
#if defined(__linux__)
1158
static uint64_t qemu_next_deadline_dyntick(void)
1159
{
1160
    int64_t delta;
1161
    int64_t rtdelta;
1162

    
1163
    if (use_icount)
1164
        delta = INT32_MAX;
1165
    else
1166
        delta = (qemu_next_deadline() + 999) / 1000;
1167

    
1168
    if (active_timers[QEMU_CLOCK_REALTIME]) {
1169
        rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
1170
                 qemu_get_clock(rt_clock))*1000;
1171
        if (rtdelta < delta)
1172
            delta = rtdelta;
1173
    }
1174

    
1175
    if (delta < MIN_TIMER_REARM_US)
1176
        delta = MIN_TIMER_REARM_US;
1177

    
1178
    return delta;
1179
}
1180
#endif
1181

    
1182
#ifndef _WIN32
1183

    
1184
/* Sets a specific flag */
1185
static int fcntl_setfl(int fd, int flag)
1186
{
1187
    int flags;
1188

    
1189
    flags = fcntl(fd, F_GETFL);
1190
    if (flags == -1)
1191
        return -errno;
1192

    
1193
    if (fcntl(fd, F_SETFL, flags | flag) == -1)
1194
        return -errno;
1195

    
1196
    return 0;
1197
}
1198

    
1199
#if defined(__linux__)
1200

    
1201
#define RTC_FREQ 1024
1202

    
1203
static void enable_sigio_timer(int fd)
1204
{
1205
    struct sigaction act;
1206

    
1207
    /* timer signal */
1208
    sigfillset(&act.sa_mask);
1209
    act.sa_flags = 0;
1210
    act.sa_handler = host_alarm_handler;
1211

    
1212
    sigaction(SIGIO, &act, NULL);
1213
    fcntl_setfl(fd, O_ASYNC);
1214
    fcntl(fd, F_SETOWN, getpid());
1215
}
1216

    
1217
static int hpet_start_timer(struct qemu_alarm_timer *t)
1218
{
1219
    struct hpet_info info;
1220
    int r, fd;
1221

    
1222
    fd = qemu_open("/dev/hpet", O_RDONLY);
1223
    if (fd < 0)
1224
        return -1;
1225

    
1226
    /* Set frequency */
1227
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1228
    if (r < 0) {
1229
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1230
                "error, but for better emulation accuracy type:\n"
1231
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1232
        goto fail;
1233
    }
1234

    
1235
    /* Check capabilities */
1236
    r = ioctl(fd, HPET_INFO, &info);
1237
    if (r < 0)
1238
        goto fail;
1239

    
1240
    /* Enable periodic mode */
1241
    r = ioctl(fd, HPET_EPI, 0);
1242
    if (info.hi_flags && (r < 0))
1243
        goto fail;
1244

    
1245
    /* Enable interrupt */
1246
    r = ioctl(fd, HPET_IE_ON, 0);
1247
    if (r < 0)
1248
        goto fail;
1249

    
1250
    enable_sigio_timer(fd);
1251
    t->priv = (void *)(long)fd;
1252

    
1253
    return 0;
1254
fail:
1255
    close(fd);
1256
    return -1;
1257
}
1258

    
1259
static void hpet_stop_timer(struct qemu_alarm_timer *t)
1260
{
1261
    int fd = (long)t->priv;
1262

    
1263
    close(fd);
1264
}
1265

    
1266
static int rtc_start_timer(struct qemu_alarm_timer *t)
1267
{
1268
    int rtc_fd;
1269
    unsigned long current_rtc_freq = 0;
1270

    
1271
    TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
1272
    if (rtc_fd < 0)
1273
        return -1;
1274
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1275
    if (current_rtc_freq != RTC_FREQ &&
1276
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1277
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1278
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1279
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1280
        goto fail;
1281
    }
1282
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1283
    fail:
1284
        close(rtc_fd);
1285
        return -1;
1286
    }
1287

    
1288
    enable_sigio_timer(rtc_fd);
1289

    
1290
    t->priv = (void *)(long)rtc_fd;
1291

    
1292
    return 0;
1293
}
1294

    
1295
static void rtc_stop_timer(struct qemu_alarm_timer *t)
1296
{
1297
    int rtc_fd = (long)t->priv;
1298

    
1299
    close(rtc_fd);
1300
}
1301

    
1302
static int dynticks_start_timer(struct qemu_alarm_timer *t)
1303
{
1304
    struct sigevent ev;
1305
    timer_t host_timer;
1306
    struct sigaction act;
1307

    
1308
    sigfillset(&act.sa_mask);
1309
    act.sa_flags = 0;
1310
    act.sa_handler = host_alarm_handler;
1311

    
1312
    sigaction(SIGALRM, &act, NULL);
1313

    
1314
    /* 
1315
     * Initialize ev struct to 0 to avoid valgrind complaining
1316
     * about uninitialized data in timer_create call
1317
     */
1318
    memset(&ev, 0, sizeof(ev));
1319
    ev.sigev_value.sival_int = 0;
1320
    ev.sigev_notify = SIGEV_SIGNAL;
1321
    ev.sigev_signo = SIGALRM;
1322

    
1323
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1324
        perror("timer_create");
1325

    
1326
        /* disable dynticks */
1327
        fprintf(stderr, "Dynamic Ticks disabled\n");
1328

    
1329
        return -1;
1330
    }
1331

    
1332
    t->priv = (void *)(long)host_timer;
1333

    
1334
    return 0;
1335
}
1336

    
1337
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1338
{
1339
    timer_t host_timer = (timer_t)(long)t->priv;
1340

    
1341
    timer_delete(host_timer);
1342
}
1343

    
1344
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1345
{
1346
    timer_t host_timer = (timer_t)(long)t->priv;
1347
    struct itimerspec timeout;
1348
    int64_t nearest_delta_us = INT64_MAX;
1349
    int64_t current_us;
1350

    
1351
    assert(alarm_has_dynticks(t));
1352
    if (!active_timers[QEMU_CLOCK_REALTIME] &&
1353
        !active_timers[QEMU_CLOCK_VIRTUAL] &&
1354
        !active_timers[QEMU_CLOCK_HOST])
1355
        return;
1356

    
1357
    nearest_delta_us = qemu_next_deadline_dyntick();
1358

    
1359
    /* check whether a timer is already running */
1360
    if (timer_gettime(host_timer, &timeout)) {
1361
        perror("gettime");
1362
        fprintf(stderr, "Internal timer error: aborting\n");
1363
        exit(1);
1364
    }
1365
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1366
    if (current_us && current_us <= nearest_delta_us)
1367
        return;
1368

    
1369
    timeout.it_interval.tv_sec = 0;
1370
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1371
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1372
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1373
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1374
        perror("settime");
1375
        fprintf(stderr, "Internal timer error: aborting\n");
1376
        exit(1);
1377
    }
1378
}
1379

    
1380
#endif /* defined(__linux__) */
1381

    
1382
static int unix_start_timer(struct qemu_alarm_timer *t)
1383
{
1384
    struct sigaction act;
1385
    struct itimerval itv;
1386
    int err;
1387

    
1388
    /* timer signal */
1389
    sigfillset(&act.sa_mask);
1390
    act.sa_flags = 0;
1391
    act.sa_handler = host_alarm_handler;
1392

    
1393
    sigaction(SIGALRM, &act, NULL);
1394

    
1395
    itv.it_interval.tv_sec = 0;
1396
    /* for i386 kernel 2.6 to get 1 ms */
1397
    itv.it_interval.tv_usec = 999;
1398
    itv.it_value.tv_sec = 0;
1399
    itv.it_value.tv_usec = 10 * 1000;
1400

    
1401
    err = setitimer(ITIMER_REAL, &itv, NULL);
1402
    if (err)
1403
        return -1;
1404

    
1405
    return 0;
1406
}
1407

    
1408
static void unix_stop_timer(struct qemu_alarm_timer *t)
1409
{
1410
    struct itimerval itv;
1411

    
1412
    memset(&itv, 0, sizeof(itv));
1413
    setitimer(ITIMER_REAL, &itv, NULL);
1414
}
1415

    
1416
#endif /* !defined(_WIN32) */
1417

    
1418

    
1419
#ifdef _WIN32
1420

    
1421
static int win32_start_timer(struct qemu_alarm_timer *t)
1422
{
1423
    TIMECAPS tc;
1424
    struct qemu_alarm_win32 *data = t->priv;
1425
    UINT flags;
1426

    
1427
    memset(&tc, 0, sizeof(tc));
1428
    timeGetDevCaps(&tc, sizeof(tc));
1429

    
1430
    data->period = tc.wPeriodMin;
1431
    timeBeginPeriod(data->period);
1432

    
1433
    flags = TIME_CALLBACK_FUNCTION;
1434
    if (alarm_has_dynticks(t))
1435
        flags |= TIME_ONESHOT;
1436
    else
1437
        flags |= TIME_PERIODIC;
1438

    
1439
    data->timerId = timeSetEvent(1,         // interval (ms)
1440
                        data->period,       // resolution
1441
                        host_alarm_handler, // function
1442
                        (DWORD)t,           // parameter
1443
                        flags);
1444

    
1445
    if (!data->timerId) {
1446
        fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1447
                GetLastError());
1448
        timeEndPeriod(data->period);
1449
        return -1;
1450
    }
1451

    
1452
    return 0;
1453
}
1454

    
1455
static void win32_stop_timer(struct qemu_alarm_timer *t)
1456
{
1457
    struct qemu_alarm_win32 *data = t->priv;
1458

    
1459
    timeKillEvent(data->timerId);
1460
    timeEndPeriod(data->period);
1461
}
1462

    
1463
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1464
{
1465
    struct qemu_alarm_win32 *data = t->priv;
1466

    
1467
    assert(alarm_has_dynticks(t));
1468
    if (!active_timers[QEMU_CLOCK_REALTIME] &&
1469
        !active_timers[QEMU_CLOCK_VIRTUAL] &&
1470
        !active_timers[QEMU_CLOCK_HOST])
1471
        return;
1472

    
1473
    timeKillEvent(data->timerId);
1474

    
1475
    data->timerId = timeSetEvent(1,
1476
                        data->period,
1477
                        host_alarm_handler,
1478
                        (DWORD)t,
1479
                        TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1480

    
1481
    if (!data->timerId) {
1482
        fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1483
                GetLastError());
1484

    
1485
        timeEndPeriod(data->period);
1486
        exit(1);
1487
    }
1488
}
1489

    
1490
#endif /* _WIN32 */
1491

    
1492
static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1493
{
1494
    if (running)
1495
        qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1496
}
1497

    
1498
static int init_timer_alarm(void)
1499
{
1500
    struct qemu_alarm_timer *t = NULL;
1501
    int i, err = -1;
1502

    
1503
    for (i = 0; alarm_timers[i].name; i++) {
1504
        t = &alarm_timers[i];
1505

    
1506
        err = t->start(t);
1507
        if (!err)
1508
            break;
1509
    }
1510

    
1511
    if (err) {
1512
        err = -ENOENT;
1513
        goto fail;
1514
    }
1515

    
1516
    /* first event is at time 0 */
1517
    t->pending = 1;
1518
    alarm_timer = t;
1519
    qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1520

    
1521
    return 0;
1522

    
1523
fail:
1524
    return err;
1525
}
1526

    
1527
static void quit_timers(void)
1528
{
1529
    struct qemu_alarm_timer *t = alarm_timer;
1530
    alarm_timer = NULL;
1531
    t->stop(t);
1532
}
1533

    
1534
/***********************************************************/
1535
/* host time/date access */
1536
void qemu_get_timedate(struct tm *tm, int offset)
1537
{
1538
    time_t ti;
1539
    struct tm *ret;
1540

    
1541
    time(&ti);
1542
    ti += offset;
1543
    if (rtc_date_offset == -1) {
1544
        if (rtc_utc)
1545
            ret = gmtime(&ti);
1546
        else
1547
            ret = localtime(&ti);
1548
    } else {
1549
        ti -= rtc_date_offset;
1550
        ret = gmtime(&ti);
1551
    }
1552

    
1553
    memcpy(tm, ret, sizeof(struct tm));
1554
}
1555

    
1556
int qemu_timedate_diff(struct tm *tm)
1557
{
1558
    time_t seconds;
1559

    
1560
    if (rtc_date_offset == -1)
1561
        if (rtc_utc)
1562
            seconds = mktimegm(tm);
1563
        else
1564
            seconds = mktime(tm);
1565
    else
1566
        seconds = mktimegm(tm) + rtc_date_offset;
1567

    
1568
    return seconds - time(NULL);
1569
}
1570

    
1571
void rtc_change_mon_event(struct tm *tm)
1572
{
1573
    QObject *data;
1574

    
1575
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
1576
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
1577
    qobject_decref(data);
1578
}
1579

    
1580
static void configure_rtc_date_offset(const char *startdate, int legacy)
1581
{
1582
    time_t rtc_start_date;
1583
    struct tm tm;
1584

    
1585
    if (!strcmp(startdate, "now") && legacy) {
1586
        rtc_date_offset = -1;
1587
    } else {
1588
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1589
                   &tm.tm_year,
1590
                   &tm.tm_mon,
1591
                   &tm.tm_mday,
1592
                   &tm.tm_hour,
1593
                   &tm.tm_min,
1594
                   &tm.tm_sec) == 6) {
1595
            /* OK */
1596
        } else if (sscanf(startdate, "%d-%d-%d",
1597
                          &tm.tm_year,
1598
                          &tm.tm_mon,
1599
                          &tm.tm_mday) == 3) {
1600
            tm.tm_hour = 0;
1601
            tm.tm_min = 0;
1602
            tm.tm_sec = 0;
1603
        } else {
1604
            goto date_fail;
1605
        }
1606
        tm.tm_year -= 1900;
1607
        tm.tm_mon--;
1608
        rtc_start_date = mktimegm(&tm);
1609
        if (rtc_start_date == -1) {
1610
        date_fail:
1611
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
1612
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
1613
            exit(1);
1614
        }
1615
        rtc_date_offset = time(NULL) - rtc_start_date;
1616
    }
1617
}
1618

    
1619
static void configure_rtc(QemuOpts *opts)
1620
{
1621
    const char *value;
1622

    
1623
    value = qemu_opt_get(opts, "base");
1624
    if (value) {
1625
        if (!strcmp(value, "utc")) {
1626
            rtc_utc = 1;
1627
        } else if (!strcmp(value, "localtime")) {
1628
            rtc_utc = 0;
1629
        } else {
1630
            configure_rtc_date_offset(value, 0);
1631
        }
1632
    }
1633
    value = qemu_opt_get(opts, "clock");
1634
    if (value) {
1635
        if (!strcmp(value, "host")) {
1636
            rtc_clock = host_clock;
1637
        } else if (!strcmp(value, "vm")) {
1638
            rtc_clock = vm_clock;
1639
        } else {
1640
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1641
            exit(1);
1642
        }
1643
    }
1644
#ifdef CONFIG_TARGET_I386
1645
    value = qemu_opt_get(opts, "driftfix");
1646
    if (value) {
1647
        if (!strcmp(buf, "slew")) {
1648
            rtc_td_hack = 1;
1649
        } else if (!strcmp(buf, "none")) {
1650
            rtc_td_hack = 0;
1651
        } else {
1652
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1653
            exit(1);
1654
        }
1655
    }
1656
#endif
1657
}
1658

    
1659
#ifdef _WIN32
1660
static void socket_cleanup(void)
1661
{
1662
    WSACleanup();
1663
}
1664

    
1665
static int socket_init(void)
1666
{
1667
    WSADATA Data;
1668
    int ret, err;
1669

    
1670
    ret = WSAStartup(MAKEWORD(2,2), &Data);
1671
    if (ret != 0) {
1672
        err = WSAGetLastError();
1673
        fprintf(stderr, "WSAStartup: %d\n", err);
1674
        return -1;
1675
    }
1676
    atexit(socket_cleanup);
1677
    return 0;
1678
}
1679
#endif
1680

    
1681
/***********************************************************/
1682
/* Bluetooth support */
1683
static int nb_hcis;
1684
static int cur_hci;
1685
static struct HCIInfo *hci_table[MAX_NICS];
1686

    
1687
static struct bt_vlan_s {
1688
    struct bt_scatternet_s net;
1689
    int id;
1690
    struct bt_vlan_s *next;
1691
} *first_bt_vlan;
1692

    
1693
/* find or alloc a new bluetooth "VLAN" */
1694
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1695
{
1696
    struct bt_vlan_s **pvlan, *vlan;
1697
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1698
        if (vlan->id == id)
1699
            return &vlan->net;
1700
    }
1701
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1702
    vlan->id = id;
1703
    pvlan = &first_bt_vlan;
1704
    while (*pvlan != NULL)
1705
        pvlan = &(*pvlan)->next;
1706
    *pvlan = vlan;
1707
    return &vlan->net;
1708
}
1709

    
1710
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1711
{
1712
}
1713

    
1714
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1715
{
1716
    return -ENOTSUP;
1717
}
1718

    
1719
static struct HCIInfo null_hci = {
1720
    .cmd_send = null_hci_send,
1721
    .sco_send = null_hci_send,
1722
    .acl_send = null_hci_send,
1723
    .bdaddr_set = null_hci_addr_set,
1724
};
1725

    
1726
struct HCIInfo *qemu_next_hci(void)
1727
{
1728
    if (cur_hci == nb_hcis)
1729
        return &null_hci;
1730

    
1731
    return hci_table[cur_hci++];
1732
}
1733

    
1734
static struct HCIInfo *hci_init(const char *str)
1735
{
1736
    char *endp;
1737
    struct bt_scatternet_s *vlan = 0;
1738

    
1739
    if (!strcmp(str, "null"))
1740
        /* null */
1741
        return &null_hci;
1742
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1743
        /* host[:hciN] */
1744
        return bt_host_hci(str[4] ? str + 5 : "hci0");
1745
    else if (!strncmp(str, "hci", 3)) {
1746
        /* hci[,vlan=n] */
1747
        if (str[3]) {
1748
            if (!strncmp(str + 3, ",vlan=", 6)) {
1749
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1750
                if (*endp)
1751
                    vlan = 0;
1752
            }
1753
        } else
1754
            vlan = qemu_find_bt_vlan(0);
1755
        if (vlan)
1756
           return bt_new_hci(vlan);
1757
    }
1758

    
1759
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1760

    
1761
    return 0;
1762
}
1763

    
1764
static int bt_hci_parse(const char *str)
1765
{
1766
    struct HCIInfo *hci;
1767
    bdaddr_t bdaddr;
1768

    
1769
    if (nb_hcis >= MAX_NICS) {
1770
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1771
        return -1;
1772
    }
1773

    
1774
    hci = hci_init(str);
1775
    if (!hci)
1776
        return -1;
1777

    
1778
    bdaddr.b[0] = 0x52;
1779
    bdaddr.b[1] = 0x54;
1780
    bdaddr.b[2] = 0x00;
1781
    bdaddr.b[3] = 0x12;
1782
    bdaddr.b[4] = 0x34;
1783
    bdaddr.b[5] = 0x56 + nb_hcis;
1784
    hci->bdaddr_set(hci, bdaddr.b);
1785

    
1786
    hci_table[nb_hcis++] = hci;
1787

    
1788
    return 0;
1789
}
1790

    
1791
static void bt_vhci_add(int vlan_id)
1792
{
1793
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1794

    
1795
    if (!vlan->slave)
1796
        fprintf(stderr, "qemu: warning: adding a VHCI to "
1797
                        "an empty scatternet %i\n", vlan_id);
1798

    
1799
    bt_vhci_init(bt_new_hci(vlan));
1800
}
1801

    
1802
static struct bt_device_s *bt_device_add(const char *opt)
1803
{
1804
    struct bt_scatternet_s *vlan;
1805
    int vlan_id = 0;
1806
    char *endp = strstr(opt, ",vlan=");
1807
    int len = (endp ? endp - opt : strlen(opt)) + 1;
1808
    char devname[10];
1809

    
1810
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
1811

    
1812
    if (endp) {
1813
        vlan_id = strtol(endp + 6, &endp, 0);
1814
        if (*endp) {
1815
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1816
            return 0;
1817
        }
1818
    }
1819

    
1820
    vlan = qemu_find_bt_vlan(vlan_id);
1821

    
1822
    if (!vlan->slave)
1823
        fprintf(stderr, "qemu: warning: adding a slave device to "
1824
                        "an empty scatternet %i\n", vlan_id);
1825

    
1826
    if (!strcmp(devname, "keyboard"))
1827
        return bt_keyboard_init(vlan);
1828

    
1829
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1830
    return 0;
1831
}
1832

    
1833
static int bt_parse(const char *opt)
1834
{
1835
    const char *endp, *p;
1836
    int vlan;
1837

    
1838
    if (strstart(opt, "hci", &endp)) {
1839
        if (!*endp || *endp == ',') {
1840
            if (*endp)
1841
                if (!strstart(endp, ",vlan=", 0))
1842
                    opt = endp + 1;
1843

    
1844
            return bt_hci_parse(opt);
1845
       }
1846
    } else if (strstart(opt, "vhci", &endp)) {
1847
        if (!*endp || *endp == ',') {
1848
            if (*endp) {
1849
                if (strstart(endp, ",vlan=", &p)) {
1850
                    vlan = strtol(p, (char **) &endp, 0);
1851
                    if (*endp) {
1852
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1853
                        return 1;
1854
                    }
1855
                } else {
1856
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1857
                    return 1;
1858
                }
1859
            } else
1860
                vlan = 0;
1861

    
1862
            bt_vhci_add(vlan);
1863
            return 0;
1864
        }
1865
    } else if (strstart(opt, "device:", &endp))
1866
        return !bt_device_add(endp);
1867

    
1868
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1869
    return 1;
1870
}
1871

    
1872
/***********************************************************/
1873
/* QEMU Block devices */
1874

    
1875
#define HD_ALIAS "index=%d,media=disk"
1876
#define CDROM_ALIAS "index=2,media=cdrom"
1877
#define FD_ALIAS "index=%d,if=floppy"
1878
#define PFLASH_ALIAS "if=pflash"
1879
#define MTD_ALIAS "if=mtd"
1880
#define SD_ALIAS "index=0,if=sd"
1881

    
1882
QemuOpts *drive_add(const char *file, const char *fmt, ...)
1883
{
1884
    va_list ap;
1885
    char optstr[1024];
1886
    QemuOpts *opts;
1887

    
1888
    va_start(ap, fmt);
1889
    vsnprintf(optstr, sizeof(optstr), fmt, ap);
1890
    va_end(ap);
1891

    
1892
    opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
1893
    if (!opts) {
1894
        fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1895
                __FUNCTION__, optstr);
1896
        return NULL;
1897
    }
1898
    if (file)
1899
        qemu_opt_set(opts, "file", file);
1900
    return opts;
1901
}
1902

    
1903
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
1904
{
1905
    DriveInfo *dinfo;
1906

    
1907
    /* seek interface, bus and unit */
1908

    
1909
    QTAILQ_FOREACH(dinfo, &drives, next) {
1910
        if (dinfo->type == type &&
1911
            dinfo->bus == bus &&
1912
            dinfo->unit == unit)
1913
            return dinfo;
1914
    }
1915

    
1916
    return NULL;
1917
}
1918

    
1919
DriveInfo *drive_get_by_id(const char *id)
1920
{
1921
    DriveInfo *dinfo;
1922

    
1923
    QTAILQ_FOREACH(dinfo, &drives, next) {
1924
        if (strcmp(id, dinfo->id))
1925
            continue;
1926
        return dinfo;
1927
    }
1928
    return NULL;
1929
}
1930

    
1931
int drive_get_max_bus(BlockInterfaceType type)
1932
{
1933
    int max_bus;
1934
    DriveInfo *dinfo;
1935

    
1936
    max_bus = -1;
1937
    QTAILQ_FOREACH(dinfo, &drives, next) {
1938
        if(dinfo->type == type &&
1939
           dinfo->bus > max_bus)
1940
            max_bus = dinfo->bus;
1941
    }
1942
    return max_bus;
1943
}
1944

    
1945
const char *drive_get_serial(BlockDriverState *bdrv)
1946
{
1947
    DriveInfo *dinfo;
1948

    
1949
    QTAILQ_FOREACH(dinfo, &drives, next) {
1950
        if (dinfo->bdrv == bdrv)
1951
            return dinfo->serial;
1952
    }
1953

    
1954
    return "\0";
1955
}
1956

    
1957
BlockInterfaceErrorAction drive_get_on_error(
1958
    BlockDriverState *bdrv, int is_read)
1959
{
1960
    DriveInfo *dinfo;
1961

    
1962
    QTAILQ_FOREACH(dinfo, &drives, next) {
1963
        if (dinfo->bdrv == bdrv)
1964
            return is_read ? dinfo->on_read_error : dinfo->on_write_error;
1965
    }
1966

    
1967
    return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
1968
}
1969

    
1970
static void bdrv_format_print(void *opaque, const char *name)
1971
{
1972
    fprintf(stderr, " %s", name);
1973
}
1974

    
1975
void drive_uninit(DriveInfo *dinfo)
1976
{
1977
    qemu_opts_del(dinfo->opts);
1978
    bdrv_delete(dinfo->bdrv);
1979
    QTAILQ_REMOVE(&drives, dinfo, next);
1980
    qemu_free(dinfo);
1981
}
1982

    
1983
static int parse_block_error_action(const char *buf, int is_read)
1984
{
1985
    if (!strcmp(buf, "ignore")) {
1986
        return BLOCK_ERR_IGNORE;
1987
    } else if (!is_read && !strcmp(buf, "enospc")) {
1988
        return BLOCK_ERR_STOP_ENOSPC;
1989
    } else if (!strcmp(buf, "stop")) {
1990
        return BLOCK_ERR_STOP_ANY;
1991
    } else if (!strcmp(buf, "report")) {
1992
        return BLOCK_ERR_REPORT;
1993
    } else {
1994
        fprintf(stderr, "qemu: '%s' invalid %s error action\n",
1995
            buf, is_read ? "read" : "write");
1996
        return -1;
1997
    }
1998
}
1999

    
2000
DriveInfo *drive_init(QemuOpts *opts, void *opaque,
2001
                      int *fatal_error)
2002
{
2003
    const char *buf;
2004
    const char *file = NULL;
2005
    char devname[128];
2006
    const char *serial;
2007
    const char *mediastr = "";
2008
    BlockInterfaceType type;
2009
    enum { MEDIA_DISK, MEDIA_CDROM } media;
2010
    int bus_id, unit_id;
2011
    int cyls, heads, secs, translation;
2012
    BlockDriver *drv = NULL;
2013
    QEMUMachine *machine = opaque;
2014
    int max_devs;
2015
    int index;
2016
    int cache;
2017
    int aio = 0;
2018
    int ro = 0;
2019
    int bdrv_flags;
2020
    int on_read_error, on_write_error;
2021
    const char *devaddr;
2022
    DriveInfo *dinfo;
2023
    int snapshot = 0;
2024

    
2025
    *fatal_error = 1;
2026

    
2027
    translation = BIOS_ATA_TRANSLATION_AUTO;
2028
    cache = 1;
2029

    
2030
    if (machine && machine->use_scsi) {
2031
        type = IF_SCSI;
2032
        max_devs = MAX_SCSI_DEVS;
2033
        pstrcpy(devname, sizeof(devname), "scsi");
2034
    } else {
2035
        type = IF_IDE;
2036
        max_devs = MAX_IDE_DEVS;
2037
        pstrcpy(devname, sizeof(devname), "ide");
2038
    }
2039
    media = MEDIA_DISK;
2040

    
2041
    /* extract parameters */
2042
    bus_id  = qemu_opt_get_number(opts, "bus", 0);
2043
    unit_id = qemu_opt_get_number(opts, "unit", -1);
2044
    index   = qemu_opt_get_number(opts, "index", -1);
2045

    
2046
    cyls  = qemu_opt_get_number(opts, "cyls", 0);
2047
    heads = qemu_opt_get_number(opts, "heads", 0);
2048
    secs  = qemu_opt_get_number(opts, "secs", 0);
2049

    
2050
    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
2051
    ro = qemu_opt_get_bool(opts, "readonly", 0);
2052

    
2053
    file = qemu_opt_get(opts, "file");
2054
    serial = qemu_opt_get(opts, "serial");
2055

    
2056
    if ((buf = qemu_opt_get(opts, "if")) != NULL) {
2057
        pstrcpy(devname, sizeof(devname), buf);
2058
        if (!strcmp(buf, "ide")) {
2059
            type = IF_IDE;
2060
            max_devs = MAX_IDE_DEVS;
2061
        } else if (!strcmp(buf, "scsi")) {
2062
            type = IF_SCSI;
2063
            max_devs = MAX_SCSI_DEVS;
2064
        } else if (!strcmp(buf, "floppy")) {
2065
            type = IF_FLOPPY;
2066
            max_devs = 0;
2067
        } else if (!strcmp(buf, "pflash")) {
2068
            type = IF_PFLASH;
2069
            max_devs = 0;
2070
        } else if (!strcmp(buf, "mtd")) {
2071
            type = IF_MTD;
2072
            max_devs = 0;
2073
        } else if (!strcmp(buf, "sd")) {
2074
            type = IF_SD;
2075
            max_devs = 0;
2076
        } else if (!strcmp(buf, "virtio")) {
2077
            type = IF_VIRTIO;
2078
            max_devs = 0;
2079
        } else if (!strcmp(buf, "xen")) {
2080
            type = IF_XEN;
2081
            max_devs = 0;
2082
        } else if (!strcmp(buf, "none")) {
2083
            type = IF_NONE;
2084
            max_devs = 0;
2085
        } else {
2086
            fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
2087
            return NULL;
2088
        }
2089
    }
2090

    
2091
    if (cyls || heads || secs) {
2092
        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
2093
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
2094
            return NULL;
2095
        }
2096
        if (heads < 1 || (type == IF_IDE && heads > 16)) {
2097
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
2098
            return NULL;
2099
        }
2100
        if (secs < 1 || (type == IF_IDE && secs > 63)) {
2101
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
2102
            return NULL;
2103
        }
2104
    }
2105

    
2106
    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
2107
        if (!cyls) {
2108
            fprintf(stderr,
2109
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
2110
                    buf);
2111
            return NULL;
2112
        }
2113
        if (!strcmp(buf, "none"))
2114
            translation = BIOS_ATA_TRANSLATION_NONE;
2115
        else if (!strcmp(buf, "lba"))
2116
            translation = BIOS_ATA_TRANSLATION_LBA;
2117
        else if (!strcmp(buf, "auto"))
2118
            translation = BIOS_ATA_TRANSLATION_AUTO;
2119
        else {
2120
            fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
2121
            return NULL;
2122
        }
2123
    }
2124

    
2125
    if ((buf = qemu_opt_get(opts, "media")) != NULL) {
2126
        if (!strcmp(buf, "disk")) {
2127
            media = MEDIA_DISK;
2128
        } else if (!strcmp(buf, "cdrom")) {
2129
            if (cyls || secs || heads) {
2130
                fprintf(stderr,
2131
                        "qemu: '%s' invalid physical CHS format\n", buf);
2132
                return NULL;
2133
            }
2134
            media = MEDIA_CDROM;
2135
        } else {
2136
            fprintf(stderr, "qemu: '%s' invalid media\n", buf);
2137
            return NULL;
2138
        }
2139
    }
2140

    
2141
    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
2142
        if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2143
            cache = 0;
2144
        else if (!strcmp(buf, "writethrough"))
2145
            cache = 1;
2146
        else if (!strcmp(buf, "writeback"))
2147
            cache = 2;
2148
        else {
2149
           fprintf(stderr, "qemu: invalid cache option\n");
2150
           return NULL;
2151
        }
2152
    }
2153

    
2154
#ifdef CONFIG_LINUX_AIO
2155
    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2156
        if (!strcmp(buf, "threads"))
2157
            aio = 0;
2158
        else if (!strcmp(buf, "native"))
2159
            aio = 1;
2160
        else {
2161
           fprintf(stderr, "qemu: invalid aio option\n");
2162
           return NULL;
2163
        }
2164
    }
2165
#endif
2166

    
2167
    if ((buf = qemu_opt_get(opts, "format")) != NULL) {
2168
       if (strcmp(buf, "?") == 0) {
2169
            fprintf(stderr, "qemu: Supported formats:");
2170
            bdrv_iterate_format(bdrv_format_print, NULL);
2171
            fprintf(stderr, "\n");
2172
            return NULL;
2173
        }
2174
        drv = bdrv_find_whitelisted_format(buf);
2175
        if (!drv) {
2176
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2177
            return NULL;
2178
        }
2179
    }
2180

    
2181
    on_write_error = BLOCK_ERR_STOP_ENOSPC;
2182
    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
2183
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
2184
            fprintf(stderr, "werror is no supported by this format\n");
2185
            return NULL;
2186
        }
2187

    
2188
        on_write_error = parse_block_error_action(buf, 0);
2189
        if (on_write_error < 0) {
2190
            return NULL;
2191
        }
2192
    }
2193

    
2194
    on_read_error = BLOCK_ERR_REPORT;
2195
    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
2196
        if (type != IF_IDE && type != IF_VIRTIO) {
2197
            fprintf(stderr, "rerror is no supported by this format\n");
2198
            return NULL;
2199
        }
2200

    
2201
        on_read_error = parse_block_error_action(buf, 1);
2202
        if (on_read_error < 0) {
2203
            return NULL;
2204
        }
2205
    }
2206

    
2207
    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
2208
        if (type != IF_VIRTIO) {
2209
            fprintf(stderr, "addr is not supported\n");
2210
            return NULL;
2211
        }
2212
    }
2213

    
2214
    /* compute bus and unit according index */
2215

    
2216
    if (index != -1) {
2217
        if (bus_id != 0 || unit_id != -1) {
2218
            fprintf(stderr,
2219
                    "qemu: index cannot be used with bus and unit\n");
2220
            return NULL;
2221
        }
2222
        if (max_devs == 0)
2223
        {
2224
            unit_id = index;
2225
            bus_id = 0;
2226
        } else {
2227
            unit_id = index % max_devs;
2228
            bus_id = index / max_devs;
2229
        }
2230
    }
2231

    
2232
    /* if user doesn't specify a unit_id,
2233
     * try to find the first free
2234
     */
2235

    
2236
    if (unit_id == -1) {
2237
       unit_id = 0;
2238
       while (drive_get(type, bus_id, unit_id) != NULL) {
2239
           unit_id++;
2240
           if (max_devs && unit_id >= max_devs) {
2241
               unit_id -= max_devs;
2242
               bus_id++;
2243
           }
2244
       }
2245
    }
2246

    
2247
    /* check unit id */
2248

    
2249
    if (max_devs && unit_id >= max_devs) {
2250
        fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2251
                unit_id, max_devs - 1);
2252
        return NULL;
2253
    }
2254

    
2255
    /*
2256
     * ignore multiple definitions
2257
     */
2258

    
2259
    if (drive_get(type, bus_id, unit_id) != NULL) {
2260
        *fatal_error = 0;
2261
        return NULL;
2262
    }
2263

    
2264
    /* init */
2265

    
2266
    dinfo = qemu_mallocz(sizeof(*dinfo));
2267
    if ((buf = qemu_opts_id(opts)) != NULL) {
2268
        dinfo->id = qemu_strdup(buf);
2269
    } else {
2270
        /* no id supplied -> create one */
2271
        dinfo->id = qemu_mallocz(32);
2272
        if (type == IF_IDE || type == IF_SCSI)
2273
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2274
        if (max_devs)
2275
            snprintf(dinfo->id, 32, "%s%i%s%i",
2276
                     devname, bus_id, mediastr, unit_id);
2277
        else
2278
            snprintf(dinfo->id, 32, "%s%s%i",
2279
                     devname, mediastr, unit_id);
2280
    }
2281
    dinfo->bdrv = bdrv_new(dinfo->id);
2282
    dinfo->devaddr = devaddr;
2283
    dinfo->type = type;
2284
    dinfo->bus = bus_id;
2285
    dinfo->unit = unit_id;
2286
    dinfo->on_read_error = on_read_error;
2287
    dinfo->on_write_error = on_write_error;
2288
    dinfo->opts = opts;
2289
    if (serial)
2290
        strncpy(dinfo->serial, serial, sizeof(serial));
2291
    QTAILQ_INSERT_TAIL(&drives, dinfo, next);
2292

    
2293
    switch(type) {
2294
    case IF_IDE:
2295
    case IF_SCSI:
2296
    case IF_XEN:
2297
    case IF_NONE:
2298
        switch(media) {
2299
        case MEDIA_DISK:
2300
            if (cyls != 0) {
2301
                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2302
                bdrv_set_translation_hint(dinfo->bdrv, translation);
2303
            }
2304
            break;
2305
        case MEDIA_CDROM:
2306
            bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
2307
            break;
2308
        }
2309
        break;
2310
    case IF_SD:
2311
        /* FIXME: This isn't really a floppy, but it's a reasonable
2312
           approximation.  */
2313
    case IF_FLOPPY:
2314
        bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
2315
        break;
2316
    case IF_PFLASH:
2317
    case IF_MTD:
2318
        break;
2319
    case IF_VIRTIO:
2320
        /* add virtio block device */
2321
        opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2322
        qemu_opt_set(opts, "driver", "virtio-blk-pci");
2323
        qemu_opt_set(opts, "drive", dinfo->id);
2324
        if (devaddr)
2325
            qemu_opt_set(opts, "addr", devaddr);
2326
        break;
2327
    case IF_COUNT:
2328
        abort();
2329
    }
2330
    if (!file) {
2331
        *fatal_error = 0;
2332
        return NULL;
2333
    }
2334
    bdrv_flags = 0;
2335
    if (snapshot) {
2336
        bdrv_flags |= BDRV_O_SNAPSHOT;
2337
        cache = 2; /* always use write-back with snapshot */
2338
    }
2339
    if (cache == 0) /* no caching */
2340
        bdrv_flags |= BDRV_O_NOCACHE;
2341
    else if (cache == 2) /* write-back */
2342
        bdrv_flags |= BDRV_O_CACHE_WB;
2343

    
2344
    if (aio == 1) {
2345
        bdrv_flags |= BDRV_O_NATIVE_AIO;
2346
    } else {
2347
        bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2348
    }
2349

    
2350
    if (ro == 1) {
2351
        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
2352
            fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
2353
            return NULL;
2354
        }
2355
    }
2356
    /* 
2357
     * cdrom is read-only. Set it now, after above interface checking
2358
     * since readonly attribute not explicitly required, so no error.
2359
     */
2360
    if (media == MEDIA_CDROM) {
2361
        ro = 1;
2362
    }
2363
    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
2364

    
2365
    if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
2366
        fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2367
                        file, strerror(errno));
2368
        return NULL;
2369
    }
2370

    
2371
    if (bdrv_key_required(dinfo->bdrv))
2372
        autostart = 0;
2373
    *fatal_error = 0;
2374
    return dinfo;
2375
}
2376

    
2377
static int drive_init_func(QemuOpts *opts, void *opaque)
2378
{
2379
    QEMUMachine *machine = opaque;
2380
    int fatal_error = 0;
2381

    
2382
    if (drive_init(opts, machine, &fatal_error) == NULL) {
2383
        if (fatal_error)
2384
            return 1;
2385
    }
2386
    return 0;
2387
}
2388

    
2389
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2390
{
2391
    if (NULL == qemu_opt_get(opts, "snapshot")) {
2392
        qemu_opt_set(opts, "snapshot", "on");
2393
    }
2394
    return 0;
2395
}
2396

    
2397
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2398
{
2399
    boot_set_handler = func;
2400
    boot_set_opaque = opaque;
2401
}
2402

    
2403
int qemu_boot_set(const char *boot_devices)
2404
{
2405
    if (!boot_set_handler) {
2406
        return -EINVAL;
2407
    }
2408
    return boot_set_handler(boot_set_opaque, boot_devices);
2409
}
2410

    
2411
static int parse_bootdevices(char *devices)
2412
{
2413
    /* We just do some generic consistency checks */
2414
    const char *p;
2415
    int bitmap = 0;
2416

    
2417
    for (p = devices; *p != '\0'; p++) {
2418
        /* Allowed boot devices are:
2419
         * a-b: floppy disk drives
2420
         * c-f: IDE disk drives
2421
         * g-m: machine implementation dependant drives
2422
         * n-p: network devices
2423
         * It's up to each machine implementation to check if the given boot
2424
         * devices match the actual hardware implementation and firmware
2425
         * features.
2426
         */
2427
        if (*p < 'a' || *p > 'p') {
2428
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
2429
            exit(1);
2430
        }
2431
        if (bitmap & (1 << (*p - 'a'))) {
2432
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2433
            exit(1);
2434
        }
2435
        bitmap |= 1 << (*p - 'a');
2436
    }
2437
    return bitmap;
2438
}
2439

    
2440
static void restore_boot_devices(void *opaque)
2441
{
2442
    char *standard_boot_devices = opaque;
2443

    
2444
    qemu_boot_set(standard_boot_devices);
2445

    
2446
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2447
    qemu_free(standard_boot_devices);
2448
}
2449

    
2450
static void numa_add(const char *optarg)
2451
{
2452
    char option[128];
2453
    char *endptr;
2454
    unsigned long long value, endvalue;
2455
    int nodenr;
2456

    
2457
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
2458
    if (!strcmp(option, "node")) {
2459
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2460
            nodenr = nb_numa_nodes;
2461
        } else {
2462
            nodenr = strtoull(option, NULL, 10);
2463
        }
2464

    
2465
        if (get_param_value(option, 128, "mem", optarg) == 0) {
2466
            node_mem[nodenr] = 0;
2467
        } else {
2468
            value = strtoull(option, &endptr, 0);
2469
            switch (*endptr) {
2470
            case 0: case 'M': case 'm':
2471
                value <<= 20;
2472
                break;
2473
            case 'G': case 'g':
2474
                value <<= 30;
2475
                break;
2476
            }
2477
            node_mem[nodenr] = value;
2478
        }
2479
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
2480
            node_cpumask[nodenr] = 0;
2481
        } else {
2482
            value = strtoull(option, &endptr, 10);
2483
            if (value >= 64) {
2484
                value = 63;
2485
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2486
            } else {
2487
                if (*endptr == '-') {
2488
                    endvalue = strtoull(endptr+1, &endptr, 10);
2489
                    if (endvalue >= 63) {
2490
                        endvalue = 62;
2491
                        fprintf(stderr,
2492
                            "only 63 CPUs in NUMA mode supported.\n");
2493
                    }
2494
                    value = (2ULL << endvalue) - (1ULL << value);
2495
                } else {
2496
                    value = 1ULL << value;
2497
                }
2498
            }
2499
            node_cpumask[nodenr] = value;
2500
        }
2501
        nb_numa_nodes++;
2502
    }
2503
    return;
2504
}
2505

    
2506
static void smp_parse(const char *optarg)
2507
{
2508
    int smp, sockets = 0, threads = 0, cores = 0;
2509
    char *endptr;
2510
    char option[128];
2511

    
2512
    smp = strtoul(optarg, &endptr, 10);
2513
    if (endptr != optarg) {
2514
        if (*endptr == ',') {
2515
            endptr++;
2516
        }
2517
    }
2518
    if (get_param_value(option, 128, "sockets", endptr) != 0)
2519
        sockets = strtoull(option, NULL, 10);
2520
    if (get_param_value(option, 128, "cores", endptr) != 0)
2521
        cores = strtoull(option, NULL, 10);
2522
    if (get_param_value(option, 128, "threads", endptr) != 0)
2523
        threads = strtoull(option, NULL, 10);
2524
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2525
        max_cpus = strtoull(option, NULL, 10);
2526

    
2527
    /* compute missing values, prefer sockets over cores over threads */
2528
    if (smp == 0 || sockets == 0) {
2529
        sockets = sockets > 0 ? sockets : 1;
2530
        cores = cores > 0 ? cores : 1;
2531
        threads = threads > 0 ? threads : 1;
2532
        if (smp == 0) {
2533
            smp = cores * threads * sockets;
2534
        }
2535
    } else {
2536
        if (cores == 0) {
2537
            threads = threads > 0 ? threads : 1;
2538
            cores = smp / (sockets * threads);
2539
        } else {
2540
            if (sockets) {
2541
                threads = smp / (cores * sockets);
2542
            }
2543
        }
2544
    }
2545
    smp_cpus = smp;
2546
    smp_cores = cores > 0 ? cores : 1;
2547
    smp_threads = threads > 0 ? threads : 1;
2548
    if (max_cpus == 0)
2549
        max_cpus = smp_cpus;
2550
}
2551

    
2552
/***********************************************************/
2553
/* USB devices */
2554

    
2555
static int usb_device_add(const char *devname, int is_hotplug)
2556
{
2557
    const char *p;
2558
    USBDevice *dev = NULL;
2559

    
2560
    if (!usb_enabled)
2561
        return -1;
2562

    
2563
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
2564
    dev = usbdevice_create(devname);
2565
    if (dev)
2566
        goto done;
2567

    
2568
    /* the other ones */
2569
    if (strstart(devname, "host:", &p)) {
2570
        dev = usb_host_device_open(p);
2571
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2572
        dev = usb_bt_init(devname[2] ? hci_init(p) :
2573
                        bt_new_hci(qemu_find_bt_vlan(0)));
2574
    } else {
2575
        return -1;
2576
    }
2577
    if (!dev)
2578
        return -1;
2579

    
2580
done:
2581
    return 0;
2582
}
2583

    
2584
static int usb_device_del(const char *devname)
2585
{
2586
    int bus_num, addr;
2587
    const char *p;
2588

    
2589
    if (strstart(devname, "host:", &p))
2590
        return usb_host_device_close(p);
2591

    
2592
    if (!usb_enabled)
2593
        return -1;
2594

    
2595
    p = strchr(devname, '.');
2596
    if (!p)
2597
        return -1;
2598
    bus_num = strtoul(devname, NULL, 0);
2599
    addr = strtoul(p + 1, NULL, 0);
2600

    
2601
    return usb_device_delete_addr(bus_num, addr);
2602
}
2603

    
2604
static int usb_parse(const char *cmdline)
2605
{
2606
    int r;
2607
    r = usb_device_add(cmdline, 0);
2608
    if (r < 0) {
2609
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
2610
    }
2611
    return r;
2612
}
2613

    
2614
void do_usb_add(Monitor *mon, const QDict *qdict)
2615
{
2616
    const char *devname = qdict_get_str(qdict, "devname");
2617
    if (usb_device_add(devname, 1) < 0) {
2618
        error_report("could not add USB device '%s'", devname);
2619
    }
2620
}
2621

    
2622
void do_usb_del(Monitor *mon, const QDict *qdict)
2623
{
2624
    const char *devname = qdict_get_str(qdict, "devname");
2625
    if (usb_device_del(devname) < 0) {
2626
        error_report("could not delete USB device '%s'", devname);
2627
    }
2628
}
2629

    
2630
/***********************************************************/
2631
/* PCMCIA/Cardbus */
2632

    
2633
static struct pcmcia_socket_entry_s {
2634
    PCMCIASocket *socket;
2635
    struct pcmcia_socket_entry_s *next;
2636
} *pcmcia_sockets = 0;
2637

    
2638
void pcmcia_socket_register(PCMCIASocket *socket)
2639
{
2640
    struct pcmcia_socket_entry_s *entry;
2641

    
2642
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2643
    entry->socket = socket;
2644
    entry->next = pcmcia_sockets;
2645
    pcmcia_sockets = entry;
2646
}
2647

    
2648
void pcmcia_socket_unregister(PCMCIASocket *socket)
2649
{
2650
    struct pcmcia_socket_entry_s *entry, **ptr;
2651

    
2652
    ptr = &pcmcia_sockets;
2653
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2654
        if (entry->socket == socket) {
2655
            *ptr = entry->next;
2656
            qemu_free(entry);
2657
        }
2658
}
2659

    
2660
void pcmcia_info(Monitor *mon)
2661
{
2662
    struct pcmcia_socket_entry_s *iter;
2663

    
2664
    if (!pcmcia_sockets)
2665
        monitor_printf(mon, "No PCMCIA sockets\n");
2666

    
2667
    for (iter = pcmcia_sockets; iter; iter = iter->next)
2668
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2669
                       iter->socket->attached ? iter->socket->card_string :
2670
                       "Empty");
2671
}
2672

    
2673
/***********************************************************/
2674
/* I/O handling */
2675

    
2676
typedef struct IOHandlerRecord {
2677
    int fd;
2678
    IOCanRWHandler *fd_read_poll;
2679
    IOHandler *fd_read;
2680
    IOHandler *fd_write;
2681
    int deleted;
2682
    void *opaque;
2683
    /* temporary data */
2684
    struct pollfd *ufd;
2685
    struct IOHandlerRecord *next;
2686
} IOHandlerRecord;
2687

    
2688
static IOHandlerRecord *first_io_handler;
2689

    
2690
/* XXX: fd_read_poll should be suppressed, but an API change is
2691
   necessary in the character devices to suppress fd_can_read(). */
2692
int qemu_set_fd_handler2(int fd,
2693
                         IOCanRWHandler *fd_read_poll,
2694
                         IOHandler *fd_read,
2695
                         IOHandler *fd_write,
2696
                         void *opaque)
2697
{
2698
    IOHandlerRecord **pioh, *ioh;
2699

    
2700
    if (!fd_read && !fd_write) {
2701
        pioh = &first_io_handler;
2702
        for(;;) {
2703
            ioh = *pioh;
2704
            if (ioh == NULL)
2705
                break;
2706
            if (ioh->fd == fd) {
2707
                ioh->deleted = 1;
2708
                break;
2709
            }
2710
            pioh = &ioh->next;
2711
        }
2712
    } else {
2713
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2714
            if (ioh->fd == fd)
2715
                goto found;
2716
        }
2717
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2718
        ioh->next = first_io_handler;
2719
        first_io_handler = ioh;
2720
    found:
2721
        ioh->fd = fd;
2722
        ioh->fd_read_poll = fd_read_poll;
2723
        ioh->fd_read = fd_read;
2724
        ioh->fd_write = fd_write;
2725
        ioh->opaque = opaque;
2726
        ioh->deleted = 0;
2727
    }
2728
    return 0;
2729
}
2730

    
2731
int qemu_set_fd_handler(int fd,
2732
                        IOHandler *fd_read,
2733
                        IOHandler *fd_write,
2734
                        void *opaque)
2735
{
2736
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2737
}
2738

    
2739
#ifdef _WIN32
2740
/***********************************************************/
2741
/* Polling handling */
2742

    
2743
typedef struct PollingEntry {
2744
    PollingFunc *func;
2745
    void *opaque;
2746
    struct PollingEntry *next;
2747
} PollingEntry;
2748

    
2749
static PollingEntry *first_polling_entry;
2750

    
2751
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2752
{
2753
    PollingEntry **ppe, *pe;
2754
    pe = qemu_mallocz(sizeof(PollingEntry));
2755
    pe->func = func;
2756
    pe->opaque = opaque;
2757
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2758
    *ppe = pe;
2759
    return 0;
2760
}
2761

    
2762
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2763
{
2764
    PollingEntry **ppe, *pe;
2765
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2766
        pe = *ppe;
2767
        if (pe->func == func && pe->opaque == opaque) {
2768
            *ppe = pe->next;
2769
            qemu_free(pe);
2770
            break;
2771
        }
2772
    }
2773
}
2774

    
2775
/***********************************************************/
2776
/* Wait objects support */
2777
typedef struct WaitObjects {
2778
    int num;
2779
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2780
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2781
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2782
} WaitObjects;
2783

    
2784
static WaitObjects wait_objects = {0};
2785

    
2786
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2787
{
2788
    WaitObjects *w = &wait_objects;
2789

    
2790
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
2791
        return -1;
2792
    w->events[w->num] = handle;
2793
    w->func[w->num] = func;
2794
    w->opaque[w->num] = opaque;
2795
    w->num++;
2796
    return 0;
2797
}
2798

    
2799
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2800
{
2801
    int i, found;
2802
    WaitObjects *w = &wait_objects;
2803

    
2804
    found = 0;
2805
    for (i = 0; i < w->num; i++) {
2806
        if (w->events[i] == handle)
2807
            found = 1;
2808
        if (found) {
2809
            w->events[i] = w->events[i + 1];
2810
            w->func[i] = w->func[i + 1];
2811
            w->opaque[i] = w->opaque[i + 1];
2812
        }
2813
    }
2814
    if (found)
2815
        w->num--;
2816
}
2817
#endif
2818

    
2819
/***********************************************************/
2820
/* ram save/restore */
2821

    
2822
#define RAM_SAVE_FLAG_FULL        0x01 /* Obsolete, not used anymore */
2823
#define RAM_SAVE_FLAG_COMPRESS        0x02
2824
#define RAM_SAVE_FLAG_MEM_SIZE        0x04
2825
#define RAM_SAVE_FLAG_PAGE        0x08
2826
#define RAM_SAVE_FLAG_EOS        0x10
2827

    
2828
static int is_dup_page(uint8_t *page, uint8_t ch)
2829
{
2830
    uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2831
    uint32_t *array = (uint32_t *)page;
2832
    int i;
2833

    
2834
    for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2835
        if (array[i] != val)
2836
            return 0;
2837
    }
2838

    
2839
    return 1;
2840
}
2841

    
2842
static int ram_save_block(QEMUFile *f)
2843
{
2844
    static ram_addr_t current_addr = 0;
2845
    ram_addr_t saved_addr = current_addr;
2846
    ram_addr_t addr = 0;
2847
    int found = 0;
2848

    
2849
    while (addr < last_ram_offset) {
2850
        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
2851
            uint8_t *p;
2852

    
2853
            cpu_physical_memory_reset_dirty(current_addr,
2854
                                            current_addr + TARGET_PAGE_SIZE,
2855
                                            MIGRATION_DIRTY_FLAG);
2856

    
2857
            p = qemu_get_ram_ptr(current_addr);
2858

    
2859
            if (is_dup_page(p, *p)) {
2860
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
2861
                qemu_put_byte(f, *p);
2862
            } else {
2863
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
2864
                qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
2865
            }
2866

    
2867
            found = 1;
2868
            break;
2869
        }
2870
        addr += TARGET_PAGE_SIZE;
2871
        current_addr = (saved_addr + addr) % last_ram_offset;
2872
    }
2873

    
2874
    return found;
2875
}
2876

    
2877
static uint64_t bytes_transferred;
2878

    
2879
static ram_addr_t ram_save_remaining(void)
2880
{
2881
    ram_addr_t addr;
2882
    ram_addr_t count = 0;
2883

    
2884
    for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2885
        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2886
            count++;
2887
    }
2888

    
2889
    return count;
2890
}
2891

    
2892
uint64_t ram_bytes_remaining(void)
2893
{
2894
    return ram_save_remaining() * TARGET_PAGE_SIZE;
2895
}
2896

    
2897
uint64_t ram_bytes_transferred(void)
2898
{
2899
    return bytes_transferred;
2900
}
2901

    
2902
uint64_t ram_bytes_total(void)
2903
{
2904
    return last_ram_offset;
2905
}
2906

    
2907
static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
2908
{
2909
    ram_addr_t addr;
2910
    uint64_t bytes_transferred_last;
2911
    double bwidth = 0;
2912
    uint64_t expected_time = 0;
2913

    
2914
    if (stage < 0) {
2915
        cpu_physical_memory_set_dirty_tracking(0);
2916
        return 0;
2917
    }
2918

    
2919
    if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
2920
        qemu_file_set_error(f);
2921
        return 0;
2922
    }
2923

    
2924
    if (stage == 1) {
2925
        bytes_transferred = 0;
2926

    
2927
        /* Make sure all dirty bits are set */
2928
        for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2929
            if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2930
                cpu_physical_memory_set_dirty(addr);
2931
        }
2932

    
2933
        /* Enable dirty memory tracking */
2934
        cpu_physical_memory_set_dirty_tracking(1);
2935

    
2936
        qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2937
    }
2938

    
2939
    bytes_transferred_last = bytes_transferred;
2940
    bwidth = qemu_get_clock_ns(rt_clock);
2941

    
2942
    while (!qemu_file_rate_limit(f)) {
2943
        int ret;
2944

    
2945
        ret = ram_save_block(f);
2946
        bytes_transferred += ret * TARGET_PAGE_SIZE;
2947
        if (ret == 0) /* no more blocks */
2948
            break;
2949
    }
2950

    
2951
    bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
2952
    bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2953

    
2954
    /* if we haven't transferred anything this round, force expected_time to a
2955
     * a very high value, but without crashing */
2956
    if (bwidth == 0)
2957
        bwidth = 0.000001;
2958

    
2959
    /* try transferring iterative blocks of memory */
2960
    if (stage == 3) {
2961
        /* flush all remaining blocks regardless of rate limiting */
2962
        while (ram_save_block(f) != 0) {
2963
            bytes_transferred += TARGET_PAGE_SIZE;
2964
        }
2965
        cpu_physical_memory_set_dirty_tracking(0);
2966
    }
2967

    
2968
    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2969

    
2970
    expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2971

    
2972
    return (stage == 2) && (expected_time <= migrate_max_downtime());
2973
}
2974

    
2975
static int ram_load(QEMUFile *f, void *opaque, int version_id)
2976
{
2977
    ram_addr_t addr;
2978
    int flags;
2979

    
2980
    if (version_id != 3)
2981
        return -EINVAL;
2982

    
2983
    do {
2984
        addr = qemu_get_be64(f);
2985

    
2986
        flags = addr & ~TARGET_PAGE_MASK;
2987
        addr &= TARGET_PAGE_MASK;
2988

    
2989
        if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2990
            if (addr != last_ram_offset)
2991
                return -EINVAL;
2992
        }
2993

    
2994
        if (flags & RAM_SAVE_FLAG_COMPRESS) {
2995
            uint8_t ch = qemu_get_byte(f);
2996
            memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2997
#ifndef _WIN32
2998
            if (ch == 0 &&
2999
                (!kvm_enabled() || kvm_has_sync_mmu())) {
3000
                madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
3001
            }
3002
#endif
3003
        } else if (flags & RAM_SAVE_FLAG_PAGE) {
3004
            qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
3005
        }
3006
        if (qemu_file_has_error(f)) {
3007
            return -EIO;
3008
        }
3009
    } while (!(flags & RAM_SAVE_FLAG_EOS));
3010

    
3011
    return 0;
3012
}
3013

    
3014
void qemu_service_io(void)
3015
{
3016
    qemu_notify_event();
3017
}
3018

    
3019
/***********************************************************/
3020
/* machine registration */
3021

    
3022
static QEMUMachine *first_machine = NULL;
3023
QEMUMachine *current_machine = NULL;
3024

    
3025
int qemu_register_machine(QEMUMachine *m)
3026
{
3027
    QEMUMachine **pm;
3028
    pm = &first_machine;
3029
    while (*pm != NULL)
3030
        pm = &(*pm)->next;
3031
    m->next = NULL;
3032
    *pm = m;
3033
    return 0;
3034
}
3035

    
3036
static QEMUMachine *find_machine(const char *name)
3037
{
3038
    QEMUMachine *m;
3039

    
3040
    for(m = first_machine; m != NULL; m = m->next) {
3041
        if (!strcmp(m->name, name))
3042
            return m;
3043
        if (m->alias && !strcmp(m->alias, name))
3044
            return m;
3045
    }
3046
    return NULL;
3047
}
3048

    
3049
static QEMUMachine *find_default_machine(void)
3050
{
3051
    QEMUMachine *m;
3052

    
3053
    for(m = first_machine; m != NULL; m = m->next) {
3054
        if (m->is_default) {
3055
            return m;
3056
        }
3057
    }
3058
    return NULL;
3059
}
3060

    
3061
/***********************************************************/
3062
/* main execution loop */
3063

    
3064
static void gui_update(void *opaque)
3065
{
3066
    uint64_t interval = GUI_REFRESH_INTERVAL;
3067
    DisplayState *ds = opaque;
3068
    DisplayChangeListener *dcl = ds->listeners;
3069

    
3070
    qemu_flush_coalesced_mmio_buffer();
3071
    dpy_refresh(ds);
3072

    
3073
    while (dcl != NULL) {
3074
        if (dcl->gui_timer_interval &&
3075
            dcl->gui_timer_interval < interval)
3076
            interval = dcl->gui_timer_interval;
3077
        dcl = dcl->next;
3078
    }
3079
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
3080
}
3081

    
3082
static void nographic_update(void *opaque)
3083
{
3084
    uint64_t interval = GUI_REFRESH_INTERVAL;
3085

    
3086
    qemu_flush_coalesced_mmio_buffer();
3087
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3088
}
3089

    
3090
void cpu_synchronize_all_states(void)
3091
{
3092
    CPUState *cpu;
3093

    
3094
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
3095
        cpu_synchronize_state(cpu);
3096
    }
3097
}
3098

    
3099
void cpu_synchronize_all_post_reset(void)
3100
{
3101
    CPUState *cpu;
3102

    
3103
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
3104
        cpu_synchronize_post_reset(cpu);
3105
    }
3106
}
3107

    
3108
void cpu_synchronize_all_post_init(void)
3109
{
3110
    CPUState *cpu;
3111

    
3112
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
3113
        cpu_synchronize_post_init(cpu);
3114
    }
3115
}
3116

    
3117
struct vm_change_state_entry {
3118
    VMChangeStateHandler *cb;
3119
    void *opaque;
3120
    QLIST_ENTRY (vm_change_state_entry) entries;
3121
};
3122

    
3123
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3124

    
3125
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3126
                                                     void *opaque)
3127
{
3128
    VMChangeStateEntry *e;
3129

    
3130
    e = qemu_mallocz(sizeof (*e));
3131

    
3132
    e->cb = cb;
3133
    e->opaque = opaque;
3134
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3135
    return e;
3136
}
3137

    
3138
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3139
{
3140
    QLIST_REMOVE (e, entries);
3141
    qemu_free (e);
3142
}
3143

    
3144
static void vm_state_notify(int running, int reason)
3145
{
3146
    VMChangeStateEntry *e;
3147

    
3148
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3149
        e->cb(e->opaque, running, reason);
3150
    }
3151
}
3152

    
3153
static void resume_all_vcpus(void);
3154
static void pause_all_vcpus(void);
3155

    
3156
void vm_start(void)
3157
{
3158
    if (!vm_running) {
3159
        cpu_enable_ticks();
3160
        vm_running = 1;
3161
        vm_state_notify(1, 0);
3162
        resume_all_vcpus();
3163
    }
3164
}
3165

    
3166
/* reset/shutdown handler */
3167

    
3168
typedef struct QEMUResetEntry {
3169
    QTAILQ_ENTRY(QEMUResetEntry) entry;
3170
    QEMUResetHandler *func;
3171
    void *opaque;
3172
} QEMUResetEntry;
3173

    
3174
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3175
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
3176
static int reset_requested;
3177
static int shutdown_requested;
3178
static int powerdown_requested;
3179
static int debug_requested;
3180
static int vmstop_requested;
3181

    
3182
int qemu_shutdown_requested(void)
3183
{
3184
    int r = shutdown_requested;
3185
    shutdown_requested = 0;
3186
    return r;
3187
}
3188

    
3189
int qemu_reset_requested(void)
3190
{
3191
    int r = reset_requested;
3192
    reset_requested = 0;
3193
    return r;
3194
}
3195

    
3196
int qemu_powerdown_requested(void)
3197
{
3198
    int r = powerdown_requested;
3199
    powerdown_requested = 0;
3200
    return r;
3201
}
3202

    
3203
static int qemu_debug_requested(void)
3204
{
3205
    int r = debug_requested;
3206
    debug_requested = 0;
3207
    return r;
3208
}
3209

    
3210
static int qemu_vmstop_requested(void)
3211
{
3212
    int r = vmstop_requested;
3213
    vmstop_requested = 0;
3214
    return r;
3215
}
3216

    
3217
static void do_vm_stop(int reason)
3218
{
3219
    if (vm_running) {
3220
        cpu_disable_ticks();
3221
        vm_running = 0;
3222
        pause_all_vcpus();
3223
        vm_state_notify(0, reason);
3224
        monitor_protocol_event(QEVENT_STOP, NULL);
3225
    }
3226
}
3227

    
3228
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3229
{
3230
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
3231

    
3232
    re->func = func;
3233
    re->opaque = opaque;
3234
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
3235
}
3236

    
3237
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
3238
{
3239
    QEMUResetEntry *re;
3240

    
3241
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
3242
        if (re->func == func && re->opaque == opaque) {
3243
            QTAILQ_REMOVE(&reset_handlers, re, entry);
3244
            qemu_free(re);
3245
            return;
3246
        }
3247
    }
3248
}
3249

    
3250
void qemu_system_reset(void)
3251
{
3252
    QEMUResetEntry *re, *nre;
3253

    
3254
    /* reset all devices */
3255
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
3256
        re->func(re->opaque);
3257
    }
3258
    monitor_protocol_event(QEVENT_RESET, NULL);
3259
    cpu_synchronize_all_post_reset();
3260
}
3261

    
3262
void qemu_system_reset_request(void)
3263
{
3264
    if (no_reboot) {
3265
        shutdown_requested = 1;
3266
    } else {
3267
        reset_requested = 1;
3268
    }
3269
    qemu_notify_event();
3270
}
3271

    
3272
void qemu_system_shutdown_request(void)
3273
{
3274
    shutdown_requested = 1;
3275
    qemu_notify_event();
3276
}
3277

    
3278
void qemu_system_powerdown_request(void)
3279
{
3280
    powerdown_requested = 1;
3281
    qemu_notify_event();
3282
}
3283

    
3284
#ifdef CONFIG_IOTHREAD
3285
static void qemu_system_vmstop_request(int reason)
3286
{
3287
    vmstop_requested = reason;
3288
    qemu_notify_event();
3289
}
3290
#endif
3291

    
3292
#ifndef _WIN32
3293
static int io_thread_fd = -1;
3294

    
3295
static void qemu_event_increment(void)
3296
{
3297
    /* Write 8 bytes to be compatible with eventfd.  */
3298
    static uint64_t val = 1;
3299
    ssize_t ret;
3300

    
3301
    if (io_thread_fd == -1)
3302
        return;
3303

    
3304
    do {
3305
        ret = write(io_thread_fd, &val, sizeof(val));
3306
    } while (ret < 0 && errno == EINTR);
3307

    
3308
    /* EAGAIN is fine, a read must be pending.  */
3309
    if (ret < 0 && errno != EAGAIN) {
3310
        fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
3311
                strerror(errno));
3312
        exit (1);
3313
    }
3314
}
3315

    
3316
static void qemu_event_read(void *opaque)
3317
{
3318
    int fd = (unsigned long)opaque;
3319
    ssize_t len;
3320
    char buffer[512];
3321

    
3322
    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
3323
    do {
3324
        len = read(fd, buffer, sizeof(buffer));
3325
    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
3326
}
3327

    
3328
static int qemu_event_init(void)
3329
{
3330
    int err;
3331
    int fds[2];
3332

    
3333
    err = qemu_eventfd(fds);
3334
    if (err == -1)
3335
        return -errno;
3336

    
3337
    err = fcntl_setfl(fds[0], O_NONBLOCK);
3338
    if (err < 0)
3339
        goto fail;
3340

    
3341
    err = fcntl_setfl(fds[1], O_NONBLOCK);
3342
    if (err < 0)
3343
        goto fail;
3344

    
3345
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3346
                         (void *)(unsigned long)fds[0]);
3347

    
3348
    io_thread_fd = fds[1];
3349
    return 0;
3350

    
3351
fail:
3352
    close(fds[0]);
3353
    close(fds[1]);
3354
    return err;
3355
}
3356
#else
3357
HANDLE qemu_event_handle;
3358

    
3359
static void dummy_event_handler(void *opaque)
3360
{
3361
}
3362

    
3363
static int qemu_event_init(void)
3364
{
3365
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3366
    if (!qemu_event_handle) {
3367
        fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
3368
        return -1;
3369
    }
3370
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3371
    return 0;
3372
}
3373

    
3374
static void qemu_event_increment(void)
3375
{
3376
    if (!SetEvent(qemu_event_handle)) {
3377
        fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
3378
                GetLastError());
3379
        exit (1);
3380
    }
3381
}
3382
#endif
3383

    
3384
static int cpu_can_run(CPUState *env)
3385
{
3386
    if (env->stop)
3387
        return 0;
3388
    if (env->stopped)
3389
        return 0;
3390
    if (!vm_running)
3391
        return 0;
3392
    return 1;
3393
}
3394

    
3395
#ifndef CONFIG_IOTHREAD
3396
static int qemu_init_main_loop(void)
3397
{
3398
    return qemu_event_init();
3399
}
3400

    
3401
void qemu_init_vcpu(void *_env)
3402
{
3403
    CPUState *env = _env;
3404

    
3405
    env->nr_cores = smp_cores;
3406
    env->nr_threads = smp_threads;
3407
    if (kvm_enabled())
3408
        kvm_init_vcpu(env);
3409
    return;
3410
}
3411

    
3412
int qemu_cpu_self(void *env)
3413
{
3414
    return 1;
3415
}
3416

    
3417
static void resume_all_vcpus(void)
3418
{
3419
}
3420

    
3421
static void pause_all_vcpus(void)
3422
{
3423
}
3424

    
3425
void qemu_cpu_kick(void *env)
3426
{
3427
    return;
3428
}
3429

    
3430
void qemu_notify_event(void)
3431
{
3432
    CPUState *env = cpu_single_env;
3433

    
3434
    qemu_event_increment ();
3435
    if (env) {
3436
        cpu_exit(env);
3437
    }
3438
    if (next_cpu && env != next_cpu) {
3439
        cpu_exit(next_cpu);
3440
    }
3441
}
3442

    
3443
void qemu_mutex_lock_iothread(void) {}
3444
void qemu_mutex_unlock_iothread(void) {}
3445

    
3446
void vm_stop(int reason)
3447
{
3448
    do_vm_stop(reason);
3449
}
3450

    
3451
#else /* CONFIG_IOTHREAD */
3452

    
3453
#include "qemu-thread.h"
3454

    
3455
QemuMutex qemu_global_mutex;
3456
static QemuMutex qemu_fair_mutex;
3457

    
3458
static QemuThread io_thread;
3459

    
3460
static QemuThread *tcg_cpu_thread;
3461
static QemuCond *tcg_halt_cond;
3462

    
3463
static int qemu_system_ready;
3464
/* cpu creation */
3465
static QemuCond qemu_cpu_cond;
3466
/* system init */
3467
static QemuCond qemu_system_cond;
3468
static QemuCond qemu_pause_cond;
3469

    
3470
static void tcg_block_io_signals(void);
3471
static void kvm_block_io_signals(CPUState *env);
3472
static void unblock_io_signals(void);
3473
static int tcg_has_work(void);
3474
static int cpu_has_work(CPUState *env);
3475

    
3476
static int qemu_init_main_loop(void)
3477
{
3478
    int ret;
3479

    
3480
    ret = qemu_event_init();
3481
    if (ret)
3482
        return ret;
3483

    
3484
    qemu_cond_init(&qemu_pause_cond);
3485
    qemu_mutex_init(&qemu_fair_mutex);
3486
    qemu_mutex_init(&qemu_global_mutex);
3487
    qemu_mutex_lock(&qemu_global_mutex);
3488

    
3489
    unblock_io_signals();
3490
    qemu_thread_self(&io_thread);
3491

    
3492
    return 0;
3493
}
3494

    
3495
static void qemu_wait_io_event_common(CPUState *env)
3496
{
3497
    if (env->stop) {
3498
        env->stop = 0;
3499
        env->stopped = 1;
3500
        qemu_cond_signal(&qemu_pause_cond);
3501
    }
3502
}
3503

    
3504
static void qemu_wait_io_event(CPUState *env)
3505
{
3506
    while (!tcg_has_work())
3507
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3508

    
3509
    qemu_mutex_unlock(&qemu_global_mutex);
3510

    
3511
    /*
3512
     * Users of qemu_global_mutex can be starved, having no chance
3513
     * to acquire it since this path will get to it first.
3514
     * So use another lock to provide fairness.
3515
     */
3516
    qemu_mutex_lock(&qemu_fair_mutex);
3517
    qemu_mutex_unlock(&qemu_fair_mutex);
3518

    
3519
    qemu_mutex_lock(&qemu_global_mutex);
3520
    qemu_wait_io_event_common(env);
3521
}
3522

    
3523
static void qemu_kvm_eat_signal(CPUState *env, int timeout)
3524
{
3525
    struct timespec ts;
3526
    int r, e;
3527
    siginfo_t siginfo;
3528
    sigset_t waitset;
3529

    
3530
    ts.tv_sec = timeout / 1000;
3531
    ts.tv_nsec = (timeout % 1000) * 1000000;
3532

    
3533
    sigemptyset(&waitset);
3534
    sigaddset(&waitset, SIG_IPI);
3535

    
3536
    qemu_mutex_unlock(&qemu_global_mutex);
3537
    r = sigtimedwait(&waitset, &siginfo, &ts);
3538
    e = errno;
3539
    qemu_mutex_lock(&qemu_global_mutex);
3540

    
3541
    if (r == -1 && !(e == EAGAIN || e == EINTR)) {
3542
        fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
3543
        exit(1);
3544
    }
3545
}
3546

    
3547
static void qemu_kvm_wait_io_event(CPUState *env)
3548
{
3549
    while (!cpu_has_work(env))
3550
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3551

    
3552
    qemu_kvm_eat_signal(env, 0);
3553
    qemu_wait_io_event_common(env);
3554
}
3555

    
3556
static int qemu_cpu_exec(CPUState *env);
3557

    
3558
static void *kvm_cpu_thread_fn(void *arg)
3559
{
3560
    CPUState *env = arg;
3561

    
3562
    qemu_thread_self(env->thread);
3563
    if (kvm_enabled())
3564
        kvm_init_vcpu(env);
3565

    
3566
    kvm_block_io_signals(env);
3567

    
3568
    /* signal CPU creation */
3569
    qemu_mutex_lock(&qemu_global_mutex);
3570
    env->created = 1;
3571
    qemu_cond_signal(&qemu_cpu_cond);
3572

    
3573
    /* and wait for machine initialization */
3574
    while (!qemu_system_ready)
3575
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3576

    
3577
    while (1) {
3578
        if (cpu_can_run(env))
3579
            qemu_cpu_exec(env);
3580
        qemu_kvm_wait_io_event(env);
3581
    }
3582

    
3583
    return NULL;
3584
}
3585

    
3586
static void tcg_cpu_exec(void);
3587

    
3588
static void *tcg_cpu_thread_fn(void *arg)
3589
{
3590
    CPUState *env = arg;
3591

    
3592
    tcg_block_io_signals();
3593
    qemu_thread_self(env->thread);
3594

    
3595
    /* signal CPU creation */
3596
    qemu_mutex_lock(&qemu_global_mutex);
3597
    for (env = first_cpu; env != NULL; env = env->next_cpu)
3598
        env->created = 1;
3599
    qemu_cond_signal(&qemu_cpu_cond);
3600

    
3601
    /* and wait for machine initialization */
3602
    while (!qemu_system_ready)
3603
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3604

    
3605
    while (1) {
3606
        tcg_cpu_exec();
3607
        qemu_wait_io_event(cur_cpu);
3608
    }
3609

    
3610
    return NULL;
3611
}
3612

    
3613
void qemu_cpu_kick(void *_env)
3614
{
3615
    CPUState *env = _env;
3616
    qemu_cond_broadcast(env->halt_cond);
3617
    if (kvm_enabled())
3618
        qemu_thread_signal(env->thread, SIG_IPI);
3619
}
3620

    
3621
int qemu_cpu_self(void *_env)
3622
{
3623
    CPUState *env = _env;
3624
    QemuThread this;
3625
 
3626
    qemu_thread_self(&this);
3627
 
3628
    return qemu_thread_equal(&this, env->thread);
3629
}
3630

    
3631
static void cpu_signal(int sig)
3632
{
3633
    if (cpu_single_env)
3634
        cpu_exit(cpu_single_env);
3635
}
3636

    
3637
static void tcg_block_io_signals(void)
3638
{
3639
    sigset_t set;
3640
    struct sigaction sigact;
3641

    
3642
    sigemptyset(&set);
3643
    sigaddset(&set, SIGUSR2);
3644
    sigaddset(&set, SIGIO);
3645
    sigaddset(&set, SIGALRM);
3646
    sigaddset(&set, SIGCHLD);
3647
    pthread_sigmask(SIG_BLOCK, &set, NULL);
3648

    
3649
    sigemptyset(&set);
3650
    sigaddset(&set, SIG_IPI);
3651
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3652

    
3653
    memset(&sigact, 0, sizeof(sigact));
3654
    sigact.sa_handler = cpu_signal;
3655
    sigaction(SIG_IPI, &sigact, NULL);
3656
}
3657

    
3658
static void dummy_signal(int sig)
3659
{
3660
}
3661

    
3662
static void kvm_block_io_signals(CPUState *env)
3663
{
3664
    int r;
3665
    sigset_t set;
3666
    struct sigaction sigact;
3667

    
3668
    sigemptyset(&set);
3669
    sigaddset(&set, SIGUSR2);
3670
    sigaddset(&set, SIGIO);
3671
    sigaddset(&set, SIGALRM);
3672
    sigaddset(&set, SIGCHLD);
3673
    sigaddset(&set, SIG_IPI);
3674
    pthread_sigmask(SIG_BLOCK, &set, NULL);
3675

    
3676
    pthread_sigmask(SIG_BLOCK, NULL, &set);
3677
    sigdelset(&set, SIG_IPI);
3678

    
3679
    memset(&sigact, 0, sizeof(sigact));
3680
    sigact.sa_handler = dummy_signal;
3681
    sigaction(SIG_IPI, &sigact, NULL);
3682

    
3683
    r = kvm_set_signal_mask(env, &set);
3684
    if (r) {
3685
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
3686
        exit(1);
3687
    }
3688
}
3689

    
3690
static void unblock_io_signals(void)
3691
{
3692
    sigset_t set;
3693

    
3694
    sigemptyset(&set);
3695
    sigaddset(&set, SIGUSR2);
3696
    sigaddset(&set, SIGIO);
3697
    sigaddset(&set, SIGALRM);
3698
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3699

    
3700
    sigemptyset(&set);
3701
    sigaddset(&set, SIG_IPI);
3702
    pthread_sigmask(SIG_BLOCK, &set, NULL);
3703
}
3704

    
3705
static void qemu_signal_lock(unsigned int msecs)
3706
{
3707
    qemu_mutex_lock(&qemu_fair_mutex);
3708

    
3709
    while (qemu_mutex_trylock(&qemu_global_mutex)) {
3710
        qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
3711
        if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3712
            break;
3713
    }
3714
    qemu_mutex_unlock(&qemu_fair_mutex);
3715
}
3716

    
3717
void qemu_mutex_lock_iothread(void)
3718
{
3719
    if (kvm_enabled()) {
3720
        qemu_mutex_lock(&qemu_fair_mutex);
3721
        qemu_mutex_lock(&qemu_global_mutex);
3722
        qemu_mutex_unlock(&qemu_fair_mutex);
3723
    } else
3724
        qemu_signal_lock(100);
3725
}
3726

    
3727
void qemu_mutex_unlock_iothread(void)
3728
{
3729
    qemu_mutex_unlock(&qemu_global_mutex);
3730
}
3731

    
3732
static int all_vcpus_paused(void)
3733
{
3734
    CPUState *penv = first_cpu;
3735

    
3736
    while (penv) {
3737
        if (!penv->stopped)
3738
            return 0;
3739
        penv = (CPUState *)penv->next_cpu;
3740
    }
3741

    
3742
    return 1;
3743
}
3744

    
3745
static void pause_all_vcpus(void)
3746
{
3747
    CPUState *penv = first_cpu;
3748

    
3749
    while (penv) {
3750
        penv->stop = 1;
3751
        qemu_thread_signal(penv->thread, SIG_IPI);
3752
        qemu_cpu_kick(penv);
3753
        penv = (CPUState *)penv->next_cpu;
3754
    }
3755

    
3756
    while (!all_vcpus_paused()) {
3757
        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3758
        penv = first_cpu;
3759
        while (penv) {
3760
            qemu_thread_signal(penv->thread, SIG_IPI);
3761
            penv = (CPUState *)penv->next_cpu;
3762
        }
3763
    }
3764
}
3765

    
3766
static void resume_all_vcpus(void)
3767
{
3768
    CPUState *penv = first_cpu;
3769

    
3770
    while (penv) {
3771
        penv->stop = 0;
3772
        penv->stopped = 0;
3773
        qemu_thread_signal(penv->thread, SIG_IPI);
3774
        qemu_cpu_kick(penv);
3775
        penv = (CPUState *)penv->next_cpu;
3776
    }
3777
}
3778

    
3779
static void tcg_init_vcpu(void *_env)
3780
{
3781
    CPUState *env = _env;
3782
    /* share a single thread for all cpus with TCG */
3783
    if (!tcg_cpu_thread) {
3784
        env->thread = qemu_mallocz(sizeof(QemuThread));
3785
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3786
        qemu_cond_init(env->halt_cond);
3787
        qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3788
        while (env->created == 0)
3789
            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3790
        tcg_cpu_thread = env->thread;
3791
        tcg_halt_cond = env->halt_cond;
3792
    } else {
3793
        env->thread = tcg_cpu_thread;
3794
        env->halt_cond = tcg_halt_cond;
3795
    }
3796
}
3797

    
3798
static void kvm_start_vcpu(CPUState *env)
3799
{
3800
    env->thread = qemu_mallocz(sizeof(QemuThread));
3801
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3802
    qemu_cond_init(env->halt_cond);
3803
    qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3804
    while (env->created == 0)
3805
        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3806
}
3807

    
3808
void qemu_init_vcpu(void *_env)
3809
{
3810
    CPUState *env = _env;
3811

    
3812
    env->nr_cores = smp_cores;
3813
    env->nr_threads = smp_threads;
3814
    if (kvm_enabled())
3815
        kvm_start_vcpu(env);
3816
    else
3817
        tcg_init_vcpu(env);
3818
}
3819

    
3820
void qemu_notify_event(void)
3821
{
3822
    qemu_event_increment();
3823
}
3824

    
3825
void vm_stop(int reason)
3826
{
3827
    QemuThread me;
3828
    qemu_thread_self(&me);
3829

    
3830
    if (!qemu_thread_equal(&me, &io_thread)) {
3831
        qemu_system_vmstop_request(reason);
3832
        /*
3833
         * FIXME: should not return to device code in case
3834
         * vm_stop() has been requested.
3835
         */
3836
        if (cpu_single_env) {
3837
            cpu_exit(cpu_single_env);
3838
            cpu_single_env->stop = 1;
3839
        }
3840
        return;
3841
    }
3842
    do_vm_stop(reason);
3843
}
3844

    
3845
#endif
3846

    
3847

    
3848
#ifdef _WIN32
3849
static void host_main_loop_wait(int *timeout)
3850
{
3851
    int ret, ret2, i;
3852
    PollingEntry *pe;
3853

    
3854

    
3855
    /* XXX: need to suppress polling by better using win32 events */
3856
    ret = 0;
3857
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3858
        ret |= pe->func(pe->opaque);
3859
    }
3860
    if (ret == 0) {
3861
        int err;
3862
        WaitObjects *w = &wait_objects;
3863

    
3864
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
3865
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3866
            if (w->func[ret - WAIT_OBJECT_0])
3867
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
3868

    
3869
            /* Check for additional signaled events */
3870
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
3871

    
3872
                /* Check if event is signaled */
3873
                ret2 = WaitForSingleObject(w->events[i], 0);
3874
                if(ret2 == WAIT_OBJECT_0) {
3875
                    if (w->func[i])
3876
                        w->func[i](w->opaque[i]);
3877
                } else if (ret2 == WAIT_TIMEOUT) {
3878
                } else {
3879
                    err = GetLastError();
3880
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
3881
                }
3882
            }
3883
        } else if (ret == WAIT_TIMEOUT) {
3884
        } else {
3885
            err = GetLastError();
3886
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
3887
        }
3888
    }
3889

    
3890
    *timeout = 0;
3891
}
3892
#else
3893
static void host_main_loop_wait(int *timeout)
3894
{
3895
}
3896
#endif
3897

    
3898
void main_loop_wait(int timeout)
3899
{
3900
    IOHandlerRecord *ioh;
3901
    fd_set rfds, wfds, xfds;
3902
    int ret, nfds;
3903
    struct timeval tv;
3904

    
3905
    qemu_bh_update_timeout(&timeout);
3906

    
3907
    host_main_loop_wait(&timeout);
3908

    
3909
    /* poll any events */
3910
    /* XXX: separate device handlers from system ones */
3911
    nfds = -1;
3912
    FD_ZERO(&rfds);
3913
    FD_ZERO(&wfds);
3914
    FD_ZERO(&xfds);
3915
    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3916
        if (ioh->deleted)
3917
            continue;
3918
        if (ioh->fd_read &&
3919
            (!ioh->fd_read_poll ||
3920
             ioh->fd_read_poll(ioh->opaque) != 0)) {
3921
            FD_SET(ioh->fd, &rfds);
3922
            if (ioh->fd > nfds)
3923
                nfds = ioh->fd;
3924
        }
3925
        if (ioh->fd_write) {
3926
            FD_SET(ioh->fd, &wfds);
3927
            if (ioh->fd > nfds)
3928
                nfds = ioh->fd;
3929
        }
3930
    }
3931

    
3932
    tv.tv_sec = timeout / 1000;
3933
    tv.tv_usec = (timeout % 1000) * 1000;
3934

    
3935
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3936

    
3937
    qemu_mutex_unlock_iothread();
3938
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3939
    qemu_mutex_lock_iothread();
3940
    if (ret > 0) {
3941
        IOHandlerRecord **pioh;
3942

    
3943
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3944
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
3945
                ioh->fd_read(ioh->opaque);
3946
            }
3947
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
3948
                ioh->fd_write(ioh->opaque);
3949
            }
3950
        }
3951

    
3952
        /* remove deleted IO handlers */
3953
        pioh = &first_io_handler;
3954
        while (*pioh) {
3955
            ioh = *pioh;
3956
            if (ioh->deleted) {
3957
                *pioh = ioh->next;
3958
                qemu_free(ioh);
3959
            } else
3960
                pioh = &ioh->next;
3961
        }
3962
    }
3963

    
3964
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
3965

    
3966
    qemu_run_all_timers();
3967

    
3968
    /* Check bottom-halves last in case any of the earlier events triggered
3969
       them.  */
3970
    qemu_bh_poll();
3971

    
3972
}
3973

    
3974
static int qemu_cpu_exec(CPUState *env)
3975
{
3976
    int ret;
3977
#ifdef CONFIG_PROFILER
3978
    int64_t ti;
3979
#endif
3980

    
3981
#ifdef CONFIG_PROFILER
3982
    ti = profile_getclock();
3983
#endif
3984
    if (use_icount) {
3985
        int64_t count;
3986
        int decr;
3987
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3988
        env->icount_decr.u16.low = 0;
3989
        env->icount_extra = 0;
3990
        count = qemu_icount_round (qemu_next_deadline());
3991
        qemu_icount += count;
3992
        decr = (count > 0xffff) ? 0xffff : count;
3993
        count -= decr;
3994
        env->icount_decr.u16.low = decr;
3995
        env->icount_extra = count;
3996
    }
3997
    ret = cpu_exec(env);
3998
#ifdef CONFIG_PROFILER
3999
    qemu_time += profile_getclock() - ti;
4000
#endif
4001
    if (use_icount) {
4002
        /* Fold pending instructions back into the
4003
           instruction counter, and clear the interrupt flag.  */
4004
        qemu_icount -= (env->icount_decr.u16.low
4005
                        + env->icount_extra);
4006
        env->icount_decr.u32 = 0;
4007
        env->icount_extra = 0;
4008
    }
4009
    return ret;
4010
}
4011

    
4012
static void tcg_cpu_exec(void)
4013
{
4014
    int ret = 0;
4015

    
4016
    if (next_cpu == NULL)
4017
        next_cpu = first_cpu;
4018
    for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4019
        CPUState *env = cur_cpu = next_cpu;
4020

    
4021
        qemu_clock_enable(vm_clock,
4022
                          (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
4023

    
4024
        if (qemu_alarm_pending())
4025
            break;
4026
        if (cpu_can_run(env))
4027
            ret = qemu_cpu_exec(env);
4028
        else if (env->stop)
4029
            break;
4030

    
4031
        if (ret == EXCP_DEBUG) {
4032
            gdb_set_stop_cpu(env);
4033
            debug_requested = 1;
4034
            break;
4035
        }
4036
    }
4037
}
4038

    
4039
static int cpu_has_work(CPUState *env)
4040
{
4041
    if (env->stop)
4042
        return 1;
4043
    if (env->stopped)
4044
        return 0;
4045
    if (!env->halted)
4046
        return 1;
4047
    if (qemu_cpu_has_work(env))
4048
        return 1;
4049
    return 0;
4050
}
4051

    
4052
static int tcg_has_work(void)
4053
{
4054
    CPUState *env;
4055

    
4056
    for (env = first_cpu; env != NULL; env = env->next_cpu)
4057
        if (cpu_has_work(env))
4058
            return 1;
4059
    return 0;
4060
}
4061

    
4062
static int qemu_calculate_timeout(void)
4063
{
4064
#ifndef CONFIG_IOTHREAD
4065
    int timeout;
4066

    
4067
    if (!vm_running)
4068
        timeout = 5000;
4069
    else if (tcg_has_work())
4070
        timeout = 0;
4071
    else {
4072
     /* XXX: use timeout computed from timers */
4073
        int64_t add;
4074
        int64_t delta;
4075
        /* Advance virtual time to the next event.  */
4076
        delta = qemu_icount_delta();
4077
        if (delta > 0) {
4078
            /* If virtual time is ahead of real time then just
4079
               wait for IO.  */
4080
            timeout = (delta + 999999) / 1000000;
4081
        } else {
4082
            /* Wait for either IO to occur or the next
4083
               timer event.  */
4084
            add = qemu_next_deadline();
4085
            /* We advance the timer before checking for IO.
4086
               Limit the amount we advance so that early IO
4087
               activity won't get the guest too far ahead.  */
4088
            if (add > 10000000)
4089
                add = 10000000;
4090
            delta += add;
4091
            qemu_icount += qemu_icount_round (add);
4092
            timeout = delta / 1000000;
4093
            if (timeout < 0)
4094
                timeout = 0;
4095
        }
4096
    }
4097

    
4098
    return timeout;
4099
#else /* CONFIG_IOTHREAD */
4100
    return 1000;
4101
#endif
4102
}
4103

    
4104
static int vm_can_run(void)
4105
{
4106
    if (powerdown_requested)
4107
        return 0;
4108
    if (reset_requested)
4109
        return 0;
4110
    if (shutdown_requested)
4111
        return 0;
4112
    if (debug_requested)
4113
        return 0;
4114
    return 1;
4115
}
4116

    
4117
qemu_irq qemu_system_powerdown;
4118

    
4119
static void main_loop(void)
4120
{
4121
    int r;
4122

    
4123
#ifdef CONFIG_IOTHREAD
4124
    qemu_system_ready = 1;
4125
    qemu_cond_broadcast(&qemu_system_cond);
4126
#endif
4127

    
4128
    for (;;) {
4129
        do {
4130
#ifdef CONFIG_PROFILER
4131
            int64_t ti;
4132
#endif
4133
#ifndef CONFIG_IOTHREAD
4134
            tcg_cpu_exec();
4135
#endif
4136
#ifdef CONFIG_PROFILER
4137
            ti = profile_getclock();
4138
#endif
4139
            main_loop_wait(qemu_calculate_timeout());
4140
#ifdef CONFIG_PROFILER
4141
            dev_time += profile_getclock() - ti;
4142
#endif
4143
        } while (vm_can_run());
4144

    
4145
        if (qemu_debug_requested()) {
4146
            vm_stop(EXCP_DEBUG);
4147
        }
4148
        if (qemu_shutdown_requested()) {
4149
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
4150
            if (no_shutdown) {
4151
                vm_stop(0);
4152
                no_shutdown = 0;
4153
            } else
4154
                break;
4155
        }
4156
        if (qemu_reset_requested()) {
4157
            pause_all_vcpus();
4158
            qemu_system_reset();
4159
            resume_all_vcpus();
4160
        }
4161
        if (qemu_powerdown_requested()) {
4162
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
4163
            qemu_irq_raise(qemu_system_powerdown);
4164
        }
4165
        if ((r = qemu_vmstop_requested())) {
4166
            vm_stop(r);
4167
        }
4168
    }
4169
    pause_all_vcpus();
4170
}
4171

    
4172
static void version(void)
4173
{
4174
    printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
4175
}
4176

    
4177
static void help(int exitcode)
4178
{
4179
    const char *options_help =
4180
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4181
           opt_help
4182
#define DEFHEADING(text) stringify(text) "\n"
4183
#include "qemu-options.h"
4184
#undef DEF
4185
#undef DEFHEADING
4186
#undef GEN_DOCS
4187
        ;
4188
    version();
4189
    printf("usage: %s [options] [disk_image]\n"
4190
           "\n"
4191
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4192
           "\n"
4193
           "%s\n"
4194
           "During emulation, the following keys are useful:\n"
4195
           "ctrl-alt-f      toggle full screen\n"
4196
           "ctrl-alt-n      switch to virtual console 'n'\n"
4197
           "ctrl-alt        toggle mouse and keyboard grab\n"
4198
           "\n"
4199
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
4200
           "qemu",
4201
           options_help);
4202
    exit(exitcode);
4203
}
4204

    
4205
#define HAS_ARG 0x0001
4206

    
4207
enum {
4208
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4209
    opt_enum,
4210
#define DEFHEADING(text)
4211
#include "qemu-options.h"
4212
#undef DEF
4213
#undef DEFHEADING
4214
#undef GEN_DOCS
4215
};
4216

    
4217
typedef struct QEMUOption {
4218
    const char *name;
4219
    int flags;
4220
    int index;
4221
} QEMUOption;
4222

    
4223
static const QEMUOption qemu_options[] = {
4224
    { "h", 0, QEMU_OPTION_h },
4225
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4226
    { option, opt_arg, opt_enum },
4227
#define DEFHEADING(text)
4228
#include "qemu-options.h"
4229
#undef DEF
4230
#undef DEFHEADING
4231
#undef GEN_DOCS
4232
    { NULL },
4233
};
4234

    
4235
#ifdef HAS_AUDIO
4236
struct soundhw soundhw[] = {
4237
#ifdef HAS_AUDIO_CHOICE
4238
#if defined(TARGET_I386) || defined(TARGET_MIPS)
4239
    {
4240
        "pcspk",
4241
        "PC speaker",
4242
        0,
4243
        1,
4244
        { .init_isa = pcspk_audio_init }
4245
    },
4246
#endif
4247

    
4248
#ifdef CONFIG_SB16
4249
    {
4250
        "sb16",
4251
        "Creative Sound Blaster 16",
4252
        0,
4253
        1,
4254
        { .init_isa = SB16_init }
4255
    },
4256
#endif
4257

    
4258
#ifdef CONFIG_CS4231A
4259
    {
4260
        "cs4231a",
4261
        "CS4231A",
4262
        0,
4263
        1,
4264
        { .init_isa = cs4231a_init }
4265
    },
4266
#endif
4267

    
4268
#ifdef CONFIG_ADLIB
4269
    {
4270
        "adlib",
4271
#ifdef HAS_YMF262
4272
        "Yamaha YMF262 (OPL3)",
4273
#else
4274
        "Yamaha YM3812 (OPL2)",
4275
#endif
4276
        0,
4277
        1,
4278
        { .init_isa = Adlib_init }
4279
    },
4280
#endif
4281

    
4282
#ifdef CONFIG_GUS
4283
    {
4284
        "gus",
4285
        "Gravis Ultrasound GF1",
4286
        0,
4287
        1,
4288
        { .init_isa = GUS_init }
4289
    },
4290
#endif
4291

    
4292
#ifdef CONFIG_AC97
4293
    {
4294
        "ac97",
4295
        "Intel 82801AA AC97 Audio",
4296
        0,
4297
        0,
4298
        { .init_pci = ac97_init }
4299
    },
4300
#endif
4301

    
4302
#ifdef CONFIG_ES1370
4303
    {
4304
        "es1370",
4305
        "ENSONIQ AudioPCI ES1370",
4306
        0,
4307
        0,
4308
        { .init_pci = es1370_init }
4309
    },
4310
#endif
4311

    
4312
#endif /* HAS_AUDIO_CHOICE */
4313

    
4314
    { NULL, NULL, 0, 0, { NULL } }
4315
};
4316

    
4317
static void select_soundhw (const char *optarg)
4318
{
4319
    struct soundhw *c;
4320

    
4321
    if (*optarg == '?') {
4322
    show_valid_cards:
4323

    
4324
        printf ("Valid sound card names (comma separated):\n");
4325
        for (c = soundhw; c->name; ++c) {
4326
            printf ("%-11s %s\n", c->name, c->descr);
4327
        }
4328
        printf ("\n-soundhw all will enable all of the above\n");
4329
        exit (*optarg != '?');
4330
    }
4331
    else {
4332
        size_t l;
4333
        const char *p;
4334
        char *e;
4335
        int bad_card = 0;
4336

    
4337
        if (!strcmp (optarg, "all")) {
4338
            for (c = soundhw; c->name; ++c) {
4339
                c->enabled = 1;
4340
            }
4341
            return;
4342
        }
4343

    
4344
        p = optarg;
4345
        while (*p) {
4346
            e = strchr (p, ',');
4347
            l = !e ? strlen (p) : (size_t) (e - p);
4348

    
4349
            for (c = soundhw; c->name; ++c) {
4350
                if (!strncmp (c->name, p, l) && !c->name[l]) {
4351
                    c->enabled = 1;
4352
                    break;
4353
                }
4354
            }
4355

    
4356
            if (!c->name) {
4357
                if (l > 80) {
4358
                    fprintf (stderr,
4359
                             "Unknown sound card name (too big to show)\n");
4360
                }
4361
                else {
4362
                    fprintf (stderr, "Unknown sound card name `%.*s'\n",
4363
                             (int) l, p);
4364
                }
4365
                bad_card = 1;
4366
            }
4367
            p += l + (e != NULL);
4368
        }
4369

    
4370
        if (bad_card)
4371
            goto show_valid_cards;
4372
    }
4373
}
4374
#endif
4375

    
4376
static void select_vgahw (const char *p)
4377
{
4378
    const char *opts;
4379

    
4380
    default_vga = 0;
4381
    vga_interface_type = VGA_NONE;
4382
    if (strstart(p, "std", &opts)) {
4383
        vga_interface_type = VGA_STD;
4384
    } else if (strstart(p, "cirrus", &opts)) {
4385
        vga_interface_type = VGA_CIRRUS;
4386
    } else if (strstart(p, "vmware", &opts)) {
4387
        vga_interface_type = VGA_VMWARE;
4388
    } else if (strstart(p, "xenfb", &opts)) {
4389
        vga_interface_type = VGA_XENFB;
4390
    } else if (!strstart(p, "none", &opts)) {
4391
    invalid_vga:
4392
        fprintf(stderr, "Unknown vga type: %s\n", p);
4393
        exit(1);
4394
    }
4395
    while (*opts) {
4396
        const char *nextopt;
4397

    
4398
        if (strstart(opts, ",retrace=", &nextopt)) {
4399
            opts = nextopt;
4400
            if (strstart(opts, "dumb", &nextopt))
4401
                vga_retrace_method = VGA_RETRACE_DUMB;
4402
            else if (strstart(opts, "precise", &nextopt))
4403
                vga_retrace_method = VGA_RETRACE_PRECISE;
4404
            else goto invalid_vga;
4405
        } else goto invalid_vga;
4406
        opts = nextopt;
4407
    }
4408
}
4409

    
4410
#ifdef TARGET_I386
4411
static int balloon_parse(const char *arg)
4412
{
4413
    QemuOpts *opts;
4414

    
4415
    if (strcmp(arg, "none") == 0) {
4416
        return 0;
4417
    }
4418

    
4419
    if (!strncmp(arg, "virtio", 6)) {
4420
        if (arg[6] == ',') {
4421
            /* have params -> parse them */
4422
            opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
4423
            if (!opts)
4424
                return  -1;
4425
        } else {
4426
            /* create empty opts */
4427
            opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4428
        }
4429
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4430
        return 0;
4431
    }
4432

    
4433
    return -1;
4434
}
4435
#endif
4436

    
4437
#ifdef _WIN32
4438
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4439
{
4440
    exit(STATUS_CONTROL_C_EXIT);
4441
    return TRUE;
4442
}
4443
#endif
4444

    
4445
int qemu_uuid_parse(const char *str, uint8_t *uuid)
4446
{
4447
    int ret;
4448

    
4449
    if(strlen(str) != 36)
4450
        return -1;
4451

    
4452
    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4453
            &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4454
            &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4455

    
4456
    if(ret != 16)
4457
        return -1;
4458

    
4459
#ifdef TARGET_I386
4460
    smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4461
#endif
4462

    
4463
    return 0;
4464
}
4465

    
4466
#ifndef _WIN32
4467

    
4468
static void termsig_handler(int signal)
4469
{
4470
    qemu_system_shutdown_request();
4471
}
4472

    
4473
static void sigchld_handler(int signal)
4474
{
4475
    waitpid(-1, NULL, WNOHANG);
4476
}
4477

    
4478
static void sighandler_setup(void)
4479
{
4480
    struct sigaction act;
4481

    
4482
    memset(&act, 0, sizeof(act));
4483
    act.sa_handler = termsig_handler;
4484
    sigaction(SIGINT,  &act, NULL);
4485
    sigaction(SIGHUP,  &act, NULL);
4486
    sigaction(SIGTERM, &act, NULL);
4487

    
4488
    act.sa_handler = sigchld_handler;
4489
    act.sa_flags = SA_NOCLDSTOP;
4490
    sigaction(SIGCHLD, &act, NULL);
4491
}
4492

    
4493
#endif
4494

    
4495
#ifdef _WIN32
4496
/* Look for support files in the same directory as the executable.  */
4497
static char *find_datadir(const char *argv0)
4498
{
4499
    char *p;
4500
    char buf[MAX_PATH];
4501
    DWORD len;
4502

    
4503
    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4504
    if (len == 0) {
4505
        return NULL;
4506
    }
4507

    
4508
    buf[len] = 0;
4509
    p = buf + len - 1;
4510
    while (p != buf && *p != '\\')
4511
        p--;
4512
    *p = 0;
4513
    if (access(buf, R_OK) == 0) {
4514
        return qemu_strdup(buf);
4515
    }
4516
    return NULL;
4517
}
4518
#else /* !_WIN32 */
4519

    
4520
/* Find a likely location for support files using the location of the binary.
4521
   For installed binaries this will be "$bindir/../share/qemu".  When
4522
   running from the build tree this will be "$bindir/../pc-bios".  */
4523
#define SHARE_SUFFIX "/share/qemu"
4524
#define BUILD_SUFFIX "/pc-bios"
4525
static char *find_datadir(const char *argv0)
4526
{
4527
    char *dir;
4528
    char *p = NULL;
4529
    char *res;
4530
    char buf[PATH_MAX];
4531
    size_t max_len;
4532

    
4533
#if defined(__linux__)
4534
    {
4535
        int len;
4536
        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4537
        if (len > 0) {
4538
            buf[len] = 0;
4539
            p = buf;
4540
        }
4541
    }
4542
#elif defined(__FreeBSD__)
4543
    {
4544
        int len;
4545
        len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4546
        if (len > 0) {
4547
            buf[len] = 0;
4548
            p = buf;
4549
        }
4550
    }
4551
#endif
4552
    /* If we don't have any way of figuring out the actual executable
4553
       location then try argv[0].  */
4554
    if (!p) {
4555
        p = realpath(argv0, buf);
4556
        if (!p) {
4557
            return NULL;
4558
        }
4559
    }
4560
    dir = dirname(p);
4561
    dir = dirname(dir);
4562

    
4563
    max_len = strlen(dir) +
4564
        MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4565
    res = qemu_mallocz(max_len);
4566
    snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
4567
    if (access(res, R_OK)) {
4568
        snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
4569
        if (access(res, R_OK)) {
4570
            qemu_free(res);
4571
            res = NULL;
4572
        }
4573
    }
4574

    
4575
    return res;
4576
}
4577
#undef SHARE_SUFFIX
4578
#undef BUILD_SUFFIX
4579
#endif
4580

    
4581
char *qemu_find_file(int type, const char *name)
4582
{
4583
    int len;
4584
    const char *subdir;
4585
    char *buf;
4586

    
4587
    /* If name contains path separators then try it as a straight path.  */
4588
    if ((strchr(name, '/') || strchr(name, '\\'))
4589
        && access(name, R_OK) == 0) {
4590
        return qemu_strdup(name);
4591
    }
4592
    switch (type) {
4593
    case QEMU_FILE_TYPE_BIOS:
4594
        subdir = "";
4595
        break;
4596
    case QEMU_FILE_TYPE_KEYMAP:
4597
        subdir = "keymaps/";
4598
        break;
4599
    default:
4600
        abort();
4601
    }
4602
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4603
    buf = qemu_mallocz(len);
4604
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
4605
    if (access(buf, R_OK)) {
4606
        qemu_free(buf);
4607
        return NULL;
4608
    }
4609
    return buf;
4610
}
4611

    
4612
static int device_help_func(QemuOpts *opts, void *opaque)
4613
{
4614
    return qdev_device_help(opts);
4615
}
4616

    
4617
static int device_init_func(QemuOpts *opts, void *opaque)
4618
{
4619
    DeviceState *dev;
4620

    
4621
    dev = qdev_device_add(opts);
4622
    if (!dev)
4623
        return -1;
4624
    return 0;
4625
}
4626

    
4627
static int chardev_init_func(QemuOpts *opts, void *opaque)
4628
{
4629
    CharDriverState *chr;
4630

    
4631
    chr = qemu_chr_open_opts(opts, NULL);
4632
    if (!chr)
4633
        return -1;
4634
    return 0;
4635
}
4636

    
4637
static int mon_init_func(QemuOpts *opts, void *opaque)
4638
{
4639
    CharDriverState *chr;
4640
    const char *chardev;
4641
    const char *mode;
4642
    int flags;
4643

    
4644
    mode = qemu_opt_get(opts, "mode");
4645
    if (mode == NULL) {
4646
        mode = "readline";
4647
    }
4648
    if (strcmp(mode, "readline") == 0) {
4649
        flags = MONITOR_USE_READLINE;
4650
    } else if (strcmp(mode, "control") == 0) {
4651
        flags = MONITOR_USE_CONTROL;
4652
    } else {
4653
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
4654
        exit(1);
4655
    }
4656

    
4657
    if (qemu_opt_get_bool(opts, "default", 0))
4658
        flags |= MONITOR_IS_DEFAULT;
4659

    
4660
    chardev = qemu_opt_get(opts, "chardev");
4661
    chr = qemu_chr_find(chardev);
4662
    if (chr == NULL) {
4663
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
4664
        exit(1);
4665
    }
4666

    
4667
    monitor_init(chr, flags);
4668
    return 0;
4669
}
4670

    
4671
static void monitor_parse(const char *optarg, const char *mode)
4672
{
4673
    static int monitor_device_index = 0;
4674
    QemuOpts *opts;
4675
    const char *p;
4676
    char label[32];
4677
    int def = 0;
4678

    
4679
    if (strstart(optarg, "chardev:", &p)) {
4680
        snprintf(label, sizeof(label), "%s", p);
4681
    } else {
4682
        if (monitor_device_index) {
4683
            snprintf(label, sizeof(label), "monitor%d",
4684
                     monitor_device_index);
4685
        } else {
4686
            snprintf(label, sizeof(label), "monitor");
4687
            def = 1;
4688
        }
4689
        opts = qemu_chr_parse_compat(label, optarg);
4690
        if (!opts) {
4691
            fprintf(stderr, "parse error: %s\n", optarg);
4692
            exit(1);
4693
        }
4694
    }
4695

    
4696
    opts = qemu_opts_create(&qemu_mon_opts, label, 1);
4697
    if (!opts) {
4698
        fprintf(stderr, "duplicate chardev: %s\n", label);
4699
        exit(1);
4700
    }
4701
    qemu_opt_set(opts, "mode", mode);
4702
    qemu_opt_set(opts, "chardev", label);
4703
    if (def)
4704
        qemu_opt_set(opts, "default", "on");
4705
    monitor_device_index++;
4706
}
4707

    
4708
struct device_config {
4709
    enum {
4710
        DEV_USB,       /* -usbdevice     */
4711
        DEV_BT,        /* -bt            */
4712
        DEV_SERIAL,    /* -serial        */
4713
        DEV_PARALLEL,  /* -parallel      */
4714
        DEV_VIRTCON,   /* -virtioconsole */
4715
        DEV_DEBUGCON,  /* -debugcon */
4716
    } type;
4717
    const char *cmdline;
4718
    QTAILQ_ENTRY(device_config) next;
4719
};
4720
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
4721

    
4722
static void add_device_config(int type, const char *cmdline)
4723
{
4724
    struct device_config *conf;
4725

    
4726
    conf = qemu_mallocz(sizeof(*conf));
4727
    conf->type = type;
4728
    conf->cmdline = cmdline;
4729
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
4730
}
4731

    
4732
static int foreach_device_config(int type, int (*func)(const char *cmdline))
4733
{
4734
    struct device_config *conf;
4735
    int rc;
4736

    
4737
    QTAILQ_FOREACH(conf, &device_configs, next) {
4738
        if (conf->type != type)
4739
            continue;
4740
        rc = func(conf->cmdline);
4741
        if (0 != rc)
4742
            return rc;
4743
    }
4744
    return 0;
4745
}
4746

    
4747
static int serial_parse(const char *devname)
4748
{
4749
    static int index = 0;
4750
    char label[32];
4751

    
4752
    if (strcmp(devname, "none") == 0)
4753
        return 0;
4754
    if (index == MAX_SERIAL_PORTS) {
4755
        fprintf(stderr, "qemu: too many serial ports\n");
4756
        exit(1);
4757
    }
4758
    snprintf(label, sizeof(label), "serial%d", index);
4759
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
4760
    if (!serial_hds[index]) {
4761
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
4762
                devname, strerror(errno));
4763
        return -1;
4764
    }
4765
    index++;
4766
    return 0;
4767
}
4768

    
4769
static int parallel_parse(const char *devname)
4770
{
4771
    static int index = 0;
4772
    char label[32];
4773

    
4774
    if (strcmp(devname, "none") == 0)
4775
        return 0;
4776
    if (index == MAX_PARALLEL_PORTS) {
4777
        fprintf(stderr, "qemu: too many parallel ports\n");
4778
        exit(1);
4779
    }
4780
    snprintf(label, sizeof(label), "parallel%d", index);
4781
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
4782
    if (!parallel_hds[index]) {
4783
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
4784
                devname, strerror(errno));
4785
        return -1;
4786
    }
4787
    index++;
4788
    return 0;
4789
}
4790

    
4791
static int virtcon_parse(const char *devname)
4792
{
4793
    static int index = 0;
4794
    char label[32];
4795
    QemuOpts *bus_opts, *dev_opts;
4796

    
4797
    if (strcmp(devname, "none") == 0)
4798
        return 0;
4799
    if (index == MAX_VIRTIO_CONSOLES) {
4800
        fprintf(stderr, "qemu: too many virtio consoles\n");
4801
        exit(1);
4802
    }
4803

    
4804
    bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4805
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
4806

    
4807
    dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4808
    qemu_opt_set(dev_opts, "driver", "virtconsole");
4809

    
4810
    snprintf(label, sizeof(label), "virtcon%d", index);
4811
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
4812
    if (!virtcon_hds[index]) {
4813
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
4814
                devname, strerror(errno));
4815
        return -1;
4816
    }
4817
    qemu_opt_set(dev_opts, "chardev", label);
4818

    
4819
    index++;
4820
    return 0;
4821
}
4822

    
4823
static int debugcon_parse(const char *devname)
4824
{   
4825
    QemuOpts *opts;
4826

    
4827
    if (!qemu_chr_open("debugcon", devname, NULL)) {
4828
        exit(1);
4829
    }
4830
    opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
4831
    if (!opts) {
4832
        fprintf(stderr, "qemu: already have a debugcon device\n");
4833
        exit(1);
4834
    }
4835
    qemu_opt_set(opts, "driver", "isa-debugcon");
4836
    qemu_opt_set(opts, "chardev", "debugcon");
4837
    return 0;
4838
}
4839

    
4840
static const QEMUOption *lookup_opt(int argc, char **argv,
4841
                                    const char **poptarg, int *poptind)
4842
{
4843
    const QEMUOption *popt;
4844
    int optind = *poptind;
4845
    char *r = argv[optind];
4846
    const char *optarg;
4847

    
4848
    loc_set_cmdline(argv, optind, 1);
4849
    optind++;
4850
    /* Treat --foo the same as -foo.  */
4851
    if (r[1] == '-')
4852
        r++;
4853
    popt = qemu_options;
4854
    for(;;) {
4855
        if (!popt->name) {
4856
            error_report("invalid option");
4857
            exit(1);
4858
        }
4859
        if (!strcmp(popt->name, r + 1))
4860
            break;
4861
        popt++;
4862
    }
4863
    if (popt->flags & HAS_ARG) {
4864
        if (optind >= argc) {
4865
            error_report("requires an argument");
4866
            exit(1);
4867
        }
4868
        optarg = argv[optind++];
4869
        loc_set_cmdline(argv, optind - 2, 2);
4870
    } else {
4871
        optarg = NULL;
4872
    }
4873

    
4874
    *poptarg = optarg;
4875
    *poptind = optind;
4876

    
4877
    return popt;
4878
}
4879

    
4880
int main(int argc, char **argv, char **envp)
4881
{
4882
    const char *gdbstub_dev = NULL;
4883
    uint32_t boot_devices_bitmap = 0;
4884
    int i;
4885
    int snapshot, linux_boot, net_boot;
4886
    const char *icount_option = NULL;
4887
    const char *initrd_filename;
4888
    const char *kernel_filename, *kernel_cmdline;
4889
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
4890
    DisplayState *ds;
4891
    DisplayChangeListener *dcl;
4892
    int cyls, heads, secs, translation;
4893
    QemuOpts *hda_opts = NULL, *opts;
4894
    int optind;
4895
    const char *optarg;
4896
    const char *loadvm = NULL;
4897
    QEMUMachine *machine;
4898
    const char *cpu_model;
4899
#ifndef _WIN32
4900
    int fds[2];
4901
#endif
4902
    int tb_size;
4903
    const char *pid_file = NULL;
4904
    const char *incoming = NULL;
4905
#ifndef _WIN32
4906
    int fd = 0;
4907
    struct passwd *pwd = NULL;
4908
    const char *chroot_dir = NULL;
4909
    const char *run_as = NULL;
4910
#endif
4911
    CPUState *env;
4912
    int show_vnc_port = 0;
4913
    int defconfig = 1;
4914

    
4915
    error_set_progname(argv[0]);
4916

    
4917
    init_clocks();
4918

    
4919
    qemu_cache_utils_init(envp);
4920

    
4921
    QLIST_INIT (&vm_change_state_head);
4922
#ifndef _WIN32
4923
    {
4924
        struct sigaction act;
4925
        sigfillset(&act.sa_mask);
4926
        act.sa_flags = 0;
4927
        act.sa_handler = SIG_IGN;
4928
        sigaction(SIGPIPE, &act, NULL);
4929
    }
4930
#else
4931
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
4932
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
4933
       QEMU to run on a single CPU */
4934
    {
4935
        HANDLE h;
4936
        DWORD mask, smask;
4937
        int i;
4938
        h = GetCurrentProcess();
4939
        if (GetProcessAffinityMask(h, &mask, &smask)) {
4940
            for(i = 0; i < 32; i++) {
4941
                if (mask & (1 << i))
4942
                    break;
4943
            }
4944
            if (i != 32) {
4945
                mask = 1 << i;
4946
                SetProcessAffinityMask(h, mask);
4947
            }
4948
        }
4949
    }
4950
#endif
4951

    
4952
    module_call_init(MODULE_INIT_MACHINE);
4953
    machine = find_default_machine();
4954
    cpu_model = NULL;
4955
    initrd_filename = NULL;
4956
    ram_size = 0;
4957
    snapshot = 0;
4958
    kernel_filename = NULL;
4959
    kernel_cmdline = "";
4960
    cyls = heads = secs = 0;
4961
    translation = BIOS_ATA_TRANSLATION_AUTO;
4962

    
4963
    for (i = 0; i < MAX_NODES; i++) {
4964
        node_mem[i] = 0;
4965
        node_cpumask[i] = 0;
4966
    }
4967

    
4968
    nb_numa_nodes = 0;
4969
    nb_nics = 0;
4970

    
4971
    tb_size = 0;
4972
    autostart= 1;
4973

    
4974
    /* first pass of option parsing */
4975
    optind = 1;
4976
    while (optind < argc) {
4977
        if (argv[optind][0] != '-') {
4978
            /* disk image */
4979
            optind++;
4980
            continue;
4981
        } else {
4982
            const QEMUOption *popt;
4983

    
4984
            popt = lookup_opt(argc, argv, &optarg, &optind);
4985
            switch (popt->index) {
4986
            case QEMU_OPTION_nodefconfig:
4987
                defconfig=0;
4988
                break;
4989
            }
4990
        }
4991
    }
4992

    
4993
    if (defconfig) {
4994
        const char *fname;
4995
        FILE *fp;
4996

    
4997
        fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
4998
        fp = fopen(fname, "r");
4999
        if (fp) {
5000
            if (qemu_config_parse(fp, fname) != 0) {
5001
                exit(1);
5002
            }
5003
            fclose(fp);
5004
        }
5005

    
5006
        fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
5007
        fp = fopen(fname, "r");
5008
        if (fp) {
5009
            if (qemu_config_parse(fp, fname) != 0) {
5010
                exit(1);
5011
            }
5012
            fclose(fp);
5013
        }
5014
    }
5015
#if defined(cpudef_setup)
5016
    cpudef_setup(); /* parse cpu definitions in target config file */
5017
#endif
5018

    
5019
    /* second pass of option parsing */
5020
    optind = 1;
5021
    for(;;) {
5022
        if (optind >= argc)
5023
            break;
5024
        if (argv[optind][0] != '-') {
5025
            hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
5026
        } else {
5027
            const QEMUOption *popt;
5028

    
5029
            popt = lookup_opt(argc, argv, &optarg, &optind);
5030
            switch(popt->index) {
5031
            case QEMU_OPTION_M:
5032
                machine = find_machine(optarg);
5033
                if (!machine) {
5034
                    QEMUMachine *m;
5035
                    printf("Supported machines are:\n");
5036
                    for(m = first_machine; m != NULL; m = m->next) {
5037
                        if (m->alias)
5038
                            printf("%-10s %s (alias of %s)\n",
5039
                                   m->alias, m->desc, m->name);
5040
                        printf("%-10s %s%s\n",
5041
                               m->name, m->desc,
5042
                               m->is_default ? " (default)" : "");
5043
                    }
5044
                    exit(*optarg != '?');
5045
                }
5046
                break;
5047
            case QEMU_OPTION_cpu:
5048
                /* hw initialization will check this */
5049
                if (*optarg == '?') {
5050
/* XXX: implement xxx_cpu_list for targets that still miss it */
5051
#if defined(cpu_list_id)
5052
                    cpu_list_id(stdout, &fprintf, optarg);
5053
#elif defined(cpu_list)
5054
                    cpu_list(stdout, &fprintf);                /* deprecated */
5055
#endif
5056
                    exit(0);
5057
                } else {
5058
                    cpu_model = optarg;
5059
                }
5060
                break;
5061
            case QEMU_OPTION_initrd:
5062
                initrd_filename = optarg;
5063
                break;
5064
            case QEMU_OPTION_hda:
5065
                if (cyls == 0)
5066
                    hda_opts = drive_add(optarg, HD_ALIAS, 0);
5067
                else
5068
                    hda_opts = drive_add(optarg, HD_ALIAS
5069
                             ",cyls=%d,heads=%d,secs=%d%s",
5070
                             0, cyls, heads, secs,
5071
                             translation == BIOS_ATA_TRANSLATION_LBA ?
5072
                                 ",trans=lba" :
5073
                             translation == BIOS_ATA_TRANSLATION_NONE ?
5074
                                 ",trans=none" : "");
5075
                 break;
5076
            case QEMU_OPTION_hdb:
5077
            case QEMU_OPTION_hdc:
5078
            case QEMU_OPTION_hdd:
5079
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
5080
                break;
5081
            case QEMU_OPTION_drive:
5082
                drive_add(NULL, "%s", optarg);
5083
                break;
5084
            case QEMU_OPTION_set:
5085
                if (qemu_set_option(optarg) != 0)
5086
                    exit(1);
5087
                break;
5088
            case QEMU_OPTION_global:
5089
                if (qemu_global_option(optarg) != 0)
5090
                    exit(1);
5091
                break;
5092
            case QEMU_OPTION_mtdblock:
5093
                drive_add(optarg, MTD_ALIAS);
5094
                break;
5095
            case QEMU_OPTION_sd:
5096
                drive_add(optarg, SD_ALIAS);
5097
                break;
5098
            case QEMU_OPTION_pflash:
5099
                drive_add(optarg, PFLASH_ALIAS);
5100
                break;
5101
            case QEMU_OPTION_snapshot:
5102
                snapshot = 1;
5103
                break;
5104
            case QEMU_OPTION_hdachs:
5105
                {
5106
                    const char *p;
5107
                    p = optarg;
5108
                    cyls = strtol(p, (char **)&p, 0);
5109
                    if (cyls < 1 || cyls > 16383)
5110
                        goto chs_fail;
5111
                    if (*p != ',')
5112
                        goto chs_fail;
5113
                    p++;
5114
                    heads = strtol(p, (char **)&p, 0);
5115
                    if (heads < 1 || heads > 16)
5116
                        goto chs_fail;
5117
                    if (*p != ',')
5118
                        goto chs_fail;
5119
                    p++;
5120
                    secs = strtol(p, (char **)&p, 0);
5121
                    if (secs < 1 || secs > 63)
5122
                        goto chs_fail;
5123
                    if (*p == ',') {
5124
                        p++;
5125
                        if (!strcmp(p, "none"))
5126
                            translation = BIOS_ATA_TRANSLATION_NONE;
5127
                        else if (!strcmp(p, "lba"))
5128
                            translation = BIOS_ATA_TRANSLATION_LBA;
5129
                        else if (!strcmp(p, "auto"))
5130
                            translation = BIOS_ATA_TRANSLATION_AUTO;
5131
                        else
5132
                            goto chs_fail;
5133
                    } else if (*p != '\0') {
5134
                    chs_fail:
5135
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
5136
                        exit(1);
5137
                    }
5138
                    if (hda_opts != NULL) {
5139
                        char num[16];
5140
                        snprintf(num, sizeof(num), "%d", cyls);
5141
                        qemu_opt_set(hda_opts, "cyls", num);
5142
                        snprintf(num, sizeof(num), "%d", heads);
5143
                        qemu_opt_set(hda_opts, "heads", num);
5144
                        snprintf(num, sizeof(num), "%d", secs);
5145
                        qemu_opt_set(hda_opts, "secs", num);
5146
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
5147
                            qemu_opt_set(hda_opts, "trans", "lba");
5148
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
5149
                            qemu_opt_set(hda_opts, "trans", "none");
5150
                    }
5151
                }
5152
                break;
5153
            case QEMU_OPTION_numa:
5154
                if (nb_numa_nodes >= MAX_NODES) {
5155
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
5156
                    exit(1);
5157
                }
5158
                numa_add(optarg);
5159
                break;
5160
            case QEMU_OPTION_nographic:
5161
                display_type = DT_NOGRAPHIC;
5162
                break;
5163
#ifdef CONFIG_CURSES
5164
            case QEMU_OPTION_curses:
5165
                display_type = DT_CURSES;
5166
                break;
5167
#endif
5168
            case QEMU_OPTION_portrait:
5169
                graphic_rotate = 1;
5170
                break;
5171
            case QEMU_OPTION_kernel:
5172
                kernel_filename = optarg;
5173
                break;
5174
            case QEMU_OPTION_append:
5175
                kernel_cmdline = optarg;
5176
                break;
5177
            case QEMU_OPTION_cdrom:
5178
                drive_add(optarg, CDROM_ALIAS);
5179
                break;
5180
            case QEMU_OPTION_boot:
5181
                {
5182
                    static const char * const params[] = {
5183
                        "order", "once", "menu", NULL
5184
                    };
5185
                    char buf[sizeof(boot_devices)];
5186
                    char *standard_boot_devices;
5187
                    int legacy = 0;
5188

    
5189
                    if (!strchr(optarg, '=')) {
5190
                        legacy = 1;
5191
                        pstrcpy(buf, sizeof(buf), optarg);
5192
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5193
                        fprintf(stderr,
5194
                                "qemu: unknown boot parameter '%s' in '%s'\n",
5195
                                buf, optarg);
5196
                        exit(1);
5197
                    }
5198

    
5199
                    if (legacy ||
5200
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
5201
                        boot_devices_bitmap = parse_bootdevices(buf);
5202
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
5203
                    }
5204
                    if (!legacy) {
5205
                        if (get_param_value(buf, sizeof(buf),
5206
                                            "once", optarg)) {
5207
                            boot_devices_bitmap |= parse_bootdevices(buf);
5208
                            standard_boot_devices = qemu_strdup(boot_devices);
5209
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
5210
                            qemu_register_reset(restore_boot_devices,
5211
                                                standard_boot_devices);
5212
                        }
5213
                        if (get_param_value(buf, sizeof(buf),
5214
                                            "menu", optarg)) {
5215
                            if (!strcmp(buf, "on")) {
5216
                                boot_menu = 1;
5217
                            } else if (!strcmp(buf, "off")) {
5218
                                boot_menu = 0;
5219
                            } else {
5220
                                fprintf(stderr,
5221
                                        "qemu: invalid option value '%s'\n",
5222
                                        buf);
5223
                                exit(1);
5224
                            }
5225
                        }
5226
                    }
5227
                }
5228
                break;
5229
            case QEMU_OPTION_fda:
5230
            case QEMU_OPTION_fdb:
5231
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
5232
                break;
5233
#ifdef TARGET_I386
5234
            case QEMU_OPTION_no_fd_bootchk:
5235
                fd_bootchk = 0;
5236
                break;
5237
#endif
5238
            case QEMU_OPTION_netdev:
5239
                if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
5240
                    exit(1);
5241
                }
5242
                break;
5243
            case QEMU_OPTION_net:
5244
                if (net_client_parse(&qemu_net_opts, optarg) == -1) {
5245
                    exit(1);
5246
                }
5247
                break;
5248
#ifdef CONFIG_SLIRP
5249
            case QEMU_OPTION_tftp:
5250
                legacy_tftp_prefix = optarg;
5251
                break;
5252
            case QEMU_OPTION_bootp:
5253
                legacy_bootp_filename = optarg;
5254
                break;
5255
#ifndef _WIN32
5256
            case QEMU_OPTION_smb:
5257
                if (net_slirp_smb(optarg) < 0)
5258
                    exit(1);
5259
                break;
5260
#endif
5261
            case QEMU_OPTION_redir:
5262
                if (net_slirp_redir(optarg) < 0)
5263
                    exit(1);
5264
                break;
5265
#endif
5266
            case QEMU_OPTION_bt:
5267
                add_device_config(DEV_BT, optarg);
5268
                break;
5269
#ifdef HAS_AUDIO
5270
            case QEMU_OPTION_audio_help:
5271
                AUD_help ();
5272
                exit (0);
5273
                break;
5274
            case QEMU_OPTION_soundhw:
5275
                select_soundhw (optarg);
5276
                break;
5277
#endif
5278
            case QEMU_OPTION_h:
5279
                help(0);
5280
                break;
5281
            case QEMU_OPTION_version:
5282
                version();
5283
                exit(0);
5284
                break;
5285
            case QEMU_OPTION_m: {
5286
                uint64_t value;
5287
                char *ptr;
5288

    
5289
                value = strtoul(optarg, &ptr, 10);
5290
                switch (*ptr) {
5291
                case 0: case 'M': case 'm':
5292
                    value <<= 20;
5293
                    break;
5294
                case 'G': case 'g':
5295
                    value <<= 30;
5296
                    break;
5297
                default:
5298
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
5299
                    exit(1);
5300
                }
5301

    
5302
                /* On 32-bit hosts, QEMU is limited by virtual address space */
5303
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
5304
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5305
                    exit(1);
5306
                }
5307
                if (value != (uint64_t)(ram_addr_t)value) {
5308
                    fprintf(stderr, "qemu: ram size too large\n");
5309
                    exit(1);
5310
                }
5311
                ram_size = value;
5312
                break;
5313
            }
5314
            case QEMU_OPTION_mempath:
5315
                mem_path = optarg;
5316
                break;
5317
#ifdef MAP_POPULATE
5318
            case QEMU_OPTION_mem_prealloc:
5319
                mem_prealloc = 1;
5320
                break;
5321
#endif
5322
            case QEMU_OPTION_d:
5323
                {
5324
                    int mask;
5325
                    const CPULogItem *item;
5326

    
5327
                    mask = cpu_str_to_log_mask(optarg);
5328
                    if (!mask) {
5329
                        printf("Log items (comma separated):\n");
5330
                    for(item = cpu_log_items; item->mask != 0; item++) {
5331
                        printf("%-10s %s\n", item->name, item->help);
5332
                    }
5333
                    exit(1);
5334
                    }
5335
                    cpu_set_log(mask);
5336
                }
5337
                break;
5338
            case QEMU_OPTION_s:
5339
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
5340
                break;
5341
            case QEMU_OPTION_gdb:
5342
                gdbstub_dev = optarg;
5343
                break;
5344
            case QEMU_OPTION_L:
5345
                data_dir = optarg;
5346
                break;
5347
            case QEMU_OPTION_bios:
5348
                bios_name = optarg;
5349
                break;
5350
            case QEMU_OPTION_singlestep:
5351
                singlestep = 1;
5352
                break;
5353
            case QEMU_OPTION_S:
5354
                autostart = 0;
5355
                break;
5356
            case QEMU_OPTION_k:
5357
                keyboard_layout = optarg;
5358
                break;
5359
            case QEMU_OPTION_localtime:
5360
                rtc_utc = 0;
5361
                break;
5362
            case QEMU_OPTION_vga:
5363
                select_vgahw (optarg);
5364
                break;
5365
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
5366
            case QEMU_OPTION_g:
5367
                {
5368
                    const char *p;
5369
                    int w, h, depth;
5370
                    p = optarg;
5371
                    w = strtol(p, (char **)&p, 10);
5372
                    if (w <= 0) {
5373
                    graphic_error:
5374
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
5375
                        exit(1);
5376
                    }
5377
                    if (*p != 'x')
5378
                        goto graphic_error;
5379
                    p++;
5380
                    h = strtol(p, (char **)&p, 10);
5381
                    if (h <= 0)
5382
                        goto graphic_error;
5383
                    if (*p == 'x') {
5384
                        p++;
5385
                        depth = strtol(p, (char **)&p, 10);
5386
                        if (depth != 8 && depth != 15 && depth != 16 &&
5387
                            depth != 24 && depth != 32)
5388
                            goto graphic_error;
5389
                    } else if (*p == '\0') {
5390
                        depth = graphic_depth;
5391
                    } else {
5392
                        goto graphic_error;
5393
                    }
5394

    
5395
                    graphic_width = w;
5396
                    graphic_height = h;
5397
                    graphic_depth = depth;
5398
                }
5399
                break;
5400
#endif
5401
            case QEMU_OPTION_echr:
5402
                {
5403
                    char *r;
5404
                    term_escape_char = strtol(optarg, &r, 0);
5405
                    if (r == optarg)
5406
                        printf("Bad argument to echr\n");
5407
                    break;
5408
                }
5409
            case QEMU_OPTION_monitor:
5410
                monitor_parse(optarg, "readline");
5411
                default_monitor = 0;
5412
                break;
5413
            case QEMU_OPTION_qmp:
5414
                monitor_parse(optarg, "control");
5415
                default_monitor = 0;
5416
                break;
5417
            case QEMU_OPTION_mon:
5418
                opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
5419
                if (!opts) {
5420
                    fprintf(stderr, "parse error: %s\n", optarg);
5421
                    exit(1);
5422
                }
5423
                default_monitor = 0;
5424
                break;
5425
            case QEMU_OPTION_chardev:
5426
                opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
5427
                if (!opts) {
5428
                    fprintf(stderr, "parse error: %s\n", optarg);
5429
                    exit(1);
5430
                }
5431
                break;
5432
            case QEMU_OPTION_serial:
5433
                add_device_config(DEV_SERIAL, optarg);
5434
                default_serial = 0;
5435
                if (strncmp(optarg, "mon:", 4) == 0) {
5436
                    default_monitor = 0;
5437
                }
5438
                break;
5439
            case QEMU_OPTION_watchdog:
5440
                if (watchdog) {
5441
                    fprintf(stderr,
5442
                            "qemu: only one watchdog option may be given\n");
5443
                    return 1;
5444
                }
5445
                watchdog = optarg;
5446
                break;
5447
            case QEMU_OPTION_watchdog_action:
5448
                if (select_watchdog_action(optarg) == -1) {
5449
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
5450
                    exit(1);
5451
                }
5452
                break;
5453
            case QEMU_OPTION_virtiocon:
5454
                add_device_config(DEV_VIRTCON, optarg);
5455
                default_virtcon = 0;
5456
                if (strncmp(optarg, "mon:", 4) == 0) {
5457
                    default_monitor = 0;
5458
                }
5459
                break;
5460
            case QEMU_OPTION_parallel:
5461
                add_device_config(DEV_PARALLEL, optarg);
5462
                default_parallel = 0;
5463
                if (strncmp(optarg, "mon:", 4) == 0) {
5464
                    default_monitor = 0;
5465
                }
5466
                break;
5467
            case QEMU_OPTION_debugcon:
5468
                add_device_config(DEV_DEBUGCON, optarg);
5469
                break;
5470
            case QEMU_OPTION_loadvm:
5471
                loadvm = optarg;
5472
                break;
5473
            case QEMU_OPTION_full_screen:
5474
                full_screen = 1;
5475
                break;
5476
#ifdef CONFIG_SDL
5477
            case QEMU_OPTION_no_frame:
5478
                no_frame = 1;
5479
                break;
5480
            case QEMU_OPTION_alt_grab:
5481
                alt_grab = 1;
5482
                break;
5483
            case QEMU_OPTION_ctrl_grab:
5484
                ctrl_grab = 1;
5485
                break;
5486
            case QEMU_OPTION_no_quit:
5487
                no_quit = 1;
5488
                break;
5489
            case QEMU_OPTION_sdl:
5490
                display_type = DT_SDL;
5491
                break;
5492
#endif
5493
            case QEMU_OPTION_pidfile:
5494
                pid_file = optarg;
5495
                break;
5496
#ifdef TARGET_I386
5497
            case QEMU_OPTION_win2k_hack:
5498
                win2k_install_hack = 1;
5499
                break;
5500
            case QEMU_OPTION_rtc_td_hack:
5501
                rtc_td_hack = 1;
5502
                break;
5503
            case QEMU_OPTION_acpitable:
5504
                if(acpi_table_add(optarg) < 0) {
5505
                    fprintf(stderr, "Wrong acpi table provided\n");
5506
                    exit(1);
5507
                }
5508
                break;
5509
            case QEMU_OPTION_smbios:
5510
                if(smbios_entry_add(optarg) < 0) {
5511
                    fprintf(stderr, "Wrong smbios provided\n");
5512
                    exit(1);
5513
                }
5514
                break;
5515
#endif
5516
#ifdef CONFIG_KVM
5517
            case QEMU_OPTION_enable_kvm:
5518
                kvm_allowed = 1;
5519
                break;
5520
#endif
5521
            case QEMU_OPTION_usb:
5522
                usb_enabled = 1;
5523
                break;
5524
            case QEMU_OPTION_usbdevice:
5525
                usb_enabled = 1;
5526
                add_device_config(DEV_USB, optarg);
5527
                break;
5528
            case QEMU_OPTION_device:
5529
                if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
5530
                    exit(1);
5531
                }
5532
                break;
5533
            case QEMU_OPTION_smp:
5534
                smp_parse(optarg);
5535
                if (smp_cpus < 1) {
5536
                    fprintf(stderr, "Invalid number of CPUs\n");
5537
                    exit(1);
5538
                }
5539
                if (max_cpus < smp_cpus) {
5540
                    fprintf(stderr, "maxcpus must be equal to or greater than "
5541
                            "smp\n");
5542
                    exit(1);
5543
                }
5544
                if (max_cpus > 255) {
5545
                    fprintf(stderr, "Unsupported number of maxcpus\n");
5546
                    exit(1);
5547
                }
5548
                break;
5549
            case QEMU_OPTION_vnc:
5550
                display_type = DT_VNC;
5551
                vnc_display = optarg;
5552
                break;
5553
#ifdef TARGET_I386
5554
            case QEMU_OPTION_no_acpi:
5555
                acpi_enabled = 0;
5556
                break;
5557
            case QEMU_OPTION_no_hpet:
5558
                no_hpet = 1;
5559
                break;
5560
            case QEMU_OPTION_balloon:
5561
                if (balloon_parse(optarg) < 0) {
5562
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5563
                    exit(1);
5564
                }
5565
                break;
5566
#endif
5567
            case QEMU_OPTION_no_reboot:
5568
                no_reboot = 1;
5569
                break;
5570
            case QEMU_OPTION_no_shutdown:
5571
                no_shutdown = 1;
5572
                break;
5573
            case QEMU_OPTION_show_cursor:
5574
                cursor_hide = 0;
5575
                break;
5576
            case QEMU_OPTION_uuid:
5577
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5578
                    fprintf(stderr, "Fail to parse UUID string."
5579
                            " Wrong format.\n");
5580
                    exit(1);
5581
                }
5582
                break;
5583
#ifndef _WIN32
5584
            case QEMU_OPTION_daemonize:
5585
                daemonize = 1;
5586
                break;
5587
#endif
5588
            case QEMU_OPTION_option_rom:
5589
                if (nb_option_roms >= MAX_OPTION_ROMS) {
5590
                    fprintf(stderr, "Too many option ROMs\n");
5591
                    exit(1);
5592
                }
5593
                option_rom[nb_option_roms] = optarg;
5594
                nb_option_roms++;
5595
                break;
5596
#if defined(TARGET_ARM) || defined(TARGET_M68K)
5597
            case QEMU_OPTION_semihosting:
5598
                semihosting_enabled = 1;
5599
                break;
5600
#endif
5601
            case QEMU_OPTION_name:
5602
                qemu_name = qemu_strdup(optarg);
5603
                 {
5604
                     char *p = strchr(qemu_name, ',');
5605
                     if (p != NULL) {
5606
                        *p++ = 0;
5607
                        if (strncmp(p, "process=", 8)) {
5608
                            fprintf(stderr, "Unknown subargument %s to -name", p);
5609
                            exit(1);
5610
                        }
5611
                        p += 8;
5612
                        set_proc_name(p);
5613
                     }        
5614
                 }        
5615
                break;
5616
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
5617
            case QEMU_OPTION_prom_env:
5618
                if (nb_prom_envs >= MAX_PROM_ENVS) {
5619
                    fprintf(stderr, "Too many prom variables\n");
5620
                    exit(1);
5621
                }
5622
                prom_envs[nb_prom_envs] = optarg;
5623
                nb_prom_envs++;
5624
                break;
5625
#endif
5626
#ifdef TARGET_ARM
5627
            case QEMU_OPTION_old_param:
5628
                old_param = 1;
5629
                break;
5630
#endif
5631
            case QEMU_OPTION_clock:
5632
                configure_alarms(optarg);
5633
                break;
5634
            case QEMU_OPTION_startdate:
5635
                configure_rtc_date_offset(optarg, 1);
5636
                break;
5637
            case QEMU_OPTION_rtc:
5638
                opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
5639
                if (!opts) {
5640
                    fprintf(stderr, "parse error: %s\n", optarg);
5641
                    exit(1);
5642
                }
5643
                configure_rtc(opts);
5644
                break;
5645
            case QEMU_OPTION_tb_size:
5646
                tb_size = strtol(optarg, NULL, 0);
5647
                if (tb_size < 0)
5648
                    tb_size = 0;
5649
                break;
5650
            case QEMU_OPTION_icount:
5651
                icount_option = optarg;
5652
                break;
5653
            case QEMU_OPTION_incoming:
5654
                incoming = optarg;
5655
                break;
5656
            case QEMU_OPTION_nodefaults:
5657
                default_serial = 0;
5658
                default_parallel = 0;
5659
                default_virtcon = 0;
5660
                default_monitor = 0;
5661
                default_vga = 0;
5662
                default_net = 0;
5663
                default_floppy = 0;
5664
                default_cdrom = 0;
5665
                default_sdcard = 0;
5666
                break;
5667
#ifndef _WIN32
5668
            case QEMU_OPTION_chroot:
5669
                chroot_dir = optarg;
5670
                break;
5671
            case QEMU_OPTION_runas:
5672
                run_as = optarg;
5673
                break;
5674
#endif
5675
#ifdef CONFIG_XEN
5676
            case QEMU_OPTION_xen_domid:
5677
                xen_domid = atoi(optarg);
5678
                break;
5679
            case QEMU_OPTION_xen_create:
5680
                xen_mode = XEN_CREATE;
5681
                break;
5682
            case QEMU_OPTION_xen_attach:
5683
                xen_mode = XEN_ATTACH;
5684
                break;
5685
#endif
5686
            case QEMU_OPTION_readconfig:
5687
                {
5688
                    FILE *fp;
5689
                    fp = fopen(optarg, "r");
5690
                    if (fp == NULL) {
5691
                        fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5692
                        exit(1);
5693
                    }
5694
                    if (qemu_config_parse(fp, optarg) != 0) {
5695
                        exit(1);
5696
                    }
5697
                    fclose(fp);
5698
                    break;
5699
                }
5700
            case QEMU_OPTION_writeconfig:
5701
                {
5702
                    FILE *fp;
5703
                    if (strcmp(optarg, "-") == 0) {
5704
                        fp = stdout;
5705
                    } else {
5706
                        fp = fopen(optarg, "w");
5707
                        if (fp == NULL) {
5708
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5709
                            exit(1);
5710
                        }
5711
                    }
5712
                    qemu_config_write(fp);
5713
                    fclose(fp);
5714
                    break;
5715
                }
5716
            }
5717
        }
5718
    }
5719
    loc_set_none();
5720

    
5721
    /* If no data_dir is specified then try to find it relative to the
5722
       executable path.  */
5723
    if (!data_dir) {
5724
        data_dir = find_datadir(argv[0]);
5725
    }
5726
    /* If all else fails use the install patch specified when building.  */
5727
    if (!data_dir) {
5728
        data_dir = CONFIG_QEMU_SHAREDIR;
5729
    }
5730

    
5731
    /*
5732
     * Default to max_cpus = smp_cpus, in case the user doesn't
5733
     * specify a max_cpus value.
5734
     */
5735
    if (!max_cpus)
5736
        max_cpus = smp_cpus;
5737

    
5738
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5739
    if (smp_cpus > machine->max_cpus) {
5740
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5741
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
5742
                machine->max_cpus);
5743
        exit(1);
5744
    }
5745

    
5746
    qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
5747
    qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
5748

    
5749
    if (machine->no_serial) {
5750
        default_serial = 0;
5751
    }
5752
    if (machine->no_parallel) {
5753
        default_parallel = 0;
5754
    }
5755
    if (!machine->use_virtcon) {
5756
        default_virtcon = 0;
5757
    }
5758
    if (machine->no_vga) {
5759
        default_vga = 0;
5760
    }
5761
    if (machine->no_floppy) {
5762
        default_floppy = 0;
5763
    }
5764
    if (machine->no_cdrom) {
5765
        default_cdrom = 0;
5766
    }
5767
    if (machine->no_sdcard) {
5768
        default_sdcard = 0;
5769
    }
5770

    
5771
    if (display_type == DT_NOGRAPHIC) {
5772
        if (default_parallel)
5773
            add_device_config(DEV_PARALLEL, "null");
5774
        if (default_serial && default_monitor) {
5775
            add_device_config(DEV_SERIAL, "mon:stdio");
5776
        } else if (default_virtcon && default_monitor) {
5777
            add_device_config(DEV_VIRTCON, "mon:stdio");
5778
        } else {
5779
            if (default_serial)
5780
                add_device_config(DEV_SERIAL, "stdio");
5781
            if (default_virtcon)
5782
                add_device_config(DEV_VIRTCON, "stdio");
5783
            if (default_monitor)
5784
                monitor_parse("stdio", "readline");
5785
        }
5786
    } else {
5787
        if (default_serial)
5788
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
5789
        if (default_parallel)
5790
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
5791
        if (default_monitor)
5792
            monitor_parse("vc:80Cx24C", "readline");
5793
        if (default_virtcon)
5794
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
5795
    }
5796
    if (default_vga)
5797
        vga_interface_type = VGA_CIRRUS;
5798

    
5799
    if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
5800
        exit(1);
5801

    
5802
#ifndef _WIN32
5803
    if (daemonize) {
5804
        pid_t pid;
5805

    
5806
        if (pipe(fds) == -1)
5807
            exit(1);
5808

    
5809
        pid = fork();
5810
        if (pid > 0) {
5811
            uint8_t status;
5812
            ssize_t len;
5813

    
5814
            close(fds[1]);
5815

    
5816
        again:
5817
            len = read(fds[0], &status, 1);
5818
            if (len == -1 && (errno == EINTR))
5819
                goto again;
5820

    
5821
            if (len != 1)
5822
                exit(1);
5823
            else if (status == 1) {
5824
                fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
5825
                exit(1);
5826
            } else
5827
                exit(0);
5828
        } else if (pid < 0)
5829
            exit(1);
5830

    
5831
        close(fds[0]);
5832
        qemu_set_cloexec(fds[1]);
5833

    
5834
        setsid();
5835

    
5836
        pid = fork();
5837
        if (pid > 0)
5838
            exit(0);
5839
        else if (pid < 0)
5840
            exit(1);
5841

    
5842
        umask(027);
5843

    
5844
        signal(SIGTSTP, SIG_IGN);
5845
        signal(SIGTTOU, SIG_IGN);
5846
        signal(SIGTTIN, SIG_IGN);
5847
    }
5848
#endif
5849

    
5850
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5851
#ifndef _WIN32
5852
        if (daemonize) {
5853
            uint8_t status = 1;
5854
            if (write(fds[1], &status, 1) != 1) {
5855
                perror("daemonize. Writing to pipe\n");
5856
            }
5857
        } else
5858
#endif
5859
            fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
5860
        exit(1);
5861
    }
5862

    
5863
    if (kvm_enabled()) {
5864
        int ret;
5865

    
5866
        ret = kvm_init(smp_cpus);
5867
        if (ret < 0) {
5868
            fprintf(stderr, "failed to initialize KVM\n");
5869
            exit(1);
5870
        }
5871
    }
5872

    
5873
    if (qemu_init_main_loop()) {
5874
        fprintf(stderr, "qemu_init_main_loop failed\n");
5875
        exit(1);
5876
    }
5877
    linux_boot = (kernel_filename != NULL);
5878

    
5879
    if (!linux_boot && *kernel_cmdline != '\0') {
5880
        fprintf(stderr, "-append only allowed with -kernel option\n");
5881
        exit(1);
5882
    }
5883

    
5884
    if (!linux_boot && initrd_filename != NULL) {
5885
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
5886
        exit(1);
5887
    }
5888

    
5889
#ifndef _WIN32
5890
    /* Win32 doesn't support line-buffering and requires size >= 2 */
5891
    setvbuf(stdout, NULL, _IOLBF, 0);
5892
#endif
5893

    
5894
    if (init_timer_alarm() < 0) {
5895
        fprintf(stderr, "could not initialize alarm timer\n");
5896
        exit(1);
5897
    }
5898
    configure_icount(icount_option);
5899

    
5900
#ifdef _WIN32
5901
    socket_init();
5902
#endif
5903

    
5904
    if (net_init_clients() < 0) {
5905
        exit(1);
5906
    }
5907

    
5908
    net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5909
    net_set_boot_mask(net_boot);
5910

    
5911
    /* init the bluetooth world */
5912
    if (foreach_device_config(DEV_BT, bt_parse))
5913
        exit(1);
5914

    
5915
    /* init the memory */
5916
    if (ram_size == 0)
5917
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5918

    
5919
    /* init the dynamic translator */
5920
    cpu_exec_init_all(tb_size * 1024 * 1024);
5921

    
5922
    bdrv_init_with_whitelist();
5923

    
5924
    blk_mig_init();
5925

    
5926
    if (default_cdrom) {
5927
        /* we always create the cdrom drive, even if no disk is there */
5928
        drive_add(NULL, CDROM_ALIAS);
5929
    }
5930

    
5931
    if (default_floppy) {
5932
        /* we always create at least one floppy */
5933
        drive_add(NULL, FD_ALIAS, 0);
5934
    }
5935

    
5936
    if (default_sdcard) {
5937
        /* we always create one sd slot, even if no card is in it */
5938
        drive_add(NULL, SD_ALIAS);
5939
    }
5940

    
5941
    /* open the virtual block devices */
5942
    if (snapshot)
5943
        qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5944
    if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
5945
        exit(1);
5946

    
5947
    register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, 
5948
                         ram_load, NULL);
5949

    
5950
    if (nb_numa_nodes > 0) {
5951
        int i;
5952

    
5953
        if (nb_numa_nodes > smp_cpus) {
5954
            nb_numa_nodes = smp_cpus;
5955
        }
5956

    
5957
        /* If no memory size if given for any node, assume the default case
5958
         * and distribute the available memory equally across all nodes
5959
         */
5960
        for (i = 0; i < nb_numa_nodes; i++) {
5961
            if (node_mem[i] != 0)
5962
                break;
5963
        }
5964
        if (i == nb_numa_nodes) {
5965
            uint64_t usedmem = 0;
5966

    
5967
            /* On Linux, the each node's border has to be 8MB aligned,
5968
             * the final node gets the rest.
5969
             */
5970
            for (i = 0; i < nb_numa_nodes - 1; i++) {
5971
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5972
                usedmem += node_mem[i];
5973
            }
5974
            node_mem[i] = ram_size - usedmem;
5975
        }
5976

    
5977
        for (i = 0; i < nb_numa_nodes; i++) {
5978
            if (node_cpumask[i] != 0)
5979
                break;
5980
        }
5981
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
5982
         * must cope with this anyway, because there are BIOSes out there in
5983
         * real machines which also use this scheme.
5984
         */
5985
        if (i == nb_numa_nodes) {
5986
            for (i = 0; i < smp_cpus; i++) {
5987
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
5988
            }
5989
        }
5990
    }
5991

    
5992
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
5993
        exit(1);
5994
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
5995
        exit(1);
5996
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
5997
        exit(1);
5998
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
5999
        exit(1);
6000

    
6001
    module_call_init(MODULE_INIT_DEVICE);
6002

    
6003
    if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
6004
        exit(0);
6005

    
6006
    if (watchdog) {
6007
        i = select_watchdog(watchdog);
6008
        if (i > 0)
6009
            exit (i == 1 ? 1 : 0);
6010
    }
6011

    
6012
    if (machine->compat_props) {
6013
        qdev_prop_register_global_list(machine->compat_props);
6014
    }
6015
    qemu_add_globals();
6016

    
6017
    machine->init(ram_size, boot_devices,
6018
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6019

    
6020
    cpu_synchronize_all_post_init();
6021

    
6022
#ifndef _WIN32
6023
    /* must be after terminal init, SDL library changes signal handlers */
6024
    sighandler_setup();
6025
#endif
6026

    
6027
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
6028
        for (i = 0; i < nb_numa_nodes; i++) {
6029
            if (node_cpumask[i] & (1 << env->cpu_index)) {
6030
                env->numa_node = i;
6031
            }
6032
        }
6033
    }
6034

    
6035
    current_machine = machine;
6036

    
6037
    /* init USB devices */
6038
    if (usb_enabled) {
6039
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
6040
            exit(1);
6041
    }
6042

    
6043
    /* init generic devices */
6044
    if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
6045
        exit(1);
6046

    
6047
    net_check_clients();
6048

    
6049
    /* just use the first displaystate for the moment */
6050
    ds = get_displaystate();
6051

    
6052
    if (display_type == DT_DEFAULT) {
6053
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6054
        display_type = DT_SDL;
6055
#else
6056
        display_type = DT_VNC;
6057
        vnc_display = "localhost:0,to=99";
6058
        show_vnc_port = 1;
6059
#endif
6060
    }
6061
        
6062

    
6063
    switch (display_type) {
6064
    case DT_NOGRAPHIC:
6065
        break;
6066
#if defined(CONFIG_CURSES)
6067
    case DT_CURSES:
6068
        curses_display_init(ds, full_screen);
6069
        break;
6070
#endif
6071
#if defined(CONFIG_SDL)
6072
    case DT_SDL:
6073
        sdl_display_init(ds, full_screen, no_frame);
6074
        break;
6075
#elif defined(CONFIG_COCOA)
6076
    case DT_SDL:
6077
        cocoa_display_init(ds, full_screen);
6078
        break;
6079
#endif
6080
    case DT_VNC:
6081
        vnc_display_init(ds);
6082
        if (vnc_display_open(ds, vnc_display) < 0)
6083
            exit(1);
6084

    
6085
        if (show_vnc_port) {
6086
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
6087
        }
6088
        break;
6089
    default:
6090
        break;
6091
    }
6092
    dpy_resize(ds);
6093

    
6094
    dcl = ds->listeners;
6095
    while (dcl != NULL) {
6096
        if (dcl->dpy_refresh != NULL) {
6097
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6098
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
6099
        }
6100
        dcl = dcl->next;
6101
    }
6102

    
6103
    if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
6104
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6105
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6106
    }
6107

    
6108
    text_consoles_set_display(ds);
6109

    
6110
    if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
6111
        exit(1);
6112

    
6113
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6114
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6115
                gdbstub_dev);
6116
        exit(1);
6117
    }
6118

    
6119
    qdev_machine_creation_done();
6120

    
6121
    if (rom_load_all() != 0) {
6122
        fprintf(stderr, "rom loading failed\n");
6123
        exit(1);
6124
    }
6125

    
6126
    qemu_system_reset();
6127
    if (loadvm) {
6128
        if (load_vmstate(loadvm) < 0) {
6129
            autostart = 0;
6130
        }
6131
    }
6132

    
6133
    if (incoming) {
6134
        qemu_start_incoming_migration(incoming);
6135
    } else if (autostart) {
6136
        vm_start();
6137
    }
6138

    
6139
#ifndef _WIN32
6140
    if (daemonize) {
6141
        uint8_t status = 0;
6142
        ssize_t len;
6143

    
6144
    again1:
6145
        len = write(fds[1], &status, 1);
6146
        if (len == -1 && (errno == EINTR))
6147
            goto again1;
6148

    
6149
        if (len != 1)
6150
            exit(1);
6151

    
6152
        if (chdir("/")) {
6153
            perror("not able to chdir to /");
6154
            exit(1);
6155
        }
6156
        TFR(fd = qemu_open("/dev/null", O_RDWR));
6157
        if (fd == -1)
6158
            exit(1);
6159
    }
6160

    
6161
    if (run_as) {
6162
        pwd = getpwnam(run_as);
6163
        if (!pwd) {
6164
            fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6165
            exit(1);
6166
        }
6167
    }
6168

    
6169
    if (chroot_dir) {
6170
        if (chroot(chroot_dir) < 0) {
6171
            fprintf(stderr, "chroot failed\n");
6172
            exit(1);
6173
        }
6174
        if (chdir("/")) {
6175
            perror("not able to chdir to /");
6176
            exit(1);
6177
        }
6178
    }
6179

    
6180
    if (run_as) {
6181
        if (setgid(pwd->pw_gid) < 0) {
6182
            fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6183
            exit(1);
6184
        }
6185
        if (setuid(pwd->pw_uid) < 0) {
6186
            fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6187
            exit(1);
6188
        }
6189
        if (setuid(0) != -1) {
6190
            fprintf(stderr, "Dropping privileges failed\n");
6191
            exit(1);
6192
        }
6193
    }
6194

    
6195
    if (daemonize) {
6196
        dup2(fd, 0);
6197
        dup2(fd, 1);
6198
        dup2(fd, 2);
6199

    
6200
        close(fd);
6201
    }
6202
#endif
6203

    
6204
    main_loop();
6205
    quit_timers();
6206
    net_cleanup();
6207

    
6208
    return 0;
6209
}